(Equality Predicates): No longer talk about "two" functions.
[emacs.git] / src / buffer.c
blobd056ce7dc48037adc2dc4fb92bb1f8f799b393d3
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include <config.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/param.h>
29 #include <errno.h>
30 #include <stdio.h>
32 #ifndef USE_CRT_DLL
33 extern int errno;
34 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #include "lisp.h"
42 #include "intervals.h"
43 #include "window.h"
44 #include "commands.h"
45 #include "buffer.h"
46 #include "character.h"
47 #include "region-cache.h"
48 #include "indent.h"
49 #include "blockinput.h"
50 #include "keyboard.h"
51 #include "keymap.h"
52 #include "frame.h"
54 struct buffer *current_buffer; /* the current buffer */
56 /* First buffer in chain of all buffers (in reverse order of creation).
57 Threaded through ->next. */
59 struct buffer *all_buffers;
61 /* This structure holds the default values of the buffer-local variables
62 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
63 The default value occupies the same slot in this structure
64 as an individual buffer's value occupies in that buffer.
65 Setting the default value also goes through the alist of buffers
66 and stores into each buffer that does not say it has a local value. */
68 DECL_ALIGN (struct buffer, buffer_defaults);
70 /* A Lisp_Object pointer to the above, used for staticpro */
72 static Lisp_Object Vbuffer_defaults;
74 /* This structure marks which slots in a buffer have corresponding
75 default values in buffer_defaults.
76 Each such slot has a nonzero value in this structure.
77 The value has only one nonzero bit.
79 When a buffer has its own local value for a slot,
80 the entry for that slot (found in the same slot in this structure)
81 is turned on in the buffer's local_flags array.
83 If a slot in this structure is -1, then even though there may
84 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
85 and the corresponding slot in buffer_defaults is not used.
87 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
88 but there is a default value which is copied into each buffer.
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 DECL_ALIGN (struct buffer, buffer_local_symbols);
100 /* A Lisp_Object pointer to the above, used for staticpro */
101 static Lisp_Object Vbuffer_local_symbols;
103 /* Flags indicating which built-in buffer-local variables
104 are permanent locals. */
105 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
107 /* Number of per-buffer variables used. */
109 int last_per_buffer_idx;
111 EXFUN (Fset_buffer, 1);
112 void set_buffer_internal P_ ((struct buffer *b));
113 void set_buffer_internal_1 P_ ((struct buffer *b));
114 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
115 int after, Lisp_Object arg1,
116 Lisp_Object arg2, Lisp_Object arg3));
117 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
118 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
120 /* Alist of all buffer names vs the buffers. */
121 /* This used to be a variable, but is no longer,
122 to prevent lossage due to user rplac'ing this alist or its elements. */
123 Lisp_Object Vbuffer_alist;
125 /* Functions to call before and after each text change. */
126 Lisp_Object Vbefore_change_functions;
127 Lisp_Object Vafter_change_functions;
129 Lisp_Object Vtransient_mark_mode;
131 /* t means ignore all read-only text properties.
132 A list means ignore such a property if its value is a member of the list.
133 Any non-nil value means ignore buffer-read-only. */
134 Lisp_Object Vinhibit_read_only;
136 /* List of functions to call that can query about killing a buffer.
137 If any of these functions returns nil, we don't kill it. */
138 Lisp_Object Vkill_buffer_query_functions;
139 Lisp_Object Qkill_buffer_query_functions;
141 /* Hook run before changing a major mode. */
142 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
144 /* List of functions to call before changing an unmodified buffer. */
145 Lisp_Object Vfirst_change_hook;
147 Lisp_Object Qfirst_change_hook;
148 Lisp_Object Qbefore_change_functions;
149 Lisp_Object Qafter_change_functions;
150 Lisp_Object Qucs_set_table_for_input;
152 /* If nonzero, all modification hooks are suppressed. */
153 int inhibit_modification_hooks;
155 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
156 Lisp_Object Qpermanent_local_hook;
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 static void alloc_buffer_text P_ ((struct buffer *, size_t));
175 static void free_buffer_text P_ ((struct buffer *b));
176 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
177 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
178 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
180 extern char * emacs_strerror P_ ((int));
182 /* For debugging; temporary. See set_buffer_internal. */
183 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
185 void
186 nsberror (spec)
187 Lisp_Object spec;
189 if (STRINGP (spec))
190 error ("No buffer named %s", SDATA (spec));
191 error ("Invalid buffer argument");
194 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
195 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
196 Value is nil if OBJECT is not a buffer or if it has been killed. */)
197 (object)
198 Lisp_Object object;
200 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
201 ? Qt : Qnil);
204 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
205 doc: /* Return a list of all existing live buffers.
206 If the optional arg FRAME is a frame, we return the buffer list
207 in the proper order for that frame: the buffers in FRAME's `buffer-list'
208 frame parameter come first, followed by the rest of the buffers. */)
209 (frame)
210 Lisp_Object frame;
212 Lisp_Object general;
213 general = Fmapcar (Qcdr, Vbuffer_alist);
215 if (FRAMEP (frame))
217 Lisp_Object framelist, prevlist, tail;
218 Lisp_Object args[3];
220 CHECK_FRAME (frame);
222 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
223 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
225 /* Remove from GENERAL any buffer that duplicates one in
226 FRAMELIST or PREVLIST. */
227 tail = framelist;
228 while (CONSP (tail))
230 general = Fdelq (XCAR (tail), general);
231 tail = XCDR (tail);
233 tail = prevlist;
234 while (CONSP (tail))
236 general = Fdelq (XCAR (tail), general);
237 tail = XCDR (tail);
240 args[0] = framelist;
241 args[1] = general;
242 args[2] = prevlist;
243 return Fnconc (3, args);
246 return general;
249 /* Like Fassoc, but use Fstring_equal to compare
250 (which ignores text properties),
251 and don't ever QUIT. */
253 static Lisp_Object
254 assoc_ignore_text_properties (key, list)
255 register Lisp_Object key;
256 Lisp_Object list;
258 register Lisp_Object tail;
259 for (tail = list; CONSP (tail); tail = XCDR (tail))
261 register Lisp_Object elt, tem;
262 elt = XCAR (tail);
263 tem = Fstring_equal (Fcar (elt), key);
264 if (!NILP (tem))
265 return elt;
267 return Qnil;
270 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
271 doc: /* Return the buffer named NAME (a string).
272 If there is no live buffer named NAME, return nil.
273 NAME may also be a buffer; if so, the value is that buffer. */)
274 (name)
275 register Lisp_Object name;
277 if (BUFFERP (name))
278 return name;
279 CHECK_STRING (name);
281 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
284 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
285 doc: /* Return the buffer visiting file FILENAME (a string).
286 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
287 If there is no such live buffer, return nil.
288 See also `find-buffer-visiting'. */)
289 (filename)
290 register Lisp_Object filename;
292 register Lisp_Object tail, buf, tem;
293 Lisp_Object handler;
295 CHECK_STRING (filename);
296 filename = Fexpand_file_name (filename, Qnil);
298 /* If the file name has special constructs in it,
299 call the corresponding file handler. */
300 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
301 if (!NILP (handler))
302 return call2 (handler, Qget_file_buffer, filename);
304 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
306 buf = Fcdr (XCAR (tail));
307 if (!BUFFERP (buf)) continue;
308 if (!STRINGP (XBUFFER (buf)->filename)) continue;
309 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
310 if (!NILP (tem))
311 return buf;
313 return Qnil;
316 Lisp_Object
317 get_truename_buffer (filename)
318 register Lisp_Object filename;
320 register Lisp_Object tail, buf, tem;
322 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
324 buf = Fcdr (XCAR (tail));
325 if (!BUFFERP (buf)) continue;
326 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
327 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
328 if (!NILP (tem))
329 return buf;
331 return Qnil;
334 /* Incremented for each buffer created, to assign the buffer number. */
335 int buffer_count;
337 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
338 doc: /* Return the buffer named NAME, or create such a buffer and return it.
339 A new buffer is created if there is no live buffer named NAME.
340 If NAME starts with a space, the new buffer does not keep undo information.
341 If NAME is a buffer instead of a string, then it is the value returned.
342 The value is never nil. */)
343 (name)
344 register Lisp_Object name;
346 register Lisp_Object buf;
347 register struct buffer *b;
349 buf = Fget_buffer (name);
350 if (!NILP (buf))
351 return buf;
353 if (SCHARS (name) == 0)
354 error ("Empty string for buffer name is not allowed");
356 b = allocate_buffer ();
358 /* An ordinary buffer uses its own struct buffer_text. */
359 b->text = &b->own_text;
360 b->base_buffer = 0;
362 BUF_GAP_SIZE (b) = 20;
363 BLOCK_INPUT;
364 /* We allocate extra 1-byte at the tail and keep it always '\0' for
365 anchoring a search. */
366 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
367 UNBLOCK_INPUT;
368 if (! BUF_BEG_ADDR (b))
369 buffer_memory_full ();
371 BUF_PT (b) = BEG;
372 BUF_GPT (b) = BEG;
373 BUF_BEGV (b) = BEG;
374 BUF_ZV (b) = BEG;
375 BUF_Z (b) = BEG;
376 BUF_PT_BYTE (b) = BEG_BYTE;
377 BUF_GPT_BYTE (b) = BEG_BYTE;
378 BUF_BEGV_BYTE (b) = BEG_BYTE;
379 BUF_ZV_BYTE (b) = BEG_BYTE;
380 BUF_Z_BYTE (b) = BEG_BYTE;
381 BUF_MODIFF (b) = 1;
382 BUF_CHARS_MODIFF (b) = 1;
383 BUF_OVERLAY_MODIFF (b) = 1;
384 BUF_SAVE_MODIFF (b) = 1;
385 BUF_INTERVALS (b) = 0;
386 BUF_UNCHANGED_MODIFIED (b) = 1;
387 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
388 BUF_END_UNCHANGED (b) = 0;
389 BUF_BEG_UNCHANGED (b) = 0;
390 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
392 b->newline_cache = 0;
393 b->width_run_cache = 0;
394 b->width_table = Qnil;
395 b->prevent_redisplay_optimizations_p = 1;
397 /* Put this on the chain of all buffers including killed ones. */
398 b->next = all_buffers;
399 all_buffers = b;
401 /* An ordinary buffer normally doesn't need markers
402 to handle BEGV and ZV. */
403 b->pt_marker = Qnil;
404 b->begv_marker = Qnil;
405 b->zv_marker = Qnil;
407 name = Fcopy_sequence (name);
408 STRING_SET_INTERVALS (name, NULL_INTERVAL);
409 b->name = name;
411 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
413 reset_buffer (b);
414 reset_buffer_local_variables (b, 1);
416 b->mark = Fmake_marker ();
417 BUF_MARKERS (b) = NULL;
418 b->name = name;
420 /* Put this in the alist of all live buffers. */
421 XSETBUFFER (buf, b);
422 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
424 /* An error in calling the function here (should someone redefine it)
425 can lead to infinite regress until you run out of stack. rms
426 says that's not worth protecting against. */
427 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
428 /* buf is on buffer-alist, so no gcpro. */
429 call1 (Qucs_set_table_for_input, buf);
431 return buf;
435 /* Return a list of overlays which is a copy of the overlay list
436 LIST, but for buffer B. */
438 static struct Lisp_Overlay *
439 copy_overlays (b, list)
440 struct buffer *b;
441 struct Lisp_Overlay *list;
443 Lisp_Object buffer;
444 struct Lisp_Overlay *result = NULL, *tail = NULL;
446 XSETBUFFER (buffer, b);
448 for (; list; list = list->next)
450 Lisp_Object overlay, start, end, old_overlay;
451 EMACS_INT charpos;
453 XSETMISC (old_overlay, list);
454 charpos = marker_position (OVERLAY_START (old_overlay));
455 start = Fmake_marker ();
456 Fset_marker (start, make_number (charpos), buffer);
457 XMARKER (start)->insertion_type
458 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
460 charpos = marker_position (OVERLAY_END (old_overlay));
461 end = Fmake_marker ();
462 Fset_marker (end, make_number (charpos), buffer);
463 XMARKER (end)->insertion_type
464 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
466 overlay = allocate_misc ();
467 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
468 OVERLAY_START (overlay) = start;
469 OVERLAY_END (overlay) = end;
470 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
471 XOVERLAY (overlay)->next = NULL;
473 if (tail)
474 tail = tail->next = XOVERLAY (overlay);
475 else
476 result = tail = XOVERLAY (overlay);
479 return result;
483 /* Clone per-buffer values of buffer FROM.
485 Buffer TO gets the same per-buffer values as FROM, with the
486 following exceptions: (1) TO's name is left untouched, (2) markers
487 are copied and made to refer to TO, and (3) overlay lists are
488 copied. */
490 static void
491 clone_per_buffer_values (from, to)
492 struct buffer *from, *to;
494 Lisp_Object to_buffer;
495 int offset;
497 XSETBUFFER (to_buffer, to);
499 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
500 offset < sizeof *to;
501 offset += sizeof (Lisp_Object))
503 Lisp_Object obj;
505 obj = PER_BUFFER_VALUE (from, offset);
506 if (MARKERP (obj))
508 struct Lisp_Marker *m = XMARKER (obj);
509 obj = Fmake_marker ();
510 XMARKER (obj)->insertion_type = m->insertion_type;
511 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
514 PER_BUFFER_VALUE (to, offset) = obj;
517 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
519 to->overlays_before = copy_overlays (to, from->overlays_before);
520 to->overlays_after = copy_overlays (to, from->overlays_after);
522 /* Get (a copy of) the alist of Lisp-level local variables of FROM
523 and install that in TO. */
524 to->local_var_alist = buffer_lisp_local_variables (from);
527 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
528 2, 3,
529 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
530 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
531 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
532 NAME should be a string which is not the name of an existing buffer.
533 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
534 such as major and minor modes, in the indirect buffer.
535 CLONE nil means the indirect buffer's state is reset to default values. */)
536 (base_buffer, name, clone)
537 Lisp_Object base_buffer, name, clone;
539 Lisp_Object buf, tem;
540 struct buffer *b;
542 CHECK_STRING (name);
543 buf = Fget_buffer (name);
544 if (!NILP (buf))
545 error ("Buffer name `%s' is in use", SDATA (name));
547 tem = base_buffer;
548 base_buffer = Fget_buffer (base_buffer);
549 if (NILP (base_buffer))
550 error ("No such buffer: `%s'", SDATA (tem));
551 if (NILP (XBUFFER (base_buffer)->name))
552 error ("Base buffer has been killed");
554 if (SCHARS (name) == 0)
555 error ("Empty string for buffer name is not allowed");
557 b = allocate_buffer ();
559 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
560 ? XBUFFER (base_buffer)->base_buffer
561 : XBUFFER (base_buffer));
563 /* Use the base buffer's text object. */
564 b->text = b->base_buffer->text;
566 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
567 BUF_ZV (b) = BUF_ZV (b->base_buffer);
568 BUF_PT (b) = BUF_PT (b->base_buffer);
569 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
570 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
571 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
573 b->newline_cache = 0;
574 b->width_run_cache = 0;
575 b->width_table = Qnil;
577 /* Put this on the chain of all buffers including killed ones. */
578 b->next = all_buffers;
579 all_buffers = b;
581 name = Fcopy_sequence (name);
582 STRING_SET_INTERVALS (name, NULL_INTERVAL);
583 b->name = name;
585 reset_buffer (b);
586 reset_buffer_local_variables (b, 1);
588 /* Put this in the alist of all live buffers. */
589 XSETBUFFER (buf, b);
590 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
592 b->mark = Fmake_marker ();
593 b->name = name;
595 /* The multibyte status belongs to the base buffer. */
596 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
598 /* Make sure the base buffer has markers for its narrowing. */
599 if (NILP (b->base_buffer->pt_marker))
601 b->base_buffer->pt_marker = Fmake_marker ();
602 set_marker_both (b->base_buffer->pt_marker, base_buffer,
603 BUF_PT (b->base_buffer),
604 BUF_PT_BYTE (b->base_buffer));
606 if (NILP (b->base_buffer->begv_marker))
608 b->base_buffer->begv_marker = Fmake_marker ();
609 set_marker_both (b->base_buffer->begv_marker, base_buffer,
610 BUF_BEGV (b->base_buffer),
611 BUF_BEGV_BYTE (b->base_buffer));
613 if (NILP (b->base_buffer->zv_marker))
615 b->base_buffer->zv_marker = Fmake_marker ();
616 set_marker_both (b->base_buffer->zv_marker, base_buffer,
617 BUF_ZV (b->base_buffer),
618 BUF_ZV_BYTE (b->base_buffer));
619 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
622 if (NILP (clone))
624 /* Give the indirect buffer markers for its narrowing. */
625 b->pt_marker = Fmake_marker ();
626 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
627 b->begv_marker = Fmake_marker ();
628 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
629 b->zv_marker = Fmake_marker ();
630 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
631 XMARKER (b->zv_marker)->insertion_type = 1;
633 else
635 struct buffer *old_b = current_buffer;
637 clone_per_buffer_values (b->base_buffer, b);
638 b->filename = Qnil;
639 b->file_truename = Qnil;
640 b->display_count = make_number (0);
641 b->backed_up = Qnil;
642 b->auto_save_file_name = Qnil;
643 set_buffer_internal_1 (b);
644 Fset (intern ("buffer-save-without-query"), Qnil);
645 Fset (intern ("buffer-file-number"), Qnil);
646 Fset (intern ("buffer-stale-function"), Qnil);
647 set_buffer_internal_1 (old_b);
650 return buf;
653 void
654 delete_all_overlays (b)
655 struct buffer *b;
657 Lisp_Object overlay;
659 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
660 have to empty the list, otherwise we end up with overlays that
661 think they belong to this buffer while the buffer doesn't know about
662 them any more. */
663 while (b->overlays_before)
665 XSETMISC (overlay, b->overlays_before);
666 Fdelete_overlay (overlay);
668 while (b->overlays_after)
670 XSETMISC (overlay, b->overlays_after);
671 Fdelete_overlay (overlay);
673 eassert (b->overlays_before == NULL);
674 eassert (b->overlays_after == NULL);
677 /* Reinitialize everything about a buffer except its name and contents
678 and local variables.
679 If called on an already-initialized buffer, the list of overlays
680 should be deleted before calling this function, otherwise we end up
681 with overlays that claim to belong to the buffer but the buffer
682 claims it doesn't belong to it. */
684 void
685 reset_buffer (b)
686 register struct buffer *b;
688 b->filename = Qnil;
689 b->file_truename = Qnil;
690 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
691 b->modtime = 0;
692 XSETFASTINT (b->save_length, 0);
693 b->last_window_start = 1;
694 /* It is more conservative to start out "changed" than "unchanged". */
695 b->clip_changed = 0;
696 b->prevent_redisplay_optimizations_p = 1;
697 b->backed_up = Qnil;
698 b->auto_save_modified = 0;
699 b->auto_save_failure_time = -1;
700 b->auto_save_file_name = Qnil;
701 b->read_only = Qnil;
702 b->overlays_before = NULL;
703 b->overlays_after = NULL;
704 b->overlay_center = BEG;
705 b->mark_active = Qnil;
706 b->point_before_scroll = Qnil;
707 b->file_format = Qnil;
708 b->auto_save_file_format = Qt;
709 b->last_selected_window = Qnil;
710 XSETINT (b->display_count, 0);
711 b->display_time = Qnil;
712 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
713 b->cursor_type = buffer_defaults.cursor_type;
714 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
716 b->display_error_modiff = 0;
719 /* Reset buffer B's local variables info.
720 Don't use this on a buffer that has already been in use;
721 it does not treat permanent locals consistently.
722 Instead, use Fkill_all_local_variables.
724 If PERMANENT_TOO is 1, then we reset permanent
725 buffer-local variables. If PERMANENT_TOO is 0,
726 we preserve those. */
728 static void
729 reset_buffer_local_variables (b, permanent_too)
730 register struct buffer *b;
731 int permanent_too;
733 register int offset;
734 int i;
736 /* Reset the major mode to Fundamental, together with all the
737 things that depend on the major mode.
738 default-major-mode is handled at a higher level.
739 We ignore it here. */
740 b->major_mode = Qfundamental_mode;
741 b->keymap = Qnil;
742 b->mode_name = QSFundamental;
743 b->minor_modes = Qnil;
745 /* If the standard case table has been altered and invalidated,
746 fix up its insides first. */
747 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
748 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
749 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
750 Fset_standard_case_table (Vascii_downcase_table);
752 b->downcase_table = Vascii_downcase_table;
753 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
754 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
755 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
756 b->invisibility_spec = Qt;
757 #ifndef DOS_NT
758 b->buffer_file_type = Qnil;
759 #endif
761 /* Reset all (or most) per-buffer variables to their defaults. */
762 if (permanent_too)
763 b->local_var_alist = Qnil;
764 else
766 Lisp_Object tmp, prop, last = Qnil;
767 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
768 if (CONSP (XCAR (tmp))
769 && SYMBOLP (XCAR (XCAR (tmp)))
770 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
772 /* If permanent-local, keep it. */
773 last = tmp;
774 if (EQ (prop, Qpermanent_local_hook))
776 /* This is a partially permanent hook variable.
777 Preserve only the elements that want to be preserved. */
778 Lisp_Object list, newlist;
779 list = XCDR (XCAR (tmp));
780 if (!CONSP (list))
781 newlist = list;
782 else
783 for (newlist = Qnil; CONSP (list); list = XCDR (list))
785 Lisp_Object elt = XCAR (list);
786 /* Preserve element ELT if it's t,
787 if it is a function with a `permanent-local-hook' property,
788 or if it's not a symbol. */
789 if (! SYMBOLP (elt)
790 || EQ (elt, Qt)
791 || !NILP (Fget (elt, Qpermanent_local_hook)))
792 newlist = Fcons (elt, newlist);
794 XSETCDR (XCAR (tmp), Fnreverse (newlist));
797 /* Delete this local variable. */
798 else if (NILP (last))
799 b->local_var_alist = XCDR (tmp);
800 else
801 XSETCDR (last, XCDR (tmp));
804 for (i = 0; i < last_per_buffer_idx; ++i)
805 if (permanent_too || buffer_permanent_local_flags[i] == 0)
806 SET_PER_BUFFER_VALUE_P (b, i, 0);
808 /* For each slot that has a default value,
809 copy that into the slot. */
811 for (offset = PER_BUFFER_VAR_OFFSET (name);
812 offset < sizeof *b;
813 offset += sizeof (Lisp_Object))
815 int idx = PER_BUFFER_IDX (offset);
816 if ((idx > 0
817 && (permanent_too
818 || buffer_permanent_local_flags[idx] == 0))
819 /* Is -2 used anywhere? */
820 || idx == -2)
821 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
825 /* We split this away from generate-new-buffer, because rename-buffer
826 and set-visited-file-name ought to be able to use this to really
827 rename the buffer properly. */
829 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
830 1, 2, 0,
831 doc: /* Return a string that is the name of no existing buffer based on NAME.
832 If there is no live buffer named NAME, then return NAME.
833 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
834 \(starting at 2) until an unused name is found, and then return that name.
835 Optional second argument IGNORE specifies a name that is okay to use (if
836 it is in the sequence to be tried) even if a buffer with that name exists. */)
837 (name, ignore)
838 register Lisp_Object name, ignore;
840 register Lisp_Object gentemp, tem;
841 int count;
842 char number[10];
844 CHECK_STRING (name);
846 tem = Fstring_equal (name, ignore);
847 if (!NILP (tem))
848 return name;
849 tem = Fget_buffer (name);
850 if (NILP (tem))
851 return name;
853 count = 1;
854 while (1)
856 sprintf (number, "<%d>", ++count);
857 gentemp = concat2 (name, build_string (number));
858 tem = Fstring_equal (gentemp, ignore);
859 if (!NILP (tem))
860 return gentemp;
861 tem = Fget_buffer (gentemp);
862 if (NILP (tem))
863 return gentemp;
868 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
869 doc: /* Return the name of BUFFER, as a string.
870 With no argument or nil as argument, return the name of the current buffer. */)
871 (buffer)
872 register Lisp_Object buffer;
874 if (NILP (buffer))
875 return current_buffer->name;
876 CHECK_BUFFER (buffer);
877 return XBUFFER (buffer)->name;
880 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
881 doc: /* Return name of file BUFFER is visiting, or nil if none.
882 No argument or nil as argument means use the current buffer. */)
883 (buffer)
884 register Lisp_Object buffer;
886 if (NILP (buffer))
887 return current_buffer->filename;
888 CHECK_BUFFER (buffer);
889 return XBUFFER (buffer)->filename;
892 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
893 0, 1, 0,
894 doc: /* Return the base buffer of indirect buffer BUFFER.
895 If BUFFER is not indirect, return nil.
896 BUFFER defaults to the current buffer. */)
897 (buffer)
898 register Lisp_Object buffer;
900 struct buffer *base;
901 Lisp_Object base_buffer;
903 if (NILP (buffer))
904 base = current_buffer->base_buffer;
905 else
907 CHECK_BUFFER (buffer);
908 base = XBUFFER (buffer)->base_buffer;
911 if (! base)
912 return Qnil;
913 XSETBUFFER (base_buffer, base);
914 return base_buffer;
917 DEFUN ("buffer-local-value", Fbuffer_local_value,
918 Sbuffer_local_value, 2, 2, 0,
919 doc: /* Return the value of VARIABLE in BUFFER.
920 If VARIABLE does not have a buffer-local binding in BUFFER, the value
921 is the default binding of the variable. */)
922 (variable, buffer)
923 register Lisp_Object variable;
924 register Lisp_Object buffer;
926 register struct buffer *buf;
927 register Lisp_Object result;
929 CHECK_SYMBOL (variable);
930 CHECK_BUFFER (buffer);
931 buf = XBUFFER (buffer);
933 variable = indirect_variable (variable);
935 /* Look in local_var_list */
936 result = Fassoc (variable, buf->local_var_alist);
937 if (NILP (result))
939 int offset, idx;
940 int found = 0;
942 /* Look in special slots */
943 for (offset = PER_BUFFER_VAR_OFFSET (name);
944 offset < sizeof (struct buffer);
945 /* sizeof EMACS_INT == sizeof Lisp_Object */
946 offset += (sizeof (EMACS_INT)))
948 idx = PER_BUFFER_IDX (offset);
949 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
950 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
951 && EQ (PER_BUFFER_SYMBOL (offset), variable))
953 result = PER_BUFFER_VALUE (buf, offset);
954 found = 1;
955 break;
959 if (!found)
960 result = Fdefault_value (variable);
962 else
964 Lisp_Object valcontents;
965 Lisp_Object current_alist_element;
967 /* What binding is loaded right now? */
968 valcontents = SYMBOL_VALUE (variable);
969 current_alist_element
970 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
972 /* The value of the currently loaded binding is not
973 stored in it, but rather in the realvalue slot.
974 Store that value into the binding it belongs to
975 in case that is the one we are about to use. */
977 Fsetcdr (current_alist_element,
978 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
980 /* Now get the (perhaps updated) value out of the binding. */
981 result = XCDR (result);
984 if (!EQ (result, Qunbound))
985 return result;
987 xsignal1 (Qvoid_variable, variable);
990 /* Return an alist of the Lisp-level buffer-local bindings of
991 buffer BUF. That is, don't include the variables maintained
992 in special slots in the buffer object. */
994 static Lisp_Object
995 buffer_lisp_local_variables (buf)
996 struct buffer *buf;
998 Lisp_Object result = Qnil;
999 register Lisp_Object tail;
1000 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1002 Lisp_Object val, elt;
1004 elt = XCAR (tail);
1006 /* Reference each variable in the alist in buf.
1007 If inquiring about the current buffer, this gets the current values,
1008 so store them into the alist so the alist is up to date.
1009 If inquiring about some other buffer, this swaps out any values
1010 for that buffer, making the alist up to date automatically. */
1011 val = find_symbol_value (XCAR (elt));
1012 /* Use the current buffer value only if buf is the current buffer. */
1013 if (buf != current_buffer)
1014 val = XCDR (elt);
1016 /* If symbol is unbound, put just the symbol in the list. */
1017 if (EQ (val, Qunbound))
1018 result = Fcons (XCAR (elt), result);
1019 /* Otherwise, put (symbol . value) in the list. */
1020 else
1021 result = Fcons (Fcons (XCAR (elt), val), result);
1024 return result;
1027 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1028 Sbuffer_local_variables, 0, 1, 0,
1029 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1030 Most elements look like (SYMBOL . VALUE), describing one variable.
1031 For a symbol that is locally unbound, just the symbol appears in the value.
1032 Note that storing new VALUEs in these elements doesn't change the variables.
1033 No argument or nil as argument means use current buffer as BUFFER. */)
1034 (buffer)
1035 register Lisp_Object buffer;
1037 register struct buffer *buf;
1038 register Lisp_Object result;
1040 if (NILP (buffer))
1041 buf = current_buffer;
1042 else
1044 CHECK_BUFFER (buffer);
1045 buf = XBUFFER (buffer);
1048 result = buffer_lisp_local_variables (buf);
1050 /* Add on all the variables stored in special slots. */
1052 int offset, idx;
1054 for (offset = PER_BUFFER_VAR_OFFSET (name);
1055 offset < sizeof (struct buffer);
1056 /* sizeof EMACS_INT == sizeof Lisp_Object */
1057 offset += (sizeof (EMACS_INT)))
1059 idx = PER_BUFFER_IDX (offset);
1060 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1061 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1062 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1063 PER_BUFFER_VALUE (buf, offset)),
1064 result);
1068 return result;
1071 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1072 0, 1, 0,
1073 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1074 No argument or nil as argument means use current buffer as BUFFER. */)
1075 (buffer)
1076 register Lisp_Object buffer;
1078 register struct buffer *buf;
1079 if (NILP (buffer))
1080 buf = current_buffer;
1081 else
1083 CHECK_BUFFER (buffer);
1084 buf = XBUFFER (buffer);
1087 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1090 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1091 1, 1, 0,
1092 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1093 A non-nil FLAG means mark the buffer modified. */)
1094 (flag)
1095 register Lisp_Object flag;
1097 register int already;
1098 register Lisp_Object fn;
1099 Lisp_Object buffer, window;
1101 #ifdef CLASH_DETECTION
1102 /* If buffer becoming modified, lock the file.
1103 If buffer becoming unmodified, unlock the file. */
1105 fn = current_buffer->file_truename;
1106 /* Test buffer-file-name so that binding it to nil is effective. */
1107 if (!NILP (fn) && ! NILP (current_buffer->filename))
1109 already = SAVE_MODIFF < MODIFF;
1110 if (!already && !NILP (flag))
1111 lock_file (fn);
1112 else if (already && NILP (flag))
1113 unlock_file (fn);
1115 #endif /* CLASH_DETECTION */
1117 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1119 /* Set update_mode_lines only if buffer is displayed in some window.
1120 Packages like jit-lock or lazy-lock preserve a buffer's modified
1121 state by recording/restoring the state around blocks of code.
1122 Setting update_mode_lines makes redisplay consider all windows
1123 (on all frames). Stealth fontification of buffers not displayed
1124 would incur additional redisplay costs if we'd set
1125 update_modes_lines unconditionally.
1127 Ideally, I think there should be another mechanism for fontifying
1128 buffers without "modifying" buffers, or redisplay should be
1129 smarter about updating the `*' in mode lines. --gerd */
1130 XSETBUFFER (buffer, current_buffer);
1131 window = Fget_buffer_window (buffer, Qt);
1132 if (WINDOWP (window))
1134 ++update_mode_lines;
1135 current_buffer->prevent_redisplay_optimizations_p = 1;
1138 return flag;
1141 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1142 Srestore_buffer_modified_p, 1, 1, 0,
1143 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1144 It is not ensured that mode lines will be updated to show the modified
1145 state of the current buffer. Use with care. */)
1146 (flag)
1147 Lisp_Object flag;
1149 #ifdef CLASH_DETECTION
1150 Lisp_Object fn;
1152 /* If buffer becoming modified, lock the file.
1153 If buffer becoming unmodified, unlock the file. */
1155 fn = current_buffer->file_truename;
1156 /* Test buffer-file-name so that binding it to nil is effective. */
1157 if (!NILP (fn) && ! NILP (current_buffer->filename))
1159 int already = SAVE_MODIFF < MODIFF;
1160 if (!already && !NILP (flag))
1161 lock_file (fn);
1162 else if (already && NILP (flag))
1163 unlock_file (fn);
1165 #endif /* CLASH_DETECTION */
1167 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1168 return flag;
1171 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1172 0, 1, 0,
1173 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1174 Each buffer has a tick counter which is incremented each time the
1175 text in that buffer is changed. It wraps around occasionally.
1176 No argument or nil as argument means use current buffer as BUFFER. */)
1177 (buffer)
1178 register Lisp_Object buffer;
1180 register struct buffer *buf;
1181 if (NILP (buffer))
1182 buf = current_buffer;
1183 else
1185 CHECK_BUFFER (buffer);
1186 buf = XBUFFER (buffer);
1189 return make_number (BUF_MODIFF (buf));
1192 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1193 Sbuffer_chars_modified_tick, 0, 1, 0,
1194 doc: /* Return BUFFER's character-change tick counter.
1195 Each buffer has a character-change tick counter, which is set to the
1196 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1197 time text in that buffer is inserted or deleted. By comparing the
1198 values returned by two individual calls of `buffer-chars-modified-tick',
1199 you can tell whether a character change occurred in that buffer in
1200 between these calls. No argument or nil as argument means use current
1201 buffer as BUFFER. */)
1202 (buffer)
1203 register Lisp_Object buffer;
1205 register struct buffer *buf;
1206 if (NILP (buffer))
1207 buf = current_buffer;
1208 else
1210 CHECK_BUFFER (buffer);
1211 buf = XBUFFER (buffer);
1214 return make_number (BUF_CHARS_MODIFF (buf));
1217 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1218 "(list (read-string \"Rename buffer (to new name): \" \
1219 nil 'buffer-name-history (buffer-name (current-buffer))) \
1220 current-prefix-arg)",
1221 doc: /* Change current buffer's name to NEWNAME (a string).
1222 If second arg UNIQUE is nil or omitted, it is an error if a
1223 buffer named NEWNAME already exists.
1224 If UNIQUE is non-nil, come up with a new name using
1225 `generate-new-buffer-name'.
1226 Interactively, you can set UNIQUE with a prefix argument.
1227 We return the name we actually gave the buffer.
1228 This does not change the name of the visited file (if any). */)
1229 (newname, unique)
1230 register Lisp_Object newname, unique;
1232 register Lisp_Object tem, buf;
1234 CHECK_STRING (newname);
1236 if (SCHARS (newname) == 0)
1237 error ("Empty string is invalid as a buffer name");
1239 tem = Fget_buffer (newname);
1240 if (!NILP (tem))
1242 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1243 rename the buffer automatically so you can create another
1244 with the original name. It makes UNIQUE equivalent to
1245 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1246 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1247 return current_buffer->name;
1248 if (!NILP (unique))
1249 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1250 else
1251 error ("Buffer name `%s' is in use", SDATA (newname));
1254 current_buffer->name = newname;
1256 /* Catch redisplay's attention. Unless we do this, the mode lines for
1257 any windows displaying current_buffer will stay unchanged. */
1258 update_mode_lines++;
1260 XSETBUFFER (buf, current_buffer);
1261 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1262 if (NILP (current_buffer->filename)
1263 && !NILP (current_buffer->auto_save_file_name))
1264 call0 (intern ("rename-auto-save-file"));
1265 /* Refetch since that last call may have done GC. */
1266 return current_buffer->name;
1269 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1270 doc: /* Return most recently selected buffer other than BUFFER.
1271 Buffers not visible in windows are preferred to visible buffers,
1272 unless optional second argument VISIBLE-OK is non-nil.
1273 If the optional third argument FRAME is non-nil, use that frame's
1274 buffer list instead of the selected frame's buffer list.
1275 If no other buffer exists, the buffer `*scratch*' is returned.
1276 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1277 (buffer, visible_ok, frame)
1278 register Lisp_Object buffer, visible_ok, frame;
1280 Lisp_Object Fset_buffer_major_mode ();
1281 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1282 notsogood = Qnil;
1284 if (NILP (frame))
1285 frame = selected_frame;
1287 tail = Vbuffer_alist;
1288 pred = frame_buffer_predicate (frame);
1290 /* Consider buffers that have been seen in the selected frame
1291 before other buffers. */
1293 tem = frame_buffer_list (frame);
1294 add_ons = Qnil;
1295 while (CONSP (tem))
1297 if (BUFFERP (XCAR (tem)))
1298 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1299 tem = XCDR (tem);
1301 tail = nconc2 (Fnreverse (add_ons), tail);
1303 for (; CONSP (tail); tail = XCDR (tail))
1305 buf = Fcdr (XCAR (tail));
1306 if (EQ (buf, buffer))
1307 continue;
1308 if (NILP (buf))
1309 continue;
1310 if (NILP (XBUFFER (buf)->name))
1311 continue;
1312 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1313 continue;
1314 /* If the selected frame has a buffer_predicate,
1315 disregard buffers that don't fit the predicate. */
1316 if (!NILP (pred))
1318 tem = call1 (pred, buf);
1319 if (NILP (tem))
1320 continue;
1323 if (NILP (visible_ok))
1324 tem = Fget_buffer_window (buf, Qvisible);
1325 else
1326 tem = Qnil;
1327 if (NILP (tem))
1328 return buf;
1329 if (NILP (notsogood))
1330 notsogood = buf;
1332 if (!NILP (notsogood))
1333 return notsogood;
1334 buf = Fget_buffer (build_string ("*scratch*"));
1335 if (NILP (buf))
1337 buf = Fget_buffer_create (build_string ("*scratch*"));
1338 Fset_buffer_major_mode (buf);
1340 return buf;
1343 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1344 0, 1, "",
1345 doc: /* Start keeping undo information for buffer BUFFER.
1346 No argument or nil as argument means do this for the current buffer. */)
1347 (buffer)
1348 register Lisp_Object buffer;
1350 Lisp_Object real_buffer;
1352 if (NILP (buffer))
1353 XSETBUFFER (real_buffer, current_buffer);
1354 else
1356 real_buffer = Fget_buffer (buffer);
1357 if (NILP (real_buffer))
1358 nsberror (buffer);
1361 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1362 XBUFFER (real_buffer)->undo_list = Qnil;
1364 return Qnil;
1368 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1369 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1370 The buffer being killed will be current while the hook is running.\n\
1371 See `kill-buffer'."
1373 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1374 doc: /* Kill the buffer BUFFER.
1375 The argument may be a buffer or the name of a buffer.
1376 With a nil argument, kill the current buffer.
1378 Value is t if the buffer is actually killed, nil otherwise.
1380 The functions in `kill-buffer-query-functions' are called with BUFFER as
1381 the current buffer. If any of them returns nil, the buffer is not killed.
1383 The hook `kill-buffer-hook' is run before the buffer is actually killed.
1384 The buffer being killed will be current while the hook is running.
1386 Any processes that have this buffer as the `process-buffer' are killed
1387 with SIGHUP. */)
1388 (buffer)
1389 Lisp_Object buffer;
1391 Lisp_Object buf;
1392 register struct buffer *b;
1393 register Lisp_Object tem;
1394 register struct Lisp_Marker *m;
1395 struct gcpro gcpro1;
1397 if (NILP (buffer))
1398 buf = Fcurrent_buffer ();
1399 else
1400 buf = Fget_buffer (buffer);
1401 if (NILP (buf))
1402 nsberror (buffer);
1404 b = XBUFFER (buf);
1406 /* Avoid trouble for buffer already dead. */
1407 if (NILP (b->name))
1408 return Qnil;
1410 /* Query if the buffer is still modified. */
1411 if (INTERACTIVE && !NILP (b->filename)
1412 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1414 GCPRO1 (buf);
1415 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1416 b->name, make_number (0)));
1417 UNGCPRO;
1418 if (NILP (tem))
1419 return Qnil;
1422 /* Run hooks with the buffer to be killed the current buffer. */
1424 int count = SPECPDL_INDEX ();
1425 Lisp_Object arglist[1];
1427 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1428 set_buffer_internal (b);
1430 /* First run the query functions; if any query is answered no,
1431 don't kill the buffer. */
1432 arglist[0] = Qkill_buffer_query_functions;
1433 tem = Frun_hook_with_args_until_failure (1, arglist);
1434 if (NILP (tem))
1435 return unbind_to (count, Qnil);
1437 /* Then run the hooks. */
1438 Frun_hooks (1, &Qkill_buffer_hook);
1439 unbind_to (count, Qnil);
1442 /* We have no more questions to ask. Verify that it is valid
1443 to kill the buffer. This must be done after the questions
1444 since anything can happen within do_yes_or_no_p. */
1446 /* Don't kill the minibuffer now current. */
1447 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1448 return Qnil;
1450 if (NILP (b->name))
1451 return Qnil;
1453 /* When we kill a base buffer, kill all its indirect buffers.
1454 We do it at this stage so nothing terrible happens if they
1455 ask questions or their hooks get errors. */
1456 if (! b->base_buffer)
1458 struct buffer *other;
1460 GCPRO1 (buf);
1462 for (other = all_buffers; other; other = other->next)
1463 /* all_buffers contains dead buffers too;
1464 don't re-kill them. */
1465 if (other->base_buffer == b && !NILP (other->name))
1467 Lisp_Object buf;
1468 XSETBUFFER (buf, other);
1469 Fkill_buffer (buf);
1472 UNGCPRO;
1475 /* Make this buffer not be current.
1476 In the process, notice if this is the sole visible buffer
1477 and give up if so. */
1478 if (b == current_buffer)
1480 tem = Fother_buffer (buf, Qnil, Qnil);
1481 Fset_buffer (tem);
1482 if (b == current_buffer)
1483 return Qnil;
1486 /* Notice if the buffer to kill is the sole visible buffer
1487 when we're currently in the mini-buffer, and give up if so. */
1488 XSETBUFFER (tem, current_buffer);
1489 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1491 tem = Fother_buffer (buf, Qnil, Qnil);
1492 if (EQ (buf, tem))
1493 return Qnil;
1496 /* Now there is no question: we can kill the buffer. */
1498 #ifdef CLASH_DETECTION
1499 /* Unlock this buffer's file, if it is locked. */
1500 unlock_buffer (b);
1501 #endif /* CLASH_DETECTION */
1503 GCPRO1 (buf);
1504 kill_buffer_processes (buf);
1505 UNGCPRO;
1507 /* Killing buffer processes may run sentinels which may
1508 have called kill-buffer. */
1510 if (NILP (b->name))
1511 return Qnil;
1513 clear_charpos_cache (b);
1515 tem = Vinhibit_quit;
1516 Vinhibit_quit = Qt;
1517 replace_buffer_in_all_windows (buf);
1518 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1519 frames_discard_buffer (buf);
1520 Vinhibit_quit = tem;
1522 /* Delete any auto-save file, if we saved it in this session.
1523 But not if the buffer is modified. */
1524 if (STRINGP (b->auto_save_file_name)
1525 && b->auto_save_modified != 0
1526 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1527 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1528 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1530 Lisp_Object tem;
1531 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1532 if (! NILP (tem))
1533 internal_delete_file (b->auto_save_file_name);
1536 if (b->base_buffer)
1538 /* Unchain all markers that belong to this indirect buffer.
1539 Don't unchain the markers that belong to the base buffer
1540 or its other indirect buffers. */
1541 for (m = BUF_MARKERS (b); m; )
1543 struct Lisp_Marker *next = m->next;
1544 if (m->buffer == b)
1545 unchain_marker (m);
1546 m = next;
1549 else
1551 /* Unchain all markers of this buffer and its indirect buffers.
1552 and leave them pointing nowhere. */
1553 for (m = BUF_MARKERS (b); m; )
1555 struct Lisp_Marker *next = m->next;
1556 m->buffer = 0;
1557 m->next = NULL;
1558 m = next;
1560 BUF_MARKERS (b) = NULL;
1561 BUF_INTERVALS (b) = NULL_INTERVAL;
1563 /* Perhaps we should explicitly free the interval tree here... */
1566 /* Reset the local variables, so that this buffer's local values
1567 won't be protected from GC. They would be protected
1568 if they happened to remain encached in their symbols.
1569 This gets rid of them for certain. */
1570 swap_out_buffer_local_variables (b);
1571 reset_buffer_local_variables (b, 1);
1573 b->name = Qnil;
1575 BLOCK_INPUT;
1576 if (! b->base_buffer)
1577 free_buffer_text (b);
1579 if (b->newline_cache)
1581 free_region_cache (b->newline_cache);
1582 b->newline_cache = 0;
1584 if (b->width_run_cache)
1586 free_region_cache (b->width_run_cache);
1587 b->width_run_cache = 0;
1589 b->width_table = Qnil;
1590 UNBLOCK_INPUT;
1591 b->undo_list = Qnil;
1593 return Qt;
1596 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1597 we do this each time BUF is selected visibly, the more recently
1598 selected buffers are always closer to the front of the list. This
1599 means that other_buffer is more likely to choose a relevant buffer. */
1601 void
1602 record_buffer (buf)
1603 Lisp_Object buf;
1605 register Lisp_Object link, prev;
1606 Lisp_Object frame;
1607 frame = selected_frame;
1609 prev = Qnil;
1610 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1612 if (EQ (XCDR (XCAR (link)), buf))
1613 break;
1614 prev = link;
1617 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1618 we cannot use Fdelq itself here because it allows quitting. */
1620 if (NILP (prev))
1621 Vbuffer_alist = XCDR (Vbuffer_alist);
1622 else
1623 XSETCDR (prev, XCDR (XCDR (prev)));
1625 XSETCDR (link, Vbuffer_alist);
1626 Vbuffer_alist = link;
1628 /* Effectively do a delq on buried_buffer_list. */
1630 prev = Qnil;
1631 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1632 link = XCDR (link))
1634 if (EQ (XCAR (link), buf))
1636 if (NILP (prev))
1637 XFRAME (frame)->buried_buffer_list = XCDR (link);
1638 else
1639 XSETCDR (prev, XCDR (XCDR (prev)));
1640 break;
1642 prev = link;
1645 /* Now move this buffer to the front of frame_buffer_list also. */
1647 prev = Qnil;
1648 for (link = frame_buffer_list (frame); CONSP (link);
1649 link = XCDR (link))
1651 if (EQ (XCAR (link), buf))
1652 break;
1653 prev = link;
1656 /* Effectively do delq. */
1658 if (CONSP (link))
1660 if (NILP (prev))
1661 set_frame_buffer_list (frame,
1662 XCDR (frame_buffer_list (frame)));
1663 else
1664 XSETCDR (prev, XCDR (XCDR (prev)));
1666 XSETCDR (link, frame_buffer_list (frame));
1667 set_frame_buffer_list (frame, link);
1669 else
1670 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1673 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1674 doc: /* Set an appropriate major mode for BUFFER.
1675 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1676 according to `default-major-mode'.
1677 Use this function before selecting the buffer, since it may need to inspect
1678 the current buffer's major mode. */)
1679 (buffer)
1680 Lisp_Object buffer;
1682 int count;
1683 Lisp_Object function;
1685 CHECK_BUFFER (buffer);
1687 if (STRINGP (XBUFFER (buffer)->name)
1688 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1689 function = find_symbol_value (intern ("initial-major-mode"));
1690 else
1692 function = buffer_defaults.major_mode;
1693 if (NILP (function)
1694 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1695 function = current_buffer->major_mode;
1698 if (NILP (function) || EQ (function, Qfundamental_mode))
1699 return Qnil;
1701 count = SPECPDL_INDEX ();
1703 /* To select a nonfundamental mode,
1704 select the buffer temporarily and then call the mode function. */
1706 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1708 Fset_buffer (buffer);
1709 call0 (function);
1711 return unbind_to (count, Qnil);
1714 /* If switching buffers in WINDOW would be an error, return
1715 a C string saying what the error would be. */
1717 char *
1718 no_switch_window (window)
1719 Lisp_Object window;
1721 Lisp_Object tem;
1722 if (EQ (minibuf_window, window))
1723 return "Cannot switch buffers in minibuffer window";
1724 tem = Fwindow_dedicated_p (window);
1725 if (EQ (tem, Qt))
1726 return "Cannot switch buffers in a dedicated window";
1727 return NULL;
1730 /* Switch to buffer BUFFER in the selected window.
1731 If NORECORD is non-nil, don't call record_buffer. */
1733 Lisp_Object
1734 switch_to_buffer_1 (buffer, norecord)
1735 Lisp_Object buffer, norecord;
1737 register Lisp_Object buf;
1739 if (NILP (buffer))
1740 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1741 else
1743 buf = Fget_buffer (buffer);
1744 if (NILP (buf))
1746 buf = Fget_buffer_create (buffer);
1747 Fset_buffer_major_mode (buf);
1750 Fset_buffer (buf);
1751 if (NILP (norecord))
1752 record_buffer (buf);
1754 Fset_window_buffer (EQ (selected_window, minibuf_window)
1755 ? Fnext_window (minibuf_window, Qnil, Qnil)
1756 : selected_window,
1757 buf, Qnil);
1759 return buf;
1762 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1763 doc: /* Select buffer BUFFER in the current window.
1764 If BUFFER does not identify an existing buffer,
1765 then this function creates a buffer with that name.
1767 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1768 or nil. If BUFFER is nil, then this function chooses a buffer
1769 using `other-buffer'.
1770 Optional second arg NORECORD non-nil means
1771 do not put this buffer at the front of the list of recently selected ones.
1772 This function returns the buffer it switched to.
1774 WARNING: This is NOT the way to work on another buffer temporarily
1775 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1776 the window-buffer correspondences. */)
1777 (buffer, norecord)
1778 Lisp_Object buffer, norecord;
1780 char *err;
1782 if (EQ (buffer, Fwindow_buffer (selected_window)))
1784 /* Basically a NOP. Avoid signalling an error in the case where
1785 the selected window is dedicated, or a minibuffer. */
1787 /* But do put this buffer at the front of the buffer list,
1788 unless that has been inhibited. Note that even if
1789 BUFFER is at the front of the main buffer-list already,
1790 we still want to move it to the front of the frame's buffer list. */
1791 if (NILP (norecord))
1792 record_buffer (buffer);
1793 return Fset_buffer (buffer);
1796 err = no_switch_window (selected_window);
1797 if (err) error (err);
1799 return switch_to_buffer_1 (buffer, norecord);
1802 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1803 doc: /* Select buffer BUFFER in some window, preferably a different one.
1804 BUFFER may be a buffer, a string \(a buffer name), or nil.
1805 If BUFFER is a string which is not the name of an existing buffer,
1806 then this function creates a buffer with that name.
1807 If BUFFER is nil, then it chooses some other buffer.
1808 If `pop-up-windows' is non-nil, windows can be split to do this.
1809 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1810 window even if BUFFER is already visible in the selected window,
1811 and ignore `same-window-regexps' and `same-window-buffer-names'.
1812 This function returns the buffer it switched to.
1813 This uses the function `display-buffer' as a subroutine; see the documentation
1814 of `display-buffer' for additional customization information.
1816 Optional third arg NORECORD non-nil means
1817 do not put this buffer at the front of the list of recently selected ones. */)
1818 (buffer, other_window, norecord)
1819 Lisp_Object buffer, other_window, norecord;
1821 register Lisp_Object buf;
1822 if (NILP (buffer))
1823 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1824 else
1826 buf = Fget_buffer (buffer);
1827 if (NILP (buf))
1829 buf = Fget_buffer_create (buffer);
1830 Fset_buffer_major_mode (buf);
1833 Fset_buffer (buf);
1834 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1835 return buf;
1838 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1839 doc: /* Return the current buffer as a Lisp object. */)
1842 register Lisp_Object buf;
1843 XSETBUFFER (buf, current_buffer);
1844 return buf;
1847 /* Set the current buffer to B.
1849 We previously set windows_or_buffers_changed here to invalidate
1850 global unchanged information in beg_unchanged and end_unchanged.
1851 This is no longer necessary because we now compute unchanged
1852 information on a buffer-basis. Every action affecting other
1853 windows than the selected one requires a select_window at some
1854 time, and that increments windows_or_buffers_changed. */
1856 void
1857 set_buffer_internal (b)
1858 register struct buffer *b;
1860 if (current_buffer != b)
1861 set_buffer_internal_1 (b);
1864 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1865 This is used by redisplay. */
1867 void
1868 set_buffer_internal_1 (b)
1869 register struct buffer *b;
1871 register struct buffer *old_buf;
1872 register Lisp_Object tail, valcontents;
1873 Lisp_Object tem;
1875 #ifdef USE_MMAP_FOR_BUFFERS
1876 if (b->text->beg == NULL)
1877 enlarge_buffer_text (b, 0);
1878 #endif /* USE_MMAP_FOR_BUFFERS */
1880 if (current_buffer == b)
1881 return;
1883 old_buf = current_buffer;
1884 current_buffer = b;
1885 last_known_column_point = -1; /* invalidate indentation cache */
1887 if (old_buf)
1889 /* Put the undo list back in the base buffer, so that it appears
1890 that an indirect buffer shares the undo list of its base. */
1891 if (old_buf->base_buffer)
1892 old_buf->base_buffer->undo_list = old_buf->undo_list;
1894 /* If the old current buffer has markers to record PT, BEGV and ZV
1895 when it is not current, update them now. */
1896 if (! NILP (old_buf->pt_marker))
1898 Lisp_Object obuf;
1899 XSETBUFFER (obuf, old_buf);
1900 set_marker_both (old_buf->pt_marker, obuf,
1901 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1903 if (! NILP (old_buf->begv_marker))
1905 Lisp_Object obuf;
1906 XSETBUFFER (obuf, old_buf);
1907 set_marker_both (old_buf->begv_marker, obuf,
1908 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1910 if (! NILP (old_buf->zv_marker))
1912 Lisp_Object obuf;
1913 XSETBUFFER (obuf, old_buf);
1914 set_marker_both (old_buf->zv_marker, obuf,
1915 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1919 /* Get the undo list from the base buffer, so that it appears
1920 that an indirect buffer shares the undo list of its base. */
1921 if (b->base_buffer)
1922 b->undo_list = b->base_buffer->undo_list;
1924 /* If the new current buffer has markers to record PT, BEGV and ZV
1925 when it is not current, fetch them now. */
1926 if (! NILP (b->pt_marker))
1928 BUF_PT (b) = marker_position (b->pt_marker);
1929 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1931 if (! NILP (b->begv_marker))
1933 BUF_BEGV (b) = marker_position (b->begv_marker);
1934 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1936 if (! NILP (b->zv_marker))
1938 BUF_ZV (b) = marker_position (b->zv_marker);
1939 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1942 /* Look down buffer's list of local Lisp variables
1943 to find and update any that forward into C variables. */
1945 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1947 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1948 if ((BUFFER_LOCAL_VALUEP (valcontents))
1949 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1950 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1951 /* Just reference the variable
1952 to cause it to become set for this buffer. */
1953 Fsymbol_value (XCAR (XCAR (tail)));
1956 /* Do the same with any others that were local to the previous buffer */
1958 if (old_buf)
1959 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1961 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1962 if ((BUFFER_LOCAL_VALUEP (valcontents))
1963 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1964 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1965 /* Just reference the variable
1966 to cause it to become set for this buffer. */
1967 Fsymbol_value (XCAR (XCAR (tail)));
1971 /* Switch to buffer B temporarily for redisplay purposes.
1972 This avoids certain things that don't need to be done within redisplay. */
1974 void
1975 set_buffer_temp (b)
1976 struct buffer *b;
1978 register struct buffer *old_buf;
1980 if (current_buffer == b)
1981 return;
1983 old_buf = current_buffer;
1984 current_buffer = b;
1986 if (old_buf)
1988 /* If the old current buffer has markers to record PT, BEGV and ZV
1989 when it is not current, update them now. */
1990 if (! NILP (old_buf->pt_marker))
1992 Lisp_Object obuf;
1993 XSETBUFFER (obuf, old_buf);
1994 set_marker_both (old_buf->pt_marker, obuf,
1995 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1997 if (! NILP (old_buf->begv_marker))
1999 Lisp_Object obuf;
2000 XSETBUFFER (obuf, old_buf);
2001 set_marker_both (old_buf->begv_marker, obuf,
2002 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
2004 if (! NILP (old_buf->zv_marker))
2006 Lisp_Object obuf;
2007 XSETBUFFER (obuf, old_buf);
2008 set_marker_both (old_buf->zv_marker, obuf,
2009 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
2013 /* If the new current buffer has markers to record PT, BEGV and ZV
2014 when it is not current, fetch them now. */
2015 if (! NILP (b->pt_marker))
2017 BUF_PT (b) = marker_position (b->pt_marker);
2018 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
2020 if (! NILP (b->begv_marker))
2022 BUF_BEGV (b) = marker_position (b->begv_marker);
2023 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2025 if (! NILP (b->zv_marker))
2027 BUF_ZV (b) = marker_position (b->zv_marker);
2028 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2032 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2033 doc: /* Make the buffer BUFFER current for editing operations.
2034 BUFFER may be a buffer or the name of an existing buffer.
2035 See also `save-excursion' when you want to make a buffer current temporarily.
2036 This function does not display the buffer, so its effect ends
2037 when the current command terminates.
2038 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
2039 (buffer)
2040 register Lisp_Object buffer;
2042 register Lisp_Object buf;
2043 buf = Fget_buffer (buffer);
2044 if (NILP (buf))
2045 nsberror (buffer);
2046 if (NILP (XBUFFER (buf)->name))
2047 error ("Selecting deleted buffer");
2048 set_buffer_internal (XBUFFER (buf));
2049 return buf;
2052 /* Set the current buffer to BUFFER provided it is alive. */
2054 Lisp_Object
2055 set_buffer_if_live (buffer)
2056 Lisp_Object buffer;
2058 if (! NILP (XBUFFER (buffer)->name))
2059 Fset_buffer (buffer);
2060 return Qnil;
2063 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2064 Sbarf_if_buffer_read_only, 0, 0, 0,
2065 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2068 if (!NILP (current_buffer->read_only)
2069 && NILP (Vinhibit_read_only))
2070 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2071 return Qnil;
2074 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2075 doc: /* Put BUFFER at the end of the list of all buffers.
2076 There it is the least likely candidate for `other-buffer' to return;
2077 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
2078 You can specify a buffer name as BUFFER, or an actual buffer object.
2079 If BUFFER is nil or omitted, bury the current buffer.
2080 Also, if BUFFER is nil or omitted, remove the current buffer from the
2081 selected window if it is displayed there. */)
2082 (buffer)
2083 register Lisp_Object buffer;
2085 /* Figure out what buffer we're going to bury. */
2086 if (NILP (buffer))
2088 Lisp_Object tem;
2089 XSETBUFFER (buffer, current_buffer);
2091 tem = Fwindow_buffer (selected_window);
2092 /* If we're burying the current buffer, unshow it. */
2093 if (EQ (buffer, tem))
2095 if (NILP (Fwindow_dedicated_p (selected_window)))
2096 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2097 else if (NILP (XWINDOW (selected_window)->parent))
2098 Ficonify_frame (Fwindow_frame (selected_window));
2099 else
2100 Fdelete_window (selected_window);
2103 else
2105 Lisp_Object buf1;
2107 buf1 = Fget_buffer (buffer);
2108 if (NILP (buf1))
2109 nsberror (buffer);
2110 buffer = buf1;
2113 /* Move buffer to the end of the buffer list. Do nothing if the
2114 buffer is killed. */
2115 if (!NILP (XBUFFER (buffer)->name))
2117 Lisp_Object aelt, link;
2119 aelt = Frassq (buffer, Vbuffer_alist);
2120 link = Fmemq (aelt, Vbuffer_alist);
2121 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2122 XSETCDR (link, Qnil);
2123 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2125 XFRAME (selected_frame)->buffer_list
2126 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2127 XFRAME (selected_frame)->buried_buffer_list
2128 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2131 return Qnil;
2134 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2135 doc: /* Delete the entire contents of the current buffer.
2136 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2137 so the buffer is truly empty after this. */)
2140 Fwiden ();
2142 del_range (BEG, Z);
2144 current_buffer->last_window_start = 1;
2145 /* Prevent warnings, or suspension of auto saving, that would happen
2146 if future size is less than past size. Use of erase-buffer
2147 implies that the future text is not really related to the past text. */
2148 XSETFASTINT (current_buffer->save_length, 0);
2149 return Qnil;
2152 void
2153 validate_region (b, e)
2154 register Lisp_Object *b, *e;
2156 CHECK_NUMBER_COERCE_MARKER (*b);
2157 CHECK_NUMBER_COERCE_MARKER (*e);
2159 if (XINT (*b) > XINT (*e))
2161 Lisp_Object tem;
2162 tem = *b; *b = *e; *e = tem;
2165 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2166 && XINT (*e) <= ZV))
2167 args_out_of_range (*b, *e);
2170 /* Advance BYTE_POS up to a character boundary
2171 and return the adjusted position. */
2173 static int
2174 advance_to_char_boundary (byte_pos)
2175 int byte_pos;
2177 int c;
2179 if (byte_pos == BEG)
2180 /* Beginning of buffer is always a character boundary. */
2181 return BEG;
2183 c = FETCH_BYTE (byte_pos);
2184 if (! CHAR_HEAD_P (c))
2186 /* We should advance BYTE_POS only when C is a constituent of a
2187 multibyte sequence. */
2188 int orig_byte_pos = byte_pos;
2192 byte_pos--;
2193 c = FETCH_BYTE (byte_pos);
2195 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2196 INC_POS (byte_pos);
2197 if (byte_pos < orig_byte_pos)
2198 byte_pos = orig_byte_pos;
2199 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2200 surely advance to the correct character boundary. If C is
2201 not, BYTE_POS was unchanged. */
2204 return byte_pos;
2207 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2208 1, 1, 0,
2209 doc: /* Set the multibyte flag of the current buffer to FLAG.
2210 If FLAG is t, this makes the buffer a multibyte buffer.
2211 If FLAG is nil, this makes the buffer a single-byte buffer.
2212 In these cases, the buffer contents remain unchanged as a sequence of
2213 bytes but the contents viewed as characters do change.
2214 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2215 all eight-bit bytes to eight-bit characters.
2216 If the multibyte flag was really changed, undo information of the
2217 current buffer is cleared. */)
2218 (flag)
2219 Lisp_Object flag;
2221 struct Lisp_Marker *tail, *markers;
2222 struct buffer *other;
2223 int begv, zv;
2224 int narrowed = (BEG != BEGV || Z != ZV);
2225 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2226 Lisp_Object old_undo = current_buffer->undo_list;
2227 struct gcpro gcpro1;
2229 if (current_buffer->base_buffer)
2230 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2232 /* Do nothing if nothing actually changes. */
2233 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2234 return flag;
2236 GCPRO1 (old_undo);
2238 /* Don't record these buffer changes. We will put a special undo entry
2239 instead. */
2240 current_buffer->undo_list = Qt;
2242 /* If the cached position is for this buffer, clear it out. */
2243 clear_charpos_cache (current_buffer);
2245 if (NILP (flag))
2246 begv = BEGV_BYTE, zv = ZV_BYTE;
2247 else
2248 begv = BEGV, zv = ZV;
2250 if (narrowed)
2251 Fwiden ();
2253 if (NILP (flag))
2255 int pos, stop;
2256 unsigned char *p;
2258 /* Do this first, so it can use CHAR_TO_BYTE
2259 to calculate the old correspondences. */
2260 set_intervals_multibyte (0);
2262 current_buffer->enable_multibyte_characters = Qnil;
2264 Z = Z_BYTE;
2265 BEGV = BEGV_BYTE;
2266 ZV = ZV_BYTE;
2267 GPT = GPT_BYTE;
2268 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2271 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2272 tail->charpos = tail->bytepos;
2274 /* Convert multibyte form of 8-bit characters to unibyte. */
2275 pos = BEG;
2276 stop = GPT;
2277 p = BEG_ADDR;
2278 while (1)
2280 int c, bytes;
2282 if (pos == stop)
2284 if (pos == Z)
2285 break;
2286 p = GAP_END_ADDR;
2287 stop = Z;
2289 if (ASCII_BYTE_P (*p))
2290 p++, pos++;
2291 else if (CHAR_BYTE8_HEAD_P (*p))
2293 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
2294 /* Delete all bytes for this 8-bit character but the
2295 last one, and change the last one to the charcter
2296 code. */
2297 bytes--;
2298 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2299 p = GAP_END_ADDR;
2300 *p++ = c;
2301 pos++;
2302 if (begv > pos)
2303 begv -= bytes;
2304 if (zv > pos)
2305 zv -= bytes;
2306 stop = Z;
2308 else
2310 bytes = BYTES_BY_CHAR_HEAD (*p);
2311 p += bytes, pos += bytes;
2314 if (narrowed)
2315 Fnarrow_to_region (make_number (begv), make_number (zv));
2317 else
2319 int pt = PT;
2320 int pos, stop;
2321 unsigned char *p, *pend;
2323 /* Be sure not to have a multibyte sequence striding over the GAP.
2324 Ex: We change this: "...abc\302 _GAP_ \241def..."
2325 to: "...abc _GAP_ \302\241def..." */
2327 if (EQ (flag, Qt)
2328 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2329 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2331 unsigned char *p = GPT_ADDR - 1;
2333 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2334 if (BASE_LEADING_CODE_P (*p))
2336 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2338 move_gap_both (new_gpt, new_gpt);
2342 /* Make the buffer contents valid as multibyte by converting
2343 8-bit characters to multibyte form. */
2344 pos = BEG;
2345 stop = GPT;
2346 p = BEG_ADDR;
2347 pend = GPT_ADDR;
2348 while (1)
2350 int bytes;
2352 if (pos == stop)
2354 if (pos == Z)
2355 break;
2356 p = GAP_END_ADDR;
2357 pend = Z_ADDR;
2358 stop = Z;
2361 if (ASCII_BYTE_P (*p))
2362 p++, pos++;
2363 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2364 p += bytes, pos += bytes;
2365 else
2367 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2368 int c;
2370 c = BYTE8_TO_CHAR (*p);
2371 bytes = CHAR_STRING (c, tmp);
2372 *p = tmp[0];
2373 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2374 bytes--;
2375 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2376 /* Now the gap is after the just inserted data. */
2377 pos = GPT;
2378 p = GAP_END_ADDR;
2379 if (pos <= begv)
2380 begv += bytes;
2381 if (pos <= zv)
2382 zv += bytes;
2383 if (pos <= pt)
2384 pt += bytes;
2385 pend = Z_ADDR;
2386 stop = Z;
2390 if (pt != PT)
2391 TEMP_SET_PT (pt);
2393 if (narrowed)
2394 Fnarrow_to_region (make_number (begv), make_number (zv));
2396 /* Do this first, so that chars_in_text asks the right question.
2397 set_intervals_multibyte needs it too. */
2398 current_buffer->enable_multibyte_characters = Qt;
2400 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2401 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2403 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2405 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2406 if (BEGV_BYTE > GPT_BYTE)
2407 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2408 else
2409 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2411 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2412 if (ZV_BYTE > GPT_BYTE)
2413 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2414 else
2415 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2418 int pt_byte = advance_to_char_boundary (PT_BYTE);
2419 int pt;
2421 if (pt_byte > GPT_BYTE)
2422 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2423 else
2424 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2425 TEMP_SET_PT_BOTH (pt, pt_byte);
2428 tail = markers = BUF_MARKERS (current_buffer);
2430 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2431 getting confused by the markers that have not yet been updated.
2432 It is also a signal that it should never create a marker. */
2433 BUF_MARKERS (current_buffer) = NULL;
2435 for (; tail; tail = tail->next)
2437 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2438 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2441 /* Make sure no markers were put on the chain
2442 while the chain value was incorrect. */
2443 if (BUF_MARKERS (current_buffer))
2444 abort ();
2446 BUF_MARKERS (current_buffer) = markers;
2448 /* Do this last, so it can calculate the new correspondences
2449 between chars and bytes. */
2450 set_intervals_multibyte (1);
2453 if (!EQ (old_undo, Qt))
2455 /* Represent all the above changes by a special undo entry. */
2456 extern Lisp_Object Qapply;
2457 current_buffer->undo_list = Fcons (list3 (Qapply,
2458 intern ("set-buffer-multibyte"),
2459 NILP (flag) ? Qt : Qnil),
2460 old_undo);
2463 UNGCPRO;
2465 /* Changing the multibyteness of a buffer means that all windows
2466 showing that buffer must be updated thoroughly. */
2467 current_buffer->prevent_redisplay_optimizations_p = 1;
2468 ++windows_or_buffers_changed;
2470 /* Copy this buffer's new multibyte status
2471 into all of its indirect buffers. */
2472 for (other = all_buffers; other; other = other->next)
2473 if (other->base_buffer == current_buffer && !NILP (other->name))
2475 other->enable_multibyte_characters
2476 = current_buffer->enable_multibyte_characters;
2477 other->prevent_redisplay_optimizations_p = 1;
2480 /* Restore the modifiedness of the buffer. */
2481 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2482 Fset_buffer_modified_p (Qnil);
2484 #ifdef subprocesses
2485 /* Update coding systems of this buffer's process (if any). */
2487 Lisp_Object process;
2489 process = Fget_buffer_process (Fcurrent_buffer ());
2490 if (PROCESSP (process))
2491 setup_process_coding_systems (process);
2493 #endif /* subprocesses */
2495 return flag;
2498 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2499 0, 0, 0,
2500 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2501 Most local variable bindings are eliminated so that the default values
2502 become effective once more. Also, the syntax table is set from
2503 `standard-syntax-table', the local keymap is set to nil,
2504 and the abbrev table from `fundamental-mode-abbrev-table'.
2505 This function also forces redisplay of the mode line.
2507 Every function to select a new major mode starts by
2508 calling this function.
2510 As a special exception, local variables whose names have
2511 a non-nil `permanent-local' property are not eliminated by this function.
2513 The first thing this function does is run
2514 the normal hook `change-major-mode-hook'. */)
2517 if (!NILP (Vrun_hooks))
2518 call1 (Vrun_hooks, Qchange_major_mode_hook);
2520 /* Make sure none of the bindings in local_var_alist
2521 remain swapped in, in their symbols. */
2523 swap_out_buffer_local_variables (current_buffer);
2525 /* Actually eliminate all local bindings of this buffer. */
2527 reset_buffer_local_variables (current_buffer, 0);
2529 /* Force mode-line redisplay. Useful here because all major mode
2530 commands call this function. */
2531 update_mode_lines++;
2533 return Qnil;
2536 /* Make sure no local variables remain set up with buffer B
2537 for their current values. */
2539 static void
2540 swap_out_buffer_local_variables (b)
2541 struct buffer *b;
2543 Lisp_Object oalist, alist, sym, tem, buffer;
2545 XSETBUFFER (buffer, b);
2546 oalist = b->local_var_alist;
2548 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2550 sym = XCAR (XCAR (alist));
2552 /* Need not do anything if some other buffer's binding is now encached. */
2553 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2554 if (EQ (tem, buffer))
2556 /* Symbol is set up for this buffer's old local value:
2557 swap it out! */
2558 swap_in_global_binding (sym);
2563 /* Find all the overlays in the current buffer that contain position POS.
2564 Return the number found, and store them in a vector in *VEC_PTR.
2565 Store in *LEN_PTR the size allocated for the vector.
2566 Store in *NEXT_PTR the next position after POS where an overlay starts,
2567 or ZV if there are no more overlays between POS and ZV.
2568 Store in *PREV_PTR the previous position before POS where an overlay ends,
2569 or where an overlay starts which ends at or after POS;
2570 or BEGV if there are no such overlays from BEGV to POS.
2571 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2573 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2574 when this function is called.
2576 If EXTEND is non-zero, we make the vector bigger if necessary.
2577 If EXTEND is zero, we never extend the vector,
2578 and we store only as many overlays as will fit.
2579 But we still return the total number of overlays.
2581 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2582 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2583 default (BEGV or ZV). */
2586 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2587 EMACS_INT pos;
2588 int extend;
2589 Lisp_Object **vec_ptr;
2590 int *len_ptr;
2591 int *next_ptr;
2592 int *prev_ptr;
2593 int change_req;
2595 Lisp_Object overlay, start, end;
2596 struct Lisp_Overlay *tail;
2597 int idx = 0;
2598 int len = *len_ptr;
2599 Lisp_Object *vec = *vec_ptr;
2600 int next = ZV;
2601 int prev = BEGV;
2602 int inhibit_storing = 0;
2604 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2606 int startpos, endpos;
2608 XSETMISC (overlay, tail);
2610 start = OVERLAY_START (overlay);
2611 end = OVERLAY_END (overlay);
2612 endpos = OVERLAY_POSITION (end);
2613 if (endpos < pos)
2615 if (prev < endpos)
2616 prev = endpos;
2617 break;
2619 startpos = OVERLAY_POSITION (start);
2620 /* This one ends at or after POS
2621 so its start counts for PREV_PTR if it's before POS. */
2622 if (prev < startpos && startpos < pos)
2623 prev = startpos;
2624 if (endpos == pos)
2625 continue;
2626 if (startpos <= pos)
2628 if (idx == len)
2630 /* The supplied vector is full.
2631 Either make it bigger, or don't store any more in it. */
2632 if (extend)
2634 /* Make it work with an initial len == 0. */
2635 len *= 2;
2636 if (len == 0)
2637 len = 4;
2638 *len_ptr = len;
2639 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2640 *vec_ptr = vec;
2642 else
2643 inhibit_storing = 1;
2646 if (!inhibit_storing)
2647 vec[idx] = overlay;
2648 /* Keep counting overlays even if we can't return them all. */
2649 idx++;
2651 else if (startpos < next)
2652 next = startpos;
2655 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2657 int startpos, endpos;
2659 XSETMISC (overlay, tail);
2661 start = OVERLAY_START (overlay);
2662 end = OVERLAY_END (overlay);
2663 startpos = OVERLAY_POSITION (start);
2664 if (pos < startpos)
2666 if (startpos < next)
2667 next = startpos;
2668 break;
2670 endpos = OVERLAY_POSITION (end);
2671 if (pos < endpos)
2673 if (idx == len)
2675 if (extend)
2677 /* Make it work with an initial len == 0. */
2678 len *= 2;
2679 if (len == 0)
2680 len = 4;
2681 *len_ptr = len;
2682 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2683 *vec_ptr = vec;
2685 else
2686 inhibit_storing = 1;
2689 if (!inhibit_storing)
2690 vec[idx] = overlay;
2691 idx++;
2693 if (startpos < pos && startpos > prev)
2694 prev = startpos;
2696 else if (endpos < pos && endpos > prev)
2697 prev = endpos;
2698 else if (endpos == pos && startpos > prev
2699 && (!change_req || startpos < pos))
2700 prev = startpos;
2703 if (next_ptr)
2704 *next_ptr = next;
2705 if (prev_ptr)
2706 *prev_ptr = prev;
2707 return idx;
2710 /* Find all the overlays in the current buffer that overlap the range BEG-END
2711 or are empty at BEG.
2713 Return the number found, and store them in a vector in *VEC_PTR.
2714 Store in *LEN_PTR the size allocated for the vector.
2715 Store in *NEXT_PTR the next position after POS where an overlay starts,
2716 or ZV if there are no more overlays.
2717 Store in *PREV_PTR the previous position before POS where an overlay ends,
2718 or BEGV if there are no previous overlays.
2719 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2721 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2722 when this function is called.
2724 If EXTEND is non-zero, we make the vector bigger if necessary.
2725 If EXTEND is zero, we never extend the vector,
2726 and we store only as many overlays as will fit.
2727 But we still return the total number of overlays. */
2729 static int
2730 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2731 int beg, end;
2732 int extend;
2733 Lisp_Object **vec_ptr;
2734 int *len_ptr;
2735 int *next_ptr;
2736 int *prev_ptr;
2738 Lisp_Object overlay, ostart, oend;
2739 struct Lisp_Overlay *tail;
2740 int idx = 0;
2741 int len = *len_ptr;
2742 Lisp_Object *vec = *vec_ptr;
2743 int next = ZV;
2744 int prev = BEGV;
2745 int inhibit_storing = 0;
2747 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2749 int startpos, endpos;
2751 XSETMISC (overlay, tail);
2753 ostart = OVERLAY_START (overlay);
2754 oend = OVERLAY_END (overlay);
2755 endpos = OVERLAY_POSITION (oend);
2756 if (endpos < beg)
2758 if (prev < endpos)
2759 prev = endpos;
2760 break;
2762 startpos = OVERLAY_POSITION (ostart);
2763 /* Count an interval if it either overlaps the range
2764 or is empty at the start of the range. */
2765 if ((beg < endpos && startpos < end)
2766 || (startpos == endpos && beg == endpos))
2768 if (idx == len)
2770 /* The supplied vector is full.
2771 Either make it bigger, or don't store any more in it. */
2772 if (extend)
2774 /* Make it work with an initial len == 0. */
2775 len *= 2;
2776 if (len == 0)
2777 len = 4;
2778 *len_ptr = len;
2779 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2780 *vec_ptr = vec;
2782 else
2783 inhibit_storing = 1;
2786 if (!inhibit_storing)
2787 vec[idx] = overlay;
2788 /* Keep counting overlays even if we can't return them all. */
2789 idx++;
2791 else if (startpos < next)
2792 next = startpos;
2795 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2797 int startpos, endpos;
2799 XSETMISC (overlay, tail);
2801 ostart = OVERLAY_START (overlay);
2802 oend = OVERLAY_END (overlay);
2803 startpos = OVERLAY_POSITION (ostart);
2804 if (end < startpos)
2806 if (startpos < next)
2807 next = startpos;
2808 break;
2810 endpos = OVERLAY_POSITION (oend);
2811 /* Count an interval if it either overlaps the range
2812 or is empty at the start of the range. */
2813 if ((beg < endpos && startpos < end)
2814 || (startpos == endpos && beg == endpos))
2816 if (idx == len)
2818 if (extend)
2820 /* Make it work with an initial len == 0. */
2821 len *= 2;
2822 if (len == 0)
2823 len = 4;
2824 *len_ptr = len;
2825 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2826 *vec_ptr = vec;
2828 else
2829 inhibit_storing = 1;
2832 if (!inhibit_storing)
2833 vec[idx] = overlay;
2834 idx++;
2836 else if (endpos < beg && endpos > prev)
2837 prev = endpos;
2840 if (next_ptr)
2841 *next_ptr = next;
2842 if (prev_ptr)
2843 *prev_ptr = prev;
2844 return idx;
2848 /* Return non-zero if there exists an overlay with a non-nil
2849 `mouse-face' property overlapping OVERLAY. */
2852 mouse_face_overlay_overlaps (overlay)
2853 Lisp_Object overlay;
2855 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2856 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2857 int n, i, size;
2858 Lisp_Object *v, tem;
2860 size = 10;
2861 v = (Lisp_Object *) alloca (size * sizeof *v);
2862 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2863 if (n > size)
2865 v = (Lisp_Object *) alloca (n * sizeof *v);
2866 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2869 for (i = 0; i < n; ++i)
2870 if (!EQ (v[i], overlay)
2871 && (tem = Foverlay_get (overlay, Qmouse_face),
2872 !NILP (tem)))
2873 break;
2875 return i < n;
2880 /* Fast function to just test if we're at an overlay boundary. */
2882 overlay_touches_p (pos)
2883 int pos;
2885 Lisp_Object overlay;
2886 struct Lisp_Overlay *tail;
2888 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2890 int endpos;
2892 XSETMISC (overlay ,tail);
2893 if (!OVERLAYP (overlay))
2894 abort ();
2896 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2897 if (endpos < pos)
2898 break;
2899 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2900 return 1;
2903 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2905 int startpos;
2907 XSETMISC (overlay, tail);
2908 if (!OVERLAYP (overlay))
2909 abort ();
2911 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2912 if (pos < startpos)
2913 break;
2914 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2915 return 1;
2917 return 0;
2920 struct sortvec
2922 Lisp_Object overlay;
2923 int beg, end;
2924 int priority;
2927 static int
2928 compare_overlays (v1, v2)
2929 const void *v1, *v2;
2931 const struct sortvec *s1 = (const struct sortvec *) v1;
2932 const struct sortvec *s2 = (const struct sortvec *) v2;
2933 if (s1->priority != s2->priority)
2934 return s1->priority - s2->priority;
2935 if (s1->beg != s2->beg)
2936 return s1->beg - s2->beg;
2937 if (s1->end != s2->end)
2938 return s2->end - s1->end;
2939 return 0;
2942 /* Sort an array of overlays by priority. The array is modified in place.
2943 The return value is the new size; this may be smaller than the original
2944 size if some of the overlays were invalid or were window-specific. */
2946 sort_overlays (overlay_vec, noverlays, w)
2947 Lisp_Object *overlay_vec;
2948 int noverlays;
2949 struct window *w;
2951 int i, j;
2952 struct sortvec *sortvec;
2953 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2955 /* Put the valid and relevant overlays into sortvec. */
2957 for (i = 0, j = 0; i < noverlays; i++)
2959 Lisp_Object tem;
2960 Lisp_Object overlay;
2962 overlay = overlay_vec[i];
2963 if (OVERLAY_VALID (overlay)
2964 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2965 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2967 /* If we're interested in a specific window, then ignore
2968 overlays that are limited to some other window. */
2969 if (w)
2971 Lisp_Object window;
2973 window = Foverlay_get (overlay, Qwindow);
2974 if (WINDOWP (window) && XWINDOW (window) != w)
2975 continue;
2978 /* This overlay is good and counts: put it into sortvec. */
2979 sortvec[j].overlay = overlay;
2980 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2981 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2982 tem = Foverlay_get (overlay, Qpriority);
2983 if (INTEGERP (tem))
2984 sortvec[j].priority = XINT (tem);
2985 else
2986 sortvec[j].priority = 0;
2987 j++;
2990 noverlays = j;
2992 /* Sort the overlays into the proper order: increasing priority. */
2994 if (noverlays > 1)
2995 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2997 for (i = 0; i < noverlays; i++)
2998 overlay_vec[i] = sortvec[i].overlay;
2999 return (noverlays);
3002 struct sortstr
3004 Lisp_Object string, string2;
3005 int size;
3006 int priority;
3009 struct sortstrlist
3011 struct sortstr *buf; /* An array that expands as needed; never freed. */
3012 int size; /* Allocated length of that array. */
3013 int used; /* How much of the array is currently in use. */
3014 int bytes; /* Total length of the strings in buf. */
3017 /* Buffers for storing information about the overlays touching a given
3018 position. These could be automatic variables in overlay_strings, but
3019 it's more efficient to hold onto the memory instead of repeatedly
3020 allocating and freeing it. */
3021 static struct sortstrlist overlay_heads, overlay_tails;
3022 static unsigned char *overlay_str_buf;
3024 /* Allocated length of overlay_str_buf. */
3025 static int overlay_str_len;
3027 /* A comparison function suitable for passing to qsort. */
3028 static int
3029 cmp_for_strings (as1, as2)
3030 char *as1, *as2;
3032 struct sortstr *s1 = (struct sortstr *)as1;
3033 struct sortstr *s2 = (struct sortstr *)as2;
3034 if (s1->size != s2->size)
3035 return s2->size - s1->size;
3036 if (s1->priority != s2->priority)
3037 return s1->priority - s2->priority;
3038 return 0;
3041 static void
3042 record_overlay_string (ssl, str, str2, pri, size)
3043 struct sortstrlist *ssl;
3044 Lisp_Object str, str2, pri;
3045 int size;
3047 int nbytes;
3049 if (ssl->used == ssl->size)
3051 if (ssl->buf)
3052 ssl->size *= 2;
3053 else
3054 ssl->size = 5;
3055 ssl->buf = ((struct sortstr *)
3056 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3058 ssl->buf[ssl->used].string = str;
3059 ssl->buf[ssl->used].string2 = str2;
3060 ssl->buf[ssl->used].size = size;
3061 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3062 ssl->used++;
3064 if (NILP (current_buffer->enable_multibyte_characters))
3065 nbytes = SCHARS (str);
3066 else if (! STRING_MULTIBYTE (str))
3067 nbytes = count_size_as_multibyte (SDATA (str),
3068 SBYTES (str));
3069 else
3070 nbytes = SBYTES (str);
3072 ssl->bytes += nbytes;
3074 if (STRINGP (str2))
3076 if (NILP (current_buffer->enable_multibyte_characters))
3077 nbytes = SCHARS (str2);
3078 else if (! STRING_MULTIBYTE (str2))
3079 nbytes = count_size_as_multibyte (SDATA (str2),
3080 SBYTES (str2));
3081 else
3082 nbytes = SBYTES (str2);
3084 ssl->bytes += nbytes;
3088 /* Return the concatenation of the strings associated with overlays that
3089 begin or end at POS, ignoring overlays that are specific to a window
3090 other than W. The strings are concatenated in the appropriate order:
3091 shorter overlays nest inside longer ones, and higher priority inside
3092 lower. Normally all of the after-strings come first, but zero-sized
3093 overlays have their after-strings ride along with the before-strings
3094 because it would look strange to print them inside-out.
3096 Returns the string length, and stores the contents indirectly through
3097 PSTR, if that variable is non-null. The string may be overwritten by
3098 subsequent calls. */
3101 overlay_strings (pos, w, pstr)
3102 EMACS_INT pos;
3103 struct window *w;
3104 unsigned char **pstr;
3106 Lisp_Object overlay, window, str;
3107 struct Lisp_Overlay *ov;
3108 int startpos, endpos;
3109 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3111 overlay_heads.used = overlay_heads.bytes = 0;
3112 overlay_tails.used = overlay_tails.bytes = 0;
3113 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3115 XSETMISC (overlay, ov);
3116 eassert (OVERLAYP (overlay));
3118 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3119 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3120 if (endpos < pos)
3121 break;
3122 if (endpos != pos && startpos != pos)
3123 continue;
3124 window = Foverlay_get (overlay, Qwindow);
3125 if (WINDOWP (window) && XWINDOW (window) != w)
3126 continue;
3127 if (startpos == pos
3128 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3129 record_overlay_string (&overlay_heads, str,
3130 (startpos == endpos
3131 ? Foverlay_get (overlay, Qafter_string)
3132 : Qnil),
3133 Foverlay_get (overlay, Qpriority),
3134 endpos - startpos);
3135 else if (endpos == pos
3136 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3137 record_overlay_string (&overlay_tails, str, Qnil,
3138 Foverlay_get (overlay, Qpriority),
3139 endpos - startpos);
3141 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3143 XSETMISC (overlay, ov);
3144 eassert (OVERLAYP (overlay));
3146 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3147 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3148 if (startpos > pos)
3149 break;
3150 if (endpos != pos && startpos != pos)
3151 continue;
3152 window = Foverlay_get (overlay, Qwindow);
3153 if (WINDOWP (window) && XWINDOW (window) != w)
3154 continue;
3155 if (startpos == pos
3156 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3157 record_overlay_string (&overlay_heads, str,
3158 (startpos == endpos
3159 ? Foverlay_get (overlay, Qafter_string)
3160 : Qnil),
3161 Foverlay_get (overlay, Qpriority),
3162 endpos - startpos);
3163 else if (endpos == pos
3164 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3165 record_overlay_string (&overlay_tails, str, Qnil,
3166 Foverlay_get (overlay, Qpriority),
3167 endpos - startpos);
3169 if (overlay_tails.used > 1)
3170 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3171 cmp_for_strings);
3172 if (overlay_heads.used > 1)
3173 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3174 cmp_for_strings);
3175 if (overlay_heads.bytes || overlay_tails.bytes)
3177 Lisp_Object tem;
3178 int i;
3179 unsigned char *p;
3180 int total = overlay_heads.bytes + overlay_tails.bytes;
3182 if (total > overlay_str_len)
3184 overlay_str_len = total;
3185 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3186 total);
3188 p = overlay_str_buf;
3189 for (i = overlay_tails.used; --i >= 0;)
3191 int nbytes;
3192 tem = overlay_tails.buf[i].string;
3193 nbytes = copy_text (SDATA (tem), p,
3194 SBYTES (tem),
3195 STRING_MULTIBYTE (tem), multibyte);
3196 p += nbytes;
3198 for (i = 0; i < overlay_heads.used; ++i)
3200 int nbytes;
3201 tem = overlay_heads.buf[i].string;
3202 nbytes = copy_text (SDATA (tem), p,
3203 SBYTES (tem),
3204 STRING_MULTIBYTE (tem), multibyte);
3205 p += nbytes;
3206 tem = overlay_heads.buf[i].string2;
3207 if (STRINGP (tem))
3209 nbytes = copy_text (SDATA (tem), p,
3210 SBYTES (tem),
3211 STRING_MULTIBYTE (tem), multibyte);
3212 p += nbytes;
3215 if (p != overlay_str_buf + total)
3216 abort ();
3217 if (pstr)
3218 *pstr = overlay_str_buf;
3219 return total;
3221 return 0;
3224 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3226 void
3227 recenter_overlay_lists (buf, pos)
3228 struct buffer *buf;
3229 EMACS_INT pos;
3231 Lisp_Object overlay, beg, end;
3232 struct Lisp_Overlay *prev, *tail, *next;
3234 /* See if anything in overlays_before should move to overlays_after. */
3236 /* We don't strictly need prev in this loop; it should always be nil.
3237 But we use it for symmetry and in case that should cease to be true
3238 with some future change. */
3239 prev = NULL;
3240 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3242 next = tail->next;
3243 XSETMISC (overlay, tail);
3245 /* If the overlay is not valid, get rid of it. */
3246 if (!OVERLAY_VALID (overlay))
3247 #if 1
3248 abort ();
3249 #else
3251 /* Splice the cons cell TAIL out of overlays_before. */
3252 if (!NILP (prev))
3253 XCDR (prev) = next;
3254 else
3255 buf->overlays_before = next;
3256 tail = prev;
3257 continue;
3259 #endif
3261 beg = OVERLAY_START (overlay);
3262 end = OVERLAY_END (overlay);
3264 if (OVERLAY_POSITION (end) > pos)
3266 /* OVERLAY needs to be moved. */
3267 int where = OVERLAY_POSITION (beg);
3268 struct Lisp_Overlay *other, *other_prev;
3270 /* Splice the cons cell TAIL out of overlays_before. */
3271 if (prev)
3272 prev->next = next;
3273 else
3274 buf->overlays_before = next;
3276 /* Search thru overlays_after for where to put it. */
3277 other_prev = NULL;
3278 for (other = buf->overlays_after; other;
3279 other_prev = other, other = other->next)
3281 Lisp_Object otherbeg, otheroverlay;
3283 XSETMISC (otheroverlay, other);
3284 eassert (OVERLAY_VALID (otheroverlay));
3286 otherbeg = OVERLAY_START (otheroverlay);
3287 if (OVERLAY_POSITION (otherbeg) >= where)
3288 break;
3291 /* Add TAIL to overlays_after before OTHER. */
3292 tail->next = other;
3293 if (other_prev)
3294 other_prev->next = tail;
3295 else
3296 buf->overlays_after = tail;
3297 tail = prev;
3299 else
3300 /* We've reached the things that should stay in overlays_before.
3301 All the rest of overlays_before must end even earlier,
3302 so stop now. */
3303 break;
3306 /* See if anything in overlays_after should be in overlays_before. */
3307 prev = NULL;
3308 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3310 next = tail->next;
3311 XSETMISC (overlay, tail);
3313 /* If the overlay is not valid, get rid of it. */
3314 if (!OVERLAY_VALID (overlay))
3315 #if 1
3316 abort ();
3317 #else
3319 /* Splice the cons cell TAIL out of overlays_after. */
3320 if (!NILP (prev))
3321 XCDR (prev) = next;
3322 else
3323 buf->overlays_after = next;
3324 tail = prev;
3325 continue;
3327 #endif
3329 beg = OVERLAY_START (overlay);
3330 end = OVERLAY_END (overlay);
3332 /* Stop looking, when we know that nothing further
3333 can possibly end before POS. */
3334 if (OVERLAY_POSITION (beg) > pos)
3335 break;
3337 if (OVERLAY_POSITION (end) <= pos)
3339 /* OVERLAY needs to be moved. */
3340 int where = OVERLAY_POSITION (end);
3341 struct Lisp_Overlay *other, *other_prev;
3343 /* Splice the cons cell TAIL out of overlays_after. */
3344 if (prev)
3345 prev->next = next;
3346 else
3347 buf->overlays_after = next;
3349 /* Search thru overlays_before for where to put it. */
3350 other_prev = NULL;
3351 for (other = buf->overlays_before; other;
3352 other_prev = other, other = other->next)
3354 Lisp_Object otherend, otheroverlay;
3356 XSETMISC (otheroverlay, other);
3357 eassert (OVERLAY_VALID (otheroverlay));
3359 otherend = OVERLAY_END (otheroverlay);
3360 if (OVERLAY_POSITION (otherend) <= where)
3361 break;
3364 /* Add TAIL to overlays_before before OTHER. */
3365 tail->next = other;
3366 if (other_prev)
3367 other_prev->next = tail;
3368 else
3369 buf->overlays_before = tail;
3370 tail = prev;
3374 buf->overlay_center = pos;
3377 void
3378 adjust_overlays_for_insert (pos, length)
3379 EMACS_INT pos;
3380 EMACS_INT length;
3382 /* After an insertion, the lists are still sorted properly,
3383 but we may need to update the value of the overlay center. */
3384 if (current_buffer->overlay_center >= pos)
3385 current_buffer->overlay_center += length;
3388 void
3389 adjust_overlays_for_delete (pos, length)
3390 EMACS_INT pos;
3391 EMACS_INT length;
3393 if (current_buffer->overlay_center < pos)
3394 /* The deletion was to our right. No change needed; the before- and
3395 after-lists are still consistent. */
3397 else if (current_buffer->overlay_center > pos + length)
3398 /* The deletion was to our left. We need to adjust the center value
3399 to account for the change in position, but the lists are consistent
3400 given the new value. */
3401 current_buffer->overlay_center -= length;
3402 else
3403 /* We're right in the middle. There might be things on the after-list
3404 that now belong on the before-list. Recentering will move them,
3405 and also update the center point. */
3406 recenter_overlay_lists (current_buffer, pos);
3409 /* Fix up overlays that were garbled as a result of permuting markers
3410 in the range START through END. Any overlay with at least one
3411 endpoint in this range will need to be unlinked from the overlay
3412 list and reinserted in its proper place.
3413 Such an overlay might even have negative size at this point.
3414 If so, we'll make the overlay empty. */
3415 void
3416 fix_start_end_in_overlays (start, end)
3417 register int start, end;
3419 Lisp_Object overlay;
3420 struct Lisp_Overlay *before_list, *after_list;
3421 /* These are either nil, indicating that before_list or after_list
3422 should be assigned, or the cons cell the cdr of which should be
3423 assigned. */
3424 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3425 /* 'Parent', likewise, indicates a cons cell or
3426 current_buffer->overlays_before or overlays_after, depending
3427 which loop we're in. */
3428 struct Lisp_Overlay *tail, *parent;
3429 int startpos, endpos;
3431 /* This algorithm shifts links around instead of consing and GCing.
3432 The loop invariant is that before_list (resp. after_list) is a
3433 well-formed list except that its last element, the CDR of beforep
3434 (resp. afterp) if beforep (afterp) isn't nil or before_list
3435 (after_list) if it is, is still uninitialized. So it's not a bug
3436 that before_list isn't initialized, although it may look
3437 strange. */
3438 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3440 XSETMISC (overlay, tail);
3442 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3443 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3445 /* If the overlay is backwards, make it empty. */
3446 if (endpos < startpos)
3448 startpos = endpos;
3449 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3450 Qnil);
3453 if (endpos < start)
3454 break;
3456 if (endpos < end
3457 || (startpos >= start && startpos < end))
3459 /* Add it to the end of the wrong list. Later on,
3460 recenter_overlay_lists will move it to the right place. */
3461 if (endpos < current_buffer->overlay_center)
3463 if (!afterp)
3464 after_list = tail;
3465 else
3466 afterp->next = tail;
3467 afterp = tail;
3469 else
3471 if (!beforep)
3472 before_list = tail;
3473 else
3474 beforep->next = tail;
3475 beforep = tail;
3477 if (!parent)
3478 current_buffer->overlays_before = tail->next;
3479 else
3480 parent->next = tail->next;
3481 tail = tail->next;
3483 else
3484 parent = tail, tail = parent->next;
3486 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3488 XSETMISC (overlay, tail);
3490 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3491 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3493 /* If the overlay is backwards, make it empty. */
3494 if (endpos < startpos)
3496 startpos = endpos;
3497 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3498 Qnil);
3501 if (startpos >= end)
3502 break;
3504 if (startpos >= start
3505 || (endpos >= start && endpos < end))
3507 if (endpos < current_buffer->overlay_center)
3509 if (!afterp)
3510 after_list = tail;
3511 else
3512 afterp->next = tail;
3513 afterp = tail;
3515 else
3517 if (!beforep)
3518 before_list = tail;
3519 else
3520 beforep->next = tail;
3521 beforep = tail;
3523 if (!parent)
3524 current_buffer->overlays_after = tail->next;
3525 else
3526 parent->next = tail->next;
3527 tail = tail->next;
3529 else
3530 parent = tail, tail = parent->next;
3533 /* Splice the constructed (wrong) lists into the buffer's lists,
3534 and let the recenter function make it sane again. */
3535 if (beforep)
3537 beforep->next = current_buffer->overlays_before;
3538 current_buffer->overlays_before = before_list;
3540 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3542 if (afterp)
3544 afterp->next = current_buffer->overlays_after;
3545 current_buffer->overlays_after = after_list;
3547 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3550 /* We have two types of overlay: the one whose ending marker is
3551 after-insertion-marker (this is the usual case) and the one whose
3552 ending marker is before-insertion-marker. When `overlays_before'
3553 contains overlays of the latter type and the former type in this
3554 order and both overlays end at inserting position, inserting a text
3555 increases only the ending marker of the latter type, which results
3556 in incorrect ordering of `overlays_before'.
3558 This function fixes ordering of overlays in the slot
3559 `overlays_before' of the buffer *BP. Before the insertion, `point'
3560 was at PREV, and now is at POS. */
3562 void
3563 fix_overlays_before (bp, prev, pos)
3564 struct buffer *bp;
3565 EMACS_INT prev, pos;
3567 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3568 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3569 Lisp_Object tem;
3570 EMACS_INT end;
3572 /* After the insertion, the several overlays may be in incorrect
3573 order. The possibility is that, in the list `overlays_before',
3574 an overlay which ends at POS appears after an overlay which ends
3575 at PREV. Since POS is greater than PREV, we must fix the
3576 ordering of these overlays, by moving overlays ends at POS before
3577 the overlays ends at PREV. */
3579 /* At first, find a place where disordered overlays should be linked
3580 in. It is where an overlay which end before POS exists. (i.e. an
3581 overlay whose ending marker is after-insertion-marker if disorder
3582 exists). */
3583 while (tail
3584 && (XSETMISC (tem, tail),
3585 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3587 parent = tail;
3588 tail = tail->next;
3591 /* If we don't find such an overlay,
3592 or the found one ends before PREV,
3593 or the found one is the last one in the list,
3594 we don't have to fix anything. */
3595 if (!tail || end < prev || !tail->next)
3596 return;
3598 right_pair = parent;
3599 parent = tail;
3600 tail = tail->next;
3602 /* Now, end position of overlays in the list TAIL should be before
3603 or equal to PREV. In the loop, an overlay which ends at POS is
3604 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3605 we found an overlay which ends before PREV, the remaining
3606 overlays are in correct order. */
3607 while (tail)
3609 XSETMISC (tem, tail);
3610 end = OVERLAY_POSITION (OVERLAY_END (tem));
3612 if (end == pos)
3613 { /* This overlay is disordered. */
3614 struct Lisp_Overlay *found = tail;
3616 /* Unlink the found overlay. */
3617 tail = found->next;
3618 parent->next = tail;
3619 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3620 and link it into the right place. */
3621 if (!right_pair)
3623 found->next = bp->overlays_before;
3624 bp->overlays_before = found;
3626 else
3628 found->next = right_pair->next;
3629 right_pair->next = found;
3632 else if (end == prev)
3634 parent = tail;
3635 tail = tail->next;
3637 else /* No more disordered overlay. */
3638 break;
3642 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3643 doc: /* Return t if OBJECT is an overlay. */)
3644 (object)
3645 Lisp_Object object;
3647 return (OVERLAYP (object) ? Qt : Qnil);
3650 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3651 doc: /* Create a new overlay with range BEG to END in BUFFER.
3652 If omitted, BUFFER defaults to the current buffer.
3653 BEG and END may be integers or markers.
3654 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3655 for the front of the overlay advance when text is inserted there
3656 \(which means the text *is not* included in the overlay).
3657 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3658 for the rear of the overlay advance when text is inserted there
3659 \(which means the text *is* included in the overlay). */)
3660 (beg, end, buffer, front_advance, rear_advance)
3661 Lisp_Object beg, end, buffer;
3662 Lisp_Object front_advance, rear_advance;
3664 Lisp_Object overlay;
3665 struct buffer *b;
3667 if (NILP (buffer))
3668 XSETBUFFER (buffer, current_buffer);
3669 else
3670 CHECK_BUFFER (buffer);
3671 if (MARKERP (beg)
3672 && ! EQ (Fmarker_buffer (beg), buffer))
3673 error ("Marker points into wrong buffer");
3674 if (MARKERP (end)
3675 && ! EQ (Fmarker_buffer (end), buffer))
3676 error ("Marker points into wrong buffer");
3678 CHECK_NUMBER_COERCE_MARKER (beg);
3679 CHECK_NUMBER_COERCE_MARKER (end);
3681 if (XINT (beg) > XINT (end))
3683 Lisp_Object temp;
3684 temp = beg; beg = end; end = temp;
3687 b = XBUFFER (buffer);
3689 beg = Fset_marker (Fmake_marker (), beg, buffer);
3690 end = Fset_marker (Fmake_marker (), end, buffer);
3692 if (!NILP (front_advance))
3693 XMARKER (beg)->insertion_type = 1;
3694 if (!NILP (rear_advance))
3695 XMARKER (end)->insertion_type = 1;
3697 overlay = allocate_misc ();
3698 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3699 XOVERLAY (overlay)->start = beg;
3700 XOVERLAY (overlay)->end = end;
3701 XOVERLAY (overlay)->plist = Qnil;
3702 XOVERLAY (overlay)->next = NULL;
3704 /* Put the new overlay on the wrong list. */
3705 end = OVERLAY_END (overlay);
3706 if (OVERLAY_POSITION (end) < b->overlay_center)
3708 if (b->overlays_after)
3709 XOVERLAY (overlay)->next = b->overlays_after;
3710 b->overlays_after = XOVERLAY (overlay);
3712 else
3714 if (b->overlays_before)
3715 XOVERLAY (overlay)->next = b->overlays_before;
3716 b->overlays_before = XOVERLAY (overlay);
3719 /* This puts it in the right list, and in the right order. */
3720 recenter_overlay_lists (b, b->overlay_center);
3722 /* We don't need to redisplay the region covered by the overlay, because
3723 the overlay has no properties at the moment. */
3725 return overlay;
3728 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3730 static void
3731 modify_overlay (buf, start, end)
3732 struct buffer *buf;
3733 EMACS_INT start, end;
3735 if (start > end)
3737 int temp = start;
3738 start = end;
3739 end = temp;
3742 BUF_COMPUTE_UNCHANGED (buf, start, end);
3744 /* If this is a buffer not in the selected window,
3745 we must do other windows. */
3746 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3747 windows_or_buffers_changed = 1;
3748 /* If multiple windows show this buffer, we must do other windows. */
3749 else if (buffer_shared > 1)
3750 windows_or_buffers_changed = 1;
3751 /* If we modify an overlay at the end of the buffer, we cannot
3752 be sure that window end is still valid. */
3753 else if (end >= ZV && start <= ZV)
3754 windows_or_buffers_changed = 1;
3756 ++BUF_OVERLAY_MODIFF (buf);
3760 Lisp_Object Fdelete_overlay ();
3762 static struct Lisp_Overlay *
3763 unchain_overlay (list, overlay)
3764 struct Lisp_Overlay *list, *overlay;
3766 struct Lisp_Overlay *tmp, *prev;
3767 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3768 if (tmp == overlay)
3770 if (prev)
3771 prev->next = tmp->next;
3772 else
3773 list = tmp->next;
3774 overlay->next = NULL;
3775 break;
3777 return list;
3780 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3781 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3782 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3783 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3784 buffer. */)
3785 (overlay, beg, end, buffer)
3786 Lisp_Object overlay, beg, end, buffer;
3788 struct buffer *b, *ob;
3789 Lisp_Object obuffer;
3790 int count = SPECPDL_INDEX ();
3792 CHECK_OVERLAY (overlay);
3793 if (NILP (buffer))
3794 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3795 if (NILP (buffer))
3796 XSETBUFFER (buffer, current_buffer);
3797 CHECK_BUFFER (buffer);
3799 if (MARKERP (beg)
3800 && ! EQ (Fmarker_buffer (beg), buffer))
3801 error ("Marker points into wrong buffer");
3802 if (MARKERP (end)
3803 && ! EQ (Fmarker_buffer (end), buffer))
3804 error ("Marker points into wrong buffer");
3806 CHECK_NUMBER_COERCE_MARKER (beg);
3807 CHECK_NUMBER_COERCE_MARKER (end);
3809 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3810 return Fdelete_overlay (overlay);
3812 if (XINT (beg) > XINT (end))
3814 Lisp_Object temp;
3815 temp = beg; beg = end; end = temp;
3818 specbind (Qinhibit_quit, Qt);
3820 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3821 b = XBUFFER (buffer);
3822 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3824 /* If the overlay has changed buffers, do a thorough redisplay. */
3825 if (!EQ (buffer, obuffer))
3827 /* Redisplay where the overlay was. */
3828 if (!NILP (obuffer))
3830 int o_beg;
3831 int o_end;
3833 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3834 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3836 modify_overlay (ob, o_beg, o_end);
3839 /* Redisplay where the overlay is going to be. */
3840 modify_overlay (b, XINT (beg), XINT (end));
3842 else
3843 /* Redisplay the area the overlay has just left, or just enclosed. */
3845 int o_beg, o_end;
3847 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3848 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3850 if (o_beg == XINT (beg))
3851 modify_overlay (b, o_end, XINT (end));
3852 else if (o_end == XINT (end))
3853 modify_overlay (b, o_beg, XINT (beg));
3854 else
3856 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3857 if (XINT (end) > o_end) o_end = XINT (end);
3858 modify_overlay (b, o_beg, o_end);
3862 if (!NILP (obuffer))
3864 ob->overlays_before
3865 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3866 ob->overlays_after
3867 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3868 eassert (XOVERLAY (overlay)->next == NULL);
3871 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3872 Fset_marker (OVERLAY_END (overlay), end, buffer);
3874 /* Put the overlay on the wrong list. */
3875 end = OVERLAY_END (overlay);
3876 if (OVERLAY_POSITION (end) < b->overlay_center)
3878 XOVERLAY (overlay)->next = b->overlays_after;
3879 b->overlays_after = XOVERLAY (overlay);
3881 else
3883 XOVERLAY (overlay)->next = b->overlays_before;
3884 b->overlays_before = XOVERLAY (overlay);
3887 /* This puts it in the right list, and in the right order. */
3888 recenter_overlay_lists (b, b->overlay_center);
3890 return unbind_to (count, overlay);
3893 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3894 doc: /* Delete the overlay OVERLAY from its buffer. */)
3895 (overlay)
3896 Lisp_Object overlay;
3898 Lisp_Object buffer;
3899 struct buffer *b;
3900 int count = SPECPDL_INDEX ();
3902 CHECK_OVERLAY (overlay);
3904 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3905 if (NILP (buffer))
3906 return Qnil;
3908 b = XBUFFER (buffer);
3909 specbind (Qinhibit_quit, Qt);
3911 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3912 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3913 eassert (XOVERLAY (overlay)->next == NULL);
3914 modify_overlay (b,
3915 marker_position (OVERLAY_START (overlay)),
3916 marker_position (OVERLAY_END (overlay)));
3917 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3918 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3920 /* When deleting an overlay with before or after strings, turn off
3921 display optimizations for the affected buffer, on the basis that
3922 these strings may contain newlines. This is easier to do than to
3923 check for that situation during redisplay. */
3924 if (!windows_or_buffers_changed
3925 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3926 || !NILP (Foverlay_get (overlay, Qafter_string))))
3927 b->prevent_redisplay_optimizations_p = 1;
3929 return unbind_to (count, Qnil);
3932 /* Overlay dissection functions. */
3934 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3935 doc: /* Return the position at which OVERLAY starts. */)
3936 (overlay)
3937 Lisp_Object overlay;
3939 CHECK_OVERLAY (overlay);
3941 return (Fmarker_position (OVERLAY_START (overlay)));
3944 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3945 doc: /* Return the position at which OVERLAY ends. */)
3946 (overlay)
3947 Lisp_Object overlay;
3949 CHECK_OVERLAY (overlay);
3951 return (Fmarker_position (OVERLAY_END (overlay)));
3954 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3955 doc: /* Return the buffer OVERLAY belongs to.
3956 Return nil if OVERLAY has been deleted. */)
3957 (overlay)
3958 Lisp_Object overlay;
3960 CHECK_OVERLAY (overlay);
3962 return Fmarker_buffer (OVERLAY_START (overlay));
3965 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3966 doc: /* Return a list of the properties on OVERLAY.
3967 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3968 OVERLAY. */)
3969 (overlay)
3970 Lisp_Object overlay;
3972 CHECK_OVERLAY (overlay);
3974 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3978 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3979 doc: /* Return a list of the overlays that contain the character at POS. */)
3980 (pos)
3981 Lisp_Object pos;
3983 int noverlays;
3984 Lisp_Object *overlay_vec;
3985 int len;
3986 Lisp_Object result;
3988 CHECK_NUMBER_COERCE_MARKER (pos);
3990 len = 10;
3991 /* We can't use alloca here because overlays_at can call xrealloc. */
3992 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3994 /* Put all the overlays we want in a vector in overlay_vec.
3995 Store the length in len. */
3996 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3997 (int *) 0, (int *) 0, 0);
3999 /* Make a list of them all. */
4000 result = Flist (noverlays, overlay_vec);
4002 xfree (overlay_vec);
4003 return result;
4006 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4007 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4008 Overlap means that at least one character is contained within the overlay
4009 and also contained within the specified region.
4010 Empty overlays are included in the result if they are located at BEG
4011 or between BEG and END. */)
4012 (beg, end)
4013 Lisp_Object beg, end;
4015 int noverlays;
4016 Lisp_Object *overlay_vec;
4017 int len;
4018 Lisp_Object result;
4020 CHECK_NUMBER_COERCE_MARKER (beg);
4021 CHECK_NUMBER_COERCE_MARKER (end);
4023 len = 10;
4024 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4026 /* Put all the overlays we want in a vector in overlay_vec.
4027 Store the length in len. */
4028 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4029 (int *) 0, (int *) 0);
4031 /* Make a list of them all. */
4032 result = Flist (noverlays, overlay_vec);
4034 xfree (overlay_vec);
4035 return result;
4038 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4039 1, 1, 0,
4040 doc: /* Return the next position after POS where an overlay starts or ends.
4041 If there are no overlay boundaries from POS to (point-max),
4042 the value is (point-max). */)
4043 (pos)
4044 Lisp_Object pos;
4046 int noverlays;
4047 int endpos;
4048 Lisp_Object *overlay_vec;
4049 int len;
4050 int i;
4052 CHECK_NUMBER_COERCE_MARKER (pos);
4054 len = 10;
4055 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4057 /* Put all the overlays we want in a vector in overlay_vec.
4058 Store the length in len.
4059 endpos gets the position where the next overlay starts. */
4060 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4061 &endpos, (int *) 0, 1);
4063 /* If any of these overlays ends before endpos,
4064 use its ending point instead. */
4065 for (i = 0; i < noverlays; i++)
4067 Lisp_Object oend;
4068 int oendpos;
4070 oend = OVERLAY_END (overlay_vec[i]);
4071 oendpos = OVERLAY_POSITION (oend);
4072 if (oendpos < endpos)
4073 endpos = oendpos;
4076 xfree (overlay_vec);
4077 return make_number (endpos);
4080 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4081 Sprevious_overlay_change, 1, 1, 0,
4082 doc: /* Return the previous position before POS where an overlay starts or ends.
4083 If there are no overlay boundaries from (point-min) to POS,
4084 the value is (point-min). */)
4085 (pos)
4086 Lisp_Object pos;
4088 int noverlays;
4089 int prevpos;
4090 Lisp_Object *overlay_vec;
4091 int len;
4093 CHECK_NUMBER_COERCE_MARKER (pos);
4095 /* At beginning of buffer, we know the answer;
4096 avoid bug subtracting 1 below. */
4097 if (XINT (pos) == BEGV)
4098 return pos;
4100 len = 10;
4101 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4103 /* Put all the overlays we want in a vector in overlay_vec.
4104 Store the length in len.
4105 prevpos gets the position of the previous change. */
4106 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4107 (int *) 0, &prevpos, 1);
4109 xfree (overlay_vec);
4110 return make_number (prevpos);
4113 /* These functions are for debugging overlays. */
4115 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4116 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4117 The car has all the overlays before the overlay center;
4118 the cdr has all the overlays after the overlay center.
4119 Recentering overlays moves overlays between these lists.
4120 The lists you get are copies, so that changing them has no effect.
4121 However, the overlays you get are the real objects that the buffer uses. */)
4124 struct Lisp_Overlay *ol;
4125 Lisp_Object before = Qnil, after = Qnil, tmp;
4126 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4128 XSETMISC (tmp, ol);
4129 before = Fcons (tmp, before);
4131 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4133 XSETMISC (tmp, ol);
4134 after = Fcons (tmp, after);
4136 return Fcons (Fnreverse (before), Fnreverse (after));
4139 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4140 doc: /* Recenter the overlays of the current buffer around position POS.
4141 That makes overlay lookup faster for positions near POS (but perhaps slower
4142 for positions far away from POS). */)
4143 (pos)
4144 Lisp_Object pos;
4146 CHECK_NUMBER_COERCE_MARKER (pos);
4148 recenter_overlay_lists (current_buffer, XINT (pos));
4149 return Qnil;
4152 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4153 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4154 (overlay, prop)
4155 Lisp_Object overlay, prop;
4157 CHECK_OVERLAY (overlay);
4158 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4161 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4162 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4163 (overlay, prop, value)
4164 Lisp_Object overlay, prop, value;
4166 Lisp_Object tail, buffer;
4167 int changed;
4169 CHECK_OVERLAY (overlay);
4171 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4173 for (tail = XOVERLAY (overlay)->plist;
4174 CONSP (tail) && CONSP (XCDR (tail));
4175 tail = XCDR (XCDR (tail)))
4176 if (EQ (XCAR (tail), prop))
4178 changed = !EQ (XCAR (XCDR (tail)), value);
4179 XSETCAR (XCDR (tail), value);
4180 goto found;
4182 /* It wasn't in the list, so add it to the front. */
4183 changed = !NILP (value);
4184 XOVERLAY (overlay)->plist
4185 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4186 found:
4187 if (! NILP (buffer))
4189 if (changed)
4190 modify_overlay (XBUFFER (buffer),
4191 marker_position (OVERLAY_START (overlay)),
4192 marker_position (OVERLAY_END (overlay)));
4193 if (EQ (prop, Qevaporate) && ! NILP (value)
4194 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4195 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4196 Fdelete_overlay (overlay);
4199 return value;
4202 /* Subroutine of report_overlay_modification. */
4204 /* Lisp vector holding overlay hook functions to call.
4205 Vector elements come in pairs.
4206 Each even-index element is a list of hook functions.
4207 The following odd-index element is the overlay they came from.
4209 Before the buffer change, we fill in this vector
4210 as we call overlay hook functions.
4211 After the buffer change, we get the functions to call from this vector.
4212 This way we always call the same functions before and after the change. */
4213 static Lisp_Object last_overlay_modification_hooks;
4215 /* Number of elements actually used in last_overlay_modification_hooks. */
4216 static int last_overlay_modification_hooks_used;
4218 /* Add one functionlist/overlay pair
4219 to the end of last_overlay_modification_hooks. */
4221 static void
4222 add_overlay_mod_hooklist (functionlist, overlay)
4223 Lisp_Object functionlist, overlay;
4225 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4227 if (last_overlay_modification_hooks_used == oldsize)
4228 last_overlay_modification_hooks = larger_vector
4229 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4230 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4231 functionlist); last_overlay_modification_hooks_used++;
4232 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4233 overlay); last_overlay_modification_hooks_used++;
4236 /* Run the modification-hooks of overlays that include
4237 any part of the text in START to END.
4238 If this change is an insertion, also
4239 run the insert-before-hooks of overlay starting at END,
4240 and the insert-after-hooks of overlay ending at START.
4242 This is called both before and after the modification.
4243 AFTER is nonzero when we call after the modification.
4245 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4246 When AFTER is nonzero, they are the start position,
4247 the position after the inserted new text,
4248 and the length of deleted or replaced old text. */
4250 void
4251 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4252 Lisp_Object start, end;
4253 int after;
4254 Lisp_Object arg1, arg2, arg3;
4256 Lisp_Object prop, overlay;
4257 struct Lisp_Overlay *tail;
4258 /* 1 if this change is an insertion. */
4259 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4260 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4262 overlay = Qnil;
4263 tail = NULL;
4265 /* We used to run the functions as soon as we found them and only register
4266 them in last_overlay_modification_hooks for the purpose of the `after'
4267 case. But running elisp code as we traverse the list of overlays is
4268 painful because the list can be modified by the elisp code so we had to
4269 copy at several places. We now simply do a read-only traversal that
4270 only collects the functions to run and we run them afterwards. It's
4271 simpler, especially since all the code was already there. -stef */
4273 if (!after)
4275 /* We are being called before a change.
4276 Scan the overlays to find the functions to call. */
4277 last_overlay_modification_hooks_used = 0;
4278 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4280 int startpos, endpos;
4281 Lisp_Object ostart, oend;
4283 XSETMISC (overlay, tail);
4285 ostart = OVERLAY_START (overlay);
4286 oend = OVERLAY_END (overlay);
4287 endpos = OVERLAY_POSITION (oend);
4288 if (XFASTINT (start) > endpos)
4289 break;
4290 startpos = OVERLAY_POSITION (ostart);
4291 if (insertion && (XFASTINT (start) == startpos
4292 || XFASTINT (end) == startpos))
4294 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4295 if (!NILP (prop))
4296 add_overlay_mod_hooklist (prop, overlay);
4298 if (insertion && (XFASTINT (start) == endpos
4299 || XFASTINT (end) == endpos))
4301 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4302 if (!NILP (prop))
4303 add_overlay_mod_hooklist (prop, overlay);
4305 /* Test for intersecting intervals. This does the right thing
4306 for both insertion and deletion. */
4307 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4309 prop = Foverlay_get (overlay, Qmodification_hooks);
4310 if (!NILP (prop))
4311 add_overlay_mod_hooklist (prop, overlay);
4315 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4317 int startpos, endpos;
4318 Lisp_Object ostart, oend;
4320 XSETMISC (overlay, tail);
4322 ostart = OVERLAY_START (overlay);
4323 oend = OVERLAY_END (overlay);
4324 startpos = OVERLAY_POSITION (ostart);
4325 endpos = OVERLAY_POSITION (oend);
4326 if (XFASTINT (end) < startpos)
4327 break;
4328 if (insertion && (XFASTINT (start) == startpos
4329 || XFASTINT (end) == startpos))
4331 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4332 if (!NILP (prop))
4333 add_overlay_mod_hooklist (prop, overlay);
4335 if (insertion && (XFASTINT (start) == endpos
4336 || XFASTINT (end) == endpos))
4338 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4339 if (!NILP (prop))
4340 add_overlay_mod_hooklist (prop, overlay);
4342 /* Test for intersecting intervals. This does the right thing
4343 for both insertion and deletion. */
4344 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4346 prop = Foverlay_get (overlay, Qmodification_hooks);
4347 if (!NILP (prop))
4348 add_overlay_mod_hooklist (prop, overlay);
4353 GCPRO4 (overlay, arg1, arg2, arg3);
4355 /* Call the functions recorded in last_overlay_modification_hooks.
4356 First copy the vector contents, in case some of these hooks
4357 do subsequent modification of the buffer. */
4358 int size = last_overlay_modification_hooks_used;
4359 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4360 int i;
4362 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4363 copy, size * sizeof (Lisp_Object));
4364 gcpro1.var = copy;
4365 gcpro1.nvars = size;
4367 for (i = 0; i < size;)
4369 Lisp_Object prop, overlay;
4370 prop = copy[i++];
4371 overlay = copy[i++];
4372 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4375 UNGCPRO;
4378 static void
4379 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4380 Lisp_Object list, overlay;
4381 int after;
4382 Lisp_Object arg1, arg2, arg3;
4384 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4386 GCPRO4 (list, arg1, arg2, arg3);
4388 while (CONSP (list))
4390 if (NILP (arg3))
4391 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4392 else
4393 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4394 list = XCDR (list);
4396 UNGCPRO;
4399 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4400 property is set. */
4401 void
4402 evaporate_overlays (pos)
4403 EMACS_INT pos;
4405 Lisp_Object overlay, hit_list;
4406 struct Lisp_Overlay *tail;
4408 hit_list = Qnil;
4409 if (pos <= current_buffer->overlay_center)
4410 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4412 int endpos;
4413 XSETMISC (overlay, tail);
4414 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4415 if (endpos < pos)
4416 break;
4417 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4418 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4419 hit_list = Fcons (overlay, hit_list);
4421 else
4422 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4424 int startpos;
4425 XSETMISC (overlay, tail);
4426 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4427 if (startpos > pos)
4428 break;
4429 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4430 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4431 hit_list = Fcons (overlay, hit_list);
4433 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4434 Fdelete_overlay (XCAR (hit_list));
4437 /* Somebody has tried to store a value with an unacceptable type
4438 in the slot with offset OFFSET. */
4440 void
4441 buffer_slot_type_mismatch (sym, type)
4442 Lisp_Object sym;
4443 int type;
4445 char *type_name;
4447 switch (type)
4449 case Lisp_Int:
4450 type_name = "integers";
4451 break;
4453 case Lisp_String:
4454 type_name = "strings";
4455 break;
4457 case Lisp_Symbol:
4458 type_name = "symbols";
4459 break;
4461 default:
4462 abort ();
4465 error ("Only %s should be stored in the buffer-local variable %s",
4466 type_name, SDATA (SYMBOL_NAME (sym)));
4470 /***********************************************************************
4471 Allocation with mmap
4472 ***********************************************************************/
4474 #ifdef USE_MMAP_FOR_BUFFERS
4476 #include <sys/types.h>
4477 #include <sys/mman.h>
4479 #ifndef MAP_ANON
4480 #ifdef MAP_ANONYMOUS
4481 #define MAP_ANON MAP_ANONYMOUS
4482 #else
4483 #define MAP_ANON 0
4484 #endif
4485 #endif
4487 #ifndef MAP_FAILED
4488 #define MAP_FAILED ((void *) -1)
4489 #endif
4491 #include <stdio.h>
4492 #include <errno.h>
4494 #if MAP_ANON == 0
4495 #include <fcntl.h>
4496 #endif
4498 #include "coding.h"
4501 /* Memory is allocated in regions which are mapped using mmap(2).
4502 The current implementation lets the system select mapped
4503 addresses; we're not using MAP_FIXED in general, except when
4504 trying to enlarge regions.
4506 Each mapped region starts with a mmap_region structure, the user
4507 area starts after that structure, aligned to MEM_ALIGN.
4509 +-----------------------+
4510 | struct mmap_info + |
4511 | padding |
4512 +-----------------------+
4513 | user data |
4516 +-----------------------+ */
4518 struct mmap_region
4520 /* User-specified size. */
4521 size_t nbytes_specified;
4523 /* Number of bytes mapped */
4524 size_t nbytes_mapped;
4526 /* Pointer to the location holding the address of the memory
4527 allocated with the mmap'd block. The variable actually points
4528 after this structure. */
4529 POINTER_TYPE **var;
4531 /* Next and previous in list of all mmap'd regions. */
4532 struct mmap_region *next, *prev;
4535 /* Doubly-linked list of mmap'd regions. */
4537 static struct mmap_region *mmap_regions;
4539 /* File descriptor for mmap. If we don't have anonymous mapping,
4540 /dev/zero will be opened on it. */
4542 static int mmap_fd;
4544 /* Temporary storage for mmap_set_vars, see there. */
4546 static struct mmap_region *mmap_regions_1;
4547 static int mmap_fd_1;
4549 /* Page size on this system. */
4551 static int mmap_page_size;
4553 /* 1 means mmap has been intialized. */
4555 static int mmap_initialized_p;
4557 /* Value is X rounded up to the next multiple of N. */
4559 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4561 /* Size of mmap_region structure plus padding. */
4563 #define MMAP_REGION_STRUCT_SIZE \
4564 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4566 /* Given a pointer P to the start of the user-visible part of a mapped
4567 region, return a pointer to the start of the region. */
4569 #define MMAP_REGION(P) \
4570 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4572 /* Given a pointer P to the start of a mapped region, return a pointer
4573 to the start of the user-visible part of the region. */
4575 #define MMAP_USER_AREA(P) \
4576 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4578 #define MEM_ALIGN sizeof (double)
4580 /* Predicate returning true if part of the address range [START .. END]
4581 is currently mapped. Used to prevent overwriting an existing
4582 memory mapping.
4584 Default is to conservativly assume the address range is occupied by
4585 something else. This can be overridden by system configuration
4586 files if system-specific means to determine this exists. */
4588 #ifndef MMAP_ALLOCATED_P
4589 #define MMAP_ALLOCATED_P(start, end) 1
4590 #endif
4592 /* Function prototypes. */
4594 static int mmap_free_1 P_ ((struct mmap_region *));
4595 static int mmap_enlarge P_ ((struct mmap_region *, int));
4596 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4597 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4598 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4599 static void mmap_free P_ ((POINTER_TYPE **ptr));
4600 static void mmap_init P_ ((void));
4603 /* Return a region overlapping address range START...END, or null if
4604 none. END is not including, i.e. the last byte in the range
4605 is at END - 1. */
4607 static struct mmap_region *
4608 mmap_find (start, end)
4609 POINTER_TYPE *start, *end;
4611 struct mmap_region *r;
4612 char *s = (char *) start, *e = (char *) end;
4614 for (r = mmap_regions; r; r = r->next)
4616 char *rstart = (char *) r;
4617 char *rend = rstart + r->nbytes_mapped;
4619 if (/* First byte of range, i.e. START, in this region? */
4620 (s >= rstart && s < rend)
4621 /* Last byte of range, i.e. END - 1, in this region? */
4622 || (e > rstart && e <= rend)
4623 /* First byte of this region in the range? */
4624 || (rstart >= s && rstart < e)
4625 /* Last byte of this region in the range? */
4626 || (rend > s && rend <= e))
4627 break;
4630 return r;
4634 /* Unmap a region. P is a pointer to the start of the user-araa of
4635 the region. Value is non-zero if successful. */
4637 static int
4638 mmap_free_1 (r)
4639 struct mmap_region *r;
4641 if (r->next)
4642 r->next->prev = r->prev;
4643 if (r->prev)
4644 r->prev->next = r->next;
4645 else
4646 mmap_regions = r->next;
4648 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4650 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4651 return 0;
4654 return 1;
4658 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4659 Value is non-zero if successful. */
4661 static int
4662 mmap_enlarge (r, npages)
4663 struct mmap_region *r;
4664 int npages;
4666 char *region_end = (char *) r + r->nbytes_mapped;
4667 size_t nbytes;
4668 int success = 0;
4670 if (npages < 0)
4672 /* Unmap pages at the end of the region. */
4673 nbytes = - npages * mmap_page_size;
4674 if (munmap (region_end - nbytes, nbytes) == -1)
4675 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4676 else
4678 r->nbytes_mapped -= nbytes;
4679 success = 1;
4682 else if (npages > 0)
4684 nbytes = npages * mmap_page_size;
4686 /* Try to map additional pages at the end of the region. We
4687 cannot do this if the address range is already occupied by
4688 something else because mmap deletes any previous mapping.
4689 I'm not sure this is worth doing, let's see. */
4690 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4692 POINTER_TYPE *p;
4694 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4695 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4696 if (p == MAP_FAILED)
4697 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4698 else if (p != (POINTER_TYPE *) region_end)
4700 /* Kernels are free to choose a different address. In
4701 that case, unmap what we've mapped above; we have
4702 no use for it. */
4703 if (munmap (p, nbytes) == -1)
4704 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4706 else
4708 r->nbytes_mapped += nbytes;
4709 success = 1;
4714 return success;
4718 /* Set or reset variables holding references to mapped regions. If
4719 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4720 non-zero, set all variables to the start of the user-areas
4721 of mapped regions.
4723 This function is called from Fdump_emacs to ensure that the dumped
4724 Emacs doesn't contain references to memory that won't be mapped
4725 when Emacs starts. */
4727 void
4728 mmap_set_vars (restore_p)
4729 int restore_p;
4731 struct mmap_region *r;
4733 if (restore_p)
4735 mmap_regions = mmap_regions_1;
4736 mmap_fd = mmap_fd_1;
4737 for (r = mmap_regions; r; r = r->next)
4738 *r->var = MMAP_USER_AREA (r);
4740 else
4742 for (r = mmap_regions; r; r = r->next)
4743 *r->var = NULL;
4744 mmap_regions_1 = mmap_regions;
4745 mmap_regions = NULL;
4746 mmap_fd_1 = mmap_fd;
4747 mmap_fd = -1;
4752 /* Allocate a block of storage large enough to hold NBYTES bytes of
4753 data. A pointer to the data is returned in *VAR. VAR is thus the
4754 address of some variable which will use the data area.
4756 The allocation of 0 bytes is valid.
4758 If we can't allocate the necessary memory, set *VAR to null, and
4759 return null. */
4761 static POINTER_TYPE *
4762 mmap_alloc (var, nbytes)
4763 POINTER_TYPE **var;
4764 size_t nbytes;
4766 void *p;
4767 size_t map;
4769 mmap_init ();
4771 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4772 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4773 mmap_fd, 0);
4775 if (p == MAP_FAILED)
4777 if (errno != ENOMEM)
4778 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4779 p = NULL;
4781 else
4783 struct mmap_region *r = (struct mmap_region *) p;
4785 r->nbytes_specified = nbytes;
4786 r->nbytes_mapped = map;
4787 r->var = var;
4788 r->prev = NULL;
4789 r->next = mmap_regions;
4790 if (r->next)
4791 r->next->prev = r;
4792 mmap_regions = r;
4794 p = MMAP_USER_AREA (p);
4797 return *var = p;
4801 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4802 resize it to size NBYTES. Change *VAR to reflect the new block,
4803 and return this value. If more memory cannot be allocated, then
4804 leave *VAR unchanged, and return null. */
4806 static POINTER_TYPE *
4807 mmap_realloc (var, nbytes)
4808 POINTER_TYPE **var;
4809 size_t nbytes;
4811 POINTER_TYPE *result;
4813 mmap_init ();
4815 if (*var == NULL)
4816 result = mmap_alloc (var, nbytes);
4817 else if (nbytes == 0)
4819 mmap_free (var);
4820 result = mmap_alloc (var, nbytes);
4822 else
4824 struct mmap_region *r = MMAP_REGION (*var);
4825 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4827 if (room < nbytes)
4829 /* Must enlarge. */
4830 POINTER_TYPE *old_ptr = *var;
4832 /* Try to map additional pages at the end of the region.
4833 If that fails, allocate a new region, copy data
4834 from the old region, then free it. */
4835 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4836 / mmap_page_size)))
4838 r->nbytes_specified = nbytes;
4839 *var = result = old_ptr;
4841 else if (mmap_alloc (var, nbytes))
4843 bcopy (old_ptr, *var, r->nbytes_specified);
4844 mmap_free_1 (MMAP_REGION (old_ptr));
4845 result = *var;
4846 r = MMAP_REGION (result);
4847 r->nbytes_specified = nbytes;
4849 else
4851 *var = old_ptr;
4852 result = NULL;
4855 else if (room - nbytes >= mmap_page_size)
4857 /* Shrinking by at least a page. Let's give some
4858 memory back to the system.
4860 The extra parens are to make the division happens first,
4861 on positive values, so we know it will round towards
4862 zero. */
4863 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4864 result = *var;
4865 r->nbytes_specified = nbytes;
4867 else
4869 /* Leave it alone. */
4870 result = *var;
4871 r->nbytes_specified = nbytes;
4875 return result;
4879 /* Free a block of relocatable storage whose data is pointed to by
4880 PTR. Store 0 in *PTR to show there's no block allocated. */
4882 static void
4883 mmap_free (var)
4884 POINTER_TYPE **var;
4886 mmap_init ();
4888 if (*var)
4890 mmap_free_1 (MMAP_REGION (*var));
4891 *var = NULL;
4896 /* Perform necessary intializations for the use of mmap. */
4898 static void
4899 mmap_init ()
4901 #if MAP_ANON == 0
4902 /* The value of mmap_fd is initially 0 in temacs, and -1
4903 in a dumped Emacs. */
4904 if (mmap_fd <= 0)
4906 /* No anonymous mmap -- we need the file descriptor. */
4907 mmap_fd = open ("/dev/zero", O_RDONLY);
4908 if (mmap_fd == -1)
4909 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4911 #endif /* MAP_ANON == 0 */
4913 if (mmap_initialized_p)
4914 return;
4915 mmap_initialized_p = 1;
4917 #if MAP_ANON != 0
4918 mmap_fd = -1;
4919 #endif
4921 mmap_page_size = getpagesize ();
4924 #endif /* USE_MMAP_FOR_BUFFERS */
4928 /***********************************************************************
4929 Buffer-text Allocation
4930 ***********************************************************************/
4932 #ifdef REL_ALLOC
4933 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4934 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4935 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4936 #endif /* REL_ALLOC */
4939 /* Allocate NBYTES bytes for buffer B's text buffer. */
4941 static void
4942 alloc_buffer_text (b, nbytes)
4943 struct buffer *b;
4944 size_t nbytes;
4946 POINTER_TYPE *p;
4948 BLOCK_INPUT;
4949 #if defined USE_MMAP_FOR_BUFFERS
4950 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4951 #elif defined REL_ALLOC
4952 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4953 #else
4954 p = xmalloc (nbytes);
4955 #endif
4957 if (p == NULL)
4959 UNBLOCK_INPUT;
4960 memory_full ();
4963 b->text->beg = (unsigned char *) p;
4964 UNBLOCK_INPUT;
4967 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4968 shrink it. */
4970 void
4971 enlarge_buffer_text (b, delta)
4972 struct buffer *b;
4973 int delta;
4975 POINTER_TYPE *p;
4976 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4977 + delta);
4978 BLOCK_INPUT;
4979 #if defined USE_MMAP_FOR_BUFFERS
4980 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4981 #elif defined REL_ALLOC
4982 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4983 #else
4984 p = xrealloc (b->text->beg, nbytes);
4985 #endif
4987 if (p == NULL)
4989 UNBLOCK_INPUT;
4990 memory_full ();
4993 BUF_BEG_ADDR (b) = (unsigned char *) p;
4994 UNBLOCK_INPUT;
4998 /* Free buffer B's text buffer. */
5000 static void
5001 free_buffer_text (b)
5002 struct buffer *b;
5004 BLOCK_INPUT;
5006 #if defined USE_MMAP_FOR_BUFFERS
5007 mmap_free ((POINTER_TYPE **) &b->text->beg);
5008 #elif defined REL_ALLOC
5009 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5010 #else
5011 xfree (b->text->beg);
5012 #endif
5014 BUF_BEG_ADDR (b) = NULL;
5015 UNBLOCK_INPUT;
5020 /***********************************************************************
5021 Initialization
5022 ***********************************************************************/
5024 void
5025 init_buffer_once ()
5027 int idx;
5029 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5031 /* Make sure all markable slots in buffer_defaults
5032 are initialized reasonably, so mark_buffer won't choke. */
5033 reset_buffer (&buffer_defaults);
5034 reset_buffer_local_variables (&buffer_defaults, 1);
5035 reset_buffer (&buffer_local_symbols);
5036 reset_buffer_local_variables (&buffer_local_symbols, 1);
5037 /* Prevent GC from getting confused. */
5038 buffer_defaults.text = &buffer_defaults.own_text;
5039 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5040 BUF_INTERVALS (&buffer_defaults) = 0;
5041 BUF_INTERVALS (&buffer_local_symbols) = 0;
5042 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5043 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5044 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5045 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5047 /* Set up the default values of various buffer slots. */
5048 /* Must do these before making the first buffer! */
5050 /* real setup is done in bindings.el */
5051 buffer_defaults.mode_line_format = build_string ("%-");
5052 buffer_defaults.header_line_format = Qnil;
5053 buffer_defaults.abbrev_mode = Qnil;
5054 buffer_defaults.overwrite_mode = Qnil;
5055 buffer_defaults.case_fold_search = Qt;
5056 buffer_defaults.auto_fill_function = Qnil;
5057 buffer_defaults.selective_display = Qnil;
5058 #ifndef old
5059 buffer_defaults.selective_display_ellipses = Qt;
5060 #endif
5061 buffer_defaults.abbrev_table = Qnil;
5062 buffer_defaults.display_table = Qnil;
5063 buffer_defaults.undo_list = Qnil;
5064 buffer_defaults.mark_active = Qnil;
5065 buffer_defaults.file_format = Qnil;
5066 buffer_defaults.auto_save_file_format = Qt;
5067 buffer_defaults.overlays_before = NULL;
5068 buffer_defaults.overlays_after = NULL;
5069 buffer_defaults.overlay_center = BEG;
5071 XSETFASTINT (buffer_defaults.tab_width, 8);
5072 buffer_defaults.truncate_lines = Qnil;
5073 buffer_defaults.ctl_arrow = Qt;
5074 buffer_defaults.direction_reversed = Qnil;
5075 buffer_defaults.cursor_type = Qt;
5076 buffer_defaults.extra_line_spacing = Qnil;
5077 buffer_defaults.cursor_in_non_selected_windows = Qt;
5079 #ifdef DOS_NT
5080 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5081 #endif
5082 buffer_defaults.enable_multibyte_characters = Qt;
5083 buffer_defaults.buffer_file_coding_system = Qnil;
5084 XSETFASTINT (buffer_defaults.fill_column, 70);
5085 XSETFASTINT (buffer_defaults.left_margin, 0);
5086 buffer_defaults.cache_long_line_scans = Qnil;
5087 buffer_defaults.file_truename = Qnil;
5088 XSETFASTINT (buffer_defaults.display_count, 0);
5089 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5090 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5091 buffer_defaults.left_fringe_width = Qnil;
5092 buffer_defaults.right_fringe_width = Qnil;
5093 buffer_defaults.fringes_outside_margins = Qnil;
5094 buffer_defaults.scroll_bar_width = Qnil;
5095 buffer_defaults.vertical_scroll_bar_type = Qt;
5096 buffer_defaults.indicate_empty_lines = Qnil;
5097 buffer_defaults.indicate_buffer_boundaries = Qnil;
5098 buffer_defaults.fringe_indicator_alist = Qnil;
5099 buffer_defaults.fringe_cursor_alist = Qnil;
5100 buffer_defaults.scroll_up_aggressively = Qnil;
5101 buffer_defaults.scroll_down_aggressively = Qnil;
5102 buffer_defaults.display_time = Qnil;
5104 /* Assign the local-flags to the slots that have default values.
5105 The local flag is a bit that is used in the buffer
5106 to say that it has its own local value for the slot.
5107 The local flag bits are in the local_var_flags slot of the buffer. */
5109 /* Nothing can work if this isn't true */
5110 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5112 /* 0 means not a lisp var, -1 means always local, else mask */
5113 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5114 XSETINT (buffer_local_flags.filename, -1);
5115 XSETINT (buffer_local_flags.directory, -1);
5116 XSETINT (buffer_local_flags.backed_up, -1);
5117 XSETINT (buffer_local_flags.save_length, -1);
5118 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5119 XSETINT (buffer_local_flags.read_only, -1);
5120 XSETINT (buffer_local_flags.major_mode, -1);
5121 XSETINT (buffer_local_flags.mode_name, -1);
5122 XSETINT (buffer_local_flags.undo_list, -1);
5123 XSETINT (buffer_local_flags.mark_active, -1);
5124 XSETINT (buffer_local_flags.point_before_scroll, -1);
5125 XSETINT (buffer_local_flags.file_truename, -1);
5126 XSETINT (buffer_local_flags.invisibility_spec, -1);
5127 XSETINT (buffer_local_flags.file_format, -1);
5128 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5129 XSETINT (buffer_local_flags.display_count, -1);
5130 XSETINT (buffer_local_flags.display_time, -1);
5131 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5133 idx = 1;
5134 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5135 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5136 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5137 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5138 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5139 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5140 #ifndef old
5141 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5142 #endif
5143 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5149 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5150 #ifdef DOS_NT
5151 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5152 /* Make this one a permanent local. */
5153 buffer_permanent_local_flags[idx++] = 1;
5154 #endif
5155 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5160 /* Make this one a permanent local. */
5161 buffer_permanent_local_flags[idx++] = 1;
5162 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5163 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5164 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5165 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5166 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5167 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5168 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5169 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5170 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5171 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5172 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5173 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5174 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5175 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5176 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5177 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5178 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5180 /* Need more room? */
5181 if (idx >= MAX_PER_BUFFER_VARS)
5182 abort ();
5183 last_per_buffer_idx = idx;
5185 Vbuffer_alist = Qnil;
5186 current_buffer = 0;
5187 all_buffers = 0;
5189 QSFundamental = build_string ("Fundamental");
5191 Qfundamental_mode = intern ("fundamental-mode");
5192 buffer_defaults.major_mode = Qfundamental_mode;
5194 Qmode_class = intern ("mode-class");
5196 Qprotected_field = intern ("protected-field");
5198 Qpermanent_local = intern ("permanent-local");
5200 Qkill_buffer_hook = intern ("kill-buffer-hook");
5201 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5203 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5205 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5207 /* super-magic invisible buffer */
5208 Vbuffer_alist = Qnil;
5210 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5212 inhibit_modification_hooks = 0;
5215 void
5216 init_buffer ()
5218 char *pwd;
5219 Lisp_Object temp;
5220 int len;
5222 #ifdef USE_MMAP_FOR_BUFFERS
5224 /* When using the ralloc implementation based on mmap(2), buffer
5225 text pointers will have been set to null in the dumped Emacs.
5226 Map new memory. */
5227 struct buffer *b;
5229 for (b = all_buffers; b; b = b->next)
5230 if (b->text->beg == NULL)
5231 enlarge_buffer_text (b, 0);
5233 #endif /* USE_MMAP_FOR_BUFFERS */
5235 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5236 if (NILP (buffer_defaults.enable_multibyte_characters))
5237 Fset_buffer_multibyte (Qnil);
5239 pwd = get_current_dir_name ();
5241 if (!pwd)
5242 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5244 #ifndef VMS
5245 /* Maybe this should really use some standard subroutine
5246 whose definition is filename syntax dependent. */
5247 len = strlen (pwd);
5248 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5250 /* Grow buffer to add directory separator and '\0'. */
5251 pwd = (char *) xrealloc (pwd, len + 2);
5252 pwd[len] = DIRECTORY_SEP;
5253 pwd[len + 1] = '\0';
5255 #endif /* not VMS */
5257 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5258 if (! NILP (buffer_defaults.enable_multibyte_characters))
5259 /* At this moment, we still don't know how to decode the
5260 directory name. So, we keep the bytes in multibyte form so
5261 that ENCODE_FILE correctly gets the original bytes. */
5262 current_buffer->directory
5263 = string_to_multibyte (current_buffer->directory);
5265 /* Add /: to the front of the name
5266 if it would otherwise be treated as magic. */
5267 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5268 if (! NILP (temp)
5269 /* If the default dir is just /, TEMP is non-nil
5270 because of the ange-ftp completion handler.
5271 However, it is not necessary to turn / into /:/.
5272 So avoid doing that. */
5273 && strcmp ("/", SDATA (current_buffer->directory)))
5274 current_buffer->directory
5275 = concat2 (build_string ("/:"), current_buffer->directory);
5277 temp = get_minibuffer (0);
5278 XBUFFER (temp)->directory = current_buffer->directory;
5280 free (pwd);
5283 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5284 Object stored in the current buffer. address is the address of the slot
5285 in the buffer that is current now. */
5287 /* TYPE is nil for a general Lisp variable.
5288 An integer specifies a type; then only LIsp values
5289 with that type code are allowed (except that nil is allowed too).
5290 LNAME is the LIsp-level variable name.
5291 VNAME is the name of the buffer slot.
5292 DOC is a dummy where you write the doc string as a comment. */
5293 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5294 defvar_per_buffer (lname, vname, type, 0)
5296 static void
5297 defvar_per_buffer (namestring, address, type, doc)
5298 char *namestring;
5299 Lisp_Object *address;
5300 Lisp_Object type;
5301 char *doc;
5303 Lisp_Object sym, val;
5304 int offset;
5306 sym = intern (namestring);
5307 val = allocate_misc ();
5308 offset = (char *)address - (char *)current_buffer;
5310 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5311 XBUFFER_OBJFWD (val)->offset = offset;
5312 XBUFFER_OBJFWD (val)->slottype = type;
5313 SET_SYMBOL_VALUE (sym, val);
5314 PER_BUFFER_SYMBOL (offset) = sym;
5316 if (PER_BUFFER_IDX (offset) == 0)
5317 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5318 slot of buffer_local_flags */
5319 abort ();
5323 /* initialize the buffer routines */
5324 void
5325 syms_of_buffer ()
5327 staticpro (&last_overlay_modification_hooks);
5328 last_overlay_modification_hooks
5329 = Fmake_vector (make_number (10), Qnil);
5331 staticpro (&Vbuffer_defaults);
5332 staticpro (&Vbuffer_local_symbols);
5333 staticpro (&Qfundamental_mode);
5334 staticpro (&Qmode_class);
5335 staticpro (&QSFundamental);
5336 staticpro (&Vbuffer_alist);
5337 staticpro (&Qprotected_field);
5338 staticpro (&Qpermanent_local);
5339 Qpermanent_local_hook = intern ("permanent-local-hook");
5340 staticpro (&Qpermanent_local_hook);
5341 staticpro (&Qkill_buffer_hook);
5342 Qoverlayp = intern ("overlayp");
5343 staticpro (&Qoverlayp);
5344 Qevaporate = intern ("evaporate");
5345 staticpro (&Qevaporate);
5346 Qmodification_hooks = intern ("modification-hooks");
5347 staticpro (&Qmodification_hooks);
5348 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5349 staticpro (&Qinsert_in_front_hooks);
5350 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5351 staticpro (&Qinsert_behind_hooks);
5352 Qget_file_buffer = intern ("get-file-buffer");
5353 staticpro (&Qget_file_buffer);
5354 Qpriority = intern ("priority");
5355 staticpro (&Qpriority);
5356 Qwindow = intern ("window");
5357 staticpro (&Qwindow);
5358 Qbefore_string = intern ("before-string");
5359 staticpro (&Qbefore_string);
5360 Qafter_string = intern ("after-string");
5361 staticpro (&Qafter_string);
5362 Qfirst_change_hook = intern ("first-change-hook");
5363 staticpro (&Qfirst_change_hook);
5364 Qbefore_change_functions = intern ("before-change-functions");
5365 staticpro (&Qbefore_change_functions);
5366 Qafter_change_functions = intern ("after-change-functions");
5367 staticpro (&Qafter_change_functions);
5368 /* The next one is initialized in init_buffer_once. */
5369 staticpro (&Qucs_set_table_for_input);
5371 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5372 staticpro (&Qkill_buffer_query_functions);
5374 Fput (Qprotected_field, Qerror_conditions,
5375 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5376 Fput (Qprotected_field, Qerror_message,
5377 build_string ("Attempt to modify a protected field"));
5379 /* All these use DEFVAR_LISP_NOPRO because the slots in
5380 buffer_defaults will all be marked via Vbuffer_defaults. */
5382 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5383 &buffer_defaults.mode_line_format,
5384 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5385 This is the same as (default-value 'mode-line-format). */);
5387 DEFVAR_LISP_NOPRO ("default-header-line-format",
5388 &buffer_defaults.header_line_format,
5389 doc: /* Default value of `header-line-format' for buffers that don't override it.
5390 This is the same as (default-value 'header-line-format). */);
5392 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5393 doc: /* Default value of `cursor-type' for buffers that don't override it.
5394 This is the same as (default-value 'cursor-type). */);
5396 DEFVAR_LISP_NOPRO ("default-line-spacing",
5397 &buffer_defaults.extra_line_spacing,
5398 doc: /* Default value of `line-spacing' for buffers that don't override it.
5399 This is the same as (default-value 'line-spacing). */);
5401 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5402 &buffer_defaults.cursor_in_non_selected_windows,
5403 doc: /* Default value of `cursor-in-non-selected-windows'.
5404 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5406 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5407 &buffer_defaults.abbrev_mode,
5408 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5409 This is the same as (default-value 'abbrev-mode). */);
5411 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5412 &buffer_defaults.ctl_arrow,
5413 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5414 This is the same as (default-value 'ctl-arrow). */);
5416 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5417 &buffer_defaults.direction_reversed,
5418 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5419 This is the same as (default-value 'direction-reversed). */);
5421 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5422 &buffer_defaults.enable_multibyte_characters,
5423 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5424 This is the same as (default-value 'enable-multibyte-characters). */);
5426 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5427 &buffer_defaults.buffer_file_coding_system,
5428 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5429 This is the same as (default-value 'buffer-file-coding-system). */);
5431 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5432 &buffer_defaults.truncate_lines,
5433 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5434 This is the same as (default-value 'truncate-lines). */);
5436 DEFVAR_LISP_NOPRO ("default-fill-column",
5437 &buffer_defaults.fill_column,
5438 doc: /* Default value of `fill-column' for buffers that do not override it.
5439 This is the same as (default-value 'fill-column). */);
5441 DEFVAR_LISP_NOPRO ("default-left-margin",
5442 &buffer_defaults.left_margin,
5443 doc: /* Default value of `left-margin' for buffers that do not override it.
5444 This is the same as (default-value 'left-margin). */);
5446 DEFVAR_LISP_NOPRO ("default-tab-width",
5447 &buffer_defaults.tab_width,
5448 doc: /* Default value of `tab-width' for buffers that do not override it.
5449 This is the same as (default-value 'tab-width). */);
5451 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5452 &buffer_defaults.case_fold_search,
5453 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5454 This is the same as (default-value 'case-fold-search). */);
5456 #ifdef DOS_NT
5457 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5458 &buffer_defaults.buffer_file_type,
5459 doc: /* Default file type for buffers that do not override it.
5460 This is the same as (default-value 'buffer-file-type).
5461 The file type is nil for text, t for binary. */);
5462 #endif
5464 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5465 &buffer_defaults.left_margin_cols,
5466 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5467 This is the same as (default-value 'left-margin-width). */);
5469 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5470 &buffer_defaults.right_margin_cols,
5471 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5472 This is the same as (default-value 'right-margin-width). */);
5474 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5475 &buffer_defaults.left_fringe_width,
5476 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5477 This is the same as (default-value 'left-fringe-width). */);
5479 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5480 &buffer_defaults.right_fringe_width,
5481 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5482 This is the same as (default-value 'right-fringe-width). */);
5484 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5485 &buffer_defaults.fringes_outside_margins,
5486 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5487 This is the same as (default-value 'fringes-outside-margins). */);
5489 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5490 &buffer_defaults.scroll_bar_width,
5491 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5492 This is the same as (default-value 'scroll-bar-width). */);
5494 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5495 &buffer_defaults.vertical_scroll_bar_type,
5496 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5497 This is the same as (default-value 'vertical-scroll-bar). */);
5499 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5500 &buffer_defaults.indicate_empty_lines,
5501 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5502 This is the same as (default-value 'indicate-empty-lines). */);
5504 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5505 &buffer_defaults.indicate_buffer_boundaries,
5506 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5507 This is the same as (default-value 'indicate-buffer-boundaries). */);
5509 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5510 &buffer_defaults.fringe_indicator_alist,
5511 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5512 This is the same as (default-value 'fringe-indicator-alist'). */);
5514 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5515 &buffer_defaults.fringe_cursor_alist,
5516 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5517 This is the same as (default-value 'fringe-cursor-alist'). */);
5519 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5520 &buffer_defaults.scroll_up_aggressively,
5521 doc: /* Default value of `scroll-up-aggressively'.
5522 This value applies in buffers that don't have their own local values.
5523 This is the same as (default-value 'scroll-up-aggressively). */);
5525 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5526 &buffer_defaults.scroll_down_aggressively,
5527 doc: /* Default value of `scroll-down-aggressively'.
5528 This value applies in buffers that don't have their own local values.
5529 This is the same as (default-value 'scroll-down-aggressively). */);
5531 DEFVAR_PER_BUFFER ("header-line-format",
5532 &current_buffer->header_line_format,
5533 Qnil,
5534 doc: /* Analogous to `mode-line-format', but controls the header line.
5535 The header line appears, optionally, at the top of a window;
5536 the mode line appears at the bottom. */);
5538 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5539 Qnil,
5540 doc: /* Template for displaying mode line for current buffer.
5541 Each buffer has its own value of this variable.
5542 Value may be nil, a string, a symbol or a list or cons cell.
5543 A value of nil means don't display a mode line.
5544 For a symbol, its value is used (but it is ignored if t or nil).
5545 A string appearing directly as the value of a symbol is processed verbatim
5546 in that the %-constructs below are not recognized.
5547 Note that unless the symbol is marked as a `risky-local-variable', all
5548 properties in any strings, as well as all :eval and :propertize forms
5549 in the value of that symbol will be ignored.
5550 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5551 is used as a mode line element. Be careful--FORM should not load any files,
5552 because that can cause an infinite recursion.
5553 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5554 with the specified properties PROPS applied.
5555 For a list whose car is a symbol, the symbol's value is taken,
5556 and if that is non-nil, the cadr of the list is processed recursively.
5557 Otherwise, the caddr of the list (if there is one) is processed.
5558 For a list whose car is a string or list, each element is processed
5559 recursively and the results are effectively concatenated.
5560 For a list whose car is an integer, the cdr of the list is processed
5561 and padded (if the number is positive) or truncated (if negative)
5562 to the width specified by that number.
5563 A string is printed verbatim in the mode line except for %-constructs:
5564 (%-constructs are allowed when the string is the entire mode-line-format
5565 or when it is found in a cons-cell or a list)
5566 %b -- print buffer name. %f -- print visited file name.
5567 %F -- print frame name.
5568 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5569 %& is like %*, but ignore read-only-ness.
5570 % means buffer is read-only and * means it is modified.
5571 For a modified read-only buffer, %* gives % and %+ gives *.
5572 %s -- print process status. %l -- print the current line number.
5573 %c -- print the current column number (this makes editing slower).
5574 To make the column number update correctly in all cases,
5575 `column-number-mode' must be non-nil.
5576 %i -- print the size of the buffer.
5577 %I -- like %i, but use k, M, G, etc., to abbreviate.
5578 %p -- print percent of buffer above top of window, or Top, Bot or All.
5579 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5580 or print Bottom or All.
5581 %n -- print Narrow if appropriate.
5582 %t -- visited file is text or binary (if OS supports this distinction).
5583 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5584 %Z -- like %z, but including the end-of-line format.
5585 %e -- print error message about full memory.
5586 %@ -- print @ or hyphen. @ means that default-directory is on a
5587 remote machine.
5588 %[ -- print one [ for each recursive editing level. %] similar.
5589 %% -- print %. %- -- print infinitely many dashes.
5590 Decimal digits after the % specify field width to which to pad. */);
5592 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5593 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5594 A value of nil means use current buffer's major mode,
5595 provided it is not marked as "special".
5597 When a mode is used by default, `find-file' switches to it
5598 before it reads the contents into the buffer and before
5599 it finishes setting up the buffer. Thus, the mode and
5600 its hooks should not expect certain variables such as
5601 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5603 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5604 make_number (Lisp_Symbol),
5605 doc: /* Symbol for current buffer's major mode. */);
5607 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5608 Qnil,
5609 doc: /* Pretty name of current buffer's major mode.
5610 Usually a string. See `mode-line-format' for other possible forms.
5611 Use the function `format-mode-line' to get the value as a string. */);
5613 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5614 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5616 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5617 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5619 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5620 Qnil,
5621 doc: /* *Non-nil if searches and matches should ignore case. */);
5623 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5624 make_number (Lisp_Int),
5625 doc: /* *Column beyond which automatic line-wrapping should happen.
5626 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5628 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5629 make_number (Lisp_Int),
5630 doc: /* *Column for the default `indent-line-function' to indent to.
5631 Linefeed indents to this column in Fundamental mode. */);
5633 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5634 make_number (Lisp_Int),
5635 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5637 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5638 doc: /* *Non-nil means display control chars with uparrow.
5639 A value of nil means use backslash and octal digits.
5640 This variable does not apply to characters whose display is specified
5641 in the current display table (if there is one). */);
5643 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5644 &current_buffer->enable_multibyte_characters,
5645 Qnil,
5646 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5647 Otherwise they are regarded as unibyte. This affects the display,
5648 file I/O and the behavior of various editing commands.
5650 This variable is buffer-local but you cannot set it directly;
5651 use the function `set-buffer-multibyte' to change a buffer's representation.
5652 Changing its default value with `setq-default' is supported.
5653 See also variable `default-enable-multibyte-characters' and Info node
5654 `(elisp)Text Representations'. */);
5655 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5657 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5658 &current_buffer->buffer_file_coding_system, Qnil,
5659 doc: /* Coding system to be used for encoding the buffer contents on saving.
5660 This variable applies to saving the buffer, and also to `write-region'
5661 and other functions that use `write-region'.
5662 It does not apply to sending output to subprocesses, however.
5664 If this is nil, the buffer is saved without any code conversion
5665 unless some coding system is specified in `file-coding-system-alist'
5666 for the buffer file.
5668 If the text to be saved cannot be encoded as specified by this variable,
5669 an alternative encoding is selected by `select-safe-coding-system', which see.
5671 The variable `coding-system-for-write', if non-nil, overrides this variable.
5673 This variable is never applied to a way of decoding a file while reading it. */);
5675 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5676 Qnil,
5677 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5679 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5680 doc: /* *Non-nil means do not display continuation lines.
5681 Instead, give each line of text just one screen line.
5683 Note that this is overridden by the variable
5684 `truncate-partial-width-windows' if that variable is non-nil
5685 and this buffer is not full-frame width. */);
5687 #ifdef DOS_NT
5688 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5689 Qnil,
5690 doc: /* Non-nil if the visited file is a binary file.
5691 This variable is meaningful on MS-DOG and Windows NT.
5692 On those systems, it is automatically local in every buffer.
5693 On other systems, this variable is normally always nil. */);
5694 #endif
5696 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5697 make_number (Lisp_String),
5698 doc: /* Name of default directory of current buffer. Should end with slash.
5699 To interactively change the default directory, use command `cd'. */);
5701 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5702 Qnil,
5703 doc: /* Function called (if non-nil) to perform auto-fill.
5704 It is called after self-inserting any character specified in
5705 the `auto-fill-chars' table.
5706 NOTE: This variable is not a hook;
5707 its value may not be a list of functions. */);
5709 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5710 make_number (Lisp_String),
5711 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5713 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5714 make_number (Lisp_String),
5715 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5716 The truename of a file is calculated by `file-truename'
5717 and then abbreviated with `abbreviate-file-name'. */);
5719 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5720 &current_buffer->auto_save_file_name,
5721 make_number (Lisp_String),
5722 doc: /* Name of file for auto-saving current buffer.
5723 If it is nil, that means don't auto-save this buffer. */);
5725 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5726 doc: /* Non-nil if this buffer is read-only. */);
5728 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5729 doc: /* Non-nil if this buffer's file has been backed up.
5730 Backing up is done before the first time the file is saved. */);
5732 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5733 make_number (Lisp_Int),
5734 doc: /* Length of current buffer when last read in, saved or auto-saved.
5735 0 initially. */);
5737 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5738 Qnil,
5739 doc: /* Non-nil enables selective display.
5740 An integer N as value means display only lines
5741 that start with less than N columns of space.
5742 A value of t means that the character ^M makes itself and
5743 all the rest of the line invisible; also, when saving the buffer
5744 in a file, save the ^M as a newline. */);
5746 #ifndef old
5747 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5748 &current_buffer->selective_display_ellipses,
5749 Qnil,
5750 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5751 #endif
5753 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5754 doc: /* Non-nil if self-insertion should replace existing text.
5755 The value should be one of `overwrite-mode-textual',
5756 `overwrite-mode-binary', or nil.
5757 If it is `overwrite-mode-textual', self-insertion still
5758 inserts at the end of a line, and inserts when point is before a tab,
5759 until the tab is filled in.
5760 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5762 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5763 Qnil,
5764 doc: /* Display table that controls display of the contents of current buffer.
5766 If this variable is nil, the value of `standard-display-table' is used.
5767 Each window can have its own, overriding display table, see
5768 `set-window-display-table' and `window-display-table'.
5770 The display table is a char-table created with `make-display-table'.
5771 A char-table is an array indexed by character codes. Normal array
5772 primitives `aref' and `aset' can be used to access elements of a char-table.
5774 Each of the char-table elements control how to display the corresponding
5775 text character: the element at index C in the table says how to display
5776 the character whose code is C. Each element should be a vector of
5777 characters or nil. The value nil means display the character in the
5778 default fashion; otherwise, the characters from the vector are delivered
5779 to the screen instead of the original character.
5781 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5782 to display a capital Y instead of each X character.
5784 In addition, a char-table has six extra slots to control the display of:
5786 the end of a truncated screen line (extra-slot 0, a single character);
5787 the end of a continued line (extra-slot 1, a single character);
5788 the escape character used to display character codes in octal
5789 (extra-slot 2, a single character);
5790 the character used as an arrow for control characters (extra-slot 3,
5791 a single character);
5792 the decoration indicating the presence of invisible lines (extra-slot 4,
5793 a vector of characters);
5794 the character used to draw the border between side-by-side windows
5795 (extra-slot 5, a single character).
5797 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5799 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5800 Qnil,
5801 doc: /* *Width of left marginal area for display of a buffer.
5802 A value of nil means no marginal area. */);
5804 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5805 Qnil,
5806 doc: /* *Width of right marginal area for display of a buffer.
5807 A value of nil means no marginal area. */);
5809 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5810 Qnil,
5811 doc: /* *Width of this buffer's left fringe (in pixels).
5812 A value of 0 means no left fringe is shown in this buffer's window.
5813 A value of nil means to use the left fringe width from the window's frame. */);
5815 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5816 Qnil,
5817 doc: /* *Width of this buffer's right fringe (in pixels).
5818 A value of 0 means no right fringe is shown in this buffer's window.
5819 A value of nil means to use the right fringe width from the window's frame. */);
5821 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5822 Qnil,
5823 doc: /* *Non-nil means to display fringes outside display margins.
5824 A value of nil means to display fringes between margins and buffer text. */);
5826 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5827 Qnil,
5828 doc: /* *Width of this buffer's scroll bars in pixels.
5829 A value of nil means to use the scroll bar width from the window's frame. */);
5831 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5832 Qnil,
5833 doc: /* *Position of this buffer's vertical scroll bar.
5834 The value takes effect whenever you tell a window to display this buffer;
5835 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5837 A value of `left' or `right' means put the vertical scroll bar at that side
5838 of the window; a value of nil means don't show any vertical scroll bars.
5839 A value of t (the default) means do whatever the window's frame specifies. */);
5841 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5842 &current_buffer->indicate_empty_lines, Qnil,
5843 doc: /* *Visually indicate empty lines after the buffer end.
5844 If non-nil, a bitmap is displayed in the left fringe of a window on
5845 window-systems. */);
5847 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5848 &current_buffer->indicate_buffer_boundaries, Qnil,
5849 doc: /* *Visually indicate buffer boundaries and scrolling.
5850 If non-nil, the first and last line of the buffer are marked in the fringe
5851 of a window on window-systems with angle bitmaps, or if the window can be
5852 scrolled, the top and bottom line of the window are marked with up and down
5853 arrow bitmaps.
5855 If value is a symbol `left' or `right', both angle and arrow bitmaps
5856 are displayed in the left or right fringe, resp. Any other value
5857 that doesn't look like an alist means display the angle bitmaps in
5858 the left fringe but no arrows.
5860 You can exercise more precise control by using an alist as the
5861 value. Each alist element (INDICATOR . POSITION) specifies
5862 where to show one of the indicators. INDICATOR is one of `top',
5863 `bottom', `up', `down', or t, which specifies the default position,
5864 and POSITION is one of `left', `right', or nil, meaning do not show
5865 this indicator.
5867 For example, ((top . left) (t . right)) places the top angle bitmap in
5868 left fringe, the bottom angle bitmap in right fringe, and both arrow
5869 bitmaps in right fringe. To show just the angle bitmaps in the left
5870 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5872 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5873 &current_buffer->fringe_indicator_alist, Qnil,
5874 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5875 The value is an alist where each element (INDICATOR . BITMAPS)
5876 specifies the fringe bitmaps used to display a specific logical
5877 fringe indicator.
5879 INDICATOR specifies the logical indicator type which is one of the
5880 following symbols: `truncation' , `continuation', `overlay-arrow',
5881 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5883 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5884 the actual bitmap shown in the left or right fringe for the logical
5885 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5886 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5887 are used only for the `bottom' and `one-line' indicators when the last
5888 \(only) line in has no final newline. BITMAPS may also be a single
5889 symbol which is used in both left and right fringes. */);
5891 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5892 &current_buffer->fringe_cursor_alist, Qnil,
5893 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5894 The value is an alist where each element (CURSOR . BITMAP)
5895 specifies the fringe bitmaps used to display a specific logical
5896 cursor type in the fringe.
5898 CURSOR specifies the logical cursor type which is one of the following
5899 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5900 one is used to show a hollow cursor on narrow lines display lines
5901 where the normal hollow cursor will not fit.
5903 BITMAP is the corresponding fringe bitmap shown for the logical
5904 cursor type. */);
5906 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5907 &current_buffer->scroll_up_aggressively, Qnil,
5908 doc: /* How far to scroll windows upward.
5909 If you move point off the bottom, the window scrolls automatically.
5910 This variable controls how far it scrolls. The value nil, the default,
5911 means scroll to center point. A fraction means scroll to put point
5912 that fraction of the window's height from the bottom of the window.
5913 When the value is 0.0, point goes at the bottom line, which in the simple
5914 case that you moved off with C-f means scrolling just one line. 1.0 means
5915 point goes at the top, so that in that simple case, the window
5916 scrolls by a full window height. Meaningful values are
5917 between 0.0 and 1.0, inclusive. */);
5919 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5920 &current_buffer->scroll_down_aggressively, Qnil,
5921 doc: /* How far to scroll windows downward.
5922 If you move point off the top, the window scrolls automatically.
5923 This variable controls how far it scrolls. The value nil, the default,
5924 means scroll to center point. A fraction means scroll to put point
5925 that fraction of the window's height from the top of the window.
5926 When the value is 0.0, point goes at the top line, which in the simple
5927 case that you moved off with C-b means scrolling just one line. 1.0 means
5928 point goes at the bottom, so that in that simple case, the window
5929 scrolls by a full window height. Meaningful values are
5930 between 0.0 and 1.0, inclusive. */);
5932 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5933 "Don't ask.");
5936 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5937 doc: /* List of functions to call before each text change.
5938 Two arguments are passed to each function: the positions of
5939 the beginning and end of the range of old text to be changed.
5940 \(For an insertion, the beginning and end are at the same place.)
5941 No information is given about the length of the text after the change.
5943 Buffer changes made while executing the `before-change-functions'
5944 don't call any before-change or after-change functions.
5945 That's because these variables are temporarily set to nil.
5946 As a result, a hook function cannot straightforwardly alter the value of
5947 these variables. See the Emacs Lisp manual for a way of
5948 accomplishing an equivalent result by using other variables.
5950 If an unhandled error happens in running these functions,
5951 the variable's value remains nil. That prevents the error
5952 from happening repeatedly and making Emacs nonfunctional. */);
5953 Vbefore_change_functions = Qnil;
5955 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5956 doc: /* List of functions to call after each text change.
5957 Three arguments are passed to each function: the positions of
5958 the beginning and end of the range of changed text,
5959 and the length in bytes of the pre-change text replaced by that range.
5960 \(For an insertion, the pre-change length is zero;
5961 for a deletion, that length is the number of bytes deleted,
5962 and the post-change beginning and end are at the same place.)
5964 Buffer changes made while executing the `after-change-functions'
5965 don't call any before-change or after-change functions.
5966 That's because these variables are temporarily set to nil.
5967 As a result, a hook function cannot straightforwardly alter the value of
5968 these variables. See the Emacs Lisp manual for a way of
5969 accomplishing an equivalent result by using other variables.
5971 If an unhandled error happens in running these functions,
5972 the variable's value remains nil. That prevents the error
5973 from happening repeatedly and making Emacs nonfunctional. */);
5974 Vafter_change_functions = Qnil;
5976 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5977 doc: /* A list of functions to call before changing a buffer which is unmodified.
5978 The functions are run using the `run-hooks' function. */);
5979 Vfirst_change_hook = Qnil;
5981 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5982 doc: /* List of undo entries in current buffer.
5983 Recent changes come first; older changes follow newer.
5985 An entry (BEG . END) represents an insertion which begins at
5986 position BEG and ends at position END.
5988 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5989 from (abs POSITION). If POSITION is positive, point was at the front
5990 of the text being deleted; if negative, point was at the end.
5992 An entry (t HIGH . LOW) indicates that the buffer previously had
5993 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5994 of the visited file's modification time, as of that time. If the
5995 modification time of the most recent save is different, this entry is
5996 obsolete.
5998 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5999 was modified between BEG and END. PROPERTY is the property name,
6000 and VALUE is the old value.
6002 An entry (apply FUN-NAME . ARGS) means undo the change with
6003 \(apply FUN-NAME ARGS).
6005 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6006 in the active region. BEG and END is the range affected by this entry
6007 and DELTA is the number of bytes added or deleted in that range by
6008 this change.
6010 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6011 was adjusted in position by the offset DISTANCE (an integer).
6013 An entry of the form POSITION indicates that point was at the buffer
6014 location given by the integer. Undoing an entry of this form places
6015 point at POSITION.
6017 Entries with value `nil' mark undo boundaries. The undo command treats
6018 the changes between two undo boundaries as a single step to be undone.
6020 If the value of the variable is t, undo information is not recorded. */);
6022 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6023 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6025 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6026 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6028 Normally, the line-motion functions work by scanning the buffer for
6029 newlines. Columnar operations (like `move-to-column' and
6030 `compute-motion') also work by scanning the buffer, summing character
6031 widths as they go. This works well for ordinary text, but if the
6032 buffer's lines are very long (say, more than 500 characters), these
6033 motion functions will take longer to execute. Emacs may also take
6034 longer to update the display.
6036 If `cache-long-line-scans' is non-nil, these motion functions cache the
6037 results of their scans, and consult the cache to avoid rescanning
6038 regions of the buffer until the text is modified. The caches are most
6039 beneficial when they prevent the most searching---that is, when the
6040 buffer contains long lines and large regions of characters with the
6041 same, fixed screen width.
6043 When `cache-long-line-scans' is non-nil, processing short lines will
6044 become slightly slower (because of the overhead of consulting the
6045 cache), and the caches will use memory roughly proportional to the
6046 number of newlines and characters whose screen width varies.
6048 The caches require no explicit maintenance; their accuracy is
6049 maintained internally by the Emacs primitives. Enabling or disabling
6050 the cache should not affect the behavior of any of the motion
6051 functions; it should only affect their performance. */);
6053 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6054 doc: /* Value of point before the last series of scroll operations, or nil. */);
6056 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6057 doc: /* List of formats to use when saving this buffer.
6058 Formats are defined by `format-alist'. This variable is
6059 set when a file is visited. */);
6061 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6062 &current_buffer->auto_save_file_format, Qnil,
6063 doc: /* *Format in which to write auto-save files.
6064 Should be a list of symbols naming formats that are defined in `format-alist'.
6065 If it is t, which is the default, auto-save files are written in the
6066 same format as a regular save would use. */);
6068 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6069 &current_buffer->invisibility_spec, Qnil,
6070 doc: /* Invisibility spec of this buffer.
6071 The default is t, which means that text is invisible
6072 if it has a non-nil `invisible' property.
6073 If the value is a list, a text character is invisible if its `invisible'
6074 property is an element in that list (or is a list with members in common).
6075 If an element is a cons cell of the form (PROP . ELLIPSIS),
6076 then characters with property value PROP are invisible,
6077 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6079 DEFVAR_PER_BUFFER ("buffer-display-count",
6080 &current_buffer->display_count, Qnil,
6081 doc: /* A number incremented each time this buffer is displayed in a window.
6082 The function `set-window-buffer' increments it. */);
6084 DEFVAR_PER_BUFFER ("buffer-display-time",
6085 &current_buffer->display_time, Qnil,
6086 doc: /* Time stamp updated each time this buffer is displayed in a window.
6087 The function `set-window-buffer' updates this variable
6088 to the value obtained by calling `current-time'.
6089 If the buffer has never been shown in a window, the value is nil. */);
6091 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6092 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
6093 Non-nil also enables highlighting of the region whenever the mark is active.
6094 The variable `highlight-nonselected-windows' controls whether to highlight
6095 all windows or just the selected window.
6097 If the value is `lambda', that enables Transient Mark mode temporarily
6098 until the next buffer modification. If a command sets the value to `only',
6099 that enables Transient Mark mode for the following command only.
6100 During that following command, the value of `transient-mark-mode'
6101 is `identity'. If it is still `identity' at the end of that command,
6102 it changes to nil. */);
6103 Vtransient_mark_mode = Qnil;
6105 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6106 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6107 If the value is t, disregard `buffer-read-only' and all `read-only'
6108 text properties. If the value is a list, disregard `buffer-read-only'
6109 and disregard a `read-only' text property if the property value
6110 is a member of the list. */);
6111 Vinhibit_read_only = Qnil;
6113 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6114 doc: /* Cursor to use when this buffer is in the selected window.
6115 Values are interpreted as follows:
6117 t use the cursor specified for the frame
6118 nil don't display a cursor
6119 box display a filled box cursor
6120 hollow display a hollow box cursor
6121 bar display a vertical bar cursor with default width
6122 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6123 hbar display a horizontal bar cursor with default height
6124 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6125 ANYTHING ELSE display a hollow box cursor
6127 When the buffer is displayed in a nonselected window,
6128 this variable has no effect; the cursor appears as a hollow box. */);
6130 DEFVAR_PER_BUFFER ("line-spacing",
6131 &current_buffer->extra_line_spacing, Qnil,
6132 doc: /* Additional space to put between lines when displaying a buffer.
6133 The space is measured in pixels, and put below lines on window systems.
6134 If value is a floating point number, it specifies the spacing relative
6135 to the default frame line height. A value of nil means add no extra space. */);
6137 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6138 &current_buffer->cursor_in_non_selected_windows, Qnil,
6139 doc: /* *Cursor type to display in non-selected windows.
6140 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6142 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6143 doc: /* List of functions called with no args to query before killing a buffer.
6144 The buffer being killed will be current while the functions are running.
6145 If any of them returns nil, the buffer is not killed. */);
6146 Vkill_buffer_query_functions = Qnil;
6148 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6149 doc: /* Normal hook run before changing the major mode of a buffer.
6150 The function `kill-all-local-variables' runs this before doing anything else. */);
6151 Vchange_major_mode_hook = Qnil;
6152 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6153 staticpro (&Qchange_major_mode_hook);
6155 defsubr (&Sbuffer_live_p);
6156 defsubr (&Sbuffer_list);
6157 defsubr (&Sget_buffer);
6158 defsubr (&Sget_file_buffer);
6159 defsubr (&Sget_buffer_create);
6160 defsubr (&Smake_indirect_buffer);
6161 defsubr (&Sgenerate_new_buffer_name);
6162 defsubr (&Sbuffer_name);
6163 /*defsubr (&Sbuffer_number);*/
6164 defsubr (&Sbuffer_file_name);
6165 defsubr (&Sbuffer_base_buffer);
6166 defsubr (&Sbuffer_local_value);
6167 defsubr (&Sbuffer_local_variables);
6168 defsubr (&Sbuffer_modified_p);
6169 defsubr (&Sset_buffer_modified_p);
6170 defsubr (&Sbuffer_modified_tick);
6171 defsubr (&Sbuffer_chars_modified_tick);
6172 defsubr (&Srename_buffer);
6173 defsubr (&Sother_buffer);
6174 defsubr (&Sbuffer_enable_undo);
6175 defsubr (&Skill_buffer);
6176 defsubr (&Sset_buffer_major_mode);
6177 defsubr (&Sswitch_to_buffer);
6178 defsubr (&Spop_to_buffer);
6179 defsubr (&Scurrent_buffer);
6180 defsubr (&Sset_buffer);
6181 defsubr (&Sbarf_if_buffer_read_only);
6182 defsubr (&Sbury_buffer);
6183 defsubr (&Serase_buffer);
6184 defsubr (&Sset_buffer_multibyte);
6185 defsubr (&Skill_all_local_variables);
6187 defsubr (&Soverlayp);
6188 defsubr (&Smake_overlay);
6189 defsubr (&Sdelete_overlay);
6190 defsubr (&Smove_overlay);
6191 defsubr (&Soverlay_start);
6192 defsubr (&Soverlay_end);
6193 defsubr (&Soverlay_buffer);
6194 defsubr (&Soverlay_properties);
6195 defsubr (&Soverlays_at);
6196 defsubr (&Soverlays_in);
6197 defsubr (&Snext_overlay_change);
6198 defsubr (&Sprevious_overlay_change);
6199 defsubr (&Soverlay_recenter);
6200 defsubr (&Soverlay_lists);
6201 defsubr (&Soverlay_get);
6202 defsubr (&Soverlay_put);
6203 defsubr (&Srestore_buffer_modified_p);
6206 void
6207 keys_of_buffer ()
6209 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6210 initial_define_key (control_x_map, 'k', "kill-buffer");
6212 /* This must not be in syms_of_buffer, because Qdisabled is not
6213 initialized when that function gets called. */
6214 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6217 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6218 (do not change this comment) */