]> code.delx.au - gnu-emacs/blobdiff - src/editfns.c
(Fbyte_code): Harmonize arguments with documentation.
[gnu-emacs] / src / editfns.c
index 778646be5a1bf945b00970fab67303086c949449..e115300e24bffbd389dd197191f3526504ca8359 100644 (file)
@@ -38,8 +38,16 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
+extern char **environ;
+extern Lisp_Object make_time ();
 extern void insert_from_buffer ();
 static long difftm ();
+static void set_time_zone_rule ();
+static void update_buffer_properties ();
+
+Lisp_Object Vbuffer_access_fontify_functions;
+Lisp_Object Qbuffer_access_fontify_functions;
+Lisp_Object Vbuffer_access_fontified_property;
 
 /* Some static data, and a function to initialize it for each run */
 
@@ -540,7 +548,7 @@ DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
 }
 
 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
-  "Return the current time, as the number of seconds since 12:00 AM January 1970.\n\
+  "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
 The time is returned as a list of three integers.  The first has the\n\
 most significant 16 bits of the seconds, while the second has the\n\
 least significant 16 bits.  The third integer gives the microsecond\n\
@@ -691,102 +699,79 @@ ZONE is an integer indicating the number of seconds east of Greenwich.\n\
   return Flist (9, list_args);
 }
 
-static char days_per_month[11]
-  = { 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 };
-
 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, 7, 0,
   "Convert SEC, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
 This is the reverse operation of `decode-time', which see.  ZONE defaults\n\
-to the current time zone and daylight savings time if not specified; if\n\
-specified, it can be either a list (as from `current-time-zone') or an\n\
-integer (as from `decode-time'), and is applied without consideration for\n\
-daylight savings time.\n\
+to the current time zone rule if not specified; if specified, it can\n\
+be a string (as from `set-time-zone-rule'), or it can be a list\n\
+(as from `current-time-zone') or an integer (as from `decode-time')\n\
+applied without consideration for daylight savings time.\n\
+Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
+for example, a DAY of 0 means the day preceding the given month.\n\
 Year numbers less than 100 are treated just like other year numbers.\n\
