(Fsave_restriction): Doc fix.
[bpt/emacs.git] / src / editfns.c
index 258ce2b..f1f11cf 100644 (file)
@@ -490,20 +490,26 @@ See also `gap-position'.")
 }
 
 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
-  "Return the byte position for character position POSITION.")
+  "Return the byte position for character position POSITION.\n\
+If POSITION is out of range, the value is nil.")
   (position)
      Lisp_Object position;
 {
   CHECK_NUMBER_COERCE_MARKER (position, 1);
+  if (XINT (position) < BEG || XINT (position) > Z)
+    return Qnil;
   return make_number (CHAR_TO_BYTE (XINT (position)));
 }
 
 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
-  "Return the character position for byte position BYTEPOS.")
+  "Return the character position for byte position BYTEPOS.\n\
+If BYTEPOS is out of range, the value is nil.")
   (bytepos)
      Lisp_Object bytepos;
 {
   CHECK_NUMBER (bytepos, 1);
+  if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
+    return Qnil;
   return make_number (BYTE_TO_CHAR (XINT (bytepos)));
 }
 \f
@@ -841,6 +847,58 @@ lisp_time_argument (specified_time, result)
     }
 }
 
+/* Write information into buffer S of size MAXSIZE, according to the
+   FORMAT of length FORMAT_LEN, using time information taken from *TP.
+   Return the number of bytes written, not including the terminating
+   '\0'.  If S is NULL, nothing will be written anywhere; so to
+   determine how many bytes would be written, use NULL for S and
+   ((size_t) -1) for MAXSIZE.
+
+   This function behaves like emacs_strftime, except it allows null
+   bytes in FORMAT.  */
+static size_t
+emacs_memftime (s, maxsize, format, format_len, tp)
+      char *s;
+      size_t maxsize;
+      const char *format;
+      size_t format_len;
+      const struct tm *tp;
+{
+  size_t total = 0;
+
+  /* Loop through all the null-terminated strings in the format
+     argument.  Normally there's just one null-terminated string, but
+     there can be arbitrarily many, concatenated together, if the
+     format contains '\0' bytes.  emacs_strftime stops at the first
+     '\0' byte so we must invoke it separately for each such string.  */
+  for (;;)
+    {
+      size_t len;
+      size_t result;
+
+      if (s)
+       s[0] = '\1';
+
+      result = emacs_strftime (s, maxsize, format, tp);
+
+      if (s)
+       {
+         if (result == 0 && s[0] != '\0')
+           return 0;
+         s += result + 1;
+       }
+
+      maxsize -= result + 1;
+      total += result;
+      len = strlen (format);
+      if (len == format_len)
+       return total;
+      total++;
+      format += len + 1;
+      format_len -= len + 1;
+    }
+}
+
 /*
 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
   "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
@@ -899,6 +957,7 @@ DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
 {
   time_t value;
   int size;
+  struct tm *tm;
 
   CHECK_STRING (format_string, 1);
 
@@ -908,22 +967,27 @@ DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
   /* This is probably enough.  */
   size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
 
+  tm = NILP (universal) ? localtime (&value) : gmtime (&value);
+  if (! tm)
+    error ("Specified time is not representable");
+
   while (1)
     {
       char *buf = (char *) alloca (size + 1);
       int result;
 
       buf[0] = '\1';
-      result = emacs_strftime (buf, size, XSTRING (format_string)->data,
-                              (NILP (universal) ? localtime (&value)
-                               : gmtime (&value)));
+      result = emacs_memftime (buf, size, XSTRING (format_string)->data,
+                              STRING_BYTES (XSTRING (format_string)),
+                              tm);
       if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
-       return build_string (buf);
+       return make_string (buf, result);
 
       /* If buffer was too small, make it bigger and try again.  */
-      result = emacs_strftime (NULL, 0x7fffffff, XSTRING (format_string)->data,
-                              (NILP (universal) ? localtime (&value)
-                               : gmtime (&value)));
+      result = emacs_memftime (NULL, (size_t) -1,
+                              XSTRING (format_string)->data,
+                              STRING_BYTES (XSTRING (format_string)),
+                              tm);
       size = result + 1;
     }
 }
@@ -953,6 +1017,8 @@ ZONE is an integer indicating the number of seconds east of Greenwich.\n\
     error ("Invalid time specification");
 
   decoded_time = localtime (&time_spec);
+  if (! decoded_time)
+    error ("Specified time is not representable");
   XSETFASTINT (list_args[0], decoded_time->tm_sec);
   XSETFASTINT (list_args[1], decoded_time->tm_min);
   XSETFASTINT (list_args[2], decoded_time->tm_hour);
@@ -1137,18 +1203,15 @@ the data it can't find.")
 {
   time_t value;
   struct tm *t;
+  struct tm gmt;
 
   if (lisp_time_argument (specified_time, &value)
-      && (t = gmtime (&value)) != 0)
+      && (t = gmtime (&value)) != 0
+      && (gmt = *t, t = localtime (&value)) != 0)
     {
-      struct tm gmt;
-      int offset;
-      char *s, buf[6];
-
-      gmt = *t;                /* Make a copy, in case localtime modifies *t.  */
-      t = localtime (&value);
-      offset = tm_diff (t, &gmt);
-      s = 0;
+      int offset = tm_diff (t, &gmt);
+      char *s = 0;
+      char buf[6];
 #ifdef HAVE_TM_ZONE
       if (t->tm_zone)
        s = (char *)t->tm_zone;
@@ -1326,7 +1389,13 @@ general_insert_function (insert_func, insert_from_string_func,
          if (!NILP (current_buffer->enable_multibyte_characters))
            len = CHAR_STRING (XFASTINT (val), workbuf, str);
          else
-           workbuf[0] = XINT (val), str = workbuf, len = 1;
+           {
+             workbuf[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
+                           ? XINT (val)
+                           : multibyte_char_to_unibyte (XINT (val), Qnil));
+             str = workbuf;
+             len = 1;
+           }
          (*insert_func) (str, len);
        }
       else if (STRINGP (val))
@@ -1938,9 +2007,44 @@ Both characters must have the same length of multi-byte form.")
              changed = 1;
            }
 
-         if (NILP (noundo))
-           record_change (pos, 1);
-         for (i = 0; i < len; i++) *p++ = tostr[i];
+         /* Take care of the case where the new character
+            combines with neighboring bytes.  */ 
+         if (len == 1
+             && ((! CHAR_HEAD_P (tostr[0])
+                  && pos_byte > BEGV_BYTE
+                  && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))
+                 ||
+                 (! ASCII_BYTE_P (tostr[0])
+                  && pos_byte + 1 < ZV_BYTE
+                  && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1)))))
+           {
+             Lisp_Object tem, string;
+
+             struct gcpro gcpro1;
+
+             tem = current_buffer->undo_list;
+             GCPRO1 (tem);
+
+             /* Make a multibyte string containing this
+                single-byte character.  */
+             string = Fmake_string (make_number (1),
+                                    make_number (tochar));
+             SET_STRING_BYTES (XSTRING (string), 1);
+             /* replace_range is less efficient, because it moves the gap,
+                but it handles combining correctly.  */
+             replace_range (pos, pos + 1, string,
+                            0, 0, 1);
+             if (! NILP (noundo))
+               current_buffer->undo_list = tem;
+
+             UNGCPRO;
+           }
+         else
+           {
+             if (NILP (noundo))
+               record_change (pos, 1);
+             for (i = 0; i < len; i++) *p++ = tostr[i];
+           }
        }
       INC_BOTH (pos, pos_byte);
     }
@@ -1995,9 +2099,33 @@ It returns the number of characters changed.")
          nc = tt[oc];
          if (nc != oc)
            {
-             record_change (pos, 1);
-             *p = nc;
-             signal_after_change (pos, 1, 1);
+             /* Take care of the case where the new character
+                combines with neighboring bytes.  */ 
+             if ((! CHAR_HEAD_P (nc)
+                  && pos_byte > BEGV_BYTE
+                  && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))
+                 ||
+                 (! ASCII_BYTE_P (nc)
+                  && pos_byte + 1 < ZV_BYTE
+                  && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1))))
+               {
+                 Lisp_Object string;
+
+                 string = Fmake_string (make_number (1),
+                                        make_number (nc));
+                 SET_STRING_BYTES (XSTRING (string), 1);
+
+                 /* This is less efficient, because it moves the gap,
+                    but it handles combining correctly.  */
+                 replace_range (pos, pos + 1, string,
+                                1, 0, 0);
+               }
+             else
+               {
+                 record_change (pos, 1);
+                 *p = nc;
+                 signal_after_change (pos, 1, 1);
+               }
              ++cnt;
            }
        }
@@ -2141,6 +2269,7 @@ The value returned is the value of the last form in BODY.\n\
 \n\
 `save-restriction' can get confused if, within the BODY, you widen\n\
 and then make changes outside the area within the saved restrictions.\n\
+See Info node `(elisp)Narrowing' for details and an appropriate technique.\n\
 \n\
 Note: if you are using both `save-excursion' and `save-restriction',\n\
 use `save-excursion' outermost:\n\
