Merge chages made in Gnus trunk.
[emacs.git] / src / buffer.c
blobd6c7f6db068491cae8f6744c508607eb512f3363
1 /* Buffer manipulation primitives for GNU Emacs.
3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010, 2011 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>
30 #include <unistd.h>
32 #include "lisp.h"
33 #include "intervals.h"
34 #include "window.h"
35 #include "commands.h"
36 #include "buffer.h"
37 #include "character.h"
38 #include "region-cache.h"
39 #include "indent.h"
40 #include "blockinput.h"
41 #include "keyboard.h"
42 #include "keymap.h"
43 #include "frame.h"
45 struct buffer *current_buffer; /* the current buffer */
47 /* First buffer in chain of all buffers (in reverse order of creation).
48 Threaded through ->next. */
50 struct buffer *all_buffers;
52 /* This structure holds the default values of the buffer-local variables
53 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
54 The default value occupies the same slot in this structure
55 as an individual buffer's value occupies in that buffer.
56 Setting the default value also goes through the alist of buffers
57 and stores into each buffer that does not say it has a local value. */
59 DECL_ALIGN (struct buffer, buffer_defaults);
61 /* A Lisp_Object pointer to the above, used for staticpro */
63 static Lisp_Object Vbuffer_defaults;
65 /* This structure marks which slots in a buffer have corresponding
66 default values in buffer_defaults.
67 Each such slot has a nonzero value in this structure.
68 The value has only one nonzero bit.
70 When a buffer has its own local value for a slot,
71 the entry for that slot (found in the same slot in this structure)
72 is turned on in the buffer's local_flags array.
74 If a slot in this structure is -1, then even though there may
75 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
76 and the corresponding slot in buffer_defaults is not used.
78 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
79 zero, that is a bug */
81 struct buffer buffer_local_flags;
83 /* This structure holds the names of symbols whose values may be
84 buffer-local. It is indexed and accessed in the same way as the above. */
86 DECL_ALIGN (struct buffer, buffer_local_symbols);
88 /* A Lisp_Object pointer to the above, used for staticpro */
89 static Lisp_Object Vbuffer_local_symbols;
91 /* Return the symbol of the per-buffer variable at offset OFFSET in
92 the buffer structure. */
94 #define PER_BUFFER_SYMBOL(OFFSET) \
95 (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
97 /* Flags indicating which built-in buffer-local variables
98 are permanent locals. */
99 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
101 /* Number of per-buffer variables used. */
103 int last_per_buffer_idx;
105 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
106 int after, Lisp_Object arg1,
107 Lisp_Object arg2, Lisp_Object arg3);
108 static void swap_out_buffer_local_variables (struct buffer *b);
109 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
111 /* Alist of all buffer names vs the buffers. */
112 /* This used to be a variable, but is no longer,
113 to prevent lossage due to user rplac'ing this alist or its elements. */
114 Lisp_Object Vbuffer_alist;
116 Lisp_Object Qkill_buffer_query_functions;
118 /* Hook run before changing a major mode. */
119 Lisp_Object Qchange_major_mode_hook;
121 Lisp_Object Qfirst_change_hook;
122 Lisp_Object Qbefore_change_functions;
123 Lisp_Object Qafter_change_functions;
124 Lisp_Object Qucs_set_table_for_input;
126 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
127 Lisp_Object Qpermanent_local_hook;
129 Lisp_Object Qprotected_field;
131 Lisp_Object QSFundamental; /* A string "Fundamental" */
133 Lisp_Object Qkill_buffer_hook;
135 Lisp_Object Qget_file_buffer;
137 Lisp_Object Qoverlayp;
139 Lisp_Object Qpriority, Qevaporate, Qbefore_string, Qafter_string;
141 Lisp_Object Qmodification_hooks;
142 Lisp_Object Qinsert_in_front_hooks;
143 Lisp_Object Qinsert_behind_hooks;
145 static void alloc_buffer_text (struct buffer *, size_t);
146 static void free_buffer_text (struct buffer *b);
147 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
148 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
149 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
151 /* For debugging; temporary. See set_buffer_internal. */
152 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
154 void
155 nsberror (Lisp_Object spec)
157 if (STRINGP (spec))
158 error ("No buffer named %s", SDATA (spec));
159 error ("Invalid buffer argument");
162 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
163 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
164 Value is nil if OBJECT is not a buffer or if it has been killed. */)
165 (Lisp_Object object)
167 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
168 ? Qt : Qnil);
171 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
172 doc: /* Return a list of all existing live buffers.
173 If the optional arg FRAME is a frame, we return the buffer list
174 in the proper order for that frame: the buffers in FRAME's `buffer-list'
175 frame parameter come first, followed by the rest of the buffers. */)
176 (Lisp_Object frame)
178 Lisp_Object general;
179 general = Fmapcar (Qcdr, Vbuffer_alist);
181 if (FRAMEP (frame))
183 Lisp_Object framelist, prevlist, tail;
184 Lisp_Object args[3];
186 CHECK_FRAME (frame);
188 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
189 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
191 /* Remove from GENERAL any buffer that duplicates one in
192 FRAMELIST or PREVLIST. */
193 tail = framelist;
194 while (CONSP (tail))
196 general = Fdelq (XCAR (tail), general);
197 tail = XCDR (tail);
199 tail = prevlist;
200 while (CONSP (tail))
202 general = Fdelq (XCAR (tail), general);
203 tail = XCDR (tail);
206 args[0] = framelist;
207 args[1] = general;
208 args[2] = prevlist;
209 return Fnconc (3, args);
212 return general;
215 /* Like Fassoc, but use Fstring_equal to compare
216 (which ignores text properties),
217 and don't ever QUIT. */
219 static Lisp_Object
220 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
222 register Lisp_Object tail;
223 for (tail = list; CONSP (tail); tail = XCDR (tail))
225 register Lisp_Object elt, tem;
226 elt = XCAR (tail);
227 tem = Fstring_equal (Fcar (elt), key);
228 if (!NILP (tem))
229 return elt;
231 return Qnil;
234 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
235 doc: /* Return the buffer named BUFFER-OR-NAME.
236 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
237 is a string and there is no buffer with that name, return nil. If
238 BUFFER-OR-NAME is a buffer, return it as given. */)
239 (register Lisp_Object buffer_or_name)
241 if (BUFFERP (buffer_or_name))
242 return buffer_or_name;
243 CHECK_STRING (buffer_or_name);
245 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
248 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
249 doc: /* Return the buffer visiting file FILENAME (a string).
250 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
251 If there is no such live buffer, return nil.
252 See also `find-buffer-visiting'. */)
253 (register Lisp_Object filename)
255 register Lisp_Object tail, buf, tem;
256 Lisp_Object handler;
258 CHECK_STRING (filename);
259 filename = Fexpand_file_name (filename, Qnil);
261 /* If the file name has special constructs in it,
262 call the corresponding file handler. */
263 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
264 if (!NILP (handler))
265 return call2 (handler, Qget_file_buffer, filename);
267 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
269 buf = Fcdr (XCAR (tail));
270 if (!BUFFERP (buf)) continue;
271 if (!STRINGP (XBUFFER (buf)->filename)) continue;
272 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
273 if (!NILP (tem))
274 return buf;
276 return Qnil;
279 Lisp_Object
280 get_truename_buffer (register Lisp_Object filename)
282 register Lisp_Object tail, buf, tem;
284 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
286 buf = Fcdr (XCAR (tail));
287 if (!BUFFERP (buf)) continue;
288 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
289 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
290 if (!NILP (tem))
291 return buf;
293 return Qnil;
296 /* Incremented for each buffer created, to assign the buffer number. */
297 int buffer_count;
299 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
300 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
301 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
302 return that buffer. If no such buffer exists, create a new buffer with
303 that name and return it. If BUFFER-OR-NAME starts with a space, the new
304 buffer does not keep undo information.
306 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
307 even if it is dead. The return value is never nil. */)
308 (register Lisp_Object buffer_or_name)
310 register Lisp_Object buffer, name;
311 register struct buffer *b;
313 buffer = Fget_buffer (buffer_or_name);
314 if (!NILP (buffer))
315 return buffer;
317 if (SCHARS (buffer_or_name) == 0)
318 error ("Empty string for buffer name is not allowed");
320 b = allocate_buffer ();
322 /* An ordinary buffer uses its own struct buffer_text. */
323 b->text = &b->own_text;
324 b->base_buffer = 0;
326 BUF_GAP_SIZE (b) = 20;
327 BLOCK_INPUT;
328 /* We allocate extra 1-byte at the tail and keep it always '\0' for
329 anchoring a search. */
330 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
331 UNBLOCK_INPUT;
332 if (! BUF_BEG_ADDR (b))
333 buffer_memory_full ();
335 BUF_PT (b) = BEG;
336 BUF_GPT (b) = BEG;
337 BUF_BEGV (b) = BEG;
338 BUF_ZV (b) = BEG;
339 BUF_Z (b) = BEG;
340 BUF_PT_BYTE (b) = BEG_BYTE;
341 BUF_GPT_BYTE (b) = BEG_BYTE;
342 BUF_BEGV_BYTE (b) = BEG_BYTE;
343 BUF_ZV_BYTE (b) = BEG_BYTE;
344 BUF_Z_BYTE (b) = BEG_BYTE;
345 BUF_MODIFF (b) = 1;
346 BUF_CHARS_MODIFF (b) = 1;
347 BUF_OVERLAY_MODIFF (b) = 1;
348 BUF_SAVE_MODIFF (b) = 1;
349 BUF_INTERVALS (b) = 0;
350 BUF_UNCHANGED_MODIFIED (b) = 1;
351 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
352 BUF_END_UNCHANGED (b) = 0;
353 BUF_BEG_UNCHANGED (b) = 0;
354 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
356 b->newline_cache = 0;
357 b->width_run_cache = 0;
358 b->width_table = Qnil;
359 b->prevent_redisplay_optimizations_p = 1;
361 /* Put this on the chain of all buffers including killed ones. */
362 b->next = all_buffers;
363 all_buffers = b;
365 /* An ordinary buffer normally doesn't need markers
366 to handle BEGV and ZV. */
367 b->pt_marker = Qnil;
368 b->begv_marker = Qnil;
369 b->zv_marker = Qnil;
371 name = Fcopy_sequence (buffer_or_name);
372 STRING_SET_INTERVALS (name, NULL_INTERVAL);
373 b->name = name;
375 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
377 reset_buffer (b);
378 reset_buffer_local_variables (b, 1);
380 b->mark = Fmake_marker ();
381 BUF_MARKERS (b) = NULL;
382 b->name = name;
384 /* Put this in the alist of all live buffers. */
385 XSETBUFFER (buffer, b);
386 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
388 /* An error in calling the function here (should someone redefine it)
389 can lead to infinite regress until you run out of stack. rms
390 says that's not worth protecting against. */
391 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
392 /* buffer is on buffer-alist, so no gcpro. */
393 call1 (Qucs_set_table_for_input, buffer);
395 return buffer;
399 /* Return a list of overlays which is a copy of the overlay list
400 LIST, but for buffer B. */
402 static struct Lisp_Overlay *
403 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
405 Lisp_Object buffer;
406 struct Lisp_Overlay *result = NULL, *tail = NULL;
408 XSETBUFFER (buffer, b);
410 for (; list; list = list->next)
412 Lisp_Object overlay, start, end, old_overlay;
413 EMACS_INT charpos;
415 XSETMISC (old_overlay, list);
416 charpos = marker_position (OVERLAY_START (old_overlay));
417 start = Fmake_marker ();
418 Fset_marker (start, make_number (charpos), buffer);
419 XMARKER (start)->insertion_type
420 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
422 charpos = marker_position (OVERLAY_END (old_overlay));
423 end = Fmake_marker ();
424 Fset_marker (end, make_number (charpos), buffer);
425 XMARKER (end)->insertion_type
426 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
428 overlay = allocate_misc ();
429 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
430 OVERLAY_START (overlay) = start;
431 OVERLAY_END (overlay) = end;
432 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
433 XOVERLAY (overlay)->next = NULL;
435 if (tail)
436 tail = tail->next = XOVERLAY (overlay);
437 else
438 result = tail = XOVERLAY (overlay);
441 return result;
445 /* Clone per-buffer values of buffer FROM.
447 Buffer TO gets the same per-buffer values as FROM, with the
448 following exceptions: (1) TO's name is left untouched, (2) markers
449 are copied and made to refer to TO, and (3) overlay lists are
450 copied. */
452 static void
453 clone_per_buffer_values (struct buffer *from, struct buffer *to)
455 Lisp_Object to_buffer;
456 int offset;
458 XSETBUFFER (to_buffer, to);
460 /* buffer-local Lisp variables start at `undo_list',
461 tho only the ones from `name' on are GC'd normally. */
462 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
463 offset < sizeof *to;
464 offset += sizeof (Lisp_Object))
466 Lisp_Object obj;
468 /* Don't touch the `name' which should be unique for every buffer. */
469 if (offset == PER_BUFFER_VAR_OFFSET (name))
470 continue;
472 obj = PER_BUFFER_VALUE (from, offset);
473 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
475 struct Lisp_Marker *m = XMARKER (obj);
476 obj = Fmake_marker ();
477 XMARKER (obj)->insertion_type = m->insertion_type;
478 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
481 PER_BUFFER_VALUE (to, offset) = obj;
484 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
486 to->overlays_before = copy_overlays (to, from->overlays_before);
487 to->overlays_after = copy_overlays (to, from->overlays_after);
489 /* Get (a copy of) the alist of Lisp-level local variables of FROM
490 and install that in TO. */
491 to->local_var_alist = buffer_lisp_local_variables (from);
494 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
495 2, 3,
496 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
497 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
498 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
499 NAME should be a string which is not the name of an existing buffer.
500 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
501 such as major and minor modes, in the indirect buffer.
502 CLONE nil means the indirect buffer's state is reset to default values. */)
503 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
505 Lisp_Object buf, tem;
506 struct buffer *b;
508 CHECK_STRING (name);
509 buf = Fget_buffer (name);
510 if (!NILP (buf))
511 error ("Buffer name `%s' is in use", SDATA (name));
513 tem = base_buffer;
514 base_buffer = Fget_buffer (base_buffer);
515 if (NILP (base_buffer))
516 error ("No such buffer: `%s'", SDATA (tem));
517 if (NILP (XBUFFER (base_buffer)->name))
518 error ("Base buffer has been killed");
520 if (SCHARS (name) == 0)
521 error ("Empty string for buffer name is not allowed");
523 b = allocate_buffer ();
525 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
526 ? XBUFFER (base_buffer)->base_buffer
527 : XBUFFER (base_buffer));
529 /* Use the base buffer's text object. */
530 b->text = b->base_buffer->text;
532 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
533 BUF_ZV (b) = BUF_ZV (b->base_buffer);
534 BUF_PT (b) = BUF_PT (b->base_buffer);
535 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
536 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
537 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
539 b->newline_cache = 0;
540 b->width_run_cache = 0;
541 b->width_table = Qnil;
543 /* Put this on the chain of all buffers including killed ones. */
544 b->next = all_buffers;
545 all_buffers = b;
547 name = Fcopy_sequence (name);
548 STRING_SET_INTERVALS (name, NULL_INTERVAL);
549 b->name = name;
551 reset_buffer (b);
552 reset_buffer_local_variables (b, 1);
554 /* Put this in the alist of all live buffers. */
555 XSETBUFFER (buf, b);
556 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
558 b->mark = Fmake_marker ();
559 b->name = name;
561 /* The multibyte status belongs to the base buffer. */
562 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
564 /* Make sure the base buffer has markers for its narrowing. */
565 if (NILP (b->base_buffer->pt_marker))
567 b->base_buffer->pt_marker = Fmake_marker ();
568 set_marker_both (b->base_buffer->pt_marker, base_buffer,
569 BUF_PT (b->base_buffer),
570 BUF_PT_BYTE (b->base_buffer));
572 if (NILP (b->base_buffer->begv_marker))
574 b->base_buffer->begv_marker = Fmake_marker ();
575 set_marker_both (b->base_buffer->begv_marker, base_buffer,
576 BUF_BEGV (b->base_buffer),
577 BUF_BEGV_BYTE (b->base_buffer));
579 if (NILP (b->base_buffer->zv_marker))
581 b->base_buffer->zv_marker = Fmake_marker ();
582 set_marker_both (b->base_buffer->zv_marker, base_buffer,
583 BUF_ZV (b->base_buffer),
584 BUF_ZV_BYTE (b->base_buffer));
585 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
588 if (NILP (clone))
590 /* Give the indirect buffer markers for its narrowing. */
591 b->pt_marker = Fmake_marker ();
592 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
593 b->begv_marker = Fmake_marker ();
594 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
595 b->zv_marker = Fmake_marker ();
596 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
597 XMARKER (b->zv_marker)->insertion_type = 1;
599 else
601 struct buffer *old_b = current_buffer;
603 clone_per_buffer_values (b->base_buffer, b);
604 b->filename = Qnil;
605 b->file_truename = Qnil;
606 b->display_count = make_number (0);
607 b->backed_up = Qnil;
608 b->auto_save_file_name = Qnil;
609 set_buffer_internal_1 (b);
610 Fset (intern ("buffer-save-without-query"), Qnil);
611 Fset (intern ("buffer-file-number"), Qnil);
612 Fset (intern ("buffer-stale-function"), Qnil);
613 set_buffer_internal_1 (old_b);
616 return buf;
619 void
620 delete_all_overlays (struct buffer *b)
622 Lisp_Object overlay;
624 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
625 have to empty the list, otherwise we end up with overlays that
626 think they belong to this buffer while the buffer doesn't know about
627 them any more. */
628 while (b->overlays_before)
630 XSETMISC (overlay, b->overlays_before);
631 Fdelete_overlay (overlay);
633 while (b->overlays_after)
635 XSETMISC (overlay, b->overlays_after);
636 Fdelete_overlay (overlay);
638 eassert (b->overlays_before == NULL);
639 eassert (b->overlays_after == NULL);
642 /* Reinitialize everything about a buffer except its name and contents
643 and local variables.
644 If called on an already-initialized buffer, the list of overlays
645 should be deleted before calling this function, otherwise we end up
646 with overlays that claim to belong to the buffer but the buffer
647 claims it doesn't belong to it. */
649 void
650 reset_buffer (register struct buffer *b)
652 b->filename = Qnil;
653 b->file_truename = Qnil;
654 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
655 b->modtime = 0;
656 b->modtime_size = -1;
657 XSETFASTINT (b->save_length, 0);
658 b->last_window_start = 1;
659 /* It is more conservative to start out "changed" than "unchanged". */
660 b->clip_changed = 0;
661 b->prevent_redisplay_optimizations_p = 1;
662 b->backed_up = Qnil;
663 BUF_AUTOSAVE_MODIFF (b) = 0;
664 b->auto_save_failure_time = -1;
665 b->auto_save_file_name = Qnil;
666 b->read_only = Qnil;
667 b->overlays_before = NULL;
668 b->overlays_after = NULL;
669 b->overlay_center = BEG;
670 b->mark_active = Qnil;
671 b->point_before_scroll = Qnil;
672 b->file_format = Qnil;
673 b->auto_save_file_format = Qt;
674 b->last_selected_window = Qnil;
675 XSETINT (b->display_count, 0);
676 b->display_time = Qnil;
677 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
678 b->cursor_type = buffer_defaults.cursor_type;
679 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
681 b->display_error_modiff = 0;
684 /* Reset buffer B's local variables info.
685 Don't use this on a buffer that has already been in use;
686 it does not treat permanent locals consistently.
687 Instead, use Fkill_all_local_variables.
689 If PERMANENT_TOO is 1, then we reset permanent
690 buffer-local variables. If PERMANENT_TOO is 0,
691 we preserve those. */
693 static void
694 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
696 register int offset;
697 int i;
699 /* Reset the major mode to Fundamental, together with all the
700 things that depend on the major mode.
701 default-major-mode is handled at a higher level.
702 We ignore it here. */
703 b->major_mode = Qfundamental_mode;
704 b->keymap = Qnil;
705 b->mode_name = QSFundamental;
706 b->minor_modes = Qnil;
708 /* If the standard case table has been altered and invalidated,
709 fix up its insides first. */
710 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
711 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
712 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
713 Fset_standard_case_table (Vascii_downcase_table);
715 b->downcase_table = Vascii_downcase_table;
716 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
717 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
718 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
719 b->invisibility_spec = Qt;
720 #ifndef DOS_NT
721 b->buffer_file_type = Qnil;
722 #endif
724 /* Reset all (or most) per-buffer variables to their defaults. */
725 if (permanent_too)
726 b->local_var_alist = Qnil;
727 else
729 Lisp_Object tmp, prop, last = Qnil;
730 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
731 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
733 /* If permanent-local, keep it. */
734 last = tmp;
735 if (EQ (prop, Qpermanent_local_hook))
737 /* This is a partially permanent hook variable.
738 Preserve only the elements that want to be preserved. */
739 Lisp_Object list, newlist;
740 list = XCDR (XCAR (tmp));
741 if (!CONSP (list))
742 newlist = list;
743 else
744 for (newlist = Qnil; CONSP (list); list = XCDR (list))
746 Lisp_Object elt = XCAR (list);
747 /* Preserve element ELT if it's t,
748 if it is a function with a `permanent-local-hook' property,
749 or if it's not a symbol. */
750 if (! SYMBOLP (elt)
751 || EQ (elt, Qt)
752 || !NILP (Fget (elt, Qpermanent_local_hook)))
753 newlist = Fcons (elt, newlist);
755 XSETCDR (XCAR (tmp), Fnreverse (newlist));
758 /* Delete this local variable. */
759 else if (NILP (last))
760 b->local_var_alist = XCDR (tmp);
761 else
762 XSETCDR (last, XCDR (tmp));
765 for (i = 0; i < last_per_buffer_idx; ++i)
766 if (permanent_too || buffer_permanent_local_flags[i] == 0)
767 SET_PER_BUFFER_VALUE_P (b, i, 0);
769 /* For each slot that has a default value,
770 copy that into the slot. */
772 /* buffer-local Lisp variables start at `undo_list',
773 tho only the ones from `name' on are GC'd normally. */
774 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
775 offset < sizeof *b;
776 offset += sizeof (Lisp_Object))
778 int idx = PER_BUFFER_IDX (offset);
779 if ((idx > 0
780 && (permanent_too
781 || buffer_permanent_local_flags[idx] == 0)))
782 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
786 /* We split this away from generate-new-buffer, because rename-buffer
787 and set-visited-file-name ought to be able to use this to really
788 rename the buffer properly. */
790 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
791 1, 2, 0,
792 doc: /* Return a string that is the name of no existing buffer based on NAME.
793 If there is no live buffer named NAME, then return NAME.
794 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
795 \(starting at 2) until an unused name is found, and then return that name.
796 Optional second argument IGNORE specifies a name that is okay to use (if
797 it is in the sequence to be tried) even if a buffer with that name exists. */)
798 (register Lisp_Object name, Lisp_Object ignore)
800 register Lisp_Object gentemp, tem;
801 int count;
802 char number[10];
804 CHECK_STRING (name);
806 tem = Fstring_equal (name, ignore);
807 if (!NILP (tem))
808 return name;
809 tem = Fget_buffer (name);
810 if (NILP (tem))
811 return name;
813 count = 1;
814 while (1)
816 sprintf (number, "<%d>", ++count);
817 gentemp = concat2 (name, build_string (number));
818 tem = Fstring_equal (gentemp, ignore);
819 if (!NILP (tem))
820 return gentemp;
821 tem = Fget_buffer (gentemp);
822 if (NILP (tem))
823 return gentemp;
828 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
829 doc: /* Return the name of BUFFER, as a string.
830 BUFFER defaults to the current buffer.
831 Return nil if BUFFER has been killed. */)
832 (register Lisp_Object buffer)
834 if (NILP (buffer))
835 return current_buffer->name;
836 CHECK_BUFFER (buffer);
837 return XBUFFER (buffer)->name;
840 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
841 doc: /* Return name of file BUFFER is visiting, or nil if none.
842 No argument or nil as argument means use the current buffer. */)
843 (register Lisp_Object buffer)
845 if (NILP (buffer))
846 return current_buffer->filename;
847 CHECK_BUFFER (buffer);
848 return XBUFFER (buffer)->filename;
851 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
852 0, 1, 0,
853 doc: /* Return the base buffer of indirect buffer BUFFER.
854 If BUFFER is not indirect, return nil.
855 BUFFER defaults to the current buffer. */)
856 (register Lisp_Object buffer)
858 struct buffer *base;
859 Lisp_Object base_buffer;
861 if (NILP (buffer))
862 base = current_buffer->base_buffer;
863 else
865 CHECK_BUFFER (buffer);
866 base = XBUFFER (buffer)->base_buffer;
869 if (! base)
870 return Qnil;
871 XSETBUFFER (base_buffer, base);
872 return base_buffer;
875 DEFUN ("buffer-local-value", Fbuffer_local_value,
876 Sbuffer_local_value, 2, 2, 0,
877 doc: /* Return the value of VARIABLE in BUFFER.
878 If VARIABLE does not have a buffer-local binding in BUFFER, the value
879 is the default binding of the variable. */)
880 (register Lisp_Object variable, register Lisp_Object buffer)
882 register struct buffer *buf;
883 register Lisp_Object result;
884 struct Lisp_Symbol *sym;
886 CHECK_SYMBOL (variable);
887 CHECK_BUFFER (buffer);
888 buf = XBUFFER (buffer);
889 sym = XSYMBOL (variable);
891 start:
892 switch (sym->redirect)
894 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
895 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
896 case SYMBOL_LOCALIZED:
897 { /* Look in local_var_alist. */
898 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
899 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
900 result = Fassoc (variable, buf->local_var_alist);
901 if (!NILP (result))
903 if (blv->fwd)
904 { /* What binding is loaded right now? */
905 Lisp_Object current_alist_element = blv->valcell;
907 /* The value of the currently loaded binding is not
908 stored in it, but rather in the realvalue slot.
909 Store that value into the binding it belongs to
910 in case that is the one we are about to use. */
912 XSETCDR (current_alist_element,
913 do_symval_forwarding (blv->fwd));
915 /* Now get the (perhaps updated) value out of the binding. */
916 result = XCDR (result);
918 else
919 result = Fdefault_value (variable);
920 break;
922 case SYMBOL_FORWARDED:
924 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
925 if (BUFFER_OBJFWDP (fwd))
926 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
927 else
928 result = Fdefault_value (variable);
929 break;
931 default: abort ();
934 if (!EQ (result, Qunbound))
935 return result;
937 xsignal1 (Qvoid_variable, variable);
940 /* Return an alist of the Lisp-level buffer-local bindings of
941 buffer BUF. That is, don't include the variables maintained
942 in special slots in the buffer object. */
944 static Lisp_Object
945 buffer_lisp_local_variables (struct buffer *buf)
947 Lisp_Object result = Qnil;
948 register Lisp_Object tail;
949 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
951 Lisp_Object val, elt;
953 elt = XCAR (tail);
955 /* Reference each variable in the alist in buf.
956 If inquiring about the current buffer, this gets the current values,
957 so store them into the alist so the alist is up to date.
958 If inquiring about some other buffer, this swaps out any values
959 for that buffer, making the alist up to date automatically. */
960 val = find_symbol_value (XCAR (elt));
961 /* Use the current buffer value only if buf is the current buffer. */
962 if (buf != current_buffer)
963 val = XCDR (elt);
965 result = Fcons (Fcons (XCAR (elt), val), result);
968 return result;
971 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
972 Sbuffer_local_variables, 0, 1, 0,
973 doc: /* Return an alist of variables that are buffer-local in BUFFER.
974 Most elements look like (SYMBOL . VALUE), describing one variable.
975 For a symbol that is locally unbound, just the symbol appears in the value.
976 Note that storing new VALUEs in these elements doesn't change the variables.
977 No argument or nil as argument means use current buffer as BUFFER. */)
978 (register Lisp_Object buffer)
980 register struct buffer *buf;
981 register Lisp_Object result;
983 if (NILP (buffer))
984 buf = current_buffer;
985 else
987 CHECK_BUFFER (buffer);
988 buf = XBUFFER (buffer);
991 result = buffer_lisp_local_variables (buf);
993 /* Add on all the variables stored in special slots. */
995 int offset, idx;
997 /* buffer-local Lisp variables start at `undo_list',
998 tho only the ones from `name' on are GC'd normally. */
999 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1000 offset < sizeof (struct buffer);
1001 /* sizeof EMACS_INT == sizeof Lisp_Object */
1002 offset += (sizeof (EMACS_INT)))
1004 idx = PER_BUFFER_IDX (offset);
1005 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1006 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1007 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1008 PER_BUFFER_VALUE (buf, offset)),
1009 result);
1013 return result;
1016 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1017 0, 1, 0,
1018 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1019 No argument or nil as argument means use current buffer as BUFFER. */)
1020 (register Lisp_Object buffer)
1022 register struct buffer *buf;
1023 if (NILP (buffer))
1024 buf = current_buffer;
1025 else
1027 CHECK_BUFFER (buffer);
1028 buf = XBUFFER (buffer);
1031 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1034 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1035 1, 1, 0,
1036 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1037 A non-nil FLAG means mark the buffer modified. */)
1038 (register Lisp_Object flag)
1040 register int already;
1041 register Lisp_Object fn;
1042 Lisp_Object buffer, window;
1044 #ifdef CLASH_DETECTION
1045 /* If buffer becoming modified, lock the file.
1046 If buffer becoming unmodified, unlock the file. */
1048 fn = current_buffer->file_truename;
1049 /* Test buffer-file-name so that binding it to nil is effective. */
1050 if (!NILP (fn) && ! NILP (current_buffer->filename))
1052 already = SAVE_MODIFF < MODIFF;
1053 if (!already && !NILP (flag))
1054 lock_file (fn);
1055 else if (already && NILP (flag))
1056 unlock_file (fn);
1058 #endif /* CLASH_DETECTION */
1060 /* Here we have a problem. SAVE_MODIFF is used here to encode
1061 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1062 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1063 modify SAVE_MODIFF to affect one, we may affect the other
1064 as well.
1065 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1066 if SAVE_MODIFF<auto_save_modified that means we risk changing
1067 recent-auto-save-p from t to nil.
1068 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1069 we risk changing recent-auto-save-p from nil to t. */
1070 SAVE_MODIFF = (NILP (flag)
1071 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1072 ? MODIFF
1073 /* Let's try to preserve recent-auto-save-p. */
1074 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1075 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1076 we can either decrease SAVE_MODIFF and auto_save_modified
1077 or increase MODIFF. */
1078 : MODIFF++);
1080 /* Set update_mode_lines only if buffer is displayed in some window.
1081 Packages like jit-lock or lazy-lock preserve a buffer's modified
1082 state by recording/restoring the state around blocks of code.
1083 Setting update_mode_lines makes redisplay consider all windows
1084 (on all frames). Stealth fontification of buffers not displayed
1085 would incur additional redisplay costs if we'd set
1086 update_modes_lines unconditionally.
1088 Ideally, I think there should be another mechanism for fontifying
1089 buffers without "modifying" buffers, or redisplay should be
1090 smarter about updating the `*' in mode lines. --gerd */
1091 XSETBUFFER (buffer, current_buffer);
1092 window = Fget_buffer_window (buffer, Qt);
1093 if (WINDOWP (window))
1095 ++update_mode_lines;
1096 current_buffer->prevent_redisplay_optimizations_p = 1;
1099 return flag;
1102 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1103 Srestore_buffer_modified_p, 1, 1, 0,
1104 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1105 It is not ensured that mode lines will be updated to show the modified
1106 state of the current buffer. Use with care. */)
1107 (Lisp_Object flag)
1109 #ifdef CLASH_DETECTION
1110 Lisp_Object fn;
1112 /* If buffer becoming modified, lock the file.
1113 If buffer becoming unmodified, unlock the file. */
1115 fn = current_buffer->file_truename;
1116 /* Test buffer-file-name so that binding it to nil is effective. */
1117 if (!NILP (fn) && ! NILP (current_buffer->filename))
1119 int already = SAVE_MODIFF < MODIFF;
1120 if (!already && !NILP (flag))
1121 lock_file (fn);
1122 else if (already && NILP (flag))
1123 unlock_file (fn);
1125 #endif /* CLASH_DETECTION */
1127 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1128 return flag;
1131 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1132 0, 1, 0,
1133 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1134 Each buffer has a tick counter which is incremented each time the
1135 text in that buffer is changed. It wraps around occasionally.
1136 No argument or nil as argument means use current buffer as BUFFER. */)
1137 (register Lisp_Object buffer)
1139 register struct buffer *buf;
1140 if (NILP (buffer))
1141 buf = current_buffer;
1142 else
1144 CHECK_BUFFER (buffer);
1145 buf = XBUFFER (buffer);
1148 return make_number (BUF_MODIFF (buf));
1151 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1152 Sbuffer_chars_modified_tick, 0, 1, 0,
1153 doc: /* Return BUFFER's character-change tick counter.
1154 Each buffer has a character-change tick counter, which is set to the
1155 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1156 time text in that buffer is inserted or deleted. By comparing the
1157 values returned by two individual calls of `buffer-chars-modified-tick',
1158 you can tell whether a character change occurred in that buffer in
1159 between these calls. No argument or nil as argument means use current
1160 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_CHARS_MODIFF (buf));
1175 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1176 "(list (read-string \"Rename buffer (to new name): \" \
1177 nil 'buffer-name-history (buffer-name (current-buffer))) \
1178 current-prefix-arg)",
1179 doc: /* Change current buffer's name to NEWNAME (a string).
1180 If second arg UNIQUE is nil or omitted, it is an error if a
1181 buffer named NEWNAME already exists.
1182 If UNIQUE is non-nil, come up with a new name using
1183 `generate-new-buffer-name'.
1184 Interactively, you can set UNIQUE with a prefix argument.
1185 We return the name we actually gave the buffer.
1186 This does not change the name of the visited file (if any). */)
1187 (register Lisp_Object newname, Lisp_Object unique)
1189 register Lisp_Object tem, buf;
1191 CHECK_STRING (newname);
1193 if (SCHARS (newname) == 0)
1194 error ("Empty string is invalid as a buffer name");
1196 tem = Fget_buffer (newname);
1197 if (!NILP (tem))
1199 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1200 rename the buffer automatically so you can create another
1201 with the original name. It makes UNIQUE equivalent to
1202 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1203 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1204 return current_buffer->name;
1205 if (!NILP (unique))
1206 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1207 else
1208 error ("Buffer name `%s' is in use", SDATA (newname));
1211 current_buffer->name = newname;
1213 /* Catch redisplay's attention. Unless we do this, the mode lines for
1214 any windows displaying current_buffer will stay unchanged. */
1215 update_mode_lines++;
1217 XSETBUFFER (buf, current_buffer);
1218 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1219 if (NILP (current_buffer->filename)
1220 && !NILP (current_buffer->auto_save_file_name))
1221 call0 (intern ("rename-auto-save-file"));
1222 /* Refetch since that last call may have done GC. */
1223 return current_buffer->name;
1226 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1227 doc: /* Return most recently selected buffer other than BUFFER.
1228 Buffers not visible in windows are preferred to visible buffers,
1229 unless optional second argument VISIBLE-OK is non-nil.
1230 If the optional third argument FRAME is non-nil, use that frame's
1231 buffer list instead of the selected frame's buffer list.
1232 If no other buffer exists, the buffer `*scratch*' is returned.
1233 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1234 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1236 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1237 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1238 notsogood = Qnil;
1240 if (NILP (frame))
1241 frame = selected_frame;
1243 CHECK_FRAME (frame);
1245 tail = Vbuffer_alist;
1246 pred = frame_buffer_predicate (frame);
1248 /* Consider buffers that have been seen in the selected frame
1249 before other buffers. */
1251 tem = frame_buffer_list (frame);
1252 add_ons = Qnil;
1253 while (CONSP (tem))
1255 if (BUFFERP (XCAR (tem)))
1256 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1257 tem = XCDR (tem);
1259 tail = nconc2 (Fnreverse (add_ons), tail);
1261 for (; CONSP (tail); tail = XCDR (tail))
1263 buf = Fcdr (XCAR (tail));
1264 if (EQ (buf, buffer))
1265 continue;
1266 if (NILP (buf))
1267 continue;
1268 if (NILP (XBUFFER (buf)->name))
1269 continue;
1270 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1271 continue;
1272 /* If the selected frame has a buffer_predicate,
1273 disregard buffers that don't fit the predicate. */
1274 if (!NILP (pred))
1276 tem = call1 (pred, buf);
1277 if (NILP (tem))
1278 continue;
1281 if (NILP (visible_ok))
1282 tem = Fget_buffer_window (buf, Qvisible);
1283 else
1284 tem = Qnil;
1285 if (NILP (tem))
1286 return buf;
1287 if (NILP (notsogood))
1288 notsogood = buf;
1290 if (!NILP (notsogood))
1291 return notsogood;
1292 buf = Fget_buffer (build_string ("*scratch*"));
1293 if (NILP (buf))
1295 buf = Fget_buffer_create (build_string ("*scratch*"));
1296 Fset_buffer_major_mode (buf);
1298 return buf;
1301 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1302 0, 1, "",
1303 doc: /* Start keeping undo information for buffer BUFFER.
1304 No argument or nil as argument means do this for the current buffer. */)
1305 (register Lisp_Object buffer)
1307 Lisp_Object real_buffer;
1309 if (NILP (buffer))
1310 XSETBUFFER (real_buffer, current_buffer);
1311 else
1313 real_buffer = Fget_buffer (buffer);
1314 if (NILP (real_buffer))
1315 nsberror (buffer);
1318 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1319 XBUFFER (real_buffer)->undo_list = Qnil;
1321 return Qnil;
1325 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1326 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1327 The buffer being killed will be current while the hook is running.\n\
1328 See `kill-buffer'."
1330 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1331 doc: /* Kill buffer BUFFER-OR-NAME.
1332 The argument may be a buffer or the name of an existing buffer.
1333 Argument nil or omitted means kill the current buffer. Return t if the
1334 buffer is actually killed, nil otherwise.
1336 This function calls `replace-buffer-in-windows' for cleaning up all
1337 windows currently displaying the buffer to be killed. The functions in
1338 `kill-buffer-query-functions' are called with the buffer to be killed as
1339 the current buffer. If any of them returns nil, the buffer is not
1340 killed. The hook `kill-buffer-hook' is run before the buffer is
1341 actually killed. The buffer being killed will be current while the hook
1342 is running.
1344 Any processes that have this buffer as the `process-buffer' are killed
1345 with SIGHUP. */)
1346 (Lisp_Object buffer_or_name)
1348 Lisp_Object buffer;
1349 register struct buffer *b;
1350 register Lisp_Object tem;
1351 register struct Lisp_Marker *m;
1352 struct gcpro gcpro1;
1354 if (NILP (buffer_or_name))
1355 buffer = Fcurrent_buffer ();
1356 else
1357 buffer = Fget_buffer (buffer_or_name);
1358 if (NILP (buffer))
1359 nsberror (buffer_or_name);
1361 b = XBUFFER (buffer);
1363 /* Avoid trouble for buffer already dead. */
1364 if (NILP (b->name))
1365 return Qnil;
1367 /* Query if the buffer is still modified. */
1368 if (INTERACTIVE && !NILP (b->filename)
1369 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1371 GCPRO1 (buffer);
1372 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1373 b->name, make_number (0)));
1374 UNGCPRO;
1375 if (NILP (tem))
1376 return Qnil;
1379 /* Run hooks with the buffer to be killed the current buffer. */
1381 int count = SPECPDL_INDEX ();
1382 Lisp_Object arglist[1];
1384 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1385 set_buffer_internal (b);
1387 /* First run the query functions; if any query is answered no,
1388 don't kill the buffer. */
1389 arglist[0] = Qkill_buffer_query_functions;
1390 tem = Frun_hook_with_args_until_failure (1, arglist);
1391 if (NILP (tem))
1392 return unbind_to (count, Qnil);
1394 /* Then run the hooks. */
1395 Frun_hooks (1, &Qkill_buffer_hook);
1396 unbind_to (count, Qnil);
1399 /* We have no more questions to ask. Verify that it is valid
1400 to kill the buffer. This must be done after the questions
1401 since anything can happen within do_yes_or_no_p. */
1403 /* Don't kill the minibuffer now current. */
1404 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1405 return Qnil;
1407 if (NILP (b->name))
1408 return Qnil;
1410 /* When we kill a base buffer, kill all its indirect buffers.
1411 We do it at this stage so nothing terrible happens if they
1412 ask questions or their hooks get errors. */
1413 if (! b->base_buffer)
1415 struct buffer *other;
1417 GCPRO1 (buffer);
1419 for (other = all_buffers; other; other = other->next)
1420 /* all_buffers contains dead buffers too;
1421 don't re-kill them. */
1422 if (other->base_buffer == b && !NILP (other->name))
1424 Lisp_Object buffer;
1425 XSETBUFFER (buffer, other);
1426 Fkill_buffer (buffer);
1429 UNGCPRO;
1432 /* Make this buffer not be current.
1433 In the process, notice if this is the sole visible buffer
1434 and give up if so. */
1435 if (b == current_buffer)
1437 tem = Fother_buffer (buffer, Qnil, Qnil);
1438 Fset_buffer (tem);
1439 if (b == current_buffer)
1440 return Qnil;
1443 /* Notice if the buffer to kill is the sole visible buffer
1444 when we're currently in the mini-buffer, and give up if so. */
1445 XSETBUFFER (tem, current_buffer);
1446 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1448 tem = Fother_buffer (buffer, Qnil, Qnil);
1449 if (EQ (buffer, tem))
1450 return Qnil;
1453 /* Now there is no question: we can kill the buffer. */
1455 #ifdef CLASH_DETECTION
1456 /* Unlock this buffer's file, if it is locked. */
1457 unlock_buffer (b);
1458 #endif /* CLASH_DETECTION */
1460 GCPRO1 (buffer);
1461 kill_buffer_processes (buffer);
1462 UNGCPRO;
1464 /* Killing buffer processes may run sentinels which may
1465 have called kill-buffer. */
1467 if (NILP (b->name))
1468 return Qnil;
1470 clear_charpos_cache (b);
1472 tem = Vinhibit_quit;
1473 Vinhibit_quit = Qt;
1474 replace_buffer_in_all_windows (buffer);
1475 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1476 frames_discard_buffer (buffer);
1477 Vinhibit_quit = tem;
1479 /* Delete any auto-save file, if we saved it in this session.
1480 But not if the buffer is modified. */
1481 if (STRINGP (b->auto_save_file_name)
1482 && BUF_AUTOSAVE_MODIFF (b) != 0
1483 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1484 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1485 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1487 Lisp_Object tem;
1488 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1489 if (! NILP (tem))
1490 internal_delete_file (b->auto_save_file_name);
1493 if (b->base_buffer)
1495 /* Unchain all markers that belong to this indirect buffer.
1496 Don't unchain the markers that belong to the base buffer
1497 or its other indirect buffers. */
1498 for (m = BUF_MARKERS (b); m; )
1500 struct Lisp_Marker *next = m->next;
1501 if (m->buffer == b)
1502 unchain_marker (m);
1503 m = next;
1506 else
1508 /* Unchain all markers of this buffer and its indirect buffers.
1509 and leave them pointing nowhere. */
1510 for (m = BUF_MARKERS (b); m; )
1512 struct Lisp_Marker *next = m->next;
1513 m->buffer = 0;
1514 m->next = NULL;
1515 m = next;
1517 BUF_MARKERS (b) = NULL;
1518 BUF_INTERVALS (b) = NULL_INTERVAL;
1520 /* Perhaps we should explicitly free the interval tree here... */
1523 /* Reset the local variables, so that this buffer's local values
1524 won't be protected from GC. They would be protected
1525 if they happened to remain encached in their symbols.
1526 This gets rid of them for certain. */
1527 swap_out_buffer_local_variables (b);
1528 reset_buffer_local_variables (b, 1);
1530 b->name = Qnil;
1532 BLOCK_INPUT;
1533 if (! b->base_buffer)
1534 free_buffer_text (b);
1536 if (b->newline_cache)
1538 free_region_cache (b->newline_cache);
1539 b->newline_cache = 0;
1541 if (b->width_run_cache)
1543 free_region_cache (b->width_run_cache);
1544 b->width_run_cache = 0;
1546 b->width_table = Qnil;
1547 UNBLOCK_INPUT;
1548 b->undo_list = Qnil;
1550 return Qt;
1553 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1554 we do this each time BUF is selected visibly, the more recently
1555 selected buffers are always closer to the front of the list. This
1556 means that other_buffer is more likely to choose a relevant buffer. */
1558 void
1559 record_buffer (Lisp_Object buf)
1561 register Lisp_Object link, prev;
1562 Lisp_Object frame;
1563 frame = selected_frame;
1565 prev = Qnil;
1566 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1568 if (EQ (XCDR (XCAR (link)), buf))
1569 break;
1570 prev = link;
1573 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1574 we cannot use Fdelq itself here because it allows quitting. */
1576 if (NILP (prev))
1577 Vbuffer_alist = XCDR (Vbuffer_alist);
1578 else
1579 XSETCDR (prev, XCDR (XCDR (prev)));
1581 XSETCDR (link, Vbuffer_alist);
1582 Vbuffer_alist = link;
1584 /* Effectively do a delq on buried_buffer_list. */
1586 prev = Qnil;
1587 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1588 link = XCDR (link))
1590 if (EQ (XCAR (link), buf))
1592 if (NILP (prev))
1593 XFRAME (frame)->buried_buffer_list = XCDR (link);
1594 else
1595 XSETCDR (prev, XCDR (XCDR (prev)));
1596 break;
1598 prev = link;
1601 /* Now move this buffer to the front of frame_buffer_list also. */
1603 prev = Qnil;
1604 for (link = frame_buffer_list (frame); CONSP (link);
1605 link = XCDR (link))
1607 if (EQ (XCAR (link), buf))
1608 break;
1609 prev = link;
1612 /* Effectively do delq. */
1614 if (CONSP (link))
1616 if (NILP (prev))
1617 set_frame_buffer_list (frame,
1618 XCDR (frame_buffer_list (frame)));
1619 else
1620 XSETCDR (prev, XCDR (XCDR (prev)));
1622 XSETCDR (link, frame_buffer_list (frame));
1623 set_frame_buffer_list (frame, link);
1625 else
1626 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1629 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1630 doc: /* Set an appropriate major mode for BUFFER.
1631 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1632 according to `default-major-mode'.
1633 Use this function before selecting the buffer, since it may need to inspect
1634 the current buffer's major mode. */)
1635 (Lisp_Object buffer)
1637 int count;
1638 Lisp_Object function;
1640 CHECK_BUFFER (buffer);
1642 if (STRINGP (XBUFFER (buffer)->name)
1643 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1644 function = find_symbol_value (intern ("initial-major-mode"));
1645 else
1647 function = buffer_defaults.major_mode;
1648 if (NILP (function)
1649 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1650 function = current_buffer->major_mode;
1653 if (NILP (function) || EQ (function, Qfundamental_mode))
1654 return Qnil;
1656 count = SPECPDL_INDEX ();
1658 /* To select a nonfundamental mode,
1659 select the buffer temporarily and then call the mode function. */
1661 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1663 Fset_buffer (buffer);
1664 call0 (function);
1666 return unbind_to (count, Qnil);
1669 /* Switch to buffer BUFFER in the selected window.
1670 If NORECORD is non-nil, don't call record_buffer. */
1672 Lisp_Object
1673 switch_to_buffer_1 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1675 register Lisp_Object buffer;
1677 if (NILP (buffer_or_name))
1678 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1679 else
1681 buffer = Fget_buffer (buffer_or_name);
1682 if (NILP (buffer))
1684 buffer = Fget_buffer_create (buffer_or_name);
1685 Fset_buffer_major_mode (buffer);
1688 Fset_buffer (buffer);
1689 if (NILP (norecord))
1690 record_buffer (buffer);
1692 Fset_window_buffer (EQ (selected_window, minibuf_window)
1693 ? Fnext_window (minibuf_window, Qnil, Qnil)
1694 : selected_window,
1695 buffer, Qnil);
1697 return buffer;
1700 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1701 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1702 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1703 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1704 nil. Return the buffer switched to.
1706 If BUFFER-OR-NAME is a string and does not identify an existing
1707 buffer, create a new buffer with that name. Interactively, if
1708 `confirm-nonexistent-file-or-buffer' is non-nil, request
1709 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1710 nil, switch to buffer returned by `other-buffer'.
1712 Optional second arg NORECORD non-nil means do not put this buffer
1713 at the front of the list of recently selected ones. This
1714 function returns the buffer it switched to as a Lisp object.
1716 If the selected window is the minibuffer window or dedicated to
1717 its buffer, use `pop-to-buffer' for displaying the buffer.
1719 WARNING: This is NOT the way to work on another buffer temporarily
1720 within a Lisp program! Use `set-buffer' instead. That avoids
1721 messing with the window-buffer correspondences. */)
1722 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1724 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1726 /* Basically a NOP. Avoid signalling an error in the case where
1727 the selected window is dedicated, or a minibuffer. */
1729 /* But do put this buffer at the front of the buffer list, unless
1730 that has been inhibited. Note that even if BUFFER-OR-NAME is
1731 at the front of the main buffer-list already, we still want to
1732 move it to the front of the frame's buffer list. */
1733 if (NILP (norecord))
1734 record_buffer (buffer_or_name);
1735 return Fset_buffer (buffer_or_name);
1737 else if (EQ (minibuf_window, selected_window)
1738 /* If `dedicated' is neither nil nor t, it means it's
1739 dedicatedness can be overridden by an explicit request
1740 such as a call to switch-to-buffer. */
1741 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1742 /* We can't use the selected window so let `pop-to-buffer' try some
1743 other window. */
1744 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1745 else
1746 return switch_to_buffer_1 (buffer_or_name, norecord);
1749 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1750 doc: /* Return the current buffer as a Lisp object. */)
1751 (void)
1753 register Lisp_Object buf;
1754 XSETBUFFER (buf, current_buffer);
1755 return buf;
1758 /* Set the current buffer to B.
1760 We previously set windows_or_buffers_changed here to invalidate
1761 global unchanged information in beg_unchanged and end_unchanged.
1762 This is no longer necessary because we now compute unchanged
1763 information on a buffer-basis. Every action affecting other
1764 windows than the selected one requires a select_window at some
1765 time, and that increments windows_or_buffers_changed. */
1767 void
1768 set_buffer_internal (register struct buffer *b)
1770 if (current_buffer != b)
1771 set_buffer_internal_1 (b);
1774 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1775 This is used by redisplay. */
1777 void
1778 set_buffer_internal_1 (register struct buffer *b)
1780 register struct buffer *old_buf;
1781 register Lisp_Object tail;
1783 #ifdef USE_MMAP_FOR_BUFFERS
1784 if (b->text->beg == NULL)
1785 enlarge_buffer_text (b, 0);
1786 #endif /* USE_MMAP_FOR_BUFFERS */
1788 if (current_buffer == b)
1789 return;
1791 old_buf = current_buffer;
1792 current_buffer = b;
1793 last_known_column_point = -1; /* invalidate indentation cache */
1795 if (old_buf)
1797 /* Put the undo list back in the base buffer, so that it appears
1798 that an indirect buffer shares the undo list of its base. */
1799 if (old_buf->base_buffer)
1800 old_buf->base_buffer->undo_list = old_buf->undo_list;
1802 /* If the old current buffer has markers to record PT, BEGV and ZV
1803 when it is not current, update them now. */
1804 if (! NILP (old_buf->pt_marker))
1806 Lisp_Object obuf;
1807 XSETBUFFER (obuf, old_buf);
1808 set_marker_both (old_buf->pt_marker, obuf,
1809 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1811 if (! NILP (old_buf->begv_marker))
1813 Lisp_Object obuf;
1814 XSETBUFFER (obuf, old_buf);
1815 set_marker_both (old_buf->begv_marker, obuf,
1816 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1818 if (! NILP (old_buf->zv_marker))
1820 Lisp_Object obuf;
1821 XSETBUFFER (obuf, old_buf);
1822 set_marker_both (old_buf->zv_marker, obuf,
1823 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1827 /* Get the undo list from the base buffer, so that it appears
1828 that an indirect buffer shares the undo list of its base. */
1829 if (b->base_buffer)
1830 b->undo_list = b->base_buffer->undo_list;
1832 /* If the new current buffer has markers to record PT, BEGV and ZV
1833 when it is not current, fetch them now. */
1834 if (! NILP (b->pt_marker))
1836 BUF_PT (b) = marker_position (b->pt_marker);
1837 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1839 if (! NILP (b->begv_marker))
1841 BUF_BEGV (b) = marker_position (b->begv_marker);
1842 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1844 if (! NILP (b->zv_marker))
1846 BUF_ZV (b) = marker_position (b->zv_marker);
1847 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1850 /* Look down buffer's list of local Lisp variables
1851 to find and update any that forward into C variables. */
1855 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1857 Lisp_Object var = XCAR (XCAR (tail));
1858 struct Lisp_Symbol *sym = XSYMBOL (var);
1859 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1860 && SYMBOL_BLV (sym)->fwd)
1861 /* Just reference the variable
1862 to cause it to become set for this buffer. */
1863 Fsymbol_value (var);
1866 /* Do the same with any others that were local to the previous buffer */
1867 while (b != old_buf && (b = old_buf, b));
1870 /* Switch to buffer B temporarily for redisplay purposes.
1871 This avoids certain things that don't need to be done within redisplay. */
1873 void
1874 set_buffer_temp (struct buffer *b)
1876 register struct buffer *old_buf;
1878 if (current_buffer == b)
1879 return;
1881 old_buf = current_buffer;
1882 current_buffer = b;
1884 if (old_buf)
1886 /* If the old current buffer has markers to record PT, BEGV and ZV
1887 when it is not current, update them now. */
1888 if (! NILP (old_buf->pt_marker))
1890 Lisp_Object obuf;
1891 XSETBUFFER (obuf, old_buf);
1892 set_marker_both (old_buf->pt_marker, obuf,
1893 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1895 if (! NILP (old_buf->begv_marker))
1897 Lisp_Object obuf;
1898 XSETBUFFER (obuf, old_buf);
1899 set_marker_both (old_buf->begv_marker, obuf,
1900 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1902 if (! NILP (old_buf->zv_marker))
1904 Lisp_Object obuf;
1905 XSETBUFFER (obuf, old_buf);
1906 set_marker_both (old_buf->zv_marker, obuf,
1907 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1911 /* If the new current buffer has markers to record PT, BEGV and ZV
1912 when it is not current, fetch them now. */
1913 if (! NILP (b->pt_marker))
1915 BUF_PT (b) = marker_position (b->pt_marker);
1916 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1918 if (! NILP (b->begv_marker))
1920 BUF_BEGV (b) = marker_position (b->begv_marker);
1921 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1923 if (! NILP (b->zv_marker))
1925 BUF_ZV (b) = marker_position (b->zv_marker);
1926 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1930 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1931 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1932 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1933 also `save-excursion' when you want to make a buffer current
1934 temporarily. This function does not display the buffer, so its effect
1935 ends when the current command terminates. Use `switch-to-buffer' or
1936 `pop-to-buffer' to switch buffers permanently. */)
1937 (register Lisp_Object buffer_or_name)
1939 register Lisp_Object buffer;
1940 buffer = Fget_buffer (buffer_or_name);
1941 if (NILP (buffer))
1942 nsberror (buffer_or_name);
1943 if (NILP (XBUFFER (buffer)->name))
1944 error ("Selecting deleted buffer");
1945 set_buffer_internal (XBUFFER (buffer));
1946 return buffer;
1949 /* Set the current buffer to BUFFER provided it is alive. */
1951 Lisp_Object
1952 set_buffer_if_live (Lisp_Object buffer)
1954 if (! NILP (XBUFFER (buffer)->name))
1955 Fset_buffer (buffer);
1956 return Qnil;
1959 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1960 Sbarf_if_buffer_read_only, 0, 0, 0,
1961 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1962 (void)
1964 if (!NILP (current_buffer->read_only)
1965 && NILP (Vinhibit_read_only))
1966 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1967 return Qnil;
1970 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1971 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
1972 There it is the least likely candidate for `other-buffer' to return;
1973 thus, the least likely buffer for \\[switch-to-buffer] to select by
1974 default.
1976 The argument may be a buffer name or an actual buffer object. If
1977 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
1978 from the selected window if it is displayed there. If the selected
1979 window is dedicated to its buffer, delete that window if there are other
1980 windows on the same frame. If the selected window is the only window on
1981 its frame, iconify that frame. */)
1982 (register Lisp_Object buffer_or_name)
1984 Lisp_Object buffer;
1986 /* Figure out what buffer we're going to bury. */
1987 if (NILP (buffer_or_name))
1989 Lisp_Object tem;
1990 XSETBUFFER (buffer, current_buffer);
1992 tem = Fwindow_buffer (selected_window);
1993 /* If we're burying the current buffer, unshow it. */
1994 if (EQ (buffer, tem))
1996 if (NILP (Fwindow_dedicated_p (selected_window)))
1997 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1998 else if (NILP (XWINDOW (selected_window)->parent))
1999 Ficonify_frame (Fwindow_frame (selected_window));
2000 else
2001 Fdelete_window (selected_window);
2004 else
2006 buffer = Fget_buffer (buffer_or_name);
2007 if (NILP (buffer))
2008 nsberror (buffer_or_name);
2011 /* Move buffer to the end of the buffer list. Do nothing if the
2012 buffer is killed. */
2013 if (!NILP (XBUFFER (buffer)->name))
2015 Lisp_Object aelt, link;
2017 aelt = Frassq (buffer, Vbuffer_alist);
2018 link = Fmemq (aelt, Vbuffer_alist);
2019 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2020 XSETCDR (link, Qnil);
2021 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2023 XFRAME (selected_frame)->buffer_list
2024 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2025 XFRAME (selected_frame)->buried_buffer_list
2026 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2029 return Qnil;
2032 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2033 doc: /* Delete the entire contents of the current buffer.
2034 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2035 so the buffer is truly empty after this. */)
2036 (void)
2038 Fwiden ();
2040 del_range (BEG, Z);
2042 current_buffer->last_window_start = 1;
2043 /* Prevent warnings, or suspension of auto saving, that would happen
2044 if future size is less than past size. Use of erase-buffer
2045 implies that the future text is not really related to the past text. */
2046 XSETFASTINT (current_buffer->save_length, 0);
2047 return Qnil;
2050 void
2051 validate_region (register Lisp_Object *b, register Lisp_Object *e)
2053 CHECK_NUMBER_COERCE_MARKER (*b);
2054 CHECK_NUMBER_COERCE_MARKER (*e);
2056 if (XINT (*b) > XINT (*e))
2058 Lisp_Object tem;
2059 tem = *b; *b = *e; *e = tem;
2062 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2063 && XINT (*e) <= ZV))
2064 args_out_of_range (*b, *e);
2067 /* Advance BYTE_POS up to a character boundary
2068 and return the adjusted position. */
2070 static int
2071 advance_to_char_boundary (EMACS_INT byte_pos)
2073 int c;
2075 if (byte_pos == BEG)
2076 /* Beginning of buffer is always a character boundary. */
2077 return BEG;
2079 c = FETCH_BYTE (byte_pos);
2080 if (! CHAR_HEAD_P (c))
2082 /* We should advance BYTE_POS only when C is a constituent of a
2083 multibyte sequence. */
2084 EMACS_INT orig_byte_pos = byte_pos;
2088 byte_pos--;
2089 c = FETCH_BYTE (byte_pos);
2091 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2092 INC_POS (byte_pos);
2093 if (byte_pos < orig_byte_pos)
2094 byte_pos = orig_byte_pos;
2095 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2096 surely advance to the correct character boundary. If C is
2097 not, BYTE_POS was unchanged. */
2100 return byte_pos;
2103 #ifdef REL_ALLOC
2104 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2105 #endif /* REL_ALLOC */
2107 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2108 1, 1, 0,
2109 doc: /* Swap the text between current buffer and BUFFER. */)
2110 (Lisp_Object buffer)
2112 struct buffer *other_buffer;
2113 CHECK_BUFFER (buffer);
2114 other_buffer = XBUFFER (buffer);
2116 if (NILP (other_buffer->name))
2117 error ("Cannot swap a dead buffer's text");
2119 /* Actually, it probably works just fine.
2120 * if (other_buffer == current_buffer)
2121 * error ("Cannot swap a buffer's text with itself"); */
2123 /* Actually, this may be workable as well, tho probably only if they're
2124 *both* indirect. */
2125 if (other_buffer->base_buffer
2126 || current_buffer->base_buffer)
2127 error ("Cannot swap indirect buffers's text");
2129 { /* This is probably harder to make work. */
2130 struct buffer *other;
2131 for (other = all_buffers; other; other = other->next)
2132 if (other->base_buffer == other_buffer
2133 || other->base_buffer == current_buffer)
2134 error ("One of the buffers to swap has indirect buffers");
2137 #define swapfield(field, type) \
2138 do { \
2139 type tmp##field = other_buffer->field; \
2140 other_buffer->field = current_buffer->field; \
2141 current_buffer->field = tmp##field; \
2142 } while (0)
2144 swapfield (own_text, struct buffer_text);
2145 eassert (current_buffer->text == &current_buffer->own_text);
2146 eassert (other_buffer->text == &other_buffer->own_text);
2147 #ifdef REL_ALLOC
2148 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2149 (POINTER_TYPE **) &other_buffer->own_text.beg);
2150 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2151 (POINTER_TYPE **) &current_buffer->own_text.beg);
2152 #endif /* REL_ALLOC */
2154 swapfield (pt, EMACS_INT);
2155 swapfield (pt_byte, EMACS_INT);
2156 swapfield (begv, EMACS_INT);
2157 swapfield (begv_byte, EMACS_INT);
2158 swapfield (zv, EMACS_INT);
2159 swapfield (zv_byte, EMACS_INT);
2160 eassert (!current_buffer->base_buffer);
2161 eassert (!other_buffer->base_buffer);
2162 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2163 swapfield (newline_cache, struct region_cache *);
2164 swapfield (width_run_cache, struct region_cache *);
2165 current_buffer->prevent_redisplay_optimizations_p = 1;
2166 other_buffer->prevent_redisplay_optimizations_p = 1;
2167 swapfield (overlays_before, struct Lisp_Overlay *);
2168 swapfield (overlays_after, struct Lisp_Overlay *);
2169 swapfield (overlay_center, EMACS_INT);
2170 swapfield (undo_list, Lisp_Object);
2171 swapfield (mark, Lisp_Object);
2172 swapfield (enable_multibyte_characters, Lisp_Object);
2173 swapfield (bidi_display_reordering, Lisp_Object);
2174 swapfield (bidi_paragraph_direction, Lisp_Object);
2175 /* FIXME: Not sure what we should do with these *_marker fields.
2176 Hopefully they're just nil anyway. */
2177 swapfield (pt_marker, Lisp_Object);
2178 swapfield (begv_marker, Lisp_Object);
2179 swapfield (zv_marker, Lisp_Object);
2180 current_buffer->point_before_scroll = Qnil;
2181 other_buffer->point_before_scroll = Qnil;
2183 current_buffer->text->modiff++; other_buffer->text->modiff++;
2184 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2185 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2186 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2187 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2188 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2189 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2191 struct Lisp_Marker *m;
2192 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2193 if (m->buffer == other_buffer)
2194 m->buffer = current_buffer;
2195 else
2196 /* Since there's no indirect buffer in sight, markers on
2197 BUF_MARKERS(buf) should either be for `buf' or dead. */
2198 eassert (!m->buffer);
2199 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2200 if (m->buffer == current_buffer)
2201 m->buffer = other_buffer;
2202 else
2203 /* Since there's no indirect buffer in sight, markers on
2204 BUF_MARKERS(buf) should either be for `buf' or dead. */
2205 eassert (!m->buffer);
2207 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2208 So since we just swapped the markers between the two buffers, we need
2209 to undo the effect of this swap for window markers. */
2210 Lisp_Object w = Fselected_window (), ws = Qnil;
2211 Lisp_Object buf1, buf2;
2212 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2214 while (NILP (Fmemq (w, ws)))
2216 ws = Fcons (w, ws);
2217 if (MARKERP (XWINDOW (w)->pointm)
2218 && (EQ (XWINDOW (w)->buffer, buf1)
2219 || EQ (XWINDOW (w)->buffer, buf2)))
2220 Fset_marker (XWINDOW (w)->pointm,
2221 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2222 XWINDOW (w)->buffer);
2223 w = Fnext_window (w, Qt, Qt);
2227 if (current_buffer->text->intervals)
2228 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2229 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2230 if (other_buffer->text->intervals)
2231 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2232 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2234 return Qnil;
2237 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2238 1, 1, 0,
2239 doc: /* Set the multibyte flag of the current buffer to FLAG.
2240 If FLAG is t, this makes the buffer a multibyte buffer.
2241 If FLAG is nil, this makes the buffer a single-byte buffer.
2242 In these cases, the buffer contents remain unchanged as a sequence of
2243 bytes but the contents viewed as characters do change.
2244 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2245 all eight-bit bytes to eight-bit characters.
2246 If the multibyte flag was really changed, undo information of the
2247 current buffer is cleared. */)
2248 (Lisp_Object flag)
2250 struct Lisp_Marker *tail, *markers;
2251 struct buffer *other;
2252 EMACS_INT begv, zv;
2253 int narrowed = (BEG != BEGV || Z != ZV);
2254 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2255 Lisp_Object old_undo = current_buffer->undo_list;
2256 struct gcpro gcpro1;
2258 if (current_buffer->base_buffer)
2259 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2261 /* Do nothing if nothing actually changes. */
2262 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2263 return flag;
2265 GCPRO1 (old_undo);
2267 /* Don't record these buffer changes. We will put a special undo entry
2268 instead. */
2269 current_buffer->undo_list = Qt;
2271 /* If the cached position is for this buffer, clear it out. */
2272 clear_charpos_cache (current_buffer);
2274 if (NILP (flag))
2275 begv = BEGV_BYTE, zv = ZV_BYTE;
2276 else
2277 begv = BEGV, zv = ZV;
2279 if (narrowed)
2280 Fwiden ();
2282 if (NILP (flag))
2284 EMACS_INT pos, stop;
2285 unsigned char *p;
2287 /* Do this first, so it can use CHAR_TO_BYTE
2288 to calculate the old correspondences. */
2289 set_intervals_multibyte (0);
2291 current_buffer->enable_multibyte_characters = Qnil;
2293 Z = Z_BYTE;
2294 BEGV = BEGV_BYTE;
2295 ZV = ZV_BYTE;
2296 GPT = GPT_BYTE;
2297 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2300 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2301 tail->charpos = tail->bytepos;
2303 /* Convert multibyte form of 8-bit characters to unibyte. */
2304 pos = BEG;
2305 stop = GPT;
2306 p = BEG_ADDR;
2307 while (1)
2309 int c, bytes;
2311 if (pos == stop)
2313 if (pos == Z)
2314 break;
2315 p = GAP_END_ADDR;
2316 stop = Z;
2318 if (ASCII_BYTE_P (*p))
2319 p++, pos++;
2320 else if (CHAR_BYTE8_HEAD_P (*p))
2322 c = STRING_CHAR_AND_LENGTH (p, bytes);
2323 /* Delete all bytes for this 8-bit character but the
2324 last one, and change the last one to the character
2325 code. */
2326 bytes--;
2327 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2328 p = GAP_END_ADDR;
2329 *p++ = c;
2330 pos++;
2331 if (begv > pos)
2332 begv -= bytes;
2333 if (zv > pos)
2334 zv -= bytes;
2335 stop = Z;
2337 else
2339 bytes = BYTES_BY_CHAR_HEAD (*p);
2340 p += bytes, pos += bytes;
2343 if (narrowed)
2344 Fnarrow_to_region (make_number (begv), make_number (zv));
2346 else
2348 EMACS_INT pt = PT;
2349 EMACS_INT pos, stop;
2350 unsigned char *p, *pend;
2352 /* Be sure not to have a multibyte sequence striding over the GAP.
2353 Ex: We change this: "...abc\302 _GAP_ \241def..."
2354 to: "...abc _GAP_ \302\241def..." */
2356 if (EQ (flag, Qt)
2357 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2358 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2360 unsigned char *p = GPT_ADDR - 1;
2362 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2363 if (LEADING_CODE_P (*p))
2365 EMACS_INT new_gpt = GPT_BYTE - (GPT_ADDR - p);
2367 move_gap_both (new_gpt, new_gpt);
2371 /* Make the buffer contents valid as multibyte by converting
2372 8-bit characters to multibyte form. */
2373 pos = BEG;
2374 stop = GPT;
2375 p = BEG_ADDR;
2376 pend = GPT_ADDR;
2377 while (1)
2379 int bytes;
2381 if (pos == stop)
2383 if (pos == Z)
2384 break;
2385 p = GAP_END_ADDR;
2386 pend = Z_ADDR;
2387 stop = Z;
2390 if (ASCII_BYTE_P (*p))
2391 p++, pos++;
2392 else if (EQ (flag, Qt)
2393 && ! CHAR_BYTE8_HEAD_P (*p)
2394 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2395 p += bytes, pos += bytes;
2396 else
2398 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2399 int c;
2401 c = BYTE8_TO_CHAR (*p);
2402 bytes = CHAR_STRING (c, tmp);
2403 *p = tmp[0];
2404 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2405 bytes--;
2406 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2407 /* Now the gap is after the just inserted data. */
2408 pos = GPT;
2409 p = GAP_END_ADDR;
2410 if (pos <= begv)
2411 begv += bytes;
2412 if (pos <= zv)
2413 zv += bytes;
2414 if (pos <= pt)
2415 pt += bytes;
2416 pend = Z_ADDR;
2417 stop = Z;
2421 if (pt != PT)
2422 TEMP_SET_PT (pt);
2424 if (narrowed)
2425 Fnarrow_to_region (make_number (begv), make_number (zv));
2427 /* Do this first, so that chars_in_text asks the right question.
2428 set_intervals_multibyte needs it too. */
2429 current_buffer->enable_multibyte_characters = Qt;
2431 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2432 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2434 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2436 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2437 if (BEGV_BYTE > GPT_BYTE)
2438 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2439 else
2440 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2442 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2443 if (ZV_BYTE > GPT_BYTE)
2444 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2445 else
2446 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2449 EMACS_INT pt_byte = advance_to_char_boundary (PT_BYTE);
2450 EMACS_INT pt;
2452 if (pt_byte > GPT_BYTE)
2453 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2454 else
2455 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2456 TEMP_SET_PT_BOTH (pt, pt_byte);
2459 tail = markers = BUF_MARKERS (current_buffer);
2461 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2462 getting confused by the markers that have not yet been updated.
2463 It is also a signal that it should never create a marker. */
2464 BUF_MARKERS (current_buffer) = NULL;
2466 for (; tail; tail = tail->next)
2468 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2469 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2472 /* Make sure no markers were put on the chain
2473 while the chain value was incorrect. */
2474 if (BUF_MARKERS (current_buffer))
2475 abort ();
2477 BUF_MARKERS (current_buffer) = markers;
2479 /* Do this last, so it can calculate the new correspondences
2480 between chars and bytes. */
2481 set_intervals_multibyte (1);
2484 if (!EQ (old_undo, Qt))
2486 /* Represent all the above changes by a special undo entry. */
2487 current_buffer->undo_list = Fcons (list3 (Qapply,
2488 intern ("set-buffer-multibyte"),
2489 NILP (flag) ? Qt : Qnil),
2490 old_undo);
2493 UNGCPRO;
2495 /* Changing the multibyteness of a buffer means that all windows
2496 showing that buffer must be updated thoroughly. */
2497 current_buffer->prevent_redisplay_optimizations_p = 1;
2498 ++windows_or_buffers_changed;
2500 /* Copy this buffer's new multibyte status
2501 into all of its indirect buffers. */
2502 for (other = all_buffers; other; other = other->next)
2503 if (other->base_buffer == current_buffer && !NILP (other->name))
2505 other->enable_multibyte_characters
2506 = current_buffer->enable_multibyte_characters;
2507 other->prevent_redisplay_optimizations_p = 1;
2510 /* Restore the modifiedness of the buffer. */
2511 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2512 Fset_buffer_modified_p (Qnil);
2514 /* Update coding systems of this buffer's process (if any). */
2516 Lisp_Object process;
2518 process = Fget_buffer_process (Fcurrent_buffer ());
2519 if (PROCESSP (process))
2520 setup_process_coding_systems (process);
2523 return flag;
2526 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2527 0, 0, 0,
2528 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2529 Most local variable bindings are eliminated so that the default values
2530 become effective once more. Also, the syntax table is set from
2531 `standard-syntax-table', the local keymap is set to nil,
2532 and the abbrev table from `fundamental-mode-abbrev-table'.
2533 This function also forces redisplay of the mode line.
2535 Every function to select a new major mode starts by
2536 calling this function.
2538 As a special exception, local variables whose names have
2539 a non-nil `permanent-local' property are not eliminated by this function.
2541 The first thing this function does is run
2542 the normal hook `change-major-mode-hook'. */)
2543 (void)
2545 if (!NILP (Vrun_hooks))
2546 call1 (Vrun_hooks, Qchange_major_mode_hook);
2548 /* Make sure none of the bindings in local_var_alist
2549 remain swapped in, in their symbols. */
2551 swap_out_buffer_local_variables (current_buffer);
2553 /* Actually eliminate all local bindings of this buffer. */
2555 reset_buffer_local_variables (current_buffer, 0);
2557 /* Force mode-line redisplay. Useful here because all major mode
2558 commands call this function. */
2559 update_mode_lines++;
2561 return Qnil;
2564 /* Make sure no local variables remain set up with buffer B
2565 for their current values. */
2567 static void
2568 swap_out_buffer_local_variables (struct buffer *b)
2570 Lisp_Object oalist, alist, buffer;
2572 XSETBUFFER (buffer, b);
2573 oalist = b->local_var_alist;
2575 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2577 Lisp_Object sym = XCAR (XCAR (alist));
2578 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2579 /* Need not do anything if some other buffer's binding is
2580 now encached. */
2581 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2583 /* Symbol is set up for this buffer's old local value:
2584 swap it out! */
2585 swap_in_global_binding (XSYMBOL (sym));
2590 /* Find all the overlays in the current buffer that contain position POS.
2591 Return the number found, and store them in a vector in *VEC_PTR.
2592 Store in *LEN_PTR the size allocated for the vector.
2593 Store in *NEXT_PTR the next position after POS where an overlay starts,
2594 or ZV if there are no more overlays between POS and ZV.
2595 Store in *PREV_PTR the previous position before POS where an overlay ends,
2596 or where an overlay starts which ends at or after POS;
2597 or BEGV if there are no such overlays from BEGV to POS.
2598 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2600 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2601 when this function is called.
2603 If EXTEND is non-zero, we make the vector bigger if necessary.
2604 If EXTEND is zero, we never extend the vector,
2605 and we store only as many overlays as will fit.
2606 But we still return the total number of overlays.
2608 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2609 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2610 default (BEGV or ZV). */
2613 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2614 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2616 Lisp_Object overlay, start, end;
2617 struct Lisp_Overlay *tail;
2618 int idx = 0;
2619 int len = *len_ptr;
2620 Lisp_Object *vec = *vec_ptr;
2621 EMACS_INT next = ZV;
2622 EMACS_INT prev = BEGV;
2623 int inhibit_storing = 0;
2625 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2627 EMACS_INT startpos, endpos;
2629 XSETMISC (overlay, tail);
2631 start = OVERLAY_START (overlay);
2632 end = OVERLAY_END (overlay);
2633 endpos = OVERLAY_POSITION (end);
2634 if (endpos < pos)
2636 if (prev < endpos)
2637 prev = endpos;
2638 break;
2640 startpos = OVERLAY_POSITION (start);
2641 /* This one ends at or after POS
2642 so its start counts for PREV_PTR if it's before POS. */
2643 if (prev < startpos && startpos < pos)
2644 prev = startpos;
2645 if (endpos == pos)
2646 continue;
2647 if (startpos <= pos)
2649 if (idx == len)
2651 /* The supplied vector is full.
2652 Either make it bigger, or don't store any more in it. */
2653 if (extend)
2655 /* Make it work with an initial len == 0. */
2656 len *= 2;
2657 if (len == 0)
2658 len = 4;
2659 *len_ptr = len;
2660 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2661 *vec_ptr = vec;
2663 else
2664 inhibit_storing = 1;
2667 if (!inhibit_storing)
2668 vec[idx] = overlay;
2669 /* Keep counting overlays even if we can't return them all. */
2670 idx++;
2672 else if (startpos < next)
2673 next = startpos;
2676 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2678 EMACS_INT startpos, endpos;
2680 XSETMISC (overlay, tail);
2682 start = OVERLAY_START (overlay);
2683 end = OVERLAY_END (overlay);
2684 startpos = OVERLAY_POSITION (start);
2685 if (pos < startpos)
2687 if (startpos < next)
2688 next = startpos;
2689 break;
2691 endpos = OVERLAY_POSITION (end);
2692 if (pos < endpos)
2694 if (idx == len)
2696 if (extend)
2698 /* Make it work with an initial len == 0. */
2699 len *= 2;
2700 if (len == 0)
2701 len = 4;
2702 *len_ptr = len;
2703 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2704 *vec_ptr = vec;
2706 else
2707 inhibit_storing = 1;
2710 if (!inhibit_storing)
2711 vec[idx] = overlay;
2712 idx++;
2714 if (startpos < pos && startpos > prev)
2715 prev = startpos;
2717 else if (endpos < pos && endpos > prev)
2718 prev = endpos;
2719 else if (endpos == pos && startpos > prev
2720 && (!change_req || startpos < pos))
2721 prev = startpos;
2724 if (next_ptr)
2725 *next_ptr = next;
2726 if (prev_ptr)
2727 *prev_ptr = prev;
2728 return idx;
2731 /* Find all the overlays in the current buffer that overlap the range
2732 BEG-END, or are empty at BEG, or are empty at END provided END
2733 denotes the position at the end of the current buffer.
2735 Return the number found, and store them in a vector in *VEC_PTR.
2736 Store in *LEN_PTR the size allocated for the vector.
2737 Store in *NEXT_PTR the next position after POS where an overlay starts,
2738 or ZV if there are no more overlays.
2739 Store in *PREV_PTR the previous position before POS where an overlay ends,
2740 or BEGV if there are no previous overlays.
2741 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2743 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2744 when this function is called.
2746 If EXTEND is non-zero, we make the vector bigger if necessary.
2747 If EXTEND is zero, we never extend the vector,
2748 and we store only as many overlays as will fit.
2749 But we still return the total number of overlays. */
2751 static int
2752 overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
2753 Lisp_Object **vec_ptr, int *len_ptr,
2754 EMACS_INT *next_ptr, EMACS_INT *prev_ptr)
2756 Lisp_Object overlay, ostart, oend;
2757 struct Lisp_Overlay *tail;
2758 int idx = 0;
2759 int len = *len_ptr;
2760 Lisp_Object *vec = *vec_ptr;
2761 EMACS_INT next = ZV;
2762 EMACS_INT prev = BEGV;
2763 int inhibit_storing = 0;
2764 int end_is_Z = end == Z;
2766 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2768 EMACS_INT startpos, endpos;
2770 XSETMISC (overlay, tail);
2772 ostart = OVERLAY_START (overlay);
2773 oend = OVERLAY_END (overlay);
2774 endpos = OVERLAY_POSITION (oend);
2775 if (endpos < beg)
2777 if (prev < endpos)
2778 prev = endpos;
2779 break;
2781 startpos = OVERLAY_POSITION (ostart);
2782 /* Count an interval if it overlaps the range, is empty at the
2783 start of the range, or is empty at END provided END denotes the
2784 end of the buffer. */
2785 if ((beg < endpos && startpos < end)
2786 || (startpos == endpos
2787 && (beg == endpos || (end_is_Z && endpos == end))))
2789 if (idx == len)
2791 /* The supplied vector is full.
2792 Either make it bigger, or don't store any more in it. */
2793 if (extend)
2795 /* Make it work with an initial len == 0. */
2796 len *= 2;
2797 if (len == 0)
2798 len = 4;
2799 *len_ptr = len;
2800 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2801 *vec_ptr = vec;
2803 else
2804 inhibit_storing = 1;
2807 if (!inhibit_storing)
2808 vec[idx] = overlay;
2809 /* Keep counting overlays even if we can't return them all. */
2810 idx++;
2812 else if (startpos < next)
2813 next = startpos;
2816 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2818 EMACS_INT startpos, endpos;
2820 XSETMISC (overlay, tail);
2822 ostart = OVERLAY_START (overlay);
2823 oend = OVERLAY_END (overlay);
2824 startpos = OVERLAY_POSITION (ostart);
2825 if (end < startpos)
2827 if (startpos < next)
2828 next = startpos;
2829 break;
2831 endpos = OVERLAY_POSITION (oend);
2832 /* Count an interval if it overlaps the range, is empty at the
2833 start of the range, or is empty at END provided END denotes the
2834 end of the buffer. */
2835 if ((beg < endpos && startpos < end)
2836 || (startpos == endpos
2837 && (beg == endpos || (end_is_Z && endpos == end))))
2839 if (idx == len)
2841 if (extend)
2843 /* Make it work with an initial len == 0. */
2844 len *= 2;
2845 if (len == 0)
2846 len = 4;
2847 *len_ptr = len;
2848 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2849 *vec_ptr = vec;
2851 else
2852 inhibit_storing = 1;
2855 if (!inhibit_storing)
2856 vec[idx] = overlay;
2857 idx++;
2859 else if (endpos < beg && endpos > prev)
2860 prev = endpos;
2863 if (next_ptr)
2864 *next_ptr = next;
2865 if (prev_ptr)
2866 *prev_ptr = prev;
2867 return idx;
2871 /* Return non-zero if there exists an overlay with a non-nil
2872 `mouse-face' property overlapping OVERLAY. */
2875 mouse_face_overlay_overlaps (Lisp_Object overlay)
2877 EMACS_INT start = OVERLAY_POSITION (OVERLAY_START (overlay));
2878 EMACS_INT end = OVERLAY_POSITION (OVERLAY_END (overlay));
2879 int n, i, size;
2880 Lisp_Object *v, tem;
2882 size = 10;
2883 v = (Lisp_Object *) alloca (size * sizeof *v);
2884 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2885 if (n > size)
2887 v = (Lisp_Object *) alloca (n * sizeof *v);
2888 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2891 for (i = 0; i < n; ++i)
2892 if (!EQ (v[i], overlay)
2893 && (tem = Foverlay_get (overlay, Qmouse_face),
2894 !NILP (tem)))
2895 break;
2897 return i < n;
2902 /* Fast function to just test if we're at an overlay boundary. */
2904 overlay_touches_p (EMACS_INT pos)
2906 Lisp_Object overlay;
2907 struct Lisp_Overlay *tail;
2909 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2911 EMACS_INT endpos;
2913 XSETMISC (overlay ,tail);
2914 if (!OVERLAYP (overlay))
2915 abort ();
2917 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2918 if (endpos < pos)
2919 break;
2920 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2921 return 1;
2924 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2926 EMACS_INT startpos;
2928 XSETMISC (overlay, tail);
2929 if (!OVERLAYP (overlay))
2930 abort ();
2932 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2933 if (pos < startpos)
2934 break;
2935 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2936 return 1;
2938 return 0;
2941 struct sortvec
2943 Lisp_Object overlay;
2944 EMACS_INT beg, end;
2945 int priority;
2948 static int
2949 compare_overlays (const void *v1, const void *v2)
2951 const struct sortvec *s1 = (const struct sortvec *) v1;
2952 const struct sortvec *s2 = (const struct sortvec *) v2;
2953 if (s1->priority != s2->priority)
2954 return s1->priority - s2->priority;
2955 if (s1->beg != s2->beg)
2956 return s1->beg - s2->beg;
2957 if (s1->end != s2->end)
2958 return s2->end - s1->end;
2959 return 0;
2962 /* Sort an array of overlays by priority. The array is modified in place.
2963 The return value is the new size; this may be smaller than the original
2964 size if some of the overlays were invalid or were window-specific. */
2966 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2968 int i, j;
2969 struct sortvec *sortvec;
2970 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2972 /* Put the valid and relevant overlays into sortvec. */
2974 for (i = 0, j = 0; i < noverlays; i++)
2976 Lisp_Object tem;
2977 Lisp_Object overlay;
2979 overlay = overlay_vec[i];
2980 if (OVERLAY_VALID (overlay)
2981 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2982 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2984 /* If we're interested in a specific window, then ignore
2985 overlays that are limited to some other window. */
2986 if (w)
2988 Lisp_Object window;
2990 window = Foverlay_get (overlay, Qwindow);
2991 if (WINDOWP (window) && XWINDOW (window) != w)
2992 continue;
2995 /* This overlay is good and counts: put it into sortvec. */
2996 sortvec[j].overlay = overlay;
2997 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2998 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2999 tem = Foverlay_get (overlay, Qpriority);
3000 if (INTEGERP (tem))
3001 sortvec[j].priority = XINT (tem);
3002 else
3003 sortvec[j].priority = 0;
3004 j++;
3007 noverlays = j;
3009 /* Sort the overlays into the proper order: increasing priority. */
3011 if (noverlays > 1)
3012 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3014 for (i = 0; i < noverlays; i++)
3015 overlay_vec[i] = sortvec[i].overlay;
3016 return (noverlays);
3019 struct sortstr
3021 Lisp_Object string, string2;
3022 int size;
3023 int priority;
3026 struct sortstrlist
3028 struct sortstr *buf; /* An array that expands as needed; never freed. */
3029 int size; /* Allocated length of that array. */
3030 int used; /* How much of the array is currently in use. */
3031 EMACS_INT bytes; /* Total length of the strings in buf. */
3034 /* Buffers for storing information about the overlays touching a given
3035 position. These could be automatic variables in overlay_strings, but
3036 it's more efficient to hold onto the memory instead of repeatedly
3037 allocating and freeing it. */
3038 static struct sortstrlist overlay_heads, overlay_tails;
3039 static unsigned char *overlay_str_buf;
3041 /* Allocated length of overlay_str_buf. */
3042 static EMACS_INT overlay_str_len;
3044 /* A comparison function suitable for passing to qsort. */
3045 static int
3046 cmp_for_strings (const void *as1, const void *as2)
3048 struct sortstr *s1 = (struct sortstr *)as1;
3049 struct sortstr *s2 = (struct sortstr *)as2;
3050 if (s1->size != s2->size)
3051 return s2->size - s1->size;
3052 if (s1->priority != s2->priority)
3053 return s1->priority - s2->priority;
3054 return 0;
3057 static void
3058 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
3060 EMACS_INT nbytes;
3062 if (ssl->used == ssl->size)
3064 if (ssl->buf)
3065 ssl->size *= 2;
3066 else
3067 ssl->size = 5;
3068 ssl->buf = ((struct sortstr *)
3069 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3071 ssl->buf[ssl->used].string = str;
3072 ssl->buf[ssl->used].string2 = str2;
3073 ssl->buf[ssl->used].size = size;
3074 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3075 ssl->used++;
3077 if (NILP (current_buffer->enable_multibyte_characters))
3078 nbytes = SCHARS (str);
3079 else if (! STRING_MULTIBYTE (str))
3080 nbytes = count_size_as_multibyte (SDATA (str),
3081 SBYTES (str));
3082 else
3083 nbytes = SBYTES (str);
3085 ssl->bytes += nbytes;
3087 if (STRINGP (str2))
3089 if (NILP (current_buffer->enable_multibyte_characters))
3090 nbytes = SCHARS (str2);
3091 else if (! STRING_MULTIBYTE (str2))
3092 nbytes = count_size_as_multibyte (SDATA (str2),
3093 SBYTES (str2));
3094 else
3095 nbytes = SBYTES (str2);
3097 ssl->bytes += nbytes;
3101 /* Return the concatenation of the strings associated with overlays that
3102 begin or end at POS, ignoring overlays that are specific to a window
3103 other than W. The strings are concatenated in the appropriate order:
3104 shorter overlays nest inside longer ones, and higher priority inside
3105 lower. Normally all of the after-strings come first, but zero-sized
3106 overlays have their after-strings ride along with the before-strings
3107 because it would look strange to print them inside-out.
3109 Returns the string length, and stores the contents indirectly through
3110 PSTR, if that variable is non-null. The string may be overwritten by
3111 subsequent calls. */
3113 EMACS_INT
3114 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3116 Lisp_Object overlay, window, str;
3117 struct Lisp_Overlay *ov;
3118 EMACS_INT startpos, endpos;
3119 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3121 overlay_heads.used = overlay_heads.bytes = 0;
3122 overlay_tails.used = overlay_tails.bytes = 0;
3123 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3125 XSETMISC (overlay, ov);
3126 eassert (OVERLAYP (overlay));
3128 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3129 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3130 if (endpos < pos)
3131 break;
3132 if (endpos != pos && startpos != pos)
3133 continue;
3134 window = Foverlay_get (overlay, Qwindow);
3135 if (WINDOWP (window) && XWINDOW (window) != w)
3136 continue;
3137 if (startpos == pos
3138 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3139 record_overlay_string (&overlay_heads, str,
3140 (startpos == endpos
3141 ? Foverlay_get (overlay, Qafter_string)
3142 : Qnil),
3143 Foverlay_get (overlay, Qpriority),
3144 endpos - startpos);
3145 else if (endpos == pos
3146 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3147 record_overlay_string (&overlay_tails, str, Qnil,
3148 Foverlay_get (overlay, Qpriority),
3149 endpos - startpos);
3151 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3153 XSETMISC (overlay, ov);
3154 eassert (OVERLAYP (overlay));
3156 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3157 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3158 if (startpos > pos)
3159 break;
3160 if (endpos != pos && startpos != pos)
3161 continue;
3162 window = Foverlay_get (overlay, Qwindow);
3163 if (WINDOWP (window) && XWINDOW (window) != w)
3164 continue;
3165 if (startpos == pos
3166 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3167 record_overlay_string (&overlay_heads, str,
3168 (startpos == endpos
3169 ? Foverlay_get (overlay, Qafter_string)
3170 : Qnil),
3171 Foverlay_get (overlay, Qpriority),
3172 endpos - startpos);
3173 else if (endpos == pos
3174 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3175 record_overlay_string (&overlay_tails, str, Qnil,
3176 Foverlay_get (overlay, Qpriority),
3177 endpos - startpos);
3179 if (overlay_tails.used > 1)
3180 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3181 cmp_for_strings);
3182 if (overlay_heads.used > 1)
3183 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3184 cmp_for_strings);
3185 if (overlay_heads.bytes || overlay_tails.bytes)
3187 Lisp_Object tem;
3188 EMACS_INT i;
3189 unsigned char *p;
3190 EMACS_INT total = overlay_heads.bytes + overlay_tails.bytes;
3192 if (total > overlay_str_len)
3194 overlay_str_len = total;
3195 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3196 total);
3198 p = overlay_str_buf;
3199 for (i = overlay_tails.used; --i >= 0;)
3201 EMACS_INT nbytes;
3202 tem = overlay_tails.buf[i].string;
3203 nbytes = copy_text (SDATA (tem), p,
3204 SBYTES (tem),
3205 STRING_MULTIBYTE (tem), multibyte);
3206 p += nbytes;
3208 for (i = 0; i < overlay_heads.used; ++i)
3210 EMACS_INT nbytes;
3211 tem = overlay_heads.buf[i].string;
3212 nbytes = copy_text (SDATA (tem), p,
3213 SBYTES (tem),
3214 STRING_MULTIBYTE (tem), multibyte);
3215 p += nbytes;
3216 tem = overlay_heads.buf[i].string2;
3217 if (STRINGP (tem))
3219 nbytes = copy_text (SDATA (tem), p,
3220 SBYTES (tem),
3221 STRING_MULTIBYTE (tem), multibyte);
3222 p += nbytes;
3225 if (p != overlay_str_buf + total)
3226 abort ();
3227 if (pstr)
3228 *pstr = overlay_str_buf;
3229 return total;
3231 return 0;
3234 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3236 void
3237 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3239 Lisp_Object overlay, beg, end;
3240 struct Lisp_Overlay *prev, *tail, *next;
3242 /* See if anything in overlays_before should move to overlays_after. */
3244 /* We don't strictly need prev in this loop; it should always be nil.
3245 But we use it for symmetry and in case that should cease to be true
3246 with some future change. */
3247 prev = NULL;
3248 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3250 next = tail->next;
3251 XSETMISC (overlay, tail);
3253 /* If the overlay is not valid, get rid of it. */
3254 if (!OVERLAY_VALID (overlay))
3255 #if 1
3256 abort ();
3257 #else
3259 /* Splice the cons cell TAIL out of overlays_before. */
3260 if (!NILP (prev))
3261 XCDR (prev) = next;
3262 else
3263 buf->overlays_before = next;
3264 tail = prev;
3265 continue;
3267 #endif
3269 beg = OVERLAY_START (overlay);
3270 end = OVERLAY_END (overlay);
3272 if (OVERLAY_POSITION (end) > pos)
3274 /* OVERLAY needs to be moved. */
3275 EMACS_INT where = OVERLAY_POSITION (beg);
3276 struct Lisp_Overlay *other, *other_prev;
3278 /* Splice the cons cell TAIL out of overlays_before. */
3279 if (prev)
3280 prev->next = next;
3281 else
3282 buf->overlays_before = next;
3284 /* Search thru overlays_after for where to put it. */
3285 other_prev = NULL;
3286 for (other = buf->overlays_after; other;
3287 other_prev = other, other = other->next)
3289 Lisp_Object otherbeg, otheroverlay;
3291 XSETMISC (otheroverlay, other);
3292 eassert (OVERLAY_VALID (otheroverlay));
3294 otherbeg = OVERLAY_START (otheroverlay);
3295 if (OVERLAY_POSITION (otherbeg) >= where)
3296 break;
3299 /* Add TAIL to overlays_after before OTHER. */
3300 tail->next = other;
3301 if (other_prev)
3302 other_prev->next = tail;
3303 else
3304 buf->overlays_after = tail;
3305 tail = prev;
3307 else
3308 /* We've reached the things that should stay in overlays_before.
3309 All the rest of overlays_before must end even earlier,
3310 so stop now. */
3311 break;
3314 /* See if anything in overlays_after should be in overlays_before. */
3315 prev = NULL;
3316 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3318 next = tail->next;
3319 XSETMISC (overlay, tail);
3321 /* If the overlay is not valid, get rid of it. */
3322 if (!OVERLAY_VALID (overlay))
3323 #if 1
3324 abort ();
3325 #else
3327 /* Splice the cons cell TAIL out of overlays_after. */
3328 if (!NILP (prev))
3329 XCDR (prev) = next;
3330 else
3331 buf->overlays_after = next;
3332 tail = prev;
3333 continue;
3335 #endif
3337 beg = OVERLAY_START (overlay);
3338 end = OVERLAY_END (overlay);
3340 /* Stop looking, when we know that nothing further
3341 can possibly end before POS. */
3342 if (OVERLAY_POSITION (beg) > pos)
3343 break;
3345 if (OVERLAY_POSITION (end) <= pos)
3347 /* OVERLAY needs to be moved. */
3348 EMACS_INT where = OVERLAY_POSITION (end);
3349 struct Lisp_Overlay *other, *other_prev;
3351 /* Splice the cons cell TAIL out of overlays_after. */
3352 if (prev)
3353 prev->next = next;
3354 else
3355 buf->overlays_after = next;
3357 /* Search thru overlays_before for where to put it. */
3358 other_prev = NULL;
3359 for (other = buf->overlays_before; other;
3360 other_prev = other, other = other->next)
3362 Lisp_Object otherend, otheroverlay;
3364 XSETMISC (otheroverlay, other);
3365 eassert (OVERLAY_VALID (otheroverlay));
3367 otherend = OVERLAY_END (otheroverlay);
3368 if (OVERLAY_POSITION (otherend) <= where)
3369 break;
3372 /* Add TAIL to overlays_before before OTHER. */
3373 tail->next = other;
3374 if (other_prev)
3375 other_prev->next = tail;
3376 else
3377 buf->overlays_before = tail;
3378 tail = prev;
3382 buf->overlay_center = pos;
3385 void
3386 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3388 /* After an insertion, the lists are still sorted properly,
3389 but we may need to update the value of the overlay center. */
3390 if (current_buffer->overlay_center >= pos)
3391 current_buffer->overlay_center += length;
3394 void
3395 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3397 if (current_buffer->overlay_center < pos)
3398 /* The deletion was to our right. No change needed; the before- and
3399 after-lists are still consistent. */
3401 else if (current_buffer->overlay_center > pos + length)
3402 /* The deletion was to our left. We need to adjust the center value
3403 to account for the change in position, but the lists are consistent
3404 given the new value. */
3405 current_buffer->overlay_center -= length;
3406 else
3407 /* We're right in the middle. There might be things on the after-list
3408 that now belong on the before-list. Recentering will move them,
3409 and also update the center point. */
3410 recenter_overlay_lists (current_buffer, pos);
3413 /* Fix up overlays that were garbled as a result of permuting markers
3414 in the range START through END. Any overlay with at least one
3415 endpoint in this range will need to be unlinked from the overlay
3416 list and reinserted in its proper place.
3417 Such an overlay might even have negative size at this point.
3418 If so, we'll make the overlay empty. */
3419 void
3420 fix_start_end_in_overlays (register EMACS_INT start, register EMACS_INT end)
3422 Lisp_Object overlay;
3423 struct Lisp_Overlay *before_list, *after_list;
3424 /* These are either nil, indicating that before_list or after_list
3425 should be assigned, or the cons cell the cdr of which should be
3426 assigned. */
3427 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3428 /* 'Parent', likewise, indicates a cons cell or
3429 current_buffer->overlays_before or overlays_after, depending
3430 which loop we're in. */
3431 struct Lisp_Overlay *tail, *parent;
3432 EMACS_INT startpos, endpos;
3434 /* This algorithm shifts links around instead of consing and GCing.
3435 The loop invariant is that before_list (resp. after_list) is a
3436 well-formed list except that its last element, the CDR of beforep
3437 (resp. afterp) if beforep (afterp) isn't nil or before_list
3438 (after_list) if it is, is still uninitialized. So it's not a bug
3439 that before_list isn't initialized, although it may look
3440 strange. */
3441 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3443 XSETMISC (overlay, tail);
3445 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3446 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3448 /* If the overlay is backwards, make it empty. */
3449 if (endpos < startpos)
3451 startpos = endpos;
3452 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3453 Qnil);
3456 if (endpos < start)
3457 break;
3459 if (endpos < end
3460 || (startpos >= start && startpos < end))
3462 /* Add it to the end of the wrong list. Later on,
3463 recenter_overlay_lists will move it to the right place. */
3464 if (endpos < current_buffer->overlay_center)
3466 if (!afterp)
3467 after_list = tail;
3468 else
3469 afterp->next = tail;
3470 afterp = tail;
3472 else
3474 if (!beforep)
3475 before_list = tail;
3476 else
3477 beforep->next = tail;
3478 beforep = tail;
3480 if (!parent)
3481 current_buffer->overlays_before = tail->next;
3482 else
3483 parent->next = tail->next;
3484 tail = tail->next;
3486 else
3487 parent = tail, tail = parent->next;
3489 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3491 XSETMISC (overlay, tail);
3493 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3494 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3496 /* If the overlay is backwards, make it empty. */
3497 if (endpos < startpos)
3499 startpos = endpos;
3500 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3501 Qnil);
3504 if (startpos >= end)
3505 break;
3507 if (startpos >= start
3508 || (endpos >= start && endpos < end))
3510 if (endpos < current_buffer->overlay_center)
3512 if (!afterp)
3513 after_list = tail;
3514 else
3515 afterp->next = tail;
3516 afterp = tail;
3518 else
3520 if (!beforep)
3521 before_list = tail;
3522 else
3523 beforep->next = tail;
3524 beforep = tail;
3526 if (!parent)
3527 current_buffer->overlays_after = tail->next;
3528 else
3529 parent->next = tail->next;
3530 tail = tail->next;
3532 else
3533 parent = tail, tail = parent->next;
3536 /* Splice the constructed (wrong) lists into the buffer's lists,
3537 and let the recenter function make it sane again. */
3538 if (beforep)
3540 beforep->next = current_buffer->overlays_before;
3541 current_buffer->overlays_before = before_list;
3543 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3545 if (afterp)
3547 afterp->next = current_buffer->overlays_after;
3548 current_buffer->overlays_after = after_list;
3550 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3553 /* We have two types of overlay: the one whose ending marker is
3554 after-insertion-marker (this is the usual case) and the one whose
3555 ending marker is before-insertion-marker. When `overlays_before'
3556 contains overlays of the latter type and the former type in this
3557 order and both overlays end at inserting position, inserting a text
3558 increases only the ending marker of the latter type, which results
3559 in incorrect ordering of `overlays_before'.
3561 This function fixes ordering of overlays in the slot
3562 `overlays_before' of the buffer *BP. Before the insertion, `point'
3563 was at PREV, and now is at POS. */
3565 void
3566 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3568 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3569 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3570 Lisp_Object tem;
3571 EMACS_INT end;
3573 /* After the insertion, the several overlays may be in incorrect
3574 order. The possibility is that, in the list `overlays_before',
3575 an overlay which ends at POS appears after an overlay which ends
3576 at PREV. Since POS is greater than PREV, we must fix the
3577 ordering of these overlays, by moving overlays ends at POS before
3578 the overlays ends at PREV. */
3580 /* At first, find a place where disordered overlays should be linked
3581 in. It is where an overlay which end before POS exists. (i.e. an
3582 overlay whose ending marker is after-insertion-marker if disorder
3583 exists). */
3584 while (tail
3585 && (XSETMISC (tem, tail),
3586 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3588 parent = tail;
3589 tail = tail->next;
3592 /* If we don't find such an overlay,
3593 or the found one ends before PREV,
3594 or the found one is the last one in the list,
3595 we don't have to fix anything. */
3596 if (!tail || end < prev || !tail->next)
3597 return;
3599 right_pair = parent;
3600 parent = tail;
3601 tail = tail->next;
3603 /* Now, end position of overlays in the list TAIL should be before
3604 or equal to PREV. In the loop, an overlay which ends at POS is
3605 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3606 we found an overlay which ends before PREV, the remaining
3607 overlays are in correct order. */
3608 while (tail)
3610 XSETMISC (tem, tail);
3611 end = OVERLAY_POSITION (OVERLAY_END (tem));
3613 if (end == pos)
3614 { /* This overlay is disordered. */
3615 struct Lisp_Overlay *found = tail;
3617 /* Unlink the found overlay. */
3618 tail = found->next;
3619 parent->next = tail;
3620 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3621 and link it into the right place. */
3622 if (!right_pair)
3624 found->next = bp->overlays_before;
3625 bp->overlays_before = found;
3627 else
3629 found->next = right_pair->next;
3630 right_pair->next = found;
3633 else if (end == prev)
3635 parent = tail;
3636 tail = tail->next;
3638 else /* No more disordered overlay. */
3639 break;
3643 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3644 doc: /* Return t if OBJECT is an overlay. */)
3645 (Lisp_Object object)
3647 return (OVERLAYP (object) ? Qt : Qnil);
3650 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3651 doc: /* Create a new overlay with range BEG to END in BUFFER.
3652 If omitted, BUFFER defaults to the current buffer.
3653 BEG and END may be integers or markers.
3654 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3655 for the front of the overlay advance when text is inserted there
3656 \(which means the text *is not* included in the overlay).
3657 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3658 for the rear of the overlay advance when text is inserted there
3659 \(which means the text *is* included in the overlay). */)
3660 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3662 Lisp_Object overlay;
3663 struct buffer *b;
3665 if (NILP (buffer))
3666 XSETBUFFER (buffer, current_buffer);
3667 else
3668 CHECK_BUFFER (buffer);
3669 if (MARKERP (beg)
3670 && ! EQ (Fmarker_buffer (beg), buffer))
3671 error ("Marker points into wrong buffer");
3672 if (MARKERP (end)
3673 && ! EQ (Fmarker_buffer (end), buffer))
3674 error ("Marker points into wrong buffer");
3676 CHECK_NUMBER_COERCE_MARKER (beg);
3677 CHECK_NUMBER_COERCE_MARKER (end);
3679 if (XINT (beg) > XINT (end))
3681 Lisp_Object temp;
3682 temp = beg; beg = end; end = temp;
3685 b = XBUFFER (buffer);
3687 beg = Fset_marker (Fmake_marker (), beg, buffer);
3688 end = Fset_marker (Fmake_marker (), end, buffer);
3690 if (!NILP (front_advance))
3691 XMARKER (beg)->insertion_type = 1;
3692 if (!NILP (rear_advance))
3693 XMARKER (end)->insertion_type = 1;
3695 overlay = allocate_misc ();
3696 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3697 XOVERLAY (overlay)->start = beg;
3698 XOVERLAY (overlay)->end = end;
3699 XOVERLAY (overlay)->plist = Qnil;
3700 XOVERLAY (overlay)->next = NULL;
3702 /* Put the new overlay on the wrong list. */
3703 end = OVERLAY_END (overlay);
3704 if (OVERLAY_POSITION (end) < b->overlay_center)
3706 if (b->overlays_after)
3707 XOVERLAY (overlay)->next = b->overlays_after;
3708 b->overlays_after = XOVERLAY (overlay);
3710 else
3712 if (b->overlays_before)
3713 XOVERLAY (overlay)->next = b->overlays_before;
3714 b->overlays_before = XOVERLAY (overlay);
3717 /* This puts it in the right list, and in the right order. */
3718 recenter_overlay_lists (b, b->overlay_center);
3720 /* We don't need to redisplay the region covered by the overlay, because
3721 the overlay has no properties at the moment. */
3723 return overlay;
3726 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3728 static void
3729 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3731 if (start > end)
3733 EMACS_INT temp = start;
3734 start = end;
3735 end = temp;
3738 BUF_COMPUTE_UNCHANGED (buf, start, end);
3740 /* If this is a buffer not in the selected window,
3741 we must do other windows. */
3742 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3743 windows_or_buffers_changed = 1;
3744 /* If multiple windows show this buffer, we must do other windows. */
3745 else if (buffer_shared > 1)
3746 windows_or_buffers_changed = 1;
3747 /* If we modify an overlay at the end of the buffer, we cannot
3748 be sure that window end is still valid. */
3749 else if (end >= ZV && start <= ZV)
3750 windows_or_buffers_changed = 1;
3752 ++BUF_OVERLAY_MODIFF (buf);
3756 static struct Lisp_Overlay *
3757 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3759 struct Lisp_Overlay *tmp, *prev;
3760 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3761 if (tmp == overlay)
3763 if (prev)
3764 prev->next = tmp->next;
3765 else
3766 list = tmp->next;
3767 overlay->next = NULL;
3768 break;
3770 return list;
3773 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3774 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3775 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3776 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3777 buffer. */)
3778 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3780 struct buffer *b, *ob;
3781 Lisp_Object obuffer;
3782 int count = SPECPDL_INDEX ();
3784 CHECK_OVERLAY (overlay);
3785 if (NILP (buffer))
3786 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3787 if (NILP (buffer))
3788 XSETBUFFER (buffer, current_buffer);
3789 CHECK_BUFFER (buffer);
3791 if (MARKERP (beg)
3792 && ! EQ (Fmarker_buffer (beg), buffer))
3793 error ("Marker points into wrong buffer");
3794 if (MARKERP (end)
3795 && ! EQ (Fmarker_buffer (end), buffer))
3796 error ("Marker points into wrong buffer");
3798 CHECK_NUMBER_COERCE_MARKER (beg);
3799 CHECK_NUMBER_COERCE_MARKER (end);
3801 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3802 return Fdelete_overlay (overlay);
3804 if (XINT (beg) > XINT (end))
3806 Lisp_Object temp;
3807 temp = beg; beg = end; end = temp;
3810 specbind (Qinhibit_quit, Qt);
3812 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3813 b = XBUFFER (buffer);
3814 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3816 /* If the overlay has changed buffers, do a thorough redisplay. */
3817 if (!EQ (buffer, obuffer))
3819 /* Redisplay where the overlay was. */
3820 if (!NILP (obuffer))
3822 EMACS_INT o_beg;
3823 EMACS_INT o_end;
3825 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3826 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3828 modify_overlay (ob, o_beg, o_end);
3831 /* Redisplay where the overlay is going to be. */
3832 modify_overlay (b, XINT (beg), XINT (end));
3834 else
3835 /* Redisplay the area the overlay has just left, or just enclosed. */
3837 EMACS_INT o_beg, o_end;
3839 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3840 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3842 if (o_beg == XINT (beg))
3843 modify_overlay (b, o_end, XINT (end));
3844 else if (o_end == XINT (end))
3845 modify_overlay (b, o_beg, XINT (beg));
3846 else
3848 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3849 if (XINT (end) > o_end) o_end = XINT (end);
3850 modify_overlay (b, o_beg, o_end);
3854 if (!NILP (obuffer))
3856 ob->overlays_before
3857 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3858 ob->overlays_after
3859 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3860 eassert (XOVERLAY (overlay)->next == NULL);
3863 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3864 Fset_marker (OVERLAY_END (overlay), end, buffer);
3866 /* Put the overlay on the wrong list. */
3867 end = OVERLAY_END (overlay);
3868 if (OVERLAY_POSITION (end) < b->overlay_center)
3870 XOVERLAY (overlay)->next = b->overlays_after;
3871 b->overlays_after = XOVERLAY (overlay);
3873 else
3875 XOVERLAY (overlay)->next = b->overlays_before;
3876 b->overlays_before = XOVERLAY (overlay);
3879 /* This puts it in the right list, and in the right order. */
3880 recenter_overlay_lists (b, b->overlay_center);
3882 return unbind_to (count, overlay);
3885 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3886 doc: /* Delete the overlay OVERLAY from its buffer. */)
3887 (Lisp_Object overlay)
3889 Lisp_Object buffer;
3890 struct buffer *b;
3891 int count = SPECPDL_INDEX ();
3893 CHECK_OVERLAY (overlay);
3895 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3896 if (NILP (buffer))
3897 return Qnil;
3899 b = XBUFFER (buffer);
3900 specbind (Qinhibit_quit, Qt);
3902 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3903 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3904 eassert (XOVERLAY (overlay)->next == NULL);
3905 modify_overlay (b,
3906 marker_position (OVERLAY_START (overlay)),
3907 marker_position (OVERLAY_END (overlay)));
3908 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3909 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3911 /* When deleting an overlay with before or after strings, turn off
3912 display optimizations for the affected buffer, on the basis that
3913 these strings may contain newlines. This is easier to do than to
3914 check for that situation during redisplay. */
3915 if (!windows_or_buffers_changed
3916 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3917 || !NILP (Foverlay_get (overlay, Qafter_string))))
3918 b->prevent_redisplay_optimizations_p = 1;
3920 return unbind_to (count, Qnil);
3923 /* Overlay dissection functions. */
3925 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3926 doc: /* Return the position at which OVERLAY starts. */)
3927 (Lisp_Object overlay)
3929 CHECK_OVERLAY (overlay);
3931 return (Fmarker_position (OVERLAY_START (overlay)));
3934 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3935 doc: /* Return the position at which OVERLAY ends. */)
3936 (Lisp_Object overlay)
3938 CHECK_OVERLAY (overlay);
3940 return (Fmarker_position (OVERLAY_END (overlay)));
3943 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3944 doc: /* Return the buffer OVERLAY belongs to.
3945 Return nil if OVERLAY has been deleted. */)
3946 (Lisp_Object overlay)
3948 CHECK_OVERLAY (overlay);
3950 return Fmarker_buffer (OVERLAY_START (overlay));
3953 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3954 doc: /* Return a list of the properties on OVERLAY.
3955 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3956 OVERLAY. */)
3957 (Lisp_Object overlay)
3959 CHECK_OVERLAY (overlay);
3961 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3965 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3966 doc: /* Return a list of the overlays that contain the character at POS. */)
3967 (Lisp_Object pos)
3969 int noverlays;
3970 Lisp_Object *overlay_vec;
3971 int len;
3972 Lisp_Object result;
3974 CHECK_NUMBER_COERCE_MARKER (pos);
3976 len = 10;
3977 /* We can't use alloca here because overlays_at can call xrealloc. */
3978 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3980 /* Put all the overlays we want in a vector in overlay_vec.
3981 Store the length in len. */
3982 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3983 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
3985 /* Make a list of them all. */
3986 result = Flist (noverlays, overlay_vec);
3988 xfree (overlay_vec);
3989 return result;
3992 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3993 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3994 Overlap means that at least one character is contained within the overlay
3995 and also contained within the specified region.
3996 Empty overlays are included in the result if they are located at BEG,
3997 between BEG and END, or at END provided END denotes the position at the
3998 end of the buffer. */)
3999 (Lisp_Object beg, Lisp_Object end)
4001 int noverlays;
4002 Lisp_Object *overlay_vec;
4003 int len;
4004 Lisp_Object result;
4006 CHECK_NUMBER_COERCE_MARKER (beg);
4007 CHECK_NUMBER_COERCE_MARKER (end);
4009 len = 10;
4010 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4012 /* Put all the overlays we want in a vector in overlay_vec.
4013 Store the length in len. */
4014 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4015 NULL, NULL);
4017 /* Make a list of them all. */
4018 result = Flist (noverlays, overlay_vec);
4020 xfree (overlay_vec);
4021 return result;
4024 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4025 1, 1, 0,
4026 doc: /* Return the next position after POS where an overlay starts or ends.
4027 If there are no overlay boundaries from POS to (point-max),
4028 the value is (point-max). */)
4029 (Lisp_Object pos)
4031 int noverlays;
4032 EMACS_INT endpos;
4033 Lisp_Object *overlay_vec;
4034 int len;
4035 int i;
4037 CHECK_NUMBER_COERCE_MARKER (pos);
4039 len = 10;
4040 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4042 /* Put all the overlays we want in a vector in overlay_vec.
4043 Store the length in len.
4044 endpos gets the position where the next overlay starts. */
4045 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4046 &endpos, (EMACS_INT *) 0, 1);
4048 /* If any of these overlays ends before endpos,
4049 use its ending point instead. */
4050 for (i = 0; i < noverlays; i++)
4052 Lisp_Object oend;
4053 EMACS_INT oendpos;
4055 oend = OVERLAY_END (overlay_vec[i]);
4056 oendpos = OVERLAY_POSITION (oend);
4057 if (oendpos < endpos)
4058 endpos = oendpos;
4061 xfree (overlay_vec);
4062 return make_number (endpos);
4065 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4066 Sprevious_overlay_change, 1, 1, 0,
4067 doc: /* Return the previous position before POS where an overlay starts or ends.
4068 If there are no overlay boundaries from (point-min) to POS,
4069 the value is (point-min). */)
4070 (Lisp_Object pos)
4072 int noverlays;
4073 EMACS_INT prevpos;
4074 Lisp_Object *overlay_vec;
4075 int len;
4077 CHECK_NUMBER_COERCE_MARKER (pos);
4079 /* At beginning of buffer, we know the answer;
4080 avoid bug subtracting 1 below. */
4081 if (XINT (pos) == BEGV)
4082 return pos;
4084 len = 10;
4085 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4087 /* Put all the overlays we want in a vector in overlay_vec.
4088 Store the length in len.
4089 prevpos gets the position of the previous change. */
4090 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4091 (EMACS_INT *) 0, &prevpos, 1);
4093 xfree (overlay_vec);
4094 return make_number (prevpos);
4097 /* These functions are for debugging overlays. */
4099 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4100 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4101 The car has all the overlays before the overlay center;
4102 the cdr has all the overlays after the overlay center.
4103 Recentering overlays moves overlays between these lists.
4104 The lists you get are copies, so that changing them has no effect.
4105 However, the overlays you get are the real objects that the buffer uses. */)
4106 (void)
4108 struct Lisp_Overlay *ol;
4109 Lisp_Object before = Qnil, after = Qnil, tmp;
4110 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4112 XSETMISC (tmp, ol);
4113 before = Fcons (tmp, before);
4115 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4117 XSETMISC (tmp, ol);
4118 after = Fcons (tmp, after);
4120 return Fcons (Fnreverse (before), Fnreverse (after));
4123 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4124 doc: /* Recenter the overlays of the current buffer around position POS.
4125 That makes overlay lookup faster for positions near POS (but perhaps slower
4126 for positions far away from POS). */)
4127 (Lisp_Object pos)
4129 CHECK_NUMBER_COERCE_MARKER (pos);
4131 recenter_overlay_lists (current_buffer, XINT (pos));
4132 return Qnil;
4135 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4136 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4137 (Lisp_Object overlay, Lisp_Object prop)
4139 CHECK_OVERLAY (overlay);
4140 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4143 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4144 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4145 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4147 Lisp_Object tail, buffer;
4148 int changed;
4150 CHECK_OVERLAY (overlay);
4152 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4154 for (tail = XOVERLAY (overlay)->plist;
4155 CONSP (tail) && CONSP (XCDR (tail));
4156 tail = XCDR (XCDR (tail)))
4157 if (EQ (XCAR (tail), prop))
4159 changed = !EQ (XCAR (XCDR (tail)), value);
4160 XSETCAR (XCDR (tail), value);
4161 goto found;
4163 /* It wasn't in the list, so add it to the front. */
4164 changed = !NILP (value);
4165 XOVERLAY (overlay)->plist
4166 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4167 found:
4168 if (! NILP (buffer))
4170 if (changed)
4171 modify_overlay (XBUFFER (buffer),
4172 marker_position (OVERLAY_START (overlay)),
4173 marker_position (OVERLAY_END (overlay)));
4174 if (EQ (prop, Qevaporate) && ! NILP (value)
4175 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4176 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4177 Fdelete_overlay (overlay);
4180 return value;
4183 /* Subroutine of report_overlay_modification. */
4185 /* Lisp vector holding overlay hook functions to call.
4186 Vector elements come in pairs.
4187 Each even-index element is a list of hook functions.
4188 The following odd-index element is the overlay they came from.
4190 Before the buffer change, we fill in this vector
4191 as we call overlay hook functions.
4192 After the buffer change, we get the functions to call from this vector.
4193 This way we always call the same functions before and after the change. */
4194 static Lisp_Object last_overlay_modification_hooks;
4196 /* Number of elements actually used in last_overlay_modification_hooks. */
4197 static int last_overlay_modification_hooks_used;
4199 /* Add one functionlist/overlay pair
4200 to the end of last_overlay_modification_hooks. */
4202 static void
4203 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4205 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4207 if (last_overlay_modification_hooks_used == oldsize)
4208 last_overlay_modification_hooks = larger_vector
4209 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4210 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4211 functionlist); last_overlay_modification_hooks_used++;
4212 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4213 overlay); last_overlay_modification_hooks_used++;
4216 /* Run the modification-hooks of overlays that include
4217 any part of the text in START to END.
4218 If this change is an insertion, also
4219 run the insert-before-hooks of overlay starting at END,
4220 and the insert-after-hooks of overlay ending at START.
4222 This is called both before and after the modification.
4223 AFTER is nonzero when we call after the modification.
4225 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4226 When AFTER is nonzero, they are the start position,
4227 the position after the inserted new text,
4228 and the length of deleted or replaced old text. */
4230 void
4231 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4232 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4234 Lisp_Object prop, overlay;
4235 struct Lisp_Overlay *tail;
4236 /* 1 if this change is an insertion. */
4237 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4238 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4240 overlay = Qnil;
4241 tail = NULL;
4243 /* We used to run the functions as soon as we found them and only register
4244 them in last_overlay_modification_hooks for the purpose of the `after'
4245 case. But running elisp code as we traverse the list of overlays is
4246 painful because the list can be modified by the elisp code so we had to
4247 copy at several places. We now simply do a read-only traversal that
4248 only collects the functions to run and we run them afterwards. It's
4249 simpler, especially since all the code was already there. -stef */
4251 if (!after)
4253 /* We are being called before a change.
4254 Scan the overlays to find the functions to call. */
4255 last_overlay_modification_hooks_used = 0;
4256 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4258 EMACS_INT startpos, endpos;
4259 Lisp_Object ostart, oend;
4261 XSETMISC (overlay, tail);
4263 ostart = OVERLAY_START (overlay);
4264 oend = OVERLAY_END (overlay);
4265 endpos = OVERLAY_POSITION (oend);
4266 if (XFASTINT (start) > endpos)
4267 break;
4268 startpos = OVERLAY_POSITION (ostart);
4269 if (insertion && (XFASTINT (start) == startpos
4270 || XFASTINT (end) == startpos))
4272 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4273 if (!NILP (prop))
4274 add_overlay_mod_hooklist (prop, overlay);
4276 if (insertion && (XFASTINT (start) == endpos
4277 || XFASTINT (end) == endpos))
4279 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4280 if (!NILP (prop))
4281 add_overlay_mod_hooklist (prop, overlay);
4283 /* Test for intersecting intervals. This does the right thing
4284 for both insertion and deletion. */
4285 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4287 prop = Foverlay_get (overlay, Qmodification_hooks);
4288 if (!NILP (prop))
4289 add_overlay_mod_hooklist (prop, overlay);
4293 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4295 EMACS_INT startpos, endpos;
4296 Lisp_Object ostart, oend;
4298 XSETMISC (overlay, tail);
4300 ostart = OVERLAY_START (overlay);
4301 oend = OVERLAY_END (overlay);
4302 startpos = OVERLAY_POSITION (ostart);
4303 endpos = OVERLAY_POSITION (oend);
4304 if (XFASTINT (end) < startpos)
4305 break;
4306 if (insertion && (XFASTINT (start) == startpos
4307 || XFASTINT (end) == startpos))
4309 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4310 if (!NILP (prop))
4311 add_overlay_mod_hooklist (prop, overlay);
4313 if (insertion && (XFASTINT (start) == endpos
4314 || XFASTINT (end) == endpos))
4316 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4317 if (!NILP (prop))
4318 add_overlay_mod_hooklist (prop, overlay);
4320 /* Test for intersecting intervals. This does the right thing
4321 for both insertion and deletion. */
4322 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4324 prop = Foverlay_get (overlay, Qmodification_hooks);
4325 if (!NILP (prop))
4326 add_overlay_mod_hooklist (prop, overlay);
4331 GCPRO4 (overlay, arg1, arg2, arg3);
4333 /* Call the functions recorded in last_overlay_modification_hooks.
4334 First copy the vector contents, in case some of these hooks
4335 do subsequent modification of the buffer. */
4336 int size = last_overlay_modification_hooks_used;
4337 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4338 int i;
4340 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4341 size * sizeof (Lisp_Object));
4342 gcpro1.var = copy;
4343 gcpro1.nvars = size;
4345 for (i = 0; i < size;)
4347 Lisp_Object prop, overlay;
4348 prop = copy[i++];
4349 overlay = copy[i++];
4350 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4353 UNGCPRO;
4356 static void
4357 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4358 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4360 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4362 GCPRO4 (list, arg1, arg2, arg3);
4364 while (CONSP (list))
4366 if (NILP (arg3))
4367 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4368 else
4369 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4370 list = XCDR (list);
4372 UNGCPRO;
4375 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4376 property is set. */
4377 void
4378 evaporate_overlays (EMACS_INT pos)
4380 Lisp_Object overlay, hit_list;
4381 struct Lisp_Overlay *tail;
4383 hit_list = Qnil;
4384 if (pos <= current_buffer->overlay_center)
4385 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4387 EMACS_INT endpos;
4388 XSETMISC (overlay, tail);
4389 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4390 if (endpos < pos)
4391 break;
4392 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4393 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4394 hit_list = Fcons (overlay, hit_list);
4396 else
4397 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4399 EMACS_INT startpos;
4400 XSETMISC (overlay, tail);
4401 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4402 if (startpos > pos)
4403 break;
4404 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4405 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4406 hit_list = Fcons (overlay, hit_list);
4408 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4409 Fdelete_overlay (XCAR (hit_list));
4412 /* Somebody has tried to store a value with an unacceptable type
4413 in the slot with offset OFFSET. */
4415 void
4416 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4418 Lisp_Object predicate;
4420 switch (type)
4422 case_Lisp_Int: predicate = Qintegerp; break;
4423 case Lisp_String: predicate = Qstringp; break;
4424 case Lisp_Symbol: predicate = Qsymbolp; break;
4425 default: abort ();
4428 wrong_type_argument (predicate, newval);
4432 /***********************************************************************
4433 Allocation with mmap
4434 ***********************************************************************/
4436 #ifdef USE_MMAP_FOR_BUFFERS
4438 #include <sys/types.h>
4439 #include <sys/mman.h>
4441 #ifndef MAP_ANON
4442 #ifdef MAP_ANONYMOUS
4443 #define MAP_ANON MAP_ANONYMOUS
4444 #else
4445 #define MAP_ANON 0
4446 #endif
4447 #endif
4449 #ifndef MAP_FAILED
4450 #define MAP_FAILED ((void *) -1)
4451 #endif
4453 #include <stdio.h>
4455 #if MAP_ANON == 0
4456 #include <fcntl.h>
4457 #endif
4459 #include "coding.h"
4462 /* Memory is allocated in regions which are mapped using mmap(2).
4463 The current implementation lets the system select mapped
4464 addresses; we're not using MAP_FIXED in general, except when
4465 trying to enlarge regions.
4467 Each mapped region starts with a mmap_region structure, the user
4468 area starts after that structure, aligned to MEM_ALIGN.
4470 +-----------------------+
4471 | struct mmap_info + |
4472 | padding |
4473 +-----------------------+
4474 | user data |
4477 +-----------------------+ */
4479 struct mmap_region
4481 /* User-specified size. */
4482 size_t nbytes_specified;
4484 /* Number of bytes mapped */
4485 size_t nbytes_mapped;
4487 /* Pointer to the location holding the address of the memory
4488 allocated with the mmap'd block. The variable actually points
4489 after this structure. */
4490 POINTER_TYPE **var;
4492 /* Next and previous in list of all mmap'd regions. */
4493 struct mmap_region *next, *prev;
4496 /* Doubly-linked list of mmap'd regions. */
4498 static struct mmap_region *mmap_regions;
4500 /* File descriptor for mmap. If we don't have anonymous mapping,
4501 /dev/zero will be opened on it. */
4503 static int mmap_fd;
4505 /* Temporary storage for mmap_set_vars, see there. */
4507 static struct mmap_region *mmap_regions_1;
4508 static int mmap_fd_1;
4510 /* Page size on this system. */
4512 static int mmap_page_size;
4514 /* 1 means mmap has been intialized. */
4516 static int mmap_initialized_p;
4518 /* Value is X rounded up to the next multiple of N. */
4520 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4522 /* Size of mmap_region structure plus padding. */
4524 #define MMAP_REGION_STRUCT_SIZE \
4525 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4527 /* Given a pointer P to the start of the user-visible part of a mapped
4528 region, return a pointer to the start of the region. */
4530 #define MMAP_REGION(P) \
4531 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4533 /* Given a pointer P to the start of a mapped region, return a pointer
4534 to the start of the user-visible part of the region. */
4536 #define MMAP_USER_AREA(P) \
4537 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4539 #define MEM_ALIGN sizeof (double)
4541 /* Predicate returning true if part of the address range [START .. END]
4542 is currently mapped. Used to prevent overwriting an existing
4543 memory mapping.
4545 Default is to conservativly assume the address range is occupied by
4546 something else. This can be overridden by system configuration
4547 files if system-specific means to determine this exists. */
4549 #ifndef MMAP_ALLOCATED_P
4550 #define MMAP_ALLOCATED_P(start, end) 1
4551 #endif
4553 /* Function prototypes. */
4555 static int mmap_free_1 (struct mmap_region *);
4556 static int mmap_enlarge (struct mmap_region *, int);
4557 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4558 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4559 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4560 static void mmap_free (POINTER_TYPE **ptr);
4561 static void mmap_init (void);
4564 /* Return a region overlapping address range START...END, or null if
4565 none. END is not including, i.e. the last byte in the range
4566 is at END - 1. */
4568 static struct mmap_region *
4569 mmap_find (start, end)
4570 POINTER_TYPE *start, *end;
4572 struct mmap_region *r;
4573 char *s = (char *) start, *e = (char *) end;
4575 for (r = mmap_regions; r; r = r->next)
4577 char *rstart = (char *) r;
4578 char *rend = rstart + r->nbytes_mapped;
4580 if (/* First byte of range, i.e. START, in this region? */
4581 (s >= rstart && s < rend)
4582 /* Last byte of range, i.e. END - 1, in this region? */
4583 || (e > rstart && e <= rend)
4584 /* First byte of this region in the range? */
4585 || (rstart >= s && rstart < e)
4586 /* Last byte of this region in the range? */
4587 || (rend > s && rend <= e))
4588 break;
4591 return r;
4595 /* Unmap a region. P is a pointer to the start of the user-araa of
4596 the region. Value is non-zero if successful. */
4598 static int
4599 mmap_free_1 (r)
4600 struct mmap_region *r;
4602 if (r->next)
4603 r->next->prev = r->prev;
4604 if (r->prev)
4605 r->prev->next = r->next;
4606 else
4607 mmap_regions = r->next;
4609 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4611 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4612 return 0;
4615 return 1;
4619 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4620 Value is non-zero if successful. */
4622 static int
4623 mmap_enlarge (r, npages)
4624 struct mmap_region *r;
4625 int npages;
4627 char *region_end = (char *) r + r->nbytes_mapped;
4628 size_t nbytes;
4629 int success = 0;
4631 if (npages < 0)
4633 /* Unmap pages at the end of the region. */
4634 nbytes = - npages * mmap_page_size;
4635 if (munmap (region_end - nbytes, nbytes) == -1)
4636 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4637 else
4639 r->nbytes_mapped -= nbytes;
4640 success = 1;
4643 else if (npages > 0)
4645 nbytes = npages * mmap_page_size;
4647 /* Try to map additional pages at the end of the region. We
4648 cannot do this if the address range is already occupied by
4649 something else because mmap deletes any previous mapping.
4650 I'm not sure this is worth doing, let's see. */
4651 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4653 POINTER_TYPE *p;
4655 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4656 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4657 if (p == MAP_FAILED)
4658 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4659 else if (p != (POINTER_TYPE *) region_end)
4661 /* Kernels are free to choose a different address. In
4662 that case, unmap what we've mapped above; we have
4663 no use for it. */
4664 if (munmap (p, nbytes) == -1)
4665 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4667 else
4669 r->nbytes_mapped += nbytes;
4670 success = 1;
4675 return success;
4679 /* Set or reset variables holding references to mapped regions. If
4680 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4681 non-zero, set all variables to the start of the user-areas
4682 of mapped regions.
4684 This function is called from Fdump_emacs to ensure that the dumped
4685 Emacs doesn't contain references to memory that won't be mapped
4686 when Emacs starts. */
4688 void
4689 mmap_set_vars (restore_p)
4690 int restore_p;
4692 struct mmap_region *r;
4694 if (restore_p)
4696 mmap_regions = mmap_regions_1;
4697 mmap_fd = mmap_fd_1;
4698 for (r = mmap_regions; r; r = r->next)
4699 *r->var = MMAP_USER_AREA (r);
4701 else
4703 for (r = mmap_regions; r; r = r->next)
4704 *r->var = NULL;
4705 mmap_regions_1 = mmap_regions;
4706 mmap_regions = NULL;
4707 mmap_fd_1 = mmap_fd;
4708 mmap_fd = -1;
4713 /* Allocate a block of storage large enough to hold NBYTES bytes of
4714 data. A pointer to the data is returned in *VAR. VAR is thus the
4715 address of some variable which will use the data area.
4717 The allocation of 0 bytes is valid.
4719 If we can't allocate the necessary memory, set *VAR to null, and
4720 return null. */
4722 static POINTER_TYPE *
4723 mmap_alloc (var, nbytes)
4724 POINTER_TYPE **var;
4725 size_t nbytes;
4727 void *p;
4728 size_t map;
4730 mmap_init ();
4732 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4733 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4734 mmap_fd, 0);
4736 if (p == MAP_FAILED)
4738 if (errno != ENOMEM)
4739 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4740 p = NULL;
4742 else
4744 struct mmap_region *r = (struct mmap_region *) p;
4746 r->nbytes_specified = nbytes;
4747 r->nbytes_mapped = map;
4748 r->var = var;
4749 r->prev = NULL;
4750 r->next = mmap_regions;
4751 if (r->next)
4752 r->next->prev = r;
4753 mmap_regions = r;
4755 p = MMAP_USER_AREA (p);
4758 return *var = p;
4762 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4763 resize it to size NBYTES. Change *VAR to reflect the new block,
4764 and return this value. If more memory cannot be allocated, then
4765 leave *VAR unchanged, and return null. */
4767 static POINTER_TYPE *
4768 mmap_realloc (var, nbytes)
4769 POINTER_TYPE **var;
4770 size_t nbytes;
4772 POINTER_TYPE *result;
4774 mmap_init ();
4776 if (*var == NULL)
4777 result = mmap_alloc (var, nbytes);
4778 else if (nbytes == 0)
4780 mmap_free (var);
4781 result = mmap_alloc (var, nbytes);
4783 else
4785 struct mmap_region *r = MMAP_REGION (*var);
4786 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4788 if (room < nbytes)
4790 /* Must enlarge. */
4791 POINTER_TYPE *old_ptr = *var;
4793 /* Try to map additional pages at the end of the region.
4794 If that fails, allocate a new region, copy data
4795 from the old region, then free it. */
4796 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4797 / mmap_page_size)))
4799 r->nbytes_specified = nbytes;
4800 *var = result = old_ptr;
4802 else if (mmap_alloc (var, nbytes))
4804 memcpy (*var, old_ptr, r->nbytes_specified);
4805 mmap_free_1 (MMAP_REGION (old_ptr));
4806 result = *var;
4807 r = MMAP_REGION (result);
4808 r->nbytes_specified = nbytes;
4810 else
4812 *var = old_ptr;
4813 result = NULL;
4816 else if (room - nbytes >= mmap_page_size)
4818 /* Shrinking by at least a page. Let's give some
4819 memory back to the system.
4821 The extra parens are to make the division happens first,
4822 on positive values, so we know it will round towards
4823 zero. */
4824 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4825 result = *var;
4826 r->nbytes_specified = nbytes;
4828 else
4830 /* Leave it alone. */
4831 result = *var;
4832 r->nbytes_specified = nbytes;
4836 return result;
4840 /* Free a block of relocatable storage whose data is pointed to by
4841 PTR. Store 0 in *PTR to show there's no block allocated. */
4843 static void
4844 mmap_free (var)
4845 POINTER_TYPE **var;
4847 mmap_init ();
4849 if (*var)
4851 mmap_free_1 (MMAP_REGION (*var));
4852 *var = NULL;
4857 /* Perform necessary intializations for the use of mmap. */
4859 static void
4860 mmap_init ()
4862 #if MAP_ANON == 0
4863 /* The value of mmap_fd is initially 0 in temacs, and -1
4864 in a dumped Emacs. */
4865 if (mmap_fd <= 0)
4867 /* No anonymous mmap -- we need the file descriptor. */
4868 mmap_fd = open ("/dev/zero", O_RDONLY);
4869 if (mmap_fd == -1)
4870 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4872 #endif /* MAP_ANON == 0 */
4874 if (mmap_initialized_p)
4875 return;
4876 mmap_initialized_p = 1;
4878 #if MAP_ANON != 0
4879 mmap_fd = -1;
4880 #endif
4882 mmap_page_size = getpagesize ();
4885 #endif /* USE_MMAP_FOR_BUFFERS */
4889 /***********************************************************************
4890 Buffer-text Allocation
4891 ***********************************************************************/
4893 #ifdef REL_ALLOC
4894 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4895 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4896 extern void r_alloc_free (POINTER_TYPE **ptr);
4897 #endif /* REL_ALLOC */
4900 /* Allocate NBYTES bytes for buffer B's text buffer. */
4902 static void
4903 alloc_buffer_text (struct buffer *b, size_t nbytes)
4905 POINTER_TYPE *p;
4907 BLOCK_INPUT;
4908 #if defined USE_MMAP_FOR_BUFFERS
4909 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4910 #elif defined REL_ALLOC
4911 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4912 #else
4913 p = xmalloc (nbytes);
4914 #endif
4916 if (p == NULL)
4918 UNBLOCK_INPUT;
4919 memory_full ();
4922 b->text->beg = (unsigned char *) p;
4923 UNBLOCK_INPUT;
4926 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4927 shrink it. */
4929 void
4930 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4932 POINTER_TYPE *p;
4933 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4934 + delta);
4935 BLOCK_INPUT;
4936 #if defined USE_MMAP_FOR_BUFFERS
4937 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4938 #elif defined REL_ALLOC
4939 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4940 #else
4941 p = xrealloc (b->text->beg, nbytes);
4942 #endif
4944 if (p == NULL)
4946 UNBLOCK_INPUT;
4947 memory_full ();
4950 BUF_BEG_ADDR (b) = (unsigned char *) p;
4951 UNBLOCK_INPUT;
4955 /* Free buffer B's text buffer. */
4957 static void
4958 free_buffer_text (struct buffer *b)
4960 BLOCK_INPUT;
4962 #if defined USE_MMAP_FOR_BUFFERS
4963 mmap_free ((POINTER_TYPE **) &b->text->beg);
4964 #elif defined REL_ALLOC
4965 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4966 #else
4967 xfree (b->text->beg);
4968 #endif
4970 BUF_BEG_ADDR (b) = NULL;
4971 UNBLOCK_INPUT;
4976 /***********************************************************************
4977 Initialization
4978 ***********************************************************************/
4980 void
4981 init_buffer_once (void)
4983 int idx;
4985 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
4987 /* Make sure all markable slots in buffer_defaults
4988 are initialized reasonably, so mark_buffer won't choke. */
4989 reset_buffer (&buffer_defaults);
4990 eassert (EQ (buffer_defaults.name, make_number (0)));
4991 reset_buffer_local_variables (&buffer_defaults, 1);
4992 eassert (EQ (buffer_local_symbols.name, make_number (0)));
4993 reset_buffer (&buffer_local_symbols);
4994 reset_buffer_local_variables (&buffer_local_symbols, 1);
4995 /* Prevent GC from getting confused. */
4996 buffer_defaults.text = &buffer_defaults.own_text;
4997 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4998 BUF_INTERVALS (&buffer_defaults) = 0;
4999 BUF_INTERVALS (&buffer_local_symbols) = 0;
5000 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5001 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5002 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5003 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5005 /* Set up the default values of various buffer slots. */
5006 /* Must do these before making the first buffer! */
5008 /* real setup is done in bindings.el */
5009 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5010 buffer_defaults.header_line_format = Qnil;
5011 buffer_defaults.abbrev_mode = Qnil;
5012 buffer_defaults.overwrite_mode = Qnil;
5013 buffer_defaults.case_fold_search = Qt;
5014 buffer_defaults.auto_fill_function = Qnil;
5015 buffer_defaults.selective_display = Qnil;
5016 #ifndef old
5017 buffer_defaults.selective_display_ellipses = Qt;
5018 #endif
5019 buffer_defaults.abbrev_table = Qnil;
5020 buffer_defaults.display_table = Qnil;
5021 buffer_defaults.undo_list = Qnil;
5022 buffer_defaults.mark_active = Qnil;
5023 buffer_defaults.file_format = Qnil;
5024 buffer_defaults.auto_save_file_format = Qt;
5025 buffer_defaults.overlays_before = NULL;
5026 buffer_defaults.overlays_after = NULL;
5027 buffer_defaults.overlay_center = BEG;
5029 XSETFASTINT (buffer_defaults.tab_width, 8);
5030 buffer_defaults.truncate_lines = Qnil;
5031 buffer_defaults.word_wrap = Qnil;
5032 buffer_defaults.ctl_arrow = Qt;
5033 buffer_defaults.bidi_display_reordering = Qnil;
5034 buffer_defaults.bidi_paragraph_direction = Qnil;
5035 buffer_defaults.cursor_type = Qt;
5036 buffer_defaults.extra_line_spacing = Qnil;
5037 buffer_defaults.cursor_in_non_selected_windows = Qt;
5039 #ifdef DOS_NT
5040 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5041 #endif
5042 buffer_defaults.enable_multibyte_characters = Qt;
5043 buffer_defaults.buffer_file_coding_system = Qnil;
5044 XSETFASTINT (buffer_defaults.fill_column, 70);
5045 XSETFASTINT (buffer_defaults.left_margin, 0);
5046 buffer_defaults.cache_long_line_scans = Qnil;
5047 buffer_defaults.file_truename = Qnil;
5048 XSETFASTINT (buffer_defaults.display_count, 0);
5049 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5050 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5051 buffer_defaults.left_fringe_width = Qnil;
5052 buffer_defaults.right_fringe_width = Qnil;
5053 buffer_defaults.fringes_outside_margins = Qnil;
5054 buffer_defaults.scroll_bar_width = Qnil;
5055 buffer_defaults.vertical_scroll_bar_type = Qt;
5056 buffer_defaults.indicate_empty_lines = Qnil;
5057 buffer_defaults.indicate_buffer_boundaries = Qnil;
5058 buffer_defaults.fringe_indicator_alist = Qnil;
5059 buffer_defaults.fringe_cursor_alist = Qnil;
5060 buffer_defaults.scroll_up_aggressively = Qnil;
5061 buffer_defaults.scroll_down_aggressively = Qnil;
5062 buffer_defaults.display_time = Qnil;
5064 /* Assign the local-flags to the slots that have default values.
5065 The local flag is a bit that is used in the buffer
5066 to say that it has its own local value for the slot.
5067 The local flag bits are in the local_var_flags slot of the buffer. */
5069 /* Nothing can work if this isn't true */
5070 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5072 /* 0 means not a lisp var, -1 means always local, else mask */
5073 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
5074 XSETINT (buffer_local_flags.filename, -1);
5075 XSETINT (buffer_local_flags.directory, -1);
5076 XSETINT (buffer_local_flags.backed_up, -1);
5077 XSETINT (buffer_local_flags.save_length, -1);
5078 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5079 XSETINT (buffer_local_flags.read_only, -1);
5080 XSETINT (buffer_local_flags.major_mode, -1);
5081 XSETINT (buffer_local_flags.mode_name, -1);
5082 XSETINT (buffer_local_flags.undo_list, -1);
5083 XSETINT (buffer_local_flags.mark_active, -1);
5084 XSETINT (buffer_local_flags.point_before_scroll, -1);
5085 XSETINT (buffer_local_flags.file_truename, -1);
5086 XSETINT (buffer_local_flags.invisibility_spec, -1);
5087 XSETINT (buffer_local_flags.file_format, -1);
5088 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5089 XSETINT (buffer_local_flags.display_count, -1);
5090 XSETINT (buffer_local_flags.display_time, -1);
5091 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5093 idx = 1;
5094 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5095 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5096 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5097 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5098 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5099 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5100 #ifndef old
5101 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5102 #endif
5103 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5104 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5105 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5106 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5107 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5108 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5109 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5110 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5111 #ifdef DOS_NT
5112 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5113 /* Make this one a permanent local. */
5114 buffer_permanent_local_flags[idx++] = 1;
5115 #endif
5116 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5117 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5118 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5119 XSETFASTINT (buffer_local_flags.bidi_display_reordering, idx); ++idx;
5120 XSETFASTINT (buffer_local_flags.bidi_paragraph_direction, idx); ++idx;
5121 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5122 /* Make this one a permanent local. */
5123 buffer_permanent_local_flags[idx++] = 1;
5124 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5125 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5126 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5127 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5128 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5129 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5131 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5132 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5133 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5134 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5135 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5136 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5137 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5138 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5139 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5140 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5142 /* Need more room? */
5143 if (idx >= MAX_PER_BUFFER_VARS)
5144 abort ();
5145 last_per_buffer_idx = idx;
5147 Vbuffer_alist = Qnil;
5148 current_buffer = 0;
5149 all_buffers = 0;
5151 QSFundamental = make_pure_c_string ("Fundamental");
5153 Qfundamental_mode = intern_c_string ("fundamental-mode");
5154 buffer_defaults.major_mode = Qfundamental_mode;
5156 Qmode_class = intern_c_string ("mode-class");
5158 Qprotected_field = intern_c_string ("protected-field");
5160 Qpermanent_local = intern_c_string ("permanent-local");
5162 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5163 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5165 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5167 /* super-magic invisible buffer */
5168 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5169 Vbuffer_alist = Qnil;
5171 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5173 inhibit_modification_hooks = 0;
5176 void
5177 init_buffer (void)
5179 char *pwd;
5180 Lisp_Object temp;
5181 int len;
5183 #ifdef USE_MMAP_FOR_BUFFERS
5185 /* When using the ralloc implementation based on mmap(2), buffer
5186 text pointers will have been set to null in the dumped Emacs.
5187 Map new memory. */
5188 struct buffer *b;
5190 for (b = all_buffers; b; b = b->next)
5191 if (b->text->beg == NULL)
5192 enlarge_buffer_text (b, 0);
5194 #endif /* USE_MMAP_FOR_BUFFERS */
5196 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5197 if (NILP (buffer_defaults.enable_multibyte_characters))
5198 Fset_buffer_multibyte (Qnil);
5200 pwd = get_current_dir_name ();
5202 if (!pwd)
5203 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5205 /* Maybe this should really use some standard subroutine
5206 whose definition is filename syntax dependent. */
5207 len = strlen (pwd);
5208 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5210 /* Grow buffer to add directory separator and '\0'. */
5211 pwd = (char *) realloc (pwd, len + 2);
5212 if (!pwd)
5213 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5214 pwd[len] = DIRECTORY_SEP;
5215 pwd[len + 1] = '\0';
5218 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5219 if (! NILP (buffer_defaults.enable_multibyte_characters))
5220 /* At this moment, we still don't know how to decode the
5221 directory name. So, we keep the bytes in multibyte form so
5222 that ENCODE_FILE correctly gets the original bytes. */
5223 current_buffer->directory
5224 = string_to_multibyte (current_buffer->directory);
5226 /* Add /: to the front of the name
5227 if it would otherwise be treated as magic. */
5228 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5229 if (! NILP (temp)
5230 /* If the default dir is just /, TEMP is non-nil
5231 because of the ange-ftp completion handler.
5232 However, it is not necessary to turn / into /:/.
5233 So avoid doing that. */
5234 && strcmp ("/", SDATA (current_buffer->directory)))
5235 current_buffer->directory
5236 = concat2 (build_string ("/:"), current_buffer->directory);
5238 temp = get_minibuffer (0);
5239 XBUFFER (temp)->directory = current_buffer->directory;
5241 free (pwd);
5244 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5245 Object stored in the current buffer. address is the address of the slot
5246 in the buffer that is current now. */
5248 /* TYPE is nil for a general Lisp variable.
5249 An integer specifies a type; then only Lisp values
5250 with that type code are allowed (except that nil is allowed too).
5251 LNAME is the Lisp-level variable name.
5252 VNAME is the name of the buffer slot.
5253 DOC is a dummy where you write the doc string as a comment. */
5254 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5255 do { \
5256 static struct Lisp_Buffer_Objfwd bo_fwd; \
5257 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5258 } while (0)
5260 static void
5261 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
5262 Lisp_Object *address, Lisp_Object type, char *doc)
5264 struct Lisp_Symbol *sym;
5265 int offset;
5267 sym = XSYMBOL (intern (namestring));
5268 offset = (char *)address - (char *)current_buffer;
5270 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5271 bo_fwd->offset = offset;
5272 bo_fwd->slottype = type;
5273 sym->redirect = SYMBOL_FORWARDED;
5275 /* I tried to do the job without a cast, but it seems impossible.
5276 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5277 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5279 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5281 if (PER_BUFFER_IDX (offset) == 0)
5282 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5283 slot of buffer_local_flags */
5284 abort ();
5288 /* initialize the buffer routines */
5289 void
5290 syms_of_buffer (void)
5292 staticpro (&last_overlay_modification_hooks);
5293 last_overlay_modification_hooks
5294 = Fmake_vector (make_number (10), Qnil);
5296 staticpro (&Vbuffer_defaults);
5297 staticpro (&Vbuffer_local_symbols);
5298 staticpro (&Qfundamental_mode);
5299 staticpro (&Qmode_class);
5300 staticpro (&QSFundamental);
5301 staticpro (&Vbuffer_alist);
5302 staticpro (&Qprotected_field);
5303 staticpro (&Qpermanent_local);
5304 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5305 staticpro (&Qpermanent_local_hook);
5306 staticpro (&Qkill_buffer_hook);
5307 Qoverlayp = intern_c_string ("overlayp");
5308 staticpro (&Qoverlayp);
5309 Qevaporate = intern_c_string ("evaporate");
5310 staticpro (&Qevaporate);
5311 Qmodification_hooks = intern_c_string ("modification-hooks");
5312 staticpro (&Qmodification_hooks);
5313 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5314 staticpro (&Qinsert_in_front_hooks);
5315 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5316 staticpro (&Qinsert_behind_hooks);
5317 Qget_file_buffer = intern_c_string ("get-file-buffer");
5318 staticpro (&Qget_file_buffer);
5319 Qpriority = intern_c_string ("priority");
5320 staticpro (&Qpriority);
5321 Qbefore_string = intern_c_string ("before-string");
5322 staticpro (&Qbefore_string);
5323 Qafter_string = intern_c_string ("after-string");
5324 staticpro (&Qafter_string);
5325 Qfirst_change_hook = intern_c_string ("first-change-hook");
5326 staticpro (&Qfirst_change_hook);
5327 Qbefore_change_functions = intern_c_string ("before-change-functions");
5328 staticpro (&Qbefore_change_functions);
5329 Qafter_change_functions = intern_c_string ("after-change-functions");
5330 staticpro (&Qafter_change_functions);
5331 /* The next one is initialized in init_buffer_once. */
5332 staticpro (&Qucs_set_table_for_input);
5334 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5335 staticpro (&Qkill_buffer_query_functions);
5337 Fput (Qprotected_field, Qerror_conditions,
5338 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5339 Fput (Qprotected_field, Qerror_message,
5340 make_pure_c_string ("Attempt to modify a protected field"));
5342 /* All these use DEFVAR_LISP_NOPRO because the slots in
5343 buffer_defaults will all be marked via Vbuffer_defaults. */
5345 DEFVAR_BUFFER_DEFAULTS ("default-mode-line-format",
5346 mode_line_format,
5347 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5348 This is the same as (default-value 'mode-line-format). */);
5350 DEFVAR_BUFFER_DEFAULTS ("default-header-line-format",
5351 header_line_format,
5352 doc: /* Default value of `header-line-format' for buffers that don't override it.
5353 This is the same as (default-value 'header-line-format). */);
5355 DEFVAR_BUFFER_DEFAULTS ("default-cursor-type", cursor_type,
5356 doc: /* Default value of `cursor-type' for buffers that don't override it.
5357 This is the same as (default-value 'cursor-type). */);
5359 DEFVAR_BUFFER_DEFAULTS ("default-line-spacing",
5360 extra_line_spacing,
5361 doc: /* Default value of `line-spacing' for buffers that don't override it.
5362 This is the same as (default-value 'line-spacing). */);
5364 DEFVAR_BUFFER_DEFAULTS ("default-cursor-in-non-selected-windows",
5365 cursor_in_non_selected_windows,
5366 doc: /* Default value of `cursor-in-non-selected-windows'.
5367 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5369 DEFVAR_BUFFER_DEFAULTS ("default-abbrev-mode",
5370 abbrev_mode,
5371 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5372 This is the same as (default-value 'abbrev-mode). */);
5374 DEFVAR_BUFFER_DEFAULTS ("default-ctl-arrow",
5375 ctl_arrow,
5376 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5377 This is the same as (default-value 'ctl-arrow). */);
5379 DEFVAR_BUFFER_DEFAULTS ("default-enable-multibyte-characters",
5380 enable_multibyte_characters,
5381 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5382 This is the same as (default-value 'enable-multibyte-characters). */);
5384 DEFVAR_BUFFER_DEFAULTS ("default-buffer-file-coding-system",
5385 buffer_file_coding_system,
5386 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5387 This is the same as (default-value 'buffer-file-coding-system). */);
5389 DEFVAR_BUFFER_DEFAULTS ("default-truncate-lines",
5390 truncate_lines,
5391 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5392 This is the same as (default-value 'truncate-lines). */);
5394 DEFVAR_BUFFER_DEFAULTS ("default-fill-column",
5395 fill_column,
5396 doc: /* Default value of `fill-column' for buffers that do not override it.
5397 This is the same as (default-value 'fill-column). */);
5399 DEFVAR_BUFFER_DEFAULTS ("default-left-margin",
5400 left_margin,
5401 doc: /* Default value of `left-margin' for buffers that do not override it.
5402 This is the same as (default-value 'left-margin). */);
5404 DEFVAR_BUFFER_DEFAULTS ("default-tab-width",
5405 tab_width,
5406 doc: /* Default value of `tab-width' for buffers that do not override it.
5407 This is the same as (default-value 'tab-width). */);
5409 DEFVAR_BUFFER_DEFAULTS ("default-case-fold-search",
5410 case_fold_search,
5411 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5412 This is the same as (default-value 'case-fold-search). */);
5414 #ifdef DOS_NT
5415 DEFVAR_BUFFER_DEFAULTS ("default-buffer-file-type",
5416 buffer_file_type,
5417 doc: /* Default file type for buffers that do not override it.
5418 This is the same as (default-value 'buffer-file-type).
5419 The file type is nil for text, t for binary. */);
5420 #endif
5422 DEFVAR_BUFFER_DEFAULTS ("default-left-margin-width",
5423 left_margin_cols,
5424 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5425 This is the same as (default-value 'left-margin-width). */);
5427 DEFVAR_BUFFER_DEFAULTS ("default-right-margin-width",
5428 right_margin_cols,
5429 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5430 This is the same as (default-value 'right-margin-width). */);
5432 DEFVAR_BUFFER_DEFAULTS ("default-left-fringe-width",
5433 left_fringe_width,
5434 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5435 This is the same as (default-value 'left-fringe-width). */);
5437 DEFVAR_BUFFER_DEFAULTS ("default-right-fringe-width",
5438 right_fringe_width,
5439 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5440 This is the same as (default-value 'right-fringe-width). */);
5442 DEFVAR_BUFFER_DEFAULTS ("default-fringes-outside-margins",
5443 fringes_outside_margins,
5444 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5445 This is the same as (default-value 'fringes-outside-margins). */);
5447 DEFVAR_BUFFER_DEFAULTS ("default-scroll-bar-width",
5448 scroll_bar_width,
5449 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5450 This is the same as (default-value 'scroll-bar-width). */);
5452 DEFVAR_BUFFER_DEFAULTS ("default-vertical-scroll-bar",
5453 vertical_scroll_bar_type,
5454 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5455 This is the same as (default-value 'vertical-scroll-bar). */);
5457 DEFVAR_BUFFER_DEFAULTS ("default-indicate-empty-lines",
5458 indicate_empty_lines,
5459 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5460 This is the same as (default-value 'indicate-empty-lines). */);
5462 DEFVAR_BUFFER_DEFAULTS ("default-indicate-buffer-boundaries",
5463 indicate_buffer_boundaries,
5464 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5465 This is the same as (default-value 'indicate-buffer-boundaries). */);
5467 DEFVAR_BUFFER_DEFAULTS ("default-fringe-indicator-alist",
5468 fringe_indicator_alist,
5469 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5470 This is the same as (default-value 'fringe-indicator-alist'). */);
5472 DEFVAR_BUFFER_DEFAULTS ("default-fringe-cursor-alist",
5473 fringe_cursor_alist,
5474 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5475 This is the same as (default-value 'fringe-cursor-alist'). */);
5477 DEFVAR_BUFFER_DEFAULTS ("default-scroll-up-aggressively",
5478 scroll_up_aggressively,
5479 doc: /* Default value of `scroll-up-aggressively'.
5480 This value applies in buffers that don't have their own local values.
5481 This is the same as (default-value 'scroll-up-aggressively). */);
5483 DEFVAR_BUFFER_DEFAULTS ("default-scroll-down-aggressively",
5484 scroll_down_aggressively,
5485 doc: /* Default value of `scroll-down-aggressively'.
5486 This value applies in buffers that don't have their own local values.
5487 This is the same as (default-value 'scroll-down-aggressively). */);
5489 DEFVAR_PER_BUFFER ("header-line-format",
5490 &current_buffer->header_line_format,
5491 Qnil,
5492 doc: /* Analogous to `mode-line-format', but controls the header line.
5493 The header line appears, optionally, at the top of a window;
5494 the mode line appears at the bottom. */);
5496 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5497 Qnil,
5498 doc: /* Template for displaying mode line for current buffer.
5499 Each buffer has its own value of this variable.
5500 Value may be nil, a string, a symbol or a list or cons cell.
5501 A value of nil means don't display a mode line.
5502 For a symbol, its value is used (but it is ignored if t or nil).
5503 A string appearing directly as the value of a symbol is processed verbatim
5504 in that the %-constructs below are not recognized.
5505 Note that unless the symbol is marked as a `risky-local-variable', all
5506 properties in any strings, as well as all :eval and :propertize forms
5507 in the value of that symbol will be ignored.
5508 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5509 is used as a mode line element. Be careful--FORM should not load any files,
5510 because that can cause an infinite recursion.
5511 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5512 with the specified properties PROPS applied.
5513 For a list whose car is a symbol, the symbol's value is taken,
5514 and if that is non-nil, the cadr of the list is processed recursively.
5515 Otherwise, the caddr of the list (if there is one) is processed.
5516 For a list whose car is a string or list, each element is processed
5517 recursively and the results are effectively concatenated.
5518 For a list whose car is an integer, the cdr of the list is processed
5519 and padded (if the number is positive) or truncated (if negative)
5520 to the width specified by that number.
5521 A string is printed verbatim in the mode line except for %-constructs:
5522 (%-constructs are allowed when the string is the entire mode-line-format
5523 or when it is found in a cons-cell or a list)
5524 %b -- print buffer name. %f -- print visited file name.
5525 %F -- print frame name.
5526 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5527 %& is like %*, but ignore read-only-ness.
5528 % means buffer is read-only and * means it is modified.
5529 For a modified read-only buffer, %* gives % and %+ gives *.
5530 %s -- print process status. %l -- print the current line number.
5531 %c -- print the current column number (this makes editing slower).
5532 To make the column number update correctly in all cases,
5533 `column-number-mode' must be non-nil.
5534 %i -- print the size of the buffer.
5535 %I -- like %i, but use k, M, G, etc., to abbreviate.
5536 %p -- print percent of buffer above top of window, or Top, Bot or All.
5537 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5538 or print Bottom or All.
5539 %n -- print Narrow if appropriate.
5540 %t -- visited file is text or binary (if OS supports this distinction).
5541 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5542 %Z -- like %z, but including the end-of-line format.
5543 %e -- print error message about full memory.
5544 %@ -- print @ or hyphen. @ means that default-directory is on a
5545 remote machine.
5546 %[ -- print one [ for each recursive editing level. %] similar.
5547 %% -- print %. %- -- print infinitely many dashes.
5548 Decimal digits after the % specify field width to which to pad. */);
5550 DEFVAR_BUFFER_DEFAULTS ("default-major-mode", major_mode,
5551 doc: /* *Value of `major-mode' for new buffers. */);
5553 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5554 make_number (Lisp_Symbol),
5555 doc: /* Symbol for current buffer's major mode.
5556 The default value (normally `fundamental-mode') affects new buffers.
5557 A value of nil means to use the current buffer's major mode, provided
5558 it is not marked as "special".
5560 When a mode is used by default, `find-file' switches to it before it
5561 reads the contents into the buffer and before it finishes setting up
5562 the buffer. Thus, the mode and its hooks should not expect certain
5563 variables such as `buffer-read-only' and `buffer-file-coding-system'
5564 to be set up. */);
5566 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5567 Qnil,
5568 doc: /* Pretty name of current buffer's major mode.
5569 Usually a string, but can use any of the constructs for `mode-line-format',
5570 which see.
5571 Format with `format-mode-line' to produce a string value. */);
5573 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5574 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5576 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5577 doc: /* Non-nil if Abbrev mode is enabled.
5578 Use the command `abbrev-mode' to change this variable. */);
5580 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5581 Qnil,
5582 doc: /* *Non-nil if searches and matches should ignore case. */);
5584 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5585 make_number (LISP_INT_TAG),
5586 doc: /* *Column beyond which automatic line-wrapping should happen.
5587 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5589 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5590 make_number (LISP_INT_TAG),
5591 doc: /* *Column for the default `indent-line-function' to indent to.
5592 Linefeed indents to this column in Fundamental mode. */);
5594 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5595 make_number (LISP_INT_TAG),
5596 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5598 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5599 doc: /* *Non-nil means display control chars with uparrow.
5600 A value of nil means use backslash and octal digits.
5601 This variable does not apply to characters whose display is specified
5602 in the current display table (if there is one). */);
5604 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5605 &current_buffer->enable_multibyte_characters,
5606 Qnil,
5607 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5608 Otherwise they are regarded as unibyte. This affects the display,
5609 file I/O and the behavior of various editing commands.
5611 This variable is buffer-local but you cannot set it directly;
5612 use the function `set-buffer-multibyte' to change a buffer's representation.
5613 Changing its default value with `setq-default' is supported.
5614 See also variable `default-enable-multibyte-characters' and Info node
5615 `(elisp)Text Representations'. */);
5616 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5618 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5619 &current_buffer->buffer_file_coding_system, Qnil,
5620 doc: /* Coding system to be used for encoding the buffer contents on saving.
5621 This variable applies to saving the buffer, and also to `write-region'
5622 and other functions that use `write-region'.
5623 It does not apply to sending output to subprocesses, however.
5625 If this is nil, the buffer is saved without any code conversion
5626 unless some coding system is specified in `file-coding-system-alist'
5627 for the buffer file.
5629 If the text to be saved cannot be encoded as specified by this variable,
5630 an alternative encoding is selected by `select-safe-coding-system', which see.
5632 The variable `coding-system-for-write', if non-nil, overrides this variable.
5634 This variable is never applied to a way of decoding a file while reading it. */);
5636 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5637 &current_buffer->bidi_display_reordering, Qnil,
5638 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5640 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5641 &current_buffer->bidi_paragraph_direction, Qnil,
5642 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5644 If this is nil (the default), the direction of each paragraph is
5645 determined by the first strong directional character of its text.
5646 The values of `right-to-left' and `left-to-right' override that.
5647 Any other value is treated as nil.
5649 This variable has no effect unless the buffer's value of
5650 \`bidi-display-reordering' is non-nil. */);
5652 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5653 doc: /* *Non-nil means do not display continuation lines.
5654 Instead, give each line of text just one screen line.
5656 Note that this is overridden by the variable
5657 `truncate-partial-width-windows' if that variable is non-nil
5658 and this buffer is not full-frame width. */);
5660 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5661 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5662 When word-wrapping is on, continuation lines are wrapped at the space
5663 or tab character nearest to the right window edge.
5664 If nil, continuation lines are wrapped at the right screen edge.
5666 This variable has no effect if long lines are truncated (see
5667 `truncate-lines' and `truncate-partial-width-windows'). If you use
5668 word-wrapping, you might want to reduce the value of
5669 `truncate-partial-width-windows', since wrapping can make text readable
5670 in narrower windows. */);
5672 #ifdef DOS_NT
5673 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5674 Qnil,
5675 doc: /* Non-nil if the visited file is a binary file.
5676 This variable is meaningful on MS-DOG and Windows NT.
5677 On those systems, it is automatically local in every buffer.
5678 On other systems, this variable is normally always nil. */);
5679 #endif
5681 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5682 make_number (Lisp_String),
5683 doc: /* Name of default directory of current buffer. Should end with slash.
5684 To interactively change the default directory, use command `cd'. */);
5686 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5687 Qnil,
5688 doc: /* Function called (if non-nil) to perform auto-fill.
5689 It is called after self-inserting any character specified in
5690 the `auto-fill-chars' table.
5691 NOTE: This variable is not a hook;
5692 its value may not be a list of functions. */);
5694 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5695 make_number (Lisp_String),
5696 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5698 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5699 make_number (Lisp_String),
5700 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5701 The truename of a file is calculated by `file-truename'
5702 and then abbreviated with `abbreviate-file-name'. */);
5704 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5705 &current_buffer->auto_save_file_name,
5706 make_number (Lisp_String),
5707 doc: /* Name of file for auto-saving current buffer.
5708 If it is nil, that means don't auto-save this buffer. */);
5710 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5711 doc: /* Non-nil if this buffer is read-only. */);
5713 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5714 doc: /* Non-nil if this buffer's file has been backed up.
5715 Backing up is done before the first time the file is saved. */);
5717 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5718 make_number (LISP_INT_TAG),
5719 doc: /* Length of current buffer when last read in, saved or auto-saved.
5720 0 initially.
5721 -1 means auto-saving turned off until next real save.
5723 If you set this to -2, that means don't turn off auto-saving in this buffer
5724 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5725 you probably should set this to -2 in that buffer. */);
5727 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5728 Qnil,
5729 doc: /* Non-nil enables selective display.
5730 An integer N as value means display only lines
5731 that start with less than N columns of space.
5732 A value of t means that the character ^M makes itself and
5733 all the rest of the line invisible; also, when saving the buffer
5734 in a file, save the ^M as a newline. */);
5736 #ifndef old
5737 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5738 &current_buffer->selective_display_ellipses,
5739 Qnil,
5740 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5741 #endif
5743 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5744 doc: /* Non-nil if self-insertion should replace existing text.
5745 The value should be one of `overwrite-mode-textual',
5746 `overwrite-mode-binary', or nil.
5747 If it is `overwrite-mode-textual', self-insertion still
5748 inserts at the end of a line, and inserts when point is before a tab,
5749 until the tab is filled in.
5750 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5752 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5753 Qnil,
5754 doc: /* Display table that controls display of the contents of current buffer.
5756 If this variable is nil, the value of `standard-display-table' is used.
5757 Each window can have its own, overriding display table, see
5758 `set-window-display-table' and `window-display-table'.
5760 The display table is a char-table created with `make-display-table'.
5761 A char-table is an array indexed by character codes. Normal array
5762 primitives `aref' and `aset' can be used to access elements of a char-table.
5764 Each of the char-table elements control how to display the corresponding
5765 text character: the element at index C in the table says how to display
5766 the character whose code is C. Each element should be a vector of
5767 characters or nil. The value nil means display the character in the
5768 default fashion; otherwise, the characters from the vector are delivered
5769 to the screen instead of the original character.
5771 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5772 to display a capital Y instead of each X character.
5774 In addition, a char-table has six extra slots to control the display of:
5776 the end of a truncated screen line (extra-slot 0, a single character);
5777 the end of a continued line (extra-slot 1, a single character);
5778 the escape character used to display character codes in octal
5779 (extra-slot 2, a single character);
5780 the character used as an arrow for control characters (extra-slot 3,
5781 a single character);
5782 the decoration indicating the presence of invisible lines (extra-slot 4,
5783 a vector of characters);
5784 the character used to draw the border between side-by-side windows
5785 (extra-slot 5, a single character).
5787 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5789 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5790 Qnil,
5791 doc: /* *Width of left marginal area for display of a buffer.
5792 A value of nil means no marginal area. */);
5794 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5795 Qnil,
5796 doc: /* *Width of right marginal area for display of a buffer.
5797 A value of nil means no marginal area. */);
5799 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5800 Qnil,
5801 doc: /* *Width of this buffer's left fringe (in pixels).
5802 A value of 0 means no left fringe is shown in this buffer's window.
5803 A value of nil means to use the left fringe width from the window's frame. */);
5805 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5806 Qnil,
5807 doc: /* *Width of this buffer's right fringe (in pixels).
5808 A value of 0 means no right fringe is shown in this buffer's window.
5809 A value of nil means to use the right fringe width from the window's frame. */);
5811 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5812 Qnil,
5813 doc: /* *Non-nil means to display fringes outside display margins.
5814 A value of nil means to display fringes between margins and buffer text. */);
5816 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5817 Qnil,
5818 doc: /* *Width of this buffer's scroll bars in pixels.
5819 A value of nil means to use the scroll bar width from the window's frame. */);
5821 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5822 Qnil,
5823 doc: /* *Position of this buffer's vertical scroll bar.
5824 The value takes effect whenever you tell a window to display this buffer;
5825 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5827 A value of `left' or `right' means put the vertical scroll bar at that side
5828 of the window; a value of nil means don't show any vertical scroll bars.
5829 A value of t (the default) means do whatever the window's frame specifies. */);
5831 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5832 &current_buffer->indicate_empty_lines, Qnil,
5833 doc: /* *Visually indicate empty lines after the buffer end.
5834 If non-nil, a bitmap is displayed in the left fringe of a window on
5835 window-systems. */);
5837 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5838 &current_buffer->indicate_buffer_boundaries, Qnil,
5839 doc: /* *Visually indicate buffer boundaries and scrolling.
5840 If non-nil, the first and last line of the buffer are marked in the fringe
5841 of a window on window-systems with angle bitmaps, or if the window can be
5842 scrolled, the top and bottom line of the window are marked with up and down
5843 arrow bitmaps.
5845 If value is a symbol `left' or `right', both angle and arrow bitmaps
5846 are displayed in the left or right fringe, resp. Any other value
5847 that doesn't look like an alist means display the angle bitmaps in
5848 the left fringe but no arrows.
5850 You can exercise more precise control by using an alist as the
5851 value. Each alist element (INDICATOR . POSITION) specifies
5852 where to show one of the indicators. INDICATOR is one of `top',
5853 `bottom', `up', `down', or t, which specifies the default position,
5854 and POSITION is one of `left', `right', or nil, meaning do not show
5855 this indicator.
5857 For example, ((top . left) (t . right)) places the top angle bitmap in
5858 left fringe, the bottom angle bitmap in right fringe, and both arrow
5859 bitmaps in right fringe. To show just the angle bitmaps in the left
5860 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5862 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5863 &current_buffer->fringe_indicator_alist, Qnil,
5864 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5865 The value is an alist where each element (INDICATOR . BITMAPS)
5866 specifies the fringe bitmaps used to display a specific logical
5867 fringe indicator.
5869 INDICATOR specifies the logical indicator type which is one of the
5870 following symbols: `truncation' , `continuation', `overlay-arrow',
5871 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5873 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5874 the actual bitmap shown in the left or right fringe for the logical
5875 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5876 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5877 are used only for the `bottom' and `top-bottom' indicators when the
5878 last (only) line has no final newline. BITMAPS may also be a single
5879 symbol which is used in both left and right fringes. */);
5881 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5882 &current_buffer->fringe_cursor_alist, Qnil,
5883 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5884 The value is an alist where each element (CURSOR . BITMAP)
5885 specifies the fringe bitmaps used to display a specific logical
5886 cursor type in the fringe.
5888 CURSOR specifies the logical cursor type which is one of the following
5889 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5890 one is used to show a hollow cursor on narrow lines display lines
5891 where the normal hollow cursor will not fit.
5893 BITMAP is the corresponding fringe bitmap shown for the logical
5894 cursor type. */);
5896 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5897 &current_buffer->scroll_up_aggressively, Qnil,
5898 doc: /* How far to scroll windows upward.
5899 If you move point off the bottom, the window scrolls automatically.
5900 This variable controls how far it scrolls. The value nil, the default,
5901 means scroll to center point. A fraction means scroll to put point
5902 that fraction of the window's height from the bottom of the window.
5903 When the value is 0.0, point goes at the bottom line, which in the
5904 simple case that you moved off with C-f means scrolling just one line.
5905 1.0 means point goes at the top, so that in that simple case, the
5906 window scrolls by a full window height. Meaningful values are
5907 between 0.0 and 1.0, inclusive. */);
5909 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5910 &current_buffer->scroll_down_aggressively, Qnil,
5911 doc: /* How far to scroll windows downward.
5912 If you move point off the top, the window scrolls automatically.
5913 This variable controls how far it scrolls. The value nil, the default,
5914 means scroll to center point. A fraction means scroll to put point
5915 that fraction of the window's height from the top of the window.
5916 When the value is 0.0, point goes at the top line, which in the
5917 simple case that you moved off with C-b means scrolling just one line.
5918 1.0 means point goes at the bottom, so that in that simple case, the
5919 window scrolls by a full window height. Meaningful values are
5920 between 0.0 and 1.0, inclusive. */);
5922 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5923 "Don't ask.");
5926 DEFVAR_LISP ("before-change-functions", Vbefore_change_functions,
5927 doc: /* List of functions to call before each text change.
5928 Two arguments are passed to each function: the positions of
5929 the beginning and end of the range of old text to be changed.
5930 \(For an insertion, the beginning and end are at the same place.)
5931 No information is given about the length of the text after the change.
5933 Buffer changes made while executing the `before-change-functions'
5934 don't call any before-change or after-change functions.
5935 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5937 If an unhandled error happens in running these functions,
5938 the variable's value remains nil. That prevents the error
5939 from happening repeatedly and making Emacs nonfunctional. */);
5940 Vbefore_change_functions = Qnil;
5942 DEFVAR_LISP ("after-change-functions", Vafter_change_functions,
5943 doc: /* List of functions to call after each text change.
5944 Three arguments are passed to each function: the positions of
5945 the beginning and end of the range of changed text,
5946 and the length in bytes of the pre-change text replaced by that range.
5947 \(For an insertion, the pre-change length is zero;
5948 for a deletion, that length is the number of bytes deleted,
5949 and the post-change beginning and end are at the same place.)
5951 Buffer changes made while executing the `after-change-functions'
5952 don't call any before-change or after-change functions.
5953 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5955 If an unhandled error happens in running these functions,
5956 the variable's value remains nil. That prevents the error
5957 from happening repeatedly and making Emacs nonfunctional. */);
5958 Vafter_change_functions = Qnil;
5960 DEFVAR_LISP ("first-change-hook", Vfirst_change_hook,
5961 doc: /* A list of functions to call before changing a buffer which is unmodified.
5962 The functions are run using the `run-hooks' function. */);
5963 Vfirst_change_hook = Qnil;
5965 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5966 doc: /* List of undo entries in current buffer.
5967 Recent changes come first; older changes follow newer.
5969 An entry (BEG . END) represents an insertion which begins at
5970 position BEG and ends at position END.
5972 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5973 from (abs POSITION). If POSITION is positive, point was at the front
5974 of the text being deleted; if negative, point was at the end.
5976 An entry (t HIGH . LOW) indicates that the buffer previously had
5977 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5978 of the visited file's modification time, as of that time. If the
5979 modification time of the most recent save is different, this entry is
5980 obsolete.
5982 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5983 was modified between BEG and END. PROPERTY is the property name,
5984 and VALUE is the old value.
5986 An entry (apply FUN-NAME . ARGS) means undo the change with
5987 \(apply FUN-NAME ARGS).
5989 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5990 in the active region. BEG and END is the range affected by this entry
5991 and DELTA is the number of bytes added or deleted in that range by
5992 this change.
5994 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5995 was adjusted in position by the offset DISTANCE (an integer).
5997 An entry of the form POSITION indicates that point was at the buffer
5998 location given by the integer. Undoing an entry of this form places
5999 point at POSITION.
6001 Entries with value `nil' mark undo boundaries. The undo command treats
6002 the changes between two undo boundaries as a single step to be undone.
6004 If the value of the variable is t, undo information is not recorded. */);
6006 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6007 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6009 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6010 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6012 Normally, the line-motion functions work by scanning the buffer for
6013 newlines. Columnar operations (like `move-to-column' and
6014 `compute-motion') also work by scanning the buffer, summing character
6015 widths as they go. This works well for ordinary text, but if the
6016 buffer's lines are very long (say, more than 500 characters), these
6017 motion functions will take longer to execute. Emacs may also take
6018 longer to update the display.
6020 If `cache-long-line-scans' is non-nil, these motion functions cache the
6021 results of their scans, and consult the cache to avoid rescanning
6022 regions of the buffer until the text is modified. The caches are most
6023 beneficial when they prevent the most searching---that is, when the
6024 buffer contains long lines and large regions of characters with the
6025 same, fixed screen width.
6027 When `cache-long-line-scans' is non-nil, processing short lines will
6028 become slightly slower (because of the overhead of consulting the
6029 cache), and the caches will use memory roughly proportional to the
6030 number of newlines and characters whose screen width varies.
6032 The caches require no explicit maintenance; their accuracy is
6033 maintained internally by the Emacs primitives. Enabling or disabling
6034 the cache should not affect the behavior of any of the motion
6035 functions; it should only affect their performance. */);
6037 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6038 doc: /* Value of point before the last series of scroll operations, or nil. */);
6040 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6041 doc: /* List of formats to use when saving this buffer.
6042 Formats are defined by `format-alist'. This variable is
6043 set when a file is visited. */);
6045 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6046 &current_buffer->auto_save_file_format, Qnil,
6047 doc: /* *Format in which to write auto-save files.
6048 Should be a list of symbols naming formats that are defined in `format-alist'.
6049 If it is t, which is the default, auto-save files are written in the
6050 same format as a regular save would use. */);
6052 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6053 &current_buffer->invisibility_spec, Qnil,
6054 doc: /* Invisibility spec of this buffer.
6055 The default is t, which means that text is invisible
6056 if it has a non-nil `invisible' property.
6057 If the value is a list, a text character is invisible if its `invisible'
6058 property is an element in that list (or is a list with members in common).
6059 If an element is a cons cell of the form (PROP . ELLIPSIS),
6060 then characters with property value PROP are invisible,
6061 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6063 DEFVAR_PER_BUFFER ("buffer-display-count",
6064 &current_buffer->display_count, Qnil,
6065 doc: /* A number incremented each time this buffer is displayed in a window.
6066 The function `set-window-buffer' increments it. */);
6068 DEFVAR_PER_BUFFER ("buffer-display-time",
6069 &current_buffer->display_time, Qnil,
6070 doc: /* Time stamp updated each time this buffer is displayed in a window.
6071 The function `set-window-buffer' updates this variable
6072 to the value obtained by calling `current-time'.
6073 If the buffer has never been shown in a window, the value is nil. */);
6075 DEFVAR_LISP ("transient-mark-mode", Vtransient_mark_mode,
6076 doc: /* Non-nil if Transient Mark mode is enabled.
6077 See the command `transient-mark-mode' for a description of this minor mode.
6079 Non-nil also enables highlighting of the region whenever the mark is active.
6080 The variable `highlight-nonselected-windows' controls whether to highlight
6081 all windows or just the selected window.
6083 Lisp programs may give this variable certain special values:
6085 - A value of `lambda' enables Transient Mark mode temporarily.
6086 It is disabled again after any subsequent action that would
6087 normally deactivate the mark (e.g. buffer modification).
6089 - A value of (only . OLDVAL) enables Transient Mark mode
6090 temporarily. After any subsequent point motion command that is
6091 not shift-translated, or any other action that would normally
6092 deactivate the mark (e.g. buffer modification), the value of
6093 `transient-mark-mode' is set to OLDVAL. */);
6094 Vtransient_mark_mode = Qnil;
6096 DEFVAR_LISP ("inhibit-read-only", Vinhibit_read_only,
6097 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6098 If the value is t, disregard `buffer-read-only' and all `read-only'
6099 text properties. If the value is a list, disregard `buffer-read-only'
6100 and disregard a `read-only' text property if the property value
6101 is a member of the list. */);
6102 Vinhibit_read_only = Qnil;
6104 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6105 doc: /* Cursor to use when this buffer is in the selected window.
6106 Values are interpreted as follows:
6108 t use the cursor specified for the frame
6109 nil don't display a cursor
6110 box display a filled box cursor
6111 hollow display a hollow box cursor
6112 bar display a vertical bar cursor with default width
6113 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6114 hbar display a horizontal bar cursor with default height
6115 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6116 ANYTHING ELSE display a hollow box cursor
6118 When the buffer is displayed in a non-selected window, the
6119 cursor's appearance is instead controlled by the variable
6120 `cursor-in-non-selected-windows'. */);
6122 DEFVAR_PER_BUFFER ("line-spacing",
6123 &current_buffer->extra_line_spacing, Qnil,
6124 doc: /* Additional space to put between lines when displaying a buffer.
6125 The space is measured in pixels, and put below lines on graphic displays,
6126 see `display-graphic-p'.
6127 If value is a floating point number, it specifies the spacing relative
6128 to the default frame line height. A value of nil means add no extra space. */);
6130 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6131 &current_buffer->cursor_in_non_selected_windows, Qnil,
6132 doc: /* *Non-nil means show a cursor in non-selected windows.
6133 If nil, only shows a cursor in the selected window.
6134 If t, displays a cursor related to the usual cursor type
6135 \(a solid box becomes hollow, a bar becomes a narrower bar).
6136 You can also specify the cursor type as in the `cursor-type' variable.
6137 Use Custom to set this variable and update the display." */);
6139 DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions,
6140 doc: /* List of functions called with no args to query before killing a buffer.
6141 The buffer being killed will be current while the functions are running.
6142 If any of them returns nil, the buffer is not killed. */);
6143 Vkill_buffer_query_functions = Qnil;
6145 DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
6146 doc: /* Normal hook run before changing the major mode of a buffer.
6147 The function `kill-all-local-variables' runs this before doing anything else. */);
6148 Vchange_major_mode_hook = Qnil;
6149 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6150 staticpro (&Qchange_major_mode_hook);
6152 defsubr (&Sbuffer_live_p);
6153 defsubr (&Sbuffer_list);
6154 defsubr (&Sget_buffer);
6155 defsubr (&Sget_file_buffer);
6156 defsubr (&Sget_buffer_create);
6157 defsubr (&Smake_indirect_buffer);
6158 defsubr (&Sgenerate_new_buffer_name);
6159 defsubr (&Sbuffer_name);
6160 /*defsubr (&Sbuffer_number);*/
6161 defsubr (&Sbuffer_file_name);
6162 defsubr (&Sbuffer_base_buffer);
6163 defsubr (&Sbuffer_local_value);
6164 defsubr (&Sbuffer_local_variables);
6165 defsubr (&Sbuffer_modified_p);
6166 defsubr (&Sset_buffer_modified_p);
6167 defsubr (&Sbuffer_modified_tick);
6168 defsubr (&Sbuffer_chars_modified_tick);
6169 defsubr (&Srename_buffer);
6170 defsubr (&Sother_buffer);
6171 defsubr (&Sbuffer_enable_undo);
6172 defsubr (&Skill_buffer);
6173 defsubr (&Sset_buffer_major_mode);
6174 defsubr (&Sswitch_to_buffer);
6175 defsubr (&Scurrent_buffer);
6176 defsubr (&Sset_buffer);
6177 defsubr (&Sbarf_if_buffer_read_only);
6178 defsubr (&Sbury_buffer);
6179 defsubr (&Serase_buffer);
6180 defsubr (&Sbuffer_swap_text);
6181 defsubr (&Sset_buffer_multibyte);
6182 defsubr (&Skill_all_local_variables);
6184 defsubr (&Soverlayp);
6185 defsubr (&Smake_overlay);
6186 defsubr (&Sdelete_overlay);
6187 defsubr (&Smove_overlay);
6188 defsubr (&Soverlay_start);
6189 defsubr (&Soverlay_end);
6190 defsubr (&Soverlay_buffer);
6191 defsubr (&Soverlay_properties);
6192 defsubr (&Soverlays_at);
6193 defsubr (&Soverlays_in);
6194 defsubr (&Snext_overlay_change);
6195 defsubr (&Sprevious_overlay_change);
6196 defsubr (&Soverlay_recenter);
6197 defsubr (&Soverlay_lists);
6198 defsubr (&Soverlay_get);
6199 defsubr (&Soverlay_put);
6200 defsubr (&Srestore_buffer_modified_p);
6203 void
6204 keys_of_buffer (void)
6206 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6207 initial_define_key (control_x_map, 'k', "kill-buffer");
6209 /* This must not be in syms_of_buffer, because Qdisabled is not
6210 initialized when that function gets called. */
6211 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);