update.
[chise/xemacs-chise.git.1] / src / editfns.c
index ca3d41e..8ad18f5 100644 (file)
@@ -36,6 +36,7 @@ Boston, MA 02111-1307, USA.  */
 #include "frame.h"
 #include "insdel.h"
 #include "window.h"
+#include "casetab.h"
 #include "chartab.h"
 #include "line-number.h"
 
@@ -99,26 +100,26 @@ init_editfns (void)
 }
 \f
 DEFUN ("char-to-string", Fchar_to_string, 1, 1, 0, /*
-Convert arg CH to a one-character string containing that character.
+Convert CHARACTER to a one-character string containing that character.
 */
-       (ch))
+       (character))
 {
   Bytecount len;
   Bufbyte str[MAX_EMCHAR_LEN];
 
-  if (EVENTP (ch))
+  if (EVENTP (character))
     {
-      Lisp_Object ch2 = Fevent_to_character (ch, Qt, Qnil, Qnil);
+      Lisp_Object ch2 = Fevent_to_character (character, Qt, Qnil, Qnil);
       if (NILP (ch2))
        return
          signal_simple_continuable_error
-           ("character has no ASCII equivalent:", Fcopy_event (ch, Qnil));
-      ch = ch2;
+           ("character has no ASCII equivalent:", Fcopy_event (character, Qnil));
+      character = ch2;
     }
 
-  CHECK_CHAR_COERCE_INT (ch);
+  CHECK_CHAR_COERCE_INT (character);
 
-  len = set_charptr_emchar (str, XCHAR (ch));
+  len = set_charptr_emchar (str, XCHAR (character));
   return make_string (str, len);
 }
 
@@ -126,12 +127,12 @@ DEFUN ("string-to-char", Fstring_to_char, 1, 1, 0, /*
 Convert arg STRING to a character, the first character of that string.
 An empty string will return the constant `nil'.
 */
-       (str))
+       (string))
 {
   Lisp_String *p;
-  CHECK_STRING (str);
+  CHECK_STRING (string);
 
-  p = XSTRING (str);
+  p = XSTRING (string);
   if (string_length (p) != 0)
     return make_char (string_char (p, 0));
   else
@@ -600,12 +601,18 @@ If BUFFER is nil, the current buffer is assumed.
   return make_char (BUF_FETCH_CHAR (b, n));
 }
 
