*** empty log message ***
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4e6835db 4 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
1ab256cb
RM
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
c922bc55 10the Free Software Foundation; either version 2, or (at your option)
1ab256cb
RM
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
1ab256cb 22
68c45bf0 23#include <config.h>
1ab256cb 24
2381d133
JB
25#include <sys/types.h>
26#include <sys/stat.h>
1ab256cb 27#include <sys/param.h>
9dde47f5 28#include <errno.h>
15e7873a 29#include <stdio.h>
9dde47f5 30
2538fae4 31#ifndef USE_CRT_DLL
9dde47f5 32extern int errno;
2538fae4 33#endif
1ab256cb 34
1ab256cb 35
dfcf069d
AS
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
7ee72033 39
1ab256cb 40#include "lisp.h"
21cf4cf8 41#include "intervals.h"
1ab256cb
RM
42#include "window.h"
43#include "commands.h"
44#include "buffer.h"
8f348ed5 45#include "character.h"
28e969dd 46#include "region-cache.h"
1ab256cb 47#include "indent.h"
d014bf88 48#include "blockinput.h"
2538fae4 49#include "keyboard.h"
e35f6ff7 50#include "keymap.h"
08460cd4 51#include "frame.h"
1ab256cb
RM
52
53struct buffer *current_buffer; /* the current buffer */
54
55/* First buffer in chain of all buffers (in reverse order of creation).
56 Threaded through ->next. */
57
58struct buffer *all_buffers;
59
60/* This structure holds the default values of the buffer-local variables
61 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
62 The default value occupies the same slot in this structure
63 as an individual buffer's value occupies in that buffer.
64 Setting the default value also goes through the alist of buffers
65 and stores into each buffer that does not say it has a local value. */
66
6b61353c 67DECL_ALIGN (struct buffer, buffer_defaults);
1ab256cb
RM
68
69/* A Lisp_Object pointer to the above, used for staticpro */
70
71static Lisp_Object Vbuffer_defaults;
72
73/* This structure marks which slots in a buffer have corresponding
74 default values in buffer_defaults.
75 Each such slot has a nonzero value in this structure.
76 The value has only one nonzero bit.
77
78 When a buffer has its own local value for a slot,
7c02e886
GM
79 the entry for that slot (found in the same slot in this structure)
80 is turned on in the buffer's local_flags array.
1ab256cb
RM
81
82 If a slot in this structure is -1, then even though there may
83 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
84 and the corresponding slot in buffer_defaults is not used.
85
86 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
87 but there is a default value which is copied into each buffer.
88
1ab256cb
RM
89 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
90 zero, that is a bug */
91
92struct buffer buffer_local_flags;
93
94/* This structure holds the names of symbols whose values may be
95 buffer-local. It is indexed and accessed in the same way as the above. */
96
6b61353c
KH
97DECL_ALIGN (struct buffer, buffer_local_symbols);
98
1ab256cb
RM
99/* A Lisp_Object pointer to the above, used for staticpro */
100static Lisp_Object Vbuffer_local_symbols;
101
0fa3ba92
JB
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
1bf08baf
KH
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.
109
110 If a slot here contains -1, the corresponding variable is read-only. */
0fa3ba92
JB
111struct buffer buffer_local_types;
112
13de9290
RS
113/* Flags indicating which built-in buffer-local variables
114 are permanent locals. */
7313acd0 115static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
7c02e886
GM
116
117/* Number of per-buffer variables used. */
118
7313acd0 119int last_per_buffer_idx;
13de9290 120
1ab256cb 121Lisp_Object Fset_buffer ();
01050cb5 122void set_buffer_internal ();
c7aa5005 123void set_buffer_internal_1 ();
173f2a64 124static void call_overlay_mod_hooks ();
2f3f993b 125static void swap_out_buffer_local_variables ();
13de9290 126static void reset_buffer_local_variables ();
1ab256cb
RM
127
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. */
131Lisp_Object Vbuffer_alist;
132
133/* Functions to call before and after each text change. */
5f079267
RS
134Lisp_Object Vbefore_change_functions;
135Lisp_Object Vafter_change_functions;
1ab256cb 136
c48f61ef
RS
137Lisp_Object Vtransient_mark_mode;
138
a96b68f1
RS
139/* t means ignore all read-only text properties.
140 A list means ignore such a property if its value is a member of the list.
141 Any non-nil value means ignore buffer-read-only. */
142Lisp_Object Vinhibit_read_only;
143
dcdffbf6
RS
144/* List of functions to call that can query about killing a buffer.
145 If any of these functions returns nil, we don't kill it. */
146Lisp_Object Vkill_buffer_query_functions;
5b20caf0 147Lisp_Object Qkill_buffer_query_functions;
dcdffbf6 148
43ed3b8d
CY
149/* Hook run before changing a major mode. */
150Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
151
dbc4e1c1
JB
152/* List of functions to call before changing an unmodified buffer. */
153Lisp_Object Vfirst_change_hook;
22378665 154
dbc4e1c1 155Lisp_Object Qfirst_change_hook;
22378665
RS
156Lisp_Object Qbefore_change_functions;
157Lisp_Object Qafter_change_functions;
48265e61 158Lisp_Object Qucs_set_table_for_input;
1ab256cb 159
7775635d
KH
160/* If nonzero, all modification hooks are suppressed. */
161int inhibit_modification_hooks;
162
1ab256cb
RM
163Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
164
165Lisp_Object Qprotected_field;
166
167Lisp_Object QSFundamental; /* A string "Fundamental" */
168
169Lisp_Object Qkill_buffer_hook;
170
5fe0b67e
RS
171Lisp_Object Qget_file_buffer;
172
52f8ec73
JB
173Lisp_Object Qoverlayp;
174
bbbe9545 175Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
5985d248 176
294d215f
RS
177Lisp_Object Qmodification_hooks;
178Lisp_Object Qinsert_in_front_hooks;
179Lisp_Object Qinsert_behind_hooks;
180
b86af064
GM
181static void alloc_buffer_text P_ ((struct buffer *, size_t));
182static void free_buffer_text P_ ((struct buffer *b));
2410d73a 183static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
2e293742 184static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
e1688f54 185static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
b86af064 186
cab1603f 187extern char * emacs_strerror P_ ((int));
b86af064 188
1ab256cb
RM
189/* For debugging; temporary. See set_buffer_internal. */
190/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
191
01136e9b 192void
1ab256cb
RM
193nsberror (spec)
194 Lisp_Object spec;
195{
a7a60ce9 196 if (STRINGP (spec))
d5db4077 197 error ("No buffer named %s", SDATA (spec));
1ab256cb
RM
198 error ("Invalid buffer argument");
199}
200\f
0dc88e60 201DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
7ee72033
MB
202 doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
203Value is nil if OBJECT is not a buffer or if it has been killed. */)
204 (object)
0dc88e60
RS
205 Lisp_Object object;
206{
207 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
208 ? Qt : Qnil);
209}
210
08460cd4 211DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
7ee72033 212 doc: /* Return a list of all existing live buffers.
aef466d5
RS
213If the optional arg FRAME is a frame, we return the buffer list
214in the proper order for that frame: the buffers in FRAME's `buffer-list'
215frame parameter come first, followed by the rest of the buffers. */)
7ee72033 216 (frame)
08460cd4 217 Lisp_Object frame;
1ab256cb 218{
08460cd4
RS
219 Lisp_Object framelist, general;
220 general = Fmapcar (Qcdr, Vbuffer_alist);
221
222 if (FRAMEP (frame))
223 {
224 Lisp_Object tail;
225
b7826503 226 CHECK_FRAME (frame);
08460cd4
RS
227
228 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
229
230 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
231 tail = framelist;
232 while (! NILP (tail))
233 {
7539e11f
KR
234 general = Fdelq (XCAR (tail), general);
235 tail = XCDR (tail);
08460cd4
RS
236 }
237 return nconc2 (framelist, general);
238 }
239
240 return general;
1ab256cb
RM
241}
242
04ae1b48
RS
243/* Like Fassoc, but use Fstring_equal to compare
244 (which ignores text properties),
245 and don't ever QUIT. */
246
247static Lisp_Object
248assoc_ignore_text_properties (key, list)
249 register Lisp_Object key;
250 Lisp_Object list;
251{
252 register Lisp_Object tail;
6d70a280 253 for (tail = list; CONSP (tail); tail = XCDR (tail))
04ae1b48
RS
254 {
255 register Lisp_Object elt, tem;
6d70a280 256 elt = XCAR (tail);
04ae1b48
RS
257 tem = Fstring_equal (Fcar (elt), key);
258 if (!NILP (tem))
259 return elt;
260 }
261 return Qnil;
262}
263
1ab256cb 264DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
7ee72033 265 doc: /* Return the buffer named NAME (a string).
018ba359 266If there is no live buffer named NAME, return nil.
7ee72033
MB
267NAME may also be a buffer; if so, the value is that buffer. */)
268 (name)
1ab256cb
RM
269 register Lisp_Object name;
270{
a7a60ce9 271 if (BUFFERP (name))
1ab256cb 272 return name;
b7826503 273 CHECK_STRING (name);
1ab256cb 274
04ae1b48 275 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
1ab256cb
RM
276}
277
278DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
7ee72033 279 doc: /* Return the buffer visiting file FILENAME (a string).
018ba359
PJ
280The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
281If there is no such live buffer, return nil.
7ee72033
MB
282See also `find-buffer-visiting'. */)
283 (filename)
1ab256cb
RM
284 register Lisp_Object filename;
285{
286 register Lisp_Object tail, buf, tem;
5fe0b67e
RS
287 Lisp_Object handler;
288
b7826503 289 CHECK_STRING (filename);
1ab256cb
RM
290 filename = Fexpand_file_name (filename, Qnil);
291
5fe0b67e
RS
292 /* If the file name has special constructs in it,
293 call the corresponding file handler. */
a617e913 294 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
5fe0b67e
RS
295 if (!NILP (handler))
296 return call2 (handler, Qget_file_buffer, filename);
297
7539e11f 298 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
1ab256cb 299 {
7539e11f 300 buf = Fcdr (XCAR (tail));
a7a60ce9
KH
301 if (!BUFFERP (buf)) continue;
302 if (!STRINGP (XBUFFER (buf)->filename)) continue;
1ab256cb 303 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 304 if (!NILP (tem))
1ab256cb
RM
305 return buf;
306 }
307 return Qnil;
308}
309
52e01189
RS
310Lisp_Object
311get_truename_buffer (filename)
312 register Lisp_Object filename;
313{
314 register Lisp_Object tail, buf, tem;
315
7539e11f 316 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
52e01189 317 {
7539e11f 318 buf = Fcdr (XCAR (tail));
52e01189
RS
319 if (!BUFFERP (buf)) continue;
320 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
321 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
322 if (!NILP (tem))
323 return buf;
324 }
325 return Qnil;
326}
327
1ab256cb
RM
328/* Incremented for each buffer created, to assign the buffer number. */
329int buffer_count;
330
331DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
7ee72033 332 doc: /* Return the buffer named NAME, or create such a buffer and return it.
018ba359
PJ
333A new buffer is created if there is no live buffer named NAME.
334If NAME starts with a space, the new buffer does not keep undo information.
335If NAME is a buffer instead of a string, then it is the value returned.
7ee72033
MB
336The value is never nil. */)
337 (name)
1ab256cb
RM
338 register Lisp_Object name;
339{
a9ee7a59 340 register Lisp_Object buf;
1ab256cb
RM
341 register struct buffer *b;
342
343 buf = Fget_buffer (name);
265a9e55 344 if (!NILP (buf))
1ab256cb
RM
345 return buf;
346
d5db4077 347 if (SCHARS (name) == 0)
31cd83e9
KH
348 error ("Empty string for buffer name is not allowed");
349
a21260d9 350 b = (struct buffer *) allocate_buffer ();
1ab256cb 351
336cd056
RS
352 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
353
354 /* An ordinary buffer uses its own struct buffer_text. */
355 b->text = &b->own_text;
356 b->base_buffer = 0;
357
1ab256cb 358 BUF_GAP_SIZE (b) = 20;
9ac0d9e0 359 BLOCK_INPUT;
3b06f880
KH
360 /* We allocate extra 1-byte at the tail and keep it always '\0' for
361 anchoring a search. */
b86af064 362 alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
9ac0d9e0 363 UNBLOCK_INPUT;
1ab256cb 364 if (! BUF_BEG_ADDR (b))
81841847 365 buffer_memory_full ();
1ab256cb 366
6d70a280
SM
367 BUF_PT (b) = BEG;
368 BUF_GPT (b) = BEG;
369 BUF_BEGV (b) = BEG;
370 BUF_ZV (b) = BEG;
371 BUF_Z (b) = BEG;
372 BUF_PT_BYTE (b) = BEG_BYTE;
373 BUF_GPT_BYTE (b) = BEG_BYTE;
374 BUF_BEGV_BYTE (b) = BEG_BYTE;
375 BUF_ZV_BYTE (b) = BEG_BYTE;
376 BUF_Z_BYTE (b) = BEG_BYTE;
1ab256cb 377 BUF_MODIFF (b) = 1;
3e145152 378 BUF_CHARS_MODIFF (b) = 1;
2509d356 379 BUF_OVERLAY_MODIFF (b) = 1;
336cd056
RS
380 BUF_SAVE_MODIFF (b) = 1;
381 BUF_INTERVALS (b) = 0;
b5a225b4
GM
382 BUF_UNCHANGED_MODIFIED (b) = 1;
383 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
384 BUF_END_UNCHANGED (b) = 0;
385 BUF_BEG_UNCHANGED (b) = 0;
3b06f880 386 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
1ab256cb 387
28e969dd
JB
388 b->newline_cache = 0;
389 b->width_run_cache = 0;
390 b->width_table = Qnil;
b5a225b4 391 b->prevent_redisplay_optimizations_p = 1;
28e969dd 392
1ab256cb
RM
393 /* Put this on the chain of all buffers including killed ones. */
394 b->next = all_buffers;
395 all_buffers = b;
396
336cd056
RS
397 /* An ordinary buffer normally doesn't need markers
398 to handle BEGV and ZV. */
399 b->pt_marker = Qnil;
400 b->begv_marker = Qnil;
401 b->zv_marker = Qnil;
04ae1b48
RS
402
403 name = Fcopy_sequence (name);
ab77f05c 404 STRING_SET_INTERVALS (name, NULL_INTERVAL);
1ab256cb 405 b->name = name;
04ae1b48 406
d5db4077 407 if (SREF (name, 0) != ' ')
1ab256cb
RM
408 b->undo_list = Qnil;
409 else
410 b->undo_list = Qt;
411
412 reset_buffer (b);
13de9290 413 reset_buffer_local_variables (b, 1);
1ab256cb 414
1f57cb74 415 b->mark = Fmake_marker ();
65745fad 416 BUF_MARKERS (b) = NULL;
1f57cb74
RS
417 b->name = name;
418
1ab256cb 419 /* Put this in the alist of all live buffers. */
67180c6a 420 XSETBUFFER (buf, b);
1ab256cb
RM
421 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
422
2f064abf 423 /* An error in calling the function here (should someone redefine it)
d36b182f
DL
424 can lead to infinite regress until you run out of stack. rms
425 says that's not worth protecting against. */
48265e61 426 if (!NILP (Ffboundp (Qucs_set_table_for_input)))
d36b182f 427 /* buf is on buffer-alist, so no gcpro. */
48265e61
DL
428 call1 (Qucs_set_table_for_input, buf);
429
336cd056
RS
430 return buf;
431}
432
7e9d5818 433
04e4cb3a
GM
434/* Return a list of overlays which is a copy of the overlay list
435 LIST, but for buffer B. */
436
2410d73a 437static struct Lisp_Overlay *
04e4cb3a
GM
438copy_overlays (b, list)
439 struct buffer *b;
2410d73a 440 struct Lisp_Overlay *list;
04e4cb3a 441{
2410d73a
SM
442 Lisp_Object buffer;
443 struct Lisp_Overlay *result = NULL, *tail = NULL;
04e4cb3a
GM
444
445 XSETBUFFER (buffer, b);
446
2410d73a 447 for (; list; list = list->next)
04e4cb3a
GM
448 {
449 Lisp_Object overlay, start, end, old_overlay;
27c6b98e 450 EMACS_INT charpos;
04e4cb3a 451
2410d73a 452 XSETMISC (old_overlay, list);
04e4cb3a
GM
453 charpos = marker_position (OVERLAY_START (old_overlay));
454 start = Fmake_marker ();
db3a49a6 455 Fset_marker (start, make_number (charpos), buffer);
04e4cb3a
GM
456 XMARKER (start)->insertion_type
457 = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
458
459 charpos = marker_position (OVERLAY_END (old_overlay));
460 end = Fmake_marker ();
db3a49a6 461 Fset_marker (end, make_number (charpos), buffer);
04e4cb3a
GM
462 XMARKER (end)->insertion_type
463 = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
464
465 overlay = allocate_misc ();
466 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
467 OVERLAY_START (overlay) = start;
468 OVERLAY_END (overlay) = end;
469 OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
2410d73a 470 XOVERLAY (overlay)->next = NULL;
177c0ea7 471
2410d73a
SM
472 if (tail)
473 tail = tail->next = XOVERLAY (overlay);
474 else
475 result = tail = XOVERLAY (overlay);
04e4cb3a
GM
476 }
477
2410d73a 478 return result;
04e4cb3a 479}
177c0ea7 480
04e4cb3a 481
7e9d5818
GM
482/* Clone per-buffer values of buffer FROM.
483
484 Buffer TO gets the same per-buffer values as FROM, with the
485 following exceptions: (1) TO's name is left untouched, (2) markers
486 are copied and made to refer to TO, and (3) overlay lists are
487 copied. */
488
489static void
490clone_per_buffer_values (from, to)
491 struct buffer *from, *to;
492{
54622f33 493 Lisp_Object to_buffer;
7e9d5818
GM
494 int offset;
495
496 XSETBUFFER (to_buffer, to);
177c0ea7 497
7e9d5818
GM
498 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
499 offset < sizeof *to;
500 offset += sizeof (Lisp_Object))
501 {
502 Lisp_Object obj;
503
504 obj = PER_BUFFER_VALUE (from, offset);
505 if (MARKERP (obj))
506 {
507 struct Lisp_Marker *m = XMARKER (obj);
508 obj = Fmake_marker ();
509 XMARKER (obj)->insertion_type = m->insertion_type;
510 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
511 }
512
513 PER_BUFFER_VALUE (to, offset) = obj;
514 }
515
7e9d5818 516 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
177c0ea7 517
04e4cb3a
GM
518 to->overlays_before = copy_overlays (to, from->overlays_before);
519 to->overlays_after = copy_overlays (to, from->overlays_after);
02f28bbd 520
e1688f54
RS
521 /* Get (a copy of) the alist of Lisp-level local variables of FROM
522 and install that in TO. */
523 to->local_var_alist = buffer_lisp_local_variables (from);
7e9d5818
GM
524}
525
7e9d5818
GM
526DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
527 2, 3,
193c3837 528 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
7ee72033 529 doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
6b61353c 530BASE-BUFFER should be a live buffer, or the name of an existing buffer.
018ba359
PJ
531NAME should be a string which is not the name of an existing buffer.
532Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
533such as major and minor modes, in the indirect buffer.
7ee72033
MB
534CLONE nil means the indirect buffer's state is reset to default values. */)
535 (base_buffer, name, clone)
7e9d5818 536 Lisp_Object base_buffer, name, clone;
336cd056 537{
6b61353c 538 Lisp_Object buf, tem;
7e9d5818 539 struct buffer *b;
336cd056 540
6b61353c 541 CHECK_STRING (name);
336cd056
RS
542 buf = Fget_buffer (name);
543 if (!NILP (buf))
d5db4077 544 error ("Buffer name `%s' is in use", SDATA (name));
336cd056 545
6b61353c 546 tem = base_buffer;
336cd056
RS
547 base_buffer = Fget_buffer (base_buffer);
548 if (NILP (base_buffer))
6b61353c
KH
549 error ("No such buffer: `%s'", SDATA (tem));
550 if (NILP (XBUFFER (base_buffer)->name))
551 error ("Base buffer has been killed");
336cd056 552
d5db4077 553 if (SCHARS (name) == 0)
336cd056
RS
554 error ("Empty string for buffer name is not allowed");
555
a21260d9 556 b = (struct buffer *) allocate_buffer ();
336cd056
RS
557 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
558
559 if (XBUFFER (base_buffer)->base_buffer)
560 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
561 else
562 b->base_buffer = XBUFFER (base_buffer);
563
564 /* Use the base buffer's text object. */
565 b->text = b->base_buffer->text;
566
567 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
568 BUF_ZV (b) = BUF_ZV (b->base_buffer);
569 BUF_PT (b) = BUF_PT (b->base_buffer);
3f236a40
RS
570 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
571 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
572 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
336cd056
RS
573
574 b->newline_cache = 0;
575 b->width_run_cache = 0;
576 b->width_table = Qnil;
577
578 /* Put this on the chain of all buffers including killed ones. */
579 b->next = all_buffers;
580 all_buffers = b;
581
582 name = Fcopy_sequence (name);
ab77f05c 583 STRING_SET_INTERVALS (name, NULL_INTERVAL);
336cd056
RS
584 b->name = name;
585
586 reset_buffer (b);
13de9290 587 reset_buffer_local_variables (b, 1);
336cd056
RS
588
589 /* Put this in the alist of all live buffers. */
590 XSETBUFFER (buf, b);
591 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
592
593 b->mark = Fmake_marker ();
1ab256cb 594 b->name = name;
336cd056 595
abc9d959
RS
596 /* The multibyte status belongs to the base buffer. */
597 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
598
336cd056
RS
599 /* Make sure the base buffer has markers for its narrowing. */
600 if (NILP (b->base_buffer->pt_marker))
601 {
602 b->base_buffer->pt_marker = Fmake_marker ();
3f236a40
RS
603 set_marker_both (b->base_buffer->pt_marker, base_buffer,
604 BUF_PT (b->base_buffer),
605 BUF_PT_BYTE (b->base_buffer));
336cd056
RS
606 }
607 if (NILP (b->base_buffer->begv_marker))
608 {
609 b->base_buffer->begv_marker = Fmake_marker ();
3f236a40
RS
610 set_marker_both (b->base_buffer->begv_marker, base_buffer,
611 BUF_BEGV (b->base_buffer),
612 BUF_BEGV_BYTE (b->base_buffer));
336cd056
RS
613 }
614 if (NILP (b->base_buffer->zv_marker))
615 {
616 b->base_buffer->zv_marker = Fmake_marker ();
3f236a40
RS
617 set_marker_both (b->base_buffer->zv_marker, base_buffer,
618 BUF_ZV (b->base_buffer),
619 BUF_ZV_BYTE (b->base_buffer));
26d84681 620 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
336cd056
RS
621 }
622
7e9d5818
GM
623 if (NILP (clone))
624 {
625 /* Give the indirect buffer markers for its narrowing. */
626 b->pt_marker = Fmake_marker ();
627 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
628 b->begv_marker = Fmake_marker ();
629 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
630 b->zv_marker = Fmake_marker ();
631 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
632 XMARKER (b->zv_marker)->insertion_type = 1;
633 }
634 else
7fa57e45
RS
635 {
636 struct buffer *old_b = current_buffer;
637
638 clone_per_buffer_values (b->base_buffer, b);
639 b->filename = Qnil;
640 b->file_truename = Qnil;
641 b->display_count = make_number (0);
642 b->backed_up = Qnil;
643 b->auto_save_file_name = Qnil;
644 set_buffer_internal_1 (b);
645 Fset (intern ("buffer-save-without-query"), Qnil);
646 Fset (intern ("buffer-file-number"), Qnil);
647 Fset (intern ("buffer-stale-function"), Qnil);
648 set_buffer_internal_1 (old_b);
649 }
336cd056 650
a9ee7a59 651 return buf;
1ab256cb
RM
652}
653
d4f5719a
SM
654void
655delete_all_overlays (b)
656 struct buffer *b;
657{
658 Lisp_Object overlay;
659
660 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
661 have to empty the list, otherwise we end up with overlays that
662 think they belong to this buffer while the buffer doesn't know about
663 them any more. */
664 while (b->overlays_before)
665 {
666 XSETMISC (overlay, b->overlays_before);
667 Fdelete_overlay (overlay);
668 }
669 while (b->overlays_after)
670 {
671 XSETMISC (overlay, b->overlays_after);
672 Fdelete_overlay (overlay);
673 }
674 eassert (b->overlays_before == NULL);
675 eassert (b->overlays_after == NULL);
676}
677
bcd40520 678/* Reinitialize everything about a buffer except its name and contents
6b61353c 679 and local variables.
d4f5719a
SM
680 If called on an already-initialized buffer, the list of overlays
681 should be deleted before calling this function, otherwise we end up
682 with overlays that claim to belong to the buffer but the buffer
683 claims it doesn't belong to it. */
1ab256cb
RM
684
685void
686reset_buffer (b)
687 register struct buffer *b;
688{
689 b->filename = Qnil;
f6ed2e84 690 b->file_truename = Qnil;
1ab256cb
RM
691 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
692 b->modtime = 0;
8d7a4592 693 XSETFASTINT (b->save_length, 0);
1ab256cb 694 b->last_window_start = 1;
8b264726 695 /* It is more conservative to start out "changed" than "unchanged". */
b5a225b4
GM
696 b->clip_changed = 0;
697 b->prevent_redisplay_optimizations_p = 1;
1ab256cb
RM
698 b->backed_up = Qnil;
699 b->auto_save_modified = 0;
84f6bcba 700 b->auto_save_failure_time = -1;
1ab256cb
RM
701 b->auto_save_file_name = Qnil;
702 b->read_only = Qnil;
2410d73a
SM
703 b->overlays_before = NULL;
704 b->overlays_after = NULL;
c2d5b10f 705 b->overlay_center = BEG;
dfda7a7f 706 b->mark_active = Qnil;
943e065b 707 b->point_before_scroll = Qnil;
be9aafdd 708 b->file_format = Qnil;
71ed49fa 709 b->auto_save_file_format = Qt;
0dc6f165 710 b->last_selected_window = Qnil;
7962a441 711 XSETINT (b->display_count, 0);
3fd364db 712 b->display_time = Qnil;
1bf08baf 713 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
bb2ec976 714 b->cursor_type = buffer_defaults.cursor_type;
a3bbced0 715 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
0522997d
RS
716
717 b->display_error_modiff = 0;
1ab256cb
RM
718}
719
bcd40520
RS
720/* Reset buffer B's local variables info.
721 Don't use this on a buffer that has already been in use;
722 it does not treat permanent locals consistently.
13de9290
RS
723 Instead, use Fkill_all_local_variables.
724
725 If PERMANENT_TOO is 1, then we reset permanent built-in
726 buffer-local variables. If PERMANENT_TOO is 0,
727 we preserve those. */
bcd40520 728
13de9290
RS
729static void
730reset_buffer_local_variables (b, permanent_too)
1ab256cb 731 register struct buffer *b;
13de9290 732 int permanent_too;
1ab256cb
RM
733{
734 register int offset;
7c02e886 735 int i;
1ab256cb
RM
736
737 /* Reset the major mode to Fundamental, together with all the
738 things that depend on the major mode.
739 default-major-mode is handled at a higher level.
740 We ignore it here. */
741 b->major_mode = Qfundamental_mode;
742 b->keymap = Qnil;
1ab256cb
RM
743 b->mode_name = QSFundamental;
744 b->minor_modes = Qnil;
3446af9c
RS
745
746 /* If the standard case table has been altered and invalidated,
747 fix up its insides first. */
748 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
749 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
750 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
751 Fset_standard_case_table (Vascii_downcase_table);
752
1ab256cb 753 b->downcase_table = Vascii_downcase_table;
1e9b6335
RS
754 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
755 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
756 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
3cb719bd 757 b->invisibility_spec = Qt;
2e716096
RS
758#ifndef DOS_NT
759 b->buffer_file_type = Qnil;
760#endif
3cb719bd 761
13de9290 762 /* Reset all (or most) per-buffer variables to their defaults. */
1ab256cb 763 b->local_var_alist = Qnil;
7313acd0 764 for (i = 0; i < last_per_buffer_idx; ++i)
7c02e886 765 if (permanent_too || buffer_permanent_local_flags[i] == 0)
7313acd0 766 SET_PER_BUFFER_VALUE_P (b, i, 0);
1ab256cb
RM
767
768 /* For each slot that has a default value,
769 copy that into the slot. */
770
7313acd0 771 for (offset = PER_BUFFER_VAR_OFFSET (name);
7c02e886
GM
772 offset < sizeof *b;
773 offset += sizeof (Lisp_Object))
aab80822 774 {
7313acd0 775 int idx = PER_BUFFER_IDX (offset);
7c02e886
GM
776 if ((idx > 0
777 && (permanent_too
778 || buffer_permanent_local_flags[idx] == 0))
779 /* Is -2 used anywhere? */
780 || idx == -2)
7313acd0 781 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
aab80822 782 }
1ab256cb
RM
783}
784
01050cb5
RM
785/* We split this away from generate-new-buffer, because rename-buffer
786 and set-visited-file-name ought to be able to use this to really
787 rename the buffer properly. */
788
789DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
efc7e75f 790 1, 2, 0,
7ee72033 791 doc: /* Return a string that is the name of no existing buffer based on NAME.
018ba359
PJ
792If there is no live buffer named NAME, then return NAME.
793Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
6b61353c 794\(starting at 2) until an unused name is found, and then return that name.
2f064abf
JB
795Optional second argument IGNORE specifies a name that is okay to use (if
796it is in the sequence to be tried) even if a buffer with that name exists. */)
7ee72033 797 (name, ignore)
c273e647 798 register Lisp_Object name, ignore;
1ab256cb
RM
799{
800 register Lisp_Object gentemp, tem;
801 int count;
802 char number[10];
803
b7826503 804 CHECK_STRING (name);
1ab256cb 805
6b61353c
KH
806 tem = Fstring_equal (name, ignore);
807 if (!NILP (tem))
808 return name;
1ab256cb 809 tem = Fget_buffer (name);
265a9e55 810 if (NILP (tem))
01050cb5 811 return name;
1ab256cb
RM
812
813 count = 1;
814 while (1)
815 {
816 sprintf (number, "<%d>", ++count);
817 gentemp = concat2 (name, build_string (number));
638e4fc3 818 tem = Fstring_equal (gentemp, ignore);
c273e647
RS
819 if (!NILP (tem))
820 return gentemp;
1ab256cb 821 tem = Fget_buffer (gentemp);
265a9e55 822 if (NILP (tem))
01050cb5 823 return gentemp;
1ab256cb
RM
824 }
825}
826
827\f
828DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
7ee72033
MB
829 doc: /* Return the name of BUFFER, as a string.
830With no argument or nil as argument, return the name of the current buffer. */)
831 (buffer)
1ab256cb
RM
832 register Lisp_Object buffer;
833{
265a9e55 834 if (NILP (buffer))
1ab256cb 835 return current_buffer->name;
b7826503 836 CHECK_BUFFER (buffer);
1ab256cb
RM
837 return XBUFFER (buffer)->name;
838}
839
840DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
7ee72033
MB
841 doc: /* Return name of file BUFFER is visiting, or nil if none.
842No argument or nil as argument means use the current buffer. */)
843 (buffer)
1ab256cb
RM
844 register Lisp_Object buffer;
845{
265a9e55 846 if (NILP (buffer))
1ab256cb 847 return current_buffer->filename;
b7826503 848 CHECK_BUFFER (buffer);
1ab256cb
RM
849 return XBUFFER (buffer)->filename;
850}
851
336cd056
RS
852DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
853 0, 1, 0,
7ee72033 854 doc: /* Return the base buffer of indirect buffer BUFFER.
5a72efd4
LT
855If BUFFER is not indirect, return nil.
856BUFFER defaults to the current buffer. */)
7ee72033 857 (buffer)
336cd056
RS
858 register Lisp_Object buffer;
859{
860 struct buffer *base;
861 Lisp_Object base_buffer;
862
863 if (NILP (buffer))
864 base = current_buffer->base_buffer;
865 else
866 {
b7826503 867 CHECK_BUFFER (buffer);
336cd056
RS
868 base = XBUFFER (buffer)->base_buffer;
869 }
870
871 if (! base)
872 return Qnil;
873 XSETBUFFER (base_buffer, base);
874 return base_buffer;
875}
876
79aa712d 877DEFUN ("buffer-local-value", Fbuffer_local_value,
177c0ea7 878 Sbuffer_local_value, 2, 2, 0,
79aa712d
RS
879 doc: /* Return the value of VARIABLE in BUFFER.
880If VARIABLE does not have a buffer-local binding in BUFFER, the value
5e2ad10b
JB
881is the default binding of the variable. */)
882 (variable, buffer)
883 register Lisp_Object variable;
79aa712d
RS
884 register Lisp_Object buffer;
885{
886 register struct buffer *buf;
887 register Lisp_Object result;
888
5e2ad10b 889 CHECK_SYMBOL (variable);
ae69175b 890 CHECK_BUFFER (buffer);
79aa712d
RS
891 buf = XBUFFER (buffer);
892
0e4d0c9a
RS
893 if (SYMBOLP (variable))
894 variable = indirect_variable (variable);
895
79aa712d 896 /* Look in local_var_list */
5e2ad10b 897 result = Fassoc (variable, buf->local_var_alist);
177c0ea7 898 if (NILP (result))
79aa712d
RS
899 {
900 int offset, idx;
901 int found = 0;
902
903 /* Look in special slots */
904 for (offset = PER_BUFFER_VAR_OFFSET (name);
905 offset < sizeof (struct buffer);
906 /* sizeof EMACS_INT == sizeof Lisp_Object */
907 offset += (sizeof (EMACS_INT)))
908 {
909 idx = PER_BUFFER_IDX (offset);
910 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
177c0ea7 911 && SYMBOLP (PER_BUFFER_SYMBOL (offset))
5e2ad10b 912 && EQ (PER_BUFFER_SYMBOL (offset), variable))
79aa712d
RS
913 {
914 result = PER_BUFFER_VALUE (buf, offset);
915 found = 1;
916 break;
917 }
918 }
919
920 if (!found)
5e2ad10b 921 result = Fdefault_value (variable);
79aa712d
RS
922 }
923 else
f0bac7de
RS
924 {
925 Lisp_Object valcontents;
926 Lisp_Object current_alist_element;
927
928 /* What binding is loaded right now? */
5e2ad10b 929 valcontents = SYMBOL_VALUE (variable);
f0bac7de
RS
930 current_alist_element
931 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
932
933 /* The value of the currently loaded binding is not
934 stored in it, but rather in the realvalue slot.
935 Store that value into the binding it belongs to
936 in case that is the one we are about to use. */
937
938 Fsetcdr (current_alist_element,
939 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
940
941 /* Now get the (perhaps updated) value out of the binding. */
942 result = XCDR (result);
943 }
79aa712d 944
4c4dc0b0
KS
945 if (!EQ (result, Qunbound))
946 return result;
79aa712d 947
4c4dc0b0 948 xsignal1 (Qvoid_variable, variable);
79aa712d
RS
949}
950
e1688f54 951/* Return an alist of the Lisp-level buffer-local bindings of
7fa57e45 952 buffer BUF. That is, don't include the variables maintained
e1688f54
RS
953 in special slots in the buffer object. */
954
955static Lisp_Object
956buffer_lisp_local_variables (buf)
957 struct buffer *buf;
958{
959 Lisp_Object result = Qnil;
960 register Lisp_Object tail;
961 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
962 {
963 Lisp_Object val, elt;
964
965 elt = XCAR (tail);
966
967 /* Reference each variable in the alist in buf.
968 If inquiring about the current buffer, this gets the current values,
969 so store them into the alist so the alist is up to date.
970 If inquiring about some other buffer, this swaps out any values
971 for that buffer, making the alist up to date automatically. */
972 val = find_symbol_value (XCAR (elt));
973 /* Use the current buffer value only if buf is the current buffer. */
974 if (buf != current_buffer)
975 val = XCDR (elt);
976
977 /* If symbol is unbound, put just the symbol in the list. */
978 if (EQ (val, Qunbound))
979 result = Fcons (XCAR (elt), result);
980 /* Otherwise, put (symbol . value) in the list. */
981 else
982 result = Fcons (Fcons (XCAR (elt), val), result);
983 }
984
985 return result;
986}
987
1ab256cb 988DEFUN ("buffer-local-variables", Fbuffer_local_variables,
efc7e75f 989 Sbuffer_local_variables, 0, 1, 0,
7ee72033 990 doc: /* Return an alist of variables that are buffer-local in BUFFER.
018ba359
PJ
991Most elements look like (SYMBOL . VALUE), describing one variable.
992For a symbol that is locally unbound, just the symbol appears in the value.
993Note that storing new VALUEs in these elements doesn't change the variables.
7ee72033
MB
994No argument or nil as argument means use current buffer as BUFFER. */)
995 (buffer)
1ab256cb
RM
996 register Lisp_Object buffer;
997{
998 register struct buffer *buf;
553defa4 999 register Lisp_Object result;
1ab256cb 1000
265a9e55 1001 if (NILP (buffer))
1ab256cb
RM
1002 buf = current_buffer;
1003 else
1004 {
b7826503 1005 CHECK_BUFFER (buffer);
1ab256cb
RM
1006 buf = XBUFFER (buffer);
1007 }
1008
e1688f54 1009 result = buffer_lisp_local_variables (buf);
1ab256cb 1010
1ab256cb
RM
1011 /* Add on all the variables stored in special slots. */
1012 {
7c02e886 1013 int offset, idx;
1ab256cb 1014
7313acd0 1015 for (offset = PER_BUFFER_VAR_OFFSET (name);
1ab256cb 1016 offset < sizeof (struct buffer);
7c02e886
GM
1017 /* sizeof EMACS_INT == sizeof Lisp_Object */
1018 offset += (sizeof (EMACS_INT)))
1ab256cb 1019 {
7313acd0
GM
1020 idx = PER_BUFFER_IDX (offset);
1021 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1022 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
1023 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
1024 PER_BUFFER_VALUE (buf, offset)),
7c02e886 1025 result);
1ab256cb
RM
1026 }
1027 }
553defa4
RS
1028
1029 return result;
1ab256cb 1030}
1ab256cb
RM
1031\f
1032DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
efc7e75f 1033 0, 1, 0,
7ee72033
MB
1034 doc: /* Return t if BUFFER was modified since its file was last read or saved.
1035No argument or nil as argument means use current buffer as BUFFER. */)
1036 (buffer)
1ab256cb
RM
1037 register Lisp_Object buffer;
1038{
1039 register struct buffer *buf;
265a9e55 1040 if (NILP (buffer))
1ab256cb
RM
1041 buf = current_buffer;
1042 else
1043 {
b7826503 1044 CHECK_BUFFER (buffer);
1ab256cb
RM
1045 buf = XBUFFER (buffer);
1046 }
1047
336cd056 1048 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1ab256cb
RM
1049}
1050
1051DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
efc7e75f 1052 1, 1, 0,
7ee72033
MB
1053 doc: /* Mark current buffer as modified or unmodified according to FLAG.
1054A non-nil FLAG means mark the buffer modified. */)
1055 (flag)
1ab256cb
RM
1056 register Lisp_Object flag;
1057{
1058 register int already;
1059 register Lisp_Object fn;
8ec01c70 1060 Lisp_Object buffer, window;
1ab256cb
RM
1061
1062#ifdef CLASH_DETECTION
1063 /* If buffer becoming modified, lock the file.
1064 If buffer becoming unmodified, unlock the file. */
1065
60f4dd23 1066 fn = current_buffer->file_truename;
90d456d2
KH
1067 /* Test buffer-file-name so that binding it to nil is effective. */
1068 if (!NILP (fn) && ! NILP (current_buffer->filename))
1ab256cb 1069 {
336cd056 1070 already = SAVE_MODIFF < MODIFF;
265a9e55 1071 if (!already && !NILP (flag))
1ab256cb 1072 lock_file (fn);
265a9e55 1073 else if (already && NILP (flag))
1ab256cb
RM
1074 unlock_file (fn);
1075 }
1076#endif /* CLASH_DETECTION */
1077
336cd056 1078 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
177c0ea7 1079
8ec01c70
GM
1080 /* Set update_mode_lines only if buffer is displayed in some window.
1081 Packages like jit-lock or lazy-lock preserve a buffer's modified
1082 state by recording/restoring the state around blocks of code.
1083 Setting update_mode_lines makes redisplay consider all windows
1084 (on all frames). Stealth fontification of buffers not displayed
1085 would incur additional redisplay costs if we'd set
1086 update_modes_lines unconditionally.
1087
1088 Ideally, I think there should be another mechanism for fontifying
1089 buffers without "modifying" buffers, or redisplay should be
1090 smarter about updating the `*' in mode lines. --gerd */
1091 XSETBUFFER (buffer, current_buffer);
1092 window = Fget_buffer_window (buffer, Qt);
1093 if (WINDOWP (window))
d57b83b3
GM
1094 {
1095 ++update_mode_lines;
1096 current_buffer->prevent_redisplay_optimizations_p = 1;
1097 }
177c0ea7 1098
1ab256cb
RM
1099 return flag;
1100}
1101
a8c21b48
GM
1102DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
1103 Srestore_buffer_modified_p, 1, 1, 0,
42f47086 1104 doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay.
018ba359 1105It is not ensured that mode lines will be updated to show the modified
7ee72033
MB
1106state of the current buffer. Use with care. */)
1107 (flag)
a8c21b48
GM
1108 Lisp_Object flag;
1109{
1110#ifdef CLASH_DETECTION
1111 Lisp_Object fn;
177c0ea7 1112
a8c21b48
GM
1113 /* If buffer becoming modified, lock the file.
1114 If buffer becoming unmodified, unlock the file. */
1115
1116 fn = current_buffer->file_truename;
1117 /* Test buffer-file-name so that binding it to nil is effective. */
1118 if (!NILP (fn) && ! NILP (current_buffer->filename))
1119 {
1120 int already = SAVE_MODIFF < MODIFF;
1121 if (!already && !NILP (flag))
1122 lock_file (fn);
1123 else if (already && NILP (flag))
1124 unlock_file (fn);
1125 }
1126#endif /* CLASH_DETECTION */
177c0ea7 1127
a8c21b48
GM
1128 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1129 return flag;
1130}
1131
1ab256cb 1132DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
efc7e75f 1133 0, 1, 0,
7ee72033 1134 doc: /* Return BUFFER's tick counter, incremented for each change in text.
2f064abf
JB
1135Each buffer has a tick counter which is incremented each time the
1136text in that buffer is changed. It wraps around occasionally.
7ee72033
MB
1137No argument or nil as argument means use current buffer as BUFFER. */)
1138 (buffer)
1ab256cb
RM
1139 register Lisp_Object buffer;
1140{
1141 register struct buffer *buf;
265a9e55 1142 if (NILP (buffer))
1ab256cb
RM
1143 buf = current_buffer;
1144 else
1145 {
b7826503 1146 CHECK_BUFFER (buffer);
1ab256cb
RM
1147 buf = XBUFFER (buffer);
1148 }
1149
1150 return make_number (BUF_MODIFF (buf));
1151}
3e145152
CY
1152
1153DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
1154 Sbuffer_chars_modified_tick, 0, 1, 0,
1155 doc: /* Return BUFFER's character-change tick counter.
1156Each buffer has a character-change tick counter, which is set to the
1157value of the buffer's tick counter \(see `buffer-modified-tick'), each
1158time text in that buffer is inserted or deleted. By comparing the
12bd42be 1159values returned by two individual calls of `buffer-chars-modified-tick',
3e145152
CY
1160you can tell whether a character change occurred in that buffer in
1161between these calls. No argument or nil as argument means use current
1162buffer as BUFFER. */)
1163 (buffer)
1164 register Lisp_Object buffer;
1165{
1166 register struct buffer *buf;
1167 if (NILP (buffer))
1168 buf = current_buffer;
1169 else
1170 {
1171 CHECK_BUFFER (buffer);
1172 buf = XBUFFER (buffer);
1173 }
1174
1175 return make_number (BUF_CHARS_MODIFF (buf));
1176}
1ab256cb 1177\f
01050cb5 1178DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
4c7e5f09 1179 "sRename buffer (to new name): \nP",
7ee72033 1180 doc: /* Change current buffer's name to NEWNAME (a string).
018ba359
PJ
1181If second arg UNIQUE is nil or omitted, it is an error if a
1182buffer named NEWNAME already exists.
1183If UNIQUE is non-nil, come up with a new name using
1184`generate-new-buffer-name'.
1185Interactively, you can set UNIQUE with a prefix argument.
1186We return the name we actually gave the buffer.
7ee72033
MB
1187This does not change the name of the visited file (if any). */)
1188 (newname, unique)
489c043a 1189 register Lisp_Object newname, unique;
1ab256cb
RM
1190{
1191 register Lisp_Object tem, buf;
1192
b7826503 1193 CHECK_STRING (newname);
d59698c4 1194
d5db4077 1195 if (SCHARS (newname) == 0)
d59698c4
RS
1196 error ("Empty string is invalid as a buffer name");
1197
489c043a 1198 tem = Fget_buffer (newname);
265a9e55 1199 if (!NILP (tem))
01050cb5 1200 {
8801a864
KR
1201 /* Don't short-circuit if UNIQUE is t. That is a useful way to
1202 rename the buffer automatically so you can create another
1203 with the original name. It makes UNIQUE equivalent to
1204 (rename-buffer (generate-new-buffer-name NEWNAME)). */
1205 if (NILP (unique) && XBUFFER (tem) == current_buffer)
1206 return current_buffer->name;
3bd779aa 1207 if (!NILP (unique))
489c043a 1208 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
01050cb5 1209 else
d5db4077 1210 error ("Buffer name `%s' is in use", SDATA (newname));
01050cb5 1211 }
1ab256cb 1212
489c043a 1213 current_buffer->name = newname;
76f590d7
JB
1214
1215 /* Catch redisplay's attention. Unless we do this, the mode lines for
1216 any windows displaying current_buffer will stay unchanged. */
1217 update_mode_lines++;
1218
67180c6a 1219 XSETBUFFER (buf, current_buffer);
489c043a 1220 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
cf058e49
KH
1221 if (NILP (current_buffer->filename)
1222 && !NILP (current_buffer->auto_save_file_name))
1ab256cb 1223 call0 (intern ("rename-auto-save-file"));
fb5eba9c
RS
1224 /* Refetch since that last call may have done GC. */
1225 return current_buffer->name;
1ab256cb
RM
1226}
1227
773fbdb9 1228DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
7ee72033 1229 doc: /* Return most recently selected buffer other than BUFFER.
018ba359
PJ
1230Buffers not visible in windows are preferred to visible buffers,
1231unless optional second argument VISIBLE-OK is non-nil.
1232If the optional third argument FRAME is non-nil, use that frame's
1233buffer list instead of the selected frame's buffer list.
1234If no other buffer exists, the buffer `*scratch*' is returned.
7ee72033
MB
1235If BUFFER is omitted or nil, some interesting buffer is returned. */)
1236 (buffer, visible_ok, frame)
773fbdb9 1237 register Lisp_Object buffer, visible_ok, frame;
1ab256cb 1238{
89132f25 1239 Lisp_Object Fset_buffer_major_mode ();
7962a441 1240 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1ab256cb
RM
1241 notsogood = Qnil;
1242
773fbdb9 1243 if (NILP (frame))
9ba9623d 1244 frame = selected_frame;
773fbdb9 1245
7962a441 1246 tail = Vbuffer_alist;
773fbdb9 1247 pred = frame_buffer_predicate (frame);
7962a441
RS
1248
1249 /* Consider buffers that have been seen in the selected frame
1250 before other buffers. */
177c0ea7 1251
773fbdb9 1252 tem = frame_buffer_list (frame);
7962a441
RS
1253 add_ons = Qnil;
1254 while (CONSP (tem))
1255 {
7539e11f
KR
1256 if (BUFFERP (XCAR (tem)))
1257 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1258 tem = XCDR (tem);
7962a441
RS
1259 }
1260 tail = nconc2 (Fnreverse (add_ons), tail);
1261
6d70a280 1262 for (; CONSP (tail); tail = XCDR (tail))
1ab256cb 1263 {
6d70a280 1264 buf = Fcdr (XCAR (tail));
1ab256cb
RM
1265 if (EQ (buf, buffer))
1266 continue;
6b61353c
KH
1267 if (NILP (buf))
1268 continue;
1269 if (NILP (XBUFFER (buf)->name))
1270 continue;
10ceceb8 1271 if (SREF (XBUFFER (buf)->name, 0) == ' ')
1ab256cb 1272 continue;
04ae1b48
RS
1273 /* If the selected frame has a buffer_predicate,
1274 disregard buffers that don't fit the predicate. */
7962a441 1275 if (!NILP (pred))
04ae1b48 1276 {
7962a441 1277 tem = call1 (pred, buf);
04ae1b48
RS
1278 if (NILP (tem))
1279 continue;
1280 }
04ae1b48 1281
a0ebb746 1282 if (NILP (visible_ok))
66ffe51c 1283 tem = Fget_buffer_window (buf, Qvisible);
a0ebb746
JB
1284 else
1285 tem = Qnil;
265a9e55 1286 if (NILP (tem))
1ab256cb 1287 return buf;
265a9e55 1288 if (NILP (notsogood))
1ab256cb
RM
1289 notsogood = buf;
1290 }
265a9e55 1291 if (!NILP (notsogood))
1ab256cb 1292 return notsogood;
dba1a30a
GM
1293 buf = Fget_buffer (build_string ("*scratch*"));
1294 if (NILP (buf))
1295 {
1296 buf = Fget_buffer_create (build_string ("*scratch*"));
1297 Fset_buffer_major_mode (buf);
1298 }
89132f25 1299 return buf;
1ab256cb
RM
1300}
1301\f
1ab256cb
RM
1302DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1303 0, 1, "",
7ee72033
MB
1304 doc: /* Start keeping undo information for buffer BUFFER.
1305No argument or nil as argument means do this for the current buffer. */)
1306 (buffer)
ffd56f97 1307 register Lisp_Object buffer;
1ab256cb 1308{
ffd56f97 1309 Lisp_Object real_buffer;
1ab256cb 1310
ffd56f97 1311 if (NILP (buffer))
67180c6a 1312 XSETBUFFER (real_buffer, current_buffer);
1ab256cb
RM
1313 else
1314 {
ffd56f97
JB
1315 real_buffer = Fget_buffer (buffer);
1316 if (NILP (real_buffer))
1317 nsberror (buffer);
1ab256cb
RM
1318 }
1319
ffd56f97
JB
1320 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1321 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
1322
1323 return Qnil;
1324}
1325
1326/*
1327 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1328Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1329The buffer being killed will be current while the hook is running.\n\
1330See `kill-buffer'."
1331 */
1332DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
7ee72033 1333 doc: /* Kill the buffer BUFFER.
f6e22881
JB
1334The argument may be a buffer or the name of a buffer.
1335With a nil argument, kill the current buffer.
018ba359 1336
f6e22881 1337Value is t if the buffer is actually killed, nil otherwise.
018ba359 1338
f6e22881
JB
1339The functions in `kill-buffer-query-functions' are called with BUFFER as
1340the current buffer. If any of them returns nil, the buffer is not killed.
1341
1342The hook `kill-buffer-hook' is run before the buffer is actually killed.
1343The buffer being killed will be current while the hook is running.
018ba359
PJ
1344
1345Any processes that have this buffer as the `process-buffer' are killed
7ee72033
MB
1346with SIGHUP. */)
1347 (buffer)
a25f13ae 1348 Lisp_Object buffer;
1ab256cb
RM
1349{
1350 Lisp_Object buf;
1351 register struct buffer *b;
1352 register Lisp_Object tem;
1353 register struct Lisp_Marker *m;
6af718a4 1354 struct gcpro gcpro1;
1ab256cb 1355
a25f13ae 1356 if (NILP (buffer))
1ab256cb
RM
1357 buf = Fcurrent_buffer ();
1358 else
a25f13ae 1359 buf = Fget_buffer (buffer);
265a9e55 1360 if (NILP (buf))
a25f13ae 1361 nsberror (buffer);
1ab256cb
RM
1362
1363 b = XBUFFER (buf);
1364
4a4a9db5
KH
1365 /* Avoid trouble for buffer already dead. */
1366 if (NILP (b->name))
1367 return Qnil;
1368
1ab256cb 1369 /* Query if the buffer is still modified. */
265a9e55 1370 if (INTERACTIVE && !NILP (b->filename)
336cd056 1371 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1ab256cb 1372 {
a25f13ae 1373 GCPRO1 (buf);
5c4930b0
RS
1374 tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
1375 b->name, make_number (0)));
1ab256cb 1376 UNGCPRO;
265a9e55 1377 if (NILP (tem))
1ab256cb
RM
1378 return Qnil;
1379 }
1380
dcdffbf6 1381 /* Run hooks with the buffer to be killed the current buffer. */
1ab256cb 1382 {
aed13378 1383 int count = SPECPDL_INDEX ();
5b20caf0 1384 Lisp_Object arglist[1];
1ab256cb
RM
1385
1386 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1387 set_buffer_internal (b);
dcdffbf6
RS
1388
1389 /* First run the query functions; if any query is answered no,
1390 don't kill the buffer. */
5b20caf0 1391 arglist[0] = Qkill_buffer_query_functions;
09706e1f
KR
1392 tem = Frun_hook_with_args_until_failure (1, arglist);
1393 if (NILP (tem))
5b20caf0 1394 return unbind_to (count, Qnil);
dcdffbf6
RS
1395
1396 /* Then run the hooks. */
f1597a3a 1397 Frun_hooks (1, &Qkill_buffer_hook);
1ab256cb
RM
1398 unbind_to (count, Qnil);
1399 }
1400
1401 /* We have no more questions to ask. Verify that it is valid
1402 to kill the buffer. This must be done after the questions
1403 since anything can happen within do_yes_or_no_p. */
1404
1405 /* Don't kill the minibuffer now current. */
1406 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1407 return Qnil;
1408
265a9e55 1409 if (NILP (b->name))
1ab256cb
RM
1410 return Qnil;
1411
336cd056
RS
1412 /* When we kill a base buffer, kill all its indirect buffers.
1413 We do it at this stage so nothing terrible happens if they
1414 ask questions or their hooks get errors. */
1415 if (! b->base_buffer)
1416 {
1417 struct buffer *other;
1418
1419 GCPRO1 (buf);
1420
1421 for (other = all_buffers; other; other = other->next)
4a4a9db5
KH
1422 /* all_buffers contains dead buffers too;
1423 don't re-kill them. */
1424 if (other->base_buffer == b && !NILP (other->name))
336cd056
RS
1425 {
1426 Lisp_Object buf;
1427 XSETBUFFER (buf, other);
1428 Fkill_buffer (buf);
1429 }
1430
1431 UNGCPRO;
1432 }
177c0ea7 1433
1ab256cb
RM
1434 /* Make this buffer not be current.
1435 In the process, notice if this is the sole visible buffer
1436 and give up if so. */
1437 if (b == current_buffer)
1438 {
773fbdb9 1439 tem = Fother_buffer (buf, Qnil, Qnil);
1ab256cb
RM
1440 Fset_buffer (tem);
1441 if (b == current_buffer)
1442 return Qnil;
1443 }
1444
77270fac
GM
1445 /* Notice if the buffer to kill is the sole visible buffer
1446 when we're currently in the mini-buffer, and give up if so. */
1447 XSETBUFFER (tem, current_buffer);
1448 if (EQ (tem, XWINDOW (minibuf_window)->buffer))
1449 {
1450 tem = Fother_buffer (buf, Qnil, Qnil);
77270fac
GM
1451 if (EQ (buf, tem))
1452 return Qnil;
1453 }
1454
1ab256cb
RM
1455 /* Now there is no question: we can kill the buffer. */
1456
1457#ifdef CLASH_DETECTION
1458 /* Unlock this buffer's file, if it is locked. */
1459 unlock_buffer (b);
1460#endif /* CLASH_DETECTION */
1461
49da74e6 1462 GCPRO1 (buf);
1ab256cb 1463 kill_buffer_processes (buf);
49da74e6
KS
1464 UNGCPRO;
1465
1466 /* Killing buffer processes may run sentinels which may
1467 have called kill-buffer. */
1468
1469 if (NILP (b->name))
1470 return Qnil;
1471
b93fb365 1472 clear_charpos_cache (b);
1ab256cb
RM
1473
1474 tem = Vinhibit_quit;
1475 Vinhibit_quit = Qt;
00550f94 1476 replace_buffer_in_all_windows (buf);
b26dd9cb 1477 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
7962a441 1478 frames_discard_buffer (buf);
1ab256cb
RM
1479 Vinhibit_quit = tem;
1480
9cf712eb
RS
1481 /* Delete any auto-save file, if we saved it in this session.
1482 But not if the buffer is modified. */
a7a60ce9 1483 if (STRINGP (b->auto_save_file_name)
e95a0b39 1484 && b->auto_save_modified != 0
9cf712eb 1485 && BUF_SAVE_MODIFF (b) < b->auto_save_modified
6b61353c
KH
1486 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
1487 && NILP (Fsymbol_value (intern ("auto-save-visited-file-name"))))
1ab256cb
RM
1488 {
1489 Lisp_Object tem;
1490 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 1491 if (! NILP (tem))
cbb6a418 1492 internal_delete_file (b->auto_save_file_name);
1ab256cb
RM
1493 }
1494
4a4a9db5
KH
1495 if (b->base_buffer)
1496 {
1497 /* Unchain all markers that belong to this indirect buffer.
1498 Don't unchain the markers that belong to the base buffer
1499 or its other indirect buffers. */
65745fad 1500 for (m = BUF_MARKERS (b); m; )
4a4a9db5 1501 {
65745fad 1502 struct Lisp_Marker *next = m->next;
4a4a9db5 1503 if (m->buffer == b)
65745fad
SM
1504 unchain_marker (m);
1505 m = next;
4a4a9db5
KH
1506 }
1507 }
1508 else
1ab256cb 1509 {
4a4a9db5 1510 /* Unchain all markers of this buffer and its indirect buffers.
336cd056 1511 and leave them pointing nowhere. */
65745fad 1512 for (m = BUF_MARKERS (b); m; )
336cd056 1513 {
65745fad 1514 struct Lisp_Marker *next = m->next;
336cd056 1515 m->buffer = 0;
65745fad
SM
1516 m->next = NULL;
1517 m = next;
336cd056 1518 }
65745fad 1519 BUF_MARKERS (b) = NULL;
336cd056 1520 BUF_INTERVALS (b) = NULL_INTERVAL;
336cd056
RS
1521
1522 /* Perhaps we should explicitly free the interval tree here... */
1523 }
33f7013e 1524
2f3f993b
RS
1525 /* Reset the local variables, so that this buffer's local values
1526 won't be protected from GC. They would be protected
1527 if they happened to remain encached in their symbols.
1528 This gets rid of them for certain. */
1529 swap_out_buffer_local_variables (b);
13de9290 1530 reset_buffer_local_variables (b, 1);
2f3f993b 1531
1ab256cb 1532 b->name = Qnil;
336cd056 1533
9ac0d9e0 1534 BLOCK_INPUT;
336cd056 1535 if (! b->base_buffer)
b86af064 1536 free_buffer_text (b);
336cd056 1537
28e969dd
JB
1538 if (b->newline_cache)
1539 {
1540 free_region_cache (b->newline_cache);
1541 b->newline_cache = 0;
1542 }
1543 if (b->width_run_cache)
1544 {
1545 free_region_cache (b->width_run_cache);
1546 b->width_run_cache = 0;
1547 }
1548 b->width_table = Qnil;
9ac0d9e0 1549 UNBLOCK_INPUT;
1ab256cb
RM
1550 b->undo_list = Qnil;
1551
1552 return Qt;
1553}
1554\f
36a8c287
JB
1555/* Move the assoc for buffer BUF to the front of buffer-alist. Since
1556 we do this each time BUF is selected visibly, the more recently
1557 selected buffers are always closer to the front of the list. This
1558 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb 1559
01136e9b 1560void
1ab256cb
RM
1561record_buffer (buf)
1562 Lisp_Object buf;
1563{
1564 register Lisp_Object link, prev;
773fbdb9 1565 Lisp_Object frame;
9ba9623d 1566 frame = selected_frame;
1ab256cb
RM
1567
1568 prev = Qnil;
7539e11f 1569 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1ab256cb 1570 {
7539e11f 1571 if (EQ (XCDR (XCAR (link)), buf))
1ab256cb
RM
1572 break;
1573 prev = link;
1574 }
1575
36a8c287
JB
1576 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1577 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 1578
265a9e55 1579 if (NILP (prev))
7539e11f 1580 Vbuffer_alist = XCDR (Vbuffer_alist);
1ab256cb 1581 else
f3fbd155 1582 XSETCDR (prev, XCDR (XCDR (prev)));
177c0ea7 1583
f3fbd155 1584 XSETCDR (link, Vbuffer_alist);
1ab256cb 1585 Vbuffer_alist = link;
7962a441
RS
1586
1587 /* Now move this buffer to the front of frame_buffer_list also. */
1588
1589 prev = Qnil;
773fbdb9 1590 for (link = frame_buffer_list (frame); CONSP (link);
7539e11f 1591 link = XCDR (link))
7962a441 1592 {
7539e11f 1593 if (EQ (XCAR (link), buf))
7962a441
RS
1594 break;
1595 prev = link;
1596 }
1597
1598 /* Effectively do delq. */
1599
1600 if (CONSP (link))
1601 {
1602 if (NILP (prev))
773fbdb9 1603 set_frame_buffer_list (frame,
7539e11f 1604 XCDR (frame_buffer_list (frame)));
7962a441 1605 else
f3fbd155 1606 XSETCDR (prev, XCDR (XCDR (prev)));
177c0ea7 1607
f3fbd155 1608 XSETCDR (link, frame_buffer_list (frame));
773fbdb9 1609 set_frame_buffer_list (frame, link);
7962a441
RS
1610 }
1611 else
773fbdb9 1612 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1ab256cb
RM
1613}
1614
a9ee7a59 1615DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
7ee72033 1616 doc: /* Set an appropriate major mode for BUFFER.
864b90c9 1617For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
71a0f2c6 1618according to `default-major-mode'.
018ba359 1619Use this function before selecting the buffer, since it may need to inspect
7ee72033
MB
1620the current buffer's major mode. */)
1621 (buffer)
a2428fa2 1622 Lisp_Object buffer;
a9ee7a59
KH
1623{
1624 int count;
1625 Lisp_Object function;
1626
ea4fddd8
JB
1627 CHECK_BUFFER (buffer);
1628
71a0f2c6 1629 if (STRINGP (XBUFFER (buffer)->name)
d5db4077 1630 && strcmp (SDATA (XBUFFER (buffer)->name), "*scratch*") == 0)
71a0f2c6
GM
1631 function = find_symbol_value (intern ("initial-major-mode"));
1632 else
1633 {
1634 function = buffer_defaults.major_mode;
1635 if (NILP (function)
1636 && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1637 function = current_buffer->major_mode;
1638 }
177c0ea7 1639
48265e61
DL
1640 if (NILP (function) || EQ (function, Qfundamental_mode))
1641 return Qnil;
1642
aed13378 1643 count = SPECPDL_INDEX ();
a9ee7a59 1644
48265e61
DL
1645 /* To select a nonfundamental mode,
1646 select the buffer temporarily and then call the mode function. */
a9ee7a59
KH
1647
1648 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1649
a2428fa2 1650 Fset_buffer (buffer);
48265e61 1651 call0 (function);
a9ee7a59
KH
1652
1653 return unbind_to (count, Qnil);
1654}
1655
2594e0fd
RS
1656/* If switching buffers in WINDOW would be an error, return
1657 a C string saying what the error would be. */
1658
1659char *
1660no_switch_window (window)
1661 Lisp_Object window;
1ab256cb 1662{
1ab256cb 1663 Lisp_Object tem;
2594e0fd
RS
1664 if (EQ (minibuf_window, window))
1665 return "Cannot switch buffers in minibuffer window";
1666 tem = Fwindow_dedicated_p (window);
c01d0677 1667 if (EQ (tem, Qt))
2594e0fd
RS
1668 return "Cannot switch buffers in a dedicated window";
1669 return NULL;
1670}
1671
1672/* Switch to buffer BUFFER in the selected window.
1673 If NORECORD is non-nil, don't call record_buffer. */
1674
1675Lisp_Object
1676switch_to_buffer_1 (buffer, norecord)
1677 Lisp_Object buffer, norecord;
1678{
1679 register Lisp_Object buf;
1ab256cb 1680
a25f13ae 1681 if (NILP (buffer))
773fbdb9 1682 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1ab256cb 1683 else
a9ee7a59 1684 {
a25f13ae 1685 buf = Fget_buffer (buffer);
a9ee7a59
KH
1686 if (NILP (buf))
1687 {
a25f13ae 1688 buf = Fget_buffer_create (buffer);
a9ee7a59
KH
1689 Fset_buffer_major_mode (buf);
1690 }
1691 }
1ab256cb 1692 Fset_buffer (buf);
265a9e55 1693 if (NILP (norecord))
1ab256cb
RM
1694 record_buffer (buf);
1695
1696 Fset_window_buffer (EQ (selected_window, minibuf_window)
5fcd022d
JB
1697 ? Fnext_window (minibuf_window, Qnil, Qnil)
1698 : selected_window,
2ad8731a 1699 buf, Qnil);
1ab256cb 1700
cd0c235a 1701 return buf;
1ab256cb
RM
1702}
1703
2594e0fd 1704DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
7ee72033 1705 doc: /* Select buffer BUFFER in the current window.
a6dced2a
LT
1706If BUFFER does not identify an existing buffer,
1707then this function creates a buffer with that name.
1708
1709When called from Lisp, BUFFER may be a buffer, a string \(a buffer name),
1710or nil. If BUFFER is nil, then this function chooses a buffer
1711using `other-buffer'.
018ba359
PJ
1712Optional second arg NORECORD non-nil means
1713do not put this buffer at the front of the list of recently selected ones.
a6dced2a 1714This function returns the buffer it switched to.
018ba359
PJ
1715
1716WARNING: This is NOT the way to work on another buffer temporarily
1717within a Lisp program! Use `set-buffer' instead. That avoids messing with
7ee72033
MB
1718the window-buffer correspondences. */)
1719 (buffer, norecord)
2594e0fd
RS
1720 Lisp_Object buffer, norecord;
1721{
1722 char *err;
1723
27c6b98e 1724 if (EQ (buffer, Fwindow_buffer (selected_window)))
fab45703 1725 {
611ac521
RS
1726 /* Basically a NOP. Avoid signalling an error in the case where
1727 the selected window is dedicated, or a minibuffer. */
1728
1729 /* But do put this buffer at the front of the buffer list,
1730 unless that has been inhibited. Note that even if
1731 BUFFER is at the front of the main buffer-list already,
1732 we still want to move it to the front of the frame's buffer list. */
1733 if (NILP (norecord))
fab45703 1734 record_buffer (buffer);
fab45703
NR
1735 return Fset_buffer (buffer);
1736 }
27c6b98e 1737
2594e0fd
RS
1738 err = no_switch_window (selected_window);
1739 if (err) error (err);
1740
1741 return switch_to_buffer_1 (buffer, norecord);
1742}
1743
cd0c235a 1744DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
7ee72033 1745 doc: /* Select buffer BUFFER in some window, preferably a different one.
a6dced2a
LT
1746BUFFER may be a buffer, a string \(a buffer name), or nil.
1747If BUFFER is a string which is not the name of an existing buffer,
1748then this function creates a buffer with that name.
1749If BUFFER is nil, then it chooses some other buffer.
018ba359 1750If `pop-up-windows' is non-nil, windows can be split to do this.
e198ba87 1751If optional second arg OTHER-WINDOW is non-nil, insist on finding another
6b61353c
KH
1752window even if BUFFER is already visible in the selected window,
1753and ignore `same-window-regexps' and `same-window-buffer-names'.
a6dced2a 1754This function returns the buffer it switched to.
018ba359
PJ
1755This uses the function `display-buffer' as a subroutine; see the documentation
1756of `display-buffer' for additional customization information.
1757
1758Optional third arg NORECORD non-nil means
7ee72033
MB
1759do not put this buffer at the front of the list of recently selected ones. */)
1760 (buffer, other_window, norecord)
6d12711f 1761 Lisp_Object buffer, other_window, norecord;
1ab256cb
RM
1762{
1763 register Lisp_Object buf;
a25f13ae 1764 if (NILP (buffer))
773fbdb9 1765 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1ab256cb 1766 else
7c2087ee 1767 {
a25f13ae 1768 buf = Fget_buffer (buffer);
7c2087ee
RS
1769 if (NILP (buf))
1770 {
a25f13ae 1771 buf = Fget_buffer_create (buffer);
7c2087ee
RS
1772 Fset_buffer_major_mode (buf);
1773 }
1774 }
1ab256cb 1775 Fset_buffer (buf);
f1321dc3 1776 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil), norecord);
e8b3a22d 1777 return buf;
1ab256cb
RM
1778}
1779
1780DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
7ee72033
MB
1781 doc: /* Return the current buffer as a Lisp object. */)
1782 ()
1ab256cb
RM
1783{
1784 register Lisp_Object buf;
67180c6a 1785 XSETBUFFER (buf, current_buffer);
1ab256cb
RM
1786 return buf;
1787}
1788\f
7ec44ec6
GM
1789/* Set the current buffer to B.
1790
1791 We previously set windows_or_buffers_changed here to invalidate
1792 global unchanged information in beg_unchanged and end_unchanged.
1793 This is no longer necessary because we now compute unchanged
1794 information on a buffer-basis. Every action affecting other
1795 windows than the selected one requires a select_window at some
1796 time, and that increments windows_or_buffers_changed. */
1ab256cb
RM
1797
1798void
1799set_buffer_internal (b)
1800 register struct buffer *b;
1801{
b5a225b4 1802 if (current_buffer != b)
7ec44ec6 1803 set_buffer_internal_1 (b);
c7aa5005
KH
1804}
1805
1806/* Set the current buffer to B, and do not set windows_or_buffers_changed.
1807 This is used by redisplay. */
1808
1809void
1810set_buffer_internal_1 (b)
1811 register struct buffer *b;
1812{
1813 register struct buffer *old_buf;
1814 register Lisp_Object tail, valcontents;
1815 Lisp_Object tem;
1816
b86af064 1817#ifdef USE_MMAP_FOR_BUFFERS
684b01ee 1818 if (b->text->beg == NULL)
b86af064
GM
1819 enlarge_buffer_text (b, 0);
1820#endif /* USE_MMAP_FOR_BUFFERS */
177c0ea7 1821
c7aa5005
KH
1822 if (current_buffer == b)
1823 return;
1824
1ab256cb
RM
1825 old_buf = current_buffer;
1826 current_buffer = b;
1827 last_known_column_point = -1; /* invalidate indentation cache */
1828
336cd056
RS
1829 if (old_buf)
1830 {
1831 /* Put the undo list back in the base buffer, so that it appears
1832 that an indirect buffer shares the undo list of its base. */
1833 if (old_buf->base_buffer)
1834 old_buf->base_buffer->undo_list = old_buf->undo_list;
1835
1836 /* If the old current buffer has markers to record PT, BEGV and ZV
1837 when it is not current, update them now. */
1838 if (! NILP (old_buf->pt_marker))
1839 {
1840 Lisp_Object obuf;
1841 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1842 set_marker_both (old_buf->pt_marker, obuf,
1843 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
336cd056
RS
1844 }
1845 if (! NILP (old_buf->begv_marker))
1846 {
1847 Lisp_Object obuf;
1848 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1849 set_marker_both (old_buf->begv_marker, obuf,
1850 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
336cd056
RS
1851 }
1852 if (! NILP (old_buf->zv_marker))
1853 {
1854 Lisp_Object obuf;
1855 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1856 set_marker_both (old_buf->zv_marker, obuf,
1857 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
336cd056
RS
1858 }
1859 }
1860
1861 /* Get the undo list from the base buffer, so that it appears
1862 that an indirect buffer shares the undo list of its base. */
1863 if (b->base_buffer)
1864 b->undo_list = b->base_buffer->undo_list;
1865
1866 /* If the new current buffer has markers to record PT, BEGV and ZV
1867 when it is not current, fetch them now. */
1868 if (! NILP (b->pt_marker))
3f236a40
RS
1869 {
1870 BUF_PT (b) = marker_position (b->pt_marker);
1871 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1872 }
336cd056 1873 if (! NILP (b->begv_marker))
3f236a40
RS
1874 {
1875 BUF_BEGV (b) = marker_position (b->begv_marker);
1876 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1877 }
336cd056 1878 if (! NILP (b->zv_marker))
3f236a40
RS
1879 {
1880 BUF_ZV (b) = marker_position (b->zv_marker);
1881 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1882 }
336cd056 1883
1ab256cb
RM
1884 /* Look down buffer's list of local Lisp variables
1885 to find and update any that forward into C variables. */
1886
65745fad 1887 for (tail = b->local_var_alist; CONSP (tail); tail = XCDR (tail))
1ab256cb 1888 {
f5c1dd0d 1889 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
a7a60ce9
KH
1890 if ((BUFFER_LOCAL_VALUEP (valcontents)
1891 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
3d871c85 1892 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
a7a60ce9 1893 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1894 /* Just reference the variable
1895 to cause it to become set for this buffer. */
7539e11f 1896 Fsymbol_value (XCAR (XCAR (tail)));
1ab256cb
RM
1897 }
1898
1899 /* Do the same with any others that were local to the previous buffer */
1900
1901 if (old_buf)
65745fad 1902 for (tail = old_buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1ab256cb 1903 {
f5c1dd0d 1904 valcontents = SYMBOL_VALUE (XCAR (XCAR (tail)));
a7a60ce9
KH
1905 if ((BUFFER_LOCAL_VALUEP (valcontents)
1906 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
3d871c85 1907 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
a7a60ce9 1908 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1909 /* Just reference the variable
1910 to cause it to become set for this buffer. */
7539e11f 1911 Fsymbol_value (XCAR (XCAR (tail)));
1ab256cb
RM
1912 }
1913}
1914
336cd056 1915/* Switch to buffer B temporarily for redisplay purposes.
bbbe9545 1916 This avoids certain things that don't need to be done within redisplay. */
336cd056
RS
1917
1918void
1919set_buffer_temp (b)
1920 struct buffer *b;
1921{
1922 register struct buffer *old_buf;
1923
1924 if (current_buffer == b)
1925 return;
1926
1927 old_buf = current_buffer;
1928 current_buffer = b;
1929
1930 if (old_buf)
1931 {
1932 /* If the old current buffer has markers to record PT, BEGV and ZV
1933 when it is not current, update them now. */
1934 if (! NILP (old_buf->pt_marker))
1935 {
1936 Lisp_Object obuf;
1937 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1938 set_marker_both (old_buf->pt_marker, obuf,
1939 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
336cd056
RS
1940 }
1941 if (! NILP (old_buf->begv_marker))
1942 {
1943 Lisp_Object obuf;
1944 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1945 set_marker_both (old_buf->begv_marker, obuf,
1946 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
336cd056
RS
1947 }
1948 if (! NILP (old_buf->zv_marker))
1949 {
1950 Lisp_Object obuf;
1951 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1952 set_marker_both (old_buf->zv_marker, obuf,
1953 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
336cd056
RS
1954 }
1955 }
1956
1957 /* If the new current buffer has markers to record PT, BEGV and ZV
1958 when it is not current, fetch them now. */
1959 if (! NILP (b->pt_marker))
3f236a40
RS
1960 {
1961 BUF_PT (b) = marker_position (b->pt_marker);
1962 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1963 }
336cd056 1964 if (! NILP (b->begv_marker))
3f236a40
RS
1965 {
1966 BUF_BEGV (b) = marker_position (b->begv_marker);
1967 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1968 }
336cd056 1969 if (! NILP (b->zv_marker))
3f236a40
RS
1970 {
1971 BUF_ZV (b) = marker_position (b->zv_marker);
1972 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1973 }
336cd056
RS
1974}
1975
1ab256cb 1976DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
7ee72033 1977 doc: /* Make the buffer BUFFER current for editing operations.
018ba359
PJ
1978BUFFER may be a buffer or the name of an existing buffer.
1979See also `save-excursion' when you want to make a buffer current temporarily.
1980This function does not display the buffer, so its effect ends
1981when the current command terminates.
7ee72033
MB
1982Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently. */)
1983 (buffer)
a25f13ae 1984 register Lisp_Object buffer;
1ab256cb 1985{
a25f13ae
KH
1986 register Lisp_Object buf;
1987 buf = Fget_buffer (buffer);
1988 if (NILP (buf))
1989 nsberror (buffer);
1990 if (NILP (XBUFFER (buf)->name))
1ab256cb 1991 error ("Selecting deleted buffer");
a25f13ae
KH
1992 set_buffer_internal (XBUFFER (buf));
1993 return buf;
1ab256cb 1994}
d0628b06
RS
1995
1996/* Set the current buffer to BUFFER provided it is alive. */
1997
1998Lisp_Object
1999set_buffer_if_live (buffer)
2000 Lisp_Object buffer;
2001{
2002 if (! NILP (XBUFFER (buffer)->name))
2003 Fset_buffer (buffer);
2004 return Qnil;
2005}
1ab256cb
RM
2006\f
2007DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
2008 Sbarf_if_buffer_read_only, 0, 0, 0,
7ee72033
MB
2009 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
2010 ()
1ab256cb 2011{
a96b68f1
RS
2012 if (!NILP (current_buffer->read_only)
2013 && NILP (Vinhibit_read_only))
4c4dc0b0 2014 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1ab256cb
RM
2015 return Qnil;
2016}
2017
2018DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
7ee72033 2019 doc: /* Put BUFFER at the end of the list of all buffers.
018ba359
PJ
2020There it is the least likely candidate for `other-buffer' to return;
2021thus, the least likely buffer for \\[switch-to-buffer] to select by default.
39971188 2022You can specify a buffer name as BUFFER, or an actual buffer object.
018ba359
PJ
2023If BUFFER is nil or omitted, bury the current buffer.
2024Also, if BUFFER is nil or omitted, remove the current buffer from the
7ee72033
MB
2025selected window if it is displayed there. */)
2026 (buffer)
a2428fa2 2027 register Lisp_Object buffer;
1ab256cb 2028{
b271272a 2029 /* Figure out what buffer we're going to bury. */
a2428fa2 2030 if (NILP (buffer))
a5611885 2031 {
c58dab63 2032 Lisp_Object tem;
a2428fa2 2033 XSETBUFFER (buffer, current_buffer);
0a63b212 2034
c58dab63 2035 tem = Fwindow_buffer (selected_window);
0a63b212 2036 /* If we're burying the current buffer, unshow it. */
c58dab63 2037 if (EQ (buffer, tem))
a78e0303
PJ
2038 {
2039 if (NILP (Fwindow_dedicated_p (selected_window)))
2040 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
2041 else if (NILP (XWINDOW (selected_window)->parent))
2042 Ficonify_frame (Fwindow_frame (selected_window));
2043 else
2044 Fdelete_window (selected_window);
2045 }
a5611885 2046 }
1ab256cb
RM
2047 else
2048 {
2049 Lisp_Object buf1;
177c0ea7 2050
a2428fa2 2051 buf1 = Fget_buffer (buffer);
265a9e55 2052 if (NILP (buf1))
a2428fa2
EN
2053 nsberror (buffer);
2054 buffer = buf1;
b271272a
JB
2055 }
2056
a1a8b28e
GM
2057 /* Move buffer to the end of the buffer list. Do nothing if the
2058 buffer is killed. */
2059 if (!NILP (XBUFFER (buffer)->name))
2060 {
2061 Lisp_Object aelt, link;
b271272a 2062
a1a8b28e
GM
2063 aelt = Frassq (buffer, Vbuffer_alist);
2064 link = Fmemq (aelt, Vbuffer_alist);
2065 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
f3fbd155 2066 XSETCDR (link, Qnil);
a1a8b28e 2067 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1ab256cb 2068
6c488ddb
RS
2069 /* Removing BUFFER from frame-specific lists
2070 has the effect of putting BUFFER at the end
2071 of the combined list in each frame. */
2072 frames_discard_buffer (buffer);
a1a8b28e 2073 }
dec989eb 2074
1ab256cb
RM
2075 return Qnil;
2076}
2077\f
c922bc55 2078DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
7ee72033 2079 doc: /* Delete the entire contents of the current buffer.
018ba359 2080Any narrowing restriction in effect (see `narrow-to-region') is removed,
7ee72033
MB
2081so the buffer is truly empty after this. */)
2082 ()
1ab256cb
RM
2083{
2084 Fwiden ();
c0d9a0c3
GM
2085
2086 del_range (BEG, Z);
c280bc6a 2087
1ab256cb
RM
2088 current_buffer->last_window_start = 1;
2089 /* Prevent warnings, or suspension of auto saving, that would happen
2090 if future size is less than past size. Use of erase-buffer
2091 implies that the future text is not really related to the past text. */
8d7a4592 2092 XSETFASTINT (current_buffer->save_length, 0);
1ab256cb
RM
2093 return Qnil;
2094}
2095
01136e9b 2096void
1ab256cb
RM
2097validate_region (b, e)
2098 register Lisp_Object *b, *e;
2099{
b7826503
PJ
2100 CHECK_NUMBER_COERCE_MARKER (*b);
2101 CHECK_NUMBER_COERCE_MARKER (*e);
1ab256cb
RM
2102
2103 if (XINT (*b) > XINT (*e))
2104 {
03192067
KH
2105 Lisp_Object tem;
2106 tem = *b; *b = *e; *e = tem;
1ab256cb
RM
2107 }
2108
2109 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
2110 && XINT (*e) <= ZV))
2111 args_out_of_range (*b, *e);
2112}
2113\f
b05525fa
RS
2114/* Advance BYTE_POS up to a character boundary
2115 and return the adjusted position. */
2116
2117static int
2118advance_to_char_boundary (byte_pos)
2119 int byte_pos;
2120{
f8449323 2121 int c;
b05525fa 2122
f8449323
RS
2123 if (byte_pos == BEG)
2124 /* Beginning of buffer is always a character boundary. */
6d70a280 2125 return BEG;
f8449323
RS
2126
2127 c = FETCH_BYTE (byte_pos);
2128 if (! CHAR_HEAD_P (c))
b05525fa 2129 {
1be6387d 2130 /* We should advance BYTE_POS only when C is a constituent of a
f8449323 2131 multibyte sequence. */
a9bcded1
KH
2132 int orig_byte_pos = byte_pos;
2133
2134 do
2135 {
2136 byte_pos--;
2137 c = FETCH_BYTE (byte_pos);
2138 }
2139 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
f8449323 2140 INC_POS (byte_pos);
a9bcded1
KH
2141 if (byte_pos < orig_byte_pos)
2142 byte_pos = orig_byte_pos;
f8449323
RS
2143 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2144 surely advance to the correct character boundary. If C is
2145 not, BYTE_POS was unchanged. */
b05525fa
RS
2146 }
2147
20773569 2148 return byte_pos;
b05525fa
RS
2149}
2150
3ac81adb
RS
2151DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2152 1, 1, 0,
7ee72033 2153 doc: /* Set the multibyte flag of the current buffer to FLAG.
018ba359
PJ
2154If FLAG is t, this makes the buffer a multibyte buffer.
2155If FLAG is nil, this makes the buffer a single-byte buffer.
8f924df7
KH
2156In these cases, the buffer contents remain unchanged as a sequence of
2157bytes but the contents viewed as characters do change.
2158If FLAG is `to', this makes the buffer a multibyte buffer by changing
6b61353c
KH
2159all eight-bit bytes to eight-bit characters.
2160If the multibyte flag was really changed, undo information of the
2161current buffer is cleared. */)
7ee72033 2162 (flag)
3ac81adb
RS
2163 Lisp_Object flag;
2164{
65745fad 2165 struct Lisp_Marker *tail, *markers;
abc9d959 2166 struct buffer *other;
458c8af4 2167 int begv, zv;
8d1203ea 2168 int narrowed = (BEG != BEGV || Z != ZV);
ed00559d 2169 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
38babc07
KS
2170 Lisp_Object old_undo = current_buffer->undo_list;
2171 struct gcpro gcpro1;
3ac81adb 2172
6e553d5e
RS
2173 if (current_buffer->base_buffer)
2174 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2175
70e77119
AS
2176 /* Do nothing if nothing actually changes. */
2177 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2178 return flag;
2179
38babc07
KS
2180 GCPRO1 (old_undo);
2181
2182 /* Don't record these buffer changes. We will put a special undo entry
2183 instead. */
2184 current_buffer->undo_list = Qt;
b05525fa 2185
3ac81adb
RS
2186 /* If the cached position is for this buffer, clear it out. */
2187 clear_charpos_cache (current_buffer);
2188
458c8af4
KH
2189 if (NILP (flag))
2190 begv = BEGV_BYTE, zv = ZV_BYTE;
2191 else
2192 begv = BEGV, zv = ZV;
2193
a9bcded1
KH
2194 if (narrowed)
2195 Fwiden ();
2196
3ac81adb
RS
2197 if (NILP (flag))
2198 {
a9bcded1
KH
2199 int pos, stop;
2200 unsigned char *p;
2201
3ac81adb
RS
2202 /* Do this first, so it can use CHAR_TO_BYTE
2203 to calculate the old correspondences. */
2204 set_intervals_multibyte (0);
2205
2206 current_buffer->enable_multibyte_characters = Qnil;
2207
2208 Z = Z_BYTE;
2209 BEGV = BEGV_BYTE;
2210 ZV = ZV_BYTE;
2211 GPT = GPT_BYTE;
2212 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2213
60ebfdf3 2214
65745fad
SM
2215 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2216 tail->charpos = tail->bytepos;
a9bcded1
KH
2217
2218 /* Convert multibyte form of 8-bit characters to unibyte. */
2219 pos = BEG;
2220 stop = GPT;
2221 p = BEG_ADDR;
2222 while (1)
2223 {
2224 int c, bytes;
2225
2226 if (pos == stop)
2227 {
2228 if (pos == Z)
2229 break;
2230 p = GAP_END_ADDR;
2231 stop = Z;
2232 }
8f348ed5
KH
2233 if (ASCII_BYTE_P (*p))
2234 p++, pos++;
2235 else if (CHAR_BYTE8_HEAD_P (*p))
a9bcded1 2236 {
31285a8f 2237 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
a9bcded1
KH
2238 /* Delete all bytes for this 8-bit character but the
2239 last one, and change the last one to the charcter
2240 code. */
2241 bytes--;
2242 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2243 p = GAP_END_ADDR;
2244 *p++ = c;
2245 pos++;
2246 if (begv > pos)
2247 begv -= bytes;
2248 if (zv > pos)
2249 zv -= bytes;
2250 stop = Z;
2251 }
8f924df7 2252 else
8f348ed5
KH
2253 {
2254 bytes = BYTES_BY_CHAR_HEAD (*p);
2255 p += bytes, pos += bytes;
2256 }
a9bcded1
KH
2257 }
2258 if (narrowed)
2259 Fnarrow_to_region (make_number (begv), make_number (zv));
3ac81adb
RS
2260 }
2261 else
2262 {
a9bcded1
KH
2263 int pt = PT;
2264 int pos, stop;
8f348ed5 2265 unsigned char *p, *pend;
a9bcded1 2266
673c57d2 2267 /* Be sure not to have a multibyte sequence striding over the GAP.
8f348ed5
KH
2268 Ex: We change this: "...abc\302 _GAP_ \241def..."
2269 to: "...abc _GAP_ \302\241def..." */
673c57d2 2270
8f924df7 2271 if (EQ (flag, Qt)
a3a303df 2272 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
673c57d2
KH
2273 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2274 {
2275 unsigned char *p = GPT_ADDR - 1;
2276
2277 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2278 if (BASE_LEADING_CODE_P (*p))
2279 {
2280 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2281
2282 move_gap_both (new_gpt, new_gpt);
2283 }
2284 }
2285
a9bcded1
KH
2286 /* Make the buffer contents valid as multibyte by converting
2287 8-bit characters to multibyte form. */
2288 pos = BEG;
2289 stop = GPT;
2290 p = BEG_ADDR;
8f348ed5 2291 pend = GPT_ADDR;
a9bcded1
KH
2292 while (1)
2293 {
2294 int bytes;
2295
2296 if (pos == stop)
2297 {
2298 if (pos == Z)
2299 break;
2300 p = GAP_END_ADDR;
8f348ed5 2301 pend = Z_ADDR;
a9bcded1
KH
2302 stop = Z;
2303 }
177c0ea7 2304
a3a303df
KH
2305 if (ASCII_BYTE_P (*p))
2306 p++, pos++;
8f924df7 2307 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
a9bcded1
KH
2308 p += bytes, pos += bytes;
2309 else
2310 {
2311 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
a3a303df 2312 int c;
a9bcded1 2313
8f924df7 2314 c = BYTE8_TO_CHAR (*p);
8c2fc311 2315 bytes = CHAR_STRING (c, tmp);
a9bcded1
KH
2316 *p = tmp[0];
2317 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2318 bytes--;
2319 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2320 /* Now the gap is after the just inserted data. */
2321 pos = GPT;
2322 p = GAP_END_ADDR;
2323 if (pos <= begv)
2324 begv += bytes;
2325 if (pos <= zv)
2326 zv += bytes;
2327 if (pos <= pt)
2328 pt += bytes;
31285a8f 2329 pend = Z_ADDR;
a9bcded1
KH
2330 stop = Z;
2331 }
2332 }
2333
2334 if (pt != PT)
2335 TEMP_SET_PT (pt);
2336
2337 if (narrowed)
2338 Fnarrow_to_region (make_number (begv), make_number (zv));
2339
3ac81adb
RS
2340 /* Do this first, so that chars_in_text asks the right question.
2341 set_intervals_multibyte needs it too. */
2342 current_buffer->enable_multibyte_characters = Qt;
2343
b05525fa 2344 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
3ac81adb 2345 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
b05525fa 2346
673c57d2 2347 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
b05525fa
RS
2348
2349 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
3ac81adb 2350 if (BEGV_BYTE > GPT_BYTE)
673c57d2 2351 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2352 else
2353 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2354
2355 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
3ac81adb 2356 if (ZV_BYTE > GPT_BYTE)
673c57d2 2357 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2358 else
2359 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2360
2361 {
2362 int pt_byte = advance_to_char_boundary (PT_BYTE);
2363 int pt;
2364
2365 if (pt_byte > GPT_BYTE)
673c57d2 2366 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
b05525fa
RS
2367 else
2368 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2369 TEMP_SET_PT_BOTH (pt, pt_byte);
2370 }
3ac81adb
RS
2371
2372 tail = markers = BUF_MARKERS (current_buffer);
95fb069b
RS
2373
2374 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2375 getting confused by the markers that have not yet been updated.
2376 It is also a signal that it should never create a marker. */
65745fad 2377 BUF_MARKERS (current_buffer) = NULL;
3ac81adb 2378
65745fad 2379 for (; tail; tail = tail->next)
3ac81adb 2380 {
65745fad
SM
2381 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2382 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
3ac81adb 2383 }
b69f9797
RS
2384
2385 /* Make sure no markers were put on the chain
2386 while the chain value was incorrect. */
65745fad 2387 if (BUF_MARKERS (current_buffer))
b69f9797
RS
2388 abort ();
2389
3ac81adb
RS
2390 BUF_MARKERS (current_buffer) = markers;
2391
2392 /* Do this last, so it can calculate the new correspondences
2393 between chars and bytes. */
2394 set_intervals_multibyte (1);
2395 }
2396
38babc07
KS
2397 if (!EQ (old_undo, Qt))
2398 {
2399 /* Represent all the above changes by a special undo entry. */
2400 extern Lisp_Object Qapply;
8929fd87
KS
2401 current_buffer->undo_list = Fcons (list3 (Qapply,
2402 intern ("set-buffer-multibyte"),
2403 NILP (flag) ? Qt : Qnil),
2404 old_undo);
38babc07
KS
2405 }
2406
2407 UNGCPRO;
a9bcded1 2408
724b203f
GM
2409 /* Changing the multibyteness of a buffer means that all windows
2410 showing that buffer must be updated thoroughly. */
2411 current_buffer->prevent_redisplay_optimizations_p = 1;
2412 ++windows_or_buffers_changed;
2413
abc9d959
RS
2414 /* Copy this buffer's new multibyte status
2415 into all of its indirect buffers. */
2416 for (other = all_buffers; other; other = other->next)
2417 if (other->base_buffer == current_buffer && !NILP (other->name))
724b203f
GM
2418 {
2419 other->enable_multibyte_characters
2420 = current_buffer->enable_multibyte_characters;
2421 other->prevent_redisplay_optimizations_p = 1;
2422 }
abc9d959 2423
ed00559d
KH
2424 /* Restore the modifiedness of the buffer. */
2425 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2426 Fset_buffer_modified_p (Qnil);
2427
2a2b50a9 2428#ifdef subprocesses
172f9454
KH
2429 /* Update coding systems of this buffer's process (if any). */
2430 {
2431 Lisp_Object process;
2432
2433 process = Fget_buffer_process (Fcurrent_buffer ());
2434 if (PROCESSP (process))
2435 setup_process_coding_systems (process);
2436 }
2a2b50a9 2437#endif /* subprocesses */
172f9454 2438
3ac81adb
RS
2439 return flag;
2440}
2441\f
1ab256cb 2442DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
efc7e75f 2443 0, 0, 0,
7ee72033 2444 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
018ba359
PJ
2445Most local variable bindings are eliminated so that the default values
2446become effective once more. Also, the syntax table is set from
2447`standard-syntax-table', the local keymap is set to nil,
2448and the abbrev table from `fundamental-mode-abbrev-table'.
2449This function also forces redisplay of the mode line.
2450
2451Every function to select a new major mode starts by
2452calling this function.
2453
2454As a special exception, local variables whose names have
2455a non-nil `permanent-local' property are not eliminated by this function.
2456
2457The first thing this function does is run
7ee72033
MB
2458the normal hook `change-major-mode-hook'. */)
2459 ()
1ab256cb
RM
2460{
2461 register Lisp_Object alist, sym, tem;
2462 Lisp_Object oalist;
7410477a 2463
fd186f07 2464 if (!NILP (Vrun_hooks))
43ed3b8d 2465 call1 (Vrun_hooks, Qchange_major_mode_hook);
1ab256cb
RM
2466 oalist = current_buffer->local_var_alist;
2467
2f3f993b
RS
2468 /* Make sure none of the bindings in oalist
2469 remain swapped in, in their symbols. */
1ab256cb 2470
2f3f993b 2471 swap_out_buffer_local_variables (current_buffer);
1ab256cb
RM
2472
2473 /* Actually eliminate all local bindings of this buffer. */
2474
13de9290 2475 reset_buffer_local_variables (current_buffer, 0);
1ab256cb 2476
1ab256cb
RM
2477 /* Any which are supposed to be permanent,
2478 make local again, with the same values they had. */
177c0ea7 2479
7539e11f 2480 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
1ab256cb 2481 {
7539e11f 2482 sym = XCAR (XCAR (alist));
1ab256cb 2483 tem = Fget (sym, Qpermanent_local);
265a9e55 2484 if (! NILP (tem))
01050cb5
RM
2485 {
2486 Fmake_local_variable (sym);
7539e11f 2487 Fset (sym, XCDR (XCAR (alist)));
01050cb5 2488 }
1ab256cb
RM
2489 }
2490
2491 /* Force mode-line redisplay. Useful here because all major mode
2492 commands call this function. */
2493 update_mode_lines++;
2494
2495 return Qnil;
2496}
2f3f993b
RS
2497
2498/* Make sure no local variables remain set up with buffer B
2499 for their current values. */
2500
2501static void
2502swap_out_buffer_local_variables (b)
2503 struct buffer *b;
2504{
2505 Lisp_Object oalist, alist, sym, tem, buffer;
2506
2507 XSETBUFFER (buffer, b);
2508 oalist = b->local_var_alist;
2509
7539e11f 2510 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2f3f993b 2511 {
7539e11f 2512 sym = XCAR (XCAR (alist));
2f3f993b
RS
2513
2514 /* Need not do anything if some other buffer's binding is now encached. */
f5c1dd0d 2515 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
8801a864 2516 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2f3f993b
RS
2517 {
2518 /* Symbol is set up for this buffer's old local value.
2519 Set it up for the current buffer with the default value. */
2520
f5c1dd0d 2521 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->cdr;
2f3f993b
RS
2522 /* Store the symbol's current value into the alist entry
2523 it is currently set up for. This is so that, if the
2524 local is marked permanent, and we make it local again
2525 later in Fkill_all_local_variables, we don't lose the value. */
f3fbd155
KR
2526 XSETCDR (XCAR (tem),
2527 do_symval_forwarding (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue));
2f3f993b 2528 /* Switch to the symbol's default-value alist entry. */
f3fbd155 2529 XSETCAR (tem, tem);
2f3f993b 2530 /* Mark it as current for buffer B. */
f5c1dd0d 2531 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer = buffer;
2f3f993b 2532 /* Store the current value into any forwarding in the symbol. */
3d871c85 2533 store_symval_forwarding (sym,
f5c1dd0d 2534 XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->realvalue,
9b3a7dad 2535 XCDR (tem), NULL);
2f3f993b
RS
2536 }
2537 }
2538}
1ab256cb 2539\f
2eec3b4e 2540/* Find all the overlays in the current buffer that contain position POS.
177c0ea7 2541 Return the number found, and store them in a vector in *VEC_PTR.
2eec3b4e 2542 Store in *LEN_PTR the size allocated for the vector.
52f8ec73 2543 Store in *NEXT_PTR the next position after POS where an overlay starts,
624d2678 2544 or ZV if there are no more overlays between POS and ZV.
bbbe9545 2545 Store in *PREV_PTR the previous position before POS where an overlay ends,
413e06a4 2546 or where an overlay starts which ends at or after POS;
624d2678 2547 or BEGV if there are no such overlays from BEGV to POS.
239c932b 2548 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
2549
2550 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
2551 when this function is called.
2552
2553 If EXTEND is non-zero, we make the vector bigger if necessary.
2554 If EXTEND is zero, we never extend the vector,
2555 and we store only as many overlays as will fit.
ac869cf7
MB
2556 But we still return the total number of overlays.
2557
2558 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2559 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2560 default (BEGV or ZV). */
2eec3b4e
RS
2561
2562int
ac869cf7 2563overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
c2d5b10f 2564 EMACS_INT pos;
61d54cd5 2565 int extend;
2eec3b4e
RS
2566 Lisp_Object **vec_ptr;
2567 int *len_ptr;
2568 int *next_ptr;
239c932b 2569 int *prev_ptr;
ac869cf7 2570 int change_req;
1ab256cb 2571{
2410d73a
SM
2572 Lisp_Object overlay, start, end;
2573 struct Lisp_Overlay *tail;
2eec3b4e
RS
2574 int idx = 0;
2575 int len = *len_ptr;
2576 Lisp_Object *vec = *vec_ptr;
2577 int next = ZV;
239c932b 2578 int prev = BEGV;
61d54cd5
RS
2579 int inhibit_storing = 0;
2580
2410d73a 2581 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2eec3b4e 2582 {
239c932b 2583 int startpos, endpos;
52f8ec73 2584
2410d73a 2585 XSETMISC (overlay, tail);
1ab256cb 2586
2eec3b4e
RS
2587 start = OVERLAY_START (overlay);
2588 end = OVERLAY_END (overlay);
239c932b
RS
2589 endpos = OVERLAY_POSITION (end);
2590 if (endpos < pos)
2591 {
2592 if (prev < endpos)
2593 prev = endpos;
2594 break;
2595 }
413e06a4
RS
2596 startpos = OVERLAY_POSITION (start);
2597 /* This one ends at or after POS
daa1c109 2598 so its start counts for PREV_PTR if it's before POS. */
413e06a4
RS
2599 if (prev < startpos && startpos < pos)
2600 prev = startpos;
239c932b
RS
2601 if (endpos == pos)
2602 continue;
2eec3b4e
RS
2603 if (startpos <= pos)
2604 {
2605 if (idx == len)
2606 {
61d54cd5
RS
2607 /* The supplied vector is full.
2608 Either make it bigger, or don't store any more in it. */
2609 if (extend)
2610 {
0552666b
GM
2611 /* Make it work with an initial len == 0. */
2612 len *= 2;
2613 if (len == 0)
2614 len = 4;
2615 *len_ptr = len;
61d54cd5
RS
2616 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2617 *vec_ptr = vec;
2618 }
2619 else
2620 inhibit_storing = 1;
2eec3b4e 2621 }
61d54cd5
RS
2622
2623 if (!inhibit_storing)
2624 vec[idx] = overlay;
2625 /* Keep counting overlays even if we can't return them all. */
2626 idx++;
2eec3b4e
RS
2627 }
2628 else if (startpos < next)
2629 next = startpos;
2630 }
2631
2410d73a 2632 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
1ab256cb 2633 {
239c932b 2634 int startpos, endpos;
52f8ec73 2635
2410d73a 2636 XSETMISC (overlay, tail);
2eec3b4e
RS
2637
2638 start = OVERLAY_START (overlay);
2639 end = OVERLAY_END (overlay);
2640 startpos = OVERLAY_POSITION (start);
52f8ec73 2641 if (pos < startpos)
2eec3b4e
RS
2642 {
2643 if (startpos < next)
2644 next = startpos;
2645 break;
2646 }
239c932b
RS
2647 endpos = OVERLAY_POSITION (end);
2648 if (pos < endpos)
2eec3b4e
RS
2649 {
2650 if (idx == len)
2651 {
61d54cd5
RS
2652 if (extend)
2653 {
4b0e44fc
RS
2654 /* Make it work with an initial len == 0. */
2655 len *= 2;
0552666b 2656 if (len == 0)
4b0e44fc
RS
2657 len = 4;
2658 *len_ptr = len;
61d54cd5
RS
2659 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2660 *vec_ptr = vec;
2661 }
2662 else
2663 inhibit_storing = 1;
2eec3b4e 2664 }
61d54cd5
RS
2665
2666 if (!inhibit_storing)
2667 vec[idx] = overlay;
2668 idx++;
413e06a4
RS
2669
2670 if (startpos < pos && startpos > prev)
2671 prev = startpos;
2eec3b4e 2672 }
239c932b
RS
2673 else if (endpos < pos && endpos > prev)
2674 prev = endpos;
1d5f4c1d
MB
2675 else if (endpos == pos && startpos > prev
2676 && (!change_req || startpos < pos))
413e06a4 2677 prev = startpos;
1ab256cb
RM
2678 }
2679
239c932b
RS
2680 if (next_ptr)
2681 *next_ptr = next;
2682 if (prev_ptr)
2683 *prev_ptr = prev;
2eec3b4e
RS
2684 return idx;
2685}
74514898
RS
2686\f
2687/* Find all the overlays in the current buffer that overlap the range BEG-END
2a3eeee7
RS
2688 or are empty at BEG.
2689
177c0ea7 2690 Return the number found, and store them in a vector in *VEC_PTR.
74514898
RS
2691 Store in *LEN_PTR the size allocated for the vector.
2692 Store in *NEXT_PTR the next position after POS where an overlay starts,
2693 or ZV if there are no more overlays.
2694 Store in *PREV_PTR the previous position before POS where an overlay ends,
2695 or BEGV if there are no previous overlays.
2696 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2697
2698 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2699 when this function is called.
2700
2701 If EXTEND is non-zero, we make the vector bigger if necessary.
2702 If EXTEND is zero, we never extend the vector,
2703 and we store only as many overlays as will fit.
2704 But we still return the total number of overlays. */
2705
a9b9a780 2706static int
74514898
RS
2707overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2708 int beg, end;
2709 int extend;
2710 Lisp_Object **vec_ptr;
2711 int *len_ptr;
2712 int *next_ptr;
2713 int *prev_ptr;
2714{
2410d73a
SM
2715 Lisp_Object overlay, ostart, oend;
2716 struct Lisp_Overlay *tail;
74514898
RS
2717 int idx = 0;
2718 int len = *len_ptr;
2719 Lisp_Object *vec = *vec_ptr;
2720 int next = ZV;
2721 int prev = BEGV;
2722 int inhibit_storing = 0;
2723
2410d73a 2724 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
74514898
RS
2725 {
2726 int startpos, endpos;
2727
2410d73a 2728 XSETMISC (overlay, tail);
74514898
RS
2729
2730 ostart = OVERLAY_START (overlay);
2731 oend = OVERLAY_END (overlay);
2732 endpos = OVERLAY_POSITION (oend);
2733 if (endpos < beg)
2734 {
2735 if (prev < endpos)
2736 prev = endpos;
2737 break;
2738 }
2739 startpos = OVERLAY_POSITION (ostart);
2740 /* Count an interval if it either overlaps the range
2a3eeee7 2741 or is empty at the start of the range. */
74514898 2742 if ((beg < endpos && startpos < end)
2a3eeee7 2743 || (startpos == endpos && beg == endpos))
74514898
RS
2744 {
2745 if (idx == len)
2746 {
2747 /* The supplied vector is full.
2748 Either make it bigger, or don't store any more in it. */
2749 if (extend)
2750 {
4b0e44fc
RS
2751 /* Make it work with an initial len == 0. */
2752 len *= 2;
2753 if (len == 0)
2754 len = 4;
2755 *len_ptr = len;
74514898
RS
2756 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2757 *vec_ptr = vec;
2758 }
2759 else
2760 inhibit_storing = 1;
2761 }
2762
2763 if (!inhibit_storing)
2764 vec[idx] = overlay;
2765 /* Keep counting overlays even if we can't return them all. */
2766 idx++;
2767 }
2768 else if (startpos < next)
2769 next = startpos;
2770 }
2771
2410d73a 2772 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
74514898
RS
2773 {
2774 int startpos, endpos;
2775
2410d73a 2776 XSETMISC (overlay, tail);
74514898
RS
2777
2778 ostart = OVERLAY_START (overlay);
2779 oend = OVERLAY_END (overlay);
2780 startpos = OVERLAY_POSITION (ostart);
2781 if (end < startpos)
2782 {
2783 if (startpos < next)
2784 next = startpos;
2785 break;
2786 }
2787 endpos = OVERLAY_POSITION (oend);
2a3eeee7
RS
2788 /* Count an interval if it either overlaps the range
2789 or is empty at the start of the range. */
74514898 2790 if ((beg < endpos && startpos < end)
2a3eeee7 2791 || (startpos == endpos && beg == endpos))
74514898
RS
2792 {
2793 if (idx == len)
2794 {
2795 if (extend)
2796 {
4b0e44fc
RS
2797 /* Make it work with an initial len == 0. */
2798 len *= 2;
2799 if (len == 0)
2800 len = 4;
2801 *len_ptr = len;
74514898
RS
2802 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2803 *vec_ptr = vec;
2804 }
2805 else
2806 inhibit_storing = 1;
2807 }
2808
2809 if (!inhibit_storing)
2810 vec[idx] = overlay;
2811 idx++;
2812 }
2813 else if (endpos < beg && endpos > prev)
2814 prev = endpos;
2815 }
fc04fa47 2816
74514898
RS
2817 if (next_ptr)
2818 *next_ptr = next;
2819 if (prev_ptr)
2820 *prev_ptr = prev;
2821 return idx;
2822}
09a22085
GM
2823
2824
2825/* Return non-zero if there exists an overlay with a non-nil
2826 `mouse-face' property overlapping OVERLAY. */
2827
2828int
2829mouse_face_overlay_overlaps (overlay)
2830 Lisp_Object overlay;
2831{
2832 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2833 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
bfd8410f 2834 int n, i, size;
09a22085 2835 Lisp_Object *v, tem;
177c0ea7 2836
bfd8410f
GM
2837 size = 10;
2838 v = (Lisp_Object *) alloca (size * sizeof *v);
2839 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2840 if (n > size)
09a22085 2841 {
09a22085
GM
2842 v = (Lisp_Object *) alloca (n * sizeof *v);
2843 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2844 }
2845
2846 for (i = 0; i < n; ++i)
2847 if (!EQ (v[i], overlay)
2848 && (tem = Foverlay_get (overlay, Qmouse_face),
2849 !NILP (tem)))
2850 break;
2851
2852 return i < n;
2853}
2854
2855
74514898 2856\f
fc04fa47
KH
2857/* Fast function to just test if we're at an overlay boundary. */
2858int
2859overlay_touches_p (pos)
2860 int pos;
2861{
2410d73a
SM
2862 Lisp_Object overlay;
2863 struct Lisp_Overlay *tail;
fc04fa47 2864
2410d73a 2865 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
fc04fa47
KH
2866 {
2867 int endpos;
2868
2410d73a 2869 XSETMISC (overlay ,tail);
fc04fa47
KH
2870 if (!GC_OVERLAYP (overlay))
2871 abort ();
2872
2873 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2874 if (endpos < pos)
2875 break;
2876 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2877 return 1;
2878 }
2879
2410d73a 2880 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
fc04fa47
KH
2881 {
2882 int startpos;
2883
2410d73a 2884 XSETMISC (overlay, tail);
fc04fa47
KH
2885 if (!GC_OVERLAYP (overlay))
2886 abort ();
2887
2888 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2889 if (pos < startpos)
2890 break;
2891 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2892 return 1;
2893 }
2894 return 0;
2895}
2eec3b4e 2896\f
5985d248
KH
2897struct sortvec
2898{
2899 Lisp_Object overlay;
2900 int beg, end;
2901 int priority;
2902};
2903
2904static int
dfcf069d
AS
2905compare_overlays (v1, v2)
2906 const void *v1, *v2;
5985d248 2907{
dfcf069d
AS
2908 const struct sortvec *s1 = (const struct sortvec *) v1;
2909 const struct sortvec *s2 = (const struct sortvec *) v2;
5985d248
KH
2910 if (s1->priority != s2->priority)
2911 return s1->priority - s2->priority;
2912 if (s1->beg != s2->beg)
2913 return s1->beg - s2->beg;
2914 if (s1->end != s2->end)
2915 return s2->end - s1->end;
2916 return 0;
2917}
2918
2919/* Sort an array of overlays by priority. The array is modified in place.
2920 The return value is the new size; this may be smaller than the original
2921 size if some of the overlays were invalid or were window-specific. */
2922int
2923sort_overlays (overlay_vec, noverlays, w)
2924 Lisp_Object *overlay_vec;
2925 int noverlays;
2926 struct window *w;
2927{
2928 int i, j;
2929 struct sortvec *sortvec;
2930 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2931
2932 /* Put the valid and relevant overlays into sortvec. */
2933
2934 for (i = 0, j = 0; i < noverlays; i++)
2935 {
0fa767e7 2936 Lisp_Object tem;
c99fc30f 2937 Lisp_Object overlay;
5985d248 2938
c99fc30f 2939 overlay = overlay_vec[i];
5985d248
KH
2940 if (OVERLAY_VALID (overlay)
2941 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2942 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2943 {
0fa767e7
KH
2944 /* If we're interested in a specific window, then ignore
2945 overlays that are limited to some other window. */
2946 if (w)
5985d248 2947 {
0fa767e7
KH
2948 Lisp_Object window;
2949
2950 window = Foverlay_get (overlay, Qwindow);
a7a60ce9 2951 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 2952 continue;
5985d248 2953 }
0fa767e7
KH
2954
2955 /* This overlay is good and counts: put it into sortvec. */
2956 sortvec[j].overlay = overlay;
2957 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2958 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2959 tem = Foverlay_get (overlay, Qpriority);
2960 if (INTEGERP (tem))
2961 sortvec[j].priority = XINT (tem);
2962 else
2963 sortvec[j].priority = 0;
2964 j++;
5985d248
KH
2965 }
2966 }
2967 noverlays = j;
2968
2969 /* Sort the overlays into the proper order: increasing priority. */
2970
2971 if (noverlays > 1)
2972 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2973
2974 for (i = 0; i < noverlays; i++)
2975 overlay_vec[i] = sortvec[i].overlay;
2976 return (noverlays);
2977}
2978\f
bbbe9545
KH
2979struct sortstr
2980{
cb26008f 2981 Lisp_Object string, string2;
bbbe9545
KH
2982 int size;
2983 int priority;
2984};
2985
e8185fa8
KH
2986struct sortstrlist
2987{
2988 struct sortstr *buf; /* An array that expands as needed; never freed. */
2989 int size; /* Allocated length of that array. */
2990 int used; /* How much of the array is currently in use. */
2991 int bytes; /* Total length of the strings in buf. */
2992};
2993
2994/* Buffers for storing information about the overlays touching a given
2995 position. These could be automatic variables in overlay_strings, but
2996 it's more efficient to hold onto the memory instead of repeatedly
2997 allocating and freeing it. */
2998static struct sortstrlist overlay_heads, overlay_tails;
9492daf2 2999static unsigned char *overlay_str_buf;
e8185fa8
KH
3000
3001/* Allocated length of overlay_str_buf. */
3002static int overlay_str_len;
3003
bbbe9545
KH
3004/* A comparison function suitable for passing to qsort. */
3005static int
3006cmp_for_strings (as1, as2)
3007 char *as1, *as2;
3008{
3009 struct sortstr *s1 = (struct sortstr *)as1;
3010 struct sortstr *s2 = (struct sortstr *)as2;
3011 if (s1->size != s2->size)
3012 return s2->size - s1->size;
3013 if (s1->priority != s2->priority)
3014 return s1->priority - s2->priority;
3015 return 0;
3016}
3017
e8185fa8 3018static void
cb26008f 3019record_overlay_string (ssl, str, str2, pri, size)
e8185fa8 3020 struct sortstrlist *ssl;
cb26008f 3021 Lisp_Object str, str2, pri;
e8185fa8
KH
3022 int size;
3023{
43d27a72
RS
3024 int nbytes;
3025
e8185fa8
KH
3026 if (ssl->used == ssl->size)
3027 {
3028 if (ssl->buf)
3029 ssl->size *= 2;
3030 else
3031 ssl->size = 5;
3032 ssl->buf = ((struct sortstr *)
3033 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3034 }
3035 ssl->buf[ssl->used].string = str;
cb26008f 3036 ssl->buf[ssl->used].string2 = str2;
e8185fa8
KH
3037 ssl->buf[ssl->used].size = size;
3038 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3039 ssl->used++;
43d27a72
RS
3040
3041 if (NILP (current_buffer->enable_multibyte_characters))
d5db4077 3042 nbytes = SCHARS (str);
43d27a72 3043 else if (! STRING_MULTIBYTE (str))
d5db4077
KR
3044 nbytes = count_size_as_multibyte (SDATA (str),
3045 SBYTES (str));
43d27a72 3046 else
d5db4077 3047 nbytes = SBYTES (str);
43d27a72
RS
3048
3049 ssl->bytes += nbytes;
3050
cb26008f 3051 if (STRINGP (str2))
43d27a72
RS
3052 {
3053 if (NILP (current_buffer->enable_multibyte_characters))
d5db4077 3054 nbytes = SCHARS (str2);
43d27a72 3055 else if (! STRING_MULTIBYTE (str2))
d5db4077
KR
3056 nbytes = count_size_as_multibyte (SDATA (str2),
3057 SBYTES (str2));
43d27a72 3058 else
d5db4077 3059 nbytes = SBYTES (str2);
43d27a72
RS
3060
3061 ssl->bytes += nbytes;
3062 }
e8185fa8 3063}
bbbe9545
KH
3064
3065/* Return the concatenation of the strings associated with overlays that
3066 begin or end at POS, ignoring overlays that are specific to a window
3067 other than W. The strings are concatenated in the appropriate order:
3068 shorter overlays nest inside longer ones, and higher priority inside
cb26008f
KH
3069 lower. Normally all of the after-strings come first, but zero-sized
3070 overlays have their after-strings ride along with the before-strings
3071 because it would look strange to print them inside-out.
3072
3073 Returns the string length, and stores the contents indirectly through
3074 PSTR, if that variable is non-null. The string may be overwritten by
3075 subsequent calls. */
6b5d3b89 3076
bbbe9545
KH
3077int
3078overlay_strings (pos, w, pstr)
c2d5b10f 3079 EMACS_INT pos;
bbbe9545 3080 struct window *w;
6b5d3b89 3081 unsigned char **pstr;
bbbe9545 3082{
2410d73a
SM
3083 Lisp_Object overlay, window, str;
3084 struct Lisp_Overlay *ov;
bbbe9545 3085 int startpos, endpos;
43d27a72 3086 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
bbbe9545 3087
e8185fa8
KH
3088 overlay_heads.used = overlay_heads.bytes = 0;
3089 overlay_tails.used = overlay_tails.bytes = 0;
2410d73a 3090 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
bbbe9545 3091 {
2410d73a 3092 XSETMISC (overlay, ov);
c2d5b10f 3093 eassert (OVERLAYP (overlay));
bbbe9545
KH
3094
3095 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3096 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3097 if (endpos < pos)
3098 break;
3099 if (endpos != pos && startpos != pos)
3100 continue;
3101 window = Foverlay_get (overlay, Qwindow);
3102 if (WINDOWP (window) && XWINDOW (window) != w)
3103 continue;
e8185fa8
KH
3104 if (startpos == pos
3105 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3106 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3107 (startpos == endpos
3108 ? Foverlay_get (overlay, Qafter_string)
3109 : Qnil),
3110 Foverlay_get (overlay, Qpriority),
3111 endpos - startpos);
3112 else if (endpos == pos
3113 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3114 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3115 Foverlay_get (overlay, Qpriority),
3116 endpos - startpos);
bbbe9545 3117 }
2410d73a 3118 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
bbbe9545 3119 {
2410d73a
SM
3120 XSETMISC (overlay, ov);
3121 eassert (OVERLAYP (overlay));
bbbe9545
KH
3122
3123 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3124 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3125 if (startpos > pos)
3126 break;
e8185fa8
KH
3127 if (endpos != pos && startpos != pos)
3128 continue;
3129 window = Foverlay_get (overlay, Qwindow);
3130 if (WINDOWP (window) && XWINDOW (window) != w)
3131 continue;
e8185fa8
KH
3132 if (startpos == pos
3133 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3134 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3135 (startpos == endpos
3136 ? Foverlay_get (overlay, Qafter_string)
3137 : Qnil),
3138 Foverlay_get (overlay, Qpriority),
3139 endpos - startpos);
3140 else if (endpos == pos
3141 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3142 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3143 Foverlay_get (overlay, Qpriority),
3144 endpos - startpos);
bbbe9545 3145 }
e8185fa8
KH
3146 if (overlay_tails.used > 1)
3147 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3148 cmp_for_strings);
3149 if (overlay_heads.used > 1)
3150 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3151 cmp_for_strings);
3152 if (overlay_heads.bytes || overlay_tails.bytes)
bbbe9545 3153 {
e8185fa8 3154 Lisp_Object tem;
bbbe9545 3155 int i;
9f4d7cde 3156 unsigned char *p;
e8185fa8 3157 int total = overlay_heads.bytes + overlay_tails.bytes;
bbbe9545
KH
3158
3159 if (total > overlay_str_len)
9f4d7cde
RS
3160 {
3161 overlay_str_len = total;
3162 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3163 total);
3164 }
bbbe9545 3165 p = overlay_str_buf;
e8185fa8 3166 for (i = overlay_tails.used; --i >= 0;)
bbbe9545 3167 {
43d27a72 3168 int nbytes;
e8185fa8 3169 tem = overlay_tails.buf[i].string;
d5db4077
KR
3170 nbytes = copy_text (SDATA (tem), p,
3171 SBYTES (tem),
43d27a72
RS
3172 STRING_MULTIBYTE (tem), multibyte);
3173 p += nbytes;
bbbe9545 3174 }
e8185fa8 3175 for (i = 0; i < overlay_heads.used; ++i)
bbbe9545 3176 {
43d27a72 3177 int nbytes;
e8185fa8 3178 tem = overlay_heads.buf[i].string;
d5db4077
KR
3179 nbytes = copy_text (SDATA (tem), p,
3180 SBYTES (tem),
43d27a72
RS
3181 STRING_MULTIBYTE (tem), multibyte);
3182 p += nbytes;
cb26008f
KH
3183 tem = overlay_heads.buf[i].string2;
3184 if (STRINGP (tem))
3185 {
d5db4077
KR
3186 nbytes = copy_text (SDATA (tem), p,
3187 SBYTES (tem),
43d27a72
RS
3188 STRING_MULTIBYTE (tem), multibyte);
3189 p += nbytes;
cb26008f 3190 }
bbbe9545 3191 }
cb26008f
KH
3192 if (p != overlay_str_buf + total)
3193 abort ();
bbbe9545
KH
3194 if (pstr)
3195 *pstr = overlay_str_buf;
e8185fa8 3196 return total;
bbbe9545 3197 }
e8185fa8 3198 return 0;
bbbe9545
KH
3199}
3200\f
5c4f68f1 3201/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 3202
2eec3b4e 3203void
5c4f68f1
JB
3204recenter_overlay_lists (buf, pos)
3205 struct buffer *buf;
c2d5b10f 3206 EMACS_INT pos;
2eec3b4e 3207{
2410d73a
SM
3208 Lisp_Object overlay, beg, end;
3209 struct Lisp_Overlay *prev, *tail, *next;
2eec3b4e
RS
3210
3211 /* See if anything in overlays_before should move to overlays_after. */
3212
3213 /* We don't strictly need prev in this loop; it should always be nil.
3214 But we use it for symmetry and in case that should cease to be true
3215 with some future change. */
2410d73a
SM
3216 prev = NULL;
3217 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
1ab256cb 3218 {
2410d73a
SM
3219 next = tail->next;
3220 XSETMISC (overlay, tail);
2eec3b4e
RS
3221
3222 /* If the overlay is not valid, get rid of it. */
3223 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
3224#if 1
3225 abort ();
3226#else
2eec3b4e
RS
3227 {
3228 /* Splice the cons cell TAIL out of overlays_before. */
3229 if (!NILP (prev))
7539e11f 3230 XCDR (prev) = next;
2eec3b4e 3231 else
5c4f68f1 3232 buf->overlays_before = next;
2eec3b4e
RS
3233 tail = prev;
3234 continue;
3235 }
52f8ec73 3236#endif
1ab256cb 3237
2eec3b4e
RS
3238 beg = OVERLAY_START (overlay);
3239 end = OVERLAY_END (overlay);
1ab256cb 3240
2eec3b4e 3241 if (OVERLAY_POSITION (end) > pos)
1ab256cb 3242 {
2eec3b4e
RS
3243 /* OVERLAY needs to be moved. */
3244 int where = OVERLAY_POSITION (beg);
2410d73a 3245 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3246
3247 /* Splice the cons cell TAIL out of overlays_before. */
2410d73a
SM
3248 if (prev)
3249 prev->next = next;
2eec3b4e 3250 else
5c4f68f1 3251 buf->overlays_before = next;
2eec3b4e
RS
3252
3253 /* Search thru overlays_after for where to put it. */
2410d73a
SM
3254 other_prev = NULL;
3255 for (other = buf->overlays_after; other;
3256 other_prev = other, other = other->next)
1ab256cb 3257 {
6af718a4 3258 Lisp_Object otherbeg, otheroverlay;
2eec3b4e 3259
2410d73a
SM
3260 XSETMISC (otheroverlay, other);
3261 eassert (OVERLAY_VALID (otheroverlay));
2eec3b4e
RS
3262
3263 otherbeg = OVERLAY_START (otheroverlay);
3264 if (OVERLAY_POSITION (otherbeg) >= where)
3265 break;
1ab256cb 3266 }
2eec3b4e
RS
3267
3268 /* Add TAIL to overlays_after before OTHER. */
2410d73a
SM
3269 tail->next = other;
3270 if (other_prev)
3271 other_prev->next = tail;
1ab256cb 3272 else
5c4f68f1 3273 buf->overlays_after = tail;
2eec3b4e 3274 tail = prev;
1ab256cb 3275 }
2eec3b4e
RS
3276 else
3277 /* We've reached the things that should stay in overlays_before.
3278 All the rest of overlays_before must end even earlier,
3279 so stop now. */
3280 break;
3281 }
3282
3283 /* See if anything in overlays_after should be in overlays_before. */
2410d73a
SM
3284 prev = NULL;
3285 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
2eec3b4e 3286 {
2410d73a
SM
3287 next = tail->next;
3288 XSETMISC (overlay, tail);
2eec3b4e
RS
3289
3290 /* If the overlay is not valid, get rid of it. */
3291 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
3292#if 1
3293 abort ();
3294#else
2eec3b4e
RS
3295 {
3296 /* Splice the cons cell TAIL out of overlays_after. */
3297 if (!NILP (prev))
7539e11f 3298 XCDR (prev) = next;
2eec3b4e 3299 else
5c4f68f1 3300 buf->overlays_after = next;
2eec3b4e
RS
3301 tail = prev;
3302 continue;
3303 }
52f8ec73 3304#endif
2eec3b4e
RS
3305
3306 beg = OVERLAY_START (overlay);
3307 end = OVERLAY_END (overlay);
3308
3309 /* Stop looking, when we know that nothing further
3310 can possibly end before POS. */
3311 if (OVERLAY_POSITION (beg) > pos)
3312 break;
3313
3314 if (OVERLAY_POSITION (end) <= pos)
3315 {
3316 /* OVERLAY needs to be moved. */
3317 int where = OVERLAY_POSITION (end);
2410d73a 3318 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3319
3320 /* Splice the cons cell TAIL out of overlays_after. */
2410d73a
SM
3321 if (prev)
3322 prev->next = next;
2eec3b4e 3323 else
5c4f68f1 3324 buf->overlays_after = next;
2eec3b4e
RS
3325
3326 /* Search thru overlays_before for where to put it. */
2410d73a
SM
3327 other_prev = NULL;
3328 for (other = buf->overlays_before; other;
3329 other_prev = other, other = other->next)
2eec3b4e
RS
3330 {
3331 Lisp_Object otherend, otheroverlay;
2eec3b4e 3332
2410d73a
SM
3333 XSETMISC (otheroverlay, other);
3334 eassert (OVERLAY_VALID (otheroverlay));
2eec3b4e
RS
3335
3336 otherend = OVERLAY_END (otheroverlay);
3337 if (OVERLAY_POSITION (otherend) <= where)
3338 break;
3339 }
3340
3341 /* Add TAIL to overlays_before before OTHER. */
2410d73a
SM
3342 tail->next = other;
3343 if (other_prev)
3344 other_prev->next = tail;
2eec3b4e 3345 else
5c4f68f1 3346 buf->overlays_before = tail;
2eec3b4e
RS
3347 tail = prev;
3348 }
3349 }
3350
c2d5b10f 3351 buf->overlay_center = pos;
2eec3b4e 3352}
2b1bdf65 3353
423cdb46
KH
3354void
3355adjust_overlays_for_insert (pos, length)
c2d5b10f
SM
3356 EMACS_INT pos;
3357 EMACS_INT length;
423cdb46
KH
3358{
3359 /* After an insertion, the lists are still sorted properly,
3360 but we may need to update the value of the overlay center. */
c2d5b10f
SM
3361 if (current_buffer->overlay_center >= pos)
3362 current_buffer->overlay_center += length;
423cdb46
KH
3363}
3364
3365void
3366adjust_overlays_for_delete (pos, length)
c2d5b10f
SM
3367 EMACS_INT pos;
3368 EMACS_INT length;
423cdb46 3369{
c2d5b10f 3370 if (current_buffer->overlay_center < pos)
423cdb46
KH
3371 /* The deletion was to our right. No change needed; the before- and
3372 after-lists are still consistent. */
3373 ;
c2d5b10f 3374 else if (current_buffer->overlay_center > pos + length)
423cdb46
KH
3375 /* The deletion was to our left. We need to adjust the center value
3376 to account for the change in position, but the lists are consistent
3377 given the new value. */
c2d5b10f 3378 current_buffer->overlay_center -= length;
423cdb46
KH
3379 else
3380 /* We're right in the middle. There might be things on the after-list
3381 that now belong on the before-list. Recentering will move them,
3382 and also update the center point. */
3383 recenter_overlay_lists (current_buffer, pos);
3384}
3385
2b1bdf65
KH
3386/* Fix up overlays that were garbled as a result of permuting markers
3387 in the range START through END. Any overlay with at least one
3388 endpoint in this range will need to be unlinked from the overlay
3389 list and reinserted in its proper place.
3390 Such an overlay might even have negative size at this point.
6b61353c 3391 If so, we'll make the overlay empty. */
2b1bdf65 3392void
6b61353c 3393fix_start_end_in_overlays (start, end)
2b1bdf65
KH
3394 register int start, end;
3395{
6af718a4 3396 Lisp_Object overlay;
2410d73a 3397 struct Lisp_Overlay *before_list, *after_list;
1138e742
KR
3398 /* These are either nil, indicating that before_list or after_list
3399 should be assigned, or the cons cell the cdr of which should be
3400 assigned. */
2410d73a 3401 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
1138e742
KR
3402 /* 'Parent', likewise, indicates a cons cell or
3403 current_buffer->overlays_before or overlays_after, depending
3404 which loop we're in. */
2410d73a 3405 struct Lisp_Overlay *tail, *parent;
2b1bdf65
KH
3406 int startpos, endpos;
3407
3408 /* This algorithm shifts links around instead of consing and GCing.
3409 The loop invariant is that before_list (resp. after_list) is a
1138e742
KR
3410 well-formed list except that its last element, the CDR of beforep
3411 (resp. afterp) if beforep (afterp) isn't nil or before_list
3412 (after_list) if it is, is still uninitialized. So it's not a bug
3413 that before_list isn't initialized, although it may look
3414 strange. */
2410d73a 3415 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
2b1bdf65 3416 {
2410d73a 3417 XSETMISC (overlay, tail);
6b61353c 3418
2b1bdf65 3419 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
6b61353c
KH
3420 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3421
3422 /* If the overlay is backwards, make it empty. */
3423 if (endpos < startpos)
3424 {
3425 startpos = endpos;
3426 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3427 Qnil);
3428 }
3429
2b1bdf65
KH
3430 if (endpos < start)
3431 break;
60ebfdf3 3432
2b1bdf65
KH
3433 if (endpos < end
3434 || (startpos >= start && startpos < end))
3435 {
2b1bdf65
KH
3436 /* Add it to the end of the wrong list. Later on,
3437 recenter_overlay_lists will move it to the right place. */
c2d5b10f 3438 if (endpos < current_buffer->overlay_center)
2b1bdf65 3439 {
2410d73a 3440 if (!afterp)
1138e742
KR
3441 after_list = tail;
3442 else
2410d73a 3443 afterp->next = tail;
1138e742 3444 afterp = tail;
2b1bdf65
KH
3445 }
3446 else
3447 {
2410d73a 3448 if (!beforep)
1138e742
KR
3449 before_list = tail;
3450 else
2410d73a 3451 beforep->next = tail;
1138e742 3452 beforep = tail;
2b1bdf65 3453 }
2410d73a
SM
3454 if (!parent)
3455 current_buffer->overlays_before = tail->next;
1138e742 3456 else
2410d73a
SM
3457 parent->next = tail->next;
3458 tail = tail->next;
2b1bdf65
KH
3459 }
3460 else
2410d73a 3461 parent = tail, tail = parent->next;
2b1bdf65 3462 }
2410d73a 3463 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
2b1bdf65 3464 {
2410d73a 3465 XSETMISC (overlay, tail);
6b61353c 3466
2b1bdf65 3467 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
6b61353c
KH
3468 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3469
3470 /* If the overlay is backwards, make it empty. */
3471 if (endpos < startpos)
3472 {
3473 startpos = endpos;
3474 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
60ebfdf3 3475 Qnil);
6b61353c
KH
3476 }
3477
2b1bdf65
KH
3478 if (startpos >= end)
3479 break;
6b61353c 3480
2b1bdf65
KH
3481 if (startpos >= start
3482 || (endpos >= start && endpos < end))
3483 {
c2d5b10f 3484 if (endpos < current_buffer->overlay_center)
2b1bdf65 3485 {
2410d73a 3486 if (!afterp)
1138e742
KR
3487 after_list = tail;
3488 else
2410d73a 3489 afterp->next = tail;
1138e742 3490 afterp = tail;
2b1bdf65
KH
3491 }
3492 else
3493 {
2410d73a 3494 if (!beforep)
1138e742
KR
3495 before_list = tail;
3496 else
2410d73a 3497 beforep->next = tail;
1138e742 3498 beforep = tail;
2b1bdf65 3499 }
2410d73a
SM
3500 if (!parent)
3501 current_buffer->overlays_after = tail->next;
1138e742 3502 else
2410d73a
SM
3503 parent->next = tail->next;
3504 tail = tail->next;
2b1bdf65
KH
3505 }
3506 else
2410d73a 3507 parent = tail, tail = parent->next;
2b1bdf65
KH
3508 }
3509
3510 /* Splice the constructed (wrong) lists into the buffer's lists,
3511 and let the recenter function make it sane again. */
2410d73a 3512 if (beforep)
1138e742 3513 {
2410d73a 3514 beforep->next = current_buffer->overlays_before;
1138e742
KR
3515 current_buffer->overlays_before = before_list;
3516 }
c2d5b10f 3517 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3518
2410d73a 3519 if (afterp)
1138e742 3520 {
2410d73a 3521 afterp->next = current_buffer->overlays_after;
1138e742
KR
3522 current_buffer->overlays_after = after_list;
3523 }
c2d5b10f 3524 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3525}
3b06f880
KH
3526
3527/* We have two types of overlay: the one whose ending marker is
3528 after-insertion-marker (this is the usual case) and the one whose
3529 ending marker is before-insertion-marker. When `overlays_before'
3530 contains overlays of the latter type and the former type in this
3531 order and both overlays end at inserting position, inserting a text
3532 increases only the ending marker of the latter type, which results
3533 in incorrect ordering of `overlays_before'.
3534
3535 This function fixes ordering of overlays in the slot
3536 `overlays_before' of the buffer *BP. Before the insertion, `point'
3537 was at PREV, and now is at POS. */
3538
01136e9b 3539void
3b06f880
KH
3540fix_overlays_before (bp, prev, pos)
3541 struct buffer *bp;
c2d5b10f 3542 EMACS_INT prev, pos;
3b06f880 3543{
2410d73a
SM
3544 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3545 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3546 Lisp_Object tem;
c2d5b10f 3547 EMACS_INT end;
3b06f880
KH
3548
3549 /* After the insertion, the several overlays may be in incorrect
3550 order. The possibility is that, in the list `overlays_before',
3551 an overlay which ends at POS appears after an overlay which ends
3552 at PREV. Since POS is greater than PREV, we must fix the
3553 ordering of these overlays, by moving overlays ends at POS before
3554 the overlays ends at PREV. */
3555
3556 /* At first, find a place where disordered overlays should be linked
3557 in. It is where an overlay which end before POS exists. (i.e. an
3558 overlay whose ending marker is after-insertion-marker if disorder
3559 exists). */
2410d73a
SM
3560 while (tail
3561 && (XSETMISC (tem, tail),
3562 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
1138e742
KR
3563 {
3564 parent = tail;
2410d73a 3565 tail = tail->next;
1138e742 3566 }
3b06f880
KH
3567
3568 /* If we don't find such an overlay,
3569 or the found one ends before PREV,
3570 or the found one is the last one in the list,
3571 we don't have to fix anything. */
f93ad4cf 3572 if (!tail || end < prev || !tail->next)
3b06f880
KH
3573 return;
3574
1138e742
KR
3575 right_pair = parent;
3576 parent = tail;
2410d73a 3577 tail = tail->next;
3b06f880 3578
1138e742 3579 /* Now, end position of overlays in the list TAIL should be before
3b06f880 3580 or equal to PREV. In the loop, an overlay which ends at POS is
1138e742
KR
3581 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3582 we found an overlay which ends before PREV, the remaining
3583 overlays are in correct order. */
2410d73a 3584 while (tail)
3b06f880 3585 {
2410d73a
SM
3586 XSETMISC (tem, tail);
3587 end = OVERLAY_POSITION (OVERLAY_END (tem));
3b06f880
KH
3588
3589 if (end == pos)
3590 { /* This overlay is disordered. */
2410d73a 3591 struct Lisp_Overlay *found = tail;
3b06f880
KH
3592
3593 /* Unlink the found overlay. */
2410d73a
SM
3594 tail = found->next;
3595 parent->next = tail;
1138e742
KR
3596 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3597 and link it into the right place. */
2410d73a 3598 if (!right_pair)
1138e742 3599 {
2410d73a 3600 found->next = bp->overlays_before;
1138e742
KR
3601 bp->overlays_before = found;
3602 }
3603 else
3604 {
2410d73a
SM
3605 found->next = right_pair->next;
3606 right_pair->next = found;
1138e742 3607 }
3b06f880
KH
3608 }
3609 else if (end == prev)
1138e742
KR
3610 {
3611 parent = tail;
2410d73a 3612 tail = tail->next;
1138e742 3613 }
3b06f880
KH
3614 else /* No more disordered overlay. */
3615 break;
3616 }
3617}
2eec3b4e 3618\f
52f8ec73 3619DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
7ee72033
MB
3620 doc: /* Return t if OBJECT is an overlay. */)
3621 (object)
52f8ec73
JB
3622 Lisp_Object object;
3623{
3624 return (OVERLAYP (object) ? Qt : Qnil);
3625}
3626
acac2700 3627DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
7ee72033 3628 doc: /* Create a new overlay with range BEG to END in BUFFER.
018ba359
PJ
3629If omitted, BUFFER defaults to the current buffer.
3630BEG and END may be integers or markers.
a625ee20
RS
3631The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3632for the front of the overlay advance when text is inserted there
63af6055 3633\(which means the text *is not* included in the overlay).
a625ee20
RS
3634The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3635for the rear of the overlay advance when text is inserted there
63af6055 3636\(which means the text *is* included in the overlay). */)
7ee72033 3637 (beg, end, buffer, front_advance, rear_advance)
5c4f68f1 3638 Lisp_Object beg, end, buffer;
acac2700 3639 Lisp_Object front_advance, rear_advance;
2eec3b4e
RS
3640{
3641 Lisp_Object overlay;
5c4f68f1 3642 struct buffer *b;
2eec3b4e 3643
5c4f68f1 3644 if (NILP (buffer))
67180c6a 3645 XSETBUFFER (buffer, current_buffer);
883047b9 3646 else
b7826503 3647 CHECK_BUFFER (buffer);
883047b9
JB
3648 if (MARKERP (beg)
3649 && ! EQ (Fmarker_buffer (beg), buffer))
3650 error ("Marker points into wrong buffer");
3651 if (MARKERP (end)
3652 && ! EQ (Fmarker_buffer (end), buffer))
3653 error ("Marker points into wrong buffer");
2eec3b4e 3654
b7826503
PJ
3655 CHECK_NUMBER_COERCE_MARKER (beg);
3656 CHECK_NUMBER_COERCE_MARKER (end);
5c4f68f1 3657
883047b9 3658 if (XINT (beg) > XINT (end))
5c4f68f1 3659 {
c99fc30f
KH
3660 Lisp_Object temp;
3661 temp = beg; beg = end; end = temp;
5c4f68f1 3662 }
883047b9
JB
3663
3664 b = XBUFFER (buffer);
3665
3666 beg = Fset_marker (Fmake_marker (), beg, buffer);
3667 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 3668
acac2700
RS
3669 if (!NILP (front_advance))
3670 XMARKER (beg)->insertion_type = 1;
3671 if (!NILP (rear_advance))
3672 XMARKER (end)->insertion_type = 1;
597dd755 3673
48e2e3ba 3674 overlay = allocate_misc ();
89ca3e1b 3675 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
48e2e3ba
KH
3676 XOVERLAY (overlay)->start = beg;
3677 XOVERLAY (overlay)->end = end;
3678 XOVERLAY (overlay)->plist = Qnil;
2410d73a 3679 XOVERLAY (overlay)->next = NULL;
2eec3b4e 3680
177c0ea7 3681 /* Put the new overlay on the wrong list. */
2eec3b4e 3682 end = OVERLAY_END (overlay);
c2d5b10f 3683 if (OVERLAY_POSITION (end) < b->overlay_center)
2410d73a
SM
3684 {
3685 if (b->overlays_after)
3686 XOVERLAY (overlay)->next = b->overlays_after;
3687 b->overlays_after = XOVERLAY (overlay);
3688 }
2eec3b4e 3689 else
2410d73a
SM
3690 {
3691 if (b->overlays_before)
3692 XOVERLAY (overlay)->next = b->overlays_before;
3693 b->overlays_before = XOVERLAY (overlay);
3694 }
2eec3b4e
RS
3695
3696 /* This puts it in the right list, and in the right order. */
c2d5b10f 3697 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 3698
b61982dd
JB
3699 /* We don't need to redisplay the region covered by the overlay, because
3700 the overlay has no properties at the moment. */
3701
2eec3b4e
RS
3702 return overlay;
3703}
876aa27c
RS
3704\f
3705/* Mark a section of BUF as needing redisplay because of overlays changes. */
3706
3707static void
3708modify_overlay (buf, start, end)
3709 struct buffer *buf;
c2d5b10f 3710 EMACS_INT start, end;
876aa27c 3711{
876aa27c
RS
3712 if (start > end)
3713 {
3714 int temp = start;
26f545d7
GM
3715 start = end;
3716 end = temp;
876aa27c
RS
3717 }
3718
481b5054 3719 BUF_COMPUTE_UNCHANGED (buf, start, end);
177c0ea7 3720
876aa27c
RS
3721 /* If this is a buffer not in the selected window,
3722 we must do other windows. */
3723 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3724 windows_or_buffers_changed = 1;
876aa27c
RS
3725 /* If multiple windows show this buffer, we must do other windows. */
3726 else if (buffer_shared > 1)
3727 windows_or_buffers_changed = 1;
18700091
KS
3728 /* If we modify an overlay at the end of the buffer, we cannot
3729 be sure that window end is still valid. */
3730 else if (end >= ZV && start <= ZV)
3731 windows_or_buffers_changed = 1;
876aa27c 3732
d8b9150f 3733 ++BUF_OVERLAY_MODIFF (buf);
876aa27c 3734}
2eec3b4e 3735
018ba359 3736\f
2e34157c
RS
3737Lisp_Object Fdelete_overlay ();
3738
2410d73a
SM
3739static struct Lisp_Overlay *
3740unchain_overlay (list, overlay)
3741 struct Lisp_Overlay *list, *overlay;
3742{
3743 struct Lisp_Overlay *tmp, *prev;
3744 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3745 if (tmp == overlay)
3746 {
3747 if (prev)
3748 prev->next = tmp->next;
3749 else
3750 list = tmp->next;
3751 overlay->next = NULL;
3752 break;
3753 }
3754 return list;
3755}
3756
5c4f68f1 3757DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
7ee72033 3758 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
018ba359
PJ
3759If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3760If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
7ee72033
MB
3761buffer. */)
3762 (overlay, beg, end, buffer)
5c4f68f1 3763 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 3764{
0a4469c9
RS
3765 struct buffer *b, *ob;
3766 Lisp_Object obuffer;
aed13378 3767 int count = SPECPDL_INDEX ();
5c4f68f1 3768
b7826503 3769 CHECK_OVERLAY (overlay);
5c4f68f1
JB
3770 if (NILP (buffer))
3771 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 3772 if (NILP (buffer))
67180c6a 3773 XSETBUFFER (buffer, current_buffer);
b7826503 3774 CHECK_BUFFER (buffer);
883047b9
JB
3775
3776 if (MARKERP (beg)
3777 && ! EQ (Fmarker_buffer (beg), buffer))
3778 error ("Marker points into wrong buffer");
3779 if (MARKERP (end)
3780 && ! EQ (Fmarker_buffer (end), buffer))
3781 error ("Marker points into wrong buffer");
3782
b7826503
PJ
3783 CHECK_NUMBER_COERCE_MARKER (beg);
3784 CHECK_NUMBER_COERCE_MARKER (end);
b61982dd 3785
9d7608b7
KH
3786 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3787 return Fdelete_overlay (overlay);
0a4469c9 3788
b61982dd
JB
3789 if (XINT (beg) > XINT (end))
3790 {
c99fc30f
KH
3791 Lisp_Object temp;
3792 temp = beg; beg = end; end = temp;
b61982dd
JB
3793 }
3794
9d7608b7
KH
3795 specbind (Qinhibit_quit, Qt);
3796
0a4469c9 3797 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 3798 b = XBUFFER (buffer);
8801a864 3799 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
2eec3b4e 3800
c82ed728 3801 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 3802 if (!EQ (buffer, obuffer))
50760c4a
RS
3803 {
3804 /* Redisplay where the overlay was. */
3805 if (!NILP (obuffer))
3806 {
2e34157c
RS
3807 int o_beg;
3808 int o_end;
50760c4a 3809
80509f2f
RS
3810 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3811 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
50760c4a 3812
2e34157c 3813 modify_overlay (ob, o_beg, o_end);
50760c4a
RS
3814 }
3815
3816 /* Redisplay where the overlay is going to be. */
876aa27c 3817 modify_overlay (b, XINT (beg), XINT (end));
50760c4a 3818 }
c82ed728
JB
3819 else
3820 /* Redisplay the area the overlay has just left, or just enclosed. */
3821 {
2e34157c 3822 int o_beg, o_end;
c82ed728 3823
80509f2f
RS
3824 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3825 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
c82ed728 3826
2e34157c
RS
3827 if (o_beg == XINT (beg))
3828 modify_overlay (b, o_end, XINT (end));
3829 else if (o_end == XINT (end))
3830 modify_overlay (b, o_beg, XINT (beg));
c82ed728
JB
3831 else
3832 {
2e34157c
RS
3833 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3834 if (XINT (end) > o_end) o_end = XINT (end);
3835 modify_overlay (b, o_beg, o_end);
c82ed728
JB
3836 }
3837 }
b61982dd 3838
0a4469c9
RS
3839 if (!NILP (obuffer))
3840 {
2410d73a
SM
3841 ob->overlays_before
3842 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3843 ob->overlays_after
3844 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3845 eassert (XOVERLAY (overlay)->next == NULL);
0a4469c9 3846 }
5c4f68f1
JB
3847
3848 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3849 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e 3850
177c0ea7 3851 /* Put the overlay on the wrong list. */
2eec3b4e 3852 end = OVERLAY_END (overlay);
c2d5b10f 3853 if (OVERLAY_POSITION (end) < b->overlay_center)
2410d73a 3854 {
8f924df7
KH
3855 XOVERLAY (overlay)->next = b->overlays_after;
3856 b->overlays_after = XOVERLAY (overlay);
2410d73a 3857 }
2eec3b4e 3858 else
2410d73a 3859 {
8f924df7
KH
3860 XOVERLAY (overlay)->next = b->overlays_before;
3861 b->overlays_before = XOVERLAY (overlay);
2410d73a 3862 }
2eec3b4e
RS
3863
3864 /* This puts it in the right list, and in the right order. */
c2d5b10f 3865 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 3866
0a4469c9 3867 return unbind_to (count, overlay);
2eec3b4e
RS
3868}
3869
3870DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
7ee72033
MB
3871 doc: /* Delete the overlay OVERLAY from its buffer. */)
3872 (overlay)
5c4f68f1 3873 Lisp_Object overlay;
2eec3b4e 3874{
0a4469c9 3875 Lisp_Object buffer;
5c4f68f1 3876 struct buffer *b;
aed13378 3877 int count = SPECPDL_INDEX ();
5c4f68f1 3878
b7826503 3879 CHECK_OVERLAY (overlay);
52f8ec73 3880
0a4469c9
RS
3881 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3882 if (NILP (buffer))
3883 return Qnil;
3884
3885 b = XBUFFER (buffer);
0a4469c9 3886 specbind (Qinhibit_quit, Qt);
177c0ea7 3887
2410d73a
SM
3888 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3889 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3890 eassert (XOVERLAY (overlay)->next == NULL);
876aa27c 3891 modify_overlay (b,
8231a9aa
RS
3892 marker_position (OVERLAY_START (overlay)),
3893 marker_position (OVERLAY_END (overlay)));
3ece337a
JB
3894 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3895 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3896
e58c389d 3897 /* When deleting an overlay with before or after strings, turn off
26f545d7
GM
3898 display optimizations for the affected buffer, on the basis that
3899 these strings may contain newlines. This is easier to do than to
3900 check for that situation during redisplay. */
3901 if (!windows_or_buffers_changed
3902 && (!NILP (Foverlay_get (overlay, Qbefore_string))
3903 || !NILP (Foverlay_get (overlay, Qafter_string))))
3904 b->prevent_redisplay_optimizations_p = 1;
3905
0a4469c9 3906 return unbind_to (count, Qnil);
2eec3b4e
RS
3907}
3908\f
8ebafa8d
JB
3909/* Overlay dissection functions. */
3910
3911DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
7ee72033
MB
3912 doc: /* Return the position at which OVERLAY starts. */)
3913 (overlay)
8ebafa8d
JB
3914 Lisp_Object overlay;
3915{
b7826503 3916 CHECK_OVERLAY (overlay);
8ebafa8d
JB
3917
3918 return (Fmarker_position (OVERLAY_START (overlay)));
3919}
3920
3921DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
7ee72033
MB
3922 doc: /* Return the position at which OVERLAY ends. */)
3923 (overlay)
8ebafa8d
JB
3924 Lisp_Object overlay;
3925{
b7826503 3926 CHECK_OVERLAY (overlay);
8ebafa8d
JB
3927
3928 return (Fmarker_position (OVERLAY_END (overlay)));
3929}
3930
3931DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
563f7128
LK
3932 doc: /* Return the buffer OVERLAY belongs to.
3933Return nil if OVERLAY has been deleted. */)
7ee72033 3934 (overlay)
8ebafa8d
JB
3935 Lisp_Object overlay;
3936{
b7826503 3937 CHECK_OVERLAY (overlay);
8ebafa8d
JB
3938
3939 return Fmarker_buffer (OVERLAY_START (overlay));
3940}
3941
3942DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
7ee72033 3943 doc: /* Return a list of the properties on OVERLAY.
018ba359 3944This is a copy of OVERLAY's plist; modifying its conses has no effect on
7ee72033
MB
3945OVERLAY. */)
3946 (overlay)
8ebafa8d
JB
3947 Lisp_Object overlay;
3948{
b7826503 3949 CHECK_OVERLAY (overlay);
8ebafa8d 3950
48e2e3ba 3951 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
3952}
3953
3954\f
2eec3b4e 3955DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
7ee72033
MB
3956 doc: /* Return a list of the overlays that contain position POS. */)
3957 (pos)
2eec3b4e
RS
3958 Lisp_Object pos;
3959{
3960 int noverlays;
2eec3b4e
RS
3961 Lisp_Object *overlay_vec;
3962 int len;
3963 Lisp_Object result;
3964
b7826503 3965 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
3966
3967 len = 10;
a9800ae8 3968 /* We can't use alloca here because overlays_at can call xrealloc. */
2eec3b4e
RS
3969 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3970
3971 /* Put all the overlays we want in a vector in overlay_vec.
3972 Store the length in len. */
2a77a7d7 3973 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 3974 (int *) 0, (int *) 0, 0);
2eec3b4e
RS
3975
3976 /* Make a list of them all. */
3977 result = Flist (noverlays, overlay_vec);
3978
9ac0d9e0 3979 xfree (overlay_vec);
2eec3b4e
RS
3980 return result;
3981}
3982
74514898 3983DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
7ee72033 3984 doc: /* Return a list of the overlays that overlap the region BEG ... END.
018ba359
PJ
3985Overlap means that at least one character is contained within the overlay
3986and also contained within the specified region.
3987Empty overlays are included in the result if they are located at BEG
7ee72033
MB
3988or between BEG and END. */)
3989 (beg, end)
74514898
RS
3990 Lisp_Object beg, end;
3991{
3992 int noverlays;
3993 Lisp_Object *overlay_vec;
3994 int len;
3995 Lisp_Object result;
3996
b7826503
PJ
3997 CHECK_NUMBER_COERCE_MARKER (beg);
3998 CHECK_NUMBER_COERCE_MARKER (end);
74514898
RS
3999
4000 len = 10;
4001 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4002
4003 /* Put all the overlays we want in a vector in overlay_vec.
4004 Store the length in len. */
4005 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4006 (int *) 0, (int *) 0);
4007
4008 /* Make a list of them all. */
4009 result = Flist (noverlays, overlay_vec);
4010
4011 xfree (overlay_vec);
4012 return result;
4013}
4014
2eec3b4e 4015DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
efc7e75f 4016 1, 1, 0,
7ee72033 4017 doc: /* Return the next position after POS where an overlay starts or ends.
624d2678
RS
4018If there are no overlay boundaries from POS to (point-max),
4019the value is (point-max). */)
7ee72033 4020 (pos)
2eec3b4e
RS
4021 Lisp_Object pos;
4022{
4023 int noverlays;
4024 int endpos;
4025 Lisp_Object *overlay_vec;
4026 int len;
2eec3b4e
RS
4027 int i;
4028
b7826503 4029 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
4030
4031 len = 10;
4032 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4033
4034 /* Put all the overlays we want in a vector in overlay_vec.
4035 Store the length in len.
4036 endpos gets the position where the next overlay starts. */
2a77a7d7 4037 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 4038 &endpos, (int *) 0, 1);
2eec3b4e
RS
4039
4040 /* If any of these overlays ends before endpos,
4041 use its ending point instead. */
4042 for (i = 0; i < noverlays; i++)
4043 {
4044 Lisp_Object oend;
4045 int oendpos;
4046
4047 oend = OVERLAY_END (overlay_vec[i]);
4048 oendpos = OVERLAY_POSITION (oend);
4049 if (oendpos < endpos)
4050 endpos = oendpos;
1ab256cb
RM
4051 }
4052
9ac0d9e0 4053 xfree (overlay_vec);
2eec3b4e
RS
4054 return make_number (endpos);
4055}
239c932b
RS
4056
4057DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4058 Sprevious_overlay_change, 1, 1, 0,
7ee72033 4059 doc: /* Return the previous position before POS where an overlay starts or ends.
624d2678
RS
4060If there are no overlay boundaries from (point-min) to POS,
4061the value is (point-min). */)
7ee72033 4062 (pos)
239c932b
RS
4063 Lisp_Object pos;
4064{
4065 int noverlays;
4066 int prevpos;
4067 Lisp_Object *overlay_vec;
4068 int len;
239c932b 4069
b7826503 4070 CHECK_NUMBER_COERCE_MARKER (pos);
239c932b 4071
624bbdc4
RS
4072 /* At beginning of buffer, we know the answer;
4073 avoid bug subtracting 1 below. */
4074 if (XINT (pos) == BEGV)
4075 return pos;
4076
017f0539
GM
4077 len = 10;
4078 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4079
239c932b
RS
4080 /* Put all the overlays we want in a vector in overlay_vec.
4081 Store the length in len.
daa1c109 4082 prevpos gets the position of the previous change. */
2a77a7d7 4083 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 4084 (int *) 0, &prevpos, 1);
239c932b 4085
239c932b
RS
4086 xfree (overlay_vec);
4087 return make_number (prevpos);
4088}
2eec3b4e
RS
4089\f
4090/* These functions are for debugging overlays. */
4091
4092DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
7ee72033 4093 doc: /* Return a pair of lists giving all the overlays of the current buffer.
018ba359
PJ
4094The car has all the overlays before the overlay center;
4095the cdr has all the overlays after the overlay center.
4096Recentering overlays moves overlays between these lists.
4097The lists you get are copies, so that changing them has no effect.
7ee72033
MB
4098However, the overlays you get are the real objects that the buffer uses. */)
4099 ()
2eec3b4e 4100{
2410d73a
SM
4101 struct Lisp_Overlay *ol;
4102 Lisp_Object before = Qnil, after = Qnil, tmp;
4103 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4104 {
4105 XSETMISC (tmp, ol);
4106 before = Fcons (tmp, before);
4107 }
4108 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4109 {
4110 XSETMISC (tmp, ol);
4111 after = Fcons (tmp, after);
4112 }
4113 return Fcons (Fnreverse (before), Fnreverse (after));
2eec3b4e
RS
4114}
4115
4116DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
c87426c5
RS
4117 doc: /* Recenter the overlays of the current buffer around position POS.
4118That makes overlay lookup faster for positions near POS (but perhaps slower
4119for positions far away from POS). */)
7ee72033 4120 (pos)
2eec3b4e
RS
4121 Lisp_Object pos;
4122{
b7826503 4123 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e 4124
5c4f68f1 4125 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
4126 return Qnil;
4127}
4128\f
4129DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
7ee72033
MB
4130 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4131 (overlay, prop)
2eec3b4e
RS
4132 Lisp_Object overlay, prop;
4133{
b7826503 4134 CHECK_OVERLAY (overlay);
9a593927 4135 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
2eec3b4e
RS
4136}
4137
4138DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
7ee72033
MB
4139 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4140 (overlay, prop, value)
2eec3b4e
RS
4141 Lisp_Object overlay, prop, value;
4142{
48e2e3ba 4143 Lisp_Object tail, buffer;
9d7608b7 4144 int changed;
2eec3b4e 4145
b7826503 4146 CHECK_OVERLAY (overlay);
b61982dd 4147
274a9425
RS
4148 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4149
48e2e3ba 4150 for (tail = XOVERLAY (overlay)->plist;
7539e11f
KR
4151 CONSP (tail) && CONSP (XCDR (tail));
4152 tail = XCDR (XCDR (tail)))
4153 if (EQ (XCAR (tail), prop))
274a9425 4154 {
7539e11f 4155 changed = !EQ (XCAR (XCDR (tail)), value);
f3fbd155 4156 XSETCAR (XCDR (tail), value);
9d7608b7 4157 goto found;
274a9425 4158 }
9d7608b7
KH
4159 /* It wasn't in the list, so add it to the front. */
4160 changed = !NILP (value);
48e2e3ba
KH
4161 XOVERLAY (overlay)->plist
4162 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
9d7608b7
KH
4163 found:
4164 if (! NILP (buffer))
4165 {
4166 if (changed)
876aa27c 4167 modify_overlay (XBUFFER (buffer),
26f545d7
GM
4168 marker_position (OVERLAY_START (overlay)),
4169 marker_position (OVERLAY_END (overlay)));
9d7608b7
KH
4170 if (EQ (prop, Qevaporate) && ! NILP (value)
4171 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4172 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4173 Fdelete_overlay (overlay);
4174 }
7d63db98 4175
2eec3b4e 4176 return value;
1ab256cb
RM
4177}
4178\f
9115729e
KH
4179/* Subroutine of report_overlay_modification. */
4180
4181/* Lisp vector holding overlay hook functions to call.
4182 Vector elements come in pairs.
4183 Each even-index element is a list of hook functions.
4184 The following odd-index element is the overlay they came from.
4185
4186 Before the buffer change, we fill in this vector
4187 as we call overlay hook functions.
4188 After the buffer change, we get the functions to call from this vector.
4189 This way we always call the same functions before and after the change. */
4190static Lisp_Object last_overlay_modification_hooks;
4191
4192/* Number of elements actually used in last_overlay_modification_hooks. */
4193static int last_overlay_modification_hooks_used;
4194
4195/* Add one functionlist/overlay pair
4196 to the end of last_overlay_modification_hooks. */
4197
4198static void
4199add_overlay_mod_hooklist (functionlist, overlay)
4200 Lisp_Object functionlist, overlay;
4201{
4202 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4203
4204 if (last_overlay_modification_hooks_used == oldsize)
4205 {
4206 Lisp_Object old;
4207 old = last_overlay_modification_hooks;
4208 last_overlay_modification_hooks
4209 = Fmake_vector (make_number (oldsize * 2), Qnil);
0b1f1b09
RS
4210 bcopy (XVECTOR (old)->contents,
4211 XVECTOR (last_overlay_modification_hooks)->contents,
9115729e
KH
4212 sizeof (Lisp_Object) * oldsize);
4213 }
2410d73a
SM
4214 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = functionlist;
4215 AREF (last_overlay_modification_hooks, last_overlay_modification_hooks_used++) = overlay;
9115729e
KH
4216}
4217\f
173f2a64
RS
4218/* Run the modification-hooks of overlays that include
4219 any part of the text in START to END.
9115729e
KH
4220 If this change is an insertion, also
4221 run the insert-before-hooks of overlay starting at END,
930a9140
RS
4222 and the insert-after-hooks of overlay ending at START.
4223
4224 This is called both before and after the modification.
4225 AFTER is nonzero when we call after the modification.
4226
9115729e
KH
4227 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4228 When AFTER is nonzero, they are the start position,
4229 the position after the inserted new text,
4230 and the length of deleted or replaced old text. */
173f2a64
RS
4231
4232void
930a9140 4233report_overlay_modification (start, end, after, arg1, arg2, arg3)
173f2a64 4234 Lisp_Object start, end;
930a9140
RS
4235 int after;
4236 Lisp_Object arg1, arg2, arg3;
173f2a64 4237{
2410d73a
SM
4238 Lisp_Object prop, overlay;
4239 struct Lisp_Overlay *tail;
9115729e
KH
4240 /* 1 if this change is an insertion. */
4241 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
a615c6dc 4242 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
55b48893
RS
4243
4244 overlay = Qnil;
2410d73a 4245 tail = NULL;
9115729e 4246
27c6b98e
SM
4247 /* We used to run the functions as soon as we found them and only register
4248 them in last_overlay_modification_hooks for the purpose of the `after'
4249 case. But running elisp code as we traverse the list of overlays is
4250 painful because the list can be modified by the elisp code so we had to
4251 copy at several places. We now simply do a read-only traversal that
4252 only collects the functions to run and we run them afterwards. It's
4253 simpler, especially since all the code was already there. -stef */
4254
a615c6dc 4255 if (!after)
173f2a64 4256 {
a615c6dc
SM
4257 /* We are being called before a change.
4258 Scan the overlays to find the functions to call. */
4259 last_overlay_modification_hooks_used = 0;
2410d73a 4260 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
173f2a64 4261 {
a615c6dc
SM
4262 int startpos, endpos;
4263 Lisp_Object ostart, oend;
4264
2410d73a 4265 XSETMISC (overlay, tail);
a615c6dc
SM
4266
4267 ostart = OVERLAY_START (overlay);
4268 oend = OVERLAY_END (overlay);
4269 endpos = OVERLAY_POSITION (oend);
4270 if (XFASTINT (start) > endpos)
4271 break;
4272 startpos = OVERLAY_POSITION (ostart);
4273 if (insertion && (XFASTINT (start) == startpos
4274 || XFASTINT (end) == startpos))
5fb5aa33 4275 {
a615c6dc
SM
4276 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4277 if (!NILP (prop))
4278 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4279 }
a615c6dc
SM
4280 if (insertion && (XFASTINT (start) == endpos
4281 || XFASTINT (end) == endpos))
5fb5aa33 4282 {
a615c6dc
SM
4283 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4284 if (!NILP (prop))
4285 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4286 }
a615c6dc
SM
4287 /* Test for intersecting intervals. This does the right thing
4288 for both insertion and deletion. */
4289 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4290 {
a615c6dc
SM
4291 prop = Foverlay_get (overlay, Qmodification_hooks);
4292 if (!NILP (prop))
4293 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4294 }
173f2a64 4295 }
60ebfdf3 4296
2410d73a 4297 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
173f2a64 4298 {
a615c6dc
SM
4299 int startpos, endpos;
4300 Lisp_Object ostart, oend;
4301
2410d73a 4302 XSETMISC (overlay, tail);
a615c6dc
SM
4303
4304 ostart = OVERLAY_START (overlay);
4305 oend = OVERLAY_END (overlay);
4306 startpos = OVERLAY_POSITION (ostart);
4307 endpos = OVERLAY_POSITION (oend);
4308 if (XFASTINT (end) < startpos)
4309 break;
4310 if (insertion && (XFASTINT (start) == startpos
4311 || XFASTINT (end) == startpos))
5fb5aa33 4312 {
a615c6dc
SM
4313 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4314 if (!NILP (prop))
4315 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4316 }
a615c6dc
SM
4317 if (insertion && (XFASTINT (start) == endpos
4318 || XFASTINT (end) == endpos))
5fb5aa33 4319 {
a615c6dc
SM
4320 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4321 if (!NILP (prop))
4322 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4323 }
a615c6dc
SM
4324 /* Test for intersecting intervals. This does the right thing
4325 for both insertion and deletion. */
4326 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4327 {
a615c6dc
SM
4328 prop = Foverlay_get (overlay, Qmodification_hooks);
4329 if (!NILP (prop))
4330 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4331 }
173f2a64
RS
4332 }
4333 }
55b48893 4334
a615c6dc
SM
4335 GCPRO4 (overlay, arg1, arg2, arg3);
4336 {
4337 /* Call the functions recorded in last_overlay_modification_hooks.
4338 First copy the vector contents, in case some of these hooks
4339 do subsequent modification of the buffer. */
4340 int size = last_overlay_modification_hooks_used;
4341 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4342 int i;
4343
4344 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4345 copy, size * sizeof (Lisp_Object));
4346 gcpro1.var = copy;
4347 gcpro1.nvars = size;
4348
4349 for (i = 0; i < size;)
4350 {
4351 Lisp_Object prop, overlay;
4352 prop = copy[i++];
4353 overlay = copy[i++];
4354 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4355 }
4356 }
55b48893 4357 UNGCPRO;
173f2a64
RS
4358}
4359
4360static void
930a9140
RS
4361call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4362 Lisp_Object list, overlay;
4363 int after;
4364 Lisp_Object arg1, arg2, arg3;
173f2a64 4365{
930a9140 4366 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9115729e 4367
930a9140 4368 GCPRO4 (list, arg1, arg2, arg3);
9115729e 4369
6d70a280 4370 while (CONSP (list))
173f2a64 4371 {
930a9140 4372 if (NILP (arg3))
6d70a280 4373 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
930a9140 4374 else
6d70a280
SM
4375 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4376 list = XCDR (list);
173f2a64
RS
4377 }
4378 UNGCPRO;
4379}
9d7608b7
KH
4380
4381/* Delete any zero-sized overlays at position POS, if the `evaporate'
4382 property is set. */
4383void
4384evaporate_overlays (pos)
c2d5b10f 4385 EMACS_INT pos;
9d7608b7 4386{
2410d73a
SM
4387 Lisp_Object overlay, hit_list;
4388 struct Lisp_Overlay *tail;
9d7608b7
KH
4389
4390 hit_list = Qnil;
c2d5b10f 4391 if (pos <= current_buffer->overlay_center)
2410d73a 4392 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
9d7608b7
KH
4393 {
4394 int endpos;
2410d73a 4395 XSETMISC (overlay, tail);
9d7608b7
KH
4396 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4397 if (endpos < pos)
4398 break;
4399 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
c3935f9d 4400 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4401 hit_list = Fcons (overlay, hit_list);
4402 }
4403 else
2410d73a 4404 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
9d7608b7
KH
4405 {
4406 int startpos;
2410d73a 4407 XSETMISC (overlay, tail);
9d7608b7
KH
4408 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4409 if (startpos > pos)
4410 break;
4411 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
c3935f9d 4412 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4413 hit_list = Fcons (overlay, hit_list);
4414 }
7539e11f
KR
4415 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4416 Fdelete_overlay (XCAR (hit_list));
9d7608b7 4417}
173f2a64 4418\f
54dfdeb0 4419/* Somebody has tried to store a value with an unacceptable type
1bf08baf
KH
4420 in the slot with offset OFFSET. */
4421
0fa3ba92 4422void
54dfdeb0
KH
4423buffer_slot_type_mismatch (offset)
4424 int offset;
0fa3ba92 4425{
54dfdeb0 4426 Lisp_Object sym;
0fa3ba92 4427 char *type_name;
177c0ea7 4428
7313acd0 4429 switch (XINT (PER_BUFFER_TYPE (offset)))
0fa3ba92 4430 {
7c02e886
GM
4431 case Lisp_Int:
4432 type_name = "integers";
4433 break;
177c0ea7 4434
7c02e886
GM
4435 case Lisp_String:
4436 type_name = "strings";
4437 break;
177c0ea7 4438
7c02e886
GM
4439 case Lisp_Symbol:
4440 type_name = "symbols";
4441 break;
177c0ea7 4442
0fa3ba92
JB
4443 default:
4444 abort ();
4445 }
4446
7313acd0 4447 sym = PER_BUFFER_SYMBOL (offset);
1bf08baf 4448 error ("Only %s should be stored in the buffer-local variable %s",
d5db4077 4449 type_name, SDATA (SYMBOL_NAME (sym)));
0fa3ba92 4450}
7c02e886 4451
0fa3ba92 4452\f
b86af064
GM
4453/***********************************************************************
4454 Allocation with mmap
4455 ***********************************************************************/
4456
4457#ifdef USE_MMAP_FOR_BUFFERS
4458
4459#include <sys/types.h>
4460#include <sys/mman.h>
4461
4462#ifndef MAP_ANON
4463#ifdef MAP_ANONYMOUS
4464#define MAP_ANON MAP_ANONYMOUS
4465#else
4466#define MAP_ANON 0
4467#endif
4468#endif
4469
09dfdf85
GM
4470#ifndef MAP_FAILED
4471#define MAP_FAILED ((void *) -1)
4472#endif
4473
b86af064
GM
4474#include <stdio.h>
4475#include <errno.h>
4476
4477#if MAP_ANON == 0
4478#include <fcntl.h>
4479#endif
4480
4481#include "coding.h"
4482
4483
4484/* Memory is allocated in regions which are mapped using mmap(2).
4485 The current implementation lets the system select mapped
4486 addresses; we're not using MAP_FIXED in general, except when
4487 trying to enlarge regions.
4488
4489 Each mapped region starts with a mmap_region structure, the user
4490 area starts after that structure, aligned to MEM_ALIGN.
4491
4492 +-----------------------+
4493 | struct mmap_info + |
4494 | padding |
4495 +-----------------------+
4496 | user data |
4497 | |
4498 | |
4499 +-----------------------+ */
4500
4501struct mmap_region
4502{
4503 /* User-specified size. */
4504 size_t nbytes_specified;
177c0ea7 4505
b86af064
GM
4506 /* Number of bytes mapped */
4507 size_t nbytes_mapped;
4508
4509 /* Pointer to the location holding the address of the memory
4510 allocated with the mmap'd block. The variable actually points
4511 after this structure. */
4512 POINTER_TYPE **var;
4513
4514 /* Next and previous in list of all mmap'd regions. */
4515 struct mmap_region *next, *prev;
4516};
4517
4518/* Doubly-linked list of mmap'd regions. */
4519
4520static struct mmap_region *mmap_regions;
4521
4522/* File descriptor for mmap. If we don't have anonymous mapping,
4523 /dev/zero will be opened on it. */
4524
4525static int mmap_fd;
4526
4527/* Temporary storage for mmap_set_vars, see there. */
4528
4529static struct mmap_region *mmap_regions_1;
4530static int mmap_fd_1;
4531
4532/* Page size on this system. */
4533
4534static int mmap_page_size;
4535
4536/* 1 means mmap has been intialized. */
4537
4538static int mmap_initialized_p;
4539
4540/* Value is X rounded up to the next multiple of N. */
4541
4542#define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4543
4544/* Size of mmap_region structure plus padding. */
4545
4546#define MMAP_REGION_STRUCT_SIZE \
4547 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4548
4549/* Given a pointer P to the start of the user-visible part of a mapped
4550 region, return a pointer to the start of the region. */
4551
4552#define MMAP_REGION(P) \
4553 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4554
4555/* Given a pointer P to the start of a mapped region, return a pointer
4556 to the start of the user-visible part of the region. */
4557
4558#define MMAP_USER_AREA(P) \
4559 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4560
4561#define MEM_ALIGN sizeof (double)
4562
7273faa1
DL
4563/* Predicate returning true if part of the address range [START .. END]
4564 is currently mapped. Used to prevent overwriting an existing
08327b22
GM
4565 memory mapping.
4566
4567 Default is to conservativly assume the address range is occupied by
4568 something else. This can be overridden by system configuration
4569 files if system-specific means to determine this exists. */
4570
4571#ifndef MMAP_ALLOCATED_P
4572#define MMAP_ALLOCATED_P(start, end) 1
4573#endif
4574
b86af064
GM
4575/* Function prototypes. */
4576
4577static int mmap_free_1 P_ ((struct mmap_region *));
4578static int mmap_enlarge P_ ((struct mmap_region *, int));
4579static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4580static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4581static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4582static void mmap_free P_ ((POINTER_TYPE **ptr));
4583static void mmap_init P_ ((void));
4584
4585
4586/* Return a region overlapping address range START...END, or null if
4587 none. END is not including, i.e. the last byte in the range
4588 is at END - 1. */
4589
4590static struct mmap_region *
4591mmap_find (start, end)
4592 POINTER_TYPE *start, *end;
4593{
4594 struct mmap_region *r;
4595 char *s = (char *) start, *e = (char *) end;
177c0ea7 4596
b86af064
GM
4597 for (r = mmap_regions; r; r = r->next)
4598 {
4599 char *rstart = (char *) r;
4600 char *rend = rstart + r->nbytes_mapped;
4601
4602 if (/* First byte of range, i.e. START, in this region? */
4603 (s >= rstart && s < rend)
4604 /* Last byte of range, i.e. END - 1, in this region? */
4605 || (e > rstart && e <= rend)
4606 /* First byte of this region in the range? */
4607 || (rstart >= s && rstart < e)
4608 /* Last byte of this region in the range? */
4609 || (rend > s && rend <= e))
4610 break;
4611 }
4612
4613 return r;
4614}
4615
4616
4617/* Unmap a region. P is a pointer to the start of the user-araa of
4618 the region. Value is non-zero if successful. */
4619
4620static int
4621mmap_free_1 (r)
4622 struct mmap_region *r;
4623{
4624 if (r->next)
4625 r->next->prev = r->prev;
4626 if (r->prev)
4627 r->prev->next = r->next;
4628 else
4629 mmap_regions = r->next;
177c0ea7 4630
1a15cca0 4631 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
b86af064
GM
4632 {
4633 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4634 return 0;
4635 }
4636
4637 return 1;
4638}
4639
4640
4641/* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4642 Value is non-zero if successful. */
4643
4644static int
4645mmap_enlarge (r, npages)
4646 struct mmap_region *r;
4647 int npages;
4648{
4649 char *region_end = (char *) r + r->nbytes_mapped;
4650 size_t nbytes;
4651 int success = 0;
4652
4653 if (npages < 0)
4654 {
4655 /* Unmap pages at the end of the region. */
4656 nbytes = - npages * mmap_page_size;
4657 if (munmap (region_end - nbytes, nbytes) == -1)
4658 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4659 else
4660 {
4661 r->nbytes_mapped -= nbytes;
4662 success = 1;
4663 }
4664 }
4665 else if (npages > 0)
4666 {
b86af064 4667 nbytes = npages * mmap_page_size;
177c0ea7 4668
b86af064
GM
4669 /* Try to map additional pages at the end of the region. We
4670 cannot do this if the address range is already occupied by
4671 something else because mmap deletes any previous mapping.
4672 I'm not sure this is worth doing, let's see. */
08327b22 4673 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
b86af064
GM
4674 {
4675 POINTER_TYPE *p;
177c0ea7 4676
b86af064
GM
4677 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4678 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4679 if (p == MAP_FAILED)
edaa9aed 4680 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
b86af064
GM
4681 else if (p != (POINTER_TYPE *) region_end)
4682 {
4683 /* Kernels are free to choose a different address. In
4684 that case, unmap what we've mapped above; we have
4685 no use for it. */
4686 if (munmap (p, nbytes) == -1)
4687 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4688 }
4689 else
4690 {
4691 r->nbytes_mapped += nbytes;
4692 success = 1;
4693 }
4694 }
4695 }
4696
4697 return success;
4698}
4699
4700
4701/* Set or reset variables holding references to mapped regions. If
4702 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4703 non-zero, set all variables to the start of the user-areas
4704 of mapped regions.
4705
4706 This function is called from Fdump_emacs to ensure that the dumped
4707 Emacs doesn't contain references to memory that won't be mapped
4708 when Emacs starts. */
4709
4710void
4711mmap_set_vars (restore_p)
4712 int restore_p;
4713{
4714 struct mmap_region *r;
4715
4716 if (restore_p)
4717 {
4718 mmap_regions = mmap_regions_1;
4719 mmap_fd = mmap_fd_1;
4720 for (r = mmap_regions; r; r = r->next)
4721 *r->var = MMAP_USER_AREA (r);
4722 }
4723 else
4724 {
4725 for (r = mmap_regions; r; r = r->next)
4726 *r->var = NULL;
4727 mmap_regions_1 = mmap_regions;
4728 mmap_regions = NULL;
4729 mmap_fd_1 = mmap_fd;
4730 mmap_fd = -1;
4731 }
4732}
4733
4734
4735/* Allocate a block of storage large enough to hold NBYTES bytes of
4736 data. A pointer to the data is returned in *VAR. VAR is thus the
4737 address of some variable which will use the data area.
4738
4739 The allocation of 0 bytes is valid.
4740
4741 If we can't allocate the necessary memory, set *VAR to null, and
4742 return null. */
4743
4744static POINTER_TYPE *
4745mmap_alloc (var, nbytes)
4746 POINTER_TYPE **var;
4747 size_t nbytes;
4748{
4749 void *p;
4750 size_t map;
4751
4752 mmap_init ();
4753
4754 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4755 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4756 mmap_fd, 0);
177c0ea7 4757
b86af064
GM
4758 if (p == MAP_FAILED)
4759 {
4760 if (errno != ENOMEM)
4761 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4762 p = NULL;
4763 }
4764 else
4765 {
4766 struct mmap_region *r = (struct mmap_region *) p;
177c0ea7 4767
b86af064
GM
4768 r->nbytes_specified = nbytes;
4769 r->nbytes_mapped = map;
4770 r->var = var;
4771 r->prev = NULL;
4772 r->next = mmap_regions;
4773 if (r->next)
4774 r->next->prev = r;
4775 mmap_regions = r;
177c0ea7 4776
b86af064
GM
4777 p = MMAP_USER_AREA (p);
4778 }
177c0ea7 4779
b86af064
GM
4780 return *var = p;
4781}
4782
4783
4784/* Given a pointer at address VAR to data allocated with mmap_alloc,
4785 resize it to size NBYTES. Change *VAR to reflect the new block,
4786 and return this value. If more memory cannot be allocated, then
4787 leave *VAR unchanged, and return null. */
4788
4789static POINTER_TYPE *
4790mmap_realloc (var, nbytes)
4791 POINTER_TYPE **var;
4792 size_t nbytes;
4793{
4794 POINTER_TYPE *result;
177c0ea7 4795
b86af064
GM
4796 mmap_init ();
4797
4798 if (*var == NULL)
4799 result = mmap_alloc (var, nbytes);
177c0ea7 4800 else if (nbytes == 0)
b86af064
GM
4801 {
4802 mmap_free (var);
4803 result = mmap_alloc (var, nbytes);
4804 }
4805 else
4806 {
4807 struct mmap_region *r = MMAP_REGION (*var);
4808 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
177c0ea7 4809
b86af064
GM
4810 if (room < nbytes)
4811 {
4812 /* Must enlarge. */
4813 POINTER_TYPE *old_ptr = *var;
4814
4815 /* Try to map additional pages at the end of the region.
4816 If that fails, allocate a new region, copy data
4817 from the old region, then free it. */
4818 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4819 / mmap_page_size)))
4820 {
4821 r->nbytes_specified = nbytes;
4822 *var = result = old_ptr;
4823 }
4824 else if (mmap_alloc (var, nbytes))
4825 {
4826 bcopy (old_ptr, *var, r->nbytes_specified);
4827 mmap_free_1 (MMAP_REGION (old_ptr));
4828 result = *var;
4829 r = MMAP_REGION (result);
4830 r->nbytes_specified = nbytes;
4831 }
4832 else
4833 {
4834 *var = old_ptr;
4835 result = NULL;
4836 }
4837 }
4838 else if (room - nbytes >= mmap_page_size)
4839 {
4840 /* Shrinking by at least a page. Let's give some
6bcdeb8c
KR
4841 memory back to the system.
4842
4843 The extra parens are to make the division happens first,
4844 on positive values, so we know it will round towards
4845 zero. */
bb63c5c9 4846 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
b86af064
GM
4847 result = *var;
4848 r->nbytes_specified = nbytes;
4849 }
4850 else
4851 {
4852 /* Leave it alone. */
4853 result = *var;
4854 r->nbytes_specified = nbytes;
4855 }
4856 }
4857
4858 return result;
4859}
4860
4861
4862/* Free a block of relocatable storage whose data is pointed to by
4863 PTR. Store 0 in *PTR to show there's no block allocated. */
4864
4865static void
4866mmap_free (var)
4867 POINTER_TYPE **var;
4868{
4869 mmap_init ();
177c0ea7 4870
b86af064
GM
4871 if (*var)
4872 {
4873 mmap_free_1 (MMAP_REGION (*var));
4874 *var = NULL;
4875 }
4876}
4877
4878
4879/* Perform necessary intializations for the use of mmap. */
4880
4881static void
4882mmap_init ()
4883{
4884#if MAP_ANON == 0
4885 /* The value of mmap_fd is initially 0 in temacs, and -1
4886 in a dumped Emacs. */
4887 if (mmap_fd <= 0)
4888 {
4889 /* No anonymous mmap -- we need the file descriptor. */
4890 mmap_fd = open ("/dev/zero", O_RDONLY);
4891 if (mmap_fd == -1)
4892 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4893 }
4894#endif /* MAP_ANON == 0 */
4895
4896 if (mmap_initialized_p)
4897 return;
4898 mmap_initialized_p = 1;
177c0ea7 4899
b86af064
GM
4900#if MAP_ANON != 0
4901 mmap_fd = -1;
4902#endif
177c0ea7 4903
b86af064
GM
4904 mmap_page_size = getpagesize ();
4905}
4906
4907#endif /* USE_MMAP_FOR_BUFFERS */
4908
4909
4910\f
4911/***********************************************************************
4912 Buffer-text Allocation
4913 ***********************************************************************/
4914
4915#ifdef REL_ALLOC
4916extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
4917extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
4918extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
4919#endif /* REL_ALLOC */
4920
4921
4922/* Allocate NBYTES bytes for buffer B's text buffer. */
4923
4924static void
4925alloc_buffer_text (b, nbytes)
4926 struct buffer *b;
4927 size_t nbytes;
4928{
4929 POINTER_TYPE *p;
177c0ea7 4930
b86af064
GM
4931 BLOCK_INPUT;
4932#if defined USE_MMAP_FOR_BUFFERS
4933 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4934#elif defined REL_ALLOC
4935 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4936#else
815add84 4937 p = xmalloc (nbytes);
b86af064 4938#endif
177c0ea7 4939
b86af064
GM
4940 if (p == NULL)
4941 {
4942 UNBLOCK_INPUT;
4943 memory_full ();
4944 }
4945
4946 b->text->beg = (unsigned char *) p;
4947 UNBLOCK_INPUT;
4948}
4949
4950/* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
4951 shrink it. */
4952
4953void
4954enlarge_buffer_text (b, delta)
4955 struct buffer *b;
4956 int delta;
4957{
4958 POINTER_TYPE *p;
4959 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
4960 + delta);
4961 BLOCK_INPUT;
4962#if defined USE_MMAP_FOR_BUFFERS
4963 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4964#elif defined REL_ALLOC
4965 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
4966#else
4967 p = xrealloc (b->text->beg, nbytes);
4968#endif
177c0ea7 4969
b86af064
GM
4970 if (p == NULL)
4971 {
4972 UNBLOCK_INPUT;
4973 memory_full ();
4974 }
4975
4976 BUF_BEG_ADDR (b) = (unsigned char *) p;
4977 UNBLOCK_INPUT;
4978}
4979
4980
4981/* Free buffer B's text buffer. */
4982
4983static void
4984free_buffer_text (b)
4985 struct buffer *b;
4986{
4987 BLOCK_INPUT;
4988
4989#if defined USE_MMAP_FOR_BUFFERS
4990 mmap_free ((POINTER_TYPE **) &b->text->beg);
4991#elif defined REL_ALLOC
4992 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
4993#else
4994 xfree (b->text->beg);
4995#endif
177c0ea7 4996
b86af064
GM
4997 BUF_BEG_ADDR (b) = NULL;
4998 UNBLOCK_INPUT;
4999}
5000
5001
5002\f
5003/***********************************************************************
5004 Initialization
5005 ***********************************************************************/
5006
dfcf069d 5007void
1ab256cb
RM
5008init_buffer_once ()
5009{
7c02e886
GM
5010 int idx;
5011
5012 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
13de9290 5013
1ab256cb
RM
5014 /* Make sure all markable slots in buffer_defaults
5015 are initialized reasonably, so mark_buffer won't choke. */
5016 reset_buffer (&buffer_defaults);
13de9290 5017 reset_buffer_local_variables (&buffer_defaults, 1);
1ab256cb 5018 reset_buffer (&buffer_local_symbols);
13de9290 5019 reset_buffer_local_variables (&buffer_local_symbols, 1);
336cd056
RS
5020 /* Prevent GC from getting confused. */
5021 buffer_defaults.text = &buffer_defaults.own_text;
5022 buffer_local_symbols.text = &buffer_local_symbols.own_text;
336cd056
RS
5023 BUF_INTERVALS (&buffer_defaults) = 0;
5024 BUF_INTERVALS (&buffer_local_symbols) = 0;
67180c6a
KH
5025 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5026 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
5027
5028 /* Set up the default values of various buffer slots. */
5029 /* Must do these before making the first buffer! */
5030
f532dca0 5031 /* real setup is done in bindings.el */
1ab256cb 5032 buffer_defaults.mode_line_format = build_string ("%-");
045dee35 5033 buffer_defaults.header_line_format = Qnil;
1ab256cb
RM
5034 buffer_defaults.abbrev_mode = Qnil;
5035 buffer_defaults.overwrite_mode = Qnil;
5036 buffer_defaults.case_fold_search = Qt;
5037 buffer_defaults.auto_fill_function = Qnil;
5038 buffer_defaults.selective_display = Qnil;
5039#ifndef old
5040 buffer_defaults.selective_display_ellipses = Qt;
5041#endif
5042 buffer_defaults.abbrev_table = Qnil;
5043 buffer_defaults.display_table = Qnil;
1ab256cb 5044 buffer_defaults.undo_list = Qnil;
c48f61ef 5045 buffer_defaults.mark_active = Qnil;
be9aafdd 5046 buffer_defaults.file_format = Qnil;
71ed49fa 5047 buffer_defaults.auto_save_file_format = Qt;
2410d73a
SM
5048 buffer_defaults.overlays_before = NULL;
5049 buffer_defaults.overlays_after = NULL;
c2d5b10f 5050 buffer_defaults.overlay_center = BEG;
1ab256cb 5051
8d7a4592 5052 XSETFASTINT (buffer_defaults.tab_width, 8);
1ab256cb
RM
5053 buffer_defaults.truncate_lines = Qnil;
5054 buffer_defaults.ctl_arrow = Qt;
3b06f880 5055 buffer_defaults.direction_reversed = Qnil;
bb2ec976 5056 buffer_defaults.cursor_type = Qt;
a3bbced0 5057 buffer_defaults.extra_line_spacing = Qnil;
187ccf49 5058 buffer_defaults.cursor_in_non_selected_windows = Qt;
1ab256cb 5059
f7975d07 5060#ifdef DOS_NT
0776cb1b 5061 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
54ad07d3 5062#endif
a1a17b61 5063 buffer_defaults.enable_multibyte_characters = Qt;
c71b5d9b 5064 buffer_defaults.buffer_file_coding_system = Qnil;
8d7a4592
KH
5065 XSETFASTINT (buffer_defaults.fill_column, 70);
5066 XSETFASTINT (buffer_defaults.left_margin, 0);
28e969dd 5067 buffer_defaults.cache_long_line_scans = Qnil;
f6ed2e84 5068 buffer_defaults.file_truename = Qnil;
7962a441 5069 XSETFASTINT (buffer_defaults.display_count, 0);
2ad8731a
KS
5070 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5071 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5072 buffer_defaults.left_fringe_width = Qnil;
5073 buffer_defaults.right_fringe_width = Qnil;
5074 buffer_defaults.fringes_outside_margins = Qnil;
5075 buffer_defaults.scroll_bar_width = Qnil;
5076 buffer_defaults.vertical_scroll_bar_type = Qt;
0552666b 5077 buffer_defaults.indicate_empty_lines = Qnil;
6b61353c 5078 buffer_defaults.indicate_buffer_boundaries = Qnil;
c6a46372
KS
5079 buffer_defaults.fringe_indicator_alist = Qnil;
5080 buffer_defaults.fringe_cursor_alist = Qnil;
0552666b
GM
5081 buffer_defaults.scroll_up_aggressively = Qnil;
5082 buffer_defaults.scroll_down_aggressively = Qnil;
3fd364db 5083 buffer_defaults.display_time = Qnil;
1ab256cb
RM
5084
5085 /* Assign the local-flags to the slots that have default values.
5086 The local flag is a bit that is used in the buffer
5087 to say that it has its own local value for the slot.
5088 The local flag bits are in the local_var_flags slot of the buffer. */
5089
5090 /* Nothing can work if this isn't true */
4d2f1389 5091 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
1ab256cb
RM
5092
5093 /* 0 means not a lisp var, -1 means always local, else mask */
5094 bzero (&buffer_local_flags, sizeof buffer_local_flags);
aab80822
KH
5095 XSETINT (buffer_local_flags.filename, -1);
5096 XSETINT (buffer_local_flags.directory, -1);
5097 XSETINT (buffer_local_flags.backed_up, -1);
5098 XSETINT (buffer_local_flags.save_length, -1);
5099 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5100 XSETINT (buffer_local_flags.read_only, -1);
5101 XSETINT (buffer_local_flags.major_mode, -1);
5102 XSETINT (buffer_local_flags.mode_name, -1);
5103 XSETINT (buffer_local_flags.undo_list, -1);
5104 XSETINT (buffer_local_flags.mark_active, -1);
943e065b 5105 XSETINT (buffer_local_flags.point_before_scroll, -1);
f6ed2e84 5106 XSETINT (buffer_local_flags.file_truename, -1);
3cb719bd 5107 XSETINT (buffer_local_flags.invisibility_spec, -1);
55ac8536 5108 XSETINT (buffer_local_flags.file_format, -1);
71ed49fa 5109 XSETINT (buffer_local_flags.auto_save_file_format, -1);
7962a441 5110 XSETINT (buffer_local_flags.display_count, -1);
3fd364db 5111 XSETINT (buffer_local_flags.display_time, -1);
1bf08baf 5112 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
8d7a4592 5113
7c02e886
GM
5114 idx = 1;
5115 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5116 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5117 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5118 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5119 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5120 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
1ab256cb 5121#ifndef old
7c02e886 5122 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
1ab256cb 5123#endif
7c02e886
GM
5124 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5125 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5126 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5127 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5128 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5129 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5130 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
f7975d07 5131#ifdef DOS_NT
7c02e886 5132 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
13de9290 5133 /* Make this one a permanent local. */
7c02e886 5134 buffer_permanent_local_flags[idx++] = 1;
54ad07d3 5135#endif
7c02e886
GM
5136 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5137 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5138 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5139 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
177c0ea7 5140 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
a1a17b61 5141 /* Make this one a permanent local. */
7c02e886 5142 buffer_permanent_local_flags[idx++] = 1;
2ad8731a
KS
5143 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5144 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5145 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5146 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5147 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5148 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5149 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
7c02e886 5150 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
6b61353c 5151 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
c6a46372
KS
5152 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5153 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
7c02e886
GM
5154 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5155 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5156 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
bd96bd79 5157 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
a3bbced0 5158 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
0124c5bd 5159 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
7c02e886
GM
5160
5161 /* Need more room? */
7313acd0 5162 if (idx >= MAX_PER_BUFFER_VARS)
7c02e886 5163 abort ();
7313acd0 5164 last_per_buffer_idx = idx;
177c0ea7 5165
1ab256cb
RM
5166 Vbuffer_alist = Qnil;
5167 current_buffer = 0;
5168 all_buffers = 0;
5169
5170 QSFundamental = build_string ("Fundamental");
5171
5172 Qfundamental_mode = intern ("fundamental-mode");
5173 buffer_defaults.major_mode = Qfundamental_mode;
5174
5175 Qmode_class = intern ("mode-class");
5176
5177 Qprotected_field = intern ("protected-field");
5178
5179 Qpermanent_local = intern ("permanent-local");
5180
5181 Qkill_buffer_hook = intern ("kill-buffer-hook");
fd6cfe11 5182 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
1ab256cb 5183
48265e61
DL
5184 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5185
1ab256cb 5186 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
000f8083 5187
1ab256cb
RM
5188 /* super-magic invisible buffer */
5189 Vbuffer_alist = Qnil;
5190
ffd56f97 5191 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
7775635d
KH
5192
5193 inhibit_modification_hooks = 0;
1ab256cb
RM
5194}
5195
dfcf069d 5196void
1ab256cb
RM
5197init_buffer ()
5198{
2381d133 5199 char *pwd;
136351b7 5200 Lisp_Object temp;
b639c9be 5201 int len;
1ab256cb 5202
b86af064 5203#ifdef USE_MMAP_FOR_BUFFERS
93c27ef1
GM
5204 {
5205 /* When using the ralloc implementation based on mmap(2), buffer
5206 text pointers will have been set to null in the dumped Emacs.
5207 Map new memory. */
5208 struct buffer *b;
177c0ea7 5209
93c27ef1
GM
5210 for (b = all_buffers; b; b = b->next)
5211 if (b->text->beg == NULL)
b86af064 5212 enlarge_buffer_text (b, 0);
93c27ef1 5213 }
b86af064 5214#endif /* USE_MMAP_FOR_BUFFERS */
177c0ea7 5215
1ab256cb 5216 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3d871c85
RS
5217 if (NILP (buffer_defaults.enable_multibyte_characters))
5218 Fset_buffer_multibyte (Qnil);
2381d133 5219
01537133 5220 pwd = get_current_dir_name ();
a17b5ed1 5221
156bdb41 5222 if (!pwd)
a17b5ed1 5223 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
1ab256cb
RM
5224
5225#ifndef VMS
5226 /* Maybe this should really use some standard subroutine
5227 whose definition is filename syntax dependent. */
b639c9be
RF
5228 len = strlen (pwd);
5229 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
f7975d07 5230 {
156bdb41 5231 /* Grow buffer to add directory separator and '\0'. */
b639c9be
RF
5232 pwd = (char *) xrealloc (pwd, len + 2);
5233 pwd[len] = DIRECTORY_SEP;
5234 pwd[len + 1] = '\0';
f7975d07 5235 }
1ab256cb 5236#endif /* not VMS */
0995fa35 5237
01537133 5238 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
dcd74c5f 5239 if (! NILP (buffer_defaults.enable_multibyte_characters))
f9962371 5240 /* At this moment, we still don't know how to decode the
156bdb41 5241 directory name. So, we keep the bytes in multibyte form so
dcd74c5f
KH
5242 that ENCODE_FILE correctly gets the original bytes. */
5243 current_buffer->directory
5244 = string_to_multibyte (current_buffer->directory);
136351b7 5245
0995fa35
RS
5246 /* Add /: to the front of the name
5247 if it would otherwise be treated as magic. */
5248 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
81ab2e07
KH
5249 if (! NILP (temp)
5250 /* If the default dir is just /, TEMP is non-nil
5251 because of the ange-ftp completion handler.
5252 However, it is not necessary to turn / into /:/.
5253 So avoid doing that. */
d5db4077 5254 && strcmp ("/", SDATA (current_buffer->directory)))
0995fa35
RS
5255 current_buffer->directory
5256 = concat2 (build_string ("/:"), current_buffer->directory);
5257
136351b7
RS
5258 temp = get_minibuffer (0);
5259 XBUFFER (temp)->directory = current_buffer->directory;
01537133
EZ
5260
5261 free (pwd);
1ab256cb
RM
5262}
5263
5264/* initialize the buffer routines */
dfcf069d 5265void
1ab256cb
RM
5266syms_of_buffer ()
5267{
9115729e
KH
5268 staticpro (&last_overlay_modification_hooks);
5269 last_overlay_modification_hooks
5270 = Fmake_vector (make_number (10), Qnil);
5271
1ab256cb
RM
5272 staticpro (&Vbuffer_defaults);
5273 staticpro (&Vbuffer_local_symbols);
5274 staticpro (&Qfundamental_mode);
5275 staticpro (&Qmode_class);
5276 staticpro (&QSFundamental);
5277 staticpro (&Vbuffer_alist);
5278 staticpro (&Qprotected_field);
5279 staticpro (&Qpermanent_local);
5280 staticpro (&Qkill_buffer_hook);
22378665 5281 Qoverlayp = intern ("overlayp");
52f8ec73 5282 staticpro (&Qoverlayp);
9d7608b7
KH
5283 Qevaporate = intern ("evaporate");
5284 staticpro (&Qevaporate);
294d215f 5285 Qmodification_hooks = intern ("modification-hooks");
22378665 5286 staticpro (&Qmodification_hooks);
294d215f 5287 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
22378665 5288 staticpro (&Qinsert_in_front_hooks);
294d215f 5289 Qinsert_behind_hooks = intern ("insert-behind-hooks");
22378665 5290 staticpro (&Qinsert_behind_hooks);
5fe0b67e 5291 Qget_file_buffer = intern ("get-file-buffer");
22378665 5292 staticpro (&Qget_file_buffer);
5985d248
KH
5293 Qpriority = intern ("priority");
5294 staticpro (&Qpriority);
5295 Qwindow = intern ("window");
5296 staticpro (&Qwindow);
bbbe9545
KH
5297 Qbefore_string = intern ("before-string");
5298 staticpro (&Qbefore_string);
5299 Qafter_string = intern ("after-string");
5300 staticpro (&Qafter_string);
22378665
RS
5301 Qfirst_change_hook = intern ("first-change-hook");
5302 staticpro (&Qfirst_change_hook);
5303 Qbefore_change_functions = intern ("before-change-functions");
5304 staticpro (&Qbefore_change_functions);
5305 Qafter_change_functions = intern ("after-change-functions");
5306 staticpro (&Qafter_change_functions);
48265e61 5307 staticpro (&Qucs_set_table_for_input);
1ab256cb 5308
5b20caf0
RS
5309 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5310 staticpro (&Qkill_buffer_query_functions);
5311
1ab256cb
RM
5312 Fput (Qprotected_field, Qerror_conditions,
5313 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5314 Fput (Qprotected_field, Qerror_message,
5315 build_string ("Attempt to modify a protected field"));
5316
5317 /* All these use DEFVAR_LISP_NOPRO because the slots in
5318 buffer_defaults will all be marked via Vbuffer_defaults. */
5319
5320 DEFVAR_LISP_NOPRO ("default-mode-line-format",
7ee72033
MB
5321 &buffer_defaults.mode_line_format,
5322 doc: /* Default value of `mode-line-format' for buffers that don't override it.
018ba359 5323This is the same as (default-value 'mode-line-format). */);
1ab256cb 5324
045dee35 5325 DEFVAR_LISP_NOPRO ("default-header-line-format",
7ee72033
MB
5326 &buffer_defaults.header_line_format,
5327 doc: /* Default value of `header-line-format' for buffers that don't override it.
018ba359 5328This is the same as (default-value 'header-line-format). */);
0552666b 5329
7ee72033
MB
5330 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5331 doc: /* Default value of `cursor-type' for buffers that don't override it.
018ba359 5332This is the same as (default-value 'cursor-type). */);
bd96bd79 5333
a3bbced0 5334 DEFVAR_LISP_NOPRO ("default-line-spacing",
7ee72033
MB
5335 &buffer_defaults.extra_line_spacing,
5336 doc: /* Default value of `line-spacing' for buffers that don't override it.
018ba359 5337This is the same as (default-value 'line-spacing). */);
a3bbced0 5338
187ccf49
KS
5339 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5340 &buffer_defaults.cursor_in_non_selected_windows,
5341 doc: /* Default value of `cursor-in-non-selected-windows'.
5342This is the same as (default-value 'cursor-in-non-selected-windows). */);
5343
1ab256cb 5344 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
efc7e75f 5345 &buffer_defaults.abbrev_mode,
7ee72033 5346 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
018ba359 5347This is the same as (default-value 'abbrev-mode). */);
1ab256cb
RM
5348
5349 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
efc7e75f 5350 &buffer_defaults.ctl_arrow,
7ee72033 5351 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
018ba359 5352This is the same as (default-value 'ctl-arrow). */);
1ab256cb 5353
5e2ad10b
JB
5354 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5355 &buffer_defaults.direction_reversed,
5356 doc: /* Default value of `direction-reversed' for buffers that do not override it.
018ba359 5357This is the same as (default-value 'direction-reversed). */);
177c0ea7 5358
5e2ad10b
JB
5359 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5360 &buffer_defaults.enable_multibyte_characters,
5361 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
018ba359 5362This is the same as (default-value 'enable-multibyte-characters). */);
177c0ea7 5363
5e2ad10b
JB
5364 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5365 &buffer_defaults.buffer_file_coding_system,
5366 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
018ba359 5367This is the same as (default-value 'buffer-file-coding-system). */);
177c0ea7 5368
1ab256cb 5369 DEFVAR_LISP_NOPRO ("default-truncate-lines",
efc7e75f
PJ
5370 &buffer_defaults.truncate_lines,
5371 doc: /* Default value of `truncate-lines' for buffers that do not override it.
018ba359 5372This is the same as (default-value 'truncate-lines). */);
1ab256cb
RM
5373
5374 DEFVAR_LISP_NOPRO ("default-fill-column",
efc7e75f
PJ
5375 &buffer_defaults.fill_column,
5376 doc: /* Default value of `fill-column' for buffers that do not override it.
018ba359 5377This is the same as (default-value 'fill-column). */);
1ab256cb
RM
5378
5379 DEFVAR_LISP_NOPRO ("default-left-margin",
efc7e75f
PJ
5380 &buffer_defaults.left_margin,
5381 doc: /* Default value of `left-margin' for buffers that do not override it.
018ba359 5382This is the same as (default-value 'left-margin). */);
1ab256cb
RM
5383
5384 DEFVAR_LISP_NOPRO ("default-tab-width",
bc0ede35 5385 &buffer_defaults.tab_width,
23625946 5386 doc: /* Default value of `tab-width' for buffers that do not override it.
018ba359 5387This is the same as (default-value 'tab-width). */);
1ab256cb
RM
5388
5389 DEFVAR_LISP_NOPRO ("default-case-fold-search",
efc7e75f
PJ
5390 &buffer_defaults.case_fold_search,
5391 doc: /* Default value of `case-fold-search' for buffers that don't override it.
018ba359 5392This is the same as (default-value 'case-fold-search). */);
1ab256cb 5393
f7975d07 5394#ifdef DOS_NT
177c0ea7 5395 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
7ee72033 5396 &buffer_defaults.buffer_file_type,
efc7e75f 5397 doc: /* Default file type for buffers that do not override it.
018ba359
PJ
5398This is the same as (default-value 'buffer-file-type).
5399The file type is nil for text, t for binary. */);
54ad07d3
RS
5400#endif
5401
0552666b 5402 DEFVAR_LISP_NOPRO ("default-left-margin-width",
2ad8731a 5403 &buffer_defaults.left_margin_cols,
efc7e75f 5404 doc: /* Default value of `left-margin-width' for buffers that don't override it.
018ba359 5405This is the same as (default-value 'left-margin-width). */);
0552666b
GM
5406
5407 DEFVAR_LISP_NOPRO ("default-right-margin-width",
2ad8731a
KS
5408 &buffer_defaults.right_margin_cols,
5409 doc: /* Default value of `right-margin-width' for buffers that don't override it.
018ba359 5410This is the same as (default-value 'right-margin-width). */);
177c0ea7 5411
2ad8731a
KS
5412 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5413 &buffer_defaults.left_fringe_width,
5414 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5415This is the same as (default-value 'left-fringe-width). */);
5416
5417 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5418 &buffer_defaults.right_fringe_width,
5419 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5420This is the same as (default-value 'right-fringe-width). */);
5421
5422 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5423 &buffer_defaults.fringes_outside_margins,
5424 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5425This is the same as (default-value 'fringes-outside-margins). */);
5426
5427 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5428 &buffer_defaults.scroll_bar_width,
5429 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5430This is the same as (default-value 'scroll-bar-width). */);
5431
5432 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5433 &buffer_defaults.vertical_scroll_bar_type,
5434 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5435This is the same as (default-value 'vertical-scroll-bar). */);
5436
0552666b 5437 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
efc7e75f
PJ
5438 &buffer_defaults.indicate_empty_lines,
5439 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
018ba359 5440This is the same as (default-value 'indicate-empty-lines). */);
177c0ea7 5441
6b61353c
KH
5442 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5443 &buffer_defaults.indicate_buffer_boundaries,
5444 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5445This is the same as (default-value 'indicate-buffer-boundaries). */);
5446
c6a46372
KS
5447 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5448 &buffer_defaults.fringe_indicator_alist,
5449 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5450This is the same as (default-value 'fringe-indicator-alist'). */);
5451
5452 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5453 &buffer_defaults.fringe_cursor_alist,
5454 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5455This is the same as (default-value 'fringe-cursor-alist'). */);
5456
0552666b 5457 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
efc7e75f 5458 &buffer_defaults.scroll_up_aggressively,
7614d762
RS
5459 doc: /* Default value of `scroll-up-aggressively'.
5460This value applies in buffers that don't have their own local values.
fc961256 5461This is the same as (default-value 'scroll-up-aggressively). */);
177c0ea7 5462
0552666b 5463 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
efc7e75f 5464 &buffer_defaults.scroll_down_aggressively,
7614d762
RS
5465 doc: /* Default value of `scroll-down-aggressively'.
5466This value applies in buffers that don't have their own local values.
fc961256 5467This is the same as (default-value 'scroll-down-aggressively). */);
177c0ea7 5468
045dee35 5469 DEFVAR_PER_BUFFER ("header-line-format",
018ba359 5470 &current_buffer->header_line_format,
7ee72033 5471 Qnil,
7614d762
RS
5472 doc: /* Analogous to `mode-line-format', but controls the header line.
5473The header line appears, optionally, at the top of a window;
5474the mode line appears at the bottom. */);
177c0ea7 5475
1ab256cb 5476 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
efc7e75f
PJ
5477 Qnil,
5478 doc: /* Template for displaying mode line for current buffer.
018ba359
PJ
5479Each buffer has its own value of this variable.
5480Value may be nil, a string, a symbol or a list or cons cell.
5481A value of nil means don't display a mode line.
5482For a symbol, its value is used (but it is ignored if t or nil).
5483 A string appearing directly as the value of a symbol is processed verbatim
5484 in that the %-constructs below are not recognized.
ed463255 5485 Note that unless the symbol is marked as a `risky-local-variable', all
177c0ea7 5486 properties in any strings, as well as all :eval and :propertize forms
ed463255 5487 in the value of that symbol will be ignored.
018ba359 5488For a list of the form `(:eval FORM)', FORM is evaluated and the result
9c3eecf3
RS
5489 is used as a mode line element. Be careful--FORM should not load any files,
5490 because that can cause an infinite recursion.
ed463255
KS
5491For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5492 with the specified properties PROPS applied.
018ba359
PJ
5493For a list whose car is a symbol, the symbol's value is taken,
5494 and if that is non-nil, the cadr of the list is processed recursively.
5495 Otherwise, the caddr of the list (if there is one) is processed.
5496For a list whose car is a string or list, each element is processed
5497 recursively and the results are effectively concatenated.
5498For a list whose car is an integer, the cdr of the list is processed
5499 and padded (if the number is positive) or truncated (if negative)
5500 to the width specified by that number.
5501A string is printed verbatim in the mode line except for %-constructs:
5502 (%-constructs are allowed when the string is the entire mode-line-format
5503 or when it is found in a cons-cell or a list)
5504 %b -- print buffer name. %f -- print visited file name.
5505 %F -- print frame name.
5506 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5507 %& is like %*, but ignore read-only-ness.
5508 % means buffer is read-only and * means it is modified.
5509 For a modified read-only buffer, %* gives % and %+ gives *.
5510 %s -- print process status. %l -- print the current line number.
5511 %c -- print the current column number (this makes editing slower).
5512 To make the column number update correctly in all cases,
5513 `column-number-mode' must be non-nil.
6b61353c
KH
5514 %i -- print the size of the buffer.
5515 %I -- like %i, but use k, M, G, etc., to abbreviate.
018ba359
PJ
5516 %p -- print percent of buffer above top of window, or Top, Bot or All.
5517 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5518 or print Bottom or All.
018ba359 5519 %n -- print Narrow if appropriate.
dafbe726 5520 %t -- visited file is text or binary (if OS supports this distinction).
47419860 5521 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
018ba359 5522 %Z -- like %z, but including the end-of-line format.
dafbe726 5523 %e -- print error message about full memory.
018ba359
PJ
5524 %[ -- print one [ for each recursive editing level. %] similar.
5525 %% -- print %. %- -- print infinitely many dashes.
5526Decimal digits after the % specify field width to which to pad. */);
5527
7ee72033 5528 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
efc7e75f 5529 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
0493af6f
JB
5530A value of nil means use current buffer's major mode,
5531provided it is not marked as "special".
557ca2c1
RS
5532
5533When a mode is used by default, `find-file' switches to it
5534before it reads the contents into the buffer and before
5535it finishes setting up the buffer. Thus, the mode and
5536its hooks should not expect certain variables such as
5537`buffer-read-only' and `buffer-file-coding-system' to be set up. */);
1ab256cb
RM
5538
5539 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
7ee72033 5540 make_number (Lisp_Symbol),
efc7e75f 5541 doc: /* Symbol for current buffer's major mode. */);
1ab256cb
RM
5542
5543 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
c01d0677 5544 Qnil,
efc7e75f 5545 doc: /* Pretty name of current buffer's major mode (a string). */);
1ab256cb 5546
7ee72033 5547 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
efc7e75f 5548 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
1ab256cb
RM
5549
5550 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
7ee72033 5551 Qnil,
efc7e75f 5552 doc: /* *Non-nil if searches and matches should ignore case. */);
1ab256cb
RM
5553
5554 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
7ee72033 5555 make_number (Lisp_Int),
1ac5826d 5556 doc: /* *Column beyond which automatic line-wrapping should happen.
f1ccb329 5557Interactively, you can set the buffer local value using \\[set-fill-column]. */);
1ab256cb
RM
5558
5559 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
7ee72033 5560 make_number (Lisp_Int),
fc961256 5561 doc: /* *Column for the default `indent-line-function' to indent to.
018ba359 5562Linefeed indents to this column in Fundamental mode. */);
1ab256cb
RM
5563
5564 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
7ee72033 5565 make_number (Lisp_Int),
efc7e75f 5566 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
1ab256cb 5567
7ee72033 5568 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
efc7e75f 5569 doc: /* *Non-nil means display control chars with uparrow.
018ba359
PJ
5570A value of nil means use backslash and octal digits.
5571This variable does not apply to characters whose display is specified
5572in the current display table (if there is one). */);
1ab256cb 5573
3b06f880 5574 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
1bf08baf 5575 &current_buffer->enable_multibyte_characters,
a9b9a780 5576 Qnil,
efc7e75f 5577 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
018ba359
PJ
5578Otherwise they are regarded as unibyte. This affects the display,
5579file I/O and the behavior of various editing commands.
5580
5581This variable is buffer-local but you cannot set it directly;
5582use the function `set-buffer-multibyte' to change a buffer's representation.
5583Changing its default value with `setq-default' is supported.
5584See also variable `default-enable-multibyte-characters' and Info node
5585`(elisp)Text Representations'. */);
a9b9a780 5586 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
3b06f880 5587
c71b5d9b 5588 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
7ee72033 5589 &current_buffer->buffer_file_coding_system, Qnil,
efc7e75f 5590 doc: /* Coding system to be used for encoding the buffer contents on saving.
018ba359
PJ
5591This variable applies to saving the buffer, and also to `write-region'
5592and other functions that use `write-region'.
5593It does not apply to sending output to subprocesses, however.
5594
5595If this is nil, the buffer is saved without any code conversion
5596unless some coding system is specified in `file-coding-system-alist'
5597for the buffer file.
5598
31a6cb06
EZ
5599If the text to be saved cannot be encoded as specified by this variable,
5600an alternative encoding is selected by `select-safe-coding-system', which see.
5601
018ba359
PJ
5602The variable `coding-system-for-write', if non-nil, overrides this variable.
5603
5604This variable is never applied to a way of decoding a file while reading it. */);
c71b5d9b 5605
3b06f880 5606 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
7ee72033 5607 Qnil,
efc7e75f 5608 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
3b06f880 5609
7ee72033 5610 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
7614d762
RS
5611 doc: /* *Non-nil means do not display continuation lines.
5612Instead, give each line of text just one screen line.
018ba359
PJ
5613
5614Note that this is overridden by the variable
5615`truncate-partial-width-windows' if that variable is non-nil
5616and this buffer is not full-frame width. */);
1ab256cb 5617
f7975d07 5618#ifdef DOS_NT
54ad07d3 5619 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
7ee72033 5620 Qnil,
efc7e75f 5621 doc: /* Non-nil if the visited file is a binary file.
018ba359
PJ
5622This variable is meaningful on MS-DOG and Windows NT.
5623On those systems, it is automatically local in every buffer.
5624On other systems, this variable is normally always nil. */);
54ad07d3
RS
5625#endif
5626
1ab256cb 5627 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
7ee72033 5628 make_number (Lisp_String),
efc7e75f 5629 doc: /* Name of default directory of current buffer. Should end with slash.
018ba359 5630To interactively change the default directory, use command `cd'. */);
1ab256cb
RM
5631
5632 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
7ee72033 5633 Qnil,
efc7e75f 5634 doc: /* Function called (if non-nil) to perform auto-fill.
018ba359
PJ
5635It is called after self-inserting any character specified in
5636the `auto-fill-chars' table.
5637NOTE: This variable is not a hook;
5638its value may not be a list of functions. */);
1ab256cb
RM
5639
5640 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
7ee72033 5641 make_number (Lisp_String),
efc7e75f 5642 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
1ab256cb 5643
f6ed2e84 5644 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
7ee72033 5645 make_number (Lisp_String),
efc7e75f 5646 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
018ba359
PJ
5647The truename of a file is calculated by `file-truename'
5648and then abbreviated with `abbreviate-file-name'. */);
f6ed2e84 5649
1ab256cb 5650 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 5651 &current_buffer->auto_save_file_name,
7ee72033 5652 make_number (Lisp_String),
7614d762
RS
5653 doc: /* Name of file for auto-saving current buffer.
5654If it is nil, that means don't auto-save this buffer. */);
1ab256cb 5655
7ee72033 5656 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
efc7e75f 5657 doc: /* Non-nil if this buffer is read-only. */);
1ab256cb 5658
7ee72033 5659 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
efc7e75f 5660 doc: /* Non-nil if this buffer's file has been backed up.
018ba359 5661Backing up is done before the first time the file is saved. */);
1ab256cb
RM
5662
5663 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
7ee72033 5664 make_number (Lisp_Int),
efc7e75f 5665 doc: /* Length of current buffer when last read in, saved or auto-saved.
018ba359 56660 initially. */);
1ab256cb
RM
5667
5668 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
7ee72033 5669 Qnil,
7614d762
RS
5670 doc: /* Non-nil enables selective display.
5671An Integer N as value means display only lines
5672that start with less than n columns of space.
5673A value of t means that the character ^M makes itself and
5674all the rest of the line invisible; also, when saving the buffer
5675in a file, save the ^M as a newline. */);
1ab256cb
RM
5676
5677#ifndef old
5678 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5679 &current_buffer->selective_display_ellipses,
7ee72033 5680 Qnil,
3f676284 5681 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
1ab256cb
RM
5682#endif
5683
7ee72033 5684 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
efc7e75f 5685 doc: /* Non-nil if self-insertion should replace existing text.
018ba359
PJ
5686The value should be one of `overwrite-mode-textual',
5687`overwrite-mode-binary', or nil.
5688If it is `overwrite-mode-textual', self-insertion still
5689inserts at the end of a line, and inserts when point is before a tab,
5690until the tab is filled in.
5691If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5692
54939090 5693 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
7ee72033 5694 Qnil,
efc7e75f 5695 doc: /* Display table that controls display of the contents of current buffer.
018ba359
PJ
5696
5697If this variable is nil, the value of `standard-display-table' is used.
5698Each window can have its own, overriding display table, see
5699`set-window-display-table' and `window-display-table'.
5700
5701The display table is a char-table created with `make-display-table'.
5702A char-table is an array indexed by character codes. Normal array
5703primitives `aref' and `aset' can be used to access elements of a char-table.
5704
5705Each of the char-table elements control how to display the corresponding
5706text character: the element at index C in the table says how to display
5707the character whose code is C. Each element should be a vector of
426a9163
JB
5708characters or nil. The value nil means display the character in the
5709default fashion; otherwise, the characters from the vector are delivered
5710to the screen instead of the original character.
018ba359 5711
5fd11dc8 5712For example, (aset buffer-display-table ?X [?Y]) tells Emacs
adbb3b05 5713to display a capital Y instead of each X character.
018ba359
PJ
5714
5715In addition, a char-table has six extra slots to control the display of:
5716
5717 the end of a truncated screen line (extra-slot 0, a single character);
5718 the end of a continued line (extra-slot 1, a single character);
5719 the escape character used to display character codes in octal
5720 (extra-slot 2, a single character);
5721 the character used as an arrow for control characters (extra-slot 3,
5722 a single character);
5723 the decoration indicating the presence of invisible lines (extra-slot 4,
5724 a vector of characters);
5725 the character used to draw the border between side-by-side windows
5726 (extra-slot 5, a single character).
5727
5728See also the functions `display-table-slot' and `set-display-table-slot'. */);
1ab256cb 5729
2ad8731a 5730 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
7ee72033 5731 Qnil,
efc7e75f 5732 doc: /* *Width of left marginal area for display of a buffer.
018ba359 5733A value of nil means no marginal area. */);
177c0ea7 5734
2ad8731a 5735 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
7ee72033 5736 Qnil,
efc7e75f 5737 doc: /* *Width of right marginal area for display of a buffer.
018ba359 5738A value of nil means no marginal area. */);
177c0ea7 5739
2ad8731a
KS
5740 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5741 Qnil,
5742 doc: /* *Width of this buffer's left fringe (in pixels).
5743A value of 0 means no left fringe is shown in this buffer's window.
5744A value of nil means to use the left fringe width from the window's frame. */);
5745
5746 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5747 Qnil,
5748 doc: /* *Width of this buffer's right fringe (in pixels).
5749A value of 0 means no right fringe is shown in this buffer's window.
5750A value of nil means to use the right fringe width from the window's frame. */);
5751
5752 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5753 Qnil,
5754 doc: /* *Non-nil means to display fringes outside display margins.
5755A value of nil means to display fringes between margins and buffer text. */);
5756
5757 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5758 Qnil,
5759 doc: /* *Width of this buffer's scroll bars in pixels.
5760A value of nil means to use the scroll bar width from the window's frame. */);
5761
5762 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5763 Qnil,
5764 doc: /* *Position of this buffer's vertical scroll bar.
7c6b2007 5765The value takes effect whenever you tell a window to display this buffer;
188577ce 5766for instance, with `set-window-buffer' or when `display-buffer' displays it.
7c6b2007 5767
fc2c8887
RS
5768A value of `left' or `right' means put the vertical scroll bar at that side
5769of the window; a value of nil means don't show any vertical scroll bars.
5770A value of t (the default) means do whatever the window's frame specifies. */);
2ad8731a 5771
0552666b 5772 DEFVAR_PER_BUFFER ("indicate-empty-lines",
7ee72033 5773 &current_buffer->indicate_empty_lines, Qnil,
efc7e75f 5774 doc: /* *Visually indicate empty lines after the buffer end.
018ba359
PJ
5775If non-nil, a bitmap is displayed in the left fringe of a window on
5776window-systems. */);
177c0ea7 5777
6b61353c
KH
5778 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5779 &current_buffer->indicate_buffer_boundaries, Qnil,
5780 doc: /* *Visually indicate buffer boundaries and scrolling.
5781If non-nil, the first and last line of the buffer are marked in the fringe
5782of a window on window-systems with angle bitmaps, or if the window can be
5783scrolled, the top and bottom line of the window are marked with up and down
5784arrow bitmaps.
b2229037
KS
5785
5786If value is a symbol `left' or `right', both angle and arrow bitmaps
79e3497d 5787are displayed in the left or right fringe, resp. Any other value
845a78b4 5788that doesn't look like an alist means display the angle bitmaps in
79e3497d 5789the left fringe but no arrows.
b2229037 5790
79e3497d
RS
5791You can exercise more precise control by using an alist as the
5792value. Each alist element (INDICATOR . POSITION) specifies
5793where to show one of the indicators. INDICATOR is one of `top',
b2229037
KS
5794`bottom', `up', `down', or t, which specifies the default position,
5795and POSITION is one of `left', `right', or nil, meaning do not show
5796this indicator.
5797
5798For example, ((top . left) (t . right)) places the top angle bitmap in
5799left fringe, the bottom angle bitmap in right fringe, and both arrow
6b61353c 5800bitmaps in right fringe. To show just the angle bitmaps in the left
b2229037 5801fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6b61353c 5802
c6a46372
KS
5803 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5804 &current_buffer->fringe_indicator_alist, Qnil,
5805 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5806The value is an alist where each element (INDICATOR . BITMAPS)
5807specifies the fringe bitmaps used to display a specific logical
5808fringe indicator.
5809
5810INDICATOR specifies the logical indicator type which is one of the
5811following symbols: `truncation' , `continuation', `overlay-arrow',
5812`top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5813
5814BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5815the actual bitmap shown in the left or right fringe for the logical
5816indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5817right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5818are used only for the `bottom' and `one-line' indicators when the last
63af6055 5819\(only) line in has no final newline. BITMAPS may also be a single
c6a46372
KS
5820symbol which is used in both left and right fringes. */);
5821
5822 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5823 &current_buffer->fringe_cursor_alist, Qnil,
5824 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5825The value is an alist where each element (CURSOR . BITMAP)
5826specifies the fringe bitmaps used to display a specific logical
5827cursor type in the fringe.
5828
5829CURSOR specifies the logical cursor type which is one of the following
5830symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5831one is used to show a hollow cursor on narrow lines display lines
5832where the normal hollow cursor will not fit.
5833
5834BITMAP is the corresponding fringe bitmap shown for the logical
5835cursor type. */);
5836
0552666b 5837 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
7ee72033 5838 &current_buffer->scroll_up_aggressively, Qnil,
4e0692c1
RS
5839 doc: /* How far to scroll windows upward.
5840If you move point off the bottom, the window scrolls automatically.
426a9163 5841This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
5842means scroll to center point. A fraction means scroll to put point
5843that fraction of the window's height from the bottom of the window.
5844When the value is 0.0, point goes at the bottom line, which in the simple
5845case that you moved off with C-f means scrolling just one line. 1.0 means
5846point goes at the top, so that in that simple case, the window
e5f95d5c 5847scrolls by a full window height. Meaningful values are
175e9712 5848between 0.0 and 1.0, inclusive. */);
177c0ea7 5849
0552666b 5850 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
7ee72033 5851 &current_buffer->scroll_down_aggressively, Qnil,
4e0692c1
RS
5852 doc: /* How far to scroll windows downward.
5853If you move point off the top, the window scrolls automatically.
426a9163 5854This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
5855means scroll to center point. A fraction means scroll to put point
5856that fraction of the window's height from the top of the window.
5857When the value is 0.0, point goes at the top line, which in the simple
5858case that you moved off with C-b means scrolling just one line. 1.0 means
5859point goes at the bottom, so that in that simple case, the window
e5f95d5c 5860scrolls by a full window height. Meaningful values are
175e9712 5861between 0.0 and 1.0, inclusive. */);
177c0ea7 5862
1ab256cb
RM
5863/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
5864 "Don't ask.");
5865*/
1ab256cb 5866
7ee72033
MB
5867 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
5868 doc: /* List of functions to call before each text change.
018ba359
PJ
5869Two arguments are passed to each function: the positions of
5870the beginning and end of the range of old text to be changed.
5871\(For an insertion, the beginning and end are at the same place.)
5872No information is given about the length of the text after the change.
5873
5874Buffer changes made while executing the `before-change-functions'
5875don't call any before-change or after-change functions.
5876That's because these variables are temporarily set to nil.
5877As a result, a hook function cannot straightforwardly alter the value of
5878these variables. See the Emacs Lisp manual for a way of
5879accomplishing an equivalent result by using other variables.
5880
5881If an unhandled error happens in running these functions,
5882the variable's value remains nil. That prevents the error
5883from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
5884 Vbefore_change_functions = Qnil;
5885
7ee72033 5886 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
eacdfade 5887 doc: /* List of functions to call after each text change.
018ba359
PJ
5888Three arguments are passed to each function: the positions of
5889the beginning and end of the range of changed text,
5890and the length in bytes of the pre-change text replaced by that range.
5891\(For an insertion, the pre-change length is zero;
5892for a deletion, that length is the number of bytes deleted,
5893and the post-change beginning and end are at the same place.)
5894
5895Buffer changes made while executing the `after-change-functions'
5896don't call any before-change or after-change functions.
5897That's because these variables are temporarily set to nil.
5898As a result, a hook function cannot straightforwardly alter the value of
5899these variables. See the Emacs Lisp manual for a way of
5900accomplishing an equivalent result by using other variables.
5901
5902If an unhandled error happens in running these functions,
5903the variable's value remains nil. That prevents the error
5904from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
5905 Vafter_change_functions = Qnil;
5906
7ee72033 5907 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
efc7e75f 5908 doc: /* A list of functions to call before changing a buffer which is unmodified.
018ba359 5909The functions are run using the `run-hooks' function. */);
dbc4e1c1 5910 Vfirst_change_hook = Qnil;
1ab256cb 5911
7ee72033
MB
5912 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
5913 doc: /* List of undo entries in current buffer.
018ba359
PJ
5914Recent changes come first; older changes follow newer.
5915
5916An entry (BEG . END) represents an insertion which begins at
5917position BEG and ends at position END.
5918
5919An entry (TEXT . POSITION) represents the deletion of the string TEXT
5920from (abs POSITION). If POSITION is positive, point was at the front
5921of the text being deleted; if negative, point was at the end.
5922
5923An entry (t HIGH . LOW) indicates that the buffer previously had
5924\"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
5925of the visited file's modification time, as of that time. If the
5926modification time of the most recent save is different, this entry is
5927obsolete.
5928
5929An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
5930was modified between BEG and END. PROPERTY is the property name,
5931and VALUE is the old value.
5932
7405f386
KS
5933An entry (apply FUN-NAME . ARGS) means undo the change with
5934\(apply FUN-NAME ARGS).
5935
5936An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
5937in the active region. BEG and END is the range affected by this entry
5938and DELTA is the number of bytes added or deleted in that range by
5939this change.
c6c7dc03 5940
018ba359
PJ
5941An entry (MARKER . DISTANCE) indicates that the marker MARKER
5942was adjusted in position by the offset DISTANCE (an integer).
5943
5944An entry of the form POSITION indicates that point was at the buffer
5945location given by the integer. Undoing an entry of this form places
5946point at POSITION.
5947
b4c4f2f4
JB
5948Entries with value `nil' mark undo boundaries. The undo command treats
5949the changes between two undo boundaries as a single step to be undone.
018ba359
PJ
5950
5951If the value of the variable is t, undo information is not recorded. */);
5952
7ee72033
MB
5953 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
5954 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
018ba359 5955
7ee72033
MB
5956 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
5957 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
018ba359
PJ
5958
5959Normally, the line-motion functions work by scanning the buffer for
5629f29b
DK
5960newlines. Columnar operations (like `move-to-column' and
5961`compute-motion') also work by scanning the buffer, summing character
018ba359
PJ
5962widths as they go. This works well for ordinary text, but if the
5963buffer's lines are very long (say, more than 500 characters), these
5964motion functions will take longer to execute. Emacs may also take
5965longer to update the display.
5966
5629f29b 5967If `cache-long-line-scans' is non-nil, these motion functions cache the
018ba359
PJ
5968results of their scans, and consult the cache to avoid rescanning
5969regions of the buffer until the text is modified. The caches are most
5970beneficial when they prevent the most searching---that is, when the
5971buffer contains long lines and large regions of characters with the
5972same, fixed screen width.
5973
5629f29b 5974When `cache-long-line-scans' is non-nil, processing short lines will
018ba359
PJ
5975become slightly slower (because of the overhead of consulting the
5976cache), and the caches will use memory roughly proportional to the
5977number of newlines and characters whose screen width varies.
5978
5979The caches require no explicit maintenance; their accuracy is
5980maintained internally by the Emacs primitives. Enabling or disabling
5981the cache should not affect the behavior of any of the motion
5982functions; it should only affect their performance. */);
5983
7ee72033
MB
5984 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
5985 doc: /* Value of point before the last series of scroll operations, or nil. */);
018ba359 5986
7ee72033
MB
5987 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
5988 doc: /* List of formats to use when saving this buffer.
018ba359 5989Formats are defined by `format-alist'. This variable is
a9b9a780 5990set when a file is visited. */);
be9aafdd 5991
71ed49fa
LT
5992 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
5993 &current_buffer->auto_save_file_format, Qnil,
5994 doc: /* *Format in which to write auto-save files.
5995Should be a list of symbols naming formats that are defined in `format-alist'.
5996If it is t, which is the default, auto-save files are written in the
5997same format as a regular save would use. */);
5998
3cb719bd 5999 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
7ee72033
MB
6000 &current_buffer->invisibility_spec, Qnil,
6001 doc: /* Invisibility spec of this buffer.
018ba359
PJ
6002The default is t, which means that text is invisible
6003if it has a non-nil `invisible' property.
6004If the value is a list, a text character is invisible if its `invisible'
b49dd850 6005property is an element in that list (or is a list with members in common).
018ba359
PJ
6006If an element is a cons cell of the form (PROP . ELLIPSIS),
6007then characters with property value PROP are invisible,
6008and they have an ellipsis as well if ELLIPSIS is non-nil. */);
3cb719bd 6009
7962a441 6010 DEFVAR_PER_BUFFER ("buffer-display-count",
7ee72033
MB
6011 &current_buffer->display_count, Qnil,
6012 doc: /* A number incremented each time this buffer is displayed in a window.
018ba359 6013The function `set-window-buffer' increments it. */);
3fd364db
RS
6014
6015 DEFVAR_PER_BUFFER ("buffer-display-time",
7ee72033
MB
6016 &current_buffer->display_time, Qnil,
6017 doc: /* Time stamp updated each time this buffer is displayed in a window.
018ba359
PJ
6018The function `set-window-buffer' updates this variable
6019to the value obtained by calling `current-time'.
6020If the buffer has never been shown in a window, the value is nil. */);
6021
7ee72033
MB
6022 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6023 doc: /* *Non-nil means deactivate the mark when the buffer contents change.
018ba359
PJ
6024Non-nil also enables highlighting of the region whenever the mark is active.
6025The variable `highlight-nonselected-windows' controls whether to highlight
5a39b770
RS
6026all windows or just the selected window.
6027
6028If the value is `lambda', that enables Transient Mark mode temporarily
6029until the next buffer modification. If a command sets the value to `only',
b3264fd2
RS
6030that enables Transient Mark mode for the following command only.
6031During that following command, the value of `transient-mark-mode'
6032is `identity'. If it is still `identity' at the end of that command,
6033it changes to nil. */);
c48f61ef
RS
6034 Vtransient_mark_mode = Qnil;
6035
7ee72033
MB
6036 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6037 doc: /* *Non-nil means disregard read-only status of buffers or characters.
018ba359
PJ
6038If the value is t, disregard `buffer-read-only' and all `read-only'
6039text properties. If the value is a list, disregard `buffer-read-only'
6040and disregard a `read-only' text property if the property value
6041is a member of the list. */);
a96b68f1
RS
6042 Vinhibit_read_only = Qnil;
6043
7ee72033 6044 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
f6e22881 6045 doc: /* Cursor to use when this buffer is in the selected window.
018ba359
PJ
6046Values are interpreted as follows:
6047
b8dc613f
JB
6048 t use the cursor specified for the frame
6049 nil don't display a cursor
6050 box display a filled box cursor
6051 hollow display a hollow box cursor
6052 bar display a vertical bar cursor with default width
6053 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6054 hbar display a horizontal bar cursor with default height
b4234f4c 6055 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
b8dc613f 6056 ANYTHING ELSE display a hollow box cursor
cd8d5236
RS
6057
6058When the buffer is displayed in a nonselected window,
6059this variable has no effect; the cursor appears as a hollow box. */);
bb2ec976 6060
a3bbced0 6061 DEFVAR_PER_BUFFER ("line-spacing",
7ee72033
MB
6062 &current_buffer->extra_line_spacing, Qnil,
6063 doc: /* Additional space to put between lines when displaying a buffer.
60ebfdf3
KS
6064The space is measured in pixels, and put below lines on window systems.
6065If value is a floating point number, it specifies the spacing relative
fc961256 6066to the default frame line height. A value of nil means add no extra space. */);
a3bbced0 6067
0124c5bd 6068 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
187ccf49 6069 &current_buffer->cursor_in_non_selected_windows, Qnil,
f6e22881 6070 doc: /* *Cursor type to display in non-selected windows.
fc961256 6071The value t means to use hollow box cursor. See `cursor-type' for other values. */);
0124c5bd 6072
7ee72033 6073 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
f6e22881
JB
6074 doc: /* List of functions called with no args to query before killing a buffer.
6075The buffer being killed will be current while the functions are running.
6076If any of them returns nil, the buffer is not killed. */);
dcdffbf6
RS
6077 Vkill_buffer_query_functions = Qnil;
6078
43ed3b8d
CY
6079 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6080 doc: /* Normal hook run before changing the major mode of a buffer.
6081The function `kill-all-local-variables' runs this before doing anything else. */);
6082 Vchange_major_mode_hook = Qnil;
6083 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6084 staticpro (&Qchange_major_mode_hook);
6085
0dc88e60 6086 defsubr (&Sbuffer_live_p);
1ab256cb
RM
6087 defsubr (&Sbuffer_list);
6088 defsubr (&Sget_buffer);
6089 defsubr (&Sget_file_buffer);
6090 defsubr (&Sget_buffer_create);
336cd056 6091 defsubr (&Smake_indirect_buffer);
01050cb5 6092 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
6093 defsubr (&Sbuffer_name);
6094/*defsubr (&Sbuffer_number);*/
6095 defsubr (&Sbuffer_file_name);
336cd056 6096 defsubr (&Sbuffer_base_buffer);
79aa712d 6097 defsubr (&Sbuffer_local_value);
1ab256cb
RM
6098 defsubr (&Sbuffer_local_variables);
6099 defsubr (&Sbuffer_modified_p);
6100 defsubr (&Sset_buffer_modified_p);
6101 defsubr (&Sbuffer_modified_tick);
3e145152 6102 defsubr (&Sbuffer_chars_modified_tick);
1ab256cb
RM
6103 defsubr (&Srename_buffer);
6104 defsubr (&Sother_buffer);
1ab256cb
RM
6105 defsubr (&Sbuffer_enable_undo);
6106 defsubr (&Skill_buffer);
a9ee7a59 6107 defsubr (&Sset_buffer_major_mode);
1ab256cb
RM
6108 defsubr (&Sswitch_to_buffer);
6109 defsubr (&Spop_to_buffer);
6110 defsubr (&Scurrent_buffer);
6111 defsubr (&Sset_buffer);
6112 defsubr (&Sbarf_if_buffer_read_only);
6113 defsubr (&Sbury_buffer);
3ac81adb
RS
6114 defsubr (&Serase_buffer);
6115 defsubr (&Sset_buffer_multibyte);
1ab256cb 6116 defsubr (&Skill_all_local_variables);
2eec3b4e 6117
52f8ec73 6118 defsubr (&Soverlayp);
2eec3b4e
RS
6119 defsubr (&Smake_overlay);
6120 defsubr (&Sdelete_overlay);
6121 defsubr (&Smove_overlay);
8ebafa8d
JB
6122 defsubr (&Soverlay_start);
6123 defsubr (&Soverlay_end);
6124 defsubr (&Soverlay_buffer);
6125 defsubr (&Soverlay_properties);
2eec3b4e 6126 defsubr (&Soverlays_at);
74514898 6127 defsubr (&Soverlays_in);
2eec3b4e 6128 defsubr (&Snext_overlay_change);
239c932b 6129 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
6130 defsubr (&Soverlay_recenter);
6131 defsubr (&Soverlay_lists);
6132 defsubr (&Soverlay_get);
6133 defsubr (&Soverlay_put);
a8c21b48 6134 defsubr (&Srestore_buffer_modified_p);
1ab256cb
RM
6135}
6136
dfcf069d 6137void
1ab256cb
RM
6138keys_of_buffer ()
6139{
6140 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6141 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
6142
6143 /* This must not be in syms_of_buffer, because Qdisabled is not
6144 initialized when that function gets called. */
6145 Fput (intern ("erase-buffer"), Qdisabled, Qt);
1ab256cb 6146}
6b61353c
KH
6147
6148/* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6149 (do not change this comment) */