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