+#if !defined(WINDOWSNT) && !defined(MSDOS)
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+#endif
 \f
 DEFUN ("temp-directory", Ftemp_directory, 0, 0, 0, /*
 Return the pathname to the directory to use for temporary files.
 On MS Windows, this is obtained from the TEMP or TMP environment variables,
 defaulting to / if they are both undefined.
-On Unix it is obtained from TMPDIR, with /tmp as the default
+On Unix it is obtained from TMPDIR, with /tmp as the default.
 */
        ())
 {
@@ -619,7 +626,51 @@ On Unix it is obtained from TMPDIR, with /tmp as the default
 #else /* WIN32_NATIVE */
  tmpdir = getenv ("TMPDIR");
  if (!tmpdir)
+    {
+      struct stat st;
+      int myuid = getuid();
+      static char path[5 /* strlen ("/tmp/") */ + 1 + _POSIX_PATH_MAX];
+
+      strcpy (path, "/tmp/");
+      strncat (path, user_login_name (NULL), _POSIX_PATH_MAX);
+      if (lstat(path, &st) < 0 && errno == ENOENT)
+       {
+         mkdir(path, 0700);    /* ignore retval -- checked next anyway. */
+       }
+      if (lstat(path, &st) == 0 && st.st_uid == (uid_t) myuid &&
+         S_ISDIR(st.st_mode))
+       {
+         tmpdir = path;
+       }
+      else
+       {
+         path[5 + _POSIX_PATH_MAX] = '\0';
+         strncpy(path, getenv("HOME"), 5 + _POSIX_PATH_MAX);
+         strncat(path, "/tmp/", 5 + _POSIX_PATH_MAX);
+         if (stat(path, &st) < 0 && errno == ENOENT)
+           {
+             int fd;
+             char warnpath[6+_POSIX_PATH_MAX];
+             mkdir(path, 0700);        /* ignore retvals */
+             warnpath[_POSIX_PATH_MAX] = '\0';
+             strncpy(warnpath, path, 5 + _POSIX_PATH_MAX);
+             strncat(warnpath, ".created_by_xemacs", 5 + _POSIX_PATH_MAX);
+             if ((fd = open(warnpath, O_WRONLY|O_CREAT, 0644)) > 0)
+               {
+                 write(fd, "XEmacs created this directory because /tmp/<yourname> was unavailable -- \nPlease check !\n", 89);
+                 close(fd);
+               }
+           }
+         if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
+           {
+             tmpdir = path;
+           }
+         else
+           {
    tmpdir = "/tmp";
+           }
+       }
+    }
 #endif
 
   return build_ext_string (tmpdir, Qfile_name);
@@ -777,7 +828,7 @@ value of `user-full-name' is returned.
 #endif
   tem = ((!NILP (user) && !pw)
         ? Qnil
-        : make_ext_string ((Extbyte *) p, (q ? q - p : strlen (p)),
+        : make_ext_string ((Extbyte *) p, (q ? q - p : (int) strlen (p)),
                            Qnative));
 
 #ifdef AMPERSAND_FULL_NAME
@@ -1059,10 +1110,11 @@ characters appearing in the day and month names may be incorrect.
 {
   time_t value;
   size_t size;
+  struct tm * tm;
 
   CHECK_STRING (format_string);
 
-  if (! lisp_to_time (time_, &value))
+  if (! lisp_to_time (time_, &value) || ! (tm = localtime (&value)))
     error ("Invalid time specification");
 
   /* This is probably enough.  */
@@ -1074,7 +1126,7 @@ characters appearing in the day and month names may be incorrect.
       *buf = 1;
       if (emacs_strftime (buf, size,
                          (const char *) XSTRING_DATA (format_string),
-                         localtime (&value))
+                         tm)
          || !*buf)
        return build_ext_string (buf, Qbinary);
       /* If buffer was too small, make it bigger.  */
@@ -1103,10 +1155,10 @@ ZONE is an integer indicating the number of seconds east of Greenwich.
   struct tm *decoded_time;
   Lisp_Object list_args[9];
 
-  if (! lisp_to_time (specified_time, &time_spec))
+  if (! lisp_to_time (specified_time, &time_spec)
+      || ! (decoded_time = localtime (&time_spec)))
     error ("Invalid time specification");
 
-  decoded_time = localtime (&time_spec);
   list_args[0] = make_int (decoded_time->tm_sec);
   list_args[1] = make_int (decoded_time->tm_min);
   list_args[2] = make_int (decoded_time->tm_hour);
@@ -1128,6 +1180,15 @@ ZONE is an integer indicating the number of seconds east of Greenwich.
 
 static void set_time_zone_rule (char *tzstring);
 
+/* from GNU Emacs 21, per Simon Josefsson, modified by stephen
+   The slight inefficiency is justified since negative times are weird. */
+Lisp_Object
+make_time (time_t tval)
+{
+  return list2 (make_int (tval < 0 ? tval / 0x10000 : tval >> 16),
+               make_int (tval & 0xFFFF));
+}
+
 DEFUN ("encode-time", Fencode_time, 6, MANY, 0, /*
   Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
 This is the reverse operation of `decode-time', which see.
@@ -1201,7 +1262,7 @@ If you want them to stand for years in this century, you must do that yourself.
   if (the_time == (time_t) -1)
     error ("Specified time is not representable");
 
-  return wasteful_word_to_lisp (the_time);
+  return make_time (the_time);
 }
 
 DEFUN ("current-time-string", Fcurrent_time_string, 0, 1, 0, /*
@@ -1220,17 +1281,19 @@ and from `file-attributes'.
        (specified_time))
 {
   time_t value;
-  char buf[30];
-  char *tem;
+  char *the_ctime;
+  size_t len;
 
   if (! lisp_to_time (specified_time, &value))
     value = -1;
-  tem = (char *) ctime (&value);
+  the_ctime = ctime (&value);
 
-  strncpy (buf, tem, 24);
-  buf[24] = 0;
+  /* ctime is documented as always returning a "\n\0"-terminated
+     26-byte American time string, but let's be careful anyways. */
+  for (len = 0; the_ctime[len] != '\n' && the_ctime[len] != '\0'; len++)
+    ;
 
-  return build_ext_string (buf, Qbinary);
+  return make_ext_string ((Extbyte *) the_ctime, len, Qbinary);
 }
 
 #define TM_YEAR_ORIGIN 1900
