Initial code for buffer locking.
[emacs.git] / src / buffer.c
blob10378c3edf4a986583b9daae72354e11a0a17607
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <setjmp.h>
31 #ifndef USE_CRT_DLL
32 extern int errno;
33 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #include <pthread.h>
42 #include "lisp.h"
43 #include "intervals.h"
44 #include "window.h"
45 #include "commands.h"
46 #include "buffer.h"
47 #include "character.h"
48 #include "region-cache.h"
49 #include "indent.h"
50 #include "blockinput.h"
51 #include "keyboard.h"
52 #include "keymap.h"
53 #include "frame.h"
55 /* First buffer in chain of all buffers (in reverse order of creation).
56 Threaded through ->next. */
58 struct buffer *all_buffers;
60 /* This structure holds the default values of the buffer-local variables
61 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
62 The default value occupies the same slot in this structure
63 as an individual buffer's value occupies in that buffer.
64 Setting the default value also goes through the alist of buffers
65 and stores into each buffer that does not say it has a local value. */
67 DECL_ALIGN (struct buffer, buffer_defaults);
69 /* A Lisp_Object pointer to the above, used for staticpro */
71 static Lisp_Object Vbuffer_defaults;
73 /* This structure marks which slots in a buffer have corresponding
74 default values in buffer_defaults.
75 Each such slot has a nonzero value in this structure.
76 The value has only one nonzero bit.
78 When a buffer has its own local value for a slot,
79 the entry for that slot (found in the same slot in this structure)
80 is turned on in the buffer's local_flags array.
82 If a slot in this structure is -1, then even though there may
83 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
84 and the corresponding slot in buffer_defaults is not used.
86 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
87 but there is a default value which is copied into each buffer.
89 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
90 zero, that is a bug */
92 struct buffer buffer_local_flags;
94 /* This structure holds the names of symbols whose values may be
95 buffer-local. It is indexed and accessed in the same way as the above. */
97 DECL_ALIGN (struct buffer, buffer_local_symbols);
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols;
102 /* Flags indicating which built-in buffer-local variables
103 are permanent locals. */
104 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
106 /* Number of per-buffer variables used. */
108 int last_per_buffer_idx;
110 /* condition var .. w/ global lock */
112 static pthread_cond_t buffer_cond;
114 EXFUN (Fset_buffer, 1);
115 void set_buffer_internal P_ ((struct buffer *b));
116 void set_buffer_internal_1 P_ ((struct buffer *b));
117 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
118 int after, Lisp_Object arg1,
119 Lisp_Object arg2, Lisp_Object arg3));
120 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
121 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
123 /* Alist of all buffer names vs the buffers. */
124 /* This used to be a variable, but is no longer,
125 to prevent lossage due to user rplac'ing this alist or its elements. */
126 Lisp_Object Vbuffer_alist;
128 /* Functions to call before and after each text change. */
129 Lisp_Object impl_Vbefore_change_functions;
130 Lisp_Object impl_Vafter_change_functions;
132 Lisp_Object impl_Vtransient_mark_mode;
134 /* t means ignore all read-only text properties.
135 A list means ignore such a property if its value is a member of the list.
136 Any non-nil value means ignore buffer-read-only. */
137 Lisp_Object impl_Vinhibit_read_only;
139 /* List of functions to call that can query about killing a buffer.
140 If any of these functions returns nil, we don't kill it. */
141 Lisp_Object impl_Vkill_buffer_query_functions;
142 Lisp_Object Qkill_buffer_query_functions;
144 /* Hook run before changing a major mode. */
145 Lisp_Object impl_Vchange_major_mode_hook, Qchange_major_mode_hook;
147 /* List of functions to call before changing an unmodified buffer. */
148 Lisp_Object impl_Vfirst_change_hook;
150 Lisp_Object Qfirst_change_hook;
151 Lisp_Object Qbefore_change_functions;
152 Lisp_Object Qafter_change_functions;
153 Lisp_Object Qucs_set_table_for_input;
155 /* If nonzero, all modification hooks are suppressed. */
156 int inhibit_modification_hooks;
158 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
159 Lisp_Object Qpermanent_local_hook;
161 Lisp_Object Qprotected_field;
163 Lisp_Object QSFundamental; /* A string "Fundamental" */
165 Lisp_Object Qkill_buffer_hook;
167 Lisp_Object Qget_file_buffer;
169 Lisp_Object Qoverlayp;
171 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
173 Lisp_Object Qmodification_hooks;
174 Lisp_Object Qinsert_in_front_hooks;
175 Lisp_Object Qinsert_behind_hooks;
177 static void alloc_buffer_text P_ ((struct buffer *, size_t));
178 static void free_buffer_text P_ ((struct buffer *b));
179 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
180 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
181 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
183 extern char * emacs_strerror P_ ((int));
185 /* For debugging; temporary. See set_buffer_internal. */
186 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
188 void
189 nsberror (spec)
190 Lisp_Object spec;
192 if (STRINGP (spec))
193 error ("No buffer named %s", SDATA (spec));
194 error ("Invalid buffer argument");
197 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
198 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
199 Value is nil if OBJECT is not a buffer or if it has been killed. */)
200 (object)
201 Lisp_Object object;
203 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
204 ? Qt : Qnil);
207 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
208 doc: /* Return a list of all existing live buffers.
209 If the optional arg FRAME is a frame, we return the buffer list
210 in the proper order for that frame: the buffers in FRAME's `buffer-list'
211 frame parameter come first, followed by the rest of the buffers. */)
212 (frame)
213 Lisp_Object frame;
215 Lisp_Object general;
216 general = Fmapcar (Qcdr, Vbuffer_alist);
218 if (FRAMEP (frame))
220 Lisp_Object framelist, prevlist, tail;
221 Lisp_Object args[3];
223 CHECK_FRAME (frame);
225 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
226 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
228 /* Remove from GENERAL any buffer that duplicates one in
229 FRAMELIST or PREVLIST. */
230 tail = framelist;
231 while (CONSP (tail))
233 general = Fdelq (XCAR (tail), general);
234 tail = XCDR (tail);
236 tail = prevlist;
237 while (CONSP (tail))
239 general = Fdelq (XCAR (tail), general);
240 tail = XCDR (tail);
243 args[0] = framelist;
244 args[1] = general;
245 args[2] = prevlist;
246 return Fnconc (3, args);
249 return general;
252 /* Like Fassoc, but use Fstring_equal to compare
253 (which ignores text properties),
254 and don't ever QUIT. */
256 static Lisp_Object
257 assoc_ignore_text_properties (key, list)
258 register Lisp_Object key;
259 Lisp_Object list;
261 register Lisp_Object tail;
262 for (tail = list; CONSP (tail); tail = XCDR (tail))
264 register Lisp_Object elt, tem;
265 elt = XCAR (tail);
266 tem = Fstring_equal (Fcar (elt), key);
267 if (!NILP (tem))
268 return elt;
270 return Qnil;
273 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
274 doc: /* Return the buffer named BUFFER-OR-NAME.
275 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
276 is a string and there is no buffer with that name, return nil. If
277 BUFFER-OR-NAME is a buffer, return it as given. */)
278 (buffer_or_name)
279 register Lisp_Object buffer_or_name;
281 if (BUFFERP (buffer_or_name))
282 return buffer_or_name;
283 CHECK_STRING (buffer_or_name);
285 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
288 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
289 doc: /* Return the buffer visiting file FILENAME (a string).
290 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
291 If there is no such live buffer, return nil.
292 See also `find-buffer-visiting'. */)
293 (filename)
294 register Lisp_Object filename;
296 register Lisp_Object tail, buf, tem;
297 Lisp_Object handler;
299 CHECK_STRING (filename);
300 filename = Fexpand_file_name (filename, Qnil);
302 /* If the file name has special constructs in it,
303 call the corresponding file handler. */
304 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
305 if (!NILP (handler))
306 return call2 (handler, Qget_file_buffer, filename);
308 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
310 buf = Fcdr (XCAR (tail));
311 if (!BUFFERP (buf)) continue;
312 if (!STRINGP (XBUFFER (buf)->filename)) continue;
313 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
314 if (!NILP (tem))
315 return buf;
317 return Qnil;
320 Lisp_Object
321 get_truename_buffer (filename)
322 register Lisp_Object filename;
324 register Lisp_Object tail, buf, tem;
326 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
328 buf = Fcdr (XCAR (tail));
329 if (!BUFFERP (buf)) continue;
330 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
331 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
332 if (!NILP (tem))
333 return buf;
335 return Qnil;
338 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
339 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
340 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
341 return that buffer. If no such buffer exists, create a new buffer with
342 that name and return it. If BUFFER-OR-NAME starts with a space, the new
343 buffer does not keep undo information.
345 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
346 even if it is dead. The return value is never nil. */)
347 (buffer_or_name)
348 register Lisp_Object buffer_or_name;
350 register Lisp_Object buffer, name;
351 register struct buffer *b;
353 buffer = Fget_buffer (buffer_or_name);
354 if (!NILP (buffer))
355 return buffer;
357 if (SCHARS (buffer_or_name) == 0)
358 error ("Empty string for buffer name is not allowed");
360 b = allocate_buffer ();
362 /* An ordinary buffer uses its own struct buffer_text. */
363 b->text = &b->own_text;
364 b->base_buffer = 0;
366 BUF_GAP_SIZE (b) = 20;
367 BLOCK_INPUT;
368 /* We allocate extra 1-byte at the tail and keep it always '\0' for
369 anchoring a search. */
370 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
371 UNBLOCK_INPUT;
372 if (! BUF_BEG_ADDR (b))
373 buffer_memory_full ();
375 BUF_PT (b) = BEG;
376 BUF_GPT (b) = BEG;
377 BUF_BEGV (b) = BEG;
378 BUF_ZV (b) = BEG;
379 BUF_Z (b) = BEG;
380 BUF_PT_BYTE (b) = BEG_BYTE;
381 BUF_GPT_BYTE (b) = BEG_BYTE;
382 BUF_BEGV_BYTE (b) = BEG_BYTE;
383 BUF_ZV_BYTE (b) = BEG_BYTE;
384 BUF_Z_BYTE (b) = BEG_BYTE;
385 BUF_MODIFF (b) = 1;
386 BUF_CHARS_MODIFF (b) = 1;
387 BUF_OVERLAY_MODIFF (b) = 1;
388 BUF_SAVE_MODIFF (b) = 1;
389 BUF_INTERVALS (b) = 0;
390 BUF_UNCHANGED_MODIFIED (b) = 1;
391 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
392 BUF_END_UNCHANGED (b) = 0;
393 BUF_BEG_UNCHANGED (b) = 0;
394 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
396 b->newline_cache = 0;
397 b->width_run_cache = 0;
398 b->width_table = Qnil;
399 b->prevent_redisplay_optimizations_p = 1;
401 /* Put this on the chain of all buffers including killed ones. */
402 b->next = all_buffers;
403 all_buffers = b;
405 /* An ordinary buffer normally doesn't need markers
406 to handle BEGV and ZV. */
407 b->pt_marker = Qnil;
408 b->begv_marker = Qnil;
409 b->zv_marker = Qnil;
411 name = Fcopy_sequence (buffer_or_name);
412 STRING_SET_INTERVALS (name, NULL_INTERVAL);
413 b->name = name;
415 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
417 reset_buffer (b);
418 reset_buffer_local_variables (b, 1);
420 b->mark = Fmake_marker ();
421 BUF_MARKERS (b) = NULL;
422 b->name = name;
424 /* Put this in the alist of all live buffers. */
425 XSETBUFFER (buffer, b);
426 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
428 /* An error in calling the function here (should someone redefine it)
429 can lead to infinite regress until you run out of stack. rms
430 says that's not worth protecting against. */
431 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
432 /* buffer is on buffer-alist, so no gcpro. */
433 call1 (Qucs_set_table_for_input, buffer);
435 return buffer;
439 /* Return a list of overlays which is a copy of the overlay list
440 LIST, but for buffer B. */
442 static struct Lisp_Overlay *
443 copy_overlays (b, list)
444 struct buffer *b;
445 struct Lisp_Overlay *list;
447 Lisp_Object buffer;
448 struct Lisp_Overlay *result = NULL, *tail = NULL;
450 XSETBUFFER (buffer, b);
452 for (; list; list = list->next)
454 Lisp_Object overlay, start, end, old_overlay;
455 EMACS_INT charpos;
457 XSETMISC (old_overlay, list);
458 charpos = marker_position (OVERLAY_START (old_overlay));
459 start = Fmake_marker ();
460 Fset_marker (start, make_number (charpos), buffer);
461 XMARKER (start)->insertion_type
462 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
464 charpos = marker_position (OVERLAY_END (old_overlay));
465 end = Fmake_marker ();
466 Fset_marker (end, make_number (charpos), buffer);
467 XMARKER (end)->insertion_type
468 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
470 overlay = allocate_misc ();
471 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
472 OVERLAY_START (overlay) = start;
473 OVERLAY_END (overlay) = end;
474 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
475 XOVERLAY (overlay)->next = NULL;
477 if (tail)
478 tail = tail->next = XOVERLAY (overlay);
479 else
480 result = tail = XOVERLAY (overlay);
483 return result;
487 /* Clone per-buffer values of buffer FROM.
489 Buffer TO gets the same per-buffer values as FROM, with the
490 following exceptions: (1) TO's name is left untouched, (2) markers
491 are copied and made to refer to TO, and (3) overlay lists are
492 copied. */
494 static void
495 clone_per_buffer_values (from, to)
496 struct buffer *from, *to;
498 Lisp_Object to_buffer;
499 int offset;
501 XSETBUFFER (to_buffer, to);
503 /* buffer-local Lisp variables start at `undo_list',
504 tho only the ones from `name' on are GC'd normally. */
505 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
506 offset < sizeof *to;
507 offset += sizeof (Lisp_Object))
509 Lisp_Object obj;
511 /* Don't touch the `name' which should be unique for every buffer. */
512 if (offset == PER_BUFFER_VAR_OFFSET (name))
513 continue;
515 obj = PER_BUFFER_VALUE (from, offset);
516 if (MARKERP (obj))
518 struct Lisp_Marker *m = XMARKER (obj);
519 obj = Fmake_marker ();
520 XMARKER (obj)->insertion_type = m->insertion_type;
521 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
524 PER_BUFFER_VALUE (to, offset) = obj;
527 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
529 to->overlays_before = copy_overlays (to, from->overlays_before);
530 to->overlays_after = copy_overlays (to, from->overlays_after);
532 /* Get (a copy of) the alist of Lisp-level local variables of FROM
533 and install that in TO. */
534 to->local_var_alist = buffer_lisp_local_variables (from);
537 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
538 2, 3,
539 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
540 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
541 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
542 NAME should be a string which is not the name of an existing buffer.
543 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
544 such as major and minor modes, in the indirect buffer.
545 CLONE nil means the indirect buffer's state is reset to default values. */)
546 (base_buffer, name, clone)
547 Lisp_Object base_buffer, name, clone;
549 Lisp_Object buf, tem;
550 struct buffer *b;
552 CHECK_STRING (name);
553 buf = Fget_buffer (name);
554 if (!NILP (buf))
555 error ("Buffer name `%s' is in use", SDATA (name));
557 tem = base_buffer;
558 base_buffer = Fget_buffer (base_buffer);
559 if (NILP (base_buffer))
560 error ("No such buffer: `%s'", SDATA (tem));
561 if (NILP (XBUFFER (base_buffer)->name))
562 error ("Base buffer has been killed");
564 if (SCHARS (name) == 0)
565 error ("Empty string for buffer name is not allowed");
567 b = allocate_buffer ();
569 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
570 ? XBUFFER (base_buffer)->base_buffer
571 : XBUFFER (base_buffer));
573 /* Use the base buffer's text object. */
574 b->text = b->base_buffer->text;
576 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
577 BUF_ZV (b) = BUF_ZV (b->base_buffer);
578 BUF_PT (b) = BUF_PT (b->base_buffer);
579 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
580 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
581 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
583 b->newline_cache = 0;
584 b->width_run_cache = 0;
585 b->width_table = Qnil;
587 /* Put this on the chain of all buffers including killed ones. */
588 b->next = all_buffers;
589 all_buffers = b;
591 name = Fcopy_sequence (name);
592 STRING_SET_INTERVALS (name, NULL_INTERVAL);
593 b->name = name;
595 reset_buffer (b);
596 reset_buffer_local_variables (b, 1);
598 /* Put this in the alist of all live buffers. */
599 XSETBUFFER (buf, b);
600 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
602 b->mark = Fmake_marker ();
603 b->name = name;
605 /* The multibyte status belongs to the base buffer. */
606 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
608 /* Make sure the base buffer has markers for its narrowing. */
609 if (NILP (b->base_buffer->pt_marker))
611 b->base_buffer->pt_marker = Fmake_marker ();
612 set_marker_both (b->base_buffer->pt_marker, base_buffer,
613 BUF_PT (b->base_buffer),
614 BUF_PT_BYTE (b->base_buffer));
616 if (NILP (b->base_buffer->begv_marker))
618 b->base_buffer->begv_marker = Fmake_marker ();
619 set_marker_both (b->base_buffer->begv_marker, base_buffer,
620 BUF_BEGV (b->base_buffer),
621 BUF_BEGV_BYTE (b->base_buffer));
623 if (NILP (b->base_buffer->zv_marker))
625 b->base_buffer->zv_marker = Fmake_marker ();
626 set_marker_both (b->base_buffer->zv_marker, base_buffer,
627 BUF_ZV (b->base_buffer),
628 BUF_ZV_BYTE (b->base_buffer));
629 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
632 if (NILP (clone))
634 /* Give the indirect buffer markers for its narrowing. */
635 b->pt_marker = Fmake_marker ();
636 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
637 b->begv_marker = Fmake_marker ();
638 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
639 b->zv_marker = Fmake_marker ();
640 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
641 XMARKER (b->zv_marker)->insertion_type = 1;
643 else
645 struct buffer *old_b = current_buffer;
647 clone_per_buffer_values (b->base_buffer, b);
648 b->filename = Qnil;
649 b->file_truename = Qnil;
650 b->display_count = make_number (0);
651 b->backed_up = Qnil;
652 b->auto_save_file_name = Qnil;
653 set_buffer_internal_1 (b);
654 Fset (intern ("buffer-save-without-query"), Qnil);
655 Fset (intern ("buffer-file-number"), Qnil);
656 Fset (intern ("buffer-stale-function"), Qnil);
657 set_buffer_internal_1 (old_b);
660 return buf;
663 void
664 delete_all_overlays (b)
665 struct buffer *b;
667 Lisp_Object overlay;
669 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
670 have to empty the list, otherwise we end up with overlays that
671 think they belong to this buffer while the buffer doesn't know about
672 them any more. */
673 while (b->overlays_before)
675 XSETMISC (overlay, b->overlays_before);
676 Fdelete_overlay (overlay);
678 while (b->overlays_after)
680 XSETMISC (overlay, b->overlays_after);
681 Fdelete_overlay (overlay);
683 eassert (b->overlays_before == NULL);
684 eassert (b->overlays_after == NULL);
687 /* Reinitialize everything about a buffer except its name and contents
688 and local variables.
689 If called on an already-initialized buffer, the list of overlays
690 should be deleted before calling this function, otherwise we end up
691 with overlays that claim to belong to the buffer but the buffer
692 claims it doesn't belong to it. */
694 void
695 reset_buffer (b)
696 register struct buffer *b;
698 b->filename = Qnil;
699 b->file_truename = Qnil;
700 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
701 b->modtime = 0;
702 XSETFASTINT (b->save_length, 0);
703 b->last_window_start = 1;
704 /* It is more conservative to start out "changed" than "unchanged". */
705 b->clip_changed = 0;
706 b->prevent_redisplay_optimizations_p = 1;
707 b->backed_up = Qnil;
708 BUF_AUTOSAVE_MODIFF (b) = 0;
709 b->auto_save_failure_time = -1;
710 b->auto_save_file_name = Qnil;
711 b->read_only = Qnil;
712 b->overlays_before = NULL;
713 b->overlays_after = NULL;
714 b->overlay_center = BEG;
715 b->mark_active = Qnil;
716 b->point_before_scroll = Qnil;
717 b->file_format = Qnil;
718 b->auto_save_file_format = Qt;
719 b->last_selected_window = Qnil;
720 XSETINT (b->display_count, 0);
721 b->display_time = Qnil;
722 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
723 b->cursor_type = buffer_defaults.cursor_type;
724 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
726 b->display_error_modiff = 0;
729 /* Reset buffer B's local variables info.
730 Don't use this on a buffer that has already been in use;
731 it does not treat permanent locals consistently.
732 Instead, use Fkill_all_local_variables.
734 If PERMANENT_TOO is 1, then we reset permanent
735 buffer-local variables. If PERMANENT_TOO is 0,
736 we preserve those. */
738 static void
739 reset_buffer_local_variables (b, permanent_too)
740 register struct buffer *b;
741 int permanent_too;
743 register int offset;
744 int i;
746 /* Reset the major mode to Fundamental, together with all the
747 things that depend on the major mode.
748 default-major-mode is handled at a higher level.
749 We ignore it here. */
750 b->major_mode = Qfundamental_mode;
751 b->keymap = Qnil;
752 b->mode_name = QSFundamental;
753 b->minor_modes = Qnil;
755 /* If the standard case table has been altered and invalidated,
756 fix up its insides first. */
757 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
758 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
759 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
760 Fset_standard_case_table (Vascii_downcase_table);
762 b->downcase_table = Vascii_downcase_table;
763 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
764 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
765 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
766 b->invisibility_spec = Qt;
767 #ifndef DOS_NT
768 b->buffer_file_type = Qnil;
769 #endif
771 /* Reset all (or most) per-buffer variables to their defaults. */
772 if (permanent_too)
773 b->local_var_alist = Qnil;
774 else
776 Lisp_Object tmp, prop, last = Qnil;
777 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
778 if (CONSP (XCAR (tmp))
779 && SYMBOLP (XCAR (XCAR (tmp)))
780 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
782 /* If permanent-local, keep it. */
783 last = tmp;
784 if (EQ (prop, Qpermanent_local_hook))
786 /* This is a partially permanent hook variable.
787 Preserve only the elements that want to be preserved. */
788 Lisp_Object list, newlist;
789 list = XCDR (XCAR (tmp));
790 if (!CONSP (list))
791 newlist = list;
792 else
793 for (newlist = Qnil; CONSP (list); list = XCDR (list))
795 Lisp_Object elt = XCAR (list);
796 /* Preserve element ELT if it's t,
797 if it is a function with a `permanent-local-hook' property,
798 or if it's not a symbol. */
799 if (! SYMBOLP (elt)
800 || EQ (elt, Qt)
801 || !NILP (Fget (elt, Qpermanent_local_hook)))
802 newlist = Fcons (elt, newlist);
804 XSETCDR (XCAR (tmp), Fnreverse (newlist));
807 /* Delete this local variable. */
808 else if (NILP (last))
809 b->local_var_alist = XCDR (tmp);
810 else
811 XSETCDR (last, XCDR (tmp));
814 for (i = 0; i < last_per_buffer_idx; ++i)
815 if (permanent_too || buffer_permanent_local_flags[i] == 0)
816 SET_PER_BUFFER_VALUE_P (b, i, 0);
818 /* For each slot that has a default value,
819 copy that into the slot. */
821 /* buffer-local Lisp variables start at `undo_list',
822 tho only the ones from `name' on are GC'd normally. */
823 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
824 offset < sizeof *b;
825 offset += sizeof (Lisp_Object))
827 int idx = PER_BUFFER_IDX (offset);
828 if ((idx > 0
829 && (permanent_too
830 || buffer_permanent_local_flags[idx] == 0))
831 /* Is -2 used anywhere? */
832 || idx == -2)
833 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
837 /* We split this away from generate-new-buffer, because rename-buffer
838 and set-visited-file-name ought to be able to use this to really
839 rename the buffer properly. */
841 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
842 1, 2, 0,
843 doc: /* Return a string that is the name of no existing buffer based on NAME.
844 If there is no live buffer named NAME, then return NAME.
845 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
846 \(starting at 2) until an unused name is found, and then return that name.
847 Optional second argument IGNORE specifies a name that is okay to use (if
848 it is in the sequence to be tried) even if a buffer with that name exists. */)
849 (name, ignore)
850 register Lisp_Object name, ignore;
852 register Lisp_Object gentemp, tem;
853 int count;
854 char number[10];
856 CHECK_STRING (name);
858 tem = Fstring_equal (name, ignore);
859 if (!NILP (tem))
860 return name;
861 tem = Fget_buffer (name);
862 if (NILP (tem))
863 return name;
865 count = 1;
866 while (1)
868 sprintf (number, "<%d>", ++count);
869 gentemp = concat2 (name, build_string (number));
870 tem = Fstring_equal (gentemp, ignore);
871 if (!NILP (tem))
872 return gentemp;
873 tem = Fget_buffer (gentemp);
874 if (NILP (tem))
875 return gentemp;
880 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
881 doc: /* Return the name of BUFFER, as a string.
882 BUFFER defaults to the current buffer.
883 Return nil if BUFFER has been killed. */)
884 (buffer)
885 register Lisp_Object buffer;
887 if (NILP (buffer))
888 return current_buffer->name;
889 CHECK_BUFFER (buffer);
890 return XBUFFER (buffer)->name;
893 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
894 doc: /* Return name of file BUFFER is visiting, or nil if none.
895 No argument or nil as argument means use the current buffer. */)
896 (buffer)
897 register Lisp_Object buffer;
899 if (NILP (buffer))
900 return current_buffer->filename;
901 CHECK_BUFFER (buffer);
902 return XBUFFER (buffer)->filename;
905 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
906 0, 1, 0,
907 doc: /* Return the base buffer of indirect buffer BUFFER.
908 If BUFFER is not indirect, return nil.
909 BUFFER defaults to the current buffer. */)
910 (buffer)
911 register Lisp_Object buffer;
913 struct buffer *base;
914 Lisp_Object base_buffer;
916 if (NILP (buffer))
917 base = current_buffer->base_buffer;
918 else
920 CHECK_BUFFER (buffer);
921 base = XBUFFER (buffer)->base_buffer;
924 if (! base)
925 return Qnil;
926 XSETBUFFER (base_buffer, base);
927 return base_buffer;
930 DEFUN ("buffer-local-value", Fbuffer_local_value,
931 Sbuffer_local_value, 2, 2, 0,
932 doc: /* Return the value of VARIABLE in BUFFER.
933 If VARIABLE does not have a buffer-local binding in BUFFER, the value
934 is the default binding of the variable. */)
935 (variable, buffer)
936 register Lisp_Object variable;
937 register Lisp_Object buffer;
939 register struct buffer *buf;
940 register Lisp_Object result;
941 struct Lisp_Symbol *sym;
943 CHECK_SYMBOL (variable);
944 CHECK_BUFFER (buffer);
945 buf = XBUFFER (buffer);
947 sym = indirect_variable (XSYMBOL (variable));
948 XSETSYMBOL (variable, sym);
950 /* Look in local_var_list */
951 result = Fassoc (variable, buf->local_var_alist);
952 if (NILP (result))
954 int offset, idx;
955 int found = 0;
957 /* Look in special slots */
958 /* buffer-local Lisp variables start at `undo_list',
959 tho only the ones from `name' on are GC'd normally. */
960 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
961 offset < sizeof (struct buffer);
962 /* sizeof EMACS_INT == sizeof Lisp_Object */
963 offset += (sizeof (EMACS_INT)))
965 idx = PER_BUFFER_IDX (offset);
966 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
967 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
968 && EQ (PER_BUFFER_SYMBOL (offset), variable))
970 result = PER_BUFFER_VALUE (buf, offset);
971 found = 1;
972 break;
976 if (!found)
977 result = Fdefault_value (variable);
979 else
981 Lisp_Object valcontents;
982 Lisp_Object current_alist_element;
984 /* What binding is loaded right now? */
985 valcontents = sym->value;
986 current_alist_element
987 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
989 /* The value of the currently loaded binding is not
990 stored in it, but rather in the realvalue slot.
991 Store that value into the binding it belongs to
992 in case that is the one we are about to use. */
994 Fsetcdr (current_alist_element,
995 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
997 /* Now get the (perhaps updated) value out of the binding. */
998 result = XCDR (result);
1001 if (!EQ (result, Qunbound))
1002 return result;
1004 xsignal1 (Qvoid_variable, variable);
1007 /* Return an alist of the Lisp-level buffer-local bindings of
1008 buffer BUF. That is, don't include the variables maintained
1009 in special slots in the buffer object. */
1011 static Lisp_Object
1012 buffer_lisp_local_variables (buf)
1013 struct buffer *buf;
1015 Lisp_Object result = Qnil;
1016 register Lisp_Object tail;
1017 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1019 Lisp_Object val, elt;
1021 elt = XCAR (tail);
1023 /* Reference each variable in the alist in buf.
1024 If inquiring about the current buffer, this gets the current values,
1025 so store them into the alist so the alist is up to date.
1026 If inquiring about some other buffer, this swaps out any values
1027 for that buffer, making the alist up to date automatically. */
1028 val = find_symbol_value (XCAR (elt));
1029 /* Use the current buffer value only if buf is the current buffer. */
1030 if (buf != current_buffer)
1031 val = XCDR (elt);
1033 /* If symbol is unbound, put just the symbol in the list. */
1034 if (EQ (val, Qunbound))
1035 result = Fcons (XCAR (elt), result);
1036 /* Otherwise, put (symbol . value) in the list. */
1037 else
1038 result = Fcons (Fcons (XCAR (elt), val), result);
1041 return result;
1044 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1045 Sbuffer_local_variables, 0, 1, 0,
1046 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1047 Most elements look like (SYMBOL . VALUE), describing one variable.
1048 For a symbol that is locally unbound, just the symbol appears in the value.
1049 Note that storing new VALUEs in these elements doesn't change the variables.
1050 No argument or nil as argument means use current buffer as BUFFER. */)
1051 (buffer)
1052 register Lisp_Object buffer;
1054 register struct buffer *buf;
1055 register Lisp_Object result;
1057 if (NILP (buffer))
1058 buf = current_buffer;
1059 else
1061 CHECK_BUFFER (buffer);
1062 buf = XBUFFER (buffer);
1065 result = buffer_lisp_local_variables (buf);
1067 /* Add on all the variables stored in special slots. */
1069 int offset, idx;
1071 /* buffer-local Lisp variables start at `undo_list',
1072 tho only the ones from `name' on are GC'd normally. */
1073 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1074 offset < sizeof (struct buffer);
1075 /* sizeof EMACS_INT == sizeof Lisp_Object */
1076 offset += (sizeof (EMACS_INT)))
1078 idx = PER_BUFFER_IDX (offset);
1079 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1080 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1081 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1082 PER_BUFFER_VALUE (buf, offset)),
1083 result);
1087 return result;
1090 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1091 0, 1, 0,
1092 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1093 No argument or nil as argument means use current buffer as BUFFER. */)
1094 (buffer)
1095 register Lisp_Object buffer;
1097 register struct buffer *buf;
1098 if (NILP (buffer))
1099 buf = current_buffer;
1100 else
1102 CHECK_BUFFER (buffer);
1103 buf = XBUFFER (buffer);
1106 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1109 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1110 1, 1, 0,
1111 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1112 A non-nil FLAG means mark the buffer modified. */)
1113 (flag)
1114 register Lisp_Object flag;
1116 register int already;
1117 register Lisp_Object fn;
1118 Lisp_Object buffer, window;
1120 #ifdef CLASH_DETECTION
1121 /* If buffer becoming modified, lock the file.
1122 If buffer becoming unmodified, unlock the file. */
1124 fn = current_buffer->file_truename;
1125 /* Test buffer-file-name so that binding it to nil is effective. */
1126 if (!NILP (fn) && ! NILP (current_buffer->filename))
1128 already = SAVE_MODIFF < MODIFF;
1129 if (!already && !NILP (flag))
1130 lock_file (fn);
1131 else if (already && NILP (flag))
1132 unlock_file (fn);
1134 #endif /* CLASH_DETECTION */
1136 /* Here we have a problem. SAVE_MODIFF is used here to encode
1137 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1138 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1139 modify SAVE_MODIFF to affect one, we may affect the other
1140 as well.
1141 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1142 if SAVE_MODIFF<auto_save_modified that means we risk changing
1143 recent-auto-save-p from t to nil.
1144 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1145 we risk changing recent-auto-save-p from nil to t. */
1146 SAVE_MODIFF = (NILP (flag)
1147 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1148 ? MODIFF
1149 /* Let's try to preserve recent-auto-save-p. */
1150 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1151 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1152 we can either decrease SAVE_MODIFF and auto_save_modified
1153 or increase MODIFF. */
1154 : MODIFF++);
1156 /* Set update_mode_lines only if buffer is displayed in some window.
1157 Packages like jit-lock or lazy-lock preserve a buffer's modified
1158 state by recording/restoring the state around blocks of code.
1159 Setting update_mode_lines makes redisplay consider all windows
1160 (on all frames). Stealth fontification of buffers not displayed
1161 would incur additional redisplay costs if we'd set
1162 update_modes_lines unconditionally.
1164 Ideally, I think there should be another mechanism for fontifying
1165 buffers without "modifying" buffers, or redisplay should be
1166 smarter about updating the `*' in mode lines. --gerd */
1167 XSETBUFFER (buffer, current_buffer);
1168 window = Fget_buffer_window (buffer, Qt);
1169 if (WINDOWP (window))
1171 ++update_mode_lines;
1172 current_buffer->prevent_redisplay_optimizations_p = 1;
1175 return flag;
1178 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1179 Srestore_buffer_modified_p, 1, 1, 0,
1180 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1181 It is not ensured that mode lines will be updated to show the modified
1182 state of the current buffer. Use with care. */)
1183 (flag)
1184 Lisp_Object flag;
1186 #ifdef CLASH_DETECTION
1187 Lisp_Object fn;
1189 /* If buffer becoming modified, lock the file.
1190 If buffer becoming unmodified, unlock the file. */
1192 fn = current_buffer->file_truename;
1193 /* Test buffer-file-name so that binding it to nil is effective. */
1194 if (!NILP (fn) && ! NILP (current_buffer->filename))
1196 int already = SAVE_MODIFF < MODIFF;
1197 if (!already && !NILP (flag))
1198 lock_file (fn);
1199 else if (already && NILP (flag))
1200 unlock_file (fn);
1202 #endif /* CLASH_DETECTION */
1204 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1205 return flag;
1208 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1209 0, 1, 0,
1210 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1211 Each buffer has a tick counter which is incremented each time the
1212 text in that buffer is changed. It wraps around occasionally.
1213 No argument or nil as argument means use current buffer as BUFFER. */)
1214 (buffer)
1215 register Lisp_Object buffer;
1217 register struct buffer *buf;
1218 if (NILP (buffer))
1219 buf = current_buffer;
1220 else
1222 CHECK_BUFFER (buffer);
1223 buf = XBUFFER (buffer);
1226 return make_number (BUF_MODIFF (buf));
1229 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1230 Sbuffer_chars_modified_tick, 0, 1, 0,
1231 doc: /* Return BUFFER's character-change tick counter.
1232 Each buffer has a character-change tick counter, which is set to the
1233 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1234 time text in that buffer is inserted or deleted. By comparing the
1235 values returned by two individual calls of `buffer-chars-modified-tick',
1236 you can tell whether a character change occurred in that buffer in
1237 between these calls. No argument or nil as argument means use current
1238 buffer as BUFFER. */)
1239 (buffer)
1240 register Lisp_Object buffer;
1242 register struct buffer *buf;
1243 if (NILP (buffer))
1244 buf = current_buffer;
1245 else
1247 CHECK_BUFFER (buffer);
1248 buf = XBUFFER (buffer);
1251 return make_number (BUF_CHARS_MODIFF (buf));
1254 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1255 "(list (read-string \"Rename buffer (to new name): \" \
1256 nil 'buffer-name-history (buffer-name (current-buffer))) \
1257 current-prefix-arg)",
1258 doc: /* Change current buffer's name to NEWNAME (a string).
1259 If second arg UNIQUE is nil or omitted, it is an error if a
1260 buffer named NEWNAME already exists.
1261 If UNIQUE is non-nil, come up with a new name using
1262 `generate-new-buffer-name'.
1263 Interactively, you can set UNIQUE with a prefix argument.
1264 We return the name we actually gave the buffer.
1265 This does not change the name of the visited file (if any). */)
1266 (newname, unique)
1267 register Lisp_Object newname, unique;
1269 register Lisp_Object tem, buf;
1271 CHECK_STRING (newname);
1273 if (SCHARS (newname) == 0)
1274 error ("Empty string is invalid as a buffer name");
1276 tem = Fget_buffer (newname);
1277 if (!NILP (tem))
1279 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1280 rename the buffer automatically so you can create another
1281 with the original name. It makes UNIQUE equivalent to
1282 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1283 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1284 return current_buffer->name;
1285 if (!NILP (unique))
1286 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1287 else
1288 error ("Buffer name `%s' is in use", SDATA (newname));
1291 current_buffer->name = newname;
1293 /* Catch redisplay's attention. Unless we do this, the mode lines for
1294 any windows displaying current_buffer will stay unchanged. */
1295 update_mode_lines++;
1297 XSETBUFFER (buf, current_buffer);
1298 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1299 if (NILP (current_buffer->filename)
1300 && !NILP (current_buffer->auto_save_file_name))
1301 call0 (intern ("rename-auto-save-file"));
1302 /* Refetch since that last call may have done GC. */
1303 return current_buffer->name;
1306 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1307 doc: /* Return most recently selected buffer other than BUFFER.
1308 Buffers not visible in windows are preferred to visible buffers,
1309 unless optional second argument VISIBLE-OK is non-nil.
1310 If the optional third argument FRAME is non-nil, use that frame's
1311 buffer list instead of the selected frame's buffer list.
1312 If no other buffer exists, the buffer `*scratch*' is returned.
1313 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1314 (buffer, visible_ok, frame)
1315 register Lisp_Object buffer, visible_ok, frame;
1317 Lisp_Object Fset_buffer_major_mode ();
1318 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1319 notsogood = Qnil;
1321 if (NILP (frame))
1322 frame = selected_frame;
1324 tail = Vbuffer_alist;
1325 pred = frame_buffer_predicate (frame);
1327 /* Consider buffers that have been seen in the selected frame
1328 before other buffers. */
1330 tem = frame_buffer_list (frame);
1331 add_ons = Qnil;
1332 while (CONSP (tem))
1334 if (BUFFERP (XCAR (tem)))
1335 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1336 tem = XCDR (tem);
1338 tail = nconc2 (Fnreverse (add_ons), tail);
1340 for (; CONSP (tail); tail = XCDR (tail))
1342 buf = Fcdr (XCAR (tail));
1343 if (EQ (buf, buffer))
1344 continue;
1345 if (NILP (buf))
1346 continue;
1347 if (NILP (XBUFFER (buf)->name))
1348 continue;
1349 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1350 continue;
1351 /* If the selected frame has a buffer_predicate,
1352 disregard buffers that don't fit the predicate. */
1353 if (!NILP (pred))
1355 tem = call1 (pred, buf);
1356 if (NILP (tem))
1357 continue;
1360 if (NILP (visible_ok))
1361 tem = Fget_buffer_window (buf, Qvisible);
1362 else
1363 tem = Qnil;
1364 if (NILP (tem))
1365 return buf;
1366 if (NILP (notsogood))
1367 notsogood = buf;
1369 if (!NILP (notsogood))
1370 return notsogood;
1371 buf = Fget_buffer (build_string ("*scratch*"));
1372 if (NILP (buf))
1374 buf = Fget_buffer_create (build_string ("*scratch*"));
1375 Fset_buffer_major_mode (buf);
1377 return buf;
1380 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1381 0, 1, "",
1382 doc: /* Start keeping undo information for buffer BUFFER.
1383 No argument or nil as argument means do this for the current buffer. */)
1384 (buffer)
1385 register Lisp_Object buffer;
1387 Lisp_Object real_buffer;
1389 if (NILP (buffer))
1390 XSETBUFFER (real_buffer, current_buffer);
1391 else
1393 real_buffer = Fget_buffer (buffer);
1394 if (NILP (real_buffer))
1395 nsberror (buffer);
1398 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1399 XBUFFER (real_buffer)->undo_list = Qnil;
1401 return Qnil;
1405 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1406 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1407 The buffer being killed will be current while the hook is running.\n\
1408 See `kill-buffer'."
1410 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1411 doc: /* Kill buffer BUFFER-OR-NAME.
1412 The argument may be a buffer or the name of an existing buffer.
1413 Argument nil or omitted means kill the current buffer. Return t if the
1414 buffer is actually killed, nil otherwise.
1416 This function calls `replace-buffer-in-windows' for cleaning up all
1417 windows currently displaying the buffer to be killed. The functions in
1418 `kill-buffer-query-functions' are called with the buffer to be killed as
1419 the current buffer. If any of them returns nil, the buffer is not
1420 killed. The hook `kill-buffer-hook' is run before the buffer is
1421 actually killed. The buffer being killed will be current while the hook
1422 is running.
1424 Any processes that have this buffer as the `process-buffer' are killed
1425 with SIGHUP. */)
1426 (buffer_or_name)
1427 Lisp_Object buffer_or_name;
1429 Lisp_Object buffer;
1430 register struct buffer *b;
1431 register Lisp_Object tem;
1432 register struct Lisp_Marker *m;
1433 struct gcpro gcpro1;
1435 if (NILP (buffer_or_name))
1436 buffer = Fcurrent_buffer ();
1437 else
1438 buffer = Fget_buffer (buffer_or_name);
1439 if (NILP (buffer))
1440 nsberror (buffer_or_name);
1442 b = XBUFFER (buffer);
1444 /* Avoid trouble for buffer already dead. */
1445 if (NILP (b->name))
1446 return Qnil;
1448 tem = get_current_thread ();
1449 if (!EQ (b->owner, Qnil) && !EQ (b->owner, tem))
1450 error ("Buffer locked by another thread");
1452 /* Query if the buffer is still modified. */
1453 if (INTERACTIVE && !NILP (b->filename)
1454 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1456 GCPRO1 (buffer);
1457 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1458 b->name, make_number (0)));
1459 UNGCPRO;
1460 if (NILP (tem))
1461 return Qnil;
1464 /* Run hooks with the buffer to be killed the current buffer. */
1466 int count = SPECPDL_INDEX ();
1467 Lisp_Object arglist[1];
1469 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1470 set_buffer_internal (b);
1472 /* First run the query functions; if any query is answered no,
1473 don't kill the buffer. */
1474 arglist[0] = Qkill_buffer_query_functions;
1475 tem = Frun_hook_with_args_until_failure (1, arglist);
1476 if (NILP (tem))
1477 return unbind_to (count, Qnil);
1479 /* Then run the hooks. */
1480 Frun_hooks (1, &Qkill_buffer_hook);
1481 unbind_to (count, Qnil);
1484 /* We have no more questions to ask. Verify that it is valid
1485 to kill the buffer. This must be done after the questions
1486 since anything can happen within do_yes_or_no_p. */
1488 /* Don't kill the minibuffer now current. */
1489 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1490 return Qnil;
1492 if (NILP (b->name))
1493 return Qnil;
1495 /* When we kill a base buffer, kill all its indirect buffers.
1496 We do it at this stage so nothing terrible happens if they
1497 ask questions or their hooks get errors. */
1498 if (! b->base_buffer)
1500 struct buffer *other;
1502 GCPRO1 (buffer);
1504 for (other = all_buffers; other; other = other->next)
1505 /* all_buffers contains dead buffers too;
1506 don't re-kill them. */
1507 if (other->base_buffer == b && !NILP (other->name))
1509 Lisp_Object buffer;
1510 XSETBUFFER (buffer, other);
1511 Fkill_buffer (buffer);
1514 UNGCPRO;
1517 /* Make this buffer not be current.
1518 In the process, notice if this is the sole visible buffer
1519 and give up if so. */
1520 if (b == current_buffer)
1522 tem = Fother_buffer (buffer, Qnil, Qnil);
1523 Fset_buffer (tem);
1524 if (b == current_buffer)
1525 return Qnil;
1528 /* Notice if the buffer to kill is the sole visible buffer
1529 when we're currently in the mini-buffer, and give up if so. */
1530 XSETBUFFER (tem, current_buffer);
1531 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1533 tem = Fother_buffer (buffer, Qnil, Qnil);
1534 if (EQ (buffer, tem))
1535 return Qnil;
1538 /* Now there is no question: we can kill the buffer. */
1540 #ifdef CLASH_DETECTION
1541 /* Unlock this buffer's file, if it is locked. */
1542 unlock_buffer (b);
1543 #endif /* CLASH_DETECTION */
1545 GCPRO1 (buffer);
1546 kill_buffer_processes (buffer);
1547 UNGCPRO;
1549 /* Killing buffer processes may run sentinels which may
1550 have called kill-buffer. */
1552 if (NILP (b->name))
1553 return Qnil;
1555 clear_charpos_cache (b);
1557 tem = Vinhibit_quit;
1558 Vinhibit_quit = Qt;
1559 replace_buffer_in_all_windows (buffer);
1560 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1561 frames_discard_buffer (buffer);
1562 Vinhibit_quit = tem;
1564 /* Delete any auto-save file, if we saved it in this session.
1565 But not if the buffer is modified. */
1566 if (STRINGP (b->auto_save_file_name)
1567 && BUF_AUTOSAVE_MODIFF (b) != 0
1568 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1569 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1570 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1572 Lisp_Object tem;
1573 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1574 if (! NILP (tem))
1575 internal_delete_file (b->auto_save_file_name);
1578 if (b->base_buffer)
1580 /* Unchain all markers that belong to this indirect buffer.
1581 Don't unchain the markers that belong to the base buffer
1582 or its other indirect buffers. */
1583 for (m = BUF_MARKERS (b); m; )
1585 struct Lisp_Marker *next = m->next;
1586 if (m->buffer == b)
1587 unchain_marker (m);
1588 m = next;
1591 else
1593 /* Unchain all markers of this buffer and its indirect buffers.
1594 and leave them pointing nowhere. */
1595 for (m = BUF_MARKERS (b); m; )
1597 struct Lisp_Marker *next = m->next;
1598 m->buffer = 0;
1599 m->next = NULL;
1600 m = next;
1602 BUF_MARKERS (b) = NULL;
1603 BUF_INTERVALS (b) = NULL_INTERVAL;
1605 /* Perhaps we should explicitly free the interval tree here... */
1608 /* Reset the local variables, so that this buffer's local values
1609 won't be protected from GC. They would be protected
1610 if they happened to remain encached in their symbols.
1611 This gets rid of them for certain. */
1612 swap_out_buffer_local_variables (b);
1613 reset_buffer_local_variables (b, 1);
1615 b->name = Qnil;
1617 BLOCK_INPUT;
1618 if (! b->base_buffer)
1619 free_buffer_text (b);
1621 if (b->newline_cache)
1623 free_region_cache (b->newline_cache);
1624 b->newline_cache = 0;
1626 if (b->width_run_cache)
1628 free_region_cache (b->width_run_cache);
1629 b->width_run_cache = 0;
1631 b->width_table = Qnil;
1632 UNBLOCK_INPUT;
1633 b->undo_list = Qnil;
1635 return Qt;
1638 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1639 we do this each time BUF is selected visibly, the more recently
1640 selected buffers are always closer to the front of the list. This
1641 means that other_buffer is more likely to choose a relevant buffer. */
1643 void
1644 record_buffer (buf)
1645 Lisp_Object buf;
1647 register Lisp_Object link, prev;
1648 Lisp_Object frame;
1649 frame = selected_frame;
1651 prev = Qnil;
1652 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1654 if (EQ (XCDR (XCAR (link)), buf))
1655 break;
1656 prev = link;
1659 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1660 we cannot use Fdelq itself here because it allows quitting. */
1662 if (NILP (prev))
1663 Vbuffer_alist = XCDR (Vbuffer_alist);
1664 else
1665 XSETCDR (prev, XCDR (XCDR (prev)));
1667 XSETCDR (link, Vbuffer_alist);
1668 Vbuffer_alist = link;
1670 /* Effectively do a delq on buried_buffer_list. */
1672 prev = Qnil;
1673 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1674 link = XCDR (link))
1676 if (EQ (XCAR (link), buf))
1678 if (NILP (prev))
1679 XFRAME (frame)->buried_buffer_list = XCDR (link);
1680 else
1681 XSETCDR (prev, XCDR (XCDR (prev)));
1682 break;
1684 prev = link;
1687 /* Now move this buffer to the front of frame_buffer_list also. */
1689 prev = Qnil;
1690 for (link = frame_buffer_list (frame); CONSP (link);
1691 link = XCDR (link))
1693 if (EQ (XCAR (link), buf))
1694 break;
1695 prev = link;
1698 /* Effectively do delq. */
1700 if (CONSP (link))
1702 if (NILP (prev))
1703 set_frame_buffer_list (frame,
1704 XCDR (frame_buffer_list (frame)));
1705 else
1706 XSETCDR (prev, XCDR (XCDR (prev)));
1708 XSETCDR (link, frame_buffer_list (frame));
1709 set_frame_buffer_list (frame, link);
1711 else
1712 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1715 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1716 doc: /* Set an appropriate major mode for BUFFER.
1717 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1718 according to `default-major-mode'.
1719 Use this function before selecting the buffer, since it may need to inspect
1720 the current buffer's major mode. */)
1721 (buffer)
1722 Lisp_Object buffer;
1724 int count;
1725 Lisp_Object function;
1727 CHECK_BUFFER (buffer);
1729 if (STRINGP (XBUFFER (buffer)->name)
1730 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1731 function = find_symbol_value (intern ("initial-major-mode"));
1732 else
1734 function = buffer_defaults.major_mode;
1735 if (NILP (function)
1736 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1737 function = current_buffer->major_mode;
1740 if (NILP (function) || EQ (function, Qfundamental_mode))
1741 return Qnil;
1743 count = SPECPDL_INDEX ();
1745 /* To select a nonfundamental mode,
1746 select the buffer temporarily and then call the mode function. */
1748 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1750 Fset_buffer (buffer);
1751 call0 (function);
1753 return unbind_to (count, Qnil);
1756 /* Switch to buffer BUFFER in the selected window.
1757 If NORECORD is non-nil, don't call record_buffer. */
1759 Lisp_Object
1760 switch_to_buffer_1 (buffer_or_name, norecord)
1761 Lisp_Object buffer_or_name, norecord;
1763 register Lisp_Object buffer;
1765 if (NILP (buffer_or_name))
1766 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1767 else
1769 buffer = Fget_buffer (buffer_or_name);
1770 if (NILP (buffer))
1772 buffer = Fget_buffer_create (buffer_or_name);
1773 Fset_buffer_major_mode (buffer);
1776 Fset_buffer (buffer);
1777 if (NILP (norecord))
1778 record_buffer (buffer);
1780 Fset_window_buffer (EQ (selected_window, minibuf_window)
1781 ? Fnext_window (minibuf_window, Qnil, Qnil)
1782 : selected_window,
1783 buffer, Qnil);
1785 return buffer;
1788 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1789 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1790 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1791 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1792 nil. Return the buffer switched to.
1794 If BUFFER-OR-NAME is a string and does not identify an existing
1795 buffer, create a new buffer with that name. Interactively, if
1796 `confirm-nonexistent-file-or-buffer' is non-nil, request
1797 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1798 nil, switch to buffer returned by `other-buffer'.
1800 Optional second arg NORECORD non-nil means do not put this buffer
1801 at the front of the list of recently selected ones. This
1802 function returns the buffer it switched to as a Lisp object.
1804 If the selected window is the minibuffer window or dedicated to
1805 its buffer, use `pop-to-buffer' for displaying the buffer.
1807 WARNING: This is NOT the way to work on another buffer temporarily
1808 within a Lisp program! Use `set-buffer' instead. That avoids
1809 messing with the window-buffer correspondences. */)
1810 (buffer_or_name, norecord)
1811 Lisp_Object buffer_or_name, norecord;
1813 char *err;
1815 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1817 /* Basically a NOP. Avoid signalling an error in the case where
1818 the selected window is dedicated, or a minibuffer. */
1820 /* But do put this buffer at the front of the buffer list, unless
1821 that has been inhibited. Note that even if BUFFER-OR-NAME is
1822 at the front of the main buffer-list already, we still want to
1823 move it to the front of the frame's buffer list. */
1824 if (NILP (norecord))
1825 record_buffer (buffer_or_name);
1826 return Fset_buffer (buffer_or_name);
1828 else if (EQ (minibuf_window, selected_window)
1829 /* If `dedicated' is neither nil nor t, it means it's
1830 dedicatedness can be overridden by an explicit request
1831 such as a call to switch-to-buffer. */
1832 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1833 /* We can't use the selected window so let `pop-to-buffer' try some
1834 other window. */
1835 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1836 else
1837 return switch_to_buffer_1 (buffer_or_name, norecord);
1840 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1841 doc: /* Return the current buffer as a Lisp object. */)
1844 register Lisp_Object buf;
1845 XSETBUFFER (buf, current_buffer);
1846 return buf;
1849 /* Set the current buffer to B.
1851 We previously set windows_or_buffers_changed here to invalidate
1852 global unchanged information in beg_unchanged and end_unchanged.
1853 This is no longer necessary because we now compute unchanged
1854 information on a buffer-basis. Every action affecting other
1855 windows than the selected one requires a select_window at some
1856 time, and that increments windows_or_buffers_changed. */
1858 void
1859 set_buffer_internal (b)
1860 register struct buffer *b;
1862 if (current_buffer != b)
1863 set_buffer_internal_1 (b);
1866 static void
1867 acquire_buffer (char *end, void *nb)
1869 struct buffer *new_buffer = nb;
1871 /* FIXME this check should be in the caller, for better
1872 single-threaded performance. */
1873 if (other_threads_p ())
1875 /* Let other threads try to acquire a buffer. */
1876 pthread_cond_broadcast (&buffer_cond);
1878 /* If our desired buffer is locked, wait for it. */
1879 while (!EQ (new_buffer->owner, Qnil)
1880 /* We set the owner to Qt to mean it is being killed. */
1881 && !EQ (new_buffer->owner, Qt))
1882 pthread_cond_wait (&buffer_cond, &global_lock);
1886 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1887 This is used by redisplay. */
1889 void
1890 set_buffer_internal_1 (b)
1891 register struct buffer *b;
1893 register struct buffer *old_buf;
1894 register Lisp_Object tail, valcontents;
1895 Lisp_Object tem;
1897 #ifdef USE_MMAP_FOR_BUFFERS
1898 if (b->text->beg == NULL)
1899 enlarge_buffer_text (b, 0);
1900 #endif /* USE_MMAP_FOR_BUFFERS */
1902 if (current_buffer == b)
1903 return;
1905 old_buf = current_buffer;
1906 if (current_buffer)
1907 current_buffer->owner = Qnil;
1908 flush_stack_call_func (acquire_buffer, b);
1909 /* FIXME: if buffer is killed */
1910 b->owner = get_current_thread ();
1911 current_buffer = b;
1912 last_known_column_point = -1; /* invalidate indentation cache */
1914 if (old_buf)
1916 /* Put the undo list back in the base buffer, so that it appears
1917 that an indirect buffer shares the undo list of its base. */
1918 if (old_buf->base_buffer)
1919 old_buf->base_buffer->undo_list = old_buf->undo_list;
1921 /* If the old current buffer has markers to record PT, BEGV and ZV
1922 when it is not current, update them now. */
1923 if (! NILP (old_buf->pt_marker))
1925 Lisp_Object obuf;
1926 XSETBUFFER (obuf, old_buf);
1927 set_marker_both (old_buf->pt_marker, obuf,
1928 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1930 if (! NILP (old_buf->begv_marker))
1932 Lisp_Object obuf;
1933 XSETBUFFER (obuf, old_buf);
1934 set_marker_both (old_buf->begv_marker, obuf,
1935 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1937 if (! NILP (old_buf->zv_marker))
1939 Lisp_Object obuf;
1940 XSETBUFFER (obuf, old_buf);
1941 set_marker_both (old_buf->zv_marker, obuf,
1942 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1946 /* Get the undo list from the base buffer, so that it appears
1947 that an indirect buffer shares the undo list of its base. */
1948 if (b->base_buffer)
1949 b->undo_list = b->base_buffer->undo_list;
1951 /* If the new current buffer has markers to record PT, BEGV and ZV
1952 when it is not current, fetch them now. */
1953 if (! NILP (b->pt_marker))
1955 BUF_PT (b) = marker_position (b->pt_marker);
1956 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1958 if (! NILP (b->begv_marker))
1960 BUF_BEGV (b) = marker_position (b->begv_marker);
1961 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1963 if (! NILP (b->zv_marker))
1965 BUF_ZV (b) = marker_position (b->zv_marker);
1966 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1969 /* Look down buffer's list of local Lisp variables
1970 to find and update any that forward into C variables. */
1972 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1974 if (CONSP (XCAR (tail))
1975 && SYMBOLP (XCAR (XCAR (tail)))
1976 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
1977 (BUFFER_LOCAL_VALUEP (valcontents)))
1978 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1979 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1980 /* Just reference the variable to cause it to become set for
1981 this buffer. */
1982 Fsymbol_value (XCAR (XCAR (tail)));
1985 /* Do the same with any others that were local to the previous buffer */
1987 if (old_buf)
1988 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1990 if (CONSP (tail)
1991 && SYMBOLP (XCAR (XCAR (tail)))
1992 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
1993 (BUFFER_LOCAL_VALUEP (valcontents)))
1994 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1995 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1996 /* Just reference the variable to cause it to become set for
1997 this buffer. */
1998 Fsymbol_value (XCAR (XCAR (tail)));
2002 /* Switch to buffer B temporarily for redisplay purposes.
2003 This avoids certain things that don't need to be done within redisplay. */
2005 void
2006 set_buffer_temp (b)
2007 struct buffer *b;
2009 register struct buffer *old_buf;
2011 if (current_buffer == b)
2012 return;
2014 old_buf = current_buffer;
2015 current_buffer = b;
2017 if (old_buf)
2019 /* If the old current buffer has markers to record PT, BEGV and ZV
2020 when it is not current, update them now. */
2021 if (! NILP (old_buf->pt_marker))
2023 Lisp_Object obuf;
2024 XSETBUFFER (obuf, old_buf);
2025 set_marker_both (old_buf->pt_marker, obuf,
2026 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
2028 if (! NILP (old_buf->begv_marker))
2030 Lisp_Object obuf;
2031 XSETBUFFER (obuf, old_buf);
2032 set_marker_both (old_buf->begv_marker, obuf,
2033 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
2035 if (! NILP (old_buf->zv_marker))
2037 Lisp_Object obuf;
2038 XSETBUFFER (obuf, old_buf);
2039 set_marker_both (old_buf->zv_marker, obuf,
2040 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
2044 /* If the new current buffer has markers to record PT, BEGV and ZV
2045 when it is not current, fetch them now. */
2046 if (! NILP (b->pt_marker))
2048 BUF_PT (b) = marker_position (b->pt_marker);
2049 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
2051 if (! NILP (b->begv_marker))
2053 BUF_BEGV (b) = marker_position (b->begv_marker);
2054 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2056 if (! NILP (b->zv_marker))
2058 BUF_ZV (b) = marker_position (b->zv_marker);
2059 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2063 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2064 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
2065 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
2066 also `save-excursion' when you want to make a buffer current
2067 temporarily. This function does not display the buffer, so its effect
2068 ends when the current command terminates. Use `switch-to-buffer' or
2069 `pop-to-buffer' to switch buffers permanently. */)
2070 (buffer_or_name)
2071 register Lisp_Object buffer_or_name;
2073 register Lisp_Object buffer;
2074 buffer = Fget_buffer (buffer_or_name);
2075 if (NILP (buffer))
2076 nsberror (buffer_or_name);
2077 if (NILP (XBUFFER (buffer)->name))
2078 error ("Selecting deleted buffer");
2079 set_buffer_internal (XBUFFER (buffer));
2080 return buffer;
2083 /* Set the current buffer to BUFFER provided it is alive. */
2085 Lisp_Object
2086 set_buffer_if_live (buffer)
2087 Lisp_Object buffer;
2089 if (! NILP (XBUFFER (buffer)->name))
2090 Fset_buffer (buffer);
2091 return Qnil;
2094 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2095 Sbarf_if_buffer_read_only, 0, 0, 0,
2096 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2099 if (!NILP (current_buffer->read_only)
2100 && NILP (Vinhibit_read_only))
2101 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2102 return Qnil;
2105 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2106 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
2107 There it is the least likely candidate for `other-buffer' to return;
2108 thus, the least likely buffer for \\[switch-to-buffer] to select by
2109 default.
2111 The argument may be a buffer name or an actual buffer object. If
2112 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2113 from the selected window if it is displayed there. If the selected
2114 window is dedicated to its buffer, delete that window if there are other
2115 windows on the same frame. If the selected window is the only window on
2116 its frame, iconify that frame. */)
2117 (buffer_or_name)
2118 register Lisp_Object buffer_or_name;
2120 Lisp_Object buffer;
2122 /* Figure out what buffer we're going to bury. */
2123 if (NILP (buffer_or_name))
2125 Lisp_Object tem;
2126 XSETBUFFER (buffer, current_buffer);
2128 tem = Fwindow_buffer (selected_window);
2129 /* If we're burying the current buffer, unshow it. */
2130 if (EQ (buffer, tem))
2132 if (NILP (Fwindow_dedicated_p (selected_window)))
2133 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2134 else if (NILP (XWINDOW (selected_window)->parent))
2135 Ficonify_frame (Fwindow_frame (selected_window));
2136 else
2137 Fdelete_window (selected_window);
2140 else
2142 buffer = Fget_buffer (buffer_or_name);
2143 if (NILP (buffer))
2144 nsberror (buffer_or_name);
2147 /* Move buffer to the end of the buffer list. Do nothing if the
2148 buffer is killed. */
2149 if (!NILP (XBUFFER (buffer)->name))
2151 Lisp_Object aelt, link;
2153 aelt = Frassq (buffer, Vbuffer_alist);
2154 link = Fmemq (aelt, Vbuffer_alist);
2155 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2156 XSETCDR (link, Qnil);
2157 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2159 XFRAME (selected_frame)->buffer_list
2160 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2161 XFRAME (selected_frame)->buried_buffer_list
2162 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2165 return Qnil;
2168 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2169 doc: /* Delete the entire contents of the current buffer.
2170 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2171 so the buffer is truly empty after this. */)
2174 Fwiden ();
2176 del_range (BEG, Z);
2178 current_buffer->last_window_start = 1;
2179 /* Prevent warnings, or suspension of auto saving, that would happen
2180 if future size is less than past size. Use of erase-buffer
2181 implies that the future text is not really related to the past text. */
2182 XSETFASTINT (current_buffer->save_length, 0);
2183 return Qnil;
2186 void
2187 validate_region (b, e)
2188 register Lisp_Object *b, *e;
2190 CHECK_NUMBER_COERCE_MARKER (*b);
2191 CHECK_NUMBER_COERCE_MARKER (*e);
2193 if (XINT (*b) > XINT (*e))
2195 Lisp_Object tem;
2196 tem = *b; *b = *e; *e = tem;
2199 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2200 && XINT (*e) <= ZV))
2201 args_out_of_range (*b, *e);
2204 /* Advance BYTE_POS up to a character boundary
2205 and return the adjusted position. */
2207 static int
2208 advance_to_char_boundary (byte_pos)
2209 int byte_pos;
2211 int c;
2213 if (byte_pos == BEG)
2214 /* Beginning of buffer is always a character boundary. */
2215 return BEG;
2217 c = FETCH_BYTE (byte_pos);
2218 if (! CHAR_HEAD_P (c))
2220 /* We should advance BYTE_POS only when C is a constituent of a
2221 multibyte sequence. */
2222 int orig_byte_pos = byte_pos;
2226 byte_pos--;
2227 c = FETCH_BYTE (byte_pos);
2229 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2230 INC_POS (byte_pos);
2231 if (byte_pos < orig_byte_pos)
2232 byte_pos = orig_byte_pos;
2233 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2234 surely advance to the correct character boundary. If C is
2235 not, BYTE_POS was unchanged. */
2238 return byte_pos;
2241 #ifdef REL_ALLOC
2242 extern void r_alloc_reset_variable P_ ((POINTER_TYPE *, POINTER_TYPE *));
2243 #endif /* REL_ALLOC */
2245 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2246 1, 1, 0,
2247 doc: /* Swap the text between current buffer and BUFFER. */)
2248 (buffer)
2249 Lisp_Object buffer;
2251 struct buffer *other_buffer;
2252 CHECK_BUFFER (buffer);
2253 other_buffer = XBUFFER (buffer);
2255 if (NILP (other_buffer->name))
2256 error ("Cannot swap a dead buffer's text");
2258 /* Actually, it probably works just fine.
2259 * if (other_buffer == current_buffer)
2260 * error ("Cannot swap a buffer's text with itself"); */
2262 /* Actually, this may be workable as well, tho probably only if they're
2263 *both* indirect. */
2264 if (other_buffer->base_buffer
2265 || current_buffer->base_buffer)
2266 error ("Cannot swap indirect buffers's text");
2268 { /* This is probably harder to make work. */
2269 struct buffer *other;
2270 for (other = all_buffers; other; other = other->next)
2271 if (other->base_buffer == other_buffer
2272 || other->base_buffer == current_buffer)
2273 error ("One of the buffers to swap has indirect buffers");
2276 #define swapfield(field, type) \
2277 do { \
2278 type tmp##field = other_buffer->field; \
2279 other_buffer->field = current_buffer->field; \
2280 current_buffer->field = tmp##field; \
2281 } while (0)
2283 swapfield (own_text, struct buffer_text);
2284 eassert (current_buffer->text == &current_buffer->own_text);
2285 eassert (other_buffer->text == &other_buffer->own_text);
2286 #ifdef REL_ALLOC
2287 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2288 (POINTER_TYPE **) &other_buffer->own_text.beg);
2289 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2290 (POINTER_TYPE **) &current_buffer->own_text.beg);
2291 #endif /* REL_ALLOC */
2293 swapfield (pt, EMACS_INT);
2294 swapfield (pt_byte, EMACS_INT);
2295 swapfield (begv, EMACS_INT);
2296 swapfield (begv_byte, EMACS_INT);
2297 swapfield (zv, EMACS_INT);
2298 swapfield (zv_byte, EMACS_INT);
2299 eassert (!current_buffer->base_buffer);
2300 eassert (!other_buffer->base_buffer);
2301 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2302 swapfield (newline_cache, struct region_cache *);
2303 swapfield (width_run_cache, struct region_cache *);
2304 current_buffer->prevent_redisplay_optimizations_p = 1;
2305 other_buffer->prevent_redisplay_optimizations_p = 1;
2306 swapfield (overlays_before, struct Lisp_Overlay *);
2307 swapfield (overlays_after, struct Lisp_Overlay *);
2308 swapfield (overlay_center, EMACS_INT);
2309 swapfield (undo_list, Lisp_Object);
2310 swapfield (mark, Lisp_Object);
2311 swapfield (enable_multibyte_characters, Lisp_Object);
2312 /* FIXME: Not sure what we should do with these *_marker fields.
2313 Hopefully they're just nil anyway. */
2314 swapfield (pt_marker, Lisp_Object);
2315 swapfield (begv_marker, Lisp_Object);
2316 swapfield (zv_marker, Lisp_Object);
2317 current_buffer->point_before_scroll = Qnil;
2318 other_buffer->point_before_scroll = Qnil;
2320 current_buffer->text->modiff++; other_buffer->text->modiff++;
2321 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2322 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2323 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2324 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2325 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2326 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2328 struct Lisp_Marker *m;
2329 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2330 if (m->buffer == other_buffer)
2331 m->buffer = current_buffer;
2332 else
2333 /* Since there's no indirect buffer in sight, markers on
2334 BUF_MARKERS(buf) should either be for `buf' or dead. */
2335 eassert (!m->buffer);
2336 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2337 if (m->buffer == current_buffer)
2338 m->buffer = other_buffer;
2339 else
2340 /* Since there's no indirect buffer in sight, markers on
2341 BUF_MARKERS(buf) should either be for `buf' or dead. */
2342 eassert (!m->buffer);
2344 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2345 So since we just swapped the markers between the two buffers, we need
2346 to undo the effect of this swap for window markers. */
2347 Lisp_Object w = Fselected_window (), ws = Qnil;
2348 Lisp_Object buf1, buf2;
2349 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2351 while (NILP (Fmemq (w, ws)))
2353 ws = Fcons (w, ws);
2354 if (MARKERP (XWINDOW (w)->pointm)
2355 && (EQ (XWINDOW (w)->buffer, buf1)
2356 || EQ (XWINDOW (w)->buffer, buf2)))
2357 Fset_marker (XWINDOW (w)->pointm,
2358 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2359 XWINDOW (w)->buffer);
2360 w = Fnext_window (w, Qt, Qt);
2364 if (current_buffer->text->intervals)
2365 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2366 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2367 if (other_buffer->text->intervals)
2368 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2369 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2371 return Qnil;
2374 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2375 1, 1, 0,
2376 doc: /* Set the multibyte flag of the current buffer to FLAG.
2377 If FLAG is t, this makes the buffer a multibyte buffer.
2378 If FLAG is nil, this makes the buffer a single-byte buffer.
2379 In these cases, the buffer contents remain unchanged as a sequence of
2380 bytes but the contents viewed as characters do change.
2381 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2382 all eight-bit bytes to eight-bit characters.
2383 If the multibyte flag was really changed, undo information of the
2384 current buffer is cleared. */)
2385 (flag)
2386 Lisp_Object flag;
2388 struct Lisp_Marker *tail, *markers;
2389 struct buffer *other;
2390 int begv, zv;
2391 int narrowed = (BEG != BEGV || Z != ZV);
2392 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2393 Lisp_Object old_undo = current_buffer->undo_list;
2394 struct gcpro gcpro1;
2396 if (current_buffer->base_buffer)
2397 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2399 /* Do nothing if nothing actually changes. */
2400 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2401 return flag;
2403 GCPRO1 (old_undo);
2405 /* Don't record these buffer changes. We will put a special undo entry
2406 instead. */
2407 current_buffer->undo_list = Qt;
2409 /* If the cached position is for this buffer, clear it out. */
2410 clear_charpos_cache (current_buffer);
2412 if (NILP (flag))
2413 begv = BEGV_BYTE, zv = ZV_BYTE;
2414 else
2415 begv = BEGV, zv = ZV;
2417 if (narrowed)
2418 Fwiden ();
2420 if (NILP (flag))
2422 int pos, stop;
2423 unsigned char *p;
2425 /* Do this first, so it can use CHAR_TO_BYTE
2426 to calculate the old correspondences. */
2427 set_intervals_multibyte (0);
2429 current_buffer->enable_multibyte_characters = Qnil;
2431 Z = Z_BYTE;
2432 BEGV = BEGV_BYTE;
2433 ZV = ZV_BYTE;
2434 GPT = GPT_BYTE;
2435 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2438 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2439 tail->charpos = tail->bytepos;
2441 /* Convert multibyte form of 8-bit characters to unibyte. */
2442 pos = BEG;
2443 stop = GPT;
2444 p = BEG_ADDR;
2445 while (1)
2447 int c, bytes;
2449 if (pos == stop)
2451 if (pos == Z)
2452 break;
2453 p = GAP_END_ADDR;
2454 stop = Z;
2456 if (ASCII_BYTE_P (*p))
2457 p++, pos++;
2458 else if (CHAR_BYTE8_HEAD_P (*p))
2460 c = STRING_CHAR_AND_LENGTH (p, bytes);
2461 /* Delete all bytes for this 8-bit character but the
2462 last one, and change the last one to the charcter
2463 code. */
2464 bytes--;
2465 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2466 p = GAP_END_ADDR;
2467 *p++ = c;
2468 pos++;
2469 if (begv > pos)
2470 begv -= bytes;
2471 if (zv > pos)
2472 zv -= bytes;
2473 stop = Z;
2475 else
2477 bytes = BYTES_BY_CHAR_HEAD (*p);
2478 p += bytes, pos += bytes;
2481 if (narrowed)
2482 Fnarrow_to_region (make_number (begv), make_number (zv));
2484 else
2486 int pt = PT;
2487 int pos, stop;
2488 unsigned char *p, *pend;
2490 /* Be sure not to have a multibyte sequence striding over the GAP.
2491 Ex: We change this: "...abc\302 _GAP_ \241def..."
2492 to: "...abc _GAP_ \302\241def..." */
2494 if (EQ (flag, Qt)
2495 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2496 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2498 unsigned char *p = GPT_ADDR - 1;
2500 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2501 if (BASE_LEADING_CODE_P (*p))
2503 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2505 move_gap_both (new_gpt, new_gpt);
2509 /* Make the buffer contents valid as multibyte by converting
2510 8-bit characters to multibyte form. */
2511 pos = BEG;
2512 stop = GPT;
2513 p = BEG_ADDR;
2514 pend = GPT_ADDR;
2515 while (1)
2517 int bytes;
2519 if (pos == stop)
2521 if (pos == Z)
2522 break;
2523 p = GAP_END_ADDR;
2524 pend = Z_ADDR;
2525 stop = Z;
2528 if (ASCII_BYTE_P (*p))
2529 p++, pos++;
2530 else if (EQ (flag, Qt)
2531 && ! CHAR_BYTE8_HEAD_P (*p)
2532 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2533 p += bytes, pos += bytes;
2534 else
2536 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2537 int c;
2539 c = BYTE8_TO_CHAR (*p);
2540 bytes = CHAR_STRING (c, tmp);
2541 *p = tmp[0];
2542 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2543 bytes--;
2544 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2545 /* Now the gap is after the just inserted data. */
2546 pos = GPT;
2547 p = GAP_END_ADDR;
2548 if (pos <= begv)
2549 begv += bytes;
2550 if (pos <= zv)
2551 zv += bytes;
2552 if (pos <= pt)
2553 pt += bytes;
2554 pend = Z_ADDR;
2555 stop = Z;
2559 if (pt != PT)
2560 TEMP_SET_PT (pt);
2562 if (narrowed)
2563 Fnarrow_to_region (make_number (begv), make_number (zv));
2565 /* Do this first, so that chars_in_text asks the right question.
2566 set_intervals_multibyte needs it too. */
2567 current_buffer->enable_multibyte_characters = Qt;
2569 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2570 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2572 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2574 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2575 if (BEGV_BYTE > GPT_BYTE)
2576 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2577 else
2578 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2580 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2581 if (ZV_BYTE > GPT_BYTE)
2582 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2583 else
2584 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2587 int pt_byte = advance_to_char_boundary (PT_BYTE);
2588 int pt;
2590 if (pt_byte > GPT_BYTE)
2591 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2592 else
2593 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2594 TEMP_SET_PT_BOTH (pt, pt_byte);
2597 tail = markers = BUF_MARKERS (current_buffer);
2599 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2600 getting confused by the markers that have not yet been updated.
2601 It is also a signal that it should never create a marker. */
2602 BUF_MARKERS (current_buffer) = NULL;
2604 for (; tail; tail = tail->next)
2606 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2607 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2610 /* Make sure no markers were put on the chain
2611 while the chain value was incorrect. */
2612 if (BUF_MARKERS (current_buffer))
2613 abort ();
2615 BUF_MARKERS (current_buffer) = markers;
2617 /* Do this last, so it can calculate the new correspondences
2618 between chars and bytes. */
2619 set_intervals_multibyte (1);
2622 if (!EQ (old_undo, Qt))
2624 /* Represent all the above changes by a special undo entry. */
2625 extern Lisp_Object Qapply;
2626 current_buffer->undo_list = Fcons (list3 (Qapply,
2627 intern ("set-buffer-multibyte"),
2628 NILP (flag) ? Qt : Qnil),
2629 old_undo);
2632 UNGCPRO;
2634 /* Changing the multibyteness of a buffer means that all windows
2635 showing that buffer must be updated thoroughly. */
2636 current_buffer->prevent_redisplay_optimizations_p = 1;
2637 ++windows_or_buffers_changed;
2639 /* Copy this buffer's new multibyte status
2640 into all of its indirect buffers. */
2641 for (other = all_buffers; other; other = other->next)
2642 if (other->base_buffer == current_buffer && !NILP (other->name))
2644 other->enable_multibyte_characters
2645 = current_buffer->enable_multibyte_characters;
2646 other->prevent_redisplay_optimizations_p = 1;
2649 /* Restore the modifiedness of the buffer. */
2650 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2651 Fset_buffer_modified_p (Qnil);
2653 #ifdef subprocesses
2654 /* Update coding systems of this buffer's process (if any). */
2656 Lisp_Object process;
2658 process = Fget_buffer_process (Fcurrent_buffer ());
2659 if (PROCESSP (process))
2660 setup_process_coding_systems (process);
2662 #endif /* subprocesses */
2664 return flag;
2667 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2668 0, 0, 0,
2669 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2670 Most local variable bindings are eliminated so that the default values
2671 become effective once more. Also, the syntax table is set from
2672 `standard-syntax-table', the local keymap is set to nil,
2673 and the abbrev table from `fundamental-mode-abbrev-table'.
2674 This function also forces redisplay of the mode line.
2676 Every function to select a new major mode starts by
2677 calling this function.
2679 As a special exception, local variables whose names have
2680 a non-nil `permanent-local' property are not eliminated by this function.
2682 The first thing this function does is run
2683 the normal hook `change-major-mode-hook'. */)
2686 if (!NILP (Vrun_hooks))
2687 call1 (Vrun_hooks, Qchange_major_mode_hook);
2689 /* Make sure none of the bindings in local_var_alist
2690 remain swapped in, in their symbols. */
2692 swap_out_buffer_local_variables (current_buffer);
2694 /* Actually eliminate all local bindings of this buffer. */
2696 reset_buffer_local_variables (current_buffer, 0);
2698 /* Force mode-line redisplay. Useful here because all major mode
2699 commands call this function. */
2700 update_mode_lines++;
2702 return Qnil;
2705 /* Make sure no local variables remain set up with buffer B
2706 for their current values. */
2708 static void
2709 swap_out_buffer_local_variables (b)
2710 struct buffer *b;
2712 Lisp_Object oalist, alist, sym, buffer;
2714 XSETBUFFER (buffer, b);
2715 oalist = b->local_var_alist;
2717 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2719 if (CONSP (XCAR (alist))
2720 && (sym = XCAR (XCAR (alist)), SYMBOLP (sym))
2721 /* Need not do anything if some other buffer's binding is
2722 now encached. */
2723 && EQ (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer,
2724 buffer))
2726 /* Symbol is set up for this buffer's old local value:
2727 swap it out! */
2728 swap_in_global_binding (sym);
2733 /* Find all the overlays in the current buffer that contain position POS.
2734 Return the number found, and store them in a vector in *VEC_PTR.
2735 Store in *LEN_PTR the size allocated for the vector.
2736 Store in *NEXT_PTR the next position after POS where an overlay starts,
2737 or ZV if there are no more overlays between POS and ZV.
2738 Store in *PREV_PTR the previous position before POS where an overlay ends,
2739 or where an overlay starts which ends at or after POS;
2740 or BEGV if there are no such overlays from BEGV to POS.
2741 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2743 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2744 when this function is called.
2746 If EXTEND is non-zero, we make the vector bigger if necessary.
2747 If EXTEND is zero, we never extend the vector,
2748 and we store only as many overlays as will fit.
2749 But we still return the total number of overlays.
2751 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2752 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2753 default (BEGV or ZV). */
2756 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2757 EMACS_INT pos;
2758 int extend;
2759 Lisp_Object **vec_ptr;
2760 int *len_ptr;
2761 EMACS_INT *next_ptr;
2762 EMACS_INT *prev_ptr;
2763 int change_req;
2765 Lisp_Object overlay, start, end;
2766 struct Lisp_Overlay *tail;
2767 int idx = 0;
2768 int len = *len_ptr;
2769 Lisp_Object *vec = *vec_ptr;
2770 int next = ZV;
2771 int prev = BEGV;
2772 int inhibit_storing = 0;
2774 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2776 int startpos, endpos;
2778 XSETMISC (overlay, tail);
2780 start = OVERLAY_START (overlay);
2781 end = OVERLAY_END (overlay);
2782 endpos = OVERLAY_POSITION (end);
2783 if (endpos < pos)
2785 if (prev < endpos)
2786 prev = endpos;
2787 break;
2789 startpos = OVERLAY_POSITION (start);
2790 /* This one ends at or after POS
2791 so its start counts for PREV_PTR if it's before POS. */
2792 if (prev < startpos && startpos < pos)
2793 prev = startpos;
2794 if (endpos == pos)
2795 continue;
2796 if (startpos <= pos)
2798 if (idx == len)
2800 /* The supplied vector is full.
2801 Either make it bigger, or don't store any more in it. */
2802 if (extend)
2804 /* Make it work with an initial len == 0. */
2805 len *= 2;
2806 if (len == 0)
2807 len = 4;
2808 *len_ptr = len;
2809 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2810 *vec_ptr = vec;
2812 else
2813 inhibit_storing = 1;
2816 if (!inhibit_storing)
2817 vec[idx] = overlay;
2818 /* Keep counting overlays even if we can't return them all. */
2819 idx++;
2821 else if (startpos < next)
2822 next = startpos;
2825 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2827 int startpos, endpos;
2829 XSETMISC (overlay, tail);
2831 start = OVERLAY_START (overlay);
2832 end = OVERLAY_END (overlay);
2833 startpos = OVERLAY_POSITION (start);
2834 if (pos < startpos)
2836 if (startpos < next)
2837 next = startpos;
2838 break;
2840 endpos = OVERLAY_POSITION (end);
2841 if (pos < endpos)
2843 if (idx == len)
2845 if (extend)
2847 /* Make it work with an initial len == 0. */
2848 len *= 2;
2849 if (len == 0)
2850 len = 4;
2851 *len_ptr = len;
2852 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2853 *vec_ptr = vec;
2855 else
2856 inhibit_storing = 1;
2859 if (!inhibit_storing)
2860 vec[idx] = overlay;
2861 idx++;
2863 if (startpos < pos && startpos > prev)
2864 prev = startpos;
2866 else if (endpos < pos && endpos > prev)
2867 prev = endpos;
2868 else if (endpos == pos && startpos > prev
2869 && (!change_req || startpos < pos))
2870 prev = startpos;
2873 if (next_ptr)
2874 *next_ptr = next;
2875 if (prev_ptr)
2876 *prev_ptr = prev;
2877 return idx;
2880 /* Find all the overlays in the current buffer that overlap the range
2881 BEG-END, or are empty at BEG, or are empty at END provided END
2882 denotes the position at the end of the current buffer.
2884 Return the number found, and store them in a vector in *VEC_PTR.
2885 Store in *LEN_PTR the size allocated for the vector.
2886 Store in *NEXT_PTR the next position after POS where an overlay starts,
2887 or ZV if there are no more overlays.
2888 Store in *PREV_PTR the previous position before POS where an overlay ends,
2889 or BEGV if there are no previous overlays.
2890 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2892 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2893 when this function is called.
2895 If EXTEND is non-zero, we make the vector bigger if necessary.
2896 If EXTEND is zero, we never extend the vector,
2897 and we store only as many overlays as will fit.
2898 But we still return the total number of overlays. */
2900 static int
2901 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2902 int beg, end;
2903 int extend;
2904 Lisp_Object **vec_ptr;
2905 int *len_ptr;
2906 int *next_ptr;
2907 int *prev_ptr;
2909 Lisp_Object overlay, ostart, oend;
2910 struct Lisp_Overlay *tail;
2911 int idx = 0;
2912 int len = *len_ptr;
2913 Lisp_Object *vec = *vec_ptr;
2914 int next = ZV;
2915 int prev = BEGV;
2916 int inhibit_storing = 0;
2917 int end_is_Z = end == Z;
2919 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2921 int startpos, endpos;
2923 XSETMISC (overlay, tail);
2925 ostart = OVERLAY_START (overlay);
2926 oend = OVERLAY_END (overlay);
2927 endpos = OVERLAY_POSITION (oend);
2928 if (endpos < beg)
2930 if (prev < endpos)
2931 prev = endpos;
2932 break;
2934 startpos = OVERLAY_POSITION (ostart);
2935 /* Count an interval if it overlaps the range, is empty at the
2936 start of the range, or is empty at END provided END denotes the
2937 end of the buffer. */
2938 if ((beg < endpos && startpos < end)
2939 || (startpos == endpos
2940 && (beg == endpos || (end_is_Z && endpos == end))))
2942 if (idx == len)
2944 /* The supplied vector is full.
2945 Either make it bigger, or don't store any more in it. */
2946 if (extend)
2948 /* Make it work with an initial len == 0. */
2949 len *= 2;
2950 if (len == 0)
2951 len = 4;
2952 *len_ptr = len;
2953 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2954 *vec_ptr = vec;
2956 else
2957 inhibit_storing = 1;
2960 if (!inhibit_storing)
2961 vec[idx] = overlay;
2962 /* Keep counting overlays even if we can't return them all. */
2963 idx++;
2965 else if (startpos < next)
2966 next = startpos;
2969 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2971 int startpos, endpos;
2973 XSETMISC (overlay, tail);
2975 ostart = OVERLAY_START (overlay);
2976 oend = OVERLAY_END (overlay);
2977 startpos = OVERLAY_POSITION (ostart);
2978 if (end < startpos)
2980 if (startpos < next)
2981 next = startpos;
2982 break;
2984 endpos = OVERLAY_POSITION (oend);
2985 /* Count an interval if it overlaps the range, is empty at the
2986 start of the range, or is empty at END provided END denotes the
2987 end of the buffer. */
2988 if ((beg < endpos && startpos < end)
2989 || (startpos == endpos
2990 && (beg == endpos || (end_is_Z && endpos == end))))
2992 if (idx == len)
2994 if (extend)
2996 /* Make it work with an initial len == 0. */
2997 len *= 2;
2998 if (len == 0)
2999 len = 4;
3000 *len_ptr = len;
3001 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
3002 *vec_ptr = vec;
3004 else
3005 inhibit_storing = 1;
3008 if (!inhibit_storing)
3009 vec[idx] = overlay;
3010 idx++;
3012 else if (endpos < beg && endpos > prev)
3013 prev = endpos;
3016 if (next_ptr)
3017 *next_ptr = next;
3018 if (prev_ptr)
3019 *prev_ptr = prev;
3020 return idx;
3024 /* Return non-zero if there exists an overlay with a non-nil
3025 `mouse-face' property overlapping OVERLAY. */
3028 mouse_face_overlay_overlaps (overlay)
3029 Lisp_Object overlay;
3031 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
3032 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
3033 int n, i, size;
3034 Lisp_Object *v, tem;
3036 size = 10;
3037 v = (Lisp_Object *) alloca (size * sizeof *v);
3038 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
3039 if (n > size)
3041 v = (Lisp_Object *) alloca (n * sizeof *v);
3042 overlays_in (start, end, 0, &v, &n, NULL, NULL);
3045 for (i = 0; i < n; ++i)
3046 if (!EQ (v[i], overlay)
3047 && (tem = Foverlay_get (overlay, Qmouse_face),
3048 !NILP (tem)))
3049 break;
3051 return i < n;
3056 /* Fast function to just test if we're at an overlay boundary. */
3058 overlay_touches_p (pos)
3059 int pos;
3061 Lisp_Object overlay;
3062 struct Lisp_Overlay *tail;
3064 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
3066 int endpos;
3068 XSETMISC (overlay ,tail);
3069 if (!OVERLAYP (overlay))
3070 abort ();
3072 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3073 if (endpos < pos)
3074 break;
3075 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3076 return 1;
3079 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
3081 int startpos;
3083 XSETMISC (overlay, tail);
3084 if (!OVERLAYP (overlay))
3085 abort ();
3087 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3088 if (pos < startpos)
3089 break;
3090 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3091 return 1;
3093 return 0;
3096 struct sortvec
3098 Lisp_Object overlay;
3099 int beg, end;
3100 int priority;
3103 static int
3104 compare_overlays (v1, v2)
3105 const void *v1, *v2;
3107 const struct sortvec *s1 = (const struct sortvec *) v1;
3108 const struct sortvec *s2 = (const struct sortvec *) v2;
3109 if (s1->priority != s2->priority)
3110 return s1->priority - s2->priority;
3111 if (s1->beg != s2->beg)
3112 return s1->beg - s2->beg;
3113 if (s1->end != s2->end)
3114 return s2->end - s1->end;
3115 return 0;
3118 /* Sort an array of overlays by priority. The array is modified in place.
3119 The return value is the new size; this may be smaller than the original
3120 size if some of the overlays were invalid or were window-specific. */
3122 sort_overlays (overlay_vec, noverlays, w)
3123 Lisp_Object *overlay_vec;
3124 int noverlays;
3125 struct window *w;
3127 int i, j;
3128 struct sortvec *sortvec;
3129 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3131 /* Put the valid and relevant overlays into sortvec. */
3133 for (i = 0, j = 0; i < noverlays; i++)
3135 Lisp_Object tem;
3136 Lisp_Object overlay;
3138 overlay = overlay_vec[i];
3139 if (OVERLAY_VALID (overlay)
3140 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3141 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3143 /* If we're interested in a specific window, then ignore
3144 overlays that are limited to some other window. */
3145 if (w)
3147 Lisp_Object window;
3149 window = Foverlay_get (overlay, Qwindow);
3150 if (WINDOWP (window) && XWINDOW (window) != w)
3151 continue;
3154 /* This overlay is good and counts: put it into sortvec. */
3155 sortvec[j].overlay = overlay;
3156 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3157 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3158 tem = Foverlay_get (overlay, Qpriority);
3159 if (INTEGERP (tem))
3160 sortvec[j].priority = XINT (tem);
3161 else
3162 sortvec[j].priority = 0;
3163 j++;
3166 noverlays = j;
3168 /* Sort the overlays into the proper order: increasing priority. */
3170 if (noverlays > 1)
3171 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3173 for (i = 0; i < noverlays; i++)
3174 overlay_vec[i] = sortvec[i].overlay;
3175 return (noverlays);
3178 struct sortstr
3180 Lisp_Object string, string2;
3181 int size;
3182 int priority;
3185 struct sortstrlist
3187 struct sortstr *buf; /* An array that expands as needed; never freed. */
3188 int size; /* Allocated length of that array. */
3189 int used; /* How much of the array is currently in use. */
3190 int bytes; /* Total length of the strings in buf. */
3193 /* Buffers for storing information about the overlays touching a given
3194 position. These could be automatic variables in overlay_strings, but
3195 it's more efficient to hold onto the memory instead of repeatedly
3196 allocating and freeing it. */
3197 static struct sortstrlist overlay_heads, overlay_tails;
3198 static unsigned char *overlay_str_buf;
3200 /* Allocated length of overlay_str_buf. */
3201 static int overlay_str_len;
3203 /* A comparison function suitable for passing to qsort. */
3204 static int
3205 cmp_for_strings (as1, as2)
3206 char *as1, *as2;
3208 struct sortstr *s1 = (struct sortstr *)as1;
3209 struct sortstr *s2 = (struct sortstr *)as2;
3210 if (s1->size != s2->size)
3211 return s2->size - s1->size;
3212 if (s1->priority != s2->priority)
3213 return s1->priority - s2->priority;
3214 return 0;
3217 static void
3218 record_overlay_string (ssl, str, str2, pri, size)
3219 struct sortstrlist *ssl;
3220 Lisp_Object str, str2, pri;
3221 int size;
3223 int nbytes;
3225 if (ssl->used == ssl->size)
3227 if (ssl->buf)
3228 ssl->size *= 2;
3229 else
3230 ssl->size = 5;
3231 ssl->buf = ((struct sortstr *)
3232 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3234 ssl->buf[ssl->used].string = str;
3235 ssl->buf[ssl->used].string2 = str2;
3236 ssl->buf[ssl->used].size = size;
3237 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3238 ssl->used++;
3240 if (NILP (current_buffer->enable_multibyte_characters))
3241 nbytes = SCHARS (str);
3242 else if (! STRING_MULTIBYTE (str))
3243 nbytes = count_size_as_multibyte (SDATA (str),
3244 SBYTES (str));
3245 else
3246 nbytes = SBYTES (str);
3248 ssl->bytes += nbytes;
3250 if (STRINGP (str2))
3252 if (NILP (current_buffer->enable_multibyte_characters))
3253 nbytes = SCHARS (str2);
3254 else if (! STRING_MULTIBYTE (str2))
3255 nbytes = count_size_as_multibyte (SDATA (str2),
3256 SBYTES (str2));
3257 else
3258 nbytes = SBYTES (str2);
3260 ssl->bytes += nbytes;
3264 /* Return the concatenation of the strings associated with overlays that
3265 begin or end at POS, ignoring overlays that are specific to a window
3266 other than W. The strings are concatenated in the appropriate order:
3267 shorter overlays nest inside longer ones, and higher priority inside
3268 lower. Normally all of the after-strings come first, but zero-sized
3269 overlays have their after-strings ride along with the before-strings
3270 because it would look strange to print them inside-out.
3272 Returns the string length, and stores the contents indirectly through
3273 PSTR, if that variable is non-null. The string may be overwritten by
3274 subsequent calls. */
3277 overlay_strings (pos, w, pstr)
3278 EMACS_INT pos;
3279 struct window *w;
3280 unsigned char **pstr;
3282 Lisp_Object overlay, window, str;
3283 struct Lisp_Overlay *ov;
3284 int startpos, endpos;
3285 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3287 overlay_heads.used = overlay_heads.bytes = 0;
3288 overlay_tails.used = overlay_tails.bytes = 0;
3289 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3291 XSETMISC (overlay, ov);
3292 eassert (OVERLAYP (overlay));
3294 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3295 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3296 if (endpos < pos)
3297 break;
3298 if (endpos != pos && startpos != pos)
3299 continue;
3300 window = Foverlay_get (overlay, Qwindow);
3301 if (WINDOWP (window) && XWINDOW (window) != w)
3302 continue;
3303 if (startpos == pos
3304 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3305 record_overlay_string (&overlay_heads, str,
3306 (startpos == endpos
3307 ? Foverlay_get (overlay, Qafter_string)
3308 : Qnil),
3309 Foverlay_get (overlay, Qpriority),
3310 endpos - startpos);
3311 else if (endpos == pos
3312 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3313 record_overlay_string (&overlay_tails, str, Qnil,
3314 Foverlay_get (overlay, Qpriority),
3315 endpos - startpos);
3317 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3319 XSETMISC (overlay, ov);
3320 eassert (OVERLAYP (overlay));
3322 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3323 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3324 if (startpos > pos)
3325 break;
3326 if (endpos != pos && startpos != pos)
3327 continue;
3328 window = Foverlay_get (overlay, Qwindow);
3329 if (WINDOWP (window) && XWINDOW (window) != w)
3330 continue;
3331 if (startpos == pos
3332 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3333 record_overlay_string (&overlay_heads, str,
3334 (startpos == endpos
3335 ? Foverlay_get (overlay, Qafter_string)
3336 : Qnil),
3337 Foverlay_get (overlay, Qpriority),
3338 endpos - startpos);
3339 else if (endpos == pos
3340 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3341 record_overlay_string (&overlay_tails, str, Qnil,
3342 Foverlay_get (overlay, Qpriority),
3343 endpos - startpos);
3345 if (overlay_tails.used > 1)
3346 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3347 cmp_for_strings);
3348 if (overlay_heads.used > 1)
3349 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3350 cmp_for_strings);
3351 if (overlay_heads.bytes || overlay_tails.bytes)
3353 Lisp_Object tem;
3354 int i;
3355 unsigned char *p;
3356 int total = overlay_heads.bytes + overlay_tails.bytes;
3358 if (total > overlay_str_len)
3360 overlay_str_len = total;
3361 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3362 total);
3364 p = overlay_str_buf;
3365 for (i = overlay_tails.used; --i >= 0;)
3367 int nbytes;
3368 tem = overlay_tails.buf[i].string;
3369 nbytes = copy_text (SDATA (tem), p,
3370 SBYTES (tem),
3371 STRING_MULTIBYTE (tem), multibyte);
3372 p += nbytes;
3374 for (i = 0; i < overlay_heads.used; ++i)
3376 int nbytes;
3377 tem = overlay_heads.buf[i].string;
3378 nbytes = copy_text (SDATA (tem), p,
3379 SBYTES (tem),
3380 STRING_MULTIBYTE (tem), multibyte);
3381 p += nbytes;
3382 tem = overlay_heads.buf[i].string2;
3383 if (STRINGP (tem))
3385 nbytes = copy_text (SDATA (tem), p,
3386 SBYTES (tem),
3387 STRING_MULTIBYTE (tem), multibyte);
3388 p += nbytes;
3391 if (p != overlay_str_buf + total)
3392 abort ();
3393 if (pstr)
3394 *pstr = overlay_str_buf;
3395 return total;
3397 return 0;
3400 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3402 void
3403 recenter_overlay_lists (buf, pos)
3404 struct buffer *buf;
3405 EMACS_INT pos;
3407 Lisp_Object overlay, beg, end;
3408 struct Lisp_Overlay *prev, *tail, *next;
3410 /* See if anything in overlays_before should move to overlays_after. */
3412 /* We don't strictly need prev in this loop; it should always be nil.
3413 But we use it for symmetry and in case that should cease to be true
3414 with some future change. */
3415 prev = NULL;
3416 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3418 next = tail->next;
3419 XSETMISC (overlay, tail);
3421 /* If the overlay is not valid, get rid of it. */
3422 if (!OVERLAY_VALID (overlay))
3423 #if 1
3424 abort ();
3425 #else
3427 /* Splice the cons cell TAIL out of overlays_before. */
3428 if (!NILP (prev))
3429 XCDR (prev) = next;
3430 else
3431 buf->overlays_before = next;
3432 tail = prev;
3433 continue;
3435 #endif
3437 beg = OVERLAY_START (overlay);
3438 end = OVERLAY_END (overlay);
3440 if (OVERLAY_POSITION (end) > pos)
3442 /* OVERLAY needs to be moved. */
3443 int where = OVERLAY_POSITION (beg);
3444 struct Lisp_Overlay *other, *other_prev;
3446 /* Splice the cons cell TAIL out of overlays_before. */
3447 if (prev)
3448 prev->next = next;
3449 else
3450 buf->overlays_before = next;
3452 /* Search thru overlays_after for where to put it. */
3453 other_prev = NULL;
3454 for (other = buf->overlays_after; other;
3455 other_prev = other, other = other->next)
3457 Lisp_Object otherbeg, otheroverlay;
3459 XSETMISC (otheroverlay, other);
3460 eassert (OVERLAY_VALID (otheroverlay));
3462 otherbeg = OVERLAY_START (otheroverlay);
3463 if (OVERLAY_POSITION (otherbeg) >= where)
3464 break;
3467 /* Add TAIL to overlays_after before OTHER. */
3468 tail->next = other;
3469 if (other_prev)
3470 other_prev->next = tail;
3471 else
3472 buf->overlays_after = tail;
3473 tail = prev;
3475 else
3476 /* We've reached the things that should stay in overlays_before.
3477 All the rest of overlays_before must end even earlier,
3478 so stop now. */
3479 break;
3482 /* See if anything in overlays_after should be in overlays_before. */
3483 prev = NULL;
3484 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3486 next = tail->next;
3487 XSETMISC (overlay, tail);
3489 /* If the overlay is not valid, get rid of it. */
3490 if (!OVERLAY_VALID (overlay))
3491 #if 1
3492 abort ();
3493 #else
3495 /* Splice the cons cell TAIL out of overlays_after. */
3496 if (!NILP (prev))
3497 XCDR (prev) = next;
3498 else
3499 buf->overlays_after = next;
3500 tail = prev;
3501 continue;
3503 #endif
3505 beg = OVERLAY_START (overlay);
3506 end = OVERLAY_END (overlay);
3508 /* Stop looking, when we know that nothing further
3509 can possibly end before POS. */
3510 if (OVERLAY_POSITION (beg) > pos)
3511 break;
3513 if (OVERLAY_POSITION (end) <= pos)
3515 /* OVERLAY needs to be moved. */
3516 int where = OVERLAY_POSITION (end);
3517 struct Lisp_Overlay *other, *other_prev;
3519 /* Splice the cons cell TAIL out of overlays_after. */
3520 if (prev)
3521 prev->next = next;
3522 else
3523 buf->overlays_after = next;
3525 /* Search thru overlays_before for where to put it. */
3526 other_prev = NULL;
3527 for (other = buf->overlays_before; other;
3528 other_prev = other, other = other->next)
3530 Lisp_Object otherend, otheroverlay;
3532 XSETMISC (otheroverlay, other);
3533 eassert (OVERLAY_VALID (otheroverlay));
3535 otherend = OVERLAY_END (otheroverlay);
3536 if (OVERLAY_POSITION (otherend) <= where)
3537 break;
3540 /* Add TAIL to overlays_before before OTHER. */
3541 tail->next = other;
3542 if (other_prev)
3543 other_prev->next = tail;
3544 else
3545 buf->overlays_before = tail;
3546 tail = prev;
3550 buf->overlay_center = pos;
3553 void
3554 adjust_overlays_for_insert (pos, length)
3555 EMACS_INT pos;
3556 EMACS_INT length;
3558 /* After an insertion, the lists are still sorted properly,
3559 but we may need to update the value of the overlay center. */
3560 if (current_buffer->overlay_center >= pos)
3561 current_buffer->overlay_center += length;
3564 void
3565 adjust_overlays_for_delete (pos, length)
3566 EMACS_INT pos;
3567 EMACS_INT length;
3569 if (current_buffer->overlay_center < pos)
3570 /* The deletion was to our right. No change needed; the before- and
3571 after-lists are still consistent. */
3573 else if (current_buffer->overlay_center > pos + length)
3574 /* The deletion was to our left. We need to adjust the center value
3575 to account for the change in position, but the lists are consistent
3576 given the new value. */
3577 current_buffer->overlay_center -= length;
3578 else
3579 /* We're right in the middle. There might be things on the after-list
3580 that now belong on the before-list. Recentering will move them,
3581 and also update the center point. */
3582 recenter_overlay_lists (current_buffer, pos);
3585 /* Fix up overlays that were garbled as a result of permuting markers
3586 in the range START through END. Any overlay with at least one
3587 endpoint in this range will need to be unlinked from the overlay
3588 list and reinserted in its proper place.
3589 Such an overlay might even have negative size at this point.
3590 If so, we'll make the overlay empty. */
3591 void
3592 fix_start_end_in_overlays (start, end)
3593 register int start, end;
3595 Lisp_Object overlay;
3596 struct Lisp_Overlay *before_list, *after_list;
3597 /* These are either nil, indicating that before_list or after_list
3598 should be assigned, or the cons cell the cdr of which should be
3599 assigned. */
3600 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3601 /* 'Parent', likewise, indicates a cons cell or
3602 current_buffer->overlays_before or overlays_after, depending
3603 which loop we're in. */
3604 struct Lisp_Overlay *tail, *parent;
3605 int startpos, endpos;
3607 /* This algorithm shifts links around instead of consing and GCing.
3608 The loop invariant is that before_list (resp. after_list) is a
3609 well-formed list except that its last element, the CDR of beforep
3610 (resp. afterp) if beforep (afterp) isn't nil or before_list
3611 (after_list) if it is, is still uninitialized. So it's not a bug
3612 that before_list isn't initialized, although it may look
3613 strange. */
3614 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3616 XSETMISC (overlay, tail);
3618 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3619 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3621 /* If the overlay is backwards, make it empty. */
3622 if (endpos < startpos)
3624 startpos = endpos;
3625 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3626 Qnil);
3629 if (endpos < start)
3630 break;
3632 if (endpos < end
3633 || (startpos >= start && startpos < end))
3635 /* Add it to the end of the wrong list. Later on,
3636 recenter_overlay_lists will move it to the right place. */
3637 if (endpos < current_buffer->overlay_center)
3639 if (!afterp)
3640 after_list = tail;
3641 else
3642 afterp->next = tail;
3643 afterp = tail;
3645 else
3647 if (!beforep)
3648 before_list = tail;
3649 else
3650 beforep->next = tail;
3651 beforep = tail;
3653 if (!parent)
3654 current_buffer->overlays_before = tail->next;
3655 else
3656 parent->next = tail->next;
3657 tail = tail->next;
3659 else
3660 parent = tail, tail = parent->next;
3662 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3664 XSETMISC (overlay, tail);
3666 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3667 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3669 /* If the overlay is backwards, make it empty. */
3670 if (endpos < startpos)
3672 startpos = endpos;
3673 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3674 Qnil);
3677 if (startpos >= end)
3678 break;
3680 if (startpos >= start
3681 || (endpos >= start && endpos < end))
3683 if (endpos < current_buffer->overlay_center)
3685 if (!afterp)
3686 after_list = tail;
3687 else
3688 afterp->next = tail;
3689 afterp = tail;
3691 else
3693 if (!beforep)
3694 before_list = tail;
3695 else
3696 beforep->next = tail;
3697 beforep = tail;
3699 if (!parent)
3700 current_buffer->overlays_after = tail->next;
3701 else
3702 parent->next = tail->next;
3703 tail = tail->next;
3705 else
3706 parent = tail, tail = parent->next;
3709 /* Splice the constructed (wrong) lists into the buffer's lists,
3710 and let the recenter function make it sane again. */
3711 if (beforep)
3713 beforep->next = current_buffer->overlays_before;
3714 current_buffer->overlays_before = before_list;
3716 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3718 if (afterp)
3720 afterp->next = current_buffer->overlays_after;
3721 current_buffer->overlays_after = after_list;
3723 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3726 /* We have two types of overlay: the one whose ending marker is
3727 after-insertion-marker (this is the usual case) and the one whose
3728 ending marker is before-insertion-marker. When `overlays_before'
3729 contains overlays of the latter type and the former type in this
3730 order and both overlays end at inserting position, inserting a text
3731 increases only the ending marker of the latter type, which results
3732 in incorrect ordering of `overlays_before'.
3734 This function fixes ordering of overlays in the slot
3735 `overlays_before' of the buffer *BP. Before the insertion, `point'
3736 was at PREV, and now is at POS. */
3738 void
3739 fix_overlays_before (bp, prev, pos)
3740 struct buffer *bp;
3741 EMACS_INT prev, pos;
3743 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3744 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3745 Lisp_Object tem;
3746 EMACS_INT end;
3748 /* After the insertion, the several overlays may be in incorrect
3749 order. The possibility is that, in the list `overlays_before',
3750 an overlay which ends at POS appears after an overlay which ends
3751 at PREV. Since POS is greater than PREV, we must fix the
3752 ordering of these overlays, by moving overlays ends at POS before
3753 the overlays ends at PREV. */
3755 /* At first, find a place where disordered overlays should be linked
3756 in. It is where an overlay which end before POS exists. (i.e. an
3757 overlay whose ending marker is after-insertion-marker if disorder
3758 exists). */
3759 while (tail
3760 && (XSETMISC (tem, tail),
3761 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3763 parent = tail;
3764 tail = tail->next;
3767 /* If we don't find such an overlay,
3768 or the found one ends before PREV,
3769 or the found one is the last one in the list,
3770 we don't have to fix anything. */
3771 if (!tail || end < prev || !tail->next)
3772 return;
3774 right_pair = parent;
3775 parent = tail;
3776 tail = tail->next;
3778 /* Now, end position of overlays in the list TAIL should be before
3779 or equal to PREV. In the loop, an overlay which ends at POS is
3780 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3781 we found an overlay which ends before PREV, the remaining
3782 overlays are in correct order. */
3783 while (tail)
3785 XSETMISC (tem, tail);
3786 end = OVERLAY_POSITION (OVERLAY_END (tem));
3788 if (end == pos)
3789 { /* This overlay is disordered. */
3790 struct Lisp_Overlay *found = tail;
3792 /* Unlink the found overlay. */
3793 tail = found->next;
3794 parent->next = tail;
3795 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3796 and link it into the right place. */
3797 if (!right_pair)
3799 found->next = bp->overlays_before;
3800 bp->overlays_before = found;
3802 else
3804 found->next = right_pair->next;
3805 right_pair->next = found;
3808 else if (end == prev)
3810 parent = tail;
3811 tail = tail->next;
3813 else /* No more disordered overlay. */
3814 break;
3818 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3819 doc: /* Return t if OBJECT is an overlay. */)
3820 (object)
3821 Lisp_Object object;
3823 return (OVERLAYP (object) ? Qt : Qnil);
3826 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3827 doc: /* Create a new overlay with range BEG to END in BUFFER.
3828 If omitted, BUFFER defaults to the current buffer.
3829 BEG and END may be integers or markers.
3830 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3831 for the front of the overlay advance when text is inserted there
3832 \(which means the text *is not* included in the overlay).
3833 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3834 for the rear of the overlay advance when text is inserted there
3835 \(which means the text *is* included in the overlay). */)
3836 (beg, end, buffer, front_advance, rear_advance)
3837 Lisp_Object beg, end, buffer;
3838 Lisp_Object front_advance, rear_advance;
3840 Lisp_Object overlay;
3841 struct buffer *b;
3843 if (NILP (buffer))
3844 XSETBUFFER (buffer, current_buffer);
3845 else
3846 CHECK_BUFFER (buffer);
3847 if (MARKERP (beg)
3848 && ! EQ (Fmarker_buffer (beg), buffer))
3849 error ("Marker points into wrong buffer");
3850 if (MARKERP (end)
3851 && ! EQ (Fmarker_buffer (end), buffer))
3852 error ("Marker points into wrong buffer");
3854 CHECK_NUMBER_COERCE_MARKER (beg);
3855 CHECK_NUMBER_COERCE_MARKER (end);
3857 if (XINT (beg) > XINT (end))
3859 Lisp_Object temp;
3860 temp = beg; beg = end; end = temp;
3863 b = XBUFFER (buffer);
3865 beg = Fset_marker (Fmake_marker (), beg, buffer);
3866 end = Fset_marker (Fmake_marker (), end, buffer);
3868 if (!NILP (front_advance))
3869 XMARKER (beg)->insertion_type = 1;
3870 if (!NILP (rear_advance))
3871 XMARKER (end)->insertion_type = 1;
3873 overlay = allocate_misc ();
3874 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3875 XOVERLAY (overlay)->start = beg;
3876 XOVERLAY (overlay)->end = end;
3877 XOVERLAY (overlay)->plist = Qnil;
3878 XOVERLAY (overlay)->next = NULL;
3880 /* Put the new overlay on the wrong list. */
3881 end = OVERLAY_END (overlay);
3882 if (OVERLAY_POSITION (end) < b->overlay_center)
3884 if (b->overlays_after)
3885 XOVERLAY (overlay)->next = b->overlays_after;
3886 b->overlays_after = XOVERLAY (overlay);
3888 else
3890 if (b->overlays_before)
3891 XOVERLAY (overlay)->next = b->overlays_before;
3892 b->overlays_before = XOVERLAY (overlay);
3895 /* This puts it in the right list, and in the right order. */
3896 recenter_overlay_lists (b, b->overlay_center);
3898 /* We don't need to redisplay the region covered by the overlay, because
3899 the overlay has no properties at the moment. */
3901 return overlay;
3904 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3906 static void
3907 modify_overlay (buf, start, end)
3908 struct buffer *buf;
3909 EMACS_INT start, end;
3911 if (start > end)
3913 int temp = start;
3914 start = end;
3915 end = temp;
3918 BUF_COMPUTE_UNCHANGED (buf, start, end);
3920 /* If this is a buffer not in the selected window,
3921 we must do other windows. */
3922 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3923 windows_or_buffers_changed = 1;
3924 /* If multiple windows show this buffer, we must do other windows. */
3925 else if (buffer_shared > 1)
3926 windows_or_buffers_changed = 1;
3927 /* If we modify an overlay at the end of the buffer, we cannot
3928 be sure that window end is still valid. */
3929 else if (end >= ZV && start <= ZV)
3930 windows_or_buffers_changed = 1;
3932 ++BUF_OVERLAY_MODIFF (buf);
3936 Lisp_Object Fdelete_overlay ();
3938 static struct Lisp_Overlay *
3939 unchain_overlay (list, overlay)
3940 struct Lisp_Overlay *list, *overlay;
3942 struct Lisp_Overlay *tmp, *prev;
3943 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3944 if (tmp == overlay)
3946 if (prev)
3947 prev->next = tmp->next;
3948 else
3949 list = tmp->next;
3950 overlay->next = NULL;
3951 break;
3953 return list;
3956 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3957 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3958 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3959 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3960 buffer. */)
3961 (overlay, beg, end, buffer)
3962 Lisp_Object overlay, beg, end, buffer;
3964 struct buffer *b, *ob;
3965 Lisp_Object obuffer;
3966 int count = SPECPDL_INDEX ();
3968 CHECK_OVERLAY (overlay);
3969 if (NILP (buffer))
3970 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3971 if (NILP (buffer))
3972 XSETBUFFER (buffer, current_buffer);
3973 CHECK_BUFFER (buffer);
3975 if (MARKERP (beg)
3976 && ! EQ (Fmarker_buffer (beg), buffer))
3977 error ("Marker points into wrong buffer");
3978 if (MARKERP (end)
3979 && ! EQ (Fmarker_buffer (end), buffer))
3980 error ("Marker points into wrong buffer");
3982 CHECK_NUMBER_COERCE_MARKER (beg);
3983 CHECK_NUMBER_COERCE_MARKER (end);
3985 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3986 return Fdelete_overlay (overlay);
3988 if (XINT (beg) > XINT (end))
3990 Lisp_Object temp;
3991 temp = beg; beg = end; end = temp;
3994 specbind (Qinhibit_quit, Qt);
3996 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3997 b = XBUFFER (buffer);
3998 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
4000 /* If the overlay has changed buffers, do a thorough redisplay. */
4001 if (!EQ (buffer, obuffer))
4003 /* Redisplay where the overlay was. */
4004 if (!NILP (obuffer))
4006 int o_beg;
4007 int o_end;
4009 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
4010 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
4012 modify_overlay (ob, o_beg, o_end);
4015 /* Redisplay where the overlay is going to be. */
4016 modify_overlay (b, XINT (beg), XINT (end));
4018 else
4019 /* Redisplay the area the overlay has just left, or just enclosed. */
4021 int o_beg, o_end;
4023 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
4024 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
4026 if (o_beg == XINT (beg))
4027 modify_overlay (b, o_end, XINT (end));
4028 else if (o_end == XINT (end))
4029 modify_overlay (b, o_beg, XINT (beg));
4030 else
4032 if (XINT (beg) < o_beg) o_beg = XINT (beg);
4033 if (XINT (end) > o_end) o_end = XINT (end);
4034 modify_overlay (b, o_beg, o_end);
4038 if (!NILP (obuffer))
4040 ob->overlays_before
4041 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
4042 ob->overlays_after
4043 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
4044 eassert (XOVERLAY (overlay)->next == NULL);
4047 Fset_marker (OVERLAY_START (overlay), beg, buffer);
4048 Fset_marker (OVERLAY_END (overlay), end, buffer);
4050 /* Put the overlay on the wrong list. */
4051 end = OVERLAY_END (overlay);
4052 if (OVERLAY_POSITION (end) < b->overlay_center)
4054 XOVERLAY (overlay)->next = b->overlays_after;
4055 b->overlays_after = XOVERLAY (overlay);
4057 else
4059 XOVERLAY (overlay)->next = b->overlays_before;
4060 b->overlays_before = XOVERLAY (overlay);
4063 /* This puts it in the right list, and in the right order. */
4064 recenter_overlay_lists (b, b->overlay_center);
4066 return unbind_to (count, overlay);
4069 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
4070 doc: /* Delete the overlay OVERLAY from its buffer. */)
4071 (overlay)
4072 Lisp_Object overlay;
4074 Lisp_Object buffer;
4075 struct buffer *b;
4076 int count = SPECPDL_INDEX ();
4078 CHECK_OVERLAY (overlay);
4080 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4081 if (NILP (buffer))
4082 return Qnil;
4084 b = XBUFFER (buffer);
4085 specbind (Qinhibit_quit, Qt);
4087 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4088 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4089 eassert (XOVERLAY (overlay)->next == NULL);
4090 modify_overlay (b,
4091 marker_position (OVERLAY_START (overlay)),
4092 marker_position (OVERLAY_END (overlay)));
4093 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4094 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4096 /* When deleting an overlay with before or after strings, turn off
4097 display optimizations for the affected buffer, on the basis that
4098 these strings may contain newlines. This is easier to do than to
4099 check for that situation during redisplay. */
4100 if (!windows_or_buffers_changed
4101 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4102 || !NILP (Foverlay_get (overlay, Qafter_string))))
4103 b->prevent_redisplay_optimizations_p = 1;
4105 return unbind_to (count, Qnil);
4108 /* Overlay dissection functions. */
4110 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4111 doc: /* Return the position at which OVERLAY starts. */)
4112 (overlay)
4113 Lisp_Object overlay;
4115 CHECK_OVERLAY (overlay);
4117 return (Fmarker_position (OVERLAY_START (overlay)));
4120 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4121 doc: /* Return the position at which OVERLAY ends. */)
4122 (overlay)
4123 Lisp_Object overlay;
4125 CHECK_OVERLAY (overlay);
4127 return (Fmarker_position (OVERLAY_END (overlay)));
4130 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4131 doc: /* Return the buffer OVERLAY belongs to.
4132 Return nil if OVERLAY has been deleted. */)
4133 (overlay)
4134 Lisp_Object overlay;
4136 CHECK_OVERLAY (overlay);
4138 return Fmarker_buffer (OVERLAY_START (overlay));
4141 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4142 doc: /* Return a list of the properties on OVERLAY.
4143 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4144 OVERLAY. */)
4145 (overlay)
4146 Lisp_Object overlay;
4148 CHECK_OVERLAY (overlay);
4150 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4154 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4155 doc: /* Return a list of the overlays that contain the character at POS. */)
4156 (pos)
4157 Lisp_Object pos;
4159 int noverlays;
4160 Lisp_Object *overlay_vec;
4161 int len;
4162 Lisp_Object result;
4164 CHECK_NUMBER_COERCE_MARKER (pos);
4166 len = 10;
4167 /* We can't use alloca here because overlays_at can call xrealloc. */
4168 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4170 /* Put all the overlays we want in a vector in overlay_vec.
4171 Store the length in len. */
4172 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4173 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4175 /* Make a list of them all. */
4176 result = Flist (noverlays, overlay_vec);
4178 xfree (overlay_vec);
4179 return result;
4182 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4183 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4184 Overlap means that at least one character is contained within the overlay
4185 and also contained within the specified region.
4186 Empty overlays are included in the result if they are located at BEG,
4187 between BEG and END, or at END provided END denotes the position at the
4188 end of the buffer. */)
4189 (beg, end)
4190 Lisp_Object beg, end;
4192 int noverlays;
4193 Lisp_Object *overlay_vec;
4194 int len;
4195 Lisp_Object result;
4197 CHECK_NUMBER_COERCE_MARKER (beg);
4198 CHECK_NUMBER_COERCE_MARKER (end);
4200 len = 10;
4201 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4203 /* Put all the overlays we want in a vector in overlay_vec.
4204 Store the length in len. */
4205 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4206 (int *) 0, (int *) 0);
4208 /* Make a list of them all. */
4209 result = Flist (noverlays, overlay_vec);
4211 xfree (overlay_vec);
4212 return result;
4215 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4216 1, 1, 0,
4217 doc: /* Return the next position after POS where an overlay starts or ends.
4218 If there are no overlay boundaries from POS to (point-max),
4219 the value is (point-max). */)
4220 (pos)
4221 Lisp_Object pos;
4223 int noverlays;
4224 EMACS_INT endpos;
4225 Lisp_Object *overlay_vec;
4226 int len;
4227 int i;
4229 CHECK_NUMBER_COERCE_MARKER (pos);
4231 len = 10;
4232 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4234 /* Put all the overlays we want in a vector in overlay_vec.
4235 Store the length in len.
4236 endpos gets the position where the next overlay starts. */
4237 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4238 &endpos, (EMACS_INT *) 0, 1);
4240 /* If any of these overlays ends before endpos,
4241 use its ending point instead. */
4242 for (i = 0; i < noverlays; i++)
4244 Lisp_Object oend;
4245 EMACS_INT oendpos;
4247 oend = OVERLAY_END (overlay_vec[i]);
4248 oendpos = OVERLAY_POSITION (oend);
4249 if (oendpos < endpos)
4250 endpos = oendpos;
4253 xfree (overlay_vec);
4254 return make_number (endpos);
4257 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4258 Sprevious_overlay_change, 1, 1, 0,
4259 doc: /* Return the previous position before POS where an overlay starts or ends.
4260 If there are no overlay boundaries from (point-min) to POS,
4261 the value is (point-min). */)
4262 (pos)
4263 Lisp_Object pos;
4265 int noverlays;
4266 EMACS_INT prevpos;
4267 Lisp_Object *overlay_vec;
4268 int len;
4270 CHECK_NUMBER_COERCE_MARKER (pos);
4272 /* At beginning of buffer, we know the answer;
4273 avoid bug subtracting 1 below. */
4274 if (XINT (pos) == BEGV)
4275 return pos;
4277 len = 10;
4278 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4280 /* Put all the overlays we want in a vector in overlay_vec.
4281 Store the length in len.
4282 prevpos gets the position of the previous change. */
4283 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4284 (EMACS_INT *) 0, &prevpos, 1);
4286 xfree (overlay_vec);
4287 return make_number (prevpos);
4290 /* These functions are for debugging overlays. */
4292 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4293 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4294 The car has all the overlays before the overlay center;
4295 the cdr has all the overlays after the overlay center.
4296 Recentering overlays moves overlays between these lists.
4297 The lists you get are copies, so that changing them has no effect.
4298 However, the overlays you get are the real objects that the buffer uses. */)
4301 struct Lisp_Overlay *ol;
4302 Lisp_Object before = Qnil, after = Qnil, tmp;
4303 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4305 XSETMISC (tmp, ol);
4306 before = Fcons (tmp, before);
4308 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4310 XSETMISC (tmp, ol);
4311 after = Fcons (tmp, after);
4313 return Fcons (Fnreverse (before), Fnreverse (after));
4316 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4317 doc: /* Recenter the overlays of the current buffer around position POS.
4318 That makes overlay lookup faster for positions near POS (but perhaps slower
4319 for positions far away from POS). */)
4320 (pos)
4321 Lisp_Object pos;
4323 CHECK_NUMBER_COERCE_MARKER (pos);
4325 recenter_overlay_lists (current_buffer, XINT (pos));
4326 return Qnil;
4329 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4330 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4331 (overlay, prop)
4332 Lisp_Object overlay, prop;
4334 CHECK_OVERLAY (overlay);
4335 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4338 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4339 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4340 (overlay, prop, value)
4341 Lisp_Object overlay, prop, value;
4343 Lisp_Object tail, buffer;
4344 int changed;
4346 CHECK_OVERLAY (overlay);
4348 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4350 for (tail = XOVERLAY (overlay)->plist;
4351 CONSP (tail) && CONSP (XCDR (tail));
4352 tail = XCDR (XCDR (tail)))
4353 if (EQ (XCAR (tail), prop))
4355 changed = !EQ (XCAR (XCDR (tail)), value);
4356 XSETCAR (XCDR (tail), value);
4357 goto found;
4359 /* It wasn't in the list, so add it to the front. */
4360 changed = !NILP (value);
4361 XOVERLAY (overlay)->plist
4362 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4363 found:
4364 if (! NILP (buffer))
4366 if (changed)
4367 modify_overlay (XBUFFER (buffer),
4368 marker_position (OVERLAY_START (overlay)),
4369 marker_position (OVERLAY_END (overlay)));
4370 if (EQ (prop, Qevaporate) && ! NILP (value)
4371 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4372 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4373 Fdelete_overlay (overlay);
4376 return value;
4379 /* Subroutine of report_overlay_modification. */
4381 /* Lisp vector holding overlay hook functions to call.
4382 Vector elements come in pairs.
4383 Each even-index element is a list of hook functions.
4384 The following odd-index element is the overlay they came from.
4386 Before the buffer change, we fill in this vector
4387 as we call overlay hook functions.
4388 After the buffer change, we get the functions to call from this vector.
4389 This way we always call the same functions before and after the change. */
4390 static Lisp_Object last_overlay_modification_hooks;
4392 /* Number of elements actually used in last_overlay_modification_hooks. */
4393 static int last_overlay_modification_hooks_used;
4395 /* Add one functionlist/overlay pair
4396 to the end of last_overlay_modification_hooks. */
4398 static void
4399 add_overlay_mod_hooklist (functionlist, overlay)
4400 Lisp_Object functionlist, overlay;
4402 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4404 if (last_overlay_modification_hooks_used == oldsize)
4405 last_overlay_modification_hooks = larger_vector
4406 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4407 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4408 functionlist); last_overlay_modification_hooks_used++;
4409 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4410 overlay); last_overlay_modification_hooks_used++;
4413 /* Run the modification-hooks of overlays that include
4414 any part of the text in START to END.
4415 If this change is an insertion, also
4416 run the insert-before-hooks of overlay starting at END,
4417 and the insert-after-hooks of overlay ending at START.
4419 This is called both before and after the modification.
4420 AFTER is nonzero when we call after the modification.
4422 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4423 When AFTER is nonzero, they are the start position,
4424 the position after the inserted new text,
4425 and the length of deleted or replaced old text. */
4427 void
4428 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4429 Lisp_Object start, end;
4430 int after;
4431 Lisp_Object arg1, arg2, arg3;
4433 Lisp_Object prop, overlay;
4434 struct Lisp_Overlay *tail;
4435 /* 1 if this change is an insertion. */
4436 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4437 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4439 overlay = Qnil;
4440 tail = NULL;
4442 /* We used to run the functions as soon as we found them and only register
4443 them in last_overlay_modification_hooks for the purpose of the `after'
4444 case. But running elisp code as we traverse the list of overlays is
4445 painful because the list can be modified by the elisp code so we had to
4446 copy at several places. We now simply do a read-only traversal that
4447 only collects the functions to run and we run them afterwards. It's
4448 simpler, especially since all the code was already there. -stef */
4450 if (!after)
4452 /* We are being called before a change.
4453 Scan the overlays to find the functions to call. */
4454 last_overlay_modification_hooks_used = 0;
4455 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4457 int startpos, endpos;
4458 Lisp_Object ostart, oend;
4460 XSETMISC (overlay, tail);
4462 ostart = OVERLAY_START (overlay);
4463 oend = OVERLAY_END (overlay);
4464 endpos = OVERLAY_POSITION (oend);
4465 if (XFASTINT (start) > endpos)
4466 break;
4467 startpos = OVERLAY_POSITION (ostart);
4468 if (insertion && (XFASTINT (start) == startpos
4469 || XFASTINT (end) == startpos))
4471 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4472 if (!NILP (prop))
4473 add_overlay_mod_hooklist (prop, overlay);
4475 if (insertion && (XFASTINT (start) == endpos
4476 || XFASTINT (end) == endpos))
4478 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4479 if (!NILP (prop))
4480 add_overlay_mod_hooklist (prop, overlay);
4482 /* Test for intersecting intervals. This does the right thing
4483 for both insertion and deletion. */
4484 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4486 prop = Foverlay_get (overlay, Qmodification_hooks);
4487 if (!NILP (prop))
4488 add_overlay_mod_hooklist (prop, overlay);
4492 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4494 int startpos, endpos;
4495 Lisp_Object ostart, oend;
4497 XSETMISC (overlay, tail);
4499 ostart = OVERLAY_START (overlay);
4500 oend = OVERLAY_END (overlay);
4501 startpos = OVERLAY_POSITION (ostart);
4502 endpos = OVERLAY_POSITION (oend);
4503 if (XFASTINT (end) < startpos)
4504 break;
4505 if (insertion && (XFASTINT (start) == startpos
4506 || XFASTINT (end) == startpos))
4508 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4509 if (!NILP (prop))
4510 add_overlay_mod_hooklist (prop, overlay);
4512 if (insertion && (XFASTINT (start) == endpos
4513 || XFASTINT (end) == endpos))
4515 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4516 if (!NILP (prop))
4517 add_overlay_mod_hooklist (prop, overlay);
4519 /* Test for intersecting intervals. This does the right thing
4520 for both insertion and deletion. */
4521 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4523 prop = Foverlay_get (overlay, Qmodification_hooks);
4524 if (!NILP (prop))
4525 add_overlay_mod_hooklist (prop, overlay);
4530 GCPRO4 (overlay, arg1, arg2, arg3);
4532 /* Call the functions recorded in last_overlay_modification_hooks.
4533 First copy the vector contents, in case some of these hooks
4534 do subsequent modification of the buffer. */
4535 int size = last_overlay_modification_hooks_used;
4536 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4537 int i;
4539 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4540 copy, size * sizeof (Lisp_Object));
4541 gcpro1.var = copy;
4542 gcpro1.nvars = size;
4544 for (i = 0; i < size;)
4546 Lisp_Object prop, overlay;
4547 prop = copy[i++];
4548 overlay = copy[i++];
4549 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4552 UNGCPRO;
4555 static void
4556 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4557 Lisp_Object list, overlay;
4558 int after;
4559 Lisp_Object arg1, arg2, arg3;
4561 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4563 GCPRO4 (list, arg1, arg2, arg3);
4565 while (CONSP (list))
4567 if (NILP (arg3))
4568 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4569 else
4570 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4571 list = XCDR (list);
4573 UNGCPRO;
4576 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4577 property is set. */
4578 void
4579 evaporate_overlays (pos)
4580 EMACS_INT pos;
4582 Lisp_Object overlay, hit_list;
4583 struct Lisp_Overlay *tail;
4585 hit_list = Qnil;
4586 if (pos <= current_buffer->overlay_center)
4587 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4589 int endpos;
4590 XSETMISC (overlay, tail);
4591 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4592 if (endpos < pos)
4593 break;
4594 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4595 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4596 hit_list = Fcons (overlay, hit_list);
4598 else
4599 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4601 int startpos;
4602 XSETMISC (overlay, tail);
4603 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4604 if (startpos > pos)
4605 break;
4606 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4607 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4608 hit_list = Fcons (overlay, hit_list);
4610 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4611 Fdelete_overlay (XCAR (hit_list));
4614 /* Somebody has tried to store a value with an unacceptable type
4615 in the slot with offset OFFSET. */
4617 void
4618 buffer_slot_type_mismatch (newval, type)
4619 Lisp_Object newval;
4620 int type;
4622 Lisp_Object predicate;
4624 switch (type)
4626 case_Lisp_Int: predicate = Qintegerp; break;
4627 case Lisp_String: predicate = Qstringp; break;
4628 case Lisp_Symbol: predicate = Qsymbolp; break;
4629 default: abort ();
4632 wrong_type_argument (predicate, newval);
4636 /***********************************************************************
4637 Allocation with mmap
4638 ***********************************************************************/
4640 #ifdef USE_MMAP_FOR_BUFFERS
4642 #include <sys/types.h>
4643 #include <sys/mman.h>
4645 #ifndef MAP_ANON
4646 #ifdef MAP_ANONYMOUS
4647 #define MAP_ANON MAP_ANONYMOUS
4648 #else
4649 #define MAP_ANON 0
4650 #endif
4651 #endif
4653 #ifndef MAP_FAILED
4654 #define MAP_FAILED ((void *) -1)
4655 #endif
4657 #include <stdio.h>
4658 #include <errno.h>
4660 #if MAP_ANON == 0
4661 #include <fcntl.h>
4662 #endif
4664 #include "coding.h"
4667 /* Memory is allocated in regions which are mapped using mmap(2).
4668 The current implementation lets the system select mapped
4669 addresses; we're not using MAP_FIXED in general, except when
4670 trying to enlarge regions.
4672 Each mapped region starts with a mmap_region structure, the user
4673 area starts after that structure, aligned to MEM_ALIGN.
4675 +-----------------------+
4676 | struct mmap_info + |
4677 | padding |
4678 +-----------------------+
4679 | user data |
4682 +-----------------------+ */
4684 struct mmap_region
4686 /* User-specified size. */
4687 size_t nbytes_specified;
4689 /* Number of bytes mapped */
4690 size_t nbytes_mapped;
4692 /* Pointer to the location holding the address of the memory
4693 allocated with the mmap'd block. The variable actually points
4694 after this structure. */
4695 POINTER_TYPE **var;
4697 /* Next and previous in list of all mmap'd regions. */
4698 struct mmap_region *next, *prev;
4701 /* Doubly-linked list of mmap'd regions. */
4703 static struct mmap_region *mmap_regions;
4705 /* File descriptor for mmap. If we don't have anonymous mapping,
4706 /dev/zero will be opened on it. */
4708 static int mmap_fd;
4710 /* Temporary storage for mmap_set_vars, see there. */
4712 static struct mmap_region *mmap_regions_1;
4713 static int mmap_fd_1;
4715 /* Page size on this system. */
4717 static int mmap_page_size;
4719 /* 1 means mmap has been intialized. */
4721 static int mmap_initialized_p;
4723 /* Value is X rounded up to the next multiple of N. */
4725 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4727 /* Size of mmap_region structure plus padding. */
4729 #define MMAP_REGION_STRUCT_SIZE \
4730 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4732 /* Given a pointer P to the start of the user-visible part of a mapped
4733 region, return a pointer to the start of the region. */
4735 #define MMAP_REGION(P) \
4736 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4738 /* Given a pointer P to the start of a mapped region, return a pointer
4739 to the start of the user-visible part of the region. */
4741 #define MMAP_USER_AREA(P) \
4742 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4744 #define MEM_ALIGN sizeof (double)
4746 /* Predicate returning true if part of the address range [START .. END]
4747 is currently mapped. Used to prevent overwriting an existing
4748 memory mapping.
4750 Default is to conservativly assume the address range is occupied by
4751 something else. This can be overridden by system configuration
4752 files if system-specific means to determine this exists. */
4754 #ifndef MMAP_ALLOCATED_P
4755 #define MMAP_ALLOCATED_P(start, end) 1
4756 #endif
4758 /* Function prototypes. */
4760 static int mmap_free_1 P_ ((struct mmap_region *));
4761 static int mmap_enlarge P_ ((struct mmap_region *, int));
4762 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4763 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4764 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4765 static void mmap_free P_ ((POINTER_TYPE **ptr));
4766 static void mmap_init P_ ((void));
4769 /* Return a region overlapping address range START...END, or null if
4770 none. END is not including, i.e. the last byte in the range
4771 is at END - 1. */
4773 static struct mmap_region *
4774 mmap_find (start, end)
4775 POINTER_TYPE *start, *end;
4777 struct mmap_region *r;
4778 char *s = (char *) start, *e = (char *) end;
4780 for (r = mmap_regions; r; r = r->next)
4782 char *rstart = (char *) r;
4783 char *rend = rstart + r->nbytes_mapped;
4785 if (/* First byte of range, i.e. START, in this region? */
4786 (s >= rstart && s < rend)
4787 /* Last byte of range, i.e. END - 1, in this region? */
4788 || (e > rstart && e <= rend)
4789 /* First byte of this region in the range? */
4790 || (rstart >= s && rstart < e)
4791 /* Last byte of this region in the range? */
4792 || (rend > s && rend <= e))
4793 break;
4796 return r;
4800 /* Unmap a region. P is a pointer to the start of the user-araa of
4801 the region. Value is non-zero if successful. */
4803 static int
4804 mmap_free_1 (r)
4805 struct mmap_region *r;
4807 if (r->next)
4808 r->next->prev = r->prev;
4809 if (r->prev)
4810 r->prev->next = r->next;
4811 else
4812 mmap_regions = r->next;
4814 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4816 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4817 return 0;
4820 return 1;
4824 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4825 Value is non-zero if successful. */
4827 static int
4828 mmap_enlarge (r, npages)
4829 struct mmap_region *r;
4830 int npages;
4832 char *region_end = (char *) r + r->nbytes_mapped;
4833 size_t nbytes;
4834 int success = 0;
4836 if (npages < 0)
4838 /* Unmap pages at the end of the region. */
4839 nbytes = - npages * mmap_page_size;
4840 if (munmap (region_end - nbytes, nbytes) == -1)
4841 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4842 else
4844 r->nbytes_mapped -= nbytes;
4845 success = 1;
4848 else if (npages > 0)
4850 nbytes = npages * mmap_page_size;
4852 /* Try to map additional pages at the end of the region. We
4853 cannot do this if the address range is already occupied by
4854 something else because mmap deletes any previous mapping.
4855 I'm not sure this is worth doing, let's see. */
4856 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4858 POINTER_TYPE *p;
4860 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4861 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4862 if (p == MAP_FAILED)
4863 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4864 else if (p != (POINTER_TYPE *) region_end)
4866 /* Kernels are free to choose a different address. In
4867 that case, unmap what we've mapped above; we have
4868 no use for it. */
4869 if (munmap (p, nbytes) == -1)
4870 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4872 else
4874 r->nbytes_mapped += nbytes;
4875 success = 1;
4880 return success;
4884 /* Set or reset variables holding references to mapped regions. If
4885 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4886 non-zero, set all variables to the start of the user-areas
4887 of mapped regions.
4889 This function is called from Fdump_emacs to ensure that the dumped
4890 Emacs doesn't contain references to memory that won't be mapped
4891 when Emacs starts. */
4893 void
4894 mmap_set_vars (restore_p)
4895 int restore_p;
4897 struct mmap_region *r;
4899 if (restore_p)
4901 mmap_regions = mmap_regions_1;
4902 mmap_fd = mmap_fd_1;
4903 for (r = mmap_regions; r; r = r->next)
4904 *r->var = MMAP_USER_AREA (r);
4906 else
4908 for (r = mmap_regions; r; r = r->next)
4909 *r->var = NULL;
4910 mmap_regions_1 = mmap_regions;
4911 mmap_regions = NULL;
4912 mmap_fd_1 = mmap_fd;
4913 mmap_fd = -1;
4918 /* Allocate a block of storage large enough to hold NBYTES bytes of
4919 data. A pointer to the data is returned in *VAR. VAR is thus the
4920 address of some variable which will use the data area.
4922 The allocation of 0 bytes is valid.
4924 If we can't allocate the necessary memory, set *VAR to null, and
4925 return null. */
4927 static POINTER_TYPE *
4928 mmap_alloc (var, nbytes)
4929 POINTER_TYPE **var;
4930 size_t nbytes;
4932 void *p;
4933 size_t map;
4935 mmap_init ();
4937 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4938 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4939 mmap_fd, 0);
4941 if (p == MAP_FAILED)
4943 if (errno != ENOMEM)
4944 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4945 p = NULL;
4947 else
4949 struct mmap_region *r = (struct mmap_region *) p;
4951 r->nbytes_specified = nbytes;
4952 r->nbytes_mapped = map;
4953 r->var = var;
4954 r->prev = NULL;
4955 r->next = mmap_regions;
4956 if (r->next)
4957 r->next->prev = r;
4958 mmap_regions = r;
4960 p = MMAP_USER_AREA (p);
4963 return *var = p;
4967 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4968 resize it to size NBYTES. Change *VAR to reflect the new block,
4969 and return this value. If more memory cannot be allocated, then
4970 leave *VAR unchanged, and return null. */
4972 static POINTER_TYPE *
4973 mmap_realloc (var, nbytes)
4974 POINTER_TYPE **var;
4975 size_t nbytes;
4977 POINTER_TYPE *result;
4979 mmap_init ();
4981 if (*var == NULL)
4982 result = mmap_alloc (var, nbytes);
4983 else if (nbytes == 0)
4985 mmap_free (var);
4986 result = mmap_alloc (var, nbytes);
4988 else
4990 struct mmap_region *r = MMAP_REGION (*var);
4991 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4993 if (room < nbytes)
4995 /* Must enlarge. */
4996 POINTER_TYPE *old_ptr = *var;
4998 /* Try to map additional pages at the end of the region.
4999 If that fails, allocate a new region, copy data
5000 from the old region, then free it. */
5001 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
5002 / mmap_page_size)))
5004 r->nbytes_specified = nbytes;
5005 *var = result = old_ptr;
5007 else if (mmap_alloc (var, nbytes))
5009 bcopy (old_ptr, *var, r->nbytes_specified);
5010 mmap_free_1 (MMAP_REGION (old_ptr));
5011 result = *var;
5012 r = MMAP_REGION (result);
5013 r->nbytes_specified = nbytes;
5015 else
5017 *var = old_ptr;
5018 result = NULL;
5021 else if (room - nbytes >= mmap_page_size)
5023 /* Shrinking by at least a page. Let's give some
5024 memory back to the system.
5026 The extra parens are to make the division happens first,
5027 on positive values, so we know it will round towards
5028 zero. */
5029 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
5030 result = *var;
5031 r->nbytes_specified = nbytes;
5033 else
5035 /* Leave it alone. */
5036 result = *var;
5037 r->nbytes_specified = nbytes;
5041 return result;
5045 /* Free a block of relocatable storage whose data is pointed to by
5046 PTR. Store 0 in *PTR to show there's no block allocated. */
5048 static void
5049 mmap_free (var)
5050 POINTER_TYPE **var;
5052 mmap_init ();
5054 if (*var)
5056 mmap_free_1 (MMAP_REGION (*var));
5057 *var = NULL;
5062 /* Perform necessary intializations for the use of mmap. */
5064 static void
5065 mmap_init ()
5067 #if MAP_ANON == 0
5068 /* The value of mmap_fd is initially 0 in temacs, and -1
5069 in a dumped Emacs. */
5070 if (mmap_fd <= 0)
5072 /* No anonymous mmap -- we need the file descriptor. */
5073 mmap_fd = open ("/dev/zero", O_RDONLY);
5074 if (mmap_fd == -1)
5075 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5077 #endif /* MAP_ANON == 0 */
5079 if (mmap_initialized_p)
5080 return;
5081 mmap_initialized_p = 1;
5083 #if MAP_ANON != 0
5084 mmap_fd = -1;
5085 #endif
5087 mmap_page_size = getpagesize ();
5090 #endif /* USE_MMAP_FOR_BUFFERS */
5094 /***********************************************************************
5095 Buffer-text Allocation
5096 ***********************************************************************/
5098 #ifdef REL_ALLOC
5099 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5100 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5101 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5102 #endif /* REL_ALLOC */
5105 /* Allocate NBYTES bytes for buffer B's text buffer. */
5107 static void
5108 alloc_buffer_text (b, nbytes)
5109 struct buffer *b;
5110 size_t nbytes;
5112 POINTER_TYPE *p;
5114 BLOCK_INPUT;
5115 #if defined USE_MMAP_FOR_BUFFERS
5116 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5117 #elif defined REL_ALLOC
5118 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5119 #else
5120 p = xmalloc (nbytes);
5121 #endif
5123 if (p == NULL)
5125 UNBLOCK_INPUT;
5126 memory_full ();
5129 b->text->beg = (unsigned char *) p;
5130 UNBLOCK_INPUT;
5133 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5134 shrink it. */
5136 void
5137 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5139 POINTER_TYPE *p;
5140 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5141 + delta);
5142 BLOCK_INPUT;
5143 #if defined USE_MMAP_FOR_BUFFERS
5144 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5145 #elif defined REL_ALLOC
5146 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5147 #else
5148 p = xrealloc (b->text->beg, nbytes);
5149 #endif
5151 if (p == NULL)
5153 UNBLOCK_INPUT;
5154 memory_full ();
5157 BUF_BEG_ADDR (b) = (unsigned char *) p;
5158 UNBLOCK_INPUT;
5162 /* Free buffer B's text buffer. */
5164 static void
5165 free_buffer_text (b)
5166 struct buffer *b;
5168 BLOCK_INPUT;
5170 #if defined USE_MMAP_FOR_BUFFERS
5171 mmap_free ((POINTER_TYPE **) &b->text->beg);
5172 #elif defined REL_ALLOC
5173 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5174 #else
5175 xfree (b->text->beg);
5176 #endif
5178 BUF_BEG_ADDR (b) = NULL;
5179 UNBLOCK_INPUT;
5184 /***********************************************************************
5185 Initialization
5186 ***********************************************************************/
5188 void
5189 init_buffer_once ()
5191 int idx;
5193 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5195 /* Make sure all markable slots in buffer_defaults
5196 are initialized reasonably, so mark_buffer won't choke. */
5197 reset_buffer (&buffer_defaults);
5198 reset_buffer_local_variables (&buffer_defaults, 1);
5199 reset_buffer (&buffer_local_symbols);
5200 reset_buffer_local_variables (&buffer_local_symbols, 1);
5201 /* Prevent GC from getting confused. */
5202 buffer_defaults.text = &buffer_defaults.own_text;
5203 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5204 BUF_INTERVALS (&buffer_defaults) = 0;
5205 BUF_INTERVALS (&buffer_local_symbols) = 0;
5206 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5207 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5208 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5209 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5211 /* Set up the default values of various buffer slots. */
5212 /* Must do these before making the first buffer! */
5214 /* real setup is done in bindings.el */
5215 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5216 buffer_defaults.header_line_format = Qnil;
5217 buffer_defaults.abbrev_mode = Qnil;
5218 buffer_defaults.overwrite_mode = Qnil;
5219 buffer_defaults.case_fold_search = Qt;
5220 buffer_defaults.auto_fill_function = Qnil;
5221 buffer_defaults.selective_display = Qnil;
5222 #ifndef old
5223 buffer_defaults.selective_display_ellipses = Qt;
5224 #endif
5225 buffer_defaults.abbrev_table = Qnil;
5226 buffer_defaults.display_table = Qnil;
5227 buffer_defaults.undo_list = Qnil;
5228 buffer_defaults.mark_active = Qnil;
5229 buffer_defaults.file_format = Qnil;
5230 buffer_defaults.auto_save_file_format = Qt;
5231 buffer_defaults.overlays_before = NULL;
5232 buffer_defaults.overlays_after = NULL;
5233 buffer_defaults.overlay_center = BEG;
5235 XSETFASTINT (buffer_defaults.tab_width, 8);
5236 buffer_defaults.truncate_lines = Qnil;
5237 buffer_defaults.word_wrap = Qnil;
5238 buffer_defaults.ctl_arrow = Qt;
5239 buffer_defaults.direction_reversed = Qnil;
5240 buffer_defaults.cursor_type = Qt;
5241 buffer_defaults.extra_line_spacing = Qnil;
5242 buffer_defaults.cursor_in_non_selected_windows = Qt;
5243 buffer_defaults.owner = Qnil;
5245 #ifdef DOS_NT
5246 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5247 #endif
5248 buffer_defaults.enable_multibyte_characters = Qt;
5249 buffer_defaults.buffer_file_coding_system = Qnil;
5250 XSETFASTINT (buffer_defaults.fill_column, 70);
5251 XSETFASTINT (buffer_defaults.left_margin, 0);
5252 buffer_defaults.cache_long_line_scans = Qnil;
5253 buffer_defaults.file_truename = Qnil;
5254 XSETFASTINT (buffer_defaults.display_count, 0);
5255 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5256 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5257 buffer_defaults.left_fringe_width = Qnil;
5258 buffer_defaults.right_fringe_width = Qnil;
5259 buffer_defaults.fringes_outside_margins = Qnil;
5260 buffer_defaults.scroll_bar_width = Qnil;
5261 buffer_defaults.vertical_scroll_bar_type = Qt;
5262 buffer_defaults.indicate_empty_lines = Qnil;
5263 buffer_defaults.indicate_buffer_boundaries = Qnil;
5264 buffer_defaults.fringe_indicator_alist = Qnil;
5265 buffer_defaults.fringe_cursor_alist = Qnil;
5266 buffer_defaults.scroll_up_aggressively = Qnil;
5267 buffer_defaults.scroll_down_aggressively = Qnil;
5268 buffer_defaults.display_time = Qnil;
5270 /* Assign the local-flags to the slots that have default values.
5271 The local flag is a bit that is used in the buffer
5272 to say that it has its own local value for the slot.
5273 The local flag bits are in the local_var_flags slot of the buffer. */
5275 /* Nothing can work if this isn't true */
5276 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5278 /* 0 means not a lisp var, -1 means always local, else mask */
5279 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5280 XSETINT (buffer_local_flags.filename, -1);
5281 XSETINT (buffer_local_flags.directory, -1);
5282 XSETINT (buffer_local_flags.backed_up, -1);
5283 XSETINT (buffer_local_flags.save_length, -1);
5284 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5285 XSETINT (buffer_local_flags.read_only, -1);
5286 XSETINT (buffer_local_flags.major_mode, -1);
5287 XSETINT (buffer_local_flags.mode_name, -1);
5288 XSETINT (buffer_local_flags.undo_list, -1);
5289 XSETINT (buffer_local_flags.mark_active, -1);
5290 XSETINT (buffer_local_flags.point_before_scroll, -1);
5291 XSETINT (buffer_local_flags.file_truename, -1);
5292 XSETINT (buffer_local_flags.invisibility_spec, -1);
5293 XSETINT (buffer_local_flags.file_format, -1);
5294 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5295 XSETINT (buffer_local_flags.display_count, -1);
5296 XSETINT (buffer_local_flags.display_time, -1);
5297 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5299 idx = 1;
5300 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5301 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5302 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5303 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5304 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5305 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5306 #ifndef old
5307 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5308 #endif
5309 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5310 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5311 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5312 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5313 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5314 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5315 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5316 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5317 #ifdef DOS_NT
5318 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5319 /* Make this one a permanent local. */
5320 buffer_permanent_local_flags[idx++] = 1;
5321 #endif
5322 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5323 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5324 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5325 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5326 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5327 /* Make this one a permanent local. */
5328 buffer_permanent_local_flags[idx++] = 1;
5329 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5330 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5331 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5332 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5333 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5334 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5335 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5336 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5337 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5338 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5339 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5340 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5341 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5342 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5343 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5344 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5345 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5347 /* Need more room? */
5348 if (idx >= MAX_PER_BUFFER_VARS)
5349 abort ();
5350 last_per_buffer_idx = idx;
5352 Vbuffer_alist = Qnil;
5353 current_buffer = 0;
5354 all_buffers = 0;
5356 QSFundamental = make_pure_c_string ("Fundamental");
5358 Qfundamental_mode = intern_c_string ("fundamental-mode");
5359 buffer_defaults.major_mode = Qfundamental_mode;
5361 Qmode_class = intern_c_string ("mode-class");
5363 Qprotected_field = intern_c_string ("protected-field");
5365 Qpermanent_local = intern_c_string ("permanent-local");
5367 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5368 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5370 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5372 /* super-magic invisible buffer */
5373 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5374 Vbuffer_alist = Qnil;
5376 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5378 inhibit_modification_hooks = 0;
5381 void
5382 init_buffer ()
5384 char *pwd;
5385 Lisp_Object temp;
5386 int len;
5388 pthread_cond_init (&buffer_cond, NULL);
5390 #ifdef USE_MMAP_FOR_BUFFERS
5392 /* When using the ralloc implementation based on mmap(2), buffer
5393 text pointers will have been set to null in the dumped Emacs.
5394 Map new memory. */
5395 struct buffer *b;
5397 for (b = all_buffers; b; b = b->next)
5398 if (b->text->beg == NULL)
5399 enlarge_buffer_text (b, 0);
5401 #endif /* USE_MMAP_FOR_BUFFERS */
5403 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5404 if (NILP (buffer_defaults.enable_multibyte_characters))
5405 Fset_buffer_multibyte (Qnil);
5407 pwd = get_current_dir_name ();
5409 if (!pwd)
5410 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5412 /* Maybe this should really use some standard subroutine
5413 whose definition is filename syntax dependent. */
5414 len = strlen (pwd);
5415 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5417 /* Grow buffer to add directory separator and '\0'. */
5418 pwd = (char *) realloc (pwd, len + 2);
5419 if (!pwd)
5420 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5421 pwd[len] = DIRECTORY_SEP;
5422 pwd[len + 1] = '\0';
5425 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5426 if (! NILP (buffer_defaults.enable_multibyte_characters))
5427 /* At this moment, we still don't know how to decode the
5428 directory name. So, we keep the bytes in multibyte form so
5429 that ENCODE_FILE correctly gets the original bytes. */
5430 current_buffer->directory
5431 = string_to_multibyte (current_buffer->directory);
5433 /* Add /: to the front of the name
5434 if it would otherwise be treated as magic. */
5435 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5436 if (! NILP (temp)
5437 /* If the default dir is just /, TEMP is non-nil
5438 because of the ange-ftp completion handler.
5439 However, it is not necessary to turn / into /:/.
5440 So avoid doing that. */
5441 && strcmp ("/", SDATA (current_buffer->directory)))
5442 current_buffer->directory
5443 = concat2 (build_string ("/:"), current_buffer->directory);
5445 temp = get_minibuffer (0);
5446 XBUFFER (temp)->directory = current_buffer->directory;
5448 free (pwd);
5451 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5452 Object stored in the current buffer. address is the address of the slot
5453 in the buffer that is current now. */
5455 /* TYPE is nil for a general Lisp variable.
5456 An integer specifies a type; then only LIsp values
5457 with that type code are allowed (except that nil is allowed too).
5458 LNAME is the LIsp-level variable name.
5459 VNAME is the name of the buffer slot.
5460 DOC is a dummy where you write the doc string as a comment. */
5461 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5462 defvar_per_buffer (lname, vname, type, 0)
5464 static void
5465 defvar_per_buffer (namestring, address, type, doc)
5466 char *namestring;
5467 Lisp_Object *address;
5468 Lisp_Object type;
5469 char *doc;
5471 Lisp_Object sym, val;
5472 int offset;
5474 sym = intern (namestring);
5475 val = allocate_misc ();
5476 offset = (char *)address - (char *)current_buffer;
5478 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5479 XBUFFER_OBJFWD (val)->offset = offset;
5480 XBUFFER_OBJFWD (val)->slottype = type;
5481 SET_SYMBOL_VALUE (sym, val);
5482 PER_BUFFER_SYMBOL (offset) = sym;
5484 if (PER_BUFFER_IDX (offset) == 0)
5485 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5486 slot of buffer_local_flags */
5487 abort ();
5491 /* initialize the buffer routines */
5492 void
5493 syms_of_buffer ()
5495 staticpro (&last_overlay_modification_hooks);
5496 last_overlay_modification_hooks
5497 = Fmake_vector (make_number (10), Qnil);
5499 staticpro (&Vbuffer_defaults);
5500 staticpro (&Vbuffer_local_symbols);
5501 staticpro (&Qfundamental_mode);
5502 staticpro (&Qmode_class);
5503 staticpro (&QSFundamental);
5504 staticpro (&Vbuffer_alist);
5505 staticpro (&Qprotected_field);
5506 staticpro (&Qpermanent_local);
5507 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5508 staticpro (&Qpermanent_local_hook);
5509 staticpro (&Qkill_buffer_hook);
5510 Qoverlayp = intern_c_string ("overlayp");
5511 staticpro (&Qoverlayp);
5512 Qevaporate = intern_c_string ("evaporate");
5513 staticpro (&Qevaporate);
5514 Qmodification_hooks = intern_c_string ("modification-hooks");
5515 staticpro (&Qmodification_hooks);
5516 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5517 staticpro (&Qinsert_in_front_hooks);
5518 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5519 staticpro (&Qinsert_behind_hooks);
5520 Qget_file_buffer = intern_c_string ("get-file-buffer");
5521 staticpro (&Qget_file_buffer);
5522 Qpriority = intern_c_string ("priority");
5523 staticpro (&Qpriority);
5524 Qwindow = intern_c_string ("window");
5525 staticpro (&Qwindow);
5526 Qbefore_string = intern_c_string ("before-string");
5527 staticpro (&Qbefore_string);
5528 Qafter_string = intern_c_string ("after-string");
5529 staticpro (&Qafter_string);
5530 Qfirst_change_hook = intern_c_string ("first-change-hook");
5531 staticpro (&Qfirst_change_hook);
5532 Qbefore_change_functions = intern_c_string ("before-change-functions");
5533 staticpro (&Qbefore_change_functions);
5534 Qafter_change_functions = intern_c_string ("after-change-functions");
5535 staticpro (&Qafter_change_functions);
5536 /* The next one is initialized in init_buffer_once. */
5537 staticpro (&Qucs_set_table_for_input);
5539 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5540 staticpro (&Qkill_buffer_query_functions);
5542 Fput (Qprotected_field, Qerror_conditions,
5543 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5544 Fput (Qprotected_field, Qerror_message,
5545 make_pure_c_string ("Attempt to modify a protected field"));
5547 /* All these use DEFVAR_LISP_NOPRO because the slots in
5548 buffer_defaults will all be marked via Vbuffer_defaults. */
5550 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5551 &buffer_defaults.mode_line_format,
5552 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5553 This is the same as (default-value 'mode-line-format). */);
5555 DEFVAR_LISP_NOPRO ("default-header-line-format",
5556 &buffer_defaults.header_line_format,
5557 doc: /* Default value of `header-line-format' for buffers that don't override it.
5558 This is the same as (default-value 'header-line-format). */);
5560 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5561 doc: /* Default value of `cursor-type' for buffers that don't override it.
5562 This is the same as (default-value 'cursor-type). */);
5564 DEFVAR_LISP_NOPRO ("default-line-spacing",
5565 &buffer_defaults.extra_line_spacing,
5566 doc: /* Default value of `line-spacing' for buffers that don't override it.
5567 This is the same as (default-value 'line-spacing). */);
5569 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5570 &buffer_defaults.cursor_in_non_selected_windows,
5571 doc: /* Default value of `cursor-in-non-selected-windows'.
5572 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5574 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5575 &buffer_defaults.abbrev_mode,
5576 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5577 This is the same as (default-value 'abbrev-mode). */);
5579 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5580 &buffer_defaults.ctl_arrow,
5581 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5582 This is the same as (default-value 'ctl-arrow). */);
5584 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5585 &buffer_defaults.direction_reversed,
5586 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5587 This is the same as (default-value 'direction-reversed). */);
5589 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5590 &buffer_defaults.enable_multibyte_characters,
5591 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5592 This is the same as (default-value 'enable-multibyte-characters). */);
5594 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5595 &buffer_defaults.buffer_file_coding_system,
5596 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5597 This is the same as (default-value 'buffer-file-coding-system). */);
5599 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5600 &buffer_defaults.truncate_lines,
5601 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5602 This is the same as (default-value 'truncate-lines). */);
5604 DEFVAR_LISP_NOPRO ("default-fill-column",
5605 &buffer_defaults.fill_column,
5606 doc: /* Default value of `fill-column' for buffers that do not override it.
5607 This is the same as (default-value 'fill-column). */);
5609 DEFVAR_LISP_NOPRO ("default-left-margin",
5610 &buffer_defaults.left_margin,
5611 doc: /* Default value of `left-margin' for buffers that do not override it.
5612 This is the same as (default-value 'left-margin). */);
5614 DEFVAR_LISP_NOPRO ("default-tab-width",
5615 &buffer_defaults.tab_width,
5616 doc: /* Default value of `tab-width' for buffers that do not override it.
5617 This is the same as (default-value 'tab-width). */);
5619 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5620 &buffer_defaults.case_fold_search,
5621 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5622 This is the same as (default-value 'case-fold-search). */);
5624 #ifdef DOS_NT
5625 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5626 &buffer_defaults.buffer_file_type,
5627 doc: /* Default file type for buffers that do not override it.
5628 This is the same as (default-value 'buffer-file-type).
5629 The file type is nil for text, t for binary. */);
5630 #endif
5632 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5633 &buffer_defaults.left_margin_cols,
5634 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5635 This is the same as (default-value 'left-margin-width). */);
5637 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5638 &buffer_defaults.right_margin_cols,
5639 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5640 This is the same as (default-value 'right-margin-width). */);
5642 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5643 &buffer_defaults.left_fringe_width,
5644 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5645 This is the same as (default-value 'left-fringe-width). */);
5647 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5648 &buffer_defaults.right_fringe_width,
5649 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5650 This is the same as (default-value 'right-fringe-width). */);
5652 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5653 &buffer_defaults.fringes_outside_margins,
5654 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5655 This is the same as (default-value 'fringes-outside-margins). */);
5657 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5658 &buffer_defaults.scroll_bar_width,
5659 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5660 This is the same as (default-value 'scroll-bar-width). */);
5662 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5663 &buffer_defaults.vertical_scroll_bar_type,
5664 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5665 This is the same as (default-value 'vertical-scroll-bar). */);
5667 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5668 &buffer_defaults.indicate_empty_lines,
5669 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5670 This is the same as (default-value 'indicate-empty-lines). */);
5672 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5673 &buffer_defaults.indicate_buffer_boundaries,
5674 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5675 This is the same as (default-value 'indicate-buffer-boundaries). */);
5677 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5678 &buffer_defaults.fringe_indicator_alist,
5679 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5680 This is the same as (default-value 'fringe-indicator-alist'). */);
5682 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5683 &buffer_defaults.fringe_cursor_alist,
5684 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5685 This is the same as (default-value 'fringe-cursor-alist'). */);
5687 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5688 &buffer_defaults.scroll_up_aggressively,
5689 doc: /* Default value of `scroll-up-aggressively'.
5690 This value applies in buffers that don't have their own local values.
5691 This is the same as (default-value 'scroll-up-aggressively). */);
5693 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5694 &buffer_defaults.scroll_down_aggressively,
5695 doc: /* Default value of `scroll-down-aggressively'.
5696 This value applies in buffers that don't have their own local values.
5697 This is the same as (default-value 'scroll-down-aggressively). */);
5699 DEFVAR_PER_BUFFER ("header-line-format",
5700 &current_buffer->header_line_format,
5701 Qnil,
5702 doc: /* Analogous to `mode-line-format', but controls the header line.
5703 The header line appears, optionally, at the top of a window;
5704 the mode line appears at the bottom. */);
5706 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5707 Qnil,
5708 doc: /* Template for displaying mode line for current buffer.
5709 Each buffer has its own value of this variable.
5710 Value may be nil, a string, a symbol or a list or cons cell.
5711 A value of nil means don't display a mode line.
5712 For a symbol, its value is used (but it is ignored if t or nil).
5713 A string appearing directly as the value of a symbol is processed verbatim
5714 in that the %-constructs below are not recognized.
5715 Note that unless the symbol is marked as a `risky-local-variable', all
5716 properties in any strings, as well as all :eval and :propertize forms
5717 in the value of that symbol will be ignored.
5718 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5719 is used as a mode line element. Be careful--FORM should not load any files,
5720 because that can cause an infinite recursion.
5721 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5722 with the specified properties PROPS applied.
5723 For a list whose car is a symbol, the symbol's value is taken,
5724 and if that is non-nil, the cadr of the list is processed recursively.
5725 Otherwise, the caddr of the list (if there is one) is processed.
5726 For a list whose car is a string or list, each element is processed
5727 recursively and the results are effectively concatenated.
5728 For a list whose car is an integer, the cdr of the list is processed
5729 and padded (if the number is positive) or truncated (if negative)
5730 to the width specified by that number.
5731 A string is printed verbatim in the mode line except for %-constructs:
5732 (%-constructs are allowed when the string is the entire mode-line-format
5733 or when it is found in a cons-cell or a list)
5734 %b -- print buffer name. %f -- print visited file name.
5735 %F -- print frame name.
5736 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5737 %& is like %*, but ignore read-only-ness.
5738 % means buffer is read-only and * means it is modified.
5739 For a modified read-only buffer, %* gives % and %+ gives *.
5740 %s -- print process status. %l -- print the current line number.
5741 %c -- print the current column number (this makes editing slower).
5742 To make the column number update correctly in all cases,
5743 `column-number-mode' must be non-nil.
5744 %i -- print the size of the buffer.
5745 %I -- like %i, but use k, M, G, etc., to abbreviate.
5746 %p -- print percent of buffer above top of window, or Top, Bot or All.
5747 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5748 or print Bottom or All.
5749 %n -- print Narrow if appropriate.
5750 %t -- visited file is text or binary (if OS supports this distinction).
5751 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5752 %Z -- like %z, but including the end-of-line format.
5753 %e -- print error message about full memory.
5754 %@ -- print @ or hyphen. @ means that default-directory is on a
5755 remote machine.
5756 %[ -- print one [ for each recursive editing level. %] similar.
5757 %% -- print %. %- -- print infinitely many dashes.
5758 Decimal digits after the % specify field width to which to pad. */);
5760 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5761 doc: /* *Value of `major-mode' for new buffers. */);
5763 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5764 make_number (Lisp_Symbol),
5765 doc: /* Symbol for current buffer's major mode.
5766 The default value (normally `fundamental-mode') affects new buffers.
5767 A value of nil means to use the current buffer's major mode, provided
5768 it is not marked as "special".
5770 When a mode is used by default, `find-file' switches to it before it
5771 reads the contents into the buffer and before it finishes setting up
5772 the buffer. Thus, the mode and its hooks should not expect certain
5773 variables such as `buffer-read-only' and `buffer-file-coding-system'
5774 to be set up. */);
5776 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5777 Qnil,
5778 doc: /* Pretty name of current buffer's major mode.
5779 Usually a string, but can use any of the constructs for `mode-line-format',
5780 which see.
5781 Format with `format-mode-line' to produce a string value. */);
5783 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5784 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5786 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5787 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5789 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5790 Qnil,
5791 doc: /* *Non-nil if searches and matches should ignore case. */);
5793 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5794 make_number (LISP_INT_TAG),
5795 doc: /* *Column beyond which automatic line-wrapping should happen.
5796 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5798 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5799 make_number (LISP_INT_TAG),
5800 doc: /* *Column for the default `indent-line-function' to indent to.
5801 Linefeed indents to this column in Fundamental mode. */);
5803 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5804 make_number (LISP_INT_TAG),
5805 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5807 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5808 doc: /* *Non-nil means display control chars with uparrow.
5809 A value of nil means use backslash and octal digits.
5810 This variable does not apply to characters whose display is specified
5811 in the current display table (if there is one). */);
5813 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5814 &current_buffer->enable_multibyte_characters,
5815 Qnil,
5816 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5817 Otherwise they are regarded as unibyte. This affects the display,
5818 file I/O and the behavior of various editing commands.
5820 This variable is buffer-local but you cannot set it directly;
5821 use the function `set-buffer-multibyte' to change a buffer's representation.
5822 Changing its default value with `setq-default' is supported.
5823 See also variable `default-enable-multibyte-characters' and Info node
5824 `(elisp)Text Representations'. */);
5825 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5827 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5828 &current_buffer->buffer_file_coding_system, Qnil,
5829 doc: /* Coding system to be used for encoding the buffer contents on saving.
5830 This variable applies to saving the buffer, and also to `write-region'
5831 and other functions that use `write-region'.
5832 It does not apply to sending output to subprocesses, however.
5834 If this is nil, the buffer is saved without any code conversion
5835 unless some coding system is specified in `file-coding-system-alist'
5836 for the buffer file.
5838 If the text to be saved cannot be encoded as specified by this variable,
5839 an alternative encoding is selected by `select-safe-coding-system', which see.
5841 The variable `coding-system-for-write', if non-nil, overrides this variable.
5843 This variable is never applied to a way of decoding a file while reading it. */);
5845 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5846 Qnil,
5847 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5849 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5850 doc: /* *Non-nil means do not display continuation lines.
5851 Instead, give each line of text just one screen line.
5853 Note that this is overridden by the variable
5854 `truncate-partial-width-windows' if that variable is non-nil
5855 and this buffer is not full-frame width. */);
5857 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5858 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5859 When word-wrapping is on, continuation lines are wrapped at the space
5860 or tab character nearest to the right window edge.
5861 If nil, continuation lines are wrapped at the right screen edge.
5863 This variable has no effect if long lines are truncated (see
5864 `truncate-lines' and `truncate-partial-width-windows'). If you use
5865 word-wrapping, you might want to reduce the value of
5866 `truncate-partial-width-windows', since wrapping can make text readable
5867 in narrower windows. */);
5869 #ifdef DOS_NT
5870 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5871 Qnil,
5872 doc: /* Non-nil if the visited file is a binary file.
5873 This variable is meaningful on MS-DOG and Windows NT.
5874 On those systems, it is automatically local in every buffer.
5875 On other systems, this variable is normally always nil. */);
5876 #endif
5878 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5879 make_number (Lisp_String),
5880 doc: /* Name of default directory of current buffer. Should end with slash.
5881 To interactively change the default directory, use command `cd'. */);
5883 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5884 Qnil,
5885 doc: /* Function called (if non-nil) to perform auto-fill.
5886 It is called after self-inserting any character specified in
5887 the `auto-fill-chars' table.
5888 NOTE: This variable is not a hook;
5889 its value may not be a list of functions. */);
5891 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5892 make_number (Lisp_String),
5893 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5895 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5896 make_number (Lisp_String),
5897 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5898 The truename of a file is calculated by `file-truename'
5899 and then abbreviated with `abbreviate-file-name'. */);
5901 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5902 &current_buffer->auto_save_file_name,
5903 make_number (Lisp_String),
5904 doc: /* Name of file for auto-saving current buffer.
5905 If it is nil, that means don't auto-save this buffer. */);
5907 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5908 doc: /* Non-nil if this buffer is read-only. */);
5910 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5911 doc: /* Non-nil if this buffer's file has been backed up.
5912 Backing up is done before the first time the file is saved. */);
5914 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5915 make_number (LISP_INT_TAG),
5916 doc: /* Length of current buffer when last read in, saved or auto-saved.
5917 0 initially.
5918 -1 means auto-saving turned off until next real save.
5920 If you set this to -2, that means don't turn off auto-saving in this buffer
5921 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5922 you probably should set this to -2 in that buffer. */);
5924 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5925 Qnil,
5926 doc: /* Non-nil enables selective display.
5927 An integer N as value means display only lines
5928 that start with less than N columns of space.
5929 A value of t means that the character ^M makes itself and
5930 all the rest of the line invisible; also, when saving the buffer
5931 in a file, save the ^M as a newline. */);
5933 #ifndef old
5934 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5935 &current_buffer->selective_display_ellipses,
5936 Qnil,
5937 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5938 #endif
5940 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5941 doc: /* Non-nil if self-insertion should replace existing text.
5942 The value should be one of `overwrite-mode-textual',
5943 `overwrite-mode-binary', or nil.
5944 If it is `overwrite-mode-textual', self-insertion still
5945 inserts at the end of a line, and inserts when point is before a tab,
5946 until the tab is filled in.
5947 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5949 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5950 Qnil,
5951 doc: /* Display table that controls display of the contents of current buffer.
5953 If this variable is nil, the value of `standard-display-table' is used.
5954 Each window can have its own, overriding display table, see
5955 `set-window-display-table' and `window-display-table'.
5957 The display table is a char-table created with `make-display-table'.
5958 A char-table is an array indexed by character codes. Normal array
5959 primitives `aref' and `aset' can be used to access elements of a char-table.
5961 Each of the char-table elements control how to display the corresponding
5962 text character: the element at index C in the table says how to display
5963 the character whose code is C. Each element should be a vector of
5964 characters or nil. The value nil means display the character in the
5965 default fashion; otherwise, the characters from the vector are delivered
5966 to the screen instead of the original character.
5968 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5969 to display a capital Y instead of each X character.
5971 In addition, a char-table has six extra slots to control the display of:
5973 the end of a truncated screen line (extra-slot 0, a single character);
5974 the end of a continued line (extra-slot 1, a single character);
5975 the escape character used to display character codes in octal
5976 (extra-slot 2, a single character);
5977 the character used as an arrow for control characters (extra-slot 3,
5978 a single character);
5979 the decoration indicating the presence of invisible lines (extra-slot 4,
5980 a vector of characters);
5981 the character used to draw the border between side-by-side windows
5982 (extra-slot 5, a single character).
5984 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5986 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5987 Qnil,
5988 doc: /* *Width of left marginal area for display of a buffer.
5989 A value of nil means no marginal area. */);
5991 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5992 Qnil,
5993 doc: /* *Width of right marginal area for display of a buffer.
5994 A value of nil means no marginal area. */);
5996 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5997 Qnil,
5998 doc: /* *Width of this buffer's left fringe (in pixels).
5999 A value of 0 means no left fringe is shown in this buffer's window.
6000 A value of nil means to use the left fringe width from the window's frame. */);
6002 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
6003 Qnil,
6004 doc: /* *Width of this buffer's right fringe (in pixels).
6005 A value of 0 means no right fringe is shown in this buffer's window.
6006 A value of nil means to use the right fringe width from the window's frame. */);
6008 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
6009 Qnil,
6010 doc: /* *Non-nil means to display fringes outside display margins.
6011 A value of nil means to display fringes between margins and buffer text. */);
6013 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
6014 Qnil,
6015 doc: /* *Width of this buffer's scroll bars in pixels.
6016 A value of nil means to use the scroll bar width from the window's frame. */);
6018 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
6019 Qnil,
6020 doc: /* *Position of this buffer's vertical scroll bar.
6021 The value takes effect whenever you tell a window to display this buffer;
6022 for instance, with `set-window-buffer' or when `display-buffer' displays it.
6024 A value of `left' or `right' means put the vertical scroll bar at that side
6025 of the window; a value of nil means don't show any vertical scroll bars.
6026 A value of t (the default) means do whatever the window's frame specifies. */);
6028 DEFVAR_PER_BUFFER ("indicate-empty-lines",
6029 &current_buffer->indicate_empty_lines, Qnil,
6030 doc: /* *Visually indicate empty lines after the buffer end.
6031 If non-nil, a bitmap is displayed in the left fringe of a window on
6032 window-systems. */);
6034 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
6035 &current_buffer->indicate_buffer_boundaries, Qnil,
6036 doc: /* *Visually indicate buffer boundaries and scrolling.
6037 If non-nil, the first and last line of the buffer are marked in the fringe
6038 of a window on window-systems with angle bitmaps, or if the window can be
6039 scrolled, the top and bottom line of the window are marked with up and down
6040 arrow bitmaps.
6042 If value is a symbol `left' or `right', both angle and arrow bitmaps
6043 are displayed in the left or right fringe, resp. Any other value
6044 that doesn't look like an alist means display the angle bitmaps in
6045 the left fringe but no arrows.
6047 You can exercise more precise control by using an alist as the
6048 value. Each alist element (INDICATOR . POSITION) specifies
6049 where to show one of the indicators. INDICATOR is one of `top',
6050 `bottom', `up', `down', or t, which specifies the default position,
6051 and POSITION is one of `left', `right', or nil, meaning do not show
6052 this indicator.
6054 For example, ((top . left) (t . right)) places the top angle bitmap in
6055 left fringe, the bottom angle bitmap in right fringe, and both arrow
6056 bitmaps in right fringe. To show just the angle bitmaps in the left
6057 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6059 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
6060 &current_buffer->fringe_indicator_alist, Qnil,
6061 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
6062 The value is an alist where each element (INDICATOR . BITMAPS)
6063 specifies the fringe bitmaps used to display a specific logical
6064 fringe indicator.
6066 INDICATOR specifies the logical indicator type which is one of the
6067 following symbols: `truncation' , `continuation', `overlay-arrow',
6068 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
6070 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
6071 the actual bitmap shown in the left or right fringe for the logical
6072 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
6073 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
6074 are used only for the `bottom' and `one-line' indicators when the last
6075 \(only) line in has no final newline. BITMAPS may also be a single
6076 symbol which is used in both left and right fringes. */);
6078 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
6079 &current_buffer->fringe_cursor_alist, Qnil,
6080 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
6081 The value is an alist where each element (CURSOR . BITMAP)
6082 specifies the fringe bitmaps used to display a specific logical
6083 cursor type in the fringe.
6085 CURSOR specifies the logical cursor type which is one of the following
6086 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6087 one is used to show a hollow cursor on narrow lines display lines
6088 where the normal hollow cursor will not fit.
6090 BITMAP is the corresponding fringe bitmap shown for the logical
6091 cursor type. */);
6093 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
6094 &current_buffer->scroll_up_aggressively, Qnil,
6095 doc: /* How far to scroll windows upward.
6096 If you move point off the bottom, the window scrolls automatically.
6097 This variable controls how far it scrolls. The value nil, the default,
6098 means scroll to center point. A fraction means scroll to put point
6099 that fraction of the window's height from the bottom of the window.
6100 When the value is 0.0, point goes at the bottom line, which in the
6101 simple case that you moved off with C-f means scrolling just one line.
6102 1.0 means point goes at the top, so that in that simple case, the
6103 window scrolls by a full window height. Meaningful values are
6104 between 0.0 and 1.0, inclusive. */);
6106 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6107 &current_buffer->scroll_down_aggressively, Qnil,
6108 doc: /* How far to scroll windows downward.
6109 If you move point off the top, the window scrolls automatically.
6110 This variable controls how far it scrolls. The value nil, the default,
6111 means scroll to center point. A fraction means scroll to put point
6112 that fraction of the window's height from the top of the window.
6113 When the value is 0.0, point goes at the top line, which in the
6114 simple case that you moved off with C-b means scrolling just one line.
6115 1.0 means point goes at the bottom, so that in that simple case, the
6116 window scrolls by a full window height. Meaningful values are
6117 between 0.0 and 1.0, inclusive. */);
6119 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6120 "Don't ask.");
6123 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6124 doc: /* List of functions to call before each text change.
6125 Two arguments are passed to each function: the positions of
6126 the beginning and end of the range of old text to be changed.
6127 \(For an insertion, the beginning and end are at the same place.)
6128 No information is given about the length of the text after the change.
6130 Buffer changes made while executing the `before-change-functions'
6131 don't call any before-change or after-change functions.
6132 That's because these variables are temporarily set to nil.
6133 As a result, a hook function cannot straightforwardly alter the
6134 value of these variables. See the Emacs Lisp manual for a way of
6135 accomplishing an equivalent result by using other variables.
6137 If an unhandled error happens in running these functions,
6138 the variable's value remains nil. That prevents the error
6139 from happening repeatedly and making Emacs nonfunctional. */);
6140 Vbefore_change_functions = Qnil;
6142 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6143 doc: /* List of functions to call after each text change.
6144 Three arguments are passed to each function: the positions of
6145 the beginning and end of the range of changed text,
6146 and the length in bytes of the pre-change text replaced by that range.
6147 \(For an insertion, the pre-change length is zero;
6148 for a deletion, that length is the number of bytes deleted,
6149 and the post-change beginning and end are at the same place.)
6151 Buffer changes made while executing the `after-change-functions'
6152 don't call any before-change or after-change functions.
6153 That's because these variables are temporarily set to nil.
6154 As a result, a hook function cannot straightforwardly alter the
6155 value of these variables. See the Emacs Lisp manual for a way of
6156 accomplishing an equivalent result by using other variables.
6158 If an unhandled error happens in running these functions,
6159 the variable's value remains nil. That prevents the error
6160 from happening repeatedly and making Emacs nonfunctional. */);
6161 Vafter_change_functions = Qnil;
6163 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6164 doc: /* A list of functions to call before changing a buffer which is unmodified.
6165 The functions are run using the `run-hooks' function. */);
6166 Vfirst_change_hook = Qnil;
6168 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6169 doc: /* List of undo entries in current buffer.
6170 Recent changes come first; older changes follow newer.
6172 An entry (BEG . END) represents an insertion which begins at
6173 position BEG and ends at position END.
6175 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6176 from (abs POSITION). If POSITION is positive, point was at the front
6177 of the text being deleted; if negative, point was at the end.
6179 An entry (t HIGH . LOW) indicates that the buffer previously had
6180 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6181 of the visited file's modification time, as of that time. If the
6182 modification time of the most recent save is different, this entry is
6183 obsolete.
6185 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6186 was modified between BEG and END. PROPERTY is the property name,
6187 and VALUE is the old value.
6189 An entry (apply FUN-NAME . ARGS) means undo the change with
6190 \(apply FUN-NAME ARGS).
6192 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6193 in the active region. BEG and END is the range affected by this entry
6194 and DELTA is the number of bytes added or deleted in that range by
6195 this change.
6197 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6198 was adjusted in position by the offset DISTANCE (an integer).
6200 An entry of the form POSITION indicates that point was at the buffer
6201 location given by the integer. Undoing an entry of this form places
6202 point at POSITION.
6204 Entries with value `nil' mark undo boundaries. The undo command treats
6205 the changes between two undo boundaries as a single step to be undone.
6207 If the value of the variable is t, undo information is not recorded. */);
6209 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6210 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6212 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6213 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6215 Normally, the line-motion functions work by scanning the buffer for
6216 newlines. Columnar operations (like `move-to-column' and
6217 `compute-motion') also work by scanning the buffer, summing character
6218 widths as they go. This works well for ordinary text, but if the
6219 buffer's lines are very long (say, more than 500 characters), these
6220 motion functions will take longer to execute. Emacs may also take
6221 longer to update the display.
6223 If `cache-long-line-scans' is non-nil, these motion functions cache the
6224 results of their scans, and consult the cache to avoid rescanning
6225 regions of the buffer until the text is modified. The caches are most
6226 beneficial when they prevent the most searching---that is, when the
6227 buffer contains long lines and large regions of characters with the
6228 same, fixed screen width.
6230 When `cache-long-line-scans' is non-nil, processing short lines will
6231 become slightly slower (because of the overhead of consulting the
6232 cache), and the caches will use memory roughly proportional to the
6233 number of newlines and characters whose screen width varies.
6235 The caches require no explicit maintenance; their accuracy is
6236 maintained internally by the Emacs primitives. Enabling or disabling
6237 the cache should not affect the behavior of any of the motion
6238 functions; it should only affect their performance. */);
6240 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6241 doc: /* Value of point before the last series of scroll operations, or nil. */);
6243 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6244 doc: /* List of formats to use when saving this buffer.
6245 Formats are defined by `format-alist'. This variable is
6246 set when a file is visited. */);
6248 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6249 &current_buffer->auto_save_file_format, Qnil,
6250 doc: /* *Format in which to write auto-save files.
6251 Should be a list of symbols naming formats that are defined in `format-alist'.
6252 If it is t, which is the default, auto-save files are written in the
6253 same format as a regular save would use. */);
6255 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6256 &current_buffer->invisibility_spec, Qnil,
6257 doc: /* Invisibility spec of this buffer.
6258 The default is t, which means that text is invisible
6259 if it has a non-nil `invisible' property.
6260 If the value is a list, a text character is invisible if its `invisible'
6261 property is an element in that list (or is a list with members in common).
6262 If an element is a cons cell of the form (PROP . ELLIPSIS),
6263 then characters with property value PROP are invisible,
6264 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6266 DEFVAR_PER_BUFFER ("buffer-display-count",
6267 &current_buffer->display_count, Qnil,
6268 doc: /* A number incremented each time this buffer is displayed in a window.
6269 The function `set-window-buffer' increments it. */);
6271 DEFVAR_PER_BUFFER ("buffer-display-time",
6272 &current_buffer->display_time, Qnil,
6273 doc: /* Time stamp updated each time this buffer is displayed in a window.
6274 The function `set-window-buffer' updates this variable
6275 to the value obtained by calling `current-time'.
6276 If the buffer has never been shown in a window, the value is nil. */);
6278 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6279 doc: /* */);
6280 Vtransient_mark_mode = Qnil;
6281 /* The docstring is in simple.el. If we put it here, it would be
6282 overwritten when transient-mark-mode is defined using
6283 define-minor-mode. */
6285 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6286 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6287 If the value is t, disregard `buffer-read-only' and all `read-only'
6288 text properties. If the value is a list, disregard `buffer-read-only'
6289 and disregard a `read-only' text property if the property value
6290 is a member of the list. */);
6291 Vinhibit_read_only = Qnil;
6293 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6294 doc: /* Cursor to use when this buffer is in the selected window.
6295 Values are interpreted as follows:
6297 t use the cursor specified for the frame
6298 nil don't display a cursor
6299 box display a filled box cursor
6300 hollow display a hollow box cursor
6301 bar display a vertical bar cursor with default width
6302 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6303 hbar display a horizontal bar cursor with default height
6304 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6305 ANYTHING ELSE display a hollow box cursor
6307 When the buffer is displayed in a non-selected window, the
6308 cursor's appearance is instead controlled by the variable
6309 `cursor-in-non-selected-windows'. */);
6311 DEFVAR_PER_BUFFER ("line-spacing",
6312 &current_buffer->extra_line_spacing, Qnil,
6313 doc: /* Additional space to put between lines when displaying a buffer.
6314 The space is measured in pixels, and put below lines on graphic displays,
6315 see `display-graphic-p'.
6316 If value is a floating point number, it specifies the spacing relative
6317 to the default frame line height. A value of nil means add no extra space. */);
6319 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6320 &current_buffer->cursor_in_non_selected_windows, Qnil,
6321 doc: /* *Cursor type to display in non-selected windows.
6322 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6324 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6325 doc: /* List of functions called with no args to query before killing a buffer.
6326 The buffer being killed will be current while the functions are running.
6327 If any of them returns nil, the buffer is not killed. */);
6328 Vkill_buffer_query_functions = Qnil;
6330 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6331 doc: /* Normal hook run before changing the major mode of a buffer.
6332 The function `kill-all-local-variables' runs this before doing anything else. */);
6333 Vchange_major_mode_hook = Qnil;
6334 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6335 staticpro (&Qchange_major_mode_hook);
6337 defsubr (&Sbuffer_live_p);
6338 defsubr (&Sbuffer_list);
6339 defsubr (&Sget_buffer);
6340 defsubr (&Sget_file_buffer);
6341 defsubr (&Sget_buffer_create);
6342 defsubr (&Smake_indirect_buffer);
6343 defsubr (&Sgenerate_new_buffer_name);
6344 defsubr (&Sbuffer_name);
6345 /*defsubr (&Sbuffer_number);*/
6346 defsubr (&Sbuffer_file_name);
6347 defsubr (&Sbuffer_base_buffer);
6348 defsubr (&Sbuffer_local_value);
6349 defsubr (&Sbuffer_local_variables);
6350 defsubr (&Sbuffer_modified_p);
6351 defsubr (&Sset_buffer_modified_p);
6352 defsubr (&Sbuffer_modified_tick);
6353 defsubr (&Sbuffer_chars_modified_tick);
6354 defsubr (&Srename_buffer);
6355 defsubr (&Sother_buffer);
6356 defsubr (&Sbuffer_enable_undo);
6357 defsubr (&Skill_buffer);
6358 defsubr (&Sset_buffer_major_mode);
6359 defsubr (&Sswitch_to_buffer);
6360 defsubr (&Scurrent_buffer);
6361 defsubr (&Sset_buffer);
6362 defsubr (&Sbarf_if_buffer_read_only);
6363 defsubr (&Sbury_buffer);
6364 defsubr (&Serase_buffer);
6365 defsubr (&Sbuffer_swap_text);
6366 defsubr (&Sset_buffer_multibyte);
6367 defsubr (&Skill_all_local_variables);
6369 defsubr (&Soverlayp);
6370 defsubr (&Smake_overlay);
6371 defsubr (&Sdelete_overlay);
6372 defsubr (&Smove_overlay);
6373 defsubr (&Soverlay_start);
6374 defsubr (&Soverlay_end);
6375 defsubr (&Soverlay_buffer);
6376 defsubr (&Soverlay_properties);
6377 defsubr (&Soverlays_at);
6378 defsubr (&Soverlays_in);
6379 defsubr (&Snext_overlay_change);
6380 defsubr (&Sprevious_overlay_change);
6381 defsubr (&Soverlay_recenter);
6382 defsubr (&Soverlay_lists);
6383 defsubr (&Soverlay_get);
6384 defsubr (&Soverlay_put);
6385 defsubr (&Srestore_buffer_modified_p);
6388 void
6389 keys_of_buffer ()
6391 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6392 initial_define_key (control_x_map, 'k', "kill-buffer");
6394 /* This must not be in syms_of_buffer, because Qdisabled is not
6395 initialized when that function gets called. */
6396 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6399 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6400 (do not change this comment) */