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