(Button Types): For define-button-type, clarify type of NAME.
[emacs.git] / src / buffer.c
blobcf125559f76b9063f484d7ed30fb368a31ba01cd
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
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, or (at your option)
12 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; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include <config.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/param.h>
29 #include <errno.h>
30 #include <stdio.h>
32 #ifndef USE_CRT_DLL
33 extern int errno;
34 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #include "lisp.h"
42 #include "intervals.h"
43 #include "window.h"
44 #include "commands.h"
45 #include "buffer.h"
46 #include "charset.h"
47 #include "region-cache.h"
48 #include "indent.h"
49 #include "blockinput.h"
50 #include "keyboard.h"
51 #include "keymap.h"
52 #include "frame.h"
54 struct buffer *current_buffer; /* the current buffer */
56 /* First buffer in chain of all buffers (in reverse order of creation).
57 Threaded through ->next. */
59 struct buffer *all_buffers;
61 /* This structure holds the default values of the buffer-local variables
62 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
63 The default value occupies the same slot in this structure
64 as an individual buffer's value occupies in that buffer.
65 Setting the default value also goes through the alist of buffers
66 and stores into each buffer that does not say it has a local value. */
68 DECL_ALIGN (struct buffer, buffer_defaults);
70 /* A Lisp_Object pointer to the above, used for staticpro */
72 static Lisp_Object Vbuffer_defaults;
74 /* This structure marks which slots in a buffer have corresponding
75 default values in buffer_defaults.
76 Each such slot has a nonzero value in this structure.
77 The value has only one nonzero bit.
79 When a buffer has its own local value for a slot,
80 the entry for that slot (found in the same slot in this structure)
81 is turned on in the buffer's local_flags array.
83 If a slot in this structure is -1, then even though there may
84 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
85 and the corresponding slot in buffer_defaults is not used.
87 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
88 but there is a default value which is copied into each buffer.
90 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91 zero, that is a bug */
93 struct buffer buffer_local_flags;
95 /* This structure holds the names of symbols whose values may be
96 buffer-local. It is indexed and accessed in the same way as the above. */
98 DECL_ALIGN (struct buffer, buffer_local_symbols);
100 /* A Lisp_Object pointer to the above, used for staticpro */
101 static Lisp_Object Vbuffer_local_symbols;
103 /* Flags indicating which built-in buffer-local variables
104 are permanent locals. */
105 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
107 /* Number of per-buffer variables used. */
109 int last_per_buffer_idx;
111 EXFUN (Fset_buffer, 1);
112 void set_buffer_internal P_ ((struct buffer *b));
113 void set_buffer_internal_1 P_ ((struct buffer *b));
114 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
115 int after, Lisp_Object arg1,
116 Lisp_Object arg2, Lisp_Object arg3));
117 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
118 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
120 /* Alist of all buffer names vs the buffers. */
121 /* This used to be a variable, but is no longer,
122 to prevent lossage due to user rplac'ing this alist or its elements. */
123 Lisp_Object Vbuffer_alist;
125 /* Functions to call before and after each text change. */
126 Lisp_Object Vbefore_change_functions;
127 Lisp_Object Vafter_change_functions;
129 Lisp_Object Vtransient_mark_mode;
131 /* t means ignore all read-only text properties.
132 A list means ignore such a property if its value is a member of the list.
133 Any non-nil value means ignore buffer-read-only. */
134 Lisp_Object Vinhibit_read_only;
136 /* List of functions to call that can query about killing a buffer.
137 If any of these functions returns nil, we don't kill it. */
138 Lisp_Object Vkill_buffer_query_functions;
139 Lisp_Object Qkill_buffer_query_functions;
141 /* Hook run before changing a major mode. */
142 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
144 /* List of functions to call before changing an unmodified buffer. */
145 Lisp_Object Vfirst_change_hook;
147 Lisp_Object Qfirst_change_hook;
148 Lisp_Object Qbefore_change_functions;
149 Lisp_Object Qafter_change_functions;
150 Lisp_Object Qucs_set_table_for_input;
152 /* If nonzero, all modification hooks are suppressed. */
153 int inhibit_modification_hooks;
155 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
156 Lisp_Object Qpermanent_local_hook;
158 Lisp_Object Qprotected_field;
160 Lisp_Object QSFundamental; /* A string "Fundamental" */
162 Lisp_Object Qkill_buffer_hook;
164 Lisp_Object Qget_file_buffer;
166 Lisp_Object Qoverlayp;
168 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
170 Lisp_Object Qmodification_hooks;
171 Lisp_Object Qinsert_in_front_hooks;
172 Lisp_Object Qinsert_behind_hooks;
174 static void alloc_buffer_text P_ ((struct buffer *, size_t));
175 static void free_buffer_text P_ ((struct buffer *b));
176 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
177 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
178 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
181 /* For debugging; temporary. See set_buffer_internal. */
182 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
184 void
185 nsberror (spec)
186 Lisp_Object spec;
188 if (STRINGP (spec))
189 error ("No buffer named %s", SDATA (spec));
190 error ("Invalid buffer argument");
193 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
194 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
195 Value is nil if OBJECT is not a buffer or if it has been killed. */)
196 (object)
197 Lisp_Object object;
199 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
200 ? Qt : Qnil);
203 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
204 doc: /* Return a list of all existing live buffers.
205 If the optional arg FRAME is a frame, we return the buffer list
206 in the proper order for that frame: the buffers in FRAME's `buffer-list'
207 frame parameter come first, followed by the rest of the buffers. */)
208 (frame)
209 Lisp_Object frame;
211 Lisp_Object general;
212 general = Fmapcar (Qcdr, Vbuffer_alist);
214 if (FRAMEP (frame))
216 Lisp_Object framelist, prevlist, tail;
217 Lisp_Object args[3];
219 CHECK_FRAME (frame);
221 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
222 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
224 /* Remove from GENERAL any buffer that duplicates one in
225 FRAMELIST or PREVLIST. */
226 tail = framelist;
227 while (CONSP (tail))
229 general = Fdelq (XCAR (tail), general);
230 tail = XCDR (tail);
232 tail = prevlist;
233 while (CONSP (tail))
235 general = Fdelq (XCAR (tail), general);
236 tail = XCDR (tail);
239 args[0] = framelist;
240 args[1] = general;
241 args[2] = prevlist;
242 return Fnconc (3, args);
245 return general;
248 /* Like Fassoc, but use Fstring_equal to compare
249 (which ignores text properties),
250 and don't ever QUIT. */
252 static Lisp_Object
253 assoc_ignore_text_properties (key, list)
254 register Lisp_Object key;
255 Lisp_Object list;
257 register Lisp_Object tail;
258 for (tail = list; CONSP (tail); tail = XCDR (tail))
260 register Lisp_Object elt, tem;
261 elt = XCAR (tail);
262 tem = Fstring_equal (Fcar (elt), key);
263 if (!NILP (tem))
264 return elt;
266 return Qnil;
269 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
270 doc: /* Return the buffer named NAME (a string).
271 If there is no live buffer named NAME, return nil.
272 NAME may also be a buffer; if so, the value is that buffer. */)
273 (name)
274 register Lisp_Object name;
276 if (BUFFERP (name))
277 return name;
278 CHECK_STRING (name);
280 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
283 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
284 doc: /* Return the buffer visiting file FILENAME (a string).
285 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
286 If there is no such live buffer, return nil.
287 See also `find-buffer-visiting'. */)
288 (filename)
289 register Lisp_Object filename;
291 register Lisp_Object tail, buf, tem;
292 Lisp_Object handler;
294 CHECK_STRING (filename);
295 filename = Fexpand_file_name (filename, Qnil);
297 /* If the file name has special constructs in it,
298 call the corresponding file handler. */
299 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
300 if (!NILP (handler))
301 return call2 (handler, Qget_file_buffer, filename);
303 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
305 buf = Fcdr (XCAR (tail));
306 if (!BUFFERP (buf)) continue;
307 if (!STRINGP (XBUFFER (buf)->filename)) continue;
308 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
309 if (!NILP (tem))
310 return buf;
312 return Qnil;
315 Lisp_Object
316 get_truename_buffer (filename)
317 register Lisp_Object filename;
319 register Lisp_Object tail, buf, tem;
321 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
323 buf = Fcdr (XCAR (tail));
324 if (!BUFFERP (buf)) continue;
325 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
326 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
327 if (!NILP (tem))
328 return buf;
330 return Qnil;
333 /* Incremented for each buffer created, to assign the buffer number. */
334 int buffer_count;
336 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
337 doc: /* Return the buffer named NAME, or create such a buffer and return it.
338 A new buffer is created if there is no live buffer named NAME.
339 If NAME starts with a space, the new buffer does not keep undo information.
340 If NAME is a buffer instead of a string, then it is the value returned.
341 The value is never nil. */)
342 (name)
343 register Lisp_Object name;
345 register Lisp_Object buf;
346 register struct buffer *b;
348 buf = Fget_buffer (name);
349 if (!NILP (buf))
350 return buf;
352 if (SCHARS (name) == 0)
353 error ("Empty string for buffer name is not allowed");
355 b = allocate_buffer ();
357 /* An ordinary buffer uses its own struct buffer_text. */
358 b->text = &b->own_text;
359 b->base_buffer = 0;
361 BUF_GAP_SIZE (b) = 20;
362 BLOCK_INPUT;
363 /* We allocate extra 1-byte at the tail and keep it always '\0' for
364 anchoring a search. */
365 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
366 UNBLOCK_INPUT;
367 if (! BUF_BEG_ADDR (b))
368 buffer_memory_full ();
370 BUF_PT (b) = BEG;
371 BUF_GPT (b) = BEG;
372 BUF_BEGV (b) = BEG;
373 BUF_ZV (b) = BEG;
374 BUF_Z (b) = BEG;
375 BUF_PT_BYTE (b) = BEG_BYTE;
376 BUF_GPT_BYTE (b) = BEG_BYTE;
377 BUF_BEGV_BYTE (b) = BEG_BYTE;
378 BUF_ZV_BYTE (b) = BEG_BYTE;
379 BUF_Z_BYTE (b) = BEG_BYTE;
380 BUF_MODIFF (b) = 1;
381 BUF_CHARS_MODIFF (b) = 1;
382 BUF_OVERLAY_MODIFF (b) = 1;
383 BUF_SAVE_MODIFF (b) = 1;
384 BUF_INTERVALS (b) = 0;
385 BUF_UNCHANGED_MODIFIED (b) = 1;
386 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
387 BUF_END_UNCHANGED (b) = 0;
388 BUF_BEG_UNCHANGED (b) = 0;
389 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
391 b->newline_cache = 0;
392 b->width_run_cache = 0;
393 b->width_table = Qnil;
394 b->prevent_redisplay_optimizations_p = 1;
396 /* Put this on the chain of all buffers including killed ones. */
397 b->next = all_buffers;
398 all_buffers = b;
400 /* An ordinary buffer normally doesn't need markers
401 to handle BEGV and ZV. */
402 b->pt_marker = Qnil;
403 b->begv_marker = Qnil;
404 b->zv_marker = Qnil;
406 name = Fcopy_sequence (name);
407 STRING_SET_INTERVALS (name, NULL_INTERVAL);
408 b->name = name;
410 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
412 reset_buffer (b);
413 reset_buffer_local_variables (b, 1);
415 b->mark = Fmake_marker ();
416 BUF_MARKERS (b) = NULL;
417 b->name = name;
419 /* Put this in the alist of all live buffers. */
420 XSETBUFFER (buf, b);
421 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
423 /* An error in calling the function here (should someone redefine it)
424 can lead to infinite regress until you run out of stack. rms
425 says that's not worth protecting against. */
426 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
427 /* buf is on buffer-alist, so no gcpro. */
428 call1 (Qucs_set_table_for_input, buf);
430 return buf;
434 /* Return a list of overlays which is a copy of the overlay list
435 LIST, but for buffer B. */
437 static struct Lisp_Overlay *
438 copy_overlays (b, list)
439 struct buffer *b;
440 struct Lisp_Overlay *list;
442 Lisp_Object buffer;
443 struct Lisp_Overlay *result = NULL, *tail = NULL;
445 XSETBUFFER (buffer, b);
447 for (; list; list = list->next)
449 Lisp_Object overlay, start, end, old_overlay;
450 EMACS_INT charpos;
452 XSETMISC (old_overlay, list);
453 charpos = marker_position (OVERLAY_START (old_overlay));
454 start = Fmake_marker ();
455 Fset_marker (start, make_number (charpos), buffer);
456 XMARKER (start)->insertion_type
457 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
459 charpos = marker_position (OVERLAY_END (old_overlay));
460 end = Fmake_marker ();
461 Fset_marker (end, make_number (charpos), buffer);
462 XMARKER (end)->insertion_type
463 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
465 overlay = allocate_misc ();
466 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
467 OVERLAY_START (overlay) = start;
468 OVERLAY_END (overlay) = end;
469 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
470 XOVERLAY (overlay)->next = NULL;
472 if (tail)
473 tail = tail->next = XOVERLAY (overlay);
474 else
475 result = tail = XOVERLAY (overlay);
478 return result;
482 /* Clone per-buffer values of buffer FROM.
484 Buffer TO gets the same per-buffer values as FROM, with the
485 following exceptions: (1) TO's name is left untouched, (2) markers
486 are copied and made to refer to TO, and (3) overlay lists are
487 copied. */
489 static void
490 clone_per_buffer_values (from, to)
491 struct buffer *from, *to;
493 Lisp_Object to_buffer;
494 int offset;
496 XSETBUFFER (to_buffer, to);
498 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
499 offset < sizeof *to;
500 offset += sizeof (Lisp_Object))
502 Lisp_Object obj;
504 obj = PER_BUFFER_VALUE (from, offset);
505 if (MARKERP (obj))
507 struct Lisp_Marker *m = XMARKER (obj);
508 obj = Fmake_marker ();
509 XMARKER (obj)->insertion_type = m->insertion_type;
510 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
513 PER_BUFFER_VALUE (to, offset) = obj;
516 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
518 to->overlays_before = copy_overlays (to, from->overlays_before);
519 to->overlays_after = copy_overlays (to, from->overlays_after);
521 /* Get (a copy of) the alist of Lisp-level local variables of FROM
522 and install that in TO. */
523 to->local_var_alist = buffer_lisp_local_variables (from);
526 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
527 2, 3,
528 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
529 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
530 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
531 NAME should be a string which is not the name of an existing buffer.
532 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
533 such as major and minor modes, in the indirect buffer.
534 CLONE nil means the indirect buffer's state is reset to default values. */)
535 (base_buffer, name, clone)
536 Lisp_Object base_buffer, name, clone;
538 Lisp_Object buf, tem;
539 struct buffer *b;
541 CHECK_STRING (name);
542 buf = Fget_buffer (name);
543 if (!NILP (buf))
544 error ("Buffer name `%s' is in use", SDATA (name));
546 tem = base_buffer;
547 base_buffer = Fget_buffer (base_buffer);
548 if (NILP (base_buffer))
549 error ("No such buffer: `%s'", SDATA (tem));
550 if (NILP (XBUFFER (base_buffer)->name))
551 error ("Base buffer has been killed");
553 if (SCHARS (name) == 0)
554 error ("Empty string for buffer name is not allowed");
556 b = allocate_buffer ();
558 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
559 ? XBUFFER (base_buffer)->base_buffer
560 : XBUFFER (base_buffer));
562 /* Use the base buffer's text object. */
563 b->text = b->base_buffer->text;
565 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
566 BUF_ZV (b) = BUF_ZV (b->base_buffer);
567 BUF_PT (b) = BUF_PT (b->base_buffer);
568 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
569 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
570 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
572 b->newline_cache = 0;
573 b->width_run_cache = 0;
574 b->width_table = Qnil;
576 /* Put this on the chain of all buffers including killed ones. */
577 b->next = all_buffers;
578 all_buffers = b;
580 name = Fcopy_sequence (name);
581 STRING_SET_INTERVALS (name, NULL_INTERVAL);
582 b->name = name;
584 reset_buffer (b);
585 reset_buffer_local_variables (b, 1);
587 /* Put this in the alist of all live buffers. */
588 XSETBUFFER (buf, b);
589 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
591 b->mark = Fmake_marker ();
592 b->name = name;
594 /* The multibyte status belongs to the base buffer. */
595 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
597 /* Make sure the base buffer has markers for its narrowing. */
598 if (NILP (b->base_buffer->pt_marker))
600 b->base_buffer->pt_marker = Fmake_marker ();
601 set_marker_both (b->base_buffer->pt_marker, base_buffer,
602 BUF_PT (b->base_buffer),
603 BUF_PT_BYTE (b->base_buffer));
605 if (NILP (b->base_buffer->begv_marker))
607 b->base_buffer->begv_marker = Fmake_marker ();
608 set_marker_both (b->base_buffer->begv_marker, base_buffer,
609 BUF_BEGV (b->base_buffer),
610 BUF_BEGV_BYTE (b->base_buffer));
612 if (NILP (b->base_buffer->zv_marker))
614 b->base_buffer->zv_marker = Fmake_marker ();
615 set_marker_both (b->base_buffer->zv_marker, base_buffer,
616 BUF_ZV (b->base_buffer),
617 BUF_ZV_BYTE (b->base_buffer));
618 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
621 if (NILP (clone))
623 /* Give the indirect buffer markers for its narrowing. */
624 b->pt_marker = Fmake_marker ();
625 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
626 b->begv_marker = Fmake_marker ();
627 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
628 b->zv_marker = Fmake_marker ();
629 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
630 XMARKER (b->zv_marker)->insertion_type = 1;
632 else
634 struct buffer *old_b = current_buffer;
636 clone_per_buffer_values (b->base_buffer, b);
637 b->filename = Qnil;
638 b->file_truename = Qnil;
639 b->display_count = make_number (0);
640 b->backed_up = Qnil;
641 b->auto_save_file_name = Qnil;
642 set_buffer_internal_1 (b);
643 Fset (intern ("buffer-save-without-query"), Qnil);
644 Fset (intern ("buffer-file-number"), Qnil);
645 Fset (intern ("buffer-stale-function"), Qnil);
646 set_buffer_internal_1 (old_b);
649 return buf;
652 void
653 delete_all_overlays (b)
654 struct buffer *b;
656 Lisp_Object overlay;
658 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
659 have to empty the list, otherwise we end up with overlays that
660 think they belong to this buffer while the buffer doesn't know about
661 them any more. */
662 while (b->overlays_before)
664 XSETMISC (overlay, b->overlays_before);
665 Fdelete_overlay (overlay);
667 while (b->overlays_after)
669 XSETMISC (overlay, b->overlays_after);
670 Fdelete_overlay (overlay);
672 eassert (b->overlays_before == NULL);
673 eassert (b->overlays_after == NULL);
676 /* Reinitialize everything about a buffer except its name and contents
677 and local variables.
678 If called on an already-initialized buffer, the list of overlays
679 should be deleted before calling this function, otherwise we end up
680 with overlays that claim to belong to the buffer but the buffer
681 claims it doesn't belong to it. */
683 void
684 reset_buffer (b)
685 register struct buffer *b;
687 b->filename = Qnil;
688 b->file_truename = Qnil;
689 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
690 b->modtime = 0;
691 XSETFASTINT (b->save_length, 0);
692 b->last_window_start = 1;
693 /* It is more conservative to start out "changed" than "unchanged". */
694 b->clip_changed = 0;
695 b->prevent_redisplay_optimizations_p = 1;
696 b->backed_up = Qnil;
697 b->auto_save_modified = 0;
698 b->auto_save_failure_time = -1;
699 b->auto_save_file_name = Qnil;
700 b->read_only = Qnil;
701 b->overlays_before = NULL;
702 b->overlays_after = NULL;
703 b->overlay_center = BEG;
704 b->mark_active = Qnil;
705 b->point_before_scroll = Qnil;
706 b->file_format = Qnil;
707 b->auto_save_file_format = Qt;
708 b->last_selected_window = Qnil;
709 XSETINT (b->display_count, 0);
710 b->display_time = Qnil;
711 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
712 b->cursor_type = buffer_defaults.cursor_type;
713 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
715 b->display_error_modiff = 0;
718 /* Reset buffer B's local variables info.
719 Don't use this on a buffer that has already been in use;
720 it does not treat permanent locals consistently.
721 Instead, use Fkill_all_local_variables.
723 If PERMANENT_TOO is 1, then we reset permanent
724 buffer-local variables. If PERMANENT_TOO is 0,
725 we preserve those. */
727 static void
728 reset_buffer_local_variables (b, permanent_too)
729 register struct buffer *b;
730 int permanent_too;
732 register int offset;
733 int i;
735 /* Reset the major mode to Fundamental, together with all the
736 things that depend on the major mode.
737 default-major-mode is handled at a higher level.
738 We ignore it here. */
739 b->major_mode = Qfundamental_mode;
740 b->keymap = Qnil;
741 b->mode_name = QSFundamental;
742 b->minor_modes = Qnil;
744 /* If the standard case table has been altered and invalidated,
745 fix up its insides first. */
746 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
747 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
748 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
749 Fset_standard_case_table (Vascii_downcase_table);
751 b->downcase_table = Vascii_downcase_table;
752 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
753 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
754 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
755 b->invisibility_spec = Qt;
756 #ifndef DOS_NT
757 b->buffer_file_type = Qnil;
758 #endif
760 /* Reset all (or most) per-buffer variables to their defaults. */
761 if (permanent_too)
762 b->local_var_alist = Qnil;
763 else
765 Lisp_Object tmp, prop, last = Qnil;
766 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
767 if (CONSP (XCAR (tmp))
768 && SYMBOLP (XCAR (XCAR (tmp)))
769 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
771 /* If permanent-local, keep it. */
772 last = tmp;
773 if (EQ (prop, Qpermanent_local_hook))
775 /* This is a partially permanent hook variable.
776 Preserve only the elements that want to be preserved. */
777 Lisp_Object list, newlist;
778 list = XCDR (XCAR (tmp));
779 if (!CONSP (list))
780 newlist = list;
781 else
782 for (newlist = Qnil; CONSP (list); list = XCDR (list))
784 Lisp_Object elt = XCAR (list);
785 /* Preserve element ELT if it's t,
786 if it is a function with a `permanent-local-hook' property,
787 or if it's not a symbol. */
788 if (! SYMBOLP (elt)
789 || EQ (elt, Qt)
790 || !NILP (Fget (elt, Qpermanent_local_hook)))
791 newlist = Fcons (elt, newlist);
793 XSETCDR (XCAR (tmp), Fnreverse (newlist));
796 /* Delete this local variable. */
797 else if (NILP (last))
798 b->local_var_alist = XCDR (tmp);
799 else
800 XSETCDR (last, XCDR (tmp));
803 for (i = 0; i < last_per_buffer_idx; ++i)
804 if (permanent_too || buffer_permanent_local_flags[i] == 0)
805 SET_PER_BUFFER_VALUE_P (b, i, 0);
807 /* For each slot that has a default value,
808 copy that into the slot. */
810 for (offset = PER_BUFFER_VAR_OFFSET (name);
811 offset < sizeof *b;
812 offset += sizeof (Lisp_Object))
814 int idx = PER_BUFFER_IDX (offset);
815 if ((idx > 0
816 && (permanent_too
817 || buffer_permanent_local_flags[idx] == 0))
818 /* Is -2 used anywhere? */
819 || idx == -2)
820 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
824 /* We split this away from generate-new-buffer, because rename-buffer
825 and set-visited-file-name ought to be able to use this to really
826 rename the buffer properly. */
828 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
829 1, 2, 0,
830 doc: /* Return a string that is the name of no existing buffer based on NAME.
831 If there is no live buffer named NAME, then return NAME.
832 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
833 \(starting at 2) until an unused name is found, and then return that name.
834 Optional second argument IGNORE specifies a name that is okay to use (if
835 it is in the sequence to be tried) even if a buffer with that name exists. */)
836 (name, ignore)
837 register Lisp_Object name, ignore;
839 register Lisp_Object gentemp, tem;
840 int count;
841 char number[10];
843 CHECK_STRING (name);
845 tem = Fstring_equal (name, ignore);
846 if (!NILP (tem))
847 return name;
848 tem = Fget_buffer (name);
849 if (NILP (tem))
850 return name;
852 count = 1;
853 while (1)
855 sprintf (number, "<%d>", ++count);
856 gentemp = concat2 (name, build_string (number));
857 tem = Fstring_equal (gentemp, ignore);
858 if (!NILP (tem))
859 return gentemp;
860 tem = Fget_buffer (gentemp);
861 if (NILP (tem))
862 return gentemp;
867 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
868 doc: /* Return the name of BUFFER, as a string.
869 With no argument or nil as argument, return the name of the current buffer. */)
870 (buffer)
871 register Lisp_Object buffer;
873 if (NILP (buffer))
874 return current_buffer->name;
875 CHECK_BUFFER (buffer);
876 return XBUFFER (buffer)->name;
879 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
880 doc: /* Return name of file BUFFER is visiting, or nil if none.
881 No argument or nil as argument means use the current buffer. */)
882 (buffer)
883 register Lisp_Object buffer;
885 if (NILP (buffer))
886 return current_buffer->filename;
887 CHECK_BUFFER (buffer);
888 return XBUFFER (buffer)->filename;
891 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
892 0, 1, 0,
893 doc: /* Return the base buffer of indirect buffer BUFFER.
894 If BUFFER is not indirect, return nil.
895 BUFFER defaults to the current buffer. */)
896 (buffer)
897 register Lisp_Object buffer;
899 struct buffer *base;
900 Lisp_Object base_buffer;
902 if (NILP (buffer))
903 base = current_buffer->base_buffer;
904 else
906 CHECK_BUFFER (buffer);
907 base = XBUFFER (buffer)->base_buffer;
910 if (! base)
911 return Qnil;
912 XSETBUFFER (base_buffer, base);
913 return base_buffer;
916 DEFUN ("buffer-local-value", Fbuffer_local_value,
917 Sbuffer_local_value, 2, 2, 0,
918 doc: /* Return the value of VARIABLE in BUFFER.
919 If VARIABLE does not have a buffer-local binding in BUFFER, the value
920 is the default binding of the variable. */)
921 (variable, buffer)
922 register Lisp_Object variable;
923 register Lisp_Object buffer;
925 register struct buffer *buf;
926 register Lisp_Object result;
928 CHECK_SYMBOL (variable);
929 CHECK_BUFFER (buffer);
930 buf = XBUFFER (buffer);
932 variable = indirect_variable (variable);
934 /* Look in local_var_list */
935 result = Fassoc (variable, buf->local_var_alist);
936 if (NILP (result))
938 int offset, idx;
939 int found = 0;
941 /* Look in special slots */
942 for (offset = PER_BUFFER_VAR_OFFSET (name);
943 offset < sizeof (struct buffer);
944 /* sizeof EMACS_INT == sizeof Lisp_Object */
945 offset += (sizeof (EMACS_INT)))
947 idx = PER_BUFFER_IDX (offset);
948 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
949 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
950 && EQ (PER_BUFFER_SYMBOL (offset), variable))
952 result = PER_BUFFER_VALUE (buf, offset);
953 found = 1;
954 break;
958 if (!found)
959 result = Fdefault_value (variable);
961 else
963 Lisp_Object valcontents;
964 Lisp_Object current_alist_element;
966 /* What binding is loaded right now? */
967 valcontents = SYMBOL_VALUE (variable);
968 current_alist_element
969 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
971 /* The value of the currently loaded binding is not
972 stored in it, but rather in the realvalue slot.
973 Store that value into the binding it belongs to
974 in case that is the one we are about to use. */
976 Fsetcdr (current_alist_element,
977 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
979 /* Now get the (perhaps updated) value out of the binding. */
980 result = XCDR (result);
983 if (!EQ (result, Qunbound))
984 return result;
986 xsignal1 (Qvoid_variable, variable);
989 /* Return an alist of the Lisp-level buffer-local bindings of
990 buffer BUF. That is, don't include the variables maintained
991 in special slots in the buffer object. */
993 static Lisp_Object
994 buffer_lisp_local_variables (buf)
995 struct buffer *buf;
997 Lisp_Object result = Qnil;
998 register Lisp_Object tail;
999 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1001 Lisp_Object val, elt;
1003 elt = XCAR (tail);
1005 /* Reference each variable in the alist in buf.
1006 If inquiring about the current buffer, this gets the current values,
1007 so store them into the alist so the alist is up to date.
1008 If inquiring about some other buffer, this swaps out any values
1009 for that buffer, making the alist up to date automatically. */
1010 val = find_symbol_value (XCAR (elt));
1011 /* Use the current buffer value only if buf is the current buffer. */
1012 if (buf != current_buffer)
1013 val = XCDR (elt);
1015 /* If symbol is unbound, put just the symbol in the list. */
1016 if (EQ (val, Qunbound))
1017 result = Fcons (XCAR (elt), result);
1018 /* Otherwise, put (symbol . value) in the list. */
1019 else
1020 result = Fcons (Fcons (XCAR (elt), val), result);
1023 return result;
1026 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1027 Sbuffer_local_variables, 0, 1, 0,
1028 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1029 Most elements look like (SYMBOL . VALUE), describing one variable.
1030 For a symbol that is locally unbound, just the symbol appears in the value.
1031 Note that storing new VALUEs in these elements doesn't change the variables.
1032 No argument or nil as argument means use current buffer as BUFFER. */)
1033 (buffer)
1034 register Lisp_Object buffer;
1036 register struct buffer *buf;
1037 register Lisp_Object result;
1039 if (NILP (buffer))
1040 buf = current_buffer;
1041 else
1043 CHECK_BUFFER (buffer);
1044 buf = XBUFFER (buffer);
1047 result = buffer_lisp_local_variables (buf);
1049 /* Add on all the variables stored in special slots. */
1051 int offset, idx;
1053 for (offset = PER_BUFFER_VAR_OFFSET (name);
1054 offset < sizeof (struct buffer);
1055 /* sizeof EMACS_INT == sizeof Lisp_Object */
1056 offset += (sizeof (EMACS_INT)))
1058 idx = PER_BUFFER_IDX (offset);
1059 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1060 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1061 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1062 PER_BUFFER_VALUE (buf, offset)),
1063 result);
1067 return result;
1070 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1071 0, 1, 0,
1072 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1073 No argument or nil as argument means use current buffer as BUFFER. */)
1074 (buffer)
1075 register Lisp_Object buffer;
1077 register struct buffer *buf;
1078 if (NILP (buffer))
1079 buf = current_buffer;
1080 else
1082 CHECK_BUFFER (buffer);
1083 buf = XBUFFER (buffer);
1086 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1089 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1090 1, 1, 0,
1091 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1092 A non-nil FLAG means mark the buffer modified. */)
1093 (flag)
1094 register Lisp_Object flag;
1096 register int already;
1097 register Lisp_Object fn;
1098 Lisp_Object buffer, window;
1100 #ifdef CLASH_DETECTION
1101 /* If buffer becoming modified, lock the file.
1102 If buffer becoming unmodified, unlock the file. */
1104 fn = current_buffer->file_truename;
1105 /* Test buffer-file-name so that binding it to nil is effective. */
1106 if (!NILP (fn) && ! NILP (current_buffer->filename))
1108 already = SAVE_MODIFF < MODIFF;
1109 if (!already && !NILP (flag))
1110 lock_file (fn);
1111 else if (already && NILP (flag))
1112 unlock_file (fn);
1114 #endif /* CLASH_DETECTION */
1116 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1118 /* Set update_mode_lines only if buffer is displayed in some window.
1119 Packages like jit-lock or lazy-lock preserve a buffer's modified
1120 state by recording/restoring the state around blocks of code.
1121 Setting update_mode_lines makes redisplay consider all windows
1122 (on all frames). Stealth fontification of buffers not displayed
1123 would incur additional redisplay costs if we'd set
1124 update_modes_lines unconditionally.
1126 Ideally, I think there should be another mechanism for fontifying
1127 buffers without "modifying" buffers, or redisplay should be
1128 smarter about updating the `*' in mode lines. --gerd */
1129 XSETBUFFER (buffer, current_buffer);
1130 window = Fget_buffer_window (buffer, Qt);
1131 if (WINDOWP (window))
1133 ++update_mode_lines;
1134 current_buffer->prevent_redisplay_optimizations_p = 1;
1137 return flag;
1140 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1141 Srestore_buffer_modified_p, 1, 1, 0,
1142 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1143 It is not ensured that mode lines will be updated to show the modified
1144 state of the current buffer. Use with care. */)
1145 (flag)
1146 Lisp_Object flag;
1148 #ifdef CLASH_DETECTION
1149 Lisp_Object fn;
1151 /* If buffer becoming modified, lock the file.
1152 If buffer becoming unmodified, unlock the file. */
1154 fn = current_buffer->file_truename;
1155 /* Test buffer-file-name so that binding it to nil is effective. */
1156 if (!NILP (fn) && ! NILP (current_buffer->filename))
1158 int already = SAVE_MODIFF < MODIFF;
1159 if (!already && !NILP (flag))
1160 lock_file (fn);
1161 else if (already && NILP (flag))
1162 unlock_file (fn);
1164 #endif /* CLASH_DETECTION */
1166 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1167 return flag;
1170 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1171 0, 1, 0,
1172 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1173 Each buffer has a tick counter which is incremented each time the
1174 text in that buffer is changed. It wraps around occasionally.
1175 No argument or nil as argument means use current buffer as BUFFER. */)
1176 (buffer)
1177 register Lisp_Object buffer;
1179 register struct buffer *buf;
1180 if (NILP (buffer))
1181 buf = current_buffer;
1182 else
1184 CHECK_BUFFER (buffer);
1185 buf = XBUFFER (buffer);
1188 return make_number (BUF_MODIFF (buf));
1191 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1192 Sbuffer_chars_modified_tick, 0, 1, 0,
1193 doc: /* Return BUFFER's character-change tick counter.
1194 Each buffer has a character-change tick counter, which is set to the
1195 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1196 time text in that buffer is inserted or deleted. By comparing the
1197 values returned by two individual calls of `buffer-chars-modified-tick',
1198 you can tell whether a character change occurred in that buffer in
1199 between these calls. No argument or nil as argument means use current
1200 buffer as BUFFER. */)
1201 (buffer)
1202 register Lisp_Object buffer;
1204 register struct buffer *buf;
1205 if (NILP (buffer))
1206 buf = current_buffer;
1207 else
1209 CHECK_BUFFER (buffer);
1210 buf = XBUFFER (buffer);
1213 return make_number (BUF_CHARS_MODIFF (buf));
1216 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1217 "(list (read-string \"Rename buffer (to new name): \" \
1218 nil 'buffer-name-history (buffer-name (current-buffer))) \
1219 current-prefix-arg)",
1220 doc: /* Change current buffer's name to NEWNAME (a string).
1221 If second arg UNIQUE is nil or omitted, it is an error if a
1222 buffer named NEWNAME already exists.
1223 If UNIQUE is non-nil, come up with a new name using
1224 `generate-new-buffer-name'.
1225 Interactively, you can set UNIQUE with a prefix argument.
1226 We return the name we actually gave the buffer.
1227 This does not change the name of the visited file (if any). */)
1228 (newname, unique)
1229 register Lisp_Object newname, unique;
1231 register Lisp_Object tem, buf;
1233 CHECK_STRING (newname);
1235 if (SCHARS (newname) == 0)
1236 error ("Empty string is invalid as a buffer name");
1238 tem = Fget_buffer (newname);
1239 if (!NILP (tem))
1241 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1242 rename the buffer automatically so you can create another
1243 with the original name. It makes UNIQUE equivalent to
1244 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1245 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1246 return current_buffer->name;
1247 if (!NILP (unique))
1248 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1249 else
1250 error ("Buffer name `%s' is in use", SDATA (newname));
1253 current_buffer->name = newname;
1255 /* Catch redisplay's attention. Unless we do this, the mode lines for
1256 any windows displaying current_buffer will stay unchanged. */
1257 update_mode_lines++;
1259 XSETBUFFER (buf, current_buffer);
1260 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1261 if (NILP (current_buffer->filename)
1262 && !NILP (current_buffer->auto_save_file_name))
1263 call0 (intern ("rename-auto-save-file"));
1264 /* Refetch since that last call may have done GC. */
1265 return current_buffer->name;
1268 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1269 doc: /* Return most recently selected buffer other than BUFFER.
1270 Buffers not visible in windows are preferred to visible buffers,
1271 unless optional second argument VISIBLE-OK is non-nil.
1272 If the optional third argument FRAME is non-nil, use that frame's
1273 buffer list instead of the selected frame's buffer list.
1274 If no other buffer exists, the buffer `*scratch*' is returned.
1275 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1276 (buffer, visible_ok, frame)
1277 register Lisp_Object buffer, visible_ok, frame;
1279 Lisp_Object Fset_buffer_major_mode ();
1280 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1281 notsogood = Qnil;
1283 if (NILP (frame))
1284 frame = selected_frame;
1286 tail = Vbuffer_alist;
1287 pred = frame_buffer_predicate (frame);
1289 /* Consider buffers that have been seen in the selected frame
1290 before other buffers. */
1292 tem = frame_buffer_list (frame);
1293 add_ons = Qnil;
1294 while (CONSP (tem))
1296 if (BUFFERP (XCAR (tem)))
1297 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1298 tem = XCDR (tem);
1300 tail = nconc2 (Fnreverse (add_ons), tail);
1302 for (; CONSP (tail); tail = XCDR (tail))
1304 buf = Fcdr (XCAR (tail));
1305 if (EQ (buf, buffer))
1306 continue;
1307 if (NILP (buf))
1308 continue;
1309 if (NILP (XBUFFER (buf)->name))
1310 continue;
1311 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1312 continue;
1313 /* If the selected frame has a buffer_predicate,
1314 disregard buffers that don't fit the predicate. */
1315 if (!NILP (pred))
1317 tem = call1 (pred, buf);
1318 if (NILP (tem))
1319 continue;
1322 if (NILP (visible_ok))
1323 tem = Fget_buffer_window (buf, Qvisible);
1324 else
1325 tem = Qnil;
1326 if (NILP (tem))
1327 return buf;
1328 if (NILP (notsogood))
1329 notsogood = buf;
1331 if (!NILP (notsogood))
1332 return notsogood;
1333 buf = Fget_buffer (build_string ("*scratch*"));
1334 if (NILP (buf))
1336 buf = Fget_buffer_create (build_string ("*scratch*"));
1337 Fset_buffer_major_mode (buf);
1339 return buf;
1342 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1343 0, 1, "",
1344 doc: /* Start keeping undo information for buffer BUFFER.
1345 No argument or nil as argument means do this for the current buffer. */)
1346 (buffer)
1347 register Lisp_Object buffer;
1349 Lisp_Object real_buffer;
1351 if (NILP (buffer))
1352 XSETBUFFER (real_buffer, current_buffer);
1353 else
1355 real_buffer = Fget_buffer (buffer);
1356 if (NILP (real_buffer))
1357 nsberror (buffer);
1360 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1361 XBUFFER (real_buffer)->undo_list = Qnil;
1363 return Qnil;
1367 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1368 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1369 The buffer being killed will be current while the hook is running.\n\
1370 See `kill-buffer'."
1372 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1373 doc: /* Kill the buffer BUFFER.
1374 The argument may be a buffer or the name of a buffer.
1375 With a nil argument, kill the current buffer.
1377 Value is t if the buffer is actually killed, nil otherwise.
1379 The functions in `kill-buffer-query-functions' are called with BUFFER as
1380 the current buffer. If any of them returns nil, the buffer is not killed.
1382 The hook `kill-buffer-hook' is run before the buffer is actually killed.
1383 The buffer being killed will be current while the hook is running.
1385 Any processes that have this buffer as the `process-buffer' are killed
1386 with SIGHUP. */)
1387 (buffer)
1388 Lisp_Object buffer;
1390 Lisp_Object buf;
1391 register struct buffer *b;
1392 register Lisp_Object tem;
1393 register struct Lisp_Marker *m;
1394 struct gcpro gcpro1;
1396 if (NILP (buffer))
1397 buf = Fcurrent_buffer ();
1398 else
1399 buf = Fget_buffer (buffer);
1400 if (NILP (buf))
1401 nsberror (buffer);
1403 b = XBUFFER (buf);
1405 /* Avoid trouble for buffer already dead. */
1406 if (NILP (b->name))
1407 return Qnil;
1409 /* Query if the buffer is still modified. */
1410 if (INTERACTIVE && !NILP (b->filename)
1411 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1413 GCPRO1 (buf);
1414 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1415 b->name, make_number (0)));
1416 UNGCPRO;
1417 if (NILP (tem))
1418 return Qnil;
1421 /* Run hooks with the buffer to be killed the current buffer. */
1423 int count = SPECPDL_INDEX ();
1424 Lisp_Object arglist[1];
1426 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1427 set_buffer_internal (b);
1429 /* First run the query functions; if any query is answered no,
1430 don't kill the buffer. */
1431 arglist[0] = Qkill_buffer_query_functions;
1432 tem = Frun_hook_with_args_until_failure (1, arglist);
1433 if (NILP (tem))
1434 return unbind_to (count, Qnil);
1436 /* Then run the hooks. */
1437 Frun_hooks (1, &Qkill_buffer_hook);
1438 unbind_to (count, Qnil);
1441 /* We have no more questions to ask. Verify that it is valid
1442 to kill the buffer. This must be done after the questions
1443 since anything can happen within do_yes_or_no_p. */
1445 /* Don't kill the minibuffer now current. */
1446 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1447 return Qnil;
1449 if (NILP (b->name))
1450 return Qnil;
1452 /* When we kill a base buffer, kill all its indirect buffers.
1453 We do it at this stage so nothing terrible happens if they
1454 ask questions or their hooks get errors. */
1455 if (! b->base_buffer)
1457 struct buffer *other;
1459 GCPRO1 (buf);
1461 for (other = all_buffers; other; other = other->next)
1462 /* all_buffers contains dead buffers too;
1463 don't re-kill them. */
1464 if (other->base_buffer == b && !NILP (other->name))
1466 Lisp_Object buf;
1467 XSETBUFFER (buf, other);
1468 Fkill_buffer (buf);
1471 UNGCPRO;
1474 /* Make this buffer not be current.
1475 In the process, notice if this is the sole visible buffer
1476 and give up if so. */
1477 if (b == current_buffer)
1479 tem = Fother_buffer (buf, Qnil, Qnil);
1480 Fset_buffer (tem);
1481 if (b == current_buffer)
1482 return Qnil;
1485 /* Notice if the buffer to kill is the sole visible buffer
1486 when we're currently in the mini-buffer, and give up if so. */
1487 XSETBUFFER (tem, current_buffer);
1488 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1490 tem = Fother_buffer (buf, Qnil, Qnil);
1491 if (EQ (buf, tem))
1492 return Qnil;
1495 /* Now there is no question: we can kill the buffer. */
1497 #ifdef CLASH_DETECTION
1498 /* Unlock this buffer's file, if it is locked. */
1499 unlock_buffer (b);
1500 #endif /* CLASH_DETECTION */
1502 GCPRO1 (buf);
1503 kill_buffer_processes (buf);
1504 UNGCPRO;
1506 /* Killing buffer processes may run sentinels which may
1507 have called kill-buffer. */
1509 if (NILP (b->name))
1510 return Qnil;
1512 clear_charpos_cache (b);
1514 tem = Vinhibit_quit;
1515 Vinhibit_quit = Qt;
1516 replace_buffer_in_all_windows (buf);
1517 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1518 frames_discard_buffer (buf);
1519 Vinhibit_quit = tem;
1521 /* Delete any auto-save file, if we saved it in this session.
1522 But not if the buffer is modified. */
1523 if (STRINGP (b->auto_save_file_name)
1524 && b->auto_save_modified != 0
1525 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1526 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1527 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1529 Lisp_Object tem;
1530 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1531 if (! NILP (tem))
1532 internal_delete_file (b->auto_save_file_name);
1535 if (b->base_buffer)
1537 /* Unchain all markers that belong to this indirect buffer.
1538 Don't unchain the markers that belong to the base buffer
1539 or its other indirect buffers. */
1540 for (m = BUF_MARKERS (b); m; )
1542 struct Lisp_Marker *next = m->next;
1543 if (m->buffer == b)
1544 unchain_marker (m);
1545 m = next;
1548 else
1550 /* Unchain all markers of this buffer and its indirect buffers.
1551 and leave them pointing nowhere. */
1552 for (m = BUF_MARKERS (b); m; )
1554 struct Lisp_Marker *next = m->next;
1555 m->buffer = 0;
1556 m->next = NULL;
1557 m = next;
1559 BUF_MARKERS (b) = NULL;
1560 BUF_INTERVALS (b) = NULL_INTERVAL;
1562 /* Perhaps we should explicitly free the interval tree here... */
1565 /* Reset the local variables, so that this buffer's local values
1566 won't be protected from GC. They would be protected
1567 if they happened to remain encached in their symbols.
1568 This gets rid of them for certain. */
1569 swap_out_buffer_local_variables (b);
1570 reset_buffer_local_variables (b, 1);
1572 b->name = Qnil;
1574 BLOCK_INPUT;
1575 if (! b->base_buffer)
1576 free_buffer_text (b);
1578 if (b->newline_cache)
1580 free_region_cache (b->newline_cache);
1581 b->newline_cache = 0;
1583 if (b->width_run_cache)
1585 free_region_cache (b->width_run_cache);
1586 b->width_run_cache = 0;
1588 b->width_table = Qnil;
1589 UNBLOCK_INPUT;
1590 b->undo_list = Qnil;
1592 return Qt;
1595 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1596 we do this each time BUF is selected visibly, the more recently
1597 selected buffers are always closer to the front of the list. This
1598 means that other_buffer is more likely to choose a relevant buffer. */
1600 void
1601 record_buffer (buf)
1602 Lisp_Object buf;
1604 register Lisp_Object link, prev;
1605 Lisp_Object frame;
1606 frame = selected_frame;
1608 prev = Qnil;
1609 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1611 if (EQ (XCDR (XCAR (link)), buf))
1612 break;
1613 prev = link;
1616 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1617 we cannot use Fdelq itself here because it allows quitting. */
1619 if (NILP (prev))
1620 Vbuffer_alist = XCDR (Vbuffer_alist);
1621 else
1622 XSETCDR (prev, XCDR (XCDR (prev)));
1624 XSETCDR (link, Vbuffer_alist);
1625 Vbuffer_alist = link;
1627 /* Effectively do a delq on buried_buffer_list. */
1629 prev = Qnil;
1630 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1631 link = XCDR (link))
1633 if (EQ (XCAR (link), buf))
1635 if (NILP (prev))
1636 XFRAME (frame)->buried_buffer_list = XCDR (link);
1637 else
1638 XSETCDR (prev, XCDR (XCDR (prev)));
1639 break;
1641 prev = link;
1644 /* Now move this buffer to the front of frame_buffer_list also. */
1646 prev = Qnil;
1647 for (link = frame_buffer_list (frame); CONSP (link);
1648 link = XCDR (link))
1650 if (EQ (XCAR (link), buf))
1651 break;
1652 prev = link;
1655 /* Effectively do delq. */
1657 if (CONSP (link))
1659 if (NILP (prev))
1660 set_frame_buffer_list (frame,
1661 XCDR (frame_buffer_list (frame)));
1662 else
1663 XSETCDR (prev, XCDR (XCDR (prev)));
1665 XSETCDR (link, frame_buffer_list (frame));
1666 set_frame_buffer_list (frame, link);
1668 else
1669 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1672 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1673 doc: /* Set an appropriate major mode for BUFFER.
1674 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1675 according to `default-major-mode'.
1676 Use this function before selecting the buffer, since it may need to inspect
1677 the current buffer's major mode. */)
1678 (buffer)
1679 Lisp_Object buffer;
1681 int count;
1682 Lisp_Object function;
1684 CHECK_BUFFER (buffer);
1686 if (STRINGP (XBUFFER (buffer)->name)
1687 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1688 function = find_symbol_value (intern ("initial-major-mode"));
1689 else
1691 function = buffer_defaults.major_mode;
1692 if (NILP (function)
1693 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1694 function = current_buffer->major_mode;
1697 if (NILP (function) || EQ (function, Qfundamental_mode))
1698 return Qnil;
1700 count = SPECPDL_INDEX ();
1702 /* To select a nonfundamental mode,
1703 select the buffer temporarily and then call the mode function. */
1705 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1707 Fset_buffer (buffer);
1708 call0 (function);
1710 return unbind_to (count, Qnil);
1713 /* If switching buffers in WINDOW would be an error, return
1714 a C string saying what the error would be. */
1716 char *
1717 no_switch_window (window)
1718 Lisp_Object window;
1720 Lisp_Object tem;
1721 if (EQ (minibuf_window, window))
1722 return "Cannot switch buffers in minibuffer window";
1723 tem = Fwindow_dedicated_p (window);
1724 if (EQ (tem, Qt))
1725 return "Cannot switch buffers in a dedicated window";
1726 return NULL;
1729 /* Switch to buffer BUFFER in the selected window.
1730 If NORECORD is non-nil, don't call record_buffer. */
1732 Lisp_Object
1733 switch_to_buffer_1 (buffer, norecord)
1734 Lisp_Object buffer, norecord;
1736 register Lisp_Object buf;
1738 if (NILP (buffer))
1739 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1740 else
1742 buf = Fget_buffer (buffer);
1743 if (NILP (buf))
1745 buf = Fget_buffer_create (buffer);
1746 Fset_buffer_major_mode (buf);
1749 Fset_buffer (buf);
1750 if (NILP (norecord))
1751 record_buffer (buf);
1753 Fset_window_buffer (EQ (selected_window, minibuf_window)
1754 ? Fnext_window (minibuf_window, Qnil, Qnil)
1755 : selected_window,
1756 buf, Qnil);
1758 return buf;
1761 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1762 doc: /* Select buffer BUFFER in the current window.
1763 If BUFFER does not identify an existing buffer,
1764 then this function creates a buffer with that name.
1766 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1767 or nil. If BUFFER is nil, then this function chooses a buffer
1768 using `other-buffer'.
1769 Optional second arg NORECORD non-nil means
1770 do not put this buffer at the front of the list of recently selected ones.
1771 This function returns the buffer it switched to.
1773 WARNING: This is NOT the way to work on another buffer temporarily
1774 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1775 the window-buffer correspondences. */)
1776 (buffer, norecord)
1777 Lisp_Object buffer, norecord;
1779 char *err;
1781 if (EQ (buffer, Fwindow_buffer (selected_window)))
1783 /* Basically a NOP. Avoid signalling an error in the case where
1784 the selected window is dedicated, or a minibuffer. */
1786 /* But do put this buffer at the front of the buffer list,
1787 unless that has been inhibited. Note that even if
1788 BUFFER is at the front of the main buffer-list already,
1789 we still want to move it to the front of the frame's buffer list. */
1790 if (NILP (norecord))
1791 record_buffer (buffer);
1792 return Fset_buffer (buffer);
1795 err = no_switch_window (selected_window);
1796 if (err) error (err);
1798 return switch_to_buffer_1 (buffer, norecord);
1801 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1802 doc: /* Select buffer BUFFER in some window, preferably a different one.
1803 BUFFER may be a buffer, a string \(a buffer name), or nil.
1804 If BUFFER is a string which is not the name of an existing buffer,
1805 then this function creates a buffer with that name.
1806 If BUFFER is nil, then it chooses some other buffer.
1807 If `pop-up-windows' is non-nil, windows can be split to do this.
1808 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1809 window even if BUFFER is already visible in the selected window,
1810 and ignore `same-window-regexps' and `same-window-buffer-names'.
1811 This function returns the buffer it switched to.
1812 This uses the function `display-buffer' as a subroutine; see the documentation
1813 of `display-buffer' for additional customization information.
1815 Optional third arg NORECORD non-nil means
1816 do not put this buffer at the front of the list of recently selected ones. */)
1817 (buffer, other_window, norecord)
1818 Lisp_Object buffer, other_window, norecord;
1820 register Lisp_Object buf;
1821 if (NILP (buffer))
1822 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1823 else
1825 buf = Fget_buffer (buffer);
1826 if (NILP (buf))
1828 buf = Fget_buffer_create (buffer);
1829 Fset_buffer_major_mode (buf);
1832 Fset_buffer (buf);
1833 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1834 return buf;
1837 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1838 doc: /* Return the current buffer as a Lisp object. */)
1841 register Lisp_Object buf;
1842 XSETBUFFER (buf, current_buffer);
1843 return buf;
1846 /* Set the current buffer to B.
1848 We previously set windows_or_buffers_changed here to invalidate
1849 global unchanged information in beg_unchanged and end_unchanged.
1850 This is no longer necessary because we now compute unchanged
1851 information on a buffer-basis. Every action affecting other
1852 windows than the selected one requires a select_window at some
1853 time, and that increments windows_or_buffers_changed. */
1855 void
1856 set_buffer_internal (b)
1857 register struct buffer *b;
1859 if (current_buffer != b)
1860 set_buffer_internal_1 (b);
1863 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1864 This is used by redisplay. */
1866 void
1867 set_buffer_internal_1 (b)
1868 register struct buffer *b;
1870 register struct buffer *old_buf;
1871 register Lisp_Object tail, valcontents;
1872 Lisp_Object tem;
1874 #ifdef USE_MMAP_FOR_BUFFERS
1875 if (b->text->beg == NULL)
1876 enlarge_buffer_text (b, 0);
1877 #endif /* USE_MMAP_FOR_BUFFERS */
1879 if (current_buffer == b)
1880 return;
1882 old_buf = current_buffer;
1883 current_buffer = b;
1884 last_known_column_point = -1; /* invalidate indentation cache */
1886 if (old_buf)
1888 /* Put the undo list back in the base buffer, so that it appears
1889 that an indirect buffer shares the undo list of its base. */
1890 if (old_buf->base_buffer)
1891 old_buf->base_buffer->undo_list = old_buf->undo_list;
1893 /* If the old current buffer has markers to record PT, BEGV and ZV
1894 when it is not current, update them now. */
1895 if (! NILP (old_buf->pt_marker))
1897 Lisp_Object obuf;
1898 XSETBUFFER (obuf, old_buf);
1899 set_marker_both (old_buf->pt_marker, obuf,
1900 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1902 if (! NILP (old_buf->begv_marker))
1904 Lisp_Object obuf;
1905 XSETBUFFER (obuf, old_buf);
1906 set_marker_both (old_buf->begv_marker, obuf,
1907 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1909 if (! NILP (old_buf->zv_marker))
1911 Lisp_Object obuf;
1912 XSETBUFFER (obuf, old_buf);
1913 set_marker_both (old_buf->zv_marker, obuf,
1914 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1918 /* Get the undo list from the base buffer, so that it appears
1919 that an indirect buffer shares the undo list of its base. */
1920 if (b->base_buffer)
1921 b->undo_list = b->base_buffer->undo_list;
1923 /* If the new current buffer has markers to record PT, BEGV and ZV
1924 when it is not current, fetch them now. */
1925 if (! NILP (b->pt_marker))
1927 BUF_PT (b) = marker_position (b->pt_marker);
1928 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1930 if (! NILP (b->begv_marker))
1932 BUF_BEGV (b) = marker_position (b->begv_marker);
1933 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1935 if (! NILP (b->zv_marker))
1937 BUF_ZV (b) = marker_position (b->zv_marker);
1938 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1941 /* Look down buffer's list of local Lisp variables
1942 to find and update any that forward into C variables. */
1944 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1946 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1947 if ((BUFFER_LOCAL_VALUEP (valcontents))
1948 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1949 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1950 /* Just reference the variable
1951 to cause it to become set for this buffer. */
1952 Fsymbol_value (XCAR (XCAR (tail)));
1955 /* Do the same with any others that were local to the previous buffer */
1957 if (old_buf)
1958 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1960 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1961 if ((BUFFER_LOCAL_VALUEP (valcontents))
1962 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1963 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1964 /* Just reference the variable
1965 to cause it to become set for this buffer. */
1966 Fsymbol_value (XCAR (XCAR (tail)));
1970 /* Switch to buffer B temporarily for redisplay purposes.
1971 This avoids certain things that don't need to be done within redisplay. */
1973 void
1974 set_buffer_temp (b)
1975 struct buffer *b;
1977 register struct buffer *old_buf;
1979 if (current_buffer == b)
1980 return;
1982 old_buf = current_buffer;
1983 current_buffer = b;
1985 if (old_buf)
1987 /* If the old current buffer has markers to record PT, BEGV and ZV
1988 when it is not current, update them now. */
1989 if (! NILP (old_buf->pt_marker))
1991 Lisp_Object obuf;
1992 XSETBUFFER (obuf, old_buf);
1993 set_marker_both (old_buf->pt_marker, obuf,
1994 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1996 if (! NILP (old_buf->begv_marker))
1998 Lisp_Object obuf;
1999 XSETBUFFER (obuf, old_buf);
2000 set_marker_both (old_buf->begv_marker, obuf,
2001 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
2003 if (! NILP (old_buf->zv_marker))
2005 Lisp_Object obuf;
2006 XSETBUFFER (obuf, old_buf);
2007 set_marker_both (old_buf->zv_marker, obuf,
2008 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
2012 /* If the new current buffer has markers to record PT, BEGV and ZV
2013 when it is not current, fetch them now. */
2014 if (! NILP (b->pt_marker))
2016 BUF_PT (b) = marker_position (b->pt_marker);
2017 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
2019 if (! NILP (b->begv_marker))
2021 BUF_BEGV (b) = marker_position (b->begv_marker);
2022 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2024 if (! NILP (b->zv_marker))
2026 BUF_ZV (b) = marker_position (b->zv_marker);
2027 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2031 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2032 doc: /* Make the buffer BUFFER current for editing operations.
2033 BUFFER may be a buffer or the name of an existing buffer.
2034 See also `save-excursion' when you want to make a buffer current temporarily.
2035 This function does not display the buffer, so its effect ends
2036 when the current command terminates.
2037 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
2038 (buffer)
2039 register Lisp_Object buffer;
2041 register Lisp_Object buf;
2042 buf = Fget_buffer (buffer);
2043 if (NILP (buf))
2044 nsberror (buffer);
2045 if (NILP (XBUFFER (buf)->name))
2046 error ("Selecting deleted buffer");
2047 set_buffer_internal (XBUFFER (buf));
2048 return buf;
2051 /* Set the current buffer to BUFFER provided it is alive. */
2053 Lisp_Object
2054 set_buffer_if_live (buffer)
2055 Lisp_Object buffer;
2057 if (! NILP (XBUFFER (buffer)->name))
2058 Fset_buffer (buffer);
2059 return Qnil;
2062 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2063 Sbarf_if_buffer_read_only, 0, 0, 0,
2064 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2067 if (!NILP (current_buffer->read_only)
2068 && NILP (Vinhibit_read_only))
2069 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2070 return Qnil;
2073 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2074 doc: /* Put BUFFER at the end of the list of all buffers.
2075 There it is the least likely candidate for `other-buffer' to return;
2076 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
2077 You can specify a buffer name as BUFFER, or an actual buffer object.
2078 If BUFFER is nil or omitted, bury the current buffer.
2079 Also, if BUFFER is nil or omitted, remove the current buffer from the
2080 selected window if it is displayed there. */)
2081 (buffer)
2082 register Lisp_Object buffer;
2084 /* Figure out what buffer we're going to bury. */
2085 if (NILP (buffer))
2087 Lisp_Object tem;
2088 XSETBUFFER (buffer, current_buffer);
2090 tem = Fwindow_buffer (selected_window);
2091 /* If we're burying the current buffer, unshow it. */
2092 if (EQ (buffer, tem))
2094 if (NILP (Fwindow_dedicated_p (selected_window)))
2095 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2096 else if (NILP (XWINDOW (selected_window)->parent))
2097 Ficonify_frame (Fwindow_frame (selected_window));
2098 else
2099 Fdelete_window (selected_window);
2102 else
2104 Lisp_Object buf1;
2106 buf1 = Fget_buffer (buffer);
2107 if (NILP (buf1))
2108 nsberror (buffer);
2109 buffer = buf1;
2112 /* Move buffer to the end of the buffer list. Do nothing if the
2113 buffer is killed. */
2114 if (!NILP (XBUFFER (buffer)->name))
2116 Lisp_Object aelt, link;
2118 aelt = Frassq (buffer, Vbuffer_alist);
2119 link = Fmemq (aelt, Vbuffer_alist);
2120 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2121 XSETCDR (link, Qnil);
2122 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2124 XFRAME (selected_frame)->buffer_list
2125 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2126 XFRAME (selected_frame)->buried_buffer_list
2127 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2130 return Qnil;
2133 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2134 doc: /* Delete the entire contents of the current buffer.
2135 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2136 so the buffer is truly empty after this. */)
2139 Fwiden ();
2141 del_range (BEG, Z);
2143 current_buffer->last_window_start = 1;
2144 /* Prevent warnings, or suspension of auto saving, that would happen
2145 if future size is less than past size. Use of erase-buffer
2146 implies that the future text is not really related to the past text. */
2147 XSETFASTINT (current_buffer->save_length, 0);
2148 return Qnil;
2151 void
2152 validate_region (b, e)
2153 register Lisp_Object *b, *e;
2155 CHECK_NUMBER_COERCE_MARKER (*b);
2156 CHECK_NUMBER_COERCE_MARKER (*e);
2158 if (XINT (*b) > XINT (*e))
2160 Lisp_Object tem;
2161 tem = *b; *b = *e; *e = tem;
2164 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2165 && XINT (*e) <= ZV))
2166 args_out_of_range (*b, *e);
2169 /* Advance BYTE_POS up to a character boundary
2170 and return the adjusted position. */
2172 static int
2173 advance_to_char_boundary (byte_pos)
2174 int byte_pos;
2176 int c;
2178 if (byte_pos == BEG)
2179 /* Beginning of buffer is always a character boundary. */
2180 return BEG;
2182 c = FETCH_BYTE (byte_pos);
2183 if (! CHAR_HEAD_P (c))
2185 /* We should advance BYTE_POS only when C is a constituent of a
2186 multibyte sequence. */
2187 int orig_byte_pos = byte_pos;
2191 byte_pos--;
2192 c = FETCH_BYTE (byte_pos);
2194 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2195 INC_POS (byte_pos);
2196 if (byte_pos < orig_byte_pos)
2197 byte_pos = orig_byte_pos;
2198 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2199 surely advance to the correct character boundary. If C is
2200 not, BYTE_POS was unchanged. */
2203 return byte_pos;
2206 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2207 1, 1, 0,
2208 doc: /* Set the multibyte flag of the current buffer to FLAG.
2209 If FLAG is t, this makes the buffer a multibyte buffer.
2210 If FLAG is nil, this makes the buffer a single-byte buffer.
2211 The buffer contents remain unchanged as a sequence of bytes
2212 but the contents viewed as characters do change.
2213 If the multibyte flag was really changed, undo information of the
2214 current buffer is cleared. */)
2215 (flag)
2216 Lisp_Object flag;
2218 struct Lisp_Marker *tail, *markers;
2219 struct buffer *other;
2220 int begv, zv;
2221 int narrowed = (BEG != BEGV || Z != ZV);
2222 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2223 Lisp_Object old_undo = current_buffer->undo_list;
2224 struct gcpro gcpro1;
2226 if (current_buffer->base_buffer)
2227 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2229 /* Do nothing if nothing actually changes. */
2230 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2231 return flag;
2233 GCPRO1 (old_undo);
2235 /* Don't record these buffer changes. We will put a special undo entry
2236 instead. */
2237 current_buffer->undo_list = Qt;
2239 /* If the cached position is for this buffer, clear it out. */
2240 clear_charpos_cache (current_buffer);
2242 if (NILP (flag))
2243 begv = BEGV_BYTE, zv = ZV_BYTE;
2244 else
2245 begv = BEGV, zv = ZV;
2247 if (narrowed)
2248 Fwiden ();
2250 if (NILP (flag))
2252 int pos, stop;
2253 unsigned char *p;
2255 /* Do this first, so it can use CHAR_TO_BYTE
2256 to calculate the old correspondences. */
2257 set_intervals_multibyte (0);
2259 current_buffer->enable_multibyte_characters = Qnil;
2261 Z = Z_BYTE;
2262 BEGV = BEGV_BYTE;
2263 ZV = ZV_BYTE;
2264 GPT = GPT_BYTE;
2265 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2268 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2269 tail->charpos = tail->bytepos;
2271 /* Convert multibyte form of 8-bit characters to unibyte. */
2272 pos = BEG;
2273 stop = GPT;
2274 p = BEG_ADDR;
2275 while (1)
2277 int c, bytes;
2279 if (pos == stop)
2281 if (pos == Z)
2282 break;
2283 p = GAP_END_ADDR;
2284 stop = Z;
2286 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
2287 p += bytes, pos += bytes;
2288 else
2290 c = STRING_CHAR (p, stop - pos);
2291 /* Delete all bytes for this 8-bit character but the
2292 last one, and change the last one to the charcter
2293 code. */
2294 bytes--;
2295 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2296 p = GAP_END_ADDR;
2297 *p++ = c;
2298 pos++;
2299 if (begv > pos)
2300 begv -= bytes;
2301 if (zv > pos)
2302 zv -= bytes;
2303 stop = Z;
2306 if (narrowed)
2307 Fnarrow_to_region (make_number (begv), make_number (zv));
2309 else
2311 int pt = PT;
2312 int pos, stop;
2313 unsigned char *p;
2315 /* Be sure not to have a multibyte sequence striding over the GAP.
2316 Ex: We change this: "...abc\201 _GAP_ \241def..."
2317 to: "...abc _GAP_ \201\241def..." */
2319 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2320 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2322 unsigned char *p = GPT_ADDR - 1;
2324 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2325 if (BASE_LEADING_CODE_P (*p))
2327 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2329 move_gap_both (new_gpt, new_gpt);
2333 /* Make the buffer contents valid as multibyte by converting
2334 8-bit characters to multibyte form. */
2335 pos = BEG;
2336 stop = GPT;
2337 p = BEG_ADDR;
2338 while (1)
2340 int bytes;
2342 if (pos == stop)
2344 if (pos == Z)
2345 break;
2346 p = GAP_END_ADDR;
2347 stop = Z;
2350 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2351 p += bytes, pos += bytes;
2352 else
2354 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2356 bytes = CHAR_STRING (*p, tmp);
2357 *p = tmp[0];
2358 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2359 bytes--;
2360 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2361 /* Now the gap is after the just inserted data. */
2362 pos = GPT;
2363 p = GAP_END_ADDR;
2364 if (pos <= begv)
2365 begv += bytes;
2366 if (pos <= zv)
2367 zv += bytes;
2368 if (pos <= pt)
2369 pt += bytes;
2370 stop = Z;
2374 if (pt != PT)
2375 TEMP_SET_PT (pt);
2377 if (narrowed)
2378 Fnarrow_to_region (make_number (begv), make_number (zv));
2380 /* Do this first, so that chars_in_text asks the right question.
2381 set_intervals_multibyte needs it too. */
2382 current_buffer->enable_multibyte_characters = Qt;
2384 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2385 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2387 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2389 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2390 if (BEGV_BYTE > GPT_BYTE)
2391 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2392 else
2393 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2395 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2396 if (ZV_BYTE > GPT_BYTE)
2397 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2398 else
2399 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2402 int pt_byte = advance_to_char_boundary (PT_BYTE);
2403 int pt;
2405 if (pt_byte > GPT_BYTE)
2406 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2407 else
2408 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2409 TEMP_SET_PT_BOTH (pt, pt_byte);
2412 tail = markers = BUF_MARKERS (current_buffer);
2414 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2415 getting confused by the markers that have not yet been updated.
2416 It is also a signal that it should never create a marker. */
2417 BUF_MARKERS (current_buffer) = NULL;
2419 for (; tail; tail = tail->next)
2421 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2422 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2425 /* Make sure no markers were put on the chain
2426 while the chain value was incorrect. */
2427 if (BUF_MARKERS (current_buffer))
2428 abort ();
2430 BUF_MARKERS (current_buffer) = markers;
2432 /* Do this last, so it can calculate the new correspondences
2433 between chars and bytes. */
2434 set_intervals_multibyte (1);
2437 if (!EQ (old_undo, Qt))
2439 /* Represent all the above changes by a special undo entry. */
2440 extern Lisp_Object Qapply;
2441 current_buffer->undo_list = Fcons (list3 (Qapply,
2442 intern ("set-buffer-multibyte"),
2443 NILP (flag) ? Qt : Qnil),
2444 old_undo);
2447 UNGCPRO;
2449 /* Changing the multibyteness of a buffer means that all windows
2450 showing that buffer must be updated thoroughly. */
2451 current_buffer->prevent_redisplay_optimizations_p = 1;
2452 ++windows_or_buffers_changed;
2454 /* Copy this buffer's new multibyte status
2455 into all of its indirect buffers. */
2456 for (other = all_buffers; other; other = other->next)
2457 if (other->base_buffer == current_buffer && !NILP (other->name))
2459 other->enable_multibyte_characters
2460 = current_buffer->enable_multibyte_characters;
2461 other->prevent_redisplay_optimizations_p = 1;
2464 /* Restore the modifiedness of the buffer. */
2465 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2466 Fset_buffer_modified_p (Qnil);
2468 #ifdef subprocesses
2469 /* Update coding systems of this buffer's process (if any). */
2471 Lisp_Object process;
2473 process = Fget_buffer_process (Fcurrent_buffer ());
2474 if (PROCESSP (process))
2475 setup_process_coding_systems (process);
2477 #endif /* subprocesses */
2479 return flag;
2482 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2483 0, 0, 0,
2484 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2485 Most local variable bindings are eliminated so that the default values
2486 become effective once more. Also, the syntax table is set from
2487 `standard-syntax-table', the local keymap is set to nil,
2488 and the abbrev table from `fundamental-mode-abbrev-table'.
2489 This function also forces redisplay of the mode line.
2491 Every function to select a new major mode starts by
2492 calling this function.
2494 As a special exception, local variables whose names have
2495 a non-nil `permanent-local' property are not eliminated by this function.
2497 The first thing this function does is run
2498 the normal hook `change-major-mode-hook'. */)
2501 if (!NILP (Vrun_hooks))
2502 call1 (Vrun_hooks, Qchange_major_mode_hook);
2504 /* Make sure none of the bindings in local_var_alist
2505 remain swapped in, in their symbols. */
2507 swap_out_buffer_local_variables (current_buffer);
2509 /* Actually eliminate all local bindings of this buffer. */
2511 reset_buffer_local_variables (current_buffer, 0);
2513 /* Force mode-line redisplay. Useful here because all major mode
2514 commands call this function. */
2515 update_mode_lines++;
2517 return Qnil;
2520 /* Make sure no local variables remain set up with buffer B
2521 for their current values. */
2523 static void
2524 swap_out_buffer_local_variables (b)
2525 struct buffer *b;
2527 Lisp_Object oalist, alist, sym, tem, buffer;
2529 XSETBUFFER (buffer, b);
2530 oalist = b->local_var_alist;
2532 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2534 sym = XCAR (XCAR (alist));
2536 /* Need not do anything if some other buffer's binding is now encached. */
2537 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2538 if (EQ (tem, buffer))
2540 /* Symbol is set up for this buffer's old local value:
2541 swap it out! */
2542 swap_in_global_binding (sym);
2547 /* Find all the overlays in the current buffer that contain position POS.
2548 Return the number found, and store them in a vector in *VEC_PTR.
2549 Store in *LEN_PTR the size allocated for the vector.
2550 Store in *NEXT_PTR the next position after POS where an overlay starts,
2551 or ZV if there are no more overlays between POS and ZV.
2552 Store in *PREV_PTR the previous position before POS where an overlay ends,
2553 or where an overlay starts which ends at or after POS;
2554 or BEGV if there are no such overlays from BEGV to POS.
2555 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2557 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2558 when this function is called.
2560 If EXTEND is non-zero, we make the vector bigger if necessary.
2561 If EXTEND is zero, we never extend the vector,
2562 and we store only as many overlays as will fit.
2563 But we still return the total number of overlays.
2565 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2566 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2567 default (BEGV or ZV). */
2570 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2571 EMACS_INT pos;
2572 int extend;
2573 Lisp_Object **vec_ptr;
2574 int *len_ptr;
2575 int *next_ptr;
2576 int *prev_ptr;
2577 int change_req;
2579 Lisp_Object overlay, start, end;
2580 struct Lisp_Overlay *tail;
2581 int idx = 0;
2582 int len = *len_ptr;
2583 Lisp_Object *vec = *vec_ptr;
2584 int next = ZV;
2585 int prev = BEGV;
2586 int inhibit_storing = 0;
2588 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2590 int startpos, endpos;
2592 XSETMISC (overlay, tail);
2594 start = OVERLAY_START (overlay);
2595 end = OVERLAY_END (overlay);
2596 endpos = OVERLAY_POSITION (end);
2597 if (endpos < pos)
2599 if (prev < endpos)
2600 prev = endpos;
2601 break;
2603 startpos = OVERLAY_POSITION (start);
2604 /* This one ends at or after POS
2605 so its start counts for PREV_PTR if it's before POS. */
2606 if (prev < startpos && startpos < pos)
2607 prev = startpos;
2608 if (endpos == pos)
2609 continue;
2610 if (startpos <= pos)
2612 if (idx == len)
2614 /* The supplied vector is full.
2615 Either make it bigger, or don't store any more in it. */
2616 if (extend)
2618 /* Make it work with an initial len == 0. */
2619 len *= 2;
2620 if (len == 0)
2621 len = 4;
2622 *len_ptr = len;
2623 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2624 *vec_ptr = vec;
2626 else
2627 inhibit_storing = 1;
2630 if (!inhibit_storing)
2631 vec[idx] = overlay;
2632 /* Keep counting overlays even if we can't return them all. */
2633 idx++;
2635 else if (startpos < next)
2636 next = startpos;
2639 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2641 int startpos, endpos;
2643 XSETMISC (overlay, tail);
2645 start = OVERLAY_START (overlay);
2646 end = OVERLAY_END (overlay);
2647 startpos = OVERLAY_POSITION (start);
2648 if (pos < startpos)
2650 if (startpos < next)
2651 next = startpos;
2652 break;
2654 endpos = OVERLAY_POSITION (end);
2655 if (pos < endpos)
2657 if (idx == len)
2659 if (extend)
2661 /* Make it work with an initial len == 0. */
2662 len *= 2;
2663 if (len == 0)
2664 len = 4;
2665 *len_ptr = len;
2666 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2667 *vec_ptr = vec;
2669 else
2670 inhibit_storing = 1;
2673 if (!inhibit_storing)
2674 vec[idx] = overlay;
2675 idx++;
2677 if (startpos < pos && startpos > prev)
2678 prev = startpos;
2680 else if (endpos < pos && endpos > prev)
2681 prev = endpos;
2682 else if (endpos == pos && startpos > prev
2683 && (!change_req || startpos < pos))
2684 prev = startpos;
2687 if (next_ptr)
2688 *next_ptr = next;
2689 if (prev_ptr)
2690 *prev_ptr = prev;
2691 return idx;
2694 /* Find all the overlays in the current buffer that overlap the range BEG-END
2695 or are empty at BEG.
2697 Return the number found, and store them in a vector in *VEC_PTR.
2698 Store in *LEN_PTR the size allocated for the vector.
2699 Store in *NEXT_PTR the next position after POS where an overlay starts,
2700 or ZV if there are no more overlays.
2701 Store in *PREV_PTR the previous position before POS where an overlay ends,
2702 or BEGV if there are no previous overlays.
2703 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2705 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2706 when this function is called.
2708 If EXTEND is non-zero, we make the vector bigger if necessary.
2709 If EXTEND is zero, we never extend the vector,
2710 and we store only as many overlays as will fit.
2711 But we still return the total number of overlays. */
2713 static int
2714 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2715 int beg, end;
2716 int extend;
2717 Lisp_Object **vec_ptr;
2718 int *len_ptr;
2719 int *next_ptr;
2720 int *prev_ptr;
2722 Lisp_Object overlay, ostart, oend;
2723 struct Lisp_Overlay *tail;
2724 int idx = 0;
2725 int len = *len_ptr;
2726 Lisp_Object *vec = *vec_ptr;
2727 int next = ZV;
2728 int prev = BEGV;
2729 int inhibit_storing = 0;
2731 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2733 int startpos, endpos;
2735 XSETMISC (overlay, tail);
2737 ostart = OVERLAY_START (overlay);
2738 oend = OVERLAY_END (overlay);
2739 endpos = OVERLAY_POSITION (oend);
2740 if (endpos < beg)
2742 if (prev < endpos)
2743 prev = endpos;
2744 break;
2746 startpos = OVERLAY_POSITION (ostart);
2747 /* Count an interval if it either overlaps the range
2748 or is empty at the start of the range. */
2749 if ((beg < endpos && startpos < end)
2750 || (startpos == endpos && beg == endpos))
2752 if (idx == len)
2754 /* The supplied vector is full.
2755 Either make it bigger, or don't store any more in it. */
2756 if (extend)
2758 /* Make it work with an initial len == 0. */
2759 len *= 2;
2760 if (len == 0)
2761 len = 4;
2762 *len_ptr = len;
2763 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2764 *vec_ptr = vec;
2766 else
2767 inhibit_storing = 1;
2770 if (!inhibit_storing)
2771 vec[idx] = overlay;
2772 /* Keep counting overlays even if we can't return them all. */
2773 idx++;
2775 else if (startpos < next)
2776 next = startpos;
2779 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2781 int startpos, endpos;
2783 XSETMISC (overlay, tail);
2785 ostart = OVERLAY_START (overlay);
2786 oend = OVERLAY_END (overlay);
2787 startpos = OVERLAY_POSITION (ostart);
2788 if (end < startpos)
2790 if (startpos < next)
2791 next = startpos;
2792 break;
2794 endpos = OVERLAY_POSITION (oend);
2795 /* Count an interval if it either overlaps the range
2796 or is empty at the start of the range. */
2797 if ((beg < endpos && startpos < end)
2798 || (startpos == endpos && beg == endpos))
2800 if (idx == len)
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 idx++;
2820 else if (endpos < beg && endpos > prev)
2821 prev = endpos;
2824 if (next_ptr)
2825 *next_ptr = next;
2826 if (prev_ptr)
2827 *prev_ptr = prev;
2828 return idx;
2832 /* Return non-zero if there exists an overlay with a non-nil
2833 `mouse-face' property overlapping OVERLAY. */
2836 mouse_face_overlay_overlaps (overlay)
2837 Lisp_Object overlay;
2839 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2840 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2841 int n, i, size;
2842 Lisp_Object *v, tem;
2844 size = 10;
2845 v = (Lisp_Object *) alloca (size * sizeof *v);
2846 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2847 if (n > size)
2849 v = (Lisp_Object *) alloca (n * sizeof *v);
2850 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2853 for (i = 0; i < n; ++i)
2854 if (!EQ (v[i], overlay)
2855 && (tem = Foverlay_get (overlay, Qmouse_face),
2856 !NILP (tem)))
2857 break;
2859 return i < n;
2864 /* Fast function to just test if we're at an overlay boundary. */
2866 overlay_touches_p (pos)
2867 int pos;
2869 Lisp_Object overlay;
2870 struct Lisp_Overlay *tail;
2872 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2874 int endpos;
2876 XSETMISC (overlay ,tail);
2877 if (!GC_OVERLAYP (overlay))
2878 abort ();
2880 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2881 if (endpos < pos)
2882 break;
2883 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2884 return 1;
2887 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2889 int startpos;
2891 XSETMISC (overlay, tail);
2892 if (!GC_OVERLAYP (overlay))
2893 abort ();
2895 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2896 if (pos < startpos)
2897 break;
2898 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2899 return 1;
2901 return 0;
2904 struct sortvec
2906 Lisp_Object overlay;
2907 int beg, end;
2908 int priority;
2911 static int
2912 compare_overlays (v1, v2)
2913 const void *v1, *v2;
2915 const struct sortvec *s1 = (const struct sortvec *) v1;
2916 const struct sortvec *s2 = (const struct sortvec *) v2;
2917 if (s1->priority != s2->priority)
2918 return s1->priority - s2->priority;
2919 if (s1->beg != s2->beg)
2920 return s1->beg - s2->beg;
2921 if (s1->end != s2->end)
2922 return s2->end - s1->end;
2923 return 0;
2926 /* Sort an array of overlays by priority. The array is modified in place.
2927 The return value is the new size; this may be smaller than the original
2928 size if some of the overlays were invalid or were window-specific. */
2930 sort_overlays (overlay_vec, noverlays, w)
2931 Lisp_Object *overlay_vec;
2932 int noverlays;
2933 struct window *w;
2935 int i, j;
2936 struct sortvec *sortvec;
2937 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2939 /* Put the valid and relevant overlays into sortvec. */
2941 for (i = 0, j = 0; i < noverlays; i++)
2943 Lisp_Object tem;
2944 Lisp_Object overlay;
2946 overlay = overlay_vec[i];
2947 if (OVERLAY_VALID (overlay)
2948 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2949 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2951 /* If we're interested in a specific window, then ignore
2952 overlays that are limited to some other window. */
2953 if (w)
2955 Lisp_Object window;
2957 window = Foverlay_get (overlay, Qwindow);
2958 if (WINDOWP (window) && XWINDOW (window) != w)
2959 continue;
2962 /* This overlay is good and counts: put it into sortvec. */
2963 sortvec[j].overlay = overlay;
2964 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2965 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2966 tem = Foverlay_get (overlay, Qpriority);
2967 if (INTEGERP (tem))
2968 sortvec[j].priority = XINT (tem);
2969 else
2970 sortvec[j].priority = 0;
2971 j++;
2974 noverlays = j;
2976 /* Sort the overlays into the proper order: increasing priority. */
2978 if (noverlays > 1)
2979 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2981 for (i = 0; i < noverlays; i++)
2982 overlay_vec[i] = sortvec[i].overlay;
2983 return (noverlays);
2986 struct sortstr
2988 Lisp_Object string, string2;
2989 int size;
2990 int priority;
2993 struct sortstrlist
2995 struct sortstr *buf; /* An array that expands as needed; never freed. */
2996 int size; /* Allocated length of that array. */
2997 int used; /* How much of the array is currently in use. */
2998 int bytes; /* Total length of the strings in buf. */
3001 /* Buffers for storing information about the overlays touching a given
3002 position. These could be automatic variables in overlay_strings, but
3003 it's more efficient to hold onto the memory instead of repeatedly
3004 allocating and freeing it. */
3005 static struct sortstrlist overlay_heads, overlay_tails;
3006 static unsigned char *overlay_str_buf;
3008 /* Allocated length of overlay_str_buf. */
3009 static int overlay_str_len;
3011 /* A comparison function suitable for passing to qsort. */
3012 static int
3013 cmp_for_strings (as1, as2)
3014 char *as1, *as2;
3016 struct sortstr *s1 = (struct sortstr *)as1;
3017 struct sortstr *s2 = (struct sortstr *)as2;
3018 if (s1->size != s2->size)
3019 return s2->size - s1->size;
3020 if (s1->priority != s2->priority)
3021 return s1->priority - s2->priority;
3022 return 0;
3025 static void
3026 record_overlay_string (ssl, str, str2, pri, size)
3027 struct sortstrlist *ssl;
3028 Lisp_Object str, str2, pri;
3029 int size;
3031 int nbytes;
3033 if (ssl->used == ssl->size)
3035 if (ssl->buf)
3036 ssl->size *= 2;
3037 else
3038 ssl->size = 5;
3039 ssl->buf = ((struct sortstr *)
3040 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3042 ssl->buf[ssl->used].string = str;
3043 ssl->buf[ssl->used].string2 = str2;
3044 ssl->buf[ssl->used].size = size;
3045 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3046 ssl->used++;
3048 if (NILP (current_buffer->enable_multibyte_characters))
3049 nbytes = SCHARS (str);
3050 else if (! STRING_MULTIBYTE (str))
3051 nbytes = count_size_as_multibyte (SDATA (str),
3052 SBYTES (str));
3053 else
3054 nbytes = SBYTES (str);
3056 ssl->bytes += nbytes;
3058 if (STRINGP (str2))
3060 if (NILP (current_buffer->enable_multibyte_characters))
3061 nbytes = SCHARS (str2);
3062 else if (! STRING_MULTIBYTE (str2))
3063 nbytes = count_size_as_multibyte (SDATA (str2),
3064 SBYTES (str2));
3065 else
3066 nbytes = SBYTES (str2);
3068 ssl->bytes += nbytes;
3072 /* Return the concatenation of the strings associated with overlays that
3073 begin or end at POS, ignoring overlays that are specific to a window
3074 other than W. The strings are concatenated in the appropriate order:
3075 shorter overlays nest inside longer ones, and higher priority inside
3076 lower. Normally all of the after-strings come first, but zero-sized
3077 overlays have their after-strings ride along with the before-strings
3078 because it would look strange to print them inside-out.
3080 Returns the string length, and stores the contents indirectly through
3081 PSTR, if that variable is non-null. The string may be overwritten by
3082 subsequent calls. */
3085 overlay_strings (pos, w, pstr)
3086 EMACS_INT pos;
3087 struct window *w;
3088 unsigned char **pstr;
3090 Lisp_Object overlay, window, str;
3091 struct Lisp_Overlay *ov;
3092 int startpos, endpos;
3093 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3095 overlay_heads.used = overlay_heads.bytes = 0;
3096 overlay_tails.used = overlay_tails.bytes = 0;
3097 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3099 XSETMISC (overlay, ov);
3100 eassert (OVERLAYP (overlay));
3102 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3103 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3104 if (endpos < pos)
3105 break;
3106 if (endpos != pos && startpos != pos)
3107 continue;
3108 window = Foverlay_get (overlay, Qwindow);
3109 if (WINDOWP (window) && XWINDOW (window) != w)
3110 continue;
3111 if (startpos == pos
3112 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3113 record_overlay_string (&overlay_heads, str,
3114 (startpos == endpos
3115 ? Foverlay_get (overlay, Qafter_string)
3116 : Qnil),
3117 Foverlay_get (overlay, Qpriority),
3118 endpos - startpos);
3119 else if (endpos == pos
3120 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3121 record_overlay_string (&overlay_tails, str, Qnil,
3122 Foverlay_get (overlay, Qpriority),
3123 endpos - startpos);
3125 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3127 XSETMISC (overlay, ov);
3128 eassert (OVERLAYP (overlay));
3130 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3131 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3132 if (startpos > pos)
3133 break;
3134 if (endpos != pos && startpos != pos)
3135 continue;
3136 window = Foverlay_get (overlay, Qwindow);
3137 if (WINDOWP (window) && XWINDOW (window) != w)
3138 continue;
3139 if (startpos == pos
3140 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3141 record_overlay_string (&overlay_heads, str,
3142 (startpos == endpos
3143 ? Foverlay_get (overlay, Qafter_string)
3144 : Qnil),
3145 Foverlay_get (overlay, Qpriority),
3146 endpos - startpos);
3147 else if (endpos == pos
3148 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3149 record_overlay_string (&overlay_tails, str, Qnil,
3150 Foverlay_get (overlay, Qpriority),
3151 endpos - startpos);
3153 if (overlay_tails.used > 1)
3154 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3155 cmp_for_strings);
3156 if (overlay_heads.used > 1)
3157 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3158 cmp_for_strings);
3159 if (overlay_heads.bytes || overlay_tails.bytes)
3161 Lisp_Object tem;
3162 int i;
3163 unsigned char *p;
3164 int total = overlay_heads.bytes + overlay_tails.bytes;
3166 if (total > overlay_str_len)
3168 overlay_str_len = total;
3169 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3170 total);
3172 p = overlay_str_buf;
3173 for (i = overlay_tails.used; --i >= 0;)
3175 int nbytes;
3176 tem = overlay_tails.buf[i].string;
3177 nbytes = copy_text (SDATA (tem), p,
3178 SBYTES (tem),
3179 STRING_MULTIBYTE (tem), multibyte);
3180 p += nbytes;
3182 for (i = 0; i < overlay_heads.used; ++i)
3184 int nbytes;
3185 tem = overlay_heads.buf[i].string;
3186 nbytes = copy_text (SDATA (tem), p,
3187 SBYTES (tem),
3188 STRING_MULTIBYTE (tem), multibyte);
3189 p += nbytes;
3190 tem = overlay_heads.buf[i].string2;
3191 if (STRINGP (tem))
3193 nbytes = copy_text (SDATA (tem), p,
3194 SBYTES (tem),
3195 STRING_MULTIBYTE (tem), multibyte);
3196 p += nbytes;
3199 if (p != overlay_str_buf + total)
3200 abort ();
3201 if (pstr)
3202 *pstr = overlay_str_buf;
3203 return total;
3205 return 0;
3208 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3210 void
3211 recenter_overlay_lists (buf, pos)
3212 struct buffer *buf;
3213 EMACS_INT pos;
3215 Lisp_Object overlay, beg, end;
3216 struct Lisp_Overlay *prev, *tail, *next;
3218 /* See if anything in overlays_before should move to overlays_after. */
3220 /* We don't strictly need prev in this loop; it should always be nil.
3221 But we use it for symmetry and in case that should cease to be true
3222 with some future change. */
3223 prev = NULL;
3224 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3226 next = tail->next;
3227 XSETMISC (overlay, tail);
3229 /* If the overlay is not valid, get rid of it. */
3230 if (!OVERLAY_VALID (overlay))
3231 #if 1
3232 abort ();
3233 #else
3235 /* Splice the cons cell TAIL out of overlays_before. */
3236 if (!NILP (prev))
3237 XCDR (prev) = next;
3238 else
3239 buf->overlays_before = next;
3240 tail = prev;
3241 continue;
3243 #endif
3245 beg = OVERLAY_START (overlay);
3246 end = OVERLAY_END (overlay);
3248 if (OVERLAY_POSITION (end) > pos)
3250 /* OVERLAY needs to be moved. */
3251 int where = OVERLAY_POSITION (beg);
3252 struct Lisp_Overlay *other, *other_prev;
3254 /* Splice the cons cell TAIL out of overlays_before. */
3255 if (prev)
3256 prev->next = next;
3257 else
3258 buf->overlays_before = next;
3260 /* Search thru overlays_after for where to put it. */
3261 other_prev = NULL;
3262 for (other = buf->overlays_after; other;
3263 other_prev = other, other = other->next)
3265 Lisp_Object otherbeg, otheroverlay;
3267 XSETMISC (otheroverlay, other);
3268 eassert (OVERLAY_VALID (otheroverlay));
3270 otherbeg = OVERLAY_START (otheroverlay);
3271 if (OVERLAY_POSITION (otherbeg) >= where)
3272 break;
3275 /* Add TAIL to overlays_after before OTHER. */
3276 tail->next = other;
3277 if (other_prev)
3278 other_prev->next = tail;
3279 else
3280 buf->overlays_after = tail;
3281 tail = prev;
3283 else
3284 /* We've reached the things that should stay in overlays_before.
3285 All the rest of overlays_before must end even earlier,
3286 so stop now. */
3287 break;
3290 /* See if anything in overlays_after should be in overlays_before. */
3291 prev = NULL;
3292 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3294 next = tail->next;
3295 XSETMISC (overlay, tail);
3297 /* If the overlay is not valid, get rid of it. */
3298 if (!OVERLAY_VALID (overlay))
3299 #if 1
3300 abort ();
3301 #else
3303 /* Splice the cons cell TAIL out of overlays_after. */
3304 if (!NILP (prev))
3305 XCDR (prev) = next;
3306 else
3307 buf->overlays_after = next;
3308 tail = prev;
3309 continue;
3311 #endif
3313 beg = OVERLAY_START (overlay);
3314 end = OVERLAY_END (overlay);
3316 /* Stop looking, when we know that nothing further
3317 can possibly end before POS. */
3318 if (OVERLAY_POSITION (beg) > pos)
3319 break;
3321 if (OVERLAY_POSITION (end) <= pos)
3323 /* OVERLAY needs to be moved. */
3324 int where = OVERLAY_POSITION (end);
3325 struct Lisp_Overlay *other, *other_prev;
3327 /* Splice the cons cell TAIL out of overlays_after. */
3328 if (prev)
3329 prev->next = next;
3330 else
3331 buf->overlays_after = next;
3333 /* Search thru overlays_before for where to put it. */
3334 other_prev = NULL;
3335 for (other = buf->overlays_before; other;
3336 other_prev = other, other = other->next)
3338 Lisp_Object otherend, otheroverlay;
3340 XSETMISC (otheroverlay, other);
3341 eassert (OVERLAY_VALID (otheroverlay));
3343 otherend = OVERLAY_END (otheroverlay);
3344 if (OVERLAY_POSITION (otherend) <= where)
3345 break;
3348 /* Add TAIL to overlays_before before OTHER. */
3349 tail->next = other;
3350 if (other_prev)
3351 other_prev->next = tail;
3352 else
3353 buf->overlays_before = tail;
3354 tail = prev;
3358 buf->overlay_center = pos;
3361 void
3362 adjust_overlays_for_insert (pos, length)
3363 EMACS_INT pos;
3364 EMACS_INT length;
3366 /* After an insertion, the lists are still sorted properly,
3367 but we may need to update the value of the overlay center. */
3368 if (current_buffer->overlay_center >= pos)
3369 current_buffer->overlay_center += length;
3372 void
3373 adjust_overlays_for_delete (pos, length)
3374 EMACS_INT pos;
3375 EMACS_INT length;
3377 if (current_buffer->overlay_center < pos)
3378 /* The deletion was to our right. No change needed; the before- and
3379 after-lists are still consistent. */
3381 else if (current_buffer->overlay_center > pos + length)
3382 /* The deletion was to our left. We need to adjust the center value
3383 to account for the change in position, but the lists are consistent
3384 given the new value. */
3385 current_buffer->overlay_center -= length;
3386 else
3387 /* We're right in the middle. There might be things on the after-list
3388 that now belong on the before-list. Recentering will move them,
3389 and also update the center point. */
3390 recenter_overlay_lists (current_buffer, pos);
3393 /* Fix up overlays that were garbled as a result of permuting markers
3394 in the range START through END. Any overlay with at least one
3395 endpoint in this range will need to be unlinked from the overlay
3396 list and reinserted in its proper place.
3397 Such an overlay might even have negative size at this point.
3398 If so, we'll make the overlay empty. */
3399 void
3400 fix_start_end_in_overlays (start, end)
3401 register int start, end;
3403 Lisp_Object overlay;
3404 struct Lisp_Overlay *before_list, *after_list;
3405 /* These are either nil, indicating that before_list or after_list
3406 should be assigned, or the cons cell the cdr of which should be
3407 assigned. */
3408 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3409 /* 'Parent', likewise, indicates a cons cell or
3410 current_buffer->overlays_before or overlays_after, depending
3411 which loop we're in. */
3412 struct Lisp_Overlay *tail, *parent;
3413 int startpos, endpos;
3415 /* This algorithm shifts links around instead of consing and GCing.
3416 The loop invariant is that before_list (resp. after_list) is a
3417 well-formed list except that its last element, the CDR of beforep
3418 (resp. afterp) if beforep (afterp) isn't nil or before_list
3419 (after_list) if it is, is still uninitialized. So it's not a bug
3420 that before_list isn't initialized, although it may look
3421 strange. */
3422 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3424 XSETMISC (overlay, tail);
3426 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3427 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3429 /* If the overlay is backwards, make it empty. */
3430 if (endpos < startpos)
3432 startpos = endpos;
3433 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3434 Qnil);
3437 if (endpos < start)
3438 break;
3440 if (endpos < end
3441 || (startpos >= start && startpos < end))
3443 /* Add it to the end of the wrong list. Later on,
3444 recenter_overlay_lists will move it to the right place. */
3445 if (endpos < current_buffer->overlay_center)
3447 if (!afterp)
3448 after_list = tail;
3449 else
3450 afterp->next = tail;
3451 afterp = tail;
3453 else
3455 if (!beforep)
3456 before_list = tail;
3457 else
3458 beforep->next = tail;
3459 beforep = tail;
3461 if (!parent)
3462 current_buffer->overlays_before = tail->next;
3463 else
3464 parent->next = tail->next;
3465 tail = tail->next;
3467 else
3468 parent = tail, tail = parent->next;
3470 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3472 XSETMISC (overlay, tail);
3474 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3475 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3477 /* If the overlay is backwards, make it empty. */
3478 if (endpos < startpos)
3480 startpos = endpos;
3481 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3482 Qnil);
3485 if (startpos >= end)
3486 break;
3488 if (startpos >= start
3489 || (endpos >= start && endpos < end))
3491 if (endpos < current_buffer->overlay_center)
3493 if (!afterp)
3494 after_list = tail;
3495 else
3496 afterp->next = tail;
3497 afterp = tail;
3499 else
3501 if (!beforep)
3502 before_list = tail;
3503 else
3504 beforep->next = tail;
3505 beforep = tail;
3507 if (!parent)
3508 current_buffer->overlays_after = tail->next;
3509 else
3510 parent->next = tail->next;
3511 tail = tail->next;
3513 else
3514 parent = tail, tail = parent->next;
3517 /* Splice the constructed (wrong) lists into the buffer's lists,
3518 and let the recenter function make it sane again. */
3519 if (beforep)
3521 beforep->next = current_buffer->overlays_before;
3522 current_buffer->overlays_before = before_list;
3524 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3526 if (afterp)
3528 afterp->next = current_buffer->overlays_after;
3529 current_buffer->overlays_after = after_list;
3531 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3534 /* We have two types of overlay: the one whose ending marker is
3535 after-insertion-marker (this is the usual case) and the one whose
3536 ending marker is before-insertion-marker. When `overlays_before'
3537 contains overlays of the latter type and the former type in this
3538 order and both overlays end at inserting position, inserting a text
3539 increases only the ending marker of the latter type, which results
3540 in incorrect ordering of `overlays_before'.
3542 This function fixes ordering of overlays in the slot
3543 `overlays_before' of the buffer *BP. Before the insertion, `point'
3544 was at PREV, and now is at POS. */
3546 void
3547 fix_overlays_before (bp, prev, pos)
3548 struct buffer *bp;
3549 EMACS_INT prev, pos;
3551 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3552 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3553 Lisp_Object tem;
3554 EMACS_INT end;
3556 /* After the insertion, the several overlays may be in incorrect
3557 order. The possibility is that, in the list `overlays_before',
3558 an overlay which ends at POS appears after an overlay which ends
3559 at PREV. Since POS is greater than PREV, we must fix the
3560 ordering of these overlays, by moving overlays ends at POS before
3561 the overlays ends at PREV. */
3563 /* At first, find a place where disordered overlays should be linked
3564 in. It is where an overlay which end before POS exists. (i.e. an
3565 overlay whose ending marker is after-insertion-marker if disorder
3566 exists). */
3567 while (tail
3568 && (XSETMISC (tem, tail),
3569 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3571 parent = tail;
3572 tail = tail->next;
3575 /* If we don't find such an overlay,
3576 or the found one ends before PREV,
3577 or the found one is the last one in the list,
3578 we don't have to fix anything. */
3579 if (!tail || end < prev || !tail->next)
3580 return;
3582 right_pair = parent;
3583 parent = tail;
3584 tail = tail->next;
3586 /* Now, end position of overlays in the list TAIL should be before
3587 or equal to PREV. In the loop, an overlay which ends at POS is
3588 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3589 we found an overlay which ends before PREV, the remaining
3590 overlays are in correct order. */
3591 while (tail)
3593 XSETMISC (tem, tail);
3594 end = OVERLAY_POSITION (OVERLAY_END (tem));
3596 if (end == pos)
3597 { /* This overlay is disordered. */
3598 struct Lisp_Overlay *found = tail;
3600 /* Unlink the found overlay. */
3601 tail = found->next;
3602 parent->next = tail;
3603 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3604 and link it into the right place. */
3605 if (!right_pair)
3607 found->next = bp->overlays_before;
3608 bp->overlays_before = found;
3610 else
3612 found->next = right_pair->next;
3613 right_pair->next = found;
3616 else if (end == prev)
3618 parent = tail;
3619 tail = tail->next;
3621 else /* No more disordered overlay. */
3622 break;
3626 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3627 doc: /* Return t if OBJECT is an overlay. */)
3628 (object)
3629 Lisp_Object object;
3631 return (OVERLAYP (object) ? Qt : Qnil);
3634 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3635 doc: /* Create a new overlay with range BEG to END in BUFFER.
3636 If omitted, BUFFER defaults to the current buffer.
3637 BEG and END may be integers or markers.
3638 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3639 for the front of the overlay advance when text is inserted there
3640 \(which means the text *is not* included in the overlay).
3641 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3642 for the rear of the overlay advance when text is inserted there
3643 \(which means the text *is* included in the overlay). */)
3644 (beg, end, buffer, front_advance, rear_advance)
3645 Lisp_Object beg, end, buffer;
3646 Lisp_Object front_advance, rear_advance;
3648 Lisp_Object overlay;
3649 struct buffer *b;
3651 if (NILP (buffer))
3652 XSETBUFFER (buffer, current_buffer);
3653 else
3654 CHECK_BUFFER (buffer);
3655 if (MARKERP (beg)
3656 && ! EQ (Fmarker_buffer (beg), buffer))
3657 error ("Marker points into wrong buffer");
3658 if (MARKERP (end)
3659 && ! EQ (Fmarker_buffer (end), buffer))
3660 error ("Marker points into wrong buffer");
3662 CHECK_NUMBER_COERCE_MARKER (beg);
3663 CHECK_NUMBER_COERCE_MARKER (end);
3665 if (XINT (beg) > XINT (end))
3667 Lisp_Object temp;
3668 temp = beg; beg = end; end = temp;
3671 b = XBUFFER (buffer);
3673 beg = Fset_marker (Fmake_marker (), beg, buffer);
3674 end = Fset_marker (Fmake_marker (), end, buffer);
3676 if (!NILP (front_advance))
3677 XMARKER (beg)->insertion_type = 1;
3678 if (!NILP (rear_advance))
3679 XMARKER (end)->insertion_type = 1;
3681 overlay = allocate_misc ();
3682 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3683 XOVERLAY (overlay)->start = beg;
3684 XOVERLAY (overlay)->end = end;
3685 XOVERLAY (overlay)->plist = Qnil;
3686 XOVERLAY (overlay)->next = NULL;
3688 /* Put the new overlay on the wrong list. */
3689 end = OVERLAY_END (overlay);
3690 if (OVERLAY_POSITION (end) < b->overlay_center)
3692 if (b->overlays_after)
3693 XOVERLAY (overlay)->next = b->overlays_after;
3694 b->overlays_after = XOVERLAY (overlay);
3696 else
3698 if (b->overlays_before)
3699 XOVERLAY (overlay)->next = b->overlays_before;
3700 b->overlays_before = XOVERLAY (overlay);
3703 /* This puts it in the right list, and in the right order. */
3704 recenter_overlay_lists (b, b->overlay_center);
3706 /* We don't need to redisplay the region covered by the overlay, because
3707 the overlay has no properties at the moment. */
3709 return overlay;
3712 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3714 static void
3715 modify_overlay (buf, start, end)
3716 struct buffer *buf;
3717 EMACS_INT start, end;
3719 if (start > end)
3721 int temp = start;
3722 start = end;
3723 end = temp;
3726 BUF_COMPUTE_UNCHANGED (buf, start, end);
3728 /* If this is a buffer not in the selected window,
3729 we must do other windows. */
3730 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3731 windows_or_buffers_changed = 1;
3732 /* If multiple windows show this buffer, we must do other windows. */
3733 else if (buffer_shared > 1)
3734 windows_or_buffers_changed = 1;
3735 /* If we modify an overlay at the end of the buffer, we cannot
3736 be sure that window end is still valid. */
3737 else if (end >= ZV && start <= ZV)
3738 windows_or_buffers_changed = 1;
3740 ++BUF_OVERLAY_MODIFF (buf);
3744 Lisp_Object Fdelete_overlay ();
3746 static struct Lisp_Overlay *
3747 unchain_overlay (list, overlay)
3748 struct Lisp_Overlay *list, *overlay;
3750 struct Lisp_Overlay *tmp, *prev;
3751 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3752 if (tmp == overlay)
3754 if (prev)
3755 prev->next = tmp->next;
3756 else
3757 list = tmp->next;
3758 overlay->next = NULL;
3759 break;
3761 return list;
3764 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3765 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3766 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3767 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3768 buffer. */)
3769 (overlay, beg, end, buffer)
3770 Lisp_Object overlay, beg, end, buffer;
3772 struct buffer *b, *ob;
3773 Lisp_Object obuffer;
3774 int count = SPECPDL_INDEX ();
3776 CHECK_OVERLAY (overlay);
3777 if (NILP (buffer))
3778 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3779 if (NILP (buffer))
3780 XSETBUFFER (buffer, current_buffer);
3781 CHECK_BUFFER (buffer);
3783 if (MARKERP (beg)
3784 && ! EQ (Fmarker_buffer (beg), buffer))
3785 error ("Marker points into wrong buffer");
3786 if (MARKERP (end)
3787 && ! EQ (Fmarker_buffer (end), buffer))
3788 error ("Marker points into wrong buffer");
3790 CHECK_NUMBER_COERCE_MARKER (beg);
3791 CHECK_NUMBER_COERCE_MARKER (end);
3793 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3794 return Fdelete_overlay (overlay);
3796 if (XINT (beg) > XINT (end))
3798 Lisp_Object temp;
3799 temp = beg; beg = end; end = temp;
3802 specbind (Qinhibit_quit, Qt);
3804 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3805 b = XBUFFER (buffer);
3806 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3808 /* If the overlay has changed buffers, do a thorough redisplay. */
3809 if (!EQ (buffer, obuffer))
3811 /* Redisplay where the overlay was. */
3812 if (!NILP (obuffer))
3814 int o_beg;
3815 int o_end;
3817 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3818 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3820 modify_overlay (ob, o_beg, o_end);
3823 /* Redisplay where the overlay is going to be. */
3824 modify_overlay (b, XINT (beg), XINT (end));
3826 else
3827 /* Redisplay the area the overlay has just left, or just enclosed. */
3829 int o_beg, o_end;
3831 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3832 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3834 if (o_beg == XINT (beg))
3835 modify_overlay (b, o_end, XINT (end));
3836 else if (o_end == XINT (end))
3837 modify_overlay (b, o_beg, XINT (beg));
3838 else
3840 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3841 if (XINT (end) > o_end) o_end = XINT (end);
3842 modify_overlay (b, o_beg, o_end);
3846 if (!NILP (obuffer))
3848 ob->overlays_before
3849 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3850 ob->overlays_after
3851 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3852 eassert (XOVERLAY (overlay)->next == NULL);
3855 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3856 Fset_marker (OVERLAY_END (overlay), end, buffer);
3858 /* Put the overlay on the wrong list. */
3859 end = OVERLAY_END (overlay);
3860 if (OVERLAY_POSITION (end) < b->overlay_center)
3862 XOVERLAY (overlay)->next = b->overlays_after;
3863 b->overlays_after = XOVERLAY (overlay);
3865 else
3867 XOVERLAY (overlay)->next = b->overlays_before;
3868 b->overlays_before = XOVERLAY (overlay);
3871 /* This puts it in the right list, and in the right order. */
3872 recenter_overlay_lists (b, b->overlay_center);
3874 return unbind_to (count, overlay);
3877 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3878 doc: /* Delete the overlay OVERLAY from its buffer. */)
3879 (overlay)
3880 Lisp_Object overlay;
3882 Lisp_Object buffer;
3883 struct buffer *b;
3884 int count = SPECPDL_INDEX ();
3886 CHECK_OVERLAY (overlay);
3888 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3889 if (NILP (buffer))
3890 return Qnil;
3892 b = XBUFFER (buffer);
3893 specbind (Qinhibit_quit, Qt);
3895 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3896 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3897 eassert (XOVERLAY (overlay)->next == NULL);
3898 modify_overlay (b,
3899 marker_position (OVERLAY_START (overlay)),
3900 marker_position (OVERLAY_END (overlay)));
3901 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3902 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3904 /* When deleting an overlay with before or after strings, turn off
3905 display optimizations for the affected buffer, on the basis that
3906 these strings may contain newlines. This is easier to do than to
3907 check for that situation during redisplay. */
3908 if (!windows_or_buffers_changed
3909 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3910 || !NILP (Foverlay_get (overlay, Qafter_string))))
3911 b->prevent_redisplay_optimizations_p = 1;
3913 return unbind_to (count, Qnil);
3916 /* Overlay dissection functions. */
3918 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3919 doc: /* Return the position at which OVERLAY starts. */)
3920 (overlay)
3921 Lisp_Object overlay;
3923 CHECK_OVERLAY (overlay);
3925 return (Fmarker_position (OVERLAY_START (overlay)));
3928 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3929 doc: /* Return the position at which OVERLAY ends. */)
3930 (overlay)
3931 Lisp_Object overlay;
3933 CHECK_OVERLAY (overlay);
3935 return (Fmarker_position (OVERLAY_END (overlay)));
3938 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3939 doc: /* Return the buffer OVERLAY belongs to.
3940 Return nil if OVERLAY has been deleted. */)
3941 (overlay)
3942 Lisp_Object overlay;
3944 CHECK_OVERLAY (overlay);
3946 return Fmarker_buffer (OVERLAY_START (overlay));
3949 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3950 doc: /* Return a list of the properties on OVERLAY.
3951 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3952 OVERLAY. */)
3953 (overlay)
3954 Lisp_Object overlay;
3956 CHECK_OVERLAY (overlay);
3958 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3962 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3963 doc: /* Return a list of the overlays that contain the character at POS. */)
3964 (pos)
3965 Lisp_Object pos;
3967 int noverlays;
3968 Lisp_Object *overlay_vec;
3969 int len;
3970 Lisp_Object result;
3972 CHECK_NUMBER_COERCE_MARKER (pos);
3974 len = 10;
3975 /* We can't use alloca here because overlays_at can call xrealloc. */
3976 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3978 /* Put all the overlays we want in a vector in overlay_vec.
3979 Store the length in len. */
3980 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3981 (int *) 0, (int *) 0, 0);
3983 /* Make a list of them all. */
3984 result = Flist (noverlays, overlay_vec);
3986 xfree (overlay_vec);
3987 return result;
3990 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3991 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3992 Overlap means that at least one character is contained within the overlay
3993 and also contained within the specified region.
3994 Empty overlays are included in the result if they are located at BEG
3995 or between BEG and END. */)
3996 (beg, end)
3997 Lisp_Object beg, end;
3999 int noverlays;
4000 Lisp_Object *overlay_vec;
4001 int len;
4002 Lisp_Object result;
4004 CHECK_NUMBER_COERCE_MARKER (beg);
4005 CHECK_NUMBER_COERCE_MARKER (end);
4007 len = 10;
4008 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4010 /* Put all the overlays we want in a vector in overlay_vec.
4011 Store the length in len. */
4012 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4013 (int *) 0, (int *) 0);
4015 /* Make a list of them all. */
4016 result = Flist (noverlays, overlay_vec);
4018 xfree (overlay_vec);
4019 return result;
4022 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4023 1, 1, 0,
4024 doc: /* Return the next position after POS where an overlay starts or ends.
4025 If there are no overlay boundaries from POS to (point-max),
4026 the value is (point-max). */)
4027 (pos)
4028 Lisp_Object pos;
4030 int noverlays;
4031 int endpos;
4032 Lisp_Object *overlay_vec;
4033 int len;
4034 int i;
4036 CHECK_NUMBER_COERCE_MARKER (pos);
4038 len = 10;
4039 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4041 /* Put all the overlays we want in a vector in overlay_vec.
4042 Store the length in len.
4043 endpos gets the position where the next overlay starts. */
4044 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4045 &endpos, (int *) 0, 1);
4047 /* If any of these overlays ends before endpos,
4048 use its ending point instead. */
4049 for (i = 0; i < noverlays; i++)
4051 Lisp_Object oend;
4052 int oendpos;
4054 oend = OVERLAY_END (overlay_vec[i]);
4055 oendpos = OVERLAY_POSITION (oend);
4056 if (oendpos < endpos)
4057 endpos = oendpos;
4060 xfree (overlay_vec);
4061 return make_number (endpos);
4064 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4065 Sprevious_overlay_change, 1, 1, 0,
4066 doc: /* Return the previous position before POS where an overlay starts or ends.
4067 If there are no overlay boundaries from (point-min) to POS,
4068 the value is (point-min). */)
4069 (pos)
4070 Lisp_Object pos;
4072 int noverlays;
4073 int prevpos;
4074 Lisp_Object *overlay_vec;
4075 int len;
4077 CHECK_NUMBER_COERCE_MARKER (pos);
4079 /* At beginning of buffer, we know the answer;
4080 avoid bug subtracting 1 below. */
4081 if (XINT (pos) == BEGV)
4082 return pos;
4084 len = 10;
4085 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4087 /* Put all the overlays we want in a vector in overlay_vec.
4088 Store the length in len.
4089 prevpos gets the position of the previous change. */
4090 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4091 (int *) 0, &prevpos, 1);
4093 xfree (overlay_vec);
4094 return make_number (prevpos);
4097 /* These functions are for debugging overlays. */
4099 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4100 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4101 The car has all the overlays before the overlay center;
4102 the cdr has all the overlays after the overlay center.
4103 Recentering overlays moves overlays between these lists.
4104 The lists you get are copies, so that changing them has no effect.
4105 However, the overlays you get are the real objects that the buffer uses. */)
4108 struct Lisp_Overlay *ol;
4109 Lisp_Object before = Qnil, after = Qnil, tmp;
4110 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4112 XSETMISC (tmp, ol);
4113 before = Fcons (tmp, before);
4115 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4117 XSETMISC (tmp, ol);
4118 after = Fcons (tmp, after);
4120 return Fcons (Fnreverse (before), Fnreverse (after));
4123 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4124 doc: /* Recenter the overlays of the current buffer around position POS.
4125 That makes overlay lookup faster for positions near POS (but perhaps slower
4126 for positions far away from POS). */)
4127 (pos)
4128 Lisp_Object pos;
4130 CHECK_NUMBER_COERCE_MARKER (pos);
4132 recenter_overlay_lists (current_buffer, XINT (pos));
4133 return Qnil;
4136 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4137 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4138 (overlay, prop)
4139 Lisp_Object overlay, prop;
4141 CHECK_OVERLAY (overlay);
4142 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4145 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4146 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4147 (overlay, prop, value)
4148 Lisp_Object overlay, prop, value;
4150 Lisp_Object tail, buffer;
4151 int changed;
4153 CHECK_OVERLAY (overlay);
4155 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4157 for (tail = XOVERLAY (overlay)->plist;
4158 CONSP (tail) && CONSP (XCDR (tail));
4159 tail = XCDR (XCDR (tail)))
4160 if (EQ (XCAR (tail), prop))
4162 changed = !EQ (XCAR (XCDR (tail)), value);
4163 XSETCAR (XCDR (tail), value);
4164 goto found;
4166 /* It wasn't in the list, so add it to the front. */
4167 changed = !NILP (value);
4168 XOVERLAY (overlay)->plist
4169 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4170 found:
4171 if (! NILP (buffer))
4173 if (changed)
4174 modify_overlay (XBUFFER (buffer),
4175 marker_position (OVERLAY_START (overlay)),
4176 marker_position (OVERLAY_END (overlay)));
4177 if (EQ (prop, Qevaporate) && ! NILP (value)
4178 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4179 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4180 Fdelete_overlay (overlay);
4183 return value;
4186 /* Subroutine of report_overlay_modification. */
4188 /* Lisp vector holding overlay hook functions to call.
4189 Vector elements come in pairs.
4190 Each even-index element is a list of hook functions.
4191 The following odd-index element is the overlay they came from.
4193 Before the buffer change, we fill in this vector
4194 as we call overlay hook functions.
4195 After the buffer change, we get the functions to call from this vector.
4196 This way we always call the same functions before and after the change. */
4197 static Lisp_Object last_overlay_modification_hooks;
4199 /* Number of elements actually used in last_overlay_modification_hooks. */
4200 static int last_overlay_modification_hooks_used;
4202 /* Add one functionlist/overlay pair
4203 to the end of last_overlay_modification_hooks. */
4205 static void
4206 add_overlay_mod_hooklist (functionlist, overlay)
4207 Lisp_Object functionlist, overlay;
4209 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4211 if (last_overlay_modification_hooks_used == oldsize)
4212 last_overlay_modification_hooks = larger_vector
4213 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4214 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = functionlist;
4215 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = overlay;
4218 /* Run the modification-hooks of overlays that include
4219 any part of the text in START to END.
4220 If this change is an insertion, also
4221 run the insert-before-hooks of overlay starting at END,
4222 and the insert-after-hooks of overlay ending at START.
4224 This is called both before and after the modification.
4225 AFTER is nonzero when we call after the modification.
4227 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4228 When AFTER is nonzero, they are the start position,
4229 the position after the inserted new text,
4230 and the length of deleted or replaced old text. */
4232 void
4233 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4234 Lisp_Object start, end;
4235 int after;
4236 Lisp_Object arg1, arg2, arg3;
4238 Lisp_Object prop, overlay;
4239 struct Lisp_Overlay *tail;
4240 /* 1 if this change is an insertion. */
4241 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4242 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4244 overlay = Qnil;
4245 tail = NULL;
4247 /* We used to run the functions as soon as we found them and only register
4248 them in last_overlay_modification_hooks for the purpose of the `after'
4249 case. But running elisp code as we traverse the list of overlays is
4250 painful because the list can be modified by the elisp code so we had to
4251 copy at several places. We now simply do a read-only traversal that
4252 only collects the functions to run and we run them afterwards. It's
4253 simpler, especially since all the code was already there. -stef */
4255 if (!after)
4257 /* We are being called before a change.
4258 Scan the overlays to find the functions to call. */
4259 last_overlay_modification_hooks_used = 0;
4260 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4262 int startpos, endpos;
4263 Lisp_Object ostart, oend;
4265 XSETMISC (overlay, tail);
4267 ostart = OVERLAY_START (overlay);
4268 oend = OVERLAY_END (overlay);
4269 endpos = OVERLAY_POSITION (oend);
4270 if (XFASTINT (start) > endpos)
4271 break;
4272 startpos = OVERLAY_POSITION (ostart);
4273 if (insertion && (XFASTINT (start) == startpos
4274 || XFASTINT (end) == startpos))
4276 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4277 if (!NILP (prop))
4278 add_overlay_mod_hooklist (prop, overlay);
4280 if (insertion && (XFASTINT (start) == endpos
4281 || XFASTINT (end) == endpos))
4283 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4284 if (!NILP (prop))
4285 add_overlay_mod_hooklist (prop, overlay);
4287 /* Test for intersecting intervals. This does the right thing
4288 for both insertion and deletion. */
4289 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4291 prop = Foverlay_get (overlay, Qmodification_hooks);
4292 if (!NILP (prop))
4293 add_overlay_mod_hooklist (prop, overlay);
4297 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4299 int startpos, endpos;
4300 Lisp_Object ostart, oend;
4302 XSETMISC (overlay, tail);
4304 ostart = OVERLAY_START (overlay);
4305 oend = OVERLAY_END (overlay);
4306 startpos = OVERLAY_POSITION (ostart);
4307 endpos = OVERLAY_POSITION (oend);
4308 if (XFASTINT (end) < startpos)
4309 break;
4310 if (insertion && (XFASTINT (start) == startpos
4311 || XFASTINT (end) == startpos))
4313 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4314 if (!NILP (prop))
4315 add_overlay_mod_hooklist (prop, overlay);
4317 if (insertion && (XFASTINT (start) == endpos
4318 || XFASTINT (end) == endpos))
4320 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4321 if (!NILP (prop))
4322 add_overlay_mod_hooklist (prop, overlay);
4324 /* Test for intersecting intervals. This does the right thing
4325 for both insertion and deletion. */
4326 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4328 prop = Foverlay_get (overlay, Qmodification_hooks);
4329 if (!NILP (prop))
4330 add_overlay_mod_hooklist (prop, overlay);
4335 GCPRO4 (overlay, arg1, arg2, arg3);
4337 /* Call the functions recorded in last_overlay_modification_hooks.
4338 First copy the vector contents, in case some of these hooks
4339 do subsequent modification of the buffer. */
4340 int size = last_overlay_modification_hooks_used;
4341 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4342 int i;
4344 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4345 copy, size * sizeof (Lisp_Object));
4346 gcpro1.var = copy;
4347 gcpro1.nvars = size;
4349 for (i = 0; i < size;)
4351 Lisp_Object prop, overlay;
4352 prop = copy[i++];
4353 overlay = copy[i++];
4354 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4357 UNGCPRO;
4360 static void
4361 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4362 Lisp_Object list, overlay;
4363 int after;
4364 Lisp_Object arg1, arg2, arg3;
4366 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4368 GCPRO4 (list, arg1, arg2, arg3);
4370 while (CONSP (list))
4372 if (NILP (arg3))
4373 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4374 else
4375 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4376 list = XCDR (list);
4378 UNGCPRO;
4381 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4382 property is set. */
4383 void
4384 evaporate_overlays (pos)
4385 EMACS_INT pos;
4387 Lisp_Object overlay, hit_list;
4388 struct Lisp_Overlay *tail;
4390 hit_list = Qnil;
4391 if (pos <= current_buffer->overlay_center)
4392 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4394 int endpos;
4395 XSETMISC (overlay, tail);
4396 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4397 if (endpos < pos)
4398 break;
4399 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4400 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4401 hit_list = Fcons (overlay, hit_list);
4403 else
4404 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4406 int startpos;
4407 XSETMISC (overlay, tail);
4408 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4409 if (startpos > pos)
4410 break;
4411 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4412 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4413 hit_list = Fcons (overlay, hit_list);
4415 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4416 Fdelete_overlay (XCAR (hit_list));
4419 /* Somebody has tried to store a value with an unacceptable type
4420 in the slot with offset OFFSET. */
4422 void
4423 buffer_slot_type_mismatch (sym, type)
4424 Lisp_Object sym;
4425 int type;
4427 char *type_name;
4429 switch (type)
4431 case Lisp_Int:
4432 type_name = "integers";
4433 break;
4435 case Lisp_String:
4436 type_name = "strings";
4437 break;
4439 case Lisp_Symbol:
4440 type_name = "symbols";
4441 break;
4443 default:
4444 abort ();
4447 error ("Only %s should be stored in the buffer-local variable %s",
4448 type_name, SDATA (SYMBOL_NAME (sym)));
4452 /***********************************************************************
4453 Allocation with mmap
4454 ***********************************************************************/
4456 #ifdef USE_MMAP_FOR_BUFFERS
4458 #include <sys/types.h>
4459 #include <sys/mman.h>
4461 #ifndef MAP_ANON
4462 #ifdef MAP_ANONYMOUS
4463 #define MAP_ANON MAP_ANONYMOUS
4464 #else
4465 #define MAP_ANON 0
4466 #endif
4467 #endif
4469 #ifndef MAP_FAILED
4470 #define MAP_FAILED ((void *) -1)
4471 #endif
4473 #include <stdio.h>
4474 #include <errno.h>
4476 #if MAP_ANON == 0
4477 #include <fcntl.h>
4478 #endif
4480 #include "coding.h"
4483 /* Memory is allocated in regions which are mapped using mmap(2).
4484 The current implementation lets the system select mapped
4485 addresses; we're not using MAP_FIXED in general, except when
4486 trying to enlarge regions.
4488 Each mapped region starts with a mmap_region structure, the user
4489 area starts after that structure, aligned to MEM_ALIGN.
4491 +-----------------------+
4492 | struct mmap_info + |
4493 | padding |
4494 +-----------------------+
4495 | user data |
4498 +-----------------------+ */
4500 struct mmap_region
4502 /* User-specified size. */
4503 size_t nbytes_specified;
4505 /* Number of bytes mapped */
4506 size_t nbytes_mapped;
4508 /* Pointer to the location holding the address of the memory
4509 allocated with the mmap'd block. The variable actually points
4510 after this structure. */
4511 POINTER_TYPE **var;
4513 /* Next and previous in list of all mmap'd regions. */
4514 struct mmap_region *next, *prev;
4517 /* Doubly-linked list of mmap'd regions. */
4519 static struct mmap_region *mmap_regions;
4521 /* File descriptor for mmap. If we don't have anonymous mapping,
4522 /dev/zero will be opened on it. */
4524 static int mmap_fd;
4526 /* Temporary storage for mmap_set_vars, see there. */
4528 static struct mmap_region *mmap_regions_1;
4529 static int mmap_fd_1;
4531 /* Page size on this system. */
4533 static int mmap_page_size;
4535 /* 1 means mmap has been intialized. */
4537 static int mmap_initialized_p;
4539 /* Value is X rounded up to the next multiple of N. */
4541 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4543 /* Size of mmap_region structure plus padding. */
4545 #define MMAP_REGION_STRUCT_SIZE \
4546 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4548 /* Given a pointer P to the start of the user-visible part of a mapped
4549 region, return a pointer to the start of the region. */
4551 #define MMAP_REGION(P) \
4552 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4554 /* Given a pointer P to the start of a mapped region, return a pointer
4555 to the start of the user-visible part of the region. */
4557 #define MMAP_USER_AREA(P) \
4558 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4560 #define MEM_ALIGN sizeof (double)
4562 /* Predicate returning true if part of the address range [START .. END]
4563 is currently mapped. Used to prevent overwriting an existing
4564 memory mapping.
4566 Default is to conservativly assume the address range is occupied by
4567 something else. This can be overridden by system configuration
4568 files if system-specific means to determine this exists. */
4570 #ifndef MMAP_ALLOCATED_P
4571 #define MMAP_ALLOCATED_P(start, end) 1
4572 #endif
4574 /* Function prototypes. */
4576 static int mmap_free_1 P_ ((struct mmap_region *));
4577 static int mmap_enlarge P_ ((struct mmap_region *, int));
4578 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4579 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4580 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4581 static void mmap_free P_ ((POINTER_TYPE **ptr));
4582 static void mmap_init P_ ((void));
4585 /* Return a region overlapping address range START...END, or null if
4586 none. END is not including, i.e. the last byte in the range
4587 is at END - 1. */
4589 static struct mmap_region *
4590 mmap_find (start, end)
4591 POINTER_TYPE *start, *end;
4593 struct mmap_region *r;
4594 char *s = (char *) start, *e = (char *) end;
4596 for (r = mmap_regions; r; r = r->next)
4598 char *rstart = (char *) r;
4599 char *rend = rstart + r->nbytes_mapped;
4601 if (/* First byte of range, i.e. START, in this region? */
4602 (s >= rstart && s < rend)
4603 /* Last byte of range, i.e. END - 1, in this region? */
4604 || (e > rstart && e <= rend)
4605 /* First byte of this region in the range? */
4606 || (rstart >= s && rstart < e)
4607 /* Last byte of this region in the range? */
4608 || (rend > s && rend <= e))
4609 break;
4612 return r;
4616 /* Unmap a region. P is a pointer to the start of the user-araa of
4617 the region. Value is non-zero if successful. */
4619 static int
4620 mmap_free_1 (r)
4621 struct mmap_region *r;
4623 if (r->next)
4624 r->next->prev = r->prev;
4625 if (r->prev)
4626 r->prev->next = r->next;
4627 else
4628 mmap_regions = r->next;
4630 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4632 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4633 return 0;
4636 return 1;
4640 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4641 Value is non-zero if successful. */
4643 static int
4644 mmap_enlarge (r, npages)
4645 struct mmap_region *r;
4646 int npages;
4648 char *region_end = (char *) r + r->nbytes_mapped;
4649 size_t nbytes;
4650 int success = 0;
4652 if (npages < 0)
4654 /* Unmap pages at the end of the region. */
4655 nbytes = - npages * mmap_page_size;
4656 if (munmap (region_end - nbytes, nbytes) == -1)
4657 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4658 else
4660 r->nbytes_mapped -= nbytes;
4661 success = 1;
4664 else if (npages > 0)
4666 nbytes = npages * mmap_page_size;
4668 /* Try to map additional pages at the end of the region. We
4669 cannot do this if the address range is already occupied by
4670 something else because mmap deletes any previous mapping.
4671 I'm not sure this is worth doing, let's see. */
4672 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4674 POINTER_TYPE *p;
4676 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4677 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4678 if (p == MAP_FAILED)
4679 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4680 else if (p != (POINTER_TYPE *) region_end)
4682 /* Kernels are free to choose a different address. In
4683 that case, unmap what we've mapped above; we have
4684 no use for it. */
4685 if (munmap (p, nbytes) == -1)
4686 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4688 else
4690 r->nbytes_mapped += nbytes;
4691 success = 1;
4696 return success;
4700 /* Set or reset variables holding references to mapped regions. If
4701 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4702 non-zero, set all variables to the start of the user-areas
4703 of mapped regions.
4705 This function is called from Fdump_emacs to ensure that the dumped
4706 Emacs doesn't contain references to memory that won't be mapped
4707 when Emacs starts. */
4709 void
4710 mmap_set_vars (restore_p)
4711 int restore_p;
4713 struct mmap_region *r;
4715 if (restore_p)
4717 mmap_regions = mmap_regions_1;
4718 mmap_fd = mmap_fd_1;
4719 for (r = mmap_regions; r; r = r->next)
4720 *r->var = MMAP_USER_AREA (r);
4722 else
4724 for (r = mmap_regions; r; r = r->next)
4725 *r->var = NULL;
4726 mmap_regions_1 = mmap_regions;
4727 mmap_regions = NULL;
4728 mmap_fd_1 = mmap_fd;
4729 mmap_fd = -1;
4734 /* Allocate a block of storage large enough to hold NBYTES bytes of
4735 data. A pointer to the data is returned in *VAR. VAR is thus the
4736 address of some variable which will use the data area.
4738 The allocation of 0 bytes is valid.
4740 If we can't allocate the necessary memory, set *VAR to null, and
4741 return null. */
4743 static POINTER_TYPE *
4744 mmap_alloc (var, nbytes)
4745 POINTER_TYPE **var;
4746 size_t nbytes;
4748 void *p;
4749 size_t map;
4751 mmap_init ();
4753 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4754 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4755 mmap_fd, 0);
4757 if (p == MAP_FAILED)
4759 if (errno != ENOMEM)
4760 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4761 p = NULL;
4763 else
4765 struct mmap_region *r = (struct mmap_region *) p;
4767 r->nbytes_specified = nbytes;
4768 r->nbytes_mapped = map;
4769 r->var = var;
4770 r->prev = NULL;
4771 r->next = mmap_regions;
4772 if (r->next)
4773 r->next->prev = r;
4774 mmap_regions = r;
4776 p = MMAP_USER_AREA (p);
4779 return *var = p;
4783 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4784 resize it to size NBYTES. Change *VAR to reflect the new block,
4785 and return this value. If more memory cannot be allocated, then
4786 leave *VAR unchanged, and return null. */
4788 static POINTER_TYPE *
4789 mmap_realloc (var, nbytes)
4790 POINTER_TYPE **var;
4791 size_t nbytes;
4793 POINTER_TYPE *result;
4795 mmap_init ();
4797 if (*var == NULL)
4798 result = mmap_alloc (var, nbytes);
4799 else if (nbytes == 0)
4801 mmap_free (var);
4802 result = mmap_alloc (var, nbytes);
4804 else
4806 struct mmap_region *r = MMAP_REGION (*var);
4807 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4809 if (room < nbytes)
4811 /* Must enlarge. */
4812 POINTER_TYPE *old_ptr = *var;
4814 /* Try to map additional pages at the end of the region.
4815 If that fails, allocate a new region, copy data
4816 from the old region, then free it. */
4817 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4818 / mmap_page_size)))
4820 r->nbytes_specified = nbytes;
4821 *var = result = old_ptr;
4823 else if (mmap_alloc (var, nbytes))
4825 bcopy (old_ptr, *var, r->nbytes_specified);
4826 mmap_free_1 (MMAP_REGION (old_ptr));
4827 result = *var;
4828 r = MMAP_REGION (result);
4829 r->nbytes_specified = nbytes;
4831 else
4833 *var = old_ptr;
4834 result = NULL;
4837 else if (room - nbytes >= mmap_page_size)
4839 /* Shrinking by at least a page. Let's give some
4840 memory back to the system.
4842 The extra parens are to make the division happens first,
4843 on positive values, so we know it will round towards
4844 zero. */
4845 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4846 result = *var;
4847 r->nbytes_specified = nbytes;
4849 else
4851 /* Leave it alone. */
4852 result = *var;
4853 r->nbytes_specified = nbytes;
4857 return result;
4861 /* Free a block of relocatable storage whose data is pointed to by
4862 PTR. Store 0 in *PTR to show there's no block allocated. */
4864 static void
4865 mmap_free (var)
4866 POINTER_TYPE **var;
4868 mmap_init ();
4870 if (*var)
4872 mmap_free_1 (MMAP_REGION (*var));
4873 *var = NULL;
4878 /* Perform necessary intializations for the use of mmap. */
4880 static void
4881 mmap_init ()
4883 #if MAP_ANON == 0
4884 /* The value of mmap_fd is initially 0 in temacs, and -1
4885 in a dumped Emacs. */
4886 if (mmap_fd <= 0)
4888 /* No anonymous mmap -- we need the file descriptor. */
4889 mmap_fd = open ("/dev/zero", O_RDONLY);
4890 if (mmap_fd == -1)
4891 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4893 #endif /* MAP_ANON == 0 */
4895 if (mmap_initialized_p)
4896 return;
4897 mmap_initialized_p = 1;
4899 #if MAP_ANON != 0
4900 mmap_fd = -1;
4901 #endif
4903 mmap_page_size = getpagesize ();
4906 #endif /* USE_MMAP_FOR_BUFFERS */
4910 /***********************************************************************
4911 Buffer-text Allocation
4912 ***********************************************************************/
4914 #ifdef REL_ALLOC
4915 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4916 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4917 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4918 #endif /* REL_ALLOC */
4921 /* Allocate NBYTES bytes for buffer B's text buffer. */
4923 static void
4924 alloc_buffer_text (b, nbytes)
4925 struct buffer *b;
4926 size_t nbytes;
4928 POINTER_TYPE *p;
4930 BLOCK_INPUT;
4931 #if defined USE_MMAP_FOR_BUFFERS
4932 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4933 #elif defined REL_ALLOC
4934 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4935 #else
4936 p = xmalloc (nbytes);
4937 #endif
4939 if (p == NULL)
4941 UNBLOCK_INPUT;
4942 memory_full ();
4945 b->text->beg = (unsigned char *) p;
4946 UNBLOCK_INPUT;
4949 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4950 shrink it. */
4952 void
4953 enlarge_buffer_text (b, delta)
4954 struct buffer *b;
4955 int delta;
4957 POINTER_TYPE *p;
4958 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4959 + delta);
4960 BLOCK_INPUT;
4961 #if defined USE_MMAP_FOR_BUFFERS
4962 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4963 #elif defined REL_ALLOC
4964 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4965 #else
4966 p = xrealloc (b->text->beg, nbytes);
4967 #endif
4969 if (p == NULL)
4971 UNBLOCK_INPUT;
4972 memory_full ();
4975 BUF_BEG_ADDR (b) = (unsigned char *) p;
4976 UNBLOCK_INPUT;
4980 /* Free buffer B's text buffer. */
4982 static void
4983 free_buffer_text (b)
4984 struct buffer *b;
4986 BLOCK_INPUT;
4988 #if defined USE_MMAP_FOR_BUFFERS
4989 mmap_free ((POINTER_TYPE **) &b->text->beg);
4990 #elif defined REL_ALLOC
4991 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4992 #else
4993 xfree (b->text->beg);
4994 #endif
4996 BUF_BEG_ADDR (b) = NULL;
4997 UNBLOCK_INPUT;
5002 /***********************************************************************
5003 Initialization
5004 ***********************************************************************/
5006 void
5007 init_buffer_once ()
5009 int idx;
5011 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5013 /* Make sure all markable slots in buffer_defaults
5014 are initialized reasonably, so mark_buffer won't choke. */
5015 reset_buffer (&buffer_defaults);
5016 reset_buffer_local_variables (&buffer_defaults, 1);
5017 reset_buffer (&buffer_local_symbols);
5018 reset_buffer_local_variables (&buffer_local_symbols, 1);
5019 /* Prevent GC from getting confused. */
5020 buffer_defaults.text = &buffer_defaults.own_text;
5021 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5022 BUF_INTERVALS (&buffer_defaults) = 0;
5023 BUF_INTERVALS (&buffer_local_symbols) = 0;
5024 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5025 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5026 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5027 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5029 /* Set up the default values of various buffer slots. */
5030 /* Must do these before making the first buffer! */
5032 /* real setup is done in bindings.el */
5033 buffer_defaults.mode_line_format = build_string ("%-");
5034 buffer_defaults.header_line_format = Qnil;
5035 buffer_defaults.abbrev_mode = Qnil;
5036 buffer_defaults.overwrite_mode = Qnil;
5037 buffer_defaults.case_fold_search = Qt;
5038 buffer_defaults.auto_fill_function = Qnil;
5039 buffer_defaults.selective_display = Qnil;
5040 #ifndef old
5041 buffer_defaults.selective_display_ellipses = Qt;
5042 #endif
5043 buffer_defaults.abbrev_table = Qnil;
5044 buffer_defaults.display_table = Qnil;
5045 buffer_defaults.undo_list = Qnil;
5046 buffer_defaults.mark_active = Qnil;
5047 buffer_defaults.file_format = Qnil;
5048 buffer_defaults.auto_save_file_format = Qt;
5049 buffer_defaults.overlays_before = NULL;
5050 buffer_defaults.overlays_after = NULL;
5051 buffer_defaults.overlay_center = BEG;
5053 XSETFASTINT (buffer_defaults.tab_width, 8);
5054 buffer_defaults.truncate_lines = Qnil;
5055 buffer_defaults.ctl_arrow = Qt;
5056 buffer_defaults.direction_reversed = Qnil;
5057 buffer_defaults.cursor_type = Qt;
5058 buffer_defaults.extra_line_spacing = Qnil;
5059 buffer_defaults.cursor_in_non_selected_windows = Qt;
5061 #ifdef DOS_NT
5062 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5063 #endif
5064 buffer_defaults.enable_multibyte_characters = Qt;
5065 buffer_defaults.buffer_file_coding_system = Qnil;
5066 XSETFASTINT (buffer_defaults.fill_column, 70);
5067 XSETFASTINT (buffer_defaults.left_margin, 0);
5068 buffer_defaults.cache_long_line_scans = Qnil;
5069 buffer_defaults.file_truename = Qnil;
5070 XSETFASTINT (buffer_defaults.display_count, 0);
5071 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5072 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5073 buffer_defaults.left_fringe_width = Qnil;
5074 buffer_defaults.right_fringe_width = Qnil;
5075 buffer_defaults.fringes_outside_margins = Qnil;
5076 buffer_defaults.scroll_bar_width = Qnil;
5077 buffer_defaults.vertical_scroll_bar_type = Qt;
5078 buffer_defaults.indicate_empty_lines = Qnil;
5079 buffer_defaults.indicate_buffer_boundaries = Qnil;
5080 buffer_defaults.fringe_indicator_alist = Qnil;
5081 buffer_defaults.fringe_cursor_alist = Qnil;
5082 buffer_defaults.scroll_up_aggressively = Qnil;
5083 buffer_defaults.scroll_down_aggressively = Qnil;
5084 buffer_defaults.display_time = Qnil;
5086 /* Assign the local-flags to the slots that have default values.
5087 The local flag is a bit that is used in the buffer
5088 to say that it has its own local value for the slot.
5089 The local flag bits are in the local_var_flags slot of the buffer. */
5091 /* Nothing can work if this isn't true */
5092 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5094 /* 0 means not a lisp var, -1 means always local, else mask */
5095 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5096 XSETINT (buffer_local_flags.filename, -1);
5097 XSETINT (buffer_local_flags.directory, -1);
5098 XSETINT (buffer_local_flags.backed_up, -1);
5099 XSETINT (buffer_local_flags.save_length, -1);
5100 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5101 XSETINT (buffer_local_flags.read_only, -1);
5102 XSETINT (buffer_local_flags.major_mode, -1);
5103 XSETINT (buffer_local_flags.mode_name, -1);
5104 XSETINT (buffer_local_flags.undo_list, -1);
5105 XSETINT (buffer_local_flags.mark_active, -1);
5106 XSETINT (buffer_local_flags.point_before_scroll, -1);
5107 XSETINT (buffer_local_flags.file_truename, -1);
5108 XSETINT (buffer_local_flags.invisibility_spec, -1);
5109 XSETINT (buffer_local_flags.file_format, -1);
5110 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5111 XSETINT (buffer_local_flags.display_count, -1);
5112 XSETINT (buffer_local_flags.display_time, -1);
5113 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5115 idx = 1;
5116 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5117 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5118 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5119 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5120 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5121 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5122 #ifndef old
5123 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5124 #endif
5125 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5126 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5127 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5128 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5129 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5131 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5132 #ifdef DOS_NT
5133 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5134 /* Make this one a permanent local. */
5135 buffer_permanent_local_flags[idx++] = 1;
5136 #endif
5137 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5138 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5139 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5140 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5141 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5142 /* Make this one a permanent local. */
5143 buffer_permanent_local_flags[idx++] = 1;
5144 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5149 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5150 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5151 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5152 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5154 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5160 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5162 /* Need more room? */
5163 if (idx >= MAX_PER_BUFFER_VARS)
5164 abort ();
5165 last_per_buffer_idx = idx;
5167 Vbuffer_alist = Qnil;
5168 current_buffer = 0;
5169 all_buffers = 0;
5171 QSFundamental = build_string ("Fundamental");
5173 Qfundamental_mode = intern ("fundamental-mode");
5174 buffer_defaults.major_mode = Qfundamental_mode;
5176 Qmode_class = intern ("mode-class");
5178 Qprotected_field = intern ("protected-field");
5180 Qpermanent_local = intern ("permanent-local");
5182 Qkill_buffer_hook = intern ("kill-buffer-hook");
5183 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5185 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5187 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5189 /* super-magic invisible buffer */
5190 Vbuffer_alist = Qnil;
5192 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5194 inhibit_modification_hooks = 0;
5197 void
5198 init_buffer ()
5200 char *pwd;
5201 Lisp_Object temp;
5202 int len;
5204 #ifdef USE_MMAP_FOR_BUFFERS
5206 /* When using the ralloc implementation based on mmap(2), buffer
5207 text pointers will have been set to null in the dumped Emacs.
5208 Map new memory. */
5209 struct buffer *b;
5211 for (b = all_buffers; b; b = b->next)
5212 if (b->text->beg == NULL)
5213 enlarge_buffer_text (b, 0);
5215 #endif /* USE_MMAP_FOR_BUFFERS */
5217 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5218 if (NILP (buffer_defaults.enable_multibyte_characters))
5219 Fset_buffer_multibyte (Qnil);
5221 pwd = get_current_dir_name ();
5223 if (!pwd)
5224 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5226 #ifndef VMS
5227 /* Maybe this should really use some standard subroutine
5228 whose definition is filename syntax dependent. */
5229 len = strlen (pwd);
5230 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5232 /* Grow buffer to add directory separator and '\0'. */
5233 pwd = (char *) xrealloc (pwd, len + 2);
5234 pwd[len] = DIRECTORY_SEP;
5235 pwd[len + 1] = '\0';
5237 #endif /* not VMS */
5239 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5240 if (! NILP (buffer_defaults.enable_multibyte_characters))
5241 /* At this moment, we still don't know how to decode the
5242 directory name. So, we keep the bytes in multibyte form so
5243 that ENCODE_FILE correctly gets the original bytes. */
5244 current_buffer->directory
5245 = string_to_multibyte (current_buffer->directory);
5247 /* Add /: to the front of the name
5248 if it would otherwise be treated as magic. */
5249 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5250 if (! NILP (temp)
5251 /* If the default dir is just /, TEMP is non-nil
5252 because of the ange-ftp completion handler.
5253 However, it is not necessary to turn / into /:/.
5254 So avoid doing that. */
5255 && strcmp ("/", SDATA (current_buffer->directory)))
5256 current_buffer->directory
5257 = concat2 (build_string ("/:"), current_buffer->directory);
5259 temp = get_minibuffer (0);
5260 XBUFFER (temp)->directory = current_buffer->directory;
5262 free (pwd);
5265 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5266 Object stored in the current buffer. address is the address of the slot
5267 in the buffer that is current now. */
5269 /* TYPE is nil for a general Lisp variable.
5270 An integer specifies a type; then only LIsp values
5271 with that type code are allowed (except that nil is allowed too).
5272 LNAME is the LIsp-level variable name.
5273 VNAME is the name of the buffer slot.
5274 DOC is a dummy where you write the doc string as a comment. */
5275 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5276 defvar_per_buffer (lname, vname, type, 0)
5278 static void
5279 defvar_per_buffer (namestring, address, type, doc)
5280 char *namestring;
5281 Lisp_Object *address;
5282 Lisp_Object type;
5283 char *doc;
5285 Lisp_Object sym, val;
5286 int offset;
5288 sym = intern (namestring);
5289 val = allocate_misc ();
5290 offset = (char *)address - (char *)current_buffer;
5292 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5293 XBUFFER_OBJFWD (val)->offset = offset;
5294 XBUFFER_OBJFWD (val)->slottype = type;
5295 SET_SYMBOL_VALUE (sym, val);
5296 PER_BUFFER_SYMBOL (offset) = sym;
5298 if (PER_BUFFER_IDX (offset) == 0)
5299 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5300 slot of buffer_local_flags */
5301 abort ();
5305 /* initialize the buffer routines */
5306 void
5307 syms_of_buffer ()
5309 staticpro (&last_overlay_modification_hooks);
5310 last_overlay_modification_hooks
5311 = Fmake_vector (make_number (10), Qnil);
5313 staticpro (&Vbuffer_defaults);
5314 staticpro (&Vbuffer_local_symbols);
5315 staticpro (&Qfundamental_mode);
5316 staticpro (&Qmode_class);
5317 staticpro (&QSFundamental);
5318 staticpro (&Vbuffer_alist);
5319 staticpro (&Qprotected_field);
5320 staticpro (&Qpermanent_local);
5321 Qpermanent_local_hook = intern ("permanent-local-hook");
5322 staticpro (&Qpermanent_local_hook);
5323 staticpro (&Qkill_buffer_hook);
5324 Qoverlayp = intern ("overlayp");
5325 staticpro (&Qoverlayp);
5326 Qevaporate = intern ("evaporate");
5327 staticpro (&Qevaporate);
5328 Qmodification_hooks = intern ("modification-hooks");
5329 staticpro (&Qmodification_hooks);
5330 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5331 staticpro (&Qinsert_in_front_hooks);
5332 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5333 staticpro (&Qinsert_behind_hooks);
5334 Qget_file_buffer = intern ("get-file-buffer");
5335 staticpro (&Qget_file_buffer);
5336 Qpriority = intern ("priority");
5337 staticpro (&Qpriority);
5338 Qwindow = intern ("window");
5339 staticpro (&Qwindow);
5340 Qbefore_string = intern ("before-string");
5341 staticpro (&Qbefore_string);
5342 Qafter_string = intern ("after-string");
5343 staticpro (&Qafter_string);
5344 Qfirst_change_hook = intern ("first-change-hook");
5345 staticpro (&Qfirst_change_hook);
5346 Qbefore_change_functions = intern ("before-change-functions");
5347 staticpro (&Qbefore_change_functions);
5348 Qafter_change_functions = intern ("after-change-functions");
5349 staticpro (&Qafter_change_functions);
5350 /* The next one is initialized in init_buffer_once. */
5351 staticpro (&Qucs_set_table_for_input);
5353 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5354 staticpro (&Qkill_buffer_query_functions);
5356 Fput (Qprotected_field, Qerror_conditions,
5357 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5358 Fput (Qprotected_field, Qerror_message,
5359 build_string ("Attempt to modify a protected field"));
5361 /* All these use DEFVAR_LISP_NOPRO because the slots in
5362 buffer_defaults will all be marked via Vbuffer_defaults. */
5364 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5365 &buffer_defaults.mode_line_format,
5366 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5367 This is the same as (default-value 'mode-line-format). */);
5369 DEFVAR_LISP_NOPRO ("default-header-line-format",
5370 &buffer_defaults.header_line_format,
5371 doc: /* Default value of `header-line-format' for buffers that don't override it.
5372 This is the same as (default-value 'header-line-format). */);
5374 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5375 doc: /* Default value of `cursor-type' for buffers that don't override it.
5376 This is the same as (default-value 'cursor-type). */);
5378 DEFVAR_LISP_NOPRO ("default-line-spacing",
5379 &buffer_defaults.extra_line_spacing,
5380 doc: /* Default value of `line-spacing' for buffers that don't override it.
5381 This is the same as (default-value 'line-spacing). */);
5383 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5384 &buffer_defaults.cursor_in_non_selected_windows,
5385 doc: /* Default value of `cursor-in-non-selected-windows'.
5386 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5388 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5389 &buffer_defaults.abbrev_mode,
5390 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5391 This is the same as (default-value 'abbrev-mode). */);
5393 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5394 &buffer_defaults.ctl_arrow,
5395 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5396 This is the same as (default-value 'ctl-arrow). */);
5398 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5399 &buffer_defaults.direction_reversed,
5400 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5401 This is the same as (default-value 'direction-reversed). */);
5403 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5404 &buffer_defaults.enable_multibyte_characters,
5405 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5406 This is the same as (default-value 'enable-multibyte-characters). */);
5408 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5409 &buffer_defaults.buffer_file_coding_system,
5410 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5411 This is the same as (default-value 'buffer-file-coding-system). */);
5413 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5414 &buffer_defaults.truncate_lines,
5415 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5416 This is the same as (default-value 'truncate-lines). */);
5418 DEFVAR_LISP_NOPRO ("default-fill-column",
5419 &buffer_defaults.fill_column,
5420 doc: /* Default value of `fill-column' for buffers that do not override it.
5421 This is the same as (default-value 'fill-column). */);
5423 DEFVAR_LISP_NOPRO ("default-left-margin",
5424 &buffer_defaults.left_margin,
5425 doc: /* Default value of `left-margin' for buffers that do not override it.
5426 This is the same as (default-value 'left-margin). */);
5428 DEFVAR_LISP_NOPRO ("default-tab-width",
5429 &buffer_defaults.tab_width,
5430 doc: /* Default value of `tab-width' for buffers that do not override it.
5431 This is the same as (default-value 'tab-width). */);
5433 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5434 &buffer_defaults.case_fold_search,
5435 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5436 This is the same as (default-value 'case-fold-search). */);
5438 #ifdef DOS_NT
5439 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5440 &buffer_defaults.buffer_file_type,
5441 doc: /* Default file type for buffers that do not override it.
5442 This is the same as (default-value 'buffer-file-type).
5443 The file type is nil for text, t for binary. */);
5444 #endif
5446 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5447 &buffer_defaults.left_margin_cols,
5448 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5449 This is the same as (default-value 'left-margin-width). */);
5451 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5452 &buffer_defaults.right_margin_cols,
5453 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5454 This is the same as (default-value 'right-margin-width). */);
5456 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5457 &buffer_defaults.left_fringe_width,
5458 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5459 This is the same as (default-value 'left-fringe-width). */);
5461 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5462 &buffer_defaults.right_fringe_width,
5463 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5464 This is the same as (default-value 'right-fringe-width). */);
5466 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5467 &buffer_defaults.fringes_outside_margins,
5468 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5469 This is the same as (default-value 'fringes-outside-margins). */);
5471 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5472 &buffer_defaults.scroll_bar_width,
5473 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5474 This is the same as (default-value 'scroll-bar-width). */);
5476 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5477 &buffer_defaults.vertical_scroll_bar_type,
5478 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5479 This is the same as (default-value 'vertical-scroll-bar). */);
5481 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5482 &buffer_defaults.indicate_empty_lines,
5483 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5484 This is the same as (default-value 'indicate-empty-lines). */);
5486 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5487 &buffer_defaults.indicate_buffer_boundaries,
5488 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5489 This is the same as (default-value 'indicate-buffer-boundaries). */);
5491 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5492 &buffer_defaults.fringe_indicator_alist,
5493 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5494 This is the same as (default-value 'fringe-indicator-alist'). */);
5496 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5497 &buffer_defaults.fringe_cursor_alist,
5498 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5499 This is the same as (default-value 'fringe-cursor-alist'). */);
5501 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5502 &buffer_defaults.scroll_up_aggressively,
5503 doc: /* Default value of `scroll-up-aggressively'.
5504 This value applies in buffers that don't have their own local values.
5505 This is the same as (default-value 'scroll-up-aggressively). */);
5507 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5508 &buffer_defaults.scroll_down_aggressively,
5509 doc: /* Default value of `scroll-down-aggressively'.
5510 This value applies in buffers that don't have their own local values.
5511 This is the same as (default-value 'scroll-down-aggressively). */);
5513 DEFVAR_PER_BUFFER ("header-line-format",
5514 &current_buffer->header_line_format,
5515 Qnil,
5516 doc: /* Analogous to `mode-line-format', but controls the header line.
5517 The header line appears, optionally, at the top of a window;
5518 the mode line appears at the bottom. */);
5520 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5521 Qnil,
5522 doc: /* Template for displaying mode line for current buffer.
5523 Each buffer has its own value of this variable.
5524 Value may be nil, a string, a symbol or a list or cons cell.
5525 A value of nil means don't display a mode line.
5526 For a symbol, its value is used (but it is ignored if t or nil).
5527 A string appearing directly as the value of a symbol is processed verbatim
5528 in that the %-constructs below are not recognized.
5529 Note that unless the symbol is marked as a `risky-local-variable', all
5530 properties in any strings, as well as all :eval and :propertize forms
5531 in the value of that symbol will be ignored.
5532 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5533 is used as a mode line element. Be careful--FORM should not load any files,
5534 because that can cause an infinite recursion.
5535 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5536 with the specified properties PROPS applied.
5537 For a list whose car is a symbol, the symbol's value is taken,
5538 and if that is non-nil, the cadr of the list is processed recursively.
5539 Otherwise, the caddr of the list (if there is one) is processed.
5540 For a list whose car is a string or list, each element is processed
5541 recursively and the results are effectively concatenated.
5542 For a list whose car is an integer, the cdr of the list is processed
5543 and padded (if the number is positive) or truncated (if negative)
5544 to the width specified by that number.
5545 A string is printed verbatim in the mode line except for %-constructs:
5546 (%-constructs are allowed when the string is the entire mode-line-format
5547 or when it is found in a cons-cell or a list)
5548 %b -- print buffer name. %f -- print visited file name.
5549 %F -- print frame name.
5550 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5551 %& is like %*, but ignore read-only-ness.
5552 % means buffer is read-only and * means it is modified.
5553 For a modified read-only buffer, %* gives % and %+ gives *.
5554 %s -- print process status. %l -- print the current line number.
5555 %c -- print the current column number (this makes editing slower).
5556 To make the column number update correctly in all cases,
5557 `column-number-mode' must be non-nil.
5558 %i -- print the size of the buffer.
5559 %I -- like %i, but use k, M, G, etc., to abbreviate.
5560 %p -- print percent of buffer above top of window, or Top, Bot or All.
5561 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5562 or print Bottom or All.
5563 %n -- print Narrow if appropriate.
5564 %t -- visited file is text or binary (if OS supports this distinction).
5565 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5566 %Z -- like %z, but including the end-of-line format.
5567 %e -- print error message about full memory.
5568 %@ -- print @ or hyphen. @ means that default-directory is on a
5569 remote machine.
5570 %[ -- print one [ for each recursive editing level. %] similar.
5571 %% -- print %. %- -- print infinitely many dashes.
5572 Decimal digits after the % specify field width to which to pad. */);
5574 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5575 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5576 A value of nil means use current buffer's major mode,
5577 provided it is not marked as "special".
5579 When a mode is used by default, `find-file' switches to it
5580 before it reads the contents into the buffer and before
5581 it finishes setting up the buffer. Thus, the mode and
5582 its hooks should not expect certain variables such as
5583 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5585 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5586 make_number (Lisp_Symbol),
5587 doc: /* Symbol for current buffer's major mode. */);
5589 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5590 Qnil,
5591 doc: /* Pretty name of current buffer's major mode.
5592 Usually a string. See `mode-line-format' for other possible forms. */);
5594 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5595 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5597 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5598 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5600 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5601 Qnil,
5602 doc: /* *Non-nil if searches and matches should ignore case. */);
5604 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5605 make_number (Lisp_Int),
5606 doc: /* *Column beyond which automatic line-wrapping should happen.
5607 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5609 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5610 make_number (Lisp_Int),
5611 doc: /* *Column for the default `indent-line-function' to indent to.
5612 Linefeed indents to this column in Fundamental mode. */);
5614 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5615 make_number (Lisp_Int),
5616 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5618 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5619 doc: /* *Non-nil means display control chars with uparrow.
5620 A value of nil means use backslash and octal digits.
5621 This variable does not apply to characters whose display is specified
5622 in the current display table (if there is one). */);
5624 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5625 &current_buffer->enable_multibyte_characters,
5626 Qnil,
5627 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5628 Otherwise they are regarded as unibyte. This affects the display,
5629 file I/O and the behavior of various editing commands.
5631 This variable is buffer-local but you cannot set it directly;
5632 use the function `set-buffer-multibyte' to change a buffer's representation.
5633 Changing its default value with `setq-default' is supported.
5634 See also variable `default-enable-multibyte-characters' and Info node
5635 `(elisp)Text Representations'. */);
5636 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5638 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5639 &current_buffer->buffer_file_coding_system, Qnil,
5640 doc: /* Coding system to be used for encoding the buffer contents on saving.
5641 This variable applies to saving the buffer, and also to `write-region'
5642 and other functions that use `write-region'.
5643 It does not apply to sending output to subprocesses, however.
5645 If this is nil, the buffer is saved without any code conversion
5646 unless some coding system is specified in `file-coding-system-alist'
5647 for the buffer file.
5649 If the text to be saved cannot be encoded as specified by this variable,
5650 an alternative encoding is selected by `select-safe-coding-system', which see.
5652 The variable `coding-system-for-write', if non-nil, overrides this variable.
5654 This variable is never applied to a way of decoding a file while reading it. */);
5656 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5657 Qnil,
5658 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5660 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5661 doc: /* *Non-nil means do not display continuation lines.
5662 Instead, give each line of text just one screen line.
5664 Note that this is overridden by the variable
5665 `truncate-partial-width-windows' if that variable is non-nil
5666 and this buffer is not full-frame width. */);
5668 #ifdef DOS_NT
5669 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5670 Qnil,
5671 doc: /* Non-nil if the visited file is a binary file.
5672 This variable is meaningful on MS-DOG and Windows NT.
5673 On those systems, it is automatically local in every buffer.
5674 On other systems, this variable is normally always nil. */);
5675 #endif
5677 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5678 make_number (Lisp_String),
5679 doc: /* Name of default directory of current buffer. Should end with slash.
5680 To interactively change the default directory, use command `cd'. */);
5682 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5683 Qnil,
5684 doc: /* Function called (if non-nil) to perform auto-fill.
5685 It is called after self-inserting any character specified in
5686 the `auto-fill-chars' table.
5687 NOTE: This variable is not a hook;
5688 its value may not be a list of functions. */);
5690 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5691 make_number (Lisp_String),
5692 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5694 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5695 make_number (Lisp_String),
5696 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5697 The truename of a file is calculated by `file-truename'
5698 and then abbreviated with `abbreviate-file-name'. */);
5700 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5701 &current_buffer->auto_save_file_name,
5702 make_number (Lisp_String),
5703 doc: /* Name of file for auto-saving current buffer.
5704 If it is nil, that means don't auto-save this buffer. */);
5706 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5707 doc: /* Non-nil if this buffer is read-only. */);
5709 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5710 doc: /* Non-nil if this buffer's file has been backed up.
5711 Backing up is done before the first time the file is saved. */);
5713 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5714 make_number (Lisp_Int),
5715 doc: /* Length of current buffer when last read in, saved or auto-saved.
5716 0 initially. */);
5718 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5719 Qnil,
5720 doc: /* Non-nil enables selective display.
5721 An integer N as value means display only lines
5722 that start with less than N columns of space.
5723 A value of t means that the character ^M makes itself and
5724 all the rest of the line invisible; also, when saving the buffer
5725 in a file, save the ^M as a newline. */);
5727 #ifndef old
5728 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5729 &current_buffer->selective_display_ellipses,
5730 Qnil,
5731 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5732 #endif
5734 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5735 doc: /* Non-nil if self-insertion should replace existing text.
5736 The value should be one of `overwrite-mode-textual',
5737 `overwrite-mode-binary', or nil.
5738 If it is `overwrite-mode-textual', self-insertion still
5739 inserts at the end of a line, and inserts when point is before a tab,
5740 until the tab is filled in.
5741 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5743 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5744 Qnil,
5745 doc: /* Display table that controls display of the contents of current buffer.
5747 If this variable is nil, the value of `standard-display-table' is used.
5748 Each window can have its own, overriding display table, see
5749 `set-window-display-table' and `window-display-table'.
5751 The display table is a char-table created with `make-display-table'.
5752 A char-table is an array indexed by character codes. Normal array
5753 primitives `aref' and `aset' can be used to access elements of a char-table.
5755 Each of the char-table elements control how to display the corresponding
5756 text character: the element at index C in the table says how to display
5757 the character whose code is C. Each element should be a vector of
5758 characters or nil. The value nil means display the character in the
5759 default fashion; otherwise, the characters from the vector are delivered
5760 to the screen instead of the original character.
5762 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5763 to display a capital Y instead of each X character.
5765 In addition, a char-table has six extra slots to control the display of:
5767 the end of a truncated screen line (extra-slot 0, a single character);
5768 the end of a continued line (extra-slot 1, a single character);
5769 the escape character used to display character codes in octal
5770 (extra-slot 2, a single character);
5771 the character used as an arrow for control characters (extra-slot 3,
5772 a single character);
5773 the decoration indicating the presence of invisible lines (extra-slot 4,
5774 a vector of characters);
5775 the character used to draw the border between side-by-side windows
5776 (extra-slot 5, a single character).
5778 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5780 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5781 Qnil,
5782 doc: /* *Width of left marginal area for display of a buffer.
5783 A value of nil means no marginal area. */);
5785 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5786 Qnil,
5787 doc: /* *Width of right marginal area for display of a buffer.
5788 A value of nil means no marginal area. */);
5790 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5791 Qnil,
5792 doc: /* *Width of this buffer's left fringe (in pixels).
5793 A value of 0 means no left fringe is shown in this buffer's window.
5794 A value of nil means to use the left fringe width from the window's frame. */);
5796 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5797 Qnil,
5798 doc: /* *Width of this buffer's right fringe (in pixels).
5799 A value of 0 means no right fringe is shown in this buffer's window.
5800 A value of nil means to use the right fringe width from the window's frame. */);
5802 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5803 Qnil,
5804 doc: /* *Non-nil means to display fringes outside display margins.
5805 A value of nil means to display fringes between margins and buffer text. */);
5807 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5808 Qnil,
5809 doc: /* *Width of this buffer's scroll bars in pixels.
5810 A value of nil means to use the scroll bar width from the window's frame. */);
5812 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5813 Qnil,
5814 doc: /* *Position of this buffer's vertical scroll bar.
5815 The value takes effect whenever you tell a window to display this buffer;
5816 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5818 A value of `left' or `right' means put the vertical scroll bar at that side
5819 of the window; a value of nil means don't show any vertical scroll bars.
5820 A value of t (the default) means do whatever the window's frame specifies. */);
5822 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5823 &current_buffer->indicate_empty_lines, Qnil,
5824 doc: /* *Visually indicate empty lines after the buffer end.
5825 If non-nil, a bitmap is displayed in the left fringe of a window on
5826 window-systems. */);
5828 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5829 &current_buffer->indicate_buffer_boundaries, Qnil,
5830 doc: /* *Visually indicate buffer boundaries and scrolling.
5831 If non-nil, the first and last line of the buffer are marked in the fringe
5832 of a window on window-systems with angle bitmaps, or if the window can be
5833 scrolled, the top and bottom line of the window are marked with up and down
5834 arrow bitmaps.
5836 If value is a symbol `left' or `right', both angle and arrow bitmaps
5837 are displayed in the left or right fringe, resp. Any other value
5838 that doesn't look like an alist means display the angle bitmaps in
5839 the left fringe but no arrows.
5841 You can exercise more precise control by using an alist as the
5842 value. Each alist element (INDICATOR . POSITION) specifies
5843 where to show one of the indicators. INDICATOR is one of `top',
5844 `bottom', `up', `down', or t, which specifies the default position,
5845 and POSITION is one of `left', `right', or nil, meaning do not show
5846 this indicator.
5848 For example, ((top . left) (t . right)) places the top angle bitmap in
5849 left fringe, the bottom angle bitmap in right fringe, and both arrow
5850 bitmaps in right fringe. To show just the angle bitmaps in the left
5851 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5853 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5854 &current_buffer->fringe_indicator_alist, Qnil,
5855 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5856 The value is an alist where each element (INDICATOR . BITMAPS)
5857 specifies the fringe bitmaps used to display a specific logical
5858 fringe indicator.
5860 INDICATOR specifies the logical indicator type which is one of the
5861 following symbols: `truncation' , `continuation', `overlay-arrow',
5862 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5864 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5865 the actual bitmap shown in the left or right fringe for the logical
5866 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5867 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5868 are used only for the `bottom' and `one-line' indicators when the last
5869 \(only) line in has no final newline. BITMAPS may also be a single
5870 symbol which is used in both left and right fringes. */);
5872 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5873 &current_buffer->fringe_cursor_alist, Qnil,
5874 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5875 The value is an alist where each element (CURSOR . BITMAP)
5876 specifies the fringe bitmaps used to display a specific logical
5877 cursor type in the fringe.
5879 CURSOR specifies the logical cursor type which is one of the following
5880 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5881 one is used to show a hollow cursor on narrow lines display lines
5882 where the normal hollow cursor will not fit.
5884 BITMAP is the corresponding fringe bitmap shown for the logical
5885 cursor type. */);
5887 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5888 &current_buffer->scroll_up_aggressively, Qnil,
5889 doc: /* How far to scroll windows upward.
5890 If you move point off the bottom, the window scrolls automatically.
5891 This variable controls how far it scrolls. The value nil, the default,
5892 means scroll to center point. A fraction means scroll to put point
5893 that fraction of the window's height from the bottom of the window.
5894 When the value is 0.0, point goes at the bottom line, which in the simple
5895 case that you moved off with C-f means scrolling just one line. 1.0 means
5896 point goes at the top, so that in that simple case, the window
5897 scrolls by a full window height. Meaningful values are
5898 between 0.0 and 1.0, inclusive. */);
5900 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5901 &current_buffer->scroll_down_aggressively, Qnil,
5902 doc: /* How far to scroll windows downward.
5903 If you move point off the top, the window scrolls automatically.
5904 This variable controls how far it scrolls. The value nil, the default,
5905 means scroll to center point. A fraction means scroll to put point
5906 that fraction of the window's height from the top of the window.
5907 When the value is 0.0, point goes at the top line, which in the simple
5908 case that you moved off with C-b means scrolling just one line. 1.0 means
5909 point goes at the bottom, so that in that simple case, the window
5910 scrolls by a full window height. Meaningful values are
5911 between 0.0 and 1.0, inclusive. */);
5913 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5914 "Don't ask.");
5917 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5918 doc: /* List of functions to call before each text change.
5919 Two arguments are passed to each function: the positions of
5920 the beginning and end of the range of old text to be changed.
5921 \(For an insertion, the beginning and end are at the same place.)
5922 No information is given about the length of the text after the change.
5924 Buffer changes made while executing the `before-change-functions'
5925 don't call any before-change or after-change functions.
5926 That's because these variables are temporarily set to nil.
5927 As a result, a hook function cannot straightforwardly alter the value of
5928 these variables. See the Emacs Lisp manual for a way of
5929 accomplishing an equivalent result by using other variables.
5931 If an unhandled error happens in running these functions,
5932 the variable's value remains nil. That prevents the error
5933 from happening repeatedly and making Emacs nonfunctional. */);
5934 Vbefore_change_functions = Qnil;
5936 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5937 doc: /* List of functions to call after each text change.
5938 Three arguments are passed to each function: the positions of
5939 the beginning and end of the range of changed text,
5940 and the length in bytes of the pre-change text replaced by that range.
5941 \(For an insertion, the pre-change length is zero;
5942 for a deletion, that length is the number of bytes deleted,
5943 and the post-change beginning and end are at the same place.)
5945 Buffer changes made while executing the `after-change-functions'
5946 don't call any before-change or after-change functions.
5947 That's because these variables are temporarily set to nil.
5948 As a result, a hook function cannot straightforwardly alter the value of
5949 these variables. See the Emacs Lisp manual for a way of
5950 accomplishing an equivalent result by using other variables.
5952 If an unhandled error happens in running these functions,
5953 the variable's value remains nil. That prevents the error
5954 from happening repeatedly and making Emacs nonfunctional. */);
5955 Vafter_change_functions = Qnil;
5957 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5958 doc: /* A list of functions to call before changing a buffer which is unmodified.
5959 The functions are run using the `run-hooks' function. */);
5960 Vfirst_change_hook = Qnil;
5962 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5963 doc: /* List of undo entries in current buffer.
5964 Recent changes come first; older changes follow newer.
5966 An entry (BEG . END) represents an insertion which begins at
5967 position BEG and ends at position END.
5969 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5970 from (abs POSITION). If POSITION is positive, point was at the front
5971 of the text being deleted; if negative, point was at the end.
5973 An entry (t HIGH . LOW) indicates that the buffer previously had
5974 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5975 of the visited file's modification time, as of that time. If the
5976 modification time of the most recent save is different, this entry is
5977 obsolete.
5979 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5980 was modified between BEG and END. PROPERTY is the property name,
5981 and VALUE is the old value.
5983 An entry (apply FUN-NAME . ARGS) means undo the change with
5984 \(apply FUN-NAME ARGS).
5986 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5987 in the active region. BEG and END is the range affected by this entry
5988 and DELTA is the number of bytes added or deleted in that range by
5989 this change.
5991 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5992 was adjusted in position by the offset DISTANCE (an integer).
5994 An entry of the form POSITION indicates that point was at the buffer
5995 location given by the integer. Undoing an entry of this form places
5996 point at POSITION.
5998 Entries with value `nil' mark undo boundaries. The undo command treats
5999 the changes between two undo boundaries as a single step to be undone.
6001 If the value of the variable is t, undo information is not recorded. */);
6003 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6004 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6006 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6007 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6009 Normally, the line-motion functions work by scanning the buffer for
6010 newlines. Columnar operations (like `move-to-column' and
6011 `compute-motion') also work by scanning the buffer, summing character
6012 widths as they go. This works well for ordinary text, but if the
6013 buffer's lines are very long (say, more than 500 characters), these
6014 motion functions will take longer to execute. Emacs may also take
6015 longer to update the display.
6017 If `cache-long-line-scans' is non-nil, these motion functions cache the
6018 results of their scans, and consult the cache to avoid rescanning
6019 regions of the buffer until the text is modified. The caches are most
6020 beneficial when they prevent the most searching---that is, when the
6021 buffer contains long lines and large regions of characters with the
6022 same, fixed screen width.
6024 When `cache-long-line-scans' is non-nil, processing short lines will
6025 become slightly slower (because of the overhead of consulting the
6026 cache), and the caches will use memory roughly proportional to the
6027 number of newlines and characters whose screen width varies.
6029 The caches require no explicit maintenance; their accuracy is
6030 maintained internally by the Emacs primitives. Enabling or disabling
6031 the cache should not affect the behavior of any of the motion
6032 functions; it should only affect their performance. */);
6034 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6035 doc: /* Value of point before the last series of scroll operations, or nil. */);
6037 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6038 doc: /* List of formats to use when saving this buffer.
6039 Formats are defined by `format-alist'. This variable is
6040 set when a file is visited. */);
6042 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6043 &current_buffer->auto_save_file_format, Qnil,
6044 doc: /* *Format in which to write auto-save files.
6045 Should be a list of symbols naming formats that are defined in `format-alist'.
6046 If it is t, which is the default, auto-save files are written in the
6047 same format as a regular save would use. */);
6049 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6050 &current_buffer->invisibility_spec, Qnil,
6051 doc: /* Invisibility spec of this buffer.
6052 The default is t, which means that text is invisible
6053 if it has a non-nil `invisible' property.
6054 If the value is a list, a text character is invisible if its `invisible'
6055 property is an element in that list (or is a list with members in common).
6056 If an element is a cons cell of the form (PROP . ELLIPSIS),
6057 then characters with property value PROP are invisible,
6058 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6060 DEFVAR_PER_BUFFER ("buffer-display-count",
6061 &current_buffer->display_count, Qnil,
6062 doc: /* A number incremented each time this buffer is displayed in a window.
6063 The function `set-window-buffer' increments it. */);
6065 DEFVAR_PER_BUFFER ("buffer-display-time",
6066 &current_buffer->display_time, Qnil,
6067 doc: /* Time stamp updated each time this buffer is displayed in a window.
6068 The function `set-window-buffer' updates this variable
6069 to the value obtained by calling `current-time'.
6070 If the buffer has never been shown in a window, the value is nil. */);
6072 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6073 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
6074 Non-nil also enables highlighting of the region whenever the mark is active.
6075 The variable `highlight-nonselected-windows' controls whether to highlight
6076 all windows or just the selected window.
6078 If the value is `lambda', that enables Transient Mark mode temporarily
6079 until the next buffer modification. If a command sets the value to `only',
6080 that enables Transient Mark mode for the following command only.
6081 During that following command, the value of `transient-mark-mode'
6082 is `identity'. If it is still `identity' at the end of that command,
6083 it changes to nil. */);
6084 Vtransient_mark_mode = Qnil;
6086 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6087 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6088 If the value is t, disregard `buffer-read-only' and all `read-only'
6089 text properties. If the value is a list, disregard `buffer-read-only'
6090 and disregard a `read-only' text property if the property value
6091 is a member of the list. */);
6092 Vinhibit_read_only = Qnil;
6094 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6095 doc: /* Cursor to use when this buffer is in the selected window.
6096 Values are interpreted as follows:
6098 t use the cursor specified for the frame
6099 nil don't display a cursor
6100 box display a filled box cursor
6101 hollow display a hollow box cursor
6102 bar display a vertical bar cursor with default width
6103 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6104 hbar display a horizontal bar cursor with default height
6105 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6106 ANYTHING ELSE display a hollow box cursor
6108 When the buffer is displayed in a nonselected window,
6109 this variable has no effect; the cursor appears as a hollow box. */);
6111 DEFVAR_PER_BUFFER ("line-spacing",
6112 &current_buffer->extra_line_spacing, Qnil,
6113 doc: /* Additional space to put between lines when displaying a buffer.
6114 The space is measured in pixels, and put below lines on window systems.
6115 If value is a floating point number, it specifies the spacing relative
6116 to the default frame line height. A value of nil means add no extra space. */);
6118 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6119 &current_buffer->cursor_in_non_selected_windows, Qnil,
6120 doc: /* *Cursor type to display in non-selected windows.
6121 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6123 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6124 doc: /* List of functions called with no args to query before killing a buffer.
6125 The buffer being killed will be current while the functions are running.
6126 If any of them returns nil, the buffer is not killed. */);
6127 Vkill_buffer_query_functions = Qnil;
6129 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6130 doc: /* Normal hook run before changing the major mode of a buffer.
6131 The function `kill-all-local-variables' runs this before doing anything else. */);
6132 Vchange_major_mode_hook = Qnil;
6133 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6134 staticpro (&Qchange_major_mode_hook);
6136 defsubr (&Sbuffer_live_p);
6137 defsubr (&Sbuffer_list);
6138 defsubr (&Sget_buffer);
6139 defsubr (&Sget_file_buffer);
6140 defsubr (&Sget_buffer_create);
6141 defsubr (&Smake_indirect_buffer);
6142 defsubr (&Sgenerate_new_buffer_name);
6143 defsubr (&Sbuffer_name);
6144 /*defsubr (&Sbuffer_number);*/
6145 defsubr (&Sbuffer_file_name);
6146 defsubr (&Sbuffer_base_buffer);
6147 defsubr (&Sbuffer_local_value);
6148 defsubr (&Sbuffer_local_variables);
6149 defsubr (&Sbuffer_modified_p);
6150 defsubr (&Sset_buffer_modified_p);
6151 defsubr (&Sbuffer_modified_tick);
6152 defsubr (&Sbuffer_chars_modified_tick);
6153 defsubr (&Srename_buffer);
6154 defsubr (&Sother_buffer);
6155 defsubr (&Sbuffer_enable_undo);
6156 defsubr (&Skill_buffer);
6157 defsubr (&Sset_buffer_major_mode);
6158 defsubr (&Sswitch_to_buffer);
6159 defsubr (&Spop_to_buffer);
6160 defsubr (&Scurrent_buffer);
6161 defsubr (&Sset_buffer);
6162 defsubr (&Sbarf_if_buffer_read_only);
6163 defsubr (&Sbury_buffer);
6164 defsubr (&Serase_buffer);
6165 defsubr (&Sset_buffer_multibyte);
6166 defsubr (&Skill_all_local_variables);
6168 defsubr (&Soverlayp);
6169 defsubr (&Smake_overlay);
6170 defsubr (&Sdelete_overlay);
6171 defsubr (&Smove_overlay);
6172 defsubr (&Soverlay_start);
6173 defsubr (&Soverlay_end);
6174 defsubr (&Soverlay_buffer);
6175 defsubr (&Soverlay_properties);
6176 defsubr (&Soverlays_at);
6177 defsubr (&Soverlays_in);
6178 defsubr (&Snext_overlay_change);
6179 defsubr (&Sprevious_overlay_change);
6180 defsubr (&Soverlay_recenter);
6181 defsubr (&Soverlay_lists);
6182 defsubr (&Soverlay_get);
6183 defsubr (&Soverlay_put);
6184 defsubr (&Srestore_buffer_modified_p);
6187 void
6188 keys_of_buffer ()
6190 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6191 initial_define_key (control_x_map, 'k', "kill-buffer");
6193 /* This must not be in syms_of_buffer, because Qdisabled is not
6194 initialized when that function gets called. */
6195 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6198 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6199 (do not change this comment) */