Fix issues with buffer list handling and adjust-window-trailing-edge.
[emacs.git] / src / buffer.c
blob3d1c347899e28ee2f4bb65026a82ba1e436f026f
1 /* Buffer manipulation primitives for GNU Emacs.
3 Copyright (C) 1985-1989, 1993-1995, 1997-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/param.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <setjmp.h>
28 #include <unistd.h>
30 #include "lisp.h"
31 #include "intervals.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "character.h"
36 #include "region-cache.h"
37 #include "indent.h"
38 #include "blockinput.h"
39 #include "keyboard.h"
40 #include "keymap.h"
41 #include "frame.h"
43 struct buffer *current_buffer; /* the current buffer */
45 /* First buffer in chain of all buffers (in reverse order of creation).
46 Threaded through ->next. */
48 struct buffer *all_buffers;
50 /* This structure holds the default values of the buffer-local variables
51 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
52 The default value occupies the same slot in this structure
53 as an individual buffer's value occupies in that buffer.
54 Setting the default value also goes through the alist of buffers
55 and stores into each buffer that does not say it has a local value. */
57 DECL_ALIGN (struct buffer, buffer_defaults);
59 /* A Lisp_Object pointer to the above, used for staticpro */
61 static Lisp_Object Vbuffer_defaults;
63 /* This structure marks which slots in a buffer have corresponding
64 default values in buffer_defaults.
65 Each such slot has a nonzero value in this structure.
66 The value has only one nonzero bit.
68 When a buffer has its own local value for a slot,
69 the entry for that slot (found in the same slot in this structure)
70 is turned on in the buffer's local_flags array.
72 If a slot in this structure is -1, then even though there may
73 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
74 and the corresponding slot in buffer_defaults is not used.
76 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
77 zero, that is a bug */
79 struct buffer buffer_local_flags;
81 /* This structure holds the names of symbols whose values may be
82 buffer-local. It is indexed and accessed in the same way as the above. */
84 DECL_ALIGN (struct buffer, buffer_local_symbols);
86 /* A Lisp_Object pointer to the above, used for staticpro */
87 static Lisp_Object Vbuffer_local_symbols;
89 /* Return the symbol of the per-buffer variable at offset OFFSET in
90 the buffer structure. */
92 #define PER_BUFFER_SYMBOL(OFFSET) \
93 (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
95 /* Flags indicating which built-in buffer-local variables
96 are permanent locals. */
97 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
99 /* Number of per-buffer variables used. */
101 int last_per_buffer_idx;
103 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
104 int after, Lisp_Object arg1,
105 Lisp_Object arg2, Lisp_Object arg3);
106 static void swap_out_buffer_local_variables (struct buffer *b);
107 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
109 /* Alist of all buffer names vs the buffers. */
110 /* This used to be a variable, but is no longer,
111 to prevent lossage due to user rplac'ing this alist or its elements. */
112 Lisp_Object Vbuffer_alist;
114 Lisp_Object Qkill_buffer_query_functions;
116 /* Hook run before changing a major mode. */
117 Lisp_Object Qchange_major_mode_hook;
119 Lisp_Object Qfirst_change_hook;
120 Lisp_Object Qbefore_change_functions;
121 Lisp_Object Qafter_change_functions;
122 Lisp_Object Qucs_set_table_for_input;
124 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
125 Lisp_Object Qpermanent_local_hook;
127 Lisp_Object Qprotected_field;
129 Lisp_Object QSFundamental; /* A string "Fundamental" */
131 Lisp_Object Qkill_buffer_hook;
132 Lisp_Object Qbuffer_list_update_hook;
134 Lisp_Object Qget_file_buffer;
136 Lisp_Object Qoverlayp;
138 Lisp_Object Qpriority, Qclone_number, Qevaporate, Qbefore_string, Qafter_string;
140 Lisp_Object Qmodification_hooks;
141 Lisp_Object Qinsert_in_front_hooks;
142 Lisp_Object Qinsert_behind_hooks;
144 static void alloc_buffer_text (struct buffer *, size_t);
145 static void free_buffer_text (struct buffer *b);
146 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
147 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
148 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
150 /* For debugging; temporary. See set_buffer_internal. */
151 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
153 void
154 nsberror (Lisp_Object spec)
156 if (STRINGP (spec))
157 error ("No buffer named %s", SDATA (spec));
158 error ("Invalid buffer argument");
161 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
162 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
163 Value is nil if OBJECT is not a buffer or if it has been killed. */)
164 (Lisp_Object object)
166 return ((BUFFERP (object) && ! NILP (BVAR (XBUFFER (object), name)))
167 ? Qt : Qnil);
170 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
171 doc: /* Return a list of all existing live buffers.
172 If the optional arg FRAME is a frame, we return the buffer list in the
173 proper order for that frame: the buffers show in FRAME come first,
174 followed by the rest of the buffers. */)
175 (Lisp_Object frame)
177 Lisp_Object general;
178 general = Fmapcar (Qcdr, Vbuffer_alist);
180 if (FRAMEP (frame))
182 Lisp_Object framelist, prevlist, tail;
183 Lisp_Object args[3];
185 CHECK_FRAME (frame);
186 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
187 prevlist = Fnreverse (Fcopy_sequence
188 (XFRAME (frame)->buried_buffer_list));
190 /* Remove from GENERAL any buffer that duplicates one in
191 FRAMELIST or PREVLIST. */
192 tail = framelist;
193 while (CONSP (tail))
195 general = Fdelq (XCAR (tail), general);
196 tail = XCDR (tail);
198 tail = prevlist;
199 while (CONSP (tail))
201 general = Fdelq (XCAR (tail), general);
202 tail = XCDR (tail);
205 args[0] = framelist;
206 args[1] = general;
207 args[2] = prevlist;
208 return Fnconc (3, args);
210 else
211 return general;
214 /* Like Fassoc, but use Fstring_equal to compare
215 (which ignores text properties),
216 and don't ever QUIT. */
218 static Lisp_Object
219 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
221 register Lisp_Object tail;
222 for (tail = list; CONSP (tail); tail = XCDR (tail))
224 register Lisp_Object elt, tem;
225 elt = XCAR (tail);
226 tem = Fstring_equal (Fcar (elt), key);
227 if (!NILP (tem))
228 return elt;
230 return Qnil;
233 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
234 doc: /* Return the buffer named BUFFER-OR-NAME.
235 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
236 is a string and there is no buffer with that name, return nil. If
237 BUFFER-OR-NAME is a buffer, return it as given. */)
238 (register Lisp_Object buffer_or_name)
240 if (BUFFERP (buffer_or_name))
241 return buffer_or_name;
242 CHECK_STRING (buffer_or_name);
244 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
247 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
248 doc: /* Return the buffer visiting file FILENAME (a string).
249 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
250 If there is no such live buffer, return nil.
251 See also `find-buffer-visiting'. */)
252 (register Lisp_Object filename)
254 register Lisp_Object tail, buf, tem;
255 Lisp_Object handler;
257 CHECK_STRING (filename);
258 filename = Fexpand_file_name (filename, Qnil);
260 /* If the file name has special constructs in it,
261 call the corresponding file handler. */
262 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
263 if (!NILP (handler))
264 return call2 (handler, Qget_file_buffer, filename);
266 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
268 buf = Fcdr (XCAR (tail));
269 if (!BUFFERP (buf)) continue;
270 if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue;
271 tem = Fstring_equal (BVAR (XBUFFER (buf), filename), filename);
272 if (!NILP (tem))
273 return buf;
275 return Qnil;
278 Lisp_Object
279 get_truename_buffer (register Lisp_Object filename)
281 register Lisp_Object tail, buf, tem;
283 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
285 buf = Fcdr (XCAR (tail));
286 if (!BUFFERP (buf)) continue;
287 if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
288 tem = Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename);
289 if (!NILP (tem))
290 return buf;
292 return Qnil;
295 /* Incremented for each buffer created, to assign the buffer number. */
296 int buffer_count;
298 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
299 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
300 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
301 return that buffer. If no such buffer exists, create a new buffer with
302 that name and return it. If BUFFER-OR-NAME starts with a space, the new
303 buffer does not keep undo information.
305 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
306 even if it is dead. The return value is never nil. */)
307 (register Lisp_Object buffer_or_name)
309 register Lisp_Object buffer, name;
310 register struct buffer *b;
312 buffer = Fget_buffer (buffer_or_name);
313 if (!NILP (buffer))
314 return buffer;
316 if (SCHARS (buffer_or_name) == 0)
317 error ("Empty string for buffer name is not allowed");
319 b = allocate_buffer ();
321 /* An ordinary buffer uses its own struct buffer_text. */
322 b->text = &b->own_text;
323 b->base_buffer = 0;
325 BUF_GAP_SIZE (b) = 20;
326 BLOCK_INPUT;
327 /* We allocate extra 1-byte at the tail and keep it always '\0' for
328 anchoring a search. */
329 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
330 UNBLOCK_INPUT;
331 if (! BUF_BEG_ADDR (b))
332 buffer_memory_full ();
334 BUF_PT (b) = BEG;
335 BUF_GPT (b) = BEG;
336 BUF_BEGV (b) = BEG;
337 BUF_ZV (b) = BEG;
338 BUF_Z (b) = BEG;
339 BUF_PT_BYTE (b) = BEG_BYTE;
340 BUF_GPT_BYTE (b) = BEG_BYTE;
341 BUF_BEGV_BYTE (b) = BEG_BYTE;
342 BUF_ZV_BYTE (b) = BEG_BYTE;
343 BUF_Z_BYTE (b) = BEG_BYTE;
344 BUF_MODIFF (b) = 1;
345 BUF_CHARS_MODIFF (b) = 1;
346 BUF_OVERLAY_MODIFF (b) = 1;
347 BUF_SAVE_MODIFF (b) = 1;
348 BUF_INTERVALS (b) = 0;
349 BUF_UNCHANGED_MODIFIED (b) = 1;
350 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
351 BUF_END_UNCHANGED (b) = 0;
352 BUF_BEG_UNCHANGED (b) = 0;
353 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
355 b->newline_cache = 0;
356 b->width_run_cache = 0;
357 BVAR (b, width_table) = Qnil;
358 b->prevent_redisplay_optimizations_p = 1;
360 /* Put this on the chain of all buffers including killed ones. */
361 b->next = all_buffers;
362 all_buffers = b;
364 /* An ordinary buffer normally doesn't need markers
365 to handle BEGV and ZV. */
366 BVAR (b, pt_marker) = Qnil;
367 BVAR (b, begv_marker) = Qnil;
368 BVAR (b, zv_marker) = Qnil;
370 name = Fcopy_sequence (buffer_or_name);
371 STRING_SET_INTERVALS (name, NULL_INTERVAL);
372 BVAR (b, name) = name;
374 BVAR (b, undo_list) = (SREF (name, 0) != ' ') ? Qnil : Qt;
376 reset_buffer (b);
377 reset_buffer_local_variables (b, 1);
379 BVAR (b, mark) = Fmake_marker ();
380 BUF_MARKERS (b) = NULL;
381 BVAR (b, name) = name;
383 /* Put this in the alist of all live buffers. */
384 XSETBUFFER (buffer, b);
385 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
386 /* And run buffer-list-update-hook. */
387 if (!NILP (Vrun_hooks))
388 call1 (Vrun_hooks, Qbuffer_list_update_hook);
390 /* An error in calling the function here (should someone redefine it)
391 can lead to infinite regress until you run out of stack. rms
392 says that's not worth protecting against. */
393 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
394 /* buffer is on buffer-alist, so no gcpro. */
395 call1 (Qucs_set_table_for_input, buffer);
397 return buffer;
401 /* Return a list of overlays which is a copy of the overlay list
402 LIST, but for buffer B. */
404 static struct Lisp_Overlay *
405 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
407 Lisp_Object buffer;
408 struct Lisp_Overlay *result = NULL, *tail = NULL;
410 XSETBUFFER (buffer, b);
412 for (; list; list = list->next)
414 Lisp_Object overlay, start, end, old_overlay;
415 EMACS_INT charpos;
417 XSETMISC (old_overlay, list);
418 charpos = marker_position (OVERLAY_START (old_overlay));
419 start = Fmake_marker ();
420 Fset_marker (start, make_number (charpos), buffer);
421 XMARKER (start)->insertion_type
422 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
424 charpos = marker_position (OVERLAY_END (old_overlay));
425 end = Fmake_marker ();
426 Fset_marker (end, make_number (charpos), buffer);
427 XMARKER (end)->insertion_type
428 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
430 overlay = allocate_misc ();
431 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
432 OVERLAY_START (overlay) = start;
433 OVERLAY_END (overlay) = end;
434 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
435 XOVERLAY (overlay)->next = NULL;
437 if (tail)
438 tail = tail->next = XOVERLAY (overlay);
439 else
440 result = tail = XOVERLAY (overlay);
443 return result;
447 /* Clone per-buffer values of buffer FROM.
449 Buffer TO gets the same per-buffer values as FROM, with the
450 following exceptions: (1) TO's name is left untouched, (2) markers
451 are copied and made to refer to TO, and (3) overlay lists are
452 copied. */
454 static void
455 clone_per_buffer_values (struct buffer *from, struct buffer *to)
457 Lisp_Object to_buffer;
458 int offset;
460 XSETBUFFER (to_buffer, to);
462 /* buffer-local Lisp variables start at `undo_list',
463 tho only the ones from `name' on are GC'd normally. */
464 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
465 offset < sizeof *to;
466 offset += sizeof (Lisp_Object))
468 Lisp_Object obj;
470 /* Don't touch the `name' which should be unique for every buffer. */
471 if (offset == PER_BUFFER_VAR_OFFSET (name))
472 continue;
474 obj = PER_BUFFER_VALUE (from, offset);
475 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
477 struct Lisp_Marker *m = XMARKER (obj);
478 obj = Fmake_marker ();
479 XMARKER (obj)->insertion_type = m->insertion_type;
480 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
483 PER_BUFFER_VALUE (to, offset) = obj;
486 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
488 to->overlays_before = copy_overlays (to, from->overlays_before);
489 to->overlays_after = copy_overlays (to, from->overlays_after);
491 /* Get (a copy of) the alist of Lisp-level local variables of FROM
492 and install that in TO. */
493 BVAR (to, local_var_alist) = buffer_lisp_local_variables (from);
496 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
497 2, 3,
498 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
499 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
500 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
501 NAME should be a string which is not the name of an existing buffer.
502 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
503 such as major and minor modes, in the indirect buffer.
504 CLONE nil means the indirect buffer's state is reset to default values. */)
505 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
507 Lisp_Object buf, tem;
508 struct buffer *b;
510 CHECK_STRING (name);
511 buf = Fget_buffer (name);
512 if (!NILP (buf))
513 error ("Buffer name `%s' is in use", SDATA (name));
515 tem = base_buffer;
516 base_buffer = Fget_buffer (base_buffer);
517 if (NILP (base_buffer))
518 error ("No such buffer: `%s'", SDATA (tem));
519 if (NILP (BVAR (XBUFFER (base_buffer), name)))
520 error ("Base buffer has been killed");
522 if (SCHARS (name) == 0)
523 error ("Empty string for buffer name is not allowed");
525 b = allocate_buffer ();
527 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
528 ? XBUFFER (base_buffer)->base_buffer
529 : XBUFFER (base_buffer));
531 /* Use the base buffer's text object. */
532 b->text = b->base_buffer->text;
534 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
535 BUF_ZV (b) = BUF_ZV (b->base_buffer);
536 BUF_PT (b) = BUF_PT (b->base_buffer);
537 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
538 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
539 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
541 b->newline_cache = 0;
542 b->width_run_cache = 0;
543 BVAR (b, width_table) = Qnil;
545 /* Put this on the chain of all buffers including killed ones. */
546 b->next = all_buffers;
547 all_buffers = b;
549 name = Fcopy_sequence (name);
550 STRING_SET_INTERVALS (name, NULL_INTERVAL);
551 BVAR (b, name) = name;
553 reset_buffer (b);
554 reset_buffer_local_variables (b, 1);
556 /* Put this in the alist of all live buffers. */
557 XSETBUFFER (buf, b);
558 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
560 BVAR (b, mark) = Fmake_marker ();
561 BVAR (b, name) = name;
563 /* The multibyte status belongs to the base buffer. */
564 BVAR (b, enable_multibyte_characters) = BVAR (b->base_buffer, enable_multibyte_characters);
566 /* Make sure the base buffer has markers for its narrowing. */
567 if (NILP (BVAR (b->base_buffer, pt_marker)))
569 BVAR (b->base_buffer, pt_marker) = Fmake_marker ();
570 set_marker_both (BVAR (b->base_buffer, pt_marker), base_buffer,
571 BUF_PT (b->base_buffer),
572 BUF_PT_BYTE (b->base_buffer));
574 if (NILP (BVAR (b->base_buffer, begv_marker)))
576 BVAR (b->base_buffer, begv_marker) = Fmake_marker ();
577 set_marker_both (BVAR (b->base_buffer, begv_marker), base_buffer,
578 BUF_BEGV (b->base_buffer),
579 BUF_BEGV_BYTE (b->base_buffer));
581 if (NILP (BVAR (b->base_buffer, zv_marker)))
583 BVAR (b->base_buffer, zv_marker) = Fmake_marker ();
584 set_marker_both (BVAR (b->base_buffer, zv_marker), base_buffer,
585 BUF_ZV (b->base_buffer),
586 BUF_ZV_BYTE (b->base_buffer));
587 XMARKER (BVAR (b->base_buffer, zv_marker))->insertion_type = 1;
590 if (NILP (clone))
592 /* Give the indirect buffer markers for its narrowing. */
593 BVAR (b, pt_marker) = Fmake_marker ();
594 set_marker_both (BVAR (b, pt_marker), buf, BUF_PT (b), BUF_PT_BYTE (b));
595 BVAR (b, begv_marker) = Fmake_marker ();
596 set_marker_both (BVAR (b, begv_marker), buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
597 BVAR (b, zv_marker) = Fmake_marker ();
598 set_marker_both (BVAR (b, zv_marker), buf, BUF_ZV (b), BUF_ZV_BYTE (b));
599 XMARKER (BVAR (b, zv_marker))->insertion_type = 1;
601 else
603 struct buffer *old_b = current_buffer;
605 clone_per_buffer_values (b->base_buffer, b);
606 BVAR (b, filename) = Qnil;
607 BVAR (b, file_truename) = Qnil;
608 BVAR (b, display_count) = make_number (0);
609 BVAR (b, backed_up) = Qnil;
610 BVAR (b, auto_save_file_name) = Qnil;
611 set_buffer_internal_1 (b);
612 Fset (intern ("buffer-save-without-query"), Qnil);
613 Fset (intern ("buffer-file-number"), Qnil);
614 Fset (intern ("buffer-stale-function"), Qnil);
615 set_buffer_internal_1 (old_b);
618 /* Run buffer-list-update-hook. */
619 if (!NILP (Vrun_hooks))
620 call1 (Vrun_hooks, Qbuffer_list_update_hook);
622 return buf;
625 void
626 delete_all_overlays (struct buffer *b)
628 Lisp_Object overlay;
630 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
631 have to empty the list, otherwise we end up with overlays that
632 think they belong to this buffer while the buffer doesn't know about
633 them any more. */
634 while (b->overlays_before)
636 XSETMISC (overlay, b->overlays_before);
637 Fdelete_overlay (overlay);
639 while (b->overlays_after)
641 XSETMISC (overlay, b->overlays_after);
642 Fdelete_overlay (overlay);
644 eassert (b->overlays_before == NULL);
645 eassert (b->overlays_after == NULL);
648 /* Reinitialize everything about a buffer except its name and contents
649 and local variables.
650 If called on an already-initialized buffer, the list of overlays
651 should be deleted before calling this function, otherwise we end up
652 with overlays that claim to belong to the buffer but the buffer
653 claims it doesn't belong to it. */
655 void
656 reset_buffer (register struct buffer *b)
658 BVAR (b, filename) = Qnil;
659 BVAR (b, file_truename) = Qnil;
660 BVAR (b, directory) = (current_buffer) ? BVAR (current_buffer, directory) : Qnil;
661 b->modtime = 0;
662 b->modtime_size = -1;
663 XSETFASTINT (BVAR (b, save_length), 0);
664 b->last_window_start = 1;
665 /* It is more conservative to start out "changed" than "unchanged". */
666 b->clip_changed = 0;
667 b->prevent_redisplay_optimizations_p = 1;
668 BVAR (b, backed_up) = Qnil;
669 BUF_AUTOSAVE_MODIFF (b) = 0;
670 b->auto_save_failure_time = -1;
671 BVAR (b, auto_save_file_name) = Qnil;
672 BVAR (b, read_only) = Qnil;
673 b->overlays_before = NULL;
674 b->overlays_after = NULL;
675 b->overlay_center = BEG;
676 BVAR (b, mark_active) = Qnil;
677 BVAR (b, point_before_scroll) = Qnil;
678 BVAR (b, file_format) = Qnil;
679 BVAR (b, auto_save_file_format) = Qt;
680 BVAR (b, last_selected_window) = Qnil;
681 XSETINT (BVAR (b, display_count), 0);
682 BVAR (b, display_time) = Qnil;
683 BVAR (b, enable_multibyte_characters) = BVAR (&buffer_defaults, enable_multibyte_characters);
684 BVAR (b, cursor_type) = BVAR (&buffer_defaults, cursor_type);
685 BVAR (b, extra_line_spacing) = BVAR (&buffer_defaults, extra_line_spacing);
687 b->display_error_modiff = 0;
690 /* Reset buffer B's local variables info.
691 Don't use this on a buffer that has already been in use;
692 it does not treat permanent locals consistently.
693 Instead, use Fkill_all_local_variables.
695 If PERMANENT_TOO is 1, then we reset permanent
696 buffer-local variables. If PERMANENT_TOO is 0,
697 we preserve those. */
699 static void
700 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
702 register int offset;
703 int i;
705 /* Reset the major mode to Fundamental, together with all the
706 things that depend on the major mode.
707 default-major-mode is handled at a higher level.
708 We ignore it here. */
709 BVAR (b, major_mode) = Qfundamental_mode;
710 BVAR (b, keymap) = Qnil;
711 BVAR (b, mode_name) = QSFundamental;
712 BVAR (b, minor_modes) = Qnil;
714 /* If the standard case table has been altered and invalidated,
715 fix up its insides first. */
716 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
717 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
718 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
719 Fset_standard_case_table (Vascii_downcase_table);
721 BVAR (b, downcase_table) = Vascii_downcase_table;
722 BVAR (b, upcase_table) = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
723 BVAR (b, case_canon_table) = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
724 BVAR (b, case_eqv_table) = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
725 BVAR (b, invisibility_spec) = Qt;
727 /* Reset all (or most) per-buffer variables to their defaults. */
728 if (permanent_too)
729 BVAR (b, local_var_alist) = Qnil;
730 else
732 Lisp_Object tmp, prop, last = Qnil;
733 for (tmp = BVAR (b, local_var_alist); CONSP (tmp); tmp = XCDR (tmp))
734 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
736 /* If permanent-local, keep it. */
737 last = tmp;
738 if (EQ (prop, Qpermanent_local_hook))
740 /* This is a partially permanent hook variable.
741 Preserve only the elements that want to be preserved. */
742 Lisp_Object list, newlist;
743 list = XCDR (XCAR (tmp));
744 if (!CONSP (list))
745 newlist = list;
746 else
747 for (newlist = Qnil; CONSP (list); list = XCDR (list))
749 Lisp_Object elt = XCAR (list);
750 /* Preserve element ELT if it's t,
751 if it is a function with a `permanent-local-hook' property,
752 or if it's not a symbol. */
753 if (! SYMBOLP (elt)
754 || EQ (elt, Qt)
755 || !NILP (Fget (elt, Qpermanent_local_hook)))
756 newlist = Fcons (elt, newlist);
758 XSETCDR (XCAR (tmp), Fnreverse (newlist));
761 /* Delete this local variable. */
762 else if (NILP (last))
763 BVAR (b, local_var_alist) = XCDR (tmp);
764 else
765 XSETCDR (last, XCDR (tmp));
768 for (i = 0; i < last_per_buffer_idx; ++i)
769 if (permanent_too || buffer_permanent_local_flags[i] == 0)
770 SET_PER_BUFFER_VALUE_P (b, i, 0);
772 /* For each slot that has a default value,
773 copy that into the slot. */
775 /* buffer-local Lisp variables start at `undo_list',
776 tho only the ones from `name' on are GC'd normally. */
777 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
778 offset < sizeof *b;
779 offset += sizeof (Lisp_Object))
781 int idx = PER_BUFFER_IDX (offset);
782 if ((idx > 0
783 && (permanent_too
784 || buffer_permanent_local_flags[idx] == 0)))
785 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
789 /* We split this away from generate-new-buffer, because rename-buffer
790 and set-visited-file-name ought to be able to use this to really
791 rename the buffer properly. */
793 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
794 1, 2, 0,
795 doc: /* Return a string that is the name of no existing buffer based on NAME.
796 If there is no live buffer named NAME, then return NAME.
797 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
798 \(starting at 2) until an unused name is found, and then return that name.
799 Optional second argument IGNORE specifies a name that is okay to use (if
800 it is in the sequence to be tried) even if a buffer with that name exists. */)
801 (register Lisp_Object name, Lisp_Object ignore)
803 register Lisp_Object gentemp, tem;
804 int count;
805 char number[10];
807 CHECK_STRING (name);
809 tem = Fstring_equal (name, ignore);
810 if (!NILP (tem))
811 return name;
812 tem = Fget_buffer (name);
813 if (NILP (tem))
814 return name;
816 count = 1;
817 while (1)
819 sprintf (number, "<%d>", ++count);
820 gentemp = concat2 (name, build_string (number));
821 tem = Fstring_equal (gentemp, ignore);
822 if (!NILP (tem))
823 return gentemp;
824 tem = Fget_buffer (gentemp);
825 if (NILP (tem))
826 return gentemp;
831 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
832 doc: /* Return the name of BUFFER, as a string.
833 BUFFER defaults to the current buffer.
834 Return nil if BUFFER has been killed. */)
835 (register Lisp_Object buffer)
837 if (NILP (buffer))
838 return BVAR (current_buffer, name);
839 CHECK_BUFFER (buffer);
840 return BVAR (XBUFFER (buffer), name);
843 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
844 doc: /* Return name of file BUFFER is visiting, or nil if none.
845 No argument or nil as argument means use the current buffer. */)
846 (register Lisp_Object buffer)
848 if (NILP (buffer))
849 return BVAR (current_buffer, filename);
850 CHECK_BUFFER (buffer);
851 return BVAR (XBUFFER (buffer), filename);
854 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
855 0, 1, 0,
856 doc: /* Return the base buffer of indirect buffer BUFFER.
857 If BUFFER is not indirect, return nil.
858 BUFFER defaults to the current buffer. */)
859 (register Lisp_Object buffer)
861 struct buffer *base;
862 Lisp_Object base_buffer;
864 if (NILP (buffer))
865 base = current_buffer->base_buffer;
866 else
868 CHECK_BUFFER (buffer);
869 base = XBUFFER (buffer)->base_buffer;
872 if (! base)
873 return Qnil;
874 XSETBUFFER (base_buffer, base);
875 return base_buffer;
878 DEFUN ("buffer-local-value", Fbuffer_local_value,
879 Sbuffer_local_value, 2, 2, 0,
880 doc: /* Return the value of VARIABLE in BUFFER.
881 If VARIABLE does not have a buffer-local binding in BUFFER, the value
882 is the default binding of the variable. */)
883 (register Lisp_Object variable, register Lisp_Object buffer)
885 register struct buffer *buf;
886 register Lisp_Object result;
887 struct Lisp_Symbol *sym;
889 CHECK_SYMBOL (variable);
890 CHECK_BUFFER (buffer);
891 buf = XBUFFER (buffer);
892 sym = XSYMBOL (variable);
894 start:
895 switch (sym->redirect)
897 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
898 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
899 case SYMBOL_LOCALIZED:
900 { /* Look in local_var_alist. */
901 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
902 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
903 result = Fassoc (variable, BVAR (buf, local_var_alist));
904 if (!NILP (result))
906 if (blv->fwd)
907 { /* What binding is loaded right now? */
908 Lisp_Object current_alist_element = blv->valcell;
910 /* The value of the currently loaded binding is not
911 stored in it, but rather in the realvalue slot.
912 Store that value into the binding it belongs to
913 in case that is the one we are about to use. */
915 XSETCDR (current_alist_element,
916 do_symval_forwarding (blv->fwd));
918 /* Now get the (perhaps updated) value out of the binding. */
919 result = XCDR (result);
921 else
922 result = Fdefault_value (variable);
923 break;
925 case SYMBOL_FORWARDED:
927 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
928 if (BUFFER_OBJFWDP (fwd))
929 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
930 else
931 result = Fdefault_value (variable);
932 break;
934 default: abort ();
937 if (!EQ (result, Qunbound))
938 return result;
940 xsignal1 (Qvoid_variable, variable);
943 /* Return an alist of the Lisp-level buffer-local bindings of
944 buffer BUF. That is, don't include the variables maintained
945 in special slots in the buffer object. */
947 static Lisp_Object
948 buffer_lisp_local_variables (struct buffer *buf)
950 Lisp_Object result = Qnil;
951 register Lisp_Object tail;
952 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
954 Lisp_Object val, elt;
956 elt = XCAR (tail);
958 /* Reference each variable in the alist in buf.
959 If inquiring about the current buffer, this gets the current values,
960 so store them into the alist so the alist is up to date.
961 If inquiring about some other buffer, this swaps out any values
962 for that buffer, making the alist up to date automatically. */
963 val = find_symbol_value (XCAR (elt));
964 /* Use the current buffer value only if buf is the current buffer. */
965 if (buf != current_buffer)
966 val = XCDR (elt);
968 result = Fcons (Fcons (XCAR (elt), val), result);
971 return result;
974 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
975 Sbuffer_local_variables, 0, 1, 0,
976 doc: /* Return an alist of variables that are buffer-local in BUFFER.
977 Most elements look like (SYMBOL . VALUE), describing one variable.
978 For a symbol that is locally unbound, just the symbol appears in the value.
979 Note that storing new VALUEs in these elements doesn't change the variables.
980 No argument or nil as argument means use current buffer as BUFFER. */)
981 (register Lisp_Object buffer)
983 register struct buffer *buf;
984 register Lisp_Object result;
986 if (NILP (buffer))
987 buf = current_buffer;
988 else
990 CHECK_BUFFER (buffer);
991 buf = XBUFFER (buffer);
994 result = buffer_lisp_local_variables (buf);
996 /* Add on all the variables stored in special slots. */
998 int offset, idx;
1000 /* buffer-local Lisp variables start at `undo_list',
1001 tho only the ones from `name' on are GC'd normally. */
1002 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1003 offset < sizeof (struct buffer);
1004 /* sizeof EMACS_INT == sizeof Lisp_Object */
1005 offset += (sizeof (EMACS_INT)))
1007 idx = PER_BUFFER_IDX (offset);
1008 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1009 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1010 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1011 PER_BUFFER_VALUE (buf, offset)),
1012 result);
1016 return result;
1019 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1020 0, 1, 0,
1021 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1022 No argument or nil as argument means use current buffer as BUFFER. */)
1023 (register Lisp_Object buffer)
1025 register struct buffer *buf;
1026 if (NILP (buffer))
1027 buf = current_buffer;
1028 else
1030 CHECK_BUFFER (buffer);
1031 buf = XBUFFER (buffer);
1034 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1037 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1038 1, 1, 0,
1039 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1040 A non-nil FLAG means mark the buffer modified. */)
1041 (register Lisp_Object flag)
1043 register int already;
1044 register Lisp_Object fn;
1045 Lisp_Object buffer, window;
1047 #ifdef CLASH_DETECTION
1048 /* If buffer becoming modified, lock the file.
1049 If buffer becoming unmodified, unlock the file. */
1051 fn = BVAR (current_buffer, file_truename);
1052 /* Test buffer-file-name so that binding it to nil is effective. */
1053 if (!NILP (fn) && ! NILP (BVAR (current_buffer, filename)))
1055 already = SAVE_MODIFF < MODIFF;
1056 if (!already && !NILP (flag))
1057 lock_file (fn);
1058 else if (already && NILP (flag))
1059 unlock_file (fn);
1061 #endif /* CLASH_DETECTION */
1063 /* Here we have a problem. SAVE_MODIFF is used here to encode
1064 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1065 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1066 modify SAVE_MODIFF to affect one, we may affect the other
1067 as well.
1068 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1069 if SAVE_MODIFF<auto_save_modified that means we risk changing
1070 recent-auto-save-p from t to nil.
1071 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1072 we risk changing recent-auto-save-p from nil to t. */
1073 SAVE_MODIFF = (NILP (flag)
1074 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1075 ? MODIFF
1076 /* Let's try to preserve recent-auto-save-p. */
1077 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1078 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1079 we can either decrease SAVE_MODIFF and auto_save_modified
1080 or increase MODIFF. */
1081 : MODIFF++);
1083 /* Set update_mode_lines only if buffer is displayed in some window.
1084 Packages like jit-lock or lazy-lock preserve a buffer's modified
1085 state by recording/restoring the state around blocks of code.
1086 Setting update_mode_lines makes redisplay consider all windows
1087 (on all frames). Stealth fontification of buffers not displayed
1088 would incur additional redisplay costs if we'd set
1089 update_modes_lines unconditionally.
1091 Ideally, I think there should be another mechanism for fontifying
1092 buffers without "modifying" buffers, or redisplay should be
1093 smarter about updating the `*' in mode lines. --gerd */
1094 XSETBUFFER (buffer, current_buffer);
1095 window = Fget_buffer_window (buffer, Qt);
1096 if (WINDOWP (window))
1098 ++update_mode_lines;
1099 current_buffer->prevent_redisplay_optimizations_p = 1;
1102 return flag;
1105 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1106 Srestore_buffer_modified_p, 1, 1, 0,
1107 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1108 It is not ensured that mode lines will be updated to show the modified
1109 state of the current buffer. Use with care. */)
1110 (Lisp_Object flag)
1112 #ifdef CLASH_DETECTION
1113 Lisp_Object fn;
1115 /* If buffer becoming modified, lock the file.
1116 If buffer becoming unmodified, unlock the file. */
1118 fn = BVAR (current_buffer, file_truename);
1119 /* Test buffer-file-name so that binding it to nil is effective. */
1120 if (!NILP (fn) && ! NILP (BVAR (current_buffer, filename)))
1122 int already = SAVE_MODIFF < MODIFF;
1123 if (!already && !NILP (flag))
1124 lock_file (fn);
1125 else if (already && NILP (flag))
1126 unlock_file (fn);
1128 #endif /* CLASH_DETECTION */
1130 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1131 return flag;
1134 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1135 0, 1, 0,
1136 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1137 Each buffer has a tick counter which is incremented each time the
1138 text in that buffer is changed. It wraps around occasionally.
1139 No argument or nil as argument means use current buffer as BUFFER. */)
1140 (register Lisp_Object buffer)
1142 register struct buffer *buf;
1143 if (NILP (buffer))
1144 buf = current_buffer;
1145 else
1147 CHECK_BUFFER (buffer);
1148 buf = XBUFFER (buffer);
1151 return make_number (BUF_MODIFF (buf));
1154 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1155 Sbuffer_chars_modified_tick, 0, 1, 0,
1156 doc: /* Return BUFFER's character-change tick counter.
1157 Each buffer has a character-change tick counter, which is set to the
1158 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1159 time text in that buffer is inserted or deleted. By comparing the
1160 values returned by two individual calls of `buffer-chars-modified-tick',
1161 you can tell whether a character change occurred in that buffer in
1162 between these calls. No argument or nil as argument means use current
1163 buffer as BUFFER. */)
1164 (register Lisp_Object buffer)
1166 register struct buffer *buf;
1167 if (NILP (buffer))
1168 buf = current_buffer;
1169 else
1171 CHECK_BUFFER (buffer);
1172 buf = XBUFFER (buffer);
1175 return make_number (BUF_CHARS_MODIFF (buf));
1178 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1179 "(list (read-string \"Rename buffer (to new name): \" \
1180 nil 'buffer-name-history (buffer-name (current-buffer))) \
1181 current-prefix-arg)",
1182 doc: /* Change current buffer's name to NEWNAME (a string).
1183 If second arg UNIQUE is nil or omitted, it is an error if a
1184 buffer named NEWNAME already exists.
1185 If UNIQUE is non-nil, come up with a new name using
1186 `generate-new-buffer-name'.
1187 Interactively, you can set UNIQUE with a prefix argument.
1188 We return the name we actually gave the buffer.
1189 This does not change the name of the visited file (if any). */)
1190 (register Lisp_Object newname, Lisp_Object unique)
1192 register Lisp_Object tem, buf;
1194 CHECK_STRING (newname);
1196 if (SCHARS (newname) == 0)
1197 error ("Empty string is invalid as a buffer name");
1199 tem = Fget_buffer (newname);
1200 if (!NILP (tem))
1202 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1203 rename the buffer automatically so you can create another
1204 with the original name. It makes UNIQUE equivalent to
1205 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1206 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1207 return BVAR (current_buffer, name);
1208 if (!NILP (unique))
1209 newname = Fgenerate_new_buffer_name (newname, BVAR (current_buffer, name));
1210 else
1211 error ("Buffer name `%s' is in use", SDATA (newname));
1214 BVAR (current_buffer, name) = newname;
1216 /* Catch redisplay's attention. Unless we do this, the mode lines for
1217 any windows displaying current_buffer will stay unchanged. */
1218 update_mode_lines++;
1220 XSETBUFFER (buf, current_buffer);
1221 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1222 if (NILP (BVAR (current_buffer, filename))
1223 && !NILP (BVAR (current_buffer, auto_save_file_name)))
1224 call0 (intern ("rename-auto-save-file"));
1226 /* Run buffer-list-update-hook. */
1227 if (!NILP (Vrun_hooks))
1228 call1 (Vrun_hooks, Qbuffer_list_update_hook);
1230 /* Refetch since that last call may have done GC. */
1231 return BVAR (current_buffer, name);
1234 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1235 doc: /* Return most recently selected buffer other than BUFFER.
1236 Buffers not visible in windows are preferred to visible buffers, unless
1237 optional second argument VISIBLE-OK is non-nil. Ignore the argument
1238 BUFFER unless it denotes a live buffer. If the optional third argument
1239 FRAME is non-nil, use that frame's buffer list instead of the selected
1240 frame's buffer list.
1242 The buffer is found by scanning the selected or specified frame's buffer
1243 list first, followed by the list of all buffers. If no other buffer
1244 exists, return the buffer `*scratch*' (creating it if necessary). */)
1245 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1247 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1248 Lisp_Object tail, buf, pred;
1249 Lisp_Object notsogood = Qnil;
1251 if (NILP (frame))
1252 frame = selected_frame;
1254 CHECK_FRAME (frame);
1256 pred = frame_buffer_predicate (frame);
1257 /* Consider buffers that have been seen in the frame first. */
1258 tail = XFRAME (frame)->buffer_list;
1259 for (; CONSP (tail); tail = XCDR (tail))
1261 buf = XCAR (tail);
1262 if (BUFFERP (buf) && !EQ (buf, buffer)
1263 && !NILP (BVAR (XBUFFER (buf), name))
1264 && (SREF (BVAR (XBUFFER (buf), name), 0) != ' ')
1265 /* If the frame has a buffer_predicate, disregard buffers that
1266 don't fit the predicate. */
1267 && (NILP (pred) || !NILP (call1 (pred, buf))))
1269 if (!NILP (visible_ok)
1270 || NILP (Fget_buffer_window (buf, Qvisible)))
1271 return buf;
1272 else if (NILP (notsogood))
1273 notsogood = buf;
1277 /* Consider alist of all buffers next. */
1278 tail = Vbuffer_alist;
1279 for (; CONSP (tail); tail = XCDR (tail))
1281 buf = Fcdr (XCAR (tail));
1282 if (BUFFERP (buf) && !EQ (buf, buffer)
1283 && !NILP (BVAR (XBUFFER (buf), name))
1284 && (SREF (BVAR (XBUFFER (buf), name), 0) != ' ')
1285 /* If the frame has a buffer_predicate, disregard buffers that
1286 don't fit the predicate. */
1287 && (NILP (pred) || !NILP (call1 (pred, buf))))
1289 if (!NILP (visible_ok)
1290 || NILP (Fget_buffer_window (buf, Qvisible)))
1291 return buf;
1292 else if (NILP (notsogood))
1293 notsogood = buf;
1297 if (!NILP (notsogood))
1298 return notsogood;
1299 else
1301 buf = Fget_buffer (build_string ("*scratch*"));
1302 if (NILP (buf))
1304 buf = Fget_buffer_create (build_string ("*scratch*"));
1305 Fset_buffer_major_mode (buf);
1307 return buf;
1311 /* The following function is a safe variant of Fother_buffer: It doesn't
1312 pay attention to any frame-local buffer lists, doesn't care about
1313 visibility of buffers, and doesn't evaluate any frame predicates. */
1315 Lisp_Object
1316 other_buffer_safely (Lisp_Object buffer)
1318 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1319 Lisp_Object tail, buf;
1321 tail = Vbuffer_alist;
1322 for (; CONSP (tail); tail = XCDR (tail))
1324 buf = Fcdr (XCAR (tail));
1325 if (BUFFERP (buf) && !EQ (buf, buffer)
1326 && !NILP (BVAR (XBUFFER (buf), name))
1327 && (SREF (BVAR (XBUFFER (buf), name), 0) != ' '))
1328 return buf;
1331 buf = Fget_buffer (build_string ("*scratch*"));
1332 if (NILP (buf))
1334 buf = Fget_buffer_create (build_string ("*scratch*"));
1335 Fset_buffer_major_mode (buf);
1338 return buf;
1341 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1342 0, 1, "",
1343 doc: /* Start keeping undo information for buffer BUFFER.
1344 No argument or nil as argument means do this for the current buffer. */)
1345 (register Lisp_Object buffer)
1347 Lisp_Object real_buffer;
1349 if (NILP (buffer))
1350 XSETBUFFER (real_buffer, current_buffer);
1351 else
1353 real_buffer = Fget_buffer (buffer);
1354 if (NILP (real_buffer))
1355 nsberror (buffer);
1358 if (EQ (BVAR (XBUFFER (real_buffer), undo_list), Qt))
1359 BVAR (XBUFFER (real_buffer), undo_list) = Qnil;
1361 return Qnil;
1365 DEFVAR_LISP ("kill-buffer-hook", ..., "\
1366 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1367 The buffer being killed will be current while the hook is running.\n\
1368 See `kill-buffer'."
1370 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1371 doc: /* Kill buffer BUFFER-OR-NAME.
1372 The argument may be a buffer or the name of an existing buffer.
1373 Argument nil or omitted means kill the current buffer. Return t if the
1374 buffer is actually killed, nil otherwise.
1376 This function calls `replace-buffer-in-windows' for cleaning up all
1377 windows currently displaying the buffer to be killed. The functions in
1378 `kill-buffer-query-functions' are called with the buffer to be killed as
1379 the current buffer. If any of them returns nil, the buffer is not
1380 killed. The hook `kill-buffer-hook' is run before the buffer is
1381 actually killed. The buffer being killed will be current while the hook
1382 is running.
1384 Any processes that have this buffer as the `process-buffer' are killed
1385 with SIGHUP. */)
1386 (Lisp_Object buffer_or_name)
1388 Lisp_Object buffer;
1389 register struct buffer *b;
1390 register Lisp_Object tem;
1391 register struct Lisp_Marker *m;
1392 struct gcpro gcpro1;
1394 if (NILP (buffer_or_name))
1395 buffer = Fcurrent_buffer ();
1396 else
1397 buffer = Fget_buffer (buffer_or_name);
1398 if (NILP (buffer))
1399 nsberror (buffer_or_name);
1401 b = XBUFFER (buffer);
1403 /* Avoid trouble for buffer already dead. */
1404 if (NILP (BVAR (b, name)))
1405 return Qnil;
1407 /* Query if the buffer is still modified. */
1408 if (INTERACTIVE && !NILP (BVAR (b, filename))
1409 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1411 GCPRO1 (buffer);
1412 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1413 BVAR (b, name), make_number (0)));
1414 UNGCPRO;
1415 if (NILP (tem))
1416 return Qnil;
1419 /* Run hooks with the buffer to be killed the current buffer. */
1421 int count = SPECPDL_INDEX ();
1422 Lisp_Object arglist[1];
1424 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1425 set_buffer_internal (b);
1427 /* First run the query functions; if any query is answered no,
1428 don't kill the buffer. */
1429 arglist[0] = Qkill_buffer_query_functions;
1430 tem = Frun_hook_with_args_until_failure (1, arglist);
1431 if (NILP (tem))
1432 return unbind_to (count, Qnil);
1434 /* Then run the hooks. */
1435 Frun_hooks (1, &Qkill_buffer_hook);
1436 unbind_to (count, Qnil);
1439 /* We have no more questions to ask. Verify that it is valid
1440 to kill the buffer. This must be done after the questions
1441 since anything can happen within do_yes_or_no_p. */
1443 /* Don't kill the minibuffer now current. */
1444 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1445 return Qnil;
1447 if (NILP (BVAR (b, name)))
1448 return Qnil;
1450 /* When we kill a base buffer, kill all its indirect buffers.
1451 We do it at this stage so nothing terrible happens if they
1452 ask questions or their hooks get errors. */
1453 if (! b->base_buffer)
1455 struct buffer *other;
1457 GCPRO1 (buffer);
1459 for (other = all_buffers; other; other = other->next)
1460 /* all_buffers contains dead buffers too;
1461 don't re-kill them. */
1462 if (other->base_buffer == b && !NILP (BVAR (other, name)))
1464 Lisp_Object buffer;
1465 XSETBUFFER (buffer, other);
1466 Fkill_buffer (buffer);
1469 UNGCPRO;
1472 /* Make this buffer not be current.
1473 In the process, notice if this is the sole visible buffer
1474 and give up if so. */
1475 if (b == current_buffer)
1477 tem = Fother_buffer (buffer, Qnil, Qnil);
1478 Fset_buffer (tem);
1479 if (b == current_buffer)
1480 return Qnil;
1483 /* Notice if the buffer to kill is the sole visible buffer
1484 when we're currently in the mini-buffer, and give up if so. */
1485 XSETBUFFER (tem, current_buffer);
1486 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1488 tem = Fother_buffer (buffer, Qnil, Qnil);
1489 if (EQ (buffer, tem))
1490 return Qnil;
1493 /* Now there is no question: we can kill the buffer. */
1495 #ifdef CLASH_DETECTION
1496 /* Unlock this buffer's file, if it is locked. */
1497 unlock_buffer (b);
1498 #endif /* CLASH_DETECTION */
1500 GCPRO1 (buffer);
1501 kill_buffer_processes (buffer);
1502 UNGCPRO;
1504 /* Killing buffer processes may run sentinels which may
1505 have called kill-buffer. */
1507 if (NILP (BVAR (b, name)))
1508 return Qnil;
1510 /* These may run Lisp code and into infinite loops (if someone
1511 insisted on circular lists) so allow quitting here. */
1512 replace_buffer_in_windows (buffer);
1513 frames_discard_buffer (buffer);
1515 clear_charpos_cache (b);
1517 tem = Vinhibit_quit;
1518 Vinhibit_quit = Qt;
1519 /* Remove the buffer from the list of all buffers. */
1520 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1521 /* If replace_buffer_in_windows didn't do its job correctly fix that
1522 now. */
1523 replace_buffer_in_windows_safely (buffer);
1524 Vinhibit_quit = tem;
1526 /* Delete any auto-save file, if we saved it in this session.
1527 But not if the buffer is modified. */
1528 if (STRINGP (BVAR (b, auto_save_file_name))
1529 && BUF_AUTOSAVE_MODIFF (b) != 0
1530 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1531 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1532 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1534 Lisp_Object tem;
1535 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1536 if (! NILP (tem))
1537 internal_delete_file (BVAR (b, auto_save_file_name));
1540 if (b->base_buffer)
1542 /* Unchain all markers that belong to this indirect buffer.
1543 Don't unchain the markers that belong to the base buffer
1544 or its other indirect buffers. */
1545 for (m = BUF_MARKERS (b); m; )
1547 struct Lisp_Marker *next = m->next;
1548 if (m->buffer == b)
1549 unchain_marker (m);
1550 m = next;
1553 else
1555 /* Unchain all markers of this buffer and its indirect buffers.
1556 and leave them pointing nowhere. */
1557 for (m = BUF_MARKERS (b); m; )
1559 struct Lisp_Marker *next = m->next;
1560 m->buffer = 0;
1561 m->next = NULL;
1562 m = next;
1564 BUF_MARKERS (b) = NULL;
1565 BUF_INTERVALS (b) = NULL_INTERVAL;
1567 /* Perhaps we should explicitly free the interval tree here... */
1570 /* Reset the local variables, so that this buffer's local values
1571 won't be protected from GC. They would be protected
1572 if they happened to remain encached in their symbols.
1573 This gets rid of them for certain. */
1574 swap_out_buffer_local_variables (b);
1575 reset_buffer_local_variables (b, 1);
1577 BVAR (b, name) = Qnil;
1579 BLOCK_INPUT;
1580 if (! b->base_buffer)
1581 free_buffer_text (b);
1583 if (b->newline_cache)
1585 free_region_cache (b->newline_cache);
1586 b->newline_cache = 0;
1588 if (b->width_run_cache)
1590 free_region_cache (b->width_run_cache);
1591 b->width_run_cache = 0;
1593 BVAR (b, width_table) = Qnil;
1594 UNBLOCK_INPUT;
1595 BVAR (b, undo_list) = Qnil;
1597 /* Run buffer-list-update-hook. */
1598 if (!NILP (Vrun_hooks))
1599 call1 (Vrun_hooks, Qbuffer_list_update_hook);
1601 return Qt;
1604 /* Move association for BUFFER to the front of buffer (a)lists. Since
1605 we do this each time BUFFER is selected visibly, the more recently
1606 selected buffers are always closer to the front of those lists. This
1607 means that other_buffer is more likely to choose a relevant buffer.
1609 Note that this moves BUFFER to the front of the buffer lists of the
1610 selected frame even if BUFFER is not shown there. If BUFFER is not
1611 shown in the selected frame, consider the present behavior a feature.
1612 `select-window' gets this right since it shows BUFFER in the selected
1613 window when calling us. */
1615 void
1616 record_buffer (Lisp_Object buffer)
1618 Lisp_Object aelt, link, tem;
1619 register struct frame *f = XFRAME (selected_frame);
1620 register struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
1622 CHECK_BUFFER (buffer);
1624 /* Update Vbuffer_alist (we know that it has an entry for BUFFER).
1625 Don't allow quitting since this might leave the buffer list in an
1626 inconsistent state. */
1627 tem = Vinhibit_quit;
1628 Vinhibit_quit = Qt;
1629 aelt = Frassq (buffer, Vbuffer_alist);
1630 link = Fmemq (aelt, Vbuffer_alist);
1631 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1632 XSETCDR (link, Vbuffer_alist);
1633 Vbuffer_alist = link;
1634 Vinhibit_quit = tem;
1636 /* Update buffer list of selected frame. */
1637 f->buffer_list = Fcons (buffer, Fdelq (buffer, f->buffer_list));
1638 f->buried_buffer_list = Fdelq (buffer, f->buried_buffer_list);
1640 /* Run buffer-list-update-hook. */
1641 if (!NILP (Vrun_hooks))
1642 call1 (Vrun_hooks, Qbuffer_list_update_hook);
1645 DEFUN ("record-buffer", Frecord_buffer, Srecord_buffer, 1, 1, 0,
1646 doc: /* Move BUFFER to the front of the buffer list.
1647 Return BUFFER. */)
1648 (Lisp_Object buffer)
1650 CHECK_BUFFER (buffer);
1652 record_buffer (buffer);
1654 return buffer;
1657 /* Move BUFFER to the end of the buffer (a)lists. Do nothing if the
1658 buffer is killed. For the selected frame's buffer list this moves
1659 BUFFER to its end even if it was never shown in that frame. If
1660 this happens we have a feature, hence `unrecord-buffer' should be
1661 called only when BUFFER was shown in the selected frame. */
1663 DEFUN ("unrecord-buffer", Funrecord_buffer, Sunrecord_buffer, 1, 1, 0,
1664 doc: /* Move BUFFER to the end of the buffer list.
1665 Return BUFFER. */)
1666 (Lisp_Object buffer)
1668 Lisp_Object aelt, link, tem;
1669 register struct frame *f = XFRAME (selected_frame);
1671 CHECK_BUFFER (buffer);
1673 /* Update Vbuffer_alist (we know that it has an entry for BUFFER).
1674 Don't allow quitting since this might leave the buffer list in an
1675 inconsistent state. */
1676 tem = Vinhibit_quit;
1677 Vinhibit_quit = Qt;
1678 aelt = Frassq (buffer, Vbuffer_alist);
1679 link = Fmemq (aelt, Vbuffer_alist);
1680 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1681 XSETCDR (link, Qnil);
1682 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1683 Vinhibit_quit = tem;
1685 /* Update buffer lists of selected frame. */
1686 f->buffer_list = Fdelq (buffer, f->buffer_list);
1687 f->buried_buffer_list = Fcons (buffer, Fdelq (buffer, f->buried_buffer_list));
1689 /* Run buffer-list-update-hook. */
1690 if (!NILP (Vrun_hooks))
1691 call1 (Vrun_hooks, Qbuffer_list_update_hook);
1693 return buffer;
1696 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1697 doc: /* Set an appropriate major mode for BUFFER.
1698 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1699 according to `default-major-mode'.
1700 Use this function before selecting the buffer, since it may need to inspect
1701 the current buffer's major mode. */)
1702 (Lisp_Object buffer)
1704 int count;
1705 Lisp_Object function;
1707 CHECK_BUFFER (buffer);
1709 if (STRINGP (BVAR (XBUFFER (buffer), name))
1710 && strcmp (SSDATA (BVAR (XBUFFER (buffer), name)), "*scratch*") == 0)
1711 function = find_symbol_value (intern ("initial-major-mode"));
1712 else
1714 function = BVAR (&buffer_defaults, major_mode);
1715 if (NILP (function)
1716 && NILP (Fget (BVAR (current_buffer, major_mode), Qmode_class)))
1717 function = BVAR (current_buffer, major_mode);
1720 if (NILP (function) || EQ (function, Qfundamental_mode))
1721 return Qnil;
1723 count = SPECPDL_INDEX ();
1725 /* To select a nonfundamental mode,
1726 select the buffer temporarily and then call the mode function. */
1728 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1730 Fset_buffer (buffer);
1731 call0 (function);
1733 return unbind_to (count, Qnil);
1736 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1737 doc: /* Return the current buffer as a Lisp object. */)
1738 (void)
1740 register Lisp_Object buf;
1741 XSETBUFFER (buf, current_buffer);
1742 return buf;
1745 /* Set the current buffer to B.
1747 We previously set windows_or_buffers_changed here to invalidate
1748 global unchanged information in beg_unchanged and end_unchanged.
1749 This is no longer necessary because we now compute unchanged
1750 information on a buffer-basis. Every action affecting other
1751 windows than the selected one requires a select_window at some
1752 time, and that increments windows_or_buffers_changed. */
1754 void
1755 set_buffer_internal (register struct buffer *b)
1757 if (current_buffer != b)
1758 set_buffer_internal_1 (b);
1761 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1762 This is used by redisplay. */
1764 void
1765 set_buffer_internal_1 (register struct buffer *b)
1767 register struct buffer *old_buf;
1768 register Lisp_Object tail;
1770 #ifdef USE_MMAP_FOR_BUFFERS
1771 if (b->text->beg == NULL)
1772 enlarge_buffer_text (b, 0);
1773 #endif /* USE_MMAP_FOR_BUFFERS */
1775 if (current_buffer == b)
1776 return;
1778 old_buf = current_buffer;
1779 current_buffer = b;
1780 last_known_column_point = -1; /* invalidate indentation cache */
1782 if (old_buf)
1784 /* Put the undo list back in the base buffer, so that it appears
1785 that an indirect buffer shares the undo list of its base. */
1786 if (old_buf->base_buffer)
1787 BVAR (old_buf->base_buffer, undo_list) = BVAR (old_buf, undo_list);
1789 /* If the old current buffer has markers to record PT, BEGV and ZV
1790 when it is not current, update them now. */
1791 if (! NILP (BVAR (old_buf, pt_marker)))
1793 Lisp_Object obuf;
1794 XSETBUFFER (obuf, old_buf);
1795 set_marker_both (BVAR (old_buf, pt_marker), obuf,
1796 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1798 if (! NILP (BVAR (old_buf, begv_marker)))
1800 Lisp_Object obuf;
1801 XSETBUFFER (obuf, old_buf);
1802 set_marker_both (BVAR (old_buf, begv_marker), obuf,
1803 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1805 if (! NILP (BVAR (old_buf, zv_marker)))
1807 Lisp_Object obuf;
1808 XSETBUFFER (obuf, old_buf);
1809 set_marker_both (BVAR (old_buf, zv_marker), obuf,
1810 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1814 /* Get the undo list from the base buffer, so that it appears
1815 that an indirect buffer shares the undo list of its base. */
1816 if (b->base_buffer)
1817 BVAR (b, undo_list) = BVAR (b->base_buffer, undo_list);
1819 /* If the new current buffer has markers to record PT, BEGV and ZV
1820 when it is not current, fetch them now. */
1821 if (! NILP (BVAR (b, pt_marker)))
1823 BUF_PT (b) = marker_position (BVAR (b, pt_marker));
1824 BUF_PT_BYTE (b) = marker_byte_position (BVAR (b, pt_marker));
1826 if (! NILP (BVAR (b, begv_marker)))
1828 BUF_BEGV (b) = marker_position (BVAR (b, begv_marker));
1829 BUF_BEGV_BYTE (b) = marker_byte_position (BVAR (b, begv_marker));
1831 if (! NILP (BVAR (b, zv_marker)))
1833 BUF_ZV (b) = marker_position (BVAR (b, zv_marker));
1834 BUF_ZV_BYTE (b) = marker_byte_position (BVAR (b, zv_marker));
1837 /* Look down buffer's list of local Lisp variables
1838 to find and update any that forward into C variables. */
1842 for (tail = BVAR (b, local_var_alist); CONSP (tail); tail = XCDR (tail))
1844 Lisp_Object var = XCAR (XCAR (tail));
1845 struct Lisp_Symbol *sym = XSYMBOL (var);
1846 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1847 && SYMBOL_BLV (sym)->fwd)
1848 /* Just reference the variable
1849 to cause it to become set for this buffer. */
1850 Fsymbol_value (var);
1853 /* Do the same with any others that were local to the previous buffer */
1854 while (b != old_buf && (b = old_buf, b));
1857 /* Switch to buffer B temporarily for redisplay purposes.
1858 This avoids certain things that don't need to be done within redisplay. */
1860 void
1861 set_buffer_temp (struct buffer *b)
1863 register struct buffer *old_buf;
1865 if (current_buffer == b)
1866 return;
1868 old_buf = current_buffer;
1869 current_buffer = b;
1871 if (old_buf)
1873 /* If the old current buffer has markers to record PT, BEGV and ZV
1874 when it is not current, update them now. */
1875 if (! NILP (BVAR (old_buf, pt_marker)))
1877 Lisp_Object obuf;
1878 XSETBUFFER (obuf, old_buf);
1879 set_marker_both (BVAR (old_buf, pt_marker), obuf,
1880 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1882 if (! NILP (BVAR (old_buf, begv_marker)))
1884 Lisp_Object obuf;
1885 XSETBUFFER (obuf, old_buf);
1886 set_marker_both (BVAR (old_buf, begv_marker), obuf,
1887 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1889 if (! NILP (BVAR (old_buf, zv_marker)))
1891 Lisp_Object obuf;
1892 XSETBUFFER (obuf, old_buf);
1893 set_marker_both (BVAR (old_buf, zv_marker), obuf,
1894 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1898 /* If the new current buffer has markers to record PT, BEGV and ZV
1899 when it is not current, fetch them now. */
1900 if (! NILP (BVAR (b, pt_marker)))
1902 BUF_PT (b) = marker_position (BVAR (b, pt_marker));
1903 BUF_PT_BYTE (b) = marker_byte_position (BVAR (b, pt_marker));
1905 if (! NILP (BVAR (b, begv_marker)))
1907 BUF_BEGV (b) = marker_position (BVAR (b, begv_marker));
1908 BUF_BEGV_BYTE (b) = marker_byte_position (BVAR (b, begv_marker));
1910 if (! NILP (BVAR (b, zv_marker)))
1912 BUF_ZV (b) = marker_position (BVAR (b, zv_marker));
1913 BUF_ZV_BYTE (b) = marker_byte_position (BVAR (b, zv_marker));
1917 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1918 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1919 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1920 also `save-excursion' when you want to make a buffer current
1921 temporarily. This function does not display the buffer, so its effect
1922 ends when the current command terminates. Use `switch-to-buffer' or
1923 `pop-to-buffer' to switch buffers permanently. */)
1924 (register Lisp_Object buffer_or_name)
1926 register Lisp_Object buffer;
1927 buffer = Fget_buffer (buffer_or_name);
1928 if (NILP (buffer))
1929 nsberror (buffer_or_name);
1930 if (NILP (BVAR (XBUFFER (buffer), name)))
1931 error ("Selecting deleted buffer");
1932 set_buffer_internal (XBUFFER (buffer));
1933 return buffer;
1936 /* Set the current buffer to BUFFER provided it is alive. */
1938 Lisp_Object
1939 set_buffer_if_live (Lisp_Object buffer)
1941 if (! NILP (BVAR (XBUFFER (buffer), name)))
1942 Fset_buffer (buffer);
1943 return Qnil;
1946 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1947 Sbarf_if_buffer_read_only, 0, 0, 0,
1948 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1949 (void)
1951 if (!NILP (BVAR (current_buffer, read_only))
1952 && NILP (Vinhibit_read_only))
1953 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1954 return Qnil;
1957 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1958 doc: /* Delete the entire contents of the current buffer.
1959 Any narrowing restriction in effect (see `narrow-to-region') is removed,
1960 so the buffer is truly empty after this. */)
1961 (void)
1963 Fwiden ();
1965 del_range (BEG, Z);
1967 current_buffer->last_window_start = 1;
1968 /* Prevent warnings, or suspension of auto saving, that would happen
1969 if future size is less than past size. Use of erase-buffer
1970 implies that the future text is not really related to the past text. */
1971 XSETFASTINT (BVAR (current_buffer, save_length), 0);
1972 return Qnil;
1975 void
1976 validate_region (register Lisp_Object *b, register Lisp_Object *e)
1978 CHECK_NUMBER_COERCE_MARKER (*b);
1979 CHECK_NUMBER_COERCE_MARKER (*e);
1981 if (XINT (*b) > XINT (*e))
1983 Lisp_Object tem;
1984 tem = *b; *b = *e; *e = tem;
1987 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1988 && XINT (*e) <= ZV))
1989 args_out_of_range (*b, *e);
1992 /* Advance BYTE_POS up to a character boundary
1993 and return the adjusted position. */
1995 static int
1996 advance_to_char_boundary (EMACS_INT byte_pos)
1998 int c;
2000 if (byte_pos == BEG)
2001 /* Beginning of buffer is always a character boundary. */
2002 return BEG;
2004 c = FETCH_BYTE (byte_pos);
2005 if (! CHAR_HEAD_P (c))
2007 /* We should advance BYTE_POS only when C is a constituent of a
2008 multibyte sequence. */
2009 EMACS_INT orig_byte_pos = byte_pos;
2013 byte_pos--;
2014 c = FETCH_BYTE (byte_pos);
2016 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2017 INC_POS (byte_pos);
2018 if (byte_pos < orig_byte_pos)
2019 byte_pos = orig_byte_pos;
2020 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2021 surely advance to the correct character boundary. If C is
2022 not, BYTE_POS was unchanged. */
2025 return byte_pos;
2028 #ifdef REL_ALLOC
2029 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2030 #endif /* REL_ALLOC */
2032 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2033 1, 1, 0,
2034 doc: /* Swap the text between current buffer and BUFFER. */)
2035 (Lisp_Object buffer)
2037 struct buffer *other_buffer;
2038 CHECK_BUFFER (buffer);
2039 other_buffer = XBUFFER (buffer);
2041 if (NILP (BVAR (other_buffer, name)))
2042 error ("Cannot swap a dead buffer's text");
2044 /* Actually, it probably works just fine.
2045 * if (other_buffer == current_buffer)
2046 * error ("Cannot swap a buffer's text with itself"); */
2048 /* Actually, this may be workable as well, tho probably only if they're
2049 *both* indirect. */
2050 if (other_buffer->base_buffer
2051 || current_buffer->base_buffer)
2052 error ("Cannot swap indirect buffers's text");
2054 { /* This is probably harder to make work. */
2055 struct buffer *other;
2056 for (other = all_buffers; other; other = other->next)
2057 if (other->base_buffer == other_buffer
2058 || other->base_buffer == current_buffer)
2059 error ("One of the buffers to swap has indirect buffers");
2062 #define swapfield(field, type) \
2063 do { \
2064 type tmp##field = other_buffer->field; \
2065 other_buffer->field = current_buffer->field; \
2066 current_buffer->field = tmp##field; \
2067 } while (0)
2068 #define swapfield_(field, type) \
2069 do { \
2070 type tmp##field = BVAR (other_buffer, field); \
2071 BVAR (other_buffer, field) = BVAR (current_buffer, field); \
2072 BVAR (current_buffer, field) = tmp##field; \
2073 } while (0)
2075 swapfield (own_text, struct buffer_text);
2076 eassert (current_buffer->text == &current_buffer->own_text);
2077 eassert (other_buffer->text == &other_buffer->own_text);
2078 #ifdef REL_ALLOC
2079 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2080 (POINTER_TYPE **) &other_buffer->own_text.beg);
2081 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2082 (POINTER_TYPE **) &current_buffer->own_text.beg);
2083 #endif /* REL_ALLOC */
2085 swapfield (pt, EMACS_INT);
2086 swapfield (pt_byte, EMACS_INT);
2087 swapfield (begv, EMACS_INT);
2088 swapfield (begv_byte, EMACS_INT);
2089 swapfield (zv, EMACS_INT);
2090 swapfield (zv_byte, EMACS_INT);
2091 eassert (!current_buffer->base_buffer);
2092 eassert (!other_buffer->base_buffer);
2093 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2094 swapfield (newline_cache, struct region_cache *);
2095 swapfield (width_run_cache, struct region_cache *);
2096 current_buffer->prevent_redisplay_optimizations_p = 1;
2097 other_buffer->prevent_redisplay_optimizations_p = 1;
2098 swapfield (overlays_before, struct Lisp_Overlay *);
2099 swapfield (overlays_after, struct Lisp_Overlay *);
2100 swapfield (overlay_center, EMACS_INT);
2101 swapfield_ (undo_list, Lisp_Object);
2102 swapfield_ (mark, Lisp_Object);
2103 swapfield_ (enable_multibyte_characters, Lisp_Object);
2104 swapfield_ (bidi_display_reordering, Lisp_Object);
2105 swapfield_ (bidi_paragraph_direction, Lisp_Object);
2106 /* FIXME: Not sure what we should do with these *_marker fields.
2107 Hopefully they're just nil anyway. */
2108 swapfield_ (pt_marker, Lisp_Object);
2109 swapfield_ (begv_marker, Lisp_Object);
2110 swapfield_ (zv_marker, Lisp_Object);
2111 BVAR (current_buffer, point_before_scroll) = Qnil;
2112 BVAR (other_buffer, point_before_scroll) = Qnil;
2114 current_buffer->text->modiff++; other_buffer->text->modiff++;
2115 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2116 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2117 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2118 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2119 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2120 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2122 struct Lisp_Marker *m;
2123 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2124 if (m->buffer == other_buffer)
2125 m->buffer = current_buffer;
2126 else
2127 /* Since there's no indirect buffer in sight, markers on
2128 BUF_MARKERS(buf) should either be for `buf' or dead. */
2129 eassert (!m->buffer);
2130 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2131 if (m->buffer == current_buffer)
2132 m->buffer = other_buffer;
2133 else
2134 /* Since there's no indirect buffer in sight, markers on
2135 BUF_MARKERS(buf) should either be for `buf' or dead. */
2136 eassert (!m->buffer);
2138 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2139 So since we just swapped the markers between the two buffers, we need
2140 to undo the effect of this swap for window markers. */
2141 Lisp_Object w = Fselected_window (), ws = Qnil;
2142 Lisp_Object buf1, buf2;
2143 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2145 while (NILP (Fmemq (w, ws)))
2147 ws = Fcons (w, ws);
2148 if (MARKERP (XWINDOW (w)->pointm)
2149 && (EQ (XWINDOW (w)->buffer, buf1)
2150 || EQ (XWINDOW (w)->buffer, buf2)))
2151 Fset_marker (XWINDOW (w)->pointm,
2152 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2153 XWINDOW (w)->buffer);
2154 w = Fnext_window (w, Qt, Qt);
2158 if (current_buffer->text->intervals)
2159 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2160 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2161 if (other_buffer->text->intervals)
2162 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2163 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2165 return Qnil;
2168 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2169 1, 1, 0,
2170 doc: /* Set the multibyte flag of the current buffer to FLAG.
2171 If FLAG is t, this makes the buffer a multibyte buffer.
2172 If FLAG is nil, this makes the buffer a single-byte buffer.
2173 In these cases, the buffer contents remain unchanged as a sequence of
2174 bytes but the contents viewed as characters do change.
2175 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2176 all eight-bit bytes to eight-bit characters.
2177 If the multibyte flag was really changed, undo information of the
2178 current buffer is cleared. */)
2179 (Lisp_Object flag)
2181 struct Lisp_Marker *tail, *markers;
2182 struct buffer *other;
2183 EMACS_INT begv, zv;
2184 int narrowed = (BEG != BEGV || Z != ZV);
2185 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2186 Lisp_Object old_undo = BVAR (current_buffer, undo_list);
2187 struct gcpro gcpro1;
2189 if (current_buffer->base_buffer)
2190 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2192 /* Do nothing if nothing actually changes. */
2193 if (NILP (flag) == NILP (BVAR (current_buffer, enable_multibyte_characters)))
2194 return flag;
2196 GCPRO1 (old_undo);
2198 /* Don't record these buffer changes. We will put a special undo entry
2199 instead. */
2200 BVAR (current_buffer, undo_list) = Qt;
2202 /* If the cached position is for this buffer, clear it out. */
2203 clear_charpos_cache (current_buffer);
2205 if (NILP (flag))
2206 begv = BEGV_BYTE, zv = ZV_BYTE;
2207 else
2208 begv = BEGV, zv = ZV;
2210 if (narrowed)
2211 Fwiden ();
2213 if (NILP (flag))
2215 EMACS_INT pos, stop;
2216 unsigned char *p;
2218 /* Do this first, so it can use CHAR_TO_BYTE
2219 to calculate the old correspondences. */
2220 set_intervals_multibyte (0);
2222 BVAR (current_buffer, enable_multibyte_characters) = Qnil;
2224 Z = Z_BYTE;
2225 BEGV = BEGV_BYTE;
2226 ZV = ZV_BYTE;
2227 GPT = GPT_BYTE;
2228 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2231 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2232 tail->charpos = tail->bytepos;
2234 /* Convert multibyte form of 8-bit characters to unibyte. */
2235 pos = BEG;
2236 stop = GPT;
2237 p = BEG_ADDR;
2238 while (1)
2240 int c, bytes;
2242 if (pos == stop)
2244 if (pos == Z)
2245 break;
2246 p = GAP_END_ADDR;
2247 stop = Z;
2249 if (ASCII_BYTE_P (*p))
2250 p++, pos++;
2251 else if (CHAR_BYTE8_HEAD_P (*p))
2253 c = STRING_CHAR_AND_LENGTH (p, bytes);
2254 /* Delete all bytes for this 8-bit character but the
2255 last one, and change the last one to the character
2256 code. */
2257 bytes--;
2258 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2259 p = GAP_END_ADDR;
2260 *p++ = c;
2261 pos++;
2262 if (begv > pos)
2263 begv -= bytes;
2264 if (zv > pos)
2265 zv -= bytes;
2266 stop = Z;
2268 else
2270 bytes = BYTES_BY_CHAR_HEAD (*p);
2271 p += bytes, pos += bytes;
2274 if (narrowed)
2275 Fnarrow_to_region (make_number (begv), make_number (zv));
2277 else
2279 EMACS_INT pt = PT;
2280 EMACS_INT pos, stop;
2281 unsigned char *p, *pend;
2283 /* Be sure not to have a multibyte sequence striding over the GAP.
2284 Ex: We change this: "...abc\302 _GAP_ \241def..."
2285 to: "...abc _GAP_ \302\241def..." */
2287 if (EQ (flag, Qt)
2288 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2289 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2291 unsigned char *p = GPT_ADDR - 1;
2293 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2294 if (LEADING_CODE_P (*p))
2296 EMACS_INT new_gpt = GPT_BYTE - (GPT_ADDR - p);
2298 move_gap_both (new_gpt, new_gpt);
2302 /* Make the buffer contents valid as multibyte by converting
2303 8-bit characters to multibyte form. */
2304 pos = BEG;
2305 stop = GPT;
2306 p = BEG_ADDR;
2307 pend = GPT_ADDR;
2308 while (1)
2310 int bytes;
2312 if (pos == stop)
2314 if (pos == Z)
2315 break;
2316 p = GAP_END_ADDR;
2317 pend = Z_ADDR;
2318 stop = Z;
2321 if (ASCII_BYTE_P (*p))
2322 p++, pos++;
2323 else if (EQ (flag, Qt)
2324 && ! CHAR_BYTE8_HEAD_P (*p)
2325 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2326 p += bytes, pos += bytes;
2327 else
2329 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2330 int c;
2332 c = BYTE8_TO_CHAR (*p);
2333 bytes = CHAR_STRING (c, tmp);
2334 *p = tmp[0];
2335 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2336 bytes--;
2337 insert_1_both ((char *) tmp + 1, bytes, bytes, 1, 0, 0);
2338 /* Now the gap is after the just inserted data. */
2339 pos = GPT;
2340 p = GAP_END_ADDR;
2341 if (pos <= begv)
2342 begv += bytes;
2343 if (pos <= zv)
2344 zv += bytes;
2345 if (pos <= pt)
2346 pt += bytes;
2347 pend = Z_ADDR;
2348 stop = Z;
2352 if (pt != PT)
2353 TEMP_SET_PT (pt);
2355 if (narrowed)
2356 Fnarrow_to_region (make_number (begv), make_number (zv));
2358 /* Do this first, so that chars_in_text asks the right question.
2359 set_intervals_multibyte needs it too. */
2360 BVAR (current_buffer, enable_multibyte_characters) = Qt;
2362 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2363 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2365 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2367 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2368 if (BEGV_BYTE > GPT_BYTE)
2369 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2370 else
2371 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2373 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2374 if (ZV_BYTE > GPT_BYTE)
2375 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2376 else
2377 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2380 EMACS_INT pt_byte = advance_to_char_boundary (PT_BYTE);
2381 EMACS_INT pt;
2383 if (pt_byte > GPT_BYTE)
2384 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2385 else
2386 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2387 TEMP_SET_PT_BOTH (pt, pt_byte);
2390 tail = markers = BUF_MARKERS (current_buffer);
2392 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2393 getting confused by the markers that have not yet been updated.
2394 It is also a signal that it should never create a marker. */
2395 BUF_MARKERS (current_buffer) = NULL;
2397 for (; tail; tail = tail->next)
2399 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2400 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2403 /* Make sure no markers were put on the chain
2404 while the chain value was incorrect. */
2405 if (BUF_MARKERS (current_buffer))
2406 abort ();
2408 BUF_MARKERS (current_buffer) = markers;
2410 /* Do this last, so it can calculate the new correspondences
2411 between chars and bytes. */
2412 set_intervals_multibyte (1);
2415 if (!EQ (old_undo, Qt))
2417 /* Represent all the above changes by a special undo entry. */
2418 BVAR (current_buffer, undo_list) = Fcons (list3 (Qapply,
2419 intern ("set-buffer-multibyte"),
2420 NILP (flag) ? Qt : Qnil),
2421 old_undo);
2424 UNGCPRO;
2426 /* Changing the multibyteness of a buffer means that all windows
2427 showing that buffer must be updated thoroughly. */
2428 current_buffer->prevent_redisplay_optimizations_p = 1;
2429 ++windows_or_buffers_changed;
2431 /* Copy this buffer's new multibyte status
2432 into all of its indirect buffers. */
2433 for (other = all_buffers; other; other = other->next)
2434 if (other->base_buffer == current_buffer && !NILP (BVAR (other, name)))
2436 BVAR (other, enable_multibyte_characters)
2437 = BVAR (current_buffer, enable_multibyte_characters);
2438 other->prevent_redisplay_optimizations_p = 1;
2441 /* Restore the modifiedness of the buffer. */
2442 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2443 Fset_buffer_modified_p (Qnil);
2445 /* Update coding systems of this buffer's process (if any). */
2447 Lisp_Object process;
2449 process = Fget_buffer_process (Fcurrent_buffer ());
2450 if (PROCESSP (process))
2451 setup_process_coding_systems (process);
2454 return flag;
2457 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2458 0, 0, 0,
2459 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2460 Most local variable bindings are eliminated so that the default values
2461 become effective once more. Also, the syntax table is set from
2462 `standard-syntax-table', the local keymap is set to nil,
2463 and the abbrev table from `fundamental-mode-abbrev-table'.
2464 This function also forces redisplay of the mode line.
2466 Every function to select a new major mode starts by
2467 calling this function.
2469 As a special exception, local variables whose names have
2470 a non-nil `permanent-local' property are not eliminated by this function.
2472 The first thing this function does is run
2473 the normal hook `change-major-mode-hook'. */)
2474 (void)
2476 if (!NILP (Vrun_hooks))
2477 call1 (Vrun_hooks, Qchange_major_mode_hook);
2479 /* Make sure none of the bindings in local_var_alist
2480 remain swapped in, in their symbols. */
2482 swap_out_buffer_local_variables (current_buffer);
2484 /* Actually eliminate all local bindings of this buffer. */
2486 reset_buffer_local_variables (current_buffer, 0);
2488 /* Force mode-line redisplay. Useful here because all major mode
2489 commands call this function. */
2490 update_mode_lines++;
2492 return Qnil;
2495 /* Make sure no local variables remain set up with buffer B
2496 for their current values. */
2498 static void
2499 swap_out_buffer_local_variables (struct buffer *b)
2501 Lisp_Object oalist, alist, buffer;
2503 XSETBUFFER (buffer, b);
2504 oalist = BVAR (b, local_var_alist);
2506 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2508 Lisp_Object sym = XCAR (XCAR (alist));
2509 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2510 /* Need not do anything if some other buffer's binding is
2511 now encached. */
2512 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2514 /* Symbol is set up for this buffer's old local value:
2515 swap it out! */
2516 swap_in_global_binding (XSYMBOL (sym));
2521 /* Find all the overlays in the current buffer that contain position POS.
2522 Return the number found, and store them in a vector in *VEC_PTR.
2523 Store in *LEN_PTR the size allocated for the vector.
2524 Store in *NEXT_PTR the next position after POS where an overlay starts,
2525 or ZV if there are no more overlays between POS and ZV.
2526 Store in *PREV_PTR the previous position before POS where an overlay ends,
2527 or where an overlay starts which ends at or after POS;
2528 or BEGV if there are no such overlays from BEGV to POS.
2529 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2531 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2532 when this function is called.
2534 If EXTEND is non-zero, we make the vector bigger if necessary.
2535 If EXTEND is zero, we never extend the vector,
2536 and we store only as many overlays as will fit.
2537 But we still return the total number of overlays.
2539 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2540 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2541 default (BEGV or ZV). */
2544 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2545 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2547 Lisp_Object overlay, start, end;
2548 struct Lisp_Overlay *tail;
2549 int idx = 0;
2550 int len = *len_ptr;
2551 Lisp_Object *vec = *vec_ptr;
2552 EMACS_INT next = ZV;
2553 EMACS_INT prev = BEGV;
2554 int inhibit_storing = 0;
2556 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2558 EMACS_INT startpos, endpos;
2560 XSETMISC (overlay, tail);
2562 start = OVERLAY_START (overlay);
2563 end = OVERLAY_END (overlay);
2564 endpos = OVERLAY_POSITION (end);
2565 if (endpos < pos)
2567 if (prev < endpos)
2568 prev = endpos;
2569 break;
2571 startpos = OVERLAY_POSITION (start);
2572 /* This one ends at or after POS
2573 so its start counts for PREV_PTR if it's before POS. */
2574 if (prev < startpos && startpos < pos)
2575 prev = startpos;
2576 if (endpos == pos)
2577 continue;
2578 if (startpos <= pos)
2580 if (idx == len)
2582 /* The supplied vector is full.
2583 Either make it bigger, or don't store any more in it. */
2584 if (extend)
2586 /* Make it work with an initial len == 0. */
2587 len *= 2;
2588 if (len == 0)
2589 len = 4;
2590 *len_ptr = len;
2591 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2592 *vec_ptr = vec;
2594 else
2595 inhibit_storing = 1;
2598 if (!inhibit_storing)
2599 vec[idx] = overlay;
2600 /* Keep counting overlays even if we can't return them all. */
2601 idx++;
2603 else if (startpos < next)
2604 next = startpos;
2607 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2609 EMACS_INT startpos, endpos;
2611 XSETMISC (overlay, tail);
2613 start = OVERLAY_START (overlay);
2614 end = OVERLAY_END (overlay);
2615 startpos = OVERLAY_POSITION (start);
2616 if (pos < startpos)
2618 if (startpos < next)
2619 next = startpos;
2620 break;
2622 endpos = OVERLAY_POSITION (end);
2623 if (pos < endpos)
2625 if (idx == len)
2627 if (extend)
2629 /* Make it work with an initial len == 0. */
2630 len *= 2;
2631 if (len == 0)
2632 len = 4;
2633 *len_ptr = len;
2634 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2635 *vec_ptr = vec;
2637 else
2638 inhibit_storing = 1;
2641 if (!inhibit_storing)
2642 vec[idx] = overlay;
2643 idx++;
2645 if (startpos < pos && startpos > prev)
2646 prev = startpos;
2648 else if (endpos < pos && endpos > prev)
2649 prev = endpos;
2650 else if (endpos == pos && startpos > prev
2651 && (!change_req || startpos < pos))
2652 prev = startpos;
2655 if (next_ptr)
2656 *next_ptr = next;
2657 if (prev_ptr)
2658 *prev_ptr = prev;
2659 return idx;
2662 /* Find all the overlays in the current buffer that overlap the range
2663 BEG-END, or are empty at BEG, or are empty at END provided END
2664 denotes the position at the end of the current buffer.
2666 Return the number found, and store them in a vector in *VEC_PTR.
2667 Store in *LEN_PTR the size allocated for the vector.
2668 Store in *NEXT_PTR the next position after POS where an overlay starts,
2669 or ZV if there are no more overlays.
2670 Store in *PREV_PTR the previous position before POS where an overlay ends,
2671 or BEGV if there are no previous overlays.
2672 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2674 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2675 when this function is called.
2677 If EXTEND is non-zero, we make the vector bigger if necessary.
2678 If EXTEND is zero, we never extend the vector,
2679 and we store only as many overlays as will fit.
2680 But we still return the total number of overlays. */
2682 static int
2683 overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
2684 Lisp_Object **vec_ptr, int *len_ptr,
2685 EMACS_INT *next_ptr, EMACS_INT *prev_ptr)
2687 Lisp_Object overlay, ostart, oend;
2688 struct Lisp_Overlay *tail;
2689 int idx = 0;
2690 int len = *len_ptr;
2691 Lisp_Object *vec = *vec_ptr;
2692 EMACS_INT next = ZV;
2693 EMACS_INT prev = BEGV;
2694 int inhibit_storing = 0;
2695 int end_is_Z = end == Z;
2697 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2699 EMACS_INT startpos, endpos;
2701 XSETMISC (overlay, tail);
2703 ostart = OVERLAY_START (overlay);
2704 oend = OVERLAY_END (overlay);
2705 endpos = OVERLAY_POSITION (oend);
2706 if (endpos < beg)
2708 if (prev < endpos)
2709 prev = endpos;
2710 break;
2712 startpos = OVERLAY_POSITION (ostart);
2713 /* Count an interval if it overlaps the range, is empty at the
2714 start of the range, or is empty at END provided END denotes the
2715 end of the buffer. */
2716 if ((beg < endpos && startpos < end)
2717 || (startpos == endpos
2718 && (beg == endpos || (end_is_Z && endpos == end))))
2720 if (idx == len)
2722 /* The supplied vector is full.
2723 Either make it bigger, or don't store any more in it. */
2724 if (extend)
2726 /* Make it work with an initial len == 0. */
2727 len *= 2;
2728 if (len == 0)
2729 len = 4;
2730 *len_ptr = len;
2731 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2732 *vec_ptr = vec;
2734 else
2735 inhibit_storing = 1;
2738 if (!inhibit_storing)
2739 vec[idx] = overlay;
2740 /* Keep counting overlays even if we can't return them all. */
2741 idx++;
2743 else if (startpos < next)
2744 next = startpos;
2747 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2749 EMACS_INT startpos, endpos;
2751 XSETMISC (overlay, tail);
2753 ostart = OVERLAY_START (overlay);
2754 oend = OVERLAY_END (overlay);
2755 startpos = OVERLAY_POSITION (ostart);
2756 if (end < startpos)
2758 if (startpos < next)
2759 next = startpos;
2760 break;
2762 endpos = OVERLAY_POSITION (oend);
2763 /* Count an interval if it overlaps the range, is empty at the
2764 start of the range, or is empty at END provided END denotes the
2765 end of the buffer. */
2766 if ((beg < endpos && startpos < end)
2767 || (startpos == endpos
2768 && (beg == endpos || (end_is_Z && endpos == end))))
2770 if (idx == len)
2772 if (extend)
2774 /* Make it work with an initial len == 0. */
2775 len *= 2;
2776 if (len == 0)
2777 len = 4;
2778 *len_ptr = len;
2779 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2780 *vec_ptr = vec;
2782 else
2783 inhibit_storing = 1;
2786 if (!inhibit_storing)
2787 vec[idx] = overlay;
2788 idx++;
2790 else if (endpos < beg && endpos > prev)
2791 prev = endpos;
2794 if (next_ptr)
2795 *next_ptr = next;
2796 if (prev_ptr)
2797 *prev_ptr = prev;
2798 return idx;
2802 /* Return non-zero if there exists an overlay with a non-nil
2803 `mouse-face' property overlapping OVERLAY. */
2806 mouse_face_overlay_overlaps (Lisp_Object overlay)
2808 EMACS_INT start = OVERLAY_POSITION (OVERLAY_START (overlay));
2809 EMACS_INT end = OVERLAY_POSITION (OVERLAY_END (overlay));
2810 int n, i, size;
2811 Lisp_Object *v, tem;
2813 size = 10;
2814 v = (Lisp_Object *) alloca (size * sizeof *v);
2815 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2816 if (n > size)
2818 v = (Lisp_Object *) alloca (n * sizeof *v);
2819 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2822 for (i = 0; i < n; ++i)
2823 if (!EQ (v[i], overlay)
2824 && (tem = Foverlay_get (overlay, Qmouse_face),
2825 !NILP (tem)))
2826 break;
2828 return i < n;
2833 /* Fast function to just test if we're at an overlay boundary. */
2835 overlay_touches_p (EMACS_INT pos)
2837 Lisp_Object overlay;
2838 struct Lisp_Overlay *tail;
2840 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2842 EMACS_INT endpos;
2844 XSETMISC (overlay ,tail);
2845 if (!OVERLAYP (overlay))
2846 abort ();
2848 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2849 if (endpos < pos)
2850 break;
2851 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2852 return 1;
2855 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2857 EMACS_INT startpos;
2859 XSETMISC (overlay, tail);
2860 if (!OVERLAYP (overlay))
2861 abort ();
2863 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2864 if (pos < startpos)
2865 break;
2866 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2867 return 1;
2869 return 0;
2872 struct sortvec
2874 Lisp_Object overlay;
2875 EMACS_INT beg, end;
2876 int priority;
2879 static int
2880 compare_overlays (const void *v1, const void *v2)
2882 const struct sortvec *s1 = (const struct sortvec *) v1;
2883 const struct sortvec *s2 = (const struct sortvec *) v2;
2884 if (s1->priority != s2->priority)
2885 return s1->priority - s2->priority;
2886 if (s1->beg != s2->beg)
2887 return s1->beg - s2->beg;
2888 if (s1->end != s2->end)
2889 return s2->end - s1->end;
2890 return 0;
2893 /* Sort an array of overlays by priority. The array is modified in place.
2894 The return value is the new size; this may be smaller than the original
2895 size if some of the overlays were invalid or were window-specific. */
2897 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2899 int i, j;
2900 struct sortvec *sortvec;
2901 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2903 /* Put the valid and relevant overlays into sortvec. */
2905 for (i = 0, j = 0; i < noverlays; i++)
2907 Lisp_Object tem;
2908 Lisp_Object overlay;
2910 overlay = overlay_vec[i];
2911 if (OVERLAY_VALID (overlay)
2912 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2913 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2915 /* If we're interested in a specific window, then ignore
2916 overlays that are limited to some other window. */
2917 if (w)
2919 Lisp_Object window, clone_number;
2921 window = Foverlay_get (overlay, Qwindow);
2922 clone_number = Foverlay_get (overlay, Qclone_number);
2923 if (WINDOWP (window) && XWINDOW (window) != w
2924 && (! NUMBERP (clone_number)
2925 || XFASTINT (clone_number) != XFASTINT (w->clone_number)))
2926 continue;
2929 /* This overlay is good and counts: put it into sortvec. */
2930 sortvec[j].overlay = overlay;
2931 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2932 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2933 tem = Foverlay_get (overlay, Qpriority);
2934 if (INTEGERP (tem))
2935 sortvec[j].priority = XINT (tem);
2936 else
2937 sortvec[j].priority = 0;
2938 j++;
2941 noverlays = j;
2943 /* Sort the overlays into the proper order: increasing priority. */
2945 if (noverlays > 1)
2946 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2948 for (i = 0; i < noverlays; i++)
2949 overlay_vec[i] = sortvec[i].overlay;
2950 return (noverlays);
2953 struct sortstr
2955 Lisp_Object string, string2;
2956 int size;
2957 int priority;
2960 struct sortstrlist
2962 struct sortstr *buf; /* An array that expands as needed; never freed. */
2963 int size; /* Allocated length of that array. */
2964 int used; /* How much of the array is currently in use. */
2965 EMACS_INT bytes; /* Total length of the strings in buf. */
2968 /* Buffers for storing information about the overlays touching a given
2969 position. These could be automatic variables in overlay_strings, but
2970 it's more efficient to hold onto the memory instead of repeatedly
2971 allocating and freeing it. */
2972 static struct sortstrlist overlay_heads, overlay_tails;
2973 static unsigned char *overlay_str_buf;
2975 /* Allocated length of overlay_str_buf. */
2976 static EMACS_INT overlay_str_len;
2978 /* A comparison function suitable for passing to qsort. */
2979 static int
2980 cmp_for_strings (const void *as1, const void *as2)
2982 struct sortstr *s1 = (struct sortstr *)as1;
2983 struct sortstr *s2 = (struct sortstr *)as2;
2984 if (s1->size != s2->size)
2985 return s2->size - s1->size;
2986 if (s1->priority != s2->priority)
2987 return s1->priority - s2->priority;
2988 return 0;
2991 static void
2992 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
2994 EMACS_INT nbytes;
2996 if (ssl->used == ssl->size)
2998 if (ssl->buf)
2999 ssl->size *= 2;
3000 else
3001 ssl->size = 5;
3002 ssl->buf = ((struct sortstr *)
3003 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3005 ssl->buf[ssl->used].string = str;
3006 ssl->buf[ssl->used].string2 = str2;
3007 ssl->buf[ssl->used].size = size;
3008 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3009 ssl->used++;
3011 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
3012 nbytes = SCHARS (str);
3013 else if (! STRING_MULTIBYTE (str))
3014 nbytes = count_size_as_multibyte (SDATA (str),
3015 SBYTES (str));
3016 else
3017 nbytes = SBYTES (str);
3019 ssl->bytes += nbytes;
3021 if (STRINGP (str2))
3023 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
3024 nbytes = SCHARS (str2);
3025 else if (! STRING_MULTIBYTE (str2))
3026 nbytes = count_size_as_multibyte (SDATA (str2),
3027 SBYTES (str2));
3028 else
3029 nbytes = SBYTES (str2);
3031 ssl->bytes += nbytes;
3035 /* Return the concatenation of the strings associated with overlays that
3036 begin or end at POS, ignoring overlays that are specific to a window
3037 other than W. The strings are concatenated in the appropriate order:
3038 shorter overlays nest inside longer ones, and higher priority inside
3039 lower. Normally all of the after-strings come first, but zero-sized
3040 overlays have their after-strings ride along with the before-strings
3041 because it would look strange to print them inside-out.
3043 Returns the string length, and stores the contents indirectly through
3044 PSTR, if that variable is non-null. The string may be overwritten by
3045 subsequent calls. */
3047 EMACS_INT
3048 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3050 Lisp_Object overlay, window, clone_number, str;
3051 struct Lisp_Overlay *ov;
3052 EMACS_INT startpos, endpos;
3053 int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
3055 overlay_heads.used = overlay_heads.bytes = 0;
3056 overlay_tails.used = overlay_tails.bytes = 0;
3057 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3059 XSETMISC (overlay, ov);
3060 eassert (OVERLAYP (overlay));
3062 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3063 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3064 if (endpos < pos)
3065 break;
3066 if (endpos != pos && startpos != pos)
3067 continue;
3068 window = Foverlay_get (overlay, Qwindow);
3069 clone_number = Foverlay_get (overlay, Qclone_number);
3070 if (WINDOWP (window) && XWINDOW (window) != w
3071 && (! NUMBERP (clone_number)
3072 || XFASTINT (clone_number) != XFASTINT (w->clone_number)))
3073 continue;
3075 if (startpos == pos
3076 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3077 record_overlay_string (&overlay_heads, str,
3078 (startpos == endpos
3079 ? Foverlay_get (overlay, Qafter_string)
3080 : Qnil),
3081 Foverlay_get (overlay, Qpriority),
3082 endpos - startpos);
3083 else if (endpos == pos
3084 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3085 record_overlay_string (&overlay_tails, str, Qnil,
3086 Foverlay_get (overlay, Qpriority),
3087 endpos - startpos);
3089 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3091 XSETMISC (overlay, ov);
3092 eassert (OVERLAYP (overlay));
3094 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3095 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3096 if (startpos > pos)
3097 break;
3098 if (endpos != pos && startpos != pos)
3099 continue;
3100 window = Foverlay_get (overlay, Qwindow);
3101 clone_number = Foverlay_get (overlay, Qclone_number);
3102 if (WINDOWP (window) && XWINDOW (window) != w
3103 && (! NUMBERP (clone_number)
3104 || XFASTINT (clone_number) != XFASTINT (w->clone_number)))
3105 continue;
3107 if (startpos == pos
3108 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3109 record_overlay_string (&overlay_heads, str,
3110 (startpos == endpos
3111 ? Foverlay_get (overlay, Qafter_string)
3112 : Qnil),
3113 Foverlay_get (overlay, Qpriority),
3114 endpos - startpos);
3115 else if (endpos == pos
3116 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3117 record_overlay_string (&overlay_tails, str, Qnil,
3118 Foverlay_get (overlay, Qpriority),
3119 endpos - startpos);
3121 if (overlay_tails.used > 1)
3122 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3123 cmp_for_strings);
3124 if (overlay_heads.used > 1)
3125 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3126 cmp_for_strings);
3127 if (overlay_heads.bytes || overlay_tails.bytes)
3129 Lisp_Object tem;
3130 EMACS_INT i;
3131 unsigned char *p;
3132 EMACS_INT total = overlay_heads.bytes + overlay_tails.bytes;
3134 if (total > overlay_str_len)
3136 overlay_str_len = total;
3137 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3138 total);
3140 p = overlay_str_buf;
3141 for (i = overlay_tails.used; --i >= 0;)
3143 EMACS_INT nbytes;
3144 tem = overlay_tails.buf[i].string;
3145 nbytes = copy_text (SDATA (tem), p,
3146 SBYTES (tem),
3147 STRING_MULTIBYTE (tem), multibyte);
3148 p += nbytes;
3150 for (i = 0; i < overlay_heads.used; ++i)
3152 EMACS_INT nbytes;
3153 tem = overlay_heads.buf[i].string;
3154 nbytes = copy_text (SDATA (tem), p,
3155 SBYTES (tem),
3156 STRING_MULTIBYTE (tem), multibyte);
3157 p += nbytes;
3158 tem = overlay_heads.buf[i].string2;
3159 if (STRINGP (tem))
3161 nbytes = copy_text (SDATA (tem), p,
3162 SBYTES (tem),
3163 STRING_MULTIBYTE (tem), multibyte);
3164 p += nbytes;
3167 if (p != overlay_str_buf + total)
3168 abort ();
3169 if (pstr)
3170 *pstr = overlay_str_buf;
3171 return total;
3173 return 0;
3176 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3178 void
3179 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3181 Lisp_Object overlay, beg, end;
3182 struct Lisp_Overlay *prev, *tail, *next;
3184 /* See if anything in overlays_before should move to overlays_after. */
3186 /* We don't strictly need prev in this loop; it should always be nil.
3187 But we use it for symmetry and in case that should cease to be true
3188 with some future change. */
3189 prev = NULL;
3190 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3192 next = tail->next;
3193 XSETMISC (overlay, tail);
3195 /* If the overlay is not valid, get rid of it. */
3196 if (!OVERLAY_VALID (overlay))
3197 #if 1
3198 abort ();
3199 #else
3201 /* Splice the cons cell TAIL out of overlays_before. */
3202 if (!NILP (prev))
3203 XCDR (prev) = next;
3204 else
3205 buf->overlays_before = next;
3206 tail = prev;
3207 continue;
3209 #endif
3211 beg = OVERLAY_START (overlay);
3212 end = OVERLAY_END (overlay);
3214 if (OVERLAY_POSITION (end) > pos)
3216 /* OVERLAY needs to be moved. */
3217 EMACS_INT where = OVERLAY_POSITION (beg);
3218 struct Lisp_Overlay *other, *other_prev;
3220 /* Splice the cons cell TAIL out of overlays_before. */
3221 if (prev)
3222 prev->next = next;
3223 else
3224 buf->overlays_before = next;
3226 /* Search thru overlays_after for where to put it. */
3227 other_prev = NULL;
3228 for (other = buf->overlays_after; other;
3229 other_prev = other, other = other->next)
3231 Lisp_Object otherbeg, otheroverlay;
3233 XSETMISC (otheroverlay, other);
3234 eassert (OVERLAY_VALID (otheroverlay));
3236 otherbeg = OVERLAY_START (otheroverlay);
3237 if (OVERLAY_POSITION (otherbeg) >= where)
3238 break;
3241 /* Add TAIL to overlays_after before OTHER. */
3242 tail->next = other;
3243 if (other_prev)
3244 other_prev->next = tail;
3245 else
3246 buf->overlays_after = tail;
3247 tail = prev;
3249 else
3250 /* We've reached the things that should stay in overlays_before.
3251 All the rest of overlays_before must end even earlier,
3252 so stop now. */
3253 break;
3256 /* See if anything in overlays_after should be in overlays_before. */
3257 prev = NULL;
3258 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3260 next = tail->next;
3261 XSETMISC (overlay, tail);
3263 /* If the overlay is not valid, get rid of it. */
3264 if (!OVERLAY_VALID (overlay))
3265 #if 1
3266 abort ();
3267 #else
3269 /* Splice the cons cell TAIL out of overlays_after. */
3270 if (!NILP (prev))
3271 XCDR (prev) = next;
3272 else
3273 buf->overlays_after = next;
3274 tail = prev;
3275 continue;
3277 #endif
3279 beg = OVERLAY_START (overlay);
3280 end = OVERLAY_END (overlay);
3282 /* Stop looking, when we know that nothing further
3283 can possibly end before POS. */
3284 if (OVERLAY_POSITION (beg) > pos)
3285 break;
3287 if (OVERLAY_POSITION (end) <= pos)
3289 /* OVERLAY needs to be moved. */
3290 EMACS_INT where = OVERLAY_POSITION (end);
3291 struct Lisp_Overlay *other, *other_prev;
3293 /* Splice the cons cell TAIL out of overlays_after. */
3294 if (prev)
3295 prev->next = next;
3296 else
3297 buf->overlays_after = next;
3299 /* Search thru overlays_before for where to put it. */
3300 other_prev = NULL;
3301 for (other = buf->overlays_before; other;
3302 other_prev = other, other = other->next)
3304 Lisp_Object otherend, otheroverlay;
3306 XSETMISC (otheroverlay, other);
3307 eassert (OVERLAY_VALID (otheroverlay));
3309 otherend = OVERLAY_END (otheroverlay);
3310 if (OVERLAY_POSITION (otherend) <= where)
3311 break;
3314 /* Add TAIL to overlays_before before OTHER. */
3315 tail->next = other;
3316 if (other_prev)
3317 other_prev->next = tail;
3318 else
3319 buf->overlays_before = tail;
3320 tail = prev;
3324 buf->overlay_center = pos;
3327 void
3328 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3330 /* After an insertion, the lists are still sorted properly,
3331 but we may need to update the value of the overlay center. */
3332 if (current_buffer->overlay_center >= pos)
3333 current_buffer->overlay_center += length;
3336 void
3337 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3339 if (current_buffer->overlay_center < pos)
3340 /* The deletion was to our right. No change needed; the before- and
3341 after-lists are still consistent. */
3343 else if (current_buffer->overlay_center > pos + length)
3344 /* The deletion was to our left. We need to adjust the center value
3345 to account for the change in position, but the lists are consistent
3346 given the new value. */
3347 current_buffer->overlay_center -= length;
3348 else
3349 /* We're right in the middle. There might be things on the after-list
3350 that now belong on the before-list. Recentering will move them,
3351 and also update the center point. */
3352 recenter_overlay_lists (current_buffer, pos);
3355 /* Fix up overlays that were garbled as a result of permuting markers
3356 in the range START through END. Any overlay with at least one
3357 endpoint in this range will need to be unlinked from the overlay
3358 list and reinserted in its proper place.
3359 Such an overlay might even have negative size at this point.
3360 If so, we'll make the overlay empty. */
3361 void
3362 fix_start_end_in_overlays (register EMACS_INT start, register EMACS_INT end)
3364 Lisp_Object overlay;
3365 struct Lisp_Overlay *before_list, *after_list;
3366 /* These are either nil, indicating that before_list or after_list
3367 should be assigned, or the cons cell the cdr of which should be
3368 assigned. */
3369 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3370 /* 'Parent', likewise, indicates a cons cell or
3371 current_buffer->overlays_before or overlays_after, depending
3372 which loop we're in. */
3373 struct Lisp_Overlay *tail, *parent;
3374 EMACS_INT startpos, endpos;
3376 /* This algorithm shifts links around instead of consing and GCing.
3377 The loop invariant is that before_list (resp. after_list) is a
3378 well-formed list except that its last element, the CDR of beforep
3379 (resp. afterp) if beforep (afterp) isn't nil or before_list
3380 (after_list) if it is, is still uninitialized. So it's not a bug
3381 that before_list isn't initialized, although it may look
3382 strange. */
3383 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3385 XSETMISC (overlay, tail);
3387 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3388 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3390 /* If the overlay is backwards, make it empty. */
3391 if (endpos < startpos)
3393 startpos = endpos;
3394 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3395 Qnil);
3398 if (endpos < start)
3399 break;
3401 if (endpos < end
3402 || (startpos >= start && startpos < end))
3404 /* Add it to the end of the wrong list. Later on,
3405 recenter_overlay_lists will move it to the right place. */
3406 if (endpos < current_buffer->overlay_center)
3408 if (!afterp)
3409 after_list = tail;
3410 else
3411 afterp->next = tail;
3412 afterp = tail;
3414 else
3416 if (!beforep)
3417 before_list = tail;
3418 else
3419 beforep->next = tail;
3420 beforep = tail;
3422 if (!parent)
3423 current_buffer->overlays_before = tail->next;
3424 else
3425 parent->next = tail->next;
3426 tail = tail->next;
3428 else
3429 parent = tail, tail = parent->next;
3431 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3433 XSETMISC (overlay, tail);
3435 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3436 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3438 /* If the overlay is backwards, make it empty. */
3439 if (endpos < startpos)
3441 startpos = endpos;
3442 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3443 Qnil);
3446 if (startpos >= end)
3447 break;
3449 if (startpos >= start
3450 || (endpos >= start && endpos < end))
3452 if (endpos < current_buffer->overlay_center)
3454 if (!afterp)
3455 after_list = tail;
3456 else
3457 afterp->next = tail;
3458 afterp = tail;
3460 else
3462 if (!beforep)
3463 before_list = tail;
3464 else
3465 beforep->next = tail;
3466 beforep = tail;
3468 if (!parent)
3469 current_buffer->overlays_after = tail->next;
3470 else
3471 parent->next = tail->next;
3472 tail = tail->next;
3474 else
3475 parent = tail, tail = parent->next;
3478 /* Splice the constructed (wrong) lists into the buffer's lists,
3479 and let the recenter function make it sane again. */
3480 if (beforep)
3482 beforep->next = current_buffer->overlays_before;
3483 current_buffer->overlays_before = before_list;
3485 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3487 if (afterp)
3489 afterp->next = current_buffer->overlays_after;
3490 current_buffer->overlays_after = after_list;
3492 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3495 /* We have two types of overlay: the one whose ending marker is
3496 after-insertion-marker (this is the usual case) and the one whose
3497 ending marker is before-insertion-marker. When `overlays_before'
3498 contains overlays of the latter type and the former type in this
3499 order and both overlays end at inserting position, inserting a text
3500 increases only the ending marker of the latter type, which results
3501 in incorrect ordering of `overlays_before'.
3503 This function fixes ordering of overlays in the slot
3504 `overlays_before' of the buffer *BP. Before the insertion, `point'
3505 was at PREV, and now is at POS. */
3507 void
3508 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3510 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3511 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3512 Lisp_Object tem;
3513 EMACS_INT end;
3515 /* After the insertion, the several overlays may be in incorrect
3516 order. The possibility is that, in the list `overlays_before',
3517 an overlay which ends at POS appears after an overlay which ends
3518 at PREV. Since POS is greater than PREV, we must fix the
3519 ordering of these overlays, by moving overlays ends at POS before
3520 the overlays ends at PREV. */
3522 /* At first, find a place where disordered overlays should be linked
3523 in. It is where an overlay which end before POS exists. (i.e. an
3524 overlay whose ending marker is after-insertion-marker if disorder
3525 exists). */
3526 while (tail
3527 && (XSETMISC (tem, tail),
3528 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3530 parent = tail;
3531 tail = tail->next;
3534 /* If we don't find such an overlay,
3535 or the found one ends before PREV,
3536 or the found one is the last one in the list,
3537 we don't have to fix anything. */
3538 if (!tail || end < prev || !tail->next)
3539 return;
3541 right_pair = parent;
3542 parent = tail;
3543 tail = tail->next;
3545 /* Now, end position of overlays in the list TAIL should be before
3546 or equal to PREV. In the loop, an overlay which ends at POS is
3547 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3548 we found an overlay which ends before PREV, the remaining
3549 overlays are in correct order. */
3550 while (tail)
3552 XSETMISC (tem, tail);
3553 end = OVERLAY_POSITION (OVERLAY_END (tem));
3555 if (end == pos)
3556 { /* This overlay is disordered. */
3557 struct Lisp_Overlay *found = tail;
3559 /* Unlink the found overlay. */
3560 tail = found->next;
3561 parent->next = tail;
3562 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3563 and link it into the right place. */
3564 if (!right_pair)
3566 found->next = bp->overlays_before;
3567 bp->overlays_before = found;
3569 else
3571 found->next = right_pair->next;
3572 right_pair->next = found;
3575 else if (end == prev)
3577 parent = tail;
3578 tail = tail->next;
3580 else /* No more disordered overlay. */
3581 break;
3585 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3586 doc: /* Return t if OBJECT is an overlay. */)
3587 (Lisp_Object object)
3589 return (OVERLAYP (object) ? Qt : Qnil);
3592 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3593 doc: /* Create a new overlay with range BEG to END in BUFFER.
3594 If omitted, BUFFER defaults to the current buffer.
3595 BEG and END may be integers or markers.
3596 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3597 for the front of the overlay advance when text is inserted there
3598 \(which means the text *is not* included in the overlay).
3599 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3600 for the rear of the overlay advance when text is inserted there
3601 \(which means the text *is* included in the overlay). */)
3602 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3604 Lisp_Object overlay;
3605 struct buffer *b;
3607 if (NILP (buffer))
3608 XSETBUFFER (buffer, current_buffer);
3609 else
3610 CHECK_BUFFER (buffer);
3611 if (MARKERP (beg)
3612 && ! EQ (Fmarker_buffer (beg), buffer))
3613 error ("Marker points into wrong buffer");
3614 if (MARKERP (end)
3615 && ! EQ (Fmarker_buffer (end), buffer))
3616 error ("Marker points into wrong buffer");
3618 CHECK_NUMBER_COERCE_MARKER (beg);
3619 CHECK_NUMBER_COERCE_MARKER (end);
3621 if (XINT (beg) > XINT (end))
3623 Lisp_Object temp;
3624 temp = beg; beg = end; end = temp;
3627 b = XBUFFER (buffer);
3629 beg = Fset_marker (Fmake_marker (), beg, buffer);
3630 end = Fset_marker (Fmake_marker (), end, buffer);
3632 if (!NILP (front_advance))
3633 XMARKER (beg)->insertion_type = 1;
3634 if (!NILP (rear_advance))
3635 XMARKER (end)->insertion_type = 1;
3637 overlay = allocate_misc ();
3638 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3639 XOVERLAY (overlay)->start = beg;
3640 XOVERLAY (overlay)->end = end;
3641 XOVERLAY (overlay)->plist = Qnil;
3642 XOVERLAY (overlay)->next = NULL;
3644 /* Put the new overlay on the wrong list. */
3645 end = OVERLAY_END (overlay);
3646 if (OVERLAY_POSITION (end) < b->overlay_center)
3648 if (b->overlays_after)
3649 XOVERLAY (overlay)->next = b->overlays_after;
3650 b->overlays_after = XOVERLAY (overlay);
3652 else
3654 if (b->overlays_before)
3655 XOVERLAY (overlay)->next = b->overlays_before;
3656 b->overlays_before = XOVERLAY (overlay);
3659 /* This puts it in the right list, and in the right order. */
3660 recenter_overlay_lists (b, b->overlay_center);
3662 /* We don't need to redisplay the region covered by the overlay, because
3663 the overlay has no properties at the moment. */
3665 return overlay;
3668 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3670 static void
3671 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3673 if (start > end)
3675 EMACS_INT temp = start;
3676 start = end;
3677 end = temp;
3680 BUF_COMPUTE_UNCHANGED (buf, start, end);
3682 /* If this is a buffer not in the selected window,
3683 we must do other windows. */
3684 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3685 windows_or_buffers_changed = 1;
3686 /* If multiple windows show this buffer, we must do other windows. */
3687 else if (buffer_shared > 1)
3688 windows_or_buffers_changed = 1;
3689 /* If we modify an overlay at the end of the buffer, we cannot
3690 be sure that window end is still valid. */
3691 else if (end >= ZV && start <= ZV)
3692 windows_or_buffers_changed = 1;
3694 ++BUF_OVERLAY_MODIFF (buf);
3698 static struct Lisp_Overlay *
3699 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3701 struct Lisp_Overlay *tmp, *prev;
3702 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3703 if (tmp == overlay)
3705 if (prev)
3706 prev->next = tmp->next;
3707 else
3708 list = tmp->next;
3709 overlay->next = NULL;
3710 break;
3712 return list;
3715 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3716 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3717 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3718 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3719 buffer. */)
3720 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3722 struct buffer *b, *ob;
3723 Lisp_Object obuffer;
3724 int count = SPECPDL_INDEX ();
3726 CHECK_OVERLAY (overlay);
3727 if (NILP (buffer))
3728 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3729 if (NILP (buffer))
3730 XSETBUFFER (buffer, current_buffer);
3731 CHECK_BUFFER (buffer);
3733 if (MARKERP (beg)
3734 && ! EQ (Fmarker_buffer (beg), buffer))
3735 error ("Marker points into wrong buffer");
3736 if (MARKERP (end)
3737 && ! EQ (Fmarker_buffer (end), buffer))
3738 error ("Marker points into wrong buffer");
3740 CHECK_NUMBER_COERCE_MARKER (beg);
3741 CHECK_NUMBER_COERCE_MARKER (end);
3743 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3744 return Fdelete_overlay (overlay);
3746 if (XINT (beg) > XINT (end))
3748 Lisp_Object temp;
3749 temp = beg; beg = end; end = temp;
3752 specbind (Qinhibit_quit, Qt);
3754 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3755 b = XBUFFER (buffer);
3756 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3758 /* If the overlay has changed buffers, do a thorough redisplay. */
3759 if (!EQ (buffer, obuffer))
3761 /* Redisplay where the overlay was. */
3762 if (!NILP (obuffer))
3764 EMACS_INT o_beg;
3765 EMACS_INT o_end;
3767 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3768 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3770 modify_overlay (ob, o_beg, o_end);
3773 /* Redisplay where the overlay is going to be. */
3774 modify_overlay (b, XINT (beg), XINT (end));
3776 else
3777 /* Redisplay the area the overlay has just left, or just enclosed. */
3779 EMACS_INT o_beg, o_end;
3781 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3782 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3784 if (o_beg == XINT (beg))
3785 modify_overlay (b, o_end, XINT (end));
3786 else if (o_end == XINT (end))
3787 modify_overlay (b, o_beg, XINT (beg));
3788 else
3790 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3791 if (XINT (end) > o_end) o_end = XINT (end);
3792 modify_overlay (b, o_beg, o_end);
3796 if (!NILP (obuffer))
3798 ob->overlays_before
3799 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3800 ob->overlays_after
3801 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3802 eassert (XOVERLAY (overlay)->next == NULL);
3805 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3806 Fset_marker (OVERLAY_END (overlay), end, buffer);
3808 /* Put the overlay on the wrong list. */
3809 end = OVERLAY_END (overlay);
3810 if (OVERLAY_POSITION (end) < b->overlay_center)
3812 XOVERLAY (overlay)->next = b->overlays_after;
3813 b->overlays_after = XOVERLAY (overlay);
3815 else
3817 XOVERLAY (overlay)->next = b->overlays_before;
3818 b->overlays_before = XOVERLAY (overlay);
3821 /* This puts it in the right list, and in the right order. */
3822 recenter_overlay_lists (b, b->overlay_center);
3824 return unbind_to (count, overlay);
3827 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3828 doc: /* Delete the overlay OVERLAY from its buffer. */)
3829 (Lisp_Object overlay)
3831 Lisp_Object buffer;
3832 struct buffer *b;
3833 int count = SPECPDL_INDEX ();
3835 CHECK_OVERLAY (overlay);
3837 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3838 if (NILP (buffer))
3839 return Qnil;
3841 b = XBUFFER (buffer);
3842 specbind (Qinhibit_quit, Qt);
3844 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3845 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3846 eassert (XOVERLAY (overlay)->next == NULL);
3847 modify_overlay (b,
3848 marker_position (OVERLAY_START (overlay)),
3849 marker_position (OVERLAY_END (overlay)));
3850 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3851 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3853 /* When deleting an overlay with before or after strings, turn off
3854 display optimizations for the affected buffer, on the basis that
3855 these strings may contain newlines. This is easier to do than to
3856 check for that situation during redisplay. */
3857 if (!windows_or_buffers_changed
3858 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3859 || !NILP (Foverlay_get (overlay, Qafter_string))))
3860 b->prevent_redisplay_optimizations_p = 1;
3862 return unbind_to (count, Qnil);
3865 /* Overlay dissection functions. */
3867 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3868 doc: /* Return the position at which OVERLAY starts. */)
3869 (Lisp_Object overlay)
3871 CHECK_OVERLAY (overlay);
3873 return (Fmarker_position (OVERLAY_START (overlay)));
3876 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3877 doc: /* Return the position at which OVERLAY ends. */)
3878 (Lisp_Object overlay)
3880 CHECK_OVERLAY (overlay);
3882 return (Fmarker_position (OVERLAY_END (overlay)));
3885 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3886 doc: /* Return the buffer OVERLAY belongs to.
3887 Return nil if OVERLAY has been deleted. */)
3888 (Lisp_Object overlay)
3890 CHECK_OVERLAY (overlay);
3892 return Fmarker_buffer (OVERLAY_START (overlay));
3895 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3896 doc: /* Return a list of the properties on OVERLAY.
3897 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3898 OVERLAY. */)
3899 (Lisp_Object overlay)
3901 CHECK_OVERLAY (overlay);
3903 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3907 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3908 doc: /* Return a list of the overlays that contain the character at POS. */)
3909 (Lisp_Object pos)
3911 int noverlays;
3912 Lisp_Object *overlay_vec;
3913 int len;
3914 Lisp_Object result;
3916 CHECK_NUMBER_COERCE_MARKER (pos);
3918 len = 10;
3919 /* We can't use alloca here because overlays_at can call xrealloc. */
3920 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3922 /* Put all the overlays we want in a vector in overlay_vec.
3923 Store the length in len. */
3924 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3925 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
3927 /* Make a list of them all. */
3928 result = Flist (noverlays, overlay_vec);
3930 xfree (overlay_vec);
3931 return result;
3934 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3935 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3936 Overlap means that at least one character is contained within the overlay
3937 and also contained within the specified region.
3938 Empty overlays are included in the result if they are located at BEG,
3939 between BEG and END, or at END provided END denotes the position at the
3940 end of the buffer. */)
3941 (Lisp_Object beg, Lisp_Object end)
3943 int noverlays;
3944 Lisp_Object *overlay_vec;
3945 int len;
3946 Lisp_Object result;
3948 CHECK_NUMBER_COERCE_MARKER (beg);
3949 CHECK_NUMBER_COERCE_MARKER (end);
3951 len = 10;
3952 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3954 /* Put all the overlays we want in a vector in overlay_vec.
3955 Store the length in len. */
3956 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3957 NULL, NULL);
3959 /* Make a list of them all. */
3960 result = Flist (noverlays, overlay_vec);
3962 xfree (overlay_vec);
3963 return result;
3966 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3967 1, 1, 0,
3968 doc: /* Return the next position after POS where an overlay starts or ends.
3969 If there are no overlay boundaries from POS to (point-max),
3970 the value is (point-max). */)
3971 (Lisp_Object pos)
3973 int noverlays;
3974 EMACS_INT endpos;
3975 Lisp_Object *overlay_vec;
3976 int len;
3977 int i;
3979 CHECK_NUMBER_COERCE_MARKER (pos);
3981 len = 10;
3982 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3984 /* Put all the overlays we want in a vector in overlay_vec.
3985 Store the length in len.
3986 endpos gets the position where the next overlay starts. */
3987 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3988 &endpos, (EMACS_INT *) 0, 1);
3990 /* If any of these overlays ends before endpos,
3991 use its ending point instead. */
3992 for (i = 0; i < noverlays; i++)
3994 Lisp_Object oend;
3995 EMACS_INT oendpos;
3997 oend = OVERLAY_END (overlay_vec[i]);
3998 oendpos = OVERLAY_POSITION (oend);
3999 if (oendpos < endpos)
4000 endpos = oendpos;
4003 xfree (overlay_vec);
4004 return make_number (endpos);
4007 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4008 Sprevious_overlay_change, 1, 1, 0,
4009 doc: /* Return the previous position before POS where an overlay starts or ends.
4010 If there are no overlay boundaries from (point-min) to POS,
4011 the value is (point-min). */)
4012 (Lisp_Object pos)
4014 int noverlays;
4015 EMACS_INT prevpos;
4016 Lisp_Object *overlay_vec;
4017 int len;
4019 CHECK_NUMBER_COERCE_MARKER (pos);
4021 /* At beginning of buffer, we know the answer;
4022 avoid bug subtracting 1 below. */
4023 if (XINT (pos) == BEGV)
4024 return pos;
4026 len = 10;
4027 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4029 /* Put all the overlays we want in a vector in overlay_vec.
4030 Store the length in len.
4031 prevpos gets the position of the previous change. */
4032 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4033 (EMACS_INT *) 0, &prevpos, 1);
4035 xfree (overlay_vec);
4036 return make_number (prevpos);
4039 /* These functions are for debugging overlays. */
4041 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4042 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4043 The car has all the overlays before the overlay center;
4044 the cdr has all the overlays after the overlay center.
4045 Recentering overlays moves overlays between these lists.
4046 The lists you get are copies, so that changing them has no effect.
4047 However, the overlays you get are the real objects that the buffer uses. */)
4048 (void)
4050 struct Lisp_Overlay *ol;
4051 Lisp_Object before = Qnil, after = Qnil, tmp;
4052 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4054 XSETMISC (tmp, ol);
4055 before = Fcons (tmp, before);
4057 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4059 XSETMISC (tmp, ol);
4060 after = Fcons (tmp, after);
4062 return Fcons (Fnreverse (before), Fnreverse (after));
4065 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4066 doc: /* Recenter the overlays of the current buffer around position POS.
4067 That makes overlay lookup faster for positions near POS (but perhaps slower
4068 for positions far away from POS). */)
4069 (Lisp_Object pos)
4071 CHECK_NUMBER_COERCE_MARKER (pos);
4073 recenter_overlay_lists (current_buffer, XINT (pos));
4074 return Qnil;
4077 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4078 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4079 (Lisp_Object overlay, Lisp_Object prop)
4081 CHECK_OVERLAY (overlay);
4082 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4085 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4086 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4087 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4089 Lisp_Object tail, buffer;
4090 int changed;
4092 CHECK_OVERLAY (overlay);
4094 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4096 for (tail = XOVERLAY (overlay)->plist;
4097 CONSP (tail) && CONSP (XCDR (tail));
4098 tail = XCDR (XCDR (tail)))
4099 if (EQ (XCAR (tail), prop))
4101 changed = !EQ (XCAR (XCDR (tail)), value);
4102 XSETCAR (XCDR (tail), value);
4103 goto found;
4105 /* It wasn't in the list, so add it to the front. */
4106 changed = !NILP (value);
4107 XOVERLAY (overlay)->plist
4108 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4109 found:
4110 if (! NILP (buffer))
4112 if (changed)
4113 modify_overlay (XBUFFER (buffer),
4114 marker_position (OVERLAY_START (overlay)),
4115 marker_position (OVERLAY_END (overlay)));
4116 if (EQ (prop, Qevaporate) && ! NILP (value)
4117 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4118 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4119 Fdelete_overlay (overlay);
4122 return value;
4125 /* Subroutine of report_overlay_modification. */
4127 /* Lisp vector holding overlay hook functions to call.
4128 Vector elements come in pairs.
4129 Each even-index element is a list of hook functions.
4130 The following odd-index element is the overlay they came from.
4132 Before the buffer change, we fill in this vector
4133 as we call overlay hook functions.
4134 After the buffer change, we get the functions to call from this vector.
4135 This way we always call the same functions before and after the change. */
4136 static Lisp_Object last_overlay_modification_hooks;
4138 /* Number of elements actually used in last_overlay_modification_hooks. */
4139 static int last_overlay_modification_hooks_used;
4141 /* Add one functionlist/overlay pair
4142 to the end of last_overlay_modification_hooks. */
4144 static void
4145 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4147 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4149 if (last_overlay_modification_hooks_used == oldsize)
4150 last_overlay_modification_hooks = larger_vector
4151 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4152 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4153 functionlist); last_overlay_modification_hooks_used++;
4154 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4155 overlay); last_overlay_modification_hooks_used++;
4158 /* Run the modification-hooks of overlays that include
4159 any part of the text in START to END.
4160 If this change is an insertion, also
4161 run the insert-before-hooks of overlay starting at END,
4162 and the insert-after-hooks of overlay ending at START.
4164 This is called both before and after the modification.
4165 AFTER is nonzero when we call after the modification.
4167 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4168 When AFTER is nonzero, they are the start position,
4169 the position after the inserted new text,
4170 and the length of deleted or replaced old text. */
4172 void
4173 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4174 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4176 Lisp_Object prop, overlay;
4177 struct Lisp_Overlay *tail;
4178 /* 1 if this change is an insertion. */
4179 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4180 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4182 overlay = Qnil;
4183 tail = NULL;
4185 /* We used to run the functions as soon as we found them and only register
4186 them in last_overlay_modification_hooks for the purpose of the `after'
4187 case. But running elisp code as we traverse the list of overlays is
4188 painful because the list can be modified by the elisp code so we had to
4189 copy at several places. We now simply do a read-only traversal that
4190 only collects the functions to run and we run them afterwards. It's
4191 simpler, especially since all the code was already there. -stef */
4193 if (!after)
4195 /* We are being called before a change.
4196 Scan the overlays to find the functions to call. */
4197 last_overlay_modification_hooks_used = 0;
4198 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4200 EMACS_INT startpos, endpos;
4201 Lisp_Object ostart, oend;
4203 XSETMISC (overlay, tail);
4205 ostart = OVERLAY_START (overlay);
4206 oend = OVERLAY_END (overlay);
4207 endpos = OVERLAY_POSITION (oend);
4208 if (XFASTINT (start) > endpos)
4209 break;
4210 startpos = OVERLAY_POSITION (ostart);
4211 if (insertion && (XFASTINT (start) == startpos
4212 || XFASTINT (end) == startpos))
4214 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4215 if (!NILP (prop))
4216 add_overlay_mod_hooklist (prop, overlay);
4218 if (insertion && (XFASTINT (start) == endpos
4219 || XFASTINT (end) == endpos))
4221 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4222 if (!NILP (prop))
4223 add_overlay_mod_hooklist (prop, overlay);
4225 /* Test for intersecting intervals. This does the right thing
4226 for both insertion and deletion. */
4227 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4229 prop = Foverlay_get (overlay, Qmodification_hooks);
4230 if (!NILP (prop))
4231 add_overlay_mod_hooklist (prop, overlay);
4235 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4237 EMACS_INT startpos, endpos;
4238 Lisp_Object ostart, oend;
4240 XSETMISC (overlay, tail);
4242 ostart = OVERLAY_START (overlay);
4243 oend = OVERLAY_END (overlay);
4244 startpos = OVERLAY_POSITION (ostart);
4245 endpos = OVERLAY_POSITION (oend);
4246 if (XFASTINT (end) < startpos)
4247 break;
4248 if (insertion && (XFASTINT (start) == startpos
4249 || XFASTINT (end) == startpos))
4251 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4252 if (!NILP (prop))
4253 add_overlay_mod_hooklist (prop, overlay);
4255 if (insertion && (XFASTINT (start) == endpos
4256 || XFASTINT (end) == endpos))
4258 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4259 if (!NILP (prop))
4260 add_overlay_mod_hooklist (prop, overlay);
4262 /* Test for intersecting intervals. This does the right thing
4263 for both insertion and deletion. */
4264 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4266 prop = Foverlay_get (overlay, Qmodification_hooks);
4267 if (!NILP (prop))
4268 add_overlay_mod_hooklist (prop, overlay);
4273 GCPRO4 (overlay, arg1, arg2, arg3);
4275 /* Call the functions recorded in last_overlay_modification_hooks.
4276 First copy the vector contents, in case some of these hooks
4277 do subsequent modification of the buffer. */
4278 int size = last_overlay_modification_hooks_used;
4279 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4280 int i;
4282 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4283 size * sizeof (Lisp_Object));
4284 gcpro1.var = copy;
4285 gcpro1.nvars = size;
4287 for (i = 0; i < size;)
4289 Lisp_Object prop, overlay;
4290 prop = copy[i++];
4291 overlay = copy[i++];
4292 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4295 UNGCPRO;
4298 static void
4299 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4300 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4302 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4304 GCPRO4 (list, arg1, arg2, arg3);
4306 while (CONSP (list))
4308 if (NILP (arg3))
4309 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4310 else
4311 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4312 list = XCDR (list);
4314 UNGCPRO;
4317 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4318 property is set. */
4319 void
4320 evaporate_overlays (EMACS_INT pos)
4322 Lisp_Object overlay, hit_list;
4323 struct Lisp_Overlay *tail;
4325 hit_list = Qnil;
4326 if (pos <= current_buffer->overlay_center)
4327 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4329 EMACS_INT endpos;
4330 XSETMISC (overlay, tail);
4331 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4332 if (endpos < pos)
4333 break;
4334 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4335 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4336 hit_list = Fcons (overlay, hit_list);
4338 else
4339 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4341 EMACS_INT startpos;
4342 XSETMISC (overlay, tail);
4343 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4344 if (startpos > pos)
4345 break;
4346 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4347 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4348 hit_list = Fcons (overlay, hit_list);
4350 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4351 Fdelete_overlay (XCAR (hit_list));
4354 /* Somebody has tried to store a value with an unacceptable type
4355 in the slot with offset OFFSET. */
4357 void
4358 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4360 Lisp_Object predicate;
4362 switch (type)
4364 case_Lisp_Int: predicate = Qintegerp; break;
4365 case Lisp_String: predicate = Qstringp; break;
4366 case Lisp_Symbol: predicate = Qsymbolp; break;
4367 default: abort ();
4370 wrong_type_argument (predicate, newval);
4374 /***********************************************************************
4375 Allocation with mmap
4376 ***********************************************************************/
4378 #ifdef USE_MMAP_FOR_BUFFERS
4380 #include <sys/types.h>
4381 #include <sys/mman.h>
4383 #ifndef MAP_ANON
4384 #ifdef MAP_ANONYMOUS
4385 #define MAP_ANON MAP_ANONYMOUS
4386 #else
4387 #define MAP_ANON 0
4388 #endif
4389 #endif
4391 #ifndef MAP_FAILED
4392 #define MAP_FAILED ((void *) -1)
4393 #endif
4395 #include <stdio.h>
4397 #if MAP_ANON == 0
4398 #include <fcntl.h>
4399 #endif
4401 #include "coding.h"
4404 /* Memory is allocated in regions which are mapped using mmap(2).
4405 The current implementation lets the system select mapped
4406 addresses; we're not using MAP_FIXED in general, except when
4407 trying to enlarge regions.
4409 Each mapped region starts with a mmap_region structure, the user
4410 area starts after that structure, aligned to MEM_ALIGN.
4412 +-----------------------+
4413 | struct mmap_info + |
4414 | padding |
4415 +-----------------------+
4416 | user data |
4419 +-----------------------+ */
4421 struct mmap_region
4423 /* User-specified size. */
4424 size_t nbytes_specified;
4426 /* Number of bytes mapped */
4427 size_t nbytes_mapped;
4429 /* Pointer to the location holding the address of the memory
4430 allocated with the mmap'd block. The variable actually points
4431 after this structure. */
4432 POINTER_TYPE **var;
4434 /* Next and previous in list of all mmap'd regions. */
4435 struct mmap_region *next, *prev;
4438 /* Doubly-linked list of mmap'd regions. */
4440 static struct mmap_region *mmap_regions;
4442 /* File descriptor for mmap. If we don't have anonymous mapping,
4443 /dev/zero will be opened on it. */
4445 static int mmap_fd;
4447 /* Temporary storage for mmap_set_vars, see there. */
4449 static struct mmap_region *mmap_regions_1;
4450 static int mmap_fd_1;
4452 /* Page size on this system. */
4454 static int mmap_page_size;
4456 /* 1 means mmap has been intialized. */
4458 static int mmap_initialized_p;
4460 /* Value is X rounded up to the next multiple of N. */
4462 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4464 /* Size of mmap_region structure plus padding. */
4466 #define MMAP_REGION_STRUCT_SIZE \
4467 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4469 /* Given a pointer P to the start of the user-visible part of a mapped
4470 region, return a pointer to the start of the region. */
4472 #define MMAP_REGION(P) \
4473 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4475 /* Given a pointer P to the start of a mapped region, return a pointer
4476 to the start of the user-visible part of the region. */
4478 #define MMAP_USER_AREA(P) \
4479 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4481 #define MEM_ALIGN sizeof (double)
4483 /* Predicate returning true if part of the address range [START .. END]
4484 is currently mapped. Used to prevent overwriting an existing
4485 memory mapping.
4487 Default is to conservativly assume the address range is occupied by
4488 something else. This can be overridden by system configuration
4489 files if system-specific means to determine this exists. */
4491 #ifndef MMAP_ALLOCATED_P
4492 #define MMAP_ALLOCATED_P(start, end) 1
4493 #endif
4495 /* Function prototypes. */
4497 static int mmap_free_1 (struct mmap_region *);
4498 static int mmap_enlarge (struct mmap_region *, int);
4499 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4500 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4501 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4502 static void mmap_free (POINTER_TYPE **ptr);
4503 static void mmap_init (void);
4506 /* Return a region overlapping address range START...END, or null if
4507 none. END is not including, i.e. the last byte in the range
4508 is at END - 1. */
4510 static struct mmap_region *
4511 mmap_find (start, end)
4512 POINTER_TYPE *start, *end;
4514 struct mmap_region *r;
4515 char *s = (char *) start, *e = (char *) end;
4517 for (r = mmap_regions; r; r = r->next)
4519 char *rstart = (char *) r;
4520 char *rend = rstart + r->nbytes_mapped;
4522 if (/* First byte of range, i.e. START, in this region? */
4523 (s >= rstart && s < rend)
4524 /* Last byte of range, i.e. END - 1, in this region? */
4525 || (e > rstart && e <= rend)
4526 /* First byte of this region in the range? */
4527 || (rstart >= s && rstart < e)
4528 /* Last byte of this region in the range? */
4529 || (rend > s && rend <= e))
4530 break;
4533 return r;
4537 /* Unmap a region. P is a pointer to the start of the user-araa of
4538 the region. Value is non-zero if successful. */
4540 static int
4541 mmap_free_1 (r)
4542 struct mmap_region *r;
4544 if (r->next)
4545 r->next->prev = r->prev;
4546 if (r->prev)
4547 r->prev->next = r->next;
4548 else
4549 mmap_regions = r->next;
4551 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4553 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4554 return 0;
4557 return 1;
4561 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4562 Value is non-zero if successful. */
4564 static int
4565 mmap_enlarge (r, npages)
4566 struct mmap_region *r;
4567 int npages;
4569 char *region_end = (char *) r + r->nbytes_mapped;
4570 size_t nbytes;
4571 int success = 0;
4573 if (npages < 0)
4575 /* Unmap pages at the end of the region. */
4576 nbytes = - npages * mmap_page_size;
4577 if (munmap (region_end - nbytes, nbytes) == -1)
4578 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4579 else
4581 r->nbytes_mapped -= nbytes;
4582 success = 1;
4585 else if (npages > 0)
4587 nbytes = npages * mmap_page_size;
4589 /* Try to map additional pages at the end of the region. We
4590 cannot do this if the address range is already occupied by
4591 something else because mmap deletes any previous mapping.
4592 I'm not sure this is worth doing, let's see. */
4593 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4595 POINTER_TYPE *p;
4597 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4598 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4599 if (p == MAP_FAILED)
4600 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4601 else if (p != (POINTER_TYPE *) region_end)
4603 /* Kernels are free to choose a different address. In
4604 that case, unmap what we've mapped above; we have
4605 no use for it. */
4606 if (munmap (p, nbytes) == -1)
4607 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4609 else
4611 r->nbytes_mapped += nbytes;
4612 success = 1;
4617 return success;
4621 /* Set or reset variables holding references to mapped regions. If
4622 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4623 non-zero, set all variables to the start of the user-areas
4624 of mapped regions.
4626 This function is called from Fdump_emacs to ensure that the dumped
4627 Emacs doesn't contain references to memory that won't be mapped
4628 when Emacs starts. */
4630 void
4631 mmap_set_vars (restore_p)
4632 int restore_p;
4634 struct mmap_region *r;
4636 if (restore_p)
4638 mmap_regions = mmap_regions_1;
4639 mmap_fd = mmap_fd_1;
4640 for (r = mmap_regions; r; r = r->next)
4641 *r->var = MMAP_USER_AREA (r);
4643 else
4645 for (r = mmap_regions; r; r = r->next)
4646 *r->var = NULL;
4647 mmap_regions_1 = mmap_regions;
4648 mmap_regions = NULL;
4649 mmap_fd_1 = mmap_fd;
4650 mmap_fd = -1;
4655 /* Allocate a block of storage large enough to hold NBYTES bytes of
4656 data. A pointer to the data is returned in *VAR. VAR is thus the
4657 address of some variable which will use the data area.
4659 The allocation of 0 bytes is valid.
4661 If we can't allocate the necessary memory, set *VAR to null, and
4662 return null. */
4664 static POINTER_TYPE *
4665 mmap_alloc (var, nbytes)
4666 POINTER_TYPE **var;
4667 size_t nbytes;
4669 void *p;
4670 size_t map;
4672 mmap_init ();
4674 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4675 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4676 mmap_fd, 0);
4678 if (p == MAP_FAILED)
4680 if (errno != ENOMEM)
4681 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4682 p = NULL;
4684 else
4686 struct mmap_region *r = (struct mmap_region *) p;
4688 r->nbytes_specified = nbytes;
4689 r->nbytes_mapped = map;
4690 r->var = var;
4691 r->prev = NULL;
4692 r->next = mmap_regions;
4693 if (r->next)
4694 r->next->prev = r;
4695 mmap_regions = r;
4697 p = MMAP_USER_AREA (p);
4700 return *var = p;
4704 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4705 resize it to size NBYTES. Change *VAR to reflect the new block,
4706 and return this value. If more memory cannot be allocated, then
4707 leave *VAR unchanged, and return null. */
4709 static POINTER_TYPE *
4710 mmap_realloc (var, nbytes)
4711 POINTER_TYPE **var;
4712 size_t nbytes;
4714 POINTER_TYPE *result;
4716 mmap_init ();
4718 if (*var == NULL)
4719 result = mmap_alloc (var, nbytes);
4720 else if (nbytes == 0)
4722 mmap_free (var);
4723 result = mmap_alloc (var, nbytes);
4725 else
4727 struct mmap_region *r = MMAP_REGION (*var);
4728 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4730 if (room < nbytes)
4732 /* Must enlarge. */
4733 POINTER_TYPE *old_ptr = *var;
4735 /* Try to map additional pages at the end of the region.
4736 If that fails, allocate a new region, copy data
4737 from the old region, then free it. */
4738 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4739 / mmap_page_size)))
4741 r->nbytes_specified = nbytes;
4742 *var = result = old_ptr;
4744 else if (mmap_alloc (var, nbytes))
4746 memcpy (*var, old_ptr, r->nbytes_specified);
4747 mmap_free_1 (MMAP_REGION (old_ptr));
4748 result = *var;
4749 r = MMAP_REGION (result);
4750 r->nbytes_specified = nbytes;
4752 else
4754 *var = old_ptr;
4755 result = NULL;
4758 else if (room - nbytes >= mmap_page_size)
4760 /* Shrinking by at least a page. Let's give some
4761 memory back to the system.
4763 The extra parens are to make the division happens first,
4764 on positive values, so we know it will round towards
4765 zero. */
4766 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4767 result = *var;
4768 r->nbytes_specified = nbytes;
4770 else
4772 /* Leave it alone. */
4773 result = *var;
4774 r->nbytes_specified = nbytes;
4778 return result;
4782 /* Free a block of relocatable storage whose data is pointed to by
4783 PTR. Store 0 in *PTR to show there's no block allocated. */
4785 static void
4786 mmap_free (var)
4787 POINTER_TYPE **var;
4789 mmap_init ();
4791 if (*var)
4793 mmap_free_1 (MMAP_REGION (*var));
4794 *var = NULL;
4799 /* Perform necessary intializations for the use of mmap. */
4801 static void
4802 mmap_init ()
4804 #if MAP_ANON == 0
4805 /* The value of mmap_fd is initially 0 in temacs, and -1
4806 in a dumped Emacs. */
4807 if (mmap_fd <= 0)
4809 /* No anonymous mmap -- we need the file descriptor. */
4810 mmap_fd = open ("/dev/zero", O_RDONLY);
4811 if (mmap_fd == -1)
4812 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4814 #endif /* MAP_ANON == 0 */
4816 if (mmap_initialized_p)
4817 return;
4818 mmap_initialized_p = 1;
4820 #if MAP_ANON != 0
4821 mmap_fd = -1;
4822 #endif
4824 mmap_page_size = getpagesize ();
4827 #endif /* USE_MMAP_FOR_BUFFERS */
4831 /***********************************************************************
4832 Buffer-text Allocation
4833 ***********************************************************************/
4835 #ifdef REL_ALLOC
4836 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4837 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4838 extern void r_alloc_free (POINTER_TYPE **ptr);
4839 #endif /* REL_ALLOC */
4842 /* Allocate NBYTES bytes for buffer B's text buffer. */
4844 static void
4845 alloc_buffer_text (struct buffer *b, size_t nbytes)
4847 POINTER_TYPE *p;
4849 BLOCK_INPUT;
4850 #if defined USE_MMAP_FOR_BUFFERS
4851 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4852 #elif defined REL_ALLOC
4853 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4854 #else
4855 p = xmalloc (nbytes);
4856 #endif
4858 if (p == NULL)
4860 UNBLOCK_INPUT;
4861 memory_full ();
4864 b->text->beg = (unsigned char *) p;
4865 UNBLOCK_INPUT;
4868 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4869 shrink it. */
4871 void
4872 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4874 POINTER_TYPE *p;
4875 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4876 + delta);
4877 BLOCK_INPUT;
4878 #if defined USE_MMAP_FOR_BUFFERS
4879 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4880 #elif defined REL_ALLOC
4881 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4882 #else
4883 p = xrealloc (b->text->beg, nbytes);
4884 #endif
4886 if (p == NULL)
4888 UNBLOCK_INPUT;
4889 memory_full ();
4892 BUF_BEG_ADDR (b) = (unsigned char *) p;
4893 UNBLOCK_INPUT;
4897 /* Free buffer B's text buffer. */
4899 static void
4900 free_buffer_text (struct buffer *b)
4902 BLOCK_INPUT;
4904 #if defined USE_MMAP_FOR_BUFFERS
4905 mmap_free ((POINTER_TYPE **) &b->text->beg);
4906 #elif defined REL_ALLOC
4907 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4908 #else
4909 xfree (b->text->beg);
4910 #endif
4912 BUF_BEG_ADDR (b) = NULL;
4913 UNBLOCK_INPUT;
4918 /***********************************************************************
4919 Initialization
4920 ***********************************************************************/
4922 void
4923 init_buffer_once (void)
4925 int idx;
4927 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
4929 /* Make sure all markable slots in buffer_defaults
4930 are initialized reasonably, so mark_buffer won't choke. */
4931 reset_buffer (&buffer_defaults);
4932 eassert (EQ (BVAR (&buffer_defaults, name), make_number (0)));
4933 reset_buffer_local_variables (&buffer_defaults, 1);
4934 eassert (EQ (BVAR (&buffer_local_symbols, name), make_number (0)));
4935 reset_buffer (&buffer_local_symbols);
4936 reset_buffer_local_variables (&buffer_local_symbols, 1);
4937 /* Prevent GC from getting confused. */
4938 buffer_defaults.text = &buffer_defaults.own_text;
4939 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4940 BUF_INTERVALS (&buffer_defaults) = 0;
4941 BUF_INTERVALS (&buffer_local_symbols) = 0;
4942 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
4943 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4944 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
4945 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4947 /* Set up the default values of various buffer slots. */
4948 /* Must do these before making the first buffer! */
4950 /* real setup is done in bindings.el */
4951 BVAR (&buffer_defaults, mode_line_format) = make_pure_c_string ("%-");
4952 BVAR (&buffer_defaults, header_line_format) = Qnil;
4953 BVAR (&buffer_defaults, abbrev_mode) = Qnil;
4954 BVAR (&buffer_defaults, overwrite_mode) = Qnil;
4955 BVAR (&buffer_defaults, case_fold_search) = Qt;
4956 BVAR (&buffer_defaults, auto_fill_function) = Qnil;
4957 BVAR (&buffer_defaults, selective_display) = Qnil;
4958 #ifndef old
4959 BVAR (&buffer_defaults, selective_display_ellipses) = Qt;
4960 #endif
4961 BVAR (&buffer_defaults, abbrev_table) = Qnil;
4962 BVAR (&buffer_defaults, display_table) = Qnil;
4963 BVAR (&buffer_defaults, undo_list) = Qnil;
4964 BVAR (&buffer_defaults, mark_active) = Qnil;
4965 BVAR (&buffer_defaults, file_format) = Qnil;
4966 BVAR (&buffer_defaults, auto_save_file_format) = Qt;
4967 buffer_defaults.overlays_before = NULL;
4968 buffer_defaults.overlays_after = NULL;
4969 buffer_defaults.overlay_center = BEG;
4971 XSETFASTINT (BVAR (&buffer_defaults, tab_width), 8);
4972 BVAR (&buffer_defaults, truncate_lines) = Qnil;
4973 BVAR (&buffer_defaults, word_wrap) = Qnil;
4974 BVAR (&buffer_defaults, ctl_arrow) = Qt;
4975 BVAR (&buffer_defaults, bidi_display_reordering) = Qnil;
4976 BVAR (&buffer_defaults, bidi_paragraph_direction) = Qnil;
4977 BVAR (&buffer_defaults, cursor_type) = Qt;
4978 BVAR (&buffer_defaults, extra_line_spacing) = Qnil;
4979 BVAR (&buffer_defaults, cursor_in_non_selected_windows) = Qt;
4981 BVAR (&buffer_defaults, enable_multibyte_characters) = Qt;
4982 BVAR (&buffer_defaults, buffer_file_coding_system) = Qnil;
4983 XSETFASTINT (BVAR (&buffer_defaults, fill_column), 70);
4984 XSETFASTINT (BVAR (&buffer_defaults, left_margin), 0);
4985 BVAR (&buffer_defaults, cache_long_line_scans) = Qnil;
4986 BVAR (&buffer_defaults, file_truename) = Qnil;
4987 XSETFASTINT (BVAR (&buffer_defaults, display_count), 0);
4988 XSETFASTINT (BVAR (&buffer_defaults, left_margin_cols), 0);
4989 XSETFASTINT (BVAR (&buffer_defaults, right_margin_cols), 0);
4990 BVAR (&buffer_defaults, left_fringe_width) = Qnil;
4991 BVAR (&buffer_defaults, right_fringe_width) = Qnil;
4992 BVAR (&buffer_defaults, fringes_outside_margins) = Qnil;
4993 BVAR (&buffer_defaults, scroll_bar_width) = Qnil;
4994 BVAR (&buffer_defaults, vertical_scroll_bar_type) = Qt;
4995 BVAR (&buffer_defaults, indicate_empty_lines) = Qnil;
4996 BVAR (&buffer_defaults, indicate_buffer_boundaries) = Qnil;
4997 BVAR (&buffer_defaults, fringe_indicator_alist) = Qnil;
4998 BVAR (&buffer_defaults, fringe_cursor_alist) = Qnil;
4999 BVAR (&buffer_defaults, scroll_up_aggressively) = Qnil;
5000 BVAR (&buffer_defaults, scroll_down_aggressively) = Qnil;
5001 BVAR (&buffer_defaults, display_time) = Qnil;
5003 /* Assign the local-flags to the slots that have default values.
5004 The local flag is a bit that is used in the buffer
5005 to say that it has its own local value for the slot.
5006 The local flag bits are in the local_var_flags slot of the buffer. */
5008 /* Nothing can work if this isn't true */
5009 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5011 /* 0 means not a lisp var, -1 means always local, else mask */
5012 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
5013 XSETINT (BVAR (&buffer_local_flags, filename), -1);
5014 XSETINT (BVAR (&buffer_local_flags, directory), -1);
5015 XSETINT (BVAR (&buffer_local_flags, backed_up), -1);
5016 XSETINT (BVAR (&buffer_local_flags, save_length), -1);
5017 XSETINT (BVAR (&buffer_local_flags, auto_save_file_name), -1);
5018 XSETINT (BVAR (&buffer_local_flags, read_only), -1);
5019 XSETINT (BVAR (&buffer_local_flags, major_mode), -1);
5020 XSETINT (BVAR (&buffer_local_flags, mode_name), -1);
5021 XSETINT (BVAR (&buffer_local_flags, undo_list), -1);
5022 XSETINT (BVAR (&buffer_local_flags, mark_active), -1);
5023 XSETINT (BVAR (&buffer_local_flags, point_before_scroll), -1);
5024 XSETINT (BVAR (&buffer_local_flags, file_truename), -1);
5025 XSETINT (BVAR (&buffer_local_flags, invisibility_spec), -1);
5026 XSETINT (BVAR (&buffer_local_flags, file_format), -1);
5027 XSETINT (BVAR (&buffer_local_flags, auto_save_file_format), -1);
5028 XSETINT (BVAR (&buffer_local_flags, display_count), -1);
5029 XSETINT (BVAR (&buffer_local_flags, display_time), -1);
5030 XSETINT (BVAR (&buffer_local_flags, enable_multibyte_characters), -1);
5032 idx = 1;
5033 XSETFASTINT (BVAR (&buffer_local_flags, mode_line_format), idx); ++idx;
5034 XSETFASTINT (BVAR (&buffer_local_flags, abbrev_mode), idx); ++idx;
5035 XSETFASTINT (BVAR (&buffer_local_flags, overwrite_mode), idx); ++idx;
5036 XSETFASTINT (BVAR (&buffer_local_flags, case_fold_search), idx); ++idx;
5037 XSETFASTINT (BVAR (&buffer_local_flags, auto_fill_function), idx); ++idx;
5038 XSETFASTINT (BVAR (&buffer_local_flags, selective_display), idx); ++idx;
5039 #ifndef old
5040 XSETFASTINT (BVAR (&buffer_local_flags, selective_display_ellipses), idx); ++idx;
5041 #endif
5042 XSETFASTINT (BVAR (&buffer_local_flags, tab_width), idx); ++idx;
5043 XSETFASTINT (BVAR (&buffer_local_flags, truncate_lines), idx); ++idx;
5044 XSETFASTINT (BVAR (&buffer_local_flags, word_wrap), idx); ++idx;
5045 XSETFASTINT (BVAR (&buffer_local_flags, ctl_arrow), idx); ++idx;
5046 XSETFASTINT (BVAR (&buffer_local_flags, fill_column), idx); ++idx;
5047 XSETFASTINT (BVAR (&buffer_local_flags, left_margin), idx); ++idx;
5048 XSETFASTINT (BVAR (&buffer_local_flags, abbrev_table), idx); ++idx;
5049 XSETFASTINT (BVAR (&buffer_local_flags, display_table), idx); ++idx;
5050 XSETFASTINT (BVAR (&buffer_local_flags, syntax_table), idx); ++idx;
5051 XSETFASTINT (BVAR (&buffer_local_flags, cache_long_line_scans), idx); ++idx;
5052 XSETFASTINT (BVAR (&buffer_local_flags, category_table), idx); ++idx;
5053 XSETFASTINT (BVAR (&buffer_local_flags, bidi_display_reordering), idx); ++idx;
5054 XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_direction), idx); ++idx;
5055 XSETFASTINT (BVAR (&buffer_local_flags, buffer_file_coding_system), idx);
5056 /* Make this one a permanent local. */
5057 buffer_permanent_local_flags[idx++] = 1;
5058 XSETFASTINT (BVAR (&buffer_local_flags, left_margin_cols), idx); ++idx;
5059 XSETFASTINT (BVAR (&buffer_local_flags, right_margin_cols), idx); ++idx;
5060 XSETFASTINT (BVAR (&buffer_local_flags, left_fringe_width), idx); ++idx;
5061 XSETFASTINT (BVAR (&buffer_local_flags, right_fringe_width), idx); ++idx;
5062 XSETFASTINT (BVAR (&buffer_local_flags, fringes_outside_margins), idx); ++idx;
5063 XSETFASTINT (BVAR (&buffer_local_flags, scroll_bar_width), idx); ++idx;
5064 XSETFASTINT (BVAR (&buffer_local_flags, vertical_scroll_bar_type), idx); ++idx;
5065 XSETFASTINT (BVAR (&buffer_local_flags, indicate_empty_lines), idx); ++idx;
5066 XSETFASTINT (BVAR (&buffer_local_flags, indicate_buffer_boundaries), idx); ++idx;
5067 XSETFASTINT (BVAR (&buffer_local_flags, fringe_indicator_alist), idx); ++idx;
5068 XSETFASTINT (BVAR (&buffer_local_flags, fringe_cursor_alist), idx); ++idx;
5069 XSETFASTINT (BVAR (&buffer_local_flags, scroll_up_aggressively), idx); ++idx;
5070 XSETFASTINT (BVAR (&buffer_local_flags, scroll_down_aggressively), idx); ++idx;
5071 XSETFASTINT (BVAR (&buffer_local_flags, header_line_format), idx); ++idx;
5072 XSETFASTINT (BVAR (&buffer_local_flags, cursor_type), idx); ++idx;
5073 XSETFASTINT (BVAR (&buffer_local_flags, extra_line_spacing), idx); ++idx;
5074 XSETFASTINT (BVAR (&buffer_local_flags, cursor_in_non_selected_windows), idx); ++idx;
5076 /* Need more room? */
5077 if (idx >= MAX_PER_BUFFER_VARS)
5078 abort ();
5079 last_per_buffer_idx = idx;
5081 Vbuffer_alist = Qnil;
5082 current_buffer = 0;
5083 all_buffers = 0;
5085 QSFundamental = make_pure_c_string ("Fundamental");
5087 Qfundamental_mode = intern_c_string ("fundamental-mode");
5088 BVAR (&buffer_defaults, major_mode) = Qfundamental_mode;
5090 Qmode_class = intern_c_string ("mode-class");
5092 Qprotected_field = intern_c_string ("protected-field");
5094 Qpermanent_local = intern_c_string ("permanent-local");
5096 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5097 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5099 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5101 /* super-magic invisible buffer */
5102 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5103 Vbuffer_alist = Qnil;
5105 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5107 inhibit_modification_hooks = 0;
5110 void
5111 init_buffer (void)
5113 char *pwd;
5114 Lisp_Object temp;
5115 int len;
5117 #ifdef USE_MMAP_FOR_BUFFERS
5119 /* When using the ralloc implementation based on mmap(2), buffer
5120 text pointers will have been set to null in the dumped Emacs.
5121 Map new memory. */
5122 struct buffer *b;
5124 for (b = all_buffers; b; b = b->next)
5125 if (b->text->beg == NULL)
5126 enlarge_buffer_text (b, 0);
5128 #endif /* USE_MMAP_FOR_BUFFERS */
5130 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5131 if (NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
5132 Fset_buffer_multibyte (Qnil);
5134 pwd = get_current_dir_name ();
5136 if (!pwd)
5137 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5139 /* Maybe this should really use some standard subroutine
5140 whose definition is filename syntax dependent. */
5141 len = strlen (pwd);
5142 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5144 /* Grow buffer to add directory separator and '\0'. */
5145 pwd = (char *) realloc (pwd, len + 2);
5146 if (!pwd)
5147 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5148 pwd[len] = DIRECTORY_SEP;
5149 pwd[len + 1] = '\0';
5152 BVAR (current_buffer, directory) = make_unibyte_string (pwd, strlen (pwd));
5153 if (! NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
5154 /* At this moment, we still don't know how to decode the
5155 directory name. So, we keep the bytes in multibyte form so
5156 that ENCODE_FILE correctly gets the original bytes. */
5157 BVAR (current_buffer, directory)
5158 = string_to_multibyte (BVAR (current_buffer, directory));
5160 /* Add /: to the front of the name
5161 if it would otherwise be treated as magic. */
5162 temp = Ffind_file_name_handler (BVAR (current_buffer, directory), Qt);
5163 if (! NILP (temp)
5164 /* If the default dir is just /, TEMP is non-nil
5165 because of the ange-ftp completion handler.
5166 However, it is not necessary to turn / into /:/.
5167 So avoid doing that. */
5168 && strcmp ("/", SSDATA (BVAR (current_buffer, directory))))
5169 BVAR (current_buffer, directory)
5170 = concat2 (build_string ("/:"), BVAR (current_buffer, directory));
5172 temp = get_minibuffer (0);
5173 BVAR (XBUFFER (temp), directory) = BVAR (current_buffer, directory);
5175 free (pwd);
5178 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5179 Object stored in the current buffer. address is the address of the slot
5180 in the buffer that is current now. */
5182 /* TYPE is nil for a general Lisp variable.
5183 An integer specifies a type; then only Lisp values
5184 with that type code are allowed (except that nil is allowed too).
5185 LNAME is the Lisp-level variable name.
5186 VNAME is the name of the buffer slot.
5187 DOC is a dummy where you write the doc string as a comment. */
5188 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5189 do { \
5190 static struct Lisp_Buffer_Objfwd bo_fwd; \
5191 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5192 } while (0)
5194 static void
5195 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
5196 Lisp_Object *address, Lisp_Object type, char *doc)
5198 struct Lisp_Symbol *sym;
5199 int offset;
5201 sym = XSYMBOL (intern (namestring));
5202 offset = (char *)address - (char *)current_buffer;
5204 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5205 bo_fwd->offset = offset;
5206 bo_fwd->slottype = type;
5207 sym->redirect = SYMBOL_FORWARDED;
5209 /* I tried to do the job without a cast, but it seems impossible.
5210 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5211 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5213 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5215 if (PER_BUFFER_IDX (offset) == 0)
5216 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5217 slot of buffer_local_flags */
5218 abort ();
5222 /* initialize the buffer routines */
5223 void
5224 syms_of_buffer (void)
5226 staticpro (&last_overlay_modification_hooks);
5227 last_overlay_modification_hooks
5228 = Fmake_vector (make_number (10), Qnil);
5230 staticpro (&Vbuffer_defaults);
5231 staticpro (&Vbuffer_local_symbols);
5232 staticpro (&Qfundamental_mode);
5233 staticpro (&Qmode_class);
5234 staticpro (&QSFundamental);
5235 staticpro (&Vbuffer_alist);
5236 staticpro (&Qprotected_field);
5237 staticpro (&Qpermanent_local);
5238 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5239 staticpro (&Qpermanent_local_hook);
5240 staticpro (&Qkill_buffer_hook);
5241 Qoverlayp = intern_c_string ("overlayp");
5242 staticpro (&Qoverlayp);
5243 Qevaporate = intern_c_string ("evaporate");
5244 staticpro (&Qevaporate);
5245 Qmodification_hooks = intern_c_string ("modification-hooks");
5246 staticpro (&Qmodification_hooks);
5247 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5248 staticpro (&Qinsert_in_front_hooks);
5249 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5250 staticpro (&Qinsert_behind_hooks);
5251 Qget_file_buffer = intern_c_string ("get-file-buffer");
5252 staticpro (&Qget_file_buffer);
5253 Qpriority = intern_c_string ("priority");
5254 staticpro (&Qpriority);
5255 Qclone_number = intern_c_string ("clone-number");
5256 staticpro (&Qclone_number);
5257 Qbefore_string = intern_c_string ("before-string");
5258 staticpro (&Qbefore_string);
5259 Qafter_string = intern_c_string ("after-string");
5260 staticpro (&Qafter_string);
5261 Qfirst_change_hook = intern_c_string ("first-change-hook");
5262 staticpro (&Qfirst_change_hook);
5263 Qbefore_change_functions = intern_c_string ("before-change-functions");
5264 staticpro (&Qbefore_change_functions);
5265 Qafter_change_functions = intern_c_string ("after-change-functions");
5266 staticpro (&Qafter_change_functions);
5267 /* The next one is initialized in init_buffer_once. */
5268 staticpro (&Qucs_set_table_for_input);
5270 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5271 staticpro (&Qkill_buffer_query_functions);
5273 Fput (Qprotected_field, Qerror_conditions,
5274 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5275 Fput (Qprotected_field, Qerror_message,
5276 make_pure_c_string ("Attempt to modify a protected field"));
5278 /* All these use DEFVAR_LISP_NOPRO because the slots in
5279 buffer_defaults will all be marked via Vbuffer_defaults. */
5281 DEFVAR_BUFFER_DEFAULTS ("default-mode-line-format",
5282 mode_line_format,
5283 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5284 This is the same as (default-value 'mode-line-format). */);
5286 DEFVAR_BUFFER_DEFAULTS ("default-header-line-format",
5287 header_line_format,
5288 doc: /* Default value of `header-line-format' for buffers that don't override it.
5289 This is the same as (default-value 'header-line-format). */);
5291 DEFVAR_BUFFER_DEFAULTS ("default-cursor-type", cursor_type,
5292 doc: /* Default value of `cursor-type' for buffers that don't override it.
5293 This is the same as (default-value 'cursor-type). */);
5295 DEFVAR_BUFFER_DEFAULTS ("default-line-spacing",
5296 extra_line_spacing,
5297 doc: /* Default value of `line-spacing' for buffers that don't override it.
5298 This is the same as (default-value 'line-spacing). */);
5300 DEFVAR_BUFFER_DEFAULTS ("default-cursor-in-non-selected-windows",
5301 cursor_in_non_selected_windows,
5302 doc: /* Default value of `cursor-in-non-selected-windows'.
5303 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5305 DEFVAR_BUFFER_DEFAULTS ("default-abbrev-mode",
5306 abbrev_mode,
5307 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5308 This is the same as (default-value 'abbrev-mode). */);
5310 DEFVAR_BUFFER_DEFAULTS ("default-ctl-arrow",
5311 ctl_arrow,
5312 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5313 This is the same as (default-value 'ctl-arrow). */);
5315 DEFVAR_BUFFER_DEFAULTS ("default-enable-multibyte-characters",
5316 enable_multibyte_characters,
5317 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5318 This is the same as (default-value 'enable-multibyte-characters). */);
5320 DEFVAR_BUFFER_DEFAULTS ("default-buffer-file-coding-system",
5321 buffer_file_coding_system,
5322 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5323 This is the same as (default-value 'buffer-file-coding-system). */);
5325 DEFVAR_BUFFER_DEFAULTS ("default-truncate-lines",
5326 truncate_lines,
5327 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5328 This is the same as (default-value 'truncate-lines). */);
5330 DEFVAR_BUFFER_DEFAULTS ("default-fill-column",
5331 fill_column,
5332 doc: /* Default value of `fill-column' for buffers that do not override it.
5333 This is the same as (default-value 'fill-column). */);
5335 DEFVAR_BUFFER_DEFAULTS ("default-left-margin",
5336 left_margin,
5337 doc: /* Default value of `left-margin' for buffers that do not override it.
5338 This is the same as (default-value 'left-margin). */);
5340 DEFVAR_BUFFER_DEFAULTS ("default-tab-width",
5341 tab_width,
5342 doc: /* Default value of `tab-width' for buffers that do not override it.
5343 This is the same as (default-value 'tab-width). */);
5345 DEFVAR_BUFFER_DEFAULTS ("default-case-fold-search",
5346 case_fold_search,
5347 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5348 This is the same as (default-value 'case-fold-search). */);
5350 DEFVAR_BUFFER_DEFAULTS ("default-left-margin-width",
5351 left_margin_cols,
5352 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5353 This is the same as (default-value 'left-margin-width). */);
5355 DEFVAR_BUFFER_DEFAULTS ("default-right-margin-width",
5356 right_margin_cols,
5357 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5358 This is the same as (default-value 'right-margin-width). */);
5360 DEFVAR_BUFFER_DEFAULTS ("default-left-fringe-width",
5361 left_fringe_width,
5362 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5363 This is the same as (default-value 'left-fringe-width). */);
5365 DEFVAR_BUFFER_DEFAULTS ("default-right-fringe-width",
5366 right_fringe_width,
5367 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5368 This is the same as (default-value 'right-fringe-width). */);
5370 DEFVAR_BUFFER_DEFAULTS ("default-fringes-outside-margins",
5371 fringes_outside_margins,
5372 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5373 This is the same as (default-value 'fringes-outside-margins). */);
5375 DEFVAR_BUFFER_DEFAULTS ("default-scroll-bar-width",
5376 scroll_bar_width,
5377 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5378 This is the same as (default-value 'scroll-bar-width). */);
5380 DEFVAR_BUFFER_DEFAULTS ("default-vertical-scroll-bar",
5381 vertical_scroll_bar_type,
5382 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5383 This is the same as (default-value 'vertical-scroll-bar). */);
5385 DEFVAR_BUFFER_DEFAULTS ("default-indicate-empty-lines",
5386 indicate_empty_lines,
5387 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5388 This is the same as (default-value 'indicate-empty-lines). */);
5390 DEFVAR_BUFFER_DEFAULTS ("default-indicate-buffer-boundaries",
5391 indicate_buffer_boundaries,
5392 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5393 This is the same as (default-value 'indicate-buffer-boundaries). */);
5395 DEFVAR_BUFFER_DEFAULTS ("default-fringe-indicator-alist",
5396 fringe_indicator_alist,
5397 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5398 This is the same as (default-value 'fringe-indicator-alist'). */);
5400 DEFVAR_BUFFER_DEFAULTS ("default-fringe-cursor-alist",
5401 fringe_cursor_alist,
5402 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5403 This is the same as (default-value 'fringe-cursor-alist'). */);
5405 DEFVAR_BUFFER_DEFAULTS ("default-scroll-up-aggressively",
5406 scroll_up_aggressively,
5407 doc: /* Default value of `scroll-up-aggressively'.
5408 This value applies in buffers that don't have their own local values.
5409 This is the same as (default-value 'scroll-up-aggressively). */);
5411 DEFVAR_BUFFER_DEFAULTS ("default-scroll-down-aggressively",
5412 scroll_down_aggressively,
5413 doc: /* Default value of `scroll-down-aggressively'.
5414 This value applies in buffers that don't have their own local values.
5415 This is the same as (default-value 'scroll-down-aggressively). */);
5417 DEFVAR_PER_BUFFER ("header-line-format",
5418 &BVAR (current_buffer, header_line_format),
5419 Qnil,
5420 doc: /* Analogous to `mode-line-format', but controls the header line.
5421 The header line appears, optionally, at the top of a window;
5422 the mode line appears at the bottom. */);
5424 DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format),
5425 Qnil,
5426 doc: /* Template for displaying mode line for current buffer.
5427 Each buffer has its own value of this variable.
5428 Value may be nil, a string, a symbol or a list or cons cell.
5429 A value of nil means don't display a mode line.
5430 For a symbol, its value is used (but it is ignored if t or nil).
5431 A string appearing directly as the value of a symbol is processed verbatim
5432 in that the %-constructs below are not recognized.
5433 Note that unless the symbol is marked as a `risky-local-variable', all
5434 properties in any strings, as well as all :eval and :propertize forms
5435 in the value of that symbol will be ignored.
5436 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5437 is used as a mode line element. Be careful--FORM should not load any files,
5438 because that can cause an infinite recursion.
5439 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5440 with the specified properties PROPS applied.
5441 For a list whose car is a symbol, the symbol's value is taken,
5442 and if that is non-nil, the cadr of the list is processed recursively.
5443 Otherwise, the caddr of the list (if there is one) is processed.
5444 For a list whose car is a string or list, each element is processed
5445 recursively and the results are effectively concatenated.
5446 For a list whose car is an integer, the cdr of the list is processed
5447 and padded (if the number is positive) or truncated (if negative)
5448 to the width specified by that number.
5449 A string is printed verbatim in the mode line except for %-constructs:
5450 (%-constructs are allowed when the string is the entire mode-line-format
5451 or when it is found in a cons-cell or a list)
5452 %b -- print buffer name. %f -- print visited file name.
5453 %F -- print frame name.
5454 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5455 %& is like %*, but ignore read-only-ness.
5456 % means buffer is read-only and * means it is modified.
5457 For a modified read-only buffer, %* gives % and %+ gives *.
5458 %s -- print process status. %l -- print the current line number.
5459 %c -- print the current column number (this makes editing slower).
5460 To make the column number update correctly in all cases,
5461 `column-number-mode' must be non-nil.
5462 %i -- print the size of the buffer.
5463 %I -- like %i, but use k, M, G, etc., to abbreviate.
5464 %p -- print percent of buffer above top of window, or Top, Bot or All.
5465 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5466 or print Bottom or All.
5467 %n -- print Narrow if appropriate.
5468 %t -- visited file is text or binary (if OS supports this distinction).
5469 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5470 %Z -- like %z, but including the end-of-line format.
5471 %e -- print error message about full memory.
5472 %@ -- print @ or hyphen. @ means that default-directory is on a
5473 remote machine.
5474 %[ -- print one [ for each recursive editing level. %] similar.
5475 %% -- print %. %- -- print infinitely many dashes.
5476 Decimal digits after the % specify field width to which to pad. */);
5478 DEFVAR_BUFFER_DEFAULTS ("default-major-mode", major_mode,
5479 doc: /* *Value of `major-mode' for new buffers. */);
5481 DEFVAR_PER_BUFFER ("major-mode", &BVAR (current_buffer, major_mode),
5482 make_number (Lisp_Symbol),
5483 doc: /* Symbol for current buffer's major mode.
5484 The default value (normally `fundamental-mode') affects new buffers.
5485 A value of nil means to use the current buffer's major mode, provided
5486 it is not marked as "special".
5488 When a mode is used by default, `find-file' switches to it before it
5489 reads the contents into the buffer and before it finishes setting up
5490 the buffer. Thus, the mode and its hooks should not expect certain
5491 variables such as `buffer-read-only' and `buffer-file-coding-system'
5492 to be set up. */);
5494 DEFVAR_PER_BUFFER ("mode-name", &BVAR (current_buffer, mode_name),
5495 Qnil,
5496 doc: /* Pretty name of current buffer's major mode.
5497 Usually a string, but can use any of the constructs for `mode-line-format',
5498 which see.
5499 Format with `format-mode-line' to produce a string value. */);
5501 DEFVAR_PER_BUFFER ("local-abbrev-table", &BVAR (current_buffer, abbrev_table), Qnil,
5502 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5504 DEFVAR_PER_BUFFER ("abbrev-mode", &BVAR (current_buffer, abbrev_mode), Qnil,
5505 doc: /* Non-nil if Abbrev mode is enabled.
5506 Use the command `abbrev-mode' to change this variable. */);
5508 DEFVAR_PER_BUFFER ("case-fold-search", &BVAR (current_buffer, case_fold_search),
5509 Qnil,
5510 doc: /* *Non-nil if searches and matches should ignore case. */);
5512 DEFVAR_PER_BUFFER ("fill-column", &BVAR (current_buffer, fill_column),
5513 make_number (LISP_INT_TAG),
5514 doc: /* *Column beyond which automatic line-wrapping should happen.
5515 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5517 DEFVAR_PER_BUFFER ("left-margin", &BVAR (current_buffer, left_margin),
5518 make_number (LISP_INT_TAG),
5519 doc: /* *Column for the default `indent-line-function' to indent to.
5520 Linefeed indents to this column in Fundamental mode. */);
5522 DEFVAR_PER_BUFFER ("tab-width", &BVAR (current_buffer, tab_width),
5523 make_number (LISP_INT_TAG),
5524 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5526 DEFVAR_PER_BUFFER ("ctl-arrow", &BVAR (current_buffer, ctl_arrow), Qnil,
5527 doc: /* *Non-nil means display control chars with uparrow.
5528 A value of nil means use backslash and octal digits.
5529 This variable does not apply to characters whose display is specified
5530 in the current display table (if there is one). */);
5532 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5533 &BVAR (current_buffer, enable_multibyte_characters),
5534 Qnil,
5535 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5536 Otherwise they are regarded as unibyte. This affects the display,
5537 file I/O and the behavior of various editing commands.
5539 This variable is buffer-local but you cannot set it directly;
5540 use the function `set-buffer-multibyte' to change a buffer's representation.
5541 Changing its default value with `setq-default' is supported.
5542 See also variable `default-enable-multibyte-characters' and Info node
5543 `(elisp)Text Representations'. */);
5544 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5546 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5547 &BVAR (current_buffer, buffer_file_coding_system), Qnil,
5548 doc: /* Coding system to be used for encoding the buffer contents on saving.
5549 This variable applies to saving the buffer, and also to `write-region'
5550 and other functions that use `write-region'.
5551 It does not apply to sending output to subprocesses, however.
5553 If this is nil, the buffer is saved without any code conversion
5554 unless some coding system is specified in `file-coding-system-alist'
5555 for the buffer file.
5557 If the text to be saved cannot be encoded as specified by this variable,
5558 an alternative encoding is selected by `select-safe-coding-system', which see.
5560 The variable `coding-system-for-write', if non-nil, overrides this variable.
5562 This variable is never applied to a way of decoding a file while reading it. */);
5564 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5565 &BVAR (current_buffer, bidi_display_reordering), Qnil,
5566 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5568 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5569 &BVAR (current_buffer, bidi_paragraph_direction), Qnil,
5570 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5572 If this is nil (the default), the direction of each paragraph is
5573 determined by the first strong directional character of its text.
5574 The values of `right-to-left' and `left-to-right' override that.
5575 Any other value is treated as nil.
5577 This variable has no effect unless the buffer's value of
5578 \`bidi-display-reordering' is non-nil. */);
5580 DEFVAR_PER_BUFFER ("truncate-lines", &BVAR (current_buffer, truncate_lines), Qnil,
5581 doc: /* *Non-nil means do not display continuation lines.
5582 Instead, give each line of text just one screen line.
5584 Note that this is overridden by the variable
5585 `truncate-partial-width-windows' if that variable is non-nil
5586 and this buffer is not full-frame width. */);
5588 DEFVAR_PER_BUFFER ("word-wrap", &BVAR (current_buffer, word_wrap), Qnil,
5589 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5590 When word-wrapping is on, continuation lines are wrapped at the space
5591 or tab character nearest to the right window edge.
5592 If nil, continuation lines are wrapped at the right screen edge.
5594 This variable has no effect if long lines are truncated (see
5595 `truncate-lines' and `truncate-partial-width-windows'). If you use
5596 word-wrapping, you might want to reduce the value of
5597 `truncate-partial-width-windows', since wrapping can make text readable
5598 in narrower windows. */);
5600 DEFVAR_PER_BUFFER ("default-directory", &BVAR (current_buffer, directory),
5601 make_number (Lisp_String),
5602 doc: /* Name of default directory of current buffer. Should end with slash.
5603 To interactively change the default directory, use command `cd'. */);
5605 DEFVAR_PER_BUFFER ("auto-fill-function", &BVAR (current_buffer, auto_fill_function),
5606 Qnil,
5607 doc: /* Function called (if non-nil) to perform auto-fill.
5608 It is called after self-inserting any character specified in
5609 the `auto-fill-chars' table.
5610 NOTE: This variable is not a hook;
5611 its value may not be a list of functions. */);
5613 DEFVAR_PER_BUFFER ("buffer-file-name", &BVAR (current_buffer, filename),
5614 make_number (Lisp_String),
5615 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5617 DEFVAR_PER_BUFFER ("buffer-file-truename", &BVAR (current_buffer, file_truename),
5618 make_number (Lisp_String),
5619 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5620 The truename of a file is calculated by `file-truename'
5621 and then abbreviated with `abbreviate-file-name'. */);
5623 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5624 &BVAR (current_buffer, auto_save_file_name),
5625 make_number (Lisp_String),
5626 doc: /* Name of file for auto-saving current buffer.
5627 If it is nil, that means don't auto-save this buffer. */);
5629 DEFVAR_PER_BUFFER ("buffer-read-only", &BVAR (current_buffer, read_only), Qnil,
5630 doc: /* Non-nil if this buffer is read-only. */);
5632 DEFVAR_PER_BUFFER ("buffer-backed-up", &BVAR (current_buffer, backed_up), Qnil,
5633 doc: /* Non-nil if this buffer's file has been backed up.
5634 Backing up is done before the first time the file is saved. */);
5636 DEFVAR_PER_BUFFER ("buffer-saved-size", &BVAR (current_buffer, save_length),
5637 make_number (LISP_INT_TAG),
5638 doc: /* Length of current buffer when last read in, saved or auto-saved.
5639 0 initially.
5640 -1 means auto-saving turned off until next real save.
5642 If you set this to -2, that means don't turn off auto-saving in this buffer
5643 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5644 you probably should set this to -2 in that buffer. */);
5646 DEFVAR_PER_BUFFER ("selective-display", &BVAR (current_buffer, selective_display),
5647 Qnil,
5648 doc: /* Non-nil enables selective display.
5649 An integer N as value means display only lines
5650 that start with less than N columns of space.
5651 A value of t means that the character ^M makes itself and
5652 all the rest of the line invisible; also, when saving the buffer
5653 in a file, save the ^M as a newline. */);
5655 #ifndef old
5656 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5657 &BVAR (current_buffer, selective_display_ellipses),
5658 Qnil,
5659 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5660 #endif
5662 DEFVAR_PER_BUFFER ("overwrite-mode", &BVAR (current_buffer, overwrite_mode), Qnil,
5663 doc: /* Non-nil if self-insertion should replace existing text.
5664 The value should be one of `overwrite-mode-textual',
5665 `overwrite-mode-binary', or nil.
5666 If it is `overwrite-mode-textual', self-insertion still
5667 inserts at the end of a line, and inserts when point is before a tab,
5668 until the tab is filled in.
5669 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5671 DEFVAR_PER_BUFFER ("buffer-display-table", &BVAR (current_buffer, display_table),
5672 Qnil,
5673 doc: /* Display table that controls display of the contents of current buffer.
5675 If this variable is nil, the value of `standard-display-table' is used.
5676 Each window can have its own, overriding display table, see
5677 `set-window-display-table' and `window-display-table'.
5679 The display table is a char-table created with `make-display-table'.
5680 A char-table is an array indexed by character codes. Normal array
5681 primitives `aref' and `aset' can be used to access elements of a char-table.
5683 Each of the char-table elements control how to display the corresponding
5684 text character: the element at index C in the table says how to display
5685 the character whose code is C. Each element should be a vector of
5686 characters or nil. The value nil means display the character in the
5687 default fashion; otherwise, the characters from the vector are delivered
5688 to the screen instead of the original character.
5690 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5691 to display a capital Y instead of each X character.
5693 In addition, a char-table has six extra slots to control the display of:
5695 the end of a truncated screen line (extra-slot 0, a single character);
5696 the end of a continued line (extra-slot 1, a single character);
5697 the escape character used to display character codes in octal
5698 (extra-slot 2, a single character);
5699 the character used as an arrow for control characters (extra-slot 3,
5700 a single character);
5701 the decoration indicating the presence of invisible lines (extra-slot 4,
5702 a vector of characters);
5703 the character used to draw the border between side-by-side windows
5704 (extra-slot 5, a single character).
5706 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5708 DEFVAR_PER_BUFFER ("left-margin-width", &BVAR (current_buffer, left_margin_cols),
5709 Qnil,
5710 doc: /* *Width of left marginal area for display of a buffer.
5711 A value of nil means no marginal area. */);
5713 DEFVAR_PER_BUFFER ("right-margin-width", &BVAR (current_buffer, right_margin_cols),
5714 Qnil,
5715 doc: /* *Width of right marginal area for display of a buffer.
5716 A value of nil means no marginal area. */);
5718 DEFVAR_PER_BUFFER ("left-fringe-width", &BVAR (current_buffer, left_fringe_width),
5719 Qnil,
5720 doc: /* *Width of this buffer's left fringe (in pixels).
5721 A value of 0 means no left fringe is shown in this buffer's window.
5722 A value of nil means to use the left fringe width from the window's frame. */);
5724 DEFVAR_PER_BUFFER ("right-fringe-width", &BVAR (current_buffer, right_fringe_width),
5725 Qnil,
5726 doc: /* *Width of this buffer's right fringe (in pixels).
5727 A value of 0 means no right fringe is shown in this buffer's window.
5728 A value of nil means to use the right fringe width from the window's frame. */);
5730 DEFVAR_PER_BUFFER ("fringes-outside-margins", &BVAR (current_buffer, fringes_outside_margins),
5731 Qnil,
5732 doc: /* *Non-nil means to display fringes outside display margins.
5733 A value of nil means to display fringes between margins and buffer text. */);
5735 DEFVAR_PER_BUFFER ("scroll-bar-width", &BVAR (current_buffer, scroll_bar_width),
5736 Qnil,
5737 doc: /* *Width of this buffer's scroll bars in pixels.
5738 A value of nil means to use the scroll bar width from the window's frame. */);
5740 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &BVAR (current_buffer, vertical_scroll_bar_type),
5741 Qnil,
5742 doc: /* *Position of this buffer's vertical scroll bar.
5743 The value takes effect whenever you tell a window to display this buffer;
5744 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5746 A value of `left' or `right' means put the vertical scroll bar at that side
5747 of the window; a value of nil means don't show any vertical scroll bars.
5748 A value of t (the default) means do whatever the window's frame specifies. */);
5750 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5751 &BVAR (current_buffer, indicate_empty_lines), Qnil,
5752 doc: /* *Visually indicate empty lines after the buffer end.
5753 If non-nil, a bitmap is displayed in the left fringe of a window on
5754 window-systems. */);
5756 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5757 &BVAR (current_buffer, indicate_buffer_boundaries), Qnil,
5758 doc: /* *Visually indicate buffer boundaries and scrolling.
5759 If non-nil, the first and last line of the buffer are marked in the fringe
5760 of a window on window-systems with angle bitmaps, or if the window can be
5761 scrolled, the top and bottom line of the window are marked with up and down
5762 arrow bitmaps.
5764 If value is a symbol `left' or `right', both angle and arrow bitmaps
5765 are displayed in the left or right fringe, resp. Any other value
5766 that doesn't look like an alist means display the angle bitmaps in
5767 the left fringe but no arrows.
5769 You can exercise more precise control by using an alist as the
5770 value. Each alist element (INDICATOR . POSITION) specifies
5771 where to show one of the indicators. INDICATOR is one of `top',
5772 `bottom', `up', `down', or t, which specifies the default position,
5773 and POSITION is one of `left', `right', or nil, meaning do not show
5774 this indicator.
5776 For example, ((top . left) (t . right)) places the top angle bitmap in
5777 left fringe, the bottom angle bitmap in right fringe, and both arrow
5778 bitmaps in right fringe. To show just the angle bitmaps in the left
5779 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5781 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5782 &BVAR (current_buffer, fringe_indicator_alist), Qnil,
5783 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5784 The value is an alist where each element (INDICATOR . BITMAPS)
5785 specifies the fringe bitmaps used to display a specific logical
5786 fringe indicator.
5788 INDICATOR specifies the logical indicator type which is one of the
5789 following symbols: `truncation' , `continuation', `overlay-arrow',
5790 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5792 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5793 the actual bitmap shown in the left or right fringe for the logical
5794 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5795 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5796 are used only for the `bottom' and `top-bottom' indicators when the
5797 last (only) line has no final newline. BITMAPS may also be a single
5798 symbol which is used in both left and right fringes. */);
5800 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5801 &BVAR (current_buffer, fringe_cursor_alist), Qnil,
5802 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5803 The value is an alist where each element (CURSOR . BITMAP)
5804 specifies the fringe bitmaps used to display a specific logical
5805 cursor type in the fringe.
5807 CURSOR specifies the logical cursor type which is one of the following
5808 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5809 one is used to show a hollow cursor on narrow lines display lines
5810 where the normal hollow cursor will not fit.
5812 BITMAP is the corresponding fringe bitmap shown for the logical
5813 cursor type. */);
5815 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5816 &BVAR (current_buffer, scroll_up_aggressively), Qnil,
5817 doc: /* How far to scroll windows upward.
5818 If you move point off the bottom, the window scrolls automatically.
5819 This variable controls how far it scrolls. The value nil, the default,
5820 means scroll to center point. A fraction means scroll to put point
5821 that fraction of the window's height from the bottom of the window.
5822 When the value is 0.0, point goes at the bottom line, which in the
5823 simple case that you moved off with C-f means scrolling just one line.
5824 1.0 means point goes at the top, so that in that simple case, the
5825 window scrolls by a full window height. Meaningful values are
5826 between 0.0 and 1.0, inclusive. */);
5828 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5829 &BVAR (current_buffer, scroll_down_aggressively), Qnil,
5830 doc: /* How far to scroll windows downward.
5831 If you move point off the top, the window scrolls automatically.
5832 This variable controls how far it scrolls. The value nil, the default,
5833 means scroll to center point. A fraction means scroll to put point
5834 that fraction of the window's height from the top of the window.
5835 When the value is 0.0, point goes at the top line, which in the
5836 simple case that you moved off with C-b means scrolling just one line.
5837 1.0 means point goes at the bottom, so that in that simple case, the
5838 window scrolls by a full window height. Meaningful values are
5839 between 0.0 and 1.0, inclusive. */);
5841 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5842 "Don't ask.");
5845 DEFVAR_LISP ("before-change-functions", Vbefore_change_functions,
5846 doc: /* List of functions to call before each text change.
5847 Two arguments are passed to each function: the positions of
5848 the beginning and end of the range of old text to be changed.
5849 \(For an insertion, the beginning and end are at the same place.)
5850 No information is given about the length of the text after the change.
5852 Buffer changes made while executing the `before-change-functions'
5853 don't call any before-change or after-change functions.
5854 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5856 If an unhandled error happens in running these functions,
5857 the variable's value remains nil. That prevents the error
5858 from happening repeatedly and making Emacs nonfunctional. */);
5859 Vbefore_change_functions = Qnil;
5861 DEFVAR_LISP ("after-change-functions", Vafter_change_functions,
5862 doc: /* List of functions to call after each text change.
5863 Three arguments are passed to each function: the positions of
5864 the beginning and end of the range of changed text,
5865 and the length in bytes of the pre-change text replaced by that range.
5866 \(For an insertion, the pre-change length is zero;
5867 for a deletion, that length is the number of bytes deleted,
5868 and the post-change beginning and end are at the same place.)
5870 Buffer changes made while executing the `after-change-functions'
5871 don't call any before-change or after-change functions.
5872 That's because `inhibit-modification-hooks' is temporarily set non-nil.
5874 If an unhandled error happens in running these functions,
5875 the variable's value remains nil. That prevents the error
5876 from happening repeatedly and making Emacs nonfunctional. */);
5877 Vafter_change_functions = Qnil;
5879 DEFVAR_LISP ("first-change-hook", Vfirst_change_hook,
5880 doc: /* A list of functions to call before changing a buffer which is unmodified.
5881 The functions are run using the `run-hooks' function. */);
5882 Vfirst_change_hook = Qnil;
5884 DEFVAR_PER_BUFFER ("buffer-undo-list", &BVAR (current_buffer, undo_list), Qnil,
5885 doc: /* List of undo entries in current buffer.
5886 Recent changes come first; older changes follow newer.
5888 An entry (BEG . END) represents an insertion which begins at
5889 position BEG and ends at position END.
5891 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5892 from (abs POSITION). If POSITION is positive, point was at the front
5893 of the text being deleted; if negative, point was at the end.
5895 An entry (t HIGH . LOW) indicates that the buffer previously had
5896 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5897 of the visited file's modification time, as of that time. If the
5898 modification time of the most recent save is different, this entry is
5899 obsolete.
5901 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5902 was modified between BEG and END. PROPERTY is the property name,
5903 and VALUE is the old value.
5905 An entry (apply FUN-NAME . ARGS) means undo the change with
5906 \(apply FUN-NAME ARGS).
5908 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5909 in the active region. BEG and END is the range affected by this entry
5910 and DELTA is the number of bytes added or deleted in that range by
5911 this change.
5913 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5914 was adjusted in position by the offset DISTANCE (an integer).
5916 An entry of the form POSITION indicates that point was at the buffer
5917 location given by the integer. Undoing an entry of this form places
5918 point at POSITION.
5920 Entries with value `nil' mark undo boundaries. The undo command treats
5921 the changes between two undo boundaries as a single step to be undone.
5923 If the value of the variable is t, undo information is not recorded. */);
5925 DEFVAR_PER_BUFFER ("mark-active", &BVAR (current_buffer, mark_active), Qnil,
5926 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
5928 DEFVAR_PER_BUFFER ("cache-long-line-scans", &BVAR (current_buffer, cache_long_line_scans), Qnil,
5929 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
5931 Normally, the line-motion functions work by scanning the buffer for
5932 newlines. Columnar operations (like `move-to-column' and
5933 `compute-motion') also work by scanning the buffer, summing character
5934 widths as they go. This works well for ordinary text, but if the
5935 buffer's lines are very long (say, more than 500 characters), these
5936 motion functions will take longer to execute. Emacs may also take
5937 longer to update the display.
5939 If `cache-long-line-scans' is non-nil, these motion functions cache the
5940 results of their scans, and consult the cache to avoid rescanning
5941 regions of the buffer until the text is modified. The caches are most
5942 beneficial when they prevent the most searching---that is, when the
5943 buffer contains long lines and large regions of characters with the
5944 same, fixed screen width.
5946 When `cache-long-line-scans' is non-nil, processing short lines will
5947 become slightly slower (because of the overhead of consulting the
5948 cache), and the caches will use memory roughly proportional to the
5949 number of newlines and characters whose screen width varies.
5951 The caches require no explicit maintenance; their accuracy is
5952 maintained internally by the Emacs primitives. Enabling or disabling
5953 the cache should not affect the behavior of any of the motion
5954 functions; it should only affect their performance. */);
5956 DEFVAR_PER_BUFFER ("point-before-scroll", &BVAR (current_buffer, point_before_scroll), Qnil,
5957 doc: /* Value of point before the last series of scroll operations, or nil. */);
5959 DEFVAR_PER_BUFFER ("buffer-file-format", &BVAR (current_buffer, file_format), Qnil,
5960 doc: /* List of formats to use when saving this buffer.
5961 Formats are defined by `format-alist'. This variable is
5962 set when a file is visited. */);
5964 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
5965 &BVAR (current_buffer, auto_save_file_format), Qnil,
5966 doc: /* *Format in which to write auto-save files.
5967 Should be a list of symbols naming formats that are defined in `format-alist'.
5968 If it is t, which is the default, auto-save files are written in the
5969 same format as a regular save would use. */);
5971 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5972 &BVAR (current_buffer, invisibility_spec), Qnil,
5973 doc: /* Invisibility spec of this buffer.
5974 The default is t, which means that text is invisible
5975 if it has a non-nil `invisible' property.
5976 If the value is a list, a text character is invisible if its `invisible'
5977 property is an element in that list (or is a list with members in common).
5978 If an element is a cons cell of the form (PROP . ELLIPSIS),
5979 then characters with property value PROP are invisible,
5980 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
5982 DEFVAR_PER_BUFFER ("buffer-display-count",
5983 &BVAR (current_buffer, display_count), Qnil,
5984 doc: /* A number incremented each time this buffer is displayed in a window.
5985 The function `set-window-buffer' increments it. */);
5987 DEFVAR_PER_BUFFER ("buffer-display-time",
5988 &BVAR (current_buffer, display_time), Qnil,
5989 doc: /* Time stamp updated each time this buffer is displayed in a window.
5990 The function `set-window-buffer' updates this variable
5991 to the value obtained by calling `current-time'.
5992 If the buffer has never been shown in a window, the value is nil. */);
5994 DEFVAR_LISP ("transient-mark-mode", Vtransient_mark_mode,
5995 doc: /* Non-nil if Transient Mark mode is enabled.
5996 See the command `transient-mark-mode' for a description of this minor mode.
5998 Non-nil also enables highlighting of the region whenever the mark is active.
5999 The variable `highlight-nonselected-windows' controls whether to highlight
6000 all windows or just the selected window.
6002 Lisp programs may give this variable certain special values:
6004 - A value of `lambda' enables Transient Mark mode temporarily.
6005 It is disabled again after any subsequent action that would
6006 normally deactivate the mark (e.g. buffer modification).
6008 - A value of (only . OLDVAL) enables Transient Mark mode
6009 temporarily. After any subsequent point motion command that is
6010 not shift-translated, or any other action that would normally
6011 deactivate the mark (e.g. buffer modification), the value of
6012 `transient-mark-mode' is set to OLDVAL. */);
6013 Vtransient_mark_mode = Qnil;
6015 DEFVAR_LISP ("inhibit-read-only", Vinhibit_read_only,
6016 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6017 If the value is t, disregard `buffer-read-only' and all `read-only'
6018 text properties. If the value is a list, disregard `buffer-read-only'
6019 and disregard a `read-only' text property if the property value
6020 is a member of the list. */);
6021 Vinhibit_read_only = Qnil;
6023 DEFVAR_PER_BUFFER ("cursor-type", &BVAR (current_buffer, cursor_type), Qnil,
6024 doc: /* Cursor to use when this buffer is in the selected window.
6025 Values are interpreted as follows:
6027 t use the cursor specified for the frame
6028 nil don't display a cursor
6029 box display a filled box cursor
6030 hollow display a hollow box cursor
6031 bar display a vertical bar cursor with default width
6032 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6033 hbar display a horizontal bar cursor with default height
6034 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6035 ANYTHING ELSE display a hollow box cursor
6037 When the buffer is displayed in a non-selected window, the
6038 cursor's appearance is instead controlled by the variable
6039 `cursor-in-non-selected-windows'. */);
6041 DEFVAR_PER_BUFFER ("line-spacing",
6042 &BVAR (current_buffer, extra_line_spacing), Qnil,
6043 doc: /* Additional space to put between lines when displaying a buffer.
6044 The space is measured in pixels, and put below lines on graphic displays,
6045 see `display-graphic-p'.
6046 If value is a floating point number, it specifies the spacing relative
6047 to the default frame line height. A value of nil means add no extra space. */);
6049 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6050 &BVAR (current_buffer, cursor_in_non_selected_windows), Qnil,
6051 doc: /* *Non-nil means show a cursor in non-selected windows.
6052 If nil, only shows a cursor in the selected window.
6053 If t, displays a cursor related to the usual cursor type
6054 \(a solid box becomes hollow, a bar becomes a narrower bar).
6055 You can also specify the cursor type as in the `cursor-type' variable.
6056 Use Custom to set this variable and update the display." */);
6058 DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions,
6059 doc: /* List of functions called with no args to query before killing a buffer.
6060 The buffer being killed will be current while the functions are running.
6061 If any of them returns nil, the buffer is not killed. */);
6062 Vkill_buffer_query_functions = Qnil;
6064 DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
6065 doc: /* Normal hook run before changing the major mode of a buffer.
6066 The function `kill-all-local-variables' runs this before doing anything else. */);
6067 Vchange_major_mode_hook = Qnil;
6068 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6069 staticpro (&Qchange_major_mode_hook);
6071 DEFVAR_LISP ("buffer-list-update-hook", Vbuffer_list_update_hook,
6072 doc: /* Hook run when the buffer list changes.
6073 Functions running this hook are `get-buffer-create',
6074 `make-indirect-buffer', `rename-buffer', `kill-buffer',
6075 `record-buffer' and `unrecord-buffer'. */);
6076 Vbuffer_list_update_hook = Qnil;
6077 Qbuffer_list_update_hook = intern_c_string ("buffer-list-update-hook");
6078 staticpro (&Qbuffer_list_update_hook);
6080 defsubr (&Sbuffer_live_p);
6081 defsubr (&Sbuffer_list);
6082 defsubr (&Sget_buffer);
6083 defsubr (&Sget_file_buffer);
6084 defsubr (&Sget_buffer_create);
6085 defsubr (&Smake_indirect_buffer);
6086 defsubr (&Sgenerate_new_buffer_name);
6087 defsubr (&Sbuffer_name);
6088 /*defsubr (&Sbuffer_number);*/
6089 defsubr (&Sbuffer_file_name);
6090 defsubr (&Sbuffer_base_buffer);
6091 defsubr (&Sbuffer_local_value);
6092 defsubr (&Sbuffer_local_variables);
6093 defsubr (&Sbuffer_modified_p);
6094 defsubr (&Sset_buffer_modified_p);
6095 defsubr (&Sbuffer_modified_tick);
6096 defsubr (&Sbuffer_chars_modified_tick);
6097 defsubr (&Srename_buffer);
6098 defsubr (&Sother_buffer);
6099 defsubr (&Sbuffer_enable_undo);
6100 defsubr (&Skill_buffer);
6101 defsubr (&Srecord_buffer);
6102 defsubr (&Sunrecord_buffer);
6103 defsubr (&Sset_buffer_major_mode);
6104 defsubr (&Scurrent_buffer);
6105 defsubr (&Sset_buffer);
6106 defsubr (&Sbarf_if_buffer_read_only);
6107 defsubr (&Serase_buffer);
6108 defsubr (&Sbuffer_swap_text);
6109 defsubr (&Sset_buffer_multibyte);
6110 defsubr (&Skill_all_local_variables);
6112 defsubr (&Soverlayp);
6113 defsubr (&Smake_overlay);
6114 defsubr (&Sdelete_overlay);
6115 defsubr (&Smove_overlay);
6116 defsubr (&Soverlay_start);
6117 defsubr (&Soverlay_end);
6118 defsubr (&Soverlay_buffer);
6119 defsubr (&Soverlay_properties);
6120 defsubr (&Soverlays_at);
6121 defsubr (&Soverlays_in);
6122 defsubr (&Snext_overlay_change);
6123 defsubr (&Sprevious_overlay_change);
6124 defsubr (&Soverlay_recenter);
6125 defsubr (&Soverlay_lists);
6126 defsubr (&Soverlay_get);
6127 defsubr (&Soverlay_put);
6128 defsubr (&Srestore_buffer_modified_p);
6131 void
6132 keys_of_buffer (void)
6134 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6135 initial_define_key (control_x_map, 'k', "kill-buffer");
6137 /* This must not be in syms_of_buffer, because Qdisabled is not
6138 initialized when that function gets called. */
6139 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);