]> code.delx.au - gnu-emacs/blobdiff - src/editfns.c
* macterm.c: Remove consolidated defines and code.
[gnu-emacs] / src / editfns.c
index bf4976273aad22b39df48dc6eefa7bcb22930fb6..0c012514f93aa10d214a4670989bfb08f272aa70 100644 (file)
@@ -289,16 +289,16 @@ region_limit (beginningp)
 {
   extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
   Lisp_Object m;
-  
+
   if (!NILP (Vtransient_mark_mode)
       && NILP (Vmark_even_if_inactive)
       && NILP (current_buffer->mark_active))
     Fsignal (Qmark_inactive, Qnil);
-  
+
   m = Fmarker_position (current_buffer->mark);
   if (NILP (m))
     error ("The mark is not set now, so there is no region");
-  
+
   if ((PT < XFASTINT (m)) == beginningp)
     m = make_number (PT);
   return m;
@@ -394,7 +394,7 @@ overlays_around (pos, vec, len)
    If OBJECT is a window, then that window's buffer is used, but
    window-specific overlays are considered only if they are associated
    with OBJECT. */
-static Lisp_Object
+Lisp_Object
 get_pos_property (position, prop, object)
      Lisp_Object position, object;
      register Lisp_Object prop;
@@ -456,16 +456,16 @@ get_pos_property (position, prop, object)
                }
            }
        }
-      
+
     }
 
   { /* Now check the text-properties.  */
-    int stickiness = text_property_stickiness (Qfield, position);
+    int stickiness = text_property_stickiness (prop, position);
     if (stickiness > 0)
-      return Fget_text_property (position, Qfield, Qnil);
+      return Fget_text_property (position, prop, Qnil);
     else if (stickiness < 0 && XINT (position) > BEGV)
       return Fget_text_property (make_number (XINT (position) - 1),
-                                Qfield, Qnil);
+                                prop, Qnil);
     else
       return Qnil;
   }
@@ -531,6 +531,12 @@ find_field (pos, merge_at_boundary, beg_limit, beg, end_limit, end)
        at_field_end = 1;
       if (!EQ (field, before_field))
        at_field_start = 1;
+      if (NILP (field) && at_field_start && at_field_end)
+       /* If an inserted char would have a nil field while the surrounding
+          text is non-nil, we're probably not looking at a
+          zero-length field, but instead at a non-nil field that's
+          not intended for editing (such as comint's prompts).  */
+       at_field_end = at_field_start = 0;
     }
 
   /* Note about special `boundary' fields:
@@ -875,7 +881,7 @@ save_excursion_restore (info)
   /* visible */
   info = XCDR (info);
   visible_p = !NILP (XCAR (info));
-  
+
 #if 0 /* We used to make the current buffer visible in the selected window
         if that was true previously.  That avoids some anomalies.
         But it creates others, and it wasn't documented, and it is simpler
@@ -2055,9 +2061,14 @@ Point and before-insertion markers move forward to end up
 Any other markers at the point of insertion remain before the text.
 
 If the current buffer is multibyte, unibyte strings are converted
-to multibyte for insertion (see `unibyte-char-to-multibyte').
+to multibyte for insertion (see `string-make-multibyte').
 If the current buffer is unibyte, multibyte strings are converted
-to unibyte for insertion.
+to unibyte for insertion (see `string-make-unibyte').
+
+When operating on binary data, it may be necessary to preserve the
+original bytes of a unibyte string when inserting it into a multibyte
+buffer; to accomplish this, apply `string-as-multibyte' to the string
+and insert the result.
 
 usage: (insert &rest ARGS)  */)
      (nargs, args)
@@ -2941,7 +2952,7 @@ save_restriction_restore (data)
                             clip_to_bounds (beg->charpos, pt, end->charpos),
                             clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf),
                                             end->bytepos));
-         
+
          buf->clip_changed = 1; /* Remember that the narrowing changed. */
        }
     }
@@ -3021,7 +3032,7 @@ usage: (message STRING &rest ARGS)  */)
   else
     {
       register Lisp_Object val;
-      val = nargs < 2 && STRINGP (args[0]) ? args[0] : Fformat (nargs, args);
+      val = Fformat (nargs, args);
       message3 (val, SBYTES (val), STRING_MULTIBYTE (val));
       return val;
     }
@@ -3201,6 +3212,12 @@ usage: (format STRING &rest OBJECTS)  */)
      must consider such a situation or not.  */
   int maybe_combine_byte;
   unsigned char *this_format;
