comparison src/coding.c @ 58637:ff8a37b5299b

(Vcode_conversion_workbuf_name): New variable. (syms_of_coding): Initialize and staticpro it. (set_conversion_work_buffer): New function. (run_pre_post_conversion_on_str): Use it. (run_pre_write_conversin_on_c_str): New function.
author Kenichi Handa <handa@m17n.org>
date Tue, 30 Nov 2004 08:07:11 +0000
parents ffc46016071f
children 8a2a99b47220 549734260e34 f2ebccfa87d4
comparison
equal deleted inserted replaced
58636:44a9e60f1d12 58637:ff8a37b5299b
6006 } 6006 }
6007 6007
6008 return 0; 6008 return 0;
6009 } 6009 }
6010 6010
6011 Lisp_Object 6011 /* Name (or base name) of work buffer for code conversion. */
6012 run_pre_post_conversion_on_str (str, coding, encodep) 6012 static Lisp_Object Vcode_conversion_workbuf_name;
6013 Lisp_Object str; 6013
6014 struct coding_system *coding; 6014 /* Set the current buffer to the working buffer prepared for
6015 int encodep; 6015 code-conversion. MULTIBYTE specifies the multibyteness of the
6016 { 6016 buffer. */
6017 int count = SPECPDL_INDEX (); 6017
6018 struct gcpro gcpro1, gcpro2; 6018 static struct buffer *
6019 int multibyte = STRING_MULTIBYTE (str); 6019 set_conversion_work_buffer (multibyte)
6020 int multibyte;
6021 {
6020 Lisp_Object buffer; 6022 Lisp_Object buffer;
6021 struct buffer *buf; 6023 struct buffer *buf;
6022 Lisp_Object old_deactivate_mark; 6024
6023 6025 buffer = Fget_buffer_create (Vcode_conversion_workbuf_name);
6024 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6025 record_unwind_protect (code_convert_region_unwind,
6026 Vlast_coding_system_used);
6027 /* It is not crucial to specbind this. */
6028 old_deactivate_mark = Vdeactivate_mark;
6029 GCPRO2 (str, old_deactivate_mark);
6030
6031 buffer = Fget_buffer_create (build_string (" *code-converting-work*"));
6032 buf = XBUFFER (buffer); 6026 buf = XBUFFER (buffer);
6033
6034 delete_all_overlays (buf); 6027 delete_all_overlays (buf);
6035 buf->directory = current_buffer->directory; 6028 buf->directory = current_buffer->directory;
6036 buf->read_only = Qnil; 6029 buf->read_only = Qnil;
6037 buf->filename = Qnil; 6030 buf->filename = Qnil;
6038 buf->undo_list = Qt; 6031 buf->undo_list = Qt;
6039 eassert (buf->overlays_before == NULL); 6032 eassert (buf->overlays_before == NULL);
6040 eassert (buf->overlays_after == NULL); 6033 eassert (buf->overlays_after == NULL);
6041
6042 set_buffer_internal (buf); 6034 set_buffer_internal (buf);
6043 /* Don't let Ferase_buffer stumble due to text props. */ 6035 if (BEG != BEGV || Z != ZV)
6044 specbind (Qinhibit_read_only, Qt); 6036 Fwiden ();
6037 del_range_2 (BEG, BEG_BYTE, Z, Z_BYTE, 0);
6038 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
6039 return buf;
6040 }
6041
6042 Lisp_Object
6043 run_pre_post_conversion_on_str (str, coding, encodep)
6044 Lisp_Object str;
6045 struct coding_system *coding;
6046 int encodep;
6047 {
6048 int count = SPECPDL_INDEX ();
6049 struct gcpro gcpro1, gcpro2;
6050 int multibyte = STRING_MULTIBYTE (str);
6051 struct buffer *buf;
6052 Lisp_Object old_deactivate_mark;
6053
6054 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6055 record_unwind_protect (code_convert_region_unwind,
6056 Vlast_coding_system_used);
6057 /* It is not crucial to specbind this. */
6058 old_deactivate_mark = Vdeactivate_mark;
6059 GCPRO2 (str, old_deactivate_mark);
6060
6045 /* We must insert the contents of STR as is without 6061 /* We must insert the contents of STR as is without
6046 unibyte<->multibyte conversion. For that, we adjust the 6062 unibyte<->multibyte conversion. For that, we adjust the
6047 multibyteness of the working buffer to that of STR. */ 6063 multibyteness of the working buffer to that of STR. */
6048 Ferase_buffer (); 6064 set_conversion_work_buffer (multibyte);
6049 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
6050 6065
6051 insert_from_string (str, 0, 0, 6066 insert_from_string (str, 0, 0,
6052 SCHARS (str), SBYTES (str), 0); 6067 SCHARS (str), SBYTES (str), 0);
6053 UNGCPRO; 6068 UNGCPRO;
6054 inhibit_pre_post_conversion = 1; 6069 inhibit_pre_post_conversion = 1;
6064 inhibit_pre_post_conversion = 0; 6079 inhibit_pre_post_conversion = 0;
6065 Vdeactivate_mark = old_deactivate_mark; 6080 Vdeactivate_mark = old_deactivate_mark;
6066 str = make_buffer_string (BEG, Z, 1); 6081 str = make_buffer_string (BEG, Z, 1);
6067 return unbind_to (count, str); 6082 return unbind_to (count, str);
6068 } 6083 }
6084
6085
6086 /* Run pre-write-conversion function of CODING on NCHARS/NBYTES
6087 text in *STR. *SIZE is the allocated bytes for STR. As it
6088 is intended that this function is called from encode_terminal_code,
6089 the pre-write-conversion function is run by safe_call and thus
6090 "Error during redisplay: ..." is logged when an error occurs.
6091
6092 Store the resulting text in *STR and set CODING->produced_char and
6093 CODING->produced to the number of characters and bytes
6094 respectively. If the size of *STR is too small, enlarge it by
6095 xrealloc and update *STR and *SIZE. */
6096
6097 void
6098 run_pre_write_conversin_on_c_str (str, size, nchars, nbytes, coding)
6099 unsigned char **str;
6100 int *size, nchars, nbytes;
6101 struct coding_system *coding;
6102 {
6103 struct gcpro gcpro1, gcpro2;
6104 struct buffer *cur = current_buffer;
6105 Lisp_Object old_deactivate_mark, old_last_coding_system_used;
6106 Lisp_Object args[3];
6107
6108 /* It is not crucial to specbind this. */
6109 old_deactivate_mark = Vdeactivate_mark;
6110 old_last_coding_system_used = Vlast_coding_system_used;
6111 GCPRO2 (old_deactivate_mark, old_last_coding_system_used);
6112
6113 /* We must insert the contents of STR as is without
6114 unibyte<->multibyte conversion. For that, we adjust the
6115 multibyteness of the working buffer to that of STR. */
6116 set_conversion_work_buffer (coding->src_multibyte);
6117 insert_1_both (*str, nchars, nbytes, 0, 0, 0);
6118 UNGCPRO;
6119 inhibit_pre_post_conversion = 1;
6120 args[0] = coding->pre_write_conversion;
6121 args[1] = make_number (BEG);
6122 args[2] = make_number (Z);
6123 safe_call (3, args);
6124 inhibit_pre_post_conversion = 0;
6125 Vdeactivate_mark = old_deactivate_mark;
6126 Vlast_coding_system_used = old_last_coding_system_used;
6127 coding->produced_char = Z - BEG;
6128 coding->produced = Z_BYTE - BEG_BYTE;
6129 if (coding->produced > *size)
6130 {
6131 *size = coding->produced;
6132 *str = xrealloc (*str, *size);
6133 }
6134 if (BEG < GPT && GPT < Z)
6135 move_gap (BEG);
6136 bcopy (BEG_ADDR, *str, coding->produced);
6137 coding->src_multibyte
6138 = ! NILP (current_buffer->enable_multibyte_characters);
6139 set_buffer_internal (cur);
6140 }
6141
6069 6142
6070 Lisp_Object 6143 Lisp_Object
6071 decode_coding_string (str, coding, nocopy) 6144 decode_coding_string (str, coding, nocopy)
6072 Lisp_Object str; 6145 Lisp_Object str;
6073 struct coding_system *coding; 6146 struct coding_system *coding;
7543 #ifdef emacs 7616 #ifdef emacs
7544 7617
7545 void 7618 void
7546 syms_of_coding () 7619 syms_of_coding ()
7547 { 7620 {
7621 staticpro (&Vcode_conversion_workbuf_name);
7622 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
7623
7548 Qtarget_idx = intern ("target-idx"); 7624 Qtarget_idx = intern ("target-idx");
7549 staticpro (&Qtarget_idx); 7625 staticpro (&Qtarget_idx);
7550 7626
7551 Qcoding_system_history = intern ("coding-system-history"); 7627 Qcoding_system_history = intern ("coding-system-history");
7552 staticpro (&Qcoding_system_history); 7628 staticpro (&Qcoding_system_history);