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