1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999
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)
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. */
24 #include <sys/types.h>
26 #include <sys/param.h>
32 /* in 4.1, param.h fails to define this. */
33 #define MAXPATHLEN 1024
34 #endif /* not MAXPATHLEN */
40 #include "intervals.h"
45 #include "region-cache.h"
47 #include "blockinput.h"
50 struct buffer
*current_buffer
; /* the current buffer */
52 /* First buffer in chain of all buffers (in reverse order of creation).
53 Threaded through ->next. */
55 struct buffer
*all_buffers
;
57 /* This structure holds the default values of the buffer-local variables
58 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
59 The default value occupies the same slot in this structure
60 as an individual buffer's value occupies in that buffer.
61 Setting the default value also goes through the alist of buffers
62 and stores into each buffer that does not say it has a local value. */
64 struct buffer buffer_defaults
;
66 /* A Lisp_Object pointer to the above, used for staticpro */
68 static Lisp_Object Vbuffer_defaults
;
70 /* This structure marks which slots in a buffer have corresponding
71 default values in buffer_defaults.
72 Each such slot has a nonzero value in this structure.
73 The value has only one nonzero bit.
75 When a buffer has its own local value for a slot,
76 the entry for that slot (found in the same slot in this structure)
77 is turned on in the buffer's local_flags array.
79 If a slot in this structure is -1, then even though there may
80 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
81 and the corresponding slot in buffer_defaults is not used.
83 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
84 but there is a default value which is copied into each buffer.
86 If a slot in this structure is negative, then even though there may
87 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
88 and the corresponding slot in buffer_defaults is not used.
90 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91 zero, that is a bug */
93 struct buffer buffer_local_flags
;
95 /* This structure holds the names of symbols whose values may be
96 buffer-local. It is indexed and accessed in the same way as the above. */
98 struct buffer buffer_local_symbols
;
99 /* A Lisp_Object pointer to the above, used for staticpro */
100 static Lisp_Object Vbuffer_local_symbols
;
102 /* This structure holds the required types for the values in the
103 buffer-local slots. If a slot contains Qnil, then the
104 corresponding buffer slot may contain a value of any type. If a
105 slot contains an integer, then prospective values' tags must be
106 equal to that integer (except nil is always allowed).
107 When a tag does not match, the function
108 buffer_slot_type_mismatch will signal an error.
110 If a slot here contains -1, the corresponding variable is read-only. */
111 struct buffer buffer_local_types
;
113 /* Flags indicating which built-in buffer-local variables
114 are permanent locals. */
115 static char buffer_permanent_local_flags
[MAX_PER_BUFFER_VARS
];
117 /* Number of per-buffer variables used. */
119 int last_per_buffer_idx
;
121 Lisp_Object
Fset_buffer ();
122 void set_buffer_internal ();
123 void set_buffer_internal_1 ();
124 static void call_overlay_mod_hooks ();
125 static void swap_out_buffer_local_variables ();
126 static void reset_buffer_local_variables ();
128 /* Alist of all buffer names vs the buffers. */
129 /* This used to be a variable, but is no longer,
130 to prevent lossage due to user rplac'ing this alist or its elements. */
131 Lisp_Object Vbuffer_alist
;
133 /* Functions to call before and after each text change. */
134 Lisp_Object Vbefore_change_function
;
135 Lisp_Object Vafter_change_function
;
136 Lisp_Object Vbefore_change_functions
;
137 Lisp_Object Vafter_change_functions
;
139 Lisp_Object Vtransient_mark_mode
;
141 /* t means ignore all read-only text properties.
142 A list means ignore such a property if its value is a member of the list.
143 Any non-nil value means ignore buffer-read-only. */
144 Lisp_Object Vinhibit_read_only
;
146 /* List of functions to call that can query about killing a buffer.
147 If any of these functions returns nil, we don't kill it. */
148 Lisp_Object Vkill_buffer_query_functions
;
150 /* List of functions to call before changing an unmodified buffer. */
151 Lisp_Object Vfirst_change_hook
;
153 Lisp_Object Qfirst_change_hook
;
154 Lisp_Object Qbefore_change_functions
;
155 Lisp_Object Qafter_change_functions
;
157 /* If nonzero, all modification hooks are suppressed. */
158 int inhibit_modification_hooks
;
160 Lisp_Object Qfundamental_mode
, Qmode_class
, Qpermanent_local
;
162 Lisp_Object Qprotected_field
;
164 Lisp_Object QSFundamental
; /* A string "Fundamental" */
166 Lisp_Object Qkill_buffer_hook
;
168 Lisp_Object Qget_file_buffer
;
170 Lisp_Object Qoverlayp
;
172 Lisp_Object Qpriority
, Qwindow
, Qevaporate
, Qbefore_string
, Qafter_string
;
174 Lisp_Object Qmodification_hooks
;
175 Lisp_Object Qinsert_in_front_hooks
;
176 Lisp_Object Qinsert_behind_hooks
;
178 /* For debugging; temporary. See set_buffer_internal. */
179 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
186 error ("No buffer named %s", XSTRING (spec
)->data
);
187 error ("Invalid buffer argument");
190 DEFUN ("buffer-live-p", Fbuffer_live_p
, Sbuffer_live_p
, 1, 1, 0,
191 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
192 Value is nil if OBJECT is not a buffer or if it has been killed.")
196 return ((BUFFERP (object
) && ! NILP (XBUFFER (object
)->name
))
200 DEFUN ("buffer-list", Fbuffer_list
, Sbuffer_list
, 0, 1, 0,
201 "Return a list of all existing live buffers.\n\
202 If the optional arg FRAME is a frame, we return that frame's buffer list.")
206 Lisp_Object framelist
, general
;
207 general
= Fmapcar (Qcdr
, Vbuffer_alist
);
213 CHECK_FRAME (frame
, 1);
215 framelist
= Fcopy_sequence (XFRAME (frame
)->buffer_list
);
217 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
219 while (! NILP (tail
))
221 general
= Fdelq (XCAR (tail
), general
);
224 return nconc2 (framelist
, general
);
230 /* Like Fassoc, but use Fstring_equal to compare
231 (which ignores text properties),
232 and don't ever QUIT. */
235 assoc_ignore_text_properties (key
, list
)
236 register Lisp_Object key
;
239 register Lisp_Object tail
;
240 for (tail
= list
; !NILP (tail
); tail
= Fcdr (tail
))
242 register Lisp_Object elt
, tem
;
244 tem
= Fstring_equal (Fcar (elt
), key
);
251 DEFUN ("get-buffer", Fget_buffer
, Sget_buffer
, 1, 1, 0,
252 "Return the buffer named NAME (a string).\n\
253 If there is no live buffer named NAME, return nil.\n\
254 NAME may also be a buffer; if so, the value is that buffer.")
256 register Lisp_Object name
;
260 CHECK_STRING (name
, 0);
262 return Fcdr (assoc_ignore_text_properties (name
, Vbuffer_alist
));
265 DEFUN ("get-file-buffer", Fget_file_buffer
, Sget_file_buffer
, 1, 1, 0,
266 "Return the buffer visiting file FILENAME (a string).\n\
267 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
268 If there is no such live buffer, return nil.\n\
269 See also `find-buffer-visiting'.")
271 register Lisp_Object filename
;
273 register Lisp_Object tail
, buf
, tem
;
276 CHECK_STRING (filename
, 0);
277 filename
= Fexpand_file_name (filename
, Qnil
);
279 /* If the file name has special constructs in it,
280 call the corresponding file handler. */
281 handler
= Ffind_file_name_handler (filename
, Qget_file_buffer
);
283 return call2 (handler
, Qget_file_buffer
, filename
);
285 for (tail
= Vbuffer_alist
; CONSP (tail
); tail
= XCDR (tail
))
287 buf
= Fcdr (XCAR (tail
));
288 if (!BUFFERP (buf
)) continue;
289 if (!STRINGP (XBUFFER (buf
)->filename
)) continue;
290 tem
= Fstring_equal (XBUFFER (buf
)->filename
, filename
);
298 get_truename_buffer (filename
)
299 register Lisp_Object filename
;
301 register Lisp_Object tail
, buf
, tem
;
303 for (tail
= Vbuffer_alist
; CONSP (tail
); tail
= XCDR (tail
))
305 buf
= Fcdr (XCAR (tail
));
306 if (!BUFFERP (buf
)) continue;
307 if (!STRINGP (XBUFFER (buf
)->file_truename
)) continue;
308 tem
= Fstring_equal (XBUFFER (buf
)->file_truename
, filename
);
315 /* Incremented for each buffer created, to assign the buffer number. */
318 DEFUN ("get-buffer-create", Fget_buffer_create
, Sget_buffer_create
, 1, 1, 0,
319 "Return the buffer named NAME, or create such a buffer and return it.\n\
320 A new buffer is created if there is no live buffer named NAME.\n\
321 If NAME starts with a space, the new buffer does not keep undo information.\n\
322 If NAME is a buffer instead of a string, then it is the value returned.\n\
323 The value is never nil.")
325 register Lisp_Object name
;
327 register Lisp_Object buf
;
328 register struct buffer
*b
;
330 buf
= Fget_buffer (name
);
334 if (XSTRING (name
)->size
== 0)
335 error ("Empty string for buffer name is not allowed");
337 b
= (struct buffer
*) allocate_buffer ();
339 b
->size
= sizeof (struct buffer
) / sizeof (EMACS_INT
);
341 /* An ordinary buffer uses its own struct buffer_text. */
342 b
->text
= &b
->own_text
;
345 BUF_GAP_SIZE (b
) = 20;
347 /* We allocate extra 1-byte at the tail and keep it always '\0' for
348 anchoring a search. */
349 BUFFER_ALLOC (BUF_BEG_ADDR (b
), (BUF_GAP_SIZE (b
) + 1));
351 if (! BUF_BEG_ADDR (b
))
352 buffer_memory_full ();
360 BUF_GPT_BYTE (b
) = 1;
361 BUF_BEGV_BYTE (b
) = 1;
365 BUF_OVERLAY_MODIFF (b
) = 1;
366 BUF_SAVE_MODIFF (b
) = 1;
367 BUF_INTERVALS (b
) = 0;
368 BUF_UNCHANGED_MODIFIED (b
) = 1;
369 BUF_OVERLAY_UNCHANGED_MODIFIED (b
) = 1;
370 BUF_END_UNCHANGED (b
) = 0;
371 BUF_BEG_UNCHANGED (b
) = 0;
372 *(BUF_GPT_ADDR (b
)) = *(BUF_Z_ADDR (b
)) = 0; /* Put an anchor '\0'. */
374 b
->newline_cache
= 0;
375 b
->width_run_cache
= 0;
376 b
->width_table
= Qnil
;
377 b
->prevent_redisplay_optimizations_p
= 1;
379 /* Put this on the chain of all buffers including killed ones. */
380 b
->next
= all_buffers
;
383 /* An ordinary buffer normally doesn't need markers
384 to handle BEGV and ZV. */
386 b
->begv_marker
= Qnil
;
389 name
= Fcopy_sequence (name
);
390 INITIALIZE_INTERVAL (XSTRING (name
), NULL_INTERVAL
);
393 if (XSTRING (name
)->data
[0] != ' ')
399 reset_buffer_local_variables (b
, 1);
401 /* Put this in the alist of all live buffers. */
403 Vbuffer_alist
= nconc2 (Vbuffer_alist
, Fcons (Fcons (name
, buf
), Qnil
));
405 b
->mark
= Fmake_marker ();
406 BUF_MARKERS (b
) = Qnil
;
411 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer
, Smake_indirect_buffer
, 2, 2,
412 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
413 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
414 BASE-BUFFER should be an existing buffer (or buffer name).\n\
415 NAME should be a string which is not the name of an existing buffer.")
417 register Lisp_Object base_buffer
, name
;
419 register Lisp_Object buf
;
420 register struct buffer
*b
;
422 buf
= Fget_buffer (name
);
424 error ("Buffer name `%s' is in use", XSTRING (name
)->data
);
426 base_buffer
= Fget_buffer (base_buffer
);
427 if (NILP (base_buffer
))
428 error ("No such buffer: `%s'",
429 XSTRING (XBUFFER (base_buffer
)->name
)->data
);
431 if (XSTRING (name
)->size
== 0)
432 error ("Empty string for buffer name is not allowed");
434 b
= (struct buffer
*) allocate_buffer ();
435 b
->size
= sizeof (struct buffer
) / sizeof (EMACS_INT
);
437 if (XBUFFER (base_buffer
)->base_buffer
)
438 b
->base_buffer
= XBUFFER (base_buffer
)->base_buffer
;
440 b
->base_buffer
= XBUFFER (base_buffer
);
442 /* Use the base buffer's text object. */
443 b
->text
= b
->base_buffer
->text
;
445 BUF_BEGV (b
) = BUF_BEGV (b
->base_buffer
);
446 BUF_ZV (b
) = BUF_ZV (b
->base_buffer
);
447 BUF_PT (b
) = BUF_PT (b
->base_buffer
);
448 BUF_BEGV_BYTE (b
) = BUF_BEGV_BYTE (b
->base_buffer
);
449 BUF_ZV_BYTE (b
) = BUF_ZV_BYTE (b
->base_buffer
);
450 BUF_PT_BYTE (b
) = BUF_PT_BYTE (b
->base_buffer
);
452 b
->newline_cache
= 0;
453 b
->width_run_cache
= 0;
454 b
->width_table
= Qnil
;
456 /* Put this on the chain of all buffers including killed ones. */
457 b
->next
= all_buffers
;
460 name
= Fcopy_sequence (name
);
461 INITIALIZE_INTERVAL (XSTRING (name
), NULL_INTERVAL
);
465 reset_buffer_local_variables (b
, 1);
467 /* Put this in the alist of all live buffers. */
469 Vbuffer_alist
= nconc2 (Vbuffer_alist
, Fcons (Fcons (name
, buf
), Qnil
));
471 b
->mark
= Fmake_marker ();
474 /* The multibyte status belongs to the base buffer. */
475 b
->enable_multibyte_characters
= b
->base_buffer
->enable_multibyte_characters
;
477 /* Make sure the base buffer has markers for its narrowing. */
478 if (NILP (b
->base_buffer
->pt_marker
))
480 b
->base_buffer
->pt_marker
= Fmake_marker ();
481 set_marker_both (b
->base_buffer
->pt_marker
, base_buffer
,
482 BUF_PT (b
->base_buffer
),
483 BUF_PT_BYTE (b
->base_buffer
));
485 if (NILP (b
->base_buffer
->begv_marker
))
487 b
->base_buffer
->begv_marker
= Fmake_marker ();
488 set_marker_both (b
->base_buffer
->begv_marker
, base_buffer
,
489 BUF_BEGV (b
->base_buffer
),
490 BUF_BEGV_BYTE (b
->base_buffer
));
492 if (NILP (b
->base_buffer
->zv_marker
))
494 b
->base_buffer
->zv_marker
= Fmake_marker ();
495 set_marker_both (b
->base_buffer
->zv_marker
, base_buffer
,
496 BUF_ZV (b
->base_buffer
),
497 BUF_ZV_BYTE (b
->base_buffer
));
498 XMARKER (b
->base_buffer
->zv_marker
)->insertion_type
= 1;
501 /* Give the indirect buffer markers for its narrowing. */
502 b
->pt_marker
= Fmake_marker ();
503 set_marker_both (b
->pt_marker
, buf
, BUF_PT (b
), BUF_PT_BYTE (b
));
504 b
->begv_marker
= Fmake_marker ();
505 set_marker_both (b
->begv_marker
, buf
, BUF_BEGV (b
), BUF_BEGV_BYTE (b
));
506 b
->zv_marker
= Fmake_marker ();
507 set_marker_both (b
->zv_marker
, buf
, BUF_ZV (b
), BUF_ZV_BYTE (b
));
508 XMARKER (b
->zv_marker
)->insertion_type
= 1;
513 /* Reinitialize everything about a buffer except its name and contents
514 and local variables. */
518 register struct buffer
*b
;
521 b
->file_truename
= Qnil
;
522 b
->directory
= (current_buffer
) ? current_buffer
->directory
: Qnil
;
524 XSETFASTINT (b
->save_length
, 0);
525 b
->last_window_start
= 1;
526 /* It is more conservative to start out "changed" than "unchanged". */
528 b
->prevent_redisplay_optimizations_p
= 1;
530 b
->auto_save_modified
= 0;
531 b
->auto_save_failure_time
= -1;
532 b
->auto_save_file_name
= Qnil
;
534 b
->overlays_before
= Qnil
;
535 b
->overlays_after
= Qnil
;
536 XSETFASTINT (b
->overlay_center
, 1);
537 b
->mark_active
= Qnil
;
538 b
->point_before_scroll
= Qnil
;
539 b
->file_format
= Qnil
;
540 b
->last_selected_window
= Qnil
;
541 XSETINT (b
->display_count
, 0);
542 b
->display_time
= Qnil
;
545 b
->enable_multibyte_characters
= buffer_defaults
.enable_multibyte_characters
;
546 b
->cursor_type
= buffer_defaults
.cursor_type
;
549 /* Reset buffer B's local variables info.
550 Don't use this on a buffer that has already been in use;
551 it does not treat permanent locals consistently.
552 Instead, use Fkill_all_local_variables.
554 If PERMANENT_TOO is 1, then we reset permanent built-in
555 buffer-local variables. If PERMANENT_TOO is 0,
556 we preserve those. */
559 reset_buffer_local_variables (b
, permanent_too
)
560 register struct buffer
*b
;
566 /* Reset the major mode to Fundamental, together with all the
567 things that depend on the major mode.
568 default-major-mode is handled at a higher level.
569 We ignore it here. */
570 b
->major_mode
= Qfundamental_mode
;
572 b
->abbrev_table
= Vfundamental_mode_abbrev_table
;
573 b
->mode_name
= QSFundamental
;
574 b
->minor_modes
= Qnil
;
576 /* If the standard case table has been altered and invalidated,
577 fix up its insides first. */
578 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table
)->extras
[0])
579 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table
)->extras
[1])
580 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table
)->extras
[2])))
581 Fset_standard_case_table (Vascii_downcase_table
);
583 b
->downcase_table
= Vascii_downcase_table
;
584 b
->upcase_table
= XCHAR_TABLE (Vascii_downcase_table
)->extras
[0];
585 b
->case_canon_table
= XCHAR_TABLE (Vascii_downcase_table
)->extras
[1];
586 b
->case_eqv_table
= XCHAR_TABLE (Vascii_downcase_table
)->extras
[2];
587 b
->invisibility_spec
= Qt
;
589 b
->buffer_file_type
= Qnil
;
593 b
->sort_table
= XSTRING (Vascii_sort_table
);
594 b
->folding_sort_table
= XSTRING (Vascii_folding_sort_table
);
597 /* Reset all (or most) per-buffer variables to their defaults. */
598 b
->local_var_alist
= Qnil
;
599 for (i
= 0; i
< last_per_buffer_idx
; ++i
)
600 if (permanent_too
|| buffer_permanent_local_flags
[i
] == 0)
601 SET_PER_BUFFER_VALUE_P (b
, i
, 0);
603 /* For each slot that has a default value,
604 copy that into the slot. */
606 for (offset
= PER_BUFFER_VAR_OFFSET (name
);
608 offset
+= sizeof (Lisp_Object
))
610 int idx
= PER_BUFFER_IDX (offset
);
613 || buffer_permanent_local_flags
[idx
] == 0))
614 /* Is -2 used anywhere? */
616 PER_BUFFER_VALUE (b
, offset
) = PER_BUFFER_DEFAULT (offset
);
620 /* We split this away from generate-new-buffer, because rename-buffer
621 and set-visited-file-name ought to be able to use this to really
622 rename the buffer properly. */
624 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name
, Sgenerate_new_buffer_name
,
626 "Return a string that is the name of no existing buffer based on NAME.\n\
627 If there is no live buffer named NAME, then return NAME.\n\
628 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
629 until an unused name is found, and then return that name.\n\
630 Optional second argument IGNORE specifies a name that is okay to use\n\
631 \(if it is in the sequence to be tried)\n\
632 even if a buffer with that name exists.")
634 register Lisp_Object name
, ignore
;
636 register Lisp_Object gentemp
, tem
;
640 CHECK_STRING (name
, 0);
642 tem
= Fget_buffer (name
);
649 sprintf (number
, "<%d>", ++count
);
650 gentemp
= concat2 (name
, build_string (number
));
651 tem
= Fstring_equal (gentemp
, ignore
);
654 tem
= Fget_buffer (gentemp
);
661 DEFUN ("buffer-name", Fbuffer_name
, Sbuffer_name
, 0, 1, 0,
662 "Return the name of BUFFER, as a string.\n\
663 With no argument or nil as argument, return the name of the current buffer.")
665 register Lisp_Object buffer
;
668 return current_buffer
->name
;
669 CHECK_BUFFER (buffer
, 0);
670 return XBUFFER (buffer
)->name
;
673 DEFUN ("buffer-file-name", Fbuffer_file_name
, Sbuffer_file_name
, 0, 1, 0,
674 "Return name of file BUFFER is visiting, or nil if none.\n\
675 No argument or nil as argument means use the current buffer.")
677 register Lisp_Object buffer
;
680 return current_buffer
->filename
;
681 CHECK_BUFFER (buffer
, 0);
682 return XBUFFER (buffer
)->filename
;
685 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer
, Sbuffer_base_buffer
,
687 "Return the base buffer of indirect buffer BUFFER.\n\
688 If BUFFER is not indirect, return nil.")
690 register Lisp_Object buffer
;
693 Lisp_Object base_buffer
;
696 base
= current_buffer
->base_buffer
;
699 CHECK_BUFFER (buffer
, 0);
700 base
= XBUFFER (buffer
)->base_buffer
;
705 XSETBUFFER (base_buffer
, base
);
709 DEFUN ("buffer-local-variables", Fbuffer_local_variables
,
710 Sbuffer_local_variables
, 0, 1, 0,
711 "Return an alist of variables that are buffer-local in BUFFER.\n\
712 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
713 For a symbol that is locally unbound, just the symbol appears in the value.\n\
714 Note that storing new VALUEs in these elements doesn't change the variables.\n\
715 No argument or nil as argument means use current buffer as BUFFER.")
717 register Lisp_Object buffer
;
719 register struct buffer
*buf
;
720 register Lisp_Object result
;
723 buf
= current_buffer
;
726 CHECK_BUFFER (buffer
, 0);
727 buf
= XBUFFER (buffer
);
733 register Lisp_Object tail
;
734 for (tail
= buf
->local_var_alist
; CONSP (tail
); tail
= XCDR (tail
))
736 Lisp_Object val
, elt
;
740 /* Reference each variable in the alist in buf.
741 If inquiring about the current buffer, this gets the current values,
742 so store them into the alist so the alist is up to date.
743 If inquiring about some other buffer, this swaps out any values
744 for that buffer, making the alist up to date automatically. */
745 val
= find_symbol_value (XCAR (elt
));
746 /* Use the current buffer value only if buf is the current buffer. */
747 if (buf
!= current_buffer
)
750 /* If symbol is unbound, put just the symbol in the list. */
751 if (EQ (val
, Qunbound
))
752 result
= Fcons (XCAR (elt
), result
);
753 /* Otherwise, put (symbol . value) in the list. */
755 result
= Fcons (Fcons (XCAR (elt
), val
), result
);
759 /* Add on all the variables stored in special slots. */
763 for (offset
= PER_BUFFER_VAR_OFFSET (name
);
764 offset
< sizeof (struct buffer
);
765 /* sizeof EMACS_INT == sizeof Lisp_Object */
766 offset
+= (sizeof (EMACS_INT
)))
768 idx
= PER_BUFFER_IDX (offset
);
769 if ((idx
== -1 || PER_BUFFER_VALUE_P (buf
, idx
))
770 && SYMBOLP (PER_BUFFER_SYMBOL (offset
)))
771 result
= Fcons (Fcons (PER_BUFFER_SYMBOL (offset
),
772 PER_BUFFER_VALUE (buf
, offset
)),
781 DEFUN ("buffer-modified-p", Fbuffer_modified_p
, Sbuffer_modified_p
,
783 "Return t if BUFFER was modified since its file was last read or saved.\n\
784 No argument or nil as argument means use current buffer as BUFFER.")
786 register Lisp_Object buffer
;
788 register struct buffer
*buf
;
790 buf
= current_buffer
;
793 CHECK_BUFFER (buffer
, 0);
794 buf
= XBUFFER (buffer
);
797 return BUF_SAVE_MODIFF (buf
) < BUF_MODIFF (buf
) ? Qt
: Qnil
;
800 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p
, Sset_buffer_modified_p
,
802 "Mark current buffer as modified or unmodified according to FLAG.\n\
803 A non-nil FLAG means mark the buffer modified.")
805 register Lisp_Object flag
;
807 register int already
;
808 register Lisp_Object fn
;
809 Lisp_Object buffer
, window
;
811 #ifdef CLASH_DETECTION
812 /* If buffer becoming modified, lock the file.
813 If buffer becoming unmodified, unlock the file. */
815 fn
= current_buffer
->file_truename
;
816 /* Test buffer-file-name so that binding it to nil is effective. */
817 if (!NILP (fn
) && ! NILP (current_buffer
->filename
))
819 already
= SAVE_MODIFF
< MODIFF
;
820 if (!already
&& !NILP (flag
))
822 else if (already
&& NILP (flag
))
825 #endif /* CLASH_DETECTION */
827 SAVE_MODIFF
= NILP (flag
) ? MODIFF
: 0;
829 /* Set update_mode_lines only if buffer is displayed in some window.
830 Packages like jit-lock or lazy-lock preserve a buffer's modified
831 state by recording/restoring the state around blocks of code.
832 Setting update_mode_lines makes redisplay consider all windows
833 (on all frames). Stealth fontification of buffers not displayed
834 would incur additional redisplay costs if we'd set
835 update_modes_lines unconditionally.
837 Ideally, I think there should be another mechanism for fontifying
838 buffers without "modifying" buffers, or redisplay should be
839 smarter about updating the `*' in mode lines. --gerd */
840 XSETBUFFER (buffer
, current_buffer
);
841 window
= Fget_buffer_window (buffer
, Qt
);
842 if (WINDOWP (window
))
848 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p
,
849 Srestore_buffer_modified_p
, 1, 1, 0,
850 "Like `set-buffer-modified-p', with a differences concerning redisplay.\n\
851 It is not ensured that mode lines will be updated to show the modified\n\
852 state of the current buffer. Use with care.")
856 #ifdef CLASH_DETECTION
859 /* If buffer becoming modified, lock the file.
860 If buffer becoming unmodified, unlock the file. */
862 fn
= current_buffer
->file_truename
;
863 /* Test buffer-file-name so that binding it to nil is effective. */
864 if (!NILP (fn
) && ! NILP (current_buffer
->filename
))
866 int already
= SAVE_MODIFF
< MODIFF
;
867 if (!already
&& !NILP (flag
))
869 else if (already
&& NILP (flag
))
872 #endif /* CLASH_DETECTION */
874 SAVE_MODIFF
= NILP (flag
) ? MODIFF
: 0;
878 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick
, Sbuffer_modified_tick
,
880 "Return BUFFER's tick counter, incremented for each change in text.\n\
881 Each buffer has a tick counter which is incremented each time the text in\n\
882 that buffer is changed. It wraps around occasionally.\n\
883 No argument or nil as argument means use current buffer as BUFFER.")
885 register Lisp_Object buffer
;
887 register struct buffer
*buf
;
889 buf
= current_buffer
;
892 CHECK_BUFFER (buffer
, 0);
893 buf
= XBUFFER (buffer
);
896 return make_number (BUF_MODIFF (buf
));
899 DEFUN ("rename-buffer", Frename_buffer
, Srename_buffer
, 1, 2,
900 "sRename buffer (to new name): \nP",
901 "Change current buffer's name to NEWNAME (a string).\n\
902 If second arg UNIQUE is nil or omitted, it is an error if a\n\
903 buffer named NEWNAME already exists.\n\
904 If UNIQUE is non-nil, come up with a new name using\n\
905 `generate-new-buffer-name'.\n\
906 Interactively, you can set UNIQUE with a prefix argument.\n\
907 We return the name we actually gave the buffer.\n\
908 This does not change the name of the visited file (if any).")
910 register Lisp_Object newname
, unique
;
912 register Lisp_Object tem
, buf
;
914 CHECK_STRING (newname
, 0);
916 if (XSTRING (newname
)->size
== 0)
917 error ("Empty string is invalid as a buffer name");
919 tem
= Fget_buffer (newname
);
922 /* Don't short-circuit if UNIQUE is t. That is a useful way to
923 rename the buffer automatically so you can create another
924 with the original name. It makes UNIQUE equivalent to
925 (rename-buffer (generate-new-buffer-name NEWNAME)). */
926 if (NILP (unique
) && XBUFFER (tem
) == current_buffer
)
927 return current_buffer
->name
;
929 newname
= Fgenerate_new_buffer_name (newname
, current_buffer
->name
);
931 error ("Buffer name `%s' is in use", XSTRING (newname
)->data
);
934 current_buffer
->name
= newname
;
936 /* Catch redisplay's attention. Unless we do this, the mode lines for
937 any windows displaying current_buffer will stay unchanged. */
940 XSETBUFFER (buf
, current_buffer
);
941 Fsetcar (Frassq (buf
, Vbuffer_alist
), newname
);
942 if (NILP (current_buffer
->filename
)
943 && !NILP (current_buffer
->auto_save_file_name
))
944 call0 (intern ("rename-auto-save-file"));
945 /* Refetch since that last call may have done GC. */
946 return current_buffer
->name
;
949 DEFUN ("other-buffer", Fother_buffer
, Sother_buffer
, 0, 3, 0,
950 "Return most recently selected buffer other than BUFFER.\n\
951 Buffers not visible in windows are preferred to visible buffers,\n\
952 unless optional second argument VISIBLE-OK is non-nil.\n\
953 If the optional third argument FRAME is non-nil, use that frame's\n\
954 buffer list instead of the selected frame's buffer list.\n\
955 If no other buffer exists, the buffer `*scratch*' is returned.\n\
956 If BUFFER is omitted or nil, some interesting buffer is returned.")
957 (buffer
, visible_ok
, frame
)
958 register Lisp_Object buffer
, visible_ok
, frame
;
960 Lisp_Object
Fset_buffer_major_mode ();
961 register Lisp_Object tail
, buf
, notsogood
, tem
, pred
, add_ons
;
965 frame
= selected_frame
;
967 tail
= Vbuffer_alist
;
968 pred
= frame_buffer_predicate (frame
);
970 /* Consider buffers that have been seen in the selected frame
971 before other buffers. */
973 tem
= frame_buffer_list (frame
);
977 if (BUFFERP (XCAR (tem
)))
978 add_ons
= Fcons (Fcons (Qnil
, XCAR (tem
)), add_ons
);
981 tail
= nconc2 (Fnreverse (add_ons
), tail
);
983 for (; !NILP (tail
); tail
= Fcdr (tail
))
985 buf
= Fcdr (Fcar (tail
));
986 if (EQ (buf
, buffer
))
988 if (XSTRING (XBUFFER (buf
)->name
)->data
[0] == ' ')
990 /* If the selected frame has a buffer_predicate,
991 disregard buffers that don't fit the predicate. */
994 tem
= call1 (pred
, buf
);
999 if (NILP (visible_ok
))
1000 tem
= Fget_buffer_window (buf
, Qt
);
1005 if (NILP (notsogood
))
1008 if (!NILP (notsogood
))
1010 buf
= Fget_buffer (build_string ("*scratch*"));
1013 buf
= Fget_buffer_create (build_string ("*scratch*"));
1014 Fset_buffer_major_mode (buf
);
1019 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo
, Sbuffer_disable_undo
,
1021 "Make BUFFER stop keeping undo information.\n\
1022 No argument or nil as argument means do this for the current buffer.")
1024 register Lisp_Object buffer
;
1026 Lisp_Object real_buffer
;
1029 XSETBUFFER (real_buffer
, current_buffer
);
1032 real_buffer
= Fget_buffer (buffer
);
1033 if (NILP (real_buffer
))
1037 XBUFFER (real_buffer
)->undo_list
= Qt
;
1042 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo
, Sbuffer_enable_undo
,
1044 "Start keeping undo information for buffer BUFFER.\n\
1045 No argument or nil as argument means do this for the current buffer.")
1047 register Lisp_Object buffer
;
1049 Lisp_Object real_buffer
;
1052 XSETBUFFER (real_buffer
, current_buffer
);
1055 real_buffer
= Fget_buffer (buffer
);
1056 if (NILP (real_buffer
))
1060 if (EQ (XBUFFER (real_buffer
)->undo_list
, Qt
))
1061 XBUFFER (real_buffer
)->undo_list
= Qnil
;
1067 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1068 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1069 The buffer being killed will be current while the hook is running.\n\
1072 DEFUN ("kill-buffer", Fkill_buffer
, Skill_buffer
, 1, 1, "bKill buffer: ",
1073 "Kill the buffer BUFFER.\n\
1074 The argument may be a buffer or may be the name of a buffer.\n\
1075 An argument of nil means kill the current buffer.\n\n\
1076 Value is t if the buffer is actually killed, nil if user says no.\n\n\
1077 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1078 if not void, is a list of functions to be called, with no arguments,\n\
1079 before the buffer is actually killed. The buffer to be killed is current\n\
1080 when the hook functions are called.\n\n\
1081 Any processes that have this buffer as the `process-buffer' are killed\n\
1087 register struct buffer
*b
;
1088 register Lisp_Object tem
;
1089 register struct Lisp_Marker
*m
;
1090 struct gcpro gcpro1
;
1093 buf
= Fcurrent_buffer ();
1095 buf
= Fget_buffer (buffer
);
1101 /* Avoid trouble for buffer already dead. */
1105 /* Query if the buffer is still modified. */
1106 if (INTERACTIVE
&& !NILP (b
->filename
)
1107 && BUF_MODIFF (b
) > BUF_SAVE_MODIFF (b
))
1110 tem
= do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1111 XSTRING (b
->name
)->data
));
1117 /* Run hooks with the buffer to be killed the current buffer. */
1119 int count
= specpdl_ptr
- specpdl
;
1122 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1123 set_buffer_internal (b
);
1125 /* First run the query functions; if any query is answered no,
1126 don't kill the buffer. */
1127 for (list
= Vkill_buffer_query_functions
; !NILP (list
); list
= Fcdr (list
))
1129 tem
= call0 (Fcar (list
));
1131 return unbind_to (count
, Qnil
);
1134 /* Then run the hooks. */
1135 if (!NILP (Vrun_hooks
))
1136 call1 (Vrun_hooks
, Qkill_buffer_hook
);
1137 unbind_to (count
, Qnil
);
1140 /* We have no more questions to ask. Verify that it is valid
1141 to kill the buffer. This must be done after the questions
1142 since anything can happen within do_yes_or_no_p. */
1144 /* Don't kill the minibuffer now current. */
1145 if (EQ (buf
, XWINDOW (minibuf_window
)->buffer
))
1151 /* When we kill a base buffer, kill all its indirect buffers.
1152 We do it at this stage so nothing terrible happens if they
1153 ask questions or their hooks get errors. */
1154 if (! b
->base_buffer
)
1156 struct buffer
*other
;
1160 for (other
= all_buffers
; other
; other
= other
->next
)
1161 /* all_buffers contains dead buffers too;
1162 don't re-kill them. */
1163 if (other
->base_buffer
== b
&& !NILP (other
->name
))
1166 XSETBUFFER (buf
, other
);
1173 /* Make this buffer not be current.
1174 In the process, notice if this is the sole visible buffer
1175 and give up if so. */
1176 if (b
== current_buffer
)
1178 tem
= Fother_buffer (buf
, Qnil
, Qnil
);
1180 if (b
== current_buffer
)
1184 /* Now there is no question: we can kill the buffer. */
1186 #ifdef CLASH_DETECTION
1187 /* Unlock this buffer's file, if it is locked. */
1189 #endif /* CLASH_DETECTION */
1191 kill_buffer_processes (buf
);
1193 tem
= Vinhibit_quit
;
1195 replace_buffer_in_all_windows (buf
);
1196 Vbuffer_alist
= Fdelq (Frassq (buf
, Vbuffer_alist
), Vbuffer_alist
);
1197 frames_discard_buffer (buf
);
1198 Vinhibit_quit
= tem
;
1200 /* Delete any auto-save file, if we saved it in this session. */
1201 if (STRINGP (b
->auto_save_file_name
)
1202 && b
->auto_save_modified
!= 0
1203 && BUF_SAVE_MODIFF (b
) < b
->auto_save_modified
)
1206 tem
= Fsymbol_value (intern ("delete-auto-save-files"));
1208 internal_delete_file (b
->auto_save_file_name
);
1213 /* Unchain all markers that belong to this indirect buffer.
1214 Don't unchain the markers that belong to the base buffer
1215 or its other indirect buffers. */
1216 for (tem
= BUF_MARKERS (b
); !NILP (tem
); )
1222 unchain_marker (tem
);
1228 /* Unchain all markers of this buffer and its indirect buffers.
1229 and leave them pointing nowhere. */
1230 for (tem
= BUF_MARKERS (b
); !NILP (tem
); )
1237 BUF_MARKERS (b
) = Qnil
;
1238 BUF_INTERVALS (b
) = NULL_INTERVAL
;
1240 /* Perhaps we should explicitly free the interval tree here... */
1243 /* Reset the local variables, so that this buffer's local values
1244 won't be protected from GC. They would be protected
1245 if they happened to remain encached in their symbols.
1246 This gets rid of them for certain. */
1247 swap_out_buffer_local_variables (b
);
1248 reset_buffer_local_variables (b
, 1);
1253 if (! b
->base_buffer
)
1254 BUFFER_FREE (BUF_BEG_ADDR (b
));
1256 if (b
->newline_cache
)
1258 free_region_cache (b
->newline_cache
);
1259 b
->newline_cache
= 0;
1261 if (b
->width_run_cache
)
1263 free_region_cache (b
->width_run_cache
);
1264 b
->width_run_cache
= 0;
1266 b
->width_table
= Qnil
;
1268 b
->undo_list
= Qnil
;
1273 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1274 we do this each time BUF is selected visibly, the more recently
1275 selected buffers are always closer to the front of the list. This
1276 means that other_buffer is more likely to choose a relevant buffer. */
1282 register Lisp_Object link
, prev
;
1284 frame
= selected_frame
;
1287 for (link
= Vbuffer_alist
; CONSP (link
); link
= XCDR (link
))
1289 if (EQ (XCDR (XCAR (link
)), buf
))
1294 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1295 we cannot use Fdelq itself here because it allows quitting. */
1298 Vbuffer_alist
= XCDR (Vbuffer_alist
);
1300 XCDR (prev
) = XCDR (XCDR (prev
));
1302 XCDR (link
) = Vbuffer_alist
;
1303 Vbuffer_alist
= link
;
1305 /* Now move this buffer to the front of frame_buffer_list also. */
1308 for (link
= frame_buffer_list (frame
); CONSP (link
);
1311 if (EQ (XCAR (link
), buf
))
1316 /* Effectively do delq. */
1321 set_frame_buffer_list (frame
,
1322 XCDR (frame_buffer_list (frame
)));
1324 XCDR (prev
) = XCDR (XCDR (prev
));
1326 XCDR (link
) = frame_buffer_list (frame
);
1327 set_frame_buffer_list (frame
, link
);
1330 set_frame_buffer_list (frame
, Fcons (buf
, frame_buffer_list (frame
)));
1333 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode
, Sset_buffer_major_mode
, 1, 1, 0,
1334 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1335 Use this function before selecting the buffer, since it may need to inspect\n\
1336 the current buffer's major mode.")
1341 Lisp_Object function
;
1343 function
= buffer_defaults
.major_mode
;
1344 if (NILP (function
) && NILP (Fget (current_buffer
->major_mode
, Qmode_class
)))
1345 function
= current_buffer
->major_mode
;
1347 if (NILP (function
) || EQ (function
, Qfundamental_mode
))
1350 count
= specpdl_ptr
- specpdl
;
1352 /* To select a nonfundamental mode,
1353 select the buffer temporarily and then call the mode function. */
1355 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1357 Fset_buffer (buffer
);
1360 return unbind_to (count
, Qnil
);
1363 /* If switching buffers in WINDOW would be an error, return
1364 a C string saying what the error would be. */
1367 no_switch_window (window
)
1371 if (EQ (minibuf_window
, window
))
1372 return "Cannot switch buffers in minibuffer window";
1373 tem
= Fwindow_dedicated_p (window
);
1375 return "Cannot switch buffers in a dedicated window";
1379 /* Switch to buffer BUFFER in the selected window.
1380 If NORECORD is non-nil, don't call record_buffer. */
1383 switch_to_buffer_1 (buffer
, norecord
)
1384 Lisp_Object buffer
, norecord
;
1386 register Lisp_Object buf
;
1389 buf
= Fother_buffer (Fcurrent_buffer (), Qnil
, Qnil
);
1392 buf
= Fget_buffer (buffer
);
1395 buf
= Fget_buffer_create (buffer
);
1396 Fset_buffer_major_mode (buf
);
1400 if (NILP (norecord
))
1401 record_buffer (buf
);
1403 Fset_window_buffer (EQ (selected_window
, minibuf_window
)
1404 ? Fnext_window (minibuf_window
, Qnil
, Qnil
)
1411 DEFUN ("switch-to-buffer", Fswitch_to_buffer
, Sswitch_to_buffer
, 1, 2, "BSwitch to buffer: ",
1412 "Select buffer BUFFER in the current window.\n\
1413 BUFFER may be a buffer or a buffer name.\n\
1414 Optional second arg NORECORD non-nil means\n\
1415 do not put this buffer at the front of the list of recently selected ones.\n\
1417 WARNING: This is NOT the way to work on another buffer temporarily\n\
1418 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1419 the window-buffer correspondences.")
1421 Lisp_Object buffer
, norecord
;
1425 err
= no_switch_window (selected_window
);
1426 if (err
) error (err
);
1428 return switch_to_buffer_1 (buffer
, norecord
);
1431 DEFUN ("pop-to-buffer", Fpop_to_buffer
, Spop_to_buffer
, 1, 3, 0,
1432 "Select buffer BUFFER in some window, preferably a different one.\n\
1433 If BUFFER is nil, then some other buffer is chosen.\n\
1434 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1435 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1436 window even if BUFFER is already visible in the selected window.\n\
1437 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1438 of `display-buffer' for additional customization information.\n\
1440 Optional third arg NORECORD non-nil means\n\
1441 do not put this buffer at the front of the list of recently selected ones.")
1442 (buffer
, other_window
, norecord
)
1443 Lisp_Object buffer
, other_window
, norecord
;
1445 register Lisp_Object buf
;
1447 buf
= Fother_buffer (Fcurrent_buffer (), Qnil
, Qnil
);
1450 buf
= Fget_buffer (buffer
);
1453 buf
= Fget_buffer_create (buffer
);
1454 Fset_buffer_major_mode (buf
);
1458 if (NILP (norecord
))
1459 record_buffer (buf
);
1460 Fselect_window (Fdisplay_buffer (buf
, other_window
, Qnil
));
1464 DEFUN ("current-buffer", Fcurrent_buffer
, Scurrent_buffer
, 0, 0, 0,
1465 "Return the current buffer as a Lisp object.")
1468 register Lisp_Object buf
;
1469 XSETBUFFER (buf
, current_buffer
);
1473 /* Set the current buffer to B.
1475 We previously set windows_or_buffers_changed here to invalidate
1476 global unchanged information in beg_unchanged and end_unchanged.
1477 This is no longer necessary because we now compute unchanged
1478 information on a buffer-basis. Every action affecting other
1479 windows than the selected one requires a select_window at some
1480 time, and that increments windows_or_buffers_changed. */
1483 set_buffer_internal (b
)
1484 register struct buffer
*b
;
1486 if (current_buffer
!= b
)
1487 set_buffer_internal_1 (b
);
1490 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1491 This is used by redisplay. */
1494 set_buffer_internal_1 (b
)
1495 register struct buffer
*b
;
1497 register struct buffer
*old_buf
;
1498 register Lisp_Object tail
, valcontents
;
1501 if (current_buffer
== b
)
1504 old_buf
= current_buffer
;
1506 last_known_column_point
= -1; /* invalidate indentation cache */
1510 /* Put the undo list back in the base buffer, so that it appears
1511 that an indirect buffer shares the undo list of its base. */
1512 if (old_buf
->base_buffer
)
1513 old_buf
->base_buffer
->undo_list
= old_buf
->undo_list
;
1515 /* If the old current buffer has markers to record PT, BEGV and ZV
1516 when it is not current, update them now. */
1517 if (! NILP (old_buf
->pt_marker
))
1520 XSETBUFFER (obuf
, old_buf
);
1521 set_marker_both (old_buf
->pt_marker
, obuf
,
1522 BUF_PT (old_buf
), BUF_PT_BYTE (old_buf
));
1524 if (! NILP (old_buf
->begv_marker
))
1527 XSETBUFFER (obuf
, old_buf
);
1528 set_marker_both (old_buf
->begv_marker
, obuf
,
1529 BUF_BEGV (old_buf
), BUF_BEGV_BYTE (old_buf
));
1531 if (! NILP (old_buf
->zv_marker
))
1534 XSETBUFFER (obuf
, old_buf
);
1535 set_marker_both (old_buf
->zv_marker
, obuf
,
1536 BUF_ZV (old_buf
), BUF_ZV_BYTE (old_buf
));
1540 /* Get the undo list from the base buffer, so that it appears
1541 that an indirect buffer shares the undo list of its base. */
1543 b
->undo_list
= b
->base_buffer
->undo_list
;
1545 /* If the new current buffer has markers to record PT, BEGV and ZV
1546 when it is not current, fetch them now. */
1547 if (! NILP (b
->pt_marker
))
1549 BUF_PT (b
) = marker_position (b
->pt_marker
);
1550 BUF_PT_BYTE (b
) = marker_byte_position (b
->pt_marker
);
1552 if (! NILP (b
->begv_marker
))
1554 BUF_BEGV (b
) = marker_position (b
->begv_marker
);
1555 BUF_BEGV_BYTE (b
) = marker_byte_position (b
->begv_marker
);
1557 if (! NILP (b
->zv_marker
))
1559 BUF_ZV (b
) = marker_position (b
->zv_marker
);
1560 BUF_ZV_BYTE (b
) = marker_byte_position (b
->zv_marker
);
1563 /* Look down buffer's list of local Lisp variables
1564 to find and update any that forward into C variables. */
1566 for (tail
= b
->local_var_alist
; !NILP (tail
); tail
= XCDR (tail
))
1568 valcontents
= XSYMBOL (XCAR (XCAR (tail
)))->value
;
1569 if ((BUFFER_LOCAL_VALUEP (valcontents
)
1570 || SOME_BUFFER_LOCAL_VALUEP (valcontents
))
1571 && (tem
= XBUFFER_LOCAL_VALUE (valcontents
)->realvalue
,
1572 (BOOLFWDP (tem
) || INTFWDP (tem
) || OBJFWDP (tem
))))
1573 /* Just reference the variable
1574 to cause it to become set for this buffer. */
1575 Fsymbol_value (XCAR (XCAR (tail
)));
1578 /* Do the same with any others that were local to the previous buffer */
1581 for (tail
= old_buf
->local_var_alist
; !NILP (tail
); tail
= XCDR (tail
))
1583 valcontents
= XSYMBOL (XCAR (XCAR (tail
)))->value
;
1584 if ((BUFFER_LOCAL_VALUEP (valcontents
)
1585 || SOME_BUFFER_LOCAL_VALUEP (valcontents
))
1586 && (tem
= XBUFFER_LOCAL_VALUE (valcontents
)->realvalue
,
1587 (BOOLFWDP (tem
) || INTFWDP (tem
) || OBJFWDP (tem
))))
1588 /* Just reference the variable
1589 to cause it to become set for this buffer. */
1590 Fsymbol_value (XCAR (XCAR (tail
)));
1594 /* Switch to buffer B temporarily for redisplay purposes.
1595 This avoids certain things that don't need to be done within redisplay. */
1601 register struct buffer
*old_buf
;
1603 if (current_buffer
== b
)
1606 old_buf
= current_buffer
;
1611 /* If the old current buffer has markers to record PT, BEGV and ZV
1612 when it is not current, update them now. */
1613 if (! NILP (old_buf
->pt_marker
))
1616 XSETBUFFER (obuf
, old_buf
);
1617 set_marker_both (old_buf
->pt_marker
, obuf
,
1618 BUF_PT (old_buf
), BUF_PT_BYTE (old_buf
));
1620 if (! NILP (old_buf
->begv_marker
))
1623 XSETBUFFER (obuf
, old_buf
);
1624 set_marker_both (old_buf
->begv_marker
, obuf
,
1625 BUF_BEGV (old_buf
), BUF_BEGV_BYTE (old_buf
));
1627 if (! NILP (old_buf
->zv_marker
))
1630 XSETBUFFER (obuf
, old_buf
);
1631 set_marker_both (old_buf
->zv_marker
, obuf
,
1632 BUF_ZV (old_buf
), BUF_ZV_BYTE (old_buf
));
1636 /* If the new current buffer has markers to record PT, BEGV and ZV
1637 when it is not current, fetch them now. */
1638 if (! NILP (b
->pt_marker
))
1640 BUF_PT (b
) = marker_position (b
->pt_marker
);
1641 BUF_PT_BYTE (b
) = marker_byte_position (b
->pt_marker
);
1643 if (! NILP (b
->begv_marker
))
1645 BUF_BEGV (b
) = marker_position (b
->begv_marker
);
1646 BUF_BEGV_BYTE (b
) = marker_byte_position (b
->begv_marker
);
1648 if (! NILP (b
->zv_marker
))
1650 BUF_ZV (b
) = marker_position (b
->zv_marker
);
1651 BUF_ZV_BYTE (b
) = marker_byte_position (b
->zv_marker
);
1655 DEFUN ("set-buffer", Fset_buffer
, Sset_buffer
, 1, 1, 0,
1656 "Make the buffer BUFFER current for editing operations.\n\
1657 BUFFER may be a buffer or the name of an existing buffer.\n\
1658 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1659 This function does not display the buffer, so its effect ends\n\
1660 when the current command terminates.\n\
1661 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1663 register Lisp_Object buffer
;
1665 register Lisp_Object buf
;
1666 buf
= Fget_buffer (buffer
);
1669 if (NILP (XBUFFER (buf
)->name
))
1670 error ("Selecting deleted buffer");
1671 set_buffer_internal (XBUFFER (buf
));
1675 /* Set the current buffer to BUFFER provided it is alive. */
1678 set_buffer_if_live (buffer
)
1681 if (! NILP (XBUFFER (buffer
)->name
))
1682 Fset_buffer (buffer
);
1686 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only
,
1687 Sbarf_if_buffer_read_only
, 0, 0, 0,
1688 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1691 if (!NILP (current_buffer
->read_only
)
1692 && NILP (Vinhibit_read_only
))
1693 Fsignal (Qbuffer_read_only
, (Fcons (Fcurrent_buffer (), Qnil
)));
1697 DEFUN ("bury-buffer", Fbury_buffer
, Sbury_buffer
, 0, 1, "",
1698 "Put BUFFER at the end of the list of all buffers.\n\
1699 There it is the least likely candidate for `other-buffer' to return;\n\
1700 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1701 If BUFFER is nil or omitted, bury the current buffer.\n\
1702 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1703 selected window if it is displayed there.")
1705 register Lisp_Object buffer
;
1707 /* Figure out what buffer we're going to bury. */
1710 XSETBUFFER (buffer
, current_buffer
);
1712 /* If we're burying the current buffer, unshow it. */
1713 Fswitch_to_buffer (Fother_buffer (buffer
, Qnil
, Qnil
), Qnil
);
1719 buf1
= Fget_buffer (buffer
);
1725 /* Move buffer to the end of the buffer list. */
1727 register Lisp_Object aelt
, link
;
1729 aelt
= Frassq (buffer
, Vbuffer_alist
);
1730 link
= Fmemq (aelt
, Vbuffer_alist
);
1731 Vbuffer_alist
= Fdelq (aelt
, Vbuffer_alist
);
1733 Vbuffer_alist
= nconc2 (Vbuffer_alist
, link
);
1736 frames_bury_buffer (buffer
);
1741 DEFUN ("erase-buffer", Ferase_buffer
, Serase_buffer
, 0, 0, "*",
1742 "Delete the entire contents of the current buffer.\n\
1743 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1744 so the buffer is truly empty after this.")
1751 current_buffer
->last_window_start
= 1;
1752 /* Prevent warnings, or suspension of auto saving, that would happen
1753 if future size is less than past size. Use of erase-buffer
1754 implies that the future text is not really related to the past text. */
1755 XSETFASTINT (current_buffer
->save_length
, 0);
1760 validate_region (b
, e
)
1761 register Lisp_Object
*b
, *e
;
1763 CHECK_NUMBER_COERCE_MARKER (*b
, 0);
1764 CHECK_NUMBER_COERCE_MARKER (*e
, 1);
1766 if (XINT (*b
) > XINT (*e
))
1769 tem
= *b
; *b
= *e
; *e
= tem
;
1772 if (!(BEGV
<= XINT (*b
) && XINT (*b
) <= XINT (*e
)
1773 && XINT (*e
) <= ZV
))
1774 args_out_of_range (*b
, *e
);
1777 /* Advance BYTE_POS up to a character boundary
1778 and return the adjusted position. */
1781 advance_to_char_boundary (byte_pos
)
1786 if (byte_pos
== BEG
)
1787 /* Beginning of buffer is always a character boundary. */
1790 c
= FETCH_BYTE (byte_pos
);
1791 if (! CHAR_HEAD_P (c
))
1793 /* We should advance BYTE_POS only when C is a constituent of a
1794 multibyte sequence. */
1797 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1798 surely advance to the correct character boundary. If C is
1799 not, BYTE_POS was unchanged. */
1805 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte
, Sset_buffer_multibyte
,
1807 "Set the multibyte flag of the current buffer to FLAG.\n\
1808 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1809 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1810 The buffer contents remain unchanged as a sequence of bytes\n\
1811 but the contents viewed as characters do change.")
1815 Lisp_Object tail
, markers
;
1816 struct buffer
*other
;
1818 if (current_buffer
->base_buffer
)
1819 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
1821 /* Do nothing if nothing actually changes. */
1822 if (NILP (flag
) == NILP (current_buffer
->enable_multibyte_characters
))
1825 /* It would be better to update the list,
1826 but this is good enough for now. */
1827 if (! EQ (current_buffer
->undo_list
, Qt
))
1828 current_buffer
->undo_list
= Qnil
;
1830 /* If the cached position is for this buffer, clear it out. */
1831 clear_charpos_cache (current_buffer
);
1835 /* Do this first, so it can use CHAR_TO_BYTE
1836 to calculate the old correspondences. */
1837 set_intervals_multibyte (0);
1839 current_buffer
->enable_multibyte_characters
= Qnil
;
1845 TEMP_SET_PT_BOTH (PT_BYTE
, PT_BYTE
);
1847 tail
= BUF_MARKERS (current_buffer
);
1848 while (! NILP (tail
))
1850 XMARKER (tail
)->charpos
= XMARKER (tail
)->bytepos
;
1851 tail
= XMARKER (tail
)->chain
;
1856 /* Be sure not to have a multibyte sequence striding over the GAP.
1857 Ex: We change this: "...abc\201\241\241 _GAP_ \241\241\241..."
1858 to: "...abc _GAP_ \201\241\241\241\241\241..." */
1860 if (GPT_BYTE
> 1 && GPT_BYTE
< Z_BYTE
1861 && ! CHAR_HEAD_P (*(GAP_END_ADDR
)))
1863 unsigned char *p
= GPT_ADDR
- 1;
1865 while (! CHAR_HEAD_P (*p
) && p
> BEG_ADDR
) p
--;
1866 if (BASE_LEADING_CODE_P (*p
))
1868 int new_gpt
= GPT_BYTE
- (GPT_ADDR
- p
);
1870 move_gap_both (new_gpt
, new_gpt
);
1874 /* Do this first, so that chars_in_text asks the right question.
1875 set_intervals_multibyte needs it too. */
1876 current_buffer
->enable_multibyte_characters
= Qt
;
1878 GPT_BYTE
= advance_to_char_boundary (GPT_BYTE
);
1879 GPT
= chars_in_text (BEG_ADDR
, GPT_BYTE
- BEG_BYTE
) + BEG
;
1881 Z
= chars_in_text (GAP_END_ADDR
, Z_BYTE
- GPT_BYTE
) + GPT
;
1883 BEGV_BYTE
= advance_to_char_boundary (BEGV_BYTE
);
1884 if (BEGV_BYTE
> GPT_BYTE
)
1885 BEGV
= chars_in_text (GAP_END_ADDR
, BEGV_BYTE
- GPT_BYTE
) + GPT
;
1887 BEGV
= chars_in_text (BEG_ADDR
, BEGV_BYTE
- BEG_BYTE
) + BEG
;
1889 ZV_BYTE
= advance_to_char_boundary (ZV_BYTE
);
1890 if (ZV_BYTE
> GPT_BYTE
)
1891 ZV
= chars_in_text (GAP_END_ADDR
, ZV_BYTE
- GPT_BYTE
) + GPT
;
1893 ZV
= chars_in_text (BEG_ADDR
, ZV_BYTE
- BEG_BYTE
) + BEG
;
1896 int pt_byte
= advance_to_char_boundary (PT_BYTE
);
1899 if (pt_byte
> GPT_BYTE
)
1900 pt
= chars_in_text (GAP_END_ADDR
, pt_byte
- GPT_BYTE
) + GPT
;
1902 pt
= chars_in_text (BEG_ADDR
, pt_byte
- BEG_BYTE
) + BEG
;
1903 TEMP_SET_PT_BOTH (pt
, pt_byte
);
1906 tail
= markers
= BUF_MARKERS (current_buffer
);
1908 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
1909 getting confused by the markers that have not yet been updated.
1910 It is also a signal that it should never create a marker. */
1911 BUF_MARKERS (current_buffer
) = Qnil
;
1913 while (! NILP (tail
))
1915 XMARKER (tail
)->bytepos
1916 = advance_to_char_boundary (XMARKER (tail
)->bytepos
);
1917 XMARKER (tail
)->charpos
= BYTE_TO_CHAR (XMARKER (tail
)->bytepos
);
1919 tail
= XMARKER (tail
)->chain
;
1922 /* Make sure no markers were put on the chain
1923 while the chain value was incorrect. */
1924 if (! EQ (BUF_MARKERS (current_buffer
), Qnil
))
1927 BUF_MARKERS (current_buffer
) = markers
;
1929 /* Do this last, so it can calculate the new correspondences
1930 between chars and bytes. */
1931 set_intervals_multibyte (1);
1934 /* Changing the multibyteness of a buffer means that all windows
1935 showing that buffer must be updated thoroughly. */
1936 current_buffer
->prevent_redisplay_optimizations_p
= 1;
1937 ++windows_or_buffers_changed
;
1939 /* Copy this buffer's new multibyte status
1940 into all of its indirect buffers. */
1941 for (other
= all_buffers
; other
; other
= other
->next
)
1942 if (other
->base_buffer
== current_buffer
&& !NILP (other
->name
))
1944 other
->enable_multibyte_characters
1945 = current_buffer
->enable_multibyte_characters
;
1946 other
->prevent_redisplay_optimizations_p
= 1;
1952 DEFUN ("kill-all-local-variables", Fkill_all_local_variables
, Skill_all_local_variables
,
1954 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1955 Most local variable bindings are eliminated so that the default values\n\
1956 become effective once more. Also, the syntax table is set from\n\
1957 `standard-syntax-table', the local keymap is set to nil,\n\
1958 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1959 This function also forces redisplay of the mode line.\n\
1961 Every function to select a new major mode starts by\n\
1962 calling this function.\n\n\
1963 As a special exception, local variables whose names have\n\
1964 a non-nil `permanent-local' property are not eliminated by this function.\n\
1966 The first thing this function does is run\n\
1967 the normal hook `change-major-mode-hook'.")
1970 register Lisp_Object alist
, sym
, tem
;
1973 if (!NILP (Vrun_hooks
))
1974 call1 (Vrun_hooks
, intern ("change-major-mode-hook"));
1975 oalist
= current_buffer
->local_var_alist
;
1977 /* Make sure none of the bindings in oalist
1978 remain swapped in, in their symbols. */
1980 swap_out_buffer_local_variables (current_buffer
);
1982 /* Actually eliminate all local bindings of this buffer. */
1984 reset_buffer_local_variables (current_buffer
, 0);
1986 /* Redisplay mode lines; we are changing major mode. */
1988 update_mode_lines
++;
1990 /* Any which are supposed to be permanent,
1991 make local again, with the same values they had. */
1993 for (alist
= oalist
; !NILP (alist
); alist
= XCDR (alist
))
1995 sym
= XCAR (XCAR (alist
));
1996 tem
= Fget (sym
, Qpermanent_local
);
1999 Fmake_local_variable (sym
);
2000 Fset (sym
, XCDR (XCAR (alist
)));
2004 /* Force mode-line redisplay. Useful here because all major mode
2005 commands call this function. */
2006 update_mode_lines
++;
2011 /* Make sure no local variables remain set up with buffer B
2012 for their current values. */
2015 swap_out_buffer_local_variables (b
)
2018 Lisp_Object oalist
, alist
, sym
, tem
, buffer
;
2020 XSETBUFFER (buffer
, b
);
2021 oalist
= b
->local_var_alist
;
2023 for (alist
= oalist
; !NILP (alist
); alist
= XCDR (alist
))
2025 sym
= XCAR (XCAR (alist
));
2027 /* Need not do anything if some other buffer's binding is now encached. */
2028 tem
= XBUFFER_LOCAL_VALUE (XSYMBOL (sym
)->value
)->buffer
;
2029 if (BUFFERP (tem
) && XBUFFER (tem
) == current_buffer
)
2031 /* Symbol is set up for this buffer's old local value.
2032 Set it up for the current buffer with the default value. */
2034 tem
= XBUFFER_LOCAL_VALUE (XSYMBOL (sym
)->value
)->cdr
;
2035 /* Store the symbol's current value into the alist entry
2036 it is currently set up for. This is so that, if the
2037 local is marked permanent, and we make it local again
2038 later in Fkill_all_local_variables, we don't lose the value. */
2040 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym
)->value
)->realvalue
);
2041 /* Switch to the symbol's default-value alist entry. */
2043 /* Mark it as current for buffer B. */
2044 XBUFFER_LOCAL_VALUE (XSYMBOL (sym
)->value
)->buffer
= buffer
;
2045 /* Store the current value into any forwarding in the symbol. */
2046 store_symval_forwarding (sym
,
2047 XBUFFER_LOCAL_VALUE (XSYMBOL (sym
)->value
)->realvalue
,
2053 /* Find all the overlays in the current buffer that contain position POS.
2054 Return the number found, and store them in a vector in *VEC_PTR.
2055 Store in *LEN_PTR the size allocated for the vector.
2056 Store in *NEXT_PTR the next position after POS where an overlay starts,
2057 or ZV if there are no more overlays.
2058 Store in *PREV_PTR the previous position before POS where an overlay ends,
2059 or where an overlay starts which ends at or after POS;
2060 or BEGV if there are no such overlays.
2061 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2063 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2064 when this function is called.
2066 If EXTEND is non-zero, we make the vector bigger if necessary.
2067 If EXTEND is zero, we never extend the vector,
2068 and we store only as many overlays as will fit.
2069 But we still return the total number of overlays. */
2072 overlays_at (pos
, extend
, vec_ptr
, len_ptr
, next_ptr
, prev_ptr
)
2075 Lisp_Object
**vec_ptr
;
2080 Lisp_Object tail
, overlay
, start
, end
;
2083 Lisp_Object
*vec
= *vec_ptr
;
2086 int inhibit_storing
= 0;
2088 for (tail
= current_buffer
->overlays_before
;
2092 int startpos
, endpos
;
2094 overlay
= XCAR (tail
);
2096 start
= OVERLAY_START (overlay
);
2097 end
= OVERLAY_END (overlay
);
2098 endpos
= OVERLAY_POSITION (end
);
2105 startpos
= OVERLAY_POSITION (start
);
2106 /* This one ends at or after POS
2107 so its start counts for PREV_PTR if it's before POS. */
2108 if (prev
< startpos
&& startpos
< pos
)
2112 if (startpos
<= pos
)
2116 /* The supplied vector is full.
2117 Either make it bigger, or don't store any more in it. */
2120 /* Make it work with an initial len == 0. */
2125 vec
= (Lisp_Object
*) xrealloc (vec
, len
* sizeof (Lisp_Object
));
2129 inhibit_storing
= 1;
2132 if (!inhibit_storing
)
2134 /* Keep counting overlays even if we can't return them all. */
2137 else if (startpos
< next
)
2141 for (tail
= current_buffer
->overlays_after
;
2145 int startpos
, endpos
;
2147 overlay
= XCAR (tail
);
2149 start
= OVERLAY_START (overlay
);
2150 end
= OVERLAY_END (overlay
);
2151 startpos
= OVERLAY_POSITION (start
);
2154 if (startpos
< next
)
2158 endpos
= OVERLAY_POSITION (end
);
2165 *len_ptr
= len
*= 2;
2168 vec
= (Lisp_Object
*) xrealloc (vec
, len
* sizeof (Lisp_Object
));
2172 inhibit_storing
= 1;
2175 if (!inhibit_storing
)
2179 if (startpos
< pos
&& startpos
> prev
)
2182 else if (endpos
< pos
&& endpos
> prev
)
2184 else if (endpos
== pos
&& startpos
> prev
)
2195 /* Find all the overlays in the current buffer that overlap the range BEG-END
2196 or are empty at BEG.
2198 Return the number found, and store them in a vector in *VEC_PTR.
2199 Store in *LEN_PTR the size allocated for the vector.
2200 Store in *NEXT_PTR the next position after POS where an overlay starts,
2201 or ZV if there are no more overlays.
2202 Store in *PREV_PTR the previous position before POS where an overlay ends,
2203 or BEGV if there are no previous overlays.
2204 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2206 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2207 when this function is called.
2209 If EXTEND is non-zero, we make the vector bigger if necessary.
2210 If EXTEND is zero, we never extend the vector,
2211 and we store only as many overlays as will fit.
2212 But we still return the total number of overlays. */
2215 overlays_in (beg
, end
, extend
, vec_ptr
, len_ptr
, next_ptr
, prev_ptr
)
2218 Lisp_Object
**vec_ptr
;
2223 Lisp_Object tail
, overlay
, ostart
, oend
;
2226 Lisp_Object
*vec
= *vec_ptr
;
2229 int inhibit_storing
= 0;
2231 for (tail
= current_buffer
->overlays_before
;
2235 int startpos
, endpos
;
2237 overlay
= XCAR (tail
);
2239 ostart
= OVERLAY_START (overlay
);
2240 oend
= OVERLAY_END (overlay
);
2241 endpos
= OVERLAY_POSITION (oend
);
2248 startpos
= OVERLAY_POSITION (ostart
);
2249 /* Count an interval if it either overlaps the range
2250 or is empty at the start of the range. */
2251 if ((beg
< endpos
&& startpos
< end
)
2252 || (startpos
== endpos
&& beg
== endpos
))
2256 /* The supplied vector is full.
2257 Either make it bigger, or don't store any more in it. */
2260 *len_ptr
= len
*= 2;
2261 vec
= (Lisp_Object
*) xrealloc (vec
, len
* sizeof (Lisp_Object
));
2265 inhibit_storing
= 1;
2268 if (!inhibit_storing
)
2270 /* Keep counting overlays even if we can't return them all. */
2273 else if (startpos
< next
)
2277 for (tail
= current_buffer
->overlays_after
;
2281 int startpos
, endpos
;
2283 overlay
= XCAR (tail
);
2285 ostart
= OVERLAY_START (overlay
);
2286 oend
= OVERLAY_END (overlay
);
2287 startpos
= OVERLAY_POSITION (ostart
);
2290 if (startpos
< next
)
2294 endpos
= OVERLAY_POSITION (oend
);
2295 /* Count an interval if it either overlaps the range
2296 or is empty at the start of the range. */
2297 if ((beg
< endpos
&& startpos
< end
)
2298 || (startpos
== endpos
&& beg
== endpos
))
2304 *len_ptr
= len
*= 2;
2305 vec
= (Lisp_Object
*) xrealloc (vec
, len
* sizeof (Lisp_Object
));
2309 inhibit_storing
= 1;
2312 if (!inhibit_storing
)
2316 else if (endpos
< beg
&& endpos
> prev
)
2327 /* Fast function to just test if we're at an overlay boundary. */
2329 overlay_touches_p (pos
)
2332 Lisp_Object tail
, overlay
;
2334 for (tail
= current_buffer
->overlays_before
; GC_CONSP (tail
);
2339 overlay
= XCAR (tail
);
2340 if (!GC_OVERLAYP (overlay
))
2343 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2346 if (endpos
== pos
|| OVERLAY_POSITION (OVERLAY_START (overlay
)) == pos
)
2350 for (tail
= current_buffer
->overlays_after
; GC_CONSP (tail
);
2355 overlay
= XCAR (tail
);
2356 if (!GC_OVERLAYP (overlay
))
2359 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2362 if (startpos
== pos
|| OVERLAY_POSITION (OVERLAY_END (overlay
)) == pos
)
2370 Lisp_Object overlay
;
2376 compare_overlays (v1
, v2
)
2377 const void *v1
, *v2
;
2379 const struct sortvec
*s1
= (const struct sortvec
*) v1
;
2380 const struct sortvec
*s2
= (const struct sortvec
*) v2
;
2381 if (s1
->priority
!= s2
->priority
)
2382 return s1
->priority
- s2
->priority
;
2383 if (s1
->beg
!= s2
->beg
)
2384 return s1
->beg
- s2
->beg
;
2385 if (s1
->end
!= s2
->end
)
2386 return s2
->end
- s1
->end
;
2390 /* Sort an array of overlays by priority. The array is modified in place.
2391 The return value is the new size; this may be smaller than the original
2392 size if some of the overlays were invalid or were window-specific. */
2394 sort_overlays (overlay_vec
, noverlays
, w
)
2395 Lisp_Object
*overlay_vec
;
2400 struct sortvec
*sortvec
;
2401 sortvec
= (struct sortvec
*) alloca (noverlays
* sizeof (struct sortvec
));
2403 /* Put the valid and relevant overlays into sortvec. */
2405 for (i
= 0, j
= 0; i
< noverlays
; i
++)
2408 Lisp_Object overlay
;
2410 overlay
= overlay_vec
[i
];
2411 if (OVERLAY_VALID (overlay
)
2412 && OVERLAY_POSITION (OVERLAY_START (overlay
)) > 0
2413 && OVERLAY_POSITION (OVERLAY_END (overlay
)) > 0)
2415 /* If we're interested in a specific window, then ignore
2416 overlays that are limited to some other window. */
2421 window
= Foverlay_get (overlay
, Qwindow
);
2422 if (WINDOWP (window
) && XWINDOW (window
) != w
)
2426 /* This overlay is good and counts: put it into sortvec. */
2427 sortvec
[j
].overlay
= overlay
;
2428 sortvec
[j
].beg
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2429 sortvec
[j
].end
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2430 tem
= Foverlay_get (overlay
, Qpriority
);
2432 sortvec
[j
].priority
= XINT (tem
);
2434 sortvec
[j
].priority
= 0;
2440 /* Sort the overlays into the proper order: increasing priority. */
2443 qsort (sortvec
, noverlays
, sizeof (struct sortvec
), compare_overlays
);
2445 for (i
= 0; i
< noverlays
; i
++)
2446 overlay_vec
[i
] = sortvec
[i
].overlay
;
2452 Lisp_Object string
, string2
;
2459 struct sortstr
*buf
; /* An array that expands as needed; never freed. */
2460 int size
; /* Allocated length of that array. */
2461 int used
; /* How much of the array is currently in use. */
2462 int bytes
; /* Total length of the strings in buf. */
2465 /* Buffers for storing information about the overlays touching a given
2466 position. These could be automatic variables in overlay_strings, but
2467 it's more efficient to hold onto the memory instead of repeatedly
2468 allocating and freeing it. */
2469 static struct sortstrlist overlay_heads
, overlay_tails
;
2470 static unsigned char *overlay_str_buf
;
2472 /* Allocated length of overlay_str_buf. */
2473 static int overlay_str_len
;
2475 /* A comparison function suitable for passing to qsort. */
2477 cmp_for_strings (as1
, as2
)
2480 struct sortstr
*s1
= (struct sortstr
*)as1
;
2481 struct sortstr
*s2
= (struct sortstr
*)as2
;
2482 if (s1
->size
!= s2
->size
)
2483 return s2
->size
- s1
->size
;
2484 if (s1
->priority
!= s2
->priority
)
2485 return s1
->priority
- s2
->priority
;
2490 record_overlay_string (ssl
, str
, str2
, pri
, size
)
2491 struct sortstrlist
*ssl
;
2492 Lisp_Object str
, str2
, pri
;
2497 if (ssl
->used
== ssl
->size
)
2503 ssl
->buf
= ((struct sortstr
*)
2504 xrealloc (ssl
->buf
, ssl
->size
* sizeof (struct sortstr
)));
2506 ssl
->buf
[ssl
->used
].string
= str
;
2507 ssl
->buf
[ssl
->used
].string2
= str2
;
2508 ssl
->buf
[ssl
->used
].size
= size
;
2509 ssl
->buf
[ssl
->used
].priority
= (INTEGERP (pri
) ? XINT (pri
) : 0);
2512 if (NILP (current_buffer
->enable_multibyte_characters
))
2513 nbytes
= XSTRING (str
)->size
;
2514 else if (! STRING_MULTIBYTE (str
))
2515 nbytes
= count_size_as_multibyte (XSTRING (str
)->data
,
2516 STRING_BYTES (XSTRING (str
)));
2518 nbytes
= STRING_BYTES (XSTRING (str
));
2520 ssl
->bytes
+= nbytes
;
2524 if (NILP (current_buffer
->enable_multibyte_characters
))
2525 nbytes
= XSTRING (str2
)->size
;
2526 else if (! STRING_MULTIBYTE (str2
))
2527 nbytes
= count_size_as_multibyte (XSTRING (str2
)->data
,
2528 STRING_BYTES (XSTRING (str2
)));
2530 nbytes
= STRING_BYTES (XSTRING (str2
));
2532 ssl
->bytes
+= nbytes
;
2536 /* Return the concatenation of the strings associated with overlays that
2537 begin or end at POS, ignoring overlays that are specific to a window
2538 other than W. The strings are concatenated in the appropriate order:
2539 shorter overlays nest inside longer ones, and higher priority inside
2540 lower. Normally all of the after-strings come first, but zero-sized
2541 overlays have their after-strings ride along with the before-strings
2542 because it would look strange to print them inside-out.
2544 Returns the string length, and stores the contents indirectly through
2545 PSTR, if that variable is non-null. The string may be overwritten by
2546 subsequent calls. */
2549 overlay_strings (pos
, w
, pstr
)
2552 unsigned char **pstr
;
2554 Lisp_Object ov
, overlay
, window
, str
;
2555 int startpos
, endpos
;
2556 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
2558 overlay_heads
.used
= overlay_heads
.bytes
= 0;
2559 overlay_tails
.used
= overlay_tails
.bytes
= 0;
2560 for (ov
= current_buffer
->overlays_before
; CONSP (ov
); ov
= XCDR (ov
))
2562 overlay
= XCAR (ov
);
2563 if (!OVERLAYP (overlay
))
2566 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2567 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2570 if (endpos
!= pos
&& startpos
!= pos
)
2572 window
= Foverlay_get (overlay
, Qwindow
);
2573 if (WINDOWP (window
) && XWINDOW (window
) != w
)
2576 && (str
= Foverlay_get (overlay
, Qbefore_string
), STRINGP (str
)))
2577 record_overlay_string (&overlay_heads
, str
,
2579 ? Foverlay_get (overlay
, Qafter_string
)
2581 Foverlay_get (overlay
, Qpriority
),
2583 else if (endpos
== pos
2584 && (str
= Foverlay_get (overlay
, Qafter_string
), STRINGP (str
)))
2585 record_overlay_string (&overlay_tails
, str
, Qnil
,
2586 Foverlay_get (overlay
, Qpriority
),
2589 for (ov
= current_buffer
->overlays_after
; CONSP (ov
); ov
= XCDR (ov
))
2591 overlay
= XCAR (ov
);
2592 if (!OVERLAYP (overlay
))
2595 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2596 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2599 if (endpos
!= pos
&& startpos
!= pos
)
2601 window
= Foverlay_get (overlay
, Qwindow
);
2602 if (WINDOWP (window
) && XWINDOW (window
) != w
)
2605 && (str
= Foverlay_get (overlay
, Qbefore_string
), STRINGP (str
)))
2606 record_overlay_string (&overlay_heads
, str
,
2608 ? Foverlay_get (overlay
, Qafter_string
)
2610 Foverlay_get (overlay
, Qpriority
),
2612 else if (endpos
== pos
2613 && (str
= Foverlay_get (overlay
, Qafter_string
), STRINGP (str
)))
2614 record_overlay_string (&overlay_tails
, str
, Qnil
,
2615 Foverlay_get (overlay
, Qpriority
),
2618 if (overlay_tails
.used
> 1)
2619 qsort (overlay_tails
.buf
, overlay_tails
.used
, sizeof (struct sortstr
),
2621 if (overlay_heads
.used
> 1)
2622 qsort (overlay_heads
.buf
, overlay_heads
.used
, sizeof (struct sortstr
),
2624 if (overlay_heads
.bytes
|| overlay_tails
.bytes
)
2629 int total
= overlay_heads
.bytes
+ overlay_tails
.bytes
;
2631 if (total
> overlay_str_len
)
2633 overlay_str_len
= total
;
2634 overlay_str_buf
= (unsigned char *)xrealloc (overlay_str_buf
,
2637 p
= overlay_str_buf
;
2638 for (i
= overlay_tails
.used
; --i
>= 0;)
2641 tem
= overlay_tails
.buf
[i
].string
;
2642 nbytes
= copy_text (XSTRING (tem
)->data
, p
,
2643 STRING_BYTES (XSTRING (tem
)),
2644 STRING_MULTIBYTE (tem
), multibyte
);
2647 for (i
= 0; i
< overlay_heads
.used
; ++i
)
2650 tem
= overlay_heads
.buf
[i
].string
;
2651 nbytes
= copy_text (XSTRING (tem
)->data
, p
,
2652 STRING_BYTES (XSTRING (tem
)),
2653 STRING_MULTIBYTE (tem
), multibyte
);
2655 tem
= overlay_heads
.buf
[i
].string2
;
2658 nbytes
= copy_text (XSTRING (tem
)->data
, p
,
2659 STRING_BYTES (XSTRING (tem
)),
2660 STRING_MULTIBYTE (tem
), multibyte
);
2664 if (p
!= overlay_str_buf
+ total
)
2667 *pstr
= overlay_str_buf
;
2673 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2676 recenter_overlay_lists (buf
, pos
)
2680 Lisp_Object overlay
, tail
, next
, prev
, beg
, end
;
2682 /* See if anything in overlays_before should move to overlays_after. */
2684 /* We don't strictly need prev in this loop; it should always be nil.
2685 But we use it for symmetry and in case that should cease to be true
2686 with some future change. */
2688 for (tail
= buf
->overlays_before
;
2690 prev
= tail
, tail
= next
)
2693 overlay
= XCAR (tail
);
2695 /* If the overlay is not valid, get rid of it. */
2696 if (!OVERLAY_VALID (overlay
))
2701 /* Splice the cons cell TAIL out of overlays_before. */
2705 buf
->overlays_before
= next
;
2711 beg
= OVERLAY_START (overlay
);
2712 end
= OVERLAY_END (overlay
);
2714 if (OVERLAY_POSITION (end
) > pos
)
2716 /* OVERLAY needs to be moved. */
2717 int where
= OVERLAY_POSITION (beg
);
2718 Lisp_Object other
, other_prev
;
2720 /* Splice the cons cell TAIL out of overlays_before. */
2724 buf
->overlays_before
= next
;
2726 /* Search thru overlays_after for where to put it. */
2728 for (other
= buf
->overlays_after
;
2730 other_prev
= other
, other
= XCDR (other
))
2732 Lisp_Object otherbeg
, otheroverlay
;
2734 otheroverlay
= XCAR (other
);
2735 if (! OVERLAY_VALID (otheroverlay
))
2738 otherbeg
= OVERLAY_START (otheroverlay
);
2739 if (OVERLAY_POSITION (otherbeg
) >= where
)
2743 /* Add TAIL to overlays_after before OTHER. */
2744 XCDR (tail
) = other
;
2745 if (!NILP (other_prev
))
2746 XCDR (other_prev
) = tail
;
2748 buf
->overlays_after
= tail
;
2752 /* We've reached the things that should stay in overlays_before.
2753 All the rest of overlays_before must end even earlier,
2758 /* See if anything in overlays_after should be in overlays_before. */
2760 for (tail
= buf
->overlays_after
;
2762 prev
= tail
, tail
= next
)
2765 overlay
= XCAR (tail
);
2767 /* If the overlay is not valid, get rid of it. */
2768 if (!OVERLAY_VALID (overlay
))
2773 /* Splice the cons cell TAIL out of overlays_after. */
2777 buf
->overlays_after
= next
;
2783 beg
= OVERLAY_START (overlay
);
2784 end
= OVERLAY_END (overlay
);
2786 /* Stop looking, when we know that nothing further
2787 can possibly end before POS. */
2788 if (OVERLAY_POSITION (beg
) > pos
)
2791 if (OVERLAY_POSITION (end
) <= pos
)
2793 /* OVERLAY needs to be moved. */
2794 int where
= OVERLAY_POSITION (end
);
2795 Lisp_Object other
, other_prev
;
2797 /* Splice the cons cell TAIL out of overlays_after. */
2801 buf
->overlays_after
= next
;
2803 /* Search thru overlays_before for where to put it. */
2805 for (other
= buf
->overlays_before
;
2807 other_prev
= other
, other
= XCDR (other
))
2809 Lisp_Object otherend
, otheroverlay
;
2811 otheroverlay
= XCAR (other
);
2812 if (! OVERLAY_VALID (otheroverlay
))
2815 otherend
= OVERLAY_END (otheroverlay
);
2816 if (OVERLAY_POSITION (otherend
) <= where
)
2820 /* Add TAIL to overlays_before before OTHER. */
2821 XCDR (tail
) = other
;
2822 if (!NILP (other_prev
))
2823 XCDR (other_prev
) = tail
;
2825 buf
->overlays_before
= tail
;
2830 XSETFASTINT (buf
->overlay_center
, pos
);
2834 adjust_overlays_for_insert (pos
, length
)
2838 /* After an insertion, the lists are still sorted properly,
2839 but we may need to update the value of the overlay center. */
2840 if (XFASTINT (current_buffer
->overlay_center
) >= pos
)
2841 XSETFASTINT (current_buffer
->overlay_center
,
2842 XFASTINT (current_buffer
->overlay_center
) + length
);
2846 adjust_overlays_for_delete (pos
, length
)
2850 if (XFASTINT (current_buffer
->overlay_center
) < pos
)
2851 /* The deletion was to our right. No change needed; the before- and
2852 after-lists are still consistent. */
2854 else if (XFASTINT (current_buffer
->overlay_center
) > pos
+ length
)
2855 /* The deletion was to our left. We need to adjust the center value
2856 to account for the change in position, but the lists are consistent
2857 given the new value. */
2858 XSETFASTINT (current_buffer
->overlay_center
,
2859 XFASTINT (current_buffer
->overlay_center
) - length
);
2861 /* We're right in the middle. There might be things on the after-list
2862 that now belong on the before-list. Recentering will move them,
2863 and also update the center point. */
2864 recenter_overlay_lists (current_buffer
, pos
);
2867 /* Fix up overlays that were garbled as a result of permuting markers
2868 in the range START through END. Any overlay with at least one
2869 endpoint in this range will need to be unlinked from the overlay
2870 list and reinserted in its proper place.
2871 Such an overlay might even have negative size at this point.
2872 If so, we'll reverse the endpoints. Can you think of anything
2873 better to do in this situation? */
2875 fix_overlays_in_range (start
, end
)
2876 register int start
, end
;
2878 Lisp_Object overlay
;
2879 Lisp_Object before_list
, after_list
;
2880 Lisp_Object
*ptail
, *pbefore
= &before_list
, *pafter
= &after_list
;
2881 int startpos
, endpos
;
2883 /* This algorithm shifts links around instead of consing and GCing.
2884 The loop invariant is that before_list (resp. after_list) is a
2885 well-formed list except that its last element, the one that
2886 *pbefore (resp. *pafter) points to, is still uninitialized.
2887 So it's not a bug that before_list isn't initialized, although
2888 it may look strange. */
2889 for (ptail
= ¤t_buffer
->overlays_before
; CONSP (*ptail
);)
2891 overlay
= XCAR (*ptail
);
2892 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2895 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2897 || (startpos
>= start
&& startpos
< end
))
2899 /* If the overlay is backwards, fix that now. */
2900 if (startpos
> endpos
)
2903 Fset_marker (OVERLAY_START (overlay
), make_number (endpos
),
2905 Fset_marker (OVERLAY_END (overlay
), make_number (startpos
),
2907 tem
= startpos
; startpos
= endpos
; endpos
= tem
;
2909 /* Add it to the end of the wrong list. Later on,
2910 recenter_overlay_lists will move it to the right place. */
2911 if (endpos
< XINT (current_buffer
->overlay_center
))
2914 pafter
= &XCDR (*ptail
);
2919 pbefore
= &XCDR (*ptail
);
2921 *ptail
= XCDR (*ptail
);
2924 ptail
= &XCDR (*ptail
);
2926 for (ptail
= ¤t_buffer
->overlays_after
; CONSP (*ptail
);)
2928 overlay
= XCAR (*ptail
);
2929 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
2930 if (startpos
>= end
)
2932 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
2933 if (startpos
>= start
2934 || (endpos
>= start
&& endpos
< end
))
2936 if (startpos
> endpos
)
2939 Fset_marker (OVERLAY_START (overlay
), make_number (endpos
),
2941 Fset_marker (OVERLAY_END (overlay
), make_number (startpos
),
2943 tem
= startpos
; startpos
= endpos
; endpos
= tem
;
2945 if (endpos
< XINT (current_buffer
->overlay_center
))
2948 pafter
= &XCDR (*ptail
);
2953 pbefore
= &XCDR (*ptail
);
2955 *ptail
= XCDR (*ptail
);
2958 ptail
= &XCDR (*ptail
);
2961 /* Splice the constructed (wrong) lists into the buffer's lists,
2962 and let the recenter function make it sane again. */
2963 *pbefore
= current_buffer
->overlays_before
;
2964 current_buffer
->overlays_before
= before_list
;
2965 recenter_overlay_lists (current_buffer
,
2966 XINT (current_buffer
->overlay_center
));
2968 *pafter
= current_buffer
->overlays_after
;
2969 current_buffer
->overlays_after
= after_list
;
2970 recenter_overlay_lists (current_buffer
,
2971 XINT (current_buffer
->overlay_center
));
2974 /* We have two types of overlay: the one whose ending marker is
2975 after-insertion-marker (this is the usual case) and the one whose
2976 ending marker is before-insertion-marker. When `overlays_before'
2977 contains overlays of the latter type and the former type in this
2978 order and both overlays end at inserting position, inserting a text
2979 increases only the ending marker of the latter type, which results
2980 in incorrect ordering of `overlays_before'.
2982 This function fixes ordering of overlays in the slot
2983 `overlays_before' of the buffer *BP. Before the insertion, `point'
2984 was at PREV, and now is at POS. */
2987 fix_overlays_before (bp
, prev
, pos
)
2991 Lisp_Object
*tailp
= &bp
->overlays_before
;
2992 Lisp_Object
*right_place
;
2995 /* After the insertion, the several overlays may be in incorrect
2996 order. The possibility is that, in the list `overlays_before',
2997 an overlay which ends at POS appears after an overlay which ends
2998 at PREV. Since POS is greater than PREV, we must fix the
2999 ordering of these overlays, by moving overlays ends at POS before
3000 the overlays ends at PREV. */
3002 /* At first, find a place where disordered overlays should be linked
3003 in. It is where an overlay which end before POS exists. (i.e. an
3004 overlay whose ending marker is after-insertion-marker if disorder
3006 while (!NILP (*tailp
)
3007 && ((end
= OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp
))))
3009 tailp
= &XCDR (*tailp
);
3011 /* If we don't find such an overlay,
3012 or the found one ends before PREV,
3013 or the found one is the last one in the list,
3014 we don't have to fix anything. */
3017 || NILP (XCDR (*tailp
)))
3020 right_place
= tailp
;
3021 tailp
= &XCDR (*tailp
);
3023 /* Now, end position of overlays in the list *TAILP should be before
3024 or equal to PREV. In the loop, an overlay which ends at POS is
3025 moved ahead to the place pointed by RIGHT_PLACE. If we found an
3026 overlay which ends before PREV, the remaining overlays are in
3028 while (!NILP (*tailp
))
3030 end
= OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp
)));
3033 { /* This overlay is disordered. */
3034 Lisp_Object found
= *tailp
;
3036 /* Unlink the found overlay. */
3037 *tailp
= XCDR (found
);
3038 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
3039 XCDR (found
) = *right_place
;
3040 /* Link it into the right place. */
3041 *right_place
= found
;
3043 else if (end
== prev
)
3044 tailp
= &XCDR (*tailp
);
3045 else /* No more disordered overlay. */
3050 DEFUN ("overlayp", Foverlayp
, Soverlayp
, 1, 1, 0,
3051 "Return t if OBJECT is an overlay.")
3055 return (OVERLAYP (object
) ? Qt
: Qnil
);
3058 DEFUN ("make-overlay", Fmake_overlay
, Smake_overlay
, 2, 5, 0,
3059 "Create a new overlay with range BEG to END in BUFFER.\n\
3060 If omitted, BUFFER defaults to the current buffer.\n\
3061 BEG and END may be integers or markers.\n\
3062 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
3063 front delimiter advance when text is inserted there.\n\
3064 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
3065 rear delimiter advance when text is inserted there.")
3066 (beg
, end
, buffer
, front_advance
, rear_advance
)
3067 Lisp_Object beg
, end
, buffer
;
3068 Lisp_Object front_advance
, rear_advance
;
3070 Lisp_Object overlay
;
3074 XSETBUFFER (buffer
, current_buffer
);
3076 CHECK_BUFFER (buffer
, 2);
3078 && ! EQ (Fmarker_buffer (beg
), buffer
))
3079 error ("Marker points into wrong buffer");
3081 && ! EQ (Fmarker_buffer (end
), buffer
))
3082 error ("Marker points into wrong buffer");
3084 CHECK_NUMBER_COERCE_MARKER (beg
, 1);
3085 CHECK_NUMBER_COERCE_MARKER (end
, 1);
3087 if (XINT (beg
) > XINT (end
))
3090 temp
= beg
; beg
= end
; end
= temp
;
3093 b
= XBUFFER (buffer
);
3095 beg
= Fset_marker (Fmake_marker (), beg
, buffer
);
3096 end
= Fset_marker (Fmake_marker (), end
, buffer
);
3098 if (!NILP (front_advance
))
3099 XMARKER (beg
)->insertion_type
= 1;
3100 if (!NILP (rear_advance
))
3101 XMARKER (end
)->insertion_type
= 1;
3103 overlay
= allocate_misc ();
3104 XMISCTYPE (overlay
) = Lisp_Misc_Overlay
;
3105 XOVERLAY (overlay
)->start
= beg
;
3106 XOVERLAY (overlay
)->end
= end
;
3107 XOVERLAY (overlay
)->plist
= Qnil
;
3109 /* Put the new overlay on the wrong list. */
3110 end
= OVERLAY_END (overlay
);
3111 if (OVERLAY_POSITION (end
) < XINT (b
->overlay_center
))
3112 b
->overlays_after
= Fcons (overlay
, b
->overlays_after
);
3114 b
->overlays_before
= Fcons (overlay
, b
->overlays_before
);
3116 /* This puts it in the right list, and in the right order. */
3117 recenter_overlay_lists (b
, XINT (b
->overlay_center
));
3119 /* We don't need to redisplay the region covered by the overlay, because
3120 the overlay has no properties at the moment. */
3125 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3128 modify_overlay (buf
, start
, end
)
3138 start
= end
; end
= temp
;
3141 BUF_COMPUTE_UNCHANGED (buf
, start
, end
);
3143 /* If this is a buffer not in the selected window,
3144 we must do other windows. */
3145 if (buf
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
3146 windows_or_buffers_changed
= 1;
3147 /* If multiple windows show this buffer, we must do other windows. */
3148 else if (buffer_shared
> 1)
3149 windows_or_buffers_changed
= 1;
3151 ++BUF_OVERLAY_MODIFF (buf
);
3155 Lisp_Object
Fdelete_overlay ();
3157 DEFUN ("move-overlay", Fmove_overlay
, Smove_overlay
, 3, 4, 0,
3158 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3159 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3160 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3162 (overlay
, beg
, end
, buffer
)
3163 Lisp_Object overlay
, beg
, end
, buffer
;
3165 struct buffer
*b
, *ob
;
3166 Lisp_Object obuffer
;
3167 int count
= specpdl_ptr
- specpdl
;
3169 CHECK_OVERLAY (overlay
, 0);
3171 buffer
= Fmarker_buffer (OVERLAY_START (overlay
));
3173 XSETBUFFER (buffer
, current_buffer
);
3174 CHECK_BUFFER (buffer
, 3);
3177 && ! EQ (Fmarker_buffer (beg
), buffer
))
3178 error ("Marker points into wrong buffer");
3180 && ! EQ (Fmarker_buffer (end
), buffer
))
3181 error ("Marker points into wrong buffer");
3183 CHECK_NUMBER_COERCE_MARKER (beg
, 1);
3184 CHECK_NUMBER_COERCE_MARKER (end
, 1);
3186 if (XINT (beg
) == XINT (end
) && ! NILP (Foverlay_get (overlay
, Qevaporate
)))
3187 return Fdelete_overlay (overlay
);
3189 if (XINT (beg
) > XINT (end
))
3192 temp
= beg
; beg
= end
; end
= temp
;
3195 specbind (Qinhibit_quit
, Qt
);
3197 obuffer
= Fmarker_buffer (OVERLAY_START (overlay
));
3198 b
= XBUFFER (buffer
);
3199 ob
= BUFFERP (obuffer
) ? XBUFFER (obuffer
) : (struct buffer
*) 0;
3201 /* If the overlay has changed buffers, do a thorough redisplay. */
3202 if (!EQ (buffer
, obuffer
))
3204 /* Redisplay where the overlay was. */
3205 if (!NILP (obuffer
))
3210 o_beg
= OVERLAY_POSITION (OVERLAY_START (overlay
));
3211 o_end
= OVERLAY_POSITION (OVERLAY_END (overlay
));
3213 modify_overlay (ob
, o_beg
, o_end
);
3216 /* Redisplay where the overlay is going to be. */
3217 modify_overlay (b
, XINT (beg
), XINT (end
));
3220 /* Redisplay the area the overlay has just left, or just enclosed. */
3224 o_beg
= OVERLAY_POSITION (OVERLAY_START (overlay
));
3225 o_end
= OVERLAY_POSITION (OVERLAY_END (overlay
));
3227 if (o_beg
== XINT (beg
))
3228 modify_overlay (b
, o_end
, XINT (end
));
3229 else if (o_end
== XINT (end
))
3230 modify_overlay (b
, o_beg
, XINT (beg
));
3233 if (XINT (beg
) < o_beg
) o_beg
= XINT (beg
);
3234 if (XINT (end
) > o_end
) o_end
= XINT (end
);
3235 modify_overlay (b
, o_beg
, o_end
);
3239 if (!NILP (obuffer
))
3241 ob
->overlays_before
= Fdelq (overlay
, ob
->overlays_before
);
3242 ob
->overlays_after
= Fdelq (overlay
, ob
->overlays_after
);
3245 Fset_marker (OVERLAY_START (overlay
), beg
, buffer
);
3246 Fset_marker (OVERLAY_END (overlay
), end
, buffer
);
3248 /* Put the overlay on the wrong list. */
3249 end
= OVERLAY_END (overlay
);
3250 if (OVERLAY_POSITION (end
) < XINT (b
->overlay_center
))
3251 b
->overlays_after
= Fcons (overlay
, b
->overlays_after
);
3253 b
->overlays_before
= Fcons (overlay
, b
->overlays_before
);
3255 /* This puts it in the right list, and in the right order. */
3256 recenter_overlay_lists (b
, XINT (b
->overlay_center
));
3258 return unbind_to (count
, overlay
);
3261 DEFUN ("delete-overlay", Fdelete_overlay
, Sdelete_overlay
, 1, 1, 0,
3262 "Delete the overlay OVERLAY from its buffer.")
3264 Lisp_Object overlay
;
3268 int count
= specpdl_ptr
- specpdl
;
3270 CHECK_OVERLAY (overlay
, 0);
3272 buffer
= Fmarker_buffer (OVERLAY_START (overlay
));
3276 b
= XBUFFER (buffer
);
3278 specbind (Qinhibit_quit
, Qt
);
3280 b
->overlays_before
= Fdelq (overlay
, b
->overlays_before
);
3281 b
->overlays_after
= Fdelq (overlay
, b
->overlays_after
);
3284 marker_position (OVERLAY_START (overlay
)),
3285 marker_position (OVERLAY_END (overlay
)));
3287 Fset_marker (OVERLAY_START (overlay
), Qnil
, Qnil
);
3288 Fset_marker (OVERLAY_END (overlay
), Qnil
, Qnil
);
3290 return unbind_to (count
, Qnil
);
3293 /* Overlay dissection functions. */
3295 DEFUN ("overlay-start", Foverlay_start
, Soverlay_start
, 1, 1, 0,
3296 "Return the position at which OVERLAY starts.")
3298 Lisp_Object overlay
;
3300 CHECK_OVERLAY (overlay
, 0);
3302 return (Fmarker_position (OVERLAY_START (overlay
)));
3305 DEFUN ("overlay-end", Foverlay_end
, Soverlay_end
, 1, 1, 0,
3306 "Return the position at which OVERLAY ends.")
3308 Lisp_Object overlay
;
3310 CHECK_OVERLAY (overlay
, 0);
3312 return (Fmarker_position (OVERLAY_END (overlay
)));
3315 DEFUN ("overlay-buffer", Foverlay_buffer
, Soverlay_buffer
, 1, 1, 0,
3316 "Return the buffer OVERLAY belongs to.")
3318 Lisp_Object overlay
;
3320 CHECK_OVERLAY (overlay
, 0);
3322 return Fmarker_buffer (OVERLAY_START (overlay
));
3325 DEFUN ("overlay-properties", Foverlay_properties
, Soverlay_properties
, 1, 1, 0,
3326 "Return a list of the properties on OVERLAY.\n\
3327 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3330 Lisp_Object overlay
;
3332 CHECK_OVERLAY (overlay
, 0);
3334 return Fcopy_sequence (XOVERLAY (overlay
)->plist
);
3338 DEFUN ("overlays-at", Foverlays_at
, Soverlays_at
, 1, 1, 0,
3339 "Return a list of the overlays that contain position POS.")
3344 Lisp_Object
*overlay_vec
;
3348 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
3351 overlay_vec
= (Lisp_Object
*) xmalloc (len
* sizeof (Lisp_Object
));
3353 /* Put all the overlays we want in a vector in overlay_vec.
3354 Store the length in len. */
3355 noverlays
= overlays_at (XINT (pos
), 1, &overlay_vec
, &len
,
3356 (int *) 0, (int *) 0);
3358 /* Make a list of them all. */
3359 result
= Flist (noverlays
, overlay_vec
);
3361 xfree (overlay_vec
);
3365 DEFUN ("overlays-in", Foverlays_in
, Soverlays_in
, 2, 2, 0,
3366 "Return a list of the overlays that overlap the region BEG ... END.\n\
3367 Overlap means that at least one character is contained within the overlay\n\
3368 and also contained within the specified region.\n\
3369 Empty overlays are included in the result if they are located at BEG\n\
3370 or between BEG and END.")
3372 Lisp_Object beg
, end
;
3375 Lisp_Object
*overlay_vec
;
3379 CHECK_NUMBER_COERCE_MARKER (beg
, 0);
3380 CHECK_NUMBER_COERCE_MARKER (end
, 0);
3383 overlay_vec
= (Lisp_Object
*) xmalloc (len
* sizeof (Lisp_Object
));
3385 /* Put all the overlays we want in a vector in overlay_vec.
3386 Store the length in len. */
3387 noverlays
= overlays_in (XINT (beg
), XINT (end
), 1, &overlay_vec
, &len
,
3388 (int *) 0, (int *) 0);
3390 /* Make a list of them all. */
3391 result
= Flist (noverlays
, overlay_vec
);
3393 xfree (overlay_vec
);
3397 DEFUN ("next-overlay-change", Fnext_overlay_change
, Snext_overlay_change
,
3399 "Return the next position after POS where an overlay starts or ends.\n\
3400 If there are no more overlay boundaries after POS, return (point-max).")
3406 Lisp_Object
*overlay_vec
;
3410 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
3413 overlay_vec
= (Lisp_Object
*) xmalloc (len
* sizeof (Lisp_Object
));
3415 /* Put all the overlays we want in a vector in overlay_vec.
3416 Store the length in len.
3417 endpos gets the position where the next overlay starts. */
3418 noverlays
= overlays_at (XINT (pos
), 1, &overlay_vec
, &len
,
3419 &endpos
, (int *) 0);
3421 /* If any of these overlays ends before endpos,
3422 use its ending point instead. */
3423 for (i
= 0; i
< noverlays
; i
++)
3428 oend
= OVERLAY_END (overlay_vec
[i
]);
3429 oendpos
= OVERLAY_POSITION (oend
);
3430 if (oendpos
< endpos
)
3434 xfree (overlay_vec
);
3435 return make_number (endpos
);
3438 DEFUN ("previous-overlay-change", Fprevious_overlay_change
,
3439 Sprevious_overlay_change
, 1, 1, 0,
3440 "Return the previous position before POS where an overlay starts or ends.\n\
3441 If there are no more overlay boundaries before POS, return (point-min).")
3447 Lisp_Object
*overlay_vec
;
3450 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
3453 overlay_vec
= (Lisp_Object
*) xmalloc (len
* sizeof (Lisp_Object
));
3455 /* At beginning of buffer, we know the answer;
3456 avoid bug subtracting 1 below. */
3457 if (XINT (pos
) == BEGV
)
3460 /* Put all the overlays we want in a vector in overlay_vec.
3461 Store the length in len.
3462 prevpos gets the position of the previous change. */
3463 noverlays
= overlays_at (XINT (pos
), 1, &overlay_vec
, &len
,
3464 (int *) 0, &prevpos
);
3466 xfree (overlay_vec
);
3467 return make_number (prevpos
);
3470 /* These functions are for debugging overlays. */
3472 DEFUN ("overlay-lists", Foverlay_lists
, Soverlay_lists
, 0, 0, 0,
3473 "Return a pair of lists giving all the overlays of the current buffer.\n\
3474 The car has all the overlays before the overlay center;\n\
3475 the cdr has all the overlays after the overlay center.\n\
3476 Recentering overlays moves overlays between these lists.\n\
3477 The lists you get are copies, so that changing them has no effect.\n\
3478 However, the overlays you get are the real objects that the buffer uses.")
3481 Lisp_Object before
, after
;
3482 before
= current_buffer
->overlays_before
;
3484 before
= Fcopy_sequence (before
);
3485 after
= current_buffer
->overlays_after
;
3487 after
= Fcopy_sequence (after
);
3489 return Fcons (before
, after
);
3492 DEFUN ("overlay-recenter", Foverlay_recenter
, Soverlay_recenter
, 1, 1, 0,
3493 "Recenter the overlays of the current buffer around position POS.")
3497 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
3499 recenter_overlay_lists (current_buffer
, XINT (pos
));
3503 DEFUN ("overlay-get", Foverlay_get
, Soverlay_get
, 2, 2, 0,
3504 "Get the property of overlay OVERLAY with property name PROP.")
3506 Lisp_Object overlay
, prop
;
3508 Lisp_Object plist
, fallback
;
3510 CHECK_OVERLAY (overlay
, 0);
3514 for (plist
= XOVERLAY (overlay
)->plist
;
3515 CONSP (plist
) && CONSP (XCDR (plist
));
3516 plist
= XCDR (XCDR (plist
)))
3518 if (EQ (XCAR (plist
), prop
))
3519 return XCAR (XCDR (plist
));
3520 else if (EQ (XCAR (plist
), Qcategory
))
3523 tem
= Fcar (Fcdr (plist
));
3525 fallback
= Fget (tem
, prop
);
3532 DEFUN ("overlay-put", Foverlay_put
, Soverlay_put
, 3, 3, 0,
3533 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3534 (overlay
, prop
, value
)
3535 Lisp_Object overlay
, prop
, value
;
3537 Lisp_Object tail
, buffer
;
3540 CHECK_OVERLAY (overlay
, 0);
3542 buffer
= Fmarker_buffer (OVERLAY_START (overlay
));
3544 for (tail
= XOVERLAY (overlay
)->plist
;
3545 CONSP (tail
) && CONSP (XCDR (tail
));
3546 tail
= XCDR (XCDR (tail
)))
3547 if (EQ (XCAR (tail
), prop
))
3549 changed
= !EQ (XCAR (XCDR (tail
)), value
);
3550 XCAR (XCDR (tail
)) = value
;
3553 /* It wasn't in the list, so add it to the front. */
3554 changed
= !NILP (value
);
3555 XOVERLAY (overlay
)->plist
3556 = Fcons (prop
, Fcons (value
, XOVERLAY (overlay
)->plist
));
3558 if (! NILP (buffer
))
3561 modify_overlay (XBUFFER (buffer
),
3562 marker_position (OVERLAY_START (overlay
)),
3563 marker_position (OVERLAY_END (overlay
)));
3564 if (EQ (prop
, Qevaporate
) && ! NILP (value
)
3565 && (OVERLAY_POSITION (OVERLAY_START (overlay
))
3566 == OVERLAY_POSITION (OVERLAY_END (overlay
))))
3567 Fdelete_overlay (overlay
);
3572 /* Subroutine of report_overlay_modification. */
3574 /* Lisp vector holding overlay hook functions to call.
3575 Vector elements come in pairs.
3576 Each even-index element is a list of hook functions.
3577 The following odd-index element is the overlay they came from.
3579 Before the buffer change, we fill in this vector
3580 as we call overlay hook functions.
3581 After the buffer change, we get the functions to call from this vector.
3582 This way we always call the same functions before and after the change. */
3583 static Lisp_Object last_overlay_modification_hooks
;
3585 /* Number of elements actually used in last_overlay_modification_hooks. */
3586 static int last_overlay_modification_hooks_used
;
3588 /* Add one functionlist/overlay pair
3589 to the end of last_overlay_modification_hooks. */
3592 add_overlay_mod_hooklist (functionlist
, overlay
)
3593 Lisp_Object functionlist
, overlay
;
3595 int oldsize
= XVECTOR (last_overlay_modification_hooks
)->size
;
3597 if (last_overlay_modification_hooks_used
== oldsize
)
3600 old
= last_overlay_modification_hooks
;
3601 last_overlay_modification_hooks
3602 = Fmake_vector (make_number (oldsize
* 2), Qnil
);
3603 bcopy (XVECTOR (old
)->contents
,
3604 XVECTOR (last_overlay_modification_hooks
)->contents
,
3605 sizeof (Lisp_Object
) * oldsize
);
3607 XVECTOR (last_overlay_modification_hooks
)->contents
[last_overlay_modification_hooks_used
++] = functionlist
;
3608 XVECTOR (last_overlay_modification_hooks
)->contents
[last_overlay_modification_hooks_used
++] = overlay
;
3611 /* Run the modification-hooks of overlays that include
3612 any part of the text in START to END.
3613 If this change is an insertion, also
3614 run the insert-before-hooks of overlay starting at END,
3615 and the insert-after-hooks of overlay ending at START.
3617 This is called both before and after the modification.
3618 AFTER is nonzero when we call after the modification.
3620 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3621 When AFTER is nonzero, they are the start position,
3622 the position after the inserted new text,
3623 and the length of deleted or replaced old text. */
3626 report_overlay_modification (start
, end
, after
, arg1
, arg2
, arg3
)
3627 Lisp_Object start
, end
;
3629 Lisp_Object arg1
, arg2
, arg3
;
3631 Lisp_Object prop
, overlay
, tail
;
3632 /* 1 if this change is an insertion. */
3633 int insertion
= (after
? XFASTINT (arg3
) == 0 : EQ (start
, end
));
3635 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
3639 GCPRO5 (overlay
, tail
, arg1
, arg2
, arg3
);
3643 /* Call the functions recorded in last_overlay_modification_hooks
3644 rather than scanning the overlays again.
3645 First copy the vector contents, in case some of these hooks
3646 do subsequent modification of the buffer. */
3647 int size
= last_overlay_modification_hooks_used
;
3648 Lisp_Object
*copy
= (Lisp_Object
*) alloca (size
* sizeof (Lisp_Object
));
3651 bcopy (XVECTOR (last_overlay_modification_hooks
)->contents
,
3652 copy
, size
* sizeof (Lisp_Object
));
3654 gcpro1
.nvars
= size
;
3656 for (i
= 0; i
< size
;)
3658 Lisp_Object prop
, overlay
;
3660 overlay
= copy
[i
++];
3661 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3667 /* We are being called before a change.
3668 Scan the overlays to find the functions to call. */
3669 last_overlay_modification_hooks_used
= 0;
3671 for (tail
= current_buffer
->overlays_before
;
3675 int startpos
, endpos
;
3676 Lisp_Object ostart
, oend
;
3678 overlay
= XCAR (tail
);
3680 ostart
= OVERLAY_START (overlay
);
3681 oend
= OVERLAY_END (overlay
);
3682 endpos
= OVERLAY_POSITION (oend
);
3683 if (XFASTINT (start
) > endpos
)
3685 startpos
= OVERLAY_POSITION (ostart
);
3686 if (insertion
&& (XFASTINT (start
) == startpos
3687 || XFASTINT (end
) == startpos
))
3689 prop
= Foverlay_get (overlay
, Qinsert_in_front_hooks
);
3692 /* Copy TAIL in case the hook recenters the overlay lists. */
3694 tail
= Fcopy_sequence (tail
);
3696 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3699 if (insertion
&& (XFASTINT (start
) == endpos
3700 || XFASTINT (end
) == endpos
))
3702 prop
= Foverlay_get (overlay
, Qinsert_behind_hooks
);
3706 tail
= Fcopy_sequence (tail
);
3708 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3711 /* Test for intersecting intervals. This does the right thing
3712 for both insertion and deletion. */
3713 if (XFASTINT (end
) > startpos
&& XFASTINT (start
) < endpos
)
3715 prop
= Foverlay_get (overlay
, Qmodification_hooks
);
3719 tail
= Fcopy_sequence (tail
);
3721 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3727 for (tail
= current_buffer
->overlays_after
;
3731 int startpos
, endpos
;
3732 Lisp_Object ostart
, oend
;
3734 overlay
= XCAR (tail
);
3736 ostart
= OVERLAY_START (overlay
);
3737 oend
= OVERLAY_END (overlay
);
3738 startpos
= OVERLAY_POSITION (ostart
);
3739 endpos
= OVERLAY_POSITION (oend
);
3740 if (XFASTINT (end
) < startpos
)
3742 if (insertion
&& (XFASTINT (start
) == startpos
3743 || XFASTINT (end
) == startpos
))
3745 prop
= Foverlay_get (overlay
, Qinsert_in_front_hooks
);
3749 tail
= Fcopy_sequence (tail
);
3751 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3754 if (insertion
&& (XFASTINT (start
) == endpos
3755 || XFASTINT (end
) == endpos
))
3757 prop
= Foverlay_get (overlay
, Qinsert_behind_hooks
);
3761 tail
= Fcopy_sequence (tail
);
3763 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3766 /* Test for intersecting intervals. This does the right thing
3767 for both insertion and deletion. */
3768 if (XFASTINT (end
) > startpos
&& XFASTINT (start
) < endpos
)
3770 prop
= Foverlay_get (overlay
, Qmodification_hooks
);
3774 tail
= Fcopy_sequence (tail
);
3776 call_overlay_mod_hooks (prop
, overlay
, after
, arg1
, arg2
, arg3
);
3785 call_overlay_mod_hooks (list
, overlay
, after
, arg1
, arg2
, arg3
)
3786 Lisp_Object list
, overlay
;
3788 Lisp_Object arg1
, arg2
, arg3
;
3790 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
3792 GCPRO4 (list
, arg1
, arg2
, arg3
);
3794 add_overlay_mod_hooklist (list
, overlay
);
3796 while (!NILP (list
))
3799 call4 (Fcar (list
), overlay
, after
? Qt
: Qnil
, arg1
, arg2
);
3801 call5 (Fcar (list
), overlay
, after
? Qt
: Qnil
, arg1
, arg2
, arg3
);
3807 /* Delete any zero-sized overlays at position POS, if the `evaporate'
3810 evaporate_overlays (pos
)
3813 Lisp_Object tail
, overlay
, hit_list
;
3816 if (pos
<= XFASTINT (current_buffer
->overlay_center
))
3817 for (tail
= current_buffer
->overlays_before
; CONSP (tail
);
3821 overlay
= XCAR (tail
);
3822 endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
3825 if (endpos
== pos
&& OVERLAY_POSITION (OVERLAY_START (overlay
)) == pos
3826 && ! NILP (Foverlay_get (overlay
, Qevaporate
)))
3827 hit_list
= Fcons (overlay
, hit_list
);
3830 for (tail
= current_buffer
->overlays_after
; CONSP (tail
);
3834 overlay
= XCAR (tail
);
3835 startpos
= OVERLAY_POSITION (OVERLAY_START (overlay
));
3838 if (startpos
== pos
&& OVERLAY_POSITION (OVERLAY_END (overlay
)) == pos
3839 && ! NILP (Foverlay_get (overlay
, Qevaporate
)))
3840 hit_list
= Fcons (overlay
, hit_list
);
3842 for (; CONSP (hit_list
); hit_list
= XCDR (hit_list
))
3843 Fdelete_overlay (XCAR (hit_list
));
3846 /* Somebody has tried to store a value with an unacceptable type
3847 in the slot with offset OFFSET. */
3850 buffer_slot_type_mismatch (offset
)
3856 switch (XINT (PER_BUFFER_TYPE (offset
)))
3859 type_name
= "integers";
3863 type_name
= "strings";
3867 type_name
= "symbols";
3874 sym
= PER_BUFFER_SYMBOL (offset
);
3875 error ("Only %s should be stored in the buffer-local variable %s",
3876 type_name
, XSYMBOL (sym
)->name
->data
);
3885 bzero (buffer_permanent_local_flags
, sizeof buffer_permanent_local_flags
);
3887 /* Make sure all markable slots in buffer_defaults
3888 are initialized reasonably, so mark_buffer won't choke. */
3889 reset_buffer (&buffer_defaults
);
3890 reset_buffer_local_variables (&buffer_defaults
, 1);
3891 reset_buffer (&buffer_local_symbols
);
3892 reset_buffer_local_variables (&buffer_local_symbols
, 1);
3893 /* Prevent GC from getting confused. */
3894 buffer_defaults
.text
= &buffer_defaults
.own_text
;
3895 buffer_local_symbols
.text
= &buffer_local_symbols
.own_text
;
3896 BUF_INTERVALS (&buffer_defaults
) = 0;
3897 BUF_INTERVALS (&buffer_local_symbols
) = 0;
3898 XSETBUFFER (Vbuffer_defaults
, &buffer_defaults
);
3899 XSETBUFFER (Vbuffer_local_symbols
, &buffer_local_symbols
);
3901 /* Set up the default values of various buffer slots. */
3902 /* Must do these before making the first buffer! */
3904 /* real setup is done in loaddefs.el */
3905 buffer_defaults
.mode_line_format
= build_string ("%-");
3906 buffer_defaults
.header_line_format
= Qnil
;
3907 buffer_defaults
.abbrev_mode
= Qnil
;
3908 buffer_defaults
.overwrite_mode
= Qnil
;
3909 buffer_defaults
.case_fold_search
= Qt
;
3910 buffer_defaults
.auto_fill_function
= Qnil
;
3911 buffer_defaults
.selective_display
= Qnil
;
3913 buffer_defaults
.selective_display_ellipses
= Qt
;
3915 buffer_defaults
.abbrev_table
= Qnil
;
3916 buffer_defaults
.display_table
= Qnil
;
3917 buffer_defaults
.undo_list
= Qnil
;
3918 buffer_defaults
.mark_active
= Qnil
;
3919 buffer_defaults
.file_format
= Qnil
;
3920 buffer_defaults
.overlays_before
= Qnil
;
3921 buffer_defaults
.overlays_after
= Qnil
;
3922 XSETFASTINT (buffer_defaults
.overlay_center
, BEG
);
3924 XSETFASTINT (buffer_defaults
.tab_width
, 8);
3925 buffer_defaults
.truncate_lines
= Qnil
;
3926 buffer_defaults
.ctl_arrow
= Qt
;
3927 buffer_defaults
.direction_reversed
= Qnil
;
3928 buffer_defaults
.cursor_type
= Qt
;
3931 buffer_defaults
.buffer_file_type
= Qnil
; /* TEXT */
3933 buffer_defaults
.enable_multibyte_characters
= Qt
;
3934 buffer_defaults
.buffer_file_coding_system
= Qnil
;
3935 XSETFASTINT (buffer_defaults
.fill_column
, 70);
3936 XSETFASTINT (buffer_defaults
.left_margin
, 0);
3937 buffer_defaults
.cache_long_line_scans
= Qnil
;
3938 buffer_defaults
.file_truename
= Qnil
;
3939 XSETFASTINT (buffer_defaults
.display_count
, 0);
3940 buffer_defaults
.indicate_empty_lines
= Qnil
;
3941 buffer_defaults
.scroll_up_aggressively
= Qnil
;
3942 buffer_defaults
.scroll_down_aggressively
= Qnil
;
3943 buffer_defaults
.display_time
= Qnil
;
3945 /* Assign the local-flags to the slots that have default values.
3946 The local flag is a bit that is used in the buffer
3947 to say that it has its own local value for the slot.
3948 The local flag bits are in the local_var_flags slot of the buffer. */
3950 /* Nothing can work if this isn't true */
3951 if (sizeof (EMACS_INT
) != sizeof (Lisp_Object
)) abort ();
3953 /* 0 means not a lisp var, -1 means always local, else mask */
3954 bzero (&buffer_local_flags
, sizeof buffer_local_flags
);
3955 XSETINT (buffer_local_flags
.filename
, -1);
3956 XSETINT (buffer_local_flags
.directory
, -1);
3957 XSETINT (buffer_local_flags
.backed_up
, -1);
3958 XSETINT (buffer_local_flags
.save_length
, -1);
3959 XSETINT (buffer_local_flags
.auto_save_file_name
, -1);
3960 XSETINT (buffer_local_flags
.read_only
, -1);
3961 XSETINT (buffer_local_flags
.major_mode
, -1);
3962 XSETINT (buffer_local_flags
.mode_name
, -1);
3963 XSETINT (buffer_local_flags
.undo_list
, -1);
3964 XSETINT (buffer_local_flags
.mark_active
, -1);
3965 XSETINT (buffer_local_flags
.point_before_scroll
, -1);
3966 XSETINT (buffer_local_flags
.file_truename
, -1);
3967 XSETINT (buffer_local_flags
.invisibility_spec
, -1);
3968 XSETINT (buffer_local_flags
.file_format
, -1);
3969 XSETINT (buffer_local_flags
.display_count
, -1);
3970 XSETINT (buffer_local_flags
.display_time
, -1);
3971 XSETINT (buffer_local_flags
.enable_multibyte_characters
, -1);
3974 XSETFASTINT (buffer_local_flags
.mode_line_format
, idx
); ++idx
;
3975 XSETFASTINT (buffer_local_flags
.abbrev_mode
, idx
); ++idx
;
3976 XSETFASTINT (buffer_local_flags
.overwrite_mode
, idx
); ++idx
;
3977 XSETFASTINT (buffer_local_flags
.case_fold_search
, idx
); ++idx
;
3978 XSETFASTINT (buffer_local_flags
.auto_fill_function
, idx
); ++idx
;
3979 XSETFASTINT (buffer_local_flags
.selective_display
, idx
); ++idx
;
3981 XSETFASTINT (buffer_local_flags
.selective_display_ellipses
, idx
); ++idx
;
3983 XSETFASTINT (buffer_local_flags
.tab_width
, idx
); ++idx
;
3984 XSETFASTINT (buffer_local_flags
.truncate_lines
, idx
); ++idx
;
3985 XSETFASTINT (buffer_local_flags
.ctl_arrow
, idx
); ++idx
;
3986 XSETFASTINT (buffer_local_flags
.fill_column
, idx
); ++idx
;
3987 XSETFASTINT (buffer_local_flags
.left_margin
, idx
); ++idx
;
3988 XSETFASTINT (buffer_local_flags
.abbrev_table
, idx
); ++idx
;
3989 XSETFASTINT (buffer_local_flags
.display_table
, idx
); ++idx
;
3991 XSETFASTINT (buffer_local_flags
.buffer_file_type
, idx
);
3992 /* Make this one a permanent local. */
3993 buffer_permanent_local_flags
[idx
++] = 1;
3995 XSETFASTINT (buffer_local_flags
.syntax_table
, idx
); ++idx
;
3996 XSETFASTINT (buffer_local_flags
.cache_long_line_scans
, idx
); ++idx
;
3997 XSETFASTINT (buffer_local_flags
.category_table
, idx
); ++idx
;
3998 XSETFASTINT (buffer_local_flags
.direction_reversed
, idx
); ++idx
;
3999 XSETFASTINT (buffer_local_flags
.buffer_file_coding_system
, idx
);
4000 /* Make this one a permanent local. */
4001 buffer_permanent_local_flags
[idx
++] = 1;
4002 XSETFASTINT (buffer_local_flags
.left_margin_width
, idx
); ++idx
;
4003 XSETFASTINT (buffer_local_flags
.right_margin_width
, idx
); ++idx
;
4004 XSETFASTINT (buffer_local_flags
.indicate_empty_lines
, idx
); ++idx
;
4005 XSETFASTINT (buffer_local_flags
.scroll_up_aggressively
, idx
); ++idx
;
4006 XSETFASTINT (buffer_local_flags
.scroll_down_aggressively
, idx
); ++idx
;
4007 XSETFASTINT (buffer_local_flags
.header_line_format
, idx
); ++idx
;
4008 XSETFASTINT (buffer_local_flags
.cursor_type
, idx
); ++idx
;
4010 /* Need more room? */
4011 if (idx
>= MAX_PER_BUFFER_VARS
)
4013 last_per_buffer_idx
= idx
;
4015 Vbuffer_alist
= Qnil
;
4019 QSFundamental
= build_string ("Fundamental");
4021 Qfundamental_mode
= intern ("fundamental-mode");
4022 buffer_defaults
.major_mode
= Qfundamental_mode
;
4024 Qmode_class
= intern ("mode-class");
4026 Qprotected_field
= intern ("protected-field");
4028 Qpermanent_local
= intern ("permanent-local");
4030 Qkill_buffer_hook
= intern ("kill-buffer-hook");
4032 Vprin1_to_string_buffer
= Fget_buffer_create (build_string (" prin1"));
4034 /* super-magic invisible buffer */
4035 Vbuffer_alist
= Qnil
;
4037 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4039 inhibit_modification_hooks
= 0;
4045 char buf
[MAXPATHLEN
+1];
4047 struct stat dotstat
, pwdstat
;
4051 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4052 if (NILP (buffer_defaults
.enable_multibyte_characters
))
4053 Fset_buffer_multibyte (Qnil
);
4055 /* If PWD is accurate, use it instead of calling getwd. This is faster
4056 when PWD is right, and may avoid a fatal error. */
4057 if ((pwd
= getenv ("PWD")) != 0
4058 && (IS_DIRECTORY_SEP (*pwd
) || (*pwd
&& IS_DEVICE_SEP (pwd
[1])))
4059 && stat (pwd
, &pwdstat
) == 0
4060 && stat (".", &dotstat
) == 0
4061 && dotstat
.st_ino
== pwdstat
.st_ino
4062 && dotstat
.st_dev
== pwdstat
.st_dev
4063 && strlen (pwd
) < MAXPATHLEN
)
4066 else if (getcwd (buf
, MAXPATHLEN
+1) == 0)
4067 fatal ("`getcwd' failed: %s\n", strerror (errno
));
4069 else if (getwd (buf
) == 0)
4070 fatal ("`getwd' failed: %s\n", buf
);
4074 /* Maybe this should really use some standard subroutine
4075 whose definition is filename syntax dependent. */
4077 if (!(IS_DIRECTORY_SEP (buf
[rc
- 1])))
4079 buf
[rc
] = DIRECTORY_SEP
;
4082 #endif /* not VMS */
4084 current_buffer
->directory
= build_string (buf
);
4086 /* Add /: to the front of the name
4087 if it would otherwise be treated as magic. */
4088 temp
= Ffind_file_name_handler (current_buffer
->directory
, Qt
);
4090 /* If the default dir is just /, TEMP is non-nil
4091 because of the ange-ftp completion handler.
4092 However, it is not necessary to turn / into /:/.
4093 So avoid doing that. */
4094 && strcmp ("/", XSTRING (current_buffer
->directory
)->data
))
4095 current_buffer
->directory
4096 = concat2 (build_string ("/:"), current_buffer
->directory
);
4098 temp
= get_minibuffer (0);
4099 XBUFFER (temp
)->directory
= current_buffer
->directory
;
4102 /* initialize the buffer routines */
4106 staticpro (&last_overlay_modification_hooks
);
4107 last_overlay_modification_hooks
4108 = Fmake_vector (make_number (10), Qnil
);
4110 staticpro (&Vbuffer_defaults
);
4111 staticpro (&Vbuffer_local_symbols
);
4112 staticpro (&Qfundamental_mode
);
4113 staticpro (&Qmode_class
);
4114 staticpro (&QSFundamental
);
4115 staticpro (&Vbuffer_alist
);
4116 staticpro (&Qprotected_field
);
4117 staticpro (&Qpermanent_local
);
4118 staticpro (&Qkill_buffer_hook
);
4119 Qoverlayp
= intern ("overlayp");
4120 staticpro (&Qoverlayp
);
4121 Qevaporate
= intern ("evaporate");
4122 staticpro (&Qevaporate
);
4123 Qmodification_hooks
= intern ("modification-hooks");
4124 staticpro (&Qmodification_hooks
);
4125 Qinsert_in_front_hooks
= intern ("insert-in-front-hooks");
4126 staticpro (&Qinsert_in_front_hooks
);
4127 Qinsert_behind_hooks
= intern ("insert-behind-hooks");
4128 staticpro (&Qinsert_behind_hooks
);
4129 Qget_file_buffer
= intern ("get-file-buffer");
4130 staticpro (&Qget_file_buffer
);
4131 Qpriority
= intern ("priority");
4132 staticpro (&Qpriority
);
4133 Qwindow
= intern ("window");
4134 staticpro (&Qwindow
);
4135 Qbefore_string
= intern ("before-string");
4136 staticpro (&Qbefore_string
);
4137 Qafter_string
= intern ("after-string");
4138 staticpro (&Qafter_string
);
4139 Qfirst_change_hook
= intern ("first-change-hook");
4140 staticpro (&Qfirst_change_hook
);
4141 Qbefore_change_functions
= intern ("before-change-functions");
4142 staticpro (&Qbefore_change_functions
);
4143 Qafter_change_functions
= intern ("after-change-functions");
4144 staticpro (&Qafter_change_functions
);
4146 Fput (Qprotected_field
, Qerror_conditions
,
4147 Fcons (Qprotected_field
, Fcons (Qerror
, Qnil
)));
4148 Fput (Qprotected_field
, Qerror_message
,
4149 build_string ("Attempt to modify a protected field"));
4151 /* All these use DEFVAR_LISP_NOPRO because the slots in
4152 buffer_defaults will all be marked via Vbuffer_defaults. */
4154 DEFVAR_LISP_NOPRO ("default-mode-line-format",
4155 &buffer_defaults
.mode_line_format
,
4156 "Default value of `mode-line-format' for buffers that don't override it.\n\
4157 This is the same as (default-value 'mode-line-format).");
4159 DEFVAR_LISP_NOPRO ("default-header-line-format",
4160 &buffer_defaults
.header_line_format
,
4161 "Default value of `header-line-format' for buffers that don't override it.\n\
4162 This is the same as (default-value 'header-line-format).");
4164 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults
.cursor_type
,
4165 "Default value of `cursor-type' for buffers that don't override it.\n\
4166 This is the same as (default-value 'cursor-type).");
4168 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
4169 &buffer_defaults
.abbrev_mode
,
4170 "Default value of `abbrev-mode' for buffers that do not override it.\n\
4171 This is the same as (default-value 'abbrev-mode).");
4173 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
4174 &buffer_defaults
.ctl_arrow
,
4175 "Default value of `ctl-arrow' for buffers that do not override it.\n\
4176 This is the same as (default-value 'ctl-arrow).");
4178 DEFVAR_LISP_NOPRO ("default-direction-reversed",
4179 &buffer_defaults
.direction_reversed
,
4180 "Default value of `direction_reversed' for buffers that do not override it.\n\
4181 This is the same as (default-value 'direction-reversed).");
4183 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
4184 &buffer_defaults
.enable_multibyte_characters
,
4185 "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
4186 This is the same as (default-value 'enable-multibyte-characters).");
4188 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
4189 &buffer_defaults
.buffer_file_coding_system
,
4190 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
4191 This is the same as (default-value 'buffer-file-coding-system).");
4193 DEFVAR_LISP_NOPRO ("default-truncate-lines",
4194 &buffer_defaults
.truncate_lines
,
4195 "Default value of `truncate-lines' for buffers that do not override it.\n\
4196 This is the same as (default-value 'truncate-lines).");
4198 DEFVAR_LISP_NOPRO ("default-fill-column",
4199 &buffer_defaults
.fill_column
,
4200 "Default value of `fill-column' for buffers that do not override it.\n\
4201 This is the same as (default-value 'fill-column).");
4203 DEFVAR_LISP_NOPRO ("default-left-margin",
4204 &buffer_defaults
.left_margin
,
4205 "Default value of `left-margin' for buffers that do not override it.\n\
4206 This is the same as (default-value 'left-margin).");
4208 DEFVAR_LISP_NOPRO ("default-tab-width",
4209 &buffer_defaults
.tab_width
,
4210 "Default value of `tab-width' for buffers that do not override it.\n\
4211 This is the same as (default-value 'tab-width).");
4213 DEFVAR_LISP_NOPRO ("default-case-fold-search",
4214 &buffer_defaults
.case_fold_search
,
4215 "Default value of `case-fold-search' for buffers that don't override it.\n\
4216 This is the same as (default-value 'case-fold-search).");
4219 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
4220 &buffer_defaults
.buffer_file_type
,
4221 "Default file type for buffers that do not override it.\n\
4222 This is the same as (default-value 'buffer-file-type).\n\
4223 The file type is nil for text, t for binary.");
4226 DEFVAR_LISP_NOPRO ("default-left-margin-width",
4227 &buffer_defaults
.left_margin_width
,
4228 "Default value of `left-margin-width' for buffers that don't override it.\n\
4229 This is the same as (default-value 'left-margin-width).");
4231 DEFVAR_LISP_NOPRO ("default-right-margin-width",
4232 &buffer_defaults
.right_margin_width
,
4233 "Default value of `right_margin_width' for buffers that don't override it.\n\
4234 This is the same as (default-value 'right-margin-width).");
4236 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
4237 &buffer_defaults
.indicate_empty_lines
,
4238 "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
4239 This is the same as (default-value 'indicate-empty-lines).");
4241 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
4242 &buffer_defaults
.scroll_up_aggressively
,
4243 "Default value of `scroll-up-aggressively' for buffers that\n\
4244 don't override it. This is the same as (default-value\n\
4245 'scroll-up-aggressively).");
4247 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
4248 &buffer_defaults
.scroll_down_aggressively
,
4249 "Default value of `scroll-down-aggressively' for buffers that\n\
4250 don't override it. This is the same as (default-value\n\
4251 'scroll-down-aggressively).");
4253 DEFVAR_PER_BUFFER ("header-line-format",
4254 ¤t_buffer
->header_line_format
,
4256 "Analogous to `mode-line-format', but for a mode line displayed\n\
4257 at the top of windows.");
4259 DEFVAR_PER_BUFFER ("mode-line-format", ¤t_buffer
->mode_line_format
,
4262 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
4263 But make-docfile finds it!
4264 DEFVAR_PER_BUFFER ("mode-line-format", ¤t_buffer->mode_line_format,
4266 "Template for displaying mode line for current buffer.\n\
4267 Each buffer has its own value of this variable.\n\
4268 Value may be nil, a string, a symbol or a list or cons cell.\n\
4269 A value of nil means don't display a mode line.\n\
4270 For a symbol, its value is used (but it is ignored if t or nil).\n\
4271 A string appearing directly as the value of a symbol is processed verbatim\n\
4272 in that the %-constructs below are not recognized.\n\
4273 For a list of the form `(:eval FORM)', FORM is evaluated and the result\n\
4274 is used as a mode line element.\n\
4275 For a list whose car is a symbol, the symbol's value is taken,\n\
4276 and if that is non-nil, the cadr of the list is processed recursively.\n\
4277 Otherwise, the caddr of the list (if there is one) is processed.\n\
4278 For a list whose car is a string or list, each element is processed\n\
4279 recursively and the results are effectively concatenated.\n\
4280 For a list whose car is an integer, the cdr of the list is processed\n\
4281 and padded (if the number is positive) or truncated (if negative)\n\
4282 to the width specified by that number.\n\
4283 A string is printed verbatim in the mode line except for %-constructs:\n\
4284 (%-constructs are allowed when the string is the entire mode-line-format\n\
4285 or when it is found in a cons-cell or a list)\n\
4286 %b -- print buffer name. %f -- print visited file name.\n\
4287 %F -- print frame name.\n\
4288 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
4289 %& is like %*, but ignore read-only-ness.\n\
4290 % means buffer is read-only and * means it is modified.\n\
4291 For a modified read-only buffer, %* gives % and %+ gives *.\n\
4292 %s -- print process status. %l -- print the current line number.\n\
4293 %c -- print the current column number (this makes editing slower).\n\
4294 To make the column number update correctly in all cases,\n\
4295 `column-number-mode' must be non-nil.\n\
4296 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
4297 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
4298 or print Bottom or All.\n\
4299 %m -- print the mode name.\n\
4300 %n -- print Narrow if appropriate.\n\
4301 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.\n\
4302 %Z -- like %z, but including the end-of-line format.\n\
4303 %[ -- print one [ for each recursive editing level. %] similar.\n\
4304 %% -- print %. %- -- print infinitely many dashes.\n\
4305 Decimal digits after the % specify field width to which to pad.");
4308 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults
.major_mode
,
4309 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
4310 nil here means use current buffer's major mode.");
4312 DEFVAR_PER_BUFFER ("major-mode", ¤t_buffer
->major_mode
,
4313 make_number (Lisp_Symbol
),
4314 "Symbol for current buffer's major mode.");
4316 DEFVAR_PER_BUFFER ("mode-name", ¤t_buffer
->mode_name
,
4317 make_number (Lisp_String
),
4318 "Pretty name of current buffer's major mode (a string).");
4320 DEFVAR_PER_BUFFER ("abbrev-mode", ¤t_buffer
->abbrev_mode
, Qnil
,
4321 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
4322 Automatically becomes buffer-local when set in any fashion.");
4324 DEFVAR_PER_BUFFER ("case-fold-search", ¤t_buffer
->case_fold_search
,
4326 "*Non-nil if searches and matches should ignore case.\n\
4327 Automatically becomes buffer-local when set in any fashion.");
4329 DEFVAR_PER_BUFFER ("fill-column", ¤t_buffer
->fill_column
,
4330 make_number (Lisp_Int
),
4331 "*Column beyond which automatic line-wrapping should happen.\n\
4332 Automatically becomes buffer-local when set in any fashion.");
4334 DEFVAR_PER_BUFFER ("left-margin", ¤t_buffer
->left_margin
,
4335 make_number (Lisp_Int
),
4336 "*Column for the default indent-line-function to indent to.\n\
4337 Linefeed indents to this column in Fundamental mode.\n\
4338 Automatically becomes buffer-local when set in any fashion.");
4340 DEFVAR_PER_BUFFER ("tab-width", ¤t_buffer
->tab_width
,
4341 make_number (Lisp_Int
),
4342 "*Distance between tab stops (for display of tab characters), in columns.\n\
4343 Automatically becomes buffer-local when set in any fashion.");
4345 DEFVAR_PER_BUFFER ("ctl-arrow", ¤t_buffer
->ctl_arrow
, Qnil
,
4346 "*Non-nil means display control chars with uparrow.\n\
4347 A value of nil means use backslash and octal digits.\n\
4348 Automatically becomes buffer-local when set in any fashion.\n\
4349 This variable does not apply to characters whose display is specified\n\
4350 in the current display table (if there is one).");
4352 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
4353 ¤t_buffer
->enable_multibyte_characters
,
4355 "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
4356 Otherwise they are regarded as unibyte. This affects the display,\n\
4357 file I/O and the behavior of various editing commands.\n\
4359 This variable is buffer-local but you cannot set it directly;\n\
4360 use the function `set-buffer-multibyte' to change a buffer's representation.\n\
4361 Changing its default value with `setq-default' is supported.\n\
4362 See also variable `default-enable-multibyte-characters' and Info node\n\
4363 `(elisp)Text Representations'.");
4365 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
4366 ¤t_buffer
->buffer_file_coding_system
, Qnil
,
4367 "Coding system to be used for encoding the buffer contents on saving.\n\
4368 This variable applies to saving the buffer, and also to `write-region'\n\
4369 and other functions that use `write-region'.\n\
4370 It does not apply to sending output to subprocesses, however.\n\
4372 If this is nil, the buffer is saved without any code conversion\n\
4373 unless some coding system is specified in `file-coding-system-alist'\n\
4374 for the buffer file.\n\
4376 The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
4378 This variable is never applied to a way of decoding\n\
4379 a file while reading it.");
4381 DEFVAR_PER_BUFFER ("direction-reversed", ¤t_buffer
->direction_reversed
,
4383 "*Non-nil means lines in the buffer are displayed right to left.");
4385 DEFVAR_PER_BUFFER ("truncate-lines", ¤t_buffer
->truncate_lines
, Qnil
,
4386 "*Non-nil means do not display continuation lines;\n\
4387 give each line of text one screen line.\n\
4388 Automatically becomes buffer-local when set in any fashion.\n\
4390 Note that this is overridden by the variable\n\
4391 `truncate-partial-width-windows' if that variable is non-nil\n\
4392 and this buffer is not full-frame width.");
4395 DEFVAR_PER_BUFFER ("buffer-file-type", ¤t_buffer
->buffer_file_type
,
4397 "Non-nil if the visited file is a binary file.\n\
4398 This variable is meaningful on MS-DOG and Windows NT.\n\
4399 On those systems, it is automatically local in every buffer.\n\
4400 On other systems, this variable is normally always nil.");
4403 DEFVAR_PER_BUFFER ("default-directory", ¤t_buffer
->directory
,
4404 make_number (Lisp_String
),
4405 "Name of default directory of current buffer. Should end with slash.\n\
4406 Each buffer has its own value of this variable.");
4408 DEFVAR_PER_BUFFER ("auto-fill-function", ¤t_buffer
->auto_fill_function
,
4410 "Function called (if non-nil) to perform auto-fill.\n\
4411 It is called after self-inserting a space or newline.\n\
4412 Each buffer has its own value of this variable.\n\
4413 NOTE: This variable is not a hook;\n\
4414 its value may not be a list of functions.");
4416 DEFVAR_PER_BUFFER ("buffer-file-name", ¤t_buffer
->filename
,
4417 make_number (Lisp_String
),
4418 "Name of file visited in current buffer, or nil if not visiting a file.\n\
4419 Each buffer has its own value of this variable.");
4421 DEFVAR_PER_BUFFER ("buffer-file-truename", ¤t_buffer
->file_truename
,
4422 make_number (Lisp_String
),
4423 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
4424 The truename of a file is calculated by `file-truename'\n\
4425 and then abbreviated with `abbreviate-file-name'.\n\
4426 Each buffer has its own value of this variable.");
4428 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
4429 ¤t_buffer
->auto_save_file_name
,
4430 make_number (Lisp_String
),
4431 "Name of file for auto-saving current buffer,\n\
4432 or nil if buffer should not be auto-saved.\n\
4433 Each buffer has its own value of this variable.");
4435 DEFVAR_PER_BUFFER ("buffer-read-only", ¤t_buffer
->read_only
, Qnil
,
4436 "Non-nil if this buffer is read-only.\n\
4437 Each buffer has its own value of this variable.");
4439 DEFVAR_PER_BUFFER ("buffer-backed-up", ¤t_buffer
->backed_up
, Qnil
,
4440 "Non-nil if this buffer's file has been backed up.\n\
4441 Backing up is done before the first time the file is saved.\n\
4442 Each buffer has its own value of this variable.");
4444 DEFVAR_PER_BUFFER ("buffer-saved-size", ¤t_buffer
->save_length
,
4445 make_number (Lisp_Int
),
4446 "Length of current buffer when last read in, saved or auto-saved.\n\
4448 Each buffer has its own value of this variable.");
4450 DEFVAR_PER_BUFFER ("selective-display", ¤t_buffer
->selective_display
,
4452 "Non-nil enables selective display:\n\
4453 Integer N as value means display only lines\n\
4454 that start with less than n columns of space.\n\
4455 A value of t means, after a ^M, all the rest of the line is invisible.\n\
4456 Then ^M's in the file are written into files as newlines.\n\n\
4457 Automatically becomes buffer-local when set in any fashion.");
4460 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4461 ¤t_buffer
->selective_display_ellipses
,
4463 "t means display ... on previous line when a line is invisible.\n\
4464 Automatically becomes buffer-local when set in any fashion.");
4467 DEFVAR_PER_BUFFER ("overwrite-mode", ¤t_buffer
->overwrite_mode
, Qnil
,
4468 "Non-nil if self-insertion should replace existing text.\n\
4469 The value should be one of `overwrite-mode-textual',\n\
4470 `overwrite-mode-binary', or nil.\n\
4471 If it is `overwrite-mode-textual', self-insertion still\n\
4472 inserts at the end of a line, and inserts when point is before a tab,\n\
4473 until the tab is filled in.\n\
4474 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
4475 Automatically becomes buffer-local when set in any fashion.");
4477 #if 0 /* The doc string is too long for some compilers,
4478 but make-docfile can find it in this comment. */
4479 DEFVAR_PER_BUFFER ("buffer-display-table", ¤t_buffer
->display_table
,
4481 "Display table that controls display of the contents of current buffer.\n\
4482 Automatically becomes buffer-local when set in any fashion.\n\
4483 The display table is a char-table created with `make-display-table'.\n\
4484 The ordinary char-table elements control how to display each possible text\n\
4485 character. Each value should be a vector of characters or nil;\n\
4486 nil means display the character in the default fashion.\n\
4487 There are six extra slots to control the display of\n\
4488 the end of a truncated screen line (extra-slot 0, a single character);\n\
4489 the end of a continued line (extra-slot 1, a single character);\n\
4490 the escape character used to display character codes in octal\n\
4491 (extra-slot 2, a single character);\n\
4492 the character used as an arrow for control characters (extra-slot 3,\n\
4493 a single character);\n\
4494 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
4495 a vector of characters);\n\
4496 the character used to draw the border between side-by-side windows\n\
4497 (extra-slot 5, a single character).\n\
4498 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
4499 If this variable is nil, the value of `standard-display-table' is used.\n\
4500 Each window can have its own, overriding display table.");
4502 DEFVAR_PER_BUFFER ("buffer-display-table", ¤t_buffer
->display_table
,
4505 DEFVAR_PER_BUFFER ("left-margin-width", ¤t_buffer
->left_margin_width
,
4507 "*Width of left marginal area for display of a buffer.\n\
4508 Automatically becomes buffer-local when set in any fashion.\n\
4509 A value of nil means no marginal area.");
4511 DEFVAR_PER_BUFFER ("right-margin-width", ¤t_buffer
->right_margin_width
,
4513 "*Width of right marginal area for display of a buffer.\n\
4514 Automatically becomes buffer-local when set in any fashion.\n\
4515 A value of nil means no marginal area.");
4517 DEFVAR_PER_BUFFER ("indicate-empty-lines",
4518 ¤t_buffer
->indicate_empty_lines
, Qnil
,
4519 "*Visually indicate empty lines after the buffer end.\n\
4520 If non-nil, a bitmap is displayed in the left fringe of a window on\n\
4522 Automatically becomes buffer-local when set in any fashion.\n");
4524 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
4525 ¤t_buffer
->scroll_up_aggressively
, Qnil
,
4526 "*If a number, scroll display up aggressively.\n\
4527 If scrolling a window because point is above the window start, choose\n\
4528 a new window start so that point ends up that fraction of the window's\n\
4529 height from the bottom of the window.\n\
4530 Automatically becomes buffer-local when set in any fashion.");
4532 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
4533 ¤t_buffer
->scroll_down_aggressively
, Qnil
,
4534 "*If a number, scroll display down aggressively.\n\
4535 If scrolling a window because point is below the window end, choose\n\
4536 a new window start so that point ends up that fraction of the window's\n\
4537 height from the top of the window.\n\
4538 Automatically becomes buffer-local when set in any fashion.");
4540 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4543 DEFVAR_LISP ("before-change-function", &Vbefore_change_function
,
4544 "If non-nil, a function to call before each text change (obsolete).\n\
4545 Two arguments are passed to the function: the positions of\n\
4546 the beginning and end of the range of old text to be changed.\n\
4547 \(For an insertion, the beginning and end are at the same place.)\n\
4548 No information is given about the length of the text after the change.\n\
4550 Buffer changes made while executing the `before-change-function'\n\
4551 don't call any before-change or after-change functions.\n\
4552 That's because these variables are temporarily set to nil.\n\
4553 As a result, a hook function cannot straightforwardly alter the value of\n\
4554 these variables. See the Emacs Lisp manual for a way of\n\
4555 accomplishing an equivalent result by using other variables.\n\n\
4556 This variable is obsolete; use `before-change-functions' instead.");
4557 Vbefore_change_function
= Qnil
;
4559 DEFVAR_LISP ("after-change-function", &Vafter_change_function
,
4560 "If non-nil, a Function to call after each text change (obsolete).\n\
4561 Three arguments are passed to the function: the positions of\n\
4562 the beginning and end of the range of changed text,\n\
4563 and the length of the pre-change text replaced by that range.\n\
4564 \(For an insertion, the pre-change length is zero;\n\
4565 for a deletion, that length is the number of bytes deleted,\n\
4566 and the post-change beginning and end are at the same place.)\n\
4568 Buffer changes made while executing the `after-change-function'\n\
4569 don't call any before-change or after-change functions.\n\
4570 That's because these variables are temporarily set to nil.\n\
4571 As a result, a hook function cannot straightforwardly alter the value of\n\
4572 these variables. See the Emacs Lisp manual for a way of\n\
4573 accomplishing an equivalent result by using other variables.\n\n\
4574 This variable is obsolete; use `after-change-functions' instead.");
4575 Vafter_change_function
= Qnil
;
4577 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions
,
4578 "List of functions to call before each text change.\n\
4579 Two arguments are passed to each function: the positions of\n\
4580 the beginning and end of the range of old text to be changed.\n\
4581 \(For an insertion, the beginning and end are at the same place.)\n\
4582 No information is given about the length of the text after the change.\n\
4584 Buffer changes made while executing the `before-change-functions'\n\
4585 don't call any before-change or after-change functions.\n\
4586 That's because these variables are temporarily set to nil.\n\
4587 As a result, a hook function cannot straightforwardly alter the value of\n\
4588 these variables. See the Emacs Lisp manual for a way of\n\
4589 accomplishing an equivalent result by using other variables.\n\
4591 If an unhandled error happens in running these functions,\n\
4592 the variable's value remains nil. That prevents the error\n\
4593 from happening repeatedly and making Emacs nonfunctional.");
4594 Vbefore_change_functions
= Qnil
;
4596 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions
,
4597 "List of function to call after each text change.\n\
4598 Three arguments are passed to each function: the positions of\n\
4599 the beginning and end of the range of changed text,\n\
4600 and the length in bytes of the pre-change text replaced by that range.\n\
4601 \(For an insertion, the pre-change length is zero;\n\
4602 for a deletion, that length is the number of bytes deleted,\n\
4603 and the post-change beginning and end are at the same place.)\n\
4605 Buffer changes made while executing the `after-change-functions'\n\
4606 don't call any before-change or after-change functions.\n\
4607 That's because these variables are temporarily set to nil.\n\
4608 As a result, a hook function cannot straightforwardly alter the value of\n\
4609 these variables. See the Emacs Lisp manual for a way of\n\
4610 accomplishing an equivalent result by using other variables.\n\
4612 If an unhandled error happens in running these functions,\n\
4613 the variable's value remains nil. That prevents the error\n\
4614 from happening repeatedly and making Emacs nonfunctional.");
4615 Vafter_change_functions
= Qnil
;
4617 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook
,
4618 "A list of functions to call before changing a buffer which is unmodified.\n\
4619 The functions are run using the `run-hooks' function.");
4620 Vfirst_change_hook
= Qnil
;
4622 #if 0 /* The doc string is too long for some compilers,
4623 but make-docfile can find it in this comment. */
4624 DEFVAR_PER_BUFFER ("buffer-undo-list", ¤t_buffer
->undo_list
, Qnil
,
4625 "List of undo entries in current buffer.\n\
4626 This variable is always local in all buffers.\n\
4627 Recent changes come first; older changes follow newer.\n\
4629 An entry (BEG . END) represents an insertion which begins at\n\
4630 position BEG and ends at position END.\n\
4632 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4633 from (abs POSITION). If POSITION is positive, point was at the front\n\
4634 of the text being deleted; if negative, point was at the end.\n\
4636 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4637 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4638 of the visited file's modification time, as of that time. If the\n\
4639 modification time of the most recent save is different, this entry is\n\
4642 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4643 was modified between BEG and END. PROPERTY is the property name,\n\
4644 and VALUE is the old value.\n\
4646 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4647 was adjusted in position by the offset DISTANCE (an integer).\n\
4649 An entry of the form POSITION indicates that point was at the buffer\n\
4650 location given by the integer. Undoing an entry of this form places\n\
4651 point at POSITION.\n\
4653 nil marks undo boundaries. The undo command treats the changes\n\
4654 between two undo boundaries as a single step to be undone.\n\
4656 If the value of the variable is t, undo information is not recorded.");
4658 DEFVAR_PER_BUFFER ("buffer-undo-list", ¤t_buffer
->undo_list
, Qnil
,
4661 DEFVAR_PER_BUFFER ("mark-active", ¤t_buffer
->mark_active
, Qnil
,
4662 "Non-nil means the mark and region are currently active in this buffer.\n\
4663 Automatically local in all buffers.");
4665 DEFVAR_PER_BUFFER ("cache-long-line-scans", ¤t_buffer
->cache_long_line_scans
, Qnil
,
4666 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4667 This variable is buffer-local, in all buffers.\n\
4669 Normally, the line-motion functions work by scanning the buffer for\n\
4670 newlines. Columnar operations (like move-to-column and\n\
4671 compute-motion) also work by scanning the buffer, summing character\n\
4672 widths as they go. This works well for ordinary text, but if the\n\
4673 buffer's lines are very long (say, more than 500 characters), these\n\
4674 motion functions will take longer to execute. Emacs may also take\n\
4675 longer to update the display.\n\
4677 If cache-long-line-scans is non-nil, these motion functions cache the\n\
4678 results of their scans, and consult the cache to avoid rescanning\n\
4679 regions of the buffer until the text is modified. The caches are most\n\
4680 beneficial when they prevent the most searching---that is, when the\n\
4681 buffer contains long lines and large regions of characters with the\n\
4682 same, fixed screen width.\n\
4684 When cache-long-line-scans is non-nil, processing short lines will\n\
4685 become slightly slower (because of the overhead of consulting the\n\
4686 cache), and the caches will use memory roughly proportional to the\n\
4687 number of newlines and characters whose screen width varies.\n\
4689 The caches require no explicit maintenance; their accuracy is\n\
4690 maintained internally by the Emacs primitives. Enabling or disabling\n\
4691 the cache should not affect the behavior of any of the motion\n\
4692 functions; it should only affect their performance.");
4694 DEFVAR_PER_BUFFER ("point-before-scroll", ¤t_buffer
->point_before_scroll
, Qnil
,
4695 "Value of point before the last series of scroll operations, or nil.\n\
4696 This variable is always local in all buffers.");
4698 DEFVAR_PER_BUFFER ("buffer-file-format", ¤t_buffer
->file_format
, Qnil
,
4699 "List of formats to use when saving this buffer.\n\
4700 This variable is always local in all buffers.\n\
4701 Formats are defined by `format-alist'. This variable is\n\
4702 set when a file is visited. Automatically local in all buffers.");
4704 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4705 ¤t_buffer
->invisibility_spec
, Qnil
,
4706 "Invisibility spec of this buffer.\n\
4707 This variable is always local in all buffers.\n\
4708 The default is t, which means that text is invisible\n\
4709 if it has a non-nil `invisible' property.\n\
4710 If the value is a list, a text character is invisible if its `invisible'\n\
4711 property is an element in that list.\n\
4712 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4713 then characters with property value PROP are invisible,\n\
4714 and they have an ellipsis as well if ELLIPSIS is non-nil.");
4716 DEFVAR_PER_BUFFER ("buffer-display-count",
4717 ¤t_buffer
->display_count
, Qnil
,
4718 "A number incremented each time this buffer is displayed in a window.\n\
4719 This variable is always local in all buffers.\n\
4720 The function `set-window-buffer increments it.");
4722 DEFVAR_PER_BUFFER ("buffer-display-time",
4723 ¤t_buffer
->display_time
, Qnil
,
4724 "Time stamp updated each time this buffer is displayed in a window.\n\
4725 This variable is always local in all buffers.\n\
4726 The function `set-window-buffer' updates this variable\n\
4727 to the value obtained by calling `current-time'.\n\
4728 If the buffer has never been shown in a window, the value is nil.");
4730 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode
,
4731 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4732 Non-nil also enables highlighting of the region whenever the mark is active.\n\
4733 The variable `highlight-nonselected-windows' controls whether to highlight\n\
4734 all windows or just the selected window.");
4735 Vtransient_mark_mode
= Qnil
;
4737 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only
,
4738 "*Non-nil means disregard read-only status of buffers or characters.\n\
4739 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4740 text properties. If the value is a list, disregard `buffer-read-only'\n\
4741 and disregard a `read-only' text property if the property value\n\
4742 is a member of the list.");
4743 Vinhibit_read_only
= Qnil
;
4745 DEFVAR_PER_BUFFER ("cursor-type", ¤t_buffer
->cursor_type
, Qnil
,
4746 "Cursor to use in window displaying this buffer.\n\
4747 Values are interpreted as follows:\n\
4749 t use the cursor specified for the frame\n\
4750 nil don't display a cursor\n\
4751 `bar' display a bar cursor with default width\n\
4752 (bar . WIDTH) display a bar cursor with width WIDTH\n\
4753 others display a box cursor.");
4755 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions
,
4756 "List of functions called with no args to query before killing a buffer.");
4757 Vkill_buffer_query_functions
= Qnil
;
4759 defsubr (&Sbuffer_live_p
);
4760 defsubr (&Sbuffer_list
);
4761 defsubr (&Sget_buffer
);
4762 defsubr (&Sget_file_buffer
);
4763 defsubr (&Sget_buffer_create
);
4764 defsubr (&Smake_indirect_buffer
);
4765 defsubr (&Sgenerate_new_buffer_name
);
4766 defsubr (&Sbuffer_name
);
4767 /*defsubr (&Sbuffer_number);*/
4768 defsubr (&Sbuffer_file_name
);
4769 defsubr (&Sbuffer_base_buffer
);
4770 defsubr (&Sbuffer_local_variables
);
4771 defsubr (&Sbuffer_modified_p
);
4772 defsubr (&Sset_buffer_modified_p
);
4773 defsubr (&Sbuffer_modified_tick
);
4774 defsubr (&Srename_buffer
);
4775 defsubr (&Sother_buffer
);
4776 defsubr (&Sbuffer_disable_undo
);
4777 defsubr (&Sbuffer_enable_undo
);
4778 defsubr (&Skill_buffer
);
4779 defsubr (&Sset_buffer_major_mode
);
4780 defsubr (&Sswitch_to_buffer
);
4781 defsubr (&Spop_to_buffer
);
4782 defsubr (&Scurrent_buffer
);
4783 defsubr (&Sset_buffer
);
4784 defsubr (&Sbarf_if_buffer_read_only
);
4785 defsubr (&Sbury_buffer
);
4786 defsubr (&Serase_buffer
);
4787 defsubr (&Sset_buffer_multibyte
);
4788 defsubr (&Skill_all_local_variables
);
4790 defsubr (&Soverlayp
);
4791 defsubr (&Smake_overlay
);
4792 defsubr (&Sdelete_overlay
);
4793 defsubr (&Smove_overlay
);
4794 defsubr (&Soverlay_start
);
4795 defsubr (&Soverlay_end
);
4796 defsubr (&Soverlay_buffer
);
4797 defsubr (&Soverlay_properties
);
4798 defsubr (&Soverlays_at
);
4799 defsubr (&Soverlays_in
);
4800 defsubr (&Snext_overlay_change
);
4801 defsubr (&Sprevious_overlay_change
);
4802 defsubr (&Soverlay_recenter
);
4803 defsubr (&Soverlay_lists
);
4804 defsubr (&Soverlay_get
);
4805 defsubr (&Soverlay_put
);
4806 defsubr (&Srestore_buffer_modified_p
);
4812 initial_define_key (control_x_map
, 'b', "switch-to-buffer");
4813 initial_define_key (control_x_map
, 'k', "kill-buffer");
4815 /* This must not be in syms_of_buffer, because Qdisabled is not
4816 initialized when that function gets called. */
4817 Fput (intern ("erase-buffer"), Qdisabled
, Qt
);