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