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