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