@@ -1441,7 +1504,6 @@ buffer_insert1 (struct buffer *buf, Lisp_Object arg)
       arg = wrong_type_argument (Qchar_or_string_p, arg);
       goto retry;
     }
-  zmacs_region_stays = 0;
   UNGCPRO;
 }
 
@@ -1502,7 +1564,6 @@ Any other markers at the point of insertion also end up after the text.
          goto retry;
        }
     }
-  zmacs_region_stays = 0;
   return Qnil;
 }
 
@@ -1518,7 +1579,6 @@ BUFFER defaults to the current buffer.
   struct buffer *b = decode_buffer (buffer, 1);
   CHECK_STRING (string);
   buffer_insert_lisp_string (b, string);
-  zmacs_region_stays = 0;
   return Qnil;
 }
 
@@ -1531,7 +1591,7 @@ Jamie thinks this is bogus. */
 
 \f
 DEFUN ("insert-char", Finsert_char, 1, 4, 0, /*
-Insert COUNT (second arg) copies of CHR (first arg).
+Insert COUNT copies of CHARACTER into BUFFER.
 Point and all markers are affected as in the function `insert'.
 COUNT defaults to 1 if omitted.
 The optional third arg IGNORED is INHERIT under FSF Emacs.
@@ -1540,7 +1600,7 @@ This is highly bogus, however, and XEmacs always behaves as if
 The optional fourth arg BUFFER specifies the buffer to insert the
 text into.  If BUFFER is nil, the current buffer is assumed.
 */
-       (chr, count, ignored, buffer))
+       (character, count, ignored, buffer))
 {
   /* This function can GC */
   REGISTER Bufbyte *string;
@@ -1552,7 +1612,7 @@ text into.  If BUFFER is nil, the current buffer is assumed.
   struct buffer *b = decode_buffer (buffer, 1);
   int cou;
 
-  CHECK_CHAR_COERCE_INT (chr);
+  CHECK_CHAR_COERCE_INT (character);
   if (NILP (count))
     cou = 1;
   else
@@ -1561,7 +1621,7 @@ text into.  If BUFFER is nil, the current buffer is assumed.
       cou = XINT (count);
     }
 
-  charlen = set_charptr_emchar (str, XCHAR (chr));
+  charlen = set_charptr_emchar (str, XCHAR (character));
   n = cou * charlen;
   if (n <= 0)
     return Qnil;
@@ -1589,7 +1649,6 @@ text into.  If BUFFER is nil, the current buffer is assumed.
     buffer_insert_raw_string (b, string, n);
 #endif
 
-  zmacs_region_stays = 0;
   return Qnil;
 }
 
@@ -1620,7 +1679,7 @@ If BUFFER is nil, the current buffer is assumed.
    and what the function does is probably good enough for what the
    user-code will typically want to use it for. */
 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties, 0, 3, 0, /*
-Return the text from BEG to END, as a string, without copying the extents.
+Return the text from START to END as a string, without copying the extents.
 */
        (start, end, buffer))
 {
@@ -1669,7 +1728,7 @@ determines whether case is significant or ignored.
   REGISTER Charcount len1, len2, length, i;
   struct buffer *bp1, *bp2;
   Lisp_Object trt = ((!NILP (current_buffer->case_fold_search)) ?
-                    current_buffer->case_canon_table : Qnil);
+                    XCASE_TABLE_CANON (current_buffer->case_table) : Qnil);
 
   /* Find the first buffer and its substring.  */
 
