(font-lock-keywords): Add defvar.
[emacs.git] / src / buffer.c
blob30626f11a24aeeb11931ac0538476a823a7e6de4
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 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 2, 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
35 #ifndef MAXPATHLEN
36 /* in 4.1 [probably SunOS? -stef] , param.h fails to define this. */
37 #define MAXPATHLEN 1024
38 #endif /* not MAXPATHLEN */
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #include "lisp.h"
45 #include "intervals.h"
46 #include "window.h"
47 #include "commands.h"
48 #include "buffer.h"
49 #include "charset.h"
50 #include "region-cache.h"
51 #include "indent.h"
52 #include "blockinput.h"
53 #include "keyboard.h"
54 #include "keymap.h"
55 #include "frame.h"
57 struct buffer *current_buffer; /* the current buffer */
59 /* First buffer in chain of all buffers (in reverse order of creation).
60 Threaded through ->next. */
62 struct buffer *all_buffers;
64 /* This structure holds the default values of the buffer-local variables
65 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
66 The default value occupies the same slot in this structure
67 as an individual buffer's value occupies in that buffer.
68 Setting the default value also goes through the alist of buffers
69 and stores into each buffer that does not say it has a local value. */
71 DECL_ALIGN (struct buffer, buffer_defaults);
73 /* A Lisp_Object pointer to the above, used for staticpro */
75 static Lisp_Object Vbuffer_defaults;
77 /* This structure marks which slots in a buffer have corresponding
78 default values in buffer_defaults.
79 Each such slot has a nonzero value in this structure.
80 The value has only one nonzero bit.
82 When a buffer has its own local value for a slot,
83 the entry for that slot (found in the same slot in this structure)
84 is turned on in the buffer's local_flags array.
86 If a slot in this structure is -1, then even though there may
87 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
88 and the corresponding slot in buffer_defaults is not used.
90 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
91 but there is a default value which is copied into each buffer.
93 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
94 zero, that is a bug */
96 struct buffer buffer_local_flags;
98 /* This structure holds the names of symbols whose values may be
99 buffer-local. It is indexed and accessed in the same way as the above. */
101 DECL_ALIGN (struct buffer, buffer_local_symbols);
103 /* A Lisp_Object pointer to the above, used for staticpro */
104 static Lisp_Object Vbuffer_local_symbols;
106 /* This structure holds the required types for the values in the
107 buffer-local slots. If a slot contains Qnil, then the
108 corresponding buffer slot may contain a value of any type. If a
109 slot contains an integer, then prospective values' tags must be
110 equal to that integer (except nil is always allowed).
111 When a tag does not match, the function
112 buffer_slot_type_mismatch will signal an error.
114 If a slot here contains -1, the corresponding variable is read-only. */
115 struct buffer buffer_local_types;
117 /* Flags indicating which built-in buffer-local variables
118 are permanent locals. */
119 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
121 /* Number of per-buffer variables used. */
123 int last_per_buffer_idx;
125 Lisp_Object Fset_buffer ();
126 void set_buffer_internal ();
127 void set_buffer_internal_1 ();
128 static void call_overlay_mod_hooks ();
129 static void swap_out_buffer_local_variables ();
130 static void reset_buffer_local_variables ();
132 /* Alist of all buffer names vs the buffers. */
133 /* This used to be a variable, but is no longer,
134 to prevent lossage due to user rplac'ing this alist or its elements. */
135 Lisp_Object Vbuffer_alist;
137 /* Functions to call before and after each text change. */
138 Lisp_Object Vbefore_change_functions;
139 Lisp_Object Vafter_change_functions;
141 Lisp_Object Vtransient_mark_mode;
143 /* t means ignore all read-only text properties.
144 A list means ignore such a property if its value is a member of the list.
145 Any non-nil value means ignore buffer-read-only. */
146 Lisp_Object Vinhibit_read_only;
148 /* List of functions to call that can query about killing a buffer.
149 If any of these functions returns nil, we don't kill it. */
150 Lisp_Object Vkill_buffer_query_functions;
151 Lisp_Object Qkill_buffer_query_functions;
153 /* List of functions to call before changing an unmodified buffer. */
154 Lisp_Object Vfirst_change_hook;
156 Lisp_Object Qfirst_change_hook;
157 Lisp_Object Qbefore_change_functions;
158 Lisp_Object Qafter_change_functions;
159 Lisp_Object Qucs_set_table_for_input;
161 /* If nonzero, all modification hooks are suppressed. */
162 int inhibit_modification_hooks;
164 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
166 Lisp_Object Qprotected_field;
168 Lisp_Object QSFundamental; /* A string "Fundamental" */
170 Lisp_Object Qkill_buffer_hook;
172 Lisp_Object Qget_file_buffer;
174 Lisp_Object Qoverlayp;
176 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
178 Lisp_Object Qmodification_hooks;
179 Lisp_Object Qinsert_in_front_hooks;
180 Lisp_Object Qinsert_behind_hooks;
182 static void alloc_buffer_text P_ ((struct buffer *, size_t));
183 static void free_buffer_text P_ ((struct buffer *b));
184 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
185 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
186 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
189 /* For debugging; temporary. See set_buffer_internal. */
190 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
192 void
193 nsberror (spec)
194 Lisp_Object spec;
196 if (STRINGP (spec))
197 error ("No buffer named %s", SDATA (spec));
198 error ("Invalid buffer argument");
201 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
202 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
203 Value is nil if OBJECT is not a buffer or if it has been killed. */)
204 (object)
205 Lisp_Object object;
207 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
208 ? Qt : Qnil);
211 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
212 doc: /* Return a list of all existing live buffers.
213 If the optional arg FRAME is a frame, we return the buffer list
214 in the proper order for that frame: the buffers in FRAME's `buffer-list'
215 frame parameter come first, followed by the rest of the buffers. */)
216 (frame)
217 Lisp_Object frame;
219 Lisp_Object framelist, general;
220 general = Fmapcar (Qcdr, Vbuffer_alist);
222 if (FRAMEP (frame))
224 Lisp_Object tail;
226 CHECK_FRAME (frame);
228 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
230 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
231 tail = framelist;
232 while (! NILP (tail))
234 general = Fdelq (XCAR (tail), general);
235 tail = XCDR (tail);
237 return nconc2 (framelist, general);
240 return general;
243 /* Like Fassoc, but use Fstring_equal to compare
244 (which ignores text properties),
245 and don't ever QUIT. */
247 static Lisp_Object
248 assoc_ignore_text_properties (key, list)
249 register Lisp_Object key;
250 Lisp_Object list;
252 register Lisp_Object tail;
253 for (tail = list; CONSP (tail); tail = XCDR (tail))
255 register Lisp_Object elt, tem;
256 elt = XCAR (tail);
257 tem = Fstring_equal (Fcar (elt), key);
258 if (!NILP (tem))
259 return elt;
261 return Qnil;
264 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
265 doc: /* Return the buffer named NAME (a string).
266 If there is no live buffer named NAME, return nil.
267 NAME may also be a buffer; if so, the value is that buffer. */)
268 (name)
269 register Lisp_Object name;
271 if (BUFFERP (name))
272 return name;
273 CHECK_STRING (name);
275 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
278 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
279 doc: /* Return the buffer visiting file FILENAME (a string).
280 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
281 If there is no such live buffer, return nil.
282 See also `find-buffer-visiting'. */)
283 (filename)
284 register Lisp_Object filename;
286 register Lisp_Object tail, buf, tem;
287 Lisp_Object handler;
289 CHECK_STRING (filename);
290 filename = Fexpand_file_name (filename, Qnil);
292 /* If the file name has special constructs in it,
293 call the corresponding file handler. */
294 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
295 if (!NILP (handler))
296 return call2 (handler, Qget_file_buffer, filename);
298 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
300 buf = Fcdr (XCAR (tail));
301 if (!BUFFERP (buf)) continue;
302 if (!STRINGP (XBUFFER (buf)->filename)) continue;
303 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
304 if (!NILP (tem))
305 return buf;
307 return Qnil;
310 Lisp_Object
311 get_truename_buffer (filename)
312 register Lisp_Object filename;
314 register Lisp_Object tail, buf, tem;
316 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
318 buf = Fcdr (XCAR (tail));
319 if (!BUFFERP (buf)) continue;
320 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
321 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
322 if (!NILP (tem))
323 return buf;
325 return Qnil;
328 /* Incremented for each buffer created, to assign the buffer number. */
329 int buffer_count;
331 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
332 doc: /* Return the buffer named NAME, or create such a buffer and return it.
333 A new buffer is created if there is no live buffer named NAME.
334 If NAME starts with a space, the new buffer does not keep undo information.
335 If NAME is a buffer instead of a string, then it is the value returned.
336 The value is never nil. */)
337 (name)
338 register Lisp_Object name;
340 register Lisp_Object buf;
341 register struct buffer *b;
343 buf = Fget_buffer (name);
344 if (!NILP (buf))
345 return buf;
347 if (SCHARS (name) == 0)
348 error ("Empty string for buffer name is not allowed");
350 b = (struct buffer *) allocate_buffer ();
352 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
354 /* An ordinary buffer uses its own struct buffer_text. */
355 b->text = &b->own_text;
356 b->base_buffer = 0;
358 BUF_GAP_SIZE (b) = 20;
359 BLOCK_INPUT;
360 /* We allocate extra 1-byte at the tail and keep it always '\0' for
361 anchoring a search. */
362 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
363 UNBLOCK_INPUT;
364 if (! BUF_BEG_ADDR (b))
365 buffer_memory_full ();
367 BUF_PT (b) = BEG;
368 BUF_GPT (b) = BEG;
369 BUF_BEGV (b) = BEG;
370 BUF_ZV (b) = BEG;
371 BUF_Z (b) = BEG;
372 BUF_PT_BYTE (b) = BEG_BYTE;
373 BUF_GPT_BYTE (b) = BEG_BYTE;
374 BUF_BEGV_BYTE (b) = BEG_BYTE;
375 BUF_ZV_BYTE (b) = BEG_BYTE;
376 BUF_Z_BYTE (b) = BEG_BYTE;
377 BUF_MODIFF (b) = 1;
378 BUF_OVERLAY_MODIFF (b) = 1;
379 BUF_SAVE_MODIFF (b) = 1;
380 BUF_INTERVALS (b) = 0;
381 BUF_UNCHANGED_MODIFIED (b) = 1;
382 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
383 BUF_END_UNCHANGED (b) = 0;
384 BUF_BEG_UNCHANGED (b) = 0;
385 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
387 b->newline_cache = 0;
388 b->width_run_cache = 0;
389 b->width_table = Qnil;
390 b->prevent_redisplay_optimizations_p = 1;
392 /* Put this on the chain of all buffers including killed ones. */
393 b->next = all_buffers;
394 all_buffers = b;
396 /* An ordinary buffer normally doesn't need markers
397 to handle BEGV and ZV. */
398 b->pt_marker = Qnil;
399 b->begv_marker = Qnil;
400 b->zv_marker = Qnil;
402 name = Fcopy_sequence (name);
403 STRING_SET_INTERVALS (name, NULL_INTERVAL);
404 b->name = name;
406 if (SREF (name, 0) != ' ')
407 b->undo_list = Qnil;
408 else
409 b->undo_list = Qt;
411 reset_buffer (b);
412 reset_buffer_local_variables (b, 1);
414 b->mark = Fmake_marker ();
415 BUF_MARKERS (b) = NULL;
416 b->name = name;
418 /* Put this in the alist of all live buffers. */
419 XSETBUFFER (buf, b);
420 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
422 /* An error in calling the function here (should someone redfine it)
423 can lead to infinite regress until you run out of stack. rms
424 says that's not worth protecting against. */
425 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
426 /* buf is on buffer-alist, so no gcpro. */
427 call1 (Qucs_set_table_for_input, buf);
429 return buf;
433 /* Return a list of overlays which is a copy of the overlay list
434 LIST, but for buffer B. */
436 static struct Lisp_Overlay *
437 copy_overlays (b, list)
438 struct buffer *b;
439 struct Lisp_Overlay *list;
441 Lisp_Object buffer;
442 struct Lisp_Overlay *result = NULL, *tail = NULL;
444 XSETBUFFER (buffer, b);
446 for (; list; list = list->next)
448 Lisp_Object overlay, start, end, old_overlay;
449 EMACS_INT charpos;
451 XSETMISC (old_overlay, list);
452 charpos = marker_position (OVERLAY_START (old_overlay));
453 start = Fmake_marker ();
454 Fset_marker (start, make_number (charpos), buffer);
455 XMARKER (start)->insertion_type
456 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
458 charpos = marker_position (OVERLAY_END (old_overlay));
459 end = Fmake_marker ();
460 Fset_marker (end, make_number (charpos), buffer);
461 XMARKER (end)->insertion_type
462 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
464 overlay = allocate_misc ();
465 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
466 OVERLAY_START (overlay) = start;
467 OVERLAY_END (overlay) = end;
468 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
469 XOVERLAY (overlay)->next = NULL;
471 if (tail)
472 tail = tail->next = XOVERLAY (overlay);
473 else
474 result = tail = XOVERLAY (overlay);
477 return result;
481 /* Clone per-buffer values of buffer FROM.
483 Buffer TO gets the same per-buffer values as FROM, with the
484 following exceptions: (1) TO's name is left untouched, (2) markers
485 are copied and made to refer to TO, and (3) overlay lists are
486 copied. */
488 static void
489 clone_per_buffer_values (from, to)
490 struct buffer *from, *to;
492 Lisp_Object to_buffer, tem;
493 int offset;
495 XSETBUFFER (to_buffer, to);
497 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
498 offset < sizeof *to;
499 offset += sizeof (Lisp_Object))
501 Lisp_Object obj;
503 obj = PER_BUFFER_VALUE (from, offset);
504 if (MARKERP (obj))
506 struct Lisp_Marker *m = XMARKER (obj);
507 obj = Fmake_marker ();
508 XMARKER (obj)->insertion_type = m->insertion_type;
509 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
512 PER_BUFFER_VALUE (to, offset) = obj;
515 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
517 to->overlays_before = copy_overlays (to, from->overlays_before);
518 to->overlays_after = copy_overlays (to, from->overlays_after);
520 /* Get (a copy of) the alist of Lisp-level local variables of FROM
521 and install that in TO. */
522 to->local_var_alist = buffer_lisp_local_variables (from);
525 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
526 2, 3,
527 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
528 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
529 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
530 NAME should be a string which is not the name of an existing buffer.
531 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
532 such as major and minor modes, in the indirect buffer.
533 CLONE nil means the indirect buffer's state is reset to default values. */)
534 (base_buffer, name, clone)
535 Lisp_Object base_buffer, name, clone;
537 Lisp_Object buf, tem;
538 struct buffer *b;
540 CHECK_STRING (name);
541 buf = Fget_buffer (name);
542 if (!NILP (buf))
543 error ("Buffer name `%s' is in use", SDATA (name));
545 tem = base_buffer;
546 base_buffer = Fget_buffer (base_buffer);
547 if (NILP (base_buffer))
548 error ("No such buffer: `%s'", SDATA (tem));
549 if (NILP (XBUFFER (base_buffer)->name))
550 error ("Base buffer has been killed");
552 if (SCHARS (name) == 0)
553 error ("Empty string for buffer name is not allowed");
555 b = (struct buffer *) allocate_buffer ();
556 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
558 if (XBUFFER (base_buffer)->base_buffer)
559 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
560 else
561 b->base_buffer = XBUFFER (base_buffer);
563 /* Use the base buffer's text object. */
564 b->text = b->base_buffer->text;
566 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
567 BUF_ZV (b) = BUF_ZV (b->base_buffer);
568 BUF_PT (b) = BUF_PT (b->base_buffer);
569 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
570 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
571 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
573 b->newline_cache = 0;
574 b->width_run_cache = 0;
575 b->width_table = Qnil;
577 /* Put this on the chain of all buffers including killed ones. */
578 b->next = all_buffers;
579 all_buffers = b;
581 name = Fcopy_sequence (name);
582 STRING_SET_INTERVALS (name, NULL_INTERVAL);
583 b->name = name;
585 reset_buffer (b);
586 reset_buffer_local_variables (b, 1);
588 /* Put this in the alist of all live buffers. */
589 XSETBUFFER (buf, b);
590 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
592 b->mark = Fmake_marker ();
593 b->name = name;
595 /* The multibyte status belongs to the base buffer. */
596 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
598 /* Make sure the base buffer has markers for its narrowing. */
599 if (NILP (b->base_buffer->pt_marker))
601 b->base_buffer->pt_marker = Fmake_marker ();
602 set_marker_both (b->base_buffer->pt_marker, base_buffer,
603 BUF_PT (b->base_buffer),
604 BUF_PT_BYTE (b->base_buffer));
606 if (NILP (b->base_buffer->begv_marker))
608 b->base_buffer->begv_marker = Fmake_marker ();
609 set_marker_both (b->base_buffer->begv_marker, base_buffer,
610 BUF_BEGV (b->base_buffer),
611 BUF_BEGV_BYTE (b->base_buffer));
613 if (NILP (b->base_buffer->zv_marker))
615 b->base_buffer->zv_marker = Fmake_marker ();
616 set_marker_both (b->base_buffer->zv_marker, base_buffer,
617 BUF_ZV (b->base_buffer),
618 BUF_ZV_BYTE (b->base_buffer));
619 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
622 if (NILP (clone))
624 /* Give the indirect buffer markers for its narrowing. */
625 b->pt_marker = Fmake_marker ();
626 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
627 b->begv_marker = Fmake_marker ();
628 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
629 b->zv_marker = Fmake_marker ();
630 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
631 XMARKER (b->zv_marker)->insertion_type = 1;
633 else
635 struct buffer *old_b = current_buffer;
637 clone_per_buffer_values (b->base_buffer, b);
638 b->filename = Qnil;
639 b->file_truename = Qnil;
640 b->display_count = make_number (0);
641 b->backed_up = Qnil;
642 b->auto_save_file_name = Qnil;
643 set_buffer_internal_1 (b);
644 Fset (intern ("buffer-save-without-query"), Qnil);
645 Fset (intern ("buffer-file-number"), Qnil);
646 Fset (intern ("buffer-stale-function"), Qnil);
647 set_buffer_internal_1 (old_b);
650 return buf;
653 void
654 delete_all_overlays (b)
655 struct buffer *b;
657 Lisp_Object overlay;
659 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
660 have to empty the list, otherwise we end up with overlays that
661 think they belong to this buffer while the buffer doesn't know about
662 them any more. */
663 while (b->overlays_before)
665 XSETMISC (overlay, b->overlays_before);
666 Fdelete_overlay (overlay);
668 while (b->overlays_after)
670 XSETMISC (overlay, b->overlays_after);
671 Fdelete_overlay (overlay);
673 eassert (b->overlays_before == NULL);
674 eassert (b->overlays_after == NULL);
677 /* Reinitialize everything about a buffer except its name and contents
678 and local variables.
679 If called on an already-initialized buffer, the list of overlays
680 should be deleted before calling this function, otherwise we end up
681 with overlays that claim to belong to the buffer but the buffer
682 claims it doesn't belong to it. */
684 void
685 reset_buffer (b)
686 register struct buffer *b;
688 b->filename = Qnil;
689 b->file_truename = Qnil;
690 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
691 b->modtime = 0;
692 XSETFASTINT (b->save_length, 0);
693 b->last_window_start = 1;
694 /* It is more conservative to start out "changed" than "unchanged". */
695 b->clip_changed = 0;
696 b->prevent_redisplay_optimizations_p = 1;
697 b->backed_up = Qnil;
698 b->auto_save_modified = 0;
699 b->auto_save_failure_time = -1;
700 b->auto_save_file_name = Qnil;
701 b->read_only = Qnil;
702 b->overlays_before = NULL;
703 b->overlays_after = NULL;
704 b->overlay_center = BEG;
705 b->mark_active = Qnil;
706 b->point_before_scroll = Qnil;
707 b->file_format = Qnil;
708 b->auto_save_file_format = Qt;
709 b->last_selected_window = Qnil;
710 XSETINT (b->display_count, 0);
711 b->display_time = Qnil;
712 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
713 b->cursor_type = buffer_defaults.cursor_type;
714 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
716 b->display_error_modiff = 0;
719 /* Reset buffer B's local variables info.
720 Don't use this on a buffer that has already been in use;
721 it does not treat permanent locals consistently.
722 Instead, use Fkill_all_local_variables.
724 If PERMANENT_TOO is 1, then we reset permanent built-in
725 buffer-local variables. If PERMANENT_TOO is 0,
726 we preserve those. */
728 static void
729 reset_buffer_local_variables (b, permanent_too)
730 register struct buffer *b;
731 int permanent_too;
733 register int offset;
734 int i;
736 /* Reset the major mode to Fundamental, together with all the
737 things that depend on the major mode.
738 default-major-mode is handled at a higher level.
739 We ignore it here. */
740 b->major_mode = Qfundamental_mode;
741 b->keymap = Qnil;
742 b->mode_name = QSFundamental;
743 b->minor_modes = Qnil;
745 /* If the standard case table has been altered and invalidated,
746 fix up its insides first. */
747 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
748 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
749 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
750 Fset_standard_case_table (Vascii_downcase_table);
752 b->downcase_table = Vascii_downcase_table;
753 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
754 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
755 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
756 b->invisibility_spec = Qt;
757 #ifndef DOS_NT
758 b->buffer_file_type = Qnil;
759 #endif
761 /* Reset all (or most) per-buffer variables to their defaults. */
762 b->local_var_alist = Qnil;
763 for (i = 0; i < last_per_buffer_idx; ++i)
764 if (permanent_too || buffer_permanent_local_flags[i] == 0)
765 SET_PER_BUFFER_VALUE_P (b, i, 0);
767 /* For each slot that has a default value,
768 copy that into the slot. */
770 for (offset = PER_BUFFER_VAR_OFFSET (name);
771 offset < sizeof *b;
772 offset += sizeof (Lisp_Object))
774 int idx = PER_BUFFER_IDX (offset);
775 if ((idx > 0
776 && (permanent_too
777 || buffer_permanent_local_flags[idx] == 0))
778 /* Is -2 used anywhere? */
779 || idx == -2)
780 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
784 /* We split this away from generate-new-buffer, because rename-buffer
785 and set-visited-file-name ought to be able to use this to really
786 rename the buffer properly. */
788 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
789 1, 2, 0,
790 doc: /* Return a string that is the name of no existing buffer based on NAME.
791 If there is no live buffer named NAME, then return NAME.
792 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
793 \(starting at 2) until an unused name is found, and then return that name.
794 Optional second argument IGNORE specifies a name that is okay to use
795 \(if it is in the sequence to be tried)
796 even if a buffer with that name exists. */)
797 (name, ignore)
798 register Lisp_Object name, ignore;
800 register Lisp_Object gentemp, tem;
801 int count;
802 char number[10];
804 CHECK_STRING (name);
806 tem = Fstring_equal (name, ignore);
807 if (!NILP (tem))
808 return name;
809 tem = Fget_buffer (name);
810 if (NILP (tem))
811 return name;
813 count = 1;
814 while (1)
816 sprintf (number, "<%d>", ++count);
817 gentemp = concat2 (name, build_string (number));
818 tem = Fstring_equal (gentemp, ignore);
819 if (!NILP (tem))
820 return gentemp;
821 tem = Fget_buffer (gentemp);
822 if (NILP (tem))
823 return gentemp;
828 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
829 doc: /* Return the name of BUFFER, as a string.
830 With no argument or nil as argument, return the name of the current buffer. */)
831 (buffer)
832 register Lisp_Object buffer;
834 if (NILP (buffer))
835 return current_buffer->name;
836 CHECK_BUFFER (buffer);
837 return XBUFFER (buffer)->name;
840 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
841 doc: /* Return name of file BUFFER is visiting, or nil if none.
842 No argument or nil as argument means use the current buffer. */)
843 (buffer)
844 register Lisp_Object buffer;
846 if (NILP (buffer))
847 return current_buffer->filename;
848 CHECK_BUFFER (buffer);
849 return XBUFFER (buffer)->filename;
852 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
853 0, 1, 0,
854 doc: /* Return the base buffer of indirect buffer BUFFER.
855 If BUFFER is not indirect, return nil.
856 BUFFER defaults to the current buffer. */)
857 (buffer)
858 register Lisp_Object buffer;
860 struct buffer *base;
861 Lisp_Object base_buffer;
863 if (NILP (buffer))
864 base = current_buffer->base_buffer;
865 else
867 CHECK_BUFFER (buffer);
868 base = XBUFFER (buffer)->base_buffer;
871 if (! base)
872 return Qnil;
873 XSETBUFFER (base_buffer, base);
874 return base_buffer;
877 DEFUN ("buffer-local-value", Fbuffer_local_value,
878 Sbuffer_local_value, 2, 2, 0,
879 doc: /* Return the value of VARIABLE in BUFFER.
880 If VARIABLE does not have a buffer-local binding in BUFFER, the value
881 is the default binding of the variable. */)
882 (variable, buffer)
883 register Lisp_Object variable;
884 register Lisp_Object buffer;
886 register struct buffer *buf;
887 register Lisp_Object result;
889 CHECK_SYMBOL (variable);
890 CHECK_BUFFER (buffer);
891 buf = XBUFFER (buffer);
893 if (SYMBOLP (variable))
894 variable = indirect_variable (variable);
896 /* Look in local_var_list */
897 result = Fassoc (variable, buf->local_var_alist);
898 if (NILP (result))
900 int offset, idx;
901 int found = 0;
903 /* Look in special slots */
904 for (offset = PER_BUFFER_VAR_OFFSET (name);
905 offset < sizeof (struct buffer);
906 /* sizeof EMACS_INT == sizeof Lisp_Object */
907 offset += (sizeof (EMACS_INT)))
909 idx = PER_BUFFER_IDX (offset);
910 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
911 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
912 && EQ (PER_BUFFER_SYMBOL (offset), variable))
914 result = PER_BUFFER_VALUE (buf, offset);
915 found = 1;
916 break;
920 if (!found)
921 result = Fdefault_value (variable);
923 else
925 Lisp_Object valcontents;
926 Lisp_Object current_alist_element;
928 /* What binding is loaded right now? */
929 valcontents = SYMBOL_VALUE (variable);
930 current_alist_element
931 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
933 /* The value of the currently loaded binding is not
934 stored in it, but rather in the realvalue slot.
935 Store that value into the binding it belongs to
936 in case that is the one we are about to use. */
938 Fsetcdr (current_alist_element,
939 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
941 /* Now get the (perhaps updated) value out of the binding. */
942 result = XCDR (result);
945 if (EQ (result, Qunbound))
946 return Fsignal (Qvoid_variable, Fcons (variable, Qnil));
948 return result;
951 /* Return an alist of the Lisp-level buffer-local bindings of
952 buffer BUF. That is, don't include the variables maintained
953 in special slots in the buffer object. */
955 static Lisp_Object
956 buffer_lisp_local_variables (buf)
957 struct buffer *buf;
959 Lisp_Object result = Qnil;
960 register Lisp_Object tail;
961 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
963 Lisp_Object val, elt;
965 elt = XCAR (tail);
967 /* Reference each variable in the alist in buf.
968 If inquiring about the current buffer, this gets the current values,
969 so store them into the alist so the alist is up to date.
970 If inquiring about some other buffer, this swaps out any values
971 for that buffer, making the alist up to date automatically. */
972 val = find_symbol_value (XCAR (elt));
973 /* Use the current buffer value only if buf is the current buffer. */
974 if (buf != current_buffer)
975 val = XCDR (elt);
977 /* If symbol is unbound, put just the symbol in the list. */
978 if (EQ (val, Qunbound))
979 result = Fcons (XCAR (elt), result);
980 /* Otherwise, put (symbol . value) in the list. */
981 else
982 result = Fcons (Fcons (XCAR (elt), val), result);
985 return result;
988 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
989 Sbuffer_local_variables, 0, 1, 0,
990 doc: /* Return an alist of variables that are buffer-local in BUFFER.
991 Most elements look like (SYMBOL . VALUE), describing one variable.
992 For a symbol that is locally unbound, just the symbol appears in the value.
993 Note that storing new VALUEs in these elements doesn't change the variables.
994 No argument or nil as argument means use current buffer as BUFFER. */)
995 (buffer)
996 register Lisp_Object buffer;
998 register struct buffer *buf;
999 register Lisp_Object result;
1001 if (NILP (buffer))
1002 buf = current_buffer;
1003 else
1005 CHECK_BUFFER (buffer);
1006 buf = XBUFFER (buffer);
1009 result = buffer_lisp_local_variables (buf);
1011 /* Add on all the variables stored in special slots. */
1013 int offset, idx;
1015 for (offset = PER_BUFFER_VAR_OFFSET (name);
1016 offset < sizeof (struct buffer);
1017 /* sizeof EMACS_INT == sizeof Lisp_Object */
1018 offset += (sizeof (EMACS_INT)))
1020 idx = PER_BUFFER_IDX (offset);
1021 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1022 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1023 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1024 PER_BUFFER_VALUE (buf, offset)),
1025 result);
1029 return result;
1032 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1033 0, 1, 0,
1034 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1035 No argument or nil as argument means use current buffer as BUFFER. */)
1036 (buffer)
1037 register Lisp_Object buffer;
1039 register struct buffer *buf;
1040 if (NILP (buffer))
1041 buf = current_buffer;
1042 else
1044 CHECK_BUFFER (buffer);
1045 buf = XBUFFER (buffer);
1048 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1051 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1052 1, 1, 0,
1053 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1054 A non-nil FLAG means mark the buffer modified. */)
1055 (flag)
1056 register Lisp_Object flag;
1058 register int already;
1059 register Lisp_Object fn;
1060 Lisp_Object buffer, window;
1062 #ifdef CLASH_DETECTION
1063 /* If buffer becoming modified, lock the file.
1064 If buffer becoming unmodified, unlock the file. */
1066 fn = current_buffer->file_truename;
1067 /* Test buffer-file-name so that binding it to nil is effective. */
1068 if (!NILP (fn) && ! NILP (current_buffer->filename))
1070 already = SAVE_MODIFF < MODIFF;
1071 if (!already && !NILP (flag))
1072 lock_file (fn);
1073 else if (already && NILP (flag))
1074 unlock_file (fn);
1076 #endif /* CLASH_DETECTION */
1078 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1080 /* Set update_mode_lines only if buffer is displayed in some window.
1081 Packages like jit-lock or lazy-lock preserve a buffer's modified
1082 state by recording/restoring the state around blocks of code.
1083 Setting update_mode_lines makes redisplay consider all windows
1084 (on all frames). Stealth fontification of buffers not displayed
1085 would incur additional redisplay costs if we'd set
1086 update_modes_lines unconditionally.
1088 Ideally, I think there should be another mechanism for fontifying
1089 buffers without "modifying" buffers, or redisplay should be
1090 smarter about updating the `*' in mode lines. --gerd */
1091 XSETBUFFER (buffer, current_buffer);
1092 window = Fget_buffer_window (buffer, Qt);
1093 if (WINDOWP (window))
1095 ++update_mode_lines;
1096 current_buffer->prevent_redisplay_optimizations_p = 1;
1099 return flag;
1102 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1103 Srestore_buffer_modified_p, 1, 1, 0,
1104 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1105 It is not ensured that mode lines will be updated to show the modified
1106 state of the current buffer. Use with care. */)
1107 (flag)
1108 Lisp_Object flag;
1110 #ifdef CLASH_DETECTION
1111 Lisp_Object fn;
1113 /* If buffer becoming modified, lock the file.
1114 If buffer becoming unmodified, unlock the file. */
1116 fn = current_buffer->file_truename;
1117 /* Test buffer-file-name so that binding it to nil is effective. */
1118 if (!NILP (fn) && ! NILP (current_buffer->filename))
1120 int already = SAVE_MODIFF < MODIFF;
1121 if (!already && !NILP (flag))
1122 lock_file (fn);
1123 else if (already && NILP (flag))
1124 unlock_file (fn);
1126 #endif /* CLASH_DETECTION */
1128 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1129 return flag;
1132 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1133 0, 1, 0,
1134 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1135 Each buffer has a tick counter which is incremented each time the text in
1136 that buffer is changed. It wraps around occasionally.
1137 No argument or nil as argument means use current buffer as BUFFER. */)
1138 (buffer)
1139 register Lisp_Object buffer;
1141 register struct buffer *buf;
1142 if (NILP (buffer))
1143 buf = current_buffer;
1144 else
1146 CHECK_BUFFER (buffer);
1147 buf = XBUFFER (buffer);
1150 return make_number (BUF_MODIFF (buf));
1153 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1154 "sRename buffer (to new name): \nP",
1155 doc: /* Change current buffer's name to NEWNAME (a string).
1156 If second arg UNIQUE is nil or omitted, it is an error if a
1157 buffer named NEWNAME already exists.
1158 If UNIQUE is non-nil, come up with a new name using
1159 `generate-new-buffer-name'.
1160 Interactively, you can set UNIQUE with a prefix argument.
1161 We return the name we actually gave the buffer.
1162 This does not change the name of the visited file (if any). */)
1163 (newname, unique)
1164 register Lisp_Object newname, unique;
1166 register Lisp_Object tem, buf;
1168 CHECK_STRING (newname);
1170 if (SCHARS (newname) == 0)
1171 error ("Empty string is invalid as a buffer name");
1173 tem = Fget_buffer (newname);
1174 if (!NILP (tem))
1176 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1177 rename the buffer automatically so you can create another
1178 with the original name. It makes UNIQUE equivalent to
1179 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1180 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1181 return current_buffer->name;
1182 if (!NILP (unique))
1183 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1184 else
1185 error ("Buffer name `%s' is in use", SDATA (newname));
1188 current_buffer->name = newname;
1190 /* Catch redisplay's attention. Unless we do this, the mode lines for
1191 any windows displaying current_buffer will stay unchanged. */
1192 update_mode_lines++;
1194 XSETBUFFER (buf, current_buffer);
1195 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1196 if (NILP (current_buffer->filename)
1197 && !NILP (current_buffer->auto_save_file_name))
1198 call0 (intern ("rename-auto-save-file"));
1199 /* Refetch since that last call may have done GC. */
1200 return current_buffer->name;
1203 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1204 doc: /* Return most recently selected buffer other than BUFFER.
1205 Buffers not visible in windows are preferred to visible buffers,
1206 unless optional second argument VISIBLE-OK is non-nil.
1207 If the optional third argument FRAME is non-nil, use that frame's
1208 buffer list instead of the selected frame's buffer list.
1209 If no other buffer exists, the buffer `*scratch*' is returned.
1210 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1211 (buffer, visible_ok, frame)
1212 register Lisp_Object buffer, visible_ok, frame;
1214 Lisp_Object Fset_buffer_major_mode ();
1215 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1216 notsogood = Qnil;
1218 if (NILP (frame))
1219 frame = selected_frame;
1221 tail = Vbuffer_alist;
1222 pred = frame_buffer_predicate (frame);
1224 /* Consider buffers that have been seen in the selected frame
1225 before other buffers. */
1227 tem = frame_buffer_list (frame);
1228 add_ons = Qnil;
1229 while (CONSP (tem))
1231 if (BUFFERP (XCAR (tem)))
1232 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1233 tem = XCDR (tem);
1235 tail = nconc2 (Fnreverse (add_ons), tail);
1237 for (; CONSP (tail); tail = XCDR (tail))
1239 buf = Fcdr (XCAR (tail));
1240 if (EQ (buf, buffer))
1241 continue;
1242 if (NILP (buf))
1243 continue;
1244 if (NILP (XBUFFER (buf)->name))
1245 continue;
1246 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1247 continue;
1248 /* If the selected frame has a buffer_predicate,
1249 disregard buffers that don't fit the predicate. */
1250 if (!NILP (pred))
1252 tem = call1 (pred, buf);
1253 if (NILP (tem))
1254 continue;
1257 if (NILP (visible_ok))
1258 tem = Fget_buffer_window (buf, Qvisible);
1259 else
1260 tem = Qnil;
1261 if (NILP (tem))
1262 return buf;
1263 if (NILP (notsogood))
1264 notsogood = buf;
1266 if (!NILP (notsogood))
1267 return notsogood;
1268 buf = Fget_buffer (build_string ("*scratch*"));
1269 if (NILP (buf))
1271 buf = Fget_buffer_create (build_string ("*scratch*"));
1272 Fset_buffer_major_mode (buf);
1274 return buf;
1277 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1278 0, 1, "",
1279 doc: /* Start keeping undo information for buffer BUFFER.
1280 No argument or nil as argument means do this for the current buffer. */)
1281 (buffer)
1282 register Lisp_Object buffer;
1284 Lisp_Object real_buffer;
1286 if (NILP (buffer))
1287 XSETBUFFER (real_buffer, current_buffer);
1288 else
1290 real_buffer = Fget_buffer (buffer);
1291 if (NILP (real_buffer))
1292 nsberror (buffer);
1295 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1296 XBUFFER (real_buffer)->undo_list = Qnil;
1298 return Qnil;
1302 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1303 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1304 The buffer being killed will be current while the hook is running.\n\
1305 See `kill-buffer'."
1307 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1308 doc: /* Kill the buffer BUFFER.
1309 The argument may be a buffer or may be the name of a buffer.
1310 An argument of nil means kill the current buffer.
1312 Value is t if the buffer is actually killed, nil if user says no.
1314 The value of `kill-buffer-hook' (which may be local to that buffer),
1315 if not void, is a list of functions to be called, with no arguments,
1316 before the buffer is actually killed. The buffer to be killed is current
1317 when the hook functions are called.
1319 Any processes that have this buffer as the `process-buffer' are killed
1320 with SIGHUP. */)
1321 (buffer)
1322 Lisp_Object buffer;
1324 Lisp_Object buf;
1325 register struct buffer *b;
1326 register Lisp_Object tem;
1327 register struct Lisp_Marker *m;
1328 struct gcpro gcpro1;
1330 if (NILP (buffer))
1331 buf = Fcurrent_buffer ();
1332 else
1333 buf = Fget_buffer (buffer);
1334 if (NILP (buf))
1335 nsberror (buffer);
1337 b = XBUFFER (buf);
1339 /* Avoid trouble for buffer already dead. */
1340 if (NILP (b->name))
1341 return Qnil;
1343 /* Query if the buffer is still modified. */
1344 if (INTERACTIVE && !NILP (b->filename)
1345 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1347 GCPRO1 (buf);
1348 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1349 b->name, make_number (0)));
1350 UNGCPRO;
1351 if (NILP (tem))
1352 return Qnil;
1355 /* Run hooks with the buffer to be killed the current buffer. */
1357 int count = SPECPDL_INDEX ();
1358 Lisp_Object arglist[1];
1360 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1361 set_buffer_internal (b);
1363 /* First run the query functions; if any query is answered no,
1364 don't kill the buffer. */
1365 arglist[0] = Qkill_buffer_query_functions;
1366 if (NILP (Frun_hook_with_args_until_failure (1, arglist)))
1367 return unbind_to (count, Qnil);
1369 /* Then run the hooks. */
1370 Frun_hooks (1, &Qkill_buffer_hook);
1371 unbind_to (count, Qnil);
1374 /* We have no more questions to ask. Verify that it is valid
1375 to kill the buffer. This must be done after the questions
1376 since anything can happen within do_yes_or_no_p. */
1378 /* Don't kill the minibuffer now current. */
1379 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1380 return Qnil;
1382 if (NILP (b->name))
1383 return Qnil;
1385 /* When we kill a base buffer, kill all its indirect buffers.
1386 We do it at this stage so nothing terrible happens if they
1387 ask questions or their hooks get errors. */
1388 if (! b->base_buffer)
1390 struct buffer *other;
1392 GCPRO1 (buf);
1394 for (other = all_buffers; other; other = other->next)
1395 /* all_buffers contains dead buffers too;
1396 don't re-kill them. */
1397 if (other->base_buffer == b && !NILP (other->name))
1399 Lisp_Object buf;
1400 XSETBUFFER (buf, other);
1401 Fkill_buffer (buf);
1404 UNGCPRO;
1407 /* Make this buffer not be current.
1408 In the process, notice if this is the sole visible buffer
1409 and give up if so. */
1410 if (b == current_buffer)
1412 tem = Fother_buffer (buf, Qnil, Qnil);
1413 Fset_buffer (tem);
1414 if (b == current_buffer)
1415 return Qnil;
1418 /* Notice if the buffer to kill is the sole visible buffer
1419 when we're currently in the mini-buffer, and give up if so. */
1420 XSETBUFFER (tem, current_buffer);
1421 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1423 tem = Fother_buffer (buf, Qnil, Qnil);
1424 if (EQ (buf, tem))
1425 return Qnil;
1428 /* Now there is no question: we can kill the buffer. */
1430 #ifdef CLASH_DETECTION
1431 /* Unlock this buffer's file, if it is locked. */
1432 unlock_buffer (b);
1433 #endif /* CLASH_DETECTION */
1435 kill_buffer_processes (buf);
1436 clear_charpos_cache (b);
1438 tem = Vinhibit_quit;
1439 Vinhibit_quit = Qt;
1440 replace_buffer_in_all_windows (buf);
1441 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1442 frames_discard_buffer (buf);
1443 Vinhibit_quit = tem;
1445 /* Delete any auto-save file, if we saved it in this session.
1446 But not if the buffer is modified. */
1447 if (STRINGP (b->auto_save_file_name)
1448 && b->auto_save_modified != 0
1449 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1450 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1451 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1453 Lisp_Object tem;
1454 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1455 if (! NILP (tem))
1456 internal_delete_file (b->auto_save_file_name);
1459 if (b->base_buffer)
1461 /* Unchain all markers that belong to this indirect buffer.
1462 Don't unchain the markers that belong to the base buffer
1463 or its other indirect buffers. */
1464 for (m = BUF_MARKERS (b); m; )
1466 struct Lisp_Marker *next = m->next;
1467 if (m->buffer == b)
1468 unchain_marker (m);
1469 m = next;
1472 else
1474 /* Unchain all markers of this buffer and its indirect buffers.
1475 and leave them pointing nowhere. */
1476 for (m = BUF_MARKERS (b); m; )
1478 struct Lisp_Marker *next = m->next;
1479 m->buffer = 0;
1480 m->next = NULL;
1481 m = next;
1483 BUF_MARKERS (b) = NULL;
1484 BUF_INTERVALS (b) = NULL_INTERVAL;
1486 /* Perhaps we should explicitly free the interval tree here... */
1489 /* Reset the local variables, so that this buffer's local values
1490 won't be protected from GC. They would be protected
1491 if they happened to remain encached in their symbols.
1492 This gets rid of them for certain. */
1493 swap_out_buffer_local_variables (b);
1494 reset_buffer_local_variables (b, 1);
1496 b->name = Qnil;
1498 BLOCK_INPUT;
1499 if (! b->base_buffer)
1500 free_buffer_text (b);
1502 if (b->newline_cache)
1504 free_region_cache (b->newline_cache);
1505 b->newline_cache = 0;
1507 if (b->width_run_cache)
1509 free_region_cache (b->width_run_cache);
1510 b->width_run_cache = 0;
1512 b->width_table = Qnil;
1513 UNBLOCK_INPUT;
1514 b->undo_list = Qnil;
1516 return Qt;
1519 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1520 we do this each time BUF is selected visibly, the more recently
1521 selected buffers are always closer to the front of the list. This
1522 means that other_buffer is more likely to choose a relevant buffer. */
1524 void
1525 record_buffer (buf)
1526 Lisp_Object buf;
1528 register Lisp_Object link, prev;
1529 Lisp_Object frame;
1530 frame = selected_frame;
1532 prev = Qnil;
1533 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1535 if (EQ (XCDR (XCAR (link)), buf))
1536 break;
1537 prev = link;
1540 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1541 we cannot use Fdelq itself here because it allows quitting. */
1543 if (NILP (prev))
1544 Vbuffer_alist = XCDR (Vbuffer_alist);
1545 else
1546 XSETCDR (prev, XCDR (XCDR (prev)));
1548 XSETCDR (link, Vbuffer_alist);
1549 Vbuffer_alist = link;
1551 /* Now move this buffer to the front of frame_buffer_list also. */
1553 prev = Qnil;
1554 for (link = frame_buffer_list (frame); CONSP (link);
1555 link = XCDR (link))
1557 if (EQ (XCAR (link), buf))
1558 break;
1559 prev = link;
1562 /* Effectively do delq. */
1564 if (CONSP (link))
1566 if (NILP (prev))
1567 set_frame_buffer_list (frame,
1568 XCDR (frame_buffer_list (frame)));
1569 else
1570 XSETCDR (prev, XCDR (XCDR (prev)));
1572 XSETCDR (link, frame_buffer_list (frame));
1573 set_frame_buffer_list (frame, link);
1575 else
1576 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1579 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1580 doc: /* Set an appropriate major mode for BUFFER.
1581 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1582 according to `default-major-mode'.
1583 Use this function before selecting the buffer, since it may need to inspect
1584 the current buffer's major mode. */)
1585 (buffer)
1586 Lisp_Object buffer;
1588 int count;
1589 Lisp_Object function;
1591 if (STRINGP (XBUFFER (buffer)->name)
1592 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1593 function = find_symbol_value (intern ("initial-major-mode"));
1594 else
1596 function = buffer_defaults.major_mode;
1597 if (NILP (function)
1598 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1599 function = current_buffer->major_mode;
1602 if (NILP (function) || EQ (function, Qfundamental_mode))
1603 return Qnil;
1605 count = SPECPDL_INDEX ();
1607 /* To select a nonfundamental mode,
1608 select the buffer temporarily and then call the mode function. */
1610 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1612 Fset_buffer (buffer);
1613 call0 (function);
1615 return unbind_to (count, Qnil);
1618 /* If switching buffers in WINDOW would be an error, return
1619 a C string saying what the error would be. */
1621 char *
1622 no_switch_window (window)
1623 Lisp_Object window;
1625 Lisp_Object tem;
1626 if (EQ (minibuf_window, window))
1627 return "Cannot switch buffers in minibuffer window";
1628 tem = Fwindow_dedicated_p (window);
1629 if (EQ (tem, Qt))
1630 return "Cannot switch buffers in a dedicated window";
1631 return NULL;
1634 /* Switch to buffer BUFFER in the selected window.
1635 If NORECORD is non-nil, don't call record_buffer. */
1637 Lisp_Object
1638 switch_to_buffer_1 (buffer, norecord)
1639 Lisp_Object buffer, norecord;
1641 register Lisp_Object buf;
1643 if (NILP (buffer))
1644 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1645 else
1647 buf = Fget_buffer (buffer);
1648 if (NILP (buf))
1650 buf = Fget_buffer_create (buffer);
1651 Fset_buffer_major_mode (buf);
1654 Fset_buffer (buf);
1655 if (NILP (norecord))
1656 record_buffer (buf);
1658 Fset_window_buffer (EQ (selected_window, minibuf_window)
1659 ? Fnext_window (minibuf_window, Qnil, Qnil)
1660 : selected_window,
1661 buf, Qnil);
1663 return buf;
1666 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1667 doc: /* Select buffer BUFFER in the current window.
1668 If BUFFER does not identify an existing buffer,
1669 then this function creates a buffer with that name.
1671 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1672 or nil. If BUFFER is nil, then this function chooses a buffer
1673 using `other-buffer'.
1674 Optional second arg NORECORD non-nil means
1675 do not put this buffer at the front of the list of recently selected ones.
1676 This function returns the buffer it switched to.
1678 WARNING: This is NOT the way to work on another buffer temporarily
1679 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1680 the window-buffer correspondences. */)
1681 (buffer, norecord)
1682 Lisp_Object buffer, norecord;
1684 char *err;
1686 if (EQ (buffer, Fwindow_buffer (selected_window)))
1687 /* Basically a NOP. Avoid signalling an error if the selected window
1688 is dedicated, or a minibuffer, ... */
1689 return Fset_buffer (buffer);
1691 err = no_switch_window (selected_window);
1692 if (err) error (err);
1694 return switch_to_buffer_1 (buffer, norecord);
1697 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1698 doc: /* Select buffer BUFFER in some window, preferably a different one.
1699 BUFFER may be a buffer, a string \(a buffer name), or nil.
1700 If BUFFER is a string which is not the name of an existing buffer,
1701 then this function creates a buffer with that name.
1702 If BUFFER is nil, then it chooses some other buffer.
1703 If `pop-up-windows' is non-nil, windows can be split to do this.
1704 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1705 window even if BUFFER is already visible in the selected window,
1706 and ignore `same-window-regexps' and `same-window-buffer-names'.
1707 This function returns the buffer it switched to.
1708 This uses the function `display-buffer' as a subroutine; see the documentation
1709 of `display-buffer' for additional customization information.
1711 Optional third arg NORECORD non-nil means
1712 do not put this buffer at the front of the list of recently selected ones. */)
1713 (buffer, other_window, norecord)
1714 Lisp_Object buffer, other_window, norecord;
1716 register Lisp_Object buf;
1717 if (NILP (buffer))
1718 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1719 else
1721 buf = Fget_buffer (buffer);
1722 if (NILP (buf))
1724 buf = Fget_buffer_create (buffer);
1725 Fset_buffer_major_mode (buf);
1728 Fset_buffer (buf);
1729 if (NILP (norecord))
1730 /* Why bother ? Fselect_window will do it for us anyway. -stef */
1731 record_buffer (buf);
1732 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1733 return buf;
1736 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1737 doc: /* Return the current buffer as a Lisp object. */)
1740 register Lisp_Object buf;
1741 XSETBUFFER (buf, current_buffer);
1742 return buf;
1745 /* Set the current buffer to B.
1747 We previously set windows_or_buffers_changed here to invalidate
1748 global unchanged information in beg_unchanged and end_unchanged.
1749 This is no longer necessary because we now compute unchanged
1750 information on a buffer-basis. Every action affecting other
1751 windows than the selected one requires a select_window at some
1752 time, and that increments windows_or_buffers_changed. */
1754 void
1755 set_buffer_internal (b)
1756 register struct buffer *b;
1758 if (current_buffer != b)
1759 set_buffer_internal_1 (b);
1762 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1763 This is used by redisplay. */
1765 void
1766 set_buffer_internal_1 (b)
1767 register struct buffer *b;
1769 register struct buffer *old_buf;
1770 register Lisp_Object tail, valcontents;
1771 Lisp_Object tem;
1773 #ifdef USE_MMAP_FOR_BUFFERS
1774 if (b->text->beg == NULL)
1775 enlarge_buffer_text (b, 0);
1776 #endif /* USE_MMAP_FOR_BUFFERS */
1778 if (current_buffer == b)
1779 return;
1781 old_buf = current_buffer;
1782 current_buffer = b;
1783 last_known_column_point = -1; /* invalidate indentation cache */
1785 if (old_buf)
1787 /* Put the undo list back in the base buffer, so that it appears
1788 that an indirect buffer shares the undo list of its base. */
1789 if (old_buf->base_buffer)
1790 old_buf->base_buffer->undo_list = old_buf->undo_list;
1792 /* If the old current buffer has markers to record PT, BEGV and ZV
1793 when it is not current, update them now. */
1794 if (! NILP (old_buf->pt_marker))
1796 Lisp_Object obuf;
1797 XSETBUFFER (obuf, old_buf);
1798 set_marker_both (old_buf->pt_marker, obuf,
1799 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1801 if (! NILP (old_buf->begv_marker))
1803 Lisp_Object obuf;
1804 XSETBUFFER (obuf, old_buf);
1805 set_marker_both (old_buf->begv_marker, obuf,
1806 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1808 if (! NILP (old_buf->zv_marker))
1810 Lisp_Object obuf;
1811 XSETBUFFER (obuf, old_buf);
1812 set_marker_both (old_buf->zv_marker, obuf,
1813 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1817 /* Get the undo list from the base buffer, so that it appears
1818 that an indirect buffer shares the undo list of its base. */
1819 if (b->base_buffer)
1820 b->undo_list = b->base_buffer->undo_list;
1822 /* If the new current buffer has markers to record PT, BEGV and ZV
1823 when it is not current, fetch them now. */
1824 if (! NILP (b->pt_marker))
1826 BUF_PT (b) = marker_position (b->pt_marker);
1827 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1829 if (! NILP (b->begv_marker))
1831 BUF_BEGV (b) = marker_position (b->begv_marker);
1832 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1834 if (! NILP (b->zv_marker))
1836 BUF_ZV (b) = marker_position (b->zv_marker);
1837 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1840 /* Look down buffer's list of local Lisp variables
1841 to find and update any that forward into C variables. */
1843 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1845 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1846 if ((BUFFER_LOCAL_VALUEP (valcontents)
1847 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1848 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1849 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1850 /* Just reference the variable
1851 to cause it to become set for this buffer. */
1852 Fsymbol_value (XCAR (XCAR (tail)));
1855 /* Do the same with any others that were local to the previous buffer */
1857 if (old_buf)
1858 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1860 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1861 if ((BUFFER_LOCAL_VALUEP (valcontents)
1862 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1863 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1864 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1865 /* Just reference the variable
1866 to cause it to become set for this buffer. */
1867 Fsymbol_value (XCAR (XCAR (tail)));
1871 /* Switch to buffer B temporarily for redisplay purposes.
1872 This avoids certain things that don't need to be done within redisplay. */
1874 void
1875 set_buffer_temp (b)
1876 struct buffer *b;
1878 register struct buffer *old_buf;
1880 if (current_buffer == b)
1881 return;
1883 old_buf = current_buffer;
1884 current_buffer = b;
1886 if (old_buf)
1888 /* If the old current buffer has markers to record PT, BEGV and ZV
1889 when it is not current, update them now. */
1890 if (! NILP (old_buf->pt_marker))
1892 Lisp_Object obuf;
1893 XSETBUFFER (obuf, old_buf);
1894 set_marker_both (old_buf->pt_marker, obuf,
1895 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1897 if (! NILP (old_buf->begv_marker))
1899 Lisp_Object obuf;
1900 XSETBUFFER (obuf, old_buf);
1901 set_marker_both (old_buf->begv_marker, obuf,
1902 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1904 if (! NILP (old_buf->zv_marker))
1906 Lisp_Object obuf;
1907 XSETBUFFER (obuf, old_buf);
1908 set_marker_both (old_buf->zv_marker, obuf,
1909 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1913 /* If the new current buffer has markers to record PT, BEGV and ZV
1914 when it is not current, fetch them now. */
1915 if (! NILP (b->pt_marker))
1917 BUF_PT (b) = marker_position (b->pt_marker);
1918 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1920 if (! NILP (b->begv_marker))
1922 BUF_BEGV (b) = marker_position (b->begv_marker);
1923 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1925 if (! NILP (b->zv_marker))
1927 BUF_ZV (b) = marker_position (b->zv_marker);
1928 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1932 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1933 doc: /* Make the buffer BUFFER current for editing operations.
1934 BUFFER may be a buffer or the name of an existing buffer.
1935 See also `save-excursion' when you want to make a buffer current temporarily.
1936 This function does not display the buffer, so its effect ends
1937 when the current command terminates.
1938 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
1939 (buffer)
1940 register Lisp_Object buffer;
1942 register Lisp_Object buf;
1943 buf = Fget_buffer (buffer);
1944 if (NILP (buf))
1945 nsberror (buffer);
1946 if (NILP (XBUFFER (buf)->name))
1947 error ("Selecting deleted buffer");
1948 set_buffer_internal (XBUFFER (buf));
1949 return buf;
1952 /* Set the current buffer to BUFFER provided it is alive. */
1954 Lisp_Object
1955 set_buffer_if_live (buffer)
1956 Lisp_Object buffer;
1958 if (! NILP (XBUFFER (buffer)->name))
1959 Fset_buffer (buffer);
1960 return Qnil;
1963 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1964 Sbarf_if_buffer_read_only, 0, 0, 0,
1965 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1968 if (!NILP (current_buffer->read_only)
1969 && NILP (Vinhibit_read_only))
1970 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1971 return Qnil;
1974 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1975 doc: /* Put BUFFER at the end of the list of all buffers.
1976 There it is the least likely candidate for `other-buffer' to return;
1977 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
1978 You can specify a buffer name as BUFFER, or an actual buffer object.
1979 If BUFFER is nil or omitted, bury the current buffer.
1980 Also, if BUFFER is nil or omitted, remove the current buffer from the
1981 selected window if it is displayed there. */)
1982 (buffer)
1983 register Lisp_Object buffer;
1985 /* Figure out what buffer we're going to bury. */
1986 if (NILP (buffer))
1988 Lisp_Object tem;
1989 XSETBUFFER (buffer, current_buffer);
1991 tem = Fwindow_buffer (selected_window);
1992 /* If we're burying the current buffer, unshow it. */
1993 if (EQ (buffer, tem))
1995 if (NILP (Fwindow_dedicated_p (selected_window)))
1996 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1997 else if (NILP (XWINDOW (selected_window)->parent))
1998 Ficonify_frame (Fwindow_frame (selected_window));
1999 else
2000 Fdelete_window (selected_window);
2003 else
2005 Lisp_Object buf1;
2007 buf1 = Fget_buffer (buffer);
2008 if (NILP (buf1))
2009 nsberror (buffer);
2010 buffer = buf1;
2013 /* Move buffer to the end of the buffer list. Do nothing if the
2014 buffer is killed. */
2015 if (!NILP (XBUFFER (buffer)->name))
2017 Lisp_Object aelt, link;
2019 aelt = Frassq (buffer, Vbuffer_alist);
2020 link = Fmemq (aelt, Vbuffer_alist);
2021 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2022 XSETCDR (link, Qnil);
2023 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2025 /* Removing BUFFER from frame-specific lists
2026 has the effect of putting BUFFER at the end
2027 of the combined list in each frame. */
2028 frames_discard_buffer (buffer);
2031 return Qnil;
2034 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2035 doc: /* Delete the entire contents of the current buffer.
2036 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2037 so the buffer is truly empty after this. */)
2040 Fwiden ();
2042 del_range (BEG, Z);
2044 current_buffer->last_window_start = 1;
2045 /* Prevent warnings, or suspension of auto saving, that would happen
2046 if future size is less than past size. Use of erase-buffer
2047 implies that the future text is not really related to the past text. */
2048 XSETFASTINT (current_buffer->save_length, 0);
2049 return Qnil;
2052 void
2053 validate_region (b, e)
2054 register Lisp_Object *b, *e;
2056 CHECK_NUMBER_COERCE_MARKER (*b);
2057 CHECK_NUMBER_COERCE_MARKER (*e);
2059 if (XINT (*b) > XINT (*e))
2061 Lisp_Object tem;
2062 tem = *b; *b = *e; *e = tem;
2065 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2066 && XINT (*e) <= ZV))
2067 args_out_of_range (*b, *e);
2070 /* Advance BYTE_POS up to a character boundary
2071 and return the adjusted position. */
2073 static int
2074 advance_to_char_boundary (byte_pos)
2075 int byte_pos;
2077 int c;
2079 if (byte_pos == BEG)
2080 /* Beginning of buffer is always a character boundary. */
2081 return BEG;
2083 c = FETCH_BYTE (byte_pos);
2084 if (! CHAR_HEAD_P (c))
2086 /* We should advance BYTE_POS only when C is a constituent of a
2087 multibyte sequence. */
2088 int orig_byte_pos = byte_pos;
2092 byte_pos--;
2093 c = FETCH_BYTE (byte_pos);
2095 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2096 INC_POS (byte_pos);
2097 if (byte_pos < orig_byte_pos)
2098 byte_pos = orig_byte_pos;
2099 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2100 surely advance to the correct character boundary. If C is
2101 not, BYTE_POS was unchanged. */
2104 return byte_pos;
2107 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2108 1, 1, 0,
2109 doc: /* Set the multibyte flag of the current buffer to FLAG.
2110 If FLAG is t, this makes the buffer a multibyte buffer.
2111 If FLAG is nil, this makes the buffer a single-byte buffer.
2112 The buffer contents remain unchanged as a sequence of bytes
2113 but the contents viewed as characters do change.
2114 If the multibyte flag was really changed, undo information of the
2115 current buffer is cleared. */)
2116 (flag)
2117 Lisp_Object flag;
2119 struct Lisp_Marker *tail, *markers;
2120 struct buffer *other;
2121 int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
2122 int begv, zv;
2123 int narrowed = (BEG != BEGV || Z != ZV);
2124 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2126 if (current_buffer->base_buffer)
2127 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2129 /* Do nothing if nothing actually changes. */
2130 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2131 return flag;
2133 /* It would be better to update the list,
2134 but this is good enough for now. */
2135 if (undo_enabled_p)
2136 current_buffer->undo_list = Qt;
2138 /* If the cached position is for this buffer, clear it out. */
2139 clear_charpos_cache (current_buffer);
2141 if (NILP (flag))
2142 begv = BEGV_BYTE, zv = ZV_BYTE;
2143 else
2144 begv = BEGV, zv = ZV;
2146 if (narrowed)
2147 Fwiden ();
2149 if (NILP (flag))
2151 int pos, stop;
2152 unsigned char *p;
2154 /* Do this first, so it can use CHAR_TO_BYTE
2155 to calculate the old correspondences. */
2156 set_intervals_multibyte (0);
2158 current_buffer->enable_multibyte_characters = Qnil;
2160 Z = Z_BYTE;
2161 BEGV = BEGV_BYTE;
2162 ZV = ZV_BYTE;
2163 GPT = GPT_BYTE;
2164 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2167 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2168 tail->charpos = tail->bytepos;
2170 /* Convert multibyte form of 8-bit characters to unibyte. */
2171 pos = BEG;
2172 stop = GPT;
2173 p = BEG_ADDR;
2174 while (1)
2176 int c, bytes;
2178 if (pos == stop)
2180 if (pos == Z)
2181 break;
2182 p = GAP_END_ADDR;
2183 stop = Z;
2185 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
2186 p += bytes, pos += bytes;
2187 else
2189 c = STRING_CHAR (p, stop - pos);
2190 /* Delete all bytes for this 8-bit character but the
2191 last one, and change the last one to the charcter
2192 code. */
2193 bytes--;
2194 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2195 p = GAP_END_ADDR;
2196 *p++ = c;
2197 pos++;
2198 if (begv > pos)
2199 begv -= bytes;
2200 if (zv > pos)
2201 zv -= bytes;
2202 stop = Z;
2205 if (narrowed)
2206 Fnarrow_to_region (make_number (begv), make_number (zv));
2208 else
2210 int pt = PT;
2211 int pos, stop;
2212 unsigned char *p;
2214 /* Be sure not to have a multibyte sequence striding over the GAP.
2215 Ex: We change this: "...abc\201 _GAP_ \241def..."
2216 to: "...abc _GAP_ \201\241def..." */
2218 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2219 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2221 unsigned char *p = GPT_ADDR - 1;
2223 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2224 if (BASE_LEADING_CODE_P (*p))
2226 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2228 move_gap_both (new_gpt, new_gpt);
2232 /* Make the buffer contents valid as multibyte by converting
2233 8-bit characters to multibyte form. */
2234 pos = BEG;
2235 stop = GPT;
2236 p = BEG_ADDR;
2237 while (1)
2239 int bytes;
2241 if (pos == stop)
2243 if (pos == Z)
2244 break;
2245 p = GAP_END_ADDR;
2246 stop = Z;
2249 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2250 p += bytes, pos += bytes;
2251 else
2253 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2255 bytes = CHAR_STRING (*p, tmp);
2256 *p = tmp[0];
2257 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2258 bytes--;
2259 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2260 /* Now the gap is after the just inserted data. */
2261 pos = GPT;
2262 p = GAP_END_ADDR;
2263 if (pos <= begv)
2264 begv += bytes;
2265 if (pos <= zv)
2266 zv += bytes;
2267 if (pos <= pt)
2268 pt += bytes;
2269 stop = Z;
2273 if (pt != PT)
2274 TEMP_SET_PT (pt);
2276 if (narrowed)
2277 Fnarrow_to_region (make_number (begv), make_number (zv));
2279 /* Do this first, so that chars_in_text asks the right question.
2280 set_intervals_multibyte needs it too. */
2281 current_buffer->enable_multibyte_characters = Qt;
2283 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2284 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2286 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2288 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2289 if (BEGV_BYTE > GPT_BYTE)
2290 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2291 else
2292 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2294 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2295 if (ZV_BYTE > GPT_BYTE)
2296 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2297 else
2298 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2301 int pt_byte = advance_to_char_boundary (PT_BYTE);
2302 int pt;
2304 if (pt_byte > GPT_BYTE)
2305 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2306 else
2307 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2308 TEMP_SET_PT_BOTH (pt, pt_byte);
2311 tail = markers = BUF_MARKERS (current_buffer);
2313 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2314 getting confused by the markers that have not yet been updated.
2315 It is also a signal that it should never create a marker. */
2316 BUF_MARKERS (current_buffer) = NULL;
2318 for (; tail; tail = tail->next)
2320 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2321 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2324 /* Make sure no markers were put on the chain
2325 while the chain value was incorrect. */
2326 if (BUF_MARKERS (current_buffer))
2327 abort ();
2329 BUF_MARKERS (current_buffer) = markers;
2331 /* Do this last, so it can calculate the new correspondences
2332 between chars and bytes. */
2333 set_intervals_multibyte (1);
2336 if (undo_enabled_p)
2337 current_buffer->undo_list = Qnil;
2339 /* Changing the multibyteness of a buffer means that all windows
2340 showing that buffer must be updated thoroughly. */
2341 current_buffer->prevent_redisplay_optimizations_p = 1;
2342 ++windows_or_buffers_changed;
2344 /* Copy this buffer's new multibyte status
2345 into all of its indirect buffers. */
2346 for (other = all_buffers; other; other = other->next)
2347 if (other->base_buffer == current_buffer && !NILP (other->name))
2349 other->enable_multibyte_characters
2350 = current_buffer->enable_multibyte_characters;
2351 other->prevent_redisplay_optimizations_p = 1;
2354 /* Restore the modifiedness of the buffer. */
2355 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2356 Fset_buffer_modified_p (Qnil);
2358 #ifdef subprocesses
2359 /* Update coding systems of this buffer's process (if any). */
2361 Lisp_Object process;
2363 process = Fget_buffer_process (Fcurrent_buffer ());
2364 if (PROCESSP (process))
2365 setup_process_coding_systems (process);
2367 #endif /* subprocesses */
2369 return flag;
2372 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2373 0, 0, 0,
2374 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2375 Most local variable bindings are eliminated so that the default values
2376 become effective once more. Also, the syntax table is set from
2377 `standard-syntax-table', the local keymap is set to nil,
2378 and the abbrev table from `fundamental-mode-abbrev-table'.
2379 This function also forces redisplay of the mode line.
2381 Every function to select a new major mode starts by
2382 calling this function.
2384 As a special exception, local variables whose names have
2385 a non-nil `permanent-local' property are not eliminated by this function.
2387 The first thing this function does is run
2388 the normal hook `change-major-mode-hook'. */)
2391 register Lisp_Object alist, sym, tem;
2392 Lisp_Object oalist;
2394 if (!NILP (Vrun_hooks))
2395 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
2396 oalist = current_buffer->local_var_alist;
2398 /* Make sure none of the bindings in oalist
2399 remain swapped in, in their symbols. */
2401 swap_out_buffer_local_variables (current_buffer);
2403 /* Actually eliminate all local bindings of this buffer. */
2405 reset_buffer_local_variables (current_buffer, 0);
2407 /* Any which are supposed to be permanent,
2408 make local again, with the same values they had. */
2410 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2412 sym = XCAR (XCAR (alist));
2413 tem = Fget (sym, Qpermanent_local);
2414 if (! NILP (tem))
2416 Fmake_local_variable (sym);
2417 Fset (sym, XCDR (XCAR (alist)));
2421 /* Force mode-line redisplay. Useful here because all major mode
2422 commands call this function. */
2423 update_mode_lines++;
2425 return Qnil;
2428 /* Make sure no local variables remain set up with buffer B
2429 for their current values. */
2431 static void
2432 swap_out_buffer_local_variables (b)
2433 struct buffer *b;
2435 Lisp_Object oalist, alist, sym, tem, buffer;
2437 XSETBUFFER (buffer, b);
2438 oalist = b->local_var_alist;
2440 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2442 sym = XCAR (XCAR (alist));
2444 /* Need not do anything if some other buffer's binding is now encached. */
2445 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2446 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2448 /* Symbol is set up for this buffer's old local value.
2449 Set it up for the current buffer with the default value. */
2451 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->cdr;
2452 /* Store the symbol's current value into the alist entry
2453 it is currently set up for. This is so that, if the
2454 local is marked permanent, and we make it local again
2455 later in Fkill_all_local_variables, we don't lose the value. */
2456 XSETCDR (XCAR (tem),
2457 do_symval_forwarding (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue));
2458 /* Switch to the symbol's default-value alist entry. */
2459 XSETCAR (tem, tem);
2460 /* Mark it as current for buffer B. */
2461 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer = buffer;
2462 /* Store the current value into any forwarding in the symbol. */
2463 store_symval_forwarding (sym,
2464 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue,
2465 XCDR (tem), NULL);
2470 /* Find all the overlays in the current buffer that contain position POS.
2471 Return the number found, and store them in a vector in *VEC_PTR.
2472 Store in *LEN_PTR the size allocated for the vector.
2473 Store in *NEXT_PTR the next position after POS where an overlay starts,
2474 or ZV if there are no more overlays.
2475 Store in *PREV_PTR the previous position before POS where an overlay ends,
2476 or where an overlay starts which ends at or after POS;
2477 or BEGV if there are no such overlays.
2478 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2480 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2481 when this function is called.
2483 If EXTEND is non-zero, we make the vector bigger if necessary.
2484 If EXTEND is zero, we never extend the vector,
2485 and we store only as many overlays as will fit.
2486 But we still return the total number of overlays.
2488 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2489 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2490 default (BEGV or ZV). */
2493 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2494 EMACS_INT pos;
2495 int extend;
2496 Lisp_Object **vec_ptr;
2497 int *len_ptr;
2498 int *next_ptr;
2499 int *prev_ptr;
2500 int change_req;
2502 Lisp_Object overlay, start, end;
2503 struct Lisp_Overlay *tail;
2504 int idx = 0;
2505 int len = *len_ptr;
2506 Lisp_Object *vec = *vec_ptr;
2507 int next = ZV;
2508 int prev = BEGV;
2509 int inhibit_storing = 0;
2511 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2513 int startpos, endpos;
2515 XSETMISC (overlay, tail);
2517 start = OVERLAY_START (overlay);
2518 end = OVERLAY_END (overlay);
2519 endpos = OVERLAY_POSITION (end);
2520 if (endpos < pos)
2522 if (prev < endpos)
2523 prev = endpos;
2524 break;
2526 startpos = OVERLAY_POSITION (start);
2527 /* This one ends at or after POS
2528 so its start counts for PREV_PTR if it's before POS. */
2529 if (prev < startpos && startpos < pos)
2530 prev = startpos;
2531 if (endpos == pos)
2532 continue;
2533 if (startpos <= pos)
2535 if (idx == len)
2537 /* The supplied vector is full.
2538 Either make it bigger, or don't store any more in it. */
2539 if (extend)
2541 /* Make it work with an initial len == 0. */
2542 len *= 2;
2543 if (len == 0)
2544 len = 4;
2545 *len_ptr = len;
2546 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2547 *vec_ptr = vec;
2549 else
2550 inhibit_storing = 1;
2553 if (!inhibit_storing)
2554 vec[idx] = overlay;
2555 /* Keep counting overlays even if we can't return them all. */
2556 idx++;
2558 else if (startpos < next)
2559 next = startpos;
2562 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2564 int startpos, endpos;
2566 XSETMISC (overlay, tail);
2568 start = OVERLAY_START (overlay);
2569 end = OVERLAY_END (overlay);
2570 startpos = OVERLAY_POSITION (start);
2571 if (pos < startpos)
2573 if (startpos < next)
2574 next = startpos;
2575 break;
2577 endpos = OVERLAY_POSITION (end);
2578 if (pos < endpos)
2580 if (idx == len)
2582 if (extend)
2584 /* Make it work with an initial len == 0. */
2585 len *= 2;
2586 if (len == 0)
2587 len = 4;
2588 *len_ptr = len;
2589 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2590 *vec_ptr = vec;
2592 else
2593 inhibit_storing = 1;
2596 if (!inhibit_storing)
2597 vec[idx] = overlay;
2598 idx++;
2600 if (startpos < pos && startpos > prev)
2601 prev = startpos;
2603 else if (endpos < pos && endpos > prev)
2604 prev = endpos;
2605 else if (endpos == pos && startpos > prev
2606 && (!change_req || startpos < pos))
2607 prev = startpos;
2610 if (next_ptr)
2611 *next_ptr = next;
2612 if (prev_ptr)
2613 *prev_ptr = prev;
2614 return idx;
2617 /* Find all the overlays in the current buffer that overlap the range BEG-END
2618 or are empty at BEG.
2620 Return the number found, and store them in a vector in *VEC_PTR.
2621 Store in *LEN_PTR the size allocated for the vector.
2622 Store in *NEXT_PTR the next position after POS where an overlay starts,
2623 or ZV if there are no more overlays.
2624 Store in *PREV_PTR the previous position before POS where an overlay ends,
2625 or BEGV if there are no previous overlays.
2626 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2628 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2629 when this function is called.
2631 If EXTEND is non-zero, we make the vector bigger if necessary.
2632 If EXTEND is zero, we never extend the vector,
2633 and we store only as many overlays as will fit.
2634 But we still return the total number of overlays. */
2636 static int
2637 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2638 int beg, end;
2639 int extend;
2640 Lisp_Object **vec_ptr;
2641 int *len_ptr;
2642 int *next_ptr;
2643 int *prev_ptr;
2645 Lisp_Object overlay, ostart, oend;
2646 struct Lisp_Overlay *tail;
2647 int idx = 0;
2648 int len = *len_ptr;
2649 Lisp_Object *vec = *vec_ptr;
2650 int next = ZV;
2651 int prev = BEGV;
2652 int inhibit_storing = 0;
2654 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2656 int startpos, endpos;
2658 XSETMISC (overlay, tail);
2660 ostart = OVERLAY_START (overlay);
2661 oend = OVERLAY_END (overlay);
2662 endpos = OVERLAY_POSITION (oend);
2663 if (endpos < beg)
2665 if (prev < endpos)
2666 prev = endpos;
2667 break;
2669 startpos = OVERLAY_POSITION (ostart);
2670 /* Count an interval if it either overlaps the range
2671 or is empty at the start of the range. */
2672 if ((beg < endpos && startpos < end)
2673 || (startpos == endpos && beg == endpos))
2675 if (idx == len)
2677 /* The supplied vector is full.
2678 Either make it bigger, or don't store any more in it. */
2679 if (extend)
2681 /* Make it work with an initial len == 0. */
2682 len *= 2;
2683 if (len == 0)
2684 len = 4;
2685 *len_ptr = len;
2686 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2687 *vec_ptr = vec;
2689 else
2690 inhibit_storing = 1;
2693 if (!inhibit_storing)
2694 vec[idx] = overlay;
2695 /* Keep counting overlays even if we can't return them all. */
2696 idx++;
2698 else if (startpos < next)
2699 next = startpos;
2702 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2704 int startpos, endpos;
2706 XSETMISC (overlay, tail);
2708 ostart = OVERLAY_START (overlay);
2709 oend = OVERLAY_END (overlay);
2710 startpos = OVERLAY_POSITION (ostart);
2711 if (end < startpos)
2713 if (startpos < next)
2714 next = startpos;
2715 break;
2717 endpos = OVERLAY_POSITION (oend);
2718 /* Count an interval if it either overlaps the range
2719 or is empty at the start of the range. */
2720 if ((beg < endpos && startpos < end)
2721 || (startpos == endpos && beg == endpos))
2723 if (idx == len)
2725 if (extend)
2727 /* Make it work with an initial len == 0. */
2728 len *= 2;
2729 if (len == 0)
2730 len = 4;
2731 *len_ptr = len;
2732 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2733 *vec_ptr = vec;
2735 else
2736 inhibit_storing = 1;
2739 if (!inhibit_storing)
2740 vec[idx] = overlay;
2741 idx++;
2743 else if (endpos < beg && endpos > prev)
2744 prev = endpos;
2747 if (next_ptr)
2748 *next_ptr = next;
2749 if (prev_ptr)
2750 *prev_ptr = prev;
2751 return idx;
2755 /* Return non-zero if there exists an overlay with a non-nil
2756 `mouse-face' property overlapping OVERLAY. */
2759 mouse_face_overlay_overlaps (overlay)
2760 Lisp_Object overlay;
2762 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2763 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2764 int n, i, size;
2765 Lisp_Object *v, tem;
2767 size = 10;
2768 v = (Lisp_Object *) alloca (size * sizeof *v);
2769 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2770 if (n > size)
2772 v = (Lisp_Object *) alloca (n * sizeof *v);
2773 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2776 for (i = 0; i < n; ++i)
2777 if (!EQ (v[i], overlay)
2778 && (tem = Foverlay_get (overlay, Qmouse_face),
2779 !NILP (tem)))
2780 break;
2782 return i < n;
2787 /* Fast function to just test if we're at an overlay boundary. */
2789 overlay_touches_p (pos)
2790 int pos;
2792 Lisp_Object overlay;
2793 struct Lisp_Overlay *tail;
2795 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2797 int endpos;
2799 XSETMISC (overlay ,tail);
2800 if (!GC_OVERLAYP (overlay))
2801 abort ();
2803 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2804 if (endpos < pos)
2805 break;
2806 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2807 return 1;
2810 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2812 int startpos;
2814 XSETMISC (overlay, tail);
2815 if (!GC_OVERLAYP (overlay))
2816 abort ();
2818 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2819 if (pos < startpos)
2820 break;
2821 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2822 return 1;
2824 return 0;
2827 struct sortvec
2829 Lisp_Object overlay;
2830 int beg, end;
2831 int priority;
2834 static int
2835 compare_overlays (v1, v2)
2836 const void *v1, *v2;
2838 const struct sortvec *s1 = (const struct sortvec *) v1;
2839 const struct sortvec *s2 = (const struct sortvec *) v2;
2840 if (s1->priority != s2->priority)
2841 return s1->priority - s2->priority;
2842 if (s1->beg != s2->beg)
2843 return s1->beg - s2->beg;
2844 if (s1->end != s2->end)
2845 return s2->end - s1->end;
2846 return 0;
2849 /* Sort an array of overlays by priority. The array is modified in place.
2850 The return value is the new size; this may be smaller than the original
2851 size if some of the overlays were invalid or were window-specific. */
2853 sort_overlays (overlay_vec, noverlays, w)
2854 Lisp_Object *overlay_vec;
2855 int noverlays;
2856 struct window *w;
2858 int i, j;
2859 struct sortvec *sortvec;
2860 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2862 /* Put the valid and relevant overlays into sortvec. */
2864 for (i = 0, j = 0; i < noverlays; i++)
2866 Lisp_Object tem;
2867 Lisp_Object overlay;
2869 overlay = overlay_vec[i];
2870 if (OVERLAY_VALID (overlay)
2871 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2872 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2874 /* If we're interested in a specific window, then ignore
2875 overlays that are limited to some other window. */
2876 if (w)
2878 Lisp_Object window;
2880 window = Foverlay_get (overlay, Qwindow);
2881 if (WINDOWP (window) && XWINDOW (window) != w)
2882 continue;
2885 /* This overlay is good and counts: put it into sortvec. */
2886 sortvec[j].overlay = overlay;
2887 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2888 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2889 tem = Foverlay_get (overlay, Qpriority);
2890 if (INTEGERP (tem))
2891 sortvec[j].priority = XINT (tem);
2892 else
2893 sortvec[j].priority = 0;
2894 j++;
2897 noverlays = j;
2899 /* Sort the overlays into the proper order: increasing priority. */
2901 if (noverlays > 1)
2902 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2904 for (i = 0; i < noverlays; i++)
2905 overlay_vec[i] = sortvec[i].overlay;
2906 return (noverlays);
2909 struct sortstr
2911 Lisp_Object string, string2;
2912 int size;
2913 int priority;
2916 struct sortstrlist
2918 struct sortstr *buf; /* An array that expands as needed; never freed. */
2919 int size; /* Allocated length of that array. */
2920 int used; /* How much of the array is currently in use. */
2921 int bytes; /* Total length of the strings in buf. */
2924 /* Buffers for storing information about the overlays touching a given
2925 position. These could be automatic variables in overlay_strings, but
2926 it's more efficient to hold onto the memory instead of repeatedly
2927 allocating and freeing it. */
2928 static struct sortstrlist overlay_heads, overlay_tails;
2929 static unsigned char *overlay_str_buf;
2931 /* Allocated length of overlay_str_buf. */
2932 static int overlay_str_len;
2934 /* A comparison function suitable for passing to qsort. */
2935 static int
2936 cmp_for_strings (as1, as2)
2937 char *as1, *as2;
2939 struct sortstr *s1 = (struct sortstr *)as1;
2940 struct sortstr *s2 = (struct sortstr *)as2;
2941 if (s1->size != s2->size)
2942 return s2->size - s1->size;
2943 if (s1->priority != s2->priority)
2944 return s1->priority - s2->priority;
2945 return 0;
2948 static void
2949 record_overlay_string (ssl, str, str2, pri, size)
2950 struct sortstrlist *ssl;
2951 Lisp_Object str, str2, pri;
2952 int size;
2954 int nbytes;
2956 if (ssl->used == ssl->size)
2958 if (ssl->buf)
2959 ssl->size *= 2;
2960 else
2961 ssl->size = 5;
2962 ssl->buf = ((struct sortstr *)
2963 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2965 ssl->buf[ssl->used].string = str;
2966 ssl->buf[ssl->used].string2 = str2;
2967 ssl->buf[ssl->used].size = size;
2968 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2969 ssl->used++;
2971 if (NILP (current_buffer->enable_multibyte_characters))
2972 nbytes = SCHARS (str);
2973 else if (! STRING_MULTIBYTE (str))
2974 nbytes = count_size_as_multibyte (SDATA (str),
2975 SBYTES (str));
2976 else
2977 nbytes = SBYTES (str);
2979 ssl->bytes += nbytes;
2981 if (STRINGP (str2))
2983 if (NILP (current_buffer->enable_multibyte_characters))
2984 nbytes = SCHARS (str2);
2985 else if (! STRING_MULTIBYTE (str2))
2986 nbytes = count_size_as_multibyte (SDATA (str2),
2987 SBYTES (str2));
2988 else
2989 nbytes = SBYTES (str2);
2991 ssl->bytes += nbytes;
2995 /* Return the concatenation of the strings associated with overlays that
2996 begin or end at POS, ignoring overlays that are specific to a window
2997 other than W. The strings are concatenated in the appropriate order:
2998 shorter overlays nest inside longer ones, and higher priority inside
2999 lower. Normally all of the after-strings come first, but zero-sized
3000 overlays have their after-strings ride along with the before-strings
3001 because it would look strange to print them inside-out.
3003 Returns the string length, and stores the contents indirectly through
3004 PSTR, if that variable is non-null. The string may be overwritten by
3005 subsequent calls. */
3008 overlay_strings (pos, w, pstr)
3009 EMACS_INT pos;
3010 struct window *w;
3011 unsigned char **pstr;
3013 Lisp_Object overlay, window, str;
3014 struct Lisp_Overlay *ov;
3015 int startpos, endpos;
3016 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3018 overlay_heads.used = overlay_heads.bytes = 0;
3019 overlay_tails.used = overlay_tails.bytes = 0;
3020 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3022 XSETMISC (overlay, ov);
3023 eassert (OVERLAYP (overlay));
3025 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3026 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3027 if (endpos < pos)
3028 break;
3029 if (endpos != pos && startpos != pos)
3030 continue;
3031 window = Foverlay_get (overlay, Qwindow);
3032 if (WINDOWP (window) && XWINDOW (window) != w)
3033 continue;
3034 if (startpos == pos
3035 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3036 record_overlay_string (&overlay_heads, str,
3037 (startpos == endpos
3038 ? Foverlay_get (overlay, Qafter_string)
3039 : Qnil),
3040 Foverlay_get (overlay, Qpriority),
3041 endpos - startpos);
3042 else if (endpos == pos
3043 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3044 record_overlay_string (&overlay_tails, str, Qnil,
3045 Foverlay_get (overlay, Qpriority),
3046 endpos - startpos);
3048 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3050 XSETMISC (overlay, ov);
3051 eassert (OVERLAYP (overlay));
3053 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3054 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3055 if (startpos > pos)
3056 break;
3057 if (endpos != pos && startpos != pos)
3058 continue;
3059 window = Foverlay_get (overlay, Qwindow);
3060 if (WINDOWP (window) && XWINDOW (window) != w)
3061 continue;
3062 if (startpos == pos
3063 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3064 record_overlay_string (&overlay_heads, str,
3065 (startpos == endpos
3066 ? Foverlay_get (overlay, Qafter_string)
3067 : Qnil),
3068 Foverlay_get (overlay, Qpriority),
3069 endpos - startpos);
3070 else if (endpos == pos
3071 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3072 record_overlay_string (&overlay_tails, str, Qnil,
3073 Foverlay_get (overlay, Qpriority),
3074 endpos - startpos);
3076 if (overlay_tails.used > 1)
3077 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3078 cmp_for_strings);
3079 if (overlay_heads.used > 1)
3080 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3081 cmp_for_strings);
3082 if (overlay_heads.bytes || overlay_tails.bytes)
3084 Lisp_Object tem;
3085 int i;
3086 unsigned char *p;
3087 int total = overlay_heads.bytes + overlay_tails.bytes;
3089 if (total > overlay_str_len)
3091 overlay_str_len = total;
3092 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3093 total);
3095 p = overlay_str_buf;
3096 for (i = overlay_tails.used; --i >= 0;)
3098 int nbytes;
3099 tem = overlay_tails.buf[i].string;
3100 nbytes = copy_text (SDATA (tem), p,
3101 SBYTES (tem),
3102 STRING_MULTIBYTE (tem), multibyte);
3103 p += nbytes;
3105 for (i = 0; i < overlay_heads.used; ++i)
3107 int nbytes;
3108 tem = overlay_heads.buf[i].string;
3109 nbytes = copy_text (SDATA (tem), p,
3110 SBYTES (tem),
3111 STRING_MULTIBYTE (tem), multibyte);
3112 p += nbytes;
3113 tem = overlay_heads.buf[i].string2;
3114 if (STRINGP (tem))
3116 nbytes = copy_text (SDATA (tem), p,
3117 SBYTES (tem),
3118 STRING_MULTIBYTE (tem), multibyte);
3119 p += nbytes;
3122 if (p != overlay_str_buf + total)
3123 abort ();
3124 if (pstr)
3125 *pstr = overlay_str_buf;
3126 return total;
3128 return 0;
3131 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3133 void
3134 recenter_overlay_lists (buf, pos)
3135 struct buffer *buf;
3136 EMACS_INT pos;
3138 Lisp_Object overlay, beg, end;
3139 struct Lisp_Overlay *prev, *tail, *next;
3141 /* See if anything in overlays_before should move to overlays_after. */
3143 /* We don't strictly need prev in this loop; it should always be nil.
3144 But we use it for symmetry and in case that should cease to be true
3145 with some future change. */
3146 prev = NULL;
3147 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3149 next = tail->next;
3150 XSETMISC (overlay, tail);
3152 /* If the overlay is not valid, get rid of it. */
3153 if (!OVERLAY_VALID (overlay))
3154 #if 1
3155 abort ();
3156 #else
3158 /* Splice the cons cell TAIL out of overlays_before. */
3159 if (!NILP (prev))
3160 XCDR (prev) = next;
3161 else
3162 buf->overlays_before = next;
3163 tail = prev;
3164 continue;
3166 #endif
3168 beg = OVERLAY_START (overlay);
3169 end = OVERLAY_END (overlay);
3171 if (OVERLAY_POSITION (end) > pos)
3173 /* OVERLAY needs to be moved. */
3174 int where = OVERLAY_POSITION (beg);
3175 struct Lisp_Overlay *other, *other_prev;
3177 /* Splice the cons cell TAIL out of overlays_before. */
3178 if (prev)
3179 prev->next = next;
3180 else
3181 buf->overlays_before = next;
3183 /* Search thru overlays_after for where to put it. */
3184 other_prev = NULL;
3185 for (other = buf->overlays_after; other;
3186 other_prev = other, other = other->next)
3188 Lisp_Object otherbeg, otheroverlay;
3190 XSETMISC (otheroverlay, other);
3191 eassert (OVERLAY_VALID (otheroverlay));
3193 otherbeg = OVERLAY_START (otheroverlay);
3194 if (OVERLAY_POSITION (otherbeg) >= where)
3195 break;
3198 /* Add TAIL to overlays_after before OTHER. */
3199 tail->next = other;
3200 if (other_prev)
3201 other_prev->next = tail;
3202 else
3203 buf->overlays_after = tail;
3204 tail = prev;
3206 else
3207 /* We've reached the things that should stay in overlays_before.
3208 All the rest of overlays_before must end even earlier,
3209 so stop now. */
3210 break;
3213 /* See if anything in overlays_after should be in overlays_before. */
3214 prev = NULL;
3215 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3217 next = tail->next;
3218 XSETMISC (overlay, tail);
3220 /* If the overlay is not valid, get rid of it. */
3221 if (!OVERLAY_VALID (overlay))
3222 #if 1
3223 abort ();
3224 #else
3226 /* Splice the cons cell TAIL out of overlays_after. */
3227 if (!NILP (prev))
3228 XCDR (prev) = next;
3229 else
3230 buf->overlays_after = next;
3231 tail = prev;
3232 continue;
3234 #endif
3236 beg = OVERLAY_START (overlay);
3237 end = OVERLAY_END (overlay);
3239 /* Stop looking, when we know that nothing further
3240 can possibly end before POS. */
3241 if (OVERLAY_POSITION (beg) > pos)
3242 break;
3244 if (OVERLAY_POSITION (end) <= pos)
3246 /* OVERLAY needs to be moved. */
3247 int where = OVERLAY_POSITION (end);
3248 struct Lisp_Overlay *other, *other_prev;
3250 /* Splice the cons cell TAIL out of overlays_after. */
3251 if (prev)
3252 prev->next = next;
3253 else
3254 buf->overlays_after = next;
3256 /* Search thru overlays_before for where to put it. */
3257 other_prev = NULL;
3258 for (other = buf->overlays_before; other;
3259 other_prev = other, other = other->next)
3261 Lisp_Object otherend, otheroverlay;
3263 XSETMISC (otheroverlay, other);
3264 eassert (OVERLAY_VALID (otheroverlay));
3266 otherend = OVERLAY_END (otheroverlay);
3267 if (OVERLAY_POSITION (otherend) <= where)
3268 break;
3271 /* Add TAIL to overlays_before before OTHER. */
3272 tail->next = other;
3273 if (other_prev)
3274 other_prev->next = tail;
3275 else
3276 buf->overlays_before = tail;
3277 tail = prev;
3281 buf->overlay_center = pos;
3284 void
3285 adjust_overlays_for_insert (pos, length)
3286 EMACS_INT pos;
3287 EMACS_INT length;
3289 /* After an insertion, the lists are still sorted properly,
3290 but we may need to update the value of the overlay center. */
3291 if (current_buffer->overlay_center >= pos)
3292 current_buffer->overlay_center += length;
3295 void
3296 adjust_overlays_for_delete (pos, length)
3297 EMACS_INT pos;
3298 EMACS_INT length;
3300 if (current_buffer->overlay_center < pos)
3301 /* The deletion was to our right. No change needed; the before- and
3302 after-lists are still consistent. */
3304 else if (current_buffer->overlay_center > pos + length)
3305 /* The deletion was to our left. We need to adjust the center value
3306 to account for the change in position, but the lists are consistent
3307 given the new value. */
3308 current_buffer->overlay_center -= length;
3309 else
3310 /* We're right in the middle. There might be things on the after-list
3311 that now belong on the before-list. Recentering will move them,
3312 and also update the center point. */
3313 recenter_overlay_lists (current_buffer, pos);
3316 /* Fix up overlays that were garbled as a result of permuting markers
3317 in the range START through END. Any overlay with at least one
3318 endpoint in this range will need to be unlinked from the overlay
3319 list and reinserted in its proper place.
3320 Such an overlay might even have negative size at this point.
3321 If so, we'll make the overlay empty. */
3322 void
3323 fix_start_end_in_overlays (start, end)
3324 register int start, end;
3326 Lisp_Object overlay;
3327 struct Lisp_Overlay *before_list, *after_list;
3328 /* These are either nil, indicating that before_list or after_list
3329 should be assigned, or the cons cell the cdr of which should be
3330 assigned. */
3331 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3332 /* 'Parent', likewise, indicates a cons cell or
3333 current_buffer->overlays_before or overlays_after, depending
3334 which loop we're in. */
3335 struct Lisp_Overlay *tail, *parent;
3336 int startpos, endpos;
3338 /* This algorithm shifts links around instead of consing and GCing.
3339 The loop invariant is that before_list (resp. after_list) is a
3340 well-formed list except that its last element, the CDR of beforep
3341 (resp. afterp) if beforep (afterp) isn't nil or before_list
3342 (after_list) if it is, is still uninitialized. So it's not a bug
3343 that before_list isn't initialized, although it may look
3344 strange. */
3345 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3347 XSETMISC (overlay, tail);
3349 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3350 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3352 /* If the overlay is backwards, make it empty. */
3353 if (endpos < startpos)
3355 startpos = endpos;
3356 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3357 Qnil);
3360 if (endpos < start)
3361 break;
3363 if (endpos < end
3364 || (startpos >= start && startpos < end))
3366 /* Add it to the end of the wrong list. Later on,
3367 recenter_overlay_lists will move it to the right place. */
3368 if (endpos < current_buffer->overlay_center)
3370 if (!afterp)
3371 after_list = tail;
3372 else
3373 afterp->next = tail;
3374 afterp = tail;
3376 else
3378 if (!beforep)
3379 before_list = tail;
3380 else
3381 beforep->next = tail;
3382 beforep = tail;
3384 if (!parent)
3385 current_buffer->overlays_before = tail->next;
3386 else
3387 parent->next = tail->next;
3388 tail = tail->next;
3390 else
3391 parent = tail, tail = parent->next;
3393 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3395 XSETMISC (overlay, tail);
3397 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3398 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3400 /* If the overlay is backwards, make it empty. */
3401 if (endpos < startpos)
3403 startpos = endpos;
3404 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3405 Qnil);
3408 if (startpos >= end)
3409 break;
3411 if (startpos >= start
3412 || (endpos >= start && endpos < end))
3414 if (endpos < current_buffer->overlay_center)
3416 if (!afterp)
3417 after_list = tail;
3418 else
3419 afterp->next = tail;
3420 afterp = tail;
3422 else
3424 if (!beforep)
3425 before_list = tail;
3426 else
3427 beforep->next = tail;
3428 beforep = tail;
3430 if (!parent)
3431 current_buffer->overlays_after = tail->next;
3432 else
3433 parent->next = tail->next;
3434 tail = tail->next;
3436 else
3437 parent = tail, tail = parent->next;
3440 /* Splice the constructed (wrong) lists into the buffer's lists,
3441 and let the recenter function make it sane again. */
3442 if (beforep)
3444 beforep->next = current_buffer->overlays_before;
3445 current_buffer->overlays_before = before_list;
3447 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3449 if (afterp)
3451 afterp->next = current_buffer->overlays_after;
3452 current_buffer->overlays_after = after_list;
3454 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3457 /* We have two types of overlay: the one whose ending marker is
3458 after-insertion-marker (this is the usual case) and the one whose
3459 ending marker is before-insertion-marker. When `overlays_before'
3460 contains overlays of the latter type and the former type in this
3461 order and both overlays end at inserting position, inserting a text
3462 increases only the ending marker of the latter type, which results
3463 in incorrect ordering of `overlays_before'.
3465 This function fixes ordering of overlays in the slot
3466 `overlays_before' of the buffer *BP. Before the insertion, `point'
3467 was at PREV, and now is at POS. */
3469 void
3470 fix_overlays_before (bp, prev, pos)
3471 struct buffer *bp;
3472 EMACS_INT prev, pos;
3474 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3475 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3476 Lisp_Object tem;
3477 EMACS_INT end;
3479 /* After the insertion, the several overlays may be in incorrect
3480 order. The possibility is that, in the list `overlays_before',
3481 an overlay which ends at POS appears after an overlay which ends
3482 at PREV. Since POS is greater than PREV, we must fix the
3483 ordering of these overlays, by moving overlays ends at POS before
3484 the overlays ends at PREV. */
3486 /* At first, find a place where disordered overlays should be linked
3487 in. It is where an overlay which end before POS exists. (i.e. an
3488 overlay whose ending marker is after-insertion-marker if disorder
3489 exists). */
3490 while (tail
3491 && (XSETMISC (tem, tail),
3492 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3494 parent = tail;
3495 tail = tail->next;
3498 /* If we don't find such an overlay,
3499 or the found one ends before PREV,
3500 or the found one is the last one in the list,
3501 we don't have to fix anything. */
3502 if (!tail || end < prev || !tail->next)
3503 return;
3505 right_pair = parent;
3506 parent = tail;
3507 tail = tail->next;
3509 /* Now, end position of overlays in the list TAIL should be before
3510 or equal to PREV. In the loop, an overlay which ends at POS is
3511 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3512 we found an overlay which ends before PREV, the remaining
3513 overlays are in correct order. */
3514 while (tail)
3516 XSETMISC (tem, tail);
3517 end = OVERLAY_POSITION (OVERLAY_END (tem));
3519 if (end == pos)
3520 { /* This overlay is disordered. */
3521 struct Lisp_Overlay *found = tail;
3523 /* Unlink the found overlay. */
3524 tail = found->next;
3525 parent->next = tail;
3526 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3527 and link it into the right place. */
3528 if (!right_pair)
3530 found->next = bp->overlays_before;
3531 bp->overlays_before = found;
3533 else
3535 found->next = right_pair->next;
3536 right_pair->next = found;
3539 else if (end == prev)
3541 parent = tail;
3542 tail = tail->next;
3544 else /* No more disordered overlay. */
3545 break;
3549 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3550 doc: /* Return t if OBJECT is an overlay. */)
3551 (object)
3552 Lisp_Object object;
3554 return (OVERLAYP (object) ? Qt : Qnil);
3557 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3558 doc: /* Create a new overlay with range BEG to END in BUFFER.
3559 If omitted, BUFFER defaults to the current buffer.
3560 BEG and END may be integers or markers.
3561 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3562 for the front of the overlay advance when text is inserted there
3563 (which means the text *is not* included in the overlay).
3564 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3565 for the rear of the overlay advance when text is inserted there
3566 (which means the text *is* included in the overlay). */)
3567 (beg, end, buffer, front_advance, rear_advance)
3568 Lisp_Object beg, end, buffer;
3569 Lisp_Object front_advance, rear_advance;
3571 Lisp_Object overlay;
3572 struct buffer *b;
3574 if (NILP (buffer))
3575 XSETBUFFER (buffer, current_buffer);
3576 else
3577 CHECK_BUFFER (buffer);
3578 if (MARKERP (beg)
3579 && ! EQ (Fmarker_buffer (beg), buffer))
3580 error ("Marker points into wrong buffer");
3581 if (MARKERP (end)
3582 && ! EQ (Fmarker_buffer (end), buffer))
3583 error ("Marker points into wrong buffer");
3585 CHECK_NUMBER_COERCE_MARKER (beg);
3586 CHECK_NUMBER_COERCE_MARKER (end);
3588 if (XINT (beg) > XINT (end))
3590 Lisp_Object temp;
3591 temp = beg; beg = end; end = temp;
3594 b = XBUFFER (buffer);
3596 beg = Fset_marker (Fmake_marker (), beg, buffer);
3597 end = Fset_marker (Fmake_marker (), end, buffer);
3599 if (!NILP (front_advance))
3600 XMARKER (beg)->insertion_type = 1;
3601 if (!NILP (rear_advance))
3602 XMARKER (end)->insertion_type = 1;
3604 overlay = allocate_misc ();
3605 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3606 XOVERLAY (overlay)->start = beg;
3607 XOVERLAY (overlay)->end = end;
3608 XOVERLAY (overlay)->plist = Qnil;
3609 XOVERLAY (overlay)->next = NULL;
3611 /* Put the new overlay on the wrong list. */
3612 end = OVERLAY_END (overlay);
3613 if (OVERLAY_POSITION (end) < b->overlay_center)
3615 if (b->overlays_after)
3616 XOVERLAY (overlay)->next = b->overlays_after;
3617 b->overlays_after = XOVERLAY (overlay);
3619 else
3621 if (b->overlays_before)
3622 XOVERLAY (overlay)->next = b->overlays_before;
3623 b->overlays_before = XOVERLAY (overlay);
3626 /* This puts it in the right list, and in the right order. */
3627 recenter_overlay_lists (b, b->overlay_center);
3629 /* We don't need to redisplay the region covered by the overlay, because
3630 the overlay has no properties at the moment. */
3632 return overlay;
3635 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3637 static void
3638 modify_overlay (buf, start, end)
3639 struct buffer *buf;
3640 EMACS_INT start, end;
3642 if (start > end)
3644 int temp = start;
3645 start = end;
3646 end = temp;
3649 BUF_COMPUTE_UNCHANGED (buf, start, end);
3651 /* If this is a buffer not in the selected window,
3652 we must do other windows. */
3653 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3654 windows_or_buffers_changed = 1;
3655 /* If multiple windows show this buffer, we must do other windows. */
3656 else if (buffer_shared > 1)
3657 windows_or_buffers_changed = 1;
3659 ++BUF_OVERLAY_MODIFF (buf);
3663 Lisp_Object Fdelete_overlay ();
3665 static struct Lisp_Overlay *
3666 unchain_overlay (list, overlay)
3667 struct Lisp_Overlay *list, *overlay;
3669 struct Lisp_Overlay *tmp, *prev;
3670 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3671 if (tmp == overlay)
3673 if (prev)
3674 prev->next = tmp->next;
3675 else
3676 list = tmp->next;
3677 overlay->next = NULL;
3678 break;
3680 return list;
3683 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3684 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3685 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3686 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3687 buffer. */)
3688 (overlay, beg, end, buffer)
3689 Lisp_Object overlay, beg, end, buffer;
3691 struct buffer *b, *ob;
3692 Lisp_Object obuffer;
3693 int count = SPECPDL_INDEX ();
3695 CHECK_OVERLAY (overlay);
3696 if (NILP (buffer))
3697 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3698 if (NILP (buffer))
3699 XSETBUFFER (buffer, current_buffer);
3700 CHECK_BUFFER (buffer);
3702 if (MARKERP (beg)
3703 && ! EQ (Fmarker_buffer (beg), buffer))
3704 error ("Marker points into wrong buffer");
3705 if (MARKERP (end)
3706 && ! EQ (Fmarker_buffer (end), buffer))
3707 error ("Marker points into wrong buffer");
3709 CHECK_NUMBER_COERCE_MARKER (beg);
3710 CHECK_NUMBER_COERCE_MARKER (end);
3712 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3713 return Fdelete_overlay (overlay);
3715 if (XINT (beg) > XINT (end))
3717 Lisp_Object temp;
3718 temp = beg; beg = end; end = temp;
3721 specbind (Qinhibit_quit, Qt);
3723 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3724 b = XBUFFER (buffer);
3725 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3727 /* If the overlay has changed buffers, do a thorough redisplay. */
3728 if (!EQ (buffer, obuffer))
3730 /* Redisplay where the overlay was. */
3731 if (!NILP (obuffer))
3733 int o_beg;
3734 int o_end;
3736 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3737 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3739 modify_overlay (ob, o_beg, o_end);
3742 /* Redisplay where the overlay is going to be. */
3743 modify_overlay (b, XINT (beg), XINT (end));
3745 else
3746 /* Redisplay the area the overlay has just left, or just enclosed. */
3748 int o_beg, o_end;
3750 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3751 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3753 if (o_beg == XINT (beg))
3754 modify_overlay (b, o_end, XINT (end));
3755 else if (o_end == XINT (end))
3756 modify_overlay (b, o_beg, XINT (beg));
3757 else
3759 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3760 if (XINT (end) > o_end) o_end = XINT (end);
3761 modify_overlay (b, o_beg, o_end);
3765 if (!NILP (obuffer))
3767 ob->overlays_before
3768 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3769 ob->overlays_after
3770 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3771 eassert (XOVERLAY (overlay)->next == NULL);
3774 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3775 Fset_marker (OVERLAY_END (overlay), end, buffer);
3777 /* Put the overlay on the wrong list. */
3778 end = OVERLAY_END (overlay);
3779 if (OVERLAY_POSITION (end) < b->overlay_center)
3781 XOVERLAY (overlay)->next = b->overlays_after;
3782 b->overlays_after = XOVERLAY (overlay);
3784 else
3786 XOVERLAY (overlay)->next = b->overlays_before;
3787 b->overlays_before = XOVERLAY (overlay);
3790 /* This puts it in the right list, and in the right order. */
3791 recenter_overlay_lists (b, b->overlay_center);
3793 return unbind_to (count, overlay);
3796 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3797 doc: /* Delete the overlay OVERLAY from its buffer. */)
3798 (overlay)
3799 Lisp_Object overlay;
3801 Lisp_Object buffer;
3802 struct buffer *b;
3803 int count = SPECPDL_INDEX ();
3805 CHECK_OVERLAY (overlay);
3807 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3808 if (NILP (buffer))
3809 return Qnil;
3811 b = XBUFFER (buffer);
3812 specbind (Qinhibit_quit, Qt);
3814 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3815 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3816 eassert (XOVERLAY (overlay)->next == NULL);
3817 modify_overlay (b,
3818 marker_position (OVERLAY_START (overlay)),
3819 marker_position (OVERLAY_END (overlay)));
3820 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3821 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3823 /* When deleting an overlay with before or after strings, turn off
3824 display optimizations for the affected buffer, on the basis that
3825 these strings may contain newlines. This is easier to do than to
3826 check for that situation during redisplay. */
3827 if (!windows_or_buffers_changed
3828 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3829 || !NILP (Foverlay_get (overlay, Qafter_string))))
3830 b->prevent_redisplay_optimizations_p = 1;
3832 return unbind_to (count, Qnil);
3835 /* Overlay dissection functions. */
3837 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3838 doc: /* Return the position at which OVERLAY starts. */)
3839 (overlay)
3840 Lisp_Object overlay;
3842 CHECK_OVERLAY (overlay);
3844 return (Fmarker_position (OVERLAY_START (overlay)));
3847 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3848 doc: /* Return the position at which OVERLAY ends. */)
3849 (overlay)
3850 Lisp_Object overlay;
3852 CHECK_OVERLAY (overlay);
3854 return (Fmarker_position (OVERLAY_END (overlay)));
3857 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3858 doc: /* Return the buffer OVERLAY belongs to.
3859 Return nil if OVERLAY has been deleted. */)
3860 (overlay)
3861 Lisp_Object overlay;
3863 CHECK_OVERLAY (overlay);
3865 return Fmarker_buffer (OVERLAY_START (overlay));
3868 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3869 doc: /* Return a list of the properties on OVERLAY.
3870 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3871 OVERLAY. */)
3872 (overlay)
3873 Lisp_Object overlay;
3875 CHECK_OVERLAY (overlay);
3877 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3881 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3882 doc: /* Return a list of the overlays that contain position POS. */)
3883 (pos)
3884 Lisp_Object pos;
3886 int noverlays;
3887 Lisp_Object *overlay_vec;
3888 int len;
3889 Lisp_Object result;
3891 CHECK_NUMBER_COERCE_MARKER (pos);
3893 len = 10;
3894 /* We can't use alloca here because overlays_at can call xrealloc. */
3895 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3897 /* Put all the overlays we want in a vector in overlay_vec.
3898 Store the length in len. */
3899 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3900 (int *) 0, (int *) 0, 0);
3902 /* Make a list of them all. */
3903 result = Flist (noverlays, overlay_vec);
3905 xfree (overlay_vec);
3906 return result;
3909 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3910 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3911 Overlap means that at least one character is contained within the overlay
3912 and also contained within the specified region.
3913 Empty overlays are included in the result if they are located at BEG
3914 or between BEG and END. */)
3915 (beg, end)
3916 Lisp_Object beg, end;
3918 int noverlays;
3919 Lisp_Object *overlay_vec;
3920 int len;
3921 Lisp_Object result;
3923 CHECK_NUMBER_COERCE_MARKER (beg);
3924 CHECK_NUMBER_COERCE_MARKER (end);
3926 len = 10;
3927 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3929 /* Put all the overlays we want in a vector in overlay_vec.
3930 Store the length in len. */
3931 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3932 (int *) 0, (int *) 0);
3934 /* Make a list of them all. */
3935 result = Flist (noverlays, overlay_vec);
3937 xfree (overlay_vec);
3938 return result;
3941 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3942 1, 1, 0,
3943 doc: /* Return the next position after POS where an overlay starts or ends.
3944 If there are no more overlay boundaries after POS, return (point-max). */)
3945 (pos)
3946 Lisp_Object pos;
3948 int noverlays;
3949 int endpos;
3950 Lisp_Object *overlay_vec;
3951 int len;
3952 int i;
3954 CHECK_NUMBER_COERCE_MARKER (pos);
3956 len = 10;
3957 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3959 /* Put all the overlays we want in a vector in overlay_vec.
3960 Store the length in len.
3961 endpos gets the position where the next overlay starts. */
3962 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3963 &endpos, (int *) 0, 1);
3965 /* If any of these overlays ends before endpos,
3966 use its ending point instead. */
3967 for (i = 0; i < noverlays; i++)
3969 Lisp_Object oend;
3970 int oendpos;
3972 oend = OVERLAY_END (overlay_vec[i]);
3973 oendpos = OVERLAY_POSITION (oend);
3974 if (oendpos < endpos)
3975 endpos = oendpos;
3978 xfree (overlay_vec);
3979 return make_number (endpos);
3982 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3983 Sprevious_overlay_change, 1, 1, 0,
3984 doc: /* Return the previous position before POS where an overlay starts or ends.
3985 If there are no more overlay boundaries before POS, return (point-min). */)
3986 (pos)
3987 Lisp_Object pos;
3989 int noverlays;
3990 int prevpos;
3991 Lisp_Object *overlay_vec;
3992 int len;
3994 CHECK_NUMBER_COERCE_MARKER (pos);
3996 /* At beginning of buffer, we know the answer;
3997 avoid bug subtracting 1 below. */
3998 if (XINT (pos) == BEGV)
3999 return pos;
4001 len = 10;
4002 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4004 /* Put all the overlays we want in a vector in overlay_vec.
4005 Store the length in len.
4006 prevpos gets the position of the previous change. */
4007 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4008 (int *) 0, &prevpos, 1);
4010 xfree (overlay_vec);
4011 return make_number (prevpos);
4014 /* These functions are for debugging overlays. */
4016 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4017 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4018 The car has all the overlays before the overlay center;
4019 the cdr has all the overlays after the overlay center.
4020 Recentering overlays moves overlays between these lists.
4021 The lists you get are copies, so that changing them has no effect.
4022 However, the overlays you get are the real objects that the buffer uses. */)
4025 struct Lisp_Overlay *ol;
4026 Lisp_Object before = Qnil, after = Qnil, tmp;
4027 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4029 XSETMISC (tmp, ol);
4030 before = Fcons (tmp, before);
4032 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4034 XSETMISC (tmp, ol);
4035 after = Fcons (tmp, after);
4037 return Fcons (Fnreverse (before), Fnreverse (after));
4040 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4041 doc: /* Recenter the overlays of the current buffer around position POS.
4042 That makes overlay lookup faster for positions near POS (but perhaps slower
4043 for positions far away from POS). */)
4044 (pos)
4045 Lisp_Object pos;
4047 CHECK_NUMBER_COERCE_MARKER (pos);
4049 recenter_overlay_lists (current_buffer, XINT (pos));
4050 return Qnil;
4053 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4054 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4055 (overlay, prop)
4056 Lisp_Object overlay, prop;
4058 CHECK_OVERLAY (overlay);
4059 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4062 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4063 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4064 (overlay, prop, value)
4065 Lisp_Object overlay, prop, value;
4067 Lisp_Object tail, buffer;
4068 int changed;
4070 CHECK_OVERLAY (overlay);
4072 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4074 for (tail = XOVERLAY (overlay)->plist;
4075 CONSP (tail) && CONSP (XCDR (tail));
4076 tail = XCDR (XCDR (tail)))
4077 if (EQ (XCAR (tail), prop))
4079 changed = !EQ (XCAR (XCDR (tail)), value);
4080 XSETCAR (XCDR (tail), value);
4081 goto found;
4083 /* It wasn't in the list, so add it to the front. */
4084 changed = !NILP (value);
4085 XOVERLAY (overlay)->plist
4086 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4087 found:
4088 if (! NILP (buffer))
4090 if (changed)
4091 modify_overlay (XBUFFER (buffer),
4092 marker_position (OVERLAY_START (overlay)),
4093 marker_position (OVERLAY_END (overlay)));
4094 if (EQ (prop, Qevaporate) && ! NILP (value)
4095 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4096 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4097 Fdelete_overlay (overlay);
4099 return value;
4102 /* Subroutine of report_overlay_modification. */
4104 /* Lisp vector holding overlay hook functions to call.
4105 Vector elements come in pairs.
4106 Each even-index element is a list of hook functions.
4107 The following odd-index element is the overlay they came from.
4109 Before the buffer change, we fill in this vector
4110 as we call overlay hook functions.
4111 After the buffer change, we get the functions to call from this vector.
4112 This way we always call the same functions before and after the change. */
4113 static Lisp_Object last_overlay_modification_hooks;
4115 /* Number of elements actually used in last_overlay_modification_hooks. */
4116 static int last_overlay_modification_hooks_used;
4118 /* Add one functionlist/overlay pair
4119 to the end of last_overlay_modification_hooks. */
4121 static void
4122 add_overlay_mod_hooklist (functionlist, overlay)
4123 Lisp_Object functionlist, overlay;
4125 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4127 if (last_overlay_modification_hooks_used == oldsize)
4129 Lisp_Object old;
4130 old = last_overlay_modification_hooks;
4131 last_overlay_modification_hooks
4132 = Fmake_vector (make_number (oldsize * 2), Qnil);
4133 bcopy (XVECTOR (old)->contents,
4134 XVECTOR (last_overlay_modification_hooks)->contents,
4135 sizeof (Lisp_Object) * oldsize);
4137 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = functionlist;
4138 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = overlay;
4141 /* Run the modification-hooks of overlays that include
4142 any part of the text in START to END.
4143 If this change is an insertion, also
4144 run the insert-before-hooks of overlay starting at END,
4145 and the insert-after-hooks of overlay ending at START.
4147 This is called both before and after the modification.
4148 AFTER is nonzero when we call after the modification.
4150 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4151 When AFTER is nonzero, they are the start position,
4152 the position after the inserted new text,
4153 and the length of deleted or replaced old text. */
4155 void
4156 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4157 Lisp_Object start, end;
4158 int after;
4159 Lisp_Object arg1, arg2, arg3;
4161 Lisp_Object prop, overlay;
4162 struct Lisp_Overlay *tail;
4163 /* 1 if this change is an insertion. */
4164 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4165 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4167 overlay = Qnil;
4168 tail = NULL;
4170 /* We used to run the functions as soon as we found them and only register
4171 them in last_overlay_modification_hooks for the purpose of the `after'
4172 case. But running elisp code as we traverse the list of overlays is
4173 painful because the list can be modified by the elisp code so we had to
4174 copy at several places. We now simply do a read-only traversal that
4175 only collects the functions to run and we run them afterwards. It's
4176 simpler, especially since all the code was already there. -stef */
4178 if (!after)
4180 /* We are being called before a change.
4181 Scan the overlays to find the functions to call. */
4182 last_overlay_modification_hooks_used = 0;
4183 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4185 int startpos, endpos;
4186 Lisp_Object ostart, oend;
4188 XSETMISC (overlay, tail);
4190 ostart = OVERLAY_START (overlay);
4191 oend = OVERLAY_END (overlay);
4192 endpos = OVERLAY_POSITION (oend);
4193 if (XFASTINT (start) > endpos)
4194 break;
4195 startpos = OVERLAY_POSITION (ostart);
4196 if (insertion && (XFASTINT (start) == startpos
4197 || XFASTINT (end) == startpos))
4199 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4200 if (!NILP (prop))
4201 add_overlay_mod_hooklist (prop, overlay);
4203 if (insertion && (XFASTINT (start) == endpos
4204 || XFASTINT (end) == endpos))
4206 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4207 if (!NILP (prop))
4208 add_overlay_mod_hooklist (prop, overlay);
4210 /* Test for intersecting intervals. This does the right thing
4211 for both insertion and deletion. */
4212 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4214 prop = Foverlay_get (overlay, Qmodification_hooks);
4215 if (!NILP (prop))
4216 add_overlay_mod_hooklist (prop, overlay);
4220 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4222 int startpos, endpos;
4223 Lisp_Object ostart, oend;
4225 XSETMISC (overlay, tail);
4227 ostart = OVERLAY_START (overlay);
4228 oend = OVERLAY_END (overlay);
4229 startpos = OVERLAY_POSITION (ostart);
4230 endpos = OVERLAY_POSITION (oend);
4231 if (XFASTINT (end) < startpos)
4232 break;
4233 if (insertion && (XFASTINT (start) == startpos
4234 || XFASTINT (end) == startpos))
4236 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4237 if (!NILP (prop))
4238 add_overlay_mod_hooklist (prop, overlay);
4240 if (insertion && (XFASTINT (start) == endpos
4241 || XFASTINT (end) == endpos))
4243 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4244 if (!NILP (prop))
4245 add_overlay_mod_hooklist (prop, overlay);
4247 /* Test for intersecting intervals. This does the right thing
4248 for both insertion and deletion. */
4249 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4251 prop = Foverlay_get (overlay, Qmodification_hooks);
4252 if (!NILP (prop))
4253 add_overlay_mod_hooklist (prop, overlay);
4258 GCPRO4 (overlay, arg1, arg2, arg3);
4260 /* Call the functions recorded in last_overlay_modification_hooks.
4261 First copy the vector contents, in case some of these hooks
4262 do subsequent modification of the buffer. */
4263 int size = last_overlay_modification_hooks_used;
4264 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4265 int i;
4267 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4268 copy, size * sizeof (Lisp_Object));
4269 gcpro1.var = copy;
4270 gcpro1.nvars = size;
4272 for (i = 0; i < size;)
4274 Lisp_Object prop, overlay;
4275 prop = copy[i++];
4276 overlay = copy[i++];
4277 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4280 UNGCPRO;
4283 static void
4284 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4285 Lisp_Object list, overlay;
4286 int after;
4287 Lisp_Object arg1, arg2, arg3;
4289 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4291 GCPRO4 (list, arg1, arg2, arg3);
4293 while (CONSP (list))
4295 if (NILP (arg3))
4296 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4297 else
4298 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4299 list = XCDR (list);
4301 UNGCPRO;
4304 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4305 property is set. */
4306 void
4307 evaporate_overlays (pos)
4308 EMACS_INT pos;
4310 Lisp_Object overlay, hit_list;
4311 struct Lisp_Overlay *tail;
4313 hit_list = Qnil;
4314 if (pos <= current_buffer->overlay_center)
4315 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4317 int endpos;
4318 XSETMISC (overlay, tail);
4319 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4320 if (endpos < pos)
4321 break;
4322 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4323 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4324 hit_list = Fcons (overlay, hit_list);
4326 else
4327 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4329 int startpos;
4330 XSETMISC (overlay, tail);
4331 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4332 if (startpos > pos)
4333 break;
4334 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4335 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4336 hit_list = Fcons (overlay, hit_list);
4338 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4339 Fdelete_overlay (XCAR (hit_list));
4342 /* Somebody has tried to store a value with an unacceptable type
4343 in the slot with offset OFFSET. */
4345 void
4346 buffer_slot_type_mismatch (offset)
4347 int offset;
4349 Lisp_Object sym;
4350 char *type_name;
4352 switch (XINT (PER_BUFFER_TYPE (offset)))
4354 case Lisp_Int:
4355 type_name = "integers";
4356 break;
4358 case Lisp_String:
4359 type_name = "strings";
4360 break;
4362 case Lisp_Symbol:
4363 type_name = "symbols";
4364 break;
4366 default:
4367 abort ();
4370 sym = PER_BUFFER_SYMBOL (offset);
4371 error ("Only %s should be stored in the buffer-local variable %s",
4372 type_name, SDATA (SYMBOL_NAME (sym)));
4376 /***********************************************************************
4377 Allocation with mmap
4378 ***********************************************************************/
4380 #ifdef USE_MMAP_FOR_BUFFERS
4382 #include <sys/types.h>
4383 #include <sys/mman.h>
4385 #ifndef MAP_ANON
4386 #ifdef MAP_ANONYMOUS
4387 #define MAP_ANON MAP_ANONYMOUS
4388 #else
4389 #define MAP_ANON 0
4390 #endif
4391 #endif
4393 #ifndef MAP_FAILED
4394 #define MAP_FAILED ((void *) -1)
4395 #endif
4397 #include <stdio.h>
4398 #include <errno.h>
4400 #if MAP_ANON == 0
4401 #include <fcntl.h>
4402 #endif
4404 #include "coding.h"
4407 /* Memory is allocated in regions which are mapped using mmap(2).
4408 The current implementation lets the system select mapped
4409 addresses; we're not using MAP_FIXED in general, except when
4410 trying to enlarge regions.
4412 Each mapped region starts with a mmap_region structure, the user
4413 area starts after that structure, aligned to MEM_ALIGN.
4415 +-----------------------+
4416 | struct mmap_info + |
4417 | padding |
4418 +-----------------------+
4419 | user data |
4422 +-----------------------+ */
4424 struct mmap_region
4426 /* User-specified size. */
4427 size_t nbytes_specified;
4429 /* Number of bytes mapped */
4430 size_t nbytes_mapped;
4432 /* Pointer to the location holding the address of the memory
4433 allocated with the mmap'd block. The variable actually points
4434 after this structure. */
4435 POINTER_TYPE **var;
4437 /* Next and previous in list of all mmap'd regions. */
4438 struct mmap_region *next, *prev;
4441 /* Doubly-linked list of mmap'd regions. */
4443 static struct mmap_region *mmap_regions;
4445 /* File descriptor for mmap. If we don't have anonymous mapping,
4446 /dev/zero will be opened on it. */
4448 static int mmap_fd;
4450 /* Temporary storage for mmap_set_vars, see there. */
4452 static struct mmap_region *mmap_regions_1;
4453 static int mmap_fd_1;
4455 /* Page size on this system. */
4457 static int mmap_page_size;
4459 /* 1 means mmap has been intialized. */
4461 static int mmap_initialized_p;
4463 /* Value is X rounded up to the next multiple of N. */
4465 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4467 /* Size of mmap_region structure plus padding. */
4469 #define MMAP_REGION_STRUCT_SIZE \
4470 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4472 /* Given a pointer P to the start of the user-visible part of a mapped
4473 region, return a pointer to the start of the region. */
4475 #define MMAP_REGION(P) \
4476 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4478 /* Given a pointer P to the start of a mapped region, return a pointer
4479 to the start of the user-visible part of the region. */
4481 #define MMAP_USER_AREA(P) \
4482 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4484 #define MEM_ALIGN sizeof (double)
4486 /* Predicate returning true if part of the address range [START .. END]
4487 is currently mapped. Used to prevent overwriting an existing
4488 memory mapping.
4490 Default is to conservativly assume the address range is occupied by
4491 something else. This can be overridden by system configuration
4492 files if system-specific means to determine this exists. */
4494 #ifndef MMAP_ALLOCATED_P
4495 #define MMAP_ALLOCATED_P(start, end) 1
4496 #endif
4498 /* Function prototypes. */
4500 static int mmap_free_1 P_ ((struct mmap_region *));
4501 static int mmap_enlarge P_ ((struct mmap_region *, int));
4502 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4503 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4504 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4505 static void mmap_free P_ ((POINTER_TYPE **ptr));
4506 static void mmap_init P_ ((void));
4509 /* Return a region overlapping address range START...END, or null if
4510 none. END is not including, i.e. the last byte in the range
4511 is at END - 1. */
4513 static struct mmap_region *
4514 mmap_find (start, end)
4515 POINTER_TYPE *start, *end;
4517 struct mmap_region *r;
4518 char *s = (char *) start, *e = (char *) end;
4520 for (r = mmap_regions; r; r = r->next)
4522 char *rstart = (char *) r;
4523 char *rend = rstart + r->nbytes_mapped;
4525 if (/* First byte of range, i.e. START, in this region? */
4526 (s >= rstart && s < rend)
4527 /* Last byte of range, i.e. END - 1, in this region? */
4528 || (e > rstart && e <= rend)
4529 /* First byte of this region in the range? */
4530 || (rstart >= s && rstart < e)
4531 /* Last byte of this region in the range? */
4532 || (rend > s && rend <= e))
4533 break;
4536 return r;
4540 /* Unmap a region. P is a pointer to the start of the user-araa of
4541 the region. Value is non-zero if successful. */
4543 static int
4544 mmap_free_1 (r)
4545 struct mmap_region *r;
4547 if (r->next)
4548 r->next->prev = r->prev;
4549 if (r->prev)
4550 r->prev->next = r->next;
4551 else
4552 mmap_regions = r->next;
4554 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4556 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4557 return 0;
4560 return 1;
4564 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4565 Value is non-zero if successful. */
4567 static int
4568 mmap_enlarge (r, npages)
4569 struct mmap_region *r;
4570 int npages;
4572 char *region_end = (char *) r + r->nbytes_mapped;
4573 size_t nbytes;
4574 int success = 0;
4576 if (npages < 0)
4578 /* Unmap pages at the end of the region. */
4579 nbytes = - npages * mmap_page_size;
4580 if (munmap (region_end - nbytes, nbytes) == -1)
4581 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4582 else
4584 r->nbytes_mapped -= nbytes;
4585 success = 1;
4588 else if (npages > 0)
4590 nbytes = npages * mmap_page_size;
4592 /* Try to map additional pages at the end of the region. We
4593 cannot do this if the address range is already occupied by
4594 something else because mmap deletes any previous mapping.
4595 I'm not sure this is worth doing, let's see. */
4596 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4598 POINTER_TYPE *p;
4600 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4601 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4602 if (p == MAP_FAILED)
4603 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4604 else if (p != (POINTER_TYPE *) region_end)
4606 /* Kernels are free to choose a different address. In
4607 that case, unmap what we've mapped above; we have
4608 no use for it. */
4609 if (munmap (p, nbytes) == -1)
4610 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4612 else
4614 r->nbytes_mapped += nbytes;
4615 success = 1;
4620 return success;
4624 /* Set or reset variables holding references to mapped regions. If
4625 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4626 non-zero, set all variables to the start of the user-areas
4627 of mapped regions.
4629 This function is called from Fdump_emacs to ensure that the dumped
4630 Emacs doesn't contain references to memory that won't be mapped
4631 when Emacs starts. */
4633 void
4634 mmap_set_vars (restore_p)
4635 int restore_p;
4637 struct mmap_region *r;
4639 if (restore_p)
4641 mmap_regions = mmap_regions_1;
4642 mmap_fd = mmap_fd_1;
4643 for (r = mmap_regions; r; r = r->next)
4644 *r->var = MMAP_USER_AREA (r);
4646 else
4648 for (r = mmap_regions; r; r = r->next)
4649 *r->var = NULL;
4650 mmap_regions_1 = mmap_regions;
4651 mmap_regions = NULL;
4652 mmap_fd_1 = mmap_fd;
4653 mmap_fd = -1;
4658 /* Allocate a block of storage large enough to hold NBYTES bytes of
4659 data. A pointer to the data is returned in *VAR. VAR is thus the
4660 address of some variable which will use the data area.
4662 The allocation of 0 bytes is valid.
4664 If we can't allocate the necessary memory, set *VAR to null, and
4665 return null. */
4667 static POINTER_TYPE *
4668 mmap_alloc (var, nbytes)
4669 POINTER_TYPE **var;
4670 size_t nbytes;
4672 void *p;
4673 size_t map;
4675 mmap_init ();
4677 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4678 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4679 mmap_fd, 0);
4681 if (p == MAP_FAILED)
4683 if (errno != ENOMEM)
4684 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4685 p = NULL;
4687 else
4689 struct mmap_region *r = (struct mmap_region *) p;
4691 r->nbytes_specified = nbytes;
4692 r->nbytes_mapped = map;
4693 r->var = var;
4694 r->prev = NULL;
4695 r->next = mmap_regions;
4696 if (r->next)
4697 r->next->prev = r;
4698 mmap_regions = r;
4700 p = MMAP_USER_AREA (p);
4703 return *var = p;
4707 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4708 resize it to size NBYTES. Change *VAR to reflect the new block,
4709 and return this value. If more memory cannot be allocated, then
4710 leave *VAR unchanged, and return null. */
4712 static POINTER_TYPE *
4713 mmap_realloc (var, nbytes)
4714 POINTER_TYPE **var;
4715 size_t nbytes;
4717 POINTER_TYPE *result;
4719 mmap_init ();
4721 if (*var == NULL)
4722 result = mmap_alloc (var, nbytes);
4723 else if (nbytes == 0)
4725 mmap_free (var);
4726 result = mmap_alloc (var, nbytes);
4728 else
4730 struct mmap_region *r = MMAP_REGION (*var);
4731 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4733 if (room < nbytes)
4735 /* Must enlarge. */
4736 POINTER_TYPE *old_ptr = *var;
4738 /* Try to map additional pages at the end of the region.
4739 If that fails, allocate a new region, copy data
4740 from the old region, then free it. */
4741 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4742 / mmap_page_size)))
4744 r->nbytes_specified = nbytes;
4745 *var = result = old_ptr;
4747 else if (mmap_alloc (var, nbytes))
4749 bcopy (old_ptr, *var, r->nbytes_specified);
4750 mmap_free_1 (MMAP_REGION (old_ptr));
4751 result = *var;
4752 r = MMAP_REGION (result);
4753 r->nbytes_specified = nbytes;
4755 else
4757 *var = old_ptr;
4758 result = NULL;
4761 else if (room - nbytes >= mmap_page_size)
4763 /* Shrinking by at least a page. Let's give some
4764 memory back to the system.
4766 The extra parens are to make the division happens first,
4767 on positive values, so we know it will round towards
4768 zero. */
4769 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4770 result = *var;
4771 r->nbytes_specified = nbytes;
4773 else
4775 /* Leave it alone. */
4776 result = *var;
4777 r->nbytes_specified = nbytes;
4781 return result;
4785 /* Free a block of relocatable storage whose data is pointed to by
4786 PTR. Store 0 in *PTR to show there's no block allocated. */
4788 static void
4789 mmap_free (var)
4790 POINTER_TYPE **var;
4792 mmap_init ();
4794 if (*var)
4796 mmap_free_1 (MMAP_REGION (*var));
4797 *var = NULL;
4802 /* Perform necessary intializations for the use of mmap. */
4804 static void
4805 mmap_init ()
4807 #if MAP_ANON == 0
4808 /* The value of mmap_fd is initially 0 in temacs, and -1
4809 in a dumped Emacs. */
4810 if (mmap_fd <= 0)
4812 /* No anonymous mmap -- we need the file descriptor. */
4813 mmap_fd = open ("/dev/zero", O_RDONLY);
4814 if (mmap_fd == -1)
4815 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4817 #endif /* MAP_ANON == 0 */
4819 if (mmap_initialized_p)
4820 return;
4821 mmap_initialized_p = 1;
4823 #if MAP_ANON != 0
4824 mmap_fd = -1;
4825 #endif
4827 mmap_page_size = getpagesize ();
4830 #endif /* USE_MMAP_FOR_BUFFERS */
4834 /***********************************************************************
4835 Buffer-text Allocation
4836 ***********************************************************************/
4838 #ifdef REL_ALLOC
4839 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4840 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4841 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4842 #endif /* REL_ALLOC */
4845 /* Allocate NBYTES bytes for buffer B's text buffer. */
4847 static void
4848 alloc_buffer_text (b, nbytes)
4849 struct buffer *b;
4850 size_t nbytes;
4852 POINTER_TYPE *p;
4854 BLOCK_INPUT;
4855 #if defined USE_MMAP_FOR_BUFFERS
4856 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4857 #elif defined REL_ALLOC
4858 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4859 #else
4860 p = xmalloc (nbytes);
4861 #endif
4863 if (p == NULL)
4865 UNBLOCK_INPUT;
4866 memory_full ();
4869 b->text->beg = (unsigned char *) p;
4870 UNBLOCK_INPUT;
4873 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4874 shrink it. */
4876 void
4877 enlarge_buffer_text (b, delta)
4878 struct buffer *b;
4879 int delta;
4881 POINTER_TYPE *p;
4882 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4883 + delta);
4884 BLOCK_INPUT;
4885 #if defined USE_MMAP_FOR_BUFFERS
4886 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4887 #elif defined REL_ALLOC
4888 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4889 #else
4890 p = xrealloc (b->text->beg, nbytes);
4891 #endif
4893 if (p == NULL)
4895 UNBLOCK_INPUT;
4896 memory_full ();
4899 BUF_BEG_ADDR (b) = (unsigned char *) p;
4900 UNBLOCK_INPUT;
4904 /* Free buffer B's text buffer. */
4906 static void
4907 free_buffer_text (b)
4908 struct buffer *b;
4910 BLOCK_INPUT;
4912 #if defined USE_MMAP_FOR_BUFFERS
4913 mmap_free ((POINTER_TYPE **) &b->text->beg);
4914 #elif defined REL_ALLOC
4915 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4916 #else
4917 xfree (b->text->beg);
4918 #endif
4920 BUF_BEG_ADDR (b) = NULL;
4921 UNBLOCK_INPUT;
4926 /***********************************************************************
4927 Initialization
4928 ***********************************************************************/
4930 void
4931 init_buffer_once ()
4933 int idx;
4935 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
4937 /* Make sure all markable slots in buffer_defaults
4938 are initialized reasonably, so mark_buffer won't choke. */
4939 reset_buffer (&buffer_defaults);
4940 reset_buffer_local_variables (&buffer_defaults, 1);
4941 reset_buffer (&buffer_local_symbols);
4942 reset_buffer_local_variables (&buffer_local_symbols, 1);
4943 /* Prevent GC from getting confused. */
4944 buffer_defaults.text = &buffer_defaults.own_text;
4945 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4946 BUF_INTERVALS (&buffer_defaults) = 0;
4947 BUF_INTERVALS (&buffer_local_symbols) = 0;
4948 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4949 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4951 /* Set up the default values of various buffer slots. */
4952 /* Must do these before making the first buffer! */
4954 /* real setup is done in bindings.el */
4955 buffer_defaults.mode_line_format = build_string ("%-");
4956 buffer_defaults.header_line_format = Qnil;
4957 buffer_defaults.abbrev_mode = Qnil;
4958 buffer_defaults.overwrite_mode = Qnil;
4959 buffer_defaults.case_fold_search = Qt;
4960 buffer_defaults.auto_fill_function = Qnil;
4961 buffer_defaults.selective_display = Qnil;
4962 #ifndef old
4963 buffer_defaults.selective_display_ellipses = Qt;
4964 #endif
4965 buffer_defaults.abbrev_table = Qnil;
4966 buffer_defaults.display_table = Qnil;
4967 buffer_defaults.undo_list = Qnil;
4968 buffer_defaults.mark_active = Qnil;
4969 buffer_defaults.file_format = Qnil;
4970 buffer_defaults.auto_save_file_format = Qt;
4971 buffer_defaults.overlays_before = NULL;
4972 buffer_defaults.overlays_after = NULL;
4973 buffer_defaults.overlay_center = BEG;
4975 XSETFASTINT (buffer_defaults.tab_width, 8);
4976 buffer_defaults.truncate_lines = Qnil;
4977 buffer_defaults.ctl_arrow = Qt;
4978 buffer_defaults.direction_reversed = Qnil;
4979 buffer_defaults.cursor_type = Qt;
4980 buffer_defaults.extra_line_spacing = Qnil;
4981 buffer_defaults.cursor_in_non_selected_windows = Qt;
4983 #ifdef DOS_NT
4984 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
4985 #endif
4986 buffer_defaults.enable_multibyte_characters = Qt;
4987 buffer_defaults.buffer_file_coding_system = Qnil;
4988 XSETFASTINT (buffer_defaults.fill_column, 70);
4989 XSETFASTINT (buffer_defaults.left_margin, 0);
4990 buffer_defaults.cache_long_line_scans = Qnil;
4991 buffer_defaults.file_truename = Qnil;
4992 XSETFASTINT (buffer_defaults.display_count, 0);
4993 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
4994 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
4995 buffer_defaults.left_fringe_width = Qnil;
4996 buffer_defaults.right_fringe_width = Qnil;
4997 buffer_defaults.fringes_outside_margins = Qnil;
4998 buffer_defaults.scroll_bar_width = Qnil;
4999 buffer_defaults.vertical_scroll_bar_type = Qt;
5000 buffer_defaults.indicate_empty_lines = Qnil;
5001 buffer_defaults.indicate_buffer_boundaries = Qnil;
5002 buffer_defaults.scroll_up_aggressively = Qnil;
5003 buffer_defaults.scroll_down_aggressively = Qnil;
5004 buffer_defaults.display_time = Qnil;
5006 /* Assign the local-flags to the slots that have default values.
5007 The local flag is a bit that is used in the buffer
5008 to say that it has its own local value for the slot.
5009 The local flag bits are in the local_var_flags slot of the buffer. */
5011 /* Nothing can work if this isn't true */
5012 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5014 /* 0 means not a lisp var, -1 means always local, else mask */
5015 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5016 XSETINT (buffer_local_flags.filename, -1);
5017 XSETINT (buffer_local_flags.directory, -1);
5018 XSETINT (buffer_local_flags.backed_up, -1);
5019 XSETINT (buffer_local_flags.save_length, -1);
5020 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5021 XSETINT (buffer_local_flags.read_only, -1);
5022 XSETINT (buffer_local_flags.major_mode, -1);
5023 XSETINT (buffer_local_flags.mode_name, -1);
5024 XSETINT (buffer_local_flags.undo_list, -1);
5025 XSETINT (buffer_local_flags.mark_active, -1);
5026 XSETINT (buffer_local_flags.point_before_scroll, -1);
5027 XSETINT (buffer_local_flags.file_truename, -1);
5028 XSETINT (buffer_local_flags.invisibility_spec, -1);
5029 XSETINT (buffer_local_flags.file_format, -1);
5030 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5031 XSETINT (buffer_local_flags.display_count, -1);
5032 XSETINT (buffer_local_flags.display_time, -1);
5033 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5035 idx = 1;
5036 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5037 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5038 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5039 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5040 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5041 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5042 #ifndef old
5043 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5044 #endif
5045 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5046 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5047 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5048 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5049 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5050 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5051 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5052 #ifdef DOS_NT
5053 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5054 /* Make this one a permanent local. */
5055 buffer_permanent_local_flags[idx++] = 1;
5056 #endif
5057 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5058 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5059 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5060 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5061 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5062 /* Make this one a permanent local. */
5063 buffer_permanent_local_flags[idx++] = 1;
5064 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5065 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5066 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5067 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5068 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5069 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5070 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5071 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5072 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5073 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5074 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5075 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5076 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5077 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5078 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5080 /* Need more room? */
5081 if (idx >= MAX_PER_BUFFER_VARS)
5082 abort ();
5083 last_per_buffer_idx = idx;
5085 Vbuffer_alist = Qnil;
5086 current_buffer = 0;
5087 all_buffers = 0;
5089 QSFundamental = build_string ("Fundamental");
5091 Qfundamental_mode = intern ("fundamental-mode");
5092 buffer_defaults.major_mode = Qfundamental_mode;
5094 Qmode_class = intern ("mode-class");
5096 Qprotected_field = intern ("protected-field");
5098 Qpermanent_local = intern ("permanent-local");
5100 Qkill_buffer_hook = intern ("kill-buffer-hook");
5101 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5103 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5105 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5107 /* super-magic invisible buffer */
5108 Vbuffer_alist = Qnil;
5110 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5112 inhibit_modification_hooks = 0;
5115 void
5116 init_buffer ()
5118 char buf[MAXPATHLEN + 1];
5119 char *pwd;
5120 struct stat dotstat, pwdstat;
5121 Lisp_Object temp;
5122 int rc;
5124 #ifdef USE_MMAP_FOR_BUFFERS
5126 /* When using the ralloc implementation based on mmap(2), buffer
5127 text pointers will have been set to null in the dumped Emacs.
5128 Map new memory. */
5129 struct buffer *b;
5131 for (b = all_buffers; b; b = b->next)
5132 if (b->text->beg == NULL)
5133 enlarge_buffer_text (b, 0);
5135 #endif /* USE_MMAP_FOR_BUFFERS */
5137 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5138 if (NILP (buffer_defaults.enable_multibyte_characters))
5139 Fset_buffer_multibyte (Qnil);
5141 /* If PWD is accurate, use it instead of calling getwd. PWD is
5142 sometimes a nicer name, and using it may avoid a fatal error if a
5143 parent directory is searchable but not readable. */
5144 if ((pwd = getenv ("PWD")) != 0
5145 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
5146 && stat (pwd, &pwdstat) == 0
5147 && stat (".", &dotstat) == 0
5148 && dotstat.st_ino == pwdstat.st_ino
5149 && dotstat.st_dev == pwdstat.st_dev
5150 && strlen (pwd) < MAXPATHLEN)
5151 strcpy (buf, pwd);
5152 #ifdef HAVE_GETCWD
5153 else if (getcwd (buf, MAXPATHLEN+1) == 0)
5154 fatal ("`getcwd' failed: %s\n", strerror (errno));
5155 #else
5156 else if (getwd (buf) == 0)
5157 fatal ("`getwd' failed: %s\n", buf);
5158 #endif
5160 #ifndef VMS
5161 /* Maybe this should really use some standard subroutine
5162 whose definition is filename syntax dependent. */
5163 rc = strlen (buf);
5164 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
5166 buf[rc] = DIRECTORY_SEP;
5167 buf[rc + 1] = '\0';
5169 #endif /* not VMS */
5171 current_buffer->directory = make_unibyte_string (buf, strlen (buf));
5172 if (! NILP (buffer_defaults.enable_multibyte_characters))
5173 /* At this momemnt, we still don't know how to decode the
5174 direcotry name. So, we keep the bytes in multibyte form so
5175 that ENCODE_FILE correctly gets the original bytes. */
5176 current_buffer->directory
5177 = string_to_multibyte (current_buffer->directory);
5179 /* Add /: to the front of the name
5180 if it would otherwise be treated as magic. */
5181 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5182 if (! NILP (temp)
5183 /* If the default dir is just /, TEMP is non-nil
5184 because of the ange-ftp completion handler.
5185 However, it is not necessary to turn / into /:/.
5186 So avoid doing that. */
5187 && strcmp ("/", SDATA (current_buffer->directory)))
5188 current_buffer->directory
5189 = concat2 (build_string ("/:"), current_buffer->directory);
5191 temp = get_minibuffer (0);
5192 XBUFFER (temp)->directory = current_buffer->directory;
5195 /* initialize the buffer routines */
5196 void
5197 syms_of_buffer ()
5199 staticpro (&last_overlay_modification_hooks);
5200 last_overlay_modification_hooks
5201 = Fmake_vector (make_number (10), Qnil);
5203 staticpro (&Vbuffer_defaults);
5204 staticpro (&Vbuffer_local_symbols);
5205 staticpro (&Qfundamental_mode);
5206 staticpro (&Qmode_class);
5207 staticpro (&QSFundamental);
5208 staticpro (&Vbuffer_alist);
5209 staticpro (&Qprotected_field);
5210 staticpro (&Qpermanent_local);
5211 staticpro (&Qkill_buffer_hook);
5212 Qoverlayp = intern ("overlayp");
5213 staticpro (&Qoverlayp);
5214 Qevaporate = intern ("evaporate");
5215 staticpro (&Qevaporate);
5216 Qmodification_hooks = intern ("modification-hooks");
5217 staticpro (&Qmodification_hooks);
5218 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5219 staticpro (&Qinsert_in_front_hooks);
5220 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5221 staticpro (&Qinsert_behind_hooks);
5222 Qget_file_buffer = intern ("get-file-buffer");
5223 staticpro (&Qget_file_buffer);
5224 Qpriority = intern ("priority");
5225 staticpro (&Qpriority);
5226 Qwindow = intern ("window");
5227 staticpro (&Qwindow);
5228 Qbefore_string = intern ("before-string");
5229 staticpro (&Qbefore_string);
5230 Qafter_string = intern ("after-string");
5231 staticpro (&Qafter_string);
5232 Qfirst_change_hook = intern ("first-change-hook");
5233 staticpro (&Qfirst_change_hook);
5234 Qbefore_change_functions = intern ("before-change-functions");
5235 staticpro (&Qbefore_change_functions);
5236 Qafter_change_functions = intern ("after-change-functions");
5237 staticpro (&Qafter_change_functions);
5238 staticpro (&Qucs_set_table_for_input);
5240 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5241 staticpro (&Qkill_buffer_query_functions);
5243 Fput (Qprotected_field, Qerror_conditions,
5244 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5245 Fput (Qprotected_field, Qerror_message,
5246 build_string ("Attempt to modify a protected field"));
5248 /* All these use DEFVAR_LISP_NOPRO because the slots in
5249 buffer_defaults will all be marked via Vbuffer_defaults. */
5251 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5252 &buffer_defaults.mode_line_format,
5253 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5254 This is the same as (default-value 'mode-line-format). */);
5256 DEFVAR_LISP_NOPRO ("default-header-line-format",
5257 &buffer_defaults.header_line_format,
5258 doc: /* Default value of `header-line-format' for buffers that don't override it.
5259 This is the same as (default-value 'header-line-format). */);
5261 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5262 doc: /* Default value of `cursor-type' for buffers that don't override it.
5263 This is the same as (default-value 'cursor-type). */);
5265 DEFVAR_LISP_NOPRO ("default-line-spacing",
5266 &buffer_defaults.extra_line_spacing,
5267 doc: /* Default value of `line-spacing' for buffers that don't override it.
5268 This is the same as (default-value 'line-spacing). */);
5270 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5271 &buffer_defaults.cursor_in_non_selected_windows,
5272 doc: /* Default value of `cursor-in-non-selected-windows'.
5273 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5275 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5276 &buffer_defaults.abbrev_mode,
5277 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5278 This is the same as (default-value 'abbrev-mode). */);
5280 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5281 &buffer_defaults.ctl_arrow,
5282 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5283 This is the same as (default-value 'ctl-arrow). */);
5285 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5286 &buffer_defaults.direction_reversed,
5287 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5288 This is the same as (default-value 'direction-reversed). */);
5290 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5291 &buffer_defaults.enable_multibyte_characters,
5292 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5293 This is the same as (default-value 'enable-multibyte-characters). */);
5295 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5296 &buffer_defaults.buffer_file_coding_system,
5297 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5298 This is the same as (default-value 'buffer-file-coding-system). */);
5300 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5301 &buffer_defaults.truncate_lines,
5302 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5303 This is the same as (default-value 'truncate-lines). */);
5305 DEFVAR_LISP_NOPRO ("default-fill-column",
5306 &buffer_defaults.fill_column,
5307 doc: /* Default value of `fill-column' for buffers that do not override it.
5308 This is the same as (default-value 'fill-column). */);
5310 DEFVAR_LISP_NOPRO ("default-left-margin",
5311 &buffer_defaults.left_margin,
5312 doc: /* Default value of `left-margin' for buffers that do not override it.
5313 This is the same as (default-value 'left-margin). */);
5315 DEFVAR_LISP_NOPRO ("default-tab-width",
5316 &buffer_defaults.tab_width,
5317 doc: /* Default value of `tab-width' for buffers that do not override it.
5318 This is the same as (default-value 'tab-width). */);
5320 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5321 &buffer_defaults.case_fold_search,
5322 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5323 This is the same as (default-value 'case-fold-search). */);
5325 #ifdef DOS_NT
5326 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5327 &buffer_defaults.buffer_file_type,
5328 doc: /* Default file type for buffers that do not override it.
5329 This is the same as (default-value 'buffer-file-type).
5330 The file type is nil for text, t for binary. */);
5331 #endif
5333 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5334 &buffer_defaults.left_margin_cols,
5335 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5336 This is the same as (default-value 'left-margin-width). */);
5338 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5339 &buffer_defaults.right_margin_cols,
5340 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5341 This is the same as (default-value 'right-margin-width). */);
5343 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5344 &buffer_defaults.left_fringe_width,
5345 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5346 This is the same as (default-value 'left-fringe-width). */);
5348 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5349 &buffer_defaults.right_fringe_width,
5350 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5351 This is the same as (default-value 'right-fringe-width). */);
5353 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5354 &buffer_defaults.fringes_outside_margins,
5355 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5356 This is the same as (default-value 'fringes-outside-margins). */);
5358 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5359 &buffer_defaults.scroll_bar_width,
5360 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5361 This is the same as (default-value 'scroll-bar-width). */);
5363 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5364 &buffer_defaults.vertical_scroll_bar_type,
5365 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5366 This is the same as (default-value 'vertical-scroll-bar). */);
5368 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5369 &buffer_defaults.indicate_empty_lines,
5370 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5371 This is the same as (default-value 'indicate-empty-lines). */);
5373 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5374 &buffer_defaults.indicate_buffer_boundaries,
5375 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5376 This is the same as (default-value 'indicate-buffer-boundaries). */);
5378 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5379 &buffer_defaults.scroll_up_aggressively,
5380 doc: /* Default value of `scroll-up-aggressively'.
5381 This value applies in buffers that don't have their own local values.
5382 This variable is an alias for (default-value 'scroll-up-aggressively). */);
5384 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5385 &buffer_defaults.scroll_down_aggressively,
5386 doc: /* Default value of `scroll-down-aggressively'.
5387 This value applies in buffers that don't have their own local values.
5388 This variable is an alias for (default-value 'scroll-down-aggressively). */);
5390 DEFVAR_PER_BUFFER ("header-line-format",
5391 &current_buffer->header_line_format,
5392 Qnil,
5393 doc: /* Analogous to `mode-line-format', but controls the header line.
5394 The header line appears, optionally, at the top of a window;
5395 the mode line appears at the bottom. */);
5397 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5398 Qnil,
5399 doc: /* Template for displaying mode line for current buffer.
5400 Each buffer has its own value of this variable.
5401 Value may be nil, a string, a symbol or a list or cons cell.
5402 A value of nil means don't display a mode line.
5403 For a symbol, its value is used (but it is ignored if t or nil).
5404 A string appearing directly as the value of a symbol is processed verbatim
5405 in that the %-constructs below are not recognized.
5406 Note that unless the symbol is marked as a `risky-local-variable', all
5407 properties in any strings, as well as all :eval and :propertize forms
5408 in the value of that symbol will be ignored.
5409 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5410 is used as a mode line element. Be careful--FORM should not load any files,
5411 because that can cause an infinite recursion.
5412 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5413 with the specified properties PROPS applied.
5414 For a list whose car is a symbol, the symbol's value is taken,
5415 and if that is non-nil, the cadr of the list is processed recursively.
5416 Otherwise, the caddr of the list (if there is one) is processed.
5417 For a list whose car is a string or list, each element is processed
5418 recursively and the results are effectively concatenated.
5419 For a list whose car is an integer, the cdr of the list is processed
5420 and padded (if the number is positive) or truncated (if negative)
5421 to the width specified by that number.
5422 A string is printed verbatim in the mode line except for %-constructs:
5423 (%-constructs are allowed when the string is the entire mode-line-format
5424 or when it is found in a cons-cell or a list)
5425 %b -- print buffer name. %f -- print visited file name.
5426 %F -- print frame name.
5427 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5428 %& is like %*, but ignore read-only-ness.
5429 % means buffer is read-only and * means it is modified.
5430 For a modified read-only buffer, %* gives % and %+ gives *.
5431 %s -- print process status. %l -- print the current line number.
5432 %c -- print the current column number (this makes editing slower).
5433 To make the column number update correctly in all cases,
5434 `column-number-mode' must be non-nil.
5435 %i -- print the size of the buffer.
5436 %I -- like %i, but use k, M, G, etc., to abbreviate.
5437 %p -- print percent of buffer above top of window, or Top, Bot or All.
5438 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5439 or print Bottom or All.
5440 %m -- print the mode name.
5441 %n -- print Narrow if appropriate.
5442 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.
5443 %Z -- like %z, but including the end-of-line format.
5444 %[ -- print one [ for each recursive editing level. %] similar.
5445 %% -- print %. %- -- print infinitely many dashes.
5446 Decimal digits after the % specify field width to which to pad. */);
5448 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5449 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5450 nil here means use current buffer's major mode, provided it is not
5451 marked as "special".
5453 When a mode is used by default, `find-file' switches to it
5454 before it reads the contents into the buffer and before
5455 it finishes setting up the buffer. Thus, the mode and
5456 its hooks should not expect certain variables such as
5457 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5459 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5460 make_number (Lisp_Symbol),
5461 doc: /* Symbol for current buffer's major mode. */);
5463 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5464 Qnil,
5465 doc: /* Pretty name of current buffer's major mode (a string). */);
5467 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5468 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5470 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5471 Qnil,
5472 doc: /* *Non-nil if searches and matches should ignore case. */);
5474 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5475 make_number (Lisp_Int),
5476 doc: /* *Column beyond which automatic line-wrapping should happen.
5477 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5479 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5480 make_number (Lisp_Int),
5481 doc: /* *Column for the default indent-line-function to indent to.
5482 Linefeed indents to this column in Fundamental mode. */);
5484 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5485 make_number (Lisp_Int),
5486 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5488 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5489 doc: /* *Non-nil means display control chars with uparrow.
5490 A value of nil means use backslash and octal digits.
5491 This variable does not apply to characters whose display is specified
5492 in the current display table (if there is one). */);
5494 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5495 &current_buffer->enable_multibyte_characters,
5496 Qnil,
5497 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5498 Otherwise they are regarded as unibyte. This affects the display,
5499 file I/O and the behavior of various editing commands.
5501 This variable is buffer-local but you cannot set it directly;
5502 use the function `set-buffer-multibyte' to change a buffer's representation.
5503 Changing its default value with `setq-default' is supported.
5504 See also variable `default-enable-multibyte-characters' and Info node
5505 `(elisp)Text Representations'. */);
5506 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5508 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5509 &current_buffer->buffer_file_coding_system, Qnil,
5510 doc: /* Coding system to be used for encoding the buffer contents on saving.
5511 This variable applies to saving the buffer, and also to `write-region'
5512 and other functions that use `write-region'.
5513 It does not apply to sending output to subprocesses, however.
5515 If this is nil, the buffer is saved without any code conversion
5516 unless some coding system is specified in `file-coding-system-alist'
5517 for the buffer file.
5519 If the text to be saved cannot be encoded as specified by this variable,
5520 an alternative encoding is selected by `select-safe-coding-system', which see.
5522 The variable `coding-system-for-write', if non-nil, overrides this variable.
5524 This variable is never applied to a way of decoding a file while reading it. */);
5526 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5527 Qnil,
5528 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5530 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5531 doc: /* *Non-nil means do not display continuation lines.
5532 Instead, give each line of text just one screen line.
5534 Note that this is overridden by the variable
5535 `truncate-partial-width-windows' if that variable is non-nil
5536 and this buffer is not full-frame width. */);
5538 #ifdef DOS_NT
5539 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5540 Qnil,
5541 doc: /* Non-nil if the visited file is a binary file.
5542 This variable is meaningful on MS-DOG and Windows NT.
5543 On those systems, it is automatically local in every buffer.
5544 On other systems, this variable is normally always nil. */);
5545 #endif
5547 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5548 make_number (Lisp_String),
5549 doc: /* Name of default directory of current buffer. Should end with slash.
5550 To interactively change the default directory, use command `cd'. */);
5552 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5553 Qnil,
5554 doc: /* Function called (if non-nil) to perform auto-fill.
5555 It is called after self-inserting any character specified in
5556 the `auto-fill-chars' table.
5557 NOTE: This variable is not a hook;
5558 its value may not be a list of functions. */);
5560 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5561 make_number (Lisp_String),
5562 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5564 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5565 make_number (Lisp_String),
5566 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5567 The truename of a file is calculated by `file-truename'
5568 and then abbreviated with `abbreviate-file-name'. */);
5570 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5571 &current_buffer->auto_save_file_name,
5572 make_number (Lisp_String),
5573 doc: /* Name of file for auto-saving current buffer.
5574 If it is nil, that means don't auto-save this buffer. */);
5576 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5577 doc: /* Non-nil if this buffer is read-only. */);
5579 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5580 doc: /* Non-nil if this buffer's file has been backed up.
5581 Backing up is done before the first time the file is saved. */);
5583 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5584 make_number (Lisp_Int),
5585 doc: /* Length of current buffer when last read in, saved or auto-saved.
5586 0 initially. */);
5588 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5589 Qnil,
5590 doc: /* Non-nil enables selective display.
5591 An Integer N as value means display only lines
5592 that start with less than n columns of space.
5593 A value of t means that the character ^M makes itself and
5594 all the rest of the line invisible; also, when saving the buffer
5595 in a file, save the ^M as a newline. */);
5597 #ifndef old
5598 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5599 &current_buffer->selective_display_ellipses,
5600 Qnil,
5601 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5602 #endif
5604 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5605 doc: /* Non-nil if self-insertion should replace existing text.
5606 The value should be one of `overwrite-mode-textual',
5607 `overwrite-mode-binary', or nil.
5608 If it is `overwrite-mode-textual', self-insertion still
5609 inserts at the end of a line, and inserts when point is before a tab,
5610 until the tab is filled in.
5611 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5613 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5614 Qnil,
5615 doc: /* Display table that controls display of the contents of current buffer.
5617 If this variable is nil, the value of `standard-display-table' is used.
5618 Each window can have its own, overriding display table, see
5619 `set-window-display-table' and `window-display-table'.
5621 The display table is a char-table created with `make-display-table'.
5622 A char-table is an array indexed by character codes. Normal array
5623 primitives `aref' and `aset' can be used to access elements of a char-table.
5625 Each of the char-table elements control how to display the corresponding
5626 text character: the element at index C in the table says how to display
5627 the character whose code is C. Each element should be a vector of
5628 characters or nil. nil means display the character in the default fashion;
5629 otherwise, the characters from the vector are delivered to the screen
5630 instead of the original character.
5632 For example, (aset buffer-display-table ?X ?Y) will cause Emacs to display
5633 a capital Y instead of each X character.
5635 In addition, a char-table has six extra slots to control the display of:
5637 the end of a truncated screen line (extra-slot 0, a single character);
5638 the end of a continued line (extra-slot 1, a single character);
5639 the escape character used to display character codes in octal
5640 (extra-slot 2, a single character);
5641 the character used as an arrow for control characters (extra-slot 3,
5642 a single character);
5643 the decoration indicating the presence of invisible lines (extra-slot 4,
5644 a vector of characters);
5645 the character used to draw the border between side-by-side windows
5646 (extra-slot 5, a single character).
5648 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5650 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5651 Qnil,
5652 doc: /* *Width of left marginal area for display of a buffer.
5653 A value of nil means no marginal area. */);
5655 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5656 Qnil,
5657 doc: /* *Width of right marginal area for display of a buffer.
5658 A value of nil means no marginal area. */);
5660 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5661 Qnil,
5662 doc: /* *Width of this buffer's left fringe (in pixels).
5663 A value of 0 means no left fringe is shown in this buffer's window.
5664 A value of nil means to use the left fringe width from the window's frame. */);
5666 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5667 Qnil,
5668 doc: /* *Width of this buffer's right fringe (in pixels).
5669 A value of 0 means no right fringe is shown in this buffer's window.
5670 A value of nil means to use the right fringe width from the window's frame. */);
5672 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5673 Qnil,
5674 doc: /* *Non-nil means to display fringes outside display margins.
5675 A value of nil means to display fringes between margins and buffer text. */);
5677 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5678 Qnil,
5679 doc: /* *Width of this buffer's scroll bars in pixels.
5680 A value of nil means to use the scroll bar width from the window's frame. */);
5682 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5683 Qnil,
5684 doc: /* *Position of this buffer's vertical scroll bar.
5685 The value takes effect whenever you tell a window to display this buffer;
5686 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5688 A value of `left' or `right' means put the vertical scroll bar at that side
5689 of the window; a value of nil means don't show any vertical scroll bars.
5690 A value of t (the default) means do whatever the window's frame specifies. */);
5692 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5693 &current_buffer->indicate_empty_lines, Qnil,
5694 doc: /* *Visually indicate empty lines after the buffer end.
5695 If non-nil, a bitmap is displayed in the left fringe of a window on
5696 window-systems. */);
5698 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5699 &current_buffer->indicate_buffer_boundaries, Qnil,
5700 doc: /* *Visually indicate buffer boundaries and scrolling.
5701 If non-nil, the first and last line of the buffer are marked in the fringe
5702 of a window on window-systems with angle bitmaps, or if the window can be
5703 scrolled, the top and bottom line of the window are marked with up and down
5704 arrow bitmaps.
5706 If value is a symbol `left' or `right', both angle and arrow bitmaps
5707 are displayed in the left or right fringe, resp. Any other value
5708 that doesn't look like an alist means display the angle bitmaps in
5709 the left fringe but no arrows.
5711 You can exercise more precise control by using an alist as the
5712 value. Each alist element (INDICATOR . POSITION) specifies
5713 where to show one of the indicators. INDICATOR is one of `top',
5714 `bottom', `up', `down', or t, which specifies the default position,
5715 and POSITION is one of `left', `right', or nil, meaning do not show
5716 this indicator.
5718 For example, ((top . left) (t . right)) places the top angle bitmap in
5719 left fringe, the bottom angle bitmap in right fringe, and both arrow
5720 bitmaps in right fringe. To show just the angle bitmaps in the left
5721 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5723 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5724 &current_buffer->scroll_up_aggressively, Qnil,
5725 doc: /* How far to scroll windows upward.
5726 If you move point off the bottom, the window scrolls automatically.
5727 This variable controls how far it scrolls. nil, the default,
5728 means scroll to center point. A fraction means scroll to put point
5729 that fraction of the window's height from the bottom of the window.
5730 When the value is 0.0, point goes at the bottom line, which in the simple
5731 case that you moved off with C-f means scrolling just one line. 1.0 means
5732 point goes at the top, so that in that simple case, the window
5733 scrolls by a full window height. Meaningful values are
5734 between 0.0 and 1.0, inclusive. */);
5736 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5737 &current_buffer->scroll_down_aggressively, Qnil,
5738 doc: /* How far to scroll windows downward.
5739 If you move point off the top, the window scrolls automatically.
5740 This variable controls how far it scrolls. nil, the default,
5741 means scroll to center point. A fraction means scroll to put point
5742 that fraction of the window's height from the top of the window.
5743 When the value is 0.0, point goes at the top line, which in the simple
5744 case that you moved off with C-b means scrolling just one line. 1.0 means
5745 point goes at the bottom, so that in that simple case, the window
5746 scrolls by a full window height. Meaningful values are
5747 between 0.0 and 1.0, inclusive. */);
5749 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5750 "Don't ask.");
5753 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5754 doc: /* List of functions to call before each text change.
5755 Two arguments are passed to each function: the positions of
5756 the beginning and end of the range of old text to be changed.
5757 \(For an insertion, the beginning and end are at the same place.)
5758 No information is given about the length of the text after the change.
5760 Buffer changes made while executing the `before-change-functions'
5761 don't call any before-change or after-change functions.
5762 That's because these variables are temporarily set to nil.
5763 As a result, a hook function cannot straightforwardly alter the value of
5764 these variables. See the Emacs Lisp manual for a way of
5765 accomplishing an equivalent result by using other variables.
5767 If an unhandled error happens in running these functions,
5768 the variable's value remains nil. That prevents the error
5769 from happening repeatedly and making Emacs nonfunctional. */);
5770 Vbefore_change_functions = Qnil;
5772 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5773 doc: /* List of functions to call after each text change.
5774 Three arguments are passed to each function: the positions of
5775 the beginning and end of the range of changed text,
5776 and the length in bytes of the pre-change text replaced by that range.
5777 \(For an insertion, the pre-change length is zero;
5778 for a deletion, that length is the number of bytes deleted,
5779 and the post-change beginning and end are at the same place.)
5781 Buffer changes made while executing the `after-change-functions'
5782 don't call any before-change or after-change functions.
5783 That's because these variables are temporarily set to nil.
5784 As a result, a hook function cannot straightforwardly alter the value of
5785 these variables. See the Emacs Lisp manual for a way of
5786 accomplishing an equivalent result by using other variables.
5788 If an unhandled error happens in running these functions,
5789 the variable's value remains nil. That prevents the error
5790 from happening repeatedly and making Emacs nonfunctional. */);
5791 Vafter_change_functions = Qnil;
5793 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5794 doc: /* A list of functions to call before changing a buffer which is unmodified.
5795 The functions are run using the `run-hooks' function. */);
5796 Vfirst_change_hook = Qnil;
5798 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5799 doc: /* List of undo entries in current buffer.
5800 Recent changes come first; older changes follow newer.
5802 An entry (BEG . END) represents an insertion which begins at
5803 position BEG and ends at position END.
5805 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5806 from (abs POSITION). If POSITION is positive, point was at the front
5807 of the text being deleted; if negative, point was at the end.
5809 An entry (t HIGH . LOW) indicates that the buffer previously had
5810 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5811 of the visited file's modification time, as of that time. If the
5812 modification time of the most recent save is different, this entry is
5813 obsolete.
5815 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5816 was modified between BEG and END. PROPERTY is the property name,
5817 and VALUE is the old value.
5819 An entry (apply FUN-NAME . ARGS) means undo the change with
5820 \(apply FUN-NAME ARGS).
5822 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5823 in the active region. BEG and END is the range affected by this entry
5824 and DELTA is the number of bytes added or deleted in that range by
5825 this change.
5827 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5828 was adjusted in position by the offset DISTANCE (an integer).
5830 An entry of the form POSITION indicates that point was at the buffer
5831 location given by the integer. Undoing an entry of this form places
5832 point at POSITION.
5834 nil marks undo boundaries. The undo command treats the changes
5835 between two undo boundaries as a single step to be undone.
5837 If the value of the variable is t, undo information is not recorded. */);
5839 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5840 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
5842 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5843 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
5845 Normally, the line-motion functions work by scanning the buffer for
5846 newlines. Columnar operations (like `move-to-column' and
5847 `compute-motion') also work by scanning the buffer, summing character
5848 widths as they go. This works well for ordinary text, but if the
5849 buffer's lines are very long (say, more than 500 characters), these
5850 motion functions will take longer to execute. Emacs may also take
5851 longer to update the display.
5853 If `cache-long-line-scans' is non-nil, these motion functions cache the
5854 results of their scans, and consult the cache to avoid rescanning
5855 regions of the buffer until the text is modified. The caches are most
5856 beneficial when they prevent the most searching---that is, when the
5857 buffer contains long lines and large regions of characters with the
5858 same, fixed screen width.
5860 When `cache-long-line-scans' is non-nil, processing short lines will
5861 become slightly slower (because of the overhead of consulting the
5862 cache), and the caches will use memory roughly proportional to the
5863 number of newlines and characters whose screen width varies.
5865 The caches require no explicit maintenance; their accuracy is
5866 maintained internally by the Emacs primitives. Enabling or disabling
5867 the cache should not affect the behavior of any of the motion
5868 functions; it should only affect their performance. */);
5870 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5871 doc: /* Value of point before the last series of scroll operations, or nil. */);
5873 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5874 doc: /* List of formats to use when saving this buffer.
5875 Formats are defined by `format-alist'. This variable is
5876 set when a file is visited. */);
5878 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
5879 &current_buffer->auto_save_file_format, Qnil,
5880 doc: /* *Format in which to write auto-save files.
5881 Should be a list of symbols naming formats that are defined in `format-alist'.
5882 If it is t, which is the default, auto-save files are written in the
5883 same format as a regular save would use. */);
5885 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5886 &current_buffer->invisibility_spec, Qnil,
5887 doc: /* Invisibility spec of this buffer.
5888 The default is t, which means that text is invisible
5889 if it has a non-nil `invisible' property.
5890 If the value is a list, a text character is invisible if its `invisible'
5891 property is an element in that list.
5892 If an element is a cons cell of the form (PROP . ELLIPSIS),
5893 then characters with property value PROP are invisible,
5894 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
5896 DEFVAR_PER_BUFFER ("buffer-display-count",
5897 &current_buffer->display_count, Qnil,
5898 doc: /* A number incremented each time this buffer is displayed in a window.
5899 The function `set-window-buffer' increments it. */);
5901 DEFVAR_PER_BUFFER ("buffer-display-time",
5902 &current_buffer->display_time, Qnil,
5903 doc: /* Time stamp updated each time this buffer is displayed in a window.
5904 The function `set-window-buffer' updates this variable
5905 to the value obtained by calling `current-time'.
5906 If the buffer has never been shown in a window, the value is nil. */);
5908 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
5909 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
5910 Non-nil also enables highlighting of the region whenever the mark is active.
5911 The variable `highlight-nonselected-windows' controls whether to highlight
5912 all windows or just the selected window.
5914 If the value is `lambda', that enables Transient Mark mode temporarily
5915 until the next buffer modification. If a command sets the value to `only',
5916 that enables Transient Mark mode for the following command only.
5917 During that following command, the value of `transient-mark-mode'
5918 is `identity'. If it is still `identity' at the end of that command,
5919 it changes to nil. */);
5920 Vtransient_mark_mode = Qnil;
5922 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
5923 doc: /* *Non-nil means disregard read-only status of buffers or characters.
5924 If the value is t, disregard `buffer-read-only' and all `read-only'
5925 text properties. If the value is a list, disregard `buffer-read-only'
5926 and disregard a `read-only' text property if the property value
5927 is a member of the list. */);
5928 Vinhibit_read_only = Qnil;
5930 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
5931 doc: /* Cursor to use when this buffer is in the selected window.
5932 Values are interpreted as follows:
5934 t use the cursor specified for the frame
5935 nil don't display a cursor
5936 box display a filled box cursor
5937 hollow display a hollow box cursor
5938 bar display a vertical bar cursor with default width
5939 (bar . WIDTH) display a vertical bar cursor with width WIDTH
5940 hbar display a horizontal bar cursor with default height
5941 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
5942 ANYTHING ELSE display a hollow box cursor
5944 When the buffer is displayed in a nonselected window,
5945 this variable has no effect; the cursor appears as a hollow box. */);
5947 DEFVAR_PER_BUFFER ("line-spacing",
5948 &current_buffer->extra_line_spacing, Qnil,
5949 doc: /* Additional space to put between lines when displaying a buffer.
5950 The space is measured in pixels, and put below lines on window systems.
5951 If value is a floating point number, it specifies the spacing relative
5952 to the default frame line height. */);
5954 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
5955 &current_buffer->cursor_in_non_selected_windows, Qnil,
5956 doc: /* *Cursor type to display in non-selected windows.
5957 t means to use hollow box cursor. See `cursor-type' for other values. */);
5959 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
5960 doc: /* List of functions called with no args to query before killing a buffer. */);
5961 Vkill_buffer_query_functions = Qnil;
5963 defsubr (&Sbuffer_live_p);
5964 defsubr (&Sbuffer_list);
5965 defsubr (&Sget_buffer);
5966 defsubr (&Sget_file_buffer);
5967 defsubr (&Sget_buffer_create);
5968 defsubr (&Smake_indirect_buffer);
5969 defsubr (&Sgenerate_new_buffer_name);
5970 defsubr (&Sbuffer_name);
5971 /*defsubr (&Sbuffer_number);*/
5972 defsubr (&Sbuffer_file_name);
5973 defsubr (&Sbuffer_base_buffer);
5974 defsubr (&Sbuffer_local_value);
5975 defsubr (&Sbuffer_local_variables);
5976 defsubr (&Sbuffer_modified_p);
5977 defsubr (&Sset_buffer_modified_p);
5978 defsubr (&Sbuffer_modified_tick);
5979 defsubr (&Srename_buffer);
5980 defsubr (&Sother_buffer);
5981 defsubr (&Sbuffer_enable_undo);
5982 defsubr (&Skill_buffer);
5983 defsubr (&Sset_buffer_major_mode);
5984 defsubr (&Sswitch_to_buffer);
5985 defsubr (&Spop_to_buffer);
5986 defsubr (&Scurrent_buffer);
5987 defsubr (&Sset_buffer);
5988 defsubr (&Sbarf_if_buffer_read_only);
5989 defsubr (&Sbury_buffer);
5990 defsubr (&Serase_buffer);
5991 defsubr (&Sset_buffer_multibyte);
5992 defsubr (&Skill_all_local_variables);
5994 defsubr (&Soverlayp);
5995 defsubr (&Smake_overlay);
5996 defsubr (&Sdelete_overlay);
5997 defsubr (&Smove_overlay);
5998 defsubr (&Soverlay_start);
5999 defsubr (&Soverlay_end);
6000 defsubr (&Soverlay_buffer);
6001 defsubr (&Soverlay_properties);
6002 defsubr (&Soverlays_at);
6003 defsubr (&Soverlays_in);
6004 defsubr (&Snext_overlay_change);
6005 defsubr (&Sprevious_overlay_change);
6006 defsubr (&Soverlay_recenter);
6007 defsubr (&Soverlay_lists);
6008 defsubr (&Soverlay_get);
6009 defsubr (&Soverlay_put);
6010 defsubr (&Srestore_buffer_modified_p);
6013 void
6014 keys_of_buffer ()
6016 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6017 initial_define_key (control_x_map, 'k', "kill-buffer");
6019 /* This must not be in syms_of_buffer, because Qdisabled is not
6020 initialized when that function gets called. */
6021 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6024 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6025 (do not change this comment) */