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