@@ -1943,19 +2002,18 @@ Returns the number of substitutions performed.
 
 DEFUN ("delete-region", Fdelete_region, 2, 3, "r", /*
 Delete the text between point and mark.
-When called from a program, expects two arguments,
-positions (integers or markers) specifying the stretch to be deleted.
-If BUFFER is nil, the current buffer is assumed.
+When called from a program, expects two arguments START and END
+\(integers or markers) specifying the stretch to be deleted.
+If optional third arg BUFFER is nil, the current buffer is assumed.
 */
-       (b, e, buffer))
+       (start, end, buffer))
 {
   /* This function can GC */
-  Bufpos start, end;
+  Bufpos bp_start, bp_end;
   struct buffer *buf = decode_buffer (buffer, 1);
 
-  get_buffer_range_char (buf, b, e, &start, &end, 0);
-  buffer_delete_range (buf, start, end, 0);
-  zmacs_region_stays = 0;
+  get_buffer_range_char (buf, start, end, &bp_start, &bp_end, 0);
+  buffer_delete_range (buf, bp_start, bp_end, 0);
   return Qnil;
 }
 \f
@@ -1992,7 +2050,6 @@ If BUFFER is nil, the current buffer is assumed.
 {
   struct buffer *b = decode_buffer (buffer, 1);
   widen_buffer (b, 0);
-  zmacs_region_stays = 0;
   return Qnil;
 }
 
@@ -2007,27 +2064,27 @@ See also `save-restriction'.
 When calling from a program, pass two arguments; positions (integers
 or markers) bounding the text that should remain visible.
 */
-       (b, e, buffer))
+       (start, end, buffer))
 {
-  Bufpos start, end;
+  Bufpos bp_start, bp_end;
   struct buffer *buf = decode_buffer (buffer, 1);
   Bytind bi_start, bi_end;
 
-  get_buffer_range_char (buf, b, e, &start, &end, GB_ALLOW_PAST_ACCESSIBLE);
-  bi_start = bufpos_to_bytind (buf, start);
-  bi_end = bufpos_to_bytind (buf, end);
-
-  SET_BOTH_BUF_BEGV (buf, start, bi_start);
-  SET_BOTH_BUF_ZV (buf, end, bi_end);
-  if (BUF_PT (buf) < start)
-    BUF_SET_PT (buf, start);
-  if (BUF_PT (buf) > end)
-    BUF_SET_PT (buf, end);
+  get_buffer_range_char (buf, start, end, &bp_start, &bp_end,
+                        GB_ALLOW_PAST_ACCESSIBLE);
+  bi_start = bufpos_to_bytind (buf, bp_start);
+  bi_end = bufpos_to_bytind (buf, bp_end);
+
+  SET_BOTH_BUF_BEGV (buf, bp_start, bi_start);
+  SET_BOTH_BUF_ZV (buf, bp_end, bi_end);
+  if (BUF_PT (buf) < bp_start)
+    BUF_SET_PT (buf, bp_start);
+  if (BUF_PT (buf) > bp_end)
+    BUF_SET_PT (buf, bp_end);
   MARK_CLIP_CHANGED;
   /* Changing the buffer bounds invalidates any recorded current column.  */
   invalidate_current_column ();
   narrow_line_number_cache (buf);
-  zmacs_region_stays = 0;
   return Qnil;
 }
 
@@ -2222,15 +2279,15 @@ Both arguments must be characters (i.e. NOT integers).
 Case is ignored if `case-fold-search' is non-nil in BUFFER.
 If BUFFER is nil, the current buffer is assumed.
 */
