Initialize a new buffer's owner to nil.
[emacs.git] / src / buffer.c
blobb99025409a26779a607cb55ad3b2206758056895
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 #ifndef USE_CRT_DLL
32 extern int errno;
33 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #include <pthread.h>
42 #include "lisp.h"
43 #include "intervals.h"
44 #include "window.h"
45 #include "commands.h"
46 #include "buffer.h"
47 #include "character.h"
48 #include "region-cache.h"
49 #include "indent.h"
50 #include "blockinput.h"
51 #include "keyboard.h"
52 #include "keymap.h"
53 #include "frame.h"
55 /* First buffer in chain of all buffers (in reverse order of creation).
56 Threaded through ->next. */
58 struct buffer *all_buffers;
60 /* This structure holds the default values of the buffer-local variables
61 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
62 The default value occupies the same slot in this structure
63 as an individual buffer's value occupies in that buffer.
64 Setting the default value also goes through the alist of buffers
65 and stores into each buffer that does not say it has a local value. */
67 DECL_ALIGN (struct buffer, buffer_defaults);
69 /* A Lisp_Object pointer to the above, used for staticpro */
71 static Lisp_Object Vbuffer_defaults;
73 /* This structure marks which slots in a buffer have corresponding
74 default values in buffer_defaults.
75 Each such slot has a nonzero value in this structure.
76 The value has only one nonzero bit.
78 When a buffer has its own local value for a slot,
79 the entry for that slot (found in the same slot in this structure)
80 is turned on in the buffer's local_flags array.
82 If a slot in this structure is -1, then even though there may
83 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
84 and the corresponding slot in buffer_defaults is not used.
86 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
87 but there is a default value which is copied into each buffer.
89 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
90 zero, that is a bug */
92 struct buffer buffer_local_flags;
94 /* This structure holds the names of symbols whose values may be
95 buffer-local. It is indexed and accessed in the same way as the above. */
97 DECL_ALIGN (struct buffer, buffer_local_symbols);
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols;
102 /* Flags indicating which built-in buffer-local variables
103 are permanent locals. */
104 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
106 /* Number of per-buffer variables used. */
108 int last_per_buffer_idx;
110 /* condition var .. w/ global lock */
112 static pthread_cond_t buffer_cond;
114 EXFUN (Fset_buffer, 1);
115 void set_buffer_internal P_ ((struct buffer *b));
116 void set_buffer_internal_1 P_ ((struct buffer *b));
117 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
118 int after, Lisp_Object arg1,
119 Lisp_Object arg2, Lisp_Object arg3));
120 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
121 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
123 /* Alist of all buffer names vs the buffers. */
124 /* This used to be a variable, but is no longer,
125 to prevent lossage due to user rplac'ing this alist or its elements. */
126 Lisp_Object Vbuffer_alist;
128 /* Functions to call before and after each text change. */
129 Lisp_Object impl_Vbefore_change_functions;
130 Lisp_Object impl_Vafter_change_functions;
132 Lisp_Object impl_Vtransient_mark_mode;
134 /* t means ignore all read-only text properties.
135 A list means ignore such a property if its value is a member of the list.
136 Any non-nil value means ignore buffer-read-only. */
137 Lisp_Object impl_Vinhibit_read_only;
139 /* List of functions to call that can query about killing a buffer.
140 If any of these functions returns nil, we don't kill it. */
141 Lisp_Object impl_Vkill_buffer_query_functions;
142 Lisp_Object Qkill_buffer_query_functions;
144 /* Hook run before changing a major mode. */
145 Lisp_Object impl_Vchange_major_mode_hook, Qchange_major_mode_hook;
147 /* List of functions to call before changing an unmodified buffer. */
148 Lisp_Object impl_Vfirst_change_hook;
150 Lisp_Object Qfirst_change_hook;
151 Lisp_Object Qbefore_change_functions;
152 Lisp_Object Qafter_change_functions;
153 Lisp_Object Qucs_set_table_for_input;
155 /* If nonzero, all modification hooks are suppressed. */
156 int inhibit_modification_hooks;
158 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
159 Lisp_Object Qpermanent_local_hook;
161 Lisp_Object Qprotected_field;
163 Lisp_Object QSFundamental; /* A string "Fundamental" */
165 Lisp_Object Qkill_buffer_hook;
167 Lisp_Object Qget_file_buffer;
169 Lisp_Object Qoverlayp;
171 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
173 Lisp_Object Qmodification_hooks;
174 Lisp_Object Qinsert_in_front_hooks;
175 Lisp_Object Qinsert_behind_hooks;
177 static void alloc_buffer_text P_ ((struct buffer *, size_t));
178 static void free_buffer_text P_ ((struct buffer *b));
179 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
180 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
181 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
183 extern char * emacs_strerror P_ ((int));
185 /* For debugging; temporary. See set_buffer_internal. */
186 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
188 void
189 nsberror (spec)
190 Lisp_Object spec;
192 if (STRINGP (spec))
193 error ("No buffer named %s", SDATA (spec));
194 error ("Invalid buffer argument");
197 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
198 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
199 Value is nil if OBJECT is not a buffer or if it has been killed. */)
200 (object)
201 Lisp_Object object;
203 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
204 ? Qt : Qnil);
207 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
208 doc: /* Return a list of all existing live buffers.
209 If the optional arg FRAME is a frame, we return the buffer list
210 in the proper order for that frame: the buffers in FRAME's `buffer-list'
211 frame parameter come first, followed by the rest of the buffers. */)
212 (frame)
213 Lisp_Object frame;
215 Lisp_Object general;
216 general = Fmapcar (Qcdr, Vbuffer_alist);
218 if (FRAMEP (frame))
220 Lisp_Object framelist, prevlist, tail;
221 Lisp_Object args[3];
223 CHECK_FRAME (frame);
225 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
226 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
228 /* Remove from GENERAL any buffer that duplicates one in
229 FRAMELIST or PREVLIST. */
230 tail = framelist;
231 while (CONSP (tail))
233 general = Fdelq (XCAR (tail), general);
234 tail = XCDR (tail);
236 tail = prevlist;
237 while (CONSP (tail))
239 general = Fdelq (XCAR (tail), general);
240 tail = XCDR (tail);
243 args[0] = framelist;
244 args[1] = general;
245 args[2] = prevlist;
246 return Fnconc (3, args);
249 return general;
252 /* Like Fassoc, but use Fstring_equal to compare
253 (which ignores text properties),
254 and don't ever QUIT. */
256 static Lisp_Object
257 assoc_ignore_text_properties (key, list)
258 register Lisp_Object key;
259 Lisp_Object list;
261 register Lisp_Object tail;
262 for (tail = list; CONSP (tail); tail = XCDR (tail))
264 register Lisp_Object elt, tem;
265 elt = XCAR (tail);
266 tem = Fstring_equal (Fcar (elt), key);
267 if (!NILP (tem))
268 return elt;
270 return Qnil;
273 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
274 doc: /* Return the buffer named BUFFER-OR-NAME.
275 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
276 is a string and there is no buffer with that name, return nil. If
277 BUFFER-OR-NAME is a buffer, return it as given. */)
278 (buffer_or_name)
279 register Lisp_Object buffer_or_name;
281 if (BUFFERP (buffer_or_name))
282 return buffer_or_name;
283 CHECK_STRING (buffer_or_name);
285 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
288 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
289 doc: /* Return the buffer visiting file FILENAME (a string).
290 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
291 If there is no such live buffer, return nil.
292 See also `find-buffer-visiting'. */)
293 (filename)
294 register Lisp_Object filename;
296 register Lisp_Object tail, buf, tem;
297 Lisp_Object handler;
299 CHECK_STRING (filename);
300 filename = Fexpand_file_name (filename, Qnil);
302 /* If the file name has special constructs in it,
303 call the corresponding file handler. */
304 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
305 if (!NILP (handler))
306 return call2 (handler, Qget_file_buffer, filename);
308 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
310 buf = Fcdr (XCAR (tail));
311 if (!BUFFERP (buf)) continue;
312 if (!STRINGP (XBUFFER (buf)->filename)) continue;
313 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
314 if (!NILP (tem))
315 return buf;
317 return Qnil;
320 Lisp_Object
321 get_truename_buffer (filename)
322 register Lisp_Object filename;
324 register Lisp_Object tail, buf, tem;
326 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
328 buf = Fcdr (XCAR (tail));
329 if (!BUFFERP (buf)) continue;
330 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
331 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
332 if (!NILP (tem))
333 return buf;
335 return Qnil;
338 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
339 doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
340 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
341 return that buffer. If no such buffer exists, create a new buffer with
342 that name and return it. If BUFFER-OR-NAME starts with a space, the new
343 buffer does not keep undo information.
345 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
346 even if it is dead. The return value is never nil. */)
347 (buffer_or_name)
348 register Lisp_Object buffer_or_name;
350 register Lisp_Object buffer, name;
351 register struct buffer *b;
353 buffer = Fget_buffer (buffer_or_name);
354 if (!NILP (buffer))
355 return buffer;
357 if (SCHARS (buffer_or_name) == 0)
358 error ("Empty string for buffer name is not allowed");
360 b = allocate_buffer ();
362 /* An ordinary buffer uses its own struct buffer_text. */
363 b->text = &b->own_text;
364 b->base_buffer = 0;
366 BUF_GAP_SIZE (b) = 20;
367 BLOCK_INPUT;
368 /* We allocate extra 1-byte at the tail and keep it always '\0' for
369 anchoring a search. */
370 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
371 UNBLOCK_INPUT;
372 if (! BUF_BEG_ADDR (b))
373 buffer_memory_full ();
375 BUF_PT (b) = BEG;
376 BUF_GPT (b) = BEG;
377 BUF_BEGV (b) = BEG;
378 BUF_ZV (b) = BEG;
379 BUF_Z (b) = BEG;
380 BUF_PT_BYTE (b) = BEG_BYTE;
381 BUF_GPT_BYTE (b) = BEG_BYTE;
382 BUF_BEGV_BYTE (b) = BEG_BYTE;
383 BUF_ZV_BYTE (b) = BEG_BYTE;
384 BUF_Z_BYTE (b) = BEG_BYTE;
385 BUF_MODIFF (b) = 1;
386 BUF_CHARS_MODIFF (b) = 1;
387 BUF_OVERLAY_MODIFF (b) = 1;
388 BUF_SAVE_MODIFF (b) = 1;
389 BUF_INTERVALS (b) = 0;
390 BUF_UNCHANGED_MODIFIED (b) = 1;
391 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
392 BUF_END_UNCHANGED (b) = 0;
393 BUF_BEG_UNCHANGED (b) = 0;
394 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
396 b->newline_cache = 0;
397 b->width_run_cache = 0;
398 b->width_table = Qnil;
399 b->prevent_redisplay_optimizations_p = 1;
401 /* Put this on the chain of all buffers including killed ones. */
402 b->next = all_buffers;
403 all_buffers = b;
405 /* An ordinary buffer normally doesn't need markers
406 to handle BEGV and ZV. */
407 b->pt_marker = Qnil;
408 b->begv_marker = Qnil;
409 b->zv_marker = Qnil;
411 name = Fcopy_sequence (buffer_or_name);
412 STRING_SET_INTERVALS (name, NULL_INTERVAL);
413 b->name = name;
415 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
417 reset_buffer (b);
418 reset_buffer_local_variables (b, 1);
420 b->mark = Fmake_marker ();
421 BUF_MARKERS (b) = NULL;
422 b->name = name;
423 b->owner = Qnil;
425 /* Put this in the alist of all live buffers. */
426 XSETBUFFER (buffer, b);
427 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil));
429 /* An error in calling the function here (should someone redefine it)
430 can lead to infinite regress until you run out of stack. rms
431 says that's not worth protecting against. */
432 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
433 /* buffer is on buffer-alist, so no gcpro. */
434 call1 (Qucs_set_table_for_input, buffer);
436 return buffer;
440 /* Return a list of overlays which is a copy of the overlay list
441 LIST, but for buffer B. */
443 static struct Lisp_Overlay *
444 copy_overlays (b, list)
445 struct buffer *b;
446 struct Lisp_Overlay *list;
448 Lisp_Object buffer;
449 struct Lisp_Overlay *result = NULL, *tail = NULL;
451 XSETBUFFER (buffer, b);
453 for (; list; list = list->next)
455 Lisp_Object overlay, start, end, old_overlay;
456 EMACS_INT charpos;
458 XSETMISC (old_overlay, list);
459 charpos = marker_position (OVERLAY_START (old_overlay));
460 start = Fmake_marker ();
461 Fset_marker (start, make_number (charpos), buffer);
462 XMARKER (start)->insertion_type
463 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
465 charpos = marker_position (OVERLAY_END (old_overlay));
466 end = Fmake_marker ();
467 Fset_marker (end, make_number (charpos), buffer);
468 XMARKER (end)->insertion_type
469 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
471 overlay = allocate_misc ();
472 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
473 OVERLAY_START (overlay) = start;
474 OVERLAY_END (overlay) = end;
475 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
476 XOVERLAY (overlay)->next = NULL;
478 if (tail)
479 tail = tail->next = XOVERLAY (overlay);
480 else
481 result = tail = XOVERLAY (overlay);
484 return result;
488 /* Clone per-buffer values of buffer FROM.
490 Buffer TO gets the same per-buffer values as FROM, with the
491 following exceptions: (1) TO's name is left untouched, (2) markers
492 are copied and made to refer to TO, and (3) overlay lists are
493 copied. */
495 static void
496 clone_per_buffer_values (from, to)
497 struct buffer *from, *to;
499 Lisp_Object to_buffer;
500 int offset;
502 XSETBUFFER (to_buffer, to);
504 /* buffer-local Lisp variables start at `undo_list',
505 tho only the ones from `name' on are GC'd normally. */
506 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
507 offset < sizeof *to;
508 offset += sizeof (Lisp_Object))
510 Lisp_Object obj;
512 /* Don't touch the `name' which should be unique for every buffer. */
513 if (offset == PER_BUFFER_VAR_OFFSET (name))
514 continue;
516 obj = PER_BUFFER_VALUE (from, offset);
517 if (MARKERP (obj))
519 struct Lisp_Marker *m = XMARKER (obj);
520 obj = Fmake_marker ();
521 XMARKER (obj)->insertion_type = m->insertion_type;
522 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
525 PER_BUFFER_VALUE (to, offset) = obj;
528 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
530 to->overlays_before = copy_overlays (to, from->overlays_before);
531 to->overlays_after = copy_overlays (to, from->overlays_after);
533 /* Get (a copy of) the alist of Lisp-level local variables of FROM
534 and install that in TO. */
535 to->local_var_alist = buffer_lisp_local_variables (from);
538 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
539 2, 3,
540 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
541 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
542 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
543 NAME should be a string which is not the name of an existing buffer.
544 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
545 such as major and minor modes, in the indirect buffer.
546 CLONE nil means the indirect buffer's state is reset to default values. */)
547 (base_buffer, name, clone)
548 Lisp_Object base_buffer, name, clone;
550 Lisp_Object buf, tem;
551 struct buffer *b;
553 CHECK_STRING (name);
554 buf = Fget_buffer (name);
555 if (!NILP (buf))
556 error ("Buffer name `%s' is in use", SDATA (name));
558 tem = base_buffer;
559 base_buffer = Fget_buffer (base_buffer);
560 if (NILP (base_buffer))
561 error ("No such buffer: `%s'", SDATA (tem));
562 if (NILP (XBUFFER (base_buffer)->name))
563 error ("Base buffer has been killed");
565 if (SCHARS (name) == 0)
566 error ("Empty string for buffer name is not allowed");
568 b = allocate_buffer ();
570 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
571 ? XBUFFER (base_buffer)->base_buffer
572 : XBUFFER (base_buffer));
574 /* Use the base buffer's text object. */
575 b->text = b->base_buffer->text;
577 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
578 BUF_ZV (b) = BUF_ZV (b->base_buffer);
579 BUF_PT (b) = BUF_PT (b->base_buffer);
580 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
581 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
582 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
584 b->newline_cache = 0;
585 b->width_run_cache = 0;
586 b->width_table = Qnil;
588 /* Put this on the chain of all buffers including killed ones. */
589 b->next = all_buffers;
590 all_buffers = b;
592 name = Fcopy_sequence (name);
593 STRING_SET_INTERVALS (name, NULL_INTERVAL);
594 b->name = name;
596 reset_buffer (b);
597 reset_buffer_local_variables (b, 1);
599 /* Put this in the alist of all live buffers. */
600 XSETBUFFER (buf, b);
601 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
603 b->mark = Fmake_marker ();
604 b->name = name;
605 b->owner = Qnil;
607 /* The multibyte status belongs to the base buffer. */
608 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
610 /* Make sure the base buffer has markers for its narrowing. */
611 if (NILP (b->base_buffer->pt_marker))
613 b->base_buffer->pt_marker = Fmake_marker ();
614 set_marker_both (b->base_buffer->pt_marker, base_buffer,
615 BUF_PT (b->base_buffer),
616 BUF_PT_BYTE (b->base_buffer));
618 if (NILP (b->base_buffer->begv_marker))
620 b->base_buffer->begv_marker = Fmake_marker ();
621 set_marker_both (b->base_buffer->begv_marker, base_buffer,
622 BUF_BEGV (b->base_buffer),
623 BUF_BEGV_BYTE (b->base_buffer));
625 if (NILP (b->base_buffer->zv_marker))
627 b->base_buffer->zv_marker = Fmake_marker ();
628 set_marker_both (b->base_buffer->zv_marker, base_buffer,
629 BUF_ZV (b->base_buffer),
630 BUF_ZV_BYTE (b->base_buffer));
631 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
634 if (NILP (clone))
636 /* Give the indirect buffer markers for its narrowing. */
637 b->pt_marker = Fmake_marker ();
638 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
639 b->begv_marker = Fmake_marker ();
640 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
641 b->zv_marker = Fmake_marker ();
642 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
643 XMARKER (b->zv_marker)->insertion_type = 1;
645 else
647 struct buffer *old_b = current_buffer;
649 clone_per_buffer_values (b->base_buffer, b);
650 b->filename = Qnil;
651 b->file_truename = Qnil;
652 b->display_count = make_number (0);
653 b->backed_up = Qnil;
654 b->auto_save_file_name = Qnil;
655 set_buffer_internal_1 (b);
656 Fset (intern ("buffer-save-without-query"), Qnil);
657 Fset (intern ("buffer-file-number"), Qnil);
658 Fset (intern ("buffer-stale-function"), Qnil);
659 set_buffer_internal_1 (old_b);
662 return buf;
665 void
666 delete_all_overlays (b)
667 struct buffer *b;
669 Lisp_Object overlay;
671 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
672 have to empty the list, otherwise we end up with overlays that
673 think they belong to this buffer while the buffer doesn't know about
674 them any more. */
675 while (b->overlays_before)
677 XSETMISC (overlay, b->overlays_before);
678 Fdelete_overlay (overlay);
680 while (b->overlays_after)
682 XSETMISC (overlay, b->overlays_after);
683 Fdelete_overlay (overlay);
685 eassert (b->overlays_before == NULL);
686 eassert (b->overlays_after == NULL);
689 /* Reinitialize everything about a buffer except its name and contents
690 and local variables.
691 If called on an already-initialized buffer, the list of overlays
692 should be deleted before calling this function, otherwise we end up
693 with overlays that claim to belong to the buffer but the buffer
694 claims it doesn't belong to it. */
696 void
697 reset_buffer (b)
698 register struct buffer *b;
700 b->filename = Qnil;
701 b->file_truename = Qnil;
702 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
703 b->modtime = 0;
704 XSETFASTINT (b->save_length, 0);
705 b->last_window_start = 1;
706 /* It is more conservative to start out "changed" than "unchanged". */
707 b->clip_changed = 0;
708 b->prevent_redisplay_optimizations_p = 1;
709 b->backed_up = Qnil;
710 BUF_AUTOSAVE_MODIFF (b) = 0;
711 b->auto_save_failure_time = -1;
712 b->auto_save_file_name = Qnil;
713 b->read_only = Qnil;
714 b->overlays_before = NULL;
715 b->overlays_after = NULL;
716 b->overlay_center = BEG;
717 b->mark_active = Qnil;
718 b->point_before_scroll = Qnil;
719 b->file_format = Qnil;
720 b->auto_save_file_format = Qt;
721 b->last_selected_window = Qnil;
722 XSETINT (b->display_count, 0);
723 b->display_time = Qnil;
724 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
725 b->cursor_type = buffer_defaults.cursor_type;
726 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
728 b->display_error_modiff = 0;
731 /* Reset buffer B's local variables info.
732 Don't use this on a buffer that has already been in use;
733 it does not treat permanent locals consistently.
734 Instead, use Fkill_all_local_variables.
736 If PERMANENT_TOO is 1, then we reset permanent
737 buffer-local variables. If PERMANENT_TOO is 0,
738 we preserve those. */
740 static void
741 reset_buffer_local_variables (b, permanent_too)
742 register struct buffer *b;
743 int permanent_too;
745 register int offset;
746 int i;
748 /* Reset the major mode to Fundamental, together with all the
749 things that depend on the major mode.
750 default-major-mode is handled at a higher level.
751 We ignore it here. */
752 b->major_mode = Qfundamental_mode;
753 b->keymap = Qnil;
754 b->mode_name = QSFundamental;
755 b->minor_modes = Qnil;
757 /* If the standard case table has been altered and invalidated,
758 fix up its insides first. */
759 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
760 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
761 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
762 Fset_standard_case_table (Vascii_downcase_table);
764 b->downcase_table = Vascii_downcase_table;
765 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
766 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
767 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
768 b->invisibility_spec = Qt;
769 #ifndef DOS_NT
770 b->buffer_file_type = Qnil;
771 #endif
773 /* Reset all (or most) per-buffer variables to their defaults. */
774 if (permanent_too)
775 b->local_var_alist = Qnil;
776 else
778 Lisp_Object tmp, prop, last = Qnil;
779 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
780 if (CONSP (XCAR (tmp))
781 && SYMBOLP (XCAR (XCAR (tmp)))
782 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
784 /* If permanent-local, keep it. */
785 last = tmp;
786 if (EQ (prop, Qpermanent_local_hook))
788 /* This is a partially permanent hook variable.
789 Preserve only the elements that want to be preserved. */
790 Lisp_Object list, newlist;
791 list = XCDR (XCAR (tmp));
792 if (!CONSP (list))
793 newlist = list;
794 else
795 for (newlist = Qnil; CONSP (list); list = XCDR (list))
797 Lisp_Object elt = XCAR (list);
798 /* Preserve element ELT if it's t,
799 if it is a function with a `permanent-local-hook' property,
800 or if it's not a symbol. */
801 if (! SYMBOLP (elt)
802 || EQ (elt, Qt)
803 || !NILP (Fget (elt, Qpermanent_local_hook)))
804 newlist = Fcons (elt, newlist);
806 XSETCDR (XCAR (tmp), Fnreverse (newlist));
809 /* Delete this local variable. */
810 else if (NILP (last))
811 b->local_var_alist = XCDR (tmp);
812 else
813 XSETCDR (last, XCDR (tmp));
816 for (i = 0; i < last_per_buffer_idx; ++i)
817 if (permanent_too || buffer_permanent_local_flags[i] == 0)
818 SET_PER_BUFFER_VALUE_P (b, i, 0);
820 /* For each slot that has a default value,
821 copy that into the slot. */
823 /* buffer-local Lisp variables start at `undo_list',
824 tho only the ones from `name' on are GC'd normally. */
825 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
826 offset < sizeof *b;
827 offset += sizeof (Lisp_Object))
829 int idx = PER_BUFFER_IDX (offset);
830 if ((idx > 0
831 && (permanent_too
832 || buffer_permanent_local_flags[idx] == 0))
833 /* Is -2 used anywhere? */
834 || idx == -2)
835 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
839 /* We split this away from generate-new-buffer, because rename-buffer
840 and set-visited-file-name ought to be able to use this to really
841 rename the buffer properly. */
843 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
844 1, 2, 0,
845 doc: /* Return a string that is the name of no existing buffer based on NAME.
846 If there is no live buffer named NAME, then return NAME.
847 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
848 \(starting at 2) until an unused name is found, and then return that name.
849 Optional second argument IGNORE specifies a name that is okay to use (if
850 it is in the sequence to be tried) even if a buffer with that name exists. */)
851 (name, ignore)
852 register Lisp_Object name, ignore;
854 register Lisp_Object gentemp, tem;
855 int count;
856 char number[10];
858 CHECK_STRING (name);
860 tem = Fstring_equal (name, ignore);
861 if (!NILP (tem))
862 return name;
863 tem = Fget_buffer (name);
864 if (NILP (tem))
865 return name;
867 count = 1;
868 while (1)
870 sprintf (number, "<%d>", ++count);
871 gentemp = concat2 (name, build_string (number));
872 tem = Fstring_equal (gentemp, ignore);
873 if (!NILP (tem))
874 return gentemp;
875 tem = Fget_buffer (gentemp);
876 if (NILP (tem))
877 return gentemp;
882 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
883 doc: /* Return the name of BUFFER, as a string.
884 BUFFER defaults to the current buffer.
885 Return nil if BUFFER has been killed. */)
886 (buffer)
887 register Lisp_Object buffer;
889 if (NILP (buffer))
890 return current_buffer->name;
891 CHECK_BUFFER (buffer);
892 return XBUFFER (buffer)->name;
895 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
896 doc: /* Return name of file BUFFER is visiting, or nil if none.
897 No argument or nil as argument means use the current buffer. */)
898 (buffer)
899 register Lisp_Object buffer;
901 if (NILP (buffer))
902 return current_buffer->filename;
903 CHECK_BUFFER (buffer);
904 return XBUFFER (buffer)->filename;
907 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
908 0, 1, 0,
909 doc: /* Return the base buffer of indirect buffer BUFFER.
910 If BUFFER is not indirect, return nil.
911 BUFFER defaults to the current buffer. */)
912 (buffer)
913 register Lisp_Object buffer;
915 struct buffer *base;
916 Lisp_Object base_buffer;
918 if (NILP (buffer))
919 base = current_buffer->base_buffer;
920 else
922 CHECK_BUFFER (buffer);
923 base = XBUFFER (buffer)->base_buffer;
926 if (! base)
927 return Qnil;
928 XSETBUFFER (base_buffer, base);
929 return base_buffer;
932 DEFUN ("buffer-local-value", Fbuffer_local_value,
933 Sbuffer_local_value, 2, 2, 0,
934 doc: /* Return the value of VARIABLE in BUFFER.
935 If VARIABLE does not have a buffer-local binding in BUFFER, the value
936 is the default binding of the variable. */)
937 (variable, buffer)
938 register Lisp_Object variable;
939 register Lisp_Object buffer;
941 register struct buffer *buf;
942 register Lisp_Object result;
943 struct Lisp_Symbol *sym;
945 CHECK_SYMBOL (variable);
946 CHECK_BUFFER (buffer);
947 buf = XBUFFER (buffer);
949 sym = indirect_variable (XSYMBOL (variable));
950 XSETSYMBOL (variable, sym);
952 /* Look in local_var_list */
953 result = Fassoc (variable, buf->local_var_alist);
954 if (NILP (result))
956 int offset, idx;
957 int found = 0;
959 /* Look in special slots */
960 /* buffer-local Lisp variables start at `undo_list',
961 tho only the ones from `name' on are GC'd normally. */
962 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
963 offset < sizeof (struct buffer);
964 /* sizeof EMACS_INT == sizeof Lisp_Object */
965 offset += (sizeof (EMACS_INT)))
967 idx = PER_BUFFER_IDX (offset);
968 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
969 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
970 && EQ (PER_BUFFER_SYMBOL (offset), variable))
972 result = PER_BUFFER_VALUE (buf, offset);
973 found = 1;
974 break;
978 if (!found)
979 result = Fdefault_value (variable);
981 else
983 Lisp_Object valcontents;
984 Lisp_Object current_alist_element;
986 /* What binding is loaded right now? */
987 valcontents = sym->value;
988 current_alist_element
989 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
991 /* The value of the currently loaded binding is not
992 stored in it, but rather in the realvalue slot.
993 Store that value into the binding it belongs to
994 in case that is the one we are about to use. */
996 Fsetcdr (current_alist_element,
997 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
999 /* Now get the (perhaps updated) value out of the binding. */
1000 result = XCDR (result);
1003 if (!EQ (result, Qunbound))
1004 return result;
1006 xsignal1 (Qvoid_variable, variable);
1009 /* Return an alist of the Lisp-level buffer-local bindings of
1010 buffer BUF. That is, don't include the variables maintained
1011 in special slots in the buffer object. */
1013 static Lisp_Object
1014 buffer_lisp_local_variables (buf)
1015 struct buffer *buf;
1017 Lisp_Object result = Qnil;
1018 register Lisp_Object tail;
1019 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1021 Lisp_Object val, elt;
1023 elt = XCAR (tail);
1025 /* Reference each variable in the alist in buf.
1026 If inquiring about the current buffer, this gets the current values,
1027 so store them into the alist so the alist is up to date.
1028 If inquiring about some other buffer, this swaps out any values
1029 for that buffer, making the alist up to date automatically. */
1030 val = find_symbol_value (XCAR (elt));
1031 /* Use the current buffer value only if buf is the current buffer. */
1032 if (buf != current_buffer)
1033 val = XCDR (elt);
1035 /* If symbol is unbound, put just the symbol in the list. */
1036 if (EQ (val, Qunbound))
1037 result = Fcons (XCAR (elt), result);
1038 /* Otherwise, put (symbol . value) in the list. */
1039 else
1040 result = Fcons (Fcons (XCAR (elt), val), result);
1043 return result;
1046 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1047 Sbuffer_local_variables, 0, 1, 0,
1048 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1049 Most elements look like (SYMBOL . VALUE), describing one variable.
1050 For a symbol that is locally unbound, just the symbol appears in the value.
1051 Note that storing new VALUEs in these elements doesn't change the variables.
1052 No argument or nil as argument means use current buffer as BUFFER. */)
1053 (buffer)
1054 register Lisp_Object buffer;
1056 register struct buffer *buf;
1057 register Lisp_Object result;
1059 if (NILP (buffer))
1060 buf = current_buffer;
1061 else
1063 CHECK_BUFFER (buffer);
1064 buf = XBUFFER (buffer);
1067 result = buffer_lisp_local_variables (buf);
1069 /* Add on all the variables stored in special slots. */
1071 int offset, idx;
1073 /* buffer-local Lisp variables start at `undo_list',
1074 tho only the ones from `name' on are GC'd normally. */
1075 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1076 offset < sizeof (struct buffer);
1077 /* sizeof EMACS_INT == sizeof Lisp_Object */
1078 offset += (sizeof (EMACS_INT)))
1080 idx = PER_BUFFER_IDX (offset);
1081 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1082 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1083 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1084 PER_BUFFER_VALUE (buf, offset)),
1085 result);
1089 return result;
1092 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1093 0, 1, 0,
1094 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1095 No argument or nil as argument means use current buffer as BUFFER. */)
1096 (buffer)
1097 register Lisp_Object buffer;
1099 register struct buffer *buf;
1100 if (NILP (buffer))
1101 buf = current_buffer;
1102 else
1104 CHECK_BUFFER (buffer);
1105 buf = XBUFFER (buffer);
1108 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1111 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1112 1, 1, 0,
1113 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1114 A non-nil FLAG means mark the buffer modified. */)
1115 (flag)
1116 register Lisp_Object flag;
1118 register int already;
1119 register Lisp_Object fn;
1120 Lisp_Object buffer, window;
1122 #ifdef CLASH_DETECTION
1123 /* If buffer becoming modified, lock the file.
1124 If buffer becoming unmodified, unlock the file. */
1126 fn = current_buffer->file_truename;
1127 /* Test buffer-file-name so that binding it to nil is effective. */
1128 if (!NILP (fn) && ! NILP (current_buffer->filename))
1130 already = SAVE_MODIFF < MODIFF;
1131 if (!already && !NILP (flag))
1132 lock_file (fn);
1133 else if (already && NILP (flag))
1134 unlock_file (fn);
1136 #endif /* CLASH_DETECTION */
1138 /* Here we have a problem. SAVE_MODIFF is used here to encode
1139 buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
1140 recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we
1141 modify SAVE_MODIFF to affect one, we may affect the other
1142 as well.
1143 E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
1144 if SAVE_MODIFF<auto_save_modified that means we risk changing
1145 recent-auto-save-p from t to nil.
1146 Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
1147 we risk changing recent-auto-save-p from nil to t. */
1148 SAVE_MODIFF = (NILP (flag)
1149 /* FIXME: This unavoidably sets recent-auto-save-p to nil. */
1150 ? MODIFF
1151 /* Let's try to preserve recent-auto-save-p. */
1152 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
1153 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
1154 we can either decrease SAVE_MODIFF and auto_save_modified
1155 or increase MODIFF. */
1156 : MODIFF++);
1158 /* Set update_mode_lines only if buffer is displayed in some window.
1159 Packages like jit-lock or lazy-lock preserve a buffer's modified
1160 state by recording/restoring the state around blocks of code.
1161 Setting update_mode_lines makes redisplay consider all windows
1162 (on all frames). Stealth fontification of buffers not displayed
1163 would incur additional redisplay costs if we'd set
1164 update_modes_lines unconditionally.
1166 Ideally, I think there should be another mechanism for fontifying
1167 buffers without "modifying" buffers, or redisplay should be
1168 smarter about updating the `*' in mode lines. --gerd */
1169 XSETBUFFER (buffer, current_buffer);
1170 window = Fget_buffer_window (buffer, Qt);
1171 if (WINDOWP (window))
1173 ++update_mode_lines;
1174 current_buffer->prevent_redisplay_optimizations_p = 1;
1177 return flag;
1180 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1181 Srestore_buffer_modified_p, 1, 1, 0,
1182 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1183 It is not ensured that mode lines will be updated to show the modified
1184 state of the current buffer. Use with care. */)
1185 (flag)
1186 Lisp_Object flag;
1188 #ifdef CLASH_DETECTION
1189 Lisp_Object fn;
1191 /* If buffer becoming modified, lock the file.
1192 If buffer becoming unmodified, unlock the file. */
1194 fn = current_buffer->file_truename;
1195 /* Test buffer-file-name so that binding it to nil is effective. */
1196 if (!NILP (fn) && ! NILP (current_buffer->filename))
1198 int already = SAVE_MODIFF < MODIFF;
1199 if (!already && !NILP (flag))
1200 lock_file (fn);
1201 else if (already && NILP (flag))
1202 unlock_file (fn);
1204 #endif /* CLASH_DETECTION */
1206 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1207 return flag;
1210 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1211 0, 1, 0,
1212 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1213 Each buffer has a tick counter which is incremented each time the
1214 text in that buffer is changed. It wraps around occasionally.
1215 No argument or nil as argument means use current buffer as BUFFER. */)
1216 (buffer)
1217 register Lisp_Object buffer;
1219 register struct buffer *buf;
1220 if (NILP (buffer))
1221 buf = current_buffer;
1222 else
1224 CHECK_BUFFER (buffer);
1225 buf = XBUFFER (buffer);
1228 return make_number (BUF_MODIFF (buf));
1231 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1232 Sbuffer_chars_modified_tick, 0, 1, 0,
1233 doc: /* Return BUFFER's character-change tick counter.
1234 Each buffer has a character-change tick counter, which is set to the
1235 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1236 time text in that buffer is inserted or deleted. By comparing the
1237 values returned by two individual calls of `buffer-chars-modified-tick',
1238 you can tell whether a character change occurred in that buffer in
1239 between these calls. No argument or nil as argument means use current
1240 buffer as BUFFER. */)
1241 (buffer)
1242 register Lisp_Object buffer;
1244 register struct buffer *buf;
1245 if (NILP (buffer))
1246 buf = current_buffer;
1247 else
1249 CHECK_BUFFER (buffer);
1250 buf = XBUFFER (buffer);
1253 return make_number (BUF_CHARS_MODIFF (buf));
1256 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1257 "(list (read-string \"Rename buffer (to new name): \" \
1258 nil 'buffer-name-history (buffer-name (current-buffer))) \
1259 current-prefix-arg)",
1260 doc: /* Change current buffer's name to NEWNAME (a string).
1261 If second arg UNIQUE is nil or omitted, it is an error if a
1262 buffer named NEWNAME already exists.
1263 If UNIQUE is non-nil, come up with a new name using
1264 `generate-new-buffer-name'.
1265 Interactively, you can set UNIQUE with a prefix argument.
1266 We return the name we actually gave the buffer.
1267 This does not change the name of the visited file (if any). */)
1268 (newname, unique)
1269 register Lisp_Object newname, unique;
1271 register Lisp_Object tem, buf;
1273 CHECK_STRING (newname);
1275 if (SCHARS (newname) == 0)
1276 error ("Empty string is invalid as a buffer name");
1278 tem = Fget_buffer (newname);
1279 if (!NILP (tem))
1281 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1282 rename the buffer automatically so you can create another
1283 with the original name. It makes UNIQUE equivalent to
1284 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1285 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1286 return current_buffer->name;
1287 if (!NILP (unique))
1288 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1289 else
1290 error ("Buffer name `%s' is in use", SDATA (newname));
1293 current_buffer->name = newname;
1295 /* Catch redisplay's attention. Unless we do this, the mode lines for
1296 any windows displaying current_buffer will stay unchanged. */
1297 update_mode_lines++;
1299 XSETBUFFER (buf, current_buffer);
1300 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1301 if (NILP (current_buffer->filename)
1302 && !NILP (current_buffer->auto_save_file_name))
1303 call0 (intern ("rename-auto-save-file"));
1304 /* Refetch since that last call may have done GC. */
1305 return current_buffer->name;
1308 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1309 doc: /* Return most recently selected buffer other than BUFFER.
1310 Buffers not visible in windows are preferred to visible buffers,
1311 unless optional second argument VISIBLE-OK is non-nil.
1312 If the optional third argument FRAME is non-nil, use that frame's
1313 buffer list instead of the selected frame's buffer list.
1314 If no other buffer exists, the buffer `*scratch*' is returned.
1315 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1316 (buffer, visible_ok, frame)
1317 register Lisp_Object buffer, visible_ok, frame;
1319 Lisp_Object Fset_buffer_major_mode ();
1320 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1321 notsogood = Qnil;
1323 if (NILP (frame))
1324 frame = selected_frame;
1326 tail = Vbuffer_alist;
1327 pred = frame_buffer_predicate (frame);
1329 /* Consider buffers that have been seen in the selected frame
1330 before other buffers. */
1332 tem = frame_buffer_list (frame);
1333 add_ons = Qnil;
1334 while (CONSP (tem))
1336 if (BUFFERP (XCAR (tem)))
1337 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1338 tem = XCDR (tem);
1340 tail = nconc2 (Fnreverse (add_ons), tail);
1342 for (; CONSP (tail); tail = XCDR (tail))
1344 buf = Fcdr (XCAR (tail));
1345 if (EQ (buf, buffer))
1346 continue;
1347 if (NILP (buf))
1348 continue;
1349 if (NILP (XBUFFER (buf)->name))
1350 continue;
1351 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1352 continue;
1353 /* If the selected frame has a buffer_predicate,
1354 disregard buffers that don't fit the predicate. */
1355 if (!NILP (pred))
1357 tem = call1 (pred, buf);
1358 if (NILP (tem))
1359 continue;
1362 if (NILP (visible_ok))
1363 tem = Fget_buffer_window (buf, Qvisible);
1364 else
1365 tem = Qnil;
1366 if (NILP (tem))
1367 return buf;
1368 if (NILP (notsogood))
1369 notsogood = buf;
1371 if (!NILP (notsogood))
1372 return notsogood;
1373 buf = Fget_buffer (build_string ("*scratch*"));
1374 if (NILP (buf))
1376 buf = Fget_buffer_create (build_string ("*scratch*"));
1377 Fset_buffer_major_mode (buf);
1379 return buf;
1382 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1383 0, 1, "",
1384 doc: /* Start keeping undo information for buffer BUFFER.
1385 No argument or nil as argument means do this for the current buffer. */)
1386 (buffer)
1387 register Lisp_Object buffer;
1389 Lisp_Object real_buffer;
1391 if (NILP (buffer))
1392 XSETBUFFER (real_buffer, current_buffer);
1393 else
1395 real_buffer = Fget_buffer (buffer);
1396 if (NILP (real_buffer))
1397 nsberror (buffer);
1400 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1401 XBUFFER (real_buffer)->undo_list = Qnil;
1403 return Qnil;
1407 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1408 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1409 The buffer being killed will be current while the hook is running.\n\
1410 See `kill-buffer'."
1412 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1413 doc: /* Kill buffer BUFFER-OR-NAME.
1414 The argument may be a buffer or the name of an existing buffer.
1415 Argument nil or omitted means kill the current buffer. Return t if the
1416 buffer is actually killed, nil otherwise.
1418 This function calls `replace-buffer-in-windows' for cleaning up all
1419 windows currently displaying the buffer to be killed. The functions in
1420 `kill-buffer-query-functions' are called with the buffer to be killed as
1421 the current buffer. If any of them returns nil, the buffer is not
1422 killed. The hook `kill-buffer-hook' is run before the buffer is
1423 actually killed. The buffer being killed will be current while the hook
1424 is running.
1426 Any processes that have this buffer as the `process-buffer' are killed
1427 with SIGHUP. */)
1428 (buffer_or_name)
1429 Lisp_Object buffer_or_name;
1431 Lisp_Object buffer;
1432 register struct buffer *b;
1433 register Lisp_Object tem;
1434 register struct Lisp_Marker *m;
1435 struct gcpro gcpro1;
1437 if (NILP (buffer_or_name))
1438 buffer = Fcurrent_buffer ();
1439 else
1440 buffer = Fget_buffer (buffer_or_name);
1441 if (NILP (buffer))
1442 nsberror (buffer_or_name);
1444 b = XBUFFER (buffer);
1446 /* Avoid trouble for buffer already dead. */
1447 if (NILP (b->name))
1448 return Qnil;
1450 tem = get_current_thread ();
1451 if (!EQ (b->owner, Qnil) && !EQ (b->owner, tem))
1452 error ("Buffer locked by another thread");
1454 /* Query if the buffer is still modified. */
1455 if (INTERACTIVE && !NILP (b->filename)
1456 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1458 GCPRO1 (buffer);
1459 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1460 b->name, make_number (0)));
1461 UNGCPRO;
1462 if (NILP (tem))
1463 return Qnil;
1466 /* Run hooks with the buffer to be killed the current buffer. */
1468 int count = SPECPDL_INDEX ();
1469 Lisp_Object arglist[1];
1471 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1472 set_buffer_internal (b);
1474 /* First run the query functions; if any query is answered no,
1475 don't kill the buffer. */
1476 arglist[0] = Qkill_buffer_query_functions;
1477 tem = Frun_hook_with_args_until_failure (1, arglist);
1478 if (NILP (tem))
1479 return unbind_to (count, Qnil);
1481 /* Then run the hooks. */
1482 Frun_hooks (1, &Qkill_buffer_hook);
1483 unbind_to (count, Qnil);
1486 /* We have no more questions to ask. Verify that it is valid
1487 to kill the buffer. This must be done after the questions
1488 since anything can happen within do_yes_or_no_p. */
1490 /* Don't kill the minibuffer now current. */
1491 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1492 return Qnil;
1494 if (NILP (b->name))
1495 return Qnil;
1497 /* When we kill a base buffer, kill all its indirect buffers.
1498 We do it at this stage so nothing terrible happens if they
1499 ask questions or their hooks get errors. */
1500 if (! b->base_buffer)
1502 struct buffer *other;
1504 GCPRO1 (buffer);
1506 for (other = all_buffers; other; other = other->next)
1507 /* all_buffers contains dead buffers too;
1508 don't re-kill them. */
1509 if (other->base_buffer == b && !NILP (other->name))
1511 Lisp_Object buffer;
1512 XSETBUFFER (buffer, other);
1513 Fkill_buffer (buffer);
1516 UNGCPRO;
1519 /* Make this buffer not be current.
1520 In the process, notice if this is the sole visible buffer
1521 and give up if so. */
1522 if (b == current_buffer)
1524 tem = Fother_buffer (buffer, Qnil, Qnil);
1525 Fset_buffer (tem);
1526 if (b == current_buffer)
1527 return Qnil;
1530 /* Notice if the buffer to kill is the sole visible buffer
1531 when we're currently in the mini-buffer, and give up if so. */
1532 XSETBUFFER (tem, current_buffer);
1533 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1535 tem = Fother_buffer (buffer, Qnil, Qnil);
1536 if (EQ (buffer, tem))
1537 return Qnil;
1540 /* Now there is no question: we can kill the buffer. */
1542 #ifdef CLASH_DETECTION
1543 /* Unlock this buffer's file, if it is locked. */
1544 unlock_buffer (b);
1545 #endif /* CLASH_DETECTION */
1547 GCPRO1 (buffer);
1548 kill_buffer_processes (buffer);
1549 UNGCPRO;
1551 /* Killing buffer processes may run sentinels which may
1552 have called kill-buffer. */
1554 if (NILP (b->name))
1555 return Qnil;
1557 clear_charpos_cache (b);
1559 tem = Vinhibit_quit;
1560 Vinhibit_quit = Qt;
1561 replace_buffer_in_all_windows (buffer);
1562 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1563 frames_discard_buffer (buffer);
1564 Vinhibit_quit = tem;
1566 /* Delete any auto-save file, if we saved it in this session.
1567 But not if the buffer is modified. */
1568 if (STRINGP (b->auto_save_file_name)
1569 && BUF_AUTOSAVE_MODIFF (b) != 0
1570 && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b)
1571 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1572 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1574 Lisp_Object tem;
1575 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1576 if (! NILP (tem))
1577 internal_delete_file (b->auto_save_file_name);
1580 if (b->base_buffer)
1582 /* Unchain all markers that belong to this indirect buffer.
1583 Don't unchain the markers that belong to the base buffer
1584 or its other indirect buffers. */
1585 for (m = BUF_MARKERS (b); m; )
1587 struct Lisp_Marker *next = m->next;
1588 if (m->buffer == b)
1589 unchain_marker (m);
1590 m = next;
1593 else
1595 /* Unchain all markers of this buffer and its indirect buffers.
1596 and leave them pointing nowhere. */
1597 for (m = BUF_MARKERS (b); m; )
1599 struct Lisp_Marker *next = m->next;
1600 m->buffer = 0;
1601 m->next = NULL;
1602 m = next;
1604 BUF_MARKERS (b) = NULL;
1605 BUF_INTERVALS (b) = NULL_INTERVAL;
1607 /* Perhaps we should explicitly free the interval tree here... */
1610 /* Reset the local variables, so that this buffer's local values
1611 won't be protected from GC. They would be protected
1612 if they happened to remain encached in their symbols.
1613 This gets rid of them for certain. */
1614 swap_out_buffer_local_variables (b);
1615 reset_buffer_local_variables (b, 1);
1617 b->name = Qnil;
1619 BLOCK_INPUT;
1620 if (! b->base_buffer)
1621 free_buffer_text (b);
1623 if (b->newline_cache)
1625 free_region_cache (b->newline_cache);
1626 b->newline_cache = 0;
1628 if (b->width_run_cache)
1630 free_region_cache (b->width_run_cache);
1631 b->width_run_cache = 0;
1633 b->width_table = Qnil;
1634 UNBLOCK_INPUT;
1635 b->undo_list = Qnil;
1637 return Qt;
1640 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1641 we do this each time BUF is selected visibly, the more recently
1642 selected buffers are always closer to the front of the list. This
1643 means that other_buffer is more likely to choose a relevant buffer. */
1645 void
1646 record_buffer (buf)
1647 Lisp_Object buf;
1649 register Lisp_Object link, prev;
1650 Lisp_Object frame;
1651 frame = selected_frame;
1653 prev = Qnil;
1654 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1656 if (EQ (XCDR (XCAR (link)), buf))
1657 break;
1658 prev = link;
1661 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1662 we cannot use Fdelq itself here because it allows quitting. */
1664 if (NILP (prev))
1665 Vbuffer_alist = XCDR (Vbuffer_alist);
1666 else
1667 XSETCDR (prev, XCDR (XCDR (prev)));
1669 XSETCDR (link, Vbuffer_alist);
1670 Vbuffer_alist = link;
1672 /* Effectively do a delq on buried_buffer_list. */
1674 prev = Qnil;
1675 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1676 link = XCDR (link))
1678 if (EQ (XCAR (link), buf))
1680 if (NILP (prev))
1681 XFRAME (frame)->buried_buffer_list = XCDR (link);
1682 else
1683 XSETCDR (prev, XCDR (XCDR (prev)));
1684 break;
1686 prev = link;
1689 /* Now move this buffer to the front of frame_buffer_list also. */
1691 prev = Qnil;
1692 for (link = frame_buffer_list (frame); CONSP (link);
1693 link = XCDR (link))
1695 if (EQ (XCAR (link), buf))
1696 break;
1697 prev = link;
1700 /* Effectively do delq. */
1702 if (CONSP (link))
1704 if (NILP (prev))
1705 set_frame_buffer_list (frame,
1706 XCDR (frame_buffer_list (frame)));
1707 else
1708 XSETCDR (prev, XCDR (XCDR (prev)));
1710 XSETCDR (link, frame_buffer_list (frame));
1711 set_frame_buffer_list (frame, link);
1713 else
1714 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1717 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1718 doc: /* Set an appropriate major mode for BUFFER.
1719 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1720 according to `default-major-mode'.
1721 Use this function before selecting the buffer, since it may need to inspect
1722 the current buffer's major mode. */)
1723 (buffer)
1724 Lisp_Object buffer;
1726 int count;
1727 Lisp_Object function;
1729 CHECK_BUFFER (buffer);
1731 if (STRINGP (XBUFFER (buffer)->name)
1732 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1733 function = find_symbol_value (intern ("initial-major-mode"));
1734 else
1736 function = buffer_defaults.major_mode;
1737 if (NILP (function)
1738 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1739 function = current_buffer->major_mode;
1742 if (NILP (function) || EQ (function, Qfundamental_mode))
1743 return Qnil;
1745 count = SPECPDL_INDEX ();
1747 /* To select a nonfundamental mode,
1748 select the buffer temporarily and then call the mode function. */
1750 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1752 Fset_buffer (buffer);
1753 call0 (function);
1755 return unbind_to (count, Qnil);
1758 /* Switch to buffer BUFFER in the selected window.
1759 If NORECORD is non-nil, don't call record_buffer. */
1761 Lisp_Object
1762 switch_to_buffer_1 (buffer_or_name, norecord)
1763 Lisp_Object buffer_or_name, norecord;
1765 register Lisp_Object buffer;
1767 if (NILP (buffer_or_name))
1768 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1769 else
1771 buffer = Fget_buffer (buffer_or_name);
1772 if (NILP (buffer))
1774 buffer = Fget_buffer_create (buffer_or_name);
1775 Fset_buffer_major_mode (buffer);
1778 Fset_buffer (buffer);
1779 if (NILP (norecord))
1780 record_buffer (buffer);
1782 Fset_window_buffer (EQ (selected_window, minibuf_window)
1783 ? Fnext_window (minibuf_window, Qnil, Qnil)
1784 : selected_window,
1785 buffer, Qnil);
1787 return buffer;
1790 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1791 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1792 doc: /* Make BUFFER-OR-NAME current and display it in selected window.
1793 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1794 nil. Return the buffer switched to.
1796 If BUFFER-OR-NAME is a string and does not identify an existing
1797 buffer, create a new buffer with that name. Interactively, if
1798 `confirm-nonexistent-file-or-buffer' is non-nil, request
1799 confirmation before creating a new buffer. If BUFFER-OR-NAME is
1800 nil, switch to buffer returned by `other-buffer'.
1802 Optional second arg NORECORD non-nil means do not put this buffer
1803 at the front of the list of recently selected ones. This
1804 function returns the buffer it switched to as a Lisp object.
1806 If the selected window is the minibuffer window or dedicated to
1807 its buffer, use `pop-to-buffer' for displaying the buffer.
1809 WARNING: This is NOT the way to work on another buffer temporarily
1810 within a Lisp program! Use `set-buffer' instead. That avoids
1811 messing with the window-buffer correspondences. */)
1812 (buffer_or_name, norecord)
1813 Lisp_Object buffer_or_name, norecord;
1815 char *err;
1817 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1819 /* Basically a NOP. Avoid signalling an error in the case where
1820 the selected window is dedicated, or a minibuffer. */
1822 /* But do put this buffer at the front of the buffer list, unless
1823 that has been inhibited. Note that even if BUFFER-OR-NAME is
1824 at the front of the main buffer-list already, we still want to
1825 move it to the front of the frame's buffer list. */
1826 if (NILP (norecord))
1827 record_buffer (buffer_or_name);
1828 return Fset_buffer (buffer_or_name);
1830 else if (EQ (minibuf_window, selected_window)
1831 /* If `dedicated' is neither nil nor t, it means it's
1832 dedicatedness can be overridden by an explicit request
1833 such as a call to switch-to-buffer. */
1834 || EQ (Fwindow_dedicated_p (selected_window), Qt))
1835 /* We can't use the selected window so let `pop-to-buffer' try some
1836 other window. */
1837 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1838 else
1839 return switch_to_buffer_1 (buffer_or_name, norecord);
1842 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1843 doc: /* Return the current buffer as a Lisp object. */)
1846 register Lisp_Object buf;
1847 XSETBUFFER (buf, current_buffer);
1848 return buf;
1851 /* Set the current buffer to B.
1853 We previously set windows_or_buffers_changed here to invalidate
1854 global unchanged information in beg_unchanged and end_unchanged.
1855 This is no longer necessary because we now compute unchanged
1856 information on a buffer-basis. Every action affecting other
1857 windows than the selected one requires a select_window at some
1858 time, and that increments windows_or_buffers_changed. */
1860 void
1861 set_buffer_internal (b)
1862 register struct buffer *b;
1864 if (current_buffer != b)
1865 set_buffer_internal_1 (b);
1868 static void
1869 acquire_buffer (char *end, void *nb)
1871 struct buffer *new_buffer = nb;
1873 /* FIXME this check should be in the caller, for better
1874 single-threaded performance. */
1875 if (other_threads_p ())
1877 /* Let other threads try to acquire a buffer. */
1878 pthread_cond_broadcast (&buffer_cond);
1880 /* If our desired buffer is locked, wait for it. */
1881 while (other_threads_p ()
1882 && !EQ (new_buffer->owner, Qnil)
1883 /* We set the owner to Qt to mean it is being killed. */
1884 && !EQ (new_buffer->owner, Qt))
1885 pthread_cond_wait (&buffer_cond, &global_lock);
1889 /* Mark the thread's current buffer as not having an owner. This is
1890 only ok to call when the thread is shutting down. The global lock
1891 must be held when calling this function. */
1893 void
1894 release_buffer (thread)
1895 struct thread_state *thread;
1897 if (!EQ (thread->m_current_buffer->owner, Qt))
1899 thread->m_current_buffer->owner = Qnil;
1900 pthread_cond_broadcast (&buffer_cond);
1904 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1905 This is used by redisplay. */
1907 void
1908 set_buffer_internal_1 (b)
1909 register struct buffer *b;
1911 register struct buffer *old_buf;
1912 register Lisp_Object tail, valcontents;
1913 Lisp_Object tem;
1915 #ifdef USE_MMAP_FOR_BUFFERS
1916 if (b->text->beg == NULL)
1917 enlarge_buffer_text (b, 0);
1918 #endif /* USE_MMAP_FOR_BUFFERS */
1920 if (current_buffer == b)
1921 return;
1923 old_buf = current_buffer;
1924 if (current_buffer)
1925 current_buffer->owner = Qnil;
1926 flush_stack_call_func (acquire_buffer, b);
1927 /* FIXME: if buffer is killed */
1928 b->owner = get_current_thread ();
1929 current_buffer = b;
1930 last_known_column_point = -1; /* invalidate indentation cache */
1932 if (old_buf)
1934 /* Put the undo list back in the base buffer, so that it appears
1935 that an indirect buffer shares the undo list of its base. */
1936 if (old_buf->base_buffer)
1937 old_buf->base_buffer->undo_list = old_buf->undo_list;
1939 /* If the old current buffer has markers to record PT, BEGV and ZV
1940 when it is not current, update them now. */
1941 if (! NILP (old_buf->pt_marker))
1943 Lisp_Object obuf;
1944 XSETBUFFER (obuf, old_buf);
1945 set_marker_both (old_buf->pt_marker, obuf,
1946 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1948 if (! NILP (old_buf->begv_marker))
1950 Lisp_Object obuf;
1951 XSETBUFFER (obuf, old_buf);
1952 set_marker_both (old_buf->begv_marker, obuf,
1953 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1955 if (! NILP (old_buf->zv_marker))
1957 Lisp_Object obuf;
1958 XSETBUFFER (obuf, old_buf);
1959 set_marker_both (old_buf->zv_marker, obuf,
1960 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1964 /* Get the undo list from the base buffer, so that it appears
1965 that an indirect buffer shares the undo list of its base. */
1966 if (b->base_buffer)
1967 b->undo_list = b->base_buffer->undo_list;
1969 /* If the new current buffer has markers to record PT, BEGV and ZV
1970 when it is not current, fetch them now. */
1971 if (! NILP (b->pt_marker))
1973 BUF_PT (b) = marker_position (b->pt_marker);
1974 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1976 if (! NILP (b->begv_marker))
1978 BUF_BEGV (b) = marker_position (b->begv_marker);
1979 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1981 if (! NILP (b->zv_marker))
1983 BUF_ZV (b) = marker_position (b->zv_marker);
1984 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1987 /* Look down buffer's list of local Lisp variables
1988 to find and update any that forward into C variables. */
1990 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1992 if (CONSP (XCAR (tail))
1993 && SYMBOLP (XCAR (XCAR (tail)))
1994 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
1995 (BUFFER_LOCAL_VALUEP (valcontents)))
1996 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1997 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1998 /* Just reference the variable to cause it to become set for
1999 this buffer. */
2000 Fsymbol_value (XCAR (XCAR (tail)));
2003 /* Do the same with any others that were local to the previous buffer */
2005 if (old_buf)
2006 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
2008 if (CONSP (tail)
2009 && SYMBOLP (XCAR (XCAR (tail)))
2010 && (valcontents = SYMBOL_VALUE (XCAR (XCAR (tail))),
2011 (BUFFER_LOCAL_VALUEP (valcontents)))
2012 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
2013 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
2014 /* Just reference the variable to cause it to become set for
2015 this buffer. */
2016 Fsymbol_value (XCAR (XCAR (tail)));
2020 /* Switch to buffer B temporarily for redisplay purposes.
2021 This avoids certain things that don't need to be done within redisplay. */
2023 void
2024 set_buffer_temp (b)
2025 struct buffer *b;
2027 register struct buffer *old_buf;
2029 if (current_buffer == b)
2030 return;
2032 old_buf = current_buffer;
2033 current_buffer = b;
2035 if (old_buf)
2037 /* If the old current buffer has markers to record PT, BEGV and ZV
2038 when it is not current, update them now. */
2039 if (! NILP (old_buf->pt_marker))
2041 Lisp_Object obuf;
2042 XSETBUFFER (obuf, old_buf);
2043 set_marker_both (old_buf->pt_marker, obuf,
2044 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
2046 if (! NILP (old_buf->begv_marker))
2048 Lisp_Object obuf;
2049 XSETBUFFER (obuf, old_buf);
2050 set_marker_both (old_buf->begv_marker, obuf,
2051 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
2053 if (! NILP (old_buf->zv_marker))
2055 Lisp_Object obuf;
2056 XSETBUFFER (obuf, old_buf);
2057 set_marker_both (old_buf->zv_marker, obuf,
2058 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
2062 /* If the new current buffer has markers to record PT, BEGV and ZV
2063 when it is not current, fetch them now. */
2064 if (! NILP (b->pt_marker))
2066 BUF_PT (b) = marker_position (b->pt_marker);
2067 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
2069 if (! NILP (b->begv_marker))
2071 BUF_BEGV (b) = marker_position (b->begv_marker);
2072 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
2074 if (! NILP (b->zv_marker))
2076 BUF_ZV (b) = marker_position (b->zv_marker);
2077 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2081 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2082 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
2083 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
2084 also `save-excursion' when you want to make a buffer current
2085 temporarily. This function does not display the buffer, so its effect
2086 ends when the current command terminates. Use `switch-to-buffer' or
2087 `pop-to-buffer' to switch buffers permanently. */)
2088 (buffer_or_name)
2089 register Lisp_Object buffer_or_name;
2091 register Lisp_Object buffer;
2092 buffer = Fget_buffer (buffer_or_name);
2093 if (NILP (buffer))
2094 nsberror (buffer_or_name);
2095 if (NILP (XBUFFER (buffer)->name))
2096 error ("Selecting deleted buffer");
2097 set_buffer_internal (XBUFFER (buffer));
2098 return buffer;
2101 /* Set the current buffer to BUFFER provided it is alive. */
2103 Lisp_Object
2104 set_buffer_if_live (buffer)
2105 Lisp_Object buffer;
2107 if (! NILP (XBUFFER (buffer)->name))
2108 Fset_buffer (buffer);
2109 return Qnil;
2112 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2113 Sbarf_if_buffer_read_only, 0, 0, 0,
2114 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2117 if (!NILP (current_buffer->read_only)
2118 && NILP (Vinhibit_read_only))
2119 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2120 return Qnil;
2123 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2124 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
2125 There it is the least likely candidate for `other-buffer' to return;
2126 thus, the least likely buffer for \\[switch-to-buffer] to select by
2127 default.
2129 The argument may be a buffer name or an actual buffer object. If
2130 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2131 from the selected window if it is displayed there. If the selected
2132 window is dedicated to its buffer, delete that window if there are other
2133 windows on the same frame. If the selected window is the only window on
2134 its frame, iconify that frame. */)
2135 (buffer_or_name)
2136 register Lisp_Object buffer_or_name;
2138 Lisp_Object buffer;
2140 /* Figure out what buffer we're going to bury. */
2141 if (NILP (buffer_or_name))
2143 Lisp_Object tem;
2144 XSETBUFFER (buffer, current_buffer);
2146 tem = Fwindow_buffer (selected_window);
2147 /* If we're burying the current buffer, unshow it. */
2148 if (EQ (buffer, tem))
2150 if (NILP (Fwindow_dedicated_p (selected_window)))
2151 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2152 else if (NILP (XWINDOW (selected_window)->parent))
2153 Ficonify_frame (Fwindow_frame (selected_window));
2154 else
2155 Fdelete_window (selected_window);
2158 else
2160 buffer = Fget_buffer (buffer_or_name);
2161 if (NILP (buffer))
2162 nsberror (buffer_or_name);
2165 /* Move buffer to the end of the buffer list. Do nothing if the
2166 buffer is killed. */
2167 if (!NILP (XBUFFER (buffer)->name))
2169 Lisp_Object aelt, link;
2171 aelt = Frassq (buffer, Vbuffer_alist);
2172 link = Fmemq (aelt, Vbuffer_alist);
2173 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2174 XSETCDR (link, Qnil);
2175 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2177 XFRAME (selected_frame)->buffer_list
2178 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2179 XFRAME (selected_frame)->buried_buffer_list
2180 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2183 return Qnil;
2186 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2187 doc: /* Delete the entire contents of the current buffer.
2188 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2189 so the buffer is truly empty after this. */)
2192 Fwiden ();
2194 del_range (BEG, Z);
2196 current_buffer->last_window_start = 1;
2197 /* Prevent warnings, or suspension of auto saving, that would happen
2198 if future size is less than past size. Use of erase-buffer
2199 implies that the future text is not really related to the past text. */
2200 XSETFASTINT (current_buffer->save_length, 0);
2201 return Qnil;
2204 void
2205 validate_region (b, e)
2206 register Lisp_Object *b, *e;
2208 CHECK_NUMBER_COERCE_MARKER (*b);
2209 CHECK_NUMBER_COERCE_MARKER (*e);
2211 if (XINT (*b) > XINT (*e))
2213 Lisp_Object tem;
2214 tem = *b; *b = *e; *e = tem;
2217 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2218 && XINT (*e) <= ZV))
2219 args_out_of_range (*b, *e);
2222 /* Advance BYTE_POS up to a character boundary
2223 and return the adjusted position. */
2225 static int
2226 advance_to_char_boundary (byte_pos)
2227 int byte_pos;
2229 int c;
2231 if (byte_pos == BEG)
2232 /* Beginning of buffer is always a character boundary. */
2233 return BEG;
2235 c = FETCH_BYTE (byte_pos);
2236 if (! CHAR_HEAD_P (c))
2238 /* We should advance BYTE_POS only when C is a constituent of a
2239 multibyte sequence. */
2240 int orig_byte_pos = byte_pos;
2244 byte_pos--;
2245 c = FETCH_BYTE (byte_pos);
2247 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2248 INC_POS (byte_pos);
2249 if (byte_pos < orig_byte_pos)
2250 byte_pos = orig_byte_pos;
2251 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2252 surely advance to the correct character boundary. If C is
2253 not, BYTE_POS was unchanged. */
2256 return byte_pos;
2259 #ifdef REL_ALLOC
2260 extern void r_alloc_reset_variable P_ ((POINTER_TYPE *, POINTER_TYPE *));
2261 #endif /* REL_ALLOC */
2263 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2264 1, 1, 0,
2265 doc: /* Swap the text between current buffer and BUFFER. */)
2266 (buffer)
2267 Lisp_Object buffer;
2269 struct buffer *other_buffer;
2270 CHECK_BUFFER (buffer);
2271 other_buffer = XBUFFER (buffer);
2273 if (NILP (other_buffer->name))
2274 error ("Cannot swap a dead buffer's text");
2276 /* Actually, it probably works just fine.
2277 * if (other_buffer == current_buffer)
2278 * error ("Cannot swap a buffer's text with itself"); */
2280 /* Actually, this may be workable as well, tho probably only if they're
2281 *both* indirect. */
2282 if (other_buffer->base_buffer
2283 || current_buffer->base_buffer)
2284 error ("Cannot swap indirect buffers's text");
2286 { /* This is probably harder to make work. */
2287 struct buffer *other;
2288 for (other = all_buffers; other; other = other->next)
2289 if (other->base_buffer == other_buffer
2290 || other->base_buffer == current_buffer)
2291 error ("One of the buffers to swap has indirect buffers");
2294 #define swapfield(field, type) \
2295 do { \
2296 type tmp##field = other_buffer->field; \
2297 other_buffer->field = current_buffer->field; \
2298 current_buffer->field = tmp##field; \
2299 } while (0)
2301 swapfield (own_text, struct buffer_text);
2302 eassert (current_buffer->text == &current_buffer->own_text);
2303 eassert (other_buffer->text == &other_buffer->own_text);
2304 #ifdef REL_ALLOC
2305 r_alloc_reset_variable ((POINTER_TYPE **) &current_buffer->own_text.beg,
2306 (POINTER_TYPE **) &other_buffer->own_text.beg);
2307 r_alloc_reset_variable ((POINTER_TYPE **) &other_buffer->own_text.beg,
2308 (POINTER_TYPE **) &current_buffer->own_text.beg);
2309 #endif /* REL_ALLOC */
2311 swapfield (pt, EMACS_INT);
2312 swapfield (pt_byte, EMACS_INT);
2313 swapfield (begv, EMACS_INT);
2314 swapfield (begv_byte, EMACS_INT);
2315 swapfield (zv, EMACS_INT);
2316 swapfield (zv_byte, EMACS_INT);
2317 eassert (!current_buffer->base_buffer);
2318 eassert (!other_buffer->base_buffer);
2319 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2320 swapfield (newline_cache, struct region_cache *);
2321 swapfield (width_run_cache, struct region_cache *);
2322 current_buffer->prevent_redisplay_optimizations_p = 1;
2323 other_buffer->prevent_redisplay_optimizations_p = 1;
2324 swapfield (overlays_before, struct Lisp_Overlay *);
2325 swapfield (overlays_after, struct Lisp_Overlay *);
2326 swapfield (overlay_center, EMACS_INT);
2327 swapfield (undo_list, Lisp_Object);
2328 swapfield (mark, Lisp_Object);
2329 swapfield (enable_multibyte_characters, Lisp_Object);
2330 /* FIXME: Not sure what we should do with these *_marker fields.
2331 Hopefully they're just nil anyway. */
2332 swapfield (pt_marker, Lisp_Object);
2333 swapfield (begv_marker, Lisp_Object);
2334 swapfield (zv_marker, Lisp_Object);
2335 current_buffer->point_before_scroll = Qnil;
2336 other_buffer->point_before_scroll = Qnil;
2338 current_buffer->text->modiff++; other_buffer->text->modiff++;
2339 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2340 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2341 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2342 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2343 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2344 other_buffer->text->end_unchanged = other_buffer->text->gpt;
2346 struct Lisp_Marker *m;
2347 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2348 if (m->buffer == other_buffer)
2349 m->buffer = current_buffer;
2350 else
2351 /* Since there's no indirect buffer in sight, markers on
2352 BUF_MARKERS(buf) should either be for `buf' or dead. */
2353 eassert (!m->buffer);
2354 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2355 if (m->buffer == current_buffer)
2356 m->buffer = other_buffer;
2357 else
2358 /* Since there's no indirect buffer in sight, markers on
2359 BUF_MARKERS(buf) should either be for `buf' or dead. */
2360 eassert (!m->buffer);
2362 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2363 So since we just swapped the markers between the two buffers, we need
2364 to undo the effect of this swap for window markers. */
2365 Lisp_Object w = Fselected_window (), ws = Qnil;
2366 Lisp_Object buf1, buf2;
2367 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2369 while (NILP (Fmemq (w, ws)))
2371 ws = Fcons (w, ws);
2372 if (MARKERP (XWINDOW (w)->pointm)
2373 && (EQ (XWINDOW (w)->buffer, buf1)
2374 || EQ (XWINDOW (w)->buffer, buf2)))
2375 Fset_marker (XWINDOW (w)->pointm,
2376 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2377 XWINDOW (w)->buffer);
2378 w = Fnext_window (w, Qt, Qt);
2382 if (current_buffer->text->intervals)
2383 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2384 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2385 if (other_buffer->text->intervals)
2386 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2387 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2389 return Qnil;
2392 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2393 1, 1, 0,
2394 doc: /* Set the multibyte flag of the current buffer to FLAG.
2395 If FLAG is t, this makes the buffer a multibyte buffer.
2396 If FLAG is nil, this makes the buffer a single-byte buffer.
2397 In these cases, the buffer contents remain unchanged as a sequence of
2398 bytes but the contents viewed as characters do change.
2399 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2400 all eight-bit bytes to eight-bit characters.
2401 If the multibyte flag was really changed, undo information of the
2402 current buffer is cleared. */)
2403 (flag)
2404 Lisp_Object flag;
2406 struct Lisp_Marker *tail, *markers;
2407 struct buffer *other;
2408 int begv, zv;
2409 int narrowed = (BEG != BEGV || Z != ZV);
2410 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2411 Lisp_Object old_undo = current_buffer->undo_list;
2412 struct gcpro gcpro1;
2414 if (current_buffer->base_buffer)
2415 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2417 /* Do nothing if nothing actually changes. */
2418 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2419 return flag;
2421 GCPRO1 (old_undo);
2423 /* Don't record these buffer changes. We will put a special undo entry
2424 instead. */
2425 current_buffer->undo_list = Qt;
2427 /* If the cached position is for this buffer, clear it out. */
2428 clear_charpos_cache (current_buffer);
2430 if (NILP (flag))
2431 begv = BEGV_BYTE, zv = ZV_BYTE;
2432 else
2433 begv = BEGV, zv = ZV;
2435 if (narrowed)
2436 Fwiden ();
2438 if (NILP (flag))
2440 int pos, stop;
2441 unsigned char *p;
2443 /* Do this first, so it can use CHAR_TO_BYTE
2444 to calculate the old correspondences. */
2445 set_intervals_multibyte (0);
2447 current_buffer->enable_multibyte_characters = Qnil;
2449 Z = Z_BYTE;
2450 BEGV = BEGV_BYTE;
2451 ZV = ZV_BYTE;
2452 GPT = GPT_BYTE;
2453 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2456 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2457 tail->charpos = tail->bytepos;
2459 /* Convert multibyte form of 8-bit characters to unibyte. */
2460 pos = BEG;
2461 stop = GPT;
2462 p = BEG_ADDR;
2463 while (1)
2465 int c, bytes;
2467 if (pos == stop)
2469 if (pos == Z)
2470 break;
2471 p = GAP_END_ADDR;
2472 stop = Z;
2474 if (ASCII_BYTE_P (*p))
2475 p++, pos++;
2476 else if (CHAR_BYTE8_HEAD_P (*p))
2478 c = STRING_CHAR_AND_LENGTH (p, bytes);
2479 /* Delete all bytes for this 8-bit character but the
2480 last one, and change the last one to the charcter
2481 code. */
2482 bytes--;
2483 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2484 p = GAP_END_ADDR;
2485 *p++ = c;
2486 pos++;
2487 if (begv > pos)
2488 begv -= bytes;
2489 if (zv > pos)
2490 zv -= bytes;
2491 stop = Z;
2493 else
2495 bytes = BYTES_BY_CHAR_HEAD (*p);
2496 p += bytes, pos += bytes;
2499 if (narrowed)
2500 Fnarrow_to_region (make_number (begv), make_number (zv));
2502 else
2504 int pt = PT;
2505 int pos, stop;
2506 unsigned char *p, *pend;
2508 /* Be sure not to have a multibyte sequence striding over the GAP.
2509 Ex: We change this: "...abc\302 _GAP_ \241def..."
2510 to: "...abc _GAP_ \302\241def..." */
2512 if (EQ (flag, Qt)
2513 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2514 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2516 unsigned char *p = GPT_ADDR - 1;
2518 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2519 if (BASE_LEADING_CODE_P (*p))
2521 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2523 move_gap_both (new_gpt, new_gpt);
2527 /* Make the buffer contents valid as multibyte by converting
2528 8-bit characters to multibyte form. */
2529 pos = BEG;
2530 stop = GPT;
2531 p = BEG_ADDR;
2532 pend = GPT_ADDR;
2533 while (1)
2535 int bytes;
2537 if (pos == stop)
2539 if (pos == Z)
2540 break;
2541 p = GAP_END_ADDR;
2542 pend = Z_ADDR;
2543 stop = Z;
2546 if (ASCII_BYTE_P (*p))
2547 p++, pos++;
2548 else if (EQ (flag, Qt)
2549 && ! CHAR_BYTE8_HEAD_P (*p)
2550 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2551 p += bytes, pos += bytes;
2552 else
2554 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2555 int c;
2557 c = BYTE8_TO_CHAR (*p);
2558 bytes = CHAR_STRING (c, tmp);
2559 *p = tmp[0];
2560 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2561 bytes--;
2562 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2563 /* Now the gap is after the just inserted data. */
2564 pos = GPT;
2565 p = GAP_END_ADDR;
2566 if (pos <= begv)
2567 begv += bytes;
2568 if (pos <= zv)
2569 zv += bytes;
2570 if (pos <= pt)
2571 pt += bytes;
2572 pend = Z_ADDR;
2573 stop = Z;
2577 if (pt != PT)
2578 TEMP_SET_PT (pt);
2580 if (narrowed)
2581 Fnarrow_to_region (make_number (begv), make_number (zv));
2583 /* Do this first, so that chars_in_text asks the right question.
2584 set_intervals_multibyte needs it too. */
2585 current_buffer->enable_multibyte_characters = Qt;
2587 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2588 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2590 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2592 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2593 if (BEGV_BYTE > GPT_BYTE)
2594 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2595 else
2596 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2598 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2599 if (ZV_BYTE > GPT_BYTE)
2600 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2601 else
2602 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2605 int pt_byte = advance_to_char_boundary (PT_BYTE);
2606 int pt;
2608 if (pt_byte > GPT_BYTE)
2609 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2610 else
2611 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2612 TEMP_SET_PT_BOTH (pt, pt_byte);
2615 tail = markers = BUF_MARKERS (current_buffer);
2617 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2618 getting confused by the markers that have not yet been updated.
2619 It is also a signal that it should never create a marker. */
2620 BUF_MARKERS (current_buffer) = NULL;
2622 for (; tail; tail = tail->next)
2624 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2625 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2628 /* Make sure no markers were put on the chain
2629 while the chain value was incorrect. */
2630 if (BUF_MARKERS (current_buffer))
2631 abort ();
2633 BUF_MARKERS (current_buffer) = markers;
2635 /* Do this last, so it can calculate the new correspondences
2636 between chars and bytes. */
2637 set_intervals_multibyte (1);
2640 if (!EQ (old_undo, Qt))
2642 /* Represent all the above changes by a special undo entry. */
2643 extern Lisp_Object Qapply;
2644 current_buffer->undo_list = Fcons (list3 (Qapply,
2645 intern ("set-buffer-multibyte"),
2646 NILP (flag) ? Qt : Qnil),
2647 old_undo);
2650 UNGCPRO;
2652 /* Changing the multibyteness of a buffer means that all windows
2653 showing that buffer must be updated thoroughly. */
2654 current_buffer->prevent_redisplay_optimizations_p = 1;
2655 ++windows_or_buffers_changed;
2657 /* Copy this buffer's new multibyte status
2658 into all of its indirect buffers. */
2659 for (other = all_buffers; other; other = other->next)
2660 if (other->base_buffer == current_buffer && !NILP (other->name))
2662 other->enable_multibyte_characters
2663 = current_buffer->enable_multibyte_characters;
2664 other->prevent_redisplay_optimizations_p = 1;
2667 /* Restore the modifiedness of the buffer. */
2668 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2669 Fset_buffer_modified_p (Qnil);
2671 #ifdef subprocesses
2672 /* Update coding systems of this buffer's process (if any). */
2674 Lisp_Object process;
2676 process = Fget_buffer_process (Fcurrent_buffer ());
2677 if (PROCESSP (process))
2678 setup_process_coding_systems (process);
2680 #endif /* subprocesses */
2682 return flag;
2685 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2686 0, 0, 0,
2687 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2688 Most local variable bindings are eliminated so that the default values
2689 become effective once more. Also, the syntax table is set from
2690 `standard-syntax-table', the local keymap is set to nil,
2691 and the abbrev table from `fundamental-mode-abbrev-table'.
2692 This function also forces redisplay of the mode line.
2694 Every function to select a new major mode starts by
2695 calling this function.
2697 As a special exception, local variables whose names have
2698 a non-nil `permanent-local' property are not eliminated by this function.
2700 The first thing this function does is run
2701 the normal hook `change-major-mode-hook'. */)
2704 if (!NILP (Vrun_hooks))
2705 call1 (Vrun_hooks, Qchange_major_mode_hook);
2707 /* Make sure none of the bindings in local_var_alist
2708 remain swapped in, in their symbols. */
2710 swap_out_buffer_local_variables (current_buffer);
2712 /* Actually eliminate all local bindings of this buffer. */
2714 reset_buffer_local_variables (current_buffer, 0);
2716 /* Force mode-line redisplay. Useful here because all major mode
2717 commands call this function. */
2718 update_mode_lines++;
2720 return Qnil;
2723 /* Make sure no local variables remain set up with buffer B
2724 for their current values. */
2726 static void
2727 swap_out_buffer_local_variables (b)
2728 struct buffer *b;
2730 Lisp_Object oalist, alist, sym, buffer;
2732 XSETBUFFER (buffer, b);
2733 oalist = b->local_var_alist;
2735 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2737 if (CONSP (XCAR (alist))
2738 && (sym = XCAR (XCAR (alist)), SYMBOLP (sym))
2739 /* Need not do anything if some other buffer's binding is
2740 now encached. */
2741 && EQ (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer,
2742 buffer))
2744 /* Symbol is set up for this buffer's old local value:
2745 swap it out! */
2746 swap_in_global_binding (sym);
2751 /* Find all the overlays in the current buffer that contain position POS.
2752 Return the number found, and store them in a vector in *VEC_PTR.
2753 Store in *LEN_PTR the size allocated for the vector.
2754 Store in *NEXT_PTR the next position after POS where an overlay starts,
2755 or ZV if there are no more overlays between POS and ZV.
2756 Store in *PREV_PTR the previous position before POS where an overlay ends,
2757 or where an overlay starts which ends at or after POS;
2758 or BEGV if there are no such overlays from BEGV to POS.
2759 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2761 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2762 when this function is called.
2764 If EXTEND is non-zero, we make the vector bigger if necessary.
2765 If EXTEND is zero, we never extend the vector,
2766 and we store only as many overlays as will fit.
2767 But we still return the total number of overlays.
2769 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2770 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2771 default (BEGV or ZV). */
2774 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2775 EMACS_INT pos;
2776 int extend;
2777 Lisp_Object **vec_ptr;
2778 int *len_ptr;
2779 EMACS_INT *next_ptr;
2780 EMACS_INT *prev_ptr;
2781 int change_req;
2783 Lisp_Object overlay, start, end;
2784 struct Lisp_Overlay *tail;
2785 int idx = 0;
2786 int len = *len_ptr;
2787 Lisp_Object *vec = *vec_ptr;
2788 int next = ZV;
2789 int prev = BEGV;
2790 int inhibit_storing = 0;
2792 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2794 int startpos, endpos;
2796 XSETMISC (overlay, tail);
2798 start = OVERLAY_START (overlay);
2799 end = OVERLAY_END (overlay);
2800 endpos = OVERLAY_POSITION (end);
2801 if (endpos < pos)
2803 if (prev < endpos)
2804 prev = endpos;
2805 break;
2807 startpos = OVERLAY_POSITION (start);
2808 /* This one ends at or after POS
2809 so its start counts for PREV_PTR if it's before POS. */
2810 if (prev < startpos && startpos < pos)
2811 prev = startpos;
2812 if (endpos == pos)
2813 continue;
2814 if (startpos <= pos)
2816 if (idx == len)
2818 /* The supplied vector is full.
2819 Either make it bigger, or don't store any more in it. */
2820 if (extend)
2822 /* Make it work with an initial len == 0. */
2823 len *= 2;
2824 if (len == 0)
2825 len = 4;
2826 *len_ptr = len;
2827 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2828 *vec_ptr = vec;
2830 else
2831 inhibit_storing = 1;
2834 if (!inhibit_storing)
2835 vec[idx] = overlay;
2836 /* Keep counting overlays even if we can't return them all. */
2837 idx++;
2839 else if (startpos < next)
2840 next = startpos;
2843 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2845 int startpos, endpos;
2847 XSETMISC (overlay, tail);
2849 start = OVERLAY_START (overlay);
2850 end = OVERLAY_END (overlay);
2851 startpos = OVERLAY_POSITION (start);
2852 if (pos < startpos)
2854 if (startpos < next)
2855 next = startpos;
2856 break;
2858 endpos = OVERLAY_POSITION (end);
2859 if (pos < endpos)
2861 if (idx == len)
2863 if (extend)
2865 /* Make it work with an initial len == 0. */
2866 len *= 2;
2867 if (len == 0)
2868 len = 4;
2869 *len_ptr = len;
2870 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2871 *vec_ptr = vec;
2873 else
2874 inhibit_storing = 1;
2877 if (!inhibit_storing)
2878 vec[idx] = overlay;
2879 idx++;
2881 if (startpos < pos && startpos > prev)
2882 prev = startpos;
2884 else if (endpos < pos && endpos > prev)
2885 prev = endpos;
2886 else if (endpos == pos && startpos > prev
2887 && (!change_req || startpos < pos))
2888 prev = startpos;
2891 if (next_ptr)
2892 *next_ptr = next;
2893 if (prev_ptr)
2894 *prev_ptr = prev;
2895 return idx;
2898 /* Find all the overlays in the current buffer that overlap the range
2899 BEG-END, or are empty at BEG, or are empty at END provided END
2900 denotes the position at the end of the current buffer.
2902 Return the number found, and store them in a vector in *VEC_PTR.
2903 Store in *LEN_PTR the size allocated for the vector.
2904 Store in *NEXT_PTR the next position after POS where an overlay starts,
2905 or ZV if there are no more overlays.
2906 Store in *PREV_PTR the previous position before POS where an overlay ends,
2907 or BEGV if there are no previous overlays.
2908 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2910 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2911 when this function is called.
2913 If EXTEND is non-zero, we make the vector bigger if necessary.
2914 If EXTEND is zero, we never extend the vector,
2915 and we store only as many overlays as will fit.
2916 But we still return the total number of overlays. */
2918 static int
2919 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2920 int beg, end;
2921 int extend;
2922 Lisp_Object **vec_ptr;
2923 int *len_ptr;
2924 int *next_ptr;
2925 int *prev_ptr;
2927 Lisp_Object overlay, ostart, oend;
2928 struct Lisp_Overlay *tail;
2929 int idx = 0;
2930 int len = *len_ptr;
2931 Lisp_Object *vec = *vec_ptr;
2932 int next = ZV;
2933 int prev = BEGV;
2934 int inhibit_storing = 0;
2935 int end_is_Z = end == Z;
2937 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2939 int startpos, endpos;
2941 XSETMISC (overlay, tail);
2943 ostart = OVERLAY_START (overlay);
2944 oend = OVERLAY_END (overlay);
2945 endpos = OVERLAY_POSITION (oend);
2946 if (endpos < beg)
2948 if (prev < endpos)
2949 prev = endpos;
2950 break;
2952 startpos = OVERLAY_POSITION (ostart);
2953 /* Count an interval if it overlaps the range, is empty at the
2954 start of the range, or is empty at END provided END denotes the
2955 end of the buffer. */
2956 if ((beg < endpos && startpos < end)
2957 || (startpos == endpos
2958 && (beg == endpos || (end_is_Z && endpos == end))))
2960 if (idx == len)
2962 /* The supplied vector is full.
2963 Either make it bigger, or don't store any more in it. */
2964 if (extend)
2966 /* Make it work with an initial len == 0. */
2967 len *= 2;
2968 if (len == 0)
2969 len = 4;
2970 *len_ptr = len;
2971 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2972 *vec_ptr = vec;
2974 else
2975 inhibit_storing = 1;
2978 if (!inhibit_storing)
2979 vec[idx] = overlay;
2980 /* Keep counting overlays even if we can't return them all. */
2981 idx++;
2983 else if (startpos < next)
2984 next = startpos;
2987 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2989 int startpos, endpos;
2991 XSETMISC (overlay, tail);
2993 ostart = OVERLAY_START (overlay);
2994 oend = OVERLAY_END (overlay);
2995 startpos = OVERLAY_POSITION (ostart);
2996 if (end < startpos)
2998 if (startpos < next)
2999 next = startpos;
3000 break;
3002 endpos = OVERLAY_POSITION (oend);
3003 /* Count an interval if it overlaps the range, is empty at the
3004 start of the range, or is empty at END provided END denotes the
3005 end of the buffer. */
3006 if ((beg < endpos && startpos < end)
3007 || (startpos == endpos
3008 && (beg == endpos || (end_is_Z && endpos == end))))
3010 if (idx == len)
3012 if (extend)
3014 /* Make it work with an initial len == 0. */
3015 len *= 2;
3016 if (len == 0)
3017 len = 4;
3018 *len_ptr = len;
3019 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
3020 *vec_ptr = vec;
3022 else
3023 inhibit_storing = 1;
3026 if (!inhibit_storing)
3027 vec[idx] = overlay;
3028 idx++;
3030 else if (endpos < beg && endpos > prev)
3031 prev = endpos;
3034 if (next_ptr)
3035 *next_ptr = next;
3036 if (prev_ptr)
3037 *prev_ptr = prev;
3038 return idx;
3042 /* Return non-zero if there exists an overlay with a non-nil
3043 `mouse-face' property overlapping OVERLAY. */
3046 mouse_face_overlay_overlaps (overlay)
3047 Lisp_Object overlay;
3049 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
3050 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
3051 int n, i, size;
3052 Lisp_Object *v, tem;
3054 size = 10;
3055 v = (Lisp_Object *) alloca (size * sizeof *v);
3056 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
3057 if (n > size)
3059 v = (Lisp_Object *) alloca (n * sizeof *v);
3060 overlays_in (start, end, 0, &v, &n, NULL, NULL);
3063 for (i = 0; i < n; ++i)
3064 if (!EQ (v[i], overlay)
3065 && (tem = Foverlay_get (overlay, Qmouse_face),
3066 !NILP (tem)))
3067 break;
3069 return i < n;
3074 /* Fast function to just test if we're at an overlay boundary. */
3076 overlay_touches_p (pos)
3077 int pos;
3079 Lisp_Object overlay;
3080 struct Lisp_Overlay *tail;
3082 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
3084 int endpos;
3086 XSETMISC (overlay ,tail);
3087 if (!OVERLAYP (overlay))
3088 abort ();
3090 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3091 if (endpos < pos)
3092 break;
3093 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3094 return 1;
3097 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
3099 int startpos;
3101 XSETMISC (overlay, tail);
3102 if (!OVERLAYP (overlay))
3103 abort ();
3105 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3106 if (pos < startpos)
3107 break;
3108 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3109 return 1;
3111 return 0;
3114 struct sortvec
3116 Lisp_Object overlay;
3117 int beg, end;
3118 int priority;
3121 static int
3122 compare_overlays (v1, v2)
3123 const void *v1, *v2;
3125 const struct sortvec *s1 = (const struct sortvec *) v1;
3126 const struct sortvec *s2 = (const struct sortvec *) v2;
3127 if (s1->priority != s2->priority)
3128 return s1->priority - s2->priority;
3129 if (s1->beg != s2->beg)
3130 return s1->beg - s2->beg;
3131 if (s1->end != s2->end)
3132 return s2->end - s1->end;
3133 return 0;
3136 /* Sort an array of overlays by priority. The array is modified in place.
3137 The return value is the new size; this may be smaller than the original
3138 size if some of the overlays were invalid or were window-specific. */
3140 sort_overlays (overlay_vec, noverlays, w)
3141 Lisp_Object *overlay_vec;
3142 int noverlays;
3143 struct window *w;
3145 int i, j;
3146 struct sortvec *sortvec;
3147 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3149 /* Put the valid and relevant overlays into sortvec. */
3151 for (i = 0, j = 0; i < noverlays; i++)
3153 Lisp_Object tem;
3154 Lisp_Object overlay;
3156 overlay = overlay_vec[i];
3157 if (OVERLAY_VALID (overlay)
3158 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3159 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3161 /* If we're interested in a specific window, then ignore
3162 overlays that are limited to some other window. */
3163 if (w)
3165 Lisp_Object window;
3167 window = Foverlay_get (overlay, Qwindow);
3168 if (WINDOWP (window) && XWINDOW (window) != w)
3169 continue;
3172 /* This overlay is good and counts: put it into sortvec. */
3173 sortvec[j].overlay = overlay;
3174 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3175 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3176 tem = Foverlay_get (overlay, Qpriority);
3177 if (INTEGERP (tem))
3178 sortvec[j].priority = XINT (tem);
3179 else
3180 sortvec[j].priority = 0;
3181 j++;
3184 noverlays = j;
3186 /* Sort the overlays into the proper order: increasing priority. */
3188 if (noverlays > 1)
3189 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3191 for (i = 0; i < noverlays; i++)
3192 overlay_vec[i] = sortvec[i].overlay;
3193 return (noverlays);
3196 struct sortstr
3198 Lisp_Object string, string2;
3199 int size;
3200 int priority;
3203 struct sortstrlist
3205 struct sortstr *buf; /* An array that expands as needed; never freed. */
3206 int size; /* Allocated length of that array. */
3207 int used; /* How much of the array is currently in use. */
3208 int bytes; /* Total length of the strings in buf. */
3211 /* Buffers for storing information about the overlays touching a given
3212 position. These could be automatic variables in overlay_strings, but
3213 it's more efficient to hold onto the memory instead of repeatedly
3214 allocating and freeing it. */
3215 static struct sortstrlist overlay_heads, overlay_tails;
3216 static unsigned char *overlay_str_buf;
3218 /* Allocated length of overlay_str_buf. */
3219 static int overlay_str_len;
3221 /* A comparison function suitable for passing to qsort. */
3222 static int
3223 cmp_for_strings (as1, as2)
3224 char *as1, *as2;
3226 struct sortstr *s1 = (struct sortstr *)as1;
3227 struct sortstr *s2 = (struct sortstr *)as2;
3228 if (s1->size != s2->size)
3229 return s2->size - s1->size;
3230 if (s1->priority != s2->priority)
3231 return s1->priority - s2->priority;
3232 return 0;
3235 static void
3236 record_overlay_string (ssl, str, str2, pri, size)
3237 struct sortstrlist *ssl;
3238 Lisp_Object str, str2, pri;
3239 int size;
3241 int nbytes;
3243 if (ssl->used == ssl->size)
3245 if (ssl->buf)
3246 ssl->size *= 2;
3247 else
3248 ssl->size = 5;
3249 ssl->buf = ((struct sortstr *)
3250 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3252 ssl->buf[ssl->used].string = str;
3253 ssl->buf[ssl->used].string2 = str2;
3254 ssl->buf[ssl->used].size = size;
3255 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3256 ssl->used++;
3258 if (NILP (current_buffer->enable_multibyte_characters))
3259 nbytes = SCHARS (str);
3260 else if (! STRING_MULTIBYTE (str))
3261 nbytes = count_size_as_multibyte (SDATA (str),
3262 SBYTES (str));
3263 else
3264 nbytes = SBYTES (str);
3266 ssl->bytes += nbytes;
3268 if (STRINGP (str2))
3270 if (NILP (current_buffer->enable_multibyte_characters))
3271 nbytes = SCHARS (str2);
3272 else if (! STRING_MULTIBYTE (str2))
3273 nbytes = count_size_as_multibyte (SDATA (str2),
3274 SBYTES (str2));
3275 else
3276 nbytes = SBYTES (str2);
3278 ssl->bytes += nbytes;
3282 /* Return the concatenation of the strings associated with overlays that
3283 begin or end at POS, ignoring overlays that are specific to a window
3284 other than W. The strings are concatenated in the appropriate order:
3285 shorter overlays nest inside longer ones, and higher priority inside
3286 lower. Normally all of the after-strings come first, but zero-sized
3287 overlays have their after-strings ride along with the before-strings
3288 because it would look strange to print them inside-out.
3290 Returns the string length, and stores the contents indirectly through
3291 PSTR, if that variable is non-null. The string may be overwritten by
3292 subsequent calls. */
3295 overlay_strings (pos, w, pstr)
3296 EMACS_INT pos;
3297 struct window *w;
3298 unsigned char **pstr;
3300 Lisp_Object overlay, window, str;
3301 struct Lisp_Overlay *ov;
3302 int startpos, endpos;
3303 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3305 overlay_heads.used = overlay_heads.bytes = 0;
3306 overlay_tails.used = overlay_tails.bytes = 0;
3307 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3309 XSETMISC (overlay, ov);
3310 eassert (OVERLAYP (overlay));
3312 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3313 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3314 if (endpos < pos)
3315 break;
3316 if (endpos != pos && startpos != pos)
3317 continue;
3318 window = Foverlay_get (overlay, Qwindow);
3319 if (WINDOWP (window) && XWINDOW (window) != w)
3320 continue;
3321 if (startpos == pos
3322 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3323 record_overlay_string (&overlay_heads, str,
3324 (startpos == endpos
3325 ? Foverlay_get (overlay, Qafter_string)
3326 : Qnil),
3327 Foverlay_get (overlay, Qpriority),
3328 endpos - startpos);
3329 else if (endpos == pos
3330 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3331 record_overlay_string (&overlay_tails, str, Qnil,
3332 Foverlay_get (overlay, Qpriority),
3333 endpos - startpos);
3335 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3337 XSETMISC (overlay, ov);
3338 eassert (OVERLAYP (overlay));
3340 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3341 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3342 if (startpos > pos)
3343 break;
3344 if (endpos != pos && startpos != pos)
3345 continue;
3346 window = Foverlay_get (overlay, Qwindow);
3347 if (WINDOWP (window) && XWINDOW (window) != w)
3348 continue;
3349 if (startpos == pos
3350 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3351 record_overlay_string (&overlay_heads, str,
3352 (startpos == endpos
3353 ? Foverlay_get (overlay, Qafter_string)
3354 : Qnil),
3355 Foverlay_get (overlay, Qpriority),
3356 endpos - startpos);
3357 else if (endpos == pos
3358 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3359 record_overlay_string (&overlay_tails, str, Qnil,
3360 Foverlay_get (overlay, Qpriority),
3361 endpos - startpos);
3363 if (overlay_tails.used > 1)
3364 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3365 cmp_for_strings);
3366 if (overlay_heads.used > 1)
3367 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3368 cmp_for_strings);
3369 if (overlay_heads.bytes || overlay_tails.bytes)
3371 Lisp_Object tem;
3372 int i;
3373 unsigned char *p;
3374 int total = overlay_heads.bytes + overlay_tails.bytes;
3376 if (total > overlay_str_len)
3378 overlay_str_len = total;
3379 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3380 total);
3382 p = overlay_str_buf;
3383 for (i = overlay_tails.used; --i >= 0;)
3385 int nbytes;
3386 tem = overlay_tails.buf[i].string;
3387 nbytes = copy_text (SDATA (tem), p,
3388 SBYTES (tem),
3389 STRING_MULTIBYTE (tem), multibyte);
3390 p += nbytes;
3392 for (i = 0; i < overlay_heads.used; ++i)
3394 int nbytes;
3395 tem = overlay_heads.buf[i].string;
3396 nbytes = copy_text (SDATA (tem), p,
3397 SBYTES (tem),
3398 STRING_MULTIBYTE (tem), multibyte);
3399 p += nbytes;
3400 tem = overlay_heads.buf[i].string2;
3401 if (STRINGP (tem))
3403 nbytes = copy_text (SDATA (tem), p,
3404 SBYTES (tem),
3405 STRING_MULTIBYTE (tem), multibyte);
3406 p += nbytes;
3409 if (p != overlay_str_buf + total)
3410 abort ();
3411 if (pstr)
3412 *pstr = overlay_str_buf;
3413 return total;
3415 return 0;
3418 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3420 void
3421 recenter_overlay_lists (buf, pos)
3422 struct buffer *buf;
3423 EMACS_INT pos;
3425 Lisp_Object overlay, beg, end;
3426 struct Lisp_Overlay *prev, *tail, *next;
3428 /* See if anything in overlays_before should move to overlays_after. */
3430 /* We don't strictly need prev in this loop; it should always be nil.
3431 But we use it for symmetry and in case that should cease to be true
3432 with some future change. */
3433 prev = NULL;
3434 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3436 next = tail->next;
3437 XSETMISC (overlay, tail);
3439 /* If the overlay is not valid, get rid of it. */
3440 if (!OVERLAY_VALID (overlay))
3441 #if 1
3442 abort ();
3443 #else
3445 /* Splice the cons cell TAIL out of overlays_before. */
3446 if (!NILP (prev))
3447 XCDR (prev) = next;
3448 else
3449 buf->overlays_before = next;
3450 tail = prev;
3451 continue;
3453 #endif
3455 beg = OVERLAY_START (overlay);
3456 end = OVERLAY_END (overlay);
3458 if (OVERLAY_POSITION (end) > pos)
3460 /* OVERLAY needs to be moved. */
3461 int where = OVERLAY_POSITION (beg);
3462 struct Lisp_Overlay *other, *other_prev;
3464 /* Splice the cons cell TAIL out of overlays_before. */
3465 if (prev)
3466 prev->next = next;
3467 else
3468 buf->overlays_before = next;
3470 /* Search thru overlays_after for where to put it. */
3471 other_prev = NULL;
3472 for (other = buf->overlays_after; other;
3473 other_prev = other, other = other->next)
3475 Lisp_Object otherbeg, otheroverlay;
3477 XSETMISC (otheroverlay, other);
3478 eassert (OVERLAY_VALID (otheroverlay));
3480 otherbeg = OVERLAY_START (otheroverlay);
3481 if (OVERLAY_POSITION (otherbeg) >= where)
3482 break;
3485 /* Add TAIL to overlays_after before OTHER. */
3486 tail->next = other;
3487 if (other_prev)
3488 other_prev->next = tail;
3489 else
3490 buf->overlays_after = tail;
3491 tail = prev;
3493 else
3494 /* We've reached the things that should stay in overlays_before.
3495 All the rest of overlays_before must end even earlier,
3496 so stop now. */
3497 break;
3500 /* See if anything in overlays_after should be in overlays_before. */
3501 prev = NULL;
3502 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3504 next = tail->next;
3505 XSETMISC (overlay, tail);
3507 /* If the overlay is not valid, get rid of it. */
3508 if (!OVERLAY_VALID (overlay))
3509 #if 1
3510 abort ();
3511 #else
3513 /* Splice the cons cell TAIL out of overlays_after. */
3514 if (!NILP (prev))
3515 XCDR (prev) = next;
3516 else
3517 buf->overlays_after = next;
3518 tail = prev;
3519 continue;
3521 #endif
3523 beg = OVERLAY_START (overlay);
3524 end = OVERLAY_END (overlay);
3526 /* Stop looking, when we know that nothing further
3527 can possibly end before POS. */
3528 if (OVERLAY_POSITION (beg) > pos)
3529 break;
3531 if (OVERLAY_POSITION (end) <= pos)
3533 /* OVERLAY needs to be moved. */
3534 int where = OVERLAY_POSITION (end);
3535 struct Lisp_Overlay *other, *other_prev;
3537 /* Splice the cons cell TAIL out of overlays_after. */
3538 if (prev)
3539 prev->next = next;
3540 else
3541 buf->overlays_after = next;
3543 /* Search thru overlays_before for where to put it. */
3544 other_prev = NULL;
3545 for (other = buf->overlays_before; other;
3546 other_prev = other, other = other->next)
3548 Lisp_Object otherend, otheroverlay;
3550 XSETMISC (otheroverlay, other);
3551 eassert (OVERLAY_VALID (otheroverlay));
3553 otherend = OVERLAY_END (otheroverlay);
3554 if (OVERLAY_POSITION (otherend) <= where)
3555 break;
3558 /* Add TAIL to overlays_before before OTHER. */
3559 tail->next = other;
3560 if (other_prev)
3561 other_prev->next = tail;
3562 else
3563 buf->overlays_before = tail;
3564 tail = prev;
3568 buf->overlay_center = pos;
3571 void
3572 adjust_overlays_for_insert (pos, length)
3573 EMACS_INT pos;
3574 EMACS_INT length;
3576 /* After an insertion, the lists are still sorted properly,
3577 but we may need to update the value of the overlay center. */
3578 if (current_buffer->overlay_center >= pos)
3579 current_buffer->overlay_center += length;
3582 void
3583 adjust_overlays_for_delete (pos, length)
3584 EMACS_INT pos;
3585 EMACS_INT length;
3587 if (current_buffer->overlay_center < pos)
3588 /* The deletion was to our right. No change needed; the before- and
3589 after-lists are still consistent. */
3591 else if (current_buffer->overlay_center > pos + length)
3592 /* The deletion was to our left. We need to adjust the center value
3593 to account for the change in position, but the lists are consistent
3594 given the new value. */
3595 current_buffer->overlay_center -= length;
3596 else
3597 /* We're right in the middle. There might be things on the after-list
3598 that now belong on the before-list. Recentering will move them,
3599 and also update the center point. */
3600 recenter_overlay_lists (current_buffer, pos);
3603 /* Fix up overlays that were garbled as a result of permuting markers
3604 in the range START through END. Any overlay with at least one
3605 endpoint in this range will need to be unlinked from the overlay
3606 list and reinserted in its proper place.
3607 Such an overlay might even have negative size at this point.
3608 If so, we'll make the overlay empty. */
3609 void
3610 fix_start_end_in_overlays (start, end)
3611 register int start, end;
3613 Lisp_Object overlay;
3614 struct Lisp_Overlay *before_list, *after_list;
3615 /* These are either nil, indicating that before_list or after_list
3616 should be assigned, or the cons cell the cdr of which should be
3617 assigned. */
3618 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3619 /* 'Parent', likewise, indicates a cons cell or
3620 current_buffer->overlays_before or overlays_after, depending
3621 which loop we're in. */
3622 struct Lisp_Overlay *tail, *parent;
3623 int startpos, endpos;
3625 /* This algorithm shifts links around instead of consing and GCing.
3626 The loop invariant is that before_list (resp. after_list) is a
3627 well-formed list except that its last element, the CDR of beforep
3628 (resp. afterp) if beforep (afterp) isn't nil or before_list
3629 (after_list) if it is, is still uninitialized. So it's not a bug
3630 that before_list isn't initialized, although it may look
3631 strange. */
3632 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3634 XSETMISC (overlay, tail);
3636 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3637 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3639 /* If the overlay is backwards, make it empty. */
3640 if (endpos < startpos)
3642 startpos = endpos;
3643 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3644 Qnil);
3647 if (endpos < start)
3648 break;
3650 if (endpos < end
3651 || (startpos >= start && startpos < end))
3653 /* Add it to the end of the wrong list. Later on,
3654 recenter_overlay_lists will move it to the right place. */
3655 if (endpos < current_buffer->overlay_center)
3657 if (!afterp)
3658 after_list = tail;
3659 else
3660 afterp->next = tail;
3661 afterp = tail;
3663 else
3665 if (!beforep)
3666 before_list = tail;
3667 else
3668 beforep->next = tail;
3669 beforep = tail;
3671 if (!parent)
3672 current_buffer->overlays_before = tail->next;
3673 else
3674 parent->next = tail->next;
3675 tail = tail->next;
3677 else
3678 parent = tail, tail = parent->next;
3680 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3682 XSETMISC (overlay, tail);
3684 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3685 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3687 /* If the overlay is backwards, make it empty. */
3688 if (endpos < startpos)
3690 startpos = endpos;
3691 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3692 Qnil);
3695 if (startpos >= end)
3696 break;
3698 if (startpos >= start
3699 || (endpos >= start && endpos < end))
3701 if (endpos < current_buffer->overlay_center)
3703 if (!afterp)
3704 after_list = tail;
3705 else
3706 afterp->next = tail;
3707 afterp = tail;
3709 else
3711 if (!beforep)
3712 before_list = tail;
3713 else
3714 beforep->next = tail;
3715 beforep = tail;
3717 if (!parent)
3718 current_buffer->overlays_after = tail->next;
3719 else
3720 parent->next = tail->next;
3721 tail = tail->next;
3723 else
3724 parent = tail, tail = parent->next;
3727 /* Splice the constructed (wrong) lists into the buffer's lists,
3728 and let the recenter function make it sane again. */
3729 if (beforep)
3731 beforep->next = current_buffer->overlays_before;
3732 current_buffer->overlays_before = before_list;
3734 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3736 if (afterp)
3738 afterp->next = current_buffer->overlays_after;
3739 current_buffer->overlays_after = after_list;
3741 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3744 /* We have two types of overlay: the one whose ending marker is
3745 after-insertion-marker (this is the usual case) and the one whose
3746 ending marker is before-insertion-marker. When `overlays_before'
3747 contains overlays of the latter type and the former type in this
3748 order and both overlays end at inserting position, inserting a text
3749 increases only the ending marker of the latter type, which results
3750 in incorrect ordering of `overlays_before'.
3752 This function fixes ordering of overlays in the slot
3753 `overlays_before' of the buffer *BP. Before the insertion, `point'
3754 was at PREV, and now is at POS. */
3756 void
3757 fix_overlays_before (bp, prev, pos)
3758 struct buffer *bp;
3759 EMACS_INT prev, pos;
3761 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3762 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3763 Lisp_Object tem;
3764 EMACS_INT end;
3766 /* After the insertion, the several overlays may be in incorrect
3767 order. The possibility is that, in the list `overlays_before',
3768 an overlay which ends at POS appears after an overlay which ends
3769 at PREV. Since POS is greater than PREV, we must fix the
3770 ordering of these overlays, by moving overlays ends at POS before
3771 the overlays ends at PREV. */
3773 /* At first, find a place where disordered overlays should be linked
3774 in. It is where an overlay which end before POS exists. (i.e. an
3775 overlay whose ending marker is after-insertion-marker if disorder
3776 exists). */
3777 while (tail
3778 && (XSETMISC (tem, tail),
3779 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3781 parent = tail;
3782 tail = tail->next;
3785 /* If we don't find such an overlay,
3786 or the found one ends before PREV,
3787 or the found one is the last one in the list,
3788 we don't have to fix anything. */
3789 if (!tail || end < prev || !tail->next)
3790 return;
3792 right_pair = parent;
3793 parent = tail;
3794 tail = tail->next;
3796 /* Now, end position of overlays in the list TAIL should be before
3797 or equal to PREV. In the loop, an overlay which ends at POS is
3798 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3799 we found an overlay which ends before PREV, the remaining
3800 overlays are in correct order. */
3801 while (tail)
3803 XSETMISC (tem, tail);
3804 end = OVERLAY_POSITION (OVERLAY_END (tem));
3806 if (end == pos)
3807 { /* This overlay is disordered. */
3808 struct Lisp_Overlay *found = tail;
3810 /* Unlink the found overlay. */
3811 tail = found->next;
3812 parent->next = tail;
3813 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3814 and link it into the right place. */
3815 if (!right_pair)
3817 found->next = bp->overlays_before;
3818 bp->overlays_before = found;
3820 else
3822 found->next = right_pair->next;
3823 right_pair->next = found;
3826 else if (end == prev)
3828 parent = tail;
3829 tail = tail->next;
3831 else /* No more disordered overlay. */
3832 break;
3836 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3837 doc: /* Return t if OBJECT is an overlay. */)
3838 (object)
3839 Lisp_Object object;
3841 return (OVERLAYP (object) ? Qt : Qnil);
3844 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3845 doc: /* Create a new overlay with range BEG to END in BUFFER.
3846 If omitted, BUFFER defaults to the current buffer.
3847 BEG and END may be integers or markers.
3848 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3849 for the front of the overlay advance when text is inserted there
3850 \(which means the text *is not* included in the overlay).
3851 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3852 for the rear of the overlay advance when text is inserted there
3853 \(which means the text *is* included in the overlay). */)
3854 (beg, end, buffer, front_advance, rear_advance)
3855 Lisp_Object beg, end, buffer;
3856 Lisp_Object front_advance, rear_advance;
3858 Lisp_Object overlay;
3859 struct buffer *b;
3861 if (NILP (buffer))
3862 XSETBUFFER (buffer, current_buffer);
3863 else
3864 CHECK_BUFFER (buffer);
3865 if (MARKERP (beg)
3866 && ! EQ (Fmarker_buffer (beg), buffer))
3867 error ("Marker points into wrong buffer");
3868 if (MARKERP (end)
3869 && ! EQ (Fmarker_buffer (end), buffer))
3870 error ("Marker points into wrong buffer");
3872 CHECK_NUMBER_COERCE_MARKER (beg);
3873 CHECK_NUMBER_COERCE_MARKER (end);
3875 if (XINT (beg) > XINT (end))
3877 Lisp_Object temp;
3878 temp = beg; beg = end; end = temp;
3881 b = XBUFFER (buffer);
3883 beg = Fset_marker (Fmake_marker (), beg, buffer);
3884 end = Fset_marker (Fmake_marker (), end, buffer);
3886 if (!NILP (front_advance))
3887 XMARKER (beg)->insertion_type = 1;
3888 if (!NILP (rear_advance))
3889 XMARKER (end)->insertion_type = 1;
3891 overlay = allocate_misc ();
3892 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3893 XOVERLAY (overlay)->start = beg;
3894 XOVERLAY (overlay)->end = end;
3895 XOVERLAY (overlay)->plist = Qnil;
3896 XOVERLAY (overlay)->next = NULL;
3898 /* Put the new overlay on the wrong list. */
3899 end = OVERLAY_END (overlay);
3900 if (OVERLAY_POSITION (end) < b->overlay_center)
3902 if (b->overlays_after)
3903 XOVERLAY (overlay)->next = b->overlays_after;
3904 b->overlays_after = XOVERLAY (overlay);
3906 else
3908 if (b->overlays_before)
3909 XOVERLAY (overlay)->next = b->overlays_before;
3910 b->overlays_before = XOVERLAY (overlay);
3913 /* This puts it in the right list, and in the right order. */
3914 recenter_overlay_lists (b, b->overlay_center);
3916 /* We don't need to redisplay the region covered by the overlay, because
3917 the overlay has no properties at the moment. */
3919 return overlay;
3922 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3924 static void
3925 modify_overlay (buf, start, end)
3926 struct buffer *buf;
3927 EMACS_INT start, end;
3929 if (start > end)
3931 int temp = start;
3932 start = end;
3933 end = temp;
3936 BUF_COMPUTE_UNCHANGED (buf, start, end);
3938 /* If this is a buffer not in the selected window,
3939 we must do other windows. */
3940 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3941 windows_or_buffers_changed = 1;
3942 /* If multiple windows show this buffer, we must do other windows. */
3943 else if (buffer_shared > 1)
3944 windows_or_buffers_changed = 1;
3945 /* If we modify an overlay at the end of the buffer, we cannot
3946 be sure that window end is still valid. */
3947 else if (end >= ZV && start <= ZV)
3948 windows_or_buffers_changed = 1;
3950 ++BUF_OVERLAY_MODIFF (buf);
3954 Lisp_Object Fdelete_overlay ();
3956 static struct Lisp_Overlay *
3957 unchain_overlay (list, overlay)
3958 struct Lisp_Overlay *list, *overlay;
3960 struct Lisp_Overlay *tmp, *prev;
3961 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3962 if (tmp == overlay)
3964 if (prev)
3965 prev->next = tmp->next;
3966 else
3967 list = tmp->next;
3968 overlay->next = NULL;
3969 break;
3971 return list;
3974 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3975 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3976 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3977 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3978 buffer. */)
3979 (overlay, beg, end, buffer)
3980 Lisp_Object overlay, beg, end, buffer;
3982 struct buffer *b, *ob;
3983 Lisp_Object obuffer;
3984 int count = SPECPDL_INDEX ();
3986 CHECK_OVERLAY (overlay);
3987 if (NILP (buffer))
3988 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3989 if (NILP (buffer))
3990 XSETBUFFER (buffer, current_buffer);
3991 CHECK_BUFFER (buffer);
3993 if (MARKERP (beg)
3994 && ! EQ (Fmarker_buffer (beg), buffer))
3995 error ("Marker points into wrong buffer");
3996 if (MARKERP (end)
3997 && ! EQ (Fmarker_buffer (end), buffer))
3998 error ("Marker points into wrong buffer");
4000 CHECK_NUMBER_COERCE_MARKER (beg);
4001 CHECK_NUMBER_COERCE_MARKER (end);
4003 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
4004 return Fdelete_overlay (overlay);
4006 if (XINT (beg) > XINT (end))
4008 Lisp_Object temp;
4009 temp = beg; beg = end; end = temp;
4012 specbind (Qinhibit_quit, Qt);
4014 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
4015 b = XBUFFER (buffer);
4016 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
4018 /* If the overlay has changed buffers, do a thorough redisplay. */
4019 if (!EQ (buffer, obuffer))
4021 /* Redisplay where the overlay was. */
4022 if (!NILP (obuffer))
4024 int o_beg;
4025 int o_end;
4027 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
4028 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
4030 modify_overlay (ob, o_beg, o_end);
4033 /* Redisplay where the overlay is going to be. */
4034 modify_overlay (b, XINT (beg), XINT (end));
4036 else
4037 /* Redisplay the area the overlay has just left, or just enclosed. */
4039 int o_beg, o_end;
4041 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
4042 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
4044 if (o_beg == XINT (beg))
4045 modify_overlay (b, o_end, XINT (end));
4046 else if (o_end == XINT (end))
4047 modify_overlay (b, o_beg, XINT (beg));
4048 else
4050 if (XINT (beg) < o_beg) o_beg = XINT (beg);
4051 if (XINT (end) > o_end) o_end = XINT (end);
4052 modify_overlay (b, o_beg, o_end);
4056 if (!NILP (obuffer))
4058 ob->overlays_before
4059 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
4060 ob->overlays_after
4061 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
4062 eassert (XOVERLAY (overlay)->next == NULL);
4065 Fset_marker (OVERLAY_START (overlay), beg, buffer);
4066 Fset_marker (OVERLAY_END (overlay), end, buffer);
4068 /* Put the overlay on the wrong list. */
4069 end = OVERLAY_END (overlay);
4070 if (OVERLAY_POSITION (end) < b->overlay_center)
4072 XOVERLAY (overlay)->next = b->overlays_after;
4073 b->overlays_after = XOVERLAY (overlay);
4075 else
4077 XOVERLAY (overlay)->next = b->overlays_before;
4078 b->overlays_before = XOVERLAY (overlay);
4081 /* This puts it in the right list, and in the right order. */
4082 recenter_overlay_lists (b, b->overlay_center);
4084 return unbind_to (count, overlay);
4087 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
4088 doc: /* Delete the overlay OVERLAY from its buffer. */)
4089 (overlay)
4090 Lisp_Object overlay;
4092 Lisp_Object buffer;
4093 struct buffer *b;
4094 int count = SPECPDL_INDEX ();
4096 CHECK_OVERLAY (overlay);
4098 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4099 if (NILP (buffer))
4100 return Qnil;
4102 b = XBUFFER (buffer);
4103 specbind (Qinhibit_quit, Qt);
4105 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4106 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4107 eassert (XOVERLAY (overlay)->next == NULL);
4108 modify_overlay (b,
4109 marker_position (OVERLAY_START (overlay)),
4110 marker_position (OVERLAY_END (overlay)));
4111 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4112 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4114 /* When deleting an overlay with before or after strings, turn off
4115 display optimizations for the affected buffer, on the basis that
4116 these strings may contain newlines. This is easier to do than to
4117 check for that situation during redisplay. */
4118 if (!windows_or_buffers_changed
4119 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4120 || !NILP (Foverlay_get (overlay, Qafter_string))))
4121 b->prevent_redisplay_optimizations_p = 1;
4123 return unbind_to (count, Qnil);
4126 /* Overlay dissection functions. */
4128 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4129 doc: /* Return the position at which OVERLAY starts. */)
4130 (overlay)
4131 Lisp_Object overlay;
4133 CHECK_OVERLAY (overlay);
4135 return (Fmarker_position (OVERLAY_START (overlay)));
4138 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4139 doc: /* Return the position at which OVERLAY ends. */)
4140 (overlay)
4141 Lisp_Object overlay;
4143 CHECK_OVERLAY (overlay);
4145 return (Fmarker_position (OVERLAY_END (overlay)));
4148 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4149 doc: /* Return the buffer OVERLAY belongs to.
4150 Return nil if OVERLAY has been deleted. */)
4151 (overlay)
4152 Lisp_Object overlay;
4154 CHECK_OVERLAY (overlay);
4156 return Fmarker_buffer (OVERLAY_START (overlay));
4159 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4160 doc: /* Return a list of the properties on OVERLAY.
4161 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4162 OVERLAY. */)
4163 (overlay)
4164 Lisp_Object overlay;
4166 CHECK_OVERLAY (overlay);
4168 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4172 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4173 doc: /* Return a list of the overlays that contain the character at POS. */)
4174 (pos)
4175 Lisp_Object pos;
4177 int noverlays;
4178 Lisp_Object *overlay_vec;
4179 int len;
4180 Lisp_Object result;
4182 CHECK_NUMBER_COERCE_MARKER (pos);
4184 len = 10;
4185 /* We can't use alloca here because overlays_at can call xrealloc. */
4186 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4188 /* Put all the overlays we want in a vector in overlay_vec.
4189 Store the length in len. */
4190 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4191 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4193 /* Make a list of them all. */
4194 result = Flist (noverlays, overlay_vec);
4196 xfree (overlay_vec);
4197 return result;
4200 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4201 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4202 Overlap means that at least one character is contained within the overlay
4203 and also contained within the specified region.
4204 Empty overlays are included in the result if they are located at BEG,
4205 between BEG and END, or at END provided END denotes the position at the
4206 end of the buffer. */)
4207 (beg, end)
4208 Lisp_Object beg, end;
4210 int noverlays;
4211 Lisp_Object *overlay_vec;
4212 int len;
4213 Lisp_Object result;
4215 CHECK_NUMBER_COERCE_MARKER (beg);
4216 CHECK_NUMBER_COERCE_MARKER (end);
4218 len = 10;
4219 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4221 /* Put all the overlays we want in a vector in overlay_vec.
4222 Store the length in len. */
4223 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4224 (int *) 0, (int *) 0);
4226 /* Make a list of them all. */
4227 result = Flist (noverlays, overlay_vec);
4229 xfree (overlay_vec);
4230 return result;
4233 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4234 1, 1, 0,
4235 doc: /* Return the next position after POS where an overlay starts or ends.
4236 If there are no overlay boundaries from POS to (point-max),
4237 the value is (point-max). */)
4238 (pos)
4239 Lisp_Object pos;
4241 int noverlays;
4242 EMACS_INT endpos;
4243 Lisp_Object *overlay_vec;
4244 int len;
4245 int i;
4247 CHECK_NUMBER_COERCE_MARKER (pos);
4249 len = 10;
4250 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4252 /* Put all the overlays we want in a vector in overlay_vec.
4253 Store the length in len.
4254 endpos gets the position where the next overlay starts. */
4255 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4256 &endpos, (EMACS_INT *) 0, 1);
4258 /* If any of these overlays ends before endpos,
4259 use its ending point instead. */
4260 for (i = 0; i < noverlays; i++)
4262 Lisp_Object oend;
4263 EMACS_INT oendpos;
4265 oend = OVERLAY_END (overlay_vec[i]);
4266 oendpos = OVERLAY_POSITION (oend);
4267 if (oendpos < endpos)
4268 endpos = oendpos;
4271 xfree (overlay_vec);
4272 return make_number (endpos);
4275 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4276 Sprevious_overlay_change, 1, 1, 0,
4277 doc: /* Return the previous position before POS where an overlay starts or ends.
4278 If there are no overlay boundaries from (point-min) to POS,
4279 the value is (point-min). */)
4280 (pos)
4281 Lisp_Object pos;
4283 int noverlays;
4284 EMACS_INT prevpos;
4285 Lisp_Object *overlay_vec;
4286 int len;
4288 CHECK_NUMBER_COERCE_MARKER (pos);
4290 /* At beginning of buffer, we know the answer;
4291 avoid bug subtracting 1 below. */
4292 if (XINT (pos) == BEGV)
4293 return pos;
4295 len = 10;
4296 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4298 /* Put all the overlays we want in a vector in overlay_vec.
4299 Store the length in len.
4300 prevpos gets the position of the previous change. */
4301 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4302 (EMACS_INT *) 0, &prevpos, 1);
4304 xfree (overlay_vec);
4305 return make_number (prevpos);
4308 /* These functions are for debugging overlays. */
4310 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4311 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4312 The car has all the overlays before the overlay center;
4313 the cdr has all the overlays after the overlay center.
4314 Recentering overlays moves overlays between these lists.
4315 The lists you get are copies, so that changing them has no effect.
4316 However, the overlays you get are the real objects that the buffer uses. */)
4319 struct Lisp_Overlay *ol;
4320 Lisp_Object before = Qnil, after = Qnil, tmp;
4321 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4323 XSETMISC (tmp, ol);
4324 before = Fcons (tmp, before);
4326 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4328 XSETMISC (tmp, ol);
4329 after = Fcons (tmp, after);
4331 return Fcons (Fnreverse (before), Fnreverse (after));
4334 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4335 doc: /* Recenter the overlays of the current buffer around position POS.
4336 That makes overlay lookup faster for positions near POS (but perhaps slower
4337 for positions far away from POS). */)
4338 (pos)
4339 Lisp_Object pos;
4341 CHECK_NUMBER_COERCE_MARKER (pos);
4343 recenter_overlay_lists (current_buffer, XINT (pos));
4344 return Qnil;
4347 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4348 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4349 (overlay, prop)
4350 Lisp_Object overlay, prop;
4352 CHECK_OVERLAY (overlay);
4353 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4356 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4357 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4358 (overlay, prop, value)
4359 Lisp_Object overlay, prop, value;
4361 Lisp_Object tail, buffer;
4362 int changed;
4364 CHECK_OVERLAY (overlay);
4366 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4368 for (tail = XOVERLAY (overlay)->plist;
4369 CONSP (tail) && CONSP (XCDR (tail));
4370 tail = XCDR (XCDR (tail)))
4371 if (EQ (XCAR (tail), prop))
4373 changed = !EQ (XCAR (XCDR (tail)), value);
4374 XSETCAR (XCDR (tail), value);
4375 goto found;
4377 /* It wasn't in the list, so add it to the front. */
4378 changed = !NILP (value);
4379 XOVERLAY (overlay)->plist
4380 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4381 found:
4382 if (! NILP (buffer))
4384 if (changed)
4385 modify_overlay (XBUFFER (buffer),
4386 marker_position (OVERLAY_START (overlay)),
4387 marker_position (OVERLAY_END (overlay)));
4388 if (EQ (prop, Qevaporate) && ! NILP (value)
4389 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4390 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4391 Fdelete_overlay (overlay);
4394 return value;
4397 /* Subroutine of report_overlay_modification. */
4399 /* Lisp vector holding overlay hook functions to call.
4400 Vector elements come in pairs.
4401 Each even-index element is a list of hook functions.
4402 The following odd-index element is the overlay they came from.
4404 Before the buffer change, we fill in this vector
4405 as we call overlay hook functions.
4406 After the buffer change, we get the functions to call from this vector.
4407 This way we always call the same functions before and after the change. */
4408 static Lisp_Object last_overlay_modification_hooks;
4410 /* Number of elements actually used in last_overlay_modification_hooks. */
4411 static int last_overlay_modification_hooks_used;
4413 /* Add one functionlist/overlay pair
4414 to the end of last_overlay_modification_hooks. */
4416 static void
4417 add_overlay_mod_hooklist (functionlist, overlay)
4418 Lisp_Object functionlist, overlay;
4420 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4422 if (last_overlay_modification_hooks_used == oldsize)
4423 last_overlay_modification_hooks = larger_vector
4424 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4425 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4426 functionlist); last_overlay_modification_hooks_used++;
4427 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4428 overlay); last_overlay_modification_hooks_used++;
4431 /* Run the modification-hooks of overlays that include
4432 any part of the text in START to END.
4433 If this change is an insertion, also
4434 run the insert-before-hooks of overlay starting at END,
4435 and the insert-after-hooks of overlay ending at START.
4437 This is called both before and after the modification.
4438 AFTER is nonzero when we call after the modification.
4440 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4441 When AFTER is nonzero, they are the start position,
4442 the position after the inserted new text,
4443 and the length of deleted or replaced old text. */
4445 void
4446 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4447 Lisp_Object start, end;
4448 int after;
4449 Lisp_Object arg1, arg2, arg3;
4451 Lisp_Object prop, overlay;
4452 struct Lisp_Overlay *tail;
4453 /* 1 if this change is an insertion. */
4454 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4455 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4457 overlay = Qnil;
4458 tail = NULL;
4460 /* We used to run the functions as soon as we found them and only register
4461 them in last_overlay_modification_hooks for the purpose of the `after'
4462 case. But running elisp code as we traverse the list of overlays is
4463 painful because the list can be modified by the elisp code so we had to
4464 copy at several places. We now simply do a read-only traversal that
4465 only collects the functions to run and we run them afterwards. It's
4466 simpler, especially since all the code was already there. -stef */
4468 if (!after)
4470 /* We are being called before a change.
4471 Scan the overlays to find the functions to call. */
4472 last_overlay_modification_hooks_used = 0;
4473 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4475 int startpos, endpos;
4476 Lisp_Object ostart, oend;
4478 XSETMISC (overlay, tail);
4480 ostart = OVERLAY_START (overlay);
4481 oend = OVERLAY_END (overlay);
4482 endpos = OVERLAY_POSITION (oend);
4483 if (XFASTINT (start) > endpos)
4484 break;
4485 startpos = OVERLAY_POSITION (ostart);
4486 if (insertion && (XFASTINT (start) == startpos
4487 || XFASTINT (end) == startpos))
4489 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4490 if (!NILP (prop))
4491 add_overlay_mod_hooklist (prop, overlay);
4493 if (insertion && (XFASTINT (start) == endpos
4494 || XFASTINT (end) == endpos))
4496 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4497 if (!NILP (prop))
4498 add_overlay_mod_hooklist (prop, overlay);
4500 /* Test for intersecting intervals. This does the right thing
4501 for both insertion and deletion. */
4502 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4504 prop = Foverlay_get (overlay, Qmodification_hooks);
4505 if (!NILP (prop))
4506 add_overlay_mod_hooklist (prop, overlay);
4510 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4512 int startpos, endpos;
4513 Lisp_Object ostart, oend;
4515 XSETMISC (overlay, tail);
4517 ostart = OVERLAY_START (overlay);
4518 oend = OVERLAY_END (overlay);
4519 startpos = OVERLAY_POSITION (ostart);
4520 endpos = OVERLAY_POSITION (oend);
4521 if (XFASTINT (end) < startpos)
4522 break;
4523 if (insertion && (XFASTINT (start) == startpos
4524 || XFASTINT (end) == startpos))
4526 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4527 if (!NILP (prop))
4528 add_overlay_mod_hooklist (prop, overlay);
4530 if (insertion && (XFASTINT (start) == endpos
4531 || XFASTINT (end) == endpos))
4533 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4534 if (!NILP (prop))
4535 add_overlay_mod_hooklist (prop, overlay);
4537 /* Test for intersecting intervals. This does the right thing
4538 for both insertion and deletion. */
4539 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4541 prop = Foverlay_get (overlay, Qmodification_hooks);
4542 if (!NILP (prop))
4543 add_overlay_mod_hooklist (prop, overlay);
4548 GCPRO4 (overlay, arg1, arg2, arg3);
4550 /* Call the functions recorded in last_overlay_modification_hooks.
4551 First copy the vector contents, in case some of these hooks
4552 do subsequent modification of the buffer. */
4553 int size = last_overlay_modification_hooks_used;
4554 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4555 int i;
4557 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4558 copy, size * sizeof (Lisp_Object));
4559 gcpro1.var = copy;
4560 gcpro1.nvars = size;
4562 for (i = 0; i < size;)
4564 Lisp_Object prop, overlay;
4565 prop = copy[i++];
4566 overlay = copy[i++];
4567 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4570 UNGCPRO;
4573 static void
4574 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4575 Lisp_Object list, overlay;
4576 int after;
4577 Lisp_Object arg1, arg2, arg3;
4579 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4581 GCPRO4 (list, arg1, arg2, arg3);
4583 while (CONSP (list))
4585 if (NILP (arg3))
4586 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4587 else
4588 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4589 list = XCDR (list);
4591 UNGCPRO;
4594 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4595 property is set. */
4596 void
4597 evaporate_overlays (pos)
4598 EMACS_INT pos;
4600 Lisp_Object overlay, hit_list;
4601 struct Lisp_Overlay *tail;
4603 hit_list = Qnil;
4604 if (pos <= current_buffer->overlay_center)
4605 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4607 int endpos;
4608 XSETMISC (overlay, tail);
4609 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4610 if (endpos < pos)
4611 break;
4612 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4613 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4614 hit_list = Fcons (overlay, hit_list);
4616 else
4617 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4619 int startpos;
4620 XSETMISC (overlay, tail);
4621 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4622 if (startpos > pos)
4623 break;
4624 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4625 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4626 hit_list = Fcons (overlay, hit_list);
4628 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4629 Fdelete_overlay (XCAR (hit_list));
4632 /* Somebody has tried to store a value with an unacceptable type
4633 in the slot with offset OFFSET. */
4635 void
4636 buffer_slot_type_mismatch (newval, type)
4637 Lisp_Object newval;
4638 int type;
4640 Lisp_Object predicate;
4642 switch (type)
4644 case_Lisp_Int: predicate = Qintegerp; break;
4645 case Lisp_String: predicate = Qstringp; break;
4646 case Lisp_Symbol: predicate = Qsymbolp; break;
4647 default: abort ();
4650 wrong_type_argument (predicate, newval);
4654 /***********************************************************************
4655 Allocation with mmap
4656 ***********************************************************************/
4658 #ifdef USE_MMAP_FOR_BUFFERS
4660 #include <sys/types.h>
4661 #include <sys/mman.h>
4663 #ifndef MAP_ANON
4664 #ifdef MAP_ANONYMOUS
4665 #define MAP_ANON MAP_ANONYMOUS
4666 #else
4667 #define MAP_ANON 0
4668 #endif
4669 #endif
4671 #ifndef MAP_FAILED
4672 #define MAP_FAILED ((void *) -1)
4673 #endif
4675 #include <stdio.h>
4676 #include <errno.h>
4678 #if MAP_ANON == 0
4679 #include <fcntl.h>
4680 #endif
4682 #include "coding.h"
4685 /* Memory is allocated in regions which are mapped using mmap(2).
4686 The current implementation lets the system select mapped
4687 addresses; we're not using MAP_FIXED in general, except when
4688 trying to enlarge regions.
4690 Each mapped region starts with a mmap_region structure, the user
4691 area starts after that structure, aligned to MEM_ALIGN.
4693 +-----------------------+
4694 | struct mmap_info + |
4695 | padding |
4696 +-----------------------+
4697 | user data |
4700 +-----------------------+ */
4702 struct mmap_region
4704 /* User-specified size. */
4705 size_t nbytes_specified;
4707 /* Number of bytes mapped */
4708 size_t nbytes_mapped;
4710 /* Pointer to the location holding the address of the memory
4711 allocated with the mmap'd block. The variable actually points
4712 after this structure. */
4713 POINTER_TYPE **var;
4715 /* Next and previous in list of all mmap'd regions. */
4716 struct mmap_region *next, *prev;
4719 /* Doubly-linked list of mmap'd regions. */
4721 static struct mmap_region *mmap_regions;
4723 /* File descriptor for mmap. If we don't have anonymous mapping,
4724 /dev/zero will be opened on it. */
4726 static int mmap_fd;
4728 /* Temporary storage for mmap_set_vars, see there. */
4730 static struct mmap_region *mmap_regions_1;
4731 static int mmap_fd_1;
4733 /* Page size on this system. */
4735 static int mmap_page_size;
4737 /* 1 means mmap has been intialized. */
4739 static int mmap_initialized_p;
4741 /* Value is X rounded up to the next multiple of N. */
4743 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4745 /* Size of mmap_region structure plus padding. */
4747 #define MMAP_REGION_STRUCT_SIZE \
4748 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4750 /* Given a pointer P to the start of the user-visible part of a mapped
4751 region, return a pointer to the start of the region. */
4753 #define MMAP_REGION(P) \
4754 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4756 /* Given a pointer P to the start of a mapped region, return a pointer
4757 to the start of the user-visible part of the region. */
4759 #define MMAP_USER_AREA(P) \
4760 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4762 #define MEM_ALIGN sizeof (double)
4764 /* Predicate returning true if part of the address range [START .. END]
4765 is currently mapped. Used to prevent overwriting an existing
4766 memory mapping.
4768 Default is to conservativly assume the address range is occupied by
4769 something else. This can be overridden by system configuration
4770 files if system-specific means to determine this exists. */
4772 #ifndef MMAP_ALLOCATED_P
4773 #define MMAP_ALLOCATED_P(start, end) 1
4774 #endif
4776 /* Function prototypes. */
4778 static int mmap_free_1 P_ ((struct mmap_region *));
4779 static int mmap_enlarge P_ ((struct mmap_region *, int));
4780 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4781 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4782 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4783 static void mmap_free P_ ((POINTER_TYPE **ptr));
4784 static void mmap_init P_ ((void));
4787 /* Return a region overlapping address range START...END, or null if
4788 none. END is not including, i.e. the last byte in the range
4789 is at END - 1. */
4791 static struct mmap_region *
4792 mmap_find (start, end)
4793 POINTER_TYPE *start, *end;
4795 struct mmap_region *r;
4796 char *s = (char *) start, *e = (char *) end;
4798 for (r = mmap_regions; r; r = r->next)
4800 char *rstart = (char *) r;
4801 char *rend = rstart + r->nbytes_mapped;
4803 if (/* First byte of range, i.e. START, in this region? */
4804 (s >= rstart && s < rend)
4805 /* Last byte of range, i.e. END - 1, in this region? */
4806 || (e > rstart && e <= rend)
4807 /* First byte of this region in the range? */
4808 || (rstart >= s && rstart < e)
4809 /* Last byte of this region in the range? */
4810 || (rend > s && rend <= e))
4811 break;
4814 return r;
4818 /* Unmap a region. P is a pointer to the start of the user-araa of
4819 the region. Value is non-zero if successful. */
4821 static int
4822 mmap_free_1 (r)
4823 struct mmap_region *r;
4825 if (r->next)
4826 r->next->prev = r->prev;
4827 if (r->prev)
4828 r->prev->next = r->next;
4829 else
4830 mmap_regions = r->next;
4832 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4834 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4835 return 0;
4838 return 1;
4842 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4843 Value is non-zero if successful. */
4845 static int
4846 mmap_enlarge (r, npages)
4847 struct mmap_region *r;
4848 int npages;
4850 char *region_end = (char *) r + r->nbytes_mapped;
4851 size_t nbytes;
4852 int success = 0;
4854 if (npages < 0)
4856 /* Unmap pages at the end of the region. */
4857 nbytes = - npages * mmap_page_size;
4858 if (munmap (region_end - nbytes, nbytes) == -1)
4859 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4860 else
4862 r->nbytes_mapped -= nbytes;
4863 success = 1;
4866 else if (npages > 0)
4868 nbytes = npages * mmap_page_size;
4870 /* Try to map additional pages at the end of the region. We
4871 cannot do this if the address range is already occupied by
4872 something else because mmap deletes any previous mapping.
4873 I'm not sure this is worth doing, let's see. */
4874 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4876 POINTER_TYPE *p;
4878 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4879 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4880 if (p == MAP_FAILED)
4881 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4882 else if (p != (POINTER_TYPE *) region_end)
4884 /* Kernels are free to choose a different address. In
4885 that case, unmap what we've mapped above; we have
4886 no use for it. */
4887 if (munmap (p, nbytes) == -1)
4888 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4890 else
4892 r->nbytes_mapped += nbytes;
4893 success = 1;
4898 return success;
4902 /* Set or reset variables holding references to mapped regions. If
4903 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4904 non-zero, set all variables to the start of the user-areas
4905 of mapped regions.
4907 This function is called from Fdump_emacs to ensure that the dumped
4908 Emacs doesn't contain references to memory that won't be mapped
4909 when Emacs starts. */
4911 void
4912 mmap_set_vars (restore_p)
4913 int restore_p;
4915 struct mmap_region *r;
4917 if (restore_p)
4919 mmap_regions = mmap_regions_1;
4920 mmap_fd = mmap_fd_1;
4921 for (r = mmap_regions; r; r = r->next)
4922 *r->var = MMAP_USER_AREA (r);
4924 else
4926 for (r = mmap_regions; r; r = r->next)
4927 *r->var = NULL;
4928 mmap_regions_1 = mmap_regions;
4929 mmap_regions = NULL;
4930 mmap_fd_1 = mmap_fd;
4931 mmap_fd = -1;
4936 /* Allocate a block of storage large enough to hold NBYTES bytes of
4937 data. A pointer to the data is returned in *VAR. VAR is thus the
4938 address of some variable which will use the data area.
4940 The allocation of 0 bytes is valid.
4942 If we can't allocate the necessary memory, set *VAR to null, and
4943 return null. */
4945 static POINTER_TYPE *
4946 mmap_alloc (var, nbytes)
4947 POINTER_TYPE **var;
4948 size_t nbytes;
4950 void *p;
4951 size_t map;
4953 mmap_init ();
4955 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4956 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4957 mmap_fd, 0);
4959 if (p == MAP_FAILED)
4961 if (errno != ENOMEM)
4962 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4963 p = NULL;
4965 else
4967 struct mmap_region *r = (struct mmap_region *) p;
4969 r->nbytes_specified = nbytes;
4970 r->nbytes_mapped = map;
4971 r->var = var;
4972 r->prev = NULL;
4973 r->next = mmap_regions;
4974 if (r->next)
4975 r->next->prev = r;
4976 mmap_regions = r;
4978 p = MMAP_USER_AREA (p);
4981 return *var = p;
4985 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4986 resize it to size NBYTES. Change *VAR to reflect the new block,
4987 and return this value. If more memory cannot be allocated, then
4988 leave *VAR unchanged, and return null. */
4990 static POINTER_TYPE *
4991 mmap_realloc (var, nbytes)
4992 POINTER_TYPE **var;
4993 size_t nbytes;
4995 POINTER_TYPE *result;
4997 mmap_init ();
4999 if (*var == NULL)
5000 result = mmap_alloc (var, nbytes);
5001 else if (nbytes == 0)
5003 mmap_free (var);
5004 result = mmap_alloc (var, nbytes);
5006 else
5008 struct mmap_region *r = MMAP_REGION (*var);
5009 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
5011 if (room < nbytes)
5013 /* Must enlarge. */
5014 POINTER_TYPE *old_ptr = *var;
5016 /* Try to map additional pages at the end of the region.
5017 If that fails, allocate a new region, copy data
5018 from the old region, then free it. */
5019 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
5020 / mmap_page_size)))
5022 r->nbytes_specified = nbytes;
5023 *var = result = old_ptr;
5025 else if (mmap_alloc (var, nbytes))
5027 bcopy (old_ptr, *var, r->nbytes_specified);
5028 mmap_free_1 (MMAP_REGION (old_ptr));
5029 result = *var;
5030 r = MMAP_REGION (result);
5031 r->nbytes_specified = nbytes;
5033 else
5035 *var = old_ptr;
5036 result = NULL;
5039 else if (room - nbytes >= mmap_page_size)
5041 /* Shrinking by at least a page. Let's give some
5042 memory back to the system.
5044 The extra parens are to make the division happens first,
5045 on positive values, so we know it will round towards
5046 zero. */
5047 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
5048 result = *var;
5049 r->nbytes_specified = nbytes;
5051 else
5053 /* Leave it alone. */
5054 result = *var;
5055 r->nbytes_specified = nbytes;
5059 return result;
5063 /* Free a block of relocatable storage whose data is pointed to by
5064 PTR. Store 0 in *PTR to show there's no block allocated. */
5066 static void
5067 mmap_free (var)
5068 POINTER_TYPE **var;
5070 mmap_init ();
5072 if (*var)
5074 mmap_free_1 (MMAP_REGION (*var));
5075 *var = NULL;
5080 /* Perform necessary intializations for the use of mmap. */
5082 static void
5083 mmap_init ()
5085 #if MAP_ANON == 0
5086 /* The value of mmap_fd is initially 0 in temacs, and -1
5087 in a dumped Emacs. */
5088 if (mmap_fd <= 0)
5090 /* No anonymous mmap -- we need the file descriptor. */
5091 mmap_fd = open ("/dev/zero", O_RDONLY);
5092 if (mmap_fd == -1)
5093 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5095 #endif /* MAP_ANON == 0 */
5097 if (mmap_initialized_p)
5098 return;
5099 mmap_initialized_p = 1;
5101 #if MAP_ANON != 0
5102 mmap_fd = -1;
5103 #endif
5105 mmap_page_size = getpagesize ();
5108 #endif /* USE_MMAP_FOR_BUFFERS */
5112 /***********************************************************************
5113 Buffer-text Allocation
5114 ***********************************************************************/
5116 #ifdef REL_ALLOC
5117 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5118 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5119 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5120 #endif /* REL_ALLOC */
5123 /* Allocate NBYTES bytes for buffer B's text buffer. */
5125 static void
5126 alloc_buffer_text (b, nbytes)
5127 struct buffer *b;
5128 size_t nbytes;
5130 POINTER_TYPE *p;
5132 BLOCK_INPUT;
5133 #if defined USE_MMAP_FOR_BUFFERS
5134 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5135 #elif defined REL_ALLOC
5136 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5137 #else
5138 p = xmalloc (nbytes);
5139 #endif
5141 if (p == NULL)
5143 UNBLOCK_INPUT;
5144 memory_full ();
5147 b->text->beg = (unsigned char *) p;
5148 UNBLOCK_INPUT;
5151 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5152 shrink it. */
5154 void
5155 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5157 POINTER_TYPE *p;
5158 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5159 + delta);
5160 BLOCK_INPUT;
5161 #if defined USE_MMAP_FOR_BUFFERS
5162 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5163 #elif defined REL_ALLOC
5164 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5165 #else
5166 p = xrealloc (b->text->beg, nbytes);
5167 #endif
5169 if (p == NULL)
5171 UNBLOCK_INPUT;
5172 memory_full ();
5175 BUF_BEG_ADDR (b) = (unsigned char *) p;
5176 UNBLOCK_INPUT;
5180 /* Free buffer B's text buffer. */
5182 static void
5183 free_buffer_text (b)
5184 struct buffer *b;
5186 BLOCK_INPUT;
5188 #if defined USE_MMAP_FOR_BUFFERS
5189 mmap_free ((POINTER_TYPE **) &b->text->beg);
5190 #elif defined REL_ALLOC
5191 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5192 #else
5193 xfree (b->text->beg);
5194 #endif
5196 BUF_BEG_ADDR (b) = NULL;
5197 UNBLOCK_INPUT;
5202 /***********************************************************************
5203 Initialization
5204 ***********************************************************************/
5206 void
5207 init_buffer_once ()
5209 int idx;
5211 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5213 /* Make sure all markable slots in buffer_defaults
5214 are initialized reasonably, so mark_buffer won't choke. */
5215 reset_buffer (&buffer_defaults);
5216 reset_buffer_local_variables (&buffer_defaults, 1);
5217 reset_buffer (&buffer_local_symbols);
5218 reset_buffer_local_variables (&buffer_local_symbols, 1);
5219 /* Prevent GC from getting confused. */
5220 buffer_defaults.text = &buffer_defaults.own_text;
5221 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5222 BUF_INTERVALS (&buffer_defaults) = 0;
5223 BUF_INTERVALS (&buffer_local_symbols) = 0;
5224 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5225 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5226 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5227 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5229 /* Set up the default values of various buffer slots. */
5230 /* Must do these before making the first buffer! */
5232 /* real setup is done in bindings.el */
5233 buffer_defaults.mode_line_format = make_pure_c_string ("%-");
5234 buffer_defaults.header_line_format = Qnil;
5235 buffer_defaults.abbrev_mode = Qnil;
5236 buffer_defaults.overwrite_mode = Qnil;
5237 buffer_defaults.case_fold_search = Qt;
5238 buffer_defaults.auto_fill_function = Qnil;
5239 buffer_defaults.selective_display = Qnil;
5240 #ifndef old
5241 buffer_defaults.selective_display_ellipses = Qt;
5242 #endif
5243 buffer_defaults.abbrev_table = Qnil;
5244 buffer_defaults.display_table = Qnil;
5245 buffer_defaults.undo_list = Qnil;
5246 buffer_defaults.mark_active = Qnil;
5247 buffer_defaults.file_format = Qnil;
5248 buffer_defaults.auto_save_file_format = Qt;
5249 buffer_defaults.overlays_before = NULL;
5250 buffer_defaults.overlays_after = NULL;
5251 buffer_defaults.overlay_center = BEG;
5253 XSETFASTINT (buffer_defaults.tab_width, 8);
5254 buffer_defaults.truncate_lines = Qnil;
5255 buffer_defaults.word_wrap = Qnil;
5256 buffer_defaults.ctl_arrow = Qt;
5257 buffer_defaults.direction_reversed = Qnil;
5258 buffer_defaults.cursor_type = Qt;
5259 buffer_defaults.extra_line_spacing = Qnil;
5260 buffer_defaults.cursor_in_non_selected_windows = Qt;
5261 buffer_defaults.owner = Qnil;
5263 #ifdef DOS_NT
5264 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5265 #endif
5266 buffer_defaults.enable_multibyte_characters = Qt;
5267 buffer_defaults.buffer_file_coding_system = Qnil;
5268 XSETFASTINT (buffer_defaults.fill_column, 70);
5269 XSETFASTINT (buffer_defaults.left_margin, 0);
5270 buffer_defaults.cache_long_line_scans = Qnil;
5271 buffer_defaults.file_truename = Qnil;
5272 XSETFASTINT (buffer_defaults.display_count, 0);
5273 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5274 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5275 buffer_defaults.left_fringe_width = Qnil;
5276 buffer_defaults.right_fringe_width = Qnil;
5277 buffer_defaults.fringes_outside_margins = Qnil;
5278 buffer_defaults.scroll_bar_width = Qnil;
5279 buffer_defaults.vertical_scroll_bar_type = Qt;
5280 buffer_defaults.indicate_empty_lines = Qnil;
5281 buffer_defaults.indicate_buffer_boundaries = Qnil;
5282 buffer_defaults.fringe_indicator_alist = Qnil;
5283 buffer_defaults.fringe_cursor_alist = Qnil;
5284 buffer_defaults.scroll_up_aggressively = Qnil;
5285 buffer_defaults.scroll_down_aggressively = Qnil;
5286 buffer_defaults.display_time = Qnil;
5288 /* Assign the local-flags to the slots that have default values.
5289 The local flag is a bit that is used in the buffer
5290 to say that it has its own local value for the slot.
5291 The local flag bits are in the local_var_flags slot of the buffer. */
5293 /* Nothing can work if this isn't true */
5294 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5296 /* 0 means not a lisp var, -1 means always local, else mask */
5297 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5298 XSETINT (buffer_local_flags.filename, -1);
5299 XSETINT (buffer_local_flags.directory, -1);
5300 XSETINT (buffer_local_flags.backed_up, -1);
5301 XSETINT (buffer_local_flags.save_length, -1);
5302 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5303 XSETINT (buffer_local_flags.read_only, -1);
5304 XSETINT (buffer_local_flags.major_mode, -1);
5305 XSETINT (buffer_local_flags.mode_name, -1);
5306 XSETINT (buffer_local_flags.undo_list, -1);
5307 XSETINT (buffer_local_flags.mark_active, -1);
5308 XSETINT (buffer_local_flags.point_before_scroll, -1);
5309 XSETINT (buffer_local_flags.file_truename, -1);
5310 XSETINT (buffer_local_flags.invisibility_spec, -1);
5311 XSETINT (buffer_local_flags.file_format, -1);
5312 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5313 XSETINT (buffer_local_flags.display_count, -1);
5314 XSETINT (buffer_local_flags.display_time, -1);
5315 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5317 idx = 1;
5318 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5319 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5320 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5321 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5322 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5323 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5324 #ifndef old
5325 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5326 #endif
5327 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5328 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5329 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5330 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5331 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5332 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5333 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5334 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5335 #ifdef DOS_NT
5336 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5337 /* Make this one a permanent local. */
5338 buffer_permanent_local_flags[idx++] = 1;
5339 #endif
5340 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5341 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5342 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5343 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5344 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5345 /* Make this one a permanent local. */
5346 buffer_permanent_local_flags[idx++] = 1;
5347 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5348 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5349 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5350 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5351 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5352 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5353 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5354 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5355 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5356 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5357 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5358 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5359 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5360 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5361 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5362 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5363 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5365 /* Need more room? */
5366 if (idx >= MAX_PER_BUFFER_VARS)
5367 abort ();
5368 last_per_buffer_idx = idx;
5370 Vbuffer_alist = Qnil;
5371 current_buffer = 0;
5372 all_buffers = 0;
5374 QSFundamental = make_pure_c_string ("Fundamental");
5376 Qfundamental_mode = intern_c_string ("fundamental-mode");
5377 buffer_defaults.major_mode = Qfundamental_mode;
5379 Qmode_class = intern_c_string ("mode-class");
5381 Qprotected_field = intern_c_string ("protected-field");
5383 Qpermanent_local = intern_c_string ("permanent-local");
5385 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
5386 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5388 Qucs_set_table_for_input = intern_c_string ("ucs-set-table-for-input");
5390 /* super-magic invisible buffer */
5391 Vprin1_to_string_buffer = Fget_buffer_create (make_pure_c_string (" prin1"));
5392 Vbuffer_alist = Qnil;
5394 Fset_buffer (Fget_buffer_create (make_pure_c_string ("*scratch*")));
5396 inhibit_modification_hooks = 0;
5399 void
5400 init_buffer ()
5402 char *pwd;
5403 Lisp_Object temp;
5404 int len;
5406 pthread_cond_init (&buffer_cond, NULL);
5408 #ifdef USE_MMAP_FOR_BUFFERS
5410 /* When using the ralloc implementation based on mmap(2), buffer
5411 text pointers will have been set to null in the dumped Emacs.
5412 Map new memory. */
5413 struct buffer *b;
5415 for (b = all_buffers; b; b = b->next)
5416 if (b->text->beg == NULL)
5417 enlarge_buffer_text (b, 0);
5419 #endif /* USE_MMAP_FOR_BUFFERS */
5421 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5422 if (NILP (buffer_defaults.enable_multibyte_characters))
5423 Fset_buffer_multibyte (Qnil);
5425 pwd = get_current_dir_name ();
5427 if (!pwd)
5428 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5430 /* Maybe this should really use some standard subroutine
5431 whose definition is filename syntax dependent. */
5432 len = strlen (pwd);
5433 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5435 /* Grow buffer to add directory separator and '\0'. */
5436 pwd = (char *) realloc (pwd, len + 2);
5437 if (!pwd)
5438 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5439 pwd[len] = DIRECTORY_SEP;
5440 pwd[len + 1] = '\0';
5443 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5444 if (! NILP (buffer_defaults.enable_multibyte_characters))
5445 /* At this moment, we still don't know how to decode the
5446 directory name. So, we keep the bytes in multibyte form so
5447 that ENCODE_FILE correctly gets the original bytes. */
5448 current_buffer->directory
5449 = string_to_multibyte (current_buffer->directory);
5451 /* Add /: to the front of the name
5452 if it would otherwise be treated as magic. */
5453 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5454 if (! NILP (temp)
5455 /* If the default dir is just /, TEMP is non-nil
5456 because of the ange-ftp completion handler.
5457 However, it is not necessary to turn / into /:/.
5458 So avoid doing that. */
5459 && strcmp ("/", SDATA (current_buffer->directory)))
5460 current_buffer->directory
5461 = concat2 (build_string ("/:"), current_buffer->directory);
5463 temp = get_minibuffer (0);
5464 XBUFFER (temp)->directory = current_buffer->directory;
5466 free (pwd);
5469 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5470 Object stored in the current buffer. address is the address of the slot
5471 in the buffer that is current now. */
5473 /* TYPE is nil for a general Lisp variable.
5474 An integer specifies a type; then only LIsp values
5475 with that type code are allowed (except that nil is allowed too).
5476 LNAME is the LIsp-level variable name.
5477 VNAME is the name of the buffer slot.
5478 DOC is a dummy where you write the doc string as a comment. */
5479 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5480 defvar_per_buffer (lname, vname, type, 0)
5482 static void
5483 defvar_per_buffer (namestring, address, type, doc)
5484 char *namestring;
5485 Lisp_Object *address;
5486 Lisp_Object type;
5487 char *doc;
5489 Lisp_Object sym, val;
5490 int offset;
5492 sym = intern (namestring);
5493 val = allocate_misc ();
5494 offset = (char *)address - (char *)current_buffer;
5496 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5497 XBUFFER_OBJFWD (val)->offset = offset;
5498 XBUFFER_OBJFWD (val)->slottype = type;
5499 SET_SYMBOL_VALUE (sym, val);
5500 PER_BUFFER_SYMBOL (offset) = sym;
5502 if (PER_BUFFER_IDX (offset) == 0)
5503 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5504 slot of buffer_local_flags */
5505 abort ();
5509 /* initialize the buffer routines */
5510 void
5511 syms_of_buffer ()
5513 staticpro (&last_overlay_modification_hooks);
5514 last_overlay_modification_hooks
5515 = Fmake_vector (make_number (10), Qnil);
5517 staticpro (&Vbuffer_defaults);
5518 staticpro (&Vbuffer_local_symbols);
5519 staticpro (&Qfundamental_mode);
5520 staticpro (&Qmode_class);
5521 staticpro (&QSFundamental);
5522 staticpro (&Vbuffer_alist);
5523 staticpro (&Qprotected_field);
5524 staticpro (&Qpermanent_local);
5525 Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
5526 staticpro (&Qpermanent_local_hook);
5527 staticpro (&Qkill_buffer_hook);
5528 Qoverlayp = intern_c_string ("overlayp");
5529 staticpro (&Qoverlayp);
5530 Qevaporate = intern_c_string ("evaporate");
5531 staticpro (&Qevaporate);
5532 Qmodification_hooks = intern_c_string ("modification-hooks");
5533 staticpro (&Qmodification_hooks);
5534 Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
5535 staticpro (&Qinsert_in_front_hooks);
5536 Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
5537 staticpro (&Qinsert_behind_hooks);
5538 Qget_file_buffer = intern_c_string ("get-file-buffer");
5539 staticpro (&Qget_file_buffer);
5540 Qpriority = intern_c_string ("priority");
5541 staticpro (&Qpriority);
5542 Qwindow = intern_c_string ("window");
5543 staticpro (&Qwindow);
5544 Qbefore_string = intern_c_string ("before-string");
5545 staticpro (&Qbefore_string);
5546 Qafter_string = intern_c_string ("after-string");
5547 staticpro (&Qafter_string);
5548 Qfirst_change_hook = intern_c_string ("first-change-hook");
5549 staticpro (&Qfirst_change_hook);
5550 Qbefore_change_functions = intern_c_string ("before-change-functions");
5551 staticpro (&Qbefore_change_functions);
5552 Qafter_change_functions = intern_c_string ("after-change-functions");
5553 staticpro (&Qafter_change_functions);
5554 /* The next one is initialized in init_buffer_once. */
5555 staticpro (&Qucs_set_table_for_input);
5557 Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
5558 staticpro (&Qkill_buffer_query_functions);
5560 Fput (Qprotected_field, Qerror_conditions,
5561 pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
5562 Fput (Qprotected_field, Qerror_message,
5563 make_pure_c_string ("Attempt to modify a protected field"));
5565 /* All these use DEFVAR_LISP_NOPRO because the slots in
5566 buffer_defaults will all be marked via Vbuffer_defaults. */
5568 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5569 &buffer_defaults.mode_line_format,
5570 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5571 This is the same as (default-value 'mode-line-format). */);
5573 DEFVAR_LISP_NOPRO ("default-header-line-format",
5574 &buffer_defaults.header_line_format,
5575 doc: /* Default value of `header-line-format' for buffers that don't override it.
5576 This is the same as (default-value 'header-line-format). */);
5578 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5579 doc: /* Default value of `cursor-type' for buffers that don't override it.
5580 This is the same as (default-value 'cursor-type). */);
5582 DEFVAR_LISP_NOPRO ("default-line-spacing",
5583 &buffer_defaults.extra_line_spacing,
5584 doc: /* Default value of `line-spacing' for buffers that don't override it.
5585 This is the same as (default-value 'line-spacing). */);
5587 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5588 &buffer_defaults.cursor_in_non_selected_windows,
5589 doc: /* Default value of `cursor-in-non-selected-windows'.
5590 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5592 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5593 &buffer_defaults.abbrev_mode,
5594 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5595 This is the same as (default-value 'abbrev-mode). */);
5597 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5598 &buffer_defaults.ctl_arrow,
5599 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5600 This is the same as (default-value 'ctl-arrow). */);
5602 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5603 &buffer_defaults.direction_reversed,
5604 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5605 This is the same as (default-value 'direction-reversed). */);
5607 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5608 &buffer_defaults.enable_multibyte_characters,
5609 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5610 This is the same as (default-value 'enable-multibyte-characters). */);
5612 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5613 &buffer_defaults.buffer_file_coding_system,
5614 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5615 This is the same as (default-value 'buffer-file-coding-system). */);
5617 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5618 &buffer_defaults.truncate_lines,
5619 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5620 This is the same as (default-value 'truncate-lines). */);
5622 DEFVAR_LISP_NOPRO ("default-fill-column",
5623 &buffer_defaults.fill_column,
5624 doc: /* Default value of `fill-column' for buffers that do not override it.
5625 This is the same as (default-value 'fill-column). */);
5627 DEFVAR_LISP_NOPRO ("default-left-margin",
5628 &buffer_defaults.left_margin,
5629 doc: /* Default value of `left-margin' for buffers that do not override it.
5630 This is the same as (default-value 'left-margin). */);
5632 DEFVAR_LISP_NOPRO ("default-tab-width",
5633 &buffer_defaults.tab_width,
5634 doc: /* Default value of `tab-width' for buffers that do not override it.
5635 This is the same as (default-value 'tab-width). */);
5637 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5638 &buffer_defaults.case_fold_search,
5639 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5640 This is the same as (default-value 'case-fold-search). */);
5642 #ifdef DOS_NT
5643 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5644 &buffer_defaults.buffer_file_type,
5645 doc: /* Default file type for buffers that do not override it.
5646 This is the same as (default-value 'buffer-file-type).
5647 The file type is nil for text, t for binary. */);
5648 #endif
5650 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5651 &buffer_defaults.left_margin_cols,
5652 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5653 This is the same as (default-value 'left-margin-width). */);
5655 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5656 &buffer_defaults.right_margin_cols,
5657 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5658 This is the same as (default-value 'right-margin-width). */);
5660 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5661 &buffer_defaults.left_fringe_width,
5662 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5663 This is the same as (default-value 'left-fringe-width). */);
5665 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5666 &buffer_defaults.right_fringe_width,
5667 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5668 This is the same as (default-value 'right-fringe-width). */);
5670 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5671 &buffer_defaults.fringes_outside_margins,
5672 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5673 This is the same as (default-value 'fringes-outside-margins). */);
5675 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5676 &buffer_defaults.scroll_bar_width,
5677 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5678 This is the same as (default-value 'scroll-bar-width). */);
5680 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5681 &buffer_defaults.vertical_scroll_bar_type,
5682 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5683 This is the same as (default-value 'vertical-scroll-bar). */);
5685 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5686 &buffer_defaults.indicate_empty_lines,
5687 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5688 This is the same as (default-value 'indicate-empty-lines). */);
5690 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5691 &buffer_defaults.indicate_buffer_boundaries,
5692 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5693 This is the same as (default-value 'indicate-buffer-boundaries). */);
5695 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5696 &buffer_defaults.fringe_indicator_alist,
5697 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5698 This is the same as (default-value 'fringe-indicator-alist'). */);
5700 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5701 &buffer_defaults.fringe_cursor_alist,
5702 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5703 This is the same as (default-value 'fringe-cursor-alist'). */);
5705 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5706 &buffer_defaults.scroll_up_aggressively,
5707 doc: /* Default value of `scroll-up-aggressively'.
5708 This value applies in buffers that don't have their own local values.
5709 This is the same as (default-value 'scroll-up-aggressively). */);
5711 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5712 &buffer_defaults.scroll_down_aggressively,
5713 doc: /* Default value of `scroll-down-aggressively'.
5714 This value applies in buffers that don't have their own local values.
5715 This is the same as (default-value 'scroll-down-aggressively). */);
5717 DEFVAR_PER_BUFFER ("header-line-format",
5718 &current_buffer->header_line_format,
5719 Qnil,
5720 doc: /* Analogous to `mode-line-format', but controls the header line.
5721 The header line appears, optionally, at the top of a window;
5722 the mode line appears at the bottom. */);
5724 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5725 Qnil,
5726 doc: /* Template for displaying mode line for current buffer.
5727 Each buffer has its own value of this variable.
5728 Value may be nil, a string, a symbol or a list or cons cell.
5729 A value of nil means don't display a mode line.
5730 For a symbol, its value is used (but it is ignored if t or nil).
5731 A string appearing directly as the value of a symbol is processed verbatim
5732 in that the %-constructs below are not recognized.
5733 Note that unless the symbol is marked as a `risky-local-variable', all
5734 properties in any strings, as well as all :eval and :propertize forms
5735 in the value of that symbol will be ignored.
5736 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5737 is used as a mode line element. Be careful--FORM should not load any files,
5738 because that can cause an infinite recursion.
5739 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5740 with the specified properties PROPS applied.
5741 For a list whose car is a symbol, the symbol's value is taken,
5742 and if that is non-nil, the cadr of the list is processed recursively.
5743 Otherwise, the caddr of the list (if there is one) is processed.
5744 For a list whose car is a string or list, each element is processed
5745 recursively and the results are effectively concatenated.
5746 For a list whose car is an integer, the cdr of the list is processed
5747 and padded (if the number is positive) or truncated (if negative)
5748 to the width specified by that number.
5749 A string is printed verbatim in the mode line except for %-constructs:
5750 (%-constructs are allowed when the string is the entire mode-line-format
5751 or when it is found in a cons-cell or a list)
5752 %b -- print buffer name. %f -- print visited file name.
5753 %F -- print frame name.
5754 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5755 %& is like %*, but ignore read-only-ness.
5756 % means buffer is read-only and * means it is modified.
5757 For a modified read-only buffer, %* gives % and %+ gives *.
5758 %s -- print process status. %l -- print the current line number.
5759 %c -- print the current column number (this makes editing slower).
5760 To make the column number update correctly in all cases,
5761 `column-number-mode' must be non-nil.
5762 %i -- print the size of the buffer.
5763 %I -- like %i, but use k, M, G, etc., to abbreviate.
5764 %p -- print percent of buffer above top of window, or Top, Bot or All.
5765 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5766 or print Bottom or All.
5767 %n -- print Narrow if appropriate.
5768 %t -- visited file is text or binary (if OS supports this distinction).
5769 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5770 %Z -- like %z, but including the end-of-line format.
5771 %e -- print error message about full memory.
5772 %@ -- print @ or hyphen. @ means that default-directory is on a
5773 remote machine.
5774 %[ -- print one [ for each recursive editing level. %] similar.
5775 %% -- print %. %- -- print infinitely many dashes.
5776 Decimal digits after the % specify field width to which to pad. */);
5778 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5779 doc: /* *Value of `major-mode' for new buffers. */);
5781 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5782 make_number (Lisp_Symbol),
5783 doc: /* Symbol for current buffer's major mode.
5784 The default value (normally `fundamental-mode') affects new buffers.
5785 A value of nil means to use the current buffer's major mode, provided
5786 it is not marked as "special".
5788 When a mode is used by default, `find-file' switches to it before it
5789 reads the contents into the buffer and before it finishes setting up
5790 the buffer. Thus, the mode and its hooks should not expect certain
5791 variables such as `buffer-read-only' and `buffer-file-coding-system'
5792 to be set up. */);
5794 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5795 Qnil,
5796 doc: /* Pretty name of current buffer's major mode.
5797 Usually a string, but can use any of the constructs for `mode-line-format',
5798 which see.
5799 Format with `format-mode-line' to produce a string value. */);
5801 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5802 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5804 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5805 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5807 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5808 Qnil,
5809 doc: /* *Non-nil if searches and matches should ignore case. */);
5811 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5812 make_number (LISP_INT_TAG),
5813 doc: /* *Column beyond which automatic line-wrapping should happen.
5814 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5816 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5817 make_number (LISP_INT_TAG),
5818 doc: /* *Column for the default `indent-line-function' to indent to.
5819 Linefeed indents to this column in Fundamental mode. */);
5821 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5822 make_number (LISP_INT_TAG),
5823 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5825 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5826 doc: /* *Non-nil means display control chars with uparrow.
5827 A value of nil means use backslash and octal digits.
5828 This variable does not apply to characters whose display is specified
5829 in the current display table (if there is one). */);
5831 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5832 &current_buffer->enable_multibyte_characters,
5833 Qnil,
5834 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5835 Otherwise they are regarded as unibyte. This affects the display,
5836 file I/O and the behavior of various editing commands.
5838 This variable is buffer-local but you cannot set it directly;
5839 use the function `set-buffer-multibyte' to change a buffer's representation.
5840 Changing its default value with `setq-default' is supported.
5841 See also variable `default-enable-multibyte-characters' and Info node
5842 `(elisp)Text Representations'. */);
5843 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
5845 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5846 &current_buffer->buffer_file_coding_system, Qnil,
5847 doc: /* Coding system to be used for encoding the buffer contents on saving.
5848 This variable applies to saving the buffer, and also to `write-region'
5849 and other functions that use `write-region'.
5850 It does not apply to sending output to subprocesses, however.
5852 If this is nil, the buffer is saved without any code conversion
5853 unless some coding system is specified in `file-coding-system-alist'
5854 for the buffer file.
5856 If the text to be saved cannot be encoded as specified by this variable,
5857 an alternative encoding is selected by `select-safe-coding-system', which see.
5859 The variable `coding-system-for-write', if non-nil, overrides this variable.
5861 This variable is never applied to a way of decoding a file while reading it. */);
5863 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5864 Qnil,
5865 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5867 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5868 doc: /* *Non-nil means do not display continuation lines.
5869 Instead, give each line of text just one screen line.
5871 Note that this is overridden by the variable
5872 `truncate-partial-width-windows' if that variable is non-nil
5873 and this buffer is not full-frame width. */);
5875 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5876 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5877 When word-wrapping is on, continuation lines are wrapped at the space
5878 or tab character nearest to the right window edge.
5879 If nil, continuation lines are wrapped at the right screen edge.
5881 This variable has no effect if long lines are truncated (see
5882 `truncate-lines' and `truncate-partial-width-windows'). If you use
5883 word-wrapping, you might want to reduce the value of
5884 `truncate-partial-width-windows', since wrapping can make text readable
5885 in narrower windows. */);
5887 #ifdef DOS_NT
5888 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5889 Qnil,
5890 doc: /* Non-nil if the visited file is a binary file.
5891 This variable is meaningful on MS-DOG and Windows NT.
5892 On those systems, it is automatically local in every buffer.
5893 On other systems, this variable is normally always nil. */);
5894 #endif
5896 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5897 make_number (Lisp_String),
5898 doc: /* Name of default directory of current buffer. Should end with slash.
5899 To interactively change the default directory, use command `cd'. */);
5901 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5902 Qnil,
5903 doc: /* Function called (if non-nil) to perform auto-fill.
5904 It is called after self-inserting any character specified in
5905 the `auto-fill-chars' table.
5906 NOTE: This variable is not a hook;
5907 its value may not be a list of functions. */);
5909 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5910 make_number (Lisp_String),
5911 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5913 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5914 make_number (Lisp_String),
5915 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5916 The truename of a file is calculated by `file-truename'
5917 and then abbreviated with `abbreviate-file-name'. */);
5919 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5920 &current_buffer->auto_save_file_name,
5921 make_number (Lisp_String),
5922 doc: /* Name of file for auto-saving current buffer.
5923 If it is nil, that means don't auto-save this buffer. */);
5925 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5926 doc: /* Non-nil if this buffer is read-only. */);
5928 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5929 doc: /* Non-nil if this buffer's file has been backed up.
5930 Backing up is done before the first time the file is saved. */);
5932 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5933 make_number (LISP_INT_TAG),
5934 doc: /* Length of current buffer when last read in, saved or auto-saved.
5935 0 initially.
5936 -1 means auto-saving turned off until next real save.
5938 If you set this to -2, that means don't turn off auto-saving in this buffer
5939 if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5940 you probably should set this to -2 in that buffer. */);
5942 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5943 Qnil,
5944 doc: /* Non-nil enables selective display.
5945 An integer N as value means display only lines
5946 that start with less than N columns of space.
5947 A value of t means that the character ^M makes itself and
5948 all the rest of the line invisible; also, when saving the buffer
5949 in a file, save the ^M as a newline. */);
5951 #ifndef old
5952 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5953 &current_buffer->selective_display_ellipses,
5954 Qnil,
5955 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5956 #endif
5958 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5959 doc: /* Non-nil if self-insertion should replace existing text.
5960 The value should be one of `overwrite-mode-textual',
5961 `overwrite-mode-binary', or nil.
5962 If it is `overwrite-mode-textual', self-insertion still
5963 inserts at the end of a line, and inserts when point is before a tab,
5964 until the tab is filled in.
5965 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5967 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5968 Qnil,
5969 doc: /* Display table that controls display of the contents of current buffer.
5971 If this variable is nil, the value of `standard-display-table' is used.
5972 Each window can have its own, overriding display table, see
5973 `set-window-display-table' and `window-display-table'.
5975 The display table is a char-table created with `make-display-table'.
5976 A char-table is an array indexed by character codes. Normal array
5977 primitives `aref' and `aset' can be used to access elements of a char-table.
5979 Each of the char-table elements control how to display the corresponding
5980 text character: the element at index C in the table says how to display
5981 the character whose code is C. Each element should be a vector of
5982 characters or nil. The value nil means display the character in the
5983 default fashion; otherwise, the characters from the vector are delivered
5984 to the screen instead of the original character.
5986 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5987 to display a capital Y instead of each X character.
5989 In addition, a char-table has six extra slots to control the display of:
5991 the end of a truncated screen line (extra-slot 0, a single character);
5992 the end of a continued line (extra-slot 1, a single character);
5993 the escape character used to display character codes in octal
5994 (extra-slot 2, a single character);
5995 the character used as an arrow for control characters (extra-slot 3,
5996 a single character);
5997 the decoration indicating the presence of invisible lines (extra-slot 4,
5998 a vector of characters);
5999 the character used to draw the border between side-by-side windows
6000 (extra-slot 5, a single character).
6002 See also the functions `display-table-slot' and `set-display-table-slot'. */);
6004 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
6005 Qnil,
6006 doc: /* *Width of left marginal area for display of a buffer.
6007 A value of nil means no marginal area. */);
6009 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
6010 Qnil,
6011 doc: /* *Width of right marginal area for display of a buffer.
6012 A value of nil means no marginal area. */);
6014 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
6015 Qnil,
6016 doc: /* *Width of this buffer's left fringe (in pixels).
6017 A value of 0 means no left fringe is shown in this buffer's window.
6018 A value of nil means to use the left fringe width from the window's frame. */);
6020 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
6021 Qnil,
6022 doc: /* *Width of this buffer's right fringe (in pixels).
6023 A value of 0 means no right fringe is shown in this buffer's window.
6024 A value of nil means to use the right fringe width from the window's frame. */);
6026 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
6027 Qnil,
6028 doc: /* *Non-nil means to display fringes outside display margins.
6029 A value of nil means to display fringes between margins and buffer text. */);
6031 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
6032 Qnil,
6033 doc: /* *Width of this buffer's scroll bars in pixels.
6034 A value of nil means to use the scroll bar width from the window's frame. */);
6036 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
6037 Qnil,
6038 doc: /* *Position of this buffer's vertical scroll bar.
6039 The value takes effect whenever you tell a window to display this buffer;
6040 for instance, with `set-window-buffer' or when `display-buffer' displays it.
6042 A value of `left' or `right' means put the vertical scroll bar at that side
6043 of the window; a value of nil means don't show any vertical scroll bars.
6044 A value of t (the default) means do whatever the window's frame specifies. */);
6046 DEFVAR_PER_BUFFER ("indicate-empty-lines",
6047 &current_buffer->indicate_empty_lines, Qnil,
6048 doc: /* *Visually indicate empty lines after the buffer end.
6049 If non-nil, a bitmap is displayed in the left fringe of a window on
6050 window-systems. */);
6052 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
6053 &current_buffer->indicate_buffer_boundaries, Qnil,
6054 doc: /* *Visually indicate buffer boundaries and scrolling.
6055 If non-nil, the first and last line of the buffer are marked in the fringe
6056 of a window on window-systems with angle bitmaps, or if the window can be
6057 scrolled, the top and bottom line of the window are marked with up and down
6058 arrow bitmaps.
6060 If value is a symbol `left' or `right', both angle and arrow bitmaps
6061 are displayed in the left or right fringe, resp. Any other value
6062 that doesn't look like an alist means display the angle bitmaps in
6063 the left fringe but no arrows.
6065 You can exercise more precise control by using an alist as the
6066 value. Each alist element (INDICATOR . POSITION) specifies
6067 where to show one of the indicators. INDICATOR is one of `top',
6068 `bottom', `up', `down', or t, which specifies the default position,
6069 and POSITION is one of `left', `right', or nil, meaning do not show
6070 this indicator.
6072 For example, ((top . left) (t . right)) places the top angle bitmap in
6073 left fringe, the bottom angle bitmap in right fringe, and both arrow
6074 bitmaps in right fringe. To show just the angle bitmaps in the left
6075 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6077 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
6078 &current_buffer->fringe_indicator_alist, Qnil,
6079 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
6080 The value is an alist where each element (INDICATOR . BITMAPS)
6081 specifies the fringe bitmaps used to display a specific logical
6082 fringe indicator.
6084 INDICATOR specifies the logical indicator type which is one of the
6085 following symbols: `truncation' , `continuation', `overlay-arrow',
6086 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
6088 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
6089 the actual bitmap shown in the left or right fringe for the logical
6090 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
6091 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
6092 are used only for the `bottom' and `one-line' indicators when the last
6093 \(only) line in has no final newline. BITMAPS may also be a single
6094 symbol which is used in both left and right fringes. */);
6096 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
6097 &current_buffer->fringe_cursor_alist, Qnil,
6098 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
6099 The value is an alist where each element (CURSOR . BITMAP)
6100 specifies the fringe bitmaps used to display a specific logical
6101 cursor type in the fringe.
6103 CURSOR specifies the logical cursor type which is one of the following
6104 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6105 one is used to show a hollow cursor on narrow lines display lines
6106 where the normal hollow cursor will not fit.
6108 BITMAP is the corresponding fringe bitmap shown for the logical
6109 cursor type. */);
6111 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
6112 &current_buffer->scroll_up_aggressively, Qnil,
6113 doc: /* How far to scroll windows upward.
6114 If you move point off the bottom, the window scrolls automatically.
6115 This variable controls how far it scrolls. The value nil, the default,
6116 means scroll to center point. A fraction means scroll to put point
6117 that fraction of the window's height from the bottom of the window.
6118 When the value is 0.0, point goes at the bottom line, which in the
6119 simple case that you moved off with C-f means scrolling just one line.
6120 1.0 means point goes at the top, so that in that simple case, the
6121 window scrolls by a full window height. Meaningful values are
6122 between 0.0 and 1.0, inclusive. */);
6124 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6125 &current_buffer->scroll_down_aggressively, Qnil,
6126 doc: /* How far to scroll windows downward.
6127 If you move point off the top, the window scrolls automatically.
6128 This variable controls how far it scrolls. The value nil, the default,
6129 means scroll to center point. A fraction means scroll to put point
6130 that fraction of the window's height from the top of the window.
6131 When the value is 0.0, point goes at the top line, which in the
6132 simple case that you moved off with C-b means scrolling just one line.
6133 1.0 means point goes at the bottom, so that in that simple case, the
6134 window scrolls by a full window height. Meaningful values are
6135 between 0.0 and 1.0, inclusive. */);
6137 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6138 "Don't ask.");
6141 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6142 doc: /* List of functions to call before each text change.
6143 Two arguments are passed to each function: the positions of
6144 the beginning and end of the range of old text to be changed.
6145 \(For an insertion, the beginning and end are at the same place.)
6146 No information is given about the length of the text after the change.
6148 Buffer changes made while executing the `before-change-functions'
6149 don't call any before-change or after-change functions.
6150 That's because these variables are temporarily set to nil.
6151 As a result, a hook function cannot straightforwardly alter the
6152 value of these variables. See the Emacs Lisp manual for a way of
6153 accomplishing an equivalent result by using other variables.
6155 If an unhandled error happens in running these functions,
6156 the variable's value remains nil. That prevents the error
6157 from happening repeatedly and making Emacs nonfunctional. */);
6158 Vbefore_change_functions = Qnil;
6160 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6161 doc: /* List of functions to call after each text change.
6162 Three arguments are passed to each function: the positions of
6163 the beginning and end of the range of changed text,
6164 and the length in bytes of the pre-change text replaced by that range.
6165 \(For an insertion, the pre-change length is zero;
6166 for a deletion, that length is the number of bytes deleted,
6167 and the post-change beginning and end are at the same place.)
6169 Buffer changes made while executing the `after-change-functions'
6170 don't call any before-change or after-change functions.
6171 That's because these variables are temporarily set to nil.
6172 As a result, a hook function cannot straightforwardly alter the
6173 value of these variables. See the Emacs Lisp manual for a way of
6174 accomplishing an equivalent result by using other variables.
6176 If an unhandled error happens in running these functions,
6177 the variable's value remains nil. That prevents the error
6178 from happening repeatedly and making Emacs nonfunctional. */);
6179 Vafter_change_functions = Qnil;
6181 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6182 doc: /* A list of functions to call before changing a buffer which is unmodified.
6183 The functions are run using the `run-hooks' function. */);
6184 Vfirst_change_hook = Qnil;
6186 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6187 doc: /* List of undo entries in current buffer.
6188 Recent changes come first; older changes follow newer.
6190 An entry (BEG . END) represents an insertion which begins at
6191 position BEG and ends at position END.
6193 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6194 from (abs POSITION). If POSITION is positive, point was at the front
6195 of the text being deleted; if negative, point was at the end.
6197 An entry (t HIGH . LOW) indicates that the buffer previously had
6198 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6199 of the visited file's modification time, as of that time. If the
6200 modification time of the most recent save is different, this entry is
6201 obsolete.
6203 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6204 was modified between BEG and END. PROPERTY is the property name,
6205 and VALUE is the old value.
6207 An entry (apply FUN-NAME . ARGS) means undo the change with
6208 \(apply FUN-NAME ARGS).
6210 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6211 in the active region. BEG and END is the range affected by this entry
6212 and DELTA is the number of bytes added or deleted in that range by
6213 this change.
6215 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6216 was adjusted in position by the offset DISTANCE (an integer).
6218 An entry of the form POSITION indicates that point was at the buffer
6219 location given by the integer. Undoing an entry of this form places
6220 point at POSITION.
6222 Entries with value `nil' mark undo boundaries. The undo command treats
6223 the changes between two undo boundaries as a single step to be undone.
6225 If the value of the variable is t, undo information is not recorded. */);
6227 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6228 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6230 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6231 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6233 Normally, the line-motion functions work by scanning the buffer for
6234 newlines. Columnar operations (like `move-to-column' and
6235 `compute-motion') also work by scanning the buffer, summing character
6236 widths as they go. This works well for ordinary text, but if the
6237 buffer's lines are very long (say, more than 500 characters), these
6238 motion functions will take longer to execute. Emacs may also take
6239 longer to update the display.
6241 If `cache-long-line-scans' is non-nil, these motion functions cache the
6242 results of their scans, and consult the cache to avoid rescanning
6243 regions of the buffer until the text is modified. The caches are most
6244 beneficial when they prevent the most searching---that is, when the
6245 buffer contains long lines and large regions of characters with the
6246 same, fixed screen width.
6248 When `cache-long-line-scans' is non-nil, processing short lines will
6249 become slightly slower (because of the overhead of consulting the
6250 cache), and the caches will use memory roughly proportional to the
6251 number of newlines and characters whose screen width varies.
6253 The caches require no explicit maintenance; their accuracy is
6254 maintained internally by the Emacs primitives. Enabling or disabling
6255 the cache should not affect the behavior of any of the motion
6256 functions; it should only affect their performance. */);
6258 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6259 doc: /* Value of point before the last series of scroll operations, or nil. */);
6261 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6262 doc: /* List of formats to use when saving this buffer.
6263 Formats are defined by `format-alist'. This variable is
6264 set when a file is visited. */);
6266 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6267 &current_buffer->auto_save_file_format, Qnil,
6268 doc: /* *Format in which to write auto-save files.
6269 Should be a list of symbols naming formats that are defined in `format-alist'.
6270 If it is t, which is the default, auto-save files are written in the
6271 same format as a regular save would use. */);
6273 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6274 &current_buffer->invisibility_spec, Qnil,
6275 doc: /* Invisibility spec of this buffer.
6276 The default is t, which means that text is invisible
6277 if it has a non-nil `invisible' property.
6278 If the value is a list, a text character is invisible if its `invisible'
6279 property is an element in that list (or is a list with members in common).
6280 If an element is a cons cell of the form (PROP . ELLIPSIS),
6281 then characters with property value PROP are invisible,
6282 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6284 DEFVAR_PER_BUFFER ("buffer-display-count",
6285 &current_buffer->display_count, Qnil,
6286 doc: /* A number incremented each time this buffer is displayed in a window.
6287 The function `set-window-buffer' increments it. */);
6289 DEFVAR_PER_BUFFER ("buffer-display-time",
6290 &current_buffer->display_time, Qnil,
6291 doc: /* Time stamp updated each time this buffer is displayed in a window.
6292 The function `set-window-buffer' updates this variable
6293 to the value obtained by calling `current-time'.
6294 If the buffer has never been shown in a window, the value is nil. */);
6296 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6297 doc: /* */);
6298 Vtransient_mark_mode = Qnil;
6299 /* The docstring is in simple.el. If we put it here, it would be
6300 overwritten when transient-mark-mode is defined using
6301 define-minor-mode. */
6303 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6304 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6305 If the value is t, disregard `buffer-read-only' and all `read-only'
6306 text properties. If the value is a list, disregard `buffer-read-only'
6307 and disregard a `read-only' text property if the property value
6308 is a member of the list. */);
6309 Vinhibit_read_only = Qnil;
6311 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6312 doc: /* Cursor to use when this buffer is in the selected window.
6313 Values are interpreted as follows:
6315 t use the cursor specified for the frame
6316 nil don't display a cursor
6317 box display a filled box cursor
6318 hollow display a hollow box cursor
6319 bar display a vertical bar cursor with default width
6320 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6321 hbar display a horizontal bar cursor with default height
6322 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6323 ANYTHING ELSE display a hollow box cursor
6325 When the buffer is displayed in a non-selected window, the
6326 cursor's appearance is instead controlled by the variable
6327 `cursor-in-non-selected-windows'. */);
6329 DEFVAR_PER_BUFFER ("line-spacing",
6330 &current_buffer->extra_line_spacing, Qnil,
6331 doc: /* Additional space to put between lines when displaying a buffer.
6332 The space is measured in pixels, and put below lines on graphic displays,
6333 see `display-graphic-p'.
6334 If value is a floating point number, it specifies the spacing relative
6335 to the default frame line height. A value of nil means add no extra space. */);
6337 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6338 &current_buffer->cursor_in_non_selected_windows, Qnil,
6339 doc: /* *Cursor type to display in non-selected windows.
6340 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6342 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6343 doc: /* List of functions called with no args to query before killing a buffer.
6344 The buffer being killed will be current while the functions are running.
6345 If any of them returns nil, the buffer is not killed. */);
6346 Vkill_buffer_query_functions = Qnil;
6348 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6349 doc: /* Normal hook run before changing the major mode of a buffer.
6350 The function `kill-all-local-variables' runs this before doing anything else. */);
6351 Vchange_major_mode_hook = Qnil;
6352 Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
6353 staticpro (&Qchange_major_mode_hook);
6355 defsubr (&Sbuffer_live_p);
6356 defsubr (&Sbuffer_list);
6357 defsubr (&Sget_buffer);
6358 defsubr (&Sget_file_buffer);
6359 defsubr (&Sget_buffer_create);
6360 defsubr (&Smake_indirect_buffer);
6361 defsubr (&Sgenerate_new_buffer_name);
6362 defsubr (&Sbuffer_name);
6363 /*defsubr (&Sbuffer_number);*/
6364 defsubr (&Sbuffer_file_name);
6365 defsubr (&Sbuffer_base_buffer);
6366 defsubr (&Sbuffer_local_value);
6367 defsubr (&Sbuffer_local_variables);
6368 defsubr (&Sbuffer_modified_p);
6369 defsubr (&Sset_buffer_modified_p);
6370 defsubr (&Sbuffer_modified_tick);
6371 defsubr (&Sbuffer_chars_modified_tick);
6372 defsubr (&Srename_buffer);
6373 defsubr (&Sother_buffer);
6374 defsubr (&Sbuffer_enable_undo);
6375 defsubr (&Skill_buffer);
6376 defsubr (&Sset_buffer_major_mode);
6377 defsubr (&Sswitch_to_buffer);
6378 defsubr (&Scurrent_buffer);
6379 defsubr (&Sset_buffer);
6380 defsubr (&Sbarf_if_buffer_read_only);
6381 defsubr (&Sbury_buffer);
6382 defsubr (&Serase_buffer);
6383 defsubr (&Sbuffer_swap_text);
6384 defsubr (&Sset_buffer_multibyte);
6385 defsubr (&Skill_all_local_variables);
6387 defsubr (&Soverlayp);
6388 defsubr (&Smake_overlay);
6389 defsubr (&Sdelete_overlay);
6390 defsubr (&Smove_overlay);
6391 defsubr (&Soverlay_start);
6392 defsubr (&Soverlay_end);
6393 defsubr (&Soverlay_buffer);
6394 defsubr (&Soverlay_properties);
6395 defsubr (&Soverlays_at);
6396 defsubr (&Soverlays_in);
6397 defsubr (&Snext_overlay_change);
6398 defsubr (&Sprevious_overlay_change);
6399 defsubr (&Soverlay_recenter);
6400 defsubr (&Soverlay_lists);
6401 defsubr (&Soverlay_get);
6402 defsubr (&Soverlay_put);
6403 defsubr (&Srestore_buffer_modified_p);
6406 void
6407 keys_of_buffer ()
6409 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6410 initial_define_key (control_x_map, 'k', "kill-buffer");
6412 /* This must not be in syms_of_buffer, because Qdisabled is not
6413 initialized when that function gets called. */
6414 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
6417 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6418 (do not change this comment) */