notes/copyright: Report status more accurately for non-GPL files.
[emacs.git] / src / buffer.c
blob9766c60da9e09ad9fbdee0b66b0ece02d60c2009
1 /* Buffer manipulation primitives for GNU Emacs.
3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <setjmp.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #include "lisp.h"
36 #include "intervals.h"
37 #include "window.h"
38 #include "commands.h"
39 #include "buffer.h"
40 #include "character.h"
41 #include "region-cache.h"
42 #include "indent.h"
43 #include "blockinput.h"
44 #include "keyboard.h"
45 #include "keymap.h"
46 #include "frame.h"
48 struct buffer *current_buffer; /* the current buffer */
50 /* First buffer in chain of all buffers (in reverse order of creation).
51 Threaded through ->next. */
53 struct buffer *all_buffers;
55 /* This structure holds the default values of the buffer-local variables
56 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
57 The default value occupies the same slot in this structure
58 as an individual buffer's value occupies in that buffer.
59 Setting the default value also goes through the alist of buffers
60 and stores into each buffer that does not say it has a local value. */
62 DECL_ALIGN (struct buffer, buffer_defaults);
64 /* A Lisp_Object pointer to the above, used for staticpro */
66 static Lisp_Object Vbuffer_defaults;
68 /* This structure marks which slots in a buffer have corresponding
69 default values in buffer_defaults.
70 Each such slot has a nonzero value in this structure.
71 The value has only one nonzero bit.
73 When a buffer has its own local value for a slot,
74 the entry for that slot (found in the same slot in this structure)
75 is turned on in the buffer's local_flags array.
77 If a slot in this structure is -1, then even though there may
78 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
79 and the corresponding slot in buffer_defaults is not used.
81 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
82 zero, that is a bug */
84 struct buffer buffer_local_flags;
86 /* This structure holds the names of symbols whose values may be
87 buffer-local. It is indexed and accessed in the same way as the above. */
89 DECL_ALIGN (struct buffer, buffer_local_symbols);
91 /* A Lisp_Object pointer to the above, used for staticpro */
92 static Lisp_Object Vbuffer_local_symbols;
94 /* Return the symbol of the per-buffer variable at offset OFFSET in
95 the buffer structure. */
97 #define PER_BUFFER_SYMBOL(OFFSET) \
98 (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
100 /* Flags indicating which built-in buffer-local variables
101 are permanent locals. */
102 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
104 /* Number of per-buffer variables used. */
106 int last_per_buffer_idx;
108 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
109 int after, Lisp_Object arg1,
110 Lisp_Object arg2, Lisp_Object arg3);
111 static void swap_out_buffer_local_variables (struct buffer *b);
112 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
114 /* Alist of all buffer names vs the buffers. */
115 /* This used to be a variable, but is no longer,
116 to prevent lossage due to user rplac'ing this alist or its elements. */
117 Lisp_Object Vbuffer_alist;
119 /* Functions to call before and after each text change. */
120 Lisp_Object Vbefore_change_functions;
121 Lisp_Object Vafter_change_functions;
123 Lisp_Object Vtransient_mark_mode;
125 /* t means ignore all read-only text properties.
126 A list means ignore such a property if its value is a member of the list.
127 Any non-nil value means ignore buffer-read-only. */
128 Lisp_Object Vinhibit_read_only;
130 /* List of functions to call that can query about killing a buffer.
131 If any of these functions returns nil, we don't kill it. */
132 Lisp_Object Vkill_buffer_query_functions;
133 Lisp_Object Qkill_buffer_query_functions;
135 /* Hook run before changing a major mode. */
136 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
138 /* List of functions to call before changing an unmodified buffer. */
139 Lisp_Object Vfirst_change_hook;
141 Lisp_Object Qfirst_change_hook;
142 Lisp_Object Qbefore_change_functions;
143 Lisp_Object Qafter_change_functions;
144 Lisp_Object Qucs_set_table_for_input;
146 /* If nonzero, all modification hooks are suppressed. */
147 int inhibit_modification_hooks;
149 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
150 Lisp_Object Qpermanent_local_hook;
152 Lisp_Object Qprotected_field;
154 Lisp_Object QSFundamental; /* A string "Fundamental" */
156 Lisp_Object Qkill_buffer_hook;
158 Lisp_Object Qget_file_buffer;
160 Lisp_Object Qoverlayp;
162 Lisp_Object Qpriority, Qevaporate, Qbefore_string, Qafter_string;
164 Lisp_Object Qmodification_hooks;
165 Lisp_Object Qinsert_in_front_hooks;
166 Lisp_Object Qinsert_behind_hooks;
168 static void alloc_buffer_text (struct buffer *, size_t);
169 static void free_buffer_text (struct buffer *b);
170 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
171 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
172 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
174 /* For debugging; temporary. See set_buffer_internal. */
175 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
177 void
178 nsberror (Lisp_Object spec)
180 if (STRINGP (spec))
181 error ("No buffer named %s", SDATA (spec));
182 error ("Invalid buffer argument");
185 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
186 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
187 Value is nil if OBJECT is not a buffer or if it has been killed. */)
188 (Lisp_Object object)
190 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
191 ? Qt : Qnil);
194 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
195 doc: /* Return a list of all existing live buffers.
196 If the optional arg FRAME is a frame, we return the buffer list
197 in the proper order for that frame: the buffers in FRAME's `buffer-list'
198 frame parameter come first, followed by the rest of the buffers. */)
199 (Lisp_Object frame)
201 Lisp_Object general;
202 general = Fmapcar (Qcdr, Vbuffer_alist);
204 if (FRAMEP (frame))
206 Lisp_Object framelist, prevlist, tail;
207 Lisp_Object args[3];
209 CHECK_FRAME (frame);
211 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
212 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
214 /* Remove from GENERAL any buffer that duplicates one in
215 FRAMELIST or PREVLIST. */
216 tail = framelist;
217 while (CONSP (tail))
219 general = Fdelq (XCAR (tail), general);
220 tail = XCDR (tail);
222 tail = prevlist;
223 while (CONSP (tail))
225 general = Fdelq (XCAR (tail), general);
226 tail = XCDR (tail);
229 args[0] = framelist;
230 args[1] = general;
231 args[2] = prevlist;
232 return Fnconc (3, args);
235 return general;
238 /* Like Fassoc, but use Fstring_equal to compare
239 (which ignores text properties),
240 and don't ever QUIT. */
242 static Lisp_Object
243 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
245 register Lisp_Object tail;
246 for (tail = list; CONSP (tail); tail = XCDR (tail))
248 register Lisp_Object elt, tem;
249 elt = XCAR (tail);
250 tem = Fstring_equal (Fcar (elt), key);
251 if (!NILP (tem))
252 return elt;
254 return Qnil;
257 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
258 doc: /* Return the buffer named BUFFER-OR-NAME.
259 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
260 is a string and there is no buffer with that name, return nil. If
261 BUFFER-OR-NAME is a buffer, return it as given. */)
262 (register Lisp_Object buffer_or_name)
264 if (BUFFERP (buffer_or_name))
265 return buffer_or_name;
266 CHECK_STRING (buffer_or_name);
268 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
271 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
272 doc: /* Return the buffer visiting file FILENAME (a string).
273 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
274 If there is no such live buffer, return nil.
275 See also `find-buffer-visiting'. */)
276 (register Lisp_Object filename)
278 register Lisp_Object tail, buf, tem;
279 Lisp_Object handler;
281 CHECK_STRING (filename);
282 filename = Fexpand_file_name (filename, Qnil);
284 /* If the file name has special constructs in it,
285 call the corresponding file handler. */
286 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
287 if (!NILP (handler))
288 return call2 (handler, Qget_file_buffer, filename);
290 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
292 buf = Fcdr (XCAR (tail));
293 if (!BUFFERP (buf)) continue;
294 if (!STRINGP (XBUFFER (buf)->filename)) continue;
295 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
296 if (!NILP (tem))
297 return buf;
299 return Qnil;
302 Lisp_Object
303 get_truename_buffer (register Lisp_Object filename)
305 register Lisp_Object tail, buf, tem;
307 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
309 buf = Fcdr (XCAR (tail));
310 if (!BUFFERP (buf)) continue;
311 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
312 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
313 if (!NILP (tem))
314 return buf;
316 return Qnil;
319 /* Incremented for each buffer created, to assign the buffer number. */
320 int buffer_count;
322 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
323 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
324 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
325 return that buffer. If no such buffer exists, create a new buffer with
326 that name and return it. If BUFFER-OR-NAME starts with a space, the new
327 buffer does not keep undo information.
329 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
330 even if it is dead. The return value is never nil. */)
331 (register Lisp_Object buffer_or_name)
333 register Lisp_Object buffer, name;
334 register struct buffer *b;
336 buffer = Fget_buffer (buffer_or_name);
337 if (!NILP (buffer))
338 return buffer;
340 if (SCHARS (buffer_or_name) == 0)
341 error ("Empty string for buffer name is not allowed");
343 b = allocate_buffer ();
345 /* An ordinary buffer uses its own struct buffer_text. */
346 b->text = &b->own_text;
347 b->base_buffer = 0;
349 BUF_GAP_SIZE (b) = 20;
350 BLOCK_INPUT;
351 /* We allocate extra 1-byte at the tail and keep it always '\0' for
352 anchoring a search. */
353 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
354 UNBLOCK_INPUT;
355 if (! BUF_BEG_ADDR (b))
356 buffer_memory_full ();
358 BUF_PT (b) = BEG;
359 BUF_GPT (b) = BEG;
360 BUF_BEGV (b) = BEG;
361 BUF_ZV (b) = BEG;
362 BUF_Z (b) = BEG;
363 BUF_PT_BYTE (b) = BEG_BYTE;
364 BUF_GPT_BYTE (b) = BEG_BYTE;
365 BUF_BEGV_BYTE (b) = BEG_BYTE;
366 BUF_ZV_BYTE (b) = BEG_BYTE;
367 BUF_Z_BYTE (b) = BEG_BYTE;
368 BUF_MODIFF (b) = 1;
369 BUF_CHARS_MODIFF (b) = 1;
370 BUF_OVERLAY_MODIFF (b) = 1;
371 BUF_SAVE_MODIFF (b) = 1;
372 BUF_INTERVALS (b) = 0;
373 BUF_UNCHANGED_MODIFIED (b) = 1;
374 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
375 BUF_END_UNCHANGED (b) = 0;
376 BUF_BEG_UNCHANGED (b) = 0;
377 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
379 b->newline_cache = 0;
380 b->width_run_cache = 0;
381 b->width_table = Qnil;
382 b->prevent_redisplay_optimizations_p = 1;
384 /* Put this on the chain of all buffers including killed ones. */
385 b->next = all_buffers;
386 all_buffers = b;
388 /* An ordinary buffer normally doesn't need markers
389 to handle BEGV and ZV. */
390 b->pt_marker = Qnil;
391 b->begv_marker = Qnil;
392 b->zv_marker = Qnil;
394 name = Fcopy_sequence (buffer_or_name);
395 STRING_SET_INTERVALS (name, NULL_INTERVAL);
396 b->name = name;
398 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
400 reset_buffer (b);
401 reset_buffer_local_variables (b, 1);
403 b->mark = Fmake_marker ();
404 BUF_MARKERS (b) = NULL;
405 b->name = name;
407 /* Put this in the alist of all live buffers. */
408 XSETBUFFER (buffer, b);
409 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
411 /* An error in calling the function here (should someone redefine it)
412 can lead to infinite regress until you run out of stack. rms
413 says that's not worth protecting against. */
414 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
415 /* buffer is on buffer-alist, so no gcpro. */
416 call1 (Qucs_set_table_for_input, buffer);
418 return buffer;
422 /* Return a list of overlays which is a copy of the overlay list
423 LIST, but for buffer B. */
425 static struct Lisp_Overlay *
426 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
428 Lisp_Object buffer;
429 struct Lisp_Overlay *result = NULL, *tail = NULL;
431 XSETBUFFER (buffer, b);
433 for (; list; list = list->next)
435 Lisp_Object overlay, start, end, old_overlay;
436 EMACS_INT charpos;
438 XSETMISC (old_overlay, list);
439 charpos = marker_position (OVERLAY_START (old_overlay));
440 start = Fmake_marker ();
441 Fset_marker (start, make_number (charpos), buffer);
442 XMARKER (start)->insertion_type
443 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
445 charpos = marker_position (OVERLAY_END (old_overlay));
446 end = Fmake_marker ();
447 Fset_marker (end, make_number (charpos), buffer);
448 XMARKER (end)->insertion_type
449 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
451 overlay = allocate_misc ();
452 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
453 OVERLAY_START (overlay) = start;
454 OVERLAY_END (overlay) = end;
455 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
456 XOVERLAY (overlay)->next = NULL;
458 if (tail)
459 tail = tail->next = XOVERLAY (overlay);
460 else
461 result = tail = XOVERLAY (overlay);
464 return result;
468 /* Clone per-buffer values of buffer FROM.
470 Buffer TO gets the same per-buffer values as FROM, with the
471 following exceptions: (1) TO's name is left untouched, (2) markers
472 are copied and made to refer to TO, and (3) overlay lists are
473 copied. */
475 static void
476 clone_per_buffer_values (struct buffer *from, struct buffer *to)
478 Lisp_Object to_buffer;
479 int offset;
481 XSETBUFFER (to_buffer, to);
483 /* buffer-local Lisp variables start at `undo_list',
484 tho only the ones from `name' on are GC'd normally. */
485 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
486 offset < sizeof *to;
487 offset += sizeof (Lisp_Object))
489 Lisp_Object obj;
491 /* Don't touch the `name' which should be unique for every buffer. */
492 if (offset == PER_BUFFER_VAR_OFFSET (name))
493 continue;
495 obj = PER_BUFFER_VALUE (from, offset);
496 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
498 struct Lisp_Marker *m = XMARKER (obj);
499 obj = Fmake_marker ();
500 XMARKER (obj)->insertion_type = m->insertion_type;
501 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
504 PER_BUFFER_VALUE (to, offset) = obj;
507 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
509 to->overlays_before = copy_overlays (to, from->overlays_before);
510 to->overlays_after = copy_overlays (to, from->overlays_after);
512 /* Get (a copy of) the alist of Lisp-level local variables of FROM
513 and install that in TO. */
514 to->local_var_alist = buffer_lisp_local_variables (from);
517 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
518 2, 3,
519 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
520 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
521 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
522 NAME should be a string which is not the name of an existing buffer.
523 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
524 such as major and minor modes, in the indirect buffer.
525 CLONE nil means the indirect buffer's state is reset to default values. */)
526 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
528 Lisp_Object buf, tem;
529 struct buffer *b;
531 CHECK_STRING (name);
532 buf = Fget_buffer (name);
533 if (!NILP (buf))
534 error ("Buffer name `%s' is in use", SDATA (name));
536 tem = base_buffer;
537 base_buffer = Fget_buffer (base_buffer);
538 if (NILP (base_buffer))
539 error ("No such buffer: `%s'", SDATA (tem));
540 if (NILP (XBUFFER (base_buffer)->name))
541 error ("Base buffer has been killed");
543 if (SCHARS (name) == 0)
544 error ("Empty string for buffer name is not allowed");
546 b = allocate_buffer ();
548 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
549 ? XBUFFER (base_buffer)->base_buffer
550 : XBUFFER (base_buffer));
552 /* Use the base buffer's text object. */
553 b->text = b->base_buffer->text;
555 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
556 BUF_ZV (b) = BUF_ZV (b->base_buffer);
557 BUF_PT (b) = BUF_PT (b->base_buffer);
558 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
559 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
560 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
562 b->newline_cache = 0;
563 b->width_run_cache = 0;
564 b->width_table = Qnil;
566 /* Put this on the chain of all buffers including killed ones. */
567 b->next = all_buffers;
568 all_buffers = b;
570 name = Fcopy_sequence (name);
571 STRING_SET_INTERVALS (name, NULL_INTERVAL);
572 b->name = name;
574 reset_buffer (b);
575 reset_buffer_local_variables (b, 1);
577 /* Put this in the alist of all live buffers. */
578 XSETBUFFER (buf, b);
579 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
581 b->mark = Fmake_marker ();
582 b->name = name;
584 /* The multibyte status belongs to the base buffer. */
585 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
587 /* Make sure the base buffer has markers for its narrowing. */
588 if (NILP (b->base_buffer->pt_marker))
590 b->base_buffer->pt_marker = Fmake_marker ();
591 set_marker_both (b->base_buffer->pt_marker, base_buffer,
592 BUF_PT (b->base_buffer),
593 BUF_PT_BYTE (b->base_buffer));
595 if (NILP (b->base_buffer->begv_marker))
597 b->base_buffer->begv_marker = Fmake_marker ();
598 set_marker_both (b->base_buffer->begv_marker, base_buffer,
599 BUF_BEGV (b->base_buffer),
600 BUF_BEGV_BYTE (b->base_buffer));
602 if (NILP (b->base_buffer->zv_marker))
604 b->base_buffer->zv_marker = Fmake_marker ();
605 set_marker_both (b->base_buffer->zv_marker, base_buffer,
606 BUF_ZV (b->base_buffer),
607 BUF_ZV_BYTE (b->base_buffer));
608 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
611 if (NILP (clone))
613 /* Give the indirect buffer markers for its narrowing. */
614 b->pt_marker = Fmake_marker ();
615 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
616 b->begv_marker = Fmake_marker ();
617 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
618 b->zv_marker = Fmake_marker ();
619 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
620 XMARKER (b->zv_marker)->insertion_type = 1;
622 else
624 struct buffer *old_b = current_buffer;
626 clone_per_buffer_values (b->base_buffer, b);
627 b->filename = Qnil;
628 b->file_truename = Qnil;
629 b->display_count = make_number (0);
630 b->backed_up = Qnil;
631 b->auto_save_file_name = Qnil;
632 set_buffer_internal_1 (b);
633 Fset (intern ("buffer-save-without-query"), Qnil);
634 Fset (intern ("buffer-file-number"), Qnil);
635 Fset (intern ("buffer-stale-function"), Qnil);
636 set_buffer_internal_1 (old_b);
639 return buf;
642 void
643 delete_all_overlays (struct buffer *b)
645 Lisp_Object overlay;
647 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
648 have to empty the list, otherwise we end up with overlays that
649 think they belong to this buffer while the buffer doesn't know about
650 them any more. */
651 while (b->overlays_before)
653 XSETMISC (overlay, b->overlays_before);
654 Fdelete_overlay (overlay);
656 while (b->overlays_after)
658 XSETMISC (overlay, b->overlays_after);
659 Fdelete_overlay (overlay);
661 eassert (b->overlays_before == NULL);
662 eassert (b->overlays_after == NULL);
665 /* Reinitialize everything about a buffer except its name and contents
666 and local variables.
667 If called on an already-initialized buffer, the list of overlays
668 should be deleted before calling this function, otherwise we end up
669 with overlays that claim to belong to the buffer but the buffer
670 claims it doesn't belong to it. */
672 void
673 reset_buffer (register struct buffer *b)
675 b->filename = Qnil;
676 b->file_truename = Qnil;
677 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
678 b->modtime = 0;
679 b->modtime_size = -1;
680 XSETFASTINT (b->save_length, 0);
681 b->last_window_start = 1;
682 /* It is more conservative to start out "changed" than "unchanged". */
683 b->clip_changed = 0;
684 b->prevent_redisplay_optimizations_p = 1;
685 b->backed_up = Qnil;
686 BUF_AUTOSAVE_MODIFF (b) = 0;
687 b->auto_save_failure_time = -1;
688 b->auto_save_file_name = Qnil;
689 b->read_only = Qnil;
690 b->overlays_before = NULL;
691 b->overlays_after = NULL;
692 b->overlay_center = BEG;
693 b->mark_active = Qnil;
694 b->point_before_scroll = Qnil;
695 b->file_format = Qnil;
696 b->auto_save_file_format = Qt;
697 b->last_selected_window = Qnil;
698 XSETINT (b->display_count, 0);
699 b->display_time = Qnil;
700 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
701 b->cursor_type = buffer_defaults.cursor_type;
702 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
704 b->display_error_modiff = 0;
707 /* Reset buffer B's local variables info.
708 Don't use this on a buffer that has already been in use;
709 it does not treat permanent locals consistently.
710 Instead, use Fkill_all_local_variables.
712 If PERMANENT_TOO is 1, then we reset permanent
713 buffer-local variables. If PERMANENT_TOO is 0,
714 we preserve those. */
716 static void
717 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
719 register int offset;
720 int i;
722 /* Reset the major mode to Fundamental, together with all the
723 things that depend on the major mode.
724 default-major-mode is handled at a higher level.
725 We ignore it here. */
726 b->major_mode = Qfundamental_mode;
727 b->keymap = Qnil;
728 b->mode_name = QSFundamental;
729 b->minor_modes = Qnil;
731 /* If the standard case table has been altered and invalidated,
732 fix up its insides first. */
733 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
734 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
735 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
736 Fset_standard_case_table (Vascii_downcase_table);
738 b->downcase_table = Vascii_downcase_table;
739 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
740 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
741 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
742 b->invisibility_spec = Qt;
743 #ifndef DOS_NT
744 b->buffer_file_type = Qnil;
745 #endif
747 /* Reset all (or most) per-buffer variables to their defaults. */
748 if (permanent_too)
749 b->local_var_alist = Qnil;
750 else
752 Lisp_Object tmp, prop, last = Qnil;
753 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
754 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
756 /* If permanent-local, keep it. */
757 last = tmp;
758 if (EQ (prop, Qpermanent_local_hook))
760 /* This is a partially permanent hook variable.
761 Preserve only the elements that want to be preserved. */
762 Lisp_Object list, newlist;
763 list = XCDR (XCAR (tmp));
764 if (!CONSP (list))
765 newlist = list;
766 else
767 for (newlist = Qnil; CONSP (list); list = XCDR (list))
769 Lisp_Object elt = XCAR (list);
770 /* Preserve element ELT if it's t,
771 if it is a function with a `permanent-local-hook' property,
772 or if it's not a symbol. */
773 if (! SYMBOLP (elt)
774 || EQ (elt, Qt)
775 || !NILP (Fget (elt, Qpermanent_local_hook)))
776 newlist = Fcons (elt, newlist);
778 XSETCDR (XCAR (tmp), Fnreverse (newlist));
781 /* Delete this local variable. */
782 else if (NILP (last))
783 b->local_var_alist = XCDR (tmp);
784 else
785 XSETCDR (last, XCDR (tmp));
788 for (i = 0; i < last_per_buffer_idx; ++i)
789 if (permanent_too || buffer_permanent_local_flags[i] == 0)
790 SET_PER_BUFFER_VALUE_P (b, i, 0);
792 /* For each slot that has a default value,
793 copy that into the slot. */
795 /* buffer-local Lisp variables start at `undo_list',
796 tho only the ones from `name' on are GC'd normally. */
797 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
798 offset < sizeof *b;
799 offset += sizeof (Lisp_Object))
801 int idx = PER_BUFFER_IDX (offset);
802 if ((idx > 0
803 && (permanent_too
804 || buffer_permanent_local_flags[idx] == 0)))
805 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
809 /* We split this away from generate-new-buffer, because rename-buffer
810 and set-visited-file-name ought to be able to use this to really
811 rename the buffer properly. */
813 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
814 1, 2, 0,
815 doc: /* Return a string that is the name of no existing buffer based on NAME.
816 If there is no live buffer named NAME, then return NAME.
817 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
818 \(starting at 2) until an unused name is found, and then return that name.
819 Optional second argument IGNORE specifies a name that is okay to use (if
820 it is in the sequence to be tried) even if a buffer with that name exists. */)
821 (register Lisp_Object name, Lisp_Object ignore)
823 register Lisp_Object gentemp, tem;
824 int count;
825 char number[10];
827 CHECK_STRING (name);
829 tem = Fstring_equal (name, ignore);
830 if (!NILP (tem))
831 return name;
832 tem = Fget_buffer (name);
833 if (NILP (tem))
834 return name;
836 count = 1;
837 while (1)
839 sprintf (number, "<%d>", ++count);
840 gentemp = concat2 (name, build_string (number));
841 tem = Fstring_equal (gentemp, ignore);
842 if (!NILP (tem))
843 return gentemp;
844 tem = Fget_buffer (gentemp);
845 if (NILP (tem))
846 return gentemp;
851 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
852 doc: /* Return the name of BUFFER, as a string.
853 BUFFER defaults to the current buffer.
854 Return nil if BUFFER has been killed. */)
855 (register Lisp_Object buffer)
857 if (NILP (buffer))
858 return current_buffer->name;
859 CHECK_BUFFER (buffer);
860 return XBUFFER (buffer)->name;
863 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
864 doc: /* Return name of file BUFFER is visiting, or nil if none.
865 No argument or nil as argument means use the current buffer. */)
866 (register Lisp_Object buffer)
868 if (NILP (buffer))
869 return current_buffer->filename;
870 CHECK_BUFFER (buffer);
871 return XBUFFER (buffer)->filename;
874 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
875 0, 1, 0,
876 doc: /* Return the base buffer of indirect buffer BUFFER.
877 If BUFFER is not indirect, return nil.
878 BUFFER defaults to the current buffer. */)
879 (register Lisp_Object buffer)
881 struct buffer *base;
882 Lisp_Object base_buffer;
884 if (NILP (buffer))
885 base = current_buffer->base_buffer;
886 else
888 CHECK_BUFFER (buffer);
889 base = XBUFFER (buffer)->base_buffer;
892 if (! base)
893 return Qnil;
894 XSETBUFFER (base_buffer, base);
895 return base_buffer;
898 DEFUN ("buffer-local-value", Fbuffer_local_value,
899 Sbuffer_local_value, 2, 2, 0,
900 doc: /* Return the value of VARIABLE in BUFFER.
901 If VARIABLE does not have a buffer-local binding in BUFFER, the value
902 is the default binding of the variable. */)
903 (register Lisp_Object variable, register Lisp_Object buffer)
905 register struct buffer *buf;
906 register Lisp_Object result;
907 struct Lisp_Symbol *sym;
909 CHECK_SYMBOL (variable);
910 CHECK_BUFFER (buffer);
911 buf = XBUFFER (buffer);
912 sym = XSYMBOL (variable);
914 start:
915 switch (sym->redirect)
917 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
918 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
919 case SYMBOL_LOCALIZED:
920 { /* Look in local_var_alist. */
921 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
922 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
923 result = Fassoc (variable, buf->local_var_alist);
924 if (!NILP (result))
926 if (blv->fwd)
927 { /* What binding is loaded right now? */
928 Lisp_Object current_alist_element = blv->valcell;
930 /* The value of the currently loaded binding is not
931 stored in it, but rather in the realvalue slot.
932 Store that value into the binding it belongs to
933 in case that is the one we are about to use. */
935 XSETCDR (current_alist_element,
936 do_symval_forwarding (blv->fwd));
938 /* Now get the (perhaps updated) value out of the binding. */
939 result = XCDR (result);
941 else
942 result = Fdefault_value (variable);
943 break;
945 case SYMBOL_FORWARDED:
947 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
948 if (BUFFER_OBJFWDP (fwd))
949 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
950 else
951 result = Fdefault_value (variable);
952 break;
954 default: abort ();
957 if (!EQ (result, Qunbound))
958 return result;
960 xsignal1 (Qvoid_variable, variable);
963 /* Return an alist of the Lisp-level buffer-local bindings of
964 buffer BUF. That is, don't include the variables maintained
965 in special slots in the buffer object. */
967 static Lisp_Object
968 buffer_lisp_local_variables (struct buffer *buf)
970 Lisp_Object result = Qnil;
971 register Lisp_Object tail;
972 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
974 Lisp_Object val, elt;
976 elt = XCAR (tail);
978 /* Reference each variable in the alist in buf.
979 If inquiring about the current buffer, this gets the current values,
980 so store them into the alist so the alist is up to date.
981 If inquiring about some other buffer, this swaps out any values
982 for that buffer, making the alist up to date automatically. */
983 val = find_symbol_value (XCAR (elt));
984 /* Use the current buffer value only if buf is the current buffer. */
985 if (buf != current_buffer)
986 val = XCDR (elt);
988 result = Fcons (Fcons (XCAR (elt), val), result);
991 return result;
994 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
995 Sbuffer_local_variables, 0, 1, 0,
996 doc: /* Return an alist of variables that are buffer-local in BUFFER.
997 Most elements look like (SYMBOL . VALUE), describing one variable.
998 For a symbol that is locally unbound, just the symbol appears in the value.
999 Note that storing new VALUEs in these elements doesn't change the variables.
1000 No argument or nil as argument means use current buffer as BUFFER. */)
1001 (register Lisp_Object buffer)
1003 register struct buffer *buf;
1004 register Lisp_Object result;
1006 if (NILP (buffer))
1007 buf = current_buffer;
1008 else
1010 CHECK_BUFFER (buffer);
1011 buf = XBUFFER (buffer);
1014 result = buffer_lisp_local_variables (buf);
1016 /* Add on all the variables stored in special slots. */
1018 int offset, idx;
1020 /* buffer-local Lisp variables start at `undo_list',
1021 tho only the ones from `name' on are GC'd normally. */
1022 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1023 offset < sizeof (struct buffer);
1024 /* sizeof EMACS_INT == sizeof Lisp_Object */
1025 offset += (sizeof (EMACS_INT)))
1027 idx = PER_BUFFER_IDX (offset);
1028 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1029 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1030 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1031 PER_BUFFER_VALUE (buf, offset)),
1032 result);
1036 return result;
1039 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1040 0, 1, 0,
1041 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1042 No argument or nil as argument means use current buffer as BUFFER. */)
1043 (register Lisp_Object buffer)
1045 register struct buffer *buf;
1046 if (NILP (buffer))
1047 buf = current_buffer;
1048 else
1050 CHECK_BUFFER (buffer);
1051 buf = XBUFFER (buffer);
1054 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1057 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1058 1, 1, 0,
1059 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1060 A non-nil FLAG means mark the buffer modified. */)
1061 (register Lisp_Object flag)
1063 register int already;
1064 register Lisp_Object fn;
1065 Lisp_Object buffer, window;
1067 #ifdef CLASH_DETECTION
1068 /* If buffer becoming modified, lock the file.
1069 If buffer becoming unmodified, unlock the file. */
1071 fn = current_buffer->file_truename;
1072 /* Test buffer-file-name so that binding it to nil is effective. */
1073 if (!NILP (fn) && ! NILP (current_buffer->filename))
1075 already = SAVE_MODIFF < MODIFF;
1076 if (!already && !NILP (flag))
1077 lock_file (fn);
1078 else if (already && NILP (flag))
1079 unlock_file (fn);
1081 #endif /* CLASH_DETECTION */
1083 /* Here we have a problem. SAVE_MODIFF is used here to encode
1084 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1085 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1086 modify SAVE_MODIFF to affect one, we may affect the other
1087 as well.
1088 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1089 if SAVE_MODIFF<auto_save_modified that means we risk changing
1090 recent-auto-save-p from t to nil.
1091 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1092 we risk changing recent-auto-save-p from nil to t. */
1093 SAVE_MODIFF = (NILP (flag)
1094 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1095 ? MODIFF
1096 /* Let's try to preserve recent-auto-save-p. */
1097 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1098 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1099 we can either decrease SAVE_MODIFF and auto_save_modified
1100 or increase MODIFF. */
1101 : MODIFF++);
1103 /* Set update_mode_lines only if buffer is displayed in some window.
1104 Packages like jit-lock or lazy-lock preserve a buffer's modified
1105 state by recording/restoring the state around blocks of code.
1106 Setting update_mode_lines makes redisplay consider all windows
1107 (on all frames). Stealth fontification of buffers not displayed
1108 would incur additional redisplay costs if we'd set
1109 update_modes_lines unconditionally.
1111 Ideally, I think there should be another mechanism for fontifying
1112 buffers without "modifying" buffers, or redisplay should be
1113 smarter about updating the `*' in mode lines. --gerd */
1114 XSETBUFFER (buffer, current_buffer);
1115 window = Fget_buffer_window (buffer, Qt);
1116 if (WINDOWP (window))
1118 ++update_mode_lines;
1119 current_buffer->prevent_redisplay_optimizations_p = 1;
1122 return flag;
1125 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1126 Srestore_buffer_modified_p, 1, 1, 0,
1127 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1128 It is not ensured that mode lines will be updated to show the modified
1129 state of the current buffer. Use with care. */)
1130 (Lisp_Object flag)
1132 #ifdef CLASH_DETECTION
1133 Lisp_Object fn;
1135 /* If buffer becoming modified, lock the file.
1136 If buffer becoming unmodified, unlock the file. */
1138 fn = current_buffer->file_truename;
1139 /* Test buffer-file-name so that binding it to nil is effective. */
1140 if (!NILP (fn) && ! NILP (current_buffer->filename))
1142 int already = SAVE_MODIFF < MODIFF;
1143 if (!already && !NILP (flag))
1144 lock_file (fn);
1145 else if (already && NILP (flag))
1146 unlock_file (fn);
1148 #endif /* CLASH_DETECTION */
1150 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1151 return flag;
1154 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1155 0, 1, 0,
1156 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1157 Each buffer has a tick counter which is incremented each time the
1158 text in that buffer is changed. It wraps around occasionally.
1159 No argument or nil as argument means use current buffer as BUFFER. */)
1160 (register Lisp_Object buffer)
1162 register struct buffer *buf;
1163 if (NILP (buffer))
1164 buf = current_buffer;
1165 else
1167 CHECK_BUFFER (buffer);
1168 buf = XBUFFER (buffer);
1171 return make_number (BUF_MODIFF (buf));
1174 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1175 Sbuffer_chars_modified_tick, 0, 1, 0,
1176 doc: /* Return BUFFER's character-change tick counter.
1177 Each buffer has a character-change tick counter, which is set to the
1178 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1179 time text in that buffer is inserted or deleted. By comparing the
1180 values returned by two individual calls of `buffer-chars-modified-tick',
1181 you can tell whether a character change occurred in that buffer in
1182 between these calls. No argument or nil as argument means use current
1183 buffer as BUFFER. */)
1184 (register Lisp_Object buffer)
1186 register struct buffer *buf;
1187 if (NILP (buffer))
1188 buf = current_buffer;
1189 else
1191 CHECK_BUFFER (buffer);
1192 buf = XBUFFER (buffer);
1195 return make_number (BUF_CHARS_MODIFF (buf));
1198 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1199 "(list (read-string \"Rename buffer (to new name): \" \
1200 nil 'buffer-name-history (buffer-name (current-buffer))) \
1201 current-prefix-arg)",
1202 doc: /* Change current buffer's name to NEWNAME (a string).
1203 If second arg UNIQUE is nil or omitted, it is an error if a
1204 buffer named NEWNAME already exists.
1205 If UNIQUE is non-nil, come up with a new name using
1206 `generate-new-buffer-name'.
1207 Interactively, you can set UNIQUE with a prefix argument.
1208 We return the name we actually gave the buffer.
1209 This does not change the name of the visited file (if any). */)
1210 (register Lisp_Object newname, Lisp_Object unique)
1212 register Lisp_Object tem, buf;
1214 CHECK_STRING (newname);
1216 if (SCHARS (newname) == 0)
1217 error ("Empty string is invalid as a buffer name");
1219 tem = Fget_buffer (newname);
1220 if (!NILP (tem))
1222 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1223 rename the buffer automatically so you can create another
1224 with the original name. It makes UNIQUE equivalent to
1225 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1226 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1227 return current_buffer->name;
1228 if (!NILP (unique))
1229 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1230 else
1231 error ("Buffer name `%s' is in use", SDATA (newname));
1234 current_buffer->name = newname;
1236 /* Catch redisplay's attention. Unless we do this, the mode lines for
1237 any windows displaying current_buffer will stay unchanged. */
1238 update_mode_lines++;
1240 XSETBUFFER (buf, current_buffer);
1241 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1242 if (NILP (current_buffer->filename)
1243 && !NILP (current_buffer->auto_save_file_name))
1244 call0 (intern ("rename-auto-save-file"));
1245 /* Refetch since that last call may have done GC. */
1246 return current_buffer->name;
1249 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1250 doc: /* Return most recently selected buffer other than BUFFER.
1251 Buffers not visible in windows are preferred to visible buffers,
1252 unless optional second argument VISIBLE-OK is non-nil.
1253 If the optional third argument FRAME is non-nil, use that frame's
1254 buffer list instead of the selected frame's buffer list.
1255 If no other buffer exists, the buffer `*scratch*' is returned.
1256 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1257 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1259 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1260 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1261 notsogood = Qnil;
1263 if (NILP (frame))
1264 frame = selected_frame;
1266 CHECK_FRAME (frame);
1268 tail = Vbuffer_alist;
1269 pred = frame_buffer_predicate (frame);
1271 /* Consider buffers that have been seen in the selected frame
1272 before other buffers. */
1274 tem = frame_buffer_list (frame);
1275 add_ons = Qnil;
1276 while (CONSP (tem))
1278 if (BUFFERP (XCAR (tem)))
1279 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1280 tem = XCDR (tem);
1282 tail = nconc2 (Fnreverse (add_ons), tail);
1284 for (; CONSP (tail); tail = XCDR (tail))
1286 buf = Fcdr (XCAR (tail));
1287 if (EQ (buf, buffer))
1288 continue;
1289 if (NILP (buf))
1290 continue;
1291 if (NILP (XBUFFER (buf)->name))
1292 continue;
1293 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1294 continue;
1295 /* If the selected frame has a buffer_predicate,
1296 disregard buffers that don't fit the predicate. */
1297 if (!NILP (pred))
1299 tem = call1 (pred, buf);
1300 if (NILP (tem))
1301 continue;
1304 if (NILP (visible_ok))
1305 tem = Fget_buffer_window (buf, Qvisible);
1306 else
1307 tem = Qnil;
1308 if (NILP (tem))
1309 return buf;
1310 if (NILP (notsogood))
1311 notsogood = buf;
1313 if (!NILP (notsogood))
1314 return notsogood;
1315 buf = Fget_buffer (build_string ("*scratch*"));
1316 if (NILP (buf))
1318 buf = Fget_buffer_create (build_string ("*scratch*"));
1319 Fset_buffer_major_mode (buf);
1321 return buf;
1324 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1325 0, 1, "",
1326 doc: /* Start keeping undo information for buffer BUFFER.
1327 No argument or nil as argument means do this for the current buffer. */)
1328 (register Lisp_Object buffer)
1330 Lisp_Object real_buffer;
1332 if (NILP (buffer))
1333 XSETBUFFER (real_buffer, current_buffer);
1334 else
1336 real_buffer = Fget_buffer (buffer);
1337 if (NILP (real_buffer))
1338 nsberror (buffer);
1341 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1342 XBUFFER (real_buffer)->undo_list = Qnil;
1344 return Qnil;
1348 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1349 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1350 The buffer being killed will be current while the hook is running.\n\
1351 See `kill-buffer'."
1353 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1354 doc: /* Kill buffer BUFFER-OR-NAME.
1355 The argument may be a buffer or the name of an existing buffer.
1356 Argument nil or omitted means kill the current buffer. Return t if the
1357 buffer is actually killed, nil otherwise.
1359 This function calls `replace-buffer-in-windows' for cleaning up all
1360 windows currently displaying the buffer to be killed. The functions in
1361 `kill-buffer-query-functions' are called with the buffer to be killed as
1362 the current buffer. If any of them returns nil, the buffer is not
1363 killed. The hook `kill-buffer-hook' is run before the buffer is
1364 actually killed. The buffer being killed will be current while the hook
1365 is running.
1367 Any processes that have this buffer as the `process-buffer' are killed
1368 with SIGHUP. */)
1369 (Lisp_Object buffer_or_name)
1371 Lisp_Object buffer;
1372 register struct buffer *b;
1373 register Lisp_Object tem;
1374 register struct Lisp_Marker *m;
1375 struct gcpro gcpro1;
1377 if (NILP (buffer_or_name))
1378 buffer = Fcurrent_buffer ();
1379 else
1380 buffer = Fget_buffer (buffer_or_name);
1381 if (NILP (buffer))
1382 nsberror (buffer_or_name);
1384 b = XBUFFER (buffer);
1386 /* Avoid trouble for buffer already dead. */
1387 if (NILP (b->name))
1388 return Qnil;
1390 /* Query if the buffer is still modified. */
1391 if (INTERACTIVE && !NILP (b->filename)
1392 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1394 GCPRO1 (buffer);
1395 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1396 b->name, make_number (0)));
1397 UNGCPRO;
1398 if (NILP (tem))
1399 return Qnil;
1402 /* Run hooks with the buffer to be killed the current buffer. */
1404 int count = SPECPDL_INDEX ();
1405 Lisp_Object arglist[1];
1407 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1408 set_buffer_internal (b);
1410 /* First run the query functions; if any query is answered no,
1411 don't kill the buffer. */
1412 arglist[0] = Qkill_buffer_query_functions;
1413 tem = Frun_hook_with_args_until_failure (1, arglist);
1414 if (NILP (tem))
1415 return unbind_to (count, Qnil);
1417 /* Then run the hooks. */
1418 Frun_hooks (1, &Qkill_buffer_hook);
1419 unbind_to (count, Qnil);
1422 /* We have no more questions to ask. Verify that it is valid
1423 to kill the buffer. This must be done after the questions
1424 since anything can happen within do_yes_or_no_p. */
1426 /* Don't kill the minibuffer now current. */
1427 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1428 return Qnil;
1430 if (NILP (b->name))
1431 return Qnil;
1433 /* When we kill a base buffer, kill all its indirect buffers.
1434 We do it at this stage so nothing terrible happens if they
1435 ask questions or their hooks get errors. */
1436 if (! b->base_buffer)
1438 struct buffer *other;
1440 GCPRO1 (buffer);
1442 for (other = all_buffers; other; other = other->next)
1443 /* all_buffers contains dead buffers too;
1444 don't re-kill them. */
1445 if (other->base_buffer == b && !NILP (other->name))
1447 Lisp_Object buffer;
1448 XSETBUFFER (buffer, other);
1449 Fkill_buffer (buffer);
1452 UNGCPRO;
1455 /* Make this buffer not be current.
1456 In the process, notice if this is the sole visible buffer
1457 and give up if so. */
1458 if (b == current_buffer)
1460 tem = Fother_buffer (buffer, Qnil, Qnil);
1461 Fset_buffer (tem);
1462 if (b == current_buffer)
1463 return Qnil;
1466 /* Notice if the buffer to kill is the sole visible buffer
1467 when we're currently in the mini-buffer, and give up if so. */
1468 XSETBUFFER (tem, current_buffer);
1469 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1471 tem = Fother_buffer (buffer, Qnil, Qnil);
1472 if (EQ (buffer, tem))
1473 return Qnil;
1476 /* Now there is no question: we can kill the buffer. */
1478 #ifdef CLASH_DETECTION
1479 /* Unlock this buffer's file, if it is locked. */
1480 unlock_buffer (b);
1481 #endif /* CLASH_DETECTION */
1483 GCPRO1 (buffer);
1484 kill_buffer_processes (buffer);
1485 UNGCPRO;
1487 /* Killing buffer processes may run sentinels which may
1488 have called kill-buffer. */
1490 if (NILP (b->name))
1491 return Qnil;
1493 clear_charpos_cache (b);
1495 tem = Vinhibit_quit;
1496 Vinhibit_quit = Qt;
1497 replace_buffer_in_all_windows (buffer);
1498 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1499 frames_discard_buffer (buffer);
1500 Vinhibit_quit = tem;
1502 /* Delete any auto-save file, if we saved it in this session.
1503 But not if the buffer is modified. */
1504 if (STRINGP (b->auto_save_file_name)
1505 && BUF_AUTOSAVE_MODIFF (b) != 0
1506 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1507 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1508 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1510 Lisp_Object tem;
1511 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1512 if (! NILP (tem))
1513 internal_delete_file (b->auto_save_file_name);
1516 if (b->base_buffer)
1518 /* Unchain all markers that belong to this indirect buffer.
1519 Don't unchain the markers that belong to the base buffer
1520 or its other indirect buffers. */
1521 for (m = BUF_MARKERS (b); m; )
1523 struct Lisp_Marker *next = m->next;
1524 if (m->buffer == b)
1525 unchain_marker (m);
1526 m = next;
1529 else
1531 /* Unchain all markers of this buffer and its indirect buffers.
1532 and leave them pointing nowhere. */
1533 for (m = BUF_MARKERS (b); m; )
1535 struct Lisp_Marker *next = m->next;
1536 m->buffer = 0;
1537 m->next = NULL;
1538 m = next;
1540 BUF_MARKERS (b) = NULL;
1541 BUF_INTERVALS (b) = NULL_INTERVAL;
1543 /* Perhaps we should explicitly free the interval tree here... */
1546 /* Reset the local variables, so that this buffer's local values
1547 won't be protected from GC. They would be protected
1548 if they happened to remain encached in their symbols.
1549 This gets rid of them for certain. */
1550 swap_out_buffer_local_variables (b);
1551 reset_buffer_local_variables (b, 1);
1553 b->name = Qnil;
1555 BLOCK_INPUT;
1556 if (! b->base_buffer)
1557 free_buffer_text (b);
1559 if (b->newline_cache)
1561 free_region_cache (b->newline_cache);
1562 b->newline_cache = 0;
1564 if (b->width_run_cache)
1566 free_region_cache (b->width_run_cache);
1567 b->width_run_cache = 0;
1569 b->width_table = Qnil;
1570 UNBLOCK_INPUT;
1571 b->undo_list = Qnil;
1573 return Qt;
1576 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1577 we do this each time BUF is selected visibly, the more recently
1578 selected buffers are always closer to the front of the list. This
1579 means that other_buffer is more likely to choose a relevant buffer. */
1581 void
1582 record_buffer (Lisp_Object buf)
1584 register Lisp_Object link, prev;
1585 Lisp_Object frame;
1586 frame = selected_frame;
1588 prev = Qnil;
1589 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1591 if (EQ (XCDR (XCAR (link)), buf))
1592 break;
1593 prev = link;
1596 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1597 we cannot use Fdelq itself here because it allows quitting. */
1599 if (NILP (prev))
1600 Vbuffer_alist = XCDR (Vbuffer_alist);
1601 else
1602 XSETCDR (prev, XCDR (XCDR (prev)));
1604 XSETCDR (link, Vbuffer_alist);
1605 Vbuffer_alist = link;
1607 /* Effectively do a delq on buried_buffer_list. */
1609 prev = Qnil;
1610 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1611 link = XCDR (link))
1613 if (EQ (XCAR (link), buf))
1615 if (NILP (prev))
1616 XFRAME (frame)->buried_buffer_list = XCDR (link);
1617 else
1618 XSETCDR (prev, XCDR (XCDR (prev)));
1619 break;
1621 prev = link;
1624 /* Now move this buffer to the front of frame_buffer_list also. */
1626 prev = Qnil;
1627 for (link = frame_buffer_list (frame); CONSP (link);
1628 link = XCDR (link))
1630 if (EQ (XCAR (link), buf))
1631 break;
1632 prev = link;
1635 /* Effectively do delq. */
1637 if (CONSP (link))
1639 if (NILP (prev))
1640 set_frame_buffer_list (frame,
1641 XCDR (frame_buffer_list (frame)));
1642 else
1643 XSETCDR (prev, XCDR (XCDR (prev)));
1645 XSETCDR (link, frame_buffer_list (frame));
1646 set_frame_buffer_list (frame, link);
1648 else
1649 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1652 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1653 doc: /* Set an appropriate major mode for BUFFER.
1654 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1655 according to `default-major-mode'.
1656 Use this function before selecting the buffer, since it may need to inspect
1657 the current buffer's major mode. */)
1658 (Lisp_Object buffer)
1660 int count;
1661 Lisp_Object function;
1663 CHECK_BUFFER (buffer);
1665 if (STRINGP (XBUFFER (buffer)->name)
1666 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1667 function = find_symbol_value (intern ("initial-major-mode"));
1668 else
1670 function = buffer_defaults.major_mode;
1671 if (NILP (function)
1672 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1673 function = current_buffer->major_mode;
1676 if (NILP (function) || EQ (function, Qfundamental_mode))
1677 return Qnil;
1679 count = SPECPDL_INDEX ();
1681 /* To select a nonfundamental mode,
1682 select the buffer temporarily and then call the mode function. */
1684 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1686 Fset_buffer (buffer);
1687 call0 (function);
1689 return unbind_to (count, Qnil);
1692 /* Switch to buffer BUFFER in the selected window.
1693 If NORECORD is non-nil, don't call record_buffer. */
1695 Lisp_Object
1696 switch_to_buffer_1 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1698 register Lisp_Object buffer;
1700 if (NILP (buffer_or_name))
1701 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1702 else
1704 buffer = Fget_buffer (buffer_or_name);
1705 if (NILP (buffer))
1707 buffer = Fget_buffer_create (buffer_or_name);
1708 Fset_buffer_major_mode (buffer);
1711 Fset_buffer (buffer);
1712 if (NILP (norecord))
1713 record_buffer (buffer);
1715 Fset_window_buffer (EQ (selected_window, minibuf_window)
1716 ? Fnext_window (minibuf_window, Qnil, Qnil)
1717 : selected_window,
1718 buffer, Qnil);
1720 return buffer;
1723 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1724 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1725 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1726 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1727 nil. Return the buffer switched to.
1729 If BUFFER-OR-NAME is a string and does not identify an existing
1730 buffer, create a new buffer with that name. Interactively, if
1731 `confirm-nonexistent-file-or-buffer' is non-nil, request
1732 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1733 nil, switch to buffer returned by `other-buffer'.
1735 Optional second arg NORECORD non-nil means do not put this buffer
1736 at the front of the list of recently selected ones. This
1737 function returns the buffer it switched to as a Lisp object.
1739 If the selected window is the minibuffer window or dedicated to
1740 its buffer, use `pop-to-buffer' for displaying the buffer.
1742 WARNING: This is NOT the way to work on another buffer temporarily
1743 within a Lisp program! Use `set-buffer' instead. That avoids
1744 messing with the window-buffer correspondences. */)
1745 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1747 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1749 /* Basically a NOP. Avoid signalling an error in the case where
1750 the selected window is dedicated, or a minibuffer. */
1752 /* But do put this buffer at the front of the buffer list, unless
1753 that has been inhibited. Note that even if BUFFER-OR-NAME is
1754 at the front of the main buffer-list already, we still want to
1755 move it to the front of the frame's buffer list. */
1756 if (NILP (norecord))
1757 record_buffer (buffer_or_name);
1758 return Fset_buffer (buffer_or_name);
1760 else if (EQ (minibuf_window, selected_window)
1761 /* If `dedicated' is neither nil nor t, it means it's
1762 dedicatedness can be overridden by an explicit request
1763 such as a call to switch-to-buffer. */
1764 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1765 /* We can't use the selected window so let `pop-to-buffer' try some
1766 other window. */
1767 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1768 else
1769 return switch_to_buffer_1 (buffer_or_name, norecord);
1772 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1773 doc: /* Return the current buffer as a Lisp object. */)
1774 (void)
1776 register Lisp_Object buf;
1777 XSETBUFFER (buf, current_buffer);
1778 return buf;
1781 /* Set the current buffer to B.
1783 We previously set windows_or_buffers_changed here to invalidate
1784 global unchanged information in beg_unchanged and end_unchanged.
1785 This is no longer necessary because we now compute unchanged
1786 information on a buffer-basis. Every action affecting other
1787 windows than the selected one requires a select_window at some
1788 time, and that increments windows_or_buffers_changed. */
1790 void
1791 set_buffer_internal (register struct buffer *b)
1793 if (current_buffer != b)
1794 set_buffer_internal_1 (b);
1797 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1798 This is used by redisplay. */
1800 void
1801 set_buffer_internal_1 (register struct buffer *b)
1803 register struct buffer *old_buf;
1804 register Lisp_Object tail;
1806 #ifdef USE_MMAP_FOR_BUFFERS
1807 if (b->text->beg == NULL)
1808 enlarge_buffer_text (b, 0);
1809 #endif /* USE_MMAP_FOR_BUFFERS */
1811 if (current_buffer == b)
1812 return;
1814 old_buf = current_buffer;
1815 current_buffer = b;
1816 last_known_column_point = -1; /* invalidate indentation cache */
1818 if (old_buf)
1820 /* Put the undo list back in the base buffer, so that it appears
1821 that an indirect buffer shares the undo list of its base. */
1822 if (old_buf->base_buffer)
1823 old_buf->base_buffer->undo_list = old_buf->undo_list;
1825 /* If the old current buffer has markers to record PT, BEGV and ZV
1826 when it is not current, update them now. */
1827 if (! NILP (old_buf->pt_marker))
1829 Lisp_Object obuf;
1830 XSETBUFFER (obuf, old_buf);
1831 set_marker_both (old_buf->pt_marker, obuf,
1832 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1834 if (! NILP (old_buf->begv_marker))
1836 Lisp_Object obuf;
1837 XSETBUFFER (obuf, old_buf);
1838 set_marker_both (old_buf->begv_marker, obuf,
1839 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1841 if (! NILP (old_buf->zv_marker))
1843 Lisp_Object obuf;
1844 XSETBUFFER (obuf, old_buf);
1845 set_marker_both (old_buf->zv_marker, obuf,
1846 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1850 /* Get the undo list from the base buffer, so that it appears
1851 that an indirect buffer shares the undo list of its base. */
1852 if (b->base_buffer)
1853 b->undo_list = b->base_buffer->undo_list;
1855 /* If the new current buffer has markers to record PT, BEGV and ZV
1856 when it is not current, fetch them now. */
1857 if (! NILP (b->pt_marker))
1859 BUF_PT (b) = marker_position (b->pt_marker);
1860 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1862 if (! NILP (b->begv_marker))
1864 BUF_BEGV (b) = marker_position (b->begv_marker);
1865 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1867 if (! NILP (b->zv_marker))
1869 BUF_ZV (b) = marker_position (b->zv_marker);
1870 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1873 /* Look down buffer's list of local Lisp variables
1874 to find and update any that forward into C variables. */
1878 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1880 Lisp_Object var = XCAR (XCAR (tail));
1881 struct Lisp_Symbol *sym = XSYMBOL (var);
1882 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1883 && SYMBOL_BLV (sym)->fwd)
1884 /* Just reference the variable
1885 to cause it to become set for this buffer. */
1886 Fsymbol_value (var);
1889 /* Do the same with any others that were local to the previous buffer */
1890 while (b != old_buf && (b = old_buf, b));
1893 /* Switch to buffer B temporarily for redisplay purposes.
1894 This avoids certain things that don't need to be done within redisplay. */
1896 void
1897 set_buffer_temp (struct buffer *b)
1899 register struct buffer *old_buf;
1901 if (current_buffer == b)
1902 return;
1904 old_buf = current_buffer;
1905 current_buffer = b;
1907 if (old_buf)
1909 /* If the old current buffer has markers to record PT, BEGV and ZV
1910 when it is not current, update them now. */
1911 if (! NILP (old_buf->pt_marker))
1913 Lisp_Object obuf;
1914 XSETBUFFER (obuf, old_buf);
1915 set_marker_both (old_buf->pt_marker, obuf,
1916 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1918 if (! NILP (old_buf->begv_marker))
1920 Lisp_Object obuf;
1921 XSETBUFFER (obuf, old_buf);
1922 set_marker_both (old_buf->begv_marker, obuf,
1923 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1925 if (! NILP (old_buf->zv_marker))
1927 Lisp_Object obuf;
1928 XSETBUFFER (obuf, old_buf);
1929 set_marker_both (old_buf->zv_marker, obuf,
1930 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1934 /* If the new current buffer has markers to record PT, BEGV and ZV
1935 when it is not current, fetch them now. */
1936 if (! NILP (b->pt_marker))
1938 BUF_PT (b) = marker_position (b->pt_marker);
1939 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1941 if (! NILP (b->begv_marker))
1943 BUF_BEGV (b) = marker_position (b->begv_marker);
1944 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1946 if (! NILP (b->zv_marker))
1948 BUF_ZV (b) = marker_position (b->zv_marker);
1949 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1953 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1954 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1955 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1956 also `save-excursion' when you want to make a buffer current
1957 temporarily. This function does not display the buffer, so its effect
1958 ends when the current command terminates. Use `switch-to-buffer' or
1959 `pop-to-buffer' to switch buffers permanently. */)
1960 (register Lisp_Object buffer_or_name)
1962 register Lisp_Object buffer;
1963 buffer = Fget_buffer (buffer_or_name);
1964 if (NILP (buffer))
1965 nsberror (buffer_or_name);
1966 if (NILP (XBUFFER (buffer)->name))
1967 error ("Selecting deleted buffer");
1968 set_buffer_internal (XBUFFER (buffer));
1969 return buffer;
1972 /* Set the current buffer to BUFFER provided it is alive. */
1974 Lisp_Object
1975 set_buffer_if_live (Lisp_Object buffer)
1977 if (! NILP (XBUFFER (buffer)->name))
1978 Fset_buffer (buffer);
1979 return Qnil;
1982 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1983 Sbarf_if_buffer_read_only, 0, 0, 0,
1984 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1985 (void)
1987 if (!NILP (current_buffer->read_only)
1988 && NILP (Vinhibit_read_only))
1989 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1990 return Qnil;
1993 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1994 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
1995 There it is the least likely candidate for `other-buffer' to return;
1996 thus, the least likely buffer for \\[switch-to-buffer] to select by
1997 default.
1999 The argument may be a buffer name or an actual buffer object. If
2000 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2001 from the selected window if it is displayed there. If the selected
2002 window is dedicated to its buffer, delete that window if there are other
2003 windows on the same frame. If the selected window is the only window on
2004 its frame, iconify that frame. */)
2005 (register Lisp_Object buffer_or_name)
2007 Lisp_Object buffer;
2009 /* Figure out what buffer we're going to bury. */
2010 if (NILP (buffer_or_name))
2012 Lisp_Object tem;
2013 XSETBUFFER (buffer, current_buffer);
2015 tem = Fwindow_buffer (selected_window);
2016 /* If we're burying the current buffer, unshow it. */
2017 if (EQ (buffer, tem))
2019 if (NILP (Fwindow_dedicated_p (selected_window)))
2020 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2021 else if (NILP (XWINDOW (selected_window)->parent))
2022 Ficonify_frame (Fwindow_frame (selected_window));
2023 else
2024 Fdelete_window (selected_window);
2027 else
2029 buffer = Fget_buffer (buffer_or_name);
2030 if (NILP (buffer))
2031 nsberror (buffer_or_name);
2034 /* Move buffer to the end of the buffer list. Do nothing if the
2035 buffer is killed. */
2036 if (!NILP (XBUFFER (buffer)->name))
2038 Lisp_Object aelt, link;
2040 aelt = Frassq (buffer, Vbuffer_alist);
2041 link = Fmemq (aelt, Vbuffer_alist);
2042 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2043 XSETCDR (link, Qnil);
2044 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2046 XFRAME (selected_frame)->buffer_list
2047 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2048 XFRAME (selected_frame)->buried_buffer_list
2049 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2052 return Qnil;
2055 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2056 doc: /* Delete the entire contents of the current buffer.
2057 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2058 so the buffer is truly empty after this. */)
2059 (void)
2061 Fwiden ();
2063 del_range (BEG, Z);
2065 current_buffer->last_window_start = 1;
2066 /* Prevent warnings, or suspension of auto saving, that would happen
2067 if future size is less than past size. Use of erase-buffer
2068 implies that the future text is not really related to the past text. */
2069 XSETFASTINT (current_buffer->save_length, 0);
2070 return Qnil;
2073 void
2074 validate_region (register Lisp_Object *b, register Lisp_Object *e)
2076 CHECK_NUMBER_COERCE_MARKER (*b);
2077 CHECK_NUMBER_COERCE_MARKER (*e);
2079 if (XINT (*b) > XINT (*e))
2081 Lisp_Object tem;
2082 tem = *b; *b = *e; *e = tem;
2085 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2086 && XINT (*e) <= ZV))
2087 args_out_of_range (*b, *e);
2090 /* Advance BYTE_POS up to a character boundary
2091 and return the adjusted position. */
2093 static int
2094 advance_to_char_boundary (EMACS_INT byte_pos)
2096 int c;
2098 if (byte_pos == BEG)
2099 /* Beginning of buffer is always a character boundary. */
2100 return BEG;
2102 c = FETCH_BYTE (byte_pos);
2103 if (! CHAR_HEAD_P (c))
2105 /* We should advance BYTE_POS only when C is a constituent of a
2106 multibyte sequence. */
2107 EMACS_INT orig_byte_pos = byte_pos;
2111 byte_pos--;
2112 c = FETCH_BYTE (byte_pos);
2114 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2115 INC_POS (byte_pos);
2116 if (byte_pos < orig_byte_pos)
2117 byte_pos = orig_byte_pos;
2118 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2119 surely advance to the correct character boundary. If C is
2120 not, BYTE_POS was unchanged. */
2123 return byte_pos;
2126 #ifdef REL_ALLOC
2127 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2128 #endif /* REL_ALLOC */
2130 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2131 1, 1, 0,
2132 doc: /* Swap the text between current buffer and BUFFER. */)
2133 (Lisp_Object buffer)
2135 struct buffer *other_buffer;
2136 CHECK_BUFFER (buffer);
2137 other_buffer = XBUFFER (buffer);
2139 if (NILP (other_buffer->name))
2140 error ("Cannot swap a dead buffer's text");
2142 /* Actually, it probably works just fine.
2143 * if (other_buffer == current_buffer)
2144 * error ("Cannot swap a buffer's text with itself"); */
2146 /* Actually, this may be workable as well, tho probably only if they're
2147 *both* indirect. */
2148 if (other_buffer->base_buffer
2149 || current_buffer->base_buffer)
2150 error ("Cannot swap indirect buffers's text");
2152 { /* This is probably harder to make work. */
2153 struct buffer *other;
2154 for (other = all_buffers; other; other = other->next)
2155 if (other->base_buffer == other_buffer
2156 || other->base_buffer == current_buffer)
2157 error ("One of the buffers to swap has indirect buffers");
2160 #define swapfield(field, type) \
2161 do { \
2162 type tmp##field = other_buffer->field; \
2163 other_buffer->field = current_buffer->field; \
2164 current_buffer->field = tmp##field; \
2165 } while (0)
2167 swapfield (own_text, struct buffer_text);
2168 eassert (current_buffer->text == &current_buffer->own_text);
2169 eassert (other_buffer->text == &other_buffer->own_text);
2170 #ifdef REL_ALLOC
2171 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2172 (POINTER_TYPE **) &other_buffer->own_text.beg);
2173 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2174 (POINTER_TYPE **) &current_buffer->own_text.beg);
2175 #endif /* REL_ALLOC */
2177 swapfield (pt, EMACS_INT);
2178 swapfield (pt_byte, EMACS_INT);
2179 swapfield (begv, EMACS_INT);
2180 swapfield (begv_byte, EMACS_INT);
2181 swapfield (zv, EMACS_INT);
2182 swapfield (zv_byte, EMACS_INT);
2183 eassert (!current_buffer->base_buffer);
2184 eassert (!other_buffer->base_buffer);
2185 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2186 swapfield (newline_cache, struct region_cache *);
2187 swapfield (width_run_cache, struct region_cache *);
2188 current_buffer->prevent_redisplay_optimizations_p = 1;
2189 other_buffer->prevent_redisplay_optimizations_p = 1;
2190 swapfield (overlays_before, struct Lisp_Overlay *);
2191 swapfield (overlays_after, struct Lisp_Overlay *);
2192 swapfield (overlay_center, EMACS_INT);
2193 swapfield (undo_list, Lisp_Object);
2194 swapfield (mark, Lisp_Object);
2195 swapfield (enable_multibyte_characters, Lisp_Object);
2196 swapfield (bidi_display_reordering, Lisp_Object);
2197 swapfield (bidi_paragraph_direction, Lisp_Object);
2198 /* FIXME: Not sure what we should do with these *_marker fields.
2199 Hopefully they're just nil anyway. */
2200 swapfield (pt_marker, Lisp_Object);
2201 swapfield (begv_marker, Lisp_Object);
2202 swapfield (zv_marker, Lisp_Object);
2203 current_buffer->point_before_scroll = Qnil;
2204 other_buffer->point_before_scroll = Qnil;
2206 current_buffer->text->modiff++; other_buffer->text->modiff++;
2207 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2208 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2209 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2210 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2211 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2212 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2214 struct Lisp_Marker *m;
2215 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2216 if (m->buffer == other_buffer)
2217 m->buffer = current_buffer;
2218 else
2219 /* Since there's no indirect buffer in sight, markers on
2220 BUF_MARKERS(buf) should either be for `buf' or dead. */
2221 eassert (!m->buffer);
2222 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2223 if (m->buffer == current_buffer)
2224 m->buffer = other_buffer;
2225 else
2226 /* Since there's no indirect buffer in sight, markers on
2227 BUF_MARKERS(buf) should either be for `buf' or dead. */
2228 eassert (!m->buffer);
2230 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2231 So since we just swapped the markers between the two buffers, we need
2232 to undo the effect of this swap for window markers. */
2233 Lisp_Object w = Fselected_window (), ws = Qnil;
2234 Lisp_Object buf1, buf2;
2235 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2237 while (NILP (Fmemq (w, ws)))
2239 ws = Fcons (w, ws);
2240 if (MARKERP (XWINDOW (w)->pointm)
2241 && (EQ (XWINDOW (w)->buffer, buf1)
2242 || EQ (XWINDOW (w)->buffer, buf2)))
2243 Fset_marker (XWINDOW (w)->pointm,
2244 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2245 XWINDOW (w)->buffer);
2246 w = Fnext_window (w, Qt, Qt);
2250 if (current_buffer->text->intervals)
2251 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2252 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2253 if (other_buffer->text->intervals)
2254 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2255 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2257 return Qnil;
2260 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2261 1, 1, 0,
2262 doc: /* Set the multibyte flag of the current buffer to FLAG.
2263 If FLAG is t, this makes the buffer a multibyte buffer.
2264 If FLAG is nil, this makes the buffer a single-byte buffer.
2265 In these cases, the buffer contents remain unchanged as a sequence of
2266 bytes but the contents viewed as characters do change.
2267 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2268 all eight-bit bytes to eight-bit characters.
2269 If the multibyte flag was really changed, undo information of the
2270 current buffer is cleared. */)
2271 (Lisp_Object flag)
2273 struct Lisp_Marker *tail, *markers;
2274 struct buffer *other;
2275 EMACS_INT begv, zv;
2276 int narrowed = (BEG != BEGV || Z != ZV);
2277 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2278 Lisp_Object old_undo = current_buffer->undo_list;
2279 struct gcpro gcpro1;
2281 if (current_buffer->base_buffer)
2282 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2284 /* Do nothing if nothing actually changes. */
2285 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2286 return flag;
2288 GCPRO1 (old_undo);
2290 /* Don't record these buffer changes. We will put a special undo entry
2291 instead. */
2292 current_buffer->undo_list = Qt;
2294 /* If the cached position is for this buffer, clear it out. */
2295 clear_charpos_cache (current_buffer);
2297 if (NILP (flag))
2298 begv = BEGV_BYTE, zv = ZV_BYTE;
2299 else
2300 begv = BEGV, zv = ZV;
2302 if (narrowed)
2303 Fwiden ();
2305 if (NILP (flag))
2307 EMACS_INT pos, stop;
2308 unsigned char *p;
2310 /* Do this first, so it can use CHAR_TO_BYTE
2311 to calculate the old correspondences. */
2312 set_intervals_multibyte (0);
2314 current_buffer->enable_multibyte_characters = Qnil;
2316 Z = Z_BYTE;
2317 BEGV = BEGV_BYTE;
2318 ZV = ZV_BYTE;
2319 GPT = GPT_BYTE;
2320 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2323 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2324 tail->charpos = tail->bytepos;
2326 /* Convert multibyte form of 8-bit characters to unibyte. */
2327 pos = BEG;
2328 stop = GPT;
2329 p = BEG_ADDR;
2330 while (1)
2332 int c, bytes;
2334 if (pos == stop)
2336 if (pos == Z)
2337 break;
2338 p = GAP_END_ADDR;
2339 stop = Z;
2341 if (ASCII_BYTE_P (*p))
2342 p++, pos++;
2343 else if (CHAR_BYTE8_HEAD_P (*p))
2345 c = STRING_CHAR_AND_LENGTH (p, bytes);
2346 /* Delete all bytes for this 8-bit character but the
2347 last one, and change the last one to the character
2348 code. */
2349 bytes--;
2350 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2351 p = GAP_END_ADDR;
2352 *p++ = c;
2353 pos++;
2354 if (begv > pos)
2355 begv -= bytes;
2356 if (zv > pos)
2357 zv -= bytes;
2358 stop = Z;
2360 else
2362 bytes = BYTES_BY_CHAR_HEAD (*p);
2363 p += bytes, pos += bytes;
2366 if (narrowed)
2367 Fnarrow_to_region (make_number (begv), make_number (zv));
2369 else
2371 EMACS_INT pt = PT;
2372 EMACS_INT pos, stop;
2373 unsigned char *p, *pend;
2375 /* Be sure not to have a multibyte sequence striding over the GAP.
2376 Ex: We change this: "...abc\302 _GAP_ \241def..."
2377 to: "...abc _GAP_ \302\241def..." */
2379 if (EQ (flag, Qt)
2380 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2381 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2383 unsigned char *p = GPT_ADDR - 1;
2385 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2386 if (LEADING_CODE_P (*p))
2388 EMACS_INT new_gpt = GPT_BYTE - (GPT_ADDR - p);
2390 move_gap_both (new_gpt, new_gpt);
2394 /* Make the buffer contents valid as multibyte by converting
2395 8-bit characters to multibyte form. */
2396 pos = BEG;
2397 stop = GPT;
2398 p = BEG_ADDR;
2399 pend = GPT_ADDR;
2400 while (1)
2402 int bytes;
2404 if (pos == stop)
2406 if (pos == Z)
2407 break;
2408 p = GAP_END_ADDR;
2409 pend = Z_ADDR;
2410 stop = Z;
2413 if (ASCII_BYTE_P (*p))
2414 p++, pos++;
2415 else if (EQ (flag, Qt)
2416 && ! CHAR_BYTE8_HEAD_P (*p)
2417 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2418 p += bytes, pos += bytes;
2419 else
2421 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2422 int c;
2424 c = BYTE8_TO_CHAR (*p);
2425 bytes = CHAR_STRING (c, tmp);
2426 *p = tmp[0];
2427 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2428 bytes--;
2429 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2430 /* Now the gap is after the just inserted data. */
2431 pos = GPT;
2432 p = GAP_END_ADDR;
2433 if (pos <= begv)
2434 begv += bytes;
2435 if (pos <= zv)
2436 zv += bytes;
2437 if (pos <= pt)
2438 pt += bytes;
2439 pend = Z_ADDR;
2440 stop = Z;
2444 if (pt != PT)
2445 TEMP_SET_PT (pt);
2447 if (narrowed)
2448 Fnarrow_to_region (make_number (begv), make_number (zv));
2450 /* Do this first, so that chars_in_text asks the right question.
2451 set_intervals_multibyte needs it too. */
2452 current_buffer->enable_multibyte_characters = Qt;
2454 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2455 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2457 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2459 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2460 if (BEGV_BYTE > GPT_BYTE)
2461 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2462 else
2463 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2465 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2466 if (ZV_BYTE > GPT_BYTE)
2467 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2468 else
2469 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2472 EMACS_INT pt_byte = advance_to_char_boundary (PT_BYTE);
2473 EMACS_INT pt;
2475 if (pt_byte > GPT_BYTE)
2476 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2477 else
2478 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2479 TEMP_SET_PT_BOTH (pt, pt_byte);
2482 tail = markers = BUF_MARKERS (current_buffer);
2484 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2485 getting confused by the markers that have not yet been updated.
2486 It is also a signal that it should never create a marker. */
2487 BUF_MARKERS (current_buffer) = NULL;
2489 for (; tail; tail = tail->next)
2491 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2492 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2495 /* Make sure no markers were put on the chain
2496 while the chain value was incorrect. */
2497 if (BUF_MARKERS (current_buffer))
2498 abort ();
2500 BUF_MARKERS (current_buffer) = markers;
2502 /* Do this last, so it can calculate the new correspondences
2503 between chars and bytes. */
2504 set_intervals_multibyte (1);
2507 if (!EQ (old_undo, Qt))
2509 /* Represent all the above changes by a special undo entry. */
2510 current_buffer->undo_list = Fcons (list3 (Qapply,
2511 intern ("set-buffer-multibyte"),
2512 NILP (flag) ? Qt : Qnil),
2513 old_undo);
2516 UNGCPRO;
2518 /* Changing the multibyteness of a buffer means that all windows
2519 showing that buffer must be updated thoroughly. */
2520 current_buffer->prevent_redisplay_optimizations_p = 1;
2521 ++windows_or_buffers_changed;
2523 /* Copy this buffer's new multibyte status
2524 into all of its indirect buffers. */
2525 for (other = all_buffers; other; other = other->next)
2526 if (other->base_buffer == current_buffer && !NILP (other->name))
2528 other->enable_multibyte_characters
2529 = current_buffer->enable_multibyte_characters;
2530 other->prevent_redisplay_optimizations_p = 1;
2533 /* Restore the modifiedness of the buffer. */
2534 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2535 Fset_buffer_modified_p (Qnil);
2537 /* Update coding systems of this buffer's process (if any). */
2539 Lisp_Object process;
2541 process = Fget_buffer_process (Fcurrent_buffer ());
2542 if (PROCESSP (process))
2543 setup_process_coding_systems (process);
2546 return flag;
2549 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2550 0, 0, 0,
2551 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2552 Most local variable bindings are eliminated so that the default values
2553 become effective once more. Also, the syntax table is set from
2554 `standard-syntax-table', the local keymap is set to nil,
2555 and the abbrev table from `fundamental-mode-abbrev-table'.
2556 This function also forces redisplay of the mode line.
2558 Every function to select a new major mode starts by
2559 calling this function.
2561 As a special exception, local variables whose names have
2562 a non-nil `permanent-local' property are not eliminated by this function.
2564 The first thing this function does is run
2565 the normal hook `change-major-mode-hook'. */)
2566 (void)
2568 if (!NILP (Vrun_hooks))
2569 call1 (Vrun_hooks, Qchange_major_mode_hook);
2571 /* Make sure none of the bindings in local_var_alist
2572 remain swapped in, in their symbols. */
2574 swap_out_buffer_local_variables (current_buffer);
2576 /* Actually eliminate all local bindings of this buffer. */
2578 reset_buffer_local_variables (current_buffer, 0);
2580 /* Force mode-line redisplay. Useful here because all major mode
2581 commands call this function. */
2582 update_mode_lines++;
2584 return Qnil;
2587 /* Make sure no local variables remain set up with buffer B
2588 for their current values. */
2590 static void
2591 swap_out_buffer_local_variables (struct buffer *b)
2593 Lisp_Object oalist, alist, buffer;
2595 XSETBUFFER (buffer, b);
2596 oalist = b->local_var_alist;
2598 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2600 Lisp_Object sym = XCAR (XCAR (alist));
2601 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2602 /* Need not do anything if some other buffer's binding is
2603 now encached. */
2604 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2606 /* Symbol is set up for this buffer's old local value:
2607 swap it out! */
2608 swap_in_global_binding (XSYMBOL (sym));
2613 /* Find all the overlays in the current buffer that contain position POS.
2614 Return the number found, and store them in a vector in *VEC_PTR.
2615 Store in *LEN_PTR the size allocated for the vector.
2616 Store in *NEXT_PTR the next position after POS where an overlay starts,
2617 or ZV if there are no more overlays between POS and ZV.
2618 Store in *PREV_PTR the previous position before POS where an overlay ends,
2619 or where an overlay starts which ends at or after POS;
2620 or BEGV if there are no such overlays from BEGV to POS.
2621 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2623 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2624 when this function is called.
2626 If EXTEND is non-zero, we make the vector bigger if necessary.
2627 If EXTEND is zero, we never extend the vector,
2628 and we store only as many overlays as will fit.
2629 But we still return the total number of overlays.
2631 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2632 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2633 default (BEGV or ZV). */
2636 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2637 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2639 Lisp_Object overlay, start, end;
2640 struct Lisp_Overlay *tail;
2641 int idx = 0;
2642 int len = *len_ptr;
2643 Lisp_Object *vec = *vec_ptr;
2644 EMACS_INT next = ZV;
2645 EMACS_INT prev = BEGV;
2646 int inhibit_storing = 0;
2648 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2650 EMACS_INT startpos, endpos;
2652 XSETMISC (overlay, tail);
2654 start = OVERLAY_START (overlay);
2655 end = OVERLAY_END (overlay);
2656 endpos = OVERLAY_POSITION (end);
2657 if (endpos < pos)
2659 if (prev < endpos)
2660 prev = endpos;
2661 break;
2663 startpos = OVERLAY_POSITION (start);
2664 /* This one ends at or after POS
2665 so its start counts for PREV_PTR if it's before POS. */
2666 if (prev < startpos && startpos < pos)
2667 prev = startpos;
2668 if (endpos == pos)
2669 continue;
2670 if (startpos <= pos)
2672 if (idx == len)
2674 /* The supplied vector is full.
2675 Either make it bigger, or don't store any more in it. */
2676 if (extend)
2678 /* Make it work with an initial len == 0. */
2679 len *= 2;
2680 if (len == 0)
2681 len = 4;
2682 *len_ptr = len;
2683 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2684 *vec_ptr = vec;
2686 else
2687 inhibit_storing = 1;
2690 if (!inhibit_storing)
2691 vec[idx] = overlay;
2692 /* Keep counting overlays even if we can't return them all. */
2693 idx++;
2695 else if (startpos < next)
2696 next = startpos;
2699 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2701 EMACS_INT startpos, endpos;
2703 XSETMISC (overlay, tail);
2705 start = OVERLAY_START (overlay);
2706 end = OVERLAY_END (overlay);
2707 startpos = OVERLAY_POSITION (start);
2708 if (pos < startpos)
2710 if (startpos < next)
2711 next = startpos;
2712 break;
2714 endpos = OVERLAY_POSITION (end);
2715 if (pos < endpos)
2717 if (idx == len)
2719 if (extend)
2721 /* Make it work with an initial len == 0. */
2722 len *= 2;
2723 if (len == 0)
2724 len = 4;
2725 *len_ptr = len;
2726 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2727 *vec_ptr = vec;
2729 else
2730 inhibit_storing = 1;
2733 if (!inhibit_storing)
2734 vec[idx] = overlay;
2735 idx++;
2737 if (startpos < pos && startpos > prev)
2738 prev = startpos;
2740 else if (endpos < pos && endpos > prev)
2741 prev = endpos;
2742 else if (endpos == pos && startpos > prev
2743 && (!change_req || startpos < pos))
2744 prev = startpos;
2747 if (next_ptr)
2748 *next_ptr = next;
2749 if (prev_ptr)
2750 *prev_ptr = prev;
2751 return idx;
2754 /* Find all the overlays in the current buffer that overlap the range
2755 BEG-END, or are empty at BEG, or are empty at END provided END
2756 denotes the position at the end of the current buffer.
2758 Return the number found, and store them in a vector in *VEC_PTR.
2759 Store in *LEN_PTR the size allocated for the vector.
2760 Store in *NEXT_PTR the next position after POS where an overlay starts,
2761 or ZV if there are no more overlays.
2762 Store in *PREV_PTR the previous position before POS where an overlay ends,
2763 or BEGV if there are no previous overlays.
2764 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2766 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2767 when this function is called.
2769 If EXTEND is non-zero, we make the vector bigger if necessary.
2770 If EXTEND is zero, we never extend the vector,
2771 and we store only as many overlays as will fit.
2772 But we still return the total number of overlays. */
2774 static int
2775 overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
2776 Lisp_Object **vec_ptr, int *len_ptr,
2777 EMACS_INT *next_ptr, EMACS_INT *prev_ptr)
2779 Lisp_Object overlay, ostart, oend;
2780 struct Lisp_Overlay *tail;
2781 int idx = 0;
2782 int len = *len_ptr;
2783 Lisp_Object *vec = *vec_ptr;
2784 EMACS_INT next = ZV;
2785 EMACS_INT prev = BEGV;
2786 int inhibit_storing = 0;
2787 int end_is_Z = end == Z;
2789 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2791 EMACS_INT startpos, endpos;
2793 XSETMISC (overlay, tail);
2795 ostart = OVERLAY_START (overlay);
2796 oend = OVERLAY_END (overlay);
2797 endpos = OVERLAY_POSITION (oend);
2798 if (endpos < beg)
2800 if (prev < endpos)
2801 prev = endpos;
2802 break;
2804 startpos = OVERLAY_POSITION (ostart);
2805 /* Count an interval if it overlaps the range, is empty at the
2806 start of the range, or is empty at END provided END denotes the
2807 end of the buffer. */
2808 if ((beg < endpos && startpos < end)
2809 || (startpos == endpos
2810 && (beg == endpos || (end_is_Z && endpos == end))))
2812 if (idx == len)
2814 /* The supplied vector is full.
2815 Either make it bigger, or don't store any more in it. */
2816 if (extend)
2818 /* Make it work with an initial len == 0. */
2819 len *= 2;
2820 if (len == 0)
2821 len = 4;
2822 *len_ptr = len;
2823 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2824 *vec_ptr = vec;
2826 else
2827 inhibit_storing = 1;
2830 if (!inhibit_storing)
2831 vec[idx] = overlay;
2832 /* Keep counting overlays even if we can't return them all. */
2833 idx++;
2835 else if (startpos < next)
2836 next = startpos;
2839 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2841 EMACS_INT startpos, endpos;
2843 XSETMISC (overlay, tail);
2845 ostart = OVERLAY_START (overlay);
2846 oend = OVERLAY_END (overlay);
2847 startpos = OVERLAY_POSITION (ostart);
2848 if (end < startpos)
2850 if (startpos < next)
2851 next = startpos;
2852 break;
2854 endpos = OVERLAY_POSITION (oend);
2855 /* Count an interval if it overlaps the range, is empty at the
2856 start of the range, or is empty at END provided END denotes the
2857 end of the buffer. */
2858 if ((beg < endpos && startpos < end)
2859 || (startpos == endpos
2860 && (beg == endpos || (end_is_Z && endpos == end))))
2862 if (idx == len)
2864 if (extend)
2866 /* Make it work with an initial len == 0. */
2867 len *= 2;
2868 if (len == 0)
2869 len = 4;
2870 *len_ptr = len;
2871 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2872 *vec_ptr = vec;
2874 else
2875 inhibit_storing = 1;
2878 if (!inhibit_storing)
2879 vec[idx] = overlay;
2880 idx++;
2882 else if (endpos < beg && endpos > prev)
2883 prev = endpos;
2886 if (next_ptr)
2887 *next_ptr = next;
2888 if (prev_ptr)
2889 *prev_ptr = prev;
2890 return idx;
2894 /* Return non-zero if there exists an overlay with a non-nil
2895 `mouse-face' property overlapping OVERLAY. */
2898 mouse_face_overlay_overlaps (Lisp_Object overlay)
2900 EMACS_INT start = OVERLAY_POSITION (OVERLAY_START (overlay));
2901 EMACS_INT end = OVERLAY_POSITION (OVERLAY_END (overlay));
2902 int n, i, size;
2903 Lisp_Object *v, tem;
2905 size = 10;
2906 v = (Lisp_Object *) alloca (size * sizeof *v);
2907 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2908 if (n > size)
2910 v = (Lisp_Object *) alloca (n * sizeof *v);
2911 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2914 for (i = 0; i < n; ++i)
2915 if (!EQ (v[i], overlay)
2916 && (tem = Foverlay_get (overlay, Qmouse_face),
2917 !NILP (tem)))
2918 break;
2920 return i < n;
2925 /* Fast function to just test if we're at an overlay boundary. */
2927 overlay_touches_p (EMACS_INT pos)
2929 Lisp_Object overlay;
2930 struct Lisp_Overlay *tail;
2932 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2934 EMACS_INT endpos;
2936 XSETMISC (overlay ,tail);
2937 if (!OVERLAYP (overlay))
2938 abort ();
2940 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2941 if (endpos < pos)
2942 break;
2943 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2944 return 1;
2947 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2949 EMACS_INT startpos;
2951 XSETMISC (overlay, tail);
2952 if (!OVERLAYP (overlay))
2953 abort ();
2955 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2956 if (pos < startpos)
2957 break;
2958 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2959 return 1;
2961 return 0;
2964 struct sortvec
2966 Lisp_Object overlay;
2967 EMACS_INT beg, end;
2968 int priority;
2971 static int
2972 compare_overlays (const void *v1, const void *v2)
2974 const struct sortvec *s1 = (const struct sortvec *) v1;
2975 const struct sortvec *s2 = (const struct sortvec *) v2;
2976 if (s1->priority != s2->priority)
2977 return s1->priority - s2->priority;
2978 if (s1->beg != s2->beg)
2979 return s1->beg - s2->beg;
2980 if (s1->end != s2->end)
2981 return s2->end - s1->end;
2982 return 0;
2985 /* Sort an array of overlays by priority. The array is modified in place.
2986 The return value is the new size; this may be smaller than the original
2987 size if some of the overlays were invalid or were window-specific. */
2989 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2991 int i, j;
2992 struct sortvec *sortvec;
2993 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2995 /* Put the valid and relevant overlays into sortvec. */
2997 for (i = 0, j = 0; i < noverlays; i++)
2999 Lisp_Object tem;
3000 Lisp_Object overlay;
3002 overlay = overlay_vec[i];
3003 if (OVERLAY_VALID (overlay)
3004 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3005 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3007 /* If we're interested in a specific window, then ignore
3008 overlays that are limited to some other window. */
3009 if (w)
3011 Lisp_Object window;
3013 window = Foverlay_get (overlay, Qwindow);
3014 if (WINDOWP (window) && XWINDOW (window) != w)
3015 continue;
3018 /* This overlay is good and counts: put it into sortvec. */
3019 sortvec[j].overlay = overlay;
3020 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3021 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3022 tem = Foverlay_get (overlay, Qpriority);
3023 if (INTEGERP (tem))
3024 sortvec[j].priority = XINT (tem);
3025 else
3026 sortvec[j].priority = 0;
3027 j++;
3030 noverlays = j;
3032 /* Sort the overlays into the proper order: increasing priority. */
3034 if (noverlays > 1)
3035 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3037 for (i = 0; i < noverlays; i++)
3038 overlay_vec[i] = sortvec[i].overlay;
3039 return (noverlays);
3042 struct sortstr
3044 Lisp_Object string, string2;
3045 int size;
3046 int priority;
3049 struct sortstrlist
3051 struct sortstr *buf; /* An array that expands as needed; never freed. */
3052 int size; /* Allocated length of that array. */
3053 int used; /* How much of the array is currently in use. */
3054 EMACS_INT bytes; /* Total length of the strings in buf. */
3057 /* Buffers for storing information about the overlays touching a given
3058 position. These could be automatic variables in overlay_strings, but
3059 it's more efficient to hold onto the memory instead of repeatedly
3060 allocating and freeing it. */
3061 static struct sortstrlist overlay_heads, overlay_tails;
3062 static unsigned char *overlay_str_buf;
3064 /* Allocated length of overlay_str_buf. */
3065 static EMACS_INT overlay_str_len;
3067 /* A comparison function suitable for passing to qsort. */
3068 static int
3069 cmp_for_strings (const void *as1, const void *as2)
3071 struct sortstr *s1 = (struct sortstr *)as1;
3072 struct sortstr *s2 = (struct sortstr *)as2;
3073 if (s1->size != s2->size)
3074 return s2->size - s1->size;
3075 if (s1->priority != s2->priority)
3076 return s1->priority - s2->priority;
3077 return 0;
3080 static void
3081 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
3083 EMACS_INT nbytes;
3085 if (ssl->used == ssl->size)
3087 if (ssl->buf)
3088 ssl->size *= 2;
3089 else
3090 ssl->size = 5;
3091 ssl->buf = ((struct sortstr *)
3092 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3094 ssl->buf[ssl->used].string = str;
3095 ssl->buf[ssl->used].string2 = str2;
3096 ssl->buf[ssl->used].size = size;
3097 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3098 ssl->used++;
3100 if (NILP (current_buffer->enable_multibyte_characters))
3101 nbytes = SCHARS (str);
3102 else if (! STRING_MULTIBYTE (str))
3103 nbytes = count_size_as_multibyte (SDATA (str),
3104 SBYTES (str));
3105 else
3106 nbytes = SBYTES (str);
3108 ssl->bytes += nbytes;
3110 if (STRINGP (str2))
3112 if (NILP (current_buffer->enable_multibyte_characters))
3113 nbytes = SCHARS (str2);
3114 else if (! STRING_MULTIBYTE (str2))
3115 nbytes = count_size_as_multibyte (SDATA (str2),
3116 SBYTES (str2));
3117 else
3118 nbytes = SBYTES (str2);
3120 ssl->bytes += nbytes;
3124 /* Return the concatenation of the strings associated with overlays that
3125 begin or end at POS, ignoring overlays that are specific to a window
3126 other than W. The strings are concatenated in the appropriate order:
3127 shorter overlays nest inside longer ones, and higher priority inside
3128 lower. Normally all of the after-strings come first, but zero-sized
3129 overlays have their after-strings ride along with the before-strings
3130 because it would look strange to print them inside-out.
3132 Returns the string length, and stores the contents indirectly through
3133 PSTR, if that variable is non-null. The string may be overwritten by
3134 subsequent calls. */
3136 EMACS_INT
3137 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3139 Lisp_Object overlay, window, str;
3140 struct Lisp_Overlay *ov;
3141 EMACS_INT startpos, endpos;
3142 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3144 overlay_heads.used = overlay_heads.bytes = 0;
3145 overlay_tails.used = overlay_tails.bytes = 0;
3146 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3148 XSETMISC (overlay, ov);
3149 eassert (OVERLAYP (overlay));
3151 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3152 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3153 if (endpos < pos)
3154 break;
3155 if (endpos != pos && startpos != pos)
3156 continue;
3157 window = Foverlay_get (overlay, Qwindow);
3158 if (WINDOWP (window) && XWINDOW (window) != w)
3159 continue;
3160 if (startpos == pos
3161 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3162 record_overlay_string (&overlay_heads, str,
3163 (startpos == endpos
3164 ? Foverlay_get (overlay, Qafter_string)
3165 : Qnil),
3166 Foverlay_get (overlay, Qpriority),
3167 endpos - startpos);
3168 else if (endpos == pos
3169 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3170 record_overlay_string (&overlay_tails, str, Qnil,
3171 Foverlay_get (overlay, Qpriority),
3172 endpos - startpos);
3174 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3176 XSETMISC (overlay, ov);
3177 eassert (OVERLAYP (overlay));
3179 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3180 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3181 if (startpos > pos)
3182 break;
3183 if (endpos != pos && startpos != pos)
3184 continue;
3185 window = Foverlay_get (overlay, Qwindow);
3186 if (WINDOWP (window) && XWINDOW (window) != w)
3187 continue;
3188 if (startpos == pos
3189 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3190 record_overlay_string (&overlay_heads, str,
3191 (startpos == endpos
3192 ? Foverlay_get (overlay, Qafter_string)
3193 : Qnil),
3194 Foverlay_get (overlay, Qpriority),
3195 endpos - startpos);
3196 else if (endpos == pos
3197 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3198 record_overlay_string (&overlay_tails, str, Qnil,
3199 Foverlay_get (overlay, Qpriority),
3200 endpos - startpos);
3202 if (overlay_tails.used > 1)
3203 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3204 cmp_for_strings);
3205 if (overlay_heads.used > 1)
3206 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3207 cmp_for_strings);
3208 if (overlay_heads.bytes || overlay_tails.bytes)
3210 Lisp_Object tem;
3211 EMACS_INT i;
3212 unsigned char *p;
3213 EMACS_INT total = overlay_heads.bytes + overlay_tails.bytes;
3215 if (total > overlay_str_len)
3217 overlay_str_len = total;
3218 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3219 total);
3221 p = overlay_str_buf;
3222 for (i = overlay_tails.used; --i >= 0;)
3224 EMACS_INT nbytes;
3225 tem = overlay_tails.buf[i].string;
3226 nbytes = copy_text (SDATA (tem), p,
3227 SBYTES (tem),
3228 STRING_MULTIBYTE (tem), multibyte);
3229 p += nbytes;
3231 for (i = 0; i < overlay_heads.used; ++i)
3233 EMACS_INT nbytes;
3234 tem = overlay_heads.buf[i].string;
3235 nbytes = copy_text (SDATA (tem), p,
3236 SBYTES (tem),
3237 STRING_MULTIBYTE (tem), multibyte);
3238 p += nbytes;
3239 tem = overlay_heads.buf[i].string2;
3240 if (STRINGP (tem))
3242 nbytes = copy_text (SDATA (tem), p,
3243 SBYTES (tem),
3244 STRING_MULTIBYTE (tem), multibyte);
3245 p += nbytes;
3248 if (p != overlay_str_buf + total)
3249 abort ();
3250 if (pstr)
3251 *pstr = overlay_str_buf;
3252 return total;
3254 return 0;
3257 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3259 void
3260 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3262 Lisp_Object overlay, beg, end;
3263 struct Lisp_Overlay *prev, *tail, *next;
3265 /* See if anything in overlays_before should move to overlays_after. */
3267 /* We don't strictly need prev in this loop; it should always be nil.
3268 But we use it for symmetry and in case that should cease to be true
3269 with some future change. */
3270 prev = NULL;
3271 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3273 next = tail->next;
3274 XSETMISC (overlay, tail);
3276 /* If the overlay is not valid, get rid of it. */
3277 if (!OVERLAY_VALID (overlay))
3278 #if 1
3279 abort ();
3280 #else
3282 /* Splice the cons cell TAIL out of overlays_before. */
3283 if (!NILP (prev))
3284 XCDR (prev) = next;
3285 else
3286 buf->overlays_before = next;
3287 tail = prev;
3288 continue;
3290 #endif
3292 beg = OVERLAY_START (overlay);
3293 end = OVERLAY_END (overlay);
3295 if (OVERLAY_POSITION (end) > pos)
3297 /* OVERLAY needs to be moved. */
3298 EMACS_INT where = OVERLAY_POSITION (beg);
3299 struct Lisp_Overlay *other, *other_prev;
3301 /* Splice the cons cell TAIL out of overlays_before. */
3302 if (prev)
3303 prev->next = next;
3304 else
3305 buf->overlays_before = next;
3307 /* Search thru overlays_after for where to put it. */
3308 other_prev = NULL;
3309 for (other = buf->overlays_after; other;
3310 other_prev = other, other = other->next)
3312 Lisp_Object otherbeg, otheroverlay;
3314 XSETMISC (otheroverlay, other);
3315 eassert (OVERLAY_VALID (otheroverlay));
3317 otherbeg = OVERLAY_START (otheroverlay);
3318 if (OVERLAY_POSITION (otherbeg) >= where)
3319 break;
3322 /* Add TAIL to overlays_after before OTHER. */
3323 tail->next = other;
3324 if (other_prev)
3325 other_prev->next = tail;
3326 else
3327 buf->overlays_after = tail;
3328 tail = prev;
3330 else
3331 /* We've reached the things that should stay in overlays_before.
3332 All the rest of overlays_before must end even earlier,
3333 so stop now. */
3334 break;
3337 /* See if anything in overlays_after should be in overlays_before. */
3338 prev = NULL;
3339 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3341 next = tail->next;
3342 XSETMISC (overlay, tail);
3344 /* If the overlay is not valid, get rid of it. */
3345 if (!OVERLAY_VALID (overlay))
3346 #if 1
3347 abort ();
3348 #else
3350 /* Splice the cons cell TAIL out of overlays_after. */
3351 if (!NILP (prev))
3352 XCDR (prev) = next;
3353 else
3354 buf->overlays_after = next;
3355 tail = prev;
3356 continue;
3358 #endif
3360 beg = OVERLAY_START (overlay);
3361 end = OVERLAY_END (overlay);
3363 /* Stop looking, when we know that nothing further
3364 can possibly end before POS. */
3365 if (OVERLAY_POSITION (beg) > pos)
3366 break;
3368 if (OVERLAY_POSITION (end) <= pos)
3370 /* OVERLAY needs to be moved. */
3371 EMACS_INT where = OVERLAY_POSITION (end);
3372 struct Lisp_Overlay *other, *other_prev;
3374 /* Splice the cons cell TAIL out of overlays_after. */
3375 if (prev)
3376 prev->next = next;
3377 else
3378 buf->overlays_after = next;
3380 /* Search thru overlays_before for where to put it. */
3381 other_prev = NULL;
3382 for (other = buf->overlays_before; other;
3383 other_prev = other, other = other->next)
3385 Lisp_Object otherend, otheroverlay;
3387 XSETMISC (otheroverlay, other);
3388 eassert (OVERLAY_VALID (otheroverlay));
3390 otherend = OVERLAY_END (otheroverlay);
3391 if (OVERLAY_POSITION (otherend) <= where)
3392 break;
3395 /* Add TAIL to overlays_before before OTHER. */
3396 tail->next = other;
3397 if (other_prev)
3398 other_prev->next = tail;
3399 else
3400 buf->overlays_before = tail;
3401 tail = prev;
3405 buf->overlay_center = pos;
3408 void
3409 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3411 /* After an insertion, the lists are still sorted properly,
3412 but we may need to update the value of the overlay center. */
3413 if (current_buffer->overlay_center >= pos)
3414 current_buffer->overlay_center += length;
3417 void
3418 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3420 if (current_buffer->overlay_center < pos)
3421 /* The deletion was to our right. No change needed; the before- and
3422 after-lists are still consistent. */
3424 else if (current_buffer->overlay_center > pos + length)
3425 /* The deletion was to our left. We need to adjust the center value
3426 to account for the change in position, but the lists are consistent
3427 given the new value. */
3428 current_buffer->overlay_center -= length;
3429 else
3430 /* We're right in the middle. There might be things on the after-list
3431 that now belong on the before-list. Recentering will move them,
3432 and also update the center point. */
3433 recenter_overlay_lists (current_buffer, pos);
3436 /* Fix up overlays that were garbled as a result of permuting markers
3437 in the range START through END. Any overlay with at least one
3438 endpoint in this range will need to be unlinked from the overlay
3439 list and reinserted in its proper place.
3440 Such an overlay might even have negative size at this point.
3441 If so, we'll make the overlay empty. */
3442 void
3443 fix_start_end_in_overlays (register EMACS_INT start, register EMACS_INT end)
3445 Lisp_Object overlay;
3446 struct Lisp_Overlay *before_list, *after_list;
3447 /* These are either nil, indicating that before_list or after_list
3448 should be assigned, or the cons cell the cdr of which should be
3449 assigned. */
3450 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3451 /* 'Parent', likewise, indicates a cons cell or
3452 current_buffer->overlays_before or overlays_after, depending
3453 which loop we're in. */
3454 struct Lisp_Overlay *tail, *parent;
3455 EMACS_INT startpos, endpos;
3457 /* This algorithm shifts links around instead of consing and GCing.
3458 The loop invariant is that before_list (resp. after_list) is a
3459 well-formed list except that its last element, the CDR of beforep
3460 (resp. afterp) if beforep (afterp) isn't nil or before_list
3461 (after_list) if it is, is still uninitialized. So it's not a bug
3462 that before_list isn't initialized, although it may look
3463 strange. */
3464 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3466 XSETMISC (overlay, tail);
3468 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3469 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3471 /* If the overlay is backwards, make it empty. */
3472 if (endpos < startpos)
3474 startpos = endpos;
3475 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3476 Qnil);
3479 if (endpos < start)
3480 break;
3482 if (endpos < end
3483 || (startpos >= start && startpos < end))
3485 /* Add it to the end of the wrong list. Later on,
3486 recenter_overlay_lists will move it to the right place. */
3487 if (endpos < current_buffer->overlay_center)
3489 if (!afterp)
3490 after_list = tail;
3491 else
3492 afterp->next = tail;
3493 afterp = tail;
3495 else
3497 if (!beforep)
3498 before_list = tail;
3499 else
3500 beforep->next = tail;
3501 beforep = tail;
3503 if (!parent)
3504 current_buffer->overlays_before = tail->next;
3505 else
3506 parent->next = tail->next;
3507 tail = tail->next;
3509 else
3510 parent = tail, tail = parent->next;
3512 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3514 XSETMISC (overlay, tail);
3516 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3517 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3519 /* If the overlay is backwards, make it empty. */
3520 if (endpos < startpos)
3522 startpos = endpos;
3523 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3524 Qnil);
3527 if (startpos >= end)
3528 break;
3530 if (startpos >= start
3531 || (endpos >= start && endpos < end))
3533 if (endpos < current_buffer->overlay_center)
3535 if (!afterp)
3536 after_list = tail;
3537 else
3538 afterp->next = tail;
3539 afterp = tail;
3541 else
3543 if (!beforep)
3544 before_list = tail;
3545 else
3546 beforep->next = tail;
3547 beforep = tail;
3549 if (!parent)
3550 current_buffer->overlays_after = tail->next;
3551 else
3552 parent->next = tail->next;
3553 tail = tail->next;
3555 else
3556 parent = tail, tail = parent->next;
3559 /* Splice the constructed (wrong) lists into the buffer's lists,
3560 and let the recenter function make it sane again. */
3561 if (beforep)
3563 beforep->next = current_buffer->overlays_before;
3564 current_buffer->overlays_before = before_list;
3566 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3568 if (afterp)
3570 afterp->next = current_buffer->overlays_after;
3571 current_buffer->overlays_after = after_list;
3573 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3576 /* We have two types of overlay: the one whose ending marker is
3577 after-insertion-marker (this is the usual case) and the one whose
3578 ending marker is before-insertion-marker. When `overlays_before'
3579 contains overlays of the latter type and the former type in this
3580 order and both overlays end at inserting position, inserting a text
3581 increases only the ending marker of the latter type, which results
3582 in incorrect ordering of `overlays_before'.
3584 This function fixes ordering of overlays in the slot
3585 `overlays_before' of the buffer *BP. Before the insertion, `point'
3586 was at PREV, and now is at POS. */
3588 void
3589 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3591 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3592 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3593 Lisp_Object tem;
3594 EMACS_INT end;
3596 /* After the insertion, the several overlays may be in incorrect
3597 order. The possibility is that, in the list `overlays_before',
3598 an overlay which ends at POS appears after an overlay which ends
3599 at PREV. Since POS is greater than PREV, we must fix the
3600 ordering of these overlays, by moving overlays ends at POS before
3601 the overlays ends at PREV. */
3603 /* At first, find a place where disordered overlays should be linked
3604 in. It is where an overlay which end before POS exists. (i.e. an
3605 overlay whose ending marker is after-insertion-marker if disorder
3606 exists). */
3607 while (tail
3608 && (XSETMISC (tem, tail),
3609 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3611 parent = tail;
3612 tail = tail->next;
3615 /* If we don't find such an overlay,
3616 or the found one ends before PREV,
3617 or the found one is the last one in the list,
3618 we don't have to fix anything. */
3619 if (!tail || end < prev || !tail->next)
3620 return;
3622 right_pair = parent;
3623 parent = tail;
3624 tail = tail->next;
3626 /* Now, end position of overlays in the list TAIL should be before
3627 or equal to PREV. In the loop, an overlay which ends at POS is
3628 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3629 we found an overlay which ends before PREV, the remaining
3630 overlays are in correct order. */
3631 while (tail)
3633 XSETMISC (tem, tail);
3634 end = OVERLAY_POSITION (OVERLAY_END (tem));
3636 if (end == pos)
3637 { /* This overlay is disordered. */
3638 struct Lisp_Overlay *found = tail;
3640 /* Unlink the found overlay. */
3641 tail = found->next;
3642 parent->next = tail;
3643 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3644 and link it into the right place. */
3645 if (!right_pair)
3647 found->next = bp->overlays_before;
3648 bp->overlays_before = found;
3650 else
3652 found->next = right_pair->next;
3653 right_pair->next = found;
3656 else if (end == prev)
3658 parent = tail;
3659 tail = tail->next;
3661 else /* No more disordered overlay. */
3662 break;
3666 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3667 doc: /* Return t if OBJECT is an overlay. */)
3668 (Lisp_Object object)
3670 return (OVERLAYP (object) ? Qt : Qnil);
3673 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3674 doc: /* Create a new overlay with range BEG to END in BUFFER.
3675 If omitted, BUFFER defaults to the current buffer.
3676 BEG and END may be integers or markers.
3677 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3678 for the front of the overlay advance when text is inserted there
3679 \(which means the text *is not* included in the overlay).
3680 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3681 for the rear of the overlay advance when text is inserted there
3682 \(which means the text *is* included in the overlay). */)
3683 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3685 Lisp_Object overlay;
3686 struct buffer *b;
3688 if (NILP (buffer))
3689 XSETBUFFER (buffer, current_buffer);
3690 else
3691 CHECK_BUFFER (buffer);
3692 if (MARKERP (beg)
3693 && ! EQ (Fmarker_buffer (beg), buffer))
3694 error ("Marker points into wrong buffer");
3695 if (MARKERP (end)
3696 && ! EQ (Fmarker_buffer (end), buffer))
3697 error ("Marker points into wrong buffer");
3699 CHECK_NUMBER_COERCE_MARKER (beg);
3700 CHECK_NUMBER_COERCE_MARKER (end);
3702 if (XINT (beg) > XINT (end))
3704 Lisp_Object temp;
3705 temp = beg; beg = end; end = temp;
3708 b = XBUFFER (buffer);
3710 beg = Fset_marker (Fmake_marker (), beg, buffer);
3711 end = Fset_marker (Fmake_marker (), end, buffer);
3713 if (!NILP (front_advance))
3714 XMARKER (beg)->insertion_type = 1;
3715 if (!NILP (rear_advance))
3716 XMARKER (end)->insertion_type = 1;
3718 overlay = allocate_misc ();
3719 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3720 XOVERLAY (overlay)->start = beg;
3721 XOVERLAY (overlay)->end = end;
3722 XOVERLAY (overlay)->plist = Qnil;
3723 XOVERLAY (overlay)->next = NULL;
3725 /* Put the new overlay on the wrong list. */
3726 end = OVERLAY_END (overlay);
3727 if (OVERLAY_POSITION (end) < b->overlay_center)
3729 if (b->overlays_after)
3730 XOVERLAY (overlay)->next = b->overlays_after;
3731 b->overlays_after = XOVERLAY (overlay);
3733 else
3735 if (b->overlays_before)
3736 XOVERLAY (overlay)->next = b->overlays_before;
3737 b->overlays_before = XOVERLAY (overlay);
3740 /* This puts it in the right list, and in the right order. */
3741 recenter_overlay_lists (b, b->overlay_center);
3743 /* We don't need to redisplay the region covered by the overlay, because
3744 the overlay has no properties at the moment. */
3746 return overlay;
3749 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3751 static void
3752 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3754 if (start > end)
3756 EMACS_INT temp = start;
3757 start = end;
3758 end = temp;
3761 BUF_COMPUTE_UNCHANGED (buf, start, end);
3763 /* If this is a buffer not in the selected window,
3764 we must do other windows. */
3765 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3766 windows_or_buffers_changed = 1;
3767 /* If multiple windows show this buffer, we must do other windows. */
3768 else if (buffer_shared > 1)
3769 windows_or_buffers_changed = 1;
3770 /* If we modify an overlay at the end of the buffer, we cannot
3771 be sure that window end is still valid. */
3772 else if (end >= ZV && start <= ZV)
3773 windows_or_buffers_changed = 1;
3775 ++BUF_OVERLAY_MODIFF (buf);
3779 static struct Lisp_Overlay *
3780 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3782 struct Lisp_Overlay *tmp, *prev;
3783 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3784 if (tmp == overlay)
3786 if (prev)
3787 prev->next = tmp->next;
3788 else
3789 list = tmp->next;
3790 overlay->next = NULL;
3791 break;
3793 return list;
3796 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3797 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3798 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3799 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3800 buffer. */)
3801 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3803 struct buffer *b, *ob;
3804 Lisp_Object obuffer;
3805 int count = SPECPDL_INDEX ();
3807 CHECK_OVERLAY (overlay);
3808 if (NILP (buffer))
3809 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3810 if (NILP (buffer))
3811 XSETBUFFER (buffer, current_buffer);
3812 CHECK_BUFFER (buffer);
3814 if (MARKERP (beg)
3815 && ! EQ (Fmarker_buffer (beg), buffer))
3816 error ("Marker points into wrong buffer");
3817 if (MARKERP (end)
3818 && ! EQ (Fmarker_buffer (end), buffer))
3819 error ("Marker points into wrong buffer");
3821 CHECK_NUMBER_COERCE_MARKER (beg);
3822 CHECK_NUMBER_COERCE_MARKER (end);
3824 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3825 return Fdelete_overlay (overlay);
3827 if (XINT (beg) > XINT (end))
3829 Lisp_Object temp;
3830 temp = beg; beg = end; end = temp;
3833 specbind (Qinhibit_quit, Qt);
3835 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3836 b = XBUFFER (buffer);
3837 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3839 /* If the overlay has changed buffers, do a thorough redisplay. */
3840 if (!EQ (buffer, obuffer))
3842 /* Redisplay where the overlay was. */
3843 if (!NILP (obuffer))
3845 EMACS_INT o_beg;
3846 EMACS_INT o_end;
3848 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3849 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3851 modify_overlay (ob, o_beg, o_end);
3854 /* Redisplay where the overlay is going to be. */
3855 modify_overlay (b, XINT (beg), XINT (end));
3857 else
3858 /* Redisplay the area the overlay has just left, or just enclosed. */
3860 EMACS_INT o_beg, o_end;
3862 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3863 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3865 if (o_beg == XINT (beg))
3866 modify_overlay (b, o_end, XINT (end));
3867 else if (o_end == XINT (end))
3868 modify_overlay (b, o_beg, XINT (beg));
3869 else
3871 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3872 if (XINT (end) > o_end) o_end = XINT (end);
3873 modify_overlay (b, o_beg, o_end);
3877 if (!NILP (obuffer))
3879 ob->overlays_before
3880 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3881 ob->overlays_after
3882 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3883 eassert (XOVERLAY (overlay)->next == NULL);
3886 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3887 Fset_marker (OVERLAY_END (overlay), end, buffer);
3889 /* Put the overlay on the wrong list. */
3890 end = OVERLAY_END (overlay);
3891 if (OVERLAY_POSITION (end) < b->overlay_center)
3893 XOVERLAY (overlay)->next = b->overlays_after;
3894 b->overlays_after = XOVERLAY (overlay);
3896 else
3898 XOVERLAY (overlay)->next = b->overlays_before;
3899 b->overlays_before = XOVERLAY (overlay);
3902 /* This puts it in the right list, and in the right order. */
3903 recenter_overlay_lists (b, b->overlay_center);
3905 return unbind_to (count, overlay);
3908 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3909 doc: /* Delete the overlay OVERLAY from its buffer. */)
3910 (Lisp_Object overlay)
3912 Lisp_Object buffer;
3913 struct buffer *b;
3914 int count = SPECPDL_INDEX ();
3916 CHECK_OVERLAY (overlay);
3918 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3919 if (NILP (buffer))
3920 return Qnil;
3922 b = XBUFFER (buffer);
3923 specbind (Qinhibit_quit, Qt);
3925 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3926 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3927 eassert (XOVERLAY (overlay)->next == NULL);
3928 modify_overlay (b,
3929 marker_position (OVERLAY_START (overlay)),
3930 marker_position (OVERLAY_END (overlay)));
3931 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3932 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3934 /* When deleting an overlay with before or after strings, turn off
3935 display optimizations for the affected buffer, on the basis that
3936 these strings may contain newlines. This is easier to do than to
3937 check for that situation during redisplay. */
3938 if (!windows_or_buffers_changed
3939 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3940 || !NILP (Foverlay_get (overlay, Qafter_string))))
3941 b->prevent_redisplay_optimizations_p = 1;
3943 return unbind_to (count, Qnil);
3946 /* Overlay dissection functions. */
3948 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3949 doc: /* Return the position at which OVERLAY starts. */)
3950 (Lisp_Object overlay)
3952 CHECK_OVERLAY (overlay);
3954 return (Fmarker_position (OVERLAY_START (overlay)));
3957 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3958 doc: /* Return the position at which OVERLAY ends. */)
3959 (Lisp_Object overlay)
3961 CHECK_OVERLAY (overlay);
3963 return (Fmarker_position (OVERLAY_END (overlay)));
3966 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3967 doc: /* Return the buffer OVERLAY belongs to.
3968 Return nil if OVERLAY has been deleted. */)
3969 (Lisp_Object overlay)
3971 CHECK_OVERLAY (overlay);
3973 return Fmarker_buffer (OVERLAY_START (overlay));
3976 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3977 doc: /* Return a list of the properties on OVERLAY.
3978 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3979 OVERLAY. */)
3980 (Lisp_Object overlay)
3982 CHECK_OVERLAY (overlay);
3984 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3988 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3989 doc: /* Return a list of the overlays that contain the character at POS. */)
3990 (Lisp_Object pos)
3992 int noverlays;
3993 Lisp_Object *overlay_vec;
3994 int len;
3995 Lisp_Object result;
3997 CHECK_NUMBER_COERCE_MARKER (pos);
3999 len = 10;
4000 /* We can't use alloca here because overlays_at can call xrealloc. */
4001 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4003 /* Put all the overlays we want in a vector in overlay_vec.
4004 Store the length in len. */
4005 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4006 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4008 /* Make a list of them all. */
4009 result = Flist (noverlays, overlay_vec);
4011 xfree (overlay_vec);
4012 return result;
4015 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4016 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4017 Overlap means that at least one character is contained within the overlay
4018 and also contained within the specified region.
4019 Empty overlays are included in the result if they are located at BEG,
4020 between BEG and END, or at END provided END denotes the position at the
4021 end of the buffer. */)
4022 (Lisp_Object beg, Lisp_Object end)
4024 int noverlays;
4025 Lisp_Object *overlay_vec;
4026 int len;
4027 Lisp_Object result;
4029 CHECK_NUMBER_COERCE_MARKER (beg);
4030 CHECK_NUMBER_COERCE_MARKER (end);
4032 len = 10;
4033 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4035 /* Put all the overlays we want in a vector in overlay_vec.
4036 Store the length in len. */
4037 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4038 NULL, NULL);
4040 /* Make a list of them all. */
4041 result = Flist (noverlays, overlay_vec);
4043 xfree (overlay_vec);
4044 return result;
4047 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4048 1, 1, 0,
4049 doc: /* Return the next position after POS where an overlay starts or ends.
4050 If there are no overlay boundaries from POS to (point-max),
4051 the value is (point-max). */)
4052 (Lisp_Object pos)
4054 int noverlays;
4055 EMACS_INT endpos;
4056 Lisp_Object *overlay_vec;
4057 int len;
4058 int i;
4060 CHECK_NUMBER_COERCE_MARKER (pos);
4062 len = 10;
4063 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4065 /* Put all the overlays we want in a vector in overlay_vec.
4066 Store the length in len.
4067 endpos gets the position where the next overlay starts. */
4068 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4069 &endpos, (EMACS_INT *) 0, 1);
4071 /* If any of these overlays ends before endpos,
4072 use its ending point instead. */
4073 for (i = 0; i < noverlays; i++)
4075 Lisp_Object oend;
4076 EMACS_INT oendpos;
4078 oend = OVERLAY_END (overlay_vec[i]);
4079 oendpos = OVERLAY_POSITION (oend);
4080 if (oendpos < endpos)
4081 endpos = oendpos;
4084 xfree (overlay_vec);
4085 return make_number (endpos);
4088 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4089 Sprevious_overlay_change, 1, 1, 0,
4090 doc: /* Return the previous position before POS where an overlay starts or ends.
4091 If there are no overlay boundaries from (point-min) to POS,
4092 the value is (point-min). */)
4093 (Lisp_Object pos)
4095 int noverlays;
4096 EMACS_INT prevpos;
4097 Lisp_Object *overlay_vec;
4098 int len;
4100 CHECK_NUMBER_COERCE_MARKER (pos);
4102 /* At beginning of buffer, we know the answer;
4103 avoid bug subtracting 1 below. */
4104 if (XINT (pos) == BEGV)
4105 return pos;
4107 len = 10;
4108 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4110 /* Put all the overlays we want in a vector in overlay_vec.
4111 Store the length in len.
4112 prevpos gets the position of the previous change. */
4113 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4114 (EMACS_INT *) 0, &prevpos, 1);
4116 xfree (overlay_vec);
4117 return make_number (prevpos);
4120 /* These functions are for debugging overlays. */
4122 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4123 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4124 The car has all the overlays before the overlay center;
4125 the cdr has all the overlays after the overlay center.
4126 Recentering overlays moves overlays between these lists.
4127 The lists you get are copies, so that changing them has no effect.
4128 However, the overlays you get are the real objects that the buffer uses. */)
4129 (void)
4131 struct Lisp_Overlay *ol;
4132 Lisp_Object before = Qnil, after = Qnil, tmp;
4133 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4135 XSETMISC (tmp, ol);
4136 before = Fcons (tmp, before);
4138 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4140 XSETMISC (tmp, ol);
4141 after = Fcons (tmp, after);
4143 return Fcons (Fnreverse (before), Fnreverse (after));
4146 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4147 doc: /* Recenter the overlays of the current buffer around position POS.
4148 That makes overlay lookup faster for positions near POS (but perhaps slower
4149 for positions far away from POS). */)
4150 (Lisp_Object pos)
4152 CHECK_NUMBER_COERCE_MARKER (pos);
4154 recenter_overlay_lists (current_buffer, XINT (pos));
4155 return Qnil;
4158 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4159 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4160 (Lisp_Object overlay, Lisp_Object prop)
4162 CHECK_OVERLAY (overlay);
4163 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4166 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4167 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4168 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4170 Lisp_Object tail, buffer;
4171 int changed;
4173 CHECK_OVERLAY (overlay);
4175 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4177 for (tail = XOVERLAY (overlay)->plist;
4178 CONSP (tail) && CONSP (XCDR (tail));
4179 tail = XCDR (XCDR (tail)))
4180 if (EQ (XCAR (tail), prop))
4182 changed = !EQ (XCAR (XCDR (tail)), value);
4183 XSETCAR (XCDR (tail), value);
4184 goto found;
4186 /* It wasn't in the list, so add it to the front. */
4187 changed = !NILP (value);
4188 XOVERLAY (overlay)->plist
4189 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4190 found:
4191 if (! NILP (buffer))
4193 if (changed)
4194 modify_overlay (XBUFFER (buffer),
4195 marker_position (OVERLAY_START (overlay)),
4196 marker_position (OVERLAY_END (overlay)));
4197 if (EQ (prop, Qevaporate) && ! NILP (value)
4198 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4199 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4200 Fdelete_overlay (overlay);
4203 return value;
4206 /* Subroutine of report_overlay_modification. */
4208 /* Lisp vector holding overlay hook functions to call.
4209 Vector elements come in pairs.
4210 Each even-index element is a list of hook functions.
4211 The following odd-index element is the overlay they came from.
4213 Before the buffer change, we fill in this vector
4214 as we call overlay hook functions.
4215 After the buffer change, we get the functions to call from this vector.
4216 This way we always call the same functions before and after the change. */
4217 static Lisp_Object last_overlay_modification_hooks;
4219 /* Number of elements actually used in last_overlay_modification_hooks. */
4220 static int last_overlay_modification_hooks_used;
4222 /* Add one functionlist/overlay pair
4223 to the end of last_overlay_modification_hooks. */
4225 static void
4226 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4228 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4230 if (last_overlay_modification_hooks_used == oldsize)
4231 last_overlay_modification_hooks = larger_vector
4232 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4233 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4234 functionlist); last_overlay_modification_hooks_used++;
4235 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4236 overlay); last_overlay_modification_hooks_used++;
4239 /* Run the modification-hooks of overlays that include
4240 any part of the text in START to END.
4241 If this change is an insertion, also
4242 run the insert-before-hooks of overlay starting at END,
4243 and the insert-after-hooks of overlay ending at START.
4245 This is called both before and after the modification.
4246 AFTER is nonzero when we call after the modification.
4248 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4249 When AFTER is nonzero, they are the start position,
4250 the position after the inserted new text,
4251 and the length of deleted or replaced old text. */
4253 void
4254 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4255 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4257 Lisp_Object prop, overlay;
4258 struct Lisp_Overlay *tail;
4259 /* 1 if this change is an insertion. */
4260 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4261 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4263 overlay = Qnil;
4264 tail = NULL;
4266 /* We used to run the functions as soon as we found them and only register
4267 them in last_overlay_modification_hooks for the purpose of the `after'
4268 case. But running elisp code as we traverse the list of overlays is
4269 painful because the list can be modified by the elisp code so we had to
4270 copy at several places. We now simply do a read-only traversal that
4271 only collects the functions to run and we run them afterwards. It's
4272 simpler, especially since all the code was already there. -stef */
4274 if (!after)
4276 /* We are being called before a change.
4277 Scan the overlays to find the functions to call. */
4278 last_overlay_modification_hooks_used = 0;
4279 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4281 EMACS_INT startpos, endpos;
4282 Lisp_Object ostart, oend;
4284 XSETMISC (overlay, tail);
4286 ostart = OVERLAY_START (overlay);
4287 oend = OVERLAY_END (overlay);
4288 endpos = OVERLAY_POSITION (oend);
4289 if (XFASTINT (start) > endpos)
4290 break;
4291 startpos = OVERLAY_POSITION (ostart);
4292 if (insertion && (XFASTINT (start) == startpos
4293 || XFASTINT (end) == startpos))
4295 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4296 if (!NILP (prop))
4297 add_overlay_mod_hooklist (prop, overlay);
4299 if (insertion && (XFASTINT (start) == endpos
4300 || XFASTINT (end) == endpos))
4302 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4303 if (!NILP (prop))
4304 add_overlay_mod_hooklist (prop, overlay);
4306 /* Test for intersecting intervals. This does the right thing
4307 for both insertion and deletion. */
4308 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4310 prop = Foverlay_get (overlay, Qmodification_hooks);
4311 if (!NILP (prop))
4312 add_overlay_mod_hooklist (prop, overlay);
4316 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4318 EMACS_INT startpos, endpos;
4319 Lisp_Object ostart, oend;
4321 XSETMISC (overlay, tail);
4323 ostart = OVERLAY_START (overlay);
4324 oend = OVERLAY_END (overlay);
4325 startpos = OVERLAY_POSITION (ostart);
4326 endpos = OVERLAY_POSITION (oend);
4327 if (XFASTINT (end) < startpos)
4328 break;
4329 if (insertion && (XFASTINT (start) == startpos
4330 || XFASTINT (end) == startpos))
4332 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4333 if (!NILP (prop))
4334 add_overlay_mod_hooklist (prop, overlay);
4336 if (insertion && (XFASTINT (start) == endpos
4337 || XFASTINT (end) == endpos))
4339 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4340 if (!NILP (prop))
4341 add_overlay_mod_hooklist (prop, overlay);
4343 /* Test for intersecting intervals. This does the right thing
4344 for both insertion and deletion. */
4345 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4347 prop = Foverlay_get (overlay, Qmodification_hooks);
4348 if (!NILP (prop))
4349 add_overlay_mod_hooklist (prop, overlay);
4354 GCPRO4 (overlay, arg1, arg2, arg3);
4356 /* Call the functions recorded in last_overlay_modification_hooks.
4357 First copy the vector contents, in case some of these hooks
4358 do subsequent modification of the buffer. */
4359 int size = last_overlay_modification_hooks_used;
4360 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4361 int i;
4363 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4364 size * sizeof (Lisp_Object));
4365 gcpro1.var = copy;
4366 gcpro1.nvars = size;
4368 for (i = 0; i < size;)
4370 Lisp_Object prop, overlay;
4371 prop = copy[i++];
4372 overlay = copy[i++];
4373 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4376 UNGCPRO;
4379 static void
4380 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4381 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4383 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4385 GCPRO4 (list, arg1, arg2, arg3);
4387 while (CONSP (list))
4389 if (NILP (arg3))
4390 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4391 else
4392 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4393 list = XCDR (list);
4395 UNGCPRO;
4398 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4399 property is set. */
4400 void
4401 evaporate_overlays (EMACS_INT pos)
4403 Lisp_Object overlay, hit_list;
4404 struct Lisp_Overlay *tail;
4406 hit_list = Qnil;
4407 if (pos <= current_buffer->overlay_center)
4408 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4410 EMACS_INT endpos;
4411 XSETMISC (overlay, tail);
4412 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4413 if (endpos < pos)
4414 break;
4415 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4416 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4417 hit_list = Fcons (overlay, hit_list);
4419 else
4420 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4422 EMACS_INT startpos;
4423 XSETMISC (overlay, tail);
4424 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4425 if (startpos > pos)
4426 break;
4427 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4428 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4429 hit_list = Fcons (overlay, hit_list);
4431 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4432 Fdelete_overlay (XCAR (hit_list));
4435 /* Somebody has tried to store a value with an unacceptable type
4436 in the slot with offset OFFSET. */
4438 void
4439 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4441 Lisp_Object predicate;
4443 switch (type)
4445 case_Lisp_Int: predicate = Qintegerp; break;
4446 case Lisp_String: predicate = Qstringp; break;
4447 case Lisp_Symbol: predicate = Qsymbolp; break;
4448 default: abort ();
4451 wrong_type_argument (predicate, newval);
4455 /***********************************************************************
4456 Allocation with mmap
4457 ***********************************************************************/
4459 #ifdef USE_MMAP_FOR_BUFFERS
4461 #include <sys/types.h>
4462 #include <sys/mman.h>
4464 #ifndef MAP_ANON
4465 #ifdef MAP_ANONYMOUS
4466 #define MAP_ANON MAP_ANONYMOUS
4467 #else
4468 #define MAP_ANON 0
4469 #endif
4470 #endif
4472 #ifndef MAP_FAILED
4473 #define MAP_FAILED ((void *) -1)
4474 #endif
4476 #include <stdio.h>
4478 #if MAP_ANON == 0
4479 #include <fcntl.h>
4480 #endif
4482 #include "coding.h"
4485 /* Memory is allocated in regions which are mapped using mmap(2).
4486 The current implementation lets the system select mapped
4487 addresses; we're not using MAP_FIXED in general, except when
4488 trying to enlarge regions.
4490 Each mapped region starts with a mmap_region structure, the user
4491 area starts after that structure, aligned to MEM_ALIGN.
4493 +-----------------------+
4494 | struct mmap_info + |
4495 | padding |
4496 +-----------------------+
4497 | user data |
4500 +-----------------------+ */
4502 struct mmap_region
4504 /* User-specified size. */
4505 size_t nbytes_specified;
4507 /* Number of bytes mapped */
4508 size_t nbytes_mapped;
4510 /* Pointer to the location holding the address of the memory
4511 allocated with the mmap'd block. The variable actually points
4512 after this structure. */
4513 POINTER_TYPE **var;
4515 /* Next and previous in list of all mmap'd regions. */
4516 struct mmap_region *next, *prev;
4519 /* Doubly-linked list of mmap'd regions. */
4521 static struct mmap_region *mmap_regions;
4523 /* File descriptor for mmap. If we don't have anonymous mapping,
4524 /dev/zero will be opened on it. */
4526 static int mmap_fd;
4528 /* Temporary storage for mmap_set_vars, see there. */
4530 static struct mmap_region *mmap_regions_1;
4531 static int mmap_fd_1;
4533 /* Page size on this system. */
4535 static int mmap_page_size;
4537 /* 1 means mmap has been intialized. */
4539 static int mmap_initialized_p;
4541 /* Value is X rounded up to the next multiple of N. */
4543 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4545 /* Size of mmap_region structure plus padding. */
4547 #define MMAP_REGION_STRUCT_SIZE \
4548 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4550 /* Given a pointer P to the start of the user-visible part of a mapped
4551 region, return a pointer to the start of the region. */
4553 #define MMAP_REGION(P) \
4554 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4556 /* Given a pointer P to the start of a mapped region, return a pointer
4557 to the start of the user-visible part of the region. */
4559 #define MMAP_USER_AREA(P) \
4560 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4562 #define MEM_ALIGN sizeof (double)
4564 /* Predicate returning true if part of the address range [START .. END]
4565 is currently mapped. Used to prevent overwriting an existing
4566 memory mapping.
4568 Default is to conservativly assume the address range is occupied by
4569 something else. This can be overridden by system configuration
4570 files if system-specific means to determine this exists. */
4572 #ifndef MMAP_ALLOCATED_P
4573 #define MMAP_ALLOCATED_P(start, end) 1
4574 #endif
4576 /* Function prototypes. */
4578 static int mmap_free_1 (struct mmap_region *);
4579 static int mmap_enlarge (struct mmap_region *, int);
4580 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4581 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4582 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4583 static void mmap_free (POINTER_TYPE **ptr);
4584 static void mmap_init (void);
4587 /* Return a region overlapping address range START...END, or null if
4588 none. END is not including, i.e. the last byte in the range
4589 is at END - 1. */
4591 static struct mmap_region *
4592 mmap_find (start, end)
4593 POINTER_TYPE *start, *end;
4595 struct mmap_region *r;
4596 char *s = (char *) start, *e = (char *) end;
4598 for (r = mmap_regions; r; r = r->next)
4600 char *rstart = (char *) r;
4601 char *rend = rstart + r->nbytes_mapped;
4603 if (/* First byte of range, i.e. START, in this region? */
4604 (s >= rstart && s < rend)
4605 /* Last byte of range, i.e. END - 1, in this region? */
4606 || (e > rstart && e <= rend)
4607 /* First byte of this region in the range? */
4608 || (rstart >= s && rstart < e)
4609 /* Last byte of this region in the range? */
4610 || (rend > s && rend <= e))
4611 break;
4614 return r;
4618 /* Unmap a region. P is a pointer to the start of the user-araa of
4619 the region. Value is non-zero if successful. */
4621 static int
4622 mmap_free_1 (r)
4623 struct mmap_region *r;
4625 if (r->next)
4626 r->next->prev = r->prev;
4627 if (r->prev)
4628 r->prev->next = r->next;
4629 else
4630 mmap_regions = r->next;
4632 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4634 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4635 return 0;
4638 return 1;
4642 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4643 Value is non-zero if successful. */
4645 static int
4646 mmap_enlarge (r, npages)
4647 struct mmap_region *r;
4648 int npages;
4650 char *region_end = (char *) r + r->nbytes_mapped;
4651 size_t nbytes;
4652 int success = 0;
4654 if (npages < 0)
4656 /* Unmap pages at the end of the region. */
4657 nbytes = - npages * mmap_page_size;
4658 if (munmap (region_end - nbytes, nbytes) == -1)
4659 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4660 else
4662 r->nbytes_mapped -= nbytes;
4663 success = 1;
4666 else if (npages > 0)
4668 nbytes = npages * mmap_page_size;
4670 /* Try to map additional pages at the end of the region. We
4671 cannot do this if the address range is already occupied by
4672 something else because mmap deletes any previous mapping.
4673 I'm not sure this is worth doing, let's see. */
4674 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4676 POINTER_TYPE *p;
4678 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4679 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4680 if (p == MAP_FAILED)
4681 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4682 else if (p != (POINTER_TYPE *) region_end)
4684 /* Kernels are free to choose a different address. In
4685 that case, unmap what we've mapped above; we have
4686 no use for it. */
4687 if (munmap (p, nbytes) == -1)
4688 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4690 else
4692 r->nbytes_mapped += nbytes;
4693 success = 1;
4698 return success;
4702 /* Set or reset variables holding references to mapped regions. If
4703 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4704 non-zero, set all variables to the start of the user-areas
4705 of mapped regions.
4707 This function is called from Fdump_emacs to ensure that the dumped
4708 Emacs doesn't contain references to memory that won't be mapped
4709 when Emacs starts. */
4711 void
4712 mmap_set_vars (restore_p)
4713 int restore_p;
4715 struct mmap_region *r;
4717 if (restore_p)
4719 mmap_regions = mmap_regions_1;
4720 mmap_fd = mmap_fd_1;
4721 for (r = mmap_regions; r; r = r->next)
4722 *r->var = MMAP_USER_AREA (r);
4724 else
4726 for (r = mmap_regions; r; r = r->next)
4727 *r->var = NULL;
4728 mmap_regions_1 = mmap_regions;
4729 mmap_regions = NULL;
4730 mmap_fd_1 = mmap_fd;
4731 mmap_fd = -1;
4736 /* Allocate a block of storage large enough to hold NBYTES bytes of
4737 data. A pointer to the data is returned in *VAR. VAR is thus the
4738 address of some variable which will use the data area.
4740 The allocation of 0 bytes is valid.
4742 If we can't allocate the necessary memory, set *VAR to null, and
4743 return null. */
4745 static POINTER_TYPE *
4746 mmap_alloc (var, nbytes)
4747 POINTER_TYPE **var;
4748 size_t nbytes;
4750 void *p;
4751 size_t map;
4753 mmap_init ();
4755 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4756 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4757 mmap_fd, 0);
4759 if (p == MAP_FAILED)
4761 if (errno != ENOMEM)
4762 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4763 p = NULL;
4765 else
4767 struct mmap_region *r = (struct mmap_region *) p;
4769 r->nbytes_specified = nbytes;
4770 r->nbytes_mapped = map;
4771 r->var = var;
4772 r->prev = NULL;
4773 r->next = mmap_regions;
4774 if (r->next)
4775 r->next->prev = r;
4776 mmap_regions = r;
4778 p = MMAP_USER_AREA (p);
4781 return *var = p;
4785 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4786 resize it to size NBYTES. Change *VAR to reflect the new block,
4787 and return this value. If more memory cannot be allocated, then
4788 leave *VAR unchanged, and return null. */
4790 static POINTER_TYPE *
4791 mmap_realloc (var, nbytes)
4792 POINTER_TYPE **var;
4793 size_t nbytes;
4795 POINTER_TYPE *result;
4797 mmap_init ();
4799 if (*var == NULL)
4800 result = mmap_alloc (var, nbytes);
4801 else if (nbytes == 0)
4803 mmap_free (var);
4804 result = mmap_alloc (var, nbytes);
4806 else
4808 struct mmap_region *r = MMAP_REGION (*var);
4809 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4811 if (room < nbytes)
4813 /* Must enlarge. */
4814 POINTER_TYPE *old_ptr = *var;
4816 /* Try to map additional pages at the end of the region.
4817 If that fails, allocate a new region, copy data
4818 from the old region, then free it. */
4819 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4820 / mmap_page_size)))
4822 r->nbytes_specified = nbytes;
4823 *var = result = old_ptr;
4825 else if (mmap_alloc (var, nbytes))
4827 memcpy (*var, old_ptr, r->nbytes_specified);
4828 mmap_free_1 (MMAP_REGION (old_ptr));
4829 result = *var;
4830 r = MMAP_REGION (result);
4831 r->nbytes_specified = nbytes;
4833 else
4835 *var = old_ptr;
4836 result = NULL;
4839 else if (room - nbytes >= mmap_page_size)
4841 /* Shrinking by at least a page. Let's give some
4842 memory back to the system.
4844 The extra parens are to make the division happens first,
4845 on positive values, so we know it will round towards
4846 zero. */
4847 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4848 result = *var;
4849 r->nbytes_specified = nbytes;
4851 else
4853 /* Leave it alone. */
4854 result = *var;
4855 r->nbytes_specified = nbytes;
4859 return result;
4863 /* Free a block of relocatable storage whose data is pointed to by
4864 PTR. Store 0 in *PTR to show there's no block allocated. */
4866 static void
4867 mmap_free (var)
4868 POINTER_TYPE **var;
4870 mmap_init ();
4872 if (*var)
4874 mmap_free_1 (MMAP_REGION (*var));
4875 *var = NULL;
4880 /* Perform necessary intializations for the use of mmap. */
4882 static void
4883 mmap_init ()
4885 #if MAP_ANON == 0
4886 /* The value of mmap_fd is initially 0 in temacs, and -1
4887 in a dumped Emacs. */
4888 if (mmap_fd <= 0)
4890 /* No anonymous mmap -- we need the file descriptor. */
4891 mmap_fd = open ("/dev/zero", O_RDONLY);
4892 if (mmap_fd == -1)
4893 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4895 #endif /* MAP_ANON == 0 */
4897 if (mmap_initialized_p)
4898 return;
4899 mmap_initialized_p = 1;
4901 #if MAP_ANON != 0
4902 mmap_fd = -1;
4903 #endif
4905 mmap_page_size = getpagesize ();
4908 #endif /* USE_MMAP_FOR_BUFFERS */
4912 /***********************************************************************
4913 Buffer-text Allocation
4914 ***********************************************************************/
4916 #ifdef REL_ALLOC
4917 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4918 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4919 extern void r_alloc_free (POINTER_TYPE **ptr);
4920 #endif /* REL_ALLOC */
4923 /* Allocate NBYTES bytes for buffer B's text buffer. */
4925 static void
4926 alloc_buffer_text (struct buffer *b, size_t nbytes)
4928 POINTER_TYPE *p;
4930 BLOCK_INPUT;
4931 #if defined USE_MMAP_FOR_BUFFERS
4932 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4933 #elif defined REL_ALLOC
4934 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4935 #else
4936 p = xmalloc (nbytes);
4937 #endif
4939 if (p == NULL)
4941 UNBLOCK_INPUT;
4942 memory_full ();
4945 b->text->beg = (unsigned char *) p;
4946 UNBLOCK_INPUT;
4949 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4950 shrink it. */
4952 void
4953 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4955 POINTER_TYPE *p;
4956 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4957 + delta);
4958 BLOCK_INPUT;
4959 #if defined USE_MMAP_FOR_BUFFERS
4960 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4961 #elif defined REL_ALLOC
4962 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4963 #else
4964 p = xrealloc (b->text->beg, nbytes);
4965 #endif
4967 if (p == NULL)
4969 UNBLOCK_INPUT;
4970 memory_full ();
4973 BUF_BEG_ADDR (b) = (unsigned char *) p;
4974 UNBLOCK_INPUT;
4978 /* Free buffer B's text buffer. */
4980 static void
4981 free_buffer_text (struct buffer *b)
4983 BLOCK_INPUT;
4985 #if defined USE_MMAP_FOR_BUFFERS
4986 mmap_free ((POINTER_TYPE **) &b->text->beg);
4987 #elif defined REL_ALLOC
4988 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4989 #else
4990 xfree (b->text->beg);
4991 #endif
4993 BUF_BEG_ADDR (b) = NULL;
4994 UNBLOCK_INPUT;
4999 /***********************************************************************
5000 Initialization
5001 ***********************************************************************/
5003 void
5004 init_buffer_once (void)
5006 int idx;
5008 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
5010 /* Make sure all markable slots in buffer_defaults
5011 are initialized reasonably, so mark_buffer won't choke. */
5012 reset_buffer (&buffer_defaults);
5013 eassert (EQ (buffer_defaults.name, make_number (0)));
5014 reset_buffer_local_variables (&buffer_defaults, 1);
5015 eassert (EQ (buffer_local_symbols.name, make_number (0)));
5016 reset_buffer (&buffer_local_symbols);
5017 reset_buffer_local_variables (&buffer_local_symbols, 1);
5018 /* Prevent GC from getting confused. */
5019 buffer_defaults.text = &buffer_defaults.own_text;
5020 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5021 BUF_INTERVALS (&buffer_defaults) = 0;
5022 BUF_INTERVALS (&buffer_local_symbols) = 0;
5023 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5024 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5025 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5026 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5028 /* Set up the default values of various buffer slots. */
5029 /* Must do these before making the first buffer! */
5031 /* real setup is done in bindings.el */
5032 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5033 buffer_defaults.header_line_format = Qnil;
5034 buffer_defaults.abbrev_mode = Qnil;
5035 buffer_defaults.overwrite_mode = Qnil;
5036 buffer_defaults.case_fold_search = Qt;
5037 buffer_defaults.auto_fill_function = Qnil;
5038 buffer_defaults.selective_display = Qnil;
5039 #ifndef old
5040 buffer_defaults.selective_display_ellipses = Qt;
5041 #endif
5042 buffer_defaults.abbrev_table = Qnil;
5043 buffer_defaults.display_table = Qnil;
5044 buffer_defaults.undo_list = Qnil;
5045 buffer_defaults.mark_active = Qnil;
5046 buffer_defaults.file_format = Qnil;
5047 buffer_defaults.auto_save_file_format = Qt;
5048 buffer_defaults.overlays_before = NULL;
5049 buffer_defaults.overlays_after = NULL;
5050 buffer_defaults.overlay_center = BEG;
5052 XSETFASTINT (buffer_defaults.tab_width, 8);
5053 buffer_defaults.truncate_lines = Qnil;
5054 buffer_defaults.word_wrap = Qnil;
5055 buffer_defaults.ctl_arrow = Qt;
5056 buffer_defaults.bidi_display_reordering = Qnil;
5057 buffer_defaults.bidi_paragraph_direction = Qnil;
5058 buffer_defaults.cursor_type = Qt;
5059 buffer_defaults.extra_line_spacing = Qnil;
5060 buffer_defaults.cursor_in_non_selected_windows = Qt;
5062 #ifdef DOS_NT
5063 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5064 #endif
5065 buffer_defaults.enable_multibyte_characters = Qt;
5066 buffer_defaults.buffer_file_coding_system = Qnil;
5067 XSETFASTINT (buffer_defaults.fill_column, 70);
5068 XSETFASTINT (buffer_defaults.left_margin, 0);
5069 buffer_defaults.cache_long_line_scans = Qnil;
5070 buffer_defaults.file_truename = Qnil;
5071 XSETFASTINT (buffer_defaults.display_count, 0);
5072 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5073 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5074 buffer_defaults.left_fringe_width = Qnil;
5075 buffer_defaults.right_fringe_width = Qnil;
5076 buffer_defaults.fringes_outside_margins = Qnil;
5077 buffer_defaults.scroll_bar_width = Qnil;
5078 buffer_defaults.vertical_scroll_bar_type = Qt;
5079 buffer_defaults.indicate_empty_lines = Qnil;
5080 buffer_defaults.indicate_buffer_boundaries = Qnil;
5081 buffer_defaults.fringe_indicator_alist = Qnil;
5082 buffer_defaults.fringe_cursor_alist = Qnil;
5083 buffer_defaults.scroll_up_aggressively = Qnil;
5084 buffer_defaults.scroll_down_aggressively = Qnil;
5085 buffer_defaults.display_time = Qnil;
5087 /* Assign the local-flags to the slots that have default values.
5088 The local flag is a bit that is used in the buffer
5089 to say that it has its own local value for the slot.
5090 The local flag bits are in the local_var_flags slot of the buffer. */
5092 /* Nothing can work if this isn't true */
5093 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5095 /* 0 means not a lisp var, -1 means always local, else mask */
5096 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
5097 XSETINT (buffer_local_flags.filename, -1);
5098 XSETINT (buffer_local_flags.directory, -1);
5099 XSETINT (buffer_local_flags.backed_up, -1);
5100 XSETINT (buffer_local_flags.save_length, -1);
5101 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5102 XSETINT (buffer_local_flags.read_only, -1);
5103 XSETINT (buffer_local_flags.major_mode, -1);
5104 XSETINT (buffer_local_flags.mode_name, -1);
5105 XSETINT (buffer_local_flags.undo_list, -1);
5106 XSETINT (buffer_local_flags.mark_active, -1);
5107 XSETINT (buffer_local_flags.point_before_scroll, -1);
5108 XSETINT (buffer_local_flags.file_truename, -1);
5109 XSETINT (buffer_local_flags.invisibility_spec, -1);
5110 XSETINT (buffer_local_flags.file_format, -1);
5111 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5112 XSETINT (buffer_local_flags.display_count, -1);
5113 XSETINT (buffer_local_flags.display_time, -1);
5114 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5116 idx = 1;
5117 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5118 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5119 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5120 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5121 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5122 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5123 #ifndef old
5124 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5125 #endif
5126 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5127 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5128 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5129 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5131 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5132 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5133 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5134 #ifdef DOS_NT
5135 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5136 /* Make this one a permanent local. */
5137 buffer_permanent_local_flags[idx++] = 1;
5138 #endif
5139 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5140 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5141 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5142 XSETFASTINT (buffer_local_flags.bidi_display_reordering, idx); ++idx;
5143 XSETFASTINT (buffer_local_flags.bidi_paragraph_direction, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5145 /* Make this one a permanent local. */
5146 buffer_permanent_local_flags[idx++] = 1;
5147 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5149 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5150 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5151 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5152 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5154 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5160 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5161 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5162 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5163 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5165 /* Need more room? */
5166 if (idx >= MAX_PER_BUFFER_VARS)
5167 abort ();
5168 last_per_buffer_idx = idx;
5170 Vbuffer_alist = Qnil;
5171 current_buffer = 0;
5172 all_buffers = 0;
5174 QSFundamental = make_pure_c_string ("Fundamental");
5176 Qfundamental_mode = intern_c_string ("fundamental-mode");
5177 buffer_defaults.major_mode = Qfundamental_mode;
5179 Qmode_class = intern_c_string ("mode-class");
5181 Qprotected_field = intern_c_string ("protected-field");
5183 Qpermanent_local = intern_c_string ("permanent-local");
5185 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5186 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5188 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5190 /* super-magic invisible buffer */
5191 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5192 Vbuffer_alist = Qnil;
5194 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5196 inhibit_modification_hooks = 0;
5199 void
5200 init_buffer (void)
5202 char *pwd;
5203 Lisp_Object temp;
5204 int len;
5206 #ifdef USE_MMAP_FOR_BUFFERS
5208 /* When using the ralloc implementation based on mmap(2), buffer
5209 text pointers will have been set to null in the dumped Emacs.
5210 Map new memory. */
5211 struct buffer *b;
5213 for (b = all_buffers; b; b = b->next)
5214 if (b->text->beg == NULL)
5215 enlarge_buffer_text (b, 0);
5217 #endif /* USE_MMAP_FOR_BUFFERS */
5219 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5220 if (NILP (buffer_defaults.enable_multibyte_characters))
5221 Fset_buffer_multibyte (Qnil);
5223 pwd = get_current_dir_name ();
5225 if (!pwd)
5226 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5228 /* Maybe this should really use some standard subroutine
5229 whose definition is filename syntax dependent. */
5230 len = strlen (pwd);
5231 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5233 /* Grow buffer to add directory separator and '\0'. */
5234 pwd = (char *) realloc (pwd, len + 2);
5235 if (!pwd)
5236 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5237 pwd[len] = DIRECTORY_SEP;
5238 pwd[len + 1] = '\0';
5241 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5242 if (! NILP (buffer_defaults.enable_multibyte_characters))
5243 /* At this moment, we still don't know how to decode the
5244 directory name. So, we keep the bytes in multibyte form so
5245 that ENCODE_FILE correctly gets the original bytes. */
5246 current_buffer->directory
5247 = string_to_multibyte (current_buffer->directory);
5249 /* Add /: to the front of the name
5250 if it would otherwise be treated as magic. */
5251 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5252 if (! NILP (temp)
5253 /* If the default dir is just /, TEMP is non-nil
5254 because of the ange-ftp completion handler.
5255 However, it is not necessary to turn / into /:/.
5256 So avoid doing that. */
5257 && strcmp ("/", SDATA (current_buffer->directory)))
5258 current_buffer->directory
5259 = concat2 (build_string ("/:"), current_buffer->directory);
5261 temp = get_minibuffer (0);
5262 XBUFFER (temp)->directory = current_buffer->directory;
5264 free (pwd);
5267 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5268 Object stored in the current buffer. address is the address of the slot
5269 in the buffer that is current now. */
5271 /* TYPE is nil for a general Lisp variable.
5272 An integer specifies a type; then only Lisp values
5273 with that type code are allowed (except that nil is allowed too).
5274 LNAME is the Lisp-level variable name.
5275 VNAME is the name of the buffer slot.
5276 DOC is a dummy where you write the doc string as a comment. */
5277 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5278 do { \
5279 static struct Lisp_Buffer_Objfwd bo_fwd; \
5280 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5281 } while (0)
5283 static void
5284 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
5285 Lisp_Object *address, Lisp_Object type, char *doc)
5287 struct Lisp_Symbol *sym;
5288 int offset;
5290 sym = XSYMBOL (intern (namestring));
5291 offset = (char *)address - (char *)current_buffer;
5293 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5294 bo_fwd->offset = offset;
5295 bo_fwd->slottype = type;
5296 sym->redirect = SYMBOL_FORWARDED;
5298 /* I tried to do the job without a cast, but it seems impossible.
5299 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5300 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5302 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5304 if (PER_BUFFER_IDX (offset) == 0)
5305 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5306 slot of buffer_local_flags */
5307 abort ();
5311 /* initialize the buffer routines */
5312 void
5313 syms_of_buffer (void)
5315 staticpro (&last_overlay_modification_hooks);
5316 last_overlay_modification_hooks
5317 = Fmake_vector (make_number (10), Qnil);
5319 staticpro (&Vbuffer_defaults);
5320 staticpro (&Vbuffer_local_symbols);
5321 staticpro (&Qfundamental_mode);
5322 staticpro (&Qmode_class);
5323 staticpro (&QSFundamental);
5324 staticpro (&Vbuffer_alist);
5325 staticpro (&Qprotected_field);
5326 staticpro (&Qpermanent_local);
5327 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5328 staticpro (&Qpermanent_local_hook);
5329 staticpro (&Qkill_buffer_hook);
5330 Qoverlayp = intern_c_string ("overlayp");
5331 staticpro (&Qoverlayp);
5332 Qevaporate = intern_c_string ("evaporate");
5333 staticpro (&Qevaporate);
5334 Qmodification_hooks = intern_c_string ("modification-hooks");
5335 staticpro (&Qmodification_hooks);
5336 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5337 staticpro (&Qinsert_in_front_hooks);
5338 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5339 staticpro (&Qinsert_behind_hooks);
5340 Qget_file_buffer = intern_c_string ("get-file-buffer");
5341 staticpro (&Qget_file_buffer);
5342 Qpriority = intern_c_string ("priority");
5343 staticpro (&Qpriority);
5344 Qbefore_string = intern_c_string ("before-string");
5345 staticpro (&Qbefore_string);
5346 Qafter_string = intern_c_string ("after-string");
5347 staticpro (&Qafter_string);
5348 Qfirst_change_hook = intern_c_string ("first-change-hook");
5349 staticpro (&Qfirst_change_hook);
5350 Qbefore_change_functions = intern_c_string ("before-change-functions");
5351 staticpro (&Qbefore_change_functions);
5352 Qafter_change_functions = intern_c_string ("after-change-functions");
5353 staticpro (&Qafter_change_functions);
5354 /* The next one is initialized in init_buffer_once. */
5355 staticpro (&Qucs_set_table_for_input);
5357 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5358 staticpro (&Qkill_buffer_query_functions);
5360 Fput (Qprotected_field, Qerror_conditions,
5361 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5362 Fput (Qprotected_field, Qerror_message,
5363 make_pure_c_string ("Attempt to modify a protected field"));
5365 /* All these use DEFVAR_LISP_NOPRO because the slots in
5366 buffer_defaults will all be marked via Vbuffer_defaults. */
5368 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5369 &buffer_defaults.mode_line_format,
5370 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5371 This is the same as (default-value 'mode-line-format). */);
5373 DEFVAR_LISP_NOPRO ("default-header-line-format",
5374 &buffer_defaults.header_line_format,
5375 doc: /* Default value of `header-line-format' for buffers that don't override it.
5376 This is the same as (default-value 'header-line-format). */);
5378 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5379 doc: /* Default value of `cursor-type' for buffers that don't override it.
5380 This is the same as (default-value 'cursor-type). */);
5382 DEFVAR_LISP_NOPRO ("default-line-spacing",
5383 &buffer_defaults.extra_line_spacing,
5384 doc: /* Default value of `line-spacing' for buffers that don't override it.
5385 This is the same as (default-value 'line-spacing). */);
5387 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5388 &buffer_defaults.cursor_in_non_selected_windows,
5389 doc: /* Default value of `cursor-in-non-selected-windows'.
5390 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5392 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5393 &buffer_defaults.abbrev_mode,
5394 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5395 This is the same as (default-value 'abbrev-mode). */);
5397 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5398 &buffer_defaults.ctl_arrow,
5399 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5400 This is the same as (default-value 'ctl-arrow). */);
5402 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5403 &buffer_defaults.enable_multibyte_characters,
5404 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5405 This is the same as (default-value 'enable-multibyte-characters). */);
5407 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5408 &buffer_defaults.buffer_file_coding_system,
5409 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5410 This is the same as (default-value 'buffer-file-coding-system). */);
5412 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5413 &buffer_defaults.truncate_lines,
5414 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5415 This is the same as (default-value 'truncate-lines). */);
5417 DEFVAR_LISP_NOPRO ("default-fill-column",
5418 &buffer_defaults.fill_column,
5419 doc: /* Default value of `fill-column' for buffers that do not override it.
5420 This is the same as (default-value 'fill-column). */);
5422 DEFVAR_LISP_NOPRO ("default-left-margin",
5423 &buffer_defaults.left_margin,
5424 doc: /* Default value of `left-margin' for buffers that do not override it.
5425 This is the same as (default-value 'left-margin). */);
5427 DEFVAR_LISP_NOPRO ("default-tab-width",
5428 &buffer_defaults.tab_width,
5429 doc: /* Default value of `tab-width' for buffers that do not override it.
5430 This is the same as (default-value 'tab-width). */);
5432 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5433 &buffer_defaults.case_fold_search,
5434 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5435 This is the same as (default-value 'case-fold-search). */);
5437 #ifdef DOS_NT
5438 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5439 &buffer_defaults.buffer_file_type,
5440 doc: /* Default file type for buffers that do not override it.
5441 This is the same as (default-value 'buffer-file-type).
5442 The file type is nil for text, t for binary. */);
5443 #endif
5445 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5446 &buffer_defaults.left_margin_cols,
5447 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5448 This is the same as (default-value 'left-margin-width). */);
5450 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5451 &buffer_defaults.right_margin_cols,
5452 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5453 This is the same as (default-value 'right-margin-width). */);
5455 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5456 &buffer_defaults.left_fringe_width,
5457 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5458 This is the same as (default-value 'left-fringe-width). */);
5460 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5461 &buffer_defaults.right_fringe_width,
5462 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5463 This is the same as (default-value 'right-fringe-width). */);
5465 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5466 &buffer_defaults.fringes_outside_margins,
5467 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5468 This is the same as (default-value 'fringes-outside-margins). */);
5470 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5471 &buffer_defaults.scroll_bar_width,
5472 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5473 This is the same as (default-value 'scroll-bar-width). */);
5475 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5476 &buffer_defaults.vertical_scroll_bar_type,
5477 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5478 This is the same as (default-value 'vertical-scroll-bar). */);
5480 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5481 &buffer_defaults.indicate_empty_lines,
5482 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5483 This is the same as (default-value 'indicate-empty-lines). */);
5485 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5486 &buffer_defaults.indicate_buffer_boundaries,
5487 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5488 This is the same as (default-value 'indicate-buffer-boundaries). */);
5490 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5491 &buffer_defaults.fringe_indicator_alist,
5492 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5493 This is the same as (default-value 'fringe-indicator-alist'). */);
5495 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5496 &buffer_defaults.fringe_cursor_alist,
5497 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5498 This is the same as (default-value 'fringe-cursor-alist'). */);
5500 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5501 &buffer_defaults.scroll_up_aggressively,
5502 doc: /* Default value of `scroll-up-aggressively'.
5503 This value applies in buffers that don't have their own local values.
5504 This is the same as (default-value 'scroll-up-aggressively). */);
5506 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5507 &buffer_defaults.scroll_down_aggressively,
5508 doc: /* Default value of `scroll-down-aggressively'.
5509 This value applies in buffers that don't have their own local values.
5510 This is the same as (default-value 'scroll-down-aggressively). */);
5512 DEFVAR_PER_BUFFER ("header-line-format",
5513 &current_buffer->header_line_format,
5514 Qnil,
5515 doc: /* Analogous to `mode-line-format', but controls the header line.
5516 The header line appears, optionally, at the top of a window;
5517 the mode line appears at the bottom. */);
5519 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5520 Qnil,
5521 doc: /* Template for displaying mode line for current buffer.
5522 Each buffer has its own value of this variable.
5523 Value may be nil, a string, a symbol or a list or cons cell.
5524 A value of nil means don't display a mode line.
5525 For a symbol, its value is used (but it is ignored if t or nil).
5526 A string appearing directly as the value of a symbol is processed verbatim
5527 in that the %-constructs below are not recognized.
5528 Note that unless the symbol is marked as a `risky-local-variable', all
5529 properties in any strings, as well as all :eval and :propertize forms
5530 in the value of that symbol will be ignored.
5531 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5532 is used as a mode line element. Be careful--FORM should not load any files,
5533 because that can cause an infinite recursion.
5534 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5535 with the specified properties PROPS applied.
5536 For a list whose car is a symbol, the symbol's value is taken,
5537 and if that is non-nil, the cadr of the list is processed recursively.
5538 Otherwise, the caddr of the list (if there is one) is processed.
5539 For a list whose car is a string or list, each element is processed
5540 recursively and the results are effectively concatenated.
5541 For a list whose car is an integer, the cdr of the list is processed
5542 and padded (if the number is positive) or truncated (if negative)
5543 to the width specified by that number.
5544 A string is printed verbatim in the mode line except for %-constructs:
5545 (%-constructs are allowed when the string is the entire mode-line-format
5546 or when it is found in a cons-cell or a list)
5547 %b -- print buffer name. %f -- print visited file name.
5548 %F -- print frame name.
5549 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5550 %& is like %*, but ignore read-only-ness.
5551 % means buffer is read-only and * means it is modified.
5552 For a modified read-only buffer, %* gives % and %+ gives *.
5553 %s -- print process status. %l -- print the current line number.
5554 %c -- print the current column number (this makes editing slower).
5555 To make the column number update correctly in all cases,
5556 `column-number-mode' must be non-nil.
5557 %i -- print the size of the buffer.
5558 %I -- like %i, but use k, M, G, etc., to abbreviate.
5559 %p -- print percent of buffer above top of window, or Top, Bot or All.
5560 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5561 or print Bottom or All.
5562 %n -- print Narrow if appropriate.
5563 %t -- visited file is text or binary (if OS supports this distinction).
5564 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5565 %Z -- like %z, but including the end-of-line format.
5566 %e -- print error message about full memory.
5567 %@ -- print @ or hyphen. @ means that default-directory is on a
5568 remote machine.
5569 %[ -- print one [ for each recursive editing level. %] similar.
5570 %% -- print %. %- -- print infinitely many dashes.
5571 Decimal digits after the % specify field width to which to pad. */);
5573 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5574 doc: /* *Value of `major-mode' for new buffers. */);
5576 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5577 make_number (Lisp_Symbol),
5578 doc: /* Symbol for current buffer's major mode.
5579 The default value (normally `fundamental-mode') affects new buffers.
5580 A value of nil means to use the current buffer's major mode, provided
5581 it is not marked as "special".
5583 When a mode is used by default, `find-file' switches to it before it
5584 reads the contents into the buffer and before it finishes setting up
5585 the buffer. Thus, the mode and its hooks should not expect certain
5586 variables such as `buffer-read-only' and `buffer-file-coding-system'
5587 to be set up. */);
5589 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5590 Qnil,
5591 doc: /* Pretty name of current buffer's major mode.
5592 Usually a string, but can use any of the constructs for `mode-line-format',
5593 which see.
5594 Format with `format-mode-line' to produce a string value. */);
5596 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5597 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5599 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5600 doc: /* Non-nil if Abbrev mode is enabled.
5601 Use the command `abbrev-mode' to change this variable. */);
5603 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5604 Qnil,
5605 doc: /* *Non-nil if searches and matches should ignore case. */);
5607 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5608 make_number (LISP_INT_TAG),
5609 doc: /* *Column beyond which automatic line-wrapping should happen.
5610 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5612 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5613 make_number (LISP_INT_TAG),
5614 doc: /* *Column for the default `indent-line-function' to indent to.
5615 Linefeed indents to this column in Fundamental mode. */);
5617 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5618 make_number (LISP_INT_TAG),
5619 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5621 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5622 doc: /* *Non-nil means display control chars with uparrow.
5623 A value of nil means use backslash and octal digits.
5624 This variable does not apply to characters whose display is specified
5625 in the current display table (if there is one). */);
5627 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5628 &current_buffer->enable_multibyte_characters,
5629 Qnil,
5630 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5631 Otherwise they are regarded as unibyte. This affects the display,
5632 file I/O and the behavior of various editing commands.
5634 This variable is buffer-local but you cannot set it directly;
5635 use the function `set-buffer-multibyte' to change a buffer's representation.
5636 Changing its default value with `setq-default' is supported.
5637 See also variable `default-enable-multibyte-characters' and Info node
5638 `(elisp)Text Representations'. */);
5639 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5641 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5642 &current_buffer->buffer_file_coding_system, Qnil,
5643 doc: /* Coding system to be used for encoding the buffer contents on saving.
5644 This variable applies to saving the buffer, and also to `write-region'
5645 and other functions that use `write-region'.
5646 It does not apply to sending output to subprocesses, however.
5648 If this is nil, the buffer is saved without any code conversion
5649 unless some coding system is specified in `file-coding-system-alist'
5650 for the buffer file.
5652 If the text to be saved cannot be encoded as specified by this variable,
5653 an alternative encoding is selected by `select-safe-coding-system', which see.
5655 The variable `coding-system-for-write', if non-nil, overrides this variable.
5657 This variable is never applied to a way of decoding a file while reading it. */);
5659 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5660 &current_buffer->bidi_display_reordering, Qnil,
5661 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5663 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5664 &current_buffer->bidi_paragraph_direction, Qnil,
5665 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5667 If this is nil (the default), the direction of each paragraph is
5668 determined by the first strong directional character of its text.
5669 The values of `right-to-left' and `left-to-right' override that.
5670 Any other value is treated as nil.
5672 This variable has no effect unless the buffer's value of
5673 \`bidi-display-reordering' is non-nil. */);
5675 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5676 doc: /* *Non-nil means do not display continuation lines.
5677 Instead, give each line of text just one screen line.
5679 Note that this is overridden by the variable
5680 `truncate-partial-width-windows' if that variable is non-nil
5681 and this buffer is not full-frame width. */);
5683 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5684 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5685 When word-wrapping is on, continuation lines are wrapped at the space
5686 or tab character nearest to the right window edge.
5687 If nil, continuation lines are wrapped at the right screen edge.
5689 This variable has no effect if long lines are truncated (see
5690 `truncate-lines' and `truncate-partial-width-windows'). If you use
5691 word-wrapping, you might want to reduce the value of
5692 `truncate-partial-width-windows', since wrapping can make text readable
5693 in narrower windows. */);
5695 #ifdef DOS_NT
5696 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5697 Qnil,
5698 doc: /* Non-nil if the visited file is a binary file.
5699 This variable is meaningful on MS-DOG and Windows NT.
5700 On those systems, it is automatically local in every buffer.
5701 On other systems, this variable is normally always nil. */);
5702 #endif
5704 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5705 make_number (Lisp_String),
5706 doc: /* Name of default directory of current buffer. Should end with slash.
5707 To interactively change the default directory, use command `cd'. */);
5709 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5710 Qnil,
5711 doc: /* Function called (if non-nil) to perform auto-fill.
5712 It is called after self-inserting any character specified in
5713 the `auto-fill-chars' table.
5714 NOTE: This variable is not a hook;
5715 its value may not be a list of functions. */);
5717 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5718 make_number (Lisp_String),
5719 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5721 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5722 make_number (Lisp_String),
5723 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5724 The truename of a file is calculated by `file-truename'
5725 and then abbreviated with `abbreviate-file-name'. */);
5727 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5728 &current_buffer->auto_save_file_name,
5729 make_number (Lisp_String),
5730 doc: /* Name of file for auto-saving current buffer.
5731 If it is nil, that means don't auto-save this buffer. */);
5733 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5734 doc: /* Non-nil if this buffer is read-only. */);
5736 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5737 doc: /* Non-nil if this buffer's file has been backed up.
5738 Backing up is done before the first time the file is saved. */);
5740 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5741 make_number (LISP_INT_TAG),
5742 doc: /* Length of current buffer when last read in, saved or auto-saved.
5743 0 initially.
5744 -1 means auto-saving turned off until next real save.
5746 If you set this to -2, that means don't turn off auto-saving in this buffer
5747 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5748 you probably should set this to -2 in that buffer. */);
5750 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5751 Qnil,
5752 doc: /* Non-nil enables selective display.
5753 An integer N as value means display only lines
5754 that start with less than N columns of space.
5755 A value of t means that the character ^M makes itself and
5756 all the rest of the line invisible; also, when saving the buffer
5757 in a file, save the ^M as a newline. */);
5759 #ifndef old
5760 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5761 &current_buffer->selective_display_ellipses,
5762 Qnil,
5763 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5764 #endif
5766 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5767 doc: /* Non-nil if self-insertion should replace existing text.
5768 The value should be one of `overwrite-mode-textual',
5769 `overwrite-mode-binary', or nil.
5770 If it is `overwrite-mode-textual', self-insertion still
5771 inserts at the end of a line, and inserts when point is before a tab,
5772 until the tab is filled in.
5773 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5775 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5776 Qnil,
5777 doc: /* Display table that controls display of the contents of current buffer.
5779 If this variable is nil, the value of `standard-display-table' is used.
5780 Each window can have its own, overriding display table, see
5781 `set-window-display-table' and `window-display-table'.
5783 The display table is a char-table created with `make-display-table'.
5784 A char-table is an array indexed by character codes. Normal array
5785 primitives `aref' and `aset' can be used to access elements of a char-table.
5787 Each of the char-table elements control how to display the corresponding
5788 text character: the element at index C in the table says how to display
5789 the character whose code is C. Each element should be a vector of
5790 characters or nil. The value nil means display the character in the
5791 default fashion; otherwise, the characters from the vector are delivered
5792 to the screen instead of the original character.
5794 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5795 to display a capital Y instead of each X character.
5797 In addition, a char-table has six extra slots to control the display of:
5799 the end of a truncated screen line (extra-slot 0, a single character);
5800 the end of a continued line (extra-slot 1, a single character);
5801 the escape character used to display character codes in octal
5802 (extra-slot 2, a single character);
5803 the character used as an arrow for control characters (extra-slot 3,
5804 a single character);
5805 the decoration indicating the presence of invisible lines (extra-slot 4,
5806 a vector of characters);
5807 the character used to draw the border between side-by-side windows
5808 (extra-slot 5, a single character).
5810 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5812 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5813 Qnil,
5814 doc: /* *Width of left marginal area for display of a buffer.
5815 A value of nil means no marginal area. */);
5817 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5818 Qnil,
5819 doc: /* *Width of right marginal area for display of a buffer.
5820 A value of nil means no marginal area. */);
5822 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5823 Qnil,
5824 doc: /* *Width of this buffer's left fringe (in pixels).
5825 A value of 0 means no left fringe is shown in this buffer's window.
5826 A value of nil means to use the left fringe width from the window's frame. */);
5828 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5829 Qnil,
5830 doc: /* *Width of this buffer's right fringe (in pixels).
5831 A value of 0 means no right fringe is shown in this buffer's window.
5832 A value of nil means to use the right fringe width from the window's frame. */);
5834 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5835 Qnil,
5836 doc: /* *Non-nil means to display fringes outside display margins.
5837 A value of nil means to display fringes between margins and buffer text. */);
5839 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5840 Qnil,
5841 doc: /* *Width of this buffer's scroll bars in pixels.
5842 A value of nil means to use the scroll bar width from the window's frame. */);
5844 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5845 Qnil,
5846 doc: /* *Position of this buffer's vertical scroll bar.
5847 The value takes effect whenever you tell a window to display this buffer;
5848 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5850 A value of `left' or `right' means put the vertical scroll bar at that side
5851 of the window; a value of nil means don't show any vertical scroll bars.
5852 A value of t (the default) means do whatever the window's frame specifies. */);
5854 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5855 &current_buffer->indicate_empty_lines, Qnil,
5856 doc: /* *Visually indicate empty lines after the buffer end.
5857 If non-nil, a bitmap is displayed in the left fringe of a window on
5858 window-systems. */);
5860 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5861 &current_buffer->indicate_buffer_boundaries, Qnil,
5862 doc: /* *Visually indicate buffer boundaries and scrolling.
5863 If non-nil, the first and last line of the buffer are marked in the fringe
5864 of a window on window-systems with angle bitmaps, or if the window can be
5865 scrolled, the top and bottom line of the window are marked with up and down
5866 arrow bitmaps.
5868 If value is a symbol `left' or `right', both angle and arrow bitmaps
5869 are displayed in the left or right fringe, resp. Any other value
5870 that doesn't look like an alist means display the angle bitmaps in
5871 the left fringe but no arrows.
5873 You can exercise more precise control by using an alist as the
5874 value. Each alist element (INDICATOR . POSITION) specifies
5875 where to show one of the indicators. INDICATOR is one of `top',
5876 `bottom', `up', `down', or t, which specifies the default position,
5877 and POSITION is one of `left', `right', or nil, meaning do not show
5878 this indicator.
5880 For example, ((top . left) (t . right)) places the top angle bitmap in
5881 left fringe, the bottom angle bitmap in right fringe, and both arrow
5882 bitmaps in right fringe. To show just the angle bitmaps in the left
5883 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5885 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5886 &current_buffer->fringe_indicator_alist, Qnil,
5887 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5888 The value is an alist where each element (INDICATOR . BITMAPS)
5889 specifies the fringe bitmaps used to display a specific logical
5890 fringe indicator.
5892 INDICATOR specifies the logical indicator type which is one of the
5893 following symbols: `truncation' , `continuation', `overlay-arrow',
5894 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5896 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5897 the actual bitmap shown in the left or right fringe for the logical
5898 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5899 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5900 are used only for the `bottom' and `top-bottom' indicators when the
5901 last (only) line has no final newline. BITMAPS may also be a single
5902 symbol which is used in both left and right fringes. */);
5904 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5905 &current_buffer->fringe_cursor_alist, Qnil,
5906 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5907 The value is an alist where each element (CURSOR . BITMAP)
5908 specifies the fringe bitmaps used to display a specific logical
5909 cursor type in the fringe.
5911 CURSOR specifies the logical cursor type which is one of the following
5912 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5913 one is used to show a hollow cursor on narrow lines display lines
5914 where the normal hollow cursor will not fit.
5916 BITMAP is the corresponding fringe bitmap shown for the logical
5917 cursor type. */);
5919 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5920 &current_buffer->scroll_up_aggressively, Qnil,
5921 doc: /* How far to scroll windows upward.
5922 If you move point off the bottom, the window scrolls automatically.
5923 This variable controls how far it scrolls. The value nil, the default,
5924 means scroll to center point. A fraction means scroll to put point
5925 that fraction of the window's height from the bottom of the window.
5926 When the value is 0.0, point goes at the bottom line, which in the
5927 simple case that you moved off with C-f means scrolling just one line.
5928 1.0 means point goes at the top, so that in that simple case, the
5929 window scrolls by a full window height. Meaningful values are
5930 between 0.0 and 1.0, inclusive. */);
5932 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5933 &current_buffer->scroll_down_aggressively, Qnil,
5934 doc: /* How far to scroll windows downward.
5935 If you move point off the top, the window scrolls automatically.
5936 This variable controls how far it scrolls. The value nil, the default,
5937 means scroll to center point. A fraction means scroll to put point
5938 that fraction of the window's height from the top of the window.
5939 When the value is 0.0, point goes at the top line, which in the
5940 simple case that you moved off with C-b means scrolling just one line.
5941 1.0 means point goes at the bottom, so that in that simple case, the
5942 window scrolls by a full window height. Meaningful values are
5943 between 0.0 and 1.0, inclusive. */);
5945 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5946 "Don't ask.");
5949 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5950 doc: /* List of functions to call before each text change.
5951 Two arguments are passed to each function: the positions of
5952 the beginning and end of the range of old text to be changed.
5953 \(For an insertion, the beginning and end are at the same place.)
5954 No information is given about the length of the text after the change.
5956 Buffer changes made while executing the `before-change-functions'
5957 don't call any before-change or after-change functions.
5958 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5960 If an unhandled error happens in running these functions,
5961 the variable's value remains nil. That prevents the error
5962 from happening repeatedly and making Emacs nonfunctional. */);
5963 Vbefore_change_functions = Qnil;
5965 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5966 doc: /* List of functions to call after each text change.
5967 Three arguments are passed to each function: the positions of
5968 the beginning and end of the range of changed text,
5969 and the length in bytes of the pre-change text replaced by that range.
5970 \(For an insertion, the pre-change length is zero;
5971 for a deletion, that length is the number of bytes deleted,
5972 and the post-change beginning and end are at the same place.)
5974 Buffer changes made while executing the `after-change-functions'
5975 don't call any before-change or after-change functions.
5976 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5978 If an unhandled error happens in running these functions,
5979 the variable's value remains nil. That prevents the error
5980 from happening repeatedly and making Emacs nonfunctional. */);
5981 Vafter_change_functions = Qnil;
5983 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5984 doc: /* A list of functions to call before changing a buffer which is unmodified.
5985 The functions are run using the `run-hooks' function. */);
5986 Vfirst_change_hook = Qnil;
5988 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5989 doc: /* List of undo entries in current buffer.
5990 Recent changes come first; older changes follow newer.
5992 An entry (BEG . END) represents an insertion which begins at
5993 position BEG and ends at position END.
5995 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5996 from (abs POSITION). If POSITION is positive, point was at the front
5997 of the text being deleted; if negative, point was at the end.
5999 An entry (t HIGH . LOW) indicates that the buffer previously had
6000 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6001 of the visited file's modification time, as of that time. If the
6002 modification time of the most recent save is different, this entry is
6003 obsolete.
6005 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6006 was modified between BEG and END. PROPERTY is the property name,
6007 and VALUE is the old value.
6009 An entry (apply FUN-NAME . ARGS) means undo the change with
6010 \(apply FUN-NAME ARGS).
6012 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6013 in the active region. BEG and END is the range affected by this entry
6014 and DELTA is the number of bytes added or deleted in that range by
6015 this change.
6017 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6018 was adjusted in position by the offset DISTANCE (an integer).
6020 An entry of the form POSITION indicates that point was at the buffer
6021 location given by the integer. Undoing an entry of this form places
6022 point at POSITION.
6024 Entries with value `nil' mark undo boundaries. The undo command treats
6025 the changes between two undo boundaries as a single step to be undone.
6027 If the value of the variable is t, undo information is not recorded. */);
6029 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6030 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6032 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6033 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6035 Normally, the line-motion functions work by scanning the buffer for
6036 newlines. Columnar operations (like `move-to-column' and
6037 `compute-motion') also work by scanning the buffer, summing character
6038 widths as they go. This works well for ordinary text, but if the
6039 buffer's lines are very long (say, more than 500 characters), these
6040 motion functions will take longer to execute. Emacs may also take
6041 longer to update the display.
6043 If `cache-long-line-scans' is non-nil, these motion functions cache the
6044 results of their scans, and consult the cache to avoid rescanning
6045 regions of the buffer until the text is modified. The caches are most
6046 beneficial when they prevent the most searching---that is, when the
6047 buffer contains long lines and large regions of characters with the
6048 same, fixed screen width.
6050 When `cache-long-line-scans' is non-nil, processing short lines will
6051 become slightly slower (because of the overhead of consulting the
6052 cache), and the caches will use memory roughly proportional to the
6053 number of newlines and characters whose screen width varies.
6055 The caches require no explicit maintenance; their accuracy is
6056 maintained internally by the Emacs primitives. Enabling or disabling
6057 the cache should not affect the behavior of any of the motion
6058 functions; it should only affect their performance. */);
6060 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6061 doc: /* Value of point before the last series of scroll operations, or nil. */);
6063 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6064 doc: /* List of formats to use when saving this buffer.
6065 Formats are defined by `format-alist'. This variable is
6066 set when a file is visited. */);
6068 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6069 &current_buffer->auto_save_file_format, Qnil,
6070 doc: /* *Format in which to write auto-save files.
6071 Should be a list of symbols naming formats that are defined in `format-alist'.
6072 If it is t, which is the default, auto-save files are written in the
6073 same format as a regular save would use. */);
6075 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6076 &current_buffer->invisibility_spec, Qnil,
6077 doc: /* Invisibility spec of this buffer.
6078 The default is t, which means that text is invisible
6079 if it has a non-nil `invisible' property.
6080 If the value is a list, a text character is invisible if its `invisible'
6081 property is an element in that list (or is a list with members in common).
6082 If an element is a cons cell of the form (PROP . ELLIPSIS),
6083 then characters with property value PROP are invisible,
6084 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6086 DEFVAR_PER_BUFFER ("buffer-display-count",
6087 &current_buffer->display_count, Qnil,
6088 doc: /* A number incremented each time this buffer is displayed in a window.
6089 The function `set-window-buffer' increments it. */);
6091 DEFVAR_PER_BUFFER ("buffer-display-time",
6092 &current_buffer->display_time, Qnil,
6093 doc: /* Time stamp updated each time this buffer is displayed in a window.
6094 The function `set-window-buffer' updates this variable
6095 to the value obtained by calling `current-time'.
6096 If the buffer has never been shown in a window, the value is nil. */);
6098 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6099 doc: /* Non-nil if Transient Mark mode is enabled.
6100 See the command `transient-mark-mode' for a description of this minor mode.
6102 Non-nil also enables highlighting of the region whenever the mark is active.
6103 The variable `highlight-nonselected-windows' controls whether to highlight
6104 all windows or just the selected window.
6106 Lisp programs may give this variable certain special values:
6108 - A value of `lambda' enables Transient Mark mode temporarily.
6109 It is disabled again after any subsequent action that would
6110 normally deactivate the mark (e.g. buffer modification).
6112 - A value of (only . OLDVAL) enables Transient Mark mode
6113 temporarily. After any subsequent point motion command that is
6114 not shift-translated, or any other action that would normally
6115 deactivate the mark (e.g. buffer modification), the value of
6116 `transient-mark-mode' is set to OLDVAL. */);
6117 Vtransient_mark_mode = Qnil;
6119 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6120 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6121 If the value is t, disregard `buffer-read-only' and all `read-only'
6122 text properties. If the value is a list, disregard `buffer-read-only'
6123 and disregard a `read-only' text property if the property value
6124 is a member of the list. */);
6125 Vinhibit_read_only = Qnil;
6127 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6128 doc: /* Cursor to use when this buffer is in the selected window.
6129 Values are interpreted as follows:
6131 t use the cursor specified for the frame
6132 nil don't display a cursor
6133 box display a filled box cursor
6134 hollow display a hollow box cursor
6135 bar display a vertical bar cursor with default width
6136 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6137 hbar display a horizontal bar cursor with default height
6138 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6139 ANYTHING ELSE display a hollow box cursor
6141 When the buffer is displayed in a non-selected window, the
6142 cursor's appearance is instead controlled by the variable
6143 `cursor-in-non-selected-windows'. */);
6145 DEFVAR_PER_BUFFER ("line-spacing",
6146 &current_buffer->extra_line_spacing, Qnil,
6147 doc: /* Additional space to put between lines when displaying a buffer.
6148 The space is measured in pixels, and put below lines on graphic displays,
6149 see `display-graphic-p'.
6150 If value is a floating point number, it specifies the spacing relative
6151 to the default frame line height. A value of nil means add no extra space. */);
6153 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6154 &current_buffer->cursor_in_non_selected_windows, Qnil,
6155 doc: /* *Non-nil means show a cursor in non-selected windows.
6156 If nil, only shows a cursor in the selected window.
6157 If t, displays a cursor related to the usual cursor type
6158 \(a solid box becomes hollow, a bar becomes a narrower bar).
6159 You can also specify the cursor type as in the `cursor-type' variable.
6160 Use Custom to set this variable and update the display." */);
6162 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6163 doc: /* List of functions called with no args to query before killing a buffer.
6164 The buffer being killed will be current while the functions are running.
6165 If any of them returns nil, the buffer is not killed. */);
6166 Vkill_buffer_query_functions = Qnil;
6168 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6169 doc: /* Normal hook run before changing the major mode of a buffer.
6170 The function `kill-all-local-variables' runs this before doing anything else. */);
6171 Vchange_major_mode_hook = Qnil;
6172 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6173 staticpro (&Qchange_major_mode_hook);
6175 defsubr (&Sbuffer_live_p);
6176 defsubr (&Sbuffer_list);
6177 defsubr (&Sget_buffer);
6178 defsubr (&Sget_file_buffer);
6179 defsubr (&Sget_buffer_create);
6180 defsubr (&Smake_indirect_buffer);
6181 defsubr (&Sgenerate_new_buffer_name);
6182 defsubr (&Sbuffer_name);
6183 /*defsubr (&Sbuffer_number);*/
6184 defsubr (&Sbuffer_file_name);
6185 defsubr (&Sbuffer_base_buffer);
6186 defsubr (&Sbuffer_local_value);
6187 defsubr (&Sbuffer_local_variables);
6188 defsubr (&Sbuffer_modified_p);
6189 defsubr (&Sset_buffer_modified_p);
6190 defsubr (&Sbuffer_modified_tick);
6191 defsubr (&Sbuffer_chars_modified_tick);
6192 defsubr (&Srename_buffer);
6193 defsubr (&Sother_buffer);
6194 defsubr (&Sbuffer_enable_undo);
6195 defsubr (&Skill_buffer);
6196 defsubr (&Sset_buffer_major_mode);
6197 defsubr (&Sswitch_to_buffer);
6198 defsubr (&Scurrent_buffer);
6199 defsubr (&Sset_buffer);
6200 defsubr (&Sbarf_if_buffer_read_only);
6201 defsubr (&Sbury_buffer);
6202 defsubr (&Serase_buffer);
6203 defsubr (&Sbuffer_swap_text);
6204 defsubr (&Sset_buffer_multibyte);
6205 defsubr (&Skill_all_local_variables);
6207 defsubr (&Soverlayp);
6208 defsubr (&Smake_overlay);
6209 defsubr (&Sdelete_overlay);
6210 defsubr (&Smove_overlay);
6211 defsubr (&Soverlay_start);
6212 defsubr (&Soverlay_end);
6213 defsubr (&Soverlay_buffer);
6214 defsubr (&Soverlay_properties);
6215 defsubr (&Soverlays_at);
6216 defsubr (&Soverlays_in);
6217 defsubr (&Snext_overlay_change);
6218 defsubr (&Sprevious_overlay_change);
6219 defsubr (&Soverlay_recenter);
6220 defsubr (&Soverlay_lists);
6221 defsubr (&Soverlay_get);
6222 defsubr (&Soverlay_put);
6223 defsubr (&Srestore_buffer_modified_p);
6226 void
6227 keys_of_buffer (void)
6229 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6230 initial_define_key (control_x_map, 'k', "kill-buffer");
6232 /* This must not be in syms_of_buffer, because Qdisabled is not
6233 initialized when that function gets called. */
6234 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);