aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenichi Handa1998-01-22 01:26:45 +0000
committerKenichi Handa1998-01-22 01:26:45 +0000
commitc8a6d68aed98189810b4f2d0b21d69f9f21f539b (patch)
treec1ccd301fc12b7c150c3d7f4073e917768ea039c
parent0ab6a3d89628513c77248a90ecff1e72a044773d (diff)
downloademacs-c8a6d68aed98189810b4f2d0b21d69f9f21f539b.tar.gz
emacs-c8a6d68aed98189810b4f2d0b21d69f9f21f539b.zip
(Finsert_file_contents): Even if
enable-multibyte-characters is nil, we must check `coding:' tag and `file-coding-system-alist' to handle eol-conversion. (Finsert_file_contents): Adjusted for the change of struct coding_system and function decode_coding. For normail file reading, use code_convert_region. (Fwrite_region): Adjusted for the change of struct coding_system. (e_write): Adjusted for the change of encode_coding.
-rw-r--r--src/fileio.c242
1 files changed, 105 insertions, 137 deletions
diff --git a/src/fileio.c b/src/fileio.c
index 102573baf83..5bde062e0ab 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -3143,7 +3143,6 @@ This does code conversion according to the value of\n\
3143 struct stat st; 3143 struct stat st;
3144 register int fd; 3144 register int fd;
3145 int inserted = 0; 3145 int inserted = 0;
3146 int inserted_chars = 0;
3147 register int how_much; 3146 register int how_much;
3148 register int unprocessed; 3147 register int unprocessed;
3149 int count = specpdl_ptr - specpdl; 3148 int count = specpdl_ptr - specpdl;
@@ -3265,8 +3264,6 @@ This does code conversion according to the value of\n\
3265 3264
3266 if (!NILP (Vcoding_system_for_read)) 3265 if (!NILP (Vcoding_system_for_read))
3267 val = Vcoding_system_for_read; 3266 val = Vcoding_system_for_read;
3268 else if (NILP (current_buffer->enable_multibyte_characters))
3269 val = Qemacs_mule;
3270 else 3267 else
3271 { 3268 {
3272 if (! NILP (Vset_auto_coding_function)) 3269 if (! NILP (Vset_auto_coding_function))
@@ -3317,7 +3314,20 @@ This does code conversion according to the value of\n\
3317 if (CONSP (coding_systems)) val = XCONS (coding_systems)->car; 3314 if (CONSP (coding_systems)) val = XCONS (coding_systems)->car;
3318 } 3315 }
3319 } 3316 }
3320 setup_coding_system (Fcheck_coding_system (val), &coding); 3317
3318 if (NILP (Vcoding_system_for_read)
3319 && NILP (current_buffer->enable_multibyte_characters))
3320 {
3321 /* We must suppress all text conversion except for end-of-line
3322 conversion. */
3323 struct coding_system coding_temp;
3324
3325 setup_coding_system (Fcheck_coding_system (val), &coding_temp);
3326 setup_coding_system (Qraw_text, &coding);
3327 coding.eol_type = coding_temp.eol_type;
3328 }
3329 else
3330 setup_coding_system (Fcheck_coding_system (val), &coding);
3321 } 3331 }
3322 3332
3323 /* If requested, replace the accessible part of the buffer 3333 /* If requested, replace the accessible part of the buffer
@@ -3458,8 +3468,7 @@ This does code conversion according to the value of\n\
3458 if (same_at_end > same_at_start 3468 if (same_at_end > same_at_start
3459 && FETCH_BYTE (same_at_end - 1) >= 0200 3469 && FETCH_BYTE (same_at_end - 1) >= 0200
3460 && ! NILP (current_buffer->enable_multibyte_characters) 3470 && ! NILP (current_buffer->enable_multibyte_characters)
3461 && (CODING_REQUIRE_DECODING (&coding) 3471 && (CODING_MAY_REQUIRE_DECODING (&coding)))
3462 || CODING_REQUIRE_DETECTION (&coding)))
3463 giveup_match_end = 1; 3472 giveup_match_end = 1;
3464 break; 3473 break;
3465 } 3474 }
@@ -3559,10 +3568,9 @@ This does code conversion according to the value of\n\
3559 3568
3560 how_much += this; 3569 how_much += this;
3561 3570
3562 if (CODING_REQUIRE_DECODING (&coding) 3571 if (CODING_MAY_REQUIRE_DECODING (&coding))
3563 || CODING_REQUIRE_DETECTION (&coding))
3564 { 3572 {
3565 int require, produced, consumed; 3573 int require, result;
3566 3574
3567 this += unprocessed; 3575 this += unprocessed;
3568 3576
@@ -3577,22 +3585,21 @@ This does code conversion according to the value of\n\
3577 3585
3578 /* Convert this batch with results in CONVERSION_BUFFER. */ 3586 /* Convert this batch with results in CONVERSION_BUFFER. */
3579 if (how_much >= total) /* This is the last block. */ 3587 if (how_much >= total) /* This is the last block. */
3580 coding.last_block = 1; 3588 coding.mode |= CODING_MODE_LAST_BLOCK;
3581 produced = decode_coding (&coding, read_buf, 3589 result = decode_coding (&coding, read_buf,
3582 conversion_buffer + inserted, 3590 conversion_buffer + inserted,
3583 this, bufsize - inserted, 3591 this, bufsize - inserted);
3584 &consumed);
3585 3592
3586 /* Save for next iteration whatever we didn't convert. */ 3593 /* Save for next iteration whatever we didn't convert. */
3587 unprocessed = this - consumed; 3594 unprocessed = this - coding.consumed;
3588 bcopy (read_buf + consumed, read_buf, unprocessed); 3595 bcopy (read_buf + coding.consumed, read_buf, unprocessed);
3589 this = produced; 3596 this = coding.produced;
3590 } 3597 }
3591 3598
3592 inserted += this; 3599 inserted += this;
3593 } 3600 }
3594 3601
3595 /* At this point, INSERTED is how many characters 3602 /* At this point, INSERTED is how many characters (i.e. bytes)
3596 are present in CONVERSION_BUFFER. 3603 are present in CONVERSION_BUFFER.
3597 HOW_MUCH should equal TOTAL, 3604 HOW_MUCH should equal TOTAL,
3598 or should be <= 0 if we couldn't read the file. */ 3605 or should be <= 0 if we couldn't read the file. */
@@ -3696,35 +3703,44 @@ This does code conversion according to the value of\n\
3696 } 3703 }
3697 3704
3698 /* In the following loop, HOW_MUCH contains the total bytes read so 3705 /* In the following loop, HOW_MUCH contains the total bytes read so
3699 far. Before exiting the loop, it is set to -1 if I/O error 3706 far for a regular file, and not changed for a special file. But,
3700 occurs, set to -2 if the maximum buffer size is exceeded. */ 3707 before exiting the loop, it is set to a negative value if I/O
3708 error occurs. */
3701 how_much = 0; 3709 how_much = 0;
3702 /* Total bytes inserted. */ 3710 /* Total bytes inserted. */
3703 inserted = 0; 3711 inserted = 0;
3704 /* Bytes not processed in the previous loop because short gap size. */ 3712 /* Here, we don't do code conversion in the loop. It is done by
3705 unprocessed = 0; 3713 code_convert_region after all data are read into the buffer. */
3706 while (how_much < total) 3714 while (how_much < total)
3707 { 3715 {
3708 /* try is reserved in some compilers (Microsoft C) */ 3716 /* try is reserved in some compilers (Microsoft C) */
3709 int trytry = min (total - how_much, READ_BUF_SIZE - unprocessed); 3717 int trytry = min (total - how_much, READ_BUF_SIZE);
3710 char *destination = (! (CODING_REQUIRE_DECODING (&coding) 3718 int this;
3711 || CODING_REQUIRE_DETECTION (&coding)) 3719
3712 ? (char *) (BYTE_POS_ADDR (PT_BYTE + inserted - 1) + 1) 3720 /* For a special file, GAP_SIZE should be checked every time. */
3713 : read_buf + unprocessed); 3721 if (not_regular && GAP_SIZE < trytry)
3714 int this, this_chars; 3722 make_gap (total - GAP_SIZE);
3715 3723
3716 /* Allow quitting out of the actual I/O. */ 3724 /* Allow quitting out of the actual I/O. */
3717 immediate_quit = 1; 3725 immediate_quit = 1;
3718 QUIT; 3726 QUIT;
3719 this = read (fd, destination, trytry); 3727 this = read (fd, BYTE_POS_ADDR (PT + inserted - 1) + 1, trytry);
3720 immediate_quit = 0; 3728 immediate_quit = 0;
3721 3729
3722 if (this < 0 || this + unprocessed == 0) 3730 if (this <= 0)
3723 { 3731 {
3724 how_much = this; 3732 how_much = this;
3725 break; 3733 break;
3726 } 3734 }
3727 3735
3736 GAP_SIZE -= this;
3737 GPT_BYTE += this;
3738 ZV_BYTE += this;
3739 Z_BYTE += this;
3740 GPT += this;
3741 ZV += this;
3742 Z += this;
3743
3728 /* For a regular file, where TOTAL is the real size, 3744 /* For a regular file, where TOTAL is the real size,
3729 count HOW_MUCH to compare with it. 3745 count HOW_MUCH to compare with it.
3730 For a special file, where TOTAL is just a buffer size, 3746 For a special file, where TOTAL is just a buffer size,
@@ -3732,103 +3748,56 @@ This does code conversion according to the value of\n\
3732 (INSERTED is where we count the number of characters inserted.) */ 3748 (INSERTED is where we count the number of characters inserted.) */
3733 if (! not_regular) 3749 if (! not_regular)
3734 how_much += this; 3750 how_much += this;
3751 inserted += this;
3752 }
3735 3753
3736 if (CODING_REQUIRE_DECODING (&coding) 3754 if (GAP_SIZE > 0)
3737 || CODING_REQUIRE_DETECTION (&coding)) 3755 /* Put an anchor to ensure multi-byte form ends at gap. */
3738 { 3756 *GPT_ADDR = 0;
3739 int require, produced, consumed;
3740
3741 this += unprocessed;
3742 /* Make sure that the gap is large enough. */
3743 require = decoding_buffer_size (&coding, this);
3744 if (GAP_SIZE < require)
3745 make_gap (require - GAP_SIZE);
3746
3747 if (! not_regular)
3748 {
3749 if (how_much >= total) /* This is the last block. */
3750 coding.last_block = 1;
3751 }
3752 else
3753 {
3754 /* If we encounter EOF, say it is the last block. (The
3755 data this will apply to is the UNPROCESSED characters
3756 carried over from the last batch.) */
3757 if (this == 0)
3758 coding.last_block = 1;
3759 }
3760 3757
3761 produced = decode_coding (&coding, read_buf, 3758 close (fd);
3762 BYTE_POS_ADDR (PT_BYTE + inserted - 1) + 1,
3763 this, GAP_SIZE, &consumed);
3764 if (produced > 0)
3765 {
3766 Lisp_Object temp;
3767 3759
3768 XSET (temp, Lisp_Int, Z_BYTE + produced); 3760 /* Discard the unwind protect for closing the file. */
3769 if (Z_BYTE + produced != XINT (temp)) 3761 specpdl_ptr--;
3770 {
3771 how_much = -2;
3772 break;
3773 }
3774 }
3775 unprocessed = this - consumed;
3776 bcopy (read_buf + consumed, read_buf, unprocessed);
3777 this = produced;
3778 this_chars = chars_in_text (BYTE_POS_ADDR (PT_BYTE + inserted - 1) + 1,
3779 produced);
3780 }
3781 else if (! NILP (current_buffer->enable_multibyte_characters))
3782 this_chars = chars_in_text (BYTE_POS_ADDR (PT_BYTE + inserted - 1) + 1,
3783 this);
3784 else
3785 this_chars = this;
3786 3762
3787 GAP_SIZE -= this; 3763 if (how_much < 0)
3788 GPT_BYTE += this; 3764 error ("IO error reading %s: %s",
3789 ZV_BYTE += this; 3765 XSTRING (orig_filename)->data, strerror (errno));
3790 Z_BYTE += this;
3791 GPT += this_chars;
3792 ZV += this_chars;
3793 Z += this_chars;
3794 3766
3795 if (GAP_SIZE > 0) 3767 if (inserted > 0)
3796 /* Put an anchor to ensure multi-byte form ends at gap. */ 3768 {
3797 *GPT_ADDR = 0; 3769 if (CODING_MAY_REQUIRE_DECODING (&coding))
3798 inserted += this; 3770 inserted = code_convert_region (PT, PT + inserted, &coding, 0, 0);
3799 inserted_chars += this_chars;
3800 }
3801 3771
3802#ifdef DOS_NT 3772#ifdef DOS_NT
3803 /* Use the conversion type to determine buffer-file-type 3773 /* Use the conversion type to determine buffer-file-type
3804 (find-buffer-file-type is now used to help determine the 3774 (find-buffer-file-type is now used to help determine the
3805 conversion). */ 3775 conversion). */
3806 if (coding.eol_type != CODING_EOL_UNDECIDED 3776 if (coding.eol_type != CODING_EOL_UNDECIDED
3807 && coding.eol_type != CODING_EOL_LF) 3777 && coding.eol_type != CODING_EOL_LF)
3808 current_buffer->buffer_file_type = Qnil; 3778 current_buffer->buffer_file_type = Qnil;
3809 else 3779 else
3810 current_buffer->buffer_file_type = Qt; 3780 current_buffer->buffer_file_type = Qt;
3811#endif 3781#endif
3812 3782
3813 if (inserted > 0)
3814 {
3815 record_insert (PT, inserted_chars); 3783 record_insert (PT, inserted_chars);
3816 3784
3817 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */ 3785 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
3818 offset_intervals (current_buffer, PT, inserted_chars); 3786 offset_intervals (current_buffer, PT, inserted_chars);
3819 MODIFF++; 3787 MODIFF++;
3820 }
3821
3822 close (fd);
3823 3788
3824 /* Discard the unwind protect for closing the file. */ 3789 if (! NILP (coding.post_read_conversion))
3825 specpdl_ptr--; 3790 {
3791 Lisp_Object val;
3826 3792
3827 if (how_much == -1) 3793 val = call1 (coding.post_read_conversion, make_number (inserted));
3828 error ("IO error reading %s: %s", 3794 if (!NILP (val))
3829 XSTRING (orig_filename)->data, strerror (errno)); 3795 {
3830 else if (how_much == -2) 3796 CHECK_NUMBER (val, 0);
3831 error ("Maximum buffer size exceeded"); 3797 inserted = XFASTINT (val);
3798 }
3799 }
3800 }
3832 3801
3833 set_coding_system = 1; 3802 set_coding_system = 1;
3834 3803
@@ -3871,20 +3840,20 @@ This does code conversion according to the value of\n\
3871 } 3840 }
3872 3841
3873 /* Decode file format */ 3842 /* Decode file format */
3874 if (inserted_chars > 0) 3843 if (inserted > 0)
3875 { 3844 {
3876 insval = call3 (Qformat_decode, 3845 insval = call3 (Qformat_decode,
3877 Qnil, make_number (inserted_chars), visit); 3846 Qnil, make_number (inserted), visit);
3878 CHECK_NUMBER (insval, 0); 3847 CHECK_NUMBER (insval, 0);
3879 inserted_chars = XFASTINT (insval); 3848 inserted = XFASTINT (insval);
3880 } 3849 }
3881 3850
3882 /* Call after-change hooks for the inserted text, aside from the case 3851 /* Call after-change hooks for the inserted text, aside from the case
3883 of normal visiting (not with REPLACE), which is done in a new buffer 3852 of normal visiting (not with REPLACE), which is done in a new buffer
3884 "before" the buffer is changed. */ 3853 "before" the buffer is changed. */
3885 if (inserted_chars > 0 && total > 0 3854 if (inserted > 0 && total > 0
3886 && (NILP (visit) || !NILP (replace))) 3855 && (NILP (visit) || !NILP (replace)))
3887 signal_after_change (PT, 0, inserted_chars); 3856 signal_after_change (PT, 0, inserted);
3888 3857
3889 if (set_coding_system) 3858 if (set_coding_system)
3890 Vlast_coding_system_used = coding.symbol; 3859 Vlast_coding_system_used = coding.symbol;
@@ -3892,16 +3861,13 @@ This does code conversion according to the value of\n\
3892 if (inserted > 0) 3861 if (inserted > 0)
3893 { 3862 {
3894 p = Vafter_insert_file_functions; 3863 p = Vafter_insert_file_functions;
3895 if (!NILP (coding.post_read_conversion))
3896 p = Fcons (coding.post_read_conversion, p);
3897
3898 while (!NILP (p)) 3864 while (!NILP (p))
3899 { 3865 {
3900 insval = call1 (Fcar (p), make_number (inserted_chars)); 3866 insval = call1 (Fcar (p), make_number (inserted));
3901 if (!NILP (insval)) 3867 if (!NILP (insval))
3902 { 3868 {
3903 CHECK_NUMBER (insval, 0); 3869 CHECK_NUMBER (insval, 0);
3904 inserted_chars = XFASTINT (insval); 3870 inserted = XFASTINT (insval);
3905 } 3871 }
3906 QUIT; 3872 QUIT;
3907 p = Fcdr (p); 3873 p = Fcdr (p);
@@ -4010,7 +3976,7 @@ to the file, instead of any buffer contents, and END is ignored.")
4010 had better write it out with the same coding system even if 3976 had better write it out with the same coding system even if
4011 `enable-multibyte-characters' is nil. 3977 `enable-multibyte-characters' is nil.
4012 3978
4013 If is is not set locally, we anyway have to convert EOL 3979 If it is not set locally, we anyway have to convert EOL
4014 format if the default value of `buffer-file-coding-system' 3980 format if the default value of `buffer-file-coding-system'
4015 tells that it is not Unix-like (LF only) format. */ 3981 tells that it is not Unix-like (LF only) format. */
4016 val = current_buffer->buffer_file_coding_system; 3982 val = current_buffer->buffer_file_coding_system;
@@ -4022,7 +3988,7 @@ to the file, instead of any buffer contents, and END is ignored.")
4022 if (coding_temp.eol_type == CODING_EOL_CRLF 3988 if (coding_temp.eol_type == CODING_EOL_CRLF
4023 || coding_temp.eol_type == CODING_EOL_CR) 3989 || coding_temp.eol_type == CODING_EOL_CR)
4024 { 3990 {
4025 setup_coding_system (Qemacs_mule, &coding); 3991 setup_coding_system (Qraw_text, &coding);
4026 coding.eol_type = coding_temp.eol_type; 3992 coding.eol_type = coding_temp.eol_type;
4027 goto done_setup_coding; 3993 goto done_setup_coding;
4028 } 3994 }
@@ -4033,19 +3999,22 @@ to the file, instead of any buffer contents, and END is ignored.")
4033 { 3999 {
4034 Lisp_Object args[7], coding_systems; 4000 Lisp_Object args[7], coding_systems;
4035 4001
4036 args[0] = Qwrite_region, args[1] = start, args[2] = end, 4002 args[0] = Qwrite_region; args[1] = start; args[2] = end;
4037 args[3] = filename, args[4] = append, args[5] = visit, 4003 args[3] = filename; args[4] = append; args[5] = visit;
4038 args[6] = lockname; 4004 args[6] = lockname;
4039 coding_systems = Ffind_operation_coding_system (7, args); 4005 coding_systems = Ffind_operation_coding_system (7, args);
4040 val = (CONSP (coding_systems) && !NILP (XCONS (coding_systems)->cdr) 4006 val = (CONSP (coding_systems) && !NILP (XCONS (coding_systems)->cdr)
4041 ? XCONS (coding_systems)->cdr 4007 ? XCONS (coding_systems)->cdr
4042 : current_buffer->buffer_file_coding_system); 4008 : current_buffer->buffer_file_coding_system);
4009 /* Confirm that VAL can surely encode the current region. */
4010 if (Ffboundp (Vselect_safe_coding_system_function))
4011 val = call3 (Vselect_safe_coding_system_function, start, end, val);
4043 } 4012 }
4044 setup_coding_system (Fcheck_coding_system (val), &coding); 4013 setup_coding_system (Fcheck_coding_system (val), &coding);
4045 4014
4046 done_setup_coding: 4015 done_setup_coding:
4047 if (!STRINGP (start) && !NILP (current_buffer->selective_display)) 4016 if (!STRINGP (start) && !NILP (current_buffer->selective_display))
4048 coding.selective = 1; 4017 coding.mode |= CODING_MODE_SELECTIVE_DISPLAY;
4049 } 4018 }
4050 4019
4051 Vlast_coding_system_used = coding.symbol; 4020 Vlast_coding_system_used = coding.symbol;
@@ -4279,16 +4248,17 @@ to the file, instead of any buffer contents, and END is ignored.")
4279 else 4248 else
4280 { 4249 {
4281 /* If file was empty, still need to write the annotations */ 4250 /* If file was empty, still need to write the annotations */
4282 coding.last_block = 1; 4251 coding.mode |= CODING_MODE_LAST_BLOCK;
4283 failure = 0 > a_write (desc, "", 0, XINT (start), &annotations, &coding); 4252 failure = 0 > a_write (desc, "", 0, XINT (start), &annotations, &coding);
4284 save_errno = errno; 4253 save_errno = errno;
4285 } 4254 }
4286 4255
4287 if (CODING_REQUIRE_FLUSHING (&coding) && !coding.last_block 4256 if (CODING_REQUIRE_FLUSHING (&coding)
4257 && !(coding.mode & CODING_MODE_LAST_BLOCK)
4288 && ! failure) 4258 && ! failure)
4289 { 4259 {
4290 /* We have to flush out a data. */ 4260 /* We have to flush out a data. */
4291 coding.last_block = 1; 4261 coding.mode |= CODING_MODE_LAST_BLOCK;
4292 failure = 0 > e_write (desc, "", 0, &coding); 4262 failure = 0 > e_write (desc, "", 0, &coding);
4293 save_errno = errno; 4263 save_errno = errno;
4294 } 4264 }
@@ -4542,19 +4512,17 @@ e_write (desc, addr, nbytes, coding)
4542 struct coding_system *coding; 4512 struct coding_system *coding;
4543{ 4513{
4544 char buf[WRITE_BUF_SIZE]; 4514 char buf[WRITE_BUF_SIZE];
4545 int produced, consumed;
4546 4515
4547 /* We used to have a code for handling selective display here. But, 4516 /* We used to have a code for handling selective display here. But,
4548 now it is handled within encode_coding. */ 4517 now it is handled within encode_coding. */
4549 while (1) 4518 while (1)
4550 { 4519 {
4551 produced = encode_coding (coding, addr, buf, nbytes, WRITE_BUF_SIZE, 4520 encode_coding (coding, addr, buf, nbytes, WRITE_BUF_SIZE);
4552 &consumed); 4521 nbytes -= coding->consumed, addr += coding->consumed;
4553 nbytes -= consumed, addr += consumed; 4522 if (coding->produced > 0)
4554 if (produced > 0)
4555 { 4523 {
4556 produced -= write (desc, buf, produced); 4524 coding->produced -= write (desc, buf, coding->produced);
4557 if (produced) return -1; 4525 if (coding->produced) return -1;
4558 } 4526 }
4559 if (nbytes <= 0) 4527 if (nbytes <= 0)
4560 break; 4528 break;