-If you want them to stand for years above 1900, you must do that yourself.")
+If you want them to stand for years in this century, you must do that yourself.")
   (sec, minute, hour, day, month, year, zone)
      Lisp_Object sec, minute, hour, day, month, year, zone;
 {
   time_t time;
-  int fullyear, mon, days, seconds, tz = 0;
-
-  CHECK_NATNUM (sec, 0);
-  CHECK_NATNUM (minute, 1);
-  CHECK_NATNUM (hour, 2);
-  CHECK_NATNUM (day, 3);
-  CHECK_NATNUM (month, 4);
-  CHECK_NATNUM (year, 5);
-
-  fullyear = XINT (year);
-
-  /* Adjust incoming datespec to epoch = March 1, year 0.
-     The "date" March 1, year 0, is an abstraction used purely for its
-     computational convenience; year 0 never existed.  */
-  mon = XINT (month) - 1 + 10;
-  fullyear += mon/12 - 1;
-  mon %= 12;
-
-  days = XINT (day) - 1;               /* day of month */
-  while (mon-- > 0)                    /* day of year */
-    days += days_per_month[mon];
-  days += 146097 * (fullyear/400);     /* 400 years = 146097 days */
-  fullyear %= 400;
-  days += 36524 * (fullyear/100);      /* 100 years = 36524 days */
-  fullyear %= 100;
-  days += 1461 * (fullyear/4);         /* 4 years = 1461 days */
-  fullyear %= 4;
-  days += 365 * fullyear;              /* 1 year = 365 days */
-
-  /* Adjust computed datespec to epoch = January 1, 1970.  */
-  days += 59;                          /* March 1 is 59th day.  */
-  days -= 719527;                      /* 1970 years = 719527 days */
-
-  seconds = XINT (sec) + 60 * XINT (minute) + 3600 * XINT (hour);
-
-  if (sizeof (time_t) == 4
-      && ((days+(seconds/86400) > 24854) || (days+(seconds/86400) < -24854)))
-    error ("the specified time is outside the representable range");
-
-  time = days * 86400 + seconds;
-
-  /* We have the correct value for UTC.  Adjust for timezones.  */
+  struct tm tm;
+
+  CHECK_NUMBER (sec, 0);
+  CHECK_NUMBER (minute, 1);
+  CHECK_NUMBER (hour, 2);
+  CHECK_NUMBER (day, 3);
+  CHECK_NUMBER (month, 4);
+  CHECK_NUMBER (year, 5);
+
+  tm.tm_sec = XINT (sec);
+  tm.tm_min = XINT (minute);
+  tm.tm_hour = XINT (hour);
+  tm.tm_mday = XINT (day);
+  tm.tm_mon = XINT (month) - 1;
+  tm.tm_year = XINT (year) - 1900;
+  tm.tm_isdst = -1;
+
+  if (CONSP (zone))
+    zone = Fcar (zone);
   if (NILP (zone))
+    time = mktime (&tm);
+  else
     {
-      struct tm gmt, *t;
-      time_t adjusted_time;
-      int adjusted_tz;
-      /* If the system does not use timezones, gmtime returns 0, and we
-        already have the correct value, by definition.  */
-      if ((t = gmtime (&time)) != 0)
+      char tzbuf[100];
+      char *tzstring;
+      char **oldenv = environ, **newenv;
+      
+      if (STRINGP (zone))
+       tzstring = (char *) XSTRING (zone)->data;
+      else if (INTEGERP (zone))
        {
-         gmt = *t;
-         t = localtime (&time);
-         tz = difftm (t, &gmt);
-         /* The timezone returned is that at the specified Universal Time,
-            not the local time, which is what we want.  Adjust, repeat.  */
-         adjusted_time = time - tz;
-         gmt = *gmtime (&adjusted_time); /* this is safe now */
-         t = localtime (&adjusted_time);
-         adjusted_tz = difftm (t, &gmt);
-         /* In case of discrepancy, adjust again for extra accuracy.  */
-         if (adjusted_tz != tz)
-           {
-             adjusted_time = time - adjusted_tz;
-             gmt = *gmtime (&adjusted_time);
-             t = localtime (&adjusted_time);
-             adjusted_tz = difftm (t, &gmt);
-           }
-         tz = adjusted_tz;
+         int abszone = abs (XINT (zone));
+         sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
+                  abszone / (60*60), (abszone/60) % 60, abszone % 60);
+         tzstring = tzbuf;
        }
-    }
-  else 
-    {
-      if (CONSP (zone))
-       zone = Fcar (zone);
-      CHECK_NUMBER (zone, 6);
-      tz = XINT (zone);
+      else
+       error ("Invalid time zone specification");
+
+      /* Set TZ before calling mktime; merely adjusting mktime's returned 
+        value doesn't suffice, since that would mishandle leap seconds.  */
+      set_time_zone_rule (tzstring);
+
+      time = mktime (&tm);
+
+      /* Restore TZ to previous value.  */
+      newenv = environ;
+      environ = oldenv;
+      free (newenv);
+#ifdef LOCALTIME_CACHE
+      tzset ();
+#endif
     }
 
-  return make_time (time - tz);
+  if (time == (time_t) -1)
+    error ("Specified time is not representable");
+
+  return make_time (time);
 }
 
 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
@@ -899,16 +884,17 @@ the data it can't find.")
     return Fmake_list (2, Qnil);
 }
 
