(wordify): Do the second loop by chars, not by bytes.
[bpt/emacs.git] / src / process.c
index 5c5b71e..158f9d3 100644 (file)
@@ -106,6 +106,8 @@ Boston, MA 02111-1307, USA.  */
 #include "commands.h"
 #include "frame.h"
 #include "blockinput.h"
+#include "keyboard.h"
+#include "dispextern.h"
 
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
@@ -279,6 +281,7 @@ static char pty_name[24];
 
 Lisp_Object status_convert ();
 
+void
 update_status (p)
      struct Lisp_Process *p;
 {
@@ -391,7 +394,7 @@ int
 allocate_pty ()
 {
   struct stat stb;
-  register c, i;
+  register int c, i;
   int fd;
 
   /* Some systems name their pseudoterminals so that there are gaps in
@@ -512,6 +515,7 @@ make_process (name)
   return val;
 }
 
+void
 remove_process (proc)
      register Lisp_Object proc;
 {
@@ -843,6 +847,45 @@ DEFUN ("set-process-window-size", Fset_process_window_size,
     return Qt;
 }
 
+DEFUN ("set-process-inherit-coding-system-flag",
+  Fset_process_inherit_coding_system_flag,
+  Sset_process_inherit_coding_system_flag, 2, 2, 0,
+  "Determine whether buffer of PROCESS will inherit coding-system.\n\
+If the second argument FLAG is non-nil, then the variable\n\
+`buffer-file-coding-system' of the buffer associated with PROCESS\n\
+will be bound to the value of the coding system used to decode\n\
+the process output.\n\
+\n\
+This is useful when the coding system specified for the process buffer\n\
+leaves either the character code conversion or the end-of-line conversion\n\
+unspecified, or if the coding system used to decode the process output\n\
+is more appropriate for saving the process buffer.\n\
+\n\
+Binding the variable `inherit-process-coding-system' to non-nil before\n\
+starting the process is an alternative way of setting the inherit flag\n\
+for the process which will run.")
+  (process, flag)
+     register Lisp_Object process, flag;
+{
+  CHECK_PROCESS (process, 0);
+  XPROCESS (process)->inherit_coding_system_flag = flag;
+  return flag;
+}
+
+DEFUN ("process-inherit-coding-system-flag",
+  Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
+  1, 1, 0,
+  "Return the value of inherit-coding-system flag for PROCESS.\n\
+If this flag is t, `buffer-file-coding-system' of the buffer\n\
+associated with PROCESS will inherit the coding system used to decode\n\
+the process output.")
+  (process)
+     register Lisp_Object process;
+{
+  CHECK_PROCESS (process, 0);
+  return XPROCESS (process)->inherit_coding_system_flag;
+}
+
 DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
   Sprocess_kill_without_query, 1, 2, 0,
   "Say no query needed if PROCESS is running when Emacs is exited.\n\
@@ -1089,12 +1132,12 @@ Remaining arguments are strings to give program as arguments.")
 #ifdef VMS
   /* Make a one member argv with all args concatenated
      together separated by a blank.  */
-  len = XSTRING (program)->size_byte + 2;
+  len = STRING_BYTES (XSTRING (program)) + 2;
   for (i = 3; i < nargs; i++)
     {
       tem = args[i];
       CHECK_STRING (tem, i);
-      len += XSTRING (tem)->size_byte + 1;     /* count the blank */
+      len += STRING_BYTES (XSTRING (tem)) + 1; /* count the blank */
     }
   new_argv = (unsigned char *) alloca (len);
   strcpy (new_argv, XSTRING (program)->data);
@@ -1229,6 +1272,10 @@ Remaining arguments are strings to give program as arguments.")
   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
   XPROCESS (proc)->encoding_carryover = make_number (0);
 
+  XPROCESS (proc)->inherit_coding_system_flag
+    = (NILP (buffer) || !inherit_process_coding_system
+       ? Qnil : Qt);
+
   create_process (proc, (char **) new_argv, current_dir);
 
   return unbind_to (count, proc);
@@ -2019,6 +2066,10 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
   XPROCESS (proc)->encoding_carryover = make_number (0);
 
+  XPROCESS (proc)->inherit_coding_system_flag
+    = (NILP (buffer) || !inherit_process_coding_system
+       ? Qnil : Qt);
+
   UNGCPRO;
   return proc;
 }
@@ -2173,7 +2224,7 @@ Return non-nil iff we received any output before the timeout expired.")
 static int waiting_for_user_input_p;
 
 /* This is here so breakpoints can be put on it.  */
-static
+static void
 wait_reading_process_input_1 ()
 {
 }
@@ -2210,6 +2261,7 @@ wait_reading_process_input_1 ()
      before the timeout elapsed.
    Otherwise, return true iff we received input from any process.  */
 
+int
 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
      int time_limit, microsecs;
      Lisp_Object read_kbd;
@@ -2708,6 +2760,7 @@ read_process_output_error_handler (error)
    The characters read are decoded according to PROC's coding-system
    for decoding.  */
 
+int
 read_process_output (proc, channel)
      Lisp_Object proc;
      register int channel;
@@ -2756,7 +2809,7 @@ read_process_output (proc, channel)
       char *buf = (char *) xmalloc (nbytes + carryover);
 
       bcopy (XSTRING (p->decoding_buf)->data
-            + XSTRING (p->decoding_buf)->size_byte - carryover,
+            + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
             buf, carryover);
       bcopy (chars, buf + carryover, nbytes);
       chars = buf;
@@ -2767,7 +2820,7 @@ read_process_output (proc, channel)
   if (carryover)
     /* See the comment above.  */
     bcopy (XSTRING (p->decoding_buf)->data
-          + XSTRING (p->decoding_buf)->size_byte - carryover,
+          + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
           buf, carryover);
 
   if (proc_buffered_char[channel] < 0)
@@ -2799,10 +2852,10 @@ read_process_output (proc, channel)
       int require = decoding_buffer_size (coding, nbytes);
       int result;
       
-      if (XSTRING (p->decoding_buf)->size_byte < require)
+      if (STRING_BYTES (XSTRING (p->decoding_buf)) < require)
        p->decoding_buf = make_uninit_string (require);
       result = decode_coding (coding, chars, XSTRING (p->decoding_buf)->data,
-                             nbytes, XSTRING (p->decoding_buf)->size_byte);
+                             nbytes, STRING_BYTES (XSTRING (p->decoding_buf)));
       carryover = nbytes - coding->consumed;
 
       /* A new coding system might be found by `decode_coding'.  */
