*** empty log message ***
[bpt/emacs.git] / src / editfns.c
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985, 1986, 1987, 1989 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 "config.h"
22
23 #ifdef VMS
24 #include "pwd.h"
25 #else
26 #include <pwd.h>
27 #endif
28
29 #include "lisp.h"
30 #include "buffer.h"
31 #include "window.h"
32
33 #ifdef NEED_TIME_H
34 #include <time.h>
35 #else /* not NEED_TIME_H */
36 #ifdef HAVE_TIMEVAL
37 #include <sys/time.h>
38 #endif /* HAVE_TIMEVAL */
39 #endif /* not NEED_TIME_H */
40
41 #define min(a, b) ((a) < (b) ? (a) : (b))
42 #define max(a, b) ((a) > (b) ? (a) : (b))
43
44 /* Some static data, and a function to initialize it for each run */
45
46 Lisp_Object Vsystem_name;
47 Lisp_Object Vuser_real_name; /* login name of current user ID */
48 Lisp_Object Vuser_full_name; /* full name of current user */
49 Lisp_Object Vuser_name; /* user name from USER or LOGNAME. */
50
51 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
62 Vsystem_name = build_string (get_system_name ());
63 p = XSTRING (Vsystem_name)->data;
64 while (*p)
65 {
66 if (*p == ' ' || *p == '\t')
67 *p = '-';
68 p++;
69 }
70
71 #ifndef CANNOT_DUMP
72 /* Don't bother with this on initial start when just dumping out */
73 if (!initialized)
74 return;
75 #endif /* not CANNOT_DUMP */
76
77 pw = (struct passwd *) getpwuid (getuid ());
78 Vuser_real_name = build_string (pw ? pw->pw_name : "unknown");
79
80 /* Get the effective user name, by consulting environment variables,
81 or the effective uid if those are unset. */
82 user_name = (char *) getenv ("USER");
83 if (!user_name)
84 user_name = (char *) getenv ("LOGNAME");
85 if (!user_name)
86 {
87 pw = (struct passwd *) getpwuid (geteuid ());
88 user_name = (char *) (pw ? pw->pw_name : "unknown");
89 }
90 Vuser_name = build_string (user_name);
91
92 /* If the user name claimed in the environment vars differs from
93 the real uid, use the claimed name to find the full name. */
94 tem = Fstring_equal (Vuser_name, Vuser_real_name);
95 if (NULL (tem))
96 pw = (struct passwd *) getpwnam (XSTRING (Vuser_name)->data);
97
98 p = (unsigned char *) (pw ? USER_FULL_NAME : "unknown");
99 q = (unsigned char *) index (p, ',');
100 Vuser_full_name = make_string (p, q ? q - p : strlen (p));
101
102 #ifdef AMPERSAND_FULL_NAME
103 p = XSTRING (Vuser_full_name)->data;
104 q = (char *) index (p, '&');
105 /* Substitute the login name for the &, upcasing the first character. */
106 if (q)
107 {
108 r = (char *) alloca (strlen (p) + XSTRING (Vuser_name)->size + 1);
109 bcopy (p, r, q - p);
110 r[q - p] = 0;
111 strcat (r, XSTRING (Vuser_name)->data);
112 r[q - p] = UPCASE (r[q - p]);
113 strcat (r, q + 1);
114 Vuser_full_name = build_string (r);
115 }
116 #endif /* AMPERSAND_FULL_NAME */
117 }
118 \f
119 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
120 "Convert arg CHAR to a one-character string containing that character.")
121 (n)
122 Lisp_Object n;
123 {
124 char c;
125 CHECK_NUMBER (n, 0);
126
127 c = XINT (n);
128 return make_string (&c, 1);
129 }
130
131 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
132 "Convert arg STRING to a character, the first character of that string.")
133 (str)
134 register Lisp_Object str;
135 {
136 register Lisp_Object val;
137 register struct Lisp_String *p;
138 CHECK_STRING (str, 0);
139
140 p = XSTRING (str);
141 if (p->size)
142 XFASTINT (val) = ((unsigned char *) p->data)[0];
143 else
144 XFASTINT (val) = 0;
145 return val;
146 }
147 \f
148 static Lisp_Object
149 buildmark (val)
150 int val;
151 {
152 register Lisp_Object mark;
153 mark = Fmake_marker ();
154 Fset_marker (mark, make_number (val), Qnil);
155 return mark;
156 }
157
158 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
159 "Return value of point, as an integer.\n\
160 Beginning of buffer is position (point-min)")
161 ()
162 {
163 Lisp_Object temp;
164 XFASTINT (temp) = point;
165 return temp;
166 }
167
168 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
169 "Return value of point, as a marker object.")
170 ()
171 {
172 return buildmark (point);
173 }
174
175 int
176 clip_to_bounds (lower, num, upper)
177 int lower, num, upper;
178 {
179 if (num < lower)
180 return lower;
181 else if (num > upper)
182 return upper;
183 else
184 return num;
185 }
186
187 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
188 "Set point to POSITION, a number or marker.\n\
189 Beginning of buffer is position (point-min), end is (point-max).")
190 (n)
191 register Lisp_Object n;
192 {
193 CHECK_NUMBER_COERCE_MARKER (n, 0);
194
195 SET_PT (clip_to_bounds (BEGV, XINT (n), ZV));
196 return n;
197 }
198
199 static Lisp_Object
200 region_limit (beginningp)
201 int beginningp;
202 {
203 register Lisp_Object m;
204 m = Fmarker_position (current_buffer->mark);
205 if (NULL (m)) error ("There is no region now");
206 if ((point < XFASTINT (m)) == beginningp)
207 return (make_number (point));
208 else
209 return (m);
210 }
211
212 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
213 "Return position of beginning of region, as an integer.")
214 ()
215 {
216 return (region_limit (1));
217 }
218
219 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
220 "Return position of end of region, as an integer.")
221 ()
222 {
223 return (region_limit (0));
224 }
225
226 #if 0 /* now in lisp code */
227 DEFUN ("mark", Fmark, Smark, 0, 0, 0,
228 "Return this buffer's mark value as integer, or nil if no mark.\n\
229 If you are using this in an editing command, you are most likely making\n\
230 a mistake; see the documentation of `set-mark'.")
231 ()
232 {
233 return Fmarker_position (current_buffer->mark);
234 }
235 #endif /* commented out code */
236
237 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
238 "Return this buffer's mark, as a marker object.\n\
239 Watch out! Moving this marker changes the mark position.\n\
240 If you set the marker not to point anywhere, the buffer will have no mark.")
241 ()
242 {
243 return current_buffer->mark;
244 }
245
246 #if 0 /* this is now in lisp code */
247 DEFUN ("set-mark", Fset_mark, Sset_mark, 1, 1, 0,
248 "Set this buffer's mark to POS. Don't use this function!\n\
249 That is to say, don't use this function unless you want\n\
250 the user to see that the mark has moved, and you want the previous\n\
251 mark position to be lost.\n\
252 \n\
253 Normally, when a new mark is set, the old one should go on the stack.\n\
254 This is why most applications should use push-mark, not set-mark.\n\
255 \n\
256 Novice programmers often try to use the mark for the wrong purposes.\n\
257 The mark saves a location for the user's convenience.\n\
258 Most editing commands should not alter the mark.\n\
259 To remember a location for internal use in the Lisp program,\n\
260 store it in a Lisp variable. Example:\n\
261 \n\
262 (let ((beg (point))) (forward-line 1) (delete-region beg (point))).")
263 (pos)
264 Lisp_Object pos;
265 {
266 if (NULL (pos))
267 {
268 current_buffer->mark = Qnil;
269 return Qnil;
270 }
271 CHECK_NUMBER_COERCE_MARKER (pos, 0);
272
273 if (NULL (current_buffer->mark))
274 current_buffer->mark = Fmake_marker ();
275
276 Fset_marker (current_buffer->mark, pos, Qnil);
277 return pos;
278 }
279 #endif /* commented-out code */
280
281 Lisp_Object
282 save_excursion_save ()
283 {
284 register int visible = XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer;
285
286 return Fcons (Fpoint_marker (),
287 Fcons (Fcopy_marker (current_buffer->mark), visible ? Qt : Qnil));
288 }
289
290 Lisp_Object
291 save_excursion_restore (info)
292 register Lisp_Object info;
293 {
294 register Lisp_Object tem;
295
296 tem = Fmarker_buffer (Fcar (info));
297 /* If buffer being returned to is now deleted, avoid error */
298 /* Otherwise could get error here while unwinding to top level
299 and crash */
300 /* In that case, Fmarker_buffer returns nil now. */
301 if (NULL (tem))
302 return Qnil;
303 Fset_buffer (tem);
304 tem = Fcar (info);
305 Fgoto_char (tem);
306 unchain_marker (tem);
307 tem = Fcar (Fcdr (info));
308 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
309 unchain_marker (tem);
310 tem = Fcdr (Fcdr (info));
311 if (!NULL (tem) && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
312 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
313 return Qnil;
314 }
315
316 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
317 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
318 Executes BODY just like `progn'.\n\
319 The values of point, mark and the current buffer are restored\n\
320 even in case of abnormal exit (throw or error).")
321 (args)
322 Lisp_Object args;
323 {
324 register Lisp_Object val;
325 int count = specpdl_ptr - specpdl;
326
327 record_unwind_protect (save_excursion_restore, save_excursion_save ());
328
329 val = Fprogn (args);
330 return unbind_to (count, val);
331 }
332 \f
333 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
334 "Return the number of characters in the current buffer.")
335 ()
336 {
337 Lisp_Object temp;
338 XFASTINT (temp) = Z - BEG;
339 return temp;
340 }
341
342 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
343 "Return the minimum permissible value of point in the current buffer.\n\
344 This is 1, unless a clipping restriction is in effect.")
345 ()
346 {
347 Lisp_Object temp;
348 XFASTINT (temp) = BEGV;
349 return temp;
350 }
351
352 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
353 "Return a marker to the minimum permissible value of point in this buffer.\n\
354 This is the beginning, unless a clipping restriction is in effect.")
355 ()
356 {
357 return buildmark (BEGV);
358 }
359
360 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
361 "Return the maximum permissible value of point in the current buffer.\n\
362 This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
363 in which case it is less.")
364 ()
365 {
366 Lisp_Object temp;
367 XFASTINT (temp) = ZV;
368 return temp;
369 }
370
371 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
372 "Return a marker to the maximum permissible value of point in this buffer.\n\
373 This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
374 in which case it is less.")
375 ()
376 {
377 return buildmark (ZV);
378 }
379
380 DEFUN ("following-char", Ffollchar, Sfollchar, 0, 0, 0,
381 "Return the character following point, as a number.")
382 ()
383 {
384 Lisp_Object temp;
385 XFASTINT (temp) = FETCH_CHAR (point);
386 return temp;
387 }
388
389 DEFUN ("preceding-char", Fprevchar, Sprevchar, 0, 0, 0,
390 "Return the character preceding point, as a number.")
391 ()
392 {
393 Lisp_Object temp;
394 if (point <= BEGV)
395 XFASTINT (temp) = 0;
396 else
397 XFASTINT (temp) = FETCH_CHAR (point - 1);
398 return temp;
399 }
400
401 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
402 "Return T if point is at the beginning of the buffer.\n\
403 If the buffer is narrowed, this means the beginning of the narrowed part.")
404 ()
405 {
406 if (point == BEGV)
407 return Qt;
408 return Qnil;
409 }
410
411 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
412 "Return T if point is at the end of the buffer.\n\
413 If the buffer is narrowed, this means the end of the narrowed part.")
414 ()
415 {
416 if (point == ZV)
417 return Qt;
418 return Qnil;
419 }
420
421 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
422 "Return T if point is at the beginning of a line.")
423 ()
424 {
425 if (point == BEGV || FETCH_CHAR (point - 1) == '\n')
426 return Qt;
427 return Qnil;
428 }
429
430 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
431 "Return T if point is at the end of a line.\n\
432 `End of a line' includes point being at the end of the buffer.")
433 ()
434 {
435 if (point == ZV || FETCH_CHAR (point) == '\n')
436 return Qt;
437 return Qnil;
438 }
439
440 DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
441 "Return character in current buffer at position POS.\n\
442 POS is an integer or a buffer pointer.\n\
443 If POS is out of range, the value is nil.")
444 (pos)
445 Lisp_Object pos;
446 {
447 register Lisp_Object val;
448 register int n;
449
450 CHECK_NUMBER_COERCE_MARKER (pos, 0);
451
452 n = XINT (pos);
453 if (n < BEGV || n >= ZV) return Qnil;
454
455 XFASTINT (val) = FETCH_CHAR (n);
456 return val;
457 }
458 \f
459 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 0, 0,
460 "Return the name under which the user logged in, as a string.\n\
461 This is based on the effective uid, not the real uid.\n\
462 Also, if the environment variable USER or LOGNAME is set,\n\
463 that determines the value of this function.")
464 ()
465 {
466 return Vuser_name;
467 }
468
469 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
470 0, 0, 0,
471 "Return the name of the user's real uid, as a string.\n\
472 Differs from `user-login-name' when running under `su'.")
473 ()
474 {
475 return Vuser_real_name;
476 }
477
478 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
479 "Return the effective uid of Emacs, as an integer.")
480 ()
481 {
482 return make_number (geteuid ());
483 }
484
485 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
486 "Return the real uid of Emacs, as an integer.")
487 ()
488 {
489 return make_number (getuid ());
490 }
491
492 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, 0,
493 "Return the full name of the user logged in, as a string.")
494 ()
495 {
496 return Vuser_full_name;
497 }
498
499 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
500 "Return the name of the machine you are running on, as a string.")
501 ()
502 {
503 return Vsystem_name;
504 }
505
506 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 0, 0,
507 "Return the current time, as a human-readable string.\n\
508 Programs can use it too, since the number of columns in each field is fixed.\n\
509 The format is `Sun Sep 16 01:03:52 1973'.\n\
510 In a future Emacs version, the time zone may be added at the end,\n\
511 if we can figure out a reasonably easy way to get that information.")
512 ()
513 {
514 long current_time = time ((long *) 0);
515 char buf[30];
516 register char *tem = (char *) ctime (&current_time);
517
518 strncpy (buf, tem, 24);
519 buf[24] = 0;
520
521 return build_string (buf);
522 }
523
524 #ifdef unix
525
526 DEFUN ("set-default-file-mode", Fset_default_file_mode, Sset_default_file_mode, 1, 1, "p",
527 "Set Unix `umask' value to ARGUMENT, and return old value.\n\
528 The `umask' value is the default protection mode for new files.")
529 (nmask)
530 Lisp_Object nmask;
531 {
532 CHECK_NUMBER (nmask, 0);
533 return make_number (umask (XINT (nmask)));
534 }
535
536 DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "",
537 "Tell Unix to finish all pending disk updates.")
538 ()
539 {
540 sync ();
541 return Qnil;
542 }
543
544 #endif /* unix */
545 \f
546 void
547 insert1 (arg)
548 Lisp_Object arg;
549 {
550 Finsert (1, &arg);
551 }
552
553
554 /* Callers passing one argument to Finsert need not gcpro the
555 argument "array", since the only element of the array will
556 not be used after calling insert or insert_from_string, so
557 we don't care if it gets trashed. */
558
559 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
560 "Insert the arguments, either strings or characters, at point.\n\
561 Point moves forward so that it ends up after the inserted text.\n\
562 Any other markers at the point of insertion remain before the text.")
563 (nargs, args)
564 int nargs;
565 register Lisp_Object *args;
566 {
567 register int argnum;
568 register Lisp_Object tem;
569 char str[1];
570
571 for (argnum = 0; argnum < nargs; argnum++)
572 {
573 tem = args[argnum];
574 retry:
575 if (XTYPE (tem) == Lisp_Int)
576 {
577 str[0] = XINT (tem);
578 insert (str, 1);
579 }
580 else if (XTYPE (tem) == Lisp_String)
581 {
582 insert_from_string (tem, 0, XSTRING (tem)->size);
583 }
584 else
585 {
586 tem = wrong_type_argument (Qchar_or_string_p, tem);
587 goto retry;
588 }
589 }
590
591 return Qnil;
592 }
593
594 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
595 "Insert strings or characters at point, relocating markers after the text.\n\
596 Point moves forward so that it ends up after the inserted text.\n\
597 Any other markers at the point of insertion also end up after the text.")
598 (nargs, args)
599 int nargs;
600 register Lisp_Object *args;
601 {
602 register int argnum;
603 register Lisp_Object tem;
604 char str[1];
605
606 for (argnum = 0; argnum < nargs; argnum++)
607 {
608 tem = args[argnum];
609 retry:
610 if (XTYPE (tem) == Lisp_Int)
611 {
612 str[0] = XINT (tem);
613 insert_before_markers (str, 1);
614 }
615 else if (XTYPE (tem) == Lisp_String)
616 {
617 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size);
618 }
619 else
620 {
621 tem = wrong_type_argument (Qchar_or_string_p, tem);
622 goto retry;
623 }
624 }
625
626 return Qnil;
627 }
628 \f
629 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 2, 0,
630 "Insert COUNT (second arg) copies of CHAR (first arg).\n\
631 Point and all markers are affected as in the function `insert'.\n\
632 Both arguments are required.")
633 (chr, count)
634 Lisp_Object chr, count;
635 {
636 register unsigned char *string;
637 register int strlen;
638 register int i, n;
639
640 CHECK_NUMBER (chr, 0);
641 CHECK_NUMBER (count, 1);
642
643 n = XINT (count);
644 if (n <= 0)
645 return Qnil;
646 strlen = min (n, 256);
647 string = (unsigned char *) alloca (strlen);
648 for (i = 0; i < strlen; i++)
649 string[i] = XFASTINT (chr);
650 while (n >= strlen)
651 {
652 insert (string, strlen);
653 n -= strlen;
654 }
655 if (n > 0)
656 insert (string, n);
657 return Qnil;
658 }
659
660 \f
661 /* Return a string with the contents of the current region */
662
663 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
664 "Return the contents of part of the current buffer as a string.\n\
665 The two arguments START and END are character positions;\n\
666 they can be in either order.")
667 (b, e)
668 Lisp_Object b, e;
669 {
670 register int beg, end;
671 Lisp_Object result;
672
673 validate_region (&b, &e);
674 beg = XINT (b);
675 end = XINT (e);
676
677 if (beg < GPT && end > GPT)
678 move_gap (beg);
679
680 /* Plain old make_string calls make_uninit_string, which can cause
681 the buffer arena to be compacted. make_string has no way of
682 knowing that the data has been moved, and thus copies the wrong
683 data into the string. This doesn't effect most of the other
684 users of make_string, so it should be left as is. */
685 result = make_uninit_string (end - beg);
686 bcopy (&FETCH_CHAR (beg), XSTRING (result)->data, end - beg);
687
688 return result;
689 }
690
691 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
692 "Return the contents of the current buffer as a string.")
693 ()
694 {
695 if (BEGV < GPT && ZV > GPT)
696 move_gap (BEGV);
697 return make_string (BEGV_ADDR, ZV - BEGV);
698 }
699
700 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
701 1, 3, 0,
702 "Insert before point a substring of the contents buffer BUFFER.\n\
703 BUFFER may be a buffer or a buffer name.\n\
704 Arguments START and END are character numbers specifying the substring.\n\
705 They default to the beginning and the end of BUFFER.")
706 (buf, b, e)
707 Lisp_Object buf, b, e;
708 {
709 register int beg, end, exch;
710 register struct buffer *bp;
711
712 buf = Fget_buffer (buf);
713 bp = XBUFFER (buf);
714
715 if (NULL (b))
716 beg = BUF_BEGV (bp);
717 else
718 {
719 CHECK_NUMBER_COERCE_MARKER (b, 0);
720 beg = XINT (b);
721 }
722 if (NULL (e))
723 end = BUF_ZV (bp);
724 else
725 {
726 CHECK_NUMBER_COERCE_MARKER (e, 1);
727 end = XINT (e);
728 }
729
730 if (beg > end)
731 exch = beg, beg = end, end = exch;
732
733 /* Move the gap or create enough gap in the current buffer. */
734
735 if (point != GPT)
736 move_gap (point);
737 if (GAP_SIZE < end - beg)
738 make_gap (end - beg - GAP_SIZE);
739
740 if (!(BUF_BEGV (bp) <= beg
741 && beg <= end
742 && end <= BUF_ZV (bp)))
743 args_out_of_range (b, e);
744
745 /* Now the actual insertion will not do any gap motion,
746 so it matters not if BUF is the current buffer. */
747 if (beg < BUF_GPT (bp))
748 {
749 insert (BUF_CHAR_ADDRESS (bp, beg), min (end, BUF_GPT (bp)) - beg);
750 beg = min (end, BUF_GPT (bp));
751 }
752 if (beg < end)
753 insert (BUF_CHAR_ADDRESS (bp, beg), end - beg);
754
755 return Qnil;
756 }
757 \f
758 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
759 Ssubst_char_in_region, 4, 5, 0,
760 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
761 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
762 and don't mark the buffer as really changed.")
763 (start, end, fromchar, tochar, noundo)
764 Lisp_Object start, end, fromchar, tochar, noundo;
765 {
766 register int pos, stop, look;
767
768 validate_region (&start, &end);
769 CHECK_NUMBER (fromchar, 2);
770 CHECK_NUMBER (tochar, 3);
771
772 pos = XINT (start);
773 stop = XINT (end);
774 look = XINT (fromchar);
775
776 modify_region (pos, stop);
777 if (! NULL (noundo))
778 {
779 if (MODIFF - 1 == current_buffer->save_modified)
780 current_buffer->save_modified++;
781 if (MODIFF - 1 == current_buffer->auto_save_modified)
782 current_buffer->auto_save_modified++;
783 }
784
785 while (pos < stop)
786 {
787 if (FETCH_CHAR (pos) == look)
788 {
789 if (NULL (noundo))
790 record_change (pos, 1);
791 FETCH_CHAR (pos) = XINT (tochar);
792 if (NULL (noundo))
793 signal_after_change (pos, 1, 1);
794 }
795 pos++;
796 }
797
798 return Qnil;
799 }
800
801 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
802 "From START to END, translate characters according to TABLE.\n\
803 TABLE is a string; the Nth character in it is the mapping\n\
804 for the character with code N. Returns the number of characters changed.")
805 (start, end, table)
806 Lisp_Object start;
807 Lisp_Object end;
808 register Lisp_Object table;
809 {
810 register int pos, stop; /* Limits of the region. */
811 register unsigned char *tt; /* Trans table. */
812 register int oc; /* Old character. */
813 register int nc; /* New character. */
814 int cnt; /* Number of changes made. */
815 Lisp_Object z; /* Return. */
816 int size; /* Size of translate table. */
817
818 validate_region (&start, &end);
819 CHECK_STRING (table, 2);
820
821 size = XSTRING (table)->size;
822 tt = XSTRING (table)->data;
823
824 pos = XINT (start);
825 stop = XINT (end);
826 modify_region (pos, stop);
827
828 cnt = 0;
829 for (; pos < stop; ++pos)
830 {
831 oc = FETCH_CHAR (pos);
832 if (oc < size)
833 {
834 nc = tt[oc];
835 if (nc != oc)
836 {
837 record_change (pos, 1);
838 FETCH_CHAR (pos) = nc;
839 signal_after_change (pos, 1, 1);
840 ++cnt;
841 }
842 }
843 }
844
845 XFASTINT (z) = cnt;
846 return (z);
847 }
848
849 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
850 "Delete the text between point and mark.\n\
851 When called from a program, expects two arguments,\n\
852 positions (integers or markers) specifying the stretch to be deleted.")
853 (b, e)
854 Lisp_Object b, e;
855 {
856 validate_region (&b, &e);
857 del_range (XINT (b), XINT (e));
858 return Qnil;
859 }
860 \f
861 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
862 "Remove restrictions (narrowing) from current buffer.\n\
863 This allows the buffer's full text to be seen and edited.")
864 ()
865 {
866 BEGV = BEG;
867 SET_BUF_ZV (current_buffer, Z);
868 clip_changed = 1;
869 /* Changing the buffer bounds invalidates any recorded current column. */
870 invalidate_current_column ();
871 return Qnil;
872 }
873
874 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
875 "Restrict editing in this buffer to the current region.\n\
876 The rest of the text becomes temporarily invisible and untouchable\n\
877 but is not deleted; if you save the buffer in a file, the invisible\n\
878 text is included in the file. \\[widen] makes all visible again.\n\
879 See also `save-restriction'.\n\
880 \n\
881 When calling from a program, pass two arguments; positions (integers\n\
882 or markers) bounding the text that should remain visible.")
883 (b, e)
884 register Lisp_Object b, e;
885 {
886 register int i;
887
888 CHECK_NUMBER_COERCE_MARKER (b, 0);
889 CHECK_NUMBER_COERCE_MARKER (e, 1);
890
891 if (XINT (b) > XINT (e))
892 {
893 i = XFASTINT (b);
894 b = e;
895 XFASTINT (e) = i;
896 }
897
898 if (!(BEG <= XINT (b) && XINT (b) <= XINT (e) && XINT (e) <= Z))
899 args_out_of_range (b, e);
900
901 BEGV = XFASTINT (b);
902 SET_BUF_ZV (current_buffer, XFASTINT (e));
903 if (point < XFASTINT (b))
904 SET_PT (XFASTINT (b));
905 if (point > XFASTINT (e))
906 SET_PT (XFASTINT (e));
907 clip_changed = 1;
908 /* Changing the buffer bounds invalidates any recorded current column. */
909 invalidate_current_column ();
910 return Qnil;
911 }
912
913 Lisp_Object
914 save_restriction_save ()
915 {
916 register Lisp_Object bottom, top;
917 /* Note: I tried using markers here, but it does not win
918 because insertion at the end of the saved region
919 does not advance mh and is considered "outside" the saved region. */
920 XFASTINT (bottom) = BEGV - BEG;
921 XFASTINT (top) = Z - ZV;
922
923 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
924 }
925
926 Lisp_Object
927 save_restriction_restore (data)
928 Lisp_Object data;
929 {
930 register struct buffer *buf;
931 register int newhead, newtail;
932 register Lisp_Object tem;
933
934 buf = XBUFFER (XCONS (data)->car);
935
936 data = XCONS (data)->cdr;
937
938 tem = XCONS (data)->car;
939 newhead = XINT (tem);
940 tem = XCONS (data)->cdr;
941 newtail = XINT (tem);
942 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
943 {
944 newhead = 0;
945 newtail = 0;
946 }
947 BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
948 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
949 clip_changed = 1;
950
951 /* If point is outside the new visible range, move it inside. */
952 SET_BUF_PT (buf,
953 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
954
955 return Qnil;
956 }
957
958 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
959 "Execute BODY, saving and restoring current buffer's restrictions.\n\
960 The buffer's restrictions make parts of the beginning and end invisible.\n\
961 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
962 This special form, `save-restriction', saves the current buffer's restrictions\n\
963 when it is entered, and restores them when it is exited.\n\
964 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
965 The old restrictions settings are restored\n\
966 even in case of abnormal exit (throw or error).\n\
967 \n\
968 The value returned is the value of the last form in BODY.\n\
969 \n\
970 `save-restriction' can get confused if, within the BODY, you widen\n\
971 and then make changes outside the area within the saved restrictions.\n\
972 \n\
973 Note: if you are using both `save-excursion' and `save-restriction',\n\
974 use `save-excursion' outermost:\n\
975 (save-excursion (save-restriction ...))")
976 (body)
977 Lisp_Object body;
978 {
979 register Lisp_Object val;
980 int count = specpdl_ptr - specpdl;
981
982 record_unwind_protect (save_restriction_restore, save_restriction_save ());
983 val = Fprogn (body);
984 return unbind_to (count, val);
985 }
986 \f
987 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
988 "Print a one-line message at the bottom of the screen.\n\
989 The first argument is a control string.\n\
990 It may contain %s or %d or %c to print successive following arguments.\n\
991 %s means print an argument as a string, %d means print as number in decimal,\n\
992 %c means print a number as a single character.\n\
993 The argument used by %s must be a string or a symbol;\n\
994 the argument used by %d or %c must be a number.")
995 (nargs, args)
996 int nargs;
997 Lisp_Object *args;
998 {
999 register Lisp_Object val;
1000
1001 #ifdef MULTI_SCREEN
1002 extern Lisp_Object Vglobal_minibuffer_screen;
1003
1004 if (XTYPE (Vglobal_minibuffer_screen) == Lisp_Screen)
1005 Fmake_screen_visible (Vglobal_minibuffer_screen);
1006 #endif
1007
1008 val = Fformat (nargs, args);
1009 message ("%s", XSTRING (val)->data);
1010 return val;
1011 }
1012
1013 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
1014 "Format a string out of a control-string and arguments.\n\
1015 The first argument is a control string.\n\
1016 The other arguments are substituted into it to make the result, a string.\n\
1017 It may contain %-sequences meaning to substitute the next argument.\n\
1018 %s means print a string argument. Actually, prints any object, with `princ'.\n\
1019 %d means print as number in decimal (%o octal, %x hex).\n\
1020 %c means print a number as a single character.\n\
1021 %S means print any object as an s-expression (using prin1).\n\
1022 The argument used for %d, %o, %x or %c must be a number.\n\
1023 Use %% to put a single % into the output.")
1024 (nargs, args)
1025 int nargs;
1026 register Lisp_Object *args;
1027 {
1028 register int n; /* The number of the next arg to substitute */
1029 register int total = 5; /* An estimate of the final length */
1030 char *buf;
1031 register unsigned char *format, *end;
1032 int length;
1033 extern char *index ();
1034 /* It should not be necessary to GCPRO ARGS, because
1035 the caller in the interpreter should take care of that. */
1036
1037 CHECK_STRING (args[0], 0);
1038 format = XSTRING (args[0])->data;
1039 end = format + XSTRING (args[0])->size;
1040
1041 n = 0;
1042 while (format != end)
1043 if (*format++ == '%')
1044 {
1045 int minlen;
1046
1047 /* Process a numeric arg and skip it. */
1048 minlen = atoi (format);
1049 if (minlen > 0)
1050 total += minlen;
1051 else
1052 total -= minlen;
1053 while ((*format >= '0' && *format <= '9')
1054 || *format == '-' || *format == ' ' || *format == '.')
1055 format++;
1056
1057 if (*format == '%')
1058 format++;
1059 else if (++n >= nargs)
1060 ;
1061 else if (*format == 'S')
1062 {
1063 /* For `S', prin1 the argument and then treat like a string. */
1064 register Lisp_Object tem;
1065 tem = Fprin1_to_string (args[n], Qnil);
1066 args[n] = tem;
1067 goto string;
1068 }
1069 else if (XTYPE (args[n]) == Lisp_Symbol)
1070 {
1071 XSET (args[n], Lisp_String, XSYMBOL (args[n])->name);
1072 goto string;
1073 }
1074 else if (XTYPE (args[n]) == Lisp_String)
1075 {
1076 string:
1077 total += XSTRING (args[n])->size;
1078 }
1079 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
1080 else if (XTYPE (args[n]) == Lisp_Int && *format != 's')
1081 {
1082 /* The following loop issumes the Lisp type indicates
1083 the proper way to pass the argument.
1084 So make sure we have a flonum if the argument should
1085 be a double. */
1086 if (*format == 'e' || *format == 'f' || *format == 'g')
1087 args[n] = Ffloat (args[n]);
1088 total += 10;
1089 }
1090 else if (XTYPE (args[n]) == Lisp_Float && *format != 's')
1091 {
1092 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
1093 args[n] = Ftruncate (args[n]);
1094 total += 20;
1095 }
1096 else
1097 {
1098 /* Anything but a string, convert to a string using princ. */
1099 register Lisp_Object tem;
1100 tem = Fprin1_to_string (args[n], Qt);
1101 args[n] = tem;
1102 goto string;
1103 }
1104 }
1105
1106 {
1107 register int nstrings = n + 1;
1108 register unsigned char **strings
1109 = (unsigned char **) alloca (nstrings * sizeof (unsigned char *));
1110
1111 for (n = 0; n < nstrings; n++)
1112 {
1113 if (n >= nargs)
1114 strings[n] = (unsigned char *) "";
1115 else if (XTYPE (args[n]) == Lisp_Int)
1116 /* We checked above that the corresponding format effector
1117 isn't %s, which would cause MPV. */
1118 strings[n] = (unsigned char *) XINT (args[n]);
1119 else if (XTYPE (args[n]) == Lisp_Float)
1120 {
1121 union { double d; int half[2]; } u;
1122
1123 u.d = XFLOAT (args[n])->data;
1124 strings[n++] = (unsigned char *) u.half[0];
1125 strings[n] = (unsigned char *) u.half[1];
1126 }
1127 else
1128 strings[n] = XSTRING (args[n])->data;
1129 }
1130
1131 /* Format it in bigger and bigger buf's until it all fits. */
1132 while (1)
1133 {
1134 buf = (char *) alloca (total + 1);
1135 buf[total - 1] = 0;
1136
1137 length = doprnt (buf, total + 1, strings[0], end, nargs, strings + 1);
1138 if (buf[total - 1] == 0)
1139 break;
1140
1141 total *= 2;
1142 }
1143 }
1144
1145 /* UNGCPRO; */
1146 return make_string (buf, length);
1147 }
1148
1149 /* VARARGS 1 */
1150 Lisp_Object
1151 #ifdef NO_ARG_ARRAY
1152 format1 (string1, arg0, arg1, arg2, arg3, arg4)
1153 int arg0, arg1, arg2, arg3, arg4;
1154 #else
1155 format1 (string1)
1156 #endif
1157 char *string1;
1158 {
1159 char buf[100];
1160 #ifdef NO_ARG_ARRAY
1161 int args[5];
1162 args[0] = arg0;
1163 args[1] = arg1;
1164 args[2] = arg2;
1165 args[3] = arg3;
1166 args[4] = arg4;
1167 doprnt (buf, sizeof buf, string1, 0, 5, args);
1168 #else
1169 doprnt (buf, sizeof buf, string1, 0, 5, &string1 + 1);
1170 #endif
1171 return build_string (buf);
1172 }
1173 \f
1174 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
1175 "Return t if two characters match, optionally ignoring case.\n\
1176 Both arguments must be characters (i.e. integers).\n\
1177 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
1178 (c1, c2)
1179 register Lisp_Object c1, c2;
1180 {
1181 unsigned char *downcase = DOWNCASE_TABLE;
1182 CHECK_NUMBER (c1, 0);
1183 CHECK_NUMBER (c2, 1);
1184
1185 if (!NULL (current_buffer->case_fold_search)
1186 ? downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
1187 : XINT (c1) == XINT (c2))
1188 return Qt;
1189 return Qnil;
1190 }
1191
1192 #ifndef MAINTAIN_ENVIRONMENT /* it is done in environ.c in that case */
1193 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 2, 0,
1194 "Return the value of environment variable VAR, as a string.\n\
1195 VAR should be a string. Value is nil if VAR is undefined in the environment.")
1196 (str)
1197 Lisp_Object str;
1198 {
1199 register char *val;
1200 CHECK_STRING (str, 0);
1201 val = (char *) egetenv (XSTRING (str)->data);
1202 if (!val)
1203 return Qnil;
1204 return build_string (val);
1205 }
1206 #endif /* MAINTAIN_ENVIRONMENT */
1207 \f
1208 void
1209 syms_of_editfns ()
1210 {
1211 DEFVAR_LISP ("system-name", &Vsystem_name,
1212 "The name of the machine Emacs is running on.");
1213
1214 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
1215 "The full name of the user logged in.");
1216
1217 DEFVAR_LISP ("user-name", &Vuser_name,
1218 "The user's name, based on the effective uid.");
1219
1220 DEFVAR_LISP ("user-real-name", &Vuser_real_name,
1221 "The user's name, base upon the real uid.");
1222
1223 defsubr (&Schar_equal);
1224 defsubr (&Sgoto_char);
1225 defsubr (&Sstring_to_char);
1226 defsubr (&Schar_to_string);
1227 defsubr (&Sbuffer_substring);
1228 defsubr (&Sbuffer_string);
1229
1230 defsubr (&Spoint_marker);
1231 defsubr (&Smark_marker);
1232 defsubr (&Spoint);
1233 defsubr (&Sregion_beginning);
1234 defsubr (&Sregion_end);
1235 /* defsubr (&Smark); */
1236 /* defsubr (&Sset_mark); */
1237 defsubr (&Ssave_excursion);
1238
1239 defsubr (&Sbufsize);
1240 defsubr (&Spoint_max);
1241 defsubr (&Spoint_min);
1242 defsubr (&Spoint_min_marker);
1243 defsubr (&Spoint_max_marker);
1244
1245 defsubr (&Sbobp);
1246 defsubr (&Seobp);
1247 defsubr (&Sbolp);
1248 defsubr (&Seolp);
1249 defsubr (&Sfollchar);
1250 defsubr (&Sprevchar);
1251 defsubr (&Schar_after);
1252 defsubr (&Sinsert);
1253 defsubr (&Sinsert_before_markers);
1254 defsubr (&Sinsert_char);
1255
1256 defsubr (&Suser_login_name);
1257 defsubr (&Suser_real_login_name);
1258 defsubr (&Suser_uid);
1259 defsubr (&Suser_real_uid);
1260 defsubr (&Suser_full_name);
1261 defsubr (&Scurrent_time_string);
1262 defsubr (&Ssystem_name);
1263 defsubr (&Sset_default_file_mode);
1264 defsubr (&Sunix_sync);
1265 defsubr (&Smessage);
1266 defsubr (&Sformat);
1267 #ifndef MAINTAIN_ENVIRONMENT /* in environ.c */
1268 defsubr (&Sgetenv);
1269 #endif
1270
1271 defsubr (&Sinsert_buffer_substring);
1272 defsubr (&Ssubst_char_in_region);
1273 defsubr (&Stranslate_region);
1274 defsubr (&Sdelete_region);
1275 defsubr (&Swiden);
1276 defsubr (&Snarrow_to_region);
1277 defsubr (&Ssave_restriction);
1278 }