* window.el (display-buffer-function, special-display-p)
[emacs.git] / src / buffer.c
blobbafcb020e8d27c01cec23e255802670150d60200
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 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <errno.h>
28 #include <stdio.h>
30 #ifndef USE_CRT_DLL
31 extern int errno;
32 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include "lisp.h"
40 #include "intervals.h"
41 #include "window.h"
42 #include "commands.h"
43 #include "buffer.h"
44 #include "character.h"
45 #include "region-cache.h"
46 #include "indent.h"
47 #include "blockinput.h"
48 #include "keyboard.h"
49 #include "keymap.h"
50 #include "frame.h"
52 struct buffer *current_buffer; /* the current buffer */
54 /* First buffer in chain of all buffers (in reverse order of creation).
55 Threaded through ->next. */
57 struct buffer *all_buffers;
59 /* This structure holds the default values of the buffer-local variables
60 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
61 The default value occupies the same slot in this structure
62 as an individual buffer's value occupies in that buffer.
63 Setting the default value also goes through the alist of buffers
64 and stores into each buffer that does not say it has a local value. */
66 DECL_ALIGN (struct buffer, buffer_defaults);
68 /* A Lisp_Object pointer to the above, used for staticpro */
70 static Lisp_Object Vbuffer_defaults;
72 /* This structure marks which slots in a buffer have corresponding
73 default values in buffer_defaults.
74 Each such slot has a nonzero value in this structure.
75 The value has only one nonzero bit.
77 When a buffer has its own local value for a slot,
78 the entry for that slot (found in the same slot in this structure)
79 is turned on in the buffer's local_flags array.
81 If a slot in this structure is -1, then even though there may
82 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
83 and the corresponding slot in buffer_defaults is not used.
85 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
86 but there is a default value which is copied into each buffer.
88 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
89 zero, that is a bug */
91 struct buffer buffer_local_flags;
93 /* This structure holds the names of symbols whose values may be
94 buffer-local. It is indexed and accessed in the same way as the above. */
96 DECL_ALIGN (struct buffer, buffer_local_symbols);
98 /* A Lisp_Object pointer to the above, used for staticpro */
99 static Lisp_Object Vbuffer_local_symbols;
101 /* Flags indicating which built-in buffer-local variables
102 are permanent locals. */
103 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
105 /* Number of per-buffer variables used. */
107 int last_per_buffer_idx;
109 EXFUN (Fset_buffer, 1);
110 void set_buffer_internal P_ ((struct buffer *b));
111 void set_buffer_internal_1 P_ ((struct buffer *b));
112 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
113 int after, Lisp_Object arg1,
114 Lisp_Object arg2, Lisp_Object arg3));
115 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
116 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
118 /* Alist of all buffer names vs the buffers. */
119 /* This used to be a variable, but is no longer,
120 to prevent lossage due to user rplac'ing this alist or its elements. */
121 Lisp_Object Vbuffer_alist;
123 /* Functions to call before and after each text change. */
124 Lisp_Object Vbefore_change_functions;
125 Lisp_Object Vafter_change_functions;
127 Lisp_Object Vtransient_mark_mode;
129 /* t means ignore all read-only text properties.
130 A list means ignore such a property if its value is a member of the list.
131 Any non-nil value means ignore buffer-read-only. */
132 Lisp_Object Vinhibit_read_only;
134 /* List of functions to call that can query about killing a buffer.
135 If any of these functions returns nil, we don't kill it. */
136 Lisp_Object Vkill_buffer_query_functions;
137 Lisp_Object Qkill_buffer_query_functions;
139 /* Hook run before changing a major mode. */
140 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
142 /* List of functions to call before changing an unmodified buffer. */
143 Lisp_Object Vfirst_change_hook;
145 Lisp_Object Qfirst_change_hook;
146 Lisp_Object Qbefore_change_functions;
147 Lisp_Object Qafter_change_functions;
148 Lisp_Object Qucs_set_table_for_input;
150 /* If nonzero, all modification hooks are suppressed. */
151 int inhibit_modification_hooks;
153 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
154 Lisp_Object Qpermanent_local_hook;
156 Lisp_Object Qprotected_field;
158 Lisp_Object QSFundamental; /* A string "Fundamental" */
160 Lisp_Object Qkill_buffer_hook;
162 Lisp_Object Qget_file_buffer;
164 Lisp_Object Qoverlayp;
166 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
168 Lisp_Object Qmodification_hooks;
169 Lisp_Object Qinsert_in_front_hooks;
170 Lisp_Object Qinsert_behind_hooks;
172 static void alloc_buffer_text P_ ((struct buffer *, size_t));
173 static void free_buffer_text P_ ((struct buffer *b));
174 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
175 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
176 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
178 extern char * emacs_strerror P_ ((int));
180 /* For debugging; temporary. See set_buffer_internal. */
181 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
183 void
184 nsberror (spec)
185 Lisp_Object spec;
187 if (STRINGP (spec))
188 error ("No buffer named %s", SDATA (spec));
189 error ("Invalid buffer argument");
192 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
193 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
194 Value is nil if OBJECT is not a buffer or if it has been killed. */)
195 (object)
196 Lisp_Object object;
198 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
199 ? Qt : Qnil);
202 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
203 doc: /* Return a list of all existing live buffers.
204 If the optional arg FRAME is a frame, we return the buffer list
205 in the proper order for that frame: the buffers in FRAME's `buffer-list'
206 frame parameter come first, followed by the rest of the buffers. */)
207 (frame)
208 Lisp_Object frame;
210 Lisp_Object general;
211 general = Fmapcar (Qcdr, Vbuffer_alist);
213 if (FRAMEP (frame))
215 Lisp_Object framelist, prevlist, tail;
216 Lisp_Object args[3];
218 CHECK_FRAME (frame);
220 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
221 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
223 /* Remove from GENERAL any buffer that duplicates one in
224 FRAMELIST or PREVLIST. */
225 tail = framelist;
226 while (CONSP (tail))
228 general = Fdelq (XCAR (tail), general);
229 tail = XCDR (tail);
231 tail = prevlist;
232 while (CONSP (tail))
234 general = Fdelq (XCAR (tail), general);
235 tail = XCDR (tail);
238 args[0] = framelist;
239 args[1] = general;
240 args[2] = prevlist;
241 return Fnconc (3, args);
244 return general;
247 /* Like Fassoc, but use Fstring_equal to compare
248 (which ignores text properties),
249 and don't ever QUIT. */
251 static Lisp_Object
252 assoc_ignore_text_properties (key, list)
253 register Lisp_Object key;
254 Lisp_Object list;
256 register Lisp_Object tail;
257 for (tail = list; CONSP (tail); tail = XCDR (tail))
259 register Lisp_Object elt, tem;
260 elt = XCAR (tail);
261 tem = Fstring_equal (Fcar (elt), key);
262 if (!NILP (tem))
263 return elt;
265 return Qnil;
268 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
269 doc: /* Return the buffer named NAME (a string).
270 If there is no live buffer named NAME, return nil.
271 NAME may also be a buffer; if so, the value is that buffer. */)
272 (name)
273 register Lisp_Object name;
275 if (BUFFERP (name))
276 return name;
277 CHECK_STRING (name);
279 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
282 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
283 doc: /* Return the buffer visiting file FILENAME (a string).
284 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
285 If there is no such live buffer, return nil.
286 See also `find-buffer-visiting'. */)
287 (filename)
288 register Lisp_Object filename;
290 register Lisp_Object tail, buf, tem;
291 Lisp_Object handler;
293 CHECK_STRING (filename);
294 filename = Fexpand_file_name (filename, Qnil);
296 /* If the file name has special constructs in it,
297 call the corresponding file handler. */
298 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
299 if (!NILP (handler))
300 return call2 (handler, Qget_file_buffer, filename);
302 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
304 buf = Fcdr (XCAR (tail));
305 if (!BUFFERP (buf)) continue;
306 if (!STRINGP (XBUFFER (buf)->filename)) continue;
307 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
308 if (!NILP (tem))
309 return buf;
311 return Qnil;
314 Lisp_Object
315 get_truename_buffer (filename)
316 register Lisp_Object filename;
318 register Lisp_Object tail, buf, tem;
320 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
322 buf = Fcdr (XCAR (tail));
323 if (!BUFFERP (buf)) continue;
324 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
325 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
326 if (!NILP (tem))
327 return buf;
329 return Qnil;
332 /* Incremented for each buffer created, to assign the buffer number. */
333 int buffer_count;
335 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
336 doc: /* Return the buffer named NAME, or create such a buffer and return it.
337 A new buffer is created if there is no live buffer named NAME.
338 If NAME starts with a space, the new buffer does not keep undo information.
339 If NAME is a buffer instead of a string, then it is the value returned.
340 The value is never nil. */)
341 (name)
342 register Lisp_Object name;
344 register Lisp_Object buf;
345 register struct buffer *b;
347 buf = Fget_buffer (name);
348 if (!NILP (buf))
349 return buf;
351 if (SCHARS (name) == 0)
352 error ("Empty string for buffer name is not allowed");
354 b = allocate_buffer ();
356 /* An ordinary buffer uses its own struct buffer_text. */
357 b->text = &b->own_text;
358 b->base_buffer = 0;
360 BUF_GAP_SIZE (b) = 20;
361 BLOCK_INPUT;
362 /* We allocate extra 1-byte at the tail and keep it always '\0' for
363 anchoring a search. */
364 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
365 UNBLOCK_INPUT;
366 if (! BUF_BEG_ADDR (b))
367 buffer_memory_full ();
369 BUF_PT (b) = BEG;
370 BUF_GPT (b) = BEG;
371 BUF_BEGV (b) = BEG;
372 BUF_ZV (b) = BEG;
373 BUF_Z (b) = BEG;
374 BUF_PT_BYTE (b) = BEG_BYTE;
375 BUF_GPT_BYTE (b) = BEG_BYTE;
376 BUF_BEGV_BYTE (b) = BEG_BYTE;
377 BUF_ZV_BYTE (b) = BEG_BYTE;
378 BUF_Z_BYTE (b) = BEG_BYTE;
379 BUF_MODIFF (b) = 1;
380 BUF_CHARS_MODIFF (b) = 1;
381 BUF_OVERLAY_MODIFF (b) = 1;
382 BUF_SAVE_MODIFF (b) = 1;
383 BUF_INTERVALS (b) = 0;
384 BUF_UNCHANGED_MODIFIED (b) = 1;
385 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
386 BUF_END_UNCHANGED (b) = 0;
387 BUF_BEG_UNCHANGED (b) = 0;
388 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
390 b->newline_cache = 0;
391 b->width_run_cache = 0;
392 b->width_table = Qnil;
393 b->prevent_redisplay_optimizations_p = 1;
395 /* Put this on the chain of all buffers including killed ones. */
396 b->next = all_buffers;
397 all_buffers = b;
399 /* An ordinary buffer normally doesn't need markers
400 to handle BEGV and ZV. */
401 b->pt_marker = Qnil;
402 b->begv_marker = Qnil;
403 b->zv_marker = Qnil;
405 name = Fcopy_sequence (name);
406 STRING_SET_INTERVALS (name, NULL_INTERVAL);
407 b->name = name;
409 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
411 reset_buffer (b);
412 reset_buffer_local_variables (b, 1);
414 b->mark = Fmake_marker ();
415 BUF_MARKERS (b) = NULL;
416 b->name = name;
418 /* Put this in the alist of all live buffers. */
419 XSETBUFFER (buf, b);
420 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
422 /* An error in calling the function here (should someone redefine it)
423 can lead to infinite regress until you run out of stack. rms
424 says that's not worth protecting against. */
425 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
426 /* buf is on buffer-alist, so no gcpro. */
427 call1 (Qucs_set_table_for_input, buf);
429 return buf;
433 /* Return a list of overlays which is a copy of the overlay list
434 LIST, but for buffer B. */
436 static struct Lisp_Overlay *
437 copy_overlays (b, list)
438 struct buffer *b;
439 struct Lisp_Overlay *list;
441 Lisp_Object buffer;
442 struct Lisp_Overlay *result = NULL, *tail = NULL;
444 XSETBUFFER (buffer, b);
446 for (; list; list = list->next)
448 Lisp_Object overlay, start, end, old_overlay;
449 EMACS_INT charpos;
451 XSETMISC (old_overlay, list);
452 charpos = marker_position (OVERLAY_START (old_overlay));
453 start = Fmake_marker ();
454 Fset_marker (start, make_number (charpos), buffer);
455 XMARKER (start)->insertion_type
456 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
458 charpos = marker_position (OVERLAY_END (old_overlay));
459 end = Fmake_marker ();
460 Fset_marker (end, make_number (charpos), buffer);
461 XMARKER (end)->insertion_type
462 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
464 overlay = allocate_misc ();
465 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
466 OVERLAY_START (overlay) = start;
467 OVERLAY_END (overlay) = end;
468 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
469 XOVERLAY (overlay)->next = NULL;
471 if (tail)
472 tail = tail->next = XOVERLAY (overlay);
473 else
474 result = tail = XOVERLAY (overlay);
477 return result;
481 /* Clone per-buffer values of buffer FROM.
483 Buffer TO gets the same per-buffer values as FROM, with the
484 following exceptions: (1) TO's name is left untouched, (2) markers
485 are copied and made to refer to TO, and (3) overlay lists are
486 copied. */
488 static void
489 clone_per_buffer_values (from, to)
490 struct buffer *from, *to;
492 Lisp_Object to_buffer;
493 int offset;
495 XSETBUFFER (to_buffer, to);
497 /* buffer-local Lisp variables start at `undo_list',
498 tho only the ones from `name' on are GC'd normally. */
499 for (offset = PER_BUFFER_VAR_OFFSET (undo_list) + sizeof (Lisp_Object);
500 offset < sizeof *to;
501 offset += sizeof (Lisp_Object))
503 Lisp_Object obj;
505 obj = PER_BUFFER_VALUE (from, offset);
506 if (MARKERP (obj))
508 struct Lisp_Marker *m = XMARKER (obj);
509 obj = Fmake_marker ();
510 XMARKER (obj)->insertion_type = m->insertion_type;
511 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
514 PER_BUFFER_VALUE (to, offset) = obj;
517 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
519 to->overlays_before = copy_overlays (to, from->overlays_before);
520 to->overlays_after = copy_overlays (to, from->overlays_after);
522 /* Get (a copy of) the alist of Lisp-level local variables of FROM
523 and install that in TO. */
524 to->local_var_alist = buffer_lisp_local_variables (from);
527 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
528 2, 3,
529 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
530 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
531 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
532 NAME should be a string which is not the name of an existing buffer.
533 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
534 such as major and minor modes, in the indirect buffer.
535 CLONE nil means the indirect buffer's state is reset to default values. */)
536 (base_buffer, name, clone)
537 Lisp_Object base_buffer, name, clone;
539 Lisp_Object buf, tem;
540 struct buffer *b;
542 CHECK_STRING (name);
543 buf = Fget_buffer (name);
544 if (!NILP (buf))
545 error ("Buffer name `%s' is in use", SDATA (name));
547 tem = base_buffer;
548 base_buffer = Fget_buffer (base_buffer);
549 if (NILP (base_buffer))
550 error ("No such buffer: `%s'", SDATA (tem));
551 if (NILP (XBUFFER (base_buffer)->name))
552 error ("Base buffer has been killed");
554 if (SCHARS (name) == 0)
555 error ("Empty string for buffer name is not allowed");
557 b = allocate_buffer ();
559 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
560 ? XBUFFER (base_buffer)->base_buffer
561 : XBUFFER (base_buffer));
563 /* Use the base buffer's text object. */
564 b->text = b->base_buffer->text;
566 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
567 BUF_ZV (b) = BUF_ZV (b->base_buffer);
568 BUF_PT (b) = BUF_PT (b->base_buffer);
569 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
570 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
571 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
573 b->newline_cache = 0;
574 b->width_run_cache = 0;
575 b->width_table = Qnil;
577 /* Put this on the chain of all buffers including killed ones. */
578 b->next = all_buffers;
579 all_buffers = b;
581 name = Fcopy_sequence (name);
582 STRING_SET_INTERVALS (name, NULL_INTERVAL);
583 b->name = name;
585 reset_buffer (b);
586 reset_buffer_local_variables (b, 1);
588 /* Put this in the alist of all live buffers. */
589 XSETBUFFER (buf, b);
590 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
592 b->mark = Fmake_marker ();
593 b->name = name;
595 /* The multibyte status belongs to the base buffer. */
596 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
598 /* Make sure the base buffer has markers for its narrowing. */
599 if (NILP (b->base_buffer->pt_marker))
601 b->base_buffer->pt_marker = Fmake_marker ();
602 set_marker_both (b->base_buffer->pt_marker, base_buffer,
603 BUF_PT (b->base_buffer),
604 BUF_PT_BYTE (b->base_buffer));
606 if (NILP (b->base_buffer->begv_marker))
608 b->base_buffer->begv_marker = Fmake_marker ();
609 set_marker_both (b->base_buffer->begv_marker, base_buffer,
610 BUF_BEGV (b->base_buffer),
611 BUF_BEGV_BYTE (b->base_buffer));
613 if (NILP (b->base_buffer->zv_marker))
615 b->base_buffer->zv_marker = Fmake_marker ();
616 set_marker_both (b->base_buffer->zv_marker, base_buffer,
617 BUF_ZV (b->base_buffer),
618 BUF_ZV_BYTE (b->base_buffer));
619 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
622 if (NILP (clone))
624 /* Give the indirect buffer markers for its narrowing. */
625 b->pt_marker = Fmake_marker ();
626 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
627 b->begv_marker = Fmake_marker ();
628 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
629 b->zv_marker = Fmake_marker ();
630 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
631 XMARKER (b->zv_marker)->insertion_type = 1;
633 else
635 struct buffer *old_b = current_buffer;
637 clone_per_buffer_values (b->base_buffer, b);
638 b->filename = Qnil;
639 b->file_truename = Qnil;
640 b->display_count = make_number (0);
641 b->backed_up = Qnil;
642 b->auto_save_file_name = Qnil;
643 set_buffer_internal_1 (b);
644 Fset (intern ("buffer-save-without-query"), Qnil);
645 Fset (intern ("buffer-file-number"), Qnil);
646 Fset (intern ("buffer-stale-function"), Qnil);
647 set_buffer_internal_1 (old_b);
650 return buf;
653 void
654 delete_all_overlays (b)
655 struct buffer *b;
657 Lisp_Object overlay;
659 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
660 have to empty the list, otherwise we end up with overlays that
661 think they belong to this buffer while the buffer doesn't know about
662 them any more. */
663 while (b->overlays_before)
665 XSETMISC (overlay, b->overlays_before);
666 Fdelete_overlay (overlay);
668 while (b->overlays_after)
670 XSETMISC (overlay, b->overlays_after);
671 Fdelete_overlay (overlay);
673 eassert (b->overlays_before == NULL);
674 eassert (b->overlays_after == NULL);
677 /* Reinitialize everything about a buffer except its name and contents
678 and local variables.
679 If called on an already-initialized buffer, the list of overlays
680 should be deleted before calling this function, otherwise we end up
681 with overlays that claim to belong to the buffer but the buffer
682 claims it doesn't belong to it. */
684 void
685 reset_buffer (b)
686 register struct buffer *b;
688 b->filename = Qnil;
689 b->file_truename = Qnil;
690 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
691 b->modtime = 0;
692 XSETFASTINT (b->save_length, 0);
693 b->last_window_start = 1;
694 /* It is more conservative to start out "changed" than "unchanged". */
695 b->clip_changed = 0;
696 b->prevent_redisplay_optimizations_p = 1;
697 b->backed_up = Qnil;
698 b->auto_save_modified = 0;
699 b->auto_save_failure_time = -1;
700 b->auto_save_file_name = Qnil;
701 b->read_only = Qnil;
702 b->overlays_before = NULL;
703 b->overlays_after = NULL;
704 b->overlay_center = BEG;
705 b->mark_active = Qnil;
706 b->point_before_scroll = Qnil;
707 b->file_format = Qnil;
708 b->auto_save_file_format = Qt;
709 b->last_selected_window = Qnil;
710 XSETINT (b->display_count, 0);
711 b->display_time = Qnil;
712 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
713 b->cursor_type = buffer_defaults.cursor_type;
714 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
716 b->display_error_modiff = 0;
719 /* Reset buffer B's local variables info.
720 Don't use this on a buffer that has already been in use;
721 it does not treat permanent locals consistently.
722 Instead, use Fkill_all_local_variables.
724 If PERMANENT_TOO is 1, then we reset permanent
725 buffer-local variables. If PERMANENT_TOO is 0,
726 we preserve those. */
728 static void
729 reset_buffer_local_variables (b, permanent_too)
730 register struct buffer *b;
731 int permanent_too;
733 register int offset;
734 int i;
736 /* Reset the major mode to Fundamental, together with all the
737 things that depend on the major mode.
738 default-major-mode is handled at a higher level.
739 We ignore it here. */
740 b->major_mode = Qfundamental_mode;
741 b->keymap = Qnil;
742 b->mode_name = QSFundamental;
743 b->minor_modes = Qnil;
745 /* If the standard case table has been altered and invalidated,
746 fix up its insides first. */
747 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
748 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
749 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
750 Fset_standard_case_table (Vascii_downcase_table);
752 b->downcase_table = Vascii_downcase_table;
753 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
754 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
755 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
756 b->invisibility_spec = Qt;
757 #ifndef DOS_NT
758 b->buffer_file_type = Qnil;
759 #endif
761 /* Reset all (or most) per-buffer variables to their defaults. */
762 if (permanent_too)
763 b->local_var_alist = Qnil;
764 else
766 Lisp_Object tmp, prop, last = Qnil;
767 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
768 if (CONSP (XCAR (tmp))
769 && SYMBOLP (XCAR (XCAR (tmp)))
770 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
772 /* If permanent-local, keep it. */
773 last = tmp;
774 if (EQ (prop, Qpermanent_local_hook))
776 /* This is a partially permanent hook variable.
777 Preserve only the elements that want to be preserved. */
778 Lisp_Object list, newlist;
779 list = XCDR (XCAR (tmp));
780 if (!CONSP (list))
781 newlist = list;
782 else
783 for (newlist = Qnil; CONSP (list); list = XCDR (list))
785 Lisp_Object elt = XCAR (list);
786 /* Preserve element ELT if it's t,
787 if it is a function with a `permanent-local-hook' property,
788 or if it's not a symbol. */
789 if (! SYMBOLP (elt)
790 || EQ (elt, Qt)
791 || !NILP (Fget (elt, Qpermanent_local_hook)))
792 newlist = Fcons (elt, newlist);
794 XSETCDR (XCAR (tmp), Fnreverse (newlist));
797 /* Delete this local variable. */
798 else if (NILP (last))
799 b->local_var_alist = XCDR (tmp);
800 else
801 XSETCDR (last, XCDR (tmp));
804 for (i = 0; i < last_per_buffer_idx; ++i)
805 if (permanent_too || buffer_permanent_local_flags[i] == 0)
806 SET_PER_BUFFER_VALUE_P (b, i, 0);
808 /* For each slot that has a default value,
809 copy that into the slot. */
811 /* buffer-local Lisp variables start at `undo_list',
812 tho only the ones from `name' on are GC'd normally. */
813 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
814 offset < sizeof *b;
815 offset += sizeof (Lisp_Object))
817 int idx = PER_BUFFER_IDX (offset);
818 if ((idx > 0
819 && (permanent_too
820 || buffer_permanent_local_flags[idx] == 0))
821 /* Is -2 used anywhere? */
822 || idx == -2)
823 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
827 /* We split this away from generate-new-buffer, because rename-buffer
828 and set-visited-file-name ought to be able to use this to really
829 rename the buffer properly. */
831 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
832 1, 2, 0,
833 doc: /* Return a string that is the name of no existing buffer based on NAME.
834 If there is no live buffer named NAME, then return NAME.
835 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
836 \(starting at 2) until an unused name is found, and then return that name.
837 Optional second argument IGNORE specifies a name that is okay to use (if
838 it is in the sequence to be tried) even if a buffer with that name exists. */)
839 (name, ignore)
840 register Lisp_Object name, ignore;
842 register Lisp_Object gentemp, tem;
843 int count;
844 char number[10];
846 CHECK_STRING (name);
848 tem = Fstring_equal (name, ignore);
849 if (!NILP (tem))
850 return name;
851 tem = Fget_buffer (name);
852 if (NILP (tem))
853 return name;
855 count = 1;
856 while (1)
858 sprintf (number, "<%d>", ++count);
859 gentemp = concat2 (name, build_string (number));
860 tem = Fstring_equal (gentemp, ignore);
861 if (!NILP (tem))
862 return gentemp;
863 tem = Fget_buffer (gentemp);
864 if (NILP (tem))
865 return gentemp;
870 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
871 doc: /* Return the name of BUFFER, as a string.
872 With no argument or nil as argument, return the name of the current buffer. */)
873 (buffer)
874 register Lisp_Object buffer;
876 if (NILP (buffer))
877 return current_buffer->name;
878 CHECK_BUFFER (buffer);
879 return XBUFFER (buffer)->name;
882 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
883 doc: /* Return name of file BUFFER is visiting, or nil if none.
884 No argument or nil as argument means use the current buffer. */)
885 (buffer)
886 register Lisp_Object buffer;
888 if (NILP (buffer))
889 return current_buffer->filename;
890 CHECK_BUFFER (buffer);
891 return XBUFFER (buffer)->filename;
894 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
895 0, 1, 0,
896 doc: /* Return the base buffer of indirect buffer BUFFER.
897 If BUFFER is not indirect, return nil.
898 BUFFER defaults to the current buffer. */)
899 (buffer)
900 register Lisp_Object buffer;
902 struct buffer *base;
903 Lisp_Object base_buffer;
905 if (NILP (buffer))
906 base = current_buffer->base_buffer;
907 else
909 CHECK_BUFFER (buffer);
910 base = XBUFFER (buffer)->base_buffer;
913 if (! base)
914 return Qnil;
915 XSETBUFFER (base_buffer, base);
916 return base_buffer;
919 DEFUN ("buffer-local-value", Fbuffer_local_value,
920 Sbuffer_local_value, 2, 2, 0,
921 doc: /* Return the value of VARIABLE in BUFFER.
922 If VARIABLE does not have a buffer-local binding in BUFFER, the value
923 is the default binding of the variable. */)
924 (variable, buffer)
925 register Lisp_Object variable;
926 register Lisp_Object buffer;
928 register struct buffer *buf;
929 register Lisp_Object result;
930 struct Lisp_Symbol *sym;
932 CHECK_SYMBOL (variable);
933 CHECK_BUFFER (buffer);
934 buf = XBUFFER (buffer);
936 sym = indirect_variable (XSYMBOL (variable));
937 XSETSYMBOL (variable, sym);
939 /* Look in local_var_list */
940 result = Fassoc (variable, buf->local_var_alist);
941 if (NILP (result))
943 int offset, idx;
944 int found = 0;
946 /* Look in special slots */
947 /* buffer-local Lisp variables start at `undo_list',
948 tho only the ones from `name' on are GC'd normally. */
949 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
950 offset < sizeof (struct buffer);
951 /* sizeof EMACS_INT == sizeof Lisp_Object */
952 offset += (sizeof (EMACS_INT)))
954 idx = PER_BUFFER_IDX (offset);
955 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
956 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
957 && EQ (PER_BUFFER_SYMBOL (offset), variable))
959 result = PER_BUFFER_VALUE (buf, offset);
960 found = 1;
961 break;
965 if (!found)
966 result = Fdefault_value (variable);
968 else
970 Lisp_Object valcontents;
971 Lisp_Object current_alist_element;
973 /* What binding is loaded right now? */
974 valcontents = sym->value;
975 current_alist_element
976 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
978 /* The value of the currently loaded binding is not
979 stored in it, but rather in the realvalue slot.
980 Store that value into the binding it belongs to
981 in case that is the one we are about to use. */
983 Fsetcdr (current_alist_element,
984 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
986 /* Now get the (perhaps updated) value out of the binding. */
987 result = XCDR (result);
990 if (!EQ (result, Qunbound))
991 return result;
993 xsignal1 (Qvoid_variable, variable);
996 /* Return an alist of the Lisp-level buffer-local bindings of
997 buffer BUF. That is, don't include the variables maintained
998 in special slots in the buffer object. */
1000 static Lisp_Object
1001 buffer_lisp_local_variables (buf)
1002 struct buffer *buf;
1004 Lisp_Object result = Qnil;
1005 register Lisp_Object tail;
1006 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1008 Lisp_Object val, elt;
1010 elt = XCAR (tail);
1012 /* Reference each variable in the alist in buf.
1013 If inquiring about the current buffer, this gets the current values,
1014 so store them into the alist so the alist is up to date.
1015 If inquiring about some other buffer, this swaps out any values
1016 for that buffer, making the alist up to date automatically. */
1017 val = find_symbol_value (XCAR (elt));
1018 /* Use the current buffer value only if buf is the current buffer. */
1019 if (buf != current_buffer)
1020 val = XCDR (elt);
1022 /* If symbol is unbound, put just the symbol in the list. */
1023 if (EQ (val, Qunbound))
1024 result = Fcons (XCAR (elt), result);
1025 /* Otherwise, put (symbol . value) in the list. */
1026 else
1027 result = Fcons (Fcons (XCAR (elt), val), result);
1030 return result;
1033 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1034 Sbuffer_local_variables, 0, 1, 0,
1035 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1036 Most elements look like (SYMBOL . VALUE), describing one variable.
1037 For a symbol that is locally unbound, just the symbol appears in the value.
1038 Note that storing new VALUEs in these elements doesn't change the variables.
1039 No argument or nil as argument means use current buffer as BUFFER. */)
1040 (buffer)
1041 register Lisp_Object buffer;
1043 register struct buffer *buf;
1044 register Lisp_Object result;
1046 if (NILP (buffer))
1047 buf = current_buffer;
1048 else
1050 CHECK_BUFFER (buffer);
1051 buf = XBUFFER (buffer);
1054 result = buffer_lisp_local_variables (buf);
1056 /* Add on all the variables stored in special slots. */
1058 int offset, idx;
1060 /* buffer-local Lisp variables start at `undo_list',
1061 tho only the ones from `name' on are GC'd normally. */
1062 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1063 offset < sizeof (struct buffer);
1064 /* sizeof EMACS_INT == sizeof Lisp_Object */
1065 offset += (sizeof (EMACS_INT)))
1067 idx = PER_BUFFER_IDX (offset);
1068 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1069 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1070 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1071 PER_BUFFER_VALUE (buf, offset)),
1072 result);
1076 return result;
1079 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1080 0, 1, 0,
1081 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1082 No argument or nil as argument means use current buffer as BUFFER. */)
1083 (buffer)
1084 register Lisp_Object buffer;
1086 register struct buffer *buf;
1087 if (NILP (buffer))
1088 buf = current_buffer;
1089 else
1091 CHECK_BUFFER (buffer);
1092 buf = XBUFFER (buffer);
1095 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1098 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1099 1, 1, 0,
1100 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1101 A non-nil FLAG means mark the buffer modified. */)
1102 (flag)
1103 register Lisp_Object flag;
1105 register int already;
1106 register Lisp_Object fn;
1107 Lisp_Object buffer, window;
1109 #ifdef CLASH_DETECTION
1110 /* If buffer becoming modified, lock the file.
1111 If buffer becoming unmodified, unlock the file. */
1113 fn = current_buffer->file_truename;
1114 /* Test buffer-file-name so that binding it to nil is effective. */
1115 if (!NILP (fn) && ! NILP (current_buffer->filename))
1117 already = SAVE_MODIFF < MODIFF;
1118 if (!already && !NILP (flag))
1119 lock_file (fn);
1120 else if (already && NILP (flag))
1121 unlock_file (fn);
1123 #endif /* CLASH_DETECTION */
1125 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1127 /* Set update_mode_lines only if buffer is displayed in some window.
1128 Packages like jit-lock or lazy-lock preserve a buffer's modified
1129 state by recording/restoring the state around blocks of code.
1130 Setting update_mode_lines makes redisplay consider all windows
1131 (on all frames). Stealth fontification of buffers not displayed
1132 would incur additional redisplay costs if we'd set
1133 update_modes_lines unconditionally.
1135 Ideally, I think there should be another mechanism for fontifying
1136 buffers without "modifying" buffers, or redisplay should be
1137 smarter about updating the `*' in mode lines. --gerd */
1138 XSETBUFFER (buffer, current_buffer);
1139 window = Fget_buffer_window (buffer, Qt);
1140 if (WINDOWP (window))
1142 ++update_mode_lines;
1143 current_buffer->prevent_redisplay_optimizations_p = 1;
1146 return flag;
1149 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1150 Srestore_buffer_modified_p, 1, 1, 0,
1151 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1152 It is not ensured that mode lines will be updated to show the modified
1153 state of the current buffer. Use with care. */)
1154 (flag)
1155 Lisp_Object flag;
1157 #ifdef CLASH_DETECTION
1158 Lisp_Object fn;
1160 /* If buffer becoming modified, lock the file.
1161 If buffer becoming unmodified, unlock the file. */
1163 fn = current_buffer->file_truename;
1164 /* Test buffer-file-name so that binding it to nil is effective. */
1165 if (!NILP (fn) && ! NILP (current_buffer->filename))
1167 int already = SAVE_MODIFF < MODIFF;
1168 if (!already && !NILP (flag))
1169 lock_file (fn);
1170 else if (already && NILP (flag))
1171 unlock_file (fn);
1173 #endif /* CLASH_DETECTION */
1175 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1176 return flag;
1179 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1180 0, 1, 0,
1181 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1182 Each buffer has a tick counter which is incremented each time the
1183 text in that buffer is changed. It wraps around occasionally.
1184 No argument or nil as argument means use current buffer as BUFFER. */)
1185 (buffer)
1186 register Lisp_Object buffer;
1188 register struct buffer *buf;
1189 if (NILP (buffer))
1190 buf = current_buffer;
1191 else
1193 CHECK_BUFFER (buffer);
1194 buf = XBUFFER (buffer);
1197 return make_number (BUF_MODIFF (buf));
1200 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1201 Sbuffer_chars_modified_tick, 0, 1, 0,
1202 doc: /* Return BUFFER's character-change tick counter.
1203 Each buffer has a character-change tick counter, which is set to the
1204 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1205 time text in that buffer is inserted or deleted. By comparing the
1206 values returned by two individual calls of `buffer-chars-modified-tick',
1207 you can tell whether a character change occurred in that buffer in
1208 between these calls. No argument or nil as argument means use current
1209 buffer as BUFFER. */)
1210 (buffer)
1211 register Lisp_Object buffer;
1213 register struct buffer *buf;
1214 if (NILP (buffer))
1215 buf = current_buffer;
1216 else
1218 CHECK_BUFFER (buffer);
1219 buf = XBUFFER (buffer);
1222 return make_number (BUF_CHARS_MODIFF (buf));
1225 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1226 "(list (read-string \"Rename buffer (to new name): \" \
1227 nil 'buffer-name-history (buffer-name (current-buffer))) \
1228 current-prefix-arg)",
1229 doc: /* Change current buffer's name to NEWNAME (a string).
1230 If second arg UNIQUE is nil or omitted, it is an error if a
1231 buffer named NEWNAME already exists.
1232 If UNIQUE is non-nil, come up with a new name using
1233 `generate-new-buffer-name'.
1234 Interactively, you can set UNIQUE with a prefix argument.
1235 We return the name we actually gave the buffer.
1236 This does not change the name of the visited file (if any). */)
1237 (newname, unique)
1238 register Lisp_Object newname, unique;
1240 register Lisp_Object tem, buf;
1242 CHECK_STRING (newname);
1244 if (SCHARS (newname) == 0)
1245 error ("Empty string is invalid as a buffer name");
1247 tem = Fget_buffer (newname);
1248 if (!NILP (tem))
1250 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1251 rename the buffer automatically so you can create another
1252 with the original name. It makes UNIQUE equivalent to
1253 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1254 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1255 return current_buffer->name;
1256 if (!NILP (unique))
1257 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1258 else
1259 error ("Buffer name `%s' is in use", SDATA (newname));
1262 current_buffer->name = newname;
1264 /* Catch redisplay's attention. Unless we do this, the mode lines for
1265 any windows displaying current_buffer will stay unchanged. */
1266 update_mode_lines++;
1268 XSETBUFFER (buf, current_buffer);
1269 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1270 if (NILP (current_buffer->filename)
1271 && !NILP (current_buffer->auto_save_file_name))
1272 call0 (intern ("rename-auto-save-file"));
1273 /* Refetch since that last call may have done GC. */
1274 return current_buffer->name;
1277 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1278 doc: /* Return most recently selected buffer other than BUFFER.
1279 Buffers not visible in windows are preferred to visible buffers,
1280 unless optional second argument VISIBLE-OK is non-nil.
1281 If the optional third argument FRAME is non-nil, use that frame's
1282 buffer list instead of the selected frame's buffer list.
1283 If no other buffer exists, the buffer `*scratch*' is returned.
1284 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1285 (buffer, visible_ok, frame)
1286 register Lisp_Object buffer, visible_ok, frame;
1288 Lisp_Object Fset_buffer_major_mode ();
1289 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1290 notsogood = Qnil;
1292 if (NILP (frame))
1293 frame = selected_frame;
1295 tail = Vbuffer_alist;
1296 pred = frame_buffer_predicate (frame);
1298 /* Consider buffers that have been seen in the selected frame
1299 before other buffers. */
1301 tem = frame_buffer_list (frame);
1302 add_ons = Qnil;
1303 while (CONSP (tem))
1305 if (BUFFERP (XCAR (tem)))
1306 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1307 tem = XCDR (tem);
1309 tail = nconc2 (Fnreverse (add_ons), tail);
1311 for (; CONSP (tail); tail = XCDR (tail))
1313 buf = Fcdr (XCAR (tail));
1314 if (EQ (buf, buffer))
1315 continue;
1316 if (NILP (buf))
1317 continue;
1318 if (NILP (XBUFFER (buf)->name))
1319 continue;
1320 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1321 continue;
1322 /* If the selected frame has a buffer_predicate,
1323 disregard buffers that don't fit the predicate. */
1324 if (!NILP (pred))
1326 tem = call1 (pred, buf);
1327 if (NILP (tem))
1328 continue;
1331 if (NILP (visible_ok))
1332 tem = Fget_buffer_window (buf, Qvisible);
1333 else
1334 tem = Qnil;
1335 if (NILP (tem))
1336 return buf;
1337 if (NILP (notsogood))
1338 notsogood = buf;
1340 if (!NILP (notsogood))
1341 return notsogood;
1342 buf = Fget_buffer (build_string ("*scratch*"));
1343 if (NILP (buf))
1345 buf = Fget_buffer_create (build_string ("*scratch*"));
1346 Fset_buffer_major_mode (buf);
1348 return buf;
1351 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1352 0, 1, "",
1353 doc: /* Start keeping undo information for buffer BUFFER.
1354 No argument or nil as argument means do this for the current buffer. */)
1355 (buffer)
1356 register Lisp_Object buffer;
1358 Lisp_Object real_buffer;
1360 if (NILP (buffer))
1361 XSETBUFFER (real_buffer, current_buffer);
1362 else
1364 real_buffer = Fget_buffer (buffer);
1365 if (NILP (real_buffer))
1366 nsberror (buffer);
1369 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1370 XBUFFER (real_buffer)->undo_list = Qnil;
1372 return Qnil;
1376 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1377 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1378 The buffer being killed will be current while the hook is running.\n\
1379 See `kill-buffer'."
1381 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1382 doc: /* Kill the buffer BUFFER.
1383 The argument may be a buffer or the name of a buffer.
1384 With a nil argument, kill the current buffer.
1386 Value is t if the buffer is actually killed, nil otherwise.
1388 The functions in `kill-buffer-query-functions' are called with BUFFER as
1389 the current buffer. If any of them returns nil, the buffer is not killed.
1391 The hook `kill-buffer-hook' is run before the buffer is actually killed.
1392 The buffer being killed will be current while the hook is running.
1394 Any processes that have this buffer as the `process-buffer' are killed
1395 with SIGHUP. */)
1396 (buffer)
1397 Lisp_Object buffer;
1399 Lisp_Object buf;
1400 register struct buffer *b;
1401 register Lisp_Object tem;
1402 register struct Lisp_Marker *m;
1403 struct gcpro gcpro1;
1405 if (NILP (buffer))
1406 buf = Fcurrent_buffer ();
1407 else
1408 buf = Fget_buffer (buffer);
1409 if (NILP (buf))
1410 nsberror (buffer);
1412 b = XBUFFER (buf);
1414 /* Avoid trouble for buffer already dead. */
1415 if (NILP (b->name))
1416 return Qnil;
1418 /* Query if the buffer is still modified. */
1419 if (INTERACTIVE && !NILP (b->filename)
1420 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1422 GCPRO1 (buf);
1423 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1424 b->name, make_number (0)));
1425 UNGCPRO;
1426 if (NILP (tem))
1427 return Qnil;
1430 /* Run hooks with the buffer to be killed the current buffer. */
1432 int count = SPECPDL_INDEX ();
1433 Lisp_Object arglist[1];
1435 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1436 set_buffer_internal (b);
1438 /* First run the query functions; if any query is answered no,
1439 don't kill the buffer. */
1440 arglist[0] = Qkill_buffer_query_functions;
1441 tem = Frun_hook_with_args_until_failure (1, arglist);
1442 if (NILP (tem))
1443 return unbind_to (count, Qnil);
1445 /* Then run the hooks. */
1446 Frun_hooks (1, &Qkill_buffer_hook);
1447 unbind_to (count, Qnil);
1450 /* We have no more questions to ask. Verify that it is valid
1451 to kill the buffer. This must be done after the questions
1452 since anything can happen within do_yes_or_no_p. */
1454 /* Don't kill the minibuffer now current. */
1455 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1456 return Qnil;
1458 if (NILP (b->name))
1459 return Qnil;
1461 /* When we kill a base buffer, kill all its indirect buffers.
1462 We do it at this stage so nothing terrible happens if they
1463 ask questions or their hooks get errors. */
1464 if (! b->base_buffer)
1466 struct buffer *other;
1468 GCPRO1 (buf);
1470 for (other = all_buffers; other; other = other->next)
1471 /* all_buffers contains dead buffers too;
1472 don't re-kill them. */
1473 if (other->base_buffer == b && !NILP (other->name))
1475 Lisp_Object buf;
1476 XSETBUFFER (buf, other);
1477 Fkill_buffer (buf);
1480 UNGCPRO;
1483 /* Make this buffer not be current.
1484 In the process, notice if this is the sole visible buffer
1485 and give up if so. */
1486 if (b == current_buffer)
1488 tem = Fother_buffer (buf, Qnil, Qnil);
1489 Fset_buffer (tem);
1490 if (b == current_buffer)
1491 return Qnil;
1494 /* Notice if the buffer to kill is the sole visible buffer
1495 when we're currently in the mini-buffer, and give up if so. */
1496 XSETBUFFER (tem, current_buffer);
1497 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1499 tem = Fother_buffer (buf, Qnil, Qnil);
1500 if (EQ (buf, tem))
1501 return Qnil;
1504 /* Now there is no question: we can kill the buffer. */
1506 #ifdef CLASH_DETECTION
1507 /* Unlock this buffer's file, if it is locked. */
1508 unlock_buffer (b);
1509 #endif /* CLASH_DETECTION */
1511 GCPRO1 (buf);
1512 kill_buffer_processes (buf);
1513 UNGCPRO;
1515 /* Killing buffer processes may run sentinels which may
1516 have called kill-buffer. */
1518 if (NILP (b->name))
1519 return Qnil;
1521 clear_charpos_cache (b);
1523 tem = Vinhibit_quit;
1524 Vinhibit_quit = Qt;
1525 replace_buffer_in_all_windows (buf);
1526 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1527 frames_discard_buffer (buf);
1528 Vinhibit_quit = tem;
1530 /* Delete any auto-save file, if we saved it in this session.
1531 But not if the buffer is modified. */
1532 if (STRINGP (b->auto_save_file_name)
1533 && b->auto_save_modified != 0
1534 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1535 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1536 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1538 Lisp_Object tem;
1539 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1540 if (! NILP (tem))
1541 internal_delete_file (b->auto_save_file_name);
1544 if (b->base_buffer)
1546 /* Unchain all markers that belong to this indirect buffer.
1547 Don't unchain the markers that belong to the base buffer
1548 or its other indirect buffers. */
1549 for (m = BUF_MARKERS (b); m; )
1551 struct Lisp_Marker *next = m->next;
1552 if (m->buffer == b)
1553 unchain_marker (m);
1554 m = next;
1557 else
1559 /* Unchain all markers of this buffer and its indirect buffers.
1560 and leave them pointing nowhere. */
1561 for (m = BUF_MARKERS (b); m; )
1563 struct Lisp_Marker *next = m->next;
1564 m->buffer = 0;
1565 m->next = NULL;
1566 m = next;
1568 BUF_MARKERS (b) = NULL;
1569 BUF_INTERVALS (b) = NULL_INTERVAL;
1571 /* Perhaps we should explicitly free the interval tree here... */
1574 /* Reset the local variables, so that this buffer's local values
1575 won't be protected from GC. They would be protected
1576 if they happened to remain encached in their symbols.
1577 This gets rid of them for certain. */
1578 swap_out_buffer_local_variables (b);
1579 reset_buffer_local_variables (b, 1);
1581 b->name = Qnil;
1583 BLOCK_INPUT;
1584 if (! b->base_buffer)
1585 free_buffer_text (b);
1587 if (b->newline_cache)
1589 free_region_cache (b->newline_cache);
1590 b->newline_cache = 0;
1592 if (b->width_run_cache)
1594 free_region_cache (b->width_run_cache);
1595 b->width_run_cache = 0;
1597 b->width_table = Qnil;
1598 UNBLOCK_INPUT;
1599 b->undo_list = Qnil;
1601 return Qt;
1604 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1605 we do this each time BUF is selected visibly, the more recently
1606 selected buffers are always closer to the front of the list. This
1607 means that other_buffer is more likely to choose a relevant buffer. */
1609 void
1610 record_buffer (buf)
1611 Lisp_Object buf;
1613 register Lisp_Object link, prev;
1614 Lisp_Object frame;
1615 frame = selected_frame;
1617 prev = Qnil;
1618 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1620 if (EQ (XCDR (XCAR (link)), buf))
1621 break;
1622 prev = link;
1625 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1626 we cannot use Fdelq itself here because it allows quitting. */
1628 if (NILP (prev))
1629 Vbuffer_alist = XCDR (Vbuffer_alist);
1630 else
1631 XSETCDR (prev, XCDR (XCDR (prev)));
1633 XSETCDR (link, Vbuffer_alist);
1634 Vbuffer_alist = link;
1636 /* Effectively do a delq on buried_buffer_list. */
1638 prev = Qnil;
1639 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1640 link = XCDR (link))
1642 if (EQ (XCAR (link), buf))
1644 if (NILP (prev))
1645 XFRAME (frame)->buried_buffer_list = XCDR (link);
1646 else
1647 XSETCDR (prev, XCDR (XCDR (prev)));
1648 break;
1650 prev = link;
1653 /* Now move this buffer to the front of frame_buffer_list also. */
1655 prev = Qnil;
1656 for (link = frame_buffer_list (frame); CONSP (link);
1657 link = XCDR (link))
1659 if (EQ (XCAR (link), buf))
1660 break;
1661 prev = link;
1664 /* Effectively do delq. */
1666 if (CONSP (link))
1668 if (NILP (prev))
1669 set_frame_buffer_list (frame,
1670 XCDR (frame_buffer_list (frame)));
1671 else
1672 XSETCDR (prev, XCDR (XCDR (prev)));
1674 XSETCDR (link, frame_buffer_list (frame));
1675 set_frame_buffer_list (frame, link);
1677 else
1678 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1681 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1682 doc: /* Set an appropriate major mode for BUFFER.
1683 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1684 according to `default-major-mode'.
1685 Use this function before selecting the buffer, since it may need to inspect
1686 the current buffer's major mode. */)
1687 (buffer)
1688 Lisp_Object buffer;
1690 int count;
1691 Lisp_Object function;
1693 CHECK_BUFFER (buffer);
1695 if (STRINGP (XBUFFER (buffer)->name)
1696 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1697 function = find_symbol_value (intern ("initial-major-mode"));
1698 else
1700 function = buffer_defaults.major_mode;
1701 if (NILP (function)
1702 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1703 function = current_buffer->major_mode;
1706 if (NILP (function) || EQ (function, Qfundamental_mode))
1707 return Qnil;
1709 count = SPECPDL_INDEX ();
1711 /* To select a nonfundamental mode,
1712 select the buffer temporarily and then call the mode function. */
1714 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1716 Fset_buffer (buffer);
1717 call0 (function);
1719 return unbind_to (count, Qnil);
1722 /* If switching buffers in WINDOW would be an error, return
1723 a C string saying what the error would be. */
1725 char *
1726 no_switch_window (window)
1727 Lisp_Object window;
1729 Lisp_Object tem;
1730 if (EQ (minibuf_window, window))
1731 return "Cannot switch buffers in minibuffer window";
1732 tem = Fwindow_dedicated_p (window);
1733 if (EQ (tem, Qt))
1734 return "Cannot switch buffers in a dedicated window";
1735 return NULL;
1738 /* Switch to buffer BUFFER in the selected window.
1739 If NORECORD is non-nil, don't call record_buffer. */
1741 Lisp_Object
1742 switch_to_buffer_1 (buffer, norecord)
1743 Lisp_Object buffer, norecord;
1745 register Lisp_Object buf;
1747 if (NILP (buffer))
1748 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1749 else
1751 buf = Fget_buffer (buffer);
1752 if (NILP (buf))
1754 buf = Fget_buffer_create (buffer);
1755 Fset_buffer_major_mode (buf);
1758 Fset_buffer (buf);
1759 if (NILP (norecord))
1760 record_buffer (buf);
1762 Fset_window_buffer (EQ (selected_window, minibuf_window)
1763 ? Fnext_window (minibuf_window, Qnil, Qnil)
1764 : selected_window,
1765 buf, Qnil);
1767 return buf;
1770 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1771 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1772 doc: /* Select buffer BUFFER in the current window.
1773 If BUFFER does not identify an existing buffer,
1774 then this function creates a buffer with that name.
1776 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1777 or nil. If BUFFER is nil, then this function chooses a buffer
1778 using `other-buffer'.
1779 Optional second arg NORECORD non-nil means
1780 do not put this buffer at the front of the list of recently selected ones.
1781 This function returns the buffer it switched to.
1783 WARNING: This is NOT the way to work on another buffer temporarily
1784 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1785 the window-buffer correspondences. */)
1786 (buffer, norecord)
1787 Lisp_Object buffer, norecord;
1789 char *err;
1791 if (EQ (buffer, Fwindow_buffer (selected_window)))
1793 /* Basically a NOP. Avoid signalling an error in the case where
1794 the selected window is dedicated, or a minibuffer. */
1796 /* But do put this buffer at the front of the buffer list,
1797 unless that has been inhibited. Note that even if
1798 BUFFER is at the front of the main buffer-list already,
1799 we still want to move it to the front of the frame's buffer list. */
1800 if (NILP (norecord))
1801 record_buffer (buffer);
1802 return Fset_buffer (buffer);
1805 err = no_switch_window (selected_window);
1806 if (err)
1807 /* If can't display in current window, let pop-to-buffer
1808 try some other window. */
1809 return call3 (intern ("pop-to-buffer"), buffer, Qnil, norecord);
1811 return switch_to_buffer_1 (buffer, norecord);
1814 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1815 doc: /* Return the current buffer as a Lisp object. */)
1818 register Lisp_Object buf;
1819 XSETBUFFER (buf, current_buffer);
1820 return buf;
1823 /* Set the current buffer to B.
1825 We previously set windows_or_buffers_changed here to invalidate
1826 global unchanged information in beg_unchanged and end_unchanged.
1827 This is no longer necessary because we now compute unchanged
1828 information on a buffer-basis. Every action affecting other
1829 windows than the selected one requires a select_window at some
1830 time, and that increments windows_or_buffers_changed. */
1832 void
1833 set_buffer_internal (b)
1834 register struct buffer *b;
1836 if (current_buffer != b)
1837 set_buffer_internal_1 (b);
1840 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1841 This is used by redisplay. */
1843 void
1844 set_buffer_internal_1 (b)
1845 register struct buffer *b;
1847 register struct buffer *old_buf;
1848 register Lisp_Object tail, valcontents;
1849 Lisp_Object tem;
1851 #ifdef USE_MMAP_FOR_BUFFERS
1852 if (b->text->beg == NULL)
1853 enlarge_buffer_text (b, 0);
1854 #endif /* USE_MMAP_FOR_BUFFERS */
1856 if (current_buffer == b)
1857 return;
1859 old_buf = current_buffer;
1860 current_buffer = b;
1861 last_known_column_point = -1; /* invalidate indentation cache */
1863 if (old_buf)
1865 /* Put the undo list back in the base buffer, so that it appears
1866 that an indirect buffer shares the undo list of its base. */
1867 if (old_buf->base_buffer)
1868 old_buf->base_buffer->undo_list = old_buf->undo_list;
1870 /* If the old current buffer has markers to record PT, BEGV and ZV
1871 when it is not current, update them now. */
1872 if (! NILP (old_buf->pt_marker))
1874 Lisp_Object obuf;
1875 XSETBUFFER (obuf, old_buf);
1876 set_marker_both (old_buf->pt_marker, obuf,
1877 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1879 if (! NILP (old_buf->begv_marker))
1881 Lisp_Object obuf;
1882 XSETBUFFER (obuf, old_buf);
1883 set_marker_both (old_buf->begv_marker, obuf,
1884 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1886 if (! NILP (old_buf->zv_marker))
1888 Lisp_Object obuf;
1889 XSETBUFFER (obuf, old_buf);
1890 set_marker_both (old_buf->zv_marker, obuf,
1891 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1895 /* Get the undo list from the base buffer, so that it appears
1896 that an indirect buffer shares the undo list of its base. */
1897 if (b->base_buffer)
1898 b->undo_list = b->base_buffer->undo_list;
1900 /* If the new current buffer has markers to record PT, BEGV and ZV
1901 when it is not current, fetch them now. */
1902 if (! NILP (b->pt_marker))
1904 BUF_PT (b) = marker_position (b->pt_marker);
1905 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1907 if (! NILP (b->begv_marker))
1909 BUF_BEGV (b) = marker_position (b->begv_marker);
1910 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1912 if (! NILP (b->zv_marker))
1914 BUF_ZV (b) = marker_position (b->zv_marker);
1915 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1918 /* Look down buffer's list of local Lisp variables
1919 to find and update any that forward into C variables. */
1921 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1923 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1924 if ((BUFFER_LOCAL_VALUEP (valcontents))
1925 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1926 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1927 /* Just reference the variable
1928 to cause it to become set for this buffer. */
1929 Fsymbol_value (XCAR (XCAR (tail)));
1932 /* Do the same with any others that were local to the previous buffer */
1934 if (old_buf)
1935 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1937 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1938 if ((BUFFER_LOCAL_VALUEP (valcontents))
1939 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1940 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1941 /* Just reference the variable
1942 to cause it to become set for this buffer. */
1943 Fsymbol_value (XCAR (XCAR (tail)));
1947 /* Switch to buffer B temporarily for redisplay purposes.
1948 This avoids certain things that don't need to be done within redisplay. */
1950 void
1951 set_buffer_temp (b)
1952 struct buffer *b;
1954 register struct buffer *old_buf;
1956 if (current_buffer == b)
1957 return;
1959 old_buf = current_buffer;
1960 current_buffer = b;
1962 if (old_buf)
1964 /* If the old current buffer has markers to record PT, BEGV and ZV
1965 when it is not current, update them now. */
1966 if (! NILP (old_buf->pt_marker))
1968 Lisp_Object obuf;
1969 XSETBUFFER (obuf, old_buf);
1970 set_marker_both (old_buf->pt_marker, obuf,
1971 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1973 if (! NILP (old_buf->begv_marker))
1975 Lisp_Object obuf;
1976 XSETBUFFER (obuf, old_buf);
1977 set_marker_both (old_buf->begv_marker, obuf,
1978 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1980 if (! NILP (old_buf->zv_marker))
1982 Lisp_Object obuf;
1983 XSETBUFFER (obuf, old_buf);
1984 set_marker_both (old_buf->zv_marker, obuf,
1985 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1989 /* If the new current buffer has markers to record PT, BEGV and ZV
1990 when it is not current, fetch them now. */
1991 if (! NILP (b->pt_marker))
1993 BUF_PT (b) = marker_position (b->pt_marker);
1994 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1996 if (! NILP (b->begv_marker))
1998 BUF_BEGV (b) = marker_position (b->begv_marker);
1999 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2001 if (! NILP (b->zv_marker))
2003 BUF_ZV (b) = marker_position (b->zv_marker);
2004 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2008 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2009 doc: /* Make the buffer BUFFER current for editing operations.
2010 BUFFER may be a buffer or the name of an existing buffer.
2011 See also `save-excursion' when you want to make a buffer current temporarily.
2012 This function does not display the buffer, so its effect ends
2013 when the current command terminates.
2014 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
2015 (buffer)
2016 register Lisp_Object buffer;
2018 register Lisp_Object buf;
2019 buf = Fget_buffer (buffer);
2020 if (NILP (buf))
2021 nsberror (buffer);
2022 if (NILP (XBUFFER (buf)->name))
2023 error ("Selecting deleted buffer");
2024 set_buffer_internal (XBUFFER (buf));
2025 return buf;
2028 /* Set the current buffer to BUFFER provided it is alive. */
2030 Lisp_Object
2031 set_buffer_if_live (buffer)
2032 Lisp_Object buffer;
2034 if (! NILP (XBUFFER (buffer)->name))
2035 Fset_buffer (buffer);
2036 return Qnil;
2039 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2040 Sbarf_if_buffer_read_only, 0, 0, 0,
2041 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2044 if (!NILP (current_buffer->read_only)
2045 && NILP (Vinhibit_read_only))
2046 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2047 return Qnil;
2050 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2051 doc: /* Put BUFFER at the end of the list of all buffers.
2052 There it is the least likely candidate for `other-buffer' to return;
2053 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
2054 You can specify a buffer name as BUFFER, or an actual buffer object.
2055 If BUFFER is nil or omitted, bury the current buffer.
2056 Also, if BUFFER is nil or omitted, remove the current buffer from the
2057 selected window if it is displayed there. */)
2058 (buffer)
2059 register Lisp_Object buffer;
2061 /* Figure out what buffer we're going to bury. */
2062 if (NILP (buffer))
2064 Lisp_Object tem;
2065 XSETBUFFER (buffer, current_buffer);
2067 tem = Fwindow_buffer (selected_window);
2068 /* If we're burying the current buffer, unshow it. */
2069 if (EQ (buffer, tem))
2071 if (NILP (Fwindow_dedicated_p (selected_window)))
2072 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2073 else if (NILP (XWINDOW (selected_window)->parent))
2074 Ficonify_frame (Fwindow_frame (selected_window));
2075 else
2076 Fdelete_window (selected_window);
2079 else
2081 Lisp_Object buf1;
2083 buf1 = Fget_buffer (buffer);
2084 if (NILP (buf1))
2085 nsberror (buffer);
2086 buffer = buf1;
2089 /* Move buffer to the end of the buffer list. Do nothing if the
2090 buffer is killed. */
2091 if (!NILP (XBUFFER (buffer)->name))
2093 Lisp_Object aelt, link;
2095 aelt = Frassq (buffer, Vbuffer_alist);
2096 link = Fmemq (aelt, Vbuffer_alist);
2097 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2098 XSETCDR (link, Qnil);
2099 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2101 XFRAME (selected_frame)->buffer_list
2102 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2103 XFRAME (selected_frame)->buried_buffer_list
2104 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2107 return Qnil;
2110 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2111 doc: /* Delete the entire contents of the current buffer.
2112 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2113 so the buffer is truly empty after this. */)
2116 Fwiden ();
2118 del_range (BEG, Z);
2120 current_buffer->last_window_start = 1;
2121 /* Prevent warnings, or suspension of auto saving, that would happen
2122 if future size is less than past size. Use of erase-buffer
2123 implies that the future text is not really related to the past text. */
2124 XSETFASTINT (current_buffer->save_length, 0);
2125 return Qnil;
2128 void
2129 validate_region (b, e)
2130 register Lisp_Object *b, *e;
2132 CHECK_NUMBER_COERCE_MARKER (*b);
2133 CHECK_NUMBER_COERCE_MARKER (*e);
2135 if (XINT (*b) > XINT (*e))
2137 Lisp_Object tem;
2138 tem = *b; *b = *e; *e = tem;
2141 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2142 && XINT (*e) <= ZV))
2143 args_out_of_range (*b, *e);
2146 /* Advance BYTE_POS up to a character boundary
2147 and return the adjusted position. */
2149 static int
2150 advance_to_char_boundary (byte_pos)
2151 int byte_pos;
2153 int c;
2155 if (byte_pos == BEG)
2156 /* Beginning of buffer is always a character boundary. */
2157 return BEG;
2159 c = FETCH_BYTE (byte_pos);
2160 if (! CHAR_HEAD_P (c))
2162 /* We should advance BYTE_POS only when C is a constituent of a
2163 multibyte sequence. */
2164 int orig_byte_pos = byte_pos;
2168 byte_pos--;
2169 c = FETCH_BYTE (byte_pos);
2171 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2172 INC_POS (byte_pos);
2173 if (byte_pos < orig_byte_pos)
2174 byte_pos = orig_byte_pos;
2175 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2176 surely advance to the correct character boundary. If C is
2177 not, BYTE_POS was unchanged. */
2180 return byte_pos;
2183 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2184 1, 1, 0,
2185 doc: /* Swap the text between current buffer and BUFFER. */)
2186 (buffer)
2187 Lisp_Object buffer;
2189 struct buffer *other_buffer;
2190 CHECK_BUFFER (buffer);
2191 other_buffer = XBUFFER (buffer);
2193 /* Actually, it probably works just fine.
2194 * if (other_buffer == current_buffer)
2195 * error ("Cannot swap a buffer's text with itself"); */
2197 /* Actually, this may be workable as well, tho probably only if they're
2198 *both* indirect. */
2199 if (other_buffer->base_buffer
2200 || current_buffer->base_buffer)
2201 error ("Cannot swap indirect buffers's text");
2203 { /* This is probably harder to make work. */
2204 struct buffer *other;
2205 for (other = all_buffers; other; other = other->next)
2206 if (other->base_buffer == other_buffer
2207 || other->base_buffer == current_buffer)
2208 error ("One of the buffers to swap has indirect buffers");
2211 #define swapfield(field, type) \
2212 do { \
2213 type tmp##field = other_buffer->field; \
2214 other_buffer->field = current_buffer->field; \
2215 current_buffer->field = tmp##field; \
2216 } while (0)
2218 swapfield (own_text, struct buffer_text);
2219 eassert (current_buffer->text == &current_buffer->own_text);
2220 eassert (other_buffer->text == &other_buffer->own_text);
2221 swapfield (pt, EMACS_INT);
2222 swapfield (pt_byte, EMACS_INT);
2223 swapfield (begv, EMACS_INT);
2224 swapfield (begv_byte, EMACS_INT);
2225 swapfield (zv, EMACS_INT);
2226 swapfield (zv_byte, EMACS_INT);
2227 eassert (!current_buffer->base_buffer);
2228 eassert (!other_buffer->base_buffer);
2229 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2230 swapfield (newline_cache, struct region_cache *);
2231 swapfield (width_run_cache, struct region_cache *);
2232 current_buffer->prevent_redisplay_optimizations_p = 1;
2233 other_buffer->prevent_redisplay_optimizations_p = 1;
2234 swapfield (overlays_before, struct Lisp_Overlay *);
2235 swapfield (overlays_after, struct Lisp_Overlay *);
2236 swapfield (overlay_center, EMACS_INT);
2237 swapfield (undo_list, Lisp_Object);
2238 swapfield (mark, Lisp_Object);
2239 if (MARKERP (current_buffer->mark) && XMARKER (current_buffer->mark)->buffer)
2240 XMARKER (current_buffer->mark)->buffer = current_buffer;
2241 if (MARKERP (other_buffer->mark) && XMARKER (other_buffer->mark)->buffer)
2242 XMARKER (other_buffer->mark)->buffer = other_buffer;
2243 swapfield (enable_multibyte_characters, Lisp_Object);
2244 /* FIXME: Not sure what we should do with these *_marker fields.
2245 Hopefully they're just nil anyway. */
2246 swapfield (pt_marker, Lisp_Object);
2247 swapfield (begv_marker, Lisp_Object);
2248 swapfield (zv_marker, Lisp_Object);
2249 current_buffer->point_before_scroll = Qnil;
2250 other_buffer->point_before_scroll = Qnil;
2252 current_buffer->text->modiff++; other_buffer->text->modiff++;
2253 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2254 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2255 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2256 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2257 other_buffer->text->beg_unchanged = current_buffer->text->gpt;
2258 other_buffer->text->end_unchanged = current_buffer->text->gpt;
2260 struct Lisp_Marker *m;
2261 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2262 if (m->buffer == other_buffer)
2263 m->buffer = current_buffer;
2264 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2265 if (m->buffer == current_buffer)
2266 m->buffer = other_buffer;
2268 if (current_buffer->text->intervals)
2269 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2270 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2271 if (other_buffer->text->intervals)
2272 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2273 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2275 return Qnil;
2278 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2279 1, 1, 0,
2280 doc: /* Set the multibyte flag of the current buffer to FLAG.
2281 If FLAG is t, this makes the buffer a multibyte buffer.
2282 If FLAG is nil, this makes the buffer a single-byte buffer.
2283 In these cases, the buffer contents remain unchanged as a sequence of
2284 bytes but the contents viewed as characters do change.
2285 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2286 all eight-bit bytes to eight-bit characters.
2287 If the multibyte flag was really changed, undo information of the
2288 current buffer is cleared. */)
2289 (flag)
2290 Lisp_Object flag;
2292 struct Lisp_Marker *tail, *markers;
2293 struct buffer *other;
2294 int begv, zv;
2295 int narrowed = (BEG != BEGV || Z != ZV);
2296 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2297 Lisp_Object old_undo = current_buffer->undo_list;
2298 struct gcpro gcpro1;
2300 if (current_buffer->base_buffer)
2301 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2303 /* Do nothing if nothing actually changes. */
2304 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2305 return flag;
2307 GCPRO1 (old_undo);
2309 /* Don't record these buffer changes. We will put a special undo entry
2310 instead. */
2311 current_buffer->undo_list = Qt;
2313 /* If the cached position is for this buffer, clear it out. */
2314 clear_charpos_cache (current_buffer);
2316 if (NILP (flag))
2317 begv = BEGV_BYTE, zv = ZV_BYTE;
2318 else
2319 begv = BEGV, zv = ZV;
2321 if (narrowed)
2322 Fwiden ();
2324 if (NILP (flag))
2326 int pos, stop;
2327 unsigned char *p;
2329 /* Do this first, so it can use CHAR_TO_BYTE
2330 to calculate the old correspondences. */
2331 set_intervals_multibyte (0);
2333 current_buffer->enable_multibyte_characters = Qnil;
2335 Z = Z_BYTE;
2336 BEGV = BEGV_BYTE;
2337 ZV = ZV_BYTE;
2338 GPT = GPT_BYTE;
2339 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2342 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2343 tail->charpos = tail->bytepos;
2345 /* Convert multibyte form of 8-bit characters to unibyte. */
2346 pos = BEG;
2347 stop = GPT;
2348 p = BEG_ADDR;
2349 while (1)
2351 int c, bytes;
2353 if (pos == stop)
2355 if (pos == Z)
2356 break;
2357 p = GAP_END_ADDR;
2358 stop = Z;
2360 if (ASCII_BYTE_P (*p))
2361 p++, pos++;
2362 else if (CHAR_BYTE8_HEAD_P (*p))
2364 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
2365 /* Delete all bytes for this 8-bit character but the
2366 last one, and change the last one to the charcter
2367 code. */
2368 bytes--;
2369 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2370 p = GAP_END_ADDR;
2371 *p++ = c;
2372 pos++;
2373 if (begv > pos)
2374 begv -= bytes;
2375 if (zv > pos)
2376 zv -= bytes;
2377 stop = Z;
2379 else
2381 bytes = BYTES_BY_CHAR_HEAD (*p);
2382 p += bytes, pos += bytes;
2385 if (narrowed)
2386 Fnarrow_to_region (make_number (begv), make_number (zv));
2388 else
2390 int pt = PT;
2391 int pos, stop;
2392 unsigned char *p, *pend;
2394 /* Be sure not to have a multibyte sequence striding over the GAP.
2395 Ex: We change this: "...abc\302 _GAP_ \241def..."
2396 to: "...abc _GAP_ \302\241def..." */
2398 if (EQ (flag, Qt)
2399 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2400 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2402 unsigned char *p = GPT_ADDR - 1;
2404 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2405 if (BASE_LEADING_CODE_P (*p))
2407 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2409 move_gap_both (new_gpt, new_gpt);
2413 /* Make the buffer contents valid as multibyte by converting
2414 8-bit characters to multibyte form. */
2415 pos = BEG;
2416 stop = GPT;
2417 p = BEG_ADDR;
2418 pend = GPT_ADDR;
2419 while (1)
2421 int bytes;
2423 if (pos == stop)
2425 if (pos == Z)
2426 break;
2427 p = GAP_END_ADDR;
2428 pend = Z_ADDR;
2429 stop = Z;
2432 if (ASCII_BYTE_P (*p))
2433 p++, pos++;
2434 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2435 p += bytes, pos += bytes;
2436 else
2438 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2439 int c;
2441 c = BYTE8_TO_CHAR (*p);
2442 bytes = CHAR_STRING (c, tmp);
2443 *p = tmp[0];
2444 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2445 bytes--;
2446 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2447 /* Now the gap is after the just inserted data. */
2448 pos = GPT;
2449 p = GAP_END_ADDR;
2450 if (pos <= begv)
2451 begv += bytes;
2452 if (pos <= zv)
2453 zv += bytes;
2454 if (pos <= pt)
2455 pt += bytes;
2456 pend = Z_ADDR;
2457 stop = Z;
2461 if (pt != PT)
2462 TEMP_SET_PT (pt);
2464 if (narrowed)
2465 Fnarrow_to_region (make_number (begv), make_number (zv));
2467 /* Do this first, so that chars_in_text asks the right question.
2468 set_intervals_multibyte needs it too. */
2469 current_buffer->enable_multibyte_characters = Qt;
2471 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2472 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2474 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2476 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2477 if (BEGV_BYTE > GPT_BYTE)
2478 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2479 else
2480 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2482 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2483 if (ZV_BYTE > GPT_BYTE)
2484 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2485 else
2486 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2489 int pt_byte = advance_to_char_boundary (PT_BYTE);
2490 int pt;
2492 if (pt_byte > GPT_BYTE)
2493 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2494 else
2495 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2496 TEMP_SET_PT_BOTH (pt, pt_byte);
2499 tail = markers = BUF_MARKERS (current_buffer);
2501 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2502 getting confused by the markers that have not yet been updated.
2503 It is also a signal that it should never create a marker. */
2504 BUF_MARKERS (current_buffer) = NULL;
2506 for (; tail; tail = tail->next)
2508 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2509 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2512 /* Make sure no markers were put on the chain
2513 while the chain value was incorrect. */
2514 if (BUF_MARKERS (current_buffer))
2515 abort ();
2517 BUF_MARKERS (current_buffer) = markers;
2519 /* Do this last, so it can calculate the new correspondences
2520 between chars and bytes. */
2521 set_intervals_multibyte (1);
2524 if (!EQ (old_undo, Qt))
2526 /* Represent all the above changes by a special undo entry. */
2527 extern Lisp_Object Qapply;
2528 current_buffer->undo_list = Fcons (list3 (Qapply,
2529 intern ("set-buffer-multibyte"),
2530 NILP (flag) ? Qt : Qnil),
2531 old_undo);
2534 UNGCPRO;
2536 /* Changing the multibyteness of a buffer means that all windows
2537 showing that buffer must be updated thoroughly. */
2538 current_buffer->prevent_redisplay_optimizations_p = 1;
2539 ++windows_or_buffers_changed;
2541 /* Copy this buffer's new multibyte status
2542 into all of its indirect buffers. */
2543 for (other = all_buffers; other; other = other->next)
2544 if (other->base_buffer == current_buffer && !NILP (other->name))
2546 other->enable_multibyte_characters
2547 = current_buffer->enable_multibyte_characters;
2548 other->prevent_redisplay_optimizations_p = 1;
2551 /* Restore the modifiedness of the buffer. */
2552 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2553 Fset_buffer_modified_p (Qnil);
2555 #ifdef subprocesses
2556 /* Update coding systems of this buffer's process (if any). */
2558 Lisp_Object process;
2560 process = Fget_buffer_process (Fcurrent_buffer ());
2561 if (PROCESSP (process))
2562 setup_process_coding_systems (process);
2564 #endif /* subprocesses */
2566 return flag;
2569 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2570 0, 0, 0,
2571 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2572 Most local variable bindings are eliminated so that the default values
2573 become effective once more. Also, the syntax table is set from
2574 `standard-syntax-table', the local keymap is set to nil,
2575 and the abbrev table from `fundamental-mode-abbrev-table'.
2576 This function also forces redisplay of the mode line.
2578 Every function to select a new major mode starts by
2579 calling this function.
2581 As a special exception, local variables whose names have
2582 a non-nil `permanent-local' property are not eliminated by this function.
2584 The first thing this function does is run
2585 the normal hook `change-major-mode-hook'. */)
2588 if (!NILP (Vrun_hooks))
2589 call1 (Vrun_hooks, Qchange_major_mode_hook);
2591 /* Make sure none of the bindings in local_var_alist
2592 remain swapped in, in their symbols. */
2594 swap_out_buffer_local_variables (current_buffer);
2596 /* Actually eliminate all local bindings of this buffer. */
2598 reset_buffer_local_variables (current_buffer, 0);
2600 /* Force mode-line redisplay. Useful here because all major mode
2601 commands call this function. */
2602 update_mode_lines++;
2604 return Qnil;
2607 /* Make sure no local variables remain set up with buffer B
2608 for their current values. */
2610 static void
2611 swap_out_buffer_local_variables (b)
2612 struct buffer *b;
2614 Lisp_Object oalist, alist, sym, tem, buffer;
2616 XSETBUFFER (buffer, b);
2617 oalist = b->local_var_alist;
2619 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2621 sym = XCAR (XCAR (alist));
2623 /* Need not do anything if some other buffer's binding is now encached. */
2624 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2625 if (EQ (tem, buffer))
2627 /* Symbol is set up for this buffer's old local value:
2628 swap it out! */
2629 swap_in_global_binding (sym);
2634 /* Find all the overlays in the current buffer that contain position POS.
2635 Return the number found, and store them in a vector in *VEC_PTR.
2636 Store in *LEN_PTR the size allocated for the vector.
2637 Store in *NEXT_PTR the next position after POS where an overlay starts,
2638 or ZV if there are no more overlays between POS and ZV.
2639 Store in *PREV_PTR the previous position before POS where an overlay ends,
2640 or where an overlay starts which ends at or after POS;
2641 or BEGV if there are no such overlays from BEGV to POS.
2642 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2644 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2645 when this function is called.
2647 If EXTEND is non-zero, we make the vector bigger if necessary.
2648 If EXTEND is zero, we never extend the vector,
2649 and we store only as many overlays as will fit.
2650 But we still return the total number of overlays.
2652 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2653 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2654 default (BEGV or ZV). */
2657 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2658 EMACS_INT pos;
2659 int extend;
2660 Lisp_Object **vec_ptr;
2661 int *len_ptr;
2662 EMACS_INT *next_ptr;
2663 EMACS_INT *prev_ptr;
2664 int change_req;
2666 Lisp_Object overlay, start, end;
2667 struct Lisp_Overlay *tail;
2668 int idx = 0;
2669 int len = *len_ptr;
2670 Lisp_Object *vec = *vec_ptr;
2671 int next = ZV;
2672 int prev = BEGV;
2673 int inhibit_storing = 0;
2675 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2677 int startpos, endpos;
2679 XSETMISC (overlay, tail);
2681 start = OVERLAY_START (overlay);
2682 end = OVERLAY_END (overlay);
2683 endpos = OVERLAY_POSITION (end);
2684 if (endpos < pos)
2686 if (prev < endpos)
2687 prev = endpos;
2688 break;
2690 startpos = OVERLAY_POSITION (start);
2691 /* This one ends at or after POS
2692 so its start counts for PREV_PTR if it's before POS. */
2693 if (prev < startpos && startpos < pos)
2694 prev = startpos;
2695 if (endpos == pos)
2696 continue;
2697 if (startpos <= pos)
2699 if (idx == len)
2701 /* The supplied vector is full.
2702 Either make it bigger, or don't store any more in it. */
2703 if (extend)
2705 /* Make it work with an initial len == 0. */
2706 len *= 2;
2707 if (len == 0)
2708 len = 4;
2709 *len_ptr = len;
2710 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2711 *vec_ptr = vec;
2713 else
2714 inhibit_storing = 1;
2717 if (!inhibit_storing)
2718 vec[idx] = overlay;
2719 /* Keep counting overlays even if we can't return them all. */
2720 idx++;
2722 else if (startpos < next)
2723 next = startpos;
2726 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2728 int startpos, endpos;
2730 XSETMISC (overlay, tail);
2732 start = OVERLAY_START (overlay);
2733 end = OVERLAY_END (overlay);
2734 startpos = OVERLAY_POSITION (start);
2735 if (pos < startpos)
2737 if (startpos < next)
2738 next = startpos;
2739 break;
2741 endpos = OVERLAY_POSITION (end);
2742 if (pos < endpos)
2744 if (idx == len)
2746 if (extend)
2748 /* Make it work with an initial len == 0. */
2749 len *= 2;
2750 if (len == 0)
2751 len = 4;
2752 *len_ptr = len;
2753 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2754 *vec_ptr = vec;
2756 else
2757 inhibit_storing = 1;
2760 if (!inhibit_storing)
2761 vec[idx] = overlay;
2762 idx++;
2764 if (startpos < pos && startpos > prev)
2765 prev = startpos;
2767 else if (endpos < pos && endpos > prev)
2768 prev = endpos;
2769 else if (endpos == pos && startpos > prev
2770 && (!change_req || startpos < pos))
2771 prev = startpos;
2774 if (next_ptr)
2775 *next_ptr = next;
2776 if (prev_ptr)
2777 *prev_ptr = prev;
2778 return idx;
2781 /* Find all the overlays in the current buffer that overlap the range
2782 BEG-END, or are empty at BEG, or are empty at END provided END
2783 denotes the position at the end of the current buffer.
2785 Return the number found, and store them in a vector in *VEC_PTR.
2786 Store in *LEN_PTR the size allocated for the vector.
2787 Store in *NEXT_PTR the next position after POS where an overlay starts,
2788 or ZV if there are no more overlays.
2789 Store in *PREV_PTR the previous position before POS where an overlay ends,
2790 or BEGV if there are no previous overlays.
2791 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2793 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2794 when this function is called.
2796 If EXTEND is non-zero, we make the vector bigger if necessary.
2797 If EXTEND is zero, we never extend the vector,
2798 and we store only as many overlays as will fit.
2799 But we still return the total number of overlays. */
2801 static int
2802 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2803 int beg, end;
2804 int extend;
2805 Lisp_Object **vec_ptr;
2806 int *len_ptr;
2807 int *next_ptr;
2808 int *prev_ptr;
2810 Lisp_Object overlay, ostart, oend;
2811 struct Lisp_Overlay *tail;
2812 int idx = 0;
2813 int len = *len_ptr;
2814 Lisp_Object *vec = *vec_ptr;
2815 int next = ZV;
2816 int prev = BEGV;
2817 int inhibit_storing = 0;
2818 int end_is_Z = end == Z;
2820 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2822 int startpos, endpos;
2824 XSETMISC (overlay, tail);
2826 ostart = OVERLAY_START (overlay);
2827 oend = OVERLAY_END (overlay);
2828 endpos = OVERLAY_POSITION (oend);
2829 if (endpos < beg)
2831 if (prev < endpos)
2832 prev = endpos;
2833 break;
2835 startpos = OVERLAY_POSITION (ostart);
2836 /* Count an interval if it overlaps the range, is empty at the
2837 start of the range, or is empty at END provided END denotes the
2838 end of the buffer. */
2839 if ((beg < endpos && startpos < end)
2840 || (startpos == endpos
2841 && (beg == endpos || (end_is_Z && endpos == end))))
2843 if (idx == len)
2845 /* The supplied vector is full.
2846 Either make it bigger, or don't store any more in it. */
2847 if (extend)
2849 /* Make it work with an initial len == 0. */
2850 len *= 2;
2851 if (len == 0)
2852 len = 4;
2853 *len_ptr = len;
2854 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2855 *vec_ptr = vec;
2857 else
2858 inhibit_storing = 1;
2861 if (!inhibit_storing)
2862 vec[idx] = overlay;
2863 /* Keep counting overlays even if we can't return them all. */
2864 idx++;
2866 else if (startpos < next)
2867 next = startpos;
2870 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2872 int startpos, endpos;
2874 XSETMISC (overlay, tail);
2876 ostart = OVERLAY_START (overlay);
2877 oend = OVERLAY_END (overlay);
2878 startpos = OVERLAY_POSITION (ostart);
2879 if (end < startpos)
2881 if (startpos < next)
2882 next = startpos;
2883 break;
2885 endpos = OVERLAY_POSITION (oend);
2886 /* Count an interval if it overlaps the range, is empty at the
2887 start of the range, or is empty at END provided END denotes the
2888 end of the buffer. */
2889 if ((beg < endpos && startpos < end)
2890 || (startpos == endpos
2891 && (beg == endpos || (end_is_Z && endpos == end))))
2893 if (idx == len)
2895 if (extend)
2897 /* Make it work with an initial len == 0. */
2898 len *= 2;
2899 if (len == 0)
2900 len = 4;
2901 *len_ptr = len;
2902 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2903 *vec_ptr = vec;
2905 else
2906 inhibit_storing = 1;
2909 if (!inhibit_storing)
2910 vec[idx] = overlay;
2911 idx++;
2913 else if (endpos < beg && endpos > prev)
2914 prev = endpos;
2917 if (next_ptr)
2918 *next_ptr = next;
2919 if (prev_ptr)
2920 *prev_ptr = prev;
2921 return idx;
2925 /* Return non-zero if there exists an overlay with a non-nil
2926 `mouse-face' property overlapping OVERLAY. */
2929 mouse_face_overlay_overlaps (overlay)
2930 Lisp_Object overlay;
2932 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2933 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2934 int n, i, size;
2935 Lisp_Object *v, tem;
2937 size = 10;
2938 v = (Lisp_Object *) alloca (size * sizeof *v);
2939 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2940 if (n > size)
2942 v = (Lisp_Object *) alloca (n * sizeof *v);
2943 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2946 for (i = 0; i < n; ++i)
2947 if (!EQ (v[i], overlay)
2948 && (tem = Foverlay_get (overlay, Qmouse_face),
2949 !NILP (tem)))
2950 break;
2952 return i < n;
2957 /* Fast function to just test if we're at an overlay boundary. */
2959 overlay_touches_p (pos)
2960 int pos;
2962 Lisp_Object overlay;
2963 struct Lisp_Overlay *tail;
2965 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2967 int endpos;
2969 XSETMISC (overlay ,tail);
2970 if (!OVERLAYP (overlay))
2971 abort ();
2973 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2974 if (endpos < pos)
2975 break;
2976 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2977 return 1;
2980 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2982 int startpos;
2984 XSETMISC (overlay, tail);
2985 if (!OVERLAYP (overlay))
2986 abort ();
2988 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2989 if (pos < startpos)
2990 break;
2991 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2992 return 1;
2994 return 0;
2997 struct sortvec
2999 Lisp_Object overlay;
3000 int beg, end;
3001 int priority;
3004 static int
3005 compare_overlays (v1, v2)
3006 const void *v1, *v2;
3008 const struct sortvec *s1 = (const struct sortvec *) v1;
3009 const struct sortvec *s2 = (const struct sortvec *) v2;
3010 if (s1->priority != s2->priority)
3011 return s1->priority - s2->priority;
3012 if (s1->beg != s2->beg)
3013 return s1->beg - s2->beg;
3014 if (s1->end != s2->end)
3015 return s2->end - s1->end;
3016 return 0;
3019 /* Sort an array of overlays by priority. The array is modified in place.
3020 The return value is the new size; this may be smaller than the original
3021 size if some of the overlays were invalid or were window-specific. */
3023 sort_overlays (overlay_vec, noverlays, w)
3024 Lisp_Object *overlay_vec;
3025 int noverlays;
3026 struct window *w;
3028 int i, j;
3029 struct sortvec *sortvec;
3030 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3032 /* Put the valid and relevant overlays into sortvec. */
3034 for (i = 0, j = 0; i < noverlays; i++)
3036 Lisp_Object tem;
3037 Lisp_Object overlay;
3039 overlay = overlay_vec[i];
3040 if (OVERLAY_VALID (overlay)
3041 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3042 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3044 /* If we're interested in a specific window, then ignore
3045 overlays that are limited to some other window. */
3046 if (w)
3048 Lisp_Object window;
3050 window = Foverlay_get (overlay, Qwindow);
3051 if (WINDOWP (window) && XWINDOW (window) != w)
3052 continue;
3055 /* This overlay is good and counts: put it into sortvec. */
3056 sortvec[j].overlay = overlay;
3057 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3058 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3059 tem = Foverlay_get (overlay, Qpriority);
3060 if (INTEGERP (tem))
3061 sortvec[j].priority = XINT (tem);
3062 else
3063 sortvec[j].priority = 0;
3064 j++;
3067 noverlays = j;
3069 /* Sort the overlays into the proper order: increasing priority. */
3071 if (noverlays > 1)
3072 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3074 for (i = 0; i < noverlays; i++)
3075 overlay_vec[i] = sortvec[i].overlay;
3076 return (noverlays);
3079 struct sortstr
3081 Lisp_Object string, string2;
3082 int size;
3083 int priority;
3086 struct sortstrlist
3088 struct sortstr *buf; /* An array that expands as needed; never freed. */
3089 int size; /* Allocated length of that array. */
3090 int used; /* How much of the array is currently in use. */
3091 int bytes; /* Total length of the strings in buf. */
3094 /* Buffers for storing information about the overlays touching a given
3095 position. These could be automatic variables in overlay_strings, but
3096 it's more efficient to hold onto the memory instead of repeatedly
3097 allocating and freeing it. */
3098 static struct sortstrlist overlay_heads, overlay_tails;
3099 static unsigned char *overlay_str_buf;
3101 /* Allocated length of overlay_str_buf. */
3102 static int overlay_str_len;
3104 /* A comparison function suitable for passing to qsort. */
3105 static int
3106 cmp_for_strings (as1, as2)
3107 char *as1, *as2;
3109 struct sortstr *s1 = (struct sortstr *)as1;
3110 struct sortstr *s2 = (struct sortstr *)as2;
3111 if (s1->size != s2->size)
3112 return s2->size - s1->size;
3113 if (s1->priority != s2->priority)
3114 return s1->priority - s2->priority;
3115 return 0;
3118 static void
3119 record_overlay_string (ssl, str, str2, pri, size)
3120 struct sortstrlist *ssl;
3121 Lisp_Object str, str2, pri;
3122 int size;
3124 int nbytes;
3126 if (ssl->used == ssl->size)
3128 if (ssl->buf)
3129 ssl->size *= 2;
3130 else
3131 ssl->size = 5;
3132 ssl->buf = ((struct sortstr *)
3133 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3135 ssl->buf[ssl->used].string = str;
3136 ssl->buf[ssl->used].string2 = str2;
3137 ssl->buf[ssl->used].size = size;
3138 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3139 ssl->used++;
3141 if (NILP (current_buffer->enable_multibyte_characters))
3142 nbytes = SCHARS (str);
3143 else if (! STRING_MULTIBYTE (str))
3144 nbytes = count_size_as_multibyte (SDATA (str),
3145 SBYTES (str));
3146 else
3147 nbytes = SBYTES (str);
3149 ssl->bytes += nbytes;
3151 if (STRINGP (str2))
3153 if (NILP (current_buffer->enable_multibyte_characters))
3154 nbytes = SCHARS (str2);
3155 else if (! STRING_MULTIBYTE (str2))
3156 nbytes = count_size_as_multibyte (SDATA (str2),
3157 SBYTES (str2));
3158 else
3159 nbytes = SBYTES (str2);
3161 ssl->bytes += nbytes;
3165 /* Return the concatenation of the strings associated with overlays that
3166 begin or end at POS, ignoring overlays that are specific to a window
3167 other than W. The strings are concatenated in the appropriate order:
3168 shorter overlays nest inside longer ones, and higher priority inside
3169 lower. Normally all of the after-strings come first, but zero-sized
3170 overlays have their after-strings ride along with the before-strings
3171 because it would look strange to print them inside-out.
3173 Returns the string length, and stores the contents indirectly through
3174 PSTR, if that variable is non-null. The string may be overwritten by
3175 subsequent calls. */
3178 overlay_strings (pos, w, pstr)
3179 EMACS_INT pos;
3180 struct window *w;
3181 unsigned char **pstr;
3183 Lisp_Object overlay, window, str;
3184 struct Lisp_Overlay *ov;
3185 int startpos, endpos;
3186 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3188 overlay_heads.used = overlay_heads.bytes = 0;
3189 overlay_tails.used = overlay_tails.bytes = 0;
3190 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3192 XSETMISC (overlay, ov);
3193 eassert (OVERLAYP (overlay));
3195 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3196 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3197 if (endpos < pos)
3198 break;
3199 if (endpos != pos && startpos != pos)
3200 continue;
3201 window = Foverlay_get (overlay, Qwindow);
3202 if (WINDOWP (window) && XWINDOW (window) != w)
3203 continue;
3204 if (startpos == pos
3205 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3206 record_overlay_string (&overlay_heads, str,
3207 (startpos == endpos
3208 ? Foverlay_get (overlay, Qafter_string)
3209 : Qnil),
3210 Foverlay_get (overlay, Qpriority),
3211 endpos - startpos);
3212 else if (endpos == pos
3213 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3214 record_overlay_string (&overlay_tails, str, Qnil,
3215 Foverlay_get (overlay, Qpriority),
3216 endpos - startpos);
3218 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3220 XSETMISC (overlay, ov);
3221 eassert (OVERLAYP (overlay));
3223 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3224 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3225 if (startpos > pos)
3226 break;
3227 if (endpos != pos && startpos != pos)
3228 continue;
3229 window = Foverlay_get (overlay, Qwindow);
3230 if (WINDOWP (window) && XWINDOW (window) != w)
3231 continue;
3232 if (startpos == pos
3233 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3234 record_overlay_string (&overlay_heads, str,
3235 (startpos == endpos
3236 ? Foverlay_get (overlay, Qafter_string)
3237 : Qnil),
3238 Foverlay_get (overlay, Qpriority),
3239 endpos - startpos);
3240 else if (endpos == pos
3241 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3242 record_overlay_string (&overlay_tails, str, Qnil,
3243 Foverlay_get (overlay, Qpriority),
3244 endpos - startpos);
3246 if (overlay_tails.used > 1)
3247 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3248 cmp_for_strings);
3249 if (overlay_heads.used > 1)
3250 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3251 cmp_for_strings);
3252 if (overlay_heads.bytes || overlay_tails.bytes)
3254 Lisp_Object tem;
3255 int i;
3256 unsigned char *p;
3257 int total = overlay_heads.bytes + overlay_tails.bytes;
3259 if (total > overlay_str_len)
3261 overlay_str_len = total;
3262 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3263 total);
3265 p = overlay_str_buf;
3266 for (i = overlay_tails.used; --i >= 0;)
3268 int nbytes;
3269 tem = overlay_tails.buf[i].string;
3270 nbytes = copy_text (SDATA (tem), p,
3271 SBYTES (tem),
3272 STRING_MULTIBYTE (tem), multibyte);
3273 p += nbytes;
3275 for (i = 0; i < overlay_heads.used; ++i)
3277 int nbytes;
3278 tem = overlay_heads.buf[i].string;
3279 nbytes = copy_text (SDATA (tem), p,
3280 SBYTES (tem),
3281 STRING_MULTIBYTE (tem), multibyte);
3282 p += nbytes;
3283 tem = overlay_heads.buf[i].string2;
3284 if (STRINGP (tem))
3286 nbytes = copy_text (SDATA (tem), p,
3287 SBYTES (tem),
3288 STRING_MULTIBYTE (tem), multibyte);
3289 p += nbytes;
3292 if (p != overlay_str_buf + total)
3293 abort ();
3294 if (pstr)
3295 *pstr = overlay_str_buf;
3296 return total;
3298 return 0;
3301 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3303 void
3304 recenter_overlay_lists (buf, pos)
3305 struct buffer *buf;
3306 EMACS_INT pos;
3308 Lisp_Object overlay, beg, end;
3309 struct Lisp_Overlay *prev, *tail, *next;
3311 /* See if anything in overlays_before should move to overlays_after. */
3313 /* We don't strictly need prev in this loop; it should always be nil.
3314 But we use it for symmetry and in case that should cease to be true
3315 with some future change. */
3316 prev = NULL;
3317 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3319 next = tail->next;
3320 XSETMISC (overlay, tail);
3322 /* If the overlay is not valid, get rid of it. */
3323 if (!OVERLAY_VALID (overlay))
3324 #if 1
3325 abort ();
3326 #else
3328 /* Splice the cons cell TAIL out of overlays_before. */
3329 if (!NILP (prev))
3330 XCDR (prev) = next;
3331 else
3332 buf->overlays_before = next;
3333 tail = prev;
3334 continue;
3336 #endif
3338 beg = OVERLAY_START (overlay);
3339 end = OVERLAY_END (overlay);
3341 if (OVERLAY_POSITION (end) > pos)
3343 /* OVERLAY needs to be moved. */
3344 int where = OVERLAY_POSITION (beg);
3345 struct Lisp_Overlay *other, *other_prev;
3347 /* Splice the cons cell TAIL out of overlays_before. */
3348 if (prev)
3349 prev->next = next;
3350 else
3351 buf->overlays_before = next;
3353 /* Search thru overlays_after for where to put it. */
3354 other_prev = NULL;
3355 for (other = buf->overlays_after; other;
3356 other_prev = other, other = other->next)
3358 Lisp_Object otherbeg, otheroverlay;
3360 XSETMISC (otheroverlay, other);
3361 eassert (OVERLAY_VALID (otheroverlay));
3363 otherbeg = OVERLAY_START (otheroverlay);
3364 if (OVERLAY_POSITION (otherbeg) >= where)
3365 break;
3368 /* Add TAIL to overlays_after before OTHER. */
3369 tail->next = other;
3370 if (other_prev)
3371 other_prev->next = tail;
3372 else
3373 buf->overlays_after = tail;
3374 tail = prev;
3376 else
3377 /* We've reached the things that should stay in overlays_before.
3378 All the rest of overlays_before must end even earlier,
3379 so stop now. */
3380 break;
3383 /* See if anything in overlays_after should be in overlays_before. */
3384 prev = NULL;
3385 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3387 next = tail->next;
3388 XSETMISC (overlay, tail);
3390 /* If the overlay is not valid, get rid of it. */
3391 if (!OVERLAY_VALID (overlay))
3392 #if 1
3393 abort ();
3394 #else
3396 /* Splice the cons cell TAIL out of overlays_after. */
3397 if (!NILP (prev))
3398 XCDR (prev) = next;
3399 else
3400 buf->overlays_after = next;
3401 tail = prev;
3402 continue;
3404 #endif
3406 beg = OVERLAY_START (overlay);
3407 end = OVERLAY_END (overlay);
3409 /* Stop looking, when we know that nothing further
3410 can possibly end before POS. */
3411 if (OVERLAY_POSITION (beg) > pos)
3412 break;
3414 if (OVERLAY_POSITION (end) <= pos)
3416 /* OVERLAY needs to be moved. */
3417 int where = OVERLAY_POSITION (end);
3418 struct Lisp_Overlay *other, *other_prev;
3420 /* Splice the cons cell TAIL out of overlays_after. */
3421 if (prev)
3422 prev->next = next;
3423 else
3424 buf->overlays_after = next;
3426 /* Search thru overlays_before for where to put it. */
3427 other_prev = NULL;
3428 for (other = buf->overlays_before; other;
3429 other_prev = other, other = other->next)
3431 Lisp_Object otherend, otheroverlay;
3433 XSETMISC (otheroverlay, other);
3434 eassert (OVERLAY_VALID (otheroverlay));
3436 otherend = OVERLAY_END (otheroverlay);
3437 if (OVERLAY_POSITION (otherend) <= where)
3438 break;
3441 /* Add TAIL to overlays_before before OTHER. */
3442 tail->next = other;
3443 if (other_prev)
3444 other_prev->next = tail;
3445 else
3446 buf->overlays_before = tail;
3447 tail = prev;
3451 buf->overlay_center = pos;
3454 void
3455 adjust_overlays_for_insert (pos, length)
3456 EMACS_INT pos;
3457 EMACS_INT length;
3459 /* After an insertion, the lists are still sorted properly,
3460 but we may need to update the value of the overlay center. */
3461 if (current_buffer->overlay_center >= pos)
3462 current_buffer->overlay_center += length;
3465 void
3466 adjust_overlays_for_delete (pos, length)
3467 EMACS_INT pos;
3468 EMACS_INT length;
3470 if (current_buffer->overlay_center < pos)
3471 /* The deletion was to our right. No change needed; the before- and
3472 after-lists are still consistent. */
3474 else if (current_buffer->overlay_center > pos + length)
3475 /* The deletion was to our left. We need to adjust the center value
3476 to account for the change in position, but the lists are consistent
3477 given the new value. */
3478 current_buffer->overlay_center -= length;
3479 else
3480 /* We're right in the middle. There might be things on the after-list
3481 that now belong on the before-list. Recentering will move them,
3482 and also update the center point. */
3483 recenter_overlay_lists (current_buffer, pos);
3486 /* Fix up overlays that were garbled as a result of permuting markers
3487 in the range START through END. Any overlay with at least one
3488 endpoint in this range will need to be unlinked from the overlay
3489 list and reinserted in its proper place.
3490 Such an overlay might even have negative size at this point.
3491 If so, we'll make the overlay empty. */
3492 void
3493 fix_start_end_in_overlays (start, end)
3494 register int start, end;
3496 Lisp_Object overlay;
3497 struct Lisp_Overlay *before_list, *after_list;
3498 /* These are either nil, indicating that before_list or after_list
3499 should be assigned, or the cons cell the cdr of which should be
3500 assigned. */
3501 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3502 /* 'Parent', likewise, indicates a cons cell or
3503 current_buffer->overlays_before or overlays_after, depending
3504 which loop we're in. */
3505 struct Lisp_Overlay *tail, *parent;
3506 int startpos, endpos;
3508 /* This algorithm shifts links around instead of consing and GCing.
3509 The loop invariant is that before_list (resp. after_list) is a
3510 well-formed list except that its last element, the CDR of beforep
3511 (resp. afterp) if beforep (afterp) isn't nil or before_list
3512 (after_list) if it is, is still uninitialized. So it's not a bug
3513 that before_list isn't initialized, although it may look
3514 strange. */
3515 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3517 XSETMISC (overlay, tail);
3519 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3520 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3522 /* If the overlay is backwards, make it empty. */
3523 if (endpos < startpos)
3525 startpos = endpos;
3526 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3527 Qnil);
3530 if (endpos < start)
3531 break;
3533 if (endpos < end
3534 || (startpos >= start && startpos < end))
3536 /* Add it to the end of the wrong list. Later on,
3537 recenter_overlay_lists will move it to the right place. */
3538 if (endpos < current_buffer->overlay_center)
3540 if (!afterp)
3541 after_list = tail;
3542 else
3543 afterp->next = tail;
3544 afterp = tail;
3546 else
3548 if (!beforep)
3549 before_list = tail;
3550 else
3551 beforep->next = tail;
3552 beforep = tail;
3554 if (!parent)
3555 current_buffer->overlays_before = tail->next;
3556 else
3557 parent->next = tail->next;
3558 tail = tail->next;
3560 else
3561 parent = tail, tail = parent->next;
3563 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3565 XSETMISC (overlay, tail);
3567 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3568 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3570 /* If the overlay is backwards, make it empty. */
3571 if (endpos < startpos)
3573 startpos = endpos;
3574 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3575 Qnil);
3578 if (startpos >= end)
3579 break;
3581 if (startpos >= start
3582 || (endpos >= start && endpos < end))
3584 if (endpos < current_buffer->overlay_center)
3586 if (!afterp)
3587 after_list = tail;
3588 else
3589 afterp->next = tail;
3590 afterp = tail;
3592 else
3594 if (!beforep)
3595 before_list = tail;
3596 else
3597 beforep->next = tail;
3598 beforep = tail;
3600 if (!parent)
3601 current_buffer->overlays_after = tail->next;
3602 else
3603 parent->next = tail->next;
3604 tail = tail->next;
3606 else
3607 parent = tail, tail = parent->next;
3610 /* Splice the constructed (wrong) lists into the buffer's lists,
3611 and let the recenter function make it sane again. */
3612 if (beforep)
3614 beforep->next = current_buffer->overlays_before;
3615 current_buffer->overlays_before = before_list;
3617 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3619 if (afterp)
3621 afterp->next = current_buffer->overlays_after;
3622 current_buffer->overlays_after = after_list;
3624 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3627 /* We have two types of overlay: the one whose ending marker is
3628 after-insertion-marker (this is the usual case) and the one whose
3629 ending marker is before-insertion-marker. When `overlays_before'
3630 contains overlays of the latter type and the former type in this
3631 order and both overlays end at inserting position, inserting a text
3632 increases only the ending marker of the latter type, which results
3633 in incorrect ordering of `overlays_before'.
3635 This function fixes ordering of overlays in the slot
3636 `overlays_before' of the buffer *BP. Before the insertion, `point'
3637 was at PREV, and now is at POS. */
3639 void
3640 fix_overlays_before (bp, prev, pos)
3641 struct buffer *bp;
3642 EMACS_INT prev, pos;
3644 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3645 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3646 Lisp_Object tem;
3647 EMACS_INT end;
3649 /* After the insertion, the several overlays may be in incorrect
3650 order. The possibility is that, in the list `overlays_before',
3651 an overlay which ends at POS appears after an overlay which ends
3652 at PREV. Since POS is greater than PREV, we must fix the
3653 ordering of these overlays, by moving overlays ends at POS before
3654 the overlays ends at PREV. */
3656 /* At first, find a place where disordered overlays should be linked
3657 in. It is where an overlay which end before POS exists. (i.e. an
3658 overlay whose ending marker is after-insertion-marker if disorder
3659 exists). */
3660 while (tail
3661 && (XSETMISC (tem, tail),
3662 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3664 parent = tail;
3665 tail = tail->next;
3668 /* If we don't find such an overlay,
3669 or the found one ends before PREV,
3670 or the found one is the last one in the list,
3671 we don't have to fix anything. */
3672 if (!tail || end < prev || !tail->next)
3673 return;
3675 right_pair = parent;
3676 parent = tail;
3677 tail = tail->next;
3679 /* Now, end position of overlays in the list TAIL should be before
3680 or equal to PREV. In the loop, an overlay which ends at POS is
3681 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3682 we found an overlay which ends before PREV, the remaining
3683 overlays are in correct order. */
3684 while (tail)
3686 XSETMISC (tem, tail);
3687 end = OVERLAY_POSITION (OVERLAY_END (tem));
3689 if (end == pos)
3690 { /* This overlay is disordered. */
3691 struct Lisp_Overlay *found = tail;
3693 /* Unlink the found overlay. */
3694 tail = found->next;
3695 parent->next = tail;
3696 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3697 and link it into the right place. */
3698 if (!right_pair)
3700 found->next = bp->overlays_before;
3701 bp->overlays_before = found;
3703 else
3705 found->next = right_pair->next;
3706 right_pair->next = found;
3709 else if (end == prev)
3711 parent = tail;
3712 tail = tail->next;
3714 else /* No more disordered overlay. */
3715 break;
3719 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3720 doc: /* Return t if OBJECT is an overlay. */)
3721 (object)
3722 Lisp_Object object;
3724 return (OVERLAYP (object) ? Qt : Qnil);
3727 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3728 doc: /* Create a new overlay with range BEG to END in BUFFER.
3729 If omitted, BUFFER defaults to the current buffer.
3730 BEG and END may be integers or markers.
3731 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3732 for the front of the overlay advance when text is inserted there
3733 \(which means the text *is not* included in the overlay).
3734 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3735 for the rear of the overlay advance when text is inserted there
3736 \(which means the text *is* included in the overlay). */)
3737 (beg, end, buffer, front_advance, rear_advance)
3738 Lisp_Object beg, end, buffer;
3739 Lisp_Object front_advance, rear_advance;
3741 Lisp_Object overlay;
3742 struct buffer *b;
3744 if (NILP (buffer))
3745 XSETBUFFER (buffer, current_buffer);
3746 else
3747 CHECK_BUFFER (buffer);
3748 if (MARKERP (beg)
3749 && ! EQ (Fmarker_buffer (beg), buffer))
3750 error ("Marker points into wrong buffer");
3751 if (MARKERP (end)
3752 && ! EQ (Fmarker_buffer (end), buffer))
3753 error ("Marker points into wrong buffer");
3755 CHECK_NUMBER_COERCE_MARKER (beg);
3756 CHECK_NUMBER_COERCE_MARKER (end);
3758 if (XINT (beg) > XINT (end))
3760 Lisp_Object temp;
3761 temp = beg; beg = end; end = temp;
3764 b = XBUFFER (buffer);
3766 beg = Fset_marker (Fmake_marker (), beg, buffer);
3767 end = Fset_marker (Fmake_marker (), end, buffer);
3769 if (!NILP (front_advance))
3770 XMARKER (beg)->insertion_type = 1;
3771 if (!NILP (rear_advance))
3772 XMARKER (end)->insertion_type = 1;
3774 overlay = allocate_misc ();
3775 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3776 XOVERLAY (overlay)->start = beg;
3777 XOVERLAY (overlay)->end = end;
3778 XOVERLAY (overlay)->plist = Qnil;
3779 XOVERLAY (overlay)->next = NULL;
3781 /* Put the new overlay on the wrong list. */
3782 end = OVERLAY_END (overlay);
3783 if (OVERLAY_POSITION (end) < b->overlay_center)
3785 if (b->overlays_after)
3786 XOVERLAY (overlay)->next = b->overlays_after;
3787 b->overlays_after = XOVERLAY (overlay);
3789 else
3791 if (b->overlays_before)
3792 XOVERLAY (overlay)->next = b->overlays_before;
3793 b->overlays_before = XOVERLAY (overlay);
3796 /* This puts it in the right list, and in the right order. */
3797 recenter_overlay_lists (b, b->overlay_center);
3799 /* We don't need to redisplay the region covered by the overlay, because
3800 the overlay has no properties at the moment. */
3802 return overlay;
3805 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3807 static void
3808 modify_overlay (buf, start, end)
3809 struct buffer *buf;
3810 EMACS_INT start, end;
3812 if (start > end)
3814 int temp = start;
3815 start = end;
3816 end = temp;
3819 BUF_COMPUTE_UNCHANGED (buf, start, end);
3821 /* If this is a buffer not in the selected window,
3822 we must do other windows. */
3823 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3824 windows_or_buffers_changed = 1;
3825 /* If multiple windows show this buffer, we must do other windows. */
3826 else if (buffer_shared > 1)
3827 windows_or_buffers_changed = 1;
3828 /* If we modify an overlay at the end of the buffer, we cannot
3829 be sure that window end is still valid. */
3830 else if (end >= ZV && start <= ZV)
3831 windows_or_buffers_changed = 1;
3833 ++BUF_OVERLAY_MODIFF (buf);
3837 Lisp_Object Fdelete_overlay ();
3839 static struct Lisp_Overlay *
3840 unchain_overlay (list, overlay)
3841 struct Lisp_Overlay *list, *overlay;
3843 struct Lisp_Overlay *tmp, *prev;
3844 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3845 if (tmp == overlay)
3847 if (prev)
3848 prev->next = tmp->next;
3849 else
3850 list = tmp->next;
3851 overlay->next = NULL;
3852 break;
3854 return list;
3857 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3858 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3859 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3860 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3861 buffer. */)
3862 (overlay, beg, end, buffer)
3863 Lisp_Object overlay, beg, end, buffer;
3865 struct buffer *b, *ob;
3866 Lisp_Object obuffer;
3867 int count = SPECPDL_INDEX ();
3869 CHECK_OVERLAY (overlay);
3870 if (NILP (buffer))
3871 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3872 if (NILP (buffer))
3873 XSETBUFFER (buffer, current_buffer);
3874 CHECK_BUFFER (buffer);
3876 if (MARKERP (beg)
3877 && ! EQ (Fmarker_buffer (beg), buffer))
3878 error ("Marker points into wrong buffer");
3879 if (MARKERP (end)
3880 && ! EQ (Fmarker_buffer (end), buffer))
3881 error ("Marker points into wrong buffer");
3883 CHECK_NUMBER_COERCE_MARKER (beg);
3884 CHECK_NUMBER_COERCE_MARKER (end);
3886 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3887 return Fdelete_overlay (overlay);
3889 if (XINT (beg) > XINT (end))
3891 Lisp_Object temp;
3892 temp = beg; beg = end; end = temp;
3895 specbind (Qinhibit_quit, Qt);
3897 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3898 b = XBUFFER (buffer);
3899 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3901 /* If the overlay has changed buffers, do a thorough redisplay. */
3902 if (!EQ (buffer, obuffer))
3904 /* Redisplay where the overlay was. */
3905 if (!NILP (obuffer))
3907 int o_beg;
3908 int o_end;
3910 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3911 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3913 modify_overlay (ob, o_beg, o_end);
3916 /* Redisplay where the overlay is going to be. */
3917 modify_overlay (b, XINT (beg), XINT (end));
3919 else
3920 /* Redisplay the area the overlay has just left, or just enclosed. */
3922 int o_beg, o_end;
3924 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3925 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3927 if (o_beg == XINT (beg))
3928 modify_overlay (b, o_end, XINT (end));
3929 else if (o_end == XINT (end))
3930 modify_overlay (b, o_beg, XINT (beg));
3931 else
3933 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3934 if (XINT (end) > o_end) o_end = XINT (end);
3935 modify_overlay (b, o_beg, o_end);
3939 if (!NILP (obuffer))
3941 ob->overlays_before
3942 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3943 ob->overlays_after
3944 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3945 eassert (XOVERLAY (overlay)->next == NULL);
3948 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3949 Fset_marker (OVERLAY_END (overlay), end, buffer);
3951 /* Put the overlay on the wrong list. */
3952 end = OVERLAY_END (overlay);
3953 if (OVERLAY_POSITION (end) < b->overlay_center)
3955 XOVERLAY (overlay)->next = b->overlays_after;
3956 b->overlays_after = XOVERLAY (overlay);
3958 else
3960 XOVERLAY (overlay)->next = b->overlays_before;
3961 b->overlays_before = XOVERLAY (overlay);
3964 /* This puts it in the right list, and in the right order. */
3965 recenter_overlay_lists (b, b->overlay_center);
3967 return unbind_to (count, overlay);
3970 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3971 doc: /* Delete the overlay OVERLAY from its buffer. */)
3972 (overlay)
3973 Lisp_Object overlay;
3975 Lisp_Object buffer;
3976 struct buffer *b;
3977 int count = SPECPDL_INDEX ();
3979 CHECK_OVERLAY (overlay);
3981 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3982 if (NILP (buffer))
3983 return Qnil;
3985 b = XBUFFER (buffer);
3986 specbind (Qinhibit_quit, Qt);
3988 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3989 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3990 eassert (XOVERLAY (overlay)->next == NULL);
3991 modify_overlay (b,
3992 marker_position (OVERLAY_START (overlay)),
3993 marker_position (OVERLAY_END (overlay)));
3994 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3995 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3997 /* When deleting an overlay with before or after strings, turn off
3998 display optimizations for the affected buffer, on the basis that
3999 these strings may contain newlines. This is easier to do than to
4000 check for that situation during redisplay. */
4001 if (!windows_or_buffers_changed
4002 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4003 || !NILP (Foverlay_get (overlay, Qafter_string))))
4004 b->prevent_redisplay_optimizations_p = 1;
4006 return unbind_to (count, Qnil);
4009 /* Overlay dissection functions. */
4011 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4012 doc: /* Return the position at which OVERLAY starts. */)
4013 (overlay)
4014 Lisp_Object overlay;
4016 CHECK_OVERLAY (overlay);
4018 return (Fmarker_position (OVERLAY_START (overlay)));
4021 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4022 doc: /* Return the position at which OVERLAY ends. */)
4023 (overlay)
4024 Lisp_Object overlay;
4026 CHECK_OVERLAY (overlay);
4028 return (Fmarker_position (OVERLAY_END (overlay)));
4031 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4032 doc: /* Return the buffer OVERLAY belongs to.
4033 Return nil if OVERLAY has been deleted. */)
4034 (overlay)
4035 Lisp_Object overlay;
4037 CHECK_OVERLAY (overlay);
4039 return Fmarker_buffer (OVERLAY_START (overlay));
4042 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4043 doc: /* Return a list of the properties on OVERLAY.
4044 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4045 OVERLAY. */)
4046 (overlay)
4047 Lisp_Object overlay;
4049 CHECK_OVERLAY (overlay);
4051 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4055 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4056 doc: /* Return a list of the overlays that contain the character at POS. */)
4057 (pos)
4058 Lisp_Object pos;
4060 int noverlays;
4061 Lisp_Object *overlay_vec;
4062 int len;
4063 Lisp_Object result;
4065 CHECK_NUMBER_COERCE_MARKER (pos);
4067 len = 10;
4068 /* We can't use alloca here because overlays_at can call xrealloc. */
4069 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4071 /* Put all the overlays we want in a vector in overlay_vec.
4072 Store the length in len. */
4073 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4074 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4076 /* Make a list of them all. */
4077 result = Flist (noverlays, overlay_vec);
4079 xfree (overlay_vec);
4080 return result;
4083 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4084 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4085 Overlap means that at least one character is contained within the overlay
4086 and also contained within the specified region.
4087 Empty overlays are included in the result if they are located at BEG,
4088 between BEG and END, or at END provided END denotes the position at the
4089 end of the buffer. */)
4090 (beg, end)
4091 Lisp_Object beg, end;
4093 int noverlays;
4094 Lisp_Object *overlay_vec;
4095 int len;
4096 Lisp_Object result;
4098 CHECK_NUMBER_COERCE_MARKER (beg);
4099 CHECK_NUMBER_COERCE_MARKER (end);
4101 len = 10;
4102 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4104 /* Put all the overlays we want in a vector in overlay_vec.
4105 Store the length in len. */
4106 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4107 (int *) 0, (int *) 0);
4109 /* Make a list of them all. */
4110 result = Flist (noverlays, overlay_vec);
4112 xfree (overlay_vec);
4113 return result;
4116 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4117 1, 1, 0,
4118 doc: /* Return the next position after POS where an overlay starts or ends.
4119 If there are no overlay boundaries from POS to (point-max),
4120 the value is (point-max). */)
4121 (pos)
4122 Lisp_Object pos;
4124 int noverlays;
4125 EMACS_INT endpos;
4126 Lisp_Object *overlay_vec;
4127 int len;
4128 int i;
4130 CHECK_NUMBER_COERCE_MARKER (pos);
4132 len = 10;
4133 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4135 /* Put all the overlays we want in a vector in overlay_vec.
4136 Store the length in len.
4137 endpos gets the position where the next overlay starts. */
4138 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4139 &endpos, (EMACS_INT *) 0, 1);
4141 /* If any of these overlays ends before endpos,
4142 use its ending point instead. */
4143 for (i = 0; i < noverlays; i++)
4145 Lisp_Object oend;
4146 EMACS_INT oendpos;
4148 oend = OVERLAY_END (overlay_vec[i]);
4149 oendpos = OVERLAY_POSITION (oend);
4150 if (oendpos < endpos)
4151 endpos = oendpos;
4154 xfree (overlay_vec);
4155 return make_number (endpos);
4158 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4159 Sprevious_overlay_change, 1, 1, 0,
4160 doc: /* Return the previous position before POS where an overlay starts or ends.
4161 If there are no overlay boundaries from (point-min) to POS,
4162 the value is (point-min). */)
4163 (pos)
4164 Lisp_Object pos;
4166 int noverlays;
4167 EMACS_INT prevpos;
4168 Lisp_Object *overlay_vec;
4169 int len;
4171 CHECK_NUMBER_COERCE_MARKER (pos);
4173 /* At beginning of buffer, we know the answer;
4174 avoid bug subtracting 1 below. */
4175 if (XINT (pos) == BEGV)
4176 return pos;
4178 len = 10;
4179 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4181 /* Put all the overlays we want in a vector in overlay_vec.
4182 Store the length in len.
4183 prevpos gets the position of the previous change. */
4184 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4185 (EMACS_INT *) 0, &prevpos, 1);
4187 xfree (overlay_vec);
4188 return make_number (prevpos);
4191 /* These functions are for debugging overlays. */
4193 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4194 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4195 The car has all the overlays before the overlay center;
4196 the cdr has all the overlays after the overlay center.
4197 Recentering overlays moves overlays between these lists.
4198 The lists you get are copies, so that changing them has no effect.
4199 However, the overlays you get are the real objects that the buffer uses. */)
4202 struct Lisp_Overlay *ol;
4203 Lisp_Object before = Qnil, after = Qnil, tmp;
4204 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4206 XSETMISC (tmp, ol);
4207 before = Fcons (tmp, before);
4209 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4211 XSETMISC (tmp, ol);
4212 after = Fcons (tmp, after);
4214 return Fcons (Fnreverse (before), Fnreverse (after));
4217 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4218 doc: /* Recenter the overlays of the current buffer around position POS.
4219 That makes overlay lookup faster for positions near POS (but perhaps slower
4220 for positions far away from POS). */)
4221 (pos)
4222 Lisp_Object pos;
4224 CHECK_NUMBER_COERCE_MARKER (pos);
4226 recenter_overlay_lists (current_buffer, XINT (pos));
4227 return Qnil;
4230 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4231 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4232 (overlay, prop)
4233 Lisp_Object overlay, prop;
4235 CHECK_OVERLAY (overlay);
4236 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4239 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4240 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4241 (overlay, prop, value)
4242 Lisp_Object overlay, prop, value;
4244 Lisp_Object tail, buffer;
4245 int changed;
4247 CHECK_OVERLAY (overlay);
4249 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4251 for (tail = XOVERLAY (overlay)->plist;
4252 CONSP (tail) && CONSP (XCDR (tail));
4253 tail = XCDR (XCDR (tail)))
4254 if (EQ (XCAR (tail), prop))
4256 changed = !EQ (XCAR (XCDR (tail)), value);
4257 XSETCAR (XCDR (tail), value);
4258 goto found;
4260 /* It wasn't in the list, so add it to the front. */
4261 changed = !NILP (value);
4262 XOVERLAY (overlay)->plist
4263 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4264 found:
4265 if (! NILP (buffer))
4267 if (changed)
4268 modify_overlay (XBUFFER (buffer),
4269 marker_position (OVERLAY_START (overlay)),
4270 marker_position (OVERLAY_END (overlay)));
4271 if (EQ (prop, Qevaporate) && ! NILP (value)
4272 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4273 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4274 Fdelete_overlay (overlay);
4277 return value;
4280 /* Subroutine of report_overlay_modification. */
4282 /* Lisp vector holding overlay hook functions to call.
4283 Vector elements come in pairs.
4284 Each even-index element is a list of hook functions.
4285 The following odd-index element is the overlay they came from.
4287 Before the buffer change, we fill in this vector
4288 as we call overlay hook functions.
4289 After the buffer change, we get the functions to call from this vector.
4290 This way we always call the same functions before and after the change. */
4291 static Lisp_Object last_overlay_modification_hooks;
4293 /* Number of elements actually used in last_overlay_modification_hooks. */
4294 static int last_overlay_modification_hooks_used;
4296 /* Add one functionlist/overlay pair
4297 to the end of last_overlay_modification_hooks. */
4299 static void
4300 add_overlay_mod_hooklist (functionlist, overlay)
4301 Lisp_Object functionlist, overlay;
4303 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4305 if (last_overlay_modification_hooks_used == oldsize)
4306 last_overlay_modification_hooks = larger_vector
4307 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4308 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4309 functionlist); last_overlay_modification_hooks_used++;
4310 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4311 overlay); last_overlay_modification_hooks_used++;
4314 /* Run the modification-hooks of overlays that include
4315 any part of the text in START to END.
4316 If this change is an insertion, also
4317 run the insert-before-hooks of overlay starting at END,
4318 and the insert-after-hooks of overlay ending at START.
4320 This is called both before and after the modification.
4321 AFTER is nonzero when we call after the modification.
4323 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4324 When AFTER is nonzero, they are the start position,
4325 the position after the inserted new text,
4326 and the length of deleted or replaced old text. */
4328 void
4329 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4330 Lisp_Object start, end;
4331 int after;
4332 Lisp_Object arg1, arg2, arg3;
4334 Lisp_Object prop, overlay;
4335 struct Lisp_Overlay *tail;
4336 /* 1 if this change is an insertion. */
4337 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4338 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4340 overlay = Qnil;
4341 tail = NULL;
4343 /* We used to run the functions as soon as we found them and only register
4344 them in last_overlay_modification_hooks for the purpose of the `after'
4345 case. But running elisp code as we traverse the list of overlays is
4346 painful because the list can be modified by the elisp code so we had to
4347 copy at several places. We now simply do a read-only traversal that
4348 only collects the functions to run and we run them afterwards. It's
4349 simpler, especially since all the code was already there. -stef */
4351 if (!after)
4353 /* We are being called before a change.
4354 Scan the overlays to find the functions to call. */
4355 last_overlay_modification_hooks_used = 0;
4356 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4358 int startpos, endpos;
4359 Lisp_Object ostart, oend;
4361 XSETMISC (overlay, tail);
4363 ostart = OVERLAY_START (overlay);
4364 oend = OVERLAY_END (overlay);
4365 endpos = OVERLAY_POSITION (oend);
4366 if (XFASTINT (start) > endpos)
4367 break;
4368 startpos = OVERLAY_POSITION (ostart);
4369 if (insertion && (XFASTINT (start) == startpos
4370 || XFASTINT (end) == startpos))
4372 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4373 if (!NILP (prop))
4374 add_overlay_mod_hooklist (prop, overlay);
4376 if (insertion && (XFASTINT (start) == endpos
4377 || XFASTINT (end) == endpos))
4379 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4380 if (!NILP (prop))
4381 add_overlay_mod_hooklist (prop, overlay);
4383 /* Test for intersecting intervals. This does the right thing
4384 for both insertion and deletion. */
4385 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4387 prop = Foverlay_get (overlay, Qmodification_hooks);
4388 if (!NILP (prop))
4389 add_overlay_mod_hooklist (prop, overlay);
4393 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4395 int startpos, endpos;
4396 Lisp_Object ostart, oend;
4398 XSETMISC (overlay, tail);
4400 ostart = OVERLAY_START (overlay);
4401 oend = OVERLAY_END (overlay);
4402 startpos = OVERLAY_POSITION (ostart);
4403 endpos = OVERLAY_POSITION (oend);
4404 if (XFASTINT (end) < startpos)
4405 break;
4406 if (insertion && (XFASTINT (start) == startpos
4407 || XFASTINT (end) == startpos))
4409 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4410 if (!NILP (prop))
4411 add_overlay_mod_hooklist (prop, overlay);
4413 if (insertion && (XFASTINT (start) == endpos
4414 || XFASTINT (end) == endpos))
4416 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4417 if (!NILP (prop))
4418 add_overlay_mod_hooklist (prop, overlay);
4420 /* Test for intersecting intervals. This does the right thing
4421 for both insertion and deletion. */
4422 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4424 prop = Foverlay_get (overlay, Qmodification_hooks);
4425 if (!NILP (prop))
4426 add_overlay_mod_hooklist (prop, overlay);
4431 GCPRO4 (overlay, arg1, arg2, arg3);
4433 /* Call the functions recorded in last_overlay_modification_hooks.
4434 First copy the vector contents, in case some of these hooks
4435 do subsequent modification of the buffer. */
4436 int size = last_overlay_modification_hooks_used;
4437 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4438 int i;
4440 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4441 copy, size * sizeof (Lisp_Object));
4442 gcpro1.var = copy;
4443 gcpro1.nvars = size;
4445 for (i = 0; i < size;)
4447 Lisp_Object prop, overlay;
4448 prop = copy[i++];
4449 overlay = copy[i++];
4450 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4453 UNGCPRO;
4456 static void
4457 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4458 Lisp_Object list, overlay;
4459 int after;
4460 Lisp_Object arg1, arg2, arg3;
4462 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4464 GCPRO4 (list, arg1, arg2, arg3);
4466 while (CONSP (list))
4468 if (NILP (arg3))
4469 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4470 else
4471 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4472 list = XCDR (list);
4474 UNGCPRO;
4477 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4478 property is set. */
4479 void
4480 evaporate_overlays (pos)
4481 EMACS_INT pos;
4483 Lisp_Object overlay, hit_list;
4484 struct Lisp_Overlay *tail;
4486 hit_list = Qnil;
4487 if (pos <= current_buffer->overlay_center)
4488 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4490 int endpos;
4491 XSETMISC (overlay, tail);
4492 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4493 if (endpos < pos)
4494 break;
4495 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4496 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4497 hit_list = Fcons (overlay, hit_list);
4499 else
4500 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4502 int startpos;
4503 XSETMISC (overlay, tail);
4504 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4505 if (startpos > pos)
4506 break;
4507 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4508 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4509 hit_list = Fcons (overlay, hit_list);
4511 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4512 Fdelete_overlay (XCAR (hit_list));
4515 /* Somebody has tried to store a value with an unacceptable type
4516 in the slot with offset OFFSET. */
4518 void
4519 buffer_slot_type_mismatch (newval, type)
4520 Lisp_Object newval;
4521 int type;
4523 Lisp_Object predicate;
4525 switch (type)
4527 case Lisp_Int: predicate = Qintegerp; break;
4528 case Lisp_String: predicate = Qstringp; break;
4529 case Lisp_Symbol: predicate = Qsymbolp; break;
4530 default: abort ();
4533 wrong_type_argument (predicate, newval);
4537 /***********************************************************************
4538 Allocation with mmap
4539 ***********************************************************************/
4541 #ifdef USE_MMAP_FOR_BUFFERS
4543 #include <sys/types.h>
4544 #include <sys/mman.h>
4546 #ifndef MAP_ANON
4547 #ifdef MAP_ANONYMOUS
4548 #define MAP_ANON MAP_ANONYMOUS
4549 #else
4550 #define MAP_ANON 0
4551 #endif
4552 #endif
4554 #ifndef MAP_FAILED
4555 #define MAP_FAILED ((void *) -1)
4556 #endif
4558 #include <stdio.h>
4559 #include <errno.h>
4561 #if MAP_ANON == 0
4562 #include <fcntl.h>
4563 #endif
4565 #include "coding.h"
4568 /* Memory is allocated in regions which are mapped using mmap(2).
4569 The current implementation lets the system select mapped
4570 addresses; we're not using MAP_FIXED in general, except when
4571 trying to enlarge regions.
4573 Each mapped region starts with a mmap_region structure, the user
4574 area starts after that structure, aligned to MEM_ALIGN.
4576 +-----------------------+
4577 | struct mmap_info + |
4578 | padding |
4579 +-----------------------+
4580 | user data |
4583 +-----------------------+ */
4585 struct mmap_region
4587 /* User-specified size. */
4588 size_t nbytes_specified;
4590 /* Number of bytes mapped */
4591 size_t nbytes_mapped;
4593 /* Pointer to the location holding the address of the memory
4594 allocated with the mmap'd block. The variable actually points
4595 after this structure. */
4596 POINTER_TYPE **var;
4598 /* Next and previous in list of all mmap'd regions. */
4599 struct mmap_region *next, *prev;
4602 /* Doubly-linked list of mmap'd regions. */
4604 static struct mmap_region *mmap_regions;
4606 /* File descriptor for mmap. If we don't have anonymous mapping,
4607 /dev/zero will be opened on it. */
4609 static int mmap_fd;
4611 /* Temporary storage for mmap_set_vars, see there. */
4613 static struct mmap_region *mmap_regions_1;
4614 static int mmap_fd_1;
4616 /* Page size on this system. */
4618 static int mmap_page_size;
4620 /* 1 means mmap has been intialized. */
4622 static int mmap_initialized_p;
4624 /* Value is X rounded up to the next multiple of N. */
4626 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4628 /* Size of mmap_region structure plus padding. */
4630 #define MMAP_REGION_STRUCT_SIZE \
4631 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4633 /* Given a pointer P to the start of the user-visible part of a mapped
4634 region, return a pointer to the start of the region. */
4636 #define MMAP_REGION(P) \
4637 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4639 /* Given a pointer P to the start of a mapped region, return a pointer
4640 to the start of the user-visible part of the region. */
4642 #define MMAP_USER_AREA(P) \
4643 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4645 #define MEM_ALIGN sizeof (double)
4647 /* Predicate returning true if part of the address range [START .. END]
4648 is currently mapped. Used to prevent overwriting an existing
4649 memory mapping.
4651 Default is to conservativly assume the address range is occupied by
4652 something else. This can be overridden by system configuration
4653 files if system-specific means to determine this exists. */
4655 #ifndef MMAP_ALLOCATED_P
4656 #define MMAP_ALLOCATED_P(start, end) 1
4657 #endif
4659 /* Function prototypes. */
4661 static int mmap_free_1 P_ ((struct mmap_region *));
4662 static int mmap_enlarge P_ ((struct mmap_region *, int));
4663 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4664 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4665 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4666 static void mmap_free P_ ((POINTER_TYPE **ptr));
4667 static void mmap_init P_ ((void));
4670 /* Return a region overlapping address range START...END, or null if
4671 none. END is not including, i.e. the last byte in the range
4672 is at END - 1. */
4674 static struct mmap_region *
4675 mmap_find (start, end)
4676 POINTER_TYPE *start, *end;
4678 struct mmap_region *r;
4679 char *s = (char *) start, *e = (char *) end;
4681 for (r = mmap_regions; r; r = r->next)
4683 char *rstart = (char *) r;
4684 char *rend = rstart + r->nbytes_mapped;
4686 if (/* First byte of range, i.e. START, in this region? */
4687 (s >= rstart && s < rend)
4688 /* Last byte of range, i.e. END - 1, in this region? */
4689 || (e > rstart && e <= rend)
4690 /* First byte of this region in the range? */
4691 || (rstart >= s && rstart < e)
4692 /* Last byte of this region in the range? */
4693 || (rend > s && rend <= e))
4694 break;
4697 return r;
4701 /* Unmap a region. P is a pointer to the start of the user-araa of
4702 the region. Value is non-zero if successful. */
4704 static int
4705 mmap_free_1 (r)
4706 struct mmap_region *r;
4708 if (r->next)
4709 r->next->prev = r->prev;
4710 if (r->prev)
4711 r->prev->next = r->next;
4712 else
4713 mmap_regions = r->next;
4715 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4717 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4718 return 0;
4721 return 1;
4725 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4726 Value is non-zero if successful. */
4728 static int
4729 mmap_enlarge (r, npages)
4730 struct mmap_region *r;
4731 int npages;
4733 char *region_end = (char *) r + r->nbytes_mapped;
4734 size_t nbytes;
4735 int success = 0;
4737 if (npages < 0)
4739 /* Unmap pages at the end of the region. */
4740 nbytes = - npages * mmap_page_size;
4741 if (munmap (region_end - nbytes, nbytes) == -1)
4742 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4743 else
4745 r->nbytes_mapped -= nbytes;
4746 success = 1;
4749 else if (npages > 0)
4751 nbytes = npages * mmap_page_size;
4753 /* Try to map additional pages at the end of the region. We
4754 cannot do this if the address range is already occupied by
4755 something else because mmap deletes any previous mapping.
4756 I'm not sure this is worth doing, let's see. */
4757 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4759 POINTER_TYPE *p;
4761 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4762 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4763 if (p == MAP_FAILED)
4764 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4765 else if (p != (POINTER_TYPE *) region_end)
4767 /* Kernels are free to choose a different address. In
4768 that case, unmap what we've mapped above; we have
4769 no use for it. */
4770 if (munmap (p, nbytes) == -1)
4771 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4773 else
4775 r->nbytes_mapped += nbytes;
4776 success = 1;
4781 return success;
4785 /* Set or reset variables holding references to mapped regions. If
4786 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4787 non-zero, set all variables to the start of the user-areas
4788 of mapped regions.
4790 This function is called from Fdump_emacs to ensure that the dumped
4791 Emacs doesn't contain references to memory that won't be mapped
4792 when Emacs starts. */
4794 void
4795 mmap_set_vars (restore_p)
4796 int restore_p;
4798 struct mmap_region *r;
4800 if (restore_p)
4802 mmap_regions = mmap_regions_1;
4803 mmap_fd = mmap_fd_1;
4804 for (r = mmap_regions; r; r = r->next)
4805 *r->var = MMAP_USER_AREA (r);
4807 else
4809 for (r = mmap_regions; r; r = r->next)
4810 *r->var = NULL;
4811 mmap_regions_1 = mmap_regions;
4812 mmap_regions = NULL;
4813 mmap_fd_1 = mmap_fd;
4814 mmap_fd = -1;
4819 /* Allocate a block of storage large enough to hold NBYTES bytes of
4820 data. A pointer to the data is returned in *VAR. VAR is thus the
4821 address of some variable which will use the data area.
4823 The allocation of 0 bytes is valid.
4825 If we can't allocate the necessary memory, set *VAR to null, and
4826 return null. */
4828 static POINTER_TYPE *
4829 mmap_alloc (var, nbytes)
4830 POINTER_TYPE **var;
4831 size_t nbytes;
4833 void *p;
4834 size_t map;
4836 mmap_init ();
4838 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4839 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4840 mmap_fd, 0);
4842 if (p == MAP_FAILED)
4844 if (errno != ENOMEM)
4845 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4846 p = NULL;
4848 else
4850 struct mmap_region *r = (struct mmap_region *) p;
4852 r->nbytes_specified = nbytes;
4853 r->nbytes_mapped = map;
4854 r->var = var;
4855 r->prev = NULL;
4856 r->next = mmap_regions;
4857 if (r->next)
4858 r->next->prev = r;
4859 mmap_regions = r;
4861 p = MMAP_USER_AREA (p);
4864 return *var = p;
4868 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4869 resize it to size NBYTES. Change *VAR to reflect the new block,
4870 and return this value. If more memory cannot be allocated, then
4871 leave *VAR unchanged, and return null. */
4873 static POINTER_TYPE *
4874 mmap_realloc (var, nbytes)
4875 POINTER_TYPE **var;
4876 size_t nbytes;
4878 POINTER_TYPE *result;
4880 mmap_init ();
4882 if (*var == NULL)
4883 result = mmap_alloc (var, nbytes);
4884 else if (nbytes == 0)
4886 mmap_free (var);
4887 result = mmap_alloc (var, nbytes);
4889 else
4891 struct mmap_region *r = MMAP_REGION (*var);
4892 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4894 if (room < nbytes)
4896 /* Must enlarge. */
4897 POINTER_TYPE *old_ptr = *var;
4899 /* Try to map additional pages at the end of the region.
4900 If that fails, allocate a new region, copy data
4901 from the old region, then free it. */
4902 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4903 / mmap_page_size)))
4905 r->nbytes_specified = nbytes;
4906 *var = result = old_ptr;
4908 else if (mmap_alloc (var, nbytes))
4910 bcopy (old_ptr, *var, r->nbytes_specified);
4911 mmap_free_1 (MMAP_REGION (old_ptr));
4912 result = *var;
4913 r = MMAP_REGION (result);
4914 r->nbytes_specified = nbytes;
4916 else
4918 *var = old_ptr;
4919 result = NULL;
4922 else if (room - nbytes >= mmap_page_size)
4924 /* Shrinking by at least a page. Let's give some
4925 memory back to the system.
4927 The extra parens are to make the division happens first,
4928 on positive values, so we know it will round towards
4929 zero. */
4930 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4931 result = *var;
4932 r->nbytes_specified = nbytes;
4934 else
4936 /* Leave it alone. */
4937 result = *var;
4938 r->nbytes_specified = nbytes;
4942 return result;
4946 /* Free a block of relocatable storage whose data is pointed to by
4947 PTR. Store 0 in *PTR to show there's no block allocated. */
4949 static void
4950 mmap_free (var)
4951 POINTER_TYPE **var;
4953 mmap_init ();
4955 if (*var)
4957 mmap_free_1 (MMAP_REGION (*var));
4958 *var = NULL;
4963 /* Perform necessary intializations for the use of mmap. */
4965 static void
4966 mmap_init ()
4968 #if MAP_ANON == 0
4969 /* The value of mmap_fd is initially 0 in temacs, and -1
4970 in a dumped Emacs. */
4971 if (mmap_fd <= 0)
4973 /* No anonymous mmap -- we need the file descriptor. */
4974 mmap_fd = open ("/dev/zero", O_RDONLY);
4975 if (mmap_fd == -1)
4976 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4978 #endif /* MAP_ANON == 0 */
4980 if (mmap_initialized_p)
4981 return;
4982 mmap_initialized_p = 1;
4984 #if MAP_ANON != 0
4985 mmap_fd = -1;
4986 #endif
4988 mmap_page_size = getpagesize ();
4991 #endif /* USE_MMAP_FOR_BUFFERS */
4995 /***********************************************************************
4996 Buffer-text Allocation
4997 ***********************************************************************/
4999 #ifdef REL_ALLOC
5000 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5001 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5002 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5003 #endif /* REL_ALLOC */
5006 /* Allocate NBYTES bytes for buffer B's text buffer. */
5008 static void
5009 alloc_buffer_text (b, nbytes)
5010 struct buffer *b;
5011 size_t nbytes;
5013 POINTER_TYPE *p;
5015 BLOCK_INPUT;
5016 #if defined USE_MMAP_FOR_BUFFERS
5017 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5018 #elif defined REL_ALLOC
5019 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5020 #else
5021 p = xmalloc (nbytes);
5022 #endif
5024 if (p == NULL)
5026 UNBLOCK_INPUT;
5027 memory_full ();
5030 b->text->beg = (unsigned char *) p;
5031 UNBLOCK_INPUT;
5034 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5035 shrink it. */
5037 void
5038 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5040 POINTER_TYPE *p;
5041 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5042 + delta);
5043 BLOCK_INPUT;
5044 #if defined USE_MMAP_FOR_BUFFERS
5045 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5046 #elif defined REL_ALLOC
5047 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5048 #else
5049 p = xrealloc (b->text->beg, nbytes);
5050 #endif
5052 if (p == NULL)
5054 UNBLOCK_INPUT;
5055 memory_full ();
5058 BUF_BEG_ADDR (b) = (unsigned char *) p;
5059 UNBLOCK_INPUT;
5063 /* Free buffer B's text buffer. */
5065 static void
5066 free_buffer_text (b)
5067 struct buffer *b;
5069 BLOCK_INPUT;
5071 #if defined USE_MMAP_FOR_BUFFERS
5072 mmap_free ((POINTER_TYPE **) &b->text->beg);
5073 #elif defined REL_ALLOC
5074 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5075 #else
5076 xfree (b->text->beg);
5077 #endif
5079 BUF_BEG_ADDR (b) = NULL;
5080 UNBLOCK_INPUT;
5085 /***********************************************************************
5086 Initialization
5087 ***********************************************************************/
5089 void
5090 init_buffer_once ()
5092 int idx;
5094 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5096 /* Make sure all markable slots in buffer_defaults
5097 are initialized reasonably, so mark_buffer won't choke. */
5098 reset_buffer (&buffer_defaults);
5099 reset_buffer_local_variables (&buffer_defaults, 1);
5100 reset_buffer (&buffer_local_symbols);
5101 reset_buffer_local_variables (&buffer_local_symbols, 1);
5102 /* Prevent GC from getting confused. */
5103 buffer_defaults.text = &buffer_defaults.own_text;
5104 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5105 BUF_INTERVALS (&buffer_defaults) = 0;
5106 BUF_INTERVALS (&buffer_local_symbols) = 0;
5107 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5108 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5109 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5110 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5112 /* Set up the default values of various buffer slots. */
5113 /* Must do these before making the first buffer! */
5115 /* real setup is done in bindings.el */
5116 buffer_defaults.mode_line_format = build_string ("%-");
5117 buffer_defaults.header_line_format = Qnil;
5118 buffer_defaults.abbrev_mode = Qnil;
5119 buffer_defaults.overwrite_mode = Qnil;
5120 buffer_defaults.case_fold_search = Qt;
5121 buffer_defaults.auto_fill_function = Qnil;
5122 buffer_defaults.selective_display = Qnil;
5123 #ifndef old
5124 buffer_defaults.selective_display_ellipses = Qt;
5125 #endif
5126 buffer_defaults.abbrev_table = Qnil;
5127 buffer_defaults.display_table = Qnil;
5128 buffer_defaults.undo_list = Qnil;
5129 buffer_defaults.mark_active = Qnil;
5130 buffer_defaults.file_format = Qnil;
5131 buffer_defaults.auto_save_file_format = Qt;
5132 buffer_defaults.overlays_before = NULL;
5133 buffer_defaults.overlays_after = NULL;
5134 buffer_defaults.overlay_center = BEG;
5136 XSETFASTINT (buffer_defaults.tab_width, 8);
5137 buffer_defaults.truncate_lines = Qnil;
5138 buffer_defaults.ctl_arrow = Qt;
5139 buffer_defaults.direction_reversed = Qnil;
5140 buffer_defaults.cursor_type = Qt;
5141 buffer_defaults.extra_line_spacing = Qnil;
5142 buffer_defaults.cursor_in_non_selected_windows = Qt;
5144 #ifdef DOS_NT
5145 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5146 #endif
5147 buffer_defaults.enable_multibyte_characters = Qt;
5148 buffer_defaults.buffer_file_coding_system = Qnil;
5149 XSETFASTINT (buffer_defaults.fill_column, 70);
5150 XSETFASTINT (buffer_defaults.left_margin, 0);
5151 buffer_defaults.cache_long_line_scans = Qnil;
5152 buffer_defaults.file_truename = Qnil;
5153 XSETFASTINT (buffer_defaults.display_count, 0);
5154 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5155 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5156 buffer_defaults.left_fringe_width = Qnil;
5157 buffer_defaults.right_fringe_width = Qnil;
5158 buffer_defaults.fringes_outside_margins = Qnil;
5159 buffer_defaults.scroll_bar_width = Qnil;
5160 buffer_defaults.vertical_scroll_bar_type = Qt;
5161 buffer_defaults.indicate_empty_lines = Qnil;
5162 buffer_defaults.indicate_buffer_boundaries = Qnil;
5163 buffer_defaults.fringe_indicator_alist = Qnil;
5164 buffer_defaults.fringe_cursor_alist = Qnil;
5165 buffer_defaults.scroll_up_aggressively = Qnil;
5166 buffer_defaults.scroll_down_aggressively = Qnil;
5167 buffer_defaults.display_time = Qnil;
5169 /* Assign the local-flags to the slots that have default values.
5170 The local flag is a bit that is used in the buffer
5171 to say that it has its own local value for the slot.
5172 The local flag bits are in the local_var_flags slot of the buffer. */
5174 /* Nothing can work if this isn't true */
5175 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5177 /* 0 means not a lisp var, -1 means always local, else mask */
5178 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5179 XSETINT (buffer_local_flags.filename, -1);
5180 XSETINT (buffer_local_flags.directory, -1);
5181 XSETINT (buffer_local_flags.backed_up, -1);
5182 XSETINT (buffer_local_flags.save_length, -1);
5183 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5184 XSETINT (buffer_local_flags.read_only, -1);
5185 XSETINT (buffer_local_flags.major_mode, -1);
5186 XSETINT (buffer_local_flags.mode_name, -1);
5187 XSETINT (buffer_local_flags.undo_list, -1);
5188 XSETINT (buffer_local_flags.mark_active, -1);
5189 XSETINT (buffer_local_flags.point_before_scroll, -1);
5190 XSETINT (buffer_local_flags.file_truename, -1);
5191 XSETINT (buffer_local_flags.invisibility_spec, -1);
5192 XSETINT (buffer_local_flags.file_format, -1);
5193 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5194 XSETINT (buffer_local_flags.display_count, -1);
5195 XSETINT (buffer_local_flags.display_time, -1);
5196 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5198 idx = 1;
5199 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5200 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5201 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5202 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5203 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5204 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5205 #ifndef old
5206 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5207 #endif
5208 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5209 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5210 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5211 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5212 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5213 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5214 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5215 #ifdef DOS_NT
5216 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5217 /* Make this one a permanent local. */
5218 buffer_permanent_local_flags[idx++] = 1;
5219 #endif
5220 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5221 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5222 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5223 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5224 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5225 /* Make this one a permanent local. */
5226 buffer_permanent_local_flags[idx++] = 1;
5227 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5228 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5229 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5230 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5231 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5232 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5233 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5234 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5235 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5236 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5237 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5238 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5239 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5240 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5241 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5242 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5243 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5245 /* Need more room? */
5246 if (idx >= MAX_PER_BUFFER_VARS)
5247 abort ();
5248 last_per_buffer_idx = idx;
5250 Vbuffer_alist = Qnil;
5251 current_buffer = 0;
5252 all_buffers = 0;
5254 QSFundamental = build_string ("Fundamental");
5256 Qfundamental_mode = intern ("fundamental-mode");
5257 buffer_defaults.major_mode = Qfundamental_mode;
5259 Qmode_class = intern ("mode-class");
5261 Qprotected_field = intern ("protected-field");
5263 Qpermanent_local = intern ("permanent-local");
5265 Qkill_buffer_hook = intern ("kill-buffer-hook");
5266 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5268 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5270 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5272 /* super-magic invisible buffer */
5273 Vbuffer_alist = Qnil;
5275 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5277 inhibit_modification_hooks = 0;
5280 void
5281 init_buffer ()
5283 char *pwd;
5284 Lisp_Object temp;
5285 int len;
5287 #ifdef USE_MMAP_FOR_BUFFERS
5289 /* When using the ralloc implementation based on mmap(2), buffer
5290 text pointers will have been set to null in the dumped Emacs.
5291 Map new memory. */
5292 struct buffer *b;
5294 for (b = all_buffers; b; b = b->next)
5295 if (b->text->beg == NULL)
5296 enlarge_buffer_text (b, 0);
5298 #endif /* USE_MMAP_FOR_BUFFERS */
5300 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5301 if (NILP (buffer_defaults.enable_multibyte_characters))
5302 Fset_buffer_multibyte (Qnil);
5304 pwd = get_current_dir_name ();
5306 if (!pwd)
5307 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5309 #ifndef VMS
5310 /* Maybe this should really use some standard subroutine
5311 whose definition is filename syntax dependent. */
5312 len = strlen (pwd);
5313 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5315 /* Grow buffer to add directory separator and '\0'. */
5316 pwd = (char *) xrealloc (pwd, len + 2);
5317 pwd[len] = DIRECTORY_SEP;
5318 pwd[len + 1] = '\0';
5320 #endif /* not VMS */
5322 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5323 if (! NILP (buffer_defaults.enable_multibyte_characters))
5324 /* At this moment, we still don't know how to decode the
5325 directory name. So, we keep the bytes in multibyte form so
5326 that ENCODE_FILE correctly gets the original bytes. */
5327 current_buffer->directory
5328 = string_to_multibyte (current_buffer->directory);
5330 /* Add /: to the front of the name
5331 if it would otherwise be treated as magic. */
5332 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5333 if (! NILP (temp)
5334 /* If the default dir is just /, TEMP is non-nil
5335 because of the ange-ftp completion handler.
5336 However, it is not necessary to turn / into /:/.
5337 So avoid doing that. */
5338 && strcmp ("/", SDATA (current_buffer->directory)))
5339 current_buffer->directory
5340 = concat2 (build_string ("/:"), current_buffer->directory);
5342 temp = get_minibuffer (0);
5343 XBUFFER (temp)->directory = current_buffer->directory;
5345 free (pwd);
5348 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5349 Object stored in the current buffer. address is the address of the slot
5350 in the buffer that is current now. */
5352 /* TYPE is nil for a general Lisp variable.
5353 An integer specifies a type; then only LIsp values
5354 with that type code are allowed (except that nil is allowed too).
5355 LNAME is the LIsp-level variable name.
5356 VNAME is the name of the buffer slot.
5357 DOC is a dummy where you write the doc string as a comment. */
5358 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5359 defvar_per_buffer (lname, vname, type, 0)
5361 static void
5362 defvar_per_buffer (namestring, address, type, doc)
5363 char *namestring;
5364 Lisp_Object *address;
5365 Lisp_Object type;
5366 char *doc;
5368 Lisp_Object sym, val;
5369 int offset;
5371 sym = intern (namestring);
5372 val = allocate_misc ();
5373 offset = (char *)address - (char *)current_buffer;
5375 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5376 XBUFFER_OBJFWD (val)->offset = offset;
5377 XBUFFER_OBJFWD (val)->slottype = type;
5378 SET_SYMBOL_VALUE (sym, val);
5379 PER_BUFFER_SYMBOL (offset) = sym;
5381 if (PER_BUFFER_IDX (offset) == 0)
5382 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5383 slot of buffer_local_flags */
5384 abort ();
5388 /* initialize the buffer routines */
5389 void
5390 syms_of_buffer ()
5392 staticpro (&last_overlay_modification_hooks);
5393 last_overlay_modification_hooks
5394 = Fmake_vector (make_number (10), Qnil);
5396 staticpro (&Vbuffer_defaults);
5397 staticpro (&Vbuffer_local_symbols);
5398 staticpro (&Qfundamental_mode);
5399 staticpro (&Qmode_class);
5400 staticpro (&QSFundamental);
5401 staticpro (&Vbuffer_alist);
5402 staticpro (&Qprotected_field);
5403 staticpro (&Qpermanent_local);
5404 Qpermanent_local_hook = intern ("permanent-local-hook");
5405 staticpro (&Qpermanent_local_hook);
5406 staticpro (&Qkill_buffer_hook);
5407 Qoverlayp = intern ("overlayp");
5408 staticpro (&Qoverlayp);
5409 Qevaporate = intern ("evaporate");
5410 staticpro (&Qevaporate);
5411 Qmodification_hooks = intern ("modification-hooks");
5412 staticpro (&Qmodification_hooks);
5413 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5414 staticpro (&Qinsert_in_front_hooks);
5415 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5416 staticpro (&Qinsert_behind_hooks);
5417 Qget_file_buffer = intern ("get-file-buffer");
5418 staticpro (&Qget_file_buffer);
5419 Qpriority = intern ("priority");
5420 staticpro (&Qpriority);
5421 Qwindow = intern ("window");
5422 staticpro (&Qwindow);
5423 Qbefore_string = intern ("before-string");
5424 staticpro (&Qbefore_string);
5425 Qafter_string = intern ("after-string");
5426 staticpro (&Qafter_string);
5427 Qfirst_change_hook = intern ("first-change-hook");
5428 staticpro (&Qfirst_change_hook);
5429 Qbefore_change_functions = intern ("before-change-functions");
5430 staticpro (&Qbefore_change_functions);
5431 Qafter_change_functions = intern ("after-change-functions");
5432 staticpro (&Qafter_change_functions);
5433 /* The next one is initialized in init_buffer_once. */
5434 staticpro (&Qucs_set_table_for_input);
5436 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5437 staticpro (&Qkill_buffer_query_functions);
5439 Fput (Qprotected_field, Qerror_conditions,
5440 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5441 Fput (Qprotected_field, Qerror_message,
5442 build_string ("Attempt to modify a protected field"));
5444 /* All these use DEFVAR_LISP_NOPRO because the slots in
5445 buffer_defaults will all be marked via Vbuffer_defaults. */
5447 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5448 &buffer_defaults.mode_line_format,
5449 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5450 This is the same as (default-value 'mode-line-format). */);
5452 DEFVAR_LISP_NOPRO ("default-header-line-format",
5453 &buffer_defaults.header_line_format,
5454 doc: /* Default value of `header-line-format' for buffers that don't override it.
5455 This is the same as (default-value 'header-line-format). */);
5457 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5458 doc: /* Default value of `cursor-type' for buffers that don't override it.
5459 This is the same as (default-value 'cursor-type). */);
5461 DEFVAR_LISP_NOPRO ("default-line-spacing",
5462 &buffer_defaults.extra_line_spacing,
5463 doc: /* Default value of `line-spacing' for buffers that don't override it.
5464 This is the same as (default-value 'line-spacing). */);
5466 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5467 &buffer_defaults.cursor_in_non_selected_windows,
5468 doc: /* Default value of `cursor-in-non-selected-windows'.
5469 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5471 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5472 &buffer_defaults.abbrev_mode,
5473 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5474 This is the same as (default-value 'abbrev-mode). */);
5476 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5477 &buffer_defaults.ctl_arrow,
5478 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5479 This is the same as (default-value 'ctl-arrow). */);
5481 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5482 &buffer_defaults.direction_reversed,
5483 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5484 This is the same as (default-value 'direction-reversed). */);
5486 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5487 &buffer_defaults.enable_multibyte_characters,
5488 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5489 This is the same as (default-value 'enable-multibyte-characters). */);
5491 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5492 &buffer_defaults.buffer_file_coding_system,
5493 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5494 This is the same as (default-value 'buffer-file-coding-system). */);
5496 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5497 &buffer_defaults.truncate_lines,
5498 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5499 This is the same as (default-value 'truncate-lines). */);
5501 DEFVAR_LISP_NOPRO ("default-fill-column",
5502 &buffer_defaults.fill_column,
5503 doc: /* Default value of `fill-column' for buffers that do not override it.
5504 This is the same as (default-value 'fill-column). */);
5506 DEFVAR_LISP_NOPRO ("default-left-margin",
5507 &buffer_defaults.left_margin,
5508 doc: /* Default value of `left-margin' for buffers that do not override it.
5509 This is the same as (default-value 'left-margin). */);
5511 DEFVAR_LISP_NOPRO ("default-tab-width",
5512 &buffer_defaults.tab_width,
5513 doc: /* Default value of `tab-width' for buffers that do not override it.
5514 This is the same as (default-value 'tab-width). */);
5516 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5517 &buffer_defaults.case_fold_search,
5518 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5519 This is the same as (default-value 'case-fold-search). */);
5521 #ifdef DOS_NT
5522 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5523 &buffer_defaults.buffer_file_type,
5524 doc: /* Default file type for buffers that do not override it.
5525 This is the same as (default-value 'buffer-file-type).
5526 The file type is nil for text, t for binary. */);
5527 #endif
5529 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5530 &buffer_defaults.left_margin_cols,
5531 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5532 This is the same as (default-value 'left-margin-width). */);
5534 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5535 &buffer_defaults.right_margin_cols,
5536 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5537 This is the same as (default-value 'right-margin-width). */);
5539 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5540 &buffer_defaults.left_fringe_width,
5541 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5542 This is the same as (default-value 'left-fringe-width). */);
5544 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5545 &buffer_defaults.right_fringe_width,
5546 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5547 This is the same as (default-value 'right-fringe-width). */);
5549 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5550 &buffer_defaults.fringes_outside_margins,
5551 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5552 This is the same as (default-value 'fringes-outside-margins). */);
5554 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5555 &buffer_defaults.scroll_bar_width,
5556 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5557 This is the same as (default-value 'scroll-bar-width). */);
5559 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5560 &buffer_defaults.vertical_scroll_bar_type,
5561 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5562 This is the same as (default-value 'vertical-scroll-bar). */);
5564 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5565 &buffer_defaults.indicate_empty_lines,
5566 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5567 This is the same as (default-value 'indicate-empty-lines). */);
5569 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5570 &buffer_defaults.indicate_buffer_boundaries,
5571 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5572 This is the same as (default-value 'indicate-buffer-boundaries). */);
5574 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5575 &buffer_defaults.fringe_indicator_alist,
5576 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5577 This is the same as (default-value 'fringe-indicator-alist'). */);
5579 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5580 &buffer_defaults.fringe_cursor_alist,
5581 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5582 This is the same as (default-value 'fringe-cursor-alist'). */);
5584 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5585 &buffer_defaults.scroll_up_aggressively,
5586 doc: /* Default value of `scroll-up-aggressively'.
5587 This value applies in buffers that don't have their own local values.
5588 This is the same as (default-value 'scroll-up-aggressively). */);
5590 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5591 &buffer_defaults.scroll_down_aggressively,
5592 doc: /* Default value of `scroll-down-aggressively'.
5593 This value applies in buffers that don't have their own local values.
5594 This is the same as (default-value 'scroll-down-aggressively). */);
5596 DEFVAR_PER_BUFFER ("header-line-format",
5597 &current_buffer->header_line_format,
5598 Qnil,
5599 doc: /* Analogous to `mode-line-format', but controls the header line.
5600 The header line appears, optionally, at the top of a window;
5601 the mode line appears at the bottom. */);
5603 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5604 Qnil,
5605 doc: /* Template for displaying mode line for current buffer.
5606 Each buffer has its own value of this variable.
5607 Value may be nil, a string, a symbol or a list or cons cell.
5608 A value of nil means don't display a mode line.
5609 For a symbol, its value is used (but it is ignored if t or nil).
5610 A string appearing directly as the value of a symbol is processed verbatim
5611 in that the %-constructs below are not recognized.
5612 Note that unless the symbol is marked as a `risky-local-variable', all
5613 properties in any strings, as well as all :eval and :propertize forms
5614 in the value of that symbol will be ignored.
5615 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5616 is used as a mode line element. Be careful--FORM should not load any files,
5617 because that can cause an infinite recursion.
5618 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5619 with the specified properties PROPS applied.
5620 For a list whose car is a symbol, the symbol's value is taken,
5621 and if that is non-nil, the cadr of the list is processed recursively.
5622 Otherwise, the caddr of the list (if there is one) is processed.
5623 For a list whose car is a string or list, each element is processed
5624 recursively and the results are effectively concatenated.
5625 For a list whose car is an integer, the cdr of the list is processed
5626 and padded (if the number is positive) or truncated (if negative)
5627 to the width specified by that number.
5628 A string is printed verbatim in the mode line except for %-constructs:
5629 (%-constructs are allowed when the string is the entire mode-line-format
5630 or when it is found in a cons-cell or a list)
5631 %b -- print buffer name. %f -- print visited file name.
5632 %F -- print frame name.
5633 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5634 %& is like %*, but ignore read-only-ness.
5635 % means buffer is read-only and * means it is modified.
5636 For a modified read-only buffer, %* gives % and %+ gives *.
5637 %s -- print process status. %l -- print the current line number.
5638 %c -- print the current column number (this makes editing slower).
5639 To make the column number update correctly in all cases,
5640 `column-number-mode' must be non-nil.
5641 %i -- print the size of the buffer.
5642 %I -- like %i, but use k, M, G, etc., to abbreviate.
5643 %p -- print percent of buffer above top of window, or Top, Bot or All.
5644 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5645 or print Bottom or All.
5646 %n -- print Narrow if appropriate.
5647 %t -- visited file is text or binary (if OS supports this distinction).
5648 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5649 %Z -- like %z, but including the end-of-line format.
5650 %e -- print error message about full memory.
5651 %@ -- print @ or hyphen. @ means that default-directory is on a
5652 remote machine.
5653 %[ -- print one [ for each recursive editing level. %] similar.
5654 %% -- print %. %- -- print infinitely many dashes.
5655 Decimal digits after the % specify field width to which to pad. */);
5657 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5658 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5659 A value of nil means use current buffer's major mode,
5660 provided it is not marked as "special".
5662 When a mode is used by default, `find-file' switches to it
5663 before it reads the contents into the buffer and before
5664 it finishes setting up the buffer. Thus, the mode and
5665 its hooks should not expect certain variables such as
5666 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5668 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5669 make_number (Lisp_Symbol),
5670 doc: /* Symbol for current buffer's major mode. */);
5672 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5673 Qnil,
5674 doc: /* Pretty name of current buffer's major mode.
5675 Usually a string. See `mode-line-format' for other possible forms.
5676 Use the function `format-mode-line' to get the value as a string. */);
5678 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5679 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5681 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5682 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5684 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5685 Qnil,
5686 doc: /* *Non-nil if searches and matches should ignore case. */);
5688 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5689 make_number (Lisp_Int),
5690 doc: /* *Column beyond which automatic line-wrapping should happen.
5691 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5693 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5694 make_number (Lisp_Int),
5695 doc: /* *Column for the default `indent-line-function' to indent to.
5696 Linefeed indents to this column in Fundamental mode. */);
5698 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5699 make_number (Lisp_Int),
5700 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5702 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5703 doc: /* *Non-nil means display control chars with uparrow.
5704 A value of nil means use backslash and octal digits.
5705 This variable does not apply to characters whose display is specified
5706 in the current display table (if there is one). */);
5708 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5709 &current_buffer->enable_multibyte_characters,
5710 Qnil,
5711 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5712 Otherwise they are regarded as unibyte. This affects the display,
5713 file I/O and the behavior of various editing commands.
5715 This variable is buffer-local but you cannot set it directly;
5716 use the function `set-buffer-multibyte' to change a buffer's representation.
5717 Changing its default value with `setq-default' is supported.
5718 See also variable `default-enable-multibyte-characters' and Info node
5719 `(elisp)Text Representations'. */);
5720 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5722 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5723 &current_buffer->buffer_file_coding_system, Qnil,
5724 doc: /* Coding system to be used for encoding the buffer contents on saving.
5725 This variable applies to saving the buffer, and also to `write-region'
5726 and other functions that use `write-region'.
5727 It does not apply to sending output to subprocesses, however.
5729 If this is nil, the buffer is saved without any code conversion
5730 unless some coding system is specified in `file-coding-system-alist'
5731 for the buffer file.
5733 If the text to be saved cannot be encoded as specified by this variable,
5734 an alternative encoding is selected by `select-safe-coding-system', which see.
5736 The variable `coding-system-for-write', if non-nil, overrides this variable.
5738 This variable is never applied to a way of decoding a file while reading it. */);
5740 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5741 Qnil,
5742 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5744 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5745 doc: /* *Non-nil means do not display continuation lines.
5746 Instead, give each line of text just one screen line.
5748 Note that this is overridden by the variable
5749 `truncate-partial-width-windows' if that variable is non-nil
5750 and this buffer is not full-frame width. */);
5752 #ifdef DOS_NT
5753 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5754 Qnil,
5755 doc: /* Non-nil if the visited file is a binary file.
5756 This variable is meaningful on MS-DOG and Windows NT.
5757 On those systems, it is automatically local in every buffer.
5758 On other systems, this variable is normally always nil. */);
5759 #endif
5761 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5762 make_number (Lisp_String),
5763 doc: /* Name of default directory of current buffer. Should end with slash.
5764 To interactively change the default directory, use command `cd'. */);
5766 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5767 Qnil,
5768 doc: /* Function called (if non-nil) to perform auto-fill.
5769 It is called after self-inserting any character specified in
5770 the `auto-fill-chars' table.
5771 NOTE: This variable is not a hook;
5772 its value may not be a list of functions. */);
5774 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5775 make_number (Lisp_String),
5776 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5778 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5779 make_number (Lisp_String),
5780 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5781 The truename of a file is calculated by `file-truename'
5782 and then abbreviated with `abbreviate-file-name'. */);
5784 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5785 &current_buffer->auto_save_file_name,
5786 make_number (Lisp_String),
5787 doc: /* Name of file for auto-saving current buffer.
5788 If it is nil, that means don't auto-save this buffer. */);
5790 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5791 doc: /* Non-nil if this buffer is read-only. */);
5793 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5794 doc: /* Non-nil if this buffer's file has been backed up.
5795 Backing up is done before the first time the file is saved. */);
5797 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5798 make_number (Lisp_Int),
5799 doc: /* Length of current buffer when last read in, saved or auto-saved.
5800 0 initially. */);
5802 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5803 Qnil,
5804 doc: /* Non-nil enables selective display.
5805 An integer N as value means display only lines
5806 that start with less than N columns of space.
5807 A value of t means that the character ^M makes itself and
5808 all the rest of the line invisible; also, when saving the buffer
5809 in a file, save the ^M as a newline. */);
5811 #ifndef old
5812 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5813 &current_buffer->selective_display_ellipses,
5814 Qnil,
5815 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5816 #endif
5818 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5819 doc: /* Non-nil if self-insertion should replace existing text.
5820 The value should be one of `overwrite-mode-textual',
5821 `overwrite-mode-binary', or nil.
5822 If it is `overwrite-mode-textual', self-insertion still
5823 inserts at the end of a line, and inserts when point is before a tab,
5824 until the tab is filled in.
5825 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5827 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5828 Qnil,
5829 doc: /* Display table that controls display of the contents of current buffer.
5831 If this variable is nil, the value of `standard-display-table' is used.
5832 Each window can have its own, overriding display table, see
5833 `set-window-display-table' and `window-display-table'.
5835 The display table is a char-table created with `make-display-table'.
5836 A char-table is an array indexed by character codes. Normal array
5837 primitives `aref' and `aset' can be used to access elements of a char-table.
5839 Each of the char-table elements control how to display the corresponding
5840 text character: the element at index C in the table says how to display
5841 the character whose code is C. Each element should be a vector of
5842 characters or nil. The value nil means display the character in the
5843 default fashion; otherwise, the characters from the vector are delivered
5844 to the screen instead of the original character.
5846 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5847 to display a capital Y instead of each X character.
5849 In addition, a char-table has six extra slots to control the display of:
5851 the end of a truncated screen line (extra-slot 0, a single character);
5852 the end of a continued line (extra-slot 1, a single character);
5853 the escape character used to display character codes in octal
5854 (extra-slot 2, a single character);
5855 the character used as an arrow for control characters (extra-slot 3,
5856 a single character);
5857 the decoration indicating the presence of invisible lines (extra-slot 4,
5858 a vector of characters);
5859 the character used to draw the border between side-by-side windows
5860 (extra-slot 5, a single character).
5862 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5864 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5865 Qnil,
5866 doc: /* *Width of left marginal area for display of a buffer.
5867 A value of nil means no marginal area. */);
5869 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5870 Qnil,
5871 doc: /* *Width of right marginal area for display of a buffer.
5872 A value of nil means no marginal area. */);
5874 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5875 Qnil,
5876 doc: /* *Width of this buffer's left fringe (in pixels).
5877 A value of 0 means no left fringe is shown in this buffer's window.
5878 A value of nil means to use the left fringe width from the window's frame. */);
5880 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5881 Qnil,
5882 doc: /* *Width of this buffer's right fringe (in pixels).
5883 A value of 0 means no right fringe is shown in this buffer's window.
5884 A value of nil means to use the right fringe width from the window's frame. */);
5886 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5887 Qnil,
5888 doc: /* *Non-nil means to display fringes outside display margins.
5889 A value of nil means to display fringes between margins and buffer text. */);
5891 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5892 Qnil,
5893 doc: /* *Width of this buffer's scroll bars in pixels.
5894 A value of nil means to use the scroll bar width from the window's frame. */);
5896 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5897 Qnil,
5898 doc: /* *Position of this buffer's vertical scroll bar.
5899 The value takes effect whenever you tell a window to display this buffer;
5900 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5902 A value of `left' or `right' means put the vertical scroll bar at that side
5903 of the window; a value of nil means don't show any vertical scroll bars.
5904 A value of t (the default) means do whatever the window's frame specifies. */);
5906 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5907 &current_buffer->indicate_empty_lines, Qnil,
5908 doc: /* *Visually indicate empty lines after the buffer end.
5909 If non-nil, a bitmap is displayed in the left fringe of a window on
5910 window-systems. */);
5912 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5913 &current_buffer->indicate_buffer_boundaries, Qnil,
5914 doc: /* *Visually indicate buffer boundaries and scrolling.
5915 If non-nil, the first and last line of the buffer are marked in the fringe
5916 of a window on window-systems with angle bitmaps, or if the window can be
5917 scrolled, the top and bottom line of the window are marked with up and down
5918 arrow bitmaps.
5920 If value is a symbol `left' or `right', both angle and arrow bitmaps
5921 are displayed in the left or right fringe, resp. Any other value
5922 that doesn't look like an alist means display the angle bitmaps in
5923 the left fringe but no arrows.
5925 You can exercise more precise control by using an alist as the
5926 value. Each alist element (INDICATOR . POSITION) specifies
5927 where to show one of the indicators. INDICATOR is one of `top',
5928 `bottom', `up', `down', or t, which specifies the default position,
5929 and POSITION is one of `left', `right', or nil, meaning do not show
5930 this indicator.
5932 For example, ((top . left) (t . right)) places the top angle bitmap in
5933 left fringe, the bottom angle bitmap in right fringe, and both arrow
5934 bitmaps in right fringe. To show just the angle bitmaps in the left
5935 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5937 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5938 &current_buffer->fringe_indicator_alist, Qnil,
5939 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5940 The value is an alist where each element (INDICATOR . BITMAPS)
5941 specifies the fringe bitmaps used to display a specific logical
5942 fringe indicator.
5944 INDICATOR specifies the logical indicator type which is one of the
5945 following symbols: `truncation' , `continuation', `overlay-arrow',
5946 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5948 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5949 the actual bitmap shown in the left or right fringe for the logical
5950 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5951 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5952 are used only for the `bottom' and `one-line' indicators when the last
5953 \(only) line in has no final newline. BITMAPS may also be a single
5954 symbol which is used in both left and right fringes. */);
5956 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5957 &current_buffer->fringe_cursor_alist, Qnil,
5958 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5959 The value is an alist where each element (CURSOR . BITMAP)
5960 specifies the fringe bitmaps used to display a specific logical
5961 cursor type in the fringe.
5963 CURSOR specifies the logical cursor type which is one of the following
5964 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5965 one is used to show a hollow cursor on narrow lines display lines
5966 where the normal hollow cursor will not fit.
5968 BITMAP is the corresponding fringe bitmap shown for the logical
5969 cursor type. */);
5971 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5972 &current_buffer->scroll_up_aggressively, Qnil,
5973 doc: /* How far to scroll windows upward.
5974 If you move point off the bottom, the window scrolls automatically.
5975 This variable controls how far it scrolls. The value nil, the default,
5976 means scroll to center point. A fraction means scroll to put point
5977 that fraction of the window's height from the bottom of the window.
5978 When the value is 0.0, point goes at the bottom line, which in the simple
5979 case that you moved off with C-f means scrolling just one line. 1.0 means
5980 point goes at the top, so that in that simple case, the window
5981 scrolls by a full window height. Meaningful values are
5982 between 0.0 and 1.0, inclusive. */);
5984 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5985 &current_buffer->scroll_down_aggressively, Qnil,
5986 doc: /* How far to scroll windows downward.
5987 If you move point off the top, the window scrolls automatically.
5988 This variable controls how far it scrolls. The value nil, the default,
5989 means scroll to center point. A fraction means scroll to put point
5990 that fraction of the window's height from the top of the window.
5991 When the value is 0.0, point goes at the top line, which in the simple
5992 case that you moved off with C-b means scrolling just one line. 1.0 means
5993 point goes at the bottom, so that in that simple case, the window
5994 scrolls by a full window height. Meaningful values are
5995 between 0.0 and 1.0, inclusive. */);
5997 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5998 "Don't ask.");
6001 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6002 doc: /* List of functions to call before each text change.
6003 Two arguments are passed to each function: the positions of
6004 the beginning and end of the range of old text to be changed.
6005 \(For an insertion, the beginning and end are at the same place.)
6006 No information is given about the length of the text after the change.
6008 Buffer changes made while executing the `before-change-functions'
6009 don't call any before-change or after-change functions.
6010 That's because these variables are temporarily set to nil.
6011 As a result, a hook function cannot straightforwardly alter the value of
6012 these variables. See the Emacs Lisp manual for a way of
6013 accomplishing an equivalent result by using other variables.
6015 If an unhandled error happens in running these functions,
6016 the variable's value remains nil. That prevents the error
6017 from happening repeatedly and making Emacs nonfunctional. */);
6018 Vbefore_change_functions = Qnil;
6020 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6021 doc: /* List of functions to call after each text change.
6022 Three arguments are passed to each function: the positions of
6023 the beginning and end of the range of changed text,
6024 and the length in bytes of the pre-change text replaced by that range.
6025 \(For an insertion, the pre-change length is zero;
6026 for a deletion, that length is the number of bytes deleted,
6027 and the post-change beginning and end are at the same place.)
6029 Buffer changes made while executing the `after-change-functions'
6030 don't call any before-change or after-change functions.
6031 That's because these variables are temporarily set to nil.
6032 As a result, a hook function cannot straightforwardly alter the value of
6033 these variables. See the Emacs Lisp manual for a way of
6034 accomplishing an equivalent result by using other variables.
6036 If an unhandled error happens in running these functions,
6037 the variable's value remains nil. That prevents the error
6038 from happening repeatedly and making Emacs nonfunctional. */);
6039 Vafter_change_functions = Qnil;
6041 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6042 doc: /* A list of functions to call before changing a buffer which is unmodified.
6043 The functions are run using the `run-hooks' function. */);
6044 Vfirst_change_hook = Qnil;
6046 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6047 doc: /* List of undo entries in current buffer.
6048 Recent changes come first; older changes follow newer.
6050 An entry (BEG . END) represents an insertion which begins at
6051 position BEG and ends at position END.
6053 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6054 from (abs POSITION). If POSITION is positive, point was at the front
6055 of the text being deleted; if negative, point was at the end.
6057 An entry (t HIGH . LOW) indicates that the buffer previously had
6058 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6059 of the visited file's modification time, as of that time. If the
6060 modification time of the most recent save is different, this entry is
6061 obsolete.
6063 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6064 was modified between BEG and END. PROPERTY is the property name,
6065 and VALUE is the old value.
6067 An entry (apply FUN-NAME . ARGS) means undo the change with
6068 \(apply FUN-NAME ARGS).
6070 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6071 in the active region. BEG and END is the range affected by this entry
6072 and DELTA is the number of bytes added or deleted in that range by
6073 this change.
6075 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6076 was adjusted in position by the offset DISTANCE (an integer).
6078 An entry of the form POSITION indicates that point was at the buffer
6079 location given by the integer. Undoing an entry of this form places
6080 point at POSITION.
6082 Entries with value `nil' mark undo boundaries. The undo command treats
6083 the changes between two undo boundaries as a single step to be undone.
6085 If the value of the variable is t, undo information is not recorded. */);
6087 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6088 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6090 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6091 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6093 Normally, the line-motion functions work by scanning the buffer for
6094 newlines. Columnar operations (like `move-to-column' and
6095 `compute-motion') also work by scanning the buffer, summing character
6096 widths as they go. This works well for ordinary text, but if the
6097 buffer's lines are very long (say, more than 500 characters), these
6098 motion functions will take longer to execute. Emacs may also take
6099 longer to update the display.
6101 If `cache-long-line-scans' is non-nil, these motion functions cache the
6102 results of their scans, and consult the cache to avoid rescanning
6103 regions of the buffer until the text is modified. The caches are most
6104 beneficial when they prevent the most searching---that is, when the
6105 buffer contains long lines and large regions of characters with the
6106 same, fixed screen width.
6108 When `cache-long-line-scans' is non-nil, processing short lines will
6109 become slightly slower (because of the overhead of consulting the
6110 cache), and the caches will use memory roughly proportional to the
6111 number of newlines and characters whose screen width varies.
6113 The caches require no explicit maintenance; their accuracy is
6114 maintained internally by the Emacs primitives. Enabling or disabling
6115 the cache should not affect the behavior of any of the motion
6116 functions; it should only affect their performance. */);
6118 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6119 doc: /* Value of point before the last series of scroll operations, or nil. */);
6121 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6122 doc: /* List of formats to use when saving this buffer.
6123 Formats are defined by `format-alist'. This variable is
6124 set when a file is visited. */);
6126 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6127 &current_buffer->auto_save_file_format, Qnil,
6128 doc: /* *Format in which to write auto-save files.
6129 Should be a list of symbols naming formats that are defined in `format-alist'.
6130 If it is t, which is the default, auto-save files are written in the
6131 same format as a regular save would use. */);
6133 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6134 &current_buffer->invisibility_spec, Qnil,
6135 doc: /* Invisibility spec of this buffer.
6136 The default is t, which means that text is invisible
6137 if it has a non-nil `invisible' property.
6138 If the value is a list, a text character is invisible if its `invisible'
6139 property is an element in that list (or is a list with members in common).
6140 If an element is a cons cell of the form (PROP . ELLIPSIS),
6141 then characters with property value PROP are invisible,
6142 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6144 DEFVAR_PER_BUFFER ("buffer-display-count",
6145 &current_buffer->display_count, Qnil,
6146 doc: /* A number incremented each time this buffer is displayed in a window.
6147 The function `set-window-buffer' increments it. */);
6149 DEFVAR_PER_BUFFER ("buffer-display-time",
6150 &current_buffer->display_time, Qnil,
6151 doc: /* Time stamp updated each time this buffer is displayed in a window.
6152 The function `set-window-buffer' updates this variable
6153 to the value obtained by calling `current-time'.
6154 If the buffer has never been shown in a window, the value is nil. */);
6156 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6157 doc: /* */);
6158 Vtransient_mark_mode = Qnil;
6159 /* The docstring is in simple.el. If we put it here, it would be
6160 overwritten when transient-mark-mode is defined using
6161 define-minor-mode. */
6163 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6164 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6165 If the value is t, disregard `buffer-read-only' and all `read-only'
6166 text properties. If the value is a list, disregard `buffer-read-only'
6167 and disregard a `read-only' text property if the property value
6168 is a member of the list. */);
6169 Vinhibit_read_only = Qnil;
6171 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6172 doc: /* Cursor to use when this buffer is in the selected window.
6173 Values are interpreted as follows:
6175 t use the cursor specified for the frame
6176 nil don't display a cursor
6177 box display a filled box cursor
6178 hollow display a hollow box cursor
6179 bar display a vertical bar cursor with default width
6180 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6181 hbar display a horizontal bar cursor with default height
6182 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6183 ANYTHING ELSE display a hollow box cursor
6185 When the buffer is displayed in a nonselected window,
6186 this variable has no effect; the cursor appears as a hollow box. */);
6188 DEFVAR_PER_BUFFER ("line-spacing",
6189 &current_buffer->extra_line_spacing, Qnil,
6190 doc: /* Additional space to put between lines when displaying a buffer.
6191 The space is measured in pixels, and put below lines on window systems.
6192 If value is a floating point number, it specifies the spacing relative
6193 to the default frame line height. A value of nil means add no extra space. */);
6195 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6196 &current_buffer->cursor_in_non_selected_windows, Qnil,
6197 doc: /* *Cursor type to display in non-selected windows.
6198 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6200 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6201 doc: /* List of functions called with no args to query before killing a buffer.
6202 The buffer being killed will be current while the functions are running.
6203 If any of them returns nil, the buffer is not killed. */);
6204 Vkill_buffer_query_functions = Qnil;
6206 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6207 doc: /* Normal hook run before changing the major mode of a buffer.
6208 The function `kill-all-local-variables' runs this before doing anything else. */);
6209 Vchange_major_mode_hook = Qnil;
6210 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6211 staticpro (&Qchange_major_mode_hook);
6213 defsubr (&Sbuffer_live_p);
6214 defsubr (&Sbuffer_list);
6215 defsubr (&Sget_buffer);
6216 defsubr (&Sget_file_buffer);
6217 defsubr (&Sget_buffer_create);
6218 defsubr (&Smake_indirect_buffer);
6219 defsubr (&Sgenerate_new_buffer_name);
6220 defsubr (&Sbuffer_name);
6221 /*defsubr (&Sbuffer_number);*/
6222 defsubr (&Sbuffer_file_name);
6223 defsubr (&Sbuffer_base_buffer);
6224 defsubr (&Sbuffer_local_value);
6225 defsubr (&Sbuffer_local_variables);
6226 defsubr (&Sbuffer_modified_p);
6227 defsubr (&Sset_buffer_modified_p);
6228 defsubr (&Sbuffer_modified_tick);
6229 defsubr (&Sbuffer_chars_modified_tick);
6230 defsubr (&Srename_buffer);
6231 defsubr (&Sother_buffer);
6232 defsubr (&Sbuffer_enable_undo);
6233 defsubr (&Skill_buffer);
6234 defsubr (&Sset_buffer_major_mode);
6235 defsubr (&Sswitch_to_buffer);
6236 defsubr (&Scurrent_buffer);
6237 defsubr (&Sset_buffer);
6238 defsubr (&Sbarf_if_buffer_read_only);
6239 defsubr (&Sbury_buffer);
6240 defsubr (&Serase_buffer);
6241 defsubr (&Sbuffer_swap_text);
6242 defsubr (&Sset_buffer_multibyte);
6243 defsubr (&Skill_all_local_variables);
6245 defsubr (&Soverlayp);
6246 defsubr (&Smake_overlay);
6247 defsubr (&Sdelete_overlay);
6248 defsubr (&Smove_overlay);
6249 defsubr (&Soverlay_start);
6250 defsubr (&Soverlay_end);
6251 defsubr (&Soverlay_buffer);
6252 defsubr (&Soverlay_properties);
6253 defsubr (&Soverlays_at);
6254 defsubr (&Soverlays_in);
6255 defsubr (&Snext_overlay_change);
6256 defsubr (&Sprevious_overlay_change);
6257 defsubr (&Soverlay_recenter);
6258 defsubr (&Soverlay_lists);
6259 defsubr (&Soverlay_get);
6260 defsubr (&Soverlay_put);
6261 defsubr (&Srestore_buffer_modified_p);
6264 void
6265 keys_of_buffer ()
6267 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6268 initial_define_key (control_x_map, 'k', "kill-buffer");
6270 /* This must not be in syms_of_buffer, because Qdisabled is not
6271 initialized when that function gets called. */
6272 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6275 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6276 (do not change this comment) */