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