*** empty log message ***
[emacs.git] / src / buffer.c
blobe3f769ab41bf2a7e0acd1b861dd4d9a87215fcb2
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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
34 #ifndef MAXPATHLEN
35 /* in 4.1, param.h fails to define this. */
36 #define MAXPATHLEN 1024
37 #endif /* not MAXPATHLEN */
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #include "lisp.h"
43 #include "intervals.h"
44 #include "window.h"
45 #include "commands.h"
46 #include "buffer.h"
47 #include "charset.h"
48 #include "region-cache.h"
49 #include "indent.h"
50 #include "blockinput.h"
51 #include "keyboard.h"
52 #include "frame.h"
54 struct buffer *current_buffer; /* the current buffer */
56 /* First buffer in chain of all buffers (in reverse order of creation).
57 Threaded through ->next. */
59 struct buffer *all_buffers;
61 /* This structure holds the default values of the buffer-local variables
62 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
63 The default value occupies the same slot in this structure
64 as an individual buffer's value occupies in that buffer.
65 Setting the default value also goes through the alist of buffers
66 and stores into each buffer that does not say it has a local value. */
68 struct buffer buffer_defaults;
70 /* A Lisp_Object pointer to the above, used for staticpro */
72 static Lisp_Object Vbuffer_defaults;
74 /* This structure marks which slots in a buffer have corresponding
75 default values in buffer_defaults.
76 Each such slot has a nonzero value in this structure.
77 The value has only one nonzero bit.
79 When a buffer has its own local value for a slot,
80 the entry for that slot (found in the same slot in this structure)
81 is turned on in the buffer's local_flags array.
83 If a slot in this structure is -1, then even though there may
84 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
85 and the corresponding slot in buffer_defaults is not used.
87 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
88 but there is a default value which is copied into each buffer.
90 If a slot in this structure is negative, then even though there may
91 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
92 and the corresponding slot in buffer_defaults is not used.
94 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
95 zero, that is a bug */
97 struct buffer buffer_local_flags;
99 /* This structure holds the names of symbols whose values may be
100 buffer-local. It is indexed and accessed in the same way as the above. */
102 struct buffer buffer_local_symbols;
103 /* A Lisp_Object pointer to the above, used for staticpro */
104 static Lisp_Object Vbuffer_local_symbols;
106 /* This structure holds the required types for the values in the
107 buffer-local slots. If a slot contains Qnil, then the
108 corresponding buffer slot may contain a value of any type. If a
109 slot contains an integer, then prospective values' tags must be
110 equal to that integer (except nil is always allowed).
111 When a tag does not match, the function
112 buffer_slot_type_mismatch will signal an error.
114 If a slot here contains -1, the corresponding variable is read-only. */
115 struct buffer buffer_local_types;
117 /* Flags indicating which built-in buffer-local variables
118 are permanent locals. */
119 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
121 /* Number of per-buffer variables used. */
123 int last_per_buffer_idx;
125 Lisp_Object Fset_buffer ();
126 void set_buffer_internal ();
127 void set_buffer_internal_1 ();
128 static void call_overlay_mod_hooks ();
129 static void swap_out_buffer_local_variables ();
130 static void reset_buffer_local_variables ();
132 /* Alist of all buffer names vs the buffers. */
133 /* This used to be a variable, but is no longer,
134 to prevent lossage due to user rplac'ing this alist or its elements. */
135 Lisp_Object Vbuffer_alist;
137 /* Functions to call before and after each text change. */
138 Lisp_Object Vbefore_change_functions;
139 Lisp_Object Vafter_change_functions;
141 Lisp_Object Vtransient_mark_mode;
143 /* t means ignore all read-only text properties.
144 A list means ignore such a property if its value is a member of the list.
145 Any non-nil value means ignore buffer-read-only. */
146 Lisp_Object Vinhibit_read_only;
148 /* List of functions to call that can query about killing a buffer.
149 If any of these functions returns nil, we don't kill it. */
150 Lisp_Object Vkill_buffer_query_functions;
152 /* List of functions to call before changing an unmodified buffer. */
153 Lisp_Object Vfirst_change_hook;
155 Lisp_Object Qfirst_change_hook;
156 Lisp_Object Qbefore_change_functions;
157 Lisp_Object Qafter_change_functions;
159 /* If nonzero, all modification hooks are suppressed. */
160 int inhibit_modification_hooks;
162 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
164 Lisp_Object Qprotected_field;
166 Lisp_Object QSFundamental; /* A string "Fundamental" */
168 Lisp_Object Qkill_buffer_hook;
170 Lisp_Object Qget_file_buffer;
172 Lisp_Object Qoverlayp;
174 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
176 Lisp_Object Qmodification_hooks;
177 Lisp_Object Qinsert_in_front_hooks;
178 Lisp_Object Qinsert_behind_hooks;
180 static void alloc_buffer_text P_ ((struct buffer *, size_t));
181 static void free_buffer_text P_ ((struct buffer *b));
184 /* For debugging; temporary. See set_buffer_internal. */
185 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
187 void
188 nsberror (spec)
189 Lisp_Object spec;
191 if (STRINGP (spec))
192 error ("No buffer named %s", XSTRING (spec)->data);
193 error ("Invalid buffer argument");
196 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
197 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
198 Value is nil if OBJECT is not a buffer or if it has been killed.")
199 (object)
200 Lisp_Object object;
202 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
203 ? Qt : Qnil);
206 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
207 "Return a list of all existing live buffers.\n\
208 If the optional arg FRAME is a frame, we return that frame's buffer list.")
209 (frame)
210 Lisp_Object frame;
212 Lisp_Object framelist, general;
213 general = Fmapcar (Qcdr, Vbuffer_alist);
215 if (FRAMEP (frame))
217 Lisp_Object tail;
219 CHECK_FRAME (frame, 1);
221 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
223 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
224 tail = framelist;
225 while (! NILP (tail))
227 general = Fdelq (XCAR (tail), general);
228 tail = XCDR (tail);
230 return nconc2 (framelist, general);
233 return general;
236 /* Like Fassoc, but use Fstring_equal to compare
237 (which ignores text properties),
238 and don't ever QUIT. */
240 static Lisp_Object
241 assoc_ignore_text_properties (key, list)
242 register Lisp_Object key;
243 Lisp_Object list;
245 register Lisp_Object tail;
246 for (tail = list; !NILP (tail); tail = Fcdr (tail))
248 register Lisp_Object elt, tem;
249 elt = Fcar (tail);
250 tem = Fstring_equal (Fcar (elt), key);
251 if (!NILP (tem))
252 return elt;
254 return Qnil;
257 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
258 "Return the buffer named NAME (a string).\n\
259 If there is no live buffer named NAME, return nil.\n\
260 NAME may also be a buffer; if so, the value is that buffer.")
261 (name)
262 register Lisp_Object name;
264 if (BUFFERP (name))
265 return name;
266 CHECK_STRING (name, 0);
268 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
271 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
272 "Return the buffer visiting file FILENAME (a string).\n\
273 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
274 If there is no such live buffer, return nil.\n\
275 See also `find-buffer-visiting'.")
276 (filename)
277 register Lisp_Object filename;
279 register Lisp_Object tail, buf, tem;
280 Lisp_Object handler;
282 CHECK_STRING (filename, 0);
283 filename = Fexpand_file_name (filename, Qnil);
285 /* If the file name has special constructs in it,
286 call the corresponding file handler. */
287 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
288 if (!NILP (handler))
289 return call2 (handler, Qget_file_buffer, filename);
291 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
293 buf = Fcdr (XCAR (tail));
294 if (!BUFFERP (buf)) continue;
295 if (!STRINGP (XBUFFER (buf)->filename)) continue;
296 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
297 if (!NILP (tem))
298 return buf;
300 return Qnil;
303 Lisp_Object
304 get_truename_buffer (filename)
305 register Lisp_Object filename;
307 register Lisp_Object tail, buf, tem;
309 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
311 buf = Fcdr (XCAR (tail));
312 if (!BUFFERP (buf)) continue;
313 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
314 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
315 if (!NILP (tem))
316 return buf;
318 return Qnil;
321 /* Incremented for each buffer created, to assign the buffer number. */
322 int buffer_count;
324 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
325 "Return the buffer named NAME, or create such a buffer and return it.\n\
326 A new buffer is created if there is no live buffer named NAME.\n\
327 If NAME starts with a space, the new buffer does not keep undo information.\n\
328 If NAME is a buffer instead of a string, then it is the value returned.\n\
329 The value is never nil.")
330 (name)
331 register Lisp_Object name;
333 register Lisp_Object buf;
334 register struct buffer *b;
336 buf = Fget_buffer (name);
337 if (!NILP (buf))
338 return buf;
340 if (XSTRING (name)->size == 0)
341 error ("Empty string for buffer name is not allowed");
343 b = (struct buffer *) allocate_buffer ();
345 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
347 /* An ordinary buffer uses its own struct buffer_text. */
348 b->text = &b->own_text;
349 b->base_buffer = 0;
351 BUF_GAP_SIZE (b) = 20;
352 BLOCK_INPUT;
353 /* We allocate extra 1-byte at the tail and keep it always '\0' for
354 anchoring a search. */
355 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
356 UNBLOCK_INPUT;
357 if (! BUF_BEG_ADDR (b))
358 buffer_memory_full ();
360 BUF_PT (b) = 1;
361 BUF_GPT (b) = 1;
362 BUF_BEGV (b) = 1;
363 BUF_ZV (b) = 1;
364 BUF_Z (b) = 1;
365 BUF_PT_BYTE (b) = 1;
366 BUF_GPT_BYTE (b) = 1;
367 BUF_BEGV_BYTE (b) = 1;
368 BUF_ZV_BYTE (b) = 1;
369 BUF_Z_BYTE (b) = 1;
370 BUF_MODIFF (b) = 1;
371 BUF_OVERLAY_MODIFF (b) = 1;
372 BUF_SAVE_MODIFF (b) = 1;
373 BUF_INTERVALS (b) = 0;
374 BUF_UNCHANGED_MODIFIED (b) = 1;
375 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
376 BUF_END_UNCHANGED (b) = 0;
377 BUF_BEG_UNCHANGED (b) = 0;
378 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
380 b->newline_cache = 0;
381 b->width_run_cache = 0;
382 b->width_table = Qnil;
383 b->prevent_redisplay_optimizations_p = 1;
385 /* Put this on the chain of all buffers including killed ones. */
386 b->next = all_buffers;
387 all_buffers = b;
389 /* An ordinary buffer normally doesn't need markers
390 to handle BEGV and ZV. */
391 b->pt_marker = Qnil;
392 b->begv_marker = Qnil;
393 b->zv_marker = Qnil;
395 name = Fcopy_sequence (name);
396 XSTRING (name)->intervals = NULL_INTERVAL;
397 b->name = name;
399 if (XSTRING (name)->data[0] != ' ')
400 b->undo_list = Qnil;
401 else
402 b->undo_list = Qt;
404 reset_buffer (b);
405 reset_buffer_local_variables (b, 1);
407 /* Put this in the alist of all live buffers. */
408 XSETBUFFER (buf, b);
409 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
411 b->mark = Fmake_marker ();
412 BUF_MARKERS (b) = Qnil;
413 b->name = name;
414 return buf;
418 /* Clone per-buffer values of buffer FROM.
420 Buffer TO gets the same per-buffer values as FROM, with the
421 following exceptions: (1) TO's name is left untouched, (2) markers
422 are copied and made to refer to TO, and (3) overlay lists are
423 copied. */
425 static void
426 clone_per_buffer_values (from, to)
427 struct buffer *from, *to;
429 Lisp_Object to_buffer;
430 int offset;
432 XSETBUFFER (to_buffer, to);
434 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
435 offset < sizeof *to;
436 offset += sizeof (Lisp_Object))
438 Lisp_Object obj;
440 obj = PER_BUFFER_VALUE (from, offset);
441 if (MARKERP (obj))
443 struct Lisp_Marker *m = XMARKER (obj);
444 obj = Fmake_marker ();
445 XMARKER (obj)->insertion_type = m->insertion_type;
446 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
449 PER_BUFFER_VALUE (to, offset) = obj;
452 to->overlays_after = Fcopy_sequence (from->overlays_after);
453 to->overlays_before = Fcopy_sequence (to->overlays_before);
454 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
458 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
459 2, 3,
460 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
461 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
462 BASE-BUFFER should be an existing buffer (or buffer name).\n\
463 NAME should be a string which is not the name of an existing buffer.\n\
464 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,\n\
465 such as major and minor modes, in the indirect buffer.\n\
466 CLONE nil means the indirect buffer's state is reset to default values.")
467 (base_buffer, name, clone)
468 Lisp_Object base_buffer, name, clone;
470 Lisp_Object buf;
471 struct buffer *b;
473 buf = Fget_buffer (name);
474 if (!NILP (buf))
475 error ("Buffer name `%s' is in use", XSTRING (name)->data);
477 base_buffer = Fget_buffer (base_buffer);
478 if (NILP (base_buffer))
479 error ("No such buffer: `%s'", XSTRING (name)->data);
481 if (XSTRING (name)->size == 0)
482 error ("Empty string for buffer name is not allowed");
484 b = (struct buffer *) allocate_buffer ();
485 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
487 if (XBUFFER (base_buffer)->base_buffer)
488 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
489 else
490 b->base_buffer = XBUFFER (base_buffer);
492 /* Use the base buffer's text object. */
493 b->text = b->base_buffer->text;
495 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
496 BUF_ZV (b) = BUF_ZV (b->base_buffer);
497 BUF_PT (b) = BUF_PT (b->base_buffer);
498 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
499 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
500 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
502 b->newline_cache = 0;
503 b->width_run_cache = 0;
504 b->width_table = Qnil;
506 /* Put this on the chain of all buffers including killed ones. */
507 b->next = all_buffers;
508 all_buffers = b;
510 name = Fcopy_sequence (name);
511 XSTRING (name)->intervals = NULL_INTERVAL;
512 b->name = name;
514 reset_buffer (b);
515 reset_buffer_local_variables (b, 1);
517 /* Put this in the alist of all live buffers. */
518 XSETBUFFER (buf, b);
519 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
521 b->mark = Fmake_marker ();
522 b->name = name;
524 /* The multibyte status belongs to the base buffer. */
525 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
527 /* Make sure the base buffer has markers for its narrowing. */
528 if (NILP (b->base_buffer->pt_marker))
530 b->base_buffer->pt_marker = Fmake_marker ();
531 set_marker_both (b->base_buffer->pt_marker, base_buffer,
532 BUF_PT (b->base_buffer),
533 BUF_PT_BYTE (b->base_buffer));
535 if (NILP (b->base_buffer->begv_marker))
537 b->base_buffer->begv_marker = Fmake_marker ();
538 set_marker_both (b->base_buffer->begv_marker, base_buffer,
539 BUF_BEGV (b->base_buffer),
540 BUF_BEGV_BYTE (b->base_buffer));
542 if (NILP (b->base_buffer->zv_marker))
544 b->base_buffer->zv_marker = Fmake_marker ();
545 set_marker_both (b->base_buffer->zv_marker, base_buffer,
546 BUF_ZV (b->base_buffer),
547 BUF_ZV_BYTE (b->base_buffer));
548 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
551 if (NILP (clone))
553 /* Give the indirect buffer markers for its narrowing. */
554 b->pt_marker = Fmake_marker ();
555 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
556 b->begv_marker = Fmake_marker ();
557 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
558 b->zv_marker = Fmake_marker ();
559 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
560 XMARKER (b->zv_marker)->insertion_type = 1;
562 else
563 clone_per_buffer_values (b->base_buffer, b);
565 return buf;
568 /* Reinitialize everything about a buffer except its name and contents
569 and local variables. */
571 void
572 reset_buffer (b)
573 register struct buffer *b;
575 b->filename = Qnil;
576 b->file_truename = Qnil;
577 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
578 b->modtime = 0;
579 XSETFASTINT (b->save_length, 0);
580 b->last_window_start = 1;
581 /* It is more conservative to start out "changed" than "unchanged". */
582 b->clip_changed = 0;
583 b->prevent_redisplay_optimizations_p = 1;
584 b->backed_up = Qnil;
585 b->auto_save_modified = 0;
586 b->auto_save_failure_time = -1;
587 b->auto_save_file_name = Qnil;
588 b->read_only = Qnil;
589 b->overlays_before = Qnil;
590 b->overlays_after = Qnil;
591 XSETFASTINT (b->overlay_center, 1);
592 b->mark_active = Qnil;
593 b->point_before_scroll = Qnil;
594 b->file_format = Qnil;
595 b->last_selected_window = Qnil;
596 XSETINT (b->display_count, 0);
597 b->display_time = Qnil;
598 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
599 b->cursor_type = buffer_defaults.cursor_type;
600 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
603 /* Reset buffer B's local variables info.
604 Don't use this on a buffer that has already been in use;
605 it does not treat permanent locals consistently.
606 Instead, use Fkill_all_local_variables.
608 If PERMANENT_TOO is 1, then we reset permanent built-in
609 buffer-local variables. If PERMANENT_TOO is 0,
610 we preserve those. */
612 static void
613 reset_buffer_local_variables (b, permanent_too)
614 register struct buffer *b;
615 int permanent_too;
617 register int offset;
618 int i;
620 /* Reset the major mode to Fundamental, together with all the
621 things that depend on the major mode.
622 default-major-mode is handled at a higher level.
623 We ignore it here. */
624 b->major_mode = Qfundamental_mode;
625 b->keymap = Qnil;
626 b->abbrev_table = Vfundamental_mode_abbrev_table;
627 b->mode_name = QSFundamental;
628 b->minor_modes = Qnil;
630 /* If the standard case table has been altered and invalidated,
631 fix up its insides first. */
632 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
633 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
634 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
635 Fset_standard_case_table (Vascii_downcase_table);
637 b->downcase_table = Vascii_downcase_table;
638 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
639 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
640 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
641 b->invisibility_spec = Qt;
642 #ifndef DOS_NT
643 b->buffer_file_type = Qnil;
644 #endif
646 #if 0
647 b->sort_table = XSTRING (Vascii_sort_table);
648 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
649 #endif /* 0 */
651 /* Reset all (or most) per-buffer variables to their defaults. */
652 b->local_var_alist = Qnil;
653 for (i = 0; i < last_per_buffer_idx; ++i)
654 if (permanent_too || buffer_permanent_local_flags[i] == 0)
655 SET_PER_BUFFER_VALUE_P (b, i, 0);
657 /* For each slot that has a default value,
658 copy that into the slot. */
660 for (offset = PER_BUFFER_VAR_OFFSET (name);
661 offset < sizeof *b;
662 offset += sizeof (Lisp_Object))
664 int idx = PER_BUFFER_IDX (offset);
665 if ((idx > 0
666 && (permanent_too
667 || buffer_permanent_local_flags[idx] == 0))
668 /* Is -2 used anywhere? */
669 || idx == -2)
670 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
674 /* We split this away from generate-new-buffer, because rename-buffer
675 and set-visited-file-name ought to be able to use this to really
676 rename the buffer properly. */
678 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
679 1, 2, 0,
680 "Return a string that is the name of no existing buffer based on NAME.\n\
681 If there is no live buffer named NAME, then return NAME.\n\
682 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
683 until an unused name is found, and then return that name.\n\
684 Optional second argument IGNORE specifies a name that is okay to use\n\
685 \(if it is in the sequence to be tried)\n\
686 even if a buffer with that name exists.")
687 (name, ignore)
688 register Lisp_Object name, ignore;
690 register Lisp_Object gentemp, tem;
691 int count;
692 char number[10];
694 CHECK_STRING (name, 0);
696 tem = Fget_buffer (name);
697 if (NILP (tem))
698 return name;
700 count = 1;
701 while (1)
703 sprintf (number, "<%d>", ++count);
704 gentemp = concat2 (name, build_string (number));
705 tem = Fstring_equal (gentemp, ignore);
706 if (!NILP (tem))
707 return gentemp;
708 tem = Fget_buffer (gentemp);
709 if (NILP (tem))
710 return gentemp;
715 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
716 "Return the name of BUFFER, as a string.\n\
717 With no argument or nil as argument, return the name of the current buffer.")
718 (buffer)
719 register Lisp_Object buffer;
721 if (NILP (buffer))
722 return current_buffer->name;
723 CHECK_BUFFER (buffer, 0);
724 return XBUFFER (buffer)->name;
727 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
728 "Return name of file BUFFER is visiting, or nil if none.\n\
729 No argument or nil as argument means use the current buffer.")
730 (buffer)
731 register Lisp_Object buffer;
733 if (NILP (buffer))
734 return current_buffer->filename;
735 CHECK_BUFFER (buffer, 0);
736 return XBUFFER (buffer)->filename;
739 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
740 0, 1, 0,
741 "Return the base buffer of indirect buffer BUFFER.\n\
742 If BUFFER is not indirect, return nil.")
743 (buffer)
744 register Lisp_Object buffer;
746 struct buffer *base;
747 Lisp_Object base_buffer;
749 if (NILP (buffer))
750 base = current_buffer->base_buffer;
751 else
753 CHECK_BUFFER (buffer, 0);
754 base = XBUFFER (buffer)->base_buffer;
757 if (! base)
758 return Qnil;
759 XSETBUFFER (base_buffer, base);
760 return base_buffer;
763 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
764 Sbuffer_local_variables, 0, 1, 0,
765 "Return an alist of variables that are buffer-local in BUFFER.\n\
766 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
767 For a symbol that is locally unbound, just the symbol appears in the value.\n\
768 Note that storing new VALUEs in these elements doesn't change the variables.\n\
769 No argument or nil as argument means use current buffer as BUFFER.")
770 (buffer)
771 register Lisp_Object buffer;
773 register struct buffer *buf;
774 register Lisp_Object result;
776 if (NILP (buffer))
777 buf = current_buffer;
778 else
780 CHECK_BUFFER (buffer, 0);
781 buf = XBUFFER (buffer);
784 result = Qnil;
787 register Lisp_Object tail;
788 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
790 Lisp_Object val, elt;
792 elt = XCAR (tail);
794 /* Reference each variable in the alist in buf.
795 If inquiring about the current buffer, this gets the current values,
796 so store them into the alist so the alist is up to date.
797 If inquiring about some other buffer, this swaps out any values
798 for that buffer, making the alist up to date automatically. */
799 val = find_symbol_value (XCAR (elt));
800 /* Use the current buffer value only if buf is the current buffer. */
801 if (buf != current_buffer)
802 val = XCDR (elt);
804 /* If symbol is unbound, put just the symbol in the list. */
805 if (EQ (val, Qunbound))
806 result = Fcons (XCAR (elt), result);
807 /* Otherwise, put (symbol . value) in the list. */
808 else
809 result = Fcons (Fcons (XCAR (elt), val), result);
813 /* Add on all the variables stored in special slots. */
815 int offset, idx;
817 for (offset = PER_BUFFER_VAR_OFFSET (name);
818 offset < sizeof (struct buffer);
819 /* sizeof EMACS_INT == sizeof Lisp_Object */
820 offset += (sizeof (EMACS_INT)))
822 idx = PER_BUFFER_IDX (offset);
823 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
824 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
825 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
826 PER_BUFFER_VALUE (buf, offset)),
827 result);
831 return result;
835 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
836 0, 1, 0,
837 "Return t if BUFFER was modified since its file was last read or saved.\n\
838 No argument or nil as argument means use current buffer as BUFFER.")
839 (buffer)
840 register Lisp_Object buffer;
842 register struct buffer *buf;
843 if (NILP (buffer))
844 buf = current_buffer;
845 else
847 CHECK_BUFFER (buffer, 0);
848 buf = XBUFFER (buffer);
851 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
854 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
855 1, 1, 0,
856 "Mark current buffer as modified or unmodified according to FLAG.\n\
857 A non-nil FLAG means mark the buffer modified.")
858 (flag)
859 register Lisp_Object flag;
861 register int already;
862 register Lisp_Object fn;
863 Lisp_Object buffer, window;
865 #ifdef CLASH_DETECTION
866 /* If buffer becoming modified, lock the file.
867 If buffer becoming unmodified, unlock the file. */
869 fn = current_buffer->file_truename;
870 /* Test buffer-file-name so that binding it to nil is effective. */
871 if (!NILP (fn) && ! NILP (current_buffer->filename))
873 already = SAVE_MODIFF < MODIFF;
874 if (!already && !NILP (flag))
875 lock_file (fn);
876 else if (already && NILP (flag))
877 unlock_file (fn);
879 #endif /* CLASH_DETECTION */
881 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
883 /* Set update_mode_lines only if buffer is displayed in some window.
884 Packages like jit-lock or lazy-lock preserve a buffer's modified
885 state by recording/restoring the state around blocks of code.
886 Setting update_mode_lines makes redisplay consider all windows
887 (on all frames). Stealth fontification of buffers not displayed
888 would incur additional redisplay costs if we'd set
889 update_modes_lines unconditionally.
891 Ideally, I think there should be another mechanism for fontifying
892 buffers without "modifying" buffers, or redisplay should be
893 smarter about updating the `*' in mode lines. --gerd */
894 XSETBUFFER (buffer, current_buffer);
895 window = Fget_buffer_window (buffer, Qt);
896 if (WINDOWP (window))
897 update_mode_lines++;
899 return flag;
902 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
903 Srestore_buffer_modified_p, 1, 1, 0,
904 "Like `set-buffer-modified-p', with a differences concerning redisplay.\n\
905 It is not ensured that mode lines will be updated to show the modified\n\
906 state of the current buffer. Use with care.")
907 (flag)
908 Lisp_Object flag;
910 #ifdef CLASH_DETECTION
911 Lisp_Object fn;
913 /* If buffer becoming modified, lock the file.
914 If buffer becoming unmodified, unlock the file. */
916 fn = current_buffer->file_truename;
917 /* Test buffer-file-name so that binding it to nil is effective. */
918 if (!NILP (fn) && ! NILP (current_buffer->filename))
920 int already = SAVE_MODIFF < MODIFF;
921 if (!already && !NILP (flag))
922 lock_file (fn);
923 else if (already && NILP (flag))
924 unlock_file (fn);
926 #endif /* CLASH_DETECTION */
928 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
929 return flag;
932 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
933 0, 1, 0,
934 "Return BUFFER's tick counter, incremented for each change in text.\n\
935 Each buffer has a tick counter which is incremented each time the text in\n\
936 that buffer is changed. It wraps around occasionally.\n\
937 No argument or nil as argument means use current buffer as BUFFER.")
938 (buffer)
939 register Lisp_Object buffer;
941 register struct buffer *buf;
942 if (NILP (buffer))
943 buf = current_buffer;
944 else
946 CHECK_BUFFER (buffer, 0);
947 buf = XBUFFER (buffer);
950 return make_number (BUF_MODIFF (buf));
953 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
954 "sRename buffer (to new name): \nP",
955 "Change current buffer's name to NEWNAME (a string).\n\
956 If second arg UNIQUE is nil or omitted, it is an error if a\n\
957 buffer named NEWNAME already exists.\n\
958 If UNIQUE is non-nil, come up with a new name using\n\
959 `generate-new-buffer-name'.\n\
960 Interactively, you can set UNIQUE with a prefix argument.\n\
961 We return the name we actually gave the buffer.\n\
962 This does not change the name of the visited file (if any).")
963 (newname, unique)
964 register Lisp_Object newname, unique;
966 register Lisp_Object tem, buf;
968 CHECK_STRING (newname, 0);
970 if (XSTRING (newname)->size == 0)
971 error ("Empty string is invalid as a buffer name");
973 tem = Fget_buffer (newname);
974 if (!NILP (tem))
976 /* Don't short-circuit if UNIQUE is t. That is a useful way to
977 rename the buffer automatically so you can create another
978 with the original name. It makes UNIQUE equivalent to
979 (rename-buffer (generate-new-buffer-name NEWNAME)). */
980 if (NILP (unique) && XBUFFER (tem) == current_buffer)
981 return current_buffer->name;
982 if (!NILP (unique))
983 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
984 else
985 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
988 current_buffer->name = newname;
990 /* Catch redisplay's attention. Unless we do this, the mode lines for
991 any windows displaying current_buffer will stay unchanged. */
992 update_mode_lines++;
994 XSETBUFFER (buf, current_buffer);
995 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
996 if (NILP (current_buffer->filename)
997 && !NILP (current_buffer->auto_save_file_name))
998 call0 (intern ("rename-auto-save-file"));
999 /* Refetch since that last call may have done GC. */
1000 return current_buffer->name;
1003 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1004 "Return most recently selected buffer other than BUFFER.\n\
1005 Buffers not visible in windows are preferred to visible buffers,\n\
1006 unless optional second argument VISIBLE-OK is non-nil.\n\
1007 If the optional third argument FRAME is non-nil, use that frame's\n\
1008 buffer list instead of the selected frame's buffer list.\n\
1009 If no other buffer exists, the buffer `*scratch*' is returned.\n\
1010 If BUFFER is omitted or nil, some interesting buffer is returned.")
1011 (buffer, visible_ok, frame)
1012 register Lisp_Object buffer, visible_ok, frame;
1014 Lisp_Object Fset_buffer_major_mode ();
1015 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1016 notsogood = Qnil;
1018 if (NILP (frame))
1019 frame = selected_frame;
1021 tail = Vbuffer_alist;
1022 pred = frame_buffer_predicate (frame);
1024 /* Consider buffers that have been seen in the selected frame
1025 before other buffers. */
1027 tem = frame_buffer_list (frame);
1028 add_ons = Qnil;
1029 while (CONSP (tem))
1031 if (BUFFERP (XCAR (tem)))
1032 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1033 tem = XCDR (tem);
1035 tail = nconc2 (Fnreverse (add_ons), tail);
1037 for (; !NILP (tail); tail = Fcdr (tail))
1039 buf = Fcdr (Fcar (tail));
1040 if (EQ (buf, buffer))
1041 continue;
1042 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1043 continue;
1044 /* If the selected frame has a buffer_predicate,
1045 disregard buffers that don't fit the predicate. */
1046 if (!NILP (pred))
1048 tem = call1 (pred, buf);
1049 if (NILP (tem))
1050 continue;
1053 if (NILP (visible_ok))
1054 tem = Fget_buffer_window (buf, Qvisible);
1055 else
1056 tem = Qnil;
1057 if (NILP (tem))
1058 return buf;
1059 if (NILP (notsogood))
1060 notsogood = buf;
1062 if (!NILP (notsogood))
1063 return notsogood;
1064 buf = Fget_buffer (build_string ("*scratch*"));
1065 if (NILP (buf))
1067 buf = Fget_buffer_create (build_string ("*scratch*"));
1068 Fset_buffer_major_mode (buf);
1070 return buf;
1073 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
1074 0, 1, "",
1075 "Make BUFFER stop keeping undo information.\n\
1076 No argument or nil as argument means do this for the current buffer.")
1077 (buffer)
1078 register Lisp_Object buffer;
1080 Lisp_Object real_buffer;
1082 if (NILP (buffer))
1083 XSETBUFFER (real_buffer, current_buffer);
1084 else
1086 real_buffer = Fget_buffer (buffer);
1087 if (NILP (real_buffer))
1088 nsberror (buffer);
1091 XBUFFER (real_buffer)->undo_list = Qt;
1093 return Qnil;
1096 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1097 0, 1, "",
1098 "Start keeping undo information for buffer BUFFER.\n\
1099 No argument or nil as argument means do this for the current buffer.")
1100 (buffer)
1101 register Lisp_Object buffer;
1103 Lisp_Object real_buffer;
1105 if (NILP (buffer))
1106 XSETBUFFER (real_buffer, current_buffer);
1107 else
1109 real_buffer = Fget_buffer (buffer);
1110 if (NILP (real_buffer))
1111 nsberror (buffer);
1114 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1115 XBUFFER (real_buffer)->undo_list = Qnil;
1117 return Qnil;
1121 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1122 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1123 The buffer being killed will be current while the hook is running.\n\
1124 See `kill-buffer'."
1126 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1127 "Kill the buffer BUFFER.\n\
1128 The argument may be a buffer or may be the name of a buffer.\n\
1129 An argument of nil means kill the current buffer.\n\n\
1130 Value is t if the buffer is actually killed, nil if user says no.\n\n\
1131 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1132 if not void, is a list of functions to be called, with no arguments,\n\
1133 before the buffer is actually killed. The buffer to be killed is current\n\
1134 when the hook functions are called.\n\n\
1135 Any processes that have this buffer as the `process-buffer' are killed\n\
1136 with SIGHUP.")
1137 (buffer)
1138 Lisp_Object buffer;
1140 Lisp_Object buf;
1141 register struct buffer *b;
1142 register Lisp_Object tem;
1143 register struct Lisp_Marker *m;
1144 struct gcpro gcpro1;
1146 if (NILP (buffer))
1147 buf = Fcurrent_buffer ();
1148 else
1149 buf = Fget_buffer (buffer);
1150 if (NILP (buf))
1151 nsberror (buffer);
1153 b = XBUFFER (buf);
1155 /* Avoid trouble for buffer already dead. */
1156 if (NILP (b->name))
1157 return Qnil;
1159 /* Query if the buffer is still modified. */
1160 if (INTERACTIVE && !NILP (b->filename)
1161 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1163 GCPRO1 (buf);
1164 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1165 XSTRING (b->name)->data));
1166 UNGCPRO;
1167 if (NILP (tem))
1168 return Qnil;
1171 /* Run hooks with the buffer to be killed the current buffer. */
1173 int count = specpdl_ptr - specpdl;
1174 Lisp_Object list;
1176 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1177 set_buffer_internal (b);
1179 /* First run the query functions; if any query is answered no,
1180 don't kill the buffer. */
1181 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1183 tem = call0 (Fcar (list));
1184 if (NILP (tem))
1185 return unbind_to (count, Qnil);
1188 /* Then run the hooks. */
1189 if (!NILP (Vrun_hooks))
1190 call1 (Vrun_hooks, Qkill_buffer_hook);
1191 unbind_to (count, Qnil);
1194 /* We have no more questions to ask. Verify that it is valid
1195 to kill the buffer. This must be done after the questions
1196 since anything can happen within do_yes_or_no_p. */
1198 /* Don't kill the minibuffer now current. */
1199 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1200 return Qnil;
1202 if (NILP (b->name))
1203 return Qnil;
1205 /* When we kill a base buffer, kill all its indirect buffers.
1206 We do it at this stage so nothing terrible happens if they
1207 ask questions or their hooks get errors. */
1208 if (! b->base_buffer)
1210 struct buffer *other;
1212 GCPRO1 (buf);
1214 for (other = all_buffers; other; other = other->next)
1215 /* all_buffers contains dead buffers too;
1216 don't re-kill them. */
1217 if (other->base_buffer == b && !NILP (other->name))
1219 Lisp_Object buf;
1220 XSETBUFFER (buf, other);
1221 Fkill_buffer (buf);
1224 UNGCPRO;
1227 /* Make this buffer not be current.
1228 In the process, notice if this is the sole visible buffer
1229 and give up if so. */
1230 if (b == current_buffer)
1232 tem = Fother_buffer (buf, Qnil, Qnil);
1233 Fset_buffer (tem);
1234 if (b == current_buffer)
1235 return Qnil;
1238 /* Notice if the buffer to kill is the sole visible buffer
1239 when we're currently in the mini-buffer, and give up if so. */
1240 XSETBUFFER (tem, current_buffer);
1241 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1243 tem = Fother_buffer (buf, Qnil, Qnil);
1244 if (EQ (buf, tem))
1245 return Qnil;
1248 /* Now there is no question: we can kill the buffer. */
1250 #ifdef CLASH_DETECTION
1251 /* Unlock this buffer's file, if it is locked. */
1252 unlock_buffer (b);
1253 #endif /* CLASH_DETECTION */
1255 kill_buffer_processes (buf);
1257 tem = Vinhibit_quit;
1258 Vinhibit_quit = Qt;
1259 replace_buffer_in_all_windows (buf);
1260 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1261 frames_discard_buffer (buf);
1262 Vinhibit_quit = tem;
1264 /* Delete any auto-save file, if we saved it in this session. */
1265 if (STRINGP (b->auto_save_file_name)
1266 && b->auto_save_modified != 0
1267 && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
1269 Lisp_Object tem;
1270 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1271 if (! NILP (tem))
1272 internal_delete_file (b->auto_save_file_name);
1275 if (b->base_buffer)
1277 /* Unchain all markers that belong to this indirect buffer.
1278 Don't unchain the markers that belong to the base buffer
1279 or its other indirect buffers. */
1280 for (tem = BUF_MARKERS (b); !NILP (tem); )
1282 Lisp_Object next;
1283 m = XMARKER (tem);
1284 next = m->chain;
1285 if (m->buffer == b)
1286 unchain_marker (tem);
1287 tem = next;
1290 else
1292 /* Unchain all markers of this buffer and its indirect buffers.
1293 and leave them pointing nowhere. */
1294 for (tem = BUF_MARKERS (b); !NILP (tem); )
1296 m = XMARKER (tem);
1297 m->buffer = 0;
1298 tem = m->chain;
1299 m->chain = Qnil;
1301 BUF_MARKERS (b) = Qnil;
1302 BUF_INTERVALS (b) = NULL_INTERVAL;
1304 /* Perhaps we should explicitly free the interval tree here... */
1307 /* Reset the local variables, so that this buffer's local values
1308 won't be protected from GC. They would be protected
1309 if they happened to remain encached in their symbols.
1310 This gets rid of them for certain. */
1311 swap_out_buffer_local_variables (b);
1312 reset_buffer_local_variables (b, 1);
1314 b->name = Qnil;
1316 BLOCK_INPUT;
1317 if (! b->base_buffer)
1318 free_buffer_text (b);
1320 if (b->newline_cache)
1322 free_region_cache (b->newline_cache);
1323 b->newline_cache = 0;
1325 if (b->width_run_cache)
1327 free_region_cache (b->width_run_cache);
1328 b->width_run_cache = 0;
1330 b->width_table = Qnil;
1331 UNBLOCK_INPUT;
1332 b->undo_list = Qnil;
1334 return Qt;
1337 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1338 we do this each time BUF is selected visibly, the more recently
1339 selected buffers are always closer to the front of the list. This
1340 means that other_buffer is more likely to choose a relevant buffer. */
1342 void
1343 record_buffer (buf)
1344 Lisp_Object buf;
1346 register Lisp_Object link, prev;
1347 Lisp_Object frame;
1348 frame = selected_frame;
1350 prev = Qnil;
1351 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1353 if (EQ (XCDR (XCAR (link)), buf))
1354 break;
1355 prev = link;
1358 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1359 we cannot use Fdelq itself here because it allows quitting. */
1361 if (NILP (prev))
1362 Vbuffer_alist = XCDR (Vbuffer_alist);
1363 else
1364 XCDR (prev) = XCDR (XCDR (prev));
1366 XCDR (link) = Vbuffer_alist;
1367 Vbuffer_alist = link;
1369 /* Now move this buffer to the front of frame_buffer_list also. */
1371 prev = Qnil;
1372 for (link = frame_buffer_list (frame); CONSP (link);
1373 link = XCDR (link))
1375 if (EQ (XCAR (link), buf))
1376 break;
1377 prev = link;
1380 /* Effectively do delq. */
1382 if (CONSP (link))
1384 if (NILP (prev))
1385 set_frame_buffer_list (frame,
1386 XCDR (frame_buffer_list (frame)));
1387 else
1388 XCDR (prev) = XCDR (XCDR (prev));
1390 XCDR (link) = frame_buffer_list (frame);
1391 set_frame_buffer_list (frame, link);
1393 else
1394 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1397 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1398 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1399 Use this function before selecting the buffer, since it may need to inspect\n\
1400 the current buffer's major mode.")
1401 (buffer)
1402 Lisp_Object buffer;
1404 int count;
1405 Lisp_Object function;
1407 function = buffer_defaults.major_mode;
1408 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1409 function = current_buffer->major_mode;
1411 if (NILP (function) || EQ (function, Qfundamental_mode))
1412 return Qnil;
1414 count = specpdl_ptr - specpdl;
1416 /* To select a nonfundamental mode,
1417 select the buffer temporarily and then call the mode function. */
1419 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1421 Fset_buffer (buffer);
1422 call0 (function);
1424 return unbind_to (count, Qnil);
1427 /* If switching buffers in WINDOW would be an error, return
1428 a C string saying what the error would be. */
1430 char *
1431 no_switch_window (window)
1432 Lisp_Object window;
1434 Lisp_Object tem;
1435 if (EQ (minibuf_window, window))
1436 return "Cannot switch buffers in minibuffer window";
1437 tem = Fwindow_dedicated_p (window);
1438 if (!NILP (tem))
1439 return "Cannot switch buffers in a dedicated window";
1440 return NULL;
1443 /* Switch to buffer BUFFER in the selected window.
1444 If NORECORD is non-nil, don't call record_buffer. */
1446 Lisp_Object
1447 switch_to_buffer_1 (buffer, norecord)
1448 Lisp_Object buffer, norecord;
1450 register Lisp_Object buf;
1452 if (NILP (buffer))
1453 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1454 else
1456 buf = Fget_buffer (buffer);
1457 if (NILP (buf))
1459 buf = Fget_buffer_create (buffer);
1460 Fset_buffer_major_mode (buf);
1463 Fset_buffer (buf);
1464 if (NILP (norecord))
1465 record_buffer (buf);
1467 Fset_window_buffer (EQ (selected_window, minibuf_window)
1468 ? Fnext_window (minibuf_window, Qnil, Qnil)
1469 : selected_window,
1470 buf);
1472 return buf;
1475 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1476 "Select buffer BUFFER in the current window.\n\
1477 BUFFER may be a buffer or a buffer name.\n\
1478 Optional second arg NORECORD non-nil means\n\
1479 do not put this buffer at the front of the list of recently selected ones.\n\
1481 WARNING: This is NOT the way to work on another buffer temporarily\n\
1482 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1483 the window-buffer correspondences.")
1484 (buffer, norecord)
1485 Lisp_Object buffer, norecord;
1487 char *err;
1489 err = no_switch_window (selected_window);
1490 if (err) error (err);
1492 return switch_to_buffer_1 (buffer, norecord);
1495 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1496 "Select buffer BUFFER in some window, preferably a different one.\n\
1497 If BUFFER is nil, then some other buffer is chosen.\n\
1498 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1499 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1500 window even if BUFFER is already visible in the selected window.\n\
1501 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1502 of `display-buffer' for additional customization information.\n\
1504 Optional third arg NORECORD non-nil means\n\
1505 do not put this buffer at the front of the list of recently selected ones.")
1506 (buffer, other_window, norecord)
1507 Lisp_Object buffer, other_window, norecord;
1509 register Lisp_Object buf;
1510 if (NILP (buffer))
1511 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1512 else
1514 buf = Fget_buffer (buffer);
1515 if (NILP (buf))
1517 buf = Fget_buffer_create (buffer);
1518 Fset_buffer_major_mode (buf);
1521 Fset_buffer (buf);
1522 if (NILP (norecord))
1523 record_buffer (buf);
1524 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
1525 return buf;
1528 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1529 "Return the current buffer as a Lisp object.")
1532 register Lisp_Object buf;
1533 XSETBUFFER (buf, current_buffer);
1534 return buf;
1537 /* Set the current buffer to B.
1539 We previously set windows_or_buffers_changed here to invalidate
1540 global unchanged information in beg_unchanged and end_unchanged.
1541 This is no longer necessary because we now compute unchanged
1542 information on a buffer-basis. Every action affecting other
1543 windows than the selected one requires a select_window at some
1544 time, and that increments windows_or_buffers_changed. */
1546 void
1547 set_buffer_internal (b)
1548 register struct buffer *b;
1550 if (current_buffer != b)
1551 set_buffer_internal_1 (b);
1554 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1555 This is used by redisplay. */
1557 void
1558 set_buffer_internal_1 (b)
1559 register struct buffer *b;
1561 register struct buffer *old_buf;
1562 register Lisp_Object tail, valcontents;
1563 Lisp_Object tem;
1565 #ifdef USE_MMAP_FOR_BUFFERS
1566 if (b->text->beg == NULL)
1567 enlarge_buffer_text (b, 0);
1568 #endif /* USE_MMAP_FOR_BUFFERS */
1570 if (current_buffer == b)
1571 return;
1573 old_buf = current_buffer;
1574 current_buffer = b;
1575 last_known_column_point = -1; /* invalidate indentation cache */
1577 if (old_buf)
1579 /* Put the undo list back in the base buffer, so that it appears
1580 that an indirect buffer shares the undo list of its base. */
1581 if (old_buf->base_buffer)
1582 old_buf->base_buffer->undo_list = old_buf->undo_list;
1584 /* If the old current buffer has markers to record PT, BEGV and ZV
1585 when it is not current, update them now. */
1586 if (! NILP (old_buf->pt_marker))
1588 Lisp_Object obuf;
1589 XSETBUFFER (obuf, old_buf);
1590 set_marker_both (old_buf->pt_marker, obuf,
1591 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1593 if (! NILP (old_buf->begv_marker))
1595 Lisp_Object obuf;
1596 XSETBUFFER (obuf, old_buf);
1597 set_marker_both (old_buf->begv_marker, obuf,
1598 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1600 if (! NILP (old_buf->zv_marker))
1602 Lisp_Object obuf;
1603 XSETBUFFER (obuf, old_buf);
1604 set_marker_both (old_buf->zv_marker, obuf,
1605 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1609 /* Get the undo list from the base buffer, so that it appears
1610 that an indirect buffer shares the undo list of its base. */
1611 if (b->base_buffer)
1612 b->undo_list = b->base_buffer->undo_list;
1614 /* If the new current buffer has markers to record PT, BEGV and ZV
1615 when it is not current, fetch them now. */
1616 if (! NILP (b->pt_marker))
1618 BUF_PT (b) = marker_position (b->pt_marker);
1619 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1621 if (! NILP (b->begv_marker))
1623 BUF_BEGV (b) = marker_position (b->begv_marker);
1624 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1626 if (! NILP (b->zv_marker))
1628 BUF_ZV (b) = marker_position (b->zv_marker);
1629 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1632 /* Look down buffer's list of local Lisp variables
1633 to find and update any that forward into C variables. */
1635 for (tail = b->local_var_alist; !NILP (tail); tail = XCDR (tail))
1637 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1638 if ((BUFFER_LOCAL_VALUEP (valcontents)
1639 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1640 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1641 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1642 /* Just reference the variable
1643 to cause it to become set for this buffer. */
1644 Fsymbol_value (XCAR (XCAR (tail)));
1647 /* Do the same with any others that were local to the previous buffer */
1649 if (old_buf)
1650 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCDR (tail))
1652 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1653 if ((BUFFER_LOCAL_VALUEP (valcontents)
1654 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1655 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1656 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1657 /* Just reference the variable
1658 to cause it to become set for this buffer. */
1659 Fsymbol_value (XCAR (XCAR (tail)));
1663 /* Switch to buffer B temporarily for redisplay purposes.
1664 This avoids certain things that don't need to be done within redisplay. */
1666 void
1667 set_buffer_temp (b)
1668 struct buffer *b;
1670 register struct buffer *old_buf;
1672 if (current_buffer == b)
1673 return;
1675 old_buf = current_buffer;
1676 current_buffer = b;
1678 if (old_buf)
1680 /* If the old current buffer has markers to record PT, BEGV and ZV
1681 when it is not current, update them now. */
1682 if (! NILP (old_buf->pt_marker))
1684 Lisp_Object obuf;
1685 XSETBUFFER (obuf, old_buf);
1686 set_marker_both (old_buf->pt_marker, obuf,
1687 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1689 if (! NILP (old_buf->begv_marker))
1691 Lisp_Object obuf;
1692 XSETBUFFER (obuf, old_buf);
1693 set_marker_both (old_buf->begv_marker, obuf,
1694 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1696 if (! NILP (old_buf->zv_marker))
1698 Lisp_Object obuf;
1699 XSETBUFFER (obuf, old_buf);
1700 set_marker_both (old_buf->zv_marker, obuf,
1701 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1705 /* If the new current buffer has markers to record PT, BEGV and ZV
1706 when it is not current, fetch them now. */
1707 if (! NILP (b->pt_marker))
1709 BUF_PT (b) = marker_position (b->pt_marker);
1710 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1712 if (! NILP (b->begv_marker))
1714 BUF_BEGV (b) = marker_position (b->begv_marker);
1715 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1717 if (! NILP (b->zv_marker))
1719 BUF_ZV (b) = marker_position (b->zv_marker);
1720 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1724 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1725 "Make the buffer BUFFER current for editing operations.\n\
1726 BUFFER may be a buffer or the name of an existing buffer.\n\
1727 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1728 This function does not display the buffer, so its effect ends\n\
1729 when the current command terminates.\n\
1730 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1731 (buffer)
1732 register Lisp_Object buffer;
1734 register Lisp_Object buf;
1735 buf = Fget_buffer (buffer);
1736 if (NILP (buf))
1737 nsberror (buffer);
1738 if (NILP (XBUFFER (buf)->name))
1739 error ("Selecting deleted buffer");
1740 set_buffer_internal (XBUFFER (buf));
1741 return buf;
1744 /* Set the current buffer to BUFFER provided it is alive. */
1746 Lisp_Object
1747 set_buffer_if_live (buffer)
1748 Lisp_Object buffer;
1750 if (! NILP (XBUFFER (buffer)->name))
1751 Fset_buffer (buffer);
1752 return Qnil;
1755 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1756 Sbarf_if_buffer_read_only, 0, 0, 0,
1757 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1760 if (!NILP (current_buffer->read_only)
1761 && NILP (Vinhibit_read_only))
1762 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1763 return Qnil;
1766 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1767 "Put BUFFER at the end of the list of all buffers.\n\
1768 There it is the least likely candidate for `other-buffer' to return;\n\
1769 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1770 If BUFFER is nil or omitted, bury the current buffer.\n\
1771 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1772 selected window if it is displayed there.")
1773 (buffer)
1774 register Lisp_Object buffer;
1776 /* Figure out what buffer we're going to bury. */
1777 if (NILP (buffer))
1779 XSETBUFFER (buffer, current_buffer);
1781 /* If we're burying the current buffer, unshow it. */
1782 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1784 else
1786 Lisp_Object buf1;
1788 buf1 = Fget_buffer (buffer);
1789 if (NILP (buf1))
1790 nsberror (buffer);
1791 buffer = buf1;
1794 /* Move buffer to the end of the buffer list. Do nothing if the
1795 buffer is killed. */
1796 if (!NILP (XBUFFER (buffer)->name))
1798 Lisp_Object aelt, link;
1800 aelt = Frassq (buffer, Vbuffer_alist);
1801 link = Fmemq (aelt, Vbuffer_alist);
1802 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1803 XCDR (link) = Qnil;
1804 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1806 frames_bury_buffer (buffer);
1809 return Qnil;
1812 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1813 "Delete the entire contents of the current buffer.\n\
1814 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1815 so the buffer is truly empty after this.")
1818 Fwiden ();
1820 del_range (BEG, Z);
1822 current_buffer->last_window_start = 1;
1823 /* Prevent warnings, or suspension of auto saving, that would happen
1824 if future size is less than past size. Use of erase-buffer
1825 implies that the future text is not really related to the past text. */
1826 XSETFASTINT (current_buffer->save_length, 0);
1827 return Qnil;
1830 void
1831 validate_region (b, e)
1832 register Lisp_Object *b, *e;
1834 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1835 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1837 if (XINT (*b) > XINT (*e))
1839 Lisp_Object tem;
1840 tem = *b; *b = *e; *e = tem;
1843 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1844 && XINT (*e) <= ZV))
1845 args_out_of_range (*b, *e);
1848 /* Advance BYTE_POS up to a character boundary
1849 and return the adjusted position. */
1851 static int
1852 advance_to_char_boundary (byte_pos)
1853 int byte_pos;
1855 int c;
1857 if (byte_pos == BEG)
1858 /* Beginning of buffer is always a character boundary. */
1859 return 1;
1861 c = FETCH_BYTE (byte_pos);
1862 if (! CHAR_HEAD_P (c))
1864 /* We should advance BYTE_POS only when C is a constituent of a
1865 multibyte sequence. */
1866 int orig_byte_pos = byte_pos;
1870 byte_pos--;
1871 c = FETCH_BYTE (byte_pos);
1873 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
1874 INC_POS (byte_pos);
1875 if (byte_pos < orig_byte_pos)
1876 byte_pos = orig_byte_pos;
1877 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1878 surely advance to the correct character boundary. If C is
1879 not, BYTE_POS was unchanged. */
1882 return byte_pos;
1885 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1886 1, 1, 0,
1887 "Set the multibyte flag of the current buffer to FLAG.\n\
1888 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1889 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1890 The buffer contents remain unchanged as a sequence of bytes\n\
1891 but the contents viewed as characters do change.")
1892 (flag)
1893 Lisp_Object flag;
1895 Lisp_Object tail, markers;
1896 struct buffer *other;
1897 int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
1898 int begv = BEGV, zv = ZV;
1899 int narrowed = (BEG != begv || Z != zv);
1900 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
1902 if (current_buffer->base_buffer)
1903 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
1905 /* Do nothing if nothing actually changes. */
1906 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
1907 return flag;
1909 /* It would be better to update the list,
1910 but this is good enough for now. */
1911 if (undo_enabled_p)
1912 current_buffer->undo_list = Qt;
1914 /* If the cached position is for this buffer, clear it out. */
1915 clear_charpos_cache (current_buffer);
1917 if (narrowed)
1918 Fwiden ();
1920 if (NILP (flag))
1922 int pos, stop;
1923 unsigned char *p;
1925 /* Do this first, so it can use CHAR_TO_BYTE
1926 to calculate the old correspondences. */
1927 set_intervals_multibyte (0);
1929 current_buffer->enable_multibyte_characters = Qnil;
1931 Z = Z_BYTE;
1932 BEGV = BEGV_BYTE;
1933 ZV = ZV_BYTE;
1934 GPT = GPT_BYTE;
1935 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
1937 tail = BUF_MARKERS (current_buffer);
1938 while (! NILP (tail))
1940 XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
1941 tail = XMARKER (tail)->chain;
1944 /* Convert multibyte form of 8-bit characters to unibyte. */
1945 pos = BEG;
1946 stop = GPT;
1947 p = BEG_ADDR;
1948 while (1)
1950 int c, bytes;
1952 if (pos == stop)
1954 if (pos == Z)
1955 break;
1956 p = GAP_END_ADDR;
1957 stop = Z;
1959 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
1960 p += bytes, pos += bytes;
1961 else
1963 c = STRING_CHAR (p, stop - pos);
1964 /* Delete all bytes for this 8-bit character but the
1965 last one, and change the last one to the charcter
1966 code. */
1967 bytes--;
1968 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
1969 p = GAP_END_ADDR;
1970 *p++ = c;
1971 pos++;
1972 if (begv > pos)
1973 begv -= bytes;
1974 if (zv > pos)
1975 zv -= bytes;
1976 stop = Z;
1979 if (narrowed)
1980 Fnarrow_to_region (make_number (begv), make_number (zv));
1982 else
1984 int pt = PT;
1985 int pos, stop;
1986 unsigned char *p;
1988 /* Be sure not to have a multibyte sequence striding over the GAP.
1989 Ex: We change this: "...abc\201 _GAP_ \241def..."
1990 to: "...abc _GAP_ \201\241def..." */
1992 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
1993 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
1995 unsigned char *p = GPT_ADDR - 1;
1997 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
1998 if (BASE_LEADING_CODE_P (*p))
2000 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2002 move_gap_both (new_gpt, new_gpt);
2006 /* Make the buffer contents valid as multibyte by converting
2007 8-bit characters to multibyte form. */
2008 pos = BEG;
2009 stop = GPT;
2010 p = BEG_ADDR;
2011 while (1)
2013 int bytes;
2015 if (pos == stop)
2017 if (pos == Z)
2018 break;
2019 p = GAP_END_ADDR;
2020 stop = Z;
2023 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2024 p += bytes, pos += bytes;
2025 else
2027 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2029 bytes = CHAR_STRING (*p, tmp);
2030 *p = tmp[0];
2031 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2032 bytes--;
2033 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2034 /* Now the gap is after the just inserted data. */
2035 pos = GPT;
2036 p = GAP_END_ADDR;
2037 if (pos <= begv)
2038 begv += bytes;
2039 if (pos <= zv)
2040 zv += bytes;
2041 if (pos <= pt)
2042 pt += bytes;
2043 stop = Z;
2047 if (pt != PT)
2048 TEMP_SET_PT (pt);
2050 if (narrowed)
2051 Fnarrow_to_region (make_number (begv), make_number (zv));
2053 /* Do this first, so that chars_in_text asks the right question.
2054 set_intervals_multibyte needs it too. */
2055 current_buffer->enable_multibyte_characters = Qt;
2057 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2058 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2060 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2062 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2063 if (BEGV_BYTE > GPT_BYTE)
2064 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2065 else
2066 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2068 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2069 if (ZV_BYTE > GPT_BYTE)
2070 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2071 else
2072 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2075 int pt_byte = advance_to_char_boundary (PT_BYTE);
2076 int pt;
2078 if (pt_byte > GPT_BYTE)
2079 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2080 else
2081 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2082 TEMP_SET_PT_BOTH (pt, pt_byte);
2085 tail = markers = BUF_MARKERS (current_buffer);
2087 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2088 getting confused by the markers that have not yet been updated.
2089 It is also a signal that it should never create a marker. */
2090 BUF_MARKERS (current_buffer) = Qnil;
2092 while (! NILP (tail))
2094 XMARKER (tail)->bytepos
2095 = advance_to_char_boundary (XMARKER (tail)->bytepos);
2096 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
2098 tail = XMARKER (tail)->chain;
2101 /* Make sure no markers were put on the chain
2102 while the chain value was incorrect. */
2103 if (! EQ (BUF_MARKERS (current_buffer), Qnil))
2104 abort ();
2106 BUF_MARKERS (current_buffer) = markers;
2108 /* Do this last, so it can calculate the new correspondences
2109 between chars and bytes. */
2110 set_intervals_multibyte (1);
2113 if (undo_enabled_p)
2114 current_buffer->undo_list = Qnil;
2116 /* Changing the multibyteness of a buffer means that all windows
2117 showing that buffer must be updated thoroughly. */
2118 current_buffer->prevent_redisplay_optimizations_p = 1;
2119 ++windows_or_buffers_changed;
2121 /* Copy this buffer's new multibyte status
2122 into all of its indirect buffers. */
2123 for (other = all_buffers; other; other = other->next)
2124 if (other->base_buffer == current_buffer && !NILP (other->name))
2126 other->enable_multibyte_characters
2127 = current_buffer->enable_multibyte_characters;
2128 other->prevent_redisplay_optimizations_p = 1;
2131 /* Restore the modifiedness of the buffer. */
2132 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2133 Fset_buffer_modified_p (Qnil);
2135 return flag;
2138 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2139 0, 0, 0,
2140 "Switch to Fundamental mode by killing current buffer's local variables.\n\
2141 Most local variable bindings are eliminated so that the default values\n\
2142 become effective once more. Also, the syntax table is set from\n\
2143 `standard-syntax-table', the local keymap is set to nil,\n\
2144 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
2145 This function also forces redisplay of the mode line.\n\
2147 Every function to select a new major mode starts by\n\
2148 calling this function.\n\n\
2149 As a special exception, local variables whose names have\n\
2150 a non-nil `permanent-local' property are not eliminated by this function.\n\
2152 The first thing this function does is run\n\
2153 the normal hook `change-major-mode-hook'.")
2156 register Lisp_Object alist, sym, tem;
2157 Lisp_Object oalist;
2159 if (!NILP (Vrun_hooks))
2160 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
2161 oalist = current_buffer->local_var_alist;
2163 /* Make sure none of the bindings in oalist
2164 remain swapped in, in their symbols. */
2166 swap_out_buffer_local_variables (current_buffer);
2168 /* Actually eliminate all local bindings of this buffer. */
2170 reset_buffer_local_variables (current_buffer, 0);
2172 /* Redisplay mode lines; we are changing major mode. */
2174 update_mode_lines++;
2176 /* Any which are supposed to be permanent,
2177 make local again, with the same values they had. */
2179 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2181 sym = XCAR (XCAR (alist));
2182 tem = Fget (sym, Qpermanent_local);
2183 if (! NILP (tem))
2185 Fmake_local_variable (sym);
2186 Fset (sym, XCDR (XCAR (alist)));
2190 /* Force mode-line redisplay. Useful here because all major mode
2191 commands call this function. */
2192 update_mode_lines++;
2194 return Qnil;
2197 /* Make sure no local variables remain set up with buffer B
2198 for their current values. */
2200 static void
2201 swap_out_buffer_local_variables (b)
2202 struct buffer *b;
2204 Lisp_Object oalist, alist, sym, tem, buffer;
2206 XSETBUFFER (buffer, b);
2207 oalist = b->local_var_alist;
2209 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2211 sym = XCAR (XCAR (alist));
2213 /* Need not do anything if some other buffer's binding is now encached. */
2214 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
2215 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2217 /* Symbol is set up for this buffer's old local value.
2218 Set it up for the current buffer with the default value. */
2220 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr;
2221 /* Store the symbol's current value into the alist entry
2222 it is currently set up for. This is so that, if the
2223 local is marked permanent, and we make it local again
2224 later in Fkill_all_local_variables, we don't lose the value. */
2225 XCDR (XCAR (tem))
2226 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
2227 /* Switch to the symbol's default-value alist entry. */
2228 XCAR (tem) = tem;
2229 /* Mark it as current for buffer B. */
2230 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
2231 /* Store the current value into any forwarding in the symbol. */
2232 store_symval_forwarding (sym,
2233 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
2234 XCDR (tem));
2239 /* Find all the overlays in the current buffer that contain position POS.
2240 Return the number found, and store them in a vector in *VEC_PTR.
2241 Store in *LEN_PTR the size allocated for the vector.
2242 Store in *NEXT_PTR the next position after POS where an overlay starts,
2243 or ZV if there are no more overlays.
2244 Store in *PREV_PTR the previous position before POS where an overlay ends,
2245 or where an overlay starts which ends at or after POS;
2246 or BEGV if there are no such overlays.
2247 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2249 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2250 when this function is called.
2252 If EXTEND is non-zero, we make the vector bigger if necessary.
2253 If EXTEND is zero, we never extend the vector,
2254 and we store only as many overlays as will fit.
2255 But we still return the total number of overlays.
2257 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2258 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2259 default (BEGV or ZV). */
2262 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2263 int pos;
2264 int extend;
2265 Lisp_Object **vec_ptr;
2266 int *len_ptr;
2267 int *next_ptr;
2268 int *prev_ptr;
2269 int change_req;
2271 Lisp_Object tail, overlay, start, end;
2272 int idx = 0;
2273 int len = *len_ptr;
2274 Lisp_Object *vec = *vec_ptr;
2275 int next = ZV;
2276 int prev = BEGV;
2277 int inhibit_storing = 0;
2279 for (tail = current_buffer->overlays_before;
2280 GC_CONSP (tail);
2281 tail = XCDR (tail))
2283 int startpos, endpos;
2285 overlay = XCAR (tail);
2287 start = OVERLAY_START (overlay);
2288 end = OVERLAY_END (overlay);
2289 endpos = OVERLAY_POSITION (end);
2290 if (endpos < pos)
2292 if (prev < endpos)
2293 prev = endpos;
2294 break;
2296 startpos = OVERLAY_POSITION (start);
2297 /* This one ends at or after POS
2298 so its start counts for PREV_PTR if it's before POS. */
2299 if (prev < startpos && startpos < pos)
2300 prev = startpos;
2301 if (endpos == pos)
2302 continue;
2303 if (startpos <= pos)
2305 if (idx == len)
2307 /* The supplied vector is full.
2308 Either make it bigger, or don't store any more in it. */
2309 if (extend)
2311 /* Make it work with an initial len == 0. */
2312 len *= 2;
2313 if (len == 0)
2314 len = 4;
2315 *len_ptr = len;
2316 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2317 *vec_ptr = vec;
2319 else
2320 inhibit_storing = 1;
2323 if (!inhibit_storing)
2324 vec[idx] = overlay;
2325 /* Keep counting overlays even if we can't return them all. */
2326 idx++;
2328 else if (startpos < next)
2329 next = startpos;
2332 for (tail = current_buffer->overlays_after;
2333 GC_CONSP (tail);
2334 tail = XCDR (tail))
2336 int startpos, endpos;
2338 overlay = XCAR (tail);
2340 start = OVERLAY_START (overlay);
2341 end = OVERLAY_END (overlay);
2342 startpos = OVERLAY_POSITION (start);
2343 if (pos < startpos)
2345 if (startpos < next)
2346 next = startpos;
2347 break;
2349 endpos = OVERLAY_POSITION (end);
2350 if (pos < endpos)
2352 if (idx == len)
2354 if (extend)
2356 *len_ptr = len *= 2;
2357 if (len == 0)
2358 len = *len_ptr = 4;
2359 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2360 *vec_ptr = vec;
2362 else
2363 inhibit_storing = 1;
2366 if (!inhibit_storing)
2367 vec[idx] = overlay;
2368 idx++;
2370 if (startpos < pos && startpos > prev)
2371 prev = startpos;
2373 else if (endpos < pos && endpos > prev)
2374 prev = endpos;
2375 else if (endpos == pos && startpos > prev
2376 && (!change_req || startpos < pos))
2377 prev = startpos;
2380 if (next_ptr)
2381 *next_ptr = next;
2382 if (prev_ptr)
2383 *prev_ptr = prev;
2384 return idx;
2387 /* Find all the overlays in the current buffer that overlap the range BEG-END
2388 or are empty at BEG.
2390 Return the number found, and store them in a vector in *VEC_PTR.
2391 Store in *LEN_PTR the size allocated for the vector.
2392 Store in *NEXT_PTR the next position after POS where an overlay starts,
2393 or ZV if there are no more overlays.
2394 Store in *PREV_PTR the previous position before POS where an overlay ends,
2395 or BEGV if there are no previous overlays.
2396 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2398 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2399 when this function is called.
2401 If EXTEND is non-zero, we make the vector bigger if necessary.
2402 If EXTEND is zero, we never extend the vector,
2403 and we store only as many overlays as will fit.
2404 But we still return the total number of overlays. */
2407 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2408 int beg, end;
2409 int extend;
2410 Lisp_Object **vec_ptr;
2411 int *len_ptr;
2412 int *next_ptr;
2413 int *prev_ptr;
2415 Lisp_Object tail, overlay, ostart, oend;
2416 int idx = 0;
2417 int len = *len_ptr;
2418 Lisp_Object *vec = *vec_ptr;
2419 int next = ZV;
2420 int prev = BEGV;
2421 int inhibit_storing = 0;
2423 for (tail = current_buffer->overlays_before;
2424 GC_CONSP (tail);
2425 tail = XCDR (tail))
2427 int startpos, endpos;
2429 overlay = XCAR (tail);
2431 ostart = OVERLAY_START (overlay);
2432 oend = OVERLAY_END (overlay);
2433 endpos = OVERLAY_POSITION (oend);
2434 if (endpos < beg)
2436 if (prev < endpos)
2437 prev = endpos;
2438 break;
2440 startpos = OVERLAY_POSITION (ostart);
2441 /* Count an interval if it either overlaps the range
2442 or is empty at the start of the range. */
2443 if ((beg < endpos && startpos < end)
2444 || (startpos == endpos && beg == endpos))
2446 if (idx == len)
2448 /* The supplied vector is full.
2449 Either make it bigger, or don't store any more in it. */
2450 if (extend)
2452 *len_ptr = len *= 2;
2453 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2454 *vec_ptr = vec;
2456 else
2457 inhibit_storing = 1;
2460 if (!inhibit_storing)
2461 vec[idx] = overlay;
2462 /* Keep counting overlays even if we can't return them all. */
2463 idx++;
2465 else if (startpos < next)
2466 next = startpos;
2469 for (tail = current_buffer->overlays_after;
2470 GC_CONSP (tail);
2471 tail = XCDR (tail))
2473 int startpos, endpos;
2475 overlay = XCAR (tail);
2477 ostart = OVERLAY_START (overlay);
2478 oend = OVERLAY_END (overlay);
2479 startpos = OVERLAY_POSITION (ostart);
2480 if (end < startpos)
2482 if (startpos < next)
2483 next = startpos;
2484 break;
2486 endpos = OVERLAY_POSITION (oend);
2487 /* Count an interval if it either overlaps the range
2488 or is empty at the start of the range. */
2489 if ((beg < endpos && startpos < end)
2490 || (startpos == endpos && beg == endpos))
2492 if (idx == len)
2494 if (extend)
2496 *len_ptr = len *= 2;
2497 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2498 *vec_ptr = vec;
2500 else
2501 inhibit_storing = 1;
2504 if (!inhibit_storing)
2505 vec[idx] = overlay;
2506 idx++;
2508 else if (endpos < beg && endpos > prev)
2509 prev = endpos;
2512 if (next_ptr)
2513 *next_ptr = next;
2514 if (prev_ptr)
2515 *prev_ptr = prev;
2516 return idx;
2519 /* Fast function to just test if we're at an overlay boundary. */
2521 overlay_touches_p (pos)
2522 int pos;
2524 Lisp_Object tail, overlay;
2526 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
2527 tail = XCDR (tail))
2529 int endpos;
2531 overlay = XCAR (tail);
2532 if (!GC_OVERLAYP (overlay))
2533 abort ();
2535 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2536 if (endpos < pos)
2537 break;
2538 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2539 return 1;
2542 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2543 tail = XCDR (tail))
2545 int startpos;
2547 overlay = XCAR (tail);
2548 if (!GC_OVERLAYP (overlay))
2549 abort ();
2551 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2552 if (pos < startpos)
2553 break;
2554 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2555 return 1;
2557 return 0;
2560 struct sortvec
2562 Lisp_Object overlay;
2563 int beg, end;
2564 int priority;
2567 static int
2568 compare_overlays (v1, v2)
2569 const void *v1, *v2;
2571 const struct sortvec *s1 = (const struct sortvec *) v1;
2572 const struct sortvec *s2 = (const struct sortvec *) v2;
2573 if (s1->priority != s2->priority)
2574 return s1->priority - s2->priority;
2575 if (s1->beg != s2->beg)
2576 return s1->beg - s2->beg;
2577 if (s1->end != s2->end)
2578 return s2->end - s1->end;
2579 return 0;
2582 /* Sort an array of overlays by priority. The array is modified in place.
2583 The return value is the new size; this may be smaller than the original
2584 size if some of the overlays were invalid or were window-specific. */
2586 sort_overlays (overlay_vec, noverlays, w)
2587 Lisp_Object *overlay_vec;
2588 int noverlays;
2589 struct window *w;
2591 int i, j;
2592 struct sortvec *sortvec;
2593 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2595 /* Put the valid and relevant overlays into sortvec. */
2597 for (i = 0, j = 0; i < noverlays; i++)
2599 Lisp_Object tem;
2600 Lisp_Object overlay;
2602 overlay = overlay_vec[i];
2603 if (OVERLAY_VALID (overlay)
2604 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2605 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2607 /* If we're interested in a specific window, then ignore
2608 overlays that are limited to some other window. */
2609 if (w)
2611 Lisp_Object window;
2613 window = Foverlay_get (overlay, Qwindow);
2614 if (WINDOWP (window) && XWINDOW (window) != w)
2615 continue;
2618 /* This overlay is good and counts: put it into sortvec. */
2619 sortvec[j].overlay = overlay;
2620 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2621 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2622 tem = Foverlay_get (overlay, Qpriority);
2623 if (INTEGERP (tem))
2624 sortvec[j].priority = XINT (tem);
2625 else
2626 sortvec[j].priority = 0;
2627 j++;
2630 noverlays = j;
2632 /* Sort the overlays into the proper order: increasing priority. */
2634 if (noverlays > 1)
2635 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2637 for (i = 0; i < noverlays; i++)
2638 overlay_vec[i] = sortvec[i].overlay;
2639 return (noverlays);
2642 struct sortstr
2644 Lisp_Object string, string2;
2645 int size;
2646 int priority;
2649 struct sortstrlist
2651 struct sortstr *buf; /* An array that expands as needed; never freed. */
2652 int size; /* Allocated length of that array. */
2653 int used; /* How much of the array is currently in use. */
2654 int bytes; /* Total length of the strings in buf. */
2657 /* Buffers for storing information about the overlays touching a given
2658 position. These could be automatic variables in overlay_strings, but
2659 it's more efficient to hold onto the memory instead of repeatedly
2660 allocating and freeing it. */
2661 static struct sortstrlist overlay_heads, overlay_tails;
2662 static unsigned char *overlay_str_buf;
2664 /* Allocated length of overlay_str_buf. */
2665 static int overlay_str_len;
2667 /* A comparison function suitable for passing to qsort. */
2668 static int
2669 cmp_for_strings (as1, as2)
2670 char *as1, *as2;
2672 struct sortstr *s1 = (struct sortstr *)as1;
2673 struct sortstr *s2 = (struct sortstr *)as2;
2674 if (s1->size != s2->size)
2675 return s2->size - s1->size;
2676 if (s1->priority != s2->priority)
2677 return s1->priority - s2->priority;
2678 return 0;
2681 static void
2682 record_overlay_string (ssl, str, str2, pri, size)
2683 struct sortstrlist *ssl;
2684 Lisp_Object str, str2, pri;
2685 int size;
2687 int nbytes;
2689 if (ssl->used == ssl->size)
2691 if (ssl->buf)
2692 ssl->size *= 2;
2693 else
2694 ssl->size = 5;
2695 ssl->buf = ((struct sortstr *)
2696 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2698 ssl->buf[ssl->used].string = str;
2699 ssl->buf[ssl->used].string2 = str2;
2700 ssl->buf[ssl->used].size = size;
2701 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2702 ssl->used++;
2704 if (NILP (current_buffer->enable_multibyte_characters))
2705 nbytes = XSTRING (str)->size;
2706 else if (! STRING_MULTIBYTE (str))
2707 nbytes = count_size_as_multibyte (XSTRING (str)->data,
2708 STRING_BYTES (XSTRING (str)));
2709 else
2710 nbytes = STRING_BYTES (XSTRING (str));
2712 ssl->bytes += nbytes;
2714 if (STRINGP (str2))
2716 if (NILP (current_buffer->enable_multibyte_characters))
2717 nbytes = XSTRING (str2)->size;
2718 else if (! STRING_MULTIBYTE (str2))
2719 nbytes = count_size_as_multibyte (XSTRING (str2)->data,
2720 STRING_BYTES (XSTRING (str2)));
2721 else
2722 nbytes = STRING_BYTES (XSTRING (str2));
2724 ssl->bytes += nbytes;
2728 /* Return the concatenation of the strings associated with overlays that
2729 begin or end at POS, ignoring overlays that are specific to a window
2730 other than W. The strings are concatenated in the appropriate order:
2731 shorter overlays nest inside longer ones, and higher priority inside
2732 lower. Normally all of the after-strings come first, but zero-sized
2733 overlays have their after-strings ride along with the before-strings
2734 because it would look strange to print them inside-out.
2736 Returns the string length, and stores the contents indirectly through
2737 PSTR, if that variable is non-null. The string may be overwritten by
2738 subsequent calls. */
2741 overlay_strings (pos, w, pstr)
2742 int pos;
2743 struct window *w;
2744 unsigned char **pstr;
2746 Lisp_Object ov, overlay, window, str;
2747 int startpos, endpos;
2748 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
2750 overlay_heads.used = overlay_heads.bytes = 0;
2751 overlay_tails.used = overlay_tails.bytes = 0;
2752 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
2754 overlay = XCAR (ov);
2755 if (!OVERLAYP (overlay))
2756 abort ();
2758 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2759 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2760 if (endpos < pos)
2761 break;
2762 if (endpos != pos && startpos != pos)
2763 continue;
2764 window = Foverlay_get (overlay, Qwindow);
2765 if (WINDOWP (window) && XWINDOW (window) != w)
2766 continue;
2767 if (startpos == pos
2768 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2769 record_overlay_string (&overlay_heads, str,
2770 (startpos == endpos
2771 ? Foverlay_get (overlay, Qafter_string)
2772 : Qnil),
2773 Foverlay_get (overlay, Qpriority),
2774 endpos - startpos);
2775 else if (endpos == pos
2776 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2777 record_overlay_string (&overlay_tails, str, Qnil,
2778 Foverlay_get (overlay, Qpriority),
2779 endpos - startpos);
2781 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
2783 overlay = XCAR (ov);
2784 if (!OVERLAYP (overlay))
2785 abort ();
2787 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2788 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2789 if (startpos > pos)
2790 break;
2791 if (endpos != pos && startpos != pos)
2792 continue;
2793 window = Foverlay_get (overlay, Qwindow);
2794 if (WINDOWP (window) && XWINDOW (window) != w)
2795 continue;
2796 if (startpos == pos
2797 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2798 record_overlay_string (&overlay_heads, str,
2799 (startpos == endpos
2800 ? Foverlay_get (overlay, Qafter_string)
2801 : Qnil),
2802 Foverlay_get (overlay, Qpriority),
2803 endpos - startpos);
2804 else if (endpos == pos
2805 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2806 record_overlay_string (&overlay_tails, str, Qnil,
2807 Foverlay_get (overlay, Qpriority),
2808 endpos - startpos);
2810 if (overlay_tails.used > 1)
2811 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2812 cmp_for_strings);
2813 if (overlay_heads.used > 1)
2814 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2815 cmp_for_strings);
2816 if (overlay_heads.bytes || overlay_tails.bytes)
2818 Lisp_Object tem;
2819 int i;
2820 unsigned char *p;
2821 int total = overlay_heads.bytes + overlay_tails.bytes;
2823 if (total > overlay_str_len)
2825 overlay_str_len = total;
2826 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2827 total);
2829 p = overlay_str_buf;
2830 for (i = overlay_tails.used; --i >= 0;)
2832 int nbytes;
2833 tem = overlay_tails.buf[i].string;
2834 nbytes = copy_text (XSTRING (tem)->data, p,
2835 STRING_BYTES (XSTRING (tem)),
2836 STRING_MULTIBYTE (tem), multibyte);
2837 p += nbytes;
2839 for (i = 0; i < overlay_heads.used; ++i)
2841 int nbytes;
2842 tem = overlay_heads.buf[i].string;
2843 nbytes = copy_text (XSTRING (tem)->data, p,
2844 STRING_BYTES (XSTRING (tem)),
2845 STRING_MULTIBYTE (tem), multibyte);
2846 p += nbytes;
2847 tem = overlay_heads.buf[i].string2;
2848 if (STRINGP (tem))
2850 nbytes = copy_text (XSTRING (tem)->data, p,
2851 STRING_BYTES (XSTRING (tem)),
2852 STRING_MULTIBYTE (tem), multibyte);
2853 p += nbytes;
2856 if (p != overlay_str_buf + total)
2857 abort ();
2858 if (pstr)
2859 *pstr = overlay_str_buf;
2860 return total;
2862 return 0;
2865 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2867 void
2868 recenter_overlay_lists (buf, pos)
2869 struct buffer *buf;
2870 int pos;
2872 Lisp_Object overlay, tail, next, prev, beg, end;
2874 /* See if anything in overlays_before should move to overlays_after. */
2876 /* We don't strictly need prev in this loop; it should always be nil.
2877 But we use it for symmetry and in case that should cease to be true
2878 with some future change. */
2879 prev = Qnil;
2880 for (tail = buf->overlays_before;
2881 CONSP (tail);
2882 prev = tail, tail = next)
2884 next = XCDR (tail);
2885 overlay = XCAR (tail);
2887 /* If the overlay is not valid, get rid of it. */
2888 if (!OVERLAY_VALID (overlay))
2889 #if 1
2890 abort ();
2891 #else
2893 /* Splice the cons cell TAIL out of overlays_before. */
2894 if (!NILP (prev))
2895 XCDR (prev) = next;
2896 else
2897 buf->overlays_before = next;
2898 tail = prev;
2899 continue;
2901 #endif
2903 beg = OVERLAY_START (overlay);
2904 end = OVERLAY_END (overlay);
2906 if (OVERLAY_POSITION (end) > pos)
2908 /* OVERLAY needs to be moved. */
2909 int where = OVERLAY_POSITION (beg);
2910 Lisp_Object other, other_prev;
2912 /* Splice the cons cell TAIL out of overlays_before. */
2913 if (!NILP (prev))
2914 XCDR (prev) = next;
2915 else
2916 buf->overlays_before = next;
2918 /* Search thru overlays_after for where to put it. */
2919 other_prev = Qnil;
2920 for (other = buf->overlays_after;
2921 CONSP (other);
2922 other_prev = other, other = XCDR (other))
2924 Lisp_Object otherbeg, otheroverlay;
2926 otheroverlay = XCAR (other);
2927 if (! OVERLAY_VALID (otheroverlay))
2928 abort ();
2930 otherbeg = OVERLAY_START (otheroverlay);
2931 if (OVERLAY_POSITION (otherbeg) >= where)
2932 break;
2935 /* Add TAIL to overlays_after before OTHER. */
2936 XCDR (tail) = other;
2937 if (!NILP (other_prev))
2938 XCDR (other_prev) = tail;
2939 else
2940 buf->overlays_after = tail;
2941 tail = prev;
2943 else
2944 /* We've reached the things that should stay in overlays_before.
2945 All the rest of overlays_before must end even earlier,
2946 so stop now. */
2947 break;
2950 /* See if anything in overlays_after should be in overlays_before. */
2951 prev = Qnil;
2952 for (tail = buf->overlays_after;
2953 CONSP (tail);
2954 prev = tail, tail = next)
2956 next = XCDR (tail);
2957 overlay = XCAR (tail);
2959 /* If the overlay is not valid, get rid of it. */
2960 if (!OVERLAY_VALID (overlay))
2961 #if 1
2962 abort ();
2963 #else
2965 /* Splice the cons cell TAIL out of overlays_after. */
2966 if (!NILP (prev))
2967 XCDR (prev) = next;
2968 else
2969 buf->overlays_after = next;
2970 tail = prev;
2971 continue;
2973 #endif
2975 beg = OVERLAY_START (overlay);
2976 end = OVERLAY_END (overlay);
2978 /* Stop looking, when we know that nothing further
2979 can possibly end before POS. */
2980 if (OVERLAY_POSITION (beg) > pos)
2981 break;
2983 if (OVERLAY_POSITION (end) <= pos)
2985 /* OVERLAY needs to be moved. */
2986 int where = OVERLAY_POSITION (end);
2987 Lisp_Object other, other_prev;
2989 /* Splice the cons cell TAIL out of overlays_after. */
2990 if (!NILP (prev))
2991 XCDR (prev) = next;
2992 else
2993 buf->overlays_after = next;
2995 /* Search thru overlays_before for where to put it. */
2996 other_prev = Qnil;
2997 for (other = buf->overlays_before;
2998 CONSP (other);
2999 other_prev = other, other = XCDR (other))
3001 Lisp_Object otherend, otheroverlay;
3003 otheroverlay = XCAR (other);
3004 if (! OVERLAY_VALID (otheroverlay))
3005 abort ();
3007 otherend = OVERLAY_END (otheroverlay);
3008 if (OVERLAY_POSITION (otherend) <= where)
3009 break;
3012 /* Add TAIL to overlays_before before OTHER. */
3013 XCDR (tail) = other;
3014 if (!NILP (other_prev))
3015 XCDR (other_prev) = tail;
3016 else
3017 buf->overlays_before = tail;
3018 tail = prev;
3022 XSETFASTINT (buf->overlay_center, pos);
3025 void
3026 adjust_overlays_for_insert (pos, length)
3027 int pos;
3028 int length;
3030 /* After an insertion, the lists are still sorted properly,
3031 but we may need to update the value of the overlay center. */
3032 if (XFASTINT (current_buffer->overlay_center) >= pos)
3033 XSETFASTINT (current_buffer->overlay_center,
3034 XFASTINT (current_buffer->overlay_center) + length);
3037 void
3038 adjust_overlays_for_delete (pos, length)
3039 int pos;
3040 int length;
3042 if (XFASTINT (current_buffer->overlay_center) < pos)
3043 /* The deletion was to our right. No change needed; the before- and
3044 after-lists are still consistent. */
3046 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
3047 /* The deletion was to our left. We need to adjust the center value
3048 to account for the change in position, but the lists are consistent
3049 given the new value. */
3050 XSETFASTINT (current_buffer->overlay_center,
3051 XFASTINT (current_buffer->overlay_center) - length);
3052 else
3053 /* We're right in the middle. There might be things on the after-list
3054 that now belong on the before-list. Recentering will move them,
3055 and also update the center point. */
3056 recenter_overlay_lists (current_buffer, pos);
3059 /* Fix up overlays that were garbled as a result of permuting markers
3060 in the range START through END. Any overlay with at least one
3061 endpoint in this range will need to be unlinked from the overlay
3062 list and reinserted in its proper place.
3063 Such an overlay might even have negative size at this point.
3064 If so, we'll reverse the endpoints. Can you think of anything
3065 better to do in this situation? */
3066 void
3067 fix_overlays_in_range (start, end)
3068 register int start, end;
3070 Lisp_Object overlay;
3071 Lisp_Object before_list, after_list;
3072 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
3073 int startpos, endpos;
3075 /* This algorithm shifts links around instead of consing and GCing.
3076 The loop invariant is that before_list (resp. after_list) is a
3077 well-formed list except that its last element, the one that
3078 *pbefore (resp. *pafter) points to, is still uninitialized.
3079 So it's not a bug that before_list isn't initialized, although
3080 it may look strange. */
3081 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
3083 overlay = XCAR (*ptail);
3084 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3085 if (endpos < start)
3086 break;
3087 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3088 if (endpos < end
3089 || (startpos >= start && startpos < end))
3091 /* If the overlay is backwards, fix that now. */
3092 if (startpos > endpos)
3094 int tem;
3095 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3096 Qnil);
3097 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3098 Qnil);
3099 tem = startpos; startpos = endpos; endpos = tem;
3101 /* Add it to the end of the wrong list. Later on,
3102 recenter_overlay_lists will move it to the right place. */
3103 if (endpos < XINT (current_buffer->overlay_center))
3105 *pafter = *ptail;
3106 pafter = &XCDR (*ptail);
3108 else
3110 *pbefore = *ptail;
3111 pbefore = &XCDR (*ptail);
3113 *ptail = XCDR (*ptail);
3115 else
3116 ptail = &XCDR (*ptail);
3118 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
3120 overlay = XCAR (*ptail);
3121 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3122 if (startpos >= end)
3123 break;
3124 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3125 if (startpos >= start
3126 || (endpos >= start && endpos < end))
3128 if (startpos > endpos)
3130 int tem;
3131 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3132 Qnil);
3133 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3134 Qnil);
3135 tem = startpos; startpos = endpos; endpos = tem;
3137 if (endpos < XINT (current_buffer->overlay_center))
3139 *pafter = *ptail;
3140 pafter = &XCDR (*ptail);
3142 else
3144 *pbefore = *ptail;
3145 pbefore = &XCDR (*ptail);
3147 *ptail = XCDR (*ptail);
3149 else
3150 ptail = &XCDR (*ptail);
3153 /* Splice the constructed (wrong) lists into the buffer's lists,
3154 and let the recenter function make it sane again. */
3155 *pbefore = current_buffer->overlays_before;
3156 current_buffer->overlays_before = before_list;
3157 recenter_overlay_lists (current_buffer,
3158 XINT (current_buffer->overlay_center));
3160 *pafter = current_buffer->overlays_after;
3161 current_buffer->overlays_after = after_list;
3162 recenter_overlay_lists (current_buffer,
3163 XINT (current_buffer->overlay_center));
3166 /* We have two types of overlay: the one whose ending marker is
3167 after-insertion-marker (this is the usual case) and the one whose
3168 ending marker is before-insertion-marker. When `overlays_before'
3169 contains overlays of the latter type and the former type in this
3170 order and both overlays end at inserting position, inserting a text
3171 increases only the ending marker of the latter type, which results
3172 in incorrect ordering of `overlays_before'.
3174 This function fixes ordering of overlays in the slot
3175 `overlays_before' of the buffer *BP. Before the insertion, `point'
3176 was at PREV, and now is at POS. */
3178 void
3179 fix_overlays_before (bp, prev, pos)
3180 struct buffer *bp;
3181 int prev, pos;
3183 Lisp_Object *tailp = &bp->overlays_before;
3184 Lisp_Object *right_place;
3185 int end;
3187 /* After the insertion, the several overlays may be in incorrect
3188 order. The possibility is that, in the list `overlays_before',
3189 an overlay which ends at POS appears after an overlay which ends
3190 at PREV. Since POS is greater than PREV, we must fix the
3191 ordering of these overlays, by moving overlays ends at POS before
3192 the overlays ends at PREV. */
3194 /* At first, find a place where disordered overlays should be linked
3195 in. It is where an overlay which end before POS exists. (i.e. an
3196 overlay whose ending marker is after-insertion-marker if disorder
3197 exists). */
3198 while (!NILP (*tailp)
3199 && ((end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp))))
3200 >= pos))
3201 tailp = &XCDR (*tailp);
3203 /* If we don't find such an overlay,
3204 or the found one ends before PREV,
3205 or the found one is the last one in the list,
3206 we don't have to fix anything. */
3207 if (NILP (*tailp)
3208 || end < prev
3209 || NILP (XCDR (*tailp)))
3210 return;
3212 right_place = tailp;
3213 tailp = &XCDR (*tailp);
3215 /* Now, end position of overlays in the list *TAILP should be before
3216 or equal to PREV. In the loop, an overlay which ends at POS is
3217 moved ahead to the place pointed by RIGHT_PLACE. If we found an
3218 overlay which ends before PREV, the remaining overlays are in
3219 correct order. */
3220 while (!NILP (*tailp))
3222 end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp)));
3224 if (end == pos)
3225 { /* This overlay is disordered. */
3226 Lisp_Object found = *tailp;
3228 /* Unlink the found overlay. */
3229 *tailp = XCDR (found);
3230 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
3231 XCDR (found) = *right_place;
3232 /* Link it into the right place. */
3233 *right_place = found;
3235 else if (end == prev)
3236 tailp = &XCDR (*tailp);
3237 else /* No more disordered overlay. */
3238 break;
3242 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3243 "Return t if OBJECT is an overlay.")
3244 (object)
3245 Lisp_Object object;
3247 return (OVERLAYP (object) ? Qt : Qnil);
3250 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3251 "Create a new overlay with range BEG to END in BUFFER.\n\
3252 If omitted, BUFFER defaults to the current buffer.\n\
3253 BEG and END may be integers or markers.\n\
3254 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
3255 front delimiter advance when text is inserted there.\n\
3256 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
3257 rear delimiter advance when text is inserted there.")
3258 (beg, end, buffer, front_advance, rear_advance)
3259 Lisp_Object beg, end, buffer;
3260 Lisp_Object front_advance, rear_advance;
3262 Lisp_Object overlay;
3263 struct buffer *b;
3265 if (NILP (buffer))
3266 XSETBUFFER (buffer, current_buffer);
3267 else
3268 CHECK_BUFFER (buffer, 2);
3269 if (MARKERP (beg)
3270 && ! EQ (Fmarker_buffer (beg), buffer))
3271 error ("Marker points into wrong buffer");
3272 if (MARKERP (end)
3273 && ! EQ (Fmarker_buffer (end), buffer))
3274 error ("Marker points into wrong buffer");
3276 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3277 CHECK_NUMBER_COERCE_MARKER (end, 1);
3279 if (XINT (beg) > XINT (end))
3281 Lisp_Object temp;
3282 temp = beg; beg = end; end = temp;
3285 b = XBUFFER (buffer);
3287 beg = Fset_marker (Fmake_marker (), beg, buffer);
3288 end = Fset_marker (Fmake_marker (), end, buffer);
3290 if (!NILP (front_advance))
3291 XMARKER (beg)->insertion_type = 1;
3292 if (!NILP (rear_advance))
3293 XMARKER (end)->insertion_type = 1;
3295 overlay = allocate_misc ();
3296 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3297 XOVERLAY (overlay)->start = beg;
3298 XOVERLAY (overlay)->end = end;
3299 XOVERLAY (overlay)->plist = Qnil;
3301 /* Put the new overlay on the wrong list. */
3302 end = OVERLAY_END (overlay);
3303 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3304 b->overlays_after = Fcons (overlay, b->overlays_after);
3305 else
3306 b->overlays_before = Fcons (overlay, b->overlays_before);
3308 /* This puts it in the right list, and in the right order. */
3309 recenter_overlay_lists (b, XINT (b->overlay_center));
3311 /* We don't need to redisplay the region covered by the overlay, because
3312 the overlay has no properties at the moment. */
3314 return overlay;
3317 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3319 static void
3320 modify_overlay (buf, start, end)
3321 struct buffer *buf;
3322 int start, end;
3324 if (start == end)
3325 return;
3327 if (start > end)
3329 int temp = start;
3330 start = end; end = temp;
3333 BUF_COMPUTE_UNCHANGED (buf, start, end);
3335 /* If this is a buffer not in the selected window,
3336 we must do other windows. */
3337 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3338 windows_or_buffers_changed = 1;
3339 /* If multiple windows show this buffer, we must do other windows. */
3340 else if (buffer_shared > 1)
3341 windows_or_buffers_changed = 1;
3343 ++BUF_OVERLAY_MODIFF (buf);
3346 \f\f
3347 Lisp_Object Fdelete_overlay ();
3349 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3350 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3351 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3352 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3353 buffer.")
3354 (overlay, beg, end, buffer)
3355 Lisp_Object overlay, beg, end, buffer;
3357 struct buffer *b, *ob;
3358 Lisp_Object obuffer;
3359 int count = specpdl_ptr - specpdl;
3361 CHECK_OVERLAY (overlay, 0);
3362 if (NILP (buffer))
3363 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3364 if (NILP (buffer))
3365 XSETBUFFER (buffer, current_buffer);
3366 CHECK_BUFFER (buffer, 3);
3368 if (MARKERP (beg)
3369 && ! EQ (Fmarker_buffer (beg), buffer))
3370 error ("Marker points into wrong buffer");
3371 if (MARKERP (end)
3372 && ! EQ (Fmarker_buffer (end), buffer))
3373 error ("Marker points into wrong buffer");
3375 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3376 CHECK_NUMBER_COERCE_MARKER (end, 1);
3378 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3379 return Fdelete_overlay (overlay);
3381 if (XINT (beg) > XINT (end))
3383 Lisp_Object temp;
3384 temp = beg; beg = end; end = temp;
3387 specbind (Qinhibit_quit, Qt);
3389 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3390 b = XBUFFER (buffer);
3391 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3393 /* If the overlay has changed buffers, do a thorough redisplay. */
3394 if (!EQ (buffer, obuffer))
3396 /* Redisplay where the overlay was. */
3397 if (!NILP (obuffer))
3399 int o_beg;
3400 int o_end;
3402 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3403 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3405 modify_overlay (ob, o_beg, o_end);
3408 /* Redisplay where the overlay is going to be. */
3409 modify_overlay (b, XINT (beg), XINT (end));
3411 else
3412 /* Redisplay the area the overlay has just left, or just enclosed. */
3414 int o_beg, o_end;
3416 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3417 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3419 if (o_beg == XINT (beg))
3420 modify_overlay (b, o_end, XINT (end));
3421 else if (o_end == XINT (end))
3422 modify_overlay (b, o_beg, XINT (beg));
3423 else
3425 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3426 if (XINT (end) > o_end) o_end = XINT (end);
3427 modify_overlay (b, o_beg, o_end);
3431 if (!NILP (obuffer))
3433 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
3434 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
3437 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3438 Fset_marker (OVERLAY_END (overlay), end, buffer);
3440 /* Put the overlay on the wrong list. */
3441 end = OVERLAY_END (overlay);
3442 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3443 b->overlays_after = Fcons (overlay, b->overlays_after);
3444 else
3445 b->overlays_before = Fcons (overlay, b->overlays_before);
3447 /* This puts it in the right list, and in the right order. */
3448 recenter_overlay_lists (b, XINT (b->overlay_center));
3450 return unbind_to (count, overlay);
3453 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3454 "Delete the overlay OVERLAY from its buffer.")
3455 (overlay)
3456 Lisp_Object overlay;
3458 Lisp_Object buffer;
3459 struct buffer *b;
3460 int count = specpdl_ptr - specpdl;
3462 CHECK_OVERLAY (overlay, 0);
3464 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3465 if (NILP (buffer))
3466 return Qnil;
3468 b = XBUFFER (buffer);
3470 specbind (Qinhibit_quit, Qt);
3472 b->overlays_before = Fdelq (overlay, b->overlays_before);
3473 b->overlays_after = Fdelq (overlay, b->overlays_after);
3475 modify_overlay (b,
3476 marker_position (OVERLAY_START (overlay)),
3477 marker_position (OVERLAY_END (overlay)));
3479 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3480 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3482 return unbind_to (count, Qnil);
3485 /* Overlay dissection functions. */
3487 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3488 "Return the position at which OVERLAY starts.")
3489 (overlay)
3490 Lisp_Object overlay;
3492 CHECK_OVERLAY (overlay, 0);
3494 return (Fmarker_position (OVERLAY_START (overlay)));
3497 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3498 "Return the position at which OVERLAY ends.")
3499 (overlay)
3500 Lisp_Object overlay;
3502 CHECK_OVERLAY (overlay, 0);
3504 return (Fmarker_position (OVERLAY_END (overlay)));
3507 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3508 "Return the buffer OVERLAY belongs to.")
3509 (overlay)
3510 Lisp_Object overlay;
3512 CHECK_OVERLAY (overlay, 0);
3514 return Fmarker_buffer (OVERLAY_START (overlay));
3517 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3518 "Return a list of the properties on OVERLAY.\n\
3519 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3520 OVERLAY.")
3521 (overlay)
3522 Lisp_Object overlay;
3524 CHECK_OVERLAY (overlay, 0);
3526 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3530 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3531 "Return a list of the overlays that contain position POS.")
3532 (pos)
3533 Lisp_Object pos;
3535 int noverlays;
3536 Lisp_Object *overlay_vec;
3537 int len;
3538 Lisp_Object result;
3540 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3542 len = 10;
3543 /* We can't use alloca here because overlays_at can call xrealloc. */
3544 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3546 /* Put all the overlays we want in a vector in overlay_vec.
3547 Store the length in len. */
3548 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3549 (int *) 0, (int *) 0, 0);
3551 /* Make a list of them all. */
3552 result = Flist (noverlays, overlay_vec);
3554 xfree (overlay_vec);
3555 return result;
3558 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3559 "Return a list of the overlays that overlap the region BEG ... END.\n\
3560 Overlap means that at least one character is contained within the overlay\n\
3561 and also contained within the specified region.\n\
3562 Empty overlays are included in the result if they are located at BEG\n\
3563 or between BEG and END.")
3564 (beg, end)
3565 Lisp_Object beg, end;
3567 int noverlays;
3568 Lisp_Object *overlay_vec;
3569 int len;
3570 Lisp_Object result;
3572 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3573 CHECK_NUMBER_COERCE_MARKER (end, 0);
3575 len = 10;
3576 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3578 /* Put all the overlays we want in a vector in overlay_vec.
3579 Store the length in len. */
3580 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3581 (int *) 0, (int *) 0);
3583 /* Make a list of them all. */
3584 result = Flist (noverlays, overlay_vec);
3586 xfree (overlay_vec);
3587 return result;
3590 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3591 1, 1, 0,
3592 "Return the next position after POS where an overlay starts or ends.\n\
3593 If there are no more overlay boundaries after POS, return (point-max).")
3594 (pos)
3595 Lisp_Object pos;
3597 int noverlays;
3598 int endpos;
3599 Lisp_Object *overlay_vec;
3600 int len;
3601 int i;
3603 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3605 len = 10;
3606 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3608 /* Put all the overlays we want in a vector in overlay_vec.
3609 Store the length in len.
3610 endpos gets the position where the next overlay starts. */
3611 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3612 &endpos, (int *) 0, 1);
3614 /* If any of these overlays ends before endpos,
3615 use its ending point instead. */
3616 for (i = 0; i < noverlays; i++)
3618 Lisp_Object oend;
3619 int oendpos;
3621 oend = OVERLAY_END (overlay_vec[i]);
3622 oendpos = OVERLAY_POSITION (oend);
3623 if (oendpos < endpos)
3624 endpos = oendpos;
3627 xfree (overlay_vec);
3628 return make_number (endpos);
3631 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3632 Sprevious_overlay_change, 1, 1, 0,
3633 "Return the previous position before POS where an overlay starts or ends.\n\
3634 If there are no more overlay boundaries before POS, return (point-min).")
3635 (pos)
3636 Lisp_Object pos;
3638 int noverlays;
3639 int prevpos;
3640 Lisp_Object *overlay_vec;
3641 int len;
3643 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3645 /* At beginning of buffer, we know the answer;
3646 avoid bug subtracting 1 below. */
3647 if (XINT (pos) == BEGV)
3648 return pos;
3650 len = 10;
3651 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3653 /* Put all the overlays we want in a vector in overlay_vec.
3654 Store the length in len.
3655 prevpos gets the position of the previous change. */
3656 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3657 (int *) 0, &prevpos, 1);
3659 xfree (overlay_vec);
3660 return make_number (prevpos);
3663 /* These functions are for debugging overlays. */
3665 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3666 "Return a pair of lists giving all the overlays of the current buffer.\n\
3667 The car has all the overlays before the overlay center;\n\
3668 the cdr has all the overlays after the overlay center.\n\
3669 Recentering overlays moves overlays between these lists.\n\
3670 The lists you get are copies, so that changing them has no effect.\n\
3671 However, the overlays you get are the real objects that the buffer uses.")
3674 Lisp_Object before, after;
3675 before = current_buffer->overlays_before;
3676 if (CONSP (before))
3677 before = Fcopy_sequence (before);
3678 after = current_buffer->overlays_after;
3679 if (CONSP (after))
3680 after = Fcopy_sequence (after);
3682 return Fcons (before, after);
3685 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3686 "Recenter the overlays of the current buffer around position POS.")
3687 (pos)
3688 Lisp_Object pos;
3690 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3692 recenter_overlay_lists (current_buffer, XINT (pos));
3693 return Qnil;
3696 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3697 "Get the property of overlay OVERLAY with property name PROP.")
3698 (overlay, prop)
3699 Lisp_Object overlay, prop;
3701 Lisp_Object plist, fallback;
3703 CHECK_OVERLAY (overlay, 0);
3705 fallback = Qnil;
3707 for (plist = XOVERLAY (overlay)->plist;
3708 CONSP (plist) && CONSP (XCDR (plist));
3709 plist = XCDR (XCDR (plist)))
3711 if (EQ (XCAR (plist), prop))
3712 return XCAR (XCDR (plist));
3713 else if (EQ (XCAR (plist), Qcategory))
3715 Lisp_Object tem;
3716 tem = Fcar (Fcdr (plist));
3717 if (SYMBOLP (tem))
3718 fallback = Fget (tem, prop);
3722 return fallback;
3725 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3726 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3727 (overlay, prop, value)
3728 Lisp_Object overlay, prop, value;
3730 Lisp_Object tail, buffer;
3731 int changed;
3733 CHECK_OVERLAY (overlay, 0);
3735 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3737 for (tail = XOVERLAY (overlay)->plist;
3738 CONSP (tail) && CONSP (XCDR (tail));
3739 tail = XCDR (XCDR (tail)))
3740 if (EQ (XCAR (tail), prop))
3742 changed = !EQ (XCAR (XCDR (tail)), value);
3743 XCAR (XCDR (tail)) = value;
3744 goto found;
3746 /* It wasn't in the list, so add it to the front. */
3747 changed = !NILP (value);
3748 XOVERLAY (overlay)->plist
3749 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3750 found:
3751 if (! NILP (buffer))
3753 if (changed)
3754 modify_overlay (XBUFFER (buffer),
3755 marker_position (OVERLAY_START (overlay)),
3756 marker_position (OVERLAY_END (overlay)));
3757 if (EQ (prop, Qevaporate) && ! NILP (value)
3758 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3759 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3760 Fdelete_overlay (overlay);
3762 return value;
3765 /* Subroutine of report_overlay_modification. */
3767 /* Lisp vector holding overlay hook functions to call.
3768 Vector elements come in pairs.
3769 Each even-index element is a list of hook functions.
3770 The following odd-index element is the overlay they came from.
3772 Before the buffer change, we fill in this vector
3773 as we call overlay hook functions.
3774 After the buffer change, we get the functions to call from this vector.
3775 This way we always call the same functions before and after the change. */
3776 static Lisp_Object last_overlay_modification_hooks;
3778 /* Number of elements actually used in last_overlay_modification_hooks. */
3779 static int last_overlay_modification_hooks_used;
3781 /* Add one functionlist/overlay pair
3782 to the end of last_overlay_modification_hooks. */
3784 static void
3785 add_overlay_mod_hooklist (functionlist, overlay)
3786 Lisp_Object functionlist, overlay;
3788 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3790 if (last_overlay_modification_hooks_used == oldsize)
3792 Lisp_Object old;
3793 old = last_overlay_modification_hooks;
3794 last_overlay_modification_hooks
3795 = Fmake_vector (make_number (oldsize * 2), Qnil);
3796 bcopy (XVECTOR (old)->contents,
3797 XVECTOR (last_overlay_modification_hooks)->contents,
3798 sizeof (Lisp_Object) * oldsize);
3800 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3801 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3804 /* Run the modification-hooks of overlays that include
3805 any part of the text in START to END.
3806 If this change is an insertion, also
3807 run the insert-before-hooks of overlay starting at END,
3808 and the insert-after-hooks of overlay ending at START.
3810 This is called both before and after the modification.
3811 AFTER is nonzero when we call after the modification.
3813 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3814 When AFTER is nonzero, they are the start position,
3815 the position after the inserted new text,
3816 and the length of deleted or replaced old text. */
3818 void
3819 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3820 Lisp_Object start, end;
3821 int after;
3822 Lisp_Object arg1, arg2, arg3;
3824 Lisp_Object prop, overlay, tail;
3825 /* 1 if this change is an insertion. */
3826 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3827 int tail_copied;
3828 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3830 overlay = Qnil;
3831 tail = Qnil;
3832 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3834 if (after)
3836 /* Call the functions recorded in last_overlay_modification_hooks
3837 rather than scanning the overlays again.
3838 First copy the vector contents, in case some of these hooks
3839 do subsequent modification of the buffer. */
3840 int size = last_overlay_modification_hooks_used;
3841 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3842 int i;
3844 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3845 copy, size * sizeof (Lisp_Object));
3846 gcpro1.var = copy;
3847 gcpro1.nvars = size;
3849 for (i = 0; i < size;)
3851 Lisp_Object prop, overlay;
3852 prop = copy[i++];
3853 overlay = copy[i++];
3854 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3856 UNGCPRO;
3857 return;
3860 /* We are being called before a change.
3861 Scan the overlays to find the functions to call. */
3862 last_overlay_modification_hooks_used = 0;
3863 tail_copied = 0;
3864 for (tail = current_buffer->overlays_before;
3865 CONSP (tail);
3866 tail = XCDR (tail))
3868 int startpos, endpos;
3869 Lisp_Object ostart, oend;
3871 overlay = XCAR (tail);
3873 ostart = OVERLAY_START (overlay);
3874 oend = OVERLAY_END (overlay);
3875 endpos = OVERLAY_POSITION (oend);
3876 if (XFASTINT (start) > endpos)
3877 break;
3878 startpos = OVERLAY_POSITION (ostart);
3879 if (insertion && (XFASTINT (start) == startpos
3880 || XFASTINT (end) == startpos))
3882 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3883 if (!NILP (prop))
3885 /* Copy TAIL in case the hook recenters the overlay lists. */
3886 if (!tail_copied)
3887 tail = Fcopy_sequence (tail);
3888 tail_copied = 1;
3889 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3892 if (insertion && (XFASTINT (start) == endpos
3893 || XFASTINT (end) == endpos))
3895 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3896 if (!NILP (prop))
3898 if (!tail_copied)
3899 tail = Fcopy_sequence (tail);
3900 tail_copied = 1;
3901 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3904 /* Test for intersecting intervals. This does the right thing
3905 for both insertion and deletion. */
3906 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3908 prop = Foverlay_get (overlay, Qmodification_hooks);
3909 if (!NILP (prop))
3911 if (!tail_copied)
3912 tail = Fcopy_sequence (tail);
3913 tail_copied = 1;
3914 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3919 tail_copied = 0;
3920 for (tail = current_buffer->overlays_after;
3921 CONSP (tail);
3922 tail = XCDR (tail))
3924 int startpos, endpos;
3925 Lisp_Object ostart, oend;
3927 overlay = XCAR (tail);
3929 ostart = OVERLAY_START (overlay);
3930 oend = OVERLAY_END (overlay);
3931 startpos = OVERLAY_POSITION (ostart);
3932 endpos = OVERLAY_POSITION (oend);
3933 if (XFASTINT (end) < startpos)
3934 break;
3935 if (insertion && (XFASTINT (start) == startpos
3936 || XFASTINT (end) == startpos))
3938 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3939 if (!NILP (prop))
3941 if (!tail_copied)
3942 tail = Fcopy_sequence (tail);
3943 tail_copied = 1;
3944 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3947 if (insertion && (XFASTINT (start) == endpos
3948 || XFASTINT (end) == endpos))
3950 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3951 if (!NILP (prop))
3953 if (!tail_copied)
3954 tail = Fcopy_sequence (tail);
3955 tail_copied = 1;
3956 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3959 /* Test for intersecting intervals. This does the right thing
3960 for both insertion and deletion. */
3961 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3963 prop = Foverlay_get (overlay, Qmodification_hooks);
3964 if (!NILP (prop))
3966 if (!tail_copied)
3967 tail = Fcopy_sequence (tail);
3968 tail_copied = 1;
3969 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3974 UNGCPRO;
3977 static void
3978 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3979 Lisp_Object list, overlay;
3980 int after;
3981 Lisp_Object arg1, arg2, arg3;
3983 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3985 GCPRO4 (list, arg1, arg2, arg3);
3986 if (! after)
3987 add_overlay_mod_hooklist (list, overlay);
3989 while (!NILP (list))
3991 if (NILP (arg3))
3992 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3993 else
3994 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
3995 list = Fcdr (list);
3997 UNGCPRO;
4000 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4001 property is set. */
4002 void
4003 evaporate_overlays (pos)
4004 int pos;
4006 Lisp_Object tail, overlay, hit_list;
4008 hit_list = Qnil;
4009 if (pos <= XFASTINT (current_buffer->overlay_center))
4010 for (tail = current_buffer->overlays_before; CONSP (tail);
4011 tail = XCDR (tail))
4013 int endpos;
4014 overlay = XCAR (tail);
4015 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4016 if (endpos < pos)
4017 break;
4018 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4019 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4020 hit_list = Fcons (overlay, hit_list);
4022 else
4023 for (tail = current_buffer->overlays_after; CONSP (tail);
4024 tail = XCDR (tail))
4026 int startpos;
4027 overlay = XCAR (tail);
4028 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4029 if (startpos > pos)
4030 break;
4031 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4032 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4033 hit_list = Fcons (overlay, hit_list);
4035 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4036 Fdelete_overlay (XCAR (hit_list));
4039 /* Somebody has tried to store a value with an unacceptable type
4040 in the slot with offset OFFSET. */
4042 void
4043 buffer_slot_type_mismatch (offset)
4044 int offset;
4046 Lisp_Object sym;
4047 char *type_name;
4049 switch (XINT (PER_BUFFER_TYPE (offset)))
4051 case Lisp_Int:
4052 type_name = "integers";
4053 break;
4055 case Lisp_String:
4056 type_name = "strings";
4057 break;
4059 case Lisp_Symbol:
4060 type_name = "symbols";
4061 break;
4063 default:
4064 abort ();
4067 sym = PER_BUFFER_SYMBOL (offset);
4068 error ("Only %s should be stored in the buffer-local variable %s",
4069 type_name, XSYMBOL (sym)->name->data);
4073 /***********************************************************************
4074 Allocation with mmap
4075 ***********************************************************************/
4077 #ifdef USE_MMAP_FOR_BUFFERS
4079 #include <sys/types.h>
4080 #include <sys/mman.h>
4082 #ifndef MAP_ANON
4083 #ifdef MAP_ANONYMOUS
4084 #define MAP_ANON MAP_ANONYMOUS
4085 #else
4086 #define MAP_ANON 0
4087 #endif
4088 #endif
4090 #ifndef MAP_FAILED
4091 #define MAP_FAILED ((void *) -1)
4092 #endif
4094 #include <stdio.h>
4095 #include <errno.h>
4097 #if MAP_ANON == 0
4098 #include <fcntl.h>
4099 #endif
4101 #include "coding.h"
4104 /* Memory is allocated in regions which are mapped using mmap(2).
4105 The current implementation lets the system select mapped
4106 addresses; we're not using MAP_FIXED in general, except when
4107 trying to enlarge regions.
4109 Each mapped region starts with a mmap_region structure, the user
4110 area starts after that structure, aligned to MEM_ALIGN.
4112 +-----------------------+
4113 | struct mmap_info + |
4114 | padding |
4115 +-----------------------+
4116 | user data |
4119 +-----------------------+ */
4121 struct mmap_region
4123 /* User-specified size. */
4124 size_t nbytes_specified;
4126 /* Number of bytes mapped */
4127 size_t nbytes_mapped;
4129 /* Pointer to the location holding the address of the memory
4130 allocated with the mmap'd block. The variable actually points
4131 after this structure. */
4132 POINTER_TYPE **var;
4134 /* Next and previous in list of all mmap'd regions. */
4135 struct mmap_region *next, *prev;
4138 /* Doubly-linked list of mmap'd regions. */
4140 static struct mmap_region *mmap_regions;
4142 /* File descriptor for mmap. If we don't have anonymous mapping,
4143 /dev/zero will be opened on it. */
4145 static int mmap_fd;
4147 /* Temporary storage for mmap_set_vars, see there. */
4149 static struct mmap_region *mmap_regions_1;
4150 static int mmap_fd_1;
4152 /* Page size on this system. */
4154 static int mmap_page_size;
4156 /* 1 means mmap has been intialized. */
4158 static int mmap_initialized_p;
4160 /* Value is X rounded up to the next multiple of N. */
4162 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4164 /* Size of mmap_region structure plus padding. */
4166 #define MMAP_REGION_STRUCT_SIZE \
4167 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4169 /* Given a pointer P to the start of the user-visible part of a mapped
4170 region, return a pointer to the start of the region. */
4172 #define MMAP_REGION(P) \
4173 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4175 /* Given a pointer P to the start of a mapped region, return a pointer
4176 to the start of the user-visible part of the region. */
4178 #define MMAP_USER_AREA(P) \
4179 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4181 #define MEM_ALIGN sizeof (double)
4183 /* Function prototypes. */
4185 static int mmap_free_1 P_ ((struct mmap_region *));
4186 static int mmap_enlarge P_ ((struct mmap_region *, int));
4187 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4188 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4189 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4190 static void mmap_free P_ ((POINTER_TYPE **ptr));
4191 static void mmap_init P_ ((void));
4194 /* Return a region overlapping address range START...END, or null if
4195 none. END is not including, i.e. the last byte in the range
4196 is at END - 1. */
4198 static struct mmap_region *
4199 mmap_find (start, end)
4200 POINTER_TYPE *start, *end;
4202 struct mmap_region *r;
4203 char *s = (char *) start, *e = (char *) end;
4205 for (r = mmap_regions; r; r = r->next)
4207 char *rstart = (char *) r;
4208 char *rend = rstart + r->nbytes_mapped;
4210 if (/* First byte of range, i.e. START, in this region? */
4211 (s >= rstart && s < rend)
4212 /* Last byte of range, i.e. END - 1, in this region? */
4213 || (e > rstart && e <= rend)
4214 /* First byte of this region in the range? */
4215 || (rstart >= s && rstart < e)
4216 /* Last byte of this region in the range? */
4217 || (rend > s && rend <= e))
4218 break;
4221 return r;
4225 /* Unmap a region. P is a pointer to the start of the user-araa of
4226 the region. Value is non-zero if successful. */
4228 static int
4229 mmap_free_1 (r)
4230 struct mmap_region *r;
4232 if (r->next)
4233 r->next->prev = r->prev;
4234 if (r->prev)
4235 r->prev->next = r->next;
4236 else
4237 mmap_regions = r->next;
4239 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4241 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4242 return 0;
4245 return 1;
4249 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4250 Value is non-zero if successful. */
4252 static int
4253 mmap_enlarge (r, npages)
4254 struct mmap_region *r;
4255 int npages;
4257 char *region_end = (char *) r + r->nbytes_mapped;
4258 size_t nbytes;
4259 int success = 0;
4261 if (npages < 0)
4263 /* Unmap pages at the end of the region. */
4264 nbytes = - npages * mmap_page_size;
4265 if (munmap (region_end - nbytes, nbytes) == -1)
4266 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4267 else
4269 r->nbytes_mapped -= nbytes;
4270 success = 1;
4273 else if (npages > 0)
4275 struct mmap_region *r2;
4277 nbytes = npages * mmap_page_size;
4279 /* Try to map additional pages at the end of the region. We
4280 cannot do this if the address range is already occupied by
4281 something else because mmap deletes any previous mapping.
4282 I'm not sure this is worth doing, let's see. */
4283 r2 = mmap_find (region_end, region_end + nbytes);
4284 if (r2 == NULL)
4286 POINTER_TYPE *p;
4288 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4289 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4290 if (p == MAP_FAILED)
4291 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4292 else if (p != (POINTER_TYPE *) region_end)
4294 /* Kernels are free to choose a different address. In
4295 that case, unmap what we've mapped above; we have
4296 no use for it. */
4297 if (munmap (p, nbytes) == -1)
4298 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4300 else
4302 r->nbytes_mapped += nbytes;
4303 success = 1;
4308 return success;
4312 /* Set or reset variables holding references to mapped regions. If
4313 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4314 non-zero, set all variables to the start of the user-areas
4315 of mapped regions.
4317 This function is called from Fdump_emacs to ensure that the dumped
4318 Emacs doesn't contain references to memory that won't be mapped
4319 when Emacs starts. */
4321 void
4322 mmap_set_vars (restore_p)
4323 int restore_p;
4325 struct mmap_region *r;
4327 if (restore_p)
4329 mmap_regions = mmap_regions_1;
4330 mmap_fd = mmap_fd_1;
4331 for (r = mmap_regions; r; r = r->next)
4332 *r->var = MMAP_USER_AREA (r);
4334 else
4336 for (r = mmap_regions; r; r = r->next)
4337 *r->var = NULL;
4338 mmap_regions_1 = mmap_regions;
4339 mmap_regions = NULL;
4340 mmap_fd_1 = mmap_fd;
4341 mmap_fd = -1;
4346 /* Allocate a block of storage large enough to hold NBYTES bytes of
4347 data. A pointer to the data is returned in *VAR. VAR is thus the
4348 address of some variable which will use the data area.
4350 The allocation of 0 bytes is valid.
4352 If we can't allocate the necessary memory, set *VAR to null, and
4353 return null. */
4355 static POINTER_TYPE *
4356 mmap_alloc (var, nbytes)
4357 POINTER_TYPE **var;
4358 size_t nbytes;
4360 void *p;
4361 size_t map;
4363 mmap_init ();
4365 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4366 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4367 mmap_fd, 0);
4369 if (p == MAP_FAILED)
4371 if (errno != ENOMEM)
4372 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4373 p = NULL;
4375 else
4377 struct mmap_region *r = (struct mmap_region *) p;
4379 r->nbytes_specified = nbytes;
4380 r->nbytes_mapped = map;
4381 r->var = var;
4382 r->prev = NULL;
4383 r->next = mmap_regions;
4384 if (r->next)
4385 r->next->prev = r;
4386 mmap_regions = r;
4388 p = MMAP_USER_AREA (p);
4391 return *var = p;
4395 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4396 resize it to size NBYTES. Change *VAR to reflect the new block,
4397 and return this value. If more memory cannot be allocated, then
4398 leave *VAR unchanged, and return null. */
4400 static POINTER_TYPE *
4401 mmap_realloc (var, nbytes)
4402 POINTER_TYPE **var;
4403 size_t nbytes;
4405 POINTER_TYPE *result;
4407 mmap_init ();
4409 if (*var == NULL)
4410 result = mmap_alloc (var, nbytes);
4411 else if (nbytes == 0)
4413 mmap_free (var);
4414 result = mmap_alloc (var, nbytes);
4416 else
4418 struct mmap_region *r = MMAP_REGION (*var);
4419 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4421 if (room < nbytes)
4423 /* Must enlarge. */
4424 POINTER_TYPE *old_ptr = *var;
4426 /* Try to map additional pages at the end of the region.
4427 If that fails, allocate a new region, copy data
4428 from the old region, then free it. */
4429 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4430 / mmap_page_size)))
4432 r->nbytes_specified = nbytes;
4433 *var = result = old_ptr;
4435 else if (mmap_alloc (var, nbytes))
4437 bcopy (old_ptr, *var, r->nbytes_specified);
4438 mmap_free_1 (MMAP_REGION (old_ptr));
4439 result = *var;
4440 r = MMAP_REGION (result);
4441 r->nbytes_specified = nbytes;
4443 else
4445 *var = old_ptr;
4446 result = NULL;
4449 else if (room - nbytes >= mmap_page_size)
4451 /* Shrinking by at least a page. Let's give some
4452 memory back to the system. */
4453 mmap_enlarge (r, - (room - nbytes) / mmap_page_size);
4454 result = *var;
4455 r->nbytes_specified = nbytes;
4457 else
4459 /* Leave it alone. */
4460 result = *var;
4461 r->nbytes_specified = nbytes;
4465 return result;
4469 /* Free a block of relocatable storage whose data is pointed to by
4470 PTR. Store 0 in *PTR to show there's no block allocated. */
4472 static void
4473 mmap_free (var)
4474 POINTER_TYPE **var;
4476 mmap_init ();
4478 if (*var)
4480 mmap_free_1 (MMAP_REGION (*var));
4481 *var = NULL;
4486 /* Perform necessary intializations for the use of mmap. */
4488 static void
4489 mmap_init ()
4491 #if MAP_ANON == 0
4492 /* The value of mmap_fd is initially 0 in temacs, and -1
4493 in a dumped Emacs. */
4494 if (mmap_fd <= 0)
4496 /* No anonymous mmap -- we need the file descriptor. */
4497 mmap_fd = open ("/dev/zero", O_RDONLY);
4498 if (mmap_fd == -1)
4499 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4501 #endif /* MAP_ANON == 0 */
4503 if (mmap_initialized_p)
4504 return;
4505 mmap_initialized_p = 1;
4507 #if MAP_ANON != 0
4508 mmap_fd = -1;
4509 #endif
4511 mmap_page_size = getpagesize ();
4514 #endif /* USE_MMAP_FOR_BUFFERS */
4518 /***********************************************************************
4519 Buffer-text Allocation
4520 ***********************************************************************/
4522 #ifdef REL_ALLOC
4523 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4524 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4525 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4526 #endif /* REL_ALLOC */
4529 /* Allocate NBYTES bytes for buffer B's text buffer. */
4531 static void
4532 alloc_buffer_text (b, nbytes)
4533 struct buffer *b;
4534 size_t nbytes;
4536 POINTER_TYPE *p;
4538 BLOCK_INPUT;
4539 #if defined USE_MMAP_FOR_BUFFERS
4540 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4541 #elif defined REL_ALLOC
4542 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4543 #else
4544 p = xmalloc (nbytes);
4545 #endif
4547 if (p == NULL)
4549 UNBLOCK_INPUT;
4550 memory_full ();
4553 b->text->beg = (unsigned char *) p;
4554 UNBLOCK_INPUT;
4557 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4558 shrink it. */
4560 void
4561 enlarge_buffer_text (b, delta)
4562 struct buffer *b;
4563 int delta;
4565 POINTER_TYPE *p;
4566 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4567 + delta);
4568 BLOCK_INPUT;
4569 #if defined USE_MMAP_FOR_BUFFERS
4570 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4571 #elif defined REL_ALLOC
4572 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4573 #else
4574 p = xrealloc (b->text->beg, nbytes);
4575 #endif
4577 if (p == NULL)
4579 UNBLOCK_INPUT;
4580 memory_full ();
4583 BUF_BEG_ADDR (b) = (unsigned char *) p;
4584 UNBLOCK_INPUT;
4588 /* Free buffer B's text buffer. */
4590 static void
4591 free_buffer_text (b)
4592 struct buffer *b;
4594 BLOCK_INPUT;
4596 #if defined USE_MMAP_FOR_BUFFERS
4597 mmap_free ((POINTER_TYPE **) &b->text->beg);
4598 #elif defined REL_ALLOC
4599 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4600 #else
4601 xfree (b->text->beg);
4602 #endif
4604 BUF_BEG_ADDR (b) = NULL;
4605 UNBLOCK_INPUT;
4610 /***********************************************************************
4611 Initialization
4612 ***********************************************************************/
4614 void
4615 init_buffer_once ()
4617 int idx;
4619 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
4621 /* Make sure all markable slots in buffer_defaults
4622 are initialized reasonably, so mark_buffer won't choke. */
4623 reset_buffer (&buffer_defaults);
4624 reset_buffer_local_variables (&buffer_defaults, 1);
4625 reset_buffer (&buffer_local_symbols);
4626 reset_buffer_local_variables (&buffer_local_symbols, 1);
4627 /* Prevent GC from getting confused. */
4628 buffer_defaults.text = &buffer_defaults.own_text;
4629 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4630 BUF_INTERVALS (&buffer_defaults) = 0;
4631 BUF_INTERVALS (&buffer_local_symbols) = 0;
4632 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4633 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4635 /* Set up the default values of various buffer slots. */
4636 /* Must do these before making the first buffer! */
4638 /* real setup is done in bindings.el */
4639 buffer_defaults.mode_line_format = build_string ("%-");
4640 buffer_defaults.header_line_format = Qnil;
4641 buffer_defaults.abbrev_mode = Qnil;
4642 buffer_defaults.overwrite_mode = Qnil;
4643 buffer_defaults.case_fold_search = Qt;
4644 buffer_defaults.auto_fill_function = Qnil;
4645 buffer_defaults.selective_display = Qnil;
4646 #ifndef old
4647 buffer_defaults.selective_display_ellipses = Qt;
4648 #endif
4649 buffer_defaults.abbrev_table = Qnil;
4650 buffer_defaults.display_table = Qnil;
4651 buffer_defaults.undo_list = Qnil;
4652 buffer_defaults.mark_active = Qnil;
4653 buffer_defaults.file_format = Qnil;
4654 buffer_defaults.overlays_before = Qnil;
4655 buffer_defaults.overlays_after = Qnil;
4656 XSETFASTINT (buffer_defaults.overlay_center, BEG);
4658 XSETFASTINT (buffer_defaults.tab_width, 8);
4659 buffer_defaults.truncate_lines = Qnil;
4660 buffer_defaults.ctl_arrow = Qt;
4661 buffer_defaults.direction_reversed = Qnil;
4662 buffer_defaults.cursor_type = Qt;
4663 buffer_defaults.extra_line_spacing = Qnil;
4665 #ifdef DOS_NT
4666 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
4667 #endif
4668 buffer_defaults.enable_multibyte_characters = Qt;
4669 buffer_defaults.buffer_file_coding_system = Qnil;
4670 XSETFASTINT (buffer_defaults.fill_column, 70);
4671 XSETFASTINT (buffer_defaults.left_margin, 0);
4672 buffer_defaults.cache_long_line_scans = Qnil;
4673 buffer_defaults.file_truename = Qnil;
4674 XSETFASTINT (buffer_defaults.display_count, 0);
4675 buffer_defaults.indicate_empty_lines = Qnil;
4676 buffer_defaults.scroll_up_aggressively = Qnil;
4677 buffer_defaults.scroll_down_aggressively = Qnil;
4678 buffer_defaults.display_time = Qnil;
4680 /* Assign the local-flags to the slots that have default values.
4681 The local flag is a bit that is used in the buffer
4682 to say that it has its own local value for the slot.
4683 The local flag bits are in the local_var_flags slot of the buffer. */
4685 /* Nothing can work if this isn't true */
4686 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
4688 /* 0 means not a lisp var, -1 means always local, else mask */
4689 bzero (&buffer_local_flags, sizeof buffer_local_flags);
4690 XSETINT (buffer_local_flags.filename, -1);
4691 XSETINT (buffer_local_flags.directory, -1);
4692 XSETINT (buffer_local_flags.backed_up, -1);
4693 XSETINT (buffer_local_flags.save_length, -1);
4694 XSETINT (buffer_local_flags.auto_save_file_name, -1);
4695 XSETINT (buffer_local_flags.read_only, -1);
4696 XSETINT (buffer_local_flags.major_mode, -1);
4697 XSETINT (buffer_local_flags.mode_name, -1);
4698 XSETINT (buffer_local_flags.undo_list, -1);
4699 XSETINT (buffer_local_flags.mark_active, -1);
4700 XSETINT (buffer_local_flags.point_before_scroll, -1);
4701 XSETINT (buffer_local_flags.file_truename, -1);
4702 XSETINT (buffer_local_flags.invisibility_spec, -1);
4703 XSETINT (buffer_local_flags.file_format, -1);
4704 XSETINT (buffer_local_flags.display_count, -1);
4705 XSETINT (buffer_local_flags.display_time, -1);
4706 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
4708 idx = 1;
4709 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
4710 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
4711 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
4712 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
4713 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
4714 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
4715 #ifndef old
4716 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
4717 #endif
4718 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
4719 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
4720 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
4721 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
4722 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
4723 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
4724 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
4725 #ifdef DOS_NT
4726 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
4727 /* Make this one a permanent local. */
4728 buffer_permanent_local_flags[idx++] = 1;
4729 #endif
4730 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
4731 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
4732 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
4733 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
4734 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
4735 /* Make this one a permanent local. */
4736 buffer_permanent_local_flags[idx++] = 1;
4737 XSETFASTINT (buffer_local_flags.left_margin_width, idx); ++idx;
4738 XSETFASTINT (buffer_local_flags.right_margin_width, idx); ++idx;
4739 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
4740 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
4741 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
4742 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
4743 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
4744 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
4746 /* Need more room? */
4747 if (idx >= MAX_PER_BUFFER_VARS)
4748 abort ();
4749 last_per_buffer_idx = idx;
4751 Vbuffer_alist = Qnil;
4752 current_buffer = 0;
4753 all_buffers = 0;
4755 QSFundamental = build_string ("Fundamental");
4757 Qfundamental_mode = intern ("fundamental-mode");
4758 buffer_defaults.major_mode = Qfundamental_mode;
4760 Qmode_class = intern ("mode-class");
4762 Qprotected_field = intern ("protected-field");
4764 Qpermanent_local = intern ("permanent-local");
4766 Qkill_buffer_hook = intern ("kill-buffer-hook");
4768 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
4770 /* super-magic invisible buffer */
4771 Vbuffer_alist = Qnil;
4773 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4775 inhibit_modification_hooks = 0;
4778 void
4779 init_buffer ()
4781 char buf[MAXPATHLEN + 1];
4782 char *pwd;
4783 struct stat dotstat, pwdstat;
4784 Lisp_Object temp;
4785 int rc;
4787 #ifdef USE_MMAP_FOR_BUFFERS
4789 /* When using the ralloc implementation based on mmap(2), buffer
4790 text pointers will have been set to null in the dumped Emacs.
4791 Map new memory. */
4792 struct buffer *b;
4794 for (b = all_buffers; b; b = b->next)
4795 if (b->text->beg == NULL)
4796 enlarge_buffer_text (b, 0);
4798 #endif /* USE_MMAP_FOR_BUFFERS */
4800 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4801 if (NILP (buffer_defaults.enable_multibyte_characters))
4802 Fset_buffer_multibyte (Qnil);
4804 /* If PWD is accurate, use it instead of calling getwd. This is faster
4805 when PWD is right, and may avoid a fatal error. */
4806 if ((pwd = getenv ("PWD")) != 0
4807 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
4808 && stat (pwd, &pwdstat) == 0
4809 && stat (".", &dotstat) == 0
4810 && dotstat.st_ino == pwdstat.st_ino
4811 && dotstat.st_dev == pwdstat.st_dev
4812 && strlen (pwd) < MAXPATHLEN)
4813 strcpy (buf, pwd);
4814 #ifdef HAVE_GETCWD
4815 else if (getcwd (buf, MAXPATHLEN+1) == 0)
4816 fatal ("`getcwd' failed: %s\n", strerror (errno));
4817 #else
4818 else if (getwd (buf) == 0)
4819 fatal ("`getwd' failed: %s\n", buf);
4820 #endif
4822 #ifndef VMS
4823 /* Maybe this should really use some standard subroutine
4824 whose definition is filename syntax dependent. */
4825 rc = strlen (buf);
4826 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
4828 buf[rc] = DIRECTORY_SEP;
4829 buf[rc + 1] = '\0';
4831 #endif /* not VMS */
4833 current_buffer->directory = build_string (buf);
4835 /* Add /: to the front of the name
4836 if it would otherwise be treated as magic. */
4837 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
4838 if (! NILP (temp)
4839 /* If the default dir is just /, TEMP is non-nil
4840 because of the ange-ftp completion handler.
4841 However, it is not necessary to turn / into /:/.
4842 So avoid doing that. */
4843 && strcmp ("/", XSTRING (current_buffer->directory)->data))
4844 current_buffer->directory
4845 = concat2 (build_string ("/:"), current_buffer->directory);
4847 temp = get_minibuffer (0);
4848 XBUFFER (temp)->directory = current_buffer->directory;
4851 /* initialize the buffer routines */
4852 void
4853 syms_of_buffer ()
4855 staticpro (&last_overlay_modification_hooks);
4856 last_overlay_modification_hooks
4857 = Fmake_vector (make_number (10), Qnil);
4859 staticpro (&Vbuffer_defaults);
4860 staticpro (&Vbuffer_local_symbols);
4861 staticpro (&Qfundamental_mode);
4862 staticpro (&Qmode_class);
4863 staticpro (&QSFundamental);
4864 staticpro (&Vbuffer_alist);
4865 staticpro (&Qprotected_field);
4866 staticpro (&Qpermanent_local);
4867 staticpro (&Qkill_buffer_hook);
4868 Qoverlayp = intern ("overlayp");
4869 staticpro (&Qoverlayp);
4870 Qevaporate = intern ("evaporate");
4871 staticpro (&Qevaporate);
4872 Qmodification_hooks = intern ("modification-hooks");
4873 staticpro (&Qmodification_hooks);
4874 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
4875 staticpro (&Qinsert_in_front_hooks);
4876 Qinsert_behind_hooks = intern ("insert-behind-hooks");
4877 staticpro (&Qinsert_behind_hooks);
4878 Qget_file_buffer = intern ("get-file-buffer");
4879 staticpro (&Qget_file_buffer);
4880 Qpriority = intern ("priority");
4881 staticpro (&Qpriority);
4882 Qwindow = intern ("window");
4883 staticpro (&Qwindow);
4884 Qbefore_string = intern ("before-string");
4885 staticpro (&Qbefore_string);
4886 Qafter_string = intern ("after-string");
4887 staticpro (&Qafter_string);
4888 Qfirst_change_hook = intern ("first-change-hook");
4889 staticpro (&Qfirst_change_hook);
4890 Qbefore_change_functions = intern ("before-change-functions");
4891 staticpro (&Qbefore_change_functions);
4892 Qafter_change_functions = intern ("after-change-functions");
4893 staticpro (&Qafter_change_functions);
4895 Fput (Qprotected_field, Qerror_conditions,
4896 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
4897 Fput (Qprotected_field, Qerror_message,
4898 build_string ("Attempt to modify a protected field"));
4900 /* All these use DEFVAR_LISP_NOPRO because the slots in
4901 buffer_defaults will all be marked via Vbuffer_defaults. */
4903 DEFVAR_LISP_NOPRO ("default-mode-line-format",
4904 &buffer_defaults.mode_line_format,
4905 "Default value of `mode-line-format' for buffers that don't override it.\n\
4906 This is the same as (default-value 'mode-line-format).");
4908 DEFVAR_LISP_NOPRO ("default-header-line-format",
4909 &buffer_defaults.header_line_format,
4910 "Default value of `header-line-format' for buffers that don't override it.\n\
4911 This is the same as (default-value 'header-line-format).");
4913 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
4914 "Default value of `cursor-type' for buffers that don't override it.\n\
4915 This is the same as (default-value 'cursor-type).");
4917 DEFVAR_LISP_NOPRO ("default-line-spacing",
4918 &buffer_defaults.extra_line_spacing,
4919 "Default value of `line-spacing' for buffers that don't override it.\n\
4920 This is the same as (default-value 'line-spacing).");
4922 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
4923 &buffer_defaults.abbrev_mode,
4924 "Default value of `abbrev-mode' for buffers that do not override it.\n\
4925 This is the same as (default-value 'abbrev-mode).");
4927 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
4928 &buffer_defaults.ctl_arrow,
4929 "Default value of `ctl-arrow' for buffers that do not override it.\n\
4930 This is the same as (default-value 'ctl-arrow).");
4932 DEFVAR_LISP_NOPRO ("default-direction-reversed",
4933 &buffer_defaults.direction_reversed,
4934 "Default value of `direction_reversed' for buffers that do not override it.\n\
4935 This is the same as (default-value 'direction-reversed).");
4937 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
4938 &buffer_defaults.enable_multibyte_characters,
4939 "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
4940 This is the same as (default-value 'enable-multibyte-characters).");
4942 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
4943 &buffer_defaults.buffer_file_coding_system,
4944 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
4945 This is the same as (default-value 'buffer-file-coding-system).");
4947 DEFVAR_LISP_NOPRO ("default-truncate-lines",
4948 &buffer_defaults.truncate_lines,
4949 "Default value of `truncate-lines' for buffers that do not override it.\n\
4950 This is the same as (default-value 'truncate-lines).");
4952 DEFVAR_LISP_NOPRO ("default-fill-column",
4953 &buffer_defaults.fill_column,
4954 "Default value of `fill-column' for buffers that do not override it.\n\
4955 This is the same as (default-value 'fill-column).");
4957 DEFVAR_LISP_NOPRO ("default-left-margin",
4958 &buffer_defaults.left_margin,
4959 "Default value of `left-margin' for buffers that do not override it.\n\
4960 This is the same as (default-value 'left-margin).");
4962 DEFVAR_LISP_NOPRO ("default-tab-width",
4963 &buffer_defaults.tab_width,
4964 "Default value of `tab-width' for buffers that do not override it.\n\
4965 This is the same as (default-value 'tab-width).");
4967 DEFVAR_LISP_NOPRO ("default-case-fold-search",
4968 &buffer_defaults.case_fold_search,
4969 "Default value of `case-fold-search' for buffers that don't override it.\n\
4970 This is the same as (default-value 'case-fold-search).");
4972 #ifdef DOS_NT
4973 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
4974 &buffer_defaults.buffer_file_type,
4975 "Default file type for buffers that do not override it.\n\
4976 This is the same as (default-value 'buffer-file-type).\n\
4977 The file type is nil for text, t for binary.");
4978 #endif
4980 DEFVAR_LISP_NOPRO ("default-left-margin-width",
4981 &buffer_defaults.left_margin_width,
4982 "Default value of `left-margin-width' for buffers that don't override it.\n\
4983 This is the same as (default-value 'left-margin-width).");
4985 DEFVAR_LISP_NOPRO ("default-right-margin-width",
4986 &buffer_defaults.right_margin_width,
4987 "Default value of `right_margin_width' for buffers that don't override it.\n\
4988 This is the same as (default-value 'right-margin-width).");
4990 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
4991 &buffer_defaults.indicate_empty_lines,
4992 "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
4993 This is the same as (default-value 'indicate-empty-lines).");
4995 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
4996 &buffer_defaults.scroll_up_aggressively,
4997 "Default value of `scroll-up-aggressively' for buffers that\n\
4998 don't override it. This is the same as (default-value\n\
4999 'scroll-up-aggressively).");
5001 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5002 &buffer_defaults.scroll_down_aggressively,
5003 "Default value of `scroll-down-aggressively' for buffers that\n\
5004 don't override it. This is the same as (default-value\n\
5005 'scroll-down-aggressively).");
5007 DEFVAR_PER_BUFFER ("header-line-format",
5008 &current_buffer->header_line_format,
5009 Qnil,
5010 "Analogous to `mode-line-format', but for a mode line displayed\n\
5011 at the top of windows.");
5013 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5014 Qnil, 0);
5016 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
5017 But make-docfile finds it!
5018 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5019 Qnil,
5020 "Template for displaying mode line for current buffer.\n\
5021 Each buffer has its own value of this variable.\n\
5022 Value may be nil, a string, a symbol or a list or cons cell.\n\
5023 A value of nil means don't display a mode line.\n\
5024 For a symbol, its value is used (but it is ignored if t or nil).\n\
5025 A string appearing directly as the value of a symbol is processed verbatim\n\
5026 in that the %-constructs below are not recognized.\n\
5027 For a list of the form `(:eval FORM)', FORM is evaluated and the result\n\
5028 is used as a mode line element.\n\
5029 For a list whose car is a symbol, the symbol's value is taken,\n\
5030 and if that is non-nil, the cadr of the list is processed recursively.\n\
5031 Otherwise, the caddr of the list (if there is one) is processed.\n\
5032 For a list whose car is a string or list, each element is processed\n\
5033 recursively and the results are effectively concatenated.\n\
5034 For a list whose car is an integer, the cdr of the list is processed\n\
5035 and padded (if the number is positive) or truncated (if negative)\n\
5036 to the width specified by that number.\n\
5037 A string is printed verbatim in the mode line except for %-constructs:\n\
5038 (%-constructs are allowed when the string is the entire mode-line-format\n\
5039 or when it is found in a cons-cell or a list)\n\
5040 %b -- print buffer name. %f -- print visited file name.\n\
5041 %F -- print frame name.\n\
5042 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
5043 %& is like %*, but ignore read-only-ness.\n\
5044 % means buffer is read-only and * means it is modified.\n\
5045 For a modified read-only buffer, %* gives % and %+ gives *.\n\
5046 %s -- print process status. %l -- print the current line number.\n\
5047 %c -- print the current column number (this makes editing slower).\n\
5048 To make the column number update correctly in all cases,\n\
5049 `column-number-mode' must be non-nil.\n\
5050 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
5051 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
5052 or print Bottom or All.\n\
5053 %m -- print the mode name.\n\
5054 %n -- print Narrow if appropriate.\n\
5055 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.\n\
5056 %Z -- like %z, but including the end-of-line format.\n\
5057 %[ -- print one [ for each recursive editing level. %] similar.\n\
5058 %% -- print %. %- -- print infinitely many dashes.\n\
5059 Decimal digits after the % specify field width to which to pad.");
5062 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5063 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
5064 nil here means use current buffer's major mode.");
5066 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5067 make_number (Lisp_Symbol),
5068 "Symbol for current buffer's major mode.");
5070 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5071 make_number (Lisp_String),
5072 "Pretty name of current buffer's major mode (a string).");
5074 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5075 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
5076 Automatically becomes buffer-local when set in any fashion.");
5078 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5079 Qnil,
5080 "*Non-nil if searches and matches should ignore case.\n\
5081 Automatically becomes buffer-local when set in any fashion.");
5083 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5084 make_number (Lisp_Int),
5085 "*Column beyond which automatic line-wrapping should happen.\n\
5086 Automatically becomes buffer-local when set in any fashion.");
5088 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5089 make_number (Lisp_Int),
5090 "*Column for the default indent-line-function to indent to.\n\
5091 Linefeed indents to this column in Fundamental mode.\n\
5092 Automatically becomes buffer-local when set in any fashion.");
5094 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5095 make_number (Lisp_Int),
5096 "*Distance between tab stops (for display of tab characters), in columns.\n\
5097 Automatically becomes buffer-local when set in any fashion.");
5099 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5100 "*Non-nil means display control chars with uparrow.\n\
5101 A value of nil means use backslash and octal digits.\n\
5102 Automatically becomes buffer-local when set in any fashion.\n\
5103 This variable does not apply to characters whose display is specified\n\
5104 in the current display table (if there is one).");
5106 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5107 &current_buffer->enable_multibyte_characters,
5108 make_number (-1),
5109 "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
5110 Otherwise they are regarded as unibyte. This affects the display,\n\
5111 file I/O and the behavior of various editing commands.\n\
5113 This variable is buffer-local but you cannot set it directly;\n\
5114 use the function `set-buffer-multibyte' to change a buffer's representation.\n\
5115 Changing its default value with `setq-default' is supported.\n\
5116 See also variable `default-enable-multibyte-characters' and Info node\n\
5117 `(elisp)Text Representations'.");
5119 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5120 &current_buffer->buffer_file_coding_system, Qnil,
5121 "Coding system to be used for encoding the buffer contents on saving.\n\
5122 This variable applies to saving the buffer, and also to `write-region'\n\
5123 and other functions that use `write-region'.\n\
5124 It does not apply to sending output to subprocesses, however.\n\
5126 If this is nil, the buffer is saved without any code conversion\n\
5127 unless some coding system is specified in `file-coding-system-alist'\n\
5128 for the buffer file.\n\
5130 The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
5132 This variable is never applied to a way of decoding\n\
5133 a file while reading it.");
5135 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5136 Qnil,
5137 "*Non-nil means lines in the buffer are displayed right to left.");
5139 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5140 "*Non-nil means do not display continuation lines;\n\
5141 give each line of text one screen line.\n\
5142 Automatically becomes buffer-local when set in any fashion.\n\
5144 Note that this is overridden by the variable\n\
5145 `truncate-partial-width-windows' if that variable is non-nil\n\
5146 and this buffer is not full-frame width.");
5148 #ifdef DOS_NT
5149 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5150 Qnil,
5151 "Non-nil if the visited file is a binary file.\n\
5152 This variable is meaningful on MS-DOG and Windows NT.\n\
5153 On those systems, it is automatically local in every buffer.\n\
5154 On other systems, this variable is normally always nil.");
5155 #endif
5157 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5158 make_number (Lisp_String),
5159 "Name of default directory of current buffer. Should end with slash.\n\
5160 Each buffer has its own value of this variable.");
5162 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5163 Qnil,
5164 "Function called (if non-nil) to perform auto-fill.\n\
5165 It is called after self-inserting any character specified in\n\
5166 the `auto-fill-chars' table.\n\
5167 Each buffer has its own value of this variable.\n\
5168 NOTE: This variable is not a hook;\n\
5169 its value may not be a list of functions.");
5171 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5172 make_number (Lisp_String),
5173 "Name of file visited in current buffer, or nil if not visiting a file.\n\
5174 Each buffer has its own value of this variable.");
5176 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5177 make_number (Lisp_String),
5178 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
5179 The truename of a file is calculated by `file-truename'\n\
5180 and then abbreviated with `abbreviate-file-name'.\n\
5181 Each buffer has its own value of this variable.");
5183 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5184 &current_buffer->auto_save_file_name,
5185 make_number (Lisp_String),
5186 "Name of file for auto-saving current buffer,\n\
5187 or nil if buffer should not be auto-saved.\n\
5188 Each buffer has its own value of this variable.");
5190 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5191 "Non-nil if this buffer is read-only.\n\
5192 Each buffer has its own value of this variable.");
5194 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5195 "Non-nil if this buffer's file has been backed up.\n\
5196 Backing up is done before the first time the file is saved.\n\
5197 Each buffer has its own value of this variable.");
5199 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5200 make_number (Lisp_Int),
5201 "Length of current buffer when last read in, saved or auto-saved.\n\
5202 0 initially.\n\
5203 Each buffer has its own value of this variable.");
5205 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5206 Qnil,
5207 "Non-nil enables selective display:\n\
5208 Integer N as value means display only lines\n\
5209 that start with less than n columns of space.\n\
5210 A value of t means, after a ^M, all the rest of the line is invisible.\n\
5211 Then ^M's in the file are written into files as newlines.\n\n\
5212 Automatically becomes buffer-local when set in any fashion.");
5214 #ifndef old
5215 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5216 &current_buffer->selective_display_ellipses,
5217 Qnil,
5218 "t means display ... on previous line when a line is invisible.\n\
5219 Automatically becomes buffer-local when set in any fashion.");
5220 #endif
5222 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5223 "Non-nil if self-insertion should replace existing text.\n\
5224 The value should be one of `overwrite-mode-textual',\n\
5225 `overwrite-mode-binary', or nil.\n\
5226 If it is `overwrite-mode-textual', self-insertion still\n\
5227 inserts at the end of a line, and inserts when point is before a tab,\n\
5228 until the tab is filled in.\n\
5229 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
5230 Automatically becomes buffer-local when set in any fashion.");
5232 #if 0 /* The doc string is too long for some compilers,
5233 but make-docfile can find it in this comment. */
5234 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5235 Qnil,
5236 "Display table that controls display of the contents of current buffer.\n\
5237 Automatically becomes buffer-local when set in any fashion.\n\
5238 The display table is a char-table created with `make-display-table'.\n\
5239 The ordinary char-table elements control how to display each possible text\n\
5240 character. Each value should be a vector of characters or nil;\n\
5241 nil means display the character in the default fashion.\n\
5242 There are six extra slots to control the display of\n\
5243 the end of a truncated screen line (extra-slot 0, a single character);\n\
5244 the end of a continued line (extra-slot 1, a single character);\n\
5245 the escape character used to display character codes in octal\n\
5246 (extra-slot 2, a single character);\n\
5247 the character used as an arrow for control characters (extra-slot 3,\n\
5248 a single character);\n\
5249 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
5250 a vector of characters);\n\
5251 the character used to draw the border between side-by-side windows\n\
5252 (extra-slot 5, a single character).\n\
5253 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
5254 If this variable is nil, the value of `standard-display-table' is used.\n\
5255 Each window can have its own, overriding display table.");
5256 #endif
5257 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5258 Qnil, 0);
5260 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_width,
5261 Qnil,
5262 "*Width of left marginal area for display of a buffer.\n\
5263 Automatically becomes buffer-local when set in any fashion.\n\
5264 A value of nil means no marginal area.");
5266 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_width,
5267 Qnil,
5268 "*Width of right marginal area for display of a buffer.\n\
5269 Automatically becomes buffer-local when set in any fashion.\n\
5270 A value of nil means no marginal area.");
5272 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5273 &current_buffer->indicate_empty_lines, Qnil,
5274 "*Visually indicate empty lines after the buffer end.\n\
5275 If non-nil, a bitmap is displayed in the left fringe of a window on\n\
5276 window-systems.\n\
5277 Automatically becomes buffer-local when set in any fashion.\n");
5279 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5280 &current_buffer->scroll_up_aggressively, Qnil,
5281 "*If a number, scroll display up aggressively.\n\
5282 If scrolling a window because point is above the window start, choose\n\
5283 a new window start so that point ends up that fraction of the window's\n\
5284 height from the top of the window.\n\
5285 Automatically becomes buffer-local when set in any fashion.");
5287 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5288 &current_buffer->scroll_down_aggressively, Qnil,
5289 "*If a number, scroll display down aggressively.\n\
5290 If scrolling a window because point is below the window end, choose\n\
5291 a new window start so that point ends up that fraction of the window's\n\
5292 height from the bottom of the window.\n\
5293 Automatically becomes buffer-local when set in any fashion.");
5295 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5296 "Don't ask.");
5299 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5300 "List of functions to call before each text change.\n\
5301 Two arguments are passed to each function: the positions of\n\
5302 the beginning and end of the range of old text to be changed.\n\
5303 \(For an insertion, the beginning and end are at the same place.)\n\
5304 No information is given about the length of the text after the change.\n\
5306 Buffer changes made while executing the `before-change-functions'\n\
5307 don't call any before-change or after-change functions.\n\
5308 That's because these variables are temporarily set to nil.\n\
5309 As a result, a hook function cannot straightforwardly alter the value of\n\
5310 these variables. See the Emacs Lisp manual for a way of\n\
5311 accomplishing an equivalent result by using other variables.\n\
5313 If an unhandled error happens in running these functions,\n\
5314 the variable's value remains nil. That prevents the error\n\
5315 from happening repeatedly and making Emacs nonfunctional.");
5316 Vbefore_change_functions = Qnil;
5318 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5319 "List of function to call after each text change.\n\
5320 Three arguments are passed to each function: the positions of\n\
5321 the beginning and end of the range of changed text,\n\
5322 and the length in bytes of the pre-change text replaced by that range.\n\
5323 \(For an insertion, the pre-change length is zero;\n\
5324 for a deletion, that length is the number of bytes deleted,\n\
5325 and the post-change beginning and end are at the same place.)\n\
5327 Buffer changes made while executing the `after-change-functions'\n\
5328 don't call any before-change or after-change functions.\n\
5329 That's because these variables are temporarily set to nil.\n\
5330 As a result, a hook function cannot straightforwardly alter the value of\n\
5331 these variables. See the Emacs Lisp manual for a way of\n\
5332 accomplishing an equivalent result by using other variables.\n\
5334 If an unhandled error happens in running these functions,\n\
5335 the variable's value remains nil. That prevents the error\n\
5336 from happening repeatedly and making Emacs nonfunctional.");
5337 Vafter_change_functions = Qnil;
5339 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5340 "A list of functions to call before changing a buffer which is unmodified.\n\
5341 The functions are run using the `run-hooks' function.");
5342 Vfirst_change_hook = Qnil;
5344 #if 0 /* The doc string is too long for some compilers,
5345 but make-docfile can find it in this comment. */
5346 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5347 "List of undo entries in current buffer.\n\
5348 This variable is always local in all buffers.\n\
5349 Recent changes come first; older changes follow newer.\n\
5351 An entry (BEG . END) represents an insertion which begins at\n\
5352 position BEG and ends at position END.\n\
5354 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
5355 from (abs POSITION). If POSITION is positive, point was at the front\n\
5356 of the text being deleted; if negative, point was at the end.\n\
5358 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
5359 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
5360 of the visited file's modification time, as of that time. If the\n\
5361 modification time of the most recent save is different, this entry is\n\
5362 obsolete.\n\
5364 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
5365 was modified between BEG and END. PROPERTY is the property name,\n\
5366 and VALUE is the old value.\n\
5368 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
5369 was adjusted in position by the offset DISTANCE (an integer).\n\
5371 An entry of the form POSITION indicates that point was at the buffer\n\
5372 location given by the integer. Undoing an entry of this form places\n\
5373 point at POSITION.\n\
5375 nil marks undo boundaries. The undo command treats the changes\n\
5376 between two undo boundaries as a single step to be undone.\n\
5378 If the value of the variable is t, undo information is not recorded.");
5379 #endif
5380 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5383 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5384 "Non-nil means the mark and region are currently active in this buffer.\n\
5385 Automatically local in all buffers.");
5387 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5388 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
5389 This variable is buffer-local, in all buffers.\n\
5391 Normally, the line-motion functions work by scanning the buffer for\n\
5392 newlines. Columnar operations (like move-to-column and\n\
5393 compute-motion) also work by scanning the buffer, summing character\n\
5394 widths as they go. This works well for ordinary text, but if the\n\
5395 buffer's lines are very long (say, more than 500 characters), these\n\
5396 motion functions will take longer to execute. Emacs may also take\n\
5397 longer to update the display.\n\
5399 If cache-long-line-scans is non-nil, these motion functions cache the\n\
5400 results of their scans, and consult the cache to avoid rescanning\n\
5401 regions of the buffer until the text is modified. The caches are most\n\
5402 beneficial when they prevent the most searching---that is, when the\n\
5403 buffer contains long lines and large regions of characters with the\n\
5404 same, fixed screen width.\n\
5406 When cache-long-line-scans is non-nil, processing short lines will\n\
5407 become slightly slower (because of the overhead of consulting the\n\
5408 cache), and the caches will use memory roughly proportional to the\n\
5409 number of newlines and characters whose screen width varies.\n\
5411 The caches require no explicit maintenance; their accuracy is\n\
5412 maintained internally by the Emacs primitives. Enabling or disabling\n\
5413 the cache should not affect the behavior of any of the motion\n\
5414 functions; it should only affect their performance.");
5416 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5417 "Value of point before the last series of scroll operations, or nil.\n\
5418 This variable is always local in all buffers.");
5420 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5421 "List of formats to use when saving this buffer.\n\
5422 This variable is always local in all buffers.\n\
5423 Formats are defined by `format-alist'. This variable is\n\
5424 set when a file is visited. Automatically local in all buffers.");
5426 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5427 &current_buffer->invisibility_spec, Qnil,
5428 "Invisibility spec of this buffer.\n\
5429 This variable is always local in all buffers.\n\
5430 The default is t, which means that text is invisible\n\
5431 if it has a non-nil `invisible' property.\n\
5432 If the value is a list, a text character is invisible if its `invisible'\n\
5433 property is an element in that list.\n\
5434 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
5435 then characters with property value PROP are invisible,\n\
5436 and they have an ellipsis as well if ELLIPSIS is non-nil.");
5438 DEFVAR_PER_BUFFER ("buffer-display-count",
5439 &current_buffer->display_count, Qnil,
5440 "A number incremented each time this buffer is displayed in a window.\n\
5441 This variable is always local in all buffers.\n\
5442 The function `set-window-buffer increments it.");
5444 DEFVAR_PER_BUFFER ("buffer-display-time",
5445 &current_buffer->display_time, Qnil,
5446 "Time stamp updated each time this buffer is displayed in a window.\n\
5447 This variable is always local in all buffers.\n\
5448 The function `set-window-buffer' updates this variable\n\
5449 to the value obtained by calling `current-time'.\n\
5450 If the buffer has never been shown in a window, the value is nil.");
5452 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
5453 "*Non-nil means deactivate the mark when the buffer contents change.\n\
5454 Non-nil also enables highlighting of the region whenever the mark is active.\n\
5455 The variable `highlight-nonselected-windows' controls whether to highlight\n\
5456 all windows or just the selected window.");
5457 Vtransient_mark_mode = Qnil;
5459 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
5460 "*Non-nil means disregard read-only status of buffers or characters.\n\
5461 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
5462 text properties. If the value is a list, disregard `buffer-read-only'\n\
5463 and disregard a `read-only' text property if the property value\n\
5464 is a member of the list.");
5465 Vinhibit_read_only = Qnil;
5467 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
5468 "Cursor to use in window displaying this buffer.\n\
5469 Values are interpreted as follows:\n\
5471 t use the cursor specified for the frame\n\
5472 nil don't display a cursor\n\
5473 `bar' display a bar cursor with default width\n\
5474 (bar . WIDTH) display a bar cursor with width WIDTH\n\
5475 others display a box cursor.");
5477 DEFVAR_PER_BUFFER ("line-spacing",
5478 &current_buffer->extra_line_spacing, Qnil,
5479 "Additional space to put between lines when displaying a buffer.\n\
5480 The space is measured in pixels, and put below lines on window systems.");
5482 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
5483 "List of functions called with no args to query before killing a buffer.");
5484 Vkill_buffer_query_functions = Qnil;
5486 defsubr (&Sbuffer_live_p);
5487 defsubr (&Sbuffer_list);
5488 defsubr (&Sget_buffer);
5489 defsubr (&Sget_file_buffer);
5490 defsubr (&Sget_buffer_create);
5491 defsubr (&Smake_indirect_buffer);
5492 defsubr (&Sgenerate_new_buffer_name);
5493 defsubr (&Sbuffer_name);
5494 /*defsubr (&Sbuffer_number);*/
5495 defsubr (&Sbuffer_file_name);
5496 defsubr (&Sbuffer_base_buffer);
5497 defsubr (&Sbuffer_local_variables);
5498 defsubr (&Sbuffer_modified_p);
5499 defsubr (&Sset_buffer_modified_p);
5500 defsubr (&Sbuffer_modified_tick);
5501 defsubr (&Srename_buffer);
5502 defsubr (&Sother_buffer);
5503 defsubr (&Sbuffer_disable_undo);
5504 defsubr (&Sbuffer_enable_undo);
5505 defsubr (&Skill_buffer);
5506 defsubr (&Sset_buffer_major_mode);
5507 defsubr (&Sswitch_to_buffer);
5508 defsubr (&Spop_to_buffer);
5509 defsubr (&Scurrent_buffer);
5510 defsubr (&Sset_buffer);
5511 defsubr (&Sbarf_if_buffer_read_only);
5512 defsubr (&Sbury_buffer);
5513 defsubr (&Serase_buffer);
5514 defsubr (&Sset_buffer_multibyte);
5515 defsubr (&Skill_all_local_variables);
5517 defsubr (&Soverlayp);
5518 defsubr (&Smake_overlay);
5519 defsubr (&Sdelete_overlay);
5520 defsubr (&Smove_overlay);
5521 defsubr (&Soverlay_start);
5522 defsubr (&Soverlay_end);
5523 defsubr (&Soverlay_buffer);
5524 defsubr (&Soverlay_properties);
5525 defsubr (&Soverlays_at);
5526 defsubr (&Soverlays_in);
5527 defsubr (&Snext_overlay_change);
5528 defsubr (&Sprevious_overlay_change);
5529 defsubr (&Soverlay_recenter);
5530 defsubr (&Soverlay_lists);
5531 defsubr (&Soverlay_get);
5532 defsubr (&Soverlay_put);
5533 defsubr (&Srestore_buffer_modified_p);
5536 void
5537 keys_of_buffer ()
5539 initial_define_key (control_x_map, 'b', "switch-to-buffer");
5540 initial_define_key (control_x_map, 'k', "kill-buffer");
5542 /* This must not be in syms_of_buffer, because Qdisabled is not
5543 initialized when that function gets called. */
5544 Fput (intern ("erase-buffer"), Qdisabled, Qt);