@@ -2822,11 +2875,11 @@ read_process_output (proc, channel)
             valid memory because p->outfd will be changed once EOF is
             sent to the process.  */
          if (NILP (p->encode_coding_system)
-             && proc_encode_coding_system[p->outfd])
+             && proc_encode_coding_system[XINT (p->outfd)])
            {
              p->encode_coding_system = coding->symbol;
              setup_coding_system (coding->symbol,
-                                  proc_encode_coding_system[p->outfd]);
+                                  proc_encode_coding_system[XINT (p->outfd)]);
            }
        }
 
@@ -2839,7 +2892,9 @@ read_process_output (proc, channel)
        return 0;
       chars = (char *) XSTRING (p->decoding_buf)->data;
       nbytes = coding->produced;
-      nchars = coding->produced_char;
+      nchars = (coding->fake_multibyte
+               ? multibyte_chars_in_text (chars, nbytes)
+               : coding->produced_char);
       chars_in_decoding_buf = 1;
     }
 #ifdef VMS
@@ -2848,11 +2903,12 @@ read_process_output (proc, channel)
       /* Although we don't have to decode the received data, we must
          move it to an area which we don't have to free.  */
       if (! STRINGP (p->decoding_buf)
-         || XSTRING (p->decoding_buf)->size < nbytes)
+         || STRING_BYTES (XSTRING (p->decoding_buf)) < nbytes)
        p->decoding_buf = make_uninit_string (nbytes);
       bcopy (chars, XSTRING (p->decoding_buf)->data, nbytes);
       free (chars);
       chars = XSTRING (p->decoding_buf)->data;
+      nchars = multibyte_chars_in_text (chars, nbytes);
       chars_in_decoding_buf = 1;
       carryover = 0;
     }
@@ -2861,6 +2917,19 @@ read_process_output (proc, channel)
   XSETINT (p->decoding_carryover, carryover);
   Vlast_coding_system_used = coding->symbol;
 
+  /* If the caller required, let the process associated buffer
+     inherit the coding-system used to decode the process output.  */
+  if (! NILP (p->inherit_coding_system_flag)
+      && !NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
+    {
+      struct buffer *prev_buf = current_buffer;
+
+      Fset_buffer (p->buffer);
+      call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
+            make_number (nbytes));
+      set_buffer_internal (prev_buf);
+    }
+
   /* Read and dispose of the process output.  */
   outstream = p->filter;
   if (!NILP (outstream))
@@ -2900,7 +2969,7 @@ read_process_output (proc, channel)
         save the match data in a special nonrecursive fashion.  */
       running_asynch_code = 1;
 
