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