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