+  /* Precision for each spec, or -1, a flag value meaning no precision
+     was given in that spec.  Element 0, corresonding to the format
+     string itself, will not be used.  Element NARGS, corresponding to
+     no argument, *will* be assigned to in the case that a `%' and `.'
+     occur after the final format specifier.  */
+  int *precision = (int *) (alloca(nargs * sizeof (int)));
   int longest_format;
   Lisp_Object val;
   struct info
@@ -3215,9 +3232,12 @@ usage: (format STRING &rest OBJECTS)  */)
      This is not always right; sometimes the result needs to be multibyte
      because of an object that we will pass through prin1,
      and in that case, we won't know it here.  */
-  for (n = 0; n < nargs; n++)
+  for (n = 0; n < nargs; n++) {
     if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
       multibyte = 1;
+    /* Piggyback on this loop to initialize precision[N]. */
+    precision[n] = -1;
+  }
 
   CHECK_STRING (args[0]);
 
@@ -3241,7 +3261,7 @@ usage: (format STRING &rest OBJECTS)  */)
        int thissize = 0;
        int actual_width = 0;
        unsigned char *this_format_start = format - 1;
-       int field_width, precision;
+       int field_width = 0;
 
        /* General format specifications look like
 
@@ -3257,12 +3277,17 @@ usage: (format STRING &rest OBJECTS)  */)
           the output should be padded with blanks, iff the output
           string is shorter than field-width.
 
-          if precision is specified, it specifies the number of
+          If precision is specified, it specifies the number of
           digits to print after the '.' for floats, or the max.
           number of chars to print from a string.  */
 
-       precision = field_width = 0;
-       
+       /* NOTE the handling of specifiers here differs in some ways
+           from the libc model.  There are bugs in this code that lead
+           to incorrect formatting when flags recognized by C but
+           neither parsed nor rejected here are used.  Further
+           revisions will be made soon.  */
+
+        /* incorrect list of flags to skip; will be fixed */
        while (index ("-*# 0", *format))
          ++format;
 
@@ -3272,11 +3297,13 @@ usage: (format STRING &rest OBJECTS)  */)
              field_width = 10 * field_width + *format - '0';
          }
 
+       /* N is not incremented for another few lines below, so refer to
+          element N+1 (which might be precision[NARGS]). */
        if (*format == '.')
          {
            ++format;
-           for (precision = 0; *format >= '0' && *format <= '9'; ++format)
-             precision = 10 * precision + *format - '0';
+           for (precision[n+1] = 0; *format >= '0' && *format <= '9'; ++format)
+             precision[n+1] = 10 * precision[n+1] + *format - '0';
          }
 
        if (format - this_format_start + 1 > longest_format)
@@ -3316,7 +3343,10 @@ usage: (format STRING &rest OBJECTS)  */)
          string:
            if (*format != 's' && *format != 'S')
              error ("Format specifier doesn't match argument type");
-           thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
+           /* In the case (PRECISION[N] > 0), THISSIZE may not need
+              to be as large as is calculated here.  Easy check for
+              the case PRECISION = 0. */
+           thissize = precision[n] ? CONVERTED_BYTE_SIZE (multibyte, args[n]) : 0;
            actual_width = lisp_string_width (args[n], -1, NULL, NULL);
          }
        /* Would get MPV otherwise, since Lisp_Int's `point' to low memory.  */
@@ -3334,17 +3364,29 @@ usage: (format STRING &rest OBJECTS)  */)
                error ("Invalid format operation %%%c", *format);
 
            thissize = 30;
-           if (*format == 'c'
-               && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
-                   || XINT (args[n]) == 0))
+           if (*format == 'c')
              {
-               if (! multibyte)
+               if (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
+                   /* Note: No one can remeber why we have to treat
+                      the character 0 as a multibyte character here.
+                      But, until it causes a real problem, let's
+                      don't change it.  */
+                   || XINT (args[n]) == 0)
                  {
-                   multibyte = 1;
-                   goto retry;
+                   if (! multibyte)
+                     {
+                       multibyte = 1;
+                       goto retry;
+                     }
+                   args[n] = Fchar_to_string (args[n]);
+                   thissize = SBYTES (args[n]);
+                 }
+               else if (! ASCII_BYTE_P (XINT (args[n])) && multibyte)
+                 {
+                   args[n]
+                     = Fchar_to_string (Funibyte_char_to_multibyte (args[n]));
+                   thissize = SBYTES (args[n]);
                  }
-               args[n] = Fchar_to_string (args[n]);
-               thissize = SBYTES (args[n]);
              }
          }
        else if (FLOATP (args[n]) && *format != 's')
