tarballs, not pretests.
[emacs.git] / src / buffer.c
blob96b09ed4eeebc8e1ab1ee065353cf071d08d9530
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999, 2000, 2001, 2002
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));
182 static Lisp_Object copy_overlays P_ ((struct buffer *, Lisp_Object));
183 static void modify_overlay P_ ((struct buffer *, int, int));
186 /* For debugging; temporary. See set_buffer_internal. */
187 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
189 void
190 nsberror (spec)
191 Lisp_Object spec;
193 if (STRINGP (spec))
194 error ("No buffer named %s", XSTRING (spec)->data);
195 error ("Invalid buffer argument");
198 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
199 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
200 Value is nil if OBJECT is not a buffer or if it has been killed.")
201 (object)
202 Lisp_Object object;
204 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
205 ? Qt : Qnil);
208 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
209 "Return a list of all existing live buffers.\n\
210 If the optional arg FRAME is a frame, we return that frame's buffer list.")
211 (frame)
212 Lisp_Object frame;
214 Lisp_Object framelist, general;
215 general = Fmapcar (Qcdr, Vbuffer_alist);
217 if (FRAMEP (frame))
219 Lisp_Object tail;
221 CHECK_FRAME (frame, 1);
223 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
225 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
226 tail = framelist;
227 while (! NILP (tail))
229 general = Fdelq (XCAR (tail), general);
230 tail = XCDR (tail);
232 return nconc2 (framelist, general);
235 return general;
238 /* Like Fassoc, but use Fstring_equal to compare
239 (which ignores text properties),
240 and don't ever QUIT. */
242 static Lisp_Object
243 assoc_ignore_text_properties (key, list)
244 register Lisp_Object key;
245 Lisp_Object list;
247 register Lisp_Object tail;
248 for (tail = list; !NILP (tail); tail = Fcdr (tail))
250 register Lisp_Object elt, tem;
251 elt = Fcar (tail);
252 tem = Fstring_equal (Fcar (elt), key);
253 if (!NILP (tem))
254 return elt;
256 return Qnil;
259 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
260 "Return the buffer named NAME (a string).\n\
261 If there is no live buffer named NAME, return nil.\n\
262 NAME may also be a buffer; if so, the value is that buffer.")
263 (name)
264 register Lisp_Object name;
266 if (BUFFERP (name))
267 return name;
268 CHECK_STRING (name, 0);
270 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
273 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
274 "Return the buffer visiting file FILENAME (a string).\n\
275 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
276 If there is no such live buffer, return nil.\n\
277 See also `find-buffer-visiting'.")
278 (filename)
279 register Lisp_Object filename;
281 register Lisp_Object tail, buf, tem;
282 Lisp_Object handler;
284 CHECK_STRING (filename, 0);
285 filename = Fexpand_file_name (filename, Qnil);
287 /* If the file name has special constructs in it,
288 call the corresponding file handler. */
289 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
290 if (!NILP (handler))
291 return call2 (handler, Qget_file_buffer, filename);
293 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
295 buf = Fcdr (XCAR (tail));
296 if (!BUFFERP (buf)) continue;
297 if (!STRINGP (XBUFFER (buf)->filename)) continue;
298 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
299 if (!NILP (tem))
300 return buf;
302 return Qnil;
305 Lisp_Object
306 get_truename_buffer (filename)
307 register Lisp_Object filename;
309 register Lisp_Object tail, buf, tem;
311 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
313 buf = Fcdr (XCAR (tail));
314 if (!BUFFERP (buf)) continue;
315 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
316 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
317 if (!NILP (tem))
318 return buf;
320 return Qnil;
323 /* Incremented for each buffer created, to assign the buffer number. */
324 int buffer_count;
326 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
327 "Return the buffer named NAME, or create such a buffer and return it.\n\
328 A new buffer is created if there is no live buffer named NAME.\n\
329 If NAME starts with a space, the new buffer does not keep undo information.\n\
330 If NAME is a buffer instead of a string, then it is the value returned.\n\
331 The value is never nil.")
332 (name)
333 register Lisp_Object name;
335 register Lisp_Object buf;
336 register struct buffer *b;
338 buf = Fget_buffer (name);
339 if (!NILP (buf))
340 return buf;
342 if (XSTRING (name)->size == 0)
343 error ("Empty string for buffer name is not allowed");
345 b = (struct buffer *) allocate_buffer ();
347 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
349 /* An ordinary buffer uses its own struct buffer_text. */
350 b->text = &b->own_text;
351 b->base_buffer = 0;
353 BUF_GAP_SIZE (b) = 20;
354 BLOCK_INPUT;
355 /* We allocate extra 1-byte at the tail and keep it always '\0' for
356 anchoring a search. */
357 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
358 UNBLOCK_INPUT;
359 if (! BUF_BEG_ADDR (b))
360 buffer_memory_full ();
362 BUF_PT (b) = 1;
363 BUF_GPT (b) = 1;
364 BUF_BEGV (b) = 1;
365 BUF_ZV (b) = 1;
366 BUF_Z (b) = 1;
367 BUF_PT_BYTE (b) = 1;
368 BUF_GPT_BYTE (b) = 1;
369 BUF_BEGV_BYTE (b) = 1;
370 BUF_ZV_BYTE (b) = 1;
371 BUF_Z_BYTE (b) = 1;
372 BUF_MODIFF (b) = 1;
373 BUF_OVERLAY_MODIFF (b) = 1;
374 BUF_SAVE_MODIFF (b) = 1;
375 BUF_INTERVALS (b) = 0;
376 BUF_UNCHANGED_MODIFIED (b) = 1;
377 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
378 BUF_END_UNCHANGED (b) = 0;
379 BUF_BEG_UNCHANGED (b) = 0;
380 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
382 b->newline_cache = 0;
383 b->width_run_cache = 0;
384 b->width_table = Qnil;
385 b->prevent_redisplay_optimizations_p = 1;
387 /* Put this on the chain of all buffers including killed ones. */
388 b->next = all_buffers;
389 all_buffers = b;
391 /* An ordinary buffer normally doesn't need markers
392 to handle BEGV and ZV. */
393 b->pt_marker = Qnil;
394 b->begv_marker = Qnil;
395 b->zv_marker = Qnil;
397 name = Fcopy_sequence (name);
398 XSTRING (name)->intervals = NULL_INTERVAL;
399 b->name = name;
401 if (XSTRING (name)->data[0] != ' ')
402 b->undo_list = Qnil;
403 else
404 b->undo_list = Qt;
406 reset_buffer (b);
407 reset_buffer_local_variables (b, 1);
409 /* Put this in the alist of all live buffers. */
410 XSETBUFFER (buf, b);
411 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
413 b->mark = Fmake_marker ();
414 BUF_MARKERS (b) = Qnil;
415 b->name = name;
416 return buf;
420 /* Return a list of overlays which is a copy of the overlay list
421 LIST, but for buffer B. */
423 static Lisp_Object
424 copy_overlays (b, list)
425 struct buffer *b;
426 Lisp_Object list;
428 Lisp_Object result, buffer;
430 XSETBUFFER (buffer, b);
432 for (result = Qnil; CONSP (list); list = XCDR (list))
434 Lisp_Object overlay, start, end, old_overlay;
435 int charpos;
437 old_overlay = XCAR (list);
438 charpos = marker_position (OVERLAY_START (old_overlay));
439 start = Fmake_marker ();
440 Fset_marker (start, make_number (charpos), buffer);
441 XMARKER (start)->insertion_type
442 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
444 charpos = marker_position (OVERLAY_END (old_overlay));
445 end = Fmake_marker ();
446 Fset_marker (end, make_number (charpos), buffer);
447 XMARKER (end)->insertion_type
448 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
450 overlay = allocate_misc ();
451 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
452 OVERLAY_START (overlay) = start;
453 OVERLAY_END (overlay) = end;
454 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
456 result = Fcons (overlay, result);
459 return Fnreverse (result);
463 /* Clone per-buffer values of buffer FROM.
465 Buffer TO gets the same per-buffer values as FROM, with the
466 following exceptions: (1) TO's name is left untouched, (2) markers
467 are copied and made to refer to TO, and (3) overlay lists are
468 copied. */
470 static void
471 clone_per_buffer_values (from, to)
472 struct buffer *from, *to;
474 Lisp_Object to_buffer;
475 int offset;
477 XSETBUFFER (to_buffer, to);
479 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
480 offset < sizeof *to;
481 offset += sizeof (Lisp_Object))
483 Lisp_Object obj;
485 obj = PER_BUFFER_VALUE (from, offset);
486 if (MARKERP (obj))
488 struct Lisp_Marker *m = XMARKER (obj);
489 obj = Fmake_marker ();
490 XMARKER (obj)->insertion_type = m->insertion_type;
491 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
494 PER_BUFFER_VALUE (to, offset) = obj;
497 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
499 to->overlays_before = copy_overlays (to, from->overlays_before);
500 to->overlays_after = copy_overlays (to, from->overlays_after);
504 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
505 2, 3,
506 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
507 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
508 BASE-BUFFER should be an existing buffer (or buffer name).\n\
509 NAME should be a string which is not the name of an existing buffer.\n\
510 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,\n\
511 such as major and minor modes, in the indirect buffer.\n\
512 CLONE nil means the indirect buffer's state is reset to default values.")
513 (base_buffer, name, clone)
514 Lisp_Object base_buffer, name, clone;
516 Lisp_Object buf;
517 struct buffer *b;
519 buf = Fget_buffer (name);
520 if (!NILP (buf))
521 error ("Buffer name `%s' is in use", XSTRING (name)->data);
523 base_buffer = Fget_buffer (base_buffer);
524 if (NILP (base_buffer))
525 error ("No such buffer: `%s'", XSTRING (name)->data);
527 if (XSTRING (name)->size == 0)
528 error ("Empty string for buffer name is not allowed");
530 b = (struct buffer *) allocate_buffer ();
531 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
533 if (XBUFFER (base_buffer)->base_buffer)
534 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
535 else
536 b->base_buffer = XBUFFER (base_buffer);
538 /* Use the base buffer's text object. */
539 b->text = b->base_buffer->text;
541 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
542 BUF_ZV (b) = BUF_ZV (b->base_buffer);
543 BUF_PT (b) = BUF_PT (b->base_buffer);
544 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
545 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
546 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
548 b->newline_cache = 0;
549 b->width_run_cache = 0;
550 b->width_table = Qnil;
552 /* Put this on the chain of all buffers including killed ones. */
553 b->next = all_buffers;
554 all_buffers = b;
556 name = Fcopy_sequence (name);
557 XSTRING (name)->intervals = NULL_INTERVAL;
558 b->name = name;
560 reset_buffer (b);
561 reset_buffer_local_variables (b, 1);
563 /* Put this in the alist of all live buffers. */
564 XSETBUFFER (buf, b);
565 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
567 b->mark = Fmake_marker ();
568 b->name = name;
570 /* The multibyte status belongs to the base buffer. */
571 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
573 /* Make sure the base buffer has markers for its narrowing. */
574 if (NILP (b->base_buffer->pt_marker))
576 b->base_buffer->pt_marker = Fmake_marker ();
577 set_marker_both (b->base_buffer->pt_marker, base_buffer,
578 BUF_PT (b->base_buffer),
579 BUF_PT_BYTE (b->base_buffer));
581 if (NILP (b->base_buffer->begv_marker))
583 b->base_buffer->begv_marker = Fmake_marker ();
584 set_marker_both (b->base_buffer->begv_marker, base_buffer,
585 BUF_BEGV (b->base_buffer),
586 BUF_BEGV_BYTE (b->base_buffer));
588 if (NILP (b->base_buffer->zv_marker))
590 b->base_buffer->zv_marker = Fmake_marker ();
591 set_marker_both (b->base_buffer->zv_marker, base_buffer,
592 BUF_ZV (b->base_buffer),
593 BUF_ZV_BYTE (b->base_buffer));
594 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
597 if (NILP (clone))
599 /* Give the indirect buffer markers for its narrowing. */
600 b->pt_marker = Fmake_marker ();
601 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
602 b->begv_marker = Fmake_marker ();
603 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
604 b->zv_marker = Fmake_marker ();
605 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
606 XMARKER (b->zv_marker)->insertion_type = 1;
608 else
609 clone_per_buffer_values (b->base_buffer, b);
611 return buf;
614 /* Reinitialize everything about a buffer except its name and contents
615 and local variables. */
617 void
618 reset_buffer (b)
619 register struct buffer *b;
621 b->filename = Qnil;
622 b->file_truename = Qnil;
623 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
624 b->modtime = 0;
625 XSETFASTINT (b->save_length, 0);
626 b->last_window_start = 1;
627 /* It is more conservative to start out "changed" than "unchanged". */
628 b->clip_changed = 0;
629 b->prevent_redisplay_optimizations_p = 1;
630 b->backed_up = Qnil;
631 b->auto_save_modified = 0;
632 b->auto_save_failure_time = -1;
633 b->auto_save_file_name = Qnil;
634 b->read_only = Qnil;
635 b->overlays_before = Qnil;
636 b->overlays_after = Qnil;
637 XSETFASTINT (b->overlay_center, 1);
638 b->mark_active = Qnil;
639 b->point_before_scroll = Qnil;
640 b->file_format = Qnil;
641 b->last_selected_window = Qnil;
642 XSETINT (b->display_count, 0);
643 b->display_time = Qnil;
644 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
645 b->cursor_type = buffer_defaults.cursor_type;
646 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
649 /* Reset buffer B's local variables info.
650 Don't use this on a buffer that has already been in use;
651 it does not treat permanent locals consistently.
652 Instead, use Fkill_all_local_variables.
654 If PERMANENT_TOO is 1, then we reset permanent built-in
655 buffer-local variables. If PERMANENT_TOO is 0,
656 we preserve those. */
658 static void
659 reset_buffer_local_variables (b, permanent_too)
660 register struct buffer *b;
661 int permanent_too;
663 register int offset;
664 int i;
666 /* Reset the major mode to Fundamental, together with all the
667 things that depend on the major mode.
668 default-major-mode is handled at a higher level.
669 We ignore it here. */
670 b->major_mode = Qfundamental_mode;
671 b->keymap = Qnil;
672 b->abbrev_table = Vfundamental_mode_abbrev_table;
673 b->mode_name = QSFundamental;
674 b->minor_modes = Qnil;
676 /* If the standard case table has been altered and invalidated,
677 fix up its insides first. */
678 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
679 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
680 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
681 Fset_standard_case_table (Vascii_downcase_table);
683 b->downcase_table = Vascii_downcase_table;
684 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
685 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
686 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
687 b->invisibility_spec = Qt;
688 #ifndef DOS_NT
689 b->buffer_file_type = Qnil;
690 #endif
692 #if 0
693 b->sort_table = XSTRING (Vascii_sort_table);
694 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
695 #endif /* 0 */
697 /* Reset all (or most) per-buffer variables to their defaults. */
698 b->local_var_alist = Qnil;
699 for (i = 0; i < last_per_buffer_idx; ++i)
700 if (permanent_too || buffer_permanent_local_flags[i] == 0)
701 SET_PER_BUFFER_VALUE_P (b, i, 0);
703 /* For each slot that has a default value,
704 copy that into the slot. */
706 for (offset = PER_BUFFER_VAR_OFFSET (name);
707 offset < sizeof *b;
708 offset += sizeof (Lisp_Object))
710 int idx = PER_BUFFER_IDX (offset);
711 if ((idx > 0
712 && (permanent_too
713 || buffer_permanent_local_flags[idx] == 0))
714 /* Is -2 used anywhere? */
715 || idx == -2)
716 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
720 /* We split this away from generate-new-buffer, because rename-buffer
721 and set-visited-file-name ought to be able to use this to really
722 rename the buffer properly. */
724 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
725 1, 2, 0,
726 "Return a string that is the name of no existing buffer based on NAME.\n\
727 If there is no live buffer named NAME, then return NAME.\n\
728 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
729 until an unused name is found, and then return that name.\n\
730 Optional second argument IGNORE specifies a name that is okay to use\n\
731 \(if it is in the sequence to be tried)\n\
732 even if a buffer with that name exists.")
733 (name, ignore)
734 register Lisp_Object name, ignore;
736 register Lisp_Object gentemp, tem;
737 int count;
738 char number[10];
740 CHECK_STRING (name, 0);
742 tem = Fget_buffer (name);
743 if (NILP (tem))
744 return name;
746 count = 1;
747 while (1)
749 sprintf (number, "<%d>", ++count);
750 gentemp = concat2 (name, build_string (number));
751 tem = Fstring_equal (gentemp, ignore);
752 if (!NILP (tem))
753 return gentemp;
754 tem = Fget_buffer (gentemp);
755 if (NILP (tem))
756 return gentemp;
761 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
762 "Return the name of BUFFER, as a string.\n\
763 With no argument or nil as argument, return the name of the current buffer.")
764 (buffer)
765 register Lisp_Object buffer;
767 if (NILP (buffer))
768 return current_buffer->name;
769 CHECK_BUFFER (buffer, 0);
770 return XBUFFER (buffer)->name;
773 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
774 "Return name of file BUFFER is visiting, or nil if none.\n\
775 No argument or nil as argument means use the current buffer.")
776 (buffer)
777 register Lisp_Object buffer;
779 if (NILP (buffer))
780 return current_buffer->filename;
781 CHECK_BUFFER (buffer, 0);
782 return XBUFFER (buffer)->filename;
785 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
786 0, 1, 0,
787 "Return the base buffer of indirect buffer BUFFER.\n\
788 If BUFFER is not indirect, return nil.")
789 (buffer)
790 register Lisp_Object buffer;
792 struct buffer *base;
793 Lisp_Object base_buffer;
795 if (NILP (buffer))
796 base = current_buffer->base_buffer;
797 else
799 CHECK_BUFFER (buffer, 0);
800 base = XBUFFER (buffer)->base_buffer;
803 if (! base)
804 return Qnil;
805 XSETBUFFER (base_buffer, base);
806 return base_buffer;
809 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
810 Sbuffer_local_variables, 0, 1, 0,
811 "Return an alist of variables that are buffer-local in BUFFER.\n\
812 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
813 For a symbol that is locally unbound, just the symbol appears in the value.\n\
814 Note that storing new VALUEs in these elements doesn't change the variables.\n\
815 No argument or nil as argument means use current buffer as BUFFER.")
816 (buffer)
817 register Lisp_Object buffer;
819 register struct buffer *buf;
820 register Lisp_Object result;
822 if (NILP (buffer))
823 buf = current_buffer;
824 else
826 CHECK_BUFFER (buffer, 0);
827 buf = XBUFFER (buffer);
830 result = Qnil;
833 register Lisp_Object tail;
834 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
836 Lisp_Object val, elt;
838 elt = XCAR (tail);
840 /* Reference each variable in the alist in buf.
841 If inquiring about the current buffer, this gets the current values,
842 so store them into the alist so the alist is up to date.
843 If inquiring about some other buffer, this swaps out any values
844 for that buffer, making the alist up to date automatically. */
845 val = find_symbol_value (XCAR (elt));
846 /* Use the current buffer value only if buf is the current buffer. */
847 if (buf != current_buffer)
848 val = XCDR (elt);
850 /* If symbol is unbound, put just the symbol in the list. */
851 if (EQ (val, Qunbound))
852 result = Fcons (XCAR (elt), result);
853 /* Otherwise, put (symbol . value) in the list. */
854 else
855 result = Fcons (Fcons (XCAR (elt), val), result);
859 /* Add on all the variables stored in special slots. */
861 int offset, idx;
863 for (offset = PER_BUFFER_VAR_OFFSET (name);
864 offset < sizeof (struct buffer);
865 /* sizeof EMACS_INT == sizeof Lisp_Object */
866 offset += (sizeof (EMACS_INT)))
868 idx = PER_BUFFER_IDX (offset);
869 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
870 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
871 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
872 PER_BUFFER_VALUE (buf, offset)),
873 result);
877 return result;
881 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
882 0, 1, 0,
883 "Return t if BUFFER was modified since its file was last read or saved.\n\
884 No argument or nil as argument means use current buffer as BUFFER.")
885 (buffer)
886 register Lisp_Object buffer;
888 register struct buffer *buf;
889 if (NILP (buffer))
890 buf = current_buffer;
891 else
893 CHECK_BUFFER (buffer, 0);
894 buf = XBUFFER (buffer);
897 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
900 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
901 1, 1, 0,
902 "Mark current buffer as modified or unmodified according to FLAG.\n\
903 A non-nil FLAG means mark the buffer modified.")
904 (flag)
905 register Lisp_Object flag;
907 register int already;
908 register Lisp_Object fn;
909 Lisp_Object buffer, window;
911 #ifdef CLASH_DETECTION
912 /* If buffer becoming modified, lock the file.
913 If buffer becoming unmodified, unlock the file. */
915 fn = current_buffer->file_truename;
916 /* Test buffer-file-name so that binding it to nil is effective. */
917 if (!NILP (fn) && ! NILP (current_buffer->filename))
919 already = SAVE_MODIFF < MODIFF;
920 if (!already && !NILP (flag))
921 lock_file (fn);
922 else if (already && NILP (flag))
923 unlock_file (fn);
925 #endif /* CLASH_DETECTION */
927 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
929 /* Set update_mode_lines only if buffer is displayed in some window.
930 Packages like jit-lock or lazy-lock preserve a buffer's modified
931 state by recording/restoring the state around blocks of code.
932 Setting update_mode_lines makes redisplay consider all windows
933 (on all frames). Stealth fontification of buffers not displayed
934 would incur additional redisplay costs if we'd set
935 update_modes_lines unconditionally.
937 Ideally, I think there should be another mechanism for fontifying
938 buffers without "modifying" buffers, or redisplay should be
939 smarter about updating the `*' in mode lines. --gerd */
940 XSETBUFFER (buffer, current_buffer);
941 window = Fget_buffer_window (buffer, Qt);
942 if (WINDOWP (window))
944 ++update_mode_lines;
945 current_buffer->prevent_redisplay_optimizations_p = 1;
948 return flag;
951 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
952 Srestore_buffer_modified_p, 1, 1, 0,
953 "Like `set-buffer-modified-p', with a differences concerning redisplay.\n\
954 It is not ensured that mode lines will be updated to show the modified\n\
955 state of the current buffer. Use with care.")
956 (flag)
957 Lisp_Object flag;
959 #ifdef CLASH_DETECTION
960 Lisp_Object fn;
962 /* If buffer becoming modified, lock the file.
963 If buffer becoming unmodified, unlock the file. */
965 fn = current_buffer->file_truename;
966 /* Test buffer-file-name so that binding it to nil is effective. */
967 if (!NILP (fn) && ! NILP (current_buffer->filename))
969 int already = SAVE_MODIFF < MODIFF;
970 if (!already && !NILP (flag))
971 lock_file (fn);
972 else if (already && NILP (flag))
973 unlock_file (fn);
975 #endif /* CLASH_DETECTION */
977 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
978 return flag;
981 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
982 0, 1, 0,
983 "Return BUFFER's tick counter, incremented for each change in text.\n\
984 Each buffer has a tick counter which is incremented each time the text in\n\
985 that buffer is changed. It wraps around occasionally.\n\
986 No argument or nil as argument means use current buffer as BUFFER.")
987 (buffer)
988 register Lisp_Object buffer;
990 register struct buffer *buf;
991 if (NILP (buffer))
992 buf = current_buffer;
993 else
995 CHECK_BUFFER (buffer, 0);
996 buf = XBUFFER (buffer);
999 return make_number (BUF_MODIFF (buf));
1002 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1003 "sRename buffer (to new name): \nP",
1004 "Change current buffer's name to NEWNAME (a string).\n\
1005 If second arg UNIQUE is nil or omitted, it is an error if a\n\
1006 buffer named NEWNAME already exists.\n\
1007 If UNIQUE is non-nil, come up with a new name using\n\
1008 `generate-new-buffer-name'.\n\
1009 Interactively, you can set UNIQUE with a prefix argument.\n\
1010 We return the name we actually gave the buffer.\n\
1011 This does not change the name of the visited file (if any).")
1012 (newname, unique)
1013 register Lisp_Object newname, unique;
1015 register Lisp_Object tem, buf;
1017 CHECK_STRING (newname, 0);
1019 if (XSTRING (newname)->size == 0)
1020 error ("Empty string is invalid as a buffer name");
1022 tem = Fget_buffer (newname);
1023 if (!NILP (tem))
1025 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1026 rename the buffer automatically so you can create another
1027 with the original name. It makes UNIQUE equivalent to
1028 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1029 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1030 return current_buffer->name;
1031 if (!NILP (unique))
1032 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1033 else
1034 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
1037 current_buffer->name = newname;
1039 /* Catch redisplay's attention. Unless we do this, the mode lines for
1040 any windows displaying current_buffer will stay unchanged. */
1041 update_mode_lines++;
1043 XSETBUFFER (buf, current_buffer);
1044 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1045 if (NILP (current_buffer->filename)
1046 && !NILP (current_buffer->auto_save_file_name))
1047 call0 (intern ("rename-auto-save-file"));
1048 /* Refetch since that last call may have done GC. */
1049 return current_buffer->name;
1052 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1053 "Return most recently selected buffer other than BUFFER.\n\
1054 Buffers not visible in windows are preferred to visible buffers,\n\
1055 unless optional second argument VISIBLE-OK is non-nil.\n\
1056 If the optional third argument FRAME is non-nil, use that frame's\n\
1057 buffer list instead of the selected frame's buffer list.\n\
1058 If no other buffer exists, the buffer `*scratch*' is returned.\n\
1059 If BUFFER is omitted or nil, some interesting buffer is returned.")
1060 (buffer, visible_ok, frame)
1061 register Lisp_Object buffer, visible_ok, frame;
1063 Lisp_Object Fset_buffer_major_mode ();
1064 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1065 notsogood = Qnil;
1067 if (NILP (frame))
1068 frame = selected_frame;
1070 tail = Vbuffer_alist;
1071 pred = frame_buffer_predicate (frame);
1073 /* Consider buffers that have been seen in the selected frame
1074 before other buffers. */
1076 tem = frame_buffer_list (frame);
1077 add_ons = Qnil;
1078 while (CONSP (tem))
1080 if (BUFFERP (XCAR (tem)))
1081 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1082 tem = XCDR (tem);
1084 tail = nconc2 (Fnreverse (add_ons), tail);
1086 for (; !NILP (tail); tail = Fcdr (tail))
1088 buf = Fcdr (Fcar (tail));
1089 if (EQ (buf, buffer))
1090 continue;
1091 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1092 continue;
1093 /* If the selected frame has a buffer_predicate,
1094 disregard buffers that don't fit the predicate. */
1095 if (!NILP (pred))
1097 tem = call1 (pred, buf);
1098 if (NILP (tem))
1099 continue;
1102 if (NILP (visible_ok))
1103 tem = Fget_buffer_window (buf, Qvisible);
1104 else
1105 tem = Qnil;
1106 if (NILP (tem))
1107 return buf;
1108 if (NILP (notsogood))
1109 notsogood = buf;
1111 if (!NILP (notsogood))
1112 return notsogood;
1113 buf = Fget_buffer (build_string ("*scratch*"));
1114 if (NILP (buf))
1116 buf = Fget_buffer_create (build_string ("*scratch*"));
1117 Fset_buffer_major_mode (buf);
1119 return buf;
1122 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
1123 0, 1, "",
1124 "Make BUFFER stop keeping undo information.\n\
1125 No argument or nil as argument means do this for the current buffer.")
1126 (buffer)
1127 register Lisp_Object buffer;
1129 Lisp_Object real_buffer;
1131 if (NILP (buffer))
1132 XSETBUFFER (real_buffer, current_buffer);
1133 else
1135 real_buffer = Fget_buffer (buffer);
1136 if (NILP (real_buffer))
1137 nsberror (buffer);
1140 XBUFFER (real_buffer)->undo_list = Qt;
1142 return Qnil;
1145 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1146 0, 1, "",
1147 "Start keeping undo information for buffer BUFFER.\n\
1148 No argument or nil as argument means do this for the current buffer.")
1149 (buffer)
1150 register Lisp_Object buffer;
1152 Lisp_Object real_buffer;
1154 if (NILP (buffer))
1155 XSETBUFFER (real_buffer, current_buffer);
1156 else
1158 real_buffer = Fget_buffer (buffer);
1159 if (NILP (real_buffer))
1160 nsberror (buffer);
1163 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1164 XBUFFER (real_buffer)->undo_list = Qnil;
1166 return Qnil;
1170 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1171 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1172 The buffer being killed will be current while the hook is running.\n\
1173 See `kill-buffer'."
1175 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1176 "Kill the buffer BUFFER.\n\
1177 The argument may be a buffer or may be the name of a buffer.\n\
1178 An argument of nil means kill the current buffer.\n\n\
1179 Value is t if the buffer is actually killed, nil if user says no.\n\n\
1180 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1181 if not void, is a list of functions to be called, with no arguments,\n\
1182 before the buffer is actually killed. The buffer to be killed is current\n\
1183 when the hook functions are called.\n\n\
1184 Any processes that have this buffer as the `process-buffer' are killed\n\
1185 with SIGHUP.")
1186 (buffer)
1187 Lisp_Object buffer;
1189 Lisp_Object buf;
1190 register struct buffer *b;
1191 register Lisp_Object tem;
1192 register struct Lisp_Marker *m;
1193 struct gcpro gcpro1;
1195 if (NILP (buffer))
1196 buf = Fcurrent_buffer ();
1197 else
1198 buf = Fget_buffer (buffer);
1199 if (NILP (buf))
1200 nsberror (buffer);
1202 b = XBUFFER (buf);
1204 /* Avoid trouble for buffer already dead. */
1205 if (NILP (b->name))
1206 return Qnil;
1208 /* Query if the buffer is still modified. */
1209 if (INTERACTIVE && !NILP (b->filename)
1210 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1212 GCPRO1 (buf);
1213 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1214 XSTRING (b->name)->data));
1215 UNGCPRO;
1216 if (NILP (tem))
1217 return Qnil;
1220 /* Run hooks with the buffer to be killed the current buffer. */
1222 int count = specpdl_ptr - specpdl;
1223 Lisp_Object list;
1225 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1226 set_buffer_internal (b);
1228 /* First run the query functions; if any query is answered no,
1229 don't kill the buffer. */
1230 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1232 tem = call0 (Fcar (list));
1233 if (NILP (tem))
1234 return unbind_to (count, Qnil);
1237 /* Then run the hooks. */
1238 if (!NILP (Vrun_hooks))
1239 call1 (Vrun_hooks, Qkill_buffer_hook);
1240 unbind_to (count, Qnil);
1243 /* We have no more questions to ask. Verify that it is valid
1244 to kill the buffer. This must be done after the questions
1245 since anything can happen within do_yes_or_no_p. */
1247 /* Don't kill the minibuffer now current. */
1248 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1249 return Qnil;
1251 if (NILP (b->name))
1252 return Qnil;
1254 /* When we kill a base buffer, kill all its indirect buffers.
1255 We do it at this stage so nothing terrible happens if they
1256 ask questions or their hooks get errors. */
1257 if (! b->base_buffer)
1259 struct buffer *other;
1261 GCPRO1 (buf);
1263 for (other = all_buffers; other; other = other->next)
1264 /* all_buffers contains dead buffers too;
1265 don't re-kill them. */
1266 if (other->base_buffer == b && !NILP (other->name))
1268 Lisp_Object buf;
1269 XSETBUFFER (buf, other);
1270 Fkill_buffer (buf);
1273 UNGCPRO;
1276 /* Make this buffer not be current.
1277 In the process, notice if this is the sole visible buffer
1278 and give up if so. */
1279 if (b == current_buffer)
1281 tem = Fother_buffer (buf, Qnil, Qnil);
1282 Fset_buffer (tem);
1283 if (b == current_buffer)
1284 return Qnil;
1287 /* Notice if the buffer to kill is the sole visible buffer
1288 when we're currently in the mini-buffer, and give up if so. */
1289 XSETBUFFER (tem, current_buffer);
1290 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1292 tem = Fother_buffer (buf, Qnil, Qnil);
1293 if (EQ (buf, tem))
1294 return Qnil;
1297 /* Now there is no question: we can kill the buffer. */
1299 #ifdef CLASH_DETECTION
1300 /* Unlock this buffer's file, if it is locked. */
1301 unlock_buffer (b);
1302 #endif /* CLASH_DETECTION */
1304 kill_buffer_processes (buf);
1306 tem = Vinhibit_quit;
1307 Vinhibit_quit = Qt;
1308 replace_buffer_in_all_windows (buf);
1309 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1310 frames_discard_buffer (buf);
1311 Vinhibit_quit = tem;
1313 /* Delete any auto-save file, if we saved it in this session.
1314 But not if the buffer is modified. */
1315 if (STRINGP (b->auto_save_file_name)
1316 && b->auto_save_modified != 0
1317 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1318 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
1320 Lisp_Object tem;
1321 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1322 if (! NILP (tem))
1323 internal_delete_file (b->auto_save_file_name);
1326 if (b->base_buffer)
1328 /* Unchain all markers that belong to this indirect buffer.
1329 Don't unchain the markers that belong to the base buffer
1330 or its other indirect buffers. */
1331 for (tem = BUF_MARKERS (b); !NILP (tem); )
1333 Lisp_Object next;
1334 m = XMARKER (tem);
1335 next = m->chain;
1336 if (m->buffer == b)
1337 unchain_marker (tem);
1338 tem = next;
1341 else
1343 /* Unchain all markers of this buffer and its indirect buffers.
1344 and leave them pointing nowhere. */
1345 for (tem = BUF_MARKERS (b); !NILP (tem); )
1347 m = XMARKER (tem);
1348 m->buffer = 0;
1349 tem = m->chain;
1350 m->chain = Qnil;
1352 BUF_MARKERS (b) = Qnil;
1353 BUF_INTERVALS (b) = NULL_INTERVAL;
1355 /* Perhaps we should explicitly free the interval tree here... */
1358 /* Reset the local variables, so that this buffer's local values
1359 won't be protected from GC. They would be protected
1360 if they happened to remain encached in their symbols.
1361 This gets rid of them for certain. */
1362 swap_out_buffer_local_variables (b);
1363 reset_buffer_local_variables (b, 1);
1365 b->name = Qnil;
1367 BLOCK_INPUT;
1368 if (! b->base_buffer)
1369 free_buffer_text (b);
1371 if (b->newline_cache)
1373 free_region_cache (b->newline_cache);
1374 b->newline_cache = 0;
1376 if (b->width_run_cache)
1378 free_region_cache (b->width_run_cache);
1379 b->width_run_cache = 0;
1381 b->width_table = Qnil;
1382 UNBLOCK_INPUT;
1383 b->undo_list = Qnil;
1385 return Qt;
1388 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1389 we do this each time BUF is selected visibly, the more recently
1390 selected buffers are always closer to the front of the list. This
1391 means that other_buffer is more likely to choose a relevant buffer. */
1393 void
1394 record_buffer (buf)
1395 Lisp_Object buf;
1397 register Lisp_Object link, prev;
1398 Lisp_Object frame;
1399 frame = selected_frame;
1401 prev = Qnil;
1402 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1404 if (EQ (XCDR (XCAR (link)), buf))
1405 break;
1406 prev = link;
1409 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1410 we cannot use Fdelq itself here because it allows quitting. */
1412 if (NILP (prev))
1413 Vbuffer_alist = XCDR (Vbuffer_alist);
1414 else
1415 XCDR (prev) = XCDR (XCDR (prev));
1417 XCDR (link) = Vbuffer_alist;
1418 Vbuffer_alist = link;
1420 /* Now move this buffer to the front of frame_buffer_list also. */
1422 prev = Qnil;
1423 for (link = frame_buffer_list (frame); CONSP (link);
1424 link = XCDR (link))
1426 if (EQ (XCAR (link), buf))
1427 break;
1428 prev = link;
1431 /* Effectively do delq. */
1433 if (CONSP (link))
1435 if (NILP (prev))
1436 set_frame_buffer_list (frame,
1437 XCDR (frame_buffer_list (frame)));
1438 else
1439 XCDR (prev) = XCDR (XCDR (prev));
1441 XCDR (link) = frame_buffer_list (frame);
1442 set_frame_buffer_list (frame, link);
1444 else
1445 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1448 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1449 "Set an appropriate major mode for BUFFER.\n\
1450 For the *scratch* buffer use `initial-major-mode', otherwise choose a\n\
1451 mode according to `default-major-mode'.\n\
1452 Use this function before selecting the buffer, since it may need to inspect\n\
1453 the current buffer's major mode.")
1454 (buffer)
1455 Lisp_Object buffer;
1457 int count;
1458 Lisp_Object function;
1460 if (STRINGP (XBUFFER (buffer)->name)
1461 && strcmp (XSTRING (XBUFFER (buffer)->name)->data, "*scratch*") == 0)
1462 function = find_symbol_value (intern ("initial-major-mode"));
1463 else
1465 function = buffer_defaults.major_mode;
1466 if (NILP (function)
1467 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1468 function = current_buffer->major_mode;
1471 if (NILP (function) || EQ (function, Qfundamental_mode))
1472 return Qnil;
1474 count = specpdl_ptr - specpdl;
1476 /* To select a nonfundamental mode,
1477 select the buffer temporarily and then call the mode function. */
1479 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1481 Fset_buffer (buffer);
1482 call0 (function);
1484 return unbind_to (count, Qnil);
1487 /* If switching buffers in WINDOW would be an error, return
1488 a C string saying what the error would be. */
1490 char *
1491 no_switch_window (window)
1492 Lisp_Object window;
1494 Lisp_Object tem;
1495 if (EQ (minibuf_window, window))
1496 return "Cannot switch buffers in minibuffer window";
1497 tem = Fwindow_dedicated_p (window);
1498 if (!NILP (tem))
1499 return "Cannot switch buffers in a dedicated window";
1500 return NULL;
1503 /* Switch to buffer BUFFER in the selected window.
1504 If NORECORD is non-nil, don't call record_buffer. */
1506 Lisp_Object
1507 switch_to_buffer_1 (buffer, norecord)
1508 Lisp_Object buffer, norecord;
1510 register Lisp_Object buf;
1512 if (NILP (buffer))
1513 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1514 else
1516 buf = Fget_buffer (buffer);
1517 if (NILP (buf))
1519 buf = Fget_buffer_create (buffer);
1520 Fset_buffer_major_mode (buf);
1523 Fset_buffer (buf);
1524 if (NILP (norecord))
1525 record_buffer (buf);
1527 Fset_window_buffer (EQ (selected_window, minibuf_window)
1528 ? Fnext_window (minibuf_window, Qnil, Qnil)
1529 : selected_window,
1530 buf);
1532 return buf;
1535 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1536 "Select buffer BUFFER in the current window.\n\
1537 BUFFER may be a buffer or a buffer name.\n\
1538 Optional second arg NORECORD non-nil means\n\
1539 do not put this buffer at the front of the list of recently selected ones.\n\
1541 WARNING: This is NOT the way to work on another buffer temporarily\n\
1542 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1543 the window-buffer correspondences.")
1544 (buffer, norecord)
1545 Lisp_Object buffer, norecord;
1547 char *err;
1549 err = no_switch_window (selected_window);
1550 if (err) error (err);
1552 return switch_to_buffer_1 (buffer, norecord);
1555 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1556 "Select buffer BUFFER in some window, preferably a different one.\n\
1557 If BUFFER is nil, then some other buffer is chosen.\n\
1558 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1559 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1560 window even if BUFFER is already visible in the selected window.\n\
1561 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1562 of `display-buffer' for additional customization information.\n\
1564 Optional third arg NORECORD non-nil means\n\
1565 do not put this buffer at the front of the list of recently selected ones.")
1566 (buffer, other_window, norecord)
1567 Lisp_Object buffer, other_window, norecord;
1569 register Lisp_Object buf;
1570 if (NILP (buffer))
1571 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1572 else
1574 buf = Fget_buffer (buffer);
1575 if (NILP (buf))
1577 buf = Fget_buffer_create (buffer);
1578 Fset_buffer_major_mode (buf);
1581 Fset_buffer (buf);
1582 if (NILP (norecord))
1583 record_buffer (buf);
1584 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
1585 return buf;
1588 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1589 "Return the current buffer as a Lisp object.")
1592 register Lisp_Object buf;
1593 XSETBUFFER (buf, current_buffer);
1594 return buf;
1597 /* Set the current buffer to B.
1599 We previously set windows_or_buffers_changed here to invalidate
1600 global unchanged information in beg_unchanged and end_unchanged.
1601 This is no longer necessary because we now compute unchanged
1602 information on a buffer-basis. Every action affecting other
1603 windows than the selected one requires a select_window at some
1604 time, and that increments windows_or_buffers_changed. */
1606 void
1607 set_buffer_internal (b)
1608 register struct buffer *b;
1610 if (current_buffer != b)
1611 set_buffer_internal_1 (b);
1614 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1615 This is used by redisplay. */
1617 void
1618 set_buffer_internal_1 (b)
1619 register struct buffer *b;
1621 register struct buffer *old_buf;
1622 register Lisp_Object tail, valcontents;
1623 Lisp_Object tem;
1625 #ifdef USE_MMAP_FOR_BUFFERS
1626 if (b->text->beg == NULL)
1627 enlarge_buffer_text (b, 0);
1628 #endif /* USE_MMAP_FOR_BUFFERS */
1630 if (current_buffer == b)
1631 return;
1633 old_buf = current_buffer;
1634 current_buffer = b;
1635 last_known_column_point = -1; /* invalidate indentation cache */
1637 if (old_buf)
1639 /* Put the undo list back in the base buffer, so that it appears
1640 that an indirect buffer shares the undo list of its base. */
1641 if (old_buf->base_buffer)
1642 old_buf->base_buffer->undo_list = old_buf->undo_list;
1644 /* If the old current buffer has markers to record PT, BEGV and ZV
1645 when it is not current, update them now. */
1646 if (! NILP (old_buf->pt_marker))
1648 Lisp_Object obuf;
1649 XSETBUFFER (obuf, old_buf);
1650 set_marker_both (old_buf->pt_marker, obuf,
1651 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1653 if (! NILP (old_buf->begv_marker))
1655 Lisp_Object obuf;
1656 XSETBUFFER (obuf, old_buf);
1657 set_marker_both (old_buf->begv_marker, obuf,
1658 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1660 if (! NILP (old_buf->zv_marker))
1662 Lisp_Object obuf;
1663 XSETBUFFER (obuf, old_buf);
1664 set_marker_both (old_buf->zv_marker, obuf,
1665 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1669 /* Get the undo list from the base buffer, so that it appears
1670 that an indirect buffer shares the undo list of its base. */
1671 if (b->base_buffer)
1672 b->undo_list = b->base_buffer->undo_list;
1674 /* If the new current buffer has markers to record PT, BEGV and ZV
1675 when it is not current, fetch them now. */
1676 if (! NILP (b->pt_marker))
1678 BUF_PT (b) = marker_position (b->pt_marker);
1679 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1681 if (! NILP (b->begv_marker))
1683 BUF_BEGV (b) = marker_position (b->begv_marker);
1684 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1686 if (! NILP (b->zv_marker))
1688 BUF_ZV (b) = marker_position (b->zv_marker);
1689 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1692 /* Look down buffer's list of local Lisp variables
1693 to find and update any that forward into C variables. */
1695 for (tail = b->local_var_alist; !NILP (tail); tail = XCDR (tail))
1697 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1698 if ((BUFFER_LOCAL_VALUEP (valcontents)
1699 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1700 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1701 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1702 /* Just reference the variable
1703 to cause it to become set for this buffer. */
1704 Fsymbol_value (XCAR (XCAR (tail)));
1707 /* Do the same with any others that were local to the previous buffer */
1709 if (old_buf)
1710 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCDR (tail))
1712 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
1713 if ((BUFFER_LOCAL_VALUEP (valcontents)
1714 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1715 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1716 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1717 /* Just reference the variable
1718 to cause it to become set for this buffer. */
1719 Fsymbol_value (XCAR (XCAR (tail)));
1723 /* Switch to buffer B temporarily for redisplay purposes.
1724 This avoids certain things that don't need to be done within redisplay. */
1726 void
1727 set_buffer_temp (b)
1728 struct buffer *b;
1730 register struct buffer *old_buf;
1732 if (current_buffer == b)
1733 return;
1735 old_buf = current_buffer;
1736 current_buffer = b;
1738 if (old_buf)
1740 /* If the old current buffer has markers to record PT, BEGV and ZV
1741 when it is not current, update them now. */
1742 if (! NILP (old_buf->pt_marker))
1744 Lisp_Object obuf;
1745 XSETBUFFER (obuf, old_buf);
1746 set_marker_both (old_buf->pt_marker, obuf,
1747 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1749 if (! NILP (old_buf->begv_marker))
1751 Lisp_Object obuf;
1752 XSETBUFFER (obuf, old_buf);
1753 set_marker_both (old_buf->begv_marker, obuf,
1754 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1756 if (! NILP (old_buf->zv_marker))
1758 Lisp_Object obuf;
1759 XSETBUFFER (obuf, old_buf);
1760 set_marker_both (old_buf->zv_marker, obuf,
1761 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1765 /* If the new current buffer has markers to record PT, BEGV and ZV
1766 when it is not current, fetch them now. */
1767 if (! NILP (b->pt_marker))
1769 BUF_PT (b) = marker_position (b->pt_marker);
1770 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1772 if (! NILP (b->begv_marker))
1774 BUF_BEGV (b) = marker_position (b->begv_marker);
1775 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1777 if (! NILP (b->zv_marker))
1779 BUF_ZV (b) = marker_position (b->zv_marker);
1780 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1784 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1785 "Make the buffer BUFFER current for editing operations.\n\
1786 BUFFER may be a buffer or the name of an existing buffer.\n\
1787 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1788 This function does not display the buffer, so its effect ends\n\
1789 when the current command terminates.\n\
1790 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1791 (buffer)
1792 register Lisp_Object buffer;
1794 register Lisp_Object buf;
1795 buf = Fget_buffer (buffer);
1796 if (NILP (buf))
1797 nsberror (buffer);
1798 if (NILP (XBUFFER (buf)->name))
1799 error ("Selecting deleted buffer");
1800 set_buffer_internal (XBUFFER (buf));
1801 return buf;
1804 /* Set the current buffer to BUFFER provided it is alive. */
1806 Lisp_Object
1807 set_buffer_if_live (buffer)
1808 Lisp_Object buffer;
1810 if (! NILP (XBUFFER (buffer)->name))
1811 Fset_buffer (buffer);
1812 return Qnil;
1815 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1816 Sbarf_if_buffer_read_only, 0, 0, 0,
1817 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1820 if (!NILP (current_buffer->read_only)
1821 && NILP (Vinhibit_read_only))
1822 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1823 return Qnil;
1826 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1827 "Put BUFFER at the end of the list of all buffers.\n\
1828 There it is the least likely candidate for `other-buffer' to return;\n\
1829 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1830 If BUFFER is nil or omitted, bury the current buffer.\n\
1831 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1832 selected window if it is displayed there.")
1833 (buffer)
1834 register Lisp_Object buffer;
1836 /* Figure out what buffer we're going to bury. */
1837 if (NILP (buffer))
1839 XSETBUFFER (buffer, current_buffer);
1841 /* If we're burying the current buffer, unshow it. */
1842 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1844 else
1846 Lisp_Object buf1;
1848 buf1 = Fget_buffer (buffer);
1849 if (NILP (buf1))
1850 nsberror (buffer);
1851 buffer = buf1;
1854 /* Move buffer to the end of the buffer list. Do nothing if the
1855 buffer is killed. */
1856 if (!NILP (XBUFFER (buffer)->name))
1858 Lisp_Object aelt, link;
1860 aelt = Frassq (buffer, Vbuffer_alist);
1861 link = Fmemq (aelt, Vbuffer_alist);
1862 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1863 XCDR (link) = Qnil;
1864 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1866 frames_bury_buffer (buffer);
1869 return Qnil;
1872 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1873 "Delete the entire contents of the current buffer.\n\
1874 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1875 so the buffer is truly empty after this.")
1878 Fwiden ();
1880 del_range (BEG, Z);
1882 current_buffer->last_window_start = 1;
1883 /* Prevent warnings, or suspension of auto saving, that would happen
1884 if future size is less than past size. Use of erase-buffer
1885 implies that the future text is not really related to the past text. */
1886 XSETFASTINT (current_buffer->save_length, 0);
1887 return Qnil;
1890 void
1891 validate_region (b, e)
1892 register Lisp_Object *b, *e;
1894 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1895 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1897 if (XINT (*b) > XINT (*e))
1899 Lisp_Object tem;
1900 tem = *b; *b = *e; *e = tem;
1903 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1904 && XINT (*e) <= ZV))
1905 args_out_of_range (*b, *e);
1908 /* Advance BYTE_POS up to a character boundary
1909 and return the adjusted position. */
1911 static int
1912 advance_to_char_boundary (byte_pos)
1913 int byte_pos;
1915 int c;
1917 if (byte_pos == BEG)
1918 /* Beginning of buffer is always a character boundary. */
1919 return 1;
1921 c = FETCH_BYTE (byte_pos);
1922 if (! CHAR_HEAD_P (c))
1924 /* We should advance BYTE_POS only when C is a constituent of a
1925 multibyte sequence. */
1926 int orig_byte_pos = byte_pos;
1930 byte_pos--;
1931 c = FETCH_BYTE (byte_pos);
1933 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
1934 INC_POS (byte_pos);
1935 if (byte_pos < orig_byte_pos)
1936 byte_pos = orig_byte_pos;
1937 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1938 surely advance to the correct character boundary. If C is
1939 not, BYTE_POS was unchanged. */
1942 return byte_pos;
1945 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1946 1, 1, 0,
1947 "Set the multibyte flag of the current buffer to FLAG.\n\
1948 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1949 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1950 The buffer contents remain unchanged as a sequence of bytes\n\
1951 but the contents viewed as characters do change.")
1952 (flag)
1953 Lisp_Object flag;
1955 Lisp_Object tail, markers;
1956 struct buffer *other;
1957 int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
1958 int begv = BEGV, zv = ZV;
1959 int narrowed = (BEG != begv || Z != zv);
1960 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
1962 if (current_buffer->base_buffer)
1963 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
1965 /* Do nothing if nothing actually changes. */
1966 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
1967 return flag;
1969 /* It would be better to update the list,
1970 but this is good enough for now. */
1971 if (undo_enabled_p)
1972 current_buffer->undo_list = Qt;
1974 /* If the cached position is for this buffer, clear it out. */
1975 clear_charpos_cache (current_buffer);
1977 if (narrowed)
1978 Fwiden ();
1980 if (NILP (flag))
1982 int pos, stop;
1983 unsigned char *p;
1985 /* Do this first, so it can use CHAR_TO_BYTE
1986 to calculate the old correspondences. */
1987 set_intervals_multibyte (0);
1989 current_buffer->enable_multibyte_characters = Qnil;
1991 Z = Z_BYTE;
1992 BEGV = BEGV_BYTE;
1993 ZV = ZV_BYTE;
1994 GPT = GPT_BYTE;
1995 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
1997 tail = BUF_MARKERS (current_buffer);
1998 while (! NILP (tail))
2000 XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
2001 tail = XMARKER (tail)->chain;
2004 /* Convert multibyte form of 8-bit characters to unibyte. */
2005 pos = BEG;
2006 stop = GPT;
2007 p = BEG_ADDR;
2008 while (1)
2010 int c, bytes;
2012 if (pos == stop)
2014 if (pos == Z)
2015 break;
2016 p = GAP_END_ADDR;
2017 stop = Z;
2019 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
2020 p += bytes, pos += bytes;
2021 else
2023 c = STRING_CHAR (p, stop - pos);
2024 /* Delete all bytes for this 8-bit character but the
2025 last one, and change the last one to the charcter
2026 code. */
2027 bytes--;
2028 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2029 p = GAP_END_ADDR;
2030 *p++ = c;
2031 pos++;
2032 if (begv > pos)
2033 begv -= bytes;
2034 if (zv > pos)
2035 zv -= bytes;
2036 stop = Z;
2039 if (narrowed)
2040 Fnarrow_to_region (make_number (begv), make_number (zv));
2042 else
2044 int pt = PT;
2045 int pos, stop;
2046 unsigned char *p;
2048 /* Be sure not to have a multibyte sequence striding over the GAP.
2049 Ex: We change this: "...abc\201 _GAP_ \241def..."
2050 to: "...abc _GAP_ \201\241def..." */
2052 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2053 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2055 unsigned char *p = GPT_ADDR - 1;
2057 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2058 if (BASE_LEADING_CODE_P (*p))
2060 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2062 move_gap_both (new_gpt, new_gpt);
2066 /* Make the buffer contents valid as multibyte by converting
2067 8-bit characters to multibyte form. */
2068 pos = BEG;
2069 stop = GPT;
2070 p = BEG_ADDR;
2071 while (1)
2073 int bytes;
2075 if (pos == stop)
2077 if (pos == Z)
2078 break;
2079 p = GAP_END_ADDR;
2080 stop = Z;
2083 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2084 p += bytes, pos += bytes;
2085 else
2087 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2089 bytes = CHAR_STRING (*p, tmp);
2090 *p = tmp[0];
2091 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2092 bytes--;
2093 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2094 /* Now the gap is after the just inserted data. */
2095 pos = GPT;
2096 p = GAP_END_ADDR;
2097 if (pos <= begv)
2098 begv += bytes;
2099 if (pos <= zv)
2100 zv += bytes;
2101 if (pos <= pt)
2102 pt += bytes;
2103 stop = Z;
2107 if (pt != PT)
2108 TEMP_SET_PT (pt);
2110 if (narrowed)
2111 Fnarrow_to_region (make_number (begv), make_number (zv));
2113 /* Do this first, so that chars_in_text asks the right question.
2114 set_intervals_multibyte needs it too. */
2115 current_buffer->enable_multibyte_characters = Qt;
2117 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2118 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2120 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2122 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2123 if (BEGV_BYTE > GPT_BYTE)
2124 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2125 else
2126 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2128 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2129 if (ZV_BYTE > GPT_BYTE)
2130 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2131 else
2132 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2135 int pt_byte = advance_to_char_boundary (PT_BYTE);
2136 int pt;
2138 if (pt_byte > GPT_BYTE)
2139 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2140 else
2141 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2142 TEMP_SET_PT_BOTH (pt, pt_byte);
2145 tail = markers = BUF_MARKERS (current_buffer);
2147 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2148 getting confused by the markers that have not yet been updated.
2149 It is also a signal that it should never create a marker. */
2150 BUF_MARKERS (current_buffer) = Qnil;
2152 while (! NILP (tail))
2154 XMARKER (tail)->bytepos
2155 = advance_to_char_boundary (XMARKER (tail)->bytepos);
2156 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
2158 tail = XMARKER (tail)->chain;
2161 /* Make sure no markers were put on the chain
2162 while the chain value was incorrect. */
2163 if (! EQ (BUF_MARKERS (current_buffer), Qnil))
2164 abort ();
2166 BUF_MARKERS (current_buffer) = markers;
2168 /* Do this last, so it can calculate the new correspondences
2169 between chars and bytes. */
2170 set_intervals_multibyte (1);
2173 if (undo_enabled_p)
2174 current_buffer->undo_list = Qnil;
2176 /* Changing the multibyteness of a buffer means that all windows
2177 showing that buffer must be updated thoroughly. */
2178 current_buffer->prevent_redisplay_optimizations_p = 1;
2179 ++windows_or_buffers_changed;
2181 /* Copy this buffer's new multibyte status
2182 into all of its indirect buffers. */
2183 for (other = all_buffers; other; other = other->next)
2184 if (other->base_buffer == current_buffer && !NILP (other->name))
2186 other->enable_multibyte_characters
2187 = current_buffer->enable_multibyte_characters;
2188 other->prevent_redisplay_optimizations_p = 1;
2191 /* Restore the modifiedness of the buffer. */
2192 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2193 Fset_buffer_modified_p (Qnil);
2195 return flag;
2198 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2199 0, 0, 0,
2200 "Switch to Fundamental mode by killing current buffer's local variables.\n\
2201 Most local variable bindings are eliminated so that the default values\n\
2202 become effective once more. Also, the syntax table is set from\n\
2203 `standard-syntax-table', the local keymap is set to nil,\n\
2204 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
2205 This function also forces redisplay of the mode line.\n\
2207 Every function to select a new major mode starts by\n\
2208 calling this function.\n\n\
2209 As a special exception, local variables whose names have\n\
2210 a non-nil `permanent-local' property are not eliminated by this function.\n\
2212 The first thing this function does is run\n\
2213 the normal hook `change-major-mode-hook'.")
2216 register Lisp_Object alist, sym, tem;
2217 Lisp_Object oalist;
2219 if (!NILP (Vrun_hooks))
2220 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
2221 oalist = current_buffer->local_var_alist;
2223 /* Make sure none of the bindings in oalist
2224 remain swapped in, in their symbols. */
2226 swap_out_buffer_local_variables (current_buffer);
2228 /* Actually eliminate all local bindings of this buffer. */
2230 reset_buffer_local_variables (current_buffer, 0);
2232 /* Redisplay mode lines; we are changing major mode. */
2234 update_mode_lines++;
2236 /* Any which are supposed to be permanent,
2237 make local again, with the same values they had. */
2239 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2241 sym = XCAR (XCAR (alist));
2242 tem = Fget (sym, Qpermanent_local);
2243 if (! NILP (tem))
2245 Fmake_local_variable (sym);
2246 Fset (sym, XCDR (XCAR (alist)));
2250 /* Force mode-line redisplay. Useful here because all major mode
2251 commands call this function. */
2252 update_mode_lines++;
2254 return Qnil;
2257 /* Make sure no local variables remain set up with buffer B
2258 for their current values. */
2260 static void
2261 swap_out_buffer_local_variables (b)
2262 struct buffer *b;
2264 Lisp_Object oalist, alist, sym, tem, buffer;
2266 XSETBUFFER (buffer, b);
2267 oalist = b->local_var_alist;
2269 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2271 sym = XCAR (XCAR (alist));
2273 /* Need not do anything if some other buffer's binding is now encached. */
2274 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
2275 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2277 /* Symbol is set up for this buffer's old local value.
2278 Set it up for the current buffer with the default value. */
2280 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr;
2281 /* Store the symbol's current value into the alist entry
2282 it is currently set up for. This is so that, if the
2283 local is marked permanent, and we make it local again
2284 later in Fkill_all_local_variables, we don't lose the value. */
2285 XCDR (XCAR (tem))
2286 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
2287 /* Switch to the symbol's default-value alist entry. */
2288 XCAR (tem) = tem;
2289 /* Mark it as current for buffer B. */
2290 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
2291 /* Store the current value into any forwarding in the symbol. */
2292 store_symval_forwarding (sym,
2293 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
2294 XCDR (tem), NULL);
2299 /* Find all the overlays in the current buffer that contain position POS.
2300 Return the number found, and store them in a vector in *VEC_PTR.
2301 Store in *LEN_PTR the size allocated for the vector.
2302 Store in *NEXT_PTR the next position after POS where an overlay starts,
2303 or ZV if there are no more overlays.
2304 Store in *PREV_PTR the previous position before POS where an overlay ends,
2305 or where an overlay starts which ends at or after POS;
2306 or BEGV if there are no such overlays.
2307 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2309 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2310 when this function is called.
2312 If EXTEND is non-zero, we make the vector bigger if necessary.
2313 If EXTEND is zero, we never extend the vector,
2314 and we store only as many overlays as will fit.
2315 But we still return the total number of overlays.
2317 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2318 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2319 default (BEGV or ZV). */
2322 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2323 int pos;
2324 int extend;
2325 Lisp_Object **vec_ptr;
2326 int *len_ptr;
2327 int *next_ptr;
2328 int *prev_ptr;
2329 int change_req;
2331 Lisp_Object tail, overlay, start, end;
2332 int idx = 0;
2333 int len = *len_ptr;
2334 Lisp_Object *vec = *vec_ptr;
2335 int next = ZV;
2336 int prev = BEGV;
2337 int inhibit_storing = 0;
2339 for (tail = current_buffer->overlays_before;
2340 GC_CONSP (tail);
2341 tail = XCDR (tail))
2343 int startpos, endpos;
2345 overlay = XCAR (tail);
2347 start = OVERLAY_START (overlay);
2348 end = OVERLAY_END (overlay);
2349 endpos = OVERLAY_POSITION (end);
2350 if (endpos < pos)
2352 if (prev < endpos)
2353 prev = endpos;
2354 break;
2356 startpos = OVERLAY_POSITION (start);
2357 /* This one ends at or after POS
2358 so its start counts for PREV_PTR if it's before POS. */
2359 if (prev < startpos && startpos < pos)
2360 prev = startpos;
2361 if (endpos == pos)
2362 continue;
2363 if (startpos <= pos)
2365 if (idx == len)
2367 /* The supplied vector is full.
2368 Either make it bigger, or don't store any more in it. */
2369 if (extend)
2371 /* Make it work with an initial len == 0. */
2372 len *= 2;
2373 if (len == 0)
2374 len = 4;
2375 *len_ptr = len;
2376 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2377 *vec_ptr = vec;
2379 else
2380 inhibit_storing = 1;
2383 if (!inhibit_storing)
2384 vec[idx] = overlay;
2385 /* Keep counting overlays even if we can't return them all. */
2386 idx++;
2388 else if (startpos < next)
2389 next = startpos;
2392 for (tail = current_buffer->overlays_after;
2393 GC_CONSP (tail);
2394 tail = XCDR (tail))
2396 int startpos, endpos;
2398 overlay = XCAR (tail);
2400 start = OVERLAY_START (overlay);
2401 end = OVERLAY_END (overlay);
2402 startpos = OVERLAY_POSITION (start);
2403 if (pos < startpos)
2405 if (startpos < next)
2406 next = startpos;
2407 break;
2409 endpos = OVERLAY_POSITION (end);
2410 if (pos < endpos)
2412 if (idx == len)
2414 if (extend)
2416 *len_ptr = len *= 2;
2417 if (len == 0)
2418 len = *len_ptr = 4;
2419 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2420 *vec_ptr = vec;
2422 else
2423 inhibit_storing = 1;
2426 if (!inhibit_storing)
2427 vec[idx] = overlay;
2428 idx++;
2430 if (startpos < pos && startpos > prev)
2431 prev = startpos;
2433 else if (endpos < pos && endpos > prev)
2434 prev = endpos;
2435 else if (endpos == pos && startpos > prev
2436 && (!change_req || startpos < pos))
2437 prev = startpos;
2440 if (next_ptr)
2441 *next_ptr = next;
2442 if (prev_ptr)
2443 *prev_ptr = prev;
2444 return idx;
2447 /* Find all the overlays in the current buffer that overlap the range BEG-END
2448 or are empty at BEG.
2450 Return the number found, and store them in a vector in *VEC_PTR.
2451 Store in *LEN_PTR the size allocated for the vector.
2452 Store in *NEXT_PTR the next position after POS where an overlay starts,
2453 or ZV if there are no more overlays.
2454 Store in *PREV_PTR the previous position before POS where an overlay ends,
2455 or BEGV if there are no previous overlays.
2456 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2458 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2459 when this function is called.
2461 If EXTEND is non-zero, we make the vector bigger if necessary.
2462 If EXTEND is zero, we never extend the vector,
2463 and we store only as many overlays as will fit.
2464 But we still return the total number of overlays. */
2467 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2468 int beg, end;
2469 int extend;
2470 Lisp_Object **vec_ptr;
2471 int *len_ptr;
2472 int *next_ptr;
2473 int *prev_ptr;
2475 Lisp_Object tail, overlay, ostart, oend;
2476 int idx = 0;
2477 int len = *len_ptr;
2478 Lisp_Object *vec = *vec_ptr;
2479 int next = ZV;
2480 int prev = BEGV;
2481 int inhibit_storing = 0;
2483 for (tail = current_buffer->overlays_before;
2484 GC_CONSP (tail);
2485 tail = XCDR (tail))
2487 int startpos, endpos;
2489 overlay = XCAR (tail);
2491 ostart = OVERLAY_START (overlay);
2492 oend = OVERLAY_END (overlay);
2493 endpos = OVERLAY_POSITION (oend);
2494 if (endpos < beg)
2496 if (prev < endpos)
2497 prev = endpos;
2498 break;
2500 startpos = OVERLAY_POSITION (ostart);
2501 /* Count an interval if it either overlaps the range
2502 or is empty at the start of the range. */
2503 if ((beg < endpos && startpos < end)
2504 || (startpos == endpos && beg == endpos))
2506 if (idx == len)
2508 /* The supplied vector is full.
2509 Either make it bigger, or don't store any more in it. */
2510 if (extend)
2512 *len_ptr = len *= 2;
2513 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2514 *vec_ptr = vec;
2516 else
2517 inhibit_storing = 1;
2520 if (!inhibit_storing)
2521 vec[idx] = overlay;
2522 /* Keep counting overlays even if we can't return them all. */
2523 idx++;
2525 else if (startpos < next)
2526 next = startpos;
2529 for (tail = current_buffer->overlays_after;
2530 GC_CONSP (tail);
2531 tail = XCDR (tail))
2533 int startpos, endpos;
2535 overlay = XCAR (tail);
2537 ostart = OVERLAY_START (overlay);
2538 oend = OVERLAY_END (overlay);
2539 startpos = OVERLAY_POSITION (ostart);
2540 if (end < startpos)
2542 if (startpos < next)
2543 next = startpos;
2544 break;
2546 endpos = OVERLAY_POSITION (oend);
2547 /* Count an interval if it either overlaps the range
2548 or is empty at the start of the range. */
2549 if ((beg < endpos && startpos < end)
2550 || (startpos == endpos && beg == endpos))
2552 if (idx == len)
2554 if (extend)
2556 *len_ptr = len *= 2;
2557 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2558 *vec_ptr = vec;
2560 else
2561 inhibit_storing = 1;
2564 if (!inhibit_storing)
2565 vec[idx] = overlay;
2566 idx++;
2568 else if (endpos < beg && endpos > prev)
2569 prev = endpos;
2572 if (next_ptr)
2573 *next_ptr = next;
2574 if (prev_ptr)
2575 *prev_ptr = prev;
2576 return idx;
2580 /* Return non-zero if there exists an overlay with a non-nil
2581 `mouse-face' property overlapping OVERLAY. */
2584 mouse_face_overlay_overlaps (overlay)
2585 Lisp_Object overlay;
2587 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2588 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2589 int n, i, size;
2590 Lisp_Object *v, tem;
2592 size = 10;
2593 v = (Lisp_Object *) alloca (size * sizeof *v);
2594 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2595 if (n > size)
2597 v = (Lisp_Object *) alloca (n * sizeof *v);
2598 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2601 for (i = 0; i < n; ++i)
2602 if (!EQ (v[i], overlay)
2603 && (tem = Foverlay_get (overlay, Qmouse_face),
2604 !NILP (tem)))
2605 break;
2607 return i < n;
2612 /* Fast function to just test if we're at an overlay boundary. */
2614 overlay_touches_p (pos)
2615 int pos;
2617 Lisp_Object tail, overlay;
2619 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
2620 tail = XCDR (tail))
2622 int endpos;
2624 overlay = XCAR (tail);
2625 if (!GC_OVERLAYP (overlay))
2626 abort ();
2628 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2629 if (endpos < pos)
2630 break;
2631 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2632 return 1;
2635 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2636 tail = XCDR (tail))
2638 int startpos;
2640 overlay = XCAR (tail);
2641 if (!GC_OVERLAYP (overlay))
2642 abort ();
2644 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2645 if (pos < startpos)
2646 break;
2647 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2648 return 1;
2650 return 0;
2653 struct sortvec
2655 Lisp_Object overlay;
2656 int beg, end;
2657 int priority;
2660 static int
2661 compare_overlays (v1, v2)
2662 const void *v1, *v2;
2664 const struct sortvec *s1 = (const struct sortvec *) v1;
2665 const struct sortvec *s2 = (const struct sortvec *) v2;
2666 if (s1->priority != s2->priority)
2667 return s1->priority - s2->priority;
2668 if (s1->beg != s2->beg)
2669 return s1->beg - s2->beg;
2670 if (s1->end != s2->end)
2671 return s2->end - s1->end;
2672 return 0;
2675 /* Sort an array of overlays by priority. The array is modified in place.
2676 The return value is the new size; this may be smaller than the original
2677 size if some of the overlays were invalid or were window-specific. */
2679 sort_overlays (overlay_vec, noverlays, w)
2680 Lisp_Object *overlay_vec;
2681 int noverlays;
2682 struct window *w;
2684 int i, j;
2685 struct sortvec *sortvec;
2686 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2688 /* Put the valid and relevant overlays into sortvec. */
2690 for (i = 0, j = 0; i < noverlays; i++)
2692 Lisp_Object tem;
2693 Lisp_Object overlay;
2695 overlay = overlay_vec[i];
2696 if (OVERLAY_VALID (overlay)
2697 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2698 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2700 /* If we're interested in a specific window, then ignore
2701 overlays that are limited to some other window. */
2702 if (w)
2704 Lisp_Object window;
2706 window = Foverlay_get (overlay, Qwindow);
2707 if (WINDOWP (window) && XWINDOW (window) != w)
2708 continue;
2711 /* This overlay is good and counts: put it into sortvec. */
2712 sortvec[j].overlay = overlay;
2713 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2714 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2715 tem = Foverlay_get (overlay, Qpriority);
2716 if (INTEGERP (tem))
2717 sortvec[j].priority = XINT (tem);
2718 else
2719 sortvec[j].priority = 0;
2720 j++;
2723 noverlays = j;
2725 /* Sort the overlays into the proper order: increasing priority. */
2727 if (noverlays > 1)
2728 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2730 for (i = 0; i < noverlays; i++)
2731 overlay_vec[i] = sortvec[i].overlay;
2732 return (noverlays);
2735 struct sortstr
2737 Lisp_Object string, string2;
2738 int size;
2739 int priority;
2742 struct sortstrlist
2744 struct sortstr *buf; /* An array that expands as needed; never freed. */
2745 int size; /* Allocated length of that array. */
2746 int used; /* How much of the array is currently in use. */
2747 int bytes; /* Total length of the strings in buf. */
2750 /* Buffers for storing information about the overlays touching a given
2751 position. These could be automatic variables in overlay_strings, but
2752 it's more efficient to hold onto the memory instead of repeatedly
2753 allocating and freeing it. */
2754 static struct sortstrlist overlay_heads, overlay_tails;
2755 static unsigned char *overlay_str_buf;
2757 /* Allocated length of overlay_str_buf. */
2758 static int overlay_str_len;
2760 /* A comparison function suitable for passing to qsort. */
2761 static int
2762 cmp_for_strings (as1, as2)
2763 char *as1, *as2;
2765 struct sortstr *s1 = (struct sortstr *)as1;
2766 struct sortstr *s2 = (struct sortstr *)as2;
2767 if (s1->size != s2->size)
2768 return s2->size - s1->size;
2769 if (s1->priority != s2->priority)
2770 return s1->priority - s2->priority;
2771 return 0;
2774 static void
2775 record_overlay_string (ssl, str, str2, pri, size)
2776 struct sortstrlist *ssl;
2777 Lisp_Object str, str2, pri;
2778 int size;
2780 int nbytes;
2782 if (ssl->used == ssl->size)
2784 if (ssl->buf)
2785 ssl->size *= 2;
2786 else
2787 ssl->size = 5;
2788 ssl->buf = ((struct sortstr *)
2789 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2791 ssl->buf[ssl->used].string = str;
2792 ssl->buf[ssl->used].string2 = str2;
2793 ssl->buf[ssl->used].size = size;
2794 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2795 ssl->used++;
2797 if (NILP (current_buffer->enable_multibyte_characters))
2798 nbytes = XSTRING (str)->size;
2799 else if (! STRING_MULTIBYTE (str))
2800 nbytes = count_size_as_multibyte (XSTRING (str)->data,
2801 STRING_BYTES (XSTRING (str)));
2802 else
2803 nbytes = STRING_BYTES (XSTRING (str));
2805 ssl->bytes += nbytes;
2807 if (STRINGP (str2))
2809 if (NILP (current_buffer->enable_multibyte_characters))
2810 nbytes = XSTRING (str2)->size;
2811 else if (! STRING_MULTIBYTE (str2))
2812 nbytes = count_size_as_multibyte (XSTRING (str2)->data,
2813 STRING_BYTES (XSTRING (str2)));
2814 else
2815 nbytes = STRING_BYTES (XSTRING (str2));
2817 ssl->bytes += nbytes;
2821 /* Return the concatenation of the strings associated with overlays that
2822 begin or end at POS, ignoring overlays that are specific to a window
2823 other than W. The strings are concatenated in the appropriate order:
2824 shorter overlays nest inside longer ones, and higher priority inside
2825 lower. Normally all of the after-strings come first, but zero-sized
2826 overlays have their after-strings ride along with the before-strings
2827 because it would look strange to print them inside-out.
2829 Returns the string length, and stores the contents indirectly through
2830 PSTR, if that variable is non-null. The string may be overwritten by
2831 subsequent calls. */
2834 overlay_strings (pos, w, pstr)
2835 int pos;
2836 struct window *w;
2837 unsigned char **pstr;
2839 Lisp_Object ov, overlay, window, str;
2840 int startpos, endpos;
2841 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
2843 overlay_heads.used = overlay_heads.bytes = 0;
2844 overlay_tails.used = overlay_tails.bytes = 0;
2845 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
2847 overlay = XCAR (ov);
2848 if (!OVERLAYP (overlay))
2849 abort ();
2851 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2852 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2853 if (endpos < pos)
2854 break;
2855 if (endpos != pos && startpos != pos)
2856 continue;
2857 window = Foverlay_get (overlay, Qwindow);
2858 if (WINDOWP (window) && XWINDOW (window) != w)
2859 continue;
2860 if (startpos == pos
2861 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2862 record_overlay_string (&overlay_heads, str,
2863 (startpos == endpos
2864 ? Foverlay_get (overlay, Qafter_string)
2865 : Qnil),
2866 Foverlay_get (overlay, Qpriority),
2867 endpos - startpos);
2868 else if (endpos == pos
2869 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2870 record_overlay_string (&overlay_tails, str, Qnil,
2871 Foverlay_get (overlay, Qpriority),
2872 endpos - startpos);
2874 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
2876 overlay = XCAR (ov);
2877 if (!OVERLAYP (overlay))
2878 abort ();
2880 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2881 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2882 if (startpos > pos)
2883 break;
2884 if (endpos != pos && startpos != pos)
2885 continue;
2886 window = Foverlay_get (overlay, Qwindow);
2887 if (WINDOWP (window) && XWINDOW (window) != w)
2888 continue;
2889 if (startpos == pos
2890 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2891 record_overlay_string (&overlay_heads, str,
2892 (startpos == endpos
2893 ? Foverlay_get (overlay, Qafter_string)
2894 : Qnil),
2895 Foverlay_get (overlay, Qpriority),
2896 endpos - startpos);
2897 else if (endpos == pos
2898 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2899 record_overlay_string (&overlay_tails, str, Qnil,
2900 Foverlay_get (overlay, Qpriority),
2901 endpos - startpos);
2903 if (overlay_tails.used > 1)
2904 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2905 cmp_for_strings);
2906 if (overlay_heads.used > 1)
2907 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2908 cmp_for_strings);
2909 if (overlay_heads.bytes || overlay_tails.bytes)
2911 Lisp_Object tem;
2912 int i;
2913 unsigned char *p;
2914 int total = overlay_heads.bytes + overlay_tails.bytes;
2916 if (total > overlay_str_len)
2918 overlay_str_len = total;
2919 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2920 total);
2922 p = overlay_str_buf;
2923 for (i = overlay_tails.used; --i >= 0;)
2925 int nbytes;
2926 tem = overlay_tails.buf[i].string;
2927 nbytes = copy_text (XSTRING (tem)->data, p,
2928 STRING_BYTES (XSTRING (tem)),
2929 STRING_MULTIBYTE (tem), multibyte);
2930 p += nbytes;
2932 for (i = 0; i < overlay_heads.used; ++i)
2934 int nbytes;
2935 tem = overlay_heads.buf[i].string;
2936 nbytes = copy_text (XSTRING (tem)->data, p,
2937 STRING_BYTES (XSTRING (tem)),
2938 STRING_MULTIBYTE (tem), multibyte);
2939 p += nbytes;
2940 tem = overlay_heads.buf[i].string2;
2941 if (STRINGP (tem))
2943 nbytes = copy_text (XSTRING (tem)->data, p,
2944 STRING_BYTES (XSTRING (tem)),
2945 STRING_MULTIBYTE (tem), multibyte);
2946 p += nbytes;
2949 if (p != overlay_str_buf + total)
2950 abort ();
2951 if (pstr)
2952 *pstr = overlay_str_buf;
2953 return total;
2955 return 0;
2958 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2960 void
2961 recenter_overlay_lists (buf, pos)
2962 struct buffer *buf;
2963 int pos;
2965 Lisp_Object overlay, tail, next, prev, beg, end;
2967 /* See if anything in overlays_before should move to overlays_after. */
2969 /* We don't strictly need prev in this loop; it should always be nil.
2970 But we use it for symmetry and in case that should cease to be true
2971 with some future change. */
2972 prev = Qnil;
2973 for (tail = buf->overlays_before;
2974 CONSP (tail);
2975 prev = tail, tail = next)
2977 next = XCDR (tail);
2978 overlay = XCAR (tail);
2980 /* If the overlay is not valid, get rid of it. */
2981 if (!OVERLAY_VALID (overlay))
2982 #if 1
2983 abort ();
2984 #else
2986 /* Splice the cons cell TAIL out of overlays_before. */
2987 if (!NILP (prev))
2988 XCDR (prev) = next;
2989 else
2990 buf->overlays_before = next;
2991 tail = prev;
2992 continue;
2994 #endif
2996 beg = OVERLAY_START (overlay);
2997 end = OVERLAY_END (overlay);
2999 if (OVERLAY_POSITION (end) > pos)
3001 /* OVERLAY needs to be moved. */
3002 int where = OVERLAY_POSITION (beg);
3003 Lisp_Object other, other_prev;
3005 /* Splice the cons cell TAIL out of overlays_before. */
3006 if (!NILP (prev))
3007 XCDR (prev) = next;
3008 else
3009 buf->overlays_before = next;
3011 /* Search thru overlays_after for where to put it. */
3012 other_prev = Qnil;
3013 for (other = buf->overlays_after;
3014 CONSP (other);
3015 other_prev = other, other = XCDR (other))
3017 Lisp_Object otherbeg, otheroverlay;
3019 otheroverlay = XCAR (other);
3020 if (! OVERLAY_VALID (otheroverlay))
3021 abort ();
3023 otherbeg = OVERLAY_START (otheroverlay);
3024 if (OVERLAY_POSITION (otherbeg) >= where)
3025 break;
3028 /* Add TAIL to overlays_after before OTHER. */
3029 XCDR (tail) = other;
3030 if (!NILP (other_prev))
3031 XCDR (other_prev) = tail;
3032 else
3033 buf->overlays_after = tail;
3034 tail = prev;
3036 else
3037 /* We've reached the things that should stay in overlays_before.
3038 All the rest of overlays_before must end even earlier,
3039 so stop now. */
3040 break;
3043 /* See if anything in overlays_after should be in overlays_before. */
3044 prev = Qnil;
3045 for (tail = buf->overlays_after;
3046 CONSP (tail);
3047 prev = tail, tail = next)
3049 next = XCDR (tail);
3050 overlay = XCAR (tail);
3052 /* If the overlay is not valid, get rid of it. */
3053 if (!OVERLAY_VALID (overlay))
3054 #if 1
3055 abort ();
3056 #else
3058 /* Splice the cons cell TAIL out of overlays_after. */
3059 if (!NILP (prev))
3060 XCDR (prev) = next;
3061 else
3062 buf->overlays_after = next;
3063 tail = prev;
3064 continue;
3066 #endif
3068 beg = OVERLAY_START (overlay);
3069 end = OVERLAY_END (overlay);
3071 /* Stop looking, when we know that nothing further
3072 can possibly end before POS. */
3073 if (OVERLAY_POSITION (beg) > pos)
3074 break;
3076 if (OVERLAY_POSITION (end) <= pos)
3078 /* OVERLAY needs to be moved. */
3079 int where = OVERLAY_POSITION (end);
3080 Lisp_Object other, other_prev;
3082 /* Splice the cons cell TAIL out of overlays_after. */
3083 if (!NILP (prev))
3084 XCDR (prev) = next;
3085 else
3086 buf->overlays_after = next;
3088 /* Search thru overlays_before for where to put it. */
3089 other_prev = Qnil;
3090 for (other = buf->overlays_before;
3091 CONSP (other);
3092 other_prev = other, other = XCDR (other))
3094 Lisp_Object otherend, otheroverlay;
3096 otheroverlay = XCAR (other);
3097 if (! OVERLAY_VALID (otheroverlay))
3098 abort ();
3100 otherend = OVERLAY_END (otheroverlay);
3101 if (OVERLAY_POSITION (otherend) <= where)
3102 break;
3105 /* Add TAIL to overlays_before before OTHER. */
3106 XCDR (tail) = other;
3107 if (!NILP (other_prev))
3108 XCDR (other_prev) = tail;
3109 else
3110 buf->overlays_before = tail;
3111 tail = prev;
3115 XSETFASTINT (buf->overlay_center, pos);
3118 void
3119 adjust_overlays_for_insert (pos, length)
3120 int pos;
3121 int length;
3123 /* After an insertion, the lists are still sorted properly,
3124 but we may need to update the value of the overlay center. */
3125 if (XFASTINT (current_buffer->overlay_center) >= pos)
3126 XSETFASTINT (current_buffer->overlay_center,
3127 XFASTINT (current_buffer->overlay_center) + length);
3130 void
3131 adjust_overlays_for_delete (pos, length)
3132 int pos;
3133 int length;
3135 if (XFASTINT (current_buffer->overlay_center) < pos)
3136 /* The deletion was to our right. No change needed; the before- and
3137 after-lists are still consistent. */
3139 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
3140 /* The deletion was to our left. We need to adjust the center value
3141 to account for the change in position, but the lists are consistent
3142 given the new value. */
3143 XSETFASTINT (current_buffer->overlay_center,
3144 XFASTINT (current_buffer->overlay_center) - length);
3145 else
3146 /* We're right in the middle. There might be things on the after-list
3147 that now belong on the before-list. Recentering will move them,
3148 and also update the center point. */
3149 recenter_overlay_lists (current_buffer, pos);
3152 /* Fix up overlays that were garbled as a result of permuting markers
3153 in the range START through END. Any overlay with at least one
3154 endpoint in this range will need to be unlinked from the overlay
3155 list and reinserted in its proper place.
3156 Such an overlay might even have negative size at this point.
3157 If so, we'll reverse the endpoints. Can you think of anything
3158 better to do in this situation? */
3159 void
3160 fix_overlays_in_range (start, end)
3161 register int start, end;
3163 Lisp_Object overlay;
3164 Lisp_Object before_list, after_list;
3165 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
3166 int startpos, endpos;
3168 /* This algorithm shifts links around instead of consing and GCing.
3169 The loop invariant is that before_list (resp. after_list) is a
3170 well-formed list except that its last element, the one that
3171 *pbefore (resp. *pafter) points to, is still uninitialized.
3172 So it's not a bug that before_list isn't initialized, although
3173 it may look strange. */
3174 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
3176 overlay = XCAR (*ptail);
3177 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3178 if (endpos < start)
3179 break;
3180 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3181 if (endpos < end
3182 || (startpos >= start && startpos < end))
3184 /* If the overlay is backwards, fix that now. */
3185 if (startpos > endpos)
3187 int tem;
3188 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3189 Qnil);
3190 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3191 Qnil);
3192 tem = startpos; startpos = endpos; endpos = tem;
3194 /* Add it to the end of the wrong list. Later on,
3195 recenter_overlay_lists will move it to the right place. */
3196 if (endpos < XINT (current_buffer->overlay_center))
3198 *pafter = *ptail;
3199 pafter = &XCDR (*ptail);
3201 else
3203 *pbefore = *ptail;
3204 pbefore = &XCDR (*ptail);
3206 *ptail = XCDR (*ptail);
3208 else
3209 ptail = &XCDR (*ptail);
3211 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
3213 overlay = XCAR (*ptail);
3214 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3215 if (startpos >= end)
3216 break;
3217 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3218 if (startpos >= start
3219 || (endpos >= start && endpos < end))
3221 if (startpos > endpos)
3223 int tem;
3224 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3225 Qnil);
3226 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3227 Qnil);
3228 tem = startpos; startpos = endpos; endpos = tem;
3230 if (endpos < XINT (current_buffer->overlay_center))
3232 *pafter = *ptail;
3233 pafter = &XCDR (*ptail);
3235 else
3237 *pbefore = *ptail;
3238 pbefore = &XCDR (*ptail);
3240 *ptail = XCDR (*ptail);
3242 else
3243 ptail = &XCDR (*ptail);
3246 /* Splice the constructed (wrong) lists into the buffer's lists,
3247 and let the recenter function make it sane again. */
3248 *pbefore = current_buffer->overlays_before;
3249 current_buffer->overlays_before = before_list;
3250 recenter_overlay_lists (current_buffer,
3251 XINT (current_buffer->overlay_center));
3253 *pafter = current_buffer->overlays_after;
3254 current_buffer->overlays_after = after_list;
3255 recenter_overlay_lists (current_buffer,
3256 XINT (current_buffer->overlay_center));
3259 /* We have two types of overlay: the one whose ending marker is
3260 after-insertion-marker (this is the usual case) and the one whose
3261 ending marker is before-insertion-marker. When `overlays_before'
3262 contains overlays of the latter type and the former type in this
3263 order and both overlays end at inserting position, inserting a text
3264 increases only the ending marker of the latter type, which results
3265 in incorrect ordering of `overlays_before'.
3267 This function fixes ordering of overlays in the slot
3268 `overlays_before' of the buffer *BP. Before the insertion, `point'
3269 was at PREV, and now is at POS. */
3271 void
3272 fix_overlays_before (bp, prev, pos)
3273 struct buffer *bp;
3274 int prev, pos;
3276 Lisp_Object *tailp = &bp->overlays_before;
3277 Lisp_Object *right_place;
3278 int end;
3280 /* After the insertion, the several overlays may be in incorrect
3281 order. The possibility is that, in the list `overlays_before',
3282 an overlay which ends at POS appears after an overlay which ends
3283 at PREV. Since POS is greater than PREV, we must fix the
3284 ordering of these overlays, by moving overlays ends at POS before
3285 the overlays ends at PREV. */
3287 /* At first, find a place where disordered overlays should be linked
3288 in. It is where an overlay which end before POS exists. (i.e. an
3289 overlay whose ending marker is after-insertion-marker if disorder
3290 exists). */
3291 while (!NILP (*tailp)
3292 && ((end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp))))
3293 >= pos))
3294 tailp = &XCDR (*tailp);
3296 /* If we don't find such an overlay,
3297 or the found one ends before PREV,
3298 or the found one is the last one in the list,
3299 we don't have to fix anything. */
3300 if (NILP (*tailp)
3301 || end < prev
3302 || NILP (XCDR (*tailp)))
3303 return;
3305 right_place = tailp;
3306 tailp = &XCDR (*tailp);
3308 /* Now, end position of overlays in the list *TAILP should be before
3309 or equal to PREV. In the loop, an overlay which ends at POS is
3310 moved ahead to the place pointed by RIGHT_PLACE. If we found an
3311 overlay which ends before PREV, the remaining overlays are in
3312 correct order. */
3313 while (!NILP (*tailp))
3315 end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp)));
3317 if (end == pos)
3318 { /* This overlay is disordered. */
3319 Lisp_Object found = *tailp;
3321 /* Unlink the found overlay. */
3322 *tailp = XCDR (found);
3323 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
3324 XCDR (found) = *right_place;
3325 /* Link it into the right place. */
3326 *right_place = found;
3328 else if (end == prev)
3329 tailp = &XCDR (*tailp);
3330 else /* No more disordered overlay. */
3331 break;
3335 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3336 "Return t if OBJECT is an overlay.")
3337 (object)
3338 Lisp_Object object;
3340 return (OVERLAYP (object) ? Qt : Qnil);
3343 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3344 "Create a new overlay with range BEG to END in BUFFER.\n\
3345 If omitted, BUFFER defaults to the current buffer.\n\
3346 BEG and END may be integers or markers.\n\
3347 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
3348 front delimiter advance when text is inserted there.\n\
3349 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
3350 rear delimiter advance when text is inserted there.")
3351 (beg, end, buffer, front_advance, rear_advance)
3352 Lisp_Object beg, end, buffer;
3353 Lisp_Object front_advance, rear_advance;
3355 Lisp_Object overlay;
3356 struct buffer *b;
3358 if (NILP (buffer))
3359 XSETBUFFER (buffer, current_buffer);
3360 else
3361 CHECK_BUFFER (buffer, 2);
3362 if (MARKERP (beg)
3363 && ! EQ (Fmarker_buffer (beg), buffer))
3364 error ("Marker points into wrong buffer");
3365 if (MARKERP (end)
3366 && ! EQ (Fmarker_buffer (end), buffer))
3367 error ("Marker points into wrong buffer");
3369 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3370 CHECK_NUMBER_COERCE_MARKER (end, 1);
3372 if (XINT (beg) > XINT (end))
3374 Lisp_Object temp;
3375 temp = beg; beg = end; end = temp;
3378 b = XBUFFER (buffer);
3380 beg = Fset_marker (Fmake_marker (), beg, buffer);
3381 end = Fset_marker (Fmake_marker (), end, buffer);
3383 if (!NILP (front_advance))
3384 XMARKER (beg)->insertion_type = 1;
3385 if (!NILP (rear_advance))
3386 XMARKER (end)->insertion_type = 1;
3388 overlay = allocate_misc ();
3389 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3390 XOVERLAY (overlay)->start = beg;
3391 XOVERLAY (overlay)->end = end;
3392 XOVERLAY (overlay)->plist = Qnil;
3394 /* Put the new overlay on the wrong list. */
3395 end = OVERLAY_END (overlay);
3396 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3397 b->overlays_after = Fcons (overlay, b->overlays_after);
3398 else
3399 b->overlays_before = Fcons (overlay, b->overlays_before);
3401 /* This puts it in the right list, and in the right order. */
3402 recenter_overlay_lists (b, XINT (b->overlay_center));
3404 /* We don't need to redisplay the region covered by the overlay, because
3405 the overlay has no properties at the moment. */
3407 return overlay;
3410 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3412 static void
3413 modify_overlay (buf, start, end)
3414 struct buffer *buf;
3415 int start, end;
3417 if (start > end)
3419 int temp = start;
3420 start = end;
3421 end = temp;
3424 BUF_COMPUTE_UNCHANGED (buf, start, end);
3426 /* If this is a buffer not in the selected window,
3427 we must do other windows. */
3428 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3429 windows_or_buffers_changed = 1;
3430 /* If multiple windows show this buffer, we must do other windows. */
3431 else if (buffer_shared > 1)
3432 windows_or_buffers_changed = 1;
3434 ++BUF_OVERLAY_MODIFF (buf);
3437 \f\f
3438 Lisp_Object Fdelete_overlay ();
3440 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3441 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3442 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3443 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3444 buffer.")
3445 (overlay, beg, end, buffer)
3446 Lisp_Object overlay, beg, end, buffer;
3448 struct buffer *b, *ob;
3449 Lisp_Object obuffer;
3450 int count = specpdl_ptr - specpdl;
3452 CHECK_OVERLAY (overlay, 0);
3453 if (NILP (buffer))
3454 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3455 if (NILP (buffer))
3456 XSETBUFFER (buffer, current_buffer);
3457 CHECK_BUFFER (buffer, 3);
3459 if (MARKERP (beg)
3460 && ! EQ (Fmarker_buffer (beg), buffer))
3461 error ("Marker points into wrong buffer");
3462 if (MARKERP (end)
3463 && ! EQ (Fmarker_buffer (end), buffer))
3464 error ("Marker points into wrong buffer");
3466 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3467 CHECK_NUMBER_COERCE_MARKER (end, 1);
3469 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3470 return Fdelete_overlay (overlay);
3472 if (XINT (beg) > XINT (end))
3474 Lisp_Object temp;
3475 temp = beg; beg = end; end = temp;
3478 specbind (Qinhibit_quit, Qt);
3480 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3481 b = XBUFFER (buffer);
3482 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3484 /* If the overlay has changed buffers, do a thorough redisplay. */
3485 if (!EQ (buffer, obuffer))
3487 /* Redisplay where the overlay was. */
3488 if (!NILP (obuffer))
3490 int o_beg;
3491 int o_end;
3493 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3494 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3496 modify_overlay (ob, o_beg, o_end);
3499 /* Redisplay where the overlay is going to be. */
3500 modify_overlay (b, XINT (beg), XINT (end));
3502 else
3503 /* Redisplay the area the overlay has just left, or just enclosed. */
3505 int o_beg, o_end;
3507 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3508 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3510 if (o_beg == XINT (beg))
3511 modify_overlay (b, o_end, XINT (end));
3512 else if (o_end == XINT (end))
3513 modify_overlay (b, o_beg, XINT (beg));
3514 else
3516 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3517 if (XINT (end) > o_end) o_end = XINT (end);
3518 modify_overlay (b, o_beg, o_end);
3522 if (!NILP (obuffer))
3524 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
3525 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
3528 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3529 Fset_marker (OVERLAY_END (overlay), end, buffer);
3531 /* Put the overlay on the wrong list. */
3532 end = OVERLAY_END (overlay);
3533 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3534 b->overlays_after = Fcons (overlay, b->overlays_after);
3535 else
3536 b->overlays_before = Fcons (overlay, b->overlays_before);
3538 /* This puts it in the right list, and in the right order. */
3539 recenter_overlay_lists (b, XINT (b->overlay_center));
3541 return unbind_to (count, overlay);
3544 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3545 "Delete the overlay OVERLAY from its buffer.")
3546 (overlay)
3547 Lisp_Object overlay;
3549 Lisp_Object buffer;
3550 struct buffer *b;
3551 int count = specpdl_ptr - specpdl;
3553 CHECK_OVERLAY (overlay, 0);
3555 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3556 if (NILP (buffer))
3557 return Qnil;
3559 b = XBUFFER (buffer);
3560 specbind (Qinhibit_quit, Qt);
3562 b->overlays_before = Fdelq (overlay, b->overlays_before);
3563 b->overlays_after = Fdelq (overlay, b->overlays_after);
3564 modify_overlay (b,
3565 marker_position (OVERLAY_START (overlay)),
3566 marker_position (OVERLAY_END (overlay)));
3567 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3568 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3570 /* When deleting an overlay with before or after strings, turn off
3571 display optimizations for the affected buffer, on the basis that
3572 these strings may contain newlines. This is easier to do than to
3573 check for that situation during redisplay. */
3574 if (!windows_or_buffers_changed
3575 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3576 || !NILP (Foverlay_get (overlay, Qafter_string))))
3577 b->prevent_redisplay_optimizations_p = 1;
3579 return unbind_to (count, Qnil);
3582 /* Overlay dissection functions. */
3584 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3585 "Return the position at which OVERLAY starts.")
3586 (overlay)
3587 Lisp_Object overlay;
3589 CHECK_OVERLAY (overlay, 0);
3591 return (Fmarker_position (OVERLAY_START (overlay)));
3594 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3595 "Return the position at which OVERLAY ends.")
3596 (overlay)
3597 Lisp_Object overlay;
3599 CHECK_OVERLAY (overlay, 0);
3601 return (Fmarker_position (OVERLAY_END (overlay)));
3604 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3605 "Return the buffer OVERLAY belongs to.")
3606 (overlay)
3607 Lisp_Object overlay;
3609 CHECK_OVERLAY (overlay, 0);
3611 return Fmarker_buffer (OVERLAY_START (overlay));
3614 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3615 "Return a list of the properties on OVERLAY.\n\
3616 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3617 OVERLAY.")
3618 (overlay)
3619 Lisp_Object overlay;
3621 CHECK_OVERLAY (overlay, 0);
3623 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3627 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3628 "Return a list of the overlays that contain position POS.")
3629 (pos)
3630 Lisp_Object pos;
3632 int noverlays;
3633 Lisp_Object *overlay_vec;
3634 int len;
3635 Lisp_Object result;
3637 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3639 len = 10;
3640 /* We can't use alloca here because overlays_at can call xrealloc. */
3641 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3643 /* Put all the overlays we want in a vector in overlay_vec.
3644 Store the length in len. */
3645 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3646 (int *) 0, (int *) 0, 0);
3648 /* Make a list of them all. */
3649 result = Flist (noverlays, overlay_vec);
3651 xfree (overlay_vec);
3652 return result;
3655 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3656 "Return a list of the overlays that overlap the region BEG ... END.\n\
3657 Overlap means that at least one character is contained within the overlay\n\
3658 and also contained within the specified region.\n\
3659 Empty overlays are included in the result if they are located at BEG\n\
3660 or between BEG and END.")
3661 (beg, end)
3662 Lisp_Object beg, end;
3664 int noverlays;
3665 Lisp_Object *overlay_vec;
3666 int len;
3667 Lisp_Object result;
3669 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3670 CHECK_NUMBER_COERCE_MARKER (end, 0);
3672 len = 10;
3673 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3675 /* Put all the overlays we want in a vector in overlay_vec.
3676 Store the length in len. */
3677 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3678 (int *) 0, (int *) 0);
3680 /* Make a list of them all. */
3681 result = Flist (noverlays, overlay_vec);
3683 xfree (overlay_vec);
3684 return result;
3687 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3688 1, 1, 0,
3689 "Return the next position after POS where an overlay starts or ends.\n\
3690 If there are no more overlay boundaries after POS, return (point-max).")
3691 (pos)
3692 Lisp_Object pos;
3694 int noverlays;
3695 int endpos;
3696 Lisp_Object *overlay_vec;
3697 int len;
3698 int i;
3700 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3702 len = 10;
3703 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3705 /* Put all the overlays we want in a vector in overlay_vec.
3706 Store the length in len.
3707 endpos gets the position where the next overlay starts. */
3708 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3709 &endpos, (int *) 0, 1);
3711 /* If any of these overlays ends before endpos,
3712 use its ending point instead. */
3713 for (i = 0; i < noverlays; i++)
3715 Lisp_Object oend;
3716 int oendpos;
3718 oend = OVERLAY_END (overlay_vec[i]);
3719 oendpos = OVERLAY_POSITION (oend);
3720 if (oendpos < endpos)
3721 endpos = oendpos;
3724 xfree (overlay_vec);
3725 return make_number (endpos);
3728 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3729 Sprevious_overlay_change, 1, 1, 0,
3730 "Return the previous position before POS where an overlay starts or ends.\n\
3731 If there are no more overlay boundaries before POS, return (point-min).")
3732 (pos)
3733 Lisp_Object pos;
3735 int noverlays;
3736 int prevpos;
3737 Lisp_Object *overlay_vec;
3738 int len;
3740 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3742 /* At beginning of buffer, we know the answer;
3743 avoid bug subtracting 1 below. */
3744 if (XINT (pos) == BEGV)
3745 return pos;
3747 len = 10;
3748 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3750 /* Put all the overlays we want in a vector in overlay_vec.
3751 Store the length in len.
3752 prevpos gets the position of the previous change. */
3753 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3754 (int *) 0, &prevpos, 1);
3756 xfree (overlay_vec);
3757 return make_number (prevpos);
3760 /* These functions are for debugging overlays. */
3762 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3763 "Return a pair of lists giving all the overlays of the current buffer.\n\
3764 The car has all the overlays before the overlay center;\n\
3765 the cdr has all the overlays after the overlay center.\n\
3766 Recentering overlays moves overlays between these lists.\n\
3767 The lists you get are copies, so that changing them has no effect.\n\
3768 However, the overlays you get are the real objects that the buffer uses.")
3771 Lisp_Object before, after;
3772 before = current_buffer->overlays_before;
3773 if (CONSP (before))
3774 before = Fcopy_sequence (before);
3775 after = current_buffer->overlays_after;
3776 if (CONSP (after))
3777 after = Fcopy_sequence (after);
3779 return Fcons (before, after);
3782 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3783 "Recenter the overlays of the current buffer around position POS.")
3784 (pos)
3785 Lisp_Object pos;
3787 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3789 recenter_overlay_lists (current_buffer, XINT (pos));
3790 return Qnil;
3793 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3794 "Get the property of overlay OVERLAY with property name PROP.")
3795 (overlay, prop)
3796 Lisp_Object overlay, prop;
3798 Lisp_Object plist, fallback;
3800 CHECK_OVERLAY (overlay, 0);
3802 fallback = Qnil;
3804 for (plist = XOVERLAY (overlay)->plist;
3805 CONSP (plist) && CONSP (XCDR (plist));
3806 plist = XCDR (XCDR (plist)))
3808 if (EQ (XCAR (plist), prop))
3809 return XCAR (XCDR (plist));
3810 else if (EQ (XCAR (plist), Qcategory))
3812 Lisp_Object tem;
3813 tem = Fcar (Fcdr (plist));
3814 if (SYMBOLP (tem))
3815 fallback = Fget (tem, prop);
3819 return fallback;
3822 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3823 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3824 (overlay, prop, value)
3825 Lisp_Object overlay, prop, value;
3827 Lisp_Object tail, buffer;
3828 int changed;
3830 CHECK_OVERLAY (overlay, 0);
3832 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3834 for (tail = XOVERLAY (overlay)->plist;
3835 CONSP (tail) && CONSP (XCDR (tail));
3836 tail = XCDR (XCDR (tail)))
3837 if (EQ (XCAR (tail), prop))
3839 changed = !EQ (XCAR (XCDR (tail)), value);
3840 XCAR (XCDR (tail)) = value;
3841 goto found;
3843 /* It wasn't in the list, so add it to the front. */
3844 changed = !NILP (value);
3845 XOVERLAY (overlay)->plist
3846 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3847 found:
3848 if (! NILP (buffer))
3850 if (changed)
3851 modify_overlay (XBUFFER (buffer),
3852 marker_position (OVERLAY_START (overlay)),
3853 marker_position (OVERLAY_END (overlay)));
3854 if (EQ (prop, Qevaporate) && ! NILP (value)
3855 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3856 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3857 Fdelete_overlay (overlay);
3859 return value;
3862 /* Subroutine of report_overlay_modification. */
3864 /* Lisp vector holding overlay hook functions to call.
3865 Vector elements come in pairs.
3866 Each even-index element is a list of hook functions.
3867 The following odd-index element is the overlay they came from.
3869 Before the buffer change, we fill in this vector
3870 as we call overlay hook functions.
3871 After the buffer change, we get the functions to call from this vector.
3872 This way we always call the same functions before and after the change. */
3873 static Lisp_Object last_overlay_modification_hooks;
3875 /* Number of elements actually used in last_overlay_modification_hooks. */
3876 static int last_overlay_modification_hooks_used;
3878 /* Add one functionlist/overlay pair
3879 to the end of last_overlay_modification_hooks. */
3881 static void
3882 add_overlay_mod_hooklist (functionlist, overlay)
3883 Lisp_Object functionlist, overlay;
3885 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3887 if (last_overlay_modification_hooks_used == oldsize)
3889 Lisp_Object old;
3890 old = last_overlay_modification_hooks;
3891 last_overlay_modification_hooks
3892 = Fmake_vector (make_number (oldsize * 2), Qnil);
3893 bcopy (XVECTOR (old)->contents,
3894 XVECTOR (last_overlay_modification_hooks)->contents,
3895 sizeof (Lisp_Object) * oldsize);
3897 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3898 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3901 /* Run the modification-hooks of overlays that include
3902 any part of the text in START to END.
3903 If this change is an insertion, also
3904 run the insert-before-hooks of overlay starting at END,
3905 and the insert-after-hooks of overlay ending at START.
3907 This is called both before and after the modification.
3908 AFTER is nonzero when we call after the modification.
3910 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3911 When AFTER is nonzero, they are the start position,
3912 the position after the inserted new text,
3913 and the length of deleted or replaced old text. */
3915 void
3916 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3917 Lisp_Object start, end;
3918 int after;
3919 Lisp_Object arg1, arg2, arg3;
3921 Lisp_Object prop, overlay, tail;
3922 /* 1 if this change is an insertion. */
3923 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3924 int tail_copied;
3925 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3927 overlay = Qnil;
3928 tail = Qnil;
3929 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3931 if (after)
3933 /* Call the functions recorded in last_overlay_modification_hooks
3934 rather than scanning the overlays again.
3935 First copy the vector contents, in case some of these hooks
3936 do subsequent modification of the buffer. */
3937 int size = last_overlay_modification_hooks_used;
3938 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3939 int i;
3941 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3942 copy, size * sizeof (Lisp_Object));
3943 gcpro1.var = copy;
3944 gcpro1.nvars = size;
3946 for (i = 0; i < size;)
3948 Lisp_Object prop, overlay;
3949 prop = copy[i++];
3950 overlay = copy[i++];
3951 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3953 UNGCPRO;
3954 return;
3957 /* We are being called before a change.
3958 Scan the overlays to find the functions to call. */
3959 last_overlay_modification_hooks_used = 0;
3960 tail_copied = 0;
3961 for (tail = current_buffer->overlays_before;
3962 CONSP (tail);
3963 tail = XCDR (tail))
3965 int startpos, endpos;
3966 Lisp_Object ostart, oend;
3968 overlay = XCAR (tail);
3970 ostart = OVERLAY_START (overlay);
3971 oend = OVERLAY_END (overlay);
3972 endpos = OVERLAY_POSITION (oend);
3973 if (XFASTINT (start) > endpos)
3974 break;
3975 startpos = OVERLAY_POSITION (ostart);
3976 if (insertion && (XFASTINT (start) == startpos
3977 || XFASTINT (end) == startpos))
3979 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3980 if (!NILP (prop))
3982 /* Copy TAIL in case the hook recenters the overlay lists. */
3983 if (!tail_copied)
3984 tail = Fcopy_sequence (tail);
3985 tail_copied = 1;
3986 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3989 if (insertion && (XFASTINT (start) == endpos
3990 || XFASTINT (end) == endpos))
3992 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3993 if (!NILP (prop))
3995 if (!tail_copied)
3996 tail = Fcopy_sequence (tail);
3997 tail_copied = 1;
3998 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4001 /* Test for intersecting intervals. This does the right thing
4002 for both insertion and deletion. */
4003 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4005 prop = Foverlay_get (overlay, Qmodification_hooks);
4006 if (!NILP (prop))
4008 if (!tail_copied)
4009 tail = Fcopy_sequence (tail);
4010 tail_copied = 1;
4011 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4016 tail_copied = 0;
4017 for (tail = current_buffer->overlays_after;
4018 CONSP (tail);
4019 tail = XCDR (tail))
4021 int startpos, endpos;
4022 Lisp_Object ostart, oend;
4024 overlay = XCAR (tail);
4026 ostart = OVERLAY_START (overlay);
4027 oend = OVERLAY_END (overlay);
4028 startpos = OVERLAY_POSITION (ostart);
4029 endpos = OVERLAY_POSITION (oend);
4030 if (XFASTINT (end) < startpos)
4031 break;
4032 if (insertion && (XFASTINT (start) == startpos
4033 || XFASTINT (end) == startpos))
4035 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4036 if (!NILP (prop))
4038 if (!tail_copied)
4039 tail = Fcopy_sequence (tail);
4040 tail_copied = 1;
4041 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4044 if (insertion && (XFASTINT (start) == endpos
4045 || XFASTINT (end) == endpos))
4047 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4048 if (!NILP (prop))
4050 if (!tail_copied)
4051 tail = Fcopy_sequence (tail);
4052 tail_copied = 1;
4053 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4056 /* Test for intersecting intervals. This does the right thing
4057 for both insertion and deletion. */
4058 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4060 prop = Foverlay_get (overlay, Qmodification_hooks);
4061 if (!NILP (prop))
4063 if (!tail_copied)
4064 tail = Fcopy_sequence (tail);
4065 tail_copied = 1;
4066 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4071 UNGCPRO;
4074 static void
4075 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4076 Lisp_Object list, overlay;
4077 int after;
4078 Lisp_Object arg1, arg2, arg3;
4080 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4082 GCPRO4 (list, arg1, arg2, arg3);
4083 if (! after)
4084 add_overlay_mod_hooklist (list, overlay);
4086 while (!NILP (list))
4088 if (NILP (arg3))
4089 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
4090 else
4091 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4092 list = Fcdr (list);
4094 UNGCPRO;
4097 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4098 property is set. */
4099 void
4100 evaporate_overlays (pos)
4101 int pos;
4103 Lisp_Object tail, overlay, hit_list;
4105 hit_list = Qnil;
4106 if (pos <= XFASTINT (current_buffer->overlay_center))
4107 for (tail = current_buffer->overlays_before; CONSP (tail);
4108 tail = XCDR (tail))
4110 int endpos;
4111 overlay = XCAR (tail);
4112 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4113 if (endpos < pos)
4114 break;
4115 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4116 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4117 hit_list = Fcons (overlay, hit_list);
4119 else
4120 for (tail = current_buffer->overlays_after; CONSP (tail);
4121 tail = XCDR (tail))
4123 int startpos;
4124 overlay = XCAR (tail);
4125 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4126 if (startpos > pos)
4127 break;
4128 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4129 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4130 hit_list = Fcons (overlay, hit_list);
4132 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4133 Fdelete_overlay (XCAR (hit_list));
4136 /* Somebody has tried to store a value with an unacceptable type
4137 in the slot with offset OFFSET. */
4139 void
4140 buffer_slot_type_mismatch (offset)
4141 int offset;
4143 Lisp_Object sym;
4144 char *type_name;
4146 switch (XINT (PER_BUFFER_TYPE (offset)))
4148 case Lisp_Int:
4149 type_name = "integers";
4150 break;
4152 case Lisp_String:
4153 type_name = "strings";
4154 break;
4156 case Lisp_Symbol:
4157 type_name = "symbols";
4158 break;
4160 default:
4161 abort ();
4164 sym = PER_BUFFER_SYMBOL (offset);
4165 error ("Only %s should be stored in the buffer-local variable %s",
4166 type_name, XSYMBOL (sym)->name->data);
4170 /***********************************************************************
4171 Allocation with mmap
4172 ***********************************************************************/
4174 #ifdef USE_MMAP_FOR_BUFFERS
4176 #include <sys/types.h>
4177 #include <sys/mman.h>
4179 #ifndef MAP_ANON
4180 #ifdef MAP_ANONYMOUS
4181 #define MAP_ANON MAP_ANONYMOUS
4182 #else
4183 #define MAP_ANON 0
4184 #endif
4185 #endif
4187 #ifndef MAP_FAILED
4188 #define MAP_FAILED ((void *) -1)
4189 #endif
4191 #include <stdio.h>
4192 #include <errno.h>
4194 #if MAP_ANON == 0
4195 #include <fcntl.h>
4196 #endif
4198 #include "coding.h"
4201 /* Memory is allocated in regions which are mapped using mmap(2).
4202 The current implementation lets the system select mapped
4203 addresses; we're not using MAP_FIXED in general, except when
4204 trying to enlarge regions.
4206 Each mapped region starts with a mmap_region structure, the user
4207 area starts after that structure, aligned to MEM_ALIGN.
4209 +-----------------------+
4210 | struct mmap_info + |
4211 | padding |
4212 +-----------------------+
4213 | user data |
4216 +-----------------------+ */
4218 struct mmap_region
4220 /* User-specified size. */
4221 size_t nbytes_specified;
4223 /* Number of bytes mapped */
4224 size_t nbytes_mapped;
4226 /* Pointer to the location holding the address of the memory
4227 allocated with the mmap'd block. The variable actually points
4228 after this structure. */
4229 POINTER_TYPE **var;
4231 /* Next and previous in list of all mmap'd regions. */
4232 struct mmap_region *next, *prev;
4235 /* Doubly-linked list of mmap'd regions. */
4237 static struct mmap_region *mmap_regions;
4239 /* File descriptor for mmap. If we don't have anonymous mapping,
4240 /dev/zero will be opened on it. */
4242 static int mmap_fd;
4244 /* Temporary storage for mmap_set_vars, see there. */
4246 static struct mmap_region *mmap_regions_1;
4247 static int mmap_fd_1;
4249 /* Page size on this system. */
4251 static int mmap_page_size;
4253 /* 1 means mmap has been intialized. */
4255 static int mmap_initialized_p;
4257 /* Value is X rounded up to the next multiple of N. */
4259 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4261 /* Size of mmap_region structure plus padding. */
4263 #define MMAP_REGION_STRUCT_SIZE \
4264 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4266 /* Given a pointer P to the start of the user-visible part of a mapped
4267 region, return a pointer to the start of the region. */
4269 #define MMAP_REGION(P) \
4270 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4272 /* Given a pointer P to the start of a mapped region, return a pointer
4273 to the start of the user-visible part of the region. */
4275 #define MMAP_USER_AREA(P) \
4276 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4278 #define MEM_ALIGN sizeof (double)
4280 /* Predicate returning true if part of the address range [START ..
4281 END[ is currently mapped. Used to prevent overwriting an existing
4282 memory mapping.
4284 Default is to conservativly assume the address range is occupied by
4285 something else. This can be overridden by system configuration
4286 files if system-specific means to determine this exists. */
4288 #ifndef MMAP_ALLOCATED_P
4289 #define MMAP_ALLOCATED_P(start, end) 1
4290 #endif
4292 /* Function prototypes. */
4294 static int mmap_free_1 P_ ((struct mmap_region *));
4295 static int mmap_enlarge P_ ((struct mmap_region *, int));
4296 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4297 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4298 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4299 static void mmap_free P_ ((POINTER_TYPE **ptr));
4300 static void mmap_init P_ ((void));
4303 /* Return a region overlapping address range START...END, or null if
4304 none. END is not including, i.e. the last byte in the range
4305 is at END - 1. */
4307 static struct mmap_region *
4308 mmap_find (start, end)
4309 POINTER_TYPE *start, *end;
4311 struct mmap_region *r;
4312 char *s = (char *) start, *e = (char *) end;
4314 for (r = mmap_regions; r; r = r->next)
4316 char *rstart = (char *) r;
4317 char *rend = rstart + r->nbytes_mapped;
4319 if (/* First byte of range, i.e. START, in this region? */
4320 (s >= rstart && s < rend)
4321 /* Last byte of range, i.e. END - 1, in this region? */
4322 || (e > rstart && e <= rend)
4323 /* First byte of this region in the range? */
4324 || (rstart >= s && rstart < e)
4325 /* Last byte of this region in the range? */
4326 || (rend > s && rend <= e))
4327 break;
4330 return r;
4334 /* Unmap a region. P is a pointer to the start of the user-araa of
4335 the region. Value is non-zero if successful. */
4337 static int
4338 mmap_free_1 (r)
4339 struct mmap_region *r;
4341 if (r->next)
4342 r->next->prev = r->prev;
4343 if (r->prev)
4344 r->prev->next = r->next;
4345 else
4346 mmap_regions = r->next;
4348 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4350 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4351 return 0;
4354 return 1;
4358 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4359 Value is non-zero if successful. */
4361 static int
4362 mmap_enlarge (r, npages)
4363 struct mmap_region *r;
4364 int npages;
4366 char *region_end = (char *) r + r->nbytes_mapped;
4367 size_t nbytes;
4368 int success = 0;
4370 if (npages < 0)
4372 /* Unmap pages at the end of the region. */
4373 nbytes = - npages * mmap_page_size;
4374 if (munmap (region_end - nbytes, nbytes) == -1)
4375 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4376 else
4378 r->nbytes_mapped -= nbytes;
4379 success = 1;
4382 else if (npages > 0)
4384 nbytes = npages * mmap_page_size;
4386 /* Try to map additional pages at the end of the region. We
4387 cannot do this if the address range is already occupied by
4388 something else because mmap deletes any previous mapping.
4389 I'm not sure this is worth doing, let's see. */
4390 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4392 POINTER_TYPE *p;
4394 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4395 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4396 if (p == MAP_FAILED)
4397 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4398 else if (p != (POINTER_TYPE *) region_end)
4400 /* Kernels are free to choose a different address. In
4401 that case, unmap what we've mapped above; we have
4402 no use for it. */
4403 if (munmap (p, nbytes) == -1)
4404 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4406 else
4408 r->nbytes_mapped += nbytes;
4409 success = 1;
4414 return success;
4418 /* Set or reset variables holding references to mapped regions. If
4419 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4420 non-zero, set all variables to the start of the user-areas
4421 of mapped regions.
4423 This function is called from Fdump_emacs to ensure that the dumped
4424 Emacs doesn't contain references to memory that won't be mapped
4425 when Emacs starts. */
4427 void
4428 mmap_set_vars (restore_p)
4429 int restore_p;
4431 struct mmap_region *r;
4433 if (restore_p)
4435 mmap_regions = mmap_regions_1;
4436 mmap_fd = mmap_fd_1;
4437 for (r = mmap_regions; r; r = r->next)
4438 *r->var = MMAP_USER_AREA (r);
4440 else
4442 for (r = mmap_regions; r; r = r->next)
4443 *r->var = NULL;
4444 mmap_regions_1 = mmap_regions;
4445 mmap_regions = NULL;
4446 mmap_fd_1 = mmap_fd;
4447 mmap_fd = -1;
4452 /* Allocate a block of storage large enough to hold NBYTES bytes of
4453 data. A pointer to the data is returned in *VAR. VAR is thus the
4454 address of some variable which will use the data area.
4456 The allocation of 0 bytes is valid.
4458 If we can't allocate the necessary memory, set *VAR to null, and
4459 return null. */
4461 static POINTER_TYPE *
4462 mmap_alloc (var, nbytes)
4463 POINTER_TYPE **var;
4464 size_t nbytes;
4466 void *p;
4467 size_t map;
4469 mmap_init ();
4471 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4472 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4473 mmap_fd, 0);
4475 if (p == MAP_FAILED)
4477 if (errno != ENOMEM)
4478 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4479 p = NULL;
4481 else
4483 struct mmap_region *r = (struct mmap_region *) p;
4485 r->nbytes_specified = nbytes;
4486 r->nbytes_mapped = map;
4487 r->var = var;
4488 r->prev = NULL;
4489 r->next = mmap_regions;
4490 if (r->next)
4491 r->next->prev = r;
4492 mmap_regions = r;
4494 p = MMAP_USER_AREA (p);
4497 return *var = p;
4501 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4502 resize it to size NBYTES. Change *VAR to reflect the new block,
4503 and return this value. If more memory cannot be allocated, then
4504 leave *VAR unchanged, and return null. */
4506 static POINTER_TYPE *
4507 mmap_realloc (var, nbytes)
4508 POINTER_TYPE **var;
4509 size_t nbytes;
4511 POINTER_TYPE *result;
4513 mmap_init ();
4515 if (*var == NULL)
4516 result = mmap_alloc (var, nbytes);
4517 else if (nbytes == 0)
4519 mmap_free (var);
4520 result = mmap_alloc (var, nbytes);
4522 else
4524 struct mmap_region *r = MMAP_REGION (*var);
4525 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4527 if (room < nbytes)
4529 /* Must enlarge. */
4530 POINTER_TYPE *old_ptr = *var;
4532 /* Try to map additional pages at the end of the region.
4533 If that fails, allocate a new region, copy data
4534 from the old region, then free it. */
4535 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4536 / mmap_page_size)))
4538 r->nbytes_specified = nbytes;
4539 *var = result = old_ptr;
4541 else if (mmap_alloc (var, nbytes))
4543 bcopy (old_ptr, *var, r->nbytes_specified);
4544 mmap_free_1 (MMAP_REGION (old_ptr));
4545 result = *var;
4546 r = MMAP_REGION (result);
4547 r->nbytes_specified = nbytes;
4549 else
4551 *var = old_ptr;
4552 result = NULL;
4555 else if (room - nbytes >= mmap_page_size)
4557 /* Shrinking by at least a page. Let's give some
4558 memory back to the system. */
4559 mmap_enlarge (r, - (room - nbytes) / mmap_page_size);
4560 result = *var;
4561 r->nbytes_specified = nbytes;
4563 else
4565 /* Leave it alone. */
4566 result = *var;
4567 r->nbytes_specified = nbytes;
4571 return result;
4575 /* Free a block of relocatable storage whose data is pointed to by
4576 PTR. Store 0 in *PTR to show there's no block allocated. */
4578 static void
4579 mmap_free (var)
4580 POINTER_TYPE **var;
4582 mmap_init ();
4584 if (*var)
4586 mmap_free_1 (MMAP_REGION (*var));
4587 *var = NULL;
4592 /* Perform necessary intializations for the use of mmap. */
4594 static void
4595 mmap_init ()
4597 #if MAP_ANON == 0
4598 /* The value of mmap_fd is initially 0 in temacs, and -1
4599 in a dumped Emacs. */
4600 if (mmap_fd <= 0)
4602 /* No anonymous mmap -- we need the file descriptor. */
4603 mmap_fd = open ("/dev/zero", O_RDONLY);
4604 if (mmap_fd == -1)
4605 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4607 #endif /* MAP_ANON == 0 */
4609 if (mmap_initialized_p)
4610 return;
4611 mmap_initialized_p = 1;
4613 #if MAP_ANON != 0
4614 mmap_fd = -1;
4615 #endif
4617 mmap_page_size = getpagesize ();
4620 #endif /* USE_MMAP_FOR_BUFFERS */
4624 /***********************************************************************
4625 Buffer-text Allocation
4626 ***********************************************************************/
4628 #ifdef REL_ALLOC
4629 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4630 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4631 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4632 #endif /* REL_ALLOC */
4635 /* Allocate NBYTES bytes for buffer B's text buffer. */
4637 static void
4638 alloc_buffer_text (b, nbytes)
4639 struct buffer *b;
4640 size_t nbytes;
4642 POINTER_TYPE *p;
4644 BLOCK_INPUT;
4645 #if defined USE_MMAP_FOR_BUFFERS
4646 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4647 #elif defined REL_ALLOC
4648 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4649 #else
4650 p = xmalloc (nbytes);
4651 #endif
4653 if (p == NULL)
4655 UNBLOCK_INPUT;
4656 memory_full ();
4659 b->text->beg = (unsigned char *) p;
4660 UNBLOCK_INPUT;
4663 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4664 shrink it. */
4666 void
4667 enlarge_buffer_text (b, delta)
4668 struct buffer *b;
4669 int delta;
4671 POINTER_TYPE *p;
4672 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4673 + delta);
4674 BLOCK_INPUT;
4675 #if defined USE_MMAP_FOR_BUFFERS
4676 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4677 #elif defined REL_ALLOC
4678 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4679 #else
4680 p = xrealloc (b->text->beg, nbytes);
4681 #endif
4683 if (p == NULL)
4685 UNBLOCK_INPUT;
4686 memory_full ();
4689 BUF_BEG_ADDR (b) = (unsigned char *) p;
4690 UNBLOCK_INPUT;
4694 /* Free buffer B's text buffer. */
4696 static void
4697 free_buffer_text (b)
4698 struct buffer *b;
4700 BLOCK_INPUT;
4702 #if defined USE_MMAP_FOR_BUFFERS
4703 mmap_free ((POINTER_TYPE **) &b->text->beg);
4704 #elif defined REL_ALLOC
4705 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4706 #else
4707 xfree (b->text->beg);
4708 #endif
4710 BUF_BEG_ADDR (b) = NULL;
4711 UNBLOCK_INPUT;
4716 /***********************************************************************
4717 Initialization
4718 ***********************************************************************/
4720 void
4721 init_buffer_once ()
4723 int idx;
4725 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
4727 /* Make sure all markable slots in buffer_defaults
4728 are initialized reasonably, so mark_buffer won't choke. */
4729 reset_buffer (&buffer_defaults);
4730 reset_buffer_local_variables (&buffer_defaults, 1);
4731 reset_buffer (&buffer_local_symbols);
4732 reset_buffer_local_variables (&buffer_local_symbols, 1);
4733 /* Prevent GC from getting confused. */
4734 buffer_defaults.text = &buffer_defaults.own_text;
4735 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4736 BUF_INTERVALS (&buffer_defaults) = 0;
4737 BUF_INTERVALS (&buffer_local_symbols) = 0;
4738 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4739 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4741 /* Set up the default values of various buffer slots. */
4742 /* Must do these before making the first buffer! */
4744 /* real setup is done in bindings.el */
4745 buffer_defaults.mode_line_format = build_string ("%-");
4746 buffer_defaults.header_line_format = Qnil;
4747 buffer_defaults.abbrev_mode = Qnil;
4748 buffer_defaults.overwrite_mode = Qnil;
4749 buffer_defaults.case_fold_search = Qt;
4750 buffer_defaults.auto_fill_function = Qnil;
4751 buffer_defaults.selective_display = Qnil;
4752 #ifndef old
4753 buffer_defaults.selective_display_ellipses = Qt;
4754 #endif
4755 buffer_defaults.abbrev_table = Qnil;
4756 buffer_defaults.display_table = Qnil;
4757 buffer_defaults.undo_list = Qnil;
4758 buffer_defaults.mark_active = Qnil;
4759 buffer_defaults.file_format = Qnil;
4760 buffer_defaults.overlays_before = Qnil;
4761 buffer_defaults.overlays_after = Qnil;
4762 XSETFASTINT (buffer_defaults.overlay_center, BEG);
4764 XSETFASTINT (buffer_defaults.tab_width, 8);
4765 buffer_defaults.truncate_lines = Qnil;
4766 buffer_defaults.ctl_arrow = Qt;
4767 buffer_defaults.direction_reversed = Qnil;
4768 buffer_defaults.cursor_type = Qt;
4769 buffer_defaults.extra_line_spacing = Qnil;
4771 #ifdef DOS_NT
4772 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
4773 #endif
4774 buffer_defaults.enable_multibyte_characters = Qt;
4775 buffer_defaults.buffer_file_coding_system = Qnil;
4776 XSETFASTINT (buffer_defaults.fill_column, 70);
4777 XSETFASTINT (buffer_defaults.left_margin, 0);
4778 buffer_defaults.cache_long_line_scans = Qnil;
4779 buffer_defaults.file_truename = Qnil;
4780 XSETFASTINT (buffer_defaults.display_count, 0);
4781 buffer_defaults.indicate_empty_lines = Qnil;
4782 buffer_defaults.scroll_up_aggressively = Qnil;
4783 buffer_defaults.scroll_down_aggressively = Qnil;
4784 buffer_defaults.display_time = Qnil;
4786 /* Assign the local-flags to the slots that have default values.
4787 The local flag is a bit that is used in the buffer
4788 to say that it has its own local value for the slot.
4789 The local flag bits are in the local_var_flags slot of the buffer. */
4791 /* Nothing can work if this isn't true */
4792 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
4794 /* 0 means not a lisp var, -1 means always local, else mask */
4795 bzero (&buffer_local_flags, sizeof buffer_local_flags);
4796 XSETINT (buffer_local_flags.filename, -1);
4797 XSETINT (buffer_local_flags.directory, -1);
4798 XSETINT (buffer_local_flags.backed_up, -1);
4799 XSETINT (buffer_local_flags.save_length, -1);
4800 XSETINT (buffer_local_flags.auto_save_file_name, -1);
4801 XSETINT (buffer_local_flags.read_only, -1);
4802 XSETINT (buffer_local_flags.major_mode, -1);
4803 XSETINT (buffer_local_flags.mode_name, -1);
4804 XSETINT (buffer_local_flags.undo_list, -1);
4805 XSETINT (buffer_local_flags.mark_active, -1);
4806 XSETINT (buffer_local_flags.point_before_scroll, -1);
4807 XSETINT (buffer_local_flags.file_truename, -1);
4808 XSETINT (buffer_local_flags.invisibility_spec, -1);
4809 XSETINT (buffer_local_flags.file_format, -1);
4810 XSETINT (buffer_local_flags.display_count, -1);
4811 XSETINT (buffer_local_flags.display_time, -1);
4812 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
4814 idx = 1;
4815 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
4816 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
4817 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
4818 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
4819 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
4820 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
4821 #ifndef old
4822 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
4823 #endif
4824 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
4825 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
4826 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
4827 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
4828 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
4829 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
4830 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
4831 #ifdef DOS_NT
4832 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
4833 /* Make this one a permanent local. */
4834 buffer_permanent_local_flags[idx++] = 1;
4835 #endif
4836 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
4837 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
4838 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
4839 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
4840 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
4841 /* Make this one a permanent local. */
4842 buffer_permanent_local_flags[idx++] = 1;
4843 XSETFASTINT (buffer_local_flags.left_margin_width, idx); ++idx;
4844 XSETFASTINT (buffer_local_flags.right_margin_width, idx); ++idx;
4845 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
4846 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
4847 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
4848 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
4849 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
4850 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
4852 /* Need more room? */
4853 if (idx >= MAX_PER_BUFFER_VARS)
4854 abort ();
4855 last_per_buffer_idx = idx;
4857 Vbuffer_alist = Qnil;
4858 current_buffer = 0;
4859 all_buffers = 0;
4861 QSFundamental = build_string ("Fundamental");
4863 Qfundamental_mode = intern ("fundamental-mode");
4864 buffer_defaults.major_mode = Qfundamental_mode;
4866 Qmode_class = intern ("mode-class");
4868 Qprotected_field = intern ("protected-field");
4870 Qpermanent_local = intern ("permanent-local");
4872 Qkill_buffer_hook = intern ("kill-buffer-hook");
4874 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
4876 /* super-magic invisible buffer */
4877 Vbuffer_alist = Qnil;
4879 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4881 inhibit_modification_hooks = 0;
4884 void
4885 init_buffer ()
4887 char buf[MAXPATHLEN + 1];
4888 char *pwd;
4889 struct stat dotstat, pwdstat;
4890 Lisp_Object temp;
4891 int rc;
4893 #ifdef USE_MMAP_FOR_BUFFERS
4895 /* When using the ralloc implementation based on mmap(2), buffer
4896 text pointers will have been set to null in the dumped Emacs.
4897 Map new memory. */
4898 struct buffer *b;
4900 for (b = all_buffers; b; b = b->next)
4901 if (b->text->beg == NULL)
4902 enlarge_buffer_text (b, 0);
4904 #endif /* USE_MMAP_FOR_BUFFERS */
4906 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4907 if (NILP (buffer_defaults.enable_multibyte_characters))
4908 Fset_buffer_multibyte (Qnil);
4910 /* If PWD is accurate, use it instead of calling getwd. This is faster
4911 when PWD is right, and may avoid a fatal error. */
4912 if ((pwd = getenv ("PWD")) != 0
4913 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
4914 && stat (pwd, &pwdstat) == 0
4915 && stat (".", &dotstat) == 0
4916 && dotstat.st_ino == pwdstat.st_ino
4917 && dotstat.st_dev == pwdstat.st_dev
4918 && strlen (pwd) < MAXPATHLEN)
4919 strcpy (buf, pwd);
4920 #ifdef HAVE_GETCWD
4921 else if (getcwd (buf, MAXPATHLEN+1) == 0)
4922 fatal ("`getcwd' failed: %s\n", strerror (errno));
4923 #else
4924 else if (getwd (buf) == 0)
4925 fatal ("`getwd' failed: %s\n", buf);
4926 #endif
4928 #ifndef VMS
4929 /* Maybe this should really use some standard subroutine
4930 whose definition is filename syntax dependent. */
4931 rc = strlen (buf);
4932 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
4934 buf[rc] = DIRECTORY_SEP;
4935 buf[rc + 1] = '\0';
4937 #endif /* not VMS */
4939 current_buffer->directory = build_string (buf);
4941 /* Add /: to the front of the name
4942 if it would otherwise be treated as magic. */
4943 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
4944 if (! NILP (temp)
4945 /* If the default dir is just /, TEMP is non-nil
4946 because of the ange-ftp completion handler.
4947 However, it is not necessary to turn / into /:/.
4948 So avoid doing that. */
4949 && strcmp ("/", XSTRING (current_buffer->directory)->data))
4950 current_buffer->directory
4951 = concat2 (build_string ("/:"), current_buffer->directory);
4953 temp = get_minibuffer (0);
4954 XBUFFER (temp)->directory = current_buffer->directory;
4957 /* initialize the buffer routines */
4958 void
4959 syms_of_buffer ()
4961 staticpro (&last_overlay_modification_hooks);
4962 last_overlay_modification_hooks
4963 = Fmake_vector (make_number (10), Qnil);
4965 staticpro (&Vbuffer_defaults);
4966 staticpro (&Vbuffer_local_symbols);
4967 staticpro (&Qfundamental_mode);
4968 staticpro (&Qmode_class);
4969 staticpro (&QSFundamental);
4970 staticpro (&Vbuffer_alist);
4971 staticpro (&Qprotected_field);
4972 staticpro (&Qpermanent_local);
4973 staticpro (&Qkill_buffer_hook);
4974 Qoverlayp = intern ("overlayp");
4975 staticpro (&Qoverlayp);
4976 Qevaporate = intern ("evaporate");
4977 staticpro (&Qevaporate);
4978 Qmodification_hooks = intern ("modification-hooks");
4979 staticpro (&Qmodification_hooks);
4980 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
4981 staticpro (&Qinsert_in_front_hooks);
4982 Qinsert_behind_hooks = intern ("insert-behind-hooks");
4983 staticpro (&Qinsert_behind_hooks);
4984 Qget_file_buffer = intern ("get-file-buffer");
4985 staticpro (&Qget_file_buffer);
4986 Qpriority = intern ("priority");
4987 staticpro (&Qpriority);
4988 Qwindow = intern ("window");
4989 staticpro (&Qwindow);
4990 Qbefore_string = intern ("before-string");
4991 staticpro (&Qbefore_string);
4992 Qafter_string = intern ("after-string");
4993 staticpro (&Qafter_string);
4994 Qfirst_change_hook = intern ("first-change-hook");
4995 staticpro (&Qfirst_change_hook);
4996 Qbefore_change_functions = intern ("before-change-functions");
4997 staticpro (&Qbefore_change_functions);
4998 Qafter_change_functions = intern ("after-change-functions");
4999 staticpro (&Qafter_change_functions);
5001 Fput (Qprotected_field, Qerror_conditions,
5002 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5003 Fput (Qprotected_field, Qerror_message,
5004 build_string ("Attempt to modify a protected field"));
5006 /* All these use DEFVAR_LISP_NOPRO because the slots in
5007 buffer_defaults will all be marked via Vbuffer_defaults. */
5009 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5010 &buffer_defaults.mode_line_format,
5011 "Default value of `mode-line-format' for buffers that don't override it.\n\
5012 This is the same as (default-value 'mode-line-format).");
5014 DEFVAR_LISP_NOPRO ("default-header-line-format",
5015 &buffer_defaults.header_line_format,
5016 "Default value of `header-line-format' for buffers that don't override it.\n\
5017 This is the same as (default-value 'header-line-format).");
5019 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5020 "Default value of `cursor-type' for buffers that don't override it.\n\
5021 This is the same as (default-value 'cursor-type).");
5023 DEFVAR_LISP_NOPRO ("default-line-spacing",
5024 &buffer_defaults.extra_line_spacing,
5025 "Default value of `line-spacing' for buffers that don't override it.\n\
5026 This is the same as (default-value 'line-spacing).");
5028 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5029 &buffer_defaults.abbrev_mode,
5030 "Default value of `abbrev-mode' for buffers that do not override it.\n\
5031 This is the same as (default-value 'abbrev-mode).");
5033 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5034 &buffer_defaults.ctl_arrow,
5035 "Default value of `ctl-arrow' for buffers that do not override it.\n\
5036 This is the same as (default-value 'ctl-arrow).");
5038 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5039 &buffer_defaults.direction_reversed,
5040 "Default value of `direction_reversed' for buffers that do not override it.\n\
5041 This is the same as (default-value 'direction-reversed).");
5043 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5044 &buffer_defaults.enable_multibyte_characters,
5045 "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
5046 This is the same as (default-value 'enable-multibyte-characters).");
5048 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5049 &buffer_defaults.buffer_file_coding_system,
5050 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
5051 This is the same as (default-value 'buffer-file-coding-system).");
5053 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5054 &buffer_defaults.truncate_lines,
5055 "Default value of `truncate-lines' for buffers that do not override it.\n\
5056 This is the same as (default-value 'truncate-lines).");
5058 DEFVAR_LISP_NOPRO ("default-fill-column",
5059 &buffer_defaults.fill_column,
5060 "Default value of `fill-column' for buffers that do not override it.\n\
5061 This is the same as (default-value 'fill-column).");
5063 DEFVAR_LISP_NOPRO ("default-left-margin",
5064 &buffer_defaults.left_margin,
5065 "Default value of `left-margin' for buffers that do not override it.\n\
5066 This is the same as (default-value 'left-margin).");
5068 DEFVAR_LISP_NOPRO ("default-tab-width",
5069 &buffer_defaults.tab_width,
5070 "Default value of `tab-width' for buffers that do not override it.\n\
5071 This is the same as (default-value 'tab-width).");
5073 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5074 &buffer_defaults.case_fold_search,
5075 "Default value of `case-fold-search' for buffers that don't override it.\n\
5076 This is the same as (default-value 'case-fold-search).");
5078 #ifdef DOS_NT
5079 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5080 &buffer_defaults.buffer_file_type,
5081 "Default file type for buffers that do not override it.\n\
5082 This is the same as (default-value 'buffer-file-type).\n\
5083 The file type is nil for text, t for binary.");
5084 #endif
5086 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5087 &buffer_defaults.left_margin_width,
5088 "Default value of `left-margin-width' for buffers that don't override it.\n\
5089 This is the same as (default-value 'left-margin-width).");
5091 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5092 &buffer_defaults.right_margin_width,
5093 "Default value of `right_margin_width' for buffers that don't override it.\n\
5094 This is the same as (default-value 'right-margin-width).");
5096 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5097 &buffer_defaults.indicate_empty_lines,
5098 "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
5099 This is the same as (default-value 'indicate-empty-lines).");
5101 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5102 &buffer_defaults.scroll_up_aggressively,
5103 "Default value of `scroll-up-aggressively' for buffers that\n\
5104 don't override it. This is the same as (default-value\n\
5105 'scroll-up-aggressively).");
5107 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5108 &buffer_defaults.scroll_down_aggressively,
5109 "Default value of `scroll-down-aggressively' for buffers that\n\
5110 don't override it. This is the same as (default-value\n\
5111 'scroll-down-aggressively).");
5113 DEFVAR_PER_BUFFER ("header-line-format",
5114 &current_buffer->header_line_format,
5115 Qnil,
5116 "Analogous to `mode-line-format', but for the mode line that can be\n\
5117 displayed at the top of a window.");
5119 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5120 Qnil, 0);
5122 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
5123 But make-docfile finds it!
5124 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5125 Qnil,
5126 "Template for displaying mode line for current buffer.\n\
5127 Each buffer has its own value of this variable.\n\
5128 Value may be nil, a string, a symbol or a list or cons cell.\n\
5129 A value of nil means don't display a mode line.\n\
5130 For a symbol, its value is used (but it is ignored if t or nil).\n\
5131 A string appearing directly as the value of a symbol is processed verbatim\n\
5132 in that the %-constructs below are not recognized.\n\
5133 For a list of the form `(:eval FORM)', FORM is evaluated and the result\n\
5134 is used as a mode line element.\n\
5135 For a list whose car is a symbol, the symbol's value is taken,\n\
5136 and if that is non-nil, the cadr of the list is processed recursively.\n\
5137 Otherwise, the caddr of the list (if there is one) is processed.\n\
5138 For a list whose car is a string or list, each element is processed\n\
5139 recursively and the results are effectively concatenated.\n\
5140 For a list whose car is an integer, the cdr of the list is processed\n\
5141 and padded (if the number is positive) or truncated (if negative)\n\
5142 to the width specified by that number.\n\
5143 A string is printed verbatim in the mode line except for %-constructs:\n\
5144 (%-constructs are allowed when the string is the entire mode-line-format\n\
5145 or when it is found in a cons-cell or a list)\n\
5146 %b -- print buffer name. %f -- print visited file name.\n\
5147 %F -- print frame name.\n\
5148 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
5149 %& is like %*, but ignore read-only-ness.\n\
5150 % means buffer is read-only and * means it is modified.\n\
5151 For a modified read-only buffer, %* gives % and %+ gives *.\n\
5152 %s -- print process status. %l -- print the current line number.\n\
5153 %c -- print the current column number (this makes editing slower).\n\
5154 To make the column number update correctly in all cases,\n\
5155 `column-number-mode' must be non-nil.\n\
5156 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
5157 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
5158 or print Bottom or All.\n\
5159 %m -- print the mode name.\n\
5160 %n -- print Narrow if appropriate.\n\
5161 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.\n\
5162 %Z -- like %z, but including the end-of-line format.\n\
5163 %[ -- print one [ for each recursive editing level. %] similar.\n\
5164 %% -- print %. %- -- print infinitely many dashes.\n\
5165 Decimal digits after the % specify field width to which to pad.");
5168 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5169 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
5170 nil here means use current buffer's major mode.");
5172 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5173 make_number (Lisp_Symbol),
5174 "Symbol for current buffer's major mode.");
5176 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5177 make_number (Lisp_String),
5178 "Pretty name of current buffer's major mode (a string).");
5180 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5181 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
5182 Automatically becomes buffer-local when set in any fashion.");
5184 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5185 Qnil,
5186 "*Non-nil if searches and matches should ignore case.\n\
5187 Automatically becomes buffer-local when set in any fashion.");
5189 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5190 make_number (Lisp_Int),
5191 "*Column beyond which automatic line-wrapping should happen.\n\
5192 Automatically becomes buffer-local when set in any fashion.");
5194 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5195 make_number (Lisp_Int),
5196 "*Column for the default indent-line-function to indent to.\n\
5197 Linefeed indents to this column in Fundamental mode.\n\
5198 Automatically becomes buffer-local when set in any fashion.");
5200 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5201 make_number (Lisp_Int),
5202 "*Distance between tab stops (for display of tab characters), in columns.\n\
5203 Automatically becomes buffer-local when set in any fashion.");
5205 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5206 "*Non-nil means display control chars with uparrow.\n\
5207 A value of nil means use backslash and octal digits.\n\
5208 Automatically becomes buffer-local when set in any fashion.\n\
5209 This variable does not apply to characters whose display is specified\n\
5210 in the current display table (if there is one).");
5212 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5213 &current_buffer->enable_multibyte_characters,
5214 make_number (-1),
5215 "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
5216 Otherwise they are regarded as unibyte. This affects the display,\n\
5217 file I/O and the behavior of various editing commands.\n\
5219 This variable is buffer-local but you cannot set it directly;\n\
5220 use the function `set-buffer-multibyte' to change a buffer's representation.\n\
5221 Changing its default value with `setq-default' is supported.\n\
5222 See also variable `default-enable-multibyte-characters' and Info node\n\
5223 `(elisp)Text Representations'.");
5225 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5226 &current_buffer->buffer_file_coding_system, Qnil,
5227 "Coding system to be used for encoding the buffer contents on saving.\n\
5228 This variable applies to saving the buffer, and also to `write-region'\n\
5229 and other functions that use `write-region'.\n\
5230 It does not apply to sending output to subprocesses, however.\n\
5232 If this is nil, the buffer is saved without any code conversion\n\
5233 unless some coding system is specified in `file-coding-system-alist'\n\
5234 for the buffer file.\n\
5236 The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
5238 This variable is never applied to a way of decoding\n\
5239 a file while reading it.");
5241 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5242 Qnil,
5243 "*Non-nil means lines in the buffer are displayed right to left.");
5245 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5246 "*Non-nil means do not display continuation lines;\n\
5247 give each line of text one screen line.\n\
5248 Automatically becomes buffer-local when set in any fashion.\n\
5250 Note that this is overridden by the variable\n\
5251 `truncate-partial-width-windows' if that variable is non-nil\n\
5252 and this buffer is not full-frame width.");
5254 #ifdef DOS_NT
5255 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5256 Qnil,
5257 "Non-nil if the visited file is a binary file.\n\
5258 This variable is meaningful on MS-DOG and Windows NT.\n\
5259 On those systems, it is automatically local in every buffer.\n\
5260 On other systems, this variable is normally always nil.");
5261 #endif
5263 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5264 make_number (Lisp_String),
5265 "Name of default directory of current buffer. Should end with slash.\n\
5266 Each buffer has its own value of this variable. To change the\n\
5267 default directory, use function `cd'.");
5269 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5270 Qnil,
5271 "Function called (if non-nil) to perform auto-fill.\n\
5272 It is called after self-inserting any character specified in\n\
5273 the `auto-fill-chars' table.\n\
5274 Each buffer has its own value of this variable.\n\
5275 NOTE: This variable is not a hook;\n\
5276 its value may not be a list of functions.");
5278 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5279 make_number (Lisp_String),
5280 "Name of file visited in current buffer, or nil if not visiting a file.\n\
5281 Each buffer has its own value of this variable.");
5283 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5284 make_number (Lisp_String),
5285 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
5286 The truename of a file is calculated by `file-truename'\n\
5287 and then abbreviated with `abbreviate-file-name'.\n\
5288 Each buffer has its own value of this variable.");
5290 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5291 &current_buffer->auto_save_file_name,
5292 make_number (Lisp_String),
5293 "Name of file for auto-saving current buffer,\n\
5294 or nil if buffer should not be auto-saved.\n\
5295 Each buffer has its own value of this variable.");
5297 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5298 "Non-nil if this buffer is read-only.\n\
5299 Each buffer has its own value of this variable.");
5301 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5302 "Non-nil if this buffer's file has been backed up.\n\
5303 Backing up is done before the first time the file is saved.\n\
5304 Each buffer has its own value of this variable.");
5306 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5307 make_number (Lisp_Int),
5308 "Length of current buffer when last read in, saved or auto-saved.\n\
5309 0 initially.\n\
5310 Each buffer has its own value of this variable.");
5312 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5313 Qnil,
5314 "Non-nil enables selective display:\n\
5315 Integer N as value means display only lines\n\
5316 that start with less than n columns of space.\n\
5317 A value of t means, after a ^M, all the rest of the line is invisible.\n\
5318 Then ^M's in the file are written into files as newlines.\n\n\
5319 Automatically becomes buffer-local when set in any fashion.");
5321 #ifndef old
5322 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5323 &current_buffer->selective_display_ellipses,
5324 Qnil,
5325 "t means display ... on previous line when a line is invisible.\n\
5326 Automatically becomes buffer-local when set in any fashion.");
5327 #endif
5329 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5330 "Non-nil if self-insertion should replace existing text.\n\
5331 The value should be one of `overwrite-mode-textual',\n\
5332 `overwrite-mode-binary', or nil.\n\
5333 If it is `overwrite-mode-textual', self-insertion still\n\
5334 inserts at the end of a line, and inserts when point is before a tab,\n\
5335 until the tab is filled in.\n\
5336 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
5337 Automatically becomes buffer-local when set in any fashion.");
5339 #if 0 /* The doc string is too long for some compilers,
5340 but make-docfile can find it in this comment. */
5341 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5342 Qnil,
5343 "Display table that controls display of the contents of current buffer.\n\
5344 Automatically becomes buffer-local when set in any fashion.\n\
5346 If this variable is nil, the value of `standard-display-table' is used.\n\
5347 Each window can have its own, overriding display table, see\n\
5348 `set-window-display-table' and `window-display-table'.\n\
5350 The display table is a char-table created with `make-display-table'.\n\
5351 A char-table is an array indexed by character codes. Normal array\n\
5352 primitives `aref' and `aset' can be used to access elements of a char-table.\n\
5354 Each of the char-table elements control how to display the corresponding\n\
5355 text character: the element at index C in the table says how to display\n\
5356 the character whose code is C. Each element should be a vector of\n\
5357 characters or nil. nil means display the character in the default fashion;\n\
5358 otherwise, the characters from the vector are delivered to the screen\n\
5359 instead of the original character.\n\
5361 For example, (aset buffer-display-table ?X ?Y) will cause Emacs to display\n\
5362 a capital Y instead of each X character.\n\
5364 In addition, a char-table has six extra slots to control the display of:\n\
5366 the end of a truncated screen line (extra-slot 0, a single character);\n\
5367 the end of a continued line (extra-slot 1, a single character);\n\
5368 the escape character used to display character codes in octal\n\
5369 (extra-slot 2, a single character);\n\
5370 the character used as an arrow for control characters (extra-slot 3,\n\
5371 a single character);\n\
5372 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
5373 a vector of characters);\n\
5374 the character used to draw the border between side-by-side windows\n\
5375 (extra-slot 5, a single character).\n\
5377 See also the functions `display-table-slot' and `set-display-table-slot'.");
5378 #endif
5379 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5380 Qnil, 0);
5382 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_width,
5383 Qnil,
5384 "*Width of left marginal area for display of a buffer.\n\
5385 Automatically becomes buffer-local when set in any fashion.\n\
5386 A value of nil means no marginal area.");
5388 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_width,
5389 Qnil,
5390 "*Width of right marginal area for display of a buffer.\n\
5391 Automatically becomes buffer-local when set in any fashion.\n\
5392 A value of nil means no marginal area.");
5394 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5395 &current_buffer->indicate_empty_lines, Qnil,
5396 "*Visually indicate empty lines after the buffer end.\n\
5397 If non-nil, a bitmap is displayed in the left fringe of a window on\n\
5398 window-systems.\n\
5399 Automatically becomes buffer-local when set in any fashion.\n");
5401 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5402 &current_buffer->scroll_up_aggressively, Qnil,
5403 "*If a number, scroll display up aggressively.\n\
5404 If scrolling a window because point is below the window end, choose\n\
5405 a new window start so that point ends up that fraction of the window's\n\
5406 height from the bottom of the window.\n\
5407 Automatically becomes buffer-local when set in any fashion.");
5409 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5410 &current_buffer->scroll_down_aggressively, Qnil,
5411 "*If a number, scroll display down aggressively.\n\
5412 If scrolling a window because point is above the window start, choose\n\
5413 a new window start so that point ends up that fraction of the window's\n\
5414 height from the top of the window.\n\
5415 Automatically becomes buffer-local when set in any fashion.");
5417 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5418 "Don't ask.");
5421 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5422 "List of functions to call before each text change.\n\
5423 Two arguments are passed to each function: the positions of\n\
5424 the beginning and end of the range of old text to be changed.\n\
5425 \(For an insertion, the beginning and end are at the same place.)\n\
5426 No information is given about the length of the text after the change.\n\
5428 Buffer changes made while executing the `before-change-functions'\n\
5429 don't call any before-change or after-change functions.\n\
5430 That's because these variables are temporarily set to nil.\n\
5431 As a result, a hook function cannot straightforwardly alter the value of\n\
5432 these variables. See the Emacs Lisp manual for a way of\n\
5433 accomplishing an equivalent result by using other variables.\n\
5435 If an unhandled error happens in running these functions,\n\
5436 the variable's value remains nil. That prevents the error\n\
5437 from happening repeatedly and making Emacs nonfunctional.");
5438 Vbefore_change_functions = Qnil;
5440 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5441 "List of function to call after each text change.\n\
5442 Three arguments are passed to each function: the positions of\n\
5443 the beginning and end of the range of changed text,\n\
5444 and the length in bytes of the pre-change text replaced by that range.\n\
5445 \(For an insertion, the pre-change length is zero;\n\
5446 for a deletion, that length is the number of bytes deleted,\n\
5447 and the post-change beginning and end are at the same place.)\n\
5449 Buffer changes made while executing the `after-change-functions'\n\
5450 don't call any before-change or after-change functions.\n\
5451 That's because these variables are temporarily set to nil.\n\
5452 As a result, a hook function cannot straightforwardly alter the value of\n\
5453 these variables. See the Emacs Lisp manual for a way of\n\
5454 accomplishing an equivalent result by using other variables.\n\
5456 If an unhandled error happens in running these functions,\n\
5457 the variable's value remains nil. That prevents the error\n\
5458 from happening repeatedly and making Emacs nonfunctional.");
5459 Vafter_change_functions = Qnil;
5461 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5462 "A list of functions to call before changing a buffer which is unmodified.\n\
5463 The functions are run using the `run-hooks' function.");
5464 Vfirst_change_hook = Qnil;
5466 #if 0 /* The doc string is too long for some compilers,
5467 but make-docfile can find it in this comment. */
5468 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5469 "List of undo entries in current buffer.\n\
5470 This variable is always local in all buffers.\n\
5471 Recent changes come first; older changes follow newer.\n\
5473 An entry (BEG . END) represents an insertion which begins at\n\
5474 position BEG and ends at position END.\n\
5476 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
5477 from (abs POSITION). If POSITION is positive, point was at the front\n\
5478 of the text being deleted; if negative, point was at the end.\n\
5480 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
5481 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
5482 of the visited file's modification time, as of that time. If the\n\
5483 modification time of the most recent save is different, this entry is\n\
5484 obsolete.\n\
5486 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
5487 was modified between BEG and END. PROPERTY is the property name,\n\
5488 and VALUE is the old value.\n\
5490 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
5491 was adjusted in position by the offset DISTANCE (an integer).\n\
5493 An entry of the form POSITION indicates that point was at the buffer\n\
5494 location given by the integer. Undoing an entry of this form places\n\
5495 point at POSITION.\n\
5497 nil marks undo boundaries. The undo command treats the changes\n\
5498 between two undo boundaries as a single step to be undone.\n\
5500 If the value of the variable is t, undo information is not recorded.");
5501 #endif
5502 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5505 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5506 "Non-nil means the mark and region are currently active in this buffer.\n\
5507 Automatically local in all buffers.");
5509 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5510 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
5511 This variable is buffer-local, in all buffers.\n\
5513 Normally, the line-motion functions work by scanning the buffer for\n\
5514 newlines. Columnar operations (like move-to-column and\n\
5515 compute-motion) also work by scanning the buffer, summing character\n\
5516 widths as they go. This works well for ordinary text, but if the\n\
5517 buffer's lines are very long (say, more than 500 characters), these\n\
5518 motion functions will take longer to execute. Emacs may also take\n\
5519 longer to update the display.\n\
5521 If cache-long-line-scans is non-nil, these motion functions cache the\n\
5522 results of their scans, and consult the cache to avoid rescanning\n\
5523 regions of the buffer until the text is modified. The caches are most\n\
5524 beneficial when they prevent the most searching---that is, when the\n\
5525 buffer contains long lines and large regions of characters with the\n\
5526 same, fixed screen width.\n\
5528 When cache-long-line-scans is non-nil, processing short lines will\n\
5529 become slightly slower (because of the overhead of consulting the\n\
5530 cache), and the caches will use memory roughly proportional to the\n\
5531 number of newlines and characters whose screen width varies.\n\
5533 The caches require no explicit maintenance; their accuracy is\n\
5534 maintained internally by the Emacs primitives. Enabling or disabling\n\
5535 the cache should not affect the behavior of any of the motion\n\
5536 functions; it should only affect their performance.");
5538 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5539 "Value of point before the last series of scroll operations, or nil.\n\
5540 This variable is always local in all buffers.");
5542 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5543 "List of formats to use when saving this buffer.\n\
5544 This variable is always local in all buffers.\n\
5545 Formats are defined by `format-alist'. This variable is\n\
5546 set when a file is visited. Automatically local in all buffers.");
5548 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5549 &current_buffer->invisibility_spec, Qnil,
5550 "Invisibility spec of this buffer.\n\
5551 This variable is always local in all buffers.\n\
5552 The default is t, which means that text is invisible\n\
5553 if it has a non-nil `invisible' property.\n\
5554 If the value is a list, a text character is invisible if its `invisible'\n\
5555 property is an element in that list.\n\
5556 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
5557 then characters with property value PROP are invisible,\n\
5558 and they have an ellipsis as well if ELLIPSIS is non-nil.");
5560 DEFVAR_PER_BUFFER ("buffer-display-count",
5561 &current_buffer->display_count, Qnil,
5562 "A number incremented each time this buffer is displayed in a window.\n\
5563 This variable is always local in all buffers.\n\
5564 The function `set-window-buffer increments it.");
5566 DEFVAR_PER_BUFFER ("buffer-display-time",
5567 &current_buffer->display_time, Qnil,
5568 "Time stamp updated each time this buffer is displayed in a window.\n\
5569 This variable is always local in all buffers.\n\
5570 The function `set-window-buffer' updates this variable\n\
5571 to the value obtained by calling `current-time'.\n\
5572 If the buffer has never been shown in a window, the value is nil.");
5574 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
5575 "*Non-nil means deactivate the mark when the buffer contents change.\n\
5576 Non-nil also enables highlighting of the region whenever the mark is active.\n\
5577 The variable `highlight-nonselected-windows' controls whether to highlight\n\
5578 all windows or just the selected window.");
5579 Vtransient_mark_mode = Qnil;
5581 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
5582 "*Non-nil means disregard read-only status of buffers or characters.\n\
5583 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
5584 text properties. If the value is a list, disregard `buffer-read-only'\n\
5585 and disregard a `read-only' text property if the property value\n\
5586 is a member of the list.");
5587 Vinhibit_read_only = Qnil;
5589 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
5590 "Cursor to use in window displaying this buffer.\n\
5591 Values are interpreted as follows:\n\
5593 t use the cursor specified for the frame\n\
5594 nil don't display a cursor\n\
5595 `bar' display a bar cursor with default width\n\
5596 (bar . WIDTH) display a bar cursor with width WIDTH\n\
5597 others display a box cursor.");
5599 DEFVAR_PER_BUFFER ("line-spacing",
5600 &current_buffer->extra_line_spacing, Qnil,
5601 "Additional space to put between lines when displaying a buffer.\n\
5602 The space is measured in pixels, and put below lines on window systems.");
5604 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
5605 "List of functions called with no args to query before killing a buffer.");
5606 Vkill_buffer_query_functions = Qnil;
5608 defsubr (&Sbuffer_live_p);
5609 defsubr (&Sbuffer_list);
5610 defsubr (&Sget_buffer);
5611 defsubr (&Sget_file_buffer);
5612 defsubr (&Sget_buffer_create);
5613 defsubr (&Smake_indirect_buffer);
5614 defsubr (&Sgenerate_new_buffer_name);
5615 defsubr (&Sbuffer_name);
5616 /*defsubr (&Sbuffer_number);*/
5617 defsubr (&Sbuffer_file_name);
5618 defsubr (&Sbuffer_base_buffer);
5619 defsubr (&Sbuffer_local_variables);
5620 defsubr (&Sbuffer_modified_p);
5621 defsubr (&Sset_buffer_modified_p);
5622 defsubr (&Sbuffer_modified_tick);
5623 defsubr (&Srename_buffer);
5624 defsubr (&Sother_buffer);
5625 defsubr (&Sbuffer_disable_undo);
5626 defsubr (&Sbuffer_enable_undo);
5627 defsubr (&Skill_buffer);
5628 defsubr (&Sset_buffer_major_mode);
5629 defsubr (&Sswitch_to_buffer);
5630 defsubr (&Spop_to_buffer);
5631 defsubr (&Scurrent_buffer);
5632 defsubr (&Sset_buffer);
5633 defsubr (&Sbarf_if_buffer_read_only);
5634 defsubr (&Sbury_buffer);
5635 defsubr (&Serase_buffer);
5636 defsubr (&Sset_buffer_multibyte);
5637 defsubr (&Skill_all_local_variables);
5639 defsubr (&Soverlayp);
5640 defsubr (&Smake_overlay);
5641 defsubr (&Sdelete_overlay);
5642 defsubr (&Smove_overlay);
5643 defsubr (&Soverlay_start);
5644 defsubr (&Soverlay_end);
5645 defsubr (&Soverlay_buffer);
5646 defsubr (&Soverlay_properties);
5647 defsubr (&Soverlays_at);
5648 defsubr (&Soverlays_in);
5649 defsubr (&Snext_overlay_change);
5650 defsubr (&Sprevious_overlay_change);
5651 defsubr (&Soverlay_recenter);
5652 defsubr (&Soverlay_lists);
5653 defsubr (&Soverlay_get);
5654 defsubr (&Soverlay_put);
5655 defsubr (&Srestore_buffer_modified_p);
5658 void
5659 keys_of_buffer ()
5661 initial_define_key (control_x_map, 'b', "switch-to-buffer");
5662 initial_define_key (control_x_map, 'k', "kill-buffer");
5664 /* This must not be in syms_of_buffer, because Qdisabled is not
5665 initialized when that function gets called. */
5666 Fput (intern ("erase-buffer"), Qdisabled, Qt);