Add site-lisp to EMACSLOADPATH.
[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
2102 int
2103 overlay_strings (pos, w, pstr)
2104 int pos;
2105 struct window *w;
2106 unsigned char **pstr;
2107 {
2108 Lisp_Object ov, overlay, window, str;
2109 int startpos, endpos;
2110
2111 overlay_heads.used = overlay_heads.bytes = 0;
2112 overlay_tails.used = overlay_tails.bytes = 0;
2113 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCONS (ov)->cdr)
2114 {
2115 overlay = XCONS (ov)->car;
2116 if (!OVERLAYP (overlay))
2117 abort ();
2118
2119 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2120 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2121 if (endpos < pos)
2122 break;
2123 if (endpos != pos && startpos != pos)
2124 continue;
2125 window = Foverlay_get (overlay, Qwindow);
2126 if (WINDOWP (window) && XWINDOW (window) != w)
2127 continue;
2128 if (startpos == pos
2129 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2130 record_overlay_string (&overlay_heads, str,
2131 (startpos == endpos
2132 ? Foverlay_get (overlay, Qafter_string)
2133 : Qnil),
2134 Foverlay_get (overlay, Qpriority),
2135 endpos - startpos);
2136 else if (endpos == pos
2137 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2138 record_overlay_string (&overlay_tails, str, Qnil,
2139 Foverlay_get (overlay, Qpriority),
2140 endpos - startpos);
2141 }
2142 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCONS (ov)->cdr)
2143 {
2144 overlay = XCONS (ov)->car;
2145 if (!OVERLAYP (overlay))
2146 abort ();
2147
2148 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2149 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2150 if (startpos > pos)
2151 break;
2152 if (endpos != pos && startpos != pos)
2153 continue;
2154 window = Foverlay_get (overlay, Qwindow);
2155 if (WINDOWP (window) && XWINDOW (window) != w)
2156 continue;
2157 if (startpos == pos
2158 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2159 record_overlay_string (&overlay_heads, str,
2160 (startpos == endpos
2161 ? Foverlay_get (overlay, Qafter_string)
2162 : Qnil),
2163 Foverlay_get (overlay, Qpriority),
2164 endpos - startpos);
2165 else if (endpos == pos
2166 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2167 record_overlay_string (&overlay_tails, str, Qnil,
2168 Foverlay_get (overlay, Qpriority),
2169 endpos - startpos);
2170 }
2171 if (overlay_tails.used > 1)
2172 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2173 cmp_for_strings);
2174 if (overlay_heads.used > 1)
2175 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2176 cmp_for_strings);
2177 if (overlay_heads.bytes || overlay_tails.bytes)
2178 {
2179 Lisp_Object tem;
2180 int i;
2181 char *p;
2182 int total = overlay_heads.bytes + overlay_tails.bytes;
2183
2184 if (total > overlay_str_len)
2185 overlay_str_buf = (char *)xrealloc (overlay_str_buf,
2186 overlay_str_len = total);
2187 p = overlay_str_buf;
2188 for (i = overlay_tails.used; --i >= 0;)
2189 {
2190 tem = overlay_tails.buf[i].string;
2191 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2192 p += XSTRING (tem)->size;
2193 }
2194 for (i = 0; i < overlay_heads.used; ++i)
2195 {
2196 tem = overlay_heads.buf[i].string;
2197 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2198 p += XSTRING (tem)->size;
2199 tem = overlay_heads.buf[i].string2;
2200 if (STRINGP (tem))
2201 {
2202 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2203 p += XSTRING (tem)->size;
2204 }
2205 }
2206 if (p != overlay_str_buf + total)
2207 abort ();
2208 if (pstr)
2209 *pstr = overlay_str_buf;
2210 return total;
2211 }
2212 return 0;
2213 }
2214 \f
2215 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2216
2217 void
2218 recenter_overlay_lists (buf, pos)
2219 struct buffer *buf;
2220 int pos;
2221 {
2222 Lisp_Object overlay, tail, next, prev, beg, end;
2223
2224 /* See if anything in overlays_before should move to overlays_after. */
2225
2226 /* We don't strictly need prev in this loop; it should always be nil.
2227 But we use it for symmetry and in case that should cease to be true
2228 with some future change. */
2229 prev = Qnil;
2230 for (tail = buf->overlays_before;
2231 CONSP (tail);
2232 prev = tail, tail = next)
2233 {
2234 next = XCONS (tail)->cdr;
2235 overlay = XCONS (tail)->car;
2236
2237 /* If the overlay is not valid, get rid of it. */
2238 if (!OVERLAY_VALID (overlay))
2239 #if 1
2240 abort ();
2241 #else
2242 {
2243 /* Splice the cons cell TAIL out of overlays_before. */
2244 if (!NILP (prev))
2245 XCONS (prev)->cdr = next;
2246 else
2247 buf->overlays_before = next;
2248 tail = prev;
2249 continue;
2250 }
2251 #endif
2252
2253 beg = OVERLAY_START (overlay);
2254 end = OVERLAY_END (overlay);
2255
2256 if (OVERLAY_POSITION (end) > pos)
2257 {
2258 /* OVERLAY needs to be moved. */
2259 int where = OVERLAY_POSITION (beg);
2260 Lisp_Object other, other_prev;
2261
2262 /* Splice the cons cell TAIL out of overlays_before. */
2263 if (!NILP (prev))
2264 XCONS (prev)->cdr = next;
2265 else
2266 buf->overlays_before = next;
2267
2268 /* Search thru overlays_after for where to put it. */
2269 other_prev = Qnil;
2270 for (other = buf->overlays_after;
2271 CONSP (other);
2272 other_prev = other, other = XCONS (other)->cdr)
2273 {
2274 Lisp_Object otherbeg, otheroverlay, follower;
2275 int win;
2276
2277 otheroverlay = XCONS (other)->car;
2278 if (! OVERLAY_VALID (otheroverlay))
2279 abort ();
2280
2281 otherbeg = OVERLAY_START (otheroverlay);
2282 if (OVERLAY_POSITION (otherbeg) >= where)
2283 break;
2284 }
2285
2286 /* Add TAIL to overlays_after before OTHER. */
2287 XCONS (tail)->cdr = other;
2288 if (!NILP (other_prev))
2289 XCONS (other_prev)->cdr = tail;
2290 else
2291 buf->overlays_after = tail;
2292 tail = prev;
2293 }
2294 else
2295 /* We've reached the things that should stay in overlays_before.
2296 All the rest of overlays_before must end even earlier,
2297 so stop now. */
2298 break;
2299 }
2300
2301 /* See if anything in overlays_after should be in overlays_before. */
2302 prev = Qnil;
2303 for (tail = buf->overlays_after;
2304 CONSP (tail);
2305 prev = tail, tail = next)
2306 {
2307 next = XCONS (tail)->cdr;
2308 overlay = XCONS (tail)->car;
2309
2310 /* If the overlay is not valid, get rid of it. */
2311 if (!OVERLAY_VALID (overlay))
2312 #if 1
2313 abort ();
2314 #else
2315 {
2316 /* Splice the cons cell TAIL out of overlays_after. */
2317 if (!NILP (prev))
2318 XCONS (prev)->cdr = next;
2319 else
2320 buf->overlays_after = next;
2321 tail = prev;
2322 continue;
2323 }
2324 #endif
2325
2326 beg = OVERLAY_START (overlay);
2327 end = OVERLAY_END (overlay);
2328
2329 /* Stop looking, when we know that nothing further
2330 can possibly end before POS. */
2331 if (OVERLAY_POSITION (beg) > pos)
2332 break;
2333
2334 if (OVERLAY_POSITION (end) <= pos)
2335 {
2336 /* OVERLAY needs to be moved. */
2337 int where = OVERLAY_POSITION (end);
2338 Lisp_Object other, other_prev;
2339
2340 /* Splice the cons cell TAIL out of overlays_after. */
2341 if (!NILP (prev))
2342 XCONS (prev)->cdr = next;
2343 else
2344 buf->overlays_after = next;
2345
2346 /* Search thru overlays_before for where to put it. */
2347 other_prev = Qnil;
2348 for (other = buf->overlays_before;
2349 CONSP (other);
2350 other_prev = other, other = XCONS (other)->cdr)
2351 {
2352 Lisp_Object otherend, otheroverlay;
2353 int win;
2354
2355 otheroverlay = XCONS (other)->car;
2356 if (! OVERLAY_VALID (otheroverlay))
2357 abort ();
2358
2359 otherend = OVERLAY_END (otheroverlay);
2360 if (OVERLAY_POSITION (otherend) <= where)
2361 break;
2362 }
2363
2364 /* Add TAIL to overlays_before before OTHER. */
2365 XCONS (tail)->cdr = other;
2366 if (!NILP (other_prev))
2367 XCONS (other_prev)->cdr = tail;
2368 else
2369 buf->overlays_before = tail;
2370 tail = prev;
2371 }
2372 }
2373
2374 XSETFASTINT (buf->overlay_center, pos);
2375 }
2376
2377 void
2378 adjust_overlays_for_insert (pos, length)
2379 int pos;
2380 int length;
2381 {
2382 /* After an insertion, the lists are still sorted properly,
2383 but we may need to update the value of the overlay center. */
2384 if (XFASTINT (current_buffer->overlay_center) >= pos)
2385 XSETFASTINT (current_buffer->overlay_center,
2386 XFASTINT (current_buffer->overlay_center) + length);
2387 }
2388
2389 void
2390 adjust_overlays_for_delete (pos, length)
2391 int pos;
2392 int length;
2393 {
2394 if (XFASTINT (current_buffer->overlay_center) < pos)
2395 /* The deletion was to our right. No change needed; the before- and
2396 after-lists are still consistent. */
2397 ;
2398 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
2399 /* The deletion was to our left. We need to adjust the center value
2400 to account for the change in position, but the lists are consistent
2401 given the new value. */
2402 XSETFASTINT (current_buffer->overlay_center,
2403 XFASTINT (current_buffer->overlay_center) - length);
2404 else
2405 /* We're right in the middle. There might be things on the after-list
2406 that now belong on the before-list. Recentering will move them,
2407 and also update the center point. */
2408 recenter_overlay_lists (current_buffer, pos);
2409 }
2410
2411 /* Fix up overlays that were garbled as a result of permuting markers
2412 in the range START through END. Any overlay with at least one
2413 endpoint in this range will need to be unlinked from the overlay
2414 list and reinserted in its proper place.
2415 Such an overlay might even have negative size at this point.
2416 If so, we'll reverse the endpoints. Can you think of anything
2417 better to do in this situation? */
2418 void
2419 fix_overlays_in_range (start, end)
2420 register int start, end;
2421 {
2422 Lisp_Object tem, overlay;
2423 Lisp_Object before_list, after_list;
2424 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
2425 int startpos, endpos;
2426
2427 /* This algorithm shifts links around instead of consing and GCing.
2428 The loop invariant is that before_list (resp. after_list) is a
2429 well-formed list except that its last element, the one that
2430 *pbefore (resp. *pafter) points to, is still uninitialized.
2431 So it's not a bug that before_list isn't initialized, although
2432 it may look strange. */
2433 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
2434 {
2435 overlay = XCONS (*ptail)->car;
2436 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2437 if (endpos < start)
2438 break;
2439 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2440 if (endpos < end
2441 || (startpos >= start && startpos < end))
2442 {
2443 /* If the overlay is backwards, fix that now. */
2444 if (startpos > endpos)
2445 {
2446 int tem;
2447 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2448 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2449 tem = startpos; startpos = endpos; endpos = tem;
2450 }
2451 /* Add it to the end of the wrong list. Later on,
2452 recenter_overlay_lists will move it to the right place. */
2453 if (endpos < XINT (current_buffer->overlay_center))
2454 {
2455 *pafter = *ptail;
2456 pafter = &XCONS (*ptail)->cdr;
2457 }
2458 else
2459 {
2460 *pbefore = *ptail;
2461 pbefore = &XCONS (*ptail)->cdr;
2462 }
2463 *ptail = XCONS (*ptail)->cdr;
2464 }
2465 else
2466 ptail = &XCONS (*ptail)->cdr;
2467 }
2468 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
2469 {
2470 overlay = XCONS (*ptail)->car;
2471 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2472 if (startpos >= end)
2473 break;
2474 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2475 if (startpos >= start
2476 || (endpos >= start && endpos < end))
2477 {
2478 if (startpos > endpos)
2479 {
2480 int tem;
2481 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2482 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2483 tem = startpos; startpos = endpos; endpos = tem;
2484 }
2485 if (endpos < XINT (current_buffer->overlay_center))
2486 {
2487 *pafter = *ptail;
2488 pafter = &XCONS (*ptail)->cdr;
2489 }
2490 else
2491 {
2492 *pbefore = *ptail;
2493 pbefore = &XCONS (*ptail)->cdr;
2494 }
2495 *ptail = XCONS (*ptail)->cdr;
2496 }
2497 else
2498 ptail = &XCONS (*ptail)->cdr;
2499 }
2500
2501 /* Splice the constructed (wrong) lists into the buffer's lists,
2502 and let the recenter function make it sane again. */
2503 *pbefore = current_buffer->overlays_before;
2504 current_buffer->overlays_before = before_list;
2505 recenter_overlay_lists (current_buffer,
2506 XINT (current_buffer->overlay_center));
2507
2508 *pafter = current_buffer->overlays_after;
2509 current_buffer->overlays_after = after_list;
2510 recenter_overlay_lists (current_buffer,
2511 XINT (current_buffer->overlay_center));
2512 }
2513 \f
2514 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
2515 "Return t if OBJECT is an overlay.")
2516 (object)
2517 Lisp_Object object;
2518 {
2519 return (OVERLAYP (object) ? Qt : Qnil);
2520 }
2521
2522 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
2523 "Create a new overlay with range BEG to END in BUFFER.\n\
2524 If omitted, BUFFER defaults to the current buffer.\n\
2525 BEG and END may be integers or markers.\n\
2526 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
2527 front delimiter advance when text is inserted there.\n\
2528 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
2529 rear delimiter advance when text is inserted there.")
2530 (beg, end, buffer, front_advance, rear_advance)
2531 Lisp_Object beg, end, buffer;
2532 Lisp_Object front_advance, rear_advance;
2533 {
2534 Lisp_Object overlay;
2535 struct buffer *b;
2536
2537 if (NILP (buffer))
2538 XSETBUFFER (buffer, current_buffer);
2539 else
2540 CHECK_BUFFER (buffer, 2);
2541 if (MARKERP (beg)
2542 && ! EQ (Fmarker_buffer (beg), buffer))
2543 error ("Marker points into wrong buffer");
2544 if (MARKERP (end)
2545 && ! EQ (Fmarker_buffer (end), buffer))
2546 error ("Marker points into wrong buffer");
2547
2548 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2549 CHECK_NUMBER_COERCE_MARKER (end, 1);
2550
2551 if (XINT (beg) > XINT (end))
2552 {
2553 Lisp_Object temp;
2554 temp = beg; beg = end; end = temp;
2555 }
2556
2557 b = XBUFFER (buffer);
2558
2559 beg = Fset_marker (Fmake_marker (), beg, buffer);
2560 end = Fset_marker (Fmake_marker (), end, buffer);
2561
2562 if (!NILP (front_advance))
2563 XMARKER (beg)->insertion_type = 1;
2564 if (!NILP (rear_advance))
2565 XMARKER (end)->insertion_type = 1;
2566
2567 overlay = allocate_misc ();
2568 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
2569 XOVERLAY (overlay)->start = beg;
2570 XOVERLAY (overlay)->end = end;
2571 XOVERLAY (overlay)->plist = Qnil;
2572
2573 /* Put the new overlay on the wrong list. */
2574 end = OVERLAY_END (overlay);
2575 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2576 b->overlays_after = Fcons (overlay, b->overlays_after);
2577 else
2578 b->overlays_before = Fcons (overlay, b->overlays_before);
2579
2580 /* This puts it in the right list, and in the right order. */
2581 recenter_overlay_lists (b, XINT (b->overlay_center));
2582
2583 /* We don't need to redisplay the region covered by the overlay, because
2584 the overlay has no properties at the moment. */
2585
2586 return overlay;
2587 }
2588 \f
2589 /* Mark a section of BUF as needing redisplay because of overlays changes. */
2590
2591 static void
2592 modify_overlay (buf, start, end)
2593 struct buffer *buf;
2594 int start, end;
2595 {
2596 if (start == end)
2597 return;
2598
2599 if (start > end)
2600 {
2601 int temp = start;
2602 start = end; end = temp;
2603 }
2604
2605 /* If this is a buffer not in the selected window,
2606 we must do other windows. */
2607 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2608 windows_or_buffers_changed = 1;
2609 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2610 else if (buf != current_buffer)
2611 windows_or_buffers_changed = 1;
2612 /* If multiple windows show this buffer, we must do other windows. */
2613 else if (buffer_shared > 1)
2614 windows_or_buffers_changed = 1;
2615 else
2616 {
2617 if (unchanged_modified == MODIFF
2618 && overlay_unchanged_modified == OVERLAY_MODIFF)
2619 {
2620 beg_unchanged = start - BEG;
2621 end_unchanged = Z - end;
2622 }
2623 else
2624 {
2625 if (Z - end < end_unchanged)
2626 end_unchanged = Z - end;
2627 if (start - BEG < beg_unchanged)
2628 beg_unchanged = start - BEG;
2629 }
2630 }
2631
2632 ++OVERLAY_MODIFF;
2633 }
2634
2635 \f\f
2636 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
2637 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
2638 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
2639 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
2640 buffer.")
2641 (overlay, beg, end, buffer)
2642 Lisp_Object overlay, beg, end, buffer;
2643 {
2644 struct buffer *b, *ob;
2645 Lisp_Object obuffer;
2646 int count = specpdl_ptr - specpdl;
2647
2648 CHECK_OVERLAY (overlay, 0);
2649 if (NILP (buffer))
2650 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2651 if (NILP (buffer))
2652 XSETBUFFER (buffer, current_buffer);
2653 CHECK_BUFFER (buffer, 3);
2654
2655 if (MARKERP (beg)
2656 && ! EQ (Fmarker_buffer (beg), buffer))
2657 error ("Marker points into wrong buffer");
2658 if (MARKERP (end)
2659 && ! EQ (Fmarker_buffer (end), buffer))
2660 error ("Marker points into wrong buffer");
2661
2662 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2663 CHECK_NUMBER_COERCE_MARKER (end, 1);
2664
2665 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
2666 return Fdelete_overlay (overlay);
2667
2668 if (XINT (beg) > XINT (end))
2669 {
2670 Lisp_Object temp;
2671 temp = beg; beg = end; end = temp;
2672 }
2673
2674 specbind (Qinhibit_quit, Qt);
2675
2676 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
2677 b = XBUFFER (buffer);
2678 ob = XBUFFER (obuffer);
2679
2680 /* If the overlay has changed buffers, do a thorough redisplay. */
2681 if (!EQ (buffer, obuffer))
2682 {
2683 /* Redisplay where the overlay was. */
2684 if (!NILP (obuffer))
2685 {
2686 Lisp_Object o_beg;
2687 Lisp_Object o_end;
2688
2689 o_beg = OVERLAY_START (overlay);
2690 o_end = OVERLAY_END (overlay);
2691 o_beg = OVERLAY_POSITION (o_beg);
2692 o_end = OVERLAY_POSITION (o_end);
2693
2694 modify_overlay (ob, XINT (o_beg), XINT (o_end));
2695 }
2696
2697 /* Redisplay where the overlay is going to be. */
2698 modify_overlay (b, XINT (beg), XINT (end));
2699 }
2700 else
2701 /* Redisplay the area the overlay has just left, or just enclosed. */
2702 {
2703 Lisp_Object o_beg;
2704 Lisp_Object o_end;
2705 int change_beg, change_end;
2706
2707 o_beg = OVERLAY_START (overlay);
2708 o_end = OVERLAY_END (overlay);
2709 o_beg = OVERLAY_POSITION (o_beg);
2710 o_end = OVERLAY_POSITION (o_end);
2711
2712 if (XINT (o_beg) == XINT (beg))
2713 modify_overlay (b, XINT (o_end), XINT (end));
2714 else if (XINT (o_end) == XINT (end))
2715 modify_overlay (b, XINT (o_beg), XINT (beg));
2716 else
2717 {
2718 if (XINT (beg) < XINT (o_beg)) o_beg = beg;
2719 if (XINT (end) > XINT (o_end)) o_end = end;
2720 modify_overlay (b, XINT (o_beg), XINT (o_end));
2721 }
2722 }
2723
2724 if (!NILP (obuffer))
2725 {
2726 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
2727 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
2728 }
2729
2730 Fset_marker (OVERLAY_START (overlay), beg, buffer);
2731 Fset_marker (OVERLAY_END (overlay), end, buffer);
2732
2733 /* Put the overlay on the wrong list. */
2734 end = OVERLAY_END (overlay);
2735 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2736 b->overlays_after = Fcons (overlay, b->overlays_after);
2737 else
2738 b->overlays_before = Fcons (overlay, b->overlays_before);
2739
2740 /* This puts it in the right list, and in the right order. */
2741 recenter_overlay_lists (b, XINT (b->overlay_center));
2742
2743 return unbind_to (count, overlay);
2744 }
2745
2746 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
2747 "Delete the overlay OVERLAY from its buffer.")
2748 (overlay)
2749 Lisp_Object overlay;
2750 {
2751 Lisp_Object buffer;
2752 struct buffer *b;
2753 int count = specpdl_ptr - specpdl;
2754
2755 CHECK_OVERLAY (overlay, 0);
2756
2757 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2758 if (NILP (buffer))
2759 return Qnil;
2760
2761 b = XBUFFER (buffer);
2762
2763 specbind (Qinhibit_quit, Qt);
2764
2765 b->overlays_before = Fdelq (overlay, b->overlays_before);
2766 b->overlays_after = Fdelq (overlay, b->overlays_after);
2767
2768 modify_overlay (b,
2769 marker_position (OVERLAY_START (overlay)),
2770 marker_position (OVERLAY_END (overlay)));
2771
2772 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
2773 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
2774
2775 return unbind_to (count, Qnil);
2776 }
2777 \f
2778 /* Overlay dissection functions. */
2779
2780 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
2781 "Return the position at which OVERLAY starts.")
2782 (overlay)
2783 Lisp_Object overlay;
2784 {
2785 CHECK_OVERLAY (overlay, 0);
2786
2787 return (Fmarker_position (OVERLAY_START (overlay)));
2788 }
2789
2790 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
2791 "Return the position at which OVERLAY ends.")
2792 (overlay)
2793 Lisp_Object overlay;
2794 {
2795 CHECK_OVERLAY (overlay, 0);
2796
2797 return (Fmarker_position (OVERLAY_END (overlay)));
2798 }
2799
2800 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
2801 "Return the buffer OVERLAY belongs to.")
2802 (overlay)
2803 Lisp_Object overlay;
2804 {
2805 CHECK_OVERLAY (overlay, 0);
2806
2807 return Fmarker_buffer (OVERLAY_START (overlay));
2808 }
2809
2810 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
2811 "Return a list of the properties on OVERLAY.\n\
2812 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
2813 OVERLAY.")
2814 (overlay)
2815 Lisp_Object overlay;
2816 {
2817 CHECK_OVERLAY (overlay, 0);
2818
2819 return Fcopy_sequence (XOVERLAY (overlay)->plist);
2820 }
2821
2822 \f
2823 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
2824 "Return a list of the overlays that contain position POS.")
2825 (pos)
2826 Lisp_Object pos;
2827 {
2828 int noverlays;
2829 Lisp_Object *overlay_vec;
2830 int len;
2831 Lisp_Object result;
2832
2833 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2834
2835 len = 10;
2836 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2837
2838 /* Put all the overlays we want in a vector in overlay_vec.
2839 Store the length in len. */
2840 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
2841 (int *) 0, (int *) 0);
2842
2843 /* Make a list of them all. */
2844 result = Flist (noverlays, overlay_vec);
2845
2846 xfree (overlay_vec);
2847 return result;
2848 }
2849
2850 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
2851 "Return a list of the overlays that overlap the region BEG ... END.\n\
2852 Overlap means that at least one character is contained within the overlay\n\
2853 and also contained within the specified region.\n\
2854 Empty overlays are included in the result if they are located at BEG\n\
2855 or between BEG and END.")
2856 (beg, end)
2857 Lisp_Object beg, end;
2858 {
2859 int noverlays;
2860 Lisp_Object *overlay_vec;
2861 int len;
2862 Lisp_Object result;
2863
2864 CHECK_NUMBER_COERCE_MARKER (beg, 0);
2865 CHECK_NUMBER_COERCE_MARKER (end, 0);
2866
2867 len = 10;
2868 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2869
2870 /* Put all the overlays we want in a vector in overlay_vec.
2871 Store the length in len. */
2872 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
2873 (int *) 0, (int *) 0);
2874
2875 /* Make a list of them all. */
2876 result = Flist (noverlays, overlay_vec);
2877
2878 xfree (overlay_vec);
2879 return result;
2880 }
2881
2882 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
2883 1, 1, 0,
2884 "Return the next position after POS where an overlay starts or ends.\n\
2885 If there are no more overlay boundaries after POS, return (point-max).")
2886 (pos)
2887 Lisp_Object pos;
2888 {
2889 int noverlays;
2890 int endpos;
2891 Lisp_Object *overlay_vec;
2892 int len;
2893 int i;
2894
2895 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2896
2897 len = 10;
2898 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2899
2900 /* Put all the overlays we want in a vector in overlay_vec.
2901 Store the length in len.
2902 endpos gets the position where the next overlay starts. */
2903 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
2904 &endpos, (int *) 0);
2905
2906 /* If any of these overlays ends before endpos,
2907 use its ending point instead. */
2908 for (i = 0; i < noverlays; i++)
2909 {
2910 Lisp_Object oend;
2911 int oendpos;
2912
2913 oend = OVERLAY_END (overlay_vec[i]);
2914 oendpos = OVERLAY_POSITION (oend);
2915 if (oendpos < endpos)
2916 endpos = oendpos;
2917 }
2918
2919 xfree (overlay_vec);
2920 return make_number (endpos);
2921 }
2922
2923 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
2924 Sprevious_overlay_change, 1, 1, 0,
2925 "Return the previous position before POS where an overlay starts or ends.\n\
2926 If there are no more overlay boundaries before POS, return (point-min).")
2927 (pos)
2928 Lisp_Object pos;
2929 {
2930 int noverlays;
2931 int prevpos;
2932 Lisp_Object *overlay_vec;
2933 int len;
2934 int i;
2935 Lisp_Object tail;
2936
2937 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2938
2939 len = 10;
2940 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2941
2942 /* At beginning of buffer, we know the answer;
2943 avoid bug subtracting 1 below. */
2944 if (XINT (pos) == BEGV)
2945 return pos;
2946
2947 /* Put all the overlays we want in a vector in overlay_vec.
2948 Store the length in len.
2949 prevpos gets the position of an overlay end. */
2950 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
2951 (int *) 0, &prevpos);
2952
2953 /* If any of these overlays starts after prevpos,
2954 maybe use its starting point instead. */
2955 for (i = 0; i < noverlays; i++)
2956 {
2957 Lisp_Object ostart;
2958 int ostartpos;
2959
2960 ostart = OVERLAY_START (overlay_vec[i]);
2961 ostartpos = OVERLAY_POSITION (ostart);
2962 if (ostartpos > prevpos && ostartpos < XINT (pos))
2963 prevpos = ostartpos;
2964 }
2965
2966 /* If any overlay ends at pos, consider its starting point too. */
2967 for (tail = current_buffer->overlays_before;
2968 GC_CONSP (tail);
2969 tail = XCONS (tail)->cdr)
2970 {
2971 Lisp_Object overlay, ostart;
2972 int ostartpos;
2973
2974 overlay = XCONS (tail)->car;
2975
2976 ostart = OVERLAY_START (overlay);
2977 ostartpos = OVERLAY_POSITION (ostart);
2978 if (ostartpos > prevpos && ostartpos < XINT (pos))
2979 prevpos = ostartpos;
2980 }
2981
2982 xfree (overlay_vec);
2983 return make_number (prevpos);
2984 }
2985 \f
2986 /* These functions are for debugging overlays. */
2987
2988 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
2989 "Return a pair of lists giving all the overlays of the current buffer.\n\
2990 The car has all the overlays before the overlay center;\n\
2991 the cdr has all the overlays after the overlay center.\n\
2992 Recentering overlays moves overlays between these lists.\n\
2993 The lists you get are copies, so that changing them has no effect.\n\
2994 However, the overlays you get are the real objects that the buffer uses.")
2995 ()
2996 {
2997 Lisp_Object before, after;
2998 before = current_buffer->overlays_before;
2999 if (CONSP (before))
3000 before = Fcopy_sequence (before);
3001 after = current_buffer->overlays_after;
3002 if (CONSP (after))
3003 after = Fcopy_sequence (after);
3004
3005 return Fcons (before, after);
3006 }
3007
3008 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3009 "Recenter the overlays of the current buffer around position POS.")
3010 (pos)
3011 Lisp_Object pos;
3012 {
3013 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3014
3015 recenter_overlay_lists (current_buffer, XINT (pos));
3016 return Qnil;
3017 }
3018 \f
3019 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3020 "Get the property of overlay OVERLAY with property name PROP.")
3021 (overlay, prop)
3022 Lisp_Object overlay, prop;
3023 {
3024 Lisp_Object plist, fallback;
3025
3026 CHECK_OVERLAY (overlay, 0);
3027
3028 fallback = Qnil;
3029
3030 for (plist = XOVERLAY (overlay)->plist;
3031 CONSP (plist) && CONSP (XCONS (plist)->cdr);
3032 plist = XCONS (XCONS (plist)->cdr)->cdr)
3033 {
3034 if (EQ (XCONS (plist)->car, prop))
3035 return XCONS (XCONS (plist)->cdr)->car;
3036 else if (EQ (XCONS (plist)->car, Qcategory))
3037 {
3038 Lisp_Object tem;
3039 tem = Fcar (Fcdr (plist));
3040 if (SYMBOLP (tem))
3041 fallback = Fget (tem, prop);
3042 }
3043 }
3044
3045 return fallback;
3046 }
3047
3048 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3049 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3050 (overlay, prop, value)
3051 Lisp_Object overlay, prop, value;
3052 {
3053 Lisp_Object tail, buffer;
3054 int changed;
3055
3056 CHECK_OVERLAY (overlay, 0);
3057
3058 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3059
3060 for (tail = XOVERLAY (overlay)->plist;
3061 CONSP (tail) && CONSP (XCONS (tail)->cdr);
3062 tail = XCONS (XCONS (tail)->cdr)->cdr)
3063 if (EQ (XCONS (tail)->car, prop))
3064 {
3065 changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
3066 XCONS (XCONS (tail)->cdr)->car = value;
3067 goto found;
3068 }
3069 /* It wasn't in the list, so add it to the front. */
3070 changed = !NILP (value);
3071 XOVERLAY (overlay)->plist
3072 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3073 found:
3074 if (! NILP (buffer))
3075 {
3076 if (changed)
3077 modify_overlay (XBUFFER (buffer),
3078 marker_position (OVERLAY_START (overlay)),
3079 marker_position (OVERLAY_END (overlay)));
3080 if (EQ (prop, Qevaporate) && ! NILP (value)
3081 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3082 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3083 Fdelete_overlay (overlay);
3084 }
3085 return value;
3086 }
3087 \f
3088 /* Subroutine of report_overlay_modification. */
3089
3090 /* Lisp vector holding overlay hook functions to call.
3091 Vector elements come in pairs.
3092 Each even-index element is a list of hook functions.
3093 The following odd-index element is the overlay they came from.
3094
3095 Before the buffer change, we fill in this vector
3096 as we call overlay hook functions.
3097 After the buffer change, we get the functions to call from this vector.
3098 This way we always call the same functions before and after the change. */
3099 static Lisp_Object last_overlay_modification_hooks;
3100
3101 /* Number of elements actually used in last_overlay_modification_hooks. */
3102 static int last_overlay_modification_hooks_used;
3103
3104 /* Add one functionlist/overlay pair
3105 to the end of last_overlay_modification_hooks. */
3106
3107 static void
3108 add_overlay_mod_hooklist (functionlist, overlay)
3109 Lisp_Object functionlist, overlay;
3110 {
3111 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3112
3113 if (last_overlay_modification_hooks_used == oldsize)
3114 {
3115 Lisp_Object old;
3116 old = last_overlay_modification_hooks;
3117 last_overlay_modification_hooks
3118 = Fmake_vector (make_number (oldsize * 2), Qnil);
3119 bcopy (XVECTOR (old)->contents,
3120 XVECTOR (last_overlay_modification_hooks)->contents,
3121 sizeof (Lisp_Object) * oldsize);
3122 }
3123 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3124 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3125 }
3126 \f
3127 /* Run the modification-hooks of overlays that include
3128 any part of the text in START to END.
3129 If this change is an insertion, also
3130 run the insert-before-hooks of overlay starting at END,
3131 and the insert-after-hooks of overlay ending at START.
3132
3133 This is called both before and after the modification.
3134 AFTER is nonzero when we call after the modification.
3135
3136 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3137 When AFTER is nonzero, they are the start position,
3138 the position after the inserted new text,
3139 and the length of deleted or replaced old text. */
3140
3141 void
3142 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3143 Lisp_Object start, end;
3144 int after;
3145 Lisp_Object arg1, arg2, arg3;
3146 {
3147 Lisp_Object prop, overlay, tail;
3148 /* 1 if this change is an insertion. */
3149 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3150 int tail_copied;
3151 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3152
3153 overlay = Qnil;
3154 tail = Qnil;
3155 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3156
3157 if (after)
3158 {
3159 /* Call the functions recorded in last_overlay_modification_hooks
3160 rather than scanning the overlays again.
3161 First copy the vector contents, in case some of these hooks
3162 do subsequent modification of the buffer. */
3163 int size = last_overlay_modification_hooks_used;
3164 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3165 int i;
3166
3167 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3168 copy, size * sizeof (Lisp_Object));
3169 gcpro1.var = copy;
3170 gcpro1.nvars = size;
3171
3172 for (i = 0; i < size;)
3173 {
3174 Lisp_Object prop, overlay;
3175 prop = copy[i++];
3176 overlay = copy[i++];
3177 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3178 }
3179 UNGCPRO;
3180 return;
3181 }
3182
3183 /* We are being called before a change.
3184 Scan the overlays to find the functions to call. */
3185 last_overlay_modification_hooks_used = 0;
3186 tail_copied = 0;
3187 for (tail = current_buffer->overlays_before;
3188 CONSP (tail);
3189 tail = XCONS (tail)->cdr)
3190 {
3191 int startpos, endpos;
3192 Lisp_Object ostart, oend;
3193
3194 overlay = XCONS (tail)->car;
3195
3196 ostart = OVERLAY_START (overlay);
3197 oend = OVERLAY_END (overlay);
3198 endpos = OVERLAY_POSITION (oend);
3199 if (XFASTINT (start) > endpos)
3200 break;
3201 startpos = OVERLAY_POSITION (ostart);
3202 if (insertion && (XFASTINT (start) == startpos
3203 || XFASTINT (end) == startpos))
3204 {
3205 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3206 if (!NILP (prop))
3207 {
3208 /* Copy TAIL in case the hook recenters the overlay lists. */
3209 if (!tail_copied)
3210 tail = Fcopy_sequence (tail);
3211 tail_copied = 1;
3212 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3213 }
3214 }
3215 if (insertion && (XFASTINT (start) == endpos
3216 || XFASTINT (end) == endpos))
3217 {
3218 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3219 if (!NILP (prop))
3220 {
3221 if (!tail_copied)
3222 tail = Fcopy_sequence (tail);
3223 tail_copied = 1;
3224 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3225 }
3226 }
3227 /* Test for intersecting intervals. This does the right thing
3228 for both insertion and deletion. */
3229 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3230 {
3231 prop = Foverlay_get (overlay, Qmodification_hooks);
3232 if (!NILP (prop))
3233 {
3234 if (!tail_copied)
3235 tail = Fcopy_sequence (tail);
3236 tail_copied = 1;
3237 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3238 }
3239 }
3240 }
3241
3242 tail_copied = 0;
3243 for (tail = current_buffer->overlays_after;
3244 CONSP (tail);
3245 tail = XCONS (tail)->cdr)
3246 {
3247 int startpos, endpos;
3248 Lisp_Object ostart, oend;
3249
3250 overlay = XCONS (tail)->car;
3251
3252 ostart = OVERLAY_START (overlay);
3253 oend = OVERLAY_END (overlay);
3254 startpos = OVERLAY_POSITION (ostart);
3255 endpos = OVERLAY_POSITION (oend);
3256 if (XFASTINT (end) < startpos)
3257 break;
3258 if (insertion && (XFASTINT (start) == startpos
3259 || XFASTINT (end) == startpos))
3260 {
3261 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3262 if (!NILP (prop))
3263 {
3264 if (!tail_copied)
3265 tail = Fcopy_sequence (tail);
3266 tail_copied = 1;
3267 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3268 }
3269 }
3270 if (insertion && (XFASTINT (start) == endpos
3271 || XFASTINT (end) == endpos))
3272 {
3273 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3274 if (!NILP (prop))
3275 {
3276 if (!tail_copied)
3277 tail = Fcopy_sequence (tail);
3278 tail_copied = 1;
3279 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3280 }
3281 }
3282 /* Test for intersecting intervals. This does the right thing
3283 for both insertion and deletion. */
3284 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3285 {
3286 prop = Foverlay_get (overlay, Qmodification_hooks);
3287 if (!NILP (prop))
3288 {
3289 if (!tail_copied)
3290 tail = Fcopy_sequence (tail);
3291 tail_copied = 1;
3292 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3293 }
3294 }
3295 }
3296
3297 UNGCPRO;
3298 }
3299
3300 static void
3301 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3302 Lisp_Object list, overlay;
3303 int after;
3304 Lisp_Object arg1, arg2, arg3;
3305 {
3306 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3307
3308 GCPRO4 (list, arg1, arg2, arg3);
3309 if (! after)
3310 add_overlay_mod_hooklist (list, overlay);
3311
3312 while (!NILP (list))
3313 {
3314 if (NILP (arg3))
3315 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3316 else
3317 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
3318 list = Fcdr (list);
3319 }
3320 UNGCPRO;
3321 }
3322
3323 /* Delete any zero-sized overlays at position POS, if the `evaporate'
3324 property is set. */
3325 void
3326 evaporate_overlays (pos)
3327 int pos;
3328 {
3329 Lisp_Object tail, overlay, hit_list;
3330
3331 hit_list = Qnil;
3332 if (pos <= XFASTINT (current_buffer->overlay_center))
3333 for (tail = current_buffer->overlays_before; CONSP (tail);
3334 tail = XCONS (tail)->cdr)
3335 {
3336 int endpos;
3337 overlay = XCONS (tail)->car;
3338 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3339 if (endpos < pos)
3340 break;
3341 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
3342 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3343 hit_list = Fcons (overlay, hit_list);
3344 }
3345 else
3346 for (tail = current_buffer->overlays_after; CONSP (tail);
3347 tail = XCONS (tail)->cdr)
3348 {
3349 int startpos;
3350 overlay = XCONS (tail)->car;
3351 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3352 if (startpos > pos)
3353 break;
3354 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
3355 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3356 hit_list = Fcons (overlay, hit_list);
3357 }
3358 for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
3359 Fdelete_overlay (XCONS (hit_list)->car);
3360 }
3361 \f
3362 /* Somebody has tried to store a value with an unacceptable type
3363 into the buffer-local slot with offset OFFSET. */
3364 void
3365 buffer_slot_type_mismatch (offset)
3366 int offset;
3367 {
3368 Lisp_Object sym;
3369 char *type_name;
3370 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
3371 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
3372 {
3373 case Lisp_Int: type_name = "integers"; break;
3374 case Lisp_String: type_name = "strings"; break;
3375 case Lisp_Symbol: type_name = "symbols"; break;
3376 default:
3377 abort ();
3378 }
3379
3380 error ("only %s should be stored in the buffer-local variable %s",
3381 type_name, XSYMBOL (sym)->name->data);
3382 }
3383 \f
3384 init_buffer_once ()
3385 {
3386 register Lisp_Object tem;
3387
3388 buffer_permanent_local_flags = 0;
3389
3390 /* Make sure all markable slots in buffer_defaults
3391 are initialized reasonably, so mark_buffer won't choke. */
3392 reset_buffer (&buffer_defaults);
3393 reset_buffer_local_variables (&buffer_defaults, 1);
3394 reset_buffer (&buffer_local_symbols);
3395 reset_buffer_local_variables (&buffer_local_symbols, 1);
3396 /* Prevent GC from getting confused. */
3397 buffer_defaults.text = &buffer_defaults.own_text;
3398 buffer_local_symbols.text = &buffer_local_symbols.own_text;
3399 #ifdef USE_TEXT_PROPERTIES
3400 BUF_INTERVALS (&buffer_defaults) = 0;
3401 BUF_INTERVALS (&buffer_local_symbols) = 0;
3402 #endif
3403 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3404 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
3405
3406 /* Set up the default values of various buffer slots. */
3407 /* Must do these before making the first buffer! */
3408
3409 /* real setup is done in loaddefs.el */
3410 buffer_defaults.mode_line_format = build_string ("%-");
3411 buffer_defaults.abbrev_mode = Qnil;
3412 buffer_defaults.overwrite_mode = Qnil;
3413 buffer_defaults.case_fold_search = Qt;
3414 buffer_defaults.auto_fill_function = Qnil;
3415 buffer_defaults.selective_display = Qnil;
3416 #ifndef old
3417 buffer_defaults.selective_display_ellipses = Qt;
3418 #endif
3419 buffer_defaults.abbrev_table = Qnil;
3420 buffer_defaults.display_table = Qnil;
3421 buffer_defaults.undo_list = Qnil;
3422 buffer_defaults.mark_active = Qnil;
3423 buffer_defaults.file_format = Qnil;
3424 buffer_defaults.overlays_before = Qnil;
3425 buffer_defaults.overlays_after = Qnil;
3426 XSETFASTINT (buffer_defaults.overlay_center, BEG);
3427
3428 XSETFASTINT (buffer_defaults.tab_width, 8);
3429 buffer_defaults.truncate_lines = Qnil;
3430 buffer_defaults.ctl_arrow = Qt;
3431
3432 #ifdef DOS_NT
3433 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
3434 #endif
3435 XSETFASTINT (buffer_defaults.fill_column, 70);
3436 XSETFASTINT (buffer_defaults.left_margin, 0);
3437 buffer_defaults.cache_long_line_scans = Qnil;
3438 buffer_defaults.file_truename = Qnil;
3439
3440 /* Assign the local-flags to the slots that have default values.
3441 The local flag is a bit that is used in the buffer
3442 to say that it has its own local value for the slot.
3443 The local flag bits are in the local_var_flags slot of the buffer. */
3444
3445 /* Nothing can work if this isn't true */
3446 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
3447
3448 /* 0 means not a lisp var, -1 means always local, else mask */
3449 bzero (&buffer_local_flags, sizeof buffer_local_flags);
3450 XSETINT (buffer_local_flags.filename, -1);
3451 XSETINT (buffer_local_flags.directory, -1);
3452 XSETINT (buffer_local_flags.backed_up, -1);
3453 XSETINT (buffer_local_flags.save_length, -1);
3454 XSETINT (buffer_local_flags.auto_save_file_name, -1);
3455 XSETINT (buffer_local_flags.read_only, -1);
3456 XSETINT (buffer_local_flags.major_mode, -1);
3457 XSETINT (buffer_local_flags.mode_name, -1);
3458 XSETINT (buffer_local_flags.undo_list, -1);
3459 XSETINT (buffer_local_flags.mark_active, -1);
3460 XSETINT (buffer_local_flags.point_before_scroll, -1);
3461 XSETINT (buffer_local_flags.file_truename, -1);
3462 XSETINT (buffer_local_flags.invisibility_spec, -1);
3463 XSETINT (buffer_local_flags.file_format, -1);
3464
3465 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
3466 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
3467 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
3468 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
3469 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
3470 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
3471 #ifndef old
3472 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
3473 #endif
3474 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
3475 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
3476 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
3477 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
3478 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
3479 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
3480 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
3481 #ifdef DOS_NT
3482 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
3483 /* Make this one a permanent local. */
3484 buffer_permanent_local_flags |= 0x4000;
3485 #endif
3486 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
3487 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
3488
3489 Vbuffer_alist = Qnil;
3490 current_buffer = 0;
3491 all_buffers = 0;
3492
3493 QSFundamental = build_string ("Fundamental");
3494
3495 Qfundamental_mode = intern ("fundamental-mode");
3496 buffer_defaults.major_mode = Qfundamental_mode;
3497
3498 Qmode_class = intern ("mode-class");
3499
3500 Qprotected_field = intern ("protected-field");
3501
3502 Qpermanent_local = intern ("permanent-local");
3503
3504 Qkill_buffer_hook = intern ("kill-buffer-hook");
3505
3506 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
3507
3508 /* super-magic invisible buffer */
3509 Vbuffer_alist = Qnil;
3510
3511 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3512 }
3513
3514 init_buffer ()
3515 {
3516 char buf[MAXPATHLEN+1];
3517 char *pwd;
3518 struct stat dotstat, pwdstat;
3519 Lisp_Object temp;
3520 int rc;
3521
3522 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3523
3524 /* If PWD is accurate, use it instead of calling getwd. This is faster
3525 when PWD is right, and may avoid a fatal error. */
3526 if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
3527 && stat (pwd, &pwdstat) == 0
3528 && stat (".", &dotstat) == 0
3529 && dotstat.st_ino == pwdstat.st_ino
3530 && dotstat.st_dev == pwdstat.st_dev
3531 && strlen (pwd) < MAXPATHLEN)
3532 strcpy (buf, pwd);
3533 else if (getwd (buf) == 0)
3534 fatal ("`getwd' failed: %s\n", buf);
3535
3536 #ifndef VMS
3537 /* Maybe this should really use some standard subroutine
3538 whose definition is filename syntax dependent. */
3539 rc = strlen (buf);
3540 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
3541 {
3542 buf[rc] = DIRECTORY_SEP;
3543 buf[rc + 1] = '\0';
3544 }
3545 #endif /* not VMS */
3546
3547 current_buffer->directory = build_string (buf);
3548
3549 /* Add /: to the front of the name
3550 if it would otherwise be treated as magic. */
3551 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
3552 if (! NILP (temp))
3553 current_buffer->directory
3554 = concat2 (build_string ("/:"), current_buffer->directory);
3555
3556 temp = get_minibuffer (0);
3557 XBUFFER (temp)->directory = current_buffer->directory;
3558 }
3559
3560 /* initialize the buffer routines */
3561 syms_of_buffer ()
3562 {
3563 extern Lisp_Object Qdisabled;
3564
3565 staticpro (&last_overlay_modification_hooks);
3566 last_overlay_modification_hooks
3567 = Fmake_vector (make_number (10), Qnil);
3568
3569 staticpro (&Vbuffer_defaults);
3570 staticpro (&Vbuffer_local_symbols);
3571 staticpro (&Qfundamental_mode);
3572 staticpro (&Qmode_class);
3573 staticpro (&QSFundamental);
3574 staticpro (&Vbuffer_alist);
3575 staticpro (&Qprotected_field);
3576 staticpro (&Qpermanent_local);
3577 staticpro (&Qkill_buffer_hook);
3578 Qoverlayp = intern ("overlayp");
3579 staticpro (&Qoverlayp);
3580 Qevaporate = intern ("evaporate");
3581 staticpro (&Qevaporate);
3582 Qmodification_hooks = intern ("modification-hooks");
3583 staticpro (&Qmodification_hooks);
3584 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
3585 staticpro (&Qinsert_in_front_hooks);
3586 Qinsert_behind_hooks = intern ("insert-behind-hooks");
3587 staticpro (&Qinsert_behind_hooks);
3588 Qget_file_buffer = intern ("get-file-buffer");
3589 staticpro (&Qget_file_buffer);
3590 Qpriority = intern ("priority");
3591 staticpro (&Qpriority);
3592 Qwindow = intern ("window");
3593 staticpro (&Qwindow);
3594 Qbefore_string = intern ("before-string");
3595 staticpro (&Qbefore_string);
3596 Qafter_string = intern ("after-string");
3597 staticpro (&Qafter_string);
3598 Qfirst_change_hook = intern ("first-change-hook");
3599 staticpro (&Qfirst_change_hook);
3600 Qbefore_change_functions = intern ("before-change-functions");
3601 staticpro (&Qbefore_change_functions);
3602 Qafter_change_functions = intern ("after-change-functions");
3603 staticpro (&Qafter_change_functions);
3604
3605 Fput (Qprotected_field, Qerror_conditions,
3606 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
3607 Fput (Qprotected_field, Qerror_message,
3608 build_string ("Attempt to modify a protected field"));
3609
3610 /* All these use DEFVAR_LISP_NOPRO because the slots in
3611 buffer_defaults will all be marked via Vbuffer_defaults. */
3612
3613 DEFVAR_LISP_NOPRO ("default-mode-line-format",
3614 &buffer_defaults.mode_line_format,
3615 "Default value of `mode-line-format' for buffers that don't override it.\n\
3616 This is the same as (default-value 'mode-line-format).");
3617
3618 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
3619 &buffer_defaults.abbrev_mode,
3620 "Default value of `abbrev-mode' for buffers that do not override it.\n\
3621 This is the same as (default-value 'abbrev-mode).");
3622
3623 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
3624 &buffer_defaults.ctl_arrow,
3625 "Default value of `ctl-arrow' for buffers that do not override it.\n\
3626 This is the same as (default-value 'ctl-arrow).");
3627
3628 DEFVAR_LISP_NOPRO ("default-truncate-lines",
3629 &buffer_defaults.truncate_lines,
3630 "Default value of `truncate-lines' for buffers that do not override it.\n\
3631 This is the same as (default-value 'truncate-lines).");
3632
3633 DEFVAR_LISP_NOPRO ("default-fill-column",
3634 &buffer_defaults.fill_column,
3635 "Default value of `fill-column' for buffers that do not override it.\n\
3636 This is the same as (default-value 'fill-column).");
3637
3638 DEFVAR_LISP_NOPRO ("default-left-margin",
3639 &buffer_defaults.left_margin,
3640 "Default value of `left-margin' for buffers that do not override it.\n\
3641 This is the same as (default-value 'left-margin).");
3642
3643 DEFVAR_LISP_NOPRO ("default-tab-width",
3644 &buffer_defaults.tab_width,
3645 "Default value of `tab-width' for buffers that do not override it.\n\
3646 This is the same as (default-value 'tab-width).");
3647
3648 DEFVAR_LISP_NOPRO ("default-case-fold-search",
3649 &buffer_defaults.case_fold_search,
3650 "Default value of `case-fold-search' for buffers that don't override it.\n\
3651 This is the same as (default-value 'case-fold-search).");
3652
3653 #ifdef DOS_NT
3654 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
3655 &buffer_defaults.buffer_file_type,
3656 "Default file type for buffers that do not override it.\n\
3657 This is the same as (default-value 'buffer-file-type).\n\
3658 The file type is nil for text, t for binary.");
3659 #endif
3660
3661 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
3662 Qnil, 0);
3663
3664 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
3665 But make-docfile finds it!
3666 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
3667 Qnil,
3668 "Template for displaying mode line for current buffer.\n\
3669 Each buffer has its own value of this variable.\n\
3670 Value may be a string, a symbol or a list or cons cell.\n\
3671 For a symbol, its value is used (but it is ignored if t or nil).\n\
3672 A string appearing directly as the value of a symbol is processed verbatim\n\
3673 in that the %-constructs below are not recognized.\n\
3674 For a list whose car is a symbol, the symbol's value is taken,\n\
3675 and if that is non-nil, the cadr of the list is processed recursively.\n\
3676 Otherwise, the caddr of the list (if there is one) is processed.\n\
3677 For a list whose car is a string or list, each element is processed\n\
3678 recursively and the results are effectively concatenated.\n\
3679 For a list whose car is an integer, the cdr of the list is processed\n\
3680 and padded (if the number is positive) or truncated (if negative)\n\
3681 to the width specified by that number.\n\
3682 A string is printed verbatim in the mode line except for %-constructs:\n\
3683 (%-constructs are allowed when the string is the entire mode-line-format\n\
3684 or when it is found in a cons-cell or a list)\n\
3685 %b -- print buffer name. %f -- print visited file name.\n\
3686 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
3687 % means buffer is read-only and * means it is modified.\n\
3688 For a modified read-only buffer, %* gives % and %+ gives *.\n\
3689 %s -- print process status. %l -- print the current line number.\n\
3690 %c -- print the current column number (this makes editing slower).\n\
3691 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
3692 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
3693 or print Bottom or All.\n\
3694 %n -- print Narrow if appropriate.\n\
3695 %t -- print T if file is text, B if binary.\n\
3696 %[ -- print one [ for each recursive editing level. %] similar.\n\
3697 %% -- print %. %- -- print infinitely many dashes.\n\
3698 Decimal digits after the % specify field width to which to pad.");
3699 */
3700
3701 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
3702 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
3703 nil here means use current buffer's major mode.");
3704
3705 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
3706 make_number (Lisp_Symbol),
3707 "Symbol for current buffer's major mode.");
3708
3709 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
3710 make_number (Lisp_String),
3711 "Pretty name of current buffer's major mode (a string).");
3712
3713 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
3714 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
3715 Automatically becomes buffer-local when set in any fashion.");
3716
3717 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
3718 Qnil,
3719 "*Non-nil if searches should ignore case.\n\
3720 Automatically becomes buffer-local when set in any fashion.");
3721
3722 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
3723 make_number (Lisp_Int),
3724 "*Column beyond which automatic line-wrapping should happen.\n\
3725 Automatically becomes buffer-local when set in any fashion.");
3726
3727 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
3728 make_number (Lisp_Int),
3729 "*Column for the default indent-line-function to indent to.\n\
3730 Linefeed indents to this column in Fundamental mode.\n\
3731 Automatically becomes buffer-local when set in any fashion.");
3732
3733 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
3734 make_number (Lisp_Int),
3735 "*Distance between tab stops (for display of tab characters), in columns.\n\
3736 Automatically becomes buffer-local when set in any fashion.");
3737
3738 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
3739 "*Non-nil means display control chars with uparrow.\n\
3740 Nil means use backslash and octal digits.\n\
3741 Automatically becomes buffer-local when set in any fashion.\n\
3742 This variable does not apply to characters whose display is specified\n\
3743 in the current display table (if there is one).");
3744
3745 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
3746 "*Non-nil means do not display continuation lines;\n\
3747 give each line of text one screen line.\n\
3748 Automatically becomes buffer-local when set in any fashion.\n\
3749 \n\
3750 Note that this is overridden by the variable\n\
3751 `truncate-partial-width-windows' if that variable is non-nil\n\
3752 and this buffer is not full-frame width.");
3753
3754 #ifdef DOS_NT
3755 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
3756 Qnil,
3757 "Non-nil if the visited file is a binary file.\n\
3758 This variable is meaningful on MS-DOG and Windows NT.\n\
3759 On those systems, it is automatically local in every buffer.\n\
3760 On other systems, this variable is normally always nil.");
3761 #endif
3762
3763 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
3764 make_number (Lisp_String),
3765 "Name of default directory of current buffer. Should end with slash.\n\
3766 Each buffer has its own value of this variable.");
3767
3768 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
3769 Qnil,
3770 "Function called (if non-nil) to perform auto-fill.\n\
3771 It is called after self-inserting a space or newline.\n\
3772 Each buffer has its own value of this variable.\n\
3773 NOTE: This variable is not a hook;\n\
3774 its value may not be a list of functions.");
3775
3776 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
3777 make_number (Lisp_String),
3778 "Name of file visited in current buffer, or nil if not visiting a file.\n\
3779 Each buffer has its own value of this variable.");
3780
3781 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
3782 make_number (Lisp_String),
3783 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
3784 The truename of a file is calculated by `file-truename'\n\
3785 and then abbreviated with `abbreviate-file-name'.\n\
3786 Each buffer has its own value of this variable.");
3787
3788 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3789 &current_buffer->auto_save_file_name,
3790 make_number (Lisp_String),
3791 "Name of file for auto-saving current buffer,\n\
3792 or nil if buffer should not be auto-saved.\n\
3793 Each buffer has its own value of this variable.");
3794
3795 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
3796 "Non-nil if this buffer is read-only.\n\
3797 Each buffer has its own value of this variable.");
3798
3799 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
3800 "Non-nil if this buffer's file has been backed up.\n\
3801 Backing up is done before the first time the file is saved.\n\
3802 Each buffer has its own value of this variable.");
3803
3804 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
3805 make_number (Lisp_Int),
3806 "Length of current buffer when last read in, saved or auto-saved.\n\
3807 0 initially.\n\
3808 Each buffer has its own value of this variable.");
3809
3810 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
3811 Qnil,
3812 "Non-nil enables selective display:\n\
3813 Integer N as value means display only lines\n\
3814 that start with less than n columns of space.\n\
3815 A value of t means, after a ^M, all the rest of the line is invisible.\n\
3816 Then ^M's in the file are written into files as newlines.\n\n\
3817 Automatically becomes buffer-local when set in any fashion.");
3818
3819 #ifndef old
3820 DEFVAR_PER_BUFFER ("selective-display-ellipses",
3821 &current_buffer->selective_display_ellipses,
3822 Qnil,
3823 "t means display ... on previous line when a line is invisible.\n\
3824 Automatically becomes buffer-local when set in any fashion.");
3825 #endif
3826
3827 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
3828 "Non-nil if self-insertion should replace existing text.\n\
3829 The value should be one of `overwrite-mode-textual',\n\
3830 `overwrite-mode-binary', or nil.\n\
3831 If it is `overwrite-mode-textual', self-insertion still\n\
3832 inserts at the end of a line, and inserts when point is before a tab,\n\
3833 until the tab is filled in.\n\
3834 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
3835 Automatically becomes buffer-local when set in any fashion.");
3836
3837 #if 0 /* The doc string is too long for some compilers,
3838 but make-docfile can find it in this comment. */
3839 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
3840 Qnil,
3841 "Display table that controls display of the contents of current buffer.\n\
3842 Automatically becomes buffer-local when set in any fashion.\n\
3843 The display table is a char-table created with `make-display-table'.\n\
3844 The ordinary char-table elements control how to display each possible text\n\
3845 character. Each value should be a vector of characters or nil;\n\
3846 nil means display the character in the default fashion.\n\
3847 There are six extra slots to control the display of\n\
3848 the end of a truncated screen line (extra-slot 0, a single character);\n\
3849 the end of a continued line (extra-slot 1, a single character);\n\
3850 the escape character used to display character codes in octal\n\
3851 (extra-slot 2, a single character);\n\
3852 the character used as an arrow for control characters (extra-slot 3,\n\
3853 a single character);\n\
3854 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
3855 a vector of characters);\n\
3856 the character used to draw the border between side-by-side windows\n\
3857 (extra-slot 5, a single character).\n\
3858 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
3859 If this variable is nil, the value of `standard-display-table' is used.\n\
3860 Each window can have its own, overriding display table.");
3861 #endif
3862 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
3863 Qnil, 0);
3864
3865 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
3866 "Don't ask.");
3867 */
3868 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
3869 "If non-nil, a function to call before each text change (obsolete).\n\
3870 Two arguments are passed to the function: the positions of\n\
3871 the beginning and end of the range of old text to be changed.\n\
3872 \(For an insertion, the beginning and end are at the same place.)\n\
3873 No information is given about the length of the text after the change.\n\
3874 \n\
3875 Buffer changes made while executing the `before-change-function'\n\
3876 don't call any before-change or after-change functions.\n\
3877 That's because these variables are temporarily set to nil.\n\
3878 As a result, a hook function cannot straightforwardly alter the value of\n\
3879 these variables. See the Emacs Lisp manual for a way of\n\
3880 accomplishing an equivalent result by using other variables.\n\n\
3881 This variable is obsolete; use `before-change-functions' instead.");
3882 Vbefore_change_function = Qnil;
3883
3884 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
3885 "If non-nil, a Function to call after each text change (obsolete).\n\
3886 Three arguments are passed to the function: the positions of\n\
3887 the beginning and end of the range of changed text,\n\
3888 and the length of the pre-change text replaced by that range.\n\
3889 \(For an insertion, the pre-change length is zero;\n\
3890 for a deletion, that length is the number of characters deleted,\n\
3891 and the post-change beginning and end are at the same place.)\n\
3892 \n\
3893 Buffer changes made while executing the `after-change-function'\n\
3894 don't call any before-change or after-change functions.\n\
3895 That's because these variables are temporarily set to nil.\n\
3896 As a result, a hook function cannot straightforwardly alter the value of\n\
3897 these variables. See the Emacs Lisp manual for a way of\n\
3898 accomplishing an equivalent result by using other variables.\n\n\
3899 This variable is obsolete; use `after-change-functions' instead.");
3900 Vafter_change_function = Qnil;
3901
3902 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
3903 "List of functions to call before each text change.\n\
3904 Two arguments are passed to each function: the positions of\n\
3905 the beginning and end of the range of old text to be changed.\n\
3906 \(For an insertion, the beginning and end are at the same place.)\n\
3907 No information is given about the length of the text after the change.\n\
3908 \n\
3909 Buffer changes made while executing the `before-change-functions'\n\
3910 don't call any before-change or after-change functions.\n\
3911 That's because these variables are temporarily set to nil.\n\
3912 As a result, a hook function cannot straightforwardly alter the value of\n\
3913 these variables. See the Emacs Lisp manual for a way of\n\
3914 accomplishing an equivalent result by using other variables.");
3915 Vbefore_change_functions = Qnil;
3916
3917 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
3918 "List of function to call after each text change.\n\
3919 Three arguments are passed to each function: the positions of\n\
3920 the beginning and end of the range of changed text,\n\
3921 and the length of the pre-change text replaced by that range.\n\
3922 \(For an insertion, the pre-change length is zero;\n\
3923 for a deletion, that length is the number of characters deleted,\n\
3924 and the post-change beginning and end are at the same place.)\n\
3925 \n\
3926 Buffer changes made while executing the `after-change-functions'\n\
3927 don't call any before-change or after-change functions.\n\
3928 That's because these variables are temporarily set to nil.\n\
3929 As a result, a hook function cannot straightforwardly alter the value of\n\
3930 these variables. See the Emacs Lisp manual for a way of\n\
3931 accomplishing an equivalent result by using other variables.");
3932
3933 Vafter_change_functions = Qnil;
3934
3935 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
3936 "A list of functions to call before changing a buffer which is unmodified.\n\
3937 The functions are run using the `run-hooks' function.");
3938 Vfirst_change_hook = Qnil;
3939
3940 #if 0 /* The doc string is too long for some compilers,
3941 but make-docfile can find it in this comment. */
3942 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
3943 "List of undo entries in current buffer.\n\
3944 Recent changes come first; older changes follow newer.\n\
3945 \n\
3946 An entry (BEG . END) represents an insertion which begins at\n\
3947 position BEG and ends at position END.\n\
3948 \n\
3949 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
3950 from (abs POSITION). If POSITION is positive, point was at the front\n\
3951 of the text being deleted; if negative, point was at the end.\n\
3952 \n\
3953 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
3954 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
3955 of the visited file's modification time, as of that time. If the\n\
3956 modification time of the most recent save is different, this entry is\n\
3957 obsolete.\n\
3958 \n\
3959 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
3960 was modified between BEG and END. PROPERTY is the property name,\n\
3961 and VALUE is the old value.\n\
3962 \n\
3963 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
3964 was adjusted in position by the offset DISTANCE (an integer).\n\
3965 \n\
3966 An entry of the form POSITION indicates that point was at the buffer\n\
3967 location given by the integer. Undoing an entry of this form places\n\
3968 point at POSITION.\n\
3969 \n\
3970 nil marks undo boundaries. The undo command treats the changes\n\
3971 between two undo boundaries as a single step to be undone.\n\
3972 \n\
3973 If the value of the variable is t, undo information is not recorded.");
3974 #endif
3975 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
3976 0);
3977
3978 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
3979 "Non-nil means the mark and region are currently active in this buffer.\n\
3980 Automatically local in all buffers.");
3981
3982 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
3983 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
3984 This variable is buffer-local, in all buffers.\n\
3985 \n\
3986 Normally, the line-motion functions work by scanning the buffer for\n\
3987 newlines. Columnar operations (like move-to-column and\n\
3988 compute-motion) also work by scanning the buffer, summing character\n\
3989 widths as they go. This works well for ordinary text, but if the\n\
3990 buffer's lines are very long (say, more than 500 characters), these\n\
3991 motion functions will take longer to execute. Emacs may also take\n\
3992 longer to update the display.\n\
3993 \n\
3994 If cache-long-line-scans is non-nil, these motion functions cache the\n\
3995 results of their scans, and consult the cache to avoid rescanning\n\
3996 regions of the buffer until the text is modified. The caches are most\n\
3997 beneficial when they prevent the most searching---that is, when the\n\
3998 buffer contains long lines and large regions of characters with the\n\
3999 same, fixed screen width.\n\
4000 \n\
4001 When cache-long-line-scans is non-nil, processing short lines will\n\
4002 become slightly slower (because of the overhead of consulting the\n\
4003 cache), and the caches will use memory roughly proportional to the\n\
4004 number of newlines and characters whose screen width varies.\n\
4005 \n\
4006 The caches require no explicit maintenance; their accuracy is\n\
4007 maintained internally by the Emacs primitives. Enabling or disabling\n\
4008 the cache should not affect the behavior of any of the motion\n\
4009 functions; it should only affect their performance.");
4010
4011 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
4012 "Value of point before the last series of scroll operations, or nil.");
4013
4014 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4015 "List of formats to use when saving this buffer.\n\
4016 Formats are defined by `format-alist'. This variable is\n\
4017 set when a file is visited. Automatically local in all buffers.");
4018
4019 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4020 &current_buffer->invisibility_spec, Qnil,
4021 "Invisibility spec of this buffer.\n\
4022 The default is t, which means that text is invisible\n\
4023 if it has a non-nil `invisible' property.\n\
4024 If the value is a list, a text character is invisible if its `invisible'\n\
4025 property is an element in that list.\n\
4026 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4027 then characters with property value PROP are invisible,\n\
4028 and they have an ellipsis as well if ELLIPSIS is non-nil.");
4029
4030 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
4031 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4032 Non-nil also enables highlighting of the region whenever the mark is active.\n\
4033 The variable `highlight-nonselected-windows' controls whether to highlight\n\
4034 all windows or just the selected window.");
4035 Vtransient_mark_mode = Qnil;
4036
4037 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
4038 "*Non-nil means disregard read-only status of buffers or characters.\n\
4039 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4040 text properties. If the value is a list, disregard `buffer-read-only'\n\
4041 and disregard a `read-only' text property if the property value\n\
4042 is a member of the list.");
4043 Vinhibit_read_only = Qnil;
4044
4045 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4046 "List of functions called with no args to query before killing a buffer.");
4047 Vkill_buffer_query_functions = Qnil;
4048
4049 defsubr (&Sbuffer_live_p);
4050 defsubr (&Sbuffer_list);
4051 defsubr (&Sget_buffer);
4052 defsubr (&Sget_file_buffer);
4053 defsubr (&Sget_buffer_create);
4054 defsubr (&Smake_indirect_buffer);
4055 defsubr (&Sgenerate_new_buffer_name);
4056 defsubr (&Sbuffer_name);
4057 /*defsubr (&Sbuffer_number);*/
4058 defsubr (&Sbuffer_file_name);
4059 defsubr (&Sbuffer_base_buffer);
4060 defsubr (&Sbuffer_local_variables);
4061 defsubr (&Sbuffer_modified_p);
4062 defsubr (&Sset_buffer_modified_p);
4063 defsubr (&Sbuffer_modified_tick);
4064 defsubr (&Srename_buffer);
4065 defsubr (&Sother_buffer);
4066 defsubr (&Sbuffer_disable_undo);
4067 defsubr (&Sbuffer_enable_undo);
4068 defsubr (&Skill_buffer);
4069 defsubr (&Serase_buffer);
4070 defsubr (&Sset_buffer_major_mode);
4071 defsubr (&Sswitch_to_buffer);
4072 defsubr (&Spop_to_buffer);
4073 defsubr (&Scurrent_buffer);
4074 defsubr (&Sset_buffer);
4075 defsubr (&Sbarf_if_buffer_read_only);
4076 defsubr (&Sbury_buffer);
4077 defsubr (&Skill_all_local_variables);
4078
4079 defsubr (&Soverlayp);
4080 defsubr (&Smake_overlay);
4081 defsubr (&Sdelete_overlay);
4082 defsubr (&Smove_overlay);
4083 defsubr (&Soverlay_start);
4084 defsubr (&Soverlay_end);
4085 defsubr (&Soverlay_buffer);
4086 defsubr (&Soverlay_properties);
4087 defsubr (&Soverlays_at);
4088 defsubr (&Soverlays_in);
4089 defsubr (&Snext_overlay_change);
4090 defsubr (&Sprevious_overlay_change);
4091 defsubr (&Soverlay_recenter);
4092 defsubr (&Soverlay_lists);
4093 defsubr (&Soverlay_get);
4094 defsubr (&Soverlay_put);
4095 }
4096
4097 keys_of_buffer ()
4098 {
4099 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4100 initial_define_key (control_x_map, 'k', "kill-buffer");
4101
4102 /* This must not be in syms_of_buffer, because Qdisabled is not
4103 initialized when that function gets called. */
4104 Fput (intern ("erase-buffer"), Qdisabled, Qt);
4105 }