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