(Fnarrow_to_region): Swap using temp Lisp_Object, not int.
[bpt/emacs.git] / src / editfns.c
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94 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 1, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <sys/types.h>
22
23 #include <config.h>
24
25 #ifdef VMS
26 #include "vms-pwd.h"
27 #else
28 #include <pwd.h>
29 #endif
30
31 #include "lisp.h"
32 #include "intervals.h"
33 #include "buffer.h"
34 #include "window.h"
35
36 #include "systime.h"
37
38 #define min(a, b) ((a) < (b) ? (a) : (b))
39 #define max(a, b) ((a) > (b) ? (a) : (b))
40
41 extern void insert_from_buffer ();
42 static long difftm ();
43
44 /* Some static data, and a function to initialize it for each run */
45
46 Lisp_Object Vsystem_name;
47 Lisp_Object Vuser_real_name; /* login name of current user ID */
48 Lisp_Object Vuser_full_name; /* full name of current user */
49 Lisp_Object Vuser_name; /* user name from LOGNAME or USER */
50
51 void
52 init_editfns ()
53 {
54 char *user_name;
55 register unsigned char *p, *q, *r;
56 struct passwd *pw; /* password entry for the current user */
57 extern char *index ();
58 Lisp_Object tem;
59
60 /* Set up system_name even when dumping. */
61 init_system_name ();
62
63 #ifndef CANNOT_DUMP
64 /* Don't bother with this on initial start when just dumping out */
65 if (!initialized)
66 return;
67 #endif /* not CANNOT_DUMP */
68
69 pw = (struct passwd *) getpwuid (getuid ());
70 #ifdef MSDOS
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");
75 #else
76 Vuser_real_name = build_string (pw ? pw->pw_name : "unknown");
77 #endif
78
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");
82 if (!user_name)
83 #ifdef WINDOWSNT
84 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
85 #else /* WINDOWSNT */
86 user_name = (char *) getenv ("USER");
87 #endif /* WINDOWSNT */
88 if (!user_name)
89 {
90 pw = (struct passwd *) getpwuid (geteuid ());
91 user_name = (char *) (pw ? pw->pw_name : "unknown");
92 }
93 Vuser_name = build_string (user_name);
94
95 /* If the user name claimed in the environment vars differs from
96 the real uid, use the claimed name to find the full name. */
97 tem = Fstring_equal (Vuser_name, Vuser_real_name);
98 if (NILP (tem))
99 pw = (struct passwd *) getpwnam (XSTRING (Vuser_name)->data);
100
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));
104
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. */
109 if (q)
110 {
111 r = (char *) alloca (strlen (p) + XSTRING (Vuser_name)->size + 1);
112 bcopy (p, r, q - p);
113 r[q - p] = 0;
114 strcat (r, XSTRING (Vuser_name)->data);
115 r[q - p] = UPCASE (r[q - p]);
116 strcat (r, q + 1);
117 Vuser_full_name = build_string (r);
118 }
119 #endif /* AMPERSAND_FULL_NAME */
120 }
121 \f
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.")
124 (n)
125 Lisp_Object n;
126 {
127 char c;
128 CHECK_NUMBER (n, 0);
129
130 c = XINT (n);
131 return make_string (&c, 1);
132 }
133
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.")
136 (str)
137 register Lisp_Object str;
138 {
139 register Lisp_Object val;
140 register struct Lisp_String *p;
141 CHECK_STRING (str, 0);
142
143 p = XSTRING (str);
144 if (p->size)
145 XSETFASTINT (val, ((unsigned char *) p->data)[0]);
146 else
147 XSETFASTINT (val, 0);
148 return val;
149 }
150 \f
151 static Lisp_Object
152 buildmark (val)
153 int val;
154 {
155 register Lisp_Object mark;
156 mark = Fmake_marker ();
157 Fset_marker (mark, make_number (val), Qnil);
158 return mark;
159 }
160
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)")
164 ()
165 {
166 Lisp_Object temp;
167 XSETFASTINT (temp, point);
168 return temp;
169 }
170
171 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
172 "Return value of point, as a marker object.")
173 ()
174 {
175 return buildmark (point);
176 }
177
178 int
179 clip_to_bounds (lower, num, upper)
180 int lower, num, upper;
181 {
182 if (num < lower)
183 return lower;
184 else if (num > upper)
185 return upper;
186 else
187 return num;
188 }
189
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).")
193 (n)
194 register Lisp_Object n;
195 {
196 CHECK_NUMBER_COERCE_MARKER (n, 0);
197
198 SET_PT (clip_to_bounds (BEGV, XINT (n), ZV));
199 return n;
200 }
201
202 static Lisp_Object
203 region_limit (beginningp)
204 int beginningp;
205 {
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));
215 else
216 return (m);
217 }
218
219 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
220 "Return position of beginning of region, as an integer.")
221 ()
222 {
223 return (region_limit (1));
224 }
225
226 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
227 "Return position of end of region, as an integer.")
228 ()
229 {
230 return (region_limit (0));
231 }
232
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'.")
238 ()
239 {
240 return Fmarker_position (current_buffer->mark);
241 }
242 #endif /* commented out code */
243
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.")
248 ()
249 {
250 return current_buffer->mark;
251 }
252
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\
259 \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\
262 \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\
268 \n\
269 (let ((beg (point))) (forward-line 1) (delete-region beg (point))).")
270 (pos)
271 Lisp_Object pos;
272 {
273 if (NILP (pos))
274 {
275 current_buffer->mark = Qnil;
276 return Qnil;
277 }
278 CHECK_NUMBER_COERCE_MARKER (pos, 0);
279
280 if (NILP (current_buffer->mark))
281 current_buffer->mark = Fmake_marker ();
282
283 Fset_marker (current_buffer->mark, pos, Qnil);
284 return pos;
285 }
286 #endif /* commented-out code */
287
288 Lisp_Object
289 save_excursion_save ()
290 {
291 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
292 == current_buffer);
293
294 return Fcons (Fpoint_marker (),
295 Fcons (Fcopy_marker (current_buffer->mark),
296 Fcons (visible ? Qt : Qnil,
297 current_buffer->mark_active)));
298 }
299
300 Lisp_Object
301 save_excursion_restore (info)
302 register Lisp_Object info;
303 {
304 register Lisp_Object tem, tem1, omark, nmark;
305
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
309 and crash */
310 /* In that case, Fmarker_buffer returns nil now. */
311 if (NILP (tem))
312 return Qnil;
313 Fset_buffer (tem);
314 tem = Fcar (info);
315 Fgoto_char (tem);
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. */
327 tem1 = Fcar (tem);
328 if (!NILP (tem1)
329 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
330 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
331 #endif /* 0 */
332
333 tem1 = current_buffer->mark_active;
334 current_buffer->mark_active = Fcdr (tem);
335 if (!NILP (Vrun_hooks))
336 {
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))
340 {
341 if (! EQ (omark, nmark))
342 call1 (Vrun_hooks, intern ("activate-mark-hook"));
343 }
344 /* If mark has ceased to be active, run deactivate hook. */
345 else if (! NILP (tem1))
346 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
347 }
348 return Qnil;
349 }
350
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.")
357 (args)
358 Lisp_Object args;
359 {
360 register Lisp_Object val;
361 int count = specpdl_ptr - specpdl;
362
363 record_unwind_protect (save_excursion_restore, save_excursion_save ());
364
365 val = Fprogn (args);
366 return unbind_to (count, val);
367 }
368 \f
369 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
370 "Return the number of characters in the current buffer.")
371 ()
372 {
373 Lisp_Object temp;
374 XSETFASTINT (temp, Z - BEG);
375 return temp;
376 }
377
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.")
381 ()
382 {
383 Lisp_Object temp;
384 XSETFASTINT (temp, BEGV);
385 return temp;
386 }
387
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.")
391 ()
392 {
393 return buildmark (BEGV);
394 }
395
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.")
400 ()
401 {
402 Lisp_Object temp;
403 XSETFASTINT (temp, ZV);
404 return temp;
405 }
406
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.")
411 ()
412 {
413 return buildmark (ZV);
414 }
415
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.")
419 ()
420 {
421 Lisp_Object temp;
422 if (point >= ZV)
423 XSETFASTINT (temp, 0);
424 else
425 XSETFASTINT (temp, FETCH_CHAR (point));
426 return temp;
427 }
428
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.")
432 ()
433 {
434 Lisp_Object temp;
435 if (point <= BEGV)
436 XSETFASTINT (temp, 0);
437 else
438 XSETFASTINT (temp, FETCH_CHAR (point - 1));
439 return temp;
440 }
441
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.")
445 ()
446 {
447 if (point == BEGV)
448 return Qt;
449 return Qnil;
450 }
451
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.")
455 ()
456 {
457 if (point == ZV)
458 return Qt;
459 return Qnil;
460 }
461
462 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
463 "Return T if point is at the beginning of a line.")
464 ()
465 {
466 if (point == BEGV || FETCH_CHAR (point - 1) == '\n')
467 return Qt;
468 return Qnil;
469 }
470
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.")
474 ()
475 {
476 if (point == ZV || FETCH_CHAR (point) == '\n')
477 return Qt;
478 return Qnil;
479 }
480
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.")
485 (pos)
486 Lisp_Object pos;
487 {
488 register Lisp_Object val;
489 register int n;
490
491 CHECK_NUMBER_COERCE_MARKER (pos, 0);
492
493 n = XINT (pos);
494 if (n < BEGV || n >= ZV) return Qnil;
495
496 XSETFASTINT (val, FETCH_CHAR (n));
497 return val;
498 }
499 \f
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.")
507 (uid)
508 Lisp_Object uid;
509 {
510 struct passwd *pw;
511
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))
516 init_editfns ();
517
518 if (NILP (uid))
519 return Vuser_name;
520
521 CHECK_NUMBER (uid, 0);
522 pw = (struct passwd *) getpwuid (XINT (uid));
523 return (pw ? build_string (pw->pw_name) : Qnil);
524 }
525
526 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
527 0, 0, 0,
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'.")
531 ()
532 {
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))
537 init_editfns ();
538 return Vuser_real_name;
539 }
540
541 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
542 "Return the effective uid of Emacs, as an integer.")
543 ()
544 {
545 return make_number (geteuid ());
546 }
547
548 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
549 "Return the real uid of Emacs, as an integer.")
550 ()
551 {
552 return make_number (getuid ());
553 }
554
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.")
557 ()
558 {
559 return Vuser_full_name;
560 }
561
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.")
564 ()
565 {
566 return Vsystem_name;
567 }
568
569 /* For the benefit of callers who don't want to include lisp.h */
570 char *
571 get_system_name ()
572 {
573 return (char *) XSTRING (Vsystem_name)->data;
574 }
575
576 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
577 "Return the process ID of Emacs, as an integer.")
578 ()
579 {
580 return make_number (getpid ());
581 }
582
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\
588 count.\n\
589 \n\
590 The microsecond count is zero on systems that do not provide\n\
591 resolution finer than a second.")
592 ()
593 {
594 EMACS_TIME t;
595 Lisp_Object result[3];
596
597 EMACS_GET_TIME (t);
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));
601
602 return Flist (3, result);
603 }
604 \f
605
606 static int
607 lisp_time_argument (specified_time, result)
608 Lisp_Object specified_time;
609 time_t *result;
610 {
611 if (NILP (specified_time))
612 return time (result) != -1;
613 else
614 {
615 Lisp_Object high, low;
616 high = Fcar (specified_time);
617 CHECK_NUMBER (high, 0);
618 low = Fcdr (specified_time);
619 if (CONSP (low))
620 low = Fcar (low);
621 CHECK_NUMBER (low, 0);
622 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
623 return *result >> 16 == XINT (high);
624 }
625 }
626
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\
664 \n\
665 The number of options reflects the strftime(3) function.")
666 (format_string, time)
667 Lisp_Object format_string, time;
668 {
669 time_t value;
670 int size;
671
672 CHECK_STRING (format_string, 1);
673
674 if (! lisp_time_argument (time, &value))
675 error ("Invalid time specification");
676
677 /* This is probably enough. */
678 size = XSTRING (format_string)->size * 6 + 50;
679
680 while (1)
681 {
682 char *buf = (char *) alloca (size);
683 if (strftime (buf, size, XSTRING (format_string)->data,
684 localtime (&value)))
685 return build_string (buf);
686 /* If buffer was too small, make it bigger. */
687 size *= 2;
688 }
689 }
690
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.)")
703 (specified_time)
704 Lisp_Object specified_time;
705 {
706 time_t time_spec;
707 struct tm save_tm;
708 struct tm *decoded_time;
709 Lisp_Object list_args[9];
710
711 if (! lisp_time_argument (specified_time, &time_spec))
712 error ("Invalid time specification");
713
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;
723
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)
728 list_args[8] = Qnil;
729 else
730 XSETINT (list_args[8], difftm (&save_tm, decoded_time));
731 return Flist (9, list_args);
732 }
733
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\
741 (HIGH . LOW)\n\
742 or the form:\n\
743 (HIGH LOW . IGNORED).\n\
744 Thus, you can use times obtained from `current-time'\n\
745 and from `file-attributes'.")
746 (specified_time)
747 Lisp_Object specified_time;
748 {
749 time_t value;
750 char buf[30];
751 register char *tem;
752
753 if (! lisp_time_argument (specified_time, &value))
754 value = -1;
755 tem = (char *) ctime (&value);
756
757 strncpy (buf, tem, 24);
758 buf[24] = 0;
759
760 return build_string (buf);
761 }
762
763 #define TM_YEAR_ORIGIN 1900
764
765 /* Yield A - B, measured in seconds. */
766 static long
767 difftm (a, b)
768 struct tm *a, *b;
769 {
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. */
773 long days = (
774 /* difference in day of year */
775 a->tm_yday - b->tm_yday
776 /* + intervening leap days */
777 + ((ay >> 2) - (by >> 2))
778 - (ay/100 - by/100)
779 + ((ay/100 >> 2) - (by/100 >> 2))
780 /* + difference in years * 365 */
781 + (long)(ay-by) * 365
782 );
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));
786 }
787
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\
796 (HIGH . LOW)\n\
797 or 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\
801 \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.")
805 (specified_time)
806 Lisp_Object specified_time;
807 {
808 time_t value;
809 struct tm *t;
810
811 if (lisp_time_argument (specified_time, &value)
812 && (t = gmtime (&value)) != 0)
813 {
814 struct tm gmt;
815 long offset;
816 char *s, buf[6];
817
818 gmt = *t; /* Make a copy, in case localtime modifies *t. */
819 t = localtime (&value);
820 offset = difftm (t, &gmt);
821 s = 0;
822 #ifdef HAVE_TM_ZONE
823 if (t->tm_zone)
824 s = (char *)t->tm_zone;
825 #else /* not HAVE_TM_ZONE */
826 #ifdef HAVE_TZNAME
827 if (t->tm_isdst == 0 || t->tm_isdst == 1)
828 s = tzname[t->tm_isdst];
829 #endif
830 #endif /* not HAVE_TM_ZONE */
831 if (!s)
832 {
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);
836 s = buf;
837 }
838 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
839 }
840 else
841 return Fmake_list (2, Qnil);
842 }
843
844 \f
845 void
846 insert1 (arg)
847 Lisp_Object arg;
848 {
849 Finsert (1, &arg);
850 }
851
852
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. */
857
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.")
862 (nargs, args)
863 int nargs;
864 register Lisp_Object *args;
865 {
866 register int argnum;
867 register Lisp_Object tem;
868 char str[1];
869
870 for (argnum = 0; argnum < nargs; argnum++)
871 {
872 tem = args[argnum];
873 retry:
874 if (INTEGERP (tem))
875 {
876 str[0] = XINT (tem);
877 insert (str, 1);
878 }
879 else if (STRINGP (tem))
880 {
881 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
882 }
883 else
884 {
885 tem = wrong_type_argument (Qchar_or_string_p, tem);
886 goto retry;
887 }
888 }
889
890 return Qnil;
891 }
892
893 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
894 0, MANY, 0,
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.")
898 (nargs, args)
899 int nargs;
900 register Lisp_Object *args;
901 {
902 register int argnum;
903 register Lisp_Object tem;
904 char str[1];
905
906 for (argnum = 0; argnum < nargs; argnum++)
907 {
908 tem = args[argnum];
909 retry:
910 if (INTEGERP (tem))
911 {
912 str[0] = XINT (tem);
913 insert_and_inherit (str, 1);
914 }
915 else if (STRINGP (tem))
916 {
917 insert_from_string (tem, 0, XSTRING (tem)->size, 1);
918 }
919 else
920 {
921 tem = wrong_type_argument (Qchar_or_string_p, tem);
922 goto retry;
923 }
924 }
925
926 return Qnil;
927 }
928
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.")
933 (nargs, args)
934 int nargs;
935 register Lisp_Object *args;
936 {
937 register int argnum;
938 register Lisp_Object tem;
939 char str[1];
940
941 for (argnum = 0; argnum < nargs; argnum++)
942 {
943 tem = args[argnum];
944 retry:
945 if (INTEGERP (tem))
946 {
947 str[0] = XINT (tem);
948 insert_before_markers (str, 1);
949 }
950 else if (STRINGP (tem))
951 {
952 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size, 0);
953 }
954 else
955 {
956 tem = wrong_type_argument (Qchar_or_string_p, tem);
957 goto retry;
958 }
959 }
960
961 return Qnil;
962 }
963
964 DEFUN ("insert-before-markers-and-inherit",
965 Finsert_and_inherit_before_markers, Sinsert_and_inherit_before_markers,
966 0, MANY, 0,
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.")
970 (nargs, args)
971 int nargs;
972 register Lisp_Object *args;
973 {
974 register int argnum;
975 register Lisp_Object tem;
976 char str[1];
977
978 for (argnum = 0; argnum < nargs; argnum++)
979 {
980 tem = args[argnum];
981 retry:
982 if (INTEGERP (tem))
983 {
984 str[0] = XINT (tem);
985 insert_before_markers_and_inherit (str, 1);
986 }
987 else if (STRINGP (tem))
988 {
989 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size, 1);
990 }
991 else
992 {
993 tem = wrong_type_argument (Qchar_or_string_p, tem);
994 goto retry;
995 }
996 }
997
998 return Qnil;
999 }
1000 \f
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;
1009 {
1010 register unsigned char *string;
1011 register int strlen;
1012 register int i, n;
1013
1014 CHECK_NUMBER (chr, 0);
1015 CHECK_NUMBER (count, 1);
1016
1017 n = XINT (count);
1018 if (n <= 0)
1019 return Qnil;
1020 strlen = min (n, 256);
1021 string = (unsigned char *) alloca (strlen);
1022 for (i = 0; i < strlen; i++)
1023 string[i] = XFASTINT (chr);
1024 while (n >= strlen)
1025 {
1026 if (!NILP (inherit))
1027 insert_and_inherit (string, strlen);
1028 else
1029 insert (string, strlen);
1030 n -= strlen;
1031 }
1032 if (n > 0)
1033 {
1034 if (!NILP (inherit))
1035 insert_and_inherit (string, n);
1036 else
1037 insert (string, n);
1038 }
1039 return Qnil;
1040 }
1041
1042 \f
1043 /* Making strings from buffer contents. */
1044
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
1048 have them.
1049
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. */
1057
1058 Lisp_Object
1059 make_buffer_string (start, end)
1060 int start, end;
1061 {
1062 Lisp_Object result, tem, tem1;
1063
1064 if (start < GPT && GPT < end)
1065 move_gap (start);
1066
1067 result = make_uninit_string (end - start);
1068 bcopy (&FETCH_CHAR (start), XSTRING (result)->data, end - start);
1069
1070 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1071 tem1 = Ftext_properties_at (make_number (start), Qnil);
1072
1073 #ifdef USE_TEXT_PROPERTIES
1074 if (XINT (tem) != end || !NILP (tem1))
1075 copy_intervals_to_string (result, current_buffer, start, end - start);
1076 #endif
1077
1078 return result;
1079 }
1080
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.")
1085 (b, e)
1086 Lisp_Object b, e;
1087 {
1088 register int beg, end;
1089
1090 validate_region (&b, &e);
1091 beg = XINT (b);
1092 end = XINT (e);
1093
1094 return make_buffer_string (beg, end);
1095 }
1096
1097 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
1098 "Return the contents of the current buffer as a string.")
1099 ()
1100 {
1101 return make_buffer_string (BEGV, ZV);
1102 }
1103
1104 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
1105 1, 3, 0,
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.")
1110 (buf, b, e)
1111 Lisp_Object buf, b, e;
1112 {
1113 register int beg, end, temp;
1114 register struct buffer *bp;
1115 Lisp_Object buffer;
1116
1117 buffer = Fget_buffer (buf);
1118 if (NILP (buffer))
1119 nsberror (buf);
1120 bp = XBUFFER (buffer);
1121
1122 if (NILP (b))
1123 beg = BUF_BEGV (bp);
1124 else
1125 {
1126 CHECK_NUMBER_COERCE_MARKER (b, 0);
1127 beg = XINT (b);
1128 }
1129 if (NILP (e))
1130 end = BUF_ZV (bp);
1131 else
1132 {
1133 CHECK_NUMBER_COERCE_MARKER (e, 1);
1134 end = XINT (e);
1135 }
1136
1137 if (beg > end)
1138 temp = beg, beg = end, end = temp;
1139
1140 if (!(BUF_BEGV (bp) <= beg && end <= BUF_ZV (bp)))
1141 args_out_of_range (b, e);
1142
1143 insert_from_buffer (bp, beg, end - beg, 0);
1144 return Qnil;
1145 }
1146
1147 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
1148 6, 6, 0,
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;
1158 {
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);
1164
1165 /* Find the first buffer and its substring. */
1166
1167 if (NILP (buffer1))
1168 bp1 = current_buffer;
1169 else
1170 {
1171 Lisp_Object buf1;
1172 buf1 = Fget_buffer (buffer1);
1173 if (NILP (buf1))
1174 nsberror (buffer1);
1175 bp1 = XBUFFER (buf1);
1176 }
1177
1178 if (NILP (start1))
1179 begp1 = BUF_BEGV (bp1);
1180 else
1181 {
1182 CHECK_NUMBER_COERCE_MARKER (start1, 1);
1183 begp1 = XINT (start1);
1184 }
1185 if (NILP (end1))
1186 endp1 = BUF_ZV (bp1);
1187 else
1188 {
1189 CHECK_NUMBER_COERCE_MARKER (end1, 2);
1190 endp1 = XINT (end1);
1191 }
1192
1193 if (begp1 > endp1)
1194 temp = begp1, begp1 = endp1, endp1 = temp;
1195
1196 if (!(BUF_BEGV (bp1) <= begp1
1197 && begp1 <= endp1
1198 && endp1 <= BUF_ZV (bp1)))
1199 args_out_of_range (start1, end1);
1200
1201 /* Likewise for second substring. */
1202
1203 if (NILP (buffer2))
1204 bp2 = current_buffer;
1205 else
1206 {
1207 Lisp_Object buf2;
1208 buf2 = Fget_buffer (buffer2);
1209 if (NILP (buf2))
1210 nsberror (buffer2);
1211 bp2 = XBUFFER (buffer2);
1212 }
1213
1214 if (NILP (start2))
1215 begp2 = BUF_BEGV (bp2);
1216 else
1217 {
1218 CHECK_NUMBER_COERCE_MARKER (start2, 4);
1219 begp2 = XINT (start2);
1220 }
1221 if (NILP (end2))
1222 endp2 = BUF_ZV (bp2);
1223 else
1224 {
1225 CHECK_NUMBER_COERCE_MARKER (end2, 5);
1226 endp2 = XINT (end2);
1227 }
1228
1229 if (begp2 > endp2)
1230 temp = begp2, begp2 = endp2, endp2 = temp;
1231
1232 if (!(BUF_BEGV (bp2) <= begp2
1233 && begp2 <= endp2
1234 && endp2 <= BUF_ZV (bp2)))
1235 args_out_of_range (start2, end2);
1236
1237 len1 = endp1 - begp1;
1238 len2 = endp2 - begp2;
1239 length = len1;
1240 if (len2 < length)
1241 length = len2;
1242
1243 for (i = 0; i < length; i++)
1244 {
1245 int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
1246 int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
1247 if (trt)
1248 {
1249 c1 = trt[c1];
1250 c2 = trt[c2];
1251 }
1252 if (c1 < c2)
1253 return make_number (- 1 - i);
1254 if (c1 > c2)
1255 return make_number (i + 1);
1256 }
1257
1258 /* The strings match as far as they go.
1259 If one is shorter, that one is less. */
1260 if (length < len1)
1261 return make_number (length + 1);
1262 else if (length < len2)
1263 return make_number (- length - 1);
1264
1265 /* Same length too => they are equal. */
1266 return make_number (0);
1267 }
1268 \f
1269 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1270 Ssubst_char_in_region, 4, 5, 0,
1271 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1272 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1273 and don't mark the buffer as really changed.")
1274 (start, end, fromchar, tochar, noundo)
1275 Lisp_Object start, end, fromchar, tochar, noundo;
1276 {
1277 register int pos, stop, look;
1278 int changed = 0;
1279
1280 validate_region (&start, &end);
1281 CHECK_NUMBER (fromchar, 2);
1282 CHECK_NUMBER (tochar, 3);
1283
1284 pos = XINT (start);
1285 stop = XINT (end);
1286 look = XINT (fromchar);
1287
1288 while (pos < stop)
1289 {
1290 if (FETCH_CHAR (pos) == look)
1291 {
1292 if (! changed)
1293 {
1294 modify_region (current_buffer, XINT (start), stop);
1295
1296 if (! NILP (noundo))
1297 {
1298 if (MODIFF - 1 == SAVE_MODIFF)
1299 SAVE_MODIFF++;
1300 if (MODIFF - 1 == current_buffer->auto_save_modified)
1301 current_buffer->auto_save_modified++;
1302 }
1303
1304 changed = 1;
1305 }
1306
1307 if (NILP (noundo))
1308 record_change (pos, 1);
1309 FETCH_CHAR (pos) = XINT (tochar);
1310 }
1311 pos++;
1312 }
1313
1314 if (changed)
1315 signal_after_change (XINT (start),
1316 stop - XINT (start), stop - XINT (start));
1317
1318 return Qnil;
1319 }
1320
1321 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
1322 "From START to END, translate characters according to TABLE.\n\
1323 TABLE is a string; the Nth character in it is the mapping\n\
1324 for the character with code N. Returns the number of characters changed.")
1325 (start, end, table)
1326 Lisp_Object start;
1327 Lisp_Object end;
1328 register Lisp_Object table;
1329 {
1330 register int pos, stop; /* Limits of the region. */
1331 register unsigned char *tt; /* Trans table. */
1332 register int oc; /* Old character. */
1333 register int nc; /* New character. */
1334 int cnt; /* Number of changes made. */
1335 Lisp_Object z; /* Return. */
1336 int size; /* Size of translate table. */
1337
1338 validate_region (&start, &end);
1339 CHECK_STRING (table, 2);
1340
1341 size = XSTRING (table)->size;
1342 tt = XSTRING (table)->data;
1343
1344 pos = XINT (start);
1345 stop = XINT (end);
1346 modify_region (current_buffer, pos, stop);
1347
1348 cnt = 0;
1349 for (; pos < stop; ++pos)
1350 {
1351 oc = FETCH_CHAR (pos);
1352 if (oc < size)
1353 {
1354 nc = tt[oc];
1355 if (nc != oc)
1356 {
1357 record_change (pos, 1);
1358 FETCH_CHAR (pos) = nc;
1359 signal_after_change (pos, 1, 1);
1360 ++cnt;
1361 }
1362 }
1363 }
1364
1365 XSETFASTINT (z, cnt);
1366 return (z);
1367 }
1368
1369 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
1370 "Delete the text between point and mark.\n\
1371 When called from a program, expects two arguments,\n\
1372 positions (integers or markers) specifying the stretch to be deleted.")
1373 (b, e)
1374 Lisp_Object b, e;
1375 {
1376 validate_region (&b, &e);
1377 del_range (XINT (b), XINT (e));
1378 return Qnil;
1379 }
1380 \f
1381 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
1382 "Remove restrictions (narrowing) from current buffer.\n\
1383 This allows the buffer's full text to be seen and edited.")
1384 ()
1385 {
1386 BEGV = BEG;
1387 SET_BUF_ZV (current_buffer, Z);
1388 clip_changed = 1;
1389 /* Changing the buffer bounds invalidates any recorded current column. */
1390 invalidate_current_column ();
1391 return Qnil;
1392 }
1393
1394 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
1395 "Restrict editing in this buffer to the current region.\n\
1396 The rest of the text becomes temporarily invisible and untouchable\n\
1397 but is not deleted; if you save the buffer in a file, the invisible\n\
1398 text is included in the file. \\[widen] makes all visible again.\n\
1399 See also `save-restriction'.\n\
1400 \n\
1401 When calling from a program, pass two arguments; positions (integers\n\
1402 or markers) bounding the text that should remain visible.")
1403 (b, e)
1404 register Lisp_Object b, e;
1405 {
1406 CHECK_NUMBER_COERCE_MARKER (b, 0);
1407 CHECK_NUMBER_COERCE_MARKER (e, 1);
1408
1409 if (XINT (b) > XINT (e))
1410 {
1411 Lisp_Object tem;
1412 tem = b; b = e; e = tem;
1413 }
1414
1415 if (!(BEG <= XINT (b) && XINT (b) <= XINT (e) && XINT (e) <= Z))
1416 args_out_of_range (b, e);
1417
1418 BEGV = XFASTINT (b);
1419 SET_BUF_ZV (current_buffer, XFASTINT (e));
1420 if (point < XFASTINT (b))
1421 SET_PT (XFASTINT (b));
1422 if (point > XFASTINT (e))
1423 SET_PT (XFASTINT (e));
1424 clip_changed = 1;
1425 /* Changing the buffer bounds invalidates any recorded current column. */
1426 invalidate_current_column ();
1427 return Qnil;
1428 }
1429
1430 Lisp_Object
1431 save_restriction_save ()
1432 {
1433 register Lisp_Object bottom, top;
1434 /* Note: I tried using markers here, but it does not win
1435 because insertion at the end of the saved region
1436 does not advance mh and is considered "outside" the saved region. */
1437 XSETFASTINT (bottom, BEGV - BEG);
1438 XSETFASTINT (top, Z - ZV);
1439
1440 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
1441 }
1442
1443 Lisp_Object
1444 save_restriction_restore (data)
1445 Lisp_Object data;
1446 {
1447 register struct buffer *buf;
1448 register int newhead, newtail;
1449 register Lisp_Object tem;
1450
1451 buf = XBUFFER (XCONS (data)->car);
1452
1453 data = XCONS (data)->cdr;
1454
1455 tem = XCONS (data)->car;
1456 newhead = XINT (tem);
1457 tem = XCONS (data)->cdr;
1458 newtail = XINT (tem);
1459 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
1460 {
1461 newhead = 0;
1462 newtail = 0;
1463 }
1464 BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
1465 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
1466 clip_changed = 1;
1467
1468 /* If point is outside the new visible range, move it inside. */
1469 SET_BUF_PT (buf,
1470 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
1471
1472 return Qnil;
1473 }
1474
1475 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
1476 "Execute BODY, saving and restoring current buffer's restrictions.\n\
1477 The buffer's restrictions make parts of the beginning and end invisible.\n\
1478 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
1479 This special form, `save-restriction', saves the current buffer's restrictions\n\
1480 when it is entered, and restores them when it is exited.\n\
1481 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
1482 The old restrictions settings are restored\n\
1483 even in case of abnormal exit (throw or error).\n\
1484 \n\
1485 The value returned is the value of the last form in BODY.\n\
1486 \n\
1487 `save-restriction' can get confused if, within the BODY, you widen\n\
1488 and then make changes outside the area within the saved restrictions.\n\
1489 \n\
1490 Note: if you are using both `save-excursion' and `save-restriction',\n\
1491 use `save-excursion' outermost:\n\
1492 (save-excursion (save-restriction ...))")
1493 (body)
1494 Lisp_Object body;
1495 {
1496 register Lisp_Object val;
1497 int count = specpdl_ptr - specpdl;
1498
1499 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1500 val = Fprogn (body);
1501 return unbind_to (count, val);
1502 }
1503 \f
1504 /* Buffer for the most recent text displayed by Fmessage. */
1505 static char *message_text;
1506
1507 /* Allocated length of that buffer. */
1508 static int message_length;
1509
1510 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
1511 "Print a one-line message at the bottom of the screen.\n\
1512 The first argument is a control string.\n\
1513 It may contain %s or %d or %c to print successive following arguments.\n\
1514 %s means print an argument as a string, %d means print as number in decimal,\n\
1515 %c means print a number as a single character.\n\
1516 The argument used by %s must be a string or a symbol;\n\
1517 the argument used by %d or %c must be a number.\n\
1518 If the first argument is nil, clear any existing message; let the\n\
1519 minibuffer contents show.")
1520 (nargs, args)
1521 int nargs;
1522 Lisp_Object *args;
1523 {
1524 if (NILP (args[0]))
1525 {
1526 message (0);
1527 return Qnil;
1528 }
1529 else
1530 {
1531 register Lisp_Object val;
1532 val = Fformat (nargs, args);
1533 /* Copy the data so that it won't move when we GC. */
1534 if (! message_text)
1535 {
1536 message_text = (char *)xmalloc (80);
1537 message_length = 80;
1538 }
1539 if (XSTRING (val)->size > message_length)
1540 {
1541 message_length = XSTRING (val)->size;
1542 message_text = (char *)xrealloc (message_text, message_length);
1543 }
1544 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
1545 message2 (message_text, XSTRING (val)->size);
1546 return val;
1547 }
1548 }
1549
1550 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
1551 "Display a message, in a dialog box if possible.\n\
1552 If a dialog box is not available, use the echo area.\n\
1553 The first argument is a control string.\n\
1554 It may contain %s or %d or %c to print successive following arguments.\n\
1555 %s means print an argument as a string, %d means print as number in decimal,\n\
1556 %c means print a number as a single character.\n\
1557 The argument used by %s must be a string or a symbol;\n\
1558 the argument used by %d or %c must be a number.\n\
1559 If the first argument is nil, clear any existing message; let the\n\
1560 minibuffer contents show.")
1561 (nargs, args)
1562 int nargs;
1563 Lisp_Object *args;
1564 {
1565 if (NILP (args[0]))
1566 {
1567 message (0);
1568 return Qnil;
1569 }
1570 else
1571 {
1572 register Lisp_Object val;
1573 val = Fformat (nargs, args);
1574 #ifdef HAVE_X_MENU
1575 {
1576 Lisp_Object pane, menu, obj;
1577 struct gcpro gcpro1;
1578 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
1579 GCPRO1 (pane);
1580 menu = Fcons (val, pane);
1581 obj = Fx_popup_dialog (Qt, menu);
1582 UNGCPRO;
1583 return val;
1584 }
1585 #else
1586 /* Copy the data so that it won't move when we GC. */
1587 if (! message_text)
1588 {
1589 message_text = (char *)xmalloc (80);
1590 message_length = 80;
1591 }
1592 if (XSTRING (val)->size > message_length)
1593 {
1594 message_length = XSTRING (val)->size;
1595 message_text = (char *)xrealloc (message_text, message_length);
1596 }
1597 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
1598 message2 (message_text, XSTRING (val)->size);
1599 return val;
1600 #endif
1601 }
1602 }
1603 #ifdef HAVE_X_MENU
1604 extern Lisp_Object last_nonmenu_event;
1605 #endif
1606 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
1607 "Display a message in a dialog box or in the echo area.\n\
1608 If this command was invoked with the mouse, use a dialog box.\n\
1609 Otherwise, use the echo area.\n\
1610 \n\
1611 The first argument is a control string.\n\
1612 It may contain %s or %d or %c to print successive following arguments.\n\
1613 %s means print an argument as a string, %d means print as number in decimal,\n\
1614 %c means print a number as a single character.\n\
1615 The argument used by %s must be a string or a symbol;\n\
1616 the argument used by %d or %c must be a number.\n\
1617 If the first argument is nil, clear any existing message; let the\n\
1618 minibuffer contents show.")
1619 (nargs, args)
1620 int nargs;
1621 Lisp_Object *args;
1622 {
1623 #ifdef HAVE_X_MENU
1624 if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
1625 return Fmessage_box (nargs, args);
1626 #endif
1627 return Fmessage (nargs, args);
1628 }
1629
1630 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
1631 "Format a string out of a control-string and arguments.\n\
1632 The first argument is a control string.\n\
1633 The other arguments are substituted into it to make the result, a string.\n\
1634 It may contain %-sequences meaning to substitute the next argument.\n\
1635 %s means print a string argument. Actually, prints any object, with `princ'.\n\
1636 %d means print as number in decimal (%o octal, %x hex).\n\
1637 %c means print a number as a single character.\n\
1638 %S means print any object as an s-expression (using prin1).\n\
1639 The argument used for %d, %o, %x or %c must be a number.\n\
1640 Use %% to put a single % into the output.")
1641 (nargs, args)
1642 int nargs;
1643 register Lisp_Object *args;
1644 {
1645 register int n; /* The number of the next arg to substitute */
1646 register int total = 5; /* An estimate of the final length */
1647 char *buf;
1648 register unsigned char *format, *end;
1649 int length;
1650 extern char *index ();
1651 /* It should not be necessary to GCPRO ARGS, because
1652 the caller in the interpreter should take care of that. */
1653
1654 CHECK_STRING (args[0], 0);
1655 format = XSTRING (args[0])->data;
1656 end = format + XSTRING (args[0])->size;
1657
1658 n = 0;
1659 while (format != end)
1660 if (*format++ == '%')
1661 {
1662 int minlen;
1663
1664 /* Process a numeric arg and skip it. */
1665 minlen = atoi (format);
1666 if (minlen > 0)
1667 total += minlen;
1668 else
1669 total -= minlen;
1670 while ((*format >= '0' && *format <= '9')
1671 || *format == '-' || *format == ' ' || *format == '.')
1672 format++;
1673
1674 if (*format == '%')
1675 format++;
1676 else if (++n >= nargs)
1677 error ("not enough arguments for format string");
1678 else if (*format == 'S')
1679 {
1680 /* For `S', prin1 the argument and then treat like a string. */
1681 register Lisp_Object tem;
1682 tem = Fprin1_to_string (args[n], Qnil);
1683 args[n] = tem;
1684 goto string;
1685 }
1686 else if (SYMBOLP (args[n]))
1687 {
1688 XSETSTRING (args[n], XSYMBOL (args[n])->name);
1689 goto string;
1690 }
1691 else if (STRINGP (args[n]))
1692 {
1693 string:
1694 if (*format != 's' && *format != 'S')
1695 error ("format specifier doesn't match argument type");
1696 total += XSTRING (args[n])->size;
1697 }
1698 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
1699 else if (INTEGERP (args[n]) && *format != 's')
1700 {
1701 #ifdef LISP_FLOAT_TYPE
1702 /* The following loop assumes the Lisp type indicates
1703 the proper way to pass the argument.
1704 So make sure we have a flonum if the argument should
1705 be a double. */
1706 if (*format == 'e' || *format == 'f' || *format == 'g')
1707 args[n] = Ffloat (args[n]);
1708 #endif
1709 total += 10;
1710 }
1711 #ifdef LISP_FLOAT_TYPE
1712 else if (FLOATP (args[n]) && *format != 's')
1713 {
1714 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
1715 args[n] = Ftruncate (args[n]);
1716 total += 20;
1717 }
1718 #endif
1719 else
1720 {
1721 /* Anything but a string, convert to a string using princ. */
1722 register Lisp_Object tem;
1723 tem = Fprin1_to_string (args[n], Qt);
1724 args[n] = tem;
1725 goto string;
1726 }
1727 }
1728
1729 {
1730 register int nstrings = n + 1;
1731
1732 /* Allocate twice as many strings as we have %-escapes; floats occupy
1733 two slots, and we're not sure how many of those we have. */
1734 register unsigned char **strings
1735 = (unsigned char **) alloca (2 * nstrings * sizeof (unsigned char *));
1736 int i;
1737
1738 i = 0;
1739 for (n = 0; n < nstrings; n++)
1740 {
1741 if (n >= nargs)
1742 strings[i++] = (unsigned char *) "";
1743 else if (INTEGERP (args[n]))
1744 /* We checked above that the corresponding format effector
1745 isn't %s, which would cause MPV. */
1746 strings[i++] = (unsigned char *) XINT (args[n]);
1747 #ifdef LISP_FLOAT_TYPE
1748 else if (FLOATP (args[n]))
1749 {
1750 union { double d; int half[2]; } u;
1751
1752 u.d = XFLOAT (args[n])->data;
1753 strings[i++] = (unsigned char *) u.half[0];
1754 strings[i++] = (unsigned char *) u.half[1];
1755 }
1756 #endif
1757 else
1758 strings[i++] = XSTRING (args[n])->data;
1759 }
1760
1761 /* Format it in bigger and bigger buf's until it all fits. */
1762 while (1)
1763 {
1764 buf = (char *) alloca (total + 1);
1765 buf[total - 1] = 0;
1766
1767 length = doprnt (buf, total + 1, strings[0], end, i-1, strings + 1);
1768 if (buf[total - 1] == 0)
1769 break;
1770
1771 total *= 2;
1772 }
1773 }
1774
1775 /* UNGCPRO; */
1776 return make_string (buf, length);
1777 }
1778
1779 /* VARARGS 1 */
1780 Lisp_Object
1781 #ifdef NO_ARG_ARRAY
1782 format1 (string1, arg0, arg1, arg2, arg3, arg4)
1783 EMACS_INT arg0, arg1, arg2, arg3, arg4;
1784 #else
1785 format1 (string1)
1786 #endif
1787 char *string1;
1788 {
1789 char buf[100];
1790 #ifdef NO_ARG_ARRAY
1791 EMACS_INT args[5];
1792 args[0] = arg0;
1793 args[1] = arg1;
1794 args[2] = arg2;
1795 args[3] = arg3;
1796 args[4] = arg4;
1797 doprnt (buf, sizeof buf, string1, 0, 5, args);
1798 #else
1799 doprnt (buf, sizeof buf, string1, 0, 5, &string1 + 1);
1800 #endif
1801 return build_string (buf);
1802 }
1803 \f
1804 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
1805 "Return t if two characters match, optionally ignoring case.\n\
1806 Both arguments must be characters (i.e. integers).\n\
1807 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
1808 (c1, c2)
1809 register Lisp_Object c1, c2;
1810 {
1811 unsigned char *downcase = DOWNCASE_TABLE;
1812 CHECK_NUMBER (c1, 0);
1813 CHECK_NUMBER (c2, 1);
1814
1815 if (!NILP (current_buffer->case_fold_search)
1816 ? (downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
1817 && (XFASTINT (c1) & ~0xff) == (XFASTINT (c2) & ~0xff))
1818 : XINT (c1) == XINT (c2))
1819 return Qt;
1820 return Qnil;
1821 }
1822 \f
1823 /* Transpose the markers in two regions of the current buffer, and
1824 adjust the ones between them if necessary (i.e.: if the regions
1825 differ in size).
1826
1827 Traverses the entire marker list of the buffer to do so, adding an
1828 appropriate amount to some, subtracting from some, and leaving the
1829 rest untouched. Most of this is copied from adjust_markers in insdel.c.
1830
1831 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
1832
1833 void
1834 transpose_markers (start1, end1, start2, end2)
1835 register int start1, end1, start2, end2;
1836 {
1837 register int amt1, amt2, diff, mpos;
1838 register Lisp_Object marker;
1839
1840 /* Update point as if it were a marker. */
1841 if (PT < start1)
1842 ;
1843 else if (PT < end1)
1844 TEMP_SET_PT (PT + (end2 - end1));
1845 else if (PT < start2)
1846 TEMP_SET_PT (PT + (end2 - start2) - (end1 - start1));
1847 else if (PT < end2)
1848 TEMP_SET_PT (PT - (start2 - start1));
1849
1850 /* We used to adjust the endpoints here to account for the gap, but that
1851 isn't good enough. Even if we assume the caller has tried to move the
1852 gap out of our way, it might still be at start1 exactly, for example;
1853 and that places it `inside' the interval, for our purposes. The amount
1854 of adjustment is nontrivial if there's a `denormalized' marker whose
1855 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
1856 the dirty work to Fmarker_position, below. */
1857
1858 /* The difference between the region's lengths */
1859 diff = (end2 - start2) - (end1 - start1);
1860
1861 /* For shifting each marker in a region by the length of the other
1862 * region plus the distance between the regions.
1863 */
1864 amt1 = (end2 - start2) + (start2 - end1);
1865 amt2 = (end1 - start1) + (start2 - end1);
1866
1867 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
1868 marker = XMARKER (marker)->chain)
1869 {
1870 mpos = Fmarker_position (marker);
1871 if (mpos >= start1 && mpos < end2)
1872 {
1873 if (mpos < end1)
1874 mpos += amt1;
1875 else if (mpos < start2)
1876 mpos += diff;
1877 else
1878 mpos -= amt2;
1879 if (mpos > GPT) mpos += GAP_SIZE;
1880 XMARKER (marker)->bufpos = mpos;
1881 }
1882 }
1883 }
1884
1885 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
1886 "Transpose region START1 to END1 with START2 to END2.\n\
1887 The regions may not be overlapping, because the size of the buffer is\n\
1888 never changed in a transposition.\n\
1889 \n\
1890 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
1891 any markers that happen to be located in the regions.\n\
1892 \n\
1893 Transposing beyond buffer boundaries is an error.")
1894 (startr1, endr1, startr2, endr2, leave_markers)
1895 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
1896 {
1897 register int start1, end1, start2, end2,
1898 gap, len1, len_mid, len2;
1899 unsigned char *start1_addr, *start2_addr, *temp;
1900
1901 #ifdef USE_TEXT_PROPERTIES
1902 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
1903 cur_intv = BUF_INTERVALS (current_buffer);
1904 #endif /* USE_TEXT_PROPERTIES */
1905
1906 validate_region (&startr1, &endr1);
1907 validate_region (&startr2, &endr2);
1908
1909 start1 = XFASTINT (startr1);
1910 end1 = XFASTINT (endr1);
1911 start2 = XFASTINT (startr2);
1912 end2 = XFASTINT (endr2);
1913 gap = GPT;
1914
1915 /* Swap the regions if they're reversed. */
1916 if (start2 < end1)
1917 {
1918 register int glumph = start1;
1919 start1 = start2;
1920 start2 = glumph;
1921 glumph = end1;
1922 end1 = end2;
1923 end2 = glumph;
1924 }
1925
1926 len1 = end1 - start1;
1927 len2 = end2 - start2;
1928
1929 if (start2 < end1)
1930 error ("transposed regions not properly ordered");
1931 else if (start1 == end1 || start2 == end2)
1932 error ("transposed region may not be of length 0");
1933
1934 /* The possibilities are:
1935 1. Adjacent (contiguous) regions, or separate but equal regions
1936 (no, really equal, in this case!), or
1937 2. Separate regions of unequal size.
1938
1939 The worst case is usually No. 2. It means that (aside from
1940 potential need for getting the gap out of the way), there also
1941 needs to be a shifting of the text between the two regions. So
1942 if they are spread far apart, we are that much slower... sigh. */
1943
1944 /* It must be pointed out that the really studly thing to do would
1945 be not to move the gap at all, but to leave it in place and work
1946 around it if necessary. This would be extremely efficient,
1947 especially considering that people are likely to do
1948 transpositions near where they are working interactively, which
1949 is exactly where the gap would be found. However, such code
1950 would be much harder to write and to read. So, if you are
1951 reading this comment and are feeling squirrely, by all means have
1952 a go! I just didn't feel like doing it, so I will simply move
1953 the gap the minimum distance to get it out of the way, and then
1954 deal with an unbroken array. */
1955
1956 /* Make sure the gap won't interfere, by moving it out of the text
1957 we will operate on. */
1958 if (start1 < gap && gap < end2)
1959 {
1960 if (gap - start1 < end2 - gap)
1961 move_gap (start1);
1962 else
1963 move_gap (end2);
1964 }
1965
1966 /* Hmmm... how about checking to see if the gap is large
1967 enough to use as the temporary storage? That would avoid an
1968 allocation... interesting. Later, don't fool with it now. */
1969
1970 /* Working without memmove, for portability (sigh), so must be
1971 careful of overlapping subsections of the array... */
1972
1973 if (end1 == start2) /* adjacent regions */
1974 {
1975 modify_region (current_buffer, start1, end2);
1976 record_change (start1, len1 + len2);
1977
1978 #ifdef USE_TEXT_PROPERTIES
1979 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
1980 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
1981 Fset_text_properties (start1, end2, Qnil, Qnil);
1982 #endif /* USE_TEXT_PROPERTIES */
1983
1984 /* First region smaller than second. */
1985 if (len1 < len2)
1986 {
1987 /* We use alloca only if it is small,
1988 because we want to avoid stack overflow. */
1989 if (len2 > 20000)
1990 temp = (unsigned char *) xmalloc (len2);
1991 else
1992 temp = (unsigned char *) alloca (len2);
1993
1994 /* Don't precompute these addresses. We have to compute them
1995 at the last minute, because the relocating allocator might
1996 have moved the buffer around during the xmalloc. */
1997 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
1998 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
1999
2000 bcopy (start2_addr, temp, len2);
2001 bcopy (start1_addr, start1_addr + len2, len1);
2002 bcopy (temp, start1_addr, len2);
2003 if (len2 > 20000)
2004 free (temp);
2005 }
2006 else
2007 /* First region not smaller than second. */
2008 {
2009 if (len1 > 20000)
2010 temp = (unsigned char *) xmalloc (len1);
2011 else
2012 temp = (unsigned char *) alloca (len1);
2013 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2014 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2015 bcopy (start1_addr, temp, len1);
2016 bcopy (start2_addr, start1_addr, len2);
2017 bcopy (temp, start1_addr + len2, len1);
2018 if (len1 > 20000)
2019 free (temp);
2020 }
2021 #ifdef USE_TEXT_PROPERTIES
2022 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
2023 len1, current_buffer, 0);
2024 graft_intervals_into_buffer (tmp_interval2, start1,
2025 len2, current_buffer, 0);
2026 #endif /* USE_TEXT_PROPERTIES */
2027 }
2028 /* Non-adjacent regions, because end1 != start2, bleagh... */
2029 else
2030 {
2031 if (len1 == len2)
2032 /* Regions are same size, though, how nice. */
2033 {
2034 modify_region (current_buffer, start1, end1);
2035 modify_region (current_buffer, start2, end2);
2036 record_change (start1, len1);
2037 record_change (start2, len2);
2038 #ifdef USE_TEXT_PROPERTIES
2039 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2040 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2041 Fset_text_properties (start1, end1, Qnil, Qnil);
2042 Fset_text_properties (start2, end2, Qnil, Qnil);
2043 #endif /* USE_TEXT_PROPERTIES */
2044
2045 if (len1 > 20000)
2046 temp = (unsigned char *) xmalloc (len1);
2047 else
2048 temp = (unsigned char *) alloca (len1);
2049 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2050 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2051 bcopy (start1_addr, temp, len1);
2052 bcopy (start2_addr, start1_addr, len2);
2053 bcopy (temp, start2_addr, len1);
2054 if (len1 > 20000)
2055 free (temp);
2056 #ifdef USE_TEXT_PROPERTIES
2057 graft_intervals_into_buffer (tmp_interval1, start2,
2058 len1, current_buffer, 0);
2059 graft_intervals_into_buffer (tmp_interval2, start1,
2060 len2, current_buffer, 0);
2061 #endif /* USE_TEXT_PROPERTIES */
2062 }
2063
2064 else if (len1 < len2) /* Second region larger than first */
2065 /* Non-adjacent & unequal size, area between must also be shifted. */
2066 {
2067 len_mid = start2 - end1;
2068 modify_region (current_buffer, start1, end2);
2069 record_change (start1, (end2 - start1));
2070 #ifdef USE_TEXT_PROPERTIES
2071 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2072 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2073 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2074 Fset_text_properties (start1, end2, Qnil, Qnil);
2075 #endif /* USE_TEXT_PROPERTIES */
2076
2077 /* holds region 2 */
2078 if (len2 > 20000)
2079 temp = (unsigned char *) xmalloc (len2);
2080 else
2081 temp = (unsigned char *) alloca (len2);
2082 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2083 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2084 bcopy (start2_addr, temp, len2);
2085 bcopy (start1_addr, start1_addr + len_mid + len2, len1);
2086 safe_bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2087 bcopy (temp, start1_addr, len2);
2088 if (len2 > 20000)
2089 free (temp);
2090 #ifdef USE_TEXT_PROPERTIES
2091 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2092 len1, current_buffer, 0);
2093 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2094 len_mid, current_buffer, 0);
2095 graft_intervals_into_buffer (tmp_interval2, start1,
2096 len2, current_buffer, 0);
2097 #endif /* USE_TEXT_PROPERTIES */
2098 }
2099 else
2100 /* Second region smaller than first. */
2101 {
2102 len_mid = start2 - end1;
2103 record_change (start1, (end2 - start1));
2104 modify_region (current_buffer, start1, end2);
2105
2106 #ifdef USE_TEXT_PROPERTIES
2107 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2108 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2109 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2110 Fset_text_properties (start1, end2, Qnil, Qnil);
2111 #endif /* USE_TEXT_PROPERTIES */
2112
2113 /* holds region 1 */
2114 if (len1 > 20000)
2115 temp = (unsigned char *) xmalloc (len1);
2116 else
2117 temp = (unsigned char *) alloca (len1);
2118 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2119 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2120 bcopy (start1_addr, temp, len1);
2121 bcopy (start2_addr, start1_addr, len2);
2122 bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2123 bcopy (temp, start1_addr + len2 + len_mid, len1);
2124 if (len1 > 20000)
2125 free (temp);
2126 #ifdef USE_TEXT_PROPERTIES
2127 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2128 len1, current_buffer, 0);
2129 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2130 len_mid, current_buffer, 0);
2131 graft_intervals_into_buffer (tmp_interval2, start1,
2132 len2, current_buffer, 0);
2133 #endif /* USE_TEXT_PROPERTIES */
2134 }
2135 }
2136
2137 /* todo: this will be slow, because for every transposition, we
2138 traverse the whole friggin marker list. Possible solutions:
2139 somehow get a list of *all* the markers across multiple
2140 transpositions and do it all in one swell phoop. Or maybe modify
2141 Emacs' marker code to keep an ordered list or tree. This might
2142 be nicer, and more beneficial in the long run, but would be a
2143 bunch of work. Plus the way they're arranged now is nice. */
2144 if (NILP (leave_markers))
2145 {
2146 transpose_markers (start1, end1, start2, end2);
2147 fix_overlays_in_range (start1, end2);
2148 }
2149
2150 return Qnil;
2151 }
2152
2153 \f
2154 void
2155 syms_of_editfns ()
2156 {
2157 DEFVAR_LISP ("system-name", &Vsystem_name,
2158 "The name of the machine Emacs is running on.");
2159
2160 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
2161 "The full name of the user logged in.");
2162
2163 DEFVAR_LISP ("user-name", &Vuser_name,
2164 "The user's name, taken from environment variables if possible.");
2165
2166 DEFVAR_LISP ("user-real-name", &Vuser_real_name,
2167 "The user's name, based upon the real uid only.");
2168
2169 defsubr (&Schar_equal);
2170 defsubr (&Sgoto_char);
2171 defsubr (&Sstring_to_char);
2172 defsubr (&Schar_to_string);
2173 defsubr (&Sbuffer_substring);
2174 defsubr (&Sbuffer_string);
2175
2176 defsubr (&Spoint_marker);
2177 defsubr (&Smark_marker);
2178 defsubr (&Spoint);
2179 defsubr (&Sregion_beginning);
2180 defsubr (&Sregion_end);
2181 /* defsubr (&Smark); */
2182 /* defsubr (&Sset_mark); */
2183 defsubr (&Ssave_excursion);
2184
2185 defsubr (&Sbufsize);
2186 defsubr (&Spoint_max);
2187 defsubr (&Spoint_min);
2188 defsubr (&Spoint_min_marker);
2189 defsubr (&Spoint_max_marker);
2190
2191 defsubr (&Sbobp);
2192 defsubr (&Seobp);
2193 defsubr (&Sbolp);
2194 defsubr (&Seolp);
2195 defsubr (&Sfollowing_char);
2196 defsubr (&Sprevious_char);
2197 defsubr (&Schar_after);
2198 defsubr (&Sinsert);
2199 defsubr (&Sinsert_before_markers);
2200 defsubr (&Sinsert_and_inherit);
2201 defsubr (&Sinsert_and_inherit_before_markers);
2202 defsubr (&Sinsert_char);
2203
2204 defsubr (&Suser_login_name);
2205 defsubr (&Suser_real_login_name);
2206 defsubr (&Suser_uid);
2207 defsubr (&Suser_real_uid);
2208 defsubr (&Suser_full_name);
2209 defsubr (&Semacs_pid);
2210 defsubr (&Scurrent_time);
2211 defsubr (&Sformat_time_string);
2212 defsubr (&Sdecode_time);
2213 defsubr (&Scurrent_time_string);
2214 defsubr (&Scurrent_time_zone);
2215 defsubr (&Ssystem_name);
2216 defsubr (&Smessage);
2217 defsubr (&Smessage_box);
2218 defsubr (&Smessage_or_box);
2219 defsubr (&Sformat);
2220
2221 defsubr (&Sinsert_buffer_substring);
2222 defsubr (&Scompare_buffer_substrings);
2223 defsubr (&Ssubst_char_in_region);
2224 defsubr (&Stranslate_region);
2225 defsubr (&Sdelete_region);
2226 defsubr (&Swiden);
2227 defsubr (&Snarrow_to_region);
2228 defsubr (&Ssave_restriction);
2229 defsubr (&Stranspose_regions);
2230 }