+/* This holds the value of `environ' produced by the previous
+   call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
+   has never been called.  */
+static char **environbuf;
+
 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
   "Set the local time zone using TZ, a string specifying a time zone rule.\n\
 If TZ is nil, use implementation-defined default time zone information.")
   (tz)
      Lisp_Object tz;
 {
-  extern char **environ;
-  static char **environbuf;
-  int envptrs;
-  char **from, **to, **newenv;
   char *tzstring;
 
   if (NILP (tz))
@@ -916,9 +902,27 @@ If TZ is nil, use implementation-defined default time zone information.")
   else
     {
       CHECK_STRING (tz, 0);
-      tzstring = XSTRING (tz)->data;
+      tzstring = (char *) XSTRING (tz)->data;
     }
 
+  set_time_zone_rule (tzstring);
+  if (environbuf)
+    free (environbuf);
+  environbuf = environ;
+
+  return Qnil;
+}
+
+/* Set the local time zone rule to TZSTRING.
+   This allocates memory into `environ', which it is the caller's
+   responsibility to free.  */
+static void
+set_time_zone_rule (tzstring)
+     char *tzstring;
+{
+  int envptrs;
+  char **from, **to, **newenv;
+
   for (from = environ; *from; from++)
     continue;
   envptrs = from - environ + 2;
@@ -938,15 +942,10 @@ If TZ is nil, use implementation-defined default time zone information.")
   *to = 0;
 
   environ = newenv;
-  if (environbuf)
-    free (environbuf);
-  environbuf = newenv;
 
 #ifdef LOCALTIME_CACHE
   tzset ();
 #endif
-
-  return Qnil;
 }
 \f
 void
@@ -1152,7 +1151,7 @@ from adjoining text, if those properties are sticky.")
 /* Return a Lisp_String containing the text of the current buffer from
    START to END.  If text properties are in use and the current buffer
    has properties in the range specified, the resulting string will also
-   have them.
+   have them, if PROPS is nonzero.
 
    We don't want to use plain old make_string here, because it calls
    make_uninit_string, which can cause the buffer arena to be
@@ -1163,8 +1162,9 @@ from adjoining text, if those properties are sticky.")
    buffer substrings.  */
 
 Lisp_Object
-make_buffer_string (start, end)
+make_buffer_string (start, end, props)
      int start, end;
+     int props;
 {
   Lisp_Object result, tem, tem1;
 
@@ -1174,17 +1174,58 @@ make_buffer_string (start, end)
   result = make_uninit_string (end - start);
   bcopy (&FETCH_CHAR (start), XSTRING (result)->data, end - start);
 
-  tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
-  tem1 = Ftext_properties_at (make_number (start), Qnil);
-
+  /* If desired, update and copy the text properties.  */
 #ifdef USE_TEXT_PROPERTIES
-  if (XINT (tem) != end || !NILP (tem1))
-    copy_intervals_to_string (result, current_buffer, start, end - start);
+  if (props)
+    {
+      update_buffer_properties (start, end);
+
+      tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
+      tem1 = Ftext_properties_at (make_number (start), Qnil);
+
+      if (XINT (tem) != end || !NILP (tem1))
+       copy_intervals_to_string (result, current_buffer, start, end - start);
+    }
 #endif
 
   return result;
 }
 
+/* Call Vbuffer_access_fontify_functions for the range START ... END
+   in the current buffer, if necessary.  */
+
+static void
+update_buffer_properties (start, end)
+     int start, end;
+{
+#ifdef USE_TEXT_PROPERTIES
+  /* If this buffer has some access functions,
+     call them, specifying the range of the buffer being accessed.  */
+  if (!NILP (Vbuffer_access_fontify_functions))
+    {
+      Lisp_Object args[3];
+      Lisp_Object tem;
+
+      args[0] = Qbuffer_access_fontify_functions;
+      XSETINT (args[1], start);
+      XSETINT (args[2], end);
+
+      /* But don't call them if we can tell that the work
+        has already been done.  */
+      if (!NILP (Vbuffer_access_fontified_property))
+       {
+         tem = Ftext_property_any (args[1], args[2],
+                                   Vbuffer_access_fontified_property,
+                                   Qnil, Qnil);
+         if (! NILP (tem))
+           Frun_hook_with_args (3, &args);
+       }
+      else
+       Frun_hook_with_args (3, &args);
+    }
+#endif
+}
+
 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
   "Return the contents of part of the current buffer as a string.\n\
 The two arguments START and END are character positions;\n\
@@ -1198,7 +1239,24 @@ they can be in either order.")
   beg = XINT (b);
   end = XINT (e);
 
