(calendar-faces): New custom group.
[emacs.git] / src / buffer.c
blob8ab1af01f3152698653d545b652d686549cab85c
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include <config.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/param.h>
29 #include <errno.h>
30 #include <stdio.h>
32 #ifndef USE_CRT_DLL
33 extern int errno;
34 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #include "lisp.h"
42 #include "intervals.h"
43 #include "window.h"
44 #include "commands.h"
45 #include "buffer.h"
46 #include "character.h"
47 #include "region-cache.h"
48 #include "indent.h"
49 #include "blockinput.h"
50 #include "keyboard.h"
51 #include "keymap.h"
52 #include "frame.h"
54 struct buffer *current_buffer; /* the current buffer */
56 /* First buffer in chain of all buffers (in reverse order of creation).
57 Threaded through ->next. */
59 struct buffer *all_buffers;
61 /* This structure holds the default values of the buffer-local variables
62 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
63 The default value occupies the same slot in this structure
64 as an individual buffer's value occupies in that buffer.
65 Setting the default value also goes through the alist of buffers
66 and stores into each buffer that does not say it has a local value. */
68 DECL_ALIGN (struct buffer, buffer_defaults);
70 /* A Lisp_Object pointer to the above, used for staticpro */
72 static Lisp_Object Vbuffer_defaults;
74 /* This structure marks which slots in a buffer have corresponding
75 default values in buffer_defaults.
76 Each such slot has a nonzero value in this structure.
77 The value has only one nonzero bit.
79 When a buffer has its own local value for a slot,
80 the entry for that slot (found in the same slot in this structure)
81 is turned on in the buffer's local_flags array.
83 If a slot in this structure is -1, then even though there may
84 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
85 and the corresponding slot in buffer_defaults is not used.
87 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
88 but there is a default value which is copied into each buffer.
90 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91 zero, that is a bug */
93 struct buffer buffer_local_flags;
95 /* This structure holds the names of symbols whose values may be
96 buffer-local. It is indexed and accessed in the same way as the above. */
98 DECL_ALIGN (struct buffer, buffer_local_symbols);
100 /* A Lisp_Object pointer to the above, used for staticpro */
101 static Lisp_Object Vbuffer_local_symbols;
103 /* Flags indicating which built-in buffer-local variables
104 are permanent locals. */
105 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
107 /* Number of per-buffer variables used. */
109 int last_per_buffer_idx;
111 EXFUN (Fset_buffer, 1);
112 void set_buffer_internal P_ ((struct buffer *b));
113 void set_buffer_internal_1 P_ ((struct buffer *b));
114 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
115 int after, Lisp_Object arg1,
116 Lisp_Object arg2, Lisp_Object arg3));
117 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
118 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
120 /* Alist of all buffer names vs the buffers. */
121 /* This used to be a variable, but is no longer,
122 to prevent lossage due to user rplac'ing this alist or its elements. */
123 Lisp_Object Vbuffer_alist;
125 /* Functions to call before and after each text change. */
126 Lisp_Object Vbefore_change_functions;
127 Lisp_Object Vafter_change_functions;
129 Lisp_Object Vtransient_mark_mode;
131 /* t means ignore all read-only text properties.
132 A list means ignore such a property if its value is a member of the list.
133 Any non-nil value means ignore buffer-read-only. */
134 Lisp_Object Vinhibit_read_only;
136 /* List of functions to call that can query about killing a buffer.
137 If any of these functions returns nil, we don't kill it. */
138 Lisp_Object Vkill_buffer_query_functions;
139 Lisp_Object Qkill_buffer_query_functions;
141 /* Hook run before changing a major mode. */
142 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
144 /* List of functions to call before changing an unmodified buffer. */
145 Lisp_Object Vfirst_change_hook;
147 Lisp_Object Qfirst_change_hook;
148 Lisp_Object Qbefore_change_functions;
149 Lisp_Object Qafter_change_functions;
150 Lisp_Object Qucs_set_table_for_input;
152 /* If nonzero, all modification hooks are suppressed. */
153 int inhibit_modification_hooks;
155 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
156 Lisp_Object Qpermanent_local_hook;
158 Lisp_Object Qprotected_field;
160 Lisp_Object QSFundamental; /* A string "Fundamental" */
162 Lisp_Object Qkill_buffer_hook;
164 Lisp_Object Qget_file_buffer;
166 Lisp_Object Qoverlayp;
168 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
170 Lisp_Object Qmodification_hooks;
171 Lisp_Object Qinsert_in_front_hooks;
172 Lisp_Object Qinsert_behind_hooks;
174 static void alloc_buffer_text P_ ((struct buffer *, size_t));
175 static void free_buffer_text P_ ((struct buffer *b));
176 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
177 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
178 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
180 extern char * emacs_strerror P_ ((int));
182 /* For debugging; temporary. See set_buffer_internal. */
183 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
185 void
186 nsberror (spec)
187 Lisp_Object spec;
189 if (STRINGP (spec))
190 error ("No buffer named %s", SDATA (spec));
191 error ("Invalid buffer argument");
194 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
195 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
196 Value is nil if OBJECT is not a buffer or if it has been killed. */)
197 (object)
198 Lisp_Object object;
200 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
201 ? Qt : Qnil);
204 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
205 doc: /* Return a list of all existing live buffers.
206 If the optional arg FRAME is a frame, we return the buffer list
207 in the proper order for that frame: the buffers in FRAME's `buffer-list'
208 frame parameter come first, followed by the rest of the buffers. */)
209 (frame)
210 Lisp_Object frame;
212 Lisp_Object general;
213 general = Fmapcar (Qcdr, Vbuffer_alist);
215 if (FRAMEP (frame))
217 Lisp_Object framelist, prevlist, tail;
218 Lisp_Object args[3];
220 CHECK_FRAME (frame);
222 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
223 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
225 /* Remove from GENERAL any buffer that duplicates one in
226 FRAMELIST or PREVLIST. */
227 tail = framelist;
228 while (CONSP (tail))
230 general = Fdelq (XCAR (tail), general);
231 tail = XCDR (tail);
233 tail = prevlist;
234 while (CONSP (tail))
236 general = Fdelq (XCAR (tail), general);
237 tail = XCDR (tail);
240 args[0] = framelist;
241 args[1] = general;
242 args[2] = prevlist;
243 return Fnconc (3, args);
246 return general;
249 /* Like Fassoc, but use Fstring_equal to compare
250 (which ignores text properties),
251 and don't ever QUIT. */
253 static Lisp_Object
254 assoc_ignore_text_properties (key, list)
255 register Lisp_Object key;
256 Lisp_Object list;
258 register Lisp_Object tail;
259 for (tail = list; CONSP (tail); tail = XCDR (tail))
261 register Lisp_Object elt, tem;
262 elt = XCAR (tail);
263 tem = Fstring_equal (Fcar (elt), key);
264 if (!NILP (tem))
265 return elt;
267 return Qnil;
270 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
271 doc: /* Return the buffer named NAME (a string).
272 If there is no live buffer named NAME, return nil.
273 NAME may also be a buffer; if so, the value is that buffer. */)
274 (name)
275 register Lisp_Object name;
277 if (BUFFERP (name))
278 return name;
279 CHECK_STRING (name);
281 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
284 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
285 doc: /* Return the buffer visiting file FILENAME (a string).
286 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
287 If there is no such live buffer, return nil.
288 See also `find-buffer-visiting'. */)
289 (filename)
290 register Lisp_Object filename;
292 register Lisp_Object tail, buf, tem;
293 Lisp_Object handler;
295 CHECK_STRING (filename);
296 filename = Fexpand_file_name (filename, Qnil);
298 /* If the file name has special constructs in it,
299 call the corresponding file handler. */
300 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
301 if (!NILP (handler))
302 return call2 (handler, Qget_file_buffer, filename);
304 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
306 buf = Fcdr (XCAR (tail));
307 if (!BUFFERP (buf)) continue;
308 if (!STRINGP (XBUFFER (buf)->filename)) continue;
309 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
310 if (!NILP (tem))
311 return buf;
313 return Qnil;
316 Lisp_Object
317 get_truename_buffer (filename)
318 register Lisp_Object filename;
320 register Lisp_Object tail, buf, tem;
322 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
324 buf = Fcdr (XCAR (tail));
325 if (!BUFFERP (buf)) continue;
326 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
327 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
328 if (!NILP (tem))
329 return buf;
331 return Qnil;
334 /* Incremented for each buffer created, to assign the buffer number. */
335 int buffer_count;
337 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
338 doc: /* Return the buffer named NAME, or create such a buffer and return it.
339 A new buffer is created if there is no live buffer named NAME.
340 If NAME starts with a space, the new buffer does not keep undo information.
341 If NAME is a buffer instead of a string, then it is the value returned.
342 The value is never nil. */)
343 (name)
344 register Lisp_Object name;
346 register Lisp_Object buf;
347 register struct buffer *b;
349 buf = Fget_buffer (name);
350 if (!NILP (buf))
351 return buf;
353 if (SCHARS (name) == 0)
354 error ("Empty string for buffer name is not allowed");
356 b = allocate_buffer ();
358 /* An ordinary buffer uses its own struct buffer_text. */
359 b->text = &b->own_text;
360 b->base_buffer = 0;
362 BUF_GAP_SIZE (b) = 20;
363 BLOCK_INPUT;
364 /* We allocate extra 1-byte at the tail and keep it always '\0' for
365 anchoring a search. */
366 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
367 UNBLOCK_INPUT;
368 if (! BUF_BEG_ADDR (b))
369 buffer_memory_full ();
371 BUF_PT (b) = BEG;
372 BUF_GPT (b) = BEG;
373 BUF_BEGV (b) = BEG;
374 BUF_ZV (b) = BEG;
375 BUF_Z (b) = BEG;
376 BUF_PT_BYTE (b) = BEG_BYTE;
377 BUF_GPT_BYTE (b) = BEG_BYTE;
378 BUF_BEGV_BYTE (b) = BEG_BYTE;
379 BUF_ZV_BYTE (b) = BEG_BYTE;
380 BUF_Z_BYTE (b) = BEG_BYTE;
381 BUF_MODIFF (b) = 1;
382 BUF_CHARS_MODIFF (b) = 1;
383 BUF_OVERLAY_MODIFF (b) = 1;
384 BUF_SAVE_MODIFF (b) = 1;
385 BUF_INTERVALS (b) = 0;
386 BUF_UNCHANGED_MODIFIED (b) = 1;
387 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
388 BUF_END_UNCHANGED (b) = 0;
389 BUF_BEG_UNCHANGED (b) = 0;
390 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
392 b->newline_cache = 0;
393 b->width_run_cache = 0;
394 b->width_table = Qnil;
395 b->prevent_redisplay_optimizations_p = 1;
397 /* Put this on the chain of all buffers including killed ones. */
398 b->next = all_buffers;
399 all_buffers = b;
401 /* An ordinary buffer normally doesn't need markers
402 to handle BEGV and ZV. */
403 b->pt_marker = Qnil;
404 b->begv_marker = Qnil;
405 b->zv_marker = Qnil;
407 name = Fcopy_sequence (name);
408 STRING_SET_INTERVALS (name, NULL_INTERVAL);
409 b->name = name;
411 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
413 reset_buffer (b);
414 reset_buffer_local_variables (b, 1);
416 b->mark = Fmake_marker ();
417 BUF_MARKERS (b) = NULL;
418 b->name = name;
420 /* Put this in the alist of all live buffers. */
421 XSETBUFFER (buf, b);
422 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
424 /* An error in calling the function here (should someone redefine it)
425 can lead to infinite regress until you run out of stack. rms
426 says that's not worth protecting against. */
427 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
428 /* buf is on buffer-alist, so no gcpro. */
429 call1 (Qucs_set_table_for_input, buf);
431 return buf;
435 /* Return a list of overlays which is a copy of the overlay list
436 LIST, but for buffer B. */
438 static struct Lisp_Overlay *
439 copy_overlays (b, list)
440 struct buffer *b;
441 struct Lisp_Overlay *list;
443 Lisp_Object buffer;
444 struct Lisp_Overlay *result = NULL, *tail = NULL;
446 XSETBUFFER (buffer, b);
448 for (; list; list = list->next)
450 Lisp_Object overlay, start, end, old_overlay;
451 EMACS_INT charpos;
453 XSETMISC (old_overlay, list);
454 charpos = marker_position (OVERLAY_START (old_overlay));
455 start = Fmake_marker ();
456 Fset_marker (start, make_number (charpos), buffer);
457 XMARKER (start)->insertion_type
458 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
460 charpos = marker_position (OVERLAY_END (old_overlay));
461 end = Fmake_marker ();
462 Fset_marker (end, make_number (charpos), buffer);
463 XMARKER (end)->insertion_type
464 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
466 overlay = allocate_misc ();
467 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
468 OVERLAY_START (overlay) = start;
469 OVERLAY_END (overlay) = end;
470 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
471 XOVERLAY (overlay)->next = NULL;
473 if (tail)
474 tail = tail->next = XOVERLAY (overlay);
475 else
476 result = tail = XOVERLAY (overlay);
479 return result;
483 /* Clone per-buffer values of buffer FROM.
485 Buffer TO gets the same per-buffer values as FROM, with the
486 following exceptions: (1) TO's name is left untouched, (2) markers
487 are copied and made to refer to TO, and (3) overlay lists are
488 copied. */
490 static void
491 clone_per_buffer_values (from, to)
492 struct buffer *from, *to;
494 Lisp_Object to_buffer;
495 int offset;
497 XSETBUFFER (to_buffer, to);
499 /* buffer-local Lisp variables start at `undo_list',
500 tho only the ones from `name' on are GC'd normally. */
501 for (offset = PER_BUFFER_VAR_OFFSET (undo_list) + sizeof (Lisp_Object);
502 offset < sizeof *to;
503 offset += sizeof (Lisp_Object))
505 Lisp_Object obj;
507 obj = PER_BUFFER_VALUE (from, offset);
508 if (MARKERP (obj))
510 struct Lisp_Marker *m = XMARKER (obj);
511 obj = Fmake_marker ();
512 XMARKER (obj)->insertion_type = m->insertion_type;
513 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
516 PER_BUFFER_VALUE (to, offset) = obj;
519 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
521 to->overlays_before = copy_overlays (to, from->overlays_before);
522 to->overlays_after = copy_overlays (to, from->overlays_after);
524 /* Get (a copy of) the alist of Lisp-level local variables of FROM
525 and install that in TO. */
526 to->local_var_alist = buffer_lisp_local_variables (from);
529 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
530 2, 3,
531 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
532 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
533 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
534 NAME should be a string which is not the name of an existing buffer.
535 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
536 such as major and minor modes, in the indirect buffer.
537 CLONE nil means the indirect buffer's state is reset to default values. */)
538 (base_buffer, name, clone)
539 Lisp_Object base_buffer, name, clone;
541 Lisp_Object buf, tem;
542 struct buffer *b;
544 CHECK_STRING (name);
545 buf = Fget_buffer (name);
546 if (!NILP (buf))
547 error ("Buffer name `%s' is in use", SDATA (name));
549 tem = base_buffer;
550 base_buffer = Fget_buffer (base_buffer);
551 if (NILP (base_buffer))
552 error ("No such buffer: `%s'", SDATA (tem));
553 if (NILP (XBUFFER (base_buffer)->name))
554 error ("Base buffer has been killed");
556 if (SCHARS (name) == 0)
557 error ("Empty string for buffer name is not allowed");
559 b = allocate_buffer ();
561 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
562 ? XBUFFER (base_buffer)->base_buffer
563 : XBUFFER (base_buffer));
565 /* Use the base buffer's text object. */
566 b->text = b->base_buffer->text;
568 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
569 BUF_ZV (b) = BUF_ZV (b->base_buffer);
570 BUF_PT (b) = BUF_PT (b->base_buffer);
571 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
572 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
573 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
575 b->newline_cache = 0;
576 b->width_run_cache = 0;
577 b->width_table = Qnil;
579 /* Put this on the chain of all buffers including killed ones. */
580 b->next = all_buffers;
581 all_buffers = b;
583 name = Fcopy_sequence (name);
584 STRING_SET_INTERVALS (name, NULL_INTERVAL);
585 b->name = name;
587 reset_buffer (b);
588 reset_buffer_local_variables (b, 1);
590 /* Put this in the alist of all live buffers. */
591 XSETBUFFER (buf, b);
592 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
594 b->mark = Fmake_marker ();
595 b->name = name;
597 /* The multibyte status belongs to the base buffer. */
598 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
600 /* Make sure the base buffer has markers for its narrowing. */
601 if (NILP (b->base_buffer->pt_marker))
603 b->base_buffer->pt_marker = Fmake_marker ();
604 set_marker_both (b->base_buffer->pt_marker, base_buffer,
605 BUF_PT (b->base_buffer),
606 BUF_PT_BYTE (b->base_buffer));
608 if (NILP (b->base_buffer->begv_marker))
610 b->base_buffer->begv_marker = Fmake_marker ();
611 set_marker_both (b->base_buffer->begv_marker, base_buffer,
612 BUF_BEGV (b->base_buffer),
613 BUF_BEGV_BYTE (b->base_buffer));
615 if (NILP (b->base_buffer->zv_marker))
617 b->base_buffer->zv_marker = Fmake_marker ();
618 set_marker_both (b->base_buffer->zv_marker, base_buffer,
619 BUF_ZV (b->base_buffer),
620 BUF_ZV_BYTE (b->base_buffer));
621 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
624 if (NILP (clone))
626 /* Give the indirect buffer markers for its narrowing. */
627 b->pt_marker = Fmake_marker ();
628 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
629 b->begv_marker = Fmake_marker ();
630 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
631 b->zv_marker = Fmake_marker ();
632 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
633 XMARKER (b->zv_marker)->insertion_type = 1;
635 else
637 struct buffer *old_b = current_buffer;
639 clone_per_buffer_values (b->base_buffer, b);
640 b->filename = Qnil;
641 b->file_truename = Qnil;
642 b->display_count = make_number (0);
643 b->backed_up = Qnil;
644 b->auto_save_file_name = Qnil;
645 set_buffer_internal_1 (b);
646 Fset (intern ("buffer-save-without-query"), Qnil);
647 Fset (intern ("buffer-file-number"), Qnil);
648 Fset (intern ("buffer-stale-function"), Qnil);
649 set_buffer_internal_1 (old_b);
652 return buf;
655 void
656 delete_all_overlays (b)
657 struct buffer *b;
659 Lisp_Object overlay;
661 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
662 have to empty the list, otherwise we end up with overlays that
663 think they belong to this buffer while the buffer doesn't know about
664 them any more. */
665 while (b->overlays_before)
667 XSETMISC (overlay, b->overlays_before);
668 Fdelete_overlay (overlay);
670 while (b->overlays_after)
672 XSETMISC (overlay, b->overlays_after);
673 Fdelete_overlay (overlay);
675 eassert (b->overlays_before == NULL);
676 eassert (b->overlays_after == NULL);
679 /* Reinitialize everything about a buffer except its name and contents
680 and local variables.
681 If called on an already-initialized buffer, the list of overlays
682 should be deleted before calling this function, otherwise we end up
683 with overlays that claim to belong to the buffer but the buffer
684 claims it doesn't belong to it. */
686 void
687 reset_buffer (b)
688 register struct buffer *b;
690 b->filename = Qnil;
691 b->file_truename = Qnil;
692 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
693 b->modtime = 0;
694 XSETFASTINT (b->save_length, 0);
695 b->last_window_start = 1;
696 /* It is more conservative to start out "changed" than "unchanged". */
697 b->clip_changed = 0;
698 b->prevent_redisplay_optimizations_p = 1;
699 b->backed_up = Qnil;
700 b->auto_save_modified = 0;
701 b->auto_save_failure_time = -1;
702 b->auto_save_file_name = Qnil;
703 b->read_only = Qnil;
704 b->overlays_before = NULL;
705 b->overlays_after = NULL;
706 b->overlay_center = BEG;
707 b->mark_active = Qnil;
708 b->point_before_scroll = Qnil;
709 b->file_format = Qnil;
710 b->auto_save_file_format = Qt;
711 b->last_selected_window = Qnil;
712 XSETINT (b->display_count, 0);
713 b->display_time = Qnil;
714 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
715 b->cursor_type = buffer_defaults.cursor_type;
716 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
718 b->display_error_modiff = 0;
721 /* Reset buffer B's local variables info.
722 Don't use this on a buffer that has already been in use;
723 it does not treat permanent locals consistently.
724 Instead, use Fkill_all_local_variables.
726 If PERMANENT_TOO is 1, then we reset permanent
727 buffer-local variables. If PERMANENT_TOO is 0,
728 we preserve those. */
730 static void
731 reset_buffer_local_variables (b, permanent_too)
732 register struct buffer *b;
733 int permanent_too;
735 register int offset;
736 int i;
738 /* Reset the major mode to Fundamental, together with all the
739 things that depend on the major mode.
740 default-major-mode is handled at a higher level.
741 We ignore it here. */
742 b->major_mode = Qfundamental_mode;
743 b->keymap = Qnil;
744 b->mode_name = QSFundamental;
745 b->minor_modes = Qnil;
747 /* If the standard case table has been altered and invalidated,
748 fix up its insides first. */
749 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
750 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
751 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
752 Fset_standard_case_table (Vascii_downcase_table);
754 b->downcase_table = Vascii_downcase_table;
755 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
756 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
757 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
758 b->invisibility_spec = Qt;
759 #ifndef DOS_NT
760 b->buffer_file_type = Qnil;
761 #endif
763 /* Reset all (or most) per-buffer variables to their defaults. */
764 if (permanent_too)
765 b->local_var_alist = Qnil;
766 else
768 Lisp_Object tmp, prop, last = Qnil;
769 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
770 if (CONSP (XCAR (tmp))
771 && SYMBOLP (XCAR (XCAR (tmp)))
772 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
774 /* If permanent-local, keep it. */
775 last = tmp;
776 if (EQ (prop, Qpermanent_local_hook))
778 /* This is a partially permanent hook variable.
779 Preserve only the elements that want to be preserved. */
780 Lisp_Object list, newlist;
781 list = XCDR (XCAR (tmp));
782 if (!CONSP (list))
783 newlist = list;
784 else
785 for (newlist = Qnil; CONSP (list); list = XCDR (list))
787 Lisp_Object elt = XCAR (list);
788 /* Preserve element ELT if it's t,
789 if it is a function with a `permanent-local-hook' property,
790 or if it's not a symbol. */
791 if (! SYMBOLP (elt)
792 || EQ (elt, Qt)
793 || !NILP (Fget (elt, Qpermanent_local_hook)))
794 newlist = Fcons (elt, newlist);
796 XSETCDR (XCAR (tmp), Fnreverse (newlist));
799 /* Delete this local variable. */
800 else if (NILP (last))
801 b->local_var_alist = XCDR (tmp);
802 else
803 XSETCDR (last, XCDR (tmp));
806 for (i = 0; i < last_per_buffer_idx; ++i)
807 if (permanent_too || buffer_permanent_local_flags[i] == 0)
808 SET_PER_BUFFER_VALUE_P (b, i, 0);
810 /* For each slot that has a default value,
811 copy that into the slot. */
813 /* buffer-local Lisp variables start at `undo_list',
814 tho only the ones from `name' on are GC'd normally. */
815 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
816 offset < sizeof *b;
817 offset += sizeof (Lisp_Object))
819 int idx = PER_BUFFER_IDX (offset);
820 if ((idx > 0
821 && (permanent_too
822 || buffer_permanent_local_flags[idx] == 0))
823 /* Is -2 used anywhere? */
824 || idx == -2)
825 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
829 /* We split this away from generate-new-buffer, because rename-buffer
830 and set-visited-file-name ought to be able to use this to really
831 rename the buffer properly. */
833 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
834 1, 2, 0,
835 doc: /* Return a string that is the name of no existing buffer based on NAME.
836 If there is no live buffer named NAME, then return NAME.
837 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
838 \(starting at 2) until an unused name is found, and then return that name.
839 Optional second argument IGNORE specifies a name that is okay to use (if
840 it is in the sequence to be tried) even if a buffer with that name exists. */)
841 (name, ignore)
842 register Lisp_Object name, ignore;
844 register Lisp_Object gentemp, tem;
845 int count;
846 char number[10];
848 CHECK_STRING (name);
850 tem = Fstring_equal (name, ignore);
851 if (!NILP (tem))
852 return name;
853 tem = Fget_buffer (name);
854 if (NILP (tem))
855 return name;
857 count = 1;
858 while (1)
860 sprintf (number, "<%d>", ++count);
861 gentemp = concat2 (name, build_string (number));
862 tem = Fstring_equal (gentemp, ignore);
863 if (!NILP (tem))
864 return gentemp;
865 tem = Fget_buffer (gentemp);
866 if (NILP (tem))
867 return gentemp;
872 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
873 doc: /* Return the name of BUFFER, as a string.
874 With no argument or nil as argument, return the name of the current buffer. */)
875 (buffer)
876 register Lisp_Object buffer;
878 if (NILP (buffer))
879 return current_buffer->name;
880 CHECK_BUFFER (buffer);
881 return XBUFFER (buffer)->name;
884 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
885 doc: /* Return name of file BUFFER is visiting, or nil if none.
886 No argument or nil as argument means use the current buffer. */)
887 (buffer)
888 register Lisp_Object buffer;
890 if (NILP (buffer))
891 return current_buffer->filename;
892 CHECK_BUFFER (buffer);
893 return XBUFFER (buffer)->filename;
896 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
897 0, 1, 0,
898 doc: /* Return the base buffer of indirect buffer BUFFER.
899 If BUFFER is not indirect, return nil.
900 BUFFER defaults to the current buffer. */)
901 (buffer)
902 register Lisp_Object buffer;
904 struct buffer *base;
905 Lisp_Object base_buffer;
907 if (NILP (buffer))
908 base = current_buffer->base_buffer;
909 else
911 CHECK_BUFFER (buffer);
912 base = XBUFFER (buffer)->base_buffer;
915 if (! base)
916 return Qnil;
917 XSETBUFFER (base_buffer, base);
918 return base_buffer;
921 DEFUN ("buffer-local-value", Fbuffer_local_value,
922 Sbuffer_local_value, 2, 2, 0,
923 doc: /* Return the value of VARIABLE in BUFFER.
924 If VARIABLE does not have a buffer-local binding in BUFFER, the value
925 is the default binding of the variable. */)
926 (variable, buffer)
927 register Lisp_Object variable;
928 register Lisp_Object buffer;
930 register struct buffer *buf;
931 register Lisp_Object result;
933 CHECK_SYMBOL (variable);
934 CHECK_BUFFER (buffer);
935 buf = XBUFFER (buffer);
937 variable = indirect_variable (variable);
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 = SYMBOL_VALUE (variable);
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, "BSwitch to buffer: ",
1771 doc: /* Select buffer BUFFER in the current window.
1772 If BUFFER does not identify an existing buffer,
1773 then this function creates a buffer with that name.
1775 When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1776 or nil. If BUFFER is nil, then this function chooses a buffer
1777 using `other-buffer'.
1778 Optional second arg NORECORD non-nil means
1779 do not put this buffer at the front of the list of recently selected ones.
1780 This function returns the buffer it switched to.
1782 WARNING: This is NOT the way to work on another buffer temporarily
1783 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1784 the window-buffer correspondences. */)
1785 (buffer, norecord)
1786 Lisp_Object buffer, norecord;
1788 char *err;
1790 if (EQ (buffer, Fwindow_buffer (selected_window)))
1792 /* Basically a NOP. Avoid signalling an error in the case where
1793 the selected window is dedicated, or a minibuffer. */
1795 /* But do put this buffer at the front of the buffer list,
1796 unless that has been inhibited. Note that even if
1797 BUFFER is at the front of the main buffer-list already,
1798 we still want to move it to the front of the frame's buffer list. */
1799 if (NILP (norecord))
1800 record_buffer (buffer);
1801 return Fset_buffer (buffer);
1804 err = no_switch_window (selected_window);
1805 if (err)
1806 /* If can't display in current window, let pop-to-buffer
1807 try some other window. */
1808 return call3 (intern ("pop-to-buffer"), buffer, Qnil, norecord);
1810 return switch_to_buffer_1 (buffer, norecord);
1813 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1814 doc: /* Select buffer BUFFER in some window, preferably a different one.
1815 BUFFER may be a buffer, a string \(a buffer name), or nil.
1816 If BUFFER is a string which is not the name of an existing buffer,
1817 then this function creates a buffer with that name.
1818 If BUFFER is nil, then it chooses some other buffer.
1819 If `pop-up-windows' is non-nil, windows can be split to do this.
1820 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1821 window even if BUFFER is already visible in the selected window,
1822 and ignore `same-window-regexps' and `same-window-buffer-names'.
1823 This function returns the buffer it switched to.
1824 This uses the function `display-buffer' as a subroutine; see the documentation
1825 of `display-buffer' for additional customization information.
1827 Optional third arg NORECORD non-nil means
1828 do not put this buffer at the front of the list of recently selected ones. */)
1829 (buffer, other_window, norecord)
1830 Lisp_Object buffer, other_window, norecord;
1832 register Lisp_Object buf;
1833 if (NILP (buffer))
1834 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1835 else
1837 buf = Fget_buffer (buffer);
1838 if (NILP (buf))
1840 buf = Fget_buffer_create (buffer);
1841 Fset_buffer_major_mode (buf);
1844 Fset_buffer (buf);
1845 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1846 return buf;
1849 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1850 doc: /* Return the current buffer as a Lisp object. */)
1853 register Lisp_Object buf;
1854 XSETBUFFER (buf, current_buffer);
1855 return buf;
1858 /* Set the current buffer to B.
1860 We previously set windows_or_buffers_changed here to invalidate
1861 global unchanged information in beg_unchanged and end_unchanged.
1862 This is no longer necessary because we now compute unchanged
1863 information on a buffer-basis. Every action affecting other
1864 windows than the selected one requires a select_window at some
1865 time, and that increments windows_or_buffers_changed. */
1867 void
1868 set_buffer_internal (b)
1869 register struct buffer *b;
1871 if (current_buffer != b)
1872 set_buffer_internal_1 (b);
1875 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1876 This is used by redisplay. */
1878 void
1879 set_buffer_internal_1 (b)
1880 register struct buffer *b;
1882 register struct buffer *old_buf;
1883 register Lisp_Object tail, valcontents;
1884 Lisp_Object tem;
1886 #ifdef USE_MMAP_FOR_BUFFERS
1887 if (b->text->beg == NULL)
1888 enlarge_buffer_text (b, 0);
1889 #endif /* USE_MMAP_FOR_BUFFERS */
1891 if (current_buffer == b)
1892 return;
1894 old_buf = current_buffer;
1895 current_buffer = b;
1896 last_known_column_point = -1; /* invalidate indentation cache */
1898 if (old_buf)
1900 /* Put the undo list back in the base buffer, so that it appears
1901 that an indirect buffer shares the undo list of its base. */
1902 if (old_buf->base_buffer)
1903 old_buf->base_buffer->undo_list = old_buf->undo_list;
1905 /* If the old current buffer has markers to record PT, BEGV and ZV
1906 when it is not current, update them now. */
1907 if (! NILP (old_buf->pt_marker))
1909 Lisp_Object obuf;
1910 XSETBUFFER (obuf, old_buf);
1911 set_marker_both (old_buf->pt_marker, obuf,
1912 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1914 if (! NILP (old_buf->begv_marker))
1916 Lisp_Object obuf;
1917 XSETBUFFER (obuf, old_buf);
1918 set_marker_both (old_buf->begv_marker, obuf,
1919 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1921 if (! NILP (old_buf->zv_marker))
1923 Lisp_Object obuf;
1924 XSETBUFFER (obuf, old_buf);
1925 set_marker_both (old_buf->zv_marker, obuf,
1926 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1930 /* Get the undo list from the base buffer, so that it appears
1931 that an indirect buffer shares the undo list of its base. */
1932 if (b->base_buffer)
1933 b->undo_list = b->base_buffer->undo_list;
1935 /* If the new current buffer has markers to record PT, BEGV and ZV
1936 when it is not current, fetch them now. */
1937 if (! NILP (b->pt_marker))
1939 BUF_PT (b) = marker_position (b->pt_marker);
1940 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1942 if (! NILP (b->begv_marker))
1944 BUF_BEGV (b) = marker_position (b->begv_marker);
1945 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1947 if (! NILP (b->zv_marker))
1949 BUF_ZV (b) = marker_position (b->zv_marker);
1950 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1953 /* Look down buffer's list of local Lisp variables
1954 to find and update any that forward into C variables. */
1956 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1958 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1959 if ((BUFFER_LOCAL_VALUEP (valcontents))
1960 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1961 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1962 /* Just reference the variable
1963 to cause it to become set for this buffer. */
1964 Fsymbol_value (XCAR (XCAR (tail)));
1967 /* Do the same with any others that were local to the previous buffer */
1969 if (old_buf)
1970 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1972 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1973 if ((BUFFER_LOCAL_VALUEP (valcontents))
1974 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1975 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1976 /* Just reference the variable
1977 to cause it to become set for this buffer. */
1978 Fsymbol_value (XCAR (XCAR (tail)));
1982 /* Switch to buffer B temporarily for redisplay purposes.
1983 This avoids certain things that don't need to be done within redisplay. */
1985 void
1986 set_buffer_temp (b)
1987 struct buffer *b;
1989 register struct buffer *old_buf;
1991 if (current_buffer == b)
1992 return;
1994 old_buf = current_buffer;
1995 current_buffer = b;
1997 if (old_buf)
1999 /* If the old current buffer has markers to record PT, BEGV and ZV
2000 when it is not current, update them now. */
2001 if (! NILP (old_buf->pt_marker))
2003 Lisp_Object obuf;
2004 XSETBUFFER (obuf, old_buf);
2005 set_marker_both (old_buf->pt_marker, obuf,
2006 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
2008 if (! NILP (old_buf->begv_marker))
2010 Lisp_Object obuf;
2011 XSETBUFFER (obuf, old_buf);
2012 set_marker_both (old_buf->begv_marker, obuf,
2013 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
2015 if (! NILP (old_buf->zv_marker))
2017 Lisp_Object obuf;
2018 XSETBUFFER (obuf, old_buf);
2019 set_marker_both (old_buf->zv_marker, obuf,
2020 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
2024 /* If the new current buffer has markers to record PT, BEGV and ZV
2025 when it is not current, fetch them now. */
2026 if (! NILP (b->pt_marker))
2028 BUF_PT (b) = marker_position (b->pt_marker);
2029 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
2031 if (! NILP (b->begv_marker))
2033 BUF_BEGV (b) = marker_position (b->begv_marker);
2034 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2036 if (! NILP (b->zv_marker))
2038 BUF_ZV (b) = marker_position (b->zv_marker);
2039 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2043 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2044 doc: /* Make the buffer BUFFER current for editing operations.
2045 BUFFER may be a buffer or the name of an existing buffer.
2046 See also `save-excursion' when you want to make a buffer current temporarily.
2047 This function does not display the buffer, so its effect ends
2048 when the current command terminates.
2049 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
2050 (buffer)
2051 register Lisp_Object buffer;
2053 register Lisp_Object buf;
2054 buf = Fget_buffer (buffer);
2055 if (NILP (buf))
2056 nsberror (buffer);
2057 if (NILP (XBUFFER (buf)->name))
2058 error ("Selecting deleted buffer");
2059 set_buffer_internal (XBUFFER (buf));
2060 return buf;
2063 /* Set the current buffer to BUFFER provided it is alive. */
2065 Lisp_Object
2066 set_buffer_if_live (buffer)
2067 Lisp_Object buffer;
2069 if (! NILP (XBUFFER (buffer)->name))
2070 Fset_buffer (buffer);
2071 return Qnil;
2074 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2075 Sbarf_if_buffer_read_only, 0, 0, 0,
2076 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2079 if (!NILP (current_buffer->read_only)
2080 && NILP (Vinhibit_read_only))
2081 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2082 return Qnil;
2085 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2086 doc: /* Put BUFFER at the end of the list of all buffers.
2087 There it is the least likely candidate for `other-buffer' to return;
2088 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
2089 You can specify a buffer name as BUFFER, or an actual buffer object.
2090 If BUFFER is nil or omitted, bury the current buffer.
2091 Also, if BUFFER is nil or omitted, remove the current buffer from the
2092 selected window if it is displayed there. */)
2093 (buffer)
2094 register Lisp_Object buffer;
2096 /* Figure out what buffer we're going to bury. */
2097 if (NILP (buffer))
2099 Lisp_Object tem;
2100 XSETBUFFER (buffer, current_buffer);
2102 tem = Fwindow_buffer (selected_window);
2103 /* If we're burying the current buffer, unshow it. */
2104 if (EQ (buffer, tem))
2106 if (NILP (Fwindow_dedicated_p (selected_window)))
2107 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2108 else if (NILP (XWINDOW (selected_window)->parent))
2109 Ficonify_frame (Fwindow_frame (selected_window));
2110 else
2111 Fdelete_window (selected_window);
2114 else
2116 Lisp_Object buf1;
2118 buf1 = Fget_buffer (buffer);
2119 if (NILP (buf1))
2120 nsberror (buffer);
2121 buffer = buf1;
2124 /* Move buffer to the end of the buffer list. Do nothing if the
2125 buffer is killed. */
2126 if (!NILP (XBUFFER (buffer)->name))
2128 Lisp_Object aelt, link;
2130 aelt = Frassq (buffer, Vbuffer_alist);
2131 link = Fmemq (aelt, Vbuffer_alist);
2132 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2133 XSETCDR (link, Qnil);
2134 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2136 XFRAME (selected_frame)->buffer_list
2137 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2138 XFRAME (selected_frame)->buried_buffer_list
2139 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2142 return Qnil;
2145 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2146 doc: /* Delete the entire contents of the current buffer.
2147 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2148 so the buffer is truly empty after this. */)
2151 Fwiden ();
2153 del_range (BEG, Z);
2155 current_buffer->last_window_start = 1;
2156 /* Prevent warnings, or suspension of auto saving, that would happen
2157 if future size is less than past size. Use of erase-buffer
2158 implies that the future text is not really related to the past text. */
2159 XSETFASTINT (current_buffer->save_length, 0);
2160 return Qnil;
2163 void
2164 validate_region (b, e)
2165 register Lisp_Object *b, *e;
2167 CHECK_NUMBER_COERCE_MARKER (*b);
2168 CHECK_NUMBER_COERCE_MARKER (*e);
2170 if (XINT (*b) > XINT (*e))
2172 Lisp_Object tem;
2173 tem = *b; *b = *e; *e = tem;
2176 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2177 && XINT (*e) <= ZV))
2178 args_out_of_range (*b, *e);
2181 /* Advance BYTE_POS up to a character boundary
2182 and return the adjusted position. */
2184 static int
2185 advance_to_char_boundary (byte_pos)
2186 int byte_pos;
2188 int c;
2190 if (byte_pos == BEG)
2191 /* Beginning of buffer is always a character boundary. */
2192 return BEG;
2194 c = FETCH_BYTE (byte_pos);
2195 if (! CHAR_HEAD_P (c))
2197 /* We should advance BYTE_POS only when C is a constituent of a
2198 multibyte sequence. */
2199 int orig_byte_pos = byte_pos;
2203 byte_pos--;
2204 c = FETCH_BYTE (byte_pos);
2206 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2207 INC_POS (byte_pos);
2208 if (byte_pos < orig_byte_pos)
2209 byte_pos = orig_byte_pos;
2210 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2211 surely advance to the correct character boundary. If C is
2212 not, BYTE_POS was unchanged. */
2215 return byte_pos;
2218 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2219 1, 1, 0,
2220 doc: /* Swap the text between current buffer and BUFFER. */)
2221 (buffer)
2222 Lisp_Object buffer;
2224 struct buffer *other_buffer;
2225 CHECK_BUFFER (buffer);
2226 other_buffer = XBUFFER (buffer);
2228 /* Actually, it probably works just fine.
2229 * if (other_buffer == current_buffer)
2230 * error ("Cannot swap a buffer's text with itself"); */
2232 /* Actually, this may be workable as well, tho probably only if they're
2233 *both* indirect. */
2234 if (other_buffer->base_buffer
2235 || current_buffer->base_buffer)
2236 error ("Cannot swap indirect buffers's text");
2238 { /* This is probably harder to make work. */
2239 struct buffer *other;
2240 for (other = all_buffers; other; other = other->next)
2241 if (other->base_buffer == other_buffer
2242 || other->base_buffer == current_buffer)
2243 error ("One of the buffers to swap has indirect buffers");
2246 #define swapfield(field, type) \
2247 do { \
2248 type tmp##field = other_buffer->field; \
2249 other_buffer->field = current_buffer->field; \
2250 current_buffer->field = tmp##field; \
2251 } while (0)
2253 swapfield (own_text, struct buffer_text);
2254 eassert (current_buffer->text == &current_buffer->own_text);
2255 eassert (other_buffer->text == &other_buffer->own_text);
2256 swapfield (pt, EMACS_INT);
2257 swapfield (pt_byte, EMACS_INT);
2258 swapfield (begv, EMACS_INT);
2259 swapfield (begv_byte, EMACS_INT);
2260 swapfield (zv, EMACS_INT);
2261 swapfield (zv_byte, EMACS_INT);
2262 eassert (!current_buffer->base_buffer);
2263 eassert (!other_buffer->base_buffer);
2264 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2265 swapfield (newline_cache, struct region_cache *);
2266 swapfield (width_run_cache, struct region_cache *);
2267 current_buffer->prevent_redisplay_optimizations_p = 1;
2268 other_buffer->prevent_redisplay_optimizations_p = 1;
2269 swapfield (overlays_before, struct Lisp_Overlay *);
2270 swapfield (overlays_after, struct Lisp_Overlay *);
2271 swapfield (overlay_center, EMACS_INT);
2272 swapfield (undo_list, Lisp_Object);
2273 swapfield (mark, Lisp_Object);
2274 if (MARKERP (current_buffer->mark) && XMARKER (current_buffer->mark)->buffer)
2275 XMARKER (current_buffer->mark)->buffer = current_buffer;
2276 if (MARKERP (other_buffer->mark) && XMARKER (other_buffer->mark)->buffer)
2277 XMARKER (other_buffer->mark)->buffer = other_buffer;
2278 swapfield (enable_multibyte_characters, Lisp_Object);
2279 /* FIXME: Not sure what we should do with these *_marker fields.
2280 Hopefully they're just nil anyway. */
2281 swapfield (pt_marker, Lisp_Object);
2282 swapfield (begv_marker, Lisp_Object);
2283 swapfield (zv_marker, Lisp_Object);
2284 current_buffer->point_before_scroll = Qnil;
2285 other_buffer->point_before_scroll = Qnil;
2287 current_buffer->text->modiff++; other_buffer->text->modiff++;
2288 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2289 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2290 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2291 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2292 other_buffer->text->beg_unchanged = current_buffer->text->gpt;
2293 other_buffer->text->end_unchanged = current_buffer->text->gpt;
2295 struct Lisp_Marker *m;
2296 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2297 if (m->buffer == other_buffer)
2298 m->buffer = current_buffer;
2299 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2300 if (m->buffer == current_buffer)
2301 m->buffer = other_buffer;
2303 if (current_buffer->text->intervals)
2304 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2305 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2306 if (other_buffer->text->intervals)
2307 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2308 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2310 return Qnil;
2313 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2314 1, 1, 0,
2315 doc: /* Set the multibyte flag of the current buffer to FLAG.
2316 If FLAG is t, this makes the buffer a multibyte buffer.
2317 If FLAG is nil, this makes the buffer a single-byte buffer.
2318 In these cases, the buffer contents remain unchanged as a sequence of
2319 bytes but the contents viewed as characters do change.
2320 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2321 all eight-bit bytes to eight-bit characters.
2322 If the multibyte flag was really changed, undo information of the
2323 current buffer is cleared. */)
2324 (flag)
2325 Lisp_Object flag;
2327 struct Lisp_Marker *tail, *markers;
2328 struct buffer *other;
2329 int begv, zv;
2330 int narrowed = (BEG != BEGV || Z != ZV);
2331 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2332 Lisp_Object old_undo = current_buffer->undo_list;
2333 struct gcpro gcpro1;
2335 if (current_buffer->base_buffer)
2336 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2338 /* Do nothing if nothing actually changes. */
2339 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2340 return flag;
2342 GCPRO1 (old_undo);
2344 /* Don't record these buffer changes. We will put a special undo entry
2345 instead. */
2346 current_buffer->undo_list = Qt;
2348 /* If the cached position is for this buffer, clear it out. */
2349 clear_charpos_cache (current_buffer);
2351 if (NILP (flag))
2352 begv = BEGV_BYTE, zv = ZV_BYTE;
2353 else
2354 begv = BEGV, zv = ZV;
2356 if (narrowed)
2357 Fwiden ();
2359 if (NILP (flag))
2361 int pos, stop;
2362 unsigned char *p;
2364 /* Do this first, so it can use CHAR_TO_BYTE
2365 to calculate the old correspondences. */
2366 set_intervals_multibyte (0);
2368 current_buffer->enable_multibyte_characters = Qnil;
2370 Z = Z_BYTE;
2371 BEGV = BEGV_BYTE;
2372 ZV = ZV_BYTE;
2373 GPT = GPT_BYTE;
2374 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2377 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2378 tail->charpos = tail->bytepos;
2380 /* Convert multibyte form of 8-bit characters to unibyte. */
2381 pos = BEG;
2382 stop = GPT;
2383 p = BEG_ADDR;
2384 while (1)
2386 int c, bytes;
2388 if (pos == stop)
2390 if (pos == Z)
2391 break;
2392 p = GAP_END_ADDR;
2393 stop = Z;
2395 if (ASCII_BYTE_P (*p))
2396 p++, pos++;
2397 else if (CHAR_BYTE8_HEAD_P (*p))
2399 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
2400 /* Delete all bytes for this 8-bit character but the
2401 last one, and change the last one to the charcter
2402 code. */
2403 bytes--;
2404 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2405 p = GAP_END_ADDR;
2406 *p++ = c;
2407 pos++;
2408 if (begv > pos)
2409 begv -= bytes;
2410 if (zv > pos)
2411 zv -= bytes;
2412 stop = Z;
2414 else
2416 bytes = BYTES_BY_CHAR_HEAD (*p);
2417 p += bytes, pos += bytes;
2420 if (narrowed)
2421 Fnarrow_to_region (make_number (begv), make_number (zv));
2423 else
2425 int pt = PT;
2426 int pos, stop;
2427 unsigned char *p, *pend;
2429 /* Be sure not to have a multibyte sequence striding over the GAP.
2430 Ex: We change this: "...abc\302 _GAP_ \241def..."
2431 to: "...abc _GAP_ \302\241def..." */
2433 if (EQ (flag, Qt)
2434 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2435 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2437 unsigned char *p = GPT_ADDR - 1;
2439 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2440 if (BASE_LEADING_CODE_P (*p))
2442 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2444 move_gap_both (new_gpt, new_gpt);
2448 /* Make the buffer contents valid as multibyte by converting
2449 8-bit characters to multibyte form. */
2450 pos = BEG;
2451 stop = GPT;
2452 p = BEG_ADDR;
2453 pend = GPT_ADDR;
2454 while (1)
2456 int bytes;
2458 if (pos == stop)
2460 if (pos == Z)
2461 break;
2462 p = GAP_END_ADDR;
2463 pend = Z_ADDR;
2464 stop = Z;
2467 if (ASCII_BYTE_P (*p))
2468 p++, pos++;
2469 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2470 p += bytes, pos += bytes;
2471 else
2473 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2474 int c;
2476 c = BYTE8_TO_CHAR (*p);
2477 bytes = CHAR_STRING (c, tmp);
2478 *p = tmp[0];
2479 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2480 bytes--;
2481 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2482 /* Now the gap is after the just inserted data. */
2483 pos = GPT;
2484 p = GAP_END_ADDR;
2485 if (pos <= begv)
2486 begv += bytes;
2487 if (pos <= zv)
2488 zv += bytes;
2489 if (pos <= pt)
2490 pt += bytes;
2491 pend = Z_ADDR;
2492 stop = Z;
2496 if (pt != PT)
2497 TEMP_SET_PT (pt);
2499 if (narrowed)
2500 Fnarrow_to_region (make_number (begv), make_number (zv));
2502 /* Do this first, so that chars_in_text asks the right question.
2503 set_intervals_multibyte needs it too. */
2504 current_buffer->enable_multibyte_characters = Qt;
2506 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2507 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2509 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2511 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2512 if (BEGV_BYTE > GPT_BYTE)
2513 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2514 else
2515 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2517 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2518 if (ZV_BYTE > GPT_BYTE)
2519 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2520 else
2521 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2524 int pt_byte = advance_to_char_boundary (PT_BYTE);
2525 int pt;
2527 if (pt_byte > GPT_BYTE)
2528 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2529 else
2530 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2531 TEMP_SET_PT_BOTH (pt, pt_byte);
2534 tail = markers = BUF_MARKERS (current_buffer);
2536 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2537 getting confused by the markers that have not yet been updated.
2538 It is also a signal that it should never create a marker. */
2539 BUF_MARKERS (current_buffer) = NULL;
2541 for (; tail; tail = tail->next)
2543 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2544 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2547 /* Make sure no markers were put on the chain
2548 while the chain value was incorrect. */
2549 if (BUF_MARKERS (current_buffer))
2550 abort ();
2552 BUF_MARKERS (current_buffer) = markers;
2554 /* Do this last, so it can calculate the new correspondences
2555 between chars and bytes. */
2556 set_intervals_multibyte (1);
2559 if (!EQ (old_undo, Qt))
2561 /* Represent all the above changes by a special undo entry. */
2562 extern Lisp_Object Qapply;
2563 current_buffer->undo_list = Fcons (list3 (Qapply,
2564 intern ("set-buffer-multibyte"),
2565 NILP (flag) ? Qt : Qnil),
2566 old_undo);
2569 UNGCPRO;
2571 /* Changing the multibyteness of a buffer means that all windows
2572 showing that buffer must be updated thoroughly. */
2573 current_buffer->prevent_redisplay_optimizations_p = 1;
2574 ++windows_or_buffers_changed;
2576 /* Copy this buffer's new multibyte status
2577 into all of its indirect buffers. */
2578 for (other = all_buffers; other; other = other->next)
2579 if (other->base_buffer == current_buffer && !NILP (other->name))
2581 other->enable_multibyte_characters
2582 = current_buffer->enable_multibyte_characters;
2583 other->prevent_redisplay_optimizations_p = 1;
2586 /* Restore the modifiedness of the buffer. */
2587 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2588 Fset_buffer_modified_p (Qnil);
2590 #ifdef subprocesses
2591 /* Update coding systems of this buffer's process (if any). */
2593 Lisp_Object process;
2595 process = Fget_buffer_process (Fcurrent_buffer ());
2596 if (PROCESSP (process))
2597 setup_process_coding_systems (process);
2599 #endif /* subprocesses */
2601 return flag;
2604 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2605 0, 0, 0,
2606 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2607 Most local variable bindings are eliminated so that the default values
2608 become effective once more. Also, the syntax table is set from
2609 `standard-syntax-table', the local keymap is set to nil,
2610 and the abbrev table from `fundamental-mode-abbrev-table'.
2611 This function also forces redisplay of the mode line.
2613 Every function to select a new major mode starts by
2614 calling this function.
2616 As a special exception, local variables whose names have
2617 a non-nil `permanent-local' property are not eliminated by this function.
2619 The first thing this function does is run
2620 the normal hook `change-major-mode-hook'. */)
2623 if (!NILP (Vrun_hooks))
2624 call1 (Vrun_hooks, Qchange_major_mode_hook);
2626 /* Make sure none of the bindings in local_var_alist
2627 remain swapped in, in their symbols. */
2629 swap_out_buffer_local_variables (current_buffer);
2631 /* Actually eliminate all local bindings of this buffer. */
2633 reset_buffer_local_variables (current_buffer, 0);
2635 /* Force mode-line redisplay. Useful here because all major mode
2636 commands call this function. */
2637 update_mode_lines++;
2639 return Qnil;
2642 /* Make sure no local variables remain set up with buffer B
2643 for their current values. */
2645 static void
2646 swap_out_buffer_local_variables (b)
2647 struct buffer *b;
2649 Lisp_Object oalist, alist, sym, tem, buffer;
2651 XSETBUFFER (buffer, b);
2652 oalist = b->local_var_alist;
2654 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2656 sym = XCAR (XCAR (alist));
2658 /* Need not do anything if some other buffer's binding is now encached. */
2659 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2660 if (EQ (tem, buffer))
2662 /* Symbol is set up for this buffer's old local value:
2663 swap it out! */
2664 swap_in_global_binding (sym);
2669 /* Find all the overlays in the current buffer that contain position POS.
2670 Return the number found, and store them in a vector in *VEC_PTR.
2671 Store in *LEN_PTR the size allocated for the vector.
2672 Store in *NEXT_PTR the next position after POS where an overlay starts,
2673 or ZV if there are no more overlays between POS and ZV.
2674 Store in *PREV_PTR the previous position before POS where an overlay ends,
2675 or where an overlay starts which ends at or after POS;
2676 or BEGV if there are no such overlays from BEGV to POS.
2677 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2679 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2680 when this function is called.
2682 If EXTEND is non-zero, we make the vector bigger if necessary.
2683 If EXTEND is zero, we never extend the vector,
2684 and we store only as many overlays as will fit.
2685 But we still return the total number of overlays.
2687 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2688 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2689 default (BEGV or ZV). */
2692 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2693 EMACS_INT pos;
2694 int extend;
2695 Lisp_Object **vec_ptr;
2696 int *len_ptr;
2697 EMACS_INT *next_ptr;
2698 EMACS_INT *prev_ptr;
2699 int change_req;
2701 Lisp_Object overlay, start, end;
2702 struct Lisp_Overlay *tail;
2703 int idx = 0;
2704 int len = *len_ptr;
2705 Lisp_Object *vec = *vec_ptr;
2706 int next = ZV;
2707 int prev = BEGV;
2708 int inhibit_storing = 0;
2710 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2712 int startpos, endpos;
2714 XSETMISC (overlay, tail);
2716 start = OVERLAY_START (overlay);
2717 end = OVERLAY_END (overlay);
2718 endpos = OVERLAY_POSITION (end);
2719 if (endpos < pos)
2721 if (prev < endpos)
2722 prev = endpos;
2723 break;
2725 startpos = OVERLAY_POSITION (start);
2726 /* This one ends at or after POS
2727 so its start counts for PREV_PTR if it's before POS. */
2728 if (prev < startpos && startpos < pos)
2729 prev = startpos;
2730 if (endpos == pos)
2731 continue;
2732 if (startpos <= pos)
2734 if (idx == len)
2736 /* The supplied vector is full.
2737 Either make it bigger, or don't store any more in it. */
2738 if (extend)
2740 /* Make it work with an initial len == 0. */
2741 len *= 2;
2742 if (len == 0)
2743 len = 4;
2744 *len_ptr = len;
2745 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2746 *vec_ptr = vec;
2748 else
2749 inhibit_storing = 1;
2752 if (!inhibit_storing)
2753 vec[idx] = overlay;
2754 /* Keep counting overlays even if we can't return them all. */
2755 idx++;
2757 else if (startpos < next)
2758 next = startpos;
2761 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2763 int startpos, endpos;
2765 XSETMISC (overlay, tail);
2767 start = OVERLAY_START (overlay);
2768 end = OVERLAY_END (overlay);
2769 startpos = OVERLAY_POSITION (start);
2770 if (pos < startpos)
2772 if (startpos < next)
2773 next = startpos;
2774 break;
2776 endpos = OVERLAY_POSITION (end);
2777 if (pos < endpos)
2779 if (idx == len)
2781 if (extend)
2783 /* Make it work with an initial len == 0. */
2784 len *= 2;
2785 if (len == 0)
2786 len = 4;
2787 *len_ptr = len;
2788 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2789 *vec_ptr = vec;
2791 else
2792 inhibit_storing = 1;
2795 if (!inhibit_storing)
2796 vec[idx] = overlay;
2797 idx++;
2799 if (startpos < pos && startpos > prev)
2800 prev = startpos;
2802 else if (endpos < pos && endpos > prev)
2803 prev = endpos;
2804 else if (endpos == pos && startpos > prev
2805 && (!change_req || startpos < pos))
2806 prev = startpos;
2809 if (next_ptr)
2810 *next_ptr = next;
2811 if (prev_ptr)
2812 *prev_ptr = prev;
2813 return idx;
2816 /* Find all the overlays in the current buffer that overlap the range
2817 BEG-END, or are empty at BEG, or are empty at END provided END
2818 denotes the position at the end of the current buffer.
2820 Return the number found, and store them in a vector in *VEC_PTR.
2821 Store in *LEN_PTR the size allocated for the vector.
2822 Store in *NEXT_PTR the next position after POS where an overlay starts,
2823 or ZV if there are no more overlays.
2824 Store in *PREV_PTR the previous position before POS where an overlay ends,
2825 or BEGV if there are no previous overlays.
2826 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2828 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2829 when this function is called.
2831 If EXTEND is non-zero, we make the vector bigger if necessary.
2832 If EXTEND is zero, we never extend the vector,
2833 and we store only as many overlays as will fit.
2834 But we still return the total number of overlays. */
2836 static int
2837 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2838 int beg, end;
2839 int extend;
2840 Lisp_Object **vec_ptr;
2841 int *len_ptr;
2842 int *next_ptr;
2843 int *prev_ptr;
2845 Lisp_Object overlay, ostart, oend;
2846 struct Lisp_Overlay *tail;
2847 int idx = 0;
2848 int len = *len_ptr;
2849 Lisp_Object *vec = *vec_ptr;
2850 int next = ZV;
2851 int prev = BEGV;
2852 int inhibit_storing = 0;
2853 int end_is_Z = end == Z;
2855 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2857 int startpos, endpos;
2859 XSETMISC (overlay, tail);
2861 ostart = OVERLAY_START (overlay);
2862 oend = OVERLAY_END (overlay);
2863 endpos = OVERLAY_POSITION (oend);
2864 if (endpos < beg)
2866 if (prev < endpos)
2867 prev = endpos;
2868 break;
2870 startpos = OVERLAY_POSITION (ostart);
2871 /* Count an interval if it overlaps the range, is empty at the
2872 start of the range, or is empty at END provided END denotes the
2873 end of the buffer. */
2874 if ((beg < endpos && startpos < end)
2875 || (startpos == endpos
2876 && (beg == endpos || (end_is_Z && endpos == end))))
2878 if (idx == len)
2880 /* The supplied vector is full.
2881 Either make it bigger, or don't store any more in it. */
2882 if (extend)
2884 /* Make it work with an initial len == 0. */
2885 len *= 2;
2886 if (len == 0)
2887 len = 4;
2888 *len_ptr = len;
2889 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2890 *vec_ptr = vec;
2892 else
2893 inhibit_storing = 1;
2896 if (!inhibit_storing)
2897 vec[idx] = overlay;
2898 /* Keep counting overlays even if we can't return them all. */
2899 idx++;
2901 else if (startpos < next)
2902 next = startpos;
2905 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2907 int startpos, endpos;
2909 XSETMISC (overlay, tail);
2911 ostart = OVERLAY_START (overlay);
2912 oend = OVERLAY_END (overlay);
2913 startpos = OVERLAY_POSITION (ostart);
2914 if (end < startpos)
2916 if (startpos < next)
2917 next = startpos;
2918 break;
2920 endpos = OVERLAY_POSITION (oend);
2921 /* Count an interval if it overlaps the range, is empty at the
2922 start of the range, or is empty at END provided END denotes the
2923 end of the buffer. */
2924 if ((beg < endpos && startpos < end)
2925 || (startpos == endpos
2926 && (beg == endpos || (end_is_Z && endpos == end))))
2928 if (idx == len)
2930 if (extend)
2932 /* Make it work with an initial len == 0. */
2933 len *= 2;
2934 if (len == 0)
2935 len = 4;
2936 *len_ptr = len;
2937 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2938 *vec_ptr = vec;
2940 else
2941 inhibit_storing = 1;
2944 if (!inhibit_storing)
2945 vec[idx] = overlay;
2946 idx++;
2948 else if (endpos < beg && endpos > prev)
2949 prev = endpos;
2952 if (next_ptr)
2953 *next_ptr = next;
2954 if (prev_ptr)
2955 *prev_ptr = prev;
2956 return idx;
2960 /* Return non-zero if there exists an overlay with a non-nil
2961 `mouse-face' property overlapping OVERLAY. */
2964 mouse_face_overlay_overlaps (overlay)
2965 Lisp_Object overlay;
2967 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2968 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2969 int n, i, size;
2970 Lisp_Object *v, tem;
2972 size = 10;
2973 v = (Lisp_Object *) alloca (size * sizeof *v);
2974 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2975 if (n > size)
2977 v = (Lisp_Object *) alloca (n * sizeof *v);
2978 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2981 for (i = 0; i < n; ++i)
2982 if (!EQ (v[i], overlay)
2983 && (tem = Foverlay_get (overlay, Qmouse_face),
2984 !NILP (tem)))
2985 break;
2987 return i < n;
2992 /* Fast function to just test if we're at an overlay boundary. */
2994 overlay_touches_p (pos)
2995 int pos;
2997 Lisp_Object overlay;
2998 struct Lisp_Overlay *tail;
3000 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
3002 int endpos;
3004 XSETMISC (overlay ,tail);
3005 if (!OVERLAYP (overlay))
3006 abort ();
3008 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3009 if (endpos < pos)
3010 break;
3011 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3012 return 1;
3015 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
3017 int startpos;
3019 XSETMISC (overlay, tail);
3020 if (!OVERLAYP (overlay))
3021 abort ();
3023 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3024 if (pos < startpos)
3025 break;
3026 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3027 return 1;
3029 return 0;
3032 struct sortvec
3034 Lisp_Object overlay;
3035 int beg, end;
3036 int priority;
3039 static int
3040 compare_overlays (v1, v2)
3041 const void *v1, *v2;
3043 const struct sortvec *s1 = (const struct sortvec *) v1;
3044 const struct sortvec *s2 = (const struct sortvec *) v2;
3045 if (s1->priority != s2->priority)
3046 return s1->priority - s2->priority;
3047 if (s1->beg != s2->beg)
3048 return s1->beg - s2->beg;
3049 if (s1->end != s2->end)
3050 return s2->end - s1->end;
3051 return 0;
3054 /* Sort an array of overlays by priority. The array is modified in place.
3055 The return value is the new size; this may be smaller than the original
3056 size if some of the overlays were invalid or were window-specific. */
3058 sort_overlays (overlay_vec, noverlays, w)
3059 Lisp_Object *overlay_vec;
3060 int noverlays;
3061 struct window *w;
3063 int i, j;
3064 struct sortvec *sortvec;
3065 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3067 /* Put the valid and relevant overlays into sortvec. */
3069 for (i = 0, j = 0; i < noverlays; i++)
3071 Lisp_Object tem;
3072 Lisp_Object overlay;
3074 overlay = overlay_vec[i];
3075 if (OVERLAY_VALID (overlay)
3076 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3077 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3079 /* If we're interested in a specific window, then ignore
3080 overlays that are limited to some other window. */
3081 if (w)
3083 Lisp_Object window;
3085 window = Foverlay_get (overlay, Qwindow);
3086 if (WINDOWP (window) && XWINDOW (window) != w)
3087 continue;
3090 /* This overlay is good and counts: put it into sortvec. */
3091 sortvec[j].overlay = overlay;
3092 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3093 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3094 tem = Foverlay_get (overlay, Qpriority);
3095 if (INTEGERP (tem))
3096 sortvec[j].priority = XINT (tem);
3097 else
3098 sortvec[j].priority = 0;
3099 j++;
3102 noverlays = j;
3104 /* Sort the overlays into the proper order: increasing priority. */
3106 if (noverlays > 1)
3107 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3109 for (i = 0; i < noverlays; i++)
3110 overlay_vec[i] = sortvec[i].overlay;
3111 return (noverlays);
3114 struct sortstr
3116 Lisp_Object string, string2;
3117 int size;
3118 int priority;
3121 struct sortstrlist
3123 struct sortstr *buf; /* An array that expands as needed; never freed. */
3124 int size; /* Allocated length of that array. */
3125 int used; /* How much of the array is currently in use. */
3126 int bytes; /* Total length of the strings in buf. */
3129 /* Buffers for storing information about the overlays touching a given
3130 position. These could be automatic variables in overlay_strings, but
3131 it's more efficient to hold onto the memory instead of repeatedly
3132 allocating and freeing it. */
3133 static struct sortstrlist overlay_heads, overlay_tails;
3134 static unsigned char *overlay_str_buf;
3136 /* Allocated length of overlay_str_buf. */
3137 static int overlay_str_len;
3139 /* A comparison function suitable for passing to qsort. */
3140 static int
3141 cmp_for_strings (as1, as2)
3142 char *as1, *as2;
3144 struct sortstr *s1 = (struct sortstr *)as1;
3145 struct sortstr *s2 = (struct sortstr *)as2;
3146 if (s1->size != s2->size)
3147 return s2->size - s1->size;
3148 if (s1->priority != s2->priority)
3149 return s1->priority - s2->priority;
3150 return 0;
3153 static void
3154 record_overlay_string (ssl, str, str2, pri, size)
3155 struct sortstrlist *ssl;
3156 Lisp_Object str, str2, pri;
3157 int size;
3159 int nbytes;
3161 if (ssl->used == ssl->size)
3163 if (ssl->buf)
3164 ssl->size *= 2;
3165 else
3166 ssl->size = 5;
3167 ssl->buf = ((struct sortstr *)
3168 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3170 ssl->buf[ssl->used].string = str;
3171 ssl->buf[ssl->used].string2 = str2;
3172 ssl->buf[ssl->used].size = size;
3173 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3174 ssl->used++;
3176 if (NILP (current_buffer->enable_multibyte_characters))
3177 nbytes = SCHARS (str);
3178 else if (! STRING_MULTIBYTE (str))
3179 nbytes = count_size_as_multibyte (SDATA (str),
3180 SBYTES (str));
3181 else
3182 nbytes = SBYTES (str);
3184 ssl->bytes += nbytes;
3186 if (STRINGP (str2))
3188 if (NILP (current_buffer->enable_multibyte_characters))
3189 nbytes = SCHARS (str2);
3190 else if (! STRING_MULTIBYTE (str2))
3191 nbytes = count_size_as_multibyte (SDATA (str2),
3192 SBYTES (str2));
3193 else
3194 nbytes = SBYTES (str2);
3196 ssl->bytes += nbytes;
3200 /* Return the concatenation of the strings associated with overlays that
3201 begin or end at POS, ignoring overlays that are specific to a window
3202 other than W. The strings are concatenated in the appropriate order:
3203 shorter overlays nest inside longer ones, and higher priority inside
3204 lower. Normally all of the after-strings come first, but zero-sized
3205 overlays have their after-strings ride along with the before-strings
3206 because it would look strange to print them inside-out.
3208 Returns the string length, and stores the contents indirectly through
3209 PSTR, if that variable is non-null. The string may be overwritten by
3210 subsequent calls. */
3213 overlay_strings (pos, w, pstr)
3214 EMACS_INT pos;
3215 struct window *w;
3216 unsigned char **pstr;
3218 Lisp_Object overlay, window, str;
3219 struct Lisp_Overlay *ov;
3220 int startpos, endpos;
3221 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3223 overlay_heads.used = overlay_heads.bytes = 0;
3224 overlay_tails.used = overlay_tails.bytes = 0;
3225 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3227 XSETMISC (overlay, ov);
3228 eassert (OVERLAYP (overlay));
3230 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3231 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3232 if (endpos < pos)
3233 break;
3234 if (endpos != pos && startpos != pos)
3235 continue;
3236 window = Foverlay_get (overlay, Qwindow);
3237 if (WINDOWP (window) && XWINDOW (window) != w)
3238 continue;
3239 if (startpos == pos
3240 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3241 record_overlay_string (&overlay_heads, str,
3242 (startpos == endpos
3243 ? Foverlay_get (overlay, Qafter_string)
3244 : Qnil),
3245 Foverlay_get (overlay, Qpriority),
3246 endpos - startpos);
3247 else if (endpos == pos
3248 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3249 record_overlay_string (&overlay_tails, str, Qnil,
3250 Foverlay_get (overlay, Qpriority),
3251 endpos - startpos);
3253 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3255 XSETMISC (overlay, ov);
3256 eassert (OVERLAYP (overlay));
3258 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3259 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3260 if (startpos > pos)
3261 break;
3262 if (endpos != pos && startpos != pos)
3263 continue;
3264 window = Foverlay_get (overlay, Qwindow);
3265 if (WINDOWP (window) && XWINDOW (window) != w)
3266 continue;
3267 if (startpos == pos
3268 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3269 record_overlay_string (&overlay_heads, str,
3270 (startpos == endpos
3271 ? Foverlay_get (overlay, Qafter_string)
3272 : Qnil),
3273 Foverlay_get (overlay, Qpriority),
3274 endpos - startpos);
3275 else if (endpos == pos
3276 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3277 record_overlay_string (&overlay_tails, str, Qnil,
3278 Foverlay_get (overlay, Qpriority),
3279 endpos - startpos);
3281 if (overlay_tails.used > 1)
3282 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3283 cmp_for_strings);
3284 if (overlay_heads.used > 1)
3285 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3286 cmp_for_strings);
3287 if (overlay_heads.bytes || overlay_tails.bytes)
3289 Lisp_Object tem;
3290 int i;
3291 unsigned char *p;
3292 int total = overlay_heads.bytes + overlay_tails.bytes;
3294 if (total > overlay_str_len)
3296 overlay_str_len = total;
3297 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3298 total);
3300 p = overlay_str_buf;
3301 for (i = overlay_tails.used; --i >= 0;)
3303 int nbytes;
3304 tem = overlay_tails.buf[i].string;
3305 nbytes = copy_text (SDATA (tem), p,
3306 SBYTES (tem),
3307 STRING_MULTIBYTE (tem), multibyte);
3308 p += nbytes;
3310 for (i = 0; i < overlay_heads.used; ++i)
3312 int nbytes;
3313 tem = overlay_heads.buf[i].string;
3314 nbytes = copy_text (SDATA (tem), p,
3315 SBYTES (tem),
3316 STRING_MULTIBYTE (tem), multibyte);
3317 p += nbytes;
3318 tem = overlay_heads.buf[i].string2;
3319 if (STRINGP (tem))
3321 nbytes = copy_text (SDATA (tem), p,
3322 SBYTES (tem),
3323 STRING_MULTIBYTE (tem), multibyte);
3324 p += nbytes;
3327 if (p != overlay_str_buf + total)
3328 abort ();
3329 if (pstr)
3330 *pstr = overlay_str_buf;
3331 return total;
3333 return 0;
3336 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3338 void
3339 recenter_overlay_lists (buf, pos)
3340 struct buffer *buf;
3341 EMACS_INT pos;
3343 Lisp_Object overlay, beg, end;
3344 struct Lisp_Overlay *prev, *tail, *next;
3346 /* See if anything in overlays_before should move to overlays_after. */
3348 /* We don't strictly need prev in this loop; it should always be nil.
3349 But we use it for symmetry and in case that should cease to be true
3350 with some future change. */
3351 prev = NULL;
3352 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3354 next = tail->next;
3355 XSETMISC (overlay, tail);
3357 /* If the overlay is not valid, get rid of it. */
3358 if (!OVERLAY_VALID (overlay))
3359 #if 1
3360 abort ();
3361 #else
3363 /* Splice the cons cell TAIL out of overlays_before. */
3364 if (!NILP (prev))
3365 XCDR (prev) = next;
3366 else
3367 buf->overlays_before = next;
3368 tail = prev;
3369 continue;
3371 #endif
3373 beg = OVERLAY_START (overlay);
3374 end = OVERLAY_END (overlay);
3376 if (OVERLAY_POSITION (end) > pos)
3378 /* OVERLAY needs to be moved. */
3379 int where = OVERLAY_POSITION (beg);
3380 struct Lisp_Overlay *other, *other_prev;
3382 /* Splice the cons cell TAIL out of overlays_before. */
3383 if (prev)
3384 prev->next = next;
3385 else
3386 buf->overlays_before = next;
3388 /* Search thru overlays_after for where to put it. */
3389 other_prev = NULL;
3390 for (other = buf->overlays_after; other;
3391 other_prev = other, other = other->next)
3393 Lisp_Object otherbeg, otheroverlay;
3395 XSETMISC (otheroverlay, other);
3396 eassert (OVERLAY_VALID (otheroverlay));
3398 otherbeg = OVERLAY_START (otheroverlay);
3399 if (OVERLAY_POSITION (otherbeg) >= where)
3400 break;
3403 /* Add TAIL to overlays_after before OTHER. */
3404 tail->next = other;
3405 if (other_prev)
3406 other_prev->next = tail;
3407 else
3408 buf->overlays_after = tail;
3409 tail = prev;
3411 else
3412 /* We've reached the things that should stay in overlays_before.
3413 All the rest of overlays_before must end even earlier,
3414 so stop now. */
3415 break;
3418 /* See if anything in overlays_after should be in overlays_before. */
3419 prev = NULL;
3420 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3422 next = tail->next;
3423 XSETMISC (overlay, tail);
3425 /* If the overlay is not valid, get rid of it. */
3426 if (!OVERLAY_VALID (overlay))
3427 #if 1
3428 abort ();
3429 #else
3431 /* Splice the cons cell TAIL out of overlays_after. */
3432 if (!NILP (prev))
3433 XCDR (prev) = next;
3434 else
3435 buf->overlays_after = next;
3436 tail = prev;
3437 continue;
3439 #endif
3441 beg = OVERLAY_START (overlay);
3442 end = OVERLAY_END (overlay);
3444 /* Stop looking, when we know that nothing further
3445 can possibly end before POS. */
3446 if (OVERLAY_POSITION (beg) > pos)
3447 break;
3449 if (OVERLAY_POSITION (end) <= pos)
3451 /* OVERLAY needs to be moved. */
3452 int where = OVERLAY_POSITION (end);
3453 struct Lisp_Overlay *other, *other_prev;
3455 /* Splice the cons cell TAIL out of overlays_after. */
3456 if (prev)
3457 prev->next = next;
3458 else
3459 buf->overlays_after = next;
3461 /* Search thru overlays_before for where to put it. */
3462 other_prev = NULL;
3463 for (other = buf->overlays_before; other;
3464 other_prev = other, other = other->next)
3466 Lisp_Object otherend, otheroverlay;
3468 XSETMISC (otheroverlay, other);
3469 eassert (OVERLAY_VALID (otheroverlay));
3471 otherend = OVERLAY_END (otheroverlay);
3472 if (OVERLAY_POSITION (otherend) <= where)
3473 break;
3476 /* Add TAIL to overlays_before before OTHER. */
3477 tail->next = other;
3478 if (other_prev)
3479 other_prev->next = tail;
3480 else
3481 buf->overlays_before = tail;
3482 tail = prev;
3486 buf->overlay_center = pos;
3489 void
3490 adjust_overlays_for_insert (pos, length)
3491 EMACS_INT pos;
3492 EMACS_INT length;
3494 /* After an insertion, the lists are still sorted properly,
3495 but we may need to update the value of the overlay center. */
3496 if (current_buffer->overlay_center >= pos)
3497 current_buffer->overlay_center += length;
3500 void
3501 adjust_overlays_for_delete (pos, length)
3502 EMACS_INT pos;
3503 EMACS_INT length;
3505 if (current_buffer->overlay_center < pos)
3506 /* The deletion was to our right. No change needed; the before- and
3507 after-lists are still consistent. */
3509 else if (current_buffer->overlay_center > pos + length)
3510 /* The deletion was to our left. We need to adjust the center value
3511 to account for the change in position, but the lists are consistent
3512 given the new value. */
3513 current_buffer->overlay_center -= length;
3514 else
3515 /* We're right in the middle. There might be things on the after-list
3516 that now belong on the before-list. Recentering will move them,
3517 and also update the center point. */
3518 recenter_overlay_lists (current_buffer, pos);
3521 /* Fix up overlays that were garbled as a result of permuting markers
3522 in the range START through END. Any overlay with at least one
3523 endpoint in this range will need to be unlinked from the overlay
3524 list and reinserted in its proper place.
3525 Such an overlay might even have negative size at this point.
3526 If so, we'll make the overlay empty. */
3527 void
3528 fix_start_end_in_overlays (start, end)
3529 register int start, end;
3531 Lisp_Object overlay;
3532 struct Lisp_Overlay *before_list, *after_list;
3533 /* These are either nil, indicating that before_list or after_list
3534 should be assigned, or the cons cell the cdr of which should be
3535 assigned. */
3536 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3537 /* 'Parent', likewise, indicates a cons cell or
3538 current_buffer->overlays_before or overlays_after, depending
3539 which loop we're in. */
3540 struct Lisp_Overlay *tail, *parent;
3541 int startpos, endpos;
3543 /* This algorithm shifts links around instead of consing and GCing.
3544 The loop invariant is that before_list (resp. after_list) is a
3545 well-formed list except that its last element, the CDR of beforep
3546 (resp. afterp) if beforep (afterp) isn't nil or before_list
3547 (after_list) if it is, is still uninitialized. So it's not a bug
3548 that before_list isn't initialized, although it may look
3549 strange. */
3550 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3552 XSETMISC (overlay, tail);
3554 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3555 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3557 /* If the overlay is backwards, make it empty. */
3558 if (endpos < startpos)
3560 startpos = endpos;
3561 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3562 Qnil);
3565 if (endpos < start)
3566 break;
3568 if (endpos < end
3569 || (startpos >= start && startpos < end))
3571 /* Add it to the end of the wrong list. Later on,
3572 recenter_overlay_lists will move it to the right place. */
3573 if (endpos < current_buffer->overlay_center)
3575 if (!afterp)
3576 after_list = tail;
3577 else
3578 afterp->next = tail;
3579 afterp = tail;
3581 else
3583 if (!beforep)
3584 before_list = tail;
3585 else
3586 beforep->next = tail;
3587 beforep = tail;
3589 if (!parent)
3590 current_buffer->overlays_before = tail->next;
3591 else
3592 parent->next = tail->next;
3593 tail = tail->next;
3595 else
3596 parent = tail, tail = parent->next;
3598 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3600 XSETMISC (overlay, tail);
3602 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3603 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3605 /* If the overlay is backwards, make it empty. */
3606 if (endpos < startpos)
3608 startpos = endpos;
3609 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3610 Qnil);
3613 if (startpos >= end)
3614 break;
3616 if (startpos >= start
3617 || (endpos >= start && endpos < end))
3619 if (endpos < current_buffer->overlay_center)
3621 if (!afterp)
3622 after_list = tail;
3623 else
3624 afterp->next = tail;
3625 afterp = tail;
3627 else
3629 if (!beforep)
3630 before_list = tail;
3631 else
3632 beforep->next = tail;
3633 beforep = tail;
3635 if (!parent)
3636 current_buffer->overlays_after = tail->next;
3637 else
3638 parent->next = tail->next;
3639 tail = tail->next;
3641 else
3642 parent = tail, tail = parent->next;
3645 /* Splice the constructed (wrong) lists into the buffer's lists,
3646 and let the recenter function make it sane again. */
3647 if (beforep)
3649 beforep->next = current_buffer->overlays_before;
3650 current_buffer->overlays_before = before_list;
3652 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3654 if (afterp)
3656 afterp->next = current_buffer->overlays_after;
3657 current_buffer->overlays_after = after_list;
3659 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3662 /* We have two types of overlay: the one whose ending marker is
3663 after-insertion-marker (this is the usual case) and the one whose
3664 ending marker is before-insertion-marker. When `overlays_before'
3665 contains overlays of the latter type and the former type in this
3666 order and both overlays end at inserting position, inserting a text
3667 increases only the ending marker of the latter type, which results
3668 in incorrect ordering of `overlays_before'.
3670 This function fixes ordering of overlays in the slot
3671 `overlays_before' of the buffer *BP. Before the insertion, `point'
3672 was at PREV, and now is at POS. */
3674 void
3675 fix_overlays_before (bp, prev, pos)
3676 struct buffer *bp;
3677 EMACS_INT prev, pos;
3679 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3680 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3681 Lisp_Object tem;
3682 EMACS_INT end;
3684 /* After the insertion, the several overlays may be in incorrect
3685 order. The possibility is that, in the list `overlays_before',
3686 an overlay which ends at POS appears after an overlay which ends
3687 at PREV. Since POS is greater than PREV, we must fix the
3688 ordering of these overlays, by moving overlays ends at POS before
3689 the overlays ends at PREV. */
3691 /* At first, find a place where disordered overlays should be linked
3692 in. It is where an overlay which end before POS exists. (i.e. an
3693 overlay whose ending marker is after-insertion-marker if disorder
3694 exists). */
3695 while (tail
3696 && (XSETMISC (tem, tail),
3697 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3699 parent = tail;
3700 tail = tail->next;
3703 /* If we don't find such an overlay,
3704 or the found one ends before PREV,
3705 or the found one is the last one in the list,
3706 we don't have to fix anything. */
3707 if (!tail || end < prev || !tail->next)
3708 return;
3710 right_pair = parent;
3711 parent = tail;
3712 tail = tail->next;
3714 /* Now, end position of overlays in the list TAIL should be before
3715 or equal to PREV. In the loop, an overlay which ends at POS is
3716 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3717 we found an overlay which ends before PREV, the remaining
3718 overlays are in correct order. */
3719 while (tail)
3721 XSETMISC (tem, tail);
3722 end = OVERLAY_POSITION (OVERLAY_END (tem));
3724 if (end == pos)
3725 { /* This overlay is disordered. */
3726 struct Lisp_Overlay *found = tail;
3728 /* Unlink the found overlay. */
3729 tail = found->next;
3730 parent->next = tail;
3731 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3732 and link it into the right place. */
3733 if (!right_pair)
3735 found->next = bp->overlays_before;
3736 bp->overlays_before = found;
3738 else
3740 found->next = right_pair->next;
3741 right_pair->next = found;
3744 else if (end == prev)
3746 parent = tail;
3747 tail = tail->next;
3749 else /* No more disordered overlay. */
3750 break;
3754 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3755 doc: /* Return t if OBJECT is an overlay. */)
3756 (object)
3757 Lisp_Object object;
3759 return (OVERLAYP (object) ? Qt : Qnil);
3762 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3763 doc: /* Create a new overlay with range BEG to END in BUFFER.
3764 If omitted, BUFFER defaults to the current buffer.
3765 BEG and END may be integers or markers.
3766 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3767 for the front of the overlay advance when text is inserted there
3768 \(which means the text *is not* included in the overlay).
3769 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3770 for the rear of the overlay advance when text is inserted there
3771 \(which means the text *is* included in the overlay). */)
3772 (beg, end, buffer, front_advance, rear_advance)
3773 Lisp_Object beg, end, buffer;
3774 Lisp_Object front_advance, rear_advance;
3776 Lisp_Object overlay;
3777 struct buffer *b;
3779 if (NILP (buffer))
3780 XSETBUFFER (buffer, current_buffer);
3781 else
3782 CHECK_BUFFER (buffer);
3783 if (MARKERP (beg)
3784 && ! EQ (Fmarker_buffer (beg), buffer))
3785 error ("Marker points into wrong buffer");
3786 if (MARKERP (end)
3787 && ! EQ (Fmarker_buffer (end), buffer))
3788 error ("Marker points into wrong buffer");
3790 CHECK_NUMBER_COERCE_MARKER (beg);
3791 CHECK_NUMBER_COERCE_MARKER (end);
3793 if (XINT (beg) > XINT (end))
3795 Lisp_Object temp;
3796 temp = beg; beg = end; end = temp;
3799 b = XBUFFER (buffer);
3801 beg = Fset_marker (Fmake_marker (), beg, buffer);
3802 end = Fset_marker (Fmake_marker (), end, buffer);
3804 if (!NILP (front_advance))
3805 XMARKER (beg)->insertion_type = 1;
3806 if (!NILP (rear_advance))
3807 XMARKER (end)->insertion_type = 1;
3809 overlay = allocate_misc ();
3810 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3811 XOVERLAY (overlay)->start = beg;
3812 XOVERLAY (overlay)->end = end;
3813 XOVERLAY (overlay)->plist = Qnil;
3814 XOVERLAY (overlay)->next = NULL;
3816 /* Put the new overlay on the wrong list. */
3817 end = OVERLAY_END (overlay);
3818 if (OVERLAY_POSITION (end) < b->overlay_center)
3820 if (b->overlays_after)
3821 XOVERLAY (overlay)->next = b->overlays_after;
3822 b->overlays_after = XOVERLAY (overlay);
3824 else
3826 if (b->overlays_before)
3827 XOVERLAY (overlay)->next = b->overlays_before;
3828 b->overlays_before = XOVERLAY (overlay);
3831 /* This puts it in the right list, and in the right order. */
3832 recenter_overlay_lists (b, b->overlay_center);
3834 /* We don't need to redisplay the region covered by the overlay, because
3835 the overlay has no properties at the moment. */
3837 return overlay;
3840 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3842 static void
3843 modify_overlay (buf, start, end)
3844 struct buffer *buf;
3845 EMACS_INT start, end;
3847 if (start > end)
3849 int temp = start;
3850 start = end;
3851 end = temp;
3854 BUF_COMPUTE_UNCHANGED (buf, start, end);
3856 /* If this is a buffer not in the selected window,
3857 we must do other windows. */
3858 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3859 windows_or_buffers_changed = 1;
3860 /* If multiple windows show this buffer, we must do other windows. */
3861 else if (buffer_shared > 1)
3862 windows_or_buffers_changed = 1;
3863 /* If we modify an overlay at the end of the buffer, we cannot
3864 be sure that window end is still valid. */
3865 else if (end >= ZV && start <= ZV)
3866 windows_or_buffers_changed = 1;
3868 ++BUF_OVERLAY_MODIFF (buf);
3872 Lisp_Object Fdelete_overlay ();
3874 static struct Lisp_Overlay *
3875 unchain_overlay (list, overlay)
3876 struct Lisp_Overlay *list, *overlay;
3878 struct Lisp_Overlay *tmp, *prev;
3879 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3880 if (tmp == overlay)
3882 if (prev)
3883 prev->next = tmp->next;
3884 else
3885 list = tmp->next;
3886 overlay->next = NULL;
3887 break;
3889 return list;
3892 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3893 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3894 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3895 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3896 buffer. */)
3897 (overlay, beg, end, buffer)
3898 Lisp_Object overlay, beg, end, buffer;
3900 struct buffer *b, *ob;
3901 Lisp_Object obuffer;
3902 int count = SPECPDL_INDEX ();
3904 CHECK_OVERLAY (overlay);
3905 if (NILP (buffer))
3906 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3907 if (NILP (buffer))
3908 XSETBUFFER (buffer, current_buffer);
3909 CHECK_BUFFER (buffer);
3911 if (MARKERP (beg)
3912 && ! EQ (Fmarker_buffer (beg), buffer))
3913 error ("Marker points into wrong buffer");
3914 if (MARKERP (end)
3915 && ! EQ (Fmarker_buffer (end), buffer))
3916 error ("Marker points into wrong buffer");
3918 CHECK_NUMBER_COERCE_MARKER (beg);
3919 CHECK_NUMBER_COERCE_MARKER (end);
3921 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3922 return Fdelete_overlay (overlay);
3924 if (XINT (beg) > XINT (end))
3926 Lisp_Object temp;
3927 temp = beg; beg = end; end = temp;
3930 specbind (Qinhibit_quit, Qt);
3932 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3933 b = XBUFFER (buffer);
3934 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3936 /* If the overlay has changed buffers, do a thorough redisplay. */
3937 if (!EQ (buffer, obuffer))
3939 /* Redisplay where the overlay was. */
3940 if (!NILP (obuffer))
3942 int o_beg;
3943 int o_end;
3945 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3946 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3948 modify_overlay (ob, o_beg, o_end);
3951 /* Redisplay where the overlay is going to be. */
3952 modify_overlay (b, XINT (beg), XINT (end));
3954 else
3955 /* Redisplay the area the overlay has just left, or just enclosed. */
3957 int o_beg, o_end;
3959 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3960 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3962 if (o_beg == XINT (beg))
3963 modify_overlay (b, o_end, XINT (end));
3964 else if (o_end == XINT (end))
3965 modify_overlay (b, o_beg, XINT (beg));
3966 else
3968 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3969 if (XINT (end) > o_end) o_end = XINT (end);
3970 modify_overlay (b, o_beg, o_end);
3974 if (!NILP (obuffer))
3976 ob->overlays_before
3977 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3978 ob->overlays_after
3979 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3980 eassert (XOVERLAY (overlay)->next == NULL);
3983 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3984 Fset_marker (OVERLAY_END (overlay), end, buffer);
3986 /* Put the overlay on the wrong list. */
3987 end = OVERLAY_END (overlay);
3988 if (OVERLAY_POSITION (end) < b->overlay_center)
3990 XOVERLAY (overlay)->next = b->overlays_after;
3991 b->overlays_after = XOVERLAY (overlay);
3993 else
3995 XOVERLAY (overlay)->next = b->overlays_before;
3996 b->overlays_before = XOVERLAY (overlay);
3999 /* This puts it in the right list, and in the right order. */
4000 recenter_overlay_lists (b, b->overlay_center);
4002 return unbind_to (count, overlay);
4005 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
4006 doc: /* Delete the overlay OVERLAY from its buffer. */)
4007 (overlay)
4008 Lisp_Object overlay;
4010 Lisp_Object buffer;
4011 struct buffer *b;
4012 int count = SPECPDL_INDEX ();
4014 CHECK_OVERLAY (overlay);
4016 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4017 if (NILP (buffer))
4018 return Qnil;
4020 b = XBUFFER (buffer);
4021 specbind (Qinhibit_quit, Qt);
4023 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4024 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4025 eassert (XOVERLAY (overlay)->next == NULL);
4026 modify_overlay (b,
4027 marker_position (OVERLAY_START (overlay)),
4028 marker_position (OVERLAY_END (overlay)));
4029 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4030 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4032 /* When deleting an overlay with before or after strings, turn off
4033 display optimizations for the affected buffer, on the basis that
4034 these strings may contain newlines. This is easier to do than to
4035 check for that situation during redisplay. */
4036 if (!windows_or_buffers_changed
4037 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4038 || !NILP (Foverlay_get (overlay, Qafter_string))))
4039 b->prevent_redisplay_optimizations_p = 1;
4041 return unbind_to (count, Qnil);
4044 /* Overlay dissection functions. */
4046 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4047 doc: /* Return the position at which OVERLAY starts. */)
4048 (overlay)
4049 Lisp_Object overlay;
4051 CHECK_OVERLAY (overlay);
4053 return (Fmarker_position (OVERLAY_START (overlay)));
4056 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4057 doc: /* Return the position at which OVERLAY ends. */)
4058 (overlay)
4059 Lisp_Object overlay;
4061 CHECK_OVERLAY (overlay);
4063 return (Fmarker_position (OVERLAY_END (overlay)));
4066 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4067 doc: /* Return the buffer OVERLAY belongs to.
4068 Return nil if OVERLAY has been deleted. */)
4069 (overlay)
4070 Lisp_Object overlay;
4072 CHECK_OVERLAY (overlay);
4074 return Fmarker_buffer (OVERLAY_START (overlay));
4077 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4078 doc: /* Return a list of the properties on OVERLAY.
4079 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4080 OVERLAY. */)
4081 (overlay)
4082 Lisp_Object overlay;
4084 CHECK_OVERLAY (overlay);
4086 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4090 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4091 doc: /* Return a list of the overlays that contain the character at POS. */)
4092 (pos)
4093 Lisp_Object pos;
4095 int noverlays;
4096 Lisp_Object *overlay_vec;
4097 int len;
4098 Lisp_Object result;
4100 CHECK_NUMBER_COERCE_MARKER (pos);
4102 len = 10;
4103 /* We can't use alloca here because overlays_at can call xrealloc. */
4104 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4106 /* Put all the overlays we want in a vector in overlay_vec.
4107 Store the length in len. */
4108 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4109 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4111 /* Make a list of them all. */
4112 result = Flist (noverlays, overlay_vec);
4114 xfree (overlay_vec);
4115 return result;
4118 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4119 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4120 Overlap means that at least one character is contained within the overlay
4121 and also contained within the specified region.
4122 Empty overlays are included in the result if they are located at BEG,
4123 between BEG and END, or at END provided END denotes the position at the
4124 end of the buffer. */)
4125 (beg, end)
4126 Lisp_Object beg, end;
4128 int noverlays;
4129 Lisp_Object *overlay_vec;
4130 int len;
4131 Lisp_Object result;
4133 CHECK_NUMBER_COERCE_MARKER (beg);
4134 CHECK_NUMBER_COERCE_MARKER (end);
4136 len = 10;
4137 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4139 /* Put all the overlays we want in a vector in overlay_vec.
4140 Store the length in len. */
4141 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4142 (int *) 0, (int *) 0);
4144 /* Make a list of them all. */
4145 result = Flist (noverlays, overlay_vec);
4147 xfree (overlay_vec);
4148 return result;
4151 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4152 1, 1, 0,
4153 doc: /* Return the next position after POS where an overlay starts or ends.
4154 If there are no overlay boundaries from POS to (point-max),
4155 the value is (point-max). */)
4156 (pos)
4157 Lisp_Object pos;
4159 int noverlays;
4160 EMACS_INT endpos;
4161 Lisp_Object *overlay_vec;
4162 int len;
4163 int i;
4165 CHECK_NUMBER_COERCE_MARKER (pos);
4167 len = 10;
4168 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4170 /* Put all the overlays we want in a vector in overlay_vec.
4171 Store the length in len.
4172 endpos gets the position where the next overlay starts. */
4173 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4174 &endpos, (EMACS_INT *) 0, 1);
4176 /* If any of these overlays ends before endpos,
4177 use its ending point instead. */
4178 for (i = 0; i < noverlays; i++)
4180 Lisp_Object oend;
4181 EMACS_INT oendpos;
4183 oend = OVERLAY_END (overlay_vec[i]);
4184 oendpos = OVERLAY_POSITION (oend);
4185 if (oendpos < endpos)
4186 endpos = oendpos;
4189 xfree (overlay_vec);
4190 return make_number (endpos);
4193 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4194 Sprevious_overlay_change, 1, 1, 0,
4195 doc: /* Return the previous position before POS where an overlay starts or ends.
4196 If there are no overlay boundaries from (point-min) to POS,
4197 the value is (point-min). */)
4198 (pos)
4199 Lisp_Object pos;
4201 int noverlays;
4202 EMACS_INT prevpos;
4203 Lisp_Object *overlay_vec;
4204 int len;
4206 CHECK_NUMBER_COERCE_MARKER (pos);
4208 /* At beginning of buffer, we know the answer;
4209 avoid bug subtracting 1 below. */
4210 if (XINT (pos) == BEGV)
4211 return pos;
4213 len = 10;
4214 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4216 /* Put all the overlays we want in a vector in overlay_vec.
4217 Store the length in len.
4218 prevpos gets the position of the previous change. */
4219 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4220 (EMACS_INT *) 0, &prevpos, 1);
4222 xfree (overlay_vec);
4223 return make_number (prevpos);
4226 /* These functions are for debugging overlays. */
4228 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4229 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4230 The car has all the overlays before the overlay center;
4231 the cdr has all the overlays after the overlay center.
4232 Recentering overlays moves overlays between these lists.
4233 The lists you get are copies, so that changing them has no effect.
4234 However, the overlays you get are the real objects that the buffer uses. */)
4237 struct Lisp_Overlay *ol;
4238 Lisp_Object before = Qnil, after = Qnil, tmp;
4239 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4241 XSETMISC (tmp, ol);
4242 before = Fcons (tmp, before);
4244 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4246 XSETMISC (tmp, ol);
4247 after = Fcons (tmp, after);
4249 return Fcons (Fnreverse (before), Fnreverse (after));
4252 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4253 doc: /* Recenter the overlays of the current buffer around position POS.
4254 That makes overlay lookup faster for positions near POS (but perhaps slower
4255 for positions far away from POS). */)
4256 (pos)
4257 Lisp_Object pos;
4259 CHECK_NUMBER_COERCE_MARKER (pos);
4261 recenter_overlay_lists (current_buffer, XINT (pos));
4262 return Qnil;
4265 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4266 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4267 (overlay, prop)
4268 Lisp_Object overlay, prop;
4270 CHECK_OVERLAY (overlay);
4271 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4274 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4275 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4276 (overlay, prop, value)
4277 Lisp_Object overlay, prop, value;
4279 Lisp_Object tail, buffer;
4280 int changed;
4282 CHECK_OVERLAY (overlay);
4284 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4286 for (tail = XOVERLAY (overlay)->plist;
4287 CONSP (tail) && CONSP (XCDR (tail));
4288 tail = XCDR (XCDR (tail)))
4289 if (EQ (XCAR (tail), prop))
4291 changed = !EQ (XCAR (XCDR (tail)), value);
4292 XSETCAR (XCDR (tail), value);
4293 goto found;
4295 /* It wasn't in the list, so add it to the front. */
4296 changed = !NILP (value);
4297 XOVERLAY (overlay)->plist
4298 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4299 found:
4300 if (! NILP (buffer))
4302 if (changed)
4303 modify_overlay (XBUFFER (buffer),
4304 marker_position (OVERLAY_START (overlay)),
4305 marker_position (OVERLAY_END (overlay)));
4306 if (EQ (prop, Qevaporate) && ! NILP (value)
4307 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4308 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4309 Fdelete_overlay (overlay);
4312 return value;
4315 /* Subroutine of report_overlay_modification. */
4317 /* Lisp vector holding overlay hook functions to call.
4318 Vector elements come in pairs.
4319 Each even-index element is a list of hook functions.
4320 The following odd-index element is the overlay they came from.
4322 Before the buffer change, we fill in this vector
4323 as we call overlay hook functions.
4324 After the buffer change, we get the functions to call from this vector.
4325 This way we always call the same functions before and after the change. */
4326 static Lisp_Object last_overlay_modification_hooks;
4328 /* Number of elements actually used in last_overlay_modification_hooks. */
4329 static int last_overlay_modification_hooks_used;
4331 /* Add one functionlist/overlay pair
4332 to the end of last_overlay_modification_hooks. */
4334 static void
4335 add_overlay_mod_hooklist (functionlist, overlay)
4336 Lisp_Object functionlist, overlay;
4338 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4340 if (last_overlay_modification_hooks_used == oldsize)
4341 last_overlay_modification_hooks = larger_vector
4342 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4343 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4344 functionlist); last_overlay_modification_hooks_used++;
4345 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4346 overlay); last_overlay_modification_hooks_used++;
4349 /* Run the modification-hooks of overlays that include
4350 any part of the text in START to END.
4351 If this change is an insertion, also
4352 run the insert-before-hooks of overlay starting at END,
4353 and the insert-after-hooks of overlay ending at START.
4355 This is called both before and after the modification.
4356 AFTER is nonzero when we call after the modification.
4358 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4359 When AFTER is nonzero, they are the start position,
4360 the position after the inserted new text,
4361 and the length of deleted or replaced old text. */
4363 void
4364 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4365 Lisp_Object start, end;
4366 int after;
4367 Lisp_Object arg1, arg2, arg3;
4369 Lisp_Object prop, overlay;
4370 struct Lisp_Overlay *tail;
4371 /* 1 if this change is an insertion. */
4372 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4373 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4375 overlay = Qnil;
4376 tail = NULL;
4378 /* We used to run the functions as soon as we found them and only register
4379 them in last_overlay_modification_hooks for the purpose of the `after'
4380 case. But running elisp code as we traverse the list of overlays is
4381 painful because the list can be modified by the elisp code so we had to
4382 copy at several places. We now simply do a read-only traversal that
4383 only collects the functions to run and we run them afterwards. It's
4384 simpler, especially since all the code was already there. -stef */
4386 if (!after)
4388 /* We are being called before a change.
4389 Scan the overlays to find the functions to call. */
4390 last_overlay_modification_hooks_used = 0;
4391 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4393 int startpos, endpos;
4394 Lisp_Object ostart, oend;
4396 XSETMISC (overlay, tail);
4398 ostart = OVERLAY_START (overlay);
4399 oend = OVERLAY_END (overlay);
4400 endpos = OVERLAY_POSITION (oend);
4401 if (XFASTINT (start) > endpos)
4402 break;
4403 startpos = OVERLAY_POSITION (ostart);
4404 if (insertion && (XFASTINT (start) == startpos
4405 || XFASTINT (end) == startpos))
4407 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4408 if (!NILP (prop))
4409 add_overlay_mod_hooklist (prop, overlay);
4411 if (insertion && (XFASTINT (start) == endpos
4412 || XFASTINT (end) == endpos))
4414 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4415 if (!NILP (prop))
4416 add_overlay_mod_hooklist (prop, overlay);
4418 /* Test for intersecting intervals. This does the right thing
4419 for both insertion and deletion. */
4420 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4422 prop = Foverlay_get (overlay, Qmodification_hooks);
4423 if (!NILP (prop))
4424 add_overlay_mod_hooklist (prop, overlay);
4428 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4430 int startpos, endpos;
4431 Lisp_Object ostart, oend;
4433 XSETMISC (overlay, tail);
4435 ostart = OVERLAY_START (overlay);
4436 oend = OVERLAY_END (overlay);
4437 startpos = OVERLAY_POSITION (ostart);
4438 endpos = OVERLAY_POSITION (oend);
4439 if (XFASTINT (end) < startpos)
4440 break;
4441 if (insertion && (XFASTINT (start) == startpos
4442 || XFASTINT (end) == startpos))
4444 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4445 if (!NILP (prop))
4446 add_overlay_mod_hooklist (prop, overlay);
4448 if (insertion && (XFASTINT (start) == endpos
4449 || XFASTINT (end) == endpos))
4451 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4452 if (!NILP (prop))
4453 add_overlay_mod_hooklist (prop, overlay);
4455 /* Test for intersecting intervals. This does the right thing
4456 for both insertion and deletion. */
4457 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4459 prop = Foverlay_get (overlay, Qmodification_hooks);
4460 if (!NILP (prop))
4461 add_overlay_mod_hooklist (prop, overlay);
4466 GCPRO4 (overlay, arg1, arg2, arg3);
4468 /* Call the functions recorded in last_overlay_modification_hooks.
4469 First copy the vector contents, in case some of these hooks
4470 do subsequent modification of the buffer. */
4471 int size = last_overlay_modification_hooks_used;
4472 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4473 int i;
4475 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4476 copy, size * sizeof (Lisp_Object));
4477 gcpro1.var = copy;
4478 gcpro1.nvars = size;
4480 for (i = 0; i < size;)
4482 Lisp_Object prop, overlay;
4483 prop = copy[i++];
4484 overlay = copy[i++];
4485 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4488 UNGCPRO;
4491 static void
4492 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4493 Lisp_Object list, overlay;
4494 int after;
4495 Lisp_Object arg1, arg2, arg3;
4497 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4499 GCPRO4 (list, arg1, arg2, arg3);
4501 while (CONSP (list))
4503 if (NILP (arg3))
4504 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4505 else
4506 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4507 list = XCDR (list);
4509 UNGCPRO;
4512 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4513 property is set. */
4514 void
4515 evaporate_overlays (pos)
4516 EMACS_INT pos;
4518 Lisp_Object overlay, hit_list;
4519 struct Lisp_Overlay *tail;
4521 hit_list = Qnil;
4522 if (pos <= current_buffer->overlay_center)
4523 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4525 int endpos;
4526 XSETMISC (overlay, tail);
4527 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4528 if (endpos < pos)
4529 break;
4530 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4531 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4532 hit_list = Fcons (overlay, hit_list);
4534 else
4535 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4537 int startpos;
4538 XSETMISC (overlay, tail);
4539 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4540 if (startpos > pos)
4541 break;
4542 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4543 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4544 hit_list = Fcons (overlay, hit_list);
4546 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4547 Fdelete_overlay (XCAR (hit_list));
4550 /* Somebody has tried to store a value with an unacceptable type
4551 in the slot with offset OFFSET. */
4553 void
4554 buffer_slot_type_mismatch (sym, type)
4555 Lisp_Object sym;
4556 int type;
4558 char *type_name;
4560 switch (type)
4562 case Lisp_Int:
4563 type_name = "integers";
4564 break;
4566 case Lisp_String:
4567 type_name = "strings";
4568 break;
4570 case Lisp_Symbol:
4571 type_name = "symbols";
4572 break;
4574 default:
4575 abort ();
4578 error ("Only %s should be stored in the buffer-local variable %s",
4579 type_name, SDATA (SYMBOL_NAME (sym)));
4583 /***********************************************************************
4584 Allocation with mmap
4585 ***********************************************************************/
4587 #ifdef USE_MMAP_FOR_BUFFERS
4589 #include <sys/types.h>
4590 #include <sys/mman.h>
4592 #ifndef MAP_ANON
4593 #ifdef MAP_ANONYMOUS
4594 #define MAP_ANON MAP_ANONYMOUS
4595 #else
4596 #define MAP_ANON 0
4597 #endif
4598 #endif
4600 #ifndef MAP_FAILED
4601 #define MAP_FAILED ((void *) -1)
4602 #endif
4604 #include <stdio.h>
4605 #include <errno.h>
4607 #if MAP_ANON == 0
4608 #include <fcntl.h>
4609 #endif
4611 #include "coding.h"
4614 /* Memory is allocated in regions which are mapped using mmap(2).
4615 The current implementation lets the system select mapped
4616 addresses; we're not using MAP_FIXED in general, except when
4617 trying to enlarge regions.
4619 Each mapped region starts with a mmap_region structure, the user
4620 area starts after that structure, aligned to MEM_ALIGN.
4622 +-----------------------+
4623 | struct mmap_info + |
4624 | padding |
4625 +-----------------------+
4626 | user data |
4629 +-----------------------+ */
4631 struct mmap_region
4633 /* User-specified size. */
4634 size_t nbytes_specified;
4636 /* Number of bytes mapped */
4637 size_t nbytes_mapped;
4639 /* Pointer to the location holding the address of the memory
4640 allocated with the mmap'd block. The variable actually points
4641 after this structure. */
4642 POINTER_TYPE **var;
4644 /* Next and previous in list of all mmap'd regions. */
4645 struct mmap_region *next, *prev;
4648 /* Doubly-linked list of mmap'd regions. */
4650 static struct mmap_region *mmap_regions;
4652 /* File descriptor for mmap. If we don't have anonymous mapping,
4653 /dev/zero will be opened on it. */
4655 static int mmap_fd;
4657 /* Temporary storage for mmap_set_vars, see there. */
4659 static struct mmap_region *mmap_regions_1;
4660 static int mmap_fd_1;
4662 /* Page size on this system. */
4664 static int mmap_page_size;
4666 /* 1 means mmap has been intialized. */
4668 static int mmap_initialized_p;
4670 /* Value is X rounded up to the next multiple of N. */
4672 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4674 /* Size of mmap_region structure plus padding. */
4676 #define MMAP_REGION_STRUCT_SIZE \
4677 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4679 /* Given a pointer P to the start of the user-visible part of a mapped
4680 region, return a pointer to the start of the region. */
4682 #define MMAP_REGION(P) \
4683 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4685 /* Given a pointer P to the start of a mapped region, return a pointer
4686 to the start of the user-visible part of the region. */
4688 #define MMAP_USER_AREA(P) \
4689 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4691 #define MEM_ALIGN sizeof (double)
4693 /* Predicate returning true if part of the address range [START .. END]
4694 is currently mapped. Used to prevent overwriting an existing
4695 memory mapping.
4697 Default is to conservativly assume the address range is occupied by
4698 something else. This can be overridden by system configuration
4699 files if system-specific means to determine this exists. */
4701 #ifndef MMAP_ALLOCATED_P
4702 #define MMAP_ALLOCATED_P(start, end) 1
4703 #endif
4705 /* Function prototypes. */
4707 static int mmap_free_1 P_ ((struct mmap_region *));
4708 static int mmap_enlarge P_ ((struct mmap_region *, int));
4709 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4710 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4711 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4712 static void mmap_free P_ ((POINTER_TYPE **ptr));
4713 static void mmap_init P_ ((void));
4716 /* Return a region overlapping address range START...END, or null if
4717 none. END is not including, i.e. the last byte in the range
4718 is at END - 1. */
4720 static struct mmap_region *
4721 mmap_find (start, end)
4722 POINTER_TYPE *start, *end;
4724 struct mmap_region *r;
4725 char *s = (char *) start, *e = (char *) end;
4727 for (r = mmap_regions; r; r = r->next)
4729 char *rstart = (char *) r;
4730 char *rend = rstart + r->nbytes_mapped;
4732 if (/* First byte of range, i.e. START, in this region? */
4733 (s >= rstart && s < rend)
4734 /* Last byte of range, i.e. END - 1, in this region? */
4735 || (e > rstart && e <= rend)
4736 /* First byte of this region in the range? */
4737 || (rstart >= s && rstart < e)
4738 /* Last byte of this region in the range? */
4739 || (rend > s && rend <= e))
4740 break;
4743 return r;
4747 /* Unmap a region. P is a pointer to the start of the user-araa of
4748 the region. Value is non-zero if successful. */
4750 static int
4751 mmap_free_1 (r)
4752 struct mmap_region *r;
4754 if (r->next)
4755 r->next->prev = r->prev;
4756 if (r->prev)
4757 r->prev->next = r->next;
4758 else
4759 mmap_regions = r->next;
4761 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4763 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4764 return 0;
4767 return 1;
4771 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4772 Value is non-zero if successful. */
4774 static int
4775 mmap_enlarge (r, npages)
4776 struct mmap_region *r;
4777 int npages;
4779 char *region_end = (char *) r + r->nbytes_mapped;
4780 size_t nbytes;
4781 int success = 0;
4783 if (npages < 0)
4785 /* Unmap pages at the end of the region. */
4786 nbytes = - npages * mmap_page_size;
4787 if (munmap (region_end - nbytes, nbytes) == -1)
4788 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4789 else
4791 r->nbytes_mapped -= nbytes;
4792 success = 1;
4795 else if (npages > 0)
4797 nbytes = npages * mmap_page_size;
4799 /* Try to map additional pages at the end of the region. We
4800 cannot do this if the address range is already occupied by
4801 something else because mmap deletes any previous mapping.
4802 I'm not sure this is worth doing, let's see. */
4803 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4805 POINTER_TYPE *p;
4807 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4808 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4809 if (p == MAP_FAILED)
4810 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4811 else if (p != (POINTER_TYPE *) region_end)
4813 /* Kernels are free to choose a different address. In
4814 that case, unmap what we've mapped above; we have
4815 no use for it. */
4816 if (munmap (p, nbytes) == -1)
4817 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4819 else
4821 r->nbytes_mapped += nbytes;
4822 success = 1;
4827 return success;
4831 /* Set or reset variables holding references to mapped regions. If
4832 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4833 non-zero, set all variables to the start of the user-areas
4834 of mapped regions.
4836 This function is called from Fdump_emacs to ensure that the dumped
4837 Emacs doesn't contain references to memory that won't be mapped
4838 when Emacs starts. */
4840 void
4841 mmap_set_vars (restore_p)
4842 int restore_p;
4844 struct mmap_region *r;
4846 if (restore_p)
4848 mmap_regions = mmap_regions_1;
4849 mmap_fd = mmap_fd_1;
4850 for (r = mmap_regions; r; r = r->next)
4851 *r->var = MMAP_USER_AREA (r);
4853 else
4855 for (r = mmap_regions; r; r = r->next)
4856 *r->var = NULL;
4857 mmap_regions_1 = mmap_regions;
4858 mmap_regions = NULL;
4859 mmap_fd_1 = mmap_fd;
4860 mmap_fd = -1;
4865 /* Allocate a block of storage large enough to hold NBYTES bytes of
4866 data. A pointer to the data is returned in *VAR. VAR is thus the
4867 address of some variable which will use the data area.
4869 The allocation of 0 bytes is valid.
4871 If we can't allocate the necessary memory, set *VAR to null, and
4872 return null. */
4874 static POINTER_TYPE *
4875 mmap_alloc (var, nbytes)
4876 POINTER_TYPE **var;
4877 size_t nbytes;
4879 void *p;
4880 size_t map;
4882 mmap_init ();
4884 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4885 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4886 mmap_fd, 0);
4888 if (p == MAP_FAILED)
4890 if (errno != ENOMEM)
4891 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4892 p = NULL;
4894 else
4896 struct mmap_region *r = (struct mmap_region *) p;
4898 r->nbytes_specified = nbytes;
4899 r->nbytes_mapped = map;
4900 r->var = var;
4901 r->prev = NULL;
4902 r->next = mmap_regions;
4903 if (r->next)
4904 r->next->prev = r;
4905 mmap_regions = r;
4907 p = MMAP_USER_AREA (p);
4910 return *var = p;
4914 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4915 resize it to size NBYTES. Change *VAR to reflect the new block,
4916 and return this value. If more memory cannot be allocated, then
4917 leave *VAR unchanged, and return null. */
4919 static POINTER_TYPE *
4920 mmap_realloc (var, nbytes)
4921 POINTER_TYPE **var;
4922 size_t nbytes;
4924 POINTER_TYPE *result;
4926 mmap_init ();
4928 if (*var == NULL)
4929 result = mmap_alloc (var, nbytes);
4930 else if (nbytes == 0)
4932 mmap_free (var);
4933 result = mmap_alloc (var, nbytes);
4935 else
4937 struct mmap_region *r = MMAP_REGION (*var);
4938 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4940 if (room < nbytes)
4942 /* Must enlarge. */
4943 POINTER_TYPE *old_ptr = *var;
4945 /* Try to map additional pages at the end of the region.
4946 If that fails, allocate a new region, copy data
4947 from the old region, then free it. */
4948 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4949 / mmap_page_size)))
4951 r->nbytes_specified = nbytes;
4952 *var = result = old_ptr;
4954 else if (mmap_alloc (var, nbytes))
4956 bcopy (old_ptr, *var, r->nbytes_specified);
4957 mmap_free_1 (MMAP_REGION (old_ptr));
4958 result = *var;
4959 r = MMAP_REGION (result);
4960 r->nbytes_specified = nbytes;
4962 else
4964 *var = old_ptr;
4965 result = NULL;
4968 else if (room - nbytes >= mmap_page_size)
4970 /* Shrinking by at least a page. Let's give some
4971 memory back to the system.
4973 The extra parens are to make the division happens first,
4974 on positive values, so we know it will round towards
4975 zero. */
4976 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4977 result = *var;
4978 r->nbytes_specified = nbytes;
4980 else
4982 /* Leave it alone. */
4983 result = *var;
4984 r->nbytes_specified = nbytes;
4988 return result;
4992 /* Free a block of relocatable storage whose data is pointed to by
4993 PTR. Store 0 in *PTR to show there's no block allocated. */
4995 static void
4996 mmap_free (var)
4997 POINTER_TYPE **var;
4999 mmap_init ();
5001 if (*var)
5003 mmap_free_1 (MMAP_REGION (*var));
5004 *var = NULL;
5009 /* Perform necessary intializations for the use of mmap. */
5011 static void
5012 mmap_init ()
5014 #if MAP_ANON == 0
5015 /* The value of mmap_fd is initially 0 in temacs, and -1
5016 in a dumped Emacs. */
5017 if (mmap_fd <= 0)
5019 /* No anonymous mmap -- we need the file descriptor. */
5020 mmap_fd = open ("/dev/zero", O_RDONLY);
5021 if (mmap_fd == -1)
5022 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5024 #endif /* MAP_ANON == 0 */
5026 if (mmap_initialized_p)
5027 return;
5028 mmap_initialized_p = 1;
5030 #if MAP_ANON != 0
5031 mmap_fd = -1;
5032 #endif
5034 mmap_page_size = getpagesize ();
5037 #endif /* USE_MMAP_FOR_BUFFERS */
5041 /***********************************************************************
5042 Buffer-text Allocation
5043 ***********************************************************************/
5045 #ifdef REL_ALLOC
5046 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5047 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5048 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5049 #endif /* REL_ALLOC */
5052 /* Allocate NBYTES bytes for buffer B's text buffer. */
5054 static void
5055 alloc_buffer_text (b, nbytes)
5056 struct buffer *b;
5057 size_t nbytes;
5059 POINTER_TYPE *p;
5061 BLOCK_INPUT;
5062 #if defined USE_MMAP_FOR_BUFFERS
5063 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5064 #elif defined REL_ALLOC
5065 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5066 #else
5067 p = xmalloc (nbytes);
5068 #endif
5070 if (p == NULL)
5072 UNBLOCK_INPUT;
5073 memory_full ();
5076 b->text->beg = (unsigned char *) p;
5077 UNBLOCK_INPUT;
5080 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5081 shrink it. */
5083 void
5084 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5086 POINTER_TYPE *p;
5087 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5088 + delta);
5089 BLOCK_INPUT;
5090 #if defined USE_MMAP_FOR_BUFFERS
5091 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5092 #elif defined REL_ALLOC
5093 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5094 #else
5095 p = xrealloc (b->text->beg, nbytes);
5096 #endif
5098 if (p == NULL)
5100 UNBLOCK_INPUT;
5101 memory_full ();
5104 BUF_BEG_ADDR (b) = (unsigned char *) p;
5105 UNBLOCK_INPUT;
5109 /* Free buffer B's text buffer. */
5111 static void
5112 free_buffer_text (b)
5113 struct buffer *b;
5115 BLOCK_INPUT;
5117 #if defined USE_MMAP_FOR_BUFFERS
5118 mmap_free ((POINTER_TYPE **) &b->text->beg);
5119 #elif defined REL_ALLOC
5120 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5121 #else
5122 xfree (b->text->beg);
5123 #endif
5125 BUF_BEG_ADDR (b) = NULL;
5126 UNBLOCK_INPUT;
5131 /***********************************************************************
5132 Initialization
5133 ***********************************************************************/
5135 void
5136 init_buffer_once ()
5138 int idx;
5140 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5142 /* Make sure all markable slots in buffer_defaults
5143 are initialized reasonably, so mark_buffer won't choke. */
5144 reset_buffer (&buffer_defaults);
5145 reset_buffer_local_variables (&buffer_defaults, 1);
5146 reset_buffer (&buffer_local_symbols);
5147 reset_buffer_local_variables (&buffer_local_symbols, 1);
5148 /* Prevent GC from getting confused. */
5149 buffer_defaults.text = &buffer_defaults.own_text;
5150 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5151 BUF_INTERVALS (&buffer_defaults) = 0;
5152 BUF_INTERVALS (&buffer_local_symbols) = 0;
5153 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5154 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5155 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5156 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5158 /* Set up the default values of various buffer slots. */
5159 /* Must do these before making the first buffer! */
5161 /* real setup is done in bindings.el */
5162 buffer_defaults.mode_line_format = build_string ("%-");
5163 buffer_defaults.header_line_format = Qnil;
5164 buffer_defaults.abbrev_mode = Qnil;
5165 buffer_defaults.overwrite_mode = Qnil;
5166 buffer_defaults.case_fold_search = Qt;
5167 buffer_defaults.auto_fill_function = Qnil;
5168 buffer_defaults.selective_display = Qnil;
5169 #ifndef old
5170 buffer_defaults.selective_display_ellipses = Qt;
5171 #endif
5172 buffer_defaults.abbrev_table = Qnil;
5173 buffer_defaults.display_table = Qnil;
5174 buffer_defaults.undo_list = Qnil;
5175 buffer_defaults.mark_active = Qnil;
5176 buffer_defaults.file_format = Qnil;
5177 buffer_defaults.auto_save_file_format = Qt;
5178 buffer_defaults.overlays_before = NULL;
5179 buffer_defaults.overlays_after = NULL;
5180 buffer_defaults.overlay_center = BEG;
5182 XSETFASTINT (buffer_defaults.tab_width, 8);
5183 buffer_defaults.truncate_lines = Qnil;
5184 buffer_defaults.ctl_arrow = Qt;
5185 buffer_defaults.direction_reversed = Qnil;
5186 buffer_defaults.cursor_type = Qt;
5187 buffer_defaults.extra_line_spacing = Qnil;
5188 buffer_defaults.cursor_in_non_selected_windows = Qt;
5190 #ifdef DOS_NT
5191 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5192 #endif
5193 buffer_defaults.enable_multibyte_characters = Qt;
5194 buffer_defaults.buffer_file_coding_system = Qnil;
5195 XSETFASTINT (buffer_defaults.fill_column, 70);
5196 XSETFASTINT (buffer_defaults.left_margin, 0);
5197 buffer_defaults.cache_long_line_scans = Qnil;
5198 buffer_defaults.file_truename = Qnil;
5199 XSETFASTINT (buffer_defaults.display_count, 0);
5200 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5201 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5202 buffer_defaults.left_fringe_width = Qnil;
5203 buffer_defaults.right_fringe_width = Qnil;
5204 buffer_defaults.fringes_outside_margins = Qnil;
5205 buffer_defaults.scroll_bar_width = Qnil;
5206 buffer_defaults.vertical_scroll_bar_type = Qt;
5207 buffer_defaults.indicate_empty_lines = Qnil;
5208 buffer_defaults.indicate_buffer_boundaries = Qnil;
5209 buffer_defaults.fringe_indicator_alist = Qnil;
5210 buffer_defaults.fringe_cursor_alist = Qnil;
5211 buffer_defaults.scroll_up_aggressively = Qnil;
5212 buffer_defaults.scroll_down_aggressively = Qnil;
5213 buffer_defaults.display_time = Qnil;
5215 /* Assign the local-flags to the slots that have default values.
5216 The local flag is a bit that is used in the buffer
5217 to say that it has its own local value for the slot.
5218 The local flag bits are in the local_var_flags slot of the buffer. */
5220 /* Nothing can work if this isn't true */
5221 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5223 /* 0 means not a lisp var, -1 means always local, else mask */
5224 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5225 XSETINT (buffer_local_flags.filename, -1);
5226 XSETINT (buffer_local_flags.directory, -1);
5227 XSETINT (buffer_local_flags.backed_up, -1);
5228 XSETINT (buffer_local_flags.save_length, -1);
5229 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5230 XSETINT (buffer_local_flags.read_only, -1);
5231 XSETINT (buffer_local_flags.major_mode, -1);
5232 XSETINT (buffer_local_flags.mode_name, -1);
5233 XSETINT (buffer_local_flags.undo_list, -1);
5234 XSETINT (buffer_local_flags.mark_active, -1);
5235 XSETINT (buffer_local_flags.point_before_scroll, -1);
5236 XSETINT (buffer_local_flags.file_truename, -1);
5237 XSETINT (buffer_local_flags.invisibility_spec, -1);
5238 XSETINT (buffer_local_flags.file_format, -1);
5239 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5240 XSETINT (buffer_local_flags.display_count, -1);
5241 XSETINT (buffer_local_flags.display_time, -1);
5242 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5244 idx = 1;
5245 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5246 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5247 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5248 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5249 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5250 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5251 #ifndef old
5252 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5253 #endif
5254 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5255 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5256 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5257 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5258 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5259 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5260 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5261 #ifdef DOS_NT
5262 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5263 /* Make this one a permanent local. */
5264 buffer_permanent_local_flags[idx++] = 1;
5265 #endif
5266 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5267 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5268 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5269 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5270 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5271 /* Make this one a permanent local. */
5272 buffer_permanent_local_flags[idx++] = 1;
5273 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5274 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5275 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5276 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5277 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5278 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5279 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5280 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5281 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5282 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5283 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5284 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5285 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5286 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5287 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5288 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5289 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5291 /* Need more room? */
5292 if (idx >= MAX_PER_BUFFER_VARS)
5293 abort ();
5294 last_per_buffer_idx = idx;
5296 Vbuffer_alist = Qnil;
5297 current_buffer = 0;
5298 all_buffers = 0;
5300 QSFundamental = build_string ("Fundamental");
5302 Qfundamental_mode = intern ("fundamental-mode");
5303 buffer_defaults.major_mode = Qfundamental_mode;
5305 Qmode_class = intern ("mode-class");
5307 Qprotected_field = intern ("protected-field");
5309 Qpermanent_local = intern ("permanent-local");
5311 Qkill_buffer_hook = intern ("kill-buffer-hook");
5312 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5314 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5316 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5318 /* super-magic invisible buffer */
5319 Vbuffer_alist = Qnil;
5321 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5323 inhibit_modification_hooks = 0;
5326 void
5327 init_buffer ()
5329 char *pwd;
5330 Lisp_Object temp;
5331 int len;
5333 #ifdef USE_MMAP_FOR_BUFFERS
5335 /* When using the ralloc implementation based on mmap(2), buffer
5336 text pointers will have been set to null in the dumped Emacs.
5337 Map new memory. */
5338 struct buffer *b;
5340 for (b = all_buffers; b; b = b->next)
5341 if (b->text->beg == NULL)
5342 enlarge_buffer_text (b, 0);
5344 #endif /* USE_MMAP_FOR_BUFFERS */
5346 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5347 if (NILP (buffer_defaults.enable_multibyte_characters))
5348 Fset_buffer_multibyte (Qnil);
5350 pwd = get_current_dir_name ();
5352 if (!pwd)
5353 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5355 #ifndef VMS
5356 /* Maybe this should really use some standard subroutine
5357 whose definition is filename syntax dependent. */
5358 len = strlen (pwd);
5359 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5361 /* Grow buffer to add directory separator and '\0'. */
5362 pwd = (char *) xrealloc (pwd, len + 2);
5363 pwd[len] = DIRECTORY_SEP;
5364 pwd[len + 1] = '\0';
5366 #endif /* not VMS */
5368 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5369 if (! NILP (buffer_defaults.enable_multibyte_characters))
5370 /* At this moment, we still don't know how to decode the
5371 directory name. So, we keep the bytes in multibyte form so
5372 that ENCODE_FILE correctly gets the original bytes. */
5373 current_buffer->directory
5374 = string_to_multibyte (current_buffer->directory);
5376 /* Add /: to the front of the name
5377 if it would otherwise be treated as magic. */
5378 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5379 if (! NILP (temp)
5380 /* If the default dir is just /, TEMP is non-nil
5381 because of the ange-ftp completion handler.
5382 However, it is not necessary to turn / into /:/.
5383 So avoid doing that. */
5384 && strcmp ("/", SDATA (current_buffer->directory)))
5385 current_buffer->directory
5386 = concat2 (build_string ("/:"), current_buffer->directory);
5388 temp = get_minibuffer (0);
5389 XBUFFER (temp)->directory = current_buffer->directory;
5391 free (pwd);
5394 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5395 Object stored in the current buffer. address is the address of the slot
5396 in the buffer that is current now. */
5398 /* TYPE is nil for a general Lisp variable.
5399 An integer specifies a type; then only LIsp values
5400 with that type code are allowed (except that nil is allowed too).
5401 LNAME is the LIsp-level variable name.
5402 VNAME is the name of the buffer slot.
5403 DOC is a dummy where you write the doc string as a comment. */
5404 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5405 defvar_per_buffer (lname, vname, type, 0)
5407 static void
5408 defvar_per_buffer (namestring, address, type, doc)
5409 char *namestring;
5410 Lisp_Object *address;
5411 Lisp_Object type;
5412 char *doc;
5414 Lisp_Object sym, val;
5415 int offset;
5417 sym = intern (namestring);
5418 val = allocate_misc ();
5419 offset = (char *)address - (char *)current_buffer;
5421 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5422 XBUFFER_OBJFWD (val)->offset = offset;
5423 XBUFFER_OBJFWD (val)->slottype = type;
5424 SET_SYMBOL_VALUE (sym, val);
5425 PER_BUFFER_SYMBOL (offset) = sym;
5427 if (PER_BUFFER_IDX (offset) == 0)
5428 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5429 slot of buffer_local_flags */
5430 abort ();
5434 /* initialize the buffer routines */
5435 void
5436 syms_of_buffer ()
5438 staticpro (&last_overlay_modification_hooks);
5439 last_overlay_modification_hooks
5440 = Fmake_vector (make_number (10), Qnil);
5442 staticpro (&Vbuffer_defaults);
5443 staticpro (&Vbuffer_local_symbols);
5444 staticpro (&Qfundamental_mode);
5445 staticpro (&Qmode_class);
5446 staticpro (&QSFundamental);
5447 staticpro (&Vbuffer_alist);
5448 staticpro (&Qprotected_field);
5449 staticpro (&Qpermanent_local);
5450 Qpermanent_local_hook = intern ("permanent-local-hook");
5451 staticpro (&Qpermanent_local_hook);
5452 staticpro (&Qkill_buffer_hook);
5453 Qoverlayp = intern ("overlayp");
5454 staticpro (&Qoverlayp);
5455 Qevaporate = intern ("evaporate");
5456 staticpro (&Qevaporate);
5457 Qmodification_hooks = intern ("modification-hooks");
5458 staticpro (&Qmodification_hooks);
5459 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5460 staticpro (&Qinsert_in_front_hooks);
5461 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5462 staticpro (&Qinsert_behind_hooks);
5463 Qget_file_buffer = intern ("get-file-buffer");
5464 staticpro (&Qget_file_buffer);
5465 Qpriority = intern ("priority");
5466 staticpro (&Qpriority);
5467 Qwindow = intern ("window");
5468 staticpro (&Qwindow);
5469 Qbefore_string = intern ("before-string");
5470 staticpro (&Qbefore_string);
5471 Qafter_string = intern ("after-string");
5472 staticpro (&Qafter_string);
5473 Qfirst_change_hook = intern ("first-change-hook");
5474 staticpro (&Qfirst_change_hook);
5475 Qbefore_change_functions = intern ("before-change-functions");
5476 staticpro (&Qbefore_change_functions);
5477 Qafter_change_functions = intern ("after-change-functions");
5478 staticpro (&Qafter_change_functions);
5479 /* The next one is initialized in init_buffer_once. */
5480 staticpro (&Qucs_set_table_for_input);
5482 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5483 staticpro (&Qkill_buffer_query_functions);
5485 Fput (Qprotected_field, Qerror_conditions,
5486 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5487 Fput (Qprotected_field, Qerror_message,
5488 build_string ("Attempt to modify a protected field"));
5490 /* All these use DEFVAR_LISP_NOPRO because the slots in
5491 buffer_defaults will all be marked via Vbuffer_defaults. */
5493 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5494 &buffer_defaults.mode_line_format,
5495 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5496 This is the same as (default-value 'mode-line-format). */);
5498 DEFVAR_LISP_NOPRO ("default-header-line-format",
5499 &buffer_defaults.header_line_format,
5500 doc: /* Default value of `header-line-format' for buffers that don't override it.
5501 This is the same as (default-value 'header-line-format). */);
5503 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5504 doc: /* Default value of `cursor-type' for buffers that don't override it.
5505 This is the same as (default-value 'cursor-type). */);
5507 DEFVAR_LISP_NOPRO ("default-line-spacing",
5508 &buffer_defaults.extra_line_spacing,
5509 doc: /* Default value of `line-spacing' for buffers that don't override it.
5510 This is the same as (default-value 'line-spacing). */);
5512 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5513 &buffer_defaults.cursor_in_non_selected_windows,
5514 doc: /* Default value of `cursor-in-non-selected-windows'.
5515 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5517 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5518 &buffer_defaults.abbrev_mode,
5519 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5520 This is the same as (default-value 'abbrev-mode). */);
5522 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5523 &buffer_defaults.ctl_arrow,
5524 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5525 This is the same as (default-value 'ctl-arrow). */);
5527 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5528 &buffer_defaults.direction_reversed,
5529 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5530 This is the same as (default-value 'direction-reversed). */);
5532 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5533 &buffer_defaults.enable_multibyte_characters,
5534 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5535 This is the same as (default-value 'enable-multibyte-characters). */);
5537 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5538 &buffer_defaults.buffer_file_coding_system,
5539 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5540 This is the same as (default-value 'buffer-file-coding-system). */);
5542 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5543 &buffer_defaults.truncate_lines,
5544 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5545 This is the same as (default-value 'truncate-lines). */);
5547 DEFVAR_LISP_NOPRO ("default-fill-column",
5548 &buffer_defaults.fill_column,
5549 doc: /* Default value of `fill-column' for buffers that do not override it.
5550 This is the same as (default-value 'fill-column). */);
5552 DEFVAR_LISP_NOPRO ("default-left-margin",
5553 &buffer_defaults.left_margin,
5554 doc: /* Default value of `left-margin' for buffers that do not override it.
5555 This is the same as (default-value 'left-margin). */);
5557 DEFVAR_LISP_NOPRO ("default-tab-width",
5558 &buffer_defaults.tab_width,
5559 doc: /* Default value of `tab-width' for buffers that do not override it.
5560 This is the same as (default-value 'tab-width). */);
5562 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5563 &buffer_defaults.case_fold_search,
5564 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5565 This is the same as (default-value 'case-fold-search). */);
5567 #ifdef DOS_NT
5568 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5569 &buffer_defaults.buffer_file_type,
5570 doc: /* Default file type for buffers that do not override it.
5571 This is the same as (default-value 'buffer-file-type).
5572 The file type is nil for text, t for binary. */);
5573 #endif
5575 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5576 &buffer_defaults.left_margin_cols,
5577 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5578 This is the same as (default-value 'left-margin-width). */);
5580 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5581 &buffer_defaults.right_margin_cols,
5582 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5583 This is the same as (default-value 'right-margin-width). */);
5585 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5586 &buffer_defaults.left_fringe_width,
5587 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5588 This is the same as (default-value 'left-fringe-width). */);
5590 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5591 &buffer_defaults.right_fringe_width,
5592 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5593 This is the same as (default-value 'right-fringe-width). */);
5595 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5596 &buffer_defaults.fringes_outside_margins,
5597 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5598 This is the same as (default-value 'fringes-outside-margins). */);
5600 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5601 &buffer_defaults.scroll_bar_width,
5602 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5603 This is the same as (default-value 'scroll-bar-width). */);
5605 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5606 &buffer_defaults.vertical_scroll_bar_type,
5607 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5608 This is the same as (default-value 'vertical-scroll-bar). */);
5610 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5611 &buffer_defaults.indicate_empty_lines,
5612 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5613 This is the same as (default-value 'indicate-empty-lines). */);
5615 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5616 &buffer_defaults.indicate_buffer_boundaries,
5617 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5618 This is the same as (default-value 'indicate-buffer-boundaries). */);
5620 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5621 &buffer_defaults.fringe_indicator_alist,
5622 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5623 This is the same as (default-value 'fringe-indicator-alist'). */);
5625 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5626 &buffer_defaults.fringe_cursor_alist,
5627 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5628 This is the same as (default-value 'fringe-cursor-alist'). */);
5630 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5631 &buffer_defaults.scroll_up_aggressively,
5632 doc: /* Default value of `scroll-up-aggressively'.
5633 This value applies in buffers that don't have their own local values.
5634 This is the same as (default-value 'scroll-up-aggressively). */);
5636 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5637 &buffer_defaults.scroll_down_aggressively,
5638 doc: /* Default value of `scroll-down-aggressively'.
5639 This value applies in buffers that don't have their own local values.
5640 This is the same as (default-value 'scroll-down-aggressively). */);
5642 DEFVAR_PER_BUFFER ("header-line-format",
5643 &current_buffer->header_line_format,
5644 Qnil,
5645 doc: /* Analogous to `mode-line-format', but controls the header line.
5646 The header line appears, optionally, at the top of a window;
5647 the mode line appears at the bottom. */);
5649 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5650 Qnil,
5651 doc: /* Template for displaying mode line for current buffer.
5652 Each buffer has its own value of this variable.
5653 Value may be nil, a string, a symbol or a list or cons cell.
5654 A value of nil means don't display a mode line.
5655 For a symbol, its value is used (but it is ignored if t or nil).
5656 A string appearing directly as the value of a symbol is processed verbatim
5657 in that the %-constructs below are not recognized.
5658 Note that unless the symbol is marked as a `risky-local-variable', all
5659 properties in any strings, as well as all :eval and :propertize forms
5660 in the value of that symbol will be ignored.
5661 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5662 is used as a mode line element. Be careful--FORM should not load any files,
5663 because that can cause an infinite recursion.
5664 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5665 with the specified properties PROPS applied.
5666 For a list whose car is a symbol, the symbol's value is taken,
5667 and if that is non-nil, the cadr of the list is processed recursively.
5668 Otherwise, the caddr of the list (if there is one) is processed.
5669 For a list whose car is a string or list, each element is processed
5670 recursively and the results are effectively concatenated.
5671 For a list whose car is an integer, the cdr of the list is processed
5672 and padded (if the number is positive) or truncated (if negative)
5673 to the width specified by that number.
5674 A string is printed verbatim in the mode line except for %-constructs:
5675 (%-constructs are allowed when the string is the entire mode-line-format
5676 or when it is found in a cons-cell or a list)
5677 %b -- print buffer name. %f -- print visited file name.
5678 %F -- print frame name.
5679 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5680 %& is like %*, but ignore read-only-ness.
5681 % means buffer is read-only and * means it is modified.
5682 For a modified read-only buffer, %* gives % and %+ gives *.
5683 %s -- print process status. %l -- print the current line number.
5684 %c -- print the current column number (this makes editing slower).
5685 To make the column number update correctly in all cases,
5686 `column-number-mode' must be non-nil.
5687 %i -- print the size of the buffer.
5688 %I -- like %i, but use k, M, G, etc., to abbreviate.
5689 %p -- print percent of buffer above top of window, or Top, Bot or All.
5690 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5691 or print Bottom or All.
5692 %n -- print Narrow if appropriate.
5693 %t -- visited file is text or binary (if OS supports this distinction).
5694 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5695 %Z -- like %z, but including the end-of-line format.
5696 %e -- print error message about full memory.
5697 %@ -- print @ or hyphen. @ means that default-directory is on a
5698 remote machine.
5699 %[ -- print one [ for each recursive editing level. %] similar.
5700 %% -- print %. %- -- print infinitely many dashes.
5701 Decimal digits after the % specify field width to which to pad. */);
5703 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5704 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5705 A value of nil means use current buffer's major mode,
5706 provided it is not marked as "special".
5708 When a mode is used by default, `find-file' switches to it
5709 before it reads the contents into the buffer and before
5710 it finishes setting up the buffer. Thus, the mode and
5711 its hooks should not expect certain variables such as
5712 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5714 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5715 make_number (Lisp_Symbol),
5716 doc: /* Symbol for current buffer's major mode. */);
5718 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5719 Qnil,
5720 doc: /* Pretty name of current buffer's major mode.
5721 Usually a string. See `mode-line-format' for other possible forms.
5722 Use the function `format-mode-line' to get the value as a string. */);
5724 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5725 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5727 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5728 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5730 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5731 Qnil,
5732 doc: /* *Non-nil if searches and matches should ignore case. */);
5734 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5735 make_number (Lisp_Int),
5736 doc: /* *Column beyond which automatic line-wrapping should happen.
5737 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5739 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5740 make_number (Lisp_Int),
5741 doc: /* *Column for the default `indent-line-function' to indent to.
5742 Linefeed indents to this column in Fundamental mode. */);
5744 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5745 make_number (Lisp_Int),
5746 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5748 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5749 doc: /* *Non-nil means display control chars with uparrow.
5750 A value of nil means use backslash and octal digits.
5751 This variable does not apply to characters whose display is specified
5752 in the current display table (if there is one). */);
5754 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5755 &current_buffer->enable_multibyte_characters,
5756 Qnil,
5757 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5758 Otherwise they are regarded as unibyte. This affects the display,
5759 file I/O and the behavior of various editing commands.
5761 This variable is buffer-local but you cannot set it directly;
5762 use the function `set-buffer-multibyte' to change a buffer's representation.
5763 Changing its default value with `setq-default' is supported.
5764 See also variable `default-enable-multibyte-characters' and Info node
5765 `(elisp)Text Representations'. */);
5766 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5768 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5769 &current_buffer->buffer_file_coding_system, Qnil,
5770 doc: /* Coding system to be used for encoding the buffer contents on saving.
5771 This variable applies to saving the buffer, and also to `write-region'
5772 and other functions that use `write-region'.
5773 It does not apply to sending output to subprocesses, however.
5775 If this is nil, the buffer is saved without any code conversion
5776 unless some coding system is specified in `file-coding-system-alist'
5777 for the buffer file.
5779 If the text to be saved cannot be encoded as specified by this variable,
5780 an alternative encoding is selected by `select-safe-coding-system', which see.
5782 The variable `coding-system-for-write', if non-nil, overrides this variable.
5784 This variable is never applied to a way of decoding a file while reading it. */);
5786 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5787 Qnil,
5788 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5790 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5791 doc: /* *Non-nil means do not display continuation lines.
5792 Instead, give each line of text just one screen line.
5794 Note that this is overridden by the variable
5795 `truncate-partial-width-windows' if that variable is non-nil
5796 and this buffer is not full-frame width. */);
5798 #ifdef DOS_NT
5799 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5800 Qnil,
5801 doc: /* Non-nil if the visited file is a binary file.
5802 This variable is meaningful on MS-DOG and Windows NT.
5803 On those systems, it is automatically local in every buffer.
5804 On other systems, this variable is normally always nil. */);
5805 #endif
5807 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5808 make_number (Lisp_String),
5809 doc: /* Name of default directory of current buffer. Should end with slash.
5810 To interactively change the default directory, use command `cd'. */);
5812 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5813 Qnil,
5814 doc: /* Function called (if non-nil) to perform auto-fill.
5815 It is called after self-inserting any character specified in
5816 the `auto-fill-chars' table.
5817 NOTE: This variable is not a hook;
5818 its value may not be a list of functions. */);
5820 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5821 make_number (Lisp_String),
5822 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5824 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5825 make_number (Lisp_String),
5826 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5827 The truename of a file is calculated by `file-truename'
5828 and then abbreviated with `abbreviate-file-name'. */);
5830 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5831 &current_buffer->auto_save_file_name,
5832 make_number (Lisp_String),
5833 doc: /* Name of file for auto-saving current buffer.
5834 If it is nil, that means don't auto-save this buffer. */);
5836 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5837 doc: /* Non-nil if this buffer is read-only. */);
5839 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5840 doc: /* Non-nil if this buffer's file has been backed up.
5841 Backing up is done before the first time the file is saved. */);
5843 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5844 make_number (Lisp_Int),
5845 doc: /* Length of current buffer when last read in, saved or auto-saved.
5846 0 initially. */);
5848 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5849 Qnil,
5850 doc: /* Non-nil enables selective display.
5851 An integer N as value means display only lines
5852 that start with less than N columns of space.
5853 A value of t means that the character ^M makes itself and
5854 all the rest of the line invisible; also, when saving the buffer
5855 in a file, save the ^M as a newline. */);
5857 #ifndef old
5858 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5859 &current_buffer->selective_display_ellipses,
5860 Qnil,
5861 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5862 #endif
5864 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5865 doc: /* Non-nil if self-insertion should replace existing text.
5866 The value should be one of `overwrite-mode-textual',
5867 `overwrite-mode-binary', or nil.
5868 If it is `overwrite-mode-textual', self-insertion still
5869 inserts at the end of a line, and inserts when point is before a tab,
5870 until the tab is filled in.
5871 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5873 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5874 Qnil,
5875 doc: /* Display table that controls display of the contents of current buffer.
5877 If this variable is nil, the value of `standard-display-table' is used.
5878 Each window can have its own, overriding display table, see
5879 `set-window-display-table' and `window-display-table'.
5881 The display table is a char-table created with `make-display-table'.
5882 A char-table is an array indexed by character codes. Normal array
5883 primitives `aref' and `aset' can be used to access elements of a char-table.
5885 Each of the char-table elements control how to display the corresponding
5886 text character: the element at index C in the table says how to display
5887 the character whose code is C. Each element should be a vector of
5888 characters or nil. The value nil means display the character in the
5889 default fashion; otherwise, the characters from the vector are delivered
5890 to the screen instead of the original character.
5892 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5893 to display a capital Y instead of each X character.
5895 In addition, a char-table has six extra slots to control the display of:
5897 the end of a truncated screen line (extra-slot 0, a single character);
5898 the end of a continued line (extra-slot 1, a single character);
5899 the escape character used to display character codes in octal
5900 (extra-slot 2, a single character);
5901 the character used as an arrow for control characters (extra-slot 3,
5902 a single character);
5903 the decoration indicating the presence of invisible lines (extra-slot 4,
5904 a vector of characters);
5905 the character used to draw the border between side-by-side windows
5906 (extra-slot 5, a single character).
5908 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5910 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5911 Qnil,
5912 doc: /* *Width of left marginal area for display of a buffer.
5913 A value of nil means no marginal area. */);
5915 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5916 Qnil,
5917 doc: /* *Width of right marginal area for display of a buffer.
5918 A value of nil means no marginal area. */);
5920 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5921 Qnil,
5922 doc: /* *Width of this buffer's left fringe (in pixels).
5923 A value of 0 means no left fringe is shown in this buffer's window.
5924 A value of nil means to use the left fringe width from the window's frame. */);
5926 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5927 Qnil,
5928 doc: /* *Width of this buffer's right fringe (in pixels).
5929 A value of 0 means no right fringe is shown in this buffer's window.
5930 A value of nil means to use the right fringe width from the window's frame. */);
5932 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5933 Qnil,
5934 doc: /* *Non-nil means to display fringes outside display margins.
5935 A value of nil means to display fringes between margins and buffer text. */);
5937 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5938 Qnil,
5939 doc: /* *Width of this buffer's scroll bars in pixels.
5940 A value of nil means to use the scroll bar width from the window's frame. */);
5942 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5943 Qnil,
5944 doc: /* *Position of this buffer's vertical scroll bar.
5945 The value takes effect whenever you tell a window to display this buffer;
5946 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5948 A value of `left' or `right' means put the vertical scroll bar at that side
5949 of the window; a value of nil means don't show any vertical scroll bars.
5950 A value of t (the default) means do whatever the window's frame specifies. */);
5952 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5953 &current_buffer->indicate_empty_lines, Qnil,
5954 doc: /* *Visually indicate empty lines after the buffer end.
5955 If non-nil, a bitmap is displayed in the left fringe of a window on
5956 window-systems. */);
5958 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5959 &current_buffer->indicate_buffer_boundaries, Qnil,
5960 doc: /* *Visually indicate buffer boundaries and scrolling.
5961 If non-nil, the first and last line of the buffer are marked in the fringe
5962 of a window on window-systems with angle bitmaps, or if the window can be
5963 scrolled, the top and bottom line of the window are marked with up and down
5964 arrow bitmaps.
5966 If value is a symbol `left' or `right', both angle and arrow bitmaps
5967 are displayed in the left or right fringe, resp. Any other value
5968 that doesn't look like an alist means display the angle bitmaps in
5969 the left fringe but no arrows.
5971 You can exercise more precise control by using an alist as the
5972 value. Each alist element (INDICATOR . POSITION) specifies
5973 where to show one of the indicators. INDICATOR is one of `top',
5974 `bottom', `up', `down', or t, which specifies the default position,
5975 and POSITION is one of `left', `right', or nil, meaning do not show
5976 this indicator.
5978 For example, ((top . left) (t . right)) places the top angle bitmap in
5979 left fringe, the bottom angle bitmap in right fringe, and both arrow
5980 bitmaps in right fringe. To show just the angle bitmaps in the left
5981 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5983 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5984 &current_buffer->fringe_indicator_alist, Qnil,
5985 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5986 The value is an alist where each element (INDICATOR . BITMAPS)
5987 specifies the fringe bitmaps used to display a specific logical
5988 fringe indicator.
5990 INDICATOR specifies the logical indicator type which is one of the
5991 following symbols: `truncation' , `continuation', `overlay-arrow',
5992 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5994 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5995 the actual bitmap shown in the left or right fringe for the logical
5996 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5997 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5998 are used only for the `bottom' and `one-line' indicators when the last
5999 \(only) line in has no final newline. BITMAPS may also be a single
6000 symbol which is used in both left and right fringes. */);
6002 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
6003 &current_buffer->fringe_cursor_alist, Qnil,
6004 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
6005 The value is an alist where each element (CURSOR . BITMAP)
6006 specifies the fringe bitmaps used to display a specific logical
6007 cursor type in the fringe.
6009 CURSOR specifies the logical cursor type which is one of the following
6010 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6011 one is used to show a hollow cursor on narrow lines display lines
6012 where the normal hollow cursor will not fit.
6014 BITMAP is the corresponding fringe bitmap shown for the logical
6015 cursor type. */);
6017 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
6018 &current_buffer->scroll_up_aggressively, Qnil,
6019 doc: /* How far to scroll windows upward.
6020 If you move point off the bottom, the window scrolls automatically.
6021 This variable controls how far it scrolls. The value nil, the default,
6022 means scroll to center point. A fraction means scroll to put point
6023 that fraction of the window's height from the bottom of the window.
6024 When the value is 0.0, point goes at the bottom line, which in the simple
6025 case that you moved off with C-f means scrolling just one line. 1.0 means
6026 point goes at the top, so that in that simple case, the window
6027 scrolls by a full window height. Meaningful values are
6028 between 0.0 and 1.0, inclusive. */);
6030 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6031 &current_buffer->scroll_down_aggressively, Qnil,
6032 doc: /* How far to scroll windows downward.
6033 If you move point off the top, the window scrolls automatically.
6034 This variable controls how far it scrolls. The value nil, the default,
6035 means scroll to center point. A fraction means scroll to put point
6036 that fraction of the window's height from the top of the window.
6037 When the value is 0.0, point goes at the top line, which in the simple
6038 case that you moved off with C-b means scrolling just one line. 1.0 means
6039 point goes at the bottom, so that in that simple case, the window
6040 scrolls by a full window height. Meaningful values are
6041 between 0.0 and 1.0, inclusive. */);
6043 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6044 "Don't ask.");
6047 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6048 doc: /* List of functions to call before each text change.
6049 Two arguments are passed to each function: the positions of
6050 the beginning and end of the range of old text to be changed.
6051 \(For an insertion, the beginning and end are at the same place.)
6052 No information is given about the length of the text after the change.
6054 Buffer changes made while executing the `before-change-functions'
6055 don't call any before-change or after-change functions.
6056 That's because these variables are temporarily set to nil.
6057 As a result, a hook function cannot straightforwardly alter the value of
6058 these variables. See the Emacs Lisp manual for a way of
6059 accomplishing an equivalent result by using other variables.
6061 If an unhandled error happens in running these functions,
6062 the variable's value remains nil. That prevents the error
6063 from happening repeatedly and making Emacs nonfunctional. */);
6064 Vbefore_change_functions = Qnil;
6066 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6067 doc: /* List of functions to call after each text change.
6068 Three arguments are passed to each function: the positions of
6069 the beginning and end of the range of changed text,
6070 and the length in bytes of the pre-change text replaced by that range.
6071 \(For an insertion, the pre-change length is zero;
6072 for a deletion, that length is the number of bytes deleted,
6073 and the post-change beginning and end are at the same place.)
6075 Buffer changes made while executing the `after-change-functions'
6076 don't call any before-change or after-change functions.
6077 That's because these variables are temporarily set to nil.
6078 As a result, a hook function cannot straightforwardly alter the value of
6079 these variables. See the Emacs Lisp manual for a way of
6080 accomplishing an equivalent result by using other variables.
6082 If an unhandled error happens in running these functions,
6083 the variable's value remains nil. That prevents the error
6084 from happening repeatedly and making Emacs nonfunctional. */);
6085 Vafter_change_functions = Qnil;
6087 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6088 doc: /* A list of functions to call before changing a buffer which is unmodified.
6089 The functions are run using the `run-hooks' function. */);
6090 Vfirst_change_hook = Qnil;
6092 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6093 doc: /* List of undo entries in current buffer.
6094 Recent changes come first; older changes follow newer.
6096 An entry (BEG . END) represents an insertion which begins at
6097 position BEG and ends at position END.
6099 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6100 from (abs POSITION). If POSITION is positive, point was at the front
6101 of the text being deleted; if negative, point was at the end.
6103 An entry (t HIGH . LOW) indicates that the buffer previously had
6104 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6105 of the visited file's modification time, as of that time. If the
6106 modification time of the most recent save is different, this entry is
6107 obsolete.
6109 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6110 was modified between BEG and END. PROPERTY is the property name,
6111 and VALUE is the old value.
6113 An entry (apply FUN-NAME . ARGS) means undo the change with
6114 \(apply FUN-NAME ARGS).
6116 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6117 in the active region. BEG and END is the range affected by this entry
6118 and DELTA is the number of bytes added or deleted in that range by
6119 this change.
6121 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6122 was adjusted in position by the offset DISTANCE (an integer).
6124 An entry of the form POSITION indicates that point was at the buffer
6125 location given by the integer. Undoing an entry of this form places
6126 point at POSITION.
6128 Entries with value `nil' mark undo boundaries. The undo command treats
6129 the changes between two undo boundaries as a single step to be undone.
6131 If the value of the variable is t, undo information is not recorded. */);
6133 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6134 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6136 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6137 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6139 Normally, the line-motion functions work by scanning the buffer for
6140 newlines. Columnar operations (like `move-to-column' and
6141 `compute-motion') also work by scanning the buffer, summing character
6142 widths as they go. This works well for ordinary text, but if the
6143 buffer's lines are very long (say, more than 500 characters), these
6144 motion functions will take longer to execute. Emacs may also take
6145 longer to update the display.
6147 If `cache-long-line-scans' is non-nil, these motion functions cache the
6148 results of their scans, and consult the cache to avoid rescanning
6149 regions of the buffer until the text is modified. The caches are most
6150 beneficial when they prevent the most searching---that is, when the
6151 buffer contains long lines and large regions of characters with the
6152 same, fixed screen width.
6154 When `cache-long-line-scans' is non-nil, processing short lines will
6155 become slightly slower (because of the overhead of consulting the
6156 cache), and the caches will use memory roughly proportional to the
6157 number of newlines and characters whose screen width varies.
6159 The caches require no explicit maintenance; their accuracy is
6160 maintained internally by the Emacs primitives. Enabling or disabling
6161 the cache should not affect the behavior of any of the motion
6162 functions; it should only affect their performance. */);
6164 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6165 doc: /* Value of point before the last series of scroll operations, or nil. */);
6167 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6168 doc: /* List of formats to use when saving this buffer.
6169 Formats are defined by `format-alist'. This variable is
6170 set when a file is visited. */);
6172 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6173 &current_buffer->auto_save_file_format, Qnil,
6174 doc: /* *Format in which to write auto-save files.
6175 Should be a list of symbols naming formats that are defined in `format-alist'.
6176 If it is t, which is the default, auto-save files are written in the
6177 same format as a regular save would use. */);
6179 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6180 &current_buffer->invisibility_spec, Qnil,
6181 doc: /* Invisibility spec of this buffer.
6182 The default is t, which means that text is invisible
6183 if it has a non-nil `invisible' property.
6184 If the value is a list, a text character is invisible if its `invisible'
6185 property is an element in that list (or is a list with members in common).
6186 If an element is a cons cell of the form (PROP . ELLIPSIS),
6187 then characters with property value PROP are invisible,
6188 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6190 DEFVAR_PER_BUFFER ("buffer-display-count",
6191 &current_buffer->display_count, Qnil,
6192 doc: /* A number incremented each time this buffer is displayed in a window.
6193 The function `set-window-buffer' increments it. */);
6195 DEFVAR_PER_BUFFER ("buffer-display-time",
6196 &current_buffer->display_time, Qnil,
6197 doc: /* Time stamp updated each time this buffer is displayed in a window.
6198 The function `set-window-buffer' updates this variable
6199 to the value obtained by calling `current-time'.
6200 If the buffer has never been shown in a window, the value is nil. */);
6202 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6203 doc: /* */);
6204 Vtransient_mark_mode = Qnil;
6205 /* The docstring is in simple.el. If we put it here, it would be
6206 overwritten when transient-mark-mode is defined using
6207 define-minor-mode. */
6209 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6210 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6211 If the value is t, disregard `buffer-read-only' and all `read-only'
6212 text properties. If the value is a list, disregard `buffer-read-only'
6213 and disregard a `read-only' text property if the property value
6214 is a member of the list. */);
6215 Vinhibit_read_only = Qnil;
6217 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6218 doc: /* Cursor to use when this buffer is in the selected window.
6219 Values are interpreted as follows:
6221 t use the cursor specified for the frame
6222 nil don't display a cursor
6223 box display a filled box cursor
6224 hollow display a hollow box cursor
6225 bar display a vertical bar cursor with default width
6226 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6227 hbar display a horizontal bar cursor with default height
6228 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6229 ANYTHING ELSE display a hollow box cursor
6231 When the buffer is displayed in a nonselected window,
6232 this variable has no effect; the cursor appears as a hollow box. */);
6234 DEFVAR_PER_BUFFER ("line-spacing",
6235 &current_buffer->extra_line_spacing, Qnil,
6236 doc: /* Additional space to put between lines when displaying a buffer.
6237 The space is measured in pixels, and put below lines on window systems.
6238 If value is a floating point number, it specifies the spacing relative
6239 to the default frame line height. A value of nil means add no extra space. */);
6241 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6242 &current_buffer->cursor_in_non_selected_windows, Qnil,
6243 doc: /* *Cursor type to display in non-selected windows.
6244 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6246 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6247 doc: /* List of functions called with no args to query before killing a buffer.
6248 The buffer being killed will be current while the functions are running.
6249 If any of them returns nil, the buffer is not killed. */);
6250 Vkill_buffer_query_functions = Qnil;
6252 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6253 doc: /* Normal hook run before changing the major mode of a buffer.
6254 The function `kill-all-local-variables' runs this before doing anything else. */);
6255 Vchange_major_mode_hook = Qnil;
6256 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6257 staticpro (&Qchange_major_mode_hook);
6259 defsubr (&Sbuffer_live_p);
6260 defsubr (&Sbuffer_list);
6261 defsubr (&Sget_buffer);
6262 defsubr (&Sget_file_buffer);
6263 defsubr (&Sget_buffer_create);
6264 defsubr (&Smake_indirect_buffer);
6265 defsubr (&Sgenerate_new_buffer_name);
6266 defsubr (&Sbuffer_name);
6267 /*defsubr (&Sbuffer_number);*/
6268 defsubr (&Sbuffer_file_name);
6269 defsubr (&Sbuffer_base_buffer);
6270 defsubr (&Sbuffer_local_value);
6271 defsubr (&Sbuffer_local_variables);
6272 defsubr (&Sbuffer_modified_p);
6273 defsubr (&Sset_buffer_modified_p);
6274 defsubr (&Sbuffer_modified_tick);
6275 defsubr (&Sbuffer_chars_modified_tick);
6276 defsubr (&Srename_buffer);
6277 defsubr (&Sother_buffer);
6278 defsubr (&Sbuffer_enable_undo);
6279 defsubr (&Skill_buffer);
6280 defsubr (&Sset_buffer_major_mode);
6281 defsubr (&Sswitch_to_buffer);
6282 defsubr (&Spop_to_buffer);
6283 defsubr (&Scurrent_buffer);
6284 defsubr (&Sset_buffer);
6285 defsubr (&Sbarf_if_buffer_read_only);
6286 defsubr (&Sbury_buffer);
6287 defsubr (&Serase_buffer);
6288 defsubr (&Sbuffer_swap_text);
6289 defsubr (&Sset_buffer_multibyte);
6290 defsubr (&Skill_all_local_variables);
6292 defsubr (&Soverlayp);
6293 defsubr (&Smake_overlay);
6294 defsubr (&Sdelete_overlay);
6295 defsubr (&Smove_overlay);
6296 defsubr (&Soverlay_start);
6297 defsubr (&Soverlay_end);
6298 defsubr (&Soverlay_buffer);
6299 defsubr (&Soverlay_properties);
6300 defsubr (&Soverlays_at);
6301 defsubr (&Soverlays_in);
6302 defsubr (&Snext_overlay_change);
6303 defsubr (&Sprevious_overlay_change);
6304 defsubr (&Soverlay_recenter);
6305 defsubr (&Soverlay_lists);
6306 defsubr (&Soverlay_get);
6307 defsubr (&Soverlay_put);
6308 defsubr (&Srestore_buffer_modified_p);
6311 void
6312 keys_of_buffer ()
6314 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6315 initial_define_key (control_x_map, 'k', "kill-buffer");
6317 /* This must not be in syms_of_buffer, because Qdisabled is not
6318 initialized when that function gets called. */
6319 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6322 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6323 (do not change this comment) */