(modify_overlay): Update prototype.
[emacs.git] / src / buffer.c
blobc14d874f1d68d30461762aa07dd6fdac4aa1e191
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999, 2000, 2001, 02, 2003
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 [probably SunOS? -stef] , 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
43 #include "lisp.h"
44 #include "intervals.h"
45 #include "window.h"
46 #include "commands.h"
47 #include "buffer.h"
48 #include "charset.h"
49 #include "region-cache.h"
50 #include "indent.h"
51 #include "blockinput.h"
52 #include "keyboard.h"
53 #include "keymap.h"
54 #include "frame.h"
56 struct buffer *current_buffer; /* the current buffer */
58 /* First buffer in chain of all buffers (in reverse order of creation).
59 Threaded through ->next. */
61 struct buffer *all_buffers;
63 /* This structure holds the default values of the buffer-local variables
64 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
65 The default value occupies the same slot in this structure
66 as an individual buffer's value occupies in that buffer.
67 Setting the default value also goes through the alist of buffers
68 and stores into each buffer that does not say it has a local value. */
70 struct buffer buffer_defaults;
72 /* A Lisp_Object pointer to the above, used for staticpro */
74 static Lisp_Object Vbuffer_defaults;
76 /* This structure marks which slots in a buffer have corresponding
77 default values in buffer_defaults.
78 Each such slot has a nonzero value in this structure.
79 The value has only one nonzero bit.
81 When a buffer has its own local value for a slot,
82 the entry for that slot (found in the same slot in this structure)
83 is turned on in the buffer's local_flags array.
85 If a slot in this structure is -1, then even though there may
86 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
87 and the corresponding slot in buffer_defaults is not used.
89 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
90 but there is a default value which is copied into each buffer.
92 If a slot in this structure is negative, then even though there may
93 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
94 and the corresponding slot in buffer_defaults is not used.
96 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
97 zero, that is a bug */
99 struct buffer buffer_local_flags;
101 /* This structure holds the names of symbols whose values may be
102 buffer-local. It is indexed and accessed in the same way as the above. */
104 struct buffer buffer_local_symbols;
105 /* A Lisp_Object pointer to the above, used for staticpro */
106 static Lisp_Object Vbuffer_local_symbols;
108 /* This structure holds the required types for the values in the
109 buffer-local slots. If a slot contains Qnil, then the
110 corresponding buffer slot may contain a value of any type. If a
111 slot contains an integer, then prospective values' tags must be
112 equal to that integer (except nil is always allowed).
113 When a tag does not match, the function
114 buffer_slot_type_mismatch will signal an error.
116 If a slot here contains -1, the corresponding variable is read-only. */
117 struct buffer buffer_local_types;
119 /* Flags indicating which built-in buffer-local variables
120 are permanent locals. */
121 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
123 /* Number of per-buffer variables used. */
125 int last_per_buffer_idx;
127 Lisp_Object Fset_buffer ();
128 void set_buffer_internal ();
129 void set_buffer_internal_1 ();
130 static void call_overlay_mod_hooks ();
131 static void swap_out_buffer_local_variables ();
132 static void reset_buffer_local_variables ();
134 /* Alist of all buffer names vs the buffers. */
135 /* This used to be a variable, but is no longer,
136 to prevent lossage due to user rplac'ing this alist or its elements. */
137 Lisp_Object Vbuffer_alist;
139 /* Functions to call before and after each text change. */
140 Lisp_Object Vbefore_change_functions;
141 Lisp_Object Vafter_change_functions;
143 Lisp_Object Vtransient_mark_mode;
145 /* t means ignore all read-only text properties.
146 A list means ignore such a property if its value is a member of the list.
147 Any non-nil value means ignore buffer-read-only. */
148 Lisp_Object Vinhibit_read_only;
150 /* List of functions to call that can query about killing a buffer.
151 If any of these functions returns nil, we don't kill it. */
152 Lisp_Object Vkill_buffer_query_functions;
153 Lisp_Object Qkill_buffer_query_functions;
155 /* List of functions to call before changing an unmodified buffer. */
156 Lisp_Object Vfirst_change_hook;
158 Lisp_Object Qfirst_change_hook;
159 Lisp_Object Qbefore_change_functions;
160 Lisp_Object Qafter_change_functions;
161 Lisp_Object Qucs_set_table_for_input;
163 /* If nonzero, all modification hooks are suppressed. */
164 int inhibit_modification_hooks;
166 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
168 Lisp_Object Qprotected_field;
170 Lisp_Object QSFundamental; /* A string "Fundamental" */
172 Lisp_Object Qkill_buffer_hook;
174 Lisp_Object Qget_file_buffer;
176 Lisp_Object Qoverlayp;
178 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
180 Lisp_Object Qmodification_hooks;
181 Lisp_Object Qinsert_in_front_hooks;
182 Lisp_Object Qinsert_behind_hooks;
184 static void alloc_buffer_text P_ ((struct buffer *, size_t));
185 static void free_buffer_text P_ ((struct buffer *b));
186 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
187 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
190 /* For debugging; temporary. See set_buffer_internal. */
191 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
193 void
194 nsberror (spec)
195 Lisp_Object spec;
197 if (STRINGP (spec))
198 error ("No buffer named %s", SDATA (spec));
199 error ("Invalid buffer argument");
202 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
203 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
204 Value is nil if OBJECT is not a buffer or if it has been killed. */)
205 (object)
206 Lisp_Object object;
208 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
209 ? Qt : Qnil);
212 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
213 doc: /* Return a list of all existing live buffers.
214 If the optional arg FRAME is a frame, we return the buffer list
215 in the proper order for that frame: the buffers in FRAME's `buffer-list'
216 frame parameter come first, followed by the rest of the buffers. */)
217 (frame)
218 Lisp_Object frame;
220 Lisp_Object framelist, general;
221 general = Fmapcar (Qcdr, Vbuffer_alist);
223 if (FRAMEP (frame))
225 Lisp_Object tail;
227 CHECK_FRAME (frame);
229 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
231 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
232 tail = framelist;
233 while (! NILP (tail))
235 general = Fdelq (XCAR (tail), general);
236 tail = XCDR (tail);
238 return nconc2 (framelist, general);
241 return general;
244 /* Like Fassoc, but use Fstring_equal to compare
245 (which ignores text properties),
246 and don't ever QUIT. */
248 static Lisp_Object
249 assoc_ignore_text_properties (key, list)
250 register Lisp_Object key;
251 Lisp_Object list;
253 register Lisp_Object tail;
254 for (tail = list; CONSP (tail); tail = XCDR (tail))
256 register Lisp_Object elt, tem;
257 elt = XCAR (tail);
258 tem = Fstring_equal (Fcar (elt), key);
259 if (!NILP (tem))
260 return elt;
262 return Qnil;
265 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
266 doc: /* Return the buffer named NAME (a string).
267 If there is no live buffer named NAME, return nil.
268 NAME may also be a buffer; if so, the value is that buffer. */)
269 (name)
270 register Lisp_Object name;
272 if (BUFFERP (name))
273 return name;
274 CHECK_STRING (name);
276 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
279 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
280 doc: /* Return the buffer visiting file FILENAME (a string).
281 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
282 If there is no such live buffer, return nil.
283 See also `find-buffer-visiting'. */)
284 (filename)
285 register Lisp_Object filename;
287 register Lisp_Object tail, buf, tem;
288 Lisp_Object handler;
290 CHECK_STRING (filename);
291 filename = Fexpand_file_name (filename, Qnil);
293 /* If the file name has special constructs in it,
294 call the corresponding file handler. */
295 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
296 if (!NILP (handler))
297 return call2 (handler, Qget_file_buffer, filename);
299 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
301 buf = Fcdr (XCAR (tail));
302 if (!BUFFERP (buf)) continue;
303 if (!STRINGP (XBUFFER (buf)->filename)) continue;
304 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
305 if (!NILP (tem))
306 return buf;
308 return Qnil;
311 Lisp_Object
312 get_truename_buffer (filename)
313 register Lisp_Object filename;
315 register Lisp_Object tail, buf, tem;
317 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
319 buf = Fcdr (XCAR (tail));
320 if (!BUFFERP (buf)) continue;
321 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
322 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
323 if (!NILP (tem))
324 return buf;
326 return Qnil;
329 /* Incremented for each buffer created, to assign the buffer number. */
330 int buffer_count;
332 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
333 doc: /* Return the buffer named NAME, or create such a buffer and return it.
334 A new buffer is created if there is no live buffer named NAME.
335 If NAME starts with a space, the new buffer does not keep undo information.
336 If NAME is a buffer instead of a string, then it is the value returned.
337 The value is never nil. */)
338 (name)
339 register Lisp_Object name;
341 register Lisp_Object buf;
342 register struct buffer *b;
344 buf = Fget_buffer (name);
345 if (!NILP (buf))
346 return buf;
348 if (SCHARS (name) == 0)
349 error ("Empty string for buffer name is not allowed");
351 b = (struct buffer *) allocate_buffer ();
353 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
355 /* An ordinary buffer uses its own struct buffer_text. */
356 b->text = &b->own_text;
357 b->base_buffer = 0;
359 BUF_GAP_SIZE (b) = 20;
360 BLOCK_INPUT;
361 /* We allocate extra 1-byte at the tail and keep it always '\0' for
362 anchoring a search. */
363 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
364 UNBLOCK_INPUT;
365 if (! BUF_BEG_ADDR (b))
366 buffer_memory_full ();
368 BUF_PT (b) = BEG;
369 BUF_GPT (b) = BEG;
370 BUF_BEGV (b) = BEG;
371 BUF_ZV (b) = BEG;
372 BUF_Z (b) = BEG;
373 BUF_PT_BYTE (b) = BEG_BYTE;
374 BUF_GPT_BYTE (b) = BEG_BYTE;
375 BUF_BEGV_BYTE (b) = BEG_BYTE;
376 BUF_ZV_BYTE (b) = BEG_BYTE;
377 BUF_Z_BYTE (b) = BEG_BYTE;
378 BUF_MODIFF (b) = 1;
379 BUF_OVERLAY_MODIFF (b) = 1;
380 BUF_SAVE_MODIFF (b) = 1;
381 BUF_INTERVALS (b) = 0;
382 BUF_UNCHANGED_MODIFIED (b) = 1;
383 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
384 BUF_END_UNCHANGED (b) = 0;
385 BUF_BEG_UNCHANGED (b) = 0;
386 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
388 b->newline_cache = 0;
389 b->width_run_cache = 0;
390 b->width_table = Qnil;
391 b->prevent_redisplay_optimizations_p = 1;
393 /* Put this on the chain of all buffers including killed ones. */
394 b->next = all_buffers;
395 all_buffers = b;
397 /* An ordinary buffer normally doesn't need markers
398 to handle BEGV and ZV. */
399 b->pt_marker = Qnil;
400 b->begv_marker = Qnil;
401 b->zv_marker = Qnil;
403 name = Fcopy_sequence (name);
404 STRING_SET_INTERVALS (name, NULL_INTERVAL);
405 b->name = name;
407 if (SREF (name, 0) != ' ')
408 b->undo_list = Qnil;
409 else
410 b->undo_list = Qt;
412 reset_buffer (b);
413 reset_buffer_local_variables (b, 1);
415 b->mark = Fmake_marker ();
416 BUF_MARKERS (b) = NULL;
417 b->name = name;
419 /* Put this in the alist of all live buffers. */
420 XSETBUFFER (buf, b);
421 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
423 /* An error in calling the function here (should someone redfine it)
424 can lead to infinite regress until you run out of stack. rms
425 says that's not worth protecting against. */
426 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
427 /* buf is on buffer-alist, so no gcpro. */
428 call1 (Qucs_set_table_for_input, buf);
430 return buf;
434 /* Return a list of overlays which is a copy of the overlay list
435 LIST, but for buffer B. */
437 static struct Lisp_Overlay *
438 copy_overlays (b, list)
439 struct buffer *b;
440 struct Lisp_Overlay *list;
442 Lisp_Object buffer;
443 struct Lisp_Overlay *result = NULL, *tail = NULL;
445 XSETBUFFER (buffer, b);
447 for (; list; list = list->next)
449 Lisp_Object overlay, start, end, old_overlay;
450 int charpos;
452 XSETMISC (old_overlay, list);
453 charpos = marker_position (OVERLAY_START (old_overlay));
454 start = Fmake_marker ();
455 Fset_marker (start, make_number (charpos), buffer);
456 XMARKER (start)->insertion_type
457 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
459 charpos = marker_position (OVERLAY_END (old_overlay));
460 end = Fmake_marker ();
461 Fset_marker (end, make_number (charpos), buffer);
462 XMARKER (end)->insertion_type
463 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
465 overlay = allocate_misc ();
466 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
467 OVERLAY_START (overlay) = start;
468 OVERLAY_END (overlay) = end;
469 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
470 XOVERLAY (overlay)->next = NULL;
472 if (tail)
473 tail = tail->next = XOVERLAY (overlay);
474 else
475 result = tail = XOVERLAY (overlay);
478 return result;
482 /* Clone per-buffer values of buffer FROM.
484 Buffer TO gets the same per-buffer values as FROM, with the
485 following exceptions: (1) TO's name is left untouched, (2) markers
486 are copied and made to refer to TO, and (3) overlay lists are
487 copied. */
489 static void
490 clone_per_buffer_values (from, to)
491 struct buffer *from, *to;
493 Lisp_Object to_buffer;
494 int offset;
496 XSETBUFFER (to_buffer, to);
498 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
499 offset < sizeof *to;
500 offset += sizeof (Lisp_Object))
502 Lisp_Object obj;
504 obj = PER_BUFFER_VALUE (from, offset);
505 if (MARKERP (obj))
507 struct Lisp_Marker *m = XMARKER (obj);
508 obj = Fmake_marker ();
509 XMARKER (obj)->insertion_type = m->insertion_type;
510 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
513 PER_BUFFER_VALUE (to, offset) = obj;
516 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
518 to->overlays_before = copy_overlays (to, from->overlays_before);
519 to->overlays_after = copy_overlays (to, from->overlays_after);
523 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
524 2, 3,
525 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
526 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
527 BASE-BUFFER should be an existing buffer (or buffer name).
528 NAME should be a string which is not the name of an existing buffer.
529 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
530 such as major and minor modes, in the indirect buffer.
531 CLONE nil means the indirect buffer's state is reset to default values. */)
532 (base_buffer, name, clone)
533 Lisp_Object base_buffer, name, clone;
535 Lisp_Object buf;
536 struct buffer *b;
538 buf = Fget_buffer (name);
539 if (!NILP (buf))
540 error ("Buffer name `%s' is in use", SDATA (name));
542 base_buffer = Fget_buffer (base_buffer);
543 if (NILP (base_buffer))
544 error ("No such buffer: `%s'", SDATA (name));
546 if (SCHARS (name) == 0)
547 error ("Empty string for buffer name is not allowed");
549 b = (struct buffer *) allocate_buffer ();
550 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
552 if (XBUFFER (base_buffer)->base_buffer)
553 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
554 else
555 b->base_buffer = XBUFFER (base_buffer);
557 /* Use the base buffer's text object. */
558 b->text = b->base_buffer->text;
560 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
561 BUF_ZV (b) = BUF_ZV (b->base_buffer);
562 BUF_PT (b) = BUF_PT (b->base_buffer);
563 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
564 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
565 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
567 b->newline_cache = 0;
568 b->width_run_cache = 0;
569 b->width_table = Qnil;
571 /* Put this on the chain of all buffers including killed ones. */
572 b->next = all_buffers;
573 all_buffers = b;
575 name = Fcopy_sequence (name);
576 STRING_SET_INTERVALS (name, NULL_INTERVAL);
577 b->name = name;
579 reset_buffer (b);
580 reset_buffer_local_variables (b, 1);
582 /* Put this in the alist of all live buffers. */
583 XSETBUFFER (buf, b);
584 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
586 b->mark = Fmake_marker ();
587 b->name = name;
589 /* The multibyte status belongs to the base buffer. */
590 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
592 /* Make sure the base buffer has markers for its narrowing. */
593 if (NILP (b->base_buffer->pt_marker))
595 b->base_buffer->pt_marker = Fmake_marker ();
596 set_marker_both (b->base_buffer->pt_marker, base_buffer,
597 BUF_PT (b->base_buffer),
598 BUF_PT_BYTE (b->base_buffer));
600 if (NILP (b->base_buffer->begv_marker))
602 b->base_buffer->begv_marker = Fmake_marker ();
603 set_marker_both (b->base_buffer->begv_marker, base_buffer,
604 BUF_BEGV (b->base_buffer),
605 BUF_BEGV_BYTE (b->base_buffer));
607 if (NILP (b->base_buffer->zv_marker))
609 b->base_buffer->zv_marker = Fmake_marker ();
610 set_marker_both (b->base_buffer->zv_marker, base_buffer,
611 BUF_ZV (b->base_buffer),
612 BUF_ZV_BYTE (b->base_buffer));
613 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
616 if (NILP (clone))
618 /* Give the indirect buffer markers for its narrowing. */
619 b->pt_marker = Fmake_marker ();
620 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
621 b->begv_marker = Fmake_marker ();
622 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
623 b->zv_marker = Fmake_marker ();
624 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
625 XMARKER (b->zv_marker)->insertion_type = 1;
627 else
628 clone_per_buffer_values (b->base_buffer, b);
630 return buf;
633 /* Reinitialize everything about a buffer except its name and contents
634 and local variables. */
636 void
637 reset_buffer (b)
638 register struct buffer *b;
640 b->filename = Qnil;
641 b->file_truename = Qnil;
642 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
643 b->modtime = 0;
644 XSETFASTINT (b->save_length, 0);
645 b->last_window_start = 1;
646 /* It is more conservative to start out "changed" than "unchanged". */
647 b->clip_changed = 0;
648 b->prevent_redisplay_optimizations_p = 1;
649 b->backed_up = Qnil;
650 b->auto_save_modified = 0;
651 b->auto_save_failure_time = -1;
652 b->auto_save_file_name = Qnil;
653 b->read_only = Qnil;
654 b->overlays_before = NULL;
655 b->overlays_after = NULL;
656 b->overlay_center = BEG;
657 b->mark_active = Qnil;
658 b->point_before_scroll = Qnil;
659 b->file_format = Qnil;
660 b->last_selected_window = Qnil;
661 XSETINT (b->display_count, 0);
662 b->display_time = Qnil;
663 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
664 b->cursor_type = buffer_defaults.cursor_type;
665 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
667 b->display_error_modiff = 0;
670 /* Reset buffer B's local variables info.
671 Don't use this on a buffer that has already been in use;
672 it does not treat permanent locals consistently.
673 Instead, use Fkill_all_local_variables.
675 If PERMANENT_TOO is 1, then we reset permanent built-in
676 buffer-local variables. If PERMANENT_TOO is 0,
677 we preserve those. */
679 static void
680 reset_buffer_local_variables (b, permanent_too)
681 register struct buffer *b;
682 int permanent_too;
684 register int offset;
685 int i;
687 /* Reset the major mode to Fundamental, together with all the
688 things that depend on the major mode.
689 default-major-mode is handled at a higher level.
690 We ignore it here. */
691 b->major_mode = Qfundamental_mode;
692 b->keymap = Qnil;
693 b->mode_name = QSFundamental;
694 b->minor_modes = Qnil;
696 /* If the standard case table has been altered and invalidated,
697 fix up its insides first. */
698 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
699 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
700 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
701 Fset_standard_case_table (Vascii_downcase_table);
703 b->downcase_table = Vascii_downcase_table;
704 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
705 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
706 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
707 b->invisibility_spec = Qt;
708 #ifndef DOS_NT
709 b->buffer_file_type = Qnil;
710 #endif
712 /* Reset all (or most) per-buffer variables to their defaults. */
713 b->local_var_alist = Qnil;
714 for (i = 0; i < last_per_buffer_idx; ++i)
715 if (permanent_too || buffer_permanent_local_flags[i] == 0)
716 SET_PER_BUFFER_VALUE_P (b, i, 0);
718 /* For each slot that has a default value,
719 copy that into the slot. */
721 for (offset = PER_BUFFER_VAR_OFFSET (name);
722 offset < sizeof *b;
723 offset += sizeof (Lisp_Object))
725 int idx = PER_BUFFER_IDX (offset);
726 if ((idx > 0
727 && (permanent_too
728 || buffer_permanent_local_flags[idx] == 0))
729 /* Is -2 used anywhere? */
730 || idx == -2)
731 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
735 /* We split this away from generate-new-buffer, because rename-buffer
736 and set-visited-file-name ought to be able to use this to really
737 rename the buffer properly. */
739 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
740 1, 2, 0,
741 doc: /* Return a string that is the name of no existing buffer based on NAME.
742 If there is no live buffer named NAME, then return NAME.
743 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
744 until an unused name is found, and then return that name.
745 Optional second argument IGNORE specifies a name that is okay to use
746 \(if it is in the sequence to be tried)
747 even if a buffer with that name exists. */)
748 (name, ignore)
749 register Lisp_Object name, ignore;
751 register Lisp_Object gentemp, tem;
752 int count;
753 char number[10];
755 CHECK_STRING (name);
757 tem = Fget_buffer (name);
758 if (NILP (tem))
759 return name;
761 count = 1;
762 while (1)
764 sprintf (number, "<%d>", ++count);
765 gentemp = concat2 (name, build_string (number));
766 tem = Fstring_equal (gentemp, ignore);
767 if (!NILP (tem))
768 return gentemp;
769 tem = Fget_buffer (gentemp);
770 if (NILP (tem))
771 return gentemp;
776 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
777 doc: /* Return the name of BUFFER, as a string.
778 With no argument or nil as argument, return the name of the current buffer. */)
779 (buffer)
780 register Lisp_Object buffer;
782 if (NILP (buffer))
783 return current_buffer->name;
784 CHECK_BUFFER (buffer);
785 return XBUFFER (buffer)->name;
788 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
789 doc: /* Return name of file BUFFER is visiting, or nil if none.
790 No argument or nil as argument means use the current buffer. */)
791 (buffer)
792 register Lisp_Object buffer;
794 if (NILP (buffer))
795 return current_buffer->filename;
796 CHECK_BUFFER (buffer);
797 return XBUFFER (buffer)->filename;
800 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
801 0, 1, 0,
802 doc: /* Return the base buffer of indirect buffer BUFFER.
803 If BUFFER is not indirect, return nil. */)
804 (buffer)
805 register Lisp_Object buffer;
807 struct buffer *base;
808 Lisp_Object base_buffer;
810 if (NILP (buffer))
811 base = current_buffer->base_buffer;
812 else
814 CHECK_BUFFER (buffer);
815 base = XBUFFER (buffer)->base_buffer;
818 if (! base)
819 return Qnil;
820 XSETBUFFER (base_buffer, base);
821 return base_buffer;
824 DEFUN ("buffer-local-value", Fbuffer_local_value,
825 Sbuffer_local_value, 2, 2, 0,
826 doc: /* Return the value of VARIABLE in BUFFER.
827 If VARIABLE does not have a buffer-local binding in BUFFER, the value
828 is the default binding of variable. */)
829 (symbol, buffer)
830 register Lisp_Object symbol;
831 register Lisp_Object buffer;
833 register struct buffer *buf;
834 register Lisp_Object result;
836 CHECK_SYMBOL (symbol);
837 CHECK_BUFFER (buffer);
838 buf = XBUFFER (buffer);
840 /* Look in local_var_list */
841 result = Fassoc (symbol, buf->local_var_alist);
842 if (NILP (result))
844 int offset, idx;
845 int found = 0;
847 /* Look in special slots */
848 for (offset = PER_BUFFER_VAR_OFFSET (name);
849 offset < sizeof (struct buffer);
850 /* sizeof EMACS_INT == sizeof Lisp_Object */
851 offset += (sizeof (EMACS_INT)))
853 idx = PER_BUFFER_IDX (offset);
854 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
855 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
856 && EQ (PER_BUFFER_SYMBOL (offset), symbol))
858 result = PER_BUFFER_VALUE (buf, offset);
859 found = 1;
860 break;
864 if (!found)
865 result = Fdefault_value (symbol);
867 else
869 Lisp_Object valcontents;
870 Lisp_Object current_alist_element;
872 /* What binding is loaded right now? */
873 valcontents = SYMBOL_VALUE (symbol);
874 current_alist_element
875 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
877 /* The value of the currently loaded binding is not
878 stored in it, but rather in the realvalue slot.
879 Store that value into the binding it belongs to
880 in case that is the one we are about to use. */
882 Fsetcdr (current_alist_element,
883 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
885 /* Now get the (perhaps updated) value out of the binding. */
886 result = XCDR (result);
889 if (EQ (result, Qunbound))
890 return Fsignal (Qvoid_variable, Fcons (symbol, Qnil));
892 return result;
895 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
896 Sbuffer_local_variables, 0, 1, 0,
897 doc: /* Return an alist of variables that are buffer-local in BUFFER.
898 Most elements look like (SYMBOL . VALUE), describing one variable.
899 For a symbol that is locally unbound, just the symbol appears in the value.
900 Note that storing new VALUEs in these elements doesn't change the variables.
901 No argument or nil as argument means use current buffer as BUFFER. */)
902 (buffer)
903 register Lisp_Object buffer;
905 register struct buffer *buf;
906 register Lisp_Object result;
908 if (NILP (buffer))
909 buf = current_buffer;
910 else
912 CHECK_BUFFER (buffer);
913 buf = XBUFFER (buffer);
916 result = Qnil;
919 register Lisp_Object tail;
920 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
922 Lisp_Object val, elt;
924 elt = XCAR (tail);
926 /* Reference each variable in the alist in buf.
927 If inquiring about the current buffer, this gets the current values,
928 so store them into the alist so the alist is up to date.
929 If inquiring about some other buffer, this swaps out any values
930 for that buffer, making the alist up to date automatically. */
931 val = find_symbol_value (XCAR (elt));
932 /* Use the current buffer value only if buf is the current buffer. */
933 if (buf != current_buffer)
934 val = XCDR (elt);
936 /* If symbol is unbound, put just the symbol in the list. */
937 if (EQ (val, Qunbound))
938 result = Fcons (XCAR (elt), result);
939 /* Otherwise, put (symbol . value) in the list. */
940 else
941 result = Fcons (Fcons (XCAR (elt), val), result);
945 /* Add on all the variables stored in special slots. */
947 int offset, idx;
949 for (offset = PER_BUFFER_VAR_OFFSET (name);
950 offset < sizeof (struct buffer);
951 /* sizeof EMACS_INT == sizeof Lisp_Object */
952 offset += (sizeof (EMACS_INT)))
954 idx = PER_BUFFER_IDX (offset);
955 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
956 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
957 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
958 PER_BUFFER_VALUE (buf, offset)),
959 result);
963 return result;
967 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
968 0, 1, 0,
969 doc: /* Return t if BUFFER was modified since its file was last read or saved.
970 No argument or nil as argument means use current buffer as BUFFER. */)
971 (buffer)
972 register Lisp_Object buffer;
974 register struct buffer *buf;
975 if (NILP (buffer))
976 buf = current_buffer;
977 else
979 CHECK_BUFFER (buffer);
980 buf = XBUFFER (buffer);
983 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
986 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
987 1, 1, 0,
988 doc: /* Mark current buffer as modified or unmodified according to FLAG.
989 A non-nil FLAG means mark the buffer modified. */)
990 (flag)
991 register Lisp_Object flag;
993 register int already;
994 register Lisp_Object fn;
995 Lisp_Object buffer, window;
997 #ifdef CLASH_DETECTION
998 /* If buffer becoming modified, lock the file.
999 If buffer becoming unmodified, unlock the file. */
1001 fn = current_buffer->file_truename;
1002 /* Test buffer-file-name so that binding it to nil is effective. */
1003 if (!NILP (fn) && ! NILP (current_buffer->filename))
1005 already = SAVE_MODIFF < MODIFF;
1006 if (!already && !NILP (flag))
1007 lock_file (fn);
1008 else if (already && NILP (flag))
1009 unlock_file (fn);
1011 #endif /* CLASH_DETECTION */
1013 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1015 /* Set update_mode_lines only if buffer is displayed in some window.
1016 Packages like jit-lock or lazy-lock preserve a buffer's modified
1017 state by recording/restoring the state around blocks of code.
1018 Setting update_mode_lines makes redisplay consider all windows
1019 (on all frames). Stealth fontification of buffers not displayed
1020 would incur additional redisplay costs if we'd set
1021 update_modes_lines unconditionally.
1023 Ideally, I think there should be another mechanism for fontifying
1024 buffers without "modifying" buffers, or redisplay should be
1025 smarter about updating the `*' in mode lines. --gerd */
1026 XSETBUFFER (buffer, current_buffer);
1027 window = Fget_buffer_window (buffer, Qt);
1028 if (WINDOWP (window))
1030 ++update_mode_lines;
1031 current_buffer->prevent_redisplay_optimizations_p = 1;
1034 return flag;
1037 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1038 Srestore_buffer_modified_p, 1, 1, 0,
1039 doc: /* Like `set-buffer-modified-p', with a differences concerning redisplay.
1040 It is not ensured that mode lines will be updated to show the modified
1041 state of the current buffer. Use with care. */)
1042 (flag)
1043 Lisp_Object flag;
1045 #ifdef CLASH_DETECTION
1046 Lisp_Object fn;
1048 /* If buffer becoming modified, lock the file.
1049 If buffer becoming unmodified, unlock the file. */
1051 fn = current_buffer->file_truename;
1052 /* Test buffer-file-name so that binding it to nil is effective. */
1053 if (!NILP (fn) && ! NILP (current_buffer->filename))
1055 int already = SAVE_MODIFF < MODIFF;
1056 if (!already && !NILP (flag))
1057 lock_file (fn);
1058 else if (already && NILP (flag))
1059 unlock_file (fn);
1061 #endif /* CLASH_DETECTION */
1063 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1064 return flag;
1067 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
1068 0, 1, 0,
1069 doc: /* Return BUFFER's tick counter, incremented for each change in text.
1070 Each buffer has a tick counter which is incremented each time the text in
1071 that buffer is changed. It wraps around occasionally.
1072 No argument or nil as argument means use current buffer as BUFFER. */)
1073 (buffer)
1074 register Lisp_Object buffer;
1076 register struct buffer *buf;
1077 if (NILP (buffer))
1078 buf = current_buffer;
1079 else
1081 CHECK_BUFFER (buffer);
1082 buf = XBUFFER (buffer);
1085 return make_number (BUF_MODIFF (buf));
1088 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1089 "sRename buffer (to new name): \nP",
1090 doc: /* Change current buffer's name to NEWNAME (a string).
1091 If second arg UNIQUE is nil or omitted, it is an error if a
1092 buffer named NEWNAME already exists.
1093 If UNIQUE is non-nil, come up with a new name using
1094 `generate-new-buffer-name'.
1095 Interactively, you can set UNIQUE with a prefix argument.
1096 We return the name we actually gave the buffer.
1097 This does not change the name of the visited file (if any). */)
1098 (newname, unique)
1099 register Lisp_Object newname, unique;
1101 register Lisp_Object tem, buf;
1103 CHECK_STRING (newname);
1105 if (SCHARS (newname) == 0)
1106 error ("Empty string is invalid as a buffer name");
1108 tem = Fget_buffer (newname);
1109 if (!NILP (tem))
1111 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1112 rename the buffer automatically so you can create another
1113 with the original name. It makes UNIQUE equivalent to
1114 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1115 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1116 return current_buffer->name;
1117 if (!NILP (unique))
1118 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
1119 else
1120 error ("Buffer name `%s' is in use", SDATA (newname));
1123 current_buffer->name = newname;
1125 /* Catch redisplay's attention. Unless we do this, the mode lines for
1126 any windows displaying current_buffer will stay unchanged. */
1127 update_mode_lines++;
1129 XSETBUFFER (buf, current_buffer);
1130 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
1131 if (NILP (current_buffer->filename)
1132 && !NILP (current_buffer->auto_save_file_name))
1133 call0 (intern ("rename-auto-save-file"));
1134 /* Refetch since that last call may have done GC. */
1135 return current_buffer->name;
1138 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1139 doc: /* Return most recently selected buffer other than BUFFER.
1140 Buffers not visible in windows are preferred to visible buffers,
1141 unless optional second argument VISIBLE-OK is non-nil.
1142 If the optional third argument FRAME is non-nil, use that frame's
1143 buffer list instead of the selected frame's buffer list.
1144 If no other buffer exists, the buffer `*scratch*' is returned.
1145 If BUFFER is omitted or nil, some interesting buffer is returned. */)
1146 (buffer, visible_ok, frame)
1147 register Lisp_Object buffer, visible_ok, frame;
1149 Lisp_Object Fset_buffer_major_mode ();
1150 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1151 notsogood = Qnil;
1153 if (NILP (frame))
1154 frame = selected_frame;
1156 tail = Vbuffer_alist;
1157 pred = frame_buffer_predicate (frame);
1159 /* Consider buffers that have been seen in the selected frame
1160 before other buffers. */
1162 tem = frame_buffer_list (frame);
1163 add_ons = Qnil;
1164 while (CONSP (tem))
1166 if (BUFFERP (XCAR (tem)))
1167 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1168 tem = XCDR (tem);
1170 tail = nconc2 (Fnreverse (add_ons), tail);
1172 for (; CONSP (tail); tail = XCDR (tail))
1174 buf = Fcdr (XCAR (tail));
1175 if (EQ (buf, buffer))
1176 continue;
1177 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1178 continue;
1179 /* If the selected frame has a buffer_predicate,
1180 disregard buffers that don't fit the predicate. */
1181 if (!NILP (pred))
1183 tem = call1 (pred, buf);
1184 if (NILP (tem))
1185 continue;
1188 if (NILP (visible_ok))
1189 tem = Fget_buffer_window (buf, Qvisible);
1190 else
1191 tem = Qnil;
1192 if (NILP (tem))
1193 return buf;
1194 if (NILP (notsogood))
1195 notsogood = buf;
1197 if (!NILP (notsogood))
1198 return notsogood;
1199 buf = Fget_buffer (build_string ("*scratch*"));
1200 if (NILP (buf))
1202 buf = Fget_buffer_create (build_string ("*scratch*"));
1203 Fset_buffer_major_mode (buf);
1205 return buf;
1208 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
1209 0, 1, "",
1210 doc: /* Make BUFFER stop keeping undo information.
1211 No argument or nil as argument means do this for the current buffer. */)
1212 (buffer)
1213 register Lisp_Object buffer;
1215 Lisp_Object real_buffer;
1217 if (NILP (buffer))
1218 XSETBUFFER (real_buffer, current_buffer);
1219 else
1221 real_buffer = Fget_buffer (buffer);
1222 if (NILP (real_buffer))
1223 nsberror (buffer);
1226 XBUFFER (real_buffer)->undo_list = Qt;
1228 return Qnil;
1231 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1232 0, 1, "",
1233 doc: /* Start keeping undo information for buffer BUFFER.
1234 No argument or nil as argument means do this for the current buffer. */)
1235 (buffer)
1236 register Lisp_Object buffer;
1238 Lisp_Object real_buffer;
1240 if (NILP (buffer))
1241 XSETBUFFER (real_buffer, current_buffer);
1242 else
1244 real_buffer = Fget_buffer (buffer);
1245 if (NILP (real_buffer))
1246 nsberror (buffer);
1249 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1250 XBUFFER (real_buffer)->undo_list = Qnil;
1252 return Qnil;
1256 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1257 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1258 The buffer being killed will be current while the hook is running.\n\
1259 See `kill-buffer'."
1261 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1262 doc: /* Kill the buffer BUFFER.
1263 The argument may be a buffer or may be the name of a buffer.
1264 An argument of nil means kill the current buffer.
1266 Value is t if the buffer is actually killed, nil if user says no.
1268 The value of `kill-buffer-hook' (which may be local to that buffer),
1269 if not void, is a list of functions to be called, with no arguments,
1270 before the buffer is actually killed. The buffer to be killed is current
1271 when the hook functions are called.
1273 Any processes that have this buffer as the `process-buffer' are killed
1274 with SIGHUP. */)
1275 (buffer)
1276 Lisp_Object buffer;
1278 Lisp_Object buf;
1279 register struct buffer *b;
1280 register Lisp_Object tem;
1281 register struct Lisp_Marker *m;
1282 struct gcpro gcpro1;
1284 if (NILP (buffer))
1285 buf = Fcurrent_buffer ();
1286 else
1287 buf = Fget_buffer (buffer);
1288 if (NILP (buf))
1289 nsberror (buffer);
1291 b = XBUFFER (buf);
1293 /* Avoid trouble for buffer already dead. */
1294 if (NILP (b->name))
1295 return Qnil;
1297 /* Query if the buffer is still modified. */
1298 if (INTERACTIVE && !NILP (b->filename)
1299 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1301 GCPRO1 (buf);
1302 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1303 b->name, make_number (0)));
1304 UNGCPRO;
1305 if (NILP (tem))
1306 return Qnil;
1309 /* Run hooks with the buffer to be killed the current buffer. */
1311 int count = SPECPDL_INDEX ();
1312 Lisp_Object arglist[1];
1314 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1315 set_buffer_internal (b);
1317 /* First run the query functions; if any query is answered no,
1318 don't kill the buffer. */
1319 arglist[0] = Qkill_buffer_query_functions;
1320 if (NILP (Frun_hook_with_args_until_failure (1, arglist)))
1321 return unbind_to (count, Qnil);
1323 /* Then run the hooks. */
1324 Frun_hooks (1, &Qkill_buffer_hook);
1325 unbind_to (count, Qnil);
1328 /* We have no more questions to ask. Verify that it is valid
1329 to kill the buffer. This must be done after the questions
1330 since anything can happen within do_yes_or_no_p. */
1332 /* Don't kill the minibuffer now current. */
1333 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1334 return Qnil;
1336 if (NILP (b->name))
1337 return Qnil;
1339 /* When we kill a base buffer, kill all its indirect buffers.
1340 We do it at this stage so nothing terrible happens if they
1341 ask questions or their hooks get errors. */
1342 if (! b->base_buffer)
1344 struct buffer *other;
1346 GCPRO1 (buf);
1348 for (other = all_buffers; other; other = other->next)
1349 /* all_buffers contains dead buffers too;
1350 don't re-kill them. */
1351 if (other->base_buffer == b && !NILP (other->name))
1353 Lisp_Object buf;
1354 XSETBUFFER (buf, other);
1355 Fkill_buffer (buf);
1358 UNGCPRO;
1361 /* Make this buffer not be current.
1362 In the process, notice if this is the sole visible buffer
1363 and give up if so. */
1364 if (b == current_buffer)
1366 tem = Fother_buffer (buf, Qnil, Qnil);
1367 Fset_buffer (tem);
1368 if (b == current_buffer)
1369 return Qnil;
1372 /* Notice if the buffer to kill is the sole visible buffer
1373 when we're currently in the mini-buffer, and give up if so. */
1374 XSETBUFFER (tem, current_buffer);
1375 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1377 tem = Fother_buffer (buf, Qnil, Qnil);
1378 if (EQ (buf, tem))
1379 return Qnil;
1382 /* Now there is no question: we can kill the buffer. */
1384 #ifdef CLASH_DETECTION
1385 /* Unlock this buffer's file, if it is locked. */
1386 unlock_buffer (b);
1387 #endif /* CLASH_DETECTION */
1389 kill_buffer_processes (buf);
1390 clear_charpos_cache (b);
1392 tem = Vinhibit_quit;
1393 Vinhibit_quit = Qt;
1394 replace_buffer_in_all_windows (buf);
1395 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1396 frames_discard_buffer (buf);
1397 Vinhibit_quit = tem;
1399 /* Delete any auto-save file, if we saved it in this session.
1400 But not if the buffer is modified. */
1401 if (STRINGP (b->auto_save_file_name)
1402 && b->auto_save_modified != 0
1403 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
1404 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
1406 Lisp_Object tem;
1407 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1408 if (! NILP (tem))
1409 internal_delete_file (b->auto_save_file_name);
1412 if (b->base_buffer)
1414 /* Unchain all markers that belong to this indirect buffer.
1415 Don't unchain the markers that belong to the base buffer
1416 or its other indirect buffers. */
1417 for (m = BUF_MARKERS (b); m; )
1419 struct Lisp_Marker *next = m->next;
1420 if (m->buffer == b)
1421 unchain_marker (m);
1422 m = next;
1425 else
1427 /* Unchain all markers of this buffer and its indirect buffers.
1428 and leave them pointing nowhere. */
1429 for (m = BUF_MARKERS (b); m; )
1431 struct Lisp_Marker *next = m->next;
1432 m->buffer = 0;
1433 m->next = NULL;
1434 m = next;
1436 BUF_MARKERS (b) = NULL;
1437 BUF_INTERVALS (b) = NULL_INTERVAL;
1439 /* Perhaps we should explicitly free the interval tree here... */
1442 /* Reset the local variables, so that this buffer's local values
1443 won't be protected from GC. They would be protected
1444 if they happened to remain encached in their symbols.
1445 This gets rid of them for certain. */
1446 swap_out_buffer_local_variables (b);
1447 reset_buffer_local_variables (b, 1);
1449 b->name = Qnil;
1451 BLOCK_INPUT;
1452 if (! b->base_buffer)
1453 free_buffer_text (b);
1455 if (b->newline_cache)
1457 free_region_cache (b->newline_cache);
1458 b->newline_cache = 0;
1460 if (b->width_run_cache)
1462 free_region_cache (b->width_run_cache);
1463 b->width_run_cache = 0;
1465 b->width_table = Qnil;
1466 UNBLOCK_INPUT;
1467 b->undo_list = Qnil;
1469 return Qt;
1472 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1473 we do this each time BUF is selected visibly, the more recently
1474 selected buffers are always closer to the front of the list. This
1475 means that other_buffer is more likely to choose a relevant buffer. */
1477 void
1478 record_buffer (buf)
1479 Lisp_Object buf;
1481 register Lisp_Object link, prev;
1482 Lisp_Object frame;
1483 frame = selected_frame;
1485 prev = Qnil;
1486 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1488 if (EQ (XCDR (XCAR (link)), buf))
1489 break;
1490 prev = link;
1493 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1494 we cannot use Fdelq itself here because it allows quitting. */
1496 if (NILP (prev))
1497 Vbuffer_alist = XCDR (Vbuffer_alist);
1498 else
1499 XSETCDR (prev, XCDR (XCDR (prev)));
1501 XSETCDR (link, Vbuffer_alist);
1502 Vbuffer_alist = link;
1504 /* Now move this buffer to the front of frame_buffer_list also. */
1506 prev = Qnil;
1507 for (link = frame_buffer_list (frame); CONSP (link);
1508 link = XCDR (link))
1510 if (EQ (XCAR (link), buf))
1511 break;
1512 prev = link;
1515 /* Effectively do delq. */
1517 if (CONSP (link))
1519 if (NILP (prev))
1520 set_frame_buffer_list (frame,
1521 XCDR (frame_buffer_list (frame)));
1522 else
1523 XSETCDR (prev, XCDR (XCDR (prev)));
1525 XSETCDR (link, frame_buffer_list (frame));
1526 set_frame_buffer_list (frame, link);
1528 else
1529 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1532 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1533 doc: /* Set an appropriate major mode for BUFFER.
1534 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
1535 according to `default-major-mode'.
1536 Use this function before selecting the buffer, since it may need to inspect
1537 the current buffer's major mode. */)
1538 (buffer)
1539 Lisp_Object buffer;
1541 int count;
1542 Lisp_Object function;
1544 if (STRINGP (XBUFFER (buffer)->name)
1545 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
1546 function = find_symbol_value (intern ("initial-major-mode"));
1547 else
1549 function = buffer_defaults.major_mode;
1550 if (NILP (function)
1551 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1552 function = current_buffer->major_mode;
1555 if (NILP (function) || EQ (function, Qfundamental_mode))
1556 return Qnil;
1558 count = SPECPDL_INDEX ();
1560 /* To select a nonfundamental mode,
1561 select the buffer temporarily and then call the mode function. */
1563 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1565 Fset_buffer (buffer);
1566 call0 (function);
1568 return unbind_to (count, Qnil);
1571 /* If switching buffers in WINDOW would be an error, return
1572 a C string saying what the error would be. */
1574 char *
1575 no_switch_window (window)
1576 Lisp_Object window;
1578 Lisp_Object tem;
1579 if (EQ (minibuf_window, window))
1580 return "Cannot switch buffers in minibuffer window";
1581 tem = Fwindow_dedicated_p (window);
1582 if (EQ (tem, Qt))
1583 return "Cannot switch buffers in a dedicated window";
1584 return NULL;
1587 /* Switch to buffer BUFFER in the selected window.
1588 If NORECORD is non-nil, don't call record_buffer. */
1590 Lisp_Object
1591 switch_to_buffer_1 (buffer, norecord)
1592 Lisp_Object buffer, norecord;
1594 register Lisp_Object buf;
1596 if (NILP (buffer))
1597 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1598 else
1600 buf = Fget_buffer (buffer);
1601 if (NILP (buf))
1603 buf = Fget_buffer_create (buffer);
1604 Fset_buffer_major_mode (buf);
1607 Fset_buffer (buf);
1608 if (NILP (norecord))
1609 record_buffer (buf);
1611 Fset_window_buffer (EQ (selected_window, minibuf_window)
1612 ? Fnext_window (minibuf_window, Qnil, Qnil)
1613 : selected_window,
1614 buf, Qnil);
1616 return buf;
1619 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1620 doc: /* Select buffer BUFFER in the current window.
1621 BUFFER may be a buffer or a buffer name.
1622 Optional second arg NORECORD non-nil means
1623 do not put this buffer at the front of the list of recently selected ones.
1625 WARNING: This is NOT the way to work on another buffer temporarily
1626 within a Lisp program! Use `set-buffer' instead. That avoids messing with
1627 the window-buffer correspondences. */)
1628 (buffer, norecord)
1629 Lisp_Object buffer, norecord;
1631 char *err;
1633 err = no_switch_window (selected_window);
1634 if (err) error (err);
1636 return switch_to_buffer_1 (buffer, norecord);
1639 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1640 doc: /* Select buffer BUFFER in some window, preferably a different one.
1641 If BUFFER is nil, then some other buffer is chosen.
1642 If `pop-up-windows' is non-nil, windows can be split to do this.
1643 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
1644 window even if BUFFER is already visible in the selected window.
1645 This uses the function `display-buffer' as a subroutine; see the documentation
1646 of `display-buffer' for additional customization information.
1648 Optional third arg NORECORD non-nil means
1649 do not put this buffer at the front of the list of recently selected ones. */)
1650 (buffer, other_window, norecord)
1651 Lisp_Object buffer, other_window, norecord;
1653 register Lisp_Object buf;
1654 if (NILP (buffer))
1655 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1656 else
1658 buf = Fget_buffer (buffer);
1659 if (NILP (buf))
1661 buf = Fget_buffer_create (buffer);
1662 Fset_buffer_major_mode (buf);
1665 Fset_buffer (buf);
1666 if (NILP (norecord))
1667 /* Why bother ? Fselect_window will do it for us anyway. -stef */
1668 record_buffer (buf);
1669 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
1670 return buf;
1673 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1674 doc: /* Return the current buffer as a Lisp object. */)
1677 register Lisp_Object buf;
1678 XSETBUFFER (buf, current_buffer);
1679 return buf;
1682 /* Set the current buffer to B.
1684 We previously set windows_or_buffers_changed here to invalidate
1685 global unchanged information in beg_unchanged and end_unchanged.
1686 This is no longer necessary because we now compute unchanged
1687 information on a buffer-basis. Every action affecting other
1688 windows than the selected one requires a select_window at some
1689 time, and that increments windows_or_buffers_changed. */
1691 void
1692 set_buffer_internal (b)
1693 register struct buffer *b;
1695 if (current_buffer != b)
1696 set_buffer_internal_1 (b);
1699 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1700 This is used by redisplay. */
1702 void
1703 set_buffer_internal_1 (b)
1704 register struct buffer *b;
1706 register struct buffer *old_buf;
1707 register Lisp_Object tail, valcontents;
1708 Lisp_Object tem;
1710 #ifdef USE_MMAP_FOR_BUFFERS
1711 if (b->text->beg == NULL)
1712 enlarge_buffer_text (b, 0);
1713 #endif /* USE_MMAP_FOR_BUFFERS */
1715 if (current_buffer == b)
1716 return;
1718 old_buf = current_buffer;
1719 current_buffer = b;
1720 last_known_column_point = -1; /* invalidate indentation cache */
1722 if (old_buf)
1724 /* Put the undo list back in the base buffer, so that it appears
1725 that an indirect buffer shares the undo list of its base. */
1726 if (old_buf->base_buffer)
1727 old_buf->base_buffer->undo_list = old_buf->undo_list;
1729 /* If the old current buffer has markers to record PT, BEGV and ZV
1730 when it is not current, update them now. */
1731 if (! NILP (old_buf->pt_marker))
1733 Lisp_Object obuf;
1734 XSETBUFFER (obuf, old_buf);
1735 set_marker_both (old_buf->pt_marker, obuf,
1736 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1738 if (! NILP (old_buf->begv_marker))
1740 Lisp_Object obuf;
1741 XSETBUFFER (obuf, old_buf);
1742 set_marker_both (old_buf->begv_marker, obuf,
1743 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1745 if (! NILP (old_buf->zv_marker))
1747 Lisp_Object obuf;
1748 XSETBUFFER (obuf, old_buf);
1749 set_marker_both (old_buf->zv_marker, obuf,
1750 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1754 /* Get the undo list from the base buffer, so that it appears
1755 that an indirect buffer shares the undo list of its base. */
1756 if (b->base_buffer)
1757 b->undo_list = b->base_buffer->undo_list;
1759 /* If the new current buffer has markers to record PT, BEGV and ZV
1760 when it is not current, fetch them now. */
1761 if (! NILP (b->pt_marker))
1763 BUF_PT (b) = marker_position (b->pt_marker);
1764 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1766 if (! NILP (b->begv_marker))
1768 BUF_BEGV (b) = marker_position (b->begv_marker);
1769 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1771 if (! NILP (b->zv_marker))
1773 BUF_ZV (b) = marker_position (b->zv_marker);
1774 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1777 /* Look down buffer's list of local Lisp variables
1778 to find and update any that forward into C variables. */
1780 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1782 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1783 if ((BUFFER_LOCAL_VALUEP (valcontents)
1784 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1785 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1786 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1787 /* Just reference the variable
1788 to cause it to become set for this buffer. */
1789 Fsymbol_value (XCAR (XCAR (tail)));
1792 /* Do the same with any others that were local to the previous buffer */
1794 if (old_buf)
1795 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1797 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
1798 if ((BUFFER_LOCAL_VALUEP (valcontents)
1799 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1800 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1801 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1802 /* Just reference the variable
1803 to cause it to become set for this buffer. */
1804 Fsymbol_value (XCAR (XCAR (tail)));
1808 /* Switch to buffer B temporarily for redisplay purposes.
1809 This avoids certain things that don't need to be done within redisplay. */
1811 void
1812 set_buffer_temp (b)
1813 struct buffer *b;
1815 register struct buffer *old_buf;
1817 if (current_buffer == b)
1818 return;
1820 old_buf = current_buffer;
1821 current_buffer = b;
1823 if (old_buf)
1825 /* If the old current buffer has markers to record PT, BEGV and ZV
1826 when it is not current, update them now. */
1827 if (! NILP (old_buf->pt_marker))
1829 Lisp_Object obuf;
1830 XSETBUFFER (obuf, old_buf);
1831 set_marker_both (old_buf->pt_marker, obuf,
1832 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1834 if (! NILP (old_buf->begv_marker))
1836 Lisp_Object obuf;
1837 XSETBUFFER (obuf, old_buf);
1838 set_marker_both (old_buf->begv_marker, obuf,
1839 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1841 if (! NILP (old_buf->zv_marker))
1843 Lisp_Object obuf;
1844 XSETBUFFER (obuf, old_buf);
1845 set_marker_both (old_buf->zv_marker, obuf,
1846 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1850 /* If the new current buffer has markers to record PT, BEGV and ZV
1851 when it is not current, fetch them now. */
1852 if (! NILP (b->pt_marker))
1854 BUF_PT (b) = marker_position (b->pt_marker);
1855 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1857 if (! NILP (b->begv_marker))
1859 BUF_BEGV (b) = marker_position (b->begv_marker);
1860 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1862 if (! NILP (b->zv_marker))
1864 BUF_ZV (b) = marker_position (b->zv_marker);
1865 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1869 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1870 doc: /* Make the buffer BUFFER current for editing operations.
1871 BUFFER may be a buffer or the name of an existing buffer.
1872 See also `save-excursion' when you want to make a buffer current temporarily.
1873 This function does not display the buffer, so its effect ends
1874 when the current command terminates.
1875 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
1876 (buffer)
1877 register Lisp_Object buffer;
1879 register Lisp_Object buf;
1880 buf = Fget_buffer (buffer);
1881 if (NILP (buf))
1882 nsberror (buffer);
1883 if (NILP (XBUFFER (buf)->name))
1884 error ("Selecting deleted buffer");
1885 set_buffer_internal (XBUFFER (buf));
1886 return buf;
1889 /* Set the current buffer to BUFFER provided it is alive. */
1891 Lisp_Object
1892 set_buffer_if_live (buffer)
1893 Lisp_Object buffer;
1895 if (! NILP (XBUFFER (buffer)->name))
1896 Fset_buffer (buffer);
1897 return Qnil;
1900 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1901 Sbarf_if_buffer_read_only, 0, 0, 0,
1902 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
1905 if (!NILP (current_buffer->read_only)
1906 && NILP (Vinhibit_read_only))
1907 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1908 return Qnil;
1911 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1912 doc: /* Put BUFFER at the end of the list of all buffers.
1913 There it is the least likely candidate for `other-buffer' to return;
1914 thus, the least likely buffer for \\[switch-to-buffer] to select by default.
1915 If BUFFER is nil or omitted, bury the current buffer.
1916 Also, if BUFFER is nil or omitted, remove the current buffer from the
1917 selected window if it is displayed there. */)
1918 (buffer)
1919 register Lisp_Object buffer;
1921 /* Figure out what buffer we're going to bury. */
1922 if (NILP (buffer))
1924 Lisp_Object tem;
1925 XSETBUFFER (buffer, current_buffer);
1927 tem = Fwindow_buffer (selected_window);
1928 /* If we're burying the current buffer, unshow it. */
1929 if (EQ (buffer, tem))
1931 if (NILP (Fwindow_dedicated_p (selected_window)))
1932 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
1933 else if (NILP (XWINDOW (selected_window)->parent))
1934 Ficonify_frame (Fwindow_frame (selected_window));
1935 else
1936 Fdelete_window (selected_window);
1939 else
1941 Lisp_Object buf1;
1943 buf1 = Fget_buffer (buffer);
1944 if (NILP (buf1))
1945 nsberror (buffer);
1946 buffer = buf1;
1949 /* Move buffer to the end of the buffer list. Do nothing if the
1950 buffer is killed. */
1951 if (!NILP (XBUFFER (buffer)->name))
1953 Lisp_Object aelt, link;
1955 aelt = Frassq (buffer, Vbuffer_alist);
1956 link = Fmemq (aelt, Vbuffer_alist);
1957 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1958 XSETCDR (link, Qnil);
1959 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1961 /* Removing BUFFER from frame-specific lists
1962 has the effect of putting BUFFER at the end
1963 of the combined list in each frame. */
1964 frames_discard_buffer (buffer);
1967 return Qnil;
1970 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1971 doc: /* Delete the entire contents of the current buffer.
1972 Any narrowing restriction in effect (see `narrow-to-region') is removed,
1973 so the buffer is truly empty after this. */)
1976 Fwiden ();
1978 del_range (BEG, Z);
1980 current_buffer->last_window_start = 1;
1981 /* Prevent warnings, or suspension of auto saving, that would happen
1982 if future size is less than past size. Use of erase-buffer
1983 implies that the future text is not really related to the past text. */
1984 XSETFASTINT (current_buffer->save_length, 0);
1985 return Qnil;
1988 void
1989 validate_region (b, e)
1990 register Lisp_Object *b, *e;
1992 CHECK_NUMBER_COERCE_MARKER (*b);
1993 CHECK_NUMBER_COERCE_MARKER (*e);
1995 if (XINT (*b) > XINT (*e))
1997 Lisp_Object tem;
1998 tem = *b; *b = *e; *e = tem;
2001 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2002 && XINT (*e) <= ZV))
2003 args_out_of_range (*b, *e);
2006 /* Advance BYTE_POS up to a character boundary
2007 and return the adjusted position. */
2009 static int
2010 advance_to_char_boundary (byte_pos)
2011 int byte_pos;
2013 int c;
2015 if (byte_pos == BEG)
2016 /* Beginning of buffer is always a character boundary. */
2017 return BEG;
2019 c = FETCH_BYTE (byte_pos);
2020 if (! CHAR_HEAD_P (c))
2022 /* We should advance BYTE_POS only when C is a constituent of a
2023 multibyte sequence. */
2024 int orig_byte_pos = byte_pos;
2028 byte_pos--;
2029 c = FETCH_BYTE (byte_pos);
2031 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
2032 INC_POS (byte_pos);
2033 if (byte_pos < orig_byte_pos)
2034 byte_pos = orig_byte_pos;
2035 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2036 surely advance to the correct character boundary. If C is
2037 not, BYTE_POS was unchanged. */
2040 return byte_pos;
2043 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2044 1, 1, 0,
2045 doc: /* Set the multibyte flag of the current buffer to FLAG.
2046 If FLAG is t, this makes the buffer a multibyte buffer.
2047 If FLAG is nil, this makes the buffer a single-byte buffer.
2048 The buffer contents remain unchanged as a sequence of bytes
2049 but the contents viewed as characters do change. */)
2050 (flag)
2051 Lisp_Object flag;
2053 struct Lisp_Marker *tail, *markers;
2054 struct buffer *other;
2055 int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
2056 int begv, zv;
2057 int narrowed = (BEG != BEGV || Z != ZV);
2058 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2060 if (current_buffer->base_buffer)
2061 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2063 /* Do nothing if nothing actually changes. */
2064 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2065 return flag;
2067 /* It would be better to update the list,
2068 but this is good enough for now. */
2069 if (undo_enabled_p)
2070 current_buffer->undo_list = Qt;
2072 /* If the cached position is for this buffer, clear it out. */
2073 clear_charpos_cache (current_buffer);
2075 if (NILP (flag))
2076 begv = BEGV_BYTE, zv = ZV_BYTE;
2077 else
2078 begv = BEGV, zv = ZV;
2080 if (narrowed)
2081 Fwiden ();
2083 if (NILP (flag))
2085 int pos, stop;
2086 unsigned char *p;
2088 /* Do this first, so it can use CHAR_TO_BYTE
2089 to calculate the old correspondences. */
2090 set_intervals_multibyte (0);
2092 current_buffer->enable_multibyte_characters = Qnil;
2094 Z = Z_BYTE;
2095 BEGV = BEGV_BYTE;
2096 ZV = ZV_BYTE;
2097 GPT = GPT_BYTE;
2098 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2101 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2102 tail->charpos = tail->bytepos;
2104 /* Convert multibyte form of 8-bit characters to unibyte. */
2105 pos = BEG;
2106 stop = GPT;
2107 p = BEG_ADDR;
2108 while (1)
2110 int c, bytes;
2112 if (pos == stop)
2114 if (pos == Z)
2115 break;
2116 p = GAP_END_ADDR;
2117 stop = Z;
2119 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
2120 p += bytes, pos += bytes;
2121 else
2123 c = STRING_CHAR (p, stop - pos);
2124 /* Delete all bytes for this 8-bit character but the
2125 last one, and change the last one to the charcter
2126 code. */
2127 bytes--;
2128 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2129 p = GAP_END_ADDR;
2130 *p++ = c;
2131 pos++;
2132 if (begv > pos)
2133 begv -= bytes;
2134 if (zv > pos)
2135 zv -= bytes;
2136 stop = Z;
2139 if (narrowed)
2140 Fnarrow_to_region (make_number (begv), make_number (zv));
2142 else
2144 int pt = PT;
2145 int pos, stop;
2146 unsigned char *p;
2148 /* Be sure not to have a multibyte sequence striding over the GAP.
2149 Ex: We change this: "...abc\201 _GAP_ \241def..."
2150 to: "...abc _GAP_ \201\241def..." */
2152 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2153 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2155 unsigned char *p = GPT_ADDR - 1;
2157 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2158 if (BASE_LEADING_CODE_P (*p))
2160 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2162 move_gap_both (new_gpt, new_gpt);
2166 /* Make the buffer contents valid as multibyte by converting
2167 8-bit characters to multibyte form. */
2168 pos = BEG;
2169 stop = GPT;
2170 p = BEG_ADDR;
2171 while (1)
2173 int bytes;
2175 if (pos == stop)
2177 if (pos == Z)
2178 break;
2179 p = GAP_END_ADDR;
2180 stop = Z;
2183 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2184 p += bytes, pos += bytes;
2185 else
2187 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2189 bytes = CHAR_STRING (*p, tmp);
2190 *p = tmp[0];
2191 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2192 bytes--;
2193 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2194 /* Now the gap is after the just inserted data. */
2195 pos = GPT;
2196 p = GAP_END_ADDR;
2197 if (pos <= begv)
2198 begv += bytes;
2199 if (pos <= zv)
2200 zv += bytes;
2201 if (pos <= pt)
2202 pt += bytes;
2203 stop = Z;
2207 if (pt != PT)
2208 TEMP_SET_PT (pt);
2210 if (narrowed)
2211 Fnarrow_to_region (make_number (begv), make_number (zv));
2213 /* Do this first, so that chars_in_text asks the right question.
2214 set_intervals_multibyte needs it too. */
2215 current_buffer->enable_multibyte_characters = Qt;
2217 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2218 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2220 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2222 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2223 if (BEGV_BYTE > GPT_BYTE)
2224 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2225 else
2226 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2228 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2229 if (ZV_BYTE > GPT_BYTE)
2230 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2231 else
2232 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2235 int pt_byte = advance_to_char_boundary (PT_BYTE);
2236 int pt;
2238 if (pt_byte > GPT_BYTE)
2239 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2240 else
2241 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2242 TEMP_SET_PT_BOTH (pt, pt_byte);
2245 tail = markers = BUF_MARKERS (current_buffer);
2247 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2248 getting confused by the markers that have not yet been updated.
2249 It is also a signal that it should never create a marker. */
2250 BUF_MARKERS (current_buffer) = NULL;
2252 for (; tail; tail = tail->next)
2254 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2255 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2258 /* Make sure no markers were put on the chain
2259 while the chain value was incorrect. */
2260 if (BUF_MARKERS (current_buffer))
2261 abort ();
2263 BUF_MARKERS (current_buffer) = markers;
2265 /* Do this last, so it can calculate the new correspondences
2266 between chars and bytes. */
2267 set_intervals_multibyte (1);
2270 if (undo_enabled_p)
2271 current_buffer->undo_list = Qnil;
2273 /* Changing the multibyteness of a buffer means that all windows
2274 showing that buffer must be updated thoroughly. */
2275 current_buffer->prevent_redisplay_optimizations_p = 1;
2276 ++windows_or_buffers_changed;
2278 /* Copy this buffer's new multibyte status
2279 into all of its indirect buffers. */
2280 for (other = all_buffers; other; other = other->next)
2281 if (other->base_buffer == current_buffer && !NILP (other->name))
2283 other->enable_multibyte_characters
2284 = current_buffer->enable_multibyte_characters;
2285 other->prevent_redisplay_optimizations_p = 1;
2288 /* Restore the modifiedness of the buffer. */
2289 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2290 Fset_buffer_modified_p (Qnil);
2292 #ifdef subprocesses
2293 /* Update coding systems of this buffer's process (if any). */
2295 Lisp_Object process;
2297 process = Fget_buffer_process (Fcurrent_buffer ());
2298 if (PROCESSP (process))
2299 setup_process_coding_systems (process);
2301 #endif /* subprocesses */
2303 return flag;
2306 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2307 0, 0, 0,
2308 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2309 Most local variable bindings are eliminated so that the default values
2310 become effective once more. Also, the syntax table is set from
2311 `standard-syntax-table', the local keymap is set to nil,
2312 and the abbrev table from `fundamental-mode-abbrev-table'.
2313 This function also forces redisplay of the mode line.
2315 Every function to select a new major mode starts by
2316 calling this function.
2318 As a special exception, local variables whose names have
2319 a non-nil `permanent-local' property are not eliminated by this function.
2321 The first thing this function does is run
2322 the normal hook `change-major-mode-hook'. */)
2325 register Lisp_Object alist, sym, tem;
2326 Lisp_Object oalist;
2328 if (!NILP (Vrun_hooks))
2329 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
2330 oalist = current_buffer->local_var_alist;
2332 /* Make sure none of the bindings in oalist
2333 remain swapped in, in their symbols. */
2335 swap_out_buffer_local_variables (current_buffer);
2337 /* Actually eliminate all local bindings of this buffer. */
2339 reset_buffer_local_variables (current_buffer, 0);
2341 /* Any which are supposed to be permanent,
2342 make local again, with the same values they had. */
2344 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2346 sym = XCAR (XCAR (alist));
2347 tem = Fget (sym, Qpermanent_local);
2348 if (! NILP (tem))
2350 Fmake_local_variable (sym);
2351 Fset (sym, XCDR (XCAR (alist)));
2355 /* Force mode-line redisplay. Useful here because all major mode
2356 commands call this function. */
2357 update_mode_lines++;
2359 return Qnil;
2362 /* Make sure no local variables remain set up with buffer B
2363 for their current values. */
2365 static void
2366 swap_out_buffer_local_variables (b)
2367 struct buffer *b;
2369 Lisp_Object oalist, alist, sym, tem, buffer;
2371 XSETBUFFER (buffer, b);
2372 oalist = b->local_var_alist;
2374 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2376 sym = XCAR (XCAR (alist));
2378 /* Need not do anything if some other buffer's binding is now encached. */
2379 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2380 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2382 /* Symbol is set up for this buffer's old local value.
2383 Set it up for the current buffer with the default value. */
2385 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->cdr;
2386 /* Store the symbol's current value into the alist entry
2387 it is currently set up for. This is so that, if the
2388 local is marked permanent, and we make it local again
2389 later in Fkill_all_local_variables, we don't lose the value. */
2390 XSETCDR (XCAR (tem),
2391 do_symval_forwarding (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue));
2392 /* Switch to the symbol's default-value alist entry. */
2393 XSETCAR (tem, tem);
2394 /* Mark it as current for buffer B. */
2395 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer = buffer;
2396 /* Store the current value into any forwarding in the symbol. */
2397 store_symval_forwarding (sym,
2398 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue,
2399 XCDR (tem), NULL);
2404 /* Find all the overlays in the current buffer that contain position POS.
2405 Return the number found, and store them in a vector in *VEC_PTR.
2406 Store in *LEN_PTR the size allocated for the vector.
2407 Store in *NEXT_PTR the next position after POS where an overlay starts,
2408 or ZV if there are no more overlays.
2409 Store in *PREV_PTR the previous position before POS where an overlay ends,
2410 or where an overlay starts which ends at or after POS;
2411 or BEGV if there are no such overlays.
2412 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2414 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2415 when this function is called.
2417 If EXTEND is non-zero, we make the vector bigger if necessary.
2418 If EXTEND is zero, we never extend the vector,
2419 and we store only as many overlays as will fit.
2420 But we still return the total number of overlays.
2422 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2423 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2424 default (BEGV or ZV). */
2427 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2428 EMACS_INT pos;
2429 int extend;
2430 Lisp_Object **vec_ptr;
2431 int *len_ptr;
2432 int *next_ptr;
2433 int *prev_ptr;
2434 int change_req;
2436 Lisp_Object overlay, start, end;
2437 struct Lisp_Overlay *tail;
2438 int idx = 0;
2439 int len = *len_ptr;
2440 Lisp_Object *vec = *vec_ptr;
2441 int next = ZV;
2442 int prev = BEGV;
2443 int inhibit_storing = 0;
2445 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2447 int startpos, endpos;
2449 XSETMISC (overlay, tail);
2451 start = OVERLAY_START (overlay);
2452 end = OVERLAY_END (overlay);
2453 endpos = OVERLAY_POSITION (end);
2454 if (endpos < pos)
2456 if (prev < endpos)
2457 prev = endpos;
2458 break;
2460 startpos = OVERLAY_POSITION (start);
2461 /* This one ends at or after POS
2462 so its start counts for PREV_PTR if it's before POS. */
2463 if (prev < startpos && startpos < pos)
2464 prev = startpos;
2465 if (endpos == pos)
2466 continue;
2467 if (startpos <= pos)
2469 if (idx == len)
2471 /* The supplied vector is full.
2472 Either make it bigger, or don't store any more in it. */
2473 if (extend)
2475 /* Make it work with an initial len == 0. */
2476 len *= 2;
2477 if (len == 0)
2478 len = 4;
2479 *len_ptr = len;
2480 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2481 *vec_ptr = vec;
2483 else
2484 inhibit_storing = 1;
2487 if (!inhibit_storing)
2488 vec[idx] = overlay;
2489 /* Keep counting overlays even if we can't return them all. */
2490 idx++;
2492 else if (startpos < next)
2493 next = startpos;
2496 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2498 int startpos, endpos;
2500 XSETMISC (overlay, tail);
2502 start = OVERLAY_START (overlay);
2503 end = OVERLAY_END (overlay);
2504 startpos = OVERLAY_POSITION (start);
2505 if (pos < startpos)
2507 if (startpos < next)
2508 next = startpos;
2509 break;
2511 endpos = OVERLAY_POSITION (end);
2512 if (pos < endpos)
2514 if (idx == len)
2516 if (extend)
2518 /* Make it work with an initial len == 0. */
2519 len *= 2;
2520 if (len == 0)
2521 len = 4;
2522 *len_ptr = len;
2523 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2524 *vec_ptr = vec;
2526 else
2527 inhibit_storing = 1;
2530 if (!inhibit_storing)
2531 vec[idx] = overlay;
2532 idx++;
2534 if (startpos < pos && startpos > prev)
2535 prev = startpos;
2537 else if (endpos < pos && endpos > prev)
2538 prev = endpos;
2539 else if (endpos == pos && startpos > prev
2540 && (!change_req || startpos < pos))
2541 prev = startpos;
2544 if (next_ptr)
2545 *next_ptr = next;
2546 if (prev_ptr)
2547 *prev_ptr = prev;
2548 return idx;
2551 /* Find all the overlays in the current buffer that overlap the range BEG-END
2552 or are empty at BEG.
2554 Return the number found, and store them in a vector in *VEC_PTR.
2555 Store in *LEN_PTR the size allocated for the vector.
2556 Store in *NEXT_PTR the next position after POS where an overlay starts,
2557 or ZV if there are no more overlays.
2558 Store in *PREV_PTR the previous position before POS where an overlay ends,
2559 or BEGV if there are no previous overlays.
2560 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2562 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2563 when this function is called.
2565 If EXTEND is non-zero, we make the vector bigger if necessary.
2566 If EXTEND is zero, we never extend the vector,
2567 and we store only as many overlays as will fit.
2568 But we still return the total number of overlays. */
2570 static int
2571 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2572 int beg, end;
2573 int extend;
2574 Lisp_Object **vec_ptr;
2575 int *len_ptr;
2576 int *next_ptr;
2577 int *prev_ptr;
2579 Lisp_Object overlay, ostart, oend;
2580 struct Lisp_Overlay *tail;
2581 int idx = 0;
2582 int len = *len_ptr;
2583 Lisp_Object *vec = *vec_ptr;
2584 int next = ZV;
2585 int prev = BEGV;
2586 int inhibit_storing = 0;
2588 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2590 int startpos, endpos;
2592 XSETMISC (overlay, tail);
2594 ostart = OVERLAY_START (overlay);
2595 oend = OVERLAY_END (overlay);
2596 endpos = OVERLAY_POSITION (oend);
2597 if (endpos < beg)
2599 if (prev < endpos)
2600 prev = endpos;
2601 break;
2603 startpos = OVERLAY_POSITION (ostart);
2604 /* Count an interval if it either overlaps the range
2605 or is empty at the start of the range. */
2606 if ((beg < endpos && startpos < end)
2607 || (startpos == endpos && beg == endpos))
2609 if (idx == len)
2611 /* The supplied vector is full.
2612 Either make it bigger, or don't store any more in it. */
2613 if (extend)
2615 /* Make it work with an initial len == 0. */
2616 len *= 2;
2617 if (len == 0)
2618 len = 4;
2619 *len_ptr = len;
2620 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2621 *vec_ptr = vec;
2623 else
2624 inhibit_storing = 1;
2627 if (!inhibit_storing)
2628 vec[idx] = overlay;
2629 /* Keep counting overlays even if we can't return them all. */
2630 idx++;
2632 else if (startpos < next)
2633 next = startpos;
2636 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2638 int startpos, endpos;
2640 XSETMISC (overlay, tail);
2642 ostart = OVERLAY_START (overlay);
2643 oend = OVERLAY_END (overlay);
2644 startpos = OVERLAY_POSITION (ostart);
2645 if (end < startpos)
2647 if (startpos < next)
2648 next = startpos;
2649 break;
2651 endpos = OVERLAY_POSITION (oend);
2652 /* Count an interval if it either overlaps the range
2653 or is empty at the start of the range. */
2654 if ((beg < endpos && startpos < end)
2655 || (startpos == endpos && beg == endpos))
2657 if (idx == len)
2659 if (extend)
2661 /* Make it work with an initial len == 0. */
2662 len *= 2;
2663 if (len == 0)
2664 len = 4;
2665 *len_ptr = len;
2666 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2667 *vec_ptr = vec;
2669 else
2670 inhibit_storing = 1;
2673 if (!inhibit_storing)
2674 vec[idx] = overlay;
2675 idx++;
2677 else if (endpos < beg && endpos > prev)
2678 prev = endpos;
2681 if (next_ptr)
2682 *next_ptr = next;
2683 if (prev_ptr)
2684 *prev_ptr = prev;
2685 return idx;
2689 /* Return non-zero if there exists an overlay with a non-nil
2690 `mouse-face' property overlapping OVERLAY. */
2693 mouse_face_overlay_overlaps (overlay)
2694 Lisp_Object overlay;
2696 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2697 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2698 int n, i, size;
2699 Lisp_Object *v, tem;
2701 size = 10;
2702 v = (Lisp_Object *) alloca (size * sizeof *v);
2703 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2704 if (n > size)
2706 v = (Lisp_Object *) alloca (n * sizeof *v);
2707 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2710 for (i = 0; i < n; ++i)
2711 if (!EQ (v[i], overlay)
2712 && (tem = Foverlay_get (overlay, Qmouse_face),
2713 !NILP (tem)))
2714 break;
2716 return i < n;
2721 /* Fast function to just test if we're at an overlay boundary. */
2723 overlay_touches_p (pos)
2724 int pos;
2726 Lisp_Object overlay;
2727 struct Lisp_Overlay *tail;
2729 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2731 int endpos;
2733 XSETMISC (overlay ,tail);
2734 if (!GC_OVERLAYP (overlay))
2735 abort ();
2737 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2738 if (endpos < pos)
2739 break;
2740 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2741 return 1;
2744 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2746 int startpos;
2748 XSETMISC (overlay, tail);
2749 if (!GC_OVERLAYP (overlay))
2750 abort ();
2752 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2753 if (pos < startpos)
2754 break;
2755 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2756 return 1;
2758 return 0;
2761 struct sortvec
2763 Lisp_Object overlay;
2764 int beg, end;
2765 int priority;
2768 static int
2769 compare_overlays (v1, v2)
2770 const void *v1, *v2;
2772 const struct sortvec *s1 = (const struct sortvec *) v1;
2773 const struct sortvec *s2 = (const struct sortvec *) v2;
2774 if (s1->priority != s2->priority)
2775 return s1->priority - s2->priority;
2776 if (s1->beg != s2->beg)
2777 return s1->beg - s2->beg;
2778 if (s1->end != s2->end)
2779 return s2->end - s1->end;
2780 return 0;
2783 /* Sort an array of overlays by priority. The array is modified in place.
2784 The return value is the new size; this may be smaller than the original
2785 size if some of the overlays were invalid or were window-specific. */
2787 sort_overlays (overlay_vec, noverlays, w)
2788 Lisp_Object *overlay_vec;
2789 int noverlays;
2790 struct window *w;
2792 int i, j;
2793 struct sortvec *sortvec;
2794 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2796 /* Put the valid and relevant overlays into sortvec. */
2798 for (i = 0, j = 0; i < noverlays; i++)
2800 Lisp_Object tem;
2801 Lisp_Object overlay;
2803 overlay = overlay_vec[i];
2804 if (OVERLAY_VALID (overlay)
2805 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2806 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2808 /* If we're interested in a specific window, then ignore
2809 overlays that are limited to some other window. */
2810 if (w)
2812 Lisp_Object window;
2814 window = Foverlay_get (overlay, Qwindow);
2815 if (WINDOWP (window) && XWINDOW (window) != w)
2816 continue;
2819 /* This overlay is good and counts: put it into sortvec. */
2820 sortvec[j].overlay = overlay;
2821 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2822 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2823 tem = Foverlay_get (overlay, Qpriority);
2824 if (INTEGERP (tem))
2825 sortvec[j].priority = XINT (tem);
2826 else
2827 sortvec[j].priority = 0;
2828 j++;
2831 noverlays = j;
2833 /* Sort the overlays into the proper order: increasing priority. */
2835 if (noverlays > 1)
2836 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2838 for (i = 0; i < noverlays; i++)
2839 overlay_vec[i] = sortvec[i].overlay;
2840 return (noverlays);
2843 struct sortstr
2845 Lisp_Object string, string2;
2846 int size;
2847 int priority;
2850 struct sortstrlist
2852 struct sortstr *buf; /* An array that expands as needed; never freed. */
2853 int size; /* Allocated length of that array. */
2854 int used; /* How much of the array is currently in use. */
2855 int bytes; /* Total length of the strings in buf. */
2858 /* Buffers for storing information about the overlays touching a given
2859 position. These could be automatic variables in overlay_strings, but
2860 it's more efficient to hold onto the memory instead of repeatedly
2861 allocating and freeing it. */
2862 static struct sortstrlist overlay_heads, overlay_tails;
2863 static unsigned char *overlay_str_buf;
2865 /* Allocated length of overlay_str_buf. */
2866 static int overlay_str_len;
2868 /* A comparison function suitable for passing to qsort. */
2869 static int
2870 cmp_for_strings (as1, as2)
2871 char *as1, *as2;
2873 struct sortstr *s1 = (struct sortstr *)as1;
2874 struct sortstr *s2 = (struct sortstr *)as2;
2875 if (s1->size != s2->size)
2876 return s2->size - s1->size;
2877 if (s1->priority != s2->priority)
2878 return s1->priority - s2->priority;
2879 return 0;
2882 static void
2883 record_overlay_string (ssl, str, str2, pri, size)
2884 struct sortstrlist *ssl;
2885 Lisp_Object str, str2, pri;
2886 int size;
2888 int nbytes;
2890 if (ssl->used == ssl->size)
2892 if (ssl->buf)
2893 ssl->size *= 2;
2894 else
2895 ssl->size = 5;
2896 ssl->buf = ((struct sortstr *)
2897 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2899 ssl->buf[ssl->used].string = str;
2900 ssl->buf[ssl->used].string2 = str2;
2901 ssl->buf[ssl->used].size = size;
2902 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2903 ssl->used++;
2905 if (NILP (current_buffer->enable_multibyte_characters))
2906 nbytes = SCHARS (str);
2907 else if (! STRING_MULTIBYTE (str))
2908 nbytes = count_size_as_multibyte (SDATA (str),
2909 SBYTES (str));
2910 else
2911 nbytes = SBYTES (str);
2913 ssl->bytes += nbytes;
2915 if (STRINGP (str2))
2917 if (NILP (current_buffer->enable_multibyte_characters))
2918 nbytes = SCHARS (str2);
2919 else if (! STRING_MULTIBYTE (str2))
2920 nbytes = count_size_as_multibyte (SDATA (str2),
2921 SBYTES (str2));
2922 else
2923 nbytes = SBYTES (str2);
2925 ssl->bytes += nbytes;
2929 /* Return the concatenation of the strings associated with overlays that
2930 begin or end at POS, ignoring overlays that are specific to a window
2931 other than W. The strings are concatenated in the appropriate order:
2932 shorter overlays nest inside longer ones, and higher priority inside
2933 lower. Normally all of the after-strings come first, but zero-sized
2934 overlays have their after-strings ride along with the before-strings
2935 because it would look strange to print them inside-out.
2937 Returns the string length, and stores the contents indirectly through
2938 PSTR, if that variable is non-null. The string may be overwritten by
2939 subsequent calls. */
2942 overlay_strings (pos, w, pstr)
2943 EMACS_INT pos;
2944 struct window *w;
2945 unsigned char **pstr;
2947 Lisp_Object overlay, window, str;
2948 struct Lisp_Overlay *ov;
2949 int startpos, endpos;
2950 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
2952 overlay_heads.used = overlay_heads.bytes = 0;
2953 overlay_tails.used = overlay_tails.bytes = 0;
2954 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
2956 XSETMISC (overlay, ov);
2957 eassert (OVERLAYP (overlay));
2959 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2960 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2961 if (endpos < pos)
2962 break;
2963 if (endpos != pos && startpos != pos)
2964 continue;
2965 window = Foverlay_get (overlay, Qwindow);
2966 if (WINDOWP (window) && XWINDOW (window) != w)
2967 continue;
2968 if (startpos == pos
2969 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2970 record_overlay_string (&overlay_heads, str,
2971 (startpos == endpos
2972 ? Foverlay_get (overlay, Qafter_string)
2973 : Qnil),
2974 Foverlay_get (overlay, Qpriority),
2975 endpos - startpos);
2976 else if (endpos == pos
2977 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2978 record_overlay_string (&overlay_tails, str, Qnil,
2979 Foverlay_get (overlay, Qpriority),
2980 endpos - startpos);
2982 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
2984 XSETMISC (overlay, ov);
2985 eassert (OVERLAYP (overlay));
2987 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2988 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2989 if (startpos > pos)
2990 break;
2991 if (endpos != pos && startpos != pos)
2992 continue;
2993 window = Foverlay_get (overlay, Qwindow);
2994 if (WINDOWP (window) && XWINDOW (window) != w)
2995 continue;
2996 if (startpos == pos
2997 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2998 record_overlay_string (&overlay_heads, str,
2999 (startpos == endpos
3000 ? Foverlay_get (overlay, Qafter_string)
3001 : Qnil),
3002 Foverlay_get (overlay, Qpriority),
3003 endpos - startpos);
3004 else if (endpos == pos
3005 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3006 record_overlay_string (&overlay_tails, str, Qnil,
3007 Foverlay_get (overlay, Qpriority),
3008 endpos - startpos);
3010 if (overlay_tails.used > 1)
3011 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3012 cmp_for_strings);
3013 if (overlay_heads.used > 1)
3014 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3015 cmp_for_strings);
3016 if (overlay_heads.bytes || overlay_tails.bytes)
3018 Lisp_Object tem;
3019 int i;
3020 unsigned char *p;
3021 int total = overlay_heads.bytes + overlay_tails.bytes;
3023 if (total > overlay_str_len)
3025 overlay_str_len = total;
3026 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3027 total);
3029 p = overlay_str_buf;
3030 for (i = overlay_tails.used; --i >= 0;)
3032 int nbytes;
3033 tem = overlay_tails.buf[i].string;
3034 nbytes = copy_text (SDATA (tem), p,
3035 SBYTES (tem),
3036 STRING_MULTIBYTE (tem), multibyte);
3037 p += nbytes;
3039 for (i = 0; i < overlay_heads.used; ++i)
3041 int nbytes;
3042 tem = overlay_heads.buf[i].string;
3043 nbytes = copy_text (SDATA (tem), p,
3044 SBYTES (tem),
3045 STRING_MULTIBYTE (tem), multibyte);
3046 p += nbytes;
3047 tem = overlay_heads.buf[i].string2;
3048 if (STRINGP (tem))
3050 nbytes = copy_text (SDATA (tem), p,
3051 SBYTES (tem),
3052 STRING_MULTIBYTE (tem), multibyte);
3053 p += nbytes;
3056 if (p != overlay_str_buf + total)
3057 abort ();
3058 if (pstr)
3059 *pstr = overlay_str_buf;
3060 return total;
3062 return 0;
3065 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3067 void
3068 recenter_overlay_lists (buf, pos)
3069 struct buffer *buf;
3070 EMACS_INT pos;
3072 Lisp_Object overlay, beg, end;
3073 struct Lisp_Overlay *prev, *tail, *next;
3075 /* See if anything in overlays_before should move to overlays_after. */
3077 /* We don't strictly need prev in this loop; it should always be nil.
3078 But we use it for symmetry and in case that should cease to be true
3079 with some future change. */
3080 prev = NULL;
3081 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3083 next = tail->next;
3084 XSETMISC (overlay, tail);
3086 /* If the overlay is not valid, get rid of it. */
3087 if (!OVERLAY_VALID (overlay))
3088 #if 1
3089 abort ();
3090 #else
3092 /* Splice the cons cell TAIL out of overlays_before. */
3093 if (!NILP (prev))
3094 XCDR (prev) = next;
3095 else
3096 buf->overlays_before = next;
3097 tail = prev;
3098 continue;
3100 #endif
3102 beg = OVERLAY_START (overlay);
3103 end = OVERLAY_END (overlay);
3105 if (OVERLAY_POSITION (end) > pos)
3107 /* OVERLAY needs to be moved. */
3108 int where = OVERLAY_POSITION (beg);
3109 struct Lisp_Overlay *other, *other_prev;
3111 /* Splice the cons cell TAIL out of overlays_before. */
3112 if (prev)
3113 prev->next = next;
3114 else
3115 buf->overlays_before = next;
3117 /* Search thru overlays_after for where to put it. */
3118 other_prev = NULL;
3119 for (other = buf->overlays_after; other;
3120 other_prev = other, other = other->next)
3122 Lisp_Object otherbeg, otheroverlay;
3124 XSETMISC (otheroverlay, other);
3125 eassert (OVERLAY_VALID (otheroverlay));
3127 otherbeg = OVERLAY_START (otheroverlay);
3128 if (OVERLAY_POSITION (otherbeg) >= where)
3129 break;
3132 /* Add TAIL to overlays_after before OTHER. */
3133 tail->next = other;
3134 if (other_prev)
3135 other_prev->next = tail;
3136 else
3137 buf->overlays_after = tail;
3138 tail = prev;
3140 else
3141 /* We've reached the things that should stay in overlays_before.
3142 All the rest of overlays_before must end even earlier,
3143 so stop now. */
3144 break;
3147 /* See if anything in overlays_after should be in overlays_before. */
3148 prev = NULL;
3149 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3151 next = tail->next;
3152 XSETMISC (overlay, tail);
3154 /* If the overlay is not valid, get rid of it. */
3155 if (!OVERLAY_VALID (overlay))
3156 #if 1
3157 abort ();
3158 #else
3160 /* Splice the cons cell TAIL out of overlays_after. */
3161 if (!NILP (prev))
3162 XCDR (prev) = next;
3163 else
3164 buf->overlays_after = next;
3165 tail = prev;
3166 continue;
3168 #endif
3170 beg = OVERLAY_START (overlay);
3171 end = OVERLAY_END (overlay);
3173 /* Stop looking, when we know that nothing further
3174 can possibly end before POS. */
3175 if (OVERLAY_POSITION (beg) > pos)
3176 break;
3178 if (OVERLAY_POSITION (end) <= pos)
3180 /* OVERLAY needs to be moved. */
3181 int where = OVERLAY_POSITION (end);
3182 struct Lisp_Overlay *other, *other_prev;
3184 /* Splice the cons cell TAIL out of overlays_after. */
3185 if (prev)
3186 prev->next = next;
3187 else
3188 buf->overlays_after = next;
3190 /* Search thru overlays_before for where to put it. */
3191 other_prev = NULL;
3192 for (other = buf->overlays_before; other;
3193 other_prev = other, other = other->next)
3195 Lisp_Object otherend, otheroverlay;
3197 XSETMISC (otheroverlay, other);
3198 eassert (OVERLAY_VALID (otheroverlay));
3200 otherend = OVERLAY_END (otheroverlay);
3201 if (OVERLAY_POSITION (otherend) <= where)
3202 break;
3205 /* Add TAIL to overlays_before before OTHER. */
3206 tail->next = other;
3207 if (other_prev)
3208 other_prev->next = tail;
3209 else
3210 buf->overlays_before = tail;
3211 tail = prev;
3215 buf->overlay_center = pos;
3218 void
3219 adjust_overlays_for_insert (pos, length)
3220 EMACS_INT pos;
3221 EMACS_INT length;
3223 /* After an insertion, the lists are still sorted properly,
3224 but we may need to update the value of the overlay center. */
3225 if (current_buffer->overlay_center >= pos)
3226 current_buffer->overlay_center += length;
3229 void
3230 adjust_overlays_for_delete (pos, length)
3231 EMACS_INT pos;
3232 EMACS_INT length;
3234 if (current_buffer->overlay_center < pos)
3235 /* The deletion was to our right. No change needed; the before- and
3236 after-lists are still consistent. */
3238 else if (current_buffer->overlay_center > pos + length)
3239 /* The deletion was to our left. We need to adjust the center value
3240 to account for the change in position, but the lists are consistent
3241 given the new value. */
3242 current_buffer->overlay_center -= length;
3243 else
3244 /* We're right in the middle. There might be things on the after-list
3245 that now belong on the before-list. Recentering will move them,
3246 and also update the center point. */
3247 recenter_overlay_lists (current_buffer, pos);
3250 /* Fix up overlays that were garbled as a result of permuting markers
3251 in the range START through END. Any overlay with at least one
3252 endpoint in this range will need to be unlinked from the overlay
3253 list and reinserted in its proper place.
3254 Such an overlay might even have negative size at this point.
3255 If so, we'll reverse the endpoints. Can you think of anything
3256 better to do in this situation? */
3257 void
3258 fix_overlays_in_range (start, end)
3259 register int start, end;
3261 Lisp_Object overlay;
3262 struct Lisp_Overlay *before_list, *after_list;
3263 /* These are either nil, indicating that before_list or after_list
3264 should be assigned, or the cons cell the cdr of which should be
3265 assigned. */
3266 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3267 /* 'Parent', likewise, indicates a cons cell or
3268 current_buffer->overlays_before or overlays_after, depending
3269 which loop we're in. */
3270 struct Lisp_Overlay *tail, *parent;
3271 int startpos, endpos;
3273 /* This algorithm shifts links around instead of consing and GCing.
3274 The loop invariant is that before_list (resp. after_list) is a
3275 well-formed list except that its last element, the CDR of beforep
3276 (resp. afterp) if beforep (afterp) isn't nil or before_list
3277 (after_list) if it is, is still uninitialized. So it's not a bug
3278 that before_list isn't initialized, although it may look
3279 strange. */
3280 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3282 XSETMISC (overlay, tail);
3283 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3284 if (endpos < start)
3285 break;
3286 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3287 if (endpos < end
3288 || (startpos >= start && startpos < end))
3290 /* If the overlay is backwards, fix that now. */
3291 if (startpos > endpos)
3293 int tem;
3294 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3295 Qnil);
3296 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3297 Qnil);
3298 tem = startpos; startpos = endpos; endpos = tem;
3300 /* Add it to the end of the wrong list. Later on,
3301 recenter_overlay_lists will move it to the right place. */
3302 if (endpos < current_buffer->overlay_center)
3304 if (!afterp)
3305 after_list = tail;
3306 else
3307 afterp->next = tail;
3308 afterp = tail;
3310 else
3312 if (!beforep)
3313 before_list = tail;
3314 else
3315 beforep->next = tail;
3316 beforep = tail;
3318 if (!parent)
3319 current_buffer->overlays_before = tail->next;
3320 else
3321 parent->next = tail->next;
3322 tail = tail->next;
3324 else
3325 parent = tail, tail = parent->next;
3327 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3329 XSETMISC (overlay, tail);
3330 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3331 if (startpos >= end)
3332 break;
3333 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3334 if (startpos >= start
3335 || (endpos >= start && endpos < end))
3337 if (startpos > endpos)
3339 int tem;
3340 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3341 Qnil);
3342 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3343 Qnil);
3344 tem = startpos; startpos = endpos; endpos = tem;
3346 if (endpos < current_buffer->overlay_center)
3348 if (!afterp)
3349 after_list = tail;
3350 else
3351 afterp->next = tail;
3352 afterp = tail;
3354 else
3356 if (!beforep)
3357 before_list = tail;
3358 else
3359 beforep->next = tail;
3360 beforep = tail;
3362 if (!parent)
3363 current_buffer->overlays_after = tail->next;
3364 else
3365 parent->next = tail->next;
3366 tail = tail->next;
3368 else
3369 parent = tail, tail = parent->next;
3372 /* Splice the constructed (wrong) lists into the buffer's lists,
3373 and let the recenter function make it sane again. */
3374 if (beforep)
3376 beforep->next = current_buffer->overlays_before;
3377 current_buffer->overlays_before = before_list;
3379 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3381 if (afterp)
3383 afterp->next = current_buffer->overlays_after;
3384 current_buffer->overlays_after = after_list;
3386 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3389 /* We have two types of overlay: the one whose ending marker is
3390 after-insertion-marker (this is the usual case) and the one whose
3391 ending marker is before-insertion-marker. When `overlays_before'
3392 contains overlays of the latter type and the former type in this
3393 order and both overlays end at inserting position, inserting a text
3394 increases only the ending marker of the latter type, which results
3395 in incorrect ordering of `overlays_before'.
3397 This function fixes ordering of overlays in the slot
3398 `overlays_before' of the buffer *BP. Before the insertion, `point'
3399 was at PREV, and now is at POS. */
3401 void
3402 fix_overlays_before (bp, prev, pos)
3403 struct buffer *bp;
3404 EMACS_INT prev, pos;
3406 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3407 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3408 Lisp_Object tem;
3409 EMACS_INT end;
3411 /* After the insertion, the several overlays may be in incorrect
3412 order. The possibility is that, in the list `overlays_before',
3413 an overlay which ends at POS appears after an overlay which ends
3414 at PREV. Since POS is greater than PREV, we must fix the
3415 ordering of these overlays, by moving overlays ends at POS before
3416 the overlays ends at PREV. */
3418 /* At first, find a place where disordered overlays should be linked
3419 in. It is where an overlay which end before POS exists. (i.e. an
3420 overlay whose ending marker is after-insertion-marker if disorder
3421 exists). */
3422 while (tail
3423 && (XSETMISC (tem, tail),
3424 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3426 parent = tail;
3427 tail = tail->next;
3430 /* If we don't find such an overlay,
3431 or the found one ends before PREV,
3432 or the found one is the last one in the list,
3433 we don't have to fix anything. */
3434 if (!tail || end < prev || !tail->next)
3435 return;
3437 right_pair = parent;
3438 parent = tail;
3439 tail = tail->next;
3441 /* Now, end position of overlays in the list TAIL should be before
3442 or equal to PREV. In the loop, an overlay which ends at POS is
3443 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3444 we found an overlay which ends before PREV, the remaining
3445 overlays are in correct order. */
3446 while (tail)
3448 XSETMISC (tem, tail);
3449 end = OVERLAY_POSITION (OVERLAY_END (tem));
3451 if (end == pos)
3452 { /* This overlay is disordered. */
3453 struct Lisp_Overlay *found = tail;
3455 /* Unlink the found overlay. */
3456 tail = found->next;
3457 parent->next = tail;
3458 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3459 and link it into the right place. */
3460 if (!right_pair)
3462 found->next = bp->overlays_before;
3463 bp->overlays_before = found;
3465 else
3467 found->next = right_pair->next;
3468 right_pair->next = found;
3471 else if (end == prev)
3473 parent = tail;
3474 tail = tail->next;
3476 else /* No more disordered overlay. */
3477 break;
3481 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3482 doc: /* Return t if OBJECT is an overlay. */)
3483 (object)
3484 Lisp_Object object;
3486 return (OVERLAYP (object) ? Qt : Qnil);
3489 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3490 doc: /* Create a new overlay with range BEG to END in BUFFER.
3491 If omitted, BUFFER defaults to the current buffer.
3492 BEG and END may be integers or markers.
3493 The fourth arg FRONT-ADVANCE, if non-nil, makes the
3494 front delimiter advance when text is inserted there.
3495 The fifth arg REAR-ADVANCE, if non-nil, makes the
3496 rear delimiter advance when text is inserted there. */)
3497 (beg, end, buffer, front_advance, rear_advance)
3498 Lisp_Object beg, end, buffer;
3499 Lisp_Object front_advance, rear_advance;
3501 Lisp_Object overlay;
3502 struct buffer *b;
3504 if (NILP (buffer))
3505 XSETBUFFER (buffer, current_buffer);
3506 else
3507 CHECK_BUFFER (buffer);
3508 if (MARKERP (beg)
3509 && ! EQ (Fmarker_buffer (beg), buffer))
3510 error ("Marker points into wrong buffer");
3511 if (MARKERP (end)
3512 && ! EQ (Fmarker_buffer (end), buffer))
3513 error ("Marker points into wrong buffer");
3515 CHECK_NUMBER_COERCE_MARKER (beg);
3516 CHECK_NUMBER_COERCE_MARKER (end);
3518 if (XINT (beg) > XINT (end))
3520 Lisp_Object temp;
3521 temp = beg; beg = end; end = temp;
3524 b = XBUFFER (buffer);
3526 beg = Fset_marker (Fmake_marker (), beg, buffer);
3527 end = Fset_marker (Fmake_marker (), end, buffer);
3529 if (!NILP (front_advance))
3530 XMARKER (beg)->insertion_type = 1;
3531 if (!NILP (rear_advance))
3532 XMARKER (end)->insertion_type = 1;
3534 overlay = allocate_misc ();
3535 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3536 XOVERLAY (overlay)->start = beg;
3537 XOVERLAY (overlay)->end = end;
3538 XOVERLAY (overlay)->plist = Qnil;
3539 XOVERLAY (overlay)->next = NULL;
3541 /* Put the new overlay on the wrong list. */
3542 end = OVERLAY_END (overlay);
3543 if (OVERLAY_POSITION (end) < b->overlay_center)
3545 if (b->overlays_after)
3546 XOVERLAY (overlay)->next = b->overlays_after;
3547 b->overlays_after = XOVERLAY (overlay);
3549 else
3551 if (b->overlays_before)
3552 XOVERLAY (overlay)->next = b->overlays_before;
3553 b->overlays_before = XOVERLAY (overlay);
3556 /* This puts it in the right list, and in the right order. */
3557 recenter_overlay_lists (b, b->overlay_center);
3559 /* We don't need to redisplay the region covered by the overlay, because
3560 the overlay has no properties at the moment. */
3562 return overlay;
3565 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3567 static void
3568 modify_overlay (buf, start, end)
3569 struct buffer *buf;
3570 EMACS_INT start, end;
3572 if (start > end)
3574 int temp = start;
3575 start = end;
3576 end = temp;
3579 BUF_COMPUTE_UNCHANGED (buf, start, end);
3581 /* If this is a buffer not in the selected window,
3582 we must do other windows. */
3583 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3584 windows_or_buffers_changed = 1;
3585 /* If multiple windows show this buffer, we must do other windows. */
3586 else if (buffer_shared > 1)
3587 windows_or_buffers_changed = 1;
3589 ++BUF_OVERLAY_MODIFF (buf);
3593 Lisp_Object Fdelete_overlay ();
3595 static struct Lisp_Overlay *
3596 unchain_overlay (list, overlay)
3597 struct Lisp_Overlay *list, *overlay;
3599 struct Lisp_Overlay *tmp, *prev;
3600 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3601 if (tmp == overlay)
3603 if (prev)
3604 prev->next = tmp->next;
3605 else
3606 list = tmp->next;
3607 overlay->next = NULL;
3608 break;
3610 return list;
3613 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3614 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3615 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3616 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3617 buffer. */)
3618 (overlay, beg, end, buffer)
3619 Lisp_Object overlay, beg, end, buffer;
3621 struct buffer *b, *ob;
3622 Lisp_Object obuffer;
3623 int count = SPECPDL_INDEX ();
3625 CHECK_OVERLAY (overlay);
3626 if (NILP (buffer))
3627 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3628 if (NILP (buffer))
3629 XSETBUFFER (buffer, current_buffer);
3630 CHECK_BUFFER (buffer);
3632 if (MARKERP (beg)
3633 && ! EQ (Fmarker_buffer (beg), buffer))
3634 error ("Marker points into wrong buffer");
3635 if (MARKERP (end)
3636 && ! EQ (Fmarker_buffer (end), buffer))
3637 error ("Marker points into wrong buffer");
3639 CHECK_NUMBER_COERCE_MARKER (beg);
3640 CHECK_NUMBER_COERCE_MARKER (end);
3642 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3643 return Fdelete_overlay (overlay);
3645 if (XINT (beg) > XINT (end))
3647 Lisp_Object temp;
3648 temp = beg; beg = end; end = temp;
3651 specbind (Qinhibit_quit, Qt);
3653 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3654 b = XBUFFER (buffer);
3655 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3657 /* If the overlay has changed buffers, do a thorough redisplay. */
3658 if (!EQ (buffer, obuffer))
3660 /* Redisplay where the overlay was. */
3661 if (!NILP (obuffer))
3663 int o_beg;
3664 int o_end;
3666 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3667 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3669 modify_overlay (ob, o_beg, o_end);
3672 /* Redisplay where the overlay is going to be. */
3673 modify_overlay (b, XINT (beg), XINT (end));
3675 else
3676 /* Redisplay the area the overlay has just left, or just enclosed. */
3678 int o_beg, o_end;
3680 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3681 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3683 if (o_beg == XINT (beg))
3684 modify_overlay (b, o_end, XINT (end));
3685 else if (o_end == XINT (end))
3686 modify_overlay (b, o_beg, XINT (beg));
3687 else
3689 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3690 if (XINT (end) > o_end) o_end = XINT (end);
3691 modify_overlay (b, o_beg, o_end);
3695 if (!NILP (obuffer))
3697 ob->overlays_before
3698 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3699 ob->overlays_after
3700 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3701 eassert (XOVERLAY (overlay)->next == NULL);
3704 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3705 Fset_marker (OVERLAY_END (overlay), end, buffer);
3707 /* Put the overlay on the wrong list. */
3708 end = OVERLAY_END (overlay);
3709 if (OVERLAY_POSITION (end) < b->overlay_center)
3711 if (b->overlays_after)
3712 XOVERLAY (overlay)->next = b->overlays_after;
3713 b->overlays_after = XOVERLAY (overlay);
3715 else
3717 if (b->overlays_before)
3718 XOVERLAY (overlay)->next = b->overlays_before;
3719 b->overlays_before = XOVERLAY (overlay);
3722 /* This puts it in the right list, and in the right order. */
3723 recenter_overlay_lists (b, b->overlay_center);
3725 return unbind_to (count, overlay);
3728 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3729 doc: /* Delete the overlay OVERLAY from its buffer. */)
3730 (overlay)
3731 Lisp_Object overlay;
3733 Lisp_Object buffer;
3734 struct buffer *b;
3735 int count = SPECPDL_INDEX ();
3737 CHECK_OVERLAY (overlay);
3739 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3740 if (NILP (buffer))
3741 return Qnil;
3743 b = XBUFFER (buffer);
3744 specbind (Qinhibit_quit, Qt);
3746 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3747 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3748 eassert (XOVERLAY (overlay)->next == NULL);
3749 modify_overlay (b,
3750 marker_position (OVERLAY_START (overlay)),
3751 marker_position (OVERLAY_END (overlay)));
3752 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3753 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3755 /* When deleting an overlay with before or after strings, turn off
3756 display optimizations for the affected buffer, on the basis that
3757 these strings may contain newlines. This is easier to do than to
3758 check for that situation during redisplay. */
3759 if (!windows_or_buffers_changed
3760 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3761 || !NILP (Foverlay_get (overlay, Qafter_string))))
3762 b->prevent_redisplay_optimizations_p = 1;
3764 return unbind_to (count, Qnil);
3767 /* Overlay dissection functions. */
3769 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3770 doc: /* Return the position at which OVERLAY starts. */)
3771 (overlay)
3772 Lisp_Object overlay;
3774 CHECK_OVERLAY (overlay);
3776 return (Fmarker_position (OVERLAY_START (overlay)));
3779 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3780 doc: /* Return the position at which OVERLAY ends. */)
3781 (overlay)
3782 Lisp_Object overlay;
3784 CHECK_OVERLAY (overlay);
3786 return (Fmarker_position (OVERLAY_END (overlay)));
3789 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3790 doc: /* Return the buffer OVERLAY belongs to. */)
3791 (overlay)
3792 Lisp_Object overlay;
3794 CHECK_OVERLAY (overlay);
3796 return Fmarker_buffer (OVERLAY_START (overlay));
3799 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3800 doc: /* Return a list of the properties on OVERLAY.
3801 This is a copy of OVERLAY's plist; modifying its conses has no effect on
3802 OVERLAY. */)
3803 (overlay)
3804 Lisp_Object overlay;
3806 CHECK_OVERLAY (overlay);
3808 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3812 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3813 doc: /* Return a list of the overlays that contain position POS. */)
3814 (pos)
3815 Lisp_Object pos;
3817 int noverlays;
3818 Lisp_Object *overlay_vec;
3819 int len;
3820 Lisp_Object result;
3822 CHECK_NUMBER_COERCE_MARKER (pos);
3824 len = 10;
3825 /* We can't use alloca here because overlays_at can call xrealloc. */
3826 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3828 /* Put all the overlays we want in a vector in overlay_vec.
3829 Store the length in len. */
3830 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3831 (int *) 0, (int *) 0, 0);
3833 /* Make a list of them all. */
3834 result = Flist (noverlays, overlay_vec);
3836 xfree (overlay_vec);
3837 return result;
3840 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3841 doc: /* Return a list of the overlays that overlap the region BEG ... END.
3842 Overlap means that at least one character is contained within the overlay
3843 and also contained within the specified region.
3844 Empty overlays are included in the result if they are located at BEG
3845 or between BEG and END. */)
3846 (beg, end)
3847 Lisp_Object beg, end;
3849 int noverlays;
3850 Lisp_Object *overlay_vec;
3851 int len;
3852 Lisp_Object result;
3854 CHECK_NUMBER_COERCE_MARKER (beg);
3855 CHECK_NUMBER_COERCE_MARKER (end);
3857 len = 10;
3858 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3860 /* Put all the overlays we want in a vector in overlay_vec.
3861 Store the length in len. */
3862 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3863 (int *) 0, (int *) 0);
3865 /* Make a list of them all. */
3866 result = Flist (noverlays, overlay_vec);
3868 xfree (overlay_vec);
3869 return result;
3872 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3873 1, 1, 0,
3874 doc: /* Return the next position after POS where an overlay starts or ends.
3875 If there are no more overlay boundaries after POS, return (point-max). */)
3876 (pos)
3877 Lisp_Object pos;
3879 int noverlays;
3880 int endpos;
3881 Lisp_Object *overlay_vec;
3882 int len;
3883 int i;
3885 CHECK_NUMBER_COERCE_MARKER (pos);
3887 len = 10;
3888 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3890 /* Put all the overlays we want in a vector in overlay_vec.
3891 Store the length in len.
3892 endpos gets the position where the next overlay starts. */
3893 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3894 &endpos, (int *) 0, 1);
3896 /* If any of these overlays ends before endpos,
3897 use its ending point instead. */
3898 for (i = 0; i < noverlays; i++)
3900 Lisp_Object oend;
3901 int oendpos;
3903 oend = OVERLAY_END (overlay_vec[i]);
3904 oendpos = OVERLAY_POSITION (oend);
3905 if (oendpos < endpos)
3906 endpos = oendpos;
3909 xfree (overlay_vec);
3910 return make_number (endpos);
3913 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3914 Sprevious_overlay_change, 1, 1, 0,
3915 doc: /* Return the previous position before POS where an overlay starts or ends.
3916 If there are no more overlay boundaries before POS, return (point-min). */)
3917 (pos)
3918 Lisp_Object pos;
3920 int noverlays;
3921 int prevpos;
3922 Lisp_Object *overlay_vec;
3923 int len;
3925 CHECK_NUMBER_COERCE_MARKER (pos);
3927 /* At beginning of buffer, we know the answer;
3928 avoid bug subtracting 1 below. */
3929 if (XINT (pos) == BEGV)
3930 return pos;
3932 len = 10;
3933 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3935 /* Put all the overlays we want in a vector in overlay_vec.
3936 Store the length in len.
3937 prevpos gets the position of the previous change. */
3938 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3939 (int *) 0, &prevpos, 1);
3941 xfree (overlay_vec);
3942 return make_number (prevpos);
3945 /* These functions are for debugging overlays. */
3947 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3948 doc: /* Return a pair of lists giving all the overlays of the current buffer.
3949 The car has all the overlays before the overlay center;
3950 the cdr has all the overlays after the overlay center.
3951 Recentering overlays moves overlays between these lists.
3952 The lists you get are copies, so that changing them has no effect.
3953 However, the overlays you get are the real objects that the buffer uses. */)
3956 struct Lisp_Overlay *ol;
3957 Lisp_Object before = Qnil, after = Qnil, tmp;
3958 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
3960 XSETMISC (tmp, ol);
3961 before = Fcons (tmp, before);
3963 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
3965 XSETMISC (tmp, ol);
3966 after = Fcons (tmp, after);
3968 return Fcons (Fnreverse (before), Fnreverse (after));
3971 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3972 doc: /* Recenter the overlays of the current buffer around position POS.
3973 That makes overlay lookup faster for positions near POS (but perhaps slower
3974 for positions far away from POS). */)
3975 (pos)
3976 Lisp_Object pos;
3978 CHECK_NUMBER_COERCE_MARKER (pos);
3980 recenter_overlay_lists (current_buffer, XINT (pos));
3981 return Qnil;
3984 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3985 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
3986 (overlay, prop)
3987 Lisp_Object overlay, prop;
3989 CHECK_OVERLAY (overlay);
3990 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
3993 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3994 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
3995 (overlay, prop, value)
3996 Lisp_Object overlay, prop, value;
3998 Lisp_Object tail, buffer;
3999 int changed;
4001 CHECK_OVERLAY (overlay);
4003 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4005 for (tail = XOVERLAY (overlay)->plist;
4006 CONSP (tail) && CONSP (XCDR (tail));
4007 tail = XCDR (XCDR (tail)))
4008 if (EQ (XCAR (tail), prop))
4010 changed = !EQ (XCAR (XCDR (tail)), value);
4011 XSETCAR (XCDR (tail), value);
4012 goto found;
4014 /* It wasn't in the list, so add it to the front. */
4015 changed = !NILP (value);
4016 XOVERLAY (overlay)->plist
4017 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4018 found:
4019 if (! NILP (buffer))
4021 if (changed)
4022 modify_overlay (XBUFFER (buffer),
4023 marker_position (OVERLAY_START (overlay)),
4024 marker_position (OVERLAY_END (overlay)));
4025 if (EQ (prop, Qevaporate) && ! NILP (value)
4026 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4027 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4028 Fdelete_overlay (overlay);
4030 return value;
4033 /* Subroutine of report_overlay_modification. */
4035 /* Lisp vector holding overlay hook functions to call.
4036 Vector elements come in pairs.
4037 Each even-index element is a list of hook functions.
4038 The following odd-index element is the overlay they came from.
4040 Before the buffer change, we fill in this vector
4041 as we call overlay hook functions.
4042 After the buffer change, we get the functions to call from this vector.
4043 This way we always call the same functions before and after the change. */
4044 static Lisp_Object last_overlay_modification_hooks;
4046 /* Number of elements actually used in last_overlay_modification_hooks. */
4047 static int last_overlay_modification_hooks_used;
4049 /* Add one functionlist/overlay pair
4050 to the end of last_overlay_modification_hooks. */
4052 static void
4053 add_overlay_mod_hooklist (functionlist, overlay)
4054 Lisp_Object functionlist, overlay;
4056 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4058 if (last_overlay_modification_hooks_used == oldsize)
4060 Lisp_Object old;
4061 old = last_overlay_modification_hooks;
4062 last_overlay_modification_hooks
4063 = Fmake_vector (make_number (oldsize * 2), Qnil);
4064 bcopy (XVECTOR (old)->contents,
4065 XVECTOR (last_overlay_modification_hooks)->contents,
4066 sizeof (Lisp_Object) * oldsize);
4068 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = functionlist;
4069 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = overlay;
4072 /* Run the modification-hooks of overlays that include
4073 any part of the text in START to END.
4074 If this change is an insertion, also
4075 run the insert-before-hooks of overlay starting at END,
4076 and the insert-after-hooks of overlay ending at START.
4078 This is called both before and after the modification.
4079 AFTER is nonzero when we call after the modification.
4081 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4082 When AFTER is nonzero, they are the start position,
4083 the position after the inserted new text,
4084 and the length of deleted or replaced old text. */
4086 void
4087 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4088 Lisp_Object start, end;
4089 int after;
4090 Lisp_Object arg1, arg2, arg3;
4092 Lisp_Object prop, overlay;
4093 struct Lisp_Overlay *tail;
4094 /* 1 if this change is an insertion. */
4095 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4096 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4098 overlay = Qnil;
4099 tail = NULL;
4101 if (!after)
4103 /* We are being called before a change.
4104 Scan the overlays to find the functions to call. */
4105 last_overlay_modification_hooks_used = 0;
4106 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4108 int startpos, endpos;
4109 Lisp_Object ostart, oend;
4111 XSETMISC (overlay, tail);
4113 ostart = OVERLAY_START (overlay);
4114 oend = OVERLAY_END (overlay);
4115 endpos = OVERLAY_POSITION (oend);
4116 if (XFASTINT (start) > endpos)
4117 break;
4118 startpos = OVERLAY_POSITION (ostart);
4119 if (insertion && (XFASTINT (start) == startpos
4120 || XFASTINT (end) == startpos))
4122 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4123 if (!NILP (prop))
4124 add_overlay_mod_hooklist (prop, overlay);
4126 if (insertion && (XFASTINT (start) == endpos
4127 || XFASTINT (end) == endpos))
4129 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4130 if (!NILP (prop))
4131 add_overlay_mod_hooklist (prop, overlay);
4133 /* Test for intersecting intervals. This does the right thing
4134 for both insertion and deletion. */
4135 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4137 prop = Foverlay_get (overlay, Qmodification_hooks);
4138 if (!NILP (prop))
4139 add_overlay_mod_hooklist (prop, overlay);
4143 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4145 int startpos, endpos;
4146 Lisp_Object ostart, oend;
4148 XSETMISC (overlay, tail);
4150 ostart = OVERLAY_START (overlay);
4151 oend = OVERLAY_END (overlay);
4152 startpos = OVERLAY_POSITION (ostart);
4153 endpos = OVERLAY_POSITION (oend);
4154 if (XFASTINT (end) < startpos)
4155 break;
4156 if (insertion && (XFASTINT (start) == startpos
4157 || XFASTINT (end) == startpos))
4159 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4160 if (!NILP (prop))
4161 add_overlay_mod_hooklist (prop, overlay);
4163 if (insertion && (XFASTINT (start) == endpos
4164 || XFASTINT (end) == endpos))
4166 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4167 if (!NILP (prop))
4168 add_overlay_mod_hooklist (prop, overlay);
4170 /* Test for intersecting intervals. This does the right thing
4171 for both insertion and deletion. */
4172 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4174 prop = Foverlay_get (overlay, Qmodification_hooks);
4175 if (!NILP (prop))
4176 add_overlay_mod_hooklist (prop, overlay);
4181 GCPRO4 (overlay, arg1, arg2, arg3);
4183 /* Call the functions recorded in last_overlay_modification_hooks.
4184 First copy the vector contents, in case some of these hooks
4185 do subsequent modification of the buffer. */
4186 int size = last_overlay_modification_hooks_used;
4187 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4188 int i;
4190 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4191 copy, size * sizeof (Lisp_Object));
4192 gcpro1.var = copy;
4193 gcpro1.nvars = size;
4195 for (i = 0; i < size;)
4197 Lisp_Object prop, overlay;
4198 prop = copy[i++];
4199 overlay = copy[i++];
4200 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4203 UNGCPRO;
4206 static void
4207 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4208 Lisp_Object list, overlay;
4209 int after;
4210 Lisp_Object arg1, arg2, arg3;
4212 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4214 GCPRO4 (list, arg1, arg2, arg3);
4216 while (CONSP (list))
4218 if (NILP (arg3))
4219 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4220 else
4221 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4222 list = XCDR (list);
4224 UNGCPRO;
4227 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4228 property is set. */
4229 void
4230 evaporate_overlays (pos)
4231 EMACS_INT pos;
4233 Lisp_Object overlay, hit_list;
4234 struct Lisp_Overlay *tail;
4236 hit_list = Qnil;
4237 if (pos <= current_buffer->overlay_center)
4238 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4240 int endpos;
4241 XSETMISC (overlay, tail);
4242 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4243 if (endpos < pos)
4244 break;
4245 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4246 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4247 hit_list = Fcons (overlay, hit_list);
4249 else
4250 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4252 int startpos;
4253 XSETMISC (overlay, tail);
4254 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4255 if (startpos > pos)
4256 break;
4257 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4258 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4259 hit_list = Fcons (overlay, hit_list);
4261 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4262 Fdelete_overlay (XCAR (hit_list));
4265 /* Somebody has tried to store a value with an unacceptable type
4266 in the slot with offset OFFSET. */
4268 void
4269 buffer_slot_type_mismatch (offset)
4270 int offset;
4272 Lisp_Object sym;
4273 char *type_name;
4275 switch (XINT (PER_BUFFER_TYPE (offset)))
4277 case Lisp_Int:
4278 type_name = "integers";
4279 break;
4281 case Lisp_String:
4282 type_name = "strings";
4283 break;
4285 case Lisp_Symbol:
4286 type_name = "symbols";
4287 break;
4289 default:
4290 abort ();
4293 sym = PER_BUFFER_SYMBOL (offset);
4294 error ("Only %s should be stored in the buffer-local variable %s",
4295 type_name, SDATA (SYMBOL_NAME (sym)));
4299 /***********************************************************************
4300 Allocation with mmap
4301 ***********************************************************************/
4303 #ifdef USE_MMAP_FOR_BUFFERS
4305 #include <sys/types.h>
4306 #include <sys/mman.h>
4308 #ifndef MAP_ANON
4309 #ifdef MAP_ANONYMOUS
4310 #define MAP_ANON MAP_ANONYMOUS
4311 #else
4312 #define MAP_ANON 0
4313 #endif
4314 #endif
4316 #ifndef MAP_FAILED
4317 #define MAP_FAILED ((void *) -1)
4318 #endif
4320 #include <stdio.h>
4321 #include <errno.h>
4323 #if MAP_ANON == 0
4324 #include <fcntl.h>
4325 #endif
4327 #include "coding.h"
4330 /* Memory is allocated in regions which are mapped using mmap(2).
4331 The current implementation lets the system select mapped
4332 addresses; we're not using MAP_FIXED in general, except when
4333 trying to enlarge regions.
4335 Each mapped region starts with a mmap_region structure, the user
4336 area starts after that structure, aligned to MEM_ALIGN.
4338 +-----------------------+
4339 | struct mmap_info + |
4340 | padding |
4341 +-----------------------+
4342 | user data |
4345 +-----------------------+ */
4347 struct mmap_region
4349 /* User-specified size. */
4350 size_t nbytes_specified;
4352 /* Number of bytes mapped */
4353 size_t nbytes_mapped;
4355 /* Pointer to the location holding the address of the memory
4356 allocated with the mmap'd block. The variable actually points
4357 after this structure. */
4358 POINTER_TYPE **var;
4360 /* Next and previous in list of all mmap'd regions. */
4361 struct mmap_region *next, *prev;
4364 /* Doubly-linked list of mmap'd regions. */
4366 static struct mmap_region *mmap_regions;
4368 /* File descriptor for mmap. If we don't have anonymous mapping,
4369 /dev/zero will be opened on it. */
4371 static int mmap_fd;
4373 /* Temporary storage for mmap_set_vars, see there. */
4375 static struct mmap_region *mmap_regions_1;
4376 static int mmap_fd_1;
4378 /* Page size on this system. */
4380 static int mmap_page_size;
4382 /* 1 means mmap has been intialized. */
4384 static int mmap_initialized_p;
4386 /* Value is X rounded up to the next multiple of N. */
4388 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4390 /* Size of mmap_region structure plus padding. */
4392 #define MMAP_REGION_STRUCT_SIZE \
4393 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4395 /* Given a pointer P to the start of the user-visible part of a mapped
4396 region, return a pointer to the start of the region. */
4398 #define MMAP_REGION(P) \
4399 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4401 /* Given a pointer P to the start of a mapped region, return a pointer
4402 to the start of the user-visible part of the region. */
4404 #define MMAP_USER_AREA(P) \
4405 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4407 #define MEM_ALIGN sizeof (double)
4409 /* Predicate returning true if part of the address range [START .. END]
4410 is currently mapped. Used to prevent overwriting an existing
4411 memory mapping.
4413 Default is to conservativly assume the address range is occupied by
4414 something else. This can be overridden by system configuration
4415 files if system-specific means to determine this exists. */
4417 #ifndef MMAP_ALLOCATED_P
4418 #define MMAP_ALLOCATED_P(start, end) 1
4419 #endif
4421 /* Function prototypes. */
4423 static int mmap_free_1 P_ ((struct mmap_region *));
4424 static int mmap_enlarge P_ ((struct mmap_region *, int));
4425 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4426 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4427 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4428 static void mmap_free P_ ((POINTER_TYPE **ptr));
4429 static void mmap_init P_ ((void));
4432 /* Return a region overlapping address range START...END, or null if
4433 none. END is not including, i.e. the last byte in the range
4434 is at END - 1. */
4436 static struct mmap_region *
4437 mmap_find (start, end)
4438 POINTER_TYPE *start, *end;
4440 struct mmap_region *r;
4441 char *s = (char *) start, *e = (char *) end;
4443 for (r = mmap_regions; r; r = r->next)
4445 char *rstart = (char *) r;
4446 char *rend = rstart + r->nbytes_mapped;
4448 if (/* First byte of range, i.e. START, in this region? */
4449 (s >= rstart && s < rend)
4450 /* Last byte of range, i.e. END - 1, in this region? */
4451 || (e > rstart && e <= rend)
4452 /* First byte of this region in the range? */
4453 || (rstart >= s && rstart < e)
4454 /* Last byte of this region in the range? */
4455 || (rend > s && rend <= e))
4456 break;
4459 return r;
4463 /* Unmap a region. P is a pointer to the start of the user-araa of
4464 the region. Value is non-zero if successful. */
4466 static int
4467 mmap_free_1 (r)
4468 struct mmap_region *r;
4470 if (r->next)
4471 r->next->prev = r->prev;
4472 if (r->prev)
4473 r->prev->next = r->next;
4474 else
4475 mmap_regions = r->next;
4477 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4479 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4480 return 0;
4483 return 1;
4487 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4488 Value is non-zero if successful. */
4490 static int
4491 mmap_enlarge (r, npages)
4492 struct mmap_region *r;
4493 int npages;
4495 char *region_end = (char *) r + r->nbytes_mapped;
4496 size_t nbytes;
4497 int success = 0;
4499 if (npages < 0)
4501 /* Unmap pages at the end of the region. */
4502 nbytes = - npages * mmap_page_size;
4503 if (munmap (region_end - nbytes, nbytes) == -1)
4504 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4505 else
4507 r->nbytes_mapped -= nbytes;
4508 success = 1;
4511 else if (npages > 0)
4513 nbytes = npages * mmap_page_size;
4515 /* Try to map additional pages at the end of the region. We
4516 cannot do this if the address range is already occupied by
4517 something else because mmap deletes any previous mapping.
4518 I'm not sure this is worth doing, let's see. */
4519 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4521 POINTER_TYPE *p;
4523 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4524 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4525 if (p == MAP_FAILED)
4526 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4527 else if (p != (POINTER_TYPE *) region_end)
4529 /* Kernels are free to choose a different address. In
4530 that case, unmap what we've mapped above; we have
4531 no use for it. */
4532 if (munmap (p, nbytes) == -1)
4533 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4535 else
4537 r->nbytes_mapped += nbytes;
4538 success = 1;
4543 return success;
4547 /* Set or reset variables holding references to mapped regions. If
4548 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4549 non-zero, set all variables to the start of the user-areas
4550 of mapped regions.
4552 This function is called from Fdump_emacs to ensure that the dumped
4553 Emacs doesn't contain references to memory that won't be mapped
4554 when Emacs starts. */
4556 void
4557 mmap_set_vars (restore_p)
4558 int restore_p;
4560 struct mmap_region *r;
4562 if (restore_p)
4564 mmap_regions = mmap_regions_1;
4565 mmap_fd = mmap_fd_1;
4566 for (r = mmap_regions; r; r = r->next)
4567 *r->var = MMAP_USER_AREA (r);
4569 else
4571 for (r = mmap_regions; r; r = r->next)
4572 *r->var = NULL;
4573 mmap_regions_1 = mmap_regions;
4574 mmap_regions = NULL;
4575 mmap_fd_1 = mmap_fd;
4576 mmap_fd = -1;
4581 /* Allocate a block of storage large enough to hold NBYTES bytes of
4582 data. A pointer to the data is returned in *VAR. VAR is thus the
4583 address of some variable which will use the data area.
4585 The allocation of 0 bytes is valid.
4587 If we can't allocate the necessary memory, set *VAR to null, and
4588 return null. */
4590 static POINTER_TYPE *
4591 mmap_alloc (var, nbytes)
4592 POINTER_TYPE **var;
4593 size_t nbytes;
4595 void *p;
4596 size_t map;
4598 mmap_init ();
4600 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4601 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4602 mmap_fd, 0);
4604 if (p == MAP_FAILED)
4606 if (errno != ENOMEM)
4607 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4608 p = NULL;
4610 else
4612 struct mmap_region *r = (struct mmap_region *) p;
4614 r->nbytes_specified = nbytes;
4615 r->nbytes_mapped = map;
4616 r->var = var;
4617 r->prev = NULL;
4618 r->next = mmap_regions;
4619 if (r->next)
4620 r->next->prev = r;
4621 mmap_regions = r;
4623 p = MMAP_USER_AREA (p);
4626 return *var = p;
4630 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4631 resize it to size NBYTES. Change *VAR to reflect the new block,
4632 and return this value. If more memory cannot be allocated, then
4633 leave *VAR unchanged, and return null. */
4635 static POINTER_TYPE *
4636 mmap_realloc (var, nbytes)
4637 POINTER_TYPE **var;
4638 size_t nbytes;
4640 POINTER_TYPE *result;
4642 mmap_init ();
4644 if (*var == NULL)
4645 result = mmap_alloc (var, nbytes);
4646 else if (nbytes == 0)
4648 mmap_free (var);
4649 result = mmap_alloc (var, nbytes);
4651 else
4653 struct mmap_region *r = MMAP_REGION (*var);
4654 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4656 if (room < nbytes)
4658 /* Must enlarge. */
4659 POINTER_TYPE *old_ptr = *var;
4661 /* Try to map additional pages at the end of the region.
4662 If that fails, allocate a new region, copy data
4663 from the old region, then free it. */
4664 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4665 / mmap_page_size)))
4667 r->nbytes_specified = nbytes;
4668 *var = result = old_ptr;
4670 else if (mmap_alloc (var, nbytes))
4672 bcopy (old_ptr, *var, r->nbytes_specified);
4673 mmap_free_1 (MMAP_REGION (old_ptr));
4674 result = *var;
4675 r = MMAP_REGION (result);
4676 r->nbytes_specified = nbytes;
4678 else
4680 *var = old_ptr;
4681 result = NULL;
4684 else if (room - nbytes >= mmap_page_size)
4686 /* Shrinking by at least a page. Let's give some
4687 memory back to the system.
4689 The extra parens are to make the division happens first,
4690 on positive values, so we know it will round towards
4691 zero. */
4692 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4693 result = *var;
4694 r->nbytes_specified = nbytes;
4696 else
4698 /* Leave it alone. */
4699 result = *var;
4700 r->nbytes_specified = nbytes;
4704 return result;
4708 /* Free a block of relocatable storage whose data is pointed to by
4709 PTR. Store 0 in *PTR to show there's no block allocated. */
4711 static void
4712 mmap_free (var)
4713 POINTER_TYPE **var;
4715 mmap_init ();
4717 if (*var)
4719 mmap_free_1 (MMAP_REGION (*var));
4720 *var = NULL;
4725 /* Perform necessary intializations for the use of mmap. */
4727 static void
4728 mmap_init ()
4730 #if MAP_ANON == 0
4731 /* The value of mmap_fd is initially 0 in temacs, and -1
4732 in a dumped Emacs. */
4733 if (mmap_fd <= 0)
4735 /* No anonymous mmap -- we need the file descriptor. */
4736 mmap_fd = open ("/dev/zero", O_RDONLY);
4737 if (mmap_fd == -1)
4738 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4740 #endif /* MAP_ANON == 0 */
4742 if (mmap_initialized_p)
4743 return;
4744 mmap_initialized_p = 1;
4746 #if MAP_ANON != 0
4747 mmap_fd = -1;
4748 #endif
4750 mmap_page_size = getpagesize ();
4753 #endif /* USE_MMAP_FOR_BUFFERS */
4757 /***********************************************************************
4758 Buffer-text Allocation
4759 ***********************************************************************/
4761 #ifdef REL_ALLOC
4762 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4763 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4764 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4765 #endif /* REL_ALLOC */
4768 /* Allocate NBYTES bytes for buffer B's text buffer. */
4770 static void
4771 alloc_buffer_text (b, nbytes)
4772 struct buffer *b;
4773 size_t nbytes;
4775 POINTER_TYPE *p;
4777 BLOCK_INPUT;
4778 #if defined USE_MMAP_FOR_BUFFERS
4779 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4780 #elif defined REL_ALLOC
4781 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4782 #else
4783 p = xmalloc (nbytes);
4784 #endif
4786 if (p == NULL)
4788 UNBLOCK_INPUT;
4789 memory_full ();
4792 b->text->beg = (unsigned char *) p;
4793 UNBLOCK_INPUT;
4796 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4797 shrink it. */
4799 void
4800 enlarge_buffer_text (b, delta)
4801 struct buffer *b;
4802 int delta;
4804 POINTER_TYPE *p;
4805 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4806 + delta);
4807 BLOCK_INPUT;
4808 #if defined USE_MMAP_FOR_BUFFERS
4809 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4810 #elif defined REL_ALLOC
4811 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4812 #else
4813 p = xrealloc (b->text->beg, nbytes);
4814 #endif
4816 if (p == NULL)
4818 UNBLOCK_INPUT;
4819 memory_full ();
4822 BUF_BEG_ADDR (b) = (unsigned char *) p;
4823 UNBLOCK_INPUT;
4827 /* Free buffer B's text buffer. */
4829 static void
4830 free_buffer_text (b)
4831 struct buffer *b;
4833 BLOCK_INPUT;
4835 #if defined USE_MMAP_FOR_BUFFERS
4836 mmap_free ((POINTER_TYPE **) &b->text->beg);
4837 #elif defined REL_ALLOC
4838 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4839 #else
4840 xfree (b->text->beg);
4841 #endif
4843 BUF_BEG_ADDR (b) = NULL;
4844 UNBLOCK_INPUT;
4849 /***********************************************************************
4850 Initialization
4851 ***********************************************************************/
4853 void
4854 init_buffer_once ()
4856 int idx;
4858 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
4860 /* Make sure all markable slots in buffer_defaults
4861 are initialized reasonably, so mark_buffer won't choke. */
4862 reset_buffer (&buffer_defaults);
4863 reset_buffer_local_variables (&buffer_defaults, 1);
4864 reset_buffer (&buffer_local_symbols);
4865 reset_buffer_local_variables (&buffer_local_symbols, 1);
4866 /* Prevent GC from getting confused. */
4867 buffer_defaults.text = &buffer_defaults.own_text;
4868 buffer_local_symbols.text = &buffer_local_symbols.own_text;
4869 BUF_INTERVALS (&buffer_defaults) = 0;
4870 BUF_INTERVALS (&buffer_local_symbols) = 0;
4871 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4872 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
4874 /* Set up the default values of various buffer slots. */
4875 /* Must do these before making the first buffer! */
4877 /* real setup is done in bindings.el */
4878 buffer_defaults.mode_line_format = build_string ("%-");
4879 buffer_defaults.header_line_format = Qnil;
4880 buffer_defaults.abbrev_mode = Qnil;
4881 buffer_defaults.overwrite_mode = Qnil;
4882 buffer_defaults.case_fold_search = Qt;
4883 buffer_defaults.auto_fill_function = Qnil;
4884 buffer_defaults.selective_display = Qnil;
4885 #ifndef old
4886 buffer_defaults.selective_display_ellipses = Qt;
4887 #endif
4888 buffer_defaults.abbrev_table = Qnil;
4889 buffer_defaults.display_table = Qnil;
4890 buffer_defaults.undo_list = Qnil;
4891 buffer_defaults.mark_active = Qnil;
4892 buffer_defaults.file_format = Qnil;
4893 buffer_defaults.overlays_before = NULL;
4894 buffer_defaults.overlays_after = NULL;
4895 buffer_defaults.overlay_center = BEG;
4897 XSETFASTINT (buffer_defaults.tab_width, 8);
4898 buffer_defaults.truncate_lines = Qnil;
4899 buffer_defaults.ctl_arrow = Qt;
4900 buffer_defaults.direction_reversed = Qnil;
4901 buffer_defaults.cursor_type = Qt;
4902 buffer_defaults.extra_line_spacing = Qnil;
4904 #ifdef DOS_NT
4905 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
4906 #endif
4907 buffer_defaults.enable_multibyte_characters = Qt;
4908 buffer_defaults.buffer_file_coding_system = Qnil;
4909 XSETFASTINT (buffer_defaults.fill_column, 70);
4910 XSETFASTINT (buffer_defaults.left_margin, 0);
4911 buffer_defaults.cache_long_line_scans = Qnil;
4912 buffer_defaults.file_truename = Qnil;
4913 XSETFASTINT (buffer_defaults.display_count, 0);
4914 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
4915 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
4916 buffer_defaults.left_fringe_width = Qnil;
4917 buffer_defaults.right_fringe_width = Qnil;
4918 buffer_defaults.fringes_outside_margins = Qnil;
4919 buffer_defaults.scroll_bar_width = Qnil;
4920 buffer_defaults.vertical_scroll_bar_type = Qt;
4921 buffer_defaults.indicate_empty_lines = Qnil;
4922 buffer_defaults.scroll_up_aggressively = Qnil;
4923 buffer_defaults.scroll_down_aggressively = Qnil;
4924 buffer_defaults.display_time = Qnil;
4926 /* Assign the local-flags to the slots that have default values.
4927 The local flag is a bit that is used in the buffer
4928 to say that it has its own local value for the slot.
4929 The local flag bits are in the local_var_flags slot of the buffer. */
4931 /* Nothing can work if this isn't true */
4932 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
4934 /* 0 means not a lisp var, -1 means always local, else mask */
4935 bzero (&buffer_local_flags, sizeof buffer_local_flags);
4936 XSETINT (buffer_local_flags.filename, -1);
4937 XSETINT (buffer_local_flags.directory, -1);
4938 XSETINT (buffer_local_flags.backed_up, -1);
4939 XSETINT (buffer_local_flags.save_length, -1);
4940 XSETINT (buffer_local_flags.auto_save_file_name, -1);
4941 XSETINT (buffer_local_flags.read_only, -1);
4942 XSETINT (buffer_local_flags.major_mode, -1);
4943 XSETINT (buffer_local_flags.mode_name, -1);
4944 XSETINT (buffer_local_flags.undo_list, -1);
4945 XSETINT (buffer_local_flags.mark_active, -1);
4946 XSETINT (buffer_local_flags.point_before_scroll, -1);
4947 XSETINT (buffer_local_flags.file_truename, -1);
4948 XSETINT (buffer_local_flags.invisibility_spec, -1);
4949 XSETINT (buffer_local_flags.file_format, -1);
4950 XSETINT (buffer_local_flags.display_count, -1);
4951 XSETINT (buffer_local_flags.display_time, -1);
4952 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
4954 idx = 1;
4955 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
4956 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
4957 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
4958 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
4959 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
4960 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
4961 #ifndef old
4962 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
4963 #endif
4964 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
4965 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
4966 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
4967 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
4968 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
4969 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
4970 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
4971 #ifdef DOS_NT
4972 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
4973 /* Make this one a permanent local. */
4974 buffer_permanent_local_flags[idx++] = 1;
4975 #endif
4976 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
4977 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
4978 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
4979 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
4980 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
4981 /* Make this one a permanent local. */
4982 buffer_permanent_local_flags[idx++] = 1;
4983 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
4984 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
4985 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
4986 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
4987 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
4988 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
4989 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
4990 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
4991 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
4992 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
4993 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
4994 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
4995 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
4997 /* Need more room? */
4998 if (idx >= MAX_PER_BUFFER_VARS)
4999 abort ();
5000 last_per_buffer_idx = idx;
5002 Vbuffer_alist = Qnil;
5003 current_buffer = 0;
5004 all_buffers = 0;
5006 QSFundamental = build_string ("Fundamental");
5008 Qfundamental_mode = intern ("fundamental-mode");
5009 buffer_defaults.major_mode = Qfundamental_mode;
5011 Qmode_class = intern ("mode-class");
5013 Qprotected_field = intern ("protected-field");
5015 Qpermanent_local = intern ("permanent-local");
5017 Qkill_buffer_hook = intern ("kill-buffer-hook");
5018 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5020 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5022 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5024 /* super-magic invisible buffer */
5025 Vbuffer_alist = Qnil;
5027 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5029 inhibit_modification_hooks = 0;
5032 void
5033 init_buffer ()
5035 char buf[MAXPATHLEN + 1];
5036 char *pwd;
5037 struct stat dotstat, pwdstat;
5038 Lisp_Object temp;
5039 int rc;
5041 #ifdef USE_MMAP_FOR_BUFFERS
5043 /* When using the ralloc implementation based on mmap(2), buffer
5044 text pointers will have been set to null in the dumped Emacs.
5045 Map new memory. */
5046 struct buffer *b;
5048 for (b = all_buffers; b; b = b->next)
5049 if (b->text->beg == NULL)
5050 enlarge_buffer_text (b, 0);
5052 #endif /* USE_MMAP_FOR_BUFFERS */
5054 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5055 if (NILP (buffer_defaults.enable_multibyte_characters))
5056 Fset_buffer_multibyte (Qnil);
5058 /* If PWD is accurate, use it instead of calling getwd. PWD is
5059 sometimes a nicer name, and using it may avoid a fatal error if a
5060 parent directory is searchable but not readable. */
5061 if ((pwd = getenv ("PWD")) != 0
5062 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
5063 && stat (pwd, &pwdstat) == 0
5064 && stat (".", &dotstat) == 0
5065 && dotstat.st_ino == pwdstat.st_ino
5066 && dotstat.st_dev == pwdstat.st_dev
5067 && strlen (pwd) < MAXPATHLEN)
5068 strcpy (buf, pwd);
5069 #ifdef HAVE_GETCWD
5070 else if (getcwd (buf, MAXPATHLEN+1) == 0)
5071 fatal ("`getcwd' failed: %s\n", strerror (errno));
5072 #else
5073 else if (getwd (buf) == 0)
5074 fatal ("`getwd' failed: %s\n", buf);
5075 #endif
5077 #ifndef VMS
5078 /* Maybe this should really use some standard subroutine
5079 whose definition is filename syntax dependent. */
5080 rc = strlen (buf);
5081 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
5083 buf[rc] = DIRECTORY_SEP;
5084 buf[rc + 1] = '\0';
5086 #endif /* not VMS */
5088 current_buffer->directory = build_string (buf);
5090 /* Add /: to the front of the name
5091 if it would otherwise be treated as magic. */
5092 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5093 if (! NILP (temp)
5094 /* If the default dir is just /, TEMP is non-nil
5095 because of the ange-ftp completion handler.
5096 However, it is not necessary to turn / into /:/.
5097 So avoid doing that. */
5098 && strcmp ("/", SDATA (current_buffer->directory)))
5099 current_buffer->directory
5100 = concat2 (build_string ("/:"), current_buffer->directory);
5102 temp = get_minibuffer (0);
5103 XBUFFER (temp)->directory = current_buffer->directory;
5106 /* initialize the buffer routines */
5107 void
5108 syms_of_buffer ()
5110 staticpro (&last_overlay_modification_hooks);
5111 last_overlay_modification_hooks
5112 = Fmake_vector (make_number (10), Qnil);
5114 staticpro (&Vbuffer_defaults);
5115 staticpro (&Vbuffer_local_symbols);
5116 staticpro (&Qfundamental_mode);
5117 staticpro (&Qmode_class);
5118 staticpro (&QSFundamental);
5119 staticpro (&Vbuffer_alist);
5120 staticpro (&Qprotected_field);
5121 staticpro (&Qpermanent_local);
5122 staticpro (&Qkill_buffer_hook);
5123 Qoverlayp = intern ("overlayp");
5124 staticpro (&Qoverlayp);
5125 Qevaporate = intern ("evaporate");
5126 staticpro (&Qevaporate);
5127 Qmodification_hooks = intern ("modification-hooks");
5128 staticpro (&Qmodification_hooks);
5129 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5130 staticpro (&Qinsert_in_front_hooks);
5131 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5132 staticpro (&Qinsert_behind_hooks);
5133 Qget_file_buffer = intern ("get-file-buffer");
5134 staticpro (&Qget_file_buffer);
5135 Qpriority = intern ("priority");
5136 staticpro (&Qpriority);
5137 Qwindow = intern ("window");
5138 staticpro (&Qwindow);
5139 Qbefore_string = intern ("before-string");
5140 staticpro (&Qbefore_string);
5141 Qafter_string = intern ("after-string");
5142 staticpro (&Qafter_string);
5143 Qfirst_change_hook = intern ("first-change-hook");
5144 staticpro (&Qfirst_change_hook);
5145 Qbefore_change_functions = intern ("before-change-functions");
5146 staticpro (&Qbefore_change_functions);
5147 Qafter_change_functions = intern ("after-change-functions");
5148 staticpro (&Qafter_change_functions);
5149 staticpro (&Qucs_set_table_for_input);
5151 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5152 staticpro (&Qkill_buffer_query_functions);
5154 Fput (Qprotected_field, Qerror_conditions,
5155 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5156 Fput (Qprotected_field, Qerror_message,
5157 build_string ("Attempt to modify a protected field"));
5159 /* All these use DEFVAR_LISP_NOPRO because the slots in
5160 buffer_defaults will all be marked via Vbuffer_defaults. */
5162 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5163 &buffer_defaults.mode_line_format,
5164 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5165 This is the same as (default-value 'mode-line-format). */);
5167 DEFVAR_LISP_NOPRO ("default-header-line-format",
5168 &buffer_defaults.header_line_format,
5169 doc: /* Default value of `header-line-format' for buffers that don't override it.
5170 This is the same as (default-value 'header-line-format). */);
5172 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5173 doc: /* Default value of `cursor-type' for buffers that don't override it.
5174 This is the same as (default-value 'cursor-type). */);
5176 DEFVAR_LISP_NOPRO ("default-line-spacing",
5177 &buffer_defaults.extra_line_spacing,
5178 doc: /* Default value of `line-spacing' for buffers that don't override it.
5179 This is the same as (default-value 'line-spacing). */);
5181 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5182 &buffer_defaults.abbrev_mode,
5183 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5184 This is the same as (default-value 'abbrev-mode). */);
5186 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5187 &buffer_defaults.ctl_arrow,
5188 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5189 This is the same as (default-value 'ctl-arrow). */);
5191 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5192 &buffer_defaults.direction_reversed,
5193 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5194 This is the same as (default-value 'direction-reversed). */);
5196 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5197 &buffer_defaults.enable_multibyte_characters,
5198 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5199 This is the same as (default-value 'enable-multibyte-characters). */);
5201 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5202 &buffer_defaults.buffer_file_coding_system,
5203 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5204 This is the same as (default-value 'buffer-file-coding-system). */);
5206 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5207 &buffer_defaults.truncate_lines,
5208 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5209 This is the same as (default-value 'truncate-lines). */);
5211 DEFVAR_LISP_NOPRO ("default-fill-column",
5212 &buffer_defaults.fill_column,
5213 doc: /* Default value of `fill-column' for buffers that do not override it.
5214 This is the same as (default-value 'fill-column). */);
5216 DEFVAR_LISP_NOPRO ("default-left-margin",
5217 &buffer_defaults.left_margin,
5218 doc: /* Default value of `left-margin' for buffers that do not override it.
5219 This is the same as (default-value 'left-margin). */);
5221 DEFVAR_LISP_NOPRO ("default-tab-width",
5222 &buffer_defaults.tab_width,
5223 doc: /* Default value of `tab-width' for buffers that do not override it.
5224 This is the same as (default-value 'tab-width). */);
5226 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5227 &buffer_defaults.case_fold_search,
5228 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5229 This is the same as (default-value 'case-fold-search). */);
5231 #ifdef DOS_NT
5232 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5233 &buffer_defaults.buffer_file_type,
5234 doc: /* Default file type for buffers that do not override it.
5235 This is the same as (default-value 'buffer-file-type).
5236 The file type is nil for text, t for binary. */);
5237 #endif
5239 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5240 &buffer_defaults.left_margin_cols,
5241 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5242 This is the same as (default-value 'left-margin-width). */);
5244 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5245 &buffer_defaults.right_margin_cols,
5246 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5247 This is the same as (default-value 'right-margin-width). */);
5249 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5250 &buffer_defaults.left_fringe_width,
5251 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5252 This is the same as (default-value 'left-fringe-width). */);
5254 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5255 &buffer_defaults.right_fringe_width,
5256 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5257 This is the same as (default-value 'right-fringe-width). */);
5259 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5260 &buffer_defaults.fringes_outside_margins,
5261 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5262 This is the same as (default-value 'fringes-outside-margins). */);
5264 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5265 &buffer_defaults.scroll_bar_width,
5266 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5267 This is the same as (default-value 'scroll-bar-width). */);
5269 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5270 &buffer_defaults.vertical_scroll_bar_type,
5271 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5272 This is the same as (default-value 'vertical-scroll-bar). */);
5274 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5275 &buffer_defaults.indicate_empty_lines,
5276 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5277 This is the same as (default-value 'indicate-empty-lines). */);
5279 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5280 &buffer_defaults.scroll_up_aggressively,
5281 doc: /* Default value of `scroll-up-aggressively'.
5282 This value applies in buffers that don't have their own local values.
5283 This variable is an alias for (default-value 'scroll-up-aggressively). */);
5285 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5286 &buffer_defaults.scroll_down_aggressively,
5287 doc: /* Default value of `scroll-down-aggressively'.
5288 This value applies in buffers that don't have their own local values.
5289 This variable is an alias for (default-value 'scroll-down-aggressively). */);
5291 DEFVAR_PER_BUFFER ("header-line-format",
5292 &current_buffer->header_line_format,
5293 Qnil,
5294 doc: /* Analogous to `mode-line-format', but controls the header line.
5295 The header line appears, optionally, at the top of a window;
5296 the mode line appears at the bottom. */);
5298 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5299 Qnil,
5300 doc: /* Template for displaying mode line for current buffer.
5301 Each buffer has its own value of this variable.
5302 Value may be nil, a string, a symbol or a list or cons cell.
5303 A value of nil means don't display a mode line.
5304 For a symbol, its value is used (but it is ignored if t or nil).
5305 A string appearing directly as the value of a symbol is processed verbatim
5306 in that the %-constructs below are not recognized.
5307 Note that unless the symbol is marked as a `risky-local-variable', all
5308 properties in any strings, as well as all :eval and :propertize forms
5309 in the value of that symbol will be ignored.
5310 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5311 is used as a mode line element. Be careful--FORM should not load any files,
5312 because that can cause an infinite recursion.
5313 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5314 with the specified properties PROPS applied.
5315 For a list whose car is a symbol, the symbol's value is taken,
5316 and if that is non-nil, the cadr of the list is processed recursively.
5317 Otherwise, the caddr of the list (if there is one) is processed.
5318 For a list whose car is a string or list, each element is processed
5319 recursively and the results are effectively concatenated.
5320 For a list whose car is an integer, the cdr of the list is processed
5321 and padded (if the number is positive) or truncated (if negative)
5322 to the width specified by that number.
5323 A string is printed verbatim in the mode line except for %-constructs:
5324 (%-constructs are allowed when the string is the entire mode-line-format
5325 or when it is found in a cons-cell or a list)
5326 %b -- print buffer name. %f -- print visited file name.
5327 %F -- print frame name.
5328 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5329 %& is like %*, but ignore read-only-ness.
5330 % means buffer is read-only and * means it is modified.
5331 For a modified read-only buffer, %* gives % and %+ gives *.
5332 %s -- print process status. %l -- print the current line number.
5333 %c -- print the current column number (this makes editing slower).
5334 To make the column number update correctly in all cases,
5335 `column-number-mode' must be non-nil.
5336 %p -- print percent of buffer above top of window, or Top, Bot or All.
5337 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5338 or print Bottom or All.
5339 %m -- print the mode name.
5340 %n -- print Narrow if appropriate.
5341 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.
5342 %Z -- like %z, but including the end-of-line format.
5343 %[ -- print one [ for each recursive editing level. %] similar.
5344 %% -- print %. %- -- print infinitely many dashes.
5345 Decimal digits after the % specify field width to which to pad. */);
5347 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5348 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5349 nil here means use current buffer's major mode. */);
5351 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5352 make_number (Lisp_Symbol),
5353 doc: /* Symbol for current buffer's major mode. */);
5355 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5356 Qnil,
5357 doc: /* Pretty name of current buffer's major mode (a string). */);
5359 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5360 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5362 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5363 Qnil,
5364 doc: /* *Non-nil if searches and matches should ignore case. */);
5366 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5367 make_number (Lisp_Int),
5368 doc: /* *Column beyond which automatic line-wrapping should happen.
5369 Interactively, you can set this using \\[set-fill-column]. */);
5371 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5372 make_number (Lisp_Int),
5373 doc: /* *Column for the default indent-line-function to indent to.
5374 Linefeed indents to this column in Fundamental mode. */);
5376 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5377 make_number (Lisp_Int),
5378 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5380 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5381 doc: /* *Non-nil means display control chars with uparrow.
5382 A value of nil means use backslash and octal digits.
5383 This variable does not apply to characters whose display is specified
5384 in the current display table (if there is one). */);
5386 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5387 &current_buffer->enable_multibyte_characters,
5388 Qnil,
5389 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5390 Otherwise they are regarded as unibyte. This affects the display,
5391 file I/O and the behavior of various editing commands.
5393 This variable is buffer-local but you cannot set it directly;
5394 use the function `set-buffer-multibyte' to change a buffer's representation.
5395 Changing its default value with `setq-default' is supported.
5396 See also variable `default-enable-multibyte-characters' and Info node
5397 `(elisp)Text Representations'. */);
5398 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5400 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5401 &current_buffer->buffer_file_coding_system, Qnil,
5402 doc: /* Coding system to be used for encoding the buffer contents on saving.
5403 This variable applies to saving the buffer, and also to `write-region'
5404 and other functions that use `write-region'.
5405 It does not apply to sending output to subprocesses, however.
5407 If this is nil, the buffer is saved without any code conversion
5408 unless some coding system is specified in `file-coding-system-alist'
5409 for the buffer file.
5411 If the text to be saved cannot be encoded as specified by this variable,
5412 an alternative encoding is selected by `select-safe-coding-system', which see.
5414 The variable `coding-system-for-write', if non-nil, overrides this variable.
5416 This variable is never applied to a way of decoding a file while reading it. */);
5418 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5419 Qnil,
5420 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5422 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5423 doc: /* *Non-nil means do not display continuation lines.
5424 Instead, give each line of text just one screen line.
5426 Note that this is overridden by the variable
5427 `truncate-partial-width-windows' if that variable is non-nil
5428 and this buffer is not full-frame width. */);
5430 #ifdef DOS_NT
5431 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5432 Qnil,
5433 doc: /* Non-nil if the visited file is a binary file.
5434 This variable is meaningful on MS-DOG and Windows NT.
5435 On those systems, it is automatically local in every buffer.
5436 On other systems, this variable is normally always nil. */);
5437 #endif
5439 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5440 make_number (Lisp_String),
5441 doc: /* Name of default directory of current buffer. Should end with slash.
5442 To interactively change the default directory, use command `cd'. */);
5444 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5445 Qnil,
5446 doc: /* Function called (if non-nil) to perform auto-fill.
5447 It is called after self-inserting any character specified in
5448 the `auto-fill-chars' table.
5449 NOTE: This variable is not a hook;
5450 its value may not be a list of functions. */);
5452 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5453 make_number (Lisp_String),
5454 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5456 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5457 make_number (Lisp_String),
5458 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5459 The truename of a file is calculated by `file-truename'
5460 and then abbreviated with `abbreviate-file-name'. */);
5462 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5463 &current_buffer->auto_save_file_name,
5464 make_number (Lisp_String),
5465 doc: /* Name of file for auto-saving current buffer.
5466 If it is nil, that means don't auto-save this buffer. */);
5468 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5469 doc: /* Non-nil if this buffer is read-only. */);
5471 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5472 doc: /* Non-nil if this buffer's file has been backed up.
5473 Backing up is done before the first time the file is saved. */);
5475 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5476 make_number (Lisp_Int),
5477 doc: /* Length of current buffer when last read in, saved or auto-saved.
5478 0 initially. */);
5480 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5481 Qnil,
5482 doc: /* Non-nil enables selective display.
5483 An Integer N as value means display only lines
5484 that start with less than n columns of space.
5485 A value of t means that the character ^M makes itself and
5486 all the rest of the line invisible; also, when saving the buffer
5487 in a file, save the ^M as a newline. */);
5489 #ifndef old
5490 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5491 &current_buffer->selective_display_ellipses,
5492 Qnil,
5493 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5494 #endif
5496 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5497 doc: /* Non-nil if self-insertion should replace existing text.
5498 The value should be one of `overwrite-mode-textual',
5499 `overwrite-mode-binary', or nil.
5500 If it is `overwrite-mode-textual', self-insertion still
5501 inserts at the end of a line, and inserts when point is before a tab,
5502 until the tab is filled in.
5503 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5505 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5506 Qnil,
5507 doc: /* Display table that controls display of the contents of current buffer.
5509 If this variable is nil, the value of `standard-display-table' is used.
5510 Each window can have its own, overriding display table, see
5511 `set-window-display-table' and `window-display-table'.
5513 The display table is a char-table created with `make-display-table'.
5514 A char-table is an array indexed by character codes. Normal array
5515 primitives `aref' and `aset' can be used to access elements of a char-table.
5517 Each of the char-table elements control how to display the corresponding
5518 text character: the element at index C in the table says how to display
5519 the character whose code is C. Each element should be a vector of
5520 characters or nil. nil means display the character in the default fashion;
5521 otherwise, the characters from the vector are delivered to the screen
5522 instead of the original character.
5524 For example, (aset buffer-display-table ?X ?Y) will cause Emacs to display
5525 a capital Y instead of each X character.
5527 In addition, a char-table has six extra slots to control the display of:
5529 the end of a truncated screen line (extra-slot 0, a single character);
5530 the end of a continued line (extra-slot 1, a single character);
5531 the escape character used to display character codes in octal
5532 (extra-slot 2, a single character);
5533 the character used as an arrow for control characters (extra-slot 3,
5534 a single character);
5535 the decoration indicating the presence of invisible lines (extra-slot 4,
5536 a vector of characters);
5537 the character used to draw the border between side-by-side windows
5538 (extra-slot 5, a single character).
5540 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5542 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5543 Qnil,
5544 doc: /* *Width of left marginal area for display of a buffer.
5545 A value of nil means no marginal area. */);
5547 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5548 Qnil,
5549 doc: /* *Width of right marginal area for display of a buffer.
5550 A value of nil means no marginal area. */);
5552 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5553 Qnil,
5554 doc: /* *Width of this buffer's left fringe (in pixels).
5555 A value of 0 means no left fringe is shown in this buffer's window.
5556 A value of nil means to use the left fringe width from the window's frame. */);
5558 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5559 Qnil,
5560 doc: /* *Width of this buffer's right fringe (in pixels).
5561 A value of 0 means no right fringe is shown in this buffer's window.
5562 A value of nil means to use the right fringe width from the window's frame. */);
5564 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5565 Qnil,
5566 doc: /* *Non-nil means to display fringes outside display margins.
5567 A value of nil means to display fringes between margins and buffer text. */);
5569 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5570 Qnil,
5571 doc: /* *Width of this buffer's scroll bars in pixels.
5572 A value of nil means to use the scroll bar width from the window's frame. */);
5574 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5575 Qnil,
5576 doc: /* *Position of this buffer's vertical scroll bar.
5577 A value of left or right means to place the vertical scroll bar at that side
5578 of the window; a value of nil means that this window has no vertical scroll bar.
5579 A value of t means to use the vertical scroll bar type from the window's frame. */);
5581 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5582 &current_buffer->indicate_empty_lines, Qnil,
5583 doc: /* *Visually indicate empty lines after the buffer end.
5584 If non-nil, a bitmap is displayed in the left fringe of a window on
5585 window-systems. */);
5587 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5588 &current_buffer->scroll_up_aggressively, Qnil,
5589 doc: /* How far to scroll windows upward.
5590 If you move point off the bottom, the window scrolls automatically.
5591 This variable controls how far it scrolls. nil, the default,
5592 means scroll to center point. A fraction means scroll to put point
5593 that fraction of the window's height from the bottom of the window.
5594 When the value is 0.0, point goes at the bottom line, which in the simple
5595 case that you moved off with C-f means scrolling just one line. 1.0 means
5596 point goes at the top, so that in that simple case, the window
5597 window scrolls by a full window height. Meaningful values are
5598 between 0.0 and 1.0, inclusive. */);
5600 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5601 &current_buffer->scroll_down_aggressively, Qnil,
5602 doc: /* How far to scroll windows downward.
5603 If you move point off the top, the window scrolls automatically.
5604 This variable controls how far it scrolls. nil, the default,
5605 means scroll to center point. A fraction means scroll to put point
5606 that fraction of the window's height from the top of the window.
5607 When the value is 0.0, point goes at the top line, which in the simple
5608 case that you moved off with C-b means scrolling just one line. 1.0 means
5609 point goes at the bottom, so that in that simple case, the window
5610 window scrolls by a full window height. Meaningful values are
5611 between 0.0 and 1.0, inclusive. */);
5613 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5614 "Don't ask.");
5617 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5618 doc: /* List of functions to call before each text change.
5619 Two arguments are passed to each function: the positions of
5620 the beginning and end of the range of old text to be changed.
5621 \(For an insertion, the beginning and end are at the same place.)
5622 No information is given about the length of the text after the change.
5624 Buffer changes made while executing the `before-change-functions'
5625 don't call any before-change or after-change functions.
5626 That's because these variables are temporarily set to nil.
5627 As a result, a hook function cannot straightforwardly alter the value of
5628 these variables. See the Emacs Lisp manual for a way of
5629 accomplishing an equivalent result by using other variables.
5631 If an unhandled error happens in running these functions,
5632 the variable's value remains nil. That prevents the error
5633 from happening repeatedly and making Emacs nonfunctional. */);
5634 Vbefore_change_functions = Qnil;
5636 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
5637 doc: /* List of functions to call after each text change.
5638 Three arguments are passed to each function: the positions of
5639 the beginning and end of the range of changed text,
5640 and the length in bytes of the pre-change text replaced by that range.
5641 \(For an insertion, the pre-change length is zero;
5642 for a deletion, that length is the number of bytes deleted,
5643 and the post-change beginning and end are at the same place.)
5645 Buffer changes made while executing the `after-change-functions'
5646 don't call any before-change or after-change functions.
5647 That's because these variables are temporarily set to nil.
5648 As a result, a hook function cannot straightforwardly alter the value of
5649 these variables. See the Emacs Lisp manual for a way of
5650 accomplishing an equivalent result by using other variables.
5652 If an unhandled error happens in running these functions,
5653 the variable's value remains nil. That prevents the error
5654 from happening repeatedly and making Emacs nonfunctional. */);
5655 Vafter_change_functions = Qnil;
5657 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
5658 doc: /* A list of functions to call before changing a buffer which is unmodified.
5659 The functions are run using the `run-hooks' function. */);
5660 Vfirst_change_hook = Qnil;
5662 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5663 doc: /* List of undo entries in current buffer.
5664 Recent changes come first; older changes follow newer.
5666 An entry (BEG . END) represents an insertion which begins at
5667 position BEG and ends at position END.
5669 An entry (TEXT . POSITION) represents the deletion of the string TEXT
5670 from (abs POSITION). If POSITION is positive, point was at the front
5671 of the text being deleted; if negative, point was at the end.
5673 An entry (t HIGH . LOW) indicates that the buffer previously had
5674 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5675 of the visited file's modification time, as of that time. If the
5676 modification time of the most recent save is different, this entry is
5677 obsolete.
5679 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5680 was modified between BEG and END. PROPERTY is the property name,
5681 and VALUE is the old value.
5683 An entry (MARKER . DISTANCE) indicates that the marker MARKER
5684 was adjusted in position by the offset DISTANCE (an integer).
5686 An entry of the form POSITION indicates that point was at the buffer
5687 location given by the integer. Undoing an entry of this form places
5688 point at POSITION.
5690 nil marks undo boundaries. The undo command treats the changes
5691 between two undo boundaries as a single step to be undone.
5693 If the value of the variable is t, undo information is not recorded. */);
5695 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5696 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
5698 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5699 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
5701 Normally, the line-motion functions work by scanning the buffer for
5702 newlines. Columnar operations (like move-to-column and
5703 compute-motion) also work by scanning the buffer, summing character
5704 widths as they go. This works well for ordinary text, but if the
5705 buffer's lines are very long (say, more than 500 characters), these
5706 motion functions will take longer to execute. Emacs may also take
5707 longer to update the display.
5709 If cache-long-line-scans is non-nil, these motion functions cache the
5710 results of their scans, and consult the cache to avoid rescanning
5711 regions of the buffer until the text is modified. The caches are most
5712 beneficial when they prevent the most searching---that is, when the
5713 buffer contains long lines and large regions of characters with the
5714 same, fixed screen width.
5716 When cache-long-line-scans is non-nil, processing short lines will
5717 become slightly slower (because of the overhead of consulting the
5718 cache), and the caches will use memory roughly proportional to the
5719 number of newlines and characters whose screen width varies.
5721 The caches require no explicit maintenance; their accuracy is
5722 maintained internally by the Emacs primitives. Enabling or disabling
5723 the cache should not affect the behavior of any of the motion
5724 functions; it should only affect their performance. */);
5726 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5727 doc: /* Value of point before the last series of scroll operations, or nil. */);
5729 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5730 doc: /* List of formats to use when saving this buffer.
5731 Formats are defined by `format-alist'. This variable is
5732 set when a file is visited. */);
5734 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
5735 &current_buffer->invisibility_spec, Qnil,
5736 doc: /* Invisibility spec of this buffer.
5737 The default is t, which means that text is invisible
5738 if it has a non-nil `invisible' property.
5739 If the value is a list, a text character is invisible if its `invisible'
5740 property is an element in that list.
5741 If an element is a cons cell of the form (PROP . ELLIPSIS),
5742 then characters with property value PROP are invisible,
5743 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
5745 DEFVAR_PER_BUFFER ("buffer-display-count",
5746 &current_buffer->display_count, Qnil,
5747 doc: /* A number incremented each time this buffer is displayed in a window.
5748 The function `set-window-buffer' increments it. */);
5750 DEFVAR_PER_BUFFER ("buffer-display-time",
5751 &current_buffer->display_time, Qnil,
5752 doc: /* Time stamp updated each time this buffer is displayed in a window.
5753 The function `set-window-buffer' updates this variable
5754 to the value obtained by calling `current-time'.
5755 If the buffer has never been shown in a window, the value is nil. */);
5757 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
5758 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
5759 Non-nil also enables highlighting of the region whenever the mark is active.
5760 The variable `highlight-nonselected-windows' controls whether to highlight
5761 all windows or just the selected window. */);
5762 Vtransient_mark_mode = Qnil;
5764 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
5765 doc: /* *Non-nil means disregard read-only status of buffers or characters.
5766 If the value is t, disregard `buffer-read-only' and all `read-only'
5767 text properties. If the value is a list, disregard `buffer-read-only'
5768 and disregard a `read-only' text property if the property value
5769 is a member of the list. */);
5770 Vinhibit_read_only = Qnil;
5772 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
5773 doc: /* Cursor to use when this buffer is in the selected window.
5774 Values are interpreted as follows:
5776 t use the cursor specified for the frame
5777 nil don't display a cursor
5778 bar display a bar cursor with default width
5779 (bar . WIDTH) display a bar cursor with width WIDTH
5780 ANYTHING ELSE display a box cursor.
5782 When the buffer is displayed in a nonselected window,
5783 this variable has no effect; the cursor appears as a hollow box. */);
5785 DEFVAR_PER_BUFFER ("line-spacing",
5786 &current_buffer->extra_line_spacing, Qnil,
5787 doc: /* Additional space to put between lines when displaying a buffer.
5788 The space is measured in pixels, and put below lines on window systems. */);
5790 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
5791 doc: /* List of functions called with no args to query before killing a buffer. */);
5792 Vkill_buffer_query_functions = Qnil;
5794 defsubr (&Sbuffer_live_p);
5795 defsubr (&Sbuffer_list);
5796 defsubr (&Sget_buffer);
5797 defsubr (&Sget_file_buffer);
5798 defsubr (&Sget_buffer_create);
5799 defsubr (&Smake_indirect_buffer);
5800 defsubr (&Sgenerate_new_buffer_name);
5801 defsubr (&Sbuffer_name);
5802 /*defsubr (&Sbuffer_number);*/
5803 defsubr (&Sbuffer_file_name);
5804 defsubr (&Sbuffer_base_buffer);
5805 defsubr (&Sbuffer_local_value);
5806 defsubr (&Sbuffer_local_variables);
5807 defsubr (&Sbuffer_modified_p);
5808 defsubr (&Sset_buffer_modified_p);
5809 defsubr (&Sbuffer_modified_tick);
5810 defsubr (&Srename_buffer);
5811 defsubr (&Sother_buffer);
5812 defsubr (&Sbuffer_disable_undo);
5813 defsubr (&Sbuffer_enable_undo);
5814 defsubr (&Skill_buffer);
5815 defsubr (&Sset_buffer_major_mode);
5816 defsubr (&Sswitch_to_buffer);
5817 defsubr (&Spop_to_buffer);
5818 defsubr (&Scurrent_buffer);
5819 defsubr (&Sset_buffer);
5820 defsubr (&Sbarf_if_buffer_read_only);
5821 defsubr (&Sbury_buffer);
5822 defsubr (&Serase_buffer);
5823 defsubr (&Sset_buffer_multibyte);
5824 defsubr (&Skill_all_local_variables);
5826 defsubr (&Soverlayp);
5827 defsubr (&Smake_overlay);
5828 defsubr (&Sdelete_overlay);
5829 defsubr (&Smove_overlay);
5830 defsubr (&Soverlay_start);
5831 defsubr (&Soverlay_end);
5832 defsubr (&Soverlay_buffer);
5833 defsubr (&Soverlay_properties);
5834 defsubr (&Soverlays_at);
5835 defsubr (&Soverlays_in);
5836 defsubr (&Snext_overlay_change);
5837 defsubr (&Sprevious_overlay_change);
5838 defsubr (&Soverlay_recenter);
5839 defsubr (&Soverlay_lists);
5840 defsubr (&Soverlay_get);
5841 defsubr (&Soverlay_put);
5842 defsubr (&Srestore_buffer_modified_p);
5845 void
5846 keys_of_buffer ()
5848 initial_define_key (control_x_map, 'b', "switch-to-buffer");
5849 initial_define_key (control_x_map, 'k', "kill-buffer");
5851 /* This must not be in syms_of_buffer, because Qdisabled is not
5852 initialized when that function gets called. */
5853 Fput (intern ("erase-buffer"), Qdisabled, Qt);