]> code.delx.au - gnu-emacs/blob - src/editfns.c
(x_window): With Motif, double extra_borders.
[gnu-emacs] / src / editfns.c
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <sys/types.h>
23
24 #include <config.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 "intervals.h"
34 #include "buffer.h"
35 #include "window.h"
36
37 #include "systime.h"
38
39 #define min(a, b) ((a) < (b) ? (a) : (b))
40 #define max(a, b) ((a) > (b) ? (a) : (b))
41
42 extern char **environ;
43 extern Lisp_Object make_time ();
44 extern void insert_from_buffer ();
45 static int tm_diff ();
46 static void update_buffer_properties ();
47 void set_time_zone_rule ();
48
49 Lisp_Object Vbuffer_access_fontify_functions;
50 Lisp_Object Qbuffer_access_fontify_functions;
51 Lisp_Object Vbuffer_access_fontified_property;
52
53 /* Some static data, and a function to initialize it for each run */
54
55 Lisp_Object Vsystem_name;
56 Lisp_Object Vuser_real_login_name; /* login name of current user ID */
57 Lisp_Object Vuser_full_name; /* full name of current user */
58 Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
59
60 void
61 init_editfns ()
62 {
63 char *user_name;
64 register unsigned char *p, *q, *r;
65 struct passwd *pw; /* password entry for the current user */
66 extern char *index ();
67 Lisp_Object tem;
68
69 /* Set up system_name even when dumping. */
70 init_system_name ();
71
72 #ifndef CANNOT_DUMP
73 /* Don't bother with this on initial start when just dumping out */
74 if (!initialized)
75 return;
76 #endif /* not CANNOT_DUMP */
77
78 pw = (struct passwd *) getpwuid (getuid ());
79 #ifdef MSDOS
80 /* We let the real user name default to "root" because that's quite
81 accurate on MSDOG and because it lets Emacs find the init file.
82 (The DVX libraries override the Djgpp libraries here.) */
83 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
84 #else
85 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
86 #endif
87
88 /* Get the effective user name, by consulting environment variables,
89 or the effective uid if those are unset. */
90 user_name = (char *) getenv ("LOGNAME");
91 if (!user_name)
92 #ifdef WINDOWSNT
93 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
94 #else /* WINDOWSNT */
95 user_name = (char *) getenv ("USER");
96 #endif /* WINDOWSNT */
97 if (!user_name)
98 {
99 pw = (struct passwd *) getpwuid (geteuid ());
100 user_name = (char *) (pw ? pw->pw_name : "unknown");
101 }
102 Vuser_login_name = build_string (user_name);
103
104 /* If the user name claimed in the environment vars differs from
105 the real uid, use the claimed name to find the full name. */
106 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
107 if (NILP (tem))
108 pw = (struct passwd *) getpwnam (XSTRING (Vuser_login_name)->data);
109
110 p = (unsigned char *) (pw ? USER_FULL_NAME : "unknown");
111 q = (unsigned char *) index (p, ',');
112 Vuser_full_name = make_string (p, q ? q - p : strlen (p));
113
114 #ifdef AMPERSAND_FULL_NAME
115 p = XSTRING (Vuser_full_name)->data;
116 q = (unsigned char *) index (p, '&');
117 /* Substitute the login name for the &, upcasing the first character. */
118 if (q)
119 {
120 r = (unsigned char *) alloca (strlen (p)
121 + XSTRING (Vuser_login_name)->size + 1);
122 bcopy (p, r, q - p);
123 r[q - p] = 0;
124 strcat (r, XSTRING (Vuser_login_name)->data);
125 r[q - p] = UPCASE (r[q - p]);
126 strcat (r, q + 1);
127 Vuser_full_name = build_string (r);
128 }
129 #endif /* AMPERSAND_FULL_NAME */
130
131 p = (unsigned char *) getenv ("NAME");
132 if (p)
133 Vuser_full_name = build_string (p);
134 }
135 \f
136 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
137 "Convert arg CHARACTER to a one-character string containing that character.")
138 (character)
139 Lisp_Object character;
140 {
141 char c;
142 CHECK_NUMBER (character, 0);
143
144 c = XINT (character);
145 return make_string (&c, 1);
146 }
147
148 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
149 "Convert arg STRING to a character, the first character of that string.")
150 (string)
151 register Lisp_Object string;
152 {
153 register Lisp_Object val;
154 register struct Lisp_String *p;
155 CHECK_STRING (string, 0);
156
157 p = XSTRING (string);
158 if (p->size)
159 XSETFASTINT (val, ((unsigned char *) p->data)[0]);
160 else
161 XSETFASTINT (val, 0);
162 return val;
163 }
164 \f
165 static Lisp_Object
166 buildmark (val)
167 int val;
168 {
169 register Lisp_Object mark;
170 mark = Fmake_marker ();
171 Fset_marker (mark, make_number (val), Qnil);
172 return mark;
173 }
174
175 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
176 "Return value of point, as an integer.\n\
177 Beginning of buffer is position (point-min)")
178 ()
179 {
180 Lisp_Object temp;
181 XSETFASTINT (temp, PT);
182 return temp;
183 }
184
185 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
186 "Return value of point, as a marker object.")
187 ()
188 {
189 return buildmark (PT);
190 }
191
192 int
193 clip_to_bounds (lower, num, upper)
194 int lower, num, upper;
195 {
196 if (num < lower)
197 return lower;
198 else if (num > upper)
199 return upper;
200 else
201 return num;
202 }
203
204 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
205 "Set point to POSITION, a number or marker.\n\
206 Beginning of buffer is position (point-min), end is (point-max).")
207 (position)
208 register Lisp_Object position;
209 {
210 CHECK_NUMBER_COERCE_MARKER (position, 0);
211
212 SET_PT (clip_to_bounds (BEGV, XINT (position), ZV));
213 return position;
214 }
215
216 static Lisp_Object
217 region_limit (beginningp)
218 int beginningp;
219 {
220 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
221 register Lisp_Object m;
222 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
223 && NILP (current_buffer->mark_active))
224 Fsignal (Qmark_inactive, Qnil);
225 m = Fmarker_position (current_buffer->mark);
226 if (NILP (m)) error ("There is no region now");
227 if ((PT < XFASTINT (m)) == beginningp)
228 return (make_number (PT));
229 else
230 return (m);
231 }
232
233 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
234 "Return position of beginning of region, as an integer.")
235 ()
236 {
237 return (region_limit (1));
238 }
239
240 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
241 "Return position of end of region, as an integer.")
242 ()
243 {
244 return (region_limit (0));
245 }
246
247 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
248 "Return this buffer's mark, as a marker object.\n\
249 Watch out! Moving this marker changes the mark position.\n\
250 If you set the marker not to point anywhere, the buffer will have no mark.")
251 ()
252 {
253 return current_buffer->mark;
254 }
255
256 Lisp_Object
257 save_excursion_save ()
258 {
259 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
260 == current_buffer);
261
262 return Fcons (Fpoint_marker (),
263 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
264 Fcons (visible ? Qt : Qnil,
265 current_buffer->mark_active)));
266 }
267
268 Lisp_Object
269 save_excursion_restore (info)
270 Lisp_Object info;
271 {
272 Lisp_Object tem, tem1, omark, nmark;
273 struct gcpro gcpro1, gcpro2, gcpro3;
274
275 tem = Fmarker_buffer (Fcar (info));
276 /* If buffer being returned to is now deleted, avoid error */
277 /* Otherwise could get error here while unwinding to top level
278 and crash */
279 /* In that case, Fmarker_buffer returns nil now. */
280 if (NILP (tem))
281 return Qnil;
282
283 omark = nmark = Qnil;
284 GCPRO3 (info, omark, nmark);
285
286 Fset_buffer (tem);
287 tem = Fcar (info);
288 Fgoto_char (tem);
289 unchain_marker (tem);
290 tem = Fcar (Fcdr (info));
291 omark = Fmarker_position (current_buffer->mark);
292 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
293 nmark = Fmarker_position (tem);
294 unchain_marker (tem);
295 tem = Fcdr (Fcdr (info));
296 #if 0 /* We used to make the current buffer visible in the selected window
297 if that was true previously. That avoids some anomalies.
298 But it creates others, and it wasn't documented, and it is simpler
299 and cleaner never to alter the window/buffer connections. */
300 tem1 = Fcar (tem);
301 if (!NILP (tem1)
302 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
303 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
304 #endif /* 0 */
305
306 tem1 = current_buffer->mark_active;
307 current_buffer->mark_active = Fcdr (tem);
308 if (!NILP (Vrun_hooks))
309 {
310 /* If mark is active now, and either was not active
311 or was at a different place, run the activate hook. */
312 if (! NILP (current_buffer->mark_active))
313 {
314 if (! EQ (omark, nmark))
315 call1 (Vrun_hooks, intern ("activate-mark-hook"));
316 }
317 /* If mark has ceased to be active, run deactivate hook. */
318 else if (! NILP (tem1))
319 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
320 }
321 UNGCPRO;
322 return Qnil;
323 }
324
325 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
326 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
327 Executes BODY just like `progn'.\n\
328 The values of point, mark and the current buffer are restored\n\
329 even in case of abnormal exit (throw or error).\n\
330 The state of activation of the mark is also restored.")
331 (args)
332 Lisp_Object args;
333 {
334 register Lisp_Object val;
335 int count = specpdl_ptr - specpdl;
336
337 record_unwind_protect (save_excursion_restore, save_excursion_save ());
338
339 val = Fprogn (args);
340 return unbind_to (count, val);
341 }
342
343 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
344 "Save the current buffer; execute BODY; restore the current buffer.\n\
345 Executes BODY just like `progn'.")
346 (args)
347 Lisp_Object args;
348 {
349 register Lisp_Object val;
350 int count = specpdl_ptr - specpdl;
351
352 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
353
354 val = Fprogn (args);
355 return unbind_to (count, val);
356 }
357 \f
358 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
359 "Return the number of characters in the current buffer.")
360 ()
361 {
362 Lisp_Object temp;
363 XSETFASTINT (temp, Z - BEG);
364 return temp;
365 }
366
367 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
368 "Return the minimum permissible value of point in the current buffer.\n\
369 This is 1, unless narrowing (a buffer restriction) is in effect.")
370 ()
371 {
372 Lisp_Object temp;
373 XSETFASTINT (temp, BEGV);
374 return temp;
375 }
376
377 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
378 "Return a marker to the minimum permissible value of point in this buffer.\n\
379 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
380 ()
381 {
382 return buildmark (BEGV);
383 }
384
385 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
386 "Return the maximum permissible value of point in the current buffer.\n\
387 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
388 is in effect, in which case it is less.")
389 ()
390 {
391 Lisp_Object temp;
392 XSETFASTINT (temp, ZV);
393 return temp;
394 }
395
396 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
397 "Return a marker to the maximum permissible value of point in this buffer.\n\
398 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
399 is in effect, in which case it is less.")
400 ()
401 {
402 return buildmark (ZV);
403 }
404
405 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
406 "Return the character following point, as a number.\n\
407 At the end of the buffer or accessible region, return 0.")
408 ()
409 {
410 Lisp_Object temp;
411 if (PT >= ZV)
412 XSETFASTINT (temp, 0);
413 else
414 XSETFASTINT (temp, FETCH_CHAR (PT));
415 return temp;
416 }
417
418 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
419 "Return the character preceding point, as a number.\n\
420 At the beginning of the buffer or accessible region, return 0.")
421 ()
422 {
423 Lisp_Object temp;
424 if (PT <= BEGV)
425 XSETFASTINT (temp, 0);
426 else
427 XSETFASTINT (temp, FETCH_CHAR (PT - 1));
428 return temp;
429 }
430
431 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
432 "Return T if point is at the beginning of the buffer.\n\
433 If the buffer is narrowed, this means the beginning of the narrowed part.")
434 ()
435 {
436 if (PT == BEGV)
437 return Qt;
438 return Qnil;
439 }
440
441 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
442 "Return T if point is at the end of the buffer.\n\
443 If the buffer is narrowed, this means the end of the narrowed part.")
444 ()
445 {
446 if (PT == ZV)
447 return Qt;
448 return Qnil;
449 }
450
451 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
452 "Return T if point is at the beginning of a line.")
453 ()
454 {
455 if (PT == BEGV || FETCH_CHAR (PT - 1) == '\n')
456 return Qt;
457 return Qnil;
458 }
459
460 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
461 "Return T if point is at the end of a line.\n\
462 `End of a line' includes point being at the end of the buffer.")
463 ()
464 {
465 if (PT == ZV || FETCH_CHAR (PT) == '\n')
466 return Qt;
467 return Qnil;
468 }
469
470 DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
471 "Return character in current buffer at position POS.\n\
472 POS is an integer or a buffer pointer.\n\
473 If POS is out of range, the value is nil.")
474 (pos)
475 Lisp_Object pos;
476 {
477 register Lisp_Object val;
478 register int n;
479
480 CHECK_NUMBER_COERCE_MARKER (pos, 0);
481
482 n = XINT (pos);
483 if (n < BEGV || n >= ZV) return Qnil;
484
485 XSETFASTINT (val, FETCH_CHAR (n));
486 return val;
487 }
488 \f
489 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
490 "Return the name under which the user logged in, as a string.\n\
491 This is based on the effective uid, not the real uid.\n\
492 Also, if the environment variable LOGNAME or USER is set,\n\
493 that determines the value of this function.\n\n\
494 If optional argument UID is an integer, return the login name of the user\n\
495 with that uid, or nil if there is no such user.")
496 (uid)
497 Lisp_Object uid;
498 {
499 struct passwd *pw;
500
501 /* Set up the user name info if we didn't do it before.
502 (That can happen if Emacs is dumpable
503 but you decide to run `temacs -l loadup' and not dump. */
504 if (INTEGERP (Vuser_login_name))
505 init_editfns ();
506
507 if (NILP (uid))
508 return Vuser_login_name;
509
510 CHECK_NUMBER (uid, 0);
511 pw = (struct passwd *) getpwuid (XINT (uid));
512 return (pw ? build_string (pw->pw_name) : Qnil);
513 }
514
515 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
516 0, 0, 0,
517 "Return the name of the user's real uid, as a string.\n\
518 This ignores the environment variables LOGNAME and USER, so it differs from\n\
519 `user-login-name' when running under `su'.")
520 ()
521 {
522 /* Set up the user name info if we didn't do it before.
523 (That can happen if Emacs is dumpable
524 but you decide to run `temacs -l loadup' and not dump. */
525 if (INTEGERP (Vuser_login_name))
526 init_editfns ();
527 return Vuser_real_login_name;
528 }
529
530 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
531 "Return the effective uid of Emacs, as an integer.")
532 ()
533 {
534 return make_number (geteuid ());
535 }
536
537 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
538 "Return the real uid of Emacs, as an integer.")
539 ()
540 {
541 return make_number (getuid ());
542 }
543
544 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, 0,
545 "Return the full name of the user logged in, as a string.")
546 ()
547 {
548 return Vuser_full_name;
549 }
550
551 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
552 "Return the name of the machine you are running on, as a string.")
553 ()
554 {
555 return Vsystem_name;
556 }
557
558 /* For the benefit of callers who don't want to include lisp.h */
559 char *
560 get_system_name ()
561 {
562 return (char *) XSTRING (Vsystem_name)->data;
563 }
564
565 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
566 "Return the process ID of Emacs, as an integer.")
567 ()
568 {
569 return make_number (getpid ());
570 }
571
572 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
573 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
574 The time is returned as a list of three integers. The first has the\n\
575 most significant 16 bits of the seconds, while the second has the\n\
576 least significant 16 bits. The third integer gives the microsecond\n\
577 count.\n\
578 \n\
579 The microsecond count is zero on systems that do not provide\n\
580 resolution finer than a second.")
581 ()
582 {
583 EMACS_TIME t;
584 Lisp_Object result[3];
585
586 EMACS_GET_TIME (t);
587 XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff);
588 XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff);
589 XSETINT (result[2], EMACS_USECS (t));
590
591 return Flist (3, result);
592 }
593 \f
594
595 static int
596 lisp_time_argument (specified_time, result)
597 Lisp_Object specified_time;
598 time_t *result;
599 {
600 if (NILP (specified_time))
601 return time (result) != -1;
602 else
603 {
604 Lisp_Object high, low;
605 high = Fcar (specified_time);
606 CHECK_NUMBER (high, 0);
607 low = Fcdr (specified_time);
608 if (CONSP (low))
609 low = Fcar (low);
610 CHECK_NUMBER (low, 0);
611 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
612 return *result >> 16 == XINT (high);
613 }
614 }
615
616 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 2, 0,
617 "Use FORMAT-STRING to format the time TIME.\n\
618 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as from\n\
619 `current-time' and `file-attributes'.\n\
620 FORMAT-STRING may contain %-sequences to substitute parts of the time.\n\
621 %a is replaced by the abbreviated name of the day of week.\n\
622 %A is replaced by the full name of the day of week.\n\
623 %b is replaced by the abbreviated name of the month.\n\
624 %B is replaced by the full name of the month.\n\
625 %c stands for the preferred date/time format of the C locale.\n\
626 %d is replaced by the day of month, zero-padded.\n\
627 %D is a synonym for \"%m/%d/%y\".\n\
628 %e is replaced by the day of month, blank-padded.\n\
629 %h is a synonym for \"%b\".\n\
630 %H is replaced by the hour (00-23).\n\
631 %I is replaced by the hour (00-12).\n\
632 %j is replaced by the day of the year (001-366).\n\
633 %k is replaced by the hour (0-23), blank padded.\n\
634 %l is replaced by the hour (1-12), blank padded.\n\
635 %m is replaced by the month (01-12).\n\
636 %M is replaced by the minute (00-59).\n\
637 %n is a synonym for \"\\n\".\n\
638 %p is replaced by AM or PM, as appropriate.\n\
639 %r is a synonym for \"%I:%M:%S %p\".\n\
640 %R is a synonym for \"%H:%M\".\n\
641 %S is replaced by the second (00-60).\n\
642 %t is a synonym for \"\\t\".\n\
643 %T is a synonym for \"%H:%M:%S\".\n\
644 %U is replaced by the week of the year (00-53), first day of week is Sunday.\n\
645 %w is replaced by the day of week (0-6), Sunday is day 0.\n\
646 %W is replaced by the week of the year (00-53), first day of week is Monday.\n\
647 %x is a locale-specific synonym, which defaults to \"%D\" in the C locale.\n\
648 %X is a locale-specific synonym, which defaults to \"%T\" in the C locale.\n\
649 %y is replaced by the year without century (00-99).\n\
650 %Y is replaced by the year with century.\n\
651 %Z is replaced by the time zone abbreviation.\n\
652 \n\
653 The number of options reflects the `strftime' function.")
654 (format_string, time)
655 Lisp_Object format_string, time;
656 {
657 time_t value;
658 int size;
659
660 CHECK_STRING (format_string, 1);
661
662 if (! lisp_time_argument (time, &value))
663 error ("Invalid time specification");
664
665 /* This is probably enough. */
666 size = XSTRING (format_string)->size * 6 + 50;
667
668 while (1)
669 {
670 char *buf = (char *) alloca (size);
671 *buf = 1;
672 if (emacs_strftime (buf, size, XSTRING (format_string)->data,
673 localtime (&value))
674 || !*buf)
675 return build_string (buf);
676 /* If buffer was too small, make it bigger. */
677 size *= 2;
678 }
679 }
680
681 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
682 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
683 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
684 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
685 to use the current time. The list has the following nine members:\n\
686 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
687 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
688 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
689 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
690 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
691 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
692 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
693 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
694 (specified_time)
695 Lisp_Object specified_time;
696 {
697 time_t time_spec;
698 struct tm save_tm;
699 struct tm *decoded_time;
700 Lisp_Object list_args[9];
701
702 if (! lisp_time_argument (specified_time, &time_spec))
703 error ("Invalid time specification");
704
705 decoded_time = localtime (&time_spec);
706 XSETFASTINT (list_args[0], decoded_time->tm_sec);
707 XSETFASTINT (list_args[1], decoded_time->tm_min);
708 XSETFASTINT (list_args[2], decoded_time->tm_hour);
709 XSETFASTINT (list_args[3], decoded_time->tm_mday);
710 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
711 XSETINT (list_args[5], decoded_time->tm_year + 1900);
712 XSETFASTINT (list_args[6], decoded_time->tm_wday);
713 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
714
715 /* Make a copy, in case gmtime modifies the struct. */
716 save_tm = *decoded_time;
717 decoded_time = gmtime (&time_spec);
718 if (decoded_time == 0)
719 list_args[8] = Qnil;
720 else
721 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
722 return Flist (9, list_args);
723 }
724
725 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
726 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
727 This is the reverse operation of `decode-time', which see.\n\
728 ZONE defaults to the current time zone rule. This can\n\
729 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
730 (as from `current-time-zone') or an integer (as from `decode-time')\n\
731 applied without consideration for daylight savings time.\n\
732 \n\
733 You can pass more than 7 arguments; then the first six arguments\n\
734 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
735 The intervening arguments are ignored.\n\
736 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
737 \n\
738 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
739 for example, a DAY of 0 means the day preceding the given month.\n\
740 Year numbers less than 100 are treated just like other year numbers.\n\
741 If you want them to stand for years in this century, you must do that yourself.")
742 (nargs, args)
743 int nargs;
744 register Lisp_Object *args;
745 {
746 time_t time;
747 struct tm tm;
748 Lisp_Object zone = (nargs > 6)? args[nargs - 1] : Qnil;
749
750 CHECK_NUMBER (args[0], 0); /* second */
751 CHECK_NUMBER (args[1], 1); /* minute */
752 CHECK_NUMBER (args[2], 2); /* hour */
753 CHECK_NUMBER (args[3], 3); /* day */
754 CHECK_NUMBER (args[4], 4); /* month */
755 CHECK_NUMBER (args[5], 5); /* year */
756
757 tm.tm_sec = XINT (args[0]);
758 tm.tm_min = XINT (args[1]);
759 tm.tm_hour = XINT (args[2]);
760 tm.tm_mday = XINT (args[3]);
761 tm.tm_mon = XINT (args[4]) - 1;
762 tm.tm_year = XINT (args[5]) - 1900;
763 tm.tm_isdst = -1;
764
765 if (CONSP (zone))
766 zone = Fcar (zone);
767 if (NILP (zone))
768 time = mktime (&tm);
769 else
770 {
771 char tzbuf[100];
772 char *tzstring;
773 char **oldenv = environ, **newenv;
774
775 if (zone == Qt)
776 tzstring = "UTC0";
777 else if (STRINGP (zone))
778 tzstring = (char *) XSTRING (zone)->data;
779 else if (INTEGERP (zone))
780 {
781 int abszone = abs (XINT (zone));
782 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
783 abszone / (60*60), (abszone/60) % 60, abszone % 60);
784 tzstring = tzbuf;
785 }
786 else
787 error ("Invalid time zone specification");
788
789 /* Set TZ before calling mktime; merely adjusting mktime's returned
790 value doesn't suffice, since that would mishandle leap seconds. */
791 set_time_zone_rule (tzstring);
792
793 time = mktime (&tm);
794
795 /* Restore TZ to previous value. */
796 newenv = environ;
797 environ = oldenv;
798 free (newenv);
799 #ifdef LOCALTIME_CACHE
800 tzset ();
801 #endif
802 }
803
804 if (time == (time_t) -1)
805 error ("Specified time is not representable");
806
807 return make_time (time);
808 }
809
810 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
811 "Return the current time, as a human-readable string.\n\
812 Programs can use this function to decode a time,\n\
813 since the number of columns in each field is fixed.\n\
814 The format is `Sun Sep 16 01:03:52 1973'.\n\
815 If an argument is given, it specifies a time to format\n\
816 instead of the current time. The argument should have the form:\n\
817 (HIGH . LOW)\n\
818 or the form:\n\
819 (HIGH LOW . IGNORED).\n\
820 Thus, you can use times obtained from `current-time'\n\
821 and from `file-attributes'.")
822 (specified_time)
823 Lisp_Object specified_time;
824 {
825 time_t value;
826 char buf[30];
827 register char *tem;
828
829 if (! lisp_time_argument (specified_time, &value))
830 value = -1;
831 tem = (char *) ctime (&value);
832
833 strncpy (buf, tem, 24);
834 buf[24] = 0;
835
836 return build_string (buf);
837 }
838
839 #define TM_YEAR_BASE 1900
840
841 /* Yield A - B, measured in seconds.
842 This function is copied from the GNU C Library. */
843 static int
844 tm_diff (a, b)
845 struct tm *a, *b;
846 {
847 /* Compute intervening leap days correctly even if year is negative.
848 Take care to avoid int overflow in leap day calculations,
849 but it's OK to assume that A and B are close to each other. */
850 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
851 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
852 int a100 = a4 / 25 - (a4 % 25 < 0);
853 int b100 = b4 / 25 - (b4 % 25 < 0);
854 int a400 = a100 >> 2;
855 int b400 = b100 >> 2;
856 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
857 int years = a->tm_year - b->tm_year;
858 int days = (365 * years + intervening_leap_days
859 + (a->tm_yday - b->tm_yday));
860 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
861 + (a->tm_min - b->tm_min))
862 + (a->tm_sec - b->tm_sec));
863 }
864
865 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
866 "Return the offset and name for the local time zone.\n\
867 This returns a list of the form (OFFSET NAME).\n\
868 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
869 A negative value means west of Greenwich.\n\
870 NAME is a string giving the name of the time zone.\n\
871 If an argument is given, it specifies when the time zone offset is determined\n\
872 instead of using the current time. The argument should have the form:\n\
873 (HIGH . LOW)\n\
874 or the form:\n\
875 (HIGH LOW . IGNORED).\n\
876 Thus, you can use times obtained from `current-time'\n\
877 and from `file-attributes'.\n\
878 \n\
879 Some operating systems cannot provide all this information to Emacs;\n\
880 in this case, `current-time-zone' returns a list containing nil for\n\
881 the data it can't find.")
882 (specified_time)
883 Lisp_Object specified_time;
884 {
885 time_t value;
886 struct tm *t;
887
888 if (lisp_time_argument (specified_time, &value)
889 && (t = gmtime (&value)) != 0)
890 {
891 struct tm gmt;
892 int offset;
893 char *s, buf[6];
894
895 gmt = *t; /* Make a copy, in case localtime modifies *t. */
896 t = localtime (&value);
897 offset = tm_diff (t, &gmt);
898 s = 0;
899 #ifdef HAVE_TM_ZONE
900 if (t->tm_zone)
901 s = (char *)t->tm_zone;
902 #else /* not HAVE_TM_ZONE */
903 #ifdef HAVE_TZNAME
904 if (t->tm_isdst == 0 || t->tm_isdst == 1)
905 s = tzname[t->tm_isdst];
906 #endif
907 #endif /* not HAVE_TM_ZONE */
908 if (!s)
909 {
910 /* No local time zone name is available; use "+-NNNN" instead. */
911 int am = (offset < 0 ? -offset : offset) / 60;
912 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
913 s = buf;
914 }
915 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
916 }
917 else
918 return Fmake_list (2, Qnil);
919 }
920
921 /* This holds the value of `environ' produced by the previous
922 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
923 has never been called. */
924 static char **environbuf;
925
926 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
927 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
928 If TZ is nil, use implementation-defined default time zone information.\n\
929 If TZ is t, use Universal Time.")
930 (tz)
931 Lisp_Object tz;
932 {
933 char *tzstring;
934
935 if (NILP (tz))
936 tzstring = 0;
937 else if (tz == Qt)
938 tzstring = "UTC0";
939 else
940 {
941 CHECK_STRING (tz, 0);
942 tzstring = (char *) XSTRING (tz)->data;
943 }
944
945 set_time_zone_rule (tzstring);
946 if (environbuf)
947 free (environbuf);
948 environbuf = environ;
949
950 return Qnil;
951 }
952
953 /* These two values are known to load tz files in buggy implementations.
954 Their values shouldn't matter in non-buggy implementations.
955 We don't use string literals for these strings,
956 since if a string in the environment is in readonly
957 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
958 See Sun bugs 1113095 and 1114114, ``Timezone routines
959 improperly modify environment''. */
960
961 static char set_time_zone_rule_tz1[] = "TZ=GMT0";
962 static char set_time_zone_rule_tz2[] = "TZ=GMT1";
963
964 /* Set the local time zone rule to TZSTRING.
965 This allocates memory into `environ', which it is the caller's
966 responsibility to free. */
967 void
968 set_time_zone_rule (tzstring)
969 char *tzstring;
970 {
971 int envptrs;
972 char **from, **to, **newenv;
973
974 /* Make the ENVIRON vector longer with room for TZSTRING. */
975 for (from = environ; *from; from++)
976 continue;
977 envptrs = from - environ + 2;
978 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
979 + (tzstring ? strlen (tzstring) + 4 : 0));
980
981 /* Add TZSTRING to the end of environ, as a value for TZ. */
982 if (tzstring)
983 {
984 char *t = (char *) (to + envptrs);
985 strcpy (t, "TZ=");
986 strcat (t, tzstring);
987 *to++ = t;
988 }
989
990 /* Copy the old environ vector elements into NEWENV,
991 but don't copy the TZ variable.
992 So we have only one definition of TZ, which came from TZSTRING. */
993 for (from = environ; *from; from++)
994 if (strncmp (*from, "TZ=", 3) != 0)
995 *to++ = *from;
996 *to = 0;
997
998 environ = newenv;
999
1000 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1001 the TZ variable is stored. If we do not have a TZSTRING,
1002 TO points to the vector slot which has the terminating null. */
1003
1004 #ifdef LOCALTIME_CACHE
1005 {
1006 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1007 "US/Pacific" that loads a tz file, then changes to a value like
1008 "XXX0" that does not load a tz file, and then changes back to
1009 its original value, the last change is (incorrectly) ignored.
1010 Also, if TZ changes twice in succession to values that do
1011 not load a tz file, tzset can dump core (see Sun bug#1225179).
1012 The following code works around these bugs. */
1013
1014 if (tzstring)
1015 {
1016 /* Temporarily set TZ to a value that loads a tz file
1017 and that differs from tzstring. */
1018 char *tz = *newenv;
1019 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1020 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
1021 tzset ();
1022 *newenv = tz;
1023 }
1024 else
1025 {
1026 /* The implied tzstring is unknown, so temporarily set TZ to
1027 two different values that each load a tz file. */
1028 *to = set_time_zone_rule_tz1;
1029 to[1] = 0;
1030 tzset ();
1031 *to = set_time_zone_rule_tz2;
1032 tzset ();
1033 *to = 0;
1034 }
1035
1036 /* Now TZ has the desired value, and tzset can be invoked safely. */
1037 }
1038
1039 tzset ();
1040 #endif
1041 }
1042 \f
1043 void
1044 insert1 (arg)
1045 Lisp_Object arg;
1046 {
1047 Finsert (1, &arg);
1048 }
1049
1050
1051 /* Callers passing one argument to Finsert need not gcpro the
1052 argument "array", since the only element of the array will
1053 not be used after calling insert or insert_from_string, so
1054 we don't care if it gets trashed. */
1055
1056 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1057 "Insert the arguments, either strings or characters, at point.\n\
1058 Point moves forward so that it ends up after the inserted text.\n\
1059 Any other markers at the point of insertion remain before the text.")
1060 (nargs, args)
1061 int nargs;
1062 register Lisp_Object *args;
1063 {
1064 register int argnum;
1065 register Lisp_Object tem;
1066 char str[1];
1067
1068 for (argnum = 0; argnum < nargs; argnum++)
1069 {
1070 tem = args[argnum];
1071 retry:
1072 if (INTEGERP (tem))
1073 {
1074 str[0] = XINT (tem);
1075 insert (str, 1);
1076 }
1077 else if (STRINGP (tem))
1078 {
1079 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
1080 }
1081 else
1082 {
1083 tem = wrong_type_argument (Qchar_or_string_p, tem);
1084 goto retry;
1085 }
1086 }
1087
1088 return Qnil;
1089 }
1090
1091 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
1092 0, MANY, 0,
1093 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1094 Point moves forward so that it ends up after the inserted text.\n\
1095 Any other markers at the point of insertion remain before the text.")
1096 (nargs, args)
1097 int nargs;
1098 register Lisp_Object *args;
1099 {
1100 register int argnum;
1101 register Lisp_Object tem;
1102 char str[1];
1103
1104 for (argnum = 0; argnum < nargs; argnum++)
1105 {
1106 tem = args[argnum];
1107 retry:
1108 if (INTEGERP (tem))
1109 {
1110 str[0] = XINT (tem);
1111 insert_and_inherit (str, 1);
1112 }
1113 else if (STRINGP (tem))
1114 {
1115 insert_from_string (tem, 0, XSTRING (tem)->size, 1);
1116 }
1117 else
1118 {
1119 tem = wrong_type_argument (Qchar_or_string_p, tem);
1120 goto retry;
1121 }
1122 }
1123
1124 return Qnil;
1125 }
1126
1127 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
1128 "Insert strings or characters at point, relocating markers after the text.\n\
1129 Point moves forward so that it ends up after the inserted text.\n\
1130 Any other markers at the point of insertion also end up after the text.")
1131 (nargs, args)
1132 int nargs;
1133 register Lisp_Object *args;
1134 {
1135 register int argnum;
1136 register Lisp_Object tem;
1137 char str[1];
1138
1139 for (argnum = 0; argnum < nargs; argnum++)
1140 {
1141 tem = args[argnum];
1142 retry:
1143 if (INTEGERP (tem))
1144 {
1145 str[0] = XINT (tem);
1146 insert_before_markers (str, 1);
1147 }
1148 else if (STRINGP (tem))
1149 {
1150 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size, 0);
1151 }
1152 else
1153 {
1154 tem = wrong_type_argument (Qchar_or_string_p, tem);
1155 goto retry;
1156 }
1157 }
1158
1159 return Qnil;
1160 }
1161
1162 DEFUN ("insert-before-markers-and-inherit",
1163 Finsert_and_inherit_before_markers, Sinsert_and_inherit_before_markers,
1164 0, MANY, 0,
1165 "Insert text at point, relocating markers and inheriting properties.\n\
1166 Point moves forward so that it ends up after the inserted text.\n\
1167 Any other markers at the point of insertion also end up after the text.")
1168 (nargs, args)
1169 int nargs;
1170 register Lisp_Object *args;
1171 {
1172 register int argnum;
1173 register Lisp_Object tem;
1174 char str[1];
1175
1176 for (argnum = 0; argnum < nargs; argnum++)
1177 {
1178 tem = args[argnum];
1179 retry:
1180 if (INTEGERP (tem))
1181 {
1182 str[0] = XINT (tem);
1183 insert_before_markers_and_inherit (str, 1);
1184 }
1185 else if (STRINGP (tem))
1186 {
1187 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size, 1);
1188 }
1189 else
1190 {
1191 tem = wrong_type_argument (Qchar_or_string_p, tem);
1192 goto retry;
1193 }
1194 }
1195
1196 return Qnil;
1197 }
1198 \f
1199 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
1200 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1201 Point and all markers are affected as in the function `insert'.\n\
1202 Both arguments are required.\n\
1203 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1204 from adjoining text, if those properties are sticky.")
1205 (character, count, inherit)
1206 Lisp_Object character, count, inherit;
1207 {
1208 register unsigned char *string;
1209 register int strlen;
1210 register int i, n;
1211
1212 CHECK_NUMBER (character, 0);
1213 CHECK_NUMBER (count, 1);
1214
1215 n = XINT (count);
1216 if (n <= 0)
1217 return Qnil;
1218 strlen = min (n, 256);
1219 string = (unsigned char *) alloca (strlen);
1220 for (i = 0; i < strlen; i++)
1221 string[i] = XFASTINT (character);
1222 while (n >= strlen)
1223 {
1224 if (!NILP (inherit))
1225 insert_and_inherit (string, strlen);
1226 else
1227 insert (string, strlen);
1228 n -= strlen;
1229 }
1230 if (n > 0)
1231 {
1232 if (!NILP (inherit))
1233 insert_and_inherit (string, n);
1234 else
1235 insert (string, n);
1236 }
1237 return Qnil;
1238 }
1239
1240 \f
1241 /* Making strings from buffer contents. */
1242
1243 /* Return a Lisp_String containing the text of the current buffer from
1244 START to END. If text properties are in use and the current buffer
1245 has properties in the range specified, the resulting string will also
1246 have them, if PROPS is nonzero.
1247
1248 We don't want to use plain old make_string here, because it calls
1249 make_uninit_string, which can cause the buffer arena to be
1250 compacted. make_string has no way of knowing that the data has
1251 been moved, and thus copies the wrong data into the string. This
1252 doesn't effect most of the other users of make_string, so it should
1253 be left as is. But we should use this function when conjuring
1254 buffer substrings. */
1255
1256 Lisp_Object
1257 make_buffer_string (start, end, props)
1258 int start, end;
1259 int props;
1260 {
1261 Lisp_Object result, tem, tem1;
1262
1263 if (start < GPT && GPT < end)
1264 move_gap (start);
1265
1266 result = make_uninit_string (end - start);
1267 bcopy (&FETCH_CHAR (start), XSTRING (result)->data, end - start);
1268
1269 /* If desired, update and copy the text properties. */
1270 #ifdef USE_TEXT_PROPERTIES
1271 if (props)
1272 {
1273 update_buffer_properties (start, end);
1274
1275 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1276 tem1 = Ftext_properties_at (make_number (start), Qnil);
1277
1278 if (XINT (tem) != end || !NILP (tem1))
1279 copy_intervals_to_string (result, current_buffer, start, end - start);
1280 }
1281 #endif
1282
1283 return result;
1284 }
1285
1286 /* Call Vbuffer_access_fontify_functions for the range START ... END
1287 in the current buffer, if necessary. */
1288
1289 static void
1290 update_buffer_properties (start, end)
1291 int start, end;
1292 {
1293 #ifdef USE_TEXT_PROPERTIES
1294 /* If this buffer has some access functions,
1295 call them, specifying the range of the buffer being accessed. */
1296 if (!NILP (Vbuffer_access_fontify_functions))
1297 {
1298 Lisp_Object args[3];
1299 Lisp_Object tem;
1300
1301 args[0] = Qbuffer_access_fontify_functions;
1302 XSETINT (args[1], start);
1303 XSETINT (args[2], end);
1304
1305 /* But don't call them if we can tell that the work
1306 has already been done. */
1307 if (!NILP (Vbuffer_access_fontified_property))
1308 {
1309 tem = Ftext_property_any (args[1], args[2],
1310 Vbuffer_access_fontified_property,
1311 Qnil, Qnil);
1312 if (! NILP (tem))
1313 Frun_hook_with_args (3, args);
1314 }
1315 else
1316 Frun_hook_with_args (3, args);
1317 }
1318 #endif
1319 }
1320
1321 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
1322 "Return the contents of part of the current buffer as a string.\n\
1323 The two arguments START and END are character positions;\n\
1324 they can be in either order.")
1325 (start, end)
1326 Lisp_Object start, end;
1327 {
1328 register int b, e;
1329
1330 validate_region (&start, &end);
1331 b = XINT (start);
1332 e = XINT (end);
1333
1334 return make_buffer_string (b, e, 1);
1335 }
1336
1337 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
1338 Sbuffer_substring_no_properties, 2, 2, 0,
1339 "Return the characters of part of the buffer, without the text properties.\n\
1340 The two arguments START and END are character positions;\n\
1341 they can be in either order.")
1342 (start, end)
1343 Lisp_Object start, end;
1344 {
1345 register int b, e;
1346
1347 validate_region (&start, &end);
1348 b = XINT (start);
1349 e = XINT (end);
1350
1351 return make_buffer_string (b, e, 0);
1352 }
1353
1354 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
1355 "Return the contents of the current buffer as a string.\n\
1356 If narrowing is in effect, this function returns only the visible part\n\
1357 of the buffer.")
1358 ()
1359 {
1360 return make_buffer_string (BEGV, ZV, 1);
1361 }
1362
1363 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
1364 1, 3, 0,
1365 "Insert before point a substring of the contents of buffer BUFFER.\n\
1366 BUFFER may be a buffer or a buffer name.\n\
1367 Arguments START and END are character numbers specifying the substring.\n\
1368 They default to the beginning and the end of BUFFER.")
1369 (buf, start, end)
1370 Lisp_Object buf, start, end;
1371 {
1372 register int b, e, temp;
1373 register struct buffer *bp, *obuf;
1374 Lisp_Object buffer;
1375
1376 buffer = Fget_buffer (buf);
1377 if (NILP (buffer))
1378 nsberror (buf);
1379 bp = XBUFFER (buffer);
1380 if (NILP (bp->name))
1381 error ("Selecting deleted buffer");
1382
1383 if (NILP (start))
1384 b = BUF_BEGV (bp);
1385 else
1386 {
1387 CHECK_NUMBER_COERCE_MARKER (start, 0);
1388 b = XINT (start);
1389 }
1390 if (NILP (end))
1391 e = BUF_ZV (bp);
1392 else
1393 {
1394 CHECK_NUMBER_COERCE_MARKER (end, 1);
1395 e = XINT (end);
1396 }
1397
1398 if (b > e)
1399 temp = b, b = e, e = temp;
1400
1401 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
1402 args_out_of_range (start, end);
1403
1404 obuf = current_buffer;
1405 set_buffer_internal_1 (bp);
1406 update_buffer_properties (b, e);
1407 set_buffer_internal_1 (obuf);
1408
1409 insert_from_buffer (bp, b, e - b, 0);
1410 return Qnil;
1411 }
1412
1413 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
1414 6, 6, 0,
1415 "Compare two substrings of two buffers; return result as number.\n\
1416 the value is -N if first string is less after N-1 chars,\n\
1417 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1418 Each substring is represented as three arguments: BUFFER, START and END.\n\
1419 That makes six args in all, three for each substring.\n\n\
1420 The value of `case-fold-search' in the current buffer\n\
1421 determines whether case is significant or ignored.")
1422 (buffer1, start1, end1, buffer2, start2, end2)
1423 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
1424 {
1425 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
1426 register struct buffer *bp1, *bp2;
1427 register Lisp_Object *trt
1428 = (!NILP (current_buffer->case_fold_search)
1429 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
1430
1431 /* Find the first buffer and its substring. */
1432
1433 if (NILP (buffer1))
1434 bp1 = current_buffer;
1435 else
1436 {
1437 Lisp_Object buf1;
1438 buf1 = Fget_buffer (buffer1);
1439 if (NILP (buf1))
1440 nsberror (buffer1);
1441 bp1 = XBUFFER (buf1);
1442 if (NILP (bp1->name))
1443 error ("Selecting deleted buffer");
1444 }
1445
1446 if (NILP (start1))
1447 begp1 = BUF_BEGV (bp1);
1448 else
1449 {
1450 CHECK_NUMBER_COERCE_MARKER (start1, 1);
1451 begp1 = XINT (start1);
1452 }
1453 if (NILP (end1))
1454 endp1 = BUF_ZV (bp1);
1455 else
1456 {
1457 CHECK_NUMBER_COERCE_MARKER (end1, 2);
1458 endp1 = XINT (end1);
1459 }
1460
1461 if (begp1 > endp1)
1462 temp = begp1, begp1 = endp1, endp1 = temp;
1463
1464 if (!(BUF_BEGV (bp1) <= begp1
1465 && begp1 <= endp1
1466 && endp1 <= BUF_ZV (bp1)))
1467 args_out_of_range (start1, end1);
1468
1469 /* Likewise for second substring. */
1470
1471 if (NILP (buffer2))
1472 bp2 = current_buffer;
1473 else
1474 {
1475 Lisp_Object buf2;
1476 buf2 = Fget_buffer (buffer2);
1477 if (NILP (buf2))
1478 nsberror (buffer2);
1479 bp2 = XBUFFER (buf2);
1480 if (NILP (bp2->name))
1481 error ("Selecting deleted buffer");
1482 }
1483
1484 if (NILP (start2))
1485 begp2 = BUF_BEGV (bp2);
1486 else
1487 {
1488 CHECK_NUMBER_COERCE_MARKER (start2, 4);
1489 begp2 = XINT (start2);
1490 }
1491 if (NILP (end2))
1492 endp2 = BUF_ZV (bp2);
1493 else
1494 {
1495 CHECK_NUMBER_COERCE_MARKER (end2, 5);
1496 endp2 = XINT (end2);
1497 }
1498
1499 if (begp2 > endp2)
1500 temp = begp2, begp2 = endp2, endp2 = temp;
1501
1502 if (!(BUF_BEGV (bp2) <= begp2
1503 && begp2 <= endp2
1504 && endp2 <= BUF_ZV (bp2)))
1505 args_out_of_range (start2, end2);
1506
1507 len1 = endp1 - begp1;
1508 len2 = endp2 - begp2;
1509 length = len1;
1510 if (len2 < length)
1511 length = len2;
1512
1513 for (i = 0; i < length; i++)
1514 {
1515 int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
1516 int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
1517 if (trt)
1518 {
1519 c1 = trt[c1];
1520 c2 = trt[c2];
1521 }
1522 if (c1 < c2)
1523 return make_number (- 1 - i);
1524 if (c1 > c2)
1525 return make_number (i + 1);
1526 }
1527
1528 /* The strings match as far as they go.
1529 If one is shorter, that one is less. */
1530 if (length < len1)
1531 return make_number (length + 1);
1532 else if (length < len2)
1533 return make_number (- length - 1);
1534
1535 /* Same length too => they are equal. */
1536 return make_number (0);
1537 }
1538 \f
1539 static Lisp_Object
1540 subst_char_in_region_unwind (arg)
1541 Lisp_Object arg;
1542 {
1543 return current_buffer->undo_list = arg;
1544 }
1545
1546 static Lisp_Object
1547 subst_char_in_region_unwind_1 (arg)
1548 Lisp_Object arg;
1549 {
1550 return current_buffer->filename = arg;
1551 }
1552
1553 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1554 Ssubst_char_in_region, 4, 5, 0,
1555 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1556 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1557 and don't mark the buffer as really changed.")
1558 (start, end, fromchar, tochar, noundo)
1559 Lisp_Object start, end, fromchar, tochar, noundo;
1560 {
1561 register int pos, stop, look;
1562 int changed = 0;
1563 int count = specpdl_ptr - specpdl;
1564
1565 validate_region (&start, &end);
1566 CHECK_NUMBER (fromchar, 2);
1567 CHECK_NUMBER (tochar, 3);
1568
1569 pos = XINT (start);
1570 stop = XINT (end);
1571 look = XINT (fromchar);
1572
1573 /* If we don't want undo, turn off putting stuff on the list.
1574 That's faster than getting rid of things,
1575 and it prevents even the entry for a first change.
1576 Also inhibit locking the file. */
1577 if (!NILP (noundo))
1578 {
1579 record_unwind_protect (subst_char_in_region_unwind,
1580 current_buffer->undo_list);
1581 current_buffer->undo_list = Qt;
1582 /* Don't do file-locking. */
1583 record_unwind_protect (subst_char_in_region_unwind_1,
1584 current_buffer->filename);
1585 current_buffer->filename = Qnil;
1586 }
1587
1588 while (pos < stop)
1589 {
1590 if (FETCH_CHAR (pos) == look)
1591 {
1592 if (! changed)
1593 {
1594 modify_region (current_buffer, XINT (start), stop);
1595
1596 if (! NILP (noundo))
1597 {
1598 if (MODIFF - 1 == SAVE_MODIFF)
1599 SAVE_MODIFF++;
1600 if (MODIFF - 1 == current_buffer->auto_save_modified)
1601 current_buffer->auto_save_modified++;
1602 }
1603
1604 changed = 1;
1605 }
1606
1607 if (NILP (noundo))
1608 record_change (pos, 1);
1609 FETCH_CHAR (pos) = XINT (tochar);
1610 }
1611 pos++;
1612 }
1613
1614 if (changed)
1615 signal_after_change (XINT (start),
1616 stop - XINT (start), stop - XINT (start));
1617
1618 unbind_to (count, Qnil);
1619 return Qnil;
1620 }
1621
1622 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
1623 "From START to END, translate characters according to TABLE.\n\
1624 TABLE is a string; the Nth character in it is the mapping\n\
1625 for the character with code N. Returns the number of characters changed.")
1626 (start, end, table)
1627 Lisp_Object start;
1628 Lisp_Object end;
1629 register Lisp_Object table;
1630 {
1631 register int pos, stop; /* Limits of the region. */
1632 register unsigned char *tt; /* Trans table. */
1633 register int oc; /* Old character. */
1634 register int nc; /* New character. */
1635 int cnt; /* Number of changes made. */
1636 Lisp_Object z; /* Return. */
1637 int size; /* Size of translate table. */
1638
1639 validate_region (&start, &end);
1640 CHECK_STRING (table, 2);
1641
1642 size = XSTRING (table)->size;
1643 tt = XSTRING (table)->data;
1644
1645 pos = XINT (start);
1646 stop = XINT (end);
1647 modify_region (current_buffer, pos, stop);
1648
1649 cnt = 0;
1650 for (; pos < stop; ++pos)
1651 {
1652 oc = FETCH_CHAR (pos);
1653 if (oc < size)
1654 {
1655 nc = tt[oc];
1656 if (nc != oc)
1657 {
1658 record_change (pos, 1);
1659 FETCH_CHAR (pos) = nc;
1660 signal_after_change (pos, 1, 1);
1661 ++cnt;
1662 }
1663 }
1664 }
1665
1666 XSETFASTINT (z, cnt);
1667 return (z);
1668 }
1669
1670 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
1671 "Delete the text between point and mark.\n\
1672 When called from a program, expects two arguments,\n\
1673 positions (integers or markers) specifying the stretch to be deleted.")
1674 (start, end)
1675 Lisp_Object start, end;
1676 {
1677 validate_region (&start, &end);
1678 del_range (XINT (start), XINT (end));
1679 return Qnil;
1680 }
1681 \f
1682 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
1683 "Remove restrictions (narrowing) from current buffer.\n\
1684 This allows the buffer's full text to be seen and edited.")
1685 ()
1686 {
1687 BEGV = BEG;
1688 SET_BUF_ZV (current_buffer, Z);
1689 current_buffer->clip_changed = 1;
1690 /* Changing the buffer bounds invalidates any recorded current column. */
1691 invalidate_current_column ();
1692 return Qnil;
1693 }
1694
1695 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
1696 "Restrict editing in this buffer to the current region.\n\
1697 The rest of the text becomes temporarily invisible and untouchable\n\
1698 but is not deleted; if you save the buffer in a file, the invisible\n\
1699 text is included in the file. \\[widen] makes all visible again.\n\
1700 See also `save-restriction'.\n\
1701 \n\
1702 When calling from a program, pass two arguments; positions (integers\n\
1703 or markers) bounding the text that should remain visible.")
1704 (start, end)
1705 register Lisp_Object start, end;
1706 {
1707 CHECK_NUMBER_COERCE_MARKER (start, 0);
1708 CHECK_NUMBER_COERCE_MARKER (end, 1);
1709
1710 if (XINT (start) > XINT (end))
1711 {
1712 Lisp_Object tem;
1713 tem = start; start = end; end = tem;
1714 }
1715
1716 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
1717 args_out_of_range (start, end);
1718
1719 BEGV = XFASTINT (start);
1720 SET_BUF_ZV (current_buffer, XFASTINT (end));
1721 if (PT < XFASTINT (start))
1722 SET_PT (XFASTINT (start));
1723 if (PT > XFASTINT (end))
1724 SET_PT (XFASTINT (end));
1725 current_buffer->clip_changed = 1;
1726 /* Changing the buffer bounds invalidates any recorded current column. */
1727 invalidate_current_column ();
1728 return Qnil;
1729 }
1730
1731 Lisp_Object
1732 save_restriction_save ()
1733 {
1734 register Lisp_Object bottom, top;
1735 /* Note: I tried using markers here, but it does not win
1736 because insertion at the end of the saved region
1737 does not advance mh and is considered "outside" the saved region. */
1738 XSETFASTINT (bottom, BEGV - BEG);
1739 XSETFASTINT (top, Z - ZV);
1740
1741 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
1742 }
1743
1744 Lisp_Object
1745 save_restriction_restore (data)
1746 Lisp_Object data;
1747 {
1748 register struct buffer *buf;
1749 register int newhead, newtail;
1750 register Lisp_Object tem;
1751
1752 buf = XBUFFER (XCONS (data)->car);
1753
1754 data = XCONS (data)->cdr;
1755
1756 tem = XCONS (data)->car;
1757 newhead = XINT (tem);
1758 tem = XCONS (data)->cdr;
1759 newtail = XINT (tem);
1760 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
1761 {
1762 newhead = 0;
1763 newtail = 0;
1764 }
1765 BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
1766 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
1767 current_buffer->clip_changed = 1;
1768
1769 /* If point is outside the new visible range, move it inside. */
1770 SET_BUF_PT (buf,
1771 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
1772
1773 return Qnil;
1774 }
1775
1776 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
1777 "Execute BODY, saving and restoring current buffer's restrictions.\n\
1778 The buffer's restrictions make parts of the beginning and end invisible.\n\
1779 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
1780 This special form, `save-restriction', saves the current buffer's restrictions\n\
1781 when it is entered, and restores them when it is exited.\n\
1782 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
1783 The old restrictions settings are restored\n\
1784 even in case of abnormal exit (throw or error).\n\
1785 \n\
1786 The value returned is the value of the last form in BODY.\n\
1787 \n\
1788 `save-restriction' can get confused if, within the BODY, you widen\n\
1789 and then make changes outside the area within the saved restrictions.\n\
1790 \n\
1791 Note: if you are using both `save-excursion' and `save-restriction',\n\
1792 use `save-excursion' outermost:\n\
1793 (save-excursion (save-restriction ...))")
1794 (body)
1795 Lisp_Object body;
1796 {
1797 register Lisp_Object val;
1798 int count = specpdl_ptr - specpdl;
1799
1800 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1801 val = Fprogn (body);
1802 return unbind_to (count, val);
1803 }
1804 \f
1805 /* Buffer for the most recent text displayed by Fmessage. */
1806 static char *message_text;
1807
1808 /* Allocated length of that buffer. */
1809 static int message_length;
1810
1811 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
1812 "Print a one-line message at the bottom of the screen.\n\
1813 The first argument is a format control string, and the rest are data\n\
1814 to be formatted under control of the string. See `format' for details.\n\
1815 \n\
1816 If the first argument is nil, clear any existing message; let the\n\
1817 minibuffer contents show.")
1818 (nargs, args)
1819 int nargs;
1820 Lisp_Object *args;
1821 {
1822 if (NILP (args[0]))
1823 {
1824 message (0);
1825 return Qnil;
1826 }
1827 else
1828 {
1829 register Lisp_Object val;
1830 val = Fformat (nargs, args);
1831 /* Copy the data so that it won't move when we GC. */
1832 if (! message_text)
1833 {
1834 message_text = (char *)xmalloc (80);
1835 message_length = 80;
1836 }
1837 if (XSTRING (val)->size > message_length)
1838 {
1839 message_length = XSTRING (val)->size;
1840 message_text = (char *)xrealloc (message_text, message_length);
1841 }
1842 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
1843 message2 (message_text, XSTRING (val)->size);
1844 return val;
1845 }
1846 }
1847
1848 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
1849 "Display a message, in a dialog box if possible.\n\
1850 If a dialog box is not available, use the echo area.\n\
1851 The first argument is a format control string, and the rest are data\n\
1852 to be formatted under control of the string. See `format' for details.\n\
1853 \n\
1854 If the first argument is nil, clear any existing message; let the\n\
1855 minibuffer contents show.")
1856 (nargs, args)
1857 int nargs;
1858 Lisp_Object *args;
1859 {
1860 if (NILP (args[0]))
1861 {
1862 message (0);
1863 return Qnil;
1864 }
1865 else
1866 {
1867 register Lisp_Object val;
1868 val = Fformat (nargs, args);
1869 #ifdef HAVE_MENUS
1870 {
1871 Lisp_Object pane, menu, obj;
1872 struct gcpro gcpro1;
1873 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
1874 GCPRO1 (pane);
1875 menu = Fcons (val, pane);
1876 obj = Fx_popup_dialog (Qt, menu);
1877 UNGCPRO;
1878 return val;
1879 }
1880 #else /* not HAVE_MENUS */
1881 /* Copy the data so that it won't move when we GC. */
1882 if (! message_text)
1883 {
1884 message_text = (char *)xmalloc (80);
1885 message_length = 80;
1886 }
1887 if (XSTRING (val)->size > message_length)
1888 {
1889 message_length = XSTRING (val)->size;
1890 message_text = (char *)xrealloc (message_text, message_length);
1891 }
1892 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
1893 message2 (message_text, XSTRING (val)->size);
1894 return val;
1895 #endif /* not HAVE_MENUS */
1896 }
1897 }
1898 #ifdef HAVE_MENUS
1899 extern Lisp_Object last_nonmenu_event;
1900 #endif
1901
1902 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
1903 "Display a message in a dialog box or in the echo area.\n\
1904 If this command was invoked with the mouse, use a dialog box.\n\
1905 Otherwise, use the echo area.\n\
1906 The first argument is a format control string, and the rest are data\n\
1907 to be formatted under control of the string. See `format' for details.\n\
1908 \n\
1909 If the first argument is nil, clear any existing message; let the\n\
1910 minibuffer contents show.")
1911 (nargs, args)
1912 int nargs;
1913 Lisp_Object *args;
1914 {
1915 #ifdef HAVE_MENUS
1916 if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
1917 return Fmessage_box (nargs, args);
1918 #endif
1919 return Fmessage (nargs, args);
1920 }
1921
1922 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
1923 "Format a string out of a control-string and arguments.\n\
1924 The first argument is a control string.\n\
1925 The other arguments are substituted into it to make the result, a string.\n\
1926 It may contain %-sequences meaning to substitute the next argument.\n\
1927 %s means print a string argument. Actually, prints any object, with `princ'.\n\
1928 %d means print as number in decimal (%o octal, %x hex).\n\
1929 %e means print a number in exponential notation.\n\
1930 %f means print a number in decimal-point notation.\n\
1931 %g means print a number in exponential notation\n\
1932 or decimal-point notation, whichever uses fewer characters.\n\
1933 %c means print a number as a single character.\n\
1934 %S means print any object as an s-expression (using prin1).\n\
1935 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
1936 Use %% to put a single % into the output.")
1937 (nargs, args)
1938 int nargs;
1939 register Lisp_Object *args;
1940 {
1941 register int n; /* The number of the next arg to substitute */
1942 register int total = 5; /* An estimate of the final length */
1943 char *buf;
1944 register unsigned char *format, *end;
1945 int length;
1946 extern char *index ();
1947 /* It should not be necessary to GCPRO ARGS, because
1948 the caller in the interpreter should take care of that. */
1949
1950 CHECK_STRING (args[0], 0);
1951 format = XSTRING (args[0])->data;
1952 end = format + XSTRING (args[0])->size;
1953
1954 n = 0;
1955 while (format != end)
1956 if (*format++ == '%')
1957 {
1958 int minlen;
1959
1960 /* Process a numeric arg and skip it. */
1961 minlen = atoi (format);
1962 if (minlen < 0)
1963 minlen = - minlen;
1964
1965 while ((*format >= '0' && *format <= '9')
1966 || *format == '-' || *format == ' ' || *format == '.')
1967 format++;
1968
1969 if (*format == '%')
1970 format++;
1971 else if (++n >= nargs)
1972 error ("Not enough arguments for format string");
1973 else if (*format == 'S')
1974 {
1975 /* For `S', prin1 the argument and then treat like a string. */
1976 register Lisp_Object tem;
1977 tem = Fprin1_to_string (args[n], Qnil);
1978 args[n] = tem;
1979 goto string;
1980 }
1981 else if (SYMBOLP (args[n]))
1982 {
1983 XSETSTRING (args[n], XSYMBOL (args[n])->name);
1984 goto string;
1985 }
1986 else if (STRINGP (args[n]))
1987 {
1988 string:
1989 if (*format != 's' && *format != 'S')
1990 error ("format specifier doesn't match argument type");
1991 total += XSTRING (args[n])->size;
1992 /* We have to put an arbitrary limit on minlen
1993 since otherwise it could make alloca fail. */
1994 if (minlen < XSTRING (args[n])->size + 1000)
1995 total += minlen;
1996 }
1997 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
1998 else if (INTEGERP (args[n]) && *format != 's')
1999 {
2000 #ifdef LISP_FLOAT_TYPE
2001 /* The following loop assumes the Lisp type indicates
2002 the proper way to pass the argument.
2003 So make sure we have a flonum if the argument should
2004 be a double. */
2005 if (*format == 'e' || *format == 'f' || *format == 'g')
2006 args[n] = Ffloat (args[n]);
2007 #endif
2008 total += 30;
2009 /* We have to put an arbitrary limit on minlen
2010 since otherwise it could make alloca fail. */
2011 if (minlen < 1000)
2012 total += minlen;
2013 }
2014 #ifdef LISP_FLOAT_TYPE
2015 else if (FLOATP (args[n]) && *format != 's')
2016 {
2017 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
2018 args[n] = Ftruncate (args[n]);
2019 total += 30;
2020 /* We have to put an arbitrary limit on minlen
2021 since otherwise it could make alloca fail. */
2022 if (minlen < 1000)
2023 total += minlen;
2024 }
2025 #endif
2026 else
2027 {
2028 /* Anything but a string, convert to a string using princ. */
2029 register Lisp_Object tem;
2030 tem = Fprin1_to_string (args[n], Qt);
2031 args[n] = tem;
2032 goto string;
2033 }
2034 }
2035
2036 {
2037 register int nstrings = n + 1;
2038
2039 /* Allocate twice as many strings as we have %-escapes; floats occupy
2040 two slots, and we're not sure how many of those we have. */
2041 register unsigned char **strings
2042 = (unsigned char **) alloca (2 * nstrings * sizeof (unsigned char *));
2043 int i;
2044
2045 i = 0;
2046 for (n = 0; n < nstrings; n++)
2047 {
2048 if (n >= nargs)
2049 strings[i++] = (unsigned char *) "";
2050 else if (INTEGERP (args[n]))
2051 /* We checked above that the corresponding format effector
2052 isn't %s, which would cause MPV. */
2053 strings[i++] = (unsigned char *) XINT (args[n]);
2054 #ifdef LISP_FLOAT_TYPE
2055 else if (FLOATP (args[n]))
2056 {
2057 union { double d; char *half[2]; } u;
2058
2059 u.d = XFLOAT (args[n])->data;
2060 strings[i++] = (unsigned char *) u.half[0];
2061 strings[i++] = (unsigned char *) u.half[1];
2062 }
2063 #endif
2064 else if (i == 0)
2065 /* The first string is treated differently
2066 because it is the format string. */
2067 strings[i++] = XSTRING (args[n])->data;
2068 else
2069 strings[i++] = (unsigned char *) XSTRING (args[n]);
2070 }
2071
2072 /* Make room in result for all the non-%-codes in the control string. */
2073 total += XSTRING (args[0])->size;
2074
2075 /* Format it in bigger and bigger buf's until it all fits. */
2076 while (1)
2077 {
2078 buf = (char *) alloca (total + 1);
2079 buf[total - 1] = 0;
2080
2081 length = doprnt_lisp (buf, total + 1, strings[0],
2082 end, i-1, strings + 1);
2083 if (buf[total - 1] == 0)
2084 break;
2085
2086 total *= 2;
2087 }
2088 }
2089
2090 /* UNGCPRO; */
2091 return make_string (buf, length);
2092 }
2093
2094 /* VARARGS 1 */
2095 Lisp_Object
2096 #ifdef NO_ARG_ARRAY
2097 format1 (string1, arg0, arg1, arg2, arg3, arg4)
2098 EMACS_INT arg0, arg1, arg2, arg3, arg4;
2099 #else
2100 format1 (string1)
2101 #endif
2102 char *string1;
2103 {
2104 char buf[100];
2105 #ifdef NO_ARG_ARRAY
2106 EMACS_INT args[5];
2107 args[0] = arg0;
2108 args[1] = arg1;
2109 args[2] = arg2;
2110 args[3] = arg3;
2111 args[4] = arg4;
2112 doprnt (buf, sizeof buf, string1, (char *)0, 5, args);
2113 #else
2114 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
2115 #endif
2116 return build_string (buf);
2117 }
2118 \f
2119 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
2120 "Return t if two characters match, optionally ignoring case.\n\
2121 Both arguments must be characters (i.e. integers).\n\
2122 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2123 (c1, c2)
2124 register Lisp_Object c1, c2;
2125 {
2126 Lisp_Object *downcase = DOWNCASE_TABLE;
2127 CHECK_NUMBER (c1, 0);
2128 CHECK_NUMBER (c2, 1);
2129
2130 if (!NILP (current_buffer->case_fold_search)
2131 ? ((XINT (downcase[0xff & XFASTINT (c1)])
2132 == XINT (downcase[0xff & XFASTINT (c2)]))
2133 && (XFASTINT (c1) & ~0xff) == (XFASTINT (c2) & ~0xff))
2134 : XINT (c1) == XINT (c2))
2135 return Qt;
2136 return Qnil;
2137 }
2138 \f
2139 /* Transpose the markers in two regions of the current buffer, and
2140 adjust the ones between them if necessary (i.e.: if the regions
2141 differ in size).
2142
2143 Traverses the entire marker list of the buffer to do so, adding an
2144 appropriate amount to some, subtracting from some, and leaving the
2145 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2146
2147 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2148
2149 void
2150 transpose_markers (start1, end1, start2, end2)
2151 register int start1, end1, start2, end2;
2152 {
2153 register int amt1, amt2, diff, mpos;
2154 register Lisp_Object marker;
2155
2156 /* Update point as if it were a marker. */
2157 if (PT < start1)
2158 ;
2159 else if (PT < end1)
2160 TEMP_SET_PT (PT + (end2 - end1));
2161 else if (PT < start2)
2162 TEMP_SET_PT (PT + (end2 - start2) - (end1 - start1));
2163 else if (PT < end2)
2164 TEMP_SET_PT (PT - (start2 - start1));
2165
2166 /* We used to adjust the endpoints here to account for the gap, but that
2167 isn't good enough. Even if we assume the caller has tried to move the
2168 gap out of our way, it might still be at start1 exactly, for example;
2169 and that places it `inside' the interval, for our purposes. The amount
2170 of adjustment is nontrivial if there's a `denormalized' marker whose
2171 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2172 the dirty work to Fmarker_position, below. */
2173
2174 /* The difference between the region's lengths */
2175 diff = (end2 - start2) - (end1 - start1);
2176
2177 /* For shifting each marker in a region by the length of the other
2178 * region plus the distance between the regions.
2179 */
2180 amt1 = (end2 - start2) + (start2 - end1);
2181 amt2 = (end1 - start1) + (start2 - end1);
2182
2183 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
2184 marker = XMARKER (marker)->chain)
2185 {
2186 mpos = Fmarker_position (marker);
2187 if (mpos >= start1 && mpos < end2)
2188 {
2189 if (mpos < end1)
2190 mpos += amt1;
2191 else if (mpos < start2)
2192 mpos += diff;
2193 else
2194 mpos -= amt2;
2195 if (mpos > GPT) mpos += GAP_SIZE;
2196 XMARKER (marker)->bufpos = mpos;
2197 }
2198 }
2199 }
2200
2201 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
2202 "Transpose region START1 to END1 with START2 to END2.\n\
2203 The regions may not be overlapping, because the size of the buffer is\n\
2204 never changed in a transposition.\n\
2205 \n\
2206 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2207 any markers that happen to be located in the regions.\n\
2208 \n\
2209 Transposing beyond buffer boundaries is an error.")
2210 (startr1, endr1, startr2, endr2, leave_markers)
2211 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
2212 {
2213 register int start1, end1, start2, end2,
2214 gap, len1, len_mid, len2;
2215 unsigned char *start1_addr, *start2_addr, *temp;
2216
2217 #ifdef USE_TEXT_PROPERTIES
2218 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
2219 cur_intv = BUF_INTERVALS (current_buffer);
2220 #endif /* USE_TEXT_PROPERTIES */
2221
2222 validate_region (&startr1, &endr1);
2223 validate_region (&startr2, &endr2);
2224
2225 start1 = XFASTINT (startr1);
2226 end1 = XFASTINT (endr1);
2227 start2 = XFASTINT (startr2);
2228 end2 = XFASTINT (endr2);
2229 gap = GPT;
2230
2231 /* Swap the regions if they're reversed. */
2232 if (start2 < end1)
2233 {
2234 register int glumph = start1;
2235 start1 = start2;
2236 start2 = glumph;
2237 glumph = end1;
2238 end1 = end2;
2239 end2 = glumph;
2240 }
2241
2242 len1 = end1 - start1;
2243 len2 = end2 - start2;
2244
2245 if (start2 < end1)
2246 error ("transposed regions not properly ordered");
2247 else if (start1 == end1 || start2 == end2)
2248 error ("transposed region may not be of length 0");
2249
2250 /* The possibilities are:
2251 1. Adjacent (contiguous) regions, or separate but equal regions
2252 (no, really equal, in this case!), or
2253 2. Separate regions of unequal size.
2254
2255 The worst case is usually No. 2. It means that (aside from
2256 potential need for getting the gap out of the way), there also
2257 needs to be a shifting of the text between the two regions. So
2258 if they are spread far apart, we are that much slower... sigh. */
2259
2260 /* It must be pointed out that the really studly thing to do would
2261 be not to move the gap at all, but to leave it in place and work
2262 around it if necessary. This would be extremely efficient,
2263 especially considering that people are likely to do
2264 transpositions near where they are working interactively, which
2265 is exactly where the gap would be found. However, such code
2266 would be much harder to write and to read. So, if you are
2267 reading this comment and are feeling squirrely, by all means have
2268 a go! I just didn't feel like doing it, so I will simply move
2269 the gap the minimum distance to get it out of the way, and then
2270 deal with an unbroken array. */
2271
2272 /* Make sure the gap won't interfere, by moving it out of the text
2273 we will operate on. */
2274 if (start1 < gap && gap < end2)
2275 {
2276 if (gap - start1 < end2 - gap)
2277 move_gap (start1);
2278 else
2279 move_gap (end2);
2280 }
2281
2282 /* Hmmm... how about checking to see if the gap is large
2283 enough to use as the temporary storage? That would avoid an
2284 allocation... interesting. Later, don't fool with it now. */
2285
2286 /* Working without memmove, for portability (sigh), so must be
2287 careful of overlapping subsections of the array... */
2288
2289 if (end1 == start2) /* adjacent regions */
2290 {
2291 modify_region (current_buffer, start1, end2);
2292 record_change (start1, len1 + len2);
2293
2294 #ifdef USE_TEXT_PROPERTIES
2295 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2296 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2297 Fset_text_properties (start1, end2, Qnil, Qnil);
2298 #endif /* USE_TEXT_PROPERTIES */
2299
2300 /* First region smaller than second. */
2301 if (len1 < len2)
2302 {
2303 /* We use alloca only if it is small,
2304 because we want to avoid stack overflow. */
2305 if (len2 > 20000)
2306 temp = (unsigned char *) xmalloc (len2);
2307 else
2308 temp = (unsigned char *) alloca (len2);
2309
2310 /* Don't precompute these addresses. We have to compute them
2311 at the last minute, because the relocating allocator might
2312 have moved the buffer around during the xmalloc. */
2313 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2314 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2315
2316 bcopy (start2_addr, temp, len2);
2317 bcopy (start1_addr, start1_addr + len2, len1);
2318 bcopy (temp, start1_addr, len2);
2319 if (len2 > 20000)
2320 free (temp);
2321 }
2322 else
2323 /* First region not smaller than second. */
2324 {
2325 if (len1 > 20000)
2326 temp = (unsigned char *) xmalloc (len1);
2327 else
2328 temp = (unsigned char *) alloca (len1);
2329 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2330 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2331 bcopy (start1_addr, temp, len1);
2332 bcopy (start2_addr, start1_addr, len2);
2333 bcopy (temp, start1_addr + len2, len1);
2334 if (len1 > 20000)
2335 free (temp);
2336 }
2337 #ifdef USE_TEXT_PROPERTIES
2338 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
2339 len1, current_buffer, 0);
2340 graft_intervals_into_buffer (tmp_interval2, start1,
2341 len2, current_buffer, 0);
2342 #endif /* USE_TEXT_PROPERTIES */
2343 }
2344 /* Non-adjacent regions, because end1 != start2, bleagh... */
2345 else
2346 {
2347 if (len1 == len2)
2348 /* Regions are same size, though, how nice. */
2349 {
2350 modify_region (current_buffer, start1, end1);
2351 modify_region (current_buffer, start2, end2);
2352 record_change (start1, len1);
2353 record_change (start2, len2);
2354 #ifdef USE_TEXT_PROPERTIES
2355 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2356 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2357 Fset_text_properties (start1, end1, Qnil, Qnil);
2358 Fset_text_properties (start2, end2, Qnil, Qnil);
2359 #endif /* USE_TEXT_PROPERTIES */
2360
2361 if (len1 > 20000)
2362 temp = (unsigned char *) xmalloc (len1);
2363 else
2364 temp = (unsigned char *) alloca (len1);
2365 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2366 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2367 bcopy (start1_addr, temp, len1);
2368 bcopy (start2_addr, start1_addr, len2);
2369 bcopy (temp, start2_addr, len1);
2370 if (len1 > 20000)
2371 free (temp);
2372 #ifdef USE_TEXT_PROPERTIES
2373 graft_intervals_into_buffer (tmp_interval1, start2,
2374 len1, current_buffer, 0);
2375 graft_intervals_into_buffer (tmp_interval2, start1,
2376 len2, current_buffer, 0);
2377 #endif /* USE_TEXT_PROPERTIES */
2378 }
2379
2380 else if (len1 < len2) /* Second region larger than first */
2381 /* Non-adjacent & unequal size, area between must also be shifted. */
2382 {
2383 len_mid = start2 - end1;
2384 modify_region (current_buffer, start1, end2);
2385 record_change (start1, (end2 - start1));
2386 #ifdef USE_TEXT_PROPERTIES
2387 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2388 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2389 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2390 Fset_text_properties (start1, end2, Qnil, Qnil);
2391 #endif /* USE_TEXT_PROPERTIES */
2392
2393 /* holds region 2 */
2394 if (len2 > 20000)
2395 temp = (unsigned char *) xmalloc (len2);
2396 else
2397 temp = (unsigned char *) alloca (len2);
2398 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2399 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2400 bcopy (start2_addr, temp, len2);
2401 bcopy (start1_addr, start1_addr + len_mid + len2, len1);
2402 safe_bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2403 bcopy (temp, start1_addr, len2);
2404 if (len2 > 20000)
2405 free (temp);
2406 #ifdef USE_TEXT_PROPERTIES
2407 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2408 len1, current_buffer, 0);
2409 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2410 len_mid, current_buffer, 0);
2411 graft_intervals_into_buffer (tmp_interval2, start1,
2412 len2, current_buffer, 0);
2413 #endif /* USE_TEXT_PROPERTIES */
2414 }
2415 else
2416 /* Second region smaller than first. */
2417 {
2418 len_mid = start2 - end1;
2419 record_change (start1, (end2 - start1));
2420 modify_region (current_buffer, start1, end2);
2421
2422 #ifdef USE_TEXT_PROPERTIES
2423 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2424 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2425 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2426 Fset_text_properties (start1, end2, Qnil, Qnil);
2427 #endif /* USE_TEXT_PROPERTIES */
2428
2429 /* holds region 1 */
2430 if (len1 > 20000)
2431 temp = (unsigned char *) xmalloc (len1);
2432 else
2433 temp = (unsigned char *) alloca (len1);
2434 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2435 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2436 bcopy (start1_addr, temp, len1);
2437 bcopy (start2_addr, start1_addr, len2);
2438 bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2439 bcopy (temp, start1_addr + len2 + len_mid, len1);
2440 if (len1 > 20000)
2441 free (temp);
2442 #ifdef USE_TEXT_PROPERTIES
2443 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2444 len1, current_buffer, 0);
2445 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2446 len_mid, current_buffer, 0);
2447 graft_intervals_into_buffer (tmp_interval2, start1,
2448 len2, current_buffer, 0);
2449 #endif /* USE_TEXT_PROPERTIES */
2450 }
2451 }
2452
2453 /* todo: this will be slow, because for every transposition, we
2454 traverse the whole friggin marker list. Possible solutions:
2455 somehow get a list of *all* the markers across multiple
2456 transpositions and do it all in one swell phoop. Or maybe modify
2457 Emacs' marker code to keep an ordered list or tree. This might
2458 be nicer, and more beneficial in the long run, but would be a
2459 bunch of work. Plus the way they're arranged now is nice. */
2460 if (NILP (leave_markers))
2461 {
2462 transpose_markers (start1, end1, start2, end2);
2463 fix_overlays_in_range (start1, end2);
2464 }
2465
2466 return Qnil;
2467 }
2468
2469 \f
2470 void
2471 syms_of_editfns ()
2472 {
2473 environbuf = 0;
2474
2475 Qbuffer_access_fontify_functions
2476 = intern ("buffer-access-fontify-functions");
2477 staticpro (&Qbuffer_access_fontify_functions);
2478
2479 DEFVAR_LISP ("buffer-access-fontify-functions",
2480 &Vbuffer_access_fontify_functions,
2481 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2482 Each function is called with two arguments which specify the range\n\
2483 of the buffer being accessed.");
2484 Vbuffer_access_fontify_functions = Qnil;
2485
2486 {
2487 Lisp_Object obuf;
2488 extern Lisp_Object Vprin1_to_string_buffer;
2489 obuf = Fcurrent_buffer ();
2490 /* Do this here, because init_buffer_once is too early--it won't work. */
2491 Fset_buffer (Vprin1_to_string_buffer);
2492 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2493 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2494 Qnil);
2495 Fset_buffer (obuf);
2496 }
2497
2498 DEFVAR_LISP ("buffer-access-fontified-property",
2499 &Vbuffer_access_fontified_property,
2500 "Property which (if non-nil) indicates text has been fontified.\n\
2501 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2502 functions if all the text being accessed has this property.");
2503 Vbuffer_access_fontified_property = Qnil;
2504
2505 DEFVAR_LISP ("system-name", &Vsystem_name,
2506 "The name of the machine Emacs is running on.");
2507
2508 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
2509 "The full name of the user logged in.");
2510
2511 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
2512 "The user's name, taken from environment variables if possible.");
2513
2514 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
2515 "The user's name, based upon the real uid only.");
2516
2517 defsubr (&Schar_equal);
2518 defsubr (&Sgoto_char);
2519 defsubr (&Sstring_to_char);
2520 defsubr (&Schar_to_string);
2521 defsubr (&Sbuffer_substring);
2522 defsubr (&Sbuffer_substring_no_properties);
2523 defsubr (&Sbuffer_string);
2524
2525 defsubr (&Spoint_marker);
2526 defsubr (&Smark_marker);
2527 defsubr (&Spoint);
2528 defsubr (&Sregion_beginning);
2529 defsubr (&Sregion_end);
2530 /* defsubr (&Smark); */
2531 /* defsubr (&Sset_mark); */
2532 defsubr (&Ssave_excursion);
2533 defsubr (&Ssave_current_buffer);
2534
2535 defsubr (&Sbufsize);
2536 defsubr (&Spoint_max);
2537 defsubr (&Spoint_min);
2538 defsubr (&Spoint_min_marker);
2539 defsubr (&Spoint_max_marker);
2540
2541 defsubr (&Sbobp);
2542 defsubr (&Seobp);
2543 defsubr (&Sbolp);
2544 defsubr (&Seolp);
2545 defsubr (&Sfollowing_char);
2546 defsubr (&Sprevious_char);
2547 defsubr (&Schar_after);
2548 defsubr (&Sinsert);
2549 defsubr (&Sinsert_before_markers);
2550 defsubr (&Sinsert_and_inherit);
2551 defsubr (&Sinsert_and_inherit_before_markers);
2552 defsubr (&Sinsert_char);
2553
2554 defsubr (&Suser_login_name);
2555 defsubr (&Suser_real_login_name);
2556 defsubr (&Suser_uid);
2557 defsubr (&Suser_real_uid);
2558 defsubr (&Suser_full_name);
2559 defsubr (&Semacs_pid);
2560 defsubr (&Scurrent_time);
2561 defsubr (&Sformat_time_string);
2562 defsubr (&Sdecode_time);
2563 defsubr (&Sencode_time);
2564 defsubr (&Scurrent_time_string);
2565 defsubr (&Scurrent_time_zone);
2566 defsubr (&Sset_time_zone_rule);
2567 defsubr (&Ssystem_name);
2568 defsubr (&Smessage);
2569 defsubr (&Smessage_box);
2570 defsubr (&Smessage_or_box);
2571 defsubr (&Sformat);
2572
2573 defsubr (&Sinsert_buffer_substring);
2574 defsubr (&Scompare_buffer_substrings);
2575 defsubr (&Ssubst_char_in_region);
2576 defsubr (&Stranslate_region);
2577 defsubr (&Sdelete_region);
2578 defsubr (&Swiden);
2579 defsubr (&Snarrow_to_region);
2580 defsubr (&Ssave_restriction);
2581 defsubr (&Stranspose_regions);
2582 }