(browse-url-text-xterm): Unquote browse-url-text-browser.
[emacs.git] / src / buffer.c
blob1ccb2f70e7d168e1f911762bb6c88a759fc3dac7
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 BEG-END
2817 or are empty at BEG.
2819 Return the number found, and store them in a vector in *VEC_PTR.
2820 Store in *LEN_PTR the size allocated for the vector.
2821 Store in *NEXT_PTR the next position after POS where an overlay starts,
2822 or ZV if there are no more overlays.
2823 Store in *PREV_PTR the previous position before POS where an overlay ends,
2824 or BEGV if there are no previous overlays.
2825 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2827 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2828 when this function is called.
2830 If EXTEND is non-zero, we make the vector bigger if necessary.
2831 If EXTEND is zero, we never extend the vector,
2832 and we store only as many overlays as will fit.
2833 But we still return the total number of overlays. */
2835 static int
2836 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2837 int beg, end;
2838 int extend;
2839 Lisp_Object **vec_ptr;
2840 int *len_ptr;
2841 int *next_ptr;
2842 int *prev_ptr;
2844 Lisp_Object overlay, ostart, oend;
2845 struct Lisp_Overlay *tail;
2846 int idx = 0;
2847 int len = *len_ptr;
2848 Lisp_Object *vec = *vec_ptr;
2849 int next = ZV;
2850 int prev = BEGV;
2851 int inhibit_storing = 0;
2853 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2855 int startpos, endpos;
2857 XSETMISC (overlay, tail);
2859 ostart = OVERLAY_START (overlay);
2860 oend = OVERLAY_END (overlay);
2861 endpos = OVERLAY_POSITION (oend);
2862 if (endpos < beg)
2864 if (prev < endpos)
2865 prev = endpos;
2866 break;
2868 startpos = OVERLAY_POSITION (ostart);
2869 /* Count an interval if it either overlaps the range
2870 or is empty at the start of the range. */
2871 if ((beg < endpos && startpos < end)
2872 || (startpos == endpos && beg == endpos))
2874 if (idx == len)
2876 /* The supplied vector is full.
2877 Either make it bigger, or don't store any more in it. */
2878 if (extend)
2880 /* Make it work with an initial len == 0. */
2881 len *= 2;
2882 if (len == 0)
2883 len = 4;
2884 *len_ptr = len;
2885 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2886 *vec_ptr = vec;
2888 else
2889 inhibit_storing = 1;
2892 if (!inhibit_storing)
2893 vec[idx] = overlay;
2894 /* Keep counting overlays even if we can't return them all. */
2895 idx++;
2897 else if (startpos < next)
2898 next = startpos;
2901 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2903 int startpos, endpos;
2905 XSETMISC (overlay, tail);
2907 ostart = OVERLAY_START (overlay);
2908 oend = OVERLAY_END (overlay);
2909 startpos = OVERLAY_POSITION (ostart);
2910 if (end < startpos)
2912 if (startpos < next)
2913 next = startpos;
2914 break;
2916 endpos = OVERLAY_POSITION (oend);
2917 /* Count an interval if it either overlaps the range
2918 or is empty at the start of the range. */
2919 if ((beg < endpos && startpos < end)
2920 || (startpos == endpos && beg == endpos))
2922 if (idx == len)
2924 if (extend)
2926 /* Make it work with an initial len == 0. */
2927 len *= 2;
2928 if (len == 0)
2929 len = 4;
2930 *len_ptr = len;
2931 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2932 *vec_ptr = vec;
2934 else
2935 inhibit_storing = 1;
2938 if (!inhibit_storing)
2939 vec[idx] = overlay;
2940 idx++;
2942 else if (endpos < beg && endpos > prev)
2943 prev = endpos;
2946 if (next_ptr)
2947 *next_ptr = next;
2948 if (prev_ptr)
2949 *prev_ptr = prev;
2950 return idx;
2954 /* Return non-zero if there exists an overlay with a non-nil
2955 `mouse-face' property overlapping OVERLAY. */
2958 mouse_face_overlay_overlaps (overlay)
2959 Lisp_Object overlay;
2961 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2962 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2963 int n, i, size;
2964 Lisp_Object *v, tem;
2966 size = 10;
2967 v = (Lisp_Object *) alloca (size * sizeof *v);
2968 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2969 if (n > size)
2971 v = (Lisp_Object *) alloca (n * sizeof *v);
2972 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2975 for (i = 0; i < n; ++i)
2976 if (!EQ (v[i], overlay)
2977 && (tem = Foverlay_get (overlay, Qmouse_face),
2978 !NILP (tem)))
2979 break;
2981 return i < n;
2986 /* Fast function to just test if we're at an overlay boundary. */
2988 overlay_touches_p (pos)
2989 int pos;
2991 Lisp_Object overlay;
2992 struct Lisp_Overlay *tail;
2994 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2996 int endpos;
2998 XSETMISC (overlay ,tail);
2999 if (!OVERLAYP (overlay))
3000 abort ();
3002 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3003 if (endpos < pos)
3004 break;
3005 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3006 return 1;
3009 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
3011 int startpos;
3013 XSETMISC (overlay, tail);
3014 if (!OVERLAYP (overlay))
3015 abort ();
3017 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3018 if (pos < startpos)
3019 break;
3020 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3021 return 1;
3023 return 0;
3026 struct sortvec
3028 Lisp_Object overlay;
3029 int beg, end;
3030 int priority;
3033 static int
3034 compare_overlays (v1, v2)
3035 const void *v1, *v2;
3037 const struct sortvec *s1 = (const struct sortvec *) v1;
3038 const struct sortvec *s2 = (const struct sortvec *) v2;
3039 if (s1->priority != s2->priority)
3040 return s1->priority - s2->priority;
3041 if (s1->beg != s2->beg)
3042 return s1->beg - s2->beg;
3043 if (s1->end != s2->end)
3044 return s2->end - s1->end;
3045 return 0;
3048 /* Sort an array of overlays by priority. The array is modified in place.
3049 The return value is the new size; this may be smaller than the original
3050 size if some of the overlays were invalid or were window-specific. */
3052 sort_overlays (overlay_vec, noverlays, w)
3053 Lisp_Object *overlay_vec;
3054 int noverlays;
3055 struct window *w;
3057 int i, j;
3058 struct sortvec *sortvec;
3059 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3061 /* Put the valid and relevant overlays into sortvec. */
3063 for (i = 0, j = 0; i < noverlays; i++)
3065 Lisp_Object tem;
3066 Lisp_Object overlay;
3068 overlay = overlay_vec[i];
3069 if (OVERLAY_VALID (overlay)
3070 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3071 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3073 /* If we're interested in a specific window, then ignore
3074 overlays that are limited to some other window. */
3075 if (w)
3077 Lisp_Object window;
3079 window = Foverlay_get (overlay, Qwindow);
3080 if (WINDOWP (window) && XWINDOW (window) != w)
3081 continue;
3084 /* This overlay is good and counts: put it into sortvec. */
3085 sortvec[j].overlay = overlay;
3086 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3087 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3088 tem = Foverlay_get (overlay, Qpriority);
3089 if (INTEGERP (tem))
3090 sortvec[j].priority = XINT (tem);
3091 else
3092 sortvec[j].priority = 0;
3093 j++;
3096 noverlays = j;
3098 /* Sort the overlays into the proper order: increasing priority. */
3100 if (noverlays > 1)
3101 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3103 for (i = 0; i < noverlays; i++)
3104 overlay_vec[i] = sortvec[i].overlay;
3105 return (noverlays);
3108 struct sortstr
3110 Lisp_Object string, string2;
3111 int size;
3112 int priority;
3115 struct sortstrlist
3117 struct sortstr *buf; /* An array that expands as needed; never freed. */
3118 int size; /* Allocated length of that array. */
3119 int used; /* How much of the array is currently in use. */
3120 int bytes; /* Total length of the strings in buf. */
3123 /* Buffers for storing information about the overlays touching a given
3124 position. These could be automatic variables in overlay_strings, but
3125 it's more efficient to hold onto the memory instead of repeatedly
3126 allocating and freeing it. */
3127 static struct sortstrlist overlay_heads, overlay_tails;
3128 static unsigned char *overlay_str_buf;
3130 /* Allocated length of overlay_str_buf. */
3131 static int overlay_str_len;
3133 /* A comparison function suitable for passing to qsort. */
3134 static int
3135 cmp_for_strings (as1, as2)
3136 char *as1, *as2;
3138 struct sortstr *s1 = (struct sortstr *)as1;
3139 struct sortstr *s2 = (struct sortstr *)as2;
3140 if (s1->size != s2->size)
3141 return s2->size - s1->size;
3142 if (s1->priority != s2->priority)
3143 return s1->priority - s2->priority;
3144 return 0;
3147 static void
3148 record_overlay_string (ssl, str, str2, pri, size)
3149 struct sortstrlist *ssl;
3150 Lisp_Object str, str2, pri;
3151 int size;
3153 int nbytes;
3155 if (ssl->used == ssl->size)
3157 if (ssl->buf)
3158 ssl->size *= 2;
3159 else
3160 ssl->size = 5;
3161 ssl->buf = ((struct sortstr *)
3162 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3164 ssl->buf[ssl->used].string = str;
3165 ssl->buf[ssl->used].string2 = str2;
3166 ssl->buf[ssl->used].size = size;
3167 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3168 ssl->used++;
3170 if (NILP (current_buffer->enable_multibyte_characters))
3171 nbytes = SCHARS (str);
3172 else if (! STRING_MULTIBYTE (str))
3173 nbytes = count_size_as_multibyte (SDATA (str),
3174 SBYTES (str));
3175 else
3176 nbytes = SBYTES (str);
3178 ssl->bytes += nbytes;
3180 if (STRINGP (str2))
3182 if (NILP (current_buffer->enable_multibyte_characters))
3183 nbytes = SCHARS (str2);
3184 else if (! STRING_MULTIBYTE (str2))
3185 nbytes = count_size_as_multibyte (SDATA (str2),
3186 SBYTES (str2));
3187 else
3188 nbytes = SBYTES (str2);
3190 ssl->bytes += nbytes;
3194 /* Return the concatenation of the strings associated with overlays that
3195 begin or end at POS, ignoring overlays that are specific to a window
3196 other than W. The strings are concatenated in the appropriate order:
3197 shorter overlays nest inside longer ones, and higher priority inside
3198 lower. Normally all of the after-strings come first, but zero-sized
3199 overlays have their after-strings ride along with the before-strings
3200 because it would look strange to print them inside-out.
3202 Returns the string length, and stores the contents indirectly through
3203 PSTR, if that variable is non-null. The string may be overwritten by
3204 subsequent calls. */
3207 overlay_strings (pos, w, pstr)
3208 EMACS_INT pos;
3209 struct window *w;
3210 unsigned char **pstr;
3212 Lisp_Object overlay, window, str;
3213 struct Lisp_Overlay *ov;
3214 int startpos, endpos;
3215 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3217 overlay_heads.used = overlay_heads.bytes = 0;
3218 overlay_tails.used = overlay_tails.bytes = 0;
3219 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3221 XSETMISC (overlay, ov);
3222 eassert (OVERLAYP (overlay));
3224 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3225 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3226 if (endpos < pos)
3227 break;
3228 if (endpos != pos && startpos != pos)
3229 continue;
3230 window = Foverlay_get (overlay, Qwindow);
3231 if (WINDOWP (window) && XWINDOW (window) != w)
3232 continue;
3233 if (startpos == pos
3234 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3235 record_overlay_string (&overlay_heads, str,
3236 (startpos == endpos
3237 ? Foverlay_get (overlay, Qafter_string)
3238 : Qnil),
3239 Foverlay_get (overlay, Qpriority),
3240 endpos - startpos);
3241 else if (endpos == pos
3242 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3243 record_overlay_string (&overlay_tails, str, Qnil,
3244 Foverlay_get (overlay, Qpriority),
3245 endpos - startpos);
3247 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3249 XSETMISC (overlay, ov);
3250 eassert (OVERLAYP (overlay));
3252 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3253 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3254 if (startpos > pos)
3255 break;
3256 if (endpos != pos && startpos != pos)
3257 continue;
3258 window = Foverlay_get (overlay, Qwindow);
3259 if (WINDOWP (window) && XWINDOW (window) != w)
3260 continue;
3261 if (startpos == pos
3262 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3263 record_overlay_string (&overlay_heads, str,
3264 (startpos == endpos
3265 ? Foverlay_get (overlay, Qafter_string)
3266 : Qnil),
3267 Foverlay_get (overlay, Qpriority),
3268 endpos - startpos);
3269 else if (endpos == pos
3270 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3271 record_overlay_string (&overlay_tails, str, Qnil,
3272 Foverlay_get (overlay, Qpriority),
3273 endpos - startpos);
3275 if (overlay_tails.used > 1)
3276 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3277 cmp_for_strings);
3278 if (overlay_heads.used > 1)
3279 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3280 cmp_for_strings);
3281 if (overlay_heads.bytes || overlay_tails.bytes)
3283 Lisp_Object tem;
3284 int i;
3285 unsigned char *p;
3286 int total = overlay_heads.bytes + overlay_tails.bytes;
3288 if (total > overlay_str_len)
3290 overlay_str_len = total;
3291 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3292 total);
3294 p = overlay_str_buf;
3295 for (i = overlay_tails.used; --i >= 0;)
3297 int nbytes;
3298 tem = overlay_tails.buf[i].string;
3299 nbytes = copy_text (SDATA (tem), p,
3300 SBYTES (tem),
3301 STRING_MULTIBYTE (tem), multibyte);
3302 p += nbytes;
3304 for (i = 0; i < overlay_heads.used; ++i)
3306 int nbytes;
3307 tem = overlay_heads.buf[i].string;
3308 nbytes = copy_text (SDATA (tem), p,
3309 SBYTES (tem),
3310 STRING_MULTIBYTE (tem), multibyte);
3311 p += nbytes;
3312 tem = overlay_heads.buf[i].string2;
3313 if (STRINGP (tem))
3315 nbytes = copy_text (SDATA (tem), p,
3316 SBYTES (tem),
3317 STRING_MULTIBYTE (tem), multibyte);
3318 p += nbytes;
3321 if (p != overlay_str_buf + total)
3322 abort ();
3323 if (pstr)
3324 *pstr = overlay_str_buf;
3325 return total;
3327 return 0;
3330 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3332 void
3333 recenter_overlay_lists (buf, pos)
3334 struct buffer *buf;
3335 EMACS_INT pos;
3337 Lisp_Object overlay, beg, end;
3338 struct Lisp_Overlay *prev, *tail, *next;
3340 /* See if anything in overlays_before should move to overlays_after. */
3342 /* We don't strictly need prev in this loop; it should always be nil.
3343 But we use it for symmetry and in case that should cease to be true
3344 with some future change. */
3345 prev = NULL;
3346 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3348 next = tail->next;
3349 XSETMISC (overlay, tail);
3351 /* If the overlay is not valid, get rid of it. */
3352 if (!OVERLAY_VALID (overlay))
3353 #if 1
3354 abort ();
3355 #else
3357 /* Splice the cons cell TAIL out of overlays_before. */
3358 if (!NILP (prev))
3359 XCDR (prev) = next;
3360 else
3361 buf->overlays_before = next;
3362 tail = prev;
3363 continue;
3365 #endif
3367 beg = OVERLAY_START (overlay);
3368 end = OVERLAY_END (overlay);
3370 if (OVERLAY_POSITION (end) > pos)
3372 /* OVERLAY needs to be moved. */
3373 int where = OVERLAY_POSITION (beg);
3374 struct Lisp_Overlay *other, *other_prev;
3376 /* Splice the cons cell TAIL out of overlays_before. */
3377 if (prev)
3378 prev->next = next;
3379 else
3380 buf->overlays_before = next;
3382 /* Search thru overlays_after for where to put it. */
3383 other_prev = NULL;
3384 for (other = buf->overlays_after; other;
3385 other_prev = other, other = other->next)
3387 Lisp_Object otherbeg, otheroverlay;
3389 XSETMISC (otheroverlay, other);
3390 eassert (OVERLAY_VALID (otheroverlay));
3392 otherbeg = OVERLAY_START (otheroverlay);
3393 if (OVERLAY_POSITION (otherbeg) >= where)
3394 break;
3397 /* Add TAIL to overlays_after before OTHER. */
3398 tail->next = other;
3399 if (other_prev)
3400 other_prev->next = tail;
3401 else
3402 buf->overlays_after = tail;
3403 tail = prev;
3405 else
3406 /* We've reached the things that should stay in overlays_before.
3407 All the rest of overlays_before must end even earlier,
3408 so stop now. */
3409 break;
3412 /* See if anything in overlays_after should be in overlays_before. */
3413 prev = NULL;
3414 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3416 next = tail->next;
3417 XSETMISC (overlay, tail);
3419 /* If the overlay is not valid, get rid of it. */
3420 if (!OVERLAY_VALID (overlay))
3421 #if 1
3422 abort ();
3423 #else
3425 /* Splice the cons cell TAIL out of overlays_after. */
3426 if (!NILP (prev))
3427 XCDR (prev) = next;
3428 else
3429 buf->overlays_after = next;
3430 tail = prev;
3431 continue;
3433 #endif
3435 beg = OVERLAY_START (overlay);
3436 end = OVERLAY_END (overlay);
3438 /* Stop looking, when we know that nothing further
3439 can possibly end before POS. */
3440 if (OVERLAY_POSITION (beg) > pos)
3441 break;
3443 if (OVERLAY_POSITION (end) <= pos)
3445 /* OVERLAY needs to be moved. */
3446 int where = OVERLAY_POSITION (end);
3447 struct Lisp_Overlay *other, *other_prev;
3449 /* Splice the cons cell TAIL out of overlays_after. */
3450 if (prev)
3451 prev->next = next;
3452 else
3453 buf->overlays_after = next;
3455 /* Search thru overlays_before for where to put it. */
3456 other_prev = NULL;
3457 for (other = buf->overlays_before; other;
3458 other_prev = other, other = other->next)
3460 Lisp_Object otherend, otheroverlay;
3462 XSETMISC (otheroverlay, other);
3463 eassert (OVERLAY_VALID (otheroverlay));
3465 otherend = OVERLAY_END (otheroverlay);
3466 if (OVERLAY_POSITION (otherend) <= where)
3467 break;
3470 /* Add TAIL to overlays_before before OTHER. */
3471 tail->next = other;
3472 if (other_prev)
3473 other_prev->next = tail;
3474 else
3475 buf->overlays_before = tail;
3476 tail = prev;
3480 buf->overlay_center = pos;
3483 void
3484 adjust_overlays_for_insert (pos, length)
3485 EMACS_INT pos;
3486 EMACS_INT length;
3488 /* After an insertion, the lists are still sorted properly,
3489 but we may need to update the value of the overlay center. */
3490 if (current_buffer->overlay_center >= pos)
3491 current_buffer->overlay_center += length;
3494 void
3495 adjust_overlays_for_delete (pos, length)
3496 EMACS_INT pos;
3497 EMACS_INT length;
3499 if (current_buffer->overlay_center < pos)
3500 /* The deletion was to our right. No change needed; the before- and
3501 after-lists are still consistent. */
3503 else if (current_buffer->overlay_center > pos + length)
3504 /* The deletion was to our left. We need to adjust the center value
3505 to account for the change in position, but the lists are consistent
3506 given the new value. */
3507 current_buffer->overlay_center -= length;
3508 else
3509 /* We're right in the middle. There might be things on the after-list
3510 that now belong on the before-list. Recentering will move them,
3511 and also update the center point. */
3512 recenter_overlay_lists (current_buffer, pos);
3515 /* Fix up overlays that were garbled as a result of permuting markers
3516 in the range START through END. Any overlay with at least one
3517 endpoint in this range will need to be unlinked from the overlay
3518 list and reinserted in its proper place.
3519 Such an overlay might even have negative size at this point.
3520 If so, we'll make the overlay empty. */
3521 void
3522 fix_start_end_in_overlays (start, end)
3523 register int start, end;
3525 Lisp_Object overlay;
3526 struct Lisp_Overlay *before_list, *after_list;
3527 /* These are either nil, indicating that before_list or after_list
3528 should be assigned, or the cons cell the cdr of which should be
3529 assigned. */
3530 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3531 /* 'Parent', likewise, indicates a cons cell or
3532 current_buffer->overlays_before or overlays_after, depending
3533 which loop we're in. */
3534 struct Lisp_Overlay *tail, *parent;
3535 int startpos, endpos;
3537 /* This algorithm shifts links around instead of consing and GCing.
3538 The loop invariant is that before_list (resp. after_list) is a
3539 well-formed list except that its last element, the CDR of beforep
3540 (resp. afterp) if beforep (afterp) isn't nil or before_list
3541 (after_list) if it is, is still uninitialized. So it's not a bug
3542 that before_list isn't initialized, although it may look
3543 strange. */
3544 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3546 XSETMISC (overlay, tail);
3548 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3549 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3551 /* If the overlay is backwards, make it empty. */
3552 if (endpos < startpos)
3554 startpos = endpos;
3555 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3556 Qnil);
3559 if (endpos < start)
3560 break;
3562 if (endpos < end
3563 || (startpos >= start && startpos < end))
3565 /* Add it to the end of the wrong list. Later on,
3566 recenter_overlay_lists will move it to the right place. */
3567 if (endpos < current_buffer->overlay_center)
3569 if (!afterp)
3570 after_list = tail;
3571 else
3572 afterp->next = tail;
3573 afterp = tail;
3575 else
3577 if (!beforep)
3578 before_list = tail;
3579 else
3580 beforep->next = tail;
3581 beforep = tail;
3583 if (!parent)
3584 current_buffer->overlays_before = tail->next;
3585 else
3586 parent->next = tail->next;
3587 tail = tail->next;
3589 else
3590 parent = tail, tail = parent->next;
3592 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3594 XSETMISC (overlay, tail);
3596 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3597 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3599 /* If the overlay is backwards, make it empty. */
3600 if (endpos < startpos)
3602 startpos = endpos;
3603 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3604 Qnil);
3607 if (startpos >= end)
3608 break;
3610 if (startpos >= start
3611 || (endpos >= start && endpos < end))
3613 if (endpos < current_buffer->overlay_center)
3615 if (!afterp)
3616 after_list = tail;
3617 else
3618 afterp->next = tail;
3619 afterp = tail;
3621 else
3623 if (!beforep)
3624 before_list = tail;
3625 else
3626 beforep->next = tail;
3627 beforep = tail;
3629 if (!parent)
3630 current_buffer->overlays_after = tail->next;
3631 else
3632 parent->next = tail->next;
3633 tail = tail->next;
3635 else
3636 parent = tail, tail = parent->next;
3639 /* Splice the constructed (wrong) lists into the buffer's lists,
3640 and let the recenter function make it sane again. */
3641 if (beforep)
3643 beforep->next = current_buffer->overlays_before;
3644 current_buffer->overlays_before = before_list;
3646 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3648 if (afterp)
3650 afterp->next = current_buffer->overlays_after;
3651 current_buffer->overlays_after = after_list;
3653 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3656 /* We have two types of overlay: the one whose ending marker is
3657 after-insertion-marker (this is the usual case) and the one whose
3658 ending marker is before-insertion-marker. When `overlays_before'
3659 contains overlays of the latter type and the former type in this
3660 order and both overlays end at inserting position, inserting a text
3661 increases only the ending marker of the latter type, which results
3662 in incorrect ordering of `overlays_before'.
3664 This function fixes ordering of overlays in the slot
3665 `overlays_before' of the buffer *BP. Before the insertion, `point'
3666 was at PREV, and now is at POS. */
3668 void
3669 fix_overlays_before (bp, prev, pos)
3670 struct buffer *bp;
3671 EMACS_INT prev, pos;
3673 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3674 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3675 Lisp_Object tem;
3676 EMACS_INT end;
3678 /* After the insertion, the several overlays may be in incorrect
3679 order. The possibility is that, in the list `overlays_before',
3680 an overlay which ends at POS appears after an overlay which ends
3681 at PREV. Since POS is greater than PREV, we must fix the
3682 ordering of these overlays, by moving overlays ends at POS before
3683 the overlays ends at PREV. */
3685 /* At first, find a place where disordered overlays should be linked
3686 in. It is where an overlay which end before POS exists. (i.e. an
3687 overlay whose ending marker is after-insertion-marker if disorder
3688 exists). */
3689 while (tail
3690 && (XSETMISC (tem, tail),
3691 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3693 parent = tail;
3694 tail = tail->next;
3697 /* If we don't find such an overlay,
3698 or the found one ends before PREV,
3699 or the found one is the last one in the list,
3700 we don't have to fix anything. */
3701 if (!tail || end < prev || !tail->next)
3702 return;
3704 right_pair = parent;
3705 parent = tail;
3706 tail = tail->next;
3708 /* Now, end position of overlays in the list TAIL should be before
3709 or equal to PREV. In the loop, an overlay which ends at POS is
3710 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3711 we found an overlay which ends before PREV, the remaining
3712 overlays are in correct order. */
3713 while (tail)
3715 XSETMISC (tem, tail);
3716 end = OVERLAY_POSITION (OVERLAY_END (tem));
3718 if (end == pos)
3719 { /* This overlay is disordered. */
3720 struct Lisp_Overlay *found = tail;
3722 /* Unlink the found overlay. */
3723 tail = found->next;
3724 parent->next = tail;
3725 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3726 and link it into the right place. */
3727 if (!right_pair)
3729 found->next = bp->overlays_before;
3730 bp->overlays_before = found;
3732 else
3734 found->next = right_pair->next;
3735 right_pair->next = found;
3738 else if (end == prev)
3740 parent = tail;
3741 tail = tail->next;
3743 else /* No more disordered overlay. */
3744 break;
3748 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3749 doc: /* Return t if OBJECT is an overlay. */)
3750 (object)
3751 Lisp_Object object;
3753 return (OVERLAYP (object) ? Qt : Qnil);
3756 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3757 doc: /* Create a new overlay with range BEG to END in BUFFER.
3758 If omitted, BUFFER defaults to the current buffer.
3759 BEG and END may be integers or markers.
3760 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3761 for the front of the overlay advance when text is inserted there
3762 \(which means the text *is not* included in the overlay).
3763 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3764 for the rear of the overlay advance when text is inserted there
3765 \(which means the text *is* included in the overlay). */)
3766 (beg, end, buffer, front_advance, rear_advance)
3767 Lisp_Object beg, end, buffer;
3768 Lisp_Object front_advance, rear_advance;
3770 Lisp_Object overlay;
3771 struct buffer *b;
3773 if (NILP (buffer))
3774 XSETBUFFER (buffer, current_buffer);
3775 else
3776 CHECK_BUFFER (buffer);
3777 if (MARKERP (beg)
3778 && ! EQ (Fmarker_buffer (beg), buffer))
3779 error ("Marker points into wrong buffer");
3780 if (MARKERP (end)
3781 && ! EQ (Fmarker_buffer (end), buffer))
3782 error ("Marker points into wrong buffer");
3784 CHECK_NUMBER_COERCE_MARKER (beg);
3785 CHECK_NUMBER_COERCE_MARKER (end);
3787 if (XINT (beg) > XINT (end))
3789 Lisp_Object temp;
3790 temp = beg; beg = end; end = temp;
3793 b = XBUFFER (buffer);
3795 beg = Fset_marker (Fmake_marker (), beg, buffer);
3796 end = Fset_marker (Fmake_marker (), end, buffer);
3798 if (!NILP (front_advance))
3799 XMARKER (beg)->insertion_type = 1;
3800 if (!NILP (rear_advance))
3801 XMARKER (end)->insertion_type = 1;
3803 overlay = allocate_misc ();
3804 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3805 XOVERLAY (overlay)->start = beg;
3806 XOVERLAY (overlay)->end = end;
3807 XOVERLAY (overlay)->plist = Qnil;
3808 XOVERLAY (overlay)->next = NULL;
3810 /* Put the new overlay on the wrong list. */
3811 end = OVERLAY_END (overlay);
3812 if (OVERLAY_POSITION (end) < b->overlay_center)
3814 if (b->overlays_after)
3815 XOVERLAY (overlay)->next = b->overlays_after;
3816 b->overlays_after = XOVERLAY (overlay);
3818 else
3820 if (b->overlays_before)
3821 XOVERLAY (overlay)->next = b->overlays_before;
3822 b->overlays_before = XOVERLAY (overlay);
3825 /* This puts it in the right list, and in the right order. */
3826 recenter_overlay_lists (b, b->overlay_center);
3828 /* We don't need to redisplay the region covered by the overlay, because
3829 the overlay has no properties at the moment. */
3831 return overlay;
3834 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3836 static void
3837 modify_overlay (buf, start, end)
3838 struct buffer *buf;
3839 EMACS_INT start, end;
3841 if (start > end)
3843 int temp = start;
3844 start = end;
3845 end = temp;
3848 BUF_COMPUTE_UNCHANGED (buf, start, end);
3850 /* If this is a buffer not in the selected window,
3851 we must do other windows. */
3852 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3853 windows_or_buffers_changed = 1;
3854 /* If multiple windows show this buffer, we must do other windows. */
3855 else if (buffer_shared > 1)
3856 windows_or_buffers_changed = 1;
3857 /* If we modify an overlay at the end of the buffer, we cannot
3858 be sure that window end is still valid. */
3859 else if (end >= ZV && start <= ZV)
3860 windows_or_buffers_changed = 1;
3862 ++BUF_OVERLAY_MODIFF (buf);
3866 Lisp_Object Fdelete_overlay ();
3868 static struct Lisp_Overlay *
3869 unchain_overlay (list, overlay)
3870 struct Lisp_Overlay *list, *overlay;
3872 struct Lisp_Overlay *tmp, *prev;
3873 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3874 if (tmp == overlay)
3876 if (prev)
3877 prev->next = tmp->next;
3878 else
3879 list = tmp->next;
3880 overlay->next = NULL;
3881 break;
3883 return list;
3886 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3887 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3888 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3889 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3890 buffer. */)
3891 (overlay, beg, end, buffer)
3892 Lisp_Object overlay, beg, end, buffer;
3894 struct buffer *b, *ob;
3895 Lisp_Object obuffer;
3896 int count = SPECPDL_INDEX ();
3898 CHECK_OVERLAY (overlay);
3899 if (NILP (buffer))
3900 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3901 if (NILP (buffer))
3902 XSETBUFFER (buffer, current_buffer);
3903 CHECK_BUFFER (buffer);
3905 if (MARKERP (beg)
3906 && ! EQ (Fmarker_buffer (beg), buffer))
3907 error ("Marker points into wrong buffer");
3908 if (MARKERP (end)
3909 && ! EQ (Fmarker_buffer (end), buffer))
3910 error ("Marker points into wrong buffer");
3912 CHECK_NUMBER_COERCE_MARKER (beg);
3913 CHECK_NUMBER_COERCE_MARKER (end);
3915 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3916 return Fdelete_overlay (overlay);
3918 if (XINT (beg) > XINT (end))
3920 Lisp_Object temp;
3921 temp = beg; beg = end; end = temp;
3924 specbind (Qinhibit_quit, Qt);
3926 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3927 b = XBUFFER (buffer);
3928 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3930 /* If the overlay has changed buffers, do a thorough redisplay. */
3931 if (!EQ (buffer, obuffer))
3933 /* Redisplay where the overlay was. */
3934 if (!NILP (obuffer))
3936 int o_beg;
3937 int o_end;
3939 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3940 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3942 modify_overlay (ob, o_beg, o_end);
3945 /* Redisplay where the overlay is going to be. */
3946 modify_overlay (b, XINT (beg), XINT (end));
3948 else
3949 /* Redisplay the area the overlay has just left, or just enclosed. */
3951 int o_beg, o_end;
3953 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3954 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3956 if (o_beg == XINT (beg))
3957 modify_overlay (b, o_end, XINT (end));
3958 else if (o_end == XINT (end))
3959 modify_overlay (b, o_beg, XINT (beg));
3960 else
3962 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3963 if (XINT (end) > o_end) o_end = XINT (end);
3964 modify_overlay (b, o_beg, o_end);
3968 if (!NILP (obuffer))
3970 ob->overlays_before
3971 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3972 ob->overlays_after
3973 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3974 eassert (XOVERLAY (overlay)->next == NULL);
3977 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3978 Fset_marker (OVERLAY_END (overlay), end, buffer);
3980 /* Put the overlay on the wrong list. */
3981 end = OVERLAY_END (overlay);
3982 if (OVERLAY_POSITION (end) < b->overlay_center)
3984 XOVERLAY (overlay)->next = b->overlays_after;
3985 b->overlays_after = XOVERLAY (overlay);
3987 else
3989 XOVERLAY (overlay)->next = b->overlays_before;
3990 b->overlays_before = XOVERLAY (overlay);
3993 /* This puts it in the right list, and in the right order. */
3994 recenter_overlay_lists (b, b->overlay_center);
3996 return unbind_to (count, overlay);
3999 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
4000 doc: /* Delete the overlay OVERLAY from its buffer. */)
4001 (overlay)
4002 Lisp_Object overlay;
4004 Lisp_Object buffer;
4005 struct buffer *b;
4006 int count = SPECPDL_INDEX ();
4008 CHECK_OVERLAY (overlay);
4010 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4011 if (NILP (buffer))
4012 return Qnil;
4014 b = XBUFFER (buffer);
4015 specbind (Qinhibit_quit, Qt);
4017 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4018 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4019 eassert (XOVERLAY (overlay)->next == NULL);
4020 modify_overlay (b,
4021 marker_position (OVERLAY_START (overlay)),
4022 marker_position (OVERLAY_END (overlay)));
4023 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4024 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4026 /* When deleting an overlay with before or after strings, turn off
4027 display optimizations for the affected buffer, on the basis that
4028 these strings may contain newlines. This is easier to do than to
4029 check for that situation during redisplay. */
4030 if (!windows_or_buffers_changed
4031 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4032 || !NILP (Foverlay_get (overlay, Qafter_string))))
4033 b->prevent_redisplay_optimizations_p = 1;
4035 return unbind_to (count, Qnil);
4038 /* Overlay dissection functions. */
4040 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4041 doc: /* Return the position at which OVERLAY starts. */)
4042 (overlay)
4043 Lisp_Object overlay;
4045 CHECK_OVERLAY (overlay);
4047 return (Fmarker_position (OVERLAY_START (overlay)));
4050 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4051 doc: /* Return the position at which OVERLAY ends. */)
4052 (overlay)
4053 Lisp_Object overlay;
4055 CHECK_OVERLAY (overlay);
4057 return (Fmarker_position (OVERLAY_END (overlay)));
4060 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4061 doc: /* Return the buffer OVERLAY belongs to.
4062 Return nil if OVERLAY has been deleted. */)
4063 (overlay)
4064 Lisp_Object overlay;
4066 CHECK_OVERLAY (overlay);
4068 return Fmarker_buffer (OVERLAY_START (overlay));
4071 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4072 doc: /* Return a list of the properties on OVERLAY.
4073 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4074 OVERLAY. */)
4075 (overlay)
4076 Lisp_Object overlay;
4078 CHECK_OVERLAY (overlay);
4080 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4084 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4085 doc: /* Return a list of the overlays that contain the character at POS. */)
4086 (pos)
4087 Lisp_Object pos;
4089 int noverlays;
4090 Lisp_Object *overlay_vec;
4091 int len;
4092 Lisp_Object result;
4094 CHECK_NUMBER_COERCE_MARKER (pos);
4096 len = 10;
4097 /* We can't use alloca here because overlays_at can call xrealloc. */
4098 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4100 /* Put all the overlays we want in a vector in overlay_vec.
4101 Store the length in len. */
4102 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4103 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4105 /* Make a list of them all. */
4106 result = Flist (noverlays, overlay_vec);
4108 xfree (overlay_vec);
4109 return result;
4112 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4113 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4114 Overlap means that at least one character is contained within the overlay
4115 and also contained within the specified region.
4116 Empty overlays are included in the result if they are located at BEG
4117 or between BEG and END. */)
4118 (beg, end)
4119 Lisp_Object beg, end;
4121 int noverlays;
4122 Lisp_Object *overlay_vec;
4123 int len;
4124 Lisp_Object result;
4126 CHECK_NUMBER_COERCE_MARKER (beg);
4127 CHECK_NUMBER_COERCE_MARKER (end);
4129 len = 10;
4130 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4132 /* Put all the overlays we want in a vector in overlay_vec.
4133 Store the length in len. */
4134 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4135 (int *) 0, (int *) 0);
4137 /* Make a list of them all. */
4138 result = Flist (noverlays, overlay_vec);
4140 xfree (overlay_vec);
4141 return result;
4144 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4145 1, 1, 0,
4146 doc: /* Return the next position after POS where an overlay starts or ends.
4147 If there are no overlay boundaries from POS to (point-max),
4148 the value is (point-max). */)
4149 (pos)
4150 Lisp_Object pos;
4152 int noverlays;
4153 EMACS_INT endpos;
4154 Lisp_Object *overlay_vec;
4155 int len;
4156 int i;
4158 CHECK_NUMBER_COERCE_MARKER (pos);
4160 len = 10;
4161 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4163 /* Put all the overlays we want in a vector in overlay_vec.
4164 Store the length in len.
4165 endpos gets the position where the next overlay starts. */
4166 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4167 &endpos, (EMACS_INT *) 0, 1);
4169 /* If any of these overlays ends before endpos,
4170 use its ending point instead. */
4171 for (i = 0; i < noverlays; i++)
4173 Lisp_Object oend;
4174 EMACS_INT oendpos;
4176 oend = OVERLAY_END (overlay_vec[i]);
4177 oendpos = OVERLAY_POSITION (oend);
4178 if (oendpos < endpos)
4179 endpos = oendpos;
4182 xfree (overlay_vec);
4183 return make_number (endpos);
4186 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4187 Sprevious_overlay_change, 1, 1, 0,
4188 doc: /* Return the previous position before POS where an overlay starts or ends.
4189 If there are no overlay boundaries from (point-min) to POS,
4190 the value is (point-min). */)
4191 (pos)
4192 Lisp_Object pos;
4194 int noverlays;
4195 EMACS_INT prevpos;
4196 Lisp_Object *overlay_vec;
4197 int len;
4199 CHECK_NUMBER_COERCE_MARKER (pos);
4201 /* At beginning of buffer, we know the answer;
4202 avoid bug subtracting 1 below. */
4203 if (XINT (pos) == BEGV)
4204 return pos;
4206 len = 10;
4207 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4209 /* Put all the overlays we want in a vector in overlay_vec.
4210 Store the length in len.
4211 prevpos gets the position of the previous change. */
4212 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4213 (EMACS_INT *) 0, &prevpos, 1);
4215 xfree (overlay_vec);
4216 return make_number (prevpos);
4219 /* These functions are for debugging overlays. */
4221 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4222 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4223 The car has all the overlays before the overlay center;
4224 the cdr has all the overlays after the overlay center.
4225 Recentering overlays moves overlays between these lists.
4226 The lists you get are copies, so that changing them has no effect.
4227 However, the overlays you get are the real objects that the buffer uses. */)
4230 struct Lisp_Overlay *ol;
4231 Lisp_Object before = Qnil, after = Qnil, tmp;
4232 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4234 XSETMISC (tmp, ol);
4235 before = Fcons (tmp, before);
4237 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4239 XSETMISC (tmp, ol);
4240 after = Fcons (tmp, after);
4242 return Fcons (Fnreverse (before), Fnreverse (after));
4245 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4246 doc: /* Recenter the overlays of the current buffer around position POS.
4247 That makes overlay lookup faster for positions near POS (but perhaps slower
4248 for positions far away from POS). */)
4249 (pos)
4250 Lisp_Object pos;
4252 CHECK_NUMBER_COERCE_MARKER (pos);
4254 recenter_overlay_lists (current_buffer, XINT (pos));
4255 return Qnil;
4258 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4259 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4260 (overlay, prop)
4261 Lisp_Object overlay, prop;
4263 CHECK_OVERLAY (overlay);
4264 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4267 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4268 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4269 (overlay, prop, value)
4270 Lisp_Object overlay, prop, value;
4272 Lisp_Object tail, buffer;
4273 int changed;
4275 CHECK_OVERLAY (overlay);
4277 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4279 for (tail = XOVERLAY (overlay)->plist;
4280 CONSP (tail) && CONSP (XCDR (tail));
4281 tail = XCDR (XCDR (tail)))
4282 if (EQ (XCAR (tail), prop))
4284 changed = !EQ (XCAR (XCDR (tail)), value);
4285 XSETCAR (XCDR (tail), value);
4286 goto found;
4288 /* It wasn't in the list, so add it to the front. */
4289 changed = !NILP (value);
4290 XOVERLAY (overlay)->plist
4291 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4292 found:
4293 if (! NILP (buffer))
4295 if (changed)
4296 modify_overlay (XBUFFER (buffer),
4297 marker_position (OVERLAY_START (overlay)),
4298 marker_position (OVERLAY_END (overlay)));
4299 if (EQ (prop, Qevaporate) && ! NILP (value)
4300 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4301 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4302 Fdelete_overlay (overlay);
4305 return value;
4308 /* Subroutine of report_overlay_modification. */
4310 /* Lisp vector holding overlay hook functions to call.
4311 Vector elements come in pairs.
4312 Each even-index element is a list of hook functions.
4313 The following odd-index element is the overlay they came from.
4315 Before the buffer change, we fill in this vector
4316 as we call overlay hook functions.
4317 After the buffer change, we get the functions to call from this vector.
4318 This way we always call the same functions before and after the change. */
4319 static Lisp_Object last_overlay_modification_hooks;
4321 /* Number of elements actually used in last_overlay_modification_hooks. */
4322 static int last_overlay_modification_hooks_used;
4324 /* Add one functionlist/overlay pair
4325 to the end of last_overlay_modification_hooks. */
4327 static void
4328 add_overlay_mod_hooklist (functionlist, overlay)
4329 Lisp_Object functionlist, overlay;
4331 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4333 if (last_overlay_modification_hooks_used == oldsize)
4334 last_overlay_modification_hooks = larger_vector
4335 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4336 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4337 functionlist); last_overlay_modification_hooks_used++;
4338 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4339 overlay); last_overlay_modification_hooks_used++;
4342 /* Run the modification-hooks of overlays that include
4343 any part of the text in START to END.
4344 If this change is an insertion, also
4345 run the insert-before-hooks of overlay starting at END,
4346 and the insert-after-hooks of overlay ending at START.
4348 This is called both before and after the modification.
4349 AFTER is nonzero when we call after the modification.
4351 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4352 When AFTER is nonzero, they are the start position,
4353 the position after the inserted new text,
4354 and the length of deleted or replaced old text. */
4356 void
4357 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4358 Lisp_Object start, end;
4359 int after;
4360 Lisp_Object arg1, arg2, arg3;
4362 Lisp_Object prop, overlay;
4363 struct Lisp_Overlay *tail;
4364 /* 1 if this change is an insertion. */
4365 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4366 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4368 overlay = Qnil;
4369 tail = NULL;
4371 /* We used to run the functions as soon as we found them and only register
4372 them in last_overlay_modification_hooks for the purpose of the `after'
4373 case. But running elisp code as we traverse the list of overlays is
4374 painful because the list can be modified by the elisp code so we had to
4375 copy at several places. We now simply do a read-only traversal that
4376 only collects the functions to run and we run them afterwards. It's
4377 simpler, especially since all the code was already there. -stef */
4379 if (!after)
4381 /* We are being called before a change.
4382 Scan the overlays to find the functions to call. */
4383 last_overlay_modification_hooks_used = 0;
4384 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4386 int startpos, endpos;
4387 Lisp_Object ostart, oend;
4389 XSETMISC (overlay, tail);
4391 ostart = OVERLAY_START (overlay);
4392 oend = OVERLAY_END (overlay);
4393 endpos = OVERLAY_POSITION (oend);
4394 if (XFASTINT (start) > endpos)
4395 break;
4396 startpos = OVERLAY_POSITION (ostart);
4397 if (insertion && (XFASTINT (start) == startpos
4398 || XFASTINT (end) == startpos))
4400 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4401 if (!NILP (prop))
4402 add_overlay_mod_hooklist (prop, overlay);
4404 if (insertion && (XFASTINT (start) == endpos
4405 || XFASTINT (end) == endpos))
4407 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4408 if (!NILP (prop))
4409 add_overlay_mod_hooklist (prop, overlay);
4411 /* Test for intersecting intervals. This does the right thing
4412 for both insertion and deletion. */
4413 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4415 prop = Foverlay_get (overlay, Qmodification_hooks);
4416 if (!NILP (prop))
4417 add_overlay_mod_hooklist (prop, overlay);
4421 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4423 int startpos, endpos;
4424 Lisp_Object ostart, oend;
4426 XSETMISC (overlay, tail);
4428 ostart = OVERLAY_START (overlay);
4429 oend = OVERLAY_END (overlay);
4430 startpos = OVERLAY_POSITION (ostart);
4431 endpos = OVERLAY_POSITION (oend);
4432 if (XFASTINT (end) < startpos)
4433 break;
4434 if (insertion && (XFASTINT (start) == startpos
4435 || XFASTINT (end) == startpos))
4437 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4438 if (!NILP (prop))
4439 add_overlay_mod_hooklist (prop, overlay);
4441 if (insertion && (XFASTINT (start) == endpos
4442 || XFASTINT (end) == endpos))
4444 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4445 if (!NILP (prop))
4446 add_overlay_mod_hooklist (prop, overlay);
4448 /* Test for intersecting intervals. This does the right thing
4449 for both insertion and deletion. */
4450 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4452 prop = Foverlay_get (overlay, Qmodification_hooks);
4453 if (!NILP (prop))
4454 add_overlay_mod_hooklist (prop, overlay);
4459 GCPRO4 (overlay, arg1, arg2, arg3);
4461 /* Call the functions recorded in last_overlay_modification_hooks.
4462 First copy the vector contents, in case some of these hooks
4463 do subsequent modification of the buffer. */
4464 int size = last_overlay_modification_hooks_used;
4465 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4466 int i;
4468 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4469 copy, size * sizeof (Lisp_Object));
4470 gcpro1.var = copy;
4471 gcpro1.nvars = size;
4473 for (i = 0; i < size;)
4475 Lisp_Object prop, overlay;
4476 prop = copy[i++];
4477 overlay = copy[i++];
4478 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4481 UNGCPRO;
4484 static void
4485 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4486 Lisp_Object list, overlay;
4487 int after;
4488 Lisp_Object arg1, arg2, arg3;
4490 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4492 GCPRO4 (list, arg1, arg2, arg3);
4494 while (CONSP (list))
4496 if (NILP (arg3))
4497 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4498 else
4499 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4500 list = XCDR (list);
4502 UNGCPRO;
4505 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4506 property is set. */
4507 void
4508 evaporate_overlays (pos)
4509 EMACS_INT pos;
4511 Lisp_Object overlay, hit_list;
4512 struct Lisp_Overlay *tail;
4514 hit_list = Qnil;
4515 if (pos <= current_buffer->overlay_center)
4516 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4518 int endpos;
4519 XSETMISC (overlay, tail);
4520 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4521 if (endpos < pos)
4522 break;
4523 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4524 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4525 hit_list = Fcons (overlay, hit_list);
4527 else
4528 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4530 int startpos;
4531 XSETMISC (overlay, tail);
4532 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4533 if (startpos > pos)
4534 break;
4535 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4536 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4537 hit_list = Fcons (overlay, hit_list);
4539 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4540 Fdelete_overlay (XCAR (hit_list));
4543 /* Somebody has tried to store a value with an unacceptable type
4544 in the slot with offset OFFSET. */
4546 void
4547 buffer_slot_type_mismatch (sym, type)
4548 Lisp_Object sym;
4549 int type;
4551 char *type_name;
4553 switch (type)
4555 case Lisp_Int:
4556 type_name = "integers";
4557 break;
4559 case Lisp_String:
4560 type_name = "strings";
4561 break;
4563 case Lisp_Symbol:
4564 type_name = "symbols";
4565 break;
4567 default:
4568 abort ();
4571 error ("Only %s should be stored in the buffer-local variable %s",
4572 type_name, SDATA (SYMBOL_NAME (sym)));
4576 /***********************************************************************
4577 Allocation with mmap
4578 ***********************************************************************/
4580 #ifdef USE_MMAP_FOR_BUFFERS
4582 #include <sys/types.h>
4583 #include <sys/mman.h>
4585 #ifndef MAP_ANON
4586 #ifdef MAP_ANONYMOUS
4587 #define MAP_ANON MAP_ANONYMOUS
4588 #else
4589 #define MAP_ANON 0
4590 #endif
4591 #endif
4593 #ifndef MAP_FAILED
4594 #define MAP_FAILED ((void *) -1)
4595 #endif
4597 #include <stdio.h>
4598 #include <errno.h>
4600 #if MAP_ANON == 0
4601 #include <fcntl.h>
4602 #endif
4604 #include "coding.h"
4607 /* Memory is allocated in regions which are mapped using mmap(2).
4608 The current implementation lets the system select mapped
4609 addresses; we're not using MAP_FIXED in general, except when
4610 trying to enlarge regions.
4612 Each mapped region starts with a mmap_region structure, the user
4613 area starts after that structure, aligned to MEM_ALIGN.
4615 +-----------------------+
4616 | struct mmap_info + |
4617 | padding |
4618 +-----------------------+
4619 | user data |
4622 +-----------------------+ */
4624 struct mmap_region
4626 /* User-specified size. */
4627 size_t nbytes_specified;
4629 /* Number of bytes mapped */
4630 size_t nbytes_mapped;
4632 /* Pointer to the location holding the address of the memory
4633 allocated with the mmap'd block. The variable actually points
4634 after this structure. */
4635 POINTER_TYPE **var;
4637 /* Next and previous in list of all mmap'd regions. */
4638 struct mmap_region *next, *prev;
4641 /* Doubly-linked list of mmap'd regions. */
4643 static struct mmap_region *mmap_regions;
4645 /* File descriptor for mmap. If we don't have anonymous mapping,
4646 /dev/zero will be opened on it. */
4648 static int mmap_fd;
4650 /* Temporary storage for mmap_set_vars, see there. */
4652 static struct mmap_region *mmap_regions_1;
4653 static int mmap_fd_1;
4655 /* Page size on this system. */
4657 static int mmap_page_size;
4659 /* 1 means mmap has been intialized. */
4661 static int mmap_initialized_p;
4663 /* Value is X rounded up to the next multiple of N. */
4665 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4667 /* Size of mmap_region structure plus padding. */
4669 #define MMAP_REGION_STRUCT_SIZE \
4670 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4672 /* Given a pointer P to the start of the user-visible part of a mapped
4673 region, return a pointer to the start of the region. */
4675 #define MMAP_REGION(P) \
4676 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4678 /* Given a pointer P to the start of a mapped region, return a pointer
4679 to the start of the user-visible part of the region. */
4681 #define MMAP_USER_AREA(P) \
4682 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4684 #define MEM_ALIGN sizeof (double)
4686 /* Predicate returning true if part of the address range [START .. END]
4687 is currently mapped. Used to prevent overwriting an existing
4688 memory mapping.
4690 Default is to conservativly assume the address range is occupied by
4691 something else. This can be overridden by system configuration
4692 files if system-specific means to determine this exists. */
4694 #ifndef MMAP_ALLOCATED_P
4695 #define MMAP_ALLOCATED_P(start, end) 1
4696 #endif
4698 /* Function prototypes. */
4700 static int mmap_free_1 P_ ((struct mmap_region *));
4701 static int mmap_enlarge P_ ((struct mmap_region *, int));
4702 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4703 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4704 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4705 static void mmap_free P_ ((POINTER_TYPE **ptr));
4706 static void mmap_init P_ ((void));
4709 /* Return a region overlapping address range START...END, or null if
4710 none. END is not including, i.e. the last byte in the range
4711 is at END - 1. */
4713 static struct mmap_region *
4714 mmap_find (start, end)
4715 POINTER_TYPE *start, *end;
4717 struct mmap_region *r;
4718 char *s = (char *) start, *e = (char *) end;
4720 for (r = mmap_regions; r; r = r->next)
4722 char *rstart = (char *) r;
4723 char *rend = rstart + r->nbytes_mapped;
4725 if (/* First byte of range, i.e. START, in this region? */
4726 (s >= rstart && s < rend)
4727 /* Last byte of range, i.e. END - 1, in this region? */
4728 || (e > rstart && e <= rend)
4729 /* First byte of this region in the range? */
4730 || (rstart >= s && rstart < e)
4731 /* Last byte of this region in the range? */
4732 || (rend > s && rend <= e))
4733 break;
4736 return r;
4740 /* Unmap a region. P is a pointer to the start of the user-araa of
4741 the region. Value is non-zero if successful. */
4743 static int
4744 mmap_free_1 (r)
4745 struct mmap_region *r;
4747 if (r->next)
4748 r->next->prev = r->prev;
4749 if (r->prev)
4750 r->prev->next = r->next;
4751 else
4752 mmap_regions = r->next;
4754 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4756 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4757 return 0;
4760 return 1;
4764 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4765 Value is non-zero if successful. */
4767 static int
4768 mmap_enlarge (r, npages)
4769 struct mmap_region *r;
4770 int npages;
4772 char *region_end = (char *) r + r->nbytes_mapped;
4773 size_t nbytes;
4774 int success = 0;
4776 if (npages < 0)
4778 /* Unmap pages at the end of the region. */
4779 nbytes = - npages * mmap_page_size;
4780 if (munmap (region_end - nbytes, nbytes) == -1)
4781 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4782 else
4784 r->nbytes_mapped -= nbytes;
4785 success = 1;
4788 else if (npages > 0)
4790 nbytes = npages * mmap_page_size;
4792 /* Try to map additional pages at the end of the region. We
4793 cannot do this if the address range is already occupied by
4794 something else because mmap deletes any previous mapping.
4795 I'm not sure this is worth doing, let's see. */
4796 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4798 POINTER_TYPE *p;
4800 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4801 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4802 if (p == MAP_FAILED)
4803 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4804 else if (p != (POINTER_TYPE *) region_end)
4806 /* Kernels are free to choose a different address. In
4807 that case, unmap what we've mapped above; we have
4808 no use for it. */
4809 if (munmap (p, nbytes) == -1)
4810 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4812 else
4814 r->nbytes_mapped += nbytes;
4815 success = 1;
4820 return success;
4824 /* Set or reset variables holding references to mapped regions. If
4825 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4826 non-zero, set all variables to the start of the user-areas
4827 of mapped regions.
4829 This function is called from Fdump_emacs to ensure that the dumped
4830 Emacs doesn't contain references to memory that won't be mapped
4831 when Emacs starts. */
4833 void
4834 mmap_set_vars (restore_p)
4835 int restore_p;
4837 struct mmap_region *r;
4839 if (restore_p)
4841 mmap_regions = mmap_regions_1;
4842 mmap_fd = mmap_fd_1;
4843 for (r = mmap_regions; r; r = r->next)
4844 *r->var = MMAP_USER_AREA (r);
4846 else
4848 for (r = mmap_regions; r; r = r->next)
4849 *r->var = NULL;
4850 mmap_regions_1 = mmap_regions;
4851 mmap_regions = NULL;
4852 mmap_fd_1 = mmap_fd;
4853 mmap_fd = -1;
4858 /* Allocate a block of storage large enough to hold NBYTES bytes of
4859 data. A pointer to the data is returned in *VAR. VAR is thus the
4860 address of some variable which will use the data area.
4862 The allocation of 0 bytes is valid.
4864 If we can't allocate the necessary memory, set *VAR to null, and
4865 return null. */
4867 static POINTER_TYPE *
4868 mmap_alloc (var, nbytes)
4869 POINTER_TYPE **var;
4870 size_t nbytes;
4872 void *p;
4873 size_t map;
4875 mmap_init ();
4877 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4878 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4879 mmap_fd, 0);
4881 if (p == MAP_FAILED)
4883 if (errno != ENOMEM)
4884 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4885 p = NULL;
4887 else
4889 struct mmap_region *r = (struct mmap_region *) p;
4891 r->nbytes_specified = nbytes;
4892 r->nbytes_mapped = map;
4893 r->var = var;
4894 r->prev = NULL;
4895 r->next = mmap_regions;
4896 if (r->next)
4897 r->next->prev = r;
4898 mmap_regions = r;
4900 p = MMAP_USER_AREA (p);
4903 return *var = p;
4907 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4908 resize it to size NBYTES. Change *VAR to reflect the new block,
4909 and return this value. If more memory cannot be allocated, then
4910 leave *VAR unchanged, and return null. */
4912 static POINTER_TYPE *
4913 mmap_realloc (var, nbytes)
4914 POINTER_TYPE **var;
4915 size_t nbytes;
4917 POINTER_TYPE *result;
4919 mmap_init ();
4921 if (*var == NULL)
4922 result = mmap_alloc (var, nbytes);
4923 else if (nbytes == 0)
4925 mmap_free (var);
4926 result = mmap_alloc (var, nbytes);
4928 else
4930 struct mmap_region *r = MMAP_REGION (*var);
4931 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4933 if (room < nbytes)
4935 /* Must enlarge. */
4936 POINTER_TYPE *old_ptr = *var;
4938 /* Try to map additional pages at the end of the region.
4939 If that fails, allocate a new region, copy data
4940 from the old region, then free it. */
4941 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4942 / mmap_page_size)))
4944 r->nbytes_specified = nbytes;
4945 *var = result = old_ptr;
4947 else if (mmap_alloc (var, nbytes))
4949 bcopy (old_ptr, *var, r->nbytes_specified);
4950 mmap_free_1 (MMAP_REGION (old_ptr));
4951 result = *var;
4952 r = MMAP_REGION (result);
4953 r->nbytes_specified = nbytes;
4955 else
4957 *var = old_ptr;
4958 result = NULL;
4961 else if (room - nbytes >= mmap_page_size)
4963 /* Shrinking by at least a page. Let's give some
4964 memory back to the system.
4966 The extra parens are to make the division happens first,
4967 on positive values, so we know it will round towards
4968 zero. */
4969 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4970 result = *var;
4971 r->nbytes_specified = nbytes;
4973 else
4975 /* Leave it alone. */
4976 result = *var;
4977 r->nbytes_specified = nbytes;
4981 return result;
4985 /* Free a block of relocatable storage whose data is pointed to by
4986 PTR. Store 0 in *PTR to show there's no block allocated. */
4988 static void
4989 mmap_free (var)
4990 POINTER_TYPE **var;
4992 mmap_init ();
4994 if (*var)
4996 mmap_free_1 (MMAP_REGION (*var));
4997 *var = NULL;
5002 /* Perform necessary intializations for the use of mmap. */
5004 static void
5005 mmap_init ()
5007 #if MAP_ANON == 0
5008 /* The value of mmap_fd is initially 0 in temacs, and -1
5009 in a dumped Emacs. */
5010 if (mmap_fd <= 0)
5012 /* No anonymous mmap -- we need the file descriptor. */
5013 mmap_fd = open ("/dev/zero", O_RDONLY);
5014 if (mmap_fd == -1)
5015 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5017 #endif /* MAP_ANON == 0 */
5019 if (mmap_initialized_p)
5020 return;
5021 mmap_initialized_p = 1;
5023 #if MAP_ANON != 0
5024 mmap_fd = -1;
5025 #endif
5027 mmap_page_size = getpagesize ();
5030 #endif /* USE_MMAP_FOR_BUFFERS */
5034 /***********************************************************************
5035 Buffer-text Allocation
5036 ***********************************************************************/
5038 #ifdef REL_ALLOC
5039 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5040 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5041 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5042 #endif /* REL_ALLOC */
5045 /* Allocate NBYTES bytes for buffer B's text buffer. */
5047 static void
5048 alloc_buffer_text (b, nbytes)
5049 struct buffer *b;
5050 size_t nbytes;
5052 POINTER_TYPE *p;
5054 BLOCK_INPUT;
5055 #if defined USE_MMAP_FOR_BUFFERS
5056 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5057 #elif defined REL_ALLOC
5058 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5059 #else
5060 p = xmalloc (nbytes);
5061 #endif
5063 if (p == NULL)
5065 UNBLOCK_INPUT;
5066 memory_full ();
5069 b->text->beg = (unsigned char *) p;
5070 UNBLOCK_INPUT;
5073 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5074 shrink it. */
5076 void
5077 enlarge_buffer_text (b, delta)
5078 struct buffer *b;
5079 int delta;
5081 POINTER_TYPE *p;
5082 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5083 + delta);
5084 BLOCK_INPUT;
5085 #if defined USE_MMAP_FOR_BUFFERS
5086 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5087 #elif defined REL_ALLOC
5088 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5089 #else
5090 p = xrealloc (b->text->beg, nbytes);
5091 #endif
5093 if (p == NULL)
5095 UNBLOCK_INPUT;
5096 memory_full ();
5099 BUF_BEG_ADDR (b) = (unsigned char *) p;
5100 UNBLOCK_INPUT;
5104 /* Free buffer B's text buffer. */
5106 static void
5107 free_buffer_text (b)
5108 struct buffer *b;
5110 BLOCK_INPUT;
5112 #if defined USE_MMAP_FOR_BUFFERS
5113 mmap_free ((POINTER_TYPE **) &b->text->beg);
5114 #elif defined REL_ALLOC
5115 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5116 #else
5117 xfree (b->text->beg);
5118 #endif
5120 BUF_BEG_ADDR (b) = NULL;
5121 UNBLOCK_INPUT;
5126 /***********************************************************************
5127 Initialization
5128 ***********************************************************************/
5130 void
5131 init_buffer_once ()
5133 int idx;
5135 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5137 /* Make sure all markable slots in buffer_defaults
5138 are initialized reasonably, so mark_buffer won't choke. */
5139 reset_buffer (&buffer_defaults);
5140 reset_buffer_local_variables (&buffer_defaults, 1);
5141 reset_buffer (&buffer_local_symbols);
5142 reset_buffer_local_variables (&buffer_local_symbols, 1);
5143 /* Prevent GC from getting confused. */
5144 buffer_defaults.text = &buffer_defaults.own_text;
5145 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5146 BUF_INTERVALS (&buffer_defaults) = 0;
5147 BUF_INTERVALS (&buffer_local_symbols) = 0;
5148 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5149 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5150 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5151 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5153 /* Set up the default values of various buffer slots. */
5154 /* Must do these before making the first buffer! */
5156 /* real setup is done in bindings.el */
5157 buffer_defaults.mode_line_format = build_string ("%-");
5158 buffer_defaults.header_line_format = Qnil;
5159 buffer_defaults.abbrev_mode = Qnil;
5160 buffer_defaults.overwrite_mode = Qnil;
5161 buffer_defaults.case_fold_search = Qt;
5162 buffer_defaults.auto_fill_function = Qnil;
5163 buffer_defaults.selective_display = Qnil;
5164 #ifndef old
5165 buffer_defaults.selective_display_ellipses = Qt;
5166 #endif
5167 buffer_defaults.abbrev_table = Qnil;
5168 buffer_defaults.display_table = Qnil;
5169 buffer_defaults.undo_list = Qnil;
5170 buffer_defaults.mark_active = Qnil;
5171 buffer_defaults.file_format = Qnil;
5172 buffer_defaults.auto_save_file_format = Qt;
5173 buffer_defaults.overlays_before = NULL;
5174 buffer_defaults.overlays_after = NULL;
5175 buffer_defaults.overlay_center = BEG;
5177 XSETFASTINT (buffer_defaults.tab_width, 8);
5178 buffer_defaults.truncate_lines = Qnil;
5179 buffer_defaults.ctl_arrow = Qt;
5180 buffer_defaults.direction_reversed = Qnil;
5181 buffer_defaults.cursor_type = Qt;
5182 buffer_defaults.extra_line_spacing = Qnil;
5183 buffer_defaults.cursor_in_non_selected_windows = Qt;
5185 #ifdef DOS_NT
5186 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5187 #endif
5188 buffer_defaults.enable_multibyte_characters = Qt;
5189 buffer_defaults.buffer_file_coding_system = Qnil;
5190 XSETFASTINT (buffer_defaults.fill_column, 70);
5191 XSETFASTINT (buffer_defaults.left_margin, 0);
5192 buffer_defaults.cache_long_line_scans = Qnil;
5193 buffer_defaults.file_truename = Qnil;
5194 XSETFASTINT (buffer_defaults.display_count, 0);
5195 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5196 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5197 buffer_defaults.left_fringe_width = Qnil;
5198 buffer_defaults.right_fringe_width = Qnil;
5199 buffer_defaults.fringes_outside_margins = Qnil;
5200 buffer_defaults.scroll_bar_width = Qnil;
5201 buffer_defaults.vertical_scroll_bar_type = Qt;
5202 buffer_defaults.indicate_empty_lines = Qnil;
5203 buffer_defaults.indicate_buffer_boundaries = Qnil;
5204 buffer_defaults.fringe_indicator_alist = Qnil;
5205 buffer_defaults.fringe_cursor_alist = Qnil;
5206 buffer_defaults.scroll_up_aggressively = Qnil;
5207 buffer_defaults.scroll_down_aggressively = Qnil;
5208 buffer_defaults.display_time = Qnil;
5210 /* Assign the local-flags to the slots that have default values.
5211 The local flag is a bit that is used in the buffer
5212 to say that it has its own local value for the slot.
5213 The local flag bits are in the local_var_flags slot of the buffer. */
5215 /* Nothing can work if this isn't true */
5216 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5218 /* 0 means not a lisp var, -1 means always local, else mask */
5219 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5220 XSETINT (buffer_local_flags.filename, -1);
5221 XSETINT (buffer_local_flags.directory, -1);
5222 XSETINT (buffer_local_flags.backed_up, -1);
5223 XSETINT (buffer_local_flags.save_length, -1);
5224 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5225 XSETINT (buffer_local_flags.read_only, -1);
5226 XSETINT (buffer_local_flags.major_mode, -1);
5227 XSETINT (buffer_local_flags.mode_name, -1);
5228 XSETINT (buffer_local_flags.undo_list, -1);
5229 XSETINT (buffer_local_flags.mark_active, -1);
5230 XSETINT (buffer_local_flags.point_before_scroll, -1);
5231 XSETINT (buffer_local_flags.file_truename, -1);
5232 XSETINT (buffer_local_flags.invisibility_spec, -1);
5233 XSETINT (buffer_local_flags.file_format, -1);
5234 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5235 XSETINT (buffer_local_flags.display_count, -1);
5236 XSETINT (buffer_local_flags.display_time, -1);
5237 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5239 idx = 1;
5240 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5241 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5242 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5243 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5244 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5245 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5246 #ifndef old
5247 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5248 #endif
5249 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5250 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5251 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5252 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5253 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5254 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5255 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5256 #ifdef DOS_NT
5257 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5258 /* Make this one a permanent local. */
5259 buffer_permanent_local_flags[idx++] = 1;
5260 #endif
5261 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5262 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5263 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5264 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5265 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5266 /* Make this one a permanent local. */
5267 buffer_permanent_local_flags[idx++] = 1;
5268 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5269 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5270 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5271 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5272 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5273 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5274 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5275 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5276 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5277 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5278 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5279 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5280 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5281 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5282 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5283 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5284 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5286 /* Need more room? */
5287 if (idx >= MAX_PER_BUFFER_VARS)
5288 abort ();
5289 last_per_buffer_idx = idx;
5291 Vbuffer_alist = Qnil;
5292 current_buffer = 0;
5293 all_buffers = 0;
5295 QSFundamental = build_string ("Fundamental");
5297 Qfundamental_mode = intern ("fundamental-mode");
5298 buffer_defaults.major_mode = Qfundamental_mode;
5300 Qmode_class = intern ("mode-class");
5302 Qprotected_field = intern ("protected-field");
5304 Qpermanent_local = intern ("permanent-local");
5306 Qkill_buffer_hook = intern ("kill-buffer-hook");
5307 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5309 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5311 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5313 /* super-magic invisible buffer */
5314 Vbuffer_alist = Qnil;
5316 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5318 inhibit_modification_hooks = 0;
5321 void
5322 init_buffer ()
5324 char *pwd;
5325 Lisp_Object temp;
5326 int len;
5328 #ifdef USE_MMAP_FOR_BUFFERS
5330 /* When using the ralloc implementation based on mmap(2), buffer
5331 text pointers will have been set to null in the dumped Emacs.
5332 Map new memory. */
5333 struct buffer *b;
5335 for (b = all_buffers; b; b = b->next)
5336 if (b->text->beg == NULL)
5337 enlarge_buffer_text (b, 0);
5339 #endif /* USE_MMAP_FOR_BUFFERS */
5341 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5342 if (NILP (buffer_defaults.enable_multibyte_characters))
5343 Fset_buffer_multibyte (Qnil);
5345 pwd = get_current_dir_name ();
5347 if (!pwd)
5348 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5350 #ifndef VMS
5351 /* Maybe this should really use some standard subroutine
5352 whose definition is filename syntax dependent. */
5353 len = strlen (pwd);
5354 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5356 /* Grow buffer to add directory separator and '\0'. */
5357 pwd = (char *) xrealloc (pwd, len + 2);
5358 pwd[len] = DIRECTORY_SEP;
5359 pwd[len + 1] = '\0';
5361 #endif /* not VMS */
5363 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5364 if (! NILP (buffer_defaults.enable_multibyte_characters))
5365 /* At this moment, we still don't know how to decode the
5366 directory name. So, we keep the bytes in multibyte form so
5367 that ENCODE_FILE correctly gets the original bytes. */
5368 current_buffer->directory
5369 = string_to_multibyte (current_buffer->directory);
5371 /* Add /: to the front of the name
5372 if it would otherwise be treated as magic. */
5373 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5374 if (! NILP (temp)
5375 /* If the default dir is just /, TEMP is non-nil
5376 because of the ange-ftp completion handler.
5377 However, it is not necessary to turn / into /:/.
5378 So avoid doing that. */
5379 && strcmp ("/", SDATA (current_buffer->directory)))
5380 current_buffer->directory
5381 = concat2 (build_string ("/:"), current_buffer->directory);
5383 temp = get_minibuffer (0);
5384 XBUFFER (temp)->directory = current_buffer->directory;
5386 free (pwd);
5389 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5390 Object stored in the current buffer. address is the address of the slot
5391 in the buffer that is current now. */
5393 /* TYPE is nil for a general Lisp variable.
5394 An integer specifies a type; then only LIsp values
5395 with that type code are allowed (except that nil is allowed too).
5396 LNAME is the LIsp-level variable name.
5397 VNAME is the name of the buffer slot.
5398 DOC is a dummy where you write the doc string as a comment. */
5399 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5400 defvar_per_buffer (lname, vname, type, 0)
5402 static void
5403 defvar_per_buffer (namestring, address, type, doc)
5404 char *namestring;
5405 Lisp_Object *address;
5406 Lisp_Object type;
5407 char *doc;
5409 Lisp_Object sym, val;
5410 int offset;
5412 sym = intern (namestring);
5413 val = allocate_misc ();
5414 offset = (char *)address - (char *)current_buffer;
5416 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5417 XBUFFER_OBJFWD (val)->offset = offset;
5418 XBUFFER_OBJFWD (val)->slottype = type;
5419 SET_SYMBOL_VALUE (sym, val);
5420 PER_BUFFER_SYMBOL (offset) = sym;
5422 if (PER_BUFFER_IDX (offset) == 0)
5423 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5424 slot of buffer_local_flags */
5425 abort ();
5429 /* initialize the buffer routines */
5430 void
5431 syms_of_buffer ()
5433 staticpro (&last_overlay_modification_hooks);
5434 last_overlay_modification_hooks
5435 = Fmake_vector (make_number (10), Qnil);
5437 staticpro (&Vbuffer_defaults);
5438 staticpro (&Vbuffer_local_symbols);
5439 staticpro (&Qfundamental_mode);
5440 staticpro (&Qmode_class);
5441 staticpro (&QSFundamental);
5442 staticpro (&Vbuffer_alist);
5443 staticpro (&Qprotected_field);
5444 staticpro (&Qpermanent_local);
5445 Qpermanent_local_hook = intern ("permanent-local-hook");
5446 staticpro (&Qpermanent_local_hook);
5447 staticpro (&Qkill_buffer_hook);
5448 Qoverlayp = intern ("overlayp");
5449 staticpro (&Qoverlayp);
5450 Qevaporate = intern ("evaporate");
5451 staticpro (&Qevaporate);
5452 Qmodification_hooks = intern ("modification-hooks");
5453 staticpro (&Qmodification_hooks);
5454 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5455 staticpro (&Qinsert_in_front_hooks);
5456 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5457 staticpro (&Qinsert_behind_hooks);
5458 Qget_file_buffer = intern ("get-file-buffer");
5459 staticpro (&Qget_file_buffer);
5460 Qpriority = intern ("priority");
5461 staticpro (&Qpriority);
5462 Qwindow = intern ("window");
5463 staticpro (&Qwindow);
5464 Qbefore_string = intern ("before-string");
5465 staticpro (&Qbefore_string);
5466 Qafter_string = intern ("after-string");
5467 staticpro (&Qafter_string);
5468 Qfirst_change_hook = intern ("first-change-hook");
5469 staticpro (&Qfirst_change_hook);
5470 Qbefore_change_functions = intern ("before-change-functions");
5471 staticpro (&Qbefore_change_functions);
5472 Qafter_change_functions = intern ("after-change-functions");
5473 staticpro (&Qafter_change_functions);
5474 /* The next one is initialized in init_buffer_once. */
5475 staticpro (&Qucs_set_table_for_input);
5477 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5478 staticpro (&Qkill_buffer_query_functions);
5480 Fput (Qprotected_field, Qerror_conditions,
5481 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5482 Fput (Qprotected_field, Qerror_message,
5483 build_string ("Attempt to modify a protected field"));
5485 /* All these use DEFVAR_LISP_NOPRO because the slots in
5486 buffer_defaults will all be marked via Vbuffer_defaults. */
5488 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5489 &buffer_defaults.mode_line_format,
5490 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5491 This is the same as (default-value 'mode-line-format). */);
5493 DEFVAR_LISP_NOPRO ("default-header-line-format",
5494 &buffer_defaults.header_line_format,
5495 doc: /* Default value of `header-line-format' for buffers that don't override it.
5496 This is the same as (default-value 'header-line-format). */);
5498 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5499 doc: /* Default value of `cursor-type' for buffers that don't override it.
5500 This is the same as (default-value 'cursor-type). */);
5502 DEFVAR_LISP_NOPRO ("default-line-spacing",
5503 &buffer_defaults.extra_line_spacing,
5504 doc: /* Default value of `line-spacing' for buffers that don't override it.
5505 This is the same as (default-value 'line-spacing). */);
5507 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5508 &buffer_defaults.cursor_in_non_selected_windows,
5509 doc: /* Default value of `cursor-in-non-selected-windows'.
5510 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5512 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5513 &buffer_defaults.abbrev_mode,
5514 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5515 This is the same as (default-value 'abbrev-mode). */);
5517 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5518 &buffer_defaults.ctl_arrow,
5519 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5520 This is the same as (default-value 'ctl-arrow). */);
5522 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5523 &buffer_defaults.direction_reversed,
5524 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5525 This is the same as (default-value 'direction-reversed). */);
5527 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5528 &buffer_defaults.enable_multibyte_characters,
5529 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5530 This is the same as (default-value 'enable-multibyte-characters). */);
5532 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5533 &buffer_defaults.buffer_file_coding_system,
5534 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5535 This is the same as (default-value 'buffer-file-coding-system). */);
5537 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5538 &buffer_defaults.truncate_lines,
5539 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5540 This is the same as (default-value 'truncate-lines). */);
5542 DEFVAR_LISP_NOPRO ("default-fill-column",
5543 &buffer_defaults.fill_column,
5544 doc: /* Default value of `fill-column' for buffers that do not override it.
5545 This is the same as (default-value 'fill-column). */);
5547 DEFVAR_LISP_NOPRO ("default-left-margin",
5548 &buffer_defaults.left_margin,
5549 doc: /* Default value of `left-margin' for buffers that do not override it.
5550 This is the same as (default-value 'left-margin). */);
5552 DEFVAR_LISP_NOPRO ("default-tab-width",
5553 &buffer_defaults.tab_width,
5554 doc: /* Default value of `tab-width' for buffers that do not override it.
5555 This is the same as (default-value 'tab-width). */);
5557 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5558 &buffer_defaults.case_fold_search,
5559 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5560 This is the same as (default-value 'case-fold-search). */);
5562 #ifdef DOS_NT
5563 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5564 &buffer_defaults.buffer_file_type,
5565 doc: /* Default file type for buffers that do not override it.
5566 This is the same as (default-value 'buffer-file-type).
5567 The file type is nil for text, t for binary. */);
5568 #endif
5570 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5571 &buffer_defaults.left_margin_cols,
5572 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5573 This is the same as (default-value 'left-margin-width). */);
5575 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5576 &buffer_defaults.right_margin_cols,
5577 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5578 This is the same as (default-value 'right-margin-width). */);
5580 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5581 &buffer_defaults.left_fringe_width,
5582 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5583 This is the same as (default-value 'left-fringe-width). */);
5585 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5586 &buffer_defaults.right_fringe_width,
5587 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5588 This is the same as (default-value 'right-fringe-width). */);
5590 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5591 &buffer_defaults.fringes_outside_margins,
5592 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5593 This is the same as (default-value 'fringes-outside-margins). */);
5595 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5596 &buffer_defaults.scroll_bar_width,
5597 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5598 This is the same as (default-value 'scroll-bar-width). */);
5600 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5601 &buffer_defaults.vertical_scroll_bar_type,
5602 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5603 This is the same as (default-value 'vertical-scroll-bar). */);
5605 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5606 &buffer_defaults.indicate_empty_lines,
5607 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5608 This is the same as (default-value 'indicate-empty-lines). */);
5610 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5611 &buffer_defaults.indicate_buffer_boundaries,
5612 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5613 This is the same as (default-value 'indicate-buffer-boundaries). */);
5615 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5616 &buffer_defaults.fringe_indicator_alist,
5617 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5618 This is the same as (default-value 'fringe-indicator-alist'). */);
5620 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5621 &buffer_defaults.fringe_cursor_alist,
5622 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5623 This is the same as (default-value 'fringe-cursor-alist'). */);
5625 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5626 &buffer_defaults.scroll_up_aggressively,
5627 doc: /* Default value of `scroll-up-aggressively'.
5628 This value applies in buffers that don't have their own local values.
5629 This is the same as (default-value 'scroll-up-aggressively). */);
5631 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5632 &buffer_defaults.scroll_down_aggressively,
5633 doc: /* Default value of `scroll-down-aggressively'.
5634 This value applies in buffers that don't have their own local values.
5635 This is the same as (default-value 'scroll-down-aggressively). */);
5637 DEFVAR_PER_BUFFER ("header-line-format",
5638 &current_buffer->header_line_format,
5639 Qnil,
5640 doc: /* Analogous to `mode-line-format', but controls the header line.
5641 The header line appears, optionally, at the top of a window;
5642 the mode line appears at the bottom. */);
5644 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5645 Qnil,
5646 doc: /* Template for displaying mode line for current buffer.
5647 Each buffer has its own value of this variable.
5648 Value may be nil, a string, a symbol or a list or cons cell.
5649 A value of nil means don't display a mode line.
5650 For a symbol, its value is used (but it is ignored if t or nil).
5651 A string appearing directly as the value of a symbol is processed verbatim
5652 in that the %-constructs below are not recognized.
5653 Note that unless the symbol is marked as a `risky-local-variable', all
5654 properties in any strings, as well as all :eval and :propertize forms
5655 in the value of that symbol will be ignored.
5656 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5657 is used as a mode line element. Be careful--FORM should not load any files,
5658 because that can cause an infinite recursion.
5659 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5660 with the specified properties PROPS applied.
5661 For a list whose car is a symbol, the symbol's value is taken,
5662 and if that is non-nil, the cadr of the list is processed recursively.
5663 Otherwise, the caddr of the list (if there is one) is processed.
5664 For a list whose car is a string or list, each element is processed
5665 recursively and the results are effectively concatenated.
5666 For a list whose car is an integer, the cdr of the list is processed
5667 and padded (if the number is positive) or truncated (if negative)
5668 to the width specified by that number.
5669 A string is printed verbatim in the mode line except for %-constructs:
5670 (%-constructs are allowed when the string is the entire mode-line-format
5671 or when it is found in a cons-cell or a list)
5672 %b -- print buffer name. %f -- print visited file name.
5673 %F -- print frame name.
5674 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5675 %& is like %*, but ignore read-only-ness.
5676 % means buffer is read-only and * means it is modified.
5677 For a modified read-only buffer, %* gives % and %+ gives *.
5678 %s -- print process status. %l -- print the current line number.
5679 %c -- print the current column number (this makes editing slower).
5680 To make the column number update correctly in all cases,
5681 `column-number-mode' must be non-nil.
5682 %i -- print the size of the buffer.
5683 %I -- like %i, but use k, M, G, etc., to abbreviate.
5684 %p -- print percent of buffer above top of window, or Top, Bot or All.
5685 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5686 or print Bottom or All.
5687 %n -- print Narrow if appropriate.
5688 %t -- visited file is text or binary (if OS supports this distinction).
5689 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5690 %Z -- like %z, but including the end-of-line format.
5691 %e -- print error message about full memory.
5692 %@ -- print @ or hyphen. @ means that default-directory is on a
5693 remote machine.
5694 %[ -- print one [ for each recursive editing level. %] similar.
5695 %% -- print %. %- -- print infinitely many dashes.
5696 Decimal digits after the % specify field width to which to pad. */);
5698 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5699 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5700 A value of nil means use current buffer's major mode,
5701 provided it is not marked as "special".
5703 When a mode is used by default, `find-file' switches to it
5704 before it reads the contents into the buffer and before
5705 it finishes setting up the buffer. Thus, the mode and
5706 its hooks should not expect certain variables such as
5707 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5709 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5710 make_number (Lisp_Symbol),
5711 doc: /* Symbol for current buffer's major mode. */);
5713 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5714 Qnil,
5715 doc: /* Pretty name of current buffer's major mode.
5716 Usually a string. See `mode-line-format' for other possible forms.
5717 Use the function `format-mode-line' to get the value as a string. */);
5719 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5720 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5722 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5723 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5725 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5726 Qnil,
5727 doc: /* *Non-nil if searches and matches should ignore case. */);
5729 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5730 make_number (Lisp_Int),
5731 doc: /* *Column beyond which automatic line-wrapping should happen.
5732 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5734 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5735 make_number (Lisp_Int),
5736 doc: /* *Column for the default `indent-line-function' to indent to.
5737 Linefeed indents to this column in Fundamental mode. */);
5739 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5740 make_number (Lisp_Int),
5741 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5743 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5744 doc: /* *Non-nil means display control chars with uparrow.
5745 A value of nil means use backslash and octal digits.
5746 This variable does not apply to characters whose display is specified
5747 in the current display table (if there is one). */);
5749 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5750 &current_buffer->enable_multibyte_characters,
5751 Qnil,
5752 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5753 Otherwise they are regarded as unibyte. This affects the display,
5754 file I/O and the behavior of various editing commands.
5756 This variable is buffer-local but you cannot set it directly;
5757 use the function `set-buffer-multibyte' to change a buffer's representation.
5758 Changing its default value with `setq-default' is supported.
5759 See also variable `default-enable-multibyte-characters' and Info node
5760 `(elisp)Text Representations'. */);
5761 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5763 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5764 &current_buffer->buffer_file_coding_system, Qnil,
5765 doc: /* Coding system to be used for encoding the buffer contents on saving.
5766 This variable applies to saving the buffer, and also to `write-region'
5767 and other functions that use `write-region'.
5768 It does not apply to sending output to subprocesses, however.
5770 If this is nil, the buffer is saved without any code conversion
5771 unless some coding system is specified in `file-coding-system-alist'
5772 for the buffer file.
5774 If the text to be saved cannot be encoded as specified by this variable,
5775 an alternative encoding is selected by `select-safe-coding-system', which see.
5777 The variable `coding-system-for-write', if non-nil, overrides this variable.
5779 This variable is never applied to a way of decoding a file while reading it. */);
5781 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5782 Qnil,
5783 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5785 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5786 doc: /* *Non-nil means do not display continuation lines.
5787 Instead, give each line of text just one screen line.
5789 Note that this is overridden by the variable
5790 `truncate-partial-width-windows' if that variable is non-nil
5791 and this buffer is not full-frame width. */);
5793 #ifdef DOS_NT
5794 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5795 Qnil,
5796 doc: /* Non-nil if the visited file is a binary file.
5797 This variable is meaningful on MS-DOG and Windows NT.
5798 On those systems, it is automatically local in every buffer.
5799 On other systems, this variable is normally always nil. */);
5800 #endif
5802 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5803 make_number (Lisp_String),
5804 doc: /* Name of default directory of current buffer. Should end with slash.
5805 To interactively change the default directory, use command `cd'. */);
5807 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5808 Qnil,
5809 doc: /* Function called (if non-nil) to perform auto-fill.
5810 It is called after self-inserting any character specified in
5811 the `auto-fill-chars' table.
5812 NOTE: This variable is not a hook;
5813 its value may not be a list of functions. */);
5815 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5816 make_number (Lisp_String),
5817 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5819 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5820 make_number (Lisp_String),
5821 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5822 The truename of a file is calculated by `file-truename'
5823 and then abbreviated with `abbreviate-file-name'. */);
5825 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5826 &current_buffer->auto_save_file_name,
5827 make_number (Lisp_String),
5828 doc: /* Name of file for auto-saving current buffer.
5829 If it is nil, that means don't auto-save this buffer. */);
5831 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5832 doc: /* Non-nil if this buffer is read-only. */);
5834 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5835 doc: /* Non-nil if this buffer's file has been backed up.
5836 Backing up is done before the first time the file is saved. */);
5838 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5839 make_number (Lisp_Int),
5840 doc: /* Length of current buffer when last read in, saved or auto-saved.
5841 0 initially. */);
5843 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5844 Qnil,
5845 doc: /* Non-nil enables selective display.
5846 An integer N as value means display only lines
5847 that start with less than N columns of space.
5848 A value of t means that the character ^M makes itself and
5849 all the rest of the line invisible; also, when saving the buffer
5850 in a file, save the ^M as a newline. */);
5852 #ifndef old
5853 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5854 &current_buffer->selective_display_ellipses,
5855 Qnil,
5856 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5857 #endif
5859 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5860 doc: /* Non-nil if self-insertion should replace existing text.
5861 The value should be one of `overwrite-mode-textual',
5862 `overwrite-mode-binary', or nil.
5863 If it is `overwrite-mode-textual', self-insertion still
5864 inserts at the end of a line, and inserts when point is before a tab,
5865 until the tab is filled in.
5866 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5868 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5869 Qnil,
5870 doc: /* Display table that controls display of the contents of current buffer.
5872 If this variable is nil, the value of `standard-display-table' is used.
5873 Each window can have its own, overriding display table, see
5874 `set-window-display-table' and `window-display-table'.
5876 The display table is a char-table created with `make-display-table'.
5877 A char-table is an array indexed by character codes. Normal array
5878 primitives `aref' and `aset' can be used to access elements of a char-table.
5880 Each of the char-table elements control how to display the corresponding
5881 text character: the element at index C in the table says how to display
5882 the character whose code is C. Each element should be a vector of
5883 characters or nil. The value nil means display the character in the
5884 default fashion; otherwise, the characters from the vector are delivered
5885 to the screen instead of the original character.
5887 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5888 to display a capital Y instead of each X character.
5890 In addition, a char-table has six extra slots to control the display of:
5892 the end of a truncated screen line (extra-slot 0, a single character);
5893 the end of a continued line (extra-slot 1, a single character);
5894 the escape character used to display character codes in octal
5895 (extra-slot 2, a single character);
5896 the character used as an arrow for control characters (extra-slot 3,
5897 a single character);
5898 the decoration indicating the presence of invisible lines (extra-slot 4,
5899 a vector of characters);
5900 the character used to draw the border between side-by-side windows
5901 (extra-slot 5, a single character).
5903 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5905 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5906 Qnil,
5907 doc: /* *Width of left marginal area for display of a buffer.
5908 A value of nil means no marginal area. */);
5910 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5911 Qnil,
5912 doc: /* *Width of right marginal area for display of a buffer.
5913 A value of nil means no marginal area. */);
5915 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5916 Qnil,
5917 doc: /* *Width of this buffer's left fringe (in pixels).
5918 A value of 0 means no left fringe is shown in this buffer's window.
5919 A value of nil means to use the left fringe width from the window's frame. */);
5921 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5922 Qnil,
5923 doc: /* *Width of this buffer's right fringe (in pixels).
5924 A value of 0 means no right fringe is shown in this buffer's window.
5925 A value of nil means to use the right fringe width from the window's frame. */);
5927 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5928 Qnil,
5929 doc: /* *Non-nil means to display fringes outside display margins.
5930 A value of nil means to display fringes between margins and buffer text. */);
5932 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5933 Qnil,
5934 doc: /* *Width of this buffer's scroll bars in pixels.
5935 A value of nil means to use the scroll bar width from the window's frame. */);
5937 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5938 Qnil,
5939 doc: /* *Position of this buffer's vertical scroll bar.
5940 The value takes effect whenever you tell a window to display this buffer;
5941 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5943 A value of `left' or `right' means put the vertical scroll bar at that side
5944 of the window; a value of nil means don't show any vertical scroll bars.
5945 A value of t (the default) means do whatever the window's frame specifies. */);
5947 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5948 &current_buffer->indicate_empty_lines, Qnil,
5949 doc: /* *Visually indicate empty lines after the buffer end.
5950 If non-nil, a bitmap is displayed in the left fringe of a window on
5951 window-systems. */);
5953 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5954 &current_buffer->indicate_buffer_boundaries, Qnil,
5955 doc: /* *Visually indicate buffer boundaries and scrolling.
5956 If non-nil, the first and last line of the buffer are marked in the fringe
5957 of a window on window-systems with angle bitmaps, or if the window can be
5958 scrolled, the top and bottom line of the window are marked with up and down
5959 arrow bitmaps.
5961 If value is a symbol `left' or `right', both angle and arrow bitmaps
5962 are displayed in the left or right fringe, resp. Any other value
5963 that doesn't look like an alist means display the angle bitmaps in
5964 the left fringe but no arrows.
5966 You can exercise more precise control by using an alist as the
5967 value. Each alist element (INDICATOR . POSITION) specifies
5968 where to show one of the indicators. INDICATOR is one of `top',
5969 `bottom', `up', `down', or t, which specifies the default position,
5970 and POSITION is one of `left', `right', or nil, meaning do not show
5971 this indicator.
5973 For example, ((top . left) (t . right)) places the top angle bitmap in
5974 left fringe, the bottom angle bitmap in right fringe, and both arrow
5975 bitmaps in right fringe. To show just the angle bitmaps in the left
5976 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5978 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5979 &current_buffer->fringe_indicator_alist, Qnil,
5980 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5981 The value is an alist where each element (INDICATOR . BITMAPS)
5982 specifies the fringe bitmaps used to display a specific logical
5983 fringe indicator.
5985 INDICATOR specifies the logical indicator type which is one of the
5986 following symbols: `truncation' , `continuation', `overlay-arrow',
5987 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5989 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5990 the actual bitmap shown in the left or right fringe for the logical
5991 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5992 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5993 are used only for the `bottom' and `one-line' indicators when the last
5994 \(only) line in has no final newline. BITMAPS may also be a single
5995 symbol which is used in both left and right fringes. */);
5997 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5998 &current_buffer->fringe_cursor_alist, Qnil,
5999 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
6000 The value is an alist where each element (CURSOR . BITMAP)
6001 specifies the fringe bitmaps used to display a specific logical
6002 cursor type in the fringe.
6004 CURSOR specifies the logical cursor type which is one of the following
6005 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6006 one is used to show a hollow cursor on narrow lines display lines
6007 where the normal hollow cursor will not fit.
6009 BITMAP is the corresponding fringe bitmap shown for the logical
6010 cursor type. */);
6012 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
6013 &current_buffer->scroll_up_aggressively, Qnil,
6014 doc: /* How far to scroll windows upward.
6015 If you move point off the bottom, the window scrolls automatically.
6016 This variable controls how far it scrolls. The value nil, the default,
6017 means scroll to center point. A fraction means scroll to put point
6018 that fraction of the window's height from the bottom of the window.
6019 When the value is 0.0, point goes at the bottom line, which in the simple
6020 case that you moved off with C-f means scrolling just one line. 1.0 means
6021 point goes at the top, so that in that simple case, the window
6022 scrolls by a full window height. Meaningful values are
6023 between 0.0 and 1.0, inclusive. */);
6025 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6026 &current_buffer->scroll_down_aggressively, Qnil,
6027 doc: /* How far to scroll windows downward.
6028 If you move point off the top, the window scrolls automatically.
6029 This variable controls how far it scrolls. The value nil, the default,
6030 means scroll to center point. A fraction means scroll to put point
6031 that fraction of the window's height from the top of the window.
6032 When the value is 0.0, point goes at the top line, which in the simple
6033 case that you moved off with C-b means scrolling just one line. 1.0 means
6034 point goes at the bottom, so that in that simple case, the window
6035 scrolls by a full window height. Meaningful values are
6036 between 0.0 and 1.0, inclusive. */);
6038 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6039 "Don't ask.");
6042 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6043 doc: /* List of functions to call before each text change.
6044 Two arguments are passed to each function: the positions of
6045 the beginning and end of the range of old text to be changed.
6046 \(For an insertion, the beginning and end are at the same place.)
6047 No information is given about the length of the text after the change.
6049 Buffer changes made while executing the `before-change-functions'
6050 don't call any before-change or after-change functions.
6051 That's because these variables are temporarily set to nil.
6052 As a result, a hook function cannot straightforwardly alter the value of
6053 these variables. See the Emacs Lisp manual for a way of
6054 accomplishing an equivalent result by using other variables.
6056 If an unhandled error happens in running these functions,
6057 the variable's value remains nil. That prevents the error
6058 from happening repeatedly and making Emacs nonfunctional. */);
6059 Vbefore_change_functions = Qnil;
6061 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6062 doc: /* List of functions to call after each text change.
6063 Three arguments are passed to each function: the positions of
6064 the beginning and end of the range of changed text,
6065 and the length in bytes of the pre-change text replaced by that range.
6066 \(For an insertion, the pre-change length is zero;
6067 for a deletion, that length is the number of bytes deleted,
6068 and the post-change beginning and end are at the same place.)
6070 Buffer changes made while executing the `after-change-functions'
6071 don't call any before-change or after-change functions.
6072 That's because these variables are temporarily set to nil.
6073 As a result, a hook function cannot straightforwardly alter the value of
6074 these variables. See the Emacs Lisp manual for a way of
6075 accomplishing an equivalent result by using other variables.
6077 If an unhandled error happens in running these functions,
6078 the variable's value remains nil. That prevents the error
6079 from happening repeatedly and making Emacs nonfunctional. */);
6080 Vafter_change_functions = Qnil;
6082 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6083 doc: /* A list of functions to call before changing a buffer which is unmodified.
6084 The functions are run using the `run-hooks' function. */);
6085 Vfirst_change_hook = Qnil;
6087 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6088 doc: /* List of undo entries in current buffer.
6089 Recent changes come first; older changes follow newer.
6091 An entry (BEG . END) represents an insertion which begins at
6092 position BEG and ends at position END.
6094 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6095 from (abs POSITION). If POSITION is positive, point was at the front
6096 of the text being deleted; if negative, point was at the end.
6098 An entry (t HIGH . LOW) indicates that the buffer previously had
6099 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6100 of the visited file's modification time, as of that time. If the
6101 modification time of the most recent save is different, this entry is
6102 obsolete.
6104 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6105 was modified between BEG and END. PROPERTY is the property name,
6106 and VALUE is the old value.
6108 An entry (apply FUN-NAME . ARGS) means undo the change with
6109 \(apply FUN-NAME ARGS).
6111 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6112 in the active region. BEG and END is the range affected by this entry
6113 and DELTA is the number of bytes added or deleted in that range by
6114 this change.
6116 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6117 was adjusted in position by the offset DISTANCE (an integer).
6119 An entry of the form POSITION indicates that point was at the buffer
6120 location given by the integer. Undoing an entry of this form places
6121 point at POSITION.
6123 Entries with value `nil' mark undo boundaries. The undo command treats
6124 the changes between two undo boundaries as a single step to be undone.
6126 If the value of the variable is t, undo information is not recorded. */);
6128 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6129 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6131 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6132 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6134 Normally, the line-motion functions work by scanning the buffer for
6135 newlines. Columnar operations (like `move-to-column' and
6136 `compute-motion') also work by scanning the buffer, summing character
6137 widths as they go. This works well for ordinary text, but if the
6138 buffer's lines are very long (say, more than 500 characters), these
6139 motion functions will take longer to execute. Emacs may also take
6140 longer to update the display.
6142 If `cache-long-line-scans' is non-nil, these motion functions cache the
6143 results of their scans, and consult the cache to avoid rescanning
6144 regions of the buffer until the text is modified. The caches are most
6145 beneficial when they prevent the most searching---that is, when the
6146 buffer contains long lines and large regions of characters with the
6147 same, fixed screen width.
6149 When `cache-long-line-scans' is non-nil, processing short lines will
6150 become slightly slower (because of the overhead of consulting the
6151 cache), and the caches will use memory roughly proportional to the
6152 number of newlines and characters whose screen width varies.
6154 The caches require no explicit maintenance; their accuracy is
6155 maintained internally by the Emacs primitives. Enabling or disabling
6156 the cache should not affect the behavior of any of the motion
6157 functions; it should only affect their performance. */);
6159 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6160 doc: /* Value of point before the last series of scroll operations, or nil. */);
6162 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6163 doc: /* List of formats to use when saving this buffer.
6164 Formats are defined by `format-alist'. This variable is
6165 set when a file is visited. */);
6167 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6168 &current_buffer->auto_save_file_format, Qnil,
6169 doc: /* *Format in which to write auto-save files.
6170 Should be a list of symbols naming formats that are defined in `format-alist'.
6171 If it is t, which is the default, auto-save files are written in the
6172 same format as a regular save would use. */);
6174 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6175 &current_buffer->invisibility_spec, Qnil,
6176 doc: /* Invisibility spec of this buffer.
6177 The default is t, which means that text is invisible
6178 if it has a non-nil `invisible' property.
6179 If the value is a list, a text character is invisible if its `invisible'
6180 property is an element in that list (or is a list with members in common).
6181 If an element is a cons cell of the form (PROP . ELLIPSIS),
6182 then characters with property value PROP are invisible,
6183 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6185 DEFVAR_PER_BUFFER ("buffer-display-count",
6186 &current_buffer->display_count, Qnil,
6187 doc: /* A number incremented each time this buffer is displayed in a window.
6188 The function `set-window-buffer' increments it. */);
6190 DEFVAR_PER_BUFFER ("buffer-display-time",
6191 &current_buffer->display_time, Qnil,
6192 doc: /* Time stamp updated each time this buffer is displayed in a window.
6193 The function `set-window-buffer' updates this variable
6194 to the value obtained by calling `current-time'.
6195 If the buffer has never been shown in a window, the value is nil. */);
6197 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6198 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
6199 Non-nil also enables highlighting of the region whenever the mark is active.
6200 The variable `highlight-nonselected-windows' controls whether to highlight
6201 all windows or just the selected window.
6203 If the value is `lambda', that enables Transient Mark mode temporarily
6204 until the next buffer modification. If a command sets the value to `only',
6205 that enables Transient Mark mode for the following command only.
6206 During that following command, the value of `transient-mark-mode'
6207 is `identity'. If it is still `identity' at the end of that command,
6208 it changes to nil. */);
6209 Vtransient_mark_mode = Qnil;
6211 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6212 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6213 If the value is t, disregard `buffer-read-only' and all `read-only'
6214 text properties. If the value is a list, disregard `buffer-read-only'
6215 and disregard a `read-only' text property if the property value
6216 is a member of the list. */);
6217 Vinhibit_read_only = Qnil;
6219 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6220 doc: /* Cursor to use when this buffer is in the selected window.
6221 Values are interpreted as follows:
6223 t use the cursor specified for the frame
6224 nil don't display a cursor
6225 box display a filled box cursor
6226 hollow display a hollow box cursor
6227 bar display a vertical bar cursor with default width
6228 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6229 hbar display a horizontal bar cursor with default height
6230 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6231 ANYTHING ELSE display a hollow box cursor
6233 When the buffer is displayed in a nonselected window,
6234 this variable has no effect; the cursor appears as a hollow box. */);
6236 DEFVAR_PER_BUFFER ("line-spacing",
6237 &current_buffer->extra_line_spacing, Qnil,
6238 doc: /* Additional space to put between lines when displaying a buffer.
6239 The space is measured in pixels, and put below lines on window systems.
6240 If value is a floating point number, it specifies the spacing relative
6241 to the default frame line height. A value of nil means add no extra space. */);
6243 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6244 &current_buffer->cursor_in_non_selected_windows, Qnil,
6245 doc: /* *Cursor type to display in non-selected windows.
6246 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6248 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6249 doc: /* List of functions called with no args to query before killing a buffer.
6250 The buffer being killed will be current while the functions are running.
6251 If any of them returns nil, the buffer is not killed. */);
6252 Vkill_buffer_query_functions = Qnil;
6254 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6255 doc: /* Normal hook run before changing the major mode of a buffer.
6256 The function `kill-all-local-variables' runs this before doing anything else. */);
6257 Vchange_major_mode_hook = Qnil;
6258 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6259 staticpro (&Qchange_major_mode_hook);
6261 defsubr (&Sbuffer_live_p);
6262 defsubr (&Sbuffer_list);
6263 defsubr (&Sget_buffer);
6264 defsubr (&Sget_file_buffer);
6265 defsubr (&Sget_buffer_create);
6266 defsubr (&Smake_indirect_buffer);
6267 defsubr (&Sgenerate_new_buffer_name);
6268 defsubr (&Sbuffer_name);
6269 /*defsubr (&Sbuffer_number);*/
6270 defsubr (&Sbuffer_file_name);
6271 defsubr (&Sbuffer_base_buffer);
6272 defsubr (&Sbuffer_local_value);
6273 defsubr (&Sbuffer_local_variables);
6274 defsubr (&Sbuffer_modified_p);
6275 defsubr (&Sset_buffer_modified_p);
6276 defsubr (&Sbuffer_modified_tick);
6277 defsubr (&Sbuffer_chars_modified_tick);
6278 defsubr (&Srename_buffer);
6279 defsubr (&Sother_buffer);
6280 defsubr (&Sbuffer_enable_undo);
6281 defsubr (&Skill_buffer);
6282 defsubr (&Sset_buffer_major_mode);
6283 defsubr (&Sswitch_to_buffer);
6284 defsubr (&Spop_to_buffer);
6285 defsubr (&Scurrent_buffer);
6286 defsubr (&Sset_buffer);
6287 defsubr (&Sbarf_if_buffer_read_only);
6288 defsubr (&Sbury_buffer);
6289 defsubr (&Serase_buffer);
6290 defsubr (&Sbuffer_swap_text);
6291 defsubr (&Sset_buffer_multibyte);
6292 defsubr (&Skill_all_local_variables);
6294 defsubr (&Soverlayp);
6295 defsubr (&Smake_overlay);
6296 defsubr (&Sdelete_overlay);
6297 defsubr (&Smove_overlay);
6298 defsubr (&Soverlay_start);
6299 defsubr (&Soverlay_end);
6300 defsubr (&Soverlay_buffer);
6301 defsubr (&Soverlay_properties);
6302 defsubr (&Soverlays_at);
6303 defsubr (&Soverlays_in);
6304 defsubr (&Snext_overlay_change);
6305 defsubr (&Sprevious_overlay_change);
6306 defsubr (&Soverlay_recenter);
6307 defsubr (&Soverlay_lists);
6308 defsubr (&Soverlay_get);
6309 defsubr (&Soverlay_put);
6310 defsubr (&Srestore_buffer_modified_p);
6313 void
6314 keys_of_buffer ()
6316 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6317 initial_define_key (control_x_map, 'k', "kill-buffer");
6319 /* This must not be in syms_of_buffer, because Qdisabled is not
6320 initialized when that function gets called. */
6321 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6324 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6325 (do not change this comment) */