Rename `image-library-alist' to `dynamic-library-alist'.
[emacs.git] / src / buffer.c
blobed3b7acd2ac3a69253330821ad96361429d46a81
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 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
110 int after, Lisp_Object arg1,
111 Lisp_Object arg2, Lisp_Object arg3);
112 static void swap_out_buffer_local_variables (struct buffer *b);
113 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
115 /* Alist of all buffer names vs the buffers. */
116 /* This used to be a variable, but is no longer,
117 to prevent lossage due to user rplac'ing this alist or its elements. */
118 Lisp_Object Vbuffer_alist;
120 /* Functions to call before and after each text change. */
121 Lisp_Object Vbefore_change_functions;
122 Lisp_Object Vafter_change_functions;
124 Lisp_Object Vtransient_mark_mode;
126 /* t means ignore all read-only text properties.
127 A list means ignore such a property if its value is a member of the list.
128 Any non-nil value means ignore buffer-read-only. */
129 Lisp_Object Vinhibit_read_only;
131 /* List of functions to call that can query about killing a buffer.
132 If any of these functions returns nil, we don't kill it. */
133 Lisp_Object Vkill_buffer_query_functions;
134 Lisp_Object Qkill_buffer_query_functions;
136 /* Hook run before changing a major mode. */
137 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
139 /* List of functions to call before changing an unmodified buffer. */
140 Lisp_Object Vfirst_change_hook;
142 Lisp_Object Qfirst_change_hook;
143 Lisp_Object Qbefore_change_functions;
144 Lisp_Object Qafter_change_functions;
145 Lisp_Object Qucs_set_table_for_input;
147 /* If nonzero, all modification hooks are suppressed. */
148 int inhibit_modification_hooks;
150 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
151 Lisp_Object Qpermanent_local_hook;
153 Lisp_Object Qprotected_field;
155 Lisp_Object QSFundamental; /* A string "Fundamental" */
157 Lisp_Object Qkill_buffer_hook;
159 Lisp_Object Qget_file_buffer;
161 Lisp_Object Qoverlayp;
163 Lisp_Object Qpriority, Qevaporate, Qbefore_string, Qafter_string;
165 Lisp_Object Qmodification_hooks;
166 Lisp_Object Qinsert_in_front_hooks;
167 Lisp_Object Qinsert_behind_hooks;
169 static void alloc_buffer_text (struct buffer *, size_t);
170 static void free_buffer_text (struct buffer *b);
171 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
172 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
173 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
175 /* For debugging; temporary. See set_buffer_internal. */
176 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
178 void
179 nsberror (Lisp_Object spec)
181 if (STRINGP (spec))
182 error ("No buffer named %s", SDATA (spec));
183 error ("Invalid buffer argument");
186 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
187 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
188 Value is nil if OBJECT is not a buffer or if it has been killed. */)
189 (Lisp_Object object)
191 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
192 ? Qt : Qnil);
195 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
196 doc: /* Return a list of all existing live buffers.
197 If the optional arg FRAME is a frame, we return the buffer list
198 in the proper order for that frame: the buffers in FRAME's `buffer-list'
199 frame parameter come first, followed by the rest of the buffers. */)
200 (Lisp_Object frame)
202 Lisp_Object general;
203 general = Fmapcar (Qcdr, Vbuffer_alist);
205 if (FRAMEP (frame))
207 Lisp_Object framelist, prevlist, tail;
208 Lisp_Object args[3];
210 CHECK_FRAME (frame);
212 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
213 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
215 /* Remove from GENERAL any buffer that duplicates one in
216 FRAMELIST or PREVLIST. */
217 tail = framelist;
218 while (CONSP (tail))
220 general = Fdelq (XCAR (tail), general);
221 tail = XCDR (tail);
223 tail = prevlist;
224 while (CONSP (tail))
226 general = Fdelq (XCAR (tail), general);
227 tail = XCDR (tail);
230 args[0] = framelist;
231 args[1] = general;
232 args[2] = prevlist;
233 return Fnconc (3, args);
236 return general;
239 /* Like Fassoc, but use Fstring_equal to compare
240 (which ignores text properties),
241 and don't ever QUIT. */
243 static Lisp_Object
244 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
246 register Lisp_Object tail;
247 for (tail = list; CONSP (tail); tail = XCDR (tail))
249 register Lisp_Object elt, tem;
250 elt = XCAR (tail);
251 tem = Fstring_equal (Fcar (elt), key);
252 if (!NILP (tem))
253 return elt;
255 return Qnil;
258 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
259 doc: /* Return the buffer named BUFFER-OR-NAME.
260 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
261 is a string and there is no buffer with that name, return nil. If
262 BUFFER-OR-NAME is a buffer, return it as given. */)
263 (register Lisp_Object buffer_or_name)
265 if (BUFFERP (buffer_or_name))
266 return buffer_or_name;
267 CHECK_STRING (buffer_or_name);
269 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
272 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
273 doc: /* Return the buffer visiting file FILENAME (a string).
274 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
275 If there is no such live buffer, return nil.
276 See also `find-buffer-visiting'. */)
277 (register Lisp_Object filename)
279 register Lisp_Object tail, buf, tem;
280 Lisp_Object handler;
282 CHECK_STRING (filename);
283 filename = Fexpand_file_name (filename, Qnil);
285 /* If the file name has special constructs in it,
286 call the corresponding file handler. */
287 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
288 if (!NILP (handler))
289 return call2 (handler, Qget_file_buffer, filename);
291 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
293 buf = Fcdr (XCAR (tail));
294 if (!BUFFERP (buf)) continue;
295 if (!STRINGP (XBUFFER (buf)->filename)) continue;
296 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
297 if (!NILP (tem))
298 return buf;
300 return Qnil;
303 Lisp_Object
304 get_truename_buffer (register Lisp_Object filename)
306 register Lisp_Object tail, buf, tem;
308 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
310 buf = Fcdr (XCAR (tail));
311 if (!BUFFERP (buf)) continue;
312 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
313 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
314 if (!NILP (tem))
315 return buf;
317 return Qnil;
320 /* Incremented for each buffer created, to assign the buffer number. */
321 int buffer_count;
323 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
324 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
325 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
326 return that buffer. If no such buffer exists, create a new buffer with
327 that name and return it. If BUFFER-OR-NAME starts with a space, the new
328 buffer does not keep undo information.
330 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
331 even if it is dead. The return value is never nil. */)
332 (register Lisp_Object buffer_or_name)
334 register Lisp_Object buffer, name;
335 register struct buffer *b;
337 buffer = Fget_buffer (buffer_or_name);
338 if (!NILP (buffer))
339 return buffer;
341 if (SCHARS (buffer_or_name) == 0)
342 error ("Empty string for buffer name is not allowed");
344 b = allocate_buffer ();
346 /* An ordinary buffer uses its own struct buffer_text. */
347 b->text = &b->own_text;
348 b->base_buffer = 0;
350 BUF_GAP_SIZE (b) = 20;
351 BLOCK_INPUT;
352 /* We allocate extra 1-byte at the tail and keep it always '\0' for
353 anchoring a search. */
354 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
355 UNBLOCK_INPUT;
356 if (! BUF_BEG_ADDR (b))
357 buffer_memory_full ();
359 BUF_PT (b) = BEG;
360 BUF_GPT (b) = BEG;
361 BUF_BEGV (b) = BEG;
362 BUF_ZV (b) = BEG;
363 BUF_Z (b) = BEG;
364 BUF_PT_BYTE (b) = BEG_BYTE;
365 BUF_GPT_BYTE (b) = BEG_BYTE;
366 BUF_BEGV_BYTE (b) = BEG_BYTE;
367 BUF_ZV_BYTE (b) = BEG_BYTE;
368 BUF_Z_BYTE (b) = BEG_BYTE;
369 BUF_MODIFF (b) = 1;
370 BUF_CHARS_MODIFF (b) = 1;
371 BUF_OVERLAY_MODIFF (b) = 1;
372 BUF_SAVE_MODIFF (b) = 1;
373 BUF_INTERVALS (b) = 0;
374 BUF_UNCHANGED_MODIFIED (b) = 1;
375 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
376 BUF_END_UNCHANGED (b) = 0;
377 BUF_BEG_UNCHANGED (b) = 0;
378 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
380 b->newline_cache = 0;
381 b->width_run_cache = 0;
382 b->width_table = Qnil;
383 b->prevent_redisplay_optimizations_p = 1;
385 /* Put this on the chain of all buffers including killed ones. */
386 b->next = all_buffers;
387 all_buffers = b;
389 /* An ordinary buffer normally doesn't need markers
390 to handle BEGV and ZV. */
391 b->pt_marker = Qnil;
392 b->begv_marker = Qnil;
393 b->zv_marker = Qnil;
395 name = Fcopy_sequence (buffer_or_name);
396 STRING_SET_INTERVALS (name, NULL_INTERVAL);
397 b->name = name;
399 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
401 reset_buffer (b);
402 reset_buffer_local_variables (b, 1);
404 b->mark = Fmake_marker ();
405 BUF_MARKERS (b) = NULL;
406 b->name = name;
408 /* Put this in the alist of all live buffers. */
409 XSETBUFFER (buffer, b);
410 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
412 /* An error in calling the function here (should someone redefine it)
413 can lead to infinite regress until you run out of stack. rms
414 says that's not worth protecting against. */
415 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
416 /* buffer is on buffer-alist, so no gcpro. */
417 call1 (Qucs_set_table_for_input, buffer);
419 return buffer;
423 /* Return a list of overlays which is a copy of the overlay list
424 LIST, but for buffer B. */
426 static struct Lisp_Overlay *
427 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
429 Lisp_Object buffer;
430 struct Lisp_Overlay *result = NULL, *tail = NULL;
432 XSETBUFFER (buffer, b);
434 for (; list; list = list->next)
436 Lisp_Object overlay, start, end, old_overlay;
437 EMACS_INT charpos;
439 XSETMISC (old_overlay, list);
440 charpos = marker_position (OVERLAY_START (old_overlay));
441 start = Fmake_marker ();
442 Fset_marker (start, make_number (charpos), buffer);
443 XMARKER (start)->insertion_type
444 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
446 charpos = marker_position (OVERLAY_END (old_overlay));
447 end = Fmake_marker ();
448 Fset_marker (end, make_number (charpos), buffer);
449 XMARKER (end)->insertion_type
450 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
452 overlay = allocate_misc ();
453 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
454 OVERLAY_START (overlay) = start;
455 OVERLAY_END (overlay) = end;
456 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
457 XOVERLAY (overlay)->next = NULL;
459 if (tail)
460 tail = tail->next = XOVERLAY (overlay);
461 else
462 result = tail = XOVERLAY (overlay);
465 return result;
469 /* Clone per-buffer values of buffer FROM.
471 Buffer TO gets the same per-buffer values as FROM, with the
472 following exceptions: (1) TO's name is left untouched, (2) markers
473 are copied and made to refer to TO, and (3) overlay lists are
474 copied. */
476 static void
477 clone_per_buffer_values (struct buffer *from, struct buffer *to)
479 Lisp_Object to_buffer;
480 int offset;
482 XSETBUFFER (to_buffer, to);
484 /* buffer-local Lisp variables start at `undo_list',
485 tho only the ones from `name' on are GC'd normally. */
486 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
487 offset < sizeof *to;
488 offset += sizeof (Lisp_Object))
490 Lisp_Object obj;
492 /* Don't touch the `name' which should be unique for every buffer. */
493 if (offset == PER_BUFFER_VAR_OFFSET (name))
494 continue;
496 obj = PER_BUFFER_VALUE (from, offset);
497 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
499 struct Lisp_Marker *m = XMARKER (obj);
500 obj = Fmake_marker ();
501 XMARKER (obj)->insertion_type = m->insertion_type;
502 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
505 PER_BUFFER_VALUE (to, offset) = obj;
508 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
510 to->overlays_before = copy_overlays (to, from->overlays_before);
511 to->overlays_after = copy_overlays (to, from->overlays_after);
513 /* Get (a copy of) the alist of Lisp-level local variables of FROM
514 and install that in TO. */
515 to->local_var_alist = buffer_lisp_local_variables (from);
518 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
519 2, 3,
520 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
521 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
522 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
523 NAME should be a string which is not the name of an existing buffer.
524 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
525 such as major and minor modes, in the indirect buffer.
526 CLONE nil means the indirect buffer's state is reset to default values. */)
527 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
529 Lisp_Object buf, tem;
530 struct buffer *b;
532 CHECK_STRING (name);
533 buf = Fget_buffer (name);
534 if (!NILP (buf))
535 error ("Buffer name `%s' is in use", SDATA (name));
537 tem = base_buffer;
538 base_buffer = Fget_buffer (base_buffer);
539 if (NILP (base_buffer))
540 error ("No such buffer: `%s'", SDATA (tem));
541 if (NILP (XBUFFER (base_buffer)->name))
542 error ("Base buffer has been killed");
544 if (SCHARS (name) == 0)
545 error ("Empty string for buffer name is not allowed");
547 b = allocate_buffer ();
549 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
550 ? XBUFFER (base_buffer)->base_buffer
551 : XBUFFER (base_buffer));
553 /* Use the base buffer's text object. */
554 b->text = b->base_buffer->text;
556 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
557 BUF_ZV (b) = BUF_ZV (b->base_buffer);
558 BUF_PT (b) = BUF_PT (b->base_buffer);
559 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
560 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
561 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
563 b->newline_cache = 0;
564 b->width_run_cache = 0;
565 b->width_table = Qnil;
567 /* Put this on the chain of all buffers including killed ones. */
568 b->next = all_buffers;
569 all_buffers = b;
571 name = Fcopy_sequence (name);
572 STRING_SET_INTERVALS (name, NULL_INTERVAL);
573 b->name = name;
575 reset_buffer (b);
576 reset_buffer_local_variables (b, 1);
578 /* Put this in the alist of all live buffers. */
579 XSETBUFFER (buf, b);
580 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
582 b->mark = Fmake_marker ();
583 b->name = name;
585 /* The multibyte status belongs to the base buffer. */
586 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
588 /* Make sure the base buffer has markers for its narrowing. */
589 if (NILP (b->base_buffer->pt_marker))
591 b->base_buffer->pt_marker = Fmake_marker ();
592 set_marker_both (b->base_buffer->pt_marker, base_buffer,
593 BUF_PT (b->base_buffer),
594 BUF_PT_BYTE (b->base_buffer));
596 if (NILP (b->base_buffer->begv_marker))
598 b->base_buffer->begv_marker = Fmake_marker ();
599 set_marker_both (b->base_buffer->begv_marker, base_buffer,
600 BUF_BEGV (b->base_buffer),
601 BUF_BEGV_BYTE (b->base_buffer));
603 if (NILP (b->base_buffer->zv_marker))
605 b->base_buffer->zv_marker = Fmake_marker ();
606 set_marker_both (b->base_buffer->zv_marker, base_buffer,
607 BUF_ZV (b->base_buffer),
608 BUF_ZV_BYTE (b->base_buffer));
609 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
612 if (NILP (clone))
614 /* Give the indirect buffer markers for its narrowing. */
615 b->pt_marker = Fmake_marker ();
616 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
617 b->begv_marker = Fmake_marker ();
618 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
619 b->zv_marker = Fmake_marker ();
620 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
621 XMARKER (b->zv_marker)->insertion_type = 1;
623 else
625 struct buffer *old_b = current_buffer;
627 clone_per_buffer_values (b->base_buffer, b);
628 b->filename = Qnil;
629 b->file_truename = Qnil;
630 b->display_count = make_number (0);
631 b->backed_up = Qnil;
632 b->auto_save_file_name = Qnil;
633 set_buffer_internal_1 (b);
634 Fset (intern ("buffer-save-without-query"), Qnil);
635 Fset (intern ("buffer-file-number"), Qnil);
636 Fset (intern ("buffer-stale-function"), Qnil);
637 set_buffer_internal_1 (old_b);
640 return buf;
643 void
644 delete_all_overlays (struct buffer *b)
646 Lisp_Object overlay;
648 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
649 have to empty the list, otherwise we end up with overlays that
650 think they belong to this buffer while the buffer doesn't know about
651 them any more. */
652 while (b->overlays_before)
654 XSETMISC (overlay, b->overlays_before);
655 Fdelete_overlay (overlay);
657 while (b->overlays_after)
659 XSETMISC (overlay, b->overlays_after);
660 Fdelete_overlay (overlay);
662 eassert (b->overlays_before == NULL);
663 eassert (b->overlays_after == NULL);
666 /* Reinitialize everything about a buffer except its name and contents
667 and local variables.
668 If called on an already-initialized buffer, the list of overlays
669 should be deleted before calling this function, otherwise we end up
670 with overlays that claim to belong to the buffer but the buffer
671 claims it doesn't belong to it. */
673 void
674 reset_buffer (register struct buffer *b)
676 b->filename = Qnil;
677 b->file_truename = Qnil;
678 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
679 b->modtime = 0;
680 b->modtime_size = -1;
681 XSETFASTINT (b->save_length, 0);
682 b->last_window_start = 1;
683 /* It is more conservative to start out "changed" than "unchanged". */
684 b->clip_changed = 0;
685 b->prevent_redisplay_optimizations_p = 1;
686 b->backed_up = Qnil;
687 BUF_AUTOSAVE_MODIFF (b) = 0;
688 b->auto_save_failure_time = -1;
689 b->auto_save_file_name = Qnil;
690 b->read_only = Qnil;
691 b->overlays_before = NULL;
692 b->overlays_after = NULL;
693 b->overlay_center = BEG;
694 b->mark_active = Qnil;
695 b->point_before_scroll = Qnil;
696 b->file_format = Qnil;
697 b->auto_save_file_format = Qt;
698 b->last_selected_window = Qnil;
699 XSETINT (b->display_count, 0);
700 b->display_time = Qnil;
701 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
702 b->cursor_type = buffer_defaults.cursor_type;
703 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
705 b->display_error_modiff = 0;
708 /* Reset buffer B's local variables info.
709 Don't use this on a buffer that has already been in use;
710 it does not treat permanent locals consistently.
711 Instead, use Fkill_all_local_variables.
713 If PERMANENT_TOO is 1, then we reset permanent
714 buffer-local variables. If PERMANENT_TOO is 0,
715 we preserve those. */
717 static void
718 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
720 register int offset;
721 int i;
723 /* Reset the major mode to Fundamental, together with all the
724 things that depend on the major mode.
725 default-major-mode is handled at a higher level.
726 We ignore it here. */
727 b->major_mode = Qfundamental_mode;
728 b->keymap = Qnil;
729 b->mode_name = QSFundamental;
730 b->minor_modes = Qnil;
732 /* If the standard case table has been altered and invalidated,
733 fix up its insides first. */
734 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
735 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
736 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
737 Fset_standard_case_table (Vascii_downcase_table);
739 b->downcase_table = Vascii_downcase_table;
740 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
741 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
742 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
743 b->invisibility_spec = Qt;
744 #ifndef DOS_NT
745 b->buffer_file_type = Qnil;
746 #endif
748 /* Reset all (or most) per-buffer variables to their defaults. */
749 if (permanent_too)
750 b->local_var_alist = Qnil;
751 else
753 Lisp_Object tmp, prop, last = Qnil;
754 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
755 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
757 /* If permanent-local, keep it. */
758 last = tmp;
759 if (EQ (prop, Qpermanent_local_hook))
761 /* This is a partially permanent hook variable.
762 Preserve only the elements that want to be preserved. */
763 Lisp_Object list, newlist;
764 list = XCDR (XCAR (tmp));
765 if (!CONSP (list))
766 newlist = list;
767 else
768 for (newlist = Qnil; CONSP (list); list = XCDR (list))
770 Lisp_Object elt = XCAR (list);
771 /* Preserve element ELT if it's t,
772 if it is a function with a `permanent-local-hook' property,
773 or if it's not a symbol. */
774 if (! SYMBOLP (elt)
775 || EQ (elt, Qt)
776 || !NILP (Fget (elt, Qpermanent_local_hook)))
777 newlist = Fcons (elt, newlist);
779 XSETCDR (XCAR (tmp), Fnreverse (newlist));
782 /* Delete this local variable. */
783 else if (NILP (last))
784 b->local_var_alist = XCDR (tmp);
785 else
786 XSETCDR (last, XCDR (tmp));
789 for (i = 0; i < last_per_buffer_idx; ++i)
790 if (permanent_too || buffer_permanent_local_flags[i] == 0)
791 SET_PER_BUFFER_VALUE_P (b, i, 0);
793 /* For each slot that has a default value,
794 copy that into the slot. */
796 /* buffer-local Lisp variables start at `undo_list',
797 tho only the ones from `name' on are GC'd normally. */
798 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
799 offset < sizeof *b;
800 offset += sizeof (Lisp_Object))
802 int idx = PER_BUFFER_IDX (offset);
803 if ((idx > 0
804 && (permanent_too
805 || buffer_permanent_local_flags[idx] == 0)))
806 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
810 /* We split this away from generate-new-buffer, because rename-buffer
811 and set-visited-file-name ought to be able to use this to really
812 rename the buffer properly. */
814 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
815 1, 2, 0,
816 doc: /* Return a string that is the name of no existing buffer based on NAME.
817 If there is no live buffer named NAME, then return NAME.
818 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
819 \(starting at 2) until an unused name is found, and then return that name.
820 Optional second argument IGNORE specifies a name that is okay to use (if
821 it is in the sequence to be tried) even if a buffer with that name exists. */)
822 (register Lisp_Object name, Lisp_Object ignore)
824 register Lisp_Object gentemp, tem;
825 int count;
826 char number[10];
828 CHECK_STRING (name);
830 tem = Fstring_equal (name, ignore);
831 if (!NILP (tem))
832 return name;
833 tem = Fget_buffer (name);
834 if (NILP (tem))
835 return name;
837 count = 1;
838 while (1)
840 sprintf (number, "<%d>", ++count);
841 gentemp = concat2 (name, build_string (number));
842 tem = Fstring_equal (gentemp, ignore);
843 if (!NILP (tem))
844 return gentemp;
845 tem = Fget_buffer (gentemp);
846 if (NILP (tem))
847 return gentemp;
852 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
853 doc: /* Return the name of BUFFER, as a string.
854 BUFFER defaults to the current buffer.
855 Return nil if BUFFER has been killed. */)
856 (register Lisp_Object buffer)
858 if (NILP (buffer))
859 return current_buffer->name;
860 CHECK_BUFFER (buffer);
861 return XBUFFER (buffer)->name;
864 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
865 doc: /* Return name of file BUFFER is visiting, or nil if none.
866 No argument or nil as argument means use the current buffer. */)
867 (register Lisp_Object buffer)
869 if (NILP (buffer))
870 return current_buffer->filename;
871 CHECK_BUFFER (buffer);
872 return XBUFFER (buffer)->filename;
875 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
876 0, 1, 0,
877 doc: /* Return the base buffer of indirect buffer BUFFER.
878 If BUFFER is not indirect, return nil.
879 BUFFER defaults to the current buffer. */)
880 (register Lisp_Object buffer)
882 struct buffer *base;
883 Lisp_Object base_buffer;
885 if (NILP (buffer))
886 base = current_buffer->base_buffer;
887 else
889 CHECK_BUFFER (buffer);
890 base = XBUFFER (buffer)->base_buffer;
893 if (! base)
894 return Qnil;
895 XSETBUFFER (base_buffer, base);
896 return base_buffer;
899 DEFUN ("buffer-local-value", Fbuffer_local_value,
900 Sbuffer_local_value, 2, 2, 0,
901 doc: /* Return the value of VARIABLE in BUFFER.
902 If VARIABLE does not have a buffer-local binding in BUFFER, the value
903 is the default binding of the variable. */)
904 (register Lisp_Object variable, register Lisp_Object buffer)
906 register struct buffer *buf;
907 register Lisp_Object result;
908 struct Lisp_Symbol *sym;
910 CHECK_SYMBOL (variable);
911 CHECK_BUFFER (buffer);
912 buf = XBUFFER (buffer);
913 sym = XSYMBOL (variable);
915 start:
916 switch (sym->redirect)
918 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
919 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
920 case SYMBOL_LOCALIZED:
921 { /* Look in local_var_alist. */
922 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
923 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
924 result = Fassoc (variable, buf->local_var_alist);
925 if (!NILP (result))
927 if (blv->fwd)
928 { /* What binding is loaded right now? */
929 Lisp_Object current_alist_element = blv->valcell;
931 /* The value of the currently loaded binding is not
932 stored in it, but rather in the realvalue slot.
933 Store that value into the binding it belongs to
934 in case that is the one we are about to use. */
936 XSETCDR (current_alist_element,
937 do_symval_forwarding (blv->fwd));
939 /* Now get the (perhaps updated) value out of the binding. */
940 result = XCDR (result);
942 else
943 result = Fdefault_value (variable);
944 break;
946 case SYMBOL_FORWARDED:
948 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
949 if (BUFFER_OBJFWDP (fwd))
950 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
951 else
952 result = Fdefault_value (variable);
953 break;
955 default: abort ();
958 if (!EQ (result, Qunbound))
959 return result;
961 xsignal1 (Qvoid_variable, variable);
964 /* Return an alist of the Lisp-level buffer-local bindings of
965 buffer BUF. That is, don't include the variables maintained
966 in special slots in the buffer object. */
968 static Lisp_Object
969 buffer_lisp_local_variables (struct buffer *buf)
971 Lisp_Object result = Qnil;
972 register Lisp_Object tail;
973 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
975 Lisp_Object val, elt;
977 elt = XCAR (tail);
979 /* Reference each variable in the alist in buf.
980 If inquiring about the current buffer, this gets the current values,
981 so store them into the alist so the alist is up to date.
982 If inquiring about some other buffer, this swaps out any values
983 for that buffer, making the alist up to date automatically. */
984 val = find_symbol_value (XCAR (elt));
985 /* Use the current buffer value only if buf is the current buffer. */
986 if (buf != current_buffer)
987 val = XCDR (elt);
989 result = Fcons (Fcons (XCAR (elt), val), result);
992 return result;
995 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
996 Sbuffer_local_variables, 0, 1, 0,
997 doc: /* Return an alist of variables that are buffer-local in BUFFER.
998 Most elements look like (SYMBOL . VALUE), describing one variable.
999 For a symbol that is locally unbound, just the symbol appears in the value.
1000 Note that storing new VALUEs in these elements doesn't change the variables.
1001 No argument or nil as argument means use current buffer as BUFFER. */)
1002 (register Lisp_Object buffer)
1004 register struct buffer *buf;
1005 register Lisp_Object result;
1007 if (NILP (buffer))
1008 buf = current_buffer;
1009 else
1011 CHECK_BUFFER (buffer);
1012 buf = XBUFFER (buffer);
1015 result = buffer_lisp_local_variables (buf);
1017 /* Add on all the variables stored in special slots. */
1019 int offset, idx;
1021 /* buffer-local Lisp variables start at `undo_list',
1022 tho only the ones from `name' on are GC'd normally. */
1023 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1024 offset < sizeof (struct buffer);
1025 /* sizeof EMACS_INT == sizeof Lisp_Object */
1026 offset += (sizeof (EMACS_INT)))
1028 idx = PER_BUFFER_IDX (offset);
1029 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1030 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1031 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1032 PER_BUFFER_VALUE (buf, offset)),
1033 result);
1037 return result;
1040 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1041 0, 1, 0,
1042 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1043 No argument or nil as argument means use current buffer as BUFFER. */)
1044 (register Lisp_Object buffer)
1046 register struct buffer *buf;
1047 if (NILP (buffer))
1048 buf = current_buffer;
1049 else
1051 CHECK_BUFFER (buffer);
1052 buf = XBUFFER (buffer);
1055 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1058 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1059 1, 1, 0,
1060 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1061 A non-nil FLAG means mark the buffer modified. */)
1062 (register Lisp_Object flag)
1064 register int already;
1065 register Lisp_Object fn;
1066 Lisp_Object buffer, window;
1068 #ifdef CLASH_DETECTION
1069 /* If buffer becoming modified, lock the file.
1070 If buffer becoming unmodified, unlock the file. */
1072 fn = current_buffer->file_truename;
1073 /* Test buffer-file-name so that binding it to nil is effective. */
1074 if (!NILP (fn) && ! NILP (current_buffer->filename))
1076 already = SAVE_MODIFF < MODIFF;
1077 if (!already && !NILP (flag))
1078 lock_file (fn);
1079 else if (already && NILP (flag))
1080 unlock_file (fn);
1082 #endif /* CLASH_DETECTION */
1084 /* Here we have a problem. SAVE_MODIFF is used here to encode
1085 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1086 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1087 modify SAVE_MODIFF to affect one, we may affect the other
1088 as well.
1089 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1090 if SAVE_MODIFF<auto_save_modified that means we risk changing
1091 recent-auto-save-p from t to nil.
1092 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1093 we risk changing recent-auto-save-p from nil to t. */
1094 SAVE_MODIFF = (NILP (flag)
1095 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1096 ? MODIFF
1097 /* Let's try to preserve recent-auto-save-p. */
1098 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1099 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1100 we can either decrease SAVE_MODIFF and auto_save_modified
1101 or increase MODIFF. */
1102 : MODIFF++);
1104 /* Set update_mode_lines only if buffer is displayed in some window.
1105 Packages like jit-lock or lazy-lock preserve a buffer's modified
1106 state by recording/restoring the state around blocks of code.
1107 Setting update_mode_lines makes redisplay consider all windows
1108 (on all frames). Stealth fontification of buffers not displayed
1109 would incur additional redisplay costs if we'd set
1110 update_modes_lines unconditionally.
1112 Ideally, I think there should be another mechanism for fontifying
1113 buffers without "modifying" buffers, or redisplay should be
1114 smarter about updating the `*' in mode lines. --gerd */
1115 XSETBUFFER (buffer, current_buffer);
1116 window = Fget_buffer_window (buffer, Qt);
1117 if (WINDOWP (window))
1119 ++update_mode_lines;
1120 current_buffer->prevent_redisplay_optimizations_p = 1;
1123 return flag;
1126 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1127 Srestore_buffer_modified_p, 1, 1, 0,
1128 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1129 It is not ensured that mode lines will be updated to show the modified
1130 state of the current buffer. Use with care. */)
1131 (Lisp_Object flag)
1133 #ifdef CLASH_DETECTION
1134 Lisp_Object fn;
1136 /* If buffer becoming modified, lock the file.
1137 If buffer becoming unmodified, unlock the file. */
1139 fn = current_buffer->file_truename;
1140 /* Test buffer-file-name so that binding it to nil is effective. */
1141 if (!NILP (fn) && ! NILP (current_buffer->filename))
1143 int already = SAVE_MODIFF < MODIFF;
1144 if (!already && !NILP (flag))
1145 lock_file (fn);
1146 else if (already && NILP (flag))
1147 unlock_file (fn);
1149 #endif /* CLASH_DETECTION */
1151 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1152 return flag;
1155 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1156 0, 1, 0,
1157 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1158 Each buffer has a tick counter which is incremented each time the
1159 text in that buffer is changed. It wraps around occasionally.
1160 No argument or nil as argument means use current buffer as BUFFER. */)
1161 (register Lisp_Object buffer)
1163 register struct buffer *buf;
1164 if (NILP (buffer))
1165 buf = current_buffer;
1166 else
1168 CHECK_BUFFER (buffer);
1169 buf = XBUFFER (buffer);
1172 return make_number (BUF_MODIFF (buf));
1175 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1176 Sbuffer_chars_modified_tick, 0, 1, 0,
1177 doc: /* Return BUFFER's character-change tick counter.
1178 Each buffer has a character-change tick counter, which is set to the
1179 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1180 time text in that buffer is inserted or deleted. By comparing the
1181 values returned by two individual calls of `buffer-chars-modified-tick',
1182 you can tell whether a character change occurred in that buffer in
1183 between these calls. No argument or nil as argument means use current
1184 buffer as BUFFER. */)
1185 (register Lisp_Object buffer)
1187 register struct buffer *buf;
1188 if (NILP (buffer))
1189 buf = current_buffer;
1190 else
1192 CHECK_BUFFER (buffer);
1193 buf = XBUFFER (buffer);
1196 return make_number (BUF_CHARS_MODIFF (buf));
1199 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1200 "(list (read-string \"Rename buffer (to new name): \" \
1201 nil 'buffer-name-history (buffer-name (current-buffer))) \
1202 current-prefix-arg)",
1203 doc: /* Change current buffer's name to NEWNAME (a string).
1204 If second arg UNIQUE is nil or omitted, it is an error if a
1205 buffer named NEWNAME already exists.
1206 If UNIQUE is non-nil, come up with a new name using
1207 `generate-new-buffer-name'.
1208 Interactively, you can set UNIQUE with a prefix argument.
1209 We return the name we actually gave the buffer.
1210 This does not change the name of the visited file (if any). */)
1211 (register Lisp_Object newname, Lisp_Object unique)
1213 register Lisp_Object tem, buf;
1215 CHECK_STRING (newname);
1217 if (SCHARS (newname) == 0)
1218 error ("Empty string is invalid as a buffer name");
1220 tem = Fget_buffer (newname);
1221 if (!NILP (tem))
1223 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1224 rename the buffer automatically so you can create another
1225 with the original name. It makes UNIQUE equivalent to
1226 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1227 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1228 return current_buffer->name;
1229 if (!NILP (unique))
1230 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1231 else
1232 error ("Buffer name `%s' is in use", SDATA (newname));
1235 current_buffer->name = newname;
1237 /* Catch redisplay's attention. Unless we do this, the mode lines for
1238 any windows displaying current_buffer will stay unchanged. */
1239 update_mode_lines++;
1241 XSETBUFFER (buf, current_buffer);
1242 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1243 if (NILP (current_buffer->filename)
1244 && !NILP (current_buffer->auto_save_file_name))
1245 call0 (intern ("rename-auto-save-file"));
1246 /* Refetch since that last call may have done GC. */
1247 return current_buffer->name;
1250 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1251 doc: /* Return most recently selected buffer other than BUFFER.
1252 Buffers not visible in windows are preferred to visible buffers,
1253 unless optional second argument VISIBLE-OK is non-nil.
1254 If the optional third argument FRAME is non-nil, use that frame's
1255 buffer list instead of the selected frame's buffer list.
1256 If no other buffer exists, the buffer `*scratch*' is returned.
1257 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1258 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1260 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1261 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1262 notsogood = Qnil;
1264 if (NILP (frame))
1265 frame = selected_frame;
1267 CHECK_FRAME (frame);
1269 tail = Vbuffer_alist;
1270 pred = frame_buffer_predicate (frame);
1272 /* Consider buffers that have been seen in the selected frame
1273 before other buffers. */
1275 tem = frame_buffer_list (frame);
1276 add_ons = Qnil;
1277 while (CONSP (tem))
1279 if (BUFFERP (XCAR (tem)))
1280 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1281 tem = XCDR (tem);
1283 tail = nconc2 (Fnreverse (add_ons), tail);
1285 for (; CONSP (tail); tail = XCDR (tail))
1287 buf = Fcdr (XCAR (tail));
1288 if (EQ (buf, buffer))
1289 continue;
1290 if (NILP (buf))
1291 continue;
1292 if (NILP (XBUFFER (buf)->name))
1293 continue;
1294 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1295 continue;
1296 /* If the selected frame has a buffer_predicate,
1297 disregard buffers that don't fit the predicate. */
1298 if (!NILP (pred))
1300 tem = call1 (pred, buf);
1301 if (NILP (tem))
1302 continue;
1305 if (NILP (visible_ok))
1306 tem = Fget_buffer_window (buf, Qvisible);
1307 else
1308 tem = Qnil;
1309 if (NILP (tem))
1310 return buf;
1311 if (NILP (notsogood))
1312 notsogood = buf;
1314 if (!NILP (notsogood))
1315 return notsogood;
1316 buf = Fget_buffer (build_string ("*scratch*"));
1317 if (NILP (buf))
1319 buf = Fget_buffer_create (build_string ("*scratch*"));
1320 Fset_buffer_major_mode (buf);
1322 return buf;
1325 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1326 0, 1, "",
1327 doc: /* Start keeping undo information for buffer BUFFER.
1328 No argument or nil as argument means do this for the current buffer. */)
1329 (register Lisp_Object buffer)
1331 Lisp_Object real_buffer;
1333 if (NILP (buffer))
1334 XSETBUFFER (real_buffer, current_buffer);
1335 else
1337 real_buffer = Fget_buffer (buffer);
1338 if (NILP (real_buffer))
1339 nsberror (buffer);
1342 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1343 XBUFFER (real_buffer)->undo_list = Qnil;
1345 return Qnil;
1349 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1350 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1351 The buffer being killed will be current while the hook is running.\n\
1352 See `kill-buffer'."
1354 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1355 doc: /* Kill buffer BUFFER-OR-NAME.
1356 The argument may be a buffer or the name of an existing buffer.
1357 Argument nil or omitted means kill the current buffer. Return t if the
1358 buffer is actually killed, nil otherwise.
1360 This function calls `replace-buffer-in-windows' for cleaning up all
1361 windows currently displaying the buffer to be killed. The functions in
1362 `kill-buffer-query-functions' are called with the buffer to be killed as
1363 the current buffer. If any of them returns nil, the buffer is not
1364 killed. The hook `kill-buffer-hook' is run before the buffer is
1365 actually killed. The buffer being killed will be current while the hook
1366 is running.
1368 Any processes that have this buffer as the `process-buffer' are killed
1369 with SIGHUP. */)
1370 (Lisp_Object buffer_or_name)
1372 Lisp_Object buffer;
1373 register struct buffer *b;
1374 register Lisp_Object tem;
1375 register struct Lisp_Marker *m;
1376 struct gcpro gcpro1;
1378 if (NILP (buffer_or_name))
1379 buffer = Fcurrent_buffer ();
1380 else
1381 buffer = Fget_buffer (buffer_or_name);
1382 if (NILP (buffer))
1383 nsberror (buffer_or_name);
1385 b = XBUFFER (buffer);
1387 /* Avoid trouble for buffer already dead. */
1388 if (NILP (b->name))
1389 return Qnil;
1391 /* Query if the buffer is still modified. */
1392 if (INTERACTIVE && !NILP (b->filename)
1393 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1395 GCPRO1 (buffer);
1396 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1397 b->name, make_number (0)));
1398 UNGCPRO;
1399 if (NILP (tem))
1400 return Qnil;
1403 /* Run hooks with the buffer to be killed the current buffer. */
1405 int count = SPECPDL_INDEX ();
1406 Lisp_Object arglist[1];
1408 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1409 set_buffer_internal (b);
1411 /* First run the query functions; if any query is answered no,
1412 don't kill the buffer. */
1413 arglist[0] = Qkill_buffer_query_functions;
1414 tem = Frun_hook_with_args_until_failure (1, arglist);
1415 if (NILP (tem))
1416 return unbind_to (count, Qnil);
1418 /* Then run the hooks. */
1419 Frun_hooks (1, &Qkill_buffer_hook);
1420 unbind_to (count, Qnil);
1423 /* We have no more questions to ask. Verify that it is valid
1424 to kill the buffer. This must be done after the questions
1425 since anything can happen within do_yes_or_no_p. */
1427 /* Don't kill the minibuffer now current. */
1428 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1429 return Qnil;
1431 if (NILP (b->name))
1432 return Qnil;
1434 /* When we kill a base buffer, kill all its indirect buffers.
1435 We do it at this stage so nothing terrible happens if they
1436 ask questions or their hooks get errors. */
1437 if (! b->base_buffer)
1439 struct buffer *other;
1441 GCPRO1 (buffer);
1443 for (other = all_buffers; other; other = other->next)
1444 /* all_buffers contains dead buffers too;
1445 don't re-kill them. */
1446 if (other->base_buffer == b && !NILP (other->name))
1448 Lisp_Object buffer;
1449 XSETBUFFER (buffer, other);
1450 Fkill_buffer (buffer);
1453 UNGCPRO;
1456 /* Make this buffer not be current.
1457 In the process, notice if this is the sole visible buffer
1458 and give up if so. */
1459 if (b == current_buffer)
1461 tem = Fother_buffer (buffer, Qnil, Qnil);
1462 Fset_buffer (tem);
1463 if (b == current_buffer)
1464 return Qnil;
1467 /* Notice if the buffer to kill is the sole visible buffer
1468 when we're currently in the mini-buffer, and give up if so. */
1469 XSETBUFFER (tem, current_buffer);
1470 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1472 tem = Fother_buffer (buffer, Qnil, Qnil);
1473 if (EQ (buffer, tem))
1474 return Qnil;
1477 /* Now there is no question: we can kill the buffer. */
1479 #ifdef CLASH_DETECTION
1480 /* Unlock this buffer's file, if it is locked. */
1481 unlock_buffer (b);
1482 #endif /* CLASH_DETECTION */
1484 GCPRO1 (buffer);
1485 kill_buffer_processes (buffer);
1486 UNGCPRO;
1488 /* Killing buffer processes may run sentinels which may
1489 have called kill-buffer. */
1491 if (NILP (b->name))
1492 return Qnil;
1494 clear_charpos_cache (b);
1496 tem = Vinhibit_quit;
1497 Vinhibit_quit = Qt;
1498 replace_buffer_in_all_windows (buffer);
1499 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1500 frames_discard_buffer (buffer);
1501 Vinhibit_quit = tem;
1503 /* Delete any auto-save file, if we saved it in this session.
1504 But not if the buffer is modified. */
1505 if (STRINGP (b->auto_save_file_name)
1506 && BUF_AUTOSAVE_MODIFF (b) != 0
1507 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1508 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1509 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1511 Lisp_Object tem;
1512 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1513 if (! NILP (tem))
1514 internal_delete_file (b->auto_save_file_name);
1517 if (b->base_buffer)
1519 /* Unchain all markers that belong to this indirect buffer.
1520 Don't unchain the markers that belong to the base buffer
1521 or its other indirect buffers. */
1522 for (m = BUF_MARKERS (b); m; )
1524 struct Lisp_Marker *next = m->next;
1525 if (m->buffer == b)
1526 unchain_marker (m);
1527 m = next;
1530 else
1532 /* Unchain all markers of this buffer and its indirect buffers.
1533 and leave them pointing nowhere. */
1534 for (m = BUF_MARKERS (b); m; )
1536 struct Lisp_Marker *next = m->next;
1537 m->buffer = 0;
1538 m->next = NULL;
1539 m = next;
1541 BUF_MARKERS (b) = NULL;
1542 BUF_INTERVALS (b) = NULL_INTERVAL;
1544 /* Perhaps we should explicitly free the interval tree here... */
1547 /* Reset the local variables, so that this buffer's local values
1548 won't be protected from GC. They would be protected
1549 if they happened to remain encached in their symbols.
1550 This gets rid of them for certain. */
1551 swap_out_buffer_local_variables (b);
1552 reset_buffer_local_variables (b, 1);
1554 b->name = Qnil;
1556 BLOCK_INPUT;
1557 if (! b->base_buffer)
1558 free_buffer_text (b);
1560 if (b->newline_cache)
1562 free_region_cache (b->newline_cache);
1563 b->newline_cache = 0;
1565 if (b->width_run_cache)
1567 free_region_cache (b->width_run_cache);
1568 b->width_run_cache = 0;
1570 b->width_table = Qnil;
1571 UNBLOCK_INPUT;
1572 b->undo_list = Qnil;
1574 return Qt;
1577 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1578 we do this each time BUF is selected visibly, the more recently
1579 selected buffers are always closer to the front of the list. This
1580 means that other_buffer is more likely to choose a relevant buffer. */
1582 void
1583 record_buffer (Lisp_Object buf)
1585 register Lisp_Object link, prev;
1586 Lisp_Object frame;
1587 frame = selected_frame;
1589 prev = Qnil;
1590 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1592 if (EQ (XCDR (XCAR (link)), buf))
1593 break;
1594 prev = link;
1597 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1598 we cannot use Fdelq itself here because it allows quitting. */
1600 if (NILP (prev))
1601 Vbuffer_alist = XCDR (Vbuffer_alist);
1602 else
1603 XSETCDR (prev, XCDR (XCDR (prev)));
1605 XSETCDR (link, Vbuffer_alist);
1606 Vbuffer_alist = link;
1608 /* Effectively do a delq on buried_buffer_list. */
1610 prev = Qnil;
1611 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1612 link = XCDR (link))
1614 if (EQ (XCAR (link), buf))
1616 if (NILP (prev))
1617 XFRAME (frame)->buried_buffer_list = XCDR (link);
1618 else
1619 XSETCDR (prev, XCDR (XCDR (prev)));
1620 break;
1622 prev = link;
1625 /* Now move this buffer to the front of frame_buffer_list also. */
1627 prev = Qnil;
1628 for (link = frame_buffer_list (frame); CONSP (link);
1629 link = XCDR (link))
1631 if (EQ (XCAR (link), buf))
1632 break;
1633 prev = link;
1636 /* Effectively do delq. */
1638 if (CONSP (link))
1640 if (NILP (prev))
1641 set_frame_buffer_list (frame,
1642 XCDR (frame_buffer_list (frame)));
1643 else
1644 XSETCDR (prev, XCDR (XCDR (prev)));
1646 XSETCDR (link, frame_buffer_list (frame));
1647 set_frame_buffer_list (frame, link);
1649 else
1650 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1653 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1654 doc: /* Set an appropriate major mode for BUFFER.
1655 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1656 according to `default-major-mode'.
1657 Use this function before selecting the buffer, since it may need to inspect
1658 the current buffer's major mode. */)
1659 (Lisp_Object buffer)
1661 int count;
1662 Lisp_Object function;
1664 CHECK_BUFFER (buffer);
1666 if (STRINGP (XBUFFER (buffer)->name)
1667 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1668 function = find_symbol_value (intern ("initial-major-mode"));
1669 else
1671 function = buffer_defaults.major_mode;
1672 if (NILP (function)
1673 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1674 function = current_buffer->major_mode;
1677 if (NILP (function) || EQ (function, Qfundamental_mode))
1678 return Qnil;
1680 count = SPECPDL_INDEX ();
1682 /* To select a nonfundamental mode,
1683 select the buffer temporarily and then call the mode function. */
1685 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1687 Fset_buffer (buffer);
1688 call0 (function);
1690 return unbind_to (count, Qnil);
1693 /* Switch to buffer BUFFER in the selected window.
1694 If NORECORD is non-nil, don't call record_buffer. */
1696 Lisp_Object
1697 switch_to_buffer_1 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1699 register Lisp_Object buffer;
1701 if (NILP (buffer_or_name))
1702 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1703 else
1705 buffer = Fget_buffer (buffer_or_name);
1706 if (NILP (buffer))
1708 buffer = Fget_buffer_create (buffer_or_name);
1709 Fset_buffer_major_mode (buffer);
1712 Fset_buffer (buffer);
1713 if (NILP (norecord))
1714 record_buffer (buffer);
1716 Fset_window_buffer (EQ (selected_window, minibuf_window)
1717 ? Fnext_window (minibuf_window, Qnil, Qnil)
1718 : selected_window,
1719 buffer, Qnil);
1721 return buffer;
1724 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1725 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1726 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1727 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1728 nil. Return the buffer switched to.
1730 If BUFFER-OR-NAME is a string and does not identify an existing
1731 buffer, create a new buffer with that name. Interactively, if
1732 `confirm-nonexistent-file-or-buffer' is non-nil, request
1733 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1734 nil, switch to buffer returned by `other-buffer'.
1736 Optional second arg NORECORD non-nil means do not put this buffer
1737 at the front of the list of recently selected ones. This
1738 function returns the buffer it switched to as a Lisp object.
1740 If the selected window is the minibuffer window or dedicated to
1741 its buffer, use `pop-to-buffer' for displaying the buffer.
1743 WARNING: This is NOT the way to work on another buffer temporarily
1744 within a Lisp program! Use `set-buffer' instead. That avoids
1745 messing with the window-buffer correspondences. */)
1746 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1748 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1750 /* Basically a NOP. Avoid signalling an error in the case where
1751 the selected window is dedicated, or a minibuffer. */
1753 /* But do put this buffer at the front of the buffer list, unless
1754 that has been inhibited. Note that even if BUFFER-OR-NAME is
1755 at the front of the main buffer-list already, we still want to
1756 move it to the front of the frame's buffer list. */
1757 if (NILP (norecord))
1758 record_buffer (buffer_or_name);
1759 return Fset_buffer (buffer_or_name);
1761 else if (EQ (minibuf_window, selected_window)
1762 /* If `dedicated' is neither nil nor t, it means it's
1763 dedicatedness can be overridden by an explicit request
1764 such as a call to switch-to-buffer. */
1765 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1766 /* We can't use the selected window so let `pop-to-buffer' try some
1767 other window. */
1768 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1769 else
1770 return switch_to_buffer_1 (buffer_or_name, norecord);
1773 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1774 doc: /* Return the current buffer as a Lisp object. */)
1775 (void)
1777 register Lisp_Object buf;
1778 XSETBUFFER (buf, current_buffer);
1779 return buf;
1782 /* Set the current buffer to B.
1784 We previously set windows_or_buffers_changed here to invalidate
1785 global unchanged information in beg_unchanged and end_unchanged.
1786 This is no longer necessary because we now compute unchanged
1787 information on a buffer-basis. Every action affecting other
1788 windows than the selected one requires a select_window at some
1789 time, and that increments windows_or_buffers_changed. */
1791 void
1792 set_buffer_internal (register struct buffer *b)
1794 if (current_buffer != b)
1795 set_buffer_internal_1 (b);
1798 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1799 This is used by redisplay. */
1801 void
1802 set_buffer_internal_1 (register struct buffer *b)
1804 register struct buffer *old_buf;
1805 register Lisp_Object tail;
1807 #ifdef USE_MMAP_FOR_BUFFERS
1808 if (b->text->beg == NULL)
1809 enlarge_buffer_text (b, 0);
1810 #endif /* USE_MMAP_FOR_BUFFERS */
1812 if (current_buffer == b)
1813 return;
1815 old_buf = current_buffer;
1816 current_buffer = b;
1817 last_known_column_point = -1; /* invalidate indentation cache */
1819 if (old_buf)
1821 /* Put the undo list back in the base buffer, so that it appears
1822 that an indirect buffer shares the undo list of its base. */
1823 if (old_buf->base_buffer)
1824 old_buf->base_buffer->undo_list = old_buf->undo_list;
1826 /* If the old current buffer has markers to record PT, BEGV and ZV
1827 when it is not current, update them now. */
1828 if (! NILP (old_buf->pt_marker))
1830 Lisp_Object obuf;
1831 XSETBUFFER (obuf, old_buf);
1832 set_marker_both (old_buf->pt_marker, obuf,
1833 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1835 if (! NILP (old_buf->begv_marker))
1837 Lisp_Object obuf;
1838 XSETBUFFER (obuf, old_buf);
1839 set_marker_both (old_buf->begv_marker, obuf,
1840 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1842 if (! NILP (old_buf->zv_marker))
1844 Lisp_Object obuf;
1845 XSETBUFFER (obuf, old_buf);
1846 set_marker_both (old_buf->zv_marker, obuf,
1847 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1851 /* Get the undo list from the base buffer, so that it appears
1852 that an indirect buffer shares the undo list of its base. */
1853 if (b->base_buffer)
1854 b->undo_list = b->base_buffer->undo_list;
1856 /* If the new current buffer has markers to record PT, BEGV and ZV
1857 when it is not current, fetch them now. */
1858 if (! NILP (b->pt_marker))
1860 BUF_PT (b) = marker_position (b->pt_marker);
1861 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1863 if (! NILP (b->begv_marker))
1865 BUF_BEGV (b) = marker_position (b->begv_marker);
1866 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1868 if (! NILP (b->zv_marker))
1870 BUF_ZV (b) = marker_position (b->zv_marker);
1871 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1874 /* Look down buffer's list of local Lisp variables
1875 to find and update any that forward into C variables. */
1879 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1881 Lisp_Object var = XCAR (XCAR (tail));
1882 struct Lisp_Symbol *sym = XSYMBOL (var);
1883 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1884 && SYMBOL_BLV (sym)->fwd)
1885 /* Just reference the variable
1886 to cause it to become set for this buffer. */
1887 Fsymbol_value (var);
1890 /* Do the same with any others that were local to the previous buffer */
1891 while (b != old_buf && (b = old_buf, b));
1894 /* Switch to buffer B temporarily for redisplay purposes.
1895 This avoids certain things that don't need to be done within redisplay. */
1897 void
1898 set_buffer_temp (struct buffer *b)
1900 register struct buffer *old_buf;
1902 if (current_buffer == b)
1903 return;
1905 old_buf = current_buffer;
1906 current_buffer = b;
1908 if (old_buf)
1910 /* If the old current buffer has markers to record PT, BEGV and ZV
1911 when it is not current, update them now. */
1912 if (! NILP (old_buf->pt_marker))
1914 Lisp_Object obuf;
1915 XSETBUFFER (obuf, old_buf);
1916 set_marker_both (old_buf->pt_marker, obuf,
1917 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1919 if (! NILP (old_buf->begv_marker))
1921 Lisp_Object obuf;
1922 XSETBUFFER (obuf, old_buf);
1923 set_marker_both (old_buf->begv_marker, obuf,
1924 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1926 if (! NILP (old_buf->zv_marker))
1928 Lisp_Object obuf;
1929 XSETBUFFER (obuf, old_buf);
1930 set_marker_both (old_buf->zv_marker, obuf,
1931 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1935 /* If the new current buffer has markers to record PT, BEGV and ZV
1936 when it is not current, fetch them now. */
1937 if (! NILP (b->pt_marker))
1939 BUF_PT (b) = marker_position (b->pt_marker);
1940 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1942 if (! NILP (b->begv_marker))
1944 BUF_BEGV (b) = marker_position (b->begv_marker);
1945 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1947 if (! NILP (b->zv_marker))
1949 BUF_ZV (b) = marker_position (b->zv_marker);
1950 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1954 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1955 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1956 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1957 also `save-excursion' when you want to make a buffer current
1958 temporarily. This function does not display the buffer, so its effect
1959 ends when the current command terminates. Use `switch-to-buffer' or
1960 `pop-to-buffer' to switch buffers permanently. */)
1961 (register Lisp_Object buffer_or_name)
1963 register Lisp_Object buffer;
1964 buffer = Fget_buffer (buffer_or_name);
1965 if (NILP (buffer))
1966 nsberror (buffer_or_name);
1967 if (NILP (XBUFFER (buffer)->name))
1968 error ("Selecting deleted buffer");
1969 set_buffer_internal (XBUFFER (buffer));
1970 return buffer;
1973 /* Set the current buffer to BUFFER provided it is alive. */
1975 Lisp_Object
1976 set_buffer_if_live (Lisp_Object buffer)
1978 if (! NILP (XBUFFER (buffer)->name))
1979 Fset_buffer (buffer);
1980 return Qnil;
1983 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1984 Sbarf_if_buffer_read_only, 0, 0, 0,
1985 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1986 (void)
1988 if (!NILP (current_buffer->read_only)
1989 && NILP (Vinhibit_read_only))
1990 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1991 return Qnil;
1994 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1995 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
1996 There it is the least likely candidate for `other-buffer' to return;
1997 thus, the least likely buffer for \\[switch-to-buffer] to select by
1998 default.
2000 The argument may be a buffer name or an actual buffer object. If
2001 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2002 from the selected window if it is displayed there. If the selected
2003 window is dedicated to its buffer, delete that window if there are other
2004 windows on the same frame. If the selected window is the only window on
2005 its frame, iconify that frame. */)
2006 (register Lisp_Object buffer_or_name)
2008 Lisp_Object buffer;
2010 /* Figure out what buffer we're going to bury. */
2011 if (NILP (buffer_or_name))
2013 Lisp_Object tem;
2014 XSETBUFFER (buffer, current_buffer);
2016 tem = Fwindow_buffer (selected_window);
2017 /* If we're burying the current buffer, unshow it. */
2018 if (EQ (buffer, tem))
2020 if (NILP (Fwindow_dedicated_p (selected_window)))
2021 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2022 else if (NILP (XWINDOW (selected_window)->parent))
2023 Ficonify_frame (Fwindow_frame (selected_window));
2024 else
2025 Fdelete_window (selected_window);
2028 else
2030 buffer = Fget_buffer (buffer_or_name);
2031 if (NILP (buffer))
2032 nsberror (buffer_or_name);
2035 /* Move buffer to the end of the buffer list. Do nothing if the
2036 buffer is killed. */
2037 if (!NILP (XBUFFER (buffer)->name))
2039 Lisp_Object aelt, link;
2041 aelt = Frassq (buffer, Vbuffer_alist);
2042 link = Fmemq (aelt, Vbuffer_alist);
2043 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2044 XSETCDR (link, Qnil);
2045 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2047 XFRAME (selected_frame)->buffer_list
2048 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2049 XFRAME (selected_frame)->buried_buffer_list
2050 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2053 return Qnil;
2056 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2057 doc: /* Delete the entire contents of the current buffer.
2058 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2059 so the buffer is truly empty after this. */)
2060 (void)
2062 Fwiden ();
2064 del_range (BEG, Z);
2066 current_buffer->last_window_start = 1;
2067 /* Prevent warnings, or suspension of auto saving, that would happen
2068 if future size is less than past size. Use of erase-buffer
2069 implies that the future text is not really related to the past text. */
2070 XSETFASTINT (current_buffer->save_length, 0);
2071 return Qnil;
2074 void
2075 validate_region (register Lisp_Object *b, register Lisp_Object *e)
2077 CHECK_NUMBER_COERCE_MARKER (*b);
2078 CHECK_NUMBER_COERCE_MARKER (*e);
2080 if (XINT (*b) > XINT (*e))
2082 Lisp_Object tem;
2083 tem = *b; *b = *e; *e = tem;
2086 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2087 && XINT (*e) <= ZV))
2088 args_out_of_range (*b, *e);
2091 /* Advance BYTE_POS up to a character boundary
2092 and return the adjusted position. */
2094 static int
2095 advance_to_char_boundary (EMACS_INT byte_pos)
2097 int c;
2099 if (byte_pos == BEG)
2100 /* Beginning of buffer is always a character boundary. */
2101 return BEG;
2103 c = FETCH_BYTE (byte_pos);
2104 if (! CHAR_HEAD_P (c))
2106 /* We should advance BYTE_POS only when C is a constituent of a
2107 multibyte sequence. */
2108 EMACS_INT orig_byte_pos = byte_pos;
2112 byte_pos--;
2113 c = FETCH_BYTE (byte_pos);
2115 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2116 INC_POS (byte_pos);
2117 if (byte_pos < orig_byte_pos)
2118 byte_pos = orig_byte_pos;
2119 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2120 surely advance to the correct character boundary. If C is
2121 not, BYTE_POS was unchanged. */
2124 return byte_pos;
2127 #ifdef REL_ALLOC
2128 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2129 #endif /* REL_ALLOC */
2131 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2132 1, 1, 0,
2133 doc: /* Swap the text between current buffer and BUFFER. */)
2134 (Lisp_Object buffer)
2136 struct buffer *other_buffer;
2137 CHECK_BUFFER (buffer);
2138 other_buffer = XBUFFER (buffer);
2140 if (NILP (other_buffer->name))
2141 error ("Cannot swap a dead buffer's text");
2143 /* Actually, it probably works just fine.
2144 * if (other_buffer == current_buffer)
2145 * error ("Cannot swap a buffer's text with itself"); */
2147 /* Actually, this may be workable as well, tho probably only if they're
2148 *both* indirect. */
2149 if (other_buffer->base_buffer
2150 || current_buffer->base_buffer)
2151 error ("Cannot swap indirect buffers's text");
2153 { /* This is probably harder to make work. */
2154 struct buffer *other;
2155 for (other = all_buffers; other; other = other->next)
2156 if (other->base_buffer == other_buffer
2157 || other->base_buffer == current_buffer)
2158 error ("One of the buffers to swap has indirect buffers");
2161 #define swapfield(field, type) \
2162 do { \
2163 type tmp##field = other_buffer->field; \
2164 other_buffer->field = current_buffer->field; \
2165 current_buffer->field = tmp##field; \
2166 } while (0)
2168 swapfield (own_text, struct buffer_text);
2169 eassert (current_buffer->text == &current_buffer->own_text);
2170 eassert (other_buffer->text == &other_buffer->own_text);
2171 #ifdef REL_ALLOC
2172 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2173 (POINTER_TYPE **) &other_buffer->own_text.beg);
2174 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2175 (POINTER_TYPE **) &current_buffer->own_text.beg);
2176 #endif /* REL_ALLOC */
2178 swapfield (pt, EMACS_INT);
2179 swapfield (pt_byte, EMACS_INT);
2180 swapfield (begv, EMACS_INT);
2181 swapfield (begv_byte, EMACS_INT);
2182 swapfield (zv, EMACS_INT);
2183 swapfield (zv_byte, EMACS_INT);
2184 eassert (!current_buffer->base_buffer);
2185 eassert (!other_buffer->base_buffer);
2186 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2187 swapfield (newline_cache, struct region_cache *);
2188 swapfield (width_run_cache, struct region_cache *);
2189 current_buffer->prevent_redisplay_optimizations_p = 1;
2190 other_buffer->prevent_redisplay_optimizations_p = 1;
2191 swapfield (overlays_before, struct Lisp_Overlay *);
2192 swapfield (overlays_after, struct Lisp_Overlay *);
2193 swapfield (overlay_center, EMACS_INT);
2194 swapfield (undo_list, Lisp_Object);
2195 swapfield (mark, Lisp_Object);
2196 swapfield (enable_multibyte_characters, Lisp_Object);
2197 swapfield (bidi_display_reordering, Lisp_Object);
2198 swapfield (bidi_paragraph_direction, Lisp_Object);
2199 /* FIXME: Not sure what we should do with these *_marker fields.
2200 Hopefully they're just nil anyway. */
2201 swapfield (pt_marker, Lisp_Object);
2202 swapfield (begv_marker, Lisp_Object);
2203 swapfield (zv_marker, Lisp_Object);
2204 current_buffer->point_before_scroll = Qnil;
2205 other_buffer->point_before_scroll = Qnil;
2207 current_buffer->text->modiff++; other_buffer->text->modiff++;
2208 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2209 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2210 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2211 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2212 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2213 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2215 struct Lisp_Marker *m;
2216 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2217 if (m->buffer == other_buffer)
2218 m->buffer = current_buffer;
2219 else
2220 /* Since there's no indirect buffer in sight, markers on
2221 BUF_MARKERS(buf) should either be for `buf' or dead. */
2222 eassert (!m->buffer);
2223 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2224 if (m->buffer == current_buffer)
2225 m->buffer = other_buffer;
2226 else
2227 /* Since there's no indirect buffer in sight, markers on
2228 BUF_MARKERS(buf) should either be for `buf' or dead. */
2229 eassert (!m->buffer);
2231 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2232 So since we just swapped the markers between the two buffers, we need
2233 to undo the effect of this swap for window markers. */
2234 Lisp_Object w = Fselected_window (), ws = Qnil;
2235 Lisp_Object buf1, buf2;
2236 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2238 while (NILP (Fmemq (w, ws)))
2240 ws = Fcons (w, ws);
2241 if (MARKERP (XWINDOW (w)->pointm)
2242 && (EQ (XWINDOW (w)->buffer, buf1)
2243 || EQ (XWINDOW (w)->buffer, buf2)))
2244 Fset_marker (XWINDOW (w)->pointm,
2245 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2246 XWINDOW (w)->buffer);
2247 w = Fnext_window (w, Qt, Qt);
2251 if (current_buffer->text->intervals)
2252 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2253 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2254 if (other_buffer->text->intervals)
2255 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2256 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2258 return Qnil;
2261 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2262 1, 1, 0,
2263 doc: /* Set the multibyte flag of the current buffer to FLAG.
2264 If FLAG is t, this makes the buffer a multibyte buffer.
2265 If FLAG is nil, this makes the buffer a single-byte buffer.
2266 In these cases, the buffer contents remain unchanged as a sequence of
2267 bytes but the contents viewed as characters do change.
2268 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2269 all eight-bit bytes to eight-bit characters.
2270 If the multibyte flag was really changed, undo information of the
2271 current buffer is cleared. */)
2272 (Lisp_Object flag)
2274 struct Lisp_Marker *tail, *markers;
2275 struct buffer *other;
2276 EMACS_INT begv, zv;
2277 int narrowed = (BEG != BEGV || Z != ZV);
2278 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2279 Lisp_Object old_undo = current_buffer->undo_list;
2280 struct gcpro gcpro1;
2282 if (current_buffer->base_buffer)
2283 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2285 /* Do nothing if nothing actually changes. */
2286 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2287 return flag;
2289 GCPRO1 (old_undo);
2291 /* Don't record these buffer changes. We will put a special undo entry
2292 instead. */
2293 current_buffer->undo_list = Qt;
2295 /* If the cached position is for this buffer, clear it out. */
2296 clear_charpos_cache (current_buffer);
2298 if (NILP (flag))
2299 begv = BEGV_BYTE, zv = ZV_BYTE;
2300 else
2301 begv = BEGV, zv = ZV;
2303 if (narrowed)
2304 Fwiden ();
2306 if (NILP (flag))
2308 EMACS_INT pos, stop;
2309 unsigned char *p;
2311 /* Do this first, so it can use CHAR_TO_BYTE
2312 to calculate the old correspondences. */
2313 set_intervals_multibyte (0);
2315 current_buffer->enable_multibyte_characters = Qnil;
2317 Z = Z_BYTE;
2318 BEGV = BEGV_BYTE;
2319 ZV = ZV_BYTE;
2320 GPT = GPT_BYTE;
2321 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2324 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2325 tail->charpos = tail->bytepos;
2327 /* Convert multibyte form of 8-bit characters to unibyte. */
2328 pos = BEG;
2329 stop = GPT;
2330 p = BEG_ADDR;
2331 while (1)
2333 int c, bytes;
2335 if (pos == stop)
2337 if (pos == Z)
2338 break;
2339 p = GAP_END_ADDR;
2340 stop = Z;
2342 if (ASCII_BYTE_P (*p))
2343 p++, pos++;
2344 else if (CHAR_BYTE8_HEAD_P (*p))
2346 c = STRING_CHAR_AND_LENGTH (p, bytes);
2347 /* Delete all bytes for this 8-bit character but the
2348 last one, and change the last one to the character
2349 code. */
2350 bytes--;
2351 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2352 p = GAP_END_ADDR;
2353 *p++ = c;
2354 pos++;
2355 if (begv > pos)
2356 begv -= bytes;
2357 if (zv > pos)
2358 zv -= bytes;
2359 stop = Z;
2361 else
2363 bytes = BYTES_BY_CHAR_HEAD (*p);
2364 p += bytes, pos += bytes;
2367 if (narrowed)
2368 Fnarrow_to_region (make_number (begv), make_number (zv));
2370 else
2372 EMACS_INT pt = PT;
2373 EMACS_INT pos, stop;
2374 unsigned char *p, *pend;
2376 /* Be sure not to have a multibyte sequence striding over the GAP.
2377 Ex: We change this: "...abc\302 _GAP_ \241def..."
2378 to: "...abc _GAP_ \302\241def..." */
2380 if (EQ (flag, Qt)
2381 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2382 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2384 unsigned char *p = GPT_ADDR - 1;
2386 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2387 if (LEADING_CODE_P (*p))
2389 EMACS_INT new_gpt = GPT_BYTE - (GPT_ADDR - p);
2391 move_gap_both (new_gpt, new_gpt);
2395 /* Make the buffer contents valid as multibyte by converting
2396 8-bit characters to multibyte form. */
2397 pos = BEG;
2398 stop = GPT;
2399 p = BEG_ADDR;
2400 pend = GPT_ADDR;
2401 while (1)
2403 int bytes;
2405 if (pos == stop)
2407 if (pos == Z)
2408 break;
2409 p = GAP_END_ADDR;
2410 pend = Z_ADDR;
2411 stop = Z;
2414 if (ASCII_BYTE_P (*p))
2415 p++, pos++;
2416 else if (EQ (flag, Qt)
2417 && ! CHAR_BYTE8_HEAD_P (*p)
2418 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2419 p += bytes, pos += bytes;
2420 else
2422 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2423 int c;
2425 c = BYTE8_TO_CHAR (*p);
2426 bytes = CHAR_STRING (c, tmp);
2427 *p = tmp[0];
2428 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2429 bytes--;
2430 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2431 /* Now the gap is after the just inserted data. */
2432 pos = GPT;
2433 p = GAP_END_ADDR;
2434 if (pos <= begv)
2435 begv += bytes;
2436 if (pos <= zv)
2437 zv += bytes;
2438 if (pos <= pt)
2439 pt += bytes;
2440 pend = Z_ADDR;
2441 stop = Z;
2445 if (pt != PT)
2446 TEMP_SET_PT (pt);
2448 if (narrowed)
2449 Fnarrow_to_region (make_number (begv), make_number (zv));
2451 /* Do this first, so that chars_in_text asks the right question.
2452 set_intervals_multibyte needs it too. */
2453 current_buffer->enable_multibyte_characters = Qt;
2455 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2456 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2458 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2460 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2461 if (BEGV_BYTE > GPT_BYTE)
2462 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2463 else
2464 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2466 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2467 if (ZV_BYTE > GPT_BYTE)
2468 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2469 else
2470 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2473 EMACS_INT pt_byte = advance_to_char_boundary (PT_BYTE);
2474 EMACS_INT pt;
2476 if (pt_byte > GPT_BYTE)
2477 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2478 else
2479 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2480 TEMP_SET_PT_BOTH (pt, pt_byte);
2483 tail = markers = BUF_MARKERS (current_buffer);
2485 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2486 getting confused by the markers that have not yet been updated.
2487 It is also a signal that it should never create a marker. */
2488 BUF_MARKERS (current_buffer) = NULL;
2490 for (; tail; tail = tail->next)
2492 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2493 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2496 /* Make sure no markers were put on the chain
2497 while the chain value was incorrect. */
2498 if (BUF_MARKERS (current_buffer))
2499 abort ();
2501 BUF_MARKERS (current_buffer) = markers;
2503 /* Do this last, so it can calculate the new correspondences
2504 between chars and bytes. */
2505 set_intervals_multibyte (1);
2508 if (!EQ (old_undo, Qt))
2510 /* Represent all the above changes by a special undo entry. */
2511 current_buffer->undo_list = Fcons (list3 (Qapply,
2512 intern ("set-buffer-multibyte"),
2513 NILP (flag) ? Qt : Qnil),
2514 old_undo);
2517 UNGCPRO;
2519 /* Changing the multibyteness of a buffer means that all windows
2520 showing that buffer must be updated thoroughly. */
2521 current_buffer->prevent_redisplay_optimizations_p = 1;
2522 ++windows_or_buffers_changed;
2524 /* Copy this buffer's new multibyte status
2525 into all of its indirect buffers. */
2526 for (other = all_buffers; other; other = other->next)
2527 if (other->base_buffer == current_buffer && !NILP (other->name))
2529 other->enable_multibyte_characters
2530 = current_buffer->enable_multibyte_characters;
2531 other->prevent_redisplay_optimizations_p = 1;
2534 /* Restore the modifiedness of the buffer. */
2535 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2536 Fset_buffer_modified_p (Qnil);
2538 /* Update coding systems of this buffer's process (if any). */
2540 Lisp_Object process;
2542 process = Fget_buffer_process (Fcurrent_buffer ());
2543 if (PROCESSP (process))
2544 setup_process_coding_systems (process);
2547 return flag;
2550 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2551 0, 0, 0,
2552 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2553 Most local variable bindings are eliminated so that the default values
2554 become effective once more. Also, the syntax table is set from
2555 `standard-syntax-table', the local keymap is set to nil,
2556 and the abbrev table from `fundamental-mode-abbrev-table'.
2557 This function also forces redisplay of the mode line.
2559 Every function to select a new major mode starts by
2560 calling this function.
2562 As a special exception, local variables whose names have
2563 a non-nil `permanent-local' property are not eliminated by this function.
2565 The first thing this function does is run
2566 the normal hook `change-major-mode-hook'. */)
2567 (void)
2569 if (!NILP (Vrun_hooks))
2570 call1 (Vrun_hooks, Qchange_major_mode_hook);
2572 /* Make sure none of the bindings in local_var_alist
2573 remain swapped in, in their symbols. */
2575 swap_out_buffer_local_variables (current_buffer);
2577 /* Actually eliminate all local bindings of this buffer. */
2579 reset_buffer_local_variables (current_buffer, 0);
2581 /* Force mode-line redisplay. Useful here because all major mode
2582 commands call this function. */
2583 update_mode_lines++;
2585 return Qnil;
2588 /* Make sure no local variables remain set up with buffer B
2589 for their current values. */
2591 static void
2592 swap_out_buffer_local_variables (struct buffer *b)
2594 Lisp_Object oalist, alist, buffer;
2596 XSETBUFFER (buffer, b);
2597 oalist = b->local_var_alist;
2599 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2601 Lisp_Object sym = XCAR (XCAR (alist));
2602 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2603 /* Need not do anything if some other buffer's binding is
2604 now encached. */
2605 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2607 /* Symbol is set up for this buffer's old local value:
2608 swap it out! */
2609 swap_in_global_binding (XSYMBOL (sym));
2614 /* Find all the overlays in the current buffer that contain position POS.
2615 Return the number found, and store them in a vector in *VEC_PTR.
2616 Store in *LEN_PTR the size allocated for the vector.
2617 Store in *NEXT_PTR the next position after POS where an overlay starts,
2618 or ZV if there are no more overlays between POS and ZV.
2619 Store in *PREV_PTR the previous position before POS where an overlay ends,
2620 or where an overlay starts which ends at or after POS;
2621 or BEGV if there are no such overlays from BEGV to POS.
2622 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2624 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2625 when this function is called.
2627 If EXTEND is non-zero, we make the vector bigger if necessary.
2628 If EXTEND is zero, we never extend the vector,
2629 and we store only as many overlays as will fit.
2630 But we still return the total number of overlays.
2632 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2633 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2634 default (BEGV or ZV). */
2637 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2638 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2640 Lisp_Object overlay, start, end;
2641 struct Lisp_Overlay *tail;
2642 int idx = 0;
2643 int len = *len_ptr;
2644 Lisp_Object *vec = *vec_ptr;
2645 EMACS_INT next = ZV;
2646 EMACS_INT prev = BEGV;
2647 int inhibit_storing = 0;
2649 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2651 EMACS_INT startpos, endpos;
2653 XSETMISC (overlay, tail);
2655 start = OVERLAY_START (overlay);
2656 end = OVERLAY_END (overlay);
2657 endpos = OVERLAY_POSITION (end);
2658 if (endpos < pos)
2660 if (prev < endpos)
2661 prev = endpos;
2662 break;
2664 startpos = OVERLAY_POSITION (start);
2665 /* This one ends at or after POS
2666 so its start counts for PREV_PTR if it's before POS. */
2667 if (prev < startpos && startpos < pos)
2668 prev = startpos;
2669 if (endpos == pos)
2670 continue;
2671 if (startpos <= pos)
2673 if (idx == len)
2675 /* The supplied vector is full.
2676 Either make it bigger, or don't store any more in it. */
2677 if (extend)
2679 /* Make it work with an initial len == 0. */
2680 len *= 2;
2681 if (len == 0)
2682 len = 4;
2683 *len_ptr = len;
2684 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2685 *vec_ptr = vec;
2687 else
2688 inhibit_storing = 1;
2691 if (!inhibit_storing)
2692 vec[idx] = overlay;
2693 /* Keep counting overlays even if we can't return them all. */
2694 idx++;
2696 else if (startpos < next)
2697 next = startpos;
2700 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2702 EMACS_INT startpos, endpos;
2704 XSETMISC (overlay, tail);
2706 start = OVERLAY_START (overlay);
2707 end = OVERLAY_END (overlay);
2708 startpos = OVERLAY_POSITION (start);
2709 if (pos < startpos)
2711 if (startpos < next)
2712 next = startpos;
2713 break;
2715 endpos = OVERLAY_POSITION (end);
2716 if (pos < endpos)
2718 if (idx == len)
2720 if (extend)
2722 /* Make it work with an initial len == 0. */
2723 len *= 2;
2724 if (len == 0)
2725 len = 4;
2726 *len_ptr = len;
2727 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2728 *vec_ptr = vec;
2730 else
2731 inhibit_storing = 1;
2734 if (!inhibit_storing)
2735 vec[idx] = overlay;
2736 idx++;
2738 if (startpos < pos && startpos > prev)
2739 prev = startpos;
2741 else if (endpos < pos && endpos > prev)
2742 prev = endpos;
2743 else if (endpos == pos && startpos > prev
2744 && (!change_req || startpos < pos))
2745 prev = startpos;
2748 if (next_ptr)
2749 *next_ptr = next;
2750 if (prev_ptr)
2751 *prev_ptr = prev;
2752 return idx;
2755 /* Find all the overlays in the current buffer that overlap the range
2756 BEG-END, or are empty at BEG, or are empty at END provided END
2757 denotes the position at the end of the current buffer.
2759 Return the number found, and store them in a vector in *VEC_PTR.
2760 Store in *LEN_PTR the size allocated for the vector.
2761 Store in *NEXT_PTR the next position after POS where an overlay starts,
2762 or ZV if there are no more overlays.
2763 Store in *PREV_PTR the previous position before POS where an overlay ends,
2764 or BEGV if there are no previous overlays.
2765 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2767 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2768 when this function is called.
2770 If EXTEND is non-zero, we make the vector bigger if necessary.
2771 If EXTEND is zero, we never extend the vector,
2772 and we store only as many overlays as will fit.
2773 But we still return the total number of overlays. */
2775 static int
2776 overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
2777 Lisp_Object **vec_ptr, int *len_ptr,
2778 EMACS_INT *next_ptr, EMACS_INT *prev_ptr)
2780 Lisp_Object overlay, ostart, oend;
2781 struct Lisp_Overlay *tail;
2782 int idx = 0;
2783 int len = *len_ptr;
2784 Lisp_Object *vec = *vec_ptr;
2785 EMACS_INT next = ZV;
2786 EMACS_INT prev = BEGV;
2787 int inhibit_storing = 0;
2788 int end_is_Z = end == Z;
2790 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2792 EMACS_INT startpos, endpos;
2794 XSETMISC (overlay, tail);
2796 ostart = OVERLAY_START (overlay);
2797 oend = OVERLAY_END (overlay);
2798 endpos = OVERLAY_POSITION (oend);
2799 if (endpos < beg)
2801 if (prev < endpos)
2802 prev = endpos;
2803 break;
2805 startpos = OVERLAY_POSITION (ostart);
2806 /* Count an interval if it overlaps the range, is empty at the
2807 start of the range, or is empty at END provided END denotes the
2808 end of the buffer. */
2809 if ((beg < endpos && startpos < end)
2810 || (startpos == endpos
2811 && (beg == endpos || (end_is_Z && endpos == end))))
2813 if (idx == len)
2815 /* The supplied vector is full.
2816 Either make it bigger, or don't store any more in it. */
2817 if (extend)
2819 /* Make it work with an initial len == 0. */
2820 len *= 2;
2821 if (len == 0)
2822 len = 4;
2823 *len_ptr = len;
2824 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2825 *vec_ptr = vec;
2827 else
2828 inhibit_storing = 1;
2831 if (!inhibit_storing)
2832 vec[idx] = overlay;
2833 /* Keep counting overlays even if we can't return them all. */
2834 idx++;
2836 else if (startpos < next)
2837 next = startpos;
2840 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2842 EMACS_INT startpos, endpos;
2844 XSETMISC (overlay, tail);
2846 ostart = OVERLAY_START (overlay);
2847 oend = OVERLAY_END (overlay);
2848 startpos = OVERLAY_POSITION (ostart);
2849 if (end < startpos)
2851 if (startpos < next)
2852 next = startpos;
2853 break;
2855 endpos = OVERLAY_POSITION (oend);
2856 /* Count an interval if it overlaps the range, is empty at the
2857 start of the range, or is empty at END provided END denotes the
2858 end of the buffer. */
2859 if ((beg < endpos && startpos < end)
2860 || (startpos == endpos
2861 && (beg == endpos || (end_is_Z && endpos == end))))
2863 if (idx == len)
2865 if (extend)
2867 /* Make it work with an initial len == 0. */
2868 len *= 2;
2869 if (len == 0)
2870 len = 4;
2871 *len_ptr = len;
2872 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2873 *vec_ptr = vec;
2875 else
2876 inhibit_storing = 1;
2879 if (!inhibit_storing)
2880 vec[idx] = overlay;
2881 idx++;
2883 else if (endpos < beg && endpos > prev)
2884 prev = endpos;
2887 if (next_ptr)
2888 *next_ptr = next;
2889 if (prev_ptr)
2890 *prev_ptr = prev;
2891 return idx;
2895 /* Return non-zero if there exists an overlay with a non-nil
2896 `mouse-face' property overlapping OVERLAY. */
2899 mouse_face_overlay_overlaps (Lisp_Object overlay)
2901 EMACS_INT start = OVERLAY_POSITION (OVERLAY_START (overlay));
2902 EMACS_INT end = OVERLAY_POSITION (OVERLAY_END (overlay));
2903 int n, i, size;
2904 Lisp_Object *v, tem;
2906 size = 10;
2907 v = (Lisp_Object *) alloca (size * sizeof *v);
2908 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2909 if (n > size)
2911 v = (Lisp_Object *) alloca (n * sizeof *v);
2912 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2915 for (i = 0; i < n; ++i)
2916 if (!EQ (v[i], overlay)
2917 && (tem = Foverlay_get (overlay, Qmouse_face),
2918 !NILP (tem)))
2919 break;
2921 return i < n;
2926 /* Fast function to just test if we're at an overlay boundary. */
2928 overlay_touches_p (EMACS_INT pos)
2930 Lisp_Object overlay;
2931 struct Lisp_Overlay *tail;
2933 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2935 EMACS_INT endpos;
2937 XSETMISC (overlay ,tail);
2938 if (!OVERLAYP (overlay))
2939 abort ();
2941 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2942 if (endpos < pos)
2943 break;
2944 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2945 return 1;
2948 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2950 EMACS_INT startpos;
2952 XSETMISC (overlay, tail);
2953 if (!OVERLAYP (overlay))
2954 abort ();
2956 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2957 if (pos < startpos)
2958 break;
2959 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2960 return 1;
2962 return 0;
2965 struct sortvec
2967 Lisp_Object overlay;
2968 EMACS_INT beg, end;
2969 int priority;
2972 static int
2973 compare_overlays (const void *v1, const void *v2)
2975 const struct sortvec *s1 = (const struct sortvec *) v1;
2976 const struct sortvec *s2 = (const struct sortvec *) v2;
2977 if (s1->priority != s2->priority)
2978 return s1->priority - s2->priority;
2979 if (s1->beg != s2->beg)
2980 return s1->beg - s2->beg;
2981 if (s1->end != s2->end)
2982 return s2->end - s1->end;
2983 return 0;
2986 /* Sort an array of overlays by priority. The array is modified in place.
2987 The return value is the new size; this may be smaller than the original
2988 size if some of the overlays were invalid or were window-specific. */
2990 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2992 int i, j;
2993 struct sortvec *sortvec;
2994 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2996 /* Put the valid and relevant overlays into sortvec. */
2998 for (i = 0, j = 0; i < noverlays; i++)
3000 Lisp_Object tem;
3001 Lisp_Object overlay;
3003 overlay = overlay_vec[i];
3004 if (OVERLAY_VALID (overlay)
3005 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3006 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3008 /* If we're interested in a specific window, then ignore
3009 overlays that are limited to some other window. */
3010 if (w)
3012 Lisp_Object window;
3014 window = Foverlay_get (overlay, Qwindow);
3015 if (WINDOWP (window) && XWINDOW (window) != w)
3016 continue;
3019 /* This overlay is good and counts: put it into sortvec. */
3020 sortvec[j].overlay = overlay;
3021 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3022 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3023 tem = Foverlay_get (overlay, Qpriority);
3024 if (INTEGERP (tem))
3025 sortvec[j].priority = XINT (tem);
3026 else
3027 sortvec[j].priority = 0;
3028 j++;
3031 noverlays = j;
3033 /* Sort the overlays into the proper order: increasing priority. */
3035 if (noverlays > 1)
3036 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3038 for (i = 0; i < noverlays; i++)
3039 overlay_vec[i] = sortvec[i].overlay;
3040 return (noverlays);
3043 struct sortstr
3045 Lisp_Object string, string2;
3046 int size;
3047 int priority;
3050 struct sortstrlist
3052 struct sortstr *buf; /* An array that expands as needed; never freed. */
3053 int size; /* Allocated length of that array. */
3054 int used; /* How much of the array is currently in use. */
3055 EMACS_INT bytes; /* Total length of the strings in buf. */
3058 /* Buffers for storing information about the overlays touching a given
3059 position. These could be automatic variables in overlay_strings, but
3060 it's more efficient to hold onto the memory instead of repeatedly
3061 allocating and freeing it. */
3062 static struct sortstrlist overlay_heads, overlay_tails;
3063 static unsigned char *overlay_str_buf;
3065 /* Allocated length of overlay_str_buf. */
3066 static EMACS_INT overlay_str_len;
3068 /* A comparison function suitable for passing to qsort. */
3069 static int
3070 cmp_for_strings (const void *as1, const void *as2)
3072 struct sortstr *s1 = (struct sortstr *)as1;
3073 struct sortstr *s2 = (struct sortstr *)as2;
3074 if (s1->size != s2->size)
3075 return s2->size - s1->size;
3076 if (s1->priority != s2->priority)
3077 return s1->priority - s2->priority;
3078 return 0;
3081 static void
3082 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
3084 EMACS_INT nbytes;
3086 if (ssl->used == ssl->size)
3088 if (ssl->buf)
3089 ssl->size *= 2;
3090 else
3091 ssl->size = 5;
3092 ssl->buf = ((struct sortstr *)
3093 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3095 ssl->buf[ssl->used].string = str;
3096 ssl->buf[ssl->used].string2 = str2;
3097 ssl->buf[ssl->used].size = size;
3098 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3099 ssl->used++;
3101 if (NILP (current_buffer->enable_multibyte_characters))
3102 nbytes = SCHARS (str);
3103 else if (! STRING_MULTIBYTE (str))
3104 nbytes = count_size_as_multibyte (SDATA (str),
3105 SBYTES (str));
3106 else
3107 nbytes = SBYTES (str);
3109 ssl->bytes += nbytes;
3111 if (STRINGP (str2))
3113 if (NILP (current_buffer->enable_multibyte_characters))
3114 nbytes = SCHARS (str2);
3115 else if (! STRING_MULTIBYTE (str2))
3116 nbytes = count_size_as_multibyte (SDATA (str2),
3117 SBYTES (str2));
3118 else
3119 nbytes = SBYTES (str2);
3121 ssl->bytes += nbytes;
3125 /* Return the concatenation of the strings associated with overlays that
3126 begin or end at POS, ignoring overlays that are specific to a window
3127 other than W. The strings are concatenated in the appropriate order:
3128 shorter overlays nest inside longer ones, and higher priority inside
3129 lower. Normally all of the after-strings come first, but zero-sized
3130 overlays have their after-strings ride along with the before-strings
3131 because it would look strange to print them inside-out.
3133 Returns the string length, and stores the contents indirectly through
3134 PSTR, if that variable is non-null. The string may be overwritten by
3135 subsequent calls. */
3137 EMACS_INT
3138 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3140 Lisp_Object overlay, window, str;
3141 struct Lisp_Overlay *ov;
3142 EMACS_INT startpos, endpos;
3143 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3145 overlay_heads.used = overlay_heads.bytes = 0;
3146 overlay_tails.used = overlay_tails.bytes = 0;
3147 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3149 XSETMISC (overlay, ov);
3150 eassert (OVERLAYP (overlay));
3152 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3153 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3154 if (endpos < pos)
3155 break;
3156 if (endpos != pos && startpos != pos)
3157 continue;
3158 window = Foverlay_get (overlay, Qwindow);
3159 if (WINDOWP (window) && XWINDOW (window) != w)
3160 continue;
3161 if (startpos == pos
3162 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3163 record_overlay_string (&overlay_heads, str,
3164 (startpos == endpos
3165 ? Foverlay_get (overlay, Qafter_string)
3166 : Qnil),
3167 Foverlay_get (overlay, Qpriority),
3168 endpos - startpos);
3169 else if (endpos == pos
3170 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3171 record_overlay_string (&overlay_tails, str, Qnil,
3172 Foverlay_get (overlay, Qpriority),
3173 endpos - startpos);
3175 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3177 XSETMISC (overlay, ov);
3178 eassert (OVERLAYP (overlay));
3180 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3181 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3182 if (startpos > pos)
3183 break;
3184 if (endpos != pos && startpos != pos)
3185 continue;
3186 window = Foverlay_get (overlay, Qwindow);
3187 if (WINDOWP (window) && XWINDOW (window) != w)
3188 continue;
3189 if (startpos == pos
3190 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3191 record_overlay_string (&overlay_heads, str,
3192 (startpos == endpos
3193 ? Foverlay_get (overlay, Qafter_string)
3194 : Qnil),
3195 Foverlay_get (overlay, Qpriority),
3196 endpos - startpos);
3197 else if (endpos == pos
3198 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3199 record_overlay_string (&overlay_tails, str, Qnil,
3200 Foverlay_get (overlay, Qpriority),
3201 endpos - startpos);
3203 if (overlay_tails.used > 1)
3204 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3205 cmp_for_strings);
3206 if (overlay_heads.used > 1)
3207 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3208 cmp_for_strings);
3209 if (overlay_heads.bytes || overlay_tails.bytes)
3211 Lisp_Object tem;
3212 EMACS_INT i;
3213 unsigned char *p;
3214 EMACS_INT total = overlay_heads.bytes + overlay_tails.bytes;
3216 if (total > overlay_str_len)
3218 overlay_str_len = total;
3219 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3220 total);
3222 p = overlay_str_buf;
3223 for (i = overlay_tails.used; --i >= 0;)
3225 EMACS_INT nbytes;
3226 tem = overlay_tails.buf[i].string;
3227 nbytes = copy_text (SDATA (tem), p,
3228 SBYTES (tem),
3229 STRING_MULTIBYTE (tem), multibyte);
3230 p += nbytes;
3232 for (i = 0; i < overlay_heads.used; ++i)
3234 EMACS_INT nbytes;
3235 tem = overlay_heads.buf[i].string;
3236 nbytes = copy_text (SDATA (tem), p,
3237 SBYTES (tem),
3238 STRING_MULTIBYTE (tem), multibyte);
3239 p += nbytes;
3240 tem = overlay_heads.buf[i].string2;
3241 if (STRINGP (tem))
3243 nbytes = copy_text (SDATA (tem), p,
3244 SBYTES (tem),
3245 STRING_MULTIBYTE (tem), multibyte);
3246 p += nbytes;
3249 if (p != overlay_str_buf + total)
3250 abort ();
3251 if (pstr)
3252 *pstr = overlay_str_buf;
3253 return total;
3255 return 0;
3258 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3260 void
3261 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3263 Lisp_Object overlay, beg, end;
3264 struct Lisp_Overlay *prev, *tail, *next;
3266 /* See if anything in overlays_before should move to overlays_after. */
3268 /* We don't strictly need prev in this loop; it should always be nil.
3269 But we use it for symmetry and in case that should cease to be true
3270 with some future change. */
3271 prev = NULL;
3272 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3274 next = tail->next;
3275 XSETMISC (overlay, tail);
3277 /* If the overlay is not valid, get rid of it. */
3278 if (!OVERLAY_VALID (overlay))
3279 #if 1
3280 abort ();
3281 #else
3283 /* Splice the cons cell TAIL out of overlays_before. */
3284 if (!NILP (prev))
3285 XCDR (prev) = next;
3286 else
3287 buf->overlays_before = next;
3288 tail = prev;
3289 continue;
3291 #endif
3293 beg = OVERLAY_START (overlay);
3294 end = OVERLAY_END (overlay);
3296 if (OVERLAY_POSITION (end) > pos)
3298 /* OVERLAY needs to be moved. */
3299 EMACS_INT where = OVERLAY_POSITION (beg);
3300 struct Lisp_Overlay *other, *other_prev;
3302 /* Splice the cons cell TAIL out of overlays_before. */
3303 if (prev)
3304 prev->next = next;
3305 else
3306 buf->overlays_before = next;
3308 /* Search thru overlays_after for where to put it. */
3309 other_prev = NULL;
3310 for (other = buf->overlays_after; other;
3311 other_prev = other, other = other->next)
3313 Lisp_Object otherbeg, otheroverlay;
3315 XSETMISC (otheroverlay, other);
3316 eassert (OVERLAY_VALID (otheroverlay));
3318 otherbeg = OVERLAY_START (otheroverlay);
3319 if (OVERLAY_POSITION (otherbeg) >= where)
3320 break;
3323 /* Add TAIL to overlays_after before OTHER. */
3324 tail->next = other;
3325 if (other_prev)
3326 other_prev->next = tail;
3327 else
3328 buf->overlays_after = tail;
3329 tail = prev;
3331 else
3332 /* We've reached the things that should stay in overlays_before.
3333 All the rest of overlays_before must end even earlier,
3334 so stop now. */
3335 break;
3338 /* See if anything in overlays_after should be in overlays_before. */
3339 prev = NULL;
3340 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3342 next = tail->next;
3343 XSETMISC (overlay, tail);
3345 /* If the overlay is not valid, get rid of it. */
3346 if (!OVERLAY_VALID (overlay))
3347 #if 1
3348 abort ();
3349 #else
3351 /* Splice the cons cell TAIL out of overlays_after. */
3352 if (!NILP (prev))
3353 XCDR (prev) = next;
3354 else
3355 buf->overlays_after = next;
3356 tail = prev;
3357 continue;
3359 #endif
3361 beg = OVERLAY_START (overlay);
3362 end = OVERLAY_END (overlay);
3364 /* Stop looking, when we know that nothing further
3365 can possibly end before POS. */
3366 if (OVERLAY_POSITION (beg) > pos)
3367 break;
3369 if (OVERLAY_POSITION (end) <= pos)
3371 /* OVERLAY needs to be moved. */
3372 EMACS_INT where = OVERLAY_POSITION (end);
3373 struct Lisp_Overlay *other, *other_prev;
3375 /* Splice the cons cell TAIL out of overlays_after. */
3376 if (prev)
3377 prev->next = next;
3378 else
3379 buf->overlays_after = next;
3381 /* Search thru overlays_before for where to put it. */
3382 other_prev = NULL;
3383 for (other = buf->overlays_before; other;
3384 other_prev = other, other = other->next)
3386 Lisp_Object otherend, otheroverlay;
3388 XSETMISC (otheroverlay, other);
3389 eassert (OVERLAY_VALID (otheroverlay));
3391 otherend = OVERLAY_END (otheroverlay);
3392 if (OVERLAY_POSITION (otherend) <= where)
3393 break;
3396 /* Add TAIL to overlays_before before OTHER. */
3397 tail->next = other;
3398 if (other_prev)
3399 other_prev->next = tail;
3400 else
3401 buf->overlays_before = tail;
3402 tail = prev;
3406 buf->overlay_center = pos;
3409 void
3410 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3412 /* After an insertion, the lists are still sorted properly,
3413 but we may need to update the value of the overlay center. */
3414 if (current_buffer->overlay_center >= pos)
3415 current_buffer->overlay_center += length;
3418 void
3419 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3421 if (current_buffer->overlay_center < pos)
3422 /* The deletion was to our right. No change needed; the before- and
3423 after-lists are still consistent. */
3425 else if (current_buffer->overlay_center > pos + length)
3426 /* The deletion was to our left. We need to adjust the center value
3427 to account for the change in position, but the lists are consistent
3428 given the new value. */
3429 current_buffer->overlay_center -= length;
3430 else
3431 /* We're right in the middle. There might be things on the after-list
3432 that now belong on the before-list. Recentering will move them,
3433 and also update the center point. */
3434 recenter_overlay_lists (current_buffer, pos);
3437 /* Fix up overlays that were garbled as a result of permuting markers
3438 in the range START through END. Any overlay with at least one
3439 endpoint in this range will need to be unlinked from the overlay
3440 list and reinserted in its proper place.
3441 Such an overlay might even have negative size at this point.
3442 If so, we'll make the overlay empty. */
3443 void
3444 fix_start_end_in_overlays (register EMACS_INT start, register EMACS_INT end)
3446 Lisp_Object overlay;
3447 struct Lisp_Overlay *before_list, *after_list;
3448 /* These are either nil, indicating that before_list or after_list
3449 should be assigned, or the cons cell the cdr of which should be
3450 assigned. */
3451 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3452 /* 'Parent', likewise, indicates a cons cell or
3453 current_buffer->overlays_before or overlays_after, depending
3454 which loop we're in. */
3455 struct Lisp_Overlay *tail, *parent;
3456 EMACS_INT startpos, endpos;
3458 /* This algorithm shifts links around instead of consing and GCing.
3459 The loop invariant is that before_list (resp. after_list) is a
3460 well-formed list except that its last element, the CDR of beforep
3461 (resp. afterp) if beforep (afterp) isn't nil or before_list
3462 (after_list) if it is, is still uninitialized. So it's not a bug
3463 that before_list isn't initialized, although it may look
3464 strange. */
3465 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3467 XSETMISC (overlay, tail);
3469 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3470 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3472 /* If the overlay is backwards, make it empty. */
3473 if (endpos < startpos)
3475 startpos = endpos;
3476 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3477 Qnil);
3480 if (endpos < start)
3481 break;
3483 if (endpos < end
3484 || (startpos >= start && startpos < end))
3486 /* Add it to the end of the wrong list. Later on,
3487 recenter_overlay_lists will move it to the right place. */
3488 if (endpos < current_buffer->overlay_center)
3490 if (!afterp)
3491 after_list = tail;
3492 else
3493 afterp->next = tail;
3494 afterp = tail;
3496 else
3498 if (!beforep)
3499 before_list = tail;
3500 else
3501 beforep->next = tail;
3502 beforep = tail;
3504 if (!parent)
3505 current_buffer->overlays_before = tail->next;
3506 else
3507 parent->next = tail->next;
3508 tail = tail->next;
3510 else
3511 parent = tail, tail = parent->next;
3513 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3515 XSETMISC (overlay, tail);
3517 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3518 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3520 /* If the overlay is backwards, make it empty. */
3521 if (endpos < startpos)
3523 startpos = endpos;
3524 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3525 Qnil);
3528 if (startpos >= end)
3529 break;
3531 if (startpos >= start
3532 || (endpos >= start && endpos < end))
3534 if (endpos < current_buffer->overlay_center)
3536 if (!afterp)
3537 after_list = tail;
3538 else
3539 afterp->next = tail;
3540 afterp = tail;
3542 else
3544 if (!beforep)
3545 before_list = tail;
3546 else
3547 beforep->next = tail;
3548 beforep = tail;
3550 if (!parent)
3551 current_buffer->overlays_after = tail->next;
3552 else
3553 parent->next = tail->next;
3554 tail = tail->next;
3556 else
3557 parent = tail, tail = parent->next;
3560 /* Splice the constructed (wrong) lists into the buffer's lists,
3561 and let the recenter function make it sane again. */
3562 if (beforep)
3564 beforep->next = current_buffer->overlays_before;
3565 current_buffer->overlays_before = before_list;
3567 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3569 if (afterp)
3571 afterp->next = current_buffer->overlays_after;
3572 current_buffer->overlays_after = after_list;
3574 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3577 /* We have two types of overlay: the one whose ending marker is
3578 after-insertion-marker (this is the usual case) and the one whose
3579 ending marker is before-insertion-marker. When `overlays_before'
3580 contains overlays of the latter type and the former type in this
3581 order and both overlays end at inserting position, inserting a text
3582 increases only the ending marker of the latter type, which results
3583 in incorrect ordering of `overlays_before'.
3585 This function fixes ordering of overlays in the slot
3586 `overlays_before' of the buffer *BP. Before the insertion, `point'
3587 was at PREV, and now is at POS. */
3589 void
3590 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3592 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3593 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3594 Lisp_Object tem;
3595 EMACS_INT end;
3597 /* After the insertion, the several overlays may be in incorrect
3598 order. The possibility is that, in the list `overlays_before',
3599 an overlay which ends at POS appears after an overlay which ends
3600 at PREV. Since POS is greater than PREV, we must fix the
3601 ordering of these overlays, by moving overlays ends at POS before
3602 the overlays ends at PREV. */
3604 /* At first, find a place where disordered overlays should be linked
3605 in. It is where an overlay which end before POS exists. (i.e. an
3606 overlay whose ending marker is after-insertion-marker if disorder
3607 exists). */
3608 while (tail
3609 && (XSETMISC (tem, tail),
3610 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3612 parent = tail;
3613 tail = tail->next;
3616 /* If we don't find such an overlay,
3617 or the found one ends before PREV,
3618 or the found one is the last one in the list,
3619 we don't have to fix anything. */
3620 if (!tail || end < prev || !tail->next)
3621 return;
3623 right_pair = parent;
3624 parent = tail;
3625 tail = tail->next;
3627 /* Now, end position of overlays in the list TAIL should be before
3628 or equal to PREV. In the loop, an overlay which ends at POS is
3629 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3630 we found an overlay which ends before PREV, the remaining
3631 overlays are in correct order. */
3632 while (tail)
3634 XSETMISC (tem, tail);
3635 end = OVERLAY_POSITION (OVERLAY_END (tem));
3637 if (end == pos)
3638 { /* This overlay is disordered. */
3639 struct Lisp_Overlay *found = tail;
3641 /* Unlink the found overlay. */
3642 tail = found->next;
3643 parent->next = tail;
3644 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3645 and link it into the right place. */
3646 if (!right_pair)
3648 found->next = bp->overlays_before;
3649 bp->overlays_before = found;
3651 else
3653 found->next = right_pair->next;
3654 right_pair->next = found;
3657 else if (end == prev)
3659 parent = tail;
3660 tail = tail->next;
3662 else /* No more disordered overlay. */
3663 break;
3667 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3668 doc: /* Return t if OBJECT is an overlay. */)
3669 (Lisp_Object object)
3671 return (OVERLAYP (object) ? Qt : Qnil);
3674 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3675 doc: /* Create a new overlay with range BEG to END in BUFFER.
3676 If omitted, BUFFER defaults to the current buffer.
3677 BEG and END may be integers or markers.
3678 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3679 for the front of the overlay advance when text is inserted there
3680 \(which means the text *is not* included in the overlay).
3681 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3682 for the rear of the overlay advance when text is inserted there
3683 \(which means the text *is* included in the overlay). */)
3684 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3686 Lisp_Object overlay;
3687 struct buffer *b;
3689 if (NILP (buffer))
3690 XSETBUFFER (buffer, current_buffer);
3691 else
3692 CHECK_BUFFER (buffer);
3693 if (MARKERP (beg)
3694 && ! EQ (Fmarker_buffer (beg), buffer))
3695 error ("Marker points into wrong buffer");
3696 if (MARKERP (end)
3697 && ! EQ (Fmarker_buffer (end), buffer))
3698 error ("Marker points into wrong buffer");
3700 CHECK_NUMBER_COERCE_MARKER (beg);
3701 CHECK_NUMBER_COERCE_MARKER (end);
3703 if (XINT (beg) > XINT (end))
3705 Lisp_Object temp;
3706 temp = beg; beg = end; end = temp;
3709 b = XBUFFER (buffer);
3711 beg = Fset_marker (Fmake_marker (), beg, buffer);
3712 end = Fset_marker (Fmake_marker (), end, buffer);
3714 if (!NILP (front_advance))
3715 XMARKER (beg)->insertion_type = 1;
3716 if (!NILP (rear_advance))
3717 XMARKER (end)->insertion_type = 1;
3719 overlay = allocate_misc ();
3720 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3721 XOVERLAY (overlay)->start = beg;
3722 XOVERLAY (overlay)->end = end;
3723 XOVERLAY (overlay)->plist = Qnil;
3724 XOVERLAY (overlay)->next = NULL;
3726 /* Put the new overlay on the wrong list. */
3727 end = OVERLAY_END (overlay);
3728 if (OVERLAY_POSITION (end) < b->overlay_center)
3730 if (b->overlays_after)
3731 XOVERLAY (overlay)->next = b->overlays_after;
3732 b->overlays_after = XOVERLAY (overlay);
3734 else
3736 if (b->overlays_before)
3737 XOVERLAY (overlay)->next = b->overlays_before;
3738 b->overlays_before = XOVERLAY (overlay);
3741 /* This puts it in the right list, and in the right order. */
3742 recenter_overlay_lists (b, b->overlay_center);
3744 /* We don't need to redisplay the region covered by the overlay, because
3745 the overlay has no properties at the moment. */
3747 return overlay;
3750 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3752 static void
3753 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3755 if (start > end)
3757 EMACS_INT temp = start;
3758 start = end;
3759 end = temp;
3762 BUF_COMPUTE_UNCHANGED (buf, start, end);
3764 /* If this is a buffer not in the selected window,
3765 we must do other windows. */
3766 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3767 windows_or_buffers_changed = 1;
3768 /* If multiple windows show this buffer, we must do other windows. */
3769 else if (buffer_shared > 1)
3770 windows_or_buffers_changed = 1;
3771 /* If we modify an overlay at the end of the buffer, we cannot
3772 be sure that window end is still valid. */
3773 else if (end >= ZV && start <= ZV)
3774 windows_or_buffers_changed = 1;
3776 ++BUF_OVERLAY_MODIFF (buf);
3780 Lisp_Object Fdelete_overlay (Lisp_Object overlay);
3782 static struct Lisp_Overlay *
3783 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3785 struct Lisp_Overlay *tmp, *prev;
3786 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3787 if (tmp == overlay)
3789 if (prev)
3790 prev->next = tmp->next;
3791 else
3792 list = tmp->next;
3793 overlay->next = NULL;
3794 break;
3796 return list;
3799 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3800 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3801 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3802 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3803 buffer. */)
3804 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3806 struct buffer *b, *ob;
3807 Lisp_Object obuffer;
3808 int count = SPECPDL_INDEX ();
3810 CHECK_OVERLAY (overlay);
3811 if (NILP (buffer))
3812 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3813 if (NILP (buffer))
3814 XSETBUFFER (buffer, current_buffer);
3815 CHECK_BUFFER (buffer);
3817 if (MARKERP (beg)
3818 && ! EQ (Fmarker_buffer (beg), buffer))
3819 error ("Marker points into wrong buffer");
3820 if (MARKERP (end)
3821 && ! EQ (Fmarker_buffer (end), buffer))
3822 error ("Marker points into wrong buffer");
3824 CHECK_NUMBER_COERCE_MARKER (beg);
3825 CHECK_NUMBER_COERCE_MARKER (end);
3827 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3828 return Fdelete_overlay (overlay);
3830 if (XINT (beg) > XINT (end))
3832 Lisp_Object temp;
3833 temp = beg; beg = end; end = temp;
3836 specbind (Qinhibit_quit, Qt);
3838 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3839 b = XBUFFER (buffer);
3840 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3842 /* If the overlay has changed buffers, do a thorough redisplay. */
3843 if (!EQ (buffer, obuffer))
3845 /* Redisplay where the overlay was. */
3846 if (!NILP (obuffer))
3848 EMACS_INT o_beg;
3849 EMACS_INT o_end;
3851 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3852 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3854 modify_overlay (ob, o_beg, o_end);
3857 /* Redisplay where the overlay is going to be. */
3858 modify_overlay (b, XINT (beg), XINT (end));
3860 else
3861 /* Redisplay the area the overlay has just left, or just enclosed. */
3863 EMACS_INT o_beg, o_end;
3865 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3866 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3868 if (o_beg == XINT (beg))
3869 modify_overlay (b, o_end, XINT (end));
3870 else if (o_end == XINT (end))
3871 modify_overlay (b, o_beg, XINT (beg));
3872 else
3874 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3875 if (XINT (end) > o_end) o_end = XINT (end);
3876 modify_overlay (b, o_beg, o_end);
3880 if (!NILP (obuffer))
3882 ob->overlays_before
3883 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3884 ob->overlays_after
3885 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3886 eassert (XOVERLAY (overlay)->next == NULL);
3889 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3890 Fset_marker (OVERLAY_END (overlay), end, buffer);
3892 /* Put the overlay on the wrong list. */
3893 end = OVERLAY_END (overlay);
3894 if (OVERLAY_POSITION (end) < b->overlay_center)
3896 XOVERLAY (overlay)->next = b->overlays_after;
3897 b->overlays_after = XOVERLAY (overlay);
3899 else
3901 XOVERLAY (overlay)->next = b->overlays_before;
3902 b->overlays_before = XOVERLAY (overlay);
3905 /* This puts it in the right list, and in the right order. */
3906 recenter_overlay_lists (b, b->overlay_center);
3908 return unbind_to (count, overlay);
3911 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3912 doc: /* Delete the overlay OVERLAY from its buffer. */)
3913 (Lisp_Object overlay)
3915 Lisp_Object buffer;
3916 struct buffer *b;
3917 int count = SPECPDL_INDEX ();
3919 CHECK_OVERLAY (overlay);
3921 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3922 if (NILP (buffer))
3923 return Qnil;
3925 b = XBUFFER (buffer);
3926 specbind (Qinhibit_quit, Qt);
3928 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3929 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3930 eassert (XOVERLAY (overlay)->next == NULL);
3931 modify_overlay (b,
3932 marker_position (OVERLAY_START (overlay)),
3933 marker_position (OVERLAY_END (overlay)));
3934 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3935 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3937 /* When deleting an overlay with before or after strings, turn off
3938 display optimizations for the affected buffer, on the basis that
3939 these strings may contain newlines. This is easier to do than to
3940 check for that situation during redisplay. */
3941 if (!windows_or_buffers_changed
3942 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3943 || !NILP (Foverlay_get (overlay, Qafter_string))))
3944 b->prevent_redisplay_optimizations_p = 1;
3946 return unbind_to (count, Qnil);
3949 /* Overlay dissection functions. */
3951 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3952 doc: /* Return the position at which OVERLAY starts. */)
3953 (Lisp_Object overlay)
3955 CHECK_OVERLAY (overlay);
3957 return (Fmarker_position (OVERLAY_START (overlay)));
3960 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3961 doc: /* Return the position at which OVERLAY ends. */)
3962 (Lisp_Object overlay)
3964 CHECK_OVERLAY (overlay);
3966 return (Fmarker_position (OVERLAY_END (overlay)));
3969 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3970 doc: /* Return the buffer OVERLAY belongs to.
3971 Return nil if OVERLAY has been deleted. */)
3972 (Lisp_Object overlay)
3974 CHECK_OVERLAY (overlay);
3976 return Fmarker_buffer (OVERLAY_START (overlay));
3979 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3980 doc: /* Return a list of the properties on OVERLAY.
3981 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3982 OVERLAY. */)
3983 (Lisp_Object overlay)
3985 CHECK_OVERLAY (overlay);
3987 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3991 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3992 doc: /* Return a list of the overlays that contain the character at POS. */)
3993 (Lisp_Object pos)
3995 int noverlays;
3996 Lisp_Object *overlay_vec;
3997 int len;
3998 Lisp_Object result;
4000 CHECK_NUMBER_COERCE_MARKER (pos);
4002 len = 10;
4003 /* We can't use alloca here because overlays_at can call xrealloc. */
4004 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4006 /* Put all the overlays we want in a vector in overlay_vec.
4007 Store the length in len. */
4008 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4009 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4011 /* Make a list of them all. */
4012 result = Flist (noverlays, overlay_vec);
4014 xfree (overlay_vec);
4015 return result;
4018 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4019 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4020 Overlap means that at least one character is contained within the overlay
4021 and also contained within the specified region.
4022 Empty overlays are included in the result if they are located at BEG,
4023 between BEG and END, or at END provided END denotes the position at the
4024 end of the buffer. */)
4025 (Lisp_Object beg, Lisp_Object end)
4027 int noverlays;
4028 Lisp_Object *overlay_vec;
4029 int len;
4030 Lisp_Object result;
4032 CHECK_NUMBER_COERCE_MARKER (beg);
4033 CHECK_NUMBER_COERCE_MARKER (end);
4035 len = 10;
4036 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4038 /* Put all the overlays we want in a vector in overlay_vec.
4039 Store the length in len. */
4040 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4041 NULL, NULL);
4043 /* Make a list of them all. */
4044 result = Flist (noverlays, overlay_vec);
4046 xfree (overlay_vec);
4047 return result;
4050 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4051 1, 1, 0,
4052 doc: /* Return the next position after POS where an overlay starts or ends.
4053 If there are no overlay boundaries from POS to (point-max),
4054 the value is (point-max). */)
4055 (Lisp_Object pos)
4057 int noverlays;
4058 EMACS_INT endpos;
4059 Lisp_Object *overlay_vec;
4060 int len;
4061 int i;
4063 CHECK_NUMBER_COERCE_MARKER (pos);
4065 len = 10;
4066 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4068 /* Put all the overlays we want in a vector in overlay_vec.
4069 Store the length in len.
4070 endpos gets the position where the next overlay starts. */
4071 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4072 &endpos, (EMACS_INT *) 0, 1);
4074 /* If any of these overlays ends before endpos,
4075 use its ending point instead. */
4076 for (i = 0; i < noverlays; i++)
4078 Lisp_Object oend;
4079 EMACS_INT oendpos;
4081 oend = OVERLAY_END (overlay_vec[i]);
4082 oendpos = OVERLAY_POSITION (oend);
4083 if (oendpos < endpos)
4084 endpos = oendpos;
4087 xfree (overlay_vec);
4088 return make_number (endpos);
4091 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4092 Sprevious_overlay_change, 1, 1, 0,
4093 doc: /* Return the previous position before POS where an overlay starts or ends.
4094 If there are no overlay boundaries from (point-min) to POS,
4095 the value is (point-min). */)
4096 (Lisp_Object pos)
4098 int noverlays;
4099 EMACS_INT prevpos;
4100 Lisp_Object *overlay_vec;
4101 int len;
4103 CHECK_NUMBER_COERCE_MARKER (pos);
4105 /* At beginning of buffer, we know the answer;
4106 avoid bug subtracting 1 below. */
4107 if (XINT (pos) == BEGV)
4108 return pos;
4110 len = 10;
4111 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4113 /* Put all the overlays we want in a vector in overlay_vec.
4114 Store the length in len.
4115 prevpos gets the position of the previous change. */
4116 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4117 (EMACS_INT *) 0, &prevpos, 1);
4119 xfree (overlay_vec);
4120 return make_number (prevpos);
4123 /* These functions are for debugging overlays. */
4125 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4126 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4127 The car has all the overlays before the overlay center;
4128 the cdr has all the overlays after the overlay center.
4129 Recentering overlays moves overlays between these lists.
4130 The lists you get are copies, so that changing them has no effect.
4131 However, the overlays you get are the real objects that the buffer uses. */)
4132 (void)
4134 struct Lisp_Overlay *ol;
4135 Lisp_Object before = Qnil, after = Qnil, tmp;
4136 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4138 XSETMISC (tmp, ol);
4139 before = Fcons (tmp, before);
4141 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4143 XSETMISC (tmp, ol);
4144 after = Fcons (tmp, after);
4146 return Fcons (Fnreverse (before), Fnreverse (after));
4149 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4150 doc: /* Recenter the overlays of the current buffer around position POS.
4151 That makes overlay lookup faster for positions near POS (but perhaps slower
4152 for positions far away from POS). */)
4153 (Lisp_Object pos)
4155 CHECK_NUMBER_COERCE_MARKER (pos);
4157 recenter_overlay_lists (current_buffer, XINT (pos));
4158 return Qnil;
4161 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4162 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4163 (Lisp_Object overlay, Lisp_Object prop)
4165 CHECK_OVERLAY (overlay);
4166 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4169 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4170 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4171 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4173 Lisp_Object tail, buffer;
4174 int changed;
4176 CHECK_OVERLAY (overlay);
4178 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4180 for (tail = XOVERLAY (overlay)->plist;
4181 CONSP (tail) && CONSP (XCDR (tail));
4182 tail = XCDR (XCDR (tail)))
4183 if (EQ (XCAR (tail), prop))
4185 changed = !EQ (XCAR (XCDR (tail)), value);
4186 XSETCAR (XCDR (tail), value);
4187 goto found;
4189 /* It wasn't in the list, so add it to the front. */
4190 changed = !NILP (value);
4191 XOVERLAY (overlay)->plist
4192 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4193 found:
4194 if (! NILP (buffer))
4196 if (changed)
4197 modify_overlay (XBUFFER (buffer),
4198 marker_position (OVERLAY_START (overlay)),
4199 marker_position (OVERLAY_END (overlay)));
4200 if (EQ (prop, Qevaporate) && ! NILP (value)
4201 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4202 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4203 Fdelete_overlay (overlay);
4206 return value;
4209 /* Subroutine of report_overlay_modification. */
4211 /* Lisp vector holding overlay hook functions to call.
4212 Vector elements come in pairs.
4213 Each even-index element is a list of hook functions.
4214 The following odd-index element is the overlay they came from.
4216 Before the buffer change, we fill in this vector
4217 as we call overlay hook functions.
4218 After the buffer change, we get the functions to call from this vector.
4219 This way we always call the same functions before and after the change. */
4220 static Lisp_Object last_overlay_modification_hooks;
4222 /* Number of elements actually used in last_overlay_modification_hooks. */
4223 static int last_overlay_modification_hooks_used;
4225 /* Add one functionlist/overlay pair
4226 to the end of last_overlay_modification_hooks. */
4228 static void
4229 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4231 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4233 if (last_overlay_modification_hooks_used == oldsize)
4234 last_overlay_modification_hooks = larger_vector
4235 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4236 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4237 functionlist); last_overlay_modification_hooks_used++;
4238 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4239 overlay); last_overlay_modification_hooks_used++;
4242 /* Run the modification-hooks of overlays that include
4243 any part of the text in START to END.
4244 If this change is an insertion, also
4245 run the insert-before-hooks of overlay starting at END,
4246 and the insert-after-hooks of overlay ending at START.
4248 This is called both before and after the modification.
4249 AFTER is nonzero when we call after the modification.
4251 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4252 When AFTER is nonzero, they are the start position,
4253 the position after the inserted new text,
4254 and the length of deleted or replaced old text. */
4256 void
4257 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4258 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4260 Lisp_Object prop, overlay;
4261 struct Lisp_Overlay *tail;
4262 /* 1 if this change is an insertion. */
4263 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4264 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4266 overlay = Qnil;
4267 tail = NULL;
4269 /* We used to run the functions as soon as we found them and only register
4270 them in last_overlay_modification_hooks for the purpose of the `after'
4271 case. But running elisp code as we traverse the list of overlays is
4272 painful because the list can be modified by the elisp code so we had to
4273 copy at several places. We now simply do a read-only traversal that
4274 only collects the functions to run and we run them afterwards. It's
4275 simpler, especially since all the code was already there. -stef */
4277 if (!after)
4279 /* We are being called before a change.
4280 Scan the overlays to find the functions to call. */
4281 last_overlay_modification_hooks_used = 0;
4282 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4284 EMACS_INT startpos, endpos;
4285 Lisp_Object ostart, oend;
4287 XSETMISC (overlay, tail);
4289 ostart = OVERLAY_START (overlay);
4290 oend = OVERLAY_END (overlay);
4291 endpos = OVERLAY_POSITION (oend);
4292 if (XFASTINT (start) > endpos)
4293 break;
4294 startpos = OVERLAY_POSITION (ostart);
4295 if (insertion && (XFASTINT (start) == startpos
4296 || XFASTINT (end) == startpos))
4298 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4299 if (!NILP (prop))
4300 add_overlay_mod_hooklist (prop, overlay);
4302 if (insertion && (XFASTINT (start) == endpos
4303 || XFASTINT (end) == endpos))
4305 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4306 if (!NILP (prop))
4307 add_overlay_mod_hooklist (prop, overlay);
4309 /* Test for intersecting intervals. This does the right thing
4310 for both insertion and deletion. */
4311 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4313 prop = Foverlay_get (overlay, Qmodification_hooks);
4314 if (!NILP (prop))
4315 add_overlay_mod_hooklist (prop, overlay);
4319 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4321 EMACS_INT startpos, endpos;
4322 Lisp_Object ostart, oend;
4324 XSETMISC (overlay, tail);
4326 ostart = OVERLAY_START (overlay);
4327 oend = OVERLAY_END (overlay);
4328 startpos = OVERLAY_POSITION (ostart);
4329 endpos = OVERLAY_POSITION (oend);
4330 if (XFASTINT (end) < startpos)
4331 break;
4332 if (insertion && (XFASTINT (start) == startpos
4333 || XFASTINT (end) == startpos))
4335 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4336 if (!NILP (prop))
4337 add_overlay_mod_hooklist (prop, overlay);
4339 if (insertion && (XFASTINT (start) == endpos
4340 || XFASTINT (end) == endpos))
4342 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4343 if (!NILP (prop))
4344 add_overlay_mod_hooklist (prop, overlay);
4346 /* Test for intersecting intervals. This does the right thing
4347 for both insertion and deletion. */
4348 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4350 prop = Foverlay_get (overlay, Qmodification_hooks);
4351 if (!NILP (prop))
4352 add_overlay_mod_hooklist (prop, overlay);
4357 GCPRO4 (overlay, arg1, arg2, arg3);
4359 /* Call the functions recorded in last_overlay_modification_hooks.
4360 First copy the vector contents, in case some of these hooks
4361 do subsequent modification of the buffer. */
4362 int size = last_overlay_modification_hooks_used;
4363 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4364 int i;
4366 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4367 size * sizeof (Lisp_Object));
4368 gcpro1.var = copy;
4369 gcpro1.nvars = size;
4371 for (i = 0; i < size;)
4373 Lisp_Object prop, overlay;
4374 prop = copy[i++];
4375 overlay = copy[i++];
4376 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4379 UNGCPRO;
4382 static void
4383 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4384 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4386 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4388 GCPRO4 (list, arg1, arg2, arg3);
4390 while (CONSP (list))
4392 if (NILP (arg3))
4393 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4394 else
4395 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4396 list = XCDR (list);
4398 UNGCPRO;
4401 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4402 property is set. */
4403 void
4404 evaporate_overlays (EMACS_INT pos)
4406 Lisp_Object overlay, hit_list;
4407 struct Lisp_Overlay *tail;
4409 hit_list = Qnil;
4410 if (pos <= current_buffer->overlay_center)
4411 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4413 EMACS_INT endpos;
4414 XSETMISC (overlay, tail);
4415 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4416 if (endpos < pos)
4417 break;
4418 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4419 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4420 hit_list = Fcons (overlay, hit_list);
4422 else
4423 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4425 EMACS_INT startpos;
4426 XSETMISC (overlay, tail);
4427 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4428 if (startpos > pos)
4429 break;
4430 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4431 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4432 hit_list = Fcons (overlay, hit_list);
4434 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4435 Fdelete_overlay (XCAR (hit_list));
4438 /* Somebody has tried to store a value with an unacceptable type
4439 in the slot with offset OFFSET. */
4441 void
4442 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4444 Lisp_Object predicate;
4446 switch (type)
4448 case_Lisp_Int: predicate = Qintegerp; break;
4449 case Lisp_String: predicate = Qstringp; break;
4450 case Lisp_Symbol: predicate = Qsymbolp; break;
4451 default: abort ();
4454 wrong_type_argument (predicate, newval);
4458 /***********************************************************************
4459 Allocation with mmap
4460 ***********************************************************************/
4462 #ifdef USE_MMAP_FOR_BUFFERS
4464 #include <sys/types.h>
4465 #include <sys/mman.h>
4467 #ifndef MAP_ANON
4468 #ifdef MAP_ANONYMOUS
4469 #define MAP_ANON MAP_ANONYMOUS
4470 #else
4471 #define MAP_ANON 0
4472 #endif
4473 #endif
4475 #ifndef MAP_FAILED
4476 #define MAP_FAILED ((void *) -1)
4477 #endif
4479 #include <stdio.h>
4481 #if MAP_ANON == 0
4482 #include <fcntl.h>
4483 #endif
4485 #include "coding.h"
4488 /* Memory is allocated in regions which are mapped using mmap(2).
4489 The current implementation lets the system select mapped
4490 addresses; we're not using MAP_FIXED in general, except when
4491 trying to enlarge regions.
4493 Each mapped region starts with a mmap_region structure, the user
4494 area starts after that structure, aligned to MEM_ALIGN.
4496 +-----------------------+
4497 | struct mmap_info + |
4498 | padding |
4499 +-----------------------+
4500 | user data |
4503 +-----------------------+ */
4505 struct mmap_region
4507 /* User-specified size. */
4508 size_t nbytes_specified;
4510 /* Number of bytes mapped */
4511 size_t nbytes_mapped;
4513 /* Pointer to the location holding the address of the memory
4514 allocated with the mmap'd block. The variable actually points
4515 after this structure. */
4516 POINTER_TYPE **var;
4518 /* Next and previous in list of all mmap'd regions. */
4519 struct mmap_region *next, *prev;
4522 /* Doubly-linked list of mmap'd regions. */
4524 static struct mmap_region *mmap_regions;
4526 /* File descriptor for mmap. If we don't have anonymous mapping,
4527 /dev/zero will be opened on it. */
4529 static int mmap_fd;
4531 /* Temporary storage for mmap_set_vars, see there. */
4533 static struct mmap_region *mmap_regions_1;
4534 static int mmap_fd_1;
4536 /* Page size on this system. */
4538 static int mmap_page_size;
4540 /* 1 means mmap has been intialized. */
4542 static int mmap_initialized_p;
4544 /* Value is X rounded up to the next multiple of N. */
4546 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4548 /* Size of mmap_region structure plus padding. */
4550 #define MMAP_REGION_STRUCT_SIZE \
4551 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4553 /* Given a pointer P to the start of the user-visible part of a mapped
4554 region, return a pointer to the start of the region. */
4556 #define MMAP_REGION(P) \
4557 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4559 /* Given a pointer P to the start of a mapped region, return a pointer
4560 to the start of the user-visible part of the region. */
4562 #define MMAP_USER_AREA(P) \
4563 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4565 #define MEM_ALIGN sizeof (double)
4567 /* Predicate returning true if part of the address range [START .. END]
4568 is currently mapped. Used to prevent overwriting an existing
4569 memory mapping.
4571 Default is to conservativly assume the address range is occupied by
4572 something else. This can be overridden by system configuration
4573 files if system-specific means to determine this exists. */
4575 #ifndef MMAP_ALLOCATED_P
4576 #define MMAP_ALLOCATED_P(start, end) 1
4577 #endif
4579 /* Function prototypes. */
4581 static int mmap_free_1 (struct mmap_region *);
4582 static int mmap_enlarge (struct mmap_region *, int);
4583 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4584 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4585 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4586 static void mmap_free (POINTER_TYPE **ptr);
4587 static void mmap_init (void);
4590 /* Return a region overlapping address range START...END, or null if
4591 none. END is not including, i.e. the last byte in the range
4592 is at END - 1. */
4594 static struct mmap_region *
4595 mmap_find (start, end)
4596 POINTER_TYPE *start, *end;
4598 struct mmap_region *r;
4599 char *s = (char *) start, *e = (char *) end;
4601 for (r = mmap_regions; r; r = r->next)
4603 char *rstart = (char *) r;
4604 char *rend = rstart + r->nbytes_mapped;
4606 if (/* First byte of range, i.e. START, in this region? */
4607 (s >= rstart && s < rend)
4608 /* Last byte of range, i.e. END - 1, in this region? */
4609 || (e > rstart && e <= rend)
4610 /* First byte of this region in the range? */
4611 || (rstart >= s && rstart < e)
4612 /* Last byte of this region in the range? */
4613 || (rend > s && rend <= e))
4614 break;
4617 return r;
4621 /* Unmap a region. P is a pointer to the start of the user-araa of
4622 the region. Value is non-zero if successful. */
4624 static int
4625 mmap_free_1 (r)
4626 struct mmap_region *r;
4628 if (r->next)
4629 r->next->prev = r->prev;
4630 if (r->prev)
4631 r->prev->next = r->next;
4632 else
4633 mmap_regions = r->next;
4635 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4637 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4638 return 0;
4641 return 1;
4645 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4646 Value is non-zero if successful. */
4648 static int
4649 mmap_enlarge (r, npages)
4650 struct mmap_region *r;
4651 int npages;
4653 char *region_end = (char *) r + r->nbytes_mapped;
4654 size_t nbytes;
4655 int success = 0;
4657 if (npages < 0)
4659 /* Unmap pages at the end of the region. */
4660 nbytes = - npages * mmap_page_size;
4661 if (munmap (region_end - nbytes, nbytes) == -1)
4662 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4663 else
4665 r->nbytes_mapped -= nbytes;
4666 success = 1;
4669 else if (npages > 0)
4671 nbytes = npages * mmap_page_size;
4673 /* Try to map additional pages at the end of the region. We
4674 cannot do this if the address range is already occupied by
4675 something else because mmap deletes any previous mapping.
4676 I'm not sure this is worth doing, let's see. */
4677 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4679 POINTER_TYPE *p;
4681 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4682 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4683 if (p == MAP_FAILED)
4684 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4685 else if (p != (POINTER_TYPE *) region_end)
4687 /* Kernels are free to choose a different address. In
4688 that case, unmap what we've mapped above; we have
4689 no use for it. */
4690 if (munmap (p, nbytes) == -1)
4691 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4693 else
4695 r->nbytes_mapped += nbytes;
4696 success = 1;
4701 return success;
4705 /* Set or reset variables holding references to mapped regions. If
4706 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4707 non-zero, set all variables to the start of the user-areas
4708 of mapped regions.
4710 This function is called from Fdump_emacs to ensure that the dumped
4711 Emacs doesn't contain references to memory that won't be mapped
4712 when Emacs starts. */
4714 void
4715 mmap_set_vars (restore_p)
4716 int restore_p;
4718 struct mmap_region *r;
4720 if (restore_p)
4722 mmap_regions = mmap_regions_1;
4723 mmap_fd = mmap_fd_1;
4724 for (r = mmap_regions; r; r = r->next)
4725 *r->var = MMAP_USER_AREA (r);
4727 else
4729 for (r = mmap_regions; r; r = r->next)
4730 *r->var = NULL;
4731 mmap_regions_1 = mmap_regions;
4732 mmap_regions = NULL;
4733 mmap_fd_1 = mmap_fd;
4734 mmap_fd = -1;
4739 /* Allocate a block of storage large enough to hold NBYTES bytes of
4740 data. A pointer to the data is returned in *VAR. VAR is thus the
4741 address of some variable which will use the data area.
4743 The allocation of 0 bytes is valid.
4745 If we can't allocate the necessary memory, set *VAR to null, and
4746 return null. */
4748 static POINTER_TYPE *
4749 mmap_alloc (var, nbytes)
4750 POINTER_TYPE **var;
4751 size_t nbytes;
4753 void *p;
4754 size_t map;
4756 mmap_init ();
4758 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4759 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4760 mmap_fd, 0);
4762 if (p == MAP_FAILED)
4764 if (errno != ENOMEM)
4765 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4766 p = NULL;
4768 else
4770 struct mmap_region *r = (struct mmap_region *) p;
4772 r->nbytes_specified = nbytes;
4773 r->nbytes_mapped = map;
4774 r->var = var;
4775 r->prev = NULL;
4776 r->next = mmap_regions;
4777 if (r->next)
4778 r->next->prev = r;
4779 mmap_regions = r;
4781 p = MMAP_USER_AREA (p);
4784 return *var = p;
4788 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4789 resize it to size NBYTES. Change *VAR to reflect the new block,
4790 and return this value. If more memory cannot be allocated, then
4791 leave *VAR unchanged, and return null. */
4793 static POINTER_TYPE *
4794 mmap_realloc (var, nbytes)
4795 POINTER_TYPE **var;
4796 size_t nbytes;
4798 POINTER_TYPE *result;
4800 mmap_init ();
4802 if (*var == NULL)
4803 result = mmap_alloc (var, nbytes);
4804 else if (nbytes == 0)
4806 mmap_free (var);
4807 result = mmap_alloc (var, nbytes);
4809 else
4811 struct mmap_region *r = MMAP_REGION (*var);
4812 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4814 if (room < nbytes)
4816 /* Must enlarge. */
4817 POINTER_TYPE *old_ptr = *var;
4819 /* Try to map additional pages at the end of the region.
4820 If that fails, allocate a new region, copy data
4821 from the old region, then free it. */
4822 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4823 / mmap_page_size)))
4825 r->nbytes_specified = nbytes;
4826 *var = result = old_ptr;
4828 else if (mmap_alloc (var, nbytes))
4830 memcpy (*var, old_ptr, r->nbytes_specified);
4831 mmap_free_1 (MMAP_REGION (old_ptr));
4832 result = *var;
4833 r = MMAP_REGION (result);
4834 r->nbytes_specified = nbytes;
4836 else
4838 *var = old_ptr;
4839 result = NULL;
4842 else if (room - nbytes >= mmap_page_size)
4844 /* Shrinking by at least a page. Let's give some
4845 memory back to the system.
4847 The extra parens are to make the division happens first,
4848 on positive values, so we know it will round towards
4849 zero. */
4850 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4851 result = *var;
4852 r->nbytes_specified = nbytes;
4854 else
4856 /* Leave it alone. */
4857 result = *var;
4858 r->nbytes_specified = nbytes;
4862 return result;
4866 /* Free a block of relocatable storage whose data is pointed to by
4867 PTR. Store 0 in *PTR to show there's no block allocated. */
4869 static void
4870 mmap_free (var)
4871 POINTER_TYPE **var;
4873 mmap_init ();
4875 if (*var)
4877 mmap_free_1 (MMAP_REGION (*var));
4878 *var = NULL;
4883 /* Perform necessary intializations for the use of mmap. */
4885 static void
4886 mmap_init ()
4888 #if MAP_ANON == 0
4889 /* The value of mmap_fd is initially 0 in temacs, and -1
4890 in a dumped Emacs. */
4891 if (mmap_fd <= 0)
4893 /* No anonymous mmap -- we need the file descriptor. */
4894 mmap_fd = open ("/dev/zero", O_RDONLY);
4895 if (mmap_fd == -1)
4896 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4898 #endif /* MAP_ANON == 0 */
4900 if (mmap_initialized_p)
4901 return;
4902 mmap_initialized_p = 1;
4904 #if MAP_ANON != 0
4905 mmap_fd = -1;
4906 #endif
4908 mmap_page_size = getpagesize ();
4911 #endif /* USE_MMAP_FOR_BUFFERS */
4915 /***********************************************************************
4916 Buffer-text Allocation
4917 ***********************************************************************/
4919 #ifdef REL_ALLOC
4920 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4921 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4922 extern void r_alloc_free (POINTER_TYPE **ptr);
4923 #endif /* REL_ALLOC */
4926 /* Allocate NBYTES bytes for buffer B's text buffer. */
4928 static void
4929 alloc_buffer_text (struct buffer *b, size_t nbytes)
4931 POINTER_TYPE *p;
4933 BLOCK_INPUT;
4934 #if defined USE_MMAP_FOR_BUFFERS
4935 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4936 #elif defined REL_ALLOC
4937 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4938 #else
4939 p = xmalloc (nbytes);
4940 #endif
4942 if (p == NULL)
4944 UNBLOCK_INPUT;
4945 memory_full ();
4948 b->text->beg = (unsigned char *) p;
4949 UNBLOCK_INPUT;
4952 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4953 shrink it. */
4955 void
4956 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4958 POINTER_TYPE *p;
4959 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4960 + delta);
4961 BLOCK_INPUT;
4962 #if defined USE_MMAP_FOR_BUFFERS
4963 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4964 #elif defined REL_ALLOC
4965 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4966 #else
4967 p = xrealloc (b->text->beg, nbytes);
4968 #endif
4970 if (p == NULL)
4972 UNBLOCK_INPUT;
4973 memory_full ();
4976 BUF_BEG_ADDR (b) = (unsigned char *) p;
4977 UNBLOCK_INPUT;
4981 /* Free buffer B's text buffer. */
4983 static void
4984 free_buffer_text (struct buffer *b)
4986 BLOCK_INPUT;
4988 #if defined USE_MMAP_FOR_BUFFERS
4989 mmap_free ((POINTER_TYPE **) &b->text->beg);
4990 #elif defined REL_ALLOC
4991 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4992 #else
4993 xfree (b->text->beg);
4994 #endif
4996 BUF_BEG_ADDR (b) = NULL;
4997 UNBLOCK_INPUT;
5002 /***********************************************************************
5003 Initialization
5004 ***********************************************************************/
5006 void
5007 init_buffer_once (void)
5009 int idx;
5011 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
5013 /* Make sure all markable slots in buffer_defaults
5014 are initialized reasonably, so mark_buffer won't choke. */
5015 reset_buffer (&buffer_defaults);
5016 eassert (EQ (buffer_defaults.name, make_number (0)));
5017 reset_buffer_local_variables (&buffer_defaults, 1);
5018 eassert (EQ (buffer_local_symbols.name, make_number (0)));
5019 reset_buffer (&buffer_local_symbols);
5020 reset_buffer_local_variables (&buffer_local_symbols, 1);
5021 /* Prevent GC from getting confused. */
5022 buffer_defaults.text = &buffer_defaults.own_text;
5023 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5024 BUF_INTERVALS (&buffer_defaults) = 0;
5025 BUF_INTERVALS (&buffer_local_symbols) = 0;
5026 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5027 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5028 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5029 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5031 /* Set up the default values of various buffer slots. */
5032 /* Must do these before making the first buffer! */
5034 /* real setup is done in bindings.el */
5035 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5036 buffer_defaults.header_line_format = Qnil;
5037 buffer_defaults.abbrev_mode = Qnil;
5038 buffer_defaults.overwrite_mode = Qnil;
5039 buffer_defaults.case_fold_search = Qt;
5040 buffer_defaults.auto_fill_function = Qnil;
5041 buffer_defaults.selective_display = Qnil;
5042 #ifndef old
5043 buffer_defaults.selective_display_ellipses = Qt;
5044 #endif
5045 buffer_defaults.abbrev_table = Qnil;
5046 buffer_defaults.display_table = Qnil;
5047 buffer_defaults.undo_list = Qnil;
5048 buffer_defaults.mark_active = Qnil;
5049 buffer_defaults.file_format = Qnil;
5050 buffer_defaults.auto_save_file_format = Qt;
5051 buffer_defaults.overlays_before = NULL;
5052 buffer_defaults.overlays_after = NULL;
5053 buffer_defaults.overlay_center = BEG;
5055 XSETFASTINT (buffer_defaults.tab_width, 8);
5056 buffer_defaults.truncate_lines = Qnil;
5057 buffer_defaults.word_wrap = Qnil;
5058 buffer_defaults.ctl_arrow = Qt;
5059 buffer_defaults.bidi_display_reordering = Qnil;
5060 buffer_defaults.bidi_paragraph_direction = Qnil;
5061 buffer_defaults.cursor_type = Qt;
5062 buffer_defaults.extra_line_spacing = Qnil;
5063 buffer_defaults.cursor_in_non_selected_windows = Qt;
5065 #ifdef DOS_NT
5066 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5067 #endif
5068 buffer_defaults.enable_multibyte_characters = Qt;
5069 buffer_defaults.buffer_file_coding_system = Qnil;
5070 XSETFASTINT (buffer_defaults.fill_column, 70);
5071 XSETFASTINT (buffer_defaults.left_margin, 0);
5072 buffer_defaults.cache_long_line_scans = Qnil;
5073 buffer_defaults.file_truename = Qnil;
5074 XSETFASTINT (buffer_defaults.display_count, 0);
5075 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5076 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5077 buffer_defaults.left_fringe_width = Qnil;
5078 buffer_defaults.right_fringe_width = Qnil;
5079 buffer_defaults.fringes_outside_margins = Qnil;
5080 buffer_defaults.scroll_bar_width = Qnil;
5081 buffer_defaults.vertical_scroll_bar_type = Qt;
5082 buffer_defaults.indicate_empty_lines = Qnil;
5083 buffer_defaults.indicate_buffer_boundaries = Qnil;
5084 buffer_defaults.fringe_indicator_alist = Qnil;
5085 buffer_defaults.fringe_cursor_alist = Qnil;
5086 buffer_defaults.scroll_up_aggressively = Qnil;
5087 buffer_defaults.scroll_down_aggressively = Qnil;
5088 buffer_defaults.display_time = Qnil;
5090 /* Assign the local-flags to the slots that have default values.
5091 The local flag is a bit that is used in the buffer
5092 to say that it has its own local value for the slot.
5093 The local flag bits are in the local_var_flags slot of the buffer. */
5095 /* Nothing can work if this isn't true */
5096 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5098 /* 0 means not a lisp var, -1 means always local, else mask */
5099 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
5100 XSETINT (buffer_local_flags.filename, -1);
5101 XSETINT (buffer_local_flags.directory, -1);
5102 XSETINT (buffer_local_flags.backed_up, -1);
5103 XSETINT (buffer_local_flags.save_length, -1);
5104 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5105 XSETINT (buffer_local_flags.read_only, -1);
5106 XSETINT (buffer_local_flags.major_mode, -1);
5107 XSETINT (buffer_local_flags.mode_name, -1);
5108 XSETINT (buffer_local_flags.undo_list, -1);
5109 XSETINT (buffer_local_flags.mark_active, -1);
5110 XSETINT (buffer_local_flags.point_before_scroll, -1);
5111 XSETINT (buffer_local_flags.file_truename, -1);
5112 XSETINT (buffer_local_flags.invisibility_spec, -1);
5113 XSETINT (buffer_local_flags.file_format, -1);
5114 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5115 XSETINT (buffer_local_flags.display_count, -1);
5116 XSETINT (buffer_local_flags.display_time, -1);
5117 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5119 idx = 1;
5120 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5121 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5122 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5123 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5124 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5125 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5126 #ifndef old
5127 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5128 #endif
5129 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5131 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5132 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5133 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5134 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5135 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5136 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5137 #ifdef DOS_NT
5138 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5139 /* Make this one a permanent local. */
5140 buffer_permanent_local_flags[idx++] = 1;
5141 #endif
5142 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5143 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.bidi_display_reordering, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.bidi_paragraph_direction, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5148 /* Make this one a permanent local. */
5149 buffer_permanent_local_flags[idx++] = 1;
5150 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5151 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5152 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5154 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5160 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5161 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5162 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5163 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5164 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5165 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5166 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5168 /* Need more room? */
5169 if (idx >= MAX_PER_BUFFER_VARS)
5170 abort ();
5171 last_per_buffer_idx = idx;
5173 Vbuffer_alist = Qnil;
5174 current_buffer = 0;
5175 all_buffers = 0;
5177 QSFundamental = make_pure_c_string ("Fundamental");
5179 Qfundamental_mode = intern_c_string ("fundamental-mode");
5180 buffer_defaults.major_mode = Qfundamental_mode;
5182 Qmode_class = intern_c_string ("mode-class");
5184 Qprotected_field = intern_c_string ("protected-field");
5186 Qpermanent_local = intern_c_string ("permanent-local");
5188 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5189 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5191 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5193 /* super-magic invisible buffer */
5194 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5195 Vbuffer_alist = Qnil;
5197 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5199 inhibit_modification_hooks = 0;
5202 void
5203 init_buffer (void)
5205 char *pwd;
5206 Lisp_Object temp;
5207 int len;
5209 #ifdef USE_MMAP_FOR_BUFFERS
5211 /* When using the ralloc implementation based on mmap(2), buffer
5212 text pointers will have been set to null in the dumped Emacs.
5213 Map new memory. */
5214 struct buffer *b;
5216 for (b = all_buffers; b; b = b->next)
5217 if (b->text->beg == NULL)
5218 enlarge_buffer_text (b, 0);
5220 #endif /* USE_MMAP_FOR_BUFFERS */
5222 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5223 if (NILP (buffer_defaults.enable_multibyte_characters))
5224 Fset_buffer_multibyte (Qnil);
5226 pwd = get_current_dir_name ();
5228 if (!pwd)
5229 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5231 /* Maybe this should really use some standard subroutine
5232 whose definition is filename syntax dependent. */
5233 len = strlen (pwd);
5234 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5236 /* Grow buffer to add directory separator and '\0'. */
5237 pwd = (char *) realloc (pwd, len + 2);
5238 if (!pwd)
5239 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5240 pwd[len] = DIRECTORY_SEP;
5241 pwd[len + 1] = '\0';
5244 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5245 if (! NILP (buffer_defaults.enable_multibyte_characters))
5246 /* At this moment, we still don't know how to decode the
5247 directory name. So, we keep the bytes in multibyte form so
5248 that ENCODE_FILE correctly gets the original bytes. */
5249 current_buffer->directory
5250 = string_to_multibyte (current_buffer->directory);
5252 /* Add /: to the front of the name
5253 if it would otherwise be treated as magic. */
5254 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5255 if (! NILP (temp)
5256 /* If the default dir is just /, TEMP is non-nil
5257 because of the ange-ftp completion handler.
5258 However, it is not necessary to turn / into /:/.
5259 So avoid doing that. */
5260 && strcmp ("/", SDATA (current_buffer->directory)))
5261 current_buffer->directory
5262 = concat2 (build_string ("/:"), current_buffer->directory);
5264 temp = get_minibuffer (0);
5265 XBUFFER (temp)->directory = current_buffer->directory;
5267 free (pwd);
5270 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5271 Object stored in the current buffer. address is the address of the slot
5272 in the buffer that is current now. */
5274 /* TYPE is nil for a general Lisp variable.
5275 An integer specifies a type; then only Lisp values
5276 with that type code are allowed (except that nil is allowed too).
5277 LNAME is the Lisp-level variable name.
5278 VNAME is the name of the buffer slot.
5279 DOC is a dummy where you write the doc string as a comment. */
5280 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5281 do { \
5282 static struct Lisp_Buffer_Objfwd bo_fwd; \
5283 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5284 } while (0)
5286 static void
5287 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
5288 Lisp_Object *address, Lisp_Object type, char *doc)
5290 struct Lisp_Symbol *sym;
5291 int offset;
5293 sym = XSYMBOL (intern (namestring));
5294 offset = (char *)address - (char *)current_buffer;
5296 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5297 bo_fwd->offset = offset;
5298 bo_fwd->slottype = type;
5299 sym->redirect = SYMBOL_FORWARDED;
5301 /* I tried to do the job without a cast, but it seems impossible.
5302 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5303 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5305 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5307 if (PER_BUFFER_IDX (offset) == 0)
5308 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5309 slot of buffer_local_flags */
5310 abort ();
5314 /* initialize the buffer routines */
5315 void
5316 syms_of_buffer (void)
5318 staticpro (&last_overlay_modification_hooks);
5319 last_overlay_modification_hooks
5320 = Fmake_vector (make_number (10), Qnil);
5322 staticpro (&Vbuffer_defaults);
5323 staticpro (&Vbuffer_local_symbols);
5324 staticpro (&Qfundamental_mode);
5325 staticpro (&Qmode_class);
5326 staticpro (&QSFundamental);
5327 staticpro (&Vbuffer_alist);
5328 staticpro (&Qprotected_field);
5329 staticpro (&Qpermanent_local);
5330 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5331 staticpro (&Qpermanent_local_hook);
5332 staticpro (&Qkill_buffer_hook);
5333 Qoverlayp = intern_c_string ("overlayp");
5334 staticpro (&Qoverlayp);
5335 Qevaporate = intern_c_string ("evaporate");
5336 staticpro (&Qevaporate);
5337 Qmodification_hooks = intern_c_string ("modification-hooks");
5338 staticpro (&Qmodification_hooks);
5339 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5340 staticpro (&Qinsert_in_front_hooks);
5341 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5342 staticpro (&Qinsert_behind_hooks);
5343 Qget_file_buffer = intern_c_string ("get-file-buffer");
5344 staticpro (&Qget_file_buffer);
5345 Qpriority = intern_c_string ("priority");
5346 staticpro (&Qpriority);
5347 Qbefore_string = intern_c_string ("before-string");
5348 staticpro (&Qbefore_string);
5349 Qafter_string = intern_c_string ("after-string");
5350 staticpro (&Qafter_string);
5351 Qfirst_change_hook = intern_c_string ("first-change-hook");
5352 staticpro (&Qfirst_change_hook);
5353 Qbefore_change_functions = intern_c_string ("before-change-functions");
5354 staticpro (&Qbefore_change_functions);
5355 Qafter_change_functions = intern_c_string ("after-change-functions");
5356 staticpro (&Qafter_change_functions);
5357 /* The next one is initialized in init_buffer_once. */
5358 staticpro (&Qucs_set_table_for_input);
5360 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5361 staticpro (&Qkill_buffer_query_functions);
5363 Fput (Qprotected_field, Qerror_conditions,
5364 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5365 Fput (Qprotected_field, Qerror_message,
5366 make_pure_c_string ("Attempt to modify a protected field"));
5368 /* All these use DEFVAR_LISP_NOPRO because the slots in
5369 buffer_defaults will all be marked via Vbuffer_defaults. */
5371 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5372 &buffer_defaults.mode_line_format,
5373 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5374 This is the same as (default-value 'mode-line-format). */);
5376 DEFVAR_LISP_NOPRO ("default-header-line-format",
5377 &buffer_defaults.header_line_format,
5378 doc: /* Default value of `header-line-format' for buffers that don't override it.
5379 This is the same as (default-value 'header-line-format). */);
5381 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5382 doc: /* Default value of `cursor-type' for buffers that don't override it.
5383 This is the same as (default-value 'cursor-type). */);
5385 DEFVAR_LISP_NOPRO ("default-line-spacing",
5386 &buffer_defaults.extra_line_spacing,
5387 doc: /* Default value of `line-spacing' for buffers that don't override it.
5388 This is the same as (default-value 'line-spacing). */);
5390 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5391 &buffer_defaults.cursor_in_non_selected_windows,
5392 doc: /* Default value of `cursor-in-non-selected-windows'.
5393 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5395 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5396 &buffer_defaults.abbrev_mode,
5397 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5398 This is the same as (default-value 'abbrev-mode). */);
5400 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5401 &buffer_defaults.ctl_arrow,
5402 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5403 This is the same as (default-value 'ctl-arrow). */);
5405 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5406 &buffer_defaults.enable_multibyte_characters,
5407 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5408 This is the same as (default-value 'enable-multibyte-characters). */);
5410 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5411 &buffer_defaults.buffer_file_coding_system,
5412 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5413 This is the same as (default-value 'buffer-file-coding-system). */);
5415 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5416 &buffer_defaults.truncate_lines,
5417 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5418 This is the same as (default-value 'truncate-lines). */);
5420 DEFVAR_LISP_NOPRO ("default-fill-column",
5421 &buffer_defaults.fill_column,
5422 doc: /* Default value of `fill-column' for buffers that do not override it.
5423 This is the same as (default-value 'fill-column). */);
5425 DEFVAR_LISP_NOPRO ("default-left-margin",
5426 &buffer_defaults.left_margin,
5427 doc: /* Default value of `left-margin' for buffers that do not override it.
5428 This is the same as (default-value 'left-margin). */);
5430 DEFVAR_LISP_NOPRO ("default-tab-width",
5431 &buffer_defaults.tab_width,
5432 doc: /* Default value of `tab-width' for buffers that do not override it.
5433 This is the same as (default-value 'tab-width). */);
5435 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5436 &buffer_defaults.case_fold_search,
5437 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5438 This is the same as (default-value 'case-fold-search). */);
5440 #ifdef DOS_NT
5441 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5442 &buffer_defaults.buffer_file_type,
5443 doc: /* Default file type for buffers that do not override it.
5444 This is the same as (default-value 'buffer-file-type).
5445 The file type is nil for text, t for binary. */);
5446 #endif
5448 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5449 &buffer_defaults.left_margin_cols,
5450 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5451 This is the same as (default-value 'left-margin-width). */);
5453 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5454 &buffer_defaults.right_margin_cols,
5455 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5456 This is the same as (default-value 'right-margin-width). */);
5458 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5459 &buffer_defaults.left_fringe_width,
5460 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5461 This is the same as (default-value 'left-fringe-width). */);
5463 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5464 &buffer_defaults.right_fringe_width,
5465 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5466 This is the same as (default-value 'right-fringe-width). */);
5468 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5469 &buffer_defaults.fringes_outside_margins,
5470 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5471 This is the same as (default-value 'fringes-outside-margins). */);
5473 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5474 &buffer_defaults.scroll_bar_width,
5475 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5476 This is the same as (default-value 'scroll-bar-width). */);
5478 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5479 &buffer_defaults.vertical_scroll_bar_type,
5480 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5481 This is the same as (default-value 'vertical-scroll-bar). */);
5483 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5484 &buffer_defaults.indicate_empty_lines,
5485 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5486 This is the same as (default-value 'indicate-empty-lines). */);
5488 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5489 &buffer_defaults.indicate_buffer_boundaries,
5490 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5491 This is the same as (default-value 'indicate-buffer-boundaries). */);
5493 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5494 &buffer_defaults.fringe_indicator_alist,
5495 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5496 This is the same as (default-value 'fringe-indicator-alist'). */);
5498 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5499 &buffer_defaults.fringe_cursor_alist,
5500 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5501 This is the same as (default-value 'fringe-cursor-alist'). */);
5503 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5504 &buffer_defaults.scroll_up_aggressively,
5505 doc: /* Default value of `scroll-up-aggressively'.
5506 This value applies in buffers that don't have their own local values.
5507 This is the same as (default-value 'scroll-up-aggressively). */);
5509 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5510 &buffer_defaults.scroll_down_aggressively,
5511 doc: /* Default value of `scroll-down-aggressively'.
5512 This value applies in buffers that don't have their own local values.
5513 This is the same as (default-value 'scroll-down-aggressively). */);
5515 DEFVAR_PER_BUFFER ("header-line-format",
5516 &current_buffer->header_line_format,
5517 Qnil,
5518 doc: /* Analogous to `mode-line-format', but controls the header line.
5519 The header line appears, optionally, at the top of a window;
5520 the mode line appears at the bottom. */);
5522 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5523 Qnil,
5524 doc: /* Template for displaying mode line for current buffer.
5525 Each buffer has its own value of this variable.
5526 Value may be nil, a string, a symbol or a list or cons cell.
5527 A value of nil means don't display a mode line.
5528 For a symbol, its value is used (but it is ignored if t or nil).
5529 A string appearing directly as the value of a symbol is processed verbatim
5530 in that the %-constructs below are not recognized.
5531 Note that unless the symbol is marked as a `risky-local-variable', all
5532 properties in any strings, as well as all :eval and :propertize forms
5533 in the value of that symbol will be ignored.
5534 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5535 is used as a mode line element. Be careful--FORM should not load any files,
5536 because that can cause an infinite recursion.
5537 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5538 with the specified properties PROPS applied.
5539 For a list whose car is a symbol, the symbol's value is taken,
5540 and if that is non-nil, the cadr of the list is processed recursively.
5541 Otherwise, the caddr of the list (if there is one) is processed.
5542 For a list whose car is a string or list, each element is processed
5543 recursively and the results are effectively concatenated.
5544 For a list whose car is an integer, the cdr of the list is processed
5545 and padded (if the number is positive) or truncated (if negative)
5546 to the width specified by that number.
5547 A string is printed verbatim in the mode line except for %-constructs:
5548 (%-constructs are allowed when the string is the entire mode-line-format
5549 or when it is found in a cons-cell or a list)
5550 %b -- print buffer name. %f -- print visited file name.
5551 %F -- print frame name.
5552 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5553 %& is like %*, but ignore read-only-ness.
5554 % means buffer is read-only and * means it is modified.
5555 For a modified read-only buffer, %* gives % and %+ gives *.
5556 %s -- print process status. %l -- print the current line number.
5557 %c -- print the current column number (this makes editing slower).
5558 To make the column number update correctly in all cases,
5559 `column-number-mode' must be non-nil.
5560 %i -- print the size of the buffer.
5561 %I -- like %i, but use k, M, G, etc., to abbreviate.
5562 %p -- print percent of buffer above top of window, or Top, Bot or All.
5563 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5564 or print Bottom or All.
5565 %n -- print Narrow if appropriate.
5566 %t -- visited file is text or binary (if OS supports this distinction).
5567 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5568 %Z -- like %z, but including the end-of-line format.
5569 %e -- print error message about full memory.
5570 %@ -- print @ or hyphen. @ means that default-directory is on a
5571 remote machine.
5572 %[ -- print one [ for each recursive editing level. %] similar.
5573 %% -- print %. %- -- print infinitely many dashes.
5574 Decimal digits after the % specify field width to which to pad. */);
5576 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5577 doc: /* *Value of `major-mode' for new buffers. */);
5579 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5580 make_number (Lisp_Symbol),
5581 doc: /* Symbol for current buffer's major mode.
5582 The default value (normally `fundamental-mode') affects new buffers.
5583 A value of nil means to use the current buffer's major mode, provided
5584 it is not marked as "special".
5586 When a mode is used by default, `find-file' switches to it before it
5587 reads the contents into the buffer and before it finishes setting up
5588 the buffer. Thus, the mode and its hooks should not expect certain
5589 variables such as `buffer-read-only' and `buffer-file-coding-system'
5590 to be set up. */);
5592 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5593 Qnil,
5594 doc: /* Pretty name of current buffer's major mode.
5595 Usually a string, but can use any of the constructs for `mode-line-format',
5596 which see.
5597 Format with `format-mode-line' to produce a string value. */);
5599 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5600 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5602 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5603 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5605 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5606 Qnil,
5607 doc: /* *Non-nil if searches and matches should ignore case. */);
5609 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5610 make_number (LISP_INT_TAG),
5611 doc: /* *Column beyond which automatic line-wrapping should happen.
5612 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5614 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5615 make_number (LISP_INT_TAG),
5616 doc: /* *Column for the default `indent-line-function' to indent to.
5617 Linefeed indents to this column in Fundamental mode. */);
5619 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5620 make_number (LISP_INT_TAG),
5621 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5623 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5624 doc: /* *Non-nil means display control chars with uparrow.
5625 A value of nil means use backslash and octal digits.
5626 This variable does not apply to characters whose display is specified
5627 in the current display table (if there is one). */);
5629 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5630 &current_buffer->enable_multibyte_characters,
5631 Qnil,
5632 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5633 Otherwise they are regarded as unibyte. This affects the display,
5634 file I/O and the behavior of various editing commands.
5636 This variable is buffer-local but you cannot set it directly;
5637 use the function `set-buffer-multibyte' to change a buffer's representation.
5638 Changing its default value with `setq-default' is supported.
5639 See also variable `default-enable-multibyte-characters' and Info node
5640 `(elisp)Text Representations'. */);
5641 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5643 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5644 &current_buffer->buffer_file_coding_system, Qnil,
5645 doc: /* Coding system to be used for encoding the buffer contents on saving.
5646 This variable applies to saving the buffer, and also to `write-region'
5647 and other functions that use `write-region'.
5648 It does not apply to sending output to subprocesses, however.
5650 If this is nil, the buffer is saved without any code conversion
5651 unless some coding system is specified in `file-coding-system-alist'
5652 for the buffer file.
5654 If the text to be saved cannot be encoded as specified by this variable,
5655 an alternative encoding is selected by `select-safe-coding-system', which see.
5657 The variable `coding-system-for-write', if non-nil, overrides this variable.
5659 This variable is never applied to a way of decoding a file while reading it. */);
5661 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5662 &current_buffer->bidi_display_reordering, Qnil,
5663 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5665 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5666 &current_buffer->bidi_paragraph_direction, Qnil,
5667 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5669 If this is nil (the default), the direction of each paragraph is
5670 determined by the first strong directional character of its text.
5671 The values of `right-to-left' and `left-to-right' override that.
5672 Any other value is treated as nil.
5674 This variable has no effect unless the buffer's value of
5675 \`bidi-display-reordering' is non-nil. */);
5677 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5678 doc: /* *Non-nil means do not display continuation lines.
5679 Instead, give each line of text just one screen line.
5681 Note that this is overridden by the variable
5682 `truncate-partial-width-windows' if that variable is non-nil
5683 and this buffer is not full-frame width. */);
5685 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5686 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5687 When word-wrapping is on, continuation lines are wrapped at the space
5688 or tab character nearest to the right window edge.
5689 If nil, continuation lines are wrapped at the right screen edge.
5691 This variable has no effect if long lines are truncated (see
5692 `truncate-lines' and `truncate-partial-width-windows'). If you use
5693 word-wrapping, you might want to reduce the value of
5694 `truncate-partial-width-windows', since wrapping can make text readable
5695 in narrower windows. */);
5697 #ifdef DOS_NT
5698 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5699 Qnil,
5700 doc: /* Non-nil if the visited file is a binary file.
5701 This variable is meaningful on MS-DOG and Windows NT.
5702 On those systems, it is automatically local in every buffer.
5703 On other systems, this variable is normally always nil. */);
5704 #endif
5706 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5707 make_number (Lisp_String),
5708 doc: /* Name of default directory of current buffer. Should end with slash.
5709 To interactively change the default directory, use command `cd'. */);
5711 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5712 Qnil,
5713 doc: /* Function called (if non-nil) to perform auto-fill.
5714 It is called after self-inserting any character specified in
5715 the `auto-fill-chars' table.
5716 NOTE: This variable is not a hook;
5717 its value may not be a list of functions. */);
5719 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5720 make_number (Lisp_String),
5721 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5723 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5724 make_number (Lisp_String),
5725 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5726 The truename of a file is calculated by `file-truename'
5727 and then abbreviated with `abbreviate-file-name'. */);
5729 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5730 &current_buffer->auto_save_file_name,
5731 make_number (Lisp_String),
5732 doc: /* Name of file for auto-saving current buffer.
5733 If it is nil, that means don't auto-save this buffer. */);
5735 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5736 doc: /* Non-nil if this buffer is read-only. */);
5738 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5739 doc: /* Non-nil if this buffer's file has been backed up.
5740 Backing up is done before the first time the file is saved. */);
5742 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5743 make_number (LISP_INT_TAG),
5744 doc: /* Length of current buffer when last read in, saved or auto-saved.
5745 0 initially.
5746 -1 means auto-saving turned off until next real save.
5748 If you set this to -2, that means don't turn off auto-saving in this buffer
5749 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5750 you probably should set this to -2 in that buffer. */);
5752 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5753 Qnil,
5754 doc: /* Non-nil enables selective display.
5755 An integer N as value means display only lines
5756 that start with less than N columns of space.
5757 A value of t means that the character ^M makes itself and
5758 all the rest of the line invisible; also, when saving the buffer
5759 in a file, save the ^M as a newline. */);
5761 #ifndef old
5762 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5763 &current_buffer->selective_display_ellipses,
5764 Qnil,
5765 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5766 #endif
5768 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5769 doc: /* Non-nil if self-insertion should replace existing text.
5770 The value should be one of `overwrite-mode-textual',
5771 `overwrite-mode-binary', or nil.
5772 If it is `overwrite-mode-textual', self-insertion still
5773 inserts at the end of a line, and inserts when point is before a tab,
5774 until the tab is filled in.
5775 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5777 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5778 Qnil,
5779 doc: /* Display table that controls display of the contents of current buffer.
5781 If this variable is nil, the value of `standard-display-table' is used.
5782 Each window can have its own, overriding display table, see
5783 `set-window-display-table' and `window-display-table'.
5785 The display table is a char-table created with `make-display-table'.
5786 A char-table is an array indexed by character codes. Normal array
5787 primitives `aref' and `aset' can be used to access elements of a char-table.
5789 Each of the char-table elements control how to display the corresponding
5790 text character: the element at index C in the table says how to display
5791 the character whose code is C. Each element should be a vector of
5792 characters or nil. The value nil means display the character in the
5793 default fashion; otherwise, the characters from the vector are delivered
5794 to the screen instead of the original character.
5796 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5797 to display a capital Y instead of each X character.
5799 In addition, a char-table has six extra slots to control the display of:
5801 the end of a truncated screen line (extra-slot 0, a single character);
5802 the end of a continued line (extra-slot 1, a single character);
5803 the escape character used to display character codes in octal
5804 (extra-slot 2, a single character);
5805 the character used as an arrow for control characters (extra-slot 3,
5806 a single character);
5807 the decoration indicating the presence of invisible lines (extra-slot 4,
5808 a vector of characters);
5809 the character used to draw the border between side-by-side windows
5810 (extra-slot 5, a single character).
5812 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5814 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5815 Qnil,
5816 doc: /* *Width of left marginal area for display of a buffer.
5817 A value of nil means no marginal area. */);
5819 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5820 Qnil,
5821 doc: /* *Width of right marginal area for display of a buffer.
5822 A value of nil means no marginal area. */);
5824 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5825 Qnil,
5826 doc: /* *Width of this buffer's left fringe (in pixels).
5827 A value of 0 means no left fringe is shown in this buffer's window.
5828 A value of nil means to use the left fringe width from the window's frame. */);
5830 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5831 Qnil,
5832 doc: /* *Width of this buffer's right fringe (in pixels).
5833 A value of 0 means no right fringe is shown in this buffer's window.
5834 A value of nil means to use the right fringe width from the window's frame. */);
5836 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5837 Qnil,
5838 doc: /* *Non-nil means to display fringes outside display margins.
5839 A value of nil means to display fringes between margins and buffer text. */);
5841 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5842 Qnil,
5843 doc: /* *Width of this buffer's scroll bars in pixels.
5844 A value of nil means to use the scroll bar width from the window's frame. */);
5846 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5847 Qnil,
5848 doc: /* *Position of this buffer's vertical scroll bar.
5849 The value takes effect whenever you tell a window to display this buffer;
5850 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5852 A value of `left' or `right' means put the vertical scroll bar at that side
5853 of the window; a value of nil means don't show any vertical scroll bars.
5854 A value of t (the default) means do whatever the window's frame specifies. */);
5856 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5857 &current_buffer->indicate_empty_lines, Qnil,
5858 doc: /* *Visually indicate empty lines after the buffer end.
5859 If non-nil, a bitmap is displayed in the left fringe of a window on
5860 window-systems. */);
5862 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5863 &current_buffer->indicate_buffer_boundaries, Qnil,
5864 doc: /* *Visually indicate buffer boundaries and scrolling.
5865 If non-nil, the first and last line of the buffer are marked in the fringe
5866 of a window on window-systems with angle bitmaps, or if the window can be
5867 scrolled, the top and bottom line of the window are marked with up and down
5868 arrow bitmaps.
5870 If value is a symbol `left' or `right', both angle and arrow bitmaps
5871 are displayed in the left or right fringe, resp. Any other value
5872 that doesn't look like an alist means display the angle bitmaps in
5873 the left fringe but no arrows.
5875 You can exercise more precise control by using an alist as the
5876 value. Each alist element (INDICATOR . POSITION) specifies
5877 where to show one of the indicators. INDICATOR is one of `top',
5878 `bottom', `up', `down', or t, which specifies the default position,
5879 and POSITION is one of `left', `right', or nil, meaning do not show
5880 this indicator.
5882 For example, ((top . left) (t . right)) places the top angle bitmap in
5883 left fringe, the bottom angle bitmap in right fringe, and both arrow
5884 bitmaps in right fringe. To show just the angle bitmaps in the left
5885 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5887 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5888 &current_buffer->fringe_indicator_alist, Qnil,
5889 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5890 The value is an alist where each element (INDICATOR . BITMAPS)
5891 specifies the fringe bitmaps used to display a specific logical
5892 fringe indicator.
5894 INDICATOR specifies the logical indicator type which is one of the
5895 following symbols: `truncation' , `continuation', `overlay-arrow',
5896 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5898 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5899 the actual bitmap shown in the left or right fringe for the logical
5900 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5901 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5902 are used only for the `bottom' and `top-bottom' indicators when the
5903 last (only) line has no final newline. BITMAPS may also be a single
5904 symbol which is used in both left and right fringes. */);
5906 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5907 &current_buffer->fringe_cursor_alist, Qnil,
5908 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5909 The value is an alist where each element (CURSOR . BITMAP)
5910 specifies the fringe bitmaps used to display a specific logical
5911 cursor type in the fringe.
5913 CURSOR specifies the logical cursor type which is one of the following
5914 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5915 one is used to show a hollow cursor on narrow lines display lines
5916 where the normal hollow cursor will not fit.
5918 BITMAP is the corresponding fringe bitmap shown for the logical
5919 cursor type. */);
5921 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5922 &current_buffer->scroll_up_aggressively, Qnil,
5923 doc: /* How far to scroll windows upward.
5924 If you move point off the bottom, the window scrolls automatically.
5925 This variable controls how far it scrolls. The value nil, the default,
5926 means scroll to center point. A fraction means scroll to put point
5927 that fraction of the window's height from the bottom of the window.
5928 When the value is 0.0, point goes at the bottom line, which in the
5929 simple case that you moved off with C-f means scrolling just one line.
5930 1.0 means point goes at the top, so that in that simple case, the
5931 window scrolls by a full window height. Meaningful values are
5932 between 0.0 and 1.0, inclusive. */);
5934 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5935 &current_buffer->scroll_down_aggressively, Qnil,
5936 doc: /* How far to scroll windows downward.
5937 If you move point off the top, the window scrolls automatically.
5938 This variable controls how far it scrolls. The value nil, the default,
5939 means scroll to center point. A fraction means scroll to put point
5940 that fraction of the window's height from the top of the window.
5941 When the value is 0.0, point goes at the top line, which in the
5942 simple case that you moved off with C-b means scrolling just one line.
5943 1.0 means point goes at the bottom, so that in that simple case, the
5944 window scrolls by a full window height. Meaningful values are
5945 between 0.0 and 1.0, inclusive. */);
5947 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5948 "Don't ask.");
5951 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5952 doc: /* List of functions to call before each text change.
5953 Two arguments are passed to each function: the positions of
5954 the beginning and end of the range of old text to be changed.
5955 \(For an insertion, the beginning and end are at the same place.)
5956 No information is given about the length of the text after the change.
5958 Buffer changes made while executing the `before-change-functions'
5959 don't call any before-change or after-change functions.
5960 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5962 If an unhandled error happens in running these functions,
5963 the variable's value remains nil. That prevents the error
5964 from happening repeatedly and making Emacs nonfunctional. */);
5965 Vbefore_change_functions = Qnil;
5967 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5968 doc: /* List of functions to call after each text change.
5969 Three arguments are passed to each function: the positions of
5970 the beginning and end of the range of changed text,
5971 and the length in bytes of the pre-change text replaced by that range.
5972 \(For an insertion, the pre-change length is zero;
5973 for a deletion, that length is the number of bytes deleted,
5974 and the post-change beginning and end are at the same place.)
5976 Buffer changes made while executing the `after-change-functions'
5977 don't call any before-change or after-change functions.
5978 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5980 If an unhandled error happens in running these functions,
5981 the variable's value remains nil. That prevents the error
5982 from happening repeatedly and making Emacs nonfunctional. */);
5983 Vafter_change_functions = Qnil;
5985 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5986 doc: /* A list of functions to call before changing a buffer which is unmodified.
5987 The functions are run using the `run-hooks' function. */);
5988 Vfirst_change_hook = Qnil;
5990 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5991 doc: /* List of undo entries in current buffer.
5992 Recent changes come first; older changes follow newer.
5994 An entry (BEG . END) represents an insertion which begins at
5995 position BEG and ends at position END.
5997 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5998 from (abs POSITION). If POSITION is positive, point was at the front
5999 of the text being deleted; if negative, point was at the end.
6001 An entry (t HIGH . LOW) indicates that the buffer previously had
6002 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6003 of the visited file's modification time, as of that time. If the
6004 modification time of the most recent save is different, this entry is
6005 obsolete.
6007 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6008 was modified between BEG and END. PROPERTY is the property name,
6009 and VALUE is the old value.
6011 An entry (apply FUN-NAME . ARGS) means undo the change with
6012 \(apply FUN-NAME ARGS).
6014 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6015 in the active region. BEG and END is the range affected by this entry
6016 and DELTA is the number of bytes added or deleted in that range by
6017 this change.
6019 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6020 was adjusted in position by the offset DISTANCE (an integer).
6022 An entry of the form POSITION indicates that point was at the buffer
6023 location given by the integer. Undoing an entry of this form places
6024 point at POSITION.
6026 Entries with value `nil' mark undo boundaries. The undo command treats
6027 the changes between two undo boundaries as a single step to be undone.
6029 If the value of the variable is t, undo information is not recorded. */);
6031 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6032 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6034 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6035 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6037 Normally, the line-motion functions work by scanning the buffer for
6038 newlines. Columnar operations (like `move-to-column' and
6039 `compute-motion') also work by scanning the buffer, summing character
6040 widths as they go. This works well for ordinary text, but if the
6041 buffer's lines are very long (say, more than 500 characters), these
6042 motion functions will take longer to execute. Emacs may also take
6043 longer to update the display.
6045 If `cache-long-line-scans' is non-nil, these motion functions cache the
6046 results of their scans, and consult the cache to avoid rescanning
6047 regions of the buffer until the text is modified. The caches are most
6048 beneficial when they prevent the most searching---that is, when the
6049 buffer contains long lines and large regions of characters with the
6050 same, fixed screen width.
6052 When `cache-long-line-scans' is non-nil, processing short lines will
6053 become slightly slower (because of the overhead of consulting the
6054 cache), and the caches will use memory roughly proportional to the
6055 number of newlines and characters whose screen width varies.
6057 The caches require no explicit maintenance; their accuracy is
6058 maintained internally by the Emacs primitives. Enabling or disabling
6059 the cache should not affect the behavior of any of the motion
6060 functions; it should only affect their performance. */);
6062 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6063 doc: /* Value of point before the last series of scroll operations, or nil. */);
6065 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6066 doc: /* List of formats to use when saving this buffer.
6067 Formats are defined by `format-alist'. This variable is
6068 set when a file is visited. */);
6070 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6071 &current_buffer->auto_save_file_format, Qnil,
6072 doc: /* *Format in which to write auto-save files.
6073 Should be a list of symbols naming formats that are defined in `format-alist'.
6074 If it is t, which is the default, auto-save files are written in the
6075 same format as a regular save would use. */);
6077 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6078 &current_buffer->invisibility_spec, Qnil,
6079 doc: /* Invisibility spec of this buffer.
6080 The default is t, which means that text is invisible
6081 if it has a non-nil `invisible' property.
6082 If the value is a list, a text character is invisible if its `invisible'
6083 property is an element in that list (or is a list with members in common).
6084 If an element is a cons cell of the form (PROP . ELLIPSIS),
6085 then characters with property value PROP are invisible,
6086 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6088 DEFVAR_PER_BUFFER ("buffer-display-count",
6089 &current_buffer->display_count, Qnil,
6090 doc: /* A number incremented each time this buffer is displayed in a window.
6091 The function `set-window-buffer' increments it. */);
6093 DEFVAR_PER_BUFFER ("buffer-display-time",
6094 &current_buffer->display_time, Qnil,
6095 doc: /* Time stamp updated each time this buffer is displayed in a window.
6096 The function `set-window-buffer' updates this variable
6097 to the value obtained by calling `current-time'.
6098 If the buffer has never been shown in a window, the value is nil. */);
6100 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6101 doc: /* */);
6102 Vtransient_mark_mode = Qnil;
6103 /* The docstring is in simple.el. If we put it here, it would be
6104 overwritten when transient-mark-mode is defined using
6105 define-minor-mode. */
6107 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6108 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6109 If the value is t, disregard `buffer-read-only' and all `read-only'
6110 text properties. If the value is a list, disregard `buffer-read-only'
6111 and disregard a `read-only' text property if the property value
6112 is a member of the list. */);
6113 Vinhibit_read_only = Qnil;
6115 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6116 doc: /* Cursor to use when this buffer is in the selected window.
6117 Values are interpreted as follows:
6119 t use the cursor specified for the frame
6120 nil don't display a cursor
6121 box display a filled box cursor
6122 hollow display a hollow box cursor
6123 bar display a vertical bar cursor with default width
6124 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6125 hbar display a horizontal bar cursor with default height
6126 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6127 ANYTHING ELSE display a hollow box cursor
6129 When the buffer is displayed in a non-selected window, the
6130 cursor's appearance is instead controlled by the variable
6131 `cursor-in-non-selected-windows'. */);
6133 DEFVAR_PER_BUFFER ("line-spacing",
6134 &current_buffer->extra_line_spacing, Qnil,
6135 doc: /* Additional space to put between lines when displaying a buffer.
6136 The space is measured in pixels, and put below lines on graphic displays,
6137 see `display-graphic-p'.
6138 If value is a floating point number, it specifies the spacing relative
6139 to the default frame line height. A value of nil means add no extra space. */);
6141 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6142 &current_buffer->cursor_in_non_selected_windows, Qnil,
6143 doc: /* *Cursor type to display in non-selected windows.
6144 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6146 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6147 doc: /* List of functions called with no args to query before killing a buffer.
6148 The buffer being killed will be current while the functions are running.
6149 If any of them returns nil, the buffer is not killed. */);
6150 Vkill_buffer_query_functions = Qnil;
6152 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6153 doc: /* Normal hook run before changing the major mode of a buffer.
6154 The function `kill-all-local-variables' runs this before doing anything else. */);
6155 Vchange_major_mode_hook = Qnil;
6156 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6157 staticpro (&Qchange_major_mode_hook);
6159 defsubr (&Sbuffer_live_p);
6160 defsubr (&Sbuffer_list);
6161 defsubr (&Sget_buffer);
6162 defsubr (&Sget_file_buffer);
6163 defsubr (&Sget_buffer_create);
6164 defsubr (&Smake_indirect_buffer);
6165 defsubr (&Sgenerate_new_buffer_name);
6166 defsubr (&Sbuffer_name);
6167 /*defsubr (&Sbuffer_number);*/
6168 defsubr (&Sbuffer_file_name);
6169 defsubr (&Sbuffer_base_buffer);
6170 defsubr (&Sbuffer_local_value);
6171 defsubr (&Sbuffer_local_variables);
6172 defsubr (&Sbuffer_modified_p);
6173 defsubr (&Sset_buffer_modified_p);
6174 defsubr (&Sbuffer_modified_tick);
6175 defsubr (&Sbuffer_chars_modified_tick);
6176 defsubr (&Srename_buffer);
6177 defsubr (&Sother_buffer);
6178 defsubr (&Sbuffer_enable_undo);
6179 defsubr (&Skill_buffer);
6180 defsubr (&Sset_buffer_major_mode);
6181 defsubr (&Sswitch_to_buffer);
6182 defsubr (&Scurrent_buffer);
6183 defsubr (&Sset_buffer);
6184 defsubr (&Sbarf_if_buffer_read_only);
6185 defsubr (&Sbury_buffer);
6186 defsubr (&Serase_buffer);
6187 defsubr (&Sbuffer_swap_text);
6188 defsubr (&Sset_buffer_multibyte);
6189 defsubr (&Skill_all_local_variables);
6191 defsubr (&Soverlayp);
6192 defsubr (&Smake_overlay);
6193 defsubr (&Sdelete_overlay);
6194 defsubr (&Smove_overlay);
6195 defsubr (&Soverlay_start);
6196 defsubr (&Soverlay_end);
6197 defsubr (&Soverlay_buffer);
6198 defsubr (&Soverlay_properties);
6199 defsubr (&Soverlays_at);
6200 defsubr (&Soverlays_in);
6201 defsubr (&Snext_overlay_change);
6202 defsubr (&Sprevious_overlay_change);
6203 defsubr (&Soverlay_recenter);
6204 defsubr (&Soverlay_lists);
6205 defsubr (&Soverlay_get);
6206 defsubr (&Soverlay_put);
6207 defsubr (&Srestore_buffer_modified_p);
6210 void
6211 keys_of_buffer (void)
6213 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6214 initial_define_key (control_x_map, 'k', "kill-buffer");
6216 /* This must not be in syms_of_buffer, because Qdisabled is not
6217 initialized when that function gets called. */
6218 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6221 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6222 (do not change this comment) */