@@ -2376,6 +2505,8 @@ Use %% to put a single % into the output.")
        if (format - this_format_start + 1 > longest_format)
          longest_format = format - this_format_start + 1;
 
+       if (format == end)
+         error ("Format string ends in middle of format specifier");
        if (*format == '%')
          format++;
        else if (++n >= nargs)
@@ -2407,7 +2538,7 @@ Use %% to put a single % into the output.")
          {
          string:
            if (*format != 's' && *format != 'S')
-             error ("format specifier doesn't match argument type");
+             error ("Format specifier doesn't match argument type");
            thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
          }
        /* Would get MPV otherwise, since Lisp_Int's `point' to low memory.  */
@@ -2527,9 +2658,9 @@ Use %% to put a single % into the output.")
 
              if (p > buf
                  && multibyte
-                 && *((unsigned char *) p - 1) >= 0x80
+                 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
                  && STRING_MULTIBYTE (args[n])
-                 && XSTRING (args[n])->data[0] >= 0xA0)
+                 && !CHAR_HEAD_P (XSTRING (args[n])->data[0]))
                maybe_combine_byte = 1;
              nbytes = copy_text (XSTRING (args[n])->data, p,
                                  STRING_BYTES (XSTRING (args[n])),
@@ -2559,8 +2690,8 @@ Use %% to put a single % into the output.")
 
              if (p > buf
                  && multibyte
-                 && *((unsigned char *) p - 1) >= 0x80
-                 && *((unsigned char *) p) >= 0xA0)
+                 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
+                 && !CHAR_HEAD_P (*((unsigned char *) p)))
                maybe_combine_byte = 1;
              this_nchars = strlen (p);
              p += this_nchars;
