* lisp/version.el (emacs-version): Change '*Step' to 'NS' for consistency with other...
[emacs/old-mirror.git] / src / buffer.c
blob3e25c28f5b4652c03c7cd662633a804b44179d97
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 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>
30 #ifndef USE_CRT_DLL
31 extern int errno;
32 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include "lisp.h"
40 #include "intervals.h"
41 #include "window.h"
42 #include "commands.h"
43 #include "buffer.h"
44 #include "character.h"
45 #include "region-cache.h"
46 #include "indent.h"
47 #include "blockinput.h"
48 #include "keyboard.h"
49 #include "keymap.h"
50 #include "frame.h"
52 struct buffer *current_buffer; /* the current buffer */
54 /* First buffer in chain of all buffers (in reverse order of creation).
55 Threaded through ->next. */
57 struct buffer *all_buffers;
59 /* This structure holds the default values of the buffer-local variables
60 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
61 The default value occupies the same slot in this structure
62 as an individual buffer's value occupies in that buffer.
63 Setting the default value also goes through the alist of buffers
64 and stores into each buffer that does not say it has a local value. */
66 DECL_ALIGN (struct buffer, buffer_defaults);
68 /* A Lisp_Object pointer to the above, used for staticpro */
70 static Lisp_Object Vbuffer_defaults;
72 /* This structure marks which slots in a buffer have corresponding
73 default values in buffer_defaults.
74 Each such slot has a nonzero value in this structure.
75 The value has only one nonzero bit.
77 When a buffer has its own local value for a slot,
78 the entry for that slot (found in the same slot in this structure)
79 is turned on in the buffer's local_flags array.
81 If a slot in this structure is -1, then even though there may
82 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
83 and the corresponding slot in buffer_defaults is not used.
85 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
86 but there is a default value which is copied into each buffer.
88 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
89 zero, that is a bug */
91 struct buffer buffer_local_flags;
93 /* This structure holds the names of symbols whose values may be
94 buffer-local. It is indexed and accessed in the same way as the above. */
96 DECL_ALIGN (struct buffer, buffer_local_symbols);
98 /* A Lisp_Object pointer to the above, used for staticpro */
99 static Lisp_Object Vbuffer_local_symbols;
101 /* Flags indicating which built-in buffer-local variables
102 are permanent locals. */
103 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
105 /* Number of per-buffer variables used. */
107 int last_per_buffer_idx;
109 EXFUN (Fset_buffer, 1);
110 void set_buffer_internal P_ ((struct buffer *b));
111 void set_buffer_internal_1 P_ ((struct buffer *b));
112 static void call_overlay_mod_hooks P_ ((Lisp_Object list, Lisp_Object overlay,
113 int after, Lisp_Object arg1,
114 Lisp_Object arg2, Lisp_Object arg3));
115 static void swap_out_buffer_local_variables P_ ((struct buffer *b));
116 static void reset_buffer_local_variables P_ ((struct buffer *b, int permanent_too));
118 /* Alist of all buffer names vs the buffers. */
119 /* This used to be a variable, but is no longer,
120 to prevent lossage due to user rplac'ing this alist or its elements. */
121 Lisp_Object Vbuffer_alist;
123 /* Functions to call before and after each text change. */
124 Lisp_Object Vbefore_change_functions;
125 Lisp_Object Vafter_change_functions;
127 Lisp_Object Vtransient_mark_mode;
129 /* t means ignore all read-only text properties.
130 A list means ignore such a property if its value is a member of the list.
131 Any non-nil value means ignore buffer-read-only. */
132 Lisp_Object Vinhibit_read_only;
134 /* List of functions to call that can query about killing a buffer.
135 If any of these functions returns nil, we don't kill it. */
136 Lisp_Object Vkill_buffer_query_functions;
137 Lisp_Object Qkill_buffer_query_functions;
139 /* Hook run before changing a major mode. */
140 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
142 /* List of functions to call before changing an unmodified buffer. */
143 Lisp_Object Vfirst_change_hook;
145 Lisp_Object Qfirst_change_hook;
146 Lisp_Object Qbefore_change_functions;
147 Lisp_Object Qafter_change_functions;
148 Lisp_Object Qucs_set_table_for_input;
150 /* If nonzero, all modification hooks are suppressed. */
151 int inhibit_modification_hooks;
153 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
154 Lisp_Object Qpermanent_local_hook;
156 Lisp_Object Qprotected_field;
158 Lisp_Object QSFundamental; /* A string "Fundamental" */
160 Lisp_Object Qkill_buffer_hook;
162 Lisp_Object Qget_file_buffer;
164 Lisp_Object Qoverlayp;
166 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
168 Lisp_Object Qmodification_hooks;
169 Lisp_Object Qinsert_in_front_hooks;
170 Lisp_Object Qinsert_behind_hooks;
172 static void alloc_buffer_text P_ ((struct buffer *, size_t));
173 static void free_buffer_text P_ ((struct buffer *b));
174 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
175 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
176 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
178 extern char * emacs_strerror P_ ((int));
180 /* For debugging; temporary. See set_buffer_internal. */
181 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
183 void
184 nsberror (spec)
185 Lisp_Object spec;
187 if (STRINGP (spec))
188 error ("No buffer named %s", SDATA (spec));
189 error ("Invalid buffer argument");
192 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
193 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
194 Value is nil if OBJECT is not a buffer or if it has been killed. */)
195 (object)
196 Lisp_Object object;
198 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
199 ? Qt : Qnil);
202 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
203 doc: /* Return a list of all existing live buffers.
204 If the optional arg FRAME is a frame, we return the buffer list
205 in the proper order for that frame: the buffers in FRAME's `buffer-list'
206 frame parameter come first, followed by the rest of the buffers. */)
207 (frame)
208 Lisp_Object frame;
210 Lisp_Object general;
211 general = Fmapcar (Qcdr, Vbuffer_alist);
213 if (FRAMEP (frame))
215 Lisp_Object framelist, prevlist, tail;
216 Lisp_Object args[3];
218 CHECK_FRAME (frame);
220 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
221 prevlist = Fnreverse (Fcopy_sequence (XFRAME (frame)->buried_buffer_list));
223 /* Remove from GENERAL any buffer that duplicates one in
224 FRAMELIST or PREVLIST. */
225 tail = framelist;
226 while (CONSP (tail))
228 general = Fdelq (XCAR (tail), general);
229 tail = XCDR (tail);
231 tail = prevlist;
232 while (CONSP (tail))
234 general = Fdelq (XCAR (tail), general);
235 tail = XCDR (tail);
238 args[0] = framelist;
239 args[1] = general;
240 args[2] = prevlist;
241 return Fnconc (3, args);
244 return general;
247 /* Like Fassoc, but use Fstring_equal to compare
248 (which ignores text properties),
249 and don't ever QUIT. */
251 static Lisp_Object
252 assoc_ignore_text_properties (key, list)
253 register Lisp_Object key;
254 Lisp_Object list;
256 register Lisp_Object tail;
257 for (tail = list; CONSP (tail); tail = XCDR (tail))
259 register Lisp_Object elt, tem;
260 elt = XCAR (tail);
261 tem = Fstring_equal (Fcar (elt), key);
262 if (!NILP (tem))
263 return elt;
265 return Qnil;
268 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
269 doc: /* Return the buffer named BUFFER-OR-NAME.
270 BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME
271 is a string and there is no buffer with that name, return nil. If
272 BUFFER-OR-NAME is a buffer, return it as given. */)
273 (buffer_or_name)
274 register Lisp_Object buffer_or_name;
276 if (BUFFERP (buffer_or_name))
277 return buffer_or_name;
278 CHECK_STRING (buffer_or_name);
280 return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
283 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
284 doc: /* Return the buffer visiting file FILENAME (a string).
285 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
286 If there is no such live buffer, return nil.
287 See also `find-buffer-visiting'. */)
288 (filename)
289 register Lisp_Object filename;
291 register Lisp_Object tail, buf, tem;
292 Lisp_Object handler;
294 CHECK_STRING (filename);
295 filename = Fexpand_file_name (filename, Qnil);
297 /* If the file name has special constructs in it,
298 call the corresponding file handler. */
299 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
300 if (!NILP (handler))
301 return call2 (handler, Qget_file_buffer, filename);
303 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
305 buf = Fcdr (XCAR (tail));
306 if (!BUFFERP (buf)) continue;
307 if (!STRINGP (XBUFFER (buf)->filename)) continue;
308 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
309 if (!NILP (tem))
310 return buf;
312 return Qnil;
315 Lisp_Object
316 get_truename_buffer (filename)
317 register Lisp_Object filename;
319 register Lisp_Object tail, buf, tem;
321 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
323 buf = Fcdr (XCAR (tail));
324 if (!BUFFERP (buf)) continue;
325 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
326 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
327 if (!NILP (tem))
328 return buf;
330 return Qnil;
333 /* Incremented for each buffer created, to assign the buffer number. */
334 int buffer_count;
336 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
337 doc: /* Return the buffer named NAME, or create such a buffer and return it.
338 A new buffer is created if there is no live buffer named NAME.
339 If NAME starts with a space, the new buffer does not keep undo information.
340 If NAME is a buffer instead of a string, then it is the value returned.
341 The value is never nil. */)
342 (name)
343 register Lisp_Object name;
345 register Lisp_Object buf;
346 register struct buffer *b;
348 buf = Fget_buffer (name);
349 if (!NILP (buf))
350 return buf;
352 if (SCHARS (name) == 0)
353 error ("Empty string for buffer name is not allowed");
355 b = allocate_buffer ();
357 /* An ordinary buffer uses its own struct buffer_text. */
358 b->text = &b->own_text;
359 b->base_buffer = 0;
361 BUF_GAP_SIZE (b) = 20;
362 BLOCK_INPUT;
363 /* We allocate extra 1-byte at the tail and keep it always '\0' for
364 anchoring a search. */
365 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
366 UNBLOCK_INPUT;
367 if (! BUF_BEG_ADDR (b))
368 buffer_memory_full ();
370 BUF_PT (b) = BEG;
371 BUF_GPT (b) = BEG;
372 BUF_BEGV (b) = BEG;
373 BUF_ZV (b) = BEG;
374 BUF_Z (b) = BEG;
375 BUF_PT_BYTE (b) = BEG_BYTE;
376 BUF_GPT_BYTE (b) = BEG_BYTE;
377 BUF_BEGV_BYTE (b) = BEG_BYTE;
378 BUF_ZV_BYTE (b) = BEG_BYTE;
379 BUF_Z_BYTE (b) = BEG_BYTE;
380 BUF_MODIFF (b) = 1;
381 BUF_CHARS_MODIFF (b) = 1;
382 BUF_OVERLAY_MODIFF (b) = 1;
383 BUF_SAVE_MODIFF (b) = 1;
384 BUF_INTERVALS (b) = 0;
385 BUF_UNCHANGED_MODIFIED (b) = 1;
386 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
387 BUF_END_UNCHANGED (b) = 0;
388 BUF_BEG_UNCHANGED (b) = 0;
389 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
391 b->newline_cache = 0;
392 b->width_run_cache = 0;
393 b->width_table = Qnil;
394 b->prevent_redisplay_optimizations_p = 1;
396 /* Put this on the chain of all buffers including killed ones. */
397 b->next = all_buffers;
398 all_buffers = b;
400 /* An ordinary buffer normally doesn't need markers
401 to handle BEGV and ZV. */
402 b->pt_marker = Qnil;
403 b->begv_marker = Qnil;
404 b->zv_marker = Qnil;
406 name = Fcopy_sequence (name);
407 STRING_SET_INTERVALS (name, NULL_INTERVAL);
408 b->name = name;
410 b->undo_list = (SREF (name, 0) != ' ') ? Qnil : Qt;
412 reset_buffer (b);
413 reset_buffer_local_variables (b, 1);
415 b->mark = Fmake_marker ();
416 BUF_MARKERS (b) = NULL;
417 b->name = name;
419 /* Put this in the alist of all live buffers. */
420 XSETBUFFER (buf, b);
421 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
423 /* An error in calling the function here (should someone redefine it)
424 can lead to infinite regress until you run out of stack. rms
425 says that's not worth protecting against. */
426 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
427 /* buf is on buffer-alist, so no gcpro. */
428 call1 (Qucs_set_table_for_input, buf);
430 return buf;
434 /* Return a list of overlays which is a copy of the overlay list
435 LIST, but for buffer B. */
437 static struct Lisp_Overlay *
438 copy_overlays (b, list)
439 struct buffer *b;
440 struct Lisp_Overlay *list;
442 Lisp_Object buffer;
443 struct Lisp_Overlay *result = NULL, *tail = NULL;
445 XSETBUFFER (buffer, b);
447 for (; list; list = list->next)
449 Lisp_Object overlay, start, end, old_overlay;
450 EMACS_INT charpos;
452 XSETMISC (old_overlay, list);
453 charpos = marker_position (OVERLAY_START (old_overlay));
454 start = Fmake_marker ();
455 Fset_marker (start, make_number (charpos), buffer);
456 XMARKER (start)->insertion_type
457 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
459 charpos = marker_position (OVERLAY_END (old_overlay));
460 end = Fmake_marker ();
461 Fset_marker (end, make_number (charpos), buffer);
462 XMARKER (end)->insertion_type
463 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
465 overlay = allocate_misc ();
466 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
467 OVERLAY_START (overlay) = start;
468 OVERLAY_END (overlay) = end;
469 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
470 XOVERLAY (overlay)->next = NULL;
472 if (tail)
473 tail = tail->next = XOVERLAY (overlay);
474 else
475 result = tail = XOVERLAY (overlay);
478 return result;
482 /* Clone per-buffer values of buffer FROM.
484 Buffer TO gets the same per-buffer values as FROM, with the
485 following exceptions: (1) TO's name is left untouched, (2) markers
486 are copied and made to refer to TO, and (3) overlay lists are
487 copied. */
489 static void
490 clone_per_buffer_values (from, to)
491 struct buffer *from, *to;
493 Lisp_Object to_buffer;
494 int offset;
496 XSETBUFFER (to_buffer, to);
498 /* buffer-local Lisp variables start at `undo_list',
499 tho only the ones from `name' on are GC'd normally. */
500 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
501 offset < sizeof *to;
502 offset += sizeof (Lisp_Object))
504 Lisp_Object obj;
506 /* Don't touch the `name' which should be unique for every buffer. */
507 if (offset == PER_BUFFER_VAR_OFFSET (name))
508 continue;
510 obj = PER_BUFFER_VALUE (from, offset);
511 if (MARKERP (obj))
513 struct Lisp_Marker *m = XMARKER (obj);
514 obj = Fmake_marker ();
515 XMARKER (obj)->insertion_type = m->insertion_type;
516 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
519 PER_BUFFER_VALUE (to, offset) = obj;
522 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
524 to->overlays_before = copy_overlays (to, from->overlays_before);
525 to->overlays_after = copy_overlays (to, from->overlays_after);
527 /* Get (a copy of) the alist of Lisp-level local variables of FROM
528 and install that in TO. */
529 to->local_var_alist = buffer_lisp_local_variables (from);
532 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
533 2, 3,
534 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
535 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
536 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
537 NAME should be a string which is not the name of an existing buffer.
538 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
539 such as major and minor modes, in the indirect buffer.
540 CLONE nil means the indirect buffer's state is reset to default values. */)
541 (base_buffer, name, clone)
542 Lisp_Object base_buffer, name, clone;
544 Lisp_Object buf, tem;
545 struct buffer *b;
547 CHECK_STRING (name);
548 buf = Fget_buffer (name);
549 if (!NILP (buf))
550 error ("Buffer name `%s' is in use", SDATA (name));
552 tem = base_buffer;
553 base_buffer = Fget_buffer (base_buffer);
554 if (NILP (base_buffer))
555 error ("No such buffer: `%s'", SDATA (tem));
556 if (NILP (XBUFFER (base_buffer)->name))
557 error ("Base buffer has been killed");
559 if (SCHARS (name) == 0)
560 error ("Empty string for buffer name is not allowed");
562 b = allocate_buffer ();
564 b->base_buffer = (XBUFFER (base_buffer)->base_buffer
565 ? XBUFFER (base_buffer)->base_buffer
566 : XBUFFER (base_buffer));
568 /* Use the base buffer's text object. */
569 b->text = b->base_buffer->text;
571 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
572 BUF_ZV (b) = BUF_ZV (b->base_buffer);
573 BUF_PT (b) = BUF_PT (b->base_buffer);
574 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
575 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
576 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
578 b->newline_cache = 0;
579 b->width_run_cache = 0;
580 b->width_table = Qnil;
582 /* Put this on the chain of all buffers including killed ones. */
583 b->next = all_buffers;
584 all_buffers = b;
586 name = Fcopy_sequence (name);
587 STRING_SET_INTERVALS (name, NULL_INTERVAL);
588 b->name = name;
590 reset_buffer (b);
591 reset_buffer_local_variables (b, 1);
593 /* Put this in the alist of all live buffers. */
594 XSETBUFFER (buf, b);
595 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
597 b->mark = Fmake_marker ();
598 b->name = name;
600 /* The multibyte status belongs to the base buffer. */
601 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
603 /* Make sure the base buffer has markers for its narrowing. */
604 if (NILP (b->base_buffer->pt_marker))
606 b->base_buffer->pt_marker = Fmake_marker ();
607 set_marker_both (b->base_buffer->pt_marker, base_buffer,
608 BUF_PT (b->base_buffer),
609 BUF_PT_BYTE (b->base_buffer));
611 if (NILP (b->base_buffer->begv_marker))
613 b->base_buffer->begv_marker = Fmake_marker ();
614 set_marker_both (b->base_buffer->begv_marker, base_buffer,
615 BUF_BEGV (b->base_buffer),
616 BUF_BEGV_BYTE (b->base_buffer));
618 if (NILP (b->base_buffer->zv_marker))
620 b->base_buffer->zv_marker = Fmake_marker ();
621 set_marker_both (b->base_buffer->zv_marker, base_buffer,
622 BUF_ZV (b->base_buffer),
623 BUF_ZV_BYTE (b->base_buffer));
624 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
627 if (NILP (clone))
629 /* Give the indirect buffer markers for its narrowing. */
630 b->pt_marker = Fmake_marker ();
631 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
632 b->begv_marker = Fmake_marker ();
633 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
634 b->zv_marker = Fmake_marker ();
635 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
636 XMARKER (b->zv_marker)->insertion_type = 1;
638 else
640 struct buffer *old_b = current_buffer;
642 clone_per_buffer_values (b->base_buffer, b);
643 b->filename = Qnil;
644 b->file_truename = Qnil;
645 b->display_count = make_number (0);
646 b->backed_up = Qnil;
647 b->auto_save_file_name = Qnil;
648 set_buffer_internal_1 (b);
649 Fset (intern ("buffer-save-without-query"), Qnil);
650 Fset (intern ("buffer-file-number"), Qnil);
651 Fset (intern ("buffer-stale-function"), Qnil);
652 set_buffer_internal_1 (old_b);
655 return buf;
658 void
659 delete_all_overlays (b)
660 struct buffer *b;
662 Lisp_Object overlay;
664 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
665 have to empty the list, otherwise we end up with overlays that
666 think they belong to this buffer while the buffer doesn't know about
667 them any more. */
668 while (b->overlays_before)
670 XSETMISC (overlay, b->overlays_before);
671 Fdelete_overlay (overlay);
673 while (b->overlays_after)
675 XSETMISC (overlay, b->overlays_after);
676 Fdelete_overlay (overlay);
678 eassert (b->overlays_before == NULL);
679 eassert (b->overlays_after == NULL);
682 /* Reinitialize everything about a buffer except its name and contents
683 and local variables.
684 If called on an already-initialized buffer, the list of overlays
685 should be deleted before calling this function, otherwise we end up
686 with overlays that claim to belong to the buffer but the buffer
687 claims it doesn't belong to it. */
689 void
690 reset_buffer (b)
691 register struct buffer *b;
693 b->filename = Qnil;
694 b->file_truename = Qnil;
695 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
696 b->modtime = 0;
697 XSETFASTINT (b->save_length, 0);
698 b->last_window_start = 1;
699 /* It is more conservative to start out "changed" than "unchanged". */
700 b->clip_changed = 0;
701 b->prevent_redisplay_optimizations_p = 1;
702 b->backed_up = Qnil;
703 b->auto_save_modified = 0;
704 b->auto_save_failure_time = -1;
705 b->auto_save_file_name = Qnil;
706 b->read_only = Qnil;
707 b->overlays_before = NULL;
708 b->overlays_after = NULL;
709 b->overlay_center = BEG;
710 b->mark_active = Qnil;
711 b->point_before_scroll = Qnil;
712 b->file_format = Qnil;
713 b->auto_save_file_format = Qt;
714 b->last_selected_window = Qnil;
715 XSETINT (b->display_count, 0);
716 b->display_time = Qnil;
717 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
718 b->cursor_type = buffer_defaults.cursor_type;
719 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
721 b->display_error_modiff = 0;
724 /* Reset buffer B's local variables info.
725 Don't use this on a buffer that has already been in use;
726 it does not treat permanent locals consistently.
727 Instead, use Fkill_all_local_variables.
729 If PERMANENT_TOO is 1, then we reset permanent
730 buffer-local variables. If PERMANENT_TOO is 0,
731 we preserve those. */
733 static void
734 reset_buffer_local_variables (b, permanent_too)
735 register struct buffer *b;
736 int permanent_too;
738 register int offset;
739 int i;
741 /* Reset the major mode to Fundamental, together with all the
742 things that depend on the major mode.
743 default-major-mode is handled at a higher level.
744 We ignore it here. */
745 b->major_mode = Qfundamental_mode;
746 b->keymap = Qnil;
747 b->mode_name = QSFundamental;
748 b->minor_modes = Qnil;
750 /* If the standard case table has been altered and invalidated,
751 fix up its insides first. */
752 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
753 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
754 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
755 Fset_standard_case_table (Vascii_downcase_table);
757 b->downcase_table = Vascii_downcase_table;
758 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
759 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
760 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
761 b->invisibility_spec = Qt;
762 #ifndef DOS_NT
763 b->buffer_file_type = Qnil;
764 #endif
766 /* Reset all (or most) per-buffer variables to their defaults. */
767 if (permanent_too)
768 b->local_var_alist = Qnil;
769 else
771 Lisp_Object tmp, prop, last = Qnil;
772 for (tmp = b->local_var_alist; CONSP (tmp); tmp = XCDR (tmp))
773 if (CONSP (XCAR (tmp))
774 && SYMBOLP (XCAR (XCAR (tmp)))
775 && !NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local)))
777 /* If permanent-local, keep it. */
778 last = tmp;
779 if (EQ (prop, Qpermanent_local_hook))
781 /* This is a partially permanent hook variable.
782 Preserve only the elements that want to be preserved. */
783 Lisp_Object list, newlist;
784 list = XCDR (XCAR (tmp));
785 if (!CONSP (list))
786 newlist = list;
787 else
788 for (newlist = Qnil; CONSP (list); list = XCDR (list))
790 Lisp_Object elt = XCAR (list);
791 /* Preserve element ELT if it's t,
792 if it is a function with a `permanent-local-hook' property,
793 or if it's not a symbol. */
794 if (! SYMBOLP (elt)
795 || EQ (elt, Qt)
796 || !NILP (Fget (elt, Qpermanent_local_hook)))
797 newlist = Fcons (elt, newlist);
799 XSETCDR (XCAR (tmp), Fnreverse (newlist));
802 /* Delete this local variable. */
803 else if (NILP (last))
804 b->local_var_alist = XCDR (tmp);
805 else
806 XSETCDR (last, XCDR (tmp));
809 for (i = 0; i < last_per_buffer_idx; ++i)
810 if (permanent_too || buffer_permanent_local_flags[i] == 0)
811 SET_PER_BUFFER_VALUE_P (b, i, 0);
813 /* For each slot that has a default value,
814 copy that into the slot. */
816 /* buffer-local Lisp variables start at `undo_list',
817 tho only the ones from `name' on are GC'd normally. */
818 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
819 offset < sizeof *b;
820 offset += sizeof (Lisp_Object))
822 int idx = PER_BUFFER_IDX (offset);
823 if ((idx > 0
824 && (permanent_too
825 || buffer_permanent_local_flags[idx] == 0))
826 /* Is -2 used anywhere? */
827 || idx == -2)
828 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
832 /* We split this away from generate-new-buffer, because rename-buffer
833 and set-visited-file-name ought to be able to use this to really
834 rename the buffer properly. */
836 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
837 1, 2, 0,
838 doc: /* Return a string that is the name of no existing buffer based on NAME.
839 If there is no live buffer named NAME, then return NAME.
840 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
841 \(starting at 2) until an unused name is found, and then return that name.
842 Optional second argument IGNORE specifies a name that is okay to use (if
843 it is in the sequence to be tried) even if a buffer with that name exists. */)
844 (name, ignore)
845 register Lisp_Object name, ignore;
847 register Lisp_Object gentemp, tem;
848 int count;
849 char number[10];
851 CHECK_STRING (name);
853 tem = Fstring_equal (name, ignore);
854 if (!NILP (tem))
855 return name;
856 tem = Fget_buffer (name);
857 if (NILP (tem))
858 return name;
860 count = 1;
861 while (1)
863 sprintf (number, "<%d>", ++count);
864 gentemp = concat2 (name, build_string (number));
865 tem = Fstring_equal (gentemp, ignore);
866 if (!NILP (tem))
867 return gentemp;
868 tem = Fget_buffer (gentemp);
869 if (NILP (tem))
870 return gentemp;
875 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
876 doc: /* Return the name of BUFFER, as a string.
877 With no argument or nil as argument, return the name of the current buffer. */)
878 (buffer)
879 register Lisp_Object buffer;
881 if (NILP (buffer))
882 return current_buffer->name;
883 CHECK_BUFFER (buffer);
884 return XBUFFER (buffer)->name;
887 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
888 doc: /* Return name of file BUFFER is visiting, or nil if none.
889 No argument or nil as argument means use the current buffer. */)
890 (buffer)
891 register Lisp_Object buffer;
893 if (NILP (buffer))
894 return current_buffer->filename;
895 CHECK_BUFFER (buffer);
896 return XBUFFER (buffer)->filename;
899 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
900 0, 1, 0,
901 doc: /* Return the base buffer of indirect buffer BUFFER.
902 If BUFFER is not indirect, return nil.
903 BUFFER defaults to the current buffer. */)
904 (buffer)
905 register Lisp_Object buffer;
907 struct buffer *base;
908 Lisp_Object base_buffer;
910 if (NILP (buffer))
911 base = current_buffer->base_buffer;
912 else
914 CHECK_BUFFER (buffer);
915 base = XBUFFER (buffer)->base_buffer;
918 if (! base)
919 return Qnil;
920 XSETBUFFER (base_buffer, base);
921 return base_buffer;
924 DEFUN ("buffer-local-value", Fbuffer_local_value,
925 Sbuffer_local_value, 2, 2, 0,
926 doc: /* Return the value of VARIABLE in BUFFER.
927 If VARIABLE does not have a buffer-local binding in BUFFER, the value
928 is the default binding of the variable. */)
929 (variable, buffer)
930 register Lisp_Object variable;
931 register Lisp_Object buffer;
933 register struct buffer *buf;
934 register Lisp_Object result;
935 struct Lisp_Symbol *sym;
937 CHECK_SYMBOL (variable);
938 CHECK_BUFFER (buffer);
939 buf = XBUFFER (buffer);
941 sym = indirect_variable (XSYMBOL (variable));
942 XSETSYMBOL (variable, sym);
944 /* Look in local_var_list */
945 result = Fassoc (variable, buf->local_var_alist);
946 if (NILP (result))
948 int offset, idx;
949 int found = 0;
951 /* Look in special slots */
952 /* buffer-local Lisp variables start at `undo_list',
953 tho only the ones from `name' on are GC'd normally. */
954 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
955 offset < sizeof (struct buffer);
956 /* sizeof EMACS_INT == sizeof Lisp_Object */
957 offset += (sizeof (EMACS_INT)))
959 idx = PER_BUFFER_IDX (offset);
960 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
961 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
962 && EQ (PER_BUFFER_SYMBOL (offset), variable))
964 result = PER_BUFFER_VALUE (buf, offset);
965 found = 1;
966 break;
970 if (!found)
971 result = Fdefault_value (variable);
973 else
975 Lisp_Object valcontents;
976 Lisp_Object current_alist_element;
978 /* What binding is loaded right now? */
979 valcontents = sym->value;
980 current_alist_element
981 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
983 /* The value of the currently loaded binding is not
984 stored in it, but rather in the realvalue slot.
985 Store that value into the binding it belongs to
986 in case that is the one we are about to use. */
988 Fsetcdr (current_alist_element,
989 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
991 /* Now get the (perhaps updated) value out of the binding. */
992 result = XCDR (result);
995 if (!EQ (result, Qunbound))
996 return result;
998 xsignal1 (Qvoid_variable, variable);
1001 /* Return an alist of the Lisp-level buffer-local bindings of
1002 buffer BUF. That is, don't include the variables maintained
1003 in special slots in the buffer object. */
1005 static Lisp_Object
1006 buffer_lisp_local_variables (buf)
1007 struct buffer *buf;
1009 Lisp_Object result = Qnil;
1010 register Lisp_Object tail;
1011 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1013 Lisp_Object val, elt;
1015 elt = XCAR (tail);
1017 /* Reference each variable in the alist in buf.
1018 If inquiring about the current buffer, this gets the current values,
1019 so store them into the alist so the alist is up to date.
1020 If inquiring about some other buffer, this swaps out any values
1021 for that buffer, making the alist up to date automatically. */
1022 val = find_symbol_value (XCAR (elt));
1023 /* Use the current buffer value only if buf is the current buffer. */
1024 if (buf != current_buffer)
1025 val = XCDR (elt);
1027 /* If symbol is unbound, put just the symbol in the list. */
1028 if (EQ (val, Qunbound))
1029 result = Fcons (XCAR (elt), result);
1030 /* Otherwise, put (symbol . value) in the list. */
1031 else
1032 result = Fcons (Fcons (XCAR (elt), val), result);
1035 return result;
1038 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
1039 Sbuffer_local_variables, 0, 1, 0,
1040 doc: /* Return an alist of variables that are buffer-local in BUFFER.
1041 Most elements look like (SYMBOL . VALUE), describing one variable.
1042 For a symbol that is locally unbound, just the symbol appears in the value.
1043 Note that storing new VALUEs in these elements doesn't change the variables.
1044 No argument or nil as argument means use current buffer as BUFFER. */)
1045 (buffer)
1046 register Lisp_Object buffer;
1048 register struct buffer *buf;
1049 register Lisp_Object result;
1051 if (NILP (buffer))
1052 buf = current_buffer;
1053 else
1055 CHECK_BUFFER (buffer);
1056 buf = XBUFFER (buffer);
1059 result = buffer_lisp_local_variables (buf);
1061 /* Add on all the variables stored in special slots. */
1063 int offset, idx;
1065 /* buffer-local Lisp variables start at `undo_list',
1066 tho only the ones from `name' on are GC'd normally. */
1067 for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
1068 offset < sizeof (struct buffer);
1069 /* sizeof EMACS_INT == sizeof Lisp_Object */
1070 offset += (sizeof (EMACS_INT)))
1072 idx = PER_BUFFER_IDX (offset);
1073 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1074 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1075 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1076 PER_BUFFER_VALUE (buf, offset)),
1077 result);
1081 return result;
1084 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
1085 0, 1, 0,
1086 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1087 No argument or nil as argument means use current buffer as BUFFER. */)
1088 (buffer)
1089 register Lisp_Object buffer;
1091 register struct buffer *buf;
1092 if (NILP (buffer))
1093 buf = current_buffer;
1094 else
1096 CHECK_BUFFER (buffer);
1097 buf = XBUFFER (buffer);
1100 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1103 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
1104 1, 1, 0,
1105 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1106 A non-nil FLAG means mark the buffer modified. */)
1107 (flag)
1108 register Lisp_Object flag;
1110 register int already;
1111 register Lisp_Object fn;
1112 Lisp_Object buffer, window;
1114 #ifdef CLASH_DETECTION
1115 /* If buffer becoming modified, lock the file.
1116 If buffer becoming unmodified, unlock the file. */
1118 fn = current_buffer->file_truename;
1119 /* Test buffer-file-name so that binding it to nil is effective. */
1120 if (!NILP (fn) && ! NILP (current_buffer->filename))
1122 already = SAVE_MODIFF < MODIFF;
1123 if (!already && !NILP (flag))
1124 lock_file (fn);
1125 else if (already && NILP (flag))
1126 unlock_file (fn);
1128 #endif /* CLASH_DETECTION */
1130 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1132 /* Set update_mode_lines only if buffer is displayed in some window.
1133 Packages like jit-lock or lazy-lock preserve a buffer's modified
1134 state by recording/restoring the state around blocks of code.
1135 Setting update_mode_lines makes redisplay consider all windows
1136 (on all frames). Stealth fontification of buffers not displayed
1137 would incur additional redisplay costs if we'd set
1138 update_modes_lines unconditionally.
1140 Ideally, I think there should be another mechanism for fontifying
1141 buffers without "modifying" buffers, or redisplay should be
1142 smarter about updating the `*' in mode lines. --gerd */
1143 XSETBUFFER (buffer, current_buffer);
1144 window = Fget_buffer_window (buffer, Qt);
1145 if (WINDOWP (window))
1147 ++update_mode_lines;
1148 current_buffer->prevent_redisplay_optimizations_p = 1;
1151 return flag;
1154 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1155 Srestore_buffer_modified_p, 1, 1, 0,
1156 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
1157 It is not ensured that mode lines will be updated to show the modified
1158 state of the current buffer. Use with care. */)
1159 (flag)
1160 Lisp_Object flag;
1162 #ifdef CLASH_DETECTION
1163 Lisp_Object fn;
1165 /* If buffer becoming modified, lock the file.
1166 If buffer becoming unmodified, unlock the file. */
1168 fn = current_buffer->file_truename;
1169 /* Test buffer-file-name so that binding it to nil is effective. */
1170 if (!NILP (fn) && ! NILP (current_buffer->filename))
1172 int already = SAVE_MODIFF < MODIFF;
1173 if (!already && !NILP (flag))
1174 lock_file (fn);
1175 else if (already && NILP (flag))
1176 unlock_file (fn);
1178 #endif /* CLASH_DETECTION */
1180 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1181 return flag;
1184 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1185 0, 1, 0,
1186 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1187 Each buffer has a tick counter which is incremented each time the
1188 text in that buffer is changed. It wraps around occasionally.
1189 No argument or nil as argument means use current buffer as BUFFER. */)
1190 (buffer)
1191 register Lisp_Object buffer;
1193 register struct buffer *buf;
1194 if (NILP (buffer))
1195 buf = current_buffer;
1196 else
1198 CHECK_BUFFER (buffer);
1199 buf = XBUFFER (buffer);
1202 return make_number (BUF_MODIFF (buf));
1205 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1206 Sbuffer_chars_modified_tick, 0, 1, 0,
1207 doc: /* Return BUFFER's character-change tick counter.
1208 Each buffer has a character-change tick counter, which is set to the
1209 value of the buffer's tick counter \(see `buffer-modified-tick'), each
1210 time text in that buffer is inserted or deleted. By comparing the
1211 values returned by two individual calls of `buffer-chars-modified-tick',
1212 you can tell whether a character change occurred in that buffer in
1213 between these calls. No argument or nil as argument means use current
1214 buffer as BUFFER. */)
1215 (buffer)
1216 register Lisp_Object buffer;
1218 register struct buffer *buf;
1219 if (NILP (buffer))
1220 buf = current_buffer;
1221 else
1223 CHECK_BUFFER (buffer);
1224 buf = XBUFFER (buffer);
1227 return make_number (BUF_CHARS_MODIFF (buf));
1230 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1231 "(list (read-string \"Rename buffer (to new name): \" \
1232 nil 'buffer-name-history (buffer-name (current-buffer))) \
1233 current-prefix-arg)",
1234 doc: /* Change current buffer's name to NEWNAME (a string).
1235 If second arg UNIQUE is nil or omitted, it is an error if a
1236 buffer named NEWNAME already exists.
1237 If UNIQUE is non-nil, come up with a new name using
1238 `generate-new-buffer-name'.
1239 Interactively, you can set UNIQUE with a prefix argument.
1240 We return the name we actually gave the buffer.
1241 This does not change the name of the visited file (if any). */)
1242 (newname, unique)
1243 register Lisp_Object newname, unique;
1245 register Lisp_Object tem, buf;
1247 CHECK_STRING (newname);
1249 if (SCHARS (newname) == 0)
1250 error ("Empty string is invalid as a buffer name");
1252 tem = Fget_buffer (newname);
1253 if (!NILP (tem))
1255 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1256 rename the buffer automatically so you can create another
1257 with the original name. It makes UNIQUE equivalent to
1258 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1259 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1260 return current_buffer->name;
1261 if (!NILP (unique))
1262 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1263 else
1264 error ("Buffer name `%s' is in use", SDATA (newname));
1267 current_buffer->name = newname;
1269 /* Catch redisplay's attention. Unless we do this, the mode lines for
1270 any windows displaying current_buffer will stay unchanged. */
1271 update_mode_lines++;
1273 XSETBUFFER (buf, current_buffer);
1274 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1275 if (NILP (current_buffer->filename)
1276 && !NILP (current_buffer->auto_save_file_name))
1277 call0 (intern ("rename-auto-save-file"));
1278 /* Refetch since that last call may have done GC. */
1279 return current_buffer->name;
1282 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1283 doc: /* Return most recently selected buffer other than BUFFER.
1284 Buffers not visible in windows are preferred to visible buffers,
1285 unless optional second argument VISIBLE-OK is non-nil.
1286 If the optional third argument FRAME is non-nil, use that frame's
1287 buffer list instead of the selected frame's buffer list.
1288 If no other buffer exists, the buffer `*scratch*' is returned.
1289 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1290 (buffer, visible_ok, frame)
1291 register Lisp_Object buffer, visible_ok, frame;
1293 Lisp_Object Fset_buffer_major_mode ();
1294 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1295 notsogood = Qnil;
1297 if (NILP (frame))
1298 frame = selected_frame;
1300 tail = Vbuffer_alist;
1301 pred = frame_buffer_predicate (frame);
1303 /* Consider buffers that have been seen in the selected frame
1304 before other buffers. */
1306 tem = frame_buffer_list (frame);
1307 add_ons = Qnil;
1308 while (CONSP (tem))
1310 if (BUFFERP (XCAR (tem)))
1311 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1312 tem = XCDR (tem);
1314 tail = nconc2 (Fnreverse (add_ons), tail);
1316 for (; CONSP (tail); tail = XCDR (tail))
1318 buf = Fcdr (XCAR (tail));
1319 if (EQ (buf, buffer))
1320 continue;
1321 if (NILP (buf))
1322 continue;
1323 if (NILP (XBUFFER (buf)->name))
1324 continue;
1325 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1326 continue;
1327 /* If the selected frame has a buffer_predicate,
1328 disregard buffers that don't fit the predicate. */
1329 if (!NILP (pred))
1331 tem = call1 (pred, buf);
1332 if (NILP (tem))
1333 continue;
1336 if (NILP (visible_ok))
1337 tem = Fget_buffer_window (buf, Qvisible);
1338 else
1339 tem = Qnil;
1340 if (NILP (tem))
1341 return buf;
1342 if (NILP (notsogood))
1343 notsogood = buf;
1345 if (!NILP (notsogood))
1346 return notsogood;
1347 buf = Fget_buffer (build_string ("*scratch*"));
1348 if (NILP (buf))
1350 buf = Fget_buffer_create (build_string ("*scratch*"));
1351 Fset_buffer_major_mode (buf);
1353 return buf;
1356 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1357 0, 1, "",
1358 doc: /* Start keeping undo information for buffer BUFFER.
1359 No argument or nil as argument means do this for the current buffer. */)
1360 (buffer)
1361 register Lisp_Object buffer;
1363 Lisp_Object real_buffer;
1365 if (NILP (buffer))
1366 XSETBUFFER (real_buffer, current_buffer);
1367 else
1369 real_buffer = Fget_buffer (buffer);
1370 if (NILP (real_buffer))
1371 nsberror (buffer);
1374 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1375 XBUFFER (real_buffer)->undo_list = Qnil;
1377 return Qnil;
1381 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1382 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1383 The buffer being killed will be current while the hook is running.\n\
1384 See `kill-buffer'."
1386 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
1387 doc: /* Kill buffer BUFFER-OR-NAME.
1388 The argument may be a buffer or the name of an existing buffer.
1389 Argument nil or omitted means kill the current buffer. Return t if the
1390 buffer is actually killed, nil otherwise.
1392 This function calls `replace-buffer-in-windows' for cleaning up all
1393 windows currently displaying the buffer to be killed. The functions in
1394 `kill-buffer-query-functions' are called with the buffer to be killed as
1395 the current buffer. If any of them returns nil, the buffer is not
1396 killed. The hook `kill-buffer-hook' is run before the buffer is
1397 actually killed. The buffer being killed will be current while the hook
1398 is running.
1400 Any processes that have this buffer as the `process-buffer' are killed
1401 with SIGHUP. */)
1402 (buffer_or_name)
1403 Lisp_Object buffer_or_name;
1405 Lisp_Object buffer;
1406 register struct buffer *b;
1407 register Lisp_Object tem;
1408 register struct Lisp_Marker *m;
1409 struct gcpro gcpro1;
1411 if (NILP (buffer_or_name))
1412 buffer = Fcurrent_buffer ();
1413 else
1414 buffer = Fget_buffer (buffer_or_name);
1415 if (NILP (buffer))
1416 nsberror (buffer_or_name);
1418 b = XBUFFER (buffer);
1420 /* Avoid trouble for buffer already dead. */
1421 if (NILP (b->name))
1422 return Qnil;
1424 /* Query if the buffer is still modified. */
1425 if (INTERACTIVE && !NILP (b->filename)
1426 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1428 GCPRO1 (buffer);
1429 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1430 b->name, make_number (0)));
1431 UNGCPRO;
1432 if (NILP (tem))
1433 return Qnil;
1436 /* Run hooks with the buffer to be killed the current buffer. */
1438 int count = SPECPDL_INDEX ();
1439 Lisp_Object arglist[1];
1441 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1442 set_buffer_internal (b);
1444 /* First run the query functions; if any query is answered no,
1445 don't kill the buffer. */
1446 arglist[0] = Qkill_buffer_query_functions;
1447 tem = Frun_hook_with_args_until_failure (1, arglist);
1448 if (NILP (tem))
1449 return unbind_to (count, Qnil);
1451 /* Then run the hooks. */
1452 Frun_hooks (1, &Qkill_buffer_hook);
1453 unbind_to (count, Qnil);
1456 /* We have no more questions to ask. Verify that it is valid
1457 to kill the buffer. This must be done after the questions
1458 since anything can happen within do_yes_or_no_p. */
1460 /* Don't kill the minibuffer now current. */
1461 if (EQ (buffer, XWINDOW (minibuf_window)->buffer))
1462 return Qnil;
1464 if (NILP (b->name))
1465 return Qnil;
1467 /* When we kill a base buffer, kill all its indirect buffers.
1468 We do it at this stage so nothing terrible happens if they
1469 ask questions or their hooks get errors. */
1470 if (! b->base_buffer)
1472 struct buffer *other;
1474 GCPRO1 (buffer);
1476 for (other = all_buffers; other; other = other->next)
1477 /* all_buffers contains dead buffers too;
1478 don't re-kill them. */
1479 if (other->base_buffer == b && !NILP (other->name))
1481 Lisp_Object buffer;
1482 XSETBUFFER (buffer, other);
1483 Fkill_buffer (buffer);
1486 UNGCPRO;
1489 /* Make this buffer not be current.
1490 In the process, notice if this is the sole visible buffer
1491 and give up if so. */
1492 if (b == current_buffer)
1494 tem = Fother_buffer (buffer, Qnil, Qnil);
1495 Fset_buffer (tem);
1496 if (b == current_buffer)
1497 return Qnil;
1500 /* Notice if the buffer to kill is the sole visible buffer
1501 when we're currently in the mini-buffer, and give up if so. */
1502 XSETBUFFER (tem, current_buffer);
1503 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1505 tem = Fother_buffer (buffer, Qnil, Qnil);
1506 if (EQ (buffer, tem))
1507 return Qnil;
1510 /* Now there is no question: we can kill the buffer. */
1512 #ifdef CLASH_DETECTION
1513 /* Unlock this buffer's file, if it is locked. */
1514 unlock_buffer (b);
1515 #endif /* CLASH_DETECTION */
1517 GCPRO1 (buffer);
1518 kill_buffer_processes (buffer);
1519 UNGCPRO;
1521 /* Killing buffer processes may run sentinels which may
1522 have called kill-buffer. */
1524 if (NILP (b->name))
1525 return Qnil;
1527 clear_charpos_cache (b);
1529 tem = Vinhibit_quit;
1530 Vinhibit_quit = Qt;
1531 replace_buffer_in_all_windows (buffer);
1532 Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
1533 frames_discard_buffer (buffer);
1534 Vinhibit_quit = tem;
1536 /* Delete any auto-save file, if we saved it in this session.
1537 But not if the buffer is modified. */
1538 if (STRINGP (b->auto_save_file_name)
1539 && b->auto_save_modified != 0
1540 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1541 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1542 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1544 Lisp_Object tem;
1545 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1546 if (! NILP (tem))
1547 internal_delete_file (b->auto_save_file_name);
1550 if (b->base_buffer)
1552 /* Unchain all markers that belong to this indirect buffer.
1553 Don't unchain the markers that belong to the base buffer
1554 or its other indirect buffers. */
1555 for (m = BUF_MARKERS (b); m; )
1557 struct Lisp_Marker *next = m->next;
1558 if (m->buffer == b)
1559 unchain_marker (m);
1560 m = next;
1563 else
1565 /* Unchain all markers of this buffer and its indirect buffers.
1566 and leave them pointing nowhere. */
1567 for (m = BUF_MARKERS (b); m; )
1569 struct Lisp_Marker *next = m->next;
1570 m->buffer = 0;
1571 m->next = NULL;
1572 m = next;
1574 BUF_MARKERS (b) = NULL;
1575 BUF_INTERVALS (b) = NULL_INTERVAL;
1577 /* Perhaps we should explicitly free the interval tree here... */
1580 /* Reset the local variables, so that this buffer's local values
1581 won't be protected from GC. They would be protected
1582 if they happened to remain encached in their symbols.
1583 This gets rid of them for certain. */
1584 swap_out_buffer_local_variables (b);
1585 reset_buffer_local_variables (b, 1);
1587 b->name = Qnil;
1589 BLOCK_INPUT;
1590 if (! b->base_buffer)
1591 free_buffer_text (b);
1593 if (b->newline_cache)
1595 free_region_cache (b->newline_cache);
1596 b->newline_cache = 0;
1598 if (b->width_run_cache)
1600 free_region_cache (b->width_run_cache);
1601 b->width_run_cache = 0;
1603 b->width_table = Qnil;
1604 UNBLOCK_INPUT;
1605 b->undo_list = Qnil;
1607 return Qt;
1610 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1611 we do this each time BUF is selected visibly, the more recently
1612 selected buffers are always closer to the front of the list. This
1613 means that other_buffer is more likely to choose a relevant buffer. */
1615 void
1616 record_buffer (buf)
1617 Lisp_Object buf;
1619 register Lisp_Object link, prev;
1620 Lisp_Object frame;
1621 frame = selected_frame;
1623 prev = Qnil;
1624 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1626 if (EQ (XCDR (XCAR (link)), buf))
1627 break;
1628 prev = link;
1631 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1632 we cannot use Fdelq itself here because it allows quitting. */
1634 if (NILP (prev))
1635 Vbuffer_alist = XCDR (Vbuffer_alist);
1636 else
1637 XSETCDR (prev, XCDR (XCDR (prev)));
1639 XSETCDR (link, Vbuffer_alist);
1640 Vbuffer_alist = link;
1642 /* Effectively do a delq on buried_buffer_list. */
1644 prev = Qnil;
1645 for (link = XFRAME (frame)->buried_buffer_list; CONSP (link);
1646 link = XCDR (link))
1648 if (EQ (XCAR (link), buf))
1650 if (NILP (prev))
1651 XFRAME (frame)->buried_buffer_list = XCDR (link);
1652 else
1653 XSETCDR (prev, XCDR (XCDR (prev)));
1654 break;
1656 prev = link;
1659 /* Now move this buffer to the front of frame_buffer_list also. */
1661 prev = Qnil;
1662 for (link = frame_buffer_list (frame); CONSP (link);
1663 link = XCDR (link))
1665 if (EQ (XCAR (link), buf))
1666 break;
1667 prev = link;
1670 /* Effectively do delq. */
1672 if (CONSP (link))
1674 if (NILP (prev))
1675 set_frame_buffer_list (frame,
1676 XCDR (frame_buffer_list (frame)));
1677 else
1678 XSETCDR (prev, XCDR (XCDR (prev)));
1680 XSETCDR (link, frame_buffer_list (frame));
1681 set_frame_buffer_list (frame, link);
1683 else
1684 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1687 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1688 doc: /* Set an appropriate major mode for BUFFER.
1689 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1690 according to `default-major-mode'.
1691 Use this function before selecting the buffer, since it may need to inspect
1692 the current buffer's major mode. */)
1693 (buffer)
1694 Lisp_Object buffer;
1696 int count;
1697 Lisp_Object function;
1699 CHECK_BUFFER (buffer);
1701 if (STRINGP (XBUFFER (buffer)->name)
1702 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1703 function = find_symbol_value (intern ("initial-major-mode"));
1704 else
1706 function = buffer_defaults.major_mode;
1707 if (NILP (function)
1708 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1709 function = current_buffer->major_mode;
1712 if (NILP (function) || EQ (function, Qfundamental_mode))
1713 return Qnil;
1715 count = SPECPDL_INDEX ();
1717 /* To select a nonfundamental mode,
1718 select the buffer temporarily and then call the mode function. */
1720 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1722 Fset_buffer (buffer);
1723 call0 (function);
1725 return unbind_to (count, Qnil);
1728 /* Switch to buffer BUFFER in the selected window.
1729 If NORECORD is non-nil, don't call record_buffer. */
1731 Lisp_Object
1732 switch_to_buffer_1 (buffer_or_name, norecord)
1733 Lisp_Object buffer_or_name, norecord;
1735 register Lisp_Object buffer;
1737 if (NILP (buffer_or_name))
1738 buffer = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1739 else
1741 buffer = Fget_buffer (buffer_or_name);
1742 if (NILP (buffer))
1744 buffer = Fget_buffer_create (buffer_or_name);
1745 Fset_buffer_major_mode (buffer);
1748 Fset_buffer (buffer);
1749 if (NILP (norecord))
1750 record_buffer (buffer);
1752 Fset_window_buffer (EQ (selected_window, minibuf_window)
1753 ? Fnext_window (minibuf_window, Qnil, Qnil)
1754 : selected_window,
1755 buffer, Qnil);
1757 return buffer;
1760 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2,
1761 "(list (read-buffer-to-switch \"Switch to buffer: \"))",
1762 doc: /* Make BUFFER-OR-NAME current and display it in the selected window.
1763 BUFFER-OR-NAME may be a buffer, a string, or nil. If BUFFER-OR-NAME is
1764 nil, then this function chooses a buffer using `other-buffer'. If
1765 BUFFER-OR-NAME is a string and does not identify an existing buffer,
1766 then this function creates a buffer with that name.
1768 Optional second arg NORECORD non-nil means do not put this buffer at the
1769 front of the list of recently selected ones. This function returns the
1770 buffer it switched to as a Lisp object.
1772 If the selected window is the minibuffer window or dedicated to its
1773 buffer, use `pop-to-buffer' for displaying the buffer.
1775 WARNING: This is NOT the way to work on another buffer temporarily
1776 within a Lisp program! Use `set-buffer' instead. That avoids messing
1777 with the window-buffer correspondences. */)
1778 (buffer_or_name, norecord)
1779 Lisp_Object buffer_or_name, norecord;
1781 char *err;
1783 if (EQ (buffer_or_name, Fwindow_buffer (selected_window)))
1785 /* Basically a NOP. Avoid signalling an error in the case where
1786 the selected window is dedicated, or a minibuffer. */
1788 /* But do put this buffer at the front of the buffer list, unless
1789 that has been inhibited. Note that even if BUFFER-OR-NAME is
1790 at the front of the main buffer-list already, we still want to
1791 move it to the front of the frame's buffer list. */
1792 if (NILP (norecord))
1793 record_buffer (buffer_or_name);
1794 return Fset_buffer (buffer_or_name);
1797 if (EQ (minibuf_window, selected_window)
1798 || !NILP (Fwindow_dedicated_p (selected_window)))
1799 /* We can't use the selected window so let `pop-to-buffer' try some
1800 other window. */
1801 return call3 (intern ("pop-to-buffer"), buffer_or_name, Qnil, norecord);
1802 else
1803 return switch_to_buffer_1 (buffer_or_name, norecord);
1806 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1807 doc: /* Return the current buffer as a Lisp object. */)
1810 register Lisp_Object buf;
1811 XSETBUFFER (buf, current_buffer);
1812 return buf;
1815 /* Set the current buffer to B.
1817 We previously set windows_or_buffers_changed here to invalidate
1818 global unchanged information in beg_unchanged and end_unchanged.
1819 This is no longer necessary because we now compute unchanged
1820 information on a buffer-basis. Every action affecting other
1821 windows than the selected one requires a select_window at some
1822 time, and that increments windows_or_buffers_changed. */
1824 void
1825 set_buffer_internal (b)
1826 register struct buffer *b;
1828 if (current_buffer != b)
1829 set_buffer_internal_1 (b);
1832 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1833 This is used by redisplay. */
1835 void
1836 set_buffer_internal_1 (b)
1837 register struct buffer *b;
1839 register struct buffer *old_buf;
1840 register Lisp_Object tail, valcontents;
1841 Lisp_Object tem;
1843 #ifdef USE_MMAP_FOR_BUFFERS
1844 if (b->text->beg == NULL)
1845 enlarge_buffer_text (b, 0);
1846 #endif /* USE_MMAP_FOR_BUFFERS */
1848 if (current_buffer == b)
1849 return;
1851 old_buf = current_buffer;
1852 current_buffer = b;
1853 last_known_column_point = -1; /* invalidate indentation cache */
1855 if (old_buf)
1857 /* Put the undo list back in the base buffer, so that it appears
1858 that an indirect buffer shares the undo list of its base. */
1859 if (old_buf->base_buffer)
1860 old_buf->base_buffer->undo_list = old_buf->undo_list;
1862 /* If the old current buffer has markers to record PT, BEGV and ZV
1863 when it is not current, update them now. */
1864 if (! NILP (old_buf->pt_marker))
1866 Lisp_Object obuf;
1867 XSETBUFFER (obuf, old_buf);
1868 set_marker_both (old_buf->pt_marker, obuf,
1869 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1871 if (! NILP (old_buf->begv_marker))
1873 Lisp_Object obuf;
1874 XSETBUFFER (obuf, old_buf);
1875 set_marker_both (old_buf->begv_marker, obuf,
1876 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1878 if (! NILP (old_buf->zv_marker))
1880 Lisp_Object obuf;
1881 XSETBUFFER (obuf, old_buf);
1882 set_marker_both (old_buf->zv_marker, obuf,
1883 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1887 /* Get the undo list from the base buffer, so that it appears
1888 that an indirect buffer shares the undo list of its base. */
1889 if (b->base_buffer)
1890 b->undo_list = b->base_buffer->undo_list;
1892 /* If the new current buffer has markers to record PT, BEGV and ZV
1893 when it is not current, fetch them now. */
1894 if (! NILP (b->pt_marker))
1896 BUF_PT (b) = marker_position (b->pt_marker);
1897 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1899 if (! NILP (b->begv_marker))
1901 BUF_BEGV (b) = marker_position (b->begv_marker);
1902 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1904 if (! NILP (b->zv_marker))
1906 BUF_ZV (b) = marker_position (b->zv_marker);
1907 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1910 /* Look down buffer's list of local Lisp variables
1911 to find and update any that forward into C variables. */
1913 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1915 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1916 if ((BUFFER_LOCAL_VALUEP (valcontents))
1917 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1918 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1919 /* Just reference the variable
1920 to cause it to become set for this buffer. */
1921 Fsymbol_value (XCAR (XCAR (tail)));
1924 /* Do the same with any others that were local to the previous buffer */
1926 if (old_buf)
1927 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1929 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1930 if ((BUFFER_LOCAL_VALUEP (valcontents))
1931 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1932 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1933 /* Just reference the variable
1934 to cause it to become set for this buffer. */
1935 Fsymbol_value (XCAR (XCAR (tail)));
1939 /* Switch to buffer B temporarily for redisplay purposes.
1940 This avoids certain things that don't need to be done within redisplay. */
1942 void
1943 set_buffer_temp (b)
1944 struct buffer *b;
1946 register struct buffer *old_buf;
1948 if (current_buffer == b)
1949 return;
1951 old_buf = current_buffer;
1952 current_buffer = b;
1954 if (old_buf)
1956 /* If the old current buffer has markers to record PT, BEGV and ZV
1957 when it is not current, update them now. */
1958 if (! NILP (old_buf->pt_marker))
1960 Lisp_Object obuf;
1961 XSETBUFFER (obuf, old_buf);
1962 set_marker_both (old_buf->pt_marker, obuf,
1963 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1965 if (! NILP (old_buf->begv_marker))
1967 Lisp_Object obuf;
1968 XSETBUFFER (obuf, old_buf);
1969 set_marker_both (old_buf->begv_marker, obuf,
1970 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1972 if (! NILP (old_buf->zv_marker))
1974 Lisp_Object obuf;
1975 XSETBUFFER (obuf, old_buf);
1976 set_marker_both (old_buf->zv_marker, obuf,
1977 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1981 /* If the new current buffer has markers to record PT, BEGV and ZV
1982 when it is not current, fetch them now. */
1983 if (! NILP (b->pt_marker))
1985 BUF_PT (b) = marker_position (b->pt_marker);
1986 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1988 if (! NILP (b->begv_marker))
1990 BUF_BEGV (b) = marker_position (b->begv_marker);
1991 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1993 if (! NILP (b->zv_marker))
1995 BUF_ZV (b) = marker_position (b->zv_marker);
1996 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
2000 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
2001 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
2002 BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
2003 also `save-excursion' when you want to make a buffer current
2004 temporarily. This function does not display the buffer, so its effect
2005 ends when the current command terminates. Use `switch-to-buffer' or
2006 `pop-to-buffer' to switch buffers permanently. */)
2007 (buffer_or_name)
2008 register Lisp_Object buffer_or_name;
2010 register Lisp_Object buffer;
2011 buffer = Fget_buffer (buffer_or_name);
2012 if (NILP (buffer))
2013 nsberror (buffer_or_name);
2014 if (NILP (XBUFFER (buffer)->name))
2015 error ("Selecting deleted buffer");
2016 set_buffer_internal (XBUFFER (buffer));
2017 return buffer;
2020 /* Set the current buffer to BUFFER provided it is alive. */
2022 Lisp_Object
2023 set_buffer_if_live (buffer)
2024 Lisp_Object buffer;
2026 if (! NILP (XBUFFER (buffer)->name))
2027 Fset_buffer (buffer);
2028 return Qnil;
2031 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2032 Sbarf_if_buffer_read_only, 0, 0, 0,
2033 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2036 if (!NILP (current_buffer->read_only)
2037 && NILP (Vinhibit_read_only))
2038 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
2039 return Qnil;
2042 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
2043 doc: /* Put BUFFER-OR-NAME at the end of the list of all buffers.
2044 There it is the least likely candidate for `other-buffer' to return;
2045 thus, the least likely buffer for \\[switch-to-buffer] to select by
2046 default.
2048 The argument may be a buffer name or an actual buffer object. If
2049 BUFFER-OR-NAME is nil or omitted, bury the current buffer and remove it
2050 from the selected window if it is displayed there. */)
2051 (buffer_or_name)
2052 register Lisp_Object buffer_or_name;
2054 Lisp_Object buffer;
2056 /* Figure out what buffer we're going to bury. */
2057 if (NILP (buffer_or_name))
2059 Lisp_Object tem;
2060 XSETBUFFER (buffer, current_buffer);
2062 tem = Fwindow_buffer (selected_window);
2063 /* If we're burying the current buffer, unshow it. */
2064 if (EQ (buffer, tem))
2066 if (NILP (Fwindow_dedicated_p (selected_window)))
2067 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2068 else if (NILP (XWINDOW (selected_window)->parent))
2069 Ficonify_frame (Fwindow_frame (selected_window));
2070 else
2071 Fdelete_window (selected_window);
2074 else
2076 buffer = Fget_buffer (buffer_or_name);
2077 if (NILP (buffer))
2078 nsberror (buffer_or_name);
2081 /* Move buffer to the end of the buffer list. Do nothing if the
2082 buffer is killed. */
2083 if (!NILP (XBUFFER (buffer)->name))
2085 Lisp_Object aelt, link;
2087 aelt = Frassq (buffer, Vbuffer_alist);
2088 link = Fmemq (aelt, Vbuffer_alist);
2089 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
2090 XSETCDR (link, Qnil);
2091 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
2093 XFRAME (selected_frame)->buffer_list
2094 = Fdelq (buffer, XFRAME (selected_frame)->buffer_list);
2095 XFRAME (selected_frame)->buried_buffer_list
2096 = Fcons (buffer, Fdelq (buffer, XFRAME (selected_frame)->buried_buffer_list));
2099 return Qnil;
2102 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
2103 doc: /* Delete the entire contents of the current buffer.
2104 Any narrowing restriction in effect (see `narrow-to-region') is removed,
2105 so the buffer is truly empty after this. */)
2108 Fwiden ();
2110 del_range (BEG, Z);
2112 current_buffer->last_window_start = 1;
2113 /* Prevent warnings, or suspension of auto saving, that would happen
2114 if future size is less than past size. Use of erase-buffer
2115 implies that the future text is not really related to the past text. */
2116 XSETFASTINT (current_buffer->save_length, 0);
2117 return Qnil;
2120 void
2121 validate_region (b, e)
2122 register Lisp_Object *b, *e;
2124 CHECK_NUMBER_COERCE_MARKER (*b);
2125 CHECK_NUMBER_COERCE_MARKER (*e);
2127 if (XINT (*b) > XINT (*e))
2129 Lisp_Object tem;
2130 tem = *b; *b = *e; *e = tem;
2133 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2134 && XINT (*e) <= ZV))
2135 args_out_of_range (*b, *e);
2138 /* Advance BYTE_POS up to a character boundary
2139 and return the adjusted position. */
2141 static int
2142 advance_to_char_boundary (byte_pos)
2143 int byte_pos;
2145 int c;
2147 if (byte_pos == BEG)
2148 /* Beginning of buffer is always a character boundary. */
2149 return BEG;
2151 c = FETCH_BYTE (byte_pos);
2152 if (! CHAR_HEAD_P (c))
2154 /* We should advance BYTE_POS only when C is a constituent of a
2155 multibyte sequence. */
2156 int orig_byte_pos = byte_pos;
2160 byte_pos--;
2161 c = FETCH_BYTE (byte_pos);
2163 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2164 INC_POS (byte_pos);
2165 if (byte_pos < orig_byte_pos)
2166 byte_pos = orig_byte_pos;
2167 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2168 surely advance to the correct character boundary. If C is
2169 not, BYTE_POS was unchanged. */
2172 return byte_pos;
2175 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2176 1, 1, 0,
2177 doc: /* Swap the text between current buffer and BUFFER. */)
2178 (buffer)
2179 Lisp_Object buffer;
2181 struct buffer *other_buffer;
2182 CHECK_BUFFER (buffer);
2183 other_buffer = XBUFFER (buffer);
2185 /* Actually, it probably works just fine.
2186 * if (other_buffer == current_buffer)
2187 * error ("Cannot swap a buffer's text with itself"); */
2189 /* Actually, this may be workable as well, tho probably only if they're
2190 *both* indirect. */
2191 if (other_buffer->base_buffer
2192 || current_buffer->base_buffer)
2193 error ("Cannot swap indirect buffers's text");
2195 { /* This is probably harder to make work. */
2196 struct buffer *other;
2197 for (other = all_buffers; other; other = other->next)
2198 if (other->base_buffer == other_buffer
2199 || other->base_buffer == current_buffer)
2200 error ("One of the buffers to swap has indirect buffers");
2203 #define swapfield(field, type) \
2204 do { \
2205 type tmp##field = other_buffer->field; \
2206 other_buffer->field = current_buffer->field; \
2207 current_buffer->field = tmp##field; \
2208 } while (0)
2210 swapfield (own_text, struct buffer_text);
2211 eassert (current_buffer->text == &current_buffer->own_text);
2212 eassert (other_buffer->text == &other_buffer->own_text);
2213 swapfield (pt, EMACS_INT);
2214 swapfield (pt_byte, EMACS_INT);
2215 swapfield (begv, EMACS_INT);
2216 swapfield (begv_byte, EMACS_INT);
2217 swapfield (zv, EMACS_INT);
2218 swapfield (zv_byte, EMACS_INT);
2219 eassert (!current_buffer->base_buffer);
2220 eassert (!other_buffer->base_buffer);
2221 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2222 swapfield (newline_cache, struct region_cache *);
2223 swapfield (width_run_cache, struct region_cache *);
2224 current_buffer->prevent_redisplay_optimizations_p = 1;
2225 other_buffer->prevent_redisplay_optimizations_p = 1;
2226 swapfield (overlays_before, struct Lisp_Overlay *);
2227 swapfield (overlays_after, struct Lisp_Overlay *);
2228 swapfield (overlay_center, EMACS_INT);
2229 swapfield (undo_list, Lisp_Object);
2230 swapfield (mark, Lisp_Object);
2231 if (MARKERP (current_buffer->mark) && XMARKER (current_buffer->mark)->buffer)
2232 XMARKER (current_buffer->mark)->buffer = current_buffer;
2233 if (MARKERP (other_buffer->mark) && XMARKER (other_buffer->mark)->buffer)
2234 XMARKER (other_buffer->mark)->buffer = other_buffer;
2235 swapfield (enable_multibyte_characters, Lisp_Object);
2236 /* FIXME: Not sure what we should do with these *_marker fields.
2237 Hopefully they're just nil anyway. */
2238 swapfield (pt_marker, Lisp_Object);
2239 swapfield (begv_marker, Lisp_Object);
2240 swapfield (zv_marker, Lisp_Object);
2241 current_buffer->point_before_scroll = Qnil;
2242 other_buffer->point_before_scroll = Qnil;
2244 current_buffer->text->modiff++; other_buffer->text->modiff++;
2245 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2246 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2247 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2248 current_buffer->text->end_unchanged = current_buffer->text->gpt;
2249 other_buffer->text->beg_unchanged = current_buffer->text->gpt;
2250 other_buffer->text->end_unchanged = current_buffer->text->gpt;
2252 struct Lisp_Marker *m;
2253 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2254 if (m->buffer == other_buffer)
2255 m->buffer = current_buffer;
2256 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2257 if (m->buffer == current_buffer)
2258 m->buffer = other_buffer;
2260 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2261 So since we just swapped the markers between the two buffers, we need
2262 to undo the effect of this swap for window markers. */
2263 Lisp_Object w = Fselected_window (), ws = Qnil;
2264 Lisp_Object buf1, buf2;
2265 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2267 while (NILP (Fmemq (w, ws)))
2269 ws = Fcons (w, ws);
2270 if (MARKERP (XWINDOW (w)->pointm)
2271 && (EQ (XWINDOW (w)->buffer, buf1)
2272 || EQ (XWINDOW (w)->buffer, buf2)))
2273 Fset_marker (XWINDOW (w)->pointm,
2274 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2275 XWINDOW (w)->buffer);
2276 w = Fnext_window (w, Qt, Qt);
2280 if (current_buffer->text->intervals)
2281 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2282 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2283 if (other_buffer->text->intervals)
2284 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2285 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2287 return Qnil;
2290 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2291 1, 1, 0,
2292 doc: /* Set the multibyte flag of the current buffer to FLAG.
2293 If FLAG is t, this makes the buffer a multibyte buffer.
2294 If FLAG is nil, this makes the buffer a single-byte buffer.
2295 In these cases, the buffer contents remain unchanged as a sequence of
2296 bytes but the contents viewed as characters do change.
2297 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2298 all eight-bit bytes to eight-bit characters.
2299 If the multibyte flag was really changed, undo information of the
2300 current buffer is cleared. */)
2301 (flag)
2302 Lisp_Object flag;
2304 struct Lisp_Marker *tail, *markers;
2305 struct buffer *other;
2306 int begv, zv;
2307 int narrowed = (BEG != BEGV || Z != ZV);
2308 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2309 Lisp_Object old_undo = current_buffer->undo_list;
2310 struct gcpro gcpro1;
2312 if (current_buffer->base_buffer)
2313 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2315 /* Do nothing if nothing actually changes. */
2316 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2317 return flag;
2319 GCPRO1 (old_undo);
2321 /* Don't record these buffer changes. We will put a special undo entry
2322 instead. */
2323 current_buffer->undo_list = Qt;
2325 /* If the cached position is for this buffer, clear it out. */
2326 clear_charpos_cache (current_buffer);
2328 if (NILP (flag))
2329 begv = BEGV_BYTE, zv = ZV_BYTE;
2330 else
2331 begv = BEGV, zv = ZV;
2333 if (narrowed)
2334 Fwiden ();
2336 if (NILP (flag))
2338 int pos, stop;
2339 unsigned char *p;
2341 /* Do this first, so it can use CHAR_TO_BYTE
2342 to calculate the old correspondences. */
2343 set_intervals_multibyte (0);
2345 current_buffer->enable_multibyte_characters = Qnil;
2347 Z = Z_BYTE;
2348 BEGV = BEGV_BYTE;
2349 ZV = ZV_BYTE;
2350 GPT = GPT_BYTE;
2351 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2354 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2355 tail->charpos = tail->bytepos;
2357 /* Convert multibyte form of 8-bit characters to unibyte. */
2358 pos = BEG;
2359 stop = GPT;
2360 p = BEG_ADDR;
2361 while (1)
2363 int c, bytes;
2365 if (pos == stop)
2367 if (pos == Z)
2368 break;
2369 p = GAP_END_ADDR;
2370 stop = Z;
2372 if (ASCII_BYTE_P (*p))
2373 p++, pos++;
2374 else if (CHAR_BYTE8_HEAD_P (*p))
2376 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
2377 /* Delete all bytes for this 8-bit character but the
2378 last one, and change the last one to the charcter
2379 code. */
2380 bytes--;
2381 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2382 p = GAP_END_ADDR;
2383 *p++ = c;
2384 pos++;
2385 if (begv > pos)
2386 begv -= bytes;
2387 if (zv > pos)
2388 zv -= bytes;
2389 stop = Z;
2391 else
2393 bytes = BYTES_BY_CHAR_HEAD (*p);
2394 p += bytes, pos += bytes;
2397 if (narrowed)
2398 Fnarrow_to_region (make_number (begv), make_number (zv));
2400 else
2402 int pt = PT;
2403 int pos, stop;
2404 unsigned char *p, *pend;
2406 /* Be sure not to have a multibyte sequence striding over the GAP.
2407 Ex: We change this: "...abc\302 _GAP_ \241def..."
2408 to: "...abc _GAP_ \302\241def..." */
2410 if (EQ (flag, Qt)
2411 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2412 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2414 unsigned char *p = GPT_ADDR - 1;
2416 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2417 if (BASE_LEADING_CODE_P (*p))
2419 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2421 move_gap_both (new_gpt, new_gpt);
2425 /* Make the buffer contents valid as multibyte by converting
2426 8-bit characters to multibyte form. */
2427 pos = BEG;
2428 stop = GPT;
2429 p = BEG_ADDR;
2430 pend = GPT_ADDR;
2431 while (1)
2433 int bytes;
2435 if (pos == stop)
2437 if (pos == Z)
2438 break;
2439 p = GAP_END_ADDR;
2440 pend = Z_ADDR;
2441 stop = Z;
2444 if (ASCII_BYTE_P (*p))
2445 p++, pos++;
2446 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2447 p += bytes, pos += bytes;
2448 else
2450 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2451 int c;
2453 c = BYTE8_TO_CHAR (*p);
2454 bytes = CHAR_STRING (c, tmp);
2455 *p = tmp[0];
2456 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2457 bytes--;
2458 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2459 /* Now the gap is after the just inserted data. */
2460 pos = GPT;
2461 p = GAP_END_ADDR;
2462 if (pos <= begv)
2463 begv += bytes;
2464 if (pos <= zv)
2465 zv += bytes;
2466 if (pos <= pt)
2467 pt += bytes;
2468 pend = Z_ADDR;
2469 stop = Z;
2473 if (pt != PT)
2474 TEMP_SET_PT (pt);
2476 if (narrowed)
2477 Fnarrow_to_region (make_number (begv), make_number (zv));
2479 /* Do this first, so that chars_in_text asks the right question.
2480 set_intervals_multibyte needs it too. */
2481 current_buffer->enable_multibyte_characters = Qt;
2483 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2484 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2486 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2488 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2489 if (BEGV_BYTE > GPT_BYTE)
2490 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2491 else
2492 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2494 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2495 if (ZV_BYTE > GPT_BYTE)
2496 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2497 else
2498 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2501 int pt_byte = advance_to_char_boundary (PT_BYTE);
2502 int pt;
2504 if (pt_byte > GPT_BYTE)
2505 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2506 else
2507 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2508 TEMP_SET_PT_BOTH (pt, pt_byte);
2511 tail = markers = BUF_MARKERS (current_buffer);
2513 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2514 getting confused by the markers that have not yet been updated.
2515 It is also a signal that it should never create a marker. */
2516 BUF_MARKERS (current_buffer) = NULL;
2518 for (; tail; tail = tail->next)
2520 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2521 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2524 /* Make sure no markers were put on the chain
2525 while the chain value was incorrect. */
2526 if (BUF_MARKERS (current_buffer))
2527 abort ();
2529 BUF_MARKERS (current_buffer) = markers;
2531 /* Do this last, so it can calculate the new correspondences
2532 between chars and bytes. */
2533 set_intervals_multibyte (1);
2536 if (!EQ (old_undo, Qt))
2538 /* Represent all the above changes by a special undo entry. */
2539 extern Lisp_Object Qapply;
2540 current_buffer->undo_list = Fcons (list3 (Qapply,
2541 intern ("set-buffer-multibyte"),
2542 NILP (flag) ? Qt : Qnil),
2543 old_undo);
2546 UNGCPRO;
2548 /* Changing the multibyteness of a buffer means that all windows
2549 showing that buffer must be updated thoroughly. */
2550 current_buffer->prevent_redisplay_optimizations_p = 1;
2551 ++windows_or_buffers_changed;
2553 /* Copy this buffer's new multibyte status
2554 into all of its indirect buffers. */
2555 for (other = all_buffers; other; other = other->next)
2556 if (other->base_buffer == current_buffer && !NILP (other->name))
2558 other->enable_multibyte_characters
2559 = current_buffer->enable_multibyte_characters;
2560 other->prevent_redisplay_optimizations_p = 1;
2563 /* Restore the modifiedness of the buffer. */
2564 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2565 Fset_buffer_modified_p (Qnil);
2567 #ifdef subprocesses
2568 /* Update coding systems of this buffer's process (if any). */
2570 Lisp_Object process;
2572 process = Fget_buffer_process (Fcurrent_buffer ());
2573 if (PROCESSP (process))
2574 setup_process_coding_systems (process);
2576 #endif /* subprocesses */
2578 return flag;
2581 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2582 0, 0, 0,
2583 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2584 Most local variable bindings are eliminated so that the default values
2585 become effective once more. Also, the syntax table is set from
2586 `standard-syntax-table', the local keymap is set to nil,
2587 and the abbrev table from `fundamental-mode-abbrev-table'.
2588 This function also forces redisplay of the mode line.
2590 Every function to select a new major mode starts by
2591 calling this function.
2593 As a special exception, local variables whose names have
2594 a non-nil `permanent-local' property are not eliminated by this function.
2596 The first thing this function does is run
2597 the normal hook `change-major-mode-hook'. */)
2600 if (!NILP (Vrun_hooks))
2601 call1 (Vrun_hooks, Qchange_major_mode_hook);
2603 /* Make sure none of the bindings in local_var_alist
2604 remain swapped in, in their symbols. */
2606 swap_out_buffer_local_variables (current_buffer);
2608 /* Actually eliminate all local bindings of this buffer. */
2610 reset_buffer_local_variables (current_buffer, 0);
2612 /* Force mode-line redisplay. Useful here because all major mode
2613 commands call this function. */
2614 update_mode_lines++;
2616 return Qnil;
2619 /* Make sure no local variables remain set up with buffer B
2620 for their current values. */
2622 static void
2623 swap_out_buffer_local_variables (b)
2624 struct buffer *b;
2626 Lisp_Object oalist, alist, sym, tem, buffer;
2628 XSETBUFFER (buffer, b);
2629 oalist = b->local_var_alist;
2631 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2633 sym = XCAR (XCAR (alist));
2635 /* Need not do anything if some other buffer's binding is now encached. */
2636 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2637 if (EQ (tem, buffer))
2639 /* Symbol is set up for this buffer's old local value:
2640 swap it out! */
2641 swap_in_global_binding (sym);
2646 /* Find all the overlays in the current buffer that contain position POS.
2647 Return the number found, and store them in a vector in *VEC_PTR.
2648 Store in *LEN_PTR the size allocated for the vector.
2649 Store in *NEXT_PTR the next position after POS where an overlay starts,
2650 or ZV if there are no more overlays between POS and ZV.
2651 Store in *PREV_PTR the previous position before POS where an overlay ends,
2652 or where an overlay starts which ends at or after POS;
2653 or BEGV if there are no such overlays from BEGV to POS.
2654 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2656 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2657 when this function is called.
2659 If EXTEND is non-zero, we make the vector bigger if necessary.
2660 If EXTEND is zero, we never extend the vector,
2661 and we store only as many overlays as will fit.
2662 But we still return the total number of overlays.
2664 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2665 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2666 default (BEGV or ZV). */
2669 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2670 EMACS_INT pos;
2671 int extend;
2672 Lisp_Object **vec_ptr;
2673 int *len_ptr;
2674 EMACS_INT *next_ptr;
2675 EMACS_INT *prev_ptr;
2676 int change_req;
2678 Lisp_Object overlay, start, end;
2679 struct Lisp_Overlay *tail;
2680 int idx = 0;
2681 int len = *len_ptr;
2682 Lisp_Object *vec = *vec_ptr;
2683 int next = ZV;
2684 int prev = BEGV;
2685 int inhibit_storing = 0;
2687 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2689 int startpos, endpos;
2691 XSETMISC (overlay, tail);
2693 start = OVERLAY_START (overlay);
2694 end = OVERLAY_END (overlay);
2695 endpos = OVERLAY_POSITION (end);
2696 if (endpos < pos)
2698 if (prev < endpos)
2699 prev = endpos;
2700 break;
2702 startpos = OVERLAY_POSITION (start);
2703 /* This one ends at or after POS
2704 so its start counts for PREV_PTR if it's before POS. */
2705 if (prev < startpos && startpos < pos)
2706 prev = startpos;
2707 if (endpos == pos)
2708 continue;
2709 if (startpos <= pos)
2711 if (idx == len)
2713 /* The supplied vector is full.
2714 Either make it bigger, or don't store any more in it. */
2715 if (extend)
2717 /* Make it work with an initial len == 0. */
2718 len *= 2;
2719 if (len == 0)
2720 len = 4;
2721 *len_ptr = len;
2722 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2723 *vec_ptr = vec;
2725 else
2726 inhibit_storing = 1;
2729 if (!inhibit_storing)
2730 vec[idx] = overlay;
2731 /* Keep counting overlays even if we can't return them all. */
2732 idx++;
2734 else if (startpos < next)
2735 next = startpos;
2738 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2740 int startpos, endpos;
2742 XSETMISC (overlay, tail);
2744 start = OVERLAY_START (overlay);
2745 end = OVERLAY_END (overlay);
2746 startpos = OVERLAY_POSITION (start);
2747 if (pos < startpos)
2749 if (startpos < next)
2750 next = startpos;
2751 break;
2753 endpos = OVERLAY_POSITION (end);
2754 if (pos < endpos)
2756 if (idx == len)
2758 if (extend)
2760 /* Make it work with an initial len == 0. */
2761 len *= 2;
2762 if (len == 0)
2763 len = 4;
2764 *len_ptr = len;
2765 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2766 *vec_ptr = vec;
2768 else
2769 inhibit_storing = 1;
2772 if (!inhibit_storing)
2773 vec[idx] = overlay;
2774 idx++;
2776 if (startpos < pos && startpos > prev)
2777 prev = startpos;
2779 else if (endpos < pos && endpos > prev)
2780 prev = endpos;
2781 else if (endpos == pos && startpos > prev
2782 && (!change_req || startpos < pos))
2783 prev = startpos;
2786 if (next_ptr)
2787 *next_ptr = next;
2788 if (prev_ptr)
2789 *prev_ptr = prev;
2790 return idx;
2793 /* Find all the overlays in the current buffer that overlap the range
2794 BEG-END, or are empty at BEG, or are empty at END provided END
2795 denotes the position at the end of the current buffer.
2797 Return the number found, and store them in a vector in *VEC_PTR.
2798 Store in *LEN_PTR the size allocated for the vector.
2799 Store in *NEXT_PTR the next position after POS where an overlay starts,
2800 or ZV if there are no more overlays.
2801 Store in *PREV_PTR the previous position before POS where an overlay ends,
2802 or BEGV if there are no previous overlays.
2803 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2805 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2806 when this function is called.
2808 If EXTEND is non-zero, we make the vector bigger if necessary.
2809 If EXTEND is zero, we never extend the vector,
2810 and we store only as many overlays as will fit.
2811 But we still return the total number of overlays. */
2813 static int
2814 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2815 int beg, end;
2816 int extend;
2817 Lisp_Object **vec_ptr;
2818 int *len_ptr;
2819 int *next_ptr;
2820 int *prev_ptr;
2822 Lisp_Object overlay, ostart, oend;
2823 struct Lisp_Overlay *tail;
2824 int idx = 0;
2825 int len = *len_ptr;
2826 Lisp_Object *vec = *vec_ptr;
2827 int next = ZV;
2828 int prev = BEGV;
2829 int inhibit_storing = 0;
2830 int end_is_Z = end == Z;
2832 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2834 int startpos, endpos;
2836 XSETMISC (overlay, tail);
2838 ostart = OVERLAY_START (overlay);
2839 oend = OVERLAY_END (overlay);
2840 endpos = OVERLAY_POSITION (oend);
2841 if (endpos < beg)
2843 if (prev < endpos)
2844 prev = endpos;
2845 break;
2847 startpos = OVERLAY_POSITION (ostart);
2848 /* Count an interval if it overlaps the range, is empty at the
2849 start of the range, or is empty at END provided END denotes the
2850 end of the buffer. */
2851 if ((beg < endpos && startpos < end)
2852 || (startpos == endpos
2853 && (beg == endpos || (end_is_Z && endpos == end))))
2855 if (idx == len)
2857 /* The supplied vector is full.
2858 Either make it bigger, or don't store any more in it. */
2859 if (extend)
2861 /* Make it work with an initial len == 0. */
2862 len *= 2;
2863 if (len == 0)
2864 len = 4;
2865 *len_ptr = len;
2866 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2867 *vec_ptr = vec;
2869 else
2870 inhibit_storing = 1;
2873 if (!inhibit_storing)
2874 vec[idx] = overlay;
2875 /* Keep counting overlays even if we can't return them all. */
2876 idx++;
2878 else if (startpos < next)
2879 next = startpos;
2882 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2884 int startpos, endpos;
2886 XSETMISC (overlay, tail);
2888 ostart = OVERLAY_START (overlay);
2889 oend = OVERLAY_END (overlay);
2890 startpos = OVERLAY_POSITION (ostart);
2891 if (end < startpos)
2893 if (startpos < next)
2894 next = startpos;
2895 break;
2897 endpos = OVERLAY_POSITION (oend);
2898 /* Count an interval if it overlaps the range, is empty at the
2899 start of the range, or is empty at END provided END denotes the
2900 end of the buffer. */
2901 if ((beg < endpos && startpos < end)
2902 || (startpos == endpos
2903 && (beg == endpos || (end_is_Z && endpos == end))))
2905 if (idx == len)
2907 if (extend)
2909 /* Make it work with an initial len == 0. */
2910 len *= 2;
2911 if (len == 0)
2912 len = 4;
2913 *len_ptr = len;
2914 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2915 *vec_ptr = vec;
2917 else
2918 inhibit_storing = 1;
2921 if (!inhibit_storing)
2922 vec[idx] = overlay;
2923 idx++;
2925 else if (endpos < beg && endpos > prev)
2926 prev = endpos;
2929 if (next_ptr)
2930 *next_ptr = next;
2931 if (prev_ptr)
2932 *prev_ptr = prev;
2933 return idx;
2937 /* Return non-zero if there exists an overlay with a non-nil
2938 `mouse-face' property overlapping OVERLAY. */
2941 mouse_face_overlay_overlaps (overlay)
2942 Lisp_Object overlay;
2944 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2945 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2946 int n, i, size;
2947 Lisp_Object *v, tem;
2949 size = 10;
2950 v = (Lisp_Object *) alloca (size * sizeof *v);
2951 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2952 if (n > size)
2954 v = (Lisp_Object *) alloca (n * sizeof *v);
2955 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2958 for (i = 0; i < n; ++i)
2959 if (!EQ (v[i], overlay)
2960 && (tem = Foverlay_get (overlay, Qmouse_face),
2961 !NILP (tem)))
2962 break;
2964 return i < n;
2969 /* Fast function to just test if we're at an overlay boundary. */
2971 overlay_touches_p (pos)
2972 int pos;
2974 Lisp_Object overlay;
2975 struct Lisp_Overlay *tail;
2977 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2979 int endpos;
2981 XSETMISC (overlay ,tail);
2982 if (!OVERLAYP (overlay))
2983 abort ();
2985 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2986 if (endpos < pos)
2987 break;
2988 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2989 return 1;
2992 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2994 int startpos;
2996 XSETMISC (overlay, tail);
2997 if (!OVERLAYP (overlay))
2998 abort ();
3000 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3001 if (pos < startpos)
3002 break;
3003 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3004 return 1;
3006 return 0;
3009 struct sortvec
3011 Lisp_Object overlay;
3012 int beg, end;
3013 int priority;
3016 static int
3017 compare_overlays (v1, v2)
3018 const void *v1, *v2;
3020 const struct sortvec *s1 = (const struct sortvec *) v1;
3021 const struct sortvec *s2 = (const struct sortvec *) v2;
3022 if (s1->priority != s2->priority)
3023 return s1->priority - s2->priority;
3024 if (s1->beg != s2->beg)
3025 return s1->beg - s2->beg;
3026 if (s1->end != s2->end)
3027 return s2->end - s1->end;
3028 return 0;
3031 /* Sort an array of overlays by priority. The array is modified in place.
3032 The return value is the new size; this may be smaller than the original
3033 size if some of the overlays were invalid or were window-specific. */
3035 sort_overlays (overlay_vec, noverlays, w)
3036 Lisp_Object *overlay_vec;
3037 int noverlays;
3038 struct window *w;
3040 int i, j;
3041 struct sortvec *sortvec;
3042 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3044 /* Put the valid and relevant overlays into sortvec. */
3046 for (i = 0, j = 0; i < noverlays; i++)
3048 Lisp_Object tem;
3049 Lisp_Object overlay;
3051 overlay = overlay_vec[i];
3052 if (OVERLAY_VALID (overlay)
3053 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3054 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3056 /* If we're interested in a specific window, then ignore
3057 overlays that are limited to some other window. */
3058 if (w)
3060 Lisp_Object window;
3062 window = Foverlay_get (overlay, Qwindow);
3063 if (WINDOWP (window) && XWINDOW (window) != w)
3064 continue;
3067 /* This overlay is good and counts: put it into sortvec. */
3068 sortvec[j].overlay = overlay;
3069 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3070 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3071 tem = Foverlay_get (overlay, Qpriority);
3072 if (INTEGERP (tem))
3073 sortvec[j].priority = XINT (tem);
3074 else
3075 sortvec[j].priority = 0;
3076 j++;
3079 noverlays = j;
3081 /* Sort the overlays into the proper order: increasing priority. */
3083 if (noverlays > 1)
3084 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3086 for (i = 0; i < noverlays; i++)
3087 overlay_vec[i] = sortvec[i].overlay;
3088 return (noverlays);
3091 struct sortstr
3093 Lisp_Object string, string2;
3094 int size;
3095 int priority;
3098 struct sortstrlist
3100 struct sortstr *buf; /* An array that expands as needed; never freed. */
3101 int size; /* Allocated length of that array. */
3102 int used; /* How much of the array is currently in use. */
3103 int bytes; /* Total length of the strings in buf. */
3106 /* Buffers for storing information about the overlays touching a given
3107 position. These could be automatic variables in overlay_strings, but
3108 it's more efficient to hold onto the memory instead of repeatedly
3109 allocating and freeing it. */
3110 static struct sortstrlist overlay_heads, overlay_tails;
3111 static unsigned char *overlay_str_buf;
3113 /* Allocated length of overlay_str_buf. */
3114 static int overlay_str_len;
3116 /* A comparison function suitable for passing to qsort. */
3117 static int
3118 cmp_for_strings (as1, as2)
3119 char *as1, *as2;
3121 struct sortstr *s1 = (struct sortstr *)as1;
3122 struct sortstr *s2 = (struct sortstr *)as2;
3123 if (s1->size != s2->size)
3124 return s2->size - s1->size;
3125 if (s1->priority != s2->priority)
3126 return s1->priority - s2->priority;
3127 return 0;
3130 static void
3131 record_overlay_string (ssl, str, str2, pri, size)
3132 struct sortstrlist *ssl;
3133 Lisp_Object str, str2, pri;
3134 int size;
3136 int nbytes;
3138 if (ssl->used == ssl->size)
3140 if (ssl->buf)
3141 ssl->size *= 2;
3142 else
3143 ssl->size = 5;
3144 ssl->buf = ((struct sortstr *)
3145 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3147 ssl->buf[ssl->used].string = str;
3148 ssl->buf[ssl->used].string2 = str2;
3149 ssl->buf[ssl->used].size = size;
3150 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3151 ssl->used++;
3153 if (NILP (current_buffer->enable_multibyte_characters))
3154 nbytes = SCHARS (str);
3155 else if (! STRING_MULTIBYTE (str))
3156 nbytes = count_size_as_multibyte (SDATA (str),
3157 SBYTES (str));
3158 else
3159 nbytes = SBYTES (str);
3161 ssl->bytes += nbytes;
3163 if (STRINGP (str2))
3165 if (NILP (current_buffer->enable_multibyte_characters))
3166 nbytes = SCHARS (str2);
3167 else if (! STRING_MULTIBYTE (str2))
3168 nbytes = count_size_as_multibyte (SDATA (str2),
3169 SBYTES (str2));
3170 else
3171 nbytes = SBYTES (str2);
3173 ssl->bytes += nbytes;
3177 /* Return the concatenation of the strings associated with overlays that
3178 begin or end at POS, ignoring overlays that are specific to a window
3179 other than W. The strings are concatenated in the appropriate order:
3180 shorter overlays nest inside longer ones, and higher priority inside
3181 lower. Normally all of the after-strings come first, but zero-sized
3182 overlays have their after-strings ride along with the before-strings
3183 because it would look strange to print them inside-out.
3185 Returns the string length, and stores the contents indirectly through
3186 PSTR, if that variable is non-null. The string may be overwritten by
3187 subsequent calls. */
3190 overlay_strings (pos, w, pstr)
3191 EMACS_INT pos;
3192 struct window *w;
3193 unsigned char **pstr;
3195 Lisp_Object overlay, window, str;
3196 struct Lisp_Overlay *ov;
3197 int startpos, endpos;
3198 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3200 overlay_heads.used = overlay_heads.bytes = 0;
3201 overlay_tails.used = overlay_tails.bytes = 0;
3202 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3204 XSETMISC (overlay, ov);
3205 eassert (OVERLAYP (overlay));
3207 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3208 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3209 if (endpos < pos)
3210 break;
3211 if (endpos != pos && startpos != pos)
3212 continue;
3213 window = Foverlay_get (overlay, Qwindow);
3214 if (WINDOWP (window) && XWINDOW (window) != w)
3215 continue;
3216 if (startpos == pos
3217 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3218 record_overlay_string (&overlay_heads, str,
3219 (startpos == endpos
3220 ? Foverlay_get (overlay, Qafter_string)
3221 : Qnil),
3222 Foverlay_get (overlay, Qpriority),
3223 endpos - startpos);
3224 else if (endpos == pos
3225 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3226 record_overlay_string (&overlay_tails, str, Qnil,
3227 Foverlay_get (overlay, Qpriority),
3228 endpos - startpos);
3230 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3232 XSETMISC (overlay, ov);
3233 eassert (OVERLAYP (overlay));
3235 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3236 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3237 if (startpos > pos)
3238 break;
3239 if (endpos != pos && startpos != pos)
3240 continue;
3241 window = Foverlay_get (overlay, Qwindow);
3242 if (WINDOWP (window) && XWINDOW (window) != w)
3243 continue;
3244 if (startpos == pos
3245 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3246 record_overlay_string (&overlay_heads, str,
3247 (startpos == endpos
3248 ? Foverlay_get (overlay, Qafter_string)
3249 : Qnil),
3250 Foverlay_get (overlay, Qpriority),
3251 endpos - startpos);
3252 else if (endpos == pos
3253 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3254 record_overlay_string (&overlay_tails, str, Qnil,
3255 Foverlay_get (overlay, Qpriority),
3256 endpos - startpos);
3258 if (overlay_tails.used > 1)
3259 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3260 cmp_for_strings);
3261 if (overlay_heads.used > 1)
3262 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3263 cmp_for_strings);
3264 if (overlay_heads.bytes || overlay_tails.bytes)
3266 Lisp_Object tem;
3267 int i;
3268 unsigned char *p;
3269 int total = overlay_heads.bytes + overlay_tails.bytes;
3271 if (total > overlay_str_len)
3273 overlay_str_len = total;
3274 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3275 total);
3277 p = overlay_str_buf;
3278 for (i = overlay_tails.used; --i >= 0;)
3280 int nbytes;
3281 tem = overlay_tails.buf[i].string;
3282 nbytes = copy_text (SDATA (tem), p,
3283 SBYTES (tem),
3284 STRING_MULTIBYTE (tem), multibyte);
3285 p += nbytes;
3287 for (i = 0; i < overlay_heads.used; ++i)
3289 int nbytes;
3290 tem = overlay_heads.buf[i].string;
3291 nbytes = copy_text (SDATA (tem), p,
3292 SBYTES (tem),
3293 STRING_MULTIBYTE (tem), multibyte);
3294 p += nbytes;
3295 tem = overlay_heads.buf[i].string2;
3296 if (STRINGP (tem))
3298 nbytes = copy_text (SDATA (tem), p,
3299 SBYTES (tem),
3300 STRING_MULTIBYTE (tem), multibyte);
3301 p += nbytes;
3304 if (p != overlay_str_buf + total)
3305 abort ();
3306 if (pstr)
3307 *pstr = overlay_str_buf;
3308 return total;
3310 return 0;
3313 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3315 void
3316 recenter_overlay_lists (buf, pos)
3317 struct buffer *buf;
3318 EMACS_INT pos;
3320 Lisp_Object overlay, beg, end;
3321 struct Lisp_Overlay *prev, *tail, *next;
3323 /* See if anything in overlays_before should move to overlays_after. */
3325 /* We don't strictly need prev in this loop; it should always be nil.
3326 But we use it for symmetry and in case that should cease to be true
3327 with some future change. */
3328 prev = NULL;
3329 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3331 next = tail->next;
3332 XSETMISC (overlay, tail);
3334 /* If the overlay is not valid, get rid of it. */
3335 if (!OVERLAY_VALID (overlay))
3336 #if 1
3337 abort ();
3338 #else
3340 /* Splice the cons cell TAIL out of overlays_before. */
3341 if (!NILP (prev))
3342 XCDR (prev) = next;
3343 else
3344 buf->overlays_before = next;
3345 tail = prev;
3346 continue;
3348 #endif
3350 beg = OVERLAY_START (overlay);
3351 end = OVERLAY_END (overlay);
3353 if (OVERLAY_POSITION (end) > pos)
3355 /* OVERLAY needs to be moved. */
3356 int where = OVERLAY_POSITION (beg);
3357 struct Lisp_Overlay *other, *other_prev;
3359 /* Splice the cons cell TAIL out of overlays_before. */
3360 if (prev)
3361 prev->next = next;
3362 else
3363 buf->overlays_before = next;
3365 /* Search thru overlays_after for where to put it. */
3366 other_prev = NULL;
3367 for (other = buf->overlays_after; other;
3368 other_prev = other, other = other->next)
3370 Lisp_Object otherbeg, otheroverlay;
3372 XSETMISC (otheroverlay, other);
3373 eassert (OVERLAY_VALID (otheroverlay));
3375 otherbeg = OVERLAY_START (otheroverlay);
3376 if (OVERLAY_POSITION (otherbeg) >= where)
3377 break;
3380 /* Add TAIL to overlays_after before OTHER. */
3381 tail->next = other;
3382 if (other_prev)
3383 other_prev->next = tail;
3384 else
3385 buf->overlays_after = tail;
3386 tail = prev;
3388 else
3389 /* We've reached the things that should stay in overlays_before.
3390 All the rest of overlays_before must end even earlier,
3391 so stop now. */
3392 break;
3395 /* See if anything in overlays_after should be in overlays_before. */
3396 prev = NULL;
3397 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3399 next = tail->next;
3400 XSETMISC (overlay, tail);
3402 /* If the overlay is not valid, get rid of it. */
3403 if (!OVERLAY_VALID (overlay))
3404 #if 1
3405 abort ();
3406 #else
3408 /* Splice the cons cell TAIL out of overlays_after. */
3409 if (!NILP (prev))
3410 XCDR (prev) = next;
3411 else
3412 buf->overlays_after = next;
3413 tail = prev;
3414 continue;
3416 #endif
3418 beg = OVERLAY_START (overlay);
3419 end = OVERLAY_END (overlay);
3421 /* Stop looking, when we know that nothing further
3422 can possibly end before POS. */
3423 if (OVERLAY_POSITION (beg) > pos)
3424 break;
3426 if (OVERLAY_POSITION (end) <= pos)
3428 /* OVERLAY needs to be moved. */
3429 int where = OVERLAY_POSITION (end);
3430 struct Lisp_Overlay *other, *other_prev;
3432 /* Splice the cons cell TAIL out of overlays_after. */
3433 if (prev)
3434 prev->next = next;
3435 else
3436 buf->overlays_after = next;
3438 /* Search thru overlays_before for where to put it. */
3439 other_prev = NULL;
3440 for (other = buf->overlays_before; other;
3441 other_prev = other, other = other->next)
3443 Lisp_Object otherend, otheroverlay;
3445 XSETMISC (otheroverlay, other);
3446 eassert (OVERLAY_VALID (otheroverlay));
3448 otherend = OVERLAY_END (otheroverlay);
3449 if (OVERLAY_POSITION (otherend) <= where)
3450 break;
3453 /* Add TAIL to overlays_before before OTHER. */
3454 tail->next = other;
3455 if (other_prev)
3456 other_prev->next = tail;
3457 else
3458 buf->overlays_before = tail;
3459 tail = prev;
3463 buf->overlay_center = pos;
3466 void
3467 adjust_overlays_for_insert (pos, length)
3468 EMACS_INT pos;
3469 EMACS_INT length;
3471 /* After an insertion, the lists are still sorted properly,
3472 but we may need to update the value of the overlay center. */
3473 if (current_buffer->overlay_center >= pos)
3474 current_buffer->overlay_center += length;
3477 void
3478 adjust_overlays_for_delete (pos, length)
3479 EMACS_INT pos;
3480 EMACS_INT length;
3482 if (current_buffer->overlay_center < pos)
3483 /* The deletion was to our right. No change needed; the before- and
3484 after-lists are still consistent. */
3486 else if (current_buffer->overlay_center > pos + length)
3487 /* The deletion was to our left. We need to adjust the center value
3488 to account for the change in position, but the lists are consistent
3489 given the new value. */
3490 current_buffer->overlay_center -= length;
3491 else
3492 /* We're right in the middle. There might be things on the after-list
3493 that now belong on the before-list. Recentering will move them,
3494 and also update the center point. */
3495 recenter_overlay_lists (current_buffer, pos);
3498 /* Fix up overlays that were garbled as a result of permuting markers
3499 in the range START through END. Any overlay with at least one
3500 endpoint in this range will need to be unlinked from the overlay
3501 list and reinserted in its proper place.
3502 Such an overlay might even have negative size at this point.
3503 If so, we'll make the overlay empty. */
3504 void
3505 fix_start_end_in_overlays (start, end)
3506 register int start, end;
3508 Lisp_Object overlay;
3509 struct Lisp_Overlay *before_list, *after_list;
3510 /* These are either nil, indicating that before_list or after_list
3511 should be assigned, or the cons cell the cdr of which should be
3512 assigned. */
3513 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3514 /* 'Parent', likewise, indicates a cons cell or
3515 current_buffer->overlays_before or overlays_after, depending
3516 which loop we're in. */
3517 struct Lisp_Overlay *tail, *parent;
3518 int startpos, endpos;
3520 /* This algorithm shifts links around instead of consing and GCing.
3521 The loop invariant is that before_list (resp. after_list) is a
3522 well-formed list except that its last element, the CDR of beforep
3523 (resp. afterp) if beforep (afterp) isn't nil or before_list
3524 (after_list) if it is, is still uninitialized. So it's not a bug
3525 that before_list isn't initialized, although it may look
3526 strange. */
3527 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3529 XSETMISC (overlay, tail);
3531 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3532 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3534 /* If the overlay is backwards, make it empty. */
3535 if (endpos < startpos)
3537 startpos = endpos;
3538 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3539 Qnil);
3542 if (endpos < start)
3543 break;
3545 if (endpos < end
3546 || (startpos >= start && startpos < end))
3548 /* Add it to the end of the wrong list. Later on,
3549 recenter_overlay_lists will move it to the right place. */
3550 if (endpos < current_buffer->overlay_center)
3552 if (!afterp)
3553 after_list = tail;
3554 else
3555 afterp->next = tail;
3556 afterp = tail;
3558 else
3560 if (!beforep)
3561 before_list = tail;
3562 else
3563 beforep->next = tail;
3564 beforep = tail;
3566 if (!parent)
3567 current_buffer->overlays_before = tail->next;
3568 else
3569 parent->next = tail->next;
3570 tail = tail->next;
3572 else
3573 parent = tail, tail = parent->next;
3575 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3577 XSETMISC (overlay, tail);
3579 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3580 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3582 /* If the overlay is backwards, make it empty. */
3583 if (endpos < startpos)
3585 startpos = endpos;
3586 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3587 Qnil);
3590 if (startpos >= end)
3591 break;
3593 if (startpos >= start
3594 || (endpos >= start && endpos < end))
3596 if (endpos < current_buffer->overlay_center)
3598 if (!afterp)
3599 after_list = tail;
3600 else
3601 afterp->next = tail;
3602 afterp = tail;
3604 else
3606 if (!beforep)
3607 before_list = tail;
3608 else
3609 beforep->next = tail;
3610 beforep = tail;
3612 if (!parent)
3613 current_buffer->overlays_after = tail->next;
3614 else
3615 parent->next = tail->next;
3616 tail = tail->next;
3618 else
3619 parent = tail, tail = parent->next;
3622 /* Splice the constructed (wrong) lists into the buffer's lists,
3623 and let the recenter function make it sane again. */
3624 if (beforep)
3626 beforep->next = current_buffer->overlays_before;
3627 current_buffer->overlays_before = before_list;
3629 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3631 if (afterp)
3633 afterp->next = current_buffer->overlays_after;
3634 current_buffer->overlays_after = after_list;
3636 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3639 /* We have two types of overlay: the one whose ending marker is
3640 after-insertion-marker (this is the usual case) and the one whose
3641 ending marker is before-insertion-marker. When `overlays_before'
3642 contains overlays of the latter type and the former type in this
3643 order and both overlays end at inserting position, inserting a text
3644 increases only the ending marker of the latter type, which results
3645 in incorrect ordering of `overlays_before'.
3647 This function fixes ordering of overlays in the slot
3648 `overlays_before' of the buffer *BP. Before the insertion, `point'
3649 was at PREV, and now is at POS. */
3651 void
3652 fix_overlays_before (bp, prev, pos)
3653 struct buffer *bp;
3654 EMACS_INT prev, pos;
3656 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3657 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3658 Lisp_Object tem;
3659 EMACS_INT end;
3661 /* After the insertion, the several overlays may be in incorrect
3662 order. The possibility is that, in the list `overlays_before',
3663 an overlay which ends at POS appears after an overlay which ends
3664 at PREV. Since POS is greater than PREV, we must fix the
3665 ordering of these overlays, by moving overlays ends at POS before
3666 the overlays ends at PREV. */
3668 /* At first, find a place where disordered overlays should be linked
3669 in. It is where an overlay which end before POS exists. (i.e. an
3670 overlay whose ending marker is after-insertion-marker if disorder
3671 exists). */
3672 while (tail
3673 && (XSETMISC (tem, tail),
3674 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3676 parent = tail;
3677 tail = tail->next;
3680 /* If we don't find such an overlay,
3681 or the found one ends before PREV,
3682 or the found one is the last one in the list,
3683 we don't have to fix anything. */
3684 if (!tail || end < prev || !tail->next)
3685 return;
3687 right_pair = parent;
3688 parent = tail;
3689 tail = tail->next;
3691 /* Now, end position of overlays in the list TAIL should be before
3692 or equal to PREV. In the loop, an overlay which ends at POS is
3693 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3694 we found an overlay which ends before PREV, the remaining
3695 overlays are in correct order. */
3696 while (tail)
3698 XSETMISC (tem, tail);
3699 end = OVERLAY_POSITION (OVERLAY_END (tem));
3701 if (end == pos)
3702 { /* This overlay is disordered. */
3703 struct Lisp_Overlay *found = tail;
3705 /* Unlink the found overlay. */
3706 tail = found->next;
3707 parent->next = tail;
3708 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3709 and link it into the right place. */
3710 if (!right_pair)
3712 found->next = bp->overlays_before;
3713 bp->overlays_before = found;
3715 else
3717 found->next = right_pair->next;
3718 right_pair->next = found;
3721 else if (end == prev)
3723 parent = tail;
3724 tail = tail->next;
3726 else /* No more disordered overlay. */
3727 break;
3731 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3732 doc: /* Return t if OBJECT is an overlay. */)
3733 (object)
3734 Lisp_Object object;
3736 return (OVERLAYP (object) ? Qt : Qnil);
3739 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3740 doc: /* Create a new overlay with range BEG to END in BUFFER.
3741 If omitted, BUFFER defaults to the current buffer.
3742 BEG and END may be integers or markers.
3743 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3744 for the front of the overlay advance when text is inserted there
3745 \(which means the text *is not* included in the overlay).
3746 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3747 for the rear of the overlay advance when text is inserted there
3748 \(which means the text *is* included in the overlay). */)
3749 (beg, end, buffer, front_advance, rear_advance)
3750 Lisp_Object beg, end, buffer;
3751 Lisp_Object front_advance, rear_advance;
3753 Lisp_Object overlay;
3754 struct buffer *b;
3756 if (NILP (buffer))
3757 XSETBUFFER (buffer, current_buffer);
3758 else
3759 CHECK_BUFFER (buffer);
3760 if (MARKERP (beg)
3761 && ! EQ (Fmarker_buffer (beg), buffer))
3762 error ("Marker points into wrong buffer");
3763 if (MARKERP (end)
3764 && ! EQ (Fmarker_buffer (end), buffer))
3765 error ("Marker points into wrong buffer");
3767 CHECK_NUMBER_COERCE_MARKER (beg);
3768 CHECK_NUMBER_COERCE_MARKER (end);
3770 if (XINT (beg) > XINT (end))
3772 Lisp_Object temp;
3773 temp = beg; beg = end; end = temp;
3776 b = XBUFFER (buffer);
3778 beg = Fset_marker (Fmake_marker (), beg, buffer);
3779 end = Fset_marker (Fmake_marker (), end, buffer);
3781 if (!NILP (front_advance))
3782 XMARKER (beg)->insertion_type = 1;
3783 if (!NILP (rear_advance))
3784 XMARKER (end)->insertion_type = 1;
3786 overlay = allocate_misc ();
3787 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3788 XOVERLAY (overlay)->start = beg;
3789 XOVERLAY (overlay)->end = end;
3790 XOVERLAY (overlay)->plist = Qnil;
3791 XOVERLAY (overlay)->next = NULL;
3793 /* Put the new overlay on the wrong list. */
3794 end = OVERLAY_END (overlay);
3795 if (OVERLAY_POSITION (end) < b->overlay_center)
3797 if (b->overlays_after)
3798 XOVERLAY (overlay)->next = b->overlays_after;
3799 b->overlays_after = XOVERLAY (overlay);
3801 else
3803 if (b->overlays_before)
3804 XOVERLAY (overlay)->next = b->overlays_before;
3805 b->overlays_before = XOVERLAY (overlay);
3808 /* This puts it in the right list, and in the right order. */
3809 recenter_overlay_lists (b, b->overlay_center);
3811 /* We don't need to redisplay the region covered by the overlay, because
3812 the overlay has no properties at the moment. */
3814 return overlay;
3817 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3819 static void
3820 modify_overlay (buf, start, end)
3821 struct buffer *buf;
3822 EMACS_INT start, end;
3824 if (start > end)
3826 int temp = start;
3827 start = end;
3828 end = temp;
3831 BUF_COMPUTE_UNCHANGED (buf, start, end);
3833 /* If this is a buffer not in the selected window,
3834 we must do other windows. */
3835 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3836 windows_or_buffers_changed = 1;
3837 /* If multiple windows show this buffer, we must do other windows. */
3838 else if (buffer_shared > 1)
3839 windows_or_buffers_changed = 1;
3840 /* If we modify an overlay at the end of the buffer, we cannot
3841 be sure that window end is still valid. */
3842 else if (end >= ZV && start <= ZV)
3843 windows_or_buffers_changed = 1;
3845 ++BUF_OVERLAY_MODIFF (buf);
3849 Lisp_Object Fdelete_overlay ();
3851 static struct Lisp_Overlay *
3852 unchain_overlay (list, overlay)
3853 struct Lisp_Overlay *list, *overlay;
3855 struct Lisp_Overlay *tmp, *prev;
3856 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3857 if (tmp == overlay)
3859 if (prev)
3860 prev->next = tmp->next;
3861 else
3862 list = tmp->next;
3863 overlay->next = NULL;
3864 break;
3866 return list;
3869 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3870 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3871 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3872 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3873 buffer. */)
3874 (overlay, beg, end, buffer)
3875 Lisp_Object overlay, beg, end, buffer;
3877 struct buffer *b, *ob;
3878 Lisp_Object obuffer;
3879 int count = SPECPDL_INDEX ();
3881 CHECK_OVERLAY (overlay);
3882 if (NILP (buffer))
3883 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3884 if (NILP (buffer))
3885 XSETBUFFER (buffer, current_buffer);
3886 CHECK_BUFFER (buffer);
3888 if (MARKERP (beg)
3889 && ! EQ (Fmarker_buffer (beg), buffer))
3890 error ("Marker points into wrong buffer");
3891 if (MARKERP (end)
3892 && ! EQ (Fmarker_buffer (end), buffer))
3893 error ("Marker points into wrong buffer");
3895 CHECK_NUMBER_COERCE_MARKER (beg);
3896 CHECK_NUMBER_COERCE_MARKER (end);
3898 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3899 return Fdelete_overlay (overlay);
3901 if (XINT (beg) > XINT (end))
3903 Lisp_Object temp;
3904 temp = beg; beg = end; end = temp;
3907 specbind (Qinhibit_quit, Qt);
3909 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3910 b = XBUFFER (buffer);
3911 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3913 /* If the overlay has changed buffers, do a thorough redisplay. */
3914 if (!EQ (buffer, obuffer))
3916 /* Redisplay where the overlay was. */
3917 if (!NILP (obuffer))
3919 int o_beg;
3920 int o_end;
3922 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3923 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3925 modify_overlay (ob, o_beg, o_end);
3928 /* Redisplay where the overlay is going to be. */
3929 modify_overlay (b, XINT (beg), XINT (end));
3931 else
3932 /* Redisplay the area the overlay has just left, or just enclosed. */
3934 int o_beg, o_end;
3936 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3937 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3939 if (o_beg == XINT (beg))
3940 modify_overlay (b, o_end, XINT (end));
3941 else if (o_end == XINT (end))
3942 modify_overlay (b, o_beg, XINT (beg));
3943 else
3945 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3946 if (XINT (end) > o_end) o_end = XINT (end);
3947 modify_overlay (b, o_beg, o_end);
3951 if (!NILP (obuffer))
3953 ob->overlays_before
3954 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3955 ob->overlays_after
3956 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3957 eassert (XOVERLAY (overlay)->next == NULL);
3960 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3961 Fset_marker (OVERLAY_END (overlay), end, buffer);
3963 /* Put the overlay on the wrong list. */
3964 end = OVERLAY_END (overlay);
3965 if (OVERLAY_POSITION (end) < b->overlay_center)
3967 XOVERLAY (overlay)->next = b->overlays_after;
3968 b->overlays_after = XOVERLAY (overlay);
3970 else
3972 XOVERLAY (overlay)->next = b->overlays_before;
3973 b->overlays_before = XOVERLAY (overlay);
3976 /* This puts it in the right list, and in the right order. */
3977 recenter_overlay_lists (b, b->overlay_center);
3979 return unbind_to (count, overlay);
3982 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3983 doc: /* Delete the overlay OVERLAY from its buffer. */)
3984 (overlay)
3985 Lisp_Object overlay;
3987 Lisp_Object buffer;
3988 struct buffer *b;
3989 int count = SPECPDL_INDEX ();
3991 CHECK_OVERLAY (overlay);
3993 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3994 if (NILP (buffer))
3995 return Qnil;
3997 b = XBUFFER (buffer);
3998 specbind (Qinhibit_quit, Qt);
4000 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4001 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4002 eassert (XOVERLAY (overlay)->next == NULL);
4003 modify_overlay (b,
4004 marker_position (OVERLAY_START (overlay)),
4005 marker_position (OVERLAY_END (overlay)));
4006 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4007 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4009 /* When deleting an overlay with before or after strings, turn off
4010 display optimizations for the affected buffer, on the basis that
4011 these strings may contain newlines. This is easier to do than to
4012 check for that situation during redisplay. */
4013 if (!windows_or_buffers_changed
4014 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4015 || !NILP (Foverlay_get (overlay, Qafter_string))))
4016 b->prevent_redisplay_optimizations_p = 1;
4018 return unbind_to (count, Qnil);
4021 /* Overlay dissection functions. */
4023 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4024 doc: /* Return the position at which OVERLAY starts. */)
4025 (overlay)
4026 Lisp_Object overlay;
4028 CHECK_OVERLAY (overlay);
4030 return (Fmarker_position (OVERLAY_START (overlay)));
4033 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4034 doc: /* Return the position at which OVERLAY ends. */)
4035 (overlay)
4036 Lisp_Object overlay;
4038 CHECK_OVERLAY (overlay);
4040 return (Fmarker_position (OVERLAY_END (overlay)));
4043 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4044 doc: /* Return the buffer OVERLAY belongs to.
4045 Return nil if OVERLAY has been deleted. */)
4046 (overlay)
4047 Lisp_Object overlay;
4049 CHECK_OVERLAY (overlay);
4051 return Fmarker_buffer (OVERLAY_START (overlay));
4054 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4055 doc: /* Return a list of the properties on OVERLAY.
4056 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4057 OVERLAY. */)
4058 (overlay)
4059 Lisp_Object overlay;
4061 CHECK_OVERLAY (overlay);
4063 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4067 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4068 doc: /* Return a list of the overlays that contain the character at POS. */)
4069 (pos)
4070 Lisp_Object pos;
4072 int noverlays;
4073 Lisp_Object *overlay_vec;
4074 int len;
4075 Lisp_Object result;
4077 CHECK_NUMBER_COERCE_MARKER (pos);
4079 len = 10;
4080 /* We can't use alloca here because overlays_at can call xrealloc. */
4081 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4083 /* Put all the overlays we want in a vector in overlay_vec.
4084 Store the length in len. */
4085 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4086 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4088 /* Make a list of them all. */
4089 result = Flist (noverlays, overlay_vec);
4091 xfree (overlay_vec);
4092 return result;
4095 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4096 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4097 Overlap means that at least one character is contained within the overlay
4098 and also contained within the specified region.
4099 Empty overlays are included in the result if they are located at BEG,
4100 between BEG and END, or at END provided END denotes the position at the
4101 end of the buffer. */)
4102 (beg, end)
4103 Lisp_Object beg, end;
4105 int noverlays;
4106 Lisp_Object *overlay_vec;
4107 int len;
4108 Lisp_Object result;
4110 CHECK_NUMBER_COERCE_MARKER (beg);
4111 CHECK_NUMBER_COERCE_MARKER (end);
4113 len = 10;
4114 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4116 /* Put all the overlays we want in a vector in overlay_vec.
4117 Store the length in len. */
4118 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4119 (int *) 0, (int *) 0);
4121 /* Make a list of them all. */
4122 result = Flist (noverlays, overlay_vec);
4124 xfree (overlay_vec);
4125 return result;
4128 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4129 1, 1, 0,
4130 doc: /* Return the next position after POS where an overlay starts or ends.
4131 If there are no overlay boundaries from POS to (point-max),
4132 the value is (point-max). */)
4133 (pos)
4134 Lisp_Object pos;
4136 int noverlays;
4137 EMACS_INT endpos;
4138 Lisp_Object *overlay_vec;
4139 int len;
4140 int i;
4142 CHECK_NUMBER_COERCE_MARKER (pos);
4144 len = 10;
4145 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4147 /* Put all the overlays we want in a vector in overlay_vec.
4148 Store the length in len.
4149 endpos gets the position where the next overlay starts. */
4150 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4151 &endpos, (EMACS_INT *) 0, 1);
4153 /* If any of these overlays ends before endpos,
4154 use its ending point instead. */
4155 for (i = 0; i < noverlays; i++)
4157 Lisp_Object oend;
4158 EMACS_INT oendpos;
4160 oend = OVERLAY_END (overlay_vec[i]);
4161 oendpos = OVERLAY_POSITION (oend);
4162 if (oendpos < endpos)
4163 endpos = oendpos;
4166 xfree (overlay_vec);
4167 return make_number (endpos);
4170 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4171 Sprevious_overlay_change, 1, 1, 0,
4172 doc: /* Return the previous position before POS where an overlay starts or ends.
4173 If there are no overlay boundaries from (point-min) to POS,
4174 the value is (point-min). */)
4175 (pos)
4176 Lisp_Object pos;
4178 int noverlays;
4179 EMACS_INT prevpos;
4180 Lisp_Object *overlay_vec;
4181 int len;
4183 CHECK_NUMBER_COERCE_MARKER (pos);
4185 /* At beginning of buffer, we know the answer;
4186 avoid bug subtracting 1 below. */
4187 if (XINT (pos) == BEGV)
4188 return pos;
4190 len = 10;
4191 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4193 /* Put all the overlays we want in a vector in overlay_vec.
4194 Store the length in len.
4195 prevpos gets the position of the previous change. */
4196 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4197 (EMACS_INT *) 0, &prevpos, 1);
4199 xfree (overlay_vec);
4200 return make_number (prevpos);
4203 /* These functions are for debugging overlays. */
4205 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4206 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4207 The car has all the overlays before the overlay center;
4208 the cdr has all the overlays after the overlay center.
4209 Recentering overlays moves overlays between these lists.
4210 The lists you get are copies, so that changing them has no effect.
4211 However, the overlays you get are the real objects that the buffer uses. */)
4214 struct Lisp_Overlay *ol;
4215 Lisp_Object before = Qnil, after = Qnil, tmp;
4216 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4218 XSETMISC (tmp, ol);
4219 before = Fcons (tmp, before);
4221 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4223 XSETMISC (tmp, ol);
4224 after = Fcons (tmp, after);
4226 return Fcons (Fnreverse (before), Fnreverse (after));
4229 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4230 doc: /* Recenter the overlays of the current buffer around position POS.
4231 That makes overlay lookup faster for positions near POS (but perhaps slower
4232 for positions far away from POS). */)
4233 (pos)
4234 Lisp_Object pos;
4236 CHECK_NUMBER_COERCE_MARKER (pos);
4238 recenter_overlay_lists (current_buffer, XINT (pos));
4239 return Qnil;
4242 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4243 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4244 (overlay, prop)
4245 Lisp_Object overlay, prop;
4247 CHECK_OVERLAY (overlay);
4248 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4251 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4252 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4253 (overlay, prop, value)
4254 Lisp_Object overlay, prop, value;
4256 Lisp_Object tail, buffer;
4257 int changed;
4259 CHECK_OVERLAY (overlay);
4261 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4263 for (tail = XOVERLAY (overlay)->plist;
4264 CONSP (tail) && CONSP (XCDR (tail));
4265 tail = XCDR (XCDR (tail)))
4266 if (EQ (XCAR (tail), prop))
4268 changed = !EQ (XCAR (XCDR (tail)), value);
4269 XSETCAR (XCDR (tail), value);
4270 goto found;
4272 /* It wasn't in the list, so add it to the front. */
4273 changed = !NILP (value);
4274 XOVERLAY (overlay)->plist
4275 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4276 found:
4277 if (! NILP (buffer))
4279 if (changed)
4280 modify_overlay (XBUFFER (buffer),
4281 marker_position (OVERLAY_START (overlay)),
4282 marker_position (OVERLAY_END (overlay)));
4283 if (EQ (prop, Qevaporate) && ! NILP (value)
4284 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4285 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4286 Fdelete_overlay (overlay);
4289 return value;
4292 /* Subroutine of report_overlay_modification. */
4294 /* Lisp vector holding overlay hook functions to call.
4295 Vector elements come in pairs.
4296 Each even-index element is a list of hook functions.
4297 The following odd-index element is the overlay they came from.
4299 Before the buffer change, we fill in this vector
4300 as we call overlay hook functions.
4301 After the buffer change, we get the functions to call from this vector.
4302 This way we always call the same functions before and after the change. */
4303 static Lisp_Object last_overlay_modification_hooks;
4305 /* Number of elements actually used in last_overlay_modification_hooks. */
4306 static int last_overlay_modification_hooks_used;
4308 /* Add one functionlist/overlay pair
4309 to the end of last_overlay_modification_hooks. */
4311 static void
4312 add_overlay_mod_hooklist (functionlist, overlay)
4313 Lisp_Object functionlist, overlay;
4315 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4317 if (last_overlay_modification_hooks_used == oldsize)
4318 last_overlay_modification_hooks = larger_vector
4319 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4320 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4321 functionlist); last_overlay_modification_hooks_used++;
4322 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4323 overlay); last_overlay_modification_hooks_used++;
4326 /* Run the modification-hooks of overlays that include
4327 any part of the text in START to END.
4328 If this change is an insertion, also
4329 run the insert-before-hooks of overlay starting at END,
4330 and the insert-after-hooks of overlay ending at START.
4332 This is called both before and after the modification.
4333 AFTER is nonzero when we call after the modification.
4335 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4336 When AFTER is nonzero, they are the start position,
4337 the position after the inserted new text,
4338 and the length of deleted or replaced old text. */
4340 void
4341 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4342 Lisp_Object start, end;
4343 int after;
4344 Lisp_Object arg1, arg2, arg3;
4346 Lisp_Object prop, overlay;
4347 struct Lisp_Overlay *tail;
4348 /* 1 if this change is an insertion. */
4349 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4350 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4352 overlay = Qnil;
4353 tail = NULL;
4355 /* We used to run the functions as soon as we found them and only register
4356 them in last_overlay_modification_hooks for the purpose of the `after'
4357 case. But running elisp code as we traverse the list of overlays is
4358 painful because the list can be modified by the elisp code so we had to
4359 copy at several places. We now simply do a read-only traversal that
4360 only collects the functions to run and we run them afterwards. It's
4361 simpler, especially since all the code was already there. -stef */
4363 if (!after)
4365 /* We are being called before a change.
4366 Scan the overlays to find the functions to call. */
4367 last_overlay_modification_hooks_used = 0;
4368 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4370 int startpos, endpos;
4371 Lisp_Object ostart, oend;
4373 XSETMISC (overlay, tail);
4375 ostart = OVERLAY_START (overlay);
4376 oend = OVERLAY_END (overlay);
4377 endpos = OVERLAY_POSITION (oend);
4378 if (XFASTINT (start) > endpos)
4379 break;
4380 startpos = OVERLAY_POSITION (ostart);
4381 if (insertion && (XFASTINT (start) == startpos
4382 || XFASTINT (end) == startpos))
4384 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4385 if (!NILP (prop))
4386 add_overlay_mod_hooklist (prop, overlay);
4388 if (insertion && (XFASTINT (start) == endpos
4389 || XFASTINT (end) == endpos))
4391 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4392 if (!NILP (prop))
4393 add_overlay_mod_hooklist (prop, overlay);
4395 /* Test for intersecting intervals. This does the right thing
4396 for both insertion and deletion. */
4397 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4399 prop = Foverlay_get (overlay, Qmodification_hooks);
4400 if (!NILP (prop))
4401 add_overlay_mod_hooklist (prop, overlay);
4405 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4407 int startpos, endpos;
4408 Lisp_Object ostart, oend;
4410 XSETMISC (overlay, tail);
4412 ostart = OVERLAY_START (overlay);
4413 oend = OVERLAY_END (overlay);
4414 startpos = OVERLAY_POSITION (ostart);
4415 endpos = OVERLAY_POSITION (oend);
4416 if (XFASTINT (end) < startpos)
4417 break;
4418 if (insertion && (XFASTINT (start) == startpos
4419 || XFASTINT (end) == startpos))
4421 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4422 if (!NILP (prop))
4423 add_overlay_mod_hooklist (prop, overlay);
4425 if (insertion && (XFASTINT (start) == endpos
4426 || XFASTINT (end) == endpos))
4428 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4429 if (!NILP (prop))
4430 add_overlay_mod_hooklist (prop, overlay);
4432 /* Test for intersecting intervals. This does the right thing
4433 for both insertion and deletion. */
4434 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4436 prop = Foverlay_get (overlay, Qmodification_hooks);
4437 if (!NILP (prop))
4438 add_overlay_mod_hooklist (prop, overlay);
4443 GCPRO4 (overlay, arg1, arg2, arg3);
4445 /* Call the functions recorded in last_overlay_modification_hooks.
4446 First copy the vector contents, in case some of these hooks
4447 do subsequent modification of the buffer. */
4448 int size = last_overlay_modification_hooks_used;
4449 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4450 int i;
4452 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4453 copy, size * sizeof (Lisp_Object));
4454 gcpro1.var = copy;
4455 gcpro1.nvars = size;
4457 for (i = 0; i < size;)
4459 Lisp_Object prop, overlay;
4460 prop = copy[i++];
4461 overlay = copy[i++];
4462 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4465 UNGCPRO;
4468 static void
4469 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4470 Lisp_Object list, overlay;
4471 int after;
4472 Lisp_Object arg1, arg2, arg3;
4474 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4476 GCPRO4 (list, arg1, arg2, arg3);
4478 while (CONSP (list))
4480 if (NILP (arg3))
4481 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4482 else
4483 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4484 list = XCDR (list);
4486 UNGCPRO;
4489 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4490 property is set. */
4491 void
4492 evaporate_overlays (pos)
4493 EMACS_INT pos;
4495 Lisp_Object overlay, hit_list;
4496 struct Lisp_Overlay *tail;
4498 hit_list = Qnil;
4499 if (pos <= current_buffer->overlay_center)
4500 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4502 int endpos;
4503 XSETMISC (overlay, tail);
4504 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4505 if (endpos < pos)
4506 break;
4507 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4508 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4509 hit_list = Fcons (overlay, hit_list);
4511 else
4512 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4514 int startpos;
4515 XSETMISC (overlay, tail);
4516 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4517 if (startpos > pos)
4518 break;
4519 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4520 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4521 hit_list = Fcons (overlay, hit_list);
4523 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4524 Fdelete_overlay (XCAR (hit_list));
4527 /* Somebody has tried to store a value with an unacceptable type
4528 in the slot with offset OFFSET. */
4530 void
4531 buffer_slot_type_mismatch (newval, type)
4532 Lisp_Object newval;
4533 int type;
4535 Lisp_Object predicate;
4537 switch (type)
4539 case Lisp_Int: predicate = Qintegerp; break;
4540 case Lisp_String: predicate = Qstringp; break;
4541 case Lisp_Symbol: predicate = Qsymbolp; break;
4542 default: abort ();
4545 wrong_type_argument (predicate, newval);
4549 /***********************************************************************
4550 Allocation with mmap
4551 ***********************************************************************/
4553 #ifdef USE_MMAP_FOR_BUFFERS
4555 #include <sys/types.h>
4556 #include <sys/mman.h>
4558 #ifndef MAP_ANON
4559 #ifdef MAP_ANONYMOUS
4560 #define MAP_ANON MAP_ANONYMOUS
4561 #else
4562 #define MAP_ANON 0
4563 #endif
4564 #endif
4566 #ifndef MAP_FAILED
4567 #define MAP_FAILED ((void *) -1)
4568 #endif
4570 #include <stdio.h>
4571 #include <errno.h>
4573 #if MAP_ANON == 0
4574 #include <fcntl.h>
4575 #endif
4577 #include "coding.h"
4580 /* Memory is allocated in regions which are mapped using mmap(2).
4581 The current implementation lets the system select mapped
4582 addresses; we're not using MAP_FIXED in general, except when
4583 trying to enlarge regions.
4585 Each mapped region starts with a mmap_region structure, the user
4586 area starts after that structure, aligned to MEM_ALIGN.
4588 +-----------------------+
4589 | struct mmap_info + |
4590 | padding |
4591 +-----------------------+
4592 | user data |
4595 +-----------------------+ */
4597 struct mmap_region
4599 /* User-specified size. */
4600 size_t nbytes_specified;
4602 /* Number of bytes mapped */
4603 size_t nbytes_mapped;
4605 /* Pointer to the location holding the address of the memory
4606 allocated with the mmap'd block. The variable actually points
4607 after this structure. */
4608 POINTER_TYPE **var;
4610 /* Next and previous in list of all mmap'd regions. */
4611 struct mmap_region *next, *prev;
4614 /* Doubly-linked list of mmap'd regions. */
4616 static struct mmap_region *mmap_regions;
4618 /* File descriptor for mmap. If we don't have anonymous mapping,
4619 /dev/zero will be opened on it. */
4621 static int mmap_fd;
4623 /* Temporary storage for mmap_set_vars, see there. */
4625 static struct mmap_region *mmap_regions_1;
4626 static int mmap_fd_1;
4628 /* Page size on this system. */
4630 static int mmap_page_size;
4632 /* 1 means mmap has been intialized. */
4634 static int mmap_initialized_p;
4636 /* Value is X rounded up to the next multiple of N. */
4638 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4640 /* Size of mmap_region structure plus padding. */
4642 #define MMAP_REGION_STRUCT_SIZE \
4643 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4645 /* Given a pointer P to the start of the user-visible part of a mapped
4646 region, return a pointer to the start of the region. */
4648 #define MMAP_REGION(P) \
4649 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4651 /* Given a pointer P to the start of a mapped region, return a pointer
4652 to the start of the user-visible part of the region. */
4654 #define MMAP_USER_AREA(P) \
4655 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4657 #define MEM_ALIGN sizeof (double)
4659 /* Predicate returning true if part of the address range [START .. END]
4660 is currently mapped. Used to prevent overwriting an existing
4661 memory mapping.
4663 Default is to conservativly assume the address range is occupied by
4664 something else. This can be overridden by system configuration
4665 files if system-specific means to determine this exists. */
4667 #ifndef MMAP_ALLOCATED_P
4668 #define MMAP_ALLOCATED_P(start, end) 1
4669 #endif
4671 /* Function prototypes. */
4673 static int mmap_free_1 P_ ((struct mmap_region *));
4674 static int mmap_enlarge P_ ((struct mmap_region *, int));
4675 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4676 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4677 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4678 static void mmap_free P_ ((POINTER_TYPE **ptr));
4679 static void mmap_init P_ ((void));
4682 /* Return a region overlapping address range START...END, or null if
4683 none. END is not including, i.e. the last byte in the range
4684 is at END - 1. */
4686 static struct mmap_region *
4687 mmap_find (start, end)
4688 POINTER_TYPE *start, *end;
4690 struct mmap_region *r;
4691 char *s = (char *) start, *e = (char *) end;
4693 for (r = mmap_regions; r; r = r->next)
4695 char *rstart = (char *) r;
4696 char *rend = rstart + r->nbytes_mapped;
4698 if (/* First byte of range, i.e. START, in this region? */
4699 (s >= rstart && s < rend)
4700 /* Last byte of range, i.e. END - 1, in this region? */
4701 || (e > rstart && e <= rend)
4702 /* First byte of this region in the range? */
4703 || (rstart >= s && rstart < e)
4704 /* Last byte of this region in the range? */
4705 || (rend > s && rend <= e))
4706 break;
4709 return r;
4713 /* Unmap a region. P is a pointer to the start of the user-araa of
4714 the region. Value is non-zero if successful. */
4716 static int
4717 mmap_free_1 (r)
4718 struct mmap_region *r;
4720 if (r->next)
4721 r->next->prev = r->prev;
4722 if (r->prev)
4723 r->prev->next = r->next;
4724 else
4725 mmap_regions = r->next;
4727 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4729 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4730 return 0;
4733 return 1;
4737 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4738 Value is non-zero if successful. */
4740 static int
4741 mmap_enlarge (r, npages)
4742 struct mmap_region *r;
4743 int npages;
4745 char *region_end = (char *) r + r->nbytes_mapped;
4746 size_t nbytes;
4747 int success = 0;
4749 if (npages < 0)
4751 /* Unmap pages at the end of the region. */
4752 nbytes = - npages * mmap_page_size;
4753 if (munmap (region_end - nbytes, nbytes) == -1)
4754 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4755 else
4757 r->nbytes_mapped -= nbytes;
4758 success = 1;
4761 else if (npages > 0)
4763 nbytes = npages * mmap_page_size;
4765 /* Try to map additional pages at the end of the region. We
4766 cannot do this if the address range is already occupied by
4767 something else because mmap deletes any previous mapping.
4768 I'm not sure this is worth doing, let's see. */
4769 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4771 POINTER_TYPE *p;
4773 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4774 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4775 if (p == MAP_FAILED)
4776 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4777 else if (p != (POINTER_TYPE *) region_end)
4779 /* Kernels are free to choose a different address. In
4780 that case, unmap what we've mapped above; we have
4781 no use for it. */
4782 if (munmap (p, nbytes) == -1)
4783 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4785 else
4787 r->nbytes_mapped += nbytes;
4788 success = 1;
4793 return success;
4797 /* Set or reset variables holding references to mapped regions. If
4798 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4799 non-zero, set all variables to the start of the user-areas
4800 of mapped regions.
4802 This function is called from Fdump_emacs to ensure that the dumped
4803 Emacs doesn't contain references to memory that won't be mapped
4804 when Emacs starts. */
4806 void
4807 mmap_set_vars (restore_p)
4808 int restore_p;
4810 struct mmap_region *r;
4812 if (restore_p)
4814 mmap_regions = mmap_regions_1;
4815 mmap_fd = mmap_fd_1;
4816 for (r = mmap_regions; r; r = r->next)
4817 *r->var = MMAP_USER_AREA (r);
4819 else
4821 for (r = mmap_regions; r; r = r->next)
4822 *r->var = NULL;
4823 mmap_regions_1 = mmap_regions;
4824 mmap_regions = NULL;
4825 mmap_fd_1 = mmap_fd;
4826 mmap_fd = -1;
4831 /* Allocate a block of storage large enough to hold NBYTES bytes of
4832 data. A pointer to the data is returned in *VAR. VAR is thus the
4833 address of some variable which will use the data area.
4835 The allocation of 0 bytes is valid.
4837 If we can't allocate the necessary memory, set *VAR to null, and
4838 return null. */
4840 static POINTER_TYPE *
4841 mmap_alloc (var, nbytes)
4842 POINTER_TYPE **var;
4843 size_t nbytes;
4845 void *p;
4846 size_t map;
4848 mmap_init ();
4850 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4851 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4852 mmap_fd, 0);
4854 if (p == MAP_FAILED)
4856 if (errno != ENOMEM)
4857 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4858 p = NULL;
4860 else
4862 struct mmap_region *r = (struct mmap_region *) p;
4864 r->nbytes_specified = nbytes;
4865 r->nbytes_mapped = map;
4866 r->var = var;
4867 r->prev = NULL;
4868 r->next = mmap_regions;
4869 if (r->next)
4870 r->next->prev = r;
4871 mmap_regions = r;
4873 p = MMAP_USER_AREA (p);
4876 return *var = p;
4880 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4881 resize it to size NBYTES. Change *VAR to reflect the new block,
4882 and return this value. If more memory cannot be allocated, then
4883 leave *VAR unchanged, and return null. */
4885 static POINTER_TYPE *
4886 mmap_realloc (var, nbytes)
4887 POINTER_TYPE **var;
4888 size_t nbytes;
4890 POINTER_TYPE *result;
4892 mmap_init ();
4894 if (*var == NULL)
4895 result = mmap_alloc (var, nbytes);
4896 else if (nbytes == 0)
4898 mmap_free (var);
4899 result = mmap_alloc (var, nbytes);
4901 else
4903 struct mmap_region *r = MMAP_REGION (*var);
4904 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4906 if (room < nbytes)
4908 /* Must enlarge. */
4909 POINTER_TYPE *old_ptr = *var;
4911 /* Try to map additional pages at the end of the region.
4912 If that fails, allocate a new region, copy data
4913 from the old region, then free it. */
4914 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4915 / mmap_page_size)))
4917 r->nbytes_specified = nbytes;
4918 *var = result = old_ptr;
4920 else if (mmap_alloc (var, nbytes))
4922 bcopy (old_ptr, *var, r->nbytes_specified);
4923 mmap_free_1 (MMAP_REGION (old_ptr));
4924 result = *var;
4925 r = MMAP_REGION (result);
4926 r->nbytes_specified = nbytes;
4928 else
4930 *var = old_ptr;
4931 result = NULL;
4934 else if (room - nbytes >= mmap_page_size)
4936 /* Shrinking by at least a page. Let's give some
4937 memory back to the system.
4939 The extra parens are to make the division happens first,
4940 on positive values, so we know it will round towards
4941 zero. */
4942 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4943 result = *var;
4944 r->nbytes_specified = nbytes;
4946 else
4948 /* Leave it alone. */
4949 result = *var;
4950 r->nbytes_specified = nbytes;
4954 return result;
4958 /* Free a block of relocatable storage whose data is pointed to by
4959 PTR. Store 0 in *PTR to show there's no block allocated. */
4961 static void
4962 mmap_free (var)
4963 POINTER_TYPE **var;
4965 mmap_init ();
4967 if (*var)
4969 mmap_free_1 (MMAP_REGION (*var));
4970 *var = NULL;
4975 /* Perform necessary intializations for the use of mmap. */
4977 static void
4978 mmap_init ()
4980 #if MAP_ANON == 0
4981 /* The value of mmap_fd is initially 0 in temacs, and -1
4982 in a dumped Emacs. */
4983 if (mmap_fd <= 0)
4985 /* No anonymous mmap -- we need the file descriptor. */
4986 mmap_fd = open ("/dev/zero", O_RDONLY);
4987 if (mmap_fd == -1)
4988 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4990 #endif /* MAP_ANON == 0 */
4992 if (mmap_initialized_p)
4993 return;
4994 mmap_initialized_p = 1;
4996 #if MAP_ANON != 0
4997 mmap_fd = -1;
4998 #endif
5000 mmap_page_size = getpagesize ();
5003 #endif /* USE_MMAP_FOR_BUFFERS */
5007 /***********************************************************************
5008 Buffer-text Allocation
5009 ***********************************************************************/
5011 #ifdef REL_ALLOC
5012 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5013 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5014 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5015 #endif /* REL_ALLOC */
5018 /* Allocate NBYTES bytes for buffer B's text buffer. */
5020 static void
5021 alloc_buffer_text (b, nbytes)
5022 struct buffer *b;
5023 size_t nbytes;
5025 POINTER_TYPE *p;
5027 BLOCK_INPUT;
5028 #if defined USE_MMAP_FOR_BUFFERS
5029 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5030 #elif defined REL_ALLOC
5031 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5032 #else
5033 p = xmalloc (nbytes);
5034 #endif
5036 if (p == NULL)
5038 UNBLOCK_INPUT;
5039 memory_full ();
5042 b->text->beg = (unsigned char *) p;
5043 UNBLOCK_INPUT;
5046 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5047 shrink it. */
5049 void
5050 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5052 POINTER_TYPE *p;
5053 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5054 + delta);
5055 BLOCK_INPUT;
5056 #if defined USE_MMAP_FOR_BUFFERS
5057 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5058 #elif defined REL_ALLOC
5059 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5060 #else
5061 p = xrealloc (b->text->beg, nbytes);
5062 #endif
5064 if (p == NULL)
5066 UNBLOCK_INPUT;
5067 memory_full ();
5070 BUF_BEG_ADDR (b) = (unsigned char *) p;
5071 UNBLOCK_INPUT;
5075 /* Free buffer B's text buffer. */
5077 static void
5078 free_buffer_text (b)
5079 struct buffer *b;
5081 BLOCK_INPUT;
5083 #if defined USE_MMAP_FOR_BUFFERS
5084 mmap_free ((POINTER_TYPE **) &b->text->beg);
5085 #elif defined REL_ALLOC
5086 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5087 #else
5088 xfree (b->text->beg);
5089 #endif
5091 BUF_BEG_ADDR (b) = NULL;
5092 UNBLOCK_INPUT;
5097 /***********************************************************************
5098 Initialization
5099 ***********************************************************************/
5101 void
5102 init_buffer_once ()
5104 int idx;
5106 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5108 /* Make sure all markable slots in buffer_defaults
5109 are initialized reasonably, so mark_buffer won't choke. */
5110 reset_buffer (&buffer_defaults);
5111 reset_buffer_local_variables (&buffer_defaults, 1);
5112 reset_buffer (&buffer_local_symbols);
5113 reset_buffer_local_variables (&buffer_local_symbols, 1);
5114 /* Prevent GC from getting confused. */
5115 buffer_defaults.text = &buffer_defaults.own_text;
5116 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5117 BUF_INTERVALS (&buffer_defaults) = 0;
5118 BUF_INTERVALS (&buffer_local_symbols) = 0;
5119 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5120 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5121 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5122 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5124 /* Set up the default values of various buffer slots. */
5125 /* Must do these before making the first buffer! */
5127 /* real setup is done in bindings.el */
5128 buffer_defaults.mode_line_format = build_string ("%-");
5129 buffer_defaults.header_line_format = Qnil;
5130 buffer_defaults.abbrev_mode = Qnil;
5131 buffer_defaults.overwrite_mode = Qnil;
5132 buffer_defaults.case_fold_search = Qt;
5133 buffer_defaults.auto_fill_function = Qnil;
5134 buffer_defaults.selective_display = Qnil;
5135 #ifndef old
5136 buffer_defaults.selective_display_ellipses = Qt;
5137 #endif
5138 buffer_defaults.abbrev_table = Qnil;
5139 buffer_defaults.display_table = Qnil;
5140 buffer_defaults.undo_list = Qnil;
5141 buffer_defaults.mark_active = Qnil;
5142 buffer_defaults.file_format = Qnil;
5143 buffer_defaults.auto_save_file_format = Qt;
5144 buffer_defaults.overlays_before = NULL;
5145 buffer_defaults.overlays_after = NULL;
5146 buffer_defaults.overlay_center = BEG;
5148 XSETFASTINT (buffer_defaults.tab_width, 8);
5149 buffer_defaults.truncate_lines = Qnil;
5150 buffer_defaults.word_wrap = Qnil;
5151 buffer_defaults.ctl_arrow = Qt;
5152 buffer_defaults.direction_reversed = Qnil;
5153 buffer_defaults.cursor_type = Qt;
5154 buffer_defaults.extra_line_spacing = Qnil;
5155 buffer_defaults.cursor_in_non_selected_windows = Qt;
5157 #ifdef DOS_NT
5158 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5159 #endif
5160 buffer_defaults.enable_multibyte_characters = Qt;
5161 buffer_defaults.buffer_file_coding_system = Qnil;
5162 XSETFASTINT (buffer_defaults.fill_column, 70);
5163 XSETFASTINT (buffer_defaults.left_margin, 0);
5164 buffer_defaults.cache_long_line_scans = Qnil;
5165 buffer_defaults.file_truename = Qnil;
5166 XSETFASTINT (buffer_defaults.display_count, 0);
5167 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5168 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5169 buffer_defaults.left_fringe_width = Qnil;
5170 buffer_defaults.right_fringe_width = Qnil;
5171 buffer_defaults.fringes_outside_margins = Qnil;
5172 buffer_defaults.scroll_bar_width = Qnil;
5173 buffer_defaults.vertical_scroll_bar_type = Qt;
5174 buffer_defaults.indicate_empty_lines = Qnil;
5175 buffer_defaults.indicate_buffer_boundaries = Qnil;
5176 buffer_defaults.fringe_indicator_alist = Qnil;
5177 buffer_defaults.fringe_cursor_alist = Qnil;
5178 buffer_defaults.scroll_up_aggressively = Qnil;
5179 buffer_defaults.scroll_down_aggressively = Qnil;
5180 buffer_defaults.display_time = Qnil;
5182 /* Assign the local-flags to the slots that have default values.
5183 The local flag is a bit that is used in the buffer
5184 to say that it has its own local value for the slot.
5185 The local flag bits are in the local_var_flags slot of the buffer. */
5187 /* Nothing can work if this isn't true */
5188 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5190 /* 0 means not a lisp var, -1 means always local, else mask */
5191 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5192 XSETINT (buffer_local_flags.filename, -1);
5193 XSETINT (buffer_local_flags.directory, -1);
5194 XSETINT (buffer_local_flags.backed_up, -1);
5195 XSETINT (buffer_local_flags.save_length, -1);
5196 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5197 XSETINT (buffer_local_flags.read_only, -1);
5198 XSETINT (buffer_local_flags.major_mode, -1);
5199 XSETINT (buffer_local_flags.mode_name, -1);
5200 XSETINT (buffer_local_flags.undo_list, -1);
5201 XSETINT (buffer_local_flags.mark_active, -1);
5202 XSETINT (buffer_local_flags.point_before_scroll, -1);
5203 XSETINT (buffer_local_flags.file_truename, -1);
5204 XSETINT (buffer_local_flags.invisibility_spec, -1);
5205 XSETINT (buffer_local_flags.file_format, -1);
5206 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5207 XSETINT (buffer_local_flags.display_count, -1);
5208 XSETINT (buffer_local_flags.display_time, -1);
5209 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5211 idx = 1;
5212 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5213 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5214 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5215 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5216 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5217 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5218 #ifndef old
5219 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5220 #endif
5221 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5222 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5223 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5224 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5225 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5226 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5227 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5228 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5229 #ifdef DOS_NT
5230 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5231 /* Make this one a permanent local. */
5232 buffer_permanent_local_flags[idx++] = 1;
5233 #endif
5234 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5235 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5236 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5237 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5238 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5239 /* Make this one a permanent local. */
5240 buffer_permanent_local_flags[idx++] = 1;
5241 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5242 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5243 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5244 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5245 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5246 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5247 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5248 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5249 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5250 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5251 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5252 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5253 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5254 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5255 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5256 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5257 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5259 /* Need more room? */
5260 if (idx >= MAX_PER_BUFFER_VARS)
5261 abort ();
5262 last_per_buffer_idx = idx;
5264 Vbuffer_alist = Qnil;
5265 current_buffer = 0;
5266 all_buffers = 0;
5268 QSFundamental = build_string ("Fundamental");
5270 Qfundamental_mode = intern ("fundamental-mode");
5271 buffer_defaults.major_mode = Qfundamental_mode;
5273 Qmode_class = intern ("mode-class");
5275 Qprotected_field = intern ("protected-field");
5277 Qpermanent_local = intern ("permanent-local");
5279 Qkill_buffer_hook = intern ("kill-buffer-hook");
5280 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5282 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5284 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5286 /* super-magic invisible buffer */
5287 Vbuffer_alist = Qnil;
5289 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5291 inhibit_modification_hooks = 0;
5294 void
5295 init_buffer ()
5297 char *pwd;
5298 Lisp_Object temp;
5299 int len;
5301 #ifdef USE_MMAP_FOR_BUFFERS
5303 /* When using the ralloc implementation based on mmap(2), buffer
5304 text pointers will have been set to null in the dumped Emacs.
5305 Map new memory. */
5306 struct buffer *b;
5308 for (b = all_buffers; b; b = b->next)
5309 if (b->text->beg == NULL)
5310 enlarge_buffer_text (b, 0);
5312 #endif /* USE_MMAP_FOR_BUFFERS */
5314 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5315 if (NILP (buffer_defaults.enable_multibyte_characters))
5316 Fset_buffer_multibyte (Qnil);
5318 pwd = get_current_dir_name ();
5320 if (!pwd)
5321 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5323 /* Maybe this should really use some standard subroutine
5324 whose definition is filename syntax dependent. */
5325 len = strlen (pwd);
5326 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5328 /* Grow buffer to add directory separator and '\0'. */
5329 pwd = (char *) xrealloc (pwd, len + 2);
5330 pwd[len] = DIRECTORY_SEP;
5331 pwd[len + 1] = '\0';
5334 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5335 if (! NILP (buffer_defaults.enable_multibyte_characters))
5336 /* At this moment, we still don't know how to decode the
5337 directory name. So, we keep the bytes in multibyte form so
5338 that ENCODE_FILE correctly gets the original bytes. */
5339 current_buffer->directory
5340 = string_to_multibyte (current_buffer->directory);
5342 /* Add /: to the front of the name
5343 if it would otherwise be treated as magic. */
5344 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5345 if (! NILP (temp)
5346 /* If the default dir is just /, TEMP is non-nil
5347 because of the ange-ftp completion handler.
5348 However, it is not necessary to turn / into /:/.
5349 So avoid doing that. */
5350 && strcmp ("/", SDATA (current_buffer->directory)))
5351 current_buffer->directory
5352 = concat2 (build_string ("/:"), current_buffer->directory);
5354 temp = get_minibuffer (0);
5355 XBUFFER (temp)->directory = current_buffer->directory;
5357 free (pwd);
5360 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5361 Object stored in the current buffer. address is the address of the slot
5362 in the buffer that is current now. */
5364 /* TYPE is nil for a general Lisp variable.
5365 An integer specifies a type; then only LIsp values
5366 with that type code are allowed (except that nil is allowed too).
5367 LNAME is the LIsp-level variable name.
5368 VNAME is the name of the buffer slot.
5369 DOC is a dummy where you write the doc string as a comment. */
5370 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5371 defvar_per_buffer (lname, vname, type, 0)
5373 static void
5374 defvar_per_buffer (namestring, address, type, doc)
5375 char *namestring;
5376 Lisp_Object *address;
5377 Lisp_Object type;
5378 char *doc;
5380 Lisp_Object sym, val;
5381 int offset;
5383 sym = intern (namestring);
5384 val = allocate_misc ();
5385 offset = (char *)address - (char *)current_buffer;
5387 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5388 XBUFFER_OBJFWD (val)->offset = offset;
5389 XBUFFER_OBJFWD (val)->slottype = type;
5390 SET_SYMBOL_VALUE (sym, val);
5391 PER_BUFFER_SYMBOL (offset) = sym;
5393 if (PER_BUFFER_IDX (offset) == 0)
5394 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5395 slot of buffer_local_flags */
5396 abort ();
5400 /* initialize the buffer routines */
5401 void
5402 syms_of_buffer ()
5404 staticpro (&last_overlay_modification_hooks);
5405 last_overlay_modification_hooks
5406 = Fmake_vector (make_number (10), Qnil);
5408 staticpro (&Vbuffer_defaults);
5409 staticpro (&Vbuffer_local_symbols);
5410 staticpro (&Qfundamental_mode);
5411 staticpro (&Qmode_class);
5412 staticpro (&QSFundamental);
5413 staticpro (&Vbuffer_alist);
5414 staticpro (&Qprotected_field);
5415 staticpro (&Qpermanent_local);
5416 Qpermanent_local_hook = intern ("permanent-local-hook");
5417 staticpro (&Qpermanent_local_hook);
5418 staticpro (&Qkill_buffer_hook);
5419 Qoverlayp = intern ("overlayp");
5420 staticpro (&Qoverlayp);
5421 Qevaporate = intern ("evaporate");
5422 staticpro (&Qevaporate);
5423 Qmodification_hooks = intern ("modification-hooks");
5424 staticpro (&Qmodification_hooks);
5425 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5426 staticpro (&Qinsert_in_front_hooks);
5427 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5428 staticpro (&Qinsert_behind_hooks);
5429 Qget_file_buffer = intern ("get-file-buffer");
5430 staticpro (&Qget_file_buffer);
5431 Qpriority = intern ("priority");
5432 staticpro (&Qpriority);
5433 Qwindow = intern ("window");
5434 staticpro (&Qwindow);
5435 Qbefore_string = intern ("before-string");
5436 staticpro (&Qbefore_string);
5437 Qafter_string = intern ("after-string");
5438 staticpro (&Qafter_string);
5439 Qfirst_change_hook = intern ("first-change-hook");
5440 staticpro (&Qfirst_change_hook);
5441 Qbefore_change_functions = intern ("before-change-functions");
5442 staticpro (&Qbefore_change_functions);
5443 Qafter_change_functions = intern ("after-change-functions");
5444 staticpro (&Qafter_change_functions);
5445 /* The next one is initialized in init_buffer_once. */
5446 staticpro (&Qucs_set_table_for_input);
5448 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5449 staticpro (&Qkill_buffer_query_functions);
5451 Fput (Qprotected_field, Qerror_conditions,
5452 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5453 Fput (Qprotected_field, Qerror_message,
5454 build_string ("Attempt to modify a protected field"));
5456 /* All these use DEFVAR_LISP_NOPRO because the slots in
5457 buffer_defaults will all be marked via Vbuffer_defaults. */
5459 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5460 &buffer_defaults.mode_line_format,
5461 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5462 This is the same as (default-value 'mode-line-format). */);
5464 DEFVAR_LISP_NOPRO ("default-header-line-format",
5465 &buffer_defaults.header_line_format,
5466 doc: /* Default value of `header-line-format' for buffers that don't override it.
5467 This is the same as (default-value 'header-line-format). */);
5469 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5470 doc: /* Default value of `cursor-type' for buffers that don't override it.
5471 This is the same as (default-value 'cursor-type). */);
5473 DEFVAR_LISP_NOPRO ("default-line-spacing",
5474 &buffer_defaults.extra_line_spacing,
5475 doc: /* Default value of `line-spacing' for buffers that don't override it.
5476 This is the same as (default-value 'line-spacing). */);
5478 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5479 &buffer_defaults.cursor_in_non_selected_windows,
5480 doc: /* Default value of `cursor-in-non-selected-windows'.
5481 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5483 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5484 &buffer_defaults.abbrev_mode,
5485 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5486 This is the same as (default-value 'abbrev-mode). */);
5488 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5489 &buffer_defaults.ctl_arrow,
5490 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5491 This is the same as (default-value 'ctl-arrow). */);
5493 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5494 &buffer_defaults.direction_reversed,
5495 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5496 This is the same as (default-value 'direction-reversed). */);
5498 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5499 &buffer_defaults.enable_multibyte_characters,
5500 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5501 This is the same as (default-value 'enable-multibyte-characters). */);
5503 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5504 &buffer_defaults.buffer_file_coding_system,
5505 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5506 This is the same as (default-value 'buffer-file-coding-system). */);
5508 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5509 &buffer_defaults.truncate_lines,
5510 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5511 This is the same as (default-value 'truncate-lines). */);
5513 DEFVAR_LISP_NOPRO ("default-fill-column",
5514 &buffer_defaults.fill_column,
5515 doc: /* Default value of `fill-column' for buffers that do not override it.
5516 This is the same as (default-value 'fill-column). */);
5518 DEFVAR_LISP_NOPRO ("default-left-margin",
5519 &buffer_defaults.left_margin,
5520 doc: /* Default value of `left-margin' for buffers that do not override it.
5521 This is the same as (default-value 'left-margin). */);
5523 DEFVAR_LISP_NOPRO ("default-tab-width",
5524 &buffer_defaults.tab_width,
5525 doc: /* Default value of `tab-width' for buffers that do not override it.
5526 This is the same as (default-value 'tab-width). */);
5528 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5529 &buffer_defaults.case_fold_search,
5530 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5531 This is the same as (default-value 'case-fold-search). */);
5533 #ifdef DOS_NT
5534 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5535 &buffer_defaults.buffer_file_type,
5536 doc: /* Default file type for buffers that do not override it.
5537 This is the same as (default-value 'buffer-file-type).
5538 The file type is nil for text, t for binary. */);
5539 #endif
5541 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5542 &buffer_defaults.left_margin_cols,
5543 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5544 This is the same as (default-value 'left-margin-width). */);
5546 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5547 &buffer_defaults.right_margin_cols,
5548 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5549 This is the same as (default-value 'right-margin-width). */);
5551 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5552 &buffer_defaults.left_fringe_width,
5553 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5554 This is the same as (default-value 'left-fringe-width). */);
5556 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5557 &buffer_defaults.right_fringe_width,
5558 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5559 This is the same as (default-value 'right-fringe-width). */);
5561 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5562 &buffer_defaults.fringes_outside_margins,
5563 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5564 This is the same as (default-value 'fringes-outside-margins). */);
5566 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5567 &buffer_defaults.scroll_bar_width,
5568 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5569 This is the same as (default-value 'scroll-bar-width). */);
5571 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5572 &buffer_defaults.vertical_scroll_bar_type,
5573 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5574 This is the same as (default-value 'vertical-scroll-bar). */);
5576 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5577 &buffer_defaults.indicate_empty_lines,
5578 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5579 This is the same as (default-value 'indicate-empty-lines). */);
5581 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5582 &buffer_defaults.indicate_buffer_boundaries,
5583 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5584 This is the same as (default-value 'indicate-buffer-boundaries). */);
5586 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5587 &buffer_defaults.fringe_indicator_alist,
5588 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5589 This is the same as (default-value 'fringe-indicator-alist'). */);
5591 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5592 &buffer_defaults.fringe_cursor_alist,
5593 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5594 This is the same as (default-value 'fringe-cursor-alist'). */);
5596 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5597 &buffer_defaults.scroll_up_aggressively,
5598 doc: /* Default value of `scroll-up-aggressively'.
5599 This value applies in buffers that don't have their own local values.
5600 This is the same as (default-value 'scroll-up-aggressively). */);
5602 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5603 &buffer_defaults.scroll_down_aggressively,
5604 doc: /* Default value of `scroll-down-aggressively'.
5605 This value applies in buffers that don't have their own local values.
5606 This is the same as (default-value 'scroll-down-aggressively). */);
5608 DEFVAR_PER_BUFFER ("header-line-format",
5609 &current_buffer->header_line_format,
5610 Qnil,
5611 doc: /* Analogous to `mode-line-format', but controls the header line.
5612 The header line appears, optionally, at the top of a window;
5613 the mode line appears at the bottom. */);
5615 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5616 Qnil,
5617 doc: /* Template for displaying mode line for current buffer.
5618 Each buffer has its own value of this variable.
5619 Value may be nil, a string, a symbol or a list or cons cell.
5620 A value of nil means don't display a mode line.
5621 For a symbol, its value is used (but it is ignored if t or nil).
5622 A string appearing directly as the value of a symbol is processed verbatim
5623 in that the %-constructs below are not recognized.
5624 Note that unless the symbol is marked as a `risky-local-variable', all
5625 properties in any strings, as well as all :eval and :propertize forms
5626 in the value of that symbol will be ignored.
5627 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5628 is used as a mode line element. Be careful--FORM should not load any files,
5629 because that can cause an infinite recursion.
5630 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5631 with the specified properties PROPS applied.
5632 For a list whose car is a symbol, the symbol's value is taken,
5633 and if that is non-nil, the cadr of the list is processed recursively.
5634 Otherwise, the caddr of the list (if there is one) is processed.
5635 For a list whose car is a string or list, each element is processed
5636 recursively and the results are effectively concatenated.
5637 For a list whose car is an integer, the cdr of the list is processed
5638 and padded (if the number is positive) or truncated (if negative)
5639 to the width specified by that number.
5640 A string is printed verbatim in the mode line except for %-constructs:
5641 (%-constructs are allowed when the string is the entire mode-line-format
5642 or when it is found in a cons-cell or a list)
5643 %b -- print buffer name. %f -- print visited file name.
5644 %F -- print frame name.
5645 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5646 %& is like %*, but ignore read-only-ness.
5647 % means buffer is read-only and * means it is modified.
5648 For a modified read-only buffer, %* gives % and %+ gives *.
5649 %s -- print process status. %l -- print the current line number.
5650 %c -- print the current column number (this makes editing slower).
5651 To make the column number update correctly in all cases,
5652 `column-number-mode' must be non-nil.
5653 %i -- print the size of the buffer.
5654 %I -- like %i, but use k, M, G, etc., to abbreviate.
5655 %p -- print percent of buffer above top of window, or Top, Bot or All.
5656 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5657 or print Bottom or All.
5658 %n -- print Narrow if appropriate.
5659 %t -- visited file is text or binary (if OS supports this distinction).
5660 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5661 %Z -- like %z, but including the end-of-line format.
5662 %e -- print error message about full memory.
5663 %@ -- print @ or hyphen. @ means that default-directory is on a
5664 remote machine.
5665 %[ -- print one [ for each recursive editing level. %] similar.
5666 %% -- print %. %- -- print infinitely many dashes.
5667 Decimal digits after the % specify field width to which to pad. */);
5669 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5670 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5671 A value of nil means use current buffer's major mode,
5672 provided it is not marked as "special".
5674 When a mode is used by default, `find-file' switches to it
5675 before it reads the contents into the buffer and before
5676 it finishes setting up the buffer. Thus, the mode and
5677 its hooks should not expect certain variables such as
5678 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5680 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5681 make_number (Lisp_Symbol),
5682 doc: /* Symbol for current buffer's major mode. */);
5684 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5685 Qnil,
5686 doc: /* Pretty name of current buffer's major mode.
5687 Usually a string, but can use any of the constructs for `mode-line-format',
5688 which see.
5689 Format with `format-mode-line' to produce a string value. */);
5691 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5692 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5694 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5695 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5697 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5698 Qnil,
5699 doc: /* *Non-nil if searches and matches should ignore case. */);
5701 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5702 make_number (Lisp_Int),
5703 doc: /* *Column beyond which automatic line-wrapping should happen.
5704 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5706 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5707 make_number (Lisp_Int),
5708 doc: /* *Column for the default `indent-line-function' to indent to.
5709 Linefeed indents to this column in Fundamental mode. */);
5711 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5712 make_number (Lisp_Int),
5713 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5715 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5716 doc: /* *Non-nil means display control chars with uparrow.
5717 A value of nil means use backslash and octal digits.
5718 This variable does not apply to characters whose display is specified
5719 in the current display table (if there is one). */);
5721 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5722 &current_buffer->enable_multibyte_characters,
5723 Qnil,
5724 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5725 Otherwise they are regarded as unibyte. This affects the display,
5726 file I/O and the behavior of various editing commands.
5728 This variable is buffer-local but you cannot set it directly;
5729 use the function `set-buffer-multibyte' to change a buffer's representation.
5730 Changing its default value with `setq-default' is supported.
5731 See also variable `default-enable-multibyte-characters' and Info node
5732 `(elisp)Text Representations'. */);
5733 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5735 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5736 &current_buffer->buffer_file_coding_system, Qnil,
5737 doc: /* Coding system to be used for encoding the buffer contents on saving.
5738 This variable applies to saving the buffer, and also to `write-region'
5739 and other functions that use `write-region'.
5740 It does not apply to sending output to subprocesses, however.
5742 If this is nil, the buffer is saved without any code conversion
5743 unless some coding system is specified in `file-coding-system-alist'
5744 for the buffer file.
5746 If the text to be saved cannot be encoded as specified by this variable,
5747 an alternative encoding is selected by `select-safe-coding-system', which see.
5749 The variable `coding-system-for-write', if non-nil, overrides this variable.
5751 This variable is never applied to a way of decoding a file while reading it. */);
5753 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5754 Qnil,
5755 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5757 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5758 doc: /* *Non-nil means do not display continuation lines.
5759 Instead, give each line of text just one screen line.
5761 Note that this is overridden by the variable
5762 `truncate-partial-width-windows' if that variable is non-nil
5763 and this buffer is not full-frame width. */);
5765 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5766 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5767 When word-wrapping is on, continuation lines are wrapped at the space
5768 or tab character nearest to the right window edge.
5769 If nil, continuation lines are wrapped at the right screen edge.
5771 This variable has no effect if long lines are truncated (see
5772 `truncate-lines' and `truncate-partial-width-windows'). If you use
5773 word-wrapping, you might want to reduce the value of
5774 `truncate-partial-width-windows', since wrapping can make text readable
5775 in narrower windows. */);
5777 #ifdef DOS_NT
5778 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5779 Qnil,
5780 doc: /* Non-nil if the visited file is a binary file.
5781 This variable is meaningful on MS-DOG and Windows NT.
5782 On those systems, it is automatically local in every buffer.
5783 On other systems, this variable is normally always nil. */);
5784 #endif
5786 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5787 make_number (Lisp_String),
5788 doc: /* Name of default directory of current buffer. Should end with slash.
5789 To interactively change the default directory, use command `cd'. */);
5791 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5792 Qnil,
5793 doc: /* Function called (if non-nil) to perform auto-fill.
5794 It is called after self-inserting any character specified in
5795 the `auto-fill-chars' table.
5796 NOTE: This variable is not a hook;
5797 its value may not be a list of functions. */);
5799 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5800 make_number (Lisp_String),
5801 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5803 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5804 make_number (Lisp_String),
5805 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5806 The truename of a file is calculated by `file-truename'
5807 and then abbreviated with `abbreviate-file-name'. */);
5809 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5810 &current_buffer->auto_save_file_name,
5811 make_number (Lisp_String),
5812 doc: /* Name of file for auto-saving current buffer.
5813 If it is nil, that means don't auto-save this buffer. */);
5815 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5816 doc: /* Non-nil if this buffer is read-only. */);
5818 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5819 doc: /* Non-nil if this buffer's file has been backed up.
5820 Backing up is done before the first time the file is saved. */);
5822 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5823 make_number (Lisp_Int),
5824 doc: /* Length of current buffer when last read in, saved or auto-saved.
5825 0 initially. */);
5827 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5828 Qnil,
5829 doc: /* Non-nil enables selective display.
5830 An integer N as value means display only lines
5831 that start with less than N columns of space.
5832 A value of t means that the character ^M makes itself and
5833 all the rest of the line invisible; also, when saving the buffer
5834 in a file, save the ^M as a newline. */);
5836 #ifndef old
5837 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5838 &current_buffer->selective_display_ellipses,
5839 Qnil,
5840 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5841 #endif
5843 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5844 doc: /* Non-nil if self-insertion should replace existing text.
5845 The value should be one of `overwrite-mode-textual',
5846 `overwrite-mode-binary', or nil.
5847 If it is `overwrite-mode-textual', self-insertion still
5848 inserts at the end of a line, and inserts when point is before a tab,
5849 until the tab is filled in.
5850 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5852 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5853 Qnil,
5854 doc: /* Display table that controls display of the contents of current buffer.
5856 If this variable is nil, the value of `standard-display-table' is used.
5857 Each window can have its own, overriding display table, see
5858 `set-window-display-table' and `window-display-table'.
5860 The display table is a char-table created with `make-display-table'.
5861 A char-table is an array indexed by character codes. Normal array
5862 primitives `aref' and `aset' can be used to access elements of a char-table.
5864 Each of the char-table elements control how to display the corresponding
5865 text character: the element at index C in the table says how to display
5866 the character whose code is C. Each element should be a vector of
5867 characters or nil. The value nil means display the character in the
5868 default fashion; otherwise, the characters from the vector are delivered
5869 to the screen instead of the original character.
5871 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5872 to display a capital Y instead of each X character.
5874 In addition, a char-table has six extra slots to control the display of:
5876 the end of a truncated screen line (extra-slot 0, a single character);
5877 the end of a continued line (extra-slot 1, a single character);
5878 the escape character used to display character codes in octal
5879 (extra-slot 2, a single character);
5880 the character used as an arrow for control characters (extra-slot 3,
5881 a single character);
5882 the decoration indicating the presence of invisible lines (extra-slot 4,
5883 a vector of characters);
5884 the character used to draw the border between side-by-side windows
5885 (extra-slot 5, a single character).
5887 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5889 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5890 Qnil,
5891 doc: /* *Width of left marginal area for display of a buffer.
5892 A value of nil means no marginal area. */);
5894 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5895 Qnil,
5896 doc: /* *Width of right marginal area for display of a buffer.
5897 A value of nil means no marginal area. */);
5899 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5900 Qnil,
5901 doc: /* *Width of this buffer's left fringe (in pixels).
5902 A value of 0 means no left fringe is shown in this buffer's window.
5903 A value of nil means to use the left fringe width from the window's frame. */);
5905 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5906 Qnil,
5907 doc: /* *Width of this buffer's right fringe (in pixels).
5908 A value of 0 means no right fringe is shown in this buffer's window.
5909 A value of nil means to use the right fringe width from the window's frame. */);
5911 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5912 Qnil,
5913 doc: /* *Non-nil means to display fringes outside display margins.
5914 A value of nil means to display fringes between margins and buffer text. */);
5916 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5917 Qnil,
5918 doc: /* *Width of this buffer's scroll bars in pixels.
5919 A value of nil means to use the scroll bar width from the window's frame. */);
5921 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5922 Qnil,
5923 doc: /* *Position of this buffer's vertical scroll bar.
5924 The value takes effect whenever you tell a window to display this buffer;
5925 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5927 A value of `left' or `right' means put the vertical scroll bar at that side
5928 of the window; a value of nil means don't show any vertical scroll bars.
5929 A value of t (the default) means do whatever the window's frame specifies. */);
5931 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5932 &current_buffer->indicate_empty_lines, Qnil,
5933 doc: /* *Visually indicate empty lines after the buffer end.
5934 If non-nil, a bitmap is displayed in the left fringe of a window on
5935 window-systems. */);
5937 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5938 &current_buffer->indicate_buffer_boundaries, Qnil,
5939 doc: /* *Visually indicate buffer boundaries and scrolling.
5940 If non-nil, the first and last line of the buffer are marked in the fringe
5941 of a window on window-systems with angle bitmaps, or if the window can be
5942 scrolled, the top and bottom line of the window are marked with up and down
5943 arrow bitmaps.
5945 If value is a symbol `left' or `right', both angle and arrow bitmaps
5946 are displayed in the left or right fringe, resp. Any other value
5947 that doesn't look like an alist means display the angle bitmaps in
5948 the left fringe but no arrows.
5950 You can exercise more precise control by using an alist as the
5951 value. Each alist element (INDICATOR . POSITION) specifies
5952 where to show one of the indicators. INDICATOR is one of `top',
5953 `bottom', `up', `down', or t, which specifies the default position,
5954 and POSITION is one of `left', `right', or nil, meaning do not show
5955 this indicator.
5957 For example, ((top . left) (t . right)) places the top angle bitmap in
5958 left fringe, the bottom angle bitmap in right fringe, and both arrow
5959 bitmaps in right fringe. To show just the angle bitmaps in the left
5960 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5962 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5963 &current_buffer->fringe_indicator_alist, Qnil,
5964 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5965 The value is an alist where each element (INDICATOR . BITMAPS)
5966 specifies the fringe bitmaps used to display a specific logical
5967 fringe indicator.
5969 INDICATOR specifies the logical indicator type which is one of the
5970 following symbols: `truncation' , `continuation', `overlay-arrow',
5971 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5973 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5974 the actual bitmap shown in the left or right fringe for the logical
5975 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5976 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5977 are used only for the `bottom' and `one-line' indicators when the last
5978 \(only) line in has no final newline. BITMAPS may also be a single
5979 symbol which is used in both left and right fringes. */);
5981 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5982 &current_buffer->fringe_cursor_alist, Qnil,
5983 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5984 The value is an alist where each element (CURSOR . BITMAP)
5985 specifies the fringe bitmaps used to display a specific logical
5986 cursor type in the fringe.
5988 CURSOR specifies the logical cursor type which is one of the following
5989 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5990 one is used to show a hollow cursor on narrow lines display lines
5991 where the normal hollow cursor will not fit.
5993 BITMAP is the corresponding fringe bitmap shown for the logical
5994 cursor type. */);
5996 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5997 &current_buffer->scroll_up_aggressively, Qnil,
5998 doc: /* How far to scroll windows upward.
5999 If you move point off the bottom, the window scrolls automatically.
6000 This variable controls how far it scrolls. The value nil, the default,
6001 means scroll to center point. A fraction means scroll to put point
6002 that fraction of the window's height from the bottom of the window.
6003 When the value is 0.0, point goes at the bottom line, which in the
6004 simple case that you moved off with C-f means scrolling just one line.
6005 1.0 means point goes at the top, so that in that simple case, the
6006 window scrolls by a full window height. Meaningful values are
6007 between 0.0 and 1.0, inclusive. */);
6009 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
6010 &current_buffer->scroll_down_aggressively, Qnil,
6011 doc: /* How far to scroll windows downward.
6012 If you move point off the top, the window scrolls automatically.
6013 This variable controls how far it scrolls. The value nil, the default,
6014 means scroll to center point. A fraction means scroll to put point
6015 that fraction of the window's height from the top of the window.
6016 When the value is 0.0, point goes at the top line, which in the
6017 simple case that you moved off with C-b means scrolling just one line.
6018 1.0 means point goes at the bottom, so that in that simple case, the
6019 window scrolls by a full window height. Meaningful values are
6020 between 0.0 and 1.0, inclusive. */);
6022 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6023 "Don't ask.");
6026 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6027 doc: /* List of functions to call before each text change.
6028 Two arguments are passed to each function: the positions of
6029 the beginning and end of the range of old text to be changed.
6030 \(For an insertion, the beginning and end are at the same place.)
6031 No information is given about the length of the text after the change.
6033 Buffer changes made while executing the `before-change-functions'
6034 don't call any before-change or after-change functions.
6035 That's because these variables are temporarily set to nil.
6036 As a result, a hook function cannot straightforwardly alter the
6037 value of these variables. See the Emacs Lisp manual for a way of
6038 accomplishing an equivalent result by using other variables.
6040 If an unhandled error happens in running these functions,
6041 the variable's value remains nil. That prevents the error
6042 from happening repeatedly and making Emacs nonfunctional. */);
6043 Vbefore_change_functions = Qnil;
6045 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6046 doc: /* List of functions to call after each text change.
6047 Three arguments are passed to each function: the positions of
6048 the beginning and end of the range of changed text,
6049 and the length in bytes of the pre-change text replaced by that range.
6050 \(For an insertion, the pre-change length is zero;
6051 for a deletion, that length is the number of bytes deleted,
6052 and the post-change beginning and end are at the same place.)
6054 Buffer changes made while executing the `after-change-functions'
6055 don't call any before-change or after-change functions.
6056 That's because these variables are temporarily set to nil.
6057 As a result, a hook function cannot straightforwardly alter the
6058 value of these variables. See the Emacs Lisp manual for a way of
6059 accomplishing an equivalent result by using other variables.
6061 If an unhandled error happens in running these functions,
6062 the variable's value remains nil. That prevents the error
6063 from happening repeatedly and making Emacs nonfunctional. */);
6064 Vafter_change_functions = Qnil;
6066 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6067 doc: /* A list of functions to call before changing a buffer which is unmodified.
6068 The functions are run using the `run-hooks' function. */);
6069 Vfirst_change_hook = Qnil;
6071 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6072 doc: /* List of undo entries in current buffer.
6073 Recent changes come first; older changes follow newer.
6075 An entry (BEG . END) represents an insertion which begins at
6076 position BEG and ends at position END.
6078 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6079 from (abs POSITION). If POSITION is positive, point was at the front
6080 of the text being deleted; if negative, point was at the end.
6082 An entry (t HIGH . LOW) indicates that the buffer previously had
6083 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6084 of the visited file's modification time, as of that time. If the
6085 modification time of the most recent save is different, this entry is
6086 obsolete.
6088 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6089 was modified between BEG and END. PROPERTY is the property name,
6090 and VALUE is the old value.
6092 An entry (apply FUN-NAME . ARGS) means undo the change with
6093 \(apply FUN-NAME ARGS).
6095 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6096 in the active region. BEG and END is the range affected by this entry
6097 and DELTA is the number of bytes added or deleted in that range by
6098 this change.
6100 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6101 was adjusted in position by the offset DISTANCE (an integer).
6103 An entry of the form POSITION indicates that point was at the buffer
6104 location given by the integer. Undoing an entry of this form places
6105 point at POSITION.
6107 Entries with value `nil' mark undo boundaries. The undo command treats
6108 the changes between two undo boundaries as a single step to be undone.
6110 If the value of the variable is t, undo information is not recorded. */);
6112 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6113 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6115 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6116 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6118 Normally, the line-motion functions work by scanning the buffer for
6119 newlines. Columnar operations (like `move-to-column' and
6120 `compute-motion') also work by scanning the buffer, summing character
6121 widths as they go. This works well for ordinary text, but if the
6122 buffer's lines are very long (say, more than 500 characters), these
6123 motion functions will take longer to execute. Emacs may also take
6124 longer to update the display.
6126 If `cache-long-line-scans' is non-nil, these motion functions cache the
6127 results of their scans, and consult the cache to avoid rescanning
6128 regions of the buffer until the text is modified. The caches are most
6129 beneficial when they prevent the most searching---that is, when the
6130 buffer contains long lines and large regions of characters with the
6131 same, fixed screen width.
6133 When `cache-long-line-scans' is non-nil, processing short lines will
6134 become slightly slower (because of the overhead of consulting the
6135 cache), and the caches will use memory roughly proportional to the
6136 number of newlines and characters whose screen width varies.
6138 The caches require no explicit maintenance; their accuracy is
6139 maintained internally by the Emacs primitives. Enabling or disabling
6140 the cache should not affect the behavior of any of the motion
6141 functions; it should only affect their performance. */);
6143 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6144 doc: /* Value of point before the last series of scroll operations, or nil. */);
6146 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6147 doc: /* List of formats to use when saving this buffer.
6148 Formats are defined by `format-alist'. This variable is
6149 set when a file is visited. */);
6151 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6152 &current_buffer->auto_save_file_format, Qnil,
6153 doc: /* *Format in which to write auto-save files.
6154 Should be a list of symbols naming formats that are defined in `format-alist'.
6155 If it is t, which is the default, auto-save files are written in the
6156 same format as a regular save would use. */);
6158 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6159 &current_buffer->invisibility_spec, Qnil,
6160 doc: /* Invisibility spec of this buffer.
6161 The default is t, which means that text is invisible
6162 if it has a non-nil `invisible' property.
6163 If the value is a list, a text character is invisible if its `invisible'
6164 property is an element in that list (or is a list with members in common).
6165 If an element is a cons cell of the form (PROP . ELLIPSIS),
6166 then characters with property value PROP are invisible,
6167 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6169 DEFVAR_PER_BUFFER ("buffer-display-count",
6170 &current_buffer->display_count, Qnil,
6171 doc: /* A number incremented each time this buffer is displayed in a window.
6172 The function `set-window-buffer' increments it. */);
6174 DEFVAR_PER_BUFFER ("buffer-display-time",
6175 &current_buffer->display_time, Qnil,
6176 doc: /* Time stamp updated each time this buffer is displayed in a window.
6177 The function `set-window-buffer' updates this variable
6178 to the value obtained by calling `current-time'.
6179 If the buffer has never been shown in a window, the value is nil. */);
6181 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6182 doc: /* */);
6183 Vtransient_mark_mode = Qnil;
6184 /* The docstring is in simple.el. If we put it here, it would be
6185 overwritten when transient-mark-mode is defined using
6186 define-minor-mode. */
6188 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6189 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6190 If the value is t, disregard `buffer-read-only' and all `read-only'
6191 text properties. If the value is a list, disregard `buffer-read-only'
6192 and disregard a `read-only' text property if the property value
6193 is a member of the list. */);
6194 Vinhibit_read_only = Qnil;
6196 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6197 doc: /* Cursor to use when this buffer is in the selected window.
6198 Values are interpreted as follows:
6200 t use the cursor specified for the frame
6201 nil don't display a cursor
6202 box display a filled box cursor
6203 hollow display a hollow box cursor
6204 bar display a vertical bar cursor with default width
6205 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6206 hbar display a horizontal bar cursor with default height
6207 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6208 ANYTHING ELSE display a hollow box cursor
6210 When the buffer is displayed in a nonselected window,
6211 this variable has no effect; the cursor appears as a hollow box. */);
6213 DEFVAR_PER_BUFFER ("line-spacing",
6214 &current_buffer->extra_line_spacing, Qnil,
6215 doc: /* Additional space to put between lines when displaying a buffer.
6216 The space is measured in pixels, and put below lines on window systems.
6217 If value is a floating point number, it specifies the spacing relative
6218 to the default frame line height. A value of nil means add no extra space. */);
6220 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6221 &current_buffer->cursor_in_non_selected_windows, Qnil,
6222 doc: /* *Cursor type to display in non-selected windows.
6223 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6225 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6226 doc: /* List of functions called with no args to query before killing a buffer.
6227 The buffer being killed will be current while the functions are running.
6228 If any of them returns nil, the buffer is not killed. */);
6229 Vkill_buffer_query_functions = Qnil;
6231 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6232 doc: /* Normal hook run before changing the major mode of a buffer.
6233 The function `kill-all-local-variables' runs this before doing anything else. */);
6234 Vchange_major_mode_hook = Qnil;
6235 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6236 staticpro (&Qchange_major_mode_hook);
6238 defsubr (&Sbuffer_live_p);
6239 defsubr (&Sbuffer_list);
6240 defsubr (&Sget_buffer);
6241 defsubr (&Sget_file_buffer);
6242 defsubr (&Sget_buffer_create);
6243 defsubr (&Smake_indirect_buffer);
6244 defsubr (&Sgenerate_new_buffer_name);
6245 defsubr (&Sbuffer_name);
6246 /*defsubr (&Sbuffer_number);*/
6247 defsubr (&Sbuffer_file_name);
6248 defsubr (&Sbuffer_base_buffer);
6249 defsubr (&Sbuffer_local_value);
6250 defsubr (&Sbuffer_local_variables);
6251 defsubr (&Sbuffer_modified_p);
6252 defsubr (&Sset_buffer_modified_p);
6253 defsubr (&Sbuffer_modified_tick);
6254 defsubr (&Sbuffer_chars_modified_tick);
6255 defsubr (&Srename_buffer);
6256 defsubr (&Sother_buffer);
6257 defsubr (&Sbuffer_enable_undo);
6258 defsubr (&Skill_buffer);
6259 defsubr (&Sset_buffer_major_mode);
6260 defsubr (&Sswitch_to_buffer);
6261 defsubr (&Scurrent_buffer);
6262 defsubr (&Sset_buffer);
6263 defsubr (&Sbarf_if_buffer_read_only);
6264 defsubr (&Sbury_buffer);
6265 defsubr (&Serase_buffer);
6266 defsubr (&Sbuffer_swap_text);
6267 defsubr (&Sset_buffer_multibyte);
6268 defsubr (&Skill_all_local_variables);
6270 defsubr (&Soverlayp);
6271 defsubr (&Smake_overlay);
6272 defsubr (&Sdelete_overlay);
6273 defsubr (&Smove_overlay);
6274 defsubr (&Soverlay_start);
6275 defsubr (&Soverlay_end);
6276 defsubr (&Soverlay_buffer);
6277 defsubr (&Soverlay_properties);
6278 defsubr (&Soverlays_at);
6279 defsubr (&Soverlays_in);
6280 defsubr (&Snext_overlay_change);
6281 defsubr (&Sprevious_overlay_change);
6282 defsubr (&Soverlay_recenter);
6283 defsubr (&Soverlay_lists);
6284 defsubr (&Soverlay_get);
6285 defsubr (&Soverlay_put);
6286 defsubr (&Srestore_buffer_modified_p);
6289 void
6290 keys_of_buffer ()
6292 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6293 initial_define_key (control_x_map, 'k', "kill-buffer");
6295 /* This must not be in syms_of_buffer, because Qdisabled is not
6296 initialized when that function gets called. */
6297 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6300 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6301 (do not change this comment) */