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