@@ -2572,8 +2703,8 @@ Use %% to put a single % into the output.")
          /* Copy a whole multibyte character.  */
          if (p > buf
              && multibyte
-             && *((unsigned char *) p - 1) >= 0x80
-             && *format >= 0xA0)
+             && !ASCII_BYTE_P (*((unsigned char *) p - 1))
+             && !CHAR_HEAD_P (*format))
            maybe_combine_byte = 1;
          *p++ = *format++;
          while (! CHAR_HEAD_P (*format)) *p++ = *format++;
@@ -2875,8 +3006,8 @@ Transposing beyond buffer boundaries is an error.")
                         start1_byte, start1_byte + len1_byte,
                         start2_byte, start2_byte + len2_byte);
 
-      replace_range (start2, end2, text1, 1, 0, 1);
-      replace_range (start1, end1, text2, 1, 0, 1);
+      replace_range (start2, end2, text1, 1, 0, 0);
+      replace_range (start1, end1, text2, 1, 0, 0);
 
       UNGCPRO;
       return Qnil;
@@ -2914,8 +3045,8 @@ Transposing beyond buffer boundaries is an error.")
          /* Don't precompute these addresses.  We have to compute them
             at the last minute, because the relocating allocator might
             have moved the buffer around during the xmalloc.  */
-         start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
-         start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
+         start1_addr = BYTE_POS_ADDR (start1_byte);
+         start2_addr = BYTE_POS_ADDR (start2_byte);
 
           bcopy (start2_addr, temp, len2_byte);
           bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
@@ -2930,8 +3061,8 @@ Transposing beyond buffer boundaries is an error.")
            temp = (unsigned char *) xmalloc (len1_byte);
          else
            temp = (unsigned char *) alloca (len1_byte);
-         start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
-         start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
+         start1_addr = BYTE_POS_ADDR (start1_byte);
+         start2_addr = BYTE_POS_ADDR (start2_byte);
           bcopy (start1_addr, temp, len1_byte);
           bcopy (start2_addr, start1_addr, len2_byte);
           bcopy (temp, start1_addr + len2_byte, len1_byte);
@@ -2970,8 +3101,8 @@ Transposing beyond buffer boundaries is an error.")
            temp = (unsigned char *) xmalloc (len1_byte);
          else
            temp = (unsigned char *) alloca (len1_byte);
-         start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
-         start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
+         start1_addr = BYTE_POS_ADDR (start1_byte);
+         start2_addr = BYTE_POS_ADDR (start2_byte);
           bcopy (start1_addr, temp, len1_byte);
           bcopy (start2_addr, start1_addr, len2_byte);
           bcopy (temp, start2_addr, len1_byte);
@@ -3003,8 +3134,8 @@ Transposing beyond buffer boundaries is an error.")
            temp = (unsigned char *) xmalloc (len2_byte);
          else
            temp = (unsigned char *) alloca (len2_byte);
-         start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
-         start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
+         start1_addr = BYTE_POS_ADDR (start1_byte);
+         start2_addr = BYTE_POS_ADDR (start2_byte);
           bcopy (start2_addr, temp, len2_byte);
           bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
           safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
@@ -3039,8 +3170,8 @@ Transposing beyond buffer boundaries is an error.")
            temp = (unsigned char *) xmalloc (len1_byte);
          else
            temp = (unsigned char *) alloca (len1_byte);
-         start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
-         start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
+         start1_addr = BYTE_POS_ADDR (start1_byte);
+         start2_addr = BYTE_POS_ADDR (start2_byte);
           bcopy (start1_addr, temp, len1_byte);
           bcopy (start2_addr, start1_addr, len2_byte);
           bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);