home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / editfns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-14  |  44.2 KB  |  1,603 lines

  1. /* Lisp functions pertaining to editing.
  2.    Copyright (C) 1985, 1986, 1987, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22.  
  23. #include <stdarg.h>
  24. #include <ctype.h>
  25.  
  26. #ifdef VMS
  27. #include "vms-pwd.h"
  28. #else
  29. #include <pwd.h>
  30. #endif
  31.  
  32. #include "lisp.h"
  33. #include "buffer.h"
  34. #include "window.h"
  35. #include "insdel.h"
  36. #include "screen.h"             /* for SCREENP */
  37. #include "events.h"             /* for EVENTP */
  38.  
  39. #include "systime.h"
  40.  
  41. #define min(a, b) ((a) < (b) ? (a) : (b))
  42. #define max(a, b) ((a) > (b) ? (a) : (b))
  43.  
  44. /* Some static data, and a function to initialize it for each run */
  45.  
  46. Lisp_Object Vsystem_name;
  47. Lisp_Object Vuser_real_name;    /* login name of current user ID */
  48. Lisp_Object Vuser_full_name;    /* full name of current user */
  49. Lisp_Object Vuser_name;        /* user name from USER or LOGNAME.  */
  50.  
  51. extern char *get_system_name (void);
  52.  
  53. Lisp_Object Qpoint, Qmark, Qregion_beginning, Qregion_end;
  54.  
  55. void
  56. init_editfns ()
  57. {
  58.   char *user_name;
  59.   register unsigned char *p, *q;
  60.   struct passwd *pw;    /* password entry for the current user */
  61.   Lisp_Object tem;
  62.  
  63.   /* Set up system_name even when dumping.  */
  64.  
  65.   Vsystem_name = build_string (get_system_name ());
  66.   p = XSTRING (Vsystem_name)->data;
  67.   while (*p)
  68.     {
  69.       if (*p == ' ' || *p == '\t')
  70.     *p = '-';
  71.       p++;
  72.     }
  73.  
  74. #ifndef CANNOT_DUMP
  75.   /* Don't bother with this on initial start when just dumping out */
  76.   if (!initialized)
  77.     return;
  78. #endif /* not CANNOT_DUMP */
  79.  
  80.   pw = (struct passwd *) getpwuid (getuid ());
  81.   Vuser_real_name = build_string (pw ? pw->pw_name : "unknown");
  82.  
  83.   /* Get the effective user name, by consulting environment variables,
  84.      or the effective uid if those are unset.  */
  85.   user_name = getenv ("USER");
  86.   if (!user_name)
  87.     user_name = getenv ("LOGNAME");
  88.   if (!user_name)
  89.     {
  90.       pw = (struct passwd *) getpwuid (geteuid ());
  91.       user_name = (char *) (pw ? pw->pw_name : "unknown");
  92.     }
  93.   Vuser_name = build_string (user_name);
  94.  
  95.   /* If the user name claimed in the environment vars differs from
  96.      the real uid, use the claimed name to find the full name.  */
  97.   tem = Fstring_equal (Vuser_name, Vuser_real_name);
  98.   if (NILP (tem))
  99.     pw = (struct passwd *) getpwnam ((char *) XSTRING (Vuser_name)->data);
  100.   
  101.   p = (unsigned char *) ((pw) ? USER_FULL_NAME : "unknown");
  102.   q = (unsigned char *) strchr ((char *) p, ',');
  103.   Vuser_full_name = make_string ((char *) p,
  104.                  (q ? q - p : strlen ((char *) p)));
  105.   
  106. #ifdef AMPERSAND_FULL_NAME
  107.   p = (char *) XSTRING (Vuser_full_name)->data;
  108.   q = strchr (p, '&');
  109.   /* Substitute the login name for the &, upcasing the first character.  */
  110.   if (q)
  111.     {
  112.       char *r = (char *) alloca (strlen (p) + XSTRING (Vuser_name)->size + 1);
  113.       memcpy (r, p, q - p);
  114.       r[q - p] = 0;
  115.       strcat (r, (char *) XSTRING (Vuser_name)->data);
  116.       r[q - p] = UPCASE (r[q - p]);
  117.       strcat (r, q + 1);
  118.       Vuser_full_name = build_string (r);
  119.     }
  120. #endif /* AMPERSAND_FULL_NAME */
  121. }
  122.  
  123. DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
  124.   "Convert arg CHAR to a one-character string containing that character.")
  125.   (n)
  126.      Lisp_Object n;
  127. {
  128.   char c;
  129.  
  130.   if (EVENTP (n))
  131.     {
  132.       Lisp_Object ch = Fevent_to_character (n, Qt);
  133.       if (NILP (ch))
  134.     return
  135.       Fsignal (Qerror,
  136.            list2 (build_string ("character has no ASCII equivalent:"),
  137.               Fcopy_event (n, Qnil)));
  138.       n = ch;
  139.     }
  140.  
  141.   CHECK_FIXNUM (n, 0);
  142.  
  143.   c = XINT (n);
  144.   return make_string (&c, 1);
  145. }
  146.  
  147. DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
  148.   "Convert arg STRING to a character, the first character of that string.")
  149.   (str)
  150.      register Lisp_Object str;
  151. {
  152.   register Lisp_Object val;
  153.   register struct Lisp_String *p;
  154.   CHECK_STRING (str, 0);
  155.  
  156.   p = XSTRING (str);
  157.   if (p->size)
  158.     XFASTINT (val) = ((unsigned char *) p->data)[0];
  159.   else
  160.     XFASTINT (val) = 0;
  161.   return val;
  162. }
  163.  
  164. static Lisp_Object
  165. buildmark (val)
  166.      int val;
  167. {
  168.   register Lisp_Object mark;
  169.   mark = Fmake_marker ();
  170.   Fset_marker (mark, make_number (val), Qnil);
  171.   return mark;
  172. }
  173.  
  174. DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
  175.   "Return value of point, as an integer.\n\
  176. Beginning of buffer is position (point-min)")
  177.   ()
  178. {
  179.   Lisp_Object temp;
  180.   XFASTINT (temp) = point;
  181.   return temp;
  182. }
  183.  
  184. DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 1, 0,
  185.    "Return value of point, as a marker object.\n\
  186. This marker is a copy; you may modify it with reckless abandon.\n\
  187. If the argument to this function is non-nil, then it returns the real\n\
  188. point-marker; modifying the position of this marker willl move point.\n\
  189. It is illegal to change the buffer of it, or make it point nowhere.")
  190.   (dont_copy_p)
  191.   Lisp_Object dont_copy_p;
  192. {
  193.   if (NILP (dont_copy_p))
  194.     return Fcopy_marker (current_buffer->point_marker);
  195.   return current_buffer->point_marker;
  196. }
  197.  
  198. int
  199. clip_to_bounds (int lower, int num, int upper)
  200. {
  201.   if (num < lower)
  202.     return lower;
  203.   else if (num > upper)
  204.     return upper;
  205.   else
  206.     return num;
  207. }
  208.  
  209. DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
  210.   "Set point to POSITION, a number or marker.\n\
  211. Beginning of buffer is position (point-min), end is (point-max).")
  212.   (n)
  213.      register Lisp_Object n;
  214. {
  215.   CHECK_FIXNUM_COERCE_MARKER (n, 0);
  216.  
  217.   SET_PT (clip_to_bounds (BEGV, XINT (n), ZV));
  218.   return n;
  219. }
  220.  
  221. static Lisp_Object
  222. region_limit (beginningp)
  223.      int beginningp;
  224. {
  225.   register Lisp_Object m;
  226.  
  227.   m = Fmarker_position (current_buffer->mark);
  228.   if (NILP (m)) error ("There is no region now");
  229.   if ((point < XFASTINT (m)) == beginningp)
  230.     return (make_number (point));
  231.   else
  232.     return (m);
  233. }
  234.  
  235. DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
  236.   "Return position of beginning of region, as an integer.")
  237.   ()
  238. {
  239.   return (region_limit (1));
  240. }
  241.  
  242. DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
  243.   "Return position of end of region, as an integer.")
  244.   ()
  245. {
  246.   return (region_limit (0));
  247. }
  248.  
  249. /* Whether to use lispm-style active-regions */
  250. int zmacs_regions;
  251.  
  252. /* Whether the region is currently active.  This is not per-buffer because
  253.    there can be only one active region at a time (since it does double-duty
  254.    as the X primary selection.)
  255.  */
  256. int zmacs_region_active_p;
  257.  
  258. int zmacs_region_stays;
  259.  
  260. Lisp_Object Vzmacs_update_region_hook,     Qzmacs_update_region_hook;
  261. Lisp_Object Vzmacs_activate_region_hook,   Qzmacs_activate_region_hook;
  262. Lisp_Object Vzmacs_deactivate_region_hook, Qzmacs_deactivate_region_hook;
  263.  
  264. DEFUN ("zmacs-activate-region", Fzmacs_activate_region,
  265.        Szmacs_activate_region, 0, 0, 0,
  266.  "Make the region between `point' and `mark' be in the active (highlighted)\n\
  267. state, if `zmacs-regions' is true.  Only a very small number of commands\n\
  268. should ever do this.")
  269.     ()
  270. {
  271.   if (! zmacs_regions) return Qnil;
  272.   zmacs_region_active_p++;
  273.   if (!NILP (Vrun_hooks) && zmacs_region_active_p == 1)
  274.     call1 (Vrun_hooks, Qzmacs_activate_region_hook);
  275.   return Qt;
  276. }
  277.  
  278.  
  279. DEFUN ("zmacs-deactivate-region", Fzmacs_deactivate_region,
  280.        Szmacs_deactivate_region, 0, 0, 0,
  281.  "Make the region between `point' and `mark' no longer be in the active\n\
  282. (highlighted) state, if `zmacs-regions' is true.  You shouldn't need to call\n\
  283. this; the command loop calls it when appropriate.\n\
  284. Returns t if the region had been active, nil otherwise.")
  285.     ()
  286. {
  287.   if (! zmacs_region_active_p)
  288.     return Qnil;
  289.   zmacs_region_active_p = 0;
  290.   if (!NILP (Vrun_hooks))
  291.     call1 (Vrun_hooks, Qzmacs_deactivate_region_hook);
  292.   return Qt;
  293. }
  294.  
  295.  
  296. void
  297. zmacs_update_region ()
  298. {
  299.   if (zmacs_region_active_p && !NILP (Vrun_hooks))
  300.     call1 (Vrun_hooks, Qzmacs_update_region_hook);
  301. }
  302.  
  303.  
  304. DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 1, 0,
  305.   "Return this buffer's mark, as a marker object.\n\
  306. If `zmacs-regions' is true, then this returns nil unless the region is\n\
  307. currently in the active (hilighted) state.  With an argument of t, this\n\
  308. returns the mark (if there is one) regardless of the zmacs-region state.\n\
  309. You should *generally* not use the mark unless the region is active, if\n\
  310. the user has expressed a preference for the zmacs-region model.\n\
  311. Watch out!  Moving this marker changes the mark position.\n\
  312. If you set the marker not to point anywhere, the buffer will have no mark.")
  313.   (inactive_p)
  314.     Lisp_Object inactive_p;
  315. {
  316.   if (! zmacs_regions || zmacs_region_active_p || !NILP (inactive_p))
  317.     return current_buffer->mark;
  318.   return Qnil;
  319. }
  320.  
  321.  
  322. Lisp_Object
  323. save_excursion_save ()
  324. {
  325.   register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
  326.               == current_buffer);
  327.  
  328.   if (XINT (Fpoint ()) != XINT (Fmarker_position (Fpoint_marker (Qt))))
  329.     abort ();
  330.   return Fcons (Fpoint_marker (Qnil),
  331.         Fcons (Fcopy_marker (current_buffer->mark),
  332.                ((visible) ? Qt : Qnil)));
  333. }
  334.  
  335. Lisp_Object
  336. save_excursion_restore (info)
  337.      register Lisp_Object info;
  338. {
  339.   register Lisp_Object tem;
  340.  
  341.   tem = Fmarker_buffer (Fcar (info));
  342.   /* If buffer being returned to is now deleted, avoid error */
  343.   /* Otherwise could get error here while unwinding to top level
  344.      and crash */
  345.   /* In that case, Fmarker_buffer returns nil now.  */
  346.   if (NILP (tem))
  347.     return Qnil;
  348.   Fset_buffer (tem);
  349.   tem = Fcar (info);
  350.   Fgoto_char (tem);
  351.   unchain_marker (tem);
  352.   tem = Fcar (Fcdr (info));
  353.   Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
  354.   unchain_marker (tem);
  355.   tem = Fcdr (Fcdr (info));
  356.   if (!NILP (tem) && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
  357.     Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
  358.   return Qnil;
  359. }
  360.  
  361. DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
  362.   "Save point, mark, and current buffer; execute BODY; restore those things.\n\
  363. Executes BODY just like `progn'.\n\
  364. The values of point, mark and the current buffer are restored\n\
  365. even in case of abnormal exit (throw or error).")
  366.   (args)
  367.      Lisp_Object args;
  368. {
  369.   int speccount = specpdl_depth;
  370.  
  371.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  372.              
  373.   return unbind_to (speccount, Fprogn (args));
  374. }
  375.  
  376. DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
  377.   "Return the number of characters in the current buffer.")
  378.   ()
  379. {
  380.   Lisp_Object temp;
  381.   XFASTINT (temp) = Z - BEG;
  382.   return temp;
  383. }
  384.  
  385. DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
  386.   "Return the minimum permissible value of point in the current buffer.\n\
  387. This is 1, unless a clipping restriction is in effect.")
  388.   ()
  389. {
  390.   Lisp_Object temp;
  391.   XFASTINT (temp) = BEGV;
  392.   return temp;
  393. }
  394.  
  395. DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
  396.   "Return a marker to the minimum permissible value of point in this buffer.\n\
  397. This is the beginning, unless a clipping restriction is in effect.")
  398.   ()
  399. {
  400.   return buildmark (BEGV);
  401. }
  402.  
  403. DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
  404.   "Return the maximum permissible value of point in the current buffer.\n\
  405. This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
  406. in which case it is less.")
  407.   ()
  408. {
  409.   Lisp_Object temp;
  410.   XFASTINT (temp) = ZV;
  411.   return temp;
  412. }
  413.  
  414. DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
  415.   "Return a marker to the maximum permissible value of point in this buffer.\n\
  416. This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
  417. in which case it is less.")
  418.   ()
  419. {
  420.   return buildmark (ZV);
  421. }
  422.  
  423. DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
  424.   "Return the character following point, as a number.\n\
  425. At the end of the buffer or accessible region, return 0.")
  426.   ()
  427. {
  428.   Lisp_Object temp;
  429.   if (point >= ZV)
  430.     XFASTINT (temp) = 0;
  431.   else
  432.     XFASTINT (temp) = CHAR_AT (point);
  433.   return temp;
  434. }
  435.  
  436. DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
  437.   "Return the character preceding point, as a number.\n\
  438. At the beginning of the buffer or accessible region, return 0.")
  439.   ()
  440. {
  441.   Lisp_Object temp;
  442.   if (point <= BEGV)
  443.     XFASTINT (temp) = 0;
  444.   else
  445.     XFASTINT (temp) = CHAR_AT (point - 1);
  446.   return temp;
  447. }
  448.  
  449. DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
  450.   "Return T if point is at the beginning of the buffer.\n\
  451. If the buffer is narrowed, this means the beginning of the narrowed part.")
  452.   ()
  453. {
  454.   if (point == BEGV)
  455.     return Qt;
  456.   return Qnil;
  457. }
  458.  
  459. DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
  460.   "Return T if point is at the end of the buffer.\n\
  461. If the buffer is narrowed, this means the end of the narrowed part.")
  462.   ()
  463. {
  464.   if (point == ZV)
  465.     return Qt;
  466.   return Qnil;
  467. }
  468.  
  469. DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
  470.   "Return T if point is at the beginning of a line.")
  471.   ()
  472. {
  473.   if (point == BEGV || CHAR_AT (point - 1) == '\n')
  474.     return Qt;
  475.   return Qnil;
  476. }
  477.  
  478. DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
  479.   "Return T if point is at the end of a line.\n\
  480. `End of a line' includes point being at the end of the buffer.")
  481.   ()
  482. {
  483.   if (point == ZV || CHAR_AT (point) == '\n')
  484.     return Qt;
  485.   return Qnil;
  486. }
  487.  
  488. DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
  489.   "Return character in current buffer at position POS.\n\
  490. POS is an integer or a buffer pointer.\n\
  491. If POS is out of range, the value is nil.")
  492.   (pos)
  493.      Lisp_Object pos;
  494. {
  495.   register Lisp_Object val;
  496.   register int n;
  497.  
  498.   CHECK_FIXNUM_COERCE_MARKER (pos, 0);
  499.  
  500.   n = XINT (pos);
  501.   if (n < BEGV || n >= ZV) return Qnil;
  502.  
  503.   XFASTINT (val) = CHAR_AT (n);
  504.   return val;
  505. }
  506.  
  507. DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 0, 0,
  508.   "Return the name under which the user logged in, as a string.\n\
  509. This is based on the effective uid, not the real uid.\n\
  510. Also, if the environment variable USER or LOGNAME is set,\n\
  511. that determines the value of this function.")
  512.   ()
  513. {
  514.   return Vuser_name;
  515. }
  516.  
  517. DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
  518.   0, 0, 0,
  519.   "Return the name of the user's real uid, as a string.\n\
  520. Differs from `user-login-name' when running under `su'.")
  521.   ()
  522. {
  523.   return Vuser_real_name;
  524. }
  525.  
  526. DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
  527.   "Return the effective uid of Emacs, as an integer.")
  528.   ()
  529. {
  530.   return make_number (geteuid ());
  531. }
  532.  
  533. DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
  534.   "Return the real uid of Emacs, as an integer.")
  535.   ()
  536. {
  537.   return make_number (getuid ());
  538. }
  539.  
  540. DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, 0,
  541.   "Return the full name of the user logged in, as a string.")
  542.   ()
  543. {
  544.   return Vuser_full_name;
  545. }
  546.  
  547. DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
  548.   "Return the name of the machine you are running on, as a string.")
  549.   ()
  550. {
  551.   return Vsystem_name;
  552. }
  553.  
  554. #if 0
  555. /* This is the FSF's version.  A merge is in order...
  556.  */
  557. DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
  558.   "Return the current time, as the number of seconds since 12:00 AM January 1970.\n\
  559. The time is returned as a list of three integers.  The first has the\n\
  560. most significant 16 bits of the seconds, while the second has the\n\
  561. least significant 16 bits.  The third integer gives the microsecond\n\
  562. count.\n\
  563. \n\
  564. The microsecond count is zero on systems that do not provide\n\
  565. resolution finer than a second.")
  566.   ()
  567. {
  568.   EMACS_TIME t;
  569.   Lisp_Object result[3];
  570.  
  571.   EMACS_GET_TIME (t);
  572.   XSET (result[0], Lisp_Int, (EMACS_SECS (t) >> 16) & 0xffff);
  573.   XSET (result[1], Lisp_Int, (EMACS_SECS (t) >> 0)  & 0xffff);
  574.   XSET (result[2], Lisp_Int, EMACS_USECS (t));
  575.  
  576.   return Flist (3, result);
  577. }
  578. #endif
  579.  
  580. DEFUN ("current-time-seconds", Fcurrent_time_seconds, Scurrent_time_seconds,
  581.        0, 1, 0,
  582. "Returns the current time as the number of seconds since Jan. 1, 1970, 00:00,\n\
  583. Greenwich Mean Time.  Since emacs can't represent integers that large, this\n\
  584. is returned as a cons of two 16-bit numbers.  You can pass this value as an\n\
  585. argument to current-time-string to get that time as a string.")
  586.    (cons)
  587.    Lisp_Object cons;
  588. {
  589.   unsigned int top, bot;
  590.   long now = time ((long *) 0);
  591.   top = (now >> 16) & 0xFFFF;
  592.   bot = now & 0xFFFF;
  593.   XSET (top, Lisp_Int, top);
  594.   XSET (bot, Lisp_Int, bot);
  595.   if (NILP (cons))
  596.     return Fcons (top, bot);
  597.   CHECK_CONS (cons, 0);
  598.   Fsetcar (cons, top);
  599.   Fsetcdr (cons, bot);
  600.   return cons;
  601. }
  602.  
  603. DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
  604.   "Return the current time, as a human-readable string.\n\
  605. Programs can use it too, since the number of columns in each field is fixed.\n\
  606. The format is `Sun Sep 16 01:03:52 1973'.\n\
  607. If the optional argument is non-nil, it must be a cons of two integers, which\n\
  608. are the upper and lower 16 bits of a time in seconds (such as the value \n\
  609. returned by the current-time-seconds function.\n\
  610. In a future Emacs version, the time zone may be added at the end.")
  611.   (arg)
  612.   Lisp_Object arg;
  613. {
  614.   long now;
  615.   char buf[30];
  616.   register char *tem;
  617.  
  618.   if (NILP (arg))
  619.     now = time ((long *) 0);
  620.   else
  621.     {
  622.       Lisp_Object top, bottom;
  623.       CHECK_CONS (arg, 1);
  624.       top = Fcar (arg);
  625.       bottom = Fcdr (arg);
  626.       if (CONSP (bottom))
  627.     bottom = Fcar (bottom);
  628.       CHECK_FIXNUM (top, 1);
  629.       CHECK_FIXNUM (bottom, 1);
  630.       now = (XINT (top) << 16) | XINT (bottom);
  631.     }
  632.   tem = (char *) ctime (&now);
  633.  
  634.   strncpy (buf, tem, 24);
  635.   buf[24] = 0;
  636.  
  637.   return build_string (buf);
  638. }
  639.  
  640. DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 0, 0,
  641.   "Return the offset, savings state, and names for the current time zone.\n\
  642. This returns a list of the form (OFFSET SAVINGS-FLAG STANDARD SAVINGS).\n\
  643. OFFSET is an integer specifying how many minutes east of Greenwich the\n\
  644.     current time zone is located.  A negative value means west of\n\
  645.     Greenwich. Note that this describes the standard time;  if daylight\n\
  646.     savings time is in effect, it does not affect this value.\n\
  647. SAVINGS-FLAG is non-nil iff daylight savings time or some other sort\n\
  648.     of seasonal time adjustment is in effect.\n\
  649. STANDARD is a string giving the name of the time zone when no seasonal\n\
  650.     time adjustment is in effect.\n\
  651. SAVINGS is a string giving the name of the time zone when there is a\n\
  652.     seasonal time adjustment in effect.\n\
  653. If the local area does not use a seasonal time adjustment,\n\
  654. SAVINGS-FLAG will always be nil, and STANDARD and SAVINGS will be the\n\
  655. same.")
  656.   ()
  657. {
  658. #ifdef EMACS_CURRENT_TIME_ZONE
  659.   int offset, savings_flag;
  660.   char standard[11];
  661.   char savings[11];
  662.  
  663.   EMACS_CURRENT_TIME_ZONE (&offset, &savings_flag, standard, savings);
  664.  
  665.   return (list4 (make_number (offset),
  666.                  ((savings_flag) ? Qt : Qnil),
  667.                  build_string (standard),
  668.                  build_string (savings)));
  669. #else
  670.   error
  671.     ("current-time-zone has not been implemented on this operating system.");
  672.   return Qnil;
  673. #endif
  674. }
  675.  
  676. #ifndef VMS
  677.  
  678. DEFUN ("set-default-file-mode", Fset_default_file_mode, Sset_default_file_mode, 1, 1, "p",
  679.   "Set Unix `umask' value to ARGUMENT, and return old value.\n\
  680. The `umask' value is the default protection mode for new files.")
  681.   (nmask)
  682.      Lisp_Object nmask;
  683. {
  684.   CHECK_FIXNUM (nmask, 0);
  685.   return make_number (umask (XINT (nmask)));
  686. }
  687.  
  688. DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "",
  689.   "Tell Unix to finish all pending disk updates.")
  690.   ()
  691. {
  692.   sync ();
  693.   return Qnil;
  694. }
  695.  
  696. #endif /* !VMS */
  697.  
  698. void
  699. insert1 (arg)
  700.      Lisp_Object arg;
  701. {
  702.   struct gcpro gcpro1;
  703.   GCPRO1 (arg);
  704.   Finsert (1, &arg);
  705.   UNGCPRO;
  706. }
  707.  
  708.  
  709. /* Callers passing one argument to Finsert need not gcpro the
  710.    argument "array", since the only element of the array will
  711.    not be used after calling insert or insert_from_string, so
  712.    we don't care if it gets trashed.  */
  713.  
  714. DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
  715.   "Insert the arguments, either strings or characters, at point.\n\
  716. Point moves forward so that it ends up after the inserted text.\n\
  717. Any other markers at the point of insertion remain before the text.")
  718.   (nargs, args)
  719.      int nargs;
  720.      register Lisp_Object *args;
  721. {
  722.   register int argnum;
  723.   register Lisp_Object tem;
  724.  
  725.   for (argnum = 0; argnum < nargs; argnum++)
  726.     {
  727.       tem = args[argnum];
  728.     retry:
  729.       if (FIXNUMP (tem))
  730.     {
  731.       insert_char (XINT (tem));
  732.     }
  733.       else if (STRINGP (tem))
  734.     {
  735.       insert_from_string (tem, 0, XSTRING (tem)->size);
  736.     }
  737.       else
  738.     {
  739.       tem = wrong_type_argument (Qchar_or_string_p, tem);
  740.       goto retry;
  741.     }
  742.     }
  743.   zmacs_region_stays = 0;
  744.   return Qnil;
  745. }
  746.  
  747. DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
  748.   "Insert strings or characters at point, relocating markers after the text.\n\
  749. Point moves forward so that it ends up after the inserted text.\n\
  750. Any other markers at the point of insertion also end up after the text.")
  751.   (nargs, args)
  752.      int nargs;
  753.      register Lisp_Object *args;
  754. {
  755.   register int argnum;
  756.   register Lisp_Object tem;
  757.  
  758.   for (argnum = 0; argnum < nargs; argnum++)
  759.     {
  760.       tem = args[argnum];
  761.     retry:
  762.       if (FIXNUMP (tem))
  763.     {
  764.       char str[1];
  765.       str[0] = XINT (tem);
  766.       insert_before_markers (str, 1);
  767.     }
  768.       else if (STRINGP (tem))
  769.     {
  770.       insert_from_string_before_markers (tem, 0, XSTRING (tem)->size);
  771.     }
  772.       else
  773.     {
  774.       tem = wrong_type_argument (Qchar_or_string_p, tem);
  775.       goto retry;
  776.     }
  777.     }
  778.   zmacs_region_stays = 0;
  779.   return Qnil;
  780. }
  781.  
  782. DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 2, 0,
  783.   "Insert COUNT (second arg) copies of CHAR (first arg).\n\
  784. Point and all markers are affected as in the function `insert'.\n\
  785. Both arguments are required.")
  786.   (chr, count)
  787.        Lisp_Object chr, count;
  788. {
  789.   register char *string;
  790.   register int strlen;
  791.   register int i, n;
  792.  
  793.   CHECK_FIXNUM (chr, 0);
  794.   CHECK_FIXNUM (count, 1);
  795.  
  796.   n = XINT (count);
  797.   if (n <= 0)
  798.     return Qnil;
  799.   strlen = min (n, 256);
  800.   string = (char *) alloca (strlen);
  801.   for (i = 0; i < strlen; i++)
  802.     string[i] = XFASTINT (chr);
  803.   while (n >= strlen)
  804.     {
  805.       insert_raw_string (string, strlen);
  806.       n -= strlen;
  807.     }
  808.   if (n > 0)
  809.     insert_raw_string (string, n);
  810.  
  811.   zmacs_region_stays = 0;
  812.   return Qnil;
  813. }
  814.  
  815.  
  816. extern Lisp_Object make_string_from_buffer ();
  817.  
  818. /* Return a string with the contents of the current region */
  819.  
  820. DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
  821.   "Return the contents of part of the current buffer as a string.\n\
  822. The two arguments START and END are character positions;\n\
  823. they can be in either order.")
  824.   (b, e)
  825.      Lisp_Object b, e;
  826. {
  827.   register int beg, end;
  828.  
  829.   validate_region (&b, &e);
  830.   beg = XINT (b);
  831.   end = XINT (e);
  832.  
  833.   return make_string_from_buffer (current_buffer, beg, end - beg);
  834. }
  835.  
  836. DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
  837.   "Return the contents of the current buffer as a string.")
  838.   ()
  839. {
  840.   return make_string_from_buffer (current_buffer, BEGV, ZV - BEGV);
  841. }
  842.  
  843. DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
  844.   1, 3, 0,
  845.   "Insert before point a substring of the contents buffer BUFFER.\n\
  846. BUFFER may be a buffer or a buffer name.\n\
  847. Arguments START and END are character numbers specifying the substring.\n\
  848. They default to the beginning and the end of BUFFER.")
  849.   (buf, b, e)
  850.      Lisp_Object buf, b, e;
  851. {
  852.   register int beg, end;
  853.   register struct buffer *bp;
  854.  
  855.   buf = Fget_buffer (buf);
  856.   bp = XBUFFER (buf);
  857.  
  858.   if (NILP (b))
  859.     beg = BUF_BEGV (bp);
  860.   else
  861.     {
  862.       CHECK_FIXNUM_COERCE_MARKER (b, 0);
  863.       beg = XINT (b);
  864.     }
  865.   if (NILP (e))
  866.     end = BUF_ZV (bp);
  867.   else
  868.     {
  869.       CHECK_FIXNUM_COERCE_MARKER (e, 1);
  870.       end = XINT (e);
  871.     }
  872.  
  873.   if (beg > end)
  874.     {
  875.       int exch = beg;
  876.       beg = end;
  877.       end = exch;
  878.     }
  879.  
  880.   /* Move the gap or create enough gap in the current buffer.  */
  881.  
  882.   if (point != GPT)
  883.     move_gap (point);
  884.   if (GAP_SIZE < end - beg)
  885.     make_gap (end - beg - GAP_SIZE);
  886.  
  887.   if (!(BUF_BEGV (bp) <= beg
  888.     && beg <= end
  889.         && end <= BUF_ZV (bp)))
  890.     args_out_of_range (b, e);
  891.  
  892.   /* Now the actual insertion will not do any gap motion,
  893.      so it matters not if BUF is the current buffer.  */
  894.   if (beg < BUF_GPT (bp))
  895.     {
  896.       insert_buffer_string (bp, beg, min (end, BUF_GPT (bp)) - beg);
  897.       beg = min (end, BUF_GPT (bp));
  898.     }
  899.   if (beg < end)
  900.     insert_buffer_string (bp, beg, end - beg);
  901.  
  902.   return Qnil;
  903. }
  904.  
  905. DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
  906.   6, 6, 0,
  907.   "Compare two substrings of two buffers; return result as number.\n\
  908. the value is -N if first string is less after N-1 chars,\n\
  909. +N if first string is greater after N-1 chars, or 0 if strings match.\n\
  910. Each substring is represented as three arguments: BUFFER, START and END.\n\
  911. That makes six args in all, three for each substring.\n\n\
  912. The value of `case-fold-search' in the current buffer\n\
  913. determines whether case is significant or ignored.")
  914.   (buffer1, start1, end1, buffer2, start2, end2)
  915.      Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
  916. {
  917.   register int begp1, endp1, begp2, endp2, len1, len2, length, i;
  918.   register struct buffer *bp1, *bp2;
  919.   register unsigned char *trt 
  920.     = ((!NILP (current_buffer->case_fold_search))
  921.        ? XSTRING (current_buffer->case_canon_table)->data 
  922.        : 0);
  923.  
  924.   /* Find the first buffer and its substring.  */
  925.  
  926.   if (NILP (buffer1))
  927.     bp1 = current_buffer;
  928.   else
  929.     {
  930.       Lisp_Object buf1;
  931.       buf1 = Fget_buffer (buffer1);
  932.       bp1 = XBUFFER (buf1);
  933.     }
  934.  
  935.   if (NILP (start1))
  936.     begp1 = BUF_BEGV (bp1);
  937.   else
  938.     {
  939.       CHECK_NUMBER_COERCE_MARKER (start1, 1);
  940.       begp1 = XINT (start1);
  941.     }
  942.   if (NILP (end1))
  943.     endp1 = BUF_ZV (bp1);
  944.   else
  945.     {
  946.       CHECK_NUMBER_COERCE_MARKER (end1, 2);
  947.       endp1 = XINT (end1);
  948.     }
  949.  
  950.   if (begp1 > endp1)
  951.   {
  952.     int temp = begp1; begp1 = endp1; endp1 = temp;
  953.   }
  954.  
  955.   if (!(BUF_BEGV (bp1) <= begp1
  956.     && begp1 <= endp1
  957.         && endp1 <= BUF_ZV (bp1)))
  958.     args_out_of_range (start1, end1);
  959.  
  960.   /* Likewise for second substring.  */
  961.  
  962.   if (NILP (buffer2))
  963.     bp2 = current_buffer;
  964.   else
  965.     {
  966.       Lisp_Object buf2;
  967.       buf2 = Fget_buffer (buffer2);
  968.       bp2 = XBUFFER (buffer2);
  969.     }
  970.  
  971.   if (NILP (start2))
  972.     begp2 = BUF_BEGV (bp2);
  973.   else
  974.     {
  975.       CHECK_NUMBER_COERCE_MARKER (start2, 4);
  976.       begp2 = XINT (start2);
  977.     }
  978.   if (NILP (end2))
  979.     endp2 = BUF_ZV (bp2);
  980.   else
  981.     {
  982.       CHECK_NUMBER_COERCE_MARKER (end2, 5);
  983.       endp2 = XINT (end2);
  984.     }
  985.  
  986.   if (begp2 > endp2)
  987.   {
  988.     int temp = begp2; begp2 = endp2; endp2 = temp;
  989.   }
  990.  
  991.   if (!(BUF_BEGV (bp2) <= begp2
  992.     && begp2 <= endp2
  993.         && endp2 <= BUF_ZV (bp2)))
  994.     args_out_of_range (start2, end2);
  995.  
  996.   len1 = endp1 - begp1;
  997.   len2 = endp2 - begp2;
  998.   length = len1;
  999.   if (len2 < length)
  1000.     length = len2;
  1001.  
  1002.   for (i = 0; i < length; i++)
  1003.     {
  1004.       int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
  1005.       int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
  1006.       if (trt)
  1007.     {
  1008.       c1 = trt[c1];
  1009.       c2 = trt[c2];
  1010.     }
  1011.       if (c1 < c2)
  1012.     return make_number (- 1 - i);
  1013.       if (c1 > c2)
  1014.     return make_number (i + 1);
  1015.     }
  1016.  
  1017.   /* The strings match as far as they go.
  1018.      If one is shorter, that one is less.  */
  1019.   if (length < len1)
  1020.     return make_number (length + 1);
  1021.   else if (length < len2)
  1022.     return make_number (- length - 1);
  1023.  
  1024.   /* Same length too => they are equal.  */
  1025.   return 0;
  1026. }
  1027.  
  1028. DEFUN ("subst-char-in-region", Fsubst_char_in_region,
  1029.   Ssubst_char_in_region, 4, 5, 0,
  1030.   "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
  1031. If optional arg NOUNDO is non-nil, don't record this change for undo\n\
  1032. and don't mark the buffer as really changed.")
  1033.   (start, end, fromchar, tochar, noundo)
  1034.      Lisp_Object start, end, fromchar, tochar, noundo;
  1035. {
  1036.   register int pos, stop, look;
  1037.  
  1038.   validate_region (&start, &end);
  1039.   CHECK_FIXNUM (fromchar, 2);
  1040.   CHECK_FIXNUM (tochar, 3);
  1041.  
  1042.   pos = XINT (start);
  1043.   stop = XINT (end);
  1044.   look = XINT (fromchar);
  1045.  
  1046.   modify_region (pos, stop);
  1047.   if (! NILP (noundo))
  1048.     {
  1049.       if (MODIFF - 1 == current_buffer->save_modified)
  1050.     current_buffer->save_modified++;
  1051.       if (MODIFF - 1 == current_buffer->auto_save_modified)
  1052.     current_buffer->auto_save_modified++;
  1053.     }
  1054.  
  1055.   while (pos < stop)
  1056.     {
  1057.       if (CHAR_AT (pos) == look)
  1058.     {
  1059.       if (NILP (noundo))
  1060.         record_change (pos, 1);
  1061.       *(CHAR_ADDRESS (pos)) = XINT (tochar);
  1062.       if (NILP (noundo))
  1063.         signal_after_change (pos, 1, 1);
  1064.     }
  1065.       pos++;
  1066.     }
  1067.  
  1068.   return Qnil;
  1069. }
  1070.  
  1071. DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
  1072.   "From START to END, translate characters according to TABLE.\n\
  1073. TABLE is a string; the Nth character in it is the mapping\n\
  1074. for the character with code N.  Returns the number of characters changed.")
  1075.   (start, end, table)
  1076.      Lisp_Object start;
  1077.      Lisp_Object end;
  1078.      register Lisp_Object table;
  1079. {
  1080.   register int pos, stop;    /* Limits of the region. */
  1081.   register unsigned char *tt;    /* Trans table. */
  1082.   register int oc;        /* Old character. */
  1083.   register int nc;        /* New character. */
  1084.   int cnt;            /* Number of changes made. */
  1085.   Lisp_Object z;        /* Return. */
  1086.   int size;            /* Size of translate table. */
  1087.  
  1088.   validate_region (&start, &end);
  1089.   CHECK_STRING (table, 2);
  1090.  
  1091.   size = XSTRING (table)->size;
  1092.   tt = XSTRING (table)->data;
  1093.  
  1094.   pos = XINT (start);
  1095.   stop = XINT (end);
  1096.   modify_region (pos, stop);
  1097.  
  1098.   cnt = 0;
  1099.   for (; pos < stop; ++pos)
  1100.     {
  1101.       oc = CHAR_AT (pos);
  1102.       if (oc < size)
  1103.     {
  1104.       nc = tt[oc];
  1105.       if (nc != oc)
  1106.         {
  1107.           record_change (pos, 1);
  1108.           *(CHAR_ADDRESS (pos)) = nc;
  1109.           signal_after_change (pos, 1, 1);
  1110.           ++cnt;
  1111.         }
  1112.     }
  1113.     }
  1114.  
  1115.   XFASTINT (z) = cnt;
  1116.   return (z);
  1117. }
  1118.  
  1119. DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
  1120.   "Delete the text between point and mark.\n\
  1121. When called from a program, expects two arguments,\n\
  1122. positions (integers or markers) specifying the stretch to be deleted.")
  1123.   (b, e)
  1124.      Lisp_Object b, e;
  1125. {
  1126.   validate_region (&b, &e);
  1127.   del_range (XINT (b), XINT (e));
  1128.   zmacs_region_stays = 0;
  1129.   return Qnil;
  1130. }
  1131.  
  1132. DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
  1133.   "Remove restrictions (narrowing) from current buffer.\n\
  1134. This allows the buffer's full text to be seen and edited.")
  1135.   ()
  1136. {
  1137.   BEGV = BEG;
  1138.   SET_BUF_ZV (current_buffer, Z);
  1139.   clip_changed = 1;
  1140.   zmacs_region_stays = 0;
  1141.   return Qnil;
  1142. }
  1143.  
  1144. DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
  1145.   "Restrict editing in this buffer to the current region.\n\
  1146. The rest of the text becomes temporarily invisible and untouchable\n\
  1147. but is not deleted; if you save the buffer in a file, the invisible\n\
  1148. text is included in the file.  \\[widen] makes all visible again.\n\
  1149. See also `save-restriction'.\n\
  1150. \n\
  1151. When calling from a program, pass two arguments; positions (integers\n\
  1152. or markers) bounding the text that should remain visible.")
  1153.   (b, e)
  1154.      register Lisp_Object b, e;
  1155. {
  1156.   register int i;
  1157.  
  1158.   CHECK_FIXNUM_COERCE_MARKER (b, 0);
  1159.   CHECK_FIXNUM_COERCE_MARKER (e, 1);
  1160.  
  1161.   if (XINT (b) > XINT (e))
  1162.     {
  1163.       i = XFASTINT (b);
  1164.       b = e;
  1165.       XFASTINT (e) = i;
  1166.     }
  1167.  
  1168.   if (!(BEG <= XINT (b) && XINT (b) <= XINT (e) && XINT (e) <= Z))
  1169.     args_out_of_range (b, e);
  1170.  
  1171.   BEGV = XFASTINT (b);
  1172.   SET_BUF_ZV (current_buffer, XFASTINT (e));
  1173.   if (point < XFASTINT (b))
  1174.     SET_PT (XFASTINT (b));
  1175.   if (point > XFASTINT (e))
  1176.     SET_PT (XFASTINT (e));
  1177.   clip_changed = 1;
  1178.   zmacs_region_stays = 0;
  1179.   return Qnil;
  1180. }
  1181.  
  1182. Lisp_Object
  1183. save_restriction_save ()
  1184. {
  1185.   register Lisp_Object bottom, top;
  1186.   /* Note: I tried using markers here, but it does not win
  1187.      because insertion at the end of the saved region
  1188.      does not advance mh and is considered "outside" the saved region. */
  1189.   XFASTINT (bottom) = BEGV - BEG;
  1190.   XFASTINT (top) = Z - ZV;
  1191.  
  1192.   return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
  1193. }
  1194.  
  1195. Lisp_Object
  1196. save_restriction_restore (data)
  1197.      Lisp_Object data;
  1198. {
  1199.   register struct buffer *buf;
  1200.   register int newhead, newtail;
  1201.   register Lisp_Object tem;
  1202.  
  1203.   buf = XBUFFER (XCONS (data)->car);
  1204.  
  1205.   data = XCONS (data)->cdr;
  1206.  
  1207.   tem = XCONS (data)->car;
  1208.   newhead = XINT (tem);
  1209.   tem = XCONS (data)->cdr;
  1210.   newtail = XINT (tem);
  1211.   if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
  1212.     {
  1213.       newhead = 0;
  1214.       newtail = 0;
  1215.     }
  1216.   BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
  1217.   SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
  1218.   clip_changed = 1;
  1219.  
  1220.   /* If point is outside the new visible range, move it inside. */
  1221.   SET_BUF_PT (buf,
  1222.               clip_to_bounds (BUF_BEGV (buf),
  1223.                               BUF_PT (buf),
  1224.                               BUF_ZV (buf)));
  1225.  
  1226.   return Qnil;
  1227. }
  1228.  
  1229. DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
  1230.   "Execute BODY, saving and restoring current buffer's restrictions.\n\
  1231. The buffer's restrictions make parts of the beginning and end invisible.\n\
  1232. \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
  1233. This special form, `save-restriction', saves the current buffer's restrictions\n\
  1234. when it is entered, and restores them when it is exited.\n\
  1235. So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
  1236. The old restrictions settings are restored\n\
  1237. even in case of abnormal exit (throw or error).\n\
  1238. \n\
  1239. The value returned is the value of the last form in BODY.\n\
  1240. \n\
  1241. `save-restriction' can get confused if, within the BODY, you widen\n\
  1242. and then make changes outside the area within the saved restrictions.\n\
  1243. \n\
  1244. Note: if you are using both `save-excursion' and `save-restriction',\n\
  1245. use `save-excursion' outermost:\n\
  1246.     (save-excursion (save-restriction ...))")
  1247.   (body)
  1248.      Lisp_Object body;
  1249. {
  1250.   int speccount = specpdl_depth;
  1251.  
  1252.   record_unwind_protect (save_restriction_restore, save_restriction_save ());
  1253.  
  1254.   return unbind_to (speccount, Fprogn (body));
  1255. }
  1256.  
  1257. DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
  1258.   "Print a one-line message at the bottom of the screen.\n\
  1259. The first argument is a control string.\n\
  1260. It may contain %s or %d or %c to print successive following arguments.\n\
  1261. %s means print an argument as a string, %d means print as number in decimal,\n\
  1262. %c means print a number as a single character.\n\
  1263. The argument used by %s must be a string or a symbol;\n\
  1264. the argument used by %d or %c must be a number.\n\
  1265. \n\
  1266. If the only argument is nil, just erase any previous message.")
  1267.   (nargs, args)
  1268.      int nargs;
  1269.      Lisp_Object *args;
  1270. {
  1271.   /* Caller is assumed to gcpro args */
  1272.   register Lisp_Object val;
  1273.  
  1274. #ifdef MULTI_SCREEN
  1275.   extern Lisp_Object Vglobal_minibuffer_screen;
  1276.  
  1277.   if (SCREENP (Vglobal_minibuffer_screen))
  1278.     Fmake_screen_visible (Vglobal_minibuffer_screen);
  1279. #endif
  1280.  
  1281.   if (nargs == 1 && NILP (args[0]))
  1282.   {
  1283.     message (0);
  1284.     return (Qnil);
  1285.   }
  1286.  
  1287.   val = Fformat (nargs, args);
  1288.   message ("%s", XSTRING (val)->data); /* Arggh!  Loses \000's.  Unix sucks */
  1289.   return val;
  1290. }
  1291.  
  1292. DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
  1293.   "Format a string out of a control-string and arguments.\n\
  1294. The first argument is a control string.\n\
  1295. The other arguments are substituted into it to make the result, a string.\n\
  1296. It may contain %-sequences meaning to substitute the next argument.\n\
  1297. %s means print strings using `princ' and other objects using `prin1'.\n\
  1298. %S means print all objects using `prin1' (including strings.)\n\
  1299. %d means print as number in decimal (%o octal, %x hex).\n\
  1300. %c means print a number as a single character.\n\
  1301. The argument used for %d, %o, %x or %c must be a number.\n\
  1302. Use %% to put a single % into the output.")
  1303.   (nargs, args)                 /* Note!! args side-effected! */
  1304.      int nargs;
  1305.      register Lisp_Object *args;
  1306. {
  1307.   /* Caller is assumed to gcpro ARGS */
  1308.   register int n;
  1309.   register int total = 5;
  1310.   char *buf;
  1311.   register char *format, *end;
  1312.   register char **strings;
  1313.  
  1314.   CHECK_STRING (args[0], 0);
  1315.  
  1316.   format = (char *) XSTRING(args[0])->data;
  1317.   end = format + XSTRING(args[0])->size;
  1318.  
  1319.   /* We have to do so much work in oder to prepare to call doprnt
  1320.      that we might as well do all of it ourself...  (Which would also
  1321.      circumvent C asciz cretinism by allowing ascii 000 chars to appear)
  1322.    */
  1323.   n = 0;
  1324.   while (format < end)
  1325.     if (*format++ == '%')
  1326.       {
  1327.     int minlen;
  1328.  
  1329.     /* Process a numeric arg and skip it.  */
  1330.     minlen = atoi ((char *) format);
  1331.     if (minlen > 0)
  1332.       total += minlen;
  1333.     else
  1334.       total -= minlen;
  1335.     while ((*format >= '0' && *format <= '9')
  1336.            || *format == '-' || *format == ' ')
  1337.       format++;
  1338.  
  1339.     if (*format == '%')
  1340.       format++;
  1341.     else if (++n >= nargs)
  1342.       ;
  1343.     else if (*format == 's' || *format == 'S')
  1344.       {
  1345.         register Lisp_Object tem;
  1346.         /* For `s', prin1 the argument only if it's not a string.  */
  1347.         if (*format == 's' && STRINGP (args[n]))
  1348.           goto string;
  1349.         tem = Fprin1_to_string (args[n], Qnil);
  1350.         args[n] = tem;
  1351.         goto string;
  1352.       }
  1353.     else if (SYMBOLP (args[n]))
  1354.       {
  1355.         XSET (args[n], Lisp_String, XSYMBOL (args[n])->name);
  1356.         goto string;
  1357.       }
  1358.     else if (STRINGP (args[n]))
  1359.       {
  1360.       string:
  1361.         total += XSTRING (args[n])->size;
  1362.       }
  1363.     /* Would get MPV otherwise, since Lisp_Int's `point' to low memory.  */
  1364.     else if (FIXNUMP (args[n]) && *format != 'S')
  1365.       total += 10;
  1366.     else
  1367.       {
  1368.         /* Anything but a string, convert to a string using princ.  */
  1369.         register Lisp_Object tem;
  1370.         tem = Fprin1_to_string (args[n], Qt);
  1371.         args[n] = tem;
  1372.         goto string;
  1373.       }
  1374.       }
  1375.  
  1376.     strings = (char **) alloca ((n + 1) * sizeof (char *));
  1377.     for (; n >= 0; n--)
  1378.       {
  1379.     if (n >= nargs)
  1380.       strings[n] = "";
  1381.     else if (FIXNUMP (args[n]))
  1382.       /* We checked above that the corresponding format effector
  1383.          isn't %s, which would cause MPV.  */
  1384.       strings[n] = (char *) XINT (args[n]);
  1385.     else
  1386.       strings[n] = (char *) XSTRING (args[n])->data;
  1387.       }
  1388.  
  1389.     /* Format it in bigger and bigger buf's until it all fits. */
  1390.     while (1)
  1391.       {
  1392.     buf = (char *) alloca (total + 1);
  1393.     buf[total - 1] = 0;
  1394.  
  1395.     doprnt (buf, total + 1, strings[0], end, nargs, strings + 1);
  1396.     if (buf[total - 1] == 0)
  1397.       break;
  1398.  
  1399.     total *= 2;
  1400.       }
  1401.  
  1402.     return build_string (buf);
  1403.   }
  1404.  
  1405. /* VARARGS 1 */
  1406. Lisp_Object
  1407. #ifdef NO_ARG_ARRAY
  1408. format1 (string1, arg0, arg1, arg2, arg3, arg4)
  1409.      int arg0, arg1, arg2, arg3, arg4;
  1410. #else
  1411. format1 (string1)
  1412. #endif
  1413.      char *string1;
  1414. {
  1415.   char buf[100];
  1416. #ifdef NO_ARG_ARRAY
  1417.   Lisp_Object args[5];
  1418.   args[0] = arg0;
  1419.   args[1] = arg1;
  1420.   args[2] = arg2;
  1421.   args[3] = arg3;
  1422.   args[4] = arg4;
  1423.   doprnt (buf, sizeof buf, string1, 0, 5, (char **)args);
  1424. #else
  1425.   doprnt (buf, sizeof buf, string1, 0, 5, &string1 + 1);
  1426. #endif
  1427.   return build_string (buf);
  1428. }
  1429.  
  1430. DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
  1431.   "Return t if two characters match, optionally ignoring case.\n\
  1432. Both arguments must be characters (i.e. integers).\n\
  1433. Case is ignored if `case-fold-search' is non-nil in the current buffer.")
  1434.   (c1, c2)
  1435.      register Lisp_Object c1, c2;
  1436. {
  1437.   unsigned char *downcase = DOWNCASE_TABLE;
  1438.   CHECK_FIXNUM (c1, 0);
  1439.   CHECK_FIXNUM (c2, 1);
  1440.  
  1441.   if (!NILP (current_buffer->case_fold_search)
  1442.       ? downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
  1443.       : XINT (c1) == XINT (c2))
  1444.     return Qt;
  1445.   return Qnil;
  1446. }
  1447.  
  1448. #ifndef MAINTAIN_ENVIRONMENT /* it is done in environ.c in that case */
  1449. DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
  1450.   "Return the value of environment variable VAR, as a string.\n\
  1451. VAR should be a string.  Value is nil if VAR is undefined in the environment.")
  1452.   (str)
  1453.      Lisp_Object str;
  1454. {
  1455.   register char *val;
  1456.   CHECK_STRING (str, 0);
  1457.   val = (char *) egetenv (XSTRING (str)->data);
  1458.   if (!val)
  1459.     return Qnil;
  1460.   return build_string (val);
  1461. }
  1462. #endif /* MAINTAIN_ENVIRONMENT */
  1463.  
  1464. void
  1465. syms_of_editfns ()
  1466. {
  1467.   DEFVAR_LISP ("system-name", &Vsystem_name,
  1468.            "The name of the machine Emacs is running on.");
  1469.   
  1470.   DEFVAR_LISP ("user-full-name", &Vuser_full_name,
  1471.            "The full name of the user logged in.");
  1472.  
  1473.   DEFVAR_LISP ("user-name", &Vuser_name,
  1474.            "The user's name, based on the effective uid.");
  1475.  
  1476.   DEFVAR_LISP ("user-real-name", &Vuser_real_name,
  1477.            "The user's name, base upon the real uid.");
  1478.  
  1479.   DEFVAR_BOOL ("zmacs-regions", &zmacs_regions,
  1480.            "*Whether LISPM-style active regions should be used.\n\
  1481. This means that commands which operate on the region (the area between the\n\
  1482. point and the mark) will only work while the region is in the ``active''\n\
  1483. state, which is indicated by hilighting.  Executing most commands causes\n\
  1484. the region to not be in the active state, so (for example) \\[kill-region] will only\n\
  1485. work immediately after activating the region.\n\
  1486. \n\
  1487. More specifically:\n\
  1488. \n\
  1489.  - Commands which operate on the region only work if the region is active.\n\
  1490.  - Only a very small set of commands cause the region to become active:\n\
  1491.    Those commands whose semantics are to mark an area, like mark-defun.\n\
  1492.  - The region is deactivated after each command that is executed, except that:\n\
  1493.  - \"Motion\" commands do not change whether the region is active or not.\n\
  1494. \n\
  1495. set-mark-command (C-SPC) pushes a mark and activates the region.  Moving the\n\
  1496. cursor with normal motion commands (C-n, C-p, etc) will cause the region\n\
  1497. between point and the recently-pushed mark to be highlighted.  It will\n\
  1498. remain highlighted until some non-motion comand is executed.\n\
  1499. \n\
  1500. exchange-point-and-mark (\\[exchange-point-and-mark]) activates the region.  So if you mark a\n\
  1501. region and execute a command that operates on it, you can reactivate the\n\
  1502. same region with \\[exchange-point-and-mark] (or perhaps \\[exchange-point-and-mark] \\[exchange-point-and-mark]) to operate on it\n\
  1503. again.\n\
  1504. \n\
  1505. Generally, commands which push marks as a means of navigation (like\n\
  1506. beginning-of-buffer and end-of-buffer (M-< and M->)) do not activate the\n\
  1507. region.  But commands which push marks as a means of marking an area of\n\
  1508. text (like mark-defun (\\[mark-defun]), mark-word (\\[mark-word]) or mark-whole-buffer (\\[mark-whole-buffer]))\n\
  1509. do activate the region.");
  1510.   /* Zmacs style active regions are now ON by default */
  1511.   zmacs_regions = 1;
  1512.   zmacs_region_active_p = 0;
  1513.  
  1514.   DEFVAR_LISP ("zmacs-activate-region-hook", &Vzmacs_activate_region_hook,
  1515.        "Function or functions called when the region becomes active;\n\
  1516. see the variable `zmacs-regions'.");
  1517.   DEFVAR_LISP ("zmacs-deactivate-region-hook", &Vzmacs_deactivate_region_hook,
  1518.        "Function or functions called when the region becomes inactive;\n\
  1519. see the variable `zmacs-regions'.");
  1520.   DEFVAR_LISP ("zmacs-update-region-hook", &Vzmacs_update_region_hook,
  1521.        "Function or functions called when the active region changes.\n\
  1522. This is called after each command that sets `region-stays' to t.\n\
  1523. See the variable `zmacs-regions'.");
  1524.   DEFVAR_BOOL ("zmacs-region-stays", &zmacs_region_stays,
  1525.    "Commands which do not wish to affect whether the region is currently\n\
  1526. hilighted should set this to t.  Normally, the region is turned off after\n\
  1527. executing each command that did not explicitly turn it on with the function\n\
  1528. zmacs-activate-region. Setting this to true lets a command be non-intrusive.\n\
  1529. See the variable `zmacs-regions'.");
  1530.   Vzmacs_update_region_hook = Qnil;
  1531.   Vzmacs_activate_region_hook = Qnil;
  1532.   Vzmacs_deactivate_region_hook = Qnil;
  1533.   zmacs_region_stays = 0;
  1534.   defsymbol (&Qzmacs_update_region_hook, "zmacs-update-region-hook");
  1535.   defsymbol (&Qzmacs_activate_region_hook, "zmacs-activate-region-hook");
  1536.   defsymbol (&Qzmacs_deactivate_region_hook, "zmacs-deactivate-region-hook");
  1537.  
  1538.   defsymbol (&Qpoint, "point");
  1539.   defsymbol (&Qmark, "mark");
  1540.   defsymbol (&Qregion_beginning, "region-beginning");
  1541.   defsymbol (&Qregion_end, "region-end");
  1542.  
  1543.   defsubr (&Schar_equal);
  1544.   defsubr (&Sgoto_char);
  1545.   defsubr (&Sstring_to_char);
  1546.   defsubr (&Schar_to_string);
  1547.   defsubr (&Sbuffer_substring);
  1548.   defsubr (&Sbuffer_string);
  1549.  
  1550.   defsubr (&Spoint_marker);
  1551.   defsubr (&Smark_marker);
  1552.   defsubr (&Spoint);
  1553.   defsubr (&Sregion_beginning);
  1554.   defsubr (&Sregion_end);
  1555.   defsubr (&Ssave_excursion);
  1556.  
  1557.   defsubr (&Sbufsize);
  1558.   defsubr (&Spoint_max);
  1559.   defsubr (&Spoint_min);
  1560.   defsubr (&Spoint_min_marker);
  1561.   defsubr (&Spoint_max_marker);
  1562.  
  1563.   defsubr (&Sbobp);
  1564.   defsubr (&Seobp);
  1565.   defsubr (&Sbolp);
  1566.   defsubr (&Seolp);
  1567.   defsubr (&Sfollowing_char);
  1568.   defsubr (&Sprevious_char);
  1569.   defsubr (&Schar_after);
  1570.   defsubr (&Sinsert);
  1571.   defsubr (&Sinsert_before_markers);
  1572.   defsubr (&Sinsert_char);
  1573.  
  1574.   defsubr (&Suser_login_name);
  1575.   defsubr (&Suser_real_login_name);
  1576.   defsubr (&Suser_uid);
  1577.   defsubr (&Suser_real_uid);
  1578.   defsubr (&Suser_full_name);
  1579.   defsubr (&Scurrent_time_string);
  1580.   defsubr (&Scurrent_time_seconds);
  1581.   defsubr (&Ssystem_name);
  1582.   defsubr (&Scurrent_time_zone);
  1583.   defsubr (&Sset_default_file_mode);
  1584.   defsubr (&Sunix_sync);
  1585.   defsubr (&Smessage);
  1586.   defsubr (&Sformat);
  1587. #ifndef MAINTAIN_ENVIRONMENT /* in environ.c */
  1588.   defsubr (&Sgetenv);
  1589. #endif
  1590.  
  1591.   defsubr (&Sinsert_buffer_substring);
  1592.   defsubr (&Scompare_buffer_substrings);
  1593.   defsubr (&Ssubst_char_in_region);
  1594.   defsubr (&Stranslate_region);
  1595.   defsubr (&Sdelete_region);
  1596.   defsubr (&Swiden);
  1597.   defsubr (&Snarrow_to_region);
  1598.   defsubr (&Ssave_restriction);
  1599.   
  1600.   defsubr (&Szmacs_activate_region);
  1601.   defsubr (&Szmacs_deactivate_region);
  1602. }
  1603.