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