* src/mem-limits.h: Remove duplicated includes.
[emacs.git] / src / buffer.c
blob9dd396e6c0872f823c82e36015c67a5b896b7ec2
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
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 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #include "lisp.h"
36 #include "intervals.h"
37 #include "window.h"
38 #include "commands.h"
39 #include "buffer.h"
40 #include "character.h"
41 #include "region-cache.h"
42 #include "indent.h"
43 #include "blockinput.h"
44 #include "keyboard.h"
45 #include "keymap.h"
46 #include "frame.h"
48 struct buffer *current_buffer; /* the current buffer */
50 /* First buffer in chain of all buffers (in reverse order of creation).
51 Threaded through ->next. */
53 struct buffer *all_buffers;
55 /* This structure holds the default values of the buffer-local variables
56 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
57 The default value occupies the same slot in this structure
58 as an individual buffer's value occupies in that buffer.
59 Setting the default value also goes through the alist of buffers
60 and stores into each buffer that does not say it has a local value. */
62 DECL_ALIGN (struct buffer, buffer_defaults);
64 /* A Lisp_Object pointer to the above, used for staticpro */
66 static Lisp_Object Vbuffer_defaults;
68 /* This structure marks which slots in a buffer have corresponding
69 default values in buffer_defaults.
70 Each such slot has a nonzero value in this structure.
71 The value has only one nonzero bit.
73 When a buffer has its own local value for a slot,
74 the entry for that slot (found in the same slot in this structure)
75 is turned on in the buffer's local_flags array.
77 If a slot in this structure is -1, then even though there may
78 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
79 and the corresponding slot in buffer_defaults is not used.
81 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
82 zero, that is a bug */
84 struct buffer buffer_local_flags;
86 /* This structure holds the names of symbols whose values may be
87 buffer-local. It is indexed and accessed in the same way as the above. */
89 DECL_ALIGN (struct buffer, buffer_local_symbols);
91 /* A Lisp_Object pointer to the above, used for staticpro */
92 static Lisp_Object Vbuffer_local_symbols;
94 /* Return the symbol of the per-buffer variable at offset OFFSET in
95 the buffer structure. */
97 #define PER_BUFFER_SYMBOL(OFFSET) \
98 (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
100 /* Flags indicating which built-in buffer-local variables
101 are permanent locals. */
102 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
104 /* Number of per-buffer variables used. */
106 int last_per_buffer_idx;
108 EXFUN (Fset_buffer, 1);
109 void set_buffer_internal (struct buffer *b);
110 void set_buffer_internal_1 (struct buffer *b);
111 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
112 int after, Lisp_Object arg1,
113 Lisp_Object arg2, Lisp_Object arg3);
114 static void swap_out_buffer_local_variables (struct buffer *b);
115 static void reset_buffer_local_variables (struct buffer *b, int permanent_too);
117 /* Alist of all buffer names vs the buffers. */
118 /* This used to be a variable, but is no longer,
119 to prevent lossage due to user rplac'ing this alist or its elements. */
120 Lisp_Object Vbuffer_alist;
122 /* Functions to call before and after each text change. */
123 Lisp_Object Vbefore_change_functions;
124 Lisp_Object Vafter_change_functions;
126 Lisp_Object Vtransient_mark_mode;
128 /* t means ignore all read-only text properties.
129 A list means ignore such a property if its value is a member of the list.
130 Any non-nil value means ignore buffer-read-only. */
131 Lisp_Object Vinhibit_read_only;
133 /* List of functions to call that can query about killing a buffer.
134 If any of these functions returns nil, we don't kill it. */
135 Lisp_Object Vkill_buffer_query_functions;
136 Lisp_Object Qkill_buffer_query_functions;
138 /* Hook run before changing a major mode. */
139 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
141 /* List of functions to call before changing an unmodified buffer. */
142 Lisp_Object Vfirst_change_hook;
144 Lisp_Object Qfirst_change_hook;
145 Lisp_Object Qbefore_change_functions;
146 Lisp_Object Qafter_change_functions;
147 Lisp_Object Qucs_set_table_for_input;
149 /* If nonzero, all modification hooks are suppressed. */
150 int inhibit_modification_hooks;
152 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
153 Lisp_Object Qpermanent_local_hook;
155 Lisp_Object Qprotected_field;
157 Lisp_Object QSFundamental; /* A string "Fundamental" */
159 Lisp_Object Qkill_buffer_hook;
161 Lisp_Object Qget_file_buffer;
163 Lisp_Object Qoverlayp;
165 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
167 Lisp_Object Qmodification_hooks;
168 Lisp_Object Qinsert_in_front_hooks;
169 Lisp_Object Qinsert_behind_hooks;
171 static void alloc_buffer_text (struct buffer *, size_t);
172 static void free_buffer_text (struct buffer *b);
173 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
174 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
175 static Lisp_Object buffer_lisp_local_variables (struct buffer *);
177 /* For debugging; temporary. See set_buffer_internal. */
178 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
180 void
181 nsberror (Lisp_Object spec)
183 if (STRINGP (spec))
184 error ("No buffer named %s", SDATA (spec));
185 error ("Invalid buffer argument");
188 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
189 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
190 Value is nil if OBJECT is not a buffer or if it has been killed. */)
191 (Lisp_Object object)
193 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
194 ? Qt : Qnil);
197 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
198 doc: /* Return a list of all existing live buffers.
199 If the optional arg FRAME is a frame, we return the buffer list
200 in the proper order for that frame: the buffers in FRAME's `buffer-list'
201 frame parameter come first, followed by the rest of the buffers. */)
202 (Lisp_Object frame)
204 Lisp_Object general;
205 general = Fmapcar (Qcdr, Vbuffer_alist);
207 if (FRAMEP (frame))
209 Lisp_Object framelist, prevlist, tail;
210 Lisp_Object args[3];
212 CHECK_FRAME (frame);
214 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
215 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
217 /* Remove from GENERAL any buffer that duplicates one in
218 FRAMELIST or PREVLIST. */
219 tail = framelist;
220 while (CONSP (tail))
222 general = Fdelq (XCAR (tail), general);
223 tail = XCDR (tail);
225 tail = prevlist;
226 while (CONSP (tail))
228 general = Fdelq (XCAR (tail), general);
229 tail = XCDR (tail);
232 args[0] = framelist;
233 args[1] = general;
234 args[2] = prevlist;
235 return Fnconc (3, args);
238 return general;
241 /* Like Fassoc, but use Fstring_equal to compare
242 (which ignores text properties),
243 and don't ever QUIT. */
245 static Lisp_Object
246 assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
248 register Lisp_Object tail;
249 for (tail = list; CONSP (tail); tail = XCDR (tail))
251 register Lisp_Object elt, tem;
252 elt = XCAR (tail);
253 tem = Fstring_equal (Fcar (elt), key);
254 if (!NILP (tem))
255 return elt;
257 return Qnil;
260 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
261 doc: /* Return the buffer named BUFFER-OR-NAME.
262 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
263 is a string and there is no buffer with that name, return nil. If
264 BUFFER-OR-NAME is a buffer, return it as given. */)
265 (register Lisp_Object buffer_or_name)
267 if (BUFFERP (buffer_or_name))
268 return buffer_or_name;
269 CHECK_STRING (buffer_or_name);
271 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
274 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
275 doc: /* Return the buffer visiting file FILENAME (a string).
276 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
277 If there is no such live buffer, return nil.
278 See also `find-buffer-visiting'. */)
279 (register Lisp_Object filename)
281 register Lisp_Object tail, buf, tem;
282 Lisp_Object handler;
284 CHECK_STRING (filename);
285 filename = Fexpand_file_name (filename, Qnil);
287 /* If the file name has special constructs in it,
288 call the corresponding file handler. */
289 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
290 if (!NILP (handler))
291 return call2 (handler, Qget_file_buffer, filename);
293 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
295 buf = Fcdr (XCAR (tail));
296 if (!BUFFERP (buf)) continue;
297 if (!STRINGP (XBUFFER (buf)->filename)) continue;
298 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
299 if (!NILP (tem))
300 return buf;
302 return Qnil;
305 Lisp_Object
306 get_truename_buffer (register Lisp_Object filename)
308 register Lisp_Object tail, buf, tem;
310 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
312 buf = Fcdr (XCAR (tail));
313 if (!BUFFERP (buf)) continue;
314 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
315 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
316 if (!NILP (tem))
317 return buf;
319 return Qnil;
322 /* Incremented for each buffer created, to assign the buffer number. */
323 int buffer_count;
325 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
326 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
327 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
328 return that buffer. If no such buffer exists, create a new buffer with
329 that name and return it. If BUFFER-OR-NAME starts with a space, the new
330 buffer does not keep undo information.
332 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
333 even if it is dead. The return value is never nil. */)
334 (register Lisp_Object buffer_or_name)
336 register Lisp_Object buffer, name;
337 register struct buffer *b;
339 buffer = Fget_buffer (buffer_or_name);
340 if (!NILP (buffer))
341 return buffer;
343 if (SCHARS (buffer_or_name) == 0)
344 error ("Empty string for buffer name is not allowed");
346 b = allocate_buffer ();
348 /* An ordinary buffer uses its own struct buffer_text. */
349 b->text = &b->own_text;
350 b->base_buffer = 0;
352 BUF_GAP_SIZE (b) = 20;
353 BLOCK_INPUT;
354 /* We allocate extra 1-byte at the tail and keep it always '\0' for
355 anchoring a search. */
356 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
357 UNBLOCK_INPUT;
358 if (! BUF_BEG_ADDR (b))
359 buffer_memory_full ();
361 BUF_PT (b) = BEG;
362 BUF_GPT (b) = BEG;
363 BUF_BEGV (b) = BEG;
364 BUF_ZV (b) = BEG;
365 BUF_Z (b) = BEG;
366 BUF_PT_BYTE (b) = BEG_BYTE;
367 BUF_GPT_BYTE (b) = BEG_BYTE;
368 BUF_BEGV_BYTE (b) = BEG_BYTE;
369 BUF_ZV_BYTE (b) = BEG_BYTE;
370 BUF_Z_BYTE (b) = BEG_BYTE;
371 BUF_MODIFF (b) = 1;
372 BUF_CHARS_MODIFF (b) = 1;
373 BUF_OVERLAY_MODIFF (b) = 1;
374 BUF_SAVE_MODIFF (b) = 1;
375 BUF_INTERVALS (b) = 0;
376 BUF_UNCHANGED_MODIFIED (b) = 1;
377 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
378 BUF_END_UNCHANGED (b) = 0;
379 BUF_BEG_UNCHANGED (b) = 0;
380 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
382 b->newline_cache = 0;
383 b->width_run_cache = 0;
384 b->width_table = Qnil;
385 b->prevent_redisplay_optimizations_p = 1;
387 /* Put this on the chain of all buffers including killed ones. */
388 b->next = all_buffers;
389 all_buffers = b;
391 /* An ordinary buffer normally doesn't need markers
392 to handle BEGV and ZV. */
393 b->pt_marker = Qnil;
394 b->begv_marker = Qnil;
395 b->zv_marker = Qnil;
397 name = Fcopy_sequence (buffer_or_name);
398 STRING_SET_INTERVALS (name, NULL_INTERVAL);
399 b->name = name;
401 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
403 reset_buffer (b);
404 reset_buffer_local_variables (b, 1);
406 b->mark = Fmake_marker ();
407 BUF_MARKERS (b) = NULL;
408 b->name = name;
410 /* Put this in the alist of all live buffers. */
411 XSETBUFFER (buffer, b);
412 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
414 /* An error in calling the function here (should someone redefine it)
415 can lead to infinite regress until you run out of stack. rms
416 says that's not worth protecting against. */
417 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
418 /* buffer is on buffer-alist, so no gcpro. */
419 call1 (Qucs_set_table_for_input, buffer);
421 return buffer;
425 /* Return a list of overlays which is a copy of the overlay list
426 LIST, but for buffer B. */
428 static struct Lisp_Overlay *
429 copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
431 Lisp_Object buffer;
432 struct Lisp_Overlay *result = NULL, *tail = NULL;
434 XSETBUFFER (buffer, b);
436 for (; list; list = list->next)
438 Lisp_Object overlay, start, end, old_overlay;
439 EMACS_INT charpos;
441 XSETMISC (old_overlay, list);
442 charpos = marker_position (OVERLAY_START (old_overlay));
443 start = Fmake_marker ();
444 Fset_marker (start, make_number (charpos), buffer);
445 XMARKER (start)->insertion_type
446 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
448 charpos = marker_position (OVERLAY_END (old_overlay));
449 end = Fmake_marker ();
450 Fset_marker (end, make_number (charpos), buffer);
451 XMARKER (end)->insertion_type
452 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
454 overlay = allocate_misc ();
455 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
456 OVERLAY_START (overlay) = start;
457 OVERLAY_END (overlay) = end;
458 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
459 XOVERLAY (overlay)->next = NULL;
461 if (tail)
462 tail = tail->next = XOVERLAY (overlay);
463 else
464 result = tail = XOVERLAY (overlay);
467 return result;
471 /* Clone per-buffer values of buffer FROM.
473 Buffer TO gets the same per-buffer values as FROM, with the
474 following exceptions: (1) TO's name is left untouched, (2) markers
475 are copied and made to refer to TO, and (3) overlay lists are
476 copied. */
478 static void
479 clone_per_buffer_values (struct buffer *from, struct buffer *to)
481 Lisp_Object to_buffer;
482 int offset;
484 XSETBUFFER (to_buffer, to);
486 /* buffer-local Lisp variables start at `undo_list',
487 tho only the ones from `name' on are GC'd normally. */
488 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
489 offset < sizeof *to;
490 offset += sizeof (Lisp_Object))
492 Lisp_Object obj;
494 /* Don't touch the `name' which should be unique for every buffer. */
495 if (offset == PER_BUFFER_VAR_OFFSET (name))
496 continue;
498 obj = PER_BUFFER_VALUE (from, offset);
499 if (MARKERP (obj) && XMARKER (obj)->buffer == from)
501 struct Lisp_Marker *m = XMARKER (obj);
502 obj = Fmake_marker ();
503 XMARKER (obj)->insertion_type = m->insertion_type;
504 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
507 PER_BUFFER_VALUE (to, offset) = obj;
510 memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
512 to->overlays_before = copy_overlays (to, from->overlays_before);
513 to->overlays_after = copy_overlays (to, from->overlays_after);
515 /* Get (a copy of) the alist of Lisp-level local variables of FROM
516 and install that in TO. */
517 to->local_var_alist = buffer_lisp_local_variables (from);
520 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
521 2, 3,
522 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
523 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
524 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
525 NAME should be a string which is not the name of an existing buffer.
526 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
527 such as major and minor modes, in the indirect buffer.
528 CLONE nil means the indirect buffer's state is reset to default values. */)
529 (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
531 Lisp_Object buf, tem;
532 struct buffer *b;
534 CHECK_STRING (name);
535 buf = Fget_buffer (name);
536 if (!NILP (buf))
537 error ("Buffer name `%s' is in use", SDATA (name));
539 tem = base_buffer;
540 base_buffer = Fget_buffer (base_buffer);
541 if (NILP (base_buffer))
542 error ("No such buffer: `%s'", SDATA (tem));
543 if (NILP (XBUFFER (base_buffer)->name))
544 error ("Base buffer has been killed");
546 if (SCHARS (name) == 0)
547 error ("Empty string for buffer name is not allowed");
549 b = allocate_buffer ();
551 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
552 ? XBUFFER (base_buffer)->base_buffer
553 : XBUFFER (base_buffer));
555 /* Use the base buffer's text object. */
556 b->text = b->base_buffer->text;
558 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
559 BUF_ZV (b) = BUF_ZV (b->base_buffer);
560 BUF_PT (b) = BUF_PT (b->base_buffer);
561 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
562 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
563 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
565 b->newline_cache = 0;
566 b->width_run_cache = 0;
567 b->width_table = Qnil;
569 /* Put this on the chain of all buffers including killed ones. */
570 b->next = all_buffers;
571 all_buffers = b;
573 name = Fcopy_sequence (name);
574 STRING_SET_INTERVALS (name, NULL_INTERVAL);
575 b->name = name;
577 reset_buffer (b);
578 reset_buffer_local_variables (b, 1);
580 /* Put this in the alist of all live buffers. */
581 XSETBUFFER (buf, b);
582 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
584 b->mark = Fmake_marker ();
585 b->name = name;
587 /* The multibyte status belongs to the base buffer. */
588 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
590 /* Make sure the base buffer has markers for its narrowing. */
591 if (NILP (b->base_buffer->pt_marker))
593 b->base_buffer->pt_marker = Fmake_marker ();
594 set_marker_both (b->base_buffer->pt_marker, base_buffer,
595 BUF_PT (b->base_buffer),
596 BUF_PT_BYTE (b->base_buffer));
598 if (NILP (b->base_buffer->begv_marker))
600 b->base_buffer->begv_marker = Fmake_marker ();
601 set_marker_both (b->base_buffer->begv_marker, base_buffer,
602 BUF_BEGV (b->base_buffer),
603 BUF_BEGV_BYTE (b->base_buffer));
605 if (NILP (b->base_buffer->zv_marker))
607 b->base_buffer->zv_marker = Fmake_marker ();
608 set_marker_both (b->base_buffer->zv_marker, base_buffer,
609 BUF_ZV (b->base_buffer),
610 BUF_ZV_BYTE (b->base_buffer));
611 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
614 if (NILP (clone))
616 /* Give the indirect buffer markers for its narrowing. */
617 b->pt_marker = Fmake_marker ();
618 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
619 b->begv_marker = Fmake_marker ();
620 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
621 b->zv_marker = Fmake_marker ();
622 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
623 XMARKER (b->zv_marker)->insertion_type = 1;
625 else
627 struct buffer *old_b = current_buffer;
629 clone_per_buffer_values (b->base_buffer, b);
630 b->filename = Qnil;
631 b->file_truename = Qnil;
632 b->display_count = make_number (0);
633 b->backed_up = Qnil;
634 b->auto_save_file_name = Qnil;
635 set_buffer_internal_1 (b);
636 Fset (intern ("buffer-save-without-query"), Qnil);
637 Fset (intern ("buffer-file-number"), Qnil);
638 Fset (intern ("buffer-stale-function"), Qnil);
639 set_buffer_internal_1 (old_b);
642 return buf;
645 void
646 delete_all_overlays (struct buffer *b)
648 Lisp_Object overlay;
650 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
651 have to empty the list, otherwise we end up with overlays that
652 think they belong to this buffer while the buffer doesn't know about
653 them any more. */
654 while (b->overlays_before)
656 XSETMISC (overlay, b->overlays_before);
657 Fdelete_overlay (overlay);
659 while (b->overlays_after)
661 XSETMISC (overlay, b->overlays_after);
662 Fdelete_overlay (overlay);
664 eassert (b->overlays_before == NULL);
665 eassert (b->overlays_after == NULL);
668 /* Reinitialize everything about a buffer except its name and contents
669 and local variables.
670 If called on an already-initialized buffer, the list of overlays
671 should be deleted before calling this function, otherwise we end up
672 with overlays that claim to belong to the buffer but the buffer
673 claims it doesn't belong to it. */
675 void
676 reset_buffer (register struct buffer *b)
678 b->filename = Qnil;
679 b->file_truename = Qnil;
680 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
681 b->modtime = 0;
682 b->modtime_size = -1;
683 XSETFASTINT (b->save_length, 0);
684 b->last_window_start = 1;
685 /* It is more conservative to start out "changed" than "unchanged". */
686 b->clip_changed = 0;
687 b->prevent_redisplay_optimizations_p = 1;
688 b->backed_up = Qnil;
689 BUF_AUTOSAVE_MODIFF (b) = 0;
690 b->auto_save_failure_time = -1;
691 b->auto_save_file_name = Qnil;
692 b->read_only = Qnil;
693 b->overlays_before = NULL;
694 b->overlays_after = NULL;
695 b->overlay_center = BEG;
696 b->mark_active = Qnil;
697 b->point_before_scroll = Qnil;
698 b->file_format = Qnil;
699 b->auto_save_file_format = Qt;
700 b->last_selected_window = Qnil;
701 XSETINT (b->display_count, 0);
702 b->display_time = Qnil;
703 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
704 b->cursor_type = buffer_defaults.cursor_type;
705 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
707 b->display_error_modiff = 0;
710 /* Reset buffer B's local variables info.
711 Don't use this on a buffer that has already been in use;
712 it does not treat permanent locals consistently.
713 Instead, use Fkill_all_local_variables.
715 If PERMANENT_TOO is 1, then we reset permanent
716 buffer-local variables. If PERMANENT_TOO is 0,
717 we preserve those. */
719 static void
720 reset_buffer_local_variables (register struct buffer *b, int permanent_too)
722 register int offset;
723 int i;
725 /* Reset the major mode to Fundamental, together with all the
726 things that depend on the major mode.
727 default-major-mode is handled at a higher level.
728 We ignore it here. */
729 b->major_mode = Qfundamental_mode;
730 b->keymap = Qnil;
731 b->mode_name = QSFundamental;
732 b->minor_modes = Qnil;
734 /* If the standard case table has been altered and invalidated,
735 fix up its insides first. */
736 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
737 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
738 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
739 Fset_standard_case_table (Vascii_downcase_table);
741 b->downcase_table = Vascii_downcase_table;
742 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
743 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
744 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
745 b->invisibility_spec = Qt;
746 #ifndef DOS_NT
747 b->buffer_file_type = Qnil;
748 #endif
750 /* Reset all (or most) per-buffer variables to their defaults. */
751 if (permanent_too)
752 b->local_var_alist = Qnil;
753 else
755 Lisp_Object tmp, prop, last = Qnil;
756 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
757 if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
759 /* If permanent-local, keep it. */
760 last = tmp;
761 if (EQ (prop, Qpermanent_local_hook))
763 /* This is a partially permanent hook variable.
764 Preserve only the elements that want to be preserved. */
765 Lisp_Object list, newlist;
766 list = XCDR (XCAR (tmp));
767 if (!CONSP (list))
768 newlist = list;
769 else
770 for (newlist = Qnil; CONSP (list); list = XCDR (list))
772 Lisp_Object elt = XCAR (list);
773 /* Preserve element ELT if it's t,
774 if it is a function with a `permanent-local-hook' property,
775 or if it's not a symbol. */
776 if (! SYMBOLP (elt)
777 || EQ (elt, Qt)
778 || !NILP (Fget (elt, Qpermanent_local_hook)))
779 newlist = Fcons (elt, newlist);
781 XSETCDR (XCAR (tmp), Fnreverse (newlist));
784 /* Delete this local variable. */
785 else if (NILP (last))
786 b->local_var_alist = XCDR (tmp);
787 else
788 XSETCDR (last, XCDR (tmp));
791 for (i = 0; i < last_per_buffer_idx; ++i)
792 if (permanent_too || buffer_permanent_local_flags[i] == 0)
793 SET_PER_BUFFER_VALUE_P (b, i, 0);
795 /* For each slot that has a default value,
796 copy that into the slot. */
798 /* buffer-local Lisp variables start at `undo_list',
799 tho only the ones from `name' on are GC'd normally. */
800 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
801 offset < sizeof *b;
802 offset += sizeof (Lisp_Object))
804 int idx = PER_BUFFER_IDX (offset);
805 if ((idx > 0
806 && (permanent_too
807 || buffer_permanent_local_flags[idx] == 0)))
808 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
812 /* We split this away from generate-new-buffer, because rename-buffer
813 and set-visited-file-name ought to be able to use this to really
814 rename the buffer properly. */
816 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
817 1, 2, 0,
818 doc: /* Return a string that is the name of no existing buffer based on NAME.
819 If there is no live buffer named NAME, then return NAME.
820 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
821 \(starting at 2) until an unused name is found, and then return that name.
822 Optional second argument IGNORE specifies a name that is okay to use (if
823 it is in the sequence to be tried) even if a buffer with that name exists. */)
824 (register Lisp_Object name, Lisp_Object ignore)
826 register Lisp_Object gentemp, tem;
827 int count;
828 char number[10];
830 CHECK_STRING (name);
832 tem = Fstring_equal (name, ignore);
833 if (!NILP (tem))
834 return name;
835 tem = Fget_buffer (name);
836 if (NILP (tem))
837 return name;
839 count = 1;
840 while (1)
842 sprintf (number, "<%d>", ++count);
843 gentemp = concat2 (name, build_string (number));
844 tem = Fstring_equal (gentemp, ignore);
845 if (!NILP (tem))
846 return gentemp;
847 tem = Fget_buffer (gentemp);
848 if (NILP (tem))
849 return gentemp;
854 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
855 doc: /* Return the name of BUFFER, as a string.
856 BUFFER defaults to the current buffer.
857 Return nil if BUFFER has been killed. */)
858 (register Lisp_Object buffer)
860 if (NILP (buffer))
861 return current_buffer->name;
862 CHECK_BUFFER (buffer);
863 return XBUFFER (buffer)->name;
866 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
867 doc: /* Return name of file BUFFER is visiting, or nil if none.
868 No argument or nil as argument means use the current buffer. */)
869 (register Lisp_Object buffer)
871 if (NILP (buffer))
872 return current_buffer->filename;
873 CHECK_BUFFER (buffer);
874 return XBUFFER (buffer)->filename;
877 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
878 0, 1, 0,
879 doc: /* Return the base buffer of indirect buffer BUFFER.
880 If BUFFER is not indirect, return nil.
881 BUFFER defaults to the current buffer. */)
882 (register Lisp_Object buffer)
884 struct buffer *base;
885 Lisp_Object base_buffer;
887 if (NILP (buffer))
888 base = current_buffer->base_buffer;
889 else
891 CHECK_BUFFER (buffer);
892 base = XBUFFER (buffer)->base_buffer;
895 if (! base)
896 return Qnil;
897 XSETBUFFER (base_buffer, base);
898 return base_buffer;
901 DEFUN ("buffer-local-value", Fbuffer_local_value,
902 Sbuffer_local_value, 2, 2, 0,
903 doc: /* Return the value of VARIABLE in BUFFER.
904 If VARIABLE does not have a buffer-local binding in BUFFER, the value
905 is the default binding of the variable. */)
906 (register Lisp_Object variable, register Lisp_Object buffer)
908 register struct buffer *buf;
909 register Lisp_Object result;
910 struct Lisp_Symbol *sym;
912 CHECK_SYMBOL (variable);
913 CHECK_BUFFER (buffer);
914 buf = XBUFFER (buffer);
915 sym = XSYMBOL (variable);
917 start:
918 switch (sym->redirect)
920 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
921 case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
922 case SYMBOL_LOCALIZED:
923 { /* Look in local_var_alist. */
924 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
925 XSETSYMBOL (variable, sym); /* Update In case of aliasing. */
926 result = Fassoc (variable, buf->local_var_alist);
927 if (!NILP (result))
929 if (blv->fwd)
930 { /* What binding is loaded right now? */
931 Lisp_Object current_alist_element = blv->valcell;
933 /* The value of the currently loaded binding is not
934 stored in it, but rather in the realvalue slot.
935 Store that value into the binding it belongs to
936 in case that is the one we are about to use. */
938 XSETCDR (current_alist_element,
939 do_symval_forwarding (blv->fwd));
941 /* Now get the (perhaps updated) value out of the binding. */
942 result = XCDR (result);
944 else
945 result = Fdefault_value (variable);
946 break;
948 case SYMBOL_FORWARDED:
950 union Lisp_Fwd *fwd = SYMBOL_FWD (sym);
951 if (BUFFER_OBJFWDP (fwd))
952 result = PER_BUFFER_VALUE (buf, XBUFFER_OBJFWD (fwd)->offset);
953 else
954 result = Fdefault_value (variable);
955 break;
957 default: abort ();
960 if (!EQ (result, Qunbound))
961 return result;
963 xsignal1 (Qvoid_variable, variable);
966 /* Return an alist of the Lisp-level buffer-local bindings of
967 buffer BUF. That is, don't include the variables maintained
968 in special slots in the buffer object. */
970 static Lisp_Object
971 buffer_lisp_local_variables (struct buffer *buf)
973 Lisp_Object result = Qnil;
974 register Lisp_Object tail;
975 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
977 Lisp_Object val, elt;
979 elt = XCAR (tail);
981 /* Reference each variable in the alist in buf.
982 If inquiring about the current buffer, this gets the current values,
983 so store them into the alist so the alist is up to date.
984 If inquiring about some other buffer, this swaps out any values
985 for that buffer, making the alist up to date automatically. */
986 val = find_symbol_value (XCAR (elt));
987 /* Use the current buffer value only if buf is the current buffer. */
988 if (buf != current_buffer)
989 val = XCDR (elt);
991 result = Fcons (Fcons (XCAR (elt), val), result);
994 return result;
997 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
998 Sbuffer_local_variables, 0, 1, 0,
999 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1000 Most elements look like (SYMBOL . VALUE), describing one variable.
1001 For a symbol that is locally unbound, just the symbol appears in the value.
1002 Note that storing new VALUEs in these elements doesn't change the variables.
1003 No argument or nil as argument means use current buffer as BUFFER. */)
1004 (register Lisp_Object buffer)
1006 register struct buffer *buf;
1007 register Lisp_Object result;
1009 if (NILP (buffer))
1010 buf = current_buffer;
1011 else
1013 CHECK_BUFFER (buffer);
1014 buf = XBUFFER (buffer);
1017 result = buffer_lisp_local_variables (buf);
1019 /* Add on all the variables stored in special slots. */
1021 int offset, idx;
1023 /* buffer-local Lisp variables start at `undo_list',
1024 tho only the ones from `name' on are GC'd normally. */
1025 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1026 offset < sizeof (struct buffer);
1027 /* sizeof EMACS_INT == sizeof Lisp_Object */
1028 offset += (sizeof (EMACS_INT)))
1030 idx = PER_BUFFER_IDX (offset);
1031 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1032 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1033 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1034 PER_BUFFER_VALUE (buf, offset)),
1035 result);
1039 return result;
1042 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1043 0, 1, 0,
1044 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1045 No argument or nil as argument means use current buffer as BUFFER. */)
1046 (register Lisp_Object buffer)
1048 register struct buffer *buf;
1049 if (NILP (buffer))
1050 buf = current_buffer;
1051 else
1053 CHECK_BUFFER (buffer);
1054 buf = XBUFFER (buffer);
1057 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1060 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1061 1, 1, 0,
1062 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1063 A non-nil FLAG means mark the buffer modified. */)
1064 (register Lisp_Object flag)
1066 register int already;
1067 register Lisp_Object fn;
1068 Lisp_Object buffer, window;
1070 #ifdef CLASH_DETECTION
1071 /* If buffer becoming modified, lock the file.
1072 If buffer becoming unmodified, unlock the file. */
1074 fn = current_buffer->file_truename;
1075 /* Test buffer-file-name so that binding it to nil is effective. */
1076 if (!NILP (fn) && ! NILP (current_buffer->filename))
1078 already = SAVE_MODIFF < MODIFF;
1079 if (!already && !NILP (flag))
1080 lock_file (fn);
1081 else if (already && NILP (flag))
1082 unlock_file (fn);
1084 #endif /* CLASH_DETECTION */
1086 /* Here we have a problem. SAVE_MODIFF is used here to encode
1087 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1088 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1089 modify SAVE_MODIFF to affect one, we may affect the other
1090 as well.
1091 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1092 if SAVE_MODIFF<auto_save_modified that means we risk changing
1093 recent-auto-save-p from t to nil.
1094 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1095 we risk changing recent-auto-save-p from nil to t. */
1096 SAVE_MODIFF = (NILP (flag)
1097 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1098 ? MODIFF
1099 /* Let's try to preserve recent-auto-save-p. */
1100 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1101 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1102 we can either decrease SAVE_MODIFF and auto_save_modified
1103 or increase MODIFF. */
1104 : MODIFF++);
1106 /* Set update_mode_lines only if buffer is displayed in some window.
1107 Packages like jit-lock or lazy-lock preserve a buffer's modified
1108 state by recording/restoring the state around blocks of code.
1109 Setting update_mode_lines makes redisplay consider all windows
1110 (on all frames). Stealth fontification of buffers not displayed
1111 would incur additional redisplay costs if we'd set
1112 update_modes_lines unconditionally.
1114 Ideally, I think there should be another mechanism for fontifying
1115 buffers without "modifying" buffers, or redisplay should be
1116 smarter about updating the `*' in mode lines. --gerd */
1117 XSETBUFFER (buffer, current_buffer);
1118 window = Fget_buffer_window (buffer, Qt);
1119 if (WINDOWP (window))
1121 ++update_mode_lines;
1122 current_buffer->prevent_redisplay_optimizations_p = 1;
1125 return flag;
1128 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1129 Srestore_buffer_modified_p, 1, 1, 0,
1130 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1131 It is not ensured that mode lines will be updated to show the modified
1132 state of the current buffer. Use with care. */)
1133 (Lisp_Object flag)
1135 #ifdef CLASH_DETECTION
1136 Lisp_Object fn;
1138 /* If buffer becoming modified, lock the file.
1139 If buffer becoming unmodified, unlock the file. */
1141 fn = current_buffer->file_truename;
1142 /* Test buffer-file-name so that binding it to nil is effective. */
1143 if (!NILP (fn) && ! NILP (current_buffer->filename))
1145 int already = SAVE_MODIFF < MODIFF;
1146 if (!already && !NILP (flag))
1147 lock_file (fn);
1148 else if (already && NILP (flag))
1149 unlock_file (fn);
1151 #endif /* CLASH_DETECTION */
1153 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1154 return flag;
1157 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1158 0, 1, 0,
1159 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1160 Each buffer has a tick counter which is incremented each time the
1161 text in that buffer is changed. It wraps around occasionally.
1162 No argument or nil as argument means use current buffer as BUFFER. */)
1163 (register Lisp_Object buffer)
1165 register struct buffer *buf;
1166 if (NILP (buffer))
1167 buf = current_buffer;
1168 else
1170 CHECK_BUFFER (buffer);
1171 buf = XBUFFER (buffer);
1174 return make_number (BUF_MODIFF (buf));
1177 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1178 Sbuffer_chars_modified_tick, 0, 1, 0,
1179 doc: /* Return BUFFER's character-change tick counter.
1180 Each buffer has a character-change tick counter, which is set to the
1181 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1182 time text in that buffer is inserted or deleted. By comparing the
1183 values returned by two individual calls of `buffer-chars-modified-tick',
1184 you can tell whether a character change occurred in that buffer in
1185 between these calls. No argument or nil as argument means use current
1186 buffer as BUFFER. */)
1187 (register Lisp_Object buffer)
1189 register struct buffer *buf;
1190 if (NILP (buffer))
1191 buf = current_buffer;
1192 else
1194 CHECK_BUFFER (buffer);
1195 buf = XBUFFER (buffer);
1198 return make_number (BUF_CHARS_MODIFF (buf));
1201 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1202 "(list (read-string \"Rename buffer (to new name): \" \
1203 nil 'buffer-name-history (buffer-name (current-buffer))) \
1204 current-prefix-arg)",
1205 doc: /* Change current buffer's name to NEWNAME (a string).
1206 If second arg UNIQUE is nil or omitted, it is an error if a
1207 buffer named NEWNAME already exists.
1208 If UNIQUE is non-nil, come up with a new name using
1209 `generate-new-buffer-name'.
1210 Interactively, you can set UNIQUE with a prefix argument.
1211 We return the name we actually gave the buffer.
1212 This does not change the name of the visited file (if any). */)
1213 (register Lisp_Object newname, Lisp_Object unique)
1215 register Lisp_Object tem, buf;
1217 CHECK_STRING (newname);
1219 if (SCHARS (newname) == 0)
1220 error ("Empty string is invalid as a buffer name");
1222 tem = Fget_buffer (newname);
1223 if (!NILP (tem))
1225 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1226 rename the buffer automatically so you can create another
1227 with the original name. It makes UNIQUE equivalent to
1228 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1229 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1230 return current_buffer->name;
1231 if (!NILP (unique))
1232 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1233 else
1234 error ("Buffer name `%s' is in use", SDATA (newname));
1237 current_buffer->name = newname;
1239 /* Catch redisplay's attention. Unless we do this, the mode lines for
1240 any windows displaying current_buffer will stay unchanged. */
1241 update_mode_lines++;
1243 XSETBUFFER (buf, current_buffer);
1244 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1245 if (NILP (current_buffer->filename)
1246 && !NILP (current_buffer->auto_save_file_name))
1247 call0 (intern ("rename-auto-save-file"));
1248 /* Refetch since that last call may have done GC. */
1249 return current_buffer->name;
1252 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1253 doc: /* Return most recently selected buffer other than BUFFER.
1254 Buffers not visible in windows are preferred to visible buffers,
1255 unless optional second argument VISIBLE-OK is non-nil.
1256 If the optional third argument FRAME is non-nil, use that frame's
1257 buffer list instead of the selected frame's buffer list.
1258 If no other buffer exists, the buffer `*scratch*' is returned.
1259 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1260 (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
1262 Lisp_Object Fset_buffer_major_mode (Lisp_Object buffer);
1263 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1264 notsogood = Qnil;
1266 if (NILP (frame))
1267 frame = selected_frame;
1269 CHECK_FRAME (frame);
1271 tail = Vbuffer_alist;
1272 pred = frame_buffer_predicate (frame);
1274 /* Consider buffers that have been seen in the selected frame
1275 before other buffers. */
1277 tem = frame_buffer_list (frame);
1278 add_ons = Qnil;
1279 while (CONSP (tem))
1281 if (BUFFERP (XCAR (tem)))
1282 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1283 tem = XCDR (tem);
1285 tail = nconc2 (Fnreverse (add_ons), tail);
1287 for (; CONSP (tail); tail = XCDR (tail))
1289 buf = Fcdr (XCAR (tail));
1290 if (EQ (buf, buffer))
1291 continue;
1292 if (NILP (buf))
1293 continue;
1294 if (NILP (XBUFFER (buf)->name))
1295 continue;
1296 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1297 continue;
1298 /* If the selected frame has a buffer_predicate,
1299 disregard buffers that don't fit the predicate. */
1300 if (!NILP (pred))
1302 tem = call1 (pred, buf);
1303 if (NILP (tem))
1304 continue;
1307 if (NILP (visible_ok))
1308 tem = Fget_buffer_window (buf, Qvisible);
1309 else
1310 tem = Qnil;
1311 if (NILP (tem))
1312 return buf;
1313 if (NILP (notsogood))
1314 notsogood = buf;
1316 if (!NILP (notsogood))
1317 return notsogood;
1318 buf = Fget_buffer (build_string ("*scratch*"));
1319 if (NILP (buf))
1321 buf = Fget_buffer_create (build_string ("*scratch*"));
1322 Fset_buffer_major_mode (buf);
1324 return buf;
1327 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1328 0, 1, "",
1329 doc: /* Start keeping undo information for buffer BUFFER.
1330 No argument or nil as argument means do this for the current buffer. */)
1331 (register Lisp_Object buffer)
1333 Lisp_Object real_buffer;
1335 if (NILP (buffer))
1336 XSETBUFFER (real_buffer, current_buffer);
1337 else
1339 real_buffer = Fget_buffer (buffer);
1340 if (NILP (real_buffer))
1341 nsberror (buffer);
1344 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1345 XBUFFER (real_buffer)->undo_list = Qnil;
1347 return Qnil;
1351 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1352 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1353 The buffer being killed will be current while the hook is running.\n\
1354 See `kill-buffer'."
1356 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1357 doc: /* Kill buffer BUFFER-OR-NAME.
1358 The argument may be a buffer or the name of an existing buffer.
1359 Argument nil or omitted means kill the current buffer. Return t if the
1360 buffer is actually killed, nil otherwise.
1362 This function calls `replace-buffer-in-windows' for cleaning up all
1363 windows currently displaying the buffer to be killed. The functions in
1364 `kill-buffer-query-functions' are called with the buffer to be killed as
1365 the current buffer. If any of them returns nil, the buffer is not
1366 killed. The hook `kill-buffer-hook' is run before the buffer is
1367 actually killed. The buffer being killed will be current while the hook
1368 is running.
1370 Any processes that have this buffer as the `process-buffer' are killed
1371 with SIGHUP. */)
1372 (Lisp_Object buffer_or_name)
1374 Lisp_Object buffer;
1375 register struct buffer *b;
1376 register Lisp_Object tem;
1377 register struct Lisp_Marker *m;
1378 struct gcpro gcpro1;
1380 if (NILP (buffer_or_name))
1381 buffer = Fcurrent_buffer ();
1382 else
1383 buffer = Fget_buffer (buffer_or_name);
1384 if (NILP (buffer))
1385 nsberror (buffer_or_name);
1387 b = XBUFFER (buffer);
1389 /* Avoid trouble for buffer already dead. */
1390 if (NILP (b->name))
1391 return Qnil;
1393 /* Query if the buffer is still modified. */
1394 if (INTERACTIVE && !NILP (b->filename)
1395 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1397 GCPRO1 (buffer);
1398 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1399 b->name, make_number (0)));
1400 UNGCPRO;
1401 if (NILP (tem))
1402 return Qnil;
1405 /* Run hooks with the buffer to be killed the current buffer. */
1407 int count = SPECPDL_INDEX ();
1408 Lisp_Object arglist[1];
1410 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1411 set_buffer_internal (b);
1413 /* First run the query functions; if any query is answered no,
1414 don't kill the buffer. */
1415 arglist[0] = Qkill_buffer_query_functions;
1416 tem = Frun_hook_with_args_until_failure (1, arglist);
1417 if (NILP (tem))
1418 return unbind_to (count, Qnil);
1420 /* Then run the hooks. */
1421 Frun_hooks (1, &Qkill_buffer_hook);
1422 unbind_to (count, Qnil);
1425 /* We have no more questions to ask. Verify that it is valid
1426 to kill the buffer. This must be done after the questions
1427 since anything can happen within do_yes_or_no_p. */
1429 /* Don't kill the minibuffer now current. */
1430 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1431 return Qnil;
1433 if (NILP (b->name))
1434 return Qnil;
1436 /* When we kill a base buffer, kill all its indirect buffers.
1437 We do it at this stage so nothing terrible happens if they
1438 ask questions or their hooks get errors. */
1439 if (! b->base_buffer)
1441 struct buffer *other;
1443 GCPRO1 (buffer);
1445 for (other = all_buffers; other; other = other->next)
1446 /* all_buffers contains dead buffers too;
1447 don't re-kill them. */
1448 if (other->base_buffer == b && !NILP (other->name))
1450 Lisp_Object buffer;
1451 XSETBUFFER (buffer, other);
1452 Fkill_buffer (buffer);
1455 UNGCPRO;
1458 /* Make this buffer not be current.
1459 In the process, notice if this is the sole visible buffer
1460 and give up if so. */
1461 if (b == current_buffer)
1463 tem = Fother_buffer (buffer, Qnil, Qnil);
1464 Fset_buffer (tem);
1465 if (b == current_buffer)
1466 return Qnil;
1469 /* Notice if the buffer to kill is the sole visible buffer
1470 when we're currently in the mini-buffer, and give up if so. */
1471 XSETBUFFER (tem, current_buffer);
1472 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1474 tem = Fother_buffer (buffer, Qnil, Qnil);
1475 if (EQ (buffer, tem))
1476 return Qnil;
1479 /* Now there is no question: we can kill the buffer. */
1481 #ifdef CLASH_DETECTION
1482 /* Unlock this buffer's file, if it is locked. */
1483 unlock_buffer (b);
1484 #endif /* CLASH_DETECTION */
1486 GCPRO1 (buffer);
1487 kill_buffer_processes (buffer);
1488 UNGCPRO;
1490 /* Killing buffer processes may run sentinels which may
1491 have called kill-buffer. */
1493 if (NILP (b->name))
1494 return Qnil;
1496 clear_charpos_cache (b);
1498 tem = Vinhibit_quit;
1499 Vinhibit_quit = Qt;
1500 replace_buffer_in_all_windows (buffer);
1501 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1502 frames_discard_buffer (buffer);
1503 Vinhibit_quit = tem;
1505 /* Delete any auto-save file, if we saved it in this session.
1506 But not if the buffer is modified. */
1507 if (STRINGP (b->auto_save_file_name)
1508 && BUF_AUTOSAVE_MODIFF (b) != 0
1509 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1510 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1511 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1513 Lisp_Object tem;
1514 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1515 if (! NILP (tem))
1516 internal_delete_file (b->auto_save_file_name);
1519 if (b->base_buffer)
1521 /* Unchain all markers that belong to this indirect buffer.
1522 Don't unchain the markers that belong to the base buffer
1523 or its other indirect buffers. */
1524 for (m = BUF_MARKERS (b); m; )
1526 struct Lisp_Marker *next = m->next;
1527 if (m->buffer == b)
1528 unchain_marker (m);
1529 m = next;
1532 else
1534 /* Unchain all markers of this buffer and its indirect buffers.
1535 and leave them pointing nowhere. */
1536 for (m = BUF_MARKERS (b); m; )
1538 struct Lisp_Marker *next = m->next;
1539 m->buffer = 0;
1540 m->next = NULL;
1541 m = next;
1543 BUF_MARKERS (b) = NULL;
1544 BUF_INTERVALS (b) = NULL_INTERVAL;
1546 /* Perhaps we should explicitly free the interval tree here... */
1549 /* Reset the local variables, so that this buffer's local values
1550 won't be protected from GC. They would be protected
1551 if they happened to remain encached in their symbols.
1552 This gets rid of them for certain. */
1553 swap_out_buffer_local_variables (b);
1554 reset_buffer_local_variables (b, 1);
1556 b->name = Qnil;
1558 BLOCK_INPUT;
1559 if (! b->base_buffer)
1560 free_buffer_text (b);
1562 if (b->newline_cache)
1564 free_region_cache (b->newline_cache);
1565 b->newline_cache = 0;
1567 if (b->width_run_cache)
1569 free_region_cache (b->width_run_cache);
1570 b->width_run_cache = 0;
1572 b->width_table = Qnil;
1573 UNBLOCK_INPUT;
1574 b->undo_list = Qnil;
1576 return Qt;
1579 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1580 we do this each time BUF is selected visibly, the more recently
1581 selected buffers are always closer to the front of the list. This
1582 means that other_buffer is more likely to choose a relevant buffer. */
1584 void
1585 record_buffer (Lisp_Object buf)
1587 register Lisp_Object link, prev;
1588 Lisp_Object frame;
1589 frame = selected_frame;
1591 prev = Qnil;
1592 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1594 if (EQ (XCDR (XCAR (link)), buf))
1595 break;
1596 prev = link;
1599 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1600 we cannot use Fdelq itself here because it allows quitting. */
1602 if (NILP (prev))
1603 Vbuffer_alist = XCDR (Vbuffer_alist);
1604 else
1605 XSETCDR (prev, XCDR (XCDR (prev)));
1607 XSETCDR (link, Vbuffer_alist);
1608 Vbuffer_alist = link;
1610 /* Effectively do a delq on buried_buffer_list. */
1612 prev = Qnil;
1613 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1614 link = XCDR (link))
1616 if (EQ (XCAR (link), buf))
1618 if (NILP (prev))
1619 XFRAME (frame)->buried_buffer_list = XCDR (link);
1620 else
1621 XSETCDR (prev, XCDR (XCDR (prev)));
1622 break;
1624 prev = link;
1627 /* Now move this buffer to the front of frame_buffer_list also. */
1629 prev = Qnil;
1630 for (link = frame_buffer_list (frame); CONSP (link);
1631 link = XCDR (link))
1633 if (EQ (XCAR (link), buf))
1634 break;
1635 prev = link;
1638 /* Effectively do delq. */
1640 if (CONSP (link))
1642 if (NILP (prev))
1643 set_frame_buffer_list (frame,
1644 XCDR (frame_buffer_list (frame)));
1645 else
1646 XSETCDR (prev, XCDR (XCDR (prev)));
1648 XSETCDR (link, frame_buffer_list (frame));
1649 set_frame_buffer_list (frame, link);
1651 else
1652 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1655 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1656 doc: /* Set an appropriate major mode for BUFFER.
1657 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1658 according to `default-major-mode'.
1659 Use this function before selecting the buffer, since it may need to inspect
1660 the current buffer's major mode. */)
1661 (Lisp_Object buffer)
1663 int count;
1664 Lisp_Object function;
1666 CHECK_BUFFER (buffer);
1668 if (STRINGP (XBUFFER (buffer)->name)
1669 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1670 function = find_symbol_value (intern ("initial-major-mode"));
1671 else
1673 function = buffer_defaults.major_mode;
1674 if (NILP (function)
1675 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1676 function = current_buffer->major_mode;
1679 if (NILP (function) || EQ (function, Qfundamental_mode))
1680 return Qnil;
1682 count = SPECPDL_INDEX ();
1684 /* To select a nonfundamental mode,
1685 select the buffer temporarily and then call the mode function. */
1687 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1689 Fset_buffer (buffer);
1690 call0 (function);
1692 return unbind_to (count, Qnil);
1695 /* Switch to buffer BUFFER in the selected window.
1696 If NORECORD is non-nil, don't call record_buffer. */
1698 Lisp_Object
1699 switch_to_buffer_1 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1701 register Lisp_Object buffer;
1703 if (NILP (buffer_or_name))
1704 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1705 else
1707 buffer = Fget_buffer (buffer_or_name);
1708 if (NILP (buffer))
1710 buffer = Fget_buffer_create (buffer_or_name);
1711 Fset_buffer_major_mode (buffer);
1714 Fset_buffer (buffer);
1715 if (NILP (norecord))
1716 record_buffer (buffer);
1718 Fset_window_buffer (EQ (selected_window, minibuf_window)
1719 ? Fnext_window (minibuf_window, Qnil, Qnil)
1720 : selected_window,
1721 buffer, Qnil);
1723 return buffer;
1726 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1727 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1728 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1729 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1730 nil. Return the buffer switched to.
1732 If BUFFER-OR-NAME is a string and does not identify an existing
1733 buffer, create a new buffer with that name. Interactively, if
1734 `confirm-nonexistent-file-or-buffer' is non-nil, request
1735 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1736 nil, switch to buffer returned by `other-buffer'.
1738 Optional second arg NORECORD non-nil means do not put this buffer
1739 at the front of the list of recently selected ones. This
1740 function returns the buffer it switched to as a Lisp object.
1742 If the selected window is the minibuffer window or dedicated to
1743 its buffer, use `pop-to-buffer' for displaying the buffer.
1745 WARNING: This is NOT the way to work on another buffer temporarily
1746 within a Lisp program! Use `set-buffer' instead. That avoids
1747 messing with the window-buffer correspondences. */)
1748 (Lisp_Object buffer_or_name, Lisp_Object norecord)
1750 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1752 /* Basically a NOP. Avoid signalling an error in the case where
1753 the selected window is dedicated, or a minibuffer. */
1755 /* But do put this buffer at the front of the buffer list, unless
1756 that has been inhibited. Note that even if BUFFER-OR-NAME is
1757 at the front of the main buffer-list already, we still want to
1758 move it to the front of the frame's buffer list. */
1759 if (NILP (norecord))
1760 record_buffer (buffer_or_name);
1761 return Fset_buffer (buffer_or_name);
1763 else if (EQ (minibuf_window, selected_window)
1764 /* If `dedicated' is neither nil nor t, it means it's
1765 dedicatedness can be overridden by an explicit request
1766 such as a call to switch-to-buffer. */
1767 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1768 /* We can't use the selected window so let `pop-to-buffer' try some
1769 other window. */
1770 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1771 else
1772 return switch_to_buffer_1 (buffer_or_name, norecord);
1775 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1776 doc: /* Return the current buffer as a Lisp object. */)
1777 (void)
1779 register Lisp_Object buf;
1780 XSETBUFFER (buf, current_buffer);
1781 return buf;
1784 /* Set the current buffer to B.
1786 We previously set windows_or_buffers_changed here to invalidate
1787 global unchanged information in beg_unchanged and end_unchanged.
1788 This is no longer necessary because we now compute unchanged
1789 information on a buffer-basis. Every action affecting other
1790 windows than the selected one requires a select_window at some
1791 time, and that increments windows_or_buffers_changed. */
1793 void
1794 set_buffer_internal (register struct buffer *b)
1796 if (current_buffer != b)
1797 set_buffer_internal_1 (b);
1800 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1801 This is used by redisplay. */
1803 void
1804 set_buffer_internal_1 (register struct buffer *b)
1806 register struct buffer *old_buf;
1807 register Lisp_Object tail;
1809 #ifdef USE_MMAP_FOR_BUFFERS
1810 if (b->text->beg == NULL)
1811 enlarge_buffer_text (b, 0);
1812 #endif /* USE_MMAP_FOR_BUFFERS */
1814 if (current_buffer == b)
1815 return;
1817 old_buf = current_buffer;
1818 current_buffer = b;
1819 last_known_column_point = -1; /* invalidate indentation cache */
1821 if (old_buf)
1823 /* Put the undo list back in the base buffer, so that it appears
1824 that an indirect buffer shares the undo list of its base. */
1825 if (old_buf->base_buffer)
1826 old_buf->base_buffer->undo_list = old_buf->undo_list;
1828 /* If the old current buffer has markers to record PT, BEGV and ZV
1829 when it is not current, update them now. */
1830 if (! NILP (old_buf->pt_marker))
1832 Lisp_Object obuf;
1833 XSETBUFFER (obuf, old_buf);
1834 set_marker_both (old_buf->pt_marker, obuf,
1835 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1837 if (! NILP (old_buf->begv_marker))
1839 Lisp_Object obuf;
1840 XSETBUFFER (obuf, old_buf);
1841 set_marker_both (old_buf->begv_marker, obuf,
1842 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1844 if (! NILP (old_buf->zv_marker))
1846 Lisp_Object obuf;
1847 XSETBUFFER (obuf, old_buf);
1848 set_marker_both (old_buf->zv_marker, obuf,
1849 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1853 /* Get the undo list from the base buffer, so that it appears
1854 that an indirect buffer shares the undo list of its base. */
1855 if (b->base_buffer)
1856 b->undo_list = b->base_buffer->undo_list;
1858 /* If the new current buffer has markers to record PT, BEGV and ZV
1859 when it is not current, fetch them now. */
1860 if (! NILP (b->pt_marker))
1862 BUF_PT (b) = marker_position (b->pt_marker);
1863 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1865 if (! NILP (b->begv_marker))
1867 BUF_BEGV (b) = marker_position (b->begv_marker);
1868 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1870 if (! NILP (b->zv_marker))
1872 BUF_ZV (b) = marker_position (b->zv_marker);
1873 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1876 /* Look down buffer's list of local Lisp variables
1877 to find and update any that forward into C variables. */
1881 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1883 Lisp_Object var = XCAR (XCAR (tail));
1884 struct Lisp_Symbol *sym = XSYMBOL (var);
1885 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
1886 && SYMBOL_BLV (sym)->fwd)
1887 /* Just reference the variable
1888 to cause it to become set for this buffer. */
1889 Fsymbol_value (var);
1892 /* Do the same with any others that were local to the previous buffer */
1893 while (b != old_buf && (b = old_buf, b));
1896 /* Switch to buffer B temporarily for redisplay purposes.
1897 This avoids certain things that don't need to be done within redisplay. */
1899 void
1900 set_buffer_temp (struct buffer *b)
1902 register struct buffer *old_buf;
1904 if (current_buffer == b)
1905 return;
1907 old_buf = current_buffer;
1908 current_buffer = b;
1910 if (old_buf)
1912 /* If the old current buffer has markers to record PT, BEGV and ZV
1913 when it is not current, update them now. */
1914 if (! NILP (old_buf->pt_marker))
1916 Lisp_Object obuf;
1917 XSETBUFFER (obuf, old_buf);
1918 set_marker_both (old_buf->pt_marker, obuf,
1919 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1921 if (! NILP (old_buf->begv_marker))
1923 Lisp_Object obuf;
1924 XSETBUFFER (obuf, old_buf);
1925 set_marker_both (old_buf->begv_marker, obuf,
1926 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1928 if (! NILP (old_buf->zv_marker))
1930 Lisp_Object obuf;
1931 XSETBUFFER (obuf, old_buf);
1932 set_marker_both (old_buf->zv_marker, obuf,
1933 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1937 /* If the new current buffer has markers to record PT, BEGV and ZV
1938 when it is not current, fetch them now. */
1939 if (! NILP (b->pt_marker))
1941 BUF_PT (b) = marker_position (b->pt_marker);
1942 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1944 if (! NILP (b->begv_marker))
1946 BUF_BEGV (b) = marker_position (b->begv_marker);
1947 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1949 if (! NILP (b->zv_marker))
1951 BUF_ZV (b) = marker_position (b->zv_marker);
1952 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1956 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1957 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
1958 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
1959 also `save-excursion' when you want to make a buffer current
1960 temporarily. This function does not display the buffer, so its effect
1961 ends when the current command terminates. Use `switch-to-buffer' or
1962 `pop-to-buffer' to switch buffers permanently. */)
1963 (register Lisp_Object buffer_or_name)
1965 register Lisp_Object buffer;
1966 buffer = Fget_buffer (buffer_or_name);
1967 if (NILP (buffer))
1968 nsberror (buffer_or_name);
1969 if (NILP (XBUFFER (buffer)->name))
1970 error ("Selecting deleted buffer");
1971 set_buffer_internal (XBUFFER (buffer));
1972 return buffer;
1975 /* Set the current buffer to BUFFER provided it is alive. */
1977 Lisp_Object
1978 set_buffer_if_live (Lisp_Object buffer)
1980 if (! NILP (XBUFFER (buffer)->name))
1981 Fset_buffer (buffer);
1982 return Qnil;
1985 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1986 Sbarf_if_buffer_read_only, 0, 0, 0,
1987 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1988 (void)
1990 if (!NILP (current_buffer->read_only)
1991 && NILP (Vinhibit_read_only))
1992 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1993 return Qnil;
1996 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1997 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
1998 There it is the least likely candidate for `other-buffer' to return;
1999 thus, the least likely buffer for \\[switch-to-buffer] to select by
2000 default.
2002 The argument may be a buffer name or an actual buffer object. If
2003 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2004 from the selected window if it is displayed there. If the selected
2005 window is dedicated to its buffer, delete that window if there are other
2006 windows on the same frame. If the selected window is the only window on
2007 its frame, iconify that frame. */)
2008 (register Lisp_Object buffer_or_name)
2010 Lisp_Object buffer;
2012 /* Figure out what buffer we're going to bury. */
2013 if (NILP (buffer_or_name))
2015 Lisp_Object tem;
2016 XSETBUFFER (buffer, current_buffer);
2018 tem = Fwindow_buffer (selected_window);
2019 /* If we're burying the current buffer, unshow it. */
2020 if (EQ (buffer, tem))
2022 if (NILP (Fwindow_dedicated_p (selected_window)))
2023 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2024 else if (NILP (XWINDOW (selected_window)->parent))
2025 Ficonify_frame (Fwindow_frame (selected_window));
2026 else
2027 Fdelete_window (selected_window);
2030 else
2032 buffer = Fget_buffer (buffer_or_name);
2033 if (NILP (buffer))
2034 nsberror (buffer_or_name);
2037 /* Move buffer to the end of the buffer list. Do nothing if the
2038 buffer is killed. */
2039 if (!NILP (XBUFFER (buffer)->name))
2041 Lisp_Object aelt, link;
2043 aelt = Frassq (buffer, Vbuffer_alist);
2044 link = Fmemq (aelt, Vbuffer_alist);
2045 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2046 XSETCDR (link, Qnil);
2047 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2049 XFRAME (selected_frame)->buffer_list
2050 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2051 XFRAME (selected_frame)->buried_buffer_list
2052 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2055 return Qnil;
2058 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2059 doc: /* Delete the entire contents of the current buffer.
2060 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2061 so the buffer is truly empty after this. */)
2062 (void)
2064 Fwiden ();
2066 del_range (BEG, Z);
2068 current_buffer->last_window_start = 1;
2069 /* Prevent warnings, or suspension of auto saving, that would happen
2070 if future size is less than past size. Use of erase-buffer
2071 implies that the future text is not really related to the past text. */
2072 XSETFASTINT (current_buffer->save_length, 0);
2073 return Qnil;
2076 void
2077 validate_region (register Lisp_Object *b, register Lisp_Object *e)
2079 CHECK_NUMBER_COERCE_MARKER (*b);
2080 CHECK_NUMBER_COERCE_MARKER (*e);
2082 if (XINT (*b) > XINT (*e))
2084 Lisp_Object tem;
2085 tem = *b; *b = *e; *e = tem;
2088 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2089 && XINT (*e) <= ZV))
2090 args_out_of_range (*b, *e);
2093 /* Advance BYTE_POS up to a character boundary
2094 and return the adjusted position. */
2096 static int
2097 advance_to_char_boundary (int byte_pos)
2099 int c;
2101 if (byte_pos == BEG)
2102 /* Beginning of buffer is always a character boundary. */
2103 return BEG;
2105 c = FETCH_BYTE (byte_pos);
2106 if (! CHAR_HEAD_P (c))
2108 /* We should advance BYTE_POS only when C is a constituent of a
2109 multibyte sequence. */
2110 int orig_byte_pos = byte_pos;
2114 byte_pos--;
2115 c = FETCH_BYTE (byte_pos);
2117 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2118 INC_POS (byte_pos);
2119 if (byte_pos < orig_byte_pos)
2120 byte_pos = orig_byte_pos;
2121 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2122 surely advance to the correct character boundary. If C is
2123 not, BYTE_POS was unchanged. */
2126 return byte_pos;
2129 #ifdef REL_ALLOC
2130 extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
2131 #endif /* REL_ALLOC */
2133 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2134 1, 1, 0,
2135 doc: /* Swap the text between current buffer and BUFFER. */)
2136 (Lisp_Object buffer)
2138 struct buffer *other_buffer;
2139 CHECK_BUFFER (buffer);
2140 other_buffer = XBUFFER (buffer);
2142 if (NILP (other_buffer->name))
2143 error ("Cannot swap a dead buffer's text");
2145 /* Actually, it probably works just fine.
2146 * if (other_buffer == current_buffer)
2147 * error ("Cannot swap a buffer's text with itself"); */
2149 /* Actually, this may be workable as well, tho probably only if they're
2150 *both* indirect. */
2151 if (other_buffer->base_buffer
2152 || current_buffer->base_buffer)
2153 error ("Cannot swap indirect buffers's text");
2155 { /* This is probably harder to make work. */
2156 struct buffer *other;
2157 for (other = all_buffers; other; other = other->next)
2158 if (other->base_buffer == other_buffer
2159 || other->base_buffer == current_buffer)
2160 error ("One of the buffers to swap has indirect buffers");
2163 #define swapfield(field, type) \
2164 do { \
2165 type tmp##field = other_buffer->field; \
2166 other_buffer->field = current_buffer->field; \
2167 current_buffer->field = tmp##field; \
2168 } while (0)
2170 swapfield (own_text, struct buffer_text);
2171 eassert (current_buffer->text == &current_buffer->own_text);
2172 eassert (other_buffer->text == &other_buffer->own_text);
2173 #ifdef REL_ALLOC
2174 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2175 (POINTER_TYPE **) &other_buffer->own_text.beg);
2176 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2177 (POINTER_TYPE **) &current_buffer->own_text.beg);
2178 #endif /* REL_ALLOC */
2180 swapfield (pt, EMACS_INT);
2181 swapfield (pt_byte, EMACS_INT);
2182 swapfield (begv, EMACS_INT);
2183 swapfield (begv_byte, EMACS_INT);
2184 swapfield (zv, EMACS_INT);
2185 swapfield (zv_byte, EMACS_INT);
2186 eassert (!current_buffer->base_buffer);
2187 eassert (!other_buffer->base_buffer);
2188 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2189 swapfield (newline_cache, struct region_cache *);
2190 swapfield (width_run_cache, struct region_cache *);
2191 current_buffer->prevent_redisplay_optimizations_p = 1;
2192 other_buffer->prevent_redisplay_optimizations_p = 1;
2193 swapfield (overlays_before, struct Lisp_Overlay *);
2194 swapfield (overlays_after, struct Lisp_Overlay *);
2195 swapfield (overlay_center, EMACS_INT);
2196 swapfield (undo_list, Lisp_Object);
2197 swapfield (mark, Lisp_Object);
2198 swapfield (enable_multibyte_characters, Lisp_Object);
2199 swapfield (bidi_display_reordering, Lisp_Object);
2200 swapfield (bidi_paragraph_direction, Lisp_Object);
2201 /* FIXME: Not sure what we should do with these *_marker fields.
2202 Hopefully they're just nil anyway. */
2203 swapfield (pt_marker, Lisp_Object);
2204 swapfield (begv_marker, Lisp_Object);
2205 swapfield (zv_marker, Lisp_Object);
2206 current_buffer->point_before_scroll = Qnil;
2207 other_buffer->point_before_scroll = Qnil;
2209 current_buffer->text->modiff++; other_buffer->text->modiff++;
2210 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2211 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2212 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2213 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2214 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2215 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2217 struct Lisp_Marker *m;
2218 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2219 if (m->buffer == other_buffer)
2220 m->buffer = current_buffer;
2221 else
2222 /* Since there's no indirect buffer in sight, markers on
2223 BUF_MARKERS(buf) should either be for `buf' or dead. */
2224 eassert (!m->buffer);
2225 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2226 if (m->buffer == current_buffer)
2227 m->buffer = other_buffer;
2228 else
2229 /* Since there's no indirect buffer in sight, markers on
2230 BUF_MARKERS(buf) should either be for `buf' or dead. */
2231 eassert (!m->buffer);
2233 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2234 So since we just swapped the markers between the two buffers, we need
2235 to undo the effect of this swap for window markers. */
2236 Lisp_Object w = Fselected_window (), ws = Qnil;
2237 Lisp_Object buf1, buf2;
2238 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2240 while (NILP (Fmemq (w, ws)))
2242 ws = Fcons (w, ws);
2243 if (MARKERP (XWINDOW (w)->pointm)
2244 && (EQ (XWINDOW (w)->buffer, buf1)
2245 || EQ (XWINDOW (w)->buffer, buf2)))
2246 Fset_marker (XWINDOW (w)->pointm,
2247 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2248 XWINDOW (w)->buffer);
2249 w = Fnext_window (w, Qt, Qt);
2253 if (current_buffer->text->intervals)
2254 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2255 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2256 if (other_buffer->text->intervals)
2257 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2258 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2260 return Qnil;
2263 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2264 1, 1, 0,
2265 doc: /* Set the multibyte flag of the current buffer to FLAG.
2266 If FLAG is t, this makes the buffer a multibyte buffer.
2267 If FLAG is nil, this makes the buffer a single-byte buffer.
2268 In these cases, the buffer contents remain unchanged as a sequence of
2269 bytes but the contents viewed as characters do change.
2270 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2271 all eight-bit bytes to eight-bit characters.
2272 If the multibyte flag was really changed, undo information of the
2273 current buffer is cleared. */)
2274 (Lisp_Object flag)
2276 struct Lisp_Marker *tail, *markers;
2277 struct buffer *other;
2278 int begv, zv;
2279 int narrowed = (BEG != BEGV || Z != ZV);
2280 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2281 Lisp_Object old_undo = current_buffer->undo_list;
2282 struct gcpro gcpro1;
2284 if (current_buffer->base_buffer)
2285 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2287 /* Do nothing if nothing actually changes. */
2288 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2289 return flag;
2291 GCPRO1 (old_undo);
2293 /* Don't record these buffer changes. We will put a special undo entry
2294 instead. */
2295 current_buffer->undo_list = Qt;
2297 /* If the cached position is for this buffer, clear it out. */
2298 clear_charpos_cache (current_buffer);
2300 if (NILP (flag))
2301 begv = BEGV_BYTE, zv = ZV_BYTE;
2302 else
2303 begv = BEGV, zv = ZV;
2305 if (narrowed)
2306 Fwiden ();
2308 if (NILP (flag))
2310 int pos, stop;
2311 unsigned char *p;
2313 /* Do this first, so it can use CHAR_TO_BYTE
2314 to calculate the old correspondences. */
2315 set_intervals_multibyte (0);
2317 current_buffer->enable_multibyte_characters = Qnil;
2319 Z = Z_BYTE;
2320 BEGV = BEGV_BYTE;
2321 ZV = ZV_BYTE;
2322 GPT = GPT_BYTE;
2323 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2326 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2327 tail->charpos = tail->bytepos;
2329 /* Convert multibyte form of 8-bit characters to unibyte. */
2330 pos = BEG;
2331 stop = GPT;
2332 p = BEG_ADDR;
2333 while (1)
2335 int c, bytes;
2337 if (pos == stop)
2339 if (pos == Z)
2340 break;
2341 p = GAP_END_ADDR;
2342 stop = Z;
2344 if (ASCII_BYTE_P (*p))
2345 p++, pos++;
2346 else if (CHAR_BYTE8_HEAD_P (*p))
2348 c = STRING_CHAR_AND_LENGTH (p, bytes);
2349 /* Delete all bytes for this 8-bit character but the
2350 last one, and change the last one to the charcter
2351 code. */
2352 bytes--;
2353 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2354 p = GAP_END_ADDR;
2355 *p++ = c;
2356 pos++;
2357 if (begv > pos)
2358 begv -= bytes;
2359 if (zv > pos)
2360 zv -= bytes;
2361 stop = Z;
2363 else
2365 bytes = BYTES_BY_CHAR_HEAD (*p);
2366 p += bytes, pos += bytes;
2369 if (narrowed)
2370 Fnarrow_to_region (make_number (begv), make_number (zv));
2372 else
2374 int pt = PT;
2375 int pos, stop;
2376 unsigned char *p, *pend;
2378 /* Be sure not to have a multibyte sequence striding over the GAP.
2379 Ex: We change this: "...abc\302 _GAP_ \241def..."
2380 to: "...abc _GAP_ \302\241def..." */
2382 if (EQ (flag, Qt)
2383 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2384 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2386 unsigned char *p = GPT_ADDR - 1;
2388 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2389 if (LEADING_CODE_P (*p))
2391 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2393 move_gap_both (new_gpt, new_gpt);
2397 /* Make the buffer contents valid as multibyte by converting
2398 8-bit characters to multibyte form. */
2399 pos = BEG;
2400 stop = GPT;
2401 p = BEG_ADDR;
2402 pend = GPT_ADDR;
2403 while (1)
2405 int bytes;
2407 if (pos == stop)
2409 if (pos == Z)
2410 break;
2411 p = GAP_END_ADDR;
2412 pend = Z_ADDR;
2413 stop = Z;
2416 if (ASCII_BYTE_P (*p))
2417 p++, pos++;
2418 else if (EQ (flag, Qt)
2419 && ! CHAR_BYTE8_HEAD_P (*p)
2420 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2421 p += bytes, pos += bytes;
2422 else
2424 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2425 int c;
2427 c = BYTE8_TO_CHAR (*p);
2428 bytes = CHAR_STRING (c, tmp);
2429 *p = tmp[0];
2430 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2431 bytes--;
2432 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2433 /* Now the gap is after the just inserted data. */
2434 pos = GPT;
2435 p = GAP_END_ADDR;
2436 if (pos <= begv)
2437 begv += bytes;
2438 if (pos <= zv)
2439 zv += bytes;
2440 if (pos <= pt)
2441 pt += bytes;
2442 pend = Z_ADDR;
2443 stop = Z;
2447 if (pt != PT)
2448 TEMP_SET_PT (pt);
2450 if (narrowed)
2451 Fnarrow_to_region (make_number (begv), make_number (zv));
2453 /* Do this first, so that chars_in_text asks the right question.
2454 set_intervals_multibyte needs it too. */
2455 current_buffer->enable_multibyte_characters = Qt;
2457 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2458 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2460 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2462 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2463 if (BEGV_BYTE > GPT_BYTE)
2464 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2465 else
2466 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2468 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2469 if (ZV_BYTE > GPT_BYTE)
2470 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2471 else
2472 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2475 int pt_byte = advance_to_char_boundary (PT_BYTE);
2476 int pt;
2478 if (pt_byte > GPT_BYTE)
2479 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2480 else
2481 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2482 TEMP_SET_PT_BOTH (pt, pt_byte);
2485 tail = markers = BUF_MARKERS (current_buffer);
2487 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2488 getting confused by the markers that have not yet been updated.
2489 It is also a signal that it should never create a marker. */
2490 BUF_MARKERS (current_buffer) = NULL;
2492 for (; tail; tail = tail->next)
2494 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2495 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2498 /* Make sure no markers were put on the chain
2499 while the chain value was incorrect. */
2500 if (BUF_MARKERS (current_buffer))
2501 abort ();
2503 BUF_MARKERS (current_buffer) = markers;
2505 /* Do this last, so it can calculate the new correspondences
2506 between chars and bytes. */
2507 set_intervals_multibyte (1);
2510 if (!EQ (old_undo, Qt))
2512 /* Represent all the above changes by a special undo entry. */
2513 current_buffer->undo_list = Fcons (list3 (Qapply,
2514 intern ("set-buffer-multibyte"),
2515 NILP (flag) ? Qt : Qnil),
2516 old_undo);
2519 UNGCPRO;
2521 /* Changing the multibyteness of a buffer means that all windows
2522 showing that buffer must be updated thoroughly. */
2523 current_buffer->prevent_redisplay_optimizations_p = 1;
2524 ++windows_or_buffers_changed;
2526 /* Copy this buffer's new multibyte status
2527 into all of its indirect buffers. */
2528 for (other = all_buffers; other; other = other->next)
2529 if (other->base_buffer == current_buffer && !NILP (other->name))
2531 other->enable_multibyte_characters
2532 = current_buffer->enable_multibyte_characters;
2533 other->prevent_redisplay_optimizations_p = 1;
2536 /* Restore the modifiedness of the buffer. */
2537 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2538 Fset_buffer_modified_p (Qnil);
2540 /* Update coding systems of this buffer's process (if any). */
2542 Lisp_Object process;
2544 process = Fget_buffer_process (Fcurrent_buffer ());
2545 if (PROCESSP (process))
2546 setup_process_coding_systems (process);
2549 return flag;
2552 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2553 0, 0, 0,
2554 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2555 Most local variable bindings are eliminated so that the default values
2556 become effective once more. Also, the syntax table is set from
2557 `standard-syntax-table', the local keymap is set to nil,
2558 and the abbrev table from `fundamental-mode-abbrev-table'.
2559 This function also forces redisplay of the mode line.
2561 Every function to select a new major mode starts by
2562 calling this function.
2564 As a special exception, local variables whose names have
2565 a non-nil `permanent-local' property are not eliminated by this function.
2567 The first thing this function does is run
2568 the normal hook `change-major-mode-hook'. */)
2569 (void)
2571 if (!NILP (Vrun_hooks))
2572 call1 (Vrun_hooks, Qchange_major_mode_hook);
2574 /* Make sure none of the bindings in local_var_alist
2575 remain swapped in, in their symbols. */
2577 swap_out_buffer_local_variables (current_buffer);
2579 /* Actually eliminate all local bindings of this buffer. */
2581 reset_buffer_local_variables (current_buffer, 0);
2583 /* Force mode-line redisplay. Useful here because all major mode
2584 commands call this function. */
2585 update_mode_lines++;
2587 return Qnil;
2590 /* Make sure no local variables remain set up with buffer B
2591 for their current values. */
2593 static void
2594 swap_out_buffer_local_variables (struct buffer *b)
2596 Lisp_Object oalist, alist, buffer;
2598 XSETBUFFER (buffer, b);
2599 oalist = b->local_var_alist;
2601 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2603 Lisp_Object sym = XCAR (XCAR (alist));
2604 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2605 /* Need not do anything if some other buffer's binding is
2606 now encached. */
2607 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2609 /* Symbol is set up for this buffer's old local value:
2610 swap it out! */
2611 swap_in_global_binding (XSYMBOL (sym));
2616 /* Find all the overlays in the current buffer that contain position POS.
2617 Return the number found, and store them in a vector in *VEC_PTR.
2618 Store in *LEN_PTR the size allocated for the vector.
2619 Store in *NEXT_PTR the next position after POS where an overlay starts,
2620 or ZV if there are no more overlays between POS and ZV.
2621 Store in *PREV_PTR the previous position before POS where an overlay ends,
2622 or where an overlay starts which ends at or after POS;
2623 or BEGV if there are no such overlays from BEGV to POS.
2624 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2626 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2627 when this function is called.
2629 If EXTEND is non-zero, we make the vector bigger if necessary.
2630 If EXTEND is zero, we never extend the vector,
2631 and we store only as many overlays as will fit.
2632 But we still return the total number of overlays.
2634 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2635 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2636 default (BEGV or ZV). */
2639 overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2640 EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
2642 Lisp_Object overlay, start, end;
2643 struct Lisp_Overlay *tail;
2644 int idx = 0;
2645 int len = *len_ptr;
2646 Lisp_Object *vec = *vec_ptr;
2647 int next = ZV;
2648 int prev = BEGV;
2649 int inhibit_storing = 0;
2651 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2653 int startpos, endpos;
2655 XSETMISC (overlay, tail);
2657 start = OVERLAY_START (overlay);
2658 end = OVERLAY_END (overlay);
2659 endpos = OVERLAY_POSITION (end);
2660 if (endpos < pos)
2662 if (prev < endpos)
2663 prev = endpos;
2664 break;
2666 startpos = OVERLAY_POSITION (start);
2667 /* This one ends at or after POS
2668 so its start counts for PREV_PTR if it's before POS. */
2669 if (prev < startpos && startpos < pos)
2670 prev = startpos;
2671 if (endpos == pos)
2672 continue;
2673 if (startpos <= pos)
2675 if (idx == len)
2677 /* The supplied vector is full.
2678 Either make it bigger, or don't store any more in it. */
2679 if (extend)
2681 /* Make it work with an initial len == 0. */
2682 len *= 2;
2683 if (len == 0)
2684 len = 4;
2685 *len_ptr = len;
2686 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2687 *vec_ptr = vec;
2689 else
2690 inhibit_storing = 1;
2693 if (!inhibit_storing)
2694 vec[idx] = overlay;
2695 /* Keep counting overlays even if we can't return them all. */
2696 idx++;
2698 else if (startpos < next)
2699 next = startpos;
2702 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2704 int startpos, endpos;
2706 XSETMISC (overlay, tail);
2708 start = OVERLAY_START (overlay);
2709 end = OVERLAY_END (overlay);
2710 startpos = OVERLAY_POSITION (start);
2711 if (pos < startpos)
2713 if (startpos < next)
2714 next = startpos;
2715 break;
2717 endpos = OVERLAY_POSITION (end);
2718 if (pos < endpos)
2720 if (idx == len)
2722 if (extend)
2724 /* Make it work with an initial len == 0. */
2725 len *= 2;
2726 if (len == 0)
2727 len = 4;
2728 *len_ptr = len;
2729 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2730 *vec_ptr = vec;
2732 else
2733 inhibit_storing = 1;
2736 if (!inhibit_storing)
2737 vec[idx] = overlay;
2738 idx++;
2740 if (startpos < pos && startpos > prev)
2741 prev = startpos;
2743 else if (endpos < pos && endpos > prev)
2744 prev = endpos;
2745 else if (endpos == pos && startpos > prev
2746 && (!change_req || startpos < pos))
2747 prev = startpos;
2750 if (next_ptr)
2751 *next_ptr = next;
2752 if (prev_ptr)
2753 *prev_ptr = prev;
2754 return idx;
2757 /* Find all the overlays in the current buffer that overlap the range
2758 BEG-END, or are empty at BEG, or are empty at END provided END
2759 denotes the position at the end of the current buffer.
2761 Return the number found, and store them in a vector in *VEC_PTR.
2762 Store in *LEN_PTR the size allocated for the vector.
2763 Store in *NEXT_PTR the next position after POS where an overlay starts,
2764 or ZV if there are no more overlays.
2765 Store in *PREV_PTR the previous position before POS where an overlay ends,
2766 or BEGV if there are no previous overlays.
2767 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2769 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2770 when this function is called.
2772 If EXTEND is non-zero, we make the vector bigger if necessary.
2773 If EXTEND is zero, we never extend the vector,
2774 and we store only as many overlays as will fit.
2775 But we still return the total number of overlays. */
2777 static int
2778 overlays_in (int beg, int end, int extend, Lisp_Object **vec_ptr, int *len_ptr,
2779 int *next_ptr, int *prev_ptr)
2781 Lisp_Object overlay, ostart, oend;
2782 struct Lisp_Overlay *tail;
2783 int idx = 0;
2784 int len = *len_ptr;
2785 Lisp_Object *vec = *vec_ptr;
2786 int next = ZV;
2787 int prev = BEGV;
2788 int inhibit_storing = 0;
2789 int end_is_Z = end == Z;
2791 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2793 int startpos, endpos;
2795 XSETMISC (overlay, tail);
2797 ostart = OVERLAY_START (overlay);
2798 oend = OVERLAY_END (overlay);
2799 endpos = OVERLAY_POSITION (oend);
2800 if (endpos < beg)
2802 if (prev < endpos)
2803 prev = endpos;
2804 break;
2806 startpos = OVERLAY_POSITION (ostart);
2807 /* Count an interval if it overlaps the range, is empty at the
2808 start of the range, or is empty at END provided END denotes the
2809 end of the buffer. */
2810 if ((beg < endpos && startpos < end)
2811 || (startpos == endpos
2812 && (beg == endpos || (end_is_Z && endpos == end))))
2814 if (idx == len)
2816 /* The supplied vector is full.
2817 Either make it bigger, or don't store any more in it. */
2818 if (extend)
2820 /* Make it work with an initial len == 0. */
2821 len *= 2;
2822 if (len == 0)
2823 len = 4;
2824 *len_ptr = len;
2825 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2826 *vec_ptr = vec;
2828 else
2829 inhibit_storing = 1;
2832 if (!inhibit_storing)
2833 vec[idx] = overlay;
2834 /* Keep counting overlays even if we can't return them all. */
2835 idx++;
2837 else if (startpos < next)
2838 next = startpos;
2841 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2843 int startpos, endpos;
2845 XSETMISC (overlay, tail);
2847 ostart = OVERLAY_START (overlay);
2848 oend = OVERLAY_END (overlay);
2849 startpos = OVERLAY_POSITION (ostart);
2850 if (end < startpos)
2852 if (startpos < next)
2853 next = startpos;
2854 break;
2856 endpos = OVERLAY_POSITION (oend);
2857 /* Count an interval if it overlaps the range, is empty at the
2858 start of the range, or is empty at END provided END denotes the
2859 end of the buffer. */
2860 if ((beg < endpos && startpos < end)
2861 || (startpos == endpos
2862 && (beg == endpos || (end_is_Z && endpos == end))))
2864 if (idx == len)
2866 if (extend)
2868 /* Make it work with an initial len == 0. */
2869 len *= 2;
2870 if (len == 0)
2871 len = 4;
2872 *len_ptr = len;
2873 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2874 *vec_ptr = vec;
2876 else
2877 inhibit_storing = 1;
2880 if (!inhibit_storing)
2881 vec[idx] = overlay;
2882 idx++;
2884 else if (endpos < beg && endpos > prev)
2885 prev = endpos;
2888 if (next_ptr)
2889 *next_ptr = next;
2890 if (prev_ptr)
2891 *prev_ptr = prev;
2892 return idx;
2896 /* Return non-zero if there exists an overlay with a non-nil
2897 `mouse-face' property overlapping OVERLAY. */
2900 mouse_face_overlay_overlaps (Lisp_Object overlay)
2902 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2903 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2904 int n, i, size;
2905 Lisp_Object *v, tem;
2907 size = 10;
2908 v = (Lisp_Object *) alloca (size * sizeof *v);
2909 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2910 if (n > size)
2912 v = (Lisp_Object *) alloca (n * sizeof *v);
2913 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2916 for (i = 0; i < n; ++i)
2917 if (!EQ (v[i], overlay)
2918 && (tem = Foverlay_get (overlay, Qmouse_face),
2919 !NILP (tem)))
2920 break;
2922 return i < n;
2927 /* Fast function to just test if we're at an overlay boundary. */
2929 overlay_touches_p (int pos)
2931 Lisp_Object overlay;
2932 struct Lisp_Overlay *tail;
2934 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2936 int endpos;
2938 XSETMISC (overlay ,tail);
2939 if (!OVERLAYP (overlay))
2940 abort ();
2942 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2943 if (endpos < pos)
2944 break;
2945 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2946 return 1;
2949 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2951 int startpos;
2953 XSETMISC (overlay, tail);
2954 if (!OVERLAYP (overlay))
2955 abort ();
2957 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2958 if (pos < startpos)
2959 break;
2960 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2961 return 1;
2963 return 0;
2966 struct sortvec
2968 Lisp_Object overlay;
2969 int beg, end;
2970 int priority;
2973 static int
2974 compare_overlays (const void *v1, const void *v2)
2976 const struct sortvec *s1 = (const struct sortvec *) v1;
2977 const struct sortvec *s2 = (const struct sortvec *) v2;
2978 if (s1->priority != s2->priority)
2979 return s1->priority - s2->priority;
2980 if (s1->beg != s2->beg)
2981 return s1->beg - s2->beg;
2982 if (s1->end != s2->end)
2983 return s2->end - s1->end;
2984 return 0;
2987 /* Sort an array of overlays by priority. The array is modified in place.
2988 The return value is the new size; this may be smaller than the original
2989 size if some of the overlays were invalid or were window-specific. */
2991 sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
2993 int i, j;
2994 struct sortvec *sortvec;
2995 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2997 /* Put the valid and relevant overlays into sortvec. */
2999 for (i = 0, j = 0; i < noverlays; i++)
3001 Lisp_Object tem;
3002 Lisp_Object overlay;
3004 overlay = overlay_vec[i];
3005 if (OVERLAY_VALID (overlay)
3006 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3007 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3009 /* If we're interested in a specific window, then ignore
3010 overlays that are limited to some other window. */
3011 if (w)
3013 Lisp_Object window;
3015 window = Foverlay_get (overlay, Qwindow);
3016 if (WINDOWP (window) && XWINDOW (window) != w)
3017 continue;
3020 /* This overlay is good and counts: put it into sortvec. */
3021 sortvec[j].overlay = overlay;
3022 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3023 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3024 tem = Foverlay_get (overlay, Qpriority);
3025 if (INTEGERP (tem))
3026 sortvec[j].priority = XINT (tem);
3027 else
3028 sortvec[j].priority = 0;
3029 j++;
3032 noverlays = j;
3034 /* Sort the overlays into the proper order: increasing priority. */
3036 if (noverlays > 1)
3037 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3039 for (i = 0; i < noverlays; i++)
3040 overlay_vec[i] = sortvec[i].overlay;
3041 return (noverlays);
3044 struct sortstr
3046 Lisp_Object string, string2;
3047 int size;
3048 int priority;
3051 struct sortstrlist
3053 struct sortstr *buf; /* An array that expands as needed; never freed. */
3054 int size; /* Allocated length of that array. */
3055 int used; /* How much of the array is currently in use. */
3056 int bytes; /* Total length of the strings in buf. */
3059 /* Buffers for storing information about the overlays touching a given
3060 position. These could be automatic variables in overlay_strings, but
3061 it's more efficient to hold onto the memory instead of repeatedly
3062 allocating and freeing it. */
3063 static struct sortstrlist overlay_heads, overlay_tails;
3064 static unsigned char *overlay_str_buf;
3066 /* Allocated length of overlay_str_buf. */
3067 static int overlay_str_len;
3069 /* A comparison function suitable for passing to qsort. */
3070 static int
3071 cmp_for_strings (const void *as1, const void *as2)
3073 struct sortstr *s1 = (struct sortstr *)as1;
3074 struct sortstr *s2 = (struct sortstr *)as2;
3075 if (s1->size != s2->size)
3076 return s2->size - s1->size;
3077 if (s1->priority != s2->priority)
3078 return s1->priority - s2->priority;
3079 return 0;
3082 static void
3083 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, Lisp_Object str2, Lisp_Object pri, int size)
3085 int nbytes;
3087 if (ssl->used == ssl->size)
3089 if (ssl->buf)
3090 ssl->size *= 2;
3091 else
3092 ssl->size = 5;
3093 ssl->buf = ((struct sortstr *)
3094 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3096 ssl->buf[ssl->used].string = str;
3097 ssl->buf[ssl->used].string2 = str2;
3098 ssl->buf[ssl->used].size = size;
3099 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3100 ssl->used++;
3102 if (NILP (current_buffer->enable_multibyte_characters))
3103 nbytes = SCHARS (str);
3104 else if (! STRING_MULTIBYTE (str))
3105 nbytes = count_size_as_multibyte (SDATA (str),
3106 SBYTES (str));
3107 else
3108 nbytes = SBYTES (str);
3110 ssl->bytes += nbytes;
3112 if (STRINGP (str2))
3114 if (NILP (current_buffer->enable_multibyte_characters))
3115 nbytes = SCHARS (str2);
3116 else if (! STRING_MULTIBYTE (str2))
3117 nbytes = count_size_as_multibyte (SDATA (str2),
3118 SBYTES (str2));
3119 else
3120 nbytes = SBYTES (str2);
3122 ssl->bytes += nbytes;
3126 /* Return the concatenation of the strings associated with overlays that
3127 begin or end at POS, ignoring overlays that are specific to a window
3128 other than W. The strings are concatenated in the appropriate order:
3129 shorter overlays nest inside longer ones, and higher priority inside
3130 lower. Normally all of the after-strings come first, but zero-sized
3131 overlays have their after-strings ride along with the before-strings
3132 because it would look strange to print them inside-out.
3134 Returns the string length, and stores the contents indirectly through
3135 PSTR, if that variable is non-null. The string may be overwritten by
3136 subsequent calls. */
3139 overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
3141 Lisp_Object overlay, window, str;
3142 struct Lisp_Overlay *ov;
3143 int startpos, endpos;
3144 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3146 overlay_heads.used = overlay_heads.bytes = 0;
3147 overlay_tails.used = overlay_tails.bytes = 0;
3148 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3150 XSETMISC (overlay, ov);
3151 eassert (OVERLAYP (overlay));
3153 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3154 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3155 if (endpos < pos)
3156 break;
3157 if (endpos != pos && startpos != pos)
3158 continue;
3159 window = Foverlay_get (overlay, Qwindow);
3160 if (WINDOWP (window) && XWINDOW (window) != w)
3161 continue;
3162 if (startpos == pos
3163 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3164 record_overlay_string (&overlay_heads, str,
3165 (startpos == endpos
3166 ? Foverlay_get (overlay, Qafter_string)
3167 : Qnil),
3168 Foverlay_get (overlay, Qpriority),
3169 endpos - startpos);
3170 else if (endpos == pos
3171 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3172 record_overlay_string (&overlay_tails, str, Qnil,
3173 Foverlay_get (overlay, Qpriority),
3174 endpos - startpos);
3176 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3178 XSETMISC (overlay, ov);
3179 eassert (OVERLAYP (overlay));
3181 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3182 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3183 if (startpos > pos)
3184 break;
3185 if (endpos != pos && startpos != pos)
3186 continue;
3187 window = Foverlay_get (overlay, Qwindow);
3188 if (WINDOWP (window) && XWINDOW (window) != w)
3189 continue;
3190 if (startpos == pos
3191 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3192 record_overlay_string (&overlay_heads, str,
3193 (startpos == endpos
3194 ? Foverlay_get (overlay, Qafter_string)
3195 : Qnil),
3196 Foverlay_get (overlay, Qpriority),
3197 endpos - startpos);
3198 else if (endpos == pos
3199 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3200 record_overlay_string (&overlay_tails, str, Qnil,
3201 Foverlay_get (overlay, Qpriority),
3202 endpos - startpos);
3204 if (overlay_tails.used > 1)
3205 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3206 cmp_for_strings);
3207 if (overlay_heads.used > 1)
3208 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3209 cmp_for_strings);
3210 if (overlay_heads.bytes || overlay_tails.bytes)
3212 Lisp_Object tem;
3213 int i;
3214 unsigned char *p;
3215 int total = overlay_heads.bytes + overlay_tails.bytes;
3217 if (total > overlay_str_len)
3219 overlay_str_len = total;
3220 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3221 total);
3223 p = overlay_str_buf;
3224 for (i = overlay_tails.used; --i >= 0;)
3226 int nbytes;
3227 tem = overlay_tails.buf[i].string;
3228 nbytes = copy_text (SDATA (tem), p,
3229 SBYTES (tem),
3230 STRING_MULTIBYTE (tem), multibyte);
3231 p += nbytes;
3233 for (i = 0; i < overlay_heads.used; ++i)
3235 int nbytes;
3236 tem = overlay_heads.buf[i].string;
3237 nbytes = copy_text (SDATA (tem), p,
3238 SBYTES (tem),
3239 STRING_MULTIBYTE (tem), multibyte);
3240 p += nbytes;
3241 tem = overlay_heads.buf[i].string2;
3242 if (STRINGP (tem))
3244 nbytes = copy_text (SDATA (tem), p,
3245 SBYTES (tem),
3246 STRING_MULTIBYTE (tem), multibyte);
3247 p += nbytes;
3250 if (p != overlay_str_buf + total)
3251 abort ();
3252 if (pstr)
3253 *pstr = overlay_str_buf;
3254 return total;
3256 return 0;
3259 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3261 void
3262 recenter_overlay_lists (struct buffer *buf, EMACS_INT pos)
3264 Lisp_Object overlay, beg, end;
3265 struct Lisp_Overlay *prev, *tail, *next;
3267 /* See if anything in overlays_before should move to overlays_after. */
3269 /* We don't strictly need prev in this loop; it should always be nil.
3270 But we use it for symmetry and in case that should cease to be true
3271 with some future change. */
3272 prev = NULL;
3273 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3275 next = tail->next;
3276 XSETMISC (overlay, tail);
3278 /* If the overlay is not valid, get rid of it. */
3279 if (!OVERLAY_VALID (overlay))
3280 #if 1
3281 abort ();
3282 #else
3284 /* Splice the cons cell TAIL out of overlays_before. */
3285 if (!NILP (prev))
3286 XCDR (prev) = next;
3287 else
3288 buf->overlays_before = next;
3289 tail = prev;
3290 continue;
3292 #endif
3294 beg = OVERLAY_START (overlay);
3295 end = OVERLAY_END (overlay);
3297 if (OVERLAY_POSITION (end) > pos)
3299 /* OVERLAY needs to be moved. */
3300 int where = OVERLAY_POSITION (beg);
3301 struct Lisp_Overlay *other, *other_prev;
3303 /* Splice the cons cell TAIL out of overlays_before. */
3304 if (prev)
3305 prev->next = next;
3306 else
3307 buf->overlays_before = next;
3309 /* Search thru overlays_after for where to put it. */
3310 other_prev = NULL;
3311 for (other = buf->overlays_after; other;
3312 other_prev = other, other = other->next)
3314 Lisp_Object otherbeg, otheroverlay;
3316 XSETMISC (otheroverlay, other);
3317 eassert (OVERLAY_VALID (otheroverlay));
3319 otherbeg = OVERLAY_START (otheroverlay);
3320 if (OVERLAY_POSITION (otherbeg) >= where)
3321 break;
3324 /* Add TAIL to overlays_after before OTHER. */
3325 tail->next = other;
3326 if (other_prev)
3327 other_prev->next = tail;
3328 else
3329 buf->overlays_after = tail;
3330 tail = prev;
3332 else
3333 /* We've reached the things that should stay in overlays_before.
3334 All the rest of overlays_before must end even earlier,
3335 so stop now. */
3336 break;
3339 /* See if anything in overlays_after should be in overlays_before. */
3340 prev = NULL;
3341 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3343 next = tail->next;
3344 XSETMISC (overlay, tail);
3346 /* If the overlay is not valid, get rid of it. */
3347 if (!OVERLAY_VALID (overlay))
3348 #if 1
3349 abort ();
3350 #else
3352 /* Splice the cons cell TAIL out of overlays_after. */
3353 if (!NILP (prev))
3354 XCDR (prev) = next;
3355 else
3356 buf->overlays_after = next;
3357 tail = prev;
3358 continue;
3360 #endif
3362 beg = OVERLAY_START (overlay);
3363 end = OVERLAY_END (overlay);
3365 /* Stop looking, when we know that nothing further
3366 can possibly end before POS. */
3367 if (OVERLAY_POSITION (beg) > pos)
3368 break;
3370 if (OVERLAY_POSITION (end) <= pos)
3372 /* OVERLAY needs to be moved. */
3373 int where = OVERLAY_POSITION (end);
3374 struct Lisp_Overlay *other, *other_prev;
3376 /* Splice the cons cell TAIL out of overlays_after. */
3377 if (prev)
3378 prev->next = next;
3379 else
3380 buf->overlays_after = next;
3382 /* Search thru overlays_before for where to put it. */
3383 other_prev = NULL;
3384 for (other = buf->overlays_before; other;
3385 other_prev = other, other = other->next)
3387 Lisp_Object otherend, otheroverlay;
3389 XSETMISC (otheroverlay, other);
3390 eassert (OVERLAY_VALID (otheroverlay));
3392 otherend = OVERLAY_END (otheroverlay);
3393 if (OVERLAY_POSITION (otherend) <= where)
3394 break;
3397 /* Add TAIL to overlays_before before OTHER. */
3398 tail->next = other;
3399 if (other_prev)
3400 other_prev->next = tail;
3401 else
3402 buf->overlays_before = tail;
3403 tail = prev;
3407 buf->overlay_center = pos;
3410 void
3411 adjust_overlays_for_insert (EMACS_INT pos, EMACS_INT length)
3413 /* After an insertion, the lists are still sorted properly,
3414 but we may need to update the value of the overlay center. */
3415 if (current_buffer->overlay_center >= pos)
3416 current_buffer->overlay_center += length;
3419 void
3420 adjust_overlays_for_delete (EMACS_INT pos, EMACS_INT length)
3422 if (current_buffer->overlay_center < pos)
3423 /* The deletion was to our right. No change needed; the before- and
3424 after-lists are still consistent. */
3426 else if (current_buffer->overlay_center > pos + length)
3427 /* The deletion was to our left. We need to adjust the center value
3428 to account for the change in position, but the lists are consistent
3429 given the new value. */
3430 current_buffer->overlay_center -= length;
3431 else
3432 /* We're right in the middle. There might be things on the after-list
3433 that now belong on the before-list. Recentering will move them,
3434 and also update the center point. */
3435 recenter_overlay_lists (current_buffer, pos);
3438 /* Fix up overlays that were garbled as a result of permuting markers
3439 in the range START through END. Any overlay with at least one
3440 endpoint in this range will need to be unlinked from the overlay
3441 list and reinserted in its proper place.
3442 Such an overlay might even have negative size at this point.
3443 If so, we'll make the overlay empty. */
3444 void
3445 fix_start_end_in_overlays (register int start, register int end)
3447 Lisp_Object overlay;
3448 struct Lisp_Overlay *before_list, *after_list;
3449 /* These are either nil, indicating that before_list or after_list
3450 should be assigned, or the cons cell the cdr of which should be
3451 assigned. */
3452 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3453 /* 'Parent', likewise, indicates a cons cell or
3454 current_buffer->overlays_before or overlays_after, depending
3455 which loop we're in. */
3456 struct Lisp_Overlay *tail, *parent;
3457 int startpos, endpos;
3459 /* This algorithm shifts links around instead of consing and GCing.
3460 The loop invariant is that before_list (resp. after_list) is a
3461 well-formed list except that its last element, the CDR of beforep
3462 (resp. afterp) if beforep (afterp) isn't nil or before_list
3463 (after_list) if it is, is still uninitialized. So it's not a bug
3464 that before_list isn't initialized, although it may look
3465 strange. */
3466 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3468 XSETMISC (overlay, tail);
3470 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3471 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3473 /* If the overlay is backwards, make it empty. */
3474 if (endpos < startpos)
3476 startpos = endpos;
3477 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3478 Qnil);
3481 if (endpos < start)
3482 break;
3484 if (endpos < end
3485 || (startpos >= start && startpos < end))
3487 /* Add it to the end of the wrong list. Later on,
3488 recenter_overlay_lists will move it to the right place. */
3489 if (endpos < current_buffer->overlay_center)
3491 if (!afterp)
3492 after_list = tail;
3493 else
3494 afterp->next = tail;
3495 afterp = tail;
3497 else
3499 if (!beforep)
3500 before_list = tail;
3501 else
3502 beforep->next = tail;
3503 beforep = tail;
3505 if (!parent)
3506 current_buffer->overlays_before = tail->next;
3507 else
3508 parent->next = tail->next;
3509 tail = tail->next;
3511 else
3512 parent = tail, tail = parent->next;
3514 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3516 XSETMISC (overlay, tail);
3518 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3519 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3521 /* If the overlay is backwards, make it empty. */
3522 if (endpos < startpos)
3524 startpos = endpos;
3525 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3526 Qnil);
3529 if (startpos >= end)
3530 break;
3532 if (startpos >= start
3533 || (endpos >= start && endpos < end))
3535 if (endpos < current_buffer->overlay_center)
3537 if (!afterp)
3538 after_list = tail;
3539 else
3540 afterp->next = tail;
3541 afterp = tail;
3543 else
3545 if (!beforep)
3546 before_list = tail;
3547 else
3548 beforep->next = tail;
3549 beforep = tail;
3551 if (!parent)
3552 current_buffer->overlays_after = tail->next;
3553 else
3554 parent->next = tail->next;
3555 tail = tail->next;
3557 else
3558 parent = tail, tail = parent->next;
3561 /* Splice the constructed (wrong) lists into the buffer's lists,
3562 and let the recenter function make it sane again. */
3563 if (beforep)
3565 beforep->next = current_buffer->overlays_before;
3566 current_buffer->overlays_before = before_list;
3568 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3570 if (afterp)
3572 afterp->next = current_buffer->overlays_after;
3573 current_buffer->overlays_after = after_list;
3575 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3578 /* We have two types of overlay: the one whose ending marker is
3579 after-insertion-marker (this is the usual case) and the one whose
3580 ending marker is before-insertion-marker. When `overlays_before'
3581 contains overlays of the latter type and the former type in this
3582 order and both overlays end at inserting position, inserting a text
3583 increases only the ending marker of the latter type, which results
3584 in incorrect ordering of `overlays_before'.
3586 This function fixes ordering of overlays in the slot
3587 `overlays_before' of the buffer *BP. Before the insertion, `point'
3588 was at PREV, and now is at POS. */
3590 void
3591 fix_overlays_before (struct buffer *bp, EMACS_INT prev, EMACS_INT pos)
3593 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3594 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3595 Lisp_Object tem;
3596 EMACS_INT end;
3598 /* After the insertion, the several overlays may be in incorrect
3599 order. The possibility is that, in the list `overlays_before',
3600 an overlay which ends at POS appears after an overlay which ends
3601 at PREV. Since POS is greater than PREV, we must fix the
3602 ordering of these overlays, by moving overlays ends at POS before
3603 the overlays ends at PREV. */
3605 /* At first, find a place where disordered overlays should be linked
3606 in. It is where an overlay which end before POS exists. (i.e. an
3607 overlay whose ending marker is after-insertion-marker if disorder
3608 exists). */
3609 while (tail
3610 && (XSETMISC (tem, tail),
3611 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3613 parent = tail;
3614 tail = tail->next;
3617 /* If we don't find such an overlay,
3618 or the found one ends before PREV,
3619 or the found one is the last one in the list,
3620 we don't have to fix anything. */
3621 if (!tail || end < prev || !tail->next)
3622 return;
3624 right_pair = parent;
3625 parent = tail;
3626 tail = tail->next;
3628 /* Now, end position of overlays in the list TAIL should be before
3629 or equal to PREV. In the loop, an overlay which ends at POS is
3630 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3631 we found an overlay which ends before PREV, the remaining
3632 overlays are in correct order. */
3633 while (tail)
3635 XSETMISC (tem, tail);
3636 end = OVERLAY_POSITION (OVERLAY_END (tem));
3638 if (end == pos)
3639 { /* This overlay is disordered. */
3640 struct Lisp_Overlay *found = tail;
3642 /* Unlink the found overlay. */
3643 tail = found->next;
3644 parent->next = tail;
3645 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3646 and link it into the right place. */
3647 if (!right_pair)
3649 found->next = bp->overlays_before;
3650 bp->overlays_before = found;
3652 else
3654 found->next = right_pair->next;
3655 right_pair->next = found;
3658 else if (end == prev)
3660 parent = tail;
3661 tail = tail->next;
3663 else /* No more disordered overlay. */
3664 break;
3668 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3669 doc: /* Return t if OBJECT is an overlay. */)
3670 (Lisp_Object object)
3672 return (OVERLAYP (object) ? Qt : Qnil);
3675 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3676 doc: /* Create a new overlay with range BEG to END in BUFFER.
3677 If omitted, BUFFER defaults to the current buffer.
3678 BEG and END may be integers or markers.
3679 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3680 for the front of the overlay advance when text is inserted there
3681 \(which means the text *is not* included in the overlay).
3682 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3683 for the rear of the overlay advance when text is inserted there
3684 \(which means the text *is* included in the overlay). */)
3685 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
3687 Lisp_Object overlay;
3688 struct buffer *b;
3690 if (NILP (buffer))
3691 XSETBUFFER (buffer, current_buffer);
3692 else
3693 CHECK_BUFFER (buffer);
3694 if (MARKERP (beg)
3695 && ! EQ (Fmarker_buffer (beg), buffer))
3696 error ("Marker points into wrong buffer");
3697 if (MARKERP (end)
3698 && ! EQ (Fmarker_buffer (end), buffer))
3699 error ("Marker points into wrong buffer");
3701 CHECK_NUMBER_COERCE_MARKER (beg);
3702 CHECK_NUMBER_COERCE_MARKER (end);
3704 if (XINT (beg) > XINT (end))
3706 Lisp_Object temp;
3707 temp = beg; beg = end; end = temp;
3710 b = XBUFFER (buffer);
3712 beg = Fset_marker (Fmake_marker (), beg, buffer);
3713 end = Fset_marker (Fmake_marker (), end, buffer);
3715 if (!NILP (front_advance))
3716 XMARKER (beg)->insertion_type = 1;
3717 if (!NILP (rear_advance))
3718 XMARKER (end)->insertion_type = 1;
3720 overlay = allocate_misc ();
3721 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3722 XOVERLAY (overlay)->start = beg;
3723 XOVERLAY (overlay)->end = end;
3724 XOVERLAY (overlay)->plist = Qnil;
3725 XOVERLAY (overlay)->next = NULL;
3727 /* Put the new overlay on the wrong list. */
3728 end = OVERLAY_END (overlay);
3729 if (OVERLAY_POSITION (end) < b->overlay_center)
3731 if (b->overlays_after)
3732 XOVERLAY (overlay)->next = b->overlays_after;
3733 b->overlays_after = XOVERLAY (overlay);
3735 else
3737 if (b->overlays_before)
3738 XOVERLAY (overlay)->next = b->overlays_before;
3739 b->overlays_before = XOVERLAY (overlay);
3742 /* This puts it in the right list, and in the right order. */
3743 recenter_overlay_lists (b, b->overlay_center);
3745 /* We don't need to redisplay the region covered by the overlay, because
3746 the overlay has no properties at the moment. */
3748 return overlay;
3751 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3753 static void
3754 modify_overlay (struct buffer *buf, EMACS_INT start, EMACS_INT end)
3756 if (start > end)
3758 int temp = start;
3759 start = end;
3760 end = temp;
3763 BUF_COMPUTE_UNCHANGED (buf, start, end);
3765 /* If this is a buffer not in the selected window,
3766 we must do other windows. */
3767 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3768 windows_or_buffers_changed = 1;
3769 /* If multiple windows show this buffer, we must do other windows. */
3770 else if (buffer_shared > 1)
3771 windows_or_buffers_changed = 1;
3772 /* If we modify an overlay at the end of the buffer, we cannot
3773 be sure that window end is still valid. */
3774 else if (end >= ZV && start <= ZV)
3775 windows_or_buffers_changed = 1;
3777 ++BUF_OVERLAY_MODIFF (buf);
3781 Lisp_Object Fdelete_overlay (Lisp_Object overlay);
3783 static struct Lisp_Overlay *
3784 unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
3786 struct Lisp_Overlay *tmp, *prev;
3787 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3788 if (tmp == overlay)
3790 if (prev)
3791 prev->next = tmp->next;
3792 else
3793 list = tmp->next;
3794 overlay->next = NULL;
3795 break;
3797 return list;
3800 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3801 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3802 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3803 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3804 buffer. */)
3805 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
3807 struct buffer *b, *ob;
3808 Lisp_Object obuffer;
3809 int count = SPECPDL_INDEX ();
3811 CHECK_OVERLAY (overlay);
3812 if (NILP (buffer))
3813 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3814 if (NILP (buffer))
3815 XSETBUFFER (buffer, current_buffer);
3816 CHECK_BUFFER (buffer);
3818 if (MARKERP (beg)
3819 && ! EQ (Fmarker_buffer (beg), buffer))
3820 error ("Marker points into wrong buffer");
3821 if (MARKERP (end)
3822 && ! EQ (Fmarker_buffer (end), buffer))
3823 error ("Marker points into wrong buffer");
3825 CHECK_NUMBER_COERCE_MARKER (beg);
3826 CHECK_NUMBER_COERCE_MARKER (end);
3828 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3829 return Fdelete_overlay (overlay);
3831 if (XINT (beg) > XINT (end))
3833 Lisp_Object temp;
3834 temp = beg; beg = end; end = temp;
3837 specbind (Qinhibit_quit, Qt);
3839 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3840 b = XBUFFER (buffer);
3841 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3843 /* If the overlay has changed buffers, do a thorough redisplay. */
3844 if (!EQ (buffer, obuffer))
3846 /* Redisplay where the overlay was. */
3847 if (!NILP (obuffer))
3849 int o_beg;
3850 int o_end;
3852 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3853 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3855 modify_overlay (ob, o_beg, o_end);
3858 /* Redisplay where the overlay is going to be. */
3859 modify_overlay (b, XINT (beg), XINT (end));
3861 else
3862 /* Redisplay the area the overlay has just left, or just enclosed. */
3864 int o_beg, o_end;
3866 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3867 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3869 if (o_beg == XINT (beg))
3870 modify_overlay (b, o_end, XINT (end));
3871 else if (o_end == XINT (end))
3872 modify_overlay (b, o_beg, XINT (beg));
3873 else
3875 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3876 if (XINT (end) > o_end) o_end = XINT (end);
3877 modify_overlay (b, o_beg, o_end);
3881 if (!NILP (obuffer))
3883 ob->overlays_before
3884 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3885 ob->overlays_after
3886 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3887 eassert (XOVERLAY (overlay)->next == NULL);
3890 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3891 Fset_marker (OVERLAY_END (overlay), end, buffer);
3893 /* Put the overlay on the wrong list. */
3894 end = OVERLAY_END (overlay);
3895 if (OVERLAY_POSITION (end) < b->overlay_center)
3897 XOVERLAY (overlay)->next = b->overlays_after;
3898 b->overlays_after = XOVERLAY (overlay);
3900 else
3902 XOVERLAY (overlay)->next = b->overlays_before;
3903 b->overlays_before = XOVERLAY (overlay);
3906 /* This puts it in the right list, and in the right order. */
3907 recenter_overlay_lists (b, b->overlay_center);
3909 return unbind_to (count, overlay);
3912 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3913 doc: /* Delete the overlay OVERLAY from its buffer. */)
3914 (Lisp_Object overlay)
3916 Lisp_Object buffer;
3917 struct buffer *b;
3918 int count = SPECPDL_INDEX ();
3920 CHECK_OVERLAY (overlay);
3922 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3923 if (NILP (buffer))
3924 return Qnil;
3926 b = XBUFFER (buffer);
3927 specbind (Qinhibit_quit, Qt);
3929 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3930 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3931 eassert (XOVERLAY (overlay)->next == NULL);
3932 modify_overlay (b,
3933 marker_position (OVERLAY_START (overlay)),
3934 marker_position (OVERLAY_END (overlay)));
3935 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3936 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3938 /* When deleting an overlay with before or after strings, turn off
3939 display optimizations for the affected buffer, on the basis that
3940 these strings may contain newlines. This is easier to do than to
3941 check for that situation during redisplay. */
3942 if (!windows_or_buffers_changed
3943 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3944 || !NILP (Foverlay_get (overlay, Qafter_string))))
3945 b->prevent_redisplay_optimizations_p = 1;
3947 return unbind_to (count, Qnil);
3950 /* Overlay dissection functions. */
3952 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3953 doc: /* Return the position at which OVERLAY starts. */)
3954 (Lisp_Object overlay)
3956 CHECK_OVERLAY (overlay);
3958 return (Fmarker_position (OVERLAY_START (overlay)));
3961 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3962 doc: /* Return the position at which OVERLAY ends. */)
3963 (Lisp_Object overlay)
3965 CHECK_OVERLAY (overlay);
3967 return (Fmarker_position (OVERLAY_END (overlay)));
3970 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3971 doc: /* Return the buffer OVERLAY belongs to.
3972 Return nil if OVERLAY has been deleted. */)
3973 (Lisp_Object overlay)
3975 CHECK_OVERLAY (overlay);
3977 return Fmarker_buffer (OVERLAY_START (overlay));
3980 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3981 doc: /* Return a list of the properties on OVERLAY.
3982 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3983 OVERLAY. */)
3984 (Lisp_Object overlay)
3986 CHECK_OVERLAY (overlay);
3988 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3992 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3993 doc: /* Return a list of the overlays that contain the character at POS. */)
3994 (Lisp_Object pos)
3996 int noverlays;
3997 Lisp_Object *overlay_vec;
3998 int len;
3999 Lisp_Object result;
4001 CHECK_NUMBER_COERCE_MARKER (pos);
4003 len = 10;
4004 /* We can't use alloca here because overlays_at can call xrealloc. */
4005 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4007 /* Put all the overlays we want in a vector in overlay_vec.
4008 Store the length in len. */
4009 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4010 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4012 /* Make a list of them all. */
4013 result = Flist (noverlays, overlay_vec);
4015 xfree (overlay_vec);
4016 return result;
4019 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4020 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4021 Overlap means that at least one character is contained within the overlay
4022 and also contained within the specified region.
4023 Empty overlays are included in the result if they are located at BEG,
4024 between BEG and END, or at END provided END denotes the position at the
4025 end of the buffer. */)
4026 (Lisp_Object beg, Lisp_Object end)
4028 int noverlays;
4029 Lisp_Object *overlay_vec;
4030 int len;
4031 Lisp_Object result;
4033 CHECK_NUMBER_COERCE_MARKER (beg);
4034 CHECK_NUMBER_COERCE_MARKER (end);
4036 len = 10;
4037 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4039 /* Put all the overlays we want in a vector in overlay_vec.
4040 Store the length in len. */
4041 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4042 (int *) 0, (int *) 0);
4044 /* Make a list of them all. */
4045 result = Flist (noverlays, overlay_vec);
4047 xfree (overlay_vec);
4048 return result;
4051 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4052 1, 1, 0,
4053 doc: /* Return the next position after POS where an overlay starts or ends.
4054 If there are no overlay boundaries from POS to (point-max),
4055 the value is (point-max). */)
4056 (Lisp_Object pos)
4058 int noverlays;
4059 EMACS_INT endpos;
4060 Lisp_Object *overlay_vec;
4061 int len;
4062 int i;
4064 CHECK_NUMBER_COERCE_MARKER (pos);
4066 len = 10;
4067 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4069 /* Put all the overlays we want in a vector in overlay_vec.
4070 Store the length in len.
4071 endpos gets the position where the next overlay starts. */
4072 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4073 &endpos, (EMACS_INT *) 0, 1);
4075 /* If any of these overlays ends before endpos,
4076 use its ending point instead. */
4077 for (i = 0; i < noverlays; i++)
4079 Lisp_Object oend;
4080 EMACS_INT oendpos;
4082 oend = OVERLAY_END (overlay_vec[i]);
4083 oendpos = OVERLAY_POSITION (oend);
4084 if (oendpos < endpos)
4085 endpos = oendpos;
4088 xfree (overlay_vec);
4089 return make_number (endpos);
4092 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4093 Sprevious_overlay_change, 1, 1, 0,
4094 doc: /* Return the previous position before POS where an overlay starts or ends.
4095 If there are no overlay boundaries from (point-min) to POS,
4096 the value is (point-min). */)
4097 (Lisp_Object pos)
4099 int noverlays;
4100 EMACS_INT prevpos;
4101 Lisp_Object *overlay_vec;
4102 int len;
4104 CHECK_NUMBER_COERCE_MARKER (pos);
4106 /* At beginning of buffer, we know the answer;
4107 avoid bug subtracting 1 below. */
4108 if (XINT (pos) == BEGV)
4109 return pos;
4111 len = 10;
4112 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4114 /* Put all the overlays we want in a vector in overlay_vec.
4115 Store the length in len.
4116 prevpos gets the position of the previous change. */
4117 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4118 (EMACS_INT *) 0, &prevpos, 1);
4120 xfree (overlay_vec);
4121 return make_number (prevpos);
4124 /* These functions are for debugging overlays. */
4126 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4127 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4128 The car has all the overlays before the overlay center;
4129 the cdr has all the overlays after the overlay center.
4130 Recentering overlays moves overlays between these lists.
4131 The lists you get are copies, so that changing them has no effect.
4132 However, the overlays you get are the real objects that the buffer uses. */)
4133 (void)
4135 struct Lisp_Overlay *ol;
4136 Lisp_Object before = Qnil, after = Qnil, tmp;
4137 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4139 XSETMISC (tmp, ol);
4140 before = Fcons (tmp, before);
4142 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4144 XSETMISC (tmp, ol);
4145 after = Fcons (tmp, after);
4147 return Fcons (Fnreverse (before), Fnreverse (after));
4150 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4151 doc: /* Recenter the overlays of the current buffer around position POS.
4152 That makes overlay lookup faster for positions near POS (but perhaps slower
4153 for positions far away from POS). */)
4154 (Lisp_Object pos)
4156 CHECK_NUMBER_COERCE_MARKER (pos);
4158 recenter_overlay_lists (current_buffer, XINT (pos));
4159 return Qnil;
4162 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4163 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4164 (Lisp_Object overlay, Lisp_Object prop)
4166 CHECK_OVERLAY (overlay);
4167 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4170 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4171 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4172 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
4174 Lisp_Object tail, buffer;
4175 int changed;
4177 CHECK_OVERLAY (overlay);
4179 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4181 for (tail = XOVERLAY (overlay)->plist;
4182 CONSP (tail) && CONSP (XCDR (tail));
4183 tail = XCDR (XCDR (tail)))
4184 if (EQ (XCAR (tail), prop))
4186 changed = !EQ (XCAR (XCDR (tail)), value);
4187 XSETCAR (XCDR (tail), value);
4188 goto found;
4190 /* It wasn't in the list, so add it to the front. */
4191 changed = !NILP (value);
4192 XOVERLAY (overlay)->plist
4193 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4194 found:
4195 if (! NILP (buffer))
4197 if (changed)
4198 modify_overlay (XBUFFER (buffer),
4199 marker_position (OVERLAY_START (overlay)),
4200 marker_position (OVERLAY_END (overlay)));
4201 if (EQ (prop, Qevaporate) && ! NILP (value)
4202 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4203 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4204 Fdelete_overlay (overlay);
4207 return value;
4210 /* Subroutine of report_overlay_modification. */
4212 /* Lisp vector holding overlay hook functions to call.
4213 Vector elements come in pairs.
4214 Each even-index element is a list of hook functions.
4215 The following odd-index element is the overlay they came from.
4217 Before the buffer change, we fill in this vector
4218 as we call overlay hook functions.
4219 After the buffer change, we get the functions to call from this vector.
4220 This way we always call the same functions before and after the change. */
4221 static Lisp_Object last_overlay_modification_hooks;
4223 /* Number of elements actually used in last_overlay_modification_hooks. */
4224 static int last_overlay_modification_hooks_used;
4226 /* Add one functionlist/overlay pair
4227 to the end of last_overlay_modification_hooks. */
4229 static void
4230 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
4232 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4234 if (last_overlay_modification_hooks_used == oldsize)
4235 last_overlay_modification_hooks = larger_vector
4236 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4237 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4238 functionlist); last_overlay_modification_hooks_used++;
4239 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4240 overlay); last_overlay_modification_hooks_used++;
4243 /* Run the modification-hooks of overlays that include
4244 any part of the text in START to END.
4245 If this change is an insertion, also
4246 run the insert-before-hooks of overlay starting at END,
4247 and the insert-after-hooks of overlay ending at START.
4249 This is called both before and after the modification.
4250 AFTER is nonzero when we call after the modification.
4252 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4253 When AFTER is nonzero, they are the start position,
4254 the position after the inserted new text,
4255 and the length of deleted or replaced old text. */
4257 void
4258 report_overlay_modification (Lisp_Object start, Lisp_Object end, int after,
4259 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4261 Lisp_Object prop, overlay;
4262 struct Lisp_Overlay *tail;
4263 /* 1 if this change is an insertion. */
4264 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4265 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4267 overlay = Qnil;
4268 tail = NULL;
4270 /* We used to run the functions as soon as we found them and only register
4271 them in last_overlay_modification_hooks for the purpose of the `after'
4272 case. But running elisp code as we traverse the list of overlays is
4273 painful because the list can be modified by the elisp code so we had to
4274 copy at several places. We now simply do a read-only traversal that
4275 only collects the functions to run and we run them afterwards. It's
4276 simpler, especially since all the code was already there. -stef */
4278 if (!after)
4280 /* We are being called before a change.
4281 Scan the overlays to find the functions to call. */
4282 last_overlay_modification_hooks_used = 0;
4283 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4285 int startpos, endpos;
4286 Lisp_Object ostart, oend;
4288 XSETMISC (overlay, tail);
4290 ostart = OVERLAY_START (overlay);
4291 oend = OVERLAY_END (overlay);
4292 endpos = OVERLAY_POSITION (oend);
4293 if (XFASTINT (start) > endpos)
4294 break;
4295 startpos = OVERLAY_POSITION (ostart);
4296 if (insertion && (XFASTINT (start) == startpos
4297 || XFASTINT (end) == startpos))
4299 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4300 if (!NILP (prop))
4301 add_overlay_mod_hooklist (prop, overlay);
4303 if (insertion && (XFASTINT (start) == endpos
4304 || XFASTINT (end) == endpos))
4306 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4307 if (!NILP (prop))
4308 add_overlay_mod_hooklist (prop, overlay);
4310 /* Test for intersecting intervals. This does the right thing
4311 for both insertion and deletion. */
4312 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4314 prop = Foverlay_get (overlay, Qmodification_hooks);
4315 if (!NILP (prop))
4316 add_overlay_mod_hooklist (prop, overlay);
4320 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4322 int startpos, endpos;
4323 Lisp_Object ostart, oend;
4325 XSETMISC (overlay, tail);
4327 ostart = OVERLAY_START (overlay);
4328 oend = OVERLAY_END (overlay);
4329 startpos = OVERLAY_POSITION (ostart);
4330 endpos = OVERLAY_POSITION (oend);
4331 if (XFASTINT (end) < startpos)
4332 break;
4333 if (insertion && (XFASTINT (start) == startpos
4334 || XFASTINT (end) == startpos))
4336 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4337 if (!NILP (prop))
4338 add_overlay_mod_hooklist (prop, overlay);
4340 if (insertion && (XFASTINT (start) == endpos
4341 || XFASTINT (end) == endpos))
4343 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4344 if (!NILP (prop))
4345 add_overlay_mod_hooklist (prop, overlay);
4347 /* Test for intersecting intervals. This does the right thing
4348 for both insertion and deletion. */
4349 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4351 prop = Foverlay_get (overlay, Qmodification_hooks);
4352 if (!NILP (prop))
4353 add_overlay_mod_hooklist (prop, overlay);
4358 GCPRO4 (overlay, arg1, arg2, arg3);
4360 /* Call the functions recorded in last_overlay_modification_hooks.
4361 First copy the vector contents, in case some of these hooks
4362 do subsequent modification of the buffer. */
4363 int size = last_overlay_modification_hooks_used;
4364 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4365 int i;
4367 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
4368 size * sizeof (Lisp_Object));
4369 gcpro1.var = copy;
4370 gcpro1.nvars = size;
4372 for (i = 0; i < size;)
4374 Lisp_Object prop, overlay;
4375 prop = copy[i++];
4376 overlay = copy[i++];
4377 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4380 UNGCPRO;
4383 static void
4384 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, int after,
4385 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
4387 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4389 GCPRO4 (list, arg1, arg2, arg3);
4391 while (CONSP (list))
4393 if (NILP (arg3))
4394 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4395 else
4396 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4397 list = XCDR (list);
4399 UNGCPRO;
4402 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4403 property is set. */
4404 void
4405 evaporate_overlays (EMACS_INT pos)
4407 Lisp_Object overlay, hit_list;
4408 struct Lisp_Overlay *tail;
4410 hit_list = Qnil;
4411 if (pos <= current_buffer->overlay_center)
4412 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4414 int endpos;
4415 XSETMISC (overlay, tail);
4416 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4417 if (endpos < pos)
4418 break;
4419 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4420 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4421 hit_list = Fcons (overlay, hit_list);
4423 else
4424 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4426 int startpos;
4427 XSETMISC (overlay, tail);
4428 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4429 if (startpos > pos)
4430 break;
4431 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4432 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4433 hit_list = Fcons (overlay, hit_list);
4435 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4436 Fdelete_overlay (XCAR (hit_list));
4439 /* Somebody has tried to store a value with an unacceptable type
4440 in the slot with offset OFFSET. */
4442 void
4443 buffer_slot_type_mismatch (Lisp_Object newval, int type)
4445 Lisp_Object predicate;
4447 switch (type)
4449 case_Lisp_Int: predicate = Qintegerp; break;
4450 case Lisp_String: predicate = Qstringp; break;
4451 case Lisp_Symbol: predicate = Qsymbolp; break;
4452 default: abort ();
4455 wrong_type_argument (predicate, newval);
4459 /***********************************************************************
4460 Allocation with mmap
4461 ***********************************************************************/
4463 #ifdef USE_MMAP_FOR_BUFFERS
4465 #include <sys/types.h>
4466 #include <sys/mman.h>
4468 #ifndef MAP_ANON
4469 #ifdef MAP_ANONYMOUS
4470 #define MAP_ANON MAP_ANONYMOUS
4471 #else
4472 #define MAP_ANON 0
4473 #endif
4474 #endif
4476 #ifndef MAP_FAILED
4477 #define MAP_FAILED ((void *) -1)
4478 #endif
4480 #include <stdio.h>
4482 #if MAP_ANON == 0
4483 #include <fcntl.h>
4484 #endif
4486 #include "coding.h"
4489 /* Memory is allocated in regions which are mapped using mmap(2).
4490 The current implementation lets the system select mapped
4491 addresses; we're not using MAP_FIXED in general, except when
4492 trying to enlarge regions.
4494 Each mapped region starts with a mmap_region structure, the user
4495 area starts after that structure, aligned to MEM_ALIGN.
4497 +-----------------------+
4498 | struct mmap_info + |
4499 | padding |
4500 +-----------------------+
4501 | user data |
4504 +-----------------------+ */
4506 struct mmap_region
4508 /* User-specified size. */
4509 size_t nbytes_specified;
4511 /* Number of bytes mapped */
4512 size_t nbytes_mapped;
4514 /* Pointer to the location holding the address of the memory
4515 allocated with the mmap'd block. The variable actually points
4516 after this structure. */
4517 POINTER_TYPE **var;
4519 /* Next and previous in list of all mmap'd regions. */
4520 struct mmap_region *next, *prev;
4523 /* Doubly-linked list of mmap'd regions. */
4525 static struct mmap_region *mmap_regions;
4527 /* File descriptor for mmap. If we don't have anonymous mapping,
4528 /dev/zero will be opened on it. */
4530 static int mmap_fd;
4532 /* Temporary storage for mmap_set_vars, see there. */
4534 static struct mmap_region *mmap_regions_1;
4535 static int mmap_fd_1;
4537 /* Page size on this system. */
4539 static int mmap_page_size;
4541 /* 1 means mmap has been intialized. */
4543 static int mmap_initialized_p;
4545 /* Value is X rounded up to the next multiple of N. */
4547 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4549 /* Size of mmap_region structure plus padding. */
4551 #define MMAP_REGION_STRUCT_SIZE \
4552 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4554 /* Given a pointer P to the start of the user-visible part of a mapped
4555 region, return a pointer to the start of the region. */
4557 #define MMAP_REGION(P) \
4558 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4560 /* Given a pointer P to the start of a mapped region, return a pointer
4561 to the start of the user-visible part of the region. */
4563 #define MMAP_USER_AREA(P) \
4564 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4566 #define MEM_ALIGN sizeof (double)
4568 /* Predicate returning true if part of the address range [START .. END]
4569 is currently mapped. Used to prevent overwriting an existing
4570 memory mapping.
4572 Default is to conservativly assume the address range is occupied by
4573 something else. This can be overridden by system configuration
4574 files if system-specific means to determine this exists. */
4576 #ifndef MMAP_ALLOCATED_P
4577 #define MMAP_ALLOCATED_P(start, end) 1
4578 #endif
4580 /* Function prototypes. */
4582 static int mmap_free_1 (struct mmap_region *);
4583 static int mmap_enlarge (struct mmap_region *, int);
4584 static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
4585 static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
4586 static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
4587 static void mmap_free (POINTER_TYPE **ptr);
4588 static void mmap_init (void);
4591 /* Return a region overlapping address range START...END, or null if
4592 none. END is not including, i.e. the last byte in the range
4593 is at END - 1. */
4595 static struct mmap_region *
4596 mmap_find (start, end)
4597 POINTER_TYPE *start, *end;
4599 struct mmap_region *r;
4600 char *s = (char *) start, *e = (char *) end;
4602 for (r = mmap_regions; r; r = r->next)
4604 char *rstart = (char *) r;
4605 char *rend = rstart + r->nbytes_mapped;
4607 if (/* First byte of range, i.e. START, in this region? */
4608 (s >= rstart && s < rend)
4609 /* Last byte of range, i.e. END - 1, in this region? */
4610 || (e > rstart && e <= rend)
4611 /* First byte of this region in the range? */
4612 || (rstart >= s && rstart < e)
4613 /* Last byte of this region in the range? */
4614 || (rend > s && rend <= e))
4615 break;
4618 return r;
4622 /* Unmap a region. P is a pointer to the start of the user-araa of
4623 the region. Value is non-zero if successful. */
4625 static int
4626 mmap_free_1 (r)
4627 struct mmap_region *r;
4629 if (r->next)
4630 r->next->prev = r->prev;
4631 if (r->prev)
4632 r->prev->next = r->next;
4633 else
4634 mmap_regions = r->next;
4636 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4638 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4639 return 0;
4642 return 1;
4646 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4647 Value is non-zero if successful. */
4649 static int
4650 mmap_enlarge (r, npages)
4651 struct mmap_region *r;
4652 int npages;
4654 char *region_end = (char *) r + r->nbytes_mapped;
4655 size_t nbytes;
4656 int success = 0;
4658 if (npages < 0)
4660 /* Unmap pages at the end of the region. */
4661 nbytes = - npages * mmap_page_size;
4662 if (munmap (region_end - nbytes, nbytes) == -1)
4663 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4664 else
4666 r->nbytes_mapped -= nbytes;
4667 success = 1;
4670 else if (npages > 0)
4672 nbytes = npages * mmap_page_size;
4674 /* Try to map additional pages at the end of the region. We
4675 cannot do this if the address range is already occupied by
4676 something else because mmap deletes any previous mapping.
4677 I'm not sure this is worth doing, let's see. */
4678 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4680 POINTER_TYPE *p;
4682 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4683 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4684 if (p == MAP_FAILED)
4685 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4686 else if (p != (POINTER_TYPE *) region_end)
4688 /* Kernels are free to choose a different address. In
4689 that case, unmap what we've mapped above; we have
4690 no use for it. */
4691 if (munmap (p, nbytes) == -1)
4692 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4694 else
4696 r->nbytes_mapped += nbytes;
4697 success = 1;
4702 return success;
4706 /* Set or reset variables holding references to mapped regions. If
4707 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4708 non-zero, set all variables to the start of the user-areas
4709 of mapped regions.
4711 This function is called from Fdump_emacs to ensure that the dumped
4712 Emacs doesn't contain references to memory that won't be mapped
4713 when Emacs starts. */
4715 void
4716 mmap_set_vars (restore_p)
4717 int restore_p;
4719 struct mmap_region *r;
4721 if (restore_p)
4723 mmap_regions = mmap_regions_1;
4724 mmap_fd = mmap_fd_1;
4725 for (r = mmap_regions; r; r = r->next)
4726 *r->var = MMAP_USER_AREA (r);
4728 else
4730 for (r = mmap_regions; r; r = r->next)
4731 *r->var = NULL;
4732 mmap_regions_1 = mmap_regions;
4733 mmap_regions = NULL;
4734 mmap_fd_1 = mmap_fd;
4735 mmap_fd = -1;
4740 /* Allocate a block of storage large enough to hold NBYTES bytes of
4741 data. A pointer to the data is returned in *VAR. VAR is thus the
4742 address of some variable which will use the data area.
4744 The allocation of 0 bytes is valid.
4746 If we can't allocate the necessary memory, set *VAR to null, and
4747 return null. */
4749 static POINTER_TYPE *
4750 mmap_alloc (var, nbytes)
4751 POINTER_TYPE **var;
4752 size_t nbytes;
4754 void *p;
4755 size_t map;
4757 mmap_init ();
4759 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4760 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4761 mmap_fd, 0);
4763 if (p == MAP_FAILED)
4765 if (errno != ENOMEM)
4766 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4767 p = NULL;
4769 else
4771 struct mmap_region *r = (struct mmap_region *) p;
4773 r->nbytes_specified = nbytes;
4774 r->nbytes_mapped = map;
4775 r->var = var;
4776 r->prev = NULL;
4777 r->next = mmap_regions;
4778 if (r->next)
4779 r->next->prev = r;
4780 mmap_regions = r;
4782 p = MMAP_USER_AREA (p);
4785 return *var = p;
4789 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4790 resize it to size NBYTES. Change *VAR to reflect the new block,
4791 and return this value. If more memory cannot be allocated, then
4792 leave *VAR unchanged, and return null. */
4794 static POINTER_TYPE *
4795 mmap_realloc (var, nbytes)
4796 POINTER_TYPE **var;
4797 size_t nbytes;
4799 POINTER_TYPE *result;
4801 mmap_init ();
4803 if (*var == NULL)
4804 result = mmap_alloc (var, nbytes);
4805 else if (nbytes == 0)
4807 mmap_free (var);
4808 result = mmap_alloc (var, nbytes);
4810 else
4812 struct mmap_region *r = MMAP_REGION (*var);
4813 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4815 if (room < nbytes)
4817 /* Must enlarge. */
4818 POINTER_TYPE *old_ptr = *var;
4820 /* Try to map additional pages at the end of the region.
4821 If that fails, allocate a new region, copy data
4822 from the old region, then free it. */
4823 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4824 / mmap_page_size)))
4826 r->nbytes_specified = nbytes;
4827 *var = result = old_ptr;
4829 else if (mmap_alloc (var, nbytes))
4831 memcpy (*var, old_ptr, r->nbytes_specified);
4832 mmap_free_1 (MMAP_REGION (old_ptr));
4833 result = *var;
4834 r = MMAP_REGION (result);
4835 r->nbytes_specified = nbytes;
4837 else
4839 *var = old_ptr;
4840 result = NULL;
4843 else if (room - nbytes >= mmap_page_size)
4845 /* Shrinking by at least a page. Let's give some
4846 memory back to the system.
4848 The extra parens are to make the division happens first,
4849 on positive values, so we know it will round towards
4850 zero. */
4851 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4852 result = *var;
4853 r->nbytes_specified = nbytes;
4855 else
4857 /* Leave it alone. */
4858 result = *var;
4859 r->nbytes_specified = nbytes;
4863 return result;
4867 /* Free a block of relocatable storage whose data is pointed to by
4868 PTR. Store 0 in *PTR to show there's no block allocated. */
4870 static void
4871 mmap_free (var)
4872 POINTER_TYPE **var;
4874 mmap_init ();
4876 if (*var)
4878 mmap_free_1 (MMAP_REGION (*var));
4879 *var = NULL;
4884 /* Perform necessary intializations for the use of mmap. */
4886 static void
4887 mmap_init ()
4889 #if MAP_ANON == 0
4890 /* The value of mmap_fd is initially 0 in temacs, and -1
4891 in a dumped Emacs. */
4892 if (mmap_fd <= 0)
4894 /* No anonymous mmap -- we need the file descriptor. */
4895 mmap_fd = open ("/dev/zero", O_RDONLY);
4896 if (mmap_fd == -1)
4897 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4899 #endif /* MAP_ANON == 0 */
4901 if (mmap_initialized_p)
4902 return;
4903 mmap_initialized_p = 1;
4905 #if MAP_ANON != 0
4906 mmap_fd = -1;
4907 #endif
4909 mmap_page_size = getpagesize ();
4912 #endif /* USE_MMAP_FOR_BUFFERS */
4916 /***********************************************************************
4917 Buffer-text Allocation
4918 ***********************************************************************/
4920 #ifdef REL_ALLOC
4921 extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
4922 extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
4923 extern void r_alloc_free (POINTER_TYPE **ptr);
4924 #endif /* REL_ALLOC */
4927 /* Allocate NBYTES bytes for buffer B's text buffer. */
4929 static void
4930 alloc_buffer_text (struct buffer *b, size_t nbytes)
4932 POINTER_TYPE *p;
4934 BLOCK_INPUT;
4935 #if defined USE_MMAP_FOR_BUFFERS
4936 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4937 #elif defined REL_ALLOC
4938 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4939 #else
4940 p = xmalloc (nbytes);
4941 #endif
4943 if (p == NULL)
4945 UNBLOCK_INPUT;
4946 memory_full ();
4949 b->text->beg = (unsigned char *) p;
4950 UNBLOCK_INPUT;
4953 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4954 shrink it. */
4956 void
4957 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
4959 POINTER_TYPE *p;
4960 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4961 + delta);
4962 BLOCK_INPUT;
4963 #if defined USE_MMAP_FOR_BUFFERS
4964 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4965 #elif defined REL_ALLOC
4966 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4967 #else
4968 p = xrealloc (b->text->beg, nbytes);
4969 #endif
4971 if (p == NULL)
4973 UNBLOCK_INPUT;
4974 memory_full ();
4977 BUF_BEG_ADDR (b) = (unsigned char *) p;
4978 UNBLOCK_INPUT;
4982 /* Free buffer B's text buffer. */
4984 static void
4985 free_buffer_text (struct buffer *b)
4987 BLOCK_INPUT;
4989 #if defined USE_MMAP_FOR_BUFFERS
4990 mmap_free ((POINTER_TYPE **) &b->text->beg);
4991 #elif defined REL_ALLOC
4992 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4993 #else
4994 xfree (b->text->beg);
4995 #endif
4997 BUF_BEG_ADDR (b) = NULL;
4998 UNBLOCK_INPUT;
5003 /***********************************************************************
5004 Initialization
5005 ***********************************************************************/
5007 void
5008 init_buffer_once (void)
5010 int idx;
5012 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
5014 /* Make sure all markable slots in buffer_defaults
5015 are initialized reasonably, so mark_buffer won't choke. */
5016 reset_buffer (&buffer_defaults);
5017 eassert (EQ (buffer_defaults.name, make_number (0)));
5018 reset_buffer_local_variables (&buffer_defaults, 1);
5019 eassert (EQ (buffer_local_symbols.name, make_number (0)));
5020 reset_buffer (&buffer_local_symbols);
5021 reset_buffer_local_variables (&buffer_local_symbols, 1);
5022 /* Prevent GC from getting confused. */
5023 buffer_defaults.text = &buffer_defaults.own_text;
5024 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5025 BUF_INTERVALS (&buffer_defaults) = 0;
5026 BUF_INTERVALS (&buffer_local_symbols) = 0;
5027 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5028 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5029 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5030 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5032 /* Set up the default values of various buffer slots. */
5033 /* Must do these before making the first buffer! */
5035 /* real setup is done in bindings.el */
5036 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5037 buffer_defaults.header_line_format = Qnil;
5038 buffer_defaults.abbrev_mode = Qnil;
5039 buffer_defaults.overwrite_mode = Qnil;
5040 buffer_defaults.case_fold_search = Qt;
5041 buffer_defaults.auto_fill_function = Qnil;
5042 buffer_defaults.selective_display = Qnil;
5043 #ifndef old
5044 buffer_defaults.selective_display_ellipses = Qt;
5045 #endif
5046 buffer_defaults.abbrev_table = Qnil;
5047 buffer_defaults.display_table = Qnil;
5048 buffer_defaults.undo_list = Qnil;
5049 buffer_defaults.mark_active = Qnil;
5050 buffer_defaults.file_format = Qnil;
5051 buffer_defaults.auto_save_file_format = Qt;
5052 buffer_defaults.overlays_before = NULL;
5053 buffer_defaults.overlays_after = NULL;
5054 buffer_defaults.overlay_center = BEG;
5056 XSETFASTINT (buffer_defaults.tab_width, 8);
5057 buffer_defaults.truncate_lines = Qnil;
5058 buffer_defaults.word_wrap = Qnil;
5059 buffer_defaults.ctl_arrow = Qt;
5060 buffer_defaults.bidi_display_reordering = Qnil;
5061 buffer_defaults.bidi_paragraph_direction = Qnil;
5062 buffer_defaults.cursor_type = Qt;
5063 buffer_defaults.extra_line_spacing = Qnil;
5064 buffer_defaults.cursor_in_non_selected_windows = Qt;
5066 #ifdef DOS_NT
5067 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5068 #endif
5069 buffer_defaults.enable_multibyte_characters = Qt;
5070 buffer_defaults.buffer_file_coding_system = Qnil;
5071 XSETFASTINT (buffer_defaults.fill_column, 70);
5072 XSETFASTINT (buffer_defaults.left_margin, 0);
5073 buffer_defaults.cache_long_line_scans = Qnil;
5074 buffer_defaults.file_truename = Qnil;
5075 XSETFASTINT (buffer_defaults.display_count, 0);
5076 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5077 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5078 buffer_defaults.left_fringe_width = Qnil;
5079 buffer_defaults.right_fringe_width = Qnil;
5080 buffer_defaults.fringes_outside_margins = Qnil;
5081 buffer_defaults.scroll_bar_width = Qnil;
5082 buffer_defaults.vertical_scroll_bar_type = Qt;
5083 buffer_defaults.indicate_empty_lines = Qnil;
5084 buffer_defaults.indicate_buffer_boundaries = Qnil;
5085 buffer_defaults.fringe_indicator_alist = Qnil;
5086 buffer_defaults.fringe_cursor_alist = Qnil;
5087 buffer_defaults.scroll_up_aggressively = Qnil;
5088 buffer_defaults.scroll_down_aggressively = Qnil;
5089 buffer_defaults.display_time = Qnil;
5091 /* Assign the local-flags to the slots that have default values.
5092 The local flag is a bit that is used in the buffer
5093 to say that it has its own local value for the slot.
5094 The local flag bits are in the local_var_flags slot of the buffer. */
5096 /* Nothing can work if this isn't true */
5097 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5099 /* 0 means not a lisp var, -1 means always local, else mask */
5100 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
5101 XSETINT (buffer_local_flags.filename, -1);
5102 XSETINT (buffer_local_flags.directory, -1);
5103 XSETINT (buffer_local_flags.backed_up, -1);
5104 XSETINT (buffer_local_flags.save_length, -1);
5105 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5106 XSETINT (buffer_local_flags.read_only, -1);
5107 XSETINT (buffer_local_flags.major_mode, -1);
5108 XSETINT (buffer_local_flags.mode_name, -1);
5109 XSETINT (buffer_local_flags.undo_list, -1);
5110 XSETINT (buffer_local_flags.mark_active, -1);
5111 XSETINT (buffer_local_flags.point_before_scroll, -1);
5112 XSETINT (buffer_local_flags.file_truename, -1);
5113 XSETINT (buffer_local_flags.invisibility_spec, -1);
5114 XSETINT (buffer_local_flags.file_format, -1);
5115 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5116 XSETINT (buffer_local_flags.display_count, -1);
5117 XSETINT (buffer_local_flags.display_time, -1);
5118 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5120 idx = 1;
5121 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5122 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5123 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5124 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5125 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5126 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5127 #ifndef old
5128 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5129 #endif
5130 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5131 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5132 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5133 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5134 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5135 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5136 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5137 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5138 #ifdef DOS_NT
5139 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5140 /* Make this one a permanent local. */
5141 buffer_permanent_local_flags[idx++] = 1;
5142 #endif
5143 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.bidi_display_reordering, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.bidi_paragraph_direction, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5149 /* Make this one a permanent local. */
5150 buffer_permanent_local_flags[idx++] = 1;
5151 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5152 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5154 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5157 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5158 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5159 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5160 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5161 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5162 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5163 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5164 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5165 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5166 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5167 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5169 /* Need more room? */
5170 if (idx >= MAX_PER_BUFFER_VARS)
5171 abort ();
5172 last_per_buffer_idx = idx;
5174 Vbuffer_alist = Qnil;
5175 current_buffer = 0;
5176 all_buffers = 0;
5178 QSFundamental = make_pure_c_string ("Fundamental");
5180 Qfundamental_mode = intern_c_string ("fundamental-mode");
5181 buffer_defaults.major_mode = Qfundamental_mode;
5183 Qmode_class = intern_c_string ("mode-class");
5185 Qprotected_field = intern_c_string ("protected-field");
5187 Qpermanent_local = intern_c_string ("permanent-local");
5189 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5190 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5192 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5194 /* super-magic invisible buffer */
5195 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5196 Vbuffer_alist = Qnil;
5198 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5200 inhibit_modification_hooks = 0;
5203 void
5204 init_buffer (void)
5206 char *pwd;
5207 Lisp_Object temp;
5208 int len;
5210 #ifdef USE_MMAP_FOR_BUFFERS
5212 /* When using the ralloc implementation based on mmap(2), buffer
5213 text pointers will have been set to null in the dumped Emacs.
5214 Map new memory. */
5215 struct buffer *b;
5217 for (b = all_buffers; b; b = b->next)
5218 if (b->text->beg == NULL)
5219 enlarge_buffer_text (b, 0);
5221 #endif /* USE_MMAP_FOR_BUFFERS */
5223 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5224 if (NILP (buffer_defaults.enable_multibyte_characters))
5225 Fset_buffer_multibyte (Qnil);
5227 pwd = get_current_dir_name ();
5229 if (!pwd)
5230 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5232 /* Maybe this should really use some standard subroutine
5233 whose definition is filename syntax dependent. */
5234 len = strlen (pwd);
5235 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5237 /* Grow buffer to add directory separator and '\0'. */
5238 pwd = (char *) realloc (pwd, len + 2);
5239 if (!pwd)
5240 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5241 pwd[len] = DIRECTORY_SEP;
5242 pwd[len + 1] = '\0';
5245 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5246 if (! NILP (buffer_defaults.enable_multibyte_characters))
5247 /* At this moment, we still don't know how to decode the
5248 directory name. So, we keep the bytes in multibyte form so
5249 that ENCODE_FILE correctly gets the original bytes. */
5250 current_buffer->directory
5251 = string_to_multibyte (current_buffer->directory);
5253 /* Add /: to the front of the name
5254 if it would otherwise be treated as magic. */
5255 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5256 if (! NILP (temp)
5257 /* If the default dir is just /, TEMP is non-nil
5258 because of the ange-ftp completion handler.
5259 However, it is not necessary to turn / into /:/.
5260 So avoid doing that. */
5261 && strcmp ("/", SDATA (current_buffer->directory)))
5262 current_buffer->directory
5263 = concat2 (build_string ("/:"), current_buffer->directory);
5265 temp = get_minibuffer (0);
5266 XBUFFER (temp)->directory = current_buffer->directory;
5268 free (pwd);
5271 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5272 Object stored in the current buffer. address is the address of the slot
5273 in the buffer that is current now. */
5275 /* TYPE is nil for a general Lisp variable.
5276 An integer specifies a type; then only Lisp values
5277 with that type code are allowed (except that nil is allowed too).
5278 LNAME is the Lisp-level variable name.
5279 VNAME is the name of the buffer slot.
5280 DOC is a dummy where you write the doc string as a comment. */
5281 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5282 do { \
5283 static struct Lisp_Buffer_Objfwd bo_fwd; \
5284 defvar_per_buffer (&bo_fwd, lname, vname, type, 0); \
5285 } while (0)
5287 static void
5288 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, char *namestring,
5289 Lisp_Object *address, Lisp_Object type, char *doc)
5291 struct Lisp_Symbol *sym;
5292 int offset;
5294 sym = XSYMBOL (intern (namestring));
5295 offset = (char *)address - (char *)current_buffer;
5297 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5298 bo_fwd->offset = offset;
5299 bo_fwd->slottype = type;
5300 sym->redirect = SYMBOL_FORWARDED;
5302 /* I tried to do the job without a cast, but it seems impossible.
5303 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5304 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5306 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
5308 if (PER_BUFFER_IDX (offset) == 0)
5309 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5310 slot of buffer_local_flags */
5311 abort ();
5315 /* initialize the buffer routines */
5316 void
5317 syms_of_buffer (void)
5319 staticpro (&last_overlay_modification_hooks);
5320 last_overlay_modification_hooks
5321 = Fmake_vector (make_number (10), Qnil);
5323 staticpro (&Vbuffer_defaults);
5324 staticpro (&Vbuffer_local_symbols);
5325 staticpro (&Qfundamental_mode);
5326 staticpro (&Qmode_class);
5327 staticpro (&QSFundamental);
5328 staticpro (&Vbuffer_alist);
5329 staticpro (&Qprotected_field);
5330 staticpro (&Qpermanent_local);
5331 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5332 staticpro (&Qpermanent_local_hook);
5333 staticpro (&Qkill_buffer_hook);
5334 Qoverlayp = intern_c_string ("overlayp");
5335 staticpro (&Qoverlayp);
5336 Qevaporate = intern_c_string ("evaporate");
5337 staticpro (&Qevaporate);
5338 Qmodification_hooks = intern_c_string ("modification-hooks");
5339 staticpro (&Qmodification_hooks);
5340 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5341 staticpro (&Qinsert_in_front_hooks);
5342 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5343 staticpro (&Qinsert_behind_hooks);
5344 Qget_file_buffer = intern_c_string ("get-file-buffer");
5345 staticpro (&Qget_file_buffer);
5346 Qpriority = intern_c_string ("priority");
5347 staticpro (&Qpriority);
5348 Qwindow = intern_c_string ("window");
5349 staticpro (&Qwindow);
5350 Qbefore_string = intern_c_string ("before-string");
5351 staticpro (&Qbefore_string);
5352 Qafter_string = intern_c_string ("after-string");
5353 staticpro (&Qafter_string);
5354 Qfirst_change_hook = intern_c_string ("first-change-hook");
5355 staticpro (&Qfirst_change_hook);
5356 Qbefore_change_functions = intern_c_string ("before-change-functions");
5357 staticpro (&Qbefore_change_functions);
5358 Qafter_change_functions = intern_c_string ("after-change-functions");
5359 staticpro (&Qafter_change_functions);
5360 /* The next one is initialized in init_buffer_once. */
5361 staticpro (&Qucs_set_table_for_input);
5363 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5364 staticpro (&Qkill_buffer_query_functions);
5366 Fput (Qprotected_field, Qerror_conditions,
5367 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5368 Fput (Qprotected_field, Qerror_message,
5369 make_pure_c_string ("Attempt to modify a protected field"));
5371 /* All these use DEFVAR_LISP_NOPRO because the slots in
5372 buffer_defaults will all be marked via Vbuffer_defaults. */
5374 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5375 &buffer_defaults.mode_line_format,
5376 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5377 This is the same as (default-value 'mode-line-format). */);
5379 DEFVAR_LISP_NOPRO ("default-header-line-format",
5380 &buffer_defaults.header_line_format,
5381 doc: /* Default value of `header-line-format' for buffers that don't override it.
5382 This is the same as (default-value 'header-line-format). */);
5384 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5385 doc: /* Default value of `cursor-type' for buffers that don't override it.
5386 This is the same as (default-value 'cursor-type). */);
5388 DEFVAR_LISP_NOPRO ("default-line-spacing",
5389 &buffer_defaults.extra_line_spacing,
5390 doc: /* Default value of `line-spacing' for buffers that don't override it.
5391 This is the same as (default-value 'line-spacing). */);
5393 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5394 &buffer_defaults.cursor_in_non_selected_windows,
5395 doc: /* Default value of `cursor-in-non-selected-windows'.
5396 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5398 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5399 &buffer_defaults.abbrev_mode,
5400 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5401 This is the same as (default-value 'abbrev-mode). */);
5403 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5404 &buffer_defaults.ctl_arrow,
5405 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5406 This is the same as (default-value 'ctl-arrow). */);
5408 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5409 &buffer_defaults.enable_multibyte_characters,
5410 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5411 This is the same as (default-value 'enable-multibyte-characters). */);
5413 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5414 &buffer_defaults.buffer_file_coding_system,
5415 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5416 This is the same as (default-value 'buffer-file-coding-system). */);
5418 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5419 &buffer_defaults.truncate_lines,
5420 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5421 This is the same as (default-value 'truncate-lines). */);
5423 DEFVAR_LISP_NOPRO ("default-fill-column",
5424 &buffer_defaults.fill_column,
5425 doc: /* Default value of `fill-column' for buffers that do not override it.
5426 This is the same as (default-value 'fill-column). */);
5428 DEFVAR_LISP_NOPRO ("default-left-margin",
5429 &buffer_defaults.left_margin,
5430 doc: /* Default value of `left-margin' for buffers that do not override it.
5431 This is the same as (default-value 'left-margin). */);
5433 DEFVAR_LISP_NOPRO ("default-tab-width",
5434 &buffer_defaults.tab_width,
5435 doc: /* Default value of `tab-width' for buffers that do not override it.
5436 This is the same as (default-value 'tab-width). */);
5438 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5439 &buffer_defaults.case_fold_search,
5440 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5441 This is the same as (default-value 'case-fold-search). */);
5443 #ifdef DOS_NT
5444 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5445 &buffer_defaults.buffer_file_type,
5446 doc: /* Default file type for buffers that do not override it.
5447 This is the same as (default-value 'buffer-file-type).
5448 The file type is nil for text, t for binary. */);
5449 #endif
5451 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5452 &buffer_defaults.left_margin_cols,
5453 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5454 This is the same as (default-value 'left-margin-width). */);
5456 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5457 &buffer_defaults.right_margin_cols,
5458 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5459 This is the same as (default-value 'right-margin-width). */);
5461 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5462 &buffer_defaults.left_fringe_width,
5463 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5464 This is the same as (default-value 'left-fringe-width). */);
5466 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5467 &buffer_defaults.right_fringe_width,
5468 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5469 This is the same as (default-value 'right-fringe-width). */);
5471 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5472 &buffer_defaults.fringes_outside_margins,
5473 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5474 This is the same as (default-value 'fringes-outside-margins). */);
5476 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5477 &buffer_defaults.scroll_bar_width,
5478 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5479 This is the same as (default-value 'scroll-bar-width). */);
5481 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5482 &buffer_defaults.vertical_scroll_bar_type,
5483 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5484 This is the same as (default-value 'vertical-scroll-bar). */);
5486 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5487 &buffer_defaults.indicate_empty_lines,
5488 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5489 This is the same as (default-value 'indicate-empty-lines). */);
5491 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5492 &buffer_defaults.indicate_buffer_boundaries,
5493 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5494 This is the same as (default-value 'indicate-buffer-boundaries). */);
5496 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5497 &buffer_defaults.fringe_indicator_alist,
5498 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5499 This is the same as (default-value 'fringe-indicator-alist'). */);
5501 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5502 &buffer_defaults.fringe_cursor_alist,
5503 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5504 This is the same as (default-value 'fringe-cursor-alist'). */);
5506 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5507 &buffer_defaults.scroll_up_aggressively,
5508 doc: /* Default value of `scroll-up-aggressively'.
5509 This value applies in buffers that don't have their own local values.
5510 This is the same as (default-value 'scroll-up-aggressively). */);
5512 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5513 &buffer_defaults.scroll_down_aggressively,
5514 doc: /* Default value of `scroll-down-aggressively'.
5515 This value applies in buffers that don't have their own local values.
5516 This is the same as (default-value 'scroll-down-aggressively). */);
5518 DEFVAR_PER_BUFFER ("header-line-format",
5519 &current_buffer->header_line_format,
5520 Qnil,
5521 doc: /* Analogous to `mode-line-format', but controls the header line.
5522 The header line appears, optionally, at the top of a window;
5523 the mode line appears at the bottom. */);
5525 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5526 Qnil,
5527 doc: /* Template for displaying mode line for current buffer.
5528 Each buffer has its own value of this variable.
5529 Value may be nil, a string, a symbol or a list or cons cell.
5530 A value of nil means don't display a mode line.
5531 For a symbol, its value is used (but it is ignored if t or nil).
5532 A string appearing directly as the value of a symbol is processed verbatim
5533 in that the %-constructs below are not recognized.
5534 Note that unless the symbol is marked as a `risky-local-variable', all
5535 properties in any strings, as well as all :eval and :propertize forms
5536 in the value of that symbol will be ignored.
5537 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5538 is used as a mode line element. Be careful--FORM should not load any files,
5539 because that can cause an infinite recursion.
5540 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5541 with the specified properties PROPS applied.
5542 For a list whose car is a symbol, the symbol's value is taken,
5543 and if that is non-nil, the cadr of the list is processed recursively.
5544 Otherwise, the caddr of the list (if there is one) is processed.
5545 For a list whose car is a string or list, each element is processed
5546 recursively and the results are effectively concatenated.
5547 For a list whose car is an integer, the cdr of the list is processed
5548 and padded (if the number is positive) or truncated (if negative)
5549 to the width specified by that number.
5550 A string is printed verbatim in the mode line except for %-constructs:
5551 (%-constructs are allowed when the string is the entire mode-line-format
5552 or when it is found in a cons-cell or a list)
5553 %b -- print buffer name. %f -- print visited file name.
5554 %F -- print frame name.
5555 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5556 %& is like %*, but ignore read-only-ness.
5557 % means buffer is read-only and * means it is modified.
5558 For a modified read-only buffer, %* gives % and %+ gives *.
5559 %s -- print process status. %l -- print the current line number.
5560 %c -- print the current column number (this makes editing slower).
5561 To make the column number update correctly in all cases,
5562 `column-number-mode' must be non-nil.
5563 %i -- print the size of the buffer.
5564 %I -- like %i, but use k, M, G, etc., to abbreviate.
5565 %p -- print percent of buffer above top of window, or Top, Bot or All.
5566 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5567 or print Bottom or All.
5568 %n -- print Narrow if appropriate.
5569 %t -- visited file is text or binary (if OS supports this distinction).
5570 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5571 %Z -- like %z, but including the end-of-line format.
5572 %e -- print error message about full memory.
5573 %@ -- print @ or hyphen. @ means that default-directory is on a
5574 remote machine.
5575 %[ -- print one [ for each recursive editing level. %] similar.
5576 %% -- print %. %- -- print infinitely many dashes.
5577 Decimal digits after the % specify field width to which to pad. */);
5579 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5580 doc: /* *Value of `major-mode' for new buffers. */);
5582 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5583 make_number (Lisp_Symbol),
5584 doc: /* Symbol for current buffer's major mode.
5585 The default value (normally `fundamental-mode') affects new buffers.
5586 A value of nil means to use the current buffer's major mode, provided
5587 it is not marked as "special".
5589 When a mode is used by default, `find-file' switches to it before it
5590 reads the contents into the buffer and before it finishes setting up
5591 the buffer. Thus, the mode and its hooks should not expect certain
5592 variables such as `buffer-read-only' and `buffer-file-coding-system'
5593 to be set up. */);
5595 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5596 Qnil,
5597 doc: /* Pretty name of current buffer's major mode.
5598 Usually a string, but can use any of the constructs for `mode-line-format',
5599 which see.
5600 Format with `format-mode-line' to produce a string value. */);
5602 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5603 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5605 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5606 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5608 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5609 Qnil,
5610 doc: /* *Non-nil if searches and matches should ignore case. */);
5612 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5613 make_number (LISP_INT_TAG),
5614 doc: /* *Column beyond which automatic line-wrapping should happen.
5615 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5617 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5618 make_number (LISP_INT_TAG),
5619 doc: /* *Column for the default `indent-line-function' to indent to.
5620 Linefeed indents to this column in Fundamental mode. */);
5622 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5623 make_number (LISP_INT_TAG),
5624 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5626 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5627 doc: /* *Non-nil means display control chars with uparrow.
5628 A value of nil means use backslash and octal digits.
5629 This variable does not apply to characters whose display is specified
5630 in the current display table (if there is one). */);
5632 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5633 &current_buffer->enable_multibyte_characters,
5634 Qnil,
5635 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5636 Otherwise they are regarded as unibyte. This affects the display,
5637 file I/O and the behavior of various editing commands.
5639 This variable is buffer-local but you cannot set it directly;
5640 use the function `set-buffer-multibyte' to change a buffer's representation.
5641 Changing its default value with `setq-default' is supported.
5642 See also variable `default-enable-multibyte-characters' and Info node
5643 `(elisp)Text Representations'. */);
5644 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5646 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5647 &current_buffer->buffer_file_coding_system, Qnil,
5648 doc: /* Coding system to be used for encoding the buffer contents on saving.
5649 This variable applies to saving the buffer, and also to `write-region'
5650 and other functions that use `write-region'.
5651 It does not apply to sending output to subprocesses, however.
5653 If this is nil, the buffer is saved without any code conversion
5654 unless some coding system is specified in `file-coding-system-alist'
5655 for the buffer file.
5657 If the text to be saved cannot be encoded as specified by this variable,
5658 an alternative encoding is selected by `select-safe-coding-system', which see.
5660 The variable `coding-system-for-write', if non-nil, overrides this variable.
5662 This variable is never applied to a way of decoding a file while reading it. */);
5664 DEFVAR_PER_BUFFER ("bidi-display-reordering",
5665 &current_buffer->bidi_display_reordering, Qnil,
5666 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
5668 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
5669 &current_buffer->bidi_paragraph_direction, Qnil,
5670 doc: /* *If non-nil, forces directionality of text paragraphs in the buffer.
5672 If this is nil (the default), the direction of each paragraph is
5673 determined by the first strong directional character of its text.
5674 The values of `right-to-left' and `left-to-right' override that.
5675 Any other value is treated as nil.
5677 This variable has no effect unless the buffer's value of
5678 \`bidi-display-reordering' is non-nil. */);
5680 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5681 doc: /* *Non-nil means do not display continuation lines.
5682 Instead, give each line of text just one screen line.
5684 Note that this is overridden by the variable
5685 `truncate-partial-width-windows' if that variable is non-nil
5686 and this buffer is not full-frame width. */);
5688 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5689 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5690 When word-wrapping is on, continuation lines are wrapped at the space
5691 or tab character nearest to the right window edge.
5692 If nil, continuation lines are wrapped at the right screen edge.
5694 This variable has no effect if long lines are truncated (see
5695 `truncate-lines' and `truncate-partial-width-windows'). If you use
5696 word-wrapping, you might want to reduce the value of
5697 `truncate-partial-width-windows', since wrapping can make text readable
5698 in narrower windows. */);
5700 #ifdef DOS_NT
5701 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5702 Qnil,
5703 doc: /* Non-nil if the visited file is a binary file.
5704 This variable is meaningful on MS-DOG and Windows NT.
5705 On those systems, it is automatically local in every buffer.
5706 On other systems, this variable is normally always nil. */);
5707 #endif
5709 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5710 make_number (Lisp_String),
5711 doc: /* Name of default directory of current buffer. Should end with slash.
5712 To interactively change the default directory, use command `cd'. */);
5714 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5715 Qnil,
5716 doc: /* Function called (if non-nil) to perform auto-fill.
5717 It is called after self-inserting any character specified in
5718 the `auto-fill-chars' table.
5719 NOTE: This variable is not a hook;
5720 its value may not be a list of functions. */);
5722 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5723 make_number (Lisp_String),
5724 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5726 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5727 make_number (Lisp_String),
5728 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5729 The truename of a file is calculated by `file-truename'
5730 and then abbreviated with `abbreviate-file-name'. */);
5732 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5733 &current_buffer->auto_save_file_name,
5734 make_number (Lisp_String),
5735 doc: /* Name of file for auto-saving current buffer.
5736 If it is nil, that means don't auto-save this buffer. */);
5738 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5739 doc: /* Non-nil if this buffer is read-only. */);
5741 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5742 doc: /* Non-nil if this buffer's file has been backed up.
5743 Backing up is done before the first time the file is saved. */);
5745 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5746 make_number (LISP_INT_TAG),
5747 doc: /* Length of current buffer when last read in, saved or auto-saved.
5748 0 initially.
5749 -1 means auto-saving turned off until next real save.
5751 If you set this to -2, that means don't turn off auto-saving in this buffer
5752 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5753 you probably should set this to -2 in that buffer. */);
5755 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5756 Qnil,
5757 doc: /* Non-nil enables selective display.
5758 An integer N as value means display only lines
5759 that start with less than N columns of space.
5760 A value of t means that the character ^M makes itself and
5761 all the rest of the line invisible; also, when saving the buffer
5762 in a file, save the ^M as a newline. */);
5764 #ifndef old
5765 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5766 &current_buffer->selective_display_ellipses,
5767 Qnil,
5768 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5769 #endif
5771 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5772 doc: /* Non-nil if self-insertion should replace existing text.
5773 The value should be one of `overwrite-mode-textual',
5774 `overwrite-mode-binary', or nil.
5775 If it is `overwrite-mode-textual', self-insertion still
5776 inserts at the end of a line, and inserts when point is before a tab,
5777 until the tab is filled in.
5778 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5780 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5781 Qnil,
5782 doc: /* Display table that controls display of the contents of current buffer.
5784 If this variable is nil, the value of `standard-display-table' is used.
5785 Each window can have its own, overriding display table, see
5786 `set-window-display-table' and `window-display-table'.
5788 The display table is a char-table created with `make-display-table'.
5789 A char-table is an array indexed by character codes. Normal array
5790 primitives `aref' and `aset' can be used to access elements of a char-table.
5792 Each of the char-table elements control how to display the corresponding
5793 text character: the element at index C in the table says how to display
5794 the character whose code is C. Each element should be a vector of
5795 characters or nil. The value nil means display the character in the
5796 default fashion; otherwise, the characters from the vector are delivered
5797 to the screen instead of the original character.
5799 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5800 to display a capital Y instead of each X character.
5802 In addition, a char-table has six extra slots to control the display of:
5804 the end of a truncated screen line (extra-slot 0, a single character);
5805 the end of a continued line (extra-slot 1, a single character);
5806 the escape character used to display character codes in octal
5807 (extra-slot 2, a single character);
5808 the character used as an arrow for control characters (extra-slot 3,
5809 a single character);
5810 the decoration indicating the presence of invisible lines (extra-slot 4,
5811 a vector of characters);
5812 the character used to draw the border between side-by-side windows
5813 (extra-slot 5, a single character).
5815 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5817 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5818 Qnil,
5819 doc: /* *Width of left marginal area for display of a buffer.
5820 A value of nil means no marginal area. */);
5822 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5823 Qnil,
5824 doc: /* *Width of right marginal area for display of a buffer.
5825 A value of nil means no marginal area. */);
5827 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5828 Qnil,
5829 doc: /* *Width of this buffer's left fringe (in pixels).
5830 A value of 0 means no left fringe is shown in this buffer's window.
5831 A value of nil means to use the left fringe width from the window's frame. */);
5833 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5834 Qnil,
5835 doc: /* *Width of this buffer's right fringe (in pixels).
5836 A value of 0 means no right fringe is shown in this buffer's window.
5837 A value of nil means to use the right fringe width from the window's frame. */);
5839 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5840 Qnil,
5841 doc: /* *Non-nil means to display fringes outside display margins.
5842 A value of nil means to display fringes between margins and buffer text. */);
5844 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5845 Qnil,
5846 doc: /* *Width of this buffer's scroll bars in pixels.
5847 A value of nil means to use the scroll bar width from the window's frame. */);
5849 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5850 Qnil,
5851 doc: /* *Position of this buffer's vertical scroll bar.
5852 The value takes effect whenever you tell a window to display this buffer;
5853 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5855 A value of `left' or `right' means put the vertical scroll bar at that side
5856 of the window; a value of nil means don't show any vertical scroll bars.
5857 A value of t (the default) means do whatever the window's frame specifies. */);
5859 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5860 &current_buffer->indicate_empty_lines, Qnil,
5861 doc: /* *Visually indicate empty lines after the buffer end.
5862 If non-nil, a bitmap is displayed in the left fringe of a window on
5863 window-systems. */);
5865 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5866 &current_buffer->indicate_buffer_boundaries, Qnil,
5867 doc: /* *Visually indicate buffer boundaries and scrolling.
5868 If non-nil, the first and last line of the buffer are marked in the fringe
5869 of a window on window-systems with angle bitmaps, or if the window can be
5870 scrolled, the top and bottom line of the window are marked with up and down
5871 arrow bitmaps.
5873 If value is a symbol `left' or `right', both angle and arrow bitmaps
5874 are displayed in the left or right fringe, resp. Any other value
5875 that doesn't look like an alist means display the angle bitmaps in
5876 the left fringe but no arrows.
5878 You can exercise more precise control by using an alist as the
5879 value. Each alist element (INDICATOR . POSITION) specifies
5880 where to show one of the indicators. INDICATOR is one of `top',
5881 `bottom', `up', `down', or t, which specifies the default position,
5882 and POSITION is one of `left', `right', or nil, meaning do not show
5883 this indicator.
5885 For example, ((top . left) (t . right)) places the top angle bitmap in
5886 left fringe, the bottom angle bitmap in right fringe, and both arrow
5887 bitmaps in right fringe. To show just the angle bitmaps in the left
5888 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5890 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5891 &current_buffer->fringe_indicator_alist, Qnil,
5892 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5893 The value is an alist where each element (INDICATOR . BITMAPS)
5894 specifies the fringe bitmaps used to display a specific logical
5895 fringe indicator.
5897 INDICATOR specifies the logical indicator type which is one of the
5898 following symbols: `truncation' , `continuation', `overlay-arrow',
5899 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
5901 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5902 the actual bitmap shown in the left or right fringe for the logical
5903 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5904 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5905 are used only for the `bottom' and `top-bottom' indicators when the
5906 last (only) line has no final newline. BITMAPS may also be a single
5907 symbol which is used in both left and right fringes. */);
5909 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5910 &current_buffer->fringe_cursor_alist, Qnil,
5911 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5912 The value is an alist where each element (CURSOR . BITMAP)
5913 specifies the fringe bitmaps used to display a specific logical
5914 cursor type in the fringe.
5916 CURSOR specifies the logical cursor type which is one of the following
5917 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5918 one is used to show a hollow cursor on narrow lines display lines
5919 where the normal hollow cursor will not fit.
5921 BITMAP is the corresponding fringe bitmap shown for the logical
5922 cursor type. */);
5924 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5925 &current_buffer->scroll_up_aggressively, Qnil,
5926 doc: /* How far to scroll windows upward.
5927 If you move point off the bottom, the window scrolls automatically.
5928 This variable controls how far it scrolls. The value nil, the default,
5929 means scroll to center point. A fraction means scroll to put point
5930 that fraction of the window's height from the bottom of the window.
5931 When the value is 0.0, point goes at the bottom line, which in the
5932 simple case that you moved off with C-f means scrolling just one line.
5933 1.0 means point goes at the top, so that in that simple case, the
5934 window scrolls by a full window height. Meaningful values are
5935 between 0.0 and 1.0, inclusive. */);
5937 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5938 &current_buffer->scroll_down_aggressively, Qnil,
5939 doc: /* How far to scroll windows downward.
5940 If you move point off the top, the window scrolls automatically.
5941 This variable controls how far it scrolls. The value nil, the default,
5942 means scroll to center point. A fraction means scroll to put point
5943 that fraction of the window's height from the top of the window.
5944 When the value is 0.0, point goes at the top line, which in the
5945 simple case that you moved off with C-b means scrolling just one line.
5946 1.0 means point goes at the bottom, so that in that simple case, the
5947 window scrolls by a full window height. Meaningful values are
5948 between 0.0 and 1.0, inclusive. */);
5950 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5951 "Don't ask.");
5954 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5955 doc: /* List of functions to call before each text change.
5956 Two arguments are passed to each function: the positions of
5957 the beginning and end of the range of old text to be changed.
5958 \(For an insertion, the beginning and end are at the same place.)
5959 No information is given about the length of the text after the change.
5961 Buffer changes made while executing the `before-change-functions'
5962 don't call any before-change or after-change functions.
5963 That's because these variables are temporarily set to nil.
5964 As a result, a hook function cannot straightforwardly alter the
5965 value of these variables. See the Emacs Lisp manual for a way of
5966 accomplishing an equivalent result by using other variables.
5968 If an unhandled error happens in running these functions,
5969 the variable's value remains nil. That prevents the error
5970 from happening repeatedly and making Emacs nonfunctional. */);
5971 Vbefore_change_functions = Qnil;
5973 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5974 doc: /* List of functions to call after each text change.
5975 Three arguments are passed to each function: the positions of
5976 the beginning and end of the range of changed text,
5977 and the length in bytes of the pre-change text replaced by that range.
5978 \(For an insertion, the pre-change length is zero;
5979 for a deletion, that length is the number of bytes deleted,
5980 and the post-change beginning and end are at the same place.)
5982 Buffer changes made while executing the `after-change-functions'
5983 don't call any before-change or after-change functions.
5984 That's because these variables are temporarily set to nil.
5985 As a result, a hook function cannot straightforwardly alter the
5986 value of these variables. See the Emacs Lisp manual for a way of
5987 accomplishing an equivalent result by using other variables.
5989 If an unhandled error happens in running these functions,
5990 the variable's value remains nil. That prevents the error
5991 from happening repeatedly and making Emacs nonfunctional. */);
5992 Vafter_change_functions = Qnil;
5994 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5995 doc: /* A list of functions to call before changing a buffer which is unmodified.
5996 The functions are run using the `run-hooks' function. */);
5997 Vfirst_change_hook = Qnil;
5999 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6000 doc: /* List of undo entries in current buffer.
6001 Recent changes come first; older changes follow newer.
6003 An entry (BEG . END) represents an insertion which begins at
6004 position BEG and ends at position END.
6006 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6007 from (abs POSITION). If POSITION is positive, point was at the front
6008 of the text being deleted; if negative, point was at the end.
6010 An entry (t HIGH . LOW) indicates that the buffer previously had
6011 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6012 of the visited file's modification time, as of that time. If the
6013 modification time of the most recent save is different, this entry is
6014 obsolete.
6016 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6017 was modified between BEG and END. PROPERTY is the property name,
6018 and VALUE is the old value.
6020 An entry (apply FUN-NAME . ARGS) means undo the change with
6021 \(apply FUN-NAME ARGS).
6023 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6024 in the active region. BEG and END is the range affected by this entry
6025 and DELTA is the number of bytes added or deleted in that range by
6026 this change.
6028 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6029 was adjusted in position by the offset DISTANCE (an integer).
6031 An entry of the form POSITION indicates that point was at the buffer
6032 location given by the integer. Undoing an entry of this form places
6033 point at POSITION.
6035 Entries with value `nil' mark undo boundaries. The undo command treats
6036 the changes between two undo boundaries as a single step to be undone.
6038 If the value of the variable is t, undo information is not recorded. */);
6040 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6041 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6043 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6044 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6046 Normally, the line-motion functions work by scanning the buffer for
6047 newlines. Columnar operations (like `move-to-column' and
6048 `compute-motion') also work by scanning the buffer, summing character
6049 widths as they go. This works well for ordinary text, but if the
6050 buffer's lines are very long (say, more than 500 characters), these
6051 motion functions will take longer to execute. Emacs may also take
6052 longer to update the display.
6054 If `cache-long-line-scans' is non-nil, these motion functions cache the
6055 results of their scans, and consult the cache to avoid rescanning
6056 regions of the buffer until the text is modified. The caches are most
6057 beneficial when they prevent the most searching---that is, when the
6058 buffer contains long lines and large regions of characters with the
6059 same, fixed screen width.
6061 When `cache-long-line-scans' is non-nil, processing short lines will
6062 become slightly slower (because of the overhead of consulting the
6063 cache), and the caches will use memory roughly proportional to the
6064 number of newlines and characters whose screen width varies.
6066 The caches require no explicit maintenance; their accuracy is
6067 maintained internally by the Emacs primitives. Enabling or disabling
6068 the cache should not affect the behavior of any of the motion
6069 functions; it should only affect their performance. */);
6071 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6072 doc: /* Value of point before the last series of scroll operations, or nil. */);
6074 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6075 doc: /* List of formats to use when saving this buffer.
6076 Formats are defined by `format-alist'. This variable is
6077 set when a file is visited. */);
6079 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6080 &current_buffer->auto_save_file_format, Qnil,
6081 doc: /* *Format in which to write auto-save files.
6082 Should be a list of symbols naming formats that are defined in `format-alist'.
6083 If it is t, which is the default, auto-save files are written in the
6084 same format as a regular save would use. */);
6086 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6087 &current_buffer->invisibility_spec, Qnil,
6088 doc: /* Invisibility spec of this buffer.
6089 The default is t, which means that text is invisible
6090 if it has a non-nil `invisible' property.
6091 If the value is a list, a text character is invisible if its `invisible'
6092 property is an element in that list (or is a list with members in common).
6093 If an element is a cons cell of the form (PROP . ELLIPSIS),
6094 then characters with property value PROP are invisible,
6095 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6097 DEFVAR_PER_BUFFER ("buffer-display-count",
6098 &current_buffer->display_count, Qnil,
6099 doc: /* A number incremented each time this buffer is displayed in a window.
6100 The function `set-window-buffer' increments it. */);
6102 DEFVAR_PER_BUFFER ("buffer-display-time",
6103 &current_buffer->display_time, Qnil,
6104 doc: /* Time stamp updated each time this buffer is displayed in a window.
6105 The function `set-window-buffer' updates this variable
6106 to the value obtained by calling `current-time'.
6107 If the buffer has never been shown in a window, the value is nil. */);
6109 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6110 doc: /* */);
6111 Vtransient_mark_mode = Qnil;
6112 /* The docstring is in simple.el. If we put it here, it would be
6113 overwritten when transient-mark-mode is defined using
6114 define-minor-mode. */
6116 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6117 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6118 If the value is t, disregard `buffer-read-only' and all `read-only'
6119 text properties. If the value is a list, disregard `buffer-read-only'
6120 and disregard a `read-only' text property if the property value
6121 is a member of the list. */);
6122 Vinhibit_read_only = Qnil;
6124 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6125 doc: /* Cursor to use when this buffer is in the selected window.
6126 Values are interpreted as follows:
6128 t use the cursor specified for the frame
6129 nil don't display a cursor
6130 box display a filled box cursor
6131 hollow display a hollow box cursor
6132 bar display a vertical bar cursor with default width
6133 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6134 hbar display a horizontal bar cursor with default height
6135 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6136 ANYTHING ELSE display a hollow box cursor
6138 When the buffer is displayed in a non-selected window, the
6139 cursor's appearance is instead controlled by the variable
6140 `cursor-in-non-selected-windows'. */);
6142 DEFVAR_PER_BUFFER ("line-spacing",
6143 &current_buffer->extra_line_spacing, Qnil,
6144 doc: /* Additional space to put between lines when displaying a buffer.
6145 The space is measured in pixels, and put below lines on graphic displays,
6146 see `display-graphic-p'.
6147 If value is a floating point number, it specifies the spacing relative
6148 to the default frame line height. A value of nil means add no extra space. */);
6150 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6151 &current_buffer->cursor_in_non_selected_windows, Qnil,
6152 doc: /* *Cursor type to display in non-selected windows.
6153 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6155 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6156 doc: /* List of functions called with no args to query before killing a buffer.
6157 The buffer being killed will be current while the functions are running.
6158 If any of them returns nil, the buffer is not killed. */);
6159 Vkill_buffer_query_functions = Qnil;
6161 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6162 doc: /* Normal hook run before changing the major mode of a buffer.
6163 The function `kill-all-local-variables' runs this before doing anything else. */);
6164 Vchange_major_mode_hook = Qnil;
6165 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6166 staticpro (&Qchange_major_mode_hook);
6168 defsubr (&Sbuffer_live_p);
6169 defsubr (&Sbuffer_list);
6170 defsubr (&Sget_buffer);
6171 defsubr (&Sget_file_buffer);
6172 defsubr (&Sget_buffer_create);
6173 defsubr (&Smake_indirect_buffer);
6174 defsubr (&Sgenerate_new_buffer_name);
6175 defsubr (&Sbuffer_name);
6176 /*defsubr (&Sbuffer_number);*/
6177 defsubr (&Sbuffer_file_name);
6178 defsubr (&Sbuffer_base_buffer);
6179 defsubr (&Sbuffer_local_value);
6180 defsubr (&Sbuffer_local_variables);
6181 defsubr (&Sbuffer_modified_p);
6182 defsubr (&Sset_buffer_modified_p);
6183 defsubr (&Sbuffer_modified_tick);
6184 defsubr (&Sbuffer_chars_modified_tick);
6185 defsubr (&Srename_buffer);
6186 defsubr (&Sother_buffer);
6187 defsubr (&Sbuffer_enable_undo);
6188 defsubr (&Skill_buffer);
6189 defsubr (&Sset_buffer_major_mode);
6190 defsubr (&Sswitch_to_buffer);
6191 defsubr (&Scurrent_buffer);
6192 defsubr (&Sset_buffer);
6193 defsubr (&Sbarf_if_buffer_read_only);
6194 defsubr (&Sbury_buffer);
6195 defsubr (&Serase_buffer);
6196 defsubr (&Sbuffer_swap_text);
6197 defsubr (&Sset_buffer_multibyte);
6198 defsubr (&Skill_all_local_variables);
6200 defsubr (&Soverlayp);
6201 defsubr (&Smake_overlay);
6202 defsubr (&Sdelete_overlay);
6203 defsubr (&Smove_overlay);
6204 defsubr (&Soverlay_start);
6205 defsubr (&Soverlay_end);
6206 defsubr (&Soverlay_buffer);
6207 defsubr (&Soverlay_properties);
6208 defsubr (&Soverlays_at);
6209 defsubr (&Soverlays_in);
6210 defsubr (&Snext_overlay_change);
6211 defsubr (&Sprevious_overlay_change);
6212 defsubr (&Soverlay_recenter);
6213 defsubr (&Soverlay_lists);
6214 defsubr (&Soverlay_get);
6215 defsubr (&Soverlay_put);
6216 defsubr (&Srestore_buffer_modified_p);
6219 void
6220 keys_of_buffer (void)
6222 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6223 initial_define_key (control_x_map, 'k', "kill-buffer");
6225 /* This must not be in syms_of_buffer, because Qdisabled is not
6226 initialized when that function gets called. */
6227 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6230 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6231 (do not change this comment) */