-  return make_buffer_string (beg, end);
+  return make_buffer_string (beg, end, 1);
+}
+
+DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
+       Sbuffer_substring_no_properties, 2, 2, 0,
+  "Return the characters of part of the buffer, without the text properties.\n\
+The two arguments START and END are character positions;\n\
+they can be in either order.")
+  (b, e)
+     Lisp_Object b, e;
+{
+  register int beg, end;
+
+  validate_region (&b, &e);
+  beg = XINT (b);
+  end = XINT (e);
+
+  return make_buffer_string (beg, end, 0);
 }
 
 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
@@ -1207,7 +1265,7 @@ If narrowing is in effect, this function returns only the visible part\n\
 of the buffer.")
   ()
 {
-  return make_buffer_string (BEGV, ZV);
+  return make_buffer_string (BEGV, ZV, 1);
 }
 
 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
@@ -1220,7 +1278,7 @@ They default to the beginning and the end of BUFFER.")
      Lisp_Object buf, b, e;
 {
   register int beg, end, temp;
-  register struct buffer *bp;
+  register struct buffer *bp, *obuf;
   Lisp_Object buffer;
 
   buffer = Fget_buffer (buf);
@@ -1249,6 +1307,11 @@ They default to the beginning and the end of BUFFER.")
   if (!(BUF_BEGV (bp) <= beg && end <= BUF_ZV (bp)))
     args_out_of_range (b, e);
 
+  obuf = current_buffer;
+  set_buffer_internal_1 (bp);
+  update_buffer_properties (beg, end);
+  set_buffer_internal_1 (obuf);
+
   insert_from_buffer (bp, beg, end - beg, 0);
   return Qnil;
 }
@@ -1687,12 +1750,9 @@ minibuffer contents show.")
 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
   "Display a message, in a dialog box if possible.\n\
 If a dialog box is not available, use the echo area.\n\
-The first argument is a control string.\n\
-It may contain %s or %d or %c to print successive following arguments.\n\
-%s means print an argument as a string, %d means print as number in decimal,\n\
-%c means print a number as a single character.\n\
-The argument used by %s must be a string or a symbol;\n\
-the argument used by %d or %c must be a number.\n\
+The first argument is a format control string, and the rest are data\n\
+to be formatted under control of the string.  See `format' for details.\n\
+\n\
 If the first argument is nil, clear any existing message; let the\n\
 minibuffer contents show.")
   (nargs, args)
@@ -1708,7 +1768,7 @@ minibuffer contents show.")
     {
       register Lisp_Object val;
       val = Fformat (nargs, args);
-#ifdef HAVE_X_MENU
+#ifdef HAVE_MENUS
       {
        Lisp_Object pane, menu, obj;
        struct gcpro gcpro1;
@@ -1719,7 +1779,7 @@ minibuffer contents show.")
        UNGCPRO;
        return val;
       }
-#else
+#else /* not HAVE_MENUS */
       /* Copy the data so that it won't move when we GC.  */
       if (! message_text)
        {
@@ -1734,30 +1794,27 @@ minibuffer contents show.")
       bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
       message2 (message_text, XSTRING (val)->size);
       return val;
-#endif
+#endif /* not HAVE_MENUS */
     }
 }
-#ifdef HAVE_X_MENU
+#ifdef HAVE_MENUS
 extern Lisp_Object last_nonmenu_event;
 #endif
