Fix syntax scanning bug causing fontification crashes.
[emacs.git] / src / buffer.c
blob70d7d00edfee7e2d0d8f69b16d7b144dce57e804
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <setjmp.h>
31 #ifndef USE_CRT_DLL
32 extern int errno;
33 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #include "lisp.h"
41 #include "intervals.h"
42 #include "window.h"
43 #include "commands.h"
44 #include "buffer.h"
45 #include "character.h"
46 #include "region-cache.h"
47 #include "indent.h"
48 #include "blockinput.h"
49 #include "keyboard.h"
50 #include "keymap.h"
51 #include "frame.h"
53 struct buffer *current_buffer; /* the current buffer */
55 /* First buffer in chain of all buffers (in reverse order of creation).
56 Threaded through ->header.next.buffer. */
58 struct buffer *all_buffers;
60 /* This structure holds the default values of the buffer-local variables
61 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
62 The default value occupies the same slot in this structure
63 as an individual buffer's value occupies in that buffer.
64 Setting the default value also goes through the alist of buffers
65 and stores into each buffer that does not say it has a local value. */
67 DECL_ALIGN (struct buffer, buffer_defaults);
69 /* A Lisp_Object pointer to the above, used for staticpro */
71 static Lisp_Object Vbuffer_defaults;
73 /* This structure marks which slots in a buffer have corresponding
74 default values in buffer_defaults.
75 Each such slot has a nonzero value in this structure.
76 The value has only one nonzero bit.
78 When a buffer has its own local value for a slot,
79 the entry for that slot (found in the same slot in this structure)
80 is turned on in the buffer's local_flags array.
82 If a slot in this structure is -1, then even though there may
83 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
84 and the corresponding slot in buffer_defaults is not used.
86 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
87 but there is a default value which is copied into each buffer.
89 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
90 zero, that is a bug */
92 struct buffer buffer_local_flags;
94 /* This structure holds the names of symbols whose values may be
95 buffer-local. It is indexed and accessed in the same way as the above. */
97 DECL_ALIGN (struct buffer, buffer_local_symbols);
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols;
102 /* Flags indicating which built-in buffer-local variables
103 are permanent locals. */
104 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
106 /* Number of per-buffer variables used. */
108 int last_per_buffer_idx;
110 EXFUN (Fset_buffer, 1);
111 void set_buffer_internal P_ ((struct buffer *b));
112 void set_buffer_internal_1 P_ ((struct buffer *b));
113 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
114 int after, Lisp_Object arg1,
115 Lisp_Object arg2, Lisp_Object arg3));
116 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
117 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
119 /* Alist of all buffer names vs the buffers. */
120 /* This used to be a variable, but is no longer,
121 to prevent lossage due to user rplac'ing this alist or its elements. */
122 Lisp_Object Vbuffer_alist;
124 /* Functions to call before and after each text change. */
125 Lisp_Object Vbefore_change_functions;
126 Lisp_Object Vafter_change_functions;
128 Lisp_Object Vtransient_mark_mode;
130 /* t means ignore all read-only text properties.
131 A list means ignore such a property if its value is a member of the list.
132 Any non-nil value means ignore buffer-read-only. */
133 Lisp_Object Vinhibit_read_only;
135 /* List of functions to call that can query about killing a buffer.
136 If any of these functions returns nil, we don't kill it. */
137 Lisp_Object Vkill_buffer_query_functions;
138 Lisp_Object Qkill_buffer_query_functions;
140 /* Hook run before changing a major mode. */
141 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
143 /* List of functions to call before changing an unmodified buffer. */
144 Lisp_Object Vfirst_change_hook;
146 Lisp_Object Qfirst_change_hook;
147 Lisp_Object Qbefore_change_functions;
148 Lisp_Object Qafter_change_functions;
149 Lisp_Object Qucs_set_table_for_input;
151 /* If nonzero, all modification hooks are suppressed. */
152 int inhibit_modification_hooks;
154 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
155 Lisp_Object Qpermanent_local_hook;
157 Lisp_Object Qprotected_field;
159 Lisp_Object QSFundamental; /* A string "Fundamental" */
161 Lisp_Object Qkill_buffer_hook;
163 Lisp_Object Qget_file_buffer;
165 Lisp_Object Qoverlayp;
167 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
169 Lisp_Object Qmodification_hooks;
170 Lisp_Object Qinsert_in_front_hooks;
171 Lisp_Object Qinsert_behind_hooks;
173 static void alloc_buffer_text P_ ((struct buffer *, size_t));
174 static void free_buffer_text P_ ((struct buffer *b));
175 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
176 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
177 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
179 extern char * emacs_strerror P_ ((int));
181 /* For debugging; temporary. See set_buffer_internal. */
182 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
184 void
185 nsberror (spec)
186 Lisp_Object spec;
188 if (STRINGP (spec))
189 error ("No buffer named %s", SDATA (spec));
190 error ("Invalid buffer argument");
193 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
194 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
195 Value is nil if OBJECT is not a buffer or if it has been killed. */)
196 (object)
197 Lisp_Object object;
199 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
200 ? Qt : Qnil);
203 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
204 doc: /* Return a list of all existing live buffers.
205 If the optional arg FRAME is a frame, we return the buffer list
206 in the proper order for that frame: the buffers in FRAME's `buffer-list'
207 frame parameter come first, followed by the rest of the buffers. */)
208 (frame)
209 Lisp_Object frame;
211 Lisp_Object general;
212 general = Fmapcar (Qcdr, Vbuffer_alist);
214 if (FRAMEP (frame))
216 Lisp_Object framelist, prevlist, tail;
217 Lisp_Object args[3];
219 CHECK_FRAME (frame);
221 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
222 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
224 /* Remove from GENERAL any buffer that duplicates one in
225 FRAMELIST or PREVLIST. */
226 tail = framelist;
227 while (CONSP (tail))
229 general = Fdelq (XCAR (tail), general);
230 tail = XCDR (tail);
232 tail = prevlist;
233 while (CONSP (tail))
235 general = Fdelq (XCAR (tail), general);
236 tail = XCDR (tail);
239 args[0] = framelist;
240 args[1] = general;
241 args[2] = prevlist;
242 return Fnconc (3, args);
245 return general;
248 /* Like Fassoc, but use Fstring_equal to compare
249 (which ignores text properties),
250 and don't ever QUIT. */
252 static Lisp_Object
253 assoc_ignore_text_properties (key, list)
254 register Lisp_Object key;
255 Lisp_Object list;
257 register Lisp_Object tail;
258 for (tail = list; CONSP (tail); tail = XCDR (tail))
260 register Lisp_Object elt, tem;
261 elt = XCAR (tail);
262 tem = Fstring_equal (Fcar (elt), key);
263 if (!NILP (tem))
264 return elt;
266 return Qnil;
269 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
270 doc: /* Return the buffer named BUFFER-OR-NAME.
271 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
272 is a string and there is no buffer with that name, return nil. If
273 BUFFER-OR-NAME is a buffer, return it as given. */)
274 (buffer_or_name)
275 register Lisp_Object buffer_or_name;
277 if (BUFFERP (buffer_or_name))
278 return buffer_or_name;
279 CHECK_STRING (buffer_or_name);
281 return Fcdr (assoc_ignore_text_properties (buffer_or_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 specified by BUFFER-OR-NAME, creating a new one if needed.
339 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
340 return that buffer. If no such buffer exists, create a new buffer with
341 that name and return it. If BUFFER-OR-NAME starts with a space, the new
342 buffer does not keep undo information.
344 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
345 even if it is dead. The return value is never nil. */)
346 (buffer_or_name)
347 register Lisp_Object buffer_or_name;
349 register Lisp_Object buffer, name;
350 register struct buffer *b;
352 buffer = Fget_buffer (buffer_or_name);
353 if (!NILP (buffer))
354 return buffer;
356 if (SCHARS (buffer_or_name) == 0)
357 error ("Empty string for buffer name is not allowed");
359 b = allocate_buffer ();
361 /* An ordinary buffer uses its own struct buffer_text. */
362 b->text = &b->own_text;
363 b->base_buffer = 0;
365 BUF_GAP_SIZE (b) = 20;
366 BLOCK_INPUT;
367 /* We allocate extra 1-byte at the tail and keep it always '\0' for
368 anchoring a search. */
369 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
370 UNBLOCK_INPUT;
371 if (! BUF_BEG_ADDR (b))
372 buffer_memory_full ();
374 b->pt = BEG;
375 b->begv = BEG;
376 b->zv = BEG;
377 b->pt_byte = BEG_BYTE;
378 b->begv_byte = BEG_BYTE;
379 b->zv_byte = BEG_BYTE;
381 BUF_GPT (b) = BEG;
382 BUF_GPT_BYTE (b) = BEG_BYTE;
384 BUF_Z (b) = BEG;
385 BUF_Z_BYTE (b) = BEG_BYTE;
386 BUF_MODIFF (b) = 1;
387 BUF_CHARS_MODIFF (b) = 1;
388 BUF_OVERLAY_MODIFF (b) = 1;
389 BUF_SAVE_MODIFF (b) = 1;
390 BUF_INTERVALS (b) = 0;
391 BUF_UNCHANGED_MODIFIED (b) = 1;
392 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
393 BUF_END_UNCHANGED (b) = 0;
394 BUF_BEG_UNCHANGED (b) = 0;
395 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
397 b->newline_cache = 0;
398 b->width_run_cache = 0;
399 b->width_table = Qnil;
400 b->prevent_redisplay_optimizations_p = 1;
402 /* Put this on the chain of all buffers including killed ones. */
403 b->header.next.buffer = all_buffers;
404 all_buffers = b;
406 /* An ordinary buffer normally doesn't need markers
407 to handle BEGV and ZV. */
408 b->pt_marker = Qnil;
409 b->begv_marker = Qnil;
410 b->zv_marker = Qnil;
412 name = Fcopy_sequence (buffer_or_name);
413 STRING_SET_INTERVALS (name, NULL_INTERVAL);
414 b->name = name;
416 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
418 reset_buffer (b);
419 reset_buffer_local_variables (b, 1);
421 b->mark = Fmake_marker ();
422 BUF_MARKERS (b) = NULL;
423 b->name = name;
425 /* Put this in the alist of all live buffers. */
426 XSETBUFFER (buffer, b);
427 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
429 /* An error in calling the function here (should someone redefine it)
430 can lead to infinite regress until you run out of stack. rms
431 says that's not worth protecting against. */
432 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
433 /* buffer is on buffer-alist, so no gcpro. */
434 call1 (Qucs_set_table_for_input, buffer);
436 return buffer;
440 /* Return a list of overlays which is a copy of the overlay list
441 LIST, but for buffer B. */
443 static struct Lisp_Overlay *
444 copy_overlays (b, list)
445 struct buffer *b;
446 struct Lisp_Overlay *list;
448 Lisp_Object buffer;
449 struct Lisp_Overlay *result = NULL, *tail = NULL;
451 XSETBUFFER (buffer, b);
453 for (; list; list = list->next)
455 Lisp_Object overlay, start, end, old_overlay;
456 EMACS_INT charpos;
458 XSETMISC (old_overlay, list);
459 charpos = marker_position (OVERLAY_START (old_overlay));
460 start = Fmake_marker ();
461 Fset_marker (start, make_number (charpos), buffer);
462 XMARKER (start)->insertion_type
463 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
465 charpos = marker_position (OVERLAY_END (old_overlay));
466 end = Fmake_marker ();
467 Fset_marker (end, make_number (charpos), buffer);
468 XMARKER (end)->insertion_type
469 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
471 overlay = allocate_misc ();
472 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
473 OVERLAY_START (overlay) = start;
474 OVERLAY_END (overlay) = end;
475 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
476 XOVERLAY (overlay)->next = NULL;
478 if (tail)
479 tail = tail->next = XOVERLAY (overlay);
480 else
481 result = tail = XOVERLAY (overlay);
484 return result;
488 /* Clone per-buffer values of buffer FROM.
490 Buffer TO gets the same per-buffer values as FROM, with the
491 following exceptions: (1) TO's name is left untouched, (2) markers
492 are copied and made to refer to TO, and (3) overlay lists are
493 copied. */
495 static void
496 clone_per_buffer_values (from, to)
497 struct buffer *from, *to;
499 Lisp_Object to_buffer;
500 int offset;
502 XSETBUFFER (to_buffer, to);
504 /* buffer-local Lisp variables start at `undo_list',
505 tho only the ones from `name' on are GC'd normally. */
506 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
507 offset < sizeof *to;
508 offset += sizeof (Lisp_Object))
510 Lisp_Object obj;
512 /* Don't touch the `name' which should be unique for every buffer. */
513 if (offset == PER_BUFFER_VAR_OFFSET (name))
514 continue;
516 obj = PER_BUFFER_VALUE (from, offset);
517 if (MARKERP (obj))
519 struct Lisp_Marker *m = XMARKER (obj);
520 obj = Fmake_marker ();
521 XMARKER (obj)->insertion_type = m->insertion_type;
522 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
525 PER_BUFFER_VALUE (to, offset) = obj;
528 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
530 to->overlays_before = copy_overlays (to, from->overlays_before);
531 to->overlays_after = copy_overlays (to, from->overlays_after);
533 /* Get (a copy of) the alist of Lisp-level local variables of FROM
534 and install that in TO. */
535 to->local_var_alist = buffer_lisp_local_variables (from);
539 /* If buffer B has markers to record PT, BEGV and ZV when it is not
540 current, update these markers. */
542 static void
543 record_buffer_markers (struct buffer *b)
545 if (! NILP (b->pt_marker))
547 Lisp_Object buffer;
549 eassert (!NILP (b->begv_marker));
550 eassert (!NILP (b->zv_marker));
552 XSETBUFFER (buffer, b);
553 set_marker_both (b->pt_marker, buffer, b->pt, b->pt_byte);
554 set_marker_both (b->begv_marker, buffer, b->begv, b->begv_byte);
555 set_marker_both (b->zv_marker, buffer, b->zv, b->zv_byte);
560 /* If buffer B has markers to record PT, BEGV and ZV when it is not
561 current, fetch these values into B->begv etc. */
563 static void
564 fetch_buffer_markers (struct buffer *b)
566 if (! NILP (b->pt_marker))
568 Lisp_Object m;
570 eassert (!NILP (b->begv_marker));
571 eassert (!NILP (b->zv_marker));
573 m = b->pt_marker;
574 SET_BUF_PT_BOTH (b, marker_position (m), marker_byte_position (m));
576 m = b->begv_marker;
577 SET_BUF_BEGV_BOTH (b, marker_position (m), marker_byte_position (m));
579 m = b->zv_marker;
580 SET_BUF_ZV_BOTH (b, marker_position (m), marker_byte_position (m));
585 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
586 2, 3,
587 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
588 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
589 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
590 NAME should be a string which is not the name of an existing buffer.
591 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
592 such as major and minor modes, in the indirect buffer.
593 CLONE nil means the indirect buffer's state is reset to default values. */)
594 (base_buffer, name, clone)
595 Lisp_Object base_buffer, name, clone;
597 Lisp_Object buf, tem;
598 struct buffer *b;
600 CHECK_STRING (name);
601 buf = Fget_buffer (name);
602 if (!NILP (buf))
603 error ("Buffer name `%s' is in use", SDATA (name));
605 tem = base_buffer;
606 base_buffer = Fget_buffer (base_buffer);
607 if (NILP (base_buffer))
608 error ("No such buffer: `%s'", SDATA (tem));
609 if (NILP (XBUFFER (base_buffer)->name))
610 error ("Base buffer has been killed");
612 if (SCHARS (name) == 0)
613 error ("Empty string for buffer name is not allowed");
615 b = allocate_buffer ();
617 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
618 ? XBUFFER (base_buffer)->base_buffer
619 : XBUFFER (base_buffer));
621 /* Use the base buffer's text object. */
622 b->text = b->base_buffer->text;
624 b->pt = b->base_buffer->pt;
625 b->begv = b->base_buffer->begv;
626 b->zv = b->base_buffer->zv;
627 b->pt_byte = b->base_buffer->pt_byte;
628 b->begv_byte = b->base_buffer->begv_byte;
629 b->zv_byte = b->base_buffer->zv_byte;
631 b->newline_cache = 0;
632 b->width_run_cache = 0;
633 b->width_table = Qnil;
635 /* Put this on the chain of all buffers including killed ones. */
636 b->header.next.buffer = all_buffers;
637 all_buffers = b;
639 name = Fcopy_sequence (name);
640 STRING_SET_INTERVALS (name, NULL_INTERVAL);
641 b->name = name;
643 reset_buffer (b);
644 reset_buffer_local_variables (b, 1);
646 /* Put this in the alist of all live buffers. */
647 XSETBUFFER (buf, b);
648 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
650 b->mark = Fmake_marker ();
651 b->name = name;
653 /* The multibyte status belongs to the base buffer. */
654 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
656 /* Make sure the base buffer has markers for its narrowing. */
657 if (NILP (b->base_buffer->pt_marker))
659 eassert (NILP (b->base_buffer->begv_marker));
660 eassert (NILP (b->base_buffer->zv_marker));
662 b->base_buffer->pt_marker = Fmake_marker ();
663 set_marker_both (b->base_buffer->pt_marker, base_buffer,
664 b->base_buffer->pt,
665 b->base_buffer->pt_byte);
667 b->base_buffer->begv_marker = Fmake_marker ();
668 set_marker_both (b->base_buffer->begv_marker, base_buffer,
669 b->base_buffer->begv,
670 b->base_buffer->begv_byte);
672 b->base_buffer->zv_marker = Fmake_marker ();
673 set_marker_both (b->base_buffer->zv_marker, base_buffer,
674 b->base_buffer->zv,
675 b->base_buffer->zv_byte);
676 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
679 if (NILP (clone))
681 /* Give the indirect buffer markers for its narrowing. */
682 b->pt_marker = Fmake_marker ();
683 set_marker_both (b->pt_marker, buf, b->pt, b->pt_byte);
684 b->begv_marker = Fmake_marker ();
685 set_marker_both (b->begv_marker, buf, b->begv, b->begv_byte);
686 b->zv_marker = Fmake_marker ();
687 set_marker_both (b->zv_marker, buf, b->zv, b->zv_byte);
688 XMARKER (b->zv_marker)->insertion_type = 1;
690 else
692 struct buffer *old_b = current_buffer;
694 clone_per_buffer_values (b->base_buffer, b);
695 b->filename = Qnil;
696 b->file_truename = Qnil;
697 b->display_count = make_number (0);
698 b->backed_up = Qnil;
699 b->auto_save_file_name = Qnil;
700 set_buffer_internal_1 (b);
701 Fset (intern ("buffer-save-without-query"), Qnil);
702 Fset (intern ("buffer-file-number"), Qnil);
703 Fset (intern ("buffer-stale-function"), Qnil);
704 set_buffer_internal_1 (old_b);
707 return buf;
710 void
711 delete_all_overlays (b)
712 struct buffer *b;
714 Lisp_Object overlay;
716 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
717 have to empty the list, otherwise we end up with overlays that
718 think they belong to this buffer while the buffer doesn't know about
719 them any more. */
720 while (b->overlays_before)
722 XSETMISC (overlay, b->overlays_before);
723 Fdelete_overlay (overlay);
725 while (b->overlays_after)
727 XSETMISC (overlay, b->overlays_after);
728 Fdelete_overlay (overlay);
730 eassert (b->overlays_before == NULL);
731 eassert (b->overlays_after == NULL);
734 /* Reinitialize everything about a buffer except its name and contents
735 and local variables.
736 If called on an already-initialized buffer, the list of overlays
737 should be deleted before calling this function, otherwise we end up
738 with overlays that claim to belong to the buffer but the buffer
739 claims it doesn't belong to it. */
741 void
742 reset_buffer (b)
743 register struct buffer *b;
745 b->filename = Qnil;
746 b->file_truename = Qnil;
747 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
748 b->modtime = 0;
749 XSETFASTINT (b->save_length, 0);
750 b->last_window_start = 1;
751 /* It is more conservative to start out "changed" than "unchanged". */
752 b->clip_changed = 0;
753 b->prevent_redisplay_optimizations_p = 1;
754 b->backed_up = Qnil;
755 BUF_AUTOSAVE_MODIFF (b) = 0;
756 b->auto_save_failure_time = -1;
757 b->auto_save_file_name = Qnil;
758 b->read_only = Qnil;
759 b->overlays_before = NULL;
760 b->overlays_after = NULL;
761 b->overlay_center = BEG;
762 b->mark_active = Qnil;
763 b->point_before_scroll = Qnil;
764 b->file_format = Qnil;
765 b->auto_save_file_format = Qt;
766 b->last_selected_window = Qnil;
767 XSETINT (b->display_count, 0);
768 b->display_time = Qnil;
769 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
770 b->cursor_type = buffer_defaults.cursor_type;
771 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
773 b->display_error_modiff = 0;
776 /* Reset buffer B's local variables info.
777 Don't use this on a buffer that has already been in use;
778 it does not treat permanent locals consistently.
779 Instead, use Fkill_all_local_variables.
781 If PERMANENT_TOO is 1, then we reset permanent
782 buffer-local variables. If PERMANENT_TOO is 0,
783 we preserve those. */
785 static void
786 reset_buffer_local_variables (b, permanent_too)
787 register struct buffer *b;
788 int permanent_too;
790 register int offset;
791 int i;
793 /* Reset the major mode to Fundamental, together with all the
794 things that depend on the major mode.
795 default-major-mode is handled at a higher level.
796 We ignore it here. */
797 b->major_mode = Qfundamental_mode;
798 b->keymap = Qnil;
799 b->mode_name = QSFundamental;
800 b->minor_modes = Qnil;
802 /* If the standard case table has been altered and invalidated,
803 fix up its insides first. */
804 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
805 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
806 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
807 Fset_standard_case_table (Vascii_downcase_table);
809 b->downcase_table = Vascii_downcase_table;
810 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
811 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
812 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
813 b->invisibility_spec = Qt;
814 #ifndef DOS_NT
815 b->buffer_file_type = Qnil;
816 #endif
818 /* Reset all (or most) per-buffer variables to their defaults. */
819 if (permanent_too)
820 b->local_var_alist = Qnil;
821 else
823 Lisp_Object tmp, prop, last = Qnil;
824 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
825 if (CONSP (XCAR (tmp))
826 && SYMBOLP (XCAR (XCAR (tmp)))
827 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
829 /* If permanent-local, keep it. */
830 last = tmp;
831 if (EQ (prop, Qpermanent_local_hook))
833 /* This is a partially permanent hook variable.
834 Preserve only the elements that want to be preserved. */
835 Lisp_Object list, newlist;
836 list = XCDR (XCAR (tmp));
837 if (!CONSP (list))
838 newlist = list;
839 else
840 for (newlist = Qnil; CONSP (list); list = XCDR (list))
842 Lisp_Object elt = XCAR (list);
843 /* Preserve element ELT if it's t,
844 if it is a function with a `permanent-local-hook' property,
845 or if it's not a symbol. */
846 if (! SYMBOLP (elt)
847 || EQ (elt, Qt)
848 || !NILP (Fget (elt, Qpermanent_local_hook)))
849 newlist = Fcons (elt, newlist);
851 XSETCDR (XCAR (tmp), Fnreverse (newlist));
854 /* Delete this local variable. */
855 else if (NILP (last))
856 b->local_var_alist = XCDR (tmp);
857 else
858 XSETCDR (last, XCDR (tmp));
861 for (i = 0; i < last_per_buffer_idx; ++i)
862 if (permanent_too || buffer_permanent_local_flags[i] == 0)
863 SET_PER_BUFFER_VALUE_P (b, i, 0);
865 /* For each slot that has a default value,
866 copy that into the slot. */
868 /* buffer-local Lisp variables start at `undo_list',
869 tho only the ones from `name' on are GC'd normally. */
870 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
871 offset < sizeof *b;
872 offset += sizeof (Lisp_Object))
874 int idx = PER_BUFFER_IDX (offset);
875 if ((idx > 0
876 && (permanent_too
877 || buffer_permanent_local_flags[idx] == 0))
878 /* Is -2 used anywhere? */
879 || idx == -2)
880 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
884 /* We split this away from generate-new-buffer, because rename-buffer
885 and set-visited-file-name ought to be able to use this to really
886 rename the buffer properly. */
888 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
889 1, 2, 0,
890 doc: /* Return a string that is the name of no existing buffer based on NAME.
891 If there is no live buffer named NAME, then return NAME.
892 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
893 \(starting at 2) until an unused name is found, and then return that name.
894 Optional second argument IGNORE specifies a name that is okay to use (if
895 it is in the sequence to be tried) even if a buffer with that name exists. */)
896 (name, ignore)
897 register Lisp_Object name, ignore;
899 register Lisp_Object gentemp, tem;
900 int count;
901 char number[10];
903 CHECK_STRING (name);
905 tem = Fstring_equal (name, ignore);
906 if (!NILP (tem))
907 return name;
908 tem = Fget_buffer (name);
909 if (NILP (tem))
910 return name;
912 count = 1;
913 while (1)
915 sprintf (number, "<%d>", ++count);
916 gentemp = concat2 (name, build_string (number));
917 tem = Fstring_equal (gentemp, ignore);
918 if (!NILP (tem))
919 return gentemp;
920 tem = Fget_buffer (gentemp);
921 if (NILP (tem))
922 return gentemp;
927 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
928 doc: /* Return the name of BUFFER, as a string.
929 BUFFER defaults to the current buffer.
930 Return nil if BUFFER has been killed. */)
931 (buffer)
932 register Lisp_Object buffer;
934 if (NILP (buffer))
935 return current_buffer->name;
936 CHECK_BUFFER (buffer);
937 return XBUFFER (buffer)->name;
940 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
941 doc: /* Return name of file BUFFER is visiting, or nil if none.
942 No argument or nil as argument means use the current buffer. */)
943 (buffer)
944 register Lisp_Object buffer;
946 if (NILP (buffer))
947 return current_buffer->filename;
948 CHECK_BUFFER (buffer);
949 return XBUFFER (buffer)->filename;
952 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
953 0, 1, 0,
954 doc: /* Return the base buffer of indirect buffer BUFFER.
955 If BUFFER is not indirect, return nil.
956 BUFFER defaults to the current buffer. */)
957 (buffer)
958 register Lisp_Object buffer;
960 struct buffer *base;
961 Lisp_Object base_buffer;
963 if (NILP (buffer))
964 base = current_buffer->base_buffer;
965 else
967 CHECK_BUFFER (buffer);
968 base = XBUFFER (buffer)->base_buffer;
971 if (! base)
972 return Qnil;
973 XSETBUFFER (base_buffer, base);
974 return base_buffer;
977 DEFUN ("buffer-local-value", Fbuffer_local_value,
978 Sbuffer_local_value, 2, 2, 0,
979 doc: /* Return the value of VARIABLE in BUFFER.
980 If VARIABLE does not have a buffer-local binding in BUFFER, the value
981 is the default binding of the variable. */)
982 (variable, buffer)
983 register Lisp_Object variable;
984 register Lisp_Object buffer;
986 register struct buffer *buf;
987 register Lisp_Object result;
988 struct Lisp_Symbol *sym;
990 CHECK_SYMBOL (variable);
991 CHECK_BUFFER (buffer);
992 buf = XBUFFER (buffer);
994 sym = indirect_variable (XSYMBOL (variable));
995 XSETSYMBOL (variable, sym);
997 /* Look in local_var_list */
998 result = Fassoc (variable, buf->local_var_alist);
999 if (NILP (result))
1001 int offset, idx;
1002 int found = 0;
1004 /* Look in special slots */
1005 /* buffer-local Lisp variables start at `undo_list',
1006 tho only the ones from `name' on are GC'd normally. */
1007 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1008 offset < sizeof (struct buffer);
1009 /* sizeof EMACS_INT == sizeof Lisp_Object */
1010 offset += (sizeof (EMACS_INT)))
1012 idx = PER_BUFFER_IDX (offset);
1013 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1014 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
1015 && EQ (PER_BUFFER_SYMBOL (offset), variable))
1017 result = PER_BUFFER_VALUE (buf, offset);
1018 found = 1;
1019 break;
1023 if (!found)
1024 result = Fdefault_value (variable);
1026 else
1028 Lisp_Object valcontents;
1029 Lisp_Object current_alist_element;
1031 /* What binding is loaded right now? */
1032 valcontents = sym->value;
1033 current_alist_element
1034 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1036 /* The value of the currently loaded binding is not
1037 stored in it, but rather in the realvalue slot.
1038 Store that value into the binding it belongs to
1039 in case that is the one we are about to use. */
1041 Fsetcdr (current_alist_element,
1042 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1044 /* Now get the (perhaps updated) value out of the binding. */
1045 result = XCDR (result);
1048 if (!EQ (result, Qunbound))
1049 return result;
1051 xsignal1 (Qvoid_variable, variable);
1054 /* Return an alist of the Lisp-level buffer-local bindings of
1055 buffer BUF. That is, don't include the variables maintained
1056 in special slots in the buffer object. */
1058 static Lisp_Object
1059 buffer_lisp_local_variables (buf)
1060 struct buffer *buf;
1062 Lisp_Object result = Qnil;
1063 register Lisp_Object tail;
1064 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1066 Lisp_Object val, elt;
1068 elt = XCAR (tail);
1070 /* Reference each variable in the alist in buf.
1071 If inquiring about the current buffer, this gets the current values,
1072 so store them into the alist so the alist is up to date.
1073 If inquiring about some other buffer, this swaps out any values
1074 for that buffer, making the alist up to date automatically. */
1075 val = find_symbol_value (XCAR (elt));
1076 /* Use the current buffer value only if buf is the current buffer. */
1077 if (buf != current_buffer)
1078 val = XCDR (elt);
1080 /* If symbol is unbound, put just the symbol in the list. */
1081 if (EQ (val, Qunbound))
1082 result = Fcons (XCAR (elt), result);
1083 /* Otherwise, put (symbol . value) in the list. */
1084 else
1085 result = Fcons (Fcons (XCAR (elt), val), result);
1088 return result;
1091 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1092 Sbuffer_local_variables, 0, 1, 0,
1093 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1094 Most elements look like (SYMBOL . VALUE), describing one variable.
1095 For a symbol that is locally unbound, just the symbol appears in the value.
1096 Note that storing new VALUEs in these elements doesn't change the variables.
1097 No argument or nil as argument means use current buffer as BUFFER. */)
1098 (buffer)
1099 register Lisp_Object buffer;
1101 register struct buffer *buf;
1102 register Lisp_Object result;
1104 if (NILP (buffer))
1105 buf = current_buffer;
1106 else
1108 CHECK_BUFFER (buffer);
1109 buf = XBUFFER (buffer);
1112 result = buffer_lisp_local_variables (buf);
1114 /* Add on all the variables stored in special slots. */
1116 int offset, idx;
1118 /* buffer-local Lisp variables start at `undo_list',
1119 tho only the ones from `name' on are GC'd normally. */
1120 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1121 offset < sizeof (struct buffer);
1122 /* sizeof EMACS_INT == sizeof Lisp_Object */
1123 offset += (sizeof (EMACS_INT)))
1125 idx = PER_BUFFER_IDX (offset);
1126 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1127 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1128 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1129 PER_BUFFER_VALUE (buf, offset)),
1130 result);
1134 return result;
1137 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1138 0, 1, 0,
1139 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1140 No argument or nil as argument means use current buffer as BUFFER. */)
1141 (buffer)
1142 register Lisp_Object buffer;
1144 register struct buffer *buf;
1145 if (NILP (buffer))
1146 buf = current_buffer;
1147 else
1149 CHECK_BUFFER (buffer);
1150 buf = XBUFFER (buffer);
1153 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1156 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1157 1, 1, 0,
1158 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1159 A non-nil FLAG means mark the buffer modified. */)
1160 (flag)
1161 register Lisp_Object flag;
1163 register int already;
1164 register Lisp_Object fn;
1165 Lisp_Object buffer, window;
1167 #ifdef CLASH_DETECTION
1168 /* If buffer becoming modified, lock the file.
1169 If buffer becoming unmodified, unlock the file. */
1171 fn = current_buffer->file_truename;
1172 /* Test buffer-file-name so that binding it to nil is effective. */
1173 if (!NILP (fn) && ! NILP (current_buffer->filename))
1175 already = SAVE_MODIFF < MODIFF;
1176 if (!already && !NILP (flag))
1177 lock_file (fn);
1178 else if (already && NILP (flag))
1179 unlock_file (fn);
1181 #endif /* CLASH_DETECTION */
1183 /* Here we have a problem. SAVE_MODIFF is used here to encode
1184 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1185 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1186 modify SAVE_MODIFF to affect one, we may affect the other
1187 as well.
1188 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1189 if SAVE_MODIFF<auto_save_modified that means we risk changing
1190 recent-auto-save-p from t to nil.
1191 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1192 we risk changing recent-auto-save-p from nil to t. */
1193 SAVE_MODIFF = (NILP (flag)
1194 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1195 ? MODIFF
1196 /* Let's try to preserve recent-auto-save-p. */
1197 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1198 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1199 we can either decrease SAVE_MODIFF and auto_save_modified
1200 or increase MODIFF. */
1201 : MODIFF++);
1203 /* Set update_mode_lines only if buffer is displayed in some window.
1204 Packages like jit-lock or lazy-lock preserve a buffer's modified
1205 state by recording/restoring the state around blocks of code.
1206 Setting update_mode_lines makes redisplay consider all windows
1207 (on all frames). Stealth fontification of buffers not displayed
1208 would incur additional redisplay costs if we'd set
1209 update_modes_lines unconditionally.
1211 Ideally, I think there should be another mechanism for fontifying
1212 buffers without "modifying" buffers, or redisplay should be
1213 smarter about updating the `*' in mode lines. --gerd */
1214 XSETBUFFER (buffer, current_buffer);
1215 window = Fget_buffer_window (buffer, Qt);
1216 if (WINDOWP (window))
1218 ++update_mode_lines;
1219 current_buffer->prevent_redisplay_optimizations_p = 1;
1222 return flag;
1225 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1226 Srestore_buffer_modified_p, 1, 1, 0,
1227 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1228 It is not ensured that mode lines will be updated to show the modified
1229 state of the current buffer. Use with care. */)
1230 (flag)
1231 Lisp_Object flag;
1233 #ifdef CLASH_DETECTION
1234 Lisp_Object fn;
1236 /* If buffer becoming modified, lock the file.
1237 If buffer becoming unmodified, unlock the file. */
1239 fn = current_buffer->file_truename;
1240 /* Test buffer-file-name so that binding it to nil is effective. */
1241 if (!NILP (fn) && ! NILP (current_buffer->filename))
1243 int already = SAVE_MODIFF < MODIFF;
1244 if (!already && !NILP (flag))
1245 lock_file (fn);
1246 else if (already && NILP (flag))
1247 unlock_file (fn);
1249 #endif /* CLASH_DETECTION */
1251 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1252 return flag;
1255 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1256 0, 1, 0,
1257 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1258 Each buffer has a tick counter which is incremented each time the
1259 text in that buffer is changed. It wraps around occasionally.
1260 No argument or nil as argument means use current buffer as BUFFER. */)
1261 (buffer)
1262 register Lisp_Object buffer;
1264 register struct buffer *buf;
1265 if (NILP (buffer))
1266 buf = current_buffer;
1267 else
1269 CHECK_BUFFER (buffer);
1270 buf = XBUFFER (buffer);
1273 return make_number (BUF_MODIFF (buf));
1276 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1277 Sbuffer_chars_modified_tick, 0, 1, 0,
1278 doc: /* Return BUFFER's character-change tick counter.
1279 Each buffer has a character-change tick counter, which is set to the
1280 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1281 time text in that buffer is inserted or deleted. By comparing the
1282 values returned by two individual calls of `buffer-chars-modified-tick',
1283 you can tell whether a character change occurred in that buffer in
1284 between these calls. No argument or nil as argument means use current
1285 buffer as BUFFER. */)
1286 (buffer)
1287 register Lisp_Object buffer;
1289 register struct buffer *buf;
1290 if (NILP (buffer))
1291 buf = current_buffer;
1292 else
1294 CHECK_BUFFER (buffer);
1295 buf = XBUFFER (buffer);
1298 return make_number (BUF_CHARS_MODIFF (buf));
1301 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1302 "(list (read-string \"Rename buffer (to new name): \" \
1303 nil 'buffer-name-history (buffer-name (current-buffer))) \
1304 current-prefix-arg)",
1305 doc: /* Change current buffer's name to NEWNAME (a string).
1306 If second arg UNIQUE is nil or omitted, it is an error if a
1307 buffer named NEWNAME already exists.
1308 If UNIQUE is non-nil, come up with a new name using
1309 `generate-new-buffer-name'.
1310 Interactively, you can set UNIQUE with a prefix argument.
1311 We return the name we actually gave the buffer.
1312 This does not change the name of the visited file (if any). */)
1313 (newname, unique)
1314 register Lisp_Object newname, unique;
1316 register Lisp_Object tem, buf;
1318 CHECK_STRING (newname);
1320 if (SCHARS (newname) == 0)
1321 error ("Empty string is invalid as a buffer name");
1323 tem = Fget_buffer (newname);
1324 if (!NILP (tem))
1326 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1327 rename the buffer automatically so you can create another
1328 with the original name. It makes UNIQUE equivalent to
1329 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1330 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1331 return current_buffer->name;
1332 if (!NILP (unique))
1333 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1334 else
1335 error ("Buffer name `%s' is in use", SDATA (newname));
1338 current_buffer->name = newname;
1340 /* Catch redisplay's attention. Unless we do this, the mode lines for
1341 any windows displaying current_buffer will stay unchanged. */
1342 update_mode_lines++;
1344 XSETBUFFER (buf, current_buffer);
1345 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1346 if (NILP (current_buffer->filename)
1347 && !NILP (current_buffer->auto_save_file_name))
1348 call0 (intern ("rename-auto-save-file"));
1349 /* Refetch since that last call may have done GC. */
1350 return current_buffer->name;
1353 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1354 doc: /* Return most recently selected buffer other than BUFFER.
1355 Buffers not visible in windows are preferred to visible buffers,
1356 unless optional second argument VISIBLE-OK is non-nil.
1357 If the optional third argument FRAME is non-nil, use that frame's
1358 buffer list instead of the selected frame's buffer list.
1359 If no other buffer exists, the buffer `*scratch*' is returned.
1360 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1361 (buffer, visible_ok, frame)
1362 register Lisp_Object buffer, visible_ok, frame;
1364 Lisp_Object Fset_buffer_major_mode ();
1365 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1366 notsogood = Qnil;
1368 if (NILP (frame))
1369 frame = selected_frame;
1371 tail = Vbuffer_alist;
1372 pred = frame_buffer_predicate (frame);
1374 /* Consider buffers that have been seen in the selected frame
1375 before other buffers. */
1377 tem = frame_buffer_list (frame);
1378 add_ons = Qnil;
1379 while (CONSP (tem))
1381 if (BUFFERP (XCAR (tem)))
1382 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1383 tem = XCDR (tem);
1385 tail = nconc2 (Fnreverse (add_ons), tail);
1387 for (; CONSP (tail); tail = XCDR (tail))
1389 buf = Fcdr (XCAR (tail));
1390 if (EQ (buf, buffer))
1391 continue;
1392 if (NILP (buf))
1393 continue;
1394 if (NILP (XBUFFER (buf)->name))
1395 continue;
1396 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1397 continue;
1398 /* If the selected frame has a buffer_predicate,
1399 disregard buffers that don't fit the predicate. */
1400 if (!NILP (pred))
1402 tem = call1 (pred, buf);
1403 if (NILP (tem))
1404 continue;
1407 if (NILP (visible_ok))
1408 tem = Fget_buffer_window (buf, Qvisible);
1409 else
1410 tem = Qnil;
1411 if (NILP (tem))
1412 return buf;
1413 if (NILP (notsogood))
1414 notsogood = buf;
1416 if (!NILP (notsogood))
1417 return notsogood;
1418 buf = Fget_buffer (build_string ("*scratch*"));
1419 if (NILP (buf))
1421 buf = Fget_buffer_create (build_string ("*scratch*"));
1422 Fset_buffer_major_mode (buf);
1424 return buf;
1427 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1428 0, 1, "",
1429 doc: /* Start keeping undo information for buffer BUFFER.
1430 No argument or nil as argument means do this for the current buffer. */)
1431 (buffer)
1432 register Lisp_Object buffer;
1434 Lisp_Object real_buffer;
1436 if (NILP (buffer))
1437 XSETBUFFER (real_buffer, current_buffer);
1438 else
1440 real_buffer = Fget_buffer (buffer);
1441 if (NILP (real_buffer))
1442 nsberror (buffer);
1445 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1446 XBUFFER (real_buffer)->undo_list = Qnil;
1448 return Qnil;
1452 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1453 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1454 The buffer being killed will be current while the hook is running.\n\
1455 See `kill-buffer'."
1457 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1458 doc: /* Kill buffer BUFFER-OR-NAME.
1459 The argument may be a buffer or the name of an existing buffer.
1460 Argument nil or omitted means kill the current buffer. Return t if the
1461 buffer is actually killed, nil otherwise.
1463 This function calls `replace-buffer-in-windows' for cleaning up all
1464 windows currently displaying the buffer to be killed. The functions in
1465 `kill-buffer-query-functions' are called with the buffer to be killed as
1466 the current buffer. If any of them returns nil, the buffer is not
1467 killed. The hook `kill-buffer-hook' is run before the buffer is
1468 actually killed. The buffer being killed will be current while the hook
1469 is running.
1471 Any processes that have this buffer as the `process-buffer' are killed
1472 with SIGHUP. */)
1473 (buffer_or_name)
1474 Lisp_Object buffer_or_name;
1476 Lisp_Object buffer;
1477 register struct buffer *b;
1478 register Lisp_Object tem;
1479 register struct Lisp_Marker *m;
1480 struct gcpro gcpro1;
1482 if (NILP (buffer_or_name))
1483 buffer = Fcurrent_buffer ();
1484 else
1485 buffer = Fget_buffer (buffer_or_name);
1486 if (NILP (buffer))
1487 nsberror (buffer_or_name);
1489 b = XBUFFER (buffer);
1491 /* Avoid trouble for buffer already dead. */
1492 if (NILP (b->name))
1493 return Qnil;
1495 /* Query if the buffer is still modified. */
1496 if (INTERACTIVE && !NILP (b->filename)
1497 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1499 GCPRO1 (buffer);
1500 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1501 b->name, make_number (0)));
1502 UNGCPRO;
1503 if (NILP (tem))
1504 return Qnil;
1507 /* Run hooks with the buffer to be killed the current buffer. */
1509 int count = SPECPDL_INDEX ();
1510 Lisp_Object arglist[1];
1512 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1513 set_buffer_internal (b);
1515 /* First run the query functions; if any query is answered no,
1516 don't kill the buffer. */
1517 arglist[0] = Qkill_buffer_query_functions;
1518 tem = Frun_hook_with_args_until_failure (1, arglist);
1519 if (NILP (tem))
1520 return unbind_to (count, Qnil);
1522 /* Then run the hooks. */
1523 Frun_hooks (1, &Qkill_buffer_hook);
1524 unbind_to (count, Qnil);
1527 /* We have no more questions to ask. Verify that it is valid
1528 to kill the buffer. This must be done after the questions
1529 since anything can happen within do_yes_or_no_p. */
1531 /* Don't kill the minibuffer now current. */
1532 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1533 return Qnil;
1535 if (NILP (b->name))
1536 return Qnil;
1538 /* When we kill a base buffer, kill all its indirect buffers.
1539 We do it at this stage so nothing terrible happens if they
1540 ask questions or their hooks get errors. */
1541 if (! b->base_buffer)
1543 struct buffer *other;
1545 GCPRO1 (buffer);
1547 for (other = all_buffers; other; other = other->header.next.buffer)
1548 /* all_buffers contains dead buffers too;
1549 don't re-kill them. */
1550 if (other->base_buffer == b && !NILP (other->name))
1552 Lisp_Object buffer;
1553 XSETBUFFER (buffer, other);
1554 Fkill_buffer (buffer);
1557 UNGCPRO;
1560 /* Make this buffer not be current.
1561 In the process, notice if this is the sole visible buffer
1562 and give up if so. */
1563 if (b == current_buffer)
1565 tem = Fother_buffer (buffer, Qnil, Qnil);
1566 Fset_buffer (tem);
1567 if (b == current_buffer)
1568 return Qnil;
1571 /* Notice if the buffer to kill is the sole visible buffer
1572 when we're currently in the mini-buffer, and give up if so. */
1573 XSETBUFFER (tem, current_buffer);
1574 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1576 tem = Fother_buffer (buffer, Qnil, Qnil);
1577 if (EQ (buffer, tem))
1578 return Qnil;
1581 /* Now there is no question: we can kill the buffer. */
1583 #ifdef CLASH_DETECTION
1584 /* Unlock this buffer's file, if it is locked. */
1585 unlock_buffer (b);
1586 #endif /* CLASH_DETECTION */
1588 GCPRO1 (buffer);
1589 kill_buffer_processes (buffer);
1590 UNGCPRO;
1592 /* Killing buffer processes may run sentinels which may
1593 have called kill-buffer. */
1595 if (NILP (b->name))
1596 return Qnil;
1598 clear_charpos_cache (b);
1600 tem = Vinhibit_quit;
1601 Vinhibit_quit = Qt;
1602 replace_buffer_in_all_windows (buffer);
1603 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1604 frames_discard_buffer (buffer);
1605 Vinhibit_quit = tem;
1607 /* Delete any auto-save file, if we saved it in this session.
1608 But not if the buffer is modified. */
1609 if (STRINGP (b->auto_save_file_name)
1610 && BUF_AUTOSAVE_MODIFF (b) != 0
1611 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1612 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1613 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1615 Lisp_Object tem;
1616 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1617 if (! NILP (tem))
1618 internal_delete_file (b->auto_save_file_name);
1621 if (b->base_buffer)
1623 /* Unchain all markers that belong to this indirect buffer.
1624 Don't unchain the markers that belong to the base buffer
1625 or its other indirect buffers. */
1626 for (m = BUF_MARKERS (b); m; )
1628 struct Lisp_Marker *next = m->next;
1629 if (m->buffer == b)
1630 unchain_marker (m);
1631 m = next;
1634 else
1636 /* Unchain all markers of this buffer and its indirect buffers.
1637 and leave them pointing nowhere. */
1638 for (m = BUF_MARKERS (b); m; )
1640 struct Lisp_Marker *next = m->next;
1641 m->buffer = 0;
1642 m->next = NULL;
1643 m = next;
1645 BUF_MARKERS (b) = NULL;
1646 BUF_INTERVALS (b) = NULL_INTERVAL;
1648 /* Perhaps we should explicitly free the interval tree here... */
1651 /* Reset the local variables, so that this buffer's local values
1652 won't be protected from GC. They would be protected
1653 if they happened to remain encached in their symbols.
1654 This gets rid of them for certain. */
1655 swap_out_buffer_local_variables (b);
1656 reset_buffer_local_variables (b, 1);
1658 b->name = Qnil;
1660 BLOCK_INPUT;
1661 if (! b->base_buffer)
1662 free_buffer_text (b);
1664 if (b->newline_cache)
1666 free_region_cache (b->newline_cache);
1667 b->newline_cache = 0;
1669 if (b->width_run_cache)
1671 free_region_cache (b->width_run_cache);
1672 b->width_run_cache = 0;
1674 b->width_table = Qnil;
1675 UNBLOCK_INPUT;
1676 b->undo_list = Qnil;
1678 return Qt;
1681 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1682 we do this each time BUF is selected visibly, the more recently
1683 selected buffers are always closer to the front of the list. This
1684 means that other_buffer is more likely to choose a relevant buffer. */
1686 void
1687 record_buffer (buf)
1688 Lisp_Object buf;
1690 register Lisp_Object link, prev;
1691 Lisp_Object frame;
1692 frame = selected_frame;
1694 prev = Qnil;
1695 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1697 if (EQ (XCDR (XCAR (link)), buf))
1698 break;
1699 prev = link;
1702 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1703 we cannot use Fdelq itself here because it allows quitting. */
1705 if (NILP (prev))
1706 Vbuffer_alist = XCDR (Vbuffer_alist);
1707 else
1708 XSETCDR (prev, XCDR (XCDR (prev)));
1710 XSETCDR (link, Vbuffer_alist);
1711 Vbuffer_alist = link;
1713 /* Effectively do a delq on buried_buffer_list. */
1715 prev = Qnil;
1716 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1717 link = XCDR (link))
1719 if (EQ (XCAR (link), buf))
1721 if (NILP (prev))
1722 XFRAME (frame)->buried_buffer_list = XCDR (link);
1723 else
1724 XSETCDR (prev, XCDR (XCDR (prev)));
1725 break;
1727 prev = link;
1730 /* Now move this buffer to the front of frame_buffer_list also. */
1732 prev = Qnil;
1733 for (link = frame_buffer_list (frame); CONSP (link);
1734 link = XCDR (link))
1736 if (EQ (XCAR (link), buf))
1737 break;
1738 prev = link;
1741 /* Effectively do delq. */
1743 if (CONSP (link))
1745 if (NILP (prev))
1746 set_frame_buffer_list (frame,
1747 XCDR (frame_buffer_list (frame)));
1748 else
1749 XSETCDR (prev, XCDR (XCDR (prev)));
1751 XSETCDR (link, frame_buffer_list (frame));
1752 set_frame_buffer_list (frame, link);
1754 else
1755 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1758 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1759 doc: /* Set an appropriate major mode for BUFFER.
1760 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1761 according to `default-major-mode'.
1762 Use this function before selecting the buffer, since it may need to inspect
1763 the current buffer's major mode. */)
1764 (buffer)
1765 Lisp_Object buffer;
1767 int count;
1768 Lisp_Object function;
1770 CHECK_BUFFER (buffer);
1772 if (STRINGP (XBUFFER (buffer)->name)
1773 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1774 function = find_symbol_value (intern ("initial-major-mode"));
1775 else
1777 function = buffer_defaults.major_mode;
1778 if (NILP (function)
1779 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1780 function = current_buffer->major_mode;
1783 if (NILP (function) || EQ (function, Qfundamental_mode))
1784 return Qnil;
1786 count = SPECPDL_INDEX ();
1788 /* To select a nonfundamental mode,
1789 select the buffer temporarily and then call the mode function. */
1791 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1793 Fset_buffer (buffer);
1794 call0 (function);
1796 return unbind_to (count, Qnil);
1799 /* Switch to buffer BUFFER in the selected window.
1800 If NORECORD is non-nil, don't call record_buffer. */
1802 Lisp_Object
1803 switch_to_buffer_1 (buffer_or_name, norecord)
1804 Lisp_Object buffer_or_name, norecord;
1806 register Lisp_Object buffer;
1808 if (NILP (buffer_or_name))
1809 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1810 else
1812 buffer = Fget_buffer (buffer_or_name);
1813 if (NILP (buffer))
1815 buffer = Fget_buffer_create (buffer_or_name);
1816 Fset_buffer_major_mode (buffer);
1819 Fset_buffer (buffer);
1820 if (NILP (norecord))
1821 record_buffer (buffer);
1823 Fset_window_buffer (EQ (selected_window, minibuf_window)
1824 ? Fnext_window (minibuf_window, Qnil, Qnil)
1825 : selected_window,
1826 buffer, Qnil);
1828 return buffer;
1831 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1832 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1833 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1834 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1835 nil. Return the buffer switched to.
1837 If BUFFER-OR-NAME is a string and does not identify an existing
1838 buffer, create a new buffer with that name. Interactively, if
1839 `confirm-nonexistent-file-or-buffer' is non-nil, request
1840 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1841 nil, switch to buffer returned by `other-buffer'.
1843 Optional second arg NORECORD non-nil means do not put this buffer
1844 at the front of the list of recently selected ones. This
1845 function returns the buffer it switched to as a Lisp object.
1847 If the selected window is the minibuffer window or dedicated to
1848 its buffer, use `pop-to-buffer' for displaying the buffer.
1850 WARNING: This is NOT the way to work on another buffer temporarily
1851 within a Lisp program! Use `set-buffer' instead. That avoids
1852 messing with the window-buffer correspondences. */)
1853 (buffer_or_name, norecord)
1854 Lisp_Object buffer_or_name, norecord;
1856 char *err;
1858 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1860 /* Basically a NOP. Avoid signalling an error in the case where
1861 the selected window is dedicated, or a minibuffer. */
1863 /* But do put this buffer at the front of the buffer list, unless
1864 that has been inhibited. Note that even if BUFFER-OR-NAME is
1865 at the front of the main buffer-list already, we still want to
1866 move it to the front of the frame's buffer list. */
1867 if (NILP (norecord))
1868 record_buffer (buffer_or_name);
1869 return Fset_buffer (buffer_or_name);
1871 else if (EQ (minibuf_window, selected_window)
1872 /* If `dedicated' is neither nil nor t, it means it's
1873 dedicatedness can be overridden by an explicit request
1874 such as a call to switch-to-buffer. */
1875 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1876 /* We can't use the selected window so let `pop-to-buffer' try some
1877 other window. */
1878 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1879 else
1880 return switch_to_buffer_1 (buffer_or_name, norecord);
1883 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1884 doc: /* Return the current buffer as a Lisp object. */)
1887 register Lisp_Object buf;
1888 XSETBUFFER (buf, current_buffer);
1889 return buf;
1892 /* Set the current buffer to B.
1894 We previously set windows_or_buffers_changed here to invalidate
1895 global unchanged information in beg_unchanged and end_unchanged.
1896 This is no longer necessary because we now compute unchanged
1897 information on a buffer-basis. Every action affecting other
1898 windows than the selected one requires a select_window at some
1899 time, and that increments windows_or_buffers_changed. */
1901 void
1902 set_buffer_internal (b)
1903 register struct buffer *b;
1905 if (current_buffer != b)
1906 set_buffer_internal_1 (b);
1909 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1910 This is used by redisplay. */
1912 void
1913 set_buffer_internal_1 (b)
1914 register struct buffer *b;
1916 register struct buffer *old_buf;
1917 register Lisp_Object tail, valcontents;
1918 Lisp_Object tem;
1920 #ifdef USE_MMAP_FOR_BUFFERS
1921 if (b->text->beg == NULL)
1922 enlarge_buffer_text (b, 0);
1923 #endif /* USE_MMAP_FOR_BUFFERS */
1925 if (current_buffer == b)
1926 return;
1928 old_buf = current_buffer;
1929 current_buffer = b;
1930 last_known_column_point = -1; /* invalidate indentation cache */
1932 if (old_buf)
1934 /* Put the undo list back in the base buffer, so that it appears
1935 that an indirect buffer shares the undo list of its base. */
1936 if (old_buf->base_buffer)
1937 old_buf->base_buffer->undo_list = old_buf->undo_list;
1939 /* If the old current buffer has markers to record PT, BEGV and ZV
1940 when it is not current, update them now. */
1941 record_buffer_markers (old_buf);
1944 /* Get the undo list from the base buffer, so that it appears
1945 that an indirect buffer shares the undo list of its base. */
1946 if (b->base_buffer)
1947 b->undo_list = b->base_buffer->undo_list;
1949 /* If the new current buffer has markers to record PT, BEGV and ZV
1950 when it is not current, fetch them now. */
1951 fetch_buffer_markers (b);
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 if (CONSP (XCAR (tail))
1959 && SYMBOLP (XCAR (XCAR (tail)))
1960 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
1961 (BUFFER_LOCAL_VALUEP (valcontents)))
1962 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1963 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1964 /* Just reference the variable to cause it to become set for
1965 this buffer. */
1966 Fsymbol_value (XCAR (XCAR (tail)));
1969 /* Do the same with any others that were local to the previous buffer */
1971 if (old_buf)
1972 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1974 if (CONSP (tail)
1975 && SYMBOLP (XCAR (XCAR (tail)))
1976 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
1977 (BUFFER_LOCAL_VALUEP (valcontents)))
1978 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1979 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1980 /* Just reference the variable to cause it to become set for
1981 this buffer. */
1982 Fsymbol_value (XCAR (XCAR (tail)));
1986 /* Switch to buffer B temporarily for redisplay purposes.
1987 This avoids certain things that don't need to be done within redisplay. */
1989 void
1990 set_buffer_temp (b)
1991 struct buffer *b;
1993 register struct buffer *old_buf;
1995 if (current_buffer == b)
1996 return;
1998 old_buf = current_buffer;
1999 current_buffer = b;
2001 /* If the old current buffer has markers to record PT, BEGV and ZV
2002 when it is not current, update them now. */
2003 record_buffer_markers (old_buf);
2005 /* If the new current buffer has markers to record PT, BEGV and ZV
2006 when it is not current, fetch them now. */
2007 fetch_buffer_markers (b);
2010 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2011 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
2012 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
2013 also `save-excursion' when you want to make a buffer current
2014 temporarily. This function does not display the buffer, so its effect
2015 ends when the current command terminates. Use `switch-to-buffer' or
2016 `pop-to-buffer' to switch buffers permanently. */)
2017 (buffer_or_name)
2018 register Lisp_Object buffer_or_name;
2020 register Lisp_Object buffer;
2021 buffer = Fget_buffer (buffer_or_name);
2022 if (NILP (buffer))
2023 nsberror (buffer_or_name);
2024 if (NILP (XBUFFER (buffer)->name))
2025 error ("Selecting deleted buffer");
2026 set_buffer_internal (XBUFFER (buffer));
2027 return buffer;
2030 /* Set the current buffer to BUFFER provided it is alive. */
2032 Lisp_Object
2033 set_buffer_if_live (buffer)
2034 Lisp_Object buffer;
2036 if (! NILP (XBUFFER (buffer)->name))
2037 Fset_buffer (buffer);
2038 return Qnil;
2041 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2042 Sbarf_if_buffer_read_only, 0, 0, 0,
2043 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2046 if (!NILP (current_buffer->read_only)
2047 && NILP (Vinhibit_read_only))
2048 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2049 return Qnil;
2052 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2053 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
2054 There it is the least likely candidate for `other-buffer' to return;
2055 thus, the least likely buffer for \\[switch-to-buffer] to select by
2056 default.
2058 The argument may be a buffer name or an actual buffer object. If
2059 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2060 from the selected window if it is displayed there. If the selected
2061 window is dedicated to its buffer, delete that window if there are other
2062 windows on the same frame. If the selected window is the only window on
2063 its frame, iconify that frame. */)
2064 (buffer_or_name)
2065 register Lisp_Object buffer_or_name;
2067 Lisp_Object buffer;
2069 /* Figure out what buffer we're going to bury. */
2070 if (NILP (buffer_or_name))
2072 Lisp_Object tem;
2073 XSETBUFFER (buffer, current_buffer);
2075 tem = Fwindow_buffer (selected_window);
2076 /* If we're burying the current buffer, unshow it. */
2077 if (EQ (buffer, tem))
2079 if (NILP (Fwindow_dedicated_p (selected_window)))
2080 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2081 else if (NILP (XWINDOW (selected_window)->parent))
2082 Ficonify_frame (Fwindow_frame (selected_window));
2083 else
2084 Fdelete_window (selected_window);
2087 else
2089 buffer = Fget_buffer (buffer_or_name);
2090 if (NILP (buffer))
2091 nsberror (buffer_or_name);
2094 /* Move buffer to the end of the buffer list. Do nothing if the
2095 buffer is killed. */
2096 if (!NILP (XBUFFER (buffer)->name))
2098 Lisp_Object aelt, link;
2100 aelt = Frassq (buffer, Vbuffer_alist);
2101 link = Fmemq (aelt, Vbuffer_alist);
2102 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2103 XSETCDR (link, Qnil);
2104 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2106 XFRAME (selected_frame)->buffer_list
2107 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2108 XFRAME (selected_frame)->buried_buffer_list
2109 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2112 return Qnil;
2115 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2116 doc: /* Delete the entire contents of the current buffer.
2117 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2118 so the buffer is truly empty after this. */)
2121 Fwiden ();
2123 del_range (BEG, Z);
2125 current_buffer->last_window_start = 1;
2126 /* Prevent warnings, or suspension of auto saving, that would happen
2127 if future size is less than past size. Use of erase-buffer
2128 implies that the future text is not really related to the past text. */
2129 XSETFASTINT (current_buffer->save_length, 0);
2130 return Qnil;
2133 void
2134 validate_region (b, e)
2135 register Lisp_Object *b, *e;
2137 CHECK_NUMBER_COERCE_MARKER (*b);
2138 CHECK_NUMBER_COERCE_MARKER (*e);
2140 if (XINT (*b) > XINT (*e))
2142 Lisp_Object tem;
2143 tem = *b; *b = *e; *e = tem;
2146 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2147 && XINT (*e) <= ZV))
2148 args_out_of_range (*b, *e);
2151 /* Advance BYTE_POS up to a character boundary
2152 and return the adjusted position. */
2154 static int
2155 advance_to_char_boundary (byte_pos)
2156 int byte_pos;
2158 int c;
2160 if (byte_pos == BEG)
2161 /* Beginning of buffer is always a character boundary. */
2162 return BEG;
2164 c = FETCH_BYTE (byte_pos);
2165 if (! CHAR_HEAD_P (c))
2167 /* We should advance BYTE_POS only when C is a constituent of a
2168 multibyte sequence. */
2169 int orig_byte_pos = byte_pos;
2173 byte_pos--;
2174 c = FETCH_BYTE (byte_pos);
2176 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2177 INC_POS (byte_pos);
2178 if (byte_pos < orig_byte_pos)
2179 byte_pos = orig_byte_pos;
2180 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2181 surely advance to the correct character boundary. If C is
2182 not, BYTE_POS was unchanged. */
2185 return byte_pos;
2188 #ifdef REL_ALLOC
2189 extern void r_alloc_reset_variable P_ ((POINTER_TYPE *, POINTER_TYPE *));
2190 #endif /* REL_ALLOC */
2192 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2193 1, 1, 0,
2194 doc: /* Swap the text between current buffer and BUFFER. */)
2195 (buffer)
2196 Lisp_Object buffer;
2198 struct buffer *other_buffer;
2199 CHECK_BUFFER (buffer);
2200 other_buffer = XBUFFER (buffer);
2202 if (NILP (other_buffer->name))
2203 error ("Cannot swap a dead buffer's text");
2205 /* Actually, it probably works just fine.
2206 * if (other_buffer == current_buffer)
2207 * error ("Cannot swap a buffer's text with itself"); */
2209 /* Actually, this may be workable as well, tho probably only if they're
2210 *both* indirect. */
2211 if (other_buffer->base_buffer
2212 || current_buffer->base_buffer)
2213 error ("Cannot swap indirect buffers's text");
2215 { /* This is probably harder to make work. */
2216 struct buffer *other;
2217 for (other = all_buffers; other; other = other->header.next.buffer)
2218 if (other->base_buffer == other_buffer
2219 || other->base_buffer == current_buffer)
2220 error ("One of the buffers to swap has indirect buffers");
2223 #define swapfield(field, type) \
2224 do { \
2225 type tmp##field = other_buffer->field; \
2226 other_buffer->field = current_buffer->field; \
2227 current_buffer->field = tmp##field; \
2228 } while (0)
2230 swapfield (own_text, struct buffer_text);
2231 eassert (current_buffer->text == &current_buffer->own_text);
2232 eassert (other_buffer->text == &other_buffer->own_text);
2233 #ifdef REL_ALLOC
2234 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2235 (POINTER_TYPE **) &other_buffer->own_text.beg);
2236 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2237 (POINTER_TYPE **) &current_buffer->own_text.beg);
2238 #endif /* REL_ALLOC */
2240 swapfield (pt, EMACS_INT);
2241 swapfield (pt_byte, EMACS_INT);
2242 swapfield (begv, EMACS_INT);
2243 swapfield (begv_byte, EMACS_INT);
2244 swapfield (zv, EMACS_INT);
2245 swapfield (zv_byte, EMACS_INT);
2246 eassert (!current_buffer->base_buffer);
2247 eassert (!other_buffer->base_buffer);
2248 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2249 swapfield (newline_cache, struct region_cache *);
2250 swapfield (width_run_cache, struct region_cache *);
2251 current_buffer->prevent_redisplay_optimizations_p = 1;
2252 other_buffer->prevent_redisplay_optimizations_p = 1;
2253 swapfield (overlays_before, struct Lisp_Overlay *);
2254 swapfield (overlays_after, struct Lisp_Overlay *);
2255 swapfield (overlay_center, EMACS_INT);
2256 swapfield (undo_list, Lisp_Object);
2257 swapfield (mark, Lisp_Object);
2258 swapfield (enable_multibyte_characters, Lisp_Object);
2259 /* FIXME: Not sure what we should do with these *_marker fields.
2260 Hopefully they're just nil anyway. */
2261 swapfield (pt_marker, Lisp_Object);
2262 swapfield (begv_marker, Lisp_Object);
2263 swapfield (zv_marker, Lisp_Object);
2264 current_buffer->point_before_scroll = Qnil;
2265 other_buffer->point_before_scroll = Qnil;
2267 current_buffer->text->modiff++; other_buffer->text->modiff++;
2268 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2269 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2270 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2271 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2272 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2273 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2275 struct Lisp_Marker *m;
2276 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2277 if (m->buffer == other_buffer)
2278 m->buffer = current_buffer;
2279 else
2280 /* Since there's no indirect buffer in sight, markers on
2281 BUF_MARKERS(buf) should either be for `buf' or dead. */
2282 eassert (!m->buffer);
2283 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2284 if (m->buffer == current_buffer)
2285 m->buffer = other_buffer;
2286 else
2287 /* Since there's no indirect buffer in sight, markers on
2288 BUF_MARKERS(buf) should either be for `buf' or dead. */
2289 eassert (!m->buffer);
2291 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2292 So since we just swapped the markers between the two buffers, we need
2293 to undo the effect of this swap for window markers. */
2294 Lisp_Object w = Fselected_window (), ws = Qnil;
2295 Lisp_Object buf1, buf2;
2296 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2298 while (NILP (Fmemq (w, ws)))
2300 ws = Fcons (w, ws);
2301 if (MARKERP (XWINDOW (w)->pointm)
2302 && (EQ (XWINDOW (w)->buffer, buf1)
2303 || EQ (XWINDOW (w)->buffer, buf2)))
2304 Fset_marker (XWINDOW (w)->pointm,
2305 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2306 XWINDOW (w)->buffer);
2307 w = Fnext_window (w, Qt, Qt);
2311 if (current_buffer->text->intervals)
2312 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2313 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2314 if (other_buffer->text->intervals)
2315 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2316 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2318 return Qnil;
2321 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2322 1, 1, 0,
2323 doc: /* Set the multibyte flag of the current buffer to FLAG.
2324 If FLAG is t, this makes the buffer a multibyte buffer.
2325 If FLAG is nil, this makes the buffer a single-byte buffer.
2326 In these cases, the buffer contents remain unchanged as a sequence of
2327 bytes but the contents viewed as characters do change.
2328 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2329 all eight-bit bytes to eight-bit characters.
2330 If the multibyte flag was really changed, undo information of the
2331 current buffer is cleared. */)
2332 (flag)
2333 Lisp_Object flag;
2335 struct Lisp_Marker *tail, *markers;
2336 struct buffer *other;
2337 int begv, zv;
2338 int narrowed = (BEG != BEGV || Z != ZV);
2339 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2340 Lisp_Object old_undo = current_buffer->undo_list;
2341 struct gcpro gcpro1;
2343 if (current_buffer->base_buffer)
2344 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2346 /* Do nothing if nothing actually changes. */
2347 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2348 return flag;
2350 GCPRO1 (old_undo);
2352 /* Don't record these buffer changes. We will put a special undo entry
2353 instead. */
2354 current_buffer->undo_list = Qt;
2356 /* If the cached position is for this buffer, clear it out. */
2357 clear_charpos_cache (current_buffer);
2359 if (NILP (flag))
2360 begv = BEGV_BYTE, zv = ZV_BYTE;
2361 else
2362 begv = BEGV, zv = ZV;
2364 if (narrowed)
2365 Fwiden ();
2367 if (NILP (flag))
2369 int pos, stop;
2370 unsigned char *p;
2372 /* Do this first, so it can use CHAR_TO_BYTE
2373 to calculate the old correspondences. */
2374 set_intervals_multibyte (0);
2376 current_buffer->enable_multibyte_characters = Qnil;
2378 Z = Z_BYTE;
2379 BEGV = BEGV_BYTE;
2380 ZV = ZV_BYTE;
2381 GPT = GPT_BYTE;
2382 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2385 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2386 tail->charpos = tail->bytepos;
2388 /* Convert multibyte form of 8-bit characters to unibyte. */
2389 pos = BEG;
2390 stop = GPT;
2391 p = BEG_ADDR;
2392 while (1)
2394 int c, bytes;
2396 if (pos == stop)
2398 if (pos == Z)
2399 break;
2400 p = GAP_END_ADDR;
2401 stop = Z;
2403 if (ASCII_BYTE_P (*p))
2404 p++, pos++;
2405 else if (CHAR_BYTE8_HEAD_P (*p))
2407 c = STRING_CHAR_AND_LENGTH (p, bytes);
2408 /* Delete all bytes for this 8-bit character but the
2409 last one, and change the last one to the character
2410 code. */
2411 bytes--;
2412 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2413 p = GAP_END_ADDR;
2414 *p++ = c;
2415 pos++;
2416 if (begv > pos)
2417 begv -= bytes;
2418 if (zv > pos)
2419 zv -= bytes;
2420 stop = Z;
2422 else
2424 bytes = BYTES_BY_CHAR_HEAD (*p);
2425 p += bytes, pos += bytes;
2428 if (narrowed)
2429 Fnarrow_to_region (make_number (begv), make_number (zv));
2431 else
2433 int pt = PT;
2434 int pos, stop;
2435 unsigned char *p, *pend;
2437 /* Be sure not to have a multibyte sequence striding over the GAP.
2438 Ex: We change this: "...abc\302 _GAP_ \241def..."
2439 to: "...abc _GAP_ \302\241def..." */
2441 if (EQ (flag, Qt)
2442 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2443 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2445 unsigned char *p = GPT_ADDR - 1;
2447 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2448 if (BASE_LEADING_CODE_P (*p))
2450 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2452 move_gap_both (new_gpt, new_gpt);
2456 /* Make the buffer contents valid as multibyte by converting
2457 8-bit characters to multibyte form. */
2458 pos = BEG;
2459 stop = GPT;
2460 p = BEG_ADDR;
2461 pend = GPT_ADDR;
2462 while (1)
2464 int bytes;
2466 if (pos == stop)
2468 if (pos == Z)
2469 break;
2470 p = GAP_END_ADDR;
2471 pend = Z_ADDR;
2472 stop = Z;
2475 if (ASCII_BYTE_P (*p))
2476 p++, pos++;
2477 else if (EQ (flag, Qt)
2478 && ! CHAR_BYTE8_HEAD_P (*p)
2479 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2480 p += bytes, pos += bytes;
2481 else
2483 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2484 int c;
2486 c = BYTE8_TO_CHAR (*p);
2487 bytes = CHAR_STRING (c, tmp);
2488 *p = tmp[0];
2489 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2490 bytes--;
2491 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2492 /* Now the gap is after the just inserted data. */
2493 pos = GPT;
2494 p = GAP_END_ADDR;
2495 if (pos <= begv)
2496 begv += bytes;
2497 if (pos <= zv)
2498 zv += bytes;
2499 if (pos <= pt)
2500 pt += bytes;
2501 pend = Z_ADDR;
2502 stop = Z;
2506 if (pt != PT)
2507 TEMP_SET_PT (pt);
2509 if (narrowed)
2510 Fnarrow_to_region (make_number (begv), make_number (zv));
2512 /* Do this first, so that chars_in_text asks the right question.
2513 set_intervals_multibyte needs it too. */
2514 current_buffer->enable_multibyte_characters = Qt;
2516 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2517 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2519 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2521 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2522 if (BEGV_BYTE > GPT_BYTE)
2523 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2524 else
2525 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2527 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2528 if (ZV_BYTE > GPT_BYTE)
2529 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2530 else
2531 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2534 int pt_byte = advance_to_char_boundary (PT_BYTE);
2535 int pt;
2537 if (pt_byte > GPT_BYTE)
2538 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2539 else
2540 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2541 TEMP_SET_PT_BOTH (pt, pt_byte);
2544 tail = markers = BUF_MARKERS (current_buffer);
2546 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2547 getting confused by the markers that have not yet been updated.
2548 It is also a signal that it should never create a marker. */
2549 BUF_MARKERS (current_buffer) = NULL;
2551 for (; tail; tail = tail->next)
2553 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2554 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2557 /* Make sure no markers were put on the chain
2558 while the chain value was incorrect. */
2559 if (BUF_MARKERS (current_buffer))
2560 abort ();
2562 BUF_MARKERS (current_buffer) = markers;
2564 /* Do this last, so it can calculate the new correspondences
2565 between chars and bytes. */
2566 set_intervals_multibyte (1);
2569 if (!EQ (old_undo, Qt))
2571 /* Represent all the above changes by a special undo entry. */
2572 extern Lisp_Object Qapply;
2573 current_buffer->undo_list = Fcons (list3 (Qapply,
2574 intern ("set-buffer-multibyte"),
2575 NILP (flag) ? Qt : Qnil),
2576 old_undo);
2579 UNGCPRO;
2581 /* Changing the multibyteness of a buffer means that all windows
2582 showing that buffer must be updated thoroughly. */
2583 current_buffer->prevent_redisplay_optimizations_p = 1;
2584 ++windows_or_buffers_changed;
2586 /* Copy this buffer's new multibyte status
2587 into all of its indirect buffers. */
2588 for (other = all_buffers; other; other = other->header.next.buffer)
2589 if (other->base_buffer == current_buffer && !NILP (other->name))
2591 other->enable_multibyte_characters
2592 = current_buffer->enable_multibyte_characters;
2593 other->prevent_redisplay_optimizations_p = 1;
2596 /* Restore the modifiedness of the buffer. */
2597 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2598 Fset_buffer_modified_p (Qnil);
2600 #ifdef subprocesses
2601 /* Update coding systems of this buffer's process (if any). */
2603 Lisp_Object process;
2605 process = Fget_buffer_process (Fcurrent_buffer ());
2606 if (PROCESSP (process))
2607 setup_process_coding_systems (process);
2609 #endif /* subprocesses */
2611 return flag;
2614 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2615 0, 0, 0,
2616 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2617 Most local variable bindings are eliminated so that the default values
2618 become effective once more. Also, the syntax table is set from
2619 `standard-syntax-table', the local keymap is set to nil,
2620 and the abbrev table from `fundamental-mode-abbrev-table'.
2621 This function also forces redisplay of the mode line.
2623 Every function to select a new major mode starts by
2624 calling this function.
2626 As a special exception, local variables whose names have
2627 a non-nil `permanent-local' property are not eliminated by this function.
2629 The first thing this function does is run
2630 the normal hook `change-major-mode-hook'. */)
2633 if (!NILP (Vrun_hooks))
2634 call1 (Vrun_hooks, Qchange_major_mode_hook);
2636 /* Make sure none of the bindings in local_var_alist
2637 remain swapped in, in their symbols. */
2639 swap_out_buffer_local_variables (current_buffer);
2641 /* Actually eliminate all local bindings of this buffer. */
2643 reset_buffer_local_variables (current_buffer, 0);
2645 /* Force mode-line redisplay. Useful here because all major mode
2646 commands call this function. */
2647 update_mode_lines++;
2649 return Qnil;
2652 /* Make sure no local variables remain set up with buffer B
2653 for their current values. */
2655 static void
2656 swap_out_buffer_local_variables (b)
2657 struct buffer *b;
2659 Lisp_Object oalist, alist, sym, buffer;
2661 XSETBUFFER (buffer, b);
2662 oalist = b->local_var_alist;
2664 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2666 if (CONSP (XCAR (alist))
2667 && (sym = XCAR (XCAR (alist)), SYMBOLP (sym))
2668 /* Need not do anything if some other buffer's binding is
2669 now encached. */
2670 && EQ (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer,
2671 buffer))
2673 /* Symbol is set up for this buffer's old local value:
2674 swap it out! */
2675 swap_in_global_binding (sym);
2680 /* Find all the overlays in the current buffer that contain position POS.
2681 Return the number found, and store them in a vector in *VEC_PTR.
2682 Store in *LEN_PTR the size allocated for the vector.
2683 Store in *NEXT_PTR the next position after POS where an overlay starts,
2684 or ZV if there are no more overlays between POS and ZV.
2685 Store in *PREV_PTR the previous position before POS where an overlay ends,
2686 or where an overlay starts which ends at or after POS;
2687 or BEGV if there are no such overlays from BEGV to POS.
2688 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2690 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2691 when this function is called.
2693 If EXTEND is non-zero, we make the vector bigger if necessary.
2694 If EXTEND is zero, we never extend the vector,
2695 and we store only as many overlays as will fit.
2696 But we still return the total number of overlays.
2698 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2699 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2700 default (BEGV or ZV). */
2703 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2704 EMACS_INT pos;
2705 int extend;
2706 Lisp_Object **vec_ptr;
2707 int *len_ptr;
2708 EMACS_INT *next_ptr;
2709 EMACS_INT *prev_ptr;
2710 int change_req;
2712 Lisp_Object overlay, start, end;
2713 struct Lisp_Overlay *tail;
2714 int idx = 0;
2715 int len = *len_ptr;
2716 Lisp_Object *vec = *vec_ptr;
2717 int next = ZV;
2718 int prev = BEGV;
2719 int inhibit_storing = 0;
2721 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2723 int startpos, endpos;
2725 XSETMISC (overlay, tail);
2727 start = OVERLAY_START (overlay);
2728 end = OVERLAY_END (overlay);
2729 endpos = OVERLAY_POSITION (end);
2730 if (endpos < pos)
2732 if (prev < endpos)
2733 prev = endpos;
2734 break;
2736 startpos = OVERLAY_POSITION (start);
2737 /* This one ends at or after POS
2738 so its start counts for PREV_PTR if it's before POS. */
2739 if (prev < startpos && startpos < pos)
2740 prev = startpos;
2741 if (endpos == pos)
2742 continue;
2743 if (startpos <= pos)
2745 if (idx == len)
2747 /* The supplied vector is full.
2748 Either make it bigger, or don't store any more in it. */
2749 if (extend)
2751 /* Make it work with an initial len == 0. */
2752 len *= 2;
2753 if (len == 0)
2754 len = 4;
2755 *len_ptr = len;
2756 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2757 *vec_ptr = vec;
2759 else
2760 inhibit_storing = 1;
2763 if (!inhibit_storing)
2764 vec[idx] = overlay;
2765 /* Keep counting overlays even if we can't return them all. */
2766 idx++;
2768 else if (startpos < next)
2769 next = startpos;
2772 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2774 int startpos, endpos;
2776 XSETMISC (overlay, tail);
2778 start = OVERLAY_START (overlay);
2779 end = OVERLAY_END (overlay);
2780 startpos = OVERLAY_POSITION (start);
2781 if (pos < startpos)
2783 if (startpos < next)
2784 next = startpos;
2785 break;
2787 endpos = OVERLAY_POSITION (end);
2788 if (pos < endpos)
2790 if (idx == len)
2792 if (extend)
2794 /* Make it work with an initial len == 0. */
2795 len *= 2;
2796 if (len == 0)
2797 len = 4;
2798 *len_ptr = len;
2799 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2800 *vec_ptr = vec;
2802 else
2803 inhibit_storing = 1;
2806 if (!inhibit_storing)
2807 vec[idx] = overlay;
2808 idx++;
2810 if (startpos < pos && startpos > prev)
2811 prev = startpos;
2813 else if (endpos < pos && endpos > prev)
2814 prev = endpos;
2815 else if (endpos == pos && startpos > prev
2816 && (!change_req || startpos < pos))
2817 prev = startpos;
2820 if (next_ptr)
2821 *next_ptr = next;
2822 if (prev_ptr)
2823 *prev_ptr = prev;
2824 return idx;
2827 /* Find all the overlays in the current buffer that overlap the range
2828 BEG-END, or are empty at BEG, or are empty at END provided END
2829 denotes the position at the end of the current buffer.
2831 Return the number found, and store them in a vector in *VEC_PTR.
2832 Store in *LEN_PTR the size allocated for the vector.
2833 Store in *NEXT_PTR the next position after POS where an overlay starts,
2834 or ZV if there are no more overlays.
2835 Store in *PREV_PTR the previous position before POS where an overlay ends,
2836 or BEGV if there are no previous overlays.
2837 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2839 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2840 when this function is called.
2842 If EXTEND is non-zero, we make the vector bigger if necessary.
2843 If EXTEND is zero, we never extend the vector,
2844 and we store only as many overlays as will fit.
2845 But we still return the total number of overlays. */
2847 static int
2848 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2849 int beg, end;
2850 int extend;
2851 Lisp_Object **vec_ptr;
2852 int *len_ptr;
2853 int *next_ptr;
2854 int *prev_ptr;
2856 Lisp_Object overlay, ostart, oend;
2857 struct Lisp_Overlay *tail;
2858 int idx = 0;
2859 int len = *len_ptr;
2860 Lisp_Object *vec = *vec_ptr;
2861 int next = ZV;
2862 int prev = BEGV;
2863 int inhibit_storing = 0;
2864 int end_is_Z = end == Z;
2866 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2868 int startpos, endpos;
2870 XSETMISC (overlay, tail);
2872 ostart = OVERLAY_START (overlay);
2873 oend = OVERLAY_END (overlay);
2874 endpos = OVERLAY_POSITION (oend);
2875 if (endpos < beg)
2877 if (prev < endpos)
2878 prev = endpos;
2879 break;
2881 startpos = OVERLAY_POSITION (ostart);
2882 /* Count an interval if it overlaps the range, is empty at the
2883 start of the range, or is empty at END provided END denotes the
2884 end of the buffer. */
2885 if ((beg < endpos && startpos < end)
2886 || (startpos == endpos
2887 && (beg == endpos || (end_is_Z && endpos == end))))
2889 if (idx == len)
2891 /* The supplied vector is full.
2892 Either make it bigger, or don't store any more in it. */
2893 if (extend)
2895 /* Make it work with an initial len == 0. */
2896 len *= 2;
2897 if (len == 0)
2898 len = 4;
2899 *len_ptr = len;
2900 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2901 *vec_ptr = vec;
2903 else
2904 inhibit_storing = 1;
2907 if (!inhibit_storing)
2908 vec[idx] = overlay;
2909 /* Keep counting overlays even if we can't return them all. */
2910 idx++;
2912 else if (startpos < next)
2913 next = startpos;
2916 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2918 int startpos, endpos;
2920 XSETMISC (overlay, tail);
2922 ostart = OVERLAY_START (overlay);
2923 oend = OVERLAY_END (overlay);
2924 startpos = OVERLAY_POSITION (ostart);
2925 if (end < startpos)
2927 if (startpos < next)
2928 next = startpos;
2929 break;
2931 endpos = OVERLAY_POSITION (oend);
2932 /* Count an interval if it overlaps the range, is empty at the
2933 start of the range, or is empty at END provided END denotes the
2934 end of the buffer. */
2935 if ((beg < endpos && startpos < end)
2936 || (startpos == endpos
2937 && (beg == endpos || (end_is_Z && endpos == end))))
2939 if (idx == len)
2941 if (extend)
2943 /* Make it work with an initial len == 0. */
2944 len *= 2;
2945 if (len == 0)
2946 len = 4;
2947 *len_ptr = len;
2948 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2949 *vec_ptr = vec;
2951 else
2952 inhibit_storing = 1;
2955 if (!inhibit_storing)
2956 vec[idx] = overlay;
2957 idx++;
2959 else if (endpos < beg && endpos > prev)
2960 prev = endpos;
2963 if (next_ptr)
2964 *next_ptr = next;
2965 if (prev_ptr)
2966 *prev_ptr = prev;
2967 return idx;
2971 /* Return non-zero if there exists an overlay with a non-nil
2972 `mouse-face' property overlapping OVERLAY. */
2975 mouse_face_overlay_overlaps (overlay)
2976 Lisp_Object overlay;
2978 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2979 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2980 int n, i, size;
2981 Lisp_Object *v, tem;
2983 size = 10;
2984 v = (Lisp_Object *) alloca (size * sizeof *v);
2985 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2986 if (n > size)
2988 v = (Lisp_Object *) alloca (n * sizeof *v);
2989 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2992 for (i = 0; i < n; ++i)
2993 if (!EQ (v[i], overlay)
2994 && (tem = Foverlay_get (overlay, Qmouse_face),
2995 !NILP (tem)))
2996 break;
2998 return i < n;
3003 /* Fast function to just test if we're at an overlay boundary. */
3005 overlay_touches_p (pos)
3006 int pos;
3008 Lisp_Object overlay;
3009 struct Lisp_Overlay *tail;
3011 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
3013 int endpos;
3015 XSETMISC (overlay ,tail);
3016 if (!OVERLAYP (overlay))
3017 abort ();
3019 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3020 if (endpos < pos)
3021 break;
3022 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3023 return 1;
3026 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
3028 int startpos;
3030 XSETMISC (overlay, tail);
3031 if (!OVERLAYP (overlay))
3032 abort ();
3034 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3035 if (pos < startpos)
3036 break;
3037 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3038 return 1;
3040 return 0;
3043 struct sortvec
3045 Lisp_Object overlay;
3046 int beg, end;
3047 int priority;
3050 static int
3051 compare_overlays (v1, v2)
3052 const void *v1, *v2;
3054 const struct sortvec *s1 = (const struct sortvec *) v1;
3055 const struct sortvec *s2 = (const struct sortvec *) v2;
3056 if (s1->priority != s2->priority)
3057 return s1->priority - s2->priority;
3058 if (s1->beg != s2->beg)
3059 return s1->beg - s2->beg;
3060 if (s1->end != s2->end)
3061 return s2->end - s1->end;
3062 return 0;
3065 /* Sort an array of overlays by priority. The array is modified in place.
3066 The return value is the new size; this may be smaller than the original
3067 size if some of the overlays were invalid or were window-specific. */
3069 sort_overlays (overlay_vec, noverlays, w)
3070 Lisp_Object *overlay_vec;
3071 int noverlays;
3072 struct window *w;
3074 int i, j;
3075 struct sortvec *sortvec;
3076 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3078 /* Put the valid and relevant overlays into sortvec. */
3080 for (i = 0, j = 0; i < noverlays; i++)
3082 Lisp_Object tem;
3083 Lisp_Object overlay;
3085 overlay = overlay_vec[i];
3086 if (OVERLAY_VALID (overlay)
3087 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3088 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3090 /* If we're interested in a specific window, then ignore
3091 overlays that are limited to some other window. */
3092 if (w)
3094 Lisp_Object window;
3096 window = Foverlay_get (overlay, Qwindow);
3097 if (WINDOWP (window) && XWINDOW (window) != w)
3098 continue;
3101 /* This overlay is good and counts: put it into sortvec. */
3102 sortvec[j].overlay = overlay;
3103 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3104 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3105 tem = Foverlay_get (overlay, Qpriority);
3106 if (INTEGERP (tem))
3107 sortvec[j].priority = XINT (tem);
3108 else
3109 sortvec[j].priority = 0;
3110 j++;
3113 noverlays = j;
3115 /* Sort the overlays into the proper order: increasing priority. */
3117 if (noverlays > 1)
3118 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3120 for (i = 0; i < noverlays; i++)
3121 overlay_vec[i] = sortvec[i].overlay;
3122 return (noverlays);
3125 struct sortstr
3127 Lisp_Object string, string2;
3128 int size;
3129 int priority;
3132 struct sortstrlist
3134 struct sortstr *buf; /* An array that expands as needed; never freed. */
3135 int size; /* Allocated length of that array. */
3136 int used; /* How much of the array is currently in use. */
3137 int bytes; /* Total length of the strings in buf. */
3140 /* Buffers for storing information about the overlays touching a given
3141 position. These could be automatic variables in overlay_strings, but
3142 it's more efficient to hold onto the memory instead of repeatedly
3143 allocating and freeing it. */
3144 static struct sortstrlist overlay_heads, overlay_tails;
3145 static unsigned char *overlay_str_buf;
3147 /* Allocated length of overlay_str_buf. */
3148 static int overlay_str_len;
3150 /* A comparison function suitable for passing to qsort. */
3151 static int
3152 cmp_for_strings (as1, as2)
3153 char *as1, *as2;
3155 struct sortstr *s1 = (struct sortstr *)as1;
3156 struct sortstr *s2 = (struct sortstr *)as2;
3157 if (s1->size != s2->size)
3158 return s2->size - s1->size;
3159 if (s1->priority != s2->priority)
3160 return s1->priority - s2->priority;
3161 return 0;
3164 static void
3165 record_overlay_string (ssl, str, str2, pri, size)
3166 struct sortstrlist *ssl;
3167 Lisp_Object str, str2, pri;
3168 int size;
3170 int nbytes;
3172 if (ssl->used == ssl->size)
3174 if (ssl->buf)
3175 ssl->size *= 2;
3176 else
3177 ssl->size = 5;
3178 ssl->buf = ((struct sortstr *)
3179 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3181 ssl->buf[ssl->used].string = str;
3182 ssl->buf[ssl->used].string2 = str2;
3183 ssl->buf[ssl->used].size = size;
3184 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3185 ssl->used++;
3187 if (NILP (current_buffer->enable_multibyte_characters))
3188 nbytes = SCHARS (str);
3189 else if (! STRING_MULTIBYTE (str))
3190 nbytes = count_size_as_multibyte (SDATA (str),
3191 SBYTES (str));
3192 else
3193 nbytes = SBYTES (str);
3195 ssl->bytes += nbytes;
3197 if (STRINGP (str2))
3199 if (NILP (current_buffer->enable_multibyte_characters))
3200 nbytes = SCHARS (str2);
3201 else if (! STRING_MULTIBYTE (str2))
3202 nbytes = count_size_as_multibyte (SDATA (str2),
3203 SBYTES (str2));
3204 else
3205 nbytes = SBYTES (str2);
3207 ssl->bytes += nbytes;
3211 /* Return the concatenation of the strings associated with overlays that
3212 begin or end at POS, ignoring overlays that are specific to a window
3213 other than W. The strings are concatenated in the appropriate order:
3214 shorter overlays nest inside longer ones, and higher priority inside
3215 lower. Normally all of the after-strings come first, but zero-sized
3216 overlays have their after-strings ride along with the before-strings
3217 because it would look strange to print them inside-out.
3219 Returns the string length, and stores the contents indirectly through
3220 PSTR, if that variable is non-null. The string may be overwritten by
3221 subsequent calls. */
3224 overlay_strings (pos, w, pstr)
3225 EMACS_INT pos;
3226 struct window *w;
3227 unsigned char **pstr;
3229 Lisp_Object overlay, window, str;
3230 struct Lisp_Overlay *ov;
3231 int startpos, endpos;
3232 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3234 overlay_heads.used = overlay_heads.bytes = 0;
3235 overlay_tails.used = overlay_tails.bytes = 0;
3236 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3238 XSETMISC (overlay, ov);
3239 eassert (OVERLAYP (overlay));
3241 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3242 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3243 if (endpos < pos)
3244 break;
3245 if (endpos != pos && startpos != pos)
3246 continue;
3247 window = Foverlay_get (overlay, Qwindow);
3248 if (WINDOWP (window) && XWINDOW (window) != w)
3249 continue;
3250 if (startpos == pos
3251 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3252 record_overlay_string (&overlay_heads, str,
3253 (startpos == endpos
3254 ? Foverlay_get (overlay, Qafter_string)
3255 : Qnil),
3256 Foverlay_get (overlay, Qpriority),
3257 endpos - startpos);
3258 else if (endpos == pos
3259 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3260 record_overlay_string (&overlay_tails, str, Qnil,
3261 Foverlay_get (overlay, Qpriority),
3262 endpos - startpos);
3264 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3266 XSETMISC (overlay, ov);
3267 eassert (OVERLAYP (overlay));
3269 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3270 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3271 if (startpos > pos)
3272 break;
3273 if (endpos != pos && startpos != pos)
3274 continue;
3275 window = Foverlay_get (overlay, Qwindow);
3276 if (WINDOWP (window) && XWINDOW (window) != w)
3277 continue;
3278 if (startpos == pos
3279 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3280 record_overlay_string (&overlay_heads, str,
3281 (startpos == endpos
3282 ? Foverlay_get (overlay, Qafter_string)
3283 : Qnil),
3284 Foverlay_get (overlay, Qpriority),
3285 endpos - startpos);
3286 else if (endpos == pos
3287 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3288 record_overlay_string (&overlay_tails, str, Qnil,
3289 Foverlay_get (overlay, Qpriority),
3290 endpos - startpos);
3292 if (overlay_tails.used > 1)
3293 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3294 cmp_for_strings);
3295 if (overlay_heads.used > 1)
3296 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3297 cmp_for_strings);
3298 if (overlay_heads.bytes || overlay_tails.bytes)
3300 Lisp_Object tem;
3301 int i;
3302 unsigned char *p;
3303 int total = overlay_heads.bytes + overlay_tails.bytes;
3305 if (total > overlay_str_len)
3307 overlay_str_len = total;
3308 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3309 total);
3311 p = overlay_str_buf;
3312 for (i = overlay_tails.used; --i >= 0;)
3314 int nbytes;
3315 tem = overlay_tails.buf[i].string;
3316 nbytes = copy_text (SDATA (tem), p,
3317 SBYTES (tem),
3318 STRING_MULTIBYTE (tem), multibyte);
3319 p += nbytes;
3321 for (i = 0; i < overlay_heads.used; ++i)
3323 int nbytes;
3324 tem = overlay_heads.buf[i].string;
3325 nbytes = copy_text (SDATA (tem), p,
3326 SBYTES (tem),
3327 STRING_MULTIBYTE (tem), multibyte);
3328 p += nbytes;
3329 tem = overlay_heads.buf[i].string2;
3330 if (STRINGP (tem))
3332 nbytes = copy_text (SDATA (tem), p,
3333 SBYTES (tem),
3334 STRING_MULTIBYTE (tem), multibyte);
3335 p += nbytes;
3338 if (p != overlay_str_buf + total)
3339 abort ();
3340 if (pstr)
3341 *pstr = overlay_str_buf;
3342 return total;
3344 return 0;
3347 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3349 void
3350 recenter_overlay_lists (buf, pos)
3351 struct buffer *buf;
3352 EMACS_INT pos;
3354 Lisp_Object overlay, beg, end;
3355 struct Lisp_Overlay *prev, *tail, *next;
3357 /* See if anything in overlays_before should move to overlays_after. */
3359 /* We don't strictly need prev in this loop; it should always be nil.
3360 But we use it for symmetry and in case that should cease to be true
3361 with some future change. */
3362 prev = NULL;
3363 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3365 next = tail->next;
3366 XSETMISC (overlay, tail);
3368 /* If the overlay is not valid, get rid of it. */
3369 if (!OVERLAY_VALID (overlay))
3370 #if 1
3371 abort ();
3372 #else
3374 /* Splice the cons cell TAIL out of overlays_before. */
3375 if (!NILP (prev))
3376 XCDR (prev) = next;
3377 else
3378 buf->overlays_before = next;
3379 tail = prev;
3380 continue;
3382 #endif
3384 beg = OVERLAY_START (overlay);
3385 end = OVERLAY_END (overlay);
3387 if (OVERLAY_POSITION (end) > pos)
3389 /* OVERLAY needs to be moved. */
3390 int where = OVERLAY_POSITION (beg);
3391 struct Lisp_Overlay *other, *other_prev;
3393 /* Splice the cons cell TAIL out of overlays_before. */
3394 if (prev)
3395 prev->next = next;
3396 else
3397 buf->overlays_before = next;
3399 /* Search thru overlays_after for where to put it. */
3400 other_prev = NULL;
3401 for (other = buf->overlays_after; other;
3402 other_prev = other, other = other->next)
3404 Lisp_Object otherbeg, otheroverlay;
3406 XSETMISC (otheroverlay, other);
3407 eassert (OVERLAY_VALID (otheroverlay));
3409 otherbeg = OVERLAY_START (otheroverlay);
3410 if (OVERLAY_POSITION (otherbeg) >= where)
3411 break;
3414 /* Add TAIL to overlays_after before OTHER. */
3415 tail->next = other;
3416 if (other_prev)
3417 other_prev->next = tail;
3418 else
3419 buf->overlays_after = tail;
3420 tail = prev;
3422 else
3423 /* We've reached the things that should stay in overlays_before.
3424 All the rest of overlays_before must end even earlier,
3425 so stop now. */
3426 break;
3429 /* See if anything in overlays_after should be in overlays_before. */
3430 prev = NULL;
3431 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3433 next = tail->next;
3434 XSETMISC (overlay, tail);
3436 /* If the overlay is not valid, get rid of it. */
3437 if (!OVERLAY_VALID (overlay))
3438 #if 1
3439 abort ();
3440 #else
3442 /* Splice the cons cell TAIL out of overlays_after. */
3443 if (!NILP (prev))
3444 XCDR (prev) = next;
3445 else
3446 buf->overlays_after = next;
3447 tail = prev;
3448 continue;
3450 #endif
3452 beg = OVERLAY_START (overlay);
3453 end = OVERLAY_END (overlay);
3455 /* Stop looking, when we know that nothing further
3456 can possibly end before POS. */
3457 if (OVERLAY_POSITION (beg) > pos)
3458 break;
3460 if (OVERLAY_POSITION (end) <= pos)
3462 /* OVERLAY needs to be moved. */
3463 int where = OVERLAY_POSITION (end);
3464 struct Lisp_Overlay *other, *other_prev;
3466 /* Splice the cons cell TAIL out of overlays_after. */
3467 if (prev)
3468 prev->next = next;
3469 else
3470 buf->overlays_after = next;
3472 /* Search thru overlays_before for where to put it. */
3473 other_prev = NULL;
3474 for (other = buf->overlays_before; other;
3475 other_prev = other, other = other->next)
3477 Lisp_Object otherend, otheroverlay;
3479 XSETMISC (otheroverlay, other);
3480 eassert (OVERLAY_VALID (otheroverlay));
3482 otherend = OVERLAY_END (otheroverlay);
3483 if (OVERLAY_POSITION (otherend) <= where)
3484 break;
3487 /* Add TAIL to overlays_before before OTHER. */
3488 tail->next = other;
3489 if (other_prev)
3490 other_prev->next = tail;
3491 else
3492 buf->overlays_before = tail;
3493 tail = prev;
3497 buf->overlay_center = pos;
3500 void
3501 adjust_overlays_for_insert (pos, length)
3502 EMACS_INT pos;
3503 EMACS_INT length;
3505 /* After an insertion, the lists are still sorted properly,
3506 but we may need to update the value of the overlay center. */
3507 if (current_buffer->overlay_center >= pos)
3508 current_buffer->overlay_center += length;
3511 void
3512 adjust_overlays_for_delete (pos, length)
3513 EMACS_INT pos;
3514 EMACS_INT length;
3516 if (current_buffer->overlay_center < pos)
3517 /* The deletion was to our right. No change needed; the before- and
3518 after-lists are still consistent. */
3520 else if (current_buffer->overlay_center > pos + length)
3521 /* The deletion was to our left. We need to adjust the center value
3522 to account for the change in position, but the lists are consistent
3523 given the new value. */
3524 current_buffer->overlay_center -= length;
3525 else
3526 /* We're right in the middle. There might be things on the after-list
3527 that now belong on the before-list. Recentering will move them,
3528 and also update the center point. */
3529 recenter_overlay_lists (current_buffer, pos);
3532 /* Fix up overlays that were garbled as a result of permuting markers
3533 in the range START through END. Any overlay with at least one
3534 endpoint in this range will need to be unlinked from the overlay
3535 list and reinserted in its proper place.
3536 Such an overlay might even have negative size at this point.
3537 If so, we'll make the overlay empty. */
3538 void
3539 fix_start_end_in_overlays (start, end)
3540 register int start, end;
3542 Lisp_Object overlay;
3543 struct Lisp_Overlay *before_list, *after_list;
3544 /* These are either nil, indicating that before_list or after_list
3545 should be assigned, or the cons cell the cdr of which should be
3546 assigned. */
3547 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3548 /* 'Parent', likewise, indicates a cons cell or
3549 current_buffer->overlays_before or overlays_after, depending
3550 which loop we're in. */
3551 struct Lisp_Overlay *tail, *parent;
3552 int startpos, endpos;
3554 /* This algorithm shifts links around instead of consing and GCing.
3555 The loop invariant is that before_list (resp. after_list) is a
3556 well-formed list except that its last element, the CDR of beforep
3557 (resp. afterp) if beforep (afterp) isn't nil or before_list
3558 (after_list) if it is, is still uninitialized. So it's not a bug
3559 that before_list isn't initialized, although it may look
3560 strange. */
3561 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3563 XSETMISC (overlay, tail);
3565 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3566 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3568 /* If the overlay is backwards, make it empty. */
3569 if (endpos < startpos)
3571 startpos = endpos;
3572 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3573 Qnil);
3576 if (endpos < start)
3577 break;
3579 if (endpos < end
3580 || (startpos >= start && startpos < end))
3582 /* Add it to the end of the wrong list. Later on,
3583 recenter_overlay_lists will move it to the right place. */
3584 if (endpos < current_buffer->overlay_center)
3586 if (!afterp)
3587 after_list = tail;
3588 else
3589 afterp->next = tail;
3590 afterp = tail;
3592 else
3594 if (!beforep)
3595 before_list = tail;
3596 else
3597 beforep->next = tail;
3598 beforep = tail;
3600 if (!parent)
3601 current_buffer->overlays_before = tail->next;
3602 else
3603 parent->next = tail->next;
3604 tail = tail->next;
3606 else
3607 parent = tail, tail = parent->next;
3609 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3611 XSETMISC (overlay, tail);
3613 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3614 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3616 /* If the overlay is backwards, make it empty. */
3617 if (endpos < startpos)
3619 startpos = endpos;
3620 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3621 Qnil);
3624 if (startpos >= end)
3625 break;
3627 if (startpos >= start
3628 || (endpos >= start && endpos < end))
3630 if (endpos < current_buffer->overlay_center)
3632 if (!afterp)
3633 after_list = tail;
3634 else
3635 afterp->next = tail;
3636 afterp = tail;
3638 else
3640 if (!beforep)
3641 before_list = tail;
3642 else
3643 beforep->next = tail;
3644 beforep = tail;
3646 if (!parent)
3647 current_buffer->overlays_after = tail->next;
3648 else
3649 parent->next = tail->next;
3650 tail = tail->next;
3652 else
3653 parent = tail, tail = parent->next;
3656 /* Splice the constructed (wrong) lists into the buffer's lists,
3657 and let the recenter function make it sane again. */
3658 if (beforep)
3660 beforep->next = current_buffer->overlays_before;
3661 current_buffer->overlays_before = before_list;
3663 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3665 if (afterp)
3667 afterp->next = current_buffer->overlays_after;
3668 current_buffer->overlays_after = after_list;
3670 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3673 /* We have two types of overlay: the one whose ending marker is
3674 after-insertion-marker (this is the usual case) and the one whose
3675 ending marker is before-insertion-marker. When `overlays_before'
3676 contains overlays of the latter type and the former type in this
3677 order and both overlays end at inserting position, inserting a text
3678 increases only the ending marker of the latter type, which results
3679 in incorrect ordering of `overlays_before'.
3681 This function fixes ordering of overlays in the slot
3682 `overlays_before' of the buffer *BP. Before the insertion, `point'
3683 was at PREV, and now is at POS. */
3685 void
3686 fix_overlays_before (bp, prev, pos)
3687 struct buffer *bp;
3688 EMACS_INT prev, pos;
3690 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3691 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3692 Lisp_Object tem;
3693 EMACS_INT end;
3695 /* After the insertion, the several overlays may be in incorrect
3696 order. The possibility is that, in the list `overlays_before',
3697 an overlay which ends at POS appears after an overlay which ends
3698 at PREV. Since POS is greater than PREV, we must fix the
3699 ordering of these overlays, by moving overlays ends at POS before
3700 the overlays ends at PREV. */
3702 /* At first, find a place where disordered overlays should be linked
3703 in. It is where an overlay which end before POS exists. (i.e. an
3704 overlay whose ending marker is after-insertion-marker if disorder
3705 exists). */
3706 while (tail
3707 && (XSETMISC (tem, tail),
3708 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3710 parent = tail;
3711 tail = tail->next;
3714 /* If we don't find such an overlay,
3715 or the found one ends before PREV,
3716 or the found one is the last one in the list,
3717 we don't have to fix anything. */
3718 if (!tail || end < prev || !tail->next)
3719 return;
3721 right_pair = parent;
3722 parent = tail;
3723 tail = tail->next;
3725 /* Now, end position of overlays in the list TAIL should be before
3726 or equal to PREV. In the loop, an overlay which ends at POS is
3727 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3728 we found an overlay which ends before PREV, the remaining
3729 overlays are in correct order. */
3730 while (tail)
3732 XSETMISC (tem, tail);
3733 end = OVERLAY_POSITION (OVERLAY_END (tem));
3735 if (end == pos)
3736 { /* This overlay is disordered. */
3737 struct Lisp_Overlay *found = tail;
3739 /* Unlink the found overlay. */
3740 tail = found->next;
3741 parent->next = tail;
3742 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3743 and link it into the right place. */
3744 if (!right_pair)
3746 found->next = bp->overlays_before;
3747 bp->overlays_before = found;
3749 else
3751 found->next = right_pair->next;
3752 right_pair->next = found;
3755 else if (end == prev)
3757 parent = tail;
3758 tail = tail->next;
3760 else /* No more disordered overlay. */
3761 break;
3765 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3766 doc: /* Return t if OBJECT is an overlay. */)
3767 (object)
3768 Lisp_Object object;
3770 return (OVERLAYP (object) ? Qt : Qnil);
3773 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3774 doc: /* Create a new overlay with range BEG to END in BUFFER.
3775 If omitted, BUFFER defaults to the current buffer.
3776 BEG and END may be integers or markers.
3777 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3778 for the front of the overlay advance when text is inserted there
3779 \(which means the text *is not* included in the overlay).
3780 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3781 for the rear of the overlay advance when text is inserted there
3782 \(which means the text *is* included in the overlay). */)
3783 (beg, end, buffer, front_advance, rear_advance)
3784 Lisp_Object beg, end, buffer;
3785 Lisp_Object front_advance, rear_advance;
3787 Lisp_Object overlay;
3788 struct buffer *b;
3790 if (NILP (buffer))
3791 XSETBUFFER (buffer, current_buffer);
3792 else
3793 CHECK_BUFFER (buffer);
3794 if (MARKERP (beg)
3795 && ! EQ (Fmarker_buffer (beg), buffer))
3796 error ("Marker points into wrong buffer");
3797 if (MARKERP (end)
3798 && ! EQ (Fmarker_buffer (end), buffer))
3799 error ("Marker points into wrong buffer");
3801 CHECK_NUMBER_COERCE_MARKER (beg);
3802 CHECK_NUMBER_COERCE_MARKER (end);
3804 if (XINT (beg) > XINT (end))
3806 Lisp_Object temp;
3807 temp = beg; beg = end; end = temp;
3810 b = XBUFFER (buffer);
3812 beg = Fset_marker (Fmake_marker (), beg, buffer);
3813 end = Fset_marker (Fmake_marker (), end, buffer);
3815 if (!NILP (front_advance))
3816 XMARKER (beg)->insertion_type = 1;
3817 if (!NILP (rear_advance))
3818 XMARKER (end)->insertion_type = 1;
3820 overlay = allocate_misc ();
3821 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3822 XOVERLAY (overlay)->start = beg;
3823 XOVERLAY (overlay)->end = end;
3824 XOVERLAY (overlay)->plist = Qnil;
3825 XOVERLAY (overlay)->next = NULL;
3827 /* Put the new overlay on the wrong list. */
3828 end = OVERLAY_END (overlay);
3829 if (OVERLAY_POSITION (end) < b->overlay_center)
3831 if (b->overlays_after)
3832 XOVERLAY (overlay)->next = b->overlays_after;
3833 b->overlays_after = XOVERLAY (overlay);
3835 else
3837 if (b->overlays_before)
3838 XOVERLAY (overlay)->next = b->overlays_before;
3839 b->overlays_before = XOVERLAY (overlay);
3842 /* This puts it in the right list, and in the right order. */
3843 recenter_overlay_lists (b, b->overlay_center);
3845 /* We don't need to redisplay the region covered by the overlay, because
3846 the overlay has no properties at the moment. */
3848 return overlay;
3851 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3853 static void
3854 modify_overlay (buf, start, end)
3855 struct buffer *buf;
3856 EMACS_INT start, end;
3858 if (start > end)
3860 int temp = start;
3861 start = end;
3862 end = temp;
3865 BUF_COMPUTE_UNCHANGED (buf, start, end);
3867 /* If this is a buffer not in the selected window,
3868 we must do other windows. */
3869 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3870 windows_or_buffers_changed = 1;
3871 /* If multiple windows show this buffer, we must do other windows. */
3872 else if (buffer_shared > 1)
3873 windows_or_buffers_changed = 1;
3874 /* If we modify an overlay at the end of the buffer, we cannot
3875 be sure that window end is still valid. */
3876 else if (end >= ZV && start <= ZV)
3877 windows_or_buffers_changed = 1;
3879 ++BUF_OVERLAY_MODIFF (buf);
3883 Lisp_Object Fdelete_overlay ();
3885 static struct Lisp_Overlay *
3886 unchain_overlay (list, overlay)
3887 struct Lisp_Overlay *list, *overlay;
3889 struct Lisp_Overlay *tmp, *prev;
3890 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3891 if (tmp == overlay)
3893 if (prev)
3894 prev->next = tmp->next;
3895 else
3896 list = tmp->next;
3897 overlay->next = NULL;
3898 break;
3900 return list;
3903 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3904 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3905 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3906 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3907 buffer. */)
3908 (overlay, beg, end, buffer)
3909 Lisp_Object overlay, beg, end, buffer;
3911 struct buffer *b, *ob;
3912 Lisp_Object obuffer;
3913 int count = SPECPDL_INDEX ();
3915 CHECK_OVERLAY (overlay);
3916 if (NILP (buffer))
3917 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3918 if (NILP (buffer))
3919 XSETBUFFER (buffer, current_buffer);
3920 CHECK_BUFFER (buffer);
3922 if (MARKERP (beg)
3923 && ! EQ (Fmarker_buffer (beg), buffer))
3924 error ("Marker points into wrong buffer");
3925 if (MARKERP (end)
3926 && ! EQ (Fmarker_buffer (end), buffer))
3927 error ("Marker points into wrong buffer");
3929 CHECK_NUMBER_COERCE_MARKER (beg);
3930 CHECK_NUMBER_COERCE_MARKER (end);
3932 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3933 return Fdelete_overlay (overlay);
3935 if (XINT (beg) > XINT (end))
3937 Lisp_Object temp;
3938 temp = beg; beg = end; end = temp;
3941 specbind (Qinhibit_quit, Qt);
3943 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3944 b = XBUFFER (buffer);
3945 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3947 /* If the overlay has changed buffers, do a thorough redisplay. */
3948 if (!EQ (buffer, obuffer))
3950 /* Redisplay where the overlay was. */
3951 if (!NILP (obuffer))
3953 int o_beg;
3954 int o_end;
3956 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3957 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3959 modify_overlay (ob, o_beg, o_end);
3962 /* Redisplay where the overlay is going to be. */
3963 modify_overlay (b, XINT (beg), XINT (end));
3965 else
3966 /* Redisplay the area the overlay has just left, or just enclosed. */
3968 int o_beg, o_end;
3970 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3971 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3973 if (o_beg == XINT (beg))
3974 modify_overlay (b, o_end, XINT (end));
3975 else if (o_end == XINT (end))
3976 modify_overlay (b, o_beg, XINT (beg));
3977 else
3979 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3980 if (XINT (end) > o_end) o_end = XINT (end);
3981 modify_overlay (b, o_beg, o_end);
3985 if (!NILP (obuffer))
3987 ob->overlays_before
3988 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3989 ob->overlays_after
3990 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3991 eassert (XOVERLAY (overlay)->next == NULL);
3994 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3995 Fset_marker (OVERLAY_END (overlay), end, buffer);
3997 /* Put the overlay on the wrong list. */
3998 end = OVERLAY_END (overlay);
3999 if (OVERLAY_POSITION (end) < b->overlay_center)
4001 XOVERLAY (overlay)->next = b->overlays_after;
4002 b->overlays_after = XOVERLAY (overlay);
4004 else
4006 XOVERLAY (overlay)->next = b->overlays_before;
4007 b->overlays_before = XOVERLAY (overlay);
4010 /* This puts it in the right list, and in the right order. */
4011 recenter_overlay_lists (b, b->overlay_center);
4013 return unbind_to (count, overlay);
4016 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
4017 doc: /* Delete the overlay OVERLAY from its buffer. */)
4018 (overlay)
4019 Lisp_Object overlay;
4021 Lisp_Object buffer;
4022 struct buffer *b;
4023 int count = SPECPDL_INDEX ();
4025 CHECK_OVERLAY (overlay);
4027 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4028 if (NILP (buffer))
4029 return Qnil;
4031 b = XBUFFER (buffer);
4032 specbind (Qinhibit_quit, Qt);
4034 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4035 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4036 eassert (XOVERLAY (overlay)->next == NULL);
4037 modify_overlay (b,
4038 marker_position (OVERLAY_START (overlay)),
4039 marker_position (OVERLAY_END (overlay)));
4040 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4041 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4043 /* When deleting an overlay with before or after strings, turn off
4044 display optimizations for the affected buffer, on the basis that
4045 these strings may contain newlines. This is easier to do than to
4046 check for that situation during redisplay. */
4047 if (!windows_or_buffers_changed
4048 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4049 || !NILP (Foverlay_get (overlay, Qafter_string))))
4050 b->prevent_redisplay_optimizations_p = 1;
4052 return unbind_to (count, Qnil);
4055 /* Overlay dissection functions. */
4057 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4058 doc: /* Return the position at which OVERLAY starts. */)
4059 (overlay)
4060 Lisp_Object overlay;
4062 CHECK_OVERLAY (overlay);
4064 return (Fmarker_position (OVERLAY_START (overlay)));
4067 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4068 doc: /* Return the position at which OVERLAY ends. */)
4069 (overlay)
4070 Lisp_Object overlay;
4072 CHECK_OVERLAY (overlay);
4074 return (Fmarker_position (OVERLAY_END (overlay)));
4077 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4078 doc: /* Return the buffer OVERLAY belongs to.
4079 Return nil if OVERLAY has been deleted. */)
4080 (overlay)
4081 Lisp_Object overlay;
4083 CHECK_OVERLAY (overlay);
4085 return Fmarker_buffer (OVERLAY_START (overlay));
4088 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4089 doc: /* Return a list of the properties on OVERLAY.
4090 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4091 OVERLAY. */)
4092 (overlay)
4093 Lisp_Object overlay;
4095 CHECK_OVERLAY (overlay);
4097 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4101 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4102 doc: /* Return a list of the overlays that contain the character at POS. */)
4103 (pos)
4104 Lisp_Object pos;
4106 int noverlays;
4107 Lisp_Object *overlay_vec;
4108 int len;
4109 Lisp_Object result;
4111 CHECK_NUMBER_COERCE_MARKER (pos);
4113 len = 10;
4114 /* We can't use alloca here because overlays_at can call xrealloc. */
4115 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4117 /* Put all the overlays we want in a vector in overlay_vec.
4118 Store the length in len. */
4119 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4120 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4122 /* Make a list of them all. */
4123 result = Flist (noverlays, overlay_vec);
4125 xfree (overlay_vec);
4126 return result;
4129 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4130 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4131 Overlap means that at least one character is contained within the overlay
4132 and also contained within the specified region.
4133 Empty overlays are included in the result if they are located at BEG,
4134 between BEG and END, or at END provided END denotes the position at the
4135 end of the buffer. */)
4136 (beg, end)
4137 Lisp_Object beg, end;
4139 int noverlays;
4140 Lisp_Object *overlay_vec;
4141 int len;
4142 Lisp_Object result;
4144 CHECK_NUMBER_COERCE_MARKER (beg);
4145 CHECK_NUMBER_COERCE_MARKER (end);
4147 len = 10;
4148 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4150 /* Put all the overlays we want in a vector in overlay_vec.
4151 Store the length in len. */
4152 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4153 (int *) 0, (int *) 0);
4155 /* Make a list of them all. */
4156 result = Flist (noverlays, overlay_vec);
4158 xfree (overlay_vec);
4159 return result;
4162 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4163 1, 1, 0,
4164 doc: /* Return the next position after POS where an overlay starts or ends.
4165 If there are no overlay boundaries from POS to (point-max),
4166 the value is (point-max). */)
4167 (pos)
4168 Lisp_Object pos;
4170 int noverlays;
4171 EMACS_INT endpos;
4172 Lisp_Object *overlay_vec;
4173 int len;
4174 int i;
4176 CHECK_NUMBER_COERCE_MARKER (pos);
4178 len = 10;
4179 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4181 /* Put all the overlays we want in a vector in overlay_vec.
4182 Store the length in len.
4183 endpos gets the position where the next overlay starts. */
4184 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4185 &endpos, (EMACS_INT *) 0, 1);
4187 /* If any of these overlays ends before endpos,
4188 use its ending point instead. */
4189 for (i = 0; i < noverlays; i++)
4191 Lisp_Object oend;
4192 EMACS_INT oendpos;
4194 oend = OVERLAY_END (overlay_vec[i]);
4195 oendpos = OVERLAY_POSITION (oend);
4196 if (oendpos < endpos)
4197 endpos = oendpos;
4200 xfree (overlay_vec);
4201 return make_number (endpos);
4204 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4205 Sprevious_overlay_change, 1, 1, 0,
4206 doc: /* Return the previous position before POS where an overlay starts or ends.
4207 If there are no overlay boundaries from (point-min) to POS,
4208 the value is (point-min). */)
4209 (pos)
4210 Lisp_Object pos;
4212 int noverlays;
4213 EMACS_INT prevpos;
4214 Lisp_Object *overlay_vec;
4215 int len;
4217 CHECK_NUMBER_COERCE_MARKER (pos);
4219 /* At beginning of buffer, we know the answer;
4220 avoid bug subtracting 1 below. */
4221 if (XINT (pos) == BEGV)
4222 return pos;
4224 len = 10;
4225 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4227 /* Put all the overlays we want in a vector in overlay_vec.
4228 Store the length in len.
4229 prevpos gets the position of the previous change. */
4230 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4231 (EMACS_INT *) 0, &prevpos, 1);
4233 xfree (overlay_vec);
4234 return make_number (prevpos);
4237 /* These functions are for debugging overlays. */
4239 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4240 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4241 The car has all the overlays before the overlay center;
4242 the cdr has all the overlays after the overlay center.
4243 Recentering overlays moves overlays between these lists.
4244 The lists you get are copies, so that changing them has no effect.
4245 However, the overlays you get are the real objects that the buffer uses. */)
4248 struct Lisp_Overlay *ol;
4249 Lisp_Object before = Qnil, after = Qnil, tmp;
4250 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4252 XSETMISC (tmp, ol);
4253 before = Fcons (tmp, before);
4255 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4257 XSETMISC (tmp, ol);
4258 after = Fcons (tmp, after);
4260 return Fcons (Fnreverse (before), Fnreverse (after));
4263 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4264 doc: /* Recenter the overlays of the current buffer around position POS.
4265 That makes overlay lookup faster for positions near POS (but perhaps slower
4266 for positions far away from POS). */)
4267 (pos)
4268 Lisp_Object pos;
4270 CHECK_NUMBER_COERCE_MARKER (pos);
4272 recenter_overlay_lists (current_buffer, XINT (pos));
4273 return Qnil;
4276 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4277 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4278 (overlay, prop)
4279 Lisp_Object overlay, prop;
4281 CHECK_OVERLAY (overlay);
4282 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4285 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4286 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4287 (overlay, prop, value)
4288 Lisp_Object overlay, prop, value;
4290 Lisp_Object tail, buffer;
4291 int changed;
4293 CHECK_OVERLAY (overlay);
4295 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4297 for (tail = XOVERLAY (overlay)->plist;
4298 CONSP (tail) && CONSP (XCDR (tail));
4299 tail = XCDR (XCDR (tail)))
4300 if (EQ (XCAR (tail), prop))
4302 changed = !EQ (XCAR (XCDR (tail)), value);
4303 XSETCAR (XCDR (tail), value);
4304 goto found;
4306 /* It wasn't in the list, so add it to the front. */
4307 changed = !NILP (value);
4308 XOVERLAY (overlay)->plist
4309 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4310 found:
4311 if (! NILP (buffer))
4313 if (changed)
4314 modify_overlay (XBUFFER (buffer),
4315 marker_position (OVERLAY_START (overlay)),
4316 marker_position (OVERLAY_END (overlay)));
4317 if (EQ (prop, Qevaporate) && ! NILP (value)
4318 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4319 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4320 Fdelete_overlay (overlay);
4323 return value;
4326 /* Subroutine of report_overlay_modification. */
4328 /* Lisp vector holding overlay hook functions to call.
4329 Vector elements come in pairs.
4330 Each even-index element is a list of hook functions.
4331 The following odd-index element is the overlay they came from.
4333 Before the buffer change, we fill in this vector
4334 as we call overlay hook functions.
4335 After the buffer change, we get the functions to call from this vector.
4336 This way we always call the same functions before and after the change. */
4337 static Lisp_Object last_overlay_modification_hooks;
4339 /* Number of elements actually used in last_overlay_modification_hooks. */
4340 static int last_overlay_modification_hooks_used;
4342 /* Add one functionlist/overlay pair
4343 to the end of last_overlay_modification_hooks. */
4345 static void
4346 add_overlay_mod_hooklist (functionlist, overlay)
4347 Lisp_Object functionlist, overlay;
4349 int oldsize = XVECTOR_SIZE (last_overlay_modification_hooks);
4351 if (last_overlay_modification_hooks_used == oldsize)
4352 last_overlay_modification_hooks = larger_vector
4353 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4354 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4355 functionlist); last_overlay_modification_hooks_used++;
4356 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4357 overlay); last_overlay_modification_hooks_used++;
4360 /* Run the modification-hooks of overlays that include
4361 any part of the text in START to END.
4362 If this change is an insertion, also
4363 run the insert-before-hooks of overlay starting at END,
4364 and the insert-after-hooks of overlay ending at START.
4366 This is called both before and after the modification.
4367 AFTER is nonzero when we call after the modification.
4369 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4370 When AFTER is nonzero, they are the start position,
4371 the position after the inserted new text,
4372 and the length of deleted or replaced old text. */
4374 void
4375 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4376 Lisp_Object start, end;
4377 int after;
4378 Lisp_Object arg1, arg2, arg3;
4380 Lisp_Object prop, overlay;
4381 struct Lisp_Overlay *tail;
4382 /* 1 if this change is an insertion. */
4383 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4384 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4386 overlay = Qnil;
4387 tail = NULL;
4389 /* We used to run the functions as soon as we found them and only register
4390 them in last_overlay_modification_hooks for the purpose of the `after'
4391 case. But running elisp code as we traverse the list of overlays is
4392 painful because the list can be modified by the elisp code so we had to
4393 copy at several places. We now simply do a read-only traversal that
4394 only collects the functions to run and we run them afterwards. It's
4395 simpler, especially since all the code was already there. -stef */
4397 if (!after)
4399 /* We are being called before a change.
4400 Scan the overlays to find the functions to call. */
4401 last_overlay_modification_hooks_used = 0;
4402 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4404 int startpos, endpos;
4405 Lisp_Object ostart, oend;
4407 XSETMISC (overlay, tail);
4409 ostart = OVERLAY_START (overlay);
4410 oend = OVERLAY_END (overlay);
4411 endpos = OVERLAY_POSITION (oend);
4412 if (XFASTINT (start) > endpos)
4413 break;
4414 startpos = OVERLAY_POSITION (ostart);
4415 if (insertion && (XFASTINT (start) == startpos
4416 || XFASTINT (end) == startpos))
4418 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4419 if (!NILP (prop))
4420 add_overlay_mod_hooklist (prop, overlay);
4422 if (insertion && (XFASTINT (start) == endpos
4423 || XFASTINT (end) == endpos))
4425 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4426 if (!NILP (prop))
4427 add_overlay_mod_hooklist (prop, overlay);
4429 /* Test for intersecting intervals. This does the right thing
4430 for both insertion and deletion. */
4431 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4433 prop = Foverlay_get (overlay, Qmodification_hooks);
4434 if (!NILP (prop))
4435 add_overlay_mod_hooklist (prop, overlay);
4439 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4441 int startpos, endpos;
4442 Lisp_Object ostart, oend;
4444 XSETMISC (overlay, tail);
4446 ostart = OVERLAY_START (overlay);
4447 oend = OVERLAY_END (overlay);
4448 startpos = OVERLAY_POSITION (ostart);
4449 endpos = OVERLAY_POSITION (oend);
4450 if (XFASTINT (end) < startpos)
4451 break;
4452 if (insertion && (XFASTINT (start) == startpos
4453 || XFASTINT (end) == startpos))
4455 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4456 if (!NILP (prop))
4457 add_overlay_mod_hooklist (prop, overlay);
4459 if (insertion && (XFASTINT (start) == endpos
4460 || XFASTINT (end) == endpos))
4462 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4463 if (!NILP (prop))
4464 add_overlay_mod_hooklist (prop, overlay);
4466 /* Test for intersecting intervals. This does the right thing
4467 for both insertion and deletion. */
4468 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4470 prop = Foverlay_get (overlay, Qmodification_hooks);
4471 if (!NILP (prop))
4472 add_overlay_mod_hooklist (prop, overlay);
4477 GCPRO4 (overlay, arg1, arg2, arg3);
4479 /* Call the functions recorded in last_overlay_modification_hooks.
4480 First copy the vector contents, in case some of these hooks
4481 do subsequent modification of the buffer. */
4482 int size = last_overlay_modification_hooks_used;
4483 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4484 int i;
4486 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4487 copy, size * sizeof (Lisp_Object));
4488 gcpro1.var = copy;
4489 gcpro1.nvars = size;
4491 for (i = 0; i < size;)
4493 Lisp_Object prop, overlay;
4494 prop = copy[i++];
4495 overlay = copy[i++];
4496 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4499 UNGCPRO;
4502 static void
4503 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4504 Lisp_Object list, overlay;
4505 int after;
4506 Lisp_Object arg1, arg2, arg3;
4508 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4510 GCPRO4 (list, arg1, arg2, arg3);
4512 while (CONSP (list))
4514 if (NILP (arg3))
4515 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4516 else
4517 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4518 list = XCDR (list);
4520 UNGCPRO;
4523 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4524 property is set. */
4525 void
4526 evaporate_overlays (pos)
4527 EMACS_INT pos;
4529 Lisp_Object overlay, hit_list;
4530 struct Lisp_Overlay *tail;
4532 hit_list = Qnil;
4533 if (pos <= current_buffer->overlay_center)
4534 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4536 int endpos;
4537 XSETMISC (overlay, tail);
4538 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4539 if (endpos < pos)
4540 break;
4541 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4542 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4543 hit_list = Fcons (overlay, hit_list);
4545 else
4546 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4548 int startpos;
4549 XSETMISC (overlay, tail);
4550 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4551 if (startpos > pos)
4552 break;
4553 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4554 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4555 hit_list = Fcons (overlay, hit_list);
4557 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4558 Fdelete_overlay (XCAR (hit_list));
4561 /* Somebody has tried to store a value with an unacceptable type
4562 in the slot with offset OFFSET. */
4564 void
4565 buffer_slot_type_mismatch (newval, type)
4566 Lisp_Object newval;
4567 int type;
4569 Lisp_Object predicate;
4571 switch (type)
4573 case_Lisp_Int: predicate = Qintegerp; break;
4574 case Lisp_String: predicate = Qstringp; break;
4575 case Lisp_Symbol: predicate = Qsymbolp; break;
4576 default: abort ();
4579 wrong_type_argument (predicate, newval);
4583 /***********************************************************************
4584 Allocation with mmap
4585 ***********************************************************************/
4587 #ifdef USE_MMAP_FOR_BUFFERS
4589 #include <sys/types.h>
4590 #include <sys/mman.h>
4592 #ifndef MAP_ANON
4593 #ifdef MAP_ANONYMOUS
4594 #define MAP_ANON MAP_ANONYMOUS
4595 #else
4596 #define MAP_ANON 0
4597 #endif
4598 #endif
4600 #ifndef MAP_FAILED
4601 #define MAP_FAILED ((void *) -1)
4602 #endif
4604 #include <stdio.h>
4605 #include <errno.h>
4607 #if MAP_ANON == 0
4608 #include <fcntl.h>
4609 #endif
4611 #include "coding.h"
4614 /* Memory is allocated in regions which are mapped using mmap(2).
4615 The current implementation lets the system select mapped
4616 addresses; we're not using MAP_FIXED in general, except when
4617 trying to enlarge regions.
4619 Each mapped region starts with a mmap_region structure, the user
4620 area starts after that structure, aligned to MEM_ALIGN.
4622 +-----------------------+
4623 | struct mmap_info + |
4624 | padding |
4625 +-----------------------+
4626 | user data |
4629 +-----------------------+ */
4631 struct mmap_region
4633 /* User-specified size. */
4634 size_t nbytes_specified;
4636 /* Number of bytes mapped */
4637 size_t nbytes_mapped;
4639 /* Pointer to the location holding the address of the memory
4640 allocated with the mmap'd block. The variable actually points
4641 after this structure. */
4642 POINTER_TYPE **var;
4644 /* Next and previous in list of all mmap'd regions. */
4645 struct mmap_region *next, *prev;
4648 /* Doubly-linked list of mmap'd regions. */
4650 static struct mmap_region *mmap_regions;
4652 /* File descriptor for mmap. If we don't have anonymous mapping,
4653 /dev/zero will be opened on it. */
4655 static int mmap_fd;
4657 /* Temporary storage for mmap_set_vars, see there. */
4659 static struct mmap_region *mmap_regions_1;
4660 static int mmap_fd_1;
4662 /* Page size on this system. */
4664 static int mmap_page_size;
4666 /* 1 means mmap has been intialized. */
4668 static int mmap_initialized_p;
4670 /* Value is X rounded up to the next multiple of N. */
4672 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4674 /* Size of mmap_region structure plus padding. */
4676 #define MMAP_REGION_STRUCT_SIZE \
4677 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4679 /* Given a pointer P to the start of the user-visible part of a mapped
4680 region, return a pointer to the start of the region. */
4682 #define MMAP_REGION(P) \
4683 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4685 /* Given a pointer P to the start of a mapped region, return a pointer
4686 to the start of the user-visible part of the region. */
4688 #define MMAP_USER_AREA(P) \
4689 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4691 #define MEM_ALIGN sizeof (double)
4693 /* Predicate returning true if part of the address range [START .. END]
4694 is currently mapped. Used to prevent overwriting an existing
4695 memory mapping.
4697 Default is to conservativly assume the address range is occupied by
4698 something else. This can be overridden by system configuration
4699 files if system-specific means to determine this exists. */
4701 #ifndef MMAP_ALLOCATED_P
4702 #define MMAP_ALLOCATED_P(start, end) 1
4703 #endif
4705 /* Function prototypes. */
4707 static int mmap_free_1 P_ ((struct mmap_region *));
4708 static int mmap_enlarge P_ ((struct mmap_region *, int));
4709 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4710 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4711 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4712 static void mmap_free P_ ((POINTER_TYPE **ptr));
4713 static void mmap_init P_ ((void));
4716 /* Return a region overlapping address range START...END, or null if
4717 none. END is not including, i.e. the last byte in the range
4718 is at END - 1. */
4720 static struct mmap_region *
4721 mmap_find (start, end)
4722 POINTER_TYPE *start, *end;
4724 struct mmap_region *r;
4725 char *s = (char *) start, *e = (char *) end;
4727 for (r = mmap_regions; r; r = r->next)
4729 char *rstart = (char *) r;
4730 char *rend = rstart + r->nbytes_mapped;
4732 if (/* First byte of range, i.e. START, in this region? */
4733 (s >= rstart && s < rend)
4734 /* Last byte of range, i.e. END - 1, in this region? */
4735 || (e > rstart && e <= rend)
4736 /* First byte of this region in the range? */
4737 || (rstart >= s && rstart < e)
4738 /* Last byte of this region in the range? */
4739 || (rend > s && rend <= e))
4740 break;
4743 return r;
4747 /* Unmap a region. P is a pointer to the start of the user-araa of
4748 the region. Value is non-zero if successful. */
4750 static int
4751 mmap_free_1 (r)
4752 struct mmap_region *r;
4754 if (r->next)
4755 r->next->prev = r->prev;
4756 if (r->prev)
4757 r->prev->next = r->next;
4758 else
4759 mmap_regions = r->next;
4761 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4763 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4764 return 0;
4767 return 1;
4771 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4772 Value is non-zero if successful. */
4774 static int
4775 mmap_enlarge (r, npages)
4776 struct mmap_region *r;
4777 int npages;
4779 char *region_end = (char *) r + r->nbytes_mapped;
4780 size_t nbytes;
4781 int success = 0;
4783 if (npages < 0)
4785 /* Unmap pages at the end of the region. */
4786 nbytes = - npages * mmap_page_size;
4787 if (munmap (region_end - nbytes, nbytes) == -1)
4788 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4789 else
4791 r->nbytes_mapped -= nbytes;
4792 success = 1;
4795 else if (npages > 0)
4797 nbytes = npages * mmap_page_size;
4799 /* Try to map additional pages at the end of the region. We
4800 cannot do this if the address range is already occupied by
4801 something else because mmap deletes any previous mapping.
4802 I'm not sure this is worth doing, let's see. */
4803 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4805 POINTER_TYPE *p;
4807 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4808 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4809 if (p == MAP_FAILED)
4810 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4811 else if (p != (POINTER_TYPE *) region_end)
4813 /* Kernels are free to choose a different address. In
4814 that case, unmap what we've mapped above; we have
4815 no use for it. */
4816 if (munmap (p, nbytes) == -1)
4817 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4819 else
4821 r->nbytes_mapped += nbytes;
4822 success = 1;
4827 return success;
4831 /* Set or reset variables holding references to mapped regions. If
4832 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4833 non-zero, set all variables to the start of the user-areas
4834 of mapped regions.
4836 This function is called from Fdump_emacs to ensure that the dumped
4837 Emacs doesn't contain references to memory that won't be mapped
4838 when Emacs starts. */
4840 void
4841 mmap_set_vars (restore_p)
4842 int restore_p;
4844 struct mmap_region *r;
4846 if (restore_p)
4848 mmap_regions = mmap_regions_1;
4849 mmap_fd = mmap_fd_1;
4850 for (r = mmap_regions; r; r = r->next)
4851 *r->var = MMAP_USER_AREA (r);
4853 else
4855 for (r = mmap_regions; r; r = r->next)
4856 *r->var = NULL;
4857 mmap_regions_1 = mmap_regions;
4858 mmap_regions = NULL;
4859 mmap_fd_1 = mmap_fd;
4860 mmap_fd = -1;
4865 /* Allocate a block of storage large enough to hold NBYTES bytes of
4866 data. A pointer to the data is returned in *VAR. VAR is thus the
4867 address of some variable which will use the data area.
4869 The allocation of 0 bytes is valid.
4871 If we can't allocate the necessary memory, set *VAR to null, and
4872 return null. */
4874 static POINTER_TYPE *
4875 mmap_alloc (var, nbytes)
4876 POINTER_TYPE **var;
4877 size_t nbytes;
4879 void *p;
4880 size_t map;
4882 mmap_init ();
4884 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4885 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4886 mmap_fd, 0);
4888 if (p == MAP_FAILED)
4890 if (errno != ENOMEM)
4891 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4892 p = NULL;
4894 else
4896 struct mmap_region *r = (struct mmap_region *) p;
4898 r->nbytes_specified = nbytes;
4899 r->nbytes_mapped = map;
4900 r->var = var;
4901 r->prev = NULL;
4902 r->next = mmap_regions;
4903 if (r->next)
4904 r->next->prev = r;
4905 mmap_regions = r;
4907 p = MMAP_USER_AREA (p);
4910 return *var = p;
4914 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4915 resize it to size NBYTES. Change *VAR to reflect the new block,
4916 and return this value. If more memory cannot be allocated, then
4917 leave *VAR unchanged, and return null. */
4919 static POINTER_TYPE *
4920 mmap_realloc (var, nbytes)
4921 POINTER_TYPE **var;
4922 size_t nbytes;
4924 POINTER_TYPE *result;
4926 mmap_init ();
4928 if (*var == NULL)
4929 result = mmap_alloc (var, nbytes);
4930 else if (nbytes == 0)
4932 mmap_free (var);
4933 result = mmap_alloc (var, nbytes);
4935 else
4937 struct mmap_region *r = MMAP_REGION (*var);
4938 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4940 if (room < nbytes)
4942 /* Must enlarge. */
4943 POINTER_TYPE *old_ptr = *var;
4945 /* Try to map additional pages at the end of the region.
4946 If that fails, allocate a new region, copy data
4947 from the old region, then free it. */
4948 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4949 / mmap_page_size)))
4951 r->nbytes_specified = nbytes;
4952 *var = result = old_ptr;
4954 else if (mmap_alloc (var, nbytes))
4956 bcopy (old_ptr, *var, r->nbytes_specified);
4957 mmap_free_1 (MMAP_REGION (old_ptr));
4958 result = *var;
4959 r = MMAP_REGION (result);
4960 r->nbytes_specified = nbytes;
4962 else
4964 *var = old_ptr;
4965 result = NULL;
4968 else if (room - nbytes >= mmap_page_size)
4970 /* Shrinking by at least a page. Let's give some
4971 memory back to the system.
4973 The extra parens are to make the division happens first,
4974 on positive values, so we know it will round towards
4975 zero. */
4976 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4977 result = *var;
4978 r->nbytes_specified = nbytes;
4980 else
4982 /* Leave it alone. */
4983 result = *var;
4984 r->nbytes_specified = nbytes;
4988 return result;
4992 /* Free a block of relocatable storage whose data is pointed to by
4993 PTR. Store 0 in *PTR to show there's no block allocated. */
4995 static void
4996 mmap_free (var)
4997 POINTER_TYPE **var;
4999 mmap_init ();
5001 if (*var)
5003 mmap_free_1 (MMAP_REGION (*var));
5004 *var = NULL;
5009 /* Perform necessary intializations for the use of mmap. */
5011 static void
5012 mmap_init ()
5014 #if MAP_ANON == 0
5015 /* The value of mmap_fd is initially 0 in temacs, and -1
5016 in a dumped Emacs. */
5017 if (mmap_fd <= 0)
5019 /* No anonymous mmap -- we need the file descriptor. */
5020 mmap_fd = open ("/dev/zero", O_RDONLY);
5021 if (mmap_fd == -1)
5022 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5024 #endif /* MAP_ANON == 0 */
5026 if (mmap_initialized_p)
5027 return;
5028 mmap_initialized_p = 1;
5030 #if MAP_ANON != 0
5031 mmap_fd = -1;
5032 #endif
5034 mmap_page_size = getpagesize ();
5037 #endif /* USE_MMAP_FOR_BUFFERS */
5041 /***********************************************************************
5042 Buffer-text Allocation
5043 ***********************************************************************/
5045 #ifdef REL_ALLOC
5046 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5047 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5048 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5049 #endif /* REL_ALLOC */
5052 /* Allocate NBYTES bytes for buffer B's text buffer. */
5054 static void
5055 alloc_buffer_text (b, nbytes)
5056 struct buffer *b;
5057 size_t nbytes;
5059 POINTER_TYPE *p;
5061 BLOCK_INPUT;
5062 #if defined USE_MMAP_FOR_BUFFERS
5063 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5064 #elif defined REL_ALLOC
5065 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5066 #else
5067 p = xmalloc (nbytes);
5068 #endif
5070 if (p == NULL)
5072 UNBLOCK_INPUT;
5073 memory_full ();
5076 b->text->beg = (unsigned char *) p;
5077 UNBLOCK_INPUT;
5080 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5081 shrink it. */
5083 void
5084 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5086 POINTER_TYPE *p;
5087 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5088 + delta);
5089 BLOCK_INPUT;
5090 #if defined USE_MMAP_FOR_BUFFERS
5091 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5092 #elif defined REL_ALLOC
5093 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5094 #else
5095 p = xrealloc (b->text->beg, nbytes);
5096 #endif
5098 if (p == NULL)
5100 UNBLOCK_INPUT;
5101 memory_full ();
5104 BUF_BEG_ADDR (b) = (unsigned char *) p;
5105 UNBLOCK_INPUT;
5109 /* Free buffer B's text buffer. */
5111 static void
5112 free_buffer_text (b)
5113 struct buffer *b;
5115 BLOCK_INPUT;
5117 #if defined USE_MMAP_FOR_BUFFERS
5118 mmap_free ((POINTER_TYPE **) &b->text->beg);
5119 #elif defined REL_ALLOC
5120 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5121 #else
5122 xfree (b->text->beg);
5123 #endif
5125 BUF_BEG_ADDR (b) = NULL;
5126 UNBLOCK_INPUT;
5131 /***********************************************************************
5132 Initialization
5133 ***********************************************************************/
5135 void
5136 init_buffer_once ()
5138 int idx;
5140 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5142 /* Make sure all markable slots in buffer_defaults
5143 are initialized reasonably, so mark_buffer won't choke. */
5144 reset_buffer (&buffer_defaults);
5145 reset_buffer_local_variables (&buffer_defaults, 1);
5146 reset_buffer (&buffer_local_symbols);
5147 reset_buffer_local_variables (&buffer_local_symbols, 1);
5148 /* Prevent GC from getting confused. */
5149 buffer_defaults.text = &buffer_defaults.own_text;
5150 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5151 BUF_INTERVALS (&buffer_defaults) = 0;
5152 BUF_INTERVALS (&buffer_local_symbols) = 0;
5153 XSETPVECTYPESIZE (&buffer_defaults, PVEC_BUFFER, 0);
5154 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5155 XSETPVECTYPESIZE (&buffer_local_symbols, PVEC_BUFFER, 0);
5156 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5158 /* Set up the default values of various buffer slots. */
5159 /* Must do these before making the first buffer! */
5161 /* real setup is done in bindings.el */
5162 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5163 buffer_defaults.header_line_format = Qnil;
5164 buffer_defaults.abbrev_mode = Qnil;
5165 buffer_defaults.overwrite_mode = Qnil;
5166 buffer_defaults.case_fold_search = Qt;
5167 buffer_defaults.auto_fill_function = Qnil;
5168 buffer_defaults.selective_display = Qnil;
5169 #ifndef old
5170 buffer_defaults.selective_display_ellipses = Qt;
5171 #endif
5172 buffer_defaults.abbrev_table = Qnil;
5173 buffer_defaults.display_table = Qnil;
5174 buffer_defaults.undo_list = Qnil;
5175 buffer_defaults.mark_active = Qnil;
5176 buffer_defaults.file_format = Qnil;
5177 buffer_defaults.auto_save_file_format = Qt;
5178 buffer_defaults.overlays_before = NULL;
5179 buffer_defaults.overlays_after = NULL;
5180 buffer_defaults.overlay_center = BEG;
5182 XSETFASTINT (buffer_defaults.tab_width, 8);
5183 buffer_defaults.truncate_lines = Qnil;
5184 buffer_defaults.word_wrap = Qnil;
5185 buffer_defaults.ctl_arrow = Qt;
5186 buffer_defaults.direction_reversed = Qnil;
5187 buffer_defaults.cursor_type = Qt;
5188 buffer_defaults.extra_line_spacing = Qnil;
5189 buffer_defaults.cursor_in_non_selected_windows = Qt;
5191 #ifdef DOS_NT
5192 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5193 #endif
5194 buffer_defaults.enable_multibyte_characters = Qt;
5195 buffer_defaults.buffer_file_coding_system = Qnil;
5196 XSETFASTINT (buffer_defaults.fill_column, 70);
5197 XSETFASTINT (buffer_defaults.left_margin, 0);
5198 buffer_defaults.cache_long_line_scans = Qnil;
5199 buffer_defaults.file_truename = Qnil;
5200 XSETFASTINT (buffer_defaults.display_count, 0);
5201 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5202 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5203 buffer_defaults.left_fringe_width = Qnil;
5204 buffer_defaults.right_fringe_width = Qnil;
5205 buffer_defaults.fringes_outside_margins = Qnil;
5206 buffer_defaults.scroll_bar_width = Qnil;
5207 buffer_defaults.vertical_scroll_bar_type = Qt;
5208 buffer_defaults.indicate_empty_lines = Qnil;
5209 buffer_defaults.indicate_buffer_boundaries = Qnil;
5210 buffer_defaults.fringe_indicator_alist = Qnil;
5211 buffer_defaults.fringe_cursor_alist = Qnil;
5212 buffer_defaults.scroll_up_aggressively = Qnil;
5213 buffer_defaults.scroll_down_aggressively = Qnil;
5214 buffer_defaults.display_time = Qnil;
5216 /* Assign the local-flags to the slots that have default values.
5217 The local flag is a bit that is used in the buffer
5218 to say that it has its own local value for the slot.
5219 The local flag bits are in the local_var_flags slot of the buffer. */
5221 /* Nothing can work if this isn't true */
5222 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5224 /* 0 means not a lisp var, -1 means always local, else mask */
5225 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5226 XSETINT (buffer_local_flags.filename, -1);
5227 XSETINT (buffer_local_flags.directory, -1);
5228 XSETINT (buffer_local_flags.backed_up, -1);
5229 XSETINT (buffer_local_flags.save_length, -1);
5230 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5231 XSETINT (buffer_local_flags.read_only, -1);
5232 XSETINT (buffer_local_flags.major_mode, -1);
5233 XSETINT (buffer_local_flags.mode_name, -1);
5234 XSETINT (buffer_local_flags.undo_list, -1);
5235 XSETINT (buffer_local_flags.mark_active, -1);
5236 XSETINT (buffer_local_flags.point_before_scroll, -1);
5237 XSETINT (buffer_local_flags.file_truename, -1);
5238 XSETINT (buffer_local_flags.invisibility_spec, -1);
5239 XSETINT (buffer_local_flags.file_format, -1);
5240 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5241 XSETINT (buffer_local_flags.display_count, -1);
5242 XSETINT (buffer_local_flags.display_time, -1);
5243 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5245 idx = 1;
5246 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5247 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5248 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5249 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5250 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5251 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5252 #ifndef old
5253 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5254 #endif
5255 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5256 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5257 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5258 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5259 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5260 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5261 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5262 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5263 #ifdef DOS_NT
5264 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5265 /* Make this one a permanent local. */
5266 buffer_permanent_local_flags[idx++] = 1;
5267 #endif
5268 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5269 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5270 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5271 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5272 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5273 /* Make this one a permanent local. */
5274 buffer_permanent_local_flags[idx++] = 1;
5275 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5276 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5277 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5278 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5279 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5280 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5281 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5282 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5283 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5284 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5285 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5286 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5287 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5288 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5289 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5290 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5291 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5293 /* Need more room? */
5294 if (idx >= MAX_PER_BUFFER_VARS)
5295 abort ();
5296 last_per_buffer_idx = idx;
5298 Vbuffer_alist = Qnil;
5299 current_buffer = 0;
5300 all_buffers = 0;
5302 QSFundamental = make_pure_c_string ("Fundamental");
5304 Qfundamental_mode = intern_c_string ("fundamental-mode");
5305 buffer_defaults.major_mode = Qfundamental_mode;
5307 Qmode_class = intern_c_string ("mode-class");
5309 Qprotected_field = intern_c_string ("protected-field");
5311 Qpermanent_local = intern_c_string ("permanent-local");
5313 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5314 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5316 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5318 /* super-magic invisible buffer */
5319 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5320 Vbuffer_alist = Qnil;
5322 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5324 inhibit_modification_hooks = 0;
5327 void
5328 init_buffer ()
5330 char *pwd;
5331 Lisp_Object temp;
5332 int len;
5334 #ifdef USE_MMAP_FOR_BUFFERS
5336 /* When using the ralloc implementation based on mmap(2), buffer
5337 text pointers will have been set to null in the dumped Emacs.
5338 Map new memory. */
5339 struct buffer *b;
5341 for (b = all_buffers; b; b = b->next)
5342 if (b->text->beg == NULL)
5343 enlarge_buffer_text (b, 0);
5345 #endif /* USE_MMAP_FOR_BUFFERS */
5347 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5348 if (NILP (buffer_defaults.enable_multibyte_characters))
5349 Fset_buffer_multibyte (Qnil);
5351 pwd = get_current_dir_name ();
5353 if (!pwd)
5354 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5356 /* Maybe this should really use some standard subroutine
5357 whose definition is filename syntax dependent. */
5358 len = strlen (pwd);
5359 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5361 /* Grow buffer to add directory separator and '\0'. */
5362 pwd = (char *) realloc (pwd, len + 2);
5363 if (!pwd)
5364 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5365 pwd[len] = DIRECTORY_SEP;
5366 pwd[len + 1] = '\0';
5369 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5370 if (! NILP (buffer_defaults.enable_multibyte_characters))
5371 /* At this moment, we still don't know how to decode the
5372 directory name. So, we keep the bytes in multibyte form so
5373 that ENCODE_FILE correctly gets the original bytes. */
5374 current_buffer->directory
5375 = string_to_multibyte (current_buffer->directory);
5377 /* Add /: to the front of the name
5378 if it would otherwise be treated as magic. */
5379 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5380 if (! NILP (temp)
5381 /* If the default dir is just /, TEMP is non-nil
5382 because of the ange-ftp completion handler.
5383 However, it is not necessary to turn / into /:/.
5384 So avoid doing that. */
5385 && strcmp ("/", SDATA (current_buffer->directory)))
5386 current_buffer->directory
5387 = concat2 (build_string ("/:"), current_buffer->directory);
5389 temp = get_minibuffer (0);
5390 XBUFFER (temp)->directory = current_buffer->directory;
5392 free (pwd);
5395 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5396 Object stored in the current buffer. address is the address of the slot
5397 in the buffer that is current now. */
5399 /* TYPE is nil for a general Lisp variable.
5400 An integer specifies a type; then only LIsp values
5401 with that type code are allowed (except that nil is allowed too).
5402 LNAME is the LIsp-level variable name.
5403 VNAME is the name of the buffer slot.
5404 DOC is a dummy where you write the doc string as a comment. */
5405 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5406 defvar_per_buffer (lname, vname, type, 0)
5408 static void
5409 defvar_per_buffer (namestring, address, type, doc)
5410 char *namestring;
5411 Lisp_Object *address;
5412 Lisp_Object type;
5413 char *doc;
5415 Lisp_Object sym, val;
5416 int offset;
5418 sym = intern (namestring);
5419 val = allocate_misc ();
5420 offset = (char *)address - (char *)current_buffer;
5422 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5423 XBUFFER_OBJFWD (val)->offset = offset;
5424 XBUFFER_OBJFWD (val)->slottype = type;
5425 SET_SYMBOL_VALUE (sym, val);
5426 PER_BUFFER_SYMBOL (offset) = sym;
5428 if (PER_BUFFER_IDX (offset) == 0)
5429 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5430 slot of buffer_local_flags */
5431 abort ();
5435 /* initialize the buffer routines */
5436 void
5437 syms_of_buffer ()
5439 staticpro (&last_overlay_modification_hooks);
5440 last_overlay_modification_hooks
5441 = Fmake_vector (make_number (10), Qnil);
5443 staticpro (&Vbuffer_defaults);
5444 staticpro (&Vbuffer_local_symbols);
5445 staticpro (&Qfundamental_mode);
5446 staticpro (&Qmode_class);
5447 staticpro (&QSFundamental);
5448 staticpro (&Vbuffer_alist);
5449 staticpro (&Qprotected_field);
5450 staticpro (&Qpermanent_local);
5451 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5452 staticpro (&Qpermanent_local_hook);
5453 staticpro (&Qkill_buffer_hook);
5454 Qoverlayp = intern_c_string ("overlayp");
5455 staticpro (&Qoverlayp);
5456 Qevaporate = intern_c_string ("evaporate");
5457 staticpro (&Qevaporate);
5458 Qmodification_hooks = intern_c_string ("modification-hooks");
5459 staticpro (&Qmodification_hooks);
5460 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5461 staticpro (&Qinsert_in_front_hooks);
5462 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5463 staticpro (&Qinsert_behind_hooks);
5464 Qget_file_buffer = intern_c_string ("get-file-buffer");
5465 staticpro (&Qget_file_buffer);
5466 Qpriority = intern_c_string ("priority");
5467 staticpro (&Qpriority);
5468 Qwindow = intern_c_string ("window");
5469 staticpro (&Qwindow);
5470 Qbefore_string = intern_c_string ("before-string");
5471 staticpro (&Qbefore_string);
5472 Qafter_string = intern_c_string ("after-string");
5473 staticpro (&Qafter_string);
5474 Qfirst_change_hook = intern_c_string ("first-change-hook");
5475 staticpro (&Qfirst_change_hook);
5476 Qbefore_change_functions = intern_c_string ("before-change-functions");
5477 staticpro (&Qbefore_change_functions);
5478 Qafter_change_functions = intern_c_string ("after-change-functions");
5479 staticpro (&Qafter_change_functions);
5480 /* The next one is initialized in init_buffer_once. */
5481 staticpro (&Qucs_set_table_for_input);
5483 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5484 staticpro (&Qkill_buffer_query_functions);
5486 Fput (Qprotected_field, Qerror_conditions,
5487 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5488 Fput (Qprotected_field, Qerror_message,
5489 make_pure_c_string ("Attempt to modify a protected field"));
5491 /* All these use DEFVAR_LISP_NOPRO because the slots in
5492 buffer_defaults will all be marked via Vbuffer_defaults. */
5494 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5495 &buffer_defaults.mode_line_format,
5496 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5497 This is the same as (default-value 'mode-line-format). */);
5499 DEFVAR_LISP_NOPRO ("default-header-line-format",
5500 &buffer_defaults.header_line_format,
5501 doc: /* Default value of `header-line-format' for buffers that don't override it.
5502 This is the same as (default-value 'header-line-format). */);
5504 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5505 doc: /* Default value of `cursor-type' for buffers that don't override it.
5506 This is the same as (default-value 'cursor-type). */);
5508 DEFVAR_LISP_NOPRO ("default-line-spacing",
5509 &buffer_defaults.extra_line_spacing,
5510 doc: /* Default value of `line-spacing' for buffers that don't override it.
5511 This is the same as (default-value 'line-spacing). */);
5513 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5514 &buffer_defaults.cursor_in_non_selected_windows,
5515 doc: /* Default value of `cursor-in-non-selected-windows'.
5516 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5518 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5519 &buffer_defaults.abbrev_mode,
5520 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5521 This is the same as (default-value 'abbrev-mode). */);
5523 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5524 &buffer_defaults.ctl_arrow,
5525 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5526 This is the same as (default-value 'ctl-arrow). */);
5528 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5529 &buffer_defaults.direction_reversed,
5530 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5531 This is the same as (default-value 'direction-reversed). */);
5533 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5534 &buffer_defaults.enable_multibyte_characters,
5535 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5536 This is the same as (default-value 'enable-multibyte-characters). */);
5538 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5539 &buffer_defaults.buffer_file_coding_system,
5540 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5541 This is the same as (default-value 'buffer-file-coding-system). */);
5543 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5544 &buffer_defaults.truncate_lines,
5545 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5546 This is the same as (default-value 'truncate-lines). */);
5548 DEFVAR_LISP_NOPRO ("default-fill-column",
5549 &buffer_defaults.fill_column,
5550 doc: /* Default value of `fill-column' for buffers that do not override it.
5551 This is the same as (default-value 'fill-column). */);
5553 DEFVAR_LISP_NOPRO ("default-left-margin",
5554 &buffer_defaults.left_margin,
5555 doc: /* Default value of `left-margin' for buffers that do not override it.
5556 This is the same as (default-value 'left-margin). */);
5558 DEFVAR_LISP_NOPRO ("default-tab-width",
5559 &buffer_defaults.tab_width,
5560 doc: /* Default value of `tab-width' for buffers that do not override it.
5561 This is the same as (default-value 'tab-width). */);
5563 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5564 &buffer_defaults.case_fold_search,
5565 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5566 This is the same as (default-value 'case-fold-search). */);
5568 #ifdef DOS_NT
5569 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5570 &buffer_defaults.buffer_file_type,
5571 doc: /* Default file type for buffers that do not override it.
5572 This is the same as (default-value 'buffer-file-type).
5573 The file type is nil for text, t for binary. */);
5574 #endif
5576 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5577 &buffer_defaults.left_margin_cols,
5578 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5579 This is the same as (default-value 'left-margin-width). */);
5581 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5582 &buffer_defaults.right_margin_cols,
5583 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5584 This is the same as (default-value 'right-margin-width). */);
5586 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5587 &buffer_defaults.left_fringe_width,
5588 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5589 This is the same as (default-value 'left-fringe-width). */);
5591 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5592 &buffer_defaults.right_fringe_width,
5593 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5594 This is the same as (default-value 'right-fringe-width). */);
5596 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5597 &buffer_defaults.fringes_outside_margins,
5598 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5599 This is the same as (default-value 'fringes-outside-margins). */);
5601 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5602 &buffer_defaults.scroll_bar_width,
5603 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5604 This is the same as (default-value 'scroll-bar-width). */);
5606 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5607 &buffer_defaults.vertical_scroll_bar_type,
5608 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5609 This is the same as (default-value 'vertical-scroll-bar). */);
5611 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5612 &buffer_defaults.indicate_empty_lines,
5613 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5614 This is the same as (default-value 'indicate-empty-lines). */);
5616 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5617 &buffer_defaults.indicate_buffer_boundaries,
5618 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5619 This is the same as (default-value 'indicate-buffer-boundaries). */);
5621 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5622 &buffer_defaults.fringe_indicator_alist,
5623 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5624 This is the same as (default-value 'fringe-indicator-alist'). */);
5626 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5627 &buffer_defaults.fringe_cursor_alist,
5628 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5629 This is the same as (default-value 'fringe-cursor-alist'). */);
5631 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5632 &buffer_defaults.scroll_up_aggressively,
5633 doc: /* Default value of `scroll-up-aggressively'.
5634 This value applies in buffers that don't have their own local values.
5635 This is the same as (default-value 'scroll-up-aggressively). */);
5637 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5638 &buffer_defaults.scroll_down_aggressively,
5639 doc: /* Default value of `scroll-down-aggressively'.
5640 This value applies in buffers that don't have their own local values.
5641 This is the same as (default-value 'scroll-down-aggressively). */);
5643 DEFVAR_PER_BUFFER ("header-line-format",
5644 &current_buffer->header_line_format,
5645 Qnil,
5646 doc: /* Analogous to `mode-line-format', but controls the header line.
5647 The header line appears, optionally, at the top of a window;
5648 the mode line appears at the bottom. */);
5650 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5651 Qnil,
5652 doc: /* Template for displaying mode line for current buffer.
5653 Each buffer has its own value of this variable.
5654 Value may be nil, a string, a symbol or a list or cons cell.
5655 A value of nil means don't display a mode line.
5656 For a symbol, its value is used (but it is ignored if t or nil).
5657 A string appearing directly as the value of a symbol is processed verbatim
5658 in that the %-constructs below are not recognized.
5659 Note that unless the symbol is marked as a `risky-local-variable', all
5660 properties in any strings, as well as all :eval and :propertize forms
5661 in the value of that symbol will be ignored.
5662 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5663 is used as a mode line element. Be careful--FORM should not load any files,
5664 because that can cause an infinite recursion.
5665 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5666 with the specified properties PROPS applied.
5667 For a list whose car is a symbol, the symbol's value is taken,
5668 and if that is non-nil, the cadr of the list is processed recursively.
5669 Otherwise, the caddr of the list (if there is one) is processed.
5670 For a list whose car is a string or list, each element is processed
5671 recursively and the results are effectively concatenated.
5672 For a list whose car is an integer, the cdr of the list is processed
5673 and padded (if the number is positive) or truncated (if negative)
5674 to the width specified by that number.
5675 A string is printed verbatim in the mode line except for %-constructs:
5676 (%-constructs are allowed when the string is the entire mode-line-format
5677 or when it is found in a cons-cell or a list)
5678 %b -- print buffer name. %f -- print visited file name.
5679 %F -- print frame name.
5680 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5681 %& is like %*, but ignore read-only-ness.
5682 % means buffer is read-only and * means it is modified.
5683 For a modified read-only buffer, %* gives % and %+ gives *.
5684 %s -- print process status. %l -- print the current line number.
5685 %c -- print the current column number (this makes editing slower).
5686 To make the column number update correctly in all cases,
5687 `column-number-mode' must be non-nil.
5688 %i -- print the size of the buffer.
5689 %I -- like %i, but use k, M, G, etc., to abbreviate.
5690 %p -- print percent of buffer above top of window, or Top, Bot or All.
5691 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5692 or print Bottom or All.
5693 %n -- print Narrow if appropriate.
5694 %t -- visited file is text or binary (if OS supports this distinction).
5695 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5696 %Z -- like %z, but including the end-of-line format.
5697 %e -- print error message about full memory.
5698 %@ -- print @ or hyphen. @ means that default-directory is on a
5699 remote machine.
5700 %[ -- print one [ for each recursive editing level. %] similar.
5701 %% -- print %. %- -- print infinitely many dashes.
5702 Decimal digits after the % specify field width to which to pad. */);
5704 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5705 doc: /* *Value of `major-mode' for new buffers. */);
5707 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5708 make_number (Lisp_Symbol),
5709 doc: /* Symbol for current buffer's major mode.
5710 The default value (normally `fundamental-mode') affects new buffers.
5711 A value of nil means to use the current buffer's major mode, provided
5712 it is not marked as "special".
5714 When a mode is used by default, `find-file' switches to it before it
5715 reads the contents into the buffer and before it finishes setting up
5716 the buffer. Thus, the mode and its hooks should not expect certain
5717 variables such as `buffer-read-only' and `buffer-file-coding-system'
5718 to be set up. */);
5720 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5721 Qnil,
5722 doc: /* Pretty name of current buffer's major mode.
5723 Usually a string, but can use any of the constructs for `mode-line-format',
5724 which see.
5725 Format with `format-mode-line' to produce a string value. */);
5727 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5728 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5730 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5731 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5733 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5734 Qnil,
5735 doc: /* *Non-nil if searches and matches should ignore case. */);
5737 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5738 make_number (LISP_INT_TAG),
5739 doc: /* *Column beyond which automatic line-wrapping should happen.
5740 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5742 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5743 make_number (LISP_INT_TAG),
5744 doc: /* *Column for the default `indent-line-function' to indent to.
5745 Linefeed indents to this column in Fundamental mode. */);
5747 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5748 make_number (LISP_INT_TAG),
5749 doc: /* *Distance between tab stops (for display of tab characters), in columns.
5750 This should be an integer greater than zero. */);
5752 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5753 doc: /* *Non-nil means display control chars with uparrow.
5754 A value of nil means use backslash and octal digits.
5755 This variable does not apply to characters whose display is specified
5756 in the current display table (if there is one). */);
5758 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5759 &current_buffer->enable_multibyte_characters,
5760 Qnil,
5761 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5762 Otherwise they are regarded as unibyte. This affects the display,
5763 file I/O and the behavior of various editing commands.
5765 This variable is buffer-local but you cannot set it directly;
5766 use the function `set-buffer-multibyte' to change a buffer's representation.
5767 Changing its default value with `setq-default' is supported.
5768 See also variable `default-enable-multibyte-characters' and Info node
5769 `(elisp)Text Representations'. */);
5770 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5772 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5773 &current_buffer->buffer_file_coding_system, Qnil,
5774 doc: /* Coding system to be used for encoding the buffer contents on saving.
5775 This variable applies to saving the buffer, and also to `write-region'
5776 and other functions that use `write-region'.
5777 It does not apply to sending output to subprocesses, however.
5779 If this is nil, the buffer is saved without any code conversion
5780 unless some coding system is specified in `file-coding-system-alist'
5781 for the buffer file.
5783 If the text to be saved cannot be encoded as specified by this variable,
5784 an alternative encoding is selected by `select-safe-coding-system', which see.
5786 The variable `coding-system-for-write', if non-nil, overrides this variable.
5788 This variable is never applied to a way of decoding a file while reading it. */);
5790 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5791 Qnil,
5792 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5794 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5795 doc: /* *Non-nil means do not display continuation lines.
5796 Instead, give each line of text just one screen line.
5798 Note that this is overridden by the variable
5799 `truncate-partial-width-windows' if that variable is non-nil
5800 and this buffer is not full-frame width. */);
5802 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5803 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5804 When word-wrapping is on, continuation lines are wrapped at the space
5805 or tab character nearest to the right window edge.
5806 If nil, continuation lines are wrapped at the right screen edge.
5808 This variable has no effect if long lines are truncated (see
5809 `truncate-lines' and `truncate-partial-width-windows'). If you use
5810 word-wrapping, you might want to reduce the value of
5811 `truncate-partial-width-windows', since wrapping can make text readable
5812 in narrower windows. */);
5814 #ifdef DOS_NT
5815 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5816 Qnil,
5817 doc: /* Non-nil if the visited file is a binary file.
5818 This variable is meaningful on MS-DOG and Windows NT.
5819 On those systems, it is automatically local in every buffer.
5820 On other systems, this variable is normally always nil. */);
5821 #endif
5823 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5824 make_number (Lisp_String),
5825 doc: /* Name of default directory of current buffer. Should end with slash.
5826 To interactively change the default directory, use command `cd'. */);
5828 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5829 Qnil,
5830 doc: /* Function called (if non-nil) to perform auto-fill.
5831 It is called after self-inserting any character specified in
5832 the `auto-fill-chars' table.
5833 NOTE: This variable is not a hook;
5834 its value may not be a list of functions. */);
5836 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5837 make_number (Lisp_String),
5838 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5840 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5841 make_number (Lisp_String),
5842 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5843 The truename of a file is calculated by `file-truename'
5844 and then abbreviated with `abbreviate-file-name'. */);
5846 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5847 &current_buffer->auto_save_file_name,
5848 make_number (Lisp_String),
5849 doc: /* Name of file for auto-saving current buffer.
5850 If it is nil, that means don't auto-save this buffer. */);
5852 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5853 doc: /* Non-nil if this buffer is read-only. */);
5855 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5856 doc: /* Non-nil if this buffer's file has been backed up.
5857 Backing up is done before the first time the file is saved. */);
5859 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5860 make_number (LISP_INT_TAG),
5861 doc: /* Length of current buffer when last read in, saved or auto-saved.
5862 0 initially.
5863 -1 means auto-saving turned off until next real save.
5865 If you set this to -2, that means don't turn off auto-saving in this buffer
5866 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5867 you probably should set this to -2 in that buffer. */);
5869 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5870 Qnil,
5871 doc: /* Non-nil enables selective display.
5872 An integer N as value means display only lines
5873 that start with less than N columns of space.
5874 A value of t means that the character ^M makes itself and
5875 all the rest of the line invisible; also, when saving the buffer
5876 in a file, save the ^M as a newline. */);
5878 #ifndef old
5879 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5880 &current_buffer->selective_display_ellipses,
5881 Qnil,
5882 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5883 #endif
5885 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5886 doc: /* Non-nil if self-insertion should replace existing text.
5887 The value should be one of `overwrite-mode-textual',
5888 `overwrite-mode-binary', or nil.
5889 If it is `overwrite-mode-textual', self-insertion still
5890 inserts at the end of a line, and inserts when point is before a tab,
5891 until the tab is filled in.
5892 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5894 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5895 Qnil,
5896 doc: /* Display table that controls display of the contents of current buffer.
5898 If this variable is nil, the value of `standard-display-table' is used.
5899 Each window can have its own, overriding display table, see
5900 `set-window-display-table' and `window-display-table'.
5902 The display table is a char-table created with `make-display-table'.
5903 A char-table is an array indexed by character codes. Normal array
5904 primitives `aref' and `aset' can be used to access elements of a char-table.
5906 Each of the char-table elements control how to display the corresponding
5907 text character: the element at index C in the table says how to display
5908 the character whose code is C. Each element should be a vector of
5909 characters or nil. The value nil means display the character in the
5910 default fashion; otherwise, the characters from the vector are delivered
5911 to the screen instead of the original character.
5913 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5914 to display a capital Y instead of each X character.
5916 In addition, a char-table has six extra slots to control the display of:
5918 the end of a truncated screen line (extra-slot 0, a single character);
5919 the end of a continued line (extra-slot 1, a single character);
5920 the escape character used to display character codes in octal
5921 (extra-slot 2, a single character);
5922 the character used as an arrow for control characters (extra-slot 3,
5923 a single character);
5924 the decoration indicating the presence of invisible lines (extra-slot 4,
5925 a vector of characters);
5926 the character used to draw the border between side-by-side windows
5927 (extra-slot 5, a single character).
5929 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5931 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5932 Qnil,
5933 doc: /* *Width of left marginal area for display of a buffer.
5934 A value of nil means no marginal area. */);
5936 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5937 Qnil,
5938 doc: /* *Width of right marginal area for display of a buffer.
5939 A value of nil means no marginal area. */);
5941 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5942 Qnil,
5943 doc: /* *Width of this buffer's left fringe (in pixels).
5944 A value of 0 means no left fringe is shown in this buffer's window.
5945 A value of nil means to use the left fringe width from the window's frame. */);
5947 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5948 Qnil,
5949 doc: /* *Width of this buffer's right fringe (in pixels).
5950 A value of 0 means no right fringe is shown in this buffer's window.
5951 A value of nil means to use the right fringe width from the window's frame. */);
5953 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5954 Qnil,
5955 doc: /* *Non-nil means to display fringes outside display margins.
5956 A value of nil means to display fringes between margins and buffer text. */);
5958 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5959 Qnil,
5960 doc: /* *Width of this buffer's scroll bars in pixels.
5961 A value of nil means to use the scroll bar width from the window's frame. */);
5963 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5964 Qnil,
5965 doc: /* *Position of this buffer's vertical scroll bar.
5966 The value takes effect whenever you tell a window to display this buffer;
5967 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5969 A value of `left' or `right' means put the vertical scroll bar at that side
5970 of the window; a value of nil means don't show any vertical scroll bars.
5971 A value of t (the default) means do whatever the window's frame specifies. */);
5973 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5974 &current_buffer->indicate_empty_lines, Qnil,
5975 doc: /* *Visually indicate empty lines after the buffer end.
5976 If non-nil, a bitmap is displayed in the left fringe of a window on
5977 window-systems. */);
5979 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5980 &current_buffer->indicate_buffer_boundaries, Qnil,
5981 doc: /* *Visually indicate buffer boundaries and scrolling.
5982 If non-nil, the first and last line of the buffer are marked in the fringe
5983 of a window on window-systems with angle bitmaps, or if the window can be
5984 scrolled, the top and bottom line of the window are marked with up and down
5985 arrow bitmaps.
5987 If value is a symbol `left' or `right', both angle and arrow bitmaps
5988 are displayed in the left or right fringe, resp. Any other value
5989 that doesn't look like an alist means display the angle bitmaps in
5990 the left fringe but no arrows.
5992 You can exercise more precise control by using an alist as the
5993 value. Each alist element (INDICATOR . POSITION) specifies
5994 where to show one of the indicators. INDICATOR is one of `top',
5995 `bottom', `up', `down', or t, which specifies the default position,
5996 and POSITION is one of `left', `right', or nil, meaning do not show
5997 this indicator.
5999 For example, ((top . left) (t . right)) places the top angle bitmap in
6000 left fringe, the bottom angle bitmap in right fringe, and both arrow
6001 bitmaps in right fringe. To show just the angle bitmaps in the left
6002 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6004 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
6005 &current_buffer->fringe_indicator_alist, Qnil,
6006 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
6007 The value is an alist where each element (INDICATOR . BITMAPS)
6008 specifies the fringe bitmaps used to display a specific logical
6009 fringe indicator.
6011 INDICATOR specifies the logical indicator type which is one of the
6012 following symbols: `truncation' , `continuation', `overlay-arrow',
6013 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
6015 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
6016 the actual bitmap shown in the left or right fringe for the logical
6017 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
6018 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
6019 are used only for the `bottom' and `top-bottom' indicators when the
6020 last (only) line has no final newline. BITMAPS may also be a single
6021 symbol which is used in both left and right fringes. */);
6023 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
6024 &current_buffer->fringe_cursor_alist, Qnil,
6025 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
6026 The value is an alist where each element (CURSOR . BITMAP)
6027 specifies the fringe bitmaps used to display a specific logical
6028 cursor type in the fringe.
6030 CURSOR specifies the logical cursor type which is one of the following
6031 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6032 one is used to show a hollow cursor on narrow lines display lines
6033 where the normal hollow cursor will not fit.
6035 BITMAP is the corresponding fringe bitmap shown for the logical
6036 cursor type. */);
6038 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
6039 &current_buffer->scroll_up_aggressively, Qnil,
6040 doc: /* How far to scroll windows upward.
6041 If you move point off the bottom, the window scrolls automatically.
6042 This variable controls how far it scrolls. The value nil, the default,
6043 means scroll to center point. A fraction means scroll to put point
6044 that fraction of the window's height from the bottom of the window.
6045 When the value is 0.0, point goes at the bottom line, which in the
6046 simple case that you moved off with C-f means scrolling just one line.
6047 1.0 means point goes at the top, so that in that simple case, the
6048 window scrolls by a full window height. Meaningful values are
6049 between 0.0 and 1.0, inclusive. */);
6051 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6052 &current_buffer->scroll_down_aggressively, Qnil,
6053 doc: /* How far to scroll windows downward.
6054 If you move point off the top, the window scrolls automatically.
6055 This variable controls how far it scrolls. The value nil, the default,
6056 means scroll to center point. A fraction means scroll to put point
6057 that fraction of the window's height from the top of the window.
6058 When the value is 0.0, point goes at the top line, which in the
6059 simple case that you moved off with C-b means scrolling just one line.
6060 1.0 means point goes at the bottom, so that in that simple case, the
6061 window scrolls by a full window height. Meaningful values are
6062 between 0.0 and 1.0, inclusive. */);
6064 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6065 "Don't ask.");
6068 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6069 doc: /* List of functions to call before each text change.
6070 Two arguments are passed to each function: the positions of
6071 the beginning and end of the range of old text to be changed.
6072 \(For an insertion, the beginning and end are at the same place.)
6073 No information is given about the length of the text after the change.
6075 Buffer changes made while executing the `before-change-functions'
6076 don't call any before-change or after-change functions.
6077 That's because `inhibit-modification-hooks' is temporarily set non-nil.
6079 If an unhandled error happens in running these functions,
6080 the variable's value remains nil. That prevents the error
6081 from happening repeatedly and making Emacs nonfunctional. */);
6082 Vbefore_change_functions = Qnil;
6084 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6085 doc: /* List of functions to call after each text change.
6086 Three arguments are passed to each function: the positions of
6087 the beginning and end of the range of changed text,
6088 and the length in bytes of the pre-change text replaced by that range.
6089 \(For an insertion, the pre-change length is zero;
6090 for a deletion, that length is the number of bytes deleted,
6091 and the post-change beginning and end are at the same place.)
6093 Buffer changes made while executing the `after-change-functions'
6094 don't call any before-change or after-change functions.
6095 That's because `inhibit-modification-hooks' is temporarily set non-nil.
6097 If an unhandled error happens in running these functions,
6098 the variable's value remains nil. That prevents the error
6099 from happening repeatedly and making Emacs nonfunctional. */);
6100 Vafter_change_functions = Qnil;
6102 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6103 doc: /* A list of functions to call before changing a buffer which is unmodified.
6104 The functions are run using the `run-hooks' function. */);
6105 Vfirst_change_hook = Qnil;
6107 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6108 doc: /* List of undo entries in current buffer.
6109 Recent changes come first; older changes follow newer.
6111 An entry (BEG . END) represents an insertion which begins at
6112 position BEG and ends at position END.
6114 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6115 from (abs POSITION). If POSITION is positive, point was at the front
6116 of the text being deleted; if negative, point was at the end.
6118 An entry (t HIGH . LOW) indicates that the buffer previously had
6119 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6120 of the visited file's modification time, as of that time. If the
6121 modification time of the most recent save is different, this entry is
6122 obsolete.
6124 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6125 was modified between BEG and END. PROPERTY is the property name,
6126 and VALUE is the old value.
6128 An entry (apply FUN-NAME . ARGS) means undo the change with
6129 \(apply FUN-NAME ARGS).
6131 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6132 in the active region. BEG and END is the range affected by this entry
6133 and DELTA is the number of bytes added or deleted in that range by
6134 this change.
6136 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6137 was adjusted in position by the offset DISTANCE (an integer).
6139 An entry of the form POSITION indicates that point was at the buffer
6140 location given by the integer. Undoing an entry of this form places
6141 point at POSITION.
6143 Entries with value `nil' mark undo boundaries. The undo command treats
6144 the changes between two undo boundaries as a single step to be undone.
6146 If the value of the variable is t, undo information is not recorded. */);
6148 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6149 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6151 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6152 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6154 Normally, the line-motion functions work by scanning the buffer for
6155 newlines. Columnar operations (like `move-to-column' and
6156 `compute-motion') also work by scanning the buffer, summing character
6157 widths as they go. This works well for ordinary text, but if the
6158 buffer's lines are very long (say, more than 500 characters), these
6159 motion functions will take longer to execute. Emacs may also take
6160 longer to update the display.
6162 If `cache-long-line-scans' is non-nil, these motion functions cache the
6163 results of their scans, and consult the cache to avoid rescanning
6164 regions of the buffer until the text is modified. The caches are most
6165 beneficial when they prevent the most searching---that is, when the
6166 buffer contains long lines and large regions of characters with the
6167 same, fixed screen width.
6169 When `cache-long-line-scans' is non-nil, processing short lines will
6170 become slightly slower (because of the overhead of consulting the
6171 cache), and the caches will use memory roughly proportional to the
6172 number of newlines and characters whose screen width varies.
6174 The caches require no explicit maintenance; their accuracy is
6175 maintained internally by the Emacs primitives. Enabling or disabling
6176 the cache should not affect the behavior of any of the motion
6177 functions; it should only affect their performance. */);
6179 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6180 doc: /* Value of point before the last series of scroll operations, or nil. */);
6182 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6183 doc: /* List of formats to use when saving this buffer.
6184 Formats are defined by `format-alist'. This variable is
6185 set when a file is visited. */);
6187 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6188 &current_buffer->auto_save_file_format, Qnil,
6189 doc: /* *Format in which to write auto-save files.
6190 Should be a list of symbols naming formats that are defined in `format-alist'.
6191 If it is t, which is the default, auto-save files are written in the
6192 same format as a regular save would use. */);
6194 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6195 &current_buffer->invisibility_spec, Qnil,
6196 doc: /* Invisibility spec of this buffer.
6197 The default is t, which means that text is invisible
6198 if it has a non-nil `invisible' property.
6199 If the value is a list, a text character is invisible if its `invisible'
6200 property is an element in that list (or is a list with members in common).
6201 If an element is a cons cell of the form (PROP . ELLIPSIS),
6202 then characters with property value PROP are invisible,
6203 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6205 DEFVAR_PER_BUFFER ("buffer-display-count",
6206 &current_buffer->display_count, Qnil,
6207 doc: /* A number incremented each time this buffer is displayed in a window.
6208 The function `set-window-buffer' increments it. */);
6210 DEFVAR_PER_BUFFER ("buffer-display-time",
6211 &current_buffer->display_time, Qnil,
6212 doc: /* Time stamp updated each time this buffer is displayed in a window.
6213 The function `set-window-buffer' updates this variable
6214 to the value obtained by calling `current-time'.
6215 If the buffer has never been shown in a window, the value is nil. */);
6217 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6218 doc: /* */);
6219 Vtransient_mark_mode = Qnil;
6220 /* The docstring is in simple.el. If we put it here, it would be
6221 overwritten when transient-mark-mode is defined using
6222 define-minor-mode. */
6224 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6225 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6226 If the value is t, disregard `buffer-read-only' and all `read-only'
6227 text properties. If the value is a list, disregard `buffer-read-only'
6228 and disregard a `read-only' text property if the property value
6229 is a member of the list. */);
6230 Vinhibit_read_only = Qnil;
6232 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6233 doc: /* Cursor to use when this buffer is in the selected window.
6234 Values are interpreted as follows:
6236 t use the cursor specified for the frame
6237 nil don't display a cursor
6238 box display a filled box cursor
6239 hollow display a hollow box cursor
6240 bar display a vertical bar cursor with default width
6241 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6242 hbar display a horizontal bar cursor with default height
6243 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6244 ANYTHING ELSE display a hollow box cursor
6246 When the buffer is displayed in a non-selected window, the
6247 cursor's appearance is instead controlled by the variable
6248 `cursor-in-non-selected-windows'. */);
6250 DEFVAR_PER_BUFFER ("line-spacing",
6251 &current_buffer->extra_line_spacing, Qnil,
6252 doc: /* Additional space to put between lines when displaying a buffer.
6253 The space is measured in pixels, and put below lines on graphic displays,
6254 see `display-graphic-p'.
6255 If value is a floating point number, it specifies the spacing relative
6256 to the default frame line height. A value of nil means add no extra space. */);
6258 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6259 &current_buffer->cursor_in_non_selected_windows, Qnil,
6260 doc: /* *Cursor type to display in non-selected windows.
6261 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6263 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6264 doc: /* List of functions called with no args to query before killing a buffer.
6265 The buffer being killed will be current while the functions are running.
6266 If any of them returns nil, the buffer is not killed. */);
6267 Vkill_buffer_query_functions = Qnil;
6269 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6270 doc: /* Normal hook run before changing the major mode of a buffer.
6271 The function `kill-all-local-variables' runs this before doing anything else. */);
6272 Vchange_major_mode_hook = Qnil;
6273 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6274 staticpro (&Qchange_major_mode_hook);
6276 defsubr (&Sbuffer_live_p);
6277 defsubr (&Sbuffer_list);
6278 defsubr (&Sget_buffer);
6279 defsubr (&Sget_file_buffer);
6280 defsubr (&Sget_buffer_create);
6281 defsubr (&Smake_indirect_buffer);
6282 defsubr (&Sgenerate_new_buffer_name);
6283 defsubr (&Sbuffer_name);
6284 /*defsubr (&Sbuffer_number);*/
6285 defsubr (&Sbuffer_file_name);
6286 defsubr (&Sbuffer_base_buffer);
6287 defsubr (&Sbuffer_local_value);
6288 defsubr (&Sbuffer_local_variables);
6289 defsubr (&Sbuffer_modified_p);
6290 defsubr (&Sset_buffer_modified_p);
6291 defsubr (&Sbuffer_modified_tick);
6292 defsubr (&Sbuffer_chars_modified_tick);
6293 defsubr (&Srename_buffer);
6294 defsubr (&Sother_buffer);
6295 defsubr (&Sbuffer_enable_undo);
6296 defsubr (&Skill_buffer);
6297 defsubr (&Sset_buffer_major_mode);
6298 defsubr (&Sswitch_to_buffer);
6299 defsubr (&Scurrent_buffer);
6300 defsubr (&Sset_buffer);
6301 defsubr (&Sbarf_if_buffer_read_only);
6302 defsubr (&Sbury_buffer);
6303 defsubr (&Serase_buffer);
6304 defsubr (&Sbuffer_swap_text);
6305 defsubr (&Sset_buffer_multibyte);
6306 defsubr (&Skill_all_local_variables);
6308 defsubr (&Soverlayp);
6309 defsubr (&Smake_overlay);
6310 defsubr (&Sdelete_overlay);
6311 defsubr (&Smove_overlay);
6312 defsubr (&Soverlay_start);
6313 defsubr (&Soverlay_end);
6314 defsubr (&Soverlay_buffer);
6315 defsubr (&Soverlay_properties);
6316 defsubr (&Soverlays_at);
6317 defsubr (&Soverlays_in);
6318 defsubr (&Snext_overlay_change);
6319 defsubr (&Sprevious_overlay_change);
6320 defsubr (&Soverlay_recenter);
6321 defsubr (&Soverlay_lists);
6322 defsubr (&Soverlay_get);
6323 defsubr (&Soverlay_put);
6324 defsubr (&Srestore_buffer_modified_p);
6327 void
6328 keys_of_buffer ()
6330 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6331 initial_define_key (control_x_map, 'k', "kill-buffer");
6333 /* This must not be in syms_of_buffer, because Qdisabled is not
6334 initialized when that function gets called. */
6335 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6338 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6339 (do not change this comment) */