Don't require xt-mouse.
[emacs.git] / src / buffer.c
blobc53d427499a166e706c8460583d575d1a26ab63d
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 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/param.h>
28 #include <errno.h>
29 #include <stdio.h>
31 #ifndef USE_CRT_DLL
32 extern int errno;
33 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #include "lisp.h"
41 #include "intervals.h"
42 #include "window.h"
43 #include "commands.h"
44 #include "buffer.h"
45 #include "charset.h"
46 #include "region-cache.h"
47 #include "indent.h"
48 #include "blockinput.h"
49 #include "keyboard.h"
50 #include "keymap.h"
51 #include "frame.h"
53 struct buffer *current_buffer; /* the current buffer */
55 /* First buffer in chain of all buffers (in reverse order of creation).
56 Threaded through ->next. */
58 struct buffer *all_buffers;
60 /* This structure holds the default values of the buffer-local variables
61 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
62 The default value occupies the same slot in this structure
63 as an individual buffer's value occupies in that buffer.
64 Setting the default value also goes through the alist of buffers
65 and stores into each buffer that does not say it has a local value. */
67 DECL_ALIGN (struct buffer, buffer_defaults);
69 /* A Lisp_Object pointer to the above, used for staticpro */
71 static Lisp_Object Vbuffer_defaults;
73 /* This structure marks which slots in a buffer have corresponding
74 default values in buffer_defaults.
75 Each such slot has a nonzero value in this structure.
76 The value has only one nonzero bit.
78 When a buffer has its own local value for a slot,
79 the entry for that slot (found in the same slot in this structure)
80 is turned on in the buffer's local_flags array.
82 If a slot in this structure is -1, then even though there may
83 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
84 and the corresponding slot in buffer_defaults is not used.
86 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
87 but there is a default value which is copied into each buffer.
89 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
90 zero, that is a bug */
92 struct buffer buffer_local_flags;
94 /* This structure holds the names of symbols whose values may be
95 buffer-local. It is indexed and accessed in the same way as the above. */
97 DECL_ALIGN (struct buffer, buffer_local_symbols);
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols;
102 /* This structure holds the required types for the values in the
103 buffer-local slots. If a slot contains Qnil, then the
104 corresponding buffer slot may contain a value of any type. If a
105 slot contains an integer, then prospective values' tags must be
106 equal to that integer (except nil is always allowed).
107 When a tag does not match, the function
108 buffer_slot_type_mismatch will signal an error.
110 If a slot here contains -1, the corresponding variable is read-only. */
111 struct buffer buffer_local_types;
113 /* Flags indicating which built-in buffer-local variables
114 are permanent locals. */
115 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
117 /* Number of per-buffer variables used. */
119 int last_per_buffer_idx;
121 Lisp_Object Fset_buffer ();
122 void set_buffer_internal ();
123 void set_buffer_internal_1 ();
124 static void call_overlay_mod_hooks ();
125 static void swap_out_buffer_local_variables ();
126 static void reset_buffer_local_variables ();
128 /* Alist of all buffer names vs the buffers. */
129 /* This used to be a variable, but is no longer,
130 to prevent lossage due to user rplac'ing this alist or its elements. */
131 Lisp_Object Vbuffer_alist;
133 /* Functions to call before and after each text change. */
134 Lisp_Object Vbefore_change_functions;
135 Lisp_Object Vafter_change_functions;
137 Lisp_Object Vtransient_mark_mode;
139 /* t means ignore all read-only text properties.
140 A list means ignore such a property if its value is a member of the list.
141 Any non-nil value means ignore buffer-read-only. */
142 Lisp_Object Vinhibit_read_only;
144 /* List of functions to call that can query about killing a buffer.
145 If any of these functions returns nil, we don't kill it. */
146 Lisp_Object Vkill_buffer_query_functions;
147 Lisp_Object Qkill_buffer_query_functions;
149 /* Hook run before changing a major mode. */
150 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
152 /* List of functions to call before changing an unmodified buffer. */
153 Lisp_Object Vfirst_change_hook;
155 Lisp_Object Qfirst_change_hook;
156 Lisp_Object Qbefore_change_functions;
157 Lisp_Object Qafter_change_functions;
158 Lisp_Object Qucs_set_table_for_input;
160 /* If nonzero, all modification hooks are suppressed. */
161 int inhibit_modification_hooks;
163 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
165 Lisp_Object Qprotected_field;
167 Lisp_Object QSFundamental; /* A string "Fundamental" */
169 Lisp_Object Qkill_buffer_hook;
171 Lisp_Object Qget_file_buffer;
173 Lisp_Object Qoverlayp;
175 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
177 Lisp_Object Qmodification_hooks;
178 Lisp_Object Qinsert_in_front_hooks;
179 Lisp_Object Qinsert_behind_hooks;
181 static void alloc_buffer_text P_ ((struct buffer *, size_t));
182 static void free_buffer_text P_ ((struct buffer *b));
183 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
184 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
185 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
188 /* For debugging; temporary. See set_buffer_internal. */
189 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
191 void
192 nsberror (spec)
193 Lisp_Object spec;
195 if (STRINGP (spec))
196 error ("No buffer named %s", SDATA (spec));
197 error ("Invalid buffer argument");
200 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
201 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
202 Value is nil if OBJECT is not a buffer or if it has been killed. */)
203 (object)
204 Lisp_Object object;
206 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
207 ? Qt : Qnil);
210 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
211 doc: /* Return a list of all existing live buffers.
212 If the optional arg FRAME is a frame, we return the buffer list
213 in the proper order for that frame: the buffers in FRAME's `buffer-list'
214 frame parameter come first, followed by the rest of the buffers. */)
215 (frame)
216 Lisp_Object frame;
218 Lisp_Object general;
219 general = Fmapcar (Qcdr, Vbuffer_alist);
221 if (FRAMEP (frame))
223 Lisp_Object framelist, prevlist, tail;
224 Lisp_Object args[3];
226 CHECK_FRAME (frame);
228 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
229 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
231 /* Remove from GENERAL any buffer that duplicates one in
232 FRAMELIST or PREVLIST. */
233 tail = framelist;
234 while (CONSP (tail))
236 general = Fdelq (XCAR (tail), general);
237 tail = XCDR (tail);
239 tail = prevlist;
240 while (CONSP (tail))
242 general = Fdelq (XCAR (tail), general);
243 tail = XCDR (tail);
246 args[0] = framelist;
247 args[1] = general;
248 args[2] = prevlist;
249 return Fnconc (3, args);
252 return general;
255 /* Like Fassoc, but use Fstring_equal to compare
256 (which ignores text properties),
257 and don't ever QUIT. */
259 static Lisp_Object
260 assoc_ignore_text_properties (key, list)
261 register Lisp_Object key;
262 Lisp_Object list;
264 register Lisp_Object tail;
265 for (tail = list; CONSP (tail); tail = XCDR (tail))
267 register Lisp_Object elt, tem;
268 elt = XCAR (tail);
269 tem = Fstring_equal (Fcar (elt), key);
270 if (!NILP (tem))
271 return elt;
273 return Qnil;
276 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
277 doc: /* Return the buffer named NAME (a string).
278 If there is no live buffer named NAME, return nil.
279 NAME may also be a buffer; if so, the value is that buffer. */)
280 (name)
281 register Lisp_Object name;
283 if (BUFFERP (name))
284 return name;
285 CHECK_STRING (name);
287 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
290 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
291 doc: /* Return the buffer visiting file FILENAME (a string).
292 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
293 If there is no such live buffer, return nil.
294 See also `find-buffer-visiting'. */)
295 (filename)
296 register Lisp_Object filename;
298 register Lisp_Object tail, buf, tem;
299 Lisp_Object handler;
301 CHECK_STRING (filename);
302 filename = Fexpand_file_name (filename, Qnil);
304 /* If the file name has special constructs in it,
305 call the corresponding file handler. */
306 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
307 if (!NILP (handler))
308 return call2 (handler, Qget_file_buffer, filename);
310 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
312 buf = Fcdr (XCAR (tail));
313 if (!BUFFERP (buf)) continue;
314 if (!STRINGP (XBUFFER (buf)->filename)) continue;
315 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
316 if (!NILP (tem))
317 return buf;
319 return Qnil;
322 Lisp_Object
323 get_truename_buffer (filename)
324 register Lisp_Object filename;
326 register Lisp_Object tail, buf, tem;
328 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
330 buf = Fcdr (XCAR (tail));
331 if (!BUFFERP (buf)) continue;
332 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
333 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
334 if (!NILP (tem))
335 return buf;
337 return Qnil;
340 /* Incremented for each buffer created, to assign the buffer number. */
341 int buffer_count;
343 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
344 doc: /* Return the buffer named NAME, or create such a buffer and return it.
345 A new buffer is created if there is no live buffer named NAME.
346 If NAME starts with a space, the new buffer does not keep undo information.
347 If NAME is a buffer instead of a string, then it is the value returned.
348 The value is never nil. */)
349 (name)
350 register Lisp_Object name;
352 register Lisp_Object buf;
353 register struct buffer *b;
355 buf = Fget_buffer (name);
356 if (!NILP (buf))
357 return buf;
359 if (SCHARS (name) == 0)
360 error ("Empty string for buffer name is not allowed");
362 b = (struct buffer *) allocate_buffer ();
364 /* An ordinary buffer uses its own struct buffer_text. */
365 b->text = &b->own_text;
366 b->base_buffer = 0;
368 BUF_GAP_SIZE (b) = 20;
369 BLOCK_INPUT;
370 /* We allocate extra 1-byte at the tail and keep it always '\0' for
371 anchoring a search. */
372 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
373 UNBLOCK_INPUT;
374 if (! BUF_BEG_ADDR (b))
375 buffer_memory_full ();
377 BUF_PT (b) = BEG;
378 BUF_GPT (b) = BEG;
379 BUF_BEGV (b) = BEG;
380 BUF_ZV (b) = BEG;
381 BUF_Z (b) = BEG;
382 BUF_PT_BYTE (b) = BEG_BYTE;
383 BUF_GPT_BYTE (b) = BEG_BYTE;
384 BUF_BEGV_BYTE (b) = BEG_BYTE;
385 BUF_ZV_BYTE (b) = BEG_BYTE;
386 BUF_Z_BYTE (b) = BEG_BYTE;
387 BUF_MODIFF (b) = 1;
388 BUF_CHARS_MODIFF (b) = 1;
389 BUF_OVERLAY_MODIFF (b) = 1;
390 BUF_SAVE_MODIFF (b) = 1;
391 BUF_INTERVALS (b) = 0;
392 BUF_UNCHANGED_MODIFIED (b) = 1;
393 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
394 BUF_END_UNCHANGED (b) = 0;
395 BUF_BEG_UNCHANGED (b) = 0;
396 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
398 b->newline_cache = 0;
399 b->width_run_cache = 0;
400 b->width_table = Qnil;
401 b->prevent_redisplay_optimizations_p = 1;
403 /* Put this on the chain of all buffers including killed ones. */
404 b->next = all_buffers;
405 all_buffers = b;
407 /* An ordinary buffer normally doesn't need markers
408 to handle BEGV and ZV. */
409 b->pt_marker = Qnil;
410 b->begv_marker = Qnil;
411 b->zv_marker = Qnil;
413 name = Fcopy_sequence (name);
414 STRING_SET_INTERVALS (name, NULL_INTERVAL);
415 b->name = name;
417 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
419 reset_buffer (b);
420 reset_buffer_local_variables (b, 1);
422 b->mark = Fmake_marker ();
423 BUF_MARKERS (b) = NULL;
424 b->name = name;
426 /* Put this in the alist of all live buffers. */
427 XSETBUFFER (buf, b);
428 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
430 /* An error in calling the function here (should someone redefine it)
431 can lead to infinite regress until you run out of stack. rms
432 says that's not worth protecting against. */
433 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
434 /* buf is on buffer-alist, so no gcpro. */
435 call1 (Qucs_set_table_for_input, buf);
437 return buf;
441 /* Return a list of overlays which is a copy of the overlay list
442 LIST, but for buffer B. */
444 static struct Lisp_Overlay *
445 copy_overlays (b, list)
446 struct buffer *b;
447 struct Lisp_Overlay *list;
449 Lisp_Object buffer;
450 struct Lisp_Overlay *result = NULL, *tail = NULL;
452 XSETBUFFER (buffer, b);
454 for (; list; list = list->next)
456 Lisp_Object overlay, start, end, old_overlay;
457 EMACS_INT charpos;
459 XSETMISC (old_overlay, list);
460 charpos = marker_position (OVERLAY_START (old_overlay));
461 start = Fmake_marker ();
462 Fset_marker (start, make_number (charpos), buffer);
463 XMARKER (start)->insertion_type
464 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
466 charpos = marker_position (OVERLAY_END (old_overlay));
467 end = Fmake_marker ();
468 Fset_marker (end, make_number (charpos), buffer);
469 XMARKER (end)->insertion_type
470 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
472 overlay = allocate_misc ();
473 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
474 OVERLAY_START (overlay) = start;
475 OVERLAY_END (overlay) = end;
476 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
477 XOVERLAY (overlay)->next = NULL;
479 if (tail)
480 tail = tail->next = XOVERLAY (overlay);
481 else
482 result = tail = XOVERLAY (overlay);
485 return result;
489 /* Clone per-buffer values of buffer FROM.
491 Buffer TO gets the same per-buffer values as FROM, with the
492 following exceptions: (1) TO's name is left untouched, (2) markers
493 are copied and made to refer to TO, and (3) overlay lists are
494 copied. */
496 static void
497 clone_per_buffer_values (from, to)
498 struct buffer *from, *to;
500 Lisp_Object to_buffer;
501 int offset;
503 XSETBUFFER (to_buffer, to);
505 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
506 offset < sizeof *to;
507 offset += sizeof (Lisp_Object))
509 Lisp_Object obj;
511 obj = PER_BUFFER_VALUE (from, offset);
512 if (MARKERP (obj))
514 struct Lisp_Marker *m = XMARKER (obj);
515 obj = Fmake_marker ();
516 XMARKER (obj)->insertion_type = m->insertion_type;
517 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
520 PER_BUFFER_VALUE (to, offset) = obj;
523 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
525 to->overlays_before = copy_overlays (to, from->overlays_before);
526 to->overlays_after = copy_overlays (to, from->overlays_after);
528 /* Get (a copy of) the alist of Lisp-level local variables of FROM
529 and install that in TO. */
530 to->local_var_alist = buffer_lisp_local_variables (from);
533 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
534 2, 3,
535 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
536 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
537 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
538 NAME should be a string which is not the name of an existing buffer.
539 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
540 such as major and minor modes, in the indirect buffer.
541 CLONE nil means the indirect buffer's state is reset to default values. */)
542 (base_buffer, name, clone)
543 Lisp_Object base_buffer, name, clone;
545 Lisp_Object buf, tem;
546 struct buffer *b;
548 CHECK_STRING (name);
549 buf = Fget_buffer (name);
550 if (!NILP (buf))
551 error ("Buffer name `%s' is in use", SDATA (name));
553 tem = base_buffer;
554 base_buffer = Fget_buffer (base_buffer);
555 if (NILP (base_buffer))
556 error ("No such buffer: `%s'", SDATA (tem));
557 if (NILP (XBUFFER (base_buffer)->name))
558 error ("Base buffer has been killed");
560 if (SCHARS (name) == 0)
561 error ("Empty string for buffer name is not allowed");
563 b = (struct buffer *) allocate_buffer ();
565 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
566 ? XBUFFER (base_buffer)->base_buffer
567 : XBUFFER (base_buffer));
569 /* Use the base buffer's text object. */
570 b->text = b->base_buffer->text;
572 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
573 BUF_ZV (b) = BUF_ZV (b->base_buffer);
574 BUF_PT (b) = BUF_PT (b->base_buffer);
575 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
576 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
577 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
579 b->newline_cache = 0;
580 b->width_run_cache = 0;
581 b->width_table = Qnil;
583 /* Put this on the chain of all buffers including killed ones. */
584 b->next = all_buffers;
585 all_buffers = b;
587 name = Fcopy_sequence (name);
588 STRING_SET_INTERVALS (name, NULL_INTERVAL);
589 b->name = name;
591 reset_buffer (b);
592 reset_buffer_local_variables (b, 1);
594 /* Put this in the alist of all live buffers. */
595 XSETBUFFER (buf, b);
596 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
598 b->mark = Fmake_marker ();
599 b->name = name;
601 /* The multibyte status belongs to the base buffer. */
602 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
604 /* Make sure the base buffer has markers for its narrowing. */
605 if (NILP (b->base_buffer->pt_marker))
607 b->base_buffer->pt_marker = Fmake_marker ();
608 set_marker_both (b->base_buffer->pt_marker, base_buffer,
609 BUF_PT (b->base_buffer),
610 BUF_PT_BYTE (b->base_buffer));
612 if (NILP (b->base_buffer->begv_marker))
614 b->base_buffer->begv_marker = Fmake_marker ();
615 set_marker_both (b->base_buffer->begv_marker, base_buffer,
616 BUF_BEGV (b->base_buffer),
617 BUF_BEGV_BYTE (b->base_buffer));
619 if (NILP (b->base_buffer->zv_marker))
621 b->base_buffer->zv_marker = Fmake_marker ();
622 set_marker_both (b->base_buffer->zv_marker, base_buffer,
623 BUF_ZV (b->base_buffer),
624 BUF_ZV_BYTE (b->base_buffer));
625 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
628 if (NILP (clone))
630 /* Give the indirect buffer markers for its narrowing. */
631 b->pt_marker = Fmake_marker ();
632 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
633 b->begv_marker = Fmake_marker ();
634 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
635 b->zv_marker = Fmake_marker ();
636 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
637 XMARKER (b->zv_marker)->insertion_type = 1;
639 else
641 struct buffer *old_b = current_buffer;
643 clone_per_buffer_values (b->base_buffer, b);
644 b->filename = Qnil;
645 b->file_truename = Qnil;
646 b->display_count = make_number (0);
647 b->backed_up = Qnil;
648 b->auto_save_file_name = Qnil;
649 set_buffer_internal_1 (b);
650 Fset (intern ("buffer-save-without-query"), Qnil);
651 Fset (intern ("buffer-file-number"), Qnil);
652 Fset (intern ("buffer-stale-function"), Qnil);
653 set_buffer_internal_1 (old_b);
656 return buf;
659 void
660 delete_all_overlays (b)
661 struct buffer *b;
663 Lisp_Object overlay;
665 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
666 have to empty the list, otherwise we end up with overlays that
667 think they belong to this buffer while the buffer doesn't know about
668 them any more. */
669 while (b->overlays_before)
671 XSETMISC (overlay, b->overlays_before);
672 Fdelete_overlay (overlay);
674 while (b->overlays_after)
676 XSETMISC (overlay, b->overlays_after);
677 Fdelete_overlay (overlay);
679 eassert (b->overlays_before == NULL);
680 eassert (b->overlays_after == NULL);
683 /* Reinitialize everything about a buffer except its name and contents
684 and local variables.
685 If called on an already-initialized buffer, the list of overlays
686 should be deleted before calling this function, otherwise we end up
687 with overlays that claim to belong to the buffer but the buffer
688 claims it doesn't belong to it. */
690 void
691 reset_buffer (b)
692 register struct buffer *b;
694 b->filename = Qnil;
695 b->file_truename = Qnil;
696 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
697 b->modtime = 0;
698 XSETFASTINT (b->save_length, 0);
699 b->last_window_start = 1;
700 /* It is more conservative to start out "changed" than "unchanged". */
701 b->clip_changed = 0;
702 b->prevent_redisplay_optimizations_p = 1;
703 b->backed_up = Qnil;
704 b->auto_save_modified = 0;
705 b->auto_save_failure_time = -1;
706 b->auto_save_file_name = Qnil;
707 b->read_only = Qnil;
708 b->overlays_before = NULL;
709 b->overlays_after = NULL;
710 b->overlay_center = BEG;
711 b->mark_active = Qnil;
712 b->point_before_scroll = Qnil;
713 b->file_format = Qnil;
714 b->auto_save_file_format = Qt;
715 b->last_selected_window = Qnil;
716 XSETINT (b->display_count, 0);
717 b->display_time = Qnil;
718 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
719 b->cursor_type = buffer_defaults.cursor_type;
720 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
722 b->display_error_modiff = 0;
725 /* Reset buffer B's local variables info.
726 Don't use this on a buffer that has already been in use;
727 it does not treat permanent locals consistently.
728 Instead, use Fkill_all_local_variables.
730 If PERMANENT_TOO is 1, then we reset permanent built-in
731 buffer-local variables. If PERMANENT_TOO is 0,
732 we preserve those. */
734 static void
735 reset_buffer_local_variables (b, permanent_too)
736 register struct buffer *b;
737 int permanent_too;
739 register int offset;
740 int i;
742 /* Reset the major mode to Fundamental, together with all the
743 things that depend on the major mode.
744 default-major-mode is handled at a higher level.
745 We ignore it here. */
746 b->major_mode = Qfundamental_mode;
747 b->keymap = Qnil;
748 b->mode_name = QSFundamental;
749 b->minor_modes = Qnil;
751 /* If the standard case table has been altered and invalidated,
752 fix up its insides first. */
753 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
754 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
755 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
756 Fset_standard_case_table (Vascii_downcase_table);
758 b->downcase_table = Vascii_downcase_table;
759 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
760 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
761 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
762 b->invisibility_spec = Qt;
763 #ifndef DOS_NT
764 b->buffer_file_type = Qnil;
765 #endif
767 /* Reset all (or most) per-buffer variables to their defaults. */
768 b->local_var_alist = Qnil;
769 for (i = 0; i < last_per_buffer_idx; ++i)
770 if (permanent_too || buffer_permanent_local_flags[i] == 0)
771 SET_PER_BUFFER_VALUE_P (b, i, 0);
773 /* For each slot that has a default value,
774 copy that into the slot. */
776 for (offset = PER_BUFFER_VAR_OFFSET (name);
777 offset < sizeof *b;
778 offset += sizeof (Lisp_Object))
780 int idx = PER_BUFFER_IDX (offset);
781 if ((idx > 0
782 && (permanent_too
783 || buffer_permanent_local_flags[idx] == 0))
784 /* Is -2 used anywhere? */
785 || idx == -2)
786 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
790 /* We split this away from generate-new-buffer, because rename-buffer
791 and set-visited-file-name ought to be able to use this to really
792 rename the buffer properly. */
794 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
795 1, 2, 0,
796 doc: /* Return a string that is the name of no existing buffer based on NAME.
797 If there is no live buffer named NAME, then return NAME.
798 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
799 \(starting at 2) until an unused name is found, and then return that name.
800 Optional second argument IGNORE specifies a name that is okay to use (if
801 it is in the sequence to be tried) even if a buffer with that name exists. */)
802 (name, ignore)
803 register Lisp_Object name, ignore;
805 register Lisp_Object gentemp, tem;
806 int count;
807 char number[10];
809 CHECK_STRING (name);
811 tem = Fstring_equal (name, ignore);
812 if (!NILP (tem))
813 return name;
814 tem = Fget_buffer (name);
815 if (NILP (tem))
816 return name;
818 count = 1;
819 while (1)
821 sprintf (number, "<%d>", ++count);
822 gentemp = concat2 (name, build_string (number));
823 tem = Fstring_equal (gentemp, ignore);
824 if (!NILP (tem))
825 return gentemp;
826 tem = Fget_buffer (gentemp);
827 if (NILP (tem))
828 return gentemp;
833 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
834 doc: /* Return the name of BUFFER, as a string.
835 With no argument or nil as argument, return the name of the current buffer. */)
836 (buffer)
837 register Lisp_Object buffer;
839 if (NILP (buffer))
840 return current_buffer->name;
841 CHECK_BUFFER (buffer);
842 return XBUFFER (buffer)->name;
845 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
846 doc: /* Return name of file BUFFER is visiting, or nil if none.
847 No argument or nil as argument means use the current buffer. */)
848 (buffer)
849 register Lisp_Object buffer;
851 if (NILP (buffer))
852 return current_buffer->filename;
853 CHECK_BUFFER (buffer);
854 return XBUFFER (buffer)->filename;
857 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
858 0, 1, 0,
859 doc: /* Return the base buffer of indirect buffer BUFFER.
860 If BUFFER is not indirect, return nil.
861 BUFFER defaults to the current buffer. */)
862 (buffer)
863 register Lisp_Object buffer;
865 struct buffer *base;
866 Lisp_Object base_buffer;
868 if (NILP (buffer))
869 base = current_buffer->base_buffer;
870 else
872 CHECK_BUFFER (buffer);
873 base = XBUFFER (buffer)->base_buffer;
876 if (! base)
877 return Qnil;
878 XSETBUFFER (base_buffer, base);
879 return base_buffer;
882 DEFUN ("buffer-local-value", Fbuffer_local_value,
883 Sbuffer_local_value, 2, 2, 0,
884 doc: /* Return the value of VARIABLE in BUFFER.
885 If VARIABLE does not have a buffer-local binding in BUFFER, the value
886 is the default binding of the variable. */)
887 (variable, buffer)
888 register Lisp_Object variable;
889 register Lisp_Object buffer;
891 register struct buffer *buf;
892 register Lisp_Object result;
894 CHECK_SYMBOL (variable);
895 CHECK_BUFFER (buffer);
896 buf = XBUFFER (buffer);
898 if (SYMBOLP (variable))
899 variable = indirect_variable (variable);
901 /* Look in local_var_list */
902 result = Fassoc (variable, buf->local_var_alist);
903 if (NILP (result))
905 int offset, idx;
906 int found = 0;
908 /* Look in special slots */
909 for (offset = PER_BUFFER_VAR_OFFSET (name);
910 offset < sizeof (struct buffer);
911 /* sizeof EMACS_INT == sizeof Lisp_Object */
912 offset += (sizeof (EMACS_INT)))
914 idx = PER_BUFFER_IDX (offset);
915 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
916 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
917 && EQ (PER_BUFFER_SYMBOL (offset), variable))
919 result = PER_BUFFER_VALUE (buf, offset);
920 found = 1;
921 break;
925 if (!found)
926 result = Fdefault_value (variable);
928 else
930 Lisp_Object valcontents;
931 Lisp_Object current_alist_element;
933 /* What binding is loaded right now? */
934 valcontents = SYMBOL_VALUE (variable);
935 current_alist_element
936 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
938 /* The value of the currently loaded binding is not
939 stored in it, but rather in the realvalue slot.
940 Store that value into the binding it belongs to
941 in case that is the one we are about to use. */
943 Fsetcdr (current_alist_element,
944 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
946 /* Now get the (perhaps updated) value out of the binding. */
947 result = XCDR (result);
950 if (!EQ (result, Qunbound))
951 return result;
953 xsignal1 (Qvoid_variable, variable);
956 /* Return an alist of the Lisp-level buffer-local bindings of
957 buffer BUF. That is, don't include the variables maintained
958 in special slots in the buffer object. */
960 static Lisp_Object
961 buffer_lisp_local_variables (buf)
962 struct buffer *buf;
964 Lisp_Object result = Qnil;
965 register Lisp_Object tail;
966 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
968 Lisp_Object val, elt;
970 elt = XCAR (tail);
972 /* Reference each variable in the alist in buf.
973 If inquiring about the current buffer, this gets the current values,
974 so store them into the alist so the alist is up to date.
975 If inquiring about some other buffer, this swaps out any values
976 for that buffer, making the alist up to date automatically. */
977 val = find_symbol_value (XCAR (elt));
978 /* Use the current buffer value only if buf is the current buffer. */
979 if (buf != current_buffer)
980 val = XCDR (elt);
982 /* If symbol is unbound, put just the symbol in the list. */
983 if (EQ (val, Qunbound))
984 result = Fcons (XCAR (elt), result);
985 /* Otherwise, put (symbol . value) in the list. */
986 else
987 result = Fcons (Fcons (XCAR (elt), val), result);
990 return result;
993 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
994 Sbuffer_local_variables, 0, 1, 0,
995 doc: /* Return an alist of variables that are buffer-local in BUFFER.
996 Most elements look like (SYMBOL . VALUE), describing one variable.
997 For a symbol that is locally unbound, just the symbol appears in the value.
998 Note that storing new VALUEs in these elements doesn't change the variables.
999 No argument or nil as argument means use current buffer as BUFFER. */)
1000 (buffer)
1001 register Lisp_Object buffer;
1003 register struct buffer *buf;
1004 register Lisp_Object result;
1006 if (NILP (buffer))
1007 buf = current_buffer;
1008 else
1010 CHECK_BUFFER (buffer);
1011 buf = XBUFFER (buffer);
1014 result = buffer_lisp_local_variables (buf);
1016 /* Add on all the variables stored in special slots. */
1018 int offset, idx;
1020 for (offset = PER_BUFFER_VAR_OFFSET (name);
1021 offset < sizeof (struct buffer);
1022 /* sizeof EMACS_INT == sizeof Lisp_Object */
1023 offset += (sizeof (EMACS_INT)))
1025 idx = PER_BUFFER_IDX (offset);
1026 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1027 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1028 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1029 PER_BUFFER_VALUE (buf, offset)),
1030 result);
1034 return result;
1037 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1038 0, 1, 0,
1039 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1040 No argument or nil as argument means use current buffer as BUFFER. */)
1041 (buffer)
1042 register Lisp_Object buffer;
1044 register struct buffer *buf;
1045 if (NILP (buffer))
1046 buf = current_buffer;
1047 else
1049 CHECK_BUFFER (buffer);
1050 buf = XBUFFER (buffer);
1053 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1056 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1057 1, 1, 0,
1058 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1059 A non-nil FLAG means mark the buffer modified. */)
1060 (flag)
1061 register Lisp_Object flag;
1063 register int already;
1064 register Lisp_Object fn;
1065 Lisp_Object buffer, window;
1067 #ifdef CLASH_DETECTION
1068 /* If buffer becoming modified, lock the file.
1069 If buffer becoming unmodified, unlock the file. */
1071 fn = current_buffer->file_truename;
1072 /* Test buffer-file-name so that binding it to nil is effective. */
1073 if (!NILP (fn) && ! NILP (current_buffer->filename))
1075 already = SAVE_MODIFF < MODIFF;
1076 if (!already && !NILP (flag))
1077 lock_file (fn);
1078 else if (already && NILP (flag))
1079 unlock_file (fn);
1081 #endif /* CLASH_DETECTION */
1083 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1085 /* Set update_mode_lines only if buffer is displayed in some window.
1086 Packages like jit-lock or lazy-lock preserve a buffer's modified
1087 state by recording/restoring the state around blocks of code.
1088 Setting update_mode_lines makes redisplay consider all windows
1089 (on all frames). Stealth fontification of buffers not displayed
1090 would incur additional redisplay costs if we'd set
1091 update_modes_lines unconditionally.
1093 Ideally, I think there should be another mechanism for fontifying
1094 buffers without "modifying" buffers, or redisplay should be
1095 smarter about updating the `*' in mode lines. --gerd */
1096 XSETBUFFER (buffer, current_buffer);
1097 window = Fget_buffer_window (buffer, Qt);
1098 if (WINDOWP (window))
1100 ++update_mode_lines;
1101 current_buffer->prevent_redisplay_optimizations_p = 1;
1104 return flag;
1107 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1108 Srestore_buffer_modified_p, 1, 1, 0,
1109 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1110 It is not ensured that mode lines will be updated to show the modified
1111 state of the current buffer. Use with care. */)
1112 (flag)
1113 Lisp_Object flag;
1115 #ifdef CLASH_DETECTION
1116 Lisp_Object fn;
1118 /* If buffer becoming modified, lock the file.
1119 If buffer becoming unmodified, unlock the file. */
1121 fn = current_buffer->file_truename;
1122 /* Test buffer-file-name so that binding it to nil is effective. */
1123 if (!NILP (fn) && ! NILP (current_buffer->filename))
1125 int already = SAVE_MODIFF < MODIFF;
1126 if (!already && !NILP (flag))
1127 lock_file (fn);
1128 else if (already && NILP (flag))
1129 unlock_file (fn);
1131 #endif /* CLASH_DETECTION */
1133 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1134 return flag;
1137 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1138 0, 1, 0,
1139 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1140 Each buffer has a tick counter which is incremented each time the
1141 text in that buffer is changed. It wraps around occasionally.
1142 No argument or nil as argument means use current buffer as BUFFER. */)
1143 (buffer)
1144 register Lisp_Object buffer;
1146 register struct buffer *buf;
1147 if (NILP (buffer))
1148 buf = current_buffer;
1149 else
1151 CHECK_BUFFER (buffer);
1152 buf = XBUFFER (buffer);
1155 return make_number (BUF_MODIFF (buf));
1158 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1159 Sbuffer_chars_modified_tick, 0, 1, 0,
1160 doc: /* Return BUFFER's character-change tick counter.
1161 Each buffer has a character-change tick counter, which is set to the
1162 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1163 time text in that buffer is inserted or deleted. By comparing the
1164 values returned by two individual calls of `buffer-chars-modified-tick',
1165 you can tell whether a character change occurred in that buffer in
1166 between these calls. No argument or nil as argument means use current
1167 buffer as BUFFER. */)
1168 (buffer)
1169 register Lisp_Object buffer;
1171 register struct buffer *buf;
1172 if (NILP (buffer))
1173 buf = current_buffer;
1174 else
1176 CHECK_BUFFER (buffer);
1177 buf = XBUFFER (buffer);
1180 return make_number (BUF_CHARS_MODIFF (buf));
1183 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1184 "sRename buffer (to new name): \nP",
1185 doc: /* Change current buffer's name to NEWNAME (a string).
1186 If second arg UNIQUE is nil or omitted, it is an error if a
1187 buffer named NEWNAME already exists.
1188 If UNIQUE is non-nil, come up with a new name using
1189 `generate-new-buffer-name'.
1190 Interactively, you can set UNIQUE with a prefix argument.
1191 We return the name we actually gave the buffer.
1192 This does not change the name of the visited file (if any). */)
1193 (newname, unique)
1194 register Lisp_Object newname, unique;
1196 register Lisp_Object tem, buf;
1198 CHECK_STRING (newname);
1200 if (SCHARS (newname) == 0)
1201 error ("Empty string is invalid as a buffer name");
1203 tem = Fget_buffer (newname);
1204 if (!NILP (tem))
1206 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1207 rename the buffer automatically so you can create another
1208 with the original name. It makes UNIQUE equivalent to
1209 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1210 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1211 return current_buffer->name;
1212 if (!NILP (unique))
1213 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1214 else
1215 error ("Buffer name `%s' is in use", SDATA (newname));
1218 current_buffer->name = newname;
1220 /* Catch redisplay's attention. Unless we do this, the mode lines for
1221 any windows displaying current_buffer will stay unchanged. */
1222 update_mode_lines++;
1224 XSETBUFFER (buf, current_buffer);
1225 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1226 if (NILP (current_buffer->filename)
1227 && !NILP (current_buffer->auto_save_file_name))
1228 call0 (intern ("rename-auto-save-file"));
1229 /* Refetch since that last call may have done GC. */
1230 return current_buffer->name;
1233 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1234 doc: /* Return most recently selected buffer other than BUFFER.
1235 Buffers not visible in windows are preferred to visible buffers,
1236 unless optional second argument VISIBLE-OK is non-nil.
1237 If the optional third argument FRAME is non-nil, use that frame's
1238 buffer list instead of the selected frame's buffer list.
1239 If no other buffer exists, the buffer `*scratch*' is returned.
1240 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1241 (buffer, visible_ok, frame)
1242 register Lisp_Object buffer, visible_ok, frame;
1244 Lisp_Object Fset_buffer_major_mode ();
1245 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1246 notsogood = Qnil;
1248 if (NILP (frame))
1249 frame = selected_frame;
1251 tail = Vbuffer_alist;
1252 pred = frame_buffer_predicate (frame);
1254 /* Consider buffers that have been seen in the selected frame
1255 before other buffers. */
1257 tem = frame_buffer_list (frame);
1258 add_ons = Qnil;
1259 while (CONSP (tem))
1261 if (BUFFERP (XCAR (tem)))
1262 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1263 tem = XCDR (tem);
1265 tail = nconc2 (Fnreverse (add_ons), tail);
1267 for (; CONSP (tail); tail = XCDR (tail))
1269 buf = Fcdr (XCAR (tail));
1270 if (EQ (buf, buffer))
1271 continue;
1272 if (NILP (buf))
1273 continue;
1274 if (NILP (XBUFFER (buf)->name))
1275 continue;
1276 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1277 continue;
1278 /* If the selected frame has a buffer_predicate,
1279 disregard buffers that don't fit the predicate. */
1280 if (!NILP (pred))
1282 tem = call1 (pred, buf);
1283 if (NILP (tem))
1284 continue;
1287 if (NILP (visible_ok))
1288 tem = Fget_buffer_window (buf, Qvisible);
1289 else
1290 tem = Qnil;
1291 if (NILP (tem))
1292 return buf;
1293 if (NILP (notsogood))
1294 notsogood = buf;
1296 if (!NILP (notsogood))
1297 return notsogood;
1298 buf = Fget_buffer (build_string ("*scratch*"));
1299 if (NILP (buf))
1301 buf = Fget_buffer_create (build_string ("*scratch*"));
1302 Fset_buffer_major_mode (buf);
1304 return buf;
1307 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1308 0, 1, "",
1309 doc: /* Start keeping undo information for buffer BUFFER.
1310 No argument or nil as argument means do this for the current buffer. */)
1311 (buffer)
1312 register Lisp_Object buffer;
1314 Lisp_Object real_buffer;
1316 if (NILP (buffer))
1317 XSETBUFFER (real_buffer, current_buffer);
1318 else
1320 real_buffer = Fget_buffer (buffer);
1321 if (NILP (real_buffer))
1322 nsberror (buffer);
1325 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1326 XBUFFER (real_buffer)->undo_list = Qnil;
1328 return Qnil;
1332 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1333 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1334 The buffer being killed will be current while the hook is running.\n\
1335 See `kill-buffer'."
1337 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1338 doc: /* Kill the buffer BUFFER.
1339 The argument may be a buffer or the name of a buffer.
1340 With a nil argument, kill the current buffer.
1342 Value is t if the buffer is actually killed, nil otherwise.
1344 The functions in `kill-buffer-query-functions' are called with BUFFER as
1345 the current buffer. If any of them returns nil, the buffer is not killed.
1347 The hook `kill-buffer-hook' is run before the buffer is actually killed.
1348 The buffer being killed will be current while the hook is running.
1350 Any processes that have this buffer as the `process-buffer' are killed
1351 with SIGHUP. */)
1352 (buffer)
1353 Lisp_Object buffer;
1355 Lisp_Object buf;
1356 register struct buffer *b;
1357 register Lisp_Object tem;
1358 register struct Lisp_Marker *m;
1359 struct gcpro gcpro1;
1361 if (NILP (buffer))
1362 buf = Fcurrent_buffer ();
1363 else
1364 buf = Fget_buffer (buffer);
1365 if (NILP (buf))
1366 nsberror (buffer);
1368 b = XBUFFER (buf);
1370 /* Avoid trouble for buffer already dead. */
1371 if (NILP (b->name))
1372 return Qnil;
1374 /* Query if the buffer is still modified. */
1375 if (INTERACTIVE && !NILP (b->filename)
1376 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1378 GCPRO1 (buf);
1379 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1380 b->name, make_number (0)));
1381 UNGCPRO;
1382 if (NILP (tem))
1383 return Qnil;
1386 /* Run hooks with the buffer to be killed the current buffer. */
1388 int count = SPECPDL_INDEX ();
1389 Lisp_Object arglist[1];
1391 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1392 set_buffer_internal (b);
1394 /* First run the query functions; if any query is answered no,
1395 don't kill the buffer. */
1396 arglist[0] = Qkill_buffer_query_functions;
1397 tem = Frun_hook_with_args_until_failure (1, arglist);
1398 if (NILP (tem))
1399 return unbind_to (count, Qnil);
1401 /* Then run the hooks. */
1402 Frun_hooks (1, &Qkill_buffer_hook);
1403 unbind_to (count, Qnil);
1406 /* We have no more questions to ask. Verify that it is valid
1407 to kill the buffer. This must be done after the questions
1408 since anything can happen within do_yes_or_no_p. */
1410 /* Don't kill the minibuffer now current. */
1411 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1412 return Qnil;
1414 if (NILP (b->name))
1415 return Qnil;
1417 /* When we kill a base buffer, kill all its indirect buffers.
1418 We do it at this stage so nothing terrible happens if they
1419 ask questions or their hooks get errors. */
1420 if (! b->base_buffer)
1422 struct buffer *other;
1424 GCPRO1 (buf);
1426 for (other = all_buffers; other; other = other->next)
1427 /* all_buffers contains dead buffers too;
1428 don't re-kill them. */
1429 if (other->base_buffer == b && !NILP (other->name))
1431 Lisp_Object buf;
1432 XSETBUFFER (buf, other);
1433 Fkill_buffer (buf);
1436 UNGCPRO;
1439 /* Make this buffer not be current.
1440 In the process, notice if this is the sole visible buffer
1441 and give up if so. */
1442 if (b == current_buffer)
1444 tem = Fother_buffer (buf, Qnil, Qnil);
1445 Fset_buffer (tem);
1446 if (b == current_buffer)
1447 return Qnil;
1450 /* Notice if the buffer to kill is the sole visible buffer
1451 when we're currently in the mini-buffer, and give up if so. */
1452 XSETBUFFER (tem, current_buffer);
1453 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1455 tem = Fother_buffer (buf, Qnil, Qnil);
1456 if (EQ (buf, tem))
1457 return Qnil;
1460 /* Now there is no question: we can kill the buffer. */
1462 #ifdef CLASH_DETECTION
1463 /* Unlock this buffer's file, if it is locked. */
1464 unlock_buffer (b);
1465 #endif /* CLASH_DETECTION */
1467 GCPRO1 (buf);
1468 kill_buffer_processes (buf);
1469 UNGCPRO;
1471 /* Killing buffer processes may run sentinels which may
1472 have called kill-buffer. */
1474 if (NILP (b->name))
1475 return Qnil;
1477 clear_charpos_cache (b);
1479 tem = Vinhibit_quit;
1480 Vinhibit_quit = Qt;
1481 replace_buffer_in_all_windows (buf);
1482 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1483 frames_discard_buffer (buf);
1484 Vinhibit_quit = tem;
1486 /* Delete any auto-save file, if we saved it in this session.
1487 But not if the buffer is modified. */
1488 if (STRINGP (b->auto_save_file_name)
1489 && b->auto_save_modified != 0
1490 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1491 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1492 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1494 Lisp_Object tem;
1495 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1496 if (! NILP (tem))
1497 internal_delete_file (b->auto_save_file_name);
1500 if (b->base_buffer)
1502 /* Unchain all markers that belong to this indirect buffer.
1503 Don't unchain the markers that belong to the base buffer
1504 or its other indirect buffers. */
1505 for (m = BUF_MARKERS (b); m; )
1507 struct Lisp_Marker *next = m->next;
1508 if (m->buffer == b)
1509 unchain_marker (m);
1510 m = next;
1513 else
1515 /* Unchain all markers of this buffer and its indirect buffers.
1516 and leave them pointing nowhere. */
1517 for (m = BUF_MARKERS (b); m; )
1519 struct Lisp_Marker *next = m->next;
1520 m->buffer = 0;
1521 m->next = NULL;
1522 m = next;
1524 BUF_MARKERS (b) = NULL;
1525 BUF_INTERVALS (b) = NULL_INTERVAL;
1527 /* Perhaps we should explicitly free the interval tree here... */
1530 /* Reset the local variables, so that this buffer's local values
1531 won't be protected from GC. They would be protected
1532 if they happened to remain encached in their symbols.
1533 This gets rid of them for certain. */
1534 swap_out_buffer_local_variables (b);
1535 reset_buffer_local_variables (b, 1);
1537 b->name = Qnil;
1539 BLOCK_INPUT;
1540 if (! b->base_buffer)
1541 free_buffer_text (b);
1543 if (b->newline_cache)
1545 free_region_cache (b->newline_cache);
1546 b->newline_cache = 0;
1548 if (b->width_run_cache)
1550 free_region_cache (b->width_run_cache);
1551 b->width_run_cache = 0;
1553 b->width_table = Qnil;
1554 UNBLOCK_INPUT;
1555 b->undo_list = Qnil;
1557 return Qt;
1560 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1561 we do this each time BUF is selected visibly, the more recently
1562 selected buffers are always closer to the front of the list. This
1563 means that other_buffer is more likely to choose a relevant buffer. */
1565 void
1566 record_buffer (buf)
1567 Lisp_Object buf;
1569 register Lisp_Object link, prev;
1570 Lisp_Object frame;
1571 frame = selected_frame;
1573 prev = Qnil;
1574 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1576 if (EQ (XCDR (XCAR (link)), buf))
1577 break;
1578 prev = link;
1581 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1582 we cannot use Fdelq itself here because it allows quitting. */
1584 if (NILP (prev))
1585 Vbuffer_alist = XCDR (Vbuffer_alist);
1586 else
1587 XSETCDR (prev, XCDR (XCDR (prev)));
1589 XSETCDR (link, Vbuffer_alist);
1590 Vbuffer_alist = link;
1592 /* Effectively do a delq on buried_buffer_list. */
1594 prev = Qnil;
1595 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1596 link = XCDR (link))
1598 if (EQ (XCAR (link), buf))
1600 if (NILP (prev))
1601 XFRAME (frame)->buried_buffer_list = XCDR (link);
1602 else
1603 XSETCDR (prev, XCDR (XCDR (prev)));
1604 break;
1606 prev = link;
1609 /* Now move this buffer to the front of frame_buffer_list also. */
1611 prev = Qnil;
1612 for (link = frame_buffer_list (frame); CONSP (link);
1613 link = XCDR (link))
1615 if (EQ (XCAR (link), buf))
1616 break;
1617 prev = link;
1620 /* Effectively do delq. */
1622 if (CONSP (link))
1624 if (NILP (prev))
1625 set_frame_buffer_list (frame,
1626 XCDR (frame_buffer_list (frame)));
1627 else
1628 XSETCDR (prev, XCDR (XCDR (prev)));
1630 XSETCDR (link, frame_buffer_list (frame));
1631 set_frame_buffer_list (frame, link);
1633 else
1634 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1637 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1638 doc: /* Set an appropriate major mode for BUFFER.
1639 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1640 according to `default-major-mode'.
1641 Use this function before selecting the buffer, since it may need to inspect
1642 the current buffer's major mode. */)
1643 (buffer)
1644 Lisp_Object buffer;
1646 int count;
1647 Lisp_Object function;
1649 CHECK_BUFFER (buffer);
1651 if (STRINGP (XBUFFER (buffer)->name)
1652 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1653 function = find_symbol_value (intern ("initial-major-mode"));
1654 else
1656 function = buffer_defaults.major_mode;
1657 if (NILP (function)
1658 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1659 function = current_buffer->major_mode;
1662 if (NILP (function) || EQ (function, Qfundamental_mode))
1663 return Qnil;
1665 count = SPECPDL_INDEX ();
1667 /* To select a nonfundamental mode,
1668 select the buffer temporarily and then call the mode function. */
1670 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1672 Fset_buffer (buffer);
1673 call0 (function);
1675 return unbind_to (count, Qnil);
1678 /* If switching buffers in WINDOW would be an error, return
1679 a C string saying what the error would be. */
1681 char *
1682 no_switch_window (window)
1683 Lisp_Object window;
1685 Lisp_Object tem;
1686 if (EQ (minibuf_window, window))
1687 return "Cannot switch buffers in minibuffer window";
1688 tem = Fwindow_dedicated_p (window);
1689 if (EQ (tem, Qt))
1690 return "Cannot switch buffers in a dedicated window";
1691 return NULL;
1694 /* Switch to buffer BUFFER in the selected window.
1695 If NORECORD is non-nil, don't call record_buffer. */
1697 Lisp_Object
1698 switch_to_buffer_1 (buffer, norecord)
1699 Lisp_Object buffer, norecord;
1701 register Lisp_Object buf;
1703 if (NILP (buffer))
1704 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1705 else
1707 buf = Fget_buffer (buffer);
1708 if (NILP (buf))
1710 buf = Fget_buffer_create (buffer);
1711 Fset_buffer_major_mode (buf);
1714 Fset_buffer (buf);
1715 if (NILP (norecord))
1716 record_buffer (buf);
1718 Fset_window_buffer (EQ (selected_window, minibuf_window)
1719 ? Fnext_window (minibuf_window, Qnil, Qnil)
1720 : selected_window,
1721 buf, Qnil);
1723 return buf;
1726 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1727 doc: /* Select buffer BUFFER in the current window.
1728 If BUFFER does not identify an existing buffer,
1729 then this function creates a buffer with that name.
1731 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1732 or nil. If BUFFER is nil, then this function chooses a buffer
1733 using `other-buffer'.
1734 Optional second arg NORECORD non-nil means
1735 do not put this buffer at the front of the list of recently selected ones.
1736 This function returns the buffer it switched to.
1738 WARNING: This is NOT the way to work on another buffer temporarily
1739 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1740 the window-buffer correspondences. */)
1741 (buffer, norecord)
1742 Lisp_Object buffer, norecord;
1744 char *err;
1746 if (EQ (buffer, Fwindow_buffer (selected_window)))
1748 /* Basically a NOP. Avoid signalling an error in the case where
1749 the selected window is dedicated, or a minibuffer. */
1751 /* But do put this buffer at the front of the buffer list,
1752 unless that has been inhibited. Note that even if
1753 BUFFER is at the front of the main buffer-list already,
1754 we still want to move it to the front of the frame's buffer list. */
1755 if (NILP (norecord))
1756 record_buffer (buffer);
1757 return Fset_buffer (buffer);
1760 err = no_switch_window (selected_window);
1761 if (err) error (err);
1763 return switch_to_buffer_1 (buffer, norecord);
1766 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1767 doc: /* Select buffer BUFFER in some window, preferably a different one.
1768 BUFFER may be a buffer, a string \(a buffer name), or nil.
1769 If BUFFER is a string which is not the name of an existing buffer,
1770 then this function creates a buffer with that name.
1771 If BUFFER is nil, then it chooses some other buffer.
1772 If `pop-up-windows' is non-nil, windows can be split to do this.
1773 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1774 window even if BUFFER is already visible in the selected window,
1775 and ignore `same-window-regexps' and `same-window-buffer-names'.
1776 This function returns the buffer it switched to.
1777 This uses the function `display-buffer' as a subroutine; see the documentation
1778 of `display-buffer' for additional customization information.
1780 Optional third arg NORECORD non-nil means
1781 do not put this buffer at the front of the list of recently selected ones. */)
1782 (buffer, other_window, norecord)
1783 Lisp_Object buffer, other_window, norecord;
1785 register Lisp_Object buf;
1786 if (NILP (buffer))
1787 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1788 else
1790 buf = Fget_buffer (buffer);
1791 if (NILP (buf))
1793 buf = Fget_buffer_create (buffer);
1794 Fset_buffer_major_mode (buf);
1797 Fset_buffer (buf);
1798 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1799 return buf;
1802 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1803 doc: /* Return the current buffer as a Lisp object. */)
1806 register Lisp_Object buf;
1807 XSETBUFFER (buf, current_buffer);
1808 return buf;
1811 /* Set the current buffer to B.
1813 We previously set windows_or_buffers_changed here to invalidate
1814 global unchanged information in beg_unchanged and end_unchanged.
1815 This is no longer necessary because we now compute unchanged
1816 information on a buffer-basis. Every action affecting other
1817 windows than the selected one requires a select_window at some
1818 time, and that increments windows_or_buffers_changed. */
1820 void
1821 set_buffer_internal (b)
1822 register struct buffer *b;
1824 if (current_buffer != b)
1825 set_buffer_internal_1 (b);
1828 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1829 This is used by redisplay. */
1831 void
1832 set_buffer_internal_1 (b)
1833 register struct buffer *b;
1835 register struct buffer *old_buf;
1836 register Lisp_Object tail, valcontents;
1837 Lisp_Object tem;
1839 #ifdef USE_MMAP_FOR_BUFFERS
1840 if (b->text->beg == NULL)
1841 enlarge_buffer_text (b, 0);
1842 #endif /* USE_MMAP_FOR_BUFFERS */
1844 if (current_buffer == b)
1845 return;
1847 old_buf = current_buffer;
1848 current_buffer = b;
1849 last_known_column_point = -1; /* invalidate indentation cache */
1851 if (old_buf)
1853 /* Put the undo list back in the base buffer, so that it appears
1854 that an indirect buffer shares the undo list of its base. */
1855 if (old_buf->base_buffer)
1856 old_buf->base_buffer->undo_list = old_buf->undo_list;
1858 /* If the old current buffer has markers to record PT, BEGV and ZV
1859 when it is not current, update them now. */
1860 if (! NILP (old_buf->pt_marker))
1862 Lisp_Object obuf;
1863 XSETBUFFER (obuf, old_buf);
1864 set_marker_both (old_buf->pt_marker, obuf,
1865 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1867 if (! NILP (old_buf->begv_marker))
1869 Lisp_Object obuf;
1870 XSETBUFFER (obuf, old_buf);
1871 set_marker_both (old_buf->begv_marker, obuf,
1872 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1874 if (! NILP (old_buf->zv_marker))
1876 Lisp_Object obuf;
1877 XSETBUFFER (obuf, old_buf);
1878 set_marker_both (old_buf->zv_marker, obuf,
1879 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1883 /* Get the undo list from the base buffer, so that it appears
1884 that an indirect buffer shares the undo list of its base. */
1885 if (b->base_buffer)
1886 b->undo_list = b->base_buffer->undo_list;
1888 /* If the new current buffer has markers to record PT, BEGV and ZV
1889 when it is not current, fetch them now. */
1890 if (! NILP (b->pt_marker))
1892 BUF_PT (b) = marker_position (b->pt_marker);
1893 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1895 if (! NILP (b->begv_marker))
1897 BUF_BEGV (b) = marker_position (b->begv_marker);
1898 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1900 if (! NILP (b->zv_marker))
1902 BUF_ZV (b) = marker_position (b->zv_marker);
1903 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1906 /* Look down buffer's list of local Lisp variables
1907 to find and update any that forward into C variables. */
1909 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1911 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1912 if ((BUFFER_LOCAL_VALUEP (valcontents))
1913 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1914 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1915 /* Just reference the variable
1916 to cause it to become set for this buffer. */
1917 Fsymbol_value (XCAR (XCAR (tail)));
1920 /* Do the same with any others that were local to the previous buffer */
1922 if (old_buf)
1923 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1925 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1926 if ((BUFFER_LOCAL_VALUEP (valcontents))
1927 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1928 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1929 /* Just reference the variable
1930 to cause it to become set for this buffer. */
1931 Fsymbol_value (XCAR (XCAR (tail)));
1935 /* Switch to buffer B temporarily for redisplay purposes.
1936 This avoids certain things that don't need to be done within redisplay. */
1938 void
1939 set_buffer_temp (b)
1940 struct buffer *b;
1942 register struct buffer *old_buf;
1944 if (current_buffer == b)
1945 return;
1947 old_buf = current_buffer;
1948 current_buffer = b;
1950 if (old_buf)
1952 /* If the old current buffer has markers to record PT, BEGV and ZV
1953 when it is not current, update them now. */
1954 if (! NILP (old_buf->pt_marker))
1956 Lisp_Object obuf;
1957 XSETBUFFER (obuf, old_buf);
1958 set_marker_both (old_buf->pt_marker, obuf,
1959 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1961 if (! NILP (old_buf->begv_marker))
1963 Lisp_Object obuf;
1964 XSETBUFFER (obuf, old_buf);
1965 set_marker_both (old_buf->begv_marker, obuf,
1966 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1968 if (! NILP (old_buf->zv_marker))
1970 Lisp_Object obuf;
1971 XSETBUFFER (obuf, old_buf);
1972 set_marker_both (old_buf->zv_marker, obuf,
1973 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1977 /* If the new current buffer has markers to record PT, BEGV and ZV
1978 when it is not current, fetch them now. */
1979 if (! NILP (b->pt_marker))
1981 BUF_PT (b) = marker_position (b->pt_marker);
1982 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1984 if (! NILP (b->begv_marker))
1986 BUF_BEGV (b) = marker_position (b->begv_marker);
1987 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1989 if (! NILP (b->zv_marker))
1991 BUF_ZV (b) = marker_position (b->zv_marker);
1992 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1996 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1997 doc: /* Make the buffer BUFFER current for editing operations.
1998 BUFFER may be a buffer or the name of an existing buffer.
1999 See also `save-excursion' when you want to make a buffer current temporarily.
2000 This function does not display the buffer, so its effect ends
2001 when the current command terminates.
2002 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
2003 (buffer)
2004 register Lisp_Object buffer;
2006 register Lisp_Object buf;
2007 buf = Fget_buffer (buffer);
2008 if (NILP (buf))
2009 nsberror (buffer);
2010 if (NILP (XBUFFER (buf)->name))
2011 error ("Selecting deleted buffer");
2012 set_buffer_internal (XBUFFER (buf));
2013 return buf;
2016 /* Set the current buffer to BUFFER provided it is alive. */
2018 Lisp_Object
2019 set_buffer_if_live (buffer)
2020 Lisp_Object buffer;
2022 if (! NILP (XBUFFER (buffer)->name))
2023 Fset_buffer (buffer);
2024 return Qnil;
2027 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2028 Sbarf_if_buffer_read_only, 0, 0, 0,
2029 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2032 if (!NILP (current_buffer->read_only)
2033 && NILP (Vinhibit_read_only))
2034 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2035 return Qnil;
2038 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2039 doc: /* Put BUFFER at the end of the list of all buffers.
2040 There it is the least likely candidate for `other-buffer' to return;
2041 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
2042 You can specify a buffer name as BUFFER, or an actual buffer object.
2043 If BUFFER is nil or omitted, bury the current buffer.
2044 Also, if BUFFER is nil or omitted, remove the current buffer from the
2045 selected window if it is displayed there. */)
2046 (buffer)
2047 register Lisp_Object buffer;
2049 /* Figure out what buffer we're going to bury. */
2050 if (NILP (buffer))
2052 Lisp_Object tem;
2053 XSETBUFFER (buffer, current_buffer);
2055 tem = Fwindow_buffer (selected_window);
2056 /* If we're burying the current buffer, unshow it. */
2057 if (EQ (buffer, tem))
2059 if (NILP (Fwindow_dedicated_p (selected_window)))
2060 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2061 else if (NILP (XWINDOW (selected_window)->parent))
2062 Ficonify_frame (Fwindow_frame (selected_window));
2063 else
2064 Fdelete_window (selected_window);
2067 else
2069 Lisp_Object buf1;
2071 buf1 = Fget_buffer (buffer);
2072 if (NILP (buf1))
2073 nsberror (buffer);
2074 buffer = buf1;
2077 /* Move buffer to the end of the buffer list. Do nothing if the
2078 buffer is killed. */
2079 if (!NILP (XBUFFER (buffer)->name))
2081 Lisp_Object aelt, link;
2083 aelt = Frassq (buffer, Vbuffer_alist);
2084 link = Fmemq (aelt, Vbuffer_alist);
2085 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2086 XSETCDR (link, Qnil);
2087 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2089 XFRAME (selected_frame)->buffer_list
2090 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2091 XFRAME (selected_frame)->buried_buffer_list
2092 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2095 return Qnil;
2098 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2099 doc: /* Delete the entire contents of the current buffer.
2100 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2101 so the buffer is truly empty after this. */)
2104 Fwiden ();
2106 del_range (BEG, Z);
2108 current_buffer->last_window_start = 1;
2109 /* Prevent warnings, or suspension of auto saving, that would happen
2110 if future size is less than past size. Use of erase-buffer
2111 implies that the future text is not really related to the past text. */
2112 XSETFASTINT (current_buffer->save_length, 0);
2113 return Qnil;
2116 void
2117 validate_region (b, e)
2118 register Lisp_Object *b, *e;
2120 CHECK_NUMBER_COERCE_MARKER (*b);
2121 CHECK_NUMBER_COERCE_MARKER (*e);
2123 if (XINT (*b) > XINT (*e))
2125 Lisp_Object tem;
2126 tem = *b; *b = *e; *e = tem;
2129 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2130 && XINT (*e) <= ZV))
2131 args_out_of_range (*b, *e);
2134 /* Advance BYTE_POS up to a character boundary
2135 and return the adjusted position. */
2137 static int
2138 advance_to_char_boundary (byte_pos)
2139 int byte_pos;
2141 int c;
2143 if (byte_pos == BEG)
2144 /* Beginning of buffer is always a character boundary. */
2145 return BEG;
2147 c = FETCH_BYTE (byte_pos);
2148 if (! CHAR_HEAD_P (c))
2150 /* We should advance BYTE_POS only when C is a constituent of a
2151 multibyte sequence. */
2152 int orig_byte_pos = byte_pos;
2156 byte_pos--;
2157 c = FETCH_BYTE (byte_pos);
2159 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2160 INC_POS (byte_pos);
2161 if (byte_pos < orig_byte_pos)
2162 byte_pos = orig_byte_pos;
2163 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2164 surely advance to the correct character boundary. If C is
2165 not, BYTE_POS was unchanged. */
2168 return byte_pos;
2171 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2172 1, 1, 0,
2173 doc: /* Set the multibyte flag of the current buffer to FLAG.
2174 If FLAG is t, this makes the buffer a multibyte buffer.
2175 If FLAG is nil, this makes the buffer a single-byte buffer.
2176 The buffer contents remain unchanged as a sequence of bytes
2177 but the contents viewed as characters do change.
2178 If the multibyte flag was really changed, undo information of the
2179 current buffer is cleared. */)
2180 (flag)
2181 Lisp_Object flag;
2183 struct Lisp_Marker *tail, *markers;
2184 struct buffer *other;
2185 int begv, zv;
2186 int narrowed = (BEG != BEGV || Z != ZV);
2187 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2188 Lisp_Object old_undo = current_buffer->undo_list;
2189 struct gcpro gcpro1;
2191 if (current_buffer->base_buffer)
2192 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2194 /* Do nothing if nothing actually changes. */
2195 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2196 return flag;
2198 GCPRO1 (old_undo);
2200 /* Don't record these buffer changes. We will put a special undo entry
2201 instead. */
2202 current_buffer->undo_list = Qt;
2204 /* If the cached position is for this buffer, clear it out. */
2205 clear_charpos_cache (current_buffer);
2207 if (NILP (flag))
2208 begv = BEGV_BYTE, zv = ZV_BYTE;
2209 else
2210 begv = BEGV, zv = ZV;
2212 if (narrowed)
2213 Fwiden ();
2215 if (NILP (flag))
2217 int pos, stop;
2218 unsigned char *p;
2220 /* Do this first, so it can use CHAR_TO_BYTE
2221 to calculate the old correspondences. */
2222 set_intervals_multibyte (0);
2224 current_buffer->enable_multibyte_characters = Qnil;
2226 Z = Z_BYTE;
2227 BEGV = BEGV_BYTE;
2228 ZV = ZV_BYTE;
2229 GPT = GPT_BYTE;
2230 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2233 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2234 tail->charpos = tail->bytepos;
2236 /* Convert multibyte form of 8-bit characters to unibyte. */
2237 pos = BEG;
2238 stop = GPT;
2239 p = BEG_ADDR;
2240 while (1)
2242 int c, bytes;
2244 if (pos == stop)
2246 if (pos == Z)
2247 break;
2248 p = GAP_END_ADDR;
2249 stop = Z;
2251 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
2252 p += bytes, pos += bytes;
2253 else
2255 c = STRING_CHAR (p, stop - pos);
2256 /* Delete all bytes for this 8-bit character but the
2257 last one, and change the last one to the charcter
2258 code. */
2259 bytes--;
2260 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2261 p = GAP_END_ADDR;
2262 *p++ = c;
2263 pos++;
2264 if (begv > pos)
2265 begv -= bytes;
2266 if (zv > pos)
2267 zv -= bytes;
2268 stop = Z;
2271 if (narrowed)
2272 Fnarrow_to_region (make_number (begv), make_number (zv));
2274 else
2276 int pt = PT;
2277 int pos, stop;
2278 unsigned char *p;
2280 /* Be sure not to have a multibyte sequence striding over the GAP.
2281 Ex: We change this: "...abc\201 _GAP_ \241def..."
2282 to: "...abc _GAP_ \201\241def..." */
2284 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2285 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2287 unsigned char *p = GPT_ADDR - 1;
2289 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2290 if (BASE_LEADING_CODE_P (*p))
2292 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2294 move_gap_both (new_gpt, new_gpt);
2298 /* Make the buffer contents valid as multibyte by converting
2299 8-bit characters to multibyte form. */
2300 pos = BEG;
2301 stop = GPT;
2302 p = BEG_ADDR;
2303 while (1)
2305 int bytes;
2307 if (pos == stop)
2309 if (pos == Z)
2310 break;
2311 p = GAP_END_ADDR;
2312 stop = Z;
2315 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2316 p += bytes, pos += bytes;
2317 else
2319 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2321 bytes = CHAR_STRING (*p, tmp);
2322 *p = tmp[0];
2323 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2324 bytes--;
2325 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2326 /* Now the gap is after the just inserted data. */
2327 pos = GPT;
2328 p = GAP_END_ADDR;
2329 if (pos <= begv)
2330 begv += bytes;
2331 if (pos <= zv)
2332 zv += bytes;
2333 if (pos <= pt)
2334 pt += bytes;
2335 stop = Z;
2339 if (pt != PT)
2340 TEMP_SET_PT (pt);
2342 if (narrowed)
2343 Fnarrow_to_region (make_number (begv), make_number (zv));
2345 /* Do this first, so that chars_in_text asks the right question.
2346 set_intervals_multibyte needs it too. */
2347 current_buffer->enable_multibyte_characters = Qt;
2349 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2350 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2352 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2354 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2355 if (BEGV_BYTE > GPT_BYTE)
2356 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2357 else
2358 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2360 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2361 if (ZV_BYTE > GPT_BYTE)
2362 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2363 else
2364 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2367 int pt_byte = advance_to_char_boundary (PT_BYTE);
2368 int pt;
2370 if (pt_byte > GPT_BYTE)
2371 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2372 else
2373 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2374 TEMP_SET_PT_BOTH (pt, pt_byte);
2377 tail = markers = BUF_MARKERS (current_buffer);
2379 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2380 getting confused by the markers that have not yet been updated.
2381 It is also a signal that it should never create a marker. */
2382 BUF_MARKERS (current_buffer) = NULL;
2384 for (; tail; tail = tail->next)
2386 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2387 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2390 /* Make sure no markers were put on the chain
2391 while the chain value was incorrect. */
2392 if (BUF_MARKERS (current_buffer))
2393 abort ();
2395 BUF_MARKERS (current_buffer) = markers;
2397 /* Do this last, so it can calculate the new correspondences
2398 between chars and bytes. */
2399 set_intervals_multibyte (1);
2402 if (!EQ (old_undo, Qt))
2404 /* Represent all the above changes by a special undo entry. */
2405 extern Lisp_Object Qapply;
2406 current_buffer->undo_list = Fcons (list3 (Qapply,
2407 intern ("set-buffer-multibyte"),
2408 NILP (flag) ? Qt : Qnil),
2409 old_undo);
2412 UNGCPRO;
2414 /* Changing the multibyteness of a buffer means that all windows
2415 showing that buffer must be updated thoroughly. */
2416 current_buffer->prevent_redisplay_optimizations_p = 1;
2417 ++windows_or_buffers_changed;
2419 /* Copy this buffer's new multibyte status
2420 into all of its indirect buffers. */
2421 for (other = all_buffers; other; other = other->next)
2422 if (other->base_buffer == current_buffer && !NILP (other->name))
2424 other->enable_multibyte_characters
2425 = current_buffer->enable_multibyte_characters;
2426 other->prevent_redisplay_optimizations_p = 1;
2429 /* Restore the modifiedness of the buffer. */
2430 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2431 Fset_buffer_modified_p (Qnil);
2433 #ifdef subprocesses
2434 /* Update coding systems of this buffer's process (if any). */
2436 Lisp_Object process;
2438 process = Fget_buffer_process (Fcurrent_buffer ());
2439 if (PROCESSP (process))
2440 setup_process_coding_systems (process);
2442 #endif /* subprocesses */
2444 return flag;
2447 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2448 0, 0, 0,
2449 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2450 Most local variable bindings are eliminated so that the default values
2451 become effective once more. Also, the syntax table is set from
2452 `standard-syntax-table', the local keymap is set to nil,
2453 and the abbrev table from `fundamental-mode-abbrev-table'.
2454 This function also forces redisplay of the mode line.
2456 Every function to select a new major mode starts by
2457 calling this function.
2459 As a special exception, local variables whose names have
2460 a non-nil `permanent-local' property are not eliminated by this function.
2462 The first thing this function does is run
2463 the normal hook `change-major-mode-hook'. */)
2466 register Lisp_Object alist, sym, tem;
2467 Lisp_Object oalist;
2469 if (!NILP (Vrun_hooks))
2470 call1 (Vrun_hooks, Qchange_major_mode_hook);
2471 oalist = current_buffer->local_var_alist;
2473 /* Make sure none of the bindings in oalist
2474 remain swapped in, in their symbols. */
2476 swap_out_buffer_local_variables (current_buffer);
2478 /* Actually eliminate all local bindings of this buffer. */
2480 reset_buffer_local_variables (current_buffer, 0);
2482 /* Any which are supposed to be permanent,
2483 make local again, with the same values they had. */
2485 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2487 sym = XCAR (XCAR (alist));
2488 tem = Fget (sym, Qpermanent_local);
2489 if (! NILP (tem))
2491 Fmake_local_variable (sym);
2492 Fset (sym, XCDR (XCAR (alist)));
2496 /* Force mode-line redisplay. Useful here because all major mode
2497 commands call this function. */
2498 update_mode_lines++;
2500 return Qnil;
2503 /* Make sure no local variables remain set up with buffer B
2504 for their current values. */
2506 static void
2507 swap_out_buffer_local_variables (b)
2508 struct buffer *b;
2510 Lisp_Object oalist, alist, sym, tem, buffer;
2512 XSETBUFFER (buffer, b);
2513 oalist = b->local_var_alist;
2515 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2517 sym = XCAR (XCAR (alist));
2519 /* Need not do anything if some other buffer's binding is now encached. */
2520 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2521 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2523 /* Symbol is set up for this buffer's old local value.
2524 Set it up for the current buffer with the default value. */
2526 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->cdr;
2527 /* Store the symbol's current value into the alist entry
2528 it is currently set up for. This is so that, if the
2529 local is marked permanent, and we make it local again
2530 later in Fkill_all_local_variables, we don't lose the value. */
2531 XSETCDR (XCAR (tem),
2532 do_symval_forwarding (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue));
2533 /* Switch to the symbol's default-value alist entry. */
2534 XSETCAR (tem, tem);
2535 /* Mark it as current for buffer B. */
2536 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer = buffer;
2537 /* Store the current value into any forwarding in the symbol. */
2538 store_symval_forwarding (sym,
2539 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue,
2540 XCDR (tem), NULL);
2545 /* Find all the overlays in the current buffer that contain position POS.
2546 Return the number found, and store them in a vector in *VEC_PTR.
2547 Store in *LEN_PTR the size allocated for the vector.
2548 Store in *NEXT_PTR the next position after POS where an overlay starts,
2549 or ZV if there are no more overlays between POS and ZV.
2550 Store in *PREV_PTR the previous position before POS where an overlay ends,
2551 or where an overlay starts which ends at or after POS;
2552 or BEGV if there are no such overlays from BEGV to POS.
2553 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2555 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2556 when this function is called.
2558 If EXTEND is non-zero, we make the vector bigger if necessary.
2559 If EXTEND is zero, we never extend the vector,
2560 and we store only as many overlays as will fit.
2561 But we still return the total number of overlays.
2563 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2564 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2565 default (BEGV or ZV). */
2568 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2569 EMACS_INT pos;
2570 int extend;
2571 Lisp_Object **vec_ptr;
2572 int *len_ptr;
2573 int *next_ptr;
2574 int *prev_ptr;
2575 int change_req;
2577 Lisp_Object overlay, start, end;
2578 struct Lisp_Overlay *tail;
2579 int idx = 0;
2580 int len = *len_ptr;
2581 Lisp_Object *vec = *vec_ptr;
2582 int next = ZV;
2583 int prev = BEGV;
2584 int inhibit_storing = 0;
2586 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2588 int startpos, endpos;
2590 XSETMISC (overlay, tail);
2592 start = OVERLAY_START (overlay);
2593 end = OVERLAY_END (overlay);
2594 endpos = OVERLAY_POSITION (end);
2595 if (endpos < pos)
2597 if (prev < endpos)
2598 prev = endpos;
2599 break;
2601 startpos = OVERLAY_POSITION (start);
2602 /* This one ends at or after POS
2603 so its start counts for PREV_PTR if it's before POS. */
2604 if (prev < startpos && startpos < pos)
2605 prev = startpos;
2606 if (endpos == pos)
2607 continue;
2608 if (startpos <= pos)
2610 if (idx == len)
2612 /* The supplied vector is full.
2613 Either make it bigger, or don't store any more in it. */
2614 if (extend)
2616 /* Make it work with an initial len == 0. */
2617 len *= 2;
2618 if (len == 0)
2619 len = 4;
2620 *len_ptr = len;
2621 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2622 *vec_ptr = vec;
2624 else
2625 inhibit_storing = 1;
2628 if (!inhibit_storing)
2629 vec[idx] = overlay;
2630 /* Keep counting overlays even if we can't return them all. */
2631 idx++;
2633 else if (startpos < next)
2634 next = startpos;
2637 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2639 int startpos, endpos;
2641 XSETMISC (overlay, tail);
2643 start = OVERLAY_START (overlay);
2644 end = OVERLAY_END (overlay);
2645 startpos = OVERLAY_POSITION (start);
2646 if (pos < startpos)
2648 if (startpos < next)
2649 next = startpos;
2650 break;
2652 endpos = OVERLAY_POSITION (end);
2653 if (pos < endpos)
2655 if (idx == len)
2657 if (extend)
2659 /* Make it work with an initial len == 0. */
2660 len *= 2;
2661 if (len == 0)
2662 len = 4;
2663 *len_ptr = len;
2664 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2665 *vec_ptr = vec;
2667 else
2668 inhibit_storing = 1;
2671 if (!inhibit_storing)
2672 vec[idx] = overlay;
2673 idx++;
2675 if (startpos < pos && startpos > prev)
2676 prev = startpos;
2678 else if (endpos < pos && endpos > prev)
2679 prev = endpos;
2680 else if (endpos == pos && startpos > prev
2681 && (!change_req || startpos < pos))
2682 prev = startpos;
2685 if (next_ptr)
2686 *next_ptr = next;
2687 if (prev_ptr)
2688 *prev_ptr = prev;
2689 return idx;
2692 /* Find all the overlays in the current buffer that overlap the range BEG-END
2693 or are empty at BEG.
2695 Return the number found, and store them in a vector in *VEC_PTR.
2696 Store in *LEN_PTR the size allocated for the vector.
2697 Store in *NEXT_PTR the next position after POS where an overlay starts,
2698 or ZV if there are no more overlays.
2699 Store in *PREV_PTR the previous position before POS where an overlay ends,
2700 or BEGV if there are no previous overlays.
2701 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2703 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2704 when this function is called.
2706 If EXTEND is non-zero, we make the vector bigger if necessary.
2707 If EXTEND is zero, we never extend the vector,
2708 and we store only as many overlays as will fit.
2709 But we still return the total number of overlays. */
2711 static int
2712 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2713 int beg, end;
2714 int extend;
2715 Lisp_Object **vec_ptr;
2716 int *len_ptr;
2717 int *next_ptr;
2718 int *prev_ptr;
2720 Lisp_Object overlay, ostart, oend;
2721 struct Lisp_Overlay *tail;
2722 int idx = 0;
2723 int len = *len_ptr;
2724 Lisp_Object *vec = *vec_ptr;
2725 int next = ZV;
2726 int prev = BEGV;
2727 int inhibit_storing = 0;
2729 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2731 int startpos, endpos;
2733 XSETMISC (overlay, tail);
2735 ostart = OVERLAY_START (overlay);
2736 oend = OVERLAY_END (overlay);
2737 endpos = OVERLAY_POSITION (oend);
2738 if (endpos < beg)
2740 if (prev < endpos)
2741 prev = endpos;
2742 break;
2744 startpos = OVERLAY_POSITION (ostart);
2745 /* Count an interval if it either overlaps the range
2746 or is empty at the start of the range. */
2747 if ((beg < endpos && startpos < end)
2748 || (startpos == endpos && beg == endpos))
2750 if (idx == len)
2752 /* The supplied vector is full.
2753 Either make it bigger, or don't store any more in it. */
2754 if (extend)
2756 /* Make it work with an initial len == 0. */
2757 len *= 2;
2758 if (len == 0)
2759 len = 4;
2760 *len_ptr = len;
2761 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2762 *vec_ptr = vec;
2764 else
2765 inhibit_storing = 1;
2768 if (!inhibit_storing)
2769 vec[idx] = overlay;
2770 /* Keep counting overlays even if we can't return them all. */
2771 idx++;
2773 else if (startpos < next)
2774 next = startpos;
2777 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2779 int startpos, endpos;
2781 XSETMISC (overlay, tail);
2783 ostart = OVERLAY_START (overlay);
2784 oend = OVERLAY_END (overlay);
2785 startpos = OVERLAY_POSITION (ostart);
2786 if (end < startpos)
2788 if (startpos < next)
2789 next = startpos;
2790 break;
2792 endpos = OVERLAY_POSITION (oend);
2793 /* Count an interval if it either overlaps the range
2794 or is empty at the start of the range. */
2795 if ((beg < endpos && startpos < end)
2796 || (startpos == endpos && beg == endpos))
2798 if (idx == len)
2800 if (extend)
2802 /* Make it work with an initial len == 0. */
2803 len *= 2;
2804 if (len == 0)
2805 len = 4;
2806 *len_ptr = len;
2807 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2808 *vec_ptr = vec;
2810 else
2811 inhibit_storing = 1;
2814 if (!inhibit_storing)
2815 vec[idx] = overlay;
2816 idx++;
2818 else if (endpos < beg && endpos > prev)
2819 prev = endpos;
2822 if (next_ptr)
2823 *next_ptr = next;
2824 if (prev_ptr)
2825 *prev_ptr = prev;
2826 return idx;
2830 /* Return non-zero if there exists an overlay with a non-nil
2831 `mouse-face' property overlapping OVERLAY. */
2834 mouse_face_overlay_overlaps (overlay)
2835 Lisp_Object overlay;
2837 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2838 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2839 int n, i, size;
2840 Lisp_Object *v, tem;
2842 size = 10;
2843 v = (Lisp_Object *) alloca (size * sizeof *v);
2844 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2845 if (n > size)
2847 v = (Lisp_Object *) alloca (n * sizeof *v);
2848 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2851 for (i = 0; i < n; ++i)
2852 if (!EQ (v[i], overlay)
2853 && (tem = Foverlay_get (overlay, Qmouse_face),
2854 !NILP (tem)))
2855 break;
2857 return i < n;
2862 /* Fast function to just test if we're at an overlay boundary. */
2864 overlay_touches_p (pos)
2865 int pos;
2867 Lisp_Object overlay;
2868 struct Lisp_Overlay *tail;
2870 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2872 int endpos;
2874 XSETMISC (overlay ,tail);
2875 if (!GC_OVERLAYP (overlay))
2876 abort ();
2878 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2879 if (endpos < pos)
2880 break;
2881 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2882 return 1;
2885 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2887 int startpos;
2889 XSETMISC (overlay, tail);
2890 if (!GC_OVERLAYP (overlay))
2891 abort ();
2893 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2894 if (pos < startpos)
2895 break;
2896 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2897 return 1;
2899 return 0;
2902 struct sortvec
2904 Lisp_Object overlay;
2905 int beg, end;
2906 int priority;
2909 static int
2910 compare_overlays (v1, v2)
2911 const void *v1, *v2;
2913 const struct sortvec *s1 = (const struct sortvec *) v1;
2914 const struct sortvec *s2 = (const struct sortvec *) v2;
2915 if (s1->priority != s2->priority)
2916 return s1->priority - s2->priority;
2917 if (s1->beg != s2->beg)
2918 return s1->beg - s2->beg;
2919 if (s1->end != s2->end)
2920 return s2->end - s1->end;
2921 return 0;
2924 /* Sort an array of overlays by priority. The array is modified in place.
2925 The return value is the new size; this may be smaller than the original
2926 size if some of the overlays were invalid or were window-specific. */
2928 sort_overlays (overlay_vec, noverlays, w)
2929 Lisp_Object *overlay_vec;
2930 int noverlays;
2931 struct window *w;
2933 int i, j;
2934 struct sortvec *sortvec;
2935 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2937 /* Put the valid and relevant overlays into sortvec. */
2939 for (i = 0, j = 0; i < noverlays; i++)
2941 Lisp_Object tem;
2942 Lisp_Object overlay;
2944 overlay = overlay_vec[i];
2945 if (OVERLAY_VALID (overlay)
2946 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2947 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2949 /* If we're interested in a specific window, then ignore
2950 overlays that are limited to some other window. */
2951 if (w)
2953 Lisp_Object window;
2955 window = Foverlay_get (overlay, Qwindow);
2956 if (WINDOWP (window) && XWINDOW (window) != w)
2957 continue;
2960 /* This overlay is good and counts: put it into sortvec. */
2961 sortvec[j].overlay = overlay;
2962 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2963 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2964 tem = Foverlay_get (overlay, Qpriority);
2965 if (INTEGERP (tem))
2966 sortvec[j].priority = XINT (tem);
2967 else
2968 sortvec[j].priority = 0;
2969 j++;
2972 noverlays = j;
2974 /* Sort the overlays into the proper order: increasing priority. */
2976 if (noverlays > 1)
2977 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2979 for (i = 0; i < noverlays; i++)
2980 overlay_vec[i] = sortvec[i].overlay;
2981 return (noverlays);
2984 struct sortstr
2986 Lisp_Object string, string2;
2987 int size;
2988 int priority;
2991 struct sortstrlist
2993 struct sortstr *buf; /* An array that expands as needed; never freed. */
2994 int size; /* Allocated length of that array. */
2995 int used; /* How much of the array is currently in use. */
2996 int bytes; /* Total length of the strings in buf. */
2999 /* Buffers for storing information about the overlays touching a given
3000 position. These could be automatic variables in overlay_strings, but
3001 it's more efficient to hold onto the memory instead of repeatedly
3002 allocating and freeing it. */
3003 static struct sortstrlist overlay_heads, overlay_tails;
3004 static unsigned char *overlay_str_buf;
3006 /* Allocated length of overlay_str_buf. */
3007 static int overlay_str_len;
3009 /* A comparison function suitable for passing to qsort. */
3010 static int
3011 cmp_for_strings (as1, as2)
3012 char *as1, *as2;
3014 struct sortstr *s1 = (struct sortstr *)as1;
3015 struct sortstr *s2 = (struct sortstr *)as2;
3016 if (s1->size != s2->size)
3017 return s2->size - s1->size;
3018 if (s1->priority != s2->priority)
3019 return s1->priority - s2->priority;
3020 return 0;
3023 static void
3024 record_overlay_string (ssl, str, str2, pri, size)
3025 struct sortstrlist *ssl;
3026 Lisp_Object str, str2, pri;
3027 int size;
3029 int nbytes;
3031 if (ssl->used == ssl->size)
3033 if (ssl->buf)
3034 ssl->size *= 2;
3035 else
3036 ssl->size = 5;
3037 ssl->buf = ((struct sortstr *)
3038 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3040 ssl->buf[ssl->used].string = str;
3041 ssl->buf[ssl->used].string2 = str2;
3042 ssl->buf[ssl->used].size = size;
3043 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3044 ssl->used++;
3046 if (NILP (current_buffer->enable_multibyte_characters))
3047 nbytes = SCHARS (str);
3048 else if (! STRING_MULTIBYTE (str))
3049 nbytes = count_size_as_multibyte (SDATA (str),
3050 SBYTES (str));
3051 else
3052 nbytes = SBYTES (str);
3054 ssl->bytes += nbytes;
3056 if (STRINGP (str2))
3058 if (NILP (current_buffer->enable_multibyte_characters))
3059 nbytes = SCHARS (str2);
3060 else if (! STRING_MULTIBYTE (str2))
3061 nbytes = count_size_as_multibyte (SDATA (str2),
3062 SBYTES (str2));
3063 else
3064 nbytes = SBYTES (str2);
3066 ssl->bytes += nbytes;
3070 /* Return the concatenation of the strings associated with overlays that
3071 begin or end at POS, ignoring overlays that are specific to a window
3072 other than W. The strings are concatenated in the appropriate order:
3073 shorter overlays nest inside longer ones, and higher priority inside
3074 lower. Normally all of the after-strings come first, but zero-sized
3075 overlays have their after-strings ride along with the before-strings
3076 because it would look strange to print them inside-out.
3078 Returns the string length, and stores the contents indirectly through
3079 PSTR, if that variable is non-null. The string may be overwritten by
3080 subsequent calls. */
3083 overlay_strings (pos, w, pstr)
3084 EMACS_INT pos;
3085 struct window *w;
3086 unsigned char **pstr;
3088 Lisp_Object overlay, window, str;
3089 struct Lisp_Overlay *ov;
3090 int startpos, endpos;
3091 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3093 overlay_heads.used = overlay_heads.bytes = 0;
3094 overlay_tails.used = overlay_tails.bytes = 0;
3095 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3097 XSETMISC (overlay, ov);
3098 eassert (OVERLAYP (overlay));
3100 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3101 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3102 if (endpos < pos)
3103 break;
3104 if (endpos != pos && startpos != pos)
3105 continue;
3106 window = Foverlay_get (overlay, Qwindow);
3107 if (WINDOWP (window) && XWINDOW (window) != w)
3108 continue;
3109 if (startpos == pos
3110 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3111 record_overlay_string (&overlay_heads, str,
3112 (startpos == endpos
3113 ? Foverlay_get (overlay, Qafter_string)
3114 : Qnil),
3115 Foverlay_get (overlay, Qpriority),
3116 endpos - startpos);
3117 else if (endpos == pos
3118 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3119 record_overlay_string (&overlay_tails, str, Qnil,
3120 Foverlay_get (overlay, Qpriority),
3121 endpos - startpos);
3123 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3125 XSETMISC (overlay, ov);
3126 eassert (OVERLAYP (overlay));
3128 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3129 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3130 if (startpos > pos)
3131 break;
3132 if (endpos != pos && startpos != pos)
3133 continue;
3134 window = Foverlay_get (overlay, Qwindow);
3135 if (WINDOWP (window) && XWINDOW (window) != w)
3136 continue;
3137 if (startpos == pos
3138 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3139 record_overlay_string (&overlay_heads, str,
3140 (startpos == endpos
3141 ? Foverlay_get (overlay, Qafter_string)
3142 : Qnil),
3143 Foverlay_get (overlay, Qpriority),
3144 endpos - startpos);
3145 else if (endpos == pos
3146 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3147 record_overlay_string (&overlay_tails, str, Qnil,
3148 Foverlay_get (overlay, Qpriority),
3149 endpos - startpos);
3151 if (overlay_tails.used > 1)
3152 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3153 cmp_for_strings);
3154 if (overlay_heads.used > 1)
3155 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3156 cmp_for_strings);
3157 if (overlay_heads.bytes || overlay_tails.bytes)
3159 Lisp_Object tem;
3160 int i;
3161 unsigned char *p;
3162 int total = overlay_heads.bytes + overlay_tails.bytes;
3164 if (total > overlay_str_len)
3166 overlay_str_len = total;
3167 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3168 total);
3170 p = overlay_str_buf;
3171 for (i = overlay_tails.used; --i >= 0;)
3173 int nbytes;
3174 tem = overlay_tails.buf[i].string;
3175 nbytes = copy_text (SDATA (tem), p,
3176 SBYTES (tem),
3177 STRING_MULTIBYTE (tem), multibyte);
3178 p += nbytes;
3180 for (i = 0; i < overlay_heads.used; ++i)
3182 int nbytes;
3183 tem = overlay_heads.buf[i].string;
3184 nbytes = copy_text (SDATA (tem), p,
3185 SBYTES (tem),
3186 STRING_MULTIBYTE (tem), multibyte);
3187 p += nbytes;
3188 tem = overlay_heads.buf[i].string2;
3189 if (STRINGP (tem))
3191 nbytes = copy_text (SDATA (tem), p,
3192 SBYTES (tem),
3193 STRING_MULTIBYTE (tem), multibyte);
3194 p += nbytes;
3197 if (p != overlay_str_buf + total)
3198 abort ();
3199 if (pstr)
3200 *pstr = overlay_str_buf;
3201 return total;
3203 return 0;
3206 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3208 void
3209 recenter_overlay_lists (buf, pos)
3210 struct buffer *buf;
3211 EMACS_INT pos;
3213 Lisp_Object overlay, beg, end;
3214 struct Lisp_Overlay *prev, *tail, *next;
3216 /* See if anything in overlays_before should move to overlays_after. */
3218 /* We don't strictly need prev in this loop; it should always be nil.
3219 But we use it for symmetry and in case that should cease to be true
3220 with some future change. */
3221 prev = NULL;
3222 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3224 next = tail->next;
3225 XSETMISC (overlay, tail);
3227 /* If the overlay is not valid, get rid of it. */
3228 if (!OVERLAY_VALID (overlay))
3229 #if 1
3230 abort ();
3231 #else
3233 /* Splice the cons cell TAIL out of overlays_before. */
3234 if (!NILP (prev))
3235 XCDR (prev) = next;
3236 else
3237 buf->overlays_before = next;
3238 tail = prev;
3239 continue;
3241 #endif
3243 beg = OVERLAY_START (overlay);
3244 end = OVERLAY_END (overlay);
3246 if (OVERLAY_POSITION (end) > pos)
3248 /* OVERLAY needs to be moved. */
3249 int where = OVERLAY_POSITION (beg);
3250 struct Lisp_Overlay *other, *other_prev;
3252 /* Splice the cons cell TAIL out of overlays_before. */
3253 if (prev)
3254 prev->next = next;
3255 else
3256 buf->overlays_before = next;
3258 /* Search thru overlays_after for where to put it. */
3259 other_prev = NULL;
3260 for (other = buf->overlays_after; other;
3261 other_prev = other, other = other->next)
3263 Lisp_Object otherbeg, otheroverlay;
3265 XSETMISC (otheroverlay, other);
3266 eassert (OVERLAY_VALID (otheroverlay));
3268 otherbeg = OVERLAY_START (otheroverlay);
3269 if (OVERLAY_POSITION (otherbeg) >= where)
3270 break;
3273 /* Add TAIL to overlays_after before OTHER. */
3274 tail->next = other;
3275 if (other_prev)
3276 other_prev->next = tail;
3277 else
3278 buf->overlays_after = tail;
3279 tail = prev;
3281 else
3282 /* We've reached the things that should stay in overlays_before.
3283 All the rest of overlays_before must end even earlier,
3284 so stop now. */
3285 break;
3288 /* See if anything in overlays_after should be in overlays_before. */
3289 prev = NULL;
3290 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3292 next = tail->next;
3293 XSETMISC (overlay, tail);
3295 /* If the overlay is not valid, get rid of it. */
3296 if (!OVERLAY_VALID (overlay))
3297 #if 1
3298 abort ();
3299 #else
3301 /* Splice the cons cell TAIL out of overlays_after. */
3302 if (!NILP (prev))
3303 XCDR (prev) = next;
3304 else
3305 buf->overlays_after = next;
3306 tail = prev;
3307 continue;
3309 #endif
3311 beg = OVERLAY_START (overlay);
3312 end = OVERLAY_END (overlay);
3314 /* Stop looking, when we know that nothing further
3315 can possibly end before POS. */
3316 if (OVERLAY_POSITION (beg) > pos)
3317 break;
3319 if (OVERLAY_POSITION (end) <= pos)
3321 /* OVERLAY needs to be moved. */
3322 int where = OVERLAY_POSITION (end);
3323 struct Lisp_Overlay *other, *other_prev;
3325 /* Splice the cons cell TAIL out of overlays_after. */
3326 if (prev)
3327 prev->next = next;
3328 else
3329 buf->overlays_after = next;
3331 /* Search thru overlays_before for where to put it. */
3332 other_prev = NULL;
3333 for (other = buf->overlays_before; other;
3334 other_prev = other, other = other->next)
3336 Lisp_Object otherend, otheroverlay;
3338 XSETMISC (otheroverlay, other);
3339 eassert (OVERLAY_VALID (otheroverlay));
3341 otherend = OVERLAY_END (otheroverlay);
3342 if (OVERLAY_POSITION (otherend) <= where)
3343 break;
3346 /* Add TAIL to overlays_before before OTHER. */
3347 tail->next = other;
3348 if (other_prev)
3349 other_prev->next = tail;
3350 else
3351 buf->overlays_before = tail;
3352 tail = prev;
3356 buf->overlay_center = pos;
3359 void
3360 adjust_overlays_for_insert (pos, length)
3361 EMACS_INT pos;
3362 EMACS_INT length;
3364 /* After an insertion, the lists are still sorted properly,
3365 but we may need to update the value of the overlay center. */
3366 if (current_buffer->overlay_center >= pos)
3367 current_buffer->overlay_center += length;
3370 void
3371 adjust_overlays_for_delete (pos, length)
3372 EMACS_INT pos;
3373 EMACS_INT length;
3375 if (current_buffer->overlay_center < pos)
3376 /* The deletion was to our right. No change needed; the before- and
3377 after-lists are still consistent. */
3379 else if (current_buffer->overlay_center > pos + length)
3380 /* The deletion was to our left. We need to adjust the center value
3381 to account for the change in position, but the lists are consistent
3382 given the new value. */
3383 current_buffer->overlay_center -= length;
3384 else
3385 /* We're right in the middle. There might be things on the after-list
3386 that now belong on the before-list. Recentering will move them,
3387 and also update the center point. */
3388 recenter_overlay_lists (current_buffer, pos);
3391 /* Fix up overlays that were garbled as a result of permuting markers
3392 in the range START through END. Any overlay with at least one
3393 endpoint in this range will need to be unlinked from the overlay
3394 list and reinserted in its proper place.
3395 Such an overlay might even have negative size at this point.
3396 If so, we'll make the overlay empty. */
3397 void
3398 fix_start_end_in_overlays (start, end)
3399 register int start, end;
3401 Lisp_Object overlay;
3402 struct Lisp_Overlay *before_list, *after_list;
3403 /* These are either nil, indicating that before_list or after_list
3404 should be assigned, or the cons cell the cdr of which should be
3405 assigned. */
3406 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3407 /* 'Parent', likewise, indicates a cons cell or
3408 current_buffer->overlays_before or overlays_after, depending
3409 which loop we're in. */
3410 struct Lisp_Overlay *tail, *parent;
3411 int startpos, endpos;
3413 /* This algorithm shifts links around instead of consing and GCing.
3414 The loop invariant is that before_list (resp. after_list) is a
3415 well-formed list except that its last element, the CDR of beforep
3416 (resp. afterp) if beforep (afterp) isn't nil or before_list
3417 (after_list) if it is, is still uninitialized. So it's not a bug
3418 that before_list isn't initialized, although it may look
3419 strange. */
3420 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3422 XSETMISC (overlay, tail);
3424 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3425 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3427 /* If the overlay is backwards, make it empty. */
3428 if (endpos < startpos)
3430 startpos = endpos;
3431 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3432 Qnil);
3435 if (endpos < start)
3436 break;
3438 if (endpos < end
3439 || (startpos >= start && startpos < end))
3441 /* Add it to the end of the wrong list. Later on,
3442 recenter_overlay_lists will move it to the right place. */
3443 if (endpos < current_buffer->overlay_center)
3445 if (!afterp)
3446 after_list = tail;
3447 else
3448 afterp->next = tail;
3449 afterp = tail;
3451 else
3453 if (!beforep)
3454 before_list = tail;
3455 else
3456 beforep->next = tail;
3457 beforep = tail;
3459 if (!parent)
3460 current_buffer->overlays_before = tail->next;
3461 else
3462 parent->next = tail->next;
3463 tail = tail->next;
3465 else
3466 parent = tail, tail = parent->next;
3468 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3470 XSETMISC (overlay, tail);
3472 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3473 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3475 /* If the overlay is backwards, make it empty. */
3476 if (endpos < startpos)
3478 startpos = endpos;
3479 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3480 Qnil);
3483 if (startpos >= end)
3484 break;
3486 if (startpos >= start
3487 || (endpos >= start && endpos < end))
3489 if (endpos < current_buffer->overlay_center)
3491 if (!afterp)
3492 after_list = tail;
3493 else
3494 afterp->next = tail;
3495 afterp = tail;
3497 else
3499 if (!beforep)
3500 before_list = tail;
3501 else
3502 beforep->next = tail;
3503 beforep = tail;
3505 if (!parent)
3506 current_buffer->overlays_after = tail->next;
3507 else
3508 parent->next = tail->next;
3509 tail = tail->next;
3511 else
3512 parent = tail, tail = parent->next;
3515 /* Splice the constructed (wrong) lists into the buffer's lists,
3516 and let the recenter function make it sane again. */
3517 if (beforep)
3519 beforep->next = current_buffer->overlays_before;
3520 current_buffer->overlays_before = before_list;
3522 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3524 if (afterp)
3526 afterp->next = current_buffer->overlays_after;
3527 current_buffer->overlays_after = after_list;
3529 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3532 /* We have two types of overlay: the one whose ending marker is
3533 after-insertion-marker (this is the usual case) and the one whose
3534 ending marker is before-insertion-marker. When `overlays_before'
3535 contains overlays of the latter type and the former type in this
3536 order and both overlays end at inserting position, inserting a text
3537 increases only the ending marker of the latter type, which results
3538 in incorrect ordering of `overlays_before'.
3540 This function fixes ordering of overlays in the slot
3541 `overlays_before' of the buffer *BP. Before the insertion, `point'
3542 was at PREV, and now is at POS. */
3544 void
3545 fix_overlays_before (bp, prev, pos)
3546 struct buffer *bp;
3547 EMACS_INT prev, pos;
3549 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3550 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3551 Lisp_Object tem;
3552 EMACS_INT end;
3554 /* After the insertion, the several overlays may be in incorrect
3555 order. The possibility is that, in the list `overlays_before',
3556 an overlay which ends at POS appears after an overlay which ends
3557 at PREV. Since POS is greater than PREV, we must fix the
3558 ordering of these overlays, by moving overlays ends at POS before
3559 the overlays ends at PREV. */
3561 /* At first, find a place where disordered overlays should be linked
3562 in. It is where an overlay which end before POS exists. (i.e. an
3563 overlay whose ending marker is after-insertion-marker if disorder
3564 exists). */
3565 while (tail
3566 && (XSETMISC (tem, tail),
3567 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3569 parent = tail;
3570 tail = tail->next;
3573 /* If we don't find such an overlay,
3574 or the found one ends before PREV,
3575 or the found one is the last one in the list,
3576 we don't have to fix anything. */
3577 if (!tail || end < prev || !tail->next)
3578 return;
3580 right_pair = parent;
3581 parent = tail;
3582 tail = tail->next;
3584 /* Now, end position of overlays in the list TAIL should be before
3585 or equal to PREV. In the loop, an overlay which ends at POS is
3586 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3587 we found an overlay which ends before PREV, the remaining
3588 overlays are in correct order. */
3589 while (tail)
3591 XSETMISC (tem, tail);
3592 end = OVERLAY_POSITION (OVERLAY_END (tem));
3594 if (end == pos)
3595 { /* This overlay is disordered. */
3596 struct Lisp_Overlay *found = tail;
3598 /* Unlink the found overlay. */
3599 tail = found->next;
3600 parent->next = tail;
3601 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3602 and link it into the right place. */
3603 if (!right_pair)
3605 found->next = bp->overlays_before;
3606 bp->overlays_before = found;
3608 else
3610 found->next = right_pair->next;
3611 right_pair->next = found;
3614 else if (end == prev)
3616 parent = tail;
3617 tail = tail->next;
3619 else /* No more disordered overlay. */
3620 break;
3624 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3625 doc: /* Return t if OBJECT is an overlay. */)
3626 (object)
3627 Lisp_Object object;
3629 return (OVERLAYP (object) ? Qt : Qnil);
3632 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3633 doc: /* Create a new overlay with range BEG to END in BUFFER.
3634 If omitted, BUFFER defaults to the current buffer.
3635 BEG and END may be integers or markers.
3636 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3637 for the front of the overlay advance when text is inserted there
3638 \(which means the text *is not* included in the overlay).
3639 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3640 for the rear of the overlay advance when text is inserted there
3641 \(which means the text *is* included in the overlay). */)
3642 (beg, end, buffer, front_advance, rear_advance)
3643 Lisp_Object beg, end, buffer;
3644 Lisp_Object front_advance, rear_advance;
3646 Lisp_Object overlay;
3647 struct buffer *b;
3649 if (NILP (buffer))
3650 XSETBUFFER (buffer, current_buffer);
3651 else
3652 CHECK_BUFFER (buffer);
3653 if (MARKERP (beg)
3654 && ! EQ (Fmarker_buffer (beg), buffer))
3655 error ("Marker points into wrong buffer");
3656 if (MARKERP (end)
3657 && ! EQ (Fmarker_buffer (end), buffer))
3658 error ("Marker points into wrong buffer");
3660 CHECK_NUMBER_COERCE_MARKER (beg);
3661 CHECK_NUMBER_COERCE_MARKER (end);
3663 if (XINT (beg) > XINT (end))
3665 Lisp_Object temp;
3666 temp = beg; beg = end; end = temp;
3669 b = XBUFFER (buffer);
3671 beg = Fset_marker (Fmake_marker (), beg, buffer);
3672 end = Fset_marker (Fmake_marker (), end, buffer);
3674 if (!NILP (front_advance))
3675 XMARKER (beg)->insertion_type = 1;
3676 if (!NILP (rear_advance))
3677 XMARKER (end)->insertion_type = 1;
3679 overlay = allocate_misc ();
3680 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3681 XOVERLAY (overlay)->start = beg;
3682 XOVERLAY (overlay)->end = end;
3683 XOVERLAY (overlay)->plist = Qnil;
3684 XOVERLAY (overlay)->next = NULL;
3686 /* Put the new overlay on the wrong list. */
3687 end = OVERLAY_END (overlay);
3688 if (OVERLAY_POSITION (end) < b->overlay_center)
3690 if (b->overlays_after)
3691 XOVERLAY (overlay)->next = b->overlays_after;
3692 b->overlays_after = XOVERLAY (overlay);
3694 else
3696 if (b->overlays_before)
3697 XOVERLAY (overlay)->next = b->overlays_before;
3698 b->overlays_before = XOVERLAY (overlay);
3701 /* This puts it in the right list, and in the right order. */
3702 recenter_overlay_lists (b, b->overlay_center);
3704 /* We don't need to redisplay the region covered by the overlay, because
3705 the overlay has no properties at the moment. */
3707 return overlay;
3710 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3712 static void
3713 modify_overlay (buf, start, end)
3714 struct buffer *buf;
3715 EMACS_INT start, end;
3717 if (start > end)
3719 int temp = start;
3720 start = end;
3721 end = temp;
3724 BUF_COMPUTE_UNCHANGED (buf, start, end);
3726 /* If this is a buffer not in the selected window,
3727 we must do other windows. */
3728 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3729 windows_or_buffers_changed = 1;
3730 /* If multiple windows show this buffer, we must do other windows. */
3731 else if (buffer_shared > 1)
3732 windows_or_buffers_changed = 1;
3733 /* If we modify an overlay at the end of the buffer, we cannot
3734 be sure that window end is still valid. */
3735 else if (end >= ZV && start <= ZV)
3736 windows_or_buffers_changed = 1;
3738 ++BUF_OVERLAY_MODIFF (buf);
3742 Lisp_Object Fdelete_overlay ();
3744 static struct Lisp_Overlay *
3745 unchain_overlay (list, overlay)
3746 struct Lisp_Overlay *list, *overlay;
3748 struct Lisp_Overlay *tmp, *prev;
3749 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3750 if (tmp == overlay)
3752 if (prev)
3753 prev->next = tmp->next;
3754 else
3755 list = tmp->next;
3756 overlay->next = NULL;
3757 break;
3759 return list;
3762 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3763 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3764 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3765 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3766 buffer. */)
3767 (overlay, beg, end, buffer)
3768 Lisp_Object overlay, beg, end, buffer;
3770 struct buffer *b, *ob;
3771 Lisp_Object obuffer;
3772 int count = SPECPDL_INDEX ();
3774 CHECK_OVERLAY (overlay);
3775 if (NILP (buffer))
3776 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3777 if (NILP (buffer))
3778 XSETBUFFER (buffer, current_buffer);
3779 CHECK_BUFFER (buffer);
3781 if (MARKERP (beg)
3782 && ! EQ (Fmarker_buffer (beg), buffer))
3783 error ("Marker points into wrong buffer");
3784 if (MARKERP (end)
3785 && ! EQ (Fmarker_buffer (end), buffer))
3786 error ("Marker points into wrong buffer");
3788 CHECK_NUMBER_COERCE_MARKER (beg);
3789 CHECK_NUMBER_COERCE_MARKER (end);
3791 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3792 return Fdelete_overlay (overlay);
3794 if (XINT (beg) > XINT (end))
3796 Lisp_Object temp;
3797 temp = beg; beg = end; end = temp;
3800 specbind (Qinhibit_quit, Qt);
3802 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3803 b = XBUFFER (buffer);
3804 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3806 /* If the overlay has changed buffers, do a thorough redisplay. */
3807 if (!EQ (buffer, obuffer))
3809 /* Redisplay where the overlay was. */
3810 if (!NILP (obuffer))
3812 int o_beg;
3813 int o_end;
3815 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3816 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3818 modify_overlay (ob, o_beg, o_end);
3821 /* Redisplay where the overlay is going to be. */
3822 modify_overlay (b, XINT (beg), XINT (end));
3824 else
3825 /* Redisplay the area the overlay has just left, or just enclosed. */
3827 int o_beg, o_end;
3829 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3830 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3832 if (o_beg == XINT (beg))
3833 modify_overlay (b, o_end, XINT (end));
3834 else if (o_end == XINT (end))
3835 modify_overlay (b, o_beg, XINT (beg));
3836 else
3838 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3839 if (XINT (end) > o_end) o_end = XINT (end);
3840 modify_overlay (b, o_beg, o_end);
3844 if (!NILP (obuffer))
3846 ob->overlays_before
3847 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3848 ob->overlays_after
3849 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3850 eassert (XOVERLAY (overlay)->next == NULL);
3853 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3854 Fset_marker (OVERLAY_END (overlay), end, buffer);
3856 /* Put the overlay on the wrong list. */
3857 end = OVERLAY_END (overlay);
3858 if (OVERLAY_POSITION (end) < b->overlay_center)
3860 XOVERLAY (overlay)->next = b->overlays_after;
3861 b->overlays_after = XOVERLAY (overlay);
3863 else
3865 XOVERLAY (overlay)->next = b->overlays_before;
3866 b->overlays_before = XOVERLAY (overlay);
3869 /* This puts it in the right list, and in the right order. */
3870 recenter_overlay_lists (b, b->overlay_center);
3872 return unbind_to (count, overlay);
3875 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3876 doc: /* Delete the overlay OVERLAY from its buffer. */)
3877 (overlay)
3878 Lisp_Object overlay;
3880 Lisp_Object buffer;
3881 struct buffer *b;
3882 int count = SPECPDL_INDEX ();
3884 CHECK_OVERLAY (overlay);
3886 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3887 if (NILP (buffer))
3888 return Qnil;
3890 b = XBUFFER (buffer);
3891 specbind (Qinhibit_quit, Qt);
3893 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3894 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3895 eassert (XOVERLAY (overlay)->next == NULL);
3896 modify_overlay (b,
3897 marker_position (OVERLAY_START (overlay)),
3898 marker_position (OVERLAY_END (overlay)));
3899 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3900 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3902 /* When deleting an overlay with before or after strings, turn off
3903 display optimizations for the affected buffer, on the basis that
3904 these strings may contain newlines. This is easier to do than to
3905 check for that situation during redisplay. */
3906 if (!windows_or_buffers_changed
3907 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3908 || !NILP (Foverlay_get (overlay, Qafter_string))))
3909 b->prevent_redisplay_optimizations_p = 1;
3911 return unbind_to (count, Qnil);
3914 /* Overlay dissection functions. */
3916 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3917 doc: /* Return the position at which OVERLAY starts. */)
3918 (overlay)
3919 Lisp_Object overlay;
3921 CHECK_OVERLAY (overlay);
3923 return (Fmarker_position (OVERLAY_START (overlay)));
3926 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3927 doc: /* Return the position at which OVERLAY ends. */)
3928 (overlay)
3929 Lisp_Object overlay;
3931 CHECK_OVERLAY (overlay);
3933 return (Fmarker_position (OVERLAY_END (overlay)));
3936 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3937 doc: /* Return the buffer OVERLAY belongs to.
3938 Return nil if OVERLAY has been deleted. */)
3939 (overlay)
3940 Lisp_Object overlay;
3942 CHECK_OVERLAY (overlay);
3944 return Fmarker_buffer (OVERLAY_START (overlay));
3947 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3948 doc: /* Return a list of the properties on OVERLAY.
3949 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3950 OVERLAY. */)
3951 (overlay)
3952 Lisp_Object overlay;
3954 CHECK_OVERLAY (overlay);
3956 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3960 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3961 doc: /* Return a list of the overlays that contain the character at POS. */)
3962 (pos)
3963 Lisp_Object pos;
3965 int noverlays;
3966 Lisp_Object *overlay_vec;
3967 int len;
3968 Lisp_Object result;
3970 CHECK_NUMBER_COERCE_MARKER (pos);
3972 len = 10;
3973 /* We can't use alloca here because overlays_at can call xrealloc. */
3974 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3976 /* Put all the overlays we want in a vector in overlay_vec.
3977 Store the length in len. */
3978 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3979 (int *) 0, (int *) 0, 0);
3981 /* Make a list of them all. */
3982 result = Flist (noverlays, overlay_vec);
3984 xfree (overlay_vec);
3985 return result;
3988 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3989 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3990 Overlap means that at least one character is contained within the overlay
3991 and also contained within the specified region.
3992 Empty overlays are included in the result if they are located at BEG
3993 or between BEG and END. */)
3994 (beg, end)
3995 Lisp_Object beg, end;
3997 int noverlays;
3998 Lisp_Object *overlay_vec;
3999 int len;
4000 Lisp_Object result;
4002 CHECK_NUMBER_COERCE_MARKER (beg);
4003 CHECK_NUMBER_COERCE_MARKER (end);
4005 len = 10;
4006 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4008 /* Put all the overlays we want in a vector in overlay_vec.
4009 Store the length in len. */
4010 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4011 (int *) 0, (int *) 0);
4013 /* Make a list of them all. */
4014 result = Flist (noverlays, overlay_vec);
4016 xfree (overlay_vec);
4017 return result;
4020 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4021 1, 1, 0,
4022 doc: /* Return the next position after POS where an overlay starts or ends.
4023 If there are no overlay boundaries from POS to (point-max),
4024 the value is (point-max). */)
4025 (pos)
4026 Lisp_Object pos;
4028 int noverlays;
4029 int endpos;
4030 Lisp_Object *overlay_vec;
4031 int len;
4032 int i;
4034 CHECK_NUMBER_COERCE_MARKER (pos);
4036 len = 10;
4037 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4039 /* Put all the overlays we want in a vector in overlay_vec.
4040 Store the length in len.
4041 endpos gets the position where the next overlay starts. */
4042 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4043 &endpos, (int *) 0, 1);
4045 /* If any of these overlays ends before endpos,
4046 use its ending point instead. */
4047 for (i = 0; i < noverlays; i++)
4049 Lisp_Object oend;
4050 int oendpos;
4052 oend = OVERLAY_END (overlay_vec[i]);
4053 oendpos = OVERLAY_POSITION (oend);
4054 if (oendpos < endpos)
4055 endpos = oendpos;
4058 xfree (overlay_vec);
4059 return make_number (endpos);
4062 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4063 Sprevious_overlay_change, 1, 1, 0,
4064 doc: /* Return the previous position before POS where an overlay starts or ends.
4065 If there are no overlay boundaries from (point-min) to POS,
4066 the value is (point-min). */)
4067 (pos)
4068 Lisp_Object pos;
4070 int noverlays;
4071 int prevpos;
4072 Lisp_Object *overlay_vec;
4073 int len;
4075 CHECK_NUMBER_COERCE_MARKER (pos);
4077 /* At beginning of buffer, we know the answer;
4078 avoid bug subtracting 1 below. */
4079 if (XINT (pos) == BEGV)
4080 return pos;
4082 len = 10;
4083 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4085 /* Put all the overlays we want in a vector in overlay_vec.
4086 Store the length in len.
4087 prevpos gets the position of the previous change. */
4088 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4089 (int *) 0, &prevpos, 1);
4091 xfree (overlay_vec);
4092 return make_number (prevpos);
4095 /* These functions are for debugging overlays. */
4097 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4098 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4099 The car has all the overlays before the overlay center;
4100 the cdr has all the overlays after the overlay center.
4101 Recentering overlays moves overlays between these lists.
4102 The lists you get are copies, so that changing them has no effect.
4103 However, the overlays you get are the real objects that the buffer uses. */)
4106 struct Lisp_Overlay *ol;
4107 Lisp_Object before = Qnil, after = Qnil, tmp;
4108 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4110 XSETMISC (tmp, ol);
4111 before = Fcons (tmp, before);
4113 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4115 XSETMISC (tmp, ol);
4116 after = Fcons (tmp, after);
4118 return Fcons (Fnreverse (before), Fnreverse (after));
4121 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4122 doc: /* Recenter the overlays of the current buffer around position POS.
4123 That makes overlay lookup faster for positions near POS (but perhaps slower
4124 for positions far away from POS). */)
4125 (pos)
4126 Lisp_Object pos;
4128 CHECK_NUMBER_COERCE_MARKER (pos);
4130 recenter_overlay_lists (current_buffer, XINT (pos));
4131 return Qnil;
4134 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4135 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4136 (overlay, prop)
4137 Lisp_Object overlay, prop;
4139 CHECK_OVERLAY (overlay);
4140 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4143 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4144 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4145 (overlay, prop, value)
4146 Lisp_Object overlay, prop, value;
4148 Lisp_Object tail, buffer;
4149 int changed;
4151 CHECK_OVERLAY (overlay);
4153 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4155 for (tail = XOVERLAY (overlay)->plist;
4156 CONSP (tail) && CONSP (XCDR (tail));
4157 tail = XCDR (XCDR (tail)))
4158 if (EQ (XCAR (tail), prop))
4160 changed = !EQ (XCAR (XCDR (tail)), value);
4161 XSETCAR (XCDR (tail), value);
4162 goto found;
4164 /* It wasn't in the list, so add it to the front. */
4165 changed = !NILP (value);
4166 XOVERLAY (overlay)->plist
4167 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4168 found:
4169 if (! NILP (buffer))
4171 if (changed)
4172 modify_overlay (XBUFFER (buffer),
4173 marker_position (OVERLAY_START (overlay)),
4174 marker_position (OVERLAY_END (overlay)));
4175 if (EQ (prop, Qevaporate) && ! NILP (value)
4176 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4177 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4178 Fdelete_overlay (overlay);
4181 return value;
4184 /* Subroutine of report_overlay_modification. */
4186 /* Lisp vector holding overlay hook functions to call.
4187 Vector elements come in pairs.
4188 Each even-index element is a list of hook functions.
4189 The following odd-index element is the overlay they came from.
4191 Before the buffer change, we fill in this vector
4192 as we call overlay hook functions.
4193 After the buffer change, we get the functions to call from this vector.
4194 This way we always call the same functions before and after the change. */
4195 static Lisp_Object last_overlay_modification_hooks;
4197 /* Number of elements actually used in last_overlay_modification_hooks. */
4198 static int last_overlay_modification_hooks_used;
4200 /* Add one functionlist/overlay pair
4201 to the end of last_overlay_modification_hooks. */
4203 static void
4204 add_overlay_mod_hooklist (functionlist, overlay)
4205 Lisp_Object functionlist, overlay;
4207 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4209 if (last_overlay_modification_hooks_used == oldsize)
4210 last_overlay_modification_hooks = larger_vector
4211 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4212 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = functionlist;
4213 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = overlay;
4216 /* Run the modification-hooks of overlays that include
4217 any part of the text in START to END.
4218 If this change is an insertion, also
4219 run the insert-before-hooks of overlay starting at END,
4220 and the insert-after-hooks of overlay ending at START.
4222 This is called both before and after the modification.
4223 AFTER is nonzero when we call after the modification.
4225 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4226 When AFTER is nonzero, they are the start position,
4227 the position after the inserted new text,
4228 and the length of deleted or replaced old text. */
4230 void
4231 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4232 Lisp_Object start, end;
4233 int after;
4234 Lisp_Object arg1, arg2, arg3;
4236 Lisp_Object prop, overlay;
4237 struct Lisp_Overlay *tail;
4238 /* 1 if this change is an insertion. */
4239 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4240 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4242 overlay = Qnil;
4243 tail = NULL;
4245 /* We used to run the functions as soon as we found them and only register
4246 them in last_overlay_modification_hooks for the purpose of the `after'
4247 case. But running elisp code as we traverse the list of overlays is
4248 painful because the list can be modified by the elisp code so we had to
4249 copy at several places. We now simply do a read-only traversal that
4250 only collects the functions to run and we run them afterwards. It's
4251 simpler, especially since all the code was already there. -stef */
4253 if (!after)
4255 /* We are being called before a change.
4256 Scan the overlays to find the functions to call. */
4257 last_overlay_modification_hooks_used = 0;
4258 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4260 int startpos, endpos;
4261 Lisp_Object ostart, oend;
4263 XSETMISC (overlay, tail);
4265 ostart = OVERLAY_START (overlay);
4266 oend = OVERLAY_END (overlay);
4267 endpos = OVERLAY_POSITION (oend);
4268 if (XFASTINT (start) > endpos)
4269 break;
4270 startpos = OVERLAY_POSITION (ostart);
4271 if (insertion && (XFASTINT (start) == startpos
4272 || XFASTINT (end) == startpos))
4274 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4275 if (!NILP (prop))
4276 add_overlay_mod_hooklist (prop, overlay);
4278 if (insertion && (XFASTINT (start) == endpos
4279 || XFASTINT (end) == endpos))
4281 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4282 if (!NILP (prop))
4283 add_overlay_mod_hooklist (prop, overlay);
4285 /* Test for intersecting intervals. This does the right thing
4286 for both insertion and deletion. */
4287 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4289 prop = Foverlay_get (overlay, Qmodification_hooks);
4290 if (!NILP (prop))
4291 add_overlay_mod_hooklist (prop, overlay);
4295 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4297 int startpos, endpos;
4298 Lisp_Object ostart, oend;
4300 XSETMISC (overlay, tail);
4302 ostart = OVERLAY_START (overlay);
4303 oend = OVERLAY_END (overlay);
4304 startpos = OVERLAY_POSITION (ostart);
4305 endpos = OVERLAY_POSITION (oend);
4306 if (XFASTINT (end) < startpos)
4307 break;
4308 if (insertion && (XFASTINT (start) == startpos
4309 || XFASTINT (end) == startpos))
4311 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4312 if (!NILP (prop))
4313 add_overlay_mod_hooklist (prop, overlay);
4315 if (insertion && (XFASTINT (start) == endpos
4316 || XFASTINT (end) == endpos))
4318 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4319 if (!NILP (prop))
4320 add_overlay_mod_hooklist (prop, overlay);
4322 /* Test for intersecting intervals. This does the right thing
4323 for both insertion and deletion. */
4324 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4326 prop = Foverlay_get (overlay, Qmodification_hooks);
4327 if (!NILP (prop))
4328 add_overlay_mod_hooklist (prop, overlay);
4333 GCPRO4 (overlay, arg1, arg2, arg3);
4335 /* Call the functions recorded in last_overlay_modification_hooks.
4336 First copy the vector contents, in case some of these hooks
4337 do subsequent modification of the buffer. */
4338 int size = last_overlay_modification_hooks_used;
4339 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4340 int i;
4342 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4343 copy, size * sizeof (Lisp_Object));
4344 gcpro1.var = copy;
4345 gcpro1.nvars = size;
4347 for (i = 0; i < size;)
4349 Lisp_Object prop, overlay;
4350 prop = copy[i++];
4351 overlay = copy[i++];
4352 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4355 UNGCPRO;
4358 static void
4359 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4360 Lisp_Object list, overlay;
4361 int after;
4362 Lisp_Object arg1, arg2, arg3;
4364 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4366 GCPRO4 (list, arg1, arg2, arg3);
4368 while (CONSP (list))
4370 if (NILP (arg3))
4371 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4372 else
4373 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4374 list = XCDR (list);
4376 UNGCPRO;
4379 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4380 property is set. */
4381 void
4382 evaporate_overlays (pos)
4383 EMACS_INT pos;
4385 Lisp_Object overlay, hit_list;
4386 struct Lisp_Overlay *tail;
4388 hit_list = Qnil;
4389 if (pos <= current_buffer->overlay_center)
4390 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4392 int endpos;
4393 XSETMISC (overlay, tail);
4394 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4395 if (endpos < pos)
4396 break;
4397 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4398 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4399 hit_list = Fcons (overlay, hit_list);
4401 else
4402 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4404 int startpos;
4405 XSETMISC (overlay, tail);
4406 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4407 if (startpos > pos)
4408 break;
4409 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4410 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4411 hit_list = Fcons (overlay, hit_list);
4413 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4414 Fdelete_overlay (XCAR (hit_list));
4417 /* Somebody has tried to store a value with an unacceptable type
4418 in the slot with offset OFFSET. */
4420 void
4421 buffer_slot_type_mismatch (offset)
4422 int offset;
4424 Lisp_Object sym;
4425 char *type_name;
4427 switch (XINT (PER_BUFFER_TYPE (offset)))
4429 case Lisp_Int:
4430 type_name = "integers";
4431 break;
4433 case Lisp_String:
4434 type_name = "strings";
4435 break;
4437 case Lisp_Symbol:
4438 type_name = "symbols";
4439 break;
4441 default:
4442 abort ();
4445 sym = PER_BUFFER_SYMBOL (offset);
4446 error ("Only %s should be stored in the buffer-local variable %s",
4447 type_name, SDATA (SYMBOL_NAME (sym)));
4451 /***********************************************************************
4452 Allocation with mmap
4453 ***********************************************************************/
4455 #ifdef USE_MMAP_FOR_BUFFERS
4457 #include <sys/types.h>
4458 #include <sys/mman.h>
4460 #ifndef MAP_ANON
4461 #ifdef MAP_ANONYMOUS
4462 #define MAP_ANON MAP_ANONYMOUS
4463 #else
4464 #define MAP_ANON 0
4465 #endif
4466 #endif
4468 #ifndef MAP_FAILED
4469 #define MAP_FAILED ((void *) -1)
4470 #endif
4472 #include <stdio.h>
4473 #include <errno.h>
4475 #if MAP_ANON == 0
4476 #include <fcntl.h>
4477 #endif
4479 #include "coding.h"
4482 /* Memory is allocated in regions which are mapped using mmap(2).
4483 The current implementation lets the system select mapped
4484 addresses; we're not using MAP_FIXED in general, except when
4485 trying to enlarge regions.
4487 Each mapped region starts with a mmap_region structure, the user
4488 area starts after that structure, aligned to MEM_ALIGN.
4490 +-----------------------+
4491 | struct mmap_info + |
4492 | padding |
4493 +-----------------------+
4494 | user data |
4497 +-----------------------+ */
4499 struct mmap_region
4501 /* User-specified size. */
4502 size_t nbytes_specified;
4504 /* Number of bytes mapped */
4505 size_t nbytes_mapped;
4507 /* Pointer to the location holding the address of the memory
4508 allocated with the mmap'd block. The variable actually points
4509 after this structure. */
4510 POINTER_TYPE **var;
4512 /* Next and previous in list of all mmap'd regions. */
4513 struct mmap_region *next, *prev;
4516 /* Doubly-linked list of mmap'd regions. */
4518 static struct mmap_region *mmap_regions;
4520 /* File descriptor for mmap. If we don't have anonymous mapping,
4521 /dev/zero will be opened on it. */
4523 static int mmap_fd;
4525 /* Temporary storage for mmap_set_vars, see there. */
4527 static struct mmap_region *mmap_regions_1;
4528 static int mmap_fd_1;
4530 /* Page size on this system. */
4532 static int mmap_page_size;
4534 /* 1 means mmap has been intialized. */
4536 static int mmap_initialized_p;
4538 /* Value is X rounded up to the next multiple of N. */
4540 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4542 /* Size of mmap_region structure plus padding. */
4544 #define MMAP_REGION_STRUCT_SIZE \
4545 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4547 /* Given a pointer P to the start of the user-visible part of a mapped
4548 region, return a pointer to the start of the region. */
4550 #define MMAP_REGION(P) \
4551 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4553 /* Given a pointer P to the start of a mapped region, return a pointer
4554 to the start of the user-visible part of the region. */
4556 #define MMAP_USER_AREA(P) \
4557 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4559 #define MEM_ALIGN sizeof (double)
4561 /* Predicate returning true if part of the address range [START .. END]
4562 is currently mapped. Used to prevent overwriting an existing
4563 memory mapping.
4565 Default is to conservativly assume the address range is occupied by
4566 something else. This can be overridden by system configuration
4567 files if system-specific means to determine this exists. */
4569 #ifndef MMAP_ALLOCATED_P
4570 #define MMAP_ALLOCATED_P(start, end) 1
4571 #endif
4573 /* Function prototypes. */
4575 static int mmap_free_1 P_ ((struct mmap_region *));
4576 static int mmap_enlarge P_ ((struct mmap_region *, int));
4577 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4578 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4579 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4580 static void mmap_free P_ ((POINTER_TYPE **ptr));
4581 static void mmap_init P_ ((void));
4584 /* Return a region overlapping address range START...END, or null if
4585 none. END is not including, i.e. the last byte in the range
4586 is at END - 1. */
4588 static struct mmap_region *
4589 mmap_find (start, end)
4590 POINTER_TYPE *start, *end;
4592 struct mmap_region *r;
4593 char *s = (char *) start, *e = (char *) end;
4595 for (r = mmap_regions; r; r = r->next)
4597 char *rstart = (char *) r;
4598 char *rend = rstart + r->nbytes_mapped;
4600 if (/* First byte of range, i.e. START, in this region? */
4601 (s >= rstart && s < rend)
4602 /* Last byte of range, i.e. END - 1, in this region? */
4603 || (e > rstart && e <= rend)
4604 /* First byte of this region in the range? */
4605 || (rstart >= s && rstart < e)
4606 /* Last byte of this region in the range? */
4607 || (rend > s && rend <= e))
4608 break;
4611 return r;
4615 /* Unmap a region. P is a pointer to the start of the user-araa of
4616 the region. Value is non-zero if successful. */
4618 static int
4619 mmap_free_1 (r)
4620 struct mmap_region *r;
4622 if (r->next)
4623 r->next->prev = r->prev;
4624 if (r->prev)
4625 r->prev->next = r->next;
4626 else
4627 mmap_regions = r->next;
4629 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4631 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4632 return 0;
4635 return 1;
4639 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4640 Value is non-zero if successful. */
4642 static int
4643 mmap_enlarge (r, npages)
4644 struct mmap_region *r;
4645 int npages;
4647 char *region_end = (char *) r + r->nbytes_mapped;
4648 size_t nbytes;
4649 int success = 0;
4651 if (npages < 0)
4653 /* Unmap pages at the end of the region. */
4654 nbytes = - npages * mmap_page_size;
4655 if (munmap (region_end - nbytes, nbytes) == -1)
4656 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4657 else
4659 r->nbytes_mapped -= nbytes;
4660 success = 1;
4663 else if (npages > 0)
4665 nbytes = npages * mmap_page_size;
4667 /* Try to map additional pages at the end of the region. We
4668 cannot do this if the address range is already occupied by
4669 something else because mmap deletes any previous mapping.
4670 I'm not sure this is worth doing, let's see. */
4671 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4673 POINTER_TYPE *p;
4675 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4676 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4677 if (p == MAP_FAILED)
4678 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4679 else if (p != (POINTER_TYPE *) region_end)
4681 /* Kernels are free to choose a different address. In
4682 that case, unmap what we've mapped above; we have
4683 no use for it. */
4684 if (munmap (p, nbytes) == -1)
4685 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4687 else
4689 r->nbytes_mapped += nbytes;
4690 success = 1;
4695 return success;
4699 /* Set or reset variables holding references to mapped regions. If
4700 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4701 non-zero, set all variables to the start of the user-areas
4702 of mapped regions.
4704 This function is called from Fdump_emacs to ensure that the dumped
4705 Emacs doesn't contain references to memory that won't be mapped
4706 when Emacs starts. */
4708 void
4709 mmap_set_vars (restore_p)
4710 int restore_p;
4712 struct mmap_region *r;
4714 if (restore_p)
4716 mmap_regions = mmap_regions_1;
4717 mmap_fd = mmap_fd_1;
4718 for (r = mmap_regions; r; r = r->next)
4719 *r->var = MMAP_USER_AREA (r);
4721 else
4723 for (r = mmap_regions; r; r = r->next)
4724 *r->var = NULL;
4725 mmap_regions_1 = mmap_regions;
4726 mmap_regions = NULL;
4727 mmap_fd_1 = mmap_fd;
4728 mmap_fd = -1;
4733 /* Allocate a block of storage large enough to hold NBYTES bytes of
4734 data. A pointer to the data is returned in *VAR. VAR is thus the
4735 address of some variable which will use the data area.
4737 The allocation of 0 bytes is valid.
4739 If we can't allocate the necessary memory, set *VAR to null, and
4740 return null. */
4742 static POINTER_TYPE *
4743 mmap_alloc (var, nbytes)
4744 POINTER_TYPE **var;
4745 size_t nbytes;
4747 void *p;
4748 size_t map;
4750 mmap_init ();
4752 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4753 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4754 mmap_fd, 0);
4756 if (p == MAP_FAILED)
4758 if (errno != ENOMEM)
4759 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4760 p = NULL;
4762 else
4764 struct mmap_region *r = (struct mmap_region *) p;
4766 r->nbytes_specified = nbytes;
4767 r->nbytes_mapped = map;
4768 r->var = var;
4769 r->prev = NULL;
4770 r->next = mmap_regions;
4771 if (r->next)
4772 r->next->prev = r;
4773 mmap_regions = r;
4775 p = MMAP_USER_AREA (p);
4778 return *var = p;
4782 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4783 resize it to size NBYTES. Change *VAR to reflect the new block,
4784 and return this value. If more memory cannot be allocated, then
4785 leave *VAR unchanged, and return null. */
4787 static POINTER_TYPE *
4788 mmap_realloc (var, nbytes)
4789 POINTER_TYPE **var;
4790 size_t nbytes;
4792 POINTER_TYPE *result;
4794 mmap_init ();
4796 if (*var == NULL)
4797 result = mmap_alloc (var, nbytes);
4798 else if (nbytes == 0)
4800 mmap_free (var);
4801 result = mmap_alloc (var, nbytes);
4803 else
4805 struct mmap_region *r = MMAP_REGION (*var);
4806 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4808 if (room < nbytes)
4810 /* Must enlarge. */
4811 POINTER_TYPE *old_ptr = *var;
4813 /* Try to map additional pages at the end of the region.
4814 If that fails, allocate a new region, copy data
4815 from the old region, then free it. */
4816 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4817 / mmap_page_size)))
4819 r->nbytes_specified = nbytes;
4820 *var = result = old_ptr;
4822 else if (mmap_alloc (var, nbytes))
4824 bcopy (old_ptr, *var, r->nbytes_specified);
4825 mmap_free_1 (MMAP_REGION (old_ptr));
4826 result = *var;
4827 r = MMAP_REGION (result);
4828 r->nbytes_specified = nbytes;
4830 else
4832 *var = old_ptr;
4833 result = NULL;
4836 else if (room - nbytes >= mmap_page_size)
4838 /* Shrinking by at least a page. Let's give some
4839 memory back to the system.
4841 The extra parens are to make the division happens first,
4842 on positive values, so we know it will round towards
4843 zero. */
4844 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4845 result = *var;
4846 r->nbytes_specified = nbytes;
4848 else
4850 /* Leave it alone. */
4851 result = *var;
4852 r->nbytes_specified = nbytes;
4856 return result;
4860 /* Free a block of relocatable storage whose data is pointed to by
4861 PTR. Store 0 in *PTR to show there's no block allocated. */
4863 static void
4864 mmap_free (var)
4865 POINTER_TYPE **var;
4867 mmap_init ();
4869 if (*var)
4871 mmap_free_1 (MMAP_REGION (*var));
4872 *var = NULL;
4877 /* Perform necessary intializations for the use of mmap. */
4879 static void
4880 mmap_init ()
4882 #if MAP_ANON == 0
4883 /* The value of mmap_fd is initially 0 in temacs, and -1
4884 in a dumped Emacs. */
4885 if (mmap_fd <= 0)
4887 /* No anonymous mmap -- we need the file descriptor. */
4888 mmap_fd = open ("/dev/zero", O_RDONLY);
4889 if (mmap_fd == -1)
4890 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4892 #endif /* MAP_ANON == 0 */
4894 if (mmap_initialized_p)
4895 return;
4896 mmap_initialized_p = 1;
4898 #if MAP_ANON != 0
4899 mmap_fd = -1;
4900 #endif
4902 mmap_page_size = getpagesize ();
4905 #endif /* USE_MMAP_FOR_BUFFERS */
4909 /***********************************************************************
4910 Buffer-text Allocation
4911 ***********************************************************************/
4913 #ifdef REL_ALLOC
4914 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4915 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4916 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4917 #endif /* REL_ALLOC */
4920 /* Allocate NBYTES bytes for buffer B's text buffer. */
4922 static void
4923 alloc_buffer_text (b, nbytes)
4924 struct buffer *b;
4925 size_t nbytes;
4927 POINTER_TYPE *p;
4929 BLOCK_INPUT;
4930 #if defined USE_MMAP_FOR_BUFFERS
4931 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4932 #elif defined REL_ALLOC
4933 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4934 #else
4935 p = xmalloc (nbytes);
4936 #endif
4938 if (p == NULL)
4940 UNBLOCK_INPUT;
4941 memory_full ();
4944 b->text->beg = (unsigned char *) p;
4945 UNBLOCK_INPUT;
4948 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4949 shrink it. */
4951 void
4952 enlarge_buffer_text (b, delta)
4953 struct buffer *b;
4954 int delta;
4956 POINTER_TYPE *p;
4957 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4958 + delta);
4959 BLOCK_INPUT;
4960 #if defined USE_MMAP_FOR_BUFFERS
4961 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4962 #elif defined REL_ALLOC
4963 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4964 #else
4965 p = xrealloc (b->text->beg, nbytes);
4966 #endif
4968 if (p == NULL)
4970 UNBLOCK_INPUT;
4971 memory_full ();
4974 BUF_BEG_ADDR (b) = (unsigned char *) p;
4975 UNBLOCK_INPUT;
4979 /* Free buffer B's text buffer. */
4981 static void
4982 free_buffer_text (b)
4983 struct buffer *b;
4985 BLOCK_INPUT;
4987 #if defined USE_MMAP_FOR_BUFFERS
4988 mmap_free ((POINTER_TYPE **) &b->text->beg);
4989 #elif defined REL_ALLOC
4990 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4991 #else
4992 xfree (b->text->beg);
4993 #endif
4995 BUF_BEG_ADDR (b) = NULL;
4996 UNBLOCK_INPUT;
5001 /***********************************************************************
5002 Initialization
5003 ***********************************************************************/
5005 void
5006 init_buffer_once ()
5008 int idx;
5010 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5012 /* Make sure all markable slots in buffer_defaults
5013 are initialized reasonably, so mark_buffer won't choke. */
5014 reset_buffer (&buffer_defaults);
5015 reset_buffer_local_variables (&buffer_defaults, 1);
5016 reset_buffer (&buffer_local_symbols);
5017 reset_buffer_local_variables (&buffer_local_symbols, 1);
5018 /* Prevent GC from getting confused. */
5019 buffer_defaults.text = &buffer_defaults.own_text;
5020 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5021 BUF_INTERVALS (&buffer_defaults) = 0;
5022 BUF_INTERVALS (&buffer_local_symbols) = 0;
5023 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5024 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5025 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5026 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5028 /* Set up the default values of various buffer slots. */
5029 /* Must do these before making the first buffer! */
5031 /* real setup is done in bindings.el */
5032 buffer_defaults.mode_line_format = build_string ("%-");
5033 buffer_defaults.header_line_format = Qnil;
5034 buffer_defaults.abbrev_mode = Qnil;
5035 buffer_defaults.overwrite_mode = Qnil;
5036 buffer_defaults.case_fold_search = Qt;
5037 buffer_defaults.auto_fill_function = Qnil;
5038 buffer_defaults.selective_display = Qnil;
5039 #ifndef old
5040 buffer_defaults.selective_display_ellipses = Qt;
5041 #endif
5042 buffer_defaults.abbrev_table = Qnil;
5043 buffer_defaults.display_table = Qnil;
5044 buffer_defaults.undo_list = Qnil;
5045 buffer_defaults.mark_active = Qnil;
5046 buffer_defaults.file_format = Qnil;
5047 buffer_defaults.auto_save_file_format = Qt;
5048 buffer_defaults.overlays_before = NULL;
5049 buffer_defaults.overlays_after = NULL;
5050 buffer_defaults.overlay_center = BEG;
5052 XSETFASTINT (buffer_defaults.tab_width, 8);
5053 buffer_defaults.truncate_lines = Qnil;
5054 buffer_defaults.ctl_arrow = Qt;
5055 buffer_defaults.direction_reversed = Qnil;
5056 buffer_defaults.cursor_type = Qt;
5057 buffer_defaults.extra_line_spacing = Qnil;
5058 buffer_defaults.cursor_in_non_selected_windows = Qt;
5060 #ifdef DOS_NT
5061 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5062 #endif
5063 buffer_defaults.enable_multibyte_characters = Qt;
5064 buffer_defaults.buffer_file_coding_system = Qnil;
5065 XSETFASTINT (buffer_defaults.fill_column, 70);
5066 XSETFASTINT (buffer_defaults.left_margin, 0);
5067 buffer_defaults.cache_long_line_scans = Qnil;
5068 buffer_defaults.file_truename = Qnil;
5069 XSETFASTINT (buffer_defaults.display_count, 0);
5070 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5071 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5072 buffer_defaults.left_fringe_width = Qnil;
5073 buffer_defaults.right_fringe_width = Qnil;
5074 buffer_defaults.fringes_outside_margins = Qnil;
5075 buffer_defaults.scroll_bar_width = Qnil;
5076 buffer_defaults.vertical_scroll_bar_type = Qt;
5077 buffer_defaults.indicate_empty_lines = Qnil;
5078 buffer_defaults.indicate_buffer_boundaries = Qnil;
5079 buffer_defaults.fringe_indicator_alist = Qnil;
5080 buffer_defaults.fringe_cursor_alist = Qnil;
5081 buffer_defaults.scroll_up_aggressively = Qnil;
5082 buffer_defaults.scroll_down_aggressively = Qnil;
5083 buffer_defaults.display_time = Qnil;
5085 /* Assign the local-flags to the slots that have default values.
5086 The local flag is a bit that is used in the buffer
5087 to say that it has its own local value for the slot.
5088 The local flag bits are in the local_var_flags slot of the buffer. */
5090 /* Nothing can work if this isn't true */
5091 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5093 /* 0 means not a lisp var, -1 means always local, else mask */
5094 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5095 XSETINT (buffer_local_flags.filename, -1);
5096 XSETINT (buffer_local_flags.directory, -1);
5097 XSETINT (buffer_local_flags.backed_up, -1);
5098 XSETINT (buffer_local_flags.save_length, -1);
5099 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5100 XSETINT (buffer_local_flags.read_only, -1);
5101 XSETINT (buffer_local_flags.major_mode, -1);
5102 XSETINT (buffer_local_flags.mode_name, -1);
5103 XSETINT (buffer_local_flags.undo_list, -1);
5104 XSETINT (buffer_local_flags.mark_active, -1);
5105 XSETINT (buffer_local_flags.point_before_scroll, -1);
5106 XSETINT (buffer_local_flags.file_truename, -1);
5107 XSETINT (buffer_local_flags.invisibility_spec, -1);
5108 XSETINT (buffer_local_flags.file_format, -1);
5109 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5110 XSETINT (buffer_local_flags.display_count, -1);
5111 XSETINT (buffer_local_flags.display_time, -1);
5112 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5114 idx = 1;
5115 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5116 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5117 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5118 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5119 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5120 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5121 #ifndef old
5122 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5123 #endif
5124 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5125 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5126 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5127 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5128 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5129 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5131 #ifdef DOS_NT
5132 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5133 /* Make this one a permanent local. */
5134 buffer_permanent_local_flags[idx++] = 1;
5135 #endif
5136 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5137 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5138 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5139 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5140 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5141 /* Make this one a permanent local. */
5142 buffer_permanent_local_flags[idx++] = 1;
5143 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5149 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5150 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5151 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5152 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5154 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5161 /* Need more room? */
5162 if (idx >= MAX_PER_BUFFER_VARS)
5163 abort ();
5164 last_per_buffer_idx = idx;
5166 Vbuffer_alist = Qnil;
5167 current_buffer = 0;
5168 all_buffers = 0;
5170 QSFundamental = build_string ("Fundamental");
5172 Qfundamental_mode = intern ("fundamental-mode");
5173 buffer_defaults.major_mode = Qfundamental_mode;
5175 Qmode_class = intern ("mode-class");
5177 Qprotected_field = intern ("protected-field");
5179 Qpermanent_local = intern ("permanent-local");
5181 Qkill_buffer_hook = intern ("kill-buffer-hook");
5182 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5184 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5186 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5188 /* super-magic invisible buffer */
5189 Vbuffer_alist = Qnil;
5191 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5193 inhibit_modification_hooks = 0;
5196 void
5197 init_buffer ()
5199 char *pwd;
5200 Lisp_Object temp;
5201 int len;
5203 #ifdef USE_MMAP_FOR_BUFFERS
5205 /* When using the ralloc implementation based on mmap(2), buffer
5206 text pointers will have been set to null in the dumped Emacs.
5207 Map new memory. */
5208 struct buffer *b;
5210 for (b = all_buffers; b; b = b->next)
5211 if (b->text->beg == NULL)
5212 enlarge_buffer_text (b, 0);
5214 #endif /* USE_MMAP_FOR_BUFFERS */
5216 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5217 if (NILP (buffer_defaults.enable_multibyte_characters))
5218 Fset_buffer_multibyte (Qnil);
5220 pwd = get_current_dir_name ();
5222 if (!pwd)
5223 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5225 #ifndef VMS
5226 /* Maybe this should really use some standard subroutine
5227 whose definition is filename syntax dependent. */
5228 len = strlen (pwd);
5229 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5231 /* Grow buffer to add directory separator and '\0'. */
5232 pwd = (char *) xrealloc (pwd, len + 2);
5233 pwd[len] = DIRECTORY_SEP;
5234 pwd[len + 1] = '\0';
5236 #endif /* not VMS */
5238 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5239 if (! NILP (buffer_defaults.enable_multibyte_characters))
5240 /* At this moment, we still don't know how to decode the
5241 directory name. So, we keep the bytes in multibyte form so
5242 that ENCODE_FILE correctly gets the original bytes. */
5243 current_buffer->directory
5244 = string_to_multibyte (current_buffer->directory);
5246 /* Add /: to the front of the name
5247 if it would otherwise be treated as magic. */
5248 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5249 if (! NILP (temp)
5250 /* If the default dir is just /, TEMP is non-nil
5251 because of the ange-ftp completion handler.
5252 However, it is not necessary to turn / into /:/.
5253 So avoid doing that. */
5254 && strcmp ("/", SDATA (current_buffer->directory)))
5255 current_buffer->directory
5256 = concat2 (build_string ("/:"), current_buffer->directory);
5258 temp = get_minibuffer (0);
5259 XBUFFER (temp)->directory = current_buffer->directory;
5261 free (pwd);
5264 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5265 Object stored in the current buffer. address is the address of the slot
5266 in the buffer that is current now. */
5268 /* TYPE is nil for a general Lisp variable.
5269 An integer specifies a type; then only LIsp values
5270 with that type code are allowed (except that nil is allowed too).
5271 LNAME is the LIsp-level variable name.
5272 VNAME is the name of the buffer slot.
5273 DOC is a dummy where you write the doc string as a comment. */
5274 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5275 defvar_per_buffer (lname, vname, type, 0)
5277 static void
5278 defvar_per_buffer (namestring, address, type, doc)
5279 char *namestring;
5280 Lisp_Object *address;
5281 Lisp_Object type;
5282 char *doc;
5284 Lisp_Object sym, val;
5285 int offset;
5287 sym = intern (namestring);
5288 val = allocate_misc ();
5289 offset = (char *)address - (char *)current_buffer;
5291 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5292 XBUFFER_OBJFWD (val)->offset = offset;
5293 SET_SYMBOL_VALUE (sym, val);
5294 PER_BUFFER_SYMBOL (offset) = sym;
5295 PER_BUFFER_TYPE (offset) = type;
5297 if (PER_BUFFER_IDX (offset) == 0)
5298 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5299 slot of buffer_local_flags */
5300 abort ();
5304 /* initialize the buffer routines */
5305 void
5306 syms_of_buffer ()
5308 staticpro (&last_overlay_modification_hooks);
5309 last_overlay_modification_hooks
5310 = Fmake_vector (make_number (10), Qnil);
5312 staticpro (&Vbuffer_defaults);
5313 staticpro (&Vbuffer_local_symbols);
5314 staticpro (&Qfundamental_mode);
5315 staticpro (&Qmode_class);
5316 staticpro (&QSFundamental);
5317 staticpro (&Vbuffer_alist);
5318 staticpro (&Qprotected_field);
5319 staticpro (&Qpermanent_local);
5320 staticpro (&Qkill_buffer_hook);
5321 Qoverlayp = intern ("overlayp");
5322 staticpro (&Qoverlayp);
5323 Qevaporate = intern ("evaporate");
5324 staticpro (&Qevaporate);
5325 Qmodification_hooks = intern ("modification-hooks");
5326 staticpro (&Qmodification_hooks);
5327 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5328 staticpro (&Qinsert_in_front_hooks);
5329 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5330 staticpro (&Qinsert_behind_hooks);
5331 Qget_file_buffer = intern ("get-file-buffer");
5332 staticpro (&Qget_file_buffer);
5333 Qpriority = intern ("priority");
5334 staticpro (&Qpriority);
5335 Qwindow = intern ("window");
5336 staticpro (&Qwindow);
5337 Qbefore_string = intern ("before-string");
5338 staticpro (&Qbefore_string);
5339 Qafter_string = intern ("after-string");
5340 staticpro (&Qafter_string);
5341 Qfirst_change_hook = intern ("first-change-hook");
5342 staticpro (&Qfirst_change_hook);
5343 Qbefore_change_functions = intern ("before-change-functions");
5344 staticpro (&Qbefore_change_functions);
5345 Qafter_change_functions = intern ("after-change-functions");
5346 staticpro (&Qafter_change_functions);
5347 staticpro (&Qucs_set_table_for_input);
5349 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5350 staticpro (&Qkill_buffer_query_functions);
5352 Fput (Qprotected_field, Qerror_conditions,
5353 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5354 Fput (Qprotected_field, Qerror_message,
5355 build_string ("Attempt to modify a protected field"));
5357 /* All these use DEFVAR_LISP_NOPRO because the slots in
5358 buffer_defaults will all be marked via Vbuffer_defaults. */
5360 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5361 &buffer_defaults.mode_line_format,
5362 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5363 This is the same as (default-value 'mode-line-format). */);
5365 DEFVAR_LISP_NOPRO ("default-header-line-format",
5366 &buffer_defaults.header_line_format,
5367 doc: /* Default value of `header-line-format' for buffers that don't override it.
5368 This is the same as (default-value 'header-line-format). */);
5370 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5371 doc: /* Default value of `cursor-type' for buffers that don't override it.
5372 This is the same as (default-value 'cursor-type). */);
5374 DEFVAR_LISP_NOPRO ("default-line-spacing",
5375 &buffer_defaults.extra_line_spacing,
5376 doc: /* Default value of `line-spacing' for buffers that don't override it.
5377 This is the same as (default-value 'line-spacing). */);
5379 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5380 &buffer_defaults.cursor_in_non_selected_windows,
5381 doc: /* Default value of `cursor-in-non-selected-windows'.
5382 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5384 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5385 &buffer_defaults.abbrev_mode,
5386 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5387 This is the same as (default-value 'abbrev-mode). */);
5389 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5390 &buffer_defaults.ctl_arrow,
5391 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5392 This is the same as (default-value 'ctl-arrow). */);
5394 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5395 &buffer_defaults.direction_reversed,
5396 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5397 This is the same as (default-value 'direction-reversed). */);
5399 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5400 &buffer_defaults.enable_multibyte_characters,
5401 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5402 This is the same as (default-value 'enable-multibyte-characters). */);
5404 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5405 &buffer_defaults.buffer_file_coding_system,
5406 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5407 This is the same as (default-value 'buffer-file-coding-system). */);
5409 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5410 &buffer_defaults.truncate_lines,
5411 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5412 This is the same as (default-value 'truncate-lines). */);
5414 DEFVAR_LISP_NOPRO ("default-fill-column",
5415 &buffer_defaults.fill_column,
5416 doc: /* Default value of `fill-column' for buffers that do not override it.
5417 This is the same as (default-value 'fill-column). */);
5419 DEFVAR_LISP_NOPRO ("default-left-margin",
5420 &buffer_defaults.left_margin,
5421 doc: /* Default value of `left-margin' for buffers that do not override it.
5422 This is the same as (default-value 'left-margin). */);
5424 DEFVAR_LISP_NOPRO ("default-tab-width",
5425 &buffer_defaults.tab_width,
5426 doc: /* Default value of `tab-width' for buffers that do not override it.
5427 This is the same as (default-value 'tab-width). */);
5429 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5430 &buffer_defaults.case_fold_search,
5431 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5432 This is the same as (default-value 'case-fold-search). */);
5434 #ifdef DOS_NT
5435 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5436 &buffer_defaults.buffer_file_type,
5437 doc: /* Default file type for buffers that do not override it.
5438 This is the same as (default-value 'buffer-file-type).
5439 The file type is nil for text, t for binary. */);
5440 #endif
5442 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5443 &buffer_defaults.left_margin_cols,
5444 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5445 This is the same as (default-value 'left-margin-width). */);
5447 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5448 &buffer_defaults.right_margin_cols,
5449 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5450 This is the same as (default-value 'right-margin-width). */);
5452 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5453 &buffer_defaults.left_fringe_width,
5454 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5455 This is the same as (default-value 'left-fringe-width). */);
5457 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5458 &buffer_defaults.right_fringe_width,
5459 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5460 This is the same as (default-value 'right-fringe-width). */);
5462 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5463 &buffer_defaults.fringes_outside_margins,
5464 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5465 This is the same as (default-value 'fringes-outside-margins). */);
5467 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5468 &buffer_defaults.scroll_bar_width,
5469 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5470 This is the same as (default-value 'scroll-bar-width). */);
5472 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5473 &buffer_defaults.vertical_scroll_bar_type,
5474 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5475 This is the same as (default-value 'vertical-scroll-bar). */);
5477 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5478 &buffer_defaults.indicate_empty_lines,
5479 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5480 This is the same as (default-value 'indicate-empty-lines). */);
5482 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5483 &buffer_defaults.indicate_buffer_boundaries,
5484 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5485 This is the same as (default-value 'indicate-buffer-boundaries). */);
5487 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5488 &buffer_defaults.fringe_indicator_alist,
5489 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5490 This is the same as (default-value 'fringe-indicator-alist'). */);
5492 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5493 &buffer_defaults.fringe_cursor_alist,
5494 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5495 This is the same as (default-value 'fringe-cursor-alist'). */);
5497 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5498 &buffer_defaults.scroll_up_aggressively,
5499 doc: /* Default value of `scroll-up-aggressively'.
5500 This value applies in buffers that don't have their own local values.
5501 This is the same as (default-value 'scroll-up-aggressively). */);
5503 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5504 &buffer_defaults.scroll_down_aggressively,
5505 doc: /* Default value of `scroll-down-aggressively'.
5506 This value applies in buffers that don't have their own local values.
5507 This is the same as (default-value 'scroll-down-aggressively). */);
5509 DEFVAR_PER_BUFFER ("header-line-format",
5510 &current_buffer->header_line_format,
5511 Qnil,
5512 doc: /* Analogous to `mode-line-format', but controls the header line.
5513 The header line appears, optionally, at the top of a window;
5514 the mode line appears at the bottom. */);
5516 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5517 Qnil,
5518 doc: /* Template for displaying mode line for current buffer.
5519 Each buffer has its own value of this variable.
5520 Value may be nil, a string, a symbol or a list or cons cell.
5521 A value of nil means don't display a mode line.
5522 For a symbol, its value is used (but it is ignored if t or nil).
5523 A string appearing directly as the value of a symbol is processed verbatim
5524 in that the %-constructs below are not recognized.
5525 Note that unless the symbol is marked as a `risky-local-variable', all
5526 properties in any strings, as well as all :eval and :propertize forms
5527 in the value of that symbol will be ignored.
5528 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5529 is used as a mode line element. Be careful--FORM should not load any files,
5530 because that can cause an infinite recursion.
5531 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5532 with the specified properties PROPS applied.
5533 For a list whose car is a symbol, the symbol's value is taken,
5534 and if that is non-nil, the cadr of the list is processed recursively.
5535 Otherwise, the caddr of the list (if there is one) is processed.
5536 For a list whose car is a string or list, each element is processed
5537 recursively and the results are effectively concatenated.
5538 For a list whose car is an integer, the cdr of the list is processed
5539 and padded (if the number is positive) or truncated (if negative)
5540 to the width specified by that number.
5541 A string is printed verbatim in the mode line except for %-constructs:
5542 (%-constructs are allowed when the string is the entire mode-line-format
5543 or when it is found in a cons-cell or a list)
5544 %b -- print buffer name. %f -- print visited file name.
5545 %F -- print frame name.
5546 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5547 %& is like %*, but ignore read-only-ness.
5548 % means buffer is read-only and * means it is modified.
5549 For a modified read-only buffer, %* gives % and %+ gives *.
5550 %s -- print process status. %l -- print the current line number.
5551 %c -- print the current column number (this makes editing slower).
5552 To make the column number update correctly in all cases,
5553 `column-number-mode' must be non-nil.
5554 %i -- print the size of the buffer.
5555 %I -- like %i, but use k, M, G, etc., to abbreviate.
5556 %p -- print percent of buffer above top of window, or Top, Bot or All.
5557 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5558 or print Bottom or All.
5559 %n -- print Narrow if appropriate.
5560 %t -- visited file is text or binary (if OS supports this distinction).
5561 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5562 %Z -- like %z, but including the end-of-line format.
5563 %e -- print error message about full memory.
5564 %@ -- print @ or hyphen. @ means that default-directory is on a
5565 remote machine.
5566 %[ -- print one [ for each recursive editing level. %] similar.
5567 %% -- print %. %- -- print infinitely many dashes.
5568 Decimal digits after the % specify field width to which to pad. */);
5570 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5571 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5572 A value of nil means use current buffer's major mode,
5573 provided it is not marked as "special".
5575 When a mode is used by default, `find-file' switches to it
5576 before it reads the contents into the buffer and before
5577 it finishes setting up the buffer. Thus, the mode and
5578 its hooks should not expect certain variables such as
5579 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5581 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5582 make_number (Lisp_Symbol),
5583 doc: /* Symbol for current buffer's major mode. */);
5585 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5586 Qnil,
5587 doc: /* Pretty name of current buffer's major mode (a string). */);
5589 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5590 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5592 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5593 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5595 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5596 Qnil,
5597 doc: /* *Non-nil if searches and matches should ignore case. */);
5599 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5600 make_number (Lisp_Int),
5601 doc: /* *Column beyond which automatic line-wrapping should happen.
5602 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5604 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5605 make_number (Lisp_Int),
5606 doc: /* *Column for the default `indent-line-function' to indent to.
5607 Linefeed indents to this column in Fundamental mode. */);
5609 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5610 make_number (Lisp_Int),
5611 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5613 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5614 doc: /* *Non-nil means display control chars with uparrow.
5615 A value of nil means use backslash and octal digits.
5616 This variable does not apply to characters whose display is specified
5617 in the current display table (if there is one). */);
5619 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5620 &current_buffer->enable_multibyte_characters,
5621 Qnil,
5622 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5623 Otherwise they are regarded as unibyte. This affects the display,
5624 file I/O and the behavior of various editing commands.
5626 This variable is buffer-local but you cannot set it directly;
5627 use the function `set-buffer-multibyte' to change a buffer's representation.
5628 Changing its default value with `setq-default' is supported.
5629 See also variable `default-enable-multibyte-characters' and Info node
5630 `(elisp)Text Representations'. */);
5631 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5633 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5634 &current_buffer->buffer_file_coding_system, Qnil,
5635 doc: /* Coding system to be used for encoding the buffer contents on saving.
5636 This variable applies to saving the buffer, and also to `write-region'
5637 and other functions that use `write-region'.
5638 It does not apply to sending output to subprocesses, however.
5640 If this is nil, the buffer is saved without any code conversion
5641 unless some coding system is specified in `file-coding-system-alist'
5642 for the buffer file.
5644 If the text to be saved cannot be encoded as specified by this variable,
5645 an alternative encoding is selected by `select-safe-coding-system', which see.
5647 The variable `coding-system-for-write', if non-nil, overrides this variable.
5649 This variable is never applied to a way of decoding a file while reading it. */);
5651 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5652 Qnil,
5653 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5655 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5656 doc: /* *Non-nil means do not display continuation lines.
5657 Instead, give each line of text just one screen line.
5659 Note that this is overridden by the variable
5660 `truncate-partial-width-windows' if that variable is non-nil
5661 and this buffer is not full-frame width. */);
5663 #ifdef DOS_NT
5664 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5665 Qnil,
5666 doc: /* Non-nil if the visited file is a binary file.
5667 This variable is meaningful on MS-DOG and Windows NT.
5668 On those systems, it is automatically local in every buffer.
5669 On other systems, this variable is normally always nil. */);
5670 #endif
5672 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5673 make_number (Lisp_String),
5674 doc: /* Name of default directory of current buffer. Should end with slash.
5675 To interactively change the default directory, use command `cd'. */);
5677 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5678 Qnil,
5679 doc: /* Function called (if non-nil) to perform auto-fill.
5680 It is called after self-inserting any character specified in
5681 the `auto-fill-chars' table.
5682 NOTE: This variable is not a hook;
5683 its value may not be a list of functions. */);
5685 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5686 make_number (Lisp_String),
5687 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5689 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5690 make_number (Lisp_String),
5691 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5692 The truename of a file is calculated by `file-truename'
5693 and then abbreviated with `abbreviate-file-name'. */);
5695 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5696 &current_buffer->auto_save_file_name,
5697 make_number (Lisp_String),
5698 doc: /* Name of file for auto-saving current buffer.
5699 If it is nil, that means don't auto-save this buffer. */);
5701 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5702 doc: /* Non-nil if this buffer is read-only. */);
5704 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5705 doc: /* Non-nil if this buffer's file has been backed up.
5706 Backing up is done before the first time the file is saved. */);
5708 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5709 make_number (Lisp_Int),
5710 doc: /* Length of current buffer when last read in, saved or auto-saved.
5711 0 initially. */);
5713 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5714 Qnil,
5715 doc: /* Non-nil enables selective display.
5716 An integer N as value means display only lines
5717 that start with less than N columns of space.
5718 A value of t means that the character ^M makes itself and
5719 all the rest of the line invisible; also, when saving the buffer
5720 in a file, save the ^M as a newline. */);
5722 #ifndef old
5723 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5724 &current_buffer->selective_display_ellipses,
5725 Qnil,
5726 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5727 #endif
5729 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5730 doc: /* Non-nil if self-insertion should replace existing text.
5731 The value should be one of `overwrite-mode-textual',
5732 `overwrite-mode-binary', or nil.
5733 If it is `overwrite-mode-textual', self-insertion still
5734 inserts at the end of a line, and inserts when point is before a tab,
5735 until the tab is filled in.
5736 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5738 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5739 Qnil,
5740 doc: /* Display table that controls display of the contents of current buffer.
5742 If this variable is nil, the value of `standard-display-table' is used.
5743 Each window can have its own, overriding display table, see
5744 `set-window-display-table' and `window-display-table'.
5746 The display table is a char-table created with `make-display-table'.
5747 A char-table is an array indexed by character codes. Normal array
5748 primitives `aref' and `aset' can be used to access elements of a char-table.
5750 Each of the char-table elements control how to display the corresponding
5751 text character: the element at index C in the table says how to display
5752 the character whose code is C. Each element should be a vector of
5753 characters or nil. The value nil means display the character in the
5754 default fashion; otherwise, the characters from the vector are delivered
5755 to the screen instead of the original character.
5757 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5758 to display a capital Y instead of each X character.
5760 In addition, a char-table has six extra slots to control the display of:
5762 the end of a truncated screen line (extra-slot 0, a single character);
5763 the end of a continued line (extra-slot 1, a single character);
5764 the escape character used to display character codes in octal
5765 (extra-slot 2, a single character);
5766 the character used as an arrow for control characters (extra-slot 3,
5767 a single character);
5768 the decoration indicating the presence of invisible lines (extra-slot 4,
5769 a vector of characters);
5770 the character used to draw the border between side-by-side windows
5771 (extra-slot 5, a single character).
5773 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5775 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5776 Qnil,
5777 doc: /* *Width of left marginal area for display of a buffer.
5778 A value of nil means no marginal area. */);
5780 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5781 Qnil,
5782 doc: /* *Width of right marginal area for display of a buffer.
5783 A value of nil means no marginal area. */);
5785 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5786 Qnil,
5787 doc: /* *Width of this buffer's left fringe (in pixels).
5788 A value of 0 means no left fringe is shown in this buffer's window.
5789 A value of nil means to use the left fringe width from the window's frame. */);
5791 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5792 Qnil,
5793 doc: /* *Width of this buffer's right fringe (in pixels).
5794 A value of 0 means no right fringe is shown in this buffer's window.
5795 A value of nil means to use the right fringe width from the window's frame. */);
5797 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5798 Qnil,
5799 doc: /* *Non-nil means to display fringes outside display margins.
5800 A value of nil means to display fringes between margins and buffer text. */);
5802 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5803 Qnil,
5804 doc: /* *Width of this buffer's scroll bars in pixels.
5805 A value of nil means to use the scroll bar width from the window's frame. */);
5807 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5808 Qnil,
5809 doc: /* *Position of this buffer's vertical scroll bar.
5810 The value takes effect whenever you tell a window to display this buffer;
5811 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5813 A value of `left' or `right' means put the vertical scroll bar at that side
5814 of the window; a value of nil means don't show any vertical scroll bars.
5815 A value of t (the default) means do whatever the window's frame specifies. */);
5817 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5818 &current_buffer->indicate_empty_lines, Qnil,
5819 doc: /* *Visually indicate empty lines after the buffer end.
5820 If non-nil, a bitmap is displayed in the left fringe of a window on
5821 window-systems. */);
5823 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5824 &current_buffer->indicate_buffer_boundaries, Qnil,
5825 doc: /* *Visually indicate buffer boundaries and scrolling.
5826 If non-nil, the first and last line of the buffer are marked in the fringe
5827 of a window on window-systems with angle bitmaps, or if the window can be
5828 scrolled, the top and bottom line of the window are marked with up and down
5829 arrow bitmaps.
5831 If value is a symbol `left' or `right', both angle and arrow bitmaps
5832 are displayed in the left or right fringe, resp. Any other value
5833 that doesn't look like an alist means display the angle bitmaps in
5834 the left fringe but no arrows.
5836 You can exercise more precise control by using an alist as the
5837 value. Each alist element (INDICATOR . POSITION) specifies
5838 where to show one of the indicators. INDICATOR is one of `top',
5839 `bottom', `up', `down', or t, which specifies the default position,
5840 and POSITION is one of `left', `right', or nil, meaning do not show
5841 this indicator.
5843 For example, ((top . left) (t . right)) places the top angle bitmap in
5844 left fringe, the bottom angle bitmap in right fringe, and both arrow
5845 bitmaps in right fringe. To show just the angle bitmaps in the left
5846 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5848 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5849 &current_buffer->fringe_indicator_alist, Qnil,
5850 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5851 The value is an alist where each element (INDICATOR . BITMAPS)
5852 specifies the fringe bitmaps used to display a specific logical
5853 fringe indicator.
5855 INDICATOR specifies the logical indicator type which is one of the
5856 following symbols: `truncation' , `continuation', `overlay-arrow',
5857 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5859 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5860 the actual bitmap shown in the left or right fringe for the logical
5861 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5862 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5863 are used only for the `bottom' and `one-line' indicators when the last
5864 \(only) line in has no final newline. BITMAPS may also be a single
5865 symbol which is used in both left and right fringes. */);
5867 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5868 &current_buffer->fringe_cursor_alist, Qnil,
5869 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5870 The value is an alist where each element (CURSOR . BITMAP)
5871 specifies the fringe bitmaps used to display a specific logical
5872 cursor type in the fringe.
5874 CURSOR specifies the logical cursor type which is one of the following
5875 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5876 one is used to show a hollow cursor on narrow lines display lines
5877 where the normal hollow cursor will not fit.
5879 BITMAP is the corresponding fringe bitmap shown for the logical
5880 cursor type. */);
5882 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5883 &current_buffer->scroll_up_aggressively, Qnil,
5884 doc: /* How far to scroll windows upward.
5885 If you move point off the bottom, the window scrolls automatically.
5886 This variable controls how far it scrolls. The value nil, the default,
5887 means scroll to center point. A fraction means scroll to put point
5888 that fraction of the window's height from the bottom of the window.
5889 When the value is 0.0, point goes at the bottom line, which in the simple
5890 case that you moved off with C-f means scrolling just one line. 1.0 means
5891 point goes at the top, so that in that simple case, the window
5892 scrolls by a full window height. Meaningful values are
5893 between 0.0 and 1.0, inclusive. */);
5895 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5896 &current_buffer->scroll_down_aggressively, Qnil,
5897 doc: /* How far to scroll windows downward.
5898 If you move point off the top, the window scrolls automatically.
5899 This variable controls how far it scrolls. The value nil, the default,
5900 means scroll to center point. A fraction means scroll to put point
5901 that fraction of the window's height from the top of the window.
5902 When the value is 0.0, point goes at the top line, which in the simple
5903 case that you moved off with C-b means scrolling just one line. 1.0 means
5904 point goes at the bottom, so that in that simple case, the window
5905 scrolls by a full window height. Meaningful values are
5906 between 0.0 and 1.0, inclusive. */);
5908 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5909 "Don't ask.");
5912 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5913 doc: /* List of functions to call before each text change.
5914 Two arguments are passed to each function: the positions of
5915 the beginning and end of the range of old text to be changed.
5916 \(For an insertion, the beginning and end are at the same place.)
5917 No information is given about the length of the text after the change.
5919 Buffer changes made while executing the `before-change-functions'
5920 don't call any before-change or after-change functions.
5921 That's because these variables are temporarily set to nil.
5922 As a result, a hook function cannot straightforwardly alter the value of
5923 these variables. See the Emacs Lisp manual for a way of
5924 accomplishing an equivalent result by using other variables.
5926 If an unhandled error happens in running these functions,
5927 the variable's value remains nil. That prevents the error
5928 from happening repeatedly and making Emacs nonfunctional. */);
5929 Vbefore_change_functions = Qnil;
5931 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5932 doc: /* List of functions to call after each text change.
5933 Three arguments are passed to each function: the positions of
5934 the beginning and end of the range of changed text,
5935 and the length in bytes of the pre-change text replaced by that range.
5936 \(For an insertion, the pre-change length is zero;
5937 for a deletion, that length is the number of bytes deleted,
5938 and the post-change beginning and end are at the same place.)
5940 Buffer changes made while executing the `after-change-functions'
5941 don't call any before-change or after-change functions.
5942 That's because these variables are temporarily set to nil.
5943 As a result, a hook function cannot straightforwardly alter the value of
5944 these variables. See the Emacs Lisp manual for a way of
5945 accomplishing an equivalent result by using other variables.
5947 If an unhandled error happens in running these functions,
5948 the variable's value remains nil. That prevents the error
5949 from happening repeatedly and making Emacs nonfunctional. */);
5950 Vafter_change_functions = Qnil;
5952 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5953 doc: /* A list of functions to call before changing a buffer which is unmodified.
5954 The functions are run using the `run-hooks' function. */);
5955 Vfirst_change_hook = Qnil;
5957 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5958 doc: /* List of undo entries in current buffer.
5959 Recent changes come first; older changes follow newer.
5961 An entry (BEG . END) represents an insertion which begins at
5962 position BEG and ends at position END.
5964 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5965 from (abs POSITION). If POSITION is positive, point was at the front
5966 of the text being deleted; if negative, point was at the end.
5968 An entry (t HIGH . LOW) indicates that the buffer previously had
5969 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5970 of the visited file's modification time, as of that time. If the
5971 modification time of the most recent save is different, this entry is
5972 obsolete.
5974 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5975 was modified between BEG and END. PROPERTY is the property name,
5976 and VALUE is the old value.
5978 An entry (apply FUN-NAME . ARGS) means undo the change with
5979 \(apply FUN-NAME ARGS).
5981 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5982 in the active region. BEG and END is the range affected by this entry
5983 and DELTA is the number of bytes added or deleted in that range by
5984 this change.
5986 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5987 was adjusted in position by the offset DISTANCE (an integer).
5989 An entry of the form POSITION indicates that point was at the buffer
5990 location given by the integer. Undoing an entry of this form places
5991 point at POSITION.
5993 Entries with value `nil' mark undo boundaries. The undo command treats
5994 the changes between two undo boundaries as a single step to be undone.
5996 If the value of the variable is t, undo information is not recorded. */);
5998 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5999 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6001 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6002 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6004 Normally, the line-motion functions work by scanning the buffer for
6005 newlines. Columnar operations (like `move-to-column' and
6006 `compute-motion') also work by scanning the buffer, summing character
6007 widths as they go. This works well for ordinary text, but if the
6008 buffer's lines are very long (say, more than 500 characters), these
6009 motion functions will take longer to execute. Emacs may also take
6010 longer to update the display.
6012 If `cache-long-line-scans' is non-nil, these motion functions cache the
6013 results of their scans, and consult the cache to avoid rescanning
6014 regions of the buffer until the text is modified. The caches are most
6015 beneficial when they prevent the most searching---that is, when the
6016 buffer contains long lines and large regions of characters with the
6017 same, fixed screen width.
6019 When `cache-long-line-scans' is non-nil, processing short lines will
6020 become slightly slower (because of the overhead of consulting the
6021 cache), and the caches will use memory roughly proportional to the
6022 number of newlines and characters whose screen width varies.
6024 The caches require no explicit maintenance; their accuracy is
6025 maintained internally by the Emacs primitives. Enabling or disabling
6026 the cache should not affect the behavior of any of the motion
6027 functions; it should only affect their performance. */);
6029 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6030 doc: /* Value of point before the last series of scroll operations, or nil. */);
6032 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6033 doc: /* List of formats to use when saving this buffer.
6034 Formats are defined by `format-alist'. This variable is
6035 set when a file is visited. */);
6037 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6038 &current_buffer->auto_save_file_format, Qnil,
6039 doc: /* *Format in which to write auto-save files.
6040 Should be a list of symbols naming formats that are defined in `format-alist'.
6041 If it is t, which is the default, auto-save files are written in the
6042 same format as a regular save would use. */);
6044 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6045 &current_buffer->invisibility_spec, Qnil,
6046 doc: /* Invisibility spec of this buffer.
6047 The default is t, which means that text is invisible
6048 if it has a non-nil `invisible' property.
6049 If the value is a list, a text character is invisible if its `invisible'
6050 property is an element in that list (or is a list with members in common).
6051 If an element is a cons cell of the form (PROP . ELLIPSIS),
6052 then characters with property value PROP are invisible,
6053 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6055 DEFVAR_PER_BUFFER ("buffer-display-count",
6056 &current_buffer->display_count, Qnil,
6057 doc: /* A number incremented each time this buffer is displayed in a window.
6058 The function `set-window-buffer' increments it. */);
6060 DEFVAR_PER_BUFFER ("buffer-display-time",
6061 &current_buffer->display_time, Qnil,
6062 doc: /* Time stamp updated each time this buffer is displayed in a window.
6063 The function `set-window-buffer' updates this variable
6064 to the value obtained by calling `current-time'.
6065 If the buffer has never been shown in a window, the value is nil. */);
6067 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6068 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
6069 Non-nil also enables highlighting of the region whenever the mark is active.
6070 The variable `highlight-nonselected-windows' controls whether to highlight
6071 all windows or just the selected window.
6073 If the value is `lambda', that enables Transient Mark mode temporarily
6074 until the next buffer modification. If a command sets the value to `only',
6075 that enables Transient Mark mode for the following command only.
6076 During that following command, the value of `transient-mark-mode'
6077 is `identity'. If it is still `identity' at the end of that command,
6078 it changes to nil. */);
6079 Vtransient_mark_mode = Qnil;
6081 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6082 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6083 If the value is t, disregard `buffer-read-only' and all `read-only'
6084 text properties. If the value is a list, disregard `buffer-read-only'
6085 and disregard a `read-only' text property if the property value
6086 is a member of the list. */);
6087 Vinhibit_read_only = Qnil;
6089 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6090 doc: /* Cursor to use when this buffer is in the selected window.
6091 Values are interpreted as follows:
6093 t use the cursor specified for the frame
6094 nil don't display a cursor
6095 box display a filled box cursor
6096 hollow display a hollow box cursor
6097 bar display a vertical bar cursor with default width
6098 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6099 hbar display a horizontal bar cursor with default height
6100 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6101 ANYTHING ELSE display a hollow box cursor
6103 When the buffer is displayed in a nonselected window,
6104 this variable has no effect; the cursor appears as a hollow box. */);
6106 DEFVAR_PER_BUFFER ("line-spacing",
6107 &current_buffer->extra_line_spacing, Qnil,
6108 doc: /* Additional space to put between lines when displaying a buffer.
6109 The space is measured in pixels, and put below lines on window systems.
6110 If value is a floating point number, it specifies the spacing relative
6111 to the default frame line height. A value of nil means add no extra space. */);
6113 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6114 &current_buffer->cursor_in_non_selected_windows, Qnil,
6115 doc: /* *Cursor type to display in non-selected windows.
6116 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6118 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6119 doc: /* List of functions called with no args to query before killing a buffer.
6120 The buffer being killed will be current while the functions are running.
6121 If any of them returns nil, the buffer is not killed. */);
6122 Vkill_buffer_query_functions = Qnil;
6124 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6125 doc: /* Normal hook run before changing the major mode of a buffer.
6126 The function `kill-all-local-variables' runs this before doing anything else. */);
6127 Vchange_major_mode_hook = Qnil;
6128 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6129 staticpro (&Qchange_major_mode_hook);
6131 defsubr (&Sbuffer_live_p);
6132 defsubr (&Sbuffer_list);
6133 defsubr (&Sget_buffer);
6134 defsubr (&Sget_file_buffer);
6135 defsubr (&Sget_buffer_create);
6136 defsubr (&Smake_indirect_buffer);
6137 defsubr (&Sgenerate_new_buffer_name);
6138 defsubr (&Sbuffer_name);
6139 /*defsubr (&Sbuffer_number);*/
6140 defsubr (&Sbuffer_file_name);
6141 defsubr (&Sbuffer_base_buffer);
6142 defsubr (&Sbuffer_local_value);
6143 defsubr (&Sbuffer_local_variables);
6144 defsubr (&Sbuffer_modified_p);
6145 defsubr (&Sset_buffer_modified_p);
6146 defsubr (&Sbuffer_modified_tick);
6147 defsubr (&Sbuffer_chars_modified_tick);
6148 defsubr (&Srename_buffer);
6149 defsubr (&Sother_buffer);
6150 defsubr (&Sbuffer_enable_undo);
6151 defsubr (&Skill_buffer);
6152 defsubr (&Sset_buffer_major_mode);
6153 defsubr (&Sswitch_to_buffer);
6154 defsubr (&Spop_to_buffer);
6155 defsubr (&Scurrent_buffer);
6156 defsubr (&Sset_buffer);
6157 defsubr (&Sbarf_if_buffer_read_only);
6158 defsubr (&Sbury_buffer);
6159 defsubr (&Serase_buffer);
6160 defsubr (&Sset_buffer_multibyte);
6161 defsubr (&Skill_all_local_variables);
6163 defsubr (&Soverlayp);
6164 defsubr (&Smake_overlay);
6165 defsubr (&Sdelete_overlay);
6166 defsubr (&Smove_overlay);
6167 defsubr (&Soverlay_start);
6168 defsubr (&Soverlay_end);
6169 defsubr (&Soverlay_buffer);
6170 defsubr (&Soverlay_properties);
6171 defsubr (&Soverlays_at);
6172 defsubr (&Soverlays_in);
6173 defsubr (&Snext_overlay_change);
6174 defsubr (&Sprevious_overlay_change);
6175 defsubr (&Soverlay_recenter);
6176 defsubr (&Soverlay_lists);
6177 defsubr (&Soverlay_get);
6178 defsubr (&Soverlay_put);
6179 defsubr (&Srestore_buffer_modified_p);
6182 void
6183 keys_of_buffer ()
6185 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6186 initial_define_key (control_x_map, 'k', "kill-buffer");
6188 /* This must not be in syms_of_buffer, because Qdisabled is not
6189 initialized when that function gets called. */
6190 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6193 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6194 (do not change this comment) */