comparison src/coding.c @ 26067:f54ca66e2571

(code_convert_string): Add record_unwind_protect to assure setting inhibit_pre_post_conversion back to zero. Take care of the multibyteness of the working buffer. (inhibit_pre_post_conversion): New variable. (setup_coding_system): If inhibit_pre_post_conversion is nonzero, ignore post-read-conversion and pre-write-conversion property of the coding system. (code_convert_region_unwind): New function. (code_convert_region): Set inhibit_pre_post_conversion to 1 while running pre-write-conversion and post-read-conversion. (code_convert_string): Likewise.
author Kenichi Handa <handa@m17n.org>
date Mon, 18 Oct 1999 01:35:54 +0000
parents 1c730f145aa2
children b7aa6ac26872
comparison
equal deleted inserted replaced
26066:7087c5dd25b6 26067:f54ca66e2571
408 /* Alist of charsets vs revision number. */ 408 /* Alist of charsets vs revision number. */
409 Lisp_Object Vcharset_revision_alist; 409 Lisp_Object Vcharset_revision_alist;
410 410
411 /* Default coding systems used for process I/O. */ 411 /* Default coding systems used for process I/O. */
412 Lisp_Object Vdefault_process_coding_system; 412 Lisp_Object Vdefault_process_coding_system;
413
414 /* Global flag to tell that we can't call post-read-conversion and
415 pre-write-conversion functions. Usually the value is zero, but it
416 is set to 1 temporarily while such functions are running. This is
417 to avoid infinite recursive call. */
418 static int inhibit_pre_post_conversion;
413 419
414 420
415 /*** 2. Emacs internal format (emacs-mule) handlers ***/ 421 /*** 2. Emacs internal format (emacs-mule) handlers ***/
416 422
417 /* Emacs' internal format for encoding multiple character sets is a 423 /* Emacs' internal format for encoding multiple character sets is a
2939 2945
2940 /* Get values of coding system properties: 2946 /* Get values of coding system properties:
2941 `post-read-conversion', `pre-write-conversion', 2947 `post-read-conversion', `pre-write-conversion',
2942 `translation-table-for-decode', `translation-table-for-encode'. */ 2948 `translation-table-for-decode', `translation-table-for-encode'. */
2943 plist = XVECTOR (coding_spec)->contents[3]; 2949 plist = XVECTOR (coding_spec)->contents[3];
2944 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion); 2950 /* Pre & post conversion functions should be disabled if
2945 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion); 2951 inhibit_eol_conversion is nozero. This is the case that a code
2952 conversion function is called while those functions are running. */
2953 if (! inhibit_pre_post_conversion)
2954 {
2955 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
2956 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
2957 }
2946 val = Fplist_get (plist, Qtranslation_table_for_decode); 2958 val = Fplist_get (plist, Qtranslation_table_for_decode);
2947 if (SYMBOLP (val)) 2959 if (SYMBOLP (val))
2948 val = Fget (val, Qtranslation_table_for_decode); 2960 val = Fget (val, Qtranslation_table_for_decode);
2949 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil; 2961 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
2950 val = Fplist_get (plist, Qtranslation_table_for_encode); 2962 val = Fplist_get (plist, Qtranslation_table_for_encode);
4219 if (encodep) shrink_encoding_region (beg, end, coding, str); \ 4231 if (encodep) shrink_encoding_region (beg, end, coding, str); \
4220 else shrink_decoding_region (beg, end, coding, str); \ 4232 else shrink_decoding_region (beg, end, coding, str); \
4221 } \ 4233 } \
4222 } while (0) 4234 } while (0)
4223 4235
4236 static Lisp_Object
4237 code_convert_region_unwind (dummy)
4238 Lisp_Object dummy;
4239 {
4240 inhibit_pre_post_conversion = 0;
4241 return Qnil;
4242 }
4243
4224 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the 4244 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
4225 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by 4245 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
4226 coding system CODING, and return the status code of code conversion 4246 coding system CODING, and return the status code of code conversion
4227 (currently, this value has no meaning). 4247 (currently, this value has no meaning).
4228 4248
4343 { 4363 {
4344 /* The function in pre-write-conversion may put a new text in a 4364 /* The function in pre-write-conversion may put a new text in a
4345 new buffer. */ 4365 new buffer. */
4346 struct buffer *prev = current_buffer; 4366 struct buffer *prev = current_buffer;
4347 Lisp_Object new; 4367 Lisp_Object new;
4348 4368 int count = specpdl_ptr - specpdl;
4369
4370 record_unwind_protect (code_convert_region_unwind, Qnil);
4371 /* We should not call any more pre-write/post-read-conversion
4372 functions while this pre-write-conversion is running. */
4373 inhibit_pre_post_conversion = 1;
4349 call2 (coding->pre_write_conversion, 4374 call2 (coding->pre_write_conversion,
4350 make_number (from), make_number (to)); 4375 make_number (from), make_number (to));
4376 inhibit_pre_post_conversion = 0;
4377 /* Discard the unwind protect. */
4378 specpdl_ptr--;
4379
4351 if (current_buffer != prev) 4380 if (current_buffer != prev)
4352 { 4381 {
4353 len = ZV - BEGV; 4382 len = ZV - BEGV;
4354 new = Fcurrent_buffer (); 4383 new = Fcurrent_buffer ();
4355 set_buffer_internal_1 (prev); 4384 set_buffer_internal_1 (prev);
4624 inserted = Z - prev_Z; 4653 inserted = Z - prev_Z;
4625 4654
4626 if (! encodep && ! NILP (coding->post_read_conversion)) 4655 if (! encodep && ! NILP (coding->post_read_conversion))
4627 { 4656 {
4628 Lisp_Object val; 4657 Lisp_Object val;
4658 int count = specpdl_ptr - specpdl;
4629 4659
4630 if (from != PT) 4660 if (from != PT)
4631 TEMP_SET_PT_BOTH (from, from_byte); 4661 TEMP_SET_PT_BOTH (from, from_byte);
4632 prev_Z = Z; 4662 prev_Z = Z;
4663 record_unwind_protect (code_convert_region_unwind, Qnil);
4664 /* We should not call any more pre-write/post-read-conversion
4665 functions while this post-read-conversion is running. */
4666 inhibit_pre_post_conversion = 1;
4633 val = call1 (coding->post_read_conversion, make_number (inserted)); 4667 val = call1 (coding->post_read_conversion, make_number (inserted));
4668 inhibit_pre_post_conversion = 0;
4669 /* Discard the unwind protect. */
4670 specpdl_ptr--;
4634 CHECK_NUMBER (val, 0); 4671 CHECK_NUMBER (val, 0);
4635 inserted += Z - prev_Z; 4672 inserted += Z - prev_Z;
4636 } 4673 }
4637 4674
4638 if (orig_point >= from) 4675 if (orig_point >= from)
4669 struct gcpro gcpro1; 4706 struct gcpro gcpro1;
4670 Lisp_Object saved_coding_symbol; 4707 Lisp_Object saved_coding_symbol;
4671 int result; 4708 int result;
4672 4709
4673 saved_coding_symbol = Qnil; 4710 saved_coding_symbol = Qnil;
4674 if (encodep && !NILP (coding->pre_write_conversion) 4711 if ((encodep && !NILP (coding->pre_write_conversion)
4675 || !encodep && !NILP (coding->post_read_conversion)) 4712 || !encodep && !NILP (coding->post_read_conversion)))
4676 { 4713 {
4677 /* Since we have to call Lisp functions which assume target text 4714 /* Since we have to call Lisp functions which assume target text
4678 is in a buffer, after setting a temporary buffer, call 4715 is in a buffer, after setting a temporary buffer, call
4679 code_convert_region. */ 4716 code_convert_region. */
4680 int count = specpdl_ptr - specpdl; 4717 int count = specpdl_ptr - specpdl;
4681 struct buffer *prev = current_buffer; 4718 struct buffer *prev = current_buffer;
4719 int multibyte = STRING_MULTIBYTE (str);
4682 4720
4683 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); 4721 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4722 record_unwind_protect (code_convert_region_unwind, Qnil);
4723 inhibit_pre_post_conversion = 1;
4724 GCPRO1 (str);
4684 temp_output_buffer_setup (" *code-converting-work*"); 4725 temp_output_buffer_setup (" *code-converting-work*");
4685 set_buffer_internal (XBUFFER (Vstandard_output)); 4726 set_buffer_internal (XBUFFER (Vstandard_output));
4686 if (encodep) 4727 /* We must insert the contents of STR as is without
4687 insert_from_string (str, 0, 0, to, to_byte, 0); 4728 unibyte<->multibyte conversion. For that, we adjust the
4688 else 4729 multibyteness of the working buffer to that of STR. */
4689 { 4730 Ferase_buffer (); /* for safety */
4690 /* We must insert the contents of STR as is without 4731 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
4691 unibyte<->multibyte conversion. */ 4732 insert_from_string (str, 0, 0, to, to_byte, 0);
4692 current_buffer->enable_multibyte_characters = Qnil; 4733 UNGCPRO;
4693 insert_from_string (str, 0, 0, to_byte, to_byte, 0);
4694 current_buffer->enable_multibyte_characters = Qt;
4695 }
4696 code_convert_region (BEGV, BEGV_BYTE, ZV, ZV_BYTE, coding, encodep, 1); 4734 code_convert_region (BEGV, BEGV_BYTE, ZV, ZV_BYTE, coding, encodep, 1);
4697 if (encodep) 4735 /* Make a unibyte string if we are encoding, otherwise make a
4698 /* We must return the buffer contents as unibyte string. */ 4736 multibyte string. */
4699 current_buffer->enable_multibyte_characters = Qnil; 4737 Fset_buffer_multibyte (encodep ? Qnil : Qt);
4700 str = make_buffer_string (BEGV, ZV, 0); 4738 str = make_buffer_string (BEGV, ZV, 0);
4701 set_buffer_internal (prev);
4702 return unbind_to (count, str); 4739 return unbind_to (count, str);
4703 } 4740 }
4704 4741
4705 if (! encodep && CODING_REQUIRE_DETECTION (coding)) 4742 if (! encodep && CODING_REQUIRE_DETECTION (coding))
4706 { 4743 {
5491 #if defined (MSDOS) || defined (WINDOWSNT) 5528 #if defined (MSDOS) || defined (WINDOWSNT)
5492 system_eol_type = CODING_EOL_CRLF; 5529 system_eol_type = CODING_EOL_CRLF;
5493 #else 5530 #else
5494 system_eol_type = CODING_EOL_LF; 5531 system_eol_type = CODING_EOL_LF;
5495 #endif 5532 #endif
5533
5534 inhibit_pre_post_conversion = 0;
5496 } 5535 }
5497 5536
5498 #ifdef emacs 5537 #ifdef emacs
5499 5538
5500 void 5539 void