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