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