+
 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
   "Display a message in a dialog box or in the echo area.\n\
 If this command was invoked with the mouse, use a dialog box.\n\
 Otherwise, use the echo area.\n\
+The first argument is a format control string, and the rest are data\n\
+to be formatted under control of the string.  See `format' for details.\n\
 \n\
-The first argument is a control string.\n\
-It may contain %s or %d or %c to print successive following arguments.\n\
-%s means print an argument as a string, %d means print as number in decimal,\n\
-%c means print a number as a single character.\n\
-The argument used by %s must be a string or a symbol;\n\
-the argument used by %d or %c must be a number.\n\
 If the first argument is nil, clear any existing message; let the\n\
 minibuffer contents show.")
   (nargs, args)
      int nargs;
      Lisp_Object *args;
 {
-#ifdef HAVE_X_MENU
+#ifdef HAVE_MENUS
   if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
     return Fmessage_box (nargs, args);
 #endif
@@ -1906,8 +1963,12 @@ Use %% to put a single % into the output.")
            strings[i++] = (unsigned char *) u.half[1];
          }
 #endif
-       else
+       else if (i == 0)
+         /* The first string is treated differently
+            because it is the format string.  */
          strings[i++] = XSTRING (args[n])->data;
+       else
+         strings[i++] = (unsigned char *) XFASTINT (args[n]);
       }
 
     /* Make room in result for all the non-%-codes in the control string.  */
@@ -1919,7 +1980,8 @@ Use %% to put a single % into the output.")
        buf = (char *) alloca (total + 1);
        buf[total - 1] = 0;
 
-       length = doprnt (buf, total + 1, strings[0], end, i-1, strings + 1);
+       length = doprnt_lisp (buf, total + 1, strings[0],
+                             end, i-1, strings + 1);
        if (buf[total - 1] == 0)
          break;
 
@@ -1963,12 +2025,13 @@ Case is ignored if `case-fold-search' is non-nil in the current buffer.")
   (c1, c2)
      register Lisp_Object c1, c2;
 {
-  unsigned char *downcase = DOWNCASE_TABLE;
+  Lisp_Object *downcase = DOWNCASE_TABLE;
   CHECK_NUMBER (c1, 0);
   CHECK_NUMBER (c2, 1);
 
   if (!NILP (current_buffer->case_fold_search)
-      ? (downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
+      ? ((XINT (downcase[0xff & XFASTINT (c1)])
+         == XINT (downcase[0xff & XFASTINT (c2)]))
         && (XFASTINT (c1) & ~0xff) == (XFASTINT (c2) & ~0xff))
       : XINT (c1) == XINT (c2))
     return Qt;
@@ -2309,6 +2372,26 @@ Transposing beyond buffer boundaries is an error.")
 void
 syms_of_editfns ()
 {
+  environbuf = 0;
+
+  Qbuffer_access_fontify_functions
+    = intern ("buffer-access-fontify-functions");
+  staticpro (&Qbuffer_access_fontify_functions);
+
+  DEFVAR_LISP ("buffer-access-fontify-functions",
+              &Vbuffer_access_fontify_functions,
+              "List of functions called by `buffer-substring' to fontify if necessary.\n\
+Each function is called with two arguments which specify the range\n\
+of the buffer being accessed.");
+  Vbuffer_access_fontify_functions = Qnil;
+
+  DEFVAR_LISP ("buffer_access_fontified_property",
+              &Vbuffer_access_fontified_property,
+       "Property which (if non-nil) indicates text has been fontified.\n\
+`buffer-substring' need not call the `buffer-access-fontify-functions'\n\
+functions if all the text being accessed has this property.");
+  Vbuffer_access_fontified_property = Qnil;
+
   DEFVAR_LISP ("system-name", &Vsystem_name,
               "The name of the machine Emacs is running on.");
   
@@ -2326,6 +2409,7 @@ syms_of_editfns ()
   defsubr (&Sstring_to_char);
   defsubr (&Schar_to_string);
   defsubr (&Sbuffer_substring);
+  defsubr (&Sbuffer_substring_no_properties);
   defsubr (&Sbuffer_string);
 
   defsubr (&Spoint_marker);