]> code.delx.au - gnu-emacs/blob - src/buffer.c
(Fbuffer_list): New optional argument FRAME.
[gnu-emacs] / src / buffer.c
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26
27 #ifndef MAXPATHLEN
28 /* in 4.1, param.h fails to define this. */
29 #define MAXPATHLEN 1024
30 #endif /* not MAXPATHLEN */
31
32 #include <config.h>
33 #include "lisp.h"
34 #include "intervals.h"
35 #include "window.h"
36 #include "commands.h"
37 #include "buffer.h"
38 #include "charset.h"
39 #include "region-cache.h"
40 #include "indent.h"
41 #include "blockinput.h"
42 #include "frame.h"
43
44 struct buffer *current_buffer; /* the current buffer */
45
46 /* First buffer in chain of all buffers (in reverse order of creation).
47 Threaded through ->next. */
48
49 struct buffer *all_buffers;
50
51 /* This structure holds the default values of the buffer-local variables
52 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
53 The default value occupies the same slot in this structure
54 as an individual buffer's value occupies in that buffer.
55 Setting the default value also goes through the alist of buffers
56 and stores into each buffer that does not say it has a local value. */
57
58 struct buffer buffer_defaults;
59
60 /* A Lisp_Object pointer to the above, used for staticpro */
61
62 static Lisp_Object Vbuffer_defaults;
63
64 /* This structure marks which slots in a buffer have corresponding
65 default values in buffer_defaults.
66 Each such slot has a nonzero value in this structure.
67 The value has only one nonzero bit.
68
69 When a buffer has its own local value for a slot,
70 the bit for that slot (found in the same slot in this structure)
71 is turned on in the buffer's local_var_flags slot.
72
73 If a slot in this structure is -1, then even though there may
74 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
75 and the corresponding slot in buffer_defaults is not used.
76
77 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
78 but there is a default value which is copied into each buffer.
79
80 If a slot in this structure is negative, then even though there may
81 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
82 and the corresponding slot in buffer_defaults is not used.
83
84 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
85 zero, that is a bug */
86
87 struct buffer buffer_local_flags;
88
89 /* This structure holds the names of symbols whose values may be
90 buffer-local. It is indexed and accessed in the same way as the above. */
91
92 struct buffer buffer_local_symbols;
93 /* A Lisp_Object pointer to the above, used for staticpro */
94 static Lisp_Object Vbuffer_local_symbols;
95
96 /* This structure holds the required types for the values in the
97 buffer-local slots. If a slot contains Qnil, then the
98 corresponding buffer slot may contain a value of any type. If a
99 slot contains an integer, then prospective values' tags must be
100 equal to that integer. When a tag does not match, the function
101 buffer_slot_type_mismatch will signal an error. */
102 struct buffer buffer_local_types;
103
104 /* Flags indicating which built-in buffer-local variables
105 are permanent locals. */
106 static int buffer_permanent_local_flags;
107
108 Lisp_Object Fset_buffer ();
109 void set_buffer_internal ();
110 void set_buffer_internal_1 ();
111 static void call_overlay_mod_hooks ();
112 static void swap_out_buffer_local_variables ();
113 static void reset_buffer_local_variables ();
114
115 /* Alist of all buffer names vs the buffers. */
116 /* This used to be a variable, but is no longer,
117 to prevent lossage due to user rplac'ing this alist or its elements. */
118 Lisp_Object Vbuffer_alist;
119
120 /* Functions to call before and after each text change. */
121 Lisp_Object Vbefore_change_function;
122 Lisp_Object Vafter_change_function;
123 Lisp_Object Vbefore_change_functions;
124 Lisp_Object Vafter_change_functions;
125
126 Lisp_Object Vtransient_mark_mode;
127
128 /* t means ignore all read-only text properties.
129 A list means ignore such a property if its value is a member of the list.
130 Any non-nil value means ignore buffer-read-only. */
131 Lisp_Object Vinhibit_read_only;
132
133 /* List of functions to call that can query about killing a buffer.
134 If any of these functions returns nil, we don't kill it. */
135 Lisp_Object Vkill_buffer_query_functions;
136
137 /* List of functions to call before changing an unmodified buffer. */
138 Lisp_Object Vfirst_change_hook;
139
140 Lisp_Object Qfirst_change_hook;
141 Lisp_Object Qbefore_change_functions;
142 Lisp_Object Qafter_change_functions;
143
144 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
145
146 Lisp_Object Qprotected_field;
147
148 Lisp_Object QSFundamental; /* A string "Fundamental" */
149
150 Lisp_Object Qkill_buffer_hook;
151
152 Lisp_Object Qget_file_buffer;
153
154 Lisp_Object Qoverlayp;
155
156 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
157
158 Lisp_Object Qmodification_hooks;
159 Lisp_Object Qinsert_in_front_hooks;
160 Lisp_Object Qinsert_behind_hooks;
161
162 /* For debugging; temporary. See set_buffer_internal. */
163 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
164
165 nsberror (spec)
166 Lisp_Object spec;
167 {
168 if (STRINGP (spec))
169 error ("No buffer named %s", XSTRING (spec)->data);
170 error ("Invalid buffer argument");
171 }
172 \f
173 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
174 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
175 Value is nil if OBJECT is not a buffer or if it has been killed.")
176 (object)
177 Lisp_Object object;
178 {
179 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
180 ? Qt : Qnil);
181 }
182
183 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
184 "Return a list of all existing live buffers.\n\
185 If the optional arg FRAME is a frame, we return that frame's buffer list.")
186 (frame)
187 Lisp_Object frame;
188 {
189 Lisp_Object framelist, general;
190 general = Fmapcar (Qcdr, Vbuffer_alist);
191
192 if (FRAMEP (frame))
193 {
194 Lisp_Object tail;
195
196 CHECK_FRAME (frame, 1);
197
198 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
199
200 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
201 tail = framelist;
202 while (! NILP (tail))
203 {
204 general = Fdelq (XCONS (tail)->car, general);
205 tail = XCONS (tail)->cdr;
206 }
207 return nconc2 (framelist, general);
208 }
209
210 return general;
211 }
212
213 /* Like Fassoc, but use Fstring_equal to compare
214 (which ignores text properties),
215 and don't ever QUIT. */
216
217 static Lisp_Object
218 assoc_ignore_text_properties (key, list)
219 register Lisp_Object key;
220 Lisp_Object list;
221 {
222 register Lisp_Object tail;
223 for (tail = list; !NILP (tail); tail = Fcdr (tail))
224 {
225 register Lisp_Object elt, tem;
226 elt = Fcar (tail);
227 tem = Fstring_equal (Fcar (elt), key);
228 if (!NILP (tem))
229 return elt;
230 }
231 return Qnil;
232 }
233
234 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
235 "Return the buffer named NAME (a string).\n\
236 If there is no live buffer named NAME, return nil.\n\
237 NAME may also be a buffer; if so, the value is that buffer.")
238 (name)
239 register Lisp_Object name;
240 {
241 if (BUFFERP (name))
242 return name;
243 CHECK_STRING (name, 0);
244
245 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
246 }
247
248 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
249 "Return the buffer visiting file FILENAME (a string).\n\
250 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
251 If there is no such live buffer, return nil.\n\
252 See also `find-buffer-visiting'.")
253 (filename)
254 register Lisp_Object filename;
255 {
256 register Lisp_Object tail, buf, tem;
257 Lisp_Object handler;
258
259 CHECK_STRING (filename, 0);
260 filename = Fexpand_file_name (filename, Qnil);
261
262 /* If the file name has special constructs in it,
263 call the corresponding file handler. */
264 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
265 if (!NILP (handler))
266 return call2 (handler, Qget_file_buffer, filename);
267
268 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
269 {
270 buf = Fcdr (XCONS (tail)->car);
271 if (!BUFFERP (buf)) continue;
272 if (!STRINGP (XBUFFER (buf)->filename)) continue;
273 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
274 if (!NILP (tem))
275 return buf;
276 }
277 return Qnil;
278 }
279
280 Lisp_Object
281 get_truename_buffer (filename)
282 register Lisp_Object filename;
283 {
284 register Lisp_Object tail, buf, tem;
285
286 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
287 {
288 buf = Fcdr (XCONS (tail)->car);
289 if (!BUFFERP (buf)) continue;
290 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
291 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
292 if (!NILP (tem))
293 return buf;
294 }
295 return Qnil;
296 }
297
298 /* Incremented for each buffer created, to assign the buffer number. */
299 int buffer_count;
300
301 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
302 "Return the buffer named NAME, or create such a buffer and return it.\n\
303 A new buffer is created if there is no live buffer named NAME.\n\
304 If NAME starts with a space, the new buffer does not keep undo information.\n\
305 If NAME is a buffer instead of a string, then it is the value returned.\n\
306 The value is never nil.")
307 (name)
308 register Lisp_Object name;
309 {
310 register Lisp_Object buf;
311 register struct buffer *b;
312
313 buf = Fget_buffer (name);
314 if (!NILP (buf))
315 return buf;
316
317 if (XSTRING (name)->size == 0)
318 error ("Empty string for buffer name is not allowed");
319
320 b = (struct buffer *) xmalloc (sizeof (struct buffer));
321
322 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
323
324 /* An ordinary buffer uses its own struct buffer_text. */
325 b->text = &b->own_text;
326 b->base_buffer = 0;
327
328 BUF_GAP_SIZE (b) = 20;
329 BLOCK_INPUT;
330 /* We allocate extra 1-byte at the tail and keep it always '\0' for
331 anchoring a search. */
332 BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
333 UNBLOCK_INPUT;
334 if (! BUF_BEG_ADDR (b))
335 buffer_memory_full ();
336
337 BUF_PT (b) = 1;
338 BUF_GPT (b) = 1;
339 BUF_BEGV (b) = 1;
340 BUF_ZV (b) = 1;
341 BUF_Z (b) = 1;
342 BUF_MODIFF (b) = 1;
343 BUF_OVERLAY_MODIFF (b) = 1;
344 BUF_SAVE_MODIFF (b) = 1;
345 BUF_INTERVALS (b) = 0;
346 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
347
348 b->newline_cache = 0;
349 b->width_run_cache = 0;
350 b->width_table = Qnil;
351
352 /* Put this on the chain of all buffers including killed ones. */
353 b->next = all_buffers;
354 all_buffers = b;
355
356 /* An ordinary buffer normally doesn't need markers
357 to handle BEGV and ZV. */
358 b->pt_marker = Qnil;
359 b->begv_marker = Qnil;
360 b->zv_marker = Qnil;
361
362 name = Fcopy_sequence (name);
363 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
364 b->name = name;
365
366 if (XSTRING (name)->data[0] != ' ')
367 b->undo_list = Qnil;
368 else
369 b->undo_list = Qt;
370
371 reset_buffer (b);
372 reset_buffer_local_variables (b, 1);
373
374 /* Put this in the alist of all live buffers. */
375 XSETBUFFER (buf, b);
376 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
377
378 b->mark = Fmake_marker ();
379 BUF_MARKERS (b) = Qnil;
380 b->name = name;
381 return buf;
382 }
383
384 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
385 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
386 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
387 BASE-BUFFER should be an existing buffer (or buffer name).\n\
388 NAME should be a string which is not the name of an existing buffer.")
389 (base_buffer, name)
390 register Lisp_Object base_buffer, name;
391 {
392 register Lisp_Object buf;
393 register struct buffer *b;
394
395 buf = Fget_buffer (name);
396 if (!NILP (buf))
397 error ("Buffer name `%s' is in use", XSTRING (name)->data);
398
399 base_buffer = Fget_buffer (base_buffer);
400 if (NILP (base_buffer))
401 error ("No such buffer: `%s'",
402 XSTRING (XBUFFER (base_buffer)->name)->data);
403
404 if (XSTRING (name)->size == 0)
405 error ("Empty string for buffer name is not allowed");
406
407 b = (struct buffer *) xmalloc (sizeof (struct buffer));
408
409 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
410
411 if (XBUFFER (base_buffer)->base_buffer)
412 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
413 else
414 b->base_buffer = XBUFFER (base_buffer);
415
416 /* Use the base buffer's text object. */
417 b->text = b->base_buffer->text;
418
419 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
420 BUF_ZV (b) = BUF_ZV (b->base_buffer);
421 BUF_PT (b) = BUF_PT (b->base_buffer);
422
423 b->newline_cache = 0;
424 b->width_run_cache = 0;
425 b->width_table = Qnil;
426
427 /* Put this on the chain of all buffers including killed ones. */
428 b->next = all_buffers;
429 all_buffers = b;
430
431 name = Fcopy_sequence (name);
432 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
433 b->name = name;
434
435 reset_buffer (b);
436 reset_buffer_local_variables (b, 1);
437
438 /* Put this in the alist of all live buffers. */
439 XSETBUFFER (buf, b);
440 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
441
442 b->mark = Fmake_marker ();
443 b->name = name;
444
445 /* Make sure the base buffer has markers for its narrowing. */
446 if (NILP (b->base_buffer->pt_marker))
447 {
448 b->base_buffer->pt_marker = Fmake_marker ();
449 Fset_marker (b->base_buffer->pt_marker,
450 make_number (BUF_PT (b->base_buffer)), base_buffer);
451 }
452 if (NILP (b->base_buffer->begv_marker))
453 {
454 b->base_buffer->begv_marker = Fmake_marker ();
455 Fset_marker (b->base_buffer->begv_marker,
456 make_number (BUF_BEGV (b->base_buffer)), base_buffer);
457 }
458 if (NILP (b->base_buffer->zv_marker))
459 {
460 b->base_buffer->zv_marker = Fmake_marker ();
461 Fset_marker (b->base_buffer->zv_marker,
462 make_number (BUF_ZV (b->base_buffer)), base_buffer);
463 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
464 }
465
466 /* Give the indirect buffer markers for its narrowing. */
467 b->pt_marker = Fmake_marker ();
468 Fset_marker (b->pt_marker, make_number (BUF_PT (b)), buf);
469 b->begv_marker = Fmake_marker ();
470 Fset_marker (b->begv_marker, make_number (BUF_BEGV (b)), buf);
471 b->zv_marker = Fmake_marker ();
472 Fset_marker (b->zv_marker, make_number (BUF_ZV (b)), buf);
473
474 XMARKER (b->zv_marker)->insertion_type = 1;
475
476 return buf;
477 }
478
479 /* Reinitialize everything about a buffer except its name and contents
480 and local variables. */
481
482 void
483 reset_buffer (b)
484 register struct buffer *b;
485 {
486 b->filename = Qnil;
487 b->file_truename = Qnil;
488 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
489 b->modtime = 0;
490 XSETFASTINT (b->save_length, 0);
491 b->last_window_start = 1;
492 /* It is more conservative to start out "changed" than "unchanged". */
493 b->clip_changed = 1;
494 b->backed_up = Qnil;
495 b->auto_save_modified = 0;
496 b->auto_save_failure_time = -1;
497 b->auto_save_file_name = Qnil;
498 b->read_only = Qnil;
499 b->overlays_before = Qnil;
500 b->overlays_after = Qnil;
501 XSETFASTINT (b->overlay_center, 1);
502 b->mark_active = Qnil;
503 b->point_before_scroll = Qnil;
504 b->file_format = Qnil;
505 b->last_selected_window = Qnil;
506 XSETINT (b->display_count, 0);
507 b->extra2 = Qnil;
508 b->extra3 = Qnil;
509 }
510
511 /* Reset buffer B's local variables info.
512 Don't use this on a buffer that has already been in use;
513 it does not treat permanent locals consistently.
514 Instead, use Fkill_all_local_variables.
515
516 If PERMANENT_TOO is 1, then we reset permanent built-in
517 buffer-local variables. If PERMANENT_TOO is 0,
518 we preserve those. */
519
520 static void
521 reset_buffer_local_variables (b, permanent_too)
522 register struct buffer *b;
523 int permanent_too;
524 {
525 register int offset;
526 int dont_reset;
527
528 /* Decide which built-in local variables to reset. */
529 if (permanent_too)
530 dont_reset = 0;
531 else
532 dont_reset = buffer_permanent_local_flags;
533
534 /* Reset the major mode to Fundamental, together with all the
535 things that depend on the major mode.
536 default-major-mode is handled at a higher level.
537 We ignore it here. */
538 b->major_mode = Qfundamental_mode;
539 b->keymap = Qnil;
540 b->abbrev_table = Vfundamental_mode_abbrev_table;
541 b->mode_name = QSFundamental;
542 b->minor_modes = Qnil;
543
544 /* If the standard case table has been altered and invalidated,
545 fix up its insides first. */
546 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
547 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
548 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
549 Fset_standard_case_table (Vascii_downcase_table);
550
551 b->downcase_table = Vascii_downcase_table;
552 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
553 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
554 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
555 b->invisibility_spec = Qt;
556 #ifndef DOS_NT
557 b->buffer_file_type = Qnil;
558 #endif
559
560 #if 0
561 b->sort_table = XSTRING (Vascii_sort_table);
562 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
563 #endif /* 0 */
564
565 /* Reset all (or most) per-buffer variables to their defaults. */
566 b->local_var_alist = Qnil;
567 b->local_var_flags &= dont_reset;
568
569 /* For each slot that has a default value,
570 copy that into the slot. */
571
572 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
573 offset < sizeof (struct buffer);
574 offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
575 {
576 int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
577 if ((flag > 0
578 /* Don't reset a permanent local. */
579 && ! (dont_reset & flag))
580 || flag == -2)
581 *(Lisp_Object *)(offset + (char *)b)
582 = *(Lisp_Object *)(offset + (char *)&buffer_defaults);
583 }
584 }
585
586 /* We split this away from generate-new-buffer, because rename-buffer
587 and set-visited-file-name ought to be able to use this to really
588 rename the buffer properly. */
589
590 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
591 1, 2, 0,
592 "Return a string that is the name of no existing buffer based on NAME.\n\
593 If there is no live buffer named NAME, then return NAME.\n\
594 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
595 until an unused name is found, and then return that name.\n\
596 Optional second argument IGNORE specifies a name that is okay to use\n\
597 \(if it is in the sequence to be tried)\n\
598 even if a buffer with that name exists.")
599 (name, ignore)
600 register Lisp_Object name, ignore;
601 {
602 register Lisp_Object gentemp, tem;
603 int count;
604 char number[10];
605
606 CHECK_STRING (name, 0);
607
608 tem = Fget_buffer (name);
609 if (NILP (tem))
610 return name;
611
612 count = 1;
613 while (1)
614 {
615 sprintf (number, "<%d>", ++count);
616 gentemp = concat2 (name, build_string (number));
617 tem = Fstring_equal (gentemp, ignore);
618 if (!NILP (tem))
619 return gentemp;
620 tem = Fget_buffer (gentemp);
621 if (NILP (tem))
622 return gentemp;
623 }
624 }
625
626 \f
627 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
628 "Return the name of BUFFER, as a string.\n\
629 With no argument or nil as argument, return the name of the current buffer.")
630 (buffer)
631 register Lisp_Object buffer;
632 {
633 if (NILP (buffer))
634 return current_buffer->name;
635 CHECK_BUFFER (buffer, 0);
636 return XBUFFER (buffer)->name;
637 }
638
639 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
640 "Return name of file BUFFER is visiting, or nil if none.\n\
641 No argument or nil as argument means use the current buffer.")
642 (buffer)
643 register Lisp_Object buffer;
644 {
645 if (NILP (buffer))
646 return current_buffer->filename;
647 CHECK_BUFFER (buffer, 0);
648 return XBUFFER (buffer)->filename;
649 }
650
651 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
652 0, 1, 0,
653 "Return the base buffer of indirect buffer BUFFER.\n\
654 If BUFFER is not indirect, return nil.")
655 (buffer)
656 register Lisp_Object buffer;
657 {
658 struct buffer *base;
659 Lisp_Object base_buffer;
660
661 if (NILP (buffer))
662 base = current_buffer->base_buffer;
663 else
664 {
665 CHECK_BUFFER (buffer, 0);
666 base = XBUFFER (buffer)->base_buffer;
667 }
668
669 if (! base)
670 return Qnil;
671 XSETBUFFER (base_buffer, base);
672 return base_buffer;
673 }
674
675 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
676 Sbuffer_local_variables, 0, 1, 0,
677 "Return an alist of variables that are buffer-local in BUFFER.\n\
678 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
679 For a symbol that is locally unbound, just the symbol appears in the value.\n\
680 Note that storing new VALUEs in these elements doesn't change the variables.\n\
681 No argument or nil as argument means use current buffer as BUFFER.")
682 (buffer)
683 register Lisp_Object buffer;
684 {
685 register struct buffer *buf;
686 register Lisp_Object result;
687
688 if (NILP (buffer))
689 buf = current_buffer;
690 else
691 {
692 CHECK_BUFFER (buffer, 0);
693 buf = XBUFFER (buffer);
694 }
695
696 result = Qnil;
697
698 {
699 register Lisp_Object tail;
700 for (tail = buf->local_var_alist; CONSP (tail); tail = XCONS (tail)->cdr)
701 {
702 Lisp_Object val, elt;
703
704 elt = XCONS (tail)->car;
705
706 /* Reference each variable in the alist in buf.
707 If inquiring about the current buffer, this gets the current values,
708 so store them into the alist so the alist is up to date.
709 If inquiring about some other buffer, this swaps out any values
710 for that buffer, making the alist up to date automatically. */
711 val = find_symbol_value (XCONS (elt)->car);
712 /* Use the current buffer value only if buf is the current buffer. */
713 if (buf != current_buffer)
714 val = XCONS (elt)->cdr;
715
716 /* If symbol is unbound, put just the symbol in the list. */
717 if (EQ (val, Qunbound))
718 result = Fcons (XCONS (elt)->car, result);
719 /* Otherwise, put (symbol . value) in the list. */
720 else
721 result = Fcons (Fcons (XCONS (elt)->car, val), result);
722 }
723 }
724
725 /* Add on all the variables stored in special slots. */
726 {
727 register int offset, mask;
728
729 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
730 offset < sizeof (struct buffer);
731 offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
732 {
733 mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
734 if (mask == -1 || (buf->local_var_flags & mask))
735 if (SYMBOLP (*(Lisp_Object *)(offset
736 + (char *)&buffer_local_symbols)))
737 result = Fcons (Fcons (*((Lisp_Object *)
738 (offset + (char *)&buffer_local_symbols)),
739 *(Lisp_Object *)(offset + (char *)buf)),
740 result);
741 }
742 }
743
744 return result;
745 }
746
747 \f
748 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
749 0, 1, 0,
750 "Return t if BUFFER was modified since its file was last read or saved.\n\
751 No argument or nil as argument means use current buffer as BUFFER.")
752 (buffer)
753 register Lisp_Object buffer;
754 {
755 register struct buffer *buf;
756 if (NILP (buffer))
757 buf = current_buffer;
758 else
759 {
760 CHECK_BUFFER (buffer, 0);
761 buf = XBUFFER (buffer);
762 }
763
764 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
765 }
766
767 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
768 1, 1, 0,
769 "Mark current buffer as modified or unmodified according to FLAG.\n\
770 A non-nil FLAG means mark the buffer modified.")
771 (flag)
772 register Lisp_Object flag;
773 {
774 register int already;
775 register Lisp_Object fn;
776
777 #ifdef CLASH_DETECTION
778 /* If buffer becoming modified, lock the file.
779 If buffer becoming unmodified, unlock the file. */
780
781 fn = current_buffer->file_truename;
782 if (!NILP (fn))
783 {
784 already = SAVE_MODIFF < MODIFF;
785 if (!already && !NILP (flag))
786 lock_file (fn);
787 else if (already && NILP (flag))
788 unlock_file (fn);
789 }
790 #endif /* CLASH_DETECTION */
791
792 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
793 update_mode_lines++;
794 return flag;
795 }
796
797 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
798 0, 1, 0,
799 "Return BUFFER's tick counter, incremented for each change in text.\n\
800 Each buffer has a tick counter which is incremented each time the text in\n\
801 that buffer is changed. It wraps around occasionally.\n\
802 No argument or nil as argument means use current buffer as BUFFER.")
803 (buffer)
804 register Lisp_Object buffer;
805 {
806 register struct buffer *buf;
807 if (NILP (buffer))
808 buf = current_buffer;
809 else
810 {
811 CHECK_BUFFER (buffer, 0);
812 buf = XBUFFER (buffer);
813 }
814
815 return make_number (BUF_MODIFF (buf));
816 }
817 \f
818 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
819 "sRename buffer (to new name): \nP",
820 "Change current buffer's name to NEWNAME (a string).\n\
821 If second arg UNIQUE is nil or omitted, it is an error if a\n\
822 buffer named NEWNAME already exists.\n\
823 If UNIQUE is non-nil, come up with a new name using\n\
824 `generate-new-buffer-name'.\n\
825 Interactively, you can set UNIQUE with a prefix argument.\n\
826 We return the name we actually gave the buffer.\n\
827 This does not change the name of the visited file (if any).")
828 (newname, unique)
829 register Lisp_Object newname, unique;
830 {
831 register Lisp_Object tem, buf;
832
833 CHECK_STRING (newname, 0);
834
835 if (XSTRING (newname)->size == 0)
836 error ("Empty string is invalid as a buffer name");
837
838 tem = Fget_buffer (newname);
839 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
840 the buffer automatically so you can create another with the original name.
841 It makes UNIQUE equivalent to
842 (rename-buffer (generate-new-buffer-name NEWNAME)). */
843 if (NILP (unique) && XBUFFER (tem) == current_buffer)
844 return current_buffer->name;
845 if (!NILP (tem))
846 {
847 if (!NILP (unique))
848 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
849 else
850 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
851 }
852
853 current_buffer->name = newname;
854
855 /* Catch redisplay's attention. Unless we do this, the mode lines for
856 any windows displaying current_buffer will stay unchanged. */
857 update_mode_lines++;
858
859 XSETBUFFER (buf, current_buffer);
860 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
861 if (NILP (current_buffer->filename)
862 && !NILP (current_buffer->auto_save_file_name))
863 call0 (intern ("rename-auto-save-file"));
864 /* Refetch since that last call may have done GC. */
865 return current_buffer->name;
866 }
867
868 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
869 "Return most recently selected buffer other than BUFFER.\n\
870 Buffers not visible in windows are preferred to visible buffers,\n\
871 unless optional second argument VISIBLE-OK is non-nil.\n\
872 If no other buffer exists, the buffer `*scratch*' is returned.\n\
873 If BUFFER is omitted or nil, some interesting buffer is returned.")
874 (buffer, visible_ok)
875 register Lisp_Object buffer, visible_ok;
876 {
877 Lisp_Object Fset_buffer_major_mode ();
878 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
879 notsogood = Qnil;
880
881 tail = Vbuffer_alist;
882 pred = frame_buffer_predicate ();
883
884 /* Consider buffers that have been seen in the selected frame
885 before other buffers. */
886
887 tem = frame_buffer_list ();
888 add_ons = Qnil;
889 while (CONSP (tem))
890 {
891 if (BUFFERP (XCONS (tem)->car))
892 add_ons = Fcons (Fcons (Qnil, XCONS (tem)->car), add_ons);
893 tem = XCONS (tem)->cdr;
894 }
895 tail = nconc2 (Fnreverse (add_ons), tail);
896
897 for (; !NILP (tail); tail = Fcdr (tail))
898 {
899 buf = Fcdr (Fcar (tail));
900 if (EQ (buf, buffer))
901 continue;
902 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
903 continue;
904 /* If the selected frame has a buffer_predicate,
905 disregard buffers that don't fit the predicate. */
906 if (!NILP (pred))
907 {
908 tem = call1 (pred, buf);
909 if (NILP (tem))
910 continue;
911 }
912
913 if (NILP (visible_ok))
914 tem = Fget_buffer_window (buf, Qt);
915 else
916 tem = Qnil;
917 if (NILP (tem))
918 return buf;
919 if (NILP (notsogood))
920 notsogood = buf;
921 }
922 if (!NILP (notsogood))
923 return notsogood;
924 buf = Fget_buffer_create (build_string ("*scratch*"));
925 Fset_buffer_major_mode (buf);
926 return buf;
927 }
928 \f
929 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
930 0, 1, "",
931 "Make BUFFER stop keeping undo information.\n\
932 No argument or nil as argument means do this for the current buffer.")
933 (buffer)
934 register Lisp_Object buffer;
935 {
936 Lisp_Object real_buffer;
937
938 if (NILP (buffer))
939 XSETBUFFER (real_buffer, current_buffer);
940 else
941 {
942 real_buffer = Fget_buffer (buffer);
943 if (NILP (real_buffer))
944 nsberror (buffer);
945 }
946
947 XBUFFER (real_buffer)->undo_list = Qt;
948
949 return Qnil;
950 }
951
952 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
953 0, 1, "",
954 "Start keeping undo information for buffer BUFFER.\n\
955 No argument or nil as argument means do this for the current buffer.")
956 (buffer)
957 register Lisp_Object buffer;
958 {
959 Lisp_Object real_buffer;
960
961 if (NILP (buffer))
962 XSETBUFFER (real_buffer, current_buffer);
963 else
964 {
965 real_buffer = Fget_buffer (buffer);
966 if (NILP (real_buffer))
967 nsberror (buffer);
968 }
969
970 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
971 XBUFFER (real_buffer)->undo_list = Qnil;
972
973 return Qnil;
974 }
975
976 /*
977 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
978 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
979 The buffer being killed will be current while the hook is running.\n\
980 See `kill-buffer'."
981 */
982 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
983 "Kill the buffer BUFFER.\n\
984 The argument may be a buffer or may be the name of a buffer.\n\
985 An argument of nil means kill the current buffer.\n\n\
986 Value is t if the buffer is actually killed, nil if user says no.\n\n\
987 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
988 if not void, is a list of functions to be called, with no arguments,\n\
989 before the buffer is actually killed. The buffer to be killed is current\n\
990 when the hook functions are called.\n\n\
991 Any processes that have this buffer as the `process-buffer' are killed\n\
992 with SIGHUP.")
993 (buffer)
994 Lisp_Object buffer;
995 {
996 Lisp_Object buf;
997 register struct buffer *b;
998 register Lisp_Object tem;
999 register struct Lisp_Marker *m;
1000 struct gcpro gcpro1, gcpro2;
1001
1002 if (NILP (buffer))
1003 buf = Fcurrent_buffer ();
1004 else
1005 buf = Fget_buffer (buffer);
1006 if (NILP (buf))
1007 nsberror (buffer);
1008
1009 b = XBUFFER (buf);
1010
1011 /* Avoid trouble for buffer already dead. */
1012 if (NILP (b->name))
1013 return Qnil;
1014
1015 /* Query if the buffer is still modified. */
1016 if (INTERACTIVE && !NILP (b->filename)
1017 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1018 {
1019 GCPRO1 (buf);
1020 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1021 XSTRING (b->name)->data));
1022 UNGCPRO;
1023 if (NILP (tem))
1024 return Qnil;
1025 }
1026
1027 /* Run hooks with the buffer to be killed the current buffer. */
1028 {
1029 register Lisp_Object val;
1030 int count = specpdl_ptr - specpdl;
1031 Lisp_Object list;
1032
1033 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1034 set_buffer_internal (b);
1035
1036 /* First run the query functions; if any query is answered no,
1037 don't kill the buffer. */
1038 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1039 {
1040 tem = call0 (Fcar (list));
1041 if (NILP (tem))
1042 return unbind_to (count, Qnil);
1043 }
1044
1045 /* Then run the hooks. */
1046 if (!NILP (Vrun_hooks))
1047 call1 (Vrun_hooks, Qkill_buffer_hook);
1048 unbind_to (count, Qnil);
1049 }
1050
1051 /* We have no more questions to ask. Verify that it is valid
1052 to kill the buffer. This must be done after the questions
1053 since anything can happen within do_yes_or_no_p. */
1054
1055 /* Don't kill the minibuffer now current. */
1056 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1057 return Qnil;
1058
1059 if (NILP (b->name))
1060 return Qnil;
1061
1062 /* When we kill a base buffer, kill all its indirect buffers.
1063 We do it at this stage so nothing terrible happens if they
1064 ask questions or their hooks get errors. */
1065 if (! b->base_buffer)
1066 {
1067 struct buffer *other;
1068
1069 GCPRO1 (buf);
1070
1071 for (other = all_buffers; other; other = other->next)
1072 /* all_buffers contains dead buffers too;
1073 don't re-kill them. */
1074 if (other->base_buffer == b && !NILP (other->name))
1075 {
1076 Lisp_Object buf;
1077 XSETBUFFER (buf, other);
1078 Fkill_buffer (buf);
1079 }
1080
1081 UNGCPRO;
1082 }
1083
1084 /* Make this buffer not be current.
1085 In the process, notice if this is the sole visible buffer
1086 and give up if so. */
1087 if (b == current_buffer)
1088 {
1089 tem = Fother_buffer (buf, Qnil);
1090 Fset_buffer (tem);
1091 if (b == current_buffer)
1092 return Qnil;
1093 }
1094
1095 /* Now there is no question: we can kill the buffer. */
1096
1097 #ifdef CLASH_DETECTION
1098 /* Unlock this buffer's file, if it is locked. */
1099 unlock_buffer (b);
1100 #endif /* CLASH_DETECTION */
1101
1102 kill_buffer_processes (buf);
1103
1104 tem = Vinhibit_quit;
1105 Vinhibit_quit = Qt;
1106 replace_buffer_in_all_windows (buf);
1107 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1108 frames_discard_buffer (buf);
1109 Vinhibit_quit = tem;
1110
1111 /* Delete any auto-save file, if we saved it in this session. */
1112 if (STRINGP (b->auto_save_file_name)
1113 && b->auto_save_modified != 0
1114 && SAVE_MODIFF < b->auto_save_modified)
1115 {
1116 Lisp_Object tem;
1117 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1118 if (! NILP (tem))
1119 internal_delete_file (b->auto_save_file_name);
1120 }
1121
1122 if (b->base_buffer)
1123 {
1124 /* Unchain all markers that belong to this indirect buffer.
1125 Don't unchain the markers that belong to the base buffer
1126 or its other indirect buffers. */
1127 for (tem = BUF_MARKERS (b); !NILP (tem); )
1128 {
1129 Lisp_Object next;
1130 m = XMARKER (tem);
1131 next = m->chain;
1132 if (m->buffer == b)
1133 unchain_marker (tem);
1134 tem = next;
1135 }
1136 }
1137 else
1138 {
1139 /* Unchain all markers of this buffer and its indirect buffers.
1140 and leave them pointing nowhere. */
1141 for (tem = BUF_MARKERS (b); !NILP (tem); )
1142 {
1143 m = XMARKER (tem);
1144 m->buffer = 0;
1145 tem = m->chain;
1146 m->chain = Qnil;
1147 }
1148 BUF_MARKERS (b) = Qnil;
1149
1150 #ifdef USE_TEXT_PROPERTIES
1151 BUF_INTERVALS (b) = NULL_INTERVAL;
1152 #endif
1153
1154 /* Perhaps we should explicitly free the interval tree here... */
1155 }
1156
1157 /* Reset the local variables, so that this buffer's local values
1158 won't be protected from GC. They would be protected
1159 if they happened to remain encached in their symbols.
1160 This gets rid of them for certain. */
1161 swap_out_buffer_local_variables (b);
1162 reset_buffer_local_variables (b, 1);
1163
1164 b->name = Qnil;
1165
1166 BLOCK_INPUT;
1167 if (! b->base_buffer)
1168 BUFFER_FREE (BUF_BEG_ADDR (b));
1169
1170 if (b->newline_cache)
1171 {
1172 free_region_cache (b->newline_cache);
1173 b->newline_cache = 0;
1174 }
1175 if (b->width_run_cache)
1176 {
1177 free_region_cache (b->width_run_cache);
1178 b->width_run_cache = 0;
1179 }
1180 b->width_table = Qnil;
1181 UNBLOCK_INPUT;
1182 b->undo_list = Qnil;
1183
1184 return Qt;
1185 }
1186 \f
1187 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1188 we do this each time BUF is selected visibly, the more recently
1189 selected buffers are always closer to the front of the list. This
1190 means that other_buffer is more likely to choose a relevant buffer. */
1191
1192 record_buffer (buf)
1193 Lisp_Object buf;
1194 {
1195 register Lisp_Object link, prev;
1196
1197 prev = Qnil;
1198 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
1199 {
1200 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
1201 break;
1202 prev = link;
1203 }
1204
1205 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1206 we cannot use Fdelq itself here because it allows quitting. */
1207
1208 if (NILP (prev))
1209 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
1210 else
1211 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1212
1213 XCONS (link)->cdr = Vbuffer_alist;
1214 Vbuffer_alist = link;
1215
1216 /* Now move this buffer to the front of frame_buffer_list also. */
1217
1218 prev = Qnil;
1219 for (link = frame_buffer_list (); CONSP (link); link = XCONS (link)->cdr)
1220 {
1221 if (EQ (XCONS (link)->car, buf))
1222 break;
1223 prev = link;
1224 }
1225
1226 /* Effectively do delq. */
1227
1228 if (CONSP (link))
1229 {
1230 if (NILP (prev))
1231 set_frame_buffer_list (XCONS (frame_buffer_list ())->cdr);
1232 else
1233 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1234
1235 XCONS (link)->cdr = frame_buffer_list ();
1236 set_frame_buffer_list (link);
1237 }
1238 else
1239 set_frame_buffer_list (Fcons (buf, frame_buffer_list ()));
1240 }
1241
1242 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1243 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1244 Use this function before selecting the buffer, since it may need to inspect\n\
1245 the current buffer's major mode.")
1246 (buffer)
1247 Lisp_Object buffer;
1248 {
1249 int count;
1250 Lisp_Object function;
1251
1252 function = buffer_defaults.major_mode;
1253 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1254 function = current_buffer->major_mode;
1255
1256 if (NILP (function) || EQ (function, Qfundamental_mode))
1257 return Qnil;
1258
1259 count = specpdl_ptr - specpdl;
1260
1261 /* To select a nonfundamental mode,
1262 select the buffer temporarily and then call the mode function. */
1263
1264 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1265
1266 Fset_buffer (buffer);
1267 call0 (function);
1268
1269 return unbind_to (count, Qnil);
1270 }
1271
1272 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1273 "Select buffer BUFFER in the current window.\n\
1274 BUFFER may be a buffer or a buffer name.\n\
1275 Optional second arg NORECORD non-nil means\n\
1276 do not put this buffer at the front of the list of recently selected ones.\n\
1277 \n\
1278 WARNING: This is NOT the way to work on another buffer temporarily\n\
1279 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1280 the window-buffer correspondences.")
1281 (buffer, norecord)
1282 Lisp_Object buffer, norecord;
1283 {
1284 register Lisp_Object buf;
1285 Lisp_Object tem;
1286
1287 if (EQ (minibuf_window, selected_window))
1288 error ("Cannot switch buffers in minibuffer window");
1289 tem = Fwindow_dedicated_p (selected_window);
1290 if (!NILP (tem))
1291 error ("Cannot switch buffers in a dedicated window");
1292
1293 if (NILP (buffer))
1294 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1295 else
1296 {
1297 buf = Fget_buffer (buffer);
1298 if (NILP (buf))
1299 {
1300 buf = Fget_buffer_create (buffer);
1301 Fset_buffer_major_mode (buf);
1302 }
1303 }
1304 Fset_buffer (buf);
1305 if (NILP (norecord))
1306 record_buffer (buf);
1307
1308 Fset_window_buffer (EQ (selected_window, minibuf_window)
1309 ? Fnext_window (minibuf_window, Qnil, Qnil)
1310 : selected_window,
1311 buf);
1312
1313 return buf;
1314 }
1315
1316 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1317 "Select buffer BUFFER in some window, preferably a different one.\n\
1318 If BUFFER is nil, then some other buffer is chosen.\n\
1319 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1320 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1321 window even if BUFFER is already visible in the selected window.\n\
1322 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1323 of `display-buffer' for additional customization information.\n\
1324 \n\
1325 Optional third arg NORECORD non-nil means\n\
1326 do not put this buffer at the front of the list of recently selected ones.")
1327 (buffer, other_window, norecord)
1328 Lisp_Object buffer, other_window, norecord;
1329 {
1330 register Lisp_Object buf;
1331 if (NILP (buffer))
1332 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1333 else
1334 {
1335 buf = Fget_buffer (buffer);
1336 if (NILP (buf))
1337 {
1338 buf = Fget_buffer_create (buffer);
1339 Fset_buffer_major_mode (buf);
1340 }
1341 }
1342 Fset_buffer (buf);
1343 if (NILP (norecord))
1344 record_buffer (buf);
1345 Fselect_window (Fdisplay_buffer (buf, other_window));
1346 return buf;
1347 }
1348
1349 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1350 "Return the current buffer as a Lisp object.")
1351 ()
1352 {
1353 register Lisp_Object buf;
1354 XSETBUFFER (buf, current_buffer);
1355 return buf;
1356 }
1357 \f
1358 /* Set the current buffer to B. */
1359
1360 void
1361 set_buffer_internal (b)
1362 register struct buffer *b;
1363 {
1364 register struct buffer *old_buf;
1365 register Lisp_Object tail, valcontents;
1366 Lisp_Object tem;
1367
1368 if (current_buffer == b)
1369 return;
1370
1371 windows_or_buffers_changed = 1;
1372 set_buffer_internal_1 (b);
1373 }
1374
1375 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1376 This is used by redisplay. */
1377
1378 void
1379 set_buffer_internal_1 (b)
1380 register struct buffer *b;
1381 {
1382 register struct buffer *old_buf;
1383 register Lisp_Object tail, valcontents;
1384 Lisp_Object tem;
1385
1386 if (current_buffer == b)
1387 return;
1388
1389 old_buf = current_buffer;
1390 current_buffer = b;
1391 last_known_column_point = -1; /* invalidate indentation cache */
1392
1393 if (old_buf)
1394 {
1395 /* Put the undo list back in the base buffer, so that it appears
1396 that an indirect buffer shares the undo list of its base. */
1397 if (old_buf->base_buffer)
1398 old_buf->base_buffer->undo_list = old_buf->undo_list;
1399
1400 /* If the old current buffer has markers to record PT, BEGV and ZV
1401 when it is not current, update them now. */
1402 if (! NILP (old_buf->pt_marker))
1403 {
1404 Lisp_Object obuf;
1405 XSETBUFFER (obuf, old_buf);
1406 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1407 }
1408 if (! NILP (old_buf->begv_marker))
1409 {
1410 Lisp_Object obuf;
1411 XSETBUFFER (obuf, old_buf);
1412 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1413 }
1414 if (! NILP (old_buf->zv_marker))
1415 {
1416 Lisp_Object obuf;
1417 XSETBUFFER (obuf, old_buf);
1418 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1419 }
1420 }
1421
1422 /* Get the undo list from the base buffer, so that it appears
1423 that an indirect buffer shares the undo list of its base. */
1424 if (b->base_buffer)
1425 b->undo_list = b->base_buffer->undo_list;
1426
1427 /* If the new current buffer has markers to record PT, BEGV and ZV
1428 when it is not current, fetch them now. */
1429 if (! NILP (b->pt_marker))
1430 BUF_PT (b) = marker_position (b->pt_marker);
1431 if (! NILP (b->begv_marker))
1432 BUF_BEGV (b) = marker_position (b->begv_marker);
1433 if (! NILP (b->zv_marker))
1434 BUF_ZV (b) = marker_position (b->zv_marker);
1435
1436 /* Look down buffer's list of local Lisp variables
1437 to find and update any that forward into C variables. */
1438
1439 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1440 {
1441 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
1442 if ((BUFFER_LOCAL_VALUEP (valcontents)
1443 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1444 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
1445 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1446 /* Just reference the variable
1447 to cause it to become set for this buffer. */
1448 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1449 }
1450
1451 /* Do the same with any others that were local to the previous buffer */
1452
1453 if (old_buf)
1454 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1455 {
1456 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
1457 if ((BUFFER_LOCAL_VALUEP (valcontents)
1458 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1459 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
1460 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1461 /* Just reference the variable
1462 to cause it to become set for this buffer. */
1463 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1464 }
1465 }
1466
1467 /* Switch to buffer B temporarily for redisplay purposes.
1468 This avoids certain things that don't need to be done within redisplay. */
1469
1470 void
1471 set_buffer_temp (b)
1472 struct buffer *b;
1473 {
1474 register struct buffer *old_buf;
1475
1476 if (current_buffer == b)
1477 return;
1478
1479 old_buf = current_buffer;
1480 current_buffer = b;
1481
1482 if (old_buf)
1483 {
1484 /* If the old current buffer has markers to record PT, BEGV and ZV
1485 when it is not current, update them now. */
1486 if (! NILP (old_buf->pt_marker))
1487 {
1488 Lisp_Object obuf;
1489 XSETBUFFER (obuf, old_buf);
1490 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1491 }
1492 if (! NILP (old_buf->begv_marker))
1493 {
1494 Lisp_Object obuf;
1495 XSETBUFFER (obuf, old_buf);
1496 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1497 }
1498 if (! NILP (old_buf->zv_marker))
1499 {
1500 Lisp_Object obuf;
1501 XSETBUFFER (obuf, old_buf);
1502 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1503 }
1504 }
1505
1506 /* If the new current buffer has markers to record PT, BEGV and ZV
1507 when it is not current, fetch them now. */
1508 if (! NILP (b->pt_marker))
1509 BUF_PT (b) = marker_position (b->pt_marker);
1510 if (! NILP (b->begv_marker))
1511 BUF_BEGV (b) = marker_position (b->begv_marker);
1512 if (! NILP (b->zv_marker))
1513 BUF_ZV (b) = marker_position (b->zv_marker);
1514 }
1515
1516 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1517 "Make the buffer BUFFER current for editing operations.\n\
1518 BUFFER may be a buffer or the name of an existing buffer.\n\
1519 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1520 This function does not display the buffer, so its effect ends\n\
1521 when the current command terminates.\n\
1522 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1523 (buffer)
1524 register Lisp_Object buffer;
1525 {
1526 register Lisp_Object buf;
1527 buf = Fget_buffer (buffer);
1528 if (NILP (buf))
1529 nsberror (buffer);
1530 if (NILP (XBUFFER (buf)->name))
1531 error ("Selecting deleted buffer");
1532 set_buffer_internal (XBUFFER (buf));
1533 return buf;
1534 }
1535 \f
1536 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1537 Sbarf_if_buffer_read_only, 0, 0, 0,
1538 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1539 ()
1540 {
1541 if (!NILP (current_buffer->read_only)
1542 && NILP (Vinhibit_read_only))
1543 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1544 return Qnil;
1545 }
1546
1547 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1548 "Put BUFFER at the end of the list of all buffers.\n\
1549 There it is the least likely candidate for `other-buffer' to return;\n\
1550 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1551 If BUFFER is nil or omitted, bury the current buffer.\n\
1552 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1553 selected window if it is displayed there.")
1554 (buffer)
1555 register Lisp_Object buffer;
1556 {
1557 /* Figure out what buffer we're going to bury. */
1558 if (NILP (buffer))
1559 {
1560 XSETBUFFER (buffer, current_buffer);
1561 }
1562 else
1563 {
1564 Lisp_Object buf1;
1565
1566 buf1 = Fget_buffer (buffer);
1567 if (NILP (buf1))
1568 nsberror (buffer);
1569 buffer = buf1;
1570 }
1571
1572 /* Unshow the buffer in the selected window, if it is there. */
1573 if (EQ (XWINDOW (selected_window)->buffer, buffer))
1574 Fswitch_to_buffer (Fother_buffer (buffer, Qnil), Qnil);
1575 /* Otherwise, unshow it in other frames. */
1576 else
1577 Freplace_buffer_in_windows (buffer);
1578
1579 /* Move buffer to the end of the buffer list. */
1580 {
1581 register Lisp_Object aelt, link;
1582
1583 aelt = Frassq (buffer, Vbuffer_alist);
1584 link = Fmemq (aelt, Vbuffer_alist);
1585 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1586 XCONS (link)->cdr = Qnil;
1587 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1588 }
1589
1590 frames_bury_buffer (buffer);
1591
1592 return Qnil;
1593 }
1594 \f
1595 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1596 "Delete the entire contents of the current buffer.\n\
1597 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1598 so the buffer is truly empty after this.")
1599 ()
1600 {
1601 Fwiden ();
1602 del_range (BEG, Z);
1603 current_buffer->last_window_start = 1;
1604 /* Prevent warnings, or suspension of auto saving, that would happen
1605 if future size is less than past size. Use of erase-buffer
1606 implies that the future text is not really related to the past text. */
1607 XSETFASTINT (current_buffer->save_length, 0);
1608 return Qnil;
1609 }
1610
1611 validate_region (b, e)
1612 register Lisp_Object *b, *e;
1613 {
1614 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1615 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1616
1617 if (XINT (*b) > XINT (*e))
1618 {
1619 Lisp_Object tem;
1620 tem = *b; *b = *e; *e = tem;
1621 }
1622
1623 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1624 && XINT (*e) <= ZV))
1625 args_out_of_range (*b, *e);
1626 }
1627 \f
1628 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1629 0, 0, 0,
1630 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1631 Most local variable bindings are eliminated so that the default values\n\
1632 become effective once more. Also, the syntax table is set from\n\
1633 `standard-syntax-table', the local keymap is set to nil,\n\
1634 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1635 This function also forces redisplay of the mode line.\n\
1636 \n\
1637 Every function to select a new major mode starts by\n\
1638 calling this function.\n\n\
1639 As a special exception, local variables whose names have\n\
1640 a non-nil `permanent-local' property are not eliminated by this function.\n\
1641 \n\
1642 The first thing this function does is run\n\
1643 the normal hook `change-major-mode-hook'.")
1644 ()
1645 {
1646 register Lisp_Object alist, sym, tem;
1647 Lisp_Object oalist;
1648
1649 if (!NILP (Vrun_hooks))
1650 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1651 oalist = current_buffer->local_var_alist;
1652
1653 /* Make sure none of the bindings in oalist
1654 remain swapped in, in their symbols. */
1655
1656 swap_out_buffer_local_variables (current_buffer);
1657
1658 /* Actually eliminate all local bindings of this buffer. */
1659
1660 reset_buffer_local_variables (current_buffer, 0);
1661
1662 /* Redisplay mode lines; we are changing major mode. */
1663
1664 update_mode_lines++;
1665
1666 /* Any which are supposed to be permanent,
1667 make local again, with the same values they had. */
1668
1669 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1670 {
1671 sym = XCONS (XCONS (alist)->car)->car;
1672 tem = Fget (sym, Qpermanent_local);
1673 if (! NILP (tem))
1674 {
1675 Fmake_local_variable (sym);
1676 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1677 }
1678 }
1679
1680 /* Force mode-line redisplay. Useful here because all major mode
1681 commands call this function. */
1682 update_mode_lines++;
1683
1684 return Qnil;
1685 }
1686
1687 /* Make sure no local variables remain set up with buffer B
1688 for their current values. */
1689
1690 static void
1691 swap_out_buffer_local_variables (b)
1692 struct buffer *b;
1693 {
1694 Lisp_Object oalist, alist, sym, tem, buffer;
1695
1696 XSETBUFFER (buffer, b);
1697 oalist = b->local_var_alist;
1698
1699 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1700 {
1701 sym = XCONS (XCONS (alist)->car)->car;
1702
1703 /* Need not do anything if some other buffer's binding is now encached. */
1704 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car;
1705 if (XBUFFER (tem) == current_buffer)
1706 {
1707 /* Symbol is set up for this buffer's old local value.
1708 Set it up for the current buffer with the default value. */
1709
1710 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->cdr;
1711 /* Store the symbol's current value into the alist entry
1712 it is currently set up for. This is so that, if the
1713 local is marked permanent, and we make it local again
1714 later in Fkill_all_local_variables, we don't lose the value. */
1715 XCONS (XCONS (tem)->car)->cdr
1716 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car);
1717 /* Switch to the symbol's default-value alist entry. */
1718 XCONS (tem)->car = tem;
1719 /* Mark it as current for buffer B. */
1720 XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car
1721 = buffer;
1722 /* Store the current value into any forwarding in the symbol. */
1723 store_symval_forwarding (sym, XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car,
1724 XCONS (tem)->cdr);
1725 }
1726 }
1727 }
1728 \f
1729 /* Find all the overlays in the current buffer that contain position POS.
1730 Return the number found, and store them in a vector in *VEC_PTR.
1731 Store in *LEN_PTR the size allocated for the vector.
1732 Store in *NEXT_PTR the next position after POS where an overlay starts,
1733 or ZV if there are no more overlays.
1734 Store in *PREV_PTR the previous position before POS where an overlay ends,
1735 or BEGV if there are no previous overlays.
1736 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
1737
1738 *VEC_PTR and *LEN_PTR should contain a valid vector and size
1739 when this function is called.
1740
1741 If EXTEND is non-zero, we make the vector bigger if necessary.
1742 If EXTEND is zero, we never extend the vector,
1743 and we store only as many overlays as will fit.
1744 But we still return the total number of overlays. */
1745
1746 int
1747 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
1748 int pos;
1749 int extend;
1750 Lisp_Object **vec_ptr;
1751 int *len_ptr;
1752 int *next_ptr;
1753 int *prev_ptr;
1754 {
1755 Lisp_Object tail, overlay, start, end, result;
1756 int idx = 0;
1757 int len = *len_ptr;
1758 Lisp_Object *vec = *vec_ptr;
1759 int next = ZV;
1760 int prev = BEGV;
1761 int inhibit_storing = 0;
1762
1763 for (tail = current_buffer->overlays_before;
1764 GC_CONSP (tail);
1765 tail = XCONS (tail)->cdr)
1766 {
1767 int startpos, endpos;
1768
1769 overlay = XCONS (tail)->car;
1770
1771 start = OVERLAY_START (overlay);
1772 end = OVERLAY_END (overlay);
1773 endpos = OVERLAY_POSITION (end);
1774 if (endpos < pos)
1775 {
1776 if (prev < endpos)
1777 prev = endpos;
1778 break;
1779 }
1780 if (endpos == pos)
1781 continue;
1782 startpos = OVERLAY_POSITION (start);
1783 if (startpos <= pos)
1784 {
1785 if (idx == len)
1786 {
1787 /* The supplied vector is full.
1788 Either make it bigger, or don't store any more in it. */
1789 if (extend)
1790 {
1791 *len_ptr = len *= 2;
1792 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1793 *vec_ptr = vec;
1794 }
1795 else
1796 inhibit_storing = 1;
1797 }
1798
1799 if (!inhibit_storing)
1800 vec[idx] = overlay;
1801 /* Keep counting overlays even if we can't return them all. */
1802 idx++;
1803 }
1804 else if (startpos < next)
1805 next = startpos;
1806 }
1807
1808 for (tail = current_buffer->overlays_after;
1809 GC_CONSP (tail);
1810 tail = XCONS (tail)->cdr)
1811 {
1812 int startpos, endpos;
1813
1814 overlay = XCONS (tail)->car;
1815
1816 start = OVERLAY_START (overlay);
1817 end = OVERLAY_END (overlay);
1818 startpos = OVERLAY_POSITION (start);
1819 if (pos < startpos)
1820 {
1821 if (startpos < next)
1822 next = startpos;
1823 break;
1824 }
1825 endpos = OVERLAY_POSITION (end);
1826 if (pos < endpos)
1827 {
1828 if (idx == len)
1829 {
1830 if (extend)
1831 {
1832 *len_ptr = len *= 2;
1833 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1834 *vec_ptr = vec;
1835 }
1836 else
1837 inhibit_storing = 1;
1838 }
1839
1840 if (!inhibit_storing)
1841 vec[idx] = overlay;
1842 idx++;
1843 }
1844 else if (endpos < pos && endpos > prev)
1845 prev = endpos;
1846 }
1847
1848 if (next_ptr)
1849 *next_ptr = next;
1850 if (prev_ptr)
1851 *prev_ptr = prev;
1852 return idx;
1853 }
1854 \f
1855 /* Find all the overlays in the current buffer that overlap the range BEG-END
1856 or are empty at BEG.
1857
1858 Return the number found, and store them in a vector in *VEC_PTR.
1859 Store in *LEN_PTR the size allocated for the vector.
1860 Store in *NEXT_PTR the next position after POS where an overlay starts,
1861 or ZV if there are no more overlays.
1862 Store in *PREV_PTR the previous position before POS where an overlay ends,
1863 or BEGV if there are no previous overlays.
1864 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
1865
1866 *VEC_PTR and *LEN_PTR should contain a valid vector and size
1867 when this function is called.
1868
1869 If EXTEND is non-zero, we make the vector bigger if necessary.
1870 If EXTEND is zero, we never extend the vector,
1871 and we store only as many overlays as will fit.
1872 But we still return the total number of overlays. */
1873
1874 int
1875 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
1876 int beg, end;
1877 int extend;
1878 Lisp_Object **vec_ptr;
1879 int *len_ptr;
1880 int *next_ptr;
1881 int *prev_ptr;
1882 {
1883 Lisp_Object tail, overlay, ostart, oend, result;
1884 int idx = 0;
1885 int len = *len_ptr;
1886 Lisp_Object *vec = *vec_ptr;
1887 int next = ZV;
1888 int prev = BEGV;
1889 int inhibit_storing = 0;
1890
1891 for (tail = current_buffer->overlays_before;
1892 GC_CONSP (tail);
1893 tail = XCONS (tail)->cdr)
1894 {
1895 int startpos, endpos;
1896
1897 overlay = XCONS (tail)->car;
1898
1899 ostart = OVERLAY_START (overlay);
1900 oend = OVERLAY_END (overlay);
1901 endpos = OVERLAY_POSITION (oend);
1902 if (endpos < beg)
1903 {
1904 if (prev < endpos)
1905 prev = endpos;
1906 break;
1907 }
1908 startpos = OVERLAY_POSITION (ostart);
1909 /* Count an interval if it either overlaps the range
1910 or is empty at the start of the range. */
1911 if ((beg < endpos && startpos < end)
1912 || (startpos == endpos && beg == endpos))
1913 {
1914 if (idx == len)
1915 {
1916 /* The supplied vector is full.
1917 Either make it bigger, or don't store any more in it. */
1918 if (extend)
1919 {
1920 *len_ptr = len *= 2;
1921 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1922 *vec_ptr = vec;
1923 }
1924 else
1925 inhibit_storing = 1;
1926 }
1927
1928 if (!inhibit_storing)
1929 vec[idx] = overlay;
1930 /* Keep counting overlays even if we can't return them all. */
1931 idx++;
1932 }
1933 else if (startpos < next)
1934 next = startpos;
1935 }
1936
1937 for (tail = current_buffer->overlays_after;
1938 GC_CONSP (tail);
1939 tail = XCONS (tail)->cdr)
1940 {
1941 int startpos, endpos;
1942
1943 overlay = XCONS (tail)->car;
1944
1945 ostart = OVERLAY_START (overlay);
1946 oend = OVERLAY_END (overlay);
1947 startpos = OVERLAY_POSITION (ostart);
1948 if (end < startpos)
1949 {
1950 if (startpos < next)
1951 next = startpos;
1952 break;
1953 }
1954 endpos = OVERLAY_POSITION (oend);
1955 /* Count an interval if it either overlaps the range
1956 or is empty at the start of the range. */
1957 if ((beg < endpos && startpos < end)
1958 || (startpos == endpos && beg == endpos))
1959 {
1960 if (idx == len)
1961 {
1962 if (extend)
1963 {
1964 *len_ptr = len *= 2;
1965 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1966 *vec_ptr = vec;
1967 }
1968 else
1969 inhibit_storing = 1;
1970 }
1971
1972 if (!inhibit_storing)
1973 vec[idx] = overlay;
1974 idx++;
1975 }
1976 else if (endpos < beg && endpos > prev)
1977 prev = endpos;
1978 }
1979
1980 if (next_ptr)
1981 *next_ptr = next;
1982 if (prev_ptr)
1983 *prev_ptr = prev;
1984 return idx;
1985 }
1986 \f
1987 /* Fast function to just test if we're at an overlay boundary. */
1988 int
1989 overlay_touches_p (pos)
1990 int pos;
1991 {
1992 Lisp_Object tail, overlay;
1993
1994 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
1995 tail = XCONS (tail)->cdr)
1996 {
1997 int endpos;
1998
1999 overlay = XCONS (tail)->car;
2000 if (!GC_OVERLAYP (overlay))
2001 abort ();
2002
2003 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2004 if (endpos < pos)
2005 break;
2006 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2007 return 1;
2008 }
2009
2010 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2011 tail = XCONS (tail)->cdr)
2012 {
2013 int startpos;
2014
2015 overlay = XCONS (tail)->car;
2016 if (!GC_OVERLAYP (overlay))
2017 abort ();
2018
2019 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2020 if (pos < startpos)
2021 break;
2022 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2023 return 1;
2024 }
2025 return 0;
2026 }
2027 \f
2028 struct sortvec
2029 {
2030 Lisp_Object overlay;
2031 int beg, end;
2032 int priority;
2033 };
2034
2035 static int
2036 compare_overlays (s1, s2)
2037 struct sortvec *s1, *s2;
2038 {
2039 if (s1->priority != s2->priority)
2040 return s1->priority - s2->priority;
2041 if (s1->beg != s2->beg)
2042 return s1->beg - s2->beg;
2043 if (s1->end != s2->end)
2044 return s2->end - s1->end;
2045 return 0;
2046 }
2047
2048 /* Sort an array of overlays by priority. The array is modified in place.
2049 The return value is the new size; this may be smaller than the original
2050 size if some of the overlays were invalid or were window-specific. */
2051 int
2052 sort_overlays (overlay_vec, noverlays, w)
2053 Lisp_Object *overlay_vec;
2054 int noverlays;
2055 struct window *w;
2056 {
2057 int i, j;
2058 struct sortvec *sortvec;
2059 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2060
2061 /* Put the valid and relevant overlays into sortvec. */
2062
2063 for (i = 0, j = 0; i < noverlays; i++)
2064 {
2065 Lisp_Object tem;
2066 Lisp_Object overlay;
2067
2068 overlay = overlay_vec[i];
2069 if (OVERLAY_VALID (overlay)
2070 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2071 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2072 {
2073 /* If we're interested in a specific window, then ignore
2074 overlays that are limited to some other window. */
2075 if (w)
2076 {
2077 Lisp_Object window;
2078
2079 window = Foverlay_get (overlay, Qwindow);
2080 if (WINDOWP (window) && XWINDOW (window) != w)
2081 continue;
2082 }
2083
2084 /* This overlay is good and counts: put it into sortvec. */
2085 sortvec[j].overlay = overlay;
2086 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2087 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2088 tem = Foverlay_get (overlay, Qpriority);
2089 if (INTEGERP (tem))
2090 sortvec[j].priority = XINT (tem);
2091 else
2092 sortvec[j].priority = 0;
2093 j++;
2094 }
2095 }
2096 noverlays = j;
2097
2098 /* Sort the overlays into the proper order: increasing priority. */
2099
2100 if (noverlays > 1)
2101 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2102
2103 for (i = 0; i < noverlays; i++)
2104 overlay_vec[i] = sortvec[i].overlay;
2105 return (noverlays);
2106 }
2107 \f
2108 struct sortstr
2109 {
2110 Lisp_Object string, string2;
2111 int size;
2112 int priority;
2113 };
2114
2115 struct sortstrlist
2116 {
2117 struct sortstr *buf; /* An array that expands as needed; never freed. */
2118 int size; /* Allocated length of that array. */
2119 int used; /* How much of the array is currently in use. */
2120 int bytes; /* Total length of the strings in buf. */
2121 };
2122
2123 /* Buffers for storing information about the overlays touching a given
2124 position. These could be automatic variables in overlay_strings, but
2125 it's more efficient to hold onto the memory instead of repeatedly
2126 allocating and freeing it. */
2127 static struct sortstrlist overlay_heads, overlay_tails;
2128 static char *overlay_str_buf;
2129
2130 /* Allocated length of overlay_str_buf. */
2131 static int overlay_str_len;
2132
2133 /* A comparison function suitable for passing to qsort. */
2134 static int
2135 cmp_for_strings (as1, as2)
2136 char *as1, *as2;
2137 {
2138 struct sortstr *s1 = (struct sortstr *)as1;
2139 struct sortstr *s2 = (struct sortstr *)as2;
2140 if (s1->size != s2->size)
2141 return s2->size - s1->size;
2142 if (s1->priority != s2->priority)
2143 return s1->priority - s2->priority;
2144 return 0;
2145 }
2146
2147 static void
2148 record_overlay_string (ssl, str, str2, pri, size)
2149 struct sortstrlist *ssl;
2150 Lisp_Object str, str2, pri;
2151 int size;
2152 {
2153 if (ssl->used == ssl->size)
2154 {
2155 if (ssl->buf)
2156 ssl->size *= 2;
2157 else
2158 ssl->size = 5;
2159 ssl->buf = ((struct sortstr *)
2160 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2161 }
2162 ssl->buf[ssl->used].string = str;
2163 ssl->buf[ssl->used].string2 = str2;
2164 ssl->buf[ssl->used].size = size;
2165 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2166 ssl->used++;
2167 ssl->bytes += XSTRING (str)->size;
2168 if (STRINGP (str2))
2169 ssl->bytes += XSTRING (str2)->size;
2170 }
2171
2172 /* Return the concatenation of the strings associated with overlays that
2173 begin or end at POS, ignoring overlays that are specific to a window
2174 other than W. The strings are concatenated in the appropriate order:
2175 shorter overlays nest inside longer ones, and higher priority inside
2176 lower. Normally all of the after-strings come first, but zero-sized
2177 overlays have their after-strings ride along with the before-strings
2178 because it would look strange to print them inside-out.
2179
2180 Returns the string length, and stores the contents indirectly through
2181 PSTR, if that variable is non-null. The string may be overwritten by
2182 subsequent calls. */
2183
2184 int
2185 overlay_strings (pos, w, pstr)
2186 int pos;
2187 struct window *w;
2188 unsigned char **pstr;
2189 {
2190 Lisp_Object ov, overlay, window, str;
2191 int startpos, endpos;
2192
2193 overlay_heads.used = overlay_heads.bytes = 0;
2194 overlay_tails.used = overlay_tails.bytes = 0;
2195 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCONS (ov)->cdr)
2196 {
2197 overlay = XCONS (ov)->car;
2198 if (!OVERLAYP (overlay))
2199 abort ();
2200
2201 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2202 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2203 if (endpos < pos)
2204 break;
2205 if (endpos != pos && startpos != pos)
2206 continue;
2207 window = Foverlay_get (overlay, Qwindow);
2208 if (WINDOWP (window) && XWINDOW (window) != w)
2209 continue;
2210 if (startpos == pos
2211 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2212 record_overlay_string (&overlay_heads, str,
2213 (startpos == endpos
2214 ? Foverlay_get (overlay, Qafter_string)
2215 : Qnil),
2216 Foverlay_get (overlay, Qpriority),
2217 endpos - startpos);
2218 else if (endpos == pos
2219 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2220 record_overlay_string (&overlay_tails, str, Qnil,
2221 Foverlay_get (overlay, Qpriority),
2222 endpos - startpos);
2223 }
2224 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCONS (ov)->cdr)
2225 {
2226 overlay = XCONS (ov)->car;
2227 if (!OVERLAYP (overlay))
2228 abort ();
2229
2230 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2231 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2232 if (startpos > pos)
2233 break;
2234 if (endpos != pos && startpos != pos)
2235 continue;
2236 window = Foverlay_get (overlay, Qwindow);
2237 if (WINDOWP (window) && XWINDOW (window) != w)
2238 continue;
2239 if (startpos == pos
2240 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2241 record_overlay_string (&overlay_heads, str,
2242 (startpos == endpos
2243 ? Foverlay_get (overlay, Qafter_string)
2244 : Qnil),
2245 Foverlay_get (overlay, Qpriority),
2246 endpos - startpos);
2247 else if (endpos == pos
2248 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2249 record_overlay_string (&overlay_tails, str, Qnil,
2250 Foverlay_get (overlay, Qpriority),
2251 endpos - startpos);
2252 }
2253 if (overlay_tails.used > 1)
2254 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2255 cmp_for_strings);
2256 if (overlay_heads.used > 1)
2257 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2258 cmp_for_strings);
2259 if (overlay_heads.bytes || overlay_tails.bytes)
2260 {
2261 Lisp_Object tem;
2262 int i;
2263 char *p;
2264 int total = overlay_heads.bytes + overlay_tails.bytes;
2265
2266 if (total > overlay_str_len)
2267 overlay_str_buf = (char *)xrealloc (overlay_str_buf,
2268 overlay_str_len = total);
2269 p = overlay_str_buf;
2270 for (i = overlay_tails.used; --i >= 0;)
2271 {
2272 tem = overlay_tails.buf[i].string;
2273 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2274 p += XSTRING (tem)->size;
2275 }
2276 for (i = 0; i < overlay_heads.used; ++i)
2277 {
2278 tem = overlay_heads.buf[i].string;
2279 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2280 p += XSTRING (tem)->size;
2281 tem = overlay_heads.buf[i].string2;
2282 if (STRINGP (tem))
2283 {
2284 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2285 p += XSTRING (tem)->size;
2286 }
2287 }
2288 if (p != overlay_str_buf + total)
2289 abort ();
2290 if (pstr)
2291 *pstr = overlay_str_buf;
2292 return total;
2293 }
2294 return 0;
2295 }
2296 \f
2297 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2298
2299 void
2300 recenter_overlay_lists (buf, pos)
2301 struct buffer *buf;
2302 int pos;
2303 {
2304 Lisp_Object overlay, tail, next, prev, beg, end;
2305
2306 /* See if anything in overlays_before should move to overlays_after. */
2307
2308 /* We don't strictly need prev in this loop; it should always be nil.
2309 But we use it for symmetry and in case that should cease to be true
2310 with some future change. */
2311 prev = Qnil;
2312 for (tail = buf->overlays_before;
2313 CONSP (tail);
2314 prev = tail, tail = next)
2315 {
2316 next = XCONS (tail)->cdr;
2317 overlay = XCONS (tail)->car;
2318
2319 /* If the overlay is not valid, get rid of it. */
2320 if (!OVERLAY_VALID (overlay))
2321 #if 1
2322 abort ();
2323 #else
2324 {
2325 /* Splice the cons cell TAIL out of overlays_before. */
2326 if (!NILP (prev))
2327 XCONS (prev)->cdr = next;
2328 else
2329 buf->overlays_before = next;
2330 tail = prev;
2331 continue;
2332 }
2333 #endif
2334
2335 beg = OVERLAY_START (overlay);
2336 end = OVERLAY_END (overlay);
2337
2338 if (OVERLAY_POSITION (end) > pos)
2339 {
2340 /* OVERLAY needs to be moved. */
2341 int where = OVERLAY_POSITION (beg);
2342 Lisp_Object other, other_prev;
2343
2344 /* Splice the cons cell TAIL out of overlays_before. */
2345 if (!NILP (prev))
2346 XCONS (prev)->cdr = next;
2347 else
2348 buf->overlays_before = next;
2349
2350 /* Search thru overlays_after for where to put it. */
2351 other_prev = Qnil;
2352 for (other = buf->overlays_after;
2353 CONSP (other);
2354 other_prev = other, other = XCONS (other)->cdr)
2355 {
2356 Lisp_Object otherbeg, otheroverlay, follower;
2357 int win;
2358
2359 otheroverlay = XCONS (other)->car;
2360 if (! OVERLAY_VALID (otheroverlay))
2361 abort ();
2362
2363 otherbeg = OVERLAY_START (otheroverlay);
2364 if (OVERLAY_POSITION (otherbeg) >= where)
2365 break;
2366 }
2367
2368 /* Add TAIL to overlays_after before OTHER. */
2369 XCONS (tail)->cdr = other;
2370 if (!NILP (other_prev))
2371 XCONS (other_prev)->cdr = tail;
2372 else
2373 buf->overlays_after = tail;
2374 tail = prev;
2375 }
2376 else
2377 /* We've reached the things that should stay in overlays_before.
2378 All the rest of overlays_before must end even earlier,
2379 so stop now. */
2380 break;
2381 }
2382
2383 /* See if anything in overlays_after should be in overlays_before. */
2384 prev = Qnil;
2385 for (tail = buf->overlays_after;
2386 CONSP (tail);
2387 prev = tail, tail = next)
2388 {
2389 next = XCONS (tail)->cdr;
2390 overlay = XCONS (tail)->car;
2391
2392 /* If the overlay is not valid, get rid of it. */
2393 if (!OVERLAY_VALID (overlay))
2394 #if 1
2395 abort ();
2396 #else
2397 {
2398 /* Splice the cons cell TAIL out of overlays_after. */
2399 if (!NILP (prev))
2400 XCONS (prev)->cdr = next;
2401 else
2402 buf->overlays_after = next;
2403 tail = prev;
2404 continue;
2405 }
2406 #endif
2407
2408 beg = OVERLAY_START (overlay);
2409 end = OVERLAY_END (overlay);
2410
2411 /* Stop looking, when we know that nothing further
2412 can possibly end before POS. */
2413 if (OVERLAY_POSITION (beg) > pos)
2414 break;
2415
2416 if (OVERLAY_POSITION (end) <= pos)
2417 {
2418 /* OVERLAY needs to be moved. */
2419 int where = OVERLAY_POSITION (end);
2420 Lisp_Object other, other_prev;
2421
2422 /* Splice the cons cell TAIL out of overlays_after. */
2423 if (!NILP (prev))
2424 XCONS (prev)->cdr = next;
2425 else
2426 buf->overlays_after = next;
2427
2428 /* Search thru overlays_before for where to put it. */
2429 other_prev = Qnil;
2430 for (other = buf->overlays_before;
2431 CONSP (other);
2432 other_prev = other, other = XCONS (other)->cdr)
2433 {
2434 Lisp_Object otherend, otheroverlay;
2435 int win;
2436
2437 otheroverlay = XCONS (other)->car;
2438 if (! OVERLAY_VALID (otheroverlay))
2439 abort ();
2440
2441 otherend = OVERLAY_END (otheroverlay);
2442 if (OVERLAY_POSITION (otherend) <= where)
2443 break;
2444 }
2445
2446 /* Add TAIL to overlays_before before OTHER. */
2447 XCONS (tail)->cdr = other;
2448 if (!NILP (other_prev))
2449 XCONS (other_prev)->cdr = tail;
2450 else
2451 buf->overlays_before = tail;
2452 tail = prev;
2453 }
2454 }
2455
2456 XSETFASTINT (buf->overlay_center, pos);
2457 }
2458
2459 void
2460 adjust_overlays_for_insert (pos, length)
2461 int pos;
2462 int length;
2463 {
2464 /* After an insertion, the lists are still sorted properly,
2465 but we may need to update the value of the overlay center. */
2466 if (XFASTINT (current_buffer->overlay_center) >= pos)
2467 XSETFASTINT (current_buffer->overlay_center,
2468 XFASTINT (current_buffer->overlay_center) + length);
2469 }
2470
2471 void
2472 adjust_overlays_for_delete (pos, length)
2473 int pos;
2474 int length;
2475 {
2476 if (XFASTINT (current_buffer->overlay_center) < pos)
2477 /* The deletion was to our right. No change needed; the before- and
2478 after-lists are still consistent. */
2479 ;
2480 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
2481 /* The deletion was to our left. We need to adjust the center value
2482 to account for the change in position, but the lists are consistent
2483 given the new value. */
2484 XSETFASTINT (current_buffer->overlay_center,
2485 XFASTINT (current_buffer->overlay_center) - length);
2486 else
2487 /* We're right in the middle. There might be things on the after-list
2488 that now belong on the before-list. Recentering will move them,
2489 and also update the center point. */
2490 recenter_overlay_lists (current_buffer, pos);
2491 }
2492
2493 /* Fix up overlays that were garbled as a result of permuting markers
2494 in the range START through END. Any overlay with at least one
2495 endpoint in this range will need to be unlinked from the overlay
2496 list and reinserted in its proper place.
2497 Such an overlay might even have negative size at this point.
2498 If so, we'll reverse the endpoints. Can you think of anything
2499 better to do in this situation? */
2500 void
2501 fix_overlays_in_range (start, end)
2502 register int start, end;
2503 {
2504 Lisp_Object tem, overlay;
2505 Lisp_Object before_list, after_list;
2506 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
2507 int startpos, endpos;
2508
2509 /* This algorithm shifts links around instead of consing and GCing.
2510 The loop invariant is that before_list (resp. after_list) is a
2511 well-formed list except that its last element, the one that
2512 *pbefore (resp. *pafter) points to, is still uninitialized.
2513 So it's not a bug that before_list isn't initialized, although
2514 it may look strange. */
2515 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
2516 {
2517 overlay = XCONS (*ptail)->car;
2518 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2519 if (endpos < start)
2520 break;
2521 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2522 if (endpos < end
2523 || (startpos >= start && startpos < end))
2524 {
2525 /* If the overlay is backwards, fix that now. */
2526 if (startpos > endpos)
2527 {
2528 int tem;
2529 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2530 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2531 tem = startpos; startpos = endpos; endpos = tem;
2532 }
2533 /* Add it to the end of the wrong list. Later on,
2534 recenter_overlay_lists will move it to the right place. */
2535 if (endpos < XINT (current_buffer->overlay_center))
2536 {
2537 *pafter = *ptail;
2538 pafter = &XCONS (*ptail)->cdr;
2539 }
2540 else
2541 {
2542 *pbefore = *ptail;
2543 pbefore = &XCONS (*ptail)->cdr;
2544 }
2545 *ptail = XCONS (*ptail)->cdr;
2546 }
2547 else
2548 ptail = &XCONS (*ptail)->cdr;
2549 }
2550 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
2551 {
2552 overlay = XCONS (*ptail)->car;
2553 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2554 if (startpos >= end)
2555 break;
2556 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2557 if (startpos >= start
2558 || (endpos >= start && endpos < end))
2559 {
2560 if (startpos > endpos)
2561 {
2562 int tem;
2563 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2564 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2565 tem = startpos; startpos = endpos; endpos = tem;
2566 }
2567 if (endpos < XINT (current_buffer->overlay_center))
2568 {
2569 *pafter = *ptail;
2570 pafter = &XCONS (*ptail)->cdr;
2571 }
2572 else
2573 {
2574 *pbefore = *ptail;
2575 pbefore = &XCONS (*ptail)->cdr;
2576 }
2577 *ptail = XCONS (*ptail)->cdr;
2578 }
2579 else
2580 ptail = &XCONS (*ptail)->cdr;
2581 }
2582
2583 /* Splice the constructed (wrong) lists into the buffer's lists,
2584 and let the recenter function make it sane again. */
2585 *pbefore = current_buffer->overlays_before;
2586 current_buffer->overlays_before = before_list;
2587 recenter_overlay_lists (current_buffer,
2588 XINT (current_buffer->overlay_center));
2589
2590 *pafter = current_buffer->overlays_after;
2591 current_buffer->overlays_after = after_list;
2592 recenter_overlay_lists (current_buffer,
2593 XINT (current_buffer->overlay_center));
2594 }
2595
2596 /* We have two types of overlay: the one whose ending marker is
2597 after-insertion-marker (this is the usual case) and the one whose
2598 ending marker is before-insertion-marker. When `overlays_before'
2599 contains overlays of the latter type and the former type in this
2600 order and both overlays end at inserting position, inserting a text
2601 increases only the ending marker of the latter type, which results
2602 in incorrect ordering of `overlays_before'.
2603
2604 This function fixes ordering of overlays in the slot
2605 `overlays_before' of the buffer *BP. Before the insertion, `point'
2606 was at PREV, and now is at POS. */
2607
2608 fix_overlays_before (bp, prev, pos)
2609 struct buffer *bp;
2610 int prev, pos;
2611 {
2612 Lisp_Object *tailp = &bp->overlays_before;
2613 Lisp_Object *right_place;
2614 int end;
2615
2616 /* After the insertion, the several overlays may be in incorrect
2617 order. The possibility is that, in the list `overlays_before',
2618 an overlay which ends at POS appears after an overlay which ends
2619 at PREV. Since POS is greater than PREV, we must fix the
2620 ordering of these overlays, by moving overlays ends at POS before
2621 the overlays ends at PREV. */
2622
2623 /* At first, find a place where disordered overlays should be linked
2624 in. It is where an overlay which end before POS exists. (i.e. an
2625 overlay whose ending marker is after-insertion-marker if disorder
2626 exists). */
2627 while (!NILP (*tailp)
2628 && ((end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car)))
2629 >= pos))
2630 tailp = &XCONS (*tailp)->cdr;
2631
2632 /* If we don't find such an overlay,
2633 or the found one ends before PREV,
2634 or the found one is the last one in the list,
2635 we don't have to fix anything. */
2636 if (NILP (*tailp)
2637 || end < prev
2638 || NILP (XCONS (*tailp)->cdr))
2639 return;
2640
2641 right_place = tailp;
2642 tailp = &XCONS (*tailp)->cdr;
2643
2644 /* Now, end position of overlays in the list *TAILP should be before
2645 or equal to PREV. In the loop, an overlay which ends at POS is
2646 moved ahead to the place pointed by RIGHT_PLACE. If we found an
2647 overlay which ends before PREV, the remaining overlays are in
2648 correct order. */
2649 while (!NILP (*tailp))
2650 {
2651 end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car));
2652
2653 if (end == pos)
2654 { /* This overlay is disordered. */
2655 Lisp_Object found = *tailp;
2656
2657 /* Unlink the found overlay. */
2658 *tailp = XCONS (found)->cdr;
2659 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
2660 XCONS (found)->cdr = *right_place;
2661 /* Link it into the right place. */
2662 *right_place = found;
2663 }
2664 else if (end == prev)
2665 tailp = &XCONS (*tailp)->cdr;
2666 else /* No more disordered overlay. */
2667 break;
2668 }
2669 }
2670 \f
2671 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
2672 "Return t if OBJECT is an overlay.")
2673 (object)
2674 Lisp_Object object;
2675 {
2676 return (OVERLAYP (object) ? Qt : Qnil);
2677 }
2678
2679 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
2680 "Create a new overlay with range BEG to END in BUFFER.\n\
2681 If omitted, BUFFER defaults to the current buffer.\n\
2682 BEG and END may be integers or markers.\n\
2683 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
2684 front delimiter advance when text is inserted there.\n\
2685 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
2686 rear delimiter advance when text is inserted there.")
2687 (beg, end, buffer, front_advance, rear_advance)
2688 Lisp_Object beg, end, buffer;
2689 Lisp_Object front_advance, rear_advance;
2690 {
2691 Lisp_Object overlay;
2692 struct buffer *b;
2693
2694 if (NILP (buffer))
2695 XSETBUFFER (buffer, current_buffer);
2696 else
2697 CHECK_BUFFER (buffer, 2);
2698 if (MARKERP (beg)
2699 && ! EQ (Fmarker_buffer (beg), buffer))
2700 error ("Marker points into wrong buffer");
2701 if (MARKERP (end)
2702 && ! EQ (Fmarker_buffer (end), buffer))
2703 error ("Marker points into wrong buffer");
2704
2705 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2706 CHECK_NUMBER_COERCE_MARKER (end, 1);
2707
2708 if (XINT (beg) > XINT (end))
2709 {
2710 Lisp_Object temp;
2711 temp = beg; beg = end; end = temp;
2712 }
2713
2714 b = XBUFFER (buffer);
2715
2716 beg = Fset_marker (Fmake_marker (), beg, buffer);
2717 end = Fset_marker (Fmake_marker (), end, buffer);
2718
2719 if (!NILP (front_advance))
2720 XMARKER (beg)->insertion_type = 1;
2721 if (!NILP (rear_advance))
2722 XMARKER (end)->insertion_type = 1;
2723
2724 overlay = allocate_misc ();
2725 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
2726 XOVERLAY (overlay)->start = beg;
2727 XOVERLAY (overlay)->end = end;
2728 XOVERLAY (overlay)->plist = Qnil;
2729
2730 /* Put the new overlay on the wrong list. */
2731 end = OVERLAY_END (overlay);
2732 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2733 b->overlays_after = Fcons (overlay, b->overlays_after);
2734 else
2735 b->overlays_before = Fcons (overlay, b->overlays_before);
2736
2737 /* This puts it in the right list, and in the right order. */
2738 recenter_overlay_lists (b, XINT (b->overlay_center));
2739
2740 /* We don't need to redisplay the region covered by the overlay, because
2741 the overlay has no properties at the moment. */
2742
2743 return overlay;
2744 }
2745 \f
2746 /* Mark a section of BUF as needing redisplay because of overlays changes. */
2747
2748 static void
2749 modify_overlay (buf, start, end)
2750 struct buffer *buf;
2751 int start, end;
2752 {
2753 if (start == end)
2754 return;
2755
2756 if (start > end)
2757 {
2758 int temp = start;
2759 start = end; end = temp;
2760 }
2761
2762 /* If this is a buffer not in the selected window,
2763 we must do other windows. */
2764 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2765 windows_or_buffers_changed = 1;
2766 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2767 else if (buf != current_buffer)
2768 windows_or_buffers_changed = 1;
2769 /* If multiple windows show this buffer, we must do other windows. */
2770 else if (buffer_shared > 1)
2771 windows_or_buffers_changed = 1;
2772 else
2773 {
2774 if (unchanged_modified == MODIFF
2775 && overlay_unchanged_modified == OVERLAY_MODIFF)
2776 {
2777 beg_unchanged = start - BEG;
2778 end_unchanged = Z - end;
2779 }
2780 else
2781 {
2782 if (Z - end < end_unchanged)
2783 end_unchanged = Z - end;
2784 if (start - BEG < beg_unchanged)
2785 beg_unchanged = start - BEG;
2786 }
2787 }
2788
2789 ++OVERLAY_MODIFF;
2790 }
2791
2792 \f\f
2793 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
2794 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
2795 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
2796 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
2797 buffer.")
2798 (overlay, beg, end, buffer)
2799 Lisp_Object overlay, beg, end, buffer;
2800 {
2801 struct buffer *b, *ob;
2802 Lisp_Object obuffer;
2803 int count = specpdl_ptr - specpdl;
2804
2805 CHECK_OVERLAY (overlay, 0);
2806 if (NILP (buffer))
2807 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2808 if (NILP (buffer))
2809 XSETBUFFER (buffer, current_buffer);
2810 CHECK_BUFFER (buffer, 3);
2811
2812 if (MARKERP (beg)
2813 && ! EQ (Fmarker_buffer (beg), buffer))
2814 error ("Marker points into wrong buffer");
2815 if (MARKERP (end)
2816 && ! EQ (Fmarker_buffer (end), buffer))
2817 error ("Marker points into wrong buffer");
2818
2819 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2820 CHECK_NUMBER_COERCE_MARKER (end, 1);
2821
2822 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
2823 return Fdelete_overlay (overlay);
2824
2825 if (XINT (beg) > XINT (end))
2826 {
2827 Lisp_Object temp;
2828 temp = beg; beg = end; end = temp;
2829 }
2830
2831 specbind (Qinhibit_quit, Qt);
2832
2833 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
2834 b = XBUFFER (buffer);
2835 ob = XBUFFER (obuffer);
2836
2837 /* If the overlay has changed buffers, do a thorough redisplay. */
2838 if (!EQ (buffer, obuffer))
2839 {
2840 /* Redisplay where the overlay was. */
2841 if (!NILP (obuffer))
2842 {
2843 Lisp_Object o_beg;
2844 Lisp_Object o_end;
2845
2846 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2847 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
2848
2849 modify_overlay (ob, XINT (o_beg), XINT (o_end));
2850 }
2851
2852 /* Redisplay where the overlay is going to be. */
2853 modify_overlay (b, XINT (beg), XINT (end));
2854 }
2855 else
2856 /* Redisplay the area the overlay has just left, or just enclosed. */
2857 {
2858 Lisp_Object o_beg;
2859 Lisp_Object o_end;
2860 int change_beg, change_end;
2861
2862 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2863 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
2864
2865 if (XINT (o_beg) == XINT (beg))
2866 modify_overlay (b, XINT (o_end), XINT (end));
2867 else if (XINT (o_end) == XINT (end))
2868 modify_overlay (b, XINT (o_beg), XINT (beg));
2869 else
2870 {
2871 if (XINT (beg) < XINT (o_beg)) o_beg = beg;
2872 if (XINT (end) > XINT (o_end)) o_end = end;
2873 modify_overlay (b, XINT (o_beg), XINT (o_end));
2874 }
2875 }
2876
2877 if (!NILP (obuffer))
2878 {
2879 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
2880 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
2881 }
2882
2883 Fset_marker (OVERLAY_START (overlay), beg, buffer);
2884 Fset_marker (OVERLAY_END (overlay), end, buffer);
2885
2886 /* Put the overlay on the wrong list. */
2887 end = OVERLAY_END (overlay);
2888 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2889 b->overlays_after = Fcons (overlay, b->overlays_after);
2890 else
2891 b->overlays_before = Fcons (overlay, b->overlays_before);
2892
2893 /* This puts it in the right list, and in the right order. */
2894 recenter_overlay_lists (b, XINT (b->overlay_center));
2895
2896 return unbind_to (count, overlay);
2897 }
2898
2899 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
2900 "Delete the overlay OVERLAY from its buffer.")
2901 (overlay)
2902 Lisp_Object overlay;
2903 {
2904 Lisp_Object buffer;
2905 struct buffer *b;
2906 int count = specpdl_ptr - specpdl;
2907
2908 CHECK_OVERLAY (overlay, 0);
2909
2910 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2911 if (NILP (buffer))
2912 return Qnil;
2913
2914 b = XBUFFER (buffer);
2915
2916 specbind (Qinhibit_quit, Qt);
2917
2918 b->overlays_before = Fdelq (overlay, b->overlays_before);
2919 b->overlays_after = Fdelq (overlay, b->overlays_after);
2920
2921 modify_overlay (b,
2922 marker_position (OVERLAY_START (overlay)),
2923 marker_position (OVERLAY_END (overlay)));
2924
2925 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
2926 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
2927
2928 return unbind_to (count, Qnil);
2929 }
2930 \f
2931 /* Overlay dissection functions. */
2932
2933 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
2934 "Return the position at which OVERLAY starts.")
2935 (overlay)
2936 Lisp_Object overlay;
2937 {
2938 CHECK_OVERLAY (overlay, 0);
2939
2940 return (Fmarker_position (OVERLAY_START (overlay)));
2941 }
2942
2943 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
2944 "Return the position at which OVERLAY ends.")
2945 (overlay)
2946 Lisp_Object overlay;
2947 {
2948 CHECK_OVERLAY (overlay, 0);
2949
2950 return (Fmarker_position (OVERLAY_END (overlay)));
2951 }
2952
2953 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
2954 "Return the buffer OVERLAY belongs to.")
2955 (overlay)
2956 Lisp_Object overlay;
2957 {
2958 CHECK_OVERLAY (overlay, 0);
2959
2960 return Fmarker_buffer (OVERLAY_START (overlay));
2961 }
2962
2963 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
2964 "Return a list of the properties on OVERLAY.\n\
2965 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
2966 OVERLAY.")
2967 (overlay)
2968 Lisp_Object overlay;
2969 {
2970 CHECK_OVERLAY (overlay, 0);
2971
2972 return Fcopy_sequence (XOVERLAY (overlay)->plist);
2973 }
2974
2975 \f
2976 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
2977 "Return a list of the overlays that contain position POS.")
2978 (pos)
2979 Lisp_Object pos;
2980 {
2981 int noverlays;
2982 Lisp_Object *overlay_vec;
2983 int len;
2984 Lisp_Object result;
2985
2986 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2987
2988 len = 10;
2989 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2990
2991 /* Put all the overlays we want in a vector in overlay_vec.
2992 Store the length in len. */
2993 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
2994 (int *) 0, (int *) 0);
2995
2996 /* Make a list of them all. */
2997 result = Flist (noverlays, overlay_vec);
2998
2999 xfree (overlay_vec);
3000 return result;
3001 }
3002
3003 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3004 "Return a list of the overlays that overlap the region BEG ... END.\n\
3005 Overlap means that at least one character is contained within the overlay\n\
3006 and also contained within the specified region.\n\
3007 Empty overlays are included in the result if they are located at BEG\n\
3008 or between BEG and END.")
3009 (beg, end)
3010 Lisp_Object beg, end;
3011 {
3012 int noverlays;
3013 Lisp_Object *overlay_vec;
3014 int len;
3015 Lisp_Object result;
3016
3017 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3018 CHECK_NUMBER_COERCE_MARKER (end, 0);
3019
3020 len = 10;
3021 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3022
3023 /* Put all the overlays we want in a vector in overlay_vec.
3024 Store the length in len. */
3025 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3026 (int *) 0, (int *) 0);
3027
3028 /* Make a list of them all. */
3029 result = Flist (noverlays, overlay_vec);
3030
3031 xfree (overlay_vec);
3032 return result;
3033 }
3034
3035 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3036 1, 1, 0,
3037 "Return the next position after POS where an overlay starts or ends.\n\
3038 If there are no more overlay boundaries after POS, return (point-max).")
3039 (pos)
3040 Lisp_Object pos;
3041 {
3042 int noverlays;
3043 int endpos;
3044 Lisp_Object *overlay_vec;
3045 int len;
3046 int i;
3047
3048 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3049
3050 len = 10;
3051 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3052
3053 /* Put all the overlays we want in a vector in overlay_vec.
3054 Store the length in len.
3055 endpos gets the position where the next overlay starts. */
3056 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3057 &endpos, (int *) 0);
3058
3059 /* If any of these overlays ends before endpos,
3060 use its ending point instead. */
3061 for (i = 0; i < noverlays; i++)
3062 {
3063 Lisp_Object oend;
3064 int oendpos;
3065
3066 oend = OVERLAY_END (overlay_vec[i]);
3067 oendpos = OVERLAY_POSITION (oend);
3068 if (oendpos < endpos)
3069 endpos = oendpos;
3070 }
3071
3072 xfree (overlay_vec);
3073 return make_number (endpos);
3074 }
3075
3076 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3077 Sprevious_overlay_change, 1, 1, 0,
3078 "Return the previous position before POS where an overlay starts or ends.\n\
3079 If there are no more overlay boundaries before POS, return (point-min).")
3080 (pos)
3081 Lisp_Object pos;
3082 {
3083 int noverlays;
3084 int prevpos;
3085 Lisp_Object *overlay_vec;
3086 int len;
3087 int i;
3088 Lisp_Object tail;
3089
3090 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3091
3092 len = 10;
3093 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3094
3095 /* At beginning of buffer, we know the answer;
3096 avoid bug subtracting 1 below. */
3097 if (XINT (pos) == BEGV)
3098 return pos;
3099
3100 /* Put all the overlays we want in a vector in overlay_vec.
3101 Store the length in len.
3102 prevpos gets the position of an overlay end. */
3103 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3104 (int *) 0, &prevpos);
3105
3106 /* If any of these overlays starts after prevpos,
3107 maybe use its starting point instead. */
3108 for (i = 0; i < noverlays; i++)
3109 {
3110 Lisp_Object ostart;
3111 int ostartpos;
3112
3113 ostart = OVERLAY_START (overlay_vec[i]);
3114 ostartpos = OVERLAY_POSITION (ostart);
3115 if (ostartpos > prevpos && ostartpos < XINT (pos))
3116 prevpos = ostartpos;
3117 }
3118
3119 /* If any overlay ends at pos, consider its starting point too. */
3120 for (tail = current_buffer->overlays_before;
3121 GC_CONSP (tail);
3122 tail = XCONS (tail)->cdr)
3123 {
3124 Lisp_Object overlay, ostart;
3125 int ostartpos;
3126
3127 overlay = XCONS (tail)->car;
3128
3129 ostart = OVERLAY_START (overlay);
3130 ostartpos = OVERLAY_POSITION (ostart);
3131 if (ostartpos > prevpos && ostartpos < XINT (pos))
3132 prevpos = ostartpos;
3133 }
3134
3135 xfree (overlay_vec);
3136 return make_number (prevpos);
3137 }
3138 \f
3139 /* These functions are for debugging overlays. */
3140
3141 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3142 "Return a pair of lists giving all the overlays of the current buffer.\n\
3143 The car has all the overlays before the overlay center;\n\
3144 the cdr has all the overlays after the overlay center.\n\
3145 Recentering overlays moves overlays between these lists.\n\
3146 The lists you get are copies, so that changing them has no effect.\n\
3147 However, the overlays you get are the real objects that the buffer uses.")
3148 ()
3149 {
3150 Lisp_Object before, after;
3151 before = current_buffer->overlays_before;
3152 if (CONSP (before))
3153 before = Fcopy_sequence (before);
3154 after = current_buffer->overlays_after;
3155 if (CONSP (after))
3156 after = Fcopy_sequence (after);
3157
3158 return Fcons (before, after);
3159 }
3160
3161 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3162 "Recenter the overlays of the current buffer around position POS.")
3163 (pos)
3164 Lisp_Object pos;
3165 {
3166 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3167
3168 recenter_overlay_lists (current_buffer, XINT (pos));
3169 return Qnil;
3170 }
3171 \f
3172 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3173 "Get the property of overlay OVERLAY with property name PROP.")
3174 (overlay, prop)
3175 Lisp_Object overlay, prop;
3176 {
3177 Lisp_Object plist, fallback;
3178
3179 CHECK_OVERLAY (overlay, 0);
3180
3181 fallback = Qnil;
3182
3183 for (plist = XOVERLAY (overlay)->plist;
3184 CONSP (plist) && CONSP (XCONS (plist)->cdr);
3185 plist = XCONS (XCONS (plist)->cdr)->cdr)
3186 {
3187 if (EQ (XCONS (plist)->car, prop))
3188 return XCONS (XCONS (plist)->cdr)->car;
3189 else if (EQ (XCONS (plist)->car, Qcategory))
3190 {
3191 Lisp_Object tem;
3192 tem = Fcar (Fcdr (plist));
3193 if (SYMBOLP (tem))
3194 fallback = Fget (tem, prop);
3195 }
3196 }
3197
3198 return fallback;
3199 }
3200
3201 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3202 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3203 (overlay, prop, value)
3204 Lisp_Object overlay, prop, value;
3205 {
3206 Lisp_Object tail, buffer;
3207 int changed;
3208
3209 CHECK_OVERLAY (overlay, 0);
3210
3211 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3212
3213 for (tail = XOVERLAY (overlay)->plist;
3214 CONSP (tail) && CONSP (XCONS (tail)->cdr);
3215 tail = XCONS (XCONS (tail)->cdr)->cdr)
3216 if (EQ (XCONS (tail)->car, prop))
3217 {
3218 changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
3219 XCONS (XCONS (tail)->cdr)->car = value;
3220 goto found;
3221 }
3222 /* It wasn't in the list, so add it to the front. */
3223 changed = !NILP (value);
3224 XOVERLAY (overlay)->plist
3225 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3226 found:
3227 if (! NILP (buffer))
3228 {
3229 if (changed)
3230 modify_overlay (XBUFFER (buffer),
3231 marker_position (OVERLAY_START (overlay)),
3232 marker_position (OVERLAY_END (overlay)));
3233 if (EQ (prop, Qevaporate) && ! NILP (value)
3234 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3235 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3236 Fdelete_overlay (overlay);
3237 }
3238 return value;
3239 }
3240 \f
3241 /* Subroutine of report_overlay_modification. */
3242
3243 /* Lisp vector holding overlay hook functions to call.
3244 Vector elements come in pairs.
3245 Each even-index element is a list of hook functions.
3246 The following odd-index element is the overlay they came from.
3247
3248 Before the buffer change, we fill in this vector
3249 as we call overlay hook functions.
3250 After the buffer change, we get the functions to call from this vector.
3251 This way we always call the same functions before and after the change. */
3252 static Lisp_Object last_overlay_modification_hooks;
3253
3254 /* Number of elements actually used in last_overlay_modification_hooks. */
3255 static int last_overlay_modification_hooks_used;
3256
3257 /* Add one functionlist/overlay pair
3258 to the end of last_overlay_modification_hooks. */
3259
3260 static void
3261 add_overlay_mod_hooklist (functionlist, overlay)
3262 Lisp_Object functionlist, overlay;
3263 {
3264 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3265
3266 if (last_overlay_modification_hooks_used == oldsize)
3267 {
3268 Lisp_Object old;
3269 old = last_overlay_modification_hooks;
3270 last_overlay_modification_hooks
3271 = Fmake_vector (make_number (oldsize * 2), Qnil);
3272 bcopy (XVECTOR (old)->contents,
3273 XVECTOR (last_overlay_modification_hooks)->contents,
3274 sizeof (Lisp_Object) * oldsize);
3275 }
3276 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3277 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3278 }
3279 \f
3280 /* Run the modification-hooks of overlays that include
3281 any part of the text in START to END.
3282 If this change is an insertion, also
3283 run the insert-before-hooks of overlay starting at END,
3284 and the insert-after-hooks of overlay ending at START.
3285
3286 This is called both before and after the modification.
3287 AFTER is nonzero when we call after the modification.
3288
3289 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3290 When AFTER is nonzero, they are the start position,
3291 the position after the inserted new text,
3292 and the length of deleted or replaced old text. */
3293
3294 void
3295 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3296 Lisp_Object start, end;
3297 int after;
3298 Lisp_Object arg1, arg2, arg3;
3299 {
3300 Lisp_Object prop, overlay, tail;
3301 /* 1 if this change is an insertion. */
3302 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3303 int tail_copied;
3304 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3305
3306 overlay = Qnil;
3307 tail = Qnil;
3308 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3309
3310 if (after)
3311 {
3312 /* Call the functions recorded in last_overlay_modification_hooks
3313 rather than scanning the overlays again.
3314 First copy the vector contents, in case some of these hooks
3315 do subsequent modification of the buffer. */
3316 int size = last_overlay_modification_hooks_used;
3317 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3318 int i;
3319
3320 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3321 copy, size * sizeof (Lisp_Object));
3322 gcpro1.var = copy;
3323 gcpro1.nvars = size;
3324
3325 for (i = 0; i < size;)
3326 {
3327 Lisp_Object prop, overlay;
3328 prop = copy[i++];
3329 overlay = copy[i++];
3330 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3331 }
3332 UNGCPRO;
3333 return;
3334 }
3335
3336 /* We are being called before a change.
3337 Scan the overlays to find the functions to call. */
3338 last_overlay_modification_hooks_used = 0;
3339 tail_copied = 0;
3340 for (tail = current_buffer->overlays_before;
3341 CONSP (tail);
3342 tail = XCONS (tail)->cdr)
3343 {
3344 int startpos, endpos;
3345 Lisp_Object ostart, oend;
3346
3347 overlay = XCONS (tail)->car;
3348
3349 ostart = OVERLAY_START (overlay);
3350 oend = OVERLAY_END (overlay);
3351 endpos = OVERLAY_POSITION (oend);
3352 if (XFASTINT (start) > endpos)
3353 break;
3354 startpos = OVERLAY_POSITION (ostart);
3355 if (insertion && (XFASTINT (start) == startpos
3356 || XFASTINT (end) == startpos))
3357 {
3358 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3359 if (!NILP (prop))
3360 {
3361 /* Copy TAIL in case the hook recenters the overlay lists. */
3362 if (!tail_copied)
3363 tail = Fcopy_sequence (tail);
3364 tail_copied = 1;
3365 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3366 }
3367 }
3368 if (insertion && (XFASTINT (start) == endpos
3369 || XFASTINT (end) == endpos))
3370 {
3371 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3372 if (!NILP (prop))
3373 {
3374 if (!tail_copied)
3375 tail = Fcopy_sequence (tail);
3376 tail_copied = 1;
3377 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3378 }
3379 }
3380 /* Test for intersecting intervals. This does the right thing
3381 for both insertion and deletion. */
3382 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3383 {
3384 prop = Foverlay_get (overlay, Qmodification_hooks);
3385 if (!NILP (prop))
3386 {
3387 if (!tail_copied)
3388 tail = Fcopy_sequence (tail);
3389 tail_copied = 1;
3390 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3391 }
3392 }
3393 }
3394
3395 tail_copied = 0;
3396 for (tail = current_buffer->overlays_after;
3397 CONSP (tail);
3398 tail = XCONS (tail)->cdr)
3399 {
3400 int startpos, endpos;
3401 Lisp_Object ostart, oend;
3402
3403 overlay = XCONS (tail)->car;
3404
3405 ostart = OVERLAY_START (overlay);
3406 oend = OVERLAY_END (overlay);
3407 startpos = OVERLAY_POSITION (ostart);
3408 endpos = OVERLAY_POSITION (oend);
3409 if (XFASTINT (end) < startpos)
3410 break;
3411 if (insertion && (XFASTINT (start) == startpos
3412 || XFASTINT (end) == startpos))
3413 {
3414 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3415 if (!NILP (prop))
3416 {
3417 if (!tail_copied)
3418 tail = Fcopy_sequence (tail);
3419 tail_copied = 1;
3420 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3421 }
3422 }
3423 if (insertion && (XFASTINT (start) == endpos
3424 || XFASTINT (end) == endpos))
3425 {
3426 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3427 if (!NILP (prop))
3428 {
3429 if (!tail_copied)
3430 tail = Fcopy_sequence (tail);
3431 tail_copied = 1;
3432 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3433 }
3434 }
3435 /* Test for intersecting intervals. This does the right thing
3436 for both insertion and deletion. */
3437 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3438 {
3439 prop = Foverlay_get (overlay, Qmodification_hooks);
3440 if (!NILP (prop))
3441 {
3442 if (!tail_copied)
3443 tail = Fcopy_sequence (tail);
3444 tail_copied = 1;
3445 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3446 }
3447 }
3448 }
3449
3450 UNGCPRO;
3451 }
3452
3453 static void
3454 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3455 Lisp_Object list, overlay;
3456 int after;
3457 Lisp_Object arg1, arg2, arg3;
3458 {
3459 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3460
3461 GCPRO4 (list, arg1, arg2, arg3);
3462 if (! after)
3463 add_overlay_mod_hooklist (list, overlay);
3464
3465 while (!NILP (list))
3466 {
3467 if (NILP (arg3))
3468 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3469 else
3470 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
3471 list = Fcdr (list);
3472 }
3473 UNGCPRO;
3474 }
3475
3476 /* Delete any zero-sized overlays at position POS, if the `evaporate'
3477 property is set. */
3478 void
3479 evaporate_overlays (pos)
3480 int pos;
3481 {
3482 Lisp_Object tail, overlay, hit_list;
3483
3484 hit_list = Qnil;
3485 if (pos <= XFASTINT (current_buffer->overlay_center))
3486 for (tail = current_buffer->overlays_before; CONSP (tail);
3487 tail = XCONS (tail)->cdr)
3488 {
3489 int endpos;
3490 overlay = XCONS (tail)->car;
3491 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3492 if (endpos < pos)
3493 break;
3494 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
3495 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3496 hit_list = Fcons (overlay, hit_list);
3497 }
3498 else
3499 for (tail = current_buffer->overlays_after; CONSP (tail);
3500 tail = XCONS (tail)->cdr)
3501 {
3502 int startpos;
3503 overlay = XCONS (tail)->car;
3504 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3505 if (startpos > pos)
3506 break;
3507 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
3508 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3509 hit_list = Fcons (overlay, hit_list);
3510 }
3511 for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
3512 Fdelete_overlay (XCONS (hit_list)->car);
3513 }
3514 \f
3515 /* Somebody has tried to store a value with an unacceptable type
3516 into the buffer-local slot with offset OFFSET. */
3517 void
3518 buffer_slot_type_mismatch (offset)
3519 int offset;
3520 {
3521 Lisp_Object sym;
3522 char *type_name;
3523 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
3524 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
3525 {
3526 case Lisp_Int: type_name = "integers"; break;
3527 case Lisp_String: type_name = "strings"; break;
3528 case Lisp_Symbol: type_name = "symbols"; break;
3529 default:
3530 abort ();
3531 }
3532
3533 error ("only %s should be stored in the buffer-local variable %s",
3534 type_name, XSYMBOL (sym)->name->data);
3535 }
3536 \f
3537 init_buffer_once ()
3538 {
3539 register Lisp_Object tem;
3540
3541 buffer_permanent_local_flags = 0;
3542
3543 /* Make sure all markable slots in buffer_defaults
3544 are initialized reasonably, so mark_buffer won't choke. */
3545 reset_buffer (&buffer_defaults);
3546 reset_buffer_local_variables (&buffer_defaults, 1);
3547 reset_buffer (&buffer_local_symbols);
3548 reset_buffer_local_variables (&buffer_local_symbols, 1);
3549 /* Prevent GC from getting confused. */
3550 buffer_defaults.text = &buffer_defaults.own_text;
3551 buffer_local_symbols.text = &buffer_local_symbols.own_text;
3552 #ifdef USE_TEXT_PROPERTIES
3553 BUF_INTERVALS (&buffer_defaults) = 0;
3554 BUF_INTERVALS (&buffer_local_symbols) = 0;
3555 #endif
3556 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3557 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
3558
3559 /* Set up the default values of various buffer slots. */
3560 /* Must do these before making the first buffer! */
3561
3562 /* real setup is done in loaddefs.el */
3563 buffer_defaults.mode_line_format = build_string ("%-");
3564 buffer_defaults.abbrev_mode = Qnil;
3565 buffer_defaults.overwrite_mode = Qnil;
3566 buffer_defaults.case_fold_search = Qt;
3567 buffer_defaults.auto_fill_function = Qnil;
3568 buffer_defaults.selective_display = Qnil;
3569 #ifndef old
3570 buffer_defaults.selective_display_ellipses = Qt;
3571 #endif
3572 buffer_defaults.abbrev_table = Qnil;
3573 buffer_defaults.display_table = Qnil;
3574 buffer_defaults.undo_list = Qnil;
3575 buffer_defaults.mark_active = Qnil;
3576 buffer_defaults.file_format = Qnil;
3577 buffer_defaults.overlays_before = Qnil;
3578 buffer_defaults.overlays_after = Qnil;
3579 XSETFASTINT (buffer_defaults.overlay_center, BEG);
3580
3581 XSETFASTINT (buffer_defaults.tab_width, 8);
3582 buffer_defaults.truncate_lines = Qnil;
3583 buffer_defaults.ctl_arrow = Qt;
3584 buffer_defaults.direction_reversed = Qnil;
3585
3586 #ifdef DOS_NT
3587 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
3588 #endif
3589 buffer_defaults.enable_multibyte_characters = Qt;
3590 buffer_defaults.buffer_file_coding_system = Qnil;
3591 XSETFASTINT (buffer_defaults.fill_column, 70);
3592 XSETFASTINT (buffer_defaults.left_margin, 0);
3593 buffer_defaults.cache_long_line_scans = Qnil;
3594 buffer_defaults.file_truename = Qnil;
3595 XSETFASTINT (buffer_defaults.display_count, 0);
3596
3597 /* Assign the local-flags to the slots that have default values.
3598 The local flag is a bit that is used in the buffer
3599 to say that it has its own local value for the slot.
3600 The local flag bits are in the local_var_flags slot of the buffer. */
3601
3602 /* Nothing can work if this isn't true */
3603 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
3604
3605 /* 0 means not a lisp var, -1 means always local, else mask */
3606 bzero (&buffer_local_flags, sizeof buffer_local_flags);
3607 XSETINT (buffer_local_flags.filename, -1);
3608 XSETINT (buffer_local_flags.directory, -1);
3609 XSETINT (buffer_local_flags.backed_up, -1);
3610 XSETINT (buffer_local_flags.save_length, -1);
3611 XSETINT (buffer_local_flags.auto_save_file_name, -1);
3612 XSETINT (buffer_local_flags.read_only, -1);
3613 XSETINT (buffer_local_flags.major_mode, -1);
3614 XSETINT (buffer_local_flags.mode_name, -1);
3615 XSETINT (buffer_local_flags.undo_list, -1);
3616 XSETINT (buffer_local_flags.mark_active, -1);
3617 XSETINT (buffer_local_flags.point_before_scroll, -1);
3618 XSETINT (buffer_local_flags.file_truename, -1);
3619 XSETINT (buffer_local_flags.invisibility_spec, -1);
3620 XSETINT (buffer_local_flags.file_format, -1);
3621 XSETINT (buffer_local_flags.display_count, -1);
3622
3623 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
3624 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
3625 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
3626 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
3627 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
3628 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
3629 #ifndef old
3630 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
3631 #endif
3632 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
3633 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
3634 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
3635 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
3636 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
3637 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
3638 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
3639 #ifdef DOS_NT
3640 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
3641 /* Make this one a permanent local. */
3642 buffer_permanent_local_flags |= 0x4000;
3643 #endif
3644 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
3645 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
3646 XSETFASTINT (buffer_local_flags.category_table, 0x20000);
3647 XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
3648 XSETFASTINT (buffer_local_flags.enable_multibyte_characters, 0x80000);
3649 /* Make this one a permanent local. */
3650 buffer_permanent_local_flags |= 0x80000;
3651 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x100000);
3652 /* Make this one a permanent local. */
3653 buffer_permanent_local_flags |= 0x100000;
3654
3655 Vbuffer_alist = Qnil;
3656 current_buffer = 0;
3657 all_buffers = 0;
3658
3659 QSFundamental = build_string ("Fundamental");
3660
3661 Qfundamental_mode = intern ("fundamental-mode");
3662 buffer_defaults.major_mode = Qfundamental_mode;
3663
3664 Qmode_class = intern ("mode-class");
3665
3666 Qprotected_field = intern ("protected-field");
3667
3668 Qpermanent_local = intern ("permanent-local");
3669
3670 Qkill_buffer_hook = intern ("kill-buffer-hook");
3671
3672 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
3673
3674 /* super-magic invisible buffer */
3675 Vbuffer_alist = Qnil;
3676
3677 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3678 }
3679
3680 init_buffer ()
3681 {
3682 char buf[MAXPATHLEN+1];
3683 char *pwd;
3684 struct stat dotstat, pwdstat;
3685 Lisp_Object temp;
3686 int rc;
3687
3688 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3689
3690 /* If PWD is accurate, use it instead of calling getwd. This is faster
3691 when PWD is right, and may avoid a fatal error. */
3692 if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
3693 && stat (pwd, &pwdstat) == 0
3694 && stat (".", &dotstat) == 0
3695 && dotstat.st_ino == pwdstat.st_ino
3696 && dotstat.st_dev == pwdstat.st_dev
3697 && strlen (pwd) < MAXPATHLEN)
3698 strcpy (buf, pwd);
3699 else if (getwd (buf) == 0)
3700 fatal ("`getwd' failed: %s\n", buf);
3701
3702 #ifndef VMS
3703 /* Maybe this should really use some standard subroutine
3704 whose definition is filename syntax dependent. */
3705 rc = strlen (buf);
3706 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
3707 {
3708 buf[rc] = DIRECTORY_SEP;
3709 buf[rc + 1] = '\0';
3710 }
3711 #endif /* not VMS */
3712
3713 current_buffer->directory = build_string (buf);
3714
3715 /* Add /: to the front of the name
3716 if it would otherwise be treated as magic. */
3717 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
3718 if (! NILP (temp))
3719 current_buffer->directory
3720 = concat2 (build_string ("/:"), current_buffer->directory);
3721
3722 temp = get_minibuffer (0);
3723 XBUFFER (temp)->directory = current_buffer->directory;
3724 }
3725
3726 /* initialize the buffer routines */
3727 syms_of_buffer ()
3728 {
3729 extern Lisp_Object Qdisabled;
3730
3731 staticpro (&last_overlay_modification_hooks);
3732 last_overlay_modification_hooks
3733 = Fmake_vector (make_number (10), Qnil);
3734
3735 staticpro (&Vbuffer_defaults);
3736 staticpro (&Vbuffer_local_symbols);
3737 staticpro (&Qfundamental_mode);
3738 staticpro (&Qmode_class);
3739 staticpro (&QSFundamental);
3740 staticpro (&Vbuffer_alist);
3741 staticpro (&Qprotected_field);
3742 staticpro (&Qpermanent_local);
3743 staticpro (&Qkill_buffer_hook);
3744 Qoverlayp = intern ("overlayp");
3745 staticpro (&Qoverlayp);
3746 Qevaporate = intern ("evaporate");
3747 staticpro (&Qevaporate);
3748 Qmodification_hooks = intern ("modification-hooks");
3749 staticpro (&Qmodification_hooks);
3750 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
3751 staticpro (&Qinsert_in_front_hooks);
3752 Qinsert_behind_hooks = intern ("insert-behind-hooks");
3753 staticpro (&Qinsert_behind_hooks);
3754 Qget_file_buffer = intern ("get-file-buffer");
3755 staticpro (&Qget_file_buffer);
3756 Qpriority = intern ("priority");
3757 staticpro (&Qpriority);
3758 Qwindow = intern ("window");
3759 staticpro (&Qwindow);
3760 Qbefore_string = intern ("before-string");
3761 staticpro (&Qbefore_string);
3762 Qafter_string = intern ("after-string");
3763 staticpro (&Qafter_string);
3764 Qfirst_change_hook = intern ("first-change-hook");
3765 staticpro (&Qfirst_change_hook);
3766 Qbefore_change_functions = intern ("before-change-functions");
3767 staticpro (&Qbefore_change_functions);
3768 Qafter_change_functions = intern ("after-change-functions");
3769 staticpro (&Qafter_change_functions);
3770
3771 Fput (Qprotected_field, Qerror_conditions,
3772 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
3773 Fput (Qprotected_field, Qerror_message,
3774 build_string ("Attempt to modify a protected field"));
3775
3776 /* All these use DEFVAR_LISP_NOPRO because the slots in
3777 buffer_defaults will all be marked via Vbuffer_defaults. */
3778
3779 DEFVAR_LISP_NOPRO ("default-mode-line-format",
3780 &buffer_defaults.mode_line_format,
3781 "Default value of `mode-line-format' for buffers that don't override it.\n\
3782 This is the same as (default-value 'mode-line-format).");
3783
3784 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
3785 &buffer_defaults.abbrev_mode,
3786 "Default value of `abbrev-mode' for buffers that do not override it.\n\
3787 This is the same as (default-value 'abbrev-mode).");
3788
3789 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
3790 &buffer_defaults.ctl_arrow,
3791 "Default value of `ctl-arrow' for buffers that do not override it.\n\
3792 This is the same as (default-value 'ctl-arrow).");
3793
3794 DEFVAR_LISP_NOPRO ("default-direction-reversed",
3795 &buffer_defaults.direction_reversed,
3796 "Default value of `direction_reversed' for buffers that do not override it.\n\
3797 This is the same as (default-value 'direction-reversed).");
3798
3799 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
3800 &buffer_defaults.enable_multibyte_characters,
3801 "Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
3802 This is the same as (default-value 'enable-multibyte-characters).");
3803
3804 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
3805 &buffer_defaults.buffer_file_coding_system,
3806 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
3807 This is the same as (default-value 'buffer-file-coding-system).");
3808
3809 DEFVAR_LISP_NOPRO ("default-truncate-lines",
3810 &buffer_defaults.truncate_lines,
3811 "Default value of `truncate-lines' for buffers that do not override it.\n\
3812 This is the same as (default-value 'truncate-lines).");
3813
3814 DEFVAR_LISP_NOPRO ("default-fill-column",
3815 &buffer_defaults.fill_column,
3816 "Default value of `fill-column' for buffers that do not override it.\n\
3817 This is the same as (default-value 'fill-column).");
3818
3819 DEFVAR_LISP_NOPRO ("default-left-margin",
3820 &buffer_defaults.left_margin,
3821 "Default value of `left-margin' for buffers that do not override it.\n\
3822 This is the same as (default-value 'left-margin).");
3823
3824 DEFVAR_LISP_NOPRO ("default-tab-width",
3825 &buffer_defaults.tab_width,
3826 "Default value of `tab-width' for buffers that do not override it.\n\
3827 This is the same as (default-value 'tab-width).");
3828
3829 DEFVAR_LISP_NOPRO ("default-case-fold-search",
3830 &buffer_defaults.case_fold_search,
3831 "Default value of `case-fold-search' for buffers that don't override it.\n\
3832 This is the same as (default-value 'case-fold-search).");
3833
3834 #ifdef DOS_NT
3835 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
3836 &buffer_defaults.buffer_file_type,
3837 "Default file type for buffers that do not override it.\n\
3838 This is the same as (default-value 'buffer-file-type).\n\
3839 The file type is nil for text, t for binary.");
3840 #endif
3841
3842 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
3843 Qnil, 0);
3844
3845 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
3846 But make-docfile finds it!
3847 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
3848 Qnil,
3849 "Template for displaying mode line for current buffer.\n\
3850 Each buffer has its own value of this variable.\n\
3851 Value may be a string, a symbol or a list or cons cell.\n\
3852 For a symbol, its value is used (but it is ignored if t or nil).\n\
3853 A string appearing directly as the value of a symbol is processed verbatim\n\
3854 in that the %-constructs below are not recognized.\n\
3855 For a list whose car is a symbol, the symbol's value is taken,\n\
3856 and if that is non-nil, the cadr of the list is processed recursively.\n\
3857 Otherwise, the caddr of the list (if there is one) is processed.\n\
3858 For a list whose car is a string or list, each element is processed\n\
3859 recursively and the results are effectively concatenated.\n\
3860 For a list whose car is an integer, the cdr of the list is processed\n\
3861 and padded (if the number is positive) or truncated (if negative)\n\
3862 to the width specified by that number.\n\
3863 A string is printed verbatim in the mode line except for %-constructs:\n\
3864 (%-constructs are allowed when the string is the entire mode-line-format\n\
3865 or when it is found in a cons-cell or a list)\n\
3866 %b -- print buffer name. %f -- print visited file name.\n\
3867 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
3868 % means buffer is read-only and * means it is modified.\n\
3869 For a modified read-only buffer, %* gives % and %+ gives *.\n\
3870 %s -- print process status. %l -- print the current line number.\n\
3871 %c -- print the current column number (this makes editing slower).\n\
3872 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
3873 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
3874 or print Bottom or All.\n\
3875 %n -- print Narrow if appropriate.\n\
3876 %t -- print T if file is text, B if binary.\n\
3877 %[ -- print one [ for each recursive editing level. %] similar.\n\
3878 %% -- print %. %- -- print infinitely many dashes.\n\
3879 Decimal digits after the % specify field width to which to pad.");
3880 */
3881
3882 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
3883 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
3884 nil here means use current buffer's major mode.");
3885
3886 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
3887 make_number (Lisp_Symbol),
3888 "Symbol for current buffer's major mode.");
3889
3890 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
3891 make_number (Lisp_String),
3892 "Pretty name of current buffer's major mode (a string).");
3893
3894 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
3895 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
3896 Automatically becomes buffer-local when set in any fashion.");
3897
3898 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
3899 Qnil,
3900 "*Non-nil if searches should ignore case.\n\
3901 Automatically becomes buffer-local when set in any fashion.");
3902
3903 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
3904 make_number (Lisp_Int),
3905 "*Column beyond which automatic line-wrapping should happen.\n\
3906 Automatically becomes buffer-local when set in any fashion.");
3907
3908 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
3909 make_number (Lisp_Int),
3910 "*Column for the default indent-line-function to indent to.\n\
3911 Linefeed indents to this column in Fundamental mode.\n\
3912 Automatically becomes buffer-local when set in any fashion.");
3913
3914 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
3915 make_number (Lisp_Int),
3916 "*Distance between tab stops (for display of tab characters), in columns.\n\
3917 Automatically becomes buffer-local when set in any fashion.");
3918
3919 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
3920 "*Non-nil means display control chars with uparrow.\n\
3921 A value of nil means use backslash and octal digits.\n\
3922 Automatically becomes buffer-local when set in any fashion.\n\
3923 This variable does not apply to characters whose display is specified\n\
3924 in the current display table (if there is one).");
3925
3926 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
3927 &current_buffer->enable_multibyte_characters, Qnil,
3928 "Non-nil means the buffer contents are regarded as multi-byte form\n\
3929 of characters, not a binary code. This affects the display, file I/O,\n\
3930 and behaviors of various editing commands.");
3931
3932 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
3933 &current_buffer->buffer_file_coding_system, Qnil,
3934 "Coding system to be used for encoding the buffer contents on saving.\n\
3935 If it is nil, the buffer is saved without any code conversion unless\n\
3936 some coding system is specified in file-coding-system-alist\n\
3937 for the buffer file.\n\
3938 \n\
3939 This variable is never applied to a way of decoding\n\
3940 a file while reading it.");
3941
3942 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
3943 Qnil,
3944 "*Non-nil means lines in the buffer are displayed right to left.");
3945
3946 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
3947 "*Non-nil means do not display continuation lines;\n\
3948 give each line of text one screen line.\n\
3949 Automatically becomes buffer-local when set in any fashion.\n\
3950 \n\
3951 Note that this is overridden by the variable\n\
3952 `truncate-partial-width-windows' if that variable is non-nil\n\
3953 and this buffer is not full-frame width.");
3954
3955 #ifdef DOS_NT
3956 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
3957 Qnil,
3958 "Non-nil if the visited file is a binary file.\n\
3959 This variable is meaningful on MS-DOG and Windows NT.\n\
3960 On those systems, it is automatically local in every buffer.\n\
3961 On other systems, this variable is normally always nil.");
3962 #endif
3963
3964 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
3965 make_number (Lisp_String),
3966 "Name of default directory of current buffer. Should end with slash.\n\
3967 Each buffer has its own value of this variable.");
3968
3969 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
3970 Qnil,
3971 "Function called (if non-nil) to perform auto-fill.\n\
3972 It is called after self-inserting a space or newline.\n\
3973 Each buffer has its own value of this variable.\n\
3974 NOTE: This variable is not a hook;\n\
3975 its value may not be a list of functions.");
3976
3977 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
3978 make_number (Lisp_String),
3979 "Name of file visited in current buffer, or nil if not visiting a file.\n\
3980 Each buffer has its own value of this variable.");
3981
3982 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
3983 make_number (Lisp_String),
3984 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
3985 The truename of a file is calculated by `file-truename'\n\
3986 and then abbreviated with `abbreviate-file-name'.\n\
3987 Each buffer has its own value of this variable.");
3988
3989 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3990 &current_buffer->auto_save_file_name,
3991 make_number (Lisp_String),
3992 "Name of file for auto-saving current buffer,\n\
3993 or nil if buffer should not be auto-saved.\n\
3994 Each buffer has its own value of this variable.");
3995
3996 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
3997 "Non-nil if this buffer is read-only.\n\
3998 Each buffer has its own value of this variable.");
3999
4000 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
4001 "Non-nil if this buffer's file has been backed up.\n\
4002 Backing up is done before the first time the file is saved.\n\
4003 Each buffer has its own value of this variable.");
4004
4005 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
4006 make_number (Lisp_Int),
4007 "Length of current buffer when last read in, saved or auto-saved.\n\
4008 0 initially.\n\
4009 Each buffer has its own value of this variable.");
4010
4011 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
4012 Qnil,
4013 "Non-nil enables selective display:\n\
4014 Integer N as value means display only lines\n\
4015 that start with less than n columns of space.\n\
4016 A value of t means, after a ^M, all the rest of the line is invisible.\n\
4017 Then ^M's in the file are written into files as newlines.\n\n\
4018 Automatically becomes buffer-local when set in any fashion.");
4019
4020 #ifndef old
4021 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4022 &current_buffer->selective_display_ellipses,
4023 Qnil,
4024 "t means display ... on previous line when a line is invisible.\n\
4025 Automatically becomes buffer-local when set in any fashion.");
4026 #endif
4027
4028 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
4029 "Non-nil if self-insertion should replace existing text.\n\
4030 The value should be one of `overwrite-mode-textual',\n\
4031 `overwrite-mode-binary', or nil.\n\
4032 If it is `overwrite-mode-textual', self-insertion still\n\
4033 inserts at the end of a line, and inserts when point is before a tab,\n\
4034 until the tab is filled in.\n\
4035 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
4036 Automatically becomes buffer-local when set in any fashion.");
4037
4038 #if 0 /* The doc string is too long for some compilers,
4039 but make-docfile can find it in this comment. */
4040 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4041 Qnil,
4042 "Display table that controls display of the contents of current buffer.\n\
4043 Automatically becomes buffer-local when set in any fashion.\n\
4044 The display table is a char-table created with `make-display-table'.\n\
4045 The ordinary char-table elements control how to display each possible text\n\
4046 character. Each value should be a vector of characters or nil;\n\
4047 nil means display the character in the default fashion.\n\
4048 There are six extra slots to control the display of\n\
4049 the end of a truncated screen line (extra-slot 0, a single character);\n\
4050 the end of a continued line (extra-slot 1, a single character);\n\
4051 the escape character used to display character codes in octal\n\
4052 (extra-slot 2, a single character);\n\
4053 the character used as an arrow for control characters (extra-slot 3,\n\
4054 a single character);\n\
4055 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
4056 a vector of characters);\n\
4057 the character used to draw the border between side-by-side windows\n\
4058 (extra-slot 5, a single character).\n\
4059 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
4060 If this variable is nil, the value of `standard-display-table' is used.\n\
4061 Each window can have its own, overriding display table.");
4062 #endif
4063 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4064 Qnil, 0);
4065
4066 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4067 "Don't ask.");
4068 */
4069 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
4070 "If non-nil, a function to call before each text change (obsolete).\n\
4071 Two arguments are passed to the function: the positions of\n\
4072 the beginning and end of the range of old text to be changed.\n\
4073 \(For an insertion, the beginning and end are at the same place.)\n\
4074 No information is given about the length of the text after the change.\n\
4075 \n\
4076 Buffer changes made while executing the `before-change-function'\n\
4077 don't call any before-change or after-change functions.\n\
4078 That's because these variables are temporarily set to nil.\n\
4079 As a result, a hook function cannot straightforwardly alter the value of\n\
4080 these variables. See the Emacs Lisp manual for a way of\n\
4081 accomplishing an equivalent result by using other variables.\n\n\
4082 This variable is obsolete; use `before-change-functions' instead.");
4083 Vbefore_change_function = Qnil;
4084
4085 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
4086 "If non-nil, a Function to call after each text change (obsolete).\n\
4087 Three arguments are passed to the function: the positions of\n\
4088 the beginning and end of the range of changed text,\n\
4089 and the length of the pre-change text replaced by that range.\n\
4090 \(For an insertion, the pre-change length is zero;\n\
4091 for a deletion, that length is the number of characters deleted,\n\
4092 and the post-change beginning and end are at the same place.)\n\
4093 \n\
4094 Buffer changes made while executing the `after-change-function'\n\
4095 don't call any before-change or after-change functions.\n\
4096 That's because these variables are temporarily set to nil.\n\
4097 As a result, a hook function cannot straightforwardly alter the value of\n\
4098 these variables. See the Emacs Lisp manual for a way of\n\
4099 accomplishing an equivalent result by using other variables.\n\n\
4100 This variable is obsolete; use `after-change-functions' instead.");
4101 Vafter_change_function = Qnil;
4102
4103 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
4104 "List of functions to call before each text change.\n\
4105 Two arguments are passed to each function: the positions of\n\
4106 the beginning and end of the range of old text to be changed.\n\
4107 \(For an insertion, the beginning and end are at the same place.)\n\
4108 No information is given about the length of the text after the change.\n\
4109 \n\
4110 Buffer changes made while executing the `before-change-functions'\n\
4111 don't call any before-change or after-change functions.\n\
4112 That's because these variables are temporarily set to nil.\n\
4113 As a result, a hook function cannot straightforwardly alter the value of\n\
4114 these variables. See the Emacs Lisp manual for a way of\n\
4115 accomplishing an equivalent result by using other variables.");
4116 Vbefore_change_functions = Qnil;
4117
4118 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
4119 "List of function to call after each text change.\n\
4120 Three arguments are passed to each function: the positions of\n\
4121 the beginning and end of the range of changed text,\n\
4122 and the length of the pre-change text replaced by that range.\n\
4123 \(For an insertion, the pre-change length is zero;\n\
4124 for a deletion, that length is the number of characters deleted,\n\
4125 and the post-change beginning and end are at the same place.)\n\
4126 \n\
4127 Buffer changes made while executing the `after-change-functions'\n\
4128 don't call any before-change or after-change functions.\n\
4129 That's because these variables are temporarily set to nil.\n\
4130 As a result, a hook function cannot straightforwardly alter the value of\n\
4131 these variables. See the Emacs Lisp manual for a way of\n\
4132 accomplishing an equivalent result by using other variables.");
4133
4134 Vafter_change_functions = Qnil;
4135
4136 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
4137 "A list of functions to call before changing a buffer which is unmodified.\n\
4138 The functions are run using the `run-hooks' function.");
4139 Vfirst_change_hook = Qnil;
4140
4141 #if 0 /* The doc string is too long for some compilers,
4142 but make-docfile can find it in this comment. */
4143 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4144 "List of undo entries in current buffer.\n\
4145 Recent changes come first; older changes follow newer.\n\
4146 \n\
4147 An entry (BEG . END) represents an insertion which begins at\n\
4148 position BEG and ends at position END.\n\
4149 \n\
4150 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4151 from (abs POSITION). If POSITION is positive, point was at the front\n\
4152 of the text being deleted; if negative, point was at the end.\n\
4153 \n\
4154 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4155 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4156 of the visited file's modification time, as of that time. If the\n\
4157 modification time of the most recent save is different, this entry is\n\
4158 obsolete.\n\
4159 \n\
4160 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4161 was modified between BEG and END. PROPERTY is the property name,\n\
4162 and VALUE is the old value.\n\
4163 \n\
4164 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4165 was adjusted in position by the offset DISTANCE (an integer).\n\
4166 \n\
4167 An entry of the form POSITION indicates that point was at the buffer\n\
4168 location given by the integer. Undoing an entry of this form places\n\
4169 point at POSITION.\n\
4170 \n\
4171 nil marks undo boundaries. The undo command treats the changes\n\
4172 between two undo boundaries as a single step to be undone.\n\
4173 \n\
4174 If the value of the variable is t, undo information is not recorded.");
4175 #endif
4176 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4177 0);
4178
4179 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
4180 "Non-nil means the mark and region are currently active in this buffer.\n\
4181 Automatically local in all buffers.");
4182
4183 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
4184 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4185 This variable is buffer-local, in all buffers.\n\
4186 \n\
4187 Normally, the line-motion functions work by scanning the buffer for\n\
4188 newlines. Columnar operations (like move-to-column and\n\
4189 compute-motion) also work by scanning the buffer, summing character\n\
4190 widths as they go. This works well for ordinary text, but if the\n\
4191 buffer's lines are very long (say, more than 500 characters), these\n\
4192 motion functions will take longer to execute. Emacs may also take\n\
4193 longer to update the display.\n\
4194 \n\
4195 If cache-long-line-scans is non-nil, these motion functions cache the\n\
4196 results of their scans, and consult the cache to avoid rescanning\n\
4197 regions of the buffer until the text is modified. The caches are most\n\
4198 beneficial when they prevent the most searching---that is, when the\n\
4199 buffer contains long lines and large regions of characters with the\n\
4200 same, fixed screen width.\n\
4201 \n\
4202 When cache-long-line-scans is non-nil, processing short lines will\n\
4203 become slightly slower (because of the overhead of consulting the\n\
4204 cache), and the caches will use memory roughly proportional to the\n\
4205 number of newlines and characters whose screen width varies.\n\
4206 \n\
4207 The caches require no explicit maintenance; their accuracy is\n\
4208 maintained internally by the Emacs primitives. Enabling or disabling\n\
4209 the cache should not affect the behavior of any of the motion\n\
4210 functions; it should only affect their performance.");
4211
4212 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
4213 "Value of point before the last series of scroll operations, or nil.");
4214
4215 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4216 "List of formats to use when saving this buffer.\n\
4217 Formats are defined by `format-alist'. This variable is\n\
4218 set when a file is visited. Automatically local in all buffers.");
4219
4220 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4221 &current_buffer->invisibility_spec, Qnil,
4222 "Invisibility spec of this buffer.\n\
4223 The default is t, which means that text is invisible\n\
4224 if it has a non-nil `invisible' property.\n\
4225 If the value is a list, a text character is invisible if its `invisible'\n\
4226 property is an element in that list.\n\
4227 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4228 then characters with property value PROP are invisible,\n\
4229 and they have an ellipsis as well if ELLIPSIS is non-nil.");
4230
4231 DEFVAR_PER_BUFFER ("buffer-display-count",
4232 &current_buffer->display_count, Qnil,
4233 "A number incremented each time the buffer is displayed in a window.");
4234
4235 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
4236 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4237 Non-nil also enables highlighting of the region whenever the mark is active.\n\
4238 The variable `highlight-nonselected-windows' controls whether to highlight\n\
4239 all windows or just the selected window.");
4240 Vtransient_mark_mode = Qnil;
4241
4242 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
4243 "*Non-nil means disregard read-only status of buffers or characters.\n\
4244 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4245 text properties. If the value is a list, disregard `buffer-read-only'\n\
4246 and disregard a `read-only' text property if the property value\n\
4247 is a member of the list.");
4248 Vinhibit_read_only = Qnil;
4249
4250 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4251 "List of functions called with no args to query before killing a buffer.");
4252 Vkill_buffer_query_functions = Qnil;
4253
4254 defsubr (&Sbuffer_live_p);
4255 defsubr (&Sbuffer_list);
4256 defsubr (&Sget_buffer);
4257 defsubr (&Sget_file_buffer);
4258 defsubr (&Sget_buffer_create);
4259 defsubr (&Smake_indirect_buffer);
4260 defsubr (&Sgenerate_new_buffer_name);
4261 defsubr (&Sbuffer_name);
4262 /*defsubr (&Sbuffer_number);*/
4263 defsubr (&Sbuffer_file_name);
4264 defsubr (&Sbuffer_base_buffer);
4265 defsubr (&Sbuffer_local_variables);
4266 defsubr (&Sbuffer_modified_p);
4267 defsubr (&Sset_buffer_modified_p);
4268 defsubr (&Sbuffer_modified_tick);
4269 defsubr (&Srename_buffer);
4270 defsubr (&Sother_buffer);
4271 defsubr (&Sbuffer_disable_undo);
4272 defsubr (&Sbuffer_enable_undo);
4273 defsubr (&Skill_buffer);
4274 defsubr (&Serase_buffer);
4275 defsubr (&Sset_buffer_major_mode);
4276 defsubr (&Sswitch_to_buffer);
4277 defsubr (&Spop_to_buffer);
4278 defsubr (&Scurrent_buffer);
4279 defsubr (&Sset_buffer);
4280 defsubr (&Sbarf_if_buffer_read_only);
4281 defsubr (&Sbury_buffer);
4282 defsubr (&Skill_all_local_variables);
4283
4284 defsubr (&Soverlayp);
4285 defsubr (&Smake_overlay);
4286 defsubr (&Sdelete_overlay);
4287 defsubr (&Smove_overlay);
4288 defsubr (&Soverlay_start);
4289 defsubr (&Soverlay_end);
4290 defsubr (&Soverlay_buffer);
4291 defsubr (&Soverlay_properties);
4292 defsubr (&Soverlays_at);
4293 defsubr (&Soverlays_in);
4294 defsubr (&Snext_overlay_change);
4295 defsubr (&Sprevious_overlay_change);
4296 defsubr (&Soverlay_recenter);
4297 defsubr (&Soverlay_lists);
4298 defsubr (&Soverlay_get);
4299 defsubr (&Soverlay_put);
4300 }
4301
4302 keys_of_buffer ()
4303 {
4304 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4305 initial_define_key (control_x_map, 'k', "kill-buffer");
4306
4307 /* This must not be in syms_of_buffer, because Qdisabled is not
4308 initialized when that function gets called. */
4309 Fput (intern ("erase-buffer"), Qdisabled, Qt);
4310 }