Avoid DECLARE_INTERVALS.
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
ac869cf7 2 Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999, 2000
c922bc55 3 Free Software Foundation, Inc.
1ab256cb
RM
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
c922bc55 9the Free Software Foundation; either version 2, or (at your option)
1ab256cb
RM
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
1ab256cb 21
68c45bf0 22#include <config.h>
1ab256cb 23
2381d133
JB
24#include <sys/types.h>
25#include <sys/stat.h>
1ab256cb 26#include <sys/param.h>
9dde47f5
RS
27#include <errno.h>
28
29extern int errno;
1ab256cb
RM
30
31#ifndef MAXPATHLEN
32/* in 4.1, param.h fails to define this. */
33#define MAXPATHLEN 1024
34#endif /* not MAXPATHLEN */
35
dfcf069d
AS
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
1ab256cb 39#include "lisp.h"
21cf4cf8 40#include "intervals.h"
1ab256cb
RM
41#include "window.h"
42#include "commands.h"
43#include "buffer.h"
3b06f880 44#include "charset.h"
28e969dd 45#include "region-cache.h"
1ab256cb 46#include "indent.h"
d014bf88 47#include "blockinput.h"
08460cd4 48#include "frame.h"
1ab256cb
RM
49
50struct buffer *current_buffer; /* the current buffer */
51
52/* First buffer in chain of all buffers (in reverse order of creation).
53 Threaded through ->next. */
54
55struct buffer *all_buffers;
56
57/* This structure holds the default values of the buffer-local variables
58 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
59 The default value occupies the same slot in this structure
60 as an individual buffer's value occupies in that buffer.
61 Setting the default value also goes through the alist of buffers
62 and stores into each buffer that does not say it has a local value. */
63
64struct buffer buffer_defaults;
65
66/* A Lisp_Object pointer to the above, used for staticpro */
67
68static Lisp_Object Vbuffer_defaults;
69
70/* This structure marks which slots in a buffer have corresponding
71 default values in buffer_defaults.
72 Each such slot has a nonzero value in this structure.
73 The value has only one nonzero bit.
74
75 When a buffer has its own local value for a slot,
7c02e886
GM
76 the entry for that slot (found in the same slot in this structure)
77 is turned on in the buffer's local_flags array.
1ab256cb
RM
78
79 If a slot in this structure is -1, then even though there may
80 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
81 and the corresponding slot in buffer_defaults is not used.
82
83 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
84 but there is a default value which is copied into each buffer.
85
86 If a slot in this structure is negative, then even though there may
87 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
88 and the corresponding slot in buffer_defaults is not used.
89
90 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91 zero, that is a bug */
92
93struct buffer buffer_local_flags;
94
95/* This structure holds the names of symbols whose values may be
96 buffer-local. It is indexed and accessed in the same way as the above. */
97
98struct buffer buffer_local_symbols;
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;
147
dbc4e1c1
JB
148/* List of functions to call before changing an unmodified buffer. */
149Lisp_Object Vfirst_change_hook;
22378665 150
dbc4e1c1 151Lisp_Object Qfirst_change_hook;
22378665
RS
152Lisp_Object Qbefore_change_functions;
153Lisp_Object Qafter_change_functions;
1ab256cb 154
7775635d
KH
155/* If nonzero, all modification hooks are suppressed. */
156int inhibit_modification_hooks;
157
1ab256cb
RM
158Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
159
160Lisp_Object Qprotected_field;
161
162Lisp_Object QSFundamental; /* A string "Fundamental" */
163
164Lisp_Object Qkill_buffer_hook;
165
5fe0b67e
RS
166Lisp_Object Qget_file_buffer;
167
52f8ec73
JB
168Lisp_Object Qoverlayp;
169
bbbe9545 170Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
5985d248 171
294d215f
RS
172Lisp_Object Qmodification_hooks;
173Lisp_Object Qinsert_in_front_hooks;
174Lisp_Object Qinsert_behind_hooks;
175
1ab256cb
RM
176/* For debugging; temporary. See set_buffer_internal. */
177/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
178
01136e9b 179void
1ab256cb
RM
180nsberror (spec)
181 Lisp_Object spec;
182{
a7a60ce9 183 if (STRINGP (spec))
1ab256cb
RM
184 error ("No buffer named %s", XSTRING (spec)->data);
185 error ("Invalid buffer argument");
186}
187\f
0dc88e60
RS
188DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
189 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
190Value is nil if OBJECT is not a buffer or if it has been killed.")
191 (object)
192 Lisp_Object object;
193{
194 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
195 ? Qt : Qnil);
196}
197
08460cd4
RS
198DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
199 "Return a list of all existing live buffers.\n\
200If the optional arg FRAME is a frame, we return that frame's buffer list.")
201 (frame)
202 Lisp_Object frame;
1ab256cb 203{
08460cd4
RS
204 Lisp_Object framelist, general;
205 general = Fmapcar (Qcdr, Vbuffer_alist);
206
207 if (FRAMEP (frame))
208 {
209 Lisp_Object tail;
210
211 CHECK_FRAME (frame, 1);
212
213 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
214
215 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
216 tail = framelist;
217 while (! NILP (tail))
218 {
7539e11f
KR
219 general = Fdelq (XCAR (tail), general);
220 tail = XCDR (tail);
08460cd4
RS
221 }
222 return nconc2 (framelist, general);
223 }
224
225 return general;
1ab256cb
RM
226}
227
04ae1b48
RS
228/* Like Fassoc, but use Fstring_equal to compare
229 (which ignores text properties),
230 and don't ever QUIT. */
231
232static Lisp_Object
233assoc_ignore_text_properties (key, list)
234 register Lisp_Object key;
235 Lisp_Object list;
236{
237 register Lisp_Object tail;
238 for (tail = list; !NILP (tail); tail = Fcdr (tail))
239 {
240 register Lisp_Object elt, tem;
241 elt = Fcar (tail);
242 tem = Fstring_equal (Fcar (elt), key);
243 if (!NILP (tem))
244 return elt;
245 }
246 return Qnil;
247}
248
1ab256cb
RM
249DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
250 "Return the buffer named NAME (a string).\n\
251If there is no live buffer named NAME, return nil.\n\
252NAME may also be a buffer; if so, the value is that buffer.")
253 (name)
254 register Lisp_Object name;
255{
a7a60ce9 256 if (BUFFERP (name))
1ab256cb
RM
257 return name;
258 CHECK_STRING (name, 0);
259
04ae1b48 260 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
1ab256cb
RM
261}
262
263DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
264 "Return the buffer visiting file FILENAME (a string).\n\
92194d02 265The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
11da5363
RS
266If there is no such live buffer, return nil.\n\
267See also `find-buffer-visiting'.")
1ab256cb
RM
268 (filename)
269 register Lisp_Object filename;
270{
271 register Lisp_Object tail, buf, tem;
5fe0b67e
RS
272 Lisp_Object handler;
273
1ab256cb
RM
274 CHECK_STRING (filename, 0);
275 filename = Fexpand_file_name (filename, Qnil);
276
5fe0b67e
RS
277 /* If the file name has special constructs in it,
278 call the corresponding file handler. */
a617e913 279 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
5fe0b67e
RS
280 if (!NILP (handler))
281 return call2 (handler, Qget_file_buffer, filename);
282
7539e11f 283 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
1ab256cb 284 {
7539e11f 285 buf = Fcdr (XCAR (tail));
a7a60ce9
KH
286 if (!BUFFERP (buf)) continue;
287 if (!STRINGP (XBUFFER (buf)->filename)) continue;
1ab256cb 288 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 289 if (!NILP (tem))
1ab256cb
RM
290 return buf;
291 }
292 return Qnil;
293}
294
52e01189
RS
295Lisp_Object
296get_truename_buffer (filename)
297 register Lisp_Object filename;
298{
299 register Lisp_Object tail, buf, tem;
300
7539e11f 301 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
52e01189 302 {
7539e11f 303 buf = Fcdr (XCAR (tail));
52e01189
RS
304 if (!BUFFERP (buf)) continue;
305 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
306 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
307 if (!NILP (tem))
308 return buf;
309 }
310 return Qnil;
311}
312
1ab256cb
RM
313/* Incremented for each buffer created, to assign the buffer number. */
314int buffer_count;
315
316DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
317 "Return the buffer named NAME, or create such a buffer and return it.\n\
318A new buffer is created if there is no live buffer named NAME.\n\
b44895bc 319If NAME starts with a space, the new buffer does not keep undo information.\n\
1ab256cb
RM
320If NAME is a buffer instead of a string, then it is the value returned.\n\
321The value is never nil.")
322 (name)
323 register Lisp_Object name;
324{
a9ee7a59 325 register Lisp_Object buf;
1ab256cb
RM
326 register struct buffer *b;
327
328 buf = Fget_buffer (name);
265a9e55 329 if (!NILP (buf))
1ab256cb
RM
330 return buf;
331
31cd83e9
KH
332 if (XSTRING (name)->size == 0)
333 error ("Empty string for buffer name is not allowed");
334
a21260d9 335 b = (struct buffer *) allocate_buffer ();
1ab256cb 336
336cd056
RS
337 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
338
339 /* An ordinary buffer uses its own struct buffer_text. */
340 b->text = &b->own_text;
341 b->base_buffer = 0;
342
1ab256cb 343 BUF_GAP_SIZE (b) = 20;
9ac0d9e0 344 BLOCK_INPUT;
3b06f880
KH
345 /* We allocate extra 1-byte at the tail and keep it always '\0' for
346 anchoring a search. */
347 BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
9ac0d9e0 348 UNBLOCK_INPUT;
1ab256cb 349 if (! BUF_BEG_ADDR (b))
81841847 350 buffer_memory_full ();
1ab256cb
RM
351
352 BUF_PT (b) = 1;
353 BUF_GPT (b) = 1;
354 BUF_BEGV (b) = 1;
355 BUF_ZV (b) = 1;
356 BUF_Z (b) = 1;
3f236a40
RS
357 BUF_PT_BYTE (b) = 1;
358 BUF_GPT_BYTE (b) = 1;
359 BUF_BEGV_BYTE (b) = 1;
360 BUF_ZV_BYTE (b) = 1;
361 BUF_Z_BYTE (b) = 1;
1ab256cb 362 BUF_MODIFF (b) = 1;
2509d356 363 BUF_OVERLAY_MODIFF (b) = 1;
336cd056
RS
364 BUF_SAVE_MODIFF (b) = 1;
365 BUF_INTERVALS (b) = 0;
b5a225b4
GM
366 BUF_UNCHANGED_MODIFIED (b) = 1;
367 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
368 BUF_END_UNCHANGED (b) = 0;
369 BUF_BEG_UNCHANGED (b) = 0;
3b06f880 370 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
1ab256cb 371
28e969dd
JB
372 b->newline_cache = 0;
373 b->width_run_cache = 0;
374 b->width_table = Qnil;
b5a225b4 375 b->prevent_redisplay_optimizations_p = 1;
28e969dd 376
1ab256cb
RM
377 /* Put this on the chain of all buffers including killed ones. */
378 b->next = all_buffers;
379 all_buffers = b;
380
336cd056
RS
381 /* An ordinary buffer normally doesn't need markers
382 to handle BEGV and ZV. */
383 b->pt_marker = Qnil;
384 b->begv_marker = Qnil;
385 b->zv_marker = Qnil;
04ae1b48
RS
386
387 name = Fcopy_sequence (name);
388 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
1ab256cb 389 b->name = name;
04ae1b48 390
1ab256cb
RM
391 if (XSTRING (name)->data[0] != ' ')
392 b->undo_list = Qnil;
393 else
394 b->undo_list = Qt;
395
396 reset_buffer (b);
13de9290 397 reset_buffer_local_variables (b, 1);
1ab256cb
RM
398
399 /* Put this in the alist of all live buffers. */
67180c6a 400 XSETBUFFER (buf, b);
1ab256cb
RM
401 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
402
403 b->mark = Fmake_marker ();
336cd056
RS
404 BUF_MARKERS (b) = Qnil;
405 b->name = name;
406 return buf;
407}
408
7e9d5818
GM
409
410/* Clone per-buffer values of buffer FROM.
411
412 Buffer TO gets the same per-buffer values as FROM, with the
413 following exceptions: (1) TO's name is left untouched, (2) markers
414 are copied and made to refer to TO, and (3) overlay lists are
415 copied. */
416
417static void
418clone_per_buffer_values (from, to)
419 struct buffer *from, *to;
420{
421 Lisp_Object to_buffer;
422 int offset;
423
424 XSETBUFFER (to_buffer, to);
425
426 for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
427 offset < sizeof *to;
428 offset += sizeof (Lisp_Object))
429 {
430 Lisp_Object obj;
431
432 obj = PER_BUFFER_VALUE (from, offset);
433 if (MARKERP (obj))
434 {
435 struct Lisp_Marker *m = XMARKER (obj);
436 obj = Fmake_marker ();
437 XMARKER (obj)->insertion_type = m->insertion_type;
438 set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
439 }
440
441 PER_BUFFER_VALUE (to, offset) = obj;
442 }
443
444 to->overlays_after = Fcopy_sequence (from->overlays_after);
445 to->overlays_before = Fcopy_sequence (to->overlays_before);
446 bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
447}
448
449
450DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
451 2, 3,
193c3837 452 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
a2428fa2
EN
453 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
454BASE-BUFFER should be an existing buffer (or buffer name).\n\
7e9d5818
GM
455NAME should be a string which is not the name of an existing buffer.\n\
456Optional argument CLONE non-nil means preserve BASE-BUFFER's state,\n\
f532dca0 457such as major and minor modes, in the indirect buffer.\n\
7e9d5818
GM
458CLONE nil means the indirect buffer's state is reset to default values.")
459 (base_buffer, name, clone)
460 Lisp_Object base_buffer, name, clone;
336cd056 461{
7e9d5818
GM
462 Lisp_Object buf;
463 struct buffer *b;
336cd056
RS
464
465 buf = Fget_buffer (name);
466 if (!NILP (buf))
467 error ("Buffer name `%s' is in use", XSTRING (name)->data);
468
469 base_buffer = Fget_buffer (base_buffer);
470 if (NILP (base_buffer))
471 error ("No such buffer: `%s'",
472 XSTRING (XBUFFER (base_buffer)->name)->data);
473
474 if (XSTRING (name)->size == 0)
475 error ("Empty string for buffer name is not allowed");
476
a21260d9 477 b = (struct buffer *) allocate_buffer ();
336cd056
RS
478 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
479
480 if (XBUFFER (base_buffer)->base_buffer)
481 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
482 else
483 b->base_buffer = XBUFFER (base_buffer);
484
485 /* Use the base buffer's text object. */
486 b->text = b->base_buffer->text;
487
488 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
489 BUF_ZV (b) = BUF_ZV (b->base_buffer);
490 BUF_PT (b) = BUF_PT (b->base_buffer);
3f236a40
RS
491 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
492 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
493 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
336cd056
RS
494
495 b->newline_cache = 0;
496 b->width_run_cache = 0;
497 b->width_table = Qnil;
498
499 /* Put this on the chain of all buffers including killed ones. */
500 b->next = all_buffers;
501 all_buffers = b;
502
503 name = Fcopy_sequence (name);
504 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
505 b->name = name;
506
507 reset_buffer (b);
13de9290 508 reset_buffer_local_variables (b, 1);
336cd056
RS
509
510 /* Put this in the alist of all live buffers. */
511 XSETBUFFER (buf, b);
512 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
513
514 b->mark = Fmake_marker ();
1ab256cb 515 b->name = name;
336cd056 516
abc9d959
RS
517 /* The multibyte status belongs to the base buffer. */
518 b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
519
336cd056
RS
520 /* Make sure the base buffer has markers for its narrowing. */
521 if (NILP (b->base_buffer->pt_marker))
522 {
523 b->base_buffer->pt_marker = Fmake_marker ();
3f236a40
RS
524 set_marker_both (b->base_buffer->pt_marker, base_buffer,
525 BUF_PT (b->base_buffer),
526 BUF_PT_BYTE (b->base_buffer));
336cd056
RS
527 }
528 if (NILP (b->base_buffer->begv_marker))
529 {
530 b->base_buffer->begv_marker = Fmake_marker ();
3f236a40
RS
531 set_marker_both (b->base_buffer->begv_marker, base_buffer,
532 BUF_BEGV (b->base_buffer),
533 BUF_BEGV_BYTE (b->base_buffer));
336cd056
RS
534 }
535 if (NILP (b->base_buffer->zv_marker))
536 {
537 b->base_buffer->zv_marker = Fmake_marker ();
3f236a40
RS
538 set_marker_both (b->base_buffer->zv_marker, base_buffer,
539 BUF_ZV (b->base_buffer),
540 BUF_ZV_BYTE (b->base_buffer));
26d84681 541 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
336cd056
RS
542 }
543
7e9d5818
GM
544 if (NILP (clone))
545 {
546 /* Give the indirect buffer markers for its narrowing. */
547 b->pt_marker = Fmake_marker ();
548 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
549 b->begv_marker = Fmake_marker ();
550 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
551 b->zv_marker = Fmake_marker ();
552 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
553 XMARKER (b->zv_marker)->insertion_type = 1;
554 }
555 else
556 clone_per_buffer_values (b->base_buffer, b);
336cd056 557
a9ee7a59 558 return buf;
1ab256cb
RM
559}
560
bcd40520
RS
561/* Reinitialize everything about a buffer except its name and contents
562 and local variables. */
1ab256cb
RM
563
564void
565reset_buffer (b)
566 register struct buffer *b;
567{
568 b->filename = Qnil;
f6ed2e84 569 b->file_truename = Qnil;
1ab256cb
RM
570 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
571 b->modtime = 0;
8d7a4592 572 XSETFASTINT (b->save_length, 0);
1ab256cb 573 b->last_window_start = 1;
8b264726 574 /* It is more conservative to start out "changed" than "unchanged". */
b5a225b4
GM
575 b->clip_changed = 0;
576 b->prevent_redisplay_optimizations_p = 1;
1ab256cb
RM
577 b->backed_up = Qnil;
578 b->auto_save_modified = 0;
84f6bcba 579 b->auto_save_failure_time = -1;
1ab256cb
RM
580 b->auto_save_file_name = Qnil;
581 b->read_only = Qnil;
2eec3b4e
RS
582 b->overlays_before = Qnil;
583 b->overlays_after = Qnil;
8d7a4592 584 XSETFASTINT (b->overlay_center, 1);
dfda7a7f 585 b->mark_active = Qnil;
943e065b 586 b->point_before_scroll = Qnil;
be9aafdd 587 b->file_format = Qnil;
0dc6f165 588 b->last_selected_window = Qnil;
7962a441 589 XSETINT (b->display_count, 0);
3fd364db 590 b->display_time = Qnil;
1bf08baf 591 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
bb2ec976 592 b->cursor_type = buffer_defaults.cursor_type;
a3bbced0 593 b->extra_line_spacing = buffer_defaults.extra_line_spacing;
1ab256cb
RM
594}
595
bcd40520
RS
596/* Reset buffer B's local variables info.
597 Don't use this on a buffer that has already been in use;
598 it does not treat permanent locals consistently.
13de9290
RS
599 Instead, use Fkill_all_local_variables.
600
601 If PERMANENT_TOO is 1, then we reset permanent built-in
602 buffer-local variables. If PERMANENT_TOO is 0,
603 we preserve those. */
bcd40520 604
13de9290
RS
605static void
606reset_buffer_local_variables (b, permanent_too)
1ab256cb 607 register struct buffer *b;
13de9290 608 int permanent_too;
1ab256cb
RM
609{
610 register int offset;
7c02e886 611 int i;
1ab256cb
RM
612
613 /* Reset the major mode to Fundamental, together with all the
614 things that depend on the major mode.
615 default-major-mode is handled at a higher level.
616 We ignore it here. */
617 b->major_mode = Qfundamental_mode;
618 b->keymap = Qnil;
619 b->abbrev_table = Vfundamental_mode_abbrev_table;
620 b->mode_name = QSFundamental;
621 b->minor_modes = Qnil;
3446af9c
RS
622
623 /* If the standard case table has been altered and invalidated,
624 fix up its insides first. */
625 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
626 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
627 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
628 Fset_standard_case_table (Vascii_downcase_table);
629
1ab256cb 630 b->downcase_table = Vascii_downcase_table;
1e9b6335
RS
631 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
632 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
633 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
3cb719bd 634 b->invisibility_spec = Qt;
2e716096
RS
635#ifndef DOS_NT
636 b->buffer_file_type = Qnil;
637#endif
3cb719bd 638
1ab256cb
RM
639#if 0
640 b->sort_table = XSTRING (Vascii_sort_table);
641 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
642#endif /* 0 */
643
13de9290 644 /* Reset all (or most) per-buffer variables to their defaults. */
1ab256cb 645 b->local_var_alist = Qnil;
7313acd0 646 for (i = 0; i < last_per_buffer_idx; ++i)
7c02e886 647 if (permanent_too || buffer_permanent_local_flags[i] == 0)
7313acd0 648 SET_PER_BUFFER_VALUE_P (b, i, 0);
1ab256cb
RM
649
650 /* For each slot that has a default value,
651 copy that into the slot. */
652
7313acd0 653 for (offset = PER_BUFFER_VAR_OFFSET (name);
7c02e886
GM
654 offset < sizeof *b;
655 offset += sizeof (Lisp_Object))
aab80822 656 {
7313acd0 657 int idx = PER_BUFFER_IDX (offset);
7c02e886
GM
658 if ((idx > 0
659 && (permanent_too
660 || buffer_permanent_local_flags[idx] == 0))
661 /* Is -2 used anywhere? */
662 || idx == -2)
7313acd0 663 PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
aab80822 664 }
1ab256cb
RM
665}
666
01050cb5
RM
667/* We split this away from generate-new-buffer, because rename-buffer
668 and set-visited-file-name ought to be able to use this to really
669 rename the buffer properly. */
670
671DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
c273e647 672 1, 2, 0,
01050cb5
RM
673 "Return a string that is the name of no existing buffer based on NAME.\n\
674If there is no live buffer named NAME, then return NAME.\n\
1ab256cb 675Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
c273e647 676until an unused name is found, and then return that name.\n\
03bdd54c 677Optional second argument IGNORE specifies a name that is okay to use\n\
c273e647 678\(if it is in the sequence to be tried)\n\
e8b3a22d 679even if a buffer with that name exists.")
c273e647
RS
680 (name, ignore)
681 register Lisp_Object name, ignore;
1ab256cb
RM
682{
683 register Lisp_Object gentemp, tem;
684 int count;
685 char number[10];
686
687 CHECK_STRING (name, 0);
688
689 tem = Fget_buffer (name);
265a9e55 690 if (NILP (tem))
01050cb5 691 return name;
1ab256cb
RM
692
693 count = 1;
694 while (1)
695 {
696 sprintf (number, "<%d>", ++count);
697 gentemp = concat2 (name, build_string (number));
638e4fc3 698 tem = Fstring_equal (gentemp, ignore);
c273e647
RS
699 if (!NILP (tem))
700 return gentemp;
1ab256cb 701 tem = Fget_buffer (gentemp);
265a9e55 702 if (NILP (tem))
01050cb5 703 return gentemp;
1ab256cb
RM
704 }
705}
706
707\f
708DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
709 "Return the name of BUFFER, as a string.\n\
01050cb5 710With no argument or nil as argument, return the name of the current buffer.")
1ab256cb
RM
711 (buffer)
712 register Lisp_Object buffer;
713{
265a9e55 714 if (NILP (buffer))
1ab256cb
RM
715 return current_buffer->name;
716 CHECK_BUFFER (buffer, 0);
717 return XBUFFER (buffer)->name;
718}
719
720DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
721 "Return name of file BUFFER is visiting, or nil if none.\n\
722No argument or nil as argument means use the current buffer.")
723 (buffer)
724 register Lisp_Object buffer;
725{
265a9e55 726 if (NILP (buffer))
1ab256cb
RM
727 return current_buffer->filename;
728 CHECK_BUFFER (buffer, 0);
729 return XBUFFER (buffer)->filename;
730}
731
336cd056
RS
732DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
733 0, 1, 0,
734 "Return the base buffer of indirect buffer BUFFER.\n\
735If BUFFER is not indirect, return nil.")
736 (buffer)
737 register Lisp_Object buffer;
738{
739 struct buffer *base;
740 Lisp_Object base_buffer;
741
742 if (NILP (buffer))
743 base = current_buffer->base_buffer;
744 else
745 {
746 CHECK_BUFFER (buffer, 0);
747 base = XBUFFER (buffer)->base_buffer;
748 }
749
750 if (! base)
751 return Qnil;
752 XSETBUFFER (base_buffer, base);
753 return base_buffer;
754}
755
1ab256cb
RM
756DEFUN ("buffer-local-variables", Fbuffer_local_variables,
757 Sbuffer_local_variables, 0, 1, 0,
758 "Return an alist of variables that are buffer-local in BUFFER.\n\
553defa4
RS
759Most elements look like (SYMBOL . VALUE), describing one variable.\n\
760For a symbol that is locally unbound, just the symbol appears in the value.\n\
1ab256cb
RM
761Note that storing new VALUEs in these elements doesn't change the variables.\n\
762No argument or nil as argument means use current buffer as BUFFER.")
763 (buffer)
764 register Lisp_Object buffer;
765{
766 register struct buffer *buf;
553defa4 767 register Lisp_Object result;
1ab256cb 768
265a9e55 769 if (NILP (buffer))
1ab256cb
RM
770 buf = current_buffer;
771 else
772 {
773 CHECK_BUFFER (buffer, 0);
774 buf = XBUFFER (buffer);
775 }
776
553defa4
RS
777 result = Qnil;
778
1ab256cb 779 {
553defa4 780 register Lisp_Object tail;
7539e11f 781 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1ab256cb 782 {
553defa4
RS
783 Lisp_Object val, elt;
784
7539e11f 785 elt = XCAR (tail);
553defa4 786
e0585c64
RS
787 /* Reference each variable in the alist in buf.
788 If inquiring about the current buffer, this gets the current values,
789 so store them into the alist so the alist is up to date.
790 If inquiring about some other buffer, this swaps out any values
791 for that buffer, making the alist up to date automatically. */
7539e11f 792 val = find_symbol_value (XCAR (elt));
e0585c64
RS
793 /* Use the current buffer value only if buf is the current buffer. */
794 if (buf != current_buffer)
7539e11f 795 val = XCDR (elt);
553defa4
RS
796
797 /* If symbol is unbound, put just the symbol in the list. */
798 if (EQ (val, Qunbound))
7539e11f 799 result = Fcons (XCAR (elt), result);
553defa4
RS
800 /* Otherwise, put (symbol . value) in the list. */
801 else
7539e11f 802 result = Fcons (Fcons (XCAR (elt), val), result);
1ab256cb
RM
803 }
804 }
805
1ab256cb
RM
806 /* Add on all the variables stored in special slots. */
807 {
7c02e886 808 int offset, idx;
1ab256cb 809
7313acd0 810 for (offset = PER_BUFFER_VAR_OFFSET (name);
1ab256cb 811 offset < sizeof (struct buffer);
7c02e886
GM
812 /* sizeof EMACS_INT == sizeof Lisp_Object */
813 offset += (sizeof (EMACS_INT)))
1ab256cb 814 {
7313acd0
GM
815 idx = PER_BUFFER_IDX (offset);
816 if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
817 && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
818 result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
819 PER_BUFFER_VALUE (buf, offset)),
7c02e886 820 result);
1ab256cb
RM
821 }
822 }
553defa4
RS
823
824 return result;
1ab256cb
RM
825}
826
827\f
828DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
829 0, 1, 0,
830 "Return t if BUFFER was modified since its file was last read or saved.\n\
831No argument or nil as argument means use current buffer as BUFFER.")
832 (buffer)
833 register Lisp_Object buffer;
834{
835 register struct buffer *buf;
265a9e55 836 if (NILP (buffer))
1ab256cb
RM
837 buf = current_buffer;
838 else
839 {
840 CHECK_BUFFER (buffer, 0);
841 buf = XBUFFER (buffer);
842 }
843
336cd056 844 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1ab256cb
RM
845}
846
847DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
848 1, 1, 0,
849 "Mark current buffer as modified or unmodified according to FLAG.\n\
850A non-nil FLAG means mark the buffer modified.")
851 (flag)
852 register Lisp_Object flag;
853{
854 register int already;
855 register Lisp_Object fn;
8ec01c70 856 Lisp_Object buffer, window;
1ab256cb
RM
857
858#ifdef CLASH_DETECTION
859 /* If buffer becoming modified, lock the file.
860 If buffer becoming unmodified, unlock the file. */
861
60f4dd23 862 fn = current_buffer->file_truename;
90d456d2
KH
863 /* Test buffer-file-name so that binding it to nil is effective. */
864 if (!NILP (fn) && ! NILP (current_buffer->filename))
1ab256cb 865 {
336cd056 866 already = SAVE_MODIFF < MODIFF;
265a9e55 867 if (!already && !NILP (flag))
1ab256cb 868 lock_file (fn);
265a9e55 869 else if (already && NILP (flag))
1ab256cb
RM
870 unlock_file (fn);
871 }
872#endif /* CLASH_DETECTION */
873
336cd056 874 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
8ec01c70
GM
875
876 /* Set update_mode_lines only if buffer is displayed in some window.
877 Packages like jit-lock or lazy-lock preserve a buffer's modified
878 state by recording/restoring the state around blocks of code.
879 Setting update_mode_lines makes redisplay consider all windows
880 (on all frames). Stealth fontification of buffers not displayed
881 would incur additional redisplay costs if we'd set
882 update_modes_lines unconditionally.
883
884 Ideally, I think there should be another mechanism for fontifying
885 buffers without "modifying" buffers, or redisplay should be
886 smarter about updating the `*' in mode lines. --gerd */
887 XSETBUFFER (buffer, current_buffer);
888 window = Fget_buffer_window (buffer, Qt);
889 if (WINDOWP (window))
890 update_mode_lines++;
891
1ab256cb
RM
892 return flag;
893}
894
a8c21b48
GM
895DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
896 Srestore_buffer_modified_p, 1, 1, 0,
897 "Like `set-buffer-modified-p', with a differences concerning redisplay.\n\
898It is not ensured that mode lines will be updated to show the modified\n\
899state of the current buffer. Use with care.")
900 (flag)
901 Lisp_Object flag;
902{
903#ifdef CLASH_DETECTION
904 Lisp_Object fn;
905
906 /* If buffer becoming modified, lock the file.
907 If buffer becoming unmodified, unlock the file. */
908
909 fn = current_buffer->file_truename;
910 /* Test buffer-file-name so that binding it to nil is effective. */
911 if (!NILP (fn) && ! NILP (current_buffer->filename))
912 {
913 int already = SAVE_MODIFF < MODIFF;
914 if (!already && !NILP (flag))
915 lock_file (fn);
916 else if (already && NILP (flag))
917 unlock_file (fn);
918 }
919#endif /* CLASH_DETECTION */
920
921 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
922 return flag;
923}
924
1ab256cb
RM
925DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
926 0, 1, 0,
927 "Return BUFFER's tick counter, incremented for each change in text.\n\
928Each buffer has a tick counter which is incremented each time the text in\n\
929that buffer is changed. It wraps around occasionally.\n\
930No argument or nil as argument means use current buffer as BUFFER.")
931 (buffer)
932 register Lisp_Object buffer;
933{
934 register struct buffer *buf;
265a9e55 935 if (NILP (buffer))
1ab256cb
RM
936 buf = current_buffer;
937 else
938 {
939 CHECK_BUFFER (buffer, 0);
940 buf = XBUFFER (buffer);
941 }
942
943 return make_number (BUF_MODIFF (buf));
944}
945\f
01050cb5 946DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
4c7e5f09 947 "sRename buffer (to new name): \nP",
1ab256cb 948 "Change current buffer's name to NEWNAME (a string).\n\
3bd779aa 949If second arg UNIQUE is nil or omitted, it is an error if a\n\
01050cb5 950buffer named NEWNAME already exists.\n\
3bd779aa 951If UNIQUE is non-nil, come up with a new name using\n\
01050cb5 952`generate-new-buffer-name'.\n\
3bd779aa
RS
953Interactively, you can set UNIQUE with a prefix argument.\n\
954We return the name we actually gave the buffer.\n\
1ab256cb 955This does not change the name of the visited file (if any).")
489c043a
RS
956 (newname, unique)
957 register Lisp_Object newname, unique;
1ab256cb
RM
958{
959 register Lisp_Object tem, buf;
960
489c043a 961 CHECK_STRING (newname, 0);
d59698c4 962
489c043a 963 if (XSTRING (newname)->size == 0)
d59698c4
RS
964 error ("Empty string is invalid as a buffer name");
965
489c043a 966 tem = Fget_buffer (newname);
265a9e55 967 if (!NILP (tem))
01050cb5 968 {
8801a864
KR
969 /* Don't short-circuit if UNIQUE is t. That is a useful way to
970 rename the buffer automatically so you can create another
971 with the original name. It makes UNIQUE equivalent to
972 (rename-buffer (generate-new-buffer-name NEWNAME)). */
973 if (NILP (unique) && XBUFFER (tem) == current_buffer)
974 return current_buffer->name;
3bd779aa 975 if (!NILP (unique))
489c043a 976 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
01050cb5 977 else
489c043a 978 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
01050cb5 979 }
1ab256cb 980
489c043a 981 current_buffer->name = newname;
76f590d7
JB
982
983 /* Catch redisplay's attention. Unless we do this, the mode lines for
984 any windows displaying current_buffer will stay unchanged. */
985 update_mode_lines++;
986
67180c6a 987 XSETBUFFER (buf, current_buffer);
489c043a 988 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
cf058e49
KH
989 if (NILP (current_buffer->filename)
990 && !NILP (current_buffer->auto_save_file_name))
1ab256cb 991 call0 (intern ("rename-auto-save-file"));
fb5eba9c
RS
992 /* Refetch since that last call may have done GC. */
993 return current_buffer->name;
1ab256cb
RM
994}
995
773fbdb9 996DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
1ab256cb 997 "Return most recently selected buffer other than BUFFER.\n\
a0ebb746
JB
998Buffers not visible in windows are preferred to visible buffers,\n\
999unless optional second argument VISIBLE-OK is non-nil.\n\
97c3c30c
RS
1000If the optional third argument FRAME is non-nil, use that frame's\n\
1001buffer list instead of the selected frame's buffer list.\n\
1ab256cb
RM
1002If no other buffer exists, the buffer `*scratch*' is returned.\n\
1003If BUFFER is omitted or nil, some interesting buffer is returned.")
773fbdb9
RS
1004 (buffer, visible_ok, frame)
1005 register Lisp_Object buffer, visible_ok, frame;
1ab256cb 1006{
89132f25 1007 Lisp_Object Fset_buffer_major_mode ();
7962a441 1008 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1ab256cb
RM
1009 notsogood = Qnil;
1010
773fbdb9 1011 if (NILP (frame))
9ba9623d 1012 frame = selected_frame;
773fbdb9 1013
7962a441 1014 tail = Vbuffer_alist;
773fbdb9 1015 pred = frame_buffer_predicate (frame);
7962a441
RS
1016
1017 /* Consider buffers that have been seen in the selected frame
1018 before other buffers. */
1019
773fbdb9 1020 tem = frame_buffer_list (frame);
7962a441
RS
1021 add_ons = Qnil;
1022 while (CONSP (tem))
1023 {
7539e11f
KR
1024 if (BUFFERP (XCAR (tem)))
1025 add_ons = Fcons (Fcons (Qnil, XCAR (tem)), add_ons);
1026 tem = XCDR (tem);
7962a441
RS
1027 }
1028 tail = nconc2 (Fnreverse (add_ons), tail);
1029
1030 for (; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
1031 {
1032 buf = Fcdr (Fcar (tail));
1033 if (EQ (buf, buffer))
1034 continue;
1035 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1036 continue;
04ae1b48
RS
1037 /* If the selected frame has a buffer_predicate,
1038 disregard buffers that don't fit the predicate. */
7962a441 1039 if (!NILP (pred))
04ae1b48 1040 {
7962a441 1041 tem = call1 (pred, buf);
04ae1b48
RS
1042 if (NILP (tem))
1043 continue;
1044 }
04ae1b48 1045
a0ebb746 1046 if (NILP (visible_ok))
db732e5a 1047 tem = Fget_buffer_window (buf, Qt);
a0ebb746
JB
1048 else
1049 tem = Qnil;
265a9e55 1050 if (NILP (tem))
1ab256cb 1051 return buf;
265a9e55 1052 if (NILP (notsogood))
1ab256cb
RM
1053 notsogood = buf;
1054 }
265a9e55 1055 if (!NILP (notsogood))
1ab256cb 1056 return notsogood;
dba1a30a
GM
1057 buf = Fget_buffer (build_string ("*scratch*"));
1058 if (NILP (buf))
1059 {
1060 buf = Fget_buffer_create (build_string ("*scratch*"));
1061 Fset_buffer_major_mode (buf);
1062 }
89132f25 1063 return buf;
1ab256cb
RM
1064}
1065\f
316784fb
KH
1066DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
1067 0, 1, "",
5b8bcf48
RS
1068 "Make BUFFER stop keeping undo information.\n\
1069No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
1070 (buffer)
1071 register Lisp_Object buffer;
1ab256cb 1072{
ffd56f97
JB
1073 Lisp_Object real_buffer;
1074
1075 if (NILP (buffer))
67180c6a 1076 XSETBUFFER (real_buffer, current_buffer);
ffd56f97
JB
1077 else
1078 {
1079 real_buffer = Fget_buffer (buffer);
1080 if (NILP (real_buffer))
1081 nsberror (buffer);
1082 }
1083
1084 XBUFFER (real_buffer)->undo_list = Qt;
1085
1ab256cb
RM
1086 return Qnil;
1087}
1088
1089DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
1090 0, 1, "",
1091 "Start keeping undo information for buffer BUFFER.\n\
1092No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
1093 (buffer)
1094 register Lisp_Object buffer;
1ab256cb 1095{
ffd56f97 1096 Lisp_Object real_buffer;
1ab256cb 1097
ffd56f97 1098 if (NILP (buffer))
67180c6a 1099 XSETBUFFER (real_buffer, current_buffer);
1ab256cb
RM
1100 else
1101 {
ffd56f97
JB
1102 real_buffer = Fget_buffer (buffer);
1103 if (NILP (real_buffer))
1104 nsberror (buffer);
1ab256cb
RM
1105 }
1106
ffd56f97
JB
1107 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
1108 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
1109
1110 return Qnil;
1111}
1112
1113/*
1114 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
1115Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
1116The buffer being killed will be current while the hook is running.\n\
1117See `kill-buffer'."
1118 */
1119DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1120 "Kill the buffer BUFFER.\n\
1121The argument may be a buffer or may be the name of a buffer.\n\
1122An argument of nil means kill the current buffer.\n\n\
1123Value is t if the buffer is actually killed, nil if user says no.\n\n\
1124The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1125if not void, is a list of functions to be called, with no arguments,\n\
1126before the buffer is actually killed. The buffer to be killed is current\n\
1127when the hook functions are called.\n\n\
1128Any processes that have this buffer as the `process-buffer' are killed\n\
b64d7442 1129with SIGHUP.")
a25f13ae
KH
1130 (buffer)
1131 Lisp_Object buffer;
1ab256cb
RM
1132{
1133 Lisp_Object buf;
1134 register struct buffer *b;
1135 register Lisp_Object tem;
1136 register struct Lisp_Marker *m;
6af718a4 1137 struct gcpro gcpro1;
1ab256cb 1138
a25f13ae 1139 if (NILP (buffer))
1ab256cb
RM
1140 buf = Fcurrent_buffer ();
1141 else
a25f13ae 1142 buf = Fget_buffer (buffer);
265a9e55 1143 if (NILP (buf))
a25f13ae 1144 nsberror (buffer);
1ab256cb
RM
1145
1146 b = XBUFFER (buf);
1147
4a4a9db5
KH
1148 /* Avoid trouble for buffer already dead. */
1149 if (NILP (b->name))
1150 return Qnil;
1151
1ab256cb 1152 /* Query if the buffer is still modified. */
265a9e55 1153 if (INTERACTIVE && !NILP (b->filename)
336cd056 1154 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1ab256cb 1155 {
a25f13ae 1156 GCPRO1 (buf);
1ab256cb
RM
1157 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1158 XSTRING (b->name)->data));
1159 UNGCPRO;
265a9e55 1160 if (NILP (tem))
1ab256cb
RM
1161 return Qnil;
1162 }
1163
dcdffbf6 1164 /* Run hooks with the buffer to be killed the current buffer. */
1ab256cb 1165 {
1ab256cb 1166 int count = specpdl_ptr - specpdl;
dcdffbf6 1167 Lisp_Object list;
1ab256cb
RM
1168
1169 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1170 set_buffer_internal (b);
dcdffbf6
RS
1171
1172 /* First run the query functions; if any query is answered no,
1173 don't kill the buffer. */
1174 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1175 {
1176 tem = call0 (Fcar (list));
1177 if (NILP (tem))
1178 return unbind_to (count, Qnil);
1179 }
1180
1181 /* Then run the hooks. */
fd186f07
RS
1182 if (!NILP (Vrun_hooks))
1183 call1 (Vrun_hooks, Qkill_buffer_hook);
1ab256cb
RM
1184 unbind_to (count, Qnil);
1185 }
1186
1187 /* We have no more questions to ask. Verify that it is valid
1188 to kill the buffer. This must be done after the questions
1189 since anything can happen within do_yes_or_no_p. */
1190
1191 /* Don't kill the minibuffer now current. */
1192 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1193 return Qnil;
1194
265a9e55 1195 if (NILP (b->name))
1ab256cb
RM
1196 return Qnil;
1197
336cd056
RS
1198 /* When we kill a base buffer, kill all its indirect buffers.
1199 We do it at this stage so nothing terrible happens if they
1200 ask questions or their hooks get errors. */
1201 if (! b->base_buffer)
1202 {
1203 struct buffer *other;
1204
1205 GCPRO1 (buf);
1206
1207 for (other = all_buffers; other; other = other->next)
4a4a9db5
KH
1208 /* all_buffers contains dead buffers too;
1209 don't re-kill them. */
1210 if (other->base_buffer == b && !NILP (other->name))
336cd056
RS
1211 {
1212 Lisp_Object buf;
1213 XSETBUFFER (buf, other);
1214 Fkill_buffer (buf);
1215 }
1216
1217 UNGCPRO;
1218 }
1219
1ab256cb
RM
1220 /* Make this buffer not be current.
1221 In the process, notice if this is the sole visible buffer
1222 and give up if so. */
1223 if (b == current_buffer)
1224 {
773fbdb9 1225 tem = Fother_buffer (buf, Qnil, Qnil);
1ab256cb
RM
1226 Fset_buffer (tem);
1227 if (b == current_buffer)
1228 return Qnil;
1229 }
1230
1231 /* Now there is no question: we can kill the buffer. */
1232
1233#ifdef CLASH_DETECTION
1234 /* Unlock this buffer's file, if it is locked. */
1235 unlock_buffer (b);
1236#endif /* CLASH_DETECTION */
1237
1ab256cb 1238 kill_buffer_processes (buf);
1ab256cb
RM
1239
1240 tem = Vinhibit_quit;
1241 Vinhibit_quit = Qt;
00550f94 1242 replace_buffer_in_all_windows (buf);
b26dd9cb 1243 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
7962a441 1244 frames_discard_buffer (buf);
1ab256cb
RM
1245 Vinhibit_quit = tem;
1246
9b59d6d0 1247 /* Delete any auto-save file, if we saved it in this session. */
a7a60ce9 1248 if (STRINGP (b->auto_save_file_name)
e95a0b39 1249 && b->auto_save_modified != 0
30e0071c 1250 && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
1ab256cb
RM
1251 {
1252 Lisp_Object tem;
1253 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 1254 if (! NILP (tem))
cbb6a418 1255 internal_delete_file (b->auto_save_file_name);
1ab256cb
RM
1256 }
1257
4a4a9db5
KH
1258 if (b->base_buffer)
1259 {
1260 /* Unchain all markers that belong to this indirect buffer.
1261 Don't unchain the markers that belong to the base buffer
1262 or its other indirect buffers. */
1263 for (tem = BUF_MARKERS (b); !NILP (tem); )
1264 {
1265 Lisp_Object next;
1266 m = XMARKER (tem);
1267 next = m->chain;
1268 if (m->buffer == b)
1269 unchain_marker (tem);
1270 tem = next;
1271 }
1272 }
1273 else
1ab256cb 1274 {
4a4a9db5 1275 /* Unchain all markers of this buffer and its indirect buffers.
336cd056 1276 and leave them pointing nowhere. */
4a4a9db5 1277 for (tem = BUF_MARKERS (b); !NILP (tem); )
336cd056
RS
1278 {
1279 m = XMARKER (tem);
1280 m->buffer = 0;
1281 tem = m->chain;
1282 m->chain = Qnil;
1283 }
1284 BUF_MARKERS (b) = Qnil;
336cd056 1285 BUF_INTERVALS (b) = NULL_INTERVAL;
336cd056
RS
1286
1287 /* Perhaps we should explicitly free the interval tree here... */
1288 }
33f7013e 1289
2f3f993b
RS
1290 /* Reset the local variables, so that this buffer's local values
1291 won't be protected from GC. They would be protected
1292 if they happened to remain encached in their symbols.
1293 This gets rid of them for certain. */
1294 swap_out_buffer_local_variables (b);
13de9290 1295 reset_buffer_local_variables (b, 1);
2f3f993b 1296
1ab256cb 1297 b->name = Qnil;
336cd056 1298
9ac0d9e0 1299 BLOCK_INPUT;
336cd056
RS
1300 if (! b->base_buffer)
1301 BUFFER_FREE (BUF_BEG_ADDR (b));
1302
28e969dd
JB
1303 if (b->newline_cache)
1304 {
1305 free_region_cache (b->newline_cache);
1306 b->newline_cache = 0;
1307 }
1308 if (b->width_run_cache)
1309 {
1310 free_region_cache (b->width_run_cache);
1311 b->width_run_cache = 0;
1312 }
1313 b->width_table = Qnil;
9ac0d9e0 1314 UNBLOCK_INPUT;
1ab256cb
RM
1315 b->undo_list = Qnil;
1316
1317 return Qt;
1318}
1319\f
36a8c287
JB
1320/* Move the assoc for buffer BUF to the front of buffer-alist. Since
1321 we do this each time BUF is selected visibly, the more recently
1322 selected buffers are always closer to the front of the list. This
1323 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb 1324
01136e9b 1325void
1ab256cb
RM
1326record_buffer (buf)
1327 Lisp_Object buf;
1328{
1329 register Lisp_Object link, prev;
773fbdb9 1330 Lisp_Object frame;
9ba9623d 1331 frame = selected_frame;
1ab256cb
RM
1332
1333 prev = Qnil;
7539e11f 1334 for (link = Vbuffer_alist; CONSP (link); link = XCDR (link))
1ab256cb 1335 {
7539e11f 1336 if (EQ (XCDR (XCAR (link)), buf))
1ab256cb
RM
1337 break;
1338 prev = link;
1339 }
1340
36a8c287
JB
1341 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1342 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 1343
265a9e55 1344 if (NILP (prev))
7539e11f 1345 Vbuffer_alist = XCDR (Vbuffer_alist);
1ab256cb 1346 else
7539e11f 1347 XCDR (prev) = XCDR (XCDR (prev));
1ab256cb 1348
7539e11f 1349 XCDR (link) = Vbuffer_alist;
1ab256cb 1350 Vbuffer_alist = link;
7962a441
RS
1351
1352 /* Now move this buffer to the front of frame_buffer_list also. */
1353
1354 prev = Qnil;
773fbdb9 1355 for (link = frame_buffer_list (frame); CONSP (link);
7539e11f 1356 link = XCDR (link))
7962a441 1357 {
7539e11f 1358 if (EQ (XCAR (link), buf))
7962a441
RS
1359 break;
1360 prev = link;
1361 }
1362
1363 /* Effectively do delq. */
1364
1365 if (CONSP (link))
1366 {
1367 if (NILP (prev))
773fbdb9 1368 set_frame_buffer_list (frame,
7539e11f 1369 XCDR (frame_buffer_list (frame)));
7962a441 1370 else
7539e11f 1371 XCDR (prev) = XCDR (XCDR (prev));
7962a441 1372
7539e11f 1373 XCDR (link) = frame_buffer_list (frame);
773fbdb9 1374 set_frame_buffer_list (frame, link);
7962a441
RS
1375 }
1376 else
773fbdb9 1377 set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
1ab256cb
RM
1378}
1379
a9ee7a59
KH
1380DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1381 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1382Use this function before selecting the buffer, since it may need to inspect\n\
1383the current buffer's major mode.")
a2428fa2
EN
1384 (buffer)
1385 Lisp_Object buffer;
a9ee7a59
KH
1386{
1387 int count;
1388 Lisp_Object function;
1389
1390 function = buffer_defaults.major_mode;
1391 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1392 function = current_buffer->major_mode;
1393
1394 if (NILP (function) || EQ (function, Qfundamental_mode))
1395 return Qnil;
1396
1397 count = specpdl_ptr - specpdl;
1398
1399 /* To select a nonfundamental mode,
1400 select the buffer temporarily and then call the mode function. */
1401
1402 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1403
a2428fa2 1404 Fset_buffer (buffer);
a9ee7a59
KH
1405 call0 (function);
1406
1407 return unbind_to (count, Qnil);
1408}
1409
2594e0fd
RS
1410/* If switching buffers in WINDOW would be an error, return
1411 a C string saying what the error would be. */
1412
1413char *
1414no_switch_window (window)
1415 Lisp_Object window;
1ab256cb 1416{
1ab256cb 1417 Lisp_Object tem;
2594e0fd
RS
1418 if (EQ (minibuf_window, window))
1419 return "Cannot switch buffers in minibuffer window";
1420 tem = Fwindow_dedicated_p (window);
265a9e55 1421 if (!NILP (tem))
2594e0fd
RS
1422 return "Cannot switch buffers in a dedicated window";
1423 return NULL;
1424}
1425
1426/* Switch to buffer BUFFER in the selected window.
1427 If NORECORD is non-nil, don't call record_buffer. */
1428
1429Lisp_Object
1430switch_to_buffer_1 (buffer, norecord)
1431 Lisp_Object buffer, norecord;
1432{
1433 register Lisp_Object buf;
1ab256cb 1434
a25f13ae 1435 if (NILP (buffer))
773fbdb9 1436 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1ab256cb 1437 else
a9ee7a59 1438 {
a25f13ae 1439 buf = Fget_buffer (buffer);
a9ee7a59
KH
1440 if (NILP (buf))
1441 {
a25f13ae 1442 buf = Fget_buffer_create (buffer);
a9ee7a59
KH
1443 Fset_buffer_major_mode (buf);
1444 }
1445 }
1ab256cb 1446 Fset_buffer (buf);
265a9e55 1447 if (NILP (norecord))
1ab256cb
RM
1448 record_buffer (buf);
1449
1450 Fset_window_buffer (EQ (selected_window, minibuf_window)
5fcd022d
JB
1451 ? Fnext_window (minibuf_window, Qnil, Qnil)
1452 : selected_window,
1ab256cb
RM
1453 buf);
1454
cd0c235a 1455 return buf;
1ab256cb
RM
1456}
1457
2594e0fd
RS
1458DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1459 "Select buffer BUFFER in the current window.\n\
1460BUFFER may be a buffer or a buffer name.\n\
1461Optional second arg NORECORD non-nil means\n\
1462do not put this buffer at the front of the list of recently selected ones.\n\
1463\n\
1464WARNING: This is NOT the way to work on another buffer temporarily\n\
1465within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1466the window-buffer correspondences.")
1467 (buffer, norecord)
1468 Lisp_Object buffer, norecord;
1469{
1470 char *err;
1471
1472 err = no_switch_window (selected_window);
1473 if (err) error (err);
1474
1475 return switch_to_buffer_1 (buffer, norecord);
1476}
1477
cd0c235a 1478DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1ab256cb
RM
1479 "Select buffer BUFFER in some window, preferably a different one.\n\
1480If BUFFER is nil, then some other buffer is chosen.\n\
1481If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1482If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
405615e5
RS
1483window even if BUFFER is already visible in the selected window.\n\
1484This uses the function `display-buffer' as a subroutine; see the documentation\n\
6d12711f
RS
1485of `display-buffer' for additional customization information.\n\
1486\n\
1487Optional third arg NORECORD non-nil means\n\
1488do not put this buffer at the front of the list of recently selected ones.")
1489 (buffer, other_window, norecord)
1490 Lisp_Object buffer, other_window, norecord;
1ab256cb
RM
1491{
1492 register Lisp_Object buf;
a25f13ae 1493 if (NILP (buffer))
773fbdb9 1494 buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
1ab256cb 1495 else
7c2087ee 1496 {
a25f13ae 1497 buf = Fget_buffer (buffer);
7c2087ee
RS
1498 if (NILP (buf))
1499 {
a25f13ae 1500 buf = Fget_buffer_create (buffer);
7c2087ee
RS
1501 Fset_buffer_major_mode (buf);
1502 }
1503 }
1ab256cb 1504 Fset_buffer (buf);
6d12711f
RS
1505 if (NILP (norecord))
1506 record_buffer (buf);
6b17d756 1507 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
e8b3a22d 1508 return buf;
1ab256cb
RM
1509}
1510
1511DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1512 "Return the current buffer as a Lisp object.")
1513 ()
1514{
1515 register Lisp_Object buf;
67180c6a 1516 XSETBUFFER (buf, current_buffer);
1ab256cb
RM
1517 return buf;
1518}
1519\f
7ec44ec6
GM
1520/* Set the current buffer to B.
1521
1522 We previously set windows_or_buffers_changed here to invalidate
1523 global unchanged information in beg_unchanged and end_unchanged.
1524 This is no longer necessary because we now compute unchanged
1525 information on a buffer-basis. Every action affecting other
1526 windows than the selected one requires a select_window at some
1527 time, and that increments windows_or_buffers_changed. */
1ab256cb
RM
1528
1529void
1530set_buffer_internal (b)
1531 register struct buffer *b;
1532{
b5a225b4 1533 if (current_buffer != b)
7ec44ec6 1534 set_buffer_internal_1 (b);
c7aa5005
KH
1535}
1536
1537/* Set the current buffer to B, and do not set windows_or_buffers_changed.
1538 This is used by redisplay. */
1539
1540void
1541set_buffer_internal_1 (b)
1542 register struct buffer *b;
1543{
1544 register struct buffer *old_buf;
1545 register Lisp_Object tail, valcontents;
1546 Lisp_Object tem;
1547
1548 if (current_buffer == b)
1549 return;
1550
1ab256cb
RM
1551 old_buf = current_buffer;
1552 current_buffer = b;
1553 last_known_column_point = -1; /* invalidate indentation cache */
1554
336cd056
RS
1555 if (old_buf)
1556 {
1557 /* Put the undo list back in the base buffer, so that it appears
1558 that an indirect buffer shares the undo list of its base. */
1559 if (old_buf->base_buffer)
1560 old_buf->base_buffer->undo_list = old_buf->undo_list;
1561
1562 /* If the old current buffer has markers to record PT, BEGV and ZV
1563 when it is not current, update them now. */
1564 if (! NILP (old_buf->pt_marker))
1565 {
1566 Lisp_Object obuf;
1567 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1568 set_marker_both (old_buf->pt_marker, obuf,
1569 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
336cd056
RS
1570 }
1571 if (! NILP (old_buf->begv_marker))
1572 {
1573 Lisp_Object obuf;
1574 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1575 set_marker_both (old_buf->begv_marker, obuf,
1576 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
336cd056
RS
1577 }
1578 if (! NILP (old_buf->zv_marker))
1579 {
1580 Lisp_Object obuf;
1581 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1582 set_marker_both (old_buf->zv_marker, obuf,
1583 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
336cd056
RS
1584 }
1585 }
1586
1587 /* Get the undo list from the base buffer, so that it appears
1588 that an indirect buffer shares the undo list of its base. */
1589 if (b->base_buffer)
1590 b->undo_list = b->base_buffer->undo_list;
1591
1592 /* If the new current buffer has markers to record PT, BEGV and ZV
1593 when it is not current, fetch them now. */
1594 if (! NILP (b->pt_marker))
3f236a40
RS
1595 {
1596 BUF_PT (b) = marker_position (b->pt_marker);
1597 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1598 }
336cd056 1599 if (! NILP (b->begv_marker))
3f236a40
RS
1600 {
1601 BUF_BEGV (b) = marker_position (b->begv_marker);
1602 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1603 }
336cd056 1604 if (! NILP (b->zv_marker))
3f236a40
RS
1605 {
1606 BUF_ZV (b) = marker_position (b->zv_marker);
1607 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1608 }
336cd056 1609
1ab256cb
RM
1610 /* Look down buffer's list of local Lisp variables
1611 to find and update any that forward into C variables. */
1612
7539e11f 1613 for (tail = b->local_var_alist; !NILP (tail); tail = XCDR (tail))
1ab256cb 1614 {
7539e11f 1615 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
a7a60ce9
KH
1616 if ((BUFFER_LOCAL_VALUEP (valcontents)
1617 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
3d871c85 1618 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
a7a60ce9 1619 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1620 /* Just reference the variable
1621 to cause it to become set for this buffer. */
7539e11f 1622 Fsymbol_value (XCAR (XCAR (tail)));
1ab256cb
RM
1623 }
1624
1625 /* Do the same with any others that were local to the previous buffer */
1626
1627 if (old_buf)
7539e11f 1628 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCDR (tail))
1ab256cb 1629 {
7539e11f 1630 valcontents = XSYMBOL (XCAR (XCAR (tail)))->value;
a7a60ce9
KH
1631 if ((BUFFER_LOCAL_VALUEP (valcontents)
1632 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
3d871c85 1633 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
a7a60ce9 1634 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1635 /* Just reference the variable
1636 to cause it to become set for this buffer. */
7539e11f 1637 Fsymbol_value (XCAR (XCAR (tail)));
1ab256cb
RM
1638 }
1639}
1640
336cd056 1641/* Switch to buffer B temporarily for redisplay purposes.
bbbe9545 1642 This avoids certain things that don't need to be done within redisplay. */
336cd056
RS
1643
1644void
1645set_buffer_temp (b)
1646 struct buffer *b;
1647{
1648 register struct buffer *old_buf;
1649
1650 if (current_buffer == b)
1651 return;
1652
1653 old_buf = current_buffer;
1654 current_buffer = b;
1655
1656 if (old_buf)
1657 {
1658 /* If the old current buffer has markers to record PT, BEGV and ZV
1659 when it is not current, update them now. */
1660 if (! NILP (old_buf->pt_marker))
1661 {
1662 Lisp_Object obuf;
1663 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1664 set_marker_both (old_buf->pt_marker, obuf,
1665 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
336cd056
RS
1666 }
1667 if (! NILP (old_buf->begv_marker))
1668 {
1669 Lisp_Object obuf;
1670 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1671 set_marker_both (old_buf->begv_marker, obuf,
1672 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
336cd056
RS
1673 }
1674 if (! NILP (old_buf->zv_marker))
1675 {
1676 Lisp_Object obuf;
1677 XSETBUFFER (obuf, old_buf);
3f236a40
RS
1678 set_marker_both (old_buf->zv_marker, obuf,
1679 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
336cd056
RS
1680 }
1681 }
1682
1683 /* If the new current buffer has markers to record PT, BEGV and ZV
1684 when it is not current, fetch them now. */
1685 if (! NILP (b->pt_marker))
3f236a40
RS
1686 {
1687 BUF_PT (b) = marker_position (b->pt_marker);
1688 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1689 }
336cd056 1690 if (! NILP (b->begv_marker))
3f236a40
RS
1691 {
1692 BUF_BEGV (b) = marker_position (b->begv_marker);
1693 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1694 }
336cd056 1695 if (! NILP (b->zv_marker))
3f236a40
RS
1696 {
1697 BUF_ZV (b) = marker_position (b->zv_marker);
1698 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1699 }
336cd056
RS
1700}
1701
1ab256cb
RM
1702DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1703 "Make the buffer BUFFER current for editing operations.\n\
1704BUFFER may be a buffer or the name of an existing buffer.\n\
1705See also `save-excursion' when you want to make a buffer current temporarily.\n\
1706This function does not display the buffer, so its effect ends\n\
1707when the current command terminates.\n\
1708Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
a25f13ae
KH
1709 (buffer)
1710 register Lisp_Object buffer;
1ab256cb 1711{
a25f13ae
KH
1712 register Lisp_Object buf;
1713 buf = Fget_buffer (buffer);
1714 if (NILP (buf))
1715 nsberror (buffer);
1716 if (NILP (XBUFFER (buf)->name))
1ab256cb 1717 error ("Selecting deleted buffer");
a25f13ae
KH
1718 set_buffer_internal (XBUFFER (buf));
1719 return buf;
1ab256cb 1720}
d0628b06
RS
1721
1722/* Set the current buffer to BUFFER provided it is alive. */
1723
1724Lisp_Object
1725set_buffer_if_live (buffer)
1726 Lisp_Object buffer;
1727{
1728 if (! NILP (XBUFFER (buffer)->name))
1729 Fset_buffer (buffer);
1730 return Qnil;
1731}
1ab256cb
RM
1732\f
1733DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1734 Sbarf_if_buffer_read_only, 0, 0, 0,
1735 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1736 ()
1737{
a96b68f1
RS
1738 if (!NILP (current_buffer->read_only)
1739 && NILP (Vinhibit_read_only))
1ab256cb
RM
1740 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1741 return Qnil;
1742}
1743
1744DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1745 "Put BUFFER at the end of the list of all buffers.\n\
1746There it is the least likely candidate for `other-buffer' to return;\n\
528415e7 1747thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
a5611885
JB
1748If BUFFER is nil or omitted, bury the current buffer.\n\
1749Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1750selected window if it is displayed there.")
a2428fa2
EN
1751 (buffer)
1752 register Lisp_Object buffer;
1ab256cb 1753{
b271272a 1754 /* Figure out what buffer we're going to bury. */
a2428fa2 1755 if (NILP (buffer))
a5611885 1756 {
a2428fa2 1757 XSETBUFFER (buffer, current_buffer);
0a63b212
RS
1758
1759 /* If we're burying the current buffer, unshow it. */
773fbdb9 1760 Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
a5611885 1761 }
1ab256cb
RM
1762 else
1763 {
1764 Lisp_Object buf1;
1765
a2428fa2 1766 buf1 = Fget_buffer (buffer);
265a9e55 1767 if (NILP (buf1))
a2428fa2
EN
1768 nsberror (buffer);
1769 buffer = buf1;
b271272a
JB
1770 }
1771
a1a8b28e
GM
1772 /* Move buffer to the end of the buffer list. Do nothing if the
1773 buffer is killed. */
1774 if (!NILP (XBUFFER (buffer)->name))
1775 {
1776 Lisp_Object aelt, link;
b271272a 1777
a1a8b28e
GM
1778 aelt = Frassq (buffer, Vbuffer_alist);
1779 link = Fmemq (aelt, Vbuffer_alist);
1780 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1781 XCDR (link) = Qnil;
1782 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1ab256cb 1783
a1a8b28e
GM
1784 frames_bury_buffer (buffer);
1785 }
dec989eb 1786
1ab256cb
RM
1787 return Qnil;
1788}
1789\f
c922bc55 1790DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1ab256cb 1791 "Delete the entire contents of the current buffer.\n\
2950a20e 1792Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1ab256cb
RM
1793so the buffer is truly empty after this.")
1794 ()
1795{
1796 Fwiden ();
c0d9a0c3
GM
1797
1798 del_range (BEG, Z);
c280bc6a 1799
1ab256cb
RM
1800 current_buffer->last_window_start = 1;
1801 /* Prevent warnings, or suspension of auto saving, that would happen
1802 if future size is less than past size. Use of erase-buffer
1803 implies that the future text is not really related to the past text. */
8d7a4592 1804 XSETFASTINT (current_buffer->save_length, 0);
1ab256cb
RM
1805 return Qnil;
1806}
1807
01136e9b 1808void
1ab256cb
RM
1809validate_region (b, e)
1810 register Lisp_Object *b, *e;
1811{
1ab256cb
RM
1812 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1813 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1814
1815 if (XINT (*b) > XINT (*e))
1816 {
03192067
KH
1817 Lisp_Object tem;
1818 tem = *b; *b = *e; *e = tem;
1ab256cb
RM
1819 }
1820
1821 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1822 && XINT (*e) <= ZV))
1823 args_out_of_range (*b, *e);
1824}
1825\f
b05525fa
RS
1826/* Advance BYTE_POS up to a character boundary
1827 and return the adjusted position. */
1828
1829static int
1830advance_to_char_boundary (byte_pos)
1831 int byte_pos;
1832{
f8449323 1833 int c;
b05525fa 1834
f8449323
RS
1835 if (byte_pos == BEG)
1836 /* Beginning of buffer is always a character boundary. */
1837 return 1;
1838
1839 c = FETCH_BYTE (byte_pos);
1840 if (! CHAR_HEAD_P (c))
b05525fa 1841 {
1be6387d 1842 /* We should advance BYTE_POS only when C is a constituent of a
f8449323 1843 multibyte sequence. */
a9bcded1
KH
1844 int orig_byte_pos = byte_pos;
1845
1846 do
1847 {
1848 byte_pos--;
1849 c = FETCH_BYTE (byte_pos);
1850 }
1851 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
f8449323 1852 INC_POS (byte_pos);
a9bcded1
KH
1853 if (byte_pos < orig_byte_pos)
1854 byte_pos = orig_byte_pos;
f8449323
RS
1855 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1856 surely advance to the correct character boundary. If C is
1857 not, BYTE_POS was unchanged. */
b05525fa
RS
1858 }
1859
20773569 1860 return byte_pos;
b05525fa
RS
1861}
1862
3ac81adb
RS
1863DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1864 1, 1, 0,
1865 "Set the multibyte flag of the current buffer to FLAG.\n\
1866If FLAG is t, this makes the buffer a multibyte buffer.\n\
1867If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1868The buffer contents remain unchanged as a sequence of bytes\n\
1869but the contents viewed as characters do change.")
1870 (flag)
1871 Lisp_Object flag;
1872{
1873 Lisp_Object tail, markers;
abc9d959 1874 struct buffer *other;
a9bcded1
KH
1875 int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
1876 int begv = BEGV, zv = ZV;
1877 int narrowed = (BEG != begv || Z != zv);
ed00559d 1878 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
3ac81adb 1879
6e553d5e
RS
1880 if (current_buffer->base_buffer)
1881 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
1882
70e77119
AS
1883 /* Do nothing if nothing actually changes. */
1884 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
1885 return flag;
1886
b05525fa
RS
1887 /* It would be better to update the list,
1888 but this is good enough for now. */
a9bcded1
KH
1889 if (undo_enabled_p)
1890 current_buffer->undo_list = Qt;
b05525fa 1891
3ac81adb
RS
1892 /* If the cached position is for this buffer, clear it out. */
1893 clear_charpos_cache (current_buffer);
1894
a9bcded1
KH
1895 if (narrowed)
1896 Fwiden ();
1897
3ac81adb
RS
1898 if (NILP (flag))
1899 {
a9bcded1
KH
1900 int pos, stop;
1901 unsigned char *p;
1902
3ac81adb
RS
1903 /* Do this first, so it can use CHAR_TO_BYTE
1904 to calculate the old correspondences. */
1905 set_intervals_multibyte (0);
1906
1907 current_buffer->enable_multibyte_characters = Qnil;
1908
1909 Z = Z_BYTE;
1910 BEGV = BEGV_BYTE;
1911 ZV = ZV_BYTE;
1912 GPT = GPT_BYTE;
1913 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
1914
1915 tail = BUF_MARKERS (current_buffer);
8801a864 1916 while (! NILP (tail))
3ac81adb
RS
1917 {
1918 XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
1919 tail = XMARKER (tail)->chain;
1920 }
a9bcded1
KH
1921
1922 /* Convert multibyte form of 8-bit characters to unibyte. */
1923 pos = BEG;
1924 stop = GPT;
1925 p = BEG_ADDR;
1926 while (1)
1927 {
1928 int c, bytes;
1929
1930 if (pos == stop)
1931 {
1932 if (pos == Z)
1933 break;
1934 p = GAP_END_ADDR;
1935 stop = Z;
1936 }
1937 if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
1938 p += bytes, pos += bytes;
1939 else
1940 {
1941 c = STRING_CHAR (p, stop - pos);
1942 /* Delete all bytes for this 8-bit character but the
1943 last one, and change the last one to the charcter
1944 code. */
1945 bytes--;
1946 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
1947 p = GAP_END_ADDR;
1948 *p++ = c;
1949 pos++;
1950 if (begv > pos)
1951 begv -= bytes;
1952 if (zv > pos)
1953 zv -= bytes;
1954 stop = Z;
1955 }
1956 }
1957 if (narrowed)
1958 Fnarrow_to_region (make_number (begv), make_number (zv));
3ac81adb
RS
1959 }
1960 else
1961 {
a9bcded1
KH
1962 int pt = PT;
1963 int pos, stop;
1964 unsigned char *p;
1965
673c57d2 1966 /* Be sure not to have a multibyte sequence striding over the GAP.
a9bcded1
KH
1967 Ex: We change this: "...abc\201 _GAP_ \241def..."
1968 to: "...abc _GAP_ \201\241def..." */
673c57d2
KH
1969
1970 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
1971 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
1972 {
1973 unsigned char *p = GPT_ADDR - 1;
1974
1975 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
1976 if (BASE_LEADING_CODE_P (*p))
1977 {
1978 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
1979
1980 move_gap_both (new_gpt, new_gpt);
1981 }
1982 }
1983
a9bcded1
KH
1984 /* Make the buffer contents valid as multibyte by converting
1985 8-bit characters to multibyte form. */
1986 pos = BEG;
1987 stop = GPT;
1988 p = BEG_ADDR;
1989 while (1)
1990 {
1991 int bytes;
1992
1993 if (pos == stop)
1994 {
1995 if (pos == Z)
1996 break;
1997 p = GAP_END_ADDR;
1998 stop = Z;
1999 }
2000
2001 if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
2002 p += bytes, pos += bytes;
2003 else
2004 {
2005 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2006
2007 bytes = CHAR_STRING (*p, tmp);
2008 *p = tmp[0];
2009 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2010 bytes--;
2011 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2012 /* Now the gap is after the just inserted data. */
2013 pos = GPT;
2014 p = GAP_END_ADDR;
2015 if (pos <= begv)
2016 begv += bytes;
2017 if (pos <= zv)
2018 zv += bytes;
2019 if (pos <= pt)
2020 pt += bytes;
2021 stop = Z;
2022 }
2023 }
2024
2025 if (pt != PT)
2026 TEMP_SET_PT (pt);
2027
2028 if (narrowed)
2029 Fnarrow_to_region (make_number (begv), make_number (zv));
2030
3ac81adb
RS
2031 /* Do this first, so that chars_in_text asks the right question.
2032 set_intervals_multibyte needs it too. */
2033 current_buffer->enable_multibyte_characters = Qt;
2034
b05525fa 2035 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
3ac81adb 2036 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
b05525fa 2037
673c57d2 2038 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
b05525fa
RS
2039
2040 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
3ac81adb 2041 if (BEGV_BYTE > GPT_BYTE)
673c57d2 2042 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2043 else
2044 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2045
2046 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
3ac81adb 2047 if (ZV_BYTE > GPT_BYTE)
673c57d2 2048 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2049 else
2050 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2051
2052 {
2053 int pt_byte = advance_to_char_boundary (PT_BYTE);
2054 int pt;
2055
2056 if (pt_byte > GPT_BYTE)
673c57d2 2057 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
b05525fa
RS
2058 else
2059 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2060 TEMP_SET_PT_BOTH (pt, pt_byte);
2061 }
3ac81adb
RS
2062
2063 tail = markers = BUF_MARKERS (current_buffer);
95fb069b
RS
2064
2065 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2066 getting confused by the markers that have not yet been updated.
2067 It is also a signal that it should never create a marker. */
3ac81adb
RS
2068 BUF_MARKERS (current_buffer) = Qnil;
2069
8801a864 2070 while (! NILP (tail))
3ac81adb 2071 {
b05525fa
RS
2072 XMARKER (tail)->bytepos
2073 = advance_to_char_boundary (XMARKER (tail)->bytepos);
3ac81adb 2074 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
b05525fa 2075
3ac81adb
RS
2076 tail = XMARKER (tail)->chain;
2077 }
b69f9797
RS
2078
2079 /* Make sure no markers were put on the chain
2080 while the chain value was incorrect. */
2081 if (! EQ (BUF_MARKERS (current_buffer), Qnil))
2082 abort ();
2083
3ac81adb
RS
2084 BUF_MARKERS (current_buffer) = markers;
2085
2086 /* Do this last, so it can calculate the new correspondences
2087 between chars and bytes. */
2088 set_intervals_multibyte (1);
2089 }
2090
a9bcded1
KH
2091 if (undo_enabled_p)
2092 current_buffer->undo_list = Qnil;
2093
724b203f
GM
2094 /* Changing the multibyteness of a buffer means that all windows
2095 showing that buffer must be updated thoroughly. */
2096 current_buffer->prevent_redisplay_optimizations_p = 1;
2097 ++windows_or_buffers_changed;
2098
abc9d959
RS
2099 /* Copy this buffer's new multibyte status
2100 into all of its indirect buffers. */
2101 for (other = all_buffers; other; other = other->next)
2102 if (other->base_buffer == current_buffer && !NILP (other->name))
724b203f
GM
2103 {
2104 other->enable_multibyte_characters
2105 = current_buffer->enable_multibyte_characters;
2106 other->prevent_redisplay_optimizations_p = 1;
2107 }
abc9d959 2108
ed00559d
KH
2109 /* Restore the modifiedness of the buffer. */
2110 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2111 Fset_buffer_modified_p (Qnil);
2112
3ac81adb
RS
2113 return flag;
2114}
2115\f
1ab256cb
RM
2116DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2117 0, 0, 0,
2118 "Switch to Fundamental mode by killing current buffer's local variables.\n\
2119Most local variable bindings are eliminated so that the default values\n\
2120become effective once more. Also, the syntax table is set from\n\
2121`standard-syntax-table', the local keymap is set to nil,\n\
2122and the abbrev table from `fundamental-mode-abbrev-table'.\n\
2123This function also forces redisplay of the mode line.\n\
2124\n\
2125Every function to select a new major mode starts by\n\
2126calling this function.\n\n\
2127As a special exception, local variables whose names have\n\
c5a15222
RS
2128a non-nil `permanent-local' property are not eliminated by this function.\n\
2129\n\
2130The first thing this function does is run\n\
2131the normal hook `change-major-mode-hook'.")
1ab256cb
RM
2132 ()
2133{
2134 register Lisp_Object alist, sym, tem;
2135 Lisp_Object oalist;
7410477a 2136
fd186f07
RS
2137 if (!NILP (Vrun_hooks))
2138 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1ab256cb
RM
2139 oalist = current_buffer->local_var_alist;
2140
2f3f993b
RS
2141 /* Make sure none of the bindings in oalist
2142 remain swapped in, in their symbols. */
1ab256cb 2143
2f3f993b 2144 swap_out_buffer_local_variables (current_buffer);
1ab256cb
RM
2145
2146 /* Actually eliminate all local bindings of this buffer. */
2147
13de9290 2148 reset_buffer_local_variables (current_buffer, 0);
1ab256cb
RM
2149
2150 /* Redisplay mode lines; we are changing major mode. */
2151
2152 update_mode_lines++;
2153
2154 /* Any which are supposed to be permanent,
2155 make local again, with the same values they had. */
2156
7539e11f 2157 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
1ab256cb 2158 {
7539e11f 2159 sym = XCAR (XCAR (alist));
1ab256cb 2160 tem = Fget (sym, Qpermanent_local);
265a9e55 2161 if (! NILP (tem))
01050cb5
RM
2162 {
2163 Fmake_local_variable (sym);
7539e11f 2164 Fset (sym, XCDR (XCAR (alist)));
01050cb5 2165 }
1ab256cb
RM
2166 }
2167
2168 /* Force mode-line redisplay. Useful here because all major mode
2169 commands call this function. */
2170 update_mode_lines++;
2171
2172 return Qnil;
2173}
2f3f993b
RS
2174
2175/* Make sure no local variables remain set up with buffer B
2176 for their current values. */
2177
2178static void
2179swap_out_buffer_local_variables (b)
2180 struct buffer *b;
2181{
2182 Lisp_Object oalist, alist, sym, tem, buffer;
2183
2184 XSETBUFFER (buffer, b);
2185 oalist = b->local_var_alist;
2186
7539e11f 2187 for (alist = oalist; !NILP (alist); alist = XCDR (alist))
2f3f993b 2188 {
7539e11f 2189 sym = XCAR (XCAR (alist));
2f3f993b
RS
2190
2191 /* Need not do anything if some other buffer's binding is now encached. */
3d871c85 2192 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
8801a864 2193 if (BUFFERP (tem) && XBUFFER (tem) == current_buffer)
2f3f993b
RS
2194 {
2195 /* Symbol is set up for this buffer's old local value.
2196 Set it up for the current buffer with the default value. */
2197
3d871c85 2198 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr;
2f3f993b
RS
2199 /* Store the symbol's current value into the alist entry
2200 it is currently set up for. This is so that, if the
2201 local is marked permanent, and we make it local again
2202 later in Fkill_all_local_variables, we don't lose the value. */
7539e11f 2203 XCDR (XCAR (tem))
3d871c85 2204 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
2f3f993b 2205 /* Switch to the symbol's default-value alist entry. */
7539e11f 2206 XCAR (tem) = tem;
2f3f993b 2207 /* Mark it as current for buffer B. */
3d871c85 2208 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
2f3f993b 2209 /* Store the current value into any forwarding in the symbol. */
3d871c85
RS
2210 store_symval_forwarding (sym,
2211 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
7539e11f 2212 XCDR (tem));
2f3f993b
RS
2213 }
2214 }
2215}
1ab256cb 2216\f
2eec3b4e
RS
2217/* Find all the overlays in the current buffer that contain position POS.
2218 Return the number found, and store them in a vector in *VEC_PTR.
2219 Store in *LEN_PTR the size allocated for the vector.
52f8ec73
JB
2220 Store in *NEXT_PTR the next position after POS where an overlay starts,
2221 or ZV if there are no more overlays.
bbbe9545 2222 Store in *PREV_PTR the previous position before POS where an overlay ends,
413e06a4
RS
2223 or where an overlay starts which ends at or after POS;
2224 or BEGV if there are no such overlays.
239c932b 2225 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
2226
2227 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
2228 when this function is called.
2229
2230 If EXTEND is non-zero, we make the vector bigger if necessary.
2231 If EXTEND is zero, we never extend the vector,
2232 and we store only as many overlays as will fit.
ac869cf7
MB
2233 But we still return the total number of overlays.
2234
2235 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2236 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2237 default (BEGV or ZV). */
2eec3b4e
RS
2238
2239int
ac869cf7 2240overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2eec3b4e 2241 int pos;
61d54cd5 2242 int extend;
2eec3b4e
RS
2243 Lisp_Object **vec_ptr;
2244 int *len_ptr;
2245 int *next_ptr;
239c932b 2246 int *prev_ptr;
ac869cf7 2247 int change_req;
1ab256cb 2248{
6af718a4 2249 Lisp_Object tail, overlay, start, end;
2eec3b4e
RS
2250 int idx = 0;
2251 int len = *len_ptr;
2252 Lisp_Object *vec = *vec_ptr;
2253 int next = ZV;
239c932b 2254 int prev = BEGV;
61d54cd5
RS
2255 int inhibit_storing = 0;
2256
2eec3b4e 2257 for (tail = current_buffer->overlays_before;
8fc0589a 2258 GC_CONSP (tail);
7539e11f 2259 tail = XCDR (tail))
2eec3b4e 2260 {
239c932b 2261 int startpos, endpos;
52f8ec73 2262
7539e11f 2263 overlay = XCAR (tail);
1ab256cb 2264
2eec3b4e
RS
2265 start = OVERLAY_START (overlay);
2266 end = OVERLAY_END (overlay);
239c932b
RS
2267 endpos = OVERLAY_POSITION (end);
2268 if (endpos < pos)
2269 {
2270 if (prev < endpos)
2271 prev = endpos;
2272 break;
2273 }
413e06a4
RS
2274 startpos = OVERLAY_POSITION (start);
2275 /* This one ends at or after POS
daa1c109 2276 so its start counts for PREV_PTR if it's before POS. */
413e06a4
RS
2277 if (prev < startpos && startpos < pos)
2278 prev = startpos;
239c932b
RS
2279 if (endpos == pos)
2280 continue;
2eec3b4e
RS
2281 if (startpos <= pos)
2282 {
2283 if (idx == len)
2284 {
61d54cd5
RS
2285 /* The supplied vector is full.
2286 Either make it bigger, or don't store any more in it. */
2287 if (extend)
2288 {
0552666b
GM
2289 /* Make it work with an initial len == 0. */
2290 len *= 2;
2291 if (len == 0)
2292 len = 4;
2293 *len_ptr = len;
61d54cd5
RS
2294 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2295 *vec_ptr = vec;
2296 }
2297 else
2298 inhibit_storing = 1;
2eec3b4e 2299 }
61d54cd5
RS
2300
2301 if (!inhibit_storing)
2302 vec[idx] = overlay;
2303 /* Keep counting overlays even if we can't return them all. */
2304 idx++;
2eec3b4e
RS
2305 }
2306 else if (startpos < next)
2307 next = startpos;
2308 }
2309
2310 for (tail = current_buffer->overlays_after;
8fc0589a 2311 GC_CONSP (tail);
7539e11f 2312 tail = XCDR (tail))
1ab256cb 2313 {
239c932b 2314 int startpos, endpos;
52f8ec73 2315
7539e11f 2316 overlay = XCAR (tail);
2eec3b4e
RS
2317
2318 start = OVERLAY_START (overlay);
2319 end = OVERLAY_END (overlay);
2320 startpos = OVERLAY_POSITION (start);
52f8ec73 2321 if (pos < startpos)
2eec3b4e
RS
2322 {
2323 if (startpos < next)
2324 next = startpos;
2325 break;
2326 }
239c932b
RS
2327 endpos = OVERLAY_POSITION (end);
2328 if (pos < endpos)
2eec3b4e
RS
2329 {
2330 if (idx == len)
2331 {
61d54cd5
RS
2332 if (extend)
2333 {
2334 *len_ptr = len *= 2;
0552666b
GM
2335 if (len == 0)
2336 len = *len_ptr = 4;
61d54cd5
RS
2337 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2338 *vec_ptr = vec;
2339 }
2340 else
2341 inhibit_storing = 1;
2eec3b4e 2342 }
61d54cd5
RS
2343
2344 if (!inhibit_storing)
2345 vec[idx] = overlay;
2346 idx++;
413e06a4
RS
2347
2348 if (startpos < pos && startpos > prev)
2349 prev = startpos;
2eec3b4e 2350 }
239c932b
RS
2351 else if (endpos < pos && endpos > prev)
2352 prev = endpos;
ac869cf7 2353 else if (endpos == pos && startpos > prev && !change_req)
413e06a4 2354 prev = startpos;
1ab256cb
RM
2355 }
2356
239c932b
RS
2357 if (next_ptr)
2358 *next_ptr = next;
2359 if (prev_ptr)
2360 *prev_ptr = prev;
2eec3b4e
RS
2361 return idx;
2362}
74514898
RS
2363\f
2364/* Find all the overlays in the current buffer that overlap the range BEG-END
2a3eeee7
RS
2365 or are empty at BEG.
2366
74514898
RS
2367 Return the number found, and store them in a vector in *VEC_PTR.
2368 Store in *LEN_PTR the size allocated for the vector.
2369 Store in *NEXT_PTR the next position after POS where an overlay starts,
2370 or ZV if there are no more overlays.
2371 Store in *PREV_PTR the previous position before POS where an overlay ends,
2372 or BEGV if there are no previous overlays.
2373 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2374
2375 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2376 when this function is called.
2377
2378 If EXTEND is non-zero, we make the vector bigger if necessary.
2379 If EXTEND is zero, we never extend the vector,
2380 and we store only as many overlays as will fit.
2381 But we still return the total number of overlays. */
2382
2383int
2384overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2385 int beg, end;
2386 int extend;
2387 Lisp_Object **vec_ptr;
2388 int *len_ptr;
2389 int *next_ptr;
2390 int *prev_ptr;
2391{
6af718a4 2392 Lisp_Object tail, overlay, ostart, oend;
74514898
RS
2393 int idx = 0;
2394 int len = *len_ptr;
2395 Lisp_Object *vec = *vec_ptr;
2396 int next = ZV;
2397 int prev = BEGV;
2398 int inhibit_storing = 0;
2399
2400 for (tail = current_buffer->overlays_before;
2401 GC_CONSP (tail);
7539e11f 2402 tail = XCDR (tail))
74514898
RS
2403 {
2404 int startpos, endpos;
2405
7539e11f 2406 overlay = XCAR (tail);
74514898
RS
2407
2408 ostart = OVERLAY_START (overlay);
2409 oend = OVERLAY_END (overlay);
2410 endpos = OVERLAY_POSITION (oend);
2411 if (endpos < beg)
2412 {
2413 if (prev < endpos)
2414 prev = endpos;
2415 break;
2416 }
2417 startpos = OVERLAY_POSITION (ostart);
2418 /* Count an interval if it either overlaps the range
2a3eeee7 2419 or is empty at the start of the range. */
74514898 2420 if ((beg < endpos && startpos < end)
2a3eeee7 2421 || (startpos == endpos && beg == endpos))
74514898
RS
2422 {
2423 if (idx == len)
2424 {
2425 /* The supplied vector is full.
2426 Either make it bigger, or don't store any more in it. */
2427 if (extend)
2428 {
2429 *len_ptr = len *= 2;
2430 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2431 *vec_ptr = vec;
2432 }
2433 else
2434 inhibit_storing = 1;
2435 }
2436
2437 if (!inhibit_storing)
2438 vec[idx] = overlay;
2439 /* Keep counting overlays even if we can't return them all. */
2440 idx++;
2441 }
2442 else if (startpos < next)
2443 next = startpos;
2444 }
2445
2446 for (tail = current_buffer->overlays_after;
2447 GC_CONSP (tail);
7539e11f 2448 tail = XCDR (tail))
74514898
RS
2449 {
2450 int startpos, endpos;
2451
7539e11f 2452 overlay = XCAR (tail);
74514898
RS
2453
2454 ostart = OVERLAY_START (overlay);
2455 oend = OVERLAY_END (overlay);
2456 startpos = OVERLAY_POSITION (ostart);
2457 if (end < startpos)
2458 {
2459 if (startpos < next)
2460 next = startpos;
2461 break;
2462 }
2463 endpos = OVERLAY_POSITION (oend);
2a3eeee7
RS
2464 /* Count an interval if it either overlaps the range
2465 or is empty at the start of the range. */
74514898 2466 if ((beg < endpos && startpos < end)
2a3eeee7 2467 || (startpos == endpos && beg == endpos))
74514898
RS
2468 {
2469 if (idx == len)
2470 {
2471 if (extend)
2472 {
2473 *len_ptr = len *= 2;
2474 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2475 *vec_ptr = vec;
2476 }
2477 else
2478 inhibit_storing = 1;
2479 }
2480
2481 if (!inhibit_storing)
2482 vec[idx] = overlay;
2483 idx++;
2484 }
2485 else if (endpos < beg && endpos > prev)
2486 prev = endpos;
2487 }
fc04fa47 2488
74514898
RS
2489 if (next_ptr)
2490 *next_ptr = next;
2491 if (prev_ptr)
2492 *prev_ptr = prev;
2493 return idx;
2494}
2495\f
fc04fa47
KH
2496/* Fast function to just test if we're at an overlay boundary. */
2497int
2498overlay_touches_p (pos)
2499 int pos;
2500{
2501 Lisp_Object tail, overlay;
2502
2503 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
7539e11f 2504 tail = XCDR (tail))
fc04fa47
KH
2505 {
2506 int endpos;
2507
7539e11f 2508 overlay = XCAR (tail);
fc04fa47
KH
2509 if (!GC_OVERLAYP (overlay))
2510 abort ();
2511
2512 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2513 if (endpos < pos)
2514 break;
2515 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2516 return 1;
2517 }
2518
2519 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
7539e11f 2520 tail = XCDR (tail))
fc04fa47
KH
2521 {
2522 int startpos;
2523
7539e11f 2524 overlay = XCAR (tail);
fc04fa47
KH
2525 if (!GC_OVERLAYP (overlay))
2526 abort ();
2527
2528 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2529 if (pos < startpos)
2530 break;
2531 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2532 return 1;
2533 }
2534 return 0;
2535}
2eec3b4e 2536\f
5985d248
KH
2537struct sortvec
2538{
2539 Lisp_Object overlay;
2540 int beg, end;
2541 int priority;
2542};
2543
2544static int
dfcf069d
AS
2545compare_overlays (v1, v2)
2546 const void *v1, *v2;
5985d248 2547{
dfcf069d
AS
2548 const struct sortvec *s1 = (const struct sortvec *) v1;
2549 const struct sortvec *s2 = (const struct sortvec *) v2;
5985d248
KH
2550 if (s1->priority != s2->priority)
2551 return s1->priority - s2->priority;
2552 if (s1->beg != s2->beg)
2553 return s1->beg - s2->beg;
2554 if (s1->end != s2->end)
2555 return s2->end - s1->end;
2556 return 0;
2557}
2558
2559/* Sort an array of overlays by priority. The array is modified in place.
2560 The return value is the new size; this may be smaller than the original
2561 size if some of the overlays were invalid or were window-specific. */
2562int
2563sort_overlays (overlay_vec, noverlays, w)
2564 Lisp_Object *overlay_vec;
2565 int noverlays;
2566 struct window *w;
2567{
2568 int i, j;
2569 struct sortvec *sortvec;
2570 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2571
2572 /* Put the valid and relevant overlays into sortvec. */
2573
2574 for (i = 0, j = 0; i < noverlays; i++)
2575 {
0fa767e7 2576 Lisp_Object tem;
c99fc30f 2577 Lisp_Object overlay;
5985d248 2578
c99fc30f 2579 overlay = overlay_vec[i];
5985d248
KH
2580 if (OVERLAY_VALID (overlay)
2581 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2582 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2583 {
0fa767e7
KH
2584 /* If we're interested in a specific window, then ignore
2585 overlays that are limited to some other window. */
2586 if (w)
5985d248 2587 {
0fa767e7
KH
2588 Lisp_Object window;
2589
2590 window = Foverlay_get (overlay, Qwindow);
a7a60ce9 2591 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 2592 continue;
5985d248 2593 }
0fa767e7
KH
2594
2595 /* This overlay is good and counts: put it into sortvec. */
2596 sortvec[j].overlay = overlay;
2597 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2598 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2599 tem = Foverlay_get (overlay, Qpriority);
2600 if (INTEGERP (tem))
2601 sortvec[j].priority = XINT (tem);
2602 else
2603 sortvec[j].priority = 0;
2604 j++;
5985d248
KH
2605 }
2606 }
2607 noverlays = j;
2608
2609 /* Sort the overlays into the proper order: increasing priority. */
2610
2611 if (noverlays > 1)
2612 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2613
2614 for (i = 0; i < noverlays; i++)
2615 overlay_vec[i] = sortvec[i].overlay;
2616 return (noverlays);
2617}
2618\f
bbbe9545
KH
2619struct sortstr
2620{
cb26008f 2621 Lisp_Object string, string2;
bbbe9545
KH
2622 int size;
2623 int priority;
2624};
2625
e8185fa8
KH
2626struct sortstrlist
2627{
2628 struct sortstr *buf; /* An array that expands as needed; never freed. */
2629 int size; /* Allocated length of that array. */
2630 int used; /* How much of the array is currently in use. */
2631 int bytes; /* Total length of the strings in buf. */
2632};
2633
2634/* Buffers for storing information about the overlays touching a given
2635 position. These could be automatic variables in overlay_strings, but
2636 it's more efficient to hold onto the memory instead of repeatedly
2637 allocating and freeing it. */
2638static struct sortstrlist overlay_heads, overlay_tails;
9492daf2 2639static unsigned char *overlay_str_buf;
e8185fa8
KH
2640
2641/* Allocated length of overlay_str_buf. */
2642static int overlay_str_len;
2643
bbbe9545
KH
2644/* A comparison function suitable for passing to qsort. */
2645static int
2646cmp_for_strings (as1, as2)
2647 char *as1, *as2;
2648{
2649 struct sortstr *s1 = (struct sortstr *)as1;
2650 struct sortstr *s2 = (struct sortstr *)as2;
2651 if (s1->size != s2->size)
2652 return s2->size - s1->size;
2653 if (s1->priority != s2->priority)
2654 return s1->priority - s2->priority;
2655 return 0;
2656}
2657
e8185fa8 2658static void
cb26008f 2659record_overlay_string (ssl, str, str2, pri, size)
e8185fa8 2660 struct sortstrlist *ssl;
cb26008f 2661 Lisp_Object str, str2, pri;
e8185fa8
KH
2662 int size;
2663{
43d27a72
RS
2664 int nbytes;
2665
e8185fa8
KH
2666 if (ssl->used == ssl->size)
2667 {
2668 if (ssl->buf)
2669 ssl->size *= 2;
2670 else
2671 ssl->size = 5;
2672 ssl->buf = ((struct sortstr *)
2673 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2674 }
2675 ssl->buf[ssl->used].string = str;
cb26008f 2676 ssl->buf[ssl->used].string2 = str2;
e8185fa8
KH
2677 ssl->buf[ssl->used].size = size;
2678 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2679 ssl->used++;
43d27a72
RS
2680
2681 if (NILP (current_buffer->enable_multibyte_characters))
2682 nbytes = XSTRING (str)->size;
2683 else if (! STRING_MULTIBYTE (str))
2684 nbytes = count_size_as_multibyte (XSTRING (str)->data,
fc932ac6 2685 STRING_BYTES (XSTRING (str)));
43d27a72 2686 else
fc932ac6 2687 nbytes = STRING_BYTES (XSTRING (str));
43d27a72
RS
2688
2689 ssl->bytes += nbytes;
2690
cb26008f 2691 if (STRINGP (str2))
43d27a72
RS
2692 {
2693 if (NILP (current_buffer->enable_multibyte_characters))
2694 nbytes = XSTRING (str2)->size;
2695 else if (! STRING_MULTIBYTE (str2))
2696 nbytes = count_size_as_multibyte (XSTRING (str2)->data,
fc932ac6 2697 STRING_BYTES (XSTRING (str2)));
43d27a72 2698 else
fc932ac6 2699 nbytes = STRING_BYTES (XSTRING (str2));
43d27a72
RS
2700
2701 ssl->bytes += nbytes;
2702 }
e8185fa8 2703}
bbbe9545
KH
2704
2705/* Return the concatenation of the strings associated with overlays that
2706 begin or end at POS, ignoring overlays that are specific to a window
2707 other than W. The strings are concatenated in the appropriate order:
2708 shorter overlays nest inside longer ones, and higher priority inside
cb26008f
KH
2709 lower. Normally all of the after-strings come first, but zero-sized
2710 overlays have their after-strings ride along with the before-strings
2711 because it would look strange to print them inside-out.
2712
2713 Returns the string length, and stores the contents indirectly through
2714 PSTR, if that variable is non-null. The string may be overwritten by
2715 subsequent calls. */
6b5d3b89 2716
bbbe9545
KH
2717int
2718overlay_strings (pos, w, pstr)
2719 int pos;
2720 struct window *w;
6b5d3b89 2721 unsigned char **pstr;
bbbe9545 2722{
e8185fa8 2723 Lisp_Object ov, overlay, window, str;
bbbe9545 2724 int startpos, endpos;
43d27a72 2725 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
bbbe9545 2726
e8185fa8
KH
2727 overlay_heads.used = overlay_heads.bytes = 0;
2728 overlay_tails.used = overlay_tails.bytes = 0;
7539e11f 2729 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
bbbe9545 2730 {
7539e11f 2731 overlay = XCAR (ov);
bbbe9545
KH
2732 if (!OVERLAYP (overlay))
2733 abort ();
2734
2735 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2736 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2737 if (endpos < pos)
2738 break;
2739 if (endpos != pos && startpos != pos)
2740 continue;
2741 window = Foverlay_get (overlay, Qwindow);
2742 if (WINDOWP (window) && XWINDOW (window) != w)
2743 continue;
e8185fa8
KH
2744 if (startpos == pos
2745 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2746 record_overlay_string (&overlay_heads, str,
cb26008f
KH
2747 (startpos == endpos
2748 ? Foverlay_get (overlay, Qafter_string)
2749 : Qnil),
2750 Foverlay_get (overlay, Qpriority),
2751 endpos - startpos);
2752 else if (endpos == pos
2753 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2754 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
2755 Foverlay_get (overlay, Qpriority),
2756 endpos - startpos);
bbbe9545 2757 }
7539e11f 2758 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
bbbe9545 2759 {
7539e11f 2760 overlay = XCAR (ov);
bbbe9545
KH
2761 if (!OVERLAYP (overlay))
2762 abort ();
2763
2764 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2765 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2766 if (startpos > pos)
2767 break;
e8185fa8
KH
2768 if (endpos != pos && startpos != pos)
2769 continue;
2770 window = Foverlay_get (overlay, Qwindow);
2771 if (WINDOWP (window) && XWINDOW (window) != w)
2772 continue;
e8185fa8
KH
2773 if (startpos == pos
2774 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2775 record_overlay_string (&overlay_heads, str,
cb26008f
KH
2776 (startpos == endpos
2777 ? Foverlay_get (overlay, Qafter_string)
2778 : Qnil),
2779 Foverlay_get (overlay, Qpriority),
2780 endpos - startpos);
2781 else if (endpos == pos
2782 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2783 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
2784 Foverlay_get (overlay, Qpriority),
2785 endpos - startpos);
bbbe9545 2786 }
e8185fa8
KH
2787 if (overlay_tails.used > 1)
2788 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2789 cmp_for_strings);
2790 if (overlay_heads.used > 1)
2791 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2792 cmp_for_strings);
2793 if (overlay_heads.bytes || overlay_tails.bytes)
bbbe9545 2794 {
e8185fa8 2795 Lisp_Object tem;
bbbe9545 2796 int i;
9f4d7cde 2797 unsigned char *p;
e8185fa8 2798 int total = overlay_heads.bytes + overlay_tails.bytes;
bbbe9545
KH
2799
2800 if (total > overlay_str_len)
9f4d7cde
RS
2801 {
2802 overlay_str_len = total;
2803 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2804 total);
2805 }
bbbe9545 2806 p = overlay_str_buf;
e8185fa8 2807 for (i = overlay_tails.used; --i >= 0;)
bbbe9545 2808 {
43d27a72 2809 int nbytes;
e8185fa8 2810 tem = overlay_tails.buf[i].string;
fc932ac6
RS
2811 nbytes = copy_text (XSTRING (tem)->data, p,
2812 STRING_BYTES (XSTRING (tem)),
43d27a72
RS
2813 STRING_MULTIBYTE (tem), multibyte);
2814 p += nbytes;
bbbe9545 2815 }
e8185fa8 2816 for (i = 0; i < overlay_heads.used; ++i)
bbbe9545 2817 {
43d27a72 2818 int nbytes;
e8185fa8 2819 tem = overlay_heads.buf[i].string;
fc932ac6
RS
2820 nbytes = copy_text (XSTRING (tem)->data, p,
2821 STRING_BYTES (XSTRING (tem)),
43d27a72
RS
2822 STRING_MULTIBYTE (tem), multibyte);
2823 p += nbytes;
cb26008f
KH
2824 tem = overlay_heads.buf[i].string2;
2825 if (STRINGP (tem))
2826 {
43d27a72 2827 nbytes = copy_text (XSTRING (tem)->data, p,
fc932ac6 2828 STRING_BYTES (XSTRING (tem)),
43d27a72
RS
2829 STRING_MULTIBYTE (tem), multibyte);
2830 p += nbytes;
cb26008f 2831 }
bbbe9545 2832 }
cb26008f
KH
2833 if (p != overlay_str_buf + total)
2834 abort ();
bbbe9545
KH
2835 if (pstr)
2836 *pstr = overlay_str_buf;
e8185fa8 2837 return total;
bbbe9545 2838 }
e8185fa8 2839 return 0;
bbbe9545
KH
2840}
2841\f
5c4f68f1 2842/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 2843
2eec3b4e 2844void
5c4f68f1
JB
2845recenter_overlay_lists (buf, pos)
2846 struct buffer *buf;
2eec3b4e
RS
2847 int pos;
2848{
2849 Lisp_Object overlay, tail, next, prev, beg, end;
2850
2851 /* See if anything in overlays_before should move to overlays_after. */
2852
2853 /* We don't strictly need prev in this loop; it should always be nil.
2854 But we use it for symmetry and in case that should cease to be true
2855 with some future change. */
2856 prev = Qnil;
5c4f68f1 2857 for (tail = buf->overlays_before;
2eec3b4e
RS
2858 CONSP (tail);
2859 prev = tail, tail = next)
1ab256cb 2860 {
7539e11f
KR
2861 next = XCDR (tail);
2862 overlay = XCAR (tail);
2eec3b4e
RS
2863
2864 /* If the overlay is not valid, get rid of it. */
2865 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
2866#if 1
2867 abort ();
2868#else
2eec3b4e
RS
2869 {
2870 /* Splice the cons cell TAIL out of overlays_before. */
2871 if (!NILP (prev))
7539e11f 2872 XCDR (prev) = next;
2eec3b4e 2873 else
5c4f68f1 2874 buf->overlays_before = next;
2eec3b4e
RS
2875 tail = prev;
2876 continue;
2877 }
52f8ec73 2878#endif
1ab256cb 2879
2eec3b4e
RS
2880 beg = OVERLAY_START (overlay);
2881 end = OVERLAY_END (overlay);
1ab256cb 2882
2eec3b4e 2883 if (OVERLAY_POSITION (end) > pos)
1ab256cb 2884 {
2eec3b4e
RS
2885 /* OVERLAY needs to be moved. */
2886 int where = OVERLAY_POSITION (beg);
2887 Lisp_Object other, other_prev;
2888
2889 /* Splice the cons cell TAIL out of overlays_before. */
2890 if (!NILP (prev))
7539e11f 2891 XCDR (prev) = next;
2eec3b4e 2892 else
5c4f68f1 2893 buf->overlays_before = next;
2eec3b4e
RS
2894
2895 /* Search thru overlays_after for where to put it. */
2896 other_prev = Qnil;
5c4f68f1 2897 for (other = buf->overlays_after;
2eec3b4e 2898 CONSP (other);
7539e11f 2899 other_prev = other, other = XCDR (other))
1ab256cb 2900 {
6af718a4 2901 Lisp_Object otherbeg, otheroverlay;
2eec3b4e 2902
7539e11f 2903 otheroverlay = XCAR (other);
2eec3b4e 2904 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 2905 abort ();
2eec3b4e
RS
2906
2907 otherbeg = OVERLAY_START (otheroverlay);
2908 if (OVERLAY_POSITION (otherbeg) >= where)
2909 break;
1ab256cb 2910 }
2eec3b4e
RS
2911
2912 /* Add TAIL to overlays_after before OTHER. */
7539e11f 2913 XCDR (tail) = other;
2eec3b4e 2914 if (!NILP (other_prev))
7539e11f 2915 XCDR (other_prev) = tail;
1ab256cb 2916 else
5c4f68f1 2917 buf->overlays_after = tail;
2eec3b4e 2918 tail = prev;
1ab256cb 2919 }
2eec3b4e
RS
2920 else
2921 /* We've reached the things that should stay in overlays_before.
2922 All the rest of overlays_before must end even earlier,
2923 so stop now. */
2924 break;
2925 }
2926
2927 /* See if anything in overlays_after should be in overlays_before. */
2928 prev = Qnil;
5c4f68f1 2929 for (tail = buf->overlays_after;
2eec3b4e
RS
2930 CONSP (tail);
2931 prev = tail, tail = next)
2932 {
7539e11f
KR
2933 next = XCDR (tail);
2934 overlay = XCAR (tail);
2eec3b4e
RS
2935
2936 /* If the overlay is not valid, get rid of it. */
2937 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
2938#if 1
2939 abort ();
2940#else
2eec3b4e
RS
2941 {
2942 /* Splice the cons cell TAIL out of overlays_after. */
2943 if (!NILP (prev))
7539e11f 2944 XCDR (prev) = next;
2eec3b4e 2945 else
5c4f68f1 2946 buf->overlays_after = next;
2eec3b4e
RS
2947 tail = prev;
2948 continue;
2949 }
52f8ec73 2950#endif
2eec3b4e
RS
2951
2952 beg = OVERLAY_START (overlay);
2953 end = OVERLAY_END (overlay);
2954
2955 /* Stop looking, when we know that nothing further
2956 can possibly end before POS. */
2957 if (OVERLAY_POSITION (beg) > pos)
2958 break;
2959
2960 if (OVERLAY_POSITION (end) <= pos)
2961 {
2962 /* OVERLAY needs to be moved. */
2963 int where = OVERLAY_POSITION (end);
2964 Lisp_Object other, other_prev;
2965
2966 /* Splice the cons cell TAIL out of overlays_after. */
2967 if (!NILP (prev))
7539e11f 2968 XCDR (prev) = next;
2eec3b4e 2969 else
5c4f68f1 2970 buf->overlays_after = next;
2eec3b4e
RS
2971
2972 /* Search thru overlays_before for where to put it. */
2973 other_prev = Qnil;
5c4f68f1 2974 for (other = buf->overlays_before;
2eec3b4e 2975 CONSP (other);
7539e11f 2976 other_prev = other, other = XCDR (other))
2eec3b4e
RS
2977 {
2978 Lisp_Object otherend, otheroverlay;
2eec3b4e 2979
7539e11f 2980 otheroverlay = XCAR (other);
2eec3b4e 2981 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 2982 abort ();
2eec3b4e
RS
2983
2984 otherend = OVERLAY_END (otheroverlay);
2985 if (OVERLAY_POSITION (otherend) <= where)
2986 break;
2987 }
2988
2989 /* Add TAIL to overlays_before before OTHER. */
7539e11f 2990 XCDR (tail) = other;
2eec3b4e 2991 if (!NILP (other_prev))
7539e11f 2992 XCDR (other_prev) = tail;
2eec3b4e 2993 else
5c4f68f1 2994 buf->overlays_before = tail;
2eec3b4e
RS
2995 tail = prev;
2996 }
2997 }
2998
8d7a4592 2999 XSETFASTINT (buf->overlay_center, pos);
2eec3b4e 3000}
2b1bdf65 3001
423cdb46
KH
3002void
3003adjust_overlays_for_insert (pos, length)
3004 int pos;
3005 int length;
3006{
3007 /* After an insertion, the lists are still sorted properly,
3008 but we may need to update the value of the overlay center. */
3009 if (XFASTINT (current_buffer->overlay_center) >= pos)
3010 XSETFASTINT (current_buffer->overlay_center,
3011 XFASTINT (current_buffer->overlay_center) + length);
3012}
3013
3014void
3015adjust_overlays_for_delete (pos, length)
3016 int pos;
3017 int length;
3018{
3019 if (XFASTINT (current_buffer->overlay_center) < pos)
3020 /* The deletion was to our right. No change needed; the before- and
3021 after-lists are still consistent. */
3022 ;
3023 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
3024 /* The deletion was to our left. We need to adjust the center value
3025 to account for the change in position, but the lists are consistent
3026 given the new value. */
3027 XSETFASTINT (current_buffer->overlay_center,
3028 XFASTINT (current_buffer->overlay_center) - length);
3029 else
3030 /* We're right in the middle. There might be things on the after-list
3031 that now belong on the before-list. Recentering will move them,
3032 and also update the center point. */
3033 recenter_overlay_lists (current_buffer, pos);
3034}
3035
2b1bdf65
KH
3036/* Fix up overlays that were garbled as a result of permuting markers
3037 in the range START through END. Any overlay with at least one
3038 endpoint in this range will need to be unlinked from the overlay
3039 list and reinserted in its proper place.
3040 Such an overlay might even have negative size at this point.
3041 If so, we'll reverse the endpoints. Can you think of anything
3042 better to do in this situation? */
3043void
3044fix_overlays_in_range (start, end)
3045 register int start, end;
3046{
6af718a4 3047 Lisp_Object overlay;
2b1bdf65
KH
3048 Lisp_Object before_list, after_list;
3049 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
3050 int startpos, endpos;
3051
3052 /* This algorithm shifts links around instead of consing and GCing.
3053 The loop invariant is that before_list (resp. after_list) is a
3054 well-formed list except that its last element, the one that
3055 *pbefore (resp. *pafter) points to, is still uninitialized.
3056 So it's not a bug that before_list isn't initialized, although
3057 it may look strange. */
3058 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
3059 {
7539e11f 3060 overlay = XCAR (*ptail);
2b1bdf65
KH
3061 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3062 if (endpos < start)
3063 break;
3064 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3065 if (endpos < end
3066 || (startpos >= start && startpos < end))
3067 {
3068 /* If the overlay is backwards, fix that now. */
3069 if (startpos > endpos)
3070 {
3071 int tem;
2c99f3ea
RS
3072 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3073 Qnil);
3074 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3075 Qnil);
2b1bdf65
KH
3076 tem = startpos; startpos = endpos; endpos = tem;
3077 }
3078 /* Add it to the end of the wrong list. Later on,
3079 recenter_overlay_lists will move it to the right place. */
3080 if (endpos < XINT (current_buffer->overlay_center))
3081 {
3082 *pafter = *ptail;
7539e11f 3083 pafter = &XCDR (*ptail);
2b1bdf65
KH
3084 }
3085 else
3086 {
3087 *pbefore = *ptail;
7539e11f 3088 pbefore = &XCDR (*ptail);
2b1bdf65 3089 }
7539e11f 3090 *ptail = XCDR (*ptail);
2b1bdf65
KH
3091 }
3092 else
7539e11f 3093 ptail = &XCDR (*ptail);
2b1bdf65
KH
3094 }
3095 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
3096 {
7539e11f 3097 overlay = XCAR (*ptail);
2b1bdf65
KH
3098 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3099 if (startpos >= end)
3100 break;
3101 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3102 if (startpos >= start
3103 || (endpos >= start && endpos < end))
3104 {
3105 if (startpos > endpos)
3106 {
3107 int tem;
2c99f3ea
RS
3108 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
3109 Qnil);
3110 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
3111 Qnil);
2b1bdf65
KH
3112 tem = startpos; startpos = endpos; endpos = tem;
3113 }
3114 if (endpos < XINT (current_buffer->overlay_center))
3115 {
3116 *pafter = *ptail;
7539e11f 3117 pafter = &XCDR (*ptail);
2b1bdf65
KH
3118 }
3119 else
3120 {
3121 *pbefore = *ptail;
7539e11f 3122 pbefore = &XCDR (*ptail);
2b1bdf65 3123 }
7539e11f 3124 *ptail = XCDR (*ptail);
2b1bdf65
KH
3125 }
3126 else
7539e11f 3127 ptail = &XCDR (*ptail);
2b1bdf65
KH
3128 }
3129
3130 /* Splice the constructed (wrong) lists into the buffer's lists,
3131 and let the recenter function make it sane again. */
3132 *pbefore = current_buffer->overlays_before;
3133 current_buffer->overlays_before = before_list;
3134 recenter_overlay_lists (current_buffer,
3135 XINT (current_buffer->overlay_center));
3136
3137 *pafter = current_buffer->overlays_after;
3138 current_buffer->overlays_after = after_list;
3139 recenter_overlay_lists (current_buffer,
3140 XINT (current_buffer->overlay_center));
3141}
3b06f880
KH
3142
3143/* We have two types of overlay: the one whose ending marker is
3144 after-insertion-marker (this is the usual case) and the one whose
3145 ending marker is before-insertion-marker. When `overlays_before'
3146 contains overlays of the latter type and the former type in this
3147 order and both overlays end at inserting position, inserting a text
3148 increases only the ending marker of the latter type, which results
3149 in incorrect ordering of `overlays_before'.
3150
3151 This function fixes ordering of overlays in the slot
3152 `overlays_before' of the buffer *BP. Before the insertion, `point'
3153 was at PREV, and now is at POS. */
3154
01136e9b 3155void
3b06f880
KH
3156fix_overlays_before (bp, prev, pos)
3157 struct buffer *bp;
3158 int prev, pos;
3159{
3160 Lisp_Object *tailp = &bp->overlays_before;
3161 Lisp_Object *right_place;
3162 int end;
3163
3164 /* After the insertion, the several overlays may be in incorrect
3165 order. The possibility is that, in the list `overlays_before',
3166 an overlay which ends at POS appears after an overlay which ends
3167 at PREV. Since POS is greater than PREV, we must fix the
3168 ordering of these overlays, by moving overlays ends at POS before
3169 the overlays ends at PREV. */
3170
3171 /* At first, find a place where disordered overlays should be linked
3172 in. It is where an overlay which end before POS exists. (i.e. an
3173 overlay whose ending marker is after-insertion-marker if disorder
3174 exists). */
3175 while (!NILP (*tailp)
7539e11f 3176 && ((end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp))))
3b06f880 3177 >= pos))
7539e11f 3178 tailp = &XCDR (*tailp);
3b06f880
KH
3179
3180 /* If we don't find such an overlay,
3181 or the found one ends before PREV,
3182 or the found one is the last one in the list,
3183 we don't have to fix anything. */
3184 if (NILP (*tailp)
3185 || end < prev
7539e11f 3186 || NILP (XCDR (*tailp)))
3b06f880
KH
3187 return;
3188
3189 right_place = tailp;
7539e11f 3190 tailp = &XCDR (*tailp);
3b06f880
KH
3191
3192 /* Now, end position of overlays in the list *TAILP should be before
3193 or equal to PREV. In the loop, an overlay which ends at POS is
3194 moved ahead to the place pointed by RIGHT_PLACE. If we found an
3195 overlay which ends before PREV, the remaining overlays are in
3196 correct order. */
3197 while (!NILP (*tailp))
3198 {
7539e11f 3199 end = OVERLAY_POSITION (OVERLAY_END (XCAR (*tailp)));
3b06f880
KH
3200
3201 if (end == pos)
3202 { /* This overlay is disordered. */
3203 Lisp_Object found = *tailp;
3204
3205 /* Unlink the found overlay. */
7539e11f 3206 *tailp = XCDR (found);
3b06f880 3207 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
7539e11f 3208 XCDR (found) = *right_place;
3b06f880
KH
3209 /* Link it into the right place. */
3210 *right_place = found;
3211 }
3212 else if (end == prev)
7539e11f 3213 tailp = &XCDR (*tailp);
3b06f880
KH
3214 else /* No more disordered overlay. */
3215 break;
3216 }
3217}
2eec3b4e 3218\f
52f8ec73
JB
3219DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3220 "Return t if OBJECT is an overlay.")
3221 (object)
3222 Lisp_Object object;
3223{
3224 return (OVERLAYP (object) ? Qt : Qnil);
3225}
3226
acac2700 3227DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
5c4f68f1
JB
3228 "Create a new overlay with range BEG to END in BUFFER.\n\
3229If omitted, BUFFER defaults to the current buffer.\n\
acac2700
RS
3230BEG and END may be integers or markers.\n\
3231The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
3232front delimiter advance when text is inserted there.\n\
3233The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
3234rear delimiter advance when text is inserted there.")
3235 (beg, end, buffer, front_advance, rear_advance)
5c4f68f1 3236 Lisp_Object beg, end, buffer;
acac2700 3237 Lisp_Object front_advance, rear_advance;
2eec3b4e
RS
3238{
3239 Lisp_Object overlay;
5c4f68f1 3240 struct buffer *b;
2eec3b4e 3241
5c4f68f1 3242 if (NILP (buffer))
67180c6a 3243 XSETBUFFER (buffer, current_buffer);
883047b9
JB
3244 else
3245 CHECK_BUFFER (buffer, 2);
3246 if (MARKERP (beg)
3247 && ! EQ (Fmarker_buffer (beg), buffer))
3248 error ("Marker points into wrong buffer");
3249 if (MARKERP (end)
3250 && ! EQ (Fmarker_buffer (end), buffer))
3251 error ("Marker points into wrong buffer");
2eec3b4e 3252
883047b9
JB
3253 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3254 CHECK_NUMBER_COERCE_MARKER (end, 1);
5c4f68f1 3255
883047b9 3256 if (XINT (beg) > XINT (end))
5c4f68f1 3257 {
c99fc30f
KH
3258 Lisp_Object temp;
3259 temp = beg; beg = end; end = temp;
5c4f68f1 3260 }
883047b9
JB
3261
3262 b = XBUFFER (buffer);
3263
3264 beg = Fset_marker (Fmake_marker (), beg, buffer);
3265 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 3266
acac2700
RS
3267 if (!NILP (front_advance))
3268 XMARKER (beg)->insertion_type = 1;
3269 if (!NILP (rear_advance))
3270 XMARKER (end)->insertion_type = 1;
597dd755 3271
48e2e3ba 3272 overlay = allocate_misc ();
89ca3e1b 3273 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
48e2e3ba
KH
3274 XOVERLAY (overlay)->start = beg;
3275 XOVERLAY (overlay)->end = end;
3276 XOVERLAY (overlay)->plist = Qnil;
2eec3b4e
RS
3277
3278 /* Put the new overlay on the wrong list. */
3279 end = OVERLAY_END (overlay);
5c4f68f1
JB
3280 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3281 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 3282 else
5c4f68f1 3283 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
3284
3285 /* This puts it in the right list, and in the right order. */
5c4f68f1 3286 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 3287
b61982dd
JB
3288 /* We don't need to redisplay the region covered by the overlay, because
3289 the overlay has no properties at the moment. */
3290
2eec3b4e
RS
3291 return overlay;
3292}
876aa27c
RS
3293\f
3294/* Mark a section of BUF as needing redisplay because of overlays changes. */
3295
3296static void
3297modify_overlay (buf, start, end)
3298 struct buffer *buf;
3299 int start, end;
3300{
3301 if (start == end)
3302 return;
3303
3304 if (start > end)
3305 {
3306 int temp = start;
3307 start = end; end = temp;
3308 }
3309
481b5054
GM
3310 BUF_COMPUTE_UNCHANGED (buf, start, end);
3311
876aa27c
RS
3312 /* If this is a buffer not in the selected window,
3313 we must do other windows. */
3314 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3315 windows_or_buffers_changed = 1;
876aa27c
RS
3316 /* If multiple windows show this buffer, we must do other windows. */
3317 else if (buffer_shared > 1)
3318 windows_or_buffers_changed = 1;
876aa27c 3319
d8b9150f 3320 ++BUF_OVERLAY_MODIFF (buf);
876aa27c 3321}
2eec3b4e 3322
876aa27c 3323\f\f
2e34157c
RS
3324Lisp_Object Fdelete_overlay ();
3325
5c4f68f1
JB
3326DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3327 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3ece337a
JB
3328If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3329If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3330buffer.")
5c4f68f1
JB
3331 (overlay, beg, end, buffer)
3332 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 3333{
0a4469c9
RS
3334 struct buffer *b, *ob;
3335 Lisp_Object obuffer;
3336 int count = specpdl_ptr - specpdl;
5c4f68f1 3337
52f8ec73 3338 CHECK_OVERLAY (overlay, 0);
5c4f68f1
JB
3339 if (NILP (buffer))
3340 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 3341 if (NILP (buffer))
67180c6a 3342 XSETBUFFER (buffer, current_buffer);
5c4f68f1 3343 CHECK_BUFFER (buffer, 3);
883047b9
JB
3344
3345 if (MARKERP (beg)
3346 && ! EQ (Fmarker_buffer (beg), buffer))
3347 error ("Marker points into wrong buffer");
3348 if (MARKERP (end)
3349 && ! EQ (Fmarker_buffer (end), buffer))
3350 error ("Marker points into wrong buffer");
3351
b61982dd
JB
3352 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3353 CHECK_NUMBER_COERCE_MARKER (end, 1);
3354
9d7608b7
KH
3355 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3356 return Fdelete_overlay (overlay);
0a4469c9 3357
b61982dd
JB
3358 if (XINT (beg) > XINT (end))
3359 {
c99fc30f
KH
3360 Lisp_Object temp;
3361 temp = beg; beg = end; end = temp;
b61982dd
JB
3362 }
3363
9d7608b7
KH
3364 specbind (Qinhibit_quit, Qt);
3365
0a4469c9 3366 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 3367 b = XBUFFER (buffer);
8801a864 3368 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
2eec3b4e 3369
c82ed728 3370 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 3371 if (!EQ (buffer, obuffer))
50760c4a
RS
3372 {
3373 /* Redisplay where the overlay was. */
3374 if (!NILP (obuffer))
3375 {
2e34157c
RS
3376 int o_beg;
3377 int o_end;
50760c4a 3378
80509f2f
RS
3379 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3380 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
50760c4a 3381
2e34157c 3382 modify_overlay (ob, o_beg, o_end);
50760c4a
RS
3383 }
3384
3385 /* Redisplay where the overlay is going to be. */
876aa27c 3386 modify_overlay (b, XINT (beg), XINT (end));
50760c4a 3387 }
c82ed728
JB
3388 else
3389 /* Redisplay the area the overlay has just left, or just enclosed. */
3390 {
2e34157c 3391 int o_beg, o_end;
c82ed728 3392
80509f2f
RS
3393 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3394 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
c82ed728 3395
2e34157c
RS
3396 if (o_beg == XINT (beg))
3397 modify_overlay (b, o_end, XINT (end));
3398 else if (o_end == XINT (end))
3399 modify_overlay (b, o_beg, XINT (beg));
c82ed728
JB
3400 else
3401 {
2e34157c
RS
3402 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3403 if (XINT (end) > o_end) o_end = XINT (end);
3404 modify_overlay (b, o_beg, o_end);
c82ed728
JB
3405 }
3406 }
b61982dd 3407
0a4469c9
RS
3408 if (!NILP (obuffer))
3409 {
3410 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
3411 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
3412 }
5c4f68f1
JB
3413
3414 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3415 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e
RS
3416
3417 /* Put the overlay on the wrong list. */
3418 end = OVERLAY_END (overlay);
5c4f68f1
JB
3419 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3420 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 3421 else
5c4f68f1 3422 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
3423
3424 /* This puts it in the right list, and in the right order. */
5c4f68f1 3425 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 3426
0a4469c9 3427 return unbind_to (count, overlay);
2eec3b4e
RS
3428}
3429
3430DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
5c4f68f1 3431 "Delete the overlay OVERLAY from its buffer.")
2eec3b4e 3432 (overlay)
5c4f68f1 3433 Lisp_Object overlay;
2eec3b4e 3434{
0a4469c9 3435 Lisp_Object buffer;
5c4f68f1 3436 struct buffer *b;
0a4469c9 3437 int count = specpdl_ptr - specpdl;
5c4f68f1 3438
52f8ec73
JB
3439 CHECK_OVERLAY (overlay, 0);
3440
0a4469c9
RS
3441 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3442 if (NILP (buffer))
3443 return Qnil;
3444
3445 b = XBUFFER (buffer);
3446
3447 specbind (Qinhibit_quit, Qt);
5c4f68f1
JB
3448
3449 b->overlays_before = Fdelq (overlay, b->overlays_before);
3450 b->overlays_after = Fdelq (overlay, b->overlays_after);
3451
876aa27c 3452 modify_overlay (b,
8231a9aa
RS
3453 marker_position (OVERLAY_START (overlay)),
3454 marker_position (OVERLAY_END (overlay)));
b61982dd 3455
3ece337a
JB
3456 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3457 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3458
0a4469c9 3459 return unbind_to (count, Qnil);
2eec3b4e
RS
3460}
3461\f
8ebafa8d
JB
3462/* Overlay dissection functions. */
3463
3464DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3465 "Return the position at which OVERLAY starts.")
3466 (overlay)
3467 Lisp_Object overlay;
3468{
3469 CHECK_OVERLAY (overlay, 0);
3470
3471 return (Fmarker_position (OVERLAY_START (overlay)));
3472}
3473
3474DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3475 "Return the position at which OVERLAY ends.")
3476 (overlay)
3477 Lisp_Object overlay;
3478{
3479 CHECK_OVERLAY (overlay, 0);
3480
3481 return (Fmarker_position (OVERLAY_END (overlay)));
3482}
3483
3484DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3485 "Return the buffer OVERLAY belongs to.")
3486 (overlay)
3487 Lisp_Object overlay;
3488{
3489 CHECK_OVERLAY (overlay, 0);
3490
3491 return Fmarker_buffer (OVERLAY_START (overlay));
3492}
3493
3494DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3495 "Return a list of the properties on OVERLAY.\n\
3496This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3497OVERLAY.")
3498 (overlay)
3499 Lisp_Object overlay;
3500{
3501 CHECK_OVERLAY (overlay, 0);
3502
48e2e3ba 3503 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
3504}
3505
3506\f
2eec3b4e 3507DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
eb8c3be9 3508 "Return a list of the overlays that contain position POS.")
2eec3b4e
RS
3509 (pos)
3510 Lisp_Object pos;
3511{
3512 int noverlays;
2eec3b4e
RS
3513 Lisp_Object *overlay_vec;
3514 int len;
3515 Lisp_Object result;
3516
3517 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3518
3519 len = 10;
3520 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3521
3522 /* Put all the overlays we want in a vector in overlay_vec.
3523 Store the length in len. */
2a77a7d7 3524 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 3525 (int *) 0, (int *) 0, 0);
2eec3b4e
RS
3526
3527 /* Make a list of them all. */
3528 result = Flist (noverlays, overlay_vec);
3529
9ac0d9e0 3530 xfree (overlay_vec);
2eec3b4e
RS
3531 return result;
3532}
3533
74514898 3534DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
2a3eeee7
RS
3535 "Return a list of the overlays that overlap the region BEG ... END.\n\
3536Overlap means that at least one character is contained within the overlay\n\
3537and also contained within the specified region.\n\
3538Empty overlays are included in the result if they are located at BEG\n\
3539or between BEG and END.")
74514898
RS
3540 (beg, end)
3541 Lisp_Object beg, end;
3542{
3543 int noverlays;
3544 Lisp_Object *overlay_vec;
3545 int len;
3546 Lisp_Object result;
3547
3548 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3549 CHECK_NUMBER_COERCE_MARKER (end, 0);
3550
3551 len = 10;
3552 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3553
3554 /* Put all the overlays we want in a vector in overlay_vec.
3555 Store the length in len. */
3556 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3557 (int *) 0, (int *) 0);
3558
3559 /* Make a list of them all. */
3560 result = Flist (noverlays, overlay_vec);
3561
3562 xfree (overlay_vec);
3563 return result;
3564}
3565
2eec3b4e
RS
3566DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3567 1, 1, 0,
bbe20e81
KH
3568 "Return the next position after POS where an overlay starts or ends.\n\
3569If there are no more overlay boundaries after POS, return (point-max).")
2eec3b4e
RS
3570 (pos)
3571 Lisp_Object pos;
3572{
3573 int noverlays;
3574 int endpos;
3575 Lisp_Object *overlay_vec;
3576 int len;
2eec3b4e
RS
3577 int i;
3578
3579 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3580
3581 len = 10;
3582 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3583
3584 /* Put all the overlays we want in a vector in overlay_vec.
3585 Store the length in len.
3586 endpos gets the position where the next overlay starts. */
2a77a7d7 3587 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 3588 &endpos, (int *) 0, 1);
2eec3b4e
RS
3589
3590 /* If any of these overlays ends before endpos,
3591 use its ending point instead. */
3592 for (i = 0; i < noverlays; i++)
3593 {
3594 Lisp_Object oend;
3595 int oendpos;
3596
3597 oend = OVERLAY_END (overlay_vec[i]);
3598 oendpos = OVERLAY_POSITION (oend);
3599 if (oendpos < endpos)
3600 endpos = oendpos;
1ab256cb
RM
3601 }
3602
9ac0d9e0 3603 xfree (overlay_vec);
2eec3b4e
RS
3604 return make_number (endpos);
3605}
239c932b
RS
3606
3607DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3608 Sprevious_overlay_change, 1, 1, 0,
3609 "Return the previous position before POS where an overlay starts or ends.\n\
624bbdc4 3610If there are no more overlay boundaries before POS, return (point-min).")
239c932b
RS
3611 (pos)
3612 Lisp_Object pos;
3613{
3614 int noverlays;
3615 int prevpos;
3616 Lisp_Object *overlay_vec;
3617 int len;
239c932b
RS
3618
3619 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3620
624bbdc4
RS
3621 /* At beginning of buffer, we know the answer;
3622 avoid bug subtracting 1 below. */
3623 if (XINT (pos) == BEGV)
3624 return pos;
3625
017f0539
GM
3626 len = 10;
3627 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3628
239c932b
RS
3629 /* Put all the overlays we want in a vector in overlay_vec.
3630 Store the length in len.
daa1c109 3631 prevpos gets the position of the previous change. */
2a77a7d7 3632 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
ac869cf7 3633 (int *) 0, &prevpos, 1);
239c932b 3634
239c932b
RS
3635 xfree (overlay_vec);
3636 return make_number (prevpos);
3637}
2eec3b4e
RS
3638\f
3639/* These functions are for debugging overlays. */
3640
3641DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3642 "Return a pair of lists giving all the overlays of the current buffer.\n\
3643The car has all the overlays before the overlay center;\n\
bbe20e81 3644the cdr has all the overlays after the overlay center.\n\
2eec3b4e
RS
3645Recentering overlays moves overlays between these lists.\n\
3646The lists you get are copies, so that changing them has no effect.\n\
3647However, the overlays you get are the real objects that the buffer uses.")
3648 ()
3649{
3650 Lisp_Object before, after;
3651 before = current_buffer->overlays_before;
3652 if (CONSP (before))
3653 before = Fcopy_sequence (before);
3654 after = current_buffer->overlays_after;
3655 if (CONSP (after))
3656 after = Fcopy_sequence (after);
3657
3658 return Fcons (before, after);
3659}
3660
3661DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3662 "Recenter the overlays of the current buffer around position POS.")
3663 (pos)
3664 Lisp_Object pos;
3665{
3666 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3667
5c4f68f1 3668 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
3669 return Qnil;
3670}
3671\f
3672DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
a2428fa2 3673 "Get the property of overlay OVERLAY with property name PROP.")
2eec3b4e
RS
3674 (overlay, prop)
3675 Lisp_Object overlay, prop;
3676{
cab4777e 3677 Lisp_Object plist, fallback;
52f8ec73
JB
3678
3679 CHECK_OVERLAY (overlay, 0);
3680
cab4777e
RS
3681 fallback = Qnil;
3682
48e2e3ba 3683 for (plist = XOVERLAY (overlay)->plist;
7539e11f
KR
3684 CONSP (plist) && CONSP (XCDR (plist));
3685 plist = XCDR (XCDR (plist)))
2eec3b4e 3686 {
7539e11f
KR
3687 if (EQ (XCAR (plist), prop))
3688 return XCAR (XCDR (plist));
3689 else if (EQ (XCAR (plist), Qcategory))
cab4777e
RS
3690 {
3691 Lisp_Object tem;
3692 tem = Fcar (Fcdr (plist));
3693 if (SYMBOLP (tem))
3694 fallback = Fget (tem, prop);
3695 }
2eec3b4e 3696 }
52f8ec73 3697
cab4777e 3698 return fallback;
2eec3b4e
RS
3699}
3700
3701DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3702 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3703 (overlay, prop, value)
3704 Lisp_Object overlay, prop, value;
3705{
48e2e3ba 3706 Lisp_Object tail, buffer;
9d7608b7 3707 int changed;
2eec3b4e 3708
52f8ec73 3709 CHECK_OVERLAY (overlay, 0);
b61982dd 3710
274a9425
RS
3711 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3712
48e2e3ba 3713 for (tail = XOVERLAY (overlay)->plist;
7539e11f
KR
3714 CONSP (tail) && CONSP (XCDR (tail));
3715 tail = XCDR (XCDR (tail)))
3716 if (EQ (XCAR (tail), prop))
274a9425 3717 {
7539e11f
KR
3718 changed = !EQ (XCAR (XCDR (tail)), value);
3719 XCAR (XCDR (tail)) = value;
9d7608b7 3720 goto found;
274a9425 3721 }
9d7608b7
KH
3722 /* It wasn't in the list, so add it to the front. */
3723 changed = !NILP (value);
48e2e3ba
KH
3724 XOVERLAY (overlay)->plist
3725 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
9d7608b7
KH
3726 found:
3727 if (! NILP (buffer))
3728 {
3729 if (changed)
876aa27c 3730 modify_overlay (XBUFFER (buffer),
9d7608b7
KH
3731 marker_position (OVERLAY_START (overlay)),
3732 marker_position (OVERLAY_END (overlay)));
3733 if (EQ (prop, Qevaporate) && ! NILP (value)
3734 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3735 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3736 Fdelete_overlay (overlay);
3737 }
2eec3b4e 3738 return value;
1ab256cb
RM
3739}
3740\f
9115729e
KH
3741/* Subroutine of report_overlay_modification. */
3742
3743/* Lisp vector holding overlay hook functions to call.
3744 Vector elements come in pairs.
3745 Each even-index element is a list of hook functions.
3746 The following odd-index element is the overlay they came from.
3747
3748 Before the buffer change, we fill in this vector
3749 as we call overlay hook functions.
3750 After the buffer change, we get the functions to call from this vector.
3751 This way we always call the same functions before and after the change. */
3752static Lisp_Object last_overlay_modification_hooks;
3753
3754/* Number of elements actually used in last_overlay_modification_hooks. */
3755static int last_overlay_modification_hooks_used;
3756
3757/* Add one functionlist/overlay pair
3758 to the end of last_overlay_modification_hooks. */
3759
3760static void
3761add_overlay_mod_hooklist (functionlist, overlay)
3762 Lisp_Object functionlist, overlay;
3763{
3764 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3765
3766 if (last_overlay_modification_hooks_used == oldsize)
3767 {
3768 Lisp_Object old;
3769 old = last_overlay_modification_hooks;
3770 last_overlay_modification_hooks
3771 = Fmake_vector (make_number (oldsize * 2), Qnil);
0b1f1b09
RS
3772 bcopy (XVECTOR (old)->contents,
3773 XVECTOR (last_overlay_modification_hooks)->contents,
9115729e
KH
3774 sizeof (Lisp_Object) * oldsize);
3775 }
3776 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3777 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3778}
3779\f
173f2a64
RS
3780/* Run the modification-hooks of overlays that include
3781 any part of the text in START to END.
9115729e
KH
3782 If this change is an insertion, also
3783 run the insert-before-hooks of overlay starting at END,
930a9140
RS
3784 and the insert-after-hooks of overlay ending at START.
3785
3786 This is called both before and after the modification.
3787 AFTER is nonzero when we call after the modification.
3788
9115729e
KH
3789 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3790 When AFTER is nonzero, they are the start position,
3791 the position after the inserted new text,
3792 and the length of deleted or replaced old text. */
173f2a64
RS
3793
3794void
930a9140 3795report_overlay_modification (start, end, after, arg1, arg2, arg3)
173f2a64 3796 Lisp_Object start, end;
930a9140
RS
3797 int after;
3798 Lisp_Object arg1, arg2, arg3;
173f2a64
RS
3799{
3800 Lisp_Object prop, overlay, tail;
9115729e
KH
3801 /* 1 if this change is an insertion. */
3802 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
55b48893 3803 int tail_copied;
930a9140 3804 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
55b48893
RS
3805
3806 overlay = Qnil;
3807 tail = Qnil;
930a9140 3808 GCPRO5 (overlay, tail, arg1, arg2, arg3);
173f2a64 3809
9115729e
KH
3810 if (after)
3811 {
3812 /* Call the functions recorded in last_overlay_modification_hooks
3813 rather than scanning the overlays again.
3814 First copy the vector contents, in case some of these hooks
3815 do subsequent modification of the buffer. */
3816 int size = last_overlay_modification_hooks_used;
3817 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3818 int i;
3819
3820 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3821 copy, size * sizeof (Lisp_Object));
3822 gcpro1.var = copy;
3823 gcpro1.nvars = size;
3824
3825 for (i = 0; i < size;)
3826 {
3827 Lisp_Object prop, overlay;
3828 prop = copy[i++];
3829 overlay = copy[i++];
3830 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3831 }
3832 UNGCPRO;
3833 return;
3834 }
3835
3836 /* We are being called before a change.
3837 Scan the overlays to find the functions to call. */
3838 last_overlay_modification_hooks_used = 0;
55b48893 3839 tail_copied = 0;
173f2a64
RS
3840 for (tail = current_buffer->overlays_before;
3841 CONSP (tail);
7539e11f 3842 tail = XCDR (tail))
173f2a64
RS
3843 {
3844 int startpos, endpos;
be8b1c6b 3845 Lisp_Object ostart, oend;
173f2a64 3846
7539e11f 3847 overlay = XCAR (tail);
173f2a64
RS
3848
3849 ostart = OVERLAY_START (overlay);
3850 oend = OVERLAY_END (overlay);
3851 endpos = OVERLAY_POSITION (oend);
3852 if (XFASTINT (start) > endpos)
3853 break;
3854 startpos = OVERLAY_POSITION (ostart);
9115729e
KH
3855 if (insertion && (XFASTINT (start) == startpos
3856 || XFASTINT (end) == startpos))
173f2a64
RS
3857 {
3858 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
3859 if (!NILP (prop))
3860 {
3861 /* Copy TAIL in case the hook recenters the overlay lists. */
55b48893
RS
3862 if (!tail_copied)
3863 tail = Fcopy_sequence (tail);
3864 tail_copied = 1;
930a9140 3865 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3866 }
173f2a64 3867 }
9115729e
KH
3868 if (insertion && (XFASTINT (start) == endpos
3869 || XFASTINT (end) == endpos))
173f2a64
RS
3870 {
3871 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
3872 if (!NILP (prop))
3873 {
55b48893
RS
3874 if (!tail_copied)
3875 tail = Fcopy_sequence (tail);
3876 tail_copied = 1;
930a9140 3877 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3878 }
173f2a64 3879 }
3bd13e92
KH
3880 /* Test for intersecting intervals. This does the right thing
3881 for both insertion and deletion. */
3882 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
3883 {
3884 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
3885 if (!NILP (prop))
3886 {
55b48893
RS
3887 if (!tail_copied)
3888 tail = Fcopy_sequence (tail);
3889 tail_copied = 1;
930a9140 3890 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3891 }
173f2a64
RS
3892 }
3893 }
3894
55b48893 3895 tail_copied = 0;
173f2a64
RS
3896 for (tail = current_buffer->overlays_after;
3897 CONSP (tail);
7539e11f 3898 tail = XCDR (tail))
173f2a64
RS
3899 {
3900 int startpos, endpos;
be8b1c6b 3901 Lisp_Object ostart, oend;
173f2a64 3902
7539e11f 3903 overlay = XCAR (tail);
173f2a64
RS
3904
3905 ostart = OVERLAY_START (overlay);
3906 oend = OVERLAY_END (overlay);
3907 startpos = OVERLAY_POSITION (ostart);
cdf0b096 3908 endpos = OVERLAY_POSITION (oend);
173f2a64
RS
3909 if (XFASTINT (end) < startpos)
3910 break;
9115729e
KH
3911 if (insertion && (XFASTINT (start) == startpos
3912 || XFASTINT (end) == startpos))
173f2a64
RS
3913 {
3914 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
3915 if (!NILP (prop))
3916 {
55b48893
RS
3917 if (!tail_copied)
3918 tail = Fcopy_sequence (tail);
3919 tail_copied = 1;
930a9140 3920 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3921 }
173f2a64 3922 }
9115729e
KH
3923 if (insertion && (XFASTINT (start) == endpos
3924 || XFASTINT (end) == endpos))
173f2a64
RS
3925 {
3926 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
3927 if (!NILP (prop))
3928 {
55b48893
RS
3929 if (!tail_copied)
3930 tail = Fcopy_sequence (tail);
3931 tail_copied = 1;
930a9140 3932 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3933 }
173f2a64 3934 }
3bd13e92
KH
3935 /* Test for intersecting intervals. This does the right thing
3936 for both insertion and deletion. */
3937 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
3938 {
3939 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
3940 if (!NILP (prop))
3941 {
55b48893
RS
3942 if (!tail_copied)
3943 tail = Fcopy_sequence (tail);
3944 tail_copied = 1;
930a9140 3945 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3946 }
173f2a64
RS
3947 }
3948 }
55b48893
RS
3949
3950 UNGCPRO;
173f2a64
RS
3951}
3952
3953static void
930a9140
RS
3954call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3955 Lisp_Object list, overlay;
3956 int after;
3957 Lisp_Object arg1, arg2, arg3;
173f2a64 3958{
930a9140 3959 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9115729e 3960
930a9140 3961 GCPRO4 (list, arg1, arg2, arg3);
9115729e
KH
3962 if (! after)
3963 add_overlay_mod_hooklist (list, overlay);
3964
173f2a64
RS
3965 while (!NILP (list))
3966 {
930a9140
RS
3967 if (NILP (arg3))
3968 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3969 else
3970 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
173f2a64
RS
3971 list = Fcdr (list);
3972 }
3973 UNGCPRO;
3974}
9d7608b7
KH
3975
3976/* Delete any zero-sized overlays at position POS, if the `evaporate'
3977 property is set. */
3978void
3979evaporate_overlays (pos)
3980 int pos;
3981{
3982 Lisp_Object tail, overlay, hit_list;
3983
3984 hit_list = Qnil;
3985 if (pos <= XFASTINT (current_buffer->overlay_center))
3986 for (tail = current_buffer->overlays_before; CONSP (tail);
7539e11f 3987 tail = XCDR (tail))
9d7608b7
KH
3988 {
3989 int endpos;
7539e11f 3990 overlay = XCAR (tail);
9d7608b7
KH
3991 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3992 if (endpos < pos)
3993 break;
3994 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
c3935f9d 3995 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
3996 hit_list = Fcons (overlay, hit_list);
3997 }
3998 else
3999 for (tail = current_buffer->overlays_after; CONSP (tail);
7539e11f 4000 tail = XCDR (tail))
9d7608b7
KH
4001 {
4002 int startpos;
7539e11f 4003 overlay = XCAR (tail);
9d7608b7
KH
4004 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4005 if (startpos > pos)
4006 break;
4007 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
c3935f9d 4008 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4009 hit_list = Fcons (overlay, hit_list);
4010 }
7539e11f
KR
4011 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4012 Fdelete_overlay (XCAR (hit_list));
9d7608b7 4013}
173f2a64 4014\f
54dfdeb0 4015/* Somebody has tried to store a value with an unacceptable type
1bf08baf
KH
4016 in the slot with offset OFFSET. */
4017
0fa3ba92 4018void
54dfdeb0
KH
4019buffer_slot_type_mismatch (offset)
4020 int offset;
0fa3ba92 4021{
54dfdeb0 4022 Lisp_Object sym;
0fa3ba92 4023 char *type_name;
7c02e886 4024
7313acd0 4025 switch (XINT (PER_BUFFER_TYPE (offset)))
0fa3ba92 4026 {
7c02e886
GM
4027 case Lisp_Int:
4028 type_name = "integers";
4029 break;
4030
4031 case Lisp_String:
4032 type_name = "strings";
4033 break;
4034
4035 case Lisp_Symbol:
4036 type_name = "symbols";
4037 break;
4038
0fa3ba92
JB
4039 default:
4040 abort ();
4041 }
4042
7313acd0 4043 sym = PER_BUFFER_SYMBOL (offset);
1bf08baf 4044 error ("Only %s should be stored in the buffer-local variable %s",
54dfdeb0 4045 type_name, XSYMBOL (sym)->name->data);
0fa3ba92 4046}
7c02e886 4047
0fa3ba92 4048\f
dfcf069d 4049void
1ab256cb
RM
4050init_buffer_once ()
4051{
7c02e886
GM
4052 int idx;
4053
4054 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
13de9290 4055
1ab256cb
RM
4056 /* Make sure all markable slots in buffer_defaults
4057 are initialized reasonably, so mark_buffer won't choke. */
4058 reset_buffer (&buffer_defaults);
13de9290 4059 reset_buffer_local_variables (&buffer_defaults, 1);
1ab256cb 4060 reset_buffer (&buffer_local_symbols);
13de9290 4061 reset_buffer_local_variables (&buffer_local_symbols, 1);
336cd056
RS
4062 /* Prevent GC from getting confused. */
4063 buffer_defaults.text = &buffer_defaults.own_text;
4064 buffer_local_symbols.text = &buffer_local_symbols.own_text;
336cd056
RS
4065 BUF_INTERVALS (&buffer_defaults) = 0;
4066 BUF_INTERVALS (&buffer_local_symbols) = 0;
67180c6a
KH
4067 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
4068 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
4069
4070 /* Set up the default values of various buffer slots. */
4071 /* Must do these before making the first buffer! */
4072
f532dca0 4073 /* real setup is done in bindings.el */
1ab256cb 4074 buffer_defaults.mode_line_format = build_string ("%-");
045dee35 4075 buffer_defaults.header_line_format = Qnil;
1ab256cb
RM
4076 buffer_defaults.abbrev_mode = Qnil;
4077 buffer_defaults.overwrite_mode = Qnil;
4078 buffer_defaults.case_fold_search = Qt;
4079 buffer_defaults.auto_fill_function = Qnil;
4080 buffer_defaults.selective_display = Qnil;
4081#ifndef old
4082 buffer_defaults.selective_display_ellipses = Qt;
4083#endif
4084 buffer_defaults.abbrev_table = Qnil;
4085 buffer_defaults.display_table = Qnil;
1ab256cb 4086 buffer_defaults.undo_list = Qnil;
c48f61ef 4087 buffer_defaults.mark_active = Qnil;
be9aafdd 4088 buffer_defaults.file_format = Qnil;
2eec3b4e
RS
4089 buffer_defaults.overlays_before = Qnil;
4090 buffer_defaults.overlays_after = Qnil;
bbbe9545 4091 XSETFASTINT (buffer_defaults.overlay_center, BEG);
1ab256cb 4092
8d7a4592 4093 XSETFASTINT (buffer_defaults.tab_width, 8);
1ab256cb
RM
4094 buffer_defaults.truncate_lines = Qnil;
4095 buffer_defaults.ctl_arrow = Qt;
3b06f880 4096 buffer_defaults.direction_reversed = Qnil;
bb2ec976 4097 buffer_defaults.cursor_type = Qt;
a3bbced0 4098 buffer_defaults.extra_line_spacing = Qnil;
1ab256cb 4099
f7975d07 4100#ifdef DOS_NT
0776cb1b 4101 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
54ad07d3 4102#endif
a1a17b61 4103 buffer_defaults.enable_multibyte_characters = Qt;
c71b5d9b 4104 buffer_defaults.buffer_file_coding_system = Qnil;
8d7a4592
KH
4105 XSETFASTINT (buffer_defaults.fill_column, 70);
4106 XSETFASTINT (buffer_defaults.left_margin, 0);
28e969dd 4107 buffer_defaults.cache_long_line_scans = Qnil;
f6ed2e84 4108 buffer_defaults.file_truename = Qnil;
7962a441 4109 XSETFASTINT (buffer_defaults.display_count, 0);
0552666b
GM
4110 buffer_defaults.indicate_empty_lines = Qnil;
4111 buffer_defaults.scroll_up_aggressively = Qnil;
4112 buffer_defaults.scroll_down_aggressively = Qnil;
3fd364db 4113 buffer_defaults.display_time = Qnil;
1ab256cb
RM
4114
4115 /* Assign the local-flags to the slots that have default values.
4116 The local flag is a bit that is used in the buffer
4117 to say that it has its own local value for the slot.
4118 The local flag bits are in the local_var_flags slot of the buffer. */
4119
4120 /* Nothing can work if this isn't true */
4d2f1389 4121 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
1ab256cb
RM
4122
4123 /* 0 means not a lisp var, -1 means always local, else mask */
4124 bzero (&buffer_local_flags, sizeof buffer_local_flags);
aab80822
KH
4125 XSETINT (buffer_local_flags.filename, -1);
4126 XSETINT (buffer_local_flags.directory, -1);
4127 XSETINT (buffer_local_flags.backed_up, -1);
4128 XSETINT (buffer_local_flags.save_length, -1);
4129 XSETINT (buffer_local_flags.auto_save_file_name, -1);
4130 XSETINT (buffer_local_flags.read_only, -1);
4131 XSETINT (buffer_local_flags.major_mode, -1);
4132 XSETINT (buffer_local_flags.mode_name, -1);
4133 XSETINT (buffer_local_flags.undo_list, -1);
4134 XSETINT (buffer_local_flags.mark_active, -1);
943e065b 4135 XSETINT (buffer_local_flags.point_before_scroll, -1);
f6ed2e84 4136 XSETINT (buffer_local_flags.file_truename, -1);
3cb719bd 4137 XSETINT (buffer_local_flags.invisibility_spec, -1);
55ac8536 4138 XSETINT (buffer_local_flags.file_format, -1);
7962a441 4139 XSETINT (buffer_local_flags.display_count, -1);
3fd364db 4140 XSETINT (buffer_local_flags.display_time, -1);
1bf08baf 4141 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
8d7a4592 4142
7c02e886
GM
4143 idx = 1;
4144 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
4145 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
4146 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
4147 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
4148 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
4149 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
1ab256cb 4150#ifndef old
7c02e886 4151 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
1ab256cb 4152#endif
7c02e886
GM
4153 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
4154 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
4155 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
4156 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
4157 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
4158 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
4159 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
f7975d07 4160#ifdef DOS_NT
7c02e886 4161 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
13de9290 4162 /* Make this one a permanent local. */
7c02e886 4163 buffer_permanent_local_flags[idx++] = 1;
54ad07d3 4164#endif
7c02e886
GM
4165 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
4166 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
4167 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
4168 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
4169 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
a1a17b61 4170 /* Make this one a permanent local. */
7c02e886
GM
4171 buffer_permanent_local_flags[idx++] = 1;
4172 XSETFASTINT (buffer_local_flags.left_margin_width, idx); ++idx;
4173 XSETFASTINT (buffer_local_flags.right_margin_width, idx); ++idx;
4174 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
4175 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
4176 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
4177 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
bd96bd79 4178 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
a3bbced0 4179 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
7c02e886
GM
4180
4181 /* Need more room? */
7313acd0 4182 if (idx >= MAX_PER_BUFFER_VARS)
7c02e886 4183 abort ();
7313acd0 4184 last_per_buffer_idx = idx;
c71b5d9b 4185
1ab256cb
RM
4186 Vbuffer_alist = Qnil;
4187 current_buffer = 0;
4188 all_buffers = 0;
4189
4190 QSFundamental = build_string ("Fundamental");
4191
4192 Qfundamental_mode = intern ("fundamental-mode");
4193 buffer_defaults.major_mode = Qfundamental_mode;
4194
4195 Qmode_class = intern ("mode-class");
4196
4197 Qprotected_field = intern ("protected-field");
4198
4199 Qpermanent_local = intern ("permanent-local");
4200
4201 Qkill_buffer_hook = intern ("kill-buffer-hook");
4202
4203 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
000f8083 4204
1ab256cb
RM
4205 /* super-magic invisible buffer */
4206 Vbuffer_alist = Qnil;
4207
ffd56f97 4208 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
7775635d
KH
4209
4210 inhibit_modification_hooks = 0;
1ab256cb
RM
4211}
4212
dfcf069d 4213void
1ab256cb
RM
4214init_buffer ()
4215{
4216 char buf[MAXPATHLEN+1];
2381d133
JB
4217 char *pwd;
4218 struct stat dotstat, pwdstat;
136351b7 4219 Lisp_Object temp;
f7975d07 4220 int rc;
1ab256cb
RM
4221
4222 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3d871c85
RS
4223 if (NILP (buffer_defaults.enable_multibyte_characters))
4224 Fset_buffer_multibyte (Qnil);
2381d133
JB
4225
4226 /* If PWD is accurate, use it instead of calling getwd. This is faster
4227 when PWD is right, and may avoid a fatal error. */
309f2a6e
RS
4228 if ((pwd = getenv ("PWD")) != 0
4229 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
2381d133
JB
4230 && stat (pwd, &pwdstat) == 0
4231 && stat (".", &dotstat) == 0
4232 && dotstat.st_ino == pwdstat.st_ino
4233 && dotstat.st_dev == pwdstat.st_dev
4234 && strlen (pwd) < MAXPATHLEN)
4235 strcpy (buf, pwd);
6335beb0
RS
4236#ifdef HAVE_GETCWD
4237 else if (getcwd (buf, MAXPATHLEN+1) == 0)
9dde47f5 4238 fatal ("`getcwd' failed: %s\n", strerror (errno));
6335beb0 4239#else
2381d133 4240 else if (getwd (buf) == 0)
cf1e6391 4241 fatal ("`getwd' failed: %s\n", buf);
6335beb0 4242#endif
1ab256cb
RM
4243
4244#ifndef VMS
4245 /* Maybe this should really use some standard subroutine
4246 whose definition is filename syntax dependent. */
f7975d07
RS
4247 rc = strlen (buf);
4248 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
4249 {
4250 buf[rc] = DIRECTORY_SEP;
4251 buf[rc + 1] = '\0';
4252 }
1ab256cb 4253#endif /* not VMS */
0995fa35 4254
1ab256cb 4255 current_buffer->directory = build_string (buf);
136351b7 4256
0995fa35
RS
4257 /* Add /: to the front of the name
4258 if it would otherwise be treated as magic. */
4259 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
81ab2e07
KH
4260 if (! NILP (temp)
4261 /* If the default dir is just /, TEMP is non-nil
4262 because of the ange-ftp completion handler.
4263 However, it is not necessary to turn / into /:/.
4264 So avoid doing that. */
4265 && strcmp ("/", XSTRING (current_buffer->directory)->data))
0995fa35
RS
4266 current_buffer->directory
4267 = concat2 (build_string ("/:"), current_buffer->directory);
4268
136351b7
RS
4269 temp = get_minibuffer (0);
4270 XBUFFER (temp)->directory = current_buffer->directory;
1ab256cb
RM
4271}
4272
4273/* initialize the buffer routines */
dfcf069d 4274void
1ab256cb
RM
4275syms_of_buffer ()
4276{
9115729e
KH
4277 staticpro (&last_overlay_modification_hooks);
4278 last_overlay_modification_hooks
4279 = Fmake_vector (make_number (10), Qnil);
4280
1ab256cb
RM
4281 staticpro (&Vbuffer_defaults);
4282 staticpro (&Vbuffer_local_symbols);
4283 staticpro (&Qfundamental_mode);
4284 staticpro (&Qmode_class);
4285 staticpro (&QSFundamental);
4286 staticpro (&Vbuffer_alist);
4287 staticpro (&Qprotected_field);
4288 staticpro (&Qpermanent_local);
4289 staticpro (&Qkill_buffer_hook);
22378665 4290 Qoverlayp = intern ("overlayp");
52f8ec73 4291 staticpro (&Qoverlayp);
9d7608b7
KH
4292 Qevaporate = intern ("evaporate");
4293 staticpro (&Qevaporate);
294d215f 4294 Qmodification_hooks = intern ("modification-hooks");
22378665 4295 staticpro (&Qmodification_hooks);
294d215f 4296 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
22378665 4297 staticpro (&Qinsert_in_front_hooks);
294d215f 4298 Qinsert_behind_hooks = intern ("insert-behind-hooks");
22378665 4299 staticpro (&Qinsert_behind_hooks);
5fe0b67e 4300 Qget_file_buffer = intern ("get-file-buffer");
22378665 4301 staticpro (&Qget_file_buffer);
5985d248
KH
4302 Qpriority = intern ("priority");
4303 staticpro (&Qpriority);
4304 Qwindow = intern ("window");
4305 staticpro (&Qwindow);
bbbe9545
KH
4306 Qbefore_string = intern ("before-string");
4307 staticpro (&Qbefore_string);
4308 Qafter_string = intern ("after-string");
4309 staticpro (&Qafter_string);
22378665
RS
4310 Qfirst_change_hook = intern ("first-change-hook");
4311 staticpro (&Qfirst_change_hook);
4312 Qbefore_change_functions = intern ("before-change-functions");
4313 staticpro (&Qbefore_change_functions);
4314 Qafter_change_functions = intern ("after-change-functions");
4315 staticpro (&Qafter_change_functions);
1ab256cb
RM
4316
4317 Fput (Qprotected_field, Qerror_conditions,
4318 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
4319 Fput (Qprotected_field, Qerror_message,
4320 build_string ("Attempt to modify a protected field"));
4321
4322 /* All these use DEFVAR_LISP_NOPRO because the slots in
4323 buffer_defaults will all be marked via Vbuffer_defaults. */
4324
4325 DEFVAR_LISP_NOPRO ("default-mode-line-format",
4326 &buffer_defaults.mode_line_format,
4327 "Default value of `mode-line-format' for buffers that don't override it.\n\
4328This is the same as (default-value 'mode-line-format).");
4329
045dee35
GM
4330 DEFVAR_LISP_NOPRO ("default-header-line-format",
4331 &buffer_defaults.header_line_format,
4332 "Default value of `header-line-format' for buffers that don't override it.\n\
4333This is the same as (default-value 'header-line-format).");
0552666b 4334
bd96bd79
GM
4335 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
4336 "Default value of `cursor-type' for buffers that don't override it.\n\
4337This is the same as (default-value 'cursor-type).");
4338
a3bbced0
GM
4339 DEFVAR_LISP_NOPRO ("default-line-spacing",
4340 &buffer_defaults.extra_line_spacing,
4341 "Default value of `line-spacing' for buffers that don't override it.\n\
4342This is the same as (default-value 'line-spacing).");
4343
1ab256cb
RM
4344 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
4345 &buffer_defaults.abbrev_mode,
4346 "Default value of `abbrev-mode' for buffers that do not override it.\n\
4347This is the same as (default-value 'abbrev-mode).");
4348
4349 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
4350 &buffer_defaults.ctl_arrow,
4351 "Default value of `ctl-arrow' for buffers that do not override it.\n\
4352This is the same as (default-value 'ctl-arrow).");
4353
3b06f880
KH
4354 DEFVAR_LISP_NOPRO ("default-direction-reversed",
4355 &buffer_defaults.direction_reversed,
4356 "Default value of `direction_reversed' for buffers that do not override it.\n\
4357 This is the same as (default-value 'direction-reversed).");
4358
a1a17b61
KH
4359 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
4360 &buffer_defaults.enable_multibyte_characters,
ed8300d9 4361 "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
53fe786d 4362This is the same as (default-value 'enable-multibyte-characters).");
a1a17b61 4363
c71b5d9b
KH
4364 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
4365 &buffer_defaults.buffer_file_coding_system,
4366 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
53fe786d 4367This is the same as (default-value 'buffer-file-coding-system).");
c71b5d9b 4368
1ab256cb
RM
4369 DEFVAR_LISP_NOPRO ("default-truncate-lines",
4370 &buffer_defaults.truncate_lines,
4371 "Default value of `truncate-lines' for buffers that do not override it.\n\
4372This is the same as (default-value 'truncate-lines).");
4373
4374 DEFVAR_LISP_NOPRO ("default-fill-column",
4375 &buffer_defaults.fill_column,
4376 "Default value of `fill-column' for buffers that do not override it.\n\
4377This is the same as (default-value 'fill-column).");
4378
4379 DEFVAR_LISP_NOPRO ("default-left-margin",
4380 &buffer_defaults.left_margin,
4381 "Default value of `left-margin' for buffers that do not override it.\n\
4382This is the same as (default-value 'left-margin).");
4383
4384 DEFVAR_LISP_NOPRO ("default-tab-width",
4385 &buffer_defaults.tab_width,
4386 "Default value of `tab-width' for buffers that do not override it.\n\
4387This is the same as (default-value 'tab-width).");
4388
4389 DEFVAR_LISP_NOPRO ("default-case-fold-search",
4390 &buffer_defaults.case_fold_search,
4391 "Default value of `case-fold-search' for buffers that don't override it.\n\
4392This is the same as (default-value 'case-fold-search).");
4393
f7975d07 4394#ifdef DOS_NT
54ad07d3
RS
4395 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
4396 &buffer_defaults.buffer_file_type,
4397 "Default file type for buffers that do not override it.\n\
4398This is the same as (default-value 'buffer-file-type).\n\
4399The file type is nil for text, t for binary.");
4400#endif
4401
0552666b
GM
4402 DEFVAR_LISP_NOPRO ("default-left-margin-width",
4403 &buffer_defaults.left_margin_width,
4404 "Default value of `left-margin-width' for buffers that don't override it.\n\
4405This is the same as (default-value 'left-margin-width).");
4406
4407 DEFVAR_LISP_NOPRO ("default-right-margin-width",
4408 &buffer_defaults.right_margin_width,
4409 "Default value of `right_margin_width' for buffers that don't override it.\n\
4410This is the same as (default-value 'right-margin-width).");
4411
4412 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
4413 &buffer_defaults.indicate_empty_lines,
4414 "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
4415This is the same as (default-value 'indicate-empty-lines).");
4416
4417 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
4418 &buffer_defaults.scroll_up_aggressively,
4419 "Default value of `scroll-up-aggressively' for buffers that\n\
4420don't override it. This is the same as (default-value\n\
4421'scroll-up-aggressively).");
4422
4423 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
4424 &buffer_defaults.scroll_down_aggressively,
4425 "Default value of `scroll-down-aggressively' for buffers that\n\
4426don't override it. This is the same as (default-value\n\
4427'scroll-down-aggressively).");
4428
045dee35
GM
4429 DEFVAR_PER_BUFFER ("header-line-format",
4430 &current_buffer->header_line_format,
0552666b
GM
4431 Qnil,
4432 "Analogous to `mode-line-format', but for a mode line displayed\n\
4433at the top of windows.");
4434
0fa3ba92
JB
4435 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
4436 Qnil, 0);
1ab256cb
RM
4437
4438/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
4439 But make-docfile finds it!
4440 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
bec44fd6 4441 Qnil,
1ab256cb
RM
4442 "Template for displaying mode line for current buffer.\n\
4443Each buffer has its own value of this variable.\n\
0552666b
GM
4444Value may be nil, a string, a symbol or a list or cons cell.\n\
4445A value of nil means don't display a mode line.\n\
1ab256cb
RM
4446For a symbol, its value is used (but it is ignored if t or nil).\n\
4447 A string appearing directly as the value of a symbol is processed verbatim\n\
4448 in that the %-constructs below are not recognized.\n\
fe512f27
GM
4449For a list of the form `(:eval FORM)', FORM is evaluated and the result\n\
4450 is used as a mode line element.\n\
1ab256cb
RM
4451For a list whose car is a symbol, the symbol's value is taken,\n\
4452 and if that is non-nil, the cadr of the list is processed recursively.\n\
4453 Otherwise, the caddr of the list (if there is one) is processed.\n\
4454For a list whose car is a string or list, each element is processed\n\
4455 recursively and the results are effectively concatenated.\n\
4456For a list whose car is an integer, the cdr of the list is processed\n\
4457 and padded (if the number is positive) or truncated (if negative)\n\
4458 to the width specified by that number.\n\
4459A string is printed verbatim in the mode line except for %-constructs:\n\
4460 (%-constructs are allowed when the string is the entire mode-line-format\n\
4461 or when it is found in a cons-cell or a list)\n\
4462 %b -- print buffer name. %f -- print visited file name.\n\
5d516f2d 4463 %F -- print frame name.\n\
c2ff34f7 4464 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
08de6200 4465 %& is like %*, but ignore read-only-ness.\n\
c2ff34f7
RS
4466 % means buffer is read-only and * means it is modified.\n\
4467 For a modified read-only buffer, %* gives % and %+ gives *.\n\
a97c374a 4468 %s -- print process status. %l -- print the current line number.\n\
07924294 4469 %c -- print the current column number (this makes editing slower).\n\
6a567ad8
RS
4470 To make the column number update correctly in all cases,\n\
4471 `column-number-mode' must be non-nil.\n\
dd24e6a6 4472 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
9d130ffc 4473 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
dd24e6a6 4474 or print Bottom or All.\n\
08de6200 4475 %m -- print the mode name.\n\
1ab256cb 4476 %n -- print Narrow if appropriate.\n\
08de6200
EZ
4477 %z -- print mnemonics of buffer, terminal, and keyboard coding systems.\n\
4478 %Z -- like %z, but including the end-of-line format.\n\
1ab256cb
RM
4479 %[ -- print one [ for each recursive editing level. %] similar.\n\
4480 %% -- print %. %- -- print infinitely many dashes.\n\
4481Decimal digits after the % specify field width to which to pad.");
4482*/
4483
4484 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
4485 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
4486nil here means use current buffer's major mode.");
4487
4488 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
0fa3ba92 4489 make_number (Lisp_Symbol),
1ab256cb
RM
4490 "Symbol for current buffer's major mode.");
4491
4492 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
0fa3ba92 4493 make_number (Lisp_String),
1ab256cb
RM
4494 "Pretty name of current buffer's major mode (a string).");
4495
0fa3ba92 4496 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
1ab256cb
RM
4497 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
4498Automatically becomes buffer-local when set in any fashion.");
4499
4500 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
0fa3ba92 4501 Qnil,
5ed62574 4502 "*Non-nil if searches and matches should ignore case.\n\
1ab256cb
RM
4503Automatically becomes buffer-local when set in any fashion.");
4504
4505 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
0fa3ba92 4506 make_number (Lisp_Int),
1ab256cb
RM
4507 "*Column beyond which automatic line-wrapping should happen.\n\
4508Automatically becomes buffer-local when set in any fashion.");
4509
4510 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
0fa3ba92 4511 make_number (Lisp_Int),
1ab256cb
RM
4512 "*Column for the default indent-line-function to indent to.\n\
4513Linefeed indents to this column in Fundamental mode.\n\
4514Automatically becomes buffer-local when set in any fashion.");
4515
4516 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
0fa3ba92 4517 make_number (Lisp_Int),
1ab256cb
RM
4518 "*Distance between tab stops (for display of tab characters), in columns.\n\
4519Automatically becomes buffer-local when set in any fashion.");
4520
0fa3ba92 4521 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
1ab256cb 4522 "*Non-nil means display control chars with uparrow.\n\
6a99d31d 4523A value of nil means use backslash and octal digits.\n\
1ab256cb
RM
4524Automatically becomes buffer-local when set in any fashion.\n\
4525This variable does not apply to characters whose display is specified\n\
4526in the current display table (if there is one).");
4527
3b06f880 4528 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
1bf08baf
KH
4529 &current_buffer->enable_multibyte_characters,
4530 make_number (-1),
36bb26f7 4531 "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
ed218f73 4532Otherwise they are regarded as unibyte. This affects the display,\n\
53fe786d
RS
4533file I/O and the behavior of various editing commands.\n\
4534\n\
4535This variable is buffer-local but you cannot set it directly;\n\
4536use the function `set-buffer-multibyte' to change a buffer's representation.\n\
4537Changing its default value with `setq-default' is supported.\n\
4538See also variable `default-enable-multibyte-characters' and Info node\n\
4539`(elisp)Text Representations'.");
3b06f880 4540
c71b5d9b
KH
4541 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
4542 &current_buffer->buffer_file_coding_system, Qnil,
4543 "Coding system to be used for encoding the buffer contents on saving.\n\
940daec1
RS
4544This variable applies to saving the buffer, and also to `write-region'\n\
4545and other functions that use `write-region'.\n\
4546It does not apply to sending output to subprocesses, however.\n\
4547\n\
4548If this is nil, the buffer is saved without any code conversion\n\
4549unless some coding system is specified in `file-coding-system-alist'\n\
c71b5d9b
KH
4550for the buffer file.\n\
4551\n\
940daec1
RS
4552The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
4553\n\
c71b5d9b
KH
4554This variable is never applied to a way of decoding\n\
4555a file while reading it.");
4556
3b06f880
KH
4557 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
4558 Qnil,
4559 "*Non-nil means lines in the buffer are displayed right to left.");
4560
0fa3ba92 4561 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
1ab256cb
RM
4562 "*Non-nil means do not display continuation lines;\n\
4563give each line of text one screen line.\n\
4564Automatically becomes buffer-local when set in any fashion.\n\
4565\n\
4566Note that this is overridden by the variable\n\
4567`truncate-partial-width-windows' if that variable is non-nil\n\
502b9b64 4568and this buffer is not full-frame width.");
1ab256cb 4569
f7975d07 4570#ifdef DOS_NT
54ad07d3
RS
4571 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
4572 Qnil,
006d3d34
RS
4573 "Non-nil if the visited file is a binary file.\n\
4574This variable is meaningful on MS-DOG and Windows NT.\n\
4575On those systems, it is automatically local in every buffer.\n\
e0585c64 4576On other systems, this variable is normally always nil.");
54ad07d3
RS
4577#endif
4578
1ab256cb 4579 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
0fa3ba92 4580 make_number (Lisp_String),
1ab256cb
RM
4581 "Name of default directory of current buffer. Should end with slash.\n\
4582Each buffer has its own value of this variable.");
4583
4584 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
0fa3ba92 4585 Qnil,
1ab256cb 4586 "Function called (if non-nil) to perform auto-fill.\n\
139f13a7 4587It is called after self-inserting any character specified in\n\
6c9a7472 4588the `auto-fill-chars' table.\n\
1ab256cb 4589Each buffer has its own value of this variable.\n\
54158e68
KH
4590NOTE: This variable is not a hook;\n\
4591its value may not be a list of functions.");
1ab256cb
RM
4592
4593 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
0fa3ba92 4594 make_number (Lisp_String),
1ab256cb
RM
4595 "Name of file visited in current buffer, or nil if not visiting a file.\n\
4596Each buffer has its own value of this variable.");
4597
f6ed2e84
RS
4598 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
4599 make_number (Lisp_String),
bb4c204e 4600 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
b1c03e64
RS
4601The truename of a file is calculated by `file-truename'\n\
4602and then abbreviated with `abbreviate-file-name'.\n\
f6ed2e84
RS
4603Each buffer has its own value of this variable.");
4604
1ab256cb 4605 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 4606 &current_buffer->auto_save_file_name,
0fa3ba92 4607 make_number (Lisp_String),
1ab256cb
RM
4608 "Name of file for auto-saving current buffer,\n\
4609or nil if buffer should not be auto-saved.\n\
4610Each buffer has its own value of this variable.");
4611
0fa3ba92 4612 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
1ab256cb
RM
4613 "Non-nil if this buffer is read-only.\n\
4614Each buffer has its own value of this variable.");
4615
0fa3ba92 4616 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
1ab256cb
RM
4617 "Non-nil if this buffer's file has been backed up.\n\
4618Backing up is done before the first time the file is saved.\n\
4619Each buffer has its own value of this variable.");
4620
4621 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
0fa3ba92 4622 make_number (Lisp_Int),
1ab256cb
RM
4623 "Length of current buffer when last read in, saved or auto-saved.\n\
46240 initially.\n\
4625Each buffer has its own value of this variable.");
4626
4627 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
0fa3ba92 4628 Qnil,
1ab256cb
RM
4629 "Non-nil enables selective display:\n\
4630Integer N as value means display only lines\n\
4631 that start with less than n columns of space.\n\
4632A value of t means, after a ^M, all the rest of the line is invisible.\n\
4633 Then ^M's in the file are written into files as newlines.\n\n\
4634Automatically becomes buffer-local when set in any fashion.");
4635
4636#ifndef old
4637 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4638 &current_buffer->selective_display_ellipses,
0fa3ba92 4639 Qnil,
1ab256cb
RM
4640 "t means display ... on previous line when a line is invisible.\n\
4641Automatically becomes buffer-local when set in any fashion.");
4642#endif
4643
0fa3ba92 4644 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
1ab256cb 4645 "Non-nil if self-insertion should replace existing text.\n\
5e05d0a5
RS
4646The value should be one of `overwrite-mode-textual',\n\
4647`overwrite-mode-binary', or nil.\n\
4648If it is `overwrite-mode-textual', self-insertion still\n\
6bbb0d4a 4649inserts at the end of a line, and inserts when point is before a tab,\n\
2e94b813 4650until the tab is filled in.\n\
6bbb0d4a 4651If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
1ab256cb
RM
4652Automatically becomes buffer-local when set in any fashion.");
4653
54939090
RS
4654#if 0 /* The doc string is too long for some compilers,
4655 but make-docfile can find it in this comment. */
1ab256cb 4656 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5d305367 4657 Qnil,
1ab256cb
RM
4658 "Display table that controls display of the contents of current buffer.\n\
4659Automatically becomes buffer-local when set in any fashion.\n\
6fdc249f
EN
4660The display table is a char-table created with `make-display-table'.\n\
4661The ordinary char-table elements control how to display each possible text\n\
4662character. Each value should be a vector of characters or nil;\n\
1ab256cb 4663nil means display the character in the default fashion.\n\
6fdc249f
EN
4664There are six extra slots to control the display of\n\
4665 the end of a truncated screen line (extra-slot 0, a single character);\n\
4666 the end of a continued line (extra-slot 1, a single character);\n\
6158b3b0 4667 the escape character used to display character codes in octal\n\
6fdc249f
EN
4668 (extra-slot 2, a single character);\n\
4669 the character used as an arrow for control characters (extra-slot 3,\n\
6158b3b0 4670 a single character);\n\
6fdc249f 4671 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
a45e35e1
JB
4672 a vector of characters);\n\
4673 the character used to draw the border between side-by-side windows\n\
6fdc249f
EN
4674 (extra-slot 5, a single character).\n\
4675See also the functions `display-table-slot' and `set-display-table-slot'.\n\
1ab256cb
RM
4676If this variable is nil, the value of `standard-display-table' is used.\n\
4677Each window can have its own, overriding display table.");
54939090
RS
4678#endif
4679 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
de15914a 4680 Qnil, 0);
1ab256cb 4681
0552666b
GM
4682 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_width,
4683 Qnil,
4684 "*Width of left marginal area for display of a buffer.\n\
4685Automatically becomes buffer-local when set in any fashion.\n\
4686A value of nil means no marginal area.");
4687
4688 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_width,
4689 Qnil,
4690 "*Width of right marginal area for display of a buffer.\n\
4691Automatically becomes buffer-local when set in any fashion.\n\
4692A value of nil means no marginal area.");
4693
4694 DEFVAR_PER_BUFFER ("indicate-empty-lines",
4695 &current_buffer->indicate_empty_lines, Qnil,
9d1ee976
GM
4696 "*Visually indicate empty lines after the buffer end.\n\
4697If non-nil, a bitmap is displayed in the left fringe of a window on\n\
4698window-systems.\n\
0552666b
GM
4699Automatically becomes buffer-local when set in any fashion.\n");
4700
4701 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
4702 &current_buffer->scroll_up_aggressively, Qnil,
4703 "*If a number, scroll display up aggressively.\n\
4704If scrolling a window because point is above the window start, choose\n\
4705a new window start so that point ends up that fraction of the window's\n\
4706height from the bottom of the window.\n\
4707Automatically becomes buffer-local when set in any fashion.");
4708
4709 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
4710 &current_buffer->scroll_down_aggressively, Qnil,
4711 "*If a number, scroll display down aggressively.\n\
4712If scrolling a window because point is below the window end, choose\n\
4713a new window start so that point ends up that fraction of the window's\n\
4714height from the top of the window.\n\
4715Automatically becomes buffer-local when set in any fashion.");
4716
1ab256cb
RM
4717/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4718 "Don't ask.");
4719*/
1ab256cb 4720
5f079267
RS
4721 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
4722 "List of functions to call before each text change.\n\
4723Two arguments are passed to each function: the positions of\n\
4724the beginning and end of the range of old text to be changed.\n\
4725\(For an insertion, the beginning and end are at the same place.)\n\
4726No information is given about the length of the text after the change.\n\
5f079267
RS
4727\n\
4728Buffer changes made while executing the `before-change-functions'\n\
b86344d0
RS
4729don't call any before-change or after-change functions.\n\
4730That's because these variables are temporarily set to nil.\n\
4731As a result, a hook function cannot straightforwardly alter the value of\n\
4732these variables. See the Emacs Lisp manual for a way of\n\
9ebc22d4
RS
4733accomplishing an equivalent result by using other variables.\n\
4734\n\
4735If an unhandled error happens in running these functions,\n\
4736the variable's value remains nil. That prevents the error\n\
4737from happening repeatedly and making Emacs nonfunctional.");
5f079267
RS
4738 Vbefore_change_functions = Qnil;
4739
4740 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
4741 "List of function to call after each text change.\n\
4742Three arguments are passed to each function: the positions of\n\
4743the beginning and end of the range of changed text,\n\
839dd834 4744and the length in bytes of the pre-change text replaced by that range.\n\
5f079267 4745\(For an insertion, the pre-change length is zero;\n\
839dd834 4746for a deletion, that length is the number of bytes deleted,\n\
5f079267
RS
4747and the post-change beginning and end are at the same place.)\n\
4748\n\
4749Buffer changes made while executing the `after-change-functions'\n\
b86344d0
RS
4750don't call any before-change or after-change functions.\n\
4751That's because these variables are temporarily set to nil.\n\
4752As a result, a hook function cannot straightforwardly alter the value of\n\
4753these variables. See the Emacs Lisp manual for a way of\n\
9ebc22d4
RS
4754accomplishing an equivalent result by using other variables.\n\
4755\n\
4756If an unhandled error happens in running these functions,\n\
4757the variable's value remains nil. That prevents the error\n\
4758from happening repeatedly and making Emacs nonfunctional.");
5f079267
RS
4759 Vafter_change_functions = Qnil;
4760
dbc4e1c1
JB
4761 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
4762 "A list of functions to call before changing a buffer which is unmodified.\n\
4763The functions are run using the `run-hooks' function.");
4764 Vfirst_change_hook = Qnil;
1ab256cb 4765
54939090
RS
4766#if 0 /* The doc string is too long for some compilers,
4767 but make-docfile can find it in this comment. */
3f5fcd47 4768 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
1ab256cb 4769 "List of undo entries in current buffer.\n\
3fd364db 4770This variable is always local in all buffers.\n\
1ab256cb
RM
4771Recent changes come first; older changes follow newer.\n\
4772\n\
630f4018
KH
4773An entry (BEG . END) represents an insertion which begins at\n\
4774position BEG and ends at position END.\n\
1ab256cb
RM
4775\n\
4776An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4777from (abs POSITION). If POSITION is positive, point was at the front\n\
4778of the text being deleted; if negative, point was at the end.\n\
4779\n\
6c0df54a
RS
4780An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4781\"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4782of the visited file's modification time, as of that time. If the\n\
4783modification time of the most recent save is different, this entry is\n\
1ab256cb
RM
4784obsolete.\n\
4785\n\
6c0df54a
RS
4786An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4787was modified between BEG and END. PROPERTY is the property name,\n\
4788and VALUE is the old value.\n\
483c1fd3 4789\n\
da1c183c
RS
4790An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4791was adjusted in position by the offset DISTANCE (an integer).\n\
4792\n\
bec44fd6
JB
4793An entry of the form POSITION indicates that point was at the buffer\n\
4794location given by the integer. Undoing an entry of this form places\n\
4795point at POSITION.\n\
4796\n\
1ab256cb
RM
4797nil marks undo boundaries. The undo command treats the changes\n\
4798between two undo boundaries as a single step to be undone.\n\
4799\n\
bec44fd6 4800If the value of the variable is t, undo information is not recorded.");
54939090
RS
4801#endif
4802 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
de15914a 4803 0);
1ab256cb 4804
c48f61ef
RS
4805 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
4806 "Non-nil means the mark and region are currently active in this buffer.\n\
4807Automatically local in all buffers.");
4808
28e969dd 4809 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
f0c5b712
JB
4810 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4811This variable is buffer-local, in all buffers.\n\
28e969dd 4812\n\
f0c5b712
JB
4813Normally, the line-motion functions work by scanning the buffer for\n\
4814newlines. Columnar operations (like move-to-column and\n\
4815compute-motion) also work by scanning the buffer, summing character\n\
4816widths as they go. This works well for ordinary text, but if the\n\
28e969dd 4817buffer's lines are very long (say, more than 500 characters), these\n\
f0c5b712
JB
4818motion functions will take longer to execute. Emacs may also take\n\
4819longer to update the display.\n\
28e969dd 4820\n\
f0c5b712
JB
4821If cache-long-line-scans is non-nil, these motion functions cache the\n\
4822results of their scans, and consult the cache to avoid rescanning\n\
4823regions of the buffer until the text is modified. The caches are most\n\
4824beneficial when they prevent the most searching---that is, when the\n\
4825buffer contains long lines and large regions of characters with the\n\
4826same, fixed screen width.\n\
28e969dd 4827\n\
f0c5b712
JB
4828When cache-long-line-scans is non-nil, processing short lines will\n\
4829become slightly slower (because of the overhead of consulting the\n\
4830cache), and the caches will use memory roughly proportional to the\n\
4831number of newlines and characters whose screen width varies.\n\
4832\n\
4833The caches require no explicit maintenance; their accuracy is\n\
4834maintained internally by the Emacs primitives. Enabling or disabling\n\
4835the cache should not affect the behavior of any of the motion\n\
4836functions; it should only affect their performance.");
28e969dd 4837
943e065b 4838 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
3fd364db
RS
4839 "Value of point before the last series of scroll operations, or nil.\n\
4840This variable is always local in all buffers.");
943e065b 4841
be9aafdd
BG
4842 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4843 "List of formats to use when saving this buffer.\n\
3fd364db 4844This variable is always local in all buffers.\n\
be9aafdd
BG
4845Formats are defined by `format-alist'. This variable is\n\
4846set when a file is visited. Automatically local in all buffers.");
4847
3cb719bd
RS
4848 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4849 &current_buffer->invisibility_spec, Qnil,
4850 "Invisibility spec of this buffer.\n\
3fd364db 4851This variable is always local in all buffers.\n\
3cb719bd
RS
4852The default is t, which means that text is invisible\n\
4853if it has a non-nil `invisible' property.\n\
4854If the value is a list, a text character is invisible if its `invisible'\n\
4855property is an element in that list.\n\
554216ad
KH
4856If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4857then characters with property value PROP are invisible,\n\
3cb719bd
RS
4858and they have an ellipsis as well if ELLIPSIS is non-nil.");
4859
7962a441
RS
4860 DEFVAR_PER_BUFFER ("buffer-display-count",
4861 &current_buffer->display_count, Qnil,
3fd364db
RS
4862 "A number incremented each time this buffer is displayed in a window.\n\
4863This variable is always local in all buffers.\n\
4864The function `set-window-buffer increments it.");
4865
4866 DEFVAR_PER_BUFFER ("buffer-display-time",
4867 &current_buffer->display_time, Qnil,
4868 "Time stamp updated each time this buffer is displayed in a window.\n\
4869This variable is always local in all buffers.\n\
4870The function `set-window-buffer' updates this variable\n\
4871to the value obtained by calling `current-time'.\n\
4872If the buffer has never been shown in a window, the value is nil.");
7962a441 4873
c48f61ef 4874 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
319c537c
RS
4875 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4876Non-nil also enables highlighting of the region whenever the mark is active.\n\
4877The variable `highlight-nonselected-windows' controls whether to highlight\n\
4878all windows or just the selected window.");
c48f61ef
RS
4879 Vtransient_mark_mode = Qnil;
4880
0a4469c9 4881 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
a96b68f1
RS
4882 "*Non-nil means disregard read-only status of buffers or characters.\n\
4883If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4884text properties. If the value is a list, disregard `buffer-read-only'\n\
4885and disregard a `read-only' text property if the property value\n\
4886is a member of the list.");
4887 Vinhibit_read_only = Qnil;
4888
bb2ec976
GM
4889 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
4890 "Cursor to use in window displaying this buffer.\n\
4891Values are interpreted as follows:\n\
4892\n\
4893 t use the cursor specified for the frame\n\
4894 nil don't display a cursor\n\
4895 `bar' display a bar cursor with default width\n\
4896 (bar . WIDTH) display a bar cursor with width WIDTH\n\
4897 others display a box cursor.");
4898
a3bbced0
GM
4899 DEFVAR_PER_BUFFER ("line-spacing",
4900 &current_buffer->extra_line_spacing, Qnil,
4901 "Additional space to put between lines when displaying a buffer.\n\
4902The space is measured in pixels, and put below lines on window systems.");
4903
dcdffbf6
RS
4904 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4905 "List of functions called with no args to query before killing a buffer.");
4906 Vkill_buffer_query_functions = Qnil;
4907
0dc88e60 4908 defsubr (&Sbuffer_live_p);
1ab256cb
RM
4909 defsubr (&Sbuffer_list);
4910 defsubr (&Sget_buffer);
4911 defsubr (&Sget_file_buffer);
4912 defsubr (&Sget_buffer_create);
336cd056 4913 defsubr (&Smake_indirect_buffer);
01050cb5 4914 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
4915 defsubr (&Sbuffer_name);
4916/*defsubr (&Sbuffer_number);*/
4917 defsubr (&Sbuffer_file_name);
336cd056 4918 defsubr (&Sbuffer_base_buffer);
1ab256cb
RM
4919 defsubr (&Sbuffer_local_variables);
4920 defsubr (&Sbuffer_modified_p);
4921 defsubr (&Sset_buffer_modified_p);
4922 defsubr (&Sbuffer_modified_tick);
4923 defsubr (&Srename_buffer);
4924 defsubr (&Sother_buffer);
4925 defsubr (&Sbuffer_disable_undo);
4926 defsubr (&Sbuffer_enable_undo);
4927 defsubr (&Skill_buffer);
a9ee7a59 4928 defsubr (&Sset_buffer_major_mode);
1ab256cb
RM
4929 defsubr (&Sswitch_to_buffer);
4930 defsubr (&Spop_to_buffer);
4931 defsubr (&Scurrent_buffer);
4932 defsubr (&Sset_buffer);
4933 defsubr (&Sbarf_if_buffer_read_only);
4934 defsubr (&Sbury_buffer);
3ac81adb
RS
4935 defsubr (&Serase_buffer);
4936 defsubr (&Sset_buffer_multibyte);
1ab256cb 4937 defsubr (&Skill_all_local_variables);
2eec3b4e 4938
52f8ec73 4939 defsubr (&Soverlayp);
2eec3b4e
RS
4940 defsubr (&Smake_overlay);
4941 defsubr (&Sdelete_overlay);
4942 defsubr (&Smove_overlay);
8ebafa8d
JB
4943 defsubr (&Soverlay_start);
4944 defsubr (&Soverlay_end);
4945 defsubr (&Soverlay_buffer);
4946 defsubr (&Soverlay_properties);
2eec3b4e 4947 defsubr (&Soverlays_at);
74514898 4948 defsubr (&Soverlays_in);
2eec3b4e 4949 defsubr (&Snext_overlay_change);
239c932b 4950 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
4951 defsubr (&Soverlay_recenter);
4952 defsubr (&Soverlay_lists);
4953 defsubr (&Soverlay_get);
4954 defsubr (&Soverlay_put);
a8c21b48 4955 defsubr (&Srestore_buffer_modified_p);
1ab256cb
RM
4956}
4957
dfcf069d 4958void
1ab256cb
RM
4959keys_of_buffer ()
4960{
4961 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4962 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
4963
4964 /* This must not be in syms_of_buffer, because Qdisabled is not
4965 initialized when that function gets called. */
4966 Fput (intern ("erase-buffer"), Qdisabled, Qt);
1ab256cb 4967}