@@ -3360,7 +3402,9 @@ usage: (format STRING &rest OBJECTS)  */)
            /* Note that we're using sprintf to print floats,
               so we have to take into account what that function
               prints.  */
-           thissize = MAX_10_EXP + 100 + precision;
+           /* Filter out flag value of -1.  */
+           thissize = (MAX_10_EXP + 100
+                       + (precision[n] > 0 ? precision[n] : 0));
          }
        else
          {
@@ -3410,10 +3454,14 @@ usage: (format STRING &rest OBJECTS)  */)
          format++;
 
          /* Process a numeric arg and skip it.  */
+         /* NOTE atoi is the wrong thing to use here; will be fixed */
          minlen = atoi (format);
          if (minlen < 0)
            minlen = - minlen, negative = 1;
 
+         /* NOTE the parsing here is not consistent with the first
+             pass, and neither attempt is what we want to do.  Will be
+             fixed. */
          while ((*format >= '0' && *format <= '9')
                 || *format == '-' || *format == ' ' || *format == '.')
            format++;
@@ -3429,8 +3477,28 @@ usage: (format STRING &rest OBJECTS)  */)
 
          if (STRINGP (args[n]))
            {
-             int padding, nbytes, start, end;
-             int width = lisp_string_width (args[n], -1, NULL, NULL);
+             /* handle case (precision[n] >= 0) */
+
+             int width, padding;
+             int nbytes, start, end;
+             int nchars_string;
+
+             /* lisp_string_width ignores a precision of 0, but GNU
+                libc functions print 0 characters when the precision
+                is 0.  Imitate libc behavior here.  Changing
+                lisp_string_width is the right thing, and will be
+                done, but meanwhile we work with it. */
+
+             if (precision[n] == 0)
+               width = nchars_string = nbytes = 0;
+             else if (precision[n] > 0)
+               width = lisp_string_width (args[n], precision[n], &nchars_string, &nbytes);
+             else
+               {               /* no precision spec given for this argument */
+                 width = lisp_string_width (args[n], -1, NULL, NULL);
+                 nbytes = SBYTES (args[n]);
+                 nchars_string = SCHARS (args[n]);
+               }
 
              /* If spec requires it, pad on right with spaces.  */
              padding = minlen - width;
@@ -3442,19 +3510,19 @@ usage: (format STRING &rest OBJECTS)  */)
                  }
 
              start = nchars;
-             
+             nchars += nchars_string;
+             end = nchars;
+
              if (p > buf
                  && multibyte
                  && !ASCII_BYTE_P (*((unsigned char *) p - 1))
                  && STRING_MULTIBYTE (args[n])
                  && !CHAR_HEAD_P (SREF (args[n], 0)))
                maybe_combine_byte = 1;
-             nbytes = copy_text (SDATA (args[n]), p,
-                                 SBYTES (args[n]),
-                                 STRING_MULTIBYTE (args[n]), multibyte);
-             p += nbytes;
-             nchars += SCHARS (args[n]);
-             end = nchars;
+
+             p += copy_text (SDATA (args[n]), p,
+                             nbytes,
+                             STRING_MULTIBYTE (args[n]), multibyte);
 
              if (negative)
                while (padding-- > 0)
@@ -3584,30 +3652,17 @@ usage: (format STRING &rest OBJECTS)  */)
   return val;
 }
 
-
-/* VARARGS 1 */
 Lisp_Object
-#ifdef NO_ARG_ARRAY
-format1 (string1, arg0, arg1, arg2, arg3, arg4)
-     EMACS_INT arg0, arg1, arg2, arg3, arg4;
-#else
-format1 (string1)
-#endif
+format2 (string1, arg0, arg1)
      char *string1;
+     Lisp_Object arg0, arg1;
 {
-  char buf[100];
-#ifdef NO_ARG_ARRAY
-  EMACS_INT args[5];
-  args[0] = arg0;
-  args[1] = arg1;
-  args[2] = arg2;
-  args[3] = arg3;
-  args[4] = arg4;
-  doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
-#else
-  doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
-#endif
-  return build_string (buf);
+  Lisp_Object args[3];
+  int numargs;
+  args[0] = build_string (string1);
+  args[1] = arg0;
+  args[2] = arg1;
+  return Fformat (3, args);
 }
 \f
 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,