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