-      text = make_multibyte_string (chars, nchars, nbytes);
+      text = make_string_from_bytes (chars, nchars, nbytes);
       internal_condition_case_1 (read_process_output_call,
                                 Fcons (outstream,
                                        Fcons (proc, Fcons (text, Qnil))),
@@ -2976,8 +3045,15 @@ read_process_output (proc, channel)
       /* Insert before markers in case we are inserting where
         the buffer's mark is, and the user's next command is Meta-y.  */
       if (chars_in_decoding_buf)
-       insert_from_string_before_markers (p->decoding_buf, 0, 0,
-                                          nchars, nbytes, 0);
+       {
+         /* Since multibyteness of p->docoding_buf is corrupted, we
+             can't use insert_from_string_before_markers.  */
+         char *temp_buf;
+
+         temp_buf = (char *) alloca (nbytes);
+         bcopy (XSTRING (p->decoding_buf)->data, temp_buf, nbytes);
+         insert_before_markers (temp_buf, nbytes);
+       }
       else
        insert_1_both (chars, nchars, nbytes, 0, 1, 1);
       set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
@@ -3052,6 +3128,7 @@ send_process_trap ()
    being encoded.  Should we store them in a buffer to prepend them to
    the data send later?  */
 
+void
 send_process (proc, buf, len, object)
      volatile Lisp_Object proc;
      unsigned char *buf;
@@ -3111,7 +3188,7 @@ send_process (proc, buf, len, object)
              offset = -1;
            }
          bcopy ((XSTRING (XPROCESS (proc)->encoding_buf)->data
-                 + XSTRING (XPROCESS (proc)->encoding_buf)->size_byte
+                 + STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf))
                  - carryover),
                 temp_buf,
                 carryover);
@@ -3119,7 +3196,7 @@ send_process (proc, buf, len, object)
          buf = temp_buf;
        }
 
-      if (XSTRING (XPROCESS (proc)->encoding_buf)->size_byte < require)
+      if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require)
        {
          XPROCESS (proc)->encoding_buf = make_uninit_string (require);
 
@@ -3133,7 +3210,7 @@ send_process (proc, buf, len, object)
        }
       object = XPROCESS (proc)->encoding_buf;
       encode_coding (coding, buf, XSTRING (object)->data,
-                    len, XSTRING (object)->size_byte);
+                    len, STRING_BYTES (XSTRING (object)));
       len = coding->produced;
       buf = XSTRING (object)->data;
       if (temp_buf)
@@ -3318,7 +3395,7 @@ Output from processes can arrive in between bunches.")
   CHECK_STRING (string, 1);
   proc = get_process (process);
   send_process (proc, XSTRING (string)->data,
-               XSTRING (string)->size_byte, string);
+               STRING_BYTES (XSTRING (string)), string);
   return Qnil;
 }
 \f
@@ -4279,6 +4356,7 @@ keyboard_bit_set (mask)
   return 0;
 }
 \f
+void
 init_process ()
 {
   register int i;
@@ -4307,6 +4385,7 @@ init_process ()
   bzero (proc_encode_coding_system, sizeof proc_encode_coding_system);
 }
 
+void
 syms_of_process ()
 {
   Qprocessp = intern ("processp");
@@ -4366,6 +4445,8 @@ The value takes effect when `start-process' is called.");
   defsubr (&Sset_process_sentinel);
   defsubr (&Sprocess_sentinel);
   defsubr (&Sset_process_window_size);
+  defsubr (&Sset_process_inherit_coding_system_flag);
+  defsubr (&Sprocess_inherit_coding_system_flag);
   defsubr (&Sprocess_kill_without_query);
   defsubr (&Sprocess_contact);
   defsubr (&Slist_processes);
@@ -4398,6 +4479,8 @@ The value takes effect when `start-process' is called.");
 
 #include "lisp.h"
 #include "systime.h"
+#include "charset.h"
+#include "coding.h"
 #include "termopts.h"
 #include "sysselect.h"
 
@@ -4633,10 +4716,25 @@ DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
   return Qnil;
 }
 
+DEFUN ("process-inherit-coding-system-flag",
+  Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
+  1, 1, 0,
+  /* Don't confuse make-docfile by having two doc strings for this function.
+     make-docfile does not pay attention to #if, for good reason!  */
+  0)
+  (process)
+     register Lisp_Object process;
+{
+  /* Ignore the argument and return the value of
+     inherit-process-coding-system.  */
+  return inherit_process_coding_system ? Qt : Qnil;
+}
+
 /* Kill all processes associated with `buffer'.
    If `buffer' is nil, kill all processes.
    Since we have no subprocesses, this does nothing.  */
 
+void
 kill_buffer_processes (buffer)
      Lisp_Object buffer;
 {
@@ -4649,6 +4747,7 @@ init_process ()
 syms_of_process ()
 {
   defsubr (&Sget_buffer_process);
+  defsubr (&Sprocess_inherit_coding_system_flag);
 }
 
 \f