Merge from trunk.
[emacs.git] / src / buffer.c
blobea76cfd721371cefed7a3ead9c1794db64d56c0f
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 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 EXFUN (Fset_buffer, 1);
109 void set_buffer_internal (struct buffer *b);
110 void set_buffer_internal_1 (struct buffer *b);
111 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
112 int after, Lisp_Object arg1,
113 Lisp_Object arg2, Lisp_Object arg3);
114 static void swap_out_buffer_local_variables (struct buffer *b);
115 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
117 /* Alist of all buffer names vs the buffers. */
118 /* This used to be a variable, but is no longer,
119 to prevent lossage due to user rplac'ing this alist or its elements. */
120 Lisp_Object Vbuffer_alist;
122 /* Functions to call before and after each text change. */
123 Lisp_Object Vbefore_change_functions;
124 Lisp_Object Vafter_change_functions;
126 Lisp_Object Vtransient_mark_mode;
128 /* t means ignore all read-only text properties.
129 A list means ignore such a property if its value is a member of the list.
130 Any non-nil value means ignore buffer-read-only. */
131 Lisp_Object Vinhibit_read_only;
133 /* List of functions to call that can query about killing a buffer.
134 If any of these functions returns nil, we don't kill it. */
135 Lisp_Object Vkill_buffer_query_functions;
136 Lisp_Object Qkill_buffer_query_functions;
138 /* Hook run before changing a major mode. */
139 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
141 /* List of functions to call before changing an unmodified buffer. */
142 Lisp_Object Vfirst_change_hook;
144 Lisp_Object Qfirst_change_hook;
145 Lisp_Object Qbefore_change_functions;
146 Lisp_Object Qafter_change_functions;
147 Lisp_Object Qucs_set_table_for_input;
149 /* If nonzero, all modification hooks are suppressed. */
150 int inhibit_modification_hooks;
152 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
153 Lisp_Object Qpermanent_local_hook;
155 Lisp_Object Qprotected_field;
157 Lisp_Object QSFundamental; /* A string "Fundamental" */
159 Lisp_Object Qkill_buffer_hook;
161 Lisp_Object Qget_file_buffer;
163 Lisp_Object Qoverlayp;
165 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
167 Lisp_Object Qmodification_hooks;
168 Lisp_Object Qinsert_in_front_hooks;
169 Lisp_Object Qinsert_behind_hooks;
171 static void alloc_buffer_text (struct buffer *, size_t);
172 static void free_buffer_text (struct buffer *b);
173 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
174 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
175 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
177 /* For debugging; temporary. See set_buffer_internal. */
178 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
180 void
181 nsberror (Lisp_Object spec)
183 if (STRINGP (spec))
184 error ("No buffer named %s", SDATA (spec));
185 error ("Invalid buffer argument");
188 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
189 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
190 Value is nil if OBJECT is not a buffer or if it has been killed. */)
191 (Lisp_Object object)
193 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
194 ? Qt : Qnil);
197 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
198 doc: /* Return a list of all existing live buffers.
199 If the optional arg FRAME is a frame, we return the buffer list
200 in the proper order for that frame: the buffers in FRAME's `buffer-list'
201 frame parameter come first, followed by the rest of the buffers. */)
202 (Lisp_Object frame)
204 Lisp_Object general;
205 general = Fmapcar (Qcdr, Vbuffer_alist);
207 if (FRAMEP (frame))
209 Lisp_Object framelist, prevlist, tail;
210 Lisp_Object args[3];
212 CHECK_FRAME (frame);
214 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
215 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
217 /* Remove from GENERAL any buffer that duplicates one in
218 FRAMELIST or PREVLIST. */
219 tail = framelist;
220 while (CONSP (tail))
222 general = Fdelq (XCAR (tail), general);
223 tail = XCDR (tail);
225 tail = prevlist;
226 while (CONSP (tail))
228 general = Fdelq (XCAR (tail), general);
229 tail = XCDR (tail);
232 args[0] = framelist;
233 args[1] = general;
234 args[2] = prevlist;
235 return Fnconc (3, args);
238 return general;
241 /* Like Fassoc, but use Fstring_equal to compare
242 (which ignores text properties),
243 and don't ever QUIT. */
245 static Lisp_Object
246 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
248 register Lisp_Object tail;
249 for (tail = list; CONSP (tail); tail = XCDR (tail))
251 register Lisp_Object elt, tem;
252 elt = XCAR (tail);
253 tem = Fstring_equal (Fcar (elt), key);
254 if (!NILP (tem))
255 return elt;
257 return Qnil;
260 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
261 doc: /* Return the buffer named BUFFER-OR-NAME.
262 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
263 is a string and there is no buffer with that name, return nil. If
264 BUFFER-OR-NAME is a buffer, return it as given. */)
265 (register Lisp_Object buffer_or_name)
267 if (BUFFERP (buffer_or_name))
268 return buffer_or_name;
269 CHECK_STRING (buffer_or_name);
271 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
274 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
275 doc: /* Return the buffer visiting file FILENAME (a string).
276 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
277 If there is no such live buffer, return nil.
278 See also `find-buffer-visiting'. */)
279 (register Lisp_Object filename)
281 register Lisp_Object tail, buf, tem;
282 Lisp_Object handler;
284 CHECK_STRING (filename);
285 filename = Fexpand_file_name (filename, Qnil);
287 /* If the file name has special constructs in it,
288 call the corresponding file handler. */
289 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
290 if (!NILP (handler))
291 return call2 (handler, Qget_file_buffer, filename);
293 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
295 buf = Fcdr (XCAR (tail));
296 if (!BUFFERP (buf)) continue;
297 if (!STRINGP (XBUFFER (buf)->filename)) continue;
298 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
299 if (!NILP (tem))
300 return buf;
302 return Qnil;
305 Lisp_Object
306 get_truename_buffer (register Lisp_Object filename)
308 register Lisp_Object tail, buf, tem;
310 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
312 buf = Fcdr (XCAR (tail));
313 if (!BUFFERP (buf)) continue;
314 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
315 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
316 if (!NILP (tem))
317 return buf;
319 return Qnil;
322 /* Incremented for each buffer created, to assign the buffer number. */
323 int buffer_count;
325 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
326 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
327 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
328 return that buffer. If no such buffer exists, create a new buffer with
329 that name and return it. If BUFFER-OR-NAME starts with a space, the new
330 buffer does not keep undo information.
332 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
333 even if it is dead. The return value is never nil. */)
334 (register Lisp_Object buffer_or_name)
336 register Lisp_Object buffer, name;
337 register struct buffer *b;
339 buffer = Fget_buffer (buffer_or_name);
340 if (!NILP (buffer))
341 return buffer;
343 if (SCHARS (buffer_or_name) == 0)
344 error ("Empty string for buffer name is not allowed");
346 b = allocate_buffer ();
348 /* An ordinary buffer uses its own struct buffer_text. */
349 b->text = &b->own_text;
350 b->base_buffer = 0;
352 BUF_GAP_SIZE (b) = 20;
353 BLOCK_INPUT;
354 /* We allocate extra 1-byte at the tail and keep it always '\0' for
355 anchoring a search. */
356 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
357 UNBLOCK_INPUT;
358 if (! BUF_BEG_ADDR (b))
359 buffer_memory_full ();
361 BUF_PT (b) = BEG;
362 BUF_GPT (b) = BEG;
363 BUF_BEGV (b) = BEG;
364 BUF_ZV (b) = BEG;
365 BUF_Z (b) = BEG;
366 BUF_PT_BYTE (b) = BEG_BYTE;
367 BUF_GPT_BYTE (b) = BEG_BYTE;
368 BUF_BEGV_BYTE (b) = BEG_BYTE;
369 BUF_ZV_BYTE (b) = BEG_BYTE;
370 BUF_Z_BYTE (b) = BEG_BYTE;
371 BUF_MODIFF (b) = 1;
372 BUF_CHARS_MODIFF (b) = 1;
373 BUF_OVERLAY_MODIFF (b) = 1;
374 BUF_SAVE_MODIFF (b) = 1;
375 BUF_INTERVALS (b) = 0;
376 BUF_UNCHANGED_MODIFIED (b) = 1;
377 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
378 BUF_END_UNCHANGED (b) = 0;
379 BUF_BEG_UNCHANGED (b) = 0;
380 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
382 b->newline_cache = 0;
383 b->width_run_cache = 0;
384 b->width_table = Qnil;
385 b->prevent_redisplay_optimizations_p = 1;
387 /* Put this on the chain of all buffers including killed ones. */
388 b->next = all_buffers;
389 all_buffers = b;
391 /* An ordinary buffer normally doesn't need markers
392 to handle BEGV and ZV. */
393 b->pt_marker = Qnil;
394 b->begv_marker = Qnil;
395 b->zv_marker = Qnil;
397 name = Fcopy_sequence (buffer_or_name);
398 STRING_SET_INTERVALS (name, NULL_INTERVAL);
399 b->name = name;
401 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
403 reset_buffer (b);
404 reset_buffer_local_variables (b, 1);
406 b->mark = Fmake_marker ();
407 BUF_MARKERS (b) = NULL;
408 b->name = name;
410 /* Put this in the alist of all live buffers. */
411 XSETBUFFER (buffer, b);
412 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
414 /* An error in calling the function here (should someone redefine it)
415 can lead to infinite regress until you run out of stack. rms
416 says that's not worth protecting against. */
417 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
418 /* buffer is on buffer-alist, so no gcpro. */
419 call1 (Qucs_set_table_for_input, buffer);
421 return buffer;
425 /* Return a list of overlays which is a copy of the overlay list
426 LIST, but for buffer B. */
428 static struct Lisp_Overlay *
429 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
431 Lisp_Object buffer;
432 struct Lisp_Overlay *result = NULL, *tail = NULL;
434 XSETBUFFER (buffer, b);
436 for (; list; list = list->next)
438 Lisp_Object overlay, start, end, old_overlay;
439 EMACS_INT charpos;
441 XSETMISC (old_overlay, list);
442 charpos = marker_position (OVERLAY_START (old_overlay));
443 start = Fmake_marker ();
444 Fset_marker (start, make_number (charpos), buffer);
445 XMARKER (start)->insertion_type
446 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
448 charpos = marker_position (OVERLAY_END (old_overlay));
449 end = Fmake_marker ();
450 Fset_marker (end, make_number (charpos), buffer);
451 XMARKER (end)->insertion_type
452 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
454 overlay = allocate_misc ();
455 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
456 OVERLAY_START (overlay) = start;
457 OVERLAY_END (overlay) = end;
458 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
459 XOVERLAY (overlay)->next = NULL;
461 if (tail)
462 tail = tail->next = XOVERLAY (overlay);
463 else
464 result = tail = XOVERLAY (overlay);
467 return result;
471 /* Clone per-buffer values of buffer FROM.
473 Buffer TO gets the same per-buffer values as FROM, with the
474 following exceptions: (1) TO's name is left untouched, (2) markers
475 are copied and made to refer to TO, and (3) overlay lists are
476 copied. */
478 static void
479 clone_per_buffer_values (struct buffer *from, struct buffer *to)
481 Lisp_Object to_buffer;
482 int offset;
484 XSETBUFFER (to_buffer, to);
486 /* buffer-local Lisp variables start at `undo_list',
487 tho only the ones from `name' on are GC'd normally. */
488 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
489 offset < sizeof *to;
490 offset += sizeof (Lisp_Object))
492 Lisp_Object obj;
494 /* Don't touch the `name' which should be unique for every buffer. */
495 if (offset == PER_BUFFER_VAR_OFFSET (name))
496 continue;
498 obj = PER_BUFFER_VALUE (from, offset);
499 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
501 struct Lisp_Marker *m = XMARKER (obj);
502 obj = Fmake_marker ();
503 XMARKER (obj)->insertion_type = m->insertion_type;
504 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
507 PER_BUFFER_VALUE (to, offset) = obj;
510 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
512 to->overlays_before = copy_overlays (to, from->overlays_before);
513 to->overlays_after = copy_overlays (to, from->overlays_after);
515 /* Get (a copy of) the alist of Lisp-level local variables of FROM
516 and install that in TO. */
517 to->local_var_alist = buffer_lisp_local_variables (from);
520 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
521 2, 3,
522 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
523 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
524 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
525 NAME should be a string which is not the name of an existing buffer.
526 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
527 such as major and minor modes, in the indirect buffer.
528 CLONE nil means the indirect buffer's state is reset to default values. */)
529 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
531 Lisp_Object buf, tem;
532 struct buffer *b;
534 CHECK_STRING (name);
535 buf = Fget_buffer (name);
536 if (!NILP (buf))
537 error ("Buffer name `%s' is in use", SDATA (name));
539 tem = base_buffer;
540 base_buffer = Fget_buffer (base_buffer);
541 if (NILP (base_buffer))
542 error ("No such buffer: `%s'", SDATA (tem));
543 if (NILP (XBUFFER (base_buffer)->name))
544 error ("Base buffer has been killed");
546 if (SCHARS (name) == 0)
547 error ("Empty string for buffer name is not allowed");
549 b = allocate_buffer ();
551 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
552 ? XBUFFER (base_buffer)->base_buffer
553 : XBUFFER (base_buffer));
555 /* Use the base buffer's text object. */
556 b->text = b->base_buffer->text;
558 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
559 BUF_ZV (b) = BUF_ZV (b->base_buffer);
560 BUF_PT (b) = BUF_PT (b->base_buffer);
561 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
562 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
563 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
565 b->newline_cache = 0;
566 b->width_run_cache = 0;
567 b->width_table = Qnil;
569 /* Put this on the chain of all buffers including killed ones. */
570 b->next = all_buffers;
571 all_buffers = b;
573 name = Fcopy_sequence (name);
574 STRING_SET_INTERVALS (name, NULL_INTERVAL);
575 b->name = name;
577 reset_buffer (b);
578 reset_buffer_local_variables (b, 1);
580 /* Put this in the alist of all live buffers. */
581 XSETBUFFER (buf, b);
582 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
584 b->mark = Fmake_marker ();
585 b->name = name;
587 /* The multibyte status belongs to the base buffer. */
588 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
590 /* Make sure the base buffer has markers for its narrowing. */
591 if (NILP (b->base_buffer->pt_marker))
593 b->base_buffer->pt_marker = Fmake_marker ();
594 set_marker_both (b->base_buffer->pt_marker, base_buffer,
595 BUF_PT (b->base_buffer),
596 BUF_PT_BYTE (b->base_buffer));
598 if (NILP (b->base_buffer->begv_marker))
600 b->base_buffer->begv_marker = Fmake_marker ();
601 set_marker_both (b->base_buffer->begv_marker, base_buffer,
602 BUF_BEGV (b->base_buffer),
603 BUF_BEGV_BYTE (b->base_buffer));
605 if (NILP (b->base_buffer->zv_marker))
607 b->base_buffer->zv_marker = Fmake_marker ();
608 set_marker_both (b->base_buffer->zv_marker, base_buffer,
609 BUF_ZV (b->base_buffer),
610 BUF_ZV_BYTE (b->base_buffer));
611 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
614 if (NILP (clone))
616 /* Give the indirect buffer markers for its narrowing. */
617 b->pt_marker = Fmake_marker ();
618 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
619 b->begv_marker = Fmake_marker ();
620 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
621 b->zv_marker = Fmake_marker ();
622 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
623 XMARKER (b->zv_marker)->insertion_type = 1;
625 else
627 struct buffer *old_b = current_buffer;
629 clone_per_buffer_values (b->base_buffer, b);
630 b->filename = Qnil;
631 b->file_truename = Qnil;
632 b->display_count = make_number (0);
633 b->backed_up = Qnil;
634 b->auto_save_file_name = Qnil;
635 set_buffer_internal_1 (b);
636 Fset (intern ("buffer-save-without-query"), Qnil);
637 Fset (intern ("buffer-file-number"), Qnil);
638 Fset (intern ("buffer-stale-function"), Qnil);
639 set_buffer_internal_1 (old_b);
642 return buf;
645 void
646 delete_all_overlays (struct buffer *b)
648 Lisp_Object overlay;
650 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
651 have to empty the list, otherwise we end up with overlays that
652 think they belong to this buffer while the buffer doesn't know about
653 them any more. */
654 while (b->overlays_before)
656 XSETMISC (overlay, b->overlays_before);
657 Fdelete_overlay (overlay);
659 while (b->overlays_after)
661 XSETMISC (overlay, b->overlays_after);
662 Fdelete_overlay (overlay);
664 eassert (b->overlays_before == NULL);
665 eassert (b->overlays_after == NULL);
668 /* Reinitialize everything about a buffer except its name and contents
669 and local variables.
670 If called on an already-initialized buffer, the list of overlays
671 should be deleted before calling this function, otherwise we end up
672 with overlays that claim to belong to the buffer but the buffer
673 claims it doesn't belong to it. */
675 void
676 reset_buffer (register struct buffer *b)
678 b->filename = Qnil;
679 b->file_truename = Qnil;
680 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
681 b->modtime = 0;
682 b->modtime_size = -1;
683 XSETFASTINT (b->save_length, 0);
684 b->last_window_start = 1;
685 /* It is more conservative to start out "changed" than "unchanged". */
686 b->clip_changed = 0;
687 b->prevent_redisplay_optimizations_p = 1;
688 b->backed_up = Qnil;
689 BUF_AUTOSAVE_MODIFF (b) = 0;
690 b->auto_save_failure_time = -1;
691 b->auto_save_file_name = Qnil;
692 b->read_only = Qnil;
693 b->overlays_before = NULL;
694 b->overlays_after = NULL;
695 b->overlay_center = BEG;
696 b->mark_active = Qnil;
697 b->point_before_scroll = Qnil;
698 b->file_format = Qnil;
699 b->auto_save_file_format = Qt;
700 b->last_selected_window = Qnil;
701 XSETINT (b->display_count, 0);
702 b->display_time = Qnil;
703 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
704 b->cursor_type = buffer_defaults.cursor_type;
705 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
707 b->display_error_modiff = 0;
710 /* Reset buffer B's local variables info.
711 Don't use this on a buffer that has already been in use;
712 it does not treat permanent locals consistently.
713 Instead, use Fkill_all_local_variables.
715 If PERMANENT_TOO is 1, then we reset permanent
716 buffer-local variables. If PERMANENT_TOO is 0,
717 we preserve those. */
719 static void
720 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
722 register int offset;
723 int i;
725 /* Reset the major mode to Fundamental, together with all the
726 things that depend on the major mode.
727 default-major-mode is handled at a higher level.
728 We ignore it here. */
729 b->major_mode = Qfundamental_mode;
730 b->keymap = Qnil;
731 b->mode_name = QSFundamental;
732 b->minor_modes = Qnil;
734 /* If the standard case table has been altered and invalidated,
735 fix up its insides first. */
736 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
737 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
738 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
739 Fset_standard_case_table (Vascii_downcase_table);
741 b->downcase_table = Vascii_downcase_table;
742 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
743 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
744 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
745 b->invisibility_spec = Qt;
746 #ifndef DOS_NT
747 b->buffer_file_type = Qnil;
748 #endif
750 /* Reset all (or most) per-buffer variables to their defaults. */
751 if (permanent_too)
752 b->local_var_alist = Qnil;
753 else
755 Lisp_Object tmp, prop, last = Qnil;
756 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
757 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
759 /* If permanent-local, keep it. */
760 last = tmp;
761 if (EQ (prop, Qpermanent_local_hook))
763 /* This is a partially permanent hook variable.
764 Preserve only the elements that want to be preserved. */
765 Lisp_Object list, newlist;
766 list = XCDR (XCAR (tmp));
767 if (!CONSP (list))
768 newlist = list;
769 else
770 for (newlist = Qnil; CONSP (list); list = XCDR (list))
772 Lisp_Object elt = XCAR (list);
773 /* Preserve element ELT if it's t,
774 if it is a function with a `permanent-local-hook' property,
775 or if it's not a symbol. */
776 if (! SYMBOLP (elt)
777 || EQ (elt, Qt)
778 || !NILP (Fget (elt, Qpermanent_local_hook)))
779 newlist = Fcons (elt, newlist);
781 XSETCDR (XCAR (tmp), Fnreverse (newlist));
784 /* Delete this local variable. */
785 else if (NILP (last))
786 b->local_var_alist = XCDR (tmp);
787 else
788 XSETCDR (last, XCDR (tmp));
791 for (i = 0; i < last_per_buffer_idx; ++i)
792 if (permanent_too || buffer_permanent_local_flags[i] == 0)
793 SET_PER_BUFFER_VALUE_P (b, i, 0);
795 /* For each slot that has a default value,
796 copy that into the slot. */
798 /* buffer-local Lisp variables start at `undo_list',
799 tho only the ones from `name' on are GC'd normally. */
800 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
801 offset < sizeof *b;
802 offset += sizeof (Lisp_Object))
804 int idx = PER_BUFFER_IDX (offset);
805 if ((idx > 0
806 && (permanent_too
807 || buffer_permanent_local_flags[idx] == 0)))
808 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
812 /* We split this away from generate-new-buffer, because rename-buffer
813 and set-visited-file-name ought to be able to use this to really
814 rename the buffer properly. */
816 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
817 1, 2, 0,
818 doc: /* Return a string that is the name of no existing buffer based on NAME.
819 If there is no live buffer named NAME, then return NAME.
820 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
821 \(starting at 2) until an unused name is found, and then return that name.
822 Optional second argument IGNORE specifies a name that is okay to use (if
823 it is in the sequence to be tried) even if a buffer with that name exists. */)
824 (register Lisp_Object name, Lisp_Object ignore)
826 register Lisp_Object gentemp, tem;
827 int count;
828 char number[10];
830 CHECK_STRING (name);
832 tem = Fstring_equal (name, ignore);
833 if (!NILP (tem))
834 return name;
835 tem = Fget_buffer (name);
836 if (NILP (tem))
837 return name;
839 count = 1;
840 while (1)
842 sprintf (number, "<%d>", ++count);
843 gentemp = concat2 (name, build_string (number));
844 tem = Fstring_equal (gentemp, ignore);
845 if (!NILP (tem))
846 return gentemp;
847 tem = Fget_buffer (gentemp);
848 if (NILP (tem))
849 return gentemp;
854 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
855 doc: /* Return the name of BUFFER, as a string.
856 BUFFER defaults to the current buffer.
857 Return nil if BUFFER has been killed. */)
858 (register Lisp_Object buffer)
860 if (NILP (buffer))
861 return current_buffer->name;
862 CHECK_BUFFER (buffer);
863 return XBUFFER (buffer)->name;
866 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
867 doc: /* Return name of file BUFFER is visiting, or nil if none.
868 No argument or nil as argument means use the current buffer. */)
869 (register Lisp_Object buffer)
871 if (NILP (buffer))
872 return current_buffer->filename;
873 CHECK_BUFFER (buffer);
874 return XBUFFER (buffer)->filename;
877 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
878 0, 1, 0,
879 doc: /* Return the base buffer of indirect buffer BUFFER.
880 If BUFFER is not indirect, return nil.
881 BUFFER defaults to the current buffer. */)
882 (register Lisp_Object buffer)
884 struct buffer *base;
885 Lisp_Object base_buffer;
887 if (NILP (buffer))
888 base = current_buffer->base_buffer;
889 else
891 CHECK_BUFFER (buffer);
892 base = XBUFFER (buffer)->base_buffer;
895 if (! base)
896 return Qnil;
897 XSETBUFFER (base_buffer, base);
898 return base_buffer;
901 DEFUN ("buffer-local-value", Fbuffer_local_value,
902 Sbuffer_local_value, 2, 2, 0,
903 doc: /* Return the value of VARIABLE in BUFFER.
904 If VARIABLE does not have a buffer-local binding in BUFFER, the value
905 is the default binding of the variable. */)
906 (register Lisp_Object variable, register Lisp_Object buffer)
908 register struct buffer *buf;
909 register Lisp_Object result;
910 struct Lisp_Symbol *sym;
912 CHECK_SYMBOL (variable);
913 CHECK_BUFFER (buffer);
914 buf = XBUFFER (buffer);
915 sym = XSYMBOL (variable);
917 start:
918 switch (sym->redirect)
920 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
921 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
922 case SYMBOL_LOCALIZED:
923 { /* Look in local_var_alist. */
924 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
925 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
926 result = Fassoc (variable, buf->local_var_alist);
927 if (!NILP (result))
929 if (blv->fwd)
930 { /* What binding is loaded right now? */
931 Lisp_Object current_alist_element = blv->valcell;
933 /* The value of the currently loaded binding is not
934 stored in it, but rather in the realvalue slot.
935 Store that value into the binding it belongs to
936 in case that is the one we are about to use. */
938 XSETCDR (current_alist_element,
939 do_symval_forwarding (blv->fwd));
941 /* Now get the (perhaps updated) value out of the binding. */
942 result = XCDR (result);
944 else
945 result = Fdefault_value (variable);
946 break;
948 case SYMBOL_FORWARDED:
950 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
951 if (BUFFER_OBJFWDP (fwd))
952 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
953 else
954 result = Fdefault_value (variable);
955 break;
957 default: abort ();
960 if (!EQ (result, Qunbound))
961 return result;
963 xsignal1 (Qvoid_variable, variable);
966 /* Return an alist of the Lisp-level buffer-local bindings of
967 buffer BUF. That is, don't include the variables maintained
968 in special slots in the buffer object. */
970 static Lisp_Object
971 buffer_lisp_local_variables (struct buffer *buf)
973 Lisp_Object result = Qnil;
974 register Lisp_Object tail;
975 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
977 Lisp_Object val, elt;
979 elt = XCAR (tail);
981 /* Reference each variable in the alist in buf.
982 If inquiring about the current buffer, this gets the current values,
983 so store them into the alist so the alist is up to date.
984 If inquiring about some other buffer, this swaps out any values
985 for that buffer, making the alist up to date automatically. */
986 val = find_symbol_value (XCAR (elt));
987 /* Use the current buffer value only if buf is the current buffer. */
988 if (buf != current_buffer)
989 val = XCDR (elt);
991 result = Fcons (Fcons (XCAR (elt), val), result);
994 return result;
997 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
998 Sbuffer_local_variables, 0, 1, 0,
999 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1000 Most elements look like (SYMBOL . VALUE), describing one variable.
1001 For a symbol that is locally unbound, just the symbol appears in the value.
1002 Note that storing new VALUEs in these elements doesn't change the variables.
1003 No argument or nil as argument means use current buffer as BUFFER. */)
1004 (register Lisp_Object buffer)
1006 register struct buffer *buf;
1007 register Lisp_Object result;
1009 if (NILP (buffer))
1010 buf = current_buffer;
1011 else
1013 CHECK_BUFFER (buffer);
1014 buf = XBUFFER (buffer);
1017 result = buffer_lisp_local_variables (buf);
1019 /* Add on all the variables stored in special slots. */
1021 int offset, idx;
1023 /* buffer-local Lisp variables start at `undo_list',
1024 tho only the ones from `name' on are GC'd normally. */
1025 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1026 offset < sizeof (struct buffer);
1027 /* sizeof EMACS_INT == sizeof Lisp_Object */
1028 offset += (sizeof (EMACS_INT)))
1030 idx = PER_BUFFER_IDX (offset);
1031 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1032 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1033 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1034 PER_BUFFER_VALUE (buf, offset)),
1035 result);
1039 return result;
1042 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1043 0, 1, 0,
1044 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1045 No argument or nil as argument means use current buffer as BUFFER. */)
1046 (register Lisp_Object buffer)
1048 register struct buffer *buf;
1049 if (NILP (buffer))
1050 buf = current_buffer;
1051 else
1053 CHECK_BUFFER (buffer);
1054 buf = XBUFFER (buffer);
1057 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1060 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1061 1, 1, 0,
1062 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1063 A non-nil FLAG means mark the buffer modified. */)
1064 (register Lisp_Object flag)
1066 register int already;
1067 register Lisp_Object fn;
1068 Lisp_Object buffer, window;
1070 #ifdef CLASH_DETECTION
1071 /* If buffer becoming modified, lock the file.
1072 If buffer becoming unmodified, unlock the file. */
1074 fn = current_buffer->file_truename;
1075 /* Test buffer-file-name so that binding it to nil is effective. */
1076 if (!NILP (fn) && ! NILP (current_buffer->filename))
1078 already = SAVE_MODIFF < MODIFF;
1079 if (!already && !NILP (flag))
1080 lock_file (fn);
1081 else if (already && NILP (flag))
1082 unlock_file (fn);
1084 #endif /* CLASH_DETECTION */
1086 /* Here we have a problem. SAVE_MODIFF is used here to encode
1087 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1088 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1089 modify SAVE_MODIFF to affect one, we may affect the other
1090 as well.
1091 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1092 if SAVE_MODIFF<auto_save_modified that means we risk changing
1093 recent-auto-save-p from t to nil.
1094 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1095 we risk changing recent-auto-save-p from nil to t. */
1096 SAVE_MODIFF = (NILP (flag)
1097 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1098 ? MODIFF
1099 /* Let's try to preserve recent-auto-save-p. */
1100 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1101 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1102 we can either decrease SAVE_MODIFF and auto_save_modified
1103 or increase MODIFF. */
1104 : MODIFF++);
1106 /* Set update_mode_lines only if buffer is displayed in some window.
1107 Packages like jit-lock or lazy-lock preserve a buffer's modified
1108 state by recording/restoring the state around blocks of code.
1109 Setting update_mode_lines makes redisplay consider all windows
1110 (on all frames). Stealth fontification of buffers not displayed
1111 would incur additional redisplay costs if we'd set
1112 update_modes_lines unconditionally.
1114 Ideally, I think there should be another mechanism for fontifying
1115 buffers without "modifying" buffers, or redisplay should be
1116 smarter about updating the `*' in mode lines. --gerd */
1117 XSETBUFFER (buffer, current_buffer);
1118 window = Fget_buffer_window (buffer, Qt);
1119 if (WINDOWP (window))
1121 ++update_mode_lines;
1122 current_buffer->prevent_redisplay_optimizations_p = 1;
1125 return flag;
1128 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1129 Srestore_buffer_modified_p, 1, 1, 0,
1130 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1131 It is not ensured that mode lines will be updated to show the modified
1132 state of the current buffer. Use with care. */)
1133 (Lisp_Object flag)
1135 #ifdef CLASH_DETECTION
1136 Lisp_Object fn;
1138 /* If buffer becoming modified, lock the file.
1139 If buffer becoming unmodified, unlock the file. */
1141 fn = current_buffer->file_truename;
1142 /* Test buffer-file-name so that binding it to nil is effective. */
1143 if (!NILP (fn) && ! NILP (current_buffer->filename))
1145 int already = SAVE_MODIFF < MODIFF;
1146 if (!already && !NILP (flag))
1147 lock_file (fn);
1148 else if (already && NILP (flag))
1149 unlock_file (fn);
1151 #endif /* CLASH_DETECTION */
1153 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1154 return flag;
1157 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1158 0, 1, 0,
1159 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1160 Each buffer has a tick counter which is incremented each time the
1161 text in that buffer is changed. It wraps around occasionally.
1162 No argument or nil as argument means use current buffer as BUFFER. */)
1163 (register Lisp_Object buffer)
1165 register struct buffer *buf;
1166 if (NILP (buffer))
1167 buf = current_buffer;
1168 else
1170 CHECK_BUFFER (buffer);
1171 buf = XBUFFER (buffer);
1174 return make_number (BUF_MODIFF (buf));
1177 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1178 Sbuffer_chars_modified_tick, 0, 1, 0,
1179 doc: /* Return BUFFER's character-change tick counter.
1180 Each buffer has a character-change tick counter, which is set to the
1181 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1182 time text in that buffer is inserted or deleted. By comparing the
1183 values returned by two individual calls of `buffer-chars-modified-tick',
1184 you can tell whether a character change occurred in that buffer in
1185 between these calls. No argument or nil as argument means use current
1186 buffer as BUFFER. */)
1187 (register Lisp_Object buffer)
1189 register struct buffer *buf;
1190 if (NILP (buffer))
1191 buf = current_buffer;
1192 else
1194 CHECK_BUFFER (buffer);
1195 buf = XBUFFER (buffer);
1198 return make_number (BUF_CHARS_MODIFF (buf));
1201 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1202 "(list (read-string \"Rename buffer (to new name): \" \
1203 nil 'buffer-name-history (buffer-name (current-buffer))) \
1204 current-prefix-arg)",
1205 doc: /* Change current buffer's name to NEWNAME (a string).
1206 If second arg UNIQUE is nil or omitted, it is an error if a
1207 buffer named NEWNAME already exists.
1208 If UNIQUE is non-nil, come up with a new name using
1209 `generate-new-buffer-name'.
1210 Interactively, you can set UNIQUE with a prefix argument.
1211 We return the name we actually gave the buffer.
1212 This does not change the name of the visited file (if any). */)
1213 (register Lisp_Object newname, Lisp_Object unique)
1215 register Lisp_Object tem, buf;
1217 CHECK_STRING (newname);
1219 if (SCHARS (newname) == 0)
1220 error ("Empty string is invalid as a buffer name");
1222 tem = Fget_buffer (newname);
1223 if (!NILP (tem))
1225 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1226 rename the buffer automatically so you can create another
1227 with the original name. It makes UNIQUE equivalent to
1228 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1229 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1230 return current_buffer->name;
1231 if (!NILP (unique))
1232 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1233 else
1234 error ("Buffer name `%s' is in use", SDATA (newname));
1237 current_buffer->name = newname;
1239 /* Catch redisplay's attention. Unless we do this, the mode lines for
1240 any windows displaying current_buffer will stay unchanged. */
1241 update_mode_lines++;
1243 XSETBUFFER (buf, current_buffer);
1244 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1245 if (NILP (current_buffer->filename)
1246 && !NILP (current_buffer->auto_save_file_name))
1247 call0 (intern ("rename-auto-save-file"));
1248 /* Refetch since that last call may have done GC. */
1249 return current_buffer->name;
1252 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1253 doc: /* Return most recently selected buffer other than BUFFER.
1254 Buffers not visible in windows are preferred to visible buffers,
1255 unless optional second argument VISIBLE-OK is non-nil.
1256 If the optional third argument FRAME is non-nil, use that frame's
1257 buffer list instead of the selected frame's buffer list.
1258 If no other buffer exists, the buffer `*scratch*' is returned.
1259 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1260 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1262 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1263 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1264 notsogood = Qnil;
1266 if (NILP (frame))
1267 frame = selected_frame;
1269 CHECK_FRAME (frame);
1271 tail = Vbuffer_alist;
1272 pred = frame_buffer_predicate (frame);
1274 /* Consider buffers that have been seen in the selected frame
1275 before other buffers. */
1277 tem = frame_buffer_list (frame);
1278 add_ons = Qnil;
1279 while (CONSP (tem))
1281 if (BUFFERP (XCAR (tem)))
1282 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1283 tem = XCDR (tem);
1285 tail = nconc2 (Fnreverse (add_ons), tail);
1287 for (; CONSP (tail); tail = XCDR (tail))
1289 buf = Fcdr (XCAR (tail));
1290 if (EQ (buf, buffer))
1291 continue;
1292 if (NILP (buf))
1293 continue;
1294 if (NILP (XBUFFER (buf)->name))
1295 continue;
1296 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1297 continue;
1298 /* If the selected frame has a buffer_predicate,
1299 disregard buffers that don't fit the predicate. */
1300 if (!NILP (pred))
1302 tem = call1 (pred, buf);
1303 if (NILP (tem))
1304 continue;
1307 if (NILP (visible_ok))
1308 tem = Fget_buffer_window (buf, Qvisible);
1309 else
1310 tem = Qnil;
1311 if (NILP (tem))
1312 return buf;
1313 if (NILP (notsogood))
1314 notsogood = buf;
1316 if (!NILP (notsogood))
1317 return notsogood;
1318 buf = Fget_buffer (build_string ("*scratch*"));
1319 if (NILP (buf))
1321 buf = Fget_buffer_create (build_string ("*scratch*"));
1322 Fset_buffer_major_mode (buf);
1324 return buf;
1327 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1328 0, 1, "",
1329 doc: /* Start keeping undo information for buffer BUFFER.
1330 No argument or nil as argument means do this for the current buffer. */)
1331 (register Lisp_Object buffer)
1333 Lisp_Object real_buffer;
1335 if (NILP (buffer))
1336 XSETBUFFER (real_buffer, current_buffer);
1337 else
1339 real_buffer = Fget_buffer (buffer);
1340 if (NILP (real_buffer))
1341 nsberror (buffer);
1344 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1345 XBUFFER (real_buffer)->undo_list = Qnil;
1347 return Qnil;
1351 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1352 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1353 The buffer being killed will be current while the hook is running.\n\
1354 See `kill-buffer'."
1356 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1357 doc: /* Kill buffer BUFFER-OR-NAME.
1358 The argument may be a buffer or the name of an existing buffer.
1359 Argument nil or omitted means kill the current buffer. Return t if the
1360 buffer is actually killed, nil otherwise.
1362 This function calls `replace-buffer-in-windows' for cleaning up all
1363 windows currently displaying the buffer to be killed. The functions in
1364 `kill-buffer-query-functions' are called with the buffer to be killed as
1365 the current buffer. If any of them returns nil, the buffer is not
1366 killed. The hook `kill-buffer-hook' is run before the buffer is
1367 actually killed. The buffer being killed will be current while the hook
1368 is running.
1370 Any processes that have this buffer as the `process-buffer' are killed
1371 with SIGHUP. */)
1372 (Lisp_Object buffer_or_name)
1374 Lisp_Object buffer;
1375 register struct buffer *b;
1376 register Lisp_Object tem;
1377 register struct Lisp_Marker *m;
1378 struct gcpro gcpro1;
1380 if (NILP (buffer_or_name))
1381 buffer = Fcurrent_buffer ();
1382 else
1383 buffer = Fget_buffer (buffer_or_name);
1384 if (NILP (buffer))
1385 nsberror (buffer_or_name);
1387 b = XBUFFER (buffer);
1389 /* Avoid trouble for buffer already dead. */
1390 if (NILP (b->name))
1391 return Qnil;
1393 /* Query if the buffer is still modified. */
1394 if (INTERACTIVE && !NILP (b->filename)
1395 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1397 GCPRO1 (buffer);
1398 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1399 b->name, make_number (0)));
1400 UNGCPRO;
1401 if (NILP (tem))
1402 return Qnil;
1405 /* Run hooks with the buffer to be killed the current buffer. */
1407 int count = SPECPDL_INDEX ();
1408 Lisp_Object arglist[1];
1410 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1411 set_buffer_internal (b);
1413 /* First run the query functions; if any query is answered no,
1414 don't kill the buffer. */
1415 arglist[0] = Qkill_buffer_query_functions;
1416 tem = Frun_hook_with_args_until_failure (1, arglist);
1417 if (NILP (tem))
1418 return unbind_to (count, Qnil);
1420 /* Then run the hooks. */
1421 Frun_hooks (1, &Qkill_buffer_hook);
1422 unbind_to (count, Qnil);
1425 /* We have no more questions to ask. Verify that it is valid
1426 to kill the buffer. This must be done after the questions
1427 since anything can happen within do_yes_or_no_p. */
1429 /* Don't kill the minibuffer now current. */
1430 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1431 return Qnil;
1433 if (NILP (b->name))
1434 return Qnil;
1436 /* When we kill a base buffer, kill all its indirect buffers.
1437 We do it at this stage so nothing terrible happens if they
1438 ask questions or their hooks get errors. */
1439 if (! b->base_buffer)
1441 struct buffer *other;
1443 GCPRO1 (buffer);
1445 for (other = all_buffers; other; other = other->next)
1446 /* all_buffers contains dead buffers too;
1447 don't re-kill them. */
1448 if (other->base_buffer == b && !NILP (other->name))
1450 Lisp_Object buffer;
1451 XSETBUFFER (buffer, other);
1452 Fkill_buffer (buffer);
1455 UNGCPRO;
1458 /* Make this buffer not be current.
1459 In the process, notice if this is the sole visible buffer
1460 and give up if so. */
1461 if (b == current_buffer)
1463 tem = Fother_buffer (buffer, Qnil, Qnil);
1464 Fset_buffer (tem);
1465 if (b == current_buffer)
1466 return Qnil;
1469 /* Notice if the buffer to kill is the sole visible buffer
1470 when we're currently in the mini-buffer, and give up if so. */
1471 XSETBUFFER (tem, current_buffer);
1472 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1474 tem = Fother_buffer (buffer, Qnil, Qnil);
1475 if (EQ (buffer, tem))
1476 return Qnil;
1479 /* Now there is no question: we can kill the buffer. */
1481 #ifdef CLASH_DETECTION
1482 /* Unlock this buffer's file, if it is locked. */
1483 unlock_buffer (b);
1484 #endif /* CLASH_DETECTION */
1486 GCPRO1 (buffer);
1487 kill_buffer_processes (buffer);
1488 UNGCPRO;
1490 /* Killing buffer processes may run sentinels which may
1491 have called kill-buffer. */
1493 if (NILP (b->name))
1494 return Qnil;
1496 clear_charpos_cache (b);
1498 tem = Vinhibit_quit;
1499 Vinhibit_quit = Qt;
1500 replace_buffer_in_all_windows (buffer);
1501 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1502 frames_discard_buffer (buffer);
1503 Vinhibit_quit = tem;
1505 /* Delete any auto-save file, if we saved it in this session.
1506 But not if the buffer is modified. */
1507 if (STRINGP (b->auto_save_file_name)
1508 && BUF_AUTOSAVE_MODIFF (b) != 0
1509 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1510 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1511 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1513 Lisp_Object tem;
1514 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1515 if (! NILP (tem))
1516 internal_delete_file (b->auto_save_file_name);
1519 if (b->base_buffer)
1521 /* Unchain all markers that belong to this indirect buffer.
1522 Don't unchain the markers that belong to the base buffer
1523 or its other indirect buffers. */
1524 for (m = BUF_MARKERS (b); m; )
1526 struct Lisp_Marker *next = m->next;
1527 if (m->buffer == b)
1528 unchain_marker (m);
1529 m = next;
1532 else
1534 /* Unchain all markers of this buffer and its indirect buffers.
1535 and leave them pointing nowhere. */
1536 for (m = BUF_MARKERS (b); m; )
1538 struct Lisp_Marker *next = m->next;
1539 m->buffer = 0;
1540 m->next = NULL;
1541 m = next;
1543 BUF_MARKERS (b) = NULL;
1544 BUF_INTERVALS (b) = NULL_INTERVAL;
1546 /* Perhaps we should explicitly free the interval tree here... */
1549 /* Reset the local variables, so that this buffer's local values
1550 won't be protected from GC. They would be protected
1551 if they happened to remain encached in their symbols.
1552 This gets rid of them for certain. */
1553 swap_out_buffer_local_variables (b);
1554 reset_buffer_local_variables (b, 1);
1556 b->name = Qnil;
1558 BLOCK_INPUT;
1559 if (! b->base_buffer)
1560 free_buffer_text (b);
1562 if (b->newline_cache)
1564 free_region_cache (b->newline_cache);
1565 b->newline_cache = 0;
1567 if (b->width_run_cache)
1569 free_region_cache (b->width_run_cache);
1570 b->width_run_cache = 0;
1572 b->width_table = Qnil;
1573 UNBLOCK_INPUT;
1574 b->undo_list = Qnil;
1576 return Qt;
1579 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1580 we do this each time BUF is selected visibly, the more recently
1581 selected buffers are always closer to the front of the list. This
1582 means that other_buffer is more likely to choose a relevant buffer. */
1584 void
1585 record_buffer (Lisp_Object buf)
1587 register Lisp_Object link, prev;
1588 Lisp_Object frame;
1589 frame = selected_frame;
1591 prev = Qnil;
1592 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1594 if (EQ (XCDR (XCAR (link)), buf))
1595 break;
1596 prev = link;
1599 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1600 we cannot use Fdelq itself here because it allows quitting. */
1602 if (NILP (prev))
1603 Vbuffer_alist = XCDR (Vbuffer_alist);
1604 else
1605 XSETCDR (prev, XCDR (XCDR (prev)));
1607 XSETCDR (link, Vbuffer_alist);
1608 Vbuffer_alist = link;
1610 /* Effectively do a delq on buried_buffer_list. */
1612 prev = Qnil;
1613 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1614 link = XCDR (link))
1616 if (EQ (XCAR (link), buf))
1618 if (NILP (prev))
1619 XFRAME (frame)->buried_buffer_list = XCDR (link);
1620 else
1621 XSETCDR (prev, XCDR (XCDR (prev)));
1622 break;
1624 prev = link;
1627 /* Now move this buffer to the front of frame_buffer_list also. */
1629 prev = Qnil;
1630 for (link = frame_buffer_list (frame); CONSP (link);
1631 link = XCDR (link))
1633 if (EQ (XCAR (link), buf))
1634 break;
1635 prev = link;
1638 /* Effectively do delq. */
1640 if (CONSP (link))
1642 if (NILP (prev))
1643 set_frame_buffer_list (frame,
1644 XCDR (frame_buffer_list (frame)));
1645 else
1646 XSETCDR (prev, XCDR (XCDR (prev)));
1648 XSETCDR (link, frame_buffer_list (frame));
1649 set_frame_buffer_list (frame, link);
1651 else
1652 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1655 DEFUN ("record-buffer", Frecord_buffer, Srecord_buffer, 1, 1, 0,
1656 doc: /* Move BUFFER to the front of the buffer list. */)
1657 (Lisp_Object buffer)
1659 CHECK_BUFFER (buffer);
1661 record_buffer (buffer);
1663 return buffer;
1666 /* Move BUFFER to the end of the buffer (a)lists. Do nothing if the
1667 buffer is killed. For the selected frame and window buffer lists
1668 this moves BUFFER there even if it was never shown in that window
1669 or that frame. If this happens we have a feature
1670 (`unrecord-buffer' should be called only when BUFFER is shown in
1671 the selected window). */
1673 DEFUN ("unrecord-buffer", Funrecord_buffer, Sunrecord_buffer, 1, 1, 0,
1674 doc: /* Move BUFFER to the end of the buffer list. */)
1675 (Lisp_Object buffer)
1677 Lisp_Object aelt, link;
1678 register struct frame *f = XFRAME (selected_frame);
1680 CHECK_BUFFER (buffer);
1682 /* Update Vbuffer_alist (we know that it has an entry for BUFFER). */
1683 aelt = Frassq (buffer, Vbuffer_alist);
1684 link = Fmemq (aelt, Vbuffer_alist);
1685 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1686 XSETCDR (link, Qnil);
1687 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1689 /* Update buffer list of selected frame. */
1690 f->buffer_list = Fdelq (buffer, f->buffer_list);
1691 f->buried_buffer_list = Fcons (buffer, Fdelq (buffer, f->buried_buffer_list));
1694 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1695 doc: /* Set an appropriate major mode for BUFFER.
1696 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1697 according to `default-major-mode'.
1698 Use this function before selecting the buffer, since it may need to inspect
1699 the current buffer's major mode. */)
1700 (Lisp_Object buffer)
1702 int count;
1703 Lisp_Object function;
1705 CHECK_BUFFER (buffer);
1707 if (STRINGP (XBUFFER (buffer)->name)
1708 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1709 function = find_symbol_value (intern ("initial-major-mode"));
1710 else
1712 function = buffer_defaults.major_mode;
1713 if (NILP (function)
1714 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1715 function = current_buffer->major_mode;
1718 if (NILP (function) || EQ (function, Qfundamental_mode))
1719 return Qnil;
1721 count = SPECPDL_INDEX ();
1723 /* To select a nonfundamental mode,
1724 select the buffer temporarily and then call the mode function. */
1726 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1728 Fset_buffer (buffer);
1729 call0 (function);
1731 return unbind_to (count, Qnil);
1734 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1735 doc: /* Return the current buffer as a Lisp object. */)
1736 (void)
1738 register Lisp_Object buf;
1739 XSETBUFFER (buf, current_buffer);
1740 return buf;
1743 /* Set the current buffer to B.
1745 We previously set windows_or_buffers_changed here to invalidate
1746 global unchanged information in beg_unchanged and end_unchanged.
1747 This is no longer necessary because we now compute unchanged
1748 information on a buffer-basis. Every action affecting other
1749 windows than the selected one requires a select_window at some
1750 time, and that increments windows_or_buffers_changed. */
1752 void
1753 set_buffer_internal (register struct buffer *b)
1755 if (current_buffer != b)
1756 set_buffer_internal_1 (b);
1759 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1760 This is used by redisplay. */
1762 void
1763 set_buffer_internal_1 (register struct buffer *b)
1765 register struct buffer *old_buf;
1766 register Lisp_Object tail;
1768 #ifdef USE_MMAP_FOR_BUFFERS
1769 if (b->text->beg == NULL)
1770 enlarge_buffer_text (b, 0);
1771 #endif /* USE_MMAP_FOR_BUFFERS */
1773 if (current_buffer == b)
1774 return;
1776 old_buf = current_buffer;
1777 current_buffer = b;
1778 last_known_column_point = -1; /* invalidate indentation cache */
1780 if (old_buf)
1782 /* Put the undo list back in the base buffer, so that it appears
1783 that an indirect buffer shares the undo list of its base. */
1784 if (old_buf->base_buffer)
1785 old_buf->base_buffer->undo_list = old_buf->undo_list;
1787 /* If the old current buffer has markers to record PT, BEGV and ZV
1788 when it is not current, update them now. */
1789 if (! NILP (old_buf->pt_marker))
1791 Lisp_Object obuf;
1792 XSETBUFFER (obuf, old_buf);
1793 set_marker_both (old_buf->pt_marker, obuf,
1794 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1796 if (! NILP (old_buf->begv_marker))
1798 Lisp_Object obuf;
1799 XSETBUFFER (obuf, old_buf);
1800 set_marker_both (old_buf->begv_marker, obuf,
1801 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1803 if (! NILP (old_buf->zv_marker))
1805 Lisp_Object obuf;
1806 XSETBUFFER (obuf, old_buf);
1807 set_marker_both (old_buf->zv_marker, obuf,
1808 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1812 /* Get the undo list from the base buffer, so that it appears
1813 that an indirect buffer shares the undo list of its base. */
1814 if (b->base_buffer)
1815 b->undo_list = b->base_buffer->undo_list;
1817 /* If the new current buffer has markers to record PT, BEGV and ZV
1818 when it is not current, fetch them now. */
1819 if (! NILP (b->pt_marker))
1821 BUF_PT (b) = marker_position (b->pt_marker);
1822 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1824 if (! NILP (b->begv_marker))
1826 BUF_BEGV (b) = marker_position (b->begv_marker);
1827 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1829 if (! NILP (b->zv_marker))
1831 BUF_ZV (b) = marker_position (b->zv_marker);
1832 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1835 /* Look down buffer's list of local Lisp variables
1836 to find and update any that forward into C variables. */
1840 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1842 Lisp_Object var = XCAR (XCAR (tail));
1843 struct Lisp_Symbol *sym = XSYMBOL (var);
1844 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1845 && SYMBOL_BLV (sym)->fwd)
1846 /* Just reference the variable
1847 to cause it to become set for this buffer. */
1848 Fsymbol_value (var);
1851 /* Do the same with any others that were local to the previous buffer */
1852 while (b != old_buf && (b = old_buf, b));
1855 /* Switch to buffer B temporarily for redisplay purposes.
1856 This avoids certain things that don't need to be done within redisplay. */
1858 void
1859 set_buffer_temp (struct buffer *b)
1861 register struct buffer *old_buf;
1863 if (current_buffer == b)
1864 return;
1866 old_buf = current_buffer;
1867 current_buffer = b;
1869 if (old_buf)
1871 /* If the old current buffer has markers to record PT, BEGV and ZV
1872 when it is not current, update them now. */
1873 if (! NILP (old_buf->pt_marker))
1875 Lisp_Object obuf;
1876 XSETBUFFER (obuf, old_buf);
1877 set_marker_both (old_buf->pt_marker, obuf,
1878 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1880 if (! NILP (old_buf->begv_marker))
1882 Lisp_Object obuf;
1883 XSETBUFFER (obuf, old_buf);
1884 set_marker_both (old_buf->begv_marker, obuf,
1885 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1887 if (! NILP (old_buf->zv_marker))
1889 Lisp_Object obuf;
1890 XSETBUFFER (obuf, old_buf);
1891 set_marker_both (old_buf->zv_marker, obuf,
1892 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1896 /* If the new current buffer has markers to record PT, BEGV and ZV
1897 when it is not current, fetch them now. */
1898 if (! NILP (b->pt_marker))
1900 BUF_PT (b) = marker_position (b->pt_marker);
1901 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1903 if (! NILP (b->begv_marker))
1905 BUF_BEGV (b) = marker_position (b->begv_marker);
1906 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1908 if (! NILP (b->zv_marker))
1910 BUF_ZV (b) = marker_position (b->zv_marker);
1911 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1915 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1916 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1917 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1918 also `save-excursion' when you want to make a buffer current
1919 temporarily. This function does not display the buffer, so its effect
1920 ends when the current command terminates. Use `switch-to-buffer' or
1921 `pop-to-buffer' to switch buffers permanently. */)
1922 (register Lisp_Object buffer_or_name)
1924 register Lisp_Object buffer;
1925 buffer = Fget_buffer (buffer_or_name);
1926 if (NILP (buffer))
1927 nsberror (buffer_or_name);
1928 if (NILP (XBUFFER (buffer)->name))
1929 error ("Selecting deleted buffer");
1930 set_buffer_internal (XBUFFER (buffer));
1931 return buffer;
1934 /* Set the current buffer to BUFFER provided it is alive. */
1936 Lisp_Object
1937 set_buffer_if_live (Lisp_Object buffer)
1939 if (! NILP (XBUFFER (buffer)->name))
1940 Fset_buffer (buffer);
1941 return Qnil;
1944 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1945 Sbarf_if_buffer_read_only, 0, 0, 0,
1946 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1947 (void)
1949 if (!NILP (current_buffer->read_only)
1950 && NILP (Vinhibit_read_only))
1951 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1952 return Qnil;
1955 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1956 doc: /* Delete the entire contents of the current buffer.
1957 Any narrowing restriction in effect (see `narrow-to-region') is removed,
1958 so the buffer is truly empty after this. */)
1959 (void)
1961 Fwiden ();
1963 del_range (BEG, Z);
1965 current_buffer->last_window_start = 1;
1966 /* Prevent warnings, or suspension of auto saving, that would happen
1967 if future size is less than past size. Use of erase-buffer
1968 implies that the future text is not really related to the past text. */
1969 XSETFASTINT (current_buffer->save_length, 0);
1970 return Qnil;
1973 void
1974 validate_region (register Lisp_Object *b, register Lisp_Object *e)
1976 CHECK_NUMBER_COERCE_MARKER (*b);
1977 CHECK_NUMBER_COERCE_MARKER (*e);
1979 if (XINT (*b) > XINT (*e))
1981 Lisp_Object tem;
1982 tem = *b; *b = *e; *e = tem;
1985 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1986 && XINT (*e) <= ZV))
1987 args_out_of_range (*b, *e);
1990 /* Advance BYTE_POS up to a character boundary
1991 and return the adjusted position. */
1993 static int
1994 advance_to_char_boundary (int byte_pos)
1996 int c;
1998 if (byte_pos == BEG)
1999 /* Beginning of buffer is always a character boundary. */
2000 return BEG;
2002 c = FETCH_BYTE (byte_pos);
2003 if (! CHAR_HEAD_P (c))
2005 /* We should advance BYTE_POS only when C is a constituent of a
2006 multibyte sequence. */
2007 int orig_byte_pos = byte_pos;
2011 byte_pos--;
2012 c = FETCH_BYTE (byte_pos);
2014 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2015 INC_POS (byte_pos);
2016 if (byte_pos < orig_byte_pos)
2017 byte_pos = orig_byte_pos;
2018 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2019 surely advance to the correct character boundary. If C is
2020 not, BYTE_POS was unchanged. */
2023 return byte_pos;
2026 #ifdef REL_ALLOC
2027 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2028 #endif /* REL_ALLOC */
2030 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2031 1, 1, 0,
2032 doc: /* Swap the text between current buffer and BUFFER. */)
2033 (Lisp_Object buffer)
2035 struct buffer *other_buffer;
2036 CHECK_BUFFER (buffer);
2037 other_buffer = XBUFFER (buffer);
2039 if (NILP (other_buffer->name))
2040 error ("Cannot swap a dead buffer's text");
2042 /* Actually, it probably works just fine.
2043 * if (other_buffer == current_buffer)
2044 * error ("Cannot swap a buffer's text with itself"); */
2046 /* Actually, this may be workable as well, tho probably only if they're
2047 *both* indirect. */
2048 if (other_buffer->base_buffer
2049 || current_buffer->base_buffer)
2050 error ("Cannot swap indirect buffers's text");
2052 { /* This is probably harder to make work. */
2053 struct buffer *other;
2054 for (other = all_buffers; other; other = other->next)
2055 if (other->base_buffer == other_buffer
2056 || other->base_buffer == current_buffer)
2057 error ("One of the buffers to swap has indirect buffers");
2060 #define swapfield(field, type) \
2061 do { \
2062 type tmp##field = other_buffer->field; \
2063 other_buffer->field = current_buffer->field; \
2064 current_buffer->field = tmp##field; \
2065 } while (0)
2067 swapfield (own_text, struct buffer_text);
2068 eassert (current_buffer->text == &current_buffer->own_text);
2069 eassert (other_buffer->text == &other_buffer->own_text);
2070 #ifdef REL_ALLOC
2071 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2072 (POINTER_TYPE **) &other_buffer->own_text.beg);
2073 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2074 (POINTER_TYPE **) &current_buffer->own_text.beg);
2075 #endif /* REL_ALLOC */
2077 swapfield (pt, EMACS_INT);
2078 swapfield (pt_byte, EMACS_INT);
2079 swapfield (begv, EMACS_INT);
2080 swapfield (begv_byte, EMACS_INT);
2081 swapfield (zv, EMACS_INT);
2082 swapfield (zv_byte, EMACS_INT);
2083 eassert (!current_buffer->base_buffer);
2084 eassert (!other_buffer->base_buffer);
2085 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2086 swapfield (newline_cache, struct region_cache *);
2087 swapfield (width_run_cache, struct region_cache *);
2088 current_buffer->prevent_redisplay_optimizations_p = 1;
2089 other_buffer->prevent_redisplay_optimizations_p = 1;
2090 swapfield (overlays_before, struct Lisp_Overlay *);
2091 swapfield (overlays_after, struct Lisp_Overlay *);
2092 swapfield (overlay_center, EMACS_INT);
2093 swapfield (undo_list, Lisp_Object);
2094 swapfield (mark, Lisp_Object);
2095 swapfield (enable_multibyte_characters, Lisp_Object);
2096 swapfield (bidi_display_reordering, Lisp_Object);
2097 swapfield (bidi_paragraph_direction, Lisp_Object);
2098 /* FIXME: Not sure what we should do with these *_marker fields.
2099 Hopefully they're just nil anyway. */
2100 swapfield (pt_marker, Lisp_Object);
2101 swapfield (begv_marker, Lisp_Object);
2102 swapfield (zv_marker, Lisp_Object);
2103 current_buffer->point_before_scroll = Qnil;
2104 other_buffer->point_before_scroll = Qnil;
2106 current_buffer->text->modiff++; other_buffer->text->modiff++;
2107 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2108 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2109 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2110 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2111 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2112 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2114 struct Lisp_Marker *m;
2115 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2116 if (m->buffer == other_buffer)
2117 m->buffer = current_buffer;
2118 else
2119 /* Since there's no indirect buffer in sight, markers on
2120 BUF_MARKERS(buf) should either be for `buf' or dead. */
2121 eassert (!m->buffer);
2122 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2123 if (m->buffer == current_buffer)
2124 m->buffer = other_buffer;
2125 else
2126 /* Since there's no indirect buffer in sight, markers on
2127 BUF_MARKERS(buf) should either be for `buf' or dead. */
2128 eassert (!m->buffer);
2130 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2131 So since we just swapped the markers between the two buffers, we need
2132 to undo the effect of this swap for window markers. */
2133 Lisp_Object w = Fselected_window (), ws = Qnil;
2134 Lisp_Object buf1, buf2;
2135 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2137 while (NILP (Fmemq (w, ws)))
2139 ws = Fcons (w, ws);
2140 if (MARKERP (XWINDOW (w)->pointm)
2141 && (EQ (XWINDOW (w)->buffer, buf1)
2142 || EQ (XWINDOW (w)->buffer, buf2)))
2143 Fset_marker (XWINDOW (w)->pointm,
2144 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2145 XWINDOW (w)->buffer);
2146 w = Fnext_window (w, Qt, Qt);
2150 if (current_buffer->text->intervals)
2151 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2152 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2153 if (other_buffer->text->intervals)
2154 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2155 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2157 return Qnil;
2160 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2161 1, 1, 0,
2162 doc: /* Set the multibyte flag of the current buffer to FLAG.
2163 If FLAG is t, this makes the buffer a multibyte buffer.
2164 If FLAG is nil, this makes the buffer a single-byte buffer.
2165 In these cases, the buffer contents remain unchanged as a sequence of
2166 bytes but the contents viewed as characters do change.
2167 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2168 all eight-bit bytes to eight-bit characters.
2169 If the multibyte flag was really changed, undo information of the
2170 current buffer is cleared. */)
2171 (Lisp_Object flag)
2173 struct Lisp_Marker *tail, *markers;
2174 struct buffer *other;
2175 int begv, zv;
2176 int narrowed = (BEG != BEGV || Z != ZV);
2177 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2178 Lisp_Object old_undo = current_buffer->undo_list;
2179 struct gcpro gcpro1;
2181 if (current_buffer->base_buffer)
2182 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2184 /* Do nothing if nothing actually changes. */
2185 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2186 return flag;
2188 GCPRO1 (old_undo);
2190 /* Don't record these buffer changes. We will put a special undo entry
2191 instead. */
2192 current_buffer->undo_list = Qt;
2194 /* If the cached position is for this buffer, clear it out. */
2195 clear_charpos_cache (current_buffer);
2197 if (NILP (flag))
2198 begv = BEGV_BYTE, zv = ZV_BYTE;
2199 else
2200 begv = BEGV, zv = ZV;
2202 if (narrowed)
2203 Fwiden ();
2205 if (NILP (flag))
2207 int pos, stop;
2208 unsigned char *p;
2210 /* Do this first, so it can use CHAR_TO_BYTE
2211 to calculate the old correspondences. */
2212 set_intervals_multibyte (0);
2214 current_buffer->enable_multibyte_characters = Qnil;
2216 Z = Z_BYTE;
2217 BEGV = BEGV_BYTE;
2218 ZV = ZV_BYTE;
2219 GPT = GPT_BYTE;
2220 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2223 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2224 tail->charpos = tail->bytepos;
2226 /* Convert multibyte form of 8-bit characters to unibyte. */
2227 pos = BEG;
2228 stop = GPT;
2229 p = BEG_ADDR;
2230 while (1)
2232 int c, bytes;
2234 if (pos == stop)
2236 if (pos == Z)
2237 break;
2238 p = GAP_END_ADDR;
2239 stop = Z;
2241 if (ASCII_BYTE_P (*p))
2242 p++, pos++;
2243 else if (CHAR_BYTE8_HEAD_P (*p))
2245 c = STRING_CHAR_AND_LENGTH (p, bytes);
2246 /* Delete all bytes for this 8-bit character but the
2247 last one, and change the last one to the charcter
2248 code. */
2249 bytes--;
2250 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2251 p = GAP_END_ADDR;
2252 *p++ = c;
2253 pos++;
2254 if (begv > pos)
2255 begv -= bytes;
2256 if (zv > pos)
2257 zv -= bytes;
2258 stop = Z;
2260 else
2262 bytes = BYTES_BY_CHAR_HEAD (*p);
2263 p += bytes, pos += bytes;
2266 if (narrowed)
2267 Fnarrow_to_region (make_number (begv), make_number (zv));
2269 else
2271 int pt = PT;
2272 int pos, stop;
2273 unsigned char *p, *pend;
2275 /* Be sure not to have a multibyte sequence striding over the GAP.
2276 Ex: We change this: "...abc\302 _GAP_ \241def..."
2277 to: "...abc _GAP_ \302\241def..." */
2279 if (EQ (flag, Qt)
2280 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2281 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2283 unsigned char *p = GPT_ADDR - 1;
2285 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2286 if (LEADING_CODE_P (*p))
2288 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2290 move_gap_both (new_gpt, new_gpt);
2294 /* Make the buffer contents valid as multibyte by converting
2295 8-bit characters to multibyte form. */
2296 pos = BEG;
2297 stop = GPT;
2298 p = BEG_ADDR;
2299 pend = GPT_ADDR;
2300 while (1)
2302 int bytes;
2304 if (pos == stop)
2306 if (pos == Z)
2307 break;
2308 p = GAP_END_ADDR;
2309 pend = Z_ADDR;
2310 stop = Z;
2313 if (ASCII_BYTE_P (*p))
2314 p++, pos++;
2315 else if (EQ (flag, Qt)
2316 && ! CHAR_BYTE8_HEAD_P (*p)
2317 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2318 p += bytes, pos += bytes;
2319 else
2321 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2322 int c;
2324 c = BYTE8_TO_CHAR (*p);
2325 bytes = CHAR_STRING (c, tmp);
2326 *p = tmp[0];
2327 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2328 bytes--;
2329 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2330 /* Now the gap is after the just inserted data. */
2331 pos = GPT;
2332 p = GAP_END_ADDR;
2333 if (pos <= begv)
2334 begv += bytes;
2335 if (pos <= zv)
2336 zv += bytes;
2337 if (pos <= pt)
2338 pt += bytes;
2339 pend = Z_ADDR;
2340 stop = Z;
2344 if (pt != PT)
2345 TEMP_SET_PT (pt);
2347 if (narrowed)
2348 Fnarrow_to_region (make_number (begv), make_number (zv));
2350 /* Do this first, so that chars_in_text asks the right question.
2351 set_intervals_multibyte needs it too. */
2352 current_buffer->enable_multibyte_characters = Qt;
2354 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2355 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2357 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2359 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2360 if (BEGV_BYTE > GPT_BYTE)
2361 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2362 else
2363 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2365 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2366 if (ZV_BYTE > GPT_BYTE)
2367 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2368 else
2369 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2372 int pt_byte = advance_to_char_boundary (PT_BYTE);
2373 int pt;
2375 if (pt_byte > GPT_BYTE)
2376 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2377 else
2378 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2379 TEMP_SET_PT_BOTH (pt, pt_byte);
2382 tail = markers = BUF_MARKERS (current_buffer);
2384 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2385 getting confused by the markers that have not yet been updated.
2386 It is also a signal that it should never create a marker. */
2387 BUF_MARKERS (current_buffer) = NULL;
2389 for (; tail; tail = tail->next)
2391 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2392 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2395 /* Make sure no markers were put on the chain
2396 while the chain value was incorrect. */
2397 if (BUF_MARKERS (current_buffer))
2398 abort ();
2400 BUF_MARKERS (current_buffer) = markers;
2402 /* Do this last, so it can calculate the new correspondences
2403 between chars and bytes. */
2404 set_intervals_multibyte (1);
2407 if (!EQ (old_undo, Qt))
2409 /* Represent all the above changes by a special undo entry. */
2410 current_buffer->undo_list = Fcons (list3 (Qapply,
2411 intern ("set-buffer-multibyte"),
2412 NILP (flag) ? Qt : Qnil),
2413 old_undo);
2416 UNGCPRO;
2418 /* Changing the multibyteness of a buffer means that all windows
2419 showing that buffer must be updated thoroughly. */
2420 current_buffer->prevent_redisplay_optimizations_p = 1;
2421 ++windows_or_buffers_changed;
2423 /* Copy this buffer's new multibyte status
2424 into all of its indirect buffers. */
2425 for (other = all_buffers; other; other = other->next)
2426 if (other->base_buffer == current_buffer && !NILP (other->name))
2428 other->enable_multibyte_characters
2429 = current_buffer->enable_multibyte_characters;
2430 other->prevent_redisplay_optimizations_p = 1;
2433 /* Restore the modifiedness of the buffer. */
2434 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2435 Fset_buffer_modified_p (Qnil);
2437 /* Update coding systems of this buffer's process (if any). */
2439 Lisp_Object process;
2441 process = Fget_buffer_process (Fcurrent_buffer ());
2442 if (PROCESSP (process))
2443 setup_process_coding_systems (process);
2446 return flag;
2449 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2450 0, 0, 0,
2451 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2452 Most local variable bindings are eliminated so that the default values
2453 become effective once more. Also, the syntax table is set from
2454 `standard-syntax-table', the local keymap is set to nil,
2455 and the abbrev table from `fundamental-mode-abbrev-table'.
2456 This function also forces redisplay of the mode line.
2458 Every function to select a new major mode starts by
2459 calling this function.
2461 As a special exception, local variables whose names have
2462 a non-nil `permanent-local' property are not eliminated by this function.
2464 The first thing this function does is run
2465 the normal hook `change-major-mode-hook'. */)
2466 (void)
2468 if (!NILP (Vrun_hooks))
2469 call1 (Vrun_hooks, Qchange_major_mode_hook);
2471 /* Make sure none of the bindings in local_var_alist
2472 remain swapped in, in their symbols. */
2474 swap_out_buffer_local_variables (current_buffer);
2476 /* Actually eliminate all local bindings of this buffer. */
2478 reset_buffer_local_variables (current_buffer, 0);
2480 /* Force mode-line redisplay. Useful here because all major mode
2481 commands call this function. */
2482 update_mode_lines++;
2484 return Qnil;
2487 /* Make sure no local variables remain set up with buffer B
2488 for their current values. */
2490 static void
2491 swap_out_buffer_local_variables (struct buffer *b)
2493 Lisp_Object oalist, alist, buffer;
2495 XSETBUFFER (buffer, b);
2496 oalist = b->local_var_alist;
2498 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2500 Lisp_Object sym = XCAR (XCAR (alist));
2501 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2502 /* Need not do anything if some other buffer's binding is
2503 now encached. */
2504 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2506 /* Symbol is set up for this buffer's old local value:
2507 swap it out! */
2508 swap_in_global_binding (XSYMBOL (sym));
2513 /* Find all the overlays in the current buffer that contain position POS.
2514 Return the number found, and store them in a vector in *VEC_PTR.
2515 Store in *LEN_PTR the size allocated for the vector.
2516 Store in *NEXT_PTR the next position after POS where an overlay starts,
2517 or ZV if there are no more overlays between POS and ZV.
2518 Store in *PREV_PTR the previous position before POS where an overlay ends,
2519 or where an overlay starts which ends at or after POS;
2520 or BEGV if there are no such overlays from BEGV to POS.
2521 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2523 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2524 when this function is called.
2526 If EXTEND is non-zero, we make the vector bigger if necessary.
2527 If EXTEND is zero, we never extend the vector,
2528 and we store only as many overlays as will fit.
2529 But we still return the total number of overlays.
2531 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2532 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2533 default (BEGV or ZV). */
2536 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2537 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2539 Lisp_Object overlay, start, end;
2540 struct Lisp_Overlay *tail;
2541 int idx = 0;
2542 int len = *len_ptr;
2543 Lisp_Object *vec = *vec_ptr;
2544 int next = ZV;
2545 int prev = BEGV;
2546 int inhibit_storing = 0;
2548 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2550 int startpos, endpos;
2552 XSETMISC (overlay, tail);
2554 start = OVERLAY_START (overlay);
2555 end = OVERLAY_END (overlay);
2556 endpos = OVERLAY_POSITION (end);
2557 if (endpos < pos)
2559 if (prev < endpos)
2560 prev = endpos;
2561 break;
2563 startpos = OVERLAY_POSITION (start);
2564 /* This one ends at or after POS
2565 so its start counts for PREV_PTR if it's before POS. */
2566 if (prev < startpos && startpos < pos)
2567 prev = startpos;
2568 if (endpos == pos)
2569 continue;
2570 if (startpos <= pos)
2572 if (idx == len)
2574 /* The supplied vector is full.
2575 Either make it bigger, or don't store any more in it. */
2576 if (extend)
2578 /* Make it work with an initial len == 0. */
2579 len *= 2;
2580 if (len == 0)
2581 len = 4;
2582 *len_ptr = len;
2583 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2584 *vec_ptr = vec;
2586 else
2587 inhibit_storing = 1;
2590 if (!inhibit_storing)
2591 vec[idx] = overlay;
2592 /* Keep counting overlays even if we can't return them all. */
2593 idx++;
2595 else if (startpos < next)
2596 next = startpos;
2599 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2601 int startpos, endpos;
2603 XSETMISC (overlay, tail);
2605 start = OVERLAY_START (overlay);
2606 end = OVERLAY_END (overlay);
2607 startpos = OVERLAY_POSITION (start);
2608 if (pos < startpos)
2610 if (startpos < next)
2611 next = startpos;
2612 break;
2614 endpos = OVERLAY_POSITION (end);
2615 if (pos < endpos)
2617 if (idx == len)
2619 if (extend)
2621 /* Make it work with an initial len == 0. */
2622 len *= 2;
2623 if (len == 0)
2624 len = 4;
2625 *len_ptr = len;
2626 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2627 *vec_ptr = vec;
2629 else
2630 inhibit_storing = 1;
2633 if (!inhibit_storing)
2634 vec[idx] = overlay;
2635 idx++;
2637 if (startpos < pos && startpos > prev)
2638 prev = startpos;
2640 else if (endpos < pos && endpos > prev)
2641 prev = endpos;
2642 else if (endpos == pos && startpos > prev
2643 && (!change_req || startpos < pos))
2644 prev = startpos;
2647 if (next_ptr)
2648 *next_ptr = next;
2649 if (prev_ptr)
2650 *prev_ptr = prev;
2651 return idx;
2654 /* Find all the overlays in the current buffer that overlap the range
2655 BEG-END, or are empty at BEG, or are empty at END provided END
2656 denotes the position at the end of the current buffer.
2658 Return the number found, and store them in a vector in *VEC_PTR.
2659 Store in *LEN_PTR the size allocated for the vector.
2660 Store in *NEXT_PTR the next position after POS where an overlay starts,
2661 or ZV if there are no more overlays.
2662 Store in *PREV_PTR the previous position before POS where an overlay ends,
2663 or BEGV if there are no previous overlays.
2664 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2666 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2667 when this function is called.
2669 If EXTEND is non-zero, we make the vector bigger if necessary.
2670 If EXTEND is zero, we never extend the vector,
2671 and we store only as many overlays as will fit.
2672 But we still return the total number of overlays. */
2674 static int
2675 overlays_in (int beg, int end, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2676 int *next_ptr, int *prev_ptr)
2678 Lisp_Object overlay, ostart, oend;
2679 struct Lisp_Overlay *tail;
2680 int idx = 0;
2681 int len = *len_ptr;
2682 Lisp_Object *vec = *vec_ptr;
2683 int next = ZV;
2684 int prev = BEGV;
2685 int inhibit_storing = 0;
2686 int end_is_Z = end == Z;
2688 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2690 int startpos, endpos;
2692 XSETMISC (overlay, tail);
2694 ostart = OVERLAY_START (overlay);
2695 oend = OVERLAY_END (overlay);
2696 endpos = OVERLAY_POSITION (oend);
2697 if (endpos < beg)
2699 if (prev < endpos)
2700 prev = endpos;
2701 break;
2703 startpos = OVERLAY_POSITION (ostart);
2704 /* Count an interval if it overlaps the range, is empty at the
2705 start of the range, or is empty at END provided END denotes the
2706 end of the buffer. */
2707 if ((beg < endpos && startpos < end)
2708 || (startpos == endpos
2709 && (beg == endpos || (end_is_Z && endpos == end))))
2711 if (idx == len)
2713 /* The supplied vector is full.
2714 Either make it bigger, or don't store any more in it. */
2715 if (extend)
2717 /* Make it work with an initial len == 0. */
2718 len *= 2;
2719 if (len == 0)
2720 len = 4;
2721 *len_ptr = len;
2722 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2723 *vec_ptr = vec;
2725 else
2726 inhibit_storing = 1;
2729 if (!inhibit_storing)
2730 vec[idx] = overlay;
2731 /* Keep counting overlays even if we can't return them all. */
2732 idx++;
2734 else if (startpos < next)
2735 next = startpos;
2738 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2740 int startpos, endpos;
2742 XSETMISC (overlay, tail);
2744 ostart = OVERLAY_START (overlay);
2745 oend = OVERLAY_END (overlay);
2746 startpos = OVERLAY_POSITION (ostart);
2747 if (end < startpos)
2749 if (startpos < next)
2750 next = startpos;
2751 break;
2753 endpos = OVERLAY_POSITION (oend);
2754 /* Count an interval if it overlaps the range, is empty at the
2755 start of the range, or is empty at END provided END denotes the
2756 end of the buffer. */
2757 if ((beg < endpos && startpos < end)
2758 || (startpos == endpos
2759 && (beg == endpos || (end_is_Z && endpos == end))))
2761 if (idx == len)
2763 if (extend)
2765 /* Make it work with an initial len == 0. */
2766 len *= 2;
2767 if (len == 0)
2768 len = 4;
2769 *len_ptr = len;
2770 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2771 *vec_ptr = vec;
2773 else
2774 inhibit_storing = 1;
2777 if (!inhibit_storing)
2778 vec[idx] = overlay;
2779 idx++;
2781 else if (endpos < beg && endpos > prev)
2782 prev = endpos;
2785 if (next_ptr)
2786 *next_ptr = next;
2787 if (prev_ptr)
2788 *prev_ptr = prev;
2789 return idx;
2793 /* Return non-zero if there exists an overlay with a non-nil
2794 `mouse-face' property overlapping OVERLAY. */
2797 mouse_face_overlay_overlaps (Lisp_Object overlay)
2799 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2800 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2801 int n, i, size;
2802 Lisp_Object *v, tem;
2804 size = 10;
2805 v = (Lisp_Object *) alloca (size * sizeof *v);
2806 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2807 if (n > size)
2809 v = (Lisp_Object *) alloca (n * sizeof *v);
2810 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2813 for (i = 0; i < n; ++i)
2814 if (!EQ (v[i], overlay)
2815 && (tem = Foverlay_get (overlay, Qmouse_face),
2816 !NILP (tem)))
2817 break;
2819 return i < n;
2824 /* Fast function to just test if we're at an overlay boundary. */
2826 overlay_touches_p (int pos)
2828 Lisp_Object overlay;
2829 struct Lisp_Overlay *tail;
2831 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2833 int endpos;
2835 XSETMISC (overlay ,tail);
2836 if (!OVERLAYP (overlay))
2837 abort ();
2839 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2840 if (endpos < pos)
2841 break;
2842 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2843 return 1;
2846 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2848 int startpos;
2850 XSETMISC (overlay, tail);
2851 if (!OVERLAYP (overlay))
2852 abort ();
2854 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2855 if (pos < startpos)
2856 break;
2857 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2858 return 1;
2860 return 0;
2863 struct sortvec
2865 Lisp_Object overlay;
2866 int beg, end;
2867 int priority;
2870 static int
2871 compare_overlays (const void *v1, const void *v2)
2873 const struct sortvec *s1 = (const struct sortvec *) v1;
2874 const struct sortvec *s2 = (const struct sortvec *) v2;
2875 if (s1->priority != s2->priority)
2876 return s1->priority - s2->priority;
2877 if (s1->beg != s2->beg)
2878 return s1->beg - s2->beg;
2879 if (s1->end != s2->end)
2880 return s2->end - s1->end;
2881 return 0;
2884 /* Sort an array of overlays by priority. The array is modified in place.
2885 The return value is the new size; this may be smaller than the original
2886 size if some of the overlays were invalid or were window-specific. */
2888 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2890 int i, j;
2891 struct sortvec *sortvec;
2892 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2894 /* Put the valid and relevant overlays into sortvec. */
2896 for (i = 0, j = 0; i < noverlays; i++)
2898 Lisp_Object tem;
2899 Lisp_Object overlay;
2901 overlay = overlay_vec[i];
2902 if (OVERLAY_VALID (overlay)
2903 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2904 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2906 /* If we're interested in a specific window, then ignore
2907 overlays that are limited to some other window. */
2908 if (w)
2910 Lisp_Object window;
2912 window = Foverlay_get (overlay, Qwindow);
2913 if (WINDOWP (window) && XWINDOW (window) != w)
2914 continue;
2917 /* This overlay is good and counts: put it into sortvec. */
2918 sortvec[j].overlay = overlay;
2919 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2920 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2921 tem = Foverlay_get (overlay, Qpriority);
2922 if (INTEGERP (tem))
2923 sortvec[j].priority = XINT (tem);
2924 else
2925 sortvec[j].priority = 0;
2926 j++;
2929 noverlays = j;
2931 /* Sort the overlays into the proper order: increasing priority. */
2933 if (noverlays > 1)
2934 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2936 for (i = 0; i < noverlays; i++)
2937 overlay_vec[i] = sortvec[i].overlay;
2938 return (noverlays);
2941 struct sortstr
2943 Lisp_Object string, string2;
2944 int size;
2945 int priority;
2948 struct sortstrlist
2950 struct sortstr *buf; /* An array that expands as needed; never freed. */
2951 int size; /* Allocated length of that array. */
2952 int used; /* How much of the array is currently in use. */
2953 int bytes; /* Total length of the strings in buf. */
2956 /* Buffers for storing information about the overlays touching a given
2957 position. These could be automatic variables in overlay_strings, but
2958 it's more efficient to hold onto the memory instead of repeatedly
2959 allocating and freeing it. */
2960 static struct sortstrlist overlay_heads, overlay_tails;
2961 static unsigned char *overlay_str_buf;
2963 /* Allocated length of overlay_str_buf. */
2964 static int overlay_str_len;
2966 /* A comparison function suitable for passing to qsort. */
2967 static int
2968 cmp_for_strings (const void *as1, const void *as2)
2970 struct sortstr *s1 = (struct sortstr *)as1;
2971 struct sortstr *s2 = (struct sortstr *)as2;
2972 if (s1->size != s2->size)
2973 return s2->size - s1->size;
2974 if (s1->priority != s2->priority)
2975 return s1->priority - s2->priority;
2976 return 0;
2979 static void
2980 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
2982 int nbytes;
2984 if (ssl->used == ssl->size)
2986 if (ssl->buf)
2987 ssl->size *= 2;
2988 else
2989 ssl->size = 5;
2990 ssl->buf = ((struct sortstr *)
2991 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2993 ssl->buf[ssl->used].string = str;
2994 ssl->buf[ssl->used].string2 = str2;
2995 ssl->buf[ssl->used].size = size;
2996 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2997 ssl->used++;
2999 if (NILP (current_buffer->enable_multibyte_characters))
3000 nbytes = SCHARS (str);
3001 else if (! STRING_MULTIBYTE (str))
3002 nbytes = count_size_as_multibyte (SDATA (str),
3003 SBYTES (str));
3004 else
3005 nbytes = SBYTES (str);
3007 ssl->bytes += nbytes;
3009 if (STRINGP (str2))
3011 if (NILP (current_buffer->enable_multibyte_characters))
3012 nbytes = SCHARS (str2);
3013 else if (! STRING_MULTIBYTE (str2))
3014 nbytes = count_size_as_multibyte (SDATA (str2),
3015 SBYTES (str2));
3016 else
3017 nbytes = SBYTES (str2);
3019 ssl->bytes += nbytes;
3023 /* Return the concatenation of the strings associated with overlays that
3024 begin or end at POS, ignoring overlays that are specific to a window
3025 other than W. The strings are concatenated in the appropriate order:
3026 shorter overlays nest inside longer ones, and higher priority inside
3027 lower. Normally all of the after-strings come first, but zero-sized
3028 overlays have their after-strings ride along with the before-strings
3029 because it would look strange to print them inside-out.
3031 Returns the string length, and stores the contents indirectly through
3032 PSTR, if that variable is non-null. The string may be overwritten by
3033 subsequent calls. */
3036 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3038 Lisp_Object overlay, window, str;
3039 struct Lisp_Overlay *ov;
3040 int startpos, endpos;
3041 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3043 overlay_heads.used = overlay_heads.bytes = 0;
3044 overlay_tails.used = overlay_tails.bytes = 0;
3045 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3047 XSETMISC (overlay, ov);
3048 eassert (OVERLAYP (overlay));
3050 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3051 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3052 if (endpos < pos)
3053 break;
3054 if (endpos != pos && startpos != pos)
3055 continue;
3056 window = Foverlay_get (overlay, Qwindow);
3057 if (WINDOWP (window) && XWINDOW (window) != w)
3058 continue;
3059 if (startpos == pos
3060 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3061 record_overlay_string (&overlay_heads, str,
3062 (startpos == endpos
3063 ? Foverlay_get (overlay, Qafter_string)
3064 : Qnil),
3065 Foverlay_get (overlay, Qpriority),
3066 endpos - startpos);
3067 else if (endpos == pos
3068 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3069 record_overlay_string (&overlay_tails, str, Qnil,
3070 Foverlay_get (overlay, Qpriority),
3071 endpos - startpos);
3073 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3075 XSETMISC (overlay, ov);
3076 eassert (OVERLAYP (overlay));
3078 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3079 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3080 if (startpos > pos)
3081 break;
3082 if (endpos != pos && startpos != pos)
3083 continue;
3084 window = Foverlay_get (overlay, Qwindow);
3085 if (WINDOWP (window) && XWINDOW (window) != w)
3086 continue;
3087 if (startpos == pos
3088 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3089 record_overlay_string (&overlay_heads, str,
3090 (startpos == endpos
3091 ? Foverlay_get (overlay, Qafter_string)
3092 : Qnil),
3093 Foverlay_get (overlay, Qpriority),
3094 endpos - startpos);
3095 else if (endpos == pos
3096 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3097 record_overlay_string (&overlay_tails, str, Qnil,
3098 Foverlay_get (overlay, Qpriority),
3099 endpos - startpos);
3101 if (overlay_tails.used > 1)
3102 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3103 cmp_for_strings);
3104 if (overlay_heads.used > 1)
3105 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3106 cmp_for_strings);
3107 if (overlay_heads.bytes || overlay_tails.bytes)
3109 Lisp_Object tem;
3110 int i;
3111 unsigned char *p;
3112 int total = overlay_heads.bytes + overlay_tails.bytes;
3114 if (total > overlay_str_len)
3116 overlay_str_len = total;
3117 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3118 total);
3120 p = overlay_str_buf;
3121 for (i = overlay_tails.used; --i >= 0;)
3123 int nbytes;
3124 tem = overlay_tails.buf[i].string;
3125 nbytes = copy_text (SDATA (tem), p,
3126 SBYTES (tem),
3127 STRING_MULTIBYTE (tem), multibyte);
3128 p += nbytes;
3130 for (i = 0; i < overlay_heads.used; ++i)
3132 int nbytes;
3133 tem = overlay_heads.buf[i].string;
3134 nbytes = copy_text (SDATA (tem), p,
3135 SBYTES (tem),
3136 STRING_MULTIBYTE (tem), multibyte);
3137 p += nbytes;
3138 tem = overlay_heads.buf[i].string2;
3139 if (STRINGP (tem))
3141 nbytes = copy_text (SDATA (tem), p,
3142 SBYTES (tem),
3143 STRING_MULTIBYTE (tem), multibyte);
3144 p += nbytes;
3147 if (p != overlay_str_buf + total)
3148 abort ();
3149 if (pstr)
3150 *pstr = overlay_str_buf;
3151 return total;
3153 return 0;
3156 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3158 void
3159 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3161 Lisp_Object overlay, beg, end;
3162 struct Lisp_Overlay *prev, *tail, *next;
3164 /* See if anything in overlays_before should move to overlays_after. */
3166 /* We don't strictly need prev in this loop; it should always be nil.
3167 But we use it for symmetry and in case that should cease to be true
3168 with some future change. */
3169 prev = NULL;
3170 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3172 next = tail->next;
3173 XSETMISC (overlay, tail);
3175 /* If the overlay is not valid, get rid of it. */
3176 if (!OVERLAY_VALID (overlay))
3177 #if 1
3178 abort ();
3179 #else
3181 /* Splice the cons cell TAIL out of overlays_before. */
3182 if (!NILP (prev))
3183 XCDR (prev) = next;
3184 else
3185 buf->overlays_before = next;
3186 tail = prev;
3187 continue;
3189 #endif
3191 beg = OVERLAY_START (overlay);
3192 end = OVERLAY_END (overlay);
3194 if (OVERLAY_POSITION (end) > pos)
3196 /* OVERLAY needs to be moved. */
3197 int where = OVERLAY_POSITION (beg);
3198 struct Lisp_Overlay *other, *other_prev;
3200 /* Splice the cons cell TAIL out of overlays_before. */
3201 if (prev)
3202 prev->next = next;
3203 else
3204 buf->overlays_before = next;
3206 /* Search thru overlays_after for where to put it. */
3207 other_prev = NULL;
3208 for (other = buf->overlays_after; other;
3209 other_prev = other, other = other->next)
3211 Lisp_Object otherbeg, otheroverlay;
3213 XSETMISC (otheroverlay, other);
3214 eassert (OVERLAY_VALID (otheroverlay));
3216 otherbeg = OVERLAY_START (otheroverlay);
3217 if (OVERLAY_POSITION (otherbeg) >= where)
3218 break;
3221 /* Add TAIL to overlays_after before OTHER. */
3222 tail->next = other;
3223 if (other_prev)
3224 other_prev->next = tail;
3225 else
3226 buf->overlays_after = tail;
3227 tail = prev;
3229 else
3230 /* We've reached the things that should stay in overlays_before.
3231 All the rest of overlays_before must end even earlier,
3232 so stop now. */
3233 break;
3236 /* See if anything in overlays_after should be in overlays_before. */
3237 prev = NULL;
3238 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3240 next = tail->next;
3241 XSETMISC (overlay, tail);
3243 /* If the overlay is not valid, get rid of it. */
3244 if (!OVERLAY_VALID (overlay))
3245 #if 1
3246 abort ();
3247 #else
3249 /* Splice the cons cell TAIL out of overlays_after. */
3250 if (!NILP (prev))
3251 XCDR (prev) = next;
3252 else
3253 buf->overlays_after = next;
3254 tail = prev;
3255 continue;
3257 #endif
3259 beg = OVERLAY_START (overlay);
3260 end = OVERLAY_END (overlay);
3262 /* Stop looking, when we know that nothing further
3263 can possibly end before POS. */
3264 if (OVERLAY_POSITION (beg) > pos)
3265 break;
3267 if (OVERLAY_POSITION (end) <= pos)
3269 /* OVERLAY needs to be moved. */
3270 int where = OVERLAY_POSITION (end);
3271 struct Lisp_Overlay *other, *other_prev;
3273 /* Splice the cons cell TAIL out of overlays_after. */
3274 if (prev)
3275 prev->next = next;
3276 else
3277 buf->overlays_after = next;
3279 /* Search thru overlays_before for where to put it. */
3280 other_prev = NULL;
3281 for (other = buf->overlays_before; other;
3282 other_prev = other, other = other->next)
3284 Lisp_Object otherend, otheroverlay;
3286 XSETMISC (otheroverlay, other);
3287 eassert (OVERLAY_VALID (otheroverlay));
3289 otherend = OVERLAY_END (otheroverlay);
3290 if (OVERLAY_POSITION (otherend) <= where)
3291 break;
3294 /* Add TAIL to overlays_before before OTHER. */
3295 tail->next = other;
3296 if (other_prev)
3297 other_prev->next = tail;
3298 else
3299 buf->overlays_before = tail;
3300 tail = prev;
3304 buf->overlay_center = pos;
3307 void
3308 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3310 /* After an insertion, the lists are still sorted properly,
3311 but we may need to update the value of the overlay center. */
3312 if (current_buffer->overlay_center >= pos)
3313 current_buffer->overlay_center += length;
3316 void
3317 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3319 if (current_buffer->overlay_center < pos)
3320 /* The deletion was to our right. No change needed; the before- and
3321 after-lists are still consistent. */
3323 else if (current_buffer->overlay_center > pos + length)
3324 /* The deletion was to our left. We need to adjust the center value
3325 to account for the change in position, but the lists are consistent
3326 given the new value. */
3327 current_buffer->overlay_center -= length;
3328 else
3329 /* We're right in the middle. There might be things on the after-list
3330 that now belong on the before-list. Recentering will move them,
3331 and also update the center point. */
3332 recenter_overlay_lists (current_buffer, pos);
3335 /* Fix up overlays that were garbled as a result of permuting markers
3336 in the range START through END. Any overlay with at least one
3337 endpoint in this range will need to be unlinked from the overlay
3338 list and reinserted in its proper place.
3339 Such an overlay might even have negative size at this point.
3340 If so, we'll make the overlay empty. */
3341 void
3342 fix_start_end_in_overlays (register int start, register int end)
3344 Lisp_Object overlay;
3345 struct Lisp_Overlay *before_list, *after_list;
3346 /* These are either nil, indicating that before_list or after_list
3347 should be assigned, or the cons cell the cdr of which should be
3348 assigned. */
3349 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3350 /* 'Parent', likewise, indicates a cons cell or
3351 current_buffer->overlays_before or overlays_after, depending
3352 which loop we're in. */
3353 struct Lisp_Overlay *tail, *parent;
3354 int startpos, endpos;
3356 /* This algorithm shifts links around instead of consing and GCing.
3357 The loop invariant is that before_list (resp. after_list) is a
3358 well-formed list except that its last element, the CDR of beforep
3359 (resp. afterp) if beforep (afterp) isn't nil or before_list
3360 (after_list) if it is, is still uninitialized. So it's not a bug
3361 that before_list isn't initialized, although it may look
3362 strange. */
3363 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3365 XSETMISC (overlay, tail);
3367 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3368 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3370 /* If the overlay is backwards, make it empty. */
3371 if (endpos < startpos)
3373 startpos = endpos;
3374 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3375 Qnil);
3378 if (endpos < start)
3379 break;
3381 if (endpos < end
3382 || (startpos >= start && startpos < end))
3384 /* Add it to the end of the wrong list. Later on,
3385 recenter_overlay_lists will move it to the right place. */
3386 if (endpos < current_buffer->overlay_center)
3388 if (!afterp)
3389 after_list = tail;
3390 else
3391 afterp->next = tail;
3392 afterp = tail;
3394 else
3396 if (!beforep)
3397 before_list = tail;
3398 else
3399 beforep->next = tail;
3400 beforep = tail;
3402 if (!parent)
3403 current_buffer->overlays_before = tail->next;
3404 else
3405 parent->next = tail->next;
3406 tail = tail->next;
3408 else
3409 parent = tail, tail = parent->next;
3411 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3413 XSETMISC (overlay, tail);
3415 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3416 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3418 /* If the overlay is backwards, make it empty. */
3419 if (endpos < startpos)
3421 startpos = endpos;
3422 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3423 Qnil);
3426 if (startpos >= end)
3427 break;
3429 if (startpos >= start
3430 || (endpos >= start && endpos < end))
3432 if (endpos < current_buffer->overlay_center)
3434 if (!afterp)
3435 after_list = tail;
3436 else
3437 afterp->next = tail;
3438 afterp = tail;
3440 else
3442 if (!beforep)
3443 before_list = tail;
3444 else
3445 beforep->next = tail;
3446 beforep = tail;
3448 if (!parent)
3449 current_buffer->overlays_after = tail->next;
3450 else
3451 parent->next = tail->next;
3452 tail = tail->next;
3454 else
3455 parent = tail, tail = parent->next;
3458 /* Splice the constructed (wrong) lists into the buffer's lists,
3459 and let the recenter function make it sane again. */
3460 if (beforep)
3462 beforep->next = current_buffer->overlays_before;
3463 current_buffer->overlays_before = before_list;
3465 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3467 if (afterp)
3469 afterp->next = current_buffer->overlays_after;
3470 current_buffer->overlays_after = after_list;
3472 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3475 /* We have two types of overlay: the one whose ending marker is
3476 after-insertion-marker (this is the usual case) and the one whose
3477 ending marker is before-insertion-marker. When `overlays_before'
3478 contains overlays of the latter type and the former type in this
3479 order and both overlays end at inserting position, inserting a text
3480 increases only the ending marker of the latter type, which results
3481 in incorrect ordering of `overlays_before'.
3483 This function fixes ordering of overlays in the slot
3484 `overlays_before' of the buffer *BP. Before the insertion, `point'
3485 was at PREV, and now is at POS. */
3487 void
3488 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3490 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3491 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3492 Lisp_Object tem;
3493 EMACS_INT end;
3495 /* After the insertion, the several overlays may be in incorrect
3496 order. The possibility is that, in the list `overlays_before',
3497 an overlay which ends at POS appears after an overlay which ends
3498 at PREV. Since POS is greater than PREV, we must fix the
3499 ordering of these overlays, by moving overlays ends at POS before
3500 the overlays ends at PREV. */
3502 /* At first, find a place where disordered overlays should be linked
3503 in. It is where an overlay which end before POS exists. (i.e. an
3504 overlay whose ending marker is after-insertion-marker if disorder
3505 exists). */
3506 while (tail
3507 && (XSETMISC (tem, tail),
3508 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3510 parent = tail;
3511 tail = tail->next;
3514 /* If we don't find such an overlay,
3515 or the found one ends before PREV,
3516 or the found one is the last one in the list,
3517 we don't have to fix anything. */
3518 if (!tail || end < prev || !tail->next)
3519 return;
3521 right_pair = parent;
3522 parent = tail;
3523 tail = tail->next;
3525 /* Now, end position of overlays in the list TAIL should be before
3526 or equal to PREV. In the loop, an overlay which ends at POS is
3527 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3528 we found an overlay which ends before PREV, the remaining
3529 overlays are in correct order. */
3530 while (tail)
3532 XSETMISC (tem, tail);
3533 end = OVERLAY_POSITION (OVERLAY_END (tem));
3535 if (end == pos)
3536 { /* This overlay is disordered. */
3537 struct Lisp_Overlay *found = tail;
3539 /* Unlink the found overlay. */
3540 tail = found->next;
3541 parent->next = tail;
3542 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3543 and link it into the right place. */
3544 if (!right_pair)
3546 found->next = bp->overlays_before;
3547 bp->overlays_before = found;
3549 else
3551 found->next = right_pair->next;
3552 right_pair->next = found;
3555 else if (end == prev)
3557 parent = tail;
3558 tail = tail->next;
3560 else /* No more disordered overlay. */
3561 break;
3565 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3566 doc: /* Return t if OBJECT is an overlay. */)
3567 (Lisp_Object object)
3569 return (OVERLAYP (object) ? Qt : Qnil);
3572 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3573 doc: /* Create a new overlay with range BEG to END in BUFFER.
3574 If omitted, BUFFER defaults to the current buffer.
3575 BEG and END may be integers or markers.
3576 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3577 for the front of the overlay advance when text is inserted there
3578 \(which means the text *is not* included in the overlay).
3579 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3580 for the rear of the overlay advance when text is inserted there
3581 \(which means the text *is* included in the overlay). */)
3582 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3584 Lisp_Object overlay;
3585 struct buffer *b;
3587 if (NILP (buffer))
3588 XSETBUFFER (buffer, current_buffer);
3589 else
3590 CHECK_BUFFER (buffer);
3591 if (MARKERP (beg)
3592 && ! EQ (Fmarker_buffer (beg), buffer))
3593 error ("Marker points into wrong buffer");
3594 if (MARKERP (end)
3595 && ! EQ (Fmarker_buffer (end), buffer))
3596 error ("Marker points into wrong buffer");
3598 CHECK_NUMBER_COERCE_MARKER (beg);
3599 CHECK_NUMBER_COERCE_MARKER (end);
3601 if (XINT (beg) > XINT (end))
3603 Lisp_Object temp;
3604 temp = beg; beg = end; end = temp;
3607 b = XBUFFER (buffer);
3609 beg = Fset_marker (Fmake_marker (), beg, buffer);
3610 end = Fset_marker (Fmake_marker (), end, buffer);
3612 if (!NILP (front_advance))
3613 XMARKER (beg)->insertion_type = 1;
3614 if (!NILP (rear_advance))
3615 XMARKER (end)->insertion_type = 1;
3617 overlay = allocate_misc ();
3618 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3619 XOVERLAY (overlay)->start = beg;
3620 XOVERLAY (overlay)->end = end;
3621 XOVERLAY (overlay)->plist = Qnil;
3622 XOVERLAY (overlay)->next = NULL;
3624 /* Put the new overlay on the wrong list. */
3625 end = OVERLAY_END (overlay);
3626 if (OVERLAY_POSITION (end) < b->overlay_center)
3628 if (b->overlays_after)
3629 XOVERLAY (overlay)->next = b->overlays_after;
3630 b->overlays_after = XOVERLAY (overlay);
3632 else
3634 if (b->overlays_before)
3635 XOVERLAY (overlay)->next = b->overlays_before;
3636 b->overlays_before = XOVERLAY (overlay);
3639 /* This puts it in the right list, and in the right order. */
3640 recenter_overlay_lists (b, b->overlay_center);
3642 /* We don't need to redisplay the region covered by the overlay, because
3643 the overlay has no properties at the moment. */
3645 return overlay;
3648 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3650 static void
3651 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3653 if (start > end)
3655 int temp = start;
3656 start = end;
3657 end = temp;
3660 BUF_COMPUTE_UNCHANGED (buf, start, end);
3662 /* If this is a buffer not in the selected window,
3663 we must do other windows. */
3664 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3665 windows_or_buffers_changed = 1;
3666 /* If multiple windows show this buffer, we must do other windows. */
3667 else if (buffer_shared > 1)
3668 windows_or_buffers_changed = 1;
3669 /* If we modify an overlay at the end of the buffer, we cannot
3670 be sure that window end is still valid. */
3671 else if (end >= ZV && start <= ZV)
3672 windows_or_buffers_changed = 1;
3674 ++BUF_OVERLAY_MODIFF (buf);
3678 Lisp_Object Fdelete_overlay (Lisp_Object overlay);
3680 static struct Lisp_Overlay *
3681 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3683 struct Lisp_Overlay *tmp, *prev;
3684 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3685 if (tmp == overlay)
3687 if (prev)
3688 prev->next = tmp->next;
3689 else
3690 list = tmp->next;
3691 overlay->next = NULL;
3692 break;
3694 return list;
3697 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3698 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3699 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3700 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3701 buffer. */)
3702 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3704 struct buffer *b, *ob;
3705 Lisp_Object obuffer;
3706 int count = SPECPDL_INDEX ();
3708 CHECK_OVERLAY (overlay);
3709 if (NILP (buffer))
3710 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3711 if (NILP (buffer))
3712 XSETBUFFER (buffer, current_buffer);
3713 CHECK_BUFFER (buffer);
3715 if (MARKERP (beg)
3716 && ! EQ (Fmarker_buffer (beg), buffer))
3717 error ("Marker points into wrong buffer");
3718 if (MARKERP (end)
3719 && ! EQ (Fmarker_buffer (end), buffer))
3720 error ("Marker points into wrong buffer");
3722 CHECK_NUMBER_COERCE_MARKER (beg);
3723 CHECK_NUMBER_COERCE_MARKER (end);
3725 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3726 return Fdelete_overlay (overlay);
3728 if (XINT (beg) > XINT (end))
3730 Lisp_Object temp;
3731 temp = beg; beg = end; end = temp;
3734 specbind (Qinhibit_quit, Qt);
3736 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3737 b = XBUFFER (buffer);
3738 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3740 /* If the overlay has changed buffers, do a thorough redisplay. */
3741 if (!EQ (buffer, obuffer))
3743 /* Redisplay where the overlay was. */
3744 if (!NILP (obuffer))
3746 int o_beg;
3747 int o_end;
3749 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3750 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3752 modify_overlay (ob, o_beg, o_end);
3755 /* Redisplay where the overlay is going to be. */
3756 modify_overlay (b, XINT (beg), XINT (end));
3758 else
3759 /* Redisplay the area the overlay has just left, or just enclosed. */
3761 int o_beg, o_end;
3763 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3764 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3766 if (o_beg == XINT (beg))
3767 modify_overlay (b, o_end, XINT (end));
3768 else if (o_end == XINT (end))
3769 modify_overlay (b, o_beg, XINT (beg));
3770 else
3772 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3773 if (XINT (end) > o_end) o_end = XINT (end);
3774 modify_overlay (b, o_beg, o_end);
3778 if (!NILP (obuffer))
3780 ob->overlays_before
3781 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3782 ob->overlays_after
3783 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3784 eassert (XOVERLAY (overlay)->next == NULL);
3787 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3788 Fset_marker (OVERLAY_END (overlay), end, buffer);
3790 /* Put the overlay on the wrong list. */
3791 end = OVERLAY_END (overlay);
3792 if (OVERLAY_POSITION (end) < b->overlay_center)
3794 XOVERLAY (overlay)->next = b->overlays_after;
3795 b->overlays_after = XOVERLAY (overlay);
3797 else
3799 XOVERLAY (overlay)->next = b->overlays_before;
3800 b->overlays_before = XOVERLAY (overlay);
3803 /* This puts it in the right list, and in the right order. */
3804 recenter_overlay_lists (b, b->overlay_center);
3806 return unbind_to (count, overlay);
3809 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3810 doc: /* Delete the overlay OVERLAY from its buffer. */)
3811 (Lisp_Object overlay)
3813 Lisp_Object buffer;
3814 struct buffer *b;
3815 int count = SPECPDL_INDEX ();
3817 CHECK_OVERLAY (overlay);
3819 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3820 if (NILP (buffer))
3821 return Qnil;
3823 b = XBUFFER (buffer);
3824 specbind (Qinhibit_quit, Qt);
3826 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3827 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3828 eassert (XOVERLAY (overlay)->next == NULL);
3829 modify_overlay (b,
3830 marker_position (OVERLAY_START (overlay)),
3831 marker_position (OVERLAY_END (overlay)));
3832 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3833 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3835 /* When deleting an overlay with before or after strings, turn off
3836 display optimizations for the affected buffer, on the basis that
3837 these strings may contain newlines. This is easier to do than to
3838 check for that situation during redisplay. */
3839 if (!windows_or_buffers_changed
3840 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3841 || !NILP (Foverlay_get (overlay, Qafter_string))))
3842 b->prevent_redisplay_optimizations_p = 1;
3844 return unbind_to (count, Qnil);
3847 /* Overlay dissection functions. */
3849 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3850 doc: /* Return the position at which OVERLAY starts. */)
3851 (Lisp_Object overlay)
3853 CHECK_OVERLAY (overlay);
3855 return (Fmarker_position (OVERLAY_START (overlay)));
3858 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3859 doc: /* Return the position at which OVERLAY ends. */)
3860 (Lisp_Object overlay)
3862 CHECK_OVERLAY (overlay);
3864 return (Fmarker_position (OVERLAY_END (overlay)));
3867 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3868 doc: /* Return the buffer OVERLAY belongs to.
3869 Return nil if OVERLAY has been deleted. */)
3870 (Lisp_Object overlay)
3872 CHECK_OVERLAY (overlay);
3874 return Fmarker_buffer (OVERLAY_START (overlay));
3877 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3878 doc: /* Return a list of the properties on OVERLAY.
3879 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3880 OVERLAY. */)
3881 (Lisp_Object overlay)
3883 CHECK_OVERLAY (overlay);
3885 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3889 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3890 doc: /* Return a list of the overlays that contain the character at POS. */)
3891 (Lisp_Object pos)
3893 int noverlays;
3894 Lisp_Object *overlay_vec;
3895 int len;
3896 Lisp_Object result;
3898 CHECK_NUMBER_COERCE_MARKER (pos);
3900 len = 10;
3901 /* We can't use alloca here because overlays_at can call xrealloc. */
3902 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3904 /* Put all the overlays we want in a vector in overlay_vec.
3905 Store the length in len. */
3906 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3907 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
3909 /* Make a list of them all. */
3910 result = Flist (noverlays, overlay_vec);
3912 xfree (overlay_vec);
3913 return result;
3916 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3917 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3918 Overlap means that at least one character is contained within the overlay
3919 and also contained within the specified region.
3920 Empty overlays are included in the result if they are located at BEG,
3921 between BEG and END, or at END provided END denotes the position at the
3922 end of the buffer. */)
3923 (Lisp_Object beg, Lisp_Object end)
3925 int noverlays;
3926 Lisp_Object *overlay_vec;
3927 int len;
3928 Lisp_Object result;
3930 CHECK_NUMBER_COERCE_MARKER (beg);
3931 CHECK_NUMBER_COERCE_MARKER (end);
3933 len = 10;
3934 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3936 /* Put all the overlays we want in a vector in overlay_vec.
3937 Store the length in len. */
3938 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3939 (int *) 0, (int *) 0);
3941 /* Make a list of them all. */
3942 result = Flist (noverlays, overlay_vec);
3944 xfree (overlay_vec);
3945 return result;
3948 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3949 1, 1, 0,
3950 doc: /* Return the next position after POS where an overlay starts or ends.
3951 If there are no overlay boundaries from POS to (point-max),
3952 the value is (point-max). */)
3953 (Lisp_Object pos)
3955 int noverlays;
3956 EMACS_INT endpos;
3957 Lisp_Object *overlay_vec;
3958 int len;
3959 int i;
3961 CHECK_NUMBER_COERCE_MARKER (pos);
3963 len = 10;
3964 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3966 /* Put all the overlays we want in a vector in overlay_vec.
3967 Store the length in len.
3968 endpos gets the position where the next overlay starts. */
3969 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3970 &endpos, (EMACS_INT *) 0, 1);
3972 /* If any of these overlays ends before endpos,
3973 use its ending point instead. */
3974 for (i = 0; i < noverlays; i++)
3976 Lisp_Object oend;
3977 EMACS_INT oendpos;
3979 oend = OVERLAY_END (overlay_vec[i]);
3980 oendpos = OVERLAY_POSITION (oend);
3981 if (oendpos < endpos)
3982 endpos = oendpos;
3985 xfree (overlay_vec);
3986 return make_number (endpos);
3989 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3990 Sprevious_overlay_change, 1, 1, 0,
3991 doc: /* Return the previous position before POS where an overlay starts or ends.
3992 If there are no overlay boundaries from (point-min) to POS,
3993 the value is (point-min). */)
3994 (Lisp_Object pos)
3996 int noverlays;
3997 EMACS_INT prevpos;
3998 Lisp_Object *overlay_vec;
3999 int len;
4001 CHECK_NUMBER_COERCE_MARKER (pos);
4003 /* At beginning of buffer, we know the answer;
4004 avoid bug subtracting 1 below. */
4005 if (XINT (pos) == BEGV)
4006 return pos;
4008 len = 10;
4009 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4011 /* Put all the overlays we want in a vector in overlay_vec.
4012 Store the length in len.
4013 prevpos gets the position of the previous change. */
4014 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4015 (EMACS_INT *) 0, &prevpos, 1);
4017 xfree (overlay_vec);
4018 return make_number (prevpos);
4021 /* These functions are for debugging overlays. */
4023 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4024 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4025 The car has all the overlays before the overlay center;
4026 the cdr has all the overlays after the overlay center.
4027 Recentering overlays moves overlays between these lists.
4028 The lists you get are copies, so that changing them has no effect.
4029 However, the overlays you get are the real objects that the buffer uses. */)
4030 (void)
4032 struct Lisp_Overlay *ol;
4033 Lisp_Object before = Qnil, after = Qnil, tmp;
4034 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4036 XSETMISC (tmp, ol);
4037 before = Fcons (tmp, before);
4039 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4041 XSETMISC (tmp, ol);
4042 after = Fcons (tmp, after);
4044 return Fcons (Fnreverse (before), Fnreverse (after));
4047 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4048 doc: /* Recenter the overlays of the current buffer around position POS.
4049 That makes overlay lookup faster for positions near POS (but perhaps slower
4050 for positions far away from POS). */)
4051 (Lisp_Object pos)
4053 CHECK_NUMBER_COERCE_MARKER (pos);
4055 recenter_overlay_lists (current_buffer, XINT (pos));
4056 return Qnil;
4059 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4060 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4061 (Lisp_Object overlay, Lisp_Object prop)
4063 CHECK_OVERLAY (overlay);
4064 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4067 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4068 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4069 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4071 Lisp_Object tail, buffer;
4072 int changed;
4074 CHECK_OVERLAY (overlay);
4076 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4078 for (tail = XOVERLAY (overlay)->plist;
4079 CONSP (tail) && CONSP (XCDR (tail));
4080 tail = XCDR (XCDR (tail)))
4081 if (EQ (XCAR (tail), prop))
4083 changed = !EQ (XCAR (XCDR (tail)), value);
4084 XSETCAR (XCDR (tail), value);
4085 goto found;
4087 /* It wasn't in the list, so add it to the front. */
4088 changed = !NILP (value);
4089 XOVERLAY (overlay)->plist
4090 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4091 found:
4092 if (! NILP (buffer))
4094 if (changed)
4095 modify_overlay (XBUFFER (buffer),
4096 marker_position (OVERLAY_START (overlay)),
4097 marker_position (OVERLAY_END (overlay)));
4098 if (EQ (prop, Qevaporate) && ! NILP (value)
4099 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4100 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4101 Fdelete_overlay (overlay);
4104 return value;
4107 /* Subroutine of report_overlay_modification. */
4109 /* Lisp vector holding overlay hook functions to call.
4110 Vector elements come in pairs.
4111 Each even-index element is a list of hook functions.
4112 The following odd-index element is the overlay they came from.
4114 Before the buffer change, we fill in this vector
4115 as we call overlay hook functions.
4116 After the buffer change, we get the functions to call from this vector.
4117 This way we always call the same functions before and after the change. */
4118 static Lisp_Object last_overlay_modification_hooks;
4120 /* Number of elements actually used in last_overlay_modification_hooks. */
4121 static int last_overlay_modification_hooks_used;
4123 /* Add one functionlist/overlay pair
4124 to the end of last_overlay_modification_hooks. */
4126 static void
4127 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4129 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4131 if (last_overlay_modification_hooks_used == oldsize)
4132 last_overlay_modification_hooks = larger_vector
4133 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4134 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4135 functionlist); last_overlay_modification_hooks_used++;
4136 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4137 overlay); last_overlay_modification_hooks_used++;
4140 /* Run the modification-hooks of overlays that include
4141 any part of the text in START to END.
4142 If this change is an insertion, also
4143 run the insert-before-hooks of overlay starting at END,
4144 and the insert-after-hooks of overlay ending at START.
4146 This is called both before and after the modification.
4147 AFTER is nonzero when we call after the modification.
4149 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4150 When AFTER is nonzero, they are the start position,
4151 the position after the inserted new text,
4152 and the length of deleted or replaced old text. */
4154 void
4155 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4156 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4158 Lisp_Object prop, overlay;
4159 struct Lisp_Overlay *tail;
4160 /* 1 if this change is an insertion. */
4161 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4162 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4164 overlay = Qnil;
4165 tail = NULL;
4167 /* We used to run the functions as soon as we found them and only register
4168 them in last_overlay_modification_hooks for the purpose of the `after'
4169 case. But running elisp code as we traverse the list of overlays is
4170 painful because the list can be modified by the elisp code so we had to
4171 copy at several places. We now simply do a read-only traversal that
4172 only collects the functions to run and we run them afterwards. It's
4173 simpler, especially since all the code was already there. -stef */
4175 if (!after)
4177 /* We are being called before a change.
4178 Scan the overlays to find the functions to call. */
4179 last_overlay_modification_hooks_used = 0;
4180 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4182 int startpos, endpos;
4183 Lisp_Object ostart, oend;
4185 XSETMISC (overlay, tail);
4187 ostart = OVERLAY_START (overlay);
4188 oend = OVERLAY_END (overlay);
4189 endpos = OVERLAY_POSITION (oend);
4190 if (XFASTINT (start) > endpos)
4191 break;
4192 startpos = OVERLAY_POSITION (ostart);
4193 if (insertion && (XFASTINT (start) == startpos
4194 || XFASTINT (end) == startpos))
4196 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4197 if (!NILP (prop))
4198 add_overlay_mod_hooklist (prop, overlay);
4200 if (insertion && (XFASTINT (start) == endpos
4201 || XFASTINT (end) == endpos))
4203 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4204 if (!NILP (prop))
4205 add_overlay_mod_hooklist (prop, overlay);
4207 /* Test for intersecting intervals. This does the right thing
4208 for both insertion and deletion. */
4209 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4211 prop = Foverlay_get (overlay, Qmodification_hooks);
4212 if (!NILP (prop))
4213 add_overlay_mod_hooklist (prop, overlay);
4217 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4219 int startpos, endpos;
4220 Lisp_Object ostart, oend;
4222 XSETMISC (overlay, tail);
4224 ostart = OVERLAY_START (overlay);
4225 oend = OVERLAY_END (overlay);
4226 startpos = OVERLAY_POSITION (ostart);
4227 endpos = OVERLAY_POSITION (oend);
4228 if (XFASTINT (end) < startpos)
4229 break;
4230 if (insertion && (XFASTINT (start) == startpos
4231 || XFASTINT (end) == startpos))
4233 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4234 if (!NILP (prop))
4235 add_overlay_mod_hooklist (prop, overlay);
4237 if (insertion && (XFASTINT (start) == endpos
4238 || XFASTINT (end) == endpos))
4240 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4241 if (!NILP (prop))
4242 add_overlay_mod_hooklist (prop, overlay);
4244 /* Test for intersecting intervals. This does the right thing
4245 for both insertion and deletion. */
4246 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4248 prop = Foverlay_get (overlay, Qmodification_hooks);
4249 if (!NILP (prop))
4250 add_overlay_mod_hooklist (prop, overlay);
4255 GCPRO4 (overlay, arg1, arg2, arg3);
4257 /* Call the functions recorded in last_overlay_modification_hooks.
4258 First copy the vector contents, in case some of these hooks
4259 do subsequent modification of the buffer. */
4260 int size = last_overlay_modification_hooks_used;
4261 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4262 int i;
4264 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4265 size * sizeof (Lisp_Object));
4266 gcpro1.var = copy;
4267 gcpro1.nvars = size;
4269 for (i = 0; i < size;)
4271 Lisp_Object prop, overlay;
4272 prop = copy[i++];
4273 overlay = copy[i++];
4274 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4277 UNGCPRO;
4280 static void
4281 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4282 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4284 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4286 GCPRO4 (list, arg1, arg2, arg3);
4288 while (CONSP (list))
4290 if (NILP (arg3))
4291 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4292 else
4293 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4294 list = XCDR (list);
4296 UNGCPRO;
4299 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4300 property is set. */
4301 void
4302 evaporate_overlays (EMACS_INT pos)
4304 Lisp_Object overlay, hit_list;
4305 struct Lisp_Overlay *tail;
4307 hit_list = Qnil;
4308 if (pos <= current_buffer->overlay_center)
4309 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4311 int endpos;
4312 XSETMISC (overlay, tail);
4313 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4314 if (endpos < pos)
4315 break;
4316 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4317 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4318 hit_list = Fcons (overlay, hit_list);
4320 else
4321 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4323 int startpos;
4324 XSETMISC (overlay, tail);
4325 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4326 if (startpos > pos)
4327 break;
4328 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4329 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4330 hit_list = Fcons (overlay, hit_list);
4332 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4333 Fdelete_overlay (XCAR (hit_list));
4336 /* Somebody has tried to store a value with an unacceptable type
4337 in the slot with offset OFFSET. */
4339 void
4340 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4342 Lisp_Object predicate;
4344 switch (type)
4346 case_Lisp_Int: predicate = Qintegerp; break;
4347 case Lisp_String: predicate = Qstringp; break;
4348 case Lisp_Symbol: predicate = Qsymbolp; break;
4349 default: abort ();
4352 wrong_type_argument (predicate, newval);
4356 /***********************************************************************
4357 Allocation with mmap
4358 ***********************************************************************/
4360 #ifdef USE_MMAP_FOR_BUFFERS
4362 #include <sys/types.h>
4363 #include <sys/mman.h>
4365 #ifndef MAP_ANON
4366 #ifdef MAP_ANONYMOUS
4367 #define MAP_ANON MAP_ANONYMOUS
4368 #else
4369 #define MAP_ANON 0
4370 #endif
4371 #endif
4373 #ifndef MAP_FAILED
4374 #define MAP_FAILED ((void *) -1)
4375 #endif
4377 #include <stdio.h>
4379 #if MAP_ANON == 0
4380 #include <fcntl.h>
4381 #endif
4383 #include "coding.h"
4386 /* Memory is allocated in regions which are mapped using mmap(2).
4387 The current implementation lets the system select mapped
4388 addresses; we're not using MAP_FIXED in general, except when
4389 trying to enlarge regions.
4391 Each mapped region starts with a mmap_region structure, the user
4392 area starts after that structure, aligned to MEM_ALIGN.
4394 +-----------------------+
4395 | struct mmap_info + |
4396 | padding |
4397 +-----------------------+
4398 | user data |
4401 +-----------------------+ */
4403 struct mmap_region
4405 /* User-specified size. */
4406 size_t nbytes_specified;
4408 /* Number of bytes mapped */
4409 size_t nbytes_mapped;
4411 /* Pointer to the location holding the address of the memory
4412 allocated with the mmap'd block. The variable actually points
4413 after this structure. */
4414 POINTER_TYPE **var;
4416 /* Next and previous in list of all mmap'd regions. */
4417 struct mmap_region *next, *prev;
4420 /* Doubly-linked list of mmap'd regions. */
4422 static struct mmap_region *mmap_regions;
4424 /* File descriptor for mmap. If we don't have anonymous mapping,
4425 /dev/zero will be opened on it. */
4427 static int mmap_fd;
4429 /* Temporary storage for mmap_set_vars, see there. */
4431 static struct mmap_region *mmap_regions_1;
4432 static int mmap_fd_1;
4434 /* Page size on this system. */
4436 static int mmap_page_size;
4438 /* 1 means mmap has been intialized. */
4440 static int mmap_initialized_p;
4442 /* Value is X rounded up to the next multiple of N. */
4444 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4446 /* Size of mmap_region structure plus padding. */
4448 #define MMAP_REGION_STRUCT_SIZE \
4449 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4451 /* Given a pointer P to the start of the user-visible part of a mapped
4452 region, return a pointer to the start of the region. */
4454 #define MMAP_REGION(P) \
4455 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4457 /* Given a pointer P to the start of a mapped region, return a pointer
4458 to the start of the user-visible part of the region. */
4460 #define MMAP_USER_AREA(P) \
4461 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4463 #define MEM_ALIGN sizeof (double)
4465 /* Predicate returning true if part of the address range [START .. END]
4466 is currently mapped. Used to prevent overwriting an existing
4467 memory mapping.
4469 Default is to conservativly assume the address range is occupied by
4470 something else. This can be overridden by system configuration
4471 files if system-specific means to determine this exists. */
4473 #ifndef MMAP_ALLOCATED_P
4474 #define MMAP_ALLOCATED_P(start, end) 1
4475 #endif
4477 /* Function prototypes. */
4479 static int mmap_free_1 (struct mmap_region *);
4480 static int mmap_enlarge (struct mmap_region *, int);
4481 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4482 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4483 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4484 static void mmap_free (POINTER_TYPE **ptr);
4485 static void mmap_init (void);
4488 /* Return a region overlapping address range START...END, or null if
4489 none. END is not including, i.e. the last byte in the range
4490 is at END - 1. */
4492 static struct mmap_region *
4493 mmap_find (start, end)
4494 POINTER_TYPE *start, *end;
4496 struct mmap_region *r;
4497 char *s = (char *) start, *e = (char *) end;
4499 for (r = mmap_regions; r; r = r->next)
4501 char *rstart = (char *) r;
4502 char *rend = rstart + r->nbytes_mapped;
4504 if (/* First byte of range, i.e. START, in this region? */
4505 (s >= rstart && s < rend)
4506 /* Last byte of range, i.e. END - 1, in this region? */
4507 || (e > rstart && e <= rend)
4508 /* First byte of this region in the range? */
4509 || (rstart >= s && rstart < e)
4510 /* Last byte of this region in the range? */
4511 || (rend > s && rend <= e))
4512 break;
4515 return r;
4519 /* Unmap a region. P is a pointer to the start of the user-araa of
4520 the region. Value is non-zero if successful. */
4522 static int
4523 mmap_free_1 (r)
4524 struct mmap_region *r;
4526 if (r->next)
4527 r->next->prev = r->prev;
4528 if (r->prev)
4529 r->prev->next = r->next;
4530 else
4531 mmap_regions = r->next;
4533 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4535 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4536 return 0;
4539 return 1;
4543 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4544 Value is non-zero if successful. */
4546 static int
4547 mmap_enlarge (r, npages)
4548 struct mmap_region *r;
4549 int npages;
4551 char *region_end = (char *) r + r->nbytes_mapped;
4552 size_t nbytes;
4553 int success = 0;
4555 if (npages < 0)
4557 /* Unmap pages at the end of the region. */
4558 nbytes = - npages * mmap_page_size;
4559 if (munmap (region_end - nbytes, nbytes) == -1)
4560 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4561 else
4563 r->nbytes_mapped -= nbytes;
4564 success = 1;
4567 else if (npages > 0)
4569 nbytes = npages * mmap_page_size;
4571 /* Try to map additional pages at the end of the region. We
4572 cannot do this if the address range is already occupied by
4573 something else because mmap deletes any previous mapping.
4574 I'm not sure this is worth doing, let's see. */
4575 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4577 POINTER_TYPE *p;
4579 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4580 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4581 if (p == MAP_FAILED)
4582 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4583 else if (p != (POINTER_TYPE *) region_end)
4585 /* Kernels are free to choose a different address. In
4586 that case, unmap what we've mapped above; we have
4587 no use for it. */
4588 if (munmap (p, nbytes) == -1)
4589 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4591 else
4593 r->nbytes_mapped += nbytes;
4594 success = 1;
4599 return success;
4603 /* Set or reset variables holding references to mapped regions. If
4604 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4605 non-zero, set all variables to the start of the user-areas
4606 of mapped regions.
4608 This function is called from Fdump_emacs to ensure that the dumped
4609 Emacs doesn't contain references to memory that won't be mapped
4610 when Emacs starts. */
4612 void
4613 mmap_set_vars (restore_p)
4614 int restore_p;
4616 struct mmap_region *r;
4618 if (restore_p)
4620 mmap_regions = mmap_regions_1;
4621 mmap_fd = mmap_fd_1;
4622 for (r = mmap_regions; r; r = r->next)
4623 *r->var = MMAP_USER_AREA (r);
4625 else
4627 for (r = mmap_regions; r; r = r->next)
4628 *r->var = NULL;
4629 mmap_regions_1 = mmap_regions;
4630 mmap_regions = NULL;
4631 mmap_fd_1 = mmap_fd;
4632 mmap_fd = -1;
4637 /* Allocate a block of storage large enough to hold NBYTES bytes of
4638 data. A pointer to the data is returned in *VAR. VAR is thus the
4639 address of some variable which will use the data area.
4641 The allocation of 0 bytes is valid.
4643 If we can't allocate the necessary memory, set *VAR to null, and
4644 return null. */
4646 static POINTER_TYPE *
4647 mmap_alloc (var, nbytes)
4648 POINTER_TYPE **var;
4649 size_t nbytes;
4651 void *p;
4652 size_t map;
4654 mmap_init ();
4656 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4657 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4658 mmap_fd, 0);
4660 if (p == MAP_FAILED)
4662 if (errno != ENOMEM)
4663 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4664 p = NULL;
4666 else
4668 struct mmap_region *r = (struct mmap_region *) p;
4670 r->nbytes_specified = nbytes;
4671 r->nbytes_mapped = map;
4672 r->var = var;
4673 r->prev = NULL;
4674 r->next = mmap_regions;
4675 if (r->next)
4676 r->next->prev = r;
4677 mmap_regions = r;
4679 p = MMAP_USER_AREA (p);
4682 return *var = p;
4686 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4687 resize it to size NBYTES. Change *VAR to reflect the new block,
4688 and return this value. If more memory cannot be allocated, then
4689 leave *VAR unchanged, and return null. */
4691 static POINTER_TYPE *
4692 mmap_realloc (var, nbytes)
4693 POINTER_TYPE **var;
4694 size_t nbytes;
4696 POINTER_TYPE *result;
4698 mmap_init ();
4700 if (*var == NULL)
4701 result = mmap_alloc (var, nbytes);
4702 else if (nbytes == 0)
4704 mmap_free (var);
4705 result = mmap_alloc (var, nbytes);
4707 else
4709 struct mmap_region *r = MMAP_REGION (*var);
4710 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4712 if (room < nbytes)
4714 /* Must enlarge. */
4715 POINTER_TYPE *old_ptr = *var;
4717 /* Try to map additional pages at the end of the region.
4718 If that fails, allocate a new region, copy data
4719 from the old region, then free it. */
4720 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4721 / mmap_page_size)))
4723 r->nbytes_specified = nbytes;
4724 *var = result = old_ptr;
4726 else if (mmap_alloc (var, nbytes))
4728 memcpy (*var, old_ptr, r->nbytes_specified);
4729 mmap_free_1 (MMAP_REGION (old_ptr));
4730 result = *var;
4731 r = MMAP_REGION (result);
4732 r->nbytes_specified = nbytes;
4734 else
4736 *var = old_ptr;
4737 result = NULL;
4740 else if (room - nbytes >= mmap_page_size)
4742 /* Shrinking by at least a page. Let's give some
4743 memory back to the system.
4745 The extra parens are to make the division happens first,
4746 on positive values, so we know it will round towards
4747 zero. */
4748 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4749 result = *var;
4750 r->nbytes_specified = nbytes;
4752 else
4754 /* Leave it alone. */
4755 result = *var;
4756 r->nbytes_specified = nbytes;
4760 return result;
4764 /* Free a block of relocatable storage whose data is pointed to by
4765 PTR. Store 0 in *PTR to show there's no block allocated. */
4767 static void
4768 mmap_free (var)
4769 POINTER_TYPE **var;
4771 mmap_init ();
4773 if (*var)
4775 mmap_free_1 (MMAP_REGION (*var));
4776 *var = NULL;
4781 /* Perform necessary intializations for the use of mmap. */
4783 static void
4784 mmap_init ()
4786 #if MAP_ANON == 0
4787 /* The value of mmap_fd is initially 0 in temacs, and -1
4788 in a dumped Emacs. */
4789 if (mmap_fd <= 0)
4791 /* No anonymous mmap -- we need the file descriptor. */
4792 mmap_fd = open ("/dev/zero", O_RDONLY);
4793 if (mmap_fd == -1)
4794 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4796 #endif /* MAP_ANON == 0 */
4798 if (mmap_initialized_p)
4799 return;
4800 mmap_initialized_p = 1;
4802 #if MAP_ANON != 0
4803 mmap_fd = -1;
4804 #endif
4806 mmap_page_size = getpagesize ();
4809 #endif /* USE_MMAP_FOR_BUFFERS */
4813 /***********************************************************************
4814 Buffer-text Allocation
4815 ***********************************************************************/
4817 #ifdef REL_ALLOC
4818 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4819 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4820 extern void r_alloc_free (POINTER_TYPE **ptr);
4821 #endif /* REL_ALLOC */
4824 /* Allocate NBYTES bytes for buffer B's text buffer. */
4826 static void
4827 alloc_buffer_text (struct buffer *b, size_t nbytes)
4829 POINTER_TYPE *p;
4831 BLOCK_INPUT;
4832 #if defined USE_MMAP_FOR_BUFFERS
4833 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4834 #elif defined REL_ALLOC
4835 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4836 #else
4837 p = xmalloc (nbytes);
4838 #endif
4840 if (p == NULL)
4842 UNBLOCK_INPUT;
4843 memory_full ();
4846 b->text->beg = (unsigned char *) p;
4847 UNBLOCK_INPUT;
4850 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4851 shrink it. */
4853 void
4854 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4856 POINTER_TYPE *p;
4857 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4858 + delta);
4859 BLOCK_INPUT;
4860 #if defined USE_MMAP_FOR_BUFFERS
4861 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4862 #elif defined REL_ALLOC
4863 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4864 #else
4865 p = xrealloc (b->text->beg, nbytes);
4866 #endif
4868 if (p == NULL)
4870 UNBLOCK_INPUT;
4871 memory_full ();
4874 BUF_BEG_ADDR (b) = (unsigned char *) p;
4875 UNBLOCK_INPUT;
4879 /* Free buffer B's text buffer. */
4881 static void
4882 free_buffer_text (struct buffer *b)
4884 BLOCK_INPUT;
4886 #if defined USE_MMAP_FOR_BUFFERS
4887 mmap_free ((POINTER_TYPE **) &b->text->beg);
4888 #elif defined REL_ALLOC
4889 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4890 #else
4891 xfree (b->text->beg);
4892 #endif
4894 BUF_BEG_ADDR (b) = NULL;
4895 UNBLOCK_INPUT;
4900 /***********************************************************************
4901 Initialization
4902 ***********************************************************************/
4904 void
4905 init_buffer_once (void)
4907 int idx;
4909 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
4911 /* Make sure all markable slots in buffer_defaults
4912 are initialized reasonably, so mark_buffer won't choke. */
4913 reset_buffer (&buffer_defaults);
4914 eassert (EQ (buffer_defaults.name, make_number (0)));
4915 reset_buffer_local_variables (&buffer_defaults, 1);
4916 eassert (EQ (buffer_local_symbols.name, make_number (0)));
4917 reset_buffer (&buffer_local_symbols);
4918 reset_buffer_local_variables (&buffer_local_symbols, 1);
4919 /* Prevent GC from getting confused. */
4920 buffer_defaults.text = &buffer_defaults.own_text;
4921 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4922 BUF_INTERVALS (&buffer_defaults) = 0;
4923 BUF_INTERVALS (&buffer_local_symbols) = 0;
4924 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
4925 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4926 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
4927 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4929 /* Set up the default values of various buffer slots. */
4930 /* Must do these before making the first buffer! */
4932 /* real setup is done in bindings.el */
4933 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
4934 buffer_defaults.header_line_format = Qnil;
4935 buffer_defaults.abbrev_mode = Qnil;
4936 buffer_defaults.overwrite_mode = Qnil;
4937 buffer_defaults.case_fold_search = Qt;
4938 buffer_defaults.auto_fill_function = Qnil;
4939 buffer_defaults.selective_display = Qnil;
4940 #ifndef old
4941 buffer_defaults.selective_display_ellipses = Qt;
4942 #endif
4943 buffer_defaults.abbrev_table = Qnil;
4944 buffer_defaults.display_table = Qnil;
4945 buffer_defaults.undo_list = Qnil;
4946 buffer_defaults.mark_active = Qnil;
4947 buffer_defaults.file_format = Qnil;
4948 buffer_defaults.auto_save_file_format = Qt;
4949 buffer_defaults.overlays_before = NULL;
4950 buffer_defaults.overlays_after = NULL;
4951 buffer_defaults.overlay_center = BEG;
4953 XSETFASTINT (buffer_defaults.tab_width, 8);
4954 buffer_defaults.truncate_lines = Qnil;
4955 buffer_defaults.word_wrap = Qnil;
4956 buffer_defaults.ctl_arrow = Qt;
4957 buffer_defaults.bidi_display_reordering = Qnil;
4958 buffer_defaults.bidi_paragraph_direction = Qnil;
4959 buffer_defaults.cursor_type = Qt;
4960 buffer_defaults.extra_line_spacing = Qnil;
4961 buffer_defaults.cursor_in_non_selected_windows = Qt;
4963 #ifdef DOS_NT
4964 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
4965 #endif
4966 buffer_defaults.enable_multibyte_characters = Qt;
4967 buffer_defaults.buffer_file_coding_system = Qnil;
4968 XSETFASTINT (buffer_defaults.fill_column, 70);
4969 XSETFASTINT (buffer_defaults.left_margin, 0);
4970 buffer_defaults.cache_long_line_scans = Qnil;
4971 buffer_defaults.file_truename = Qnil;
4972 XSETFASTINT (buffer_defaults.display_count, 0);
4973 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
4974 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
4975 buffer_defaults.left_fringe_width = Qnil;
4976 buffer_defaults.right_fringe_width = Qnil;
4977 buffer_defaults.fringes_outside_margins = Qnil;
4978 buffer_defaults.scroll_bar_width = Qnil;
4979 buffer_defaults.vertical_scroll_bar_type = Qt;
4980 buffer_defaults.indicate_empty_lines = Qnil;
4981 buffer_defaults.indicate_buffer_boundaries = Qnil;
4982 buffer_defaults.fringe_indicator_alist = Qnil;
4983 buffer_defaults.fringe_cursor_alist = Qnil;
4984 buffer_defaults.scroll_up_aggressively = Qnil;
4985 buffer_defaults.scroll_down_aggressively = Qnil;
4986 buffer_defaults.display_time = Qnil;
4988 /* Assign the local-flags to the slots that have default values.
4989 The local flag is a bit that is used in the buffer
4990 to say that it has its own local value for the slot.
4991 The local flag bits are in the local_var_flags slot of the buffer. */
4993 /* Nothing can work if this isn't true */
4994 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
4996 /* 0 means not a lisp var, -1 means always local, else mask */
4997 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
4998 XSETINT (buffer_local_flags.filename, -1);
4999 XSETINT (buffer_local_flags.directory, -1);
5000 XSETINT (buffer_local_flags.backed_up, -1);
5001 XSETINT (buffer_local_flags.save_length, -1);
5002 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5003 XSETINT (buffer_local_flags.read_only, -1);
5004 XSETINT (buffer_local_flags.major_mode, -1);
5005 XSETINT (buffer_local_flags.mode_name, -1);
5006 XSETINT (buffer_local_flags.undo_list, -1);
5007 XSETINT (buffer_local_flags.mark_active, -1);
5008 XSETINT (buffer_local_flags.point_before_scroll, -1);
5009 XSETINT (buffer_local_flags.file_truename, -1);
5010 XSETINT (buffer_local_flags.invisibility_spec, -1);
5011 XSETINT (buffer_local_flags.file_format, -1);
5012 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5013 XSETINT (buffer_local_flags.display_count, -1);
5014 XSETINT (buffer_local_flags.display_time, -1);
5015 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5017 idx = 1;
5018 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5019 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5020 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5021 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5022 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5023 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5024 #ifndef old
5025 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5026 #endif
5027 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5028 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5029 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5030 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5031 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5032 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5033 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5034 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5035 #ifdef DOS_NT
5036 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5037 /* Make this one a permanent local. */
5038 buffer_permanent_local_flags[idx++] = 1;
5039 #endif
5040 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5041 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5042 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5043 XSETFASTINT (buffer_local_flags.bidi_display_reordering, idx); ++idx;
5044 XSETFASTINT (buffer_local_flags.bidi_paragraph_direction, idx); ++idx;
5045 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5046 /* Make this one a permanent local. */
5047 buffer_permanent_local_flags[idx++] = 1;
5048 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5049 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5050 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5051 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5052 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5053 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5054 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5055 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5056 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5057 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5058 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5059 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5060 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5061 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5062 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5063 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5064 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5066 /* Need more room? */
5067 if (idx >= MAX_PER_BUFFER_VARS)
5068 abort ();
5069 last_per_buffer_idx = idx;
5071 Vbuffer_alist = Qnil;
5072 current_buffer = 0;
5073 all_buffers = 0;
5075 QSFundamental = make_pure_c_string ("Fundamental");
5077 Qfundamental_mode = intern_c_string ("fundamental-mode");
5078 buffer_defaults.major_mode = Qfundamental_mode;
5080 Qmode_class = intern_c_string ("mode-class");
5082 Qprotected_field = intern_c_string ("protected-field");
5084 Qpermanent_local = intern_c_string ("permanent-local");
5086 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5087 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5089 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5091 /* super-magic invisible buffer */
5092 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5093 Vbuffer_alist = Qnil;
5095 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5097 inhibit_modification_hooks = 0;
5100 void
5101 init_buffer (void)
5103 char *pwd;
5104 Lisp_Object temp;
5105 int len;
5107 #ifdef USE_MMAP_FOR_BUFFERS
5109 /* When using the ralloc implementation based on mmap(2), buffer
5110 text pointers will have been set to null in the dumped Emacs.
5111 Map new memory. */
5112 struct buffer *b;
5114 for (b = all_buffers; b; b = b->next)
5115 if (b->text->beg == NULL)
5116 enlarge_buffer_text (b, 0);
5118 #endif /* USE_MMAP_FOR_BUFFERS */
5120 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5121 if (NILP (buffer_defaults.enable_multibyte_characters))
5122 Fset_buffer_multibyte (Qnil);
5124 pwd = get_current_dir_name ();
5126 if (!pwd)
5127 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5129 /* Maybe this should really use some standard subroutine
5130 whose definition is filename syntax dependent. */
5131 len = strlen (pwd);
5132 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5134 /* Grow buffer to add directory separator and '\0'. */
5135 pwd = (char *) realloc (pwd, len + 2);
5136 if (!pwd)
5137 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5138 pwd[len] = DIRECTORY_SEP;
5139 pwd[len + 1] = '\0';
5142 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5143 if (! NILP (buffer_defaults.enable_multibyte_characters))
5144 /* At this moment, we still don't know how to decode the
5145 directory name. So, we keep the bytes in multibyte form so
5146 that ENCODE_FILE correctly gets the original bytes. */
5147 current_buffer->directory
5148 = string_to_multibyte (current_buffer->directory);
5150 /* Add /: to the front of the name
5151 if it would otherwise be treated as magic. */
5152 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5153 if (! NILP (temp)
5154 /* If the default dir is just /, TEMP is non-nil
5155 because of the ange-ftp completion handler.
5156 However, it is not necessary to turn / into /:/.
5157 So avoid doing that. */
5158 && strcmp ("/", SDATA (current_buffer->directory)))
5159 current_buffer->directory
5160 = concat2 (build_string ("/:"), current_buffer->directory);
5162 temp = get_minibuffer (0);
5163 XBUFFER (temp)->directory = current_buffer->directory;
5165 free (pwd);
5168 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5169 Object stored in the current buffer. address is the address of the slot
5170 in the buffer that is current now. */
5172 /* TYPE is nil for a general Lisp variable.
5173 An integer specifies a type; then only Lisp values
5174 with that type code are allowed (except that nil is allowed too).
5175 LNAME is the Lisp-level variable name.
5176 VNAME is the name of the buffer slot.
5177 DOC is a dummy where you write the doc string as a comment. */
5178 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5179 do { \
5180 static struct Lisp_Buffer_Objfwd bo_fwd; \
5181 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5182 } while (0)
5184 static void
5185 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, char *namestring,
5186 Lisp_Object *address, Lisp_Object type, char *doc)
5188 struct Lisp_Symbol *sym;
5189 int offset;
5191 sym = XSYMBOL (intern (namestring));
5192 offset = (char *)address - (char *)current_buffer;
5194 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5195 bo_fwd->offset = offset;
5196 bo_fwd->slottype = type;
5197 sym->redirect = SYMBOL_FORWARDED;
5199 /* I tried to do the job without a cast, but it seems impossible.
5200 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5201 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5203 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5205 if (PER_BUFFER_IDX (offset) == 0)
5206 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5207 slot of buffer_local_flags */
5208 abort ();
5212 /* initialize the buffer routines */
5213 void
5214 syms_of_buffer (void)
5216 staticpro (&last_overlay_modification_hooks);
5217 last_overlay_modification_hooks
5218 = Fmake_vector (make_number (10), Qnil);
5220 staticpro (&Vbuffer_defaults);
5221 staticpro (&Vbuffer_local_symbols);
5222 staticpro (&Qfundamental_mode);
5223 staticpro (&Qmode_class);
5224 staticpro (&QSFundamental);
5225 staticpro (&Vbuffer_alist);
5226 staticpro (&Qprotected_field);
5227 staticpro (&Qpermanent_local);
5228 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5229 staticpro (&Qpermanent_local_hook);
5230 staticpro (&Qkill_buffer_hook);
5231 Qoverlayp = intern_c_string ("overlayp");
5232 staticpro (&Qoverlayp);
5233 Qevaporate = intern_c_string ("evaporate");
5234 staticpro (&Qevaporate);
5235 Qmodification_hooks = intern_c_string ("modification-hooks");
5236 staticpro (&Qmodification_hooks);
5237 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5238 staticpro (&Qinsert_in_front_hooks);
5239 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5240 staticpro (&Qinsert_behind_hooks);
5241 Qget_file_buffer = intern_c_string ("get-file-buffer");
5242 staticpro (&Qget_file_buffer);
5243 Qpriority = intern_c_string ("priority");
5244 staticpro (&Qpriority);
5245 Qwindow = intern_c_string ("window");
5246 staticpro (&Qwindow);
5247 Qbefore_string = intern_c_string ("before-string");
5248 staticpro (&Qbefore_string);
5249 Qafter_string = intern_c_string ("after-string");
5250 staticpro (&Qafter_string);
5251 Qfirst_change_hook = intern_c_string ("first-change-hook");
5252 staticpro (&Qfirst_change_hook);
5253 Qbefore_change_functions = intern_c_string ("before-change-functions");
5254 staticpro (&Qbefore_change_functions);
5255 Qafter_change_functions = intern_c_string ("after-change-functions");
5256 staticpro (&Qafter_change_functions);
5257 /* The next one is initialized in init_buffer_once. */
5258 staticpro (&Qucs_set_table_for_input);
5260 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5261 staticpro (&Qkill_buffer_query_functions);
5263 Fput (Qprotected_field, Qerror_conditions,
5264 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5265 Fput (Qprotected_field, Qerror_message,
5266 make_pure_c_string ("Attempt to modify a protected field"));
5268 /* All these use DEFVAR_LISP_NOPRO because the slots in
5269 buffer_defaults will all be marked via Vbuffer_defaults. */
5271 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5272 &buffer_defaults.mode_line_format,
5273 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5274 This is the same as (default-value 'mode-line-format). */);
5276 DEFVAR_LISP_NOPRO ("default-header-line-format",
5277 &buffer_defaults.header_line_format,
5278 doc: /* Default value of `header-line-format' for buffers that don't override it.
5279 This is the same as (default-value 'header-line-format). */);
5281 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5282 doc: /* Default value of `cursor-type' for buffers that don't override it.
5283 This is the same as (default-value 'cursor-type). */);
5285 DEFVAR_LISP_NOPRO ("default-line-spacing",
5286 &buffer_defaults.extra_line_spacing,
5287 doc: /* Default value of `line-spacing' for buffers that don't override it.
5288 This is the same as (default-value 'line-spacing). */);
5290 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5291 &buffer_defaults.cursor_in_non_selected_windows,
5292 doc: /* Default value of `cursor-in-non-selected-windows'.
5293 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5295 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5296 &buffer_defaults.abbrev_mode,
5297 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5298 This is the same as (default-value 'abbrev-mode). */);
5300 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5301 &buffer_defaults.ctl_arrow,
5302 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5303 This is the same as (default-value 'ctl-arrow). */);
5305 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5306 &buffer_defaults.enable_multibyte_characters,
5307 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5308 This is the same as (default-value 'enable-multibyte-characters). */);
5310 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5311 &buffer_defaults.buffer_file_coding_system,
5312 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5313 This is the same as (default-value 'buffer-file-coding-system). */);
5315 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5316 &buffer_defaults.truncate_lines,
5317 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5318 This is the same as (default-value 'truncate-lines). */);
5320 DEFVAR_LISP_NOPRO ("default-fill-column",
5321 &buffer_defaults.fill_column,
5322 doc: /* Default value of `fill-column' for buffers that do not override it.
5323 This is the same as (default-value 'fill-column). */);
5325 DEFVAR_LISP_NOPRO ("default-left-margin",
5326 &buffer_defaults.left_margin,
5327 doc: /* Default value of `left-margin' for buffers that do not override it.
5328 This is the same as (default-value 'left-margin). */);
5330 DEFVAR_LISP_NOPRO ("default-tab-width",
5331 &buffer_defaults.tab_width,
5332 doc: /* Default value of `tab-width' for buffers that do not override it.
5333 This is the same as (default-value 'tab-width). */);
5335 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5336 &buffer_defaults.case_fold_search,
5337 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5338 This is the same as (default-value 'case-fold-search). */);
5340 #ifdef DOS_NT
5341 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5342 &buffer_defaults.buffer_file_type,
5343 doc: /* Default file type for buffers that do not override it.
5344 This is the same as (default-value 'buffer-file-type).
5345 The file type is nil for text, t for binary. */);
5346 #endif
5348 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5349 &buffer_defaults.left_margin_cols,
5350 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5351 This is the same as (default-value 'left-margin-width). */);
5353 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5354 &buffer_defaults.right_margin_cols,
5355 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5356 This is the same as (default-value 'right-margin-width). */);
5358 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5359 &buffer_defaults.left_fringe_width,
5360 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5361 This is the same as (default-value 'left-fringe-width). */);
5363 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5364 &buffer_defaults.right_fringe_width,
5365 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5366 This is the same as (default-value 'right-fringe-width). */);
5368 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5369 &buffer_defaults.fringes_outside_margins,
5370 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5371 This is the same as (default-value 'fringes-outside-margins). */);
5373 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5374 &buffer_defaults.scroll_bar_width,
5375 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5376 This is the same as (default-value 'scroll-bar-width). */);
5378 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5379 &buffer_defaults.vertical_scroll_bar_type,
5380 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5381 This is the same as (default-value 'vertical-scroll-bar). */);
5383 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5384 &buffer_defaults.indicate_empty_lines,
5385 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5386 This is the same as (default-value 'indicate-empty-lines). */);
5388 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5389 &buffer_defaults.indicate_buffer_boundaries,
5390 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5391 This is the same as (default-value 'indicate-buffer-boundaries). */);
5393 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5394 &buffer_defaults.fringe_indicator_alist,
5395 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5396 This is the same as (default-value 'fringe-indicator-alist'). */);
5398 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5399 &buffer_defaults.fringe_cursor_alist,
5400 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5401 This is the same as (default-value 'fringe-cursor-alist'). */);
5403 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5404 &buffer_defaults.scroll_up_aggressively,
5405 doc: /* Default value of `scroll-up-aggressively'.
5406 This value applies in buffers that don't have their own local values.
5407 This is the same as (default-value 'scroll-up-aggressively). */);
5409 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5410 &buffer_defaults.scroll_down_aggressively,
5411 doc: /* Default value of `scroll-down-aggressively'.
5412 This value applies in buffers that don't have their own local values.
5413 This is the same as (default-value 'scroll-down-aggressively). */);
5415 DEFVAR_PER_BUFFER ("header-line-format",
5416 &current_buffer->header_line_format,
5417 Qnil,
5418 doc: /* Analogous to `mode-line-format', but controls the header line.
5419 The header line appears, optionally, at the top of a window;
5420 the mode line appears at the bottom. */);
5422 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5423 Qnil,
5424 doc: /* Template for displaying mode line for current buffer.
5425 Each buffer has its own value of this variable.
5426 Value may be nil, a string, a symbol or a list or cons cell.
5427 A value of nil means don't display a mode line.
5428 For a symbol, its value is used (but it is ignored if t or nil).
5429 A string appearing directly as the value of a symbol is processed verbatim
5430 in that the %-constructs below are not recognized.
5431 Note that unless the symbol is marked as a `risky-local-variable', all
5432 properties in any strings, as well as all :eval and :propertize forms
5433 in the value of that symbol will be ignored.
5434 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5435 is used as a mode line element. Be careful--FORM should not load any files,
5436 because that can cause an infinite recursion.
5437 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5438 with the specified properties PROPS applied.
5439 For a list whose car is a symbol, the symbol's value is taken,
5440 and if that is non-nil, the cadr of the list is processed recursively.
5441 Otherwise, the caddr of the list (if there is one) is processed.
5442 For a list whose car is a string or list, each element is processed
5443 recursively and the results are effectively concatenated.
5444 For a list whose car is an integer, the cdr of the list is processed
5445 and padded (if the number is positive) or truncated (if negative)
5446 to the width specified by that number.
5447 A string is printed verbatim in the mode line except for %-constructs:
5448 (%-constructs are allowed when the string is the entire mode-line-format
5449 or when it is found in a cons-cell or a list)
5450 %b -- print buffer name. %f -- print visited file name.
5451 %F -- print frame name.
5452 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5453 %& is like %*, but ignore read-only-ness.
5454 % means buffer is read-only and * means it is modified.
5455 For a modified read-only buffer, %* gives % and %+ gives *.
5456 %s -- print process status. %l -- print the current line number.
5457 %c -- print the current column number (this makes editing slower).
5458 To make the column number update correctly in all cases,
5459 `column-number-mode' must be non-nil.
5460 %i -- print the size of the buffer.
5461 %I -- like %i, but use k, M, G, etc., to abbreviate.
5462 %p -- print percent of buffer above top of window, or Top, Bot or All.
5463 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5464 or print Bottom or All.
5465 %n -- print Narrow if appropriate.
5466 %t -- visited file is text or binary (if OS supports this distinction).
5467 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5468 %Z -- like %z, but including the end-of-line format.
5469 %e -- print error message about full memory.
5470 %@ -- print @ or hyphen. @ means that default-directory is on a
5471 remote machine.
5472 %[ -- print one [ for each recursive editing level. %] similar.
5473 %% -- print %. %- -- print infinitely many dashes.
5474 Decimal digits after the % specify field width to which to pad. */);
5476 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5477 doc: /* *Value of `major-mode' for new buffers. */);
5479 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5480 make_number (Lisp_Symbol),
5481 doc: /* Symbol for current buffer's major mode.
5482 The default value (normally `fundamental-mode') affects new buffers.
5483 A value of nil means to use the current buffer's major mode, provided
5484 it is not marked as "special".
5486 When a mode is used by default, `find-file' switches to it before it
5487 reads the contents into the buffer and before it finishes setting up
5488 the buffer. Thus, the mode and its hooks should not expect certain
5489 variables such as `buffer-read-only' and `buffer-file-coding-system'
5490 to be set up. */);
5492 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5493 Qnil,
5494 doc: /* Pretty name of current buffer's major mode.
5495 Usually a string, but can use any of the constructs for `mode-line-format',
5496 which see.
5497 Format with `format-mode-line' to produce a string value. */);
5499 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5500 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5502 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5503 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5505 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5506 Qnil,
5507 doc: /* *Non-nil if searches and matches should ignore case. */);
5509 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5510 make_number (LISP_INT_TAG),
5511 doc: /* *Column beyond which automatic line-wrapping should happen.
5512 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5514 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5515 make_number (LISP_INT_TAG),
5516 doc: /* *Column for the default `indent-line-function' to indent to.
5517 Linefeed indents to this column in Fundamental mode. */);
5519 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5520 make_number (LISP_INT_TAG),
5521 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5523 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5524 doc: /* *Non-nil means display control chars with uparrow.
5525 A value of nil means use backslash and octal digits.
5526 This variable does not apply to characters whose display is specified
5527 in the current display table (if there is one). */);
5529 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5530 &current_buffer->enable_multibyte_characters,
5531 Qnil,
5532 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5533 Otherwise they are regarded as unibyte. This affects the display,
5534 file I/O and the behavior of various editing commands.
5536 This variable is buffer-local but you cannot set it directly;
5537 use the function `set-buffer-multibyte' to change a buffer's representation.
5538 Changing its default value with `setq-default' is supported.
5539 See also variable `default-enable-multibyte-characters' and Info node
5540 `(elisp)Text Representations'. */);
5541 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5543 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5544 &current_buffer->buffer_file_coding_system, Qnil,
5545 doc: /* Coding system to be used for encoding the buffer contents on saving.
5546 This variable applies to saving the buffer, and also to `write-region'
5547 and other functions that use `write-region'.
5548 It does not apply to sending output to subprocesses, however.
5550 If this is nil, the buffer is saved without any code conversion
5551 unless some coding system is specified in `file-coding-system-alist'
5552 for the buffer file.
5554 If the text to be saved cannot be encoded as specified by this variable,
5555 an alternative encoding is selected by `select-safe-coding-system', which see.
5557 The variable `coding-system-for-write', if non-nil, overrides this variable.
5559 This variable is never applied to a way of decoding a file while reading it. */);
5561 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5562 &current_buffer->bidi_display_reordering, Qnil,
5563 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5565 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5566 &current_buffer->bidi_paragraph_direction, Qnil,
5567 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5569 If this is nil (the default), the direction of each paragraph is
5570 determined by the first strong directional character of its text.
5571 The values of `right-to-left' and `left-to-right' override that.
5572 Any other value is treated as nil.
5574 This variable has no effect unless the buffer's value of
5575 \`bidi-display-reordering' is non-nil. */);
5577 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5578 doc: /* *Non-nil means do not display continuation lines.
5579 Instead, give each line of text just one screen line.
5581 Note that this is overridden by the variable
5582 `truncate-partial-width-windows' if that variable is non-nil
5583 and this buffer is not full-frame width. */);
5585 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5586 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5587 When word-wrapping is on, continuation lines are wrapped at the space
5588 or tab character nearest to the right window edge.
5589 If nil, continuation lines are wrapped at the right screen edge.
5591 This variable has no effect if long lines are truncated (see
5592 `truncate-lines' and `truncate-partial-width-windows'). If you use
5593 word-wrapping, you might want to reduce the value of
5594 `truncate-partial-width-windows', since wrapping can make text readable
5595 in narrower windows. */);
5597 #ifdef DOS_NT
5598 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5599 Qnil,
5600 doc: /* Non-nil if the visited file is a binary file.
5601 This variable is meaningful on MS-DOG and Windows NT.
5602 On those systems, it is automatically local in every buffer.
5603 On other systems, this variable is normally always nil. */);
5604 #endif
5606 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5607 make_number (Lisp_String),
5608 doc: /* Name of default directory of current buffer. Should end with slash.
5609 To interactively change the default directory, use command `cd'. */);
5611 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5612 Qnil,
5613 doc: /* Function called (if non-nil) to perform auto-fill.
5614 It is called after self-inserting any character specified in
5615 the `auto-fill-chars' table.
5616 NOTE: This variable is not a hook;
5617 its value may not be a list of functions. */);
5619 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5620 make_number (Lisp_String),
5621 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5623 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5624 make_number (Lisp_String),
5625 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5626 The truename of a file is calculated by `file-truename'
5627 and then abbreviated with `abbreviate-file-name'. */);
5629 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5630 &current_buffer->auto_save_file_name,
5631 make_number (Lisp_String),
5632 doc: /* Name of file for auto-saving current buffer.
5633 If it is nil, that means don't auto-save this buffer. */);
5635 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5636 doc: /* Non-nil if this buffer is read-only. */);
5638 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5639 doc: /* Non-nil if this buffer's file has been backed up.
5640 Backing up is done before the first time the file is saved. */);
5642 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5643 make_number (LISP_INT_TAG),
5644 doc: /* Length of current buffer when last read in, saved or auto-saved.
5645 0 initially.
5646 -1 means auto-saving turned off until next real save.
5648 If you set this to -2, that means don't turn off auto-saving in this buffer
5649 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5650 you probably should set this to -2 in that buffer. */);
5652 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5653 Qnil,
5654 doc: /* Non-nil enables selective display.
5655 An integer N as value means display only lines
5656 that start with less than N columns of space.
5657 A value of t means that the character ^M makes itself and
5658 all the rest of the line invisible; also, when saving the buffer
5659 in a file, save the ^M as a newline. */);
5661 #ifndef old
5662 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5663 &current_buffer->selective_display_ellipses,
5664 Qnil,
5665 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5666 #endif
5668 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5669 doc: /* Non-nil if self-insertion should replace existing text.
5670 The value should be one of `overwrite-mode-textual',
5671 `overwrite-mode-binary', or nil.
5672 If it is `overwrite-mode-textual', self-insertion still
5673 inserts at the end of a line, and inserts when point is before a tab,
5674 until the tab is filled in.
5675 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5677 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5678 Qnil,
5679 doc: /* Display table that controls display of the contents of current buffer.
5681 If this variable is nil, the value of `standard-display-table' is used.
5682 Each window can have its own, overriding display table, see
5683 `set-window-display-table' and `window-display-table'.
5685 The display table is a char-table created with `make-display-table'.
5686 A char-table is an array indexed by character codes. Normal array
5687 primitives `aref' and `aset' can be used to access elements of a char-table.
5689 Each of the char-table elements control how to display the corresponding
5690 text character: the element at index C in the table says how to display
5691 the character whose code is C. Each element should be a vector of
5692 characters or nil. The value nil means display the character in the
5693 default fashion; otherwise, the characters from the vector are delivered
5694 to the screen instead of the original character.
5696 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5697 to display a capital Y instead of each X character.
5699 In addition, a char-table has six extra slots to control the display of:
5701 the end of a truncated screen line (extra-slot 0, a single character);
5702 the end of a continued line (extra-slot 1, a single character);
5703 the escape character used to display character codes in octal
5704 (extra-slot 2, a single character);
5705 the character used as an arrow for control characters (extra-slot 3,
5706 a single character);
5707 the decoration indicating the presence of invisible lines (extra-slot 4,
5708 a vector of characters);
5709 the character used to draw the border between side-by-side windows
5710 (extra-slot 5, a single character).
5712 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5714 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5715 Qnil,
5716 doc: /* *Width of left marginal area for display of a buffer.
5717 A value of nil means no marginal area. */);
5719 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5720 Qnil,
5721 doc: /* *Width of right marginal area for display of a buffer.
5722 A value of nil means no marginal area. */);
5724 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5725 Qnil,
5726 doc: /* *Width of this buffer's left fringe (in pixels).
5727 A value of 0 means no left fringe is shown in this buffer's window.
5728 A value of nil means to use the left fringe width from the window's frame. */);
5730 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5731 Qnil,
5732 doc: /* *Width of this buffer's right fringe (in pixels).
5733 A value of 0 means no right fringe is shown in this buffer's window.
5734 A value of nil means to use the right fringe width from the window's frame. */);
5736 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5737 Qnil,
5738 doc: /* *Non-nil means to display fringes outside display margins.
5739 A value of nil means to display fringes between margins and buffer text. */);
5741 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5742 Qnil,
5743 doc: /* *Width of this buffer's scroll bars in pixels.
5744 A value of nil means to use the scroll bar width from the window's frame. */);
5746 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5747 Qnil,
5748 doc: /* *Position of this buffer's vertical scroll bar.
5749 The value takes effect whenever you tell a window to display this buffer;
5750 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5752 A value of `left' or `right' means put the vertical scroll bar at that side
5753 of the window; a value of nil means don't show any vertical scroll bars.
5754 A value of t (the default) means do whatever the window's frame specifies. */);
5756 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5757 &current_buffer->indicate_empty_lines, Qnil,
5758 doc: /* *Visually indicate empty lines after the buffer end.
5759 If non-nil, a bitmap is displayed in the left fringe of a window on
5760 window-systems. */);
5762 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5763 &current_buffer->indicate_buffer_boundaries, Qnil,
5764 doc: /* *Visually indicate buffer boundaries and scrolling.
5765 If non-nil, the first and last line of the buffer are marked in the fringe
5766 of a window on window-systems with angle bitmaps, or if the window can be
5767 scrolled, the top and bottom line of the window are marked with up and down
5768 arrow bitmaps.
5770 If value is a symbol `left' or `right', both angle and arrow bitmaps
5771 are displayed in the left or right fringe, resp. Any other value
5772 that doesn't look like an alist means display the angle bitmaps in
5773 the left fringe but no arrows.
5775 You can exercise more precise control by using an alist as the
5776 value. Each alist element (INDICATOR . POSITION) specifies
5777 where to show one of the indicators. INDICATOR is one of `top',
5778 `bottom', `up', `down', or t, which specifies the default position,
5779 and POSITION is one of `left', `right', or nil, meaning do not show
5780 this indicator.
5782 For example, ((top . left) (t . right)) places the top angle bitmap in
5783 left fringe, the bottom angle bitmap in right fringe, and both arrow
5784 bitmaps in right fringe. To show just the angle bitmaps in the left
5785 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5787 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5788 &current_buffer->fringe_indicator_alist, Qnil,
5789 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5790 The value is an alist where each element (INDICATOR . BITMAPS)
5791 specifies the fringe bitmaps used to display a specific logical
5792 fringe indicator.
5794 INDICATOR specifies the logical indicator type which is one of the
5795 following symbols: `truncation' , `continuation', `overlay-arrow',
5796 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5798 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5799 the actual bitmap shown in the left or right fringe for the logical
5800 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5801 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5802 are used only for the `bottom' and `top-bottom' indicators when the
5803 last (only) line has no final newline. BITMAPS may also be a single
5804 symbol which is used in both left and right fringes. */);
5806 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5807 &current_buffer->fringe_cursor_alist, Qnil,
5808 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5809 The value is an alist where each element (CURSOR . BITMAP)
5810 specifies the fringe bitmaps used to display a specific logical
5811 cursor type in the fringe.
5813 CURSOR specifies the logical cursor type which is one of the following
5814 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5815 one is used to show a hollow cursor on narrow lines display lines
5816 where the normal hollow cursor will not fit.
5818 BITMAP is the corresponding fringe bitmap shown for the logical
5819 cursor type. */);
5821 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5822 &current_buffer->scroll_up_aggressively, Qnil,
5823 doc: /* How far to scroll windows upward.
5824 If you move point off the bottom, the window scrolls automatically.
5825 This variable controls how far it scrolls. The value nil, the default,
5826 means scroll to center point. A fraction means scroll to put point
5827 that fraction of the window's height from the bottom of the window.
5828 When the value is 0.0, point goes at the bottom line, which in the
5829 simple case that you moved off with C-f means scrolling just one line.
5830 1.0 means point goes at the top, so that in that simple case, the
5831 window scrolls by a full window height. Meaningful values are
5832 between 0.0 and 1.0, inclusive. */);
5834 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5835 &current_buffer->scroll_down_aggressively, Qnil,
5836 doc: /* How far to scroll windows downward.
5837 If you move point off the top, the window scrolls automatically.
5838 This variable controls how far it scrolls. The value nil, the default,
5839 means scroll to center point. A fraction means scroll to put point
5840 that fraction of the window's height from the top of the window.
5841 When the value is 0.0, point goes at the top line, which in the
5842 simple case that you moved off with C-b means scrolling just one line.
5843 1.0 means point goes at the bottom, so that in that simple case, the
5844 window scrolls by a full window height. Meaningful values are
5845 between 0.0 and 1.0, inclusive. */);
5847 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5848 "Don't ask.");
5851 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5852 doc: /* List of functions to call before each text change.
5853 Two arguments are passed to each function: the positions of
5854 the beginning and end of the range of old text to be changed.
5855 \(For an insertion, the beginning and end are at the same place.)
5856 No information is given about the length of the text after the change.
5858 Buffer changes made while executing the `before-change-functions'
5859 don't call any before-change or after-change functions.
5860 That's because these variables are temporarily set to nil.
5861 As a result, a hook function cannot straightforwardly alter the
5862 value of these variables. See the Emacs Lisp manual for a way of
5863 accomplishing an equivalent result by using other variables.
5865 If an unhandled error happens in running these functions,
5866 the variable's value remains nil. That prevents the error
5867 from happening repeatedly and making Emacs nonfunctional. */);
5868 Vbefore_change_functions = Qnil;
5870 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5871 doc: /* List of functions to call after each text change.
5872 Three arguments are passed to each function: the positions of
5873 the beginning and end of the range of changed text,
5874 and the length in bytes of the pre-change text replaced by that range.
5875 \(For an insertion, the pre-change length is zero;
5876 for a deletion, that length is the number of bytes deleted,
5877 and the post-change beginning and end are at the same place.)
5879 Buffer changes made while executing the `after-change-functions'
5880 don't call any before-change or after-change functions.
5881 That's because these variables are temporarily set to nil.
5882 As a result, a hook function cannot straightforwardly alter the
5883 value of these variables. See the Emacs Lisp manual for a way of
5884 accomplishing an equivalent result by using other variables.
5886 If an unhandled error happens in running these functions,
5887 the variable's value remains nil. That prevents the error
5888 from happening repeatedly and making Emacs nonfunctional. */);
5889 Vafter_change_functions = Qnil;
5891 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5892 doc: /* A list of functions to call before changing a buffer which is unmodified.
5893 The functions are run using the `run-hooks' function. */);
5894 Vfirst_change_hook = Qnil;
5896 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5897 doc: /* List of undo entries in current buffer.
5898 Recent changes come first; older changes follow newer.
5900 An entry (BEG . END) represents an insertion which begins at
5901 position BEG and ends at position END.
5903 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5904 from (abs POSITION). If POSITION is positive, point was at the front
5905 of the text being deleted; if negative, point was at the end.
5907 An entry (t HIGH . LOW) indicates that the buffer previously had
5908 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5909 of the visited file's modification time, as of that time. If the
5910 modification time of the most recent save is different, this entry is
5911 obsolete.
5913 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5914 was modified between BEG and END. PROPERTY is the property name,
5915 and VALUE is the old value.
5917 An entry (apply FUN-NAME . ARGS) means undo the change with
5918 \(apply FUN-NAME ARGS).
5920 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5921 in the active region. BEG and END is the range affected by this entry
5922 and DELTA is the number of bytes added or deleted in that range by
5923 this change.
5925 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5926 was adjusted in position by the offset DISTANCE (an integer).
5928 An entry of the form POSITION indicates that point was at the buffer
5929 location given by the integer. Undoing an entry of this form places
5930 point at POSITION.
5932 Entries with value `nil' mark undo boundaries. The undo command treats
5933 the changes between two undo boundaries as a single step to be undone.
5935 If the value of the variable is t, undo information is not recorded. */);
5937 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5938 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
5940 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5941 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
5943 Normally, the line-motion functions work by scanning the buffer for
5944 newlines. Columnar operations (like `move-to-column' and
5945 `compute-motion') also work by scanning the buffer, summing character
5946 widths as they go. This works well for ordinary text, but if the
5947 buffer's lines are very long (say, more than 500 characters), these
5948 motion functions will take longer to execute. Emacs may also take
5949 longer to update the display.
5951 If `cache-long-line-scans' is non-nil, these motion functions cache the
5952 results of their scans, and consult the cache to avoid rescanning
5953 regions of the buffer until the text is modified. The caches are most
5954 beneficial when they prevent the most searching---that is, when the
5955 buffer contains long lines and large regions of characters with the
5956 same, fixed screen width.
5958 When `cache-long-line-scans' is non-nil, processing short lines will
5959 become slightly slower (because of the overhead of consulting the
5960 cache), and the caches will use memory roughly proportional to the
5961 number of newlines and characters whose screen width varies.
5963 The caches require no explicit maintenance; their accuracy is
5964 maintained internally by the Emacs primitives. Enabling or disabling
5965 the cache should not affect the behavior of any of the motion
5966 functions; it should only affect their performance. */);
5968 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5969 doc: /* Value of point before the last series of scroll operations, or nil. */);
5971 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5972 doc: /* List of formats to use when saving this buffer.
5973 Formats are defined by `format-alist'. This variable is
5974 set when a file is visited. */);
5976 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
5977 &current_buffer->auto_save_file_format, Qnil,
5978 doc: /* *Format in which to write auto-save files.
5979 Should be a list of symbols naming formats that are defined in `format-alist'.
5980 If it is t, which is the default, auto-save files are written in the
5981 same format as a regular save would use. */);
5983 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5984 &current_buffer->invisibility_spec, Qnil,
5985 doc: /* Invisibility spec of this buffer.
5986 The default is t, which means that text is invisible
5987 if it has a non-nil `invisible' property.
5988 If the value is a list, a text character is invisible if its `invisible'
5989 property is an element in that list (or is a list with members in common).
5990 If an element is a cons cell of the form (PROP . ELLIPSIS),
5991 then characters with property value PROP are invisible,
5992 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
5994 DEFVAR_PER_BUFFER ("buffer-display-count",
5995 &current_buffer->display_count, Qnil,
5996 doc: /* A number incremented each time this buffer is displayed in a window.
5997 The function `set-window-buffer' increments it. */);
5999 DEFVAR_PER_BUFFER ("buffer-display-time",
6000 &current_buffer->display_time, Qnil,
6001 doc: /* Time stamp updated each time this buffer is displayed in a window.
6002 The function `set-window-buffer' updates this variable
6003 to the value obtained by calling `current-time'.
6004 If the buffer has never been shown in a window, the value is nil. */);
6006 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6007 doc: /* */);
6008 Vtransient_mark_mode = Qnil;
6009 /* The docstring is in simple.el. If we put it here, it would be
6010 overwritten when transient-mark-mode is defined using
6011 define-minor-mode. */
6013 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6014 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6015 If the value is t, disregard `buffer-read-only' and all `read-only'
6016 text properties. If the value is a list, disregard `buffer-read-only'
6017 and disregard a `read-only' text property if the property value
6018 is a member of the list. */);
6019 Vinhibit_read_only = Qnil;
6021 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6022 doc: /* Cursor to use when this buffer is in the selected window.
6023 Values are interpreted as follows:
6025 t use the cursor specified for the frame
6026 nil don't display a cursor
6027 box display a filled box cursor
6028 hollow display a hollow box cursor
6029 bar display a vertical bar cursor with default width
6030 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6031 hbar display a horizontal bar cursor with default height
6032 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6033 ANYTHING ELSE display a hollow box cursor
6035 When the buffer is displayed in a non-selected window, the
6036 cursor's appearance is instead controlled by the variable
6037 `cursor-in-non-selected-windows'. */);
6039 DEFVAR_PER_BUFFER ("line-spacing",
6040 &current_buffer->extra_line_spacing, Qnil,
6041 doc: /* Additional space to put between lines when displaying a buffer.
6042 The space is measured in pixels, and put below lines on graphic displays,
6043 see `display-graphic-p'.
6044 If value is a floating point number, it specifies the spacing relative
6045 to the default frame line height. A value of nil means add no extra space. */);
6047 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6048 &current_buffer->cursor_in_non_selected_windows, Qnil,
6049 doc: /* *Cursor type to display in non-selected windows.
6050 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6052 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6053 doc: /* List of functions called with no args to query before killing a buffer.
6054 The buffer being killed will be current while the functions are running.
6055 If any of them returns nil, the buffer is not killed. */);
6056 Vkill_buffer_query_functions = Qnil;
6058 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6059 doc: /* Normal hook run before changing the major mode of a buffer.
6060 The function `kill-all-local-variables' runs this before doing anything else. */);
6061 Vchange_major_mode_hook = Qnil;
6062 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6063 staticpro (&Qchange_major_mode_hook);
6065 defsubr (&Sbuffer_live_p);
6066 defsubr (&Sbuffer_list);
6067 defsubr (&Sget_buffer);
6068 defsubr (&Sget_file_buffer);
6069 defsubr (&Sget_buffer_create);
6070 defsubr (&Smake_indirect_buffer);
6071 defsubr (&Sgenerate_new_buffer_name);
6072 defsubr (&Sbuffer_name);
6073 /*defsubr (&Sbuffer_number);*/
6074 defsubr (&Sbuffer_file_name);
6075 defsubr (&Sbuffer_base_buffer);
6076 defsubr (&Sbuffer_local_value);
6077 defsubr (&Sbuffer_local_variables);
6078 defsubr (&Sbuffer_modified_p);
6079 defsubr (&Sset_buffer_modified_p);
6080 defsubr (&Sbuffer_modified_tick);
6081 defsubr (&Sbuffer_chars_modified_tick);
6082 defsubr (&Srename_buffer);
6083 defsubr (&Sother_buffer);
6084 defsubr (&Sbuffer_enable_undo);
6085 defsubr (&Skill_buffer);
6086 defsubr (&Srecord_buffer);
6087 defsubr (&Sunrecord_buffer);
6088 defsubr (&Sset_buffer_major_mode);
6089 defsubr (&Scurrent_buffer);
6090 defsubr (&Sset_buffer);
6091 defsubr (&Sbarf_if_buffer_read_only);
6092 defsubr (&Serase_buffer);
6093 defsubr (&Sbuffer_swap_text);
6094 defsubr (&Sset_buffer_multibyte);
6095 defsubr (&Skill_all_local_variables);
6097 defsubr (&Soverlayp);
6098 defsubr (&Smake_overlay);
6099 defsubr (&Sdelete_overlay);
6100 defsubr (&Smove_overlay);
6101 defsubr (&Soverlay_start);
6102 defsubr (&Soverlay_end);
6103 defsubr (&Soverlay_buffer);
6104 defsubr (&Soverlay_properties);
6105 defsubr (&Soverlays_at);
6106 defsubr (&Soverlays_in);
6107 defsubr (&Snext_overlay_change);
6108 defsubr (&Sprevious_overlay_change);
6109 defsubr (&Soverlay_recenter);
6110 defsubr (&Soverlay_lists);
6111 defsubr (&Soverlay_get);
6112 defsubr (&Soverlay_put);
6113 defsubr (&Srestore_buffer_modified_p);
6116 void
6117 keys_of_buffer (void)
6119 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6120 initial_define_key (control_x_map, 'k', "kill-buffer");
6122 /* This must not be in syms_of_buffer, because Qdisabled is not
6123 initialized when that function gets called. */
6124 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6127 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6128 (do not change this comment) */