-       (c1, c2, buffer))
+       (character1, character2, buffer))
 {
   Emchar x1, x2;
   struct buffer *b = decode_buffer (buffer, 1);
 
-  CHECK_CHAR_COERCE_INT (c1);
-  CHECK_CHAR_COERCE_INT (c2);
-  x1 = XCHAR (c1);
-  x2 = XCHAR (c2);
+  CHECK_CHAR_COERCE_INT (character1);
+  CHECK_CHAR_COERCE_INT (character2);
+  x1 = XCHAR (character1);
+  x2 = XCHAR (character2);
 
   return (!NILP (b->case_fold_search)
          ? DOWNCASE (b, x1) == DOWNCASE (b, x2)
@@ -2242,12 +2299,12 @@ DEFUN ("char=", Fchar_Equal, 2, 2, 0, /*
 Return t if two characters match, case is significant.
 Both arguments must be characters (i.e. NOT integers).
 */
-       (c1, c2))
+       (character1, character2))
 {
-  CHECK_CHAR_COERCE_INT (c1);
-  CHECK_CHAR_COERCE_INT (c2);
+  CHECK_CHAR_COERCE_INT (character1);
+  CHECK_CHAR_COERCE_INT (character2);
 
-  return EQ (c1, c2) ? Qt : Qnil;
+  return EQ (character1, character2) ? Qt : Qnil;
 }
 \f
 #if 0 /* Undebugged FSFmacs code */
@@ -2319,36 +2376,36 @@ Transpose region START1 to END1 with START2 to END2.
 The regions may not be overlapping, because the size of the buffer is
 never changed in a transposition.
 
-Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose
+Optional fifth arg LEAVE-MARKERS, if non-nil, means don't transpose
 any markers that happen to be located in the regions. (#### BUG: currently
-this function always acts as if LEAVE_MARKERS is non-nil.)
+this function always acts as if LEAVE-MARKERS is non-nil.)
 
 Transposing beyond buffer boundaries is an error.
 */
-  (startr1, endr1, startr2, endr2, leave_markers))
+  (start1, end1, start2, end2, leave_markers))
 {
-  Bufpos start1, end1, start2, end2;
+  Bufpos startr1, endr1, startr2, endr2;
   Charcount len1, len2;
   Lisp_Object string1, string2;
   struct buffer *buf = current_buffer;
 
-  get_buffer_range_char (buf, startr1, endr1, &start1, &end1, 0);
-  get_buffer_range_char (buf, startr2, endr2, &start2, &end2, 0);
+  get_buffer_range_char (buf, start1, end1, &startr1, &endr1, 0);
+  get_buffer_range_char (buf, start2, end2, &startr2, &endr2, 0);
 
-  len1 = end1 - start1;
-  len2 = end2 - start2;
+  len1 = endr1 - startr1;
+  len2 = endr2 - startr2;
 
-  if (start2 < end1)
+  if (startr2 < endr1)
     error ("transposed regions not properly ordered");
-  else if (start1 == end1 || start2 == end2)
+  else if (startr1 == endr1 || startr2 == endr2)
     error ("transposed region may not be of length 0");
 
-  string1 = make_string_from_buffer (buf, start1, len1);
-  string2 = make_string_from_buffer (buf, start2, len2);
-  buffer_delete_range (buf, start2, end2, 0);
-  buffer_insert_lisp_string_1 (buf, start2, string1, 0);
-  buffer_delete_range (buf, start1, end1, 0);
-  buffer_insert_lisp_string_1 (buf, start1, string2, 0);
+  string1 = make_string_from_buffer (buf, startr1, len1);
+  string2 = make_string_from_buffer (buf, startr2, len2);
+  buffer_delete_range (buf, startr2, endr2, 0);
+  buffer_insert_lisp_string_1 (buf, startr2, string1, 0);
+  buffer_delete_range (buf, startr1, endr1, 0);
+  buffer_insert_lisp_string_1 (buf, startr1, string2, 0);
 
   /* In FSFmacs there is a whole bunch of really ugly code here
      to attempt to transpose the regions without using up any
@@ -2462,7 +2519,7 @@ More specifically:
 
  - Commands which operate on the region only work if the region is active.
  - Only a very small set of commands cause the region to become active:
-   Those commands whose semantics are to mark an area, like mark-defun.
+   Those commands whose semantics are to mark an area, like `mark-defun'.
  - The region is deactivated after each command that is executed, except that:
  - "Motion" commands do not change whether the region is active or not.
 
@@ -2513,6 +2570,8 @@ zmacs-activate-region. Setting this to true lets a command be non-intrusive.
 See the variable `zmacs-regions'.
 
 The same effect can be achieved using the `_' interactive specification.
+
+`zmacs-region-stays' is reset to nil before each command is executed.
 */ );
   zmacs_region_stays = 0;