(combine-run-hooks): New function.
[emacs.git] / src / buffer.c
blob878a81c47c7a7e5110527e71d4ea8acb977d24f0
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <errno.h>
29 extern int errno;
31 #ifndef MAXPATHLEN
32 /* in 4.1, param.h fails to define this. */
33 #define MAXPATHLEN 1024
34 #endif /* not MAXPATHLEN */
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include "lisp.h"
40 #include "intervals.h"
41 #include "window.h"
42 #include "commands.h"
43 #include "buffer.h"
44 #include "charset.h"
45 #include "region-cache.h"
46 #include "indent.h"
47 #include "blockinput.h"
48 #include "frame.h"
50 struct buffer *current_buffer; /* the current buffer */
52 /* First buffer in chain of all buffers (in reverse order of creation).
53 Threaded through ->next. */
55 struct buffer *all_buffers;
57 /* This structure holds the default values of the buffer-local variables
58 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
59 The default value occupies the same slot in this structure
60 as an individual buffer's value occupies in that buffer.
61 Setting the default value also goes through the alist of buffers
62 and stores into each buffer that does not say it has a local value. */
64 struct buffer buffer_defaults;
66 /* A Lisp_Object pointer to the above, used for staticpro */
68 static Lisp_Object Vbuffer_defaults;
70 /* This structure marks which slots in a buffer have corresponding
71 default values in buffer_defaults.
72 Each such slot has a nonzero value in this structure.
73 The value has only one nonzero bit.
75 When a buffer has its own local value for a slot,
76 the bit for that slot (found in the same slot in this structure)
77 is turned on in the buffer's local_var_flags slot.
79 If a slot in this structure is -1, then even though there may
80 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
81 and the corresponding slot in buffer_defaults is not used.
83 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
84 but there is a default value which is copied into each buffer.
86 If a slot in this structure is negative, then even though there may
87 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
88 and the corresponding slot in buffer_defaults is not used.
90 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91 zero, that is a bug */
93 struct buffer buffer_local_flags;
95 /* This structure holds the names of symbols whose values may be
96 buffer-local. It is indexed and accessed in the same way as the above. */
98 struct buffer buffer_local_symbols;
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols;
102 /* This structure holds the required types for the values in the
103 buffer-local slots. If a slot contains Qnil, then the
104 corresponding buffer slot may contain a value of any type. If a
105 slot contains an integer, then prospective values' tags must be
106 equal to that integer (except nil is always allowed).
107 When a tag does not match, the function
108 buffer_slot_type_mismatch will signal an error.
110 If a slot here contains -1, the corresponding variable is read-only. */
111 struct buffer buffer_local_types;
113 /* Flags indicating which built-in buffer-local variables
114 are permanent locals. */
115 static int buffer_permanent_local_flags;
117 Lisp_Object Fset_buffer ();
118 void set_buffer_internal ();
119 void set_buffer_internal_1 ();
120 static void call_overlay_mod_hooks ();
121 static void swap_out_buffer_local_variables ();
122 static void reset_buffer_local_variables ();
124 /* Alist of all buffer names vs the buffers. */
125 /* This used to be a variable, but is no longer,
126 to prevent lossage due to user rplac'ing this alist or its elements. */
127 Lisp_Object Vbuffer_alist;
129 /* Functions to call before and after each text change. */
130 Lisp_Object Vbefore_change_function;
131 Lisp_Object Vafter_change_function;
132 Lisp_Object Vbefore_change_functions;
133 Lisp_Object Vafter_change_functions;
135 Lisp_Object Vtransient_mark_mode;
137 /* t means ignore all read-only text properties.
138 A list means ignore such a property if its value is a member of the list.
139 Any non-nil value means ignore buffer-read-only. */
140 Lisp_Object Vinhibit_read_only;
142 /* List of functions to call that can query about killing a buffer.
143 If any of these functions returns nil, we don't kill it. */
144 Lisp_Object Vkill_buffer_query_functions;
146 /* List of functions to call before changing an unmodified buffer. */
147 Lisp_Object Vfirst_change_hook;
149 Lisp_Object Qfirst_change_hook;
150 Lisp_Object Qbefore_change_functions;
151 Lisp_Object Qafter_change_functions;
153 /* If nonzero, all modification hooks are suppressed. */
154 int inhibit_modification_hooks;
156 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
158 Lisp_Object Qprotected_field;
160 Lisp_Object QSFundamental; /* A string "Fundamental" */
162 Lisp_Object Qkill_buffer_hook;
164 Lisp_Object Qget_file_buffer;
166 Lisp_Object Qoverlayp;
168 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
170 Lisp_Object Qmodification_hooks;
171 Lisp_Object Qinsert_in_front_hooks;
172 Lisp_Object Qinsert_behind_hooks;
174 /* For debugging; temporary. See set_buffer_internal. */
175 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
177 void
178 nsberror (spec)
179 Lisp_Object spec;
181 if (STRINGP (spec))
182 error ("No buffer named %s", XSTRING (spec)->data);
183 error ("Invalid buffer argument");
186 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
187 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
188 Value is nil if OBJECT is not a buffer or if it has been killed.")
189 (object)
190 Lisp_Object object;
192 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
193 ? Qt : Qnil);
196 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
197 "Return a list of all existing live buffers.\n\
198 If the optional arg FRAME is a frame, we return that frame's buffer list.")
199 (frame)
200 Lisp_Object frame;
202 Lisp_Object framelist, general;
203 general = Fmapcar (Qcdr, Vbuffer_alist);
205 if (FRAMEP (frame))
207 Lisp_Object tail;
209 CHECK_FRAME (frame, 1);
211 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
213 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
214 tail = framelist;
215 while (! NILP (tail))
217 general = Fdelq (XCAR (tail), general);
218 tail = XCDR (tail);
220 return nconc2 (framelist, general);
223 return general;
226 /* Like Fassoc, but use Fstring_equal to compare
227 (which ignores text properties),
228 and don't ever QUIT. */
230 static Lisp_Object
231 assoc_ignore_text_properties (key, list)
232 register Lisp_Object key;
233 Lisp_Object list;
235 register Lisp_Object tail;
236 for (tail = list; !NILP (tail); tail = Fcdr (tail))
238 register Lisp_Object elt, tem;
239 elt = Fcar (tail);
240 tem = Fstring_equal (Fcar (elt), key);
241 if (!NILP (tem))
242 return elt;
244 return Qnil;
247 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
248 "Return the buffer named NAME (a string).\n\
249 If there is no live buffer named NAME, return nil.\n\
250 NAME may also be a buffer; if so, the value is that buffer.")
251 (name)
252 register Lisp_Object name;
254 if (BUFFERP (name))
255 return name;
256 CHECK_STRING (name, 0);
258 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
261 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
262 "Return the buffer visiting file FILENAME (a string).\n\
263 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
264 If there is no such live buffer, return nil.\n\
265 See also `find-buffer-visiting'.")
266 (filename)
267 register Lisp_Object filename;
269 register Lisp_Object tail, buf, tem;
270 Lisp_Object handler;
272 CHECK_STRING (filename, 0);
273 filename = Fexpand_file_name (filename, Qnil);
275 /* If the file name has special constructs in it,
276 call the corresponding file handler. */
277 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
278 if (!NILP (handler))
279 return call2 (handler, Qget_file_buffer, filename);
281 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
283 buf = Fcdr (XCAR (tail));
284 if (!BUFFERP (buf)) continue;
285 if (!STRINGP (XBUFFER (buf)->filename)) continue;
286 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
287 if (!NILP (tem))
288 return buf;
290 return Qnil;
293 Lisp_Object
294 get_truename_buffer (filename)
295 register Lisp_Object filename;
297 register Lisp_Object tail, buf, tem;
299 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
301 buf = Fcdr (XCAR (tail));
302 if (!BUFFERP (buf)) continue;
303 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
304 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
305 if (!NILP (tem))
306 return buf;
308 return Qnil;
311 /* Incremented for each buffer created, to assign the buffer number. */
312 int buffer_count;
314 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
315 "Return the buffer named NAME, or create such a buffer and return it.\n\
316 A new buffer is created if there is no live buffer named NAME.\n\
317 If NAME starts with a space, the new buffer does not keep undo information.\n\
318 If NAME is a buffer instead of a string, then it is the value returned.\n\
319 The value is never nil.")
320 (name)
321 register Lisp_Object name;
323 register Lisp_Object buf;
324 register struct buffer *b;
326 buf = Fget_buffer (name);
327 if (!NILP (buf))
328 return buf;
330 if (XSTRING (name)->size == 0)
331 error ("Empty string for buffer name is not allowed");
333 b = (struct buffer *) allocate_buffer ();
335 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
337 /* An ordinary buffer uses its own struct buffer_text. */
338 b->text = &b->own_text;
339 b->base_buffer = 0;
341 BUF_GAP_SIZE (b) = 20;
342 BLOCK_INPUT;
343 /* We allocate extra 1-byte at the tail and keep it always '\0' for
344 anchoring a search. */
345 BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
346 UNBLOCK_INPUT;
347 if (! BUF_BEG_ADDR (b))
348 buffer_memory_full ();
350 BUF_PT (b) = 1;
351 BUF_GPT (b) = 1;
352 BUF_BEGV (b) = 1;
353 BUF_ZV (b) = 1;
354 BUF_Z (b) = 1;
355 BUF_PT_BYTE (b) = 1;
356 BUF_GPT_BYTE (b) = 1;
357 BUF_BEGV_BYTE (b) = 1;
358 BUF_ZV_BYTE (b) = 1;
359 BUF_Z_BYTE (b) = 1;
360 BUF_MODIFF (b) = 1;
361 BUF_OVERLAY_MODIFF (b) = 1;
362 BUF_SAVE_MODIFF (b) = 1;
363 BUF_INTERVALS (b) = 0;
364 BUF_UNCHANGED_MODIFIED (b) = 1;
365 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
366 BUF_END_UNCHANGED (b) = 0;
367 BUF_BEG_UNCHANGED (b) = 0;
368 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
370 b->newline_cache = 0;
371 b->width_run_cache = 0;
372 b->width_table = Qnil;
373 b->prevent_redisplay_optimizations_p = 1;
375 /* Put this on the chain of all buffers including killed ones. */
376 b->next = all_buffers;
377 all_buffers = b;
379 /* An ordinary buffer normally doesn't need markers
380 to handle BEGV and ZV. */
381 b->pt_marker = Qnil;
382 b->begv_marker = Qnil;
383 b->zv_marker = Qnil;
385 name = Fcopy_sequence (name);
386 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
387 b->name = name;
389 if (XSTRING (name)->data[0] != ' ')
390 b->undo_list = Qnil;
391 else
392 b->undo_list = Qt;
394 reset_buffer (b);
395 reset_buffer_local_variables (b, 1);
397 /* Put this in the alist of all live buffers. */
398 XSETBUFFER (buf, b);
399 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
401 b->mark = Fmake_marker ();
402 BUF_MARKERS (b) = Qnil;
403 b->name = name;
404 return buf;
407 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
408 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
409 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
410 BASE-BUFFER should be an existing buffer (or buffer name).\n\
411 NAME should be a string which is not the name of an existing buffer.")
412 (base_buffer, name)
413 register Lisp_Object base_buffer, name;
415 register Lisp_Object buf;
416 register struct buffer *b;
418 buf = Fget_buffer (name);
419 if (!NILP (buf))
420 error ("Buffer name `%s' is in use", XSTRING (name)->data);
422 base_buffer = Fget_buffer (base_buffer);
423 if (NILP (base_buffer))
424 error ("No such buffer: `%s'",
425 XSTRING (XBUFFER (base_buffer)->name)->data);
427 if (XSTRING (name)->size == 0)
428 error ("Empty string for buffer name is not allowed");
430 b = (struct buffer *) allocate_buffer ();
431 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
433 if (XBUFFER (base_buffer)->base_buffer)
434 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
435 else
436 b->base_buffer = XBUFFER (base_buffer);
438 /* Use the base buffer's text object. */
439 b->text = b->base_buffer->text;
441 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
442 BUF_ZV (b) = BUF_ZV (b->base_buffer);
443 BUF_PT (b) = BUF_PT (b->base_buffer);
444 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
445 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
446 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
448 b->newline_cache = 0;
449 b->width_run_cache = 0;
450 b->width_table = Qnil;
452 /* Put this on the chain of all buffers including killed ones. */
453 b->next = all_buffers;
454 all_buffers = b;
456 name = Fcopy_sequence (name);
457 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
458 b->name = name;
460 reset_buffer (b);
461 reset_buffer_local_variables (b, 1);
463 /* Put this in the alist of all live buffers. */
464 XSETBUFFER (buf, b);
465 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
467 b->mark = Fmake_marker ();
468 b->name = name;
470 /* The multibyte status belongs to the base buffer. */
471 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
473 /* Make sure the base buffer has markers for its narrowing. */
474 if (NILP (b->base_buffer->pt_marker))
476 b->base_buffer->pt_marker = Fmake_marker ();
477 set_marker_both (b->base_buffer->pt_marker, base_buffer,
478 BUF_PT (b->base_buffer),
479 BUF_PT_BYTE (b->base_buffer));
481 if (NILP (b->base_buffer->begv_marker))
483 b->base_buffer->begv_marker = Fmake_marker ();
484 set_marker_both (b->base_buffer->begv_marker, base_buffer,
485 BUF_BEGV (b->base_buffer),
486 BUF_BEGV_BYTE (b->base_buffer));
488 if (NILP (b->base_buffer->zv_marker))
490 b->base_buffer->zv_marker = Fmake_marker ();
491 set_marker_both (b->base_buffer->zv_marker, base_buffer,
492 BUF_ZV (b->base_buffer),
493 BUF_ZV_BYTE (b->base_buffer));
494 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
497 /* Give the indirect buffer markers for its narrowing. */
498 b->pt_marker = Fmake_marker ();
499 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
500 b->begv_marker = Fmake_marker ();
501 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
502 b->zv_marker = Fmake_marker ();
503 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
504 XMARKER (b->zv_marker)->insertion_type = 1;
506 return buf;
509 /* Reinitialize everything about a buffer except its name and contents
510 and local variables. */
512 void
513 reset_buffer (b)
514 register struct buffer *b;
516 b->filename = Qnil;
517 b->file_truename = Qnil;
518 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
519 b->modtime = 0;
520 XSETFASTINT (b->save_length, 0);
521 b->last_window_start = 1;
522 /* It is more conservative to start out "changed" than "unchanged". */
523 b->clip_changed = 0;
524 b->prevent_redisplay_optimizations_p = 1;
525 b->backed_up = Qnil;
526 b->auto_save_modified = 0;
527 b->auto_save_failure_time = -1;
528 b->auto_save_file_name = Qnil;
529 b->read_only = Qnil;
530 b->overlays_before = Qnil;
531 b->overlays_after = Qnil;
532 XSETFASTINT (b->overlay_center, 1);
533 b->mark_active = Qnil;
534 b->point_before_scroll = Qnil;
535 b->file_format = Qnil;
536 b->last_selected_window = Qnil;
537 XSETINT (b->display_count, 0);
538 b->display_time = Qnil;
539 b->extra2 = Qnil;
540 b->extra3 = Qnil;
541 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
544 /* Reset buffer B's local variables info.
545 Don't use this on a buffer that has already been in use;
546 it does not treat permanent locals consistently.
547 Instead, use Fkill_all_local_variables.
549 If PERMANENT_TOO is 1, then we reset permanent built-in
550 buffer-local variables. If PERMANENT_TOO is 0,
551 we preserve those. */
553 static void
554 reset_buffer_local_variables (b, permanent_too)
555 register struct buffer *b;
556 int permanent_too;
558 register int offset;
559 int dont_reset;
561 /* Decide which built-in local variables to reset. */
562 if (permanent_too)
563 dont_reset = 0;
564 else
565 dont_reset = buffer_permanent_local_flags;
567 /* Reset the major mode to Fundamental, together with all the
568 things that depend on the major mode.
569 default-major-mode is handled at a higher level.
570 We ignore it here. */
571 b->major_mode = Qfundamental_mode;
572 b->keymap = Qnil;
573 b->abbrev_table = Vfundamental_mode_abbrev_table;
574 b->mode_name = QSFundamental;
575 b->minor_modes = Qnil;
577 /* If the standard case table has been altered and invalidated,
578 fix up its insides first. */
579 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
580 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
581 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
582 Fset_standard_case_table (Vascii_downcase_table);
584 b->downcase_table = Vascii_downcase_table;
585 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
586 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
587 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
588 b->invisibility_spec = Qt;
589 #ifndef DOS_NT
590 b->buffer_file_type = Qnil;
591 #endif
593 #if 0
594 b->sort_table = XSTRING (Vascii_sort_table);
595 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
596 #endif /* 0 */
598 /* Reset all (or most) per-buffer variables to their defaults. */
599 b->local_var_alist = Qnil;
600 b->local_var_flags &= dont_reset;
602 /* For each slot that has a default value,
603 copy that into the slot. */
605 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
606 offset < sizeof (struct buffer);
607 offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
609 int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
610 if ((flag > 0
611 /* Don't reset a permanent local. */
612 && ! (dont_reset & flag))
613 || flag == -2)
614 *(Lisp_Object *)(offset + (char *)b)
615 = *(Lisp_Object *)(offset + (char *)&buffer_defaults);
619 /* We split this away from generate-new-buffer, because rename-buffer
620 and set-visited-file-name ought to be able to use this to really
621 rename the buffer properly. */
623 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
624 1, 2, 0,
625 "Return a string that is the name of no existing buffer based on NAME.\n\
626 If there is no live buffer named NAME, then return NAME.\n\
627 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
628 until an unused name is found, and then return that name.\n\
629 Optional second argument IGNORE specifies a name that is okay to use\n\
630 \(if it is in the sequence to be tried)\n\
631 even if a buffer with that name exists.")
632 (name, ignore)
633 register Lisp_Object name, ignore;
635 register Lisp_Object gentemp, tem;
636 int count;
637 char number[10];
639 CHECK_STRING (name, 0);
641 tem = Fget_buffer (name);
642 if (NILP (tem))
643 return name;
645 count = 1;
646 while (1)
648 sprintf (number, "<%d>", ++count);
649 gentemp = concat2 (name, build_string (number));
650 tem = Fstring_equal (gentemp, ignore);
651 if (!NILP (tem))
652 return gentemp;
653 tem = Fget_buffer (gentemp);
654 if (NILP (tem))
655 return gentemp;
660 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
661 "Return the name of BUFFER, as a string.\n\
662 With no argument or nil as argument, return the name of the current buffer.")
663 (buffer)
664 register Lisp_Object buffer;
666 if (NILP (buffer))
667 return current_buffer->name;
668 CHECK_BUFFER (buffer, 0);
669 return XBUFFER (buffer)->name;
672 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
673 "Return name of file BUFFER is visiting, or nil if none.\n\
674 No argument or nil as argument means use the current buffer.")
675 (buffer)
676 register Lisp_Object buffer;
678 if (NILP (buffer))
679 return current_buffer->filename;
680 CHECK_BUFFER (buffer, 0);
681 return XBUFFER (buffer)->filename;
684 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
685 0, 1, 0,
686 "Return the base buffer of indirect buffer BUFFER.\n\
687 If BUFFER is not indirect, return nil.")
688 (buffer)
689 register Lisp_Object buffer;
691 struct buffer *base;
692 Lisp_Object base_buffer;
694 if (NILP (buffer))
695 base = current_buffer->base_buffer;
696 else
698 CHECK_BUFFER (buffer, 0);
699 base = XBUFFER (buffer)->base_buffer;
702 if (! base)
703 return Qnil;
704 XSETBUFFER (base_buffer, base);
705 return base_buffer;
708 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
709 Sbuffer_local_variables, 0, 1, 0,
710 "Return an alist of variables that are buffer-local in BUFFER.\n\
711 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
712 For a symbol that is locally unbound, just the symbol appears in the value.\n\
713 Note that storing new VALUEs in these elements doesn't change the variables.\n\
714 No argument or nil as argument means use current buffer as BUFFER.")
715 (buffer)
716 register Lisp_Object buffer;
718 register struct buffer *buf;
719 register Lisp_Object result;
721 if (NILP (buffer))
722 buf = current_buffer;
723 else
725 CHECK_BUFFER (buffer, 0);
726 buf = XBUFFER (buffer);
729 result = Qnil;
732 register Lisp_Object tail;
733 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
735 Lisp_Object val, elt;
737 elt = XCAR (tail);
739 /* Reference each variable in the alist in buf.
740 If inquiring about the current buffer, this gets the current values,
741 so store them into the alist so the alist is up to date.
742 If inquiring about some other buffer, this swaps out any values
743 for that buffer, making the alist up to date automatically. */
744 val = find_symbol_value (XCAR (elt));
745 /* Use the current buffer value only if buf is the current buffer. */
746 if (buf != current_buffer)
747 val = XCDR (elt);
749 /* If symbol is unbound, put just the symbol in the list. */
750 if (EQ (val, Qunbound))
751 result = Fcons (XCAR (elt), result);
752 /* Otherwise, put (symbol . value) in the list. */
753 else
754 result = Fcons (Fcons (XCAR (elt), val), result);
758 /* Add on all the variables stored in special slots. */
760 register int offset, mask;
762 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
763 offset < sizeof (struct buffer);
764 offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
766 mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
767 if (mask == -1 || (buf->local_var_flags & mask))
768 if (SYMBOLP (*(Lisp_Object *)(offset
769 + (char *)&buffer_local_symbols)))
770 result = Fcons (Fcons (*((Lisp_Object *)
771 (offset + (char *)&buffer_local_symbols)),
772 *(Lisp_Object *)(offset + (char *)buf)),
773 result);
777 return result;
781 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
782 0, 1, 0,
783 "Return t if BUFFER was modified since its file was last read or saved.\n\
784 No argument or nil as argument means use current buffer as BUFFER.")
785 (buffer)
786 register Lisp_Object buffer;
788 register struct buffer *buf;
789 if (NILP (buffer))
790 buf = current_buffer;
791 else
793 CHECK_BUFFER (buffer, 0);
794 buf = XBUFFER (buffer);
797 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
800 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
801 1, 1, 0,
802 "Mark current buffer as modified or unmodified according to FLAG.\n\
803 A non-nil FLAG means mark the buffer modified.")
804 (flag)
805 register Lisp_Object flag;
807 register int already;
808 register Lisp_Object fn;
809 Lisp_Object buffer, window;
811 #ifdef CLASH_DETECTION
812 /* If buffer becoming modified, lock the file.
813 If buffer becoming unmodified, unlock the file. */
815 fn = current_buffer->file_truename;
816 /* Test buffer-file-name so that binding it to nil is effective. */
817 if (!NILP (fn) && ! NILP (current_buffer->filename))
819 already = SAVE_MODIFF < MODIFF;
820 if (!already && !NILP (flag))
821 lock_file (fn);
822 else if (already && NILP (flag))
823 unlock_file (fn);
825 #endif /* CLASH_DETECTION */
827 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
829 /* Set update_mode_lines only if buffer is displayed in some window.
830 Packages like jit-lock or lazy-lock preserve a buffer's modified
831 state by recording/restoring the state around blocks of code.
832 Setting update_mode_lines makes redisplay consider all windows
833 (on all frames). Stealth fontification of buffers not displayed
834 would incur additional redisplay costs if we'd set
835 update_modes_lines unconditionally.
837 Ideally, I think there should be another mechanism for fontifying
838 buffers without "modifying" buffers, or redisplay should be
839 smarter about updating the `*' in mode lines. --gerd */
840 XSETBUFFER (buffer, current_buffer);
841 window = Fget_buffer_window (buffer, Qt);
842 if (WINDOWP (window))
843 update_mode_lines++;
845 return flag;
848 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
849 0, 1, 0,
850 "Return BUFFER's tick counter, incremented for each change in text.\n\
851 Each buffer has a tick counter which is incremented each time the text in\n\
852 that buffer is changed. It wraps around occasionally.\n\
853 No argument or nil as argument means use current buffer as BUFFER.")
854 (buffer)
855 register Lisp_Object buffer;
857 register struct buffer *buf;
858 if (NILP (buffer))
859 buf = current_buffer;
860 else
862 CHECK_BUFFER (buffer, 0);
863 buf = XBUFFER (buffer);
866 return make_number (BUF_MODIFF (buf));
869 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
870 "sRename buffer (to new name): \nP",
871 "Change current buffer's name to NEWNAME (a string).\n\
872 If second arg UNIQUE is nil or omitted, it is an error if a\n\
873 buffer named NEWNAME already exists.\n\
874 If UNIQUE is non-nil, come up with a new name using\n\
875 `generate-new-buffer-name'.\n\
876 Interactively, you can set UNIQUE with a prefix argument.\n\
877 We return the name we actually gave the buffer.\n\
878 This does not change the name of the visited file (if any).")
879 (newname, unique)
880 register Lisp_Object newname, unique;
882 register Lisp_Object tem, buf;
884 CHECK_STRING (newname, 0);
886 if (XSTRING (newname)->size == 0)
887 error ("Empty string is invalid as a buffer name");
889 tem = Fget_buffer (newname);
890 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
891 the buffer automatically so you can create another with the original name.
892 It makes UNIQUE equivalent to
893 (rename-buffer (generate-new-buffer-name NEWNAME)). */
894 if (NILP (unique) && XBUFFER (tem) == current_buffer)
895 return current_buffer->name;
896 if (!NILP (tem))
898 if (!NILP (unique))
899 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
900 else
901 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
904 current_buffer->name = newname;
906 /* Catch redisplay's attention. Unless we do this, the mode lines for
907 any windows displaying current_buffer will stay unchanged. */
908 update_mode_lines++;
910 XSETBUFFER (buf, current_buffer);
911 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
912 if (NILP (current_buffer->filename)
913 && !NILP (current_buffer->auto_save_file_name))
914 call0 (intern ("rename-auto-save-file"));
915 /* Refetch since that last call may have done GC. */
916 return current_buffer->name;
919 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
920 "Return most recently selected buffer other than BUFFER.\n\
921 Buffers not visible in windows are preferred to visible buffers,\n\
922 unless optional second argument VISIBLE-OK is non-nil.\n\
923 If the optional third argument FRAME is non-nil, use that frame's\n\
924 buffer list instead of the selected frame's buffer list.\n\
925 If no other buffer exists, the buffer `*scratch*' is returned.\n\
926 If BUFFER is omitted or nil, some interesting buffer is returned.")
927 (buffer, visible_ok, frame)
928 register Lisp_Object buffer, visible_ok, frame;
930 Lisp_Object Fset_buffer_major_mode ();
931 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
932 notsogood = Qnil;
934 if (NILP (frame))
935 frame = selected_frame;
937 tail = Vbuffer_alist;
938 pred = frame_buffer_predicate (frame);
940 /* Consider buffers that have been seen in the selected frame
941 before other buffers. */
943 tem = frame_buffer_list (frame);
944 add_ons = Qnil;
945 while (CONSP (tem))
947 if (BUFFERP (XCAR (tem)))
948 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
949 tem = XCDR (tem);
951 tail = nconc2 (Fnreverse (add_ons), tail);
953 for (; !NILP (tail); tail = Fcdr (tail))
955 buf = Fcdr (Fcar (tail));
956 if (EQ (buf, buffer))
957 continue;
958 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
959 continue;
960 /* If the selected frame has a buffer_predicate,
961 disregard buffers that don't fit the predicate. */
962 if (!NILP (pred))
964 tem = call1 (pred, buf);
965 if (NILP (tem))
966 continue;
969 if (NILP (visible_ok))
970 tem = Fget_buffer_window (buf, Qt);
971 else
972 tem = Qnil;
973 if (NILP (tem))
974 return buf;
975 if (NILP (notsogood))
976 notsogood = buf;
978 if (!NILP (notsogood))
979 return notsogood;
980 buf = Fget_buffer (build_string ("*scratch*"));
981 if (NILP (buf))
983 buf = Fget_buffer_create (build_string ("*scratch*"));
984 Fset_buffer_major_mode (buf);
986 return buf;
989 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
990 0, 1, "",
991 "Make BUFFER stop keeping undo information.\n\
992 No argument or nil as argument means do this for the current buffer.")
993 (buffer)
994 register Lisp_Object buffer;
996 Lisp_Object real_buffer;
998 if (NILP (buffer))
999 XSETBUFFER (real_buffer, current_buffer);
1000 else
1002 real_buffer = Fget_buffer (buffer);
1003 if (NILP (real_buffer))
1004 nsberror (buffer);
1007 XBUFFER (real_buffer)->undo_list = Qt;
1009 return Qnil;
1012 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1013 0, 1, "",
1014 "Start keeping undo information for buffer BUFFER.\n\
1015 No argument or nil as argument means do this for the current buffer.")
1016 (buffer)
1017 register Lisp_Object buffer;
1019 Lisp_Object real_buffer;
1021 if (NILP (buffer))
1022 XSETBUFFER (real_buffer, current_buffer);
1023 else
1025 real_buffer = Fget_buffer (buffer);
1026 if (NILP (real_buffer))
1027 nsberror (buffer);
1030 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1031 XBUFFER (real_buffer)->undo_list = Qnil;
1033 return Qnil;
1037 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1038 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1039 The buffer being killed will be current while the hook is running.\n\
1040 See `kill-buffer'."
1042 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1043 "Kill the buffer BUFFER.\n\
1044 The argument may be a buffer or may be the name of a buffer.\n\
1045 An argument of nil means kill the current buffer.\n\n\
1046 Value is t if the buffer is actually killed, nil if user says no.\n\n\
1047 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1048 if not void, is a list of functions to be called, with no arguments,\n\
1049 before the buffer is actually killed. The buffer to be killed is current\n\
1050 when the hook functions are called.\n\n\
1051 Any processes that have this buffer as the `process-buffer' are killed\n\
1052 with SIGHUP.")
1053 (buffer)
1054 Lisp_Object buffer;
1056 Lisp_Object buf;
1057 register struct buffer *b;
1058 register Lisp_Object tem;
1059 register struct Lisp_Marker *m;
1060 struct gcpro gcpro1;
1062 if (NILP (buffer))
1063 buf = Fcurrent_buffer ();
1064 else
1065 buf = Fget_buffer (buffer);
1066 if (NILP (buf))
1067 nsberror (buffer);
1069 b = XBUFFER (buf);
1071 /* Avoid trouble for buffer already dead. */
1072 if (NILP (b->name))
1073 return Qnil;
1075 /* Query if the buffer is still modified. */
1076 if (INTERACTIVE && !NILP (b->filename)
1077 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1079 GCPRO1 (buf);
1080 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1081 XSTRING (b->name)->data));
1082 UNGCPRO;
1083 if (NILP (tem))
1084 return Qnil;
1087 /* Run hooks with the buffer to be killed the current buffer. */
1089 int count = specpdl_ptr - specpdl;
1090 Lisp_Object list;
1092 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1093 set_buffer_internal (b);
1095 /* First run the query functions; if any query is answered no,
1096 don't kill the buffer. */
1097 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1099 tem = call0 (Fcar (list));
1100 if (NILP (tem))
1101 return unbind_to (count, Qnil);
1104 /* Then run the hooks. */
1105 if (!NILP (Vrun_hooks))
1106 call1 (Vrun_hooks, Qkill_buffer_hook);
1107 unbind_to (count, Qnil);
1110 /* We have no more questions to ask. Verify that it is valid
1111 to kill the buffer. This must be done after the questions
1112 since anything can happen within do_yes_or_no_p. */
1114 /* Don't kill the minibuffer now current. */
1115 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1116 return Qnil;
1118 if (NILP (b->name))
1119 return Qnil;
1121 /* When we kill a base buffer, kill all its indirect buffers.
1122 We do it at this stage so nothing terrible happens if they
1123 ask questions or their hooks get errors. */
1124 if (! b->base_buffer)
1126 struct buffer *other;
1128 GCPRO1 (buf);
1130 for (other = all_buffers; other; other = other->next)
1131 /* all_buffers contains dead buffers too;
1132 don't re-kill them. */
1133 if (other->base_buffer == b && !NILP (other->name))
1135 Lisp_Object buf;
1136 XSETBUFFER (buf, other);
1137 Fkill_buffer (buf);
1140 UNGCPRO;
1143 /* Make this buffer not be current.
1144 In the process, notice if this is the sole visible buffer
1145 and give up if so. */
1146 if (b == current_buffer)
1148 tem = Fother_buffer (buf, Qnil, Qnil);
1149 Fset_buffer (tem);
1150 if (b == current_buffer)
1151 return Qnil;
1154 /* Now there is no question: we can kill the buffer. */
1156 #ifdef CLASH_DETECTION
1157 /* Unlock this buffer's file, if it is locked. */
1158 unlock_buffer (b);
1159 #endif /* CLASH_DETECTION */
1161 kill_buffer_processes (buf);
1163 tem = Vinhibit_quit;
1164 Vinhibit_quit = Qt;
1165 replace_buffer_in_all_windows (buf);
1166 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1167 frames_discard_buffer (buf);
1168 Vinhibit_quit = tem;
1170 /* Delete any auto-save file, if we saved it in this session. */
1171 if (STRINGP (b->auto_save_file_name)
1172 && b->auto_save_modified != 0
1173 && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
1175 Lisp_Object tem;
1176 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1177 if (! NILP (tem))
1178 internal_delete_file (b->auto_save_file_name);
1181 if (b->base_buffer)
1183 /* Unchain all markers that belong to this indirect buffer.
1184 Don't unchain the markers that belong to the base buffer
1185 or its other indirect buffers. */
1186 for (tem = BUF_MARKERS (b); !NILP (tem); )
1188 Lisp_Object next;
1189 m = XMARKER (tem);
1190 next = m->chain;
1191 if (m->buffer == b)
1192 unchain_marker (tem);
1193 tem = next;
1196 else
1198 /* Unchain all markers of this buffer and its indirect buffers.
1199 and leave them pointing nowhere. */
1200 for (tem = BUF_MARKERS (b); !NILP (tem); )
1202 m = XMARKER (tem);
1203 m->buffer = 0;
1204 tem = m->chain;
1205 m->chain = Qnil;
1207 BUF_MARKERS (b) = Qnil;
1208 BUF_INTERVALS (b) = NULL_INTERVAL;
1210 /* Perhaps we should explicitly free the interval tree here... */
1213 /* Reset the local variables, so that this buffer's local values
1214 won't be protected from GC. They would be protected
1215 if they happened to remain encached in their symbols.
1216 This gets rid of them for certain. */
1217 swap_out_buffer_local_variables (b);
1218 reset_buffer_local_variables (b, 1);
1220 b->name = Qnil;
1222 BLOCK_INPUT;
1223 if (! b->base_buffer)
1224 BUFFER_FREE (BUF_BEG_ADDR (b));
1226 if (b->newline_cache)
1228 free_region_cache (b->newline_cache);
1229 b->newline_cache = 0;
1231 if (b->width_run_cache)
1233 free_region_cache (b->width_run_cache);
1234 b->width_run_cache = 0;
1236 b->width_table = Qnil;
1237 UNBLOCK_INPUT;
1238 b->undo_list = Qnil;
1240 return Qt;
1243 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1244 we do this each time BUF is selected visibly, the more recently
1245 selected buffers are always closer to the front of the list. This
1246 means that other_buffer is more likely to choose a relevant buffer. */
1248 void
1249 record_buffer (buf)
1250 Lisp_Object buf;
1252 register Lisp_Object link, prev;
1253 Lisp_Object frame;
1254 frame = selected_frame;
1256 prev = Qnil;
1257 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1259 if (EQ (XCDR (XCAR (link)), buf))
1260 break;
1261 prev = link;
1264 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1265 we cannot use Fdelq itself here because it allows quitting. */
1267 if (NILP (prev))
1268 Vbuffer_alist = XCDR (Vbuffer_alist);
1269 else
1270 XCDR (prev) = XCDR (XCDR (prev));
1272 XCDR (link) = Vbuffer_alist;
1273 Vbuffer_alist = link;
1275 /* Now move this buffer to the front of frame_buffer_list also. */
1277 prev = Qnil;
1278 for (link = frame_buffer_list (frame); CONSP (link);
1279 link = XCDR (link))
1281 if (EQ (XCAR (link), buf))
1282 break;
1283 prev = link;
1286 /* Effectively do delq. */
1288 if (CONSP (link))
1290 if (NILP (prev))
1291 set_frame_buffer_list (frame,
1292 XCDR (frame_buffer_list (frame)));
1293 else
1294 XCDR (prev) = XCDR (XCDR (prev));
1296 XCDR (link) = frame_buffer_list (frame);
1297 set_frame_buffer_list (frame, link);
1299 else
1300 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1303 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1304 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1305 Use this function before selecting the buffer, since it may need to inspect\n\
1306 the current buffer's major mode.")
1307 (buffer)
1308 Lisp_Object buffer;
1310 int count;
1311 Lisp_Object function;
1313 function = buffer_defaults.major_mode;
1314 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1315 function = current_buffer->major_mode;
1317 if (NILP (function) || EQ (function, Qfundamental_mode))
1318 return Qnil;
1320 count = specpdl_ptr - specpdl;
1322 /* To select a nonfundamental mode,
1323 select the buffer temporarily and then call the mode function. */
1325 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1327 Fset_buffer (buffer);
1328 call0 (function);
1330 return unbind_to (count, Qnil);
1333 /* If switching buffers in WINDOW would be an error, return
1334 a C string saying what the error would be. */
1336 char *
1337 no_switch_window (window)
1338 Lisp_Object window;
1340 Lisp_Object tem;
1341 if (EQ (minibuf_window, window))
1342 return "Cannot switch buffers in minibuffer window";
1343 tem = Fwindow_dedicated_p (window);
1344 if (!NILP (tem))
1345 return "Cannot switch buffers in a dedicated window";
1346 return NULL;
1349 /* Switch to buffer BUFFER in the selected window.
1350 If NORECORD is non-nil, don't call record_buffer. */
1352 Lisp_Object
1353 switch_to_buffer_1 (buffer, norecord)
1354 Lisp_Object buffer, norecord;
1356 register Lisp_Object buf;
1358 if (NILP (buffer))
1359 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1360 else
1362 buf = Fget_buffer (buffer);
1363 if (NILP (buf))
1365 buf = Fget_buffer_create (buffer);
1366 Fset_buffer_major_mode (buf);
1369 Fset_buffer (buf);
1370 if (NILP (norecord))
1371 record_buffer (buf);
1373 Fset_window_buffer (EQ (selected_window, minibuf_window)
1374 ? Fnext_window (minibuf_window, Qnil, Qnil)
1375 : selected_window,
1376 buf);
1378 return buf;
1381 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1382 "Select buffer BUFFER in the current window.\n\
1383 BUFFER may be a buffer or a buffer name.\n\
1384 Optional second arg NORECORD non-nil means\n\
1385 do not put this buffer at the front of the list of recently selected ones.\n\
1387 WARNING: This is NOT the way to work on another buffer temporarily\n\
1388 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1389 the window-buffer correspondences.")
1390 (buffer, norecord)
1391 Lisp_Object buffer, norecord;
1393 char *err;
1395 err = no_switch_window (selected_window);
1396 if (err) error (err);
1398 return switch_to_buffer_1 (buffer, norecord);
1401 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1402 "Select buffer BUFFER in some window, preferably a different one.\n\
1403 If BUFFER is nil, then some other buffer is chosen.\n\
1404 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1405 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1406 window even if BUFFER is already visible in the selected window.\n\
1407 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1408 of `display-buffer' for additional customization information.\n\
1410 Optional third arg NORECORD non-nil means\n\
1411 do not put this buffer at the front of the list of recently selected ones.")
1412 (buffer, other_window, norecord)
1413 Lisp_Object buffer, other_window, norecord;
1415 register Lisp_Object buf;
1416 if (NILP (buffer))
1417 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1418 else
1420 buf = Fget_buffer (buffer);
1421 if (NILP (buf))
1423 buf = Fget_buffer_create (buffer);
1424 Fset_buffer_major_mode (buf);
1427 Fset_buffer (buf);
1428 if (NILP (norecord))
1429 record_buffer (buf);
1430 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
1431 return buf;
1434 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1435 "Return the current buffer as a Lisp object.")
1438 register Lisp_Object buf;
1439 XSETBUFFER (buf, current_buffer);
1440 return buf;
1443 /* Set the current buffer to B.
1445 We previously set windows_or_buffers_changed here to invalidate
1446 global unchanged information in beg_unchanged and end_unchanged.
1447 This is no longer necessary because we now compute unchanged
1448 information on a buffer-basis. Every action affecting other
1449 windows than the selected one requires a select_window at some
1450 time, and that increments windows_or_buffers_changed. */
1452 void
1453 set_buffer_internal (b)
1454 register struct buffer *b;
1456 if (current_buffer != b)
1457 set_buffer_internal_1 (b);
1460 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1461 This is used by redisplay. */
1463 void
1464 set_buffer_internal_1 (b)
1465 register struct buffer *b;
1467 register struct buffer *old_buf;
1468 register Lisp_Object tail, valcontents;
1469 Lisp_Object tem;
1471 if (current_buffer == b)
1472 return;
1474 old_buf = current_buffer;
1475 current_buffer = b;
1476 last_known_column_point = -1; /* invalidate indentation cache */
1478 if (old_buf)
1480 /* Put the undo list back in the base buffer, so that it appears
1481 that an indirect buffer shares the undo list of its base. */
1482 if (old_buf->base_buffer)
1483 old_buf->base_buffer->undo_list = old_buf->undo_list;
1485 /* If the old current buffer has markers to record PT, BEGV and ZV
1486 when it is not current, update them now. */
1487 if (! NILP (old_buf->pt_marker))
1489 Lisp_Object obuf;
1490 XSETBUFFER (obuf, old_buf);
1491 set_marker_both (old_buf->pt_marker, obuf,
1492 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1494 if (! NILP (old_buf->begv_marker))
1496 Lisp_Object obuf;
1497 XSETBUFFER (obuf, old_buf);
1498 set_marker_both (old_buf->begv_marker, obuf,
1499 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1501 if (! NILP (old_buf->zv_marker))
1503 Lisp_Object obuf;
1504 XSETBUFFER (obuf, old_buf);
1505 set_marker_both (old_buf->zv_marker, obuf,
1506 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1510 /* Get the undo list from the base buffer, so that it appears
1511 that an indirect buffer shares the undo list of its base. */
1512 if (b->base_buffer)
1513 b->undo_list = b->base_buffer->undo_list;
1515 /* If the new current buffer has markers to record PT, BEGV and ZV
1516 when it is not current, fetch them now. */
1517 if (! NILP (b->pt_marker))
1519 BUF_PT (b) = marker_position (b->pt_marker);
1520 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1522 if (! NILP (b->begv_marker))
1524 BUF_BEGV (b) = marker_position (b->begv_marker);
1525 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1527 if (! NILP (b->zv_marker))
1529 BUF_ZV (b) = marker_position (b->zv_marker);
1530 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1533 /* Look down buffer's list of local Lisp variables
1534 to find and update any that forward into C variables. */
1536 for (tail = b->local_var_alist; !NILP (tail); tail = XCDR (tail))
1538 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1539 if ((BUFFER_LOCAL_VALUEP (valcontents)
1540 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1541 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1542 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1543 /* Just reference the variable
1544 to cause it to become set for this buffer. */
1545 Fsymbol_value (XCAR (XCAR (tail)));
1548 /* Do the same with any others that were local to the previous buffer */
1550 if (old_buf)
1551 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCDR (tail))
1553 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1554 if ((BUFFER_LOCAL_VALUEP (valcontents)
1555 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1556 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1557 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1558 /* Just reference the variable
1559 to cause it to become set for this buffer. */
1560 Fsymbol_value (XCAR (XCAR (tail)));
1564 /* Switch to buffer B temporarily for redisplay purposes.
1565 This avoids certain things that don't need to be done within redisplay. */
1567 void
1568 set_buffer_temp (b)
1569 struct buffer *b;
1571 register struct buffer *old_buf;
1573 if (current_buffer == b)
1574 return;
1576 old_buf = current_buffer;
1577 current_buffer = b;
1579 if (old_buf)
1581 /* If the old current buffer has markers to record PT, BEGV and ZV
1582 when it is not current, update them now. */
1583 if (! NILP (old_buf->pt_marker))
1585 Lisp_Object obuf;
1586 XSETBUFFER (obuf, old_buf);
1587 set_marker_both (old_buf->pt_marker, obuf,
1588 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1590 if (! NILP (old_buf->begv_marker))
1592 Lisp_Object obuf;
1593 XSETBUFFER (obuf, old_buf);
1594 set_marker_both (old_buf->begv_marker, obuf,
1595 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1597 if (! NILP (old_buf->zv_marker))
1599 Lisp_Object obuf;
1600 XSETBUFFER (obuf, old_buf);
1601 set_marker_both (old_buf->zv_marker, obuf,
1602 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1606 /* If the new current buffer has markers to record PT, BEGV and ZV
1607 when it is not current, fetch them now. */
1608 if (! NILP (b->pt_marker))
1610 BUF_PT (b) = marker_position (b->pt_marker);
1611 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1613 if (! NILP (b->begv_marker))
1615 BUF_BEGV (b) = marker_position (b->begv_marker);
1616 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1618 if (! NILP (b->zv_marker))
1620 BUF_ZV (b) = marker_position (b->zv_marker);
1621 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1625 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1626 "Make the buffer BUFFER current for editing operations.\n\
1627 BUFFER may be a buffer or the name of an existing buffer.\n\
1628 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1629 This function does not display the buffer, so its effect ends\n\
1630 when the current command terminates.\n\
1631 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1632 (buffer)
1633 register Lisp_Object buffer;
1635 register Lisp_Object buf;
1636 buf = Fget_buffer (buffer);
1637 if (NILP (buf))
1638 nsberror (buffer);
1639 if (NILP (XBUFFER (buf)->name))
1640 error ("Selecting deleted buffer");
1641 set_buffer_internal (XBUFFER (buf));
1642 return buf;
1645 /* Set the current buffer to BUFFER provided it is alive. */
1647 Lisp_Object
1648 set_buffer_if_live (buffer)
1649 Lisp_Object buffer;
1651 if (! NILP (XBUFFER (buffer)->name))
1652 Fset_buffer (buffer);
1653 return Qnil;
1656 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1657 Sbarf_if_buffer_read_only, 0, 0, 0,
1658 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1661 if (!NILP (current_buffer->read_only)
1662 && NILP (Vinhibit_read_only))
1663 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1664 return Qnil;
1667 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1668 "Put BUFFER at the end of the list of all buffers.\n\
1669 There it is the least likely candidate for `other-buffer' to return;\n\
1670 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1671 If BUFFER is nil or omitted, bury the current buffer.\n\
1672 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1673 selected window if it is displayed there.")
1674 (buffer)
1675 register Lisp_Object buffer;
1677 /* Figure out what buffer we're going to bury. */
1678 if (NILP (buffer))
1680 XSETBUFFER (buffer, current_buffer);
1682 /* If we're burying the current buffer, unshow it. */
1683 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1685 else
1687 Lisp_Object buf1;
1689 buf1 = Fget_buffer (buffer);
1690 if (NILP (buf1))
1691 nsberror (buffer);
1692 buffer = buf1;
1695 /* Move buffer to the end of the buffer list. */
1697 register Lisp_Object aelt, link;
1699 aelt = Frassq (buffer, Vbuffer_alist);
1700 link = Fmemq (aelt, Vbuffer_alist);
1701 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1702 XCDR (link) = Qnil;
1703 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1706 frames_bury_buffer (buffer);
1708 return Qnil;
1711 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1712 "Delete the entire contents of the current buffer.\n\
1713 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1714 so the buffer is truly empty after this.")
1717 Fwiden ();
1719 del_range (BEG, Z);
1721 current_buffer->last_window_start = 1;
1722 /* Prevent warnings, or suspension of auto saving, that would happen
1723 if future size is less than past size. Use of erase-buffer
1724 implies that the future text is not really related to the past text. */
1725 XSETFASTINT (current_buffer->save_length, 0);
1726 return Qnil;
1729 void
1730 validate_region (b, e)
1731 register Lisp_Object *b, *e;
1733 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1734 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1736 if (XINT (*b) > XINT (*e))
1738 Lisp_Object tem;
1739 tem = *b; *b = *e; *e = tem;
1742 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1743 && XINT (*e) <= ZV))
1744 args_out_of_range (*b, *e);
1747 /* Advance BYTE_POS up to a character boundary
1748 and return the adjusted position. */
1750 static int
1751 advance_to_char_boundary (byte_pos)
1752 int byte_pos;
1754 int c;
1756 if (byte_pos == BEG)
1757 /* Beginning of buffer is always a character boundary. */
1758 return 1;
1760 c = FETCH_BYTE (byte_pos);
1761 if (! CHAR_HEAD_P (c))
1763 /* We should advance BYTE_POS only when C is a constituent of a
1764 multibyte sequence. */
1765 DEC_POS (byte_pos);
1766 INC_POS (byte_pos);
1767 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1768 surely advance to the correct character boundary. If C is
1769 not, BYTE_POS was unchanged. */
1772 return byte_pos;
1775 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1776 1, 1, 0,
1777 "Set the multibyte flag of the current buffer to FLAG.\n\
1778 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1779 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1780 The buffer contents remain unchanged as a sequence of bytes\n\
1781 but the contents viewed as characters do change.")
1782 (flag)
1783 Lisp_Object flag;
1785 Lisp_Object tail, markers;
1786 struct buffer *other;
1788 if (current_buffer->base_buffer)
1789 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
1791 /* Do nothing if nothing actually changes. */
1792 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
1793 return flag;
1795 /* It would be better to update the list,
1796 but this is good enough for now. */
1797 if (! EQ (current_buffer->undo_list, Qt))
1798 current_buffer->undo_list = Qnil;
1800 /* If the cached position is for this buffer, clear it out. */
1801 clear_charpos_cache (current_buffer);
1803 if (NILP (flag))
1805 /* Do this first, so it can use CHAR_TO_BYTE
1806 to calculate the old correspondences. */
1807 set_intervals_multibyte (0);
1809 current_buffer->enable_multibyte_characters = Qnil;
1811 Z = Z_BYTE;
1812 BEGV = BEGV_BYTE;
1813 ZV = ZV_BYTE;
1814 GPT = GPT_BYTE;
1815 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
1817 tail = BUF_MARKERS (current_buffer);
1818 while (XSYMBOL (tail) != XSYMBOL (Qnil))
1820 XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
1821 tail = XMARKER (tail)->chain;
1824 else
1826 /* Be sure not to have a multibyte sequence striding over the GAP.
1827 Ex: We change this: "...abc\201\241\241 _GAP_ \241\241\241..."
1828 to: "...abc _GAP_ \201\241\241\241\241\241..." */
1830 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
1831 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
1833 unsigned char *p = GPT_ADDR - 1;
1835 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
1836 if (BASE_LEADING_CODE_P (*p))
1838 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
1840 move_gap_both (new_gpt, new_gpt);
1844 /* Do this first, so that chars_in_text asks the right question.
1845 set_intervals_multibyte needs it too. */
1846 current_buffer->enable_multibyte_characters = Qt;
1848 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
1849 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
1851 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
1853 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
1854 if (BEGV_BYTE > GPT_BYTE)
1855 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
1856 else
1857 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
1859 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
1860 if (ZV_BYTE > GPT_BYTE)
1861 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
1862 else
1863 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
1866 int pt_byte = advance_to_char_boundary (PT_BYTE);
1867 int pt;
1869 if (pt_byte > GPT_BYTE)
1870 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
1871 else
1872 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
1873 TEMP_SET_PT_BOTH (pt, pt_byte);
1876 tail = markers = BUF_MARKERS (current_buffer);
1878 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
1879 getting confused by the markers that have not yet been updated.
1880 It is also a signal that it should never create a marker. */
1881 BUF_MARKERS (current_buffer) = Qnil;
1883 while (XSYMBOL (tail) != XSYMBOL (Qnil))
1885 XMARKER (tail)->bytepos
1886 = advance_to_char_boundary (XMARKER (tail)->bytepos);
1887 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
1889 tail = XMARKER (tail)->chain;
1892 /* Make sure no markers were put on the chain
1893 while the chain value was incorrect. */
1894 if (! EQ (BUF_MARKERS (current_buffer), Qnil))
1895 abort ();
1897 BUF_MARKERS (current_buffer) = markers;
1899 /* Do this last, so it can calculate the new correspondences
1900 between chars and bytes. */
1901 set_intervals_multibyte (1);
1904 /* Changing the multibyteness of a buffer means that all windows
1905 showing that buffer must be updated thoroughly. */
1906 current_buffer->prevent_redisplay_optimizations_p = 1;
1907 ++windows_or_buffers_changed;
1909 /* Copy this buffer's new multibyte status
1910 into all of its indirect buffers. */
1911 for (other = all_buffers; other; other = other->next)
1912 if (other->base_buffer == current_buffer && !NILP (other->name))
1914 other->enable_multibyte_characters
1915 = current_buffer->enable_multibyte_characters;
1916 other->prevent_redisplay_optimizations_p = 1;
1919 return flag;
1922 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1923 0, 0, 0,
1924 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1925 Most local variable bindings are eliminated so that the default values\n\
1926 become effective once more. Also, the syntax table is set from\n\
1927 `standard-syntax-table', the local keymap is set to nil,\n\
1928 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1929 This function also forces redisplay of the mode line.\n\
1931 Every function to select a new major mode starts by\n\
1932 calling this function.\n\n\
1933 As a special exception, local variables whose names have\n\
1934 a non-nil `permanent-local' property are not eliminated by this function.\n\
1936 The first thing this function does is run\n\
1937 the normal hook `change-major-mode-hook'.")
1940 register Lisp_Object alist, sym, tem;
1941 Lisp_Object oalist;
1943 if (!NILP (Vrun_hooks))
1944 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1945 oalist = current_buffer->local_var_alist;
1947 /* Make sure none of the bindings in oalist
1948 remain swapped in, in their symbols. */
1950 swap_out_buffer_local_variables (current_buffer);
1952 /* Actually eliminate all local bindings of this buffer. */
1954 reset_buffer_local_variables (current_buffer, 0);
1956 /* Redisplay mode lines; we are changing major mode. */
1958 update_mode_lines++;
1960 /* Any which are supposed to be permanent,
1961 make local again, with the same values they had. */
1963 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
1965 sym = XCAR (XCAR (alist));
1966 tem = Fget (sym, Qpermanent_local);
1967 if (! NILP (tem))
1969 Fmake_local_variable (sym);
1970 Fset (sym, XCDR (XCAR (alist)));
1974 /* Force mode-line redisplay. Useful here because all major mode
1975 commands call this function. */
1976 update_mode_lines++;
1978 return Qnil;
1981 /* Make sure no local variables remain set up with buffer B
1982 for their current values. */
1984 static void
1985 swap_out_buffer_local_variables (b)
1986 struct buffer *b;
1988 Lisp_Object oalist, alist, sym, tem, buffer;
1990 XSETBUFFER (buffer, b);
1991 oalist = b->local_var_alist;
1993 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
1995 sym = XCAR (XCAR (alist));
1997 /* Need not do anything if some other buffer's binding is now encached. */
1998 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
1999 if (XBUFFER (tem) == current_buffer)
2001 /* Symbol is set up for this buffer's old local value.
2002 Set it up for the current buffer with the default value. */
2004 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr;
2005 /* Store the symbol's current value into the alist entry
2006 it is currently set up for. This is so that, if the
2007 local is marked permanent, and we make it local again
2008 later in Fkill_all_local_variables, we don't lose the value. */
2009 XCDR (XCAR (tem))
2010 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
2011 /* Switch to the symbol's default-value alist entry. */
2012 XCAR (tem) = tem;
2013 /* Mark it as current for buffer B. */
2014 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
2015 /* Store the current value into any forwarding in the symbol. */
2016 store_symval_forwarding (sym,
2017 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
2018 XCDR (tem));
2023 /* Find all the overlays in the current buffer that contain position POS.
2024 Return the number found, and store them in a vector in *VEC_PTR.
2025 Store in *LEN_PTR the size allocated for the vector.
2026 Store in *NEXT_PTR the next position after POS where an overlay starts,
2027 or ZV if there are no more overlays.
2028 Store in *PREV_PTR the previous position before POS where an overlay ends,
2029 or where an overlay starts which ends at or after POS;
2030 or BEGV if there are no such overlays.
2031 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2033 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2034 when this function is called.
2036 If EXTEND is non-zero, we make the vector bigger if necessary.
2037 If EXTEND is zero, we never extend the vector,
2038 and we store only as many overlays as will fit.
2039 But we still return the total number of overlays. */
2042 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2043 int pos;
2044 int extend;
2045 Lisp_Object **vec_ptr;
2046 int *len_ptr;
2047 int *next_ptr;
2048 int *prev_ptr;
2050 Lisp_Object tail, overlay, start, end;
2051 int idx = 0;
2052 int len = *len_ptr;
2053 Lisp_Object *vec = *vec_ptr;
2054 int next = ZV;
2055 int prev = BEGV;
2056 int inhibit_storing = 0;
2058 for (tail = current_buffer->overlays_before;
2059 GC_CONSP (tail);
2060 tail = XCDR (tail))
2062 int startpos, endpos;
2064 overlay = XCAR (tail);
2066 start = OVERLAY_START (overlay);
2067 end = OVERLAY_END (overlay);
2068 endpos = OVERLAY_POSITION (end);
2069 if (endpos < pos)
2071 if (prev < endpos)
2072 prev = endpos;
2073 break;
2075 startpos = OVERLAY_POSITION (start);
2076 /* This one ends at or after POS
2077 so its start counts for PREV_PTR if it's before POS. */
2078 if (prev < startpos && startpos < pos)
2079 prev = startpos;
2080 if (endpos == pos)
2081 continue;
2082 if (startpos <= pos)
2084 if (idx == len)
2086 /* The supplied vector is full.
2087 Either make it bigger, or don't store any more in it. */
2088 if (extend)
2090 /* Make it work with an initial len == 0. */
2091 len *= 2;
2092 if (len == 0)
2093 len = 4;
2094 *len_ptr = len;
2095 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2096 *vec_ptr = vec;
2098 else
2099 inhibit_storing = 1;
2102 if (!inhibit_storing)
2103 vec[idx] = overlay;
2104 /* Keep counting overlays even if we can't return them all. */
2105 idx++;
2107 else if (startpos < next)
2108 next = startpos;
2111 for (tail = current_buffer->overlays_after;
2112 GC_CONSP (tail);
2113 tail = XCDR (tail))
2115 int startpos, endpos;
2117 overlay = XCAR (tail);
2119 start = OVERLAY_START (overlay);
2120 end = OVERLAY_END (overlay);
2121 startpos = OVERLAY_POSITION (start);
2122 if (pos < startpos)
2124 if (startpos < next)
2125 next = startpos;
2126 break;
2128 endpos = OVERLAY_POSITION (end);
2129 if (pos < endpos)
2131 if (idx == len)
2133 if (extend)
2135 *len_ptr = len *= 2;
2136 if (len == 0)
2137 len = *len_ptr = 4;
2138 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2139 *vec_ptr = vec;
2141 else
2142 inhibit_storing = 1;
2145 if (!inhibit_storing)
2146 vec[idx] = overlay;
2147 idx++;
2149 if (startpos < pos && startpos > prev)
2150 prev = startpos;
2152 else if (endpos < pos && endpos > prev)
2153 prev = endpos;
2154 else if (endpos == pos && startpos > prev)
2155 prev = startpos;
2158 if (next_ptr)
2159 *next_ptr = next;
2160 if (prev_ptr)
2161 *prev_ptr = prev;
2162 return idx;
2165 /* Find all the overlays in the current buffer that overlap the range BEG-END
2166 or are empty at BEG.
2168 Return the number found, and store them in a vector in *VEC_PTR.
2169 Store in *LEN_PTR the size allocated for the vector.
2170 Store in *NEXT_PTR the next position after POS where an overlay starts,
2171 or ZV if there are no more overlays.
2172 Store in *PREV_PTR the previous position before POS where an overlay ends,
2173 or BEGV if there are no previous overlays.
2174 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2176 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2177 when this function is called.
2179 If EXTEND is non-zero, we make the vector bigger if necessary.
2180 If EXTEND is zero, we never extend the vector,
2181 and we store only as many overlays as will fit.
2182 But we still return the total number of overlays. */
2185 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2186 int beg, end;
2187 int extend;
2188 Lisp_Object **vec_ptr;
2189 int *len_ptr;
2190 int *next_ptr;
2191 int *prev_ptr;
2193 Lisp_Object tail, overlay, ostart, oend;
2194 int idx = 0;
2195 int len = *len_ptr;
2196 Lisp_Object *vec = *vec_ptr;
2197 int next = ZV;
2198 int prev = BEGV;
2199 int inhibit_storing = 0;
2201 for (tail = current_buffer->overlays_before;
2202 GC_CONSP (tail);
2203 tail = XCDR (tail))
2205 int startpos, endpos;
2207 overlay = XCAR (tail);
2209 ostart = OVERLAY_START (overlay);
2210 oend = OVERLAY_END (overlay);
2211 endpos = OVERLAY_POSITION (oend);
2212 if (endpos < beg)
2214 if (prev < endpos)
2215 prev = endpos;
2216 break;
2218 startpos = OVERLAY_POSITION (ostart);
2219 /* Count an interval if it either overlaps the range
2220 or is empty at the start of the range. */
2221 if ((beg < endpos && startpos < end)
2222 || (startpos == endpos && beg == endpos))
2224 if (idx == len)
2226 /* The supplied vector is full.
2227 Either make it bigger, or don't store any more in it. */
2228 if (extend)
2230 *len_ptr = len *= 2;
2231 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2232 *vec_ptr = vec;
2234 else
2235 inhibit_storing = 1;
2238 if (!inhibit_storing)
2239 vec[idx] = overlay;
2240 /* Keep counting overlays even if we can't return them all. */
2241 idx++;
2243 else if (startpos < next)
2244 next = startpos;
2247 for (tail = current_buffer->overlays_after;
2248 GC_CONSP (tail);
2249 tail = XCDR (tail))
2251 int startpos, endpos;
2253 overlay = XCAR (tail);
2255 ostart = OVERLAY_START (overlay);
2256 oend = OVERLAY_END (overlay);
2257 startpos = OVERLAY_POSITION (ostart);
2258 if (end < startpos)
2260 if (startpos < next)
2261 next = startpos;
2262 break;
2264 endpos = OVERLAY_POSITION (oend);
2265 /* Count an interval if it either overlaps the range
2266 or is empty at the start of the range. */
2267 if ((beg < endpos && startpos < end)
2268 || (startpos == endpos && beg == endpos))
2270 if (idx == len)
2272 if (extend)
2274 *len_ptr = len *= 2;
2275 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2276 *vec_ptr = vec;
2278 else
2279 inhibit_storing = 1;
2282 if (!inhibit_storing)
2283 vec[idx] = overlay;
2284 idx++;
2286 else if (endpos < beg && endpos > prev)
2287 prev = endpos;
2290 if (next_ptr)
2291 *next_ptr = next;
2292 if (prev_ptr)
2293 *prev_ptr = prev;
2294 return idx;
2297 /* Fast function to just test if we're at an overlay boundary. */
2299 overlay_touches_p (pos)
2300 int pos;
2302 Lisp_Object tail, overlay;
2304 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
2305 tail = XCDR (tail))
2307 int endpos;
2309 overlay = XCAR (tail);
2310 if (!GC_OVERLAYP (overlay))
2311 abort ();
2313 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2314 if (endpos < pos)
2315 break;
2316 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2317 return 1;
2320 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2321 tail = XCDR (tail))
2323 int startpos;
2325 overlay = XCAR (tail);
2326 if (!GC_OVERLAYP (overlay))
2327 abort ();
2329 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2330 if (pos < startpos)
2331 break;
2332 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2333 return 1;
2335 return 0;
2338 struct sortvec
2340 Lisp_Object overlay;
2341 int beg, end;
2342 int priority;
2345 static int
2346 compare_overlays (v1, v2)
2347 const void *v1, *v2;
2349 const struct sortvec *s1 = (const struct sortvec *) v1;
2350 const struct sortvec *s2 = (const struct sortvec *) v2;
2351 if (s1->priority != s2->priority)
2352 return s1->priority - s2->priority;
2353 if (s1->beg != s2->beg)
2354 return s1->beg - s2->beg;
2355 if (s1->end != s2->end)
2356 return s2->end - s1->end;
2357 return 0;
2360 /* Sort an array of overlays by priority. The array is modified in place.
2361 The return value is the new size; this may be smaller than the original
2362 size if some of the overlays were invalid or were window-specific. */
2364 sort_overlays (overlay_vec, noverlays, w)
2365 Lisp_Object *overlay_vec;
2366 int noverlays;
2367 struct window *w;
2369 int i, j;
2370 struct sortvec *sortvec;
2371 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2373 /* Put the valid and relevant overlays into sortvec. */
2375 for (i = 0, j = 0; i < noverlays; i++)
2377 Lisp_Object tem;
2378 Lisp_Object overlay;
2380 overlay = overlay_vec[i];
2381 if (OVERLAY_VALID (overlay)
2382 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2383 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2385 /* If we're interested in a specific window, then ignore
2386 overlays that are limited to some other window. */
2387 if (w)
2389 Lisp_Object window;
2391 window = Foverlay_get (overlay, Qwindow);
2392 if (WINDOWP (window) && XWINDOW (window) != w)
2393 continue;
2396 /* This overlay is good and counts: put it into sortvec. */
2397 sortvec[j].overlay = overlay;
2398 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2399 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2400 tem = Foverlay_get (overlay, Qpriority);
2401 if (INTEGERP (tem))
2402 sortvec[j].priority = XINT (tem);
2403 else
2404 sortvec[j].priority = 0;
2405 j++;
2408 noverlays = j;
2410 /* Sort the overlays into the proper order: increasing priority. */
2412 if (noverlays > 1)
2413 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2415 for (i = 0; i < noverlays; i++)
2416 overlay_vec[i] = sortvec[i].overlay;
2417 return (noverlays);
2420 struct sortstr
2422 Lisp_Object string, string2;
2423 int size;
2424 int priority;
2427 struct sortstrlist
2429 struct sortstr *buf; /* An array that expands as needed; never freed. */
2430 int size; /* Allocated length of that array. */
2431 int used; /* How much of the array is currently in use. */
2432 int bytes; /* Total length of the strings in buf. */
2435 /* Buffers for storing information about the overlays touching a given
2436 position. These could be automatic variables in overlay_strings, but
2437 it's more efficient to hold onto the memory instead of repeatedly
2438 allocating and freeing it. */
2439 static struct sortstrlist overlay_heads, overlay_tails;
2440 static unsigned char *overlay_str_buf;
2442 /* Allocated length of overlay_str_buf. */
2443 static int overlay_str_len;
2445 /* A comparison function suitable for passing to qsort. */
2446 static int
2447 cmp_for_strings (as1, as2)
2448 char *as1, *as2;
2450 struct sortstr *s1 = (struct sortstr *)as1;
2451 struct sortstr *s2 = (struct sortstr *)as2;
2452 if (s1->size != s2->size)
2453 return s2->size - s1->size;
2454 if (s1->priority != s2->priority)
2455 return s1->priority - s2->priority;
2456 return 0;
2459 static void
2460 record_overlay_string (ssl, str, str2, pri, size)
2461 struct sortstrlist *ssl;
2462 Lisp_Object str, str2, pri;
2463 int size;
2465 int nbytes;
2467 if (ssl->used == ssl->size)
2469 if (ssl->buf)
2470 ssl->size *= 2;
2471 else
2472 ssl->size = 5;
2473 ssl->buf = ((struct sortstr *)
2474 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2476 ssl->buf[ssl->used].string = str;
2477 ssl->buf[ssl->used].string2 = str2;
2478 ssl->buf[ssl->used].size = size;
2479 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2480 ssl->used++;
2482 if (NILP (current_buffer->enable_multibyte_characters))
2483 nbytes = XSTRING (str)->size;
2484 else if (! STRING_MULTIBYTE (str))
2485 nbytes = count_size_as_multibyte (XSTRING (str)->data,
2486 STRING_BYTES (XSTRING (str)));
2487 else
2488 nbytes = STRING_BYTES (XSTRING (str));
2490 ssl->bytes += nbytes;
2492 if (STRINGP (str2))
2494 if (NILP (current_buffer->enable_multibyte_characters))
2495 nbytes = XSTRING (str2)->size;
2496 else if (! STRING_MULTIBYTE (str2))
2497 nbytes = count_size_as_multibyte (XSTRING (str2)->data,
2498 STRING_BYTES (XSTRING (str2)));
2499 else
2500 nbytes = STRING_BYTES (XSTRING (str2));
2502 ssl->bytes += nbytes;
2506 /* Return the concatenation of the strings associated with overlays that
2507 begin or end at POS, ignoring overlays that are specific to a window
2508 other than W. The strings are concatenated in the appropriate order:
2509 shorter overlays nest inside longer ones, and higher priority inside
2510 lower. Normally all of the after-strings come first, but zero-sized
2511 overlays have their after-strings ride along with the before-strings
2512 because it would look strange to print them inside-out.
2514 Returns the string length, and stores the contents indirectly through
2515 PSTR, if that variable is non-null. The string may be overwritten by
2516 subsequent calls. */
2519 overlay_strings (pos, w, pstr)
2520 int pos;
2521 struct window *w;
2522 unsigned char **pstr;
2524 Lisp_Object ov, overlay, window, str;
2525 int startpos, endpos;
2526 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
2528 overlay_heads.used = overlay_heads.bytes = 0;
2529 overlay_tails.used = overlay_tails.bytes = 0;
2530 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
2532 overlay = XCAR (ov);
2533 if (!OVERLAYP (overlay))
2534 abort ();
2536 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2537 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2538 if (endpos < pos)
2539 break;
2540 if (endpos != pos && startpos != pos)
2541 continue;
2542 window = Foverlay_get (overlay, Qwindow);
2543 if (WINDOWP (window) && XWINDOW (window) != w)
2544 continue;
2545 if (startpos == pos
2546 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2547 record_overlay_string (&overlay_heads, str,
2548 (startpos == endpos
2549 ? Foverlay_get (overlay, Qafter_string)
2550 : Qnil),
2551 Foverlay_get (overlay, Qpriority),
2552 endpos - startpos);
2553 else if (endpos == pos
2554 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2555 record_overlay_string (&overlay_tails, str, Qnil,
2556 Foverlay_get (overlay, Qpriority),
2557 endpos - startpos);
2559 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
2561 overlay = XCAR (ov);
2562 if (!OVERLAYP (overlay))
2563 abort ();
2565 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2566 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2567 if (startpos > pos)
2568 break;
2569 if (endpos != pos && startpos != pos)
2570 continue;
2571 window = Foverlay_get (overlay, Qwindow);
2572 if (WINDOWP (window) && XWINDOW (window) != w)
2573 continue;
2574 if (startpos == pos
2575 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2576 record_overlay_string (&overlay_heads, str,
2577 (startpos == endpos
2578 ? Foverlay_get (overlay, Qafter_string)
2579 : Qnil),
2580 Foverlay_get (overlay, Qpriority),
2581 endpos - startpos);
2582 else if (endpos == pos
2583 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2584 record_overlay_string (&overlay_tails, str, Qnil,
2585 Foverlay_get (overlay, Qpriority),
2586 endpos - startpos);
2588 if (overlay_tails.used > 1)
2589 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2590 cmp_for_strings);
2591 if (overlay_heads.used > 1)
2592 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2593 cmp_for_strings);
2594 if (overlay_heads.bytes || overlay_tails.bytes)
2596 Lisp_Object tem;
2597 int i;
2598 unsigned char *p;
2599 int total = overlay_heads.bytes + overlay_tails.bytes;
2601 if (total > overlay_str_len)
2603 overlay_str_len = total;
2604 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2605 total);
2607 p = overlay_str_buf;
2608 for (i = overlay_tails.used; --i >= 0;)
2610 int nbytes;
2611 tem = overlay_tails.buf[i].string;
2612 nbytes = copy_text (XSTRING (tem)->data, p,
2613 STRING_BYTES (XSTRING (tem)),
2614 STRING_MULTIBYTE (tem), multibyte);
2615 p += nbytes;
2617 for (i = 0; i < overlay_heads.used; ++i)
2619 int nbytes;
2620 tem = overlay_heads.buf[i].string;
2621 nbytes = copy_text (XSTRING (tem)->data, p,
2622 STRING_BYTES (XSTRING (tem)),
2623 STRING_MULTIBYTE (tem), multibyte);
2624 p += nbytes;
2625 tem = overlay_heads.buf[i].string2;
2626 if (STRINGP (tem))
2628 nbytes = copy_text (XSTRING (tem)->data, p,
2629 STRING_BYTES (XSTRING (tem)),
2630 STRING_MULTIBYTE (tem), multibyte);
2631 p += nbytes;
2634 if (p != overlay_str_buf + total)
2635 abort ();
2636 if (pstr)
2637 *pstr = overlay_str_buf;
2638 return total;
2640 return 0;
2643 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2645 void
2646 recenter_overlay_lists (buf, pos)
2647 struct buffer *buf;
2648 int pos;
2650 Lisp_Object overlay, tail, next, prev, beg, end;
2652 /* See if anything in overlays_before should move to overlays_after. */
2654 /* We don't strictly need prev in this loop; it should always be nil.
2655 But we use it for symmetry and in case that should cease to be true
2656 with some future change. */
2657 prev = Qnil;
2658 for (tail = buf->overlays_before;
2659 CONSP (tail);
2660 prev = tail, tail = next)
2662 next = XCDR (tail);
2663 overlay = XCAR (tail);
2665 /* If the overlay is not valid, get rid of it. */
2666 if (!OVERLAY_VALID (overlay))
2667 #if 1
2668 abort ();
2669 #else
2671 /* Splice the cons cell TAIL out of overlays_before. */
2672 if (!NILP (prev))
2673 XCDR (prev) = next;
2674 else
2675 buf->overlays_before = next;
2676 tail = prev;
2677 continue;
2679 #endif
2681 beg = OVERLAY_START (overlay);
2682 end = OVERLAY_END (overlay);
2684 if (OVERLAY_POSITION (end) > pos)
2686 /* OVERLAY needs to be moved. */
2687 int where = OVERLAY_POSITION (beg);
2688 Lisp_Object other, other_prev;
2690 /* Splice the cons cell TAIL out of overlays_before. */
2691 if (!NILP (prev))
2692 XCDR (prev) = next;
2693 else
2694 buf->overlays_before = next;
2696 /* Search thru overlays_after for where to put it. */
2697 other_prev = Qnil;
2698 for (other = buf->overlays_after;
2699 CONSP (other);
2700 other_prev = other, other = XCDR (other))
2702 Lisp_Object otherbeg, otheroverlay;
2704 otheroverlay = XCAR (other);
2705 if (! OVERLAY_VALID (otheroverlay))
2706 abort ();
2708 otherbeg = OVERLAY_START (otheroverlay);
2709 if (OVERLAY_POSITION (otherbeg) >= where)
2710 break;
2713 /* Add TAIL to overlays_after before OTHER. */
2714 XCDR (tail) = other;
2715 if (!NILP (other_prev))
2716 XCDR (other_prev) = tail;
2717 else
2718 buf->overlays_after = tail;
2719 tail = prev;
2721 else
2722 /* We've reached the things that should stay in overlays_before.
2723 All the rest of overlays_before must end even earlier,
2724 so stop now. */
2725 break;
2728 /* See if anything in overlays_after should be in overlays_before. */
2729 prev = Qnil;
2730 for (tail = buf->overlays_after;
2731 CONSP (tail);
2732 prev = tail, tail = next)
2734 next = XCDR (tail);
2735 overlay = XCAR (tail);
2737 /* If the overlay is not valid, get rid of it. */
2738 if (!OVERLAY_VALID (overlay))
2739 #if 1
2740 abort ();
2741 #else
2743 /* Splice the cons cell TAIL out of overlays_after. */
2744 if (!NILP (prev))
2745 XCDR (prev) = next;
2746 else
2747 buf->overlays_after = next;
2748 tail = prev;
2749 continue;
2751 #endif
2753 beg = OVERLAY_START (overlay);
2754 end = OVERLAY_END (overlay);
2756 /* Stop looking, when we know that nothing further
2757 can possibly end before POS. */
2758 if (OVERLAY_POSITION (beg) > pos)
2759 break;
2761 if (OVERLAY_POSITION (end) <= pos)
2763 /* OVERLAY needs to be moved. */
2764 int where = OVERLAY_POSITION (end);
2765 Lisp_Object other, other_prev;
2767 /* Splice the cons cell TAIL out of overlays_after. */
2768 if (!NILP (prev))
2769 XCDR (prev) = next;
2770 else
2771 buf->overlays_after = next;
2773 /* Search thru overlays_before for where to put it. */
2774 other_prev = Qnil;
2775 for (other = buf->overlays_before;
2776 CONSP (other);
2777 other_prev = other, other = XCDR (other))
2779 Lisp_Object otherend, otheroverlay;
2781 otheroverlay = XCAR (other);
2782 if (! OVERLAY_VALID (otheroverlay))
2783 abort ();
2785 otherend = OVERLAY_END (otheroverlay);
2786 if (OVERLAY_POSITION (otherend) <= where)
2787 break;
2790 /* Add TAIL to overlays_before before OTHER. */
2791 XCDR (tail) = other;
2792 if (!NILP (other_prev))
2793 XCDR (other_prev) = tail;
2794 else
2795 buf->overlays_before = tail;
2796 tail = prev;
2800 XSETFASTINT (buf->overlay_center, pos);
2803 void
2804 adjust_overlays_for_insert (pos, length)
2805 int pos;
2806 int length;
2808 /* After an insertion, the lists are still sorted properly,
2809 but we may need to update the value of the overlay center. */
2810 if (XFASTINT (current_buffer->overlay_center) >= pos)
2811 XSETFASTINT (current_buffer->overlay_center,
2812 XFASTINT (current_buffer->overlay_center) + length);
2815 void
2816 adjust_overlays_for_delete (pos, length)
2817 int pos;
2818 int length;
2820 if (XFASTINT (current_buffer->overlay_center) < pos)
2821 /* The deletion was to our right. No change needed; the before- and
2822 after-lists are still consistent. */
2824 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
2825 /* The deletion was to our left. We need to adjust the center value
2826 to account for the change in position, but the lists are consistent
2827 given the new value. */
2828 XSETFASTINT (current_buffer->overlay_center,
2829 XFASTINT (current_buffer->overlay_center) - length);
2830 else
2831 /* We're right in the middle. There might be things on the after-list
2832 that now belong on the before-list. Recentering will move them,
2833 and also update the center point. */
2834 recenter_overlay_lists (current_buffer, pos);
2837 /* Fix up overlays that were garbled as a result of permuting markers
2838 in the range START through END. Any overlay with at least one
2839 endpoint in this range will need to be unlinked from the overlay
2840 list and reinserted in its proper place.
2841 Such an overlay might even have negative size at this point.
2842 If so, we'll reverse the endpoints. Can you think of anything
2843 better to do in this situation? */
2844 void
2845 fix_overlays_in_range (start, end)
2846 register int start, end;
2848 Lisp_Object overlay;
2849 Lisp_Object before_list, after_list;
2850 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
2851 int startpos, endpos;
2853 /* This algorithm shifts links around instead of consing and GCing.
2854 The loop invariant is that before_list (resp. after_list) is a
2855 well-formed list except that its last element, the one that
2856 *pbefore (resp. *pafter) points to, is still uninitialized.
2857 So it's not a bug that before_list isn't initialized, although
2858 it may look strange. */
2859 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
2861 overlay = XCAR (*ptail);
2862 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2863 if (endpos < start)
2864 break;
2865 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2866 if (endpos < end
2867 || (startpos >= start && startpos < end))
2869 /* If the overlay is backwards, fix that now. */
2870 if (startpos > endpos)
2872 int tem;
2873 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
2874 Qnil);
2875 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
2876 Qnil);
2877 tem = startpos; startpos = endpos; endpos = tem;
2879 /* Add it to the end of the wrong list. Later on,
2880 recenter_overlay_lists will move it to the right place. */
2881 if (endpos < XINT (current_buffer->overlay_center))
2883 *pafter = *ptail;
2884 pafter = &XCDR (*ptail);
2886 else
2888 *pbefore = *ptail;
2889 pbefore = &XCDR (*ptail);
2891 *ptail = XCDR (*ptail);
2893 else
2894 ptail = &XCDR (*ptail);
2896 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
2898 overlay = XCAR (*ptail);
2899 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2900 if (startpos >= end)
2901 break;
2902 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2903 if (startpos >= start
2904 || (endpos >= start && endpos < end))
2906 if (startpos > endpos)
2908 int tem;
2909 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
2910 Qnil);
2911 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
2912 Qnil);
2913 tem = startpos; startpos = endpos; endpos = tem;
2915 if (endpos < XINT (current_buffer->overlay_center))
2917 *pafter = *ptail;
2918 pafter = &XCDR (*ptail);
2920 else
2922 *pbefore = *ptail;
2923 pbefore = &XCDR (*ptail);
2925 *ptail = XCDR (*ptail);
2927 else
2928 ptail = &XCDR (*ptail);
2931 /* Splice the constructed (wrong) lists into the buffer's lists,
2932 and let the recenter function make it sane again. */
2933 *pbefore = current_buffer->overlays_before;
2934 current_buffer->overlays_before = before_list;
2935 recenter_overlay_lists (current_buffer,
2936 XINT (current_buffer->overlay_center));
2938 *pafter = current_buffer->overlays_after;
2939 current_buffer->overlays_after = after_list;
2940 recenter_overlay_lists (current_buffer,
2941 XINT (current_buffer->overlay_center));
2944 /* We have two types of overlay: the one whose ending marker is
2945 after-insertion-marker (this is the usual case) and the one whose
2946 ending marker is before-insertion-marker. When `overlays_before'
2947 contains overlays of the latter type and the former type in this
2948 order and both overlays end at inserting position, inserting a text
2949 increases only the ending marker of the latter type, which results
2950 in incorrect ordering of `overlays_before'.
2952 This function fixes ordering of overlays in the slot
2953 `overlays_before' of the buffer *BP. Before the insertion, `point'
2954 was at PREV, and now is at POS. */
2956 void
2957 fix_overlays_before (bp, prev, pos)
2958 struct buffer *bp;
2959 int prev, pos;
2961 Lisp_Object *tailp = &bp->overlays_before;
2962 Lisp_Object *right_place;
2963 int end;
2965 /* After the insertion, the several overlays may be in incorrect
2966 order. The possibility is that, in the list `overlays_before',
2967 an overlay which ends at POS appears after an overlay which ends
2968 at PREV. Since POS is greater than PREV, we must fix the
2969 ordering of these overlays, by moving overlays ends at POS before
2970 the overlays ends at PREV. */
2972 /* At first, find a place where disordered overlays should be linked
2973 in. It is where an overlay which end before POS exists. (i.e. an
2974 overlay whose ending marker is after-insertion-marker if disorder
2975 exists). */
2976 while (!NILP (*tailp)
2977 && ((end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp))))
2978 >= pos))
2979 tailp = &XCDR (*tailp);
2981 /* If we don't find such an overlay,
2982 or the found one ends before PREV,
2983 or the found one is the last one in the list,
2984 we don't have to fix anything. */
2985 if (NILP (*tailp)
2986 || end < prev
2987 || NILP (XCDR (*tailp)))
2988 return;
2990 right_place = tailp;
2991 tailp = &XCDR (*tailp);
2993 /* Now, end position of overlays in the list *TAILP should be before
2994 or equal to PREV. In the loop, an overlay which ends at POS is
2995 moved ahead to the place pointed by RIGHT_PLACE. If we found an
2996 overlay which ends before PREV, the remaining overlays are in
2997 correct order. */
2998 while (!NILP (*tailp))
3000 end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp)));
3002 if (end == pos)
3003 { /* This overlay is disordered. */
3004 Lisp_Object found = *tailp;
3006 /* Unlink the found overlay. */
3007 *tailp = XCDR (found);
3008 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
3009 XCDR (found) = *right_place;
3010 /* Link it into the right place. */
3011 *right_place = found;
3013 else if (end == prev)
3014 tailp = &XCDR (*tailp);
3015 else /* No more disordered overlay. */
3016 break;
3020 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3021 "Return t if OBJECT is an overlay.")
3022 (object)
3023 Lisp_Object object;
3025 return (OVERLAYP (object) ? Qt : Qnil);
3028 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3029 "Create a new overlay with range BEG to END in BUFFER.\n\
3030 If omitted, BUFFER defaults to the current buffer.\n\
3031 BEG and END may be integers or markers.\n\
3032 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
3033 front delimiter advance when text is inserted there.\n\
3034 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
3035 rear delimiter advance when text is inserted there.")
3036 (beg, end, buffer, front_advance, rear_advance)
3037 Lisp_Object beg, end, buffer;
3038 Lisp_Object front_advance, rear_advance;
3040 Lisp_Object overlay;
3041 struct buffer *b;
3043 if (NILP (buffer))
3044 XSETBUFFER (buffer, current_buffer);
3045 else
3046 CHECK_BUFFER (buffer, 2);
3047 if (MARKERP (beg)
3048 && ! EQ (Fmarker_buffer (beg), buffer))
3049 error ("Marker points into wrong buffer");
3050 if (MARKERP (end)
3051 && ! EQ (Fmarker_buffer (end), buffer))
3052 error ("Marker points into wrong buffer");
3054 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3055 CHECK_NUMBER_COERCE_MARKER (end, 1);
3057 if (XINT (beg) > XINT (end))
3059 Lisp_Object temp;
3060 temp = beg; beg = end; end = temp;
3063 b = XBUFFER (buffer);
3065 beg = Fset_marker (Fmake_marker (), beg, buffer);
3066 end = Fset_marker (Fmake_marker (), end, buffer);
3068 if (!NILP (front_advance))
3069 XMARKER (beg)->insertion_type = 1;
3070 if (!NILP (rear_advance))
3071 XMARKER (end)->insertion_type = 1;
3073 overlay = allocate_misc ();
3074 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3075 XOVERLAY (overlay)->start = beg;
3076 XOVERLAY (overlay)->end = end;
3077 XOVERLAY (overlay)->plist = Qnil;
3079 /* Put the new overlay on the wrong list. */
3080 end = OVERLAY_END (overlay);
3081 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3082 b->overlays_after = Fcons (overlay, b->overlays_after);
3083 else
3084 b->overlays_before = Fcons (overlay, b->overlays_before);
3086 /* This puts it in the right list, and in the right order. */
3087 recenter_overlay_lists (b, XINT (b->overlay_center));
3089 /* We don't need to redisplay the region covered by the overlay, because
3090 the overlay has no properties at the moment. */
3092 return overlay;
3095 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3097 static void
3098 modify_overlay (buf, start, end)
3099 struct buffer *buf;
3100 int start, end;
3102 if (start == end)
3103 return;
3105 if (start > end)
3107 int temp = start;
3108 start = end; end = temp;
3111 BUF_COMPUTE_UNCHANGED (buf, start, end);
3113 /* If this is a buffer not in the selected window,
3114 we must do other windows. */
3115 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3116 windows_or_buffers_changed = 1;
3117 /* If multiple windows show this buffer, we must do other windows. */
3118 else if (buffer_shared > 1)
3119 windows_or_buffers_changed = 1;
3121 ++BUF_OVERLAY_MODIFF (buf);
3124 \f\f
3125 Lisp_Object Fdelete_overlay ();
3127 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3128 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3129 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3130 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3131 buffer.")
3132 (overlay, beg, end, buffer)
3133 Lisp_Object overlay, beg, end, buffer;
3135 struct buffer *b, *ob;
3136 Lisp_Object obuffer;
3137 int count = specpdl_ptr - specpdl;
3139 CHECK_OVERLAY (overlay, 0);
3140 if (NILP (buffer))
3141 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3142 if (NILP (buffer))
3143 XSETBUFFER (buffer, current_buffer);
3144 CHECK_BUFFER (buffer, 3);
3146 if (MARKERP (beg)
3147 && ! EQ (Fmarker_buffer (beg), buffer))
3148 error ("Marker points into wrong buffer");
3149 if (MARKERP (end)
3150 && ! EQ (Fmarker_buffer (end), buffer))
3151 error ("Marker points into wrong buffer");
3153 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3154 CHECK_NUMBER_COERCE_MARKER (end, 1);
3156 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3157 return Fdelete_overlay (overlay);
3159 if (XINT (beg) > XINT (end))
3161 Lisp_Object temp;
3162 temp = beg; beg = end; end = temp;
3165 specbind (Qinhibit_quit, Qt);
3167 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3168 b = XBUFFER (buffer);
3169 ob = XBUFFER (obuffer);
3171 /* If the overlay has changed buffers, do a thorough redisplay. */
3172 if (!EQ (buffer, obuffer))
3174 /* Redisplay where the overlay was. */
3175 if (!NILP (obuffer))
3177 int o_beg;
3178 int o_end;
3180 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3181 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3183 modify_overlay (ob, o_beg, o_end);
3186 /* Redisplay where the overlay is going to be. */
3187 modify_overlay (b, XINT (beg), XINT (end));
3189 else
3190 /* Redisplay the area the overlay has just left, or just enclosed. */
3192 int o_beg, o_end;
3194 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3195 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3197 if (o_beg == XINT (beg))
3198 modify_overlay (b, o_end, XINT (end));
3199 else if (o_end == XINT (end))
3200 modify_overlay (b, o_beg, XINT (beg));
3201 else
3203 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3204 if (XINT (end) > o_end) o_end = XINT (end);
3205 modify_overlay (b, o_beg, o_end);
3209 if (!NILP (obuffer))
3211 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
3212 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
3215 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3216 Fset_marker (OVERLAY_END (overlay), end, buffer);
3218 /* Put the overlay on the wrong list. */
3219 end = OVERLAY_END (overlay);
3220 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3221 b->overlays_after = Fcons (overlay, b->overlays_after);
3222 else
3223 b->overlays_before = Fcons (overlay, b->overlays_before);
3225 /* This puts it in the right list, and in the right order. */
3226 recenter_overlay_lists (b, XINT (b->overlay_center));
3228 return unbind_to (count, overlay);
3231 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3232 "Delete the overlay OVERLAY from its buffer.")
3233 (overlay)
3234 Lisp_Object overlay;
3236 Lisp_Object buffer;
3237 struct buffer *b;
3238 int count = specpdl_ptr - specpdl;
3240 CHECK_OVERLAY (overlay, 0);
3242 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3243 if (NILP (buffer))
3244 return Qnil;
3246 b = XBUFFER (buffer);
3248 specbind (Qinhibit_quit, Qt);
3250 b->overlays_before = Fdelq (overlay, b->overlays_before);
3251 b->overlays_after = Fdelq (overlay, b->overlays_after);
3253 modify_overlay (b,
3254 marker_position (OVERLAY_START (overlay)),
3255 marker_position (OVERLAY_END (overlay)));
3257 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3258 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3260 return unbind_to (count, Qnil);
3263 /* Overlay dissection functions. */
3265 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3266 "Return the position at which OVERLAY starts.")
3267 (overlay)
3268 Lisp_Object overlay;
3270 CHECK_OVERLAY (overlay, 0);
3272 return (Fmarker_position (OVERLAY_START (overlay)));
3275 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3276 "Return the position at which OVERLAY ends.")
3277 (overlay)
3278 Lisp_Object overlay;
3280 CHECK_OVERLAY (overlay, 0);
3282 return (Fmarker_position (OVERLAY_END (overlay)));
3285 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3286 "Return the buffer OVERLAY belongs to.")
3287 (overlay)
3288 Lisp_Object overlay;
3290 CHECK_OVERLAY (overlay, 0);
3292 return Fmarker_buffer (OVERLAY_START (overlay));
3295 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3296 "Return a list of the properties on OVERLAY.\n\
3297 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3298 OVERLAY.")
3299 (overlay)
3300 Lisp_Object overlay;
3302 CHECK_OVERLAY (overlay, 0);
3304 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3308 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3309 "Return a list of the overlays that contain position POS.")
3310 (pos)
3311 Lisp_Object pos;
3313 int noverlays;
3314 Lisp_Object *overlay_vec;
3315 int len;
3316 Lisp_Object result;
3318 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3320 len = 10;
3321 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3323 /* Put all the overlays we want in a vector in overlay_vec.
3324 Store the length in len. */
3325 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3326 (int *) 0, (int *) 0);
3328 /* Make a list of them all. */
3329 result = Flist (noverlays, overlay_vec);
3331 xfree (overlay_vec);
3332 return result;
3335 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3336 "Return a list of the overlays that overlap the region BEG ... END.\n\
3337 Overlap means that at least one character is contained within the overlay\n\
3338 and also contained within the specified region.\n\
3339 Empty overlays are included in the result if they are located at BEG\n\
3340 or between BEG and END.")
3341 (beg, end)
3342 Lisp_Object beg, end;
3344 int noverlays;
3345 Lisp_Object *overlay_vec;
3346 int len;
3347 Lisp_Object result;
3349 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3350 CHECK_NUMBER_COERCE_MARKER (end, 0);
3352 len = 10;
3353 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3355 /* Put all the overlays we want in a vector in overlay_vec.
3356 Store the length in len. */
3357 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3358 (int *) 0, (int *) 0);
3360 /* Make a list of them all. */
3361 result = Flist (noverlays, overlay_vec);
3363 xfree (overlay_vec);
3364 return result;
3367 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3368 1, 1, 0,
3369 "Return the next position after POS where an overlay starts or ends.\n\
3370 If there are no more overlay boundaries after POS, return (point-max).")
3371 (pos)
3372 Lisp_Object pos;
3374 int noverlays;
3375 int endpos;
3376 Lisp_Object *overlay_vec;
3377 int len;
3378 int i;
3380 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3382 len = 10;
3383 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3385 /* Put all the overlays we want in a vector in overlay_vec.
3386 Store the length in len.
3387 endpos gets the position where the next overlay starts. */
3388 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3389 &endpos, (int *) 0);
3391 /* If any of these overlays ends before endpos,
3392 use its ending point instead. */
3393 for (i = 0; i < noverlays; i++)
3395 Lisp_Object oend;
3396 int oendpos;
3398 oend = OVERLAY_END (overlay_vec[i]);
3399 oendpos = OVERLAY_POSITION (oend);
3400 if (oendpos < endpos)
3401 endpos = oendpos;
3404 xfree (overlay_vec);
3405 return make_number (endpos);
3408 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3409 Sprevious_overlay_change, 1, 1, 0,
3410 "Return the previous position before POS where an overlay starts or ends.\n\
3411 If there are no more overlay boundaries before POS, return (point-min).")
3412 (pos)
3413 Lisp_Object pos;
3415 int noverlays;
3416 int prevpos;
3417 Lisp_Object *overlay_vec;
3418 int len;
3420 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3422 len = 10;
3423 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3425 /* At beginning of buffer, we know the answer;
3426 avoid bug subtracting 1 below. */
3427 if (XINT (pos) == BEGV)
3428 return pos;
3430 /* Put all the overlays we want in a vector in overlay_vec.
3431 Store the length in len.
3432 prevpos gets the position of the previous change. */
3433 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3434 (int *) 0, &prevpos);
3436 xfree (overlay_vec);
3437 return make_number (prevpos);
3440 /* These functions are for debugging overlays. */
3442 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3443 "Return a pair of lists giving all the overlays of the current buffer.\n\
3444 The car has all the overlays before the overlay center;\n\
3445 the cdr has all the overlays after the overlay center.\n\
3446 Recentering overlays moves overlays between these lists.\n\
3447 The lists you get are copies, so that changing them has no effect.\n\
3448 However, the overlays you get are the real objects that the buffer uses.")
3451 Lisp_Object before, after;
3452 before = current_buffer->overlays_before;
3453 if (CONSP (before))
3454 before = Fcopy_sequence (before);
3455 after = current_buffer->overlays_after;
3456 if (CONSP (after))
3457 after = Fcopy_sequence (after);
3459 return Fcons (before, after);
3462 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3463 "Recenter the overlays of the current buffer around position POS.")
3464 (pos)
3465 Lisp_Object pos;
3467 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3469 recenter_overlay_lists (current_buffer, XINT (pos));
3470 return Qnil;
3473 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3474 "Get the property of overlay OVERLAY with property name PROP.")
3475 (overlay, prop)
3476 Lisp_Object overlay, prop;
3478 Lisp_Object plist, fallback;
3480 CHECK_OVERLAY (overlay, 0);
3482 fallback = Qnil;
3484 for (plist = XOVERLAY (overlay)->plist;
3485 CONSP (plist) && CONSP (XCDR (plist));
3486 plist = XCDR (XCDR (plist)))
3488 if (EQ (XCAR (plist), prop))
3489 return XCAR (XCDR (plist));
3490 else if (EQ (XCAR (plist), Qcategory))
3492 Lisp_Object tem;
3493 tem = Fcar (Fcdr (plist));
3494 if (SYMBOLP (tem))
3495 fallback = Fget (tem, prop);
3499 return fallback;
3502 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3503 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3504 (overlay, prop, value)
3505 Lisp_Object overlay, prop, value;
3507 Lisp_Object tail, buffer;
3508 int changed;
3510 CHECK_OVERLAY (overlay, 0);
3512 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3514 for (tail = XOVERLAY (overlay)->plist;
3515 CONSP (tail) && CONSP (XCDR (tail));
3516 tail = XCDR (XCDR (tail)))
3517 if (EQ (XCAR (tail), prop))
3519 changed = !EQ (XCAR (XCDR (tail)), value);
3520 XCAR (XCDR (tail)) = value;
3521 goto found;
3523 /* It wasn't in the list, so add it to the front. */
3524 changed = !NILP (value);
3525 XOVERLAY (overlay)->plist
3526 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3527 found:
3528 if (! NILP (buffer))
3530 if (changed)
3531 modify_overlay (XBUFFER (buffer),
3532 marker_position (OVERLAY_START (overlay)),
3533 marker_position (OVERLAY_END (overlay)));
3534 if (EQ (prop, Qevaporate) && ! NILP (value)
3535 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3536 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3537 Fdelete_overlay (overlay);
3539 return value;
3542 /* Subroutine of report_overlay_modification. */
3544 /* Lisp vector holding overlay hook functions to call.
3545 Vector elements come in pairs.
3546 Each even-index element is a list of hook functions.
3547 The following odd-index element is the overlay they came from.
3549 Before the buffer change, we fill in this vector
3550 as we call overlay hook functions.
3551 After the buffer change, we get the functions to call from this vector.
3552 This way we always call the same functions before and after the change. */
3553 static Lisp_Object last_overlay_modification_hooks;
3555 /* Number of elements actually used in last_overlay_modification_hooks. */
3556 static int last_overlay_modification_hooks_used;
3558 /* Add one functionlist/overlay pair
3559 to the end of last_overlay_modification_hooks. */
3561 static void
3562 add_overlay_mod_hooklist (functionlist, overlay)
3563 Lisp_Object functionlist, overlay;
3565 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3567 if (last_overlay_modification_hooks_used == oldsize)
3569 Lisp_Object old;
3570 old = last_overlay_modification_hooks;
3571 last_overlay_modification_hooks
3572 = Fmake_vector (make_number (oldsize * 2), Qnil);
3573 bcopy (XVECTOR (old)->contents,
3574 XVECTOR (last_overlay_modification_hooks)->contents,
3575 sizeof (Lisp_Object) * oldsize);
3577 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3578 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3581 /* Run the modification-hooks of overlays that include
3582 any part of the text in START to END.
3583 If this change is an insertion, also
3584 run the insert-before-hooks of overlay starting at END,
3585 and the insert-after-hooks of overlay ending at START.
3587 This is called both before and after the modification.
3588 AFTER is nonzero when we call after the modification.
3590 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3591 When AFTER is nonzero, they are the start position,
3592 the position after the inserted new text,
3593 and the length of deleted or replaced old text. */
3595 void
3596 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3597 Lisp_Object start, end;
3598 int after;
3599 Lisp_Object arg1, arg2, arg3;
3601 Lisp_Object prop, overlay, tail;
3602 /* 1 if this change is an insertion. */
3603 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3604 int tail_copied;
3605 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3607 overlay = Qnil;
3608 tail = Qnil;
3609 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3611 if (after)
3613 /* Call the functions recorded in last_overlay_modification_hooks
3614 rather than scanning the overlays again.
3615 First copy the vector contents, in case some of these hooks
3616 do subsequent modification of the buffer. */
3617 int size = last_overlay_modification_hooks_used;
3618 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3619 int i;
3621 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3622 copy, size * sizeof (Lisp_Object));
3623 gcpro1.var = copy;
3624 gcpro1.nvars = size;
3626 for (i = 0; i < size;)
3628 Lisp_Object prop, overlay;
3629 prop = copy[i++];
3630 overlay = copy[i++];
3631 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3633 UNGCPRO;
3634 return;
3637 /* We are being called before a change.
3638 Scan the overlays to find the functions to call. */
3639 last_overlay_modification_hooks_used = 0;
3640 tail_copied = 0;
3641 for (tail = current_buffer->overlays_before;
3642 CONSP (tail);
3643 tail = XCDR (tail))
3645 int startpos, endpos;
3646 Lisp_Object ostart, oend;
3648 overlay = XCAR (tail);
3650 ostart = OVERLAY_START (overlay);
3651 oend = OVERLAY_END (overlay);
3652 endpos = OVERLAY_POSITION (oend);
3653 if (XFASTINT (start) > endpos)
3654 break;
3655 startpos = OVERLAY_POSITION (ostart);
3656 if (insertion && (XFASTINT (start) == startpos
3657 || XFASTINT (end) == startpos))
3659 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3660 if (!NILP (prop))
3662 /* Copy TAIL in case the hook recenters the overlay lists. */
3663 if (!tail_copied)
3664 tail = Fcopy_sequence (tail);
3665 tail_copied = 1;
3666 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3669 if (insertion && (XFASTINT (start) == endpos
3670 || XFASTINT (end) == endpos))
3672 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3673 if (!NILP (prop))
3675 if (!tail_copied)
3676 tail = Fcopy_sequence (tail);
3677 tail_copied = 1;
3678 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3681 /* Test for intersecting intervals. This does the right thing
3682 for both insertion and deletion. */
3683 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3685 prop = Foverlay_get (overlay, Qmodification_hooks);
3686 if (!NILP (prop))
3688 if (!tail_copied)
3689 tail = Fcopy_sequence (tail);
3690 tail_copied = 1;
3691 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3696 tail_copied = 0;
3697 for (tail = current_buffer->overlays_after;
3698 CONSP (tail);
3699 tail = XCDR (tail))
3701 int startpos, endpos;
3702 Lisp_Object ostart, oend;
3704 overlay = XCAR (tail);
3706 ostart = OVERLAY_START (overlay);
3707 oend = OVERLAY_END (overlay);
3708 startpos = OVERLAY_POSITION (ostart);
3709 endpos = OVERLAY_POSITION (oend);
3710 if (XFASTINT (end) < startpos)
3711 break;
3712 if (insertion && (XFASTINT (start) == startpos
3713 || XFASTINT (end) == startpos))
3715 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3716 if (!NILP (prop))
3718 if (!tail_copied)
3719 tail = Fcopy_sequence (tail);
3720 tail_copied = 1;
3721 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3724 if (insertion && (XFASTINT (start) == endpos
3725 || XFASTINT (end) == endpos))
3727 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3728 if (!NILP (prop))
3730 if (!tail_copied)
3731 tail = Fcopy_sequence (tail);
3732 tail_copied = 1;
3733 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3736 /* Test for intersecting intervals. This does the right thing
3737 for both insertion and deletion. */
3738 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3740 prop = Foverlay_get (overlay, Qmodification_hooks);
3741 if (!NILP (prop))
3743 if (!tail_copied)
3744 tail = Fcopy_sequence (tail);
3745 tail_copied = 1;
3746 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3751 UNGCPRO;
3754 static void
3755 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3756 Lisp_Object list, overlay;
3757 int after;
3758 Lisp_Object arg1, arg2, arg3;
3760 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3762 GCPRO4 (list, arg1, arg2, arg3);
3763 if (! after)
3764 add_overlay_mod_hooklist (list, overlay);
3766 while (!NILP (list))
3768 if (NILP (arg3))
3769 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3770 else
3771 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
3772 list = Fcdr (list);
3774 UNGCPRO;
3777 /* Delete any zero-sized overlays at position POS, if the `evaporate'
3778 property is set. */
3779 void
3780 evaporate_overlays (pos)
3781 int pos;
3783 Lisp_Object tail, overlay, hit_list;
3785 hit_list = Qnil;
3786 if (pos <= XFASTINT (current_buffer->overlay_center))
3787 for (tail = current_buffer->overlays_before; CONSP (tail);
3788 tail = XCDR (tail))
3790 int endpos;
3791 overlay = XCAR (tail);
3792 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3793 if (endpos < pos)
3794 break;
3795 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
3796 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3797 hit_list = Fcons (overlay, hit_list);
3799 else
3800 for (tail = current_buffer->overlays_after; CONSP (tail);
3801 tail = XCDR (tail))
3803 int startpos;
3804 overlay = XCAR (tail);
3805 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3806 if (startpos > pos)
3807 break;
3808 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
3809 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3810 hit_list = Fcons (overlay, hit_list);
3812 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
3813 Fdelete_overlay (XCAR (hit_list));
3816 /* Somebody has tried to store a value with an unacceptable type
3817 in the slot with offset OFFSET. */
3819 void
3820 buffer_slot_type_mismatch (offset)
3821 int offset;
3823 Lisp_Object sym;
3824 char *type_name;
3825 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
3826 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
3828 case Lisp_Int: type_name = "integers"; break;
3829 case Lisp_String: type_name = "strings"; break;
3830 case Lisp_Symbol: type_name = "symbols"; break;
3832 default:
3833 abort ();
3836 error ("Only %s should be stored in the buffer-local variable %s",
3837 type_name, XSYMBOL (sym)->name->data);
3840 void
3841 init_buffer_once ()
3843 buffer_permanent_local_flags = 0;
3845 /* Make sure all markable slots in buffer_defaults
3846 are initialized reasonably, so mark_buffer won't choke. */
3847 reset_buffer (&buffer_defaults);
3848 reset_buffer_local_variables (&buffer_defaults, 1);
3849 reset_buffer (&buffer_local_symbols);
3850 reset_buffer_local_variables (&buffer_local_symbols, 1);
3851 /* Prevent GC from getting confused. */
3852 buffer_defaults.text = &buffer_defaults.own_text;
3853 buffer_local_symbols.text = &buffer_local_symbols.own_text;
3854 BUF_INTERVALS (&buffer_defaults) = 0;
3855 BUF_INTERVALS (&buffer_local_symbols) = 0;
3856 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3857 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
3859 /* Set up the default values of various buffer slots. */
3860 /* Must do these before making the first buffer! */
3862 /* real setup is done in loaddefs.el */
3863 buffer_defaults.mode_line_format = build_string ("%-");
3864 buffer_defaults.header_line_format = Qnil;
3865 buffer_defaults.abbrev_mode = Qnil;
3866 buffer_defaults.overwrite_mode = Qnil;
3867 buffer_defaults.case_fold_search = Qt;
3868 buffer_defaults.auto_fill_function = Qnil;
3869 buffer_defaults.selective_display = Qnil;
3870 #ifndef old
3871 buffer_defaults.selective_display_ellipses = Qt;
3872 #endif
3873 buffer_defaults.abbrev_table = Qnil;
3874 buffer_defaults.display_table = Qnil;
3875 buffer_defaults.undo_list = Qnil;
3876 buffer_defaults.mark_active = Qnil;
3877 buffer_defaults.file_format = Qnil;
3878 buffer_defaults.overlays_before = Qnil;
3879 buffer_defaults.overlays_after = Qnil;
3880 XSETFASTINT (buffer_defaults.overlay_center, BEG);
3882 XSETFASTINT (buffer_defaults.tab_width, 8);
3883 buffer_defaults.truncate_lines = Qnil;
3884 buffer_defaults.ctl_arrow = Qt;
3885 buffer_defaults.direction_reversed = Qnil;
3887 #ifdef DOS_NT
3888 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
3889 #endif
3890 buffer_defaults.enable_multibyte_characters = Qt;
3891 buffer_defaults.buffer_file_coding_system = Qnil;
3892 XSETFASTINT (buffer_defaults.fill_column, 70);
3893 XSETFASTINT (buffer_defaults.left_margin, 0);
3894 buffer_defaults.cache_long_line_scans = Qnil;
3895 buffer_defaults.file_truename = Qnil;
3896 XSETFASTINT (buffer_defaults.display_count, 0);
3897 buffer_defaults.indicate_empty_lines = Qnil;
3898 buffer_defaults.scroll_up_aggressively = Qnil;
3899 buffer_defaults.scroll_down_aggressively = Qnil;
3900 buffer_defaults.display_time = Qnil;
3902 /* Assign the local-flags to the slots that have default values.
3903 The local flag is a bit that is used in the buffer
3904 to say that it has its own local value for the slot.
3905 The local flag bits are in the local_var_flags slot of the buffer. */
3907 /* Nothing can work if this isn't true */
3908 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
3910 /* 0 means not a lisp var, -1 means always local, else mask */
3911 bzero (&buffer_local_flags, sizeof buffer_local_flags);
3912 XSETINT (buffer_local_flags.filename, -1);
3913 XSETINT (buffer_local_flags.directory, -1);
3914 XSETINT (buffer_local_flags.backed_up, -1);
3915 XSETINT (buffer_local_flags.save_length, -1);
3916 XSETINT (buffer_local_flags.auto_save_file_name, -1);
3917 XSETINT (buffer_local_flags.read_only, -1);
3918 XSETINT (buffer_local_flags.major_mode, -1);
3919 XSETINT (buffer_local_flags.mode_name, -1);
3920 XSETINT (buffer_local_flags.undo_list, -1);
3921 XSETINT (buffer_local_flags.mark_active, -1);
3922 XSETINT (buffer_local_flags.point_before_scroll, -1);
3923 XSETINT (buffer_local_flags.file_truename, -1);
3924 XSETINT (buffer_local_flags.invisibility_spec, -1);
3925 XSETINT (buffer_local_flags.file_format, -1);
3926 XSETINT (buffer_local_flags.display_count, -1);
3927 XSETINT (buffer_local_flags.display_time, -1);
3928 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
3930 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
3931 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
3932 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
3933 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
3934 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
3935 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
3936 #ifndef old
3937 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
3938 #endif
3939 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
3940 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
3941 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
3942 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
3943 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
3944 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
3945 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
3946 #ifdef DOS_NT
3947 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
3948 /* Make this one a permanent local. */
3949 buffer_permanent_local_flags |= 0x4000;
3950 #endif
3951 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
3952 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
3953 XSETFASTINT (buffer_local_flags.category_table, 0x20000);
3954 XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
3955 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x80000);
3956 /* Make this one a permanent local. */
3957 buffer_permanent_local_flags |= 0x80000;
3958 XSETFASTINT (buffer_local_flags.left_margin_width, 0x100000);
3959 XSETFASTINT (buffer_local_flags.right_margin_width, 0x200000);
3960 XSETFASTINT (buffer_local_flags.indicate_empty_lines, 0x400000);
3961 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, 0x800000);
3962 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, 0x1000000);
3963 XSETFASTINT (buffer_local_flags.header_line_format, 0x2000000);
3965 Vbuffer_alist = Qnil;
3966 current_buffer = 0;
3967 all_buffers = 0;
3969 QSFundamental = build_string ("Fundamental");
3971 Qfundamental_mode = intern ("fundamental-mode");
3972 buffer_defaults.major_mode = Qfundamental_mode;
3974 Qmode_class = intern ("mode-class");
3976 Qprotected_field = intern ("protected-field");
3978 Qpermanent_local = intern ("permanent-local");
3980 Qkill_buffer_hook = intern ("kill-buffer-hook");
3982 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
3984 /* super-magic invisible buffer */
3985 Vbuffer_alist = Qnil;
3987 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3989 inhibit_modification_hooks = 0;
3992 void
3993 init_buffer ()
3995 char buf[MAXPATHLEN+1];
3996 char *pwd;
3997 struct stat dotstat, pwdstat;
3998 Lisp_Object temp;
3999 int rc;
4001 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4002 if (NILP (buffer_defaults.enable_multibyte_characters))
4003 Fset_buffer_multibyte (Qnil);
4005 /* If PWD is accurate, use it instead of calling getwd. This is faster
4006 when PWD is right, and may avoid a fatal error. */
4007 if ((pwd = getenv ("PWD")) != 0
4008 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
4009 && stat (pwd, &pwdstat) == 0
4010 && stat (".", &dotstat) == 0
4011 && dotstat.st_ino == pwdstat.st_ino
4012 && dotstat.st_dev == pwdstat.st_dev
4013 && strlen (pwd) < MAXPATHLEN)
4014 strcpy (buf, pwd);
4015 #ifdef HAVE_GETCWD
4016 else if (getcwd (buf, MAXPATHLEN+1) == 0)
4017 fatal ("`getcwd' failed: %s\n", strerror (errno));
4018 #else
4019 else if (getwd (buf) == 0)
4020 fatal ("`getwd' failed: %s\n", buf);
4021 #endif
4023 #ifndef VMS
4024 /* Maybe this should really use some standard subroutine
4025 whose definition is filename syntax dependent. */
4026 rc = strlen (buf);
4027 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
4029 buf[rc] = DIRECTORY_SEP;
4030 buf[rc + 1] = '\0';
4032 #endif /* not VMS */
4034 current_buffer->directory = build_string (buf);
4036 /* Add /: to the front of the name
4037 if it would otherwise be treated as magic. */
4038 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
4039 if (! NILP (temp)
4040 /* If the default dir is just /, TEMP is non-nil
4041 because of the ange-ftp completion handler.
4042 However, it is not necessary to turn / into /:/.
4043 So avoid doing that. */
4044 && strcmp ("/", XSTRING (current_buffer->directory)->data))
4045 current_buffer->directory
4046 = concat2 (build_string ("/:"), current_buffer->directory);
4048 temp = get_minibuffer (0);
4049 XBUFFER (temp)->directory = current_buffer->directory;
4052 /* initialize the buffer routines */
4053 void
4054 syms_of_buffer ()
4056 staticpro (&last_overlay_modification_hooks);
4057 last_overlay_modification_hooks
4058 = Fmake_vector (make_number (10), Qnil);
4060 staticpro (&Vbuffer_defaults);
4061 staticpro (&Vbuffer_local_symbols);
4062 staticpro (&Qfundamental_mode);
4063 staticpro (&Qmode_class);
4064 staticpro (&QSFundamental);
4065 staticpro (&Vbuffer_alist);
4066 staticpro (&Qprotected_field);
4067 staticpro (&Qpermanent_local);
4068 staticpro (&Qkill_buffer_hook);
4069 Qoverlayp = intern ("overlayp");
4070 staticpro (&Qoverlayp);
4071 Qevaporate = intern ("evaporate");
4072 staticpro (&Qevaporate);
4073 Qmodification_hooks = intern ("modification-hooks");
4074 staticpro (&Qmodification_hooks);
4075 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
4076 staticpro (&Qinsert_in_front_hooks);
4077 Qinsert_behind_hooks = intern ("insert-behind-hooks");
4078 staticpro (&Qinsert_behind_hooks);
4079 Qget_file_buffer = intern ("get-file-buffer");
4080 staticpro (&Qget_file_buffer);
4081 Qpriority = intern ("priority");
4082 staticpro (&Qpriority);
4083 Qwindow = intern ("window");
4084 staticpro (&Qwindow);
4085 Qbefore_string = intern ("before-string");
4086 staticpro (&Qbefore_string);
4087 Qafter_string = intern ("after-string");
4088 staticpro (&Qafter_string);
4089 Qfirst_change_hook = intern ("first-change-hook");
4090 staticpro (&Qfirst_change_hook);
4091 Qbefore_change_functions = intern ("before-change-functions");
4092 staticpro (&Qbefore_change_functions);
4093 Qafter_change_functions = intern ("after-change-functions");
4094 staticpro (&Qafter_change_functions);
4096 Fput (Qprotected_field, Qerror_conditions,
4097 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
4098 Fput (Qprotected_field, Qerror_message,
4099 build_string ("Attempt to modify a protected field"));
4101 /* All these use DEFVAR_LISP_NOPRO because the slots in
4102 buffer_defaults will all be marked via Vbuffer_defaults. */
4104 DEFVAR_LISP_NOPRO ("default-mode-line-format",
4105 &buffer_defaults.mode_line_format,
4106 "Default value of `mode-line-format' for buffers that don't override it.\n\
4107 This is the same as (default-value 'mode-line-format).");
4109 DEFVAR_LISP_NOPRO ("default-header-line-format",
4110 &buffer_defaults.header_line_format,
4111 "Default value of `header-line-format' for buffers that don't override it.\n\
4112 This is the same as (default-value 'header-line-format).");
4114 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
4115 &buffer_defaults.abbrev_mode,
4116 "Default value of `abbrev-mode' for buffers that do not override it.\n\
4117 This is the same as (default-value 'abbrev-mode).");
4119 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
4120 &buffer_defaults.ctl_arrow,
4121 "Default value of `ctl-arrow' for buffers that do not override it.\n\
4122 This is the same as (default-value 'ctl-arrow).");
4124 DEFVAR_LISP_NOPRO ("default-direction-reversed",
4125 &buffer_defaults.direction_reversed,
4126 "Default value of `direction_reversed' for buffers that do not override it.\n\
4127 This is the same as (default-value 'direction-reversed).");
4129 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
4130 &buffer_defaults.enable_multibyte_characters,
4131 "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
4132 This is the same as (default-value 'enable-multibyte-characters).");
4134 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
4135 &buffer_defaults.buffer_file_coding_system,
4136 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
4137 This is the same as (default-value 'buffer-file-coding-system).");
4139 DEFVAR_LISP_NOPRO ("default-truncate-lines",
4140 &buffer_defaults.truncate_lines,
4141 "Default value of `truncate-lines' for buffers that do not override it.\n\
4142 This is the same as (default-value 'truncate-lines).");
4144 DEFVAR_LISP_NOPRO ("default-fill-column",
4145 &buffer_defaults.fill_column,
4146 "Default value of `fill-column' for buffers that do not override it.\n\
4147 This is the same as (default-value 'fill-column).");
4149 DEFVAR_LISP_NOPRO ("default-left-margin",
4150 &buffer_defaults.left_margin,
4151 "Default value of `left-margin' for buffers that do not override it.\n\
4152 This is the same as (default-value 'left-margin).");
4154 DEFVAR_LISP_NOPRO ("default-tab-width",
4155 &buffer_defaults.tab_width,
4156 "Default value of `tab-width' for buffers that do not override it.\n\
4157 This is the same as (default-value 'tab-width).");
4159 DEFVAR_LISP_NOPRO ("default-case-fold-search",
4160 &buffer_defaults.case_fold_search,
4161 "Default value of `case-fold-search' for buffers that don't override it.\n\
4162 This is the same as (default-value 'case-fold-search).");
4164 #ifdef DOS_NT
4165 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
4166 &buffer_defaults.buffer_file_type,
4167 "Default file type for buffers that do not override it.\n\
4168 This is the same as (default-value 'buffer-file-type).\n\
4169 The file type is nil for text, t for binary.");
4170 #endif
4172 DEFVAR_LISP_NOPRO ("default-left-margin-width",
4173 &buffer_defaults.left_margin_width,
4174 "Default value of `left-margin-width' for buffers that don't override it.\n\
4175 This is the same as (default-value 'left-margin-width).");
4177 DEFVAR_LISP_NOPRO ("default-right-margin-width",
4178 &buffer_defaults.right_margin_width,
4179 "Default value of `right_margin_width' for buffers that don't override it.\n\
4180 This is the same as (default-value 'right-margin-width).");
4182 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
4183 &buffer_defaults.indicate_empty_lines,
4184 "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
4185 This is the same as (default-value 'indicate-empty-lines).");
4187 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
4188 &buffer_defaults.scroll_up_aggressively,
4189 "Default value of `scroll-up-aggressively' for buffers that\n\
4190 don't override it. This is the same as (default-value\n\
4191 'scroll-up-aggressively).");
4193 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
4194 &buffer_defaults.scroll_down_aggressively,
4195 "Default value of `scroll-down-aggressively' for buffers that\n\
4196 don't override it. This is the same as (default-value\n\
4197 'scroll-down-aggressively).");
4199 DEFVAR_PER_BUFFER ("header-line-format",
4200 &current_buffer->header_line_format,
4201 Qnil,
4202 "Analogous to `mode-line-format', but for a mode line displayed\n\
4203 at the top of windows.");
4205 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
4206 Qnil, 0);
4208 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
4209 But make-docfile finds it!
4210 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
4211 Qnil,
4212 "Template for displaying mode line for current buffer.\n\
4213 Each buffer has its own value of this variable.\n\
4214 Value may be nil, a string, a symbol or a list or cons cell.\n\
4215 A value of nil means don't display a mode line.\n\
4216 For a symbol, its value is used (but it is ignored if t or nil).\n\
4217 A string appearing directly as the value of a symbol is processed verbatim\n\
4218 in that the %-constructs below are not recognized.\n\
4219 For a list of the form `(:eval FORM)', FORM is evaluated and the result\n\
4220 is used as a mode line element.\n\
4221 For a list whose car is a symbol, the symbol's value is taken,\n\
4222 and if that is non-nil, the cadr of the list is processed recursively.\n\
4223 Otherwise, the caddr of the list (if there is one) is processed.\n\
4224 For a list whose car is a string or list, each element is processed\n\
4225 recursively and the results are effectively concatenated.\n\
4226 For a list whose car is an integer, the cdr of the list is processed\n\
4227 and padded (if the number is positive) or truncated (if negative)\n\
4228 to the width specified by that number.\n\
4229 A string is printed verbatim in the mode line except for %-constructs:\n\
4230 (%-constructs are allowed when the string is the entire mode-line-format\n\
4231 or when it is found in a cons-cell or a list)\n\
4232 %b -- print buffer name. %f -- print visited file name.\n\
4233 %F -- print frame name.\n\
4234 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
4235 %& is like %*, but ignore read-only-ness.\n\
4236 % means buffer is read-only and * means it is modified.\n\
4237 For a modified read-only buffer, %* gives % and %+ gives *.\n\
4238 %s -- print process status. %l -- print the current line number.\n\
4239 %c -- print the current column number (this makes editing slower).\n\
4240 To make the column number update correctly in all cases,\n\
4241 `column-number-mode' must be non-nil.\n\
4242 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
4243 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
4244 or print Bottom or All.\n\
4245 %m -- print the mode name.\n\
4246 %n -- print Narrow if appropriate.\n\
4247 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.\n\
4248 %Z -- like %z, but including the end-of-line format.\n\
4249 %[ -- print one [ for each recursive editing level. %] similar.\n\
4250 %% -- print %. %- -- print infinitely many dashes.\n\
4251 Decimal digits after the % specify field width to which to pad.");
4254 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
4255 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
4256 nil here means use current buffer's major mode.");
4258 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
4259 make_number (Lisp_Symbol),
4260 "Symbol for current buffer's major mode.");
4262 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
4263 make_number (Lisp_String),
4264 "Pretty name of current buffer's major mode (a string).");
4266 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
4267 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
4268 Automatically becomes buffer-local when set in any fashion.");
4270 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
4271 Qnil,
4272 "*Non-nil if searches and matches should ignore case.\n\
4273 Automatically becomes buffer-local when set in any fashion.");
4275 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
4276 make_number (Lisp_Int),
4277 "*Column beyond which automatic line-wrapping should happen.\n\
4278 Automatically becomes buffer-local when set in any fashion.");
4280 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
4281 make_number (Lisp_Int),
4282 "*Column for the default indent-line-function to indent to.\n\
4283 Linefeed indents to this column in Fundamental mode.\n\
4284 Automatically becomes buffer-local when set in any fashion.");
4286 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
4287 make_number (Lisp_Int),
4288 "*Distance between tab stops (for display of tab characters), in columns.\n\
4289 Automatically becomes buffer-local when set in any fashion.");
4291 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
4292 "*Non-nil means display control chars with uparrow.\n\
4293 A value of nil means use backslash and octal digits.\n\
4294 Automatically becomes buffer-local when set in any fashion.\n\
4295 This variable does not apply to characters whose display is specified\n\
4296 in the current display table (if there is one).");
4298 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
4299 &current_buffer->enable_multibyte_characters,
4300 make_number (-1),
4301 "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
4302 Otherwise they are regarded as unibyte. This affects the display,\n\
4303 file I/O and the behavior of various editing commands.\n\
4305 This variable is buffer-local but you cannot set it directly;\n\
4306 use the function `set-buffer-multibyte' to change a buffer's representation.\n\
4307 Changing its default value with `setq-default' is supported.\n\
4308 See also variable `default-enable-multibyte-characters' and Info node\n\
4309 `(elisp)Text Representations'.");
4311 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
4312 &current_buffer->buffer_file_coding_system, Qnil,
4313 "Coding system to be used for encoding the buffer contents on saving.\n\
4314 This variable applies to saving the buffer, and also to `write-region'\n\
4315 and other functions that use `write-region'.\n\
4316 It does not apply to sending output to subprocesses, however.\n\
4318 If this is nil, the buffer is saved without any code conversion\n\
4319 unless some coding system is specified in `file-coding-system-alist'\n\
4320 for the buffer file.\n\
4322 The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
4324 This variable is never applied to a way of decoding\n\
4325 a file while reading it.");
4327 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
4328 Qnil,
4329 "*Non-nil means lines in the buffer are displayed right to left.");
4331 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
4332 "*Non-nil means do not display continuation lines;\n\
4333 give each line of text one screen line.\n\
4334 Automatically becomes buffer-local when set in any fashion.\n\
4336 Note that this is overridden by the variable\n\
4337 `truncate-partial-width-windows' if that variable is non-nil\n\
4338 and this buffer is not full-frame width.");
4340 #ifdef DOS_NT
4341 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
4342 Qnil,
4343 "Non-nil if the visited file is a binary file.\n\
4344 This variable is meaningful on MS-DOG and Windows NT.\n\
4345 On those systems, it is automatically local in every buffer.\n\
4346 On other systems, this variable is normally always nil.");
4347 #endif
4349 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
4350 make_number (Lisp_String),
4351 "Name of default directory of current buffer. Should end with slash.\n\
4352 Each buffer has its own value of this variable.");
4354 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
4355 Qnil,
4356 "Function called (if non-nil) to perform auto-fill.\n\
4357 It is called after self-inserting a space or newline.\n\
4358 Each buffer has its own value of this variable.\n\
4359 NOTE: This variable is not a hook;\n\
4360 its value may not be a list of functions.");
4362 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
4363 make_number (Lisp_String),
4364 "Name of file visited in current buffer, or nil if not visiting a file.\n\
4365 Each buffer has its own value of this variable.");
4367 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
4368 make_number (Lisp_String),
4369 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
4370 The truename of a file is calculated by `file-truename'\n\
4371 and then abbreviated with `abbreviate-file-name'.\n\
4372 Each buffer has its own value of this variable.");
4374 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
4375 &current_buffer->auto_save_file_name,
4376 make_number (Lisp_String),
4377 "Name of file for auto-saving current buffer,\n\
4378 or nil if buffer should not be auto-saved.\n\
4379 Each buffer has its own value of this variable.");
4381 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
4382 "Non-nil if this buffer is read-only.\n\
4383 Each buffer has its own value of this variable.");
4385 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
4386 "Non-nil if this buffer's file has been backed up.\n\
4387 Backing up is done before the first time the file is saved.\n\
4388 Each buffer has its own value of this variable.");
4390 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
4391 make_number (Lisp_Int),
4392 "Length of current buffer when last read in, saved or auto-saved.\n\
4393 0 initially.\n\
4394 Each buffer has its own value of this variable.");
4396 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
4397 Qnil,
4398 "Non-nil enables selective display:\n\
4399 Integer N as value means display only lines\n\
4400 that start with less than n columns of space.\n\
4401 A value of t means, after a ^M, all the rest of the line is invisible.\n\
4402 Then ^M's in the file are written into files as newlines.\n\n\
4403 Automatically becomes buffer-local when set in any fashion.");
4405 #ifndef old
4406 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4407 &current_buffer->selective_display_ellipses,
4408 Qnil,
4409 "t means display ... on previous line when a line is invisible.\n\
4410 Automatically becomes buffer-local when set in any fashion.");
4411 #endif
4413 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
4414 "Non-nil if self-insertion should replace existing text.\n\
4415 The value should be one of `overwrite-mode-textual',\n\
4416 `overwrite-mode-binary', or nil.\n\
4417 If it is `overwrite-mode-textual', self-insertion still\n\
4418 inserts at the end of a line, and inserts when point is before a tab,\n\
4419 until the tab is filled in.\n\
4420 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
4421 Automatically becomes buffer-local when set in any fashion.");
4423 #if 0 /* The doc string is too long for some compilers,
4424 but make-docfile can find it in this comment. */
4425 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4426 Qnil,
4427 "Display table that controls display of the contents of current buffer.\n\
4428 Automatically becomes buffer-local when set in any fashion.\n\
4429 The display table is a char-table created with `make-display-table'.\n\
4430 The ordinary char-table elements control how to display each possible text\n\
4431 character. Each value should be a vector of characters or nil;\n\
4432 nil means display the character in the default fashion.\n\
4433 There are six extra slots to control the display of\n\
4434 the end of a truncated screen line (extra-slot 0, a single character);\n\
4435 the end of a continued line (extra-slot 1, a single character);\n\
4436 the escape character used to display character codes in octal\n\
4437 (extra-slot 2, a single character);\n\
4438 the character used as an arrow for control characters (extra-slot 3,\n\
4439 a single character);\n\
4440 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
4441 a vector of characters);\n\
4442 the character used to draw the border between side-by-side windows\n\
4443 (extra-slot 5, a single character).\n\
4444 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
4445 If this variable is nil, the value of `standard-display-table' is used.\n\
4446 Each window can have its own, overriding display table.");
4447 #endif
4448 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4449 Qnil, 0);
4451 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_width,
4452 Qnil,
4453 "*Width of left marginal area for display of a buffer.\n\
4454 Automatically becomes buffer-local when set in any fashion.\n\
4455 A value of nil means no marginal area.");
4457 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_width,
4458 Qnil,
4459 "*Width of right marginal area for display of a buffer.\n\
4460 Automatically becomes buffer-local when set in any fashion.\n\
4461 A value of nil means no marginal area.");
4463 DEFVAR_PER_BUFFER ("indicate-empty-lines",
4464 &current_buffer->indicate_empty_lines, Qnil,
4465 "*Visually indicate empty lines after the buffer end.\n\
4466 If non-nil, a bitmap is displayed in the left fringe of a window on\n\
4467 window-systems.\n\
4468 Automatically becomes buffer-local when set in any fashion.\n");
4470 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
4471 &current_buffer->scroll_up_aggressively, Qnil,
4472 "*If a number, scroll display up aggressively.\n\
4473 If scrolling a window because point is above the window start, choose\n\
4474 a new window start so that point ends up that fraction of the window's\n\
4475 height from the bottom of the window.\n\
4476 Automatically becomes buffer-local when set in any fashion.");
4478 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
4479 &current_buffer->scroll_down_aggressively, Qnil,
4480 "*If a number, scroll display down aggressively.\n\
4481 If scrolling a window because point is below the window end, choose\n\
4482 a new window start so that point ends up that fraction of the window's\n\
4483 height from the top of the window.\n\
4484 Automatically becomes buffer-local when set in any fashion.");
4486 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4487 "Don't ask.");
4489 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
4490 "If non-nil, a function to call before each text change (obsolete).\n\
4491 Two arguments are passed to the function: the positions of\n\
4492 the beginning and end of the range of old text to be changed.\n\
4493 \(For an insertion, the beginning and end are at the same place.)\n\
4494 No information is given about the length of the text after the change.\n\
4496 Buffer changes made while executing the `before-change-function'\n\
4497 don't call any before-change or after-change functions.\n\
4498 That's because these variables are temporarily set to nil.\n\
4499 As a result, a hook function cannot straightforwardly alter the value of\n\
4500 these variables. See the Emacs Lisp manual for a way of\n\
4501 accomplishing an equivalent result by using other variables.\n\n\
4502 This variable is obsolete; use `before-change-functions' instead.");
4503 Vbefore_change_function = Qnil;
4505 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
4506 "If non-nil, a Function to call after each text change (obsolete).\n\
4507 Three arguments are passed to the function: the positions of\n\
4508 the beginning and end of the range of changed text,\n\
4509 and the length of the pre-change text replaced by that range.\n\
4510 \(For an insertion, the pre-change length is zero;\n\
4511 for a deletion, that length is the number of bytes deleted,\n\
4512 and the post-change beginning and end are at the same place.)\n\
4514 Buffer changes made while executing the `after-change-function'\n\
4515 don't call any before-change or after-change functions.\n\
4516 That's because these variables are temporarily set to nil.\n\
4517 As a result, a hook function cannot straightforwardly alter the value of\n\
4518 these variables. See the Emacs Lisp manual for a way of\n\
4519 accomplishing an equivalent result by using other variables.\n\n\
4520 This variable is obsolete; use `after-change-functions' instead.");
4521 Vafter_change_function = Qnil;
4523 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
4524 "List of functions to call before each text change.\n\
4525 Two arguments are passed to each function: the positions of\n\
4526 the beginning and end of the range of old text to be changed.\n\
4527 \(For an insertion, the beginning and end are at the same place.)\n\
4528 No information is given about the length of the text after the change.\n\
4530 Buffer changes made while executing the `before-change-functions'\n\
4531 don't call any before-change or after-change functions.\n\
4532 That's because these variables are temporarily set to nil.\n\
4533 As a result, a hook function cannot straightforwardly alter the value of\n\
4534 these variables. See the Emacs Lisp manual for a way of\n\
4535 accomplishing an equivalent result by using other variables.\n\
4537 If an unhandled error happens in running these functions,\n\
4538 the variable's value remains nil. That prevents the error\n\
4539 from happening repeatedly and making Emacs nonfunctional.");
4540 Vbefore_change_functions = Qnil;
4542 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
4543 "List of function to call after each text change.\n\
4544 Three arguments are passed to each function: the positions of\n\
4545 the beginning and end of the range of changed text,\n\
4546 and the length in bytes of the pre-change text replaced by that range.\n\
4547 \(For an insertion, the pre-change length is zero;\n\
4548 for a deletion, that length is the number of bytes deleted,\n\
4549 and the post-change beginning and end are at the same place.)\n\
4551 Buffer changes made while executing the `after-change-functions'\n\
4552 don't call any before-change or after-change functions.\n\
4553 That's because these variables are temporarily set to nil.\n\
4554 As a result, a hook function cannot straightforwardly alter the value of\n\
4555 these variables. See the Emacs Lisp manual for a way of\n\
4556 accomplishing an equivalent result by using other variables.\n\
4558 If an unhandled error happens in running these functions,\n\
4559 the variable's value remains nil. That prevents the error\n\
4560 from happening repeatedly and making Emacs nonfunctional.");
4561 Vafter_change_functions = Qnil;
4563 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
4564 "A list of functions to call before changing a buffer which is unmodified.\n\
4565 The functions are run using the `run-hooks' function.");
4566 Vfirst_change_hook = Qnil;
4568 #if 0 /* The doc string is too long for some compilers,
4569 but make-docfile can find it in this comment. */
4570 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4571 "List of undo entries in current buffer.\n\
4572 This variable is always local in all buffers.\n\
4573 Recent changes come first; older changes follow newer.\n\
4575 An entry (BEG . END) represents an insertion which begins at\n\
4576 position BEG and ends at position END.\n\
4578 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4579 from (abs POSITION). If POSITION is positive, point was at the front\n\
4580 of the text being deleted; if negative, point was at the end.\n\
4582 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4583 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4584 of the visited file's modification time, as of that time. If the\n\
4585 modification time of the most recent save is different, this entry is\n\
4586 obsolete.\n\
4588 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4589 was modified between BEG and END. PROPERTY is the property name,\n\
4590 and VALUE is the old value.\n\
4592 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4593 was adjusted in position by the offset DISTANCE (an integer).\n\
4595 An entry of the form POSITION indicates that point was at the buffer\n\
4596 location given by the integer. Undoing an entry of this form places\n\
4597 point at POSITION.\n\
4599 nil marks undo boundaries. The undo command treats the changes\n\
4600 between two undo boundaries as a single step to be undone.\n\
4602 If the value of the variable is t, undo information is not recorded.");
4603 #endif
4604 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4607 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
4608 "Non-nil means the mark and region are currently active in this buffer.\n\
4609 Automatically local in all buffers.");
4611 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
4612 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4613 This variable is buffer-local, in all buffers.\n\
4615 Normally, the line-motion functions work by scanning the buffer for\n\
4616 newlines. Columnar operations (like move-to-column and\n\
4617 compute-motion) also work by scanning the buffer, summing character\n\
4618 widths as they go. This works well for ordinary text, but if the\n\
4619 buffer's lines are very long (say, more than 500 characters), these\n\
4620 motion functions will take longer to execute. Emacs may also take\n\
4621 longer to update the display.\n\
4623 If cache-long-line-scans is non-nil, these motion functions cache the\n\
4624 results of their scans, and consult the cache to avoid rescanning\n\
4625 regions of the buffer until the text is modified. The caches are most\n\
4626 beneficial when they prevent the most searching---that is, when the\n\
4627 buffer contains long lines and large regions of characters with the\n\
4628 same, fixed screen width.\n\
4630 When cache-long-line-scans is non-nil, processing short lines will\n\
4631 become slightly slower (because of the overhead of consulting the\n\
4632 cache), and the caches will use memory roughly proportional to the\n\
4633 number of newlines and characters whose screen width varies.\n\
4635 The caches require no explicit maintenance; their accuracy is\n\
4636 maintained internally by the Emacs primitives. Enabling or disabling\n\
4637 the cache should not affect the behavior of any of the motion\n\
4638 functions; it should only affect their performance.");
4640 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
4641 "Value of point before the last series of scroll operations, or nil.\n\
4642 This variable is always local in all buffers.");
4644 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4645 "List of formats to use when saving this buffer.\n\
4646 This variable is always local in all buffers.\n\
4647 Formats are defined by `format-alist'. This variable is\n\
4648 set when a file is visited. Automatically local in all buffers.");
4650 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4651 &current_buffer->invisibility_spec, Qnil,
4652 "Invisibility spec of this buffer.\n\
4653 This variable is always local in all buffers.\n\
4654 The default is t, which means that text is invisible\n\
4655 if it has a non-nil `invisible' property.\n\
4656 If the value is a list, a text character is invisible if its `invisible'\n\
4657 property is an element in that list.\n\
4658 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4659 then characters with property value PROP are invisible,\n\
4660 and they have an ellipsis as well if ELLIPSIS is non-nil.");
4662 DEFVAR_PER_BUFFER ("buffer-display-count",
4663 &current_buffer->display_count, Qnil,
4664 "A number incremented each time this buffer is displayed in a window.\n\
4665 This variable is always local in all buffers.\n\
4666 The function `set-window-buffer increments it.");
4668 DEFVAR_PER_BUFFER ("buffer-display-time",
4669 &current_buffer->display_time, Qnil,
4670 "Time stamp updated each time this buffer is displayed in a window.\n\
4671 This variable is always local in all buffers.\n\
4672 The function `set-window-buffer' updates this variable\n\
4673 to the value obtained by calling `current-time'.\n\
4674 If the buffer has never been shown in a window, the value is nil.");
4676 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
4677 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4678 Non-nil also enables highlighting of the region whenever the mark is active.\n\
4679 The variable `highlight-nonselected-windows' controls whether to highlight\n\
4680 all windows or just the selected window.");
4681 Vtransient_mark_mode = Qnil;
4683 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
4684 "*Non-nil means disregard read-only status of buffers or characters.\n\
4685 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4686 text properties. If the value is a list, disregard `buffer-read-only'\n\
4687 and disregard a `read-only' text property if the property value\n\
4688 is a member of the list.");
4689 Vinhibit_read_only = Qnil;
4691 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4692 "List of functions called with no args to query before killing a buffer.");
4693 Vkill_buffer_query_functions = Qnil;
4695 defsubr (&Sbuffer_live_p);
4696 defsubr (&Sbuffer_list);
4697 defsubr (&Sget_buffer);
4698 defsubr (&Sget_file_buffer);
4699 defsubr (&Sget_buffer_create);
4700 defsubr (&Smake_indirect_buffer);
4701 defsubr (&Sgenerate_new_buffer_name);
4702 defsubr (&Sbuffer_name);
4703 /*defsubr (&Sbuffer_number);*/
4704 defsubr (&Sbuffer_file_name);
4705 defsubr (&Sbuffer_base_buffer);
4706 defsubr (&Sbuffer_local_variables);
4707 defsubr (&Sbuffer_modified_p);
4708 defsubr (&Sset_buffer_modified_p);
4709 defsubr (&Sbuffer_modified_tick);
4710 defsubr (&Srename_buffer);
4711 defsubr (&Sother_buffer);
4712 defsubr (&Sbuffer_disable_undo);
4713 defsubr (&Sbuffer_enable_undo);
4714 defsubr (&Skill_buffer);
4715 defsubr (&Sset_buffer_major_mode);
4716 defsubr (&Sswitch_to_buffer);
4717 defsubr (&Spop_to_buffer);
4718 defsubr (&Scurrent_buffer);
4719 defsubr (&Sset_buffer);
4720 defsubr (&Sbarf_if_buffer_read_only);
4721 defsubr (&Sbury_buffer);
4722 defsubr (&Serase_buffer);
4723 defsubr (&Sset_buffer_multibyte);
4724 defsubr (&Skill_all_local_variables);
4726 defsubr (&Soverlayp);
4727 defsubr (&Smake_overlay);
4728 defsubr (&Sdelete_overlay);
4729 defsubr (&Smove_overlay);
4730 defsubr (&Soverlay_start);
4731 defsubr (&Soverlay_end);
4732 defsubr (&Soverlay_buffer);
4733 defsubr (&Soverlay_properties);
4734 defsubr (&Soverlays_at);
4735 defsubr (&Soverlays_in);
4736 defsubr (&Snext_overlay_change);
4737 defsubr (&Sprevious_overlay_change);
4738 defsubr (&Soverlay_recenter);
4739 defsubr (&Soverlay_lists);
4740 defsubr (&Soverlay_get);
4741 defsubr (&Soverlay_put);
4744 void
4745 keys_of_buffer ()
4747 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4748 initial_define_key (control_x_map, 'k', "kill-buffer");
4750 /* This must not be in syms_of_buffer, because Qdisabled is not
4751 initialized when that function gets called. */
4752 Fput (intern ("erase-buffer"), Qdisabled, Qt);