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