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