1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 1, or (at your option)
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.
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. */
21 #include <sys/types.h>
32 #include "intervals.h"
38 #define min(a, b) ((a) < (b) ? (a) : (b))
39 #define max(a, b) ((a) > (b) ? (a) : (b))
41 extern void insert_from_buffer ();
42 static long difftm ();
44 /* Some static data, and a function to initialize it for each run */
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 LOGNAME or USER */
55 register unsigned char *p
, *q
, *r
;
56 struct passwd
*pw
; /* password entry for the current user */
57 extern char *index ();
60 /* Set up system_name even when dumping. */
64 /* Don't bother with this on initial start when just dumping out */
67 #endif /* not CANNOT_DUMP */
69 pw
= (struct passwd
*) getpwuid (getuid ());
71 /* We let the real user name default to "root" because that's quite
72 accurate on MSDOG and because it lets Emacs find the init file.
73 (The DVX libraries override the Djgpp libraries here.) */
74 Vuser_real_name
= build_string (pw
? pw
->pw_name
: "root");
76 Vuser_real_name
= build_string (pw
? pw
->pw_name
: "unknown");
79 /* Get the effective user name, by consulting environment variables,
80 or the effective uid if those are unset. */
81 user_name
= (char *) getenv ("LOGNAME");
84 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
86 user_name
= (char *) getenv ("USER");
87 #endif /* WINDOWSNT */
90 pw
= (struct passwd
*) getpwuid (geteuid ());
91 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
93 Vuser_name
= build_string (user_name
);
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
);
99 pw
= (struct passwd
*) getpwnam (XSTRING (Vuser_name
)->data
);
101 p
= (unsigned char *) (pw
? USER_FULL_NAME
: "unknown");
102 q
= (unsigned char *) index (p
, ',');
103 Vuser_full_name
= make_string (p
, q
? q
- p
: strlen (p
));
105 #ifdef AMPERSAND_FULL_NAME
106 p
= XSTRING (Vuser_full_name
)->data
;
107 q
= (char *) index (p
, '&');
108 /* Substitute the login name for the &, upcasing the first character. */
111 r
= (char *) alloca (strlen (p
) + XSTRING (Vuser_name
)->size
+ 1);
114 strcat (r
, XSTRING (Vuser_name
)->data
);
115 r
[q
- p
] = UPCASE (r
[q
- p
]);
117 Vuser_full_name
= build_string (r
);
119 #endif /* AMPERSAND_FULL_NAME */
122 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
123 "Convert arg CHAR to a one-character string containing that character.")
131 return make_string (&c
, 1);
134 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
135 "Convert arg STRING to a character, the first character of that string.")
137 register Lisp_Object str
;
139 register Lisp_Object val
;
140 register struct Lisp_String
*p
;
141 CHECK_STRING (str
, 0);
145 XSETFASTINT (val
, ((unsigned char *) p
->data
)[0]);
147 XSETFASTINT (val
, 0);
155 register Lisp_Object mark
;
156 mark
= Fmake_marker ();
157 Fset_marker (mark
, make_number (val
), Qnil
);
161 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
162 "Return value of point, as an integer.\n\
163 Beginning of buffer is position (point-min)")
167 XSETFASTINT (temp
, point
);
171 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
172 "Return value of point, as a marker object.")
175 return buildmark (point
);
179 clip_to_bounds (lower
, num
, upper
)
180 int lower
, num
, upper
;
184 else if (num
> upper
)
190 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
191 "Set point to POSITION, a number or marker.\n\
192 Beginning of buffer is position (point-min), end is (point-max).")
194 register Lisp_Object n
;
196 CHECK_NUMBER_COERCE_MARKER (n
, 0);
198 SET_PT (clip_to_bounds (BEGV
, XINT (n
), ZV
));
203 region_limit (beginningp
)
206 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
207 register Lisp_Object m
;
208 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
209 && NILP (current_buffer
->mark_active
))
210 Fsignal (Qmark_inactive
, Qnil
);
211 m
= Fmarker_position (current_buffer
->mark
);
212 if (NILP (m
)) error ("There is no region now");
213 if ((point
< XFASTINT (m
)) == beginningp
)
214 return (make_number (point
));
219 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
220 "Return position of beginning of region, as an integer.")
223 return (region_limit (1));
226 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
227 "Return position of end of region, as an integer.")
230 return (region_limit (0));
233 #if 0 /* now in lisp code */
234 DEFUN ("mark", Fmark
, Smark
, 0, 0, 0,
235 "Return this buffer's mark value as integer, or nil if no mark.\n\
236 If you are using this in an editing command, you are most likely making\n\
237 a mistake; see the documentation of `set-mark'.")
240 return Fmarker_position (current_buffer
->mark
);
242 #endif /* commented out code */
244 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
245 "Return this buffer's mark, as a marker object.\n\
246 Watch out! Moving this marker changes the mark position.\n\
247 If you set the marker not to point anywhere, the buffer will have no mark.")
250 return current_buffer
->mark
;
253 #if 0 /* this is now in lisp code */
254 DEFUN ("set-mark", Fset_mark
, Sset_mark
, 1, 1, 0,
255 "Set this buffer's mark to POS. Don't use this function!\n\
256 That is to say, don't use this function unless you want\n\
257 the user to see that the mark has moved, and you want the previous\n\
258 mark position to be lost.\n\
260 Normally, when a new mark is set, the old one should go on the stack.\n\
261 This is why most applications should use push-mark, not set-mark.\n\
263 Novice programmers often try to use the mark for the wrong purposes.\n\
264 The mark saves a location for the user's convenience.\n\
265 Most editing commands should not alter the mark.\n\
266 To remember a location for internal use in the Lisp program,\n\
267 store it in a Lisp variable. Example:\n\
269 (let ((beg (point))) (forward-line 1) (delete-region beg (point))).")
275 current_buffer
->mark
= Qnil
;
278 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
280 if (NILP (current_buffer
->mark
))
281 current_buffer
->mark
= Fmake_marker ();
283 Fset_marker (current_buffer
->mark
, pos
, Qnil
);
286 #endif /* commented-out code */
289 save_excursion_save ()
291 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
294 return Fcons (Fpoint_marker (),
295 Fcons (Fcopy_marker (current_buffer
->mark
),
296 Fcons (visible
? Qt
: Qnil
,
297 current_buffer
->mark_active
)));
301 save_excursion_restore (info
)
302 register Lisp_Object info
;
304 register Lisp_Object tem
, tem1
, omark
, nmark
;
306 tem
= Fmarker_buffer (Fcar (info
));
307 /* If buffer being returned to is now deleted, avoid error */
308 /* Otherwise could get error here while unwinding to top level
310 /* In that case, Fmarker_buffer returns nil now. */
316 unchain_marker (tem
);
317 tem
= Fcar (Fcdr (info
));
318 omark
= Fmarker_position (current_buffer
->mark
);
319 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
320 nmark
= Fmarker_position (tem
);
321 unchain_marker (tem
);
322 tem
= Fcdr (Fcdr (info
));
323 #if 0 /* We used to make the current buffer visible in the selected window
324 if that was true previously. That avoids some anomalies.
325 But it creates others, and it wasn't documented, and it is simpler
326 and cleaner never to alter the window/buffer connections. */
329 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
330 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
333 tem1
= current_buffer
->mark_active
;
334 current_buffer
->mark_active
= Fcdr (tem
);
335 if (!NILP (Vrun_hooks
))
337 /* If mark is active now, and either was not active
338 or was at a different place, run the activate hook. */
339 if (! NILP (current_buffer
->mark_active
))
341 if (! EQ (omark
, nmark
))
342 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
344 /* If mark has ceased to be active, run deactivate hook. */
345 else if (! NILP (tem1
))
346 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
351 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
352 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
353 Executes BODY just like `progn'.\n\
354 The values of point, mark and the current buffer are restored\n\
355 even in case of abnormal exit (throw or error).\n\
356 The state of activation of the mark is also restored.")
360 register Lisp_Object val
;
361 int count
= specpdl_ptr
- specpdl
;
363 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
366 return unbind_to (count
, val
);
369 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
370 "Return the number of characters in the current buffer.")
374 XSETFASTINT (temp
, Z
- BEG
);
378 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
379 "Return the minimum permissible value of point in the current buffer.\n\
380 This is 1, unless narrowing (a buffer restriction) is in effect.")
384 XSETFASTINT (temp
, BEGV
);
388 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
389 "Return a marker to the minimum permissible value of point in this buffer.\n\
390 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
393 return buildmark (BEGV
);
396 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
397 "Return the maximum permissible value of point in the current buffer.\n\
398 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
399 is in effect, in which case it is less.")
403 XSETFASTINT (temp
, ZV
);
407 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
408 "Return a marker to the maximum permissible value of point in this buffer.\n\
409 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
410 is in effect, in which case it is less.")
413 return buildmark (ZV
);
416 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
417 "Return the character following point, as a number.\n\
418 At the end of the buffer or accessible region, return 0.")
423 XSETFASTINT (temp
, 0);
425 XSETFASTINT (temp
, FETCH_CHAR (point
));
429 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
430 "Return the character preceding point, as a number.\n\
431 At the beginning of the buffer or accessible region, return 0.")
436 XSETFASTINT (temp
, 0);
438 XSETFASTINT (temp
, FETCH_CHAR (point
- 1));
442 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
443 "Return T if point is at the beginning of the buffer.\n\
444 If the buffer is narrowed, this means the beginning of the narrowed part.")
452 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
453 "Return T if point is at the end of the buffer.\n\
454 If the buffer is narrowed, this means the end of the narrowed part.")
462 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
463 "Return T if point is at the beginning of a line.")
466 if (point
== BEGV
|| FETCH_CHAR (point
- 1) == '\n')
471 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
472 "Return T if point is at the end of a line.\n\
473 `End of a line' includes point being at the end of the buffer.")
476 if (point
== ZV
|| FETCH_CHAR (point
) == '\n')
481 DEFUN ("char-after", Fchar_after
, Schar_after
, 1, 1, 0,
482 "Return character in current buffer at position POS.\n\
483 POS is an integer or a buffer pointer.\n\
484 If POS is out of range, the value is nil.")
488 register Lisp_Object val
;
491 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
494 if (n
< BEGV
|| n
>= ZV
) return Qnil
;
496 XSETFASTINT (val
, FETCH_CHAR (n
));
500 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
501 "Return the name under which the user logged in, as a string.\n\
502 This is based on the effective uid, not the real uid.\n\
503 Also, if the environment variable LOGNAME or USER is set,\n\
504 that determines the value of this function.\n\n\
505 If optional argument UID is an integer, return the login name of the user\n\
506 with that uid, or nil if there is no such user.")
512 /* Set up the user name info if we didn't do it before.
513 (That can happen if Emacs is dumpable
514 but you decide to run `temacs -l loadup' and not dump. */
515 if (INTEGERP (Vuser_name
))
521 CHECK_NUMBER (uid
, 0);
522 pw
= (struct passwd
*) getpwuid (XINT (uid
));
523 return (pw
? build_string (pw
->pw_name
) : Qnil
);
526 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
528 "Return the name of the user's real uid, as a string.\n\
529 This ignores the environment variables LOGNAME and USER, so it differs from\n\
530 `user-login-name' when running under `su'.")
533 /* Set up the user name info if we didn't do it before.
534 (That can happen if Emacs is dumpable
535 but you decide to run `temacs -l loadup' and not dump. */
536 if (INTEGERP (Vuser_name
))
538 return Vuser_real_name
;
541 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
542 "Return the effective uid of Emacs, as an integer.")
545 return make_number (geteuid ());
548 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
549 "Return the real uid of Emacs, as an integer.")
552 return make_number (getuid ());
555 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 0, 0,
556 "Return the full name of the user logged in, as a string.")
559 return Vuser_full_name
;
562 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
563 "Return the name of the machine you are running on, as a string.")
569 /* For the benefit of callers who don't want to include lisp.h */
573 return (char *) XSTRING (Vsystem_name
)->data
;
576 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
577 "Return the process ID of Emacs, as an integer.")
580 return make_number (getpid ());
583 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
584 "Return the current time, as the number of seconds since 12:00 AM January 1970.\n\
585 The time is returned as a list of three integers. The first has the\n\
586 most significant 16 bits of the seconds, while the second has the\n\
587 least significant 16 bits. The third integer gives the microsecond\n\
590 The microsecond count is zero on systems that do not provide\n\
591 resolution finer than a second.")
595 Lisp_Object result
[3];
598 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
599 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
600 XSETINT (result
[2], EMACS_USECS (t
));
602 return Flist (3, result
);
607 lisp_time_argument (specified_time
, result
)
608 Lisp_Object specified_time
;
611 if (NILP (specified_time
))
612 return time (result
) != -1;
615 Lisp_Object high
, low
;
616 high
= Fcar (specified_time
);
617 CHECK_NUMBER (high
, 0);
618 low
= Fcdr (specified_time
);
621 CHECK_NUMBER (low
, 0);
622 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
623 return *result
>> 16 == XINT (high
);
627 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 2, 2, 0,
628 "Use FORMAT-STRING to format the time TIME.\n\
629 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as from\n\
630 `current-time' and `file-attributes'.\n\
631 FORMAT-STRING may contain %-sequences to substitute parts of the time.\n\
632 %a is replaced by the abbreviated name of the day of week.\n\
633 %A is replaced by the full name of the day of week.\n\
634 %b is replaced by the abbreviated name of the month.\n\
635 %B is replaced by the full name of the month.\n\
636 %c is a synonym for \"%x %X\".\n\
637 %C is a locale-specific synonym, which defaults to \"%A, %B %e, %Y\" in the C locale.\n\
638 %d is replaced by the day of month, zero-padded.\n\
639 %D is a synonym for \"%m/%d/%y\".\n\
640 %e is replaced by the day of month, blank-padded.\n\
641 %h is a synonym for \"%b\".\n\
642 %H is replaced by the hour (00-23).\n\
643 %I is replaced by the hour (00-12).\n\
644 %j is replaced by the day of the year (001-366).\n\
645 %k is replaced by the hour (0-23), blank padded.\n\
646 %l is replaced by the hour (1-12), blank padded.\n\
647 %m is replaced by the month (01-12).\n\
648 %M is replaced by the minut (00-59).\n\
649 %n is a synonym for \"\\n\".\n\
650 %p is replaced by AM or PM, as appropriate.\n\
651 %r is a synonym for \"%I:%M:%S %p\".\n\
652 %R is a synonym for \"%H:%M\".\n\
653 %S is replaced by the seconds (00-60).\n\
654 %t is a synonym for \"\\t\".\n\
655 %T is a synonym for \"%H:%M:%S\".\n\
656 %U is replaced by the week of the year (01-52), first day of week is Sunday.\n\
657 %w is replaced by the day of week (0-6), Sunday is day 0.\n\
658 %W is replaced by the week of the year (01-52), first day of week is Monday.\n\
659 %x is a locale-specific synonym, which defaults to \"%D\" in the C locale.\n\
660 %X is a locale-specific synonym, which defaults to \"%T\" in the C locale.\n\
661 %y is replaced by the year without century (00-99).\n\
662 %Y is replaced by the year with century.\n\
663 %Z is replaced by the time zone abbreviation.\n\
665 The number of options reflects the strftime(3) function.")
666 (format_string
, time
)
667 Lisp_Object format_string
, time
;
672 CHECK_STRING (format_string
, 1);
674 if (! lisp_time_argument (time
, &value
))
675 error ("Invalid time specification");
677 /* This is probably enough. */
678 size
= XSTRING (format_string
)->size
* 6 + 50;
682 char *buf
= (char *) alloca (size
);
683 if (strftime (buf
, size
, XSTRING (format_string
)->data
,
685 return build_string (buf
);
686 /* If buffer was too small, make it bigger. */
691 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
692 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
693 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
694 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
695 to use the current time. The list has the following nine members:\n\
696 SEC is an integer between 0 and 59. MINUTE is an integer between 0 and 59.\n\
697 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
698 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
699 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
700 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
701 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
702 (Note that Common Lisp has different meanings for DOW and ZONE.)")
704 Lisp_Object specified_time
;
708 struct tm
*decoded_time
;
709 Lisp_Object list_args
[9];
711 if (! lisp_time_argument (specified_time
, &time_spec
))
712 error ("Invalid time specification");
714 decoded_time
= localtime (&time_spec
);
715 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
716 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
717 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
718 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
719 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
720 XSETFASTINT (list_args
[5], decoded_time
->tm_year
+ 1900);
721 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
722 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
724 /* Make a copy, in case gmtime modifies the struct. */
725 save_tm
= *decoded_time
;
726 decoded_time
= gmtime (&time_spec
);
727 if (decoded_time
== 0)
730 XSETINT (list_args
[8], difftm (&save_tm
, decoded_time
));
731 return Flist (9, list_args
);
734 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
735 "Return the current time, as a human-readable string.\n\
736 Programs can use this function to decode a time,\n\
737 since the number of columns in each field is fixed.\n\
738 The format is `Sun Sep 16 01:03:52 1973'.\n\
739 If an argument is given, it specifies a time to format\n\
740 instead of the current time. The argument should have the form:\n\
743 (HIGH LOW . IGNORED).\n\
744 Thus, you can use times obtained from `current-time'\n\
745 and from `file-attributes'.")
747 Lisp_Object specified_time
;
753 if (! lisp_time_argument (specified_time
, &value
))
755 tem
= (char *) ctime (&value
);
757 strncpy (buf
, tem
, 24);
760 return build_string (buf
);
763 #define TM_YEAR_ORIGIN 1900
765 /* Yield A - B, measured in seconds. */
770 int ay
= a
->tm_year
+ (TM_YEAR_ORIGIN
- 1);
771 int by
= b
->tm_year
+ (TM_YEAR_ORIGIN
- 1);
772 /* Some compilers can't handle this as a single return statement. */
774 /* difference in day of year */
775 a
->tm_yday
- b
->tm_yday
776 /* + intervening leap days */
777 + ((ay
>> 2) - (by
>> 2))
779 + ((ay
/100 >> 2) - (by
/100 >> 2))
780 /* + difference in years * 365 */
781 + (long)(ay
-by
) * 365
783 return (60*(60*(24*days
+ (a
->tm_hour
- b
->tm_hour
))
784 + (a
->tm_min
- b
->tm_min
))
785 + (a
->tm_sec
- b
->tm_sec
));
788 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
789 "Return the offset and name for the local time zone.\n\
790 This returns a list of the form (OFFSET NAME).\n\
791 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
792 A negative value means west of Greenwich.\n\
793 NAME is a string giving the name of the time zone.\n\
794 If an argument is given, it specifies when the time zone offset is determined\n\
795 instead of using the current time. The argument should have the form:\n\
798 (HIGH LOW . IGNORED).\n\
799 Thus, you can use times obtained from `current-time'\n\
800 and from `file-attributes'.\n\
802 Some operating systems cannot provide all this information to Emacs;\n\
803 in this case, `current-time-zone' returns a list containing nil for\n\
804 the data it can't find.")
806 Lisp_Object specified_time
;
811 if (lisp_time_argument (specified_time
, &value
)
812 && (t
= gmtime (&value
)) != 0)
818 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
819 t
= localtime (&value
);
820 offset
= difftm (t
, &gmt
);
824 s
= (char *)t
->tm_zone
;
825 #else /* not HAVE_TM_ZONE */
827 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
828 s
= tzname
[t
->tm_isdst
];
830 #endif /* not HAVE_TM_ZONE */
833 /* No local time zone name is available; use "+-NNNN" instead. */
834 int am
= (offset
< 0 ? -offset
: offset
) / 60;
835 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
838 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
841 return Fmake_list (2, Qnil
);
853 /* Callers passing one argument to Finsert need not gcpro the
854 argument "array", since the only element of the array will
855 not be used after calling insert or insert_from_string, so
856 we don't care if it gets trashed. */
858 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
859 "Insert the arguments, either strings or characters, at point.\n\
860 Point moves forward so that it ends up after the inserted text.\n\
861 Any other markers at the point of insertion remain before the text.")
864 register Lisp_Object
*args
;
867 register Lisp_Object tem
;
870 for (argnum
= 0; argnum
< nargs
; argnum
++)
879 else if (STRINGP (tem
))
881 insert_from_string (tem
, 0, XSTRING (tem
)->size
, 0);
885 tem
= wrong_type_argument (Qchar_or_string_p
, tem
);
893 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
895 "Insert the arguments at point, inheriting properties from adjoining text.\n\
896 Point moves forward so that it ends up after the inserted text.\n\
897 Any other markers at the point of insertion remain before the text.")
900 register Lisp_Object
*args
;
903 register Lisp_Object tem
;
906 for (argnum
= 0; argnum
< nargs
; argnum
++)
913 insert_and_inherit (str
, 1);
915 else if (STRINGP (tem
))
917 insert_from_string (tem
, 0, XSTRING (tem
)->size
, 1);
921 tem
= wrong_type_argument (Qchar_or_string_p
, tem
);
929 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
930 "Insert strings or characters at point, relocating markers after the text.\n\
931 Point moves forward so that it ends up after the inserted text.\n\
932 Any other markers at the point of insertion also end up after the text.")
935 register Lisp_Object
*args
;
938 register Lisp_Object tem
;
941 for (argnum
= 0; argnum
< nargs
; argnum
++)
948 insert_before_markers (str
, 1);
950 else if (STRINGP (tem
))
952 insert_from_string_before_markers (tem
, 0, XSTRING (tem
)->size
, 0);
956 tem
= wrong_type_argument (Qchar_or_string_p
, tem
);
964 DEFUN ("insert-before-markers-and-inherit",
965 Finsert_and_inherit_before_markers
, Sinsert_and_inherit_before_markers
,
967 "Insert text at point, relocating markers and inheriting properties.\n\
968 Point moves forward so that it ends up after the inserted text.\n\
969 Any other markers at the point of insertion also end up after the text.")
972 register Lisp_Object
*args
;
975 register Lisp_Object tem
;
978 for (argnum
= 0; argnum
< nargs
; argnum
++)
985 insert_before_markers_and_inherit (str
, 1);
987 else if (STRINGP (tem
))
989 insert_from_string_before_markers (tem
, 0, XSTRING (tem
)->size
, 1);
993 tem
= wrong_type_argument (Qchar_or_string_p
, tem
);
1001 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1002 "Insert COUNT (second arg) copies of CHAR (first arg).\n\
1003 Point and all markers are affected as in the function `insert'.\n\
1004 Both arguments are required.\n\
1005 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1006 from adjoining text, if those properties are sticky.")
1007 (chr
, count
, inherit
)
1008 Lisp_Object chr
, count
, inherit
;
1010 register unsigned char *string
;
1011 register int strlen
;
1014 CHECK_NUMBER (chr
, 0);
1015 CHECK_NUMBER (count
, 1);
1020 strlen
= min (n
, 256);
1021 string
= (unsigned char *) alloca (strlen
);
1022 for (i
= 0; i
< strlen
; i
++)
1023 string
[i
] = XFASTINT (chr
);
1026 if (!NILP (inherit
))
1027 insert_and_inherit (string
, strlen
);
1029 insert (string
, strlen
);
1034 if (!NILP (inherit
))
1035 insert_and_inherit (string
, n
);
1043 /* Making strings from buffer contents. */
1045 /* Return a Lisp_String containing the text of the current buffer from
1046 START to END. If text properties are in use and the current buffer
1047 has properties in the range specified, the resulting string will also
1050 We don't want to use plain old make_string here, because it calls
1051 make_uninit_string, which can cause the buffer arena to be
1052 compacted. make_string has no way of knowing that the data has
1053 been moved, and thus copies the wrong data into the string. This
1054 doesn't effect most of the other users of make_string, so it should
1055 be left as is. But we should use this function when conjuring
1056 buffer substrings. */
1059 make_buffer_string (start
, end
)
1062 Lisp_Object result
, tem
, tem1
;
1064 if (start
< GPT
&& GPT
< end
)
1067 result
= make_uninit_string (end
- start
);
1068 bcopy (&FETCH_CHAR (start
), XSTRING (result
)->data
, end
- start
);
1070 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1071 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1073 #ifdef USE_TEXT_PROPERTIES
1074 if (XINT (tem
) != end
|| !NILP (tem1
))
1075 copy_intervals_to_string (result
, current_buffer
, start
, end
- start
);
1081 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1082 "Return the contents of part of the current buffer as a string.\n\
1083 The two arguments START and END are character positions;\n\
1084 they can be in either order.")
1088 register int beg
, end
;
1090 validate_region (&b
, &e
);
1094 return make_buffer_string (beg
, end
);
1097 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1098 "Return the contents of the current buffer as a string.")
1101 return make_buffer_string (BEGV
, ZV
);
1104 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1106 "Insert before point a substring of the contents of buffer BUFFER.\n\
1107 BUFFER may be a buffer or a buffer name.\n\
1108 Arguments START and END are character numbers specifying the substring.\n\
1109 They default to the beginning and the end of BUFFER.")
1111 Lisp_Object buf
, b
, e
;
1113 register int beg
, end
, temp
;
1114 register struct buffer
*bp
;
1117 buffer
= Fget_buffer (buf
);
1120 bp
= XBUFFER (buffer
);
1123 beg
= BUF_BEGV (bp
);
1126 CHECK_NUMBER_COERCE_MARKER (b
, 0);
1133 CHECK_NUMBER_COERCE_MARKER (e
, 1);
1138 temp
= beg
, beg
= end
, end
= temp
;
1140 if (!(BUF_BEGV (bp
) <= beg
&& end
<= BUF_ZV (bp
)))
1141 args_out_of_range (b
, e
);
1143 insert_from_buffer (bp
, beg
, end
- beg
, 0);
1147 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1149 "Compare two substrings of two buffers; return result as number.\n\
1150 the value is -N if first string is less after N-1 chars,\n\
1151 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1152 Each substring is represented as three arguments: BUFFER, START and END.\n\
1153 That makes six args in all, three for each substring.\n\n\
1154 The value of `case-fold-search' in the current buffer\n\
1155 determines whether case is significant or ignored.")
1156 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1157 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1159 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1160 register struct buffer
*bp1
, *bp2
;
1161 register unsigned char *trt
1162 = (!NILP (current_buffer
->case_fold_search
)
1163 ? XSTRING (current_buffer
->case_canon_table
)->data
: 0);
1165 /* Find the first buffer and its substring. */
1168 bp1
= current_buffer
;
1172 buf1
= Fget_buffer (buffer1
);
1175 bp1
= XBUFFER (buf1
);
1179 begp1
= BUF_BEGV (bp1
);
1182 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1183 begp1
= XINT (start1
);
1186 endp1
= BUF_ZV (bp1
);
1189 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1190 endp1
= XINT (end1
);
1194 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1196 if (!(BUF_BEGV (bp1
) <= begp1
1198 && endp1
<= BUF_ZV (bp1
)))
1199 args_out_of_range (start1
, end1
);
1201 /* Likewise for second substring. */
1204 bp2
= current_buffer
;
1208 buf2
= Fget_buffer (buffer2
);
1211 bp2
= XBUFFER (buffer2
);
1215 begp2
= BUF_BEGV (bp2
);
1218 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1219 begp2
= XINT (start2
);
1222 endp2
= BUF_ZV (bp2
);
1225 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1226 endp2
= XINT (end2
);
1230 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1232 if (!(BUF_BEGV (bp2
) <= begp2
1234 && endp2
<= BUF_ZV (bp2
)))
1235 args_out_of_range (start2
, end2
);
1237 len1
= endp1
- begp1
;
1238 len2
= endp2
- begp2
;
1243 for (i
= 0; i
< length
; i
++)
1245 int c1
= *BUF_CHAR_ADDRESS (bp1
, begp1
+ i
);
1246 int c2
= *BUF_CHAR_ADDRESS (bp2
, begp2
+ i
);
1253 return make_number (- 1 - i
);
1255 return make_number (i
+ 1);
1258 /* The strings match as far as they go.
1259 If one is shorter, that one is less. */
1261 return make_number (length
+ 1);
1262 else if (length
< len2
)
1263 return make_number (- length
- 1);
1265 /* Same length too => they are equal. */
1266 return make_number (0);
1270 subst_char_in_region_unwind (arg
)
1273 return current_buffer
->undo_list
= arg
;
1276 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1277 Ssubst_char_in_region
, 4, 5, 0,
1278 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1279 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1280 and don't mark the buffer as really changed.")
1281 (start
, end
, fromchar
, tochar
, noundo
)
1282 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1284 register int pos
, stop
, look
;
1286 int count
= specpdl_ptr
- specpdl
;
1288 validate_region (&start
, &end
);
1289 CHECK_NUMBER (fromchar
, 2);
1290 CHECK_NUMBER (tochar
, 3);
1294 look
= XINT (fromchar
);
1296 /* If we don't want undo, turn off putting stuff on the list.
1297 That's faster than getting rid of things,
1298 and it prevents even the entry for a first change. */
1301 record_unwind_protect (subst_char_in_region_unwind
,
1302 current_buffer
->undo_list
);
1303 current_buffer
->undo_list
= Qt
;
1308 if (FETCH_CHAR (pos
) == look
)
1312 modify_region (current_buffer
, XINT (start
), stop
);
1314 if (! NILP (noundo
))
1316 if (MODIFF
- 1 == SAVE_MODIFF
)
1318 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1319 current_buffer
->auto_save_modified
++;
1326 record_change (pos
, 1);
1327 FETCH_CHAR (pos
) = XINT (tochar
);
1333 signal_after_change (XINT (start
),
1334 stop
- XINT (start
), stop
- XINT (start
));
1336 unbind_to (count
, Qnil
);
1340 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1341 "From START to END, translate characters according to TABLE.\n\
1342 TABLE is a string; the Nth character in it is the mapping\n\
1343 for the character with code N. Returns the number of characters changed.")
1347 register Lisp_Object table
;
1349 register int pos
, stop
; /* Limits of the region. */
1350 register unsigned char *tt
; /* Trans table. */
1351 register int oc
; /* Old character. */
1352 register int nc
; /* New character. */
1353 int cnt
; /* Number of changes made. */
1354 Lisp_Object z
; /* Return. */
1355 int size
; /* Size of translate table. */
1357 validate_region (&start
, &end
);
1358 CHECK_STRING (table
, 2);
1360 size
= XSTRING (table
)->size
;
1361 tt
= XSTRING (table
)->data
;
1365 modify_region (current_buffer
, pos
, stop
);
1368 for (; pos
< stop
; ++pos
)
1370 oc
= FETCH_CHAR (pos
);
1376 record_change (pos
, 1);
1377 FETCH_CHAR (pos
) = nc
;
1378 signal_after_change (pos
, 1, 1);
1384 XSETFASTINT (z
, cnt
);
1388 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1389 "Delete the text between point and mark.\n\
1390 When called from a program, expects two arguments,\n\
1391 positions (integers or markers) specifying the stretch to be deleted.")
1395 validate_region (&b
, &e
);
1396 del_range (XINT (b
), XINT (e
));
1400 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1401 "Remove restrictions (narrowing) from current buffer.\n\
1402 This allows the buffer's full text to be seen and edited.")
1406 SET_BUF_ZV (current_buffer
, Z
);
1408 /* Changing the buffer bounds invalidates any recorded current column. */
1409 invalidate_current_column ();
1413 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1414 "Restrict editing in this buffer to the current region.\n\
1415 The rest of the text becomes temporarily invisible and untouchable\n\
1416 but is not deleted; if you save the buffer in a file, the invisible\n\
1417 text is included in the file. \\[widen] makes all visible again.\n\
1418 See also `save-restriction'.\n\
1420 When calling from a program, pass two arguments; positions (integers\n\
1421 or markers) bounding the text that should remain visible.")
1423 register Lisp_Object b
, e
;
1425 CHECK_NUMBER_COERCE_MARKER (b
, 0);
1426 CHECK_NUMBER_COERCE_MARKER (e
, 1);
1428 if (XINT (b
) > XINT (e
))
1431 tem
= b
; b
= e
; e
= tem
;
1434 if (!(BEG
<= XINT (b
) && XINT (b
) <= XINT (e
) && XINT (e
) <= Z
))
1435 args_out_of_range (b
, e
);
1437 BEGV
= XFASTINT (b
);
1438 SET_BUF_ZV (current_buffer
, XFASTINT (e
));
1439 if (point
< XFASTINT (b
))
1440 SET_PT (XFASTINT (b
));
1441 if (point
> XFASTINT (e
))
1442 SET_PT (XFASTINT (e
));
1444 /* Changing the buffer bounds invalidates any recorded current column. */
1445 invalidate_current_column ();
1450 save_restriction_save ()
1452 register Lisp_Object bottom
, top
;
1453 /* Note: I tried using markers here, but it does not win
1454 because insertion at the end of the saved region
1455 does not advance mh and is considered "outside" the saved region. */
1456 XSETFASTINT (bottom
, BEGV
- BEG
);
1457 XSETFASTINT (top
, Z
- ZV
);
1459 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1463 save_restriction_restore (data
)
1466 register struct buffer
*buf
;
1467 register int newhead
, newtail
;
1468 register Lisp_Object tem
;
1470 buf
= XBUFFER (XCONS (data
)->car
);
1472 data
= XCONS (data
)->cdr
;
1474 tem
= XCONS (data
)->car
;
1475 newhead
= XINT (tem
);
1476 tem
= XCONS (data
)->cdr
;
1477 newtail
= XINT (tem
);
1478 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
1483 BUF_BEGV (buf
) = BUF_BEG (buf
) + newhead
;
1484 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
1487 /* If point is outside the new visible range, move it inside. */
1489 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)));
1494 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
1495 "Execute BODY, saving and restoring current buffer's restrictions.\n\
1496 The buffer's restrictions make parts of the beginning and end invisible.\n\
1497 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
1498 This special form, `save-restriction', saves the current buffer's restrictions\n\
1499 when it is entered, and restores them when it is exited.\n\
1500 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
1501 The old restrictions settings are restored\n\
1502 even in case of abnormal exit (throw or error).\n\
1504 The value returned is the value of the last form in BODY.\n\
1506 `save-restriction' can get confused if, within the BODY, you widen\n\
1507 and then make changes outside the area within the saved restrictions.\n\
1509 Note: if you are using both `save-excursion' and `save-restriction',\n\
1510 use `save-excursion' outermost:\n\
1511 (save-excursion (save-restriction ...))")
1515 register Lisp_Object val
;
1516 int count
= specpdl_ptr
- specpdl
;
1518 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
1519 val
= Fprogn (body
);
1520 return unbind_to (count
, val
);
1523 /* Buffer for the most recent text displayed by Fmessage. */
1524 static char *message_text
;
1526 /* Allocated length of that buffer. */
1527 static int message_length
;
1529 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
1530 "Print a one-line message at the bottom of the screen.\n\
1531 The first argument is a control string.\n\
1532 It may contain %s or %d or %c to print successive following arguments.\n\
1533 %s means print an argument as a string, %d means print as number in decimal,\n\
1534 %c means print a number as a single character.\n\
1535 The argument used by %s must be a string or a symbol;\n\
1536 the argument used by %d or %c must be a number.\n\
1537 If the first argument is nil, clear any existing message; let the\n\
1538 minibuffer contents show.")
1550 register Lisp_Object val
;
1551 val
= Fformat (nargs
, args
);
1552 /* Copy the data so that it won't move when we GC. */
1555 message_text
= (char *)xmalloc (80);
1556 message_length
= 80;
1558 if (XSTRING (val
)->size
> message_length
)
1560 message_length
= XSTRING (val
)->size
;
1561 message_text
= (char *)xrealloc (message_text
, message_length
);
1563 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
1564 message2 (message_text
, XSTRING (val
)->size
);
1569 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
1570 "Display a message, in a dialog box if possible.\n\
1571 If a dialog box is not available, use the echo area.\n\
1572 The first argument is a control string.\n\
1573 It may contain %s or %d or %c to print successive following arguments.\n\
1574 %s means print an argument as a string, %d means print as number in decimal,\n\
1575 %c means print a number as a single character.\n\
1576 The argument used by %s must be a string or a symbol;\n\
1577 the argument used by %d or %c must be a number.\n\
1578 If the first argument is nil, clear any existing message; let the\n\
1579 minibuffer contents show.")
1591 register Lisp_Object val
;
1592 val
= Fformat (nargs
, args
);
1595 Lisp_Object pane
, menu
, obj
;
1596 struct gcpro gcpro1
;
1597 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
1599 menu
= Fcons (val
, pane
);
1600 obj
= Fx_popup_dialog (Qt
, menu
);
1605 /* Copy the data so that it won't move when we GC. */
1608 message_text
= (char *)xmalloc (80);
1609 message_length
= 80;
1611 if (XSTRING (val
)->size
> message_length
)
1613 message_length
= XSTRING (val
)->size
;
1614 message_text
= (char *)xrealloc (message_text
, message_length
);
1616 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
1617 message2 (message_text
, XSTRING (val
)->size
);
1623 extern Lisp_Object last_nonmenu_event
;
1625 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
1626 "Display a message in a dialog box or in the echo area.\n\
1627 If this command was invoked with the mouse, use a dialog box.\n\
1628 Otherwise, use the echo area.\n\
1630 The first argument is a control string.\n\
1631 It may contain %s or %d or %c to print successive following arguments.\n\
1632 %s means print an argument as a string, %d means print as number in decimal,\n\
1633 %c means print a number as a single character.\n\
1634 The argument used by %s must be a string or a symbol;\n\
1635 the argument used by %d or %c must be a number.\n\
1636 If the first argument is nil, clear any existing message; let the\n\
1637 minibuffer contents show.")
1643 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
1644 return Fmessage_box (nargs
, args
);
1646 return Fmessage (nargs
, args
);
1649 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
1650 "Format a string out of a control-string and arguments.\n\
1651 The first argument is a control string.\n\
1652 The other arguments are substituted into it to make the result, a string.\n\
1653 It may contain %-sequences meaning to substitute the next argument.\n\
1654 %s means print a string argument. Actually, prints any object, with `princ'.\n\
1655 %d means print as number in decimal (%o octal, %x hex).\n\
1656 %c means print a number as a single character.\n\
1657 %S means print any object as an s-expression (using prin1).\n\
1658 The argument used for %d, %o, %x or %c must be a number.\n\
1659 Use %% to put a single % into the output.")
1662 register Lisp_Object
*args
;
1664 register int n
; /* The number of the next arg to substitute */
1665 register int total
= 5; /* An estimate of the final length */
1667 register unsigned char *format
, *end
;
1669 extern char *index ();
1670 /* It should not be necessary to GCPRO ARGS, because
1671 the caller in the interpreter should take care of that. */
1673 CHECK_STRING (args
[0], 0);
1674 format
= XSTRING (args
[0])->data
;
1675 end
= format
+ XSTRING (args
[0])->size
;
1678 while (format
!= end
)
1679 if (*format
++ == '%')
1683 /* Process a numeric arg and skip it. */
1684 minlen
= atoi (format
);
1689 while ((*format
>= '0' && *format
<= '9')
1690 || *format
== '-' || *format
== ' ' || *format
== '.')
1695 else if (++n
>= nargs
)
1696 error ("not enough arguments for format string");
1697 else if (*format
== 'S')
1699 /* For `S', prin1 the argument and then treat like a string. */
1700 register Lisp_Object tem
;
1701 tem
= Fprin1_to_string (args
[n
], Qnil
);
1705 else if (SYMBOLP (args
[n
]))
1707 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
1710 else if (STRINGP (args
[n
]))
1713 if (*format
!= 's' && *format
!= 'S')
1714 error ("format specifier doesn't match argument type");
1715 total
+= XSTRING (args
[n
])->size
;
1717 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
1718 else if (INTEGERP (args
[n
]) && *format
!= 's')
1720 #ifdef LISP_FLOAT_TYPE
1721 /* The following loop assumes the Lisp type indicates
1722 the proper way to pass the argument.
1723 So make sure we have a flonum if the argument should
1725 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
1726 args
[n
] = Ffloat (args
[n
]);
1730 #ifdef LISP_FLOAT_TYPE
1731 else if (FLOATP (args
[n
]) && *format
!= 's')
1733 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
1734 args
[n
] = Ftruncate (args
[n
]);
1740 /* Anything but a string, convert to a string using princ. */
1741 register Lisp_Object tem
;
1742 tem
= Fprin1_to_string (args
[n
], Qt
);
1749 register int nstrings
= n
+ 1;
1751 /* Allocate twice as many strings as we have %-escapes; floats occupy
1752 two slots, and we're not sure how many of those we have. */
1753 register unsigned char **strings
1754 = (unsigned char **) alloca (2 * nstrings
* sizeof (unsigned char *));
1758 for (n
= 0; n
< nstrings
; n
++)
1761 strings
[i
++] = (unsigned char *) "";
1762 else if (INTEGERP (args
[n
]))
1763 /* We checked above that the corresponding format effector
1764 isn't %s, which would cause MPV. */
1765 strings
[i
++] = (unsigned char *) XINT (args
[n
]);
1766 #ifdef LISP_FLOAT_TYPE
1767 else if (FLOATP (args
[n
]))
1769 union { double d
; int half
[2]; } u
;
1771 u
.d
= XFLOAT (args
[n
])->data
;
1772 strings
[i
++] = (unsigned char *) u
.half
[0];
1773 strings
[i
++] = (unsigned char *) u
.half
[1];
1777 strings
[i
++] = XSTRING (args
[n
])->data
;
1780 /* Format it in bigger and bigger buf's until it all fits. */
1783 buf
= (char *) alloca (total
+ 1);
1786 length
= doprnt (buf
, total
+ 1, strings
[0], end
, i
-1, strings
+ 1);
1787 if (buf
[total
- 1] == 0)
1795 return make_string (buf
, length
);
1801 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
1802 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
1816 doprnt (buf
, sizeof buf
, string1
, 0, 5, args
);
1818 doprnt (buf
, sizeof buf
, string1
, 0, 5, &string1
+ 1);
1820 return build_string (buf
);
1823 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
1824 "Return t if two characters match, optionally ignoring case.\n\
1825 Both arguments must be characters (i.e. integers).\n\
1826 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
1828 register Lisp_Object c1
, c2
;
1830 unsigned char *downcase
= DOWNCASE_TABLE
;
1831 CHECK_NUMBER (c1
, 0);
1832 CHECK_NUMBER (c2
, 1);
1834 if (!NILP (current_buffer
->case_fold_search
)
1835 ? (downcase
[0xff & XFASTINT (c1
)] == downcase
[0xff & XFASTINT (c2
)]
1836 && (XFASTINT (c1
) & ~0xff) == (XFASTINT (c2
) & ~0xff))
1837 : XINT (c1
) == XINT (c2
))
1842 /* Transpose the markers in two regions of the current buffer, and
1843 adjust the ones between them if necessary (i.e.: if the regions
1846 Traverses the entire marker list of the buffer to do so, adding an
1847 appropriate amount to some, subtracting from some, and leaving the
1848 rest untouched. Most of this is copied from adjust_markers in insdel.c.
1850 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
1853 transpose_markers (start1
, end1
, start2
, end2
)
1854 register int start1
, end1
, start2
, end2
;
1856 register int amt1
, amt2
, diff
, mpos
;
1857 register Lisp_Object marker
;
1859 /* Update point as if it were a marker. */
1863 TEMP_SET_PT (PT
+ (end2
- end1
));
1864 else if (PT
< start2
)
1865 TEMP_SET_PT (PT
+ (end2
- start2
) - (end1
- start1
));
1867 TEMP_SET_PT (PT
- (start2
- start1
));
1869 /* We used to adjust the endpoints here to account for the gap, but that
1870 isn't good enough. Even if we assume the caller has tried to move the
1871 gap out of our way, it might still be at start1 exactly, for example;
1872 and that places it `inside' the interval, for our purposes. The amount
1873 of adjustment is nontrivial if there's a `denormalized' marker whose
1874 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
1875 the dirty work to Fmarker_position, below. */
1877 /* The difference between the region's lengths */
1878 diff
= (end2
- start2
) - (end1
- start1
);
1880 /* For shifting each marker in a region by the length of the other
1881 * region plus the distance between the regions.
1883 amt1
= (end2
- start2
) + (start2
- end1
);
1884 amt2
= (end1
- start1
) + (start2
- end1
);
1886 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
1887 marker
= XMARKER (marker
)->chain
)
1889 mpos
= Fmarker_position (marker
);
1890 if (mpos
>= start1
&& mpos
< end2
)
1894 else if (mpos
< start2
)
1898 if (mpos
> GPT
) mpos
+= GAP_SIZE
;
1899 XMARKER (marker
)->bufpos
= mpos
;
1904 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
1905 "Transpose region START1 to END1 with START2 to END2.\n\
1906 The regions may not be overlapping, because the size of the buffer is\n\
1907 never changed in a transposition.\n\
1909 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
1910 any markers that happen to be located in the regions.\n\
1912 Transposing beyond buffer boundaries is an error.")
1913 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
1914 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
1916 register int start1
, end1
, start2
, end2
,
1917 gap
, len1
, len_mid
, len2
;
1918 unsigned char *start1_addr
, *start2_addr
, *temp
;
1920 #ifdef USE_TEXT_PROPERTIES
1921 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
1922 cur_intv
= BUF_INTERVALS (current_buffer
);
1923 #endif /* USE_TEXT_PROPERTIES */
1925 validate_region (&startr1
, &endr1
);
1926 validate_region (&startr2
, &endr2
);
1928 start1
= XFASTINT (startr1
);
1929 end1
= XFASTINT (endr1
);
1930 start2
= XFASTINT (startr2
);
1931 end2
= XFASTINT (endr2
);
1934 /* Swap the regions if they're reversed. */
1937 register int glumph
= start1
;
1945 len1
= end1
- start1
;
1946 len2
= end2
- start2
;
1949 error ("transposed regions not properly ordered");
1950 else if (start1
== end1
|| start2
== end2
)
1951 error ("transposed region may not be of length 0");
1953 /* The possibilities are:
1954 1. Adjacent (contiguous) regions, or separate but equal regions
1955 (no, really equal, in this case!), or
1956 2. Separate regions of unequal size.
1958 The worst case is usually No. 2. It means that (aside from
1959 potential need for getting the gap out of the way), there also
1960 needs to be a shifting of the text between the two regions. So
1961 if they are spread far apart, we are that much slower... sigh. */
1963 /* It must be pointed out that the really studly thing to do would
1964 be not to move the gap at all, but to leave it in place and work
1965 around it if necessary. This would be extremely efficient,
1966 especially considering that people are likely to do
1967 transpositions near where they are working interactively, which
1968 is exactly where the gap would be found. However, such code
1969 would be much harder to write and to read. So, if you are
1970 reading this comment and are feeling squirrely, by all means have
1971 a go! I just didn't feel like doing it, so I will simply move
1972 the gap the minimum distance to get it out of the way, and then
1973 deal with an unbroken array. */
1975 /* Make sure the gap won't interfere, by moving it out of the text
1976 we will operate on. */
1977 if (start1
< gap
&& gap
< end2
)
1979 if (gap
- start1
< end2
- gap
)
1985 /* Hmmm... how about checking to see if the gap is large
1986 enough to use as the temporary storage? That would avoid an
1987 allocation... interesting. Later, don't fool with it now. */
1989 /* Working without memmove, for portability (sigh), so must be
1990 careful of overlapping subsections of the array... */
1992 if (end1
== start2
) /* adjacent regions */
1994 modify_region (current_buffer
, start1
, end2
);
1995 record_change (start1
, len1
+ len2
);
1997 #ifdef USE_TEXT_PROPERTIES
1998 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
1999 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2000 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2001 #endif /* USE_TEXT_PROPERTIES */
2003 /* First region smaller than second. */
2006 /* We use alloca only if it is small,
2007 because we want to avoid stack overflow. */
2009 temp
= (unsigned char *) xmalloc (len2
);
2011 temp
= (unsigned char *) alloca (len2
);
2013 /* Don't precompute these addresses. We have to compute them
2014 at the last minute, because the relocating allocator might
2015 have moved the buffer around during the xmalloc. */
2016 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2017 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2019 bcopy (start2_addr
, temp
, len2
);
2020 bcopy (start1_addr
, start1_addr
+ len2
, len1
);
2021 bcopy (temp
, start1_addr
, len2
);
2026 /* First region not smaller than second. */
2029 temp
= (unsigned char *) xmalloc (len1
);
2031 temp
= (unsigned char *) alloca (len1
);
2032 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2033 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2034 bcopy (start1_addr
, temp
, len1
);
2035 bcopy (start2_addr
, start1_addr
, len2
);
2036 bcopy (temp
, start1_addr
+ len2
, len1
);
2040 #ifdef USE_TEXT_PROPERTIES
2041 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2042 len1
, current_buffer
, 0);
2043 graft_intervals_into_buffer (tmp_interval2
, start1
,
2044 len2
, current_buffer
, 0);
2045 #endif /* USE_TEXT_PROPERTIES */
2047 /* Non-adjacent regions, because end1 != start2, bleagh... */
2051 /* Regions are same size, though, how nice. */
2053 modify_region (current_buffer
, start1
, end1
);
2054 modify_region (current_buffer
, start2
, end2
);
2055 record_change (start1
, len1
);
2056 record_change (start2
, len2
);
2057 #ifdef USE_TEXT_PROPERTIES
2058 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2059 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2060 Fset_text_properties (start1
, end1
, Qnil
, Qnil
);
2061 Fset_text_properties (start2
, end2
, Qnil
, Qnil
);
2062 #endif /* USE_TEXT_PROPERTIES */
2065 temp
= (unsigned char *) xmalloc (len1
);
2067 temp
= (unsigned char *) alloca (len1
);
2068 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2069 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2070 bcopy (start1_addr
, temp
, len1
);
2071 bcopy (start2_addr
, start1_addr
, len2
);
2072 bcopy (temp
, start2_addr
, len1
);
2075 #ifdef USE_TEXT_PROPERTIES
2076 graft_intervals_into_buffer (tmp_interval1
, start2
,
2077 len1
, current_buffer
, 0);
2078 graft_intervals_into_buffer (tmp_interval2
, start1
,
2079 len2
, current_buffer
, 0);
2080 #endif /* USE_TEXT_PROPERTIES */
2083 else if (len1
< len2
) /* Second region larger than first */
2084 /* Non-adjacent & unequal size, area between must also be shifted. */
2086 len_mid
= start2
- end1
;
2087 modify_region (current_buffer
, start1
, end2
);
2088 record_change (start1
, (end2
- start1
));
2089 #ifdef USE_TEXT_PROPERTIES
2090 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2091 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2092 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2093 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2094 #endif /* USE_TEXT_PROPERTIES */
2096 /* holds region 2 */
2098 temp
= (unsigned char *) xmalloc (len2
);
2100 temp
= (unsigned char *) alloca (len2
);
2101 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2102 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2103 bcopy (start2_addr
, temp
, len2
);
2104 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2
, len1
);
2105 safe_bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2106 bcopy (temp
, start1_addr
, len2
);
2109 #ifdef USE_TEXT_PROPERTIES
2110 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2111 len1
, current_buffer
, 0);
2112 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2113 len_mid
, current_buffer
, 0);
2114 graft_intervals_into_buffer (tmp_interval2
, start1
,
2115 len2
, current_buffer
, 0);
2116 #endif /* USE_TEXT_PROPERTIES */
2119 /* Second region smaller than first. */
2121 len_mid
= start2
- end1
;
2122 record_change (start1
, (end2
- start1
));
2123 modify_region (current_buffer
, start1
, end2
);
2125 #ifdef USE_TEXT_PROPERTIES
2126 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2127 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2128 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2129 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2130 #endif /* USE_TEXT_PROPERTIES */
2132 /* holds region 1 */
2134 temp
= (unsigned char *) xmalloc (len1
);
2136 temp
= (unsigned char *) alloca (len1
);
2137 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2138 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2139 bcopy (start1_addr
, temp
, len1
);
2140 bcopy (start2_addr
, start1_addr
, len2
);
2141 bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2142 bcopy (temp
, start1_addr
+ len2
+ len_mid
, len1
);
2145 #ifdef USE_TEXT_PROPERTIES
2146 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2147 len1
, current_buffer
, 0);
2148 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2149 len_mid
, current_buffer
, 0);
2150 graft_intervals_into_buffer (tmp_interval2
, start1
,
2151 len2
, current_buffer
, 0);
2152 #endif /* USE_TEXT_PROPERTIES */
2156 /* todo: this will be slow, because for every transposition, we
2157 traverse the whole friggin marker list. Possible solutions:
2158 somehow get a list of *all* the markers across multiple
2159 transpositions and do it all in one swell phoop. Or maybe modify
2160 Emacs' marker code to keep an ordered list or tree. This might
2161 be nicer, and more beneficial in the long run, but would be a
2162 bunch of work. Plus the way they're arranged now is nice. */
2163 if (NILP (leave_markers
))
2165 transpose_markers (start1
, end1
, start2
, end2
);
2166 fix_overlays_in_range (start1
, end2
);
2176 DEFVAR_LISP ("system-name", &Vsystem_name
,
2177 "The name of the machine Emacs is running on.");
2179 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2180 "The full name of the user logged in.");
2182 DEFVAR_LISP ("user-name", &Vuser_name
,
2183 "The user's name, taken from environment variables if possible.");
2185 DEFVAR_LISP ("user-real-name", &Vuser_real_name
,
2186 "The user's name, based upon the real uid only.");
2188 defsubr (&Schar_equal
);
2189 defsubr (&Sgoto_char
);
2190 defsubr (&Sstring_to_char
);
2191 defsubr (&Schar_to_string
);
2192 defsubr (&Sbuffer_substring
);
2193 defsubr (&Sbuffer_string
);
2195 defsubr (&Spoint_marker
);
2196 defsubr (&Smark_marker
);
2198 defsubr (&Sregion_beginning
);
2199 defsubr (&Sregion_end
);
2200 /* defsubr (&Smark); */
2201 /* defsubr (&Sset_mark); */
2202 defsubr (&Ssave_excursion
);
2204 defsubr (&Sbufsize
);
2205 defsubr (&Spoint_max
);
2206 defsubr (&Spoint_min
);
2207 defsubr (&Spoint_min_marker
);
2208 defsubr (&Spoint_max_marker
);
2214 defsubr (&Sfollowing_char
);
2215 defsubr (&Sprevious_char
);
2216 defsubr (&Schar_after
);
2218 defsubr (&Sinsert_before_markers
);
2219 defsubr (&Sinsert_and_inherit
);
2220 defsubr (&Sinsert_and_inherit_before_markers
);
2221 defsubr (&Sinsert_char
);
2223 defsubr (&Suser_login_name
);
2224 defsubr (&Suser_real_login_name
);
2225 defsubr (&Suser_uid
);
2226 defsubr (&Suser_real_uid
);
2227 defsubr (&Suser_full_name
);
2228 defsubr (&Semacs_pid
);
2229 defsubr (&Scurrent_time
);
2230 defsubr (&Sformat_time_string
);
2231 defsubr (&Sdecode_time
);
2232 defsubr (&Scurrent_time_string
);
2233 defsubr (&Scurrent_time_zone
);
2234 defsubr (&Ssystem_name
);
2235 defsubr (&Smessage
);
2236 defsubr (&Smessage_box
);
2237 defsubr (&Smessage_or_box
);
2240 defsubr (&Sinsert_buffer_substring
);
2241 defsubr (&Scompare_buffer_substrings
);
2242 defsubr (&Ssubst_char_in_region
);
2243 defsubr (&Stranslate_region
);
2244 defsubr (&Sdelete_region
);
2246 defsubr (&Snarrow_to_region
);
2247 defsubr (&Ssave_restriction
);
2248 defsubr (&Stranspose_regions
);