(Fvertical_motion): Use position reported by iterator
[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 }
126f1fc1
SM
2272 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2273 So since we just swapped the markers between the two buffers, we need
2274 to undo the effect of this swap for window markers. */
2275 Lisp_Object w = Fselected_window (), ws = Qnil;
2276 Lisp_Object buf1, buf2;
2277 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2278
2279 while (NILP (Fmemq (w, ws)))
2280 {
2281 ws = Fcons (w, ws);
2282 if (MARKERP (XWINDOW (w)->pointm)
2283 && (EQ (XWINDOW (w)->buffer, buf1)
2284 || EQ (XWINDOW (w)->buffer, buf2)))
2285 Fset_marker (XWINDOW (w)->pointm,
2286 make_number (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2287 XWINDOW (w)->buffer);
2288 w = Fnext_window (w, Qt, Qt);
2289 }
2290 }
2291
13cda5f9
SM
2292 if (current_buffer->text->intervals)
2293 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2294 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2295 if (other_buffer->text->intervals)
2296 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2297 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2298
2299 return Qnil;
2300}
2301
3ac81adb
RS
2302DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2303 1, 1, 0,
7ee72033 2304 doc: /* Set the multibyte flag of the current buffer to FLAG.
018ba359
PJ
2305If FLAG is t, this makes the buffer a multibyte buffer.
2306If FLAG is nil, this makes the buffer a single-byte buffer.
8f924df7
KH
2307In these cases, the buffer contents remain unchanged as a sequence of
2308bytes but the contents viewed as characters do change.
2309If FLAG is `to', this makes the buffer a multibyte buffer by changing
6b61353c
KH
2310all eight-bit bytes to eight-bit characters.
2311If the multibyte flag was really changed, undo information of the
2312current buffer is cleared. */)
7ee72033 2313 (flag)
3ac81adb
RS
2314 Lisp_Object flag;
2315{
65745fad 2316 struct Lisp_Marker *tail, *markers;
abc9d959 2317 struct buffer *other;
458c8af4 2318 int begv, zv;
8d1203ea 2319 int narrowed = (BEG != BEGV || Z != ZV);
ed00559d 2320 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
38babc07
KS
2321 Lisp_Object old_undo = current_buffer->undo_list;
2322 struct gcpro gcpro1;
3ac81adb 2323
6e553d5e
RS
2324 if (current_buffer->base_buffer)
2325 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2326
70e77119
AS
2327 /* Do nothing if nothing actually changes. */
2328 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2329 return flag;
2330
38babc07
KS
2331 GCPRO1 (old_undo);
2332
2333 /* Don't record these buffer changes. We will put a special undo entry
2334 instead. */
2335 current_buffer->undo_list = Qt;
b05525fa 2336
3ac81adb
RS
2337 /* If the cached position is for this buffer, clear it out. */
2338 clear_charpos_cache (current_buffer);
2339
458c8af4
KH
2340 if (NILP (flag))
2341 begv = BEGV_BYTE, zv = ZV_BYTE;
2342 else
2343 begv = BEGV, zv = ZV;
2344
a9bcded1
KH
2345 if (narrowed)
2346 Fwiden ();
2347
3ac81adb
RS
2348 if (NILP (flag))
2349 {
a9bcded1
KH
2350 int pos, stop;
2351 unsigned char *p;
2352
3ac81adb
RS
2353 /* Do this first, so it can use CHAR_TO_BYTE
2354 to calculate the old correspondences. */
2355 set_intervals_multibyte (0);
2356
2357 current_buffer->enable_multibyte_characters = Qnil;
2358
2359 Z = Z_BYTE;
2360 BEGV = BEGV_BYTE;
2361 ZV = ZV_BYTE;
2362 GPT = GPT_BYTE;
2363 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2364
60ebfdf3 2365
65745fad
SM
2366 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2367 tail->charpos = tail->bytepos;
a9bcded1
KH
2368
2369 /* Convert multibyte form of 8-bit characters to unibyte. */
2370 pos = BEG;
2371 stop = GPT;
2372 p = BEG_ADDR;
2373 while (1)
2374 {
2375 int c, bytes;
2376
2377 if (pos == stop)
2378 {
2379 if (pos == Z)
2380 break;
2381 p = GAP_END_ADDR;
2382 stop = Z;
2383 }
8f348ed5
KH
2384 if (ASCII_BYTE_P (*p))
2385 p++, pos++;
2386 else if (CHAR_BYTE8_HEAD_P (*p))
a9bcded1 2387 {
31285a8f 2388 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
a9bcded1
KH
2389 /* Delete all bytes for this 8-bit character but the
2390 last one, and change the last one to the charcter
2391 code. */
2392 bytes--;
2393 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2394 p = GAP_END_ADDR;
2395 *p++ = c;
2396 pos++;
2397 if (begv > pos)
2398 begv -= bytes;
2399 if (zv > pos)
2400 zv -= bytes;
2401 stop = Z;
2402 }
8f924df7 2403 else
8f348ed5
KH
2404 {
2405 bytes = BYTES_BY_CHAR_HEAD (*p);
2406 p += bytes, pos += bytes;
2407 }
a9bcded1
KH
2408 }
2409 if (narrowed)
2410 Fnarrow_to_region (make_number (begv), make_number (zv));
3ac81adb
RS
2411 }
2412 else
2413 {
a9bcded1
KH
2414 int pt = PT;
2415 int pos, stop;
8f348ed5 2416 unsigned char *p, *pend;
a9bcded1 2417
673c57d2 2418 /* Be sure not to have a multibyte sequence striding over the GAP.
8f348ed5
KH
2419 Ex: We change this: "...abc\302 _GAP_ \241def..."
2420 to: "...abc _GAP_ \302\241def..." */
673c57d2 2421
8f924df7 2422 if (EQ (flag, Qt)
a3a303df 2423 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
673c57d2
KH
2424 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2425 {
2426 unsigned char *p = GPT_ADDR - 1;
2427
2428 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2429 if (BASE_LEADING_CODE_P (*p))
2430 {
2431 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2432
2433 move_gap_both (new_gpt, new_gpt);
2434 }
2435 }
2436
a9bcded1
KH
2437 /* Make the buffer contents valid as multibyte by converting
2438 8-bit characters to multibyte form. */
2439 pos = BEG;
2440 stop = GPT;
2441 p = BEG_ADDR;
8f348ed5 2442 pend = GPT_ADDR;
a9bcded1
KH
2443 while (1)
2444 {
2445 int bytes;
2446
2447 if (pos == stop)
2448 {
2449 if (pos == Z)
2450 break;
2451 p = GAP_END_ADDR;
8f348ed5 2452 pend = Z_ADDR;
a9bcded1
KH
2453 stop = Z;
2454 }
177c0ea7 2455
a3a303df
KH
2456 if (ASCII_BYTE_P (*p))
2457 p++, pos++;
8f924df7 2458 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
a9bcded1
KH
2459 p += bytes, pos += bytes;
2460 else
2461 {
2462 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
a3a303df 2463 int c;
a9bcded1 2464
8f924df7 2465 c = BYTE8_TO_CHAR (*p);
8c2fc311 2466 bytes = CHAR_STRING (c, tmp);
a9bcded1
KH
2467 *p = tmp[0];
2468 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2469 bytes--;
2470 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2471 /* Now the gap is after the just inserted data. */
2472 pos = GPT;
2473 p = GAP_END_ADDR;
2474 if (pos <= begv)
2475 begv += bytes;
2476 if (pos <= zv)
2477 zv += bytes;
2478 if (pos <= pt)
2479 pt += bytes;
31285a8f 2480 pend = Z_ADDR;
a9bcded1
KH
2481 stop = Z;
2482 }
2483 }
2484
2485 if (pt != PT)
2486 TEMP_SET_PT (pt);
2487
2488 if (narrowed)
2489 Fnarrow_to_region (make_number (begv), make_number (zv));
2490
3ac81adb
RS
2491 /* Do this first, so that chars_in_text asks the right question.
2492 set_intervals_multibyte needs it too. */
2493 current_buffer->enable_multibyte_characters = Qt;
2494
b05525fa 2495 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
3ac81adb 2496 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
b05525fa 2497
673c57d2 2498 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
b05525fa
RS
2499
2500 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
3ac81adb 2501 if (BEGV_BYTE > GPT_BYTE)
673c57d2 2502 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2503 else
2504 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2505
2506 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
3ac81adb 2507 if (ZV_BYTE > GPT_BYTE)
673c57d2 2508 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2509 else
2510 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2511
2512 {
2513 int pt_byte = advance_to_char_boundary (PT_BYTE);
2514 int pt;
2515
2516 if (pt_byte > GPT_BYTE)
673c57d2 2517 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
b05525fa
RS
2518 else
2519 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2520 TEMP_SET_PT_BOTH (pt, pt_byte);
2521 }
3ac81adb
RS
2522
2523 tail = markers = BUF_MARKERS (current_buffer);
95fb069b
RS
2524
2525 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2526 getting confused by the markers that have not yet been updated.
2527 It is also a signal that it should never create a marker. */
65745fad 2528 BUF_MARKERS (current_buffer) = NULL;
3ac81adb 2529
65745fad 2530 for (; tail; tail = tail->next)
3ac81adb 2531 {
65745fad
SM
2532 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2533 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
3ac81adb 2534 }
b69f9797
RS
2535
2536 /* Make sure no markers were put on the chain
2537 while the chain value was incorrect. */
65745fad 2538 if (BUF_MARKERS (current_buffer))
b69f9797
RS
2539 abort ();
2540
3ac81adb
RS
2541 BUF_MARKERS (current_buffer) = markers;
2542
2543 /* Do this last, so it can calculate the new correspondences
2544 between chars and bytes. */
2545 set_intervals_multibyte (1);
2546 }
2547
38babc07
KS
2548 if (!EQ (old_undo, Qt))
2549 {
2550 /* Represent all the above changes by a special undo entry. */
2551 extern Lisp_Object Qapply;
8929fd87
KS
2552 current_buffer->undo_list = Fcons (list3 (Qapply,
2553 intern ("set-buffer-multibyte"),
2554 NILP (flag) ? Qt : Qnil),
2555 old_undo);
38babc07
KS
2556 }
2557
2558 UNGCPRO;
a9bcded1 2559
724b203f
GM
2560 /* Changing the multibyteness of a buffer means that all windows
2561 showing that buffer must be updated thoroughly. */
2562 current_buffer->prevent_redisplay_optimizations_p = 1;
2563 ++windows_or_buffers_changed;
2564
abc9d959
RS
2565 /* Copy this buffer's new multibyte status
2566 into all of its indirect buffers. */
2567 for (other = all_buffers; other; other = other->next)
2568 if (other->base_buffer == current_buffer && !NILP (other->name))
724b203f
GM
2569 {
2570 other->enable_multibyte_characters
2571 = current_buffer->enable_multibyte_characters;
2572 other->prevent_redisplay_optimizations_p = 1;
2573 }
abc9d959 2574
ed00559d
KH
2575 /* Restore the modifiedness of the buffer. */
2576 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2577 Fset_buffer_modified_p (Qnil);
2578
2a2b50a9 2579#ifdef subprocesses
172f9454
KH
2580 /* Update coding systems of this buffer's process (if any). */
2581 {
2582 Lisp_Object process;
2583
2584 process = Fget_buffer_process (Fcurrent_buffer ());
2585 if (PROCESSP (process))
2586 setup_process_coding_systems (process);
2587 }
2a2b50a9 2588#endif /* subprocesses */
172f9454 2589
3ac81adb
RS
2590 return flag;
2591}
2592\f
1ab256cb 2593DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
efc7e75f 2594 0, 0, 0,
7ee72033 2595 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
018ba359
PJ
2596Most local variable bindings are eliminated so that the default values
2597become effective once more. Also, the syntax table is set from
2598`standard-syntax-table', the local keymap is set to nil,
2599and the abbrev table from `fundamental-mode-abbrev-table'.
2600This function also forces redisplay of the mode line.
2601
2602Every function to select a new major mode starts by
2603calling this function.
2604
2605As a special exception, local variables whose names have
2606a non-nil `permanent-local' property are not eliminated by this function.
2607
2608The first thing this function does is run
7ee72033
MB
2609the normal hook `change-major-mode-hook'. */)
2610 ()
1ab256cb 2611{
fd186f07 2612 if (!NILP (Vrun_hooks))
43ed3b8d 2613 call1 (Vrun_hooks, Qchange_major_mode_hook);
1ab256cb 2614
3709505e 2615 /* Make sure none of the bindings in local_var_alist
2f3f993b 2616 remain swapped in, in their symbols. */
1ab256cb 2617
2f3f993b 2618 swap_out_buffer_local_variables (current_buffer);
1ab256cb
RM
2619
2620 /* Actually eliminate all local bindings of this buffer. */
2621
13de9290 2622 reset_buffer_local_variables (current_buffer, 0);
1ab256cb 2623
1ab256cb
RM
2624 /* Force mode-line redisplay. Useful here because all major mode
2625 commands call this function. */
2626 update_mode_lines++;
2627
2628 return Qnil;
2629}
2f3f993b
RS
2630
2631/* Make sure no local variables remain set up with buffer B
2632 for their current values. */
2633
2634static void
2635swap_out_buffer_local_variables (b)
2636 struct buffer *b;
2637{
2638 Lisp_Object oalist, alist, sym, tem, buffer;
2639
2640 XSETBUFFER (buffer, b);
2641 oalist = b->local_var_alist;
2642
67ee9f6e 2643 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2f3f993b 2644 {
7539e11f 2645 sym = XCAR (XCAR (alist));
2f3f993b
RS
2646
2647 /* Need not do anything if some other buffer's binding is now encached. */
f5c1dd0d 2648 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
e7c10f83 2649 if (EQ (tem, buffer))
2f3f993b 2650 {
e7c10f83
SM
2651 /* Symbol is set up for this buffer's old local value:
2652 swap it out! */
2653 swap_in_global_binding (sym);
2f3f993b
RS
2654 }
2655 }
2656}
1ab256cb 2657\f
2eec3b4e 2658/* Find all the overlays in the current buffer that contain position POS.
177c0ea7 2659 Return the number found, and store them in a vector in *VEC_PTR.
2eec3b4e 2660 Store in *LEN_PTR the size allocated for the vector.
52f8ec73 2661 Store in *NEXT_PTR the next position after POS where an overlay starts,
624d2678 2662 or ZV if there are no more overlays between POS and ZV.
bbbe9545 2663 Store in *PREV_PTR the previous position before POS where an overlay ends,
413e06a4 2664 or where an overlay starts which ends at or after POS;
624d2678 2665 or BEGV if there are no such overlays from BEGV to POS.
239c932b 2666 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
2667
2668 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
2669 when this function is called.
2670
2671 If EXTEND is non-zero, we make the vector bigger if necessary.
2672 If EXTEND is zero, we never extend the vector,
2673 and we store only as many overlays as will fit.
ac869cf7
MB
2674 But we still return the total number of overlays.
2675
2676 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2677 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2678 default (BEGV or ZV). */
2eec3b4e
RS
2679
2680int
ac869cf7 2681overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
c2d5b10f 2682 EMACS_INT pos;
61d54cd5 2683 int extend;
2eec3b4e
RS
2684 Lisp_Object **vec_ptr;
2685 int *len_ptr;
0f8b27ea
SM
2686 EMACS_INT *next_ptr;
2687 EMACS_INT *prev_ptr;
ac869cf7 2688 int change_req;
1ab256cb 2689{
2410d73a
SM
2690 Lisp_Object overlay, start, end;
2691 struct Lisp_Overlay *tail;
2eec3b4e
RS
2692 int idx = 0;
2693 int len = *len_ptr;
2694 Lisp_Object *vec = *vec_ptr;
2695 int next = ZV;
239c932b 2696 int prev = BEGV;
61d54cd5
RS
2697 int inhibit_storing = 0;
2698
2410d73a 2699 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2eec3b4e 2700 {
239c932b 2701 int startpos, endpos;
52f8ec73 2702
2410d73a 2703 XSETMISC (overlay, tail);
1ab256cb 2704
2eec3b4e
RS
2705 start = OVERLAY_START (overlay);
2706 end = OVERLAY_END (overlay);
239c932b
RS
2707 endpos = OVERLAY_POSITION (end);
2708 if (endpos < pos)
2709 {
2710 if (prev < endpos)
2711 prev = endpos;
2712 break;
2713 }
413e06a4
RS
2714 startpos = OVERLAY_POSITION (start);
2715 /* This one ends at or after POS
daa1c109 2716 so its start counts for PREV_PTR if it's before POS. */
413e06a4
RS
2717 if (prev < startpos && startpos < pos)
2718 prev = startpos;
239c932b
RS
2719 if (endpos == pos)
2720 continue;
2eec3b4e
RS
2721 if (startpos <= pos)
2722 {
2723 if (idx == len)
2724 {
61d54cd5
RS
2725 /* The supplied vector is full.
2726 Either make it bigger, or don't store any more in it. */
2727 if (extend)
2728 {
0552666b
GM
2729 /* Make it work with an initial len == 0. */
2730 len *= 2;
2731 if (len == 0)
2732 len = 4;
2733 *len_ptr = len;
61d54cd5
RS
2734 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2735 *vec_ptr = vec;
2736 }
2737 else
2738 inhibit_storing = 1;
2eec3b4e 2739 }
61d54cd5
RS
2740
2741 if (!inhibit_storing)
2742 vec[idx] = overlay;
2743 /* Keep counting overlays even if we can't return them all. */
2744 idx++;
2eec3b4e
RS
2745 }
2746 else if (startpos < next)
2747 next = startpos;
2748 }
2749
2410d73a 2750 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
1ab256cb 2751 {
239c932b 2752 int startpos, endpos;
52f8ec73 2753
2410d73a 2754 XSETMISC (overlay, tail);
2eec3b4e
RS
2755
2756 start = OVERLAY_START (overlay);
2757 end = OVERLAY_END (overlay);
2758 startpos = OVERLAY_POSITION (start);
52f8ec73 2759 if (pos < startpos)
2eec3b4e
RS
2760 {
2761 if (startpos < next)
2762 next = startpos;
2763 break;
2764 }
239c932b
RS
2765 endpos = OVERLAY_POSITION (end);
2766 if (pos < endpos)
2eec3b4e
RS
2767 {
2768 if (idx == len)
2769 {
61d54cd5
RS
2770 if (extend)
2771 {
4b0e44fc
RS
2772 /* Make it work with an initial len == 0. */
2773 len *= 2;
0552666b 2774 if (len == 0)
4b0e44fc
RS
2775 len = 4;
2776 *len_ptr = len;
61d54cd5
RS
2777 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2778 *vec_ptr = vec;
2779 }
2780 else
2781 inhibit_storing = 1;
2eec3b4e 2782 }
61d54cd5
RS
2783
2784 if (!inhibit_storing)
2785 vec[idx] = overlay;
2786 idx++;
413e06a4
RS
2787
2788 if (startpos < pos && startpos > prev)
2789 prev = startpos;
2eec3b4e 2790 }
239c932b
RS
2791 else if (endpos < pos && endpos > prev)
2792 prev = endpos;
1d5f4c1d
MB
2793 else if (endpos == pos && startpos > prev
2794 && (!change_req || startpos < pos))
413e06a4 2795 prev = startpos;
1ab256cb
RM
2796 }
2797
239c932b
RS
2798 if (next_ptr)
2799 *next_ptr = next;
2800 if (prev_ptr)
2801 *prev_ptr = prev;
2eec3b4e
RS
2802 return idx;
2803}
74514898 2804\f
7723e095
MR
2805/* Find all the overlays in the current buffer that overlap the range
2806 BEG-END, or are empty at BEG, or are empty at END provided END
2807 denotes the position at the end of the current buffer.
2a3eeee7 2808
177c0ea7 2809 Return the number found, and store them in a vector in *VEC_PTR.
74514898
RS
2810 Store in *LEN_PTR the size allocated for the vector.
2811 Store in *NEXT_PTR the next position after POS where an overlay starts,
2812 or ZV if there are no more overlays.
2813 Store in *PREV_PTR the previous position before POS where an overlay ends,
2814 or BEGV if there are no previous overlays.
2815 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2816
2817 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2818 when this function is called.
2819
2820 If EXTEND is non-zero, we make the vector bigger if necessary.
2821 If EXTEND is zero, we never extend the vector,
2822 and we store only as many overlays as will fit.
2823 But we still return the total number of overlays. */
2824
a9b9a780 2825static int
74514898
RS
2826overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2827 int beg, end;
2828 int extend;
2829 Lisp_Object **vec_ptr;
2830 int *len_ptr;
2831 int *next_ptr;
2832 int *prev_ptr;
2833{
2410d73a
SM
2834 Lisp_Object overlay, ostart, oend;
2835 struct Lisp_Overlay *tail;
74514898
RS
2836 int idx = 0;
2837 int len = *len_ptr;
2838 Lisp_Object *vec = *vec_ptr;
2839 int next = ZV;
2840 int prev = BEGV;
2841 int inhibit_storing = 0;
7723e095 2842 int end_is_Z = end == Z;
74514898 2843
2410d73a 2844 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
74514898
RS
2845 {
2846 int startpos, endpos;
2847
2410d73a 2848 XSETMISC (overlay, tail);
74514898
RS
2849
2850 ostart = OVERLAY_START (overlay);
2851 oend = OVERLAY_END (overlay);
2852 endpos = OVERLAY_POSITION (oend);
2853 if (endpos < beg)
2854 {
2855 if (prev < endpos)
2856 prev = endpos;
2857 break;
2858 }
2859 startpos = OVERLAY_POSITION (ostart);
7723e095
MR
2860 /* Count an interval if it overlaps the range, is empty at the
2861 start of the range, or is empty at END provided END denotes the
2862 end of the buffer. */
74514898 2863 if ((beg < endpos && startpos < end)
7723e095
MR
2864 || (startpos == endpos
2865 && (beg == endpos || (end_is_Z && endpos == end))))
74514898
RS
2866 {
2867 if (idx == len)
2868 {
2869 /* The supplied vector is full.
2870 Either make it bigger, or don't store any more in it. */
2871 if (extend)
2872 {
4b0e44fc
RS
2873 /* Make it work with an initial len == 0. */
2874 len *= 2;
2875 if (len == 0)
2876 len = 4;
2877 *len_ptr = len;
74514898
RS
2878 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2879 *vec_ptr = vec;
2880 }
2881 else
2882 inhibit_storing = 1;
2883 }
2884
2885 if (!inhibit_storing)
2886 vec[idx] = overlay;
2887 /* Keep counting overlays even if we can't return them all. */
2888 idx++;
2889 }
2890 else if (startpos < next)
2891 next = startpos;
2892 }
2893
2410d73a 2894 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
74514898
RS
2895 {
2896 int startpos, endpos;
2897
2410d73a 2898 XSETMISC (overlay, tail);
74514898
RS
2899
2900 ostart = OVERLAY_START (overlay);
2901 oend = OVERLAY_END (overlay);
2902 startpos = OVERLAY_POSITION (ostart);
2903 if (end < startpos)
2904 {
2905 if (startpos < next)
2906 next = startpos;
2907 break;
2908 }
2909 endpos = OVERLAY_POSITION (oend);
7723e095
MR
2910 /* Count an interval if it overlaps the range, is empty at the
2911 start of the range, or is empty at END provided END denotes the
2912 end of the buffer. */
74514898 2913 if ((beg < endpos && startpos < end)
7723e095
MR
2914 || (startpos == endpos
2915 && (beg == endpos || (end_is_Z && endpos == end))))
74514898
RS
2916 {
2917 if (idx == len)
2918 {
2919 if (extend)
2920 {
4b0e44fc
RS
2921 /* Make it work with an initial len == 0. */
2922 len *= 2;
2923 if (len == 0)
2924 len = 4;
2925 *len_ptr = len;
74514898
RS
2926 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2927 *vec_ptr = vec;
2928 }
2929 else
2930 inhibit_storing = 1;
2931 }
2932
2933 if (!inhibit_storing)
2934 vec[idx] = overlay;
2935 idx++;
2936 }
2937 else if (endpos < beg && endpos > prev)
2938 prev = endpos;
2939 }
fc04fa47 2940
74514898
RS
2941 if (next_ptr)
2942 *next_ptr = next;
2943 if (prev_ptr)
2944 *prev_ptr = prev;
2945 return idx;
2946}
09a22085
GM
2947
2948
2949/* Return non-zero if there exists an overlay with a non-nil
2950 `mouse-face' property overlapping OVERLAY. */
2951
2952int
2953mouse_face_overlay_overlaps (overlay)
2954 Lisp_Object overlay;
2955{
2956 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2957 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
bfd8410f 2958 int n, i, size;
09a22085 2959 Lisp_Object *v, tem;
177c0ea7 2960
bfd8410f
GM
2961 size = 10;
2962 v = (Lisp_Object *) alloca (size * sizeof *v);
2963 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2964 if (n > size)
09a22085 2965 {
09a22085
GM
2966 v = (Lisp_Object *) alloca (n * sizeof *v);
2967 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2968 }
2969
2970 for (i = 0; i < n; ++i)
2971 if (!EQ (v[i], overlay)
2972 && (tem = Foverlay_get (overlay, Qmouse_face),
2973 !NILP (tem)))
2974 break;
2975
2976 return i < n;
2977}
2978
2979
74514898 2980\f
fc04fa47
KH
2981/* Fast function to just test if we're at an overlay boundary. */
2982int
2983overlay_touches_p (pos)
2984 int pos;
2985{
2410d73a
SM
2986 Lisp_Object overlay;
2987 struct Lisp_Overlay *tail;
fc04fa47 2988
2410d73a 2989 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
fc04fa47
KH
2990 {
2991 int endpos;
2992
2410d73a 2993 XSETMISC (overlay ,tail);
8e50cc2d 2994 if (!OVERLAYP (overlay))
fc04fa47
KH
2995 abort ();
2996
2997 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2998 if (endpos < pos)
2999 break;
3000 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3001 return 1;
3002 }
3003
2410d73a 3004 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
fc04fa47
KH
3005 {
3006 int startpos;
3007
2410d73a 3008 XSETMISC (overlay, tail);
8e50cc2d 3009 if (!OVERLAYP (overlay))
fc04fa47
KH
3010 abort ();
3011
3012 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3013 if (pos < startpos)
3014 break;
3015 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3016 return 1;
3017 }
3018 return 0;
3019}
2eec3b4e 3020\f
5985d248
KH
3021struct sortvec
3022{
3023 Lisp_Object overlay;
3024 int beg, end;
3025 int priority;
3026};
3027
3028static int
dfcf069d
AS
3029compare_overlays (v1, v2)
3030 const void *v1, *v2;
5985d248 3031{
dfcf069d
AS
3032 const struct sortvec *s1 = (const struct sortvec *) v1;
3033 const struct sortvec *s2 = (const struct sortvec *) v2;
5985d248
KH
3034 if (s1->priority != s2->priority)
3035 return s1->priority - s2->priority;
3036 if (s1->beg != s2->beg)
3037 return s1->beg - s2->beg;
3038 if (s1->end != s2->end)
3039 return s2->end - s1->end;
3040 return 0;
3041}
3042
3043/* Sort an array of overlays by priority. The array is modified in place.
3044 The return value is the new size; this may be smaller than the original
3045 size if some of the overlays were invalid or were window-specific. */
3046int
3047sort_overlays (overlay_vec, noverlays, w)
3048 Lisp_Object *overlay_vec;
3049 int noverlays;
3050 struct window *w;
3051{
3052 int i, j;
3053 struct sortvec *sortvec;
3054 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3055
3056 /* Put the valid and relevant overlays into sortvec. */
3057
3058 for (i = 0, j = 0; i < noverlays; i++)
3059 {
0fa767e7 3060 Lisp_Object tem;
c99fc30f 3061 Lisp_Object overlay;
5985d248 3062
c99fc30f 3063 overlay = overlay_vec[i];
5985d248
KH
3064 if (OVERLAY_VALID (overlay)
3065 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3066 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3067 {
0fa767e7
KH
3068 /* If we're interested in a specific window, then ignore
3069 overlays that are limited to some other window. */
3070 if (w)
5985d248 3071 {
0fa767e7
KH
3072 Lisp_Object window;
3073
3074 window = Foverlay_get (overlay, Qwindow);
a7a60ce9 3075 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 3076 continue;
5985d248 3077 }
0fa767e7
KH
3078
3079 /* This overlay is good and counts: put it into sortvec. */
3080 sortvec[j].overlay = overlay;
3081 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3082 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3083 tem = Foverlay_get (overlay, Qpriority);
3084 if (INTEGERP (tem))
3085 sortvec[j].priority = XINT (tem);
3086 else
3087 sortvec[j].priority = 0;
3088 j++;
5985d248
KH
3089 }
3090 }
3091 noverlays = j;
3092
3093 /* Sort the overlays into the proper order: increasing priority. */
3094
3095 if (noverlays > 1)
3096 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3097
3098 for (i = 0; i < noverlays; i++)
3099 overlay_vec[i] = sortvec[i].overlay;
3100 return (noverlays);
3101}
3102\f
bbbe9545
KH
3103struct sortstr
3104{
cb26008f 3105 Lisp_Object string, string2;
bbbe9545
KH
3106 int size;
3107 int priority;
3108};
3109
e8185fa8
KH
3110struct sortstrlist
3111{
3112 struct sortstr *buf; /* An array that expands as needed; never freed. */
3113 int size; /* Allocated length of that array. */
3114 int used; /* How much of the array is currently in use. */
3115 int bytes; /* Total length of the strings in buf. */
3116};
3117
3118/* Buffers for storing information about the overlays touching a given
3119 position. These could be automatic variables in overlay_strings, but
3120 it's more efficient to hold onto the memory instead of repeatedly
3121 allocating and freeing it. */
3122static struct sortstrlist overlay_heads, overlay_tails;
9492daf2 3123static unsigned char *overlay_str_buf;
e8185fa8
KH
3124
3125/* Allocated length of overlay_str_buf. */
3126static int overlay_str_len;
3127
bbbe9545
KH
3128/* A comparison function suitable for passing to qsort. */
3129static int
3130cmp_for_strings (as1, as2)
3131 char *as1, *as2;
3132{
3133 struct sortstr *s1 = (struct sortstr *)as1;
3134 struct sortstr *s2 = (struct sortstr *)as2;
3135 if (s1->size != s2->size)
3136 return s2->size - s1->size;
3137 if (s1->priority != s2->priority)
3138 return s1->priority - s2->priority;
3139 return 0;
3140}
3141
e8185fa8 3142static void
cb26008f 3143record_overlay_string (ssl, str, str2, pri, size)
e8185fa8 3144 struct sortstrlist *ssl;
cb26008f 3145 Lisp_Object str, str2, pri;
e8185fa8
KH
3146 int size;
3147{
43d27a72
RS
3148 int nbytes;
3149
e8185fa8
KH
3150 if (ssl->used == ssl->size)
3151 {
3152 if (ssl->buf)
3153 ssl->size *= 2;
3154 else
3155 ssl->size = 5;
3156 ssl->buf = ((struct sortstr *)
3157 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3158 }
3159 ssl->buf[ssl->used].string = str;
cb26008f 3160 ssl->buf[ssl->used].string2 = str2;
e8185fa8
KH
3161 ssl->buf[ssl->used].size = size;
3162 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3163 ssl->used++;
43d27a72
RS
3164
3165 if (NILP (current_buffer->enable_multibyte_characters))
d5db4077 3166 nbytes = SCHARS (str);
43d27a72 3167 else if (! STRING_MULTIBYTE (str))
d5db4077
KR
3168 nbytes = count_size_as_multibyte (SDATA (str),
3169 SBYTES (str));
43d27a72 3170 else
d5db4077 3171 nbytes = SBYTES (str);
43d27a72
RS
3172
3173 ssl->bytes += nbytes;
3174
cb26008f 3175 if (STRINGP (str2))
43d27a72
RS
3176 {
3177 if (NILP (current_buffer->enable_multibyte_characters))
d5db4077 3178 nbytes = SCHARS (str2);
43d27a72 3179 else if (! STRING_MULTIBYTE (str2))
d5db4077
KR
3180 nbytes = count_size_as_multibyte (SDATA (str2),
3181 SBYTES (str2));
43d27a72 3182 else
d5db4077 3183 nbytes = SBYTES (str2);
43d27a72
RS
3184
3185 ssl->bytes += nbytes;
3186 }
e8185fa8 3187}
bbbe9545
KH
3188
3189/* Return the concatenation of the strings associated with overlays that
3190 begin or end at POS, ignoring overlays that are specific to a window
3191 other than W. The strings are concatenated in the appropriate order:
3192 shorter overlays nest inside longer ones, and higher priority inside
cb26008f
KH
3193 lower. Normally all of the after-strings come first, but zero-sized
3194 overlays have their after-strings ride along with the before-strings
3195 because it would look strange to print them inside-out.
3196
3197 Returns the string length, and stores the contents indirectly through
3198 PSTR, if that variable is non-null. The string may be overwritten by
3199 subsequent calls. */
6b5d3b89 3200
bbbe9545
KH
3201int
3202overlay_strings (pos, w, pstr)
c2d5b10f 3203 EMACS_INT pos;
bbbe9545 3204 struct window *w;
6b5d3b89 3205 unsigned char **pstr;
bbbe9545 3206{
2410d73a
SM
3207 Lisp_Object overlay, window, str;
3208 struct Lisp_Overlay *ov;
bbbe9545 3209 int startpos, endpos;
43d27a72 3210 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
bbbe9545 3211
e8185fa8
KH
3212 overlay_heads.used = overlay_heads.bytes = 0;
3213 overlay_tails.used = overlay_tails.bytes = 0;
2410d73a 3214 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
bbbe9545 3215 {
2410d73a 3216 XSETMISC (overlay, ov);
c2d5b10f 3217 eassert (OVERLAYP (overlay));
bbbe9545
KH
3218
3219 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3220 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3221 if (endpos < pos)
3222 break;
3223 if (endpos != pos && startpos != pos)
3224 continue;
3225 window = Foverlay_get (overlay, Qwindow);
3226 if (WINDOWP (window) && XWINDOW (window) != w)
3227 continue;
e8185fa8
KH
3228 if (startpos == pos
3229 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3230 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3231 (startpos == endpos
3232 ? Foverlay_get (overlay, Qafter_string)
3233 : Qnil),
3234 Foverlay_get (overlay, Qpriority),
3235 endpos - startpos);
3236 else if (endpos == pos
3237 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3238 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3239 Foverlay_get (overlay, Qpriority),
3240 endpos - startpos);
bbbe9545 3241 }
2410d73a 3242 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
bbbe9545 3243 {
2410d73a
SM
3244 XSETMISC (overlay, ov);
3245 eassert (OVERLAYP (overlay));
bbbe9545
KH
3246
3247 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3248 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3249 if (startpos > pos)
3250 break;
e8185fa8
KH
3251 if (endpos != pos && startpos != pos)
3252 continue;
3253 window = Foverlay_get (overlay, Qwindow);
3254 if (WINDOWP (window) && XWINDOW (window) != w)
3255 continue;
e8185fa8
KH
3256 if (startpos == pos
3257 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3258 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3259 (startpos == endpos
3260 ? Foverlay_get (overlay, Qafter_string)
3261 : Qnil),
3262 Foverlay_get (overlay, Qpriority),
3263 endpos - startpos);
3264 else if (endpos == pos
3265 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3266 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3267 Foverlay_get (overlay, Qpriority),
3268 endpos - startpos);
bbbe9545 3269 }
e8185fa8
KH
3270 if (overlay_tails.used > 1)
3271 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3272 cmp_for_strings);
3273 if (overlay_heads.used > 1)
3274 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3275 cmp_for_strings);
3276 if (overlay_heads.bytes || overlay_tails.bytes)
bbbe9545 3277 {
e8185fa8 3278 Lisp_Object tem;
bbbe9545 3279 int i;
9f4d7cde 3280 unsigned char *p;
e8185fa8 3281 int total = overlay_heads.bytes + overlay_tails.bytes;
bbbe9545
KH
3282
3283 if (total > overlay_str_len)
9f4d7cde
RS
3284 {
3285 overlay_str_len = total;
3286 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3287 total);
3288 }
bbbe9545 3289 p = overlay_str_buf;
e8185fa8 3290 for (i = overlay_tails.used; --i >= 0;)
bbbe9545 3291 {
43d27a72 3292 int nbytes;
e8185fa8 3293 tem = overlay_tails.buf[i].string;
d5db4077
KR
3294 nbytes = copy_text (SDATA (tem), p,
3295 SBYTES (tem),
43d27a72
RS
3296 STRING_MULTIBYTE (tem), multibyte);
3297 p += nbytes;
bbbe9545 3298 }
e8185fa8 3299 for (i = 0; i < overlay_heads.used; ++i)
bbbe9545 3300 {
43d27a72 3301 int nbytes;
e8185fa8 3302 tem = overlay_heads.buf[i].string;
d5db4077
KR
3303 nbytes = copy_text (SDATA (tem), p,
3304 SBYTES (tem),
43d27a72
RS
3305 STRING_MULTIBYTE (tem), multibyte);
3306 p += nbytes;
cb26008f
KH
3307 tem = overlay_heads.buf[i].string2;
3308 if (STRINGP (tem))
3309 {
d5db4077
KR
3310 nbytes = copy_text (SDATA (tem), p,
3311 SBYTES (tem),
43d27a72
RS
3312 STRING_MULTIBYTE (tem), multibyte);
3313 p += nbytes;
cb26008f 3314 }
bbbe9545 3315 }
cb26008f
KH
3316 if (p != overlay_str_buf + total)
3317 abort ();
bbbe9545
KH
3318 if (pstr)
3319 *pstr = overlay_str_buf;
e8185fa8 3320 return total;
bbbe9545 3321 }
e8185fa8 3322 return 0;
bbbe9545
KH
3323}
3324\f
5c4f68f1 3325/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 3326
2eec3b4e 3327void
5c4f68f1
JB
3328recenter_overlay_lists (buf, pos)
3329 struct buffer *buf;
c2d5b10f 3330 EMACS_INT pos;
2eec3b4e 3331{
2410d73a
SM
3332 Lisp_Object overlay, beg, end;
3333 struct Lisp_Overlay *prev, *tail, *next;
2eec3b4e
RS
3334
3335 /* See if anything in overlays_before should move to overlays_after. */
3336
3337 /* We don't strictly need prev in this loop; it should always be nil.
3338 But we use it for symmetry and in case that should cease to be true
3339 with some future change. */
2410d73a
SM
3340 prev = NULL;
3341 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
1ab256cb 3342 {
2410d73a
SM
3343 next = tail->next;
3344 XSETMISC (overlay, tail);
2eec3b4e
RS
3345
3346 /* If the overlay is not valid, get rid of it. */
3347 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
3348#if 1
3349 abort ();
3350#else
2eec3b4e
RS
3351 {
3352 /* Splice the cons cell TAIL out of overlays_before. */
3353 if (!NILP (prev))
7539e11f 3354 XCDR (prev) = next;
2eec3b4e 3355 else
5c4f68f1 3356 buf->overlays_before = next;
2eec3b4e
RS
3357 tail = prev;
3358 continue;
3359 }
52f8ec73 3360#endif
1ab256cb 3361
2eec3b4e
RS
3362 beg = OVERLAY_START (overlay);
3363 end = OVERLAY_END (overlay);
1ab256cb 3364
2eec3b4e 3365 if (OVERLAY_POSITION (end) > pos)
1ab256cb 3366 {
2eec3b4e
RS
3367 /* OVERLAY needs to be moved. */
3368 int where = OVERLAY_POSITION (beg);
2410d73a 3369 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3370
3371 /* Splice the cons cell TAIL out of overlays_before. */
2410d73a
SM
3372 if (prev)
3373 prev->next = next;
2eec3b4e 3374 else
5c4f68f1 3375 buf->overlays_before = next;
2eec3b4e
RS
3376
3377 /* Search thru overlays_after for where to put it. */
2410d73a
SM
3378 other_prev = NULL;
3379 for (other = buf->overlays_after; other;
3380 other_prev = other, other = other->next)
1ab256cb 3381 {
6af718a4 3382 Lisp_Object otherbeg, otheroverlay;
2eec3b4e 3383
2410d73a
SM
3384 XSETMISC (otheroverlay, other);
3385 eassert (OVERLAY_VALID (otheroverlay));
2eec3b4e
RS
3386
3387 otherbeg = OVERLAY_START (otheroverlay);
3388 if (OVERLAY_POSITION (otherbeg) >= where)
3389 break;
1ab256cb 3390 }
2eec3b4e
RS
3391
3392 /* Add TAIL to overlays_after before OTHER. */
2410d73a
SM
3393 tail->next = other;
3394 if (other_prev)
3395 other_prev->next = tail;
1ab256cb 3396 else
5c4f68f1 3397 buf->overlays_after = tail;
2eec3b4e 3398 tail = prev;
1ab256cb 3399 }
2eec3b4e
RS
3400 else
3401 /* We've reached the things that should stay in overlays_before.
3402 All the rest of overlays_before must end even earlier,
3403 so stop now. */
3404 break;
3405 }
3406
3407 /* See if anything in overlays_after should be in overlays_before. */
2410d73a
SM
3408 prev = NULL;
3409 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
2eec3b4e 3410 {
2410d73a
SM
3411 next = tail->next;
3412 XSETMISC (overlay, tail);
2eec3b4e
RS
3413
3414 /* If the overlay is not valid, get rid of it. */
3415 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
3416#if 1
3417 abort ();
3418#else
2eec3b4e
RS
3419 {
3420 /* Splice the cons cell TAIL out of overlays_after. */
3421 if (!NILP (prev))
7539e11f 3422 XCDR (prev) = next;
2eec3b4e 3423 else
5c4f68f1 3424 buf->overlays_after = next;
2eec3b4e
RS
3425 tail = prev;
3426 continue;
3427 }
52f8ec73 3428#endif
2eec3b4e
RS
3429
3430 beg = OVERLAY_START (overlay);
3431 end = OVERLAY_END (overlay);
3432
3433 /* Stop looking, when we know that nothing further
3434 can possibly end before POS. */
3435 if (OVERLAY_POSITION (beg) > pos)
3436 break;
3437
3438 if (OVERLAY_POSITION (end) <= pos)
3439 {
3440 /* OVERLAY needs to be moved. */
3441 int where = OVERLAY_POSITION (end);
2410d73a 3442 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3443
3444 /* Splice the cons cell TAIL out of overlays_after. */
2410d73a
SM
3445 if (prev)
3446 prev->next = next;
2eec3b4e 3447 else
5c4f68f1 3448 buf->overlays_after = next;
2eec3b4e
RS
3449
3450 /* Search thru overlays_before for where to put it. */
2410d73a
SM
3451 other_prev = NULL;
3452 for (other = buf->overlays_before; other;
3453 other_prev = other, other = other->next)
2eec3b4e
RS
3454 {
3455 Lisp_Object otherend, otheroverlay;
2eec3b4e 3456
2410d73a
SM
3457 XSETMISC (otheroverlay, other);
3458 eassert (OVERLAY_VALID (otheroverlay));
2eec3b4e
RS
3459
3460 otherend = OVERLAY_END (otheroverlay);
3461 if (OVERLAY_POSITION (otherend) <= where)
3462 break;
3463 }
3464
3465 /* Add TAIL to overlays_before before OTHER. */
2410d73a
SM
3466 tail->next = other;
3467 if (other_prev)
3468 other_prev->next = tail;
2eec3b4e 3469 else
5c4f68f1 3470 buf->overlays_before = tail;
2eec3b4e
RS
3471 tail = prev;
3472 }
3473 }
3474
c2d5b10f 3475 buf->overlay_center = pos;
2eec3b4e 3476}
2b1bdf65 3477
423cdb46
KH
3478void
3479adjust_overlays_for_insert (pos, length)
c2d5b10f
SM
3480 EMACS_INT pos;
3481 EMACS_INT length;
423cdb46
KH
3482{
3483 /* After an insertion, the lists are still sorted properly,
3484 but we may need to update the value of the overlay center. */
c2d5b10f
SM
3485 if (current_buffer->overlay_center >= pos)
3486 current_buffer->overlay_center += length;
423cdb46
KH
3487}
3488
3489void
3490adjust_overlays_for_delete (pos, length)
c2d5b10f
SM
3491 EMACS_INT pos;
3492 EMACS_INT length;
423cdb46 3493{
c2d5b10f 3494 if (current_buffer->overlay_center < pos)
423cdb46
KH
3495 /* The deletion was to our right. No change needed; the before- and
3496 after-lists are still consistent. */
3497 ;
c2d5b10f 3498 else if (current_buffer->overlay_center > pos + length)
423cdb46
KH
3499 /* The deletion was to our left. We need to adjust the center value
3500 to account for the change in position, but the lists are consistent
3501 given the new value. */
c2d5b10f 3502 current_buffer->overlay_center -= length;
423cdb46
KH
3503 else
3504 /* We're right in the middle. There might be things on the after-list
3505 that now belong on the before-list. Recentering will move them,
3506 and also update the center point. */
3507 recenter_overlay_lists (current_buffer, pos);
3508}
3509
2b1bdf65
KH
3510/* Fix up overlays that were garbled as a result of permuting markers
3511 in the range START through END. Any overlay with at least one
3512 endpoint in this range will need to be unlinked from the overlay
3513 list and reinserted in its proper place.
3514 Such an overlay might even have negative size at this point.
6b61353c 3515 If so, we'll make the overlay empty. */
2b1bdf65 3516void
6b61353c 3517fix_start_end_in_overlays (start, end)
2b1bdf65
KH
3518 register int start, end;
3519{
6af718a4 3520 Lisp_Object overlay;
2410d73a 3521 struct Lisp_Overlay *before_list, *after_list;
1138e742
KR
3522 /* These are either nil, indicating that before_list or after_list
3523 should be assigned, or the cons cell the cdr of which should be
3524 assigned. */
2410d73a 3525 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
1138e742
KR
3526 /* 'Parent', likewise, indicates a cons cell or
3527 current_buffer->overlays_before or overlays_after, depending
3528 which loop we're in. */
2410d73a 3529 struct Lisp_Overlay *tail, *parent;
2b1bdf65
KH
3530 int startpos, endpos;
3531
3532 /* This algorithm shifts links around instead of consing and GCing.
3533 The loop invariant is that before_list (resp. after_list) is a
1138e742
KR
3534 well-formed list except that its last element, the CDR of beforep
3535 (resp. afterp) if beforep (afterp) isn't nil or before_list
3536 (after_list) if it is, is still uninitialized. So it's not a bug
3537 that before_list isn't initialized, although it may look
3538 strange. */
2410d73a 3539 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
2b1bdf65 3540 {
2410d73a 3541 XSETMISC (overlay, tail);
6b61353c 3542
2b1bdf65 3543 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
6b61353c
KH
3544 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3545
3546 /* If the overlay is backwards, make it empty. */
3547 if (endpos < startpos)
3548 {
3549 startpos = endpos;
3550 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3551 Qnil);
3552 }
3553
2b1bdf65
KH
3554 if (endpos < start)
3555 break;
60ebfdf3 3556
2b1bdf65
KH
3557 if (endpos < end
3558 || (startpos >= start && startpos < end))
3559 {
2b1bdf65
KH
3560 /* Add it to the end of the wrong list. Later on,
3561 recenter_overlay_lists will move it to the right place. */
c2d5b10f 3562 if (endpos < current_buffer->overlay_center)
2b1bdf65 3563 {
2410d73a 3564 if (!afterp)
1138e742
KR
3565 after_list = tail;
3566 else
2410d73a 3567 afterp->next = tail;
1138e742 3568 afterp = tail;
2b1bdf65
KH
3569 }
3570 else
3571 {
2410d73a 3572 if (!beforep)
1138e742
KR
3573 before_list = tail;
3574 else
2410d73a 3575 beforep->next = tail;
1138e742 3576 beforep = tail;
2b1bdf65 3577 }
2410d73a
SM
3578 if (!parent)
3579 current_buffer->overlays_before = tail->next;
1138e742 3580 else
2410d73a
SM
3581 parent->next = tail->next;
3582 tail = tail->next;
2b1bdf65
KH
3583 }
3584 else
2410d73a 3585 parent = tail, tail = parent->next;
2b1bdf65 3586 }
2410d73a 3587 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
2b1bdf65 3588 {
2410d73a 3589 XSETMISC (overlay, tail);
6b61353c 3590
2b1bdf65 3591 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
6b61353c
KH
3592 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3593
3594 /* If the overlay is backwards, make it empty. */
3595 if (endpos < startpos)
3596 {
3597 startpos = endpos;
3598 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
60ebfdf3 3599 Qnil);
6b61353c
KH
3600 }
3601
2b1bdf65
KH
3602 if (startpos >= end)
3603 break;
6b61353c 3604
2b1bdf65
KH
3605 if (startpos >= start
3606 || (endpos >= start && endpos < end))
3607 {
c2d5b10f 3608 if (endpos < current_buffer->overlay_center)
2b1bdf65 3609 {
2410d73a 3610 if (!afterp)
1138e742
KR
3611 after_list = tail;
3612 else
2410d73a 3613 afterp->next = tail;
1138e742 3614 afterp = tail;
2b1bdf65
KH
3615 }
3616 else
3617 {
2410d73a 3618 if (!beforep)
1138e742
KR
3619 before_list = tail;
3620 else
2410d73a 3621 beforep->next = tail;
1138e742 3622 beforep = tail;
2b1bdf65 3623 }
2410d73a
SM
3624 if (!parent)
3625 current_buffer->overlays_after = tail->next;
1138e742 3626 else
2410d73a
SM
3627 parent->next = tail->next;
3628 tail = tail->next;
2b1bdf65
KH
3629 }
3630 else
2410d73a 3631 parent = tail, tail = parent->next;
2b1bdf65
KH
3632 }
3633
3634 /* Splice the constructed (wrong) lists into the buffer's lists,
3635 and let the recenter function make it sane again. */
2410d73a 3636 if (beforep)
1138e742 3637 {
2410d73a 3638 beforep->next = current_buffer->overlays_before;
1138e742
KR
3639 current_buffer->overlays_before = before_list;
3640 }
c2d5b10f 3641 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3642
2410d73a 3643 if (afterp)
1138e742 3644 {
2410d73a 3645 afterp->next = current_buffer->overlays_after;
1138e742
KR
3646 current_buffer->overlays_after = after_list;
3647 }
c2d5b10f 3648 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3649}
3b06f880
KH
3650
3651/* We have two types of overlay: the one whose ending marker is
3652 after-insertion-marker (this is the usual case) and the one whose
3653 ending marker is before-insertion-marker. When `overlays_before'
3654 contains overlays of the latter type and the former type in this
3655 order and both overlays end at inserting position, inserting a text
3656 increases only the ending marker of the latter type, which results
3657 in incorrect ordering of `overlays_before'.
3658
3659 This function fixes ordering of overlays in the slot
3660 `overlays_before' of the buffer *BP. Before the insertion, `point'
3661 was at PREV, and now is at POS. */
3662
01136e9b 3663void
3b06f880
KH
3664fix_overlays_before (bp, prev, pos)
3665 struct buffer *bp;
c2d5b10f 3666 EMACS_INT prev, pos;
3b06f880 3667{
2410d73a
SM
3668 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3669 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3670 Lisp_Object tem;
c2d5b10f 3671 EMACS_INT end;
3b06f880
KH
3672
3673 /* After the insertion, the several overlays may be in incorrect
3674 order. The possibility is that, in the list `overlays_before',
3675 an overlay which ends at POS appears after an overlay which ends
3676 at PREV. Since POS is greater than PREV, we must fix the
3677 ordering of these overlays, by moving overlays ends at POS before
3678 the overlays ends at PREV. */
3679
3680 /* At first, find a place where disordered overlays should be linked
3681 in. It is where an overlay which end before POS exists. (i.e. an
3682 overlay whose ending marker is after-insertion-marker if disorder
3683 exists). */
2410d73a
SM
3684 while (tail
3685 && (XSETMISC (tem, tail),
3686 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
1138e742
KR
3687 {
3688 parent = tail;
2410d73a 3689 tail = tail->next;
1138e742 3690 }
3b06f880
KH
3691
3692 /* If we don't find such an overlay,
3693 or the found one ends before PREV,
3694 or the found one is the last one in the list,
3695 we don't have to fix anything. */
f93ad4cf 3696 if (!tail || end < prev || !tail->next)
3b06f880
KH
3697 return;
3698
1138e742
KR
3699 right_pair = parent;
3700 parent = tail;
2410d73a 3701 tail = tail->next;
3b06f880 3702
1138e742 3703 /* Now, end position of overlays in the list TAIL should be before
3b06f880 3704 or equal to PREV. In the loop, an overlay which ends at POS is
1138e742
KR
3705 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3706 we found an overlay which ends before PREV, the remaining
3707 overlays are in correct order. */
2410d73a 3708 while (tail)
3b06f880 3709 {
2410d73a
SM
3710 XSETMISC (tem, tail);
3711 end = OVERLAY_POSITION (OVERLAY_END (tem));
3b06f880
KH
3712
3713 if (end == pos)
3714 { /* This overlay is disordered. */
2410d73a 3715 struct Lisp_Overlay *found = tail;
3b06f880
KH
3716
3717 /* Unlink the found overlay. */
2410d73a
SM
3718 tail = found->next;
3719 parent->next = tail;
1138e742
KR
3720 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3721 and link it into the right place. */
2410d73a 3722 if (!right_pair)
1138e742 3723 {
2410d73a 3724 found->next = bp->overlays_before;
1138e742
KR
3725 bp->overlays_before = found;
3726 }
3727 else
3728 {
2410d73a
SM
3729 found->next = right_pair->next;
3730 right_pair->next = found;
1138e742 3731 }
3b06f880
KH
3732 }
3733 else if (end == prev)
1138e742
KR
3734 {
3735 parent = tail;
2410d73a 3736 tail = tail->next;
1138e742 3737 }
3b06f880
KH
3738 else /* No more disordered overlay. */
3739 break;
3740 }
3741}
2eec3b4e 3742\f
52f8ec73 3743DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
7ee72033
MB
3744 doc: /* Return t if OBJECT is an overlay. */)
3745 (object)
52f8ec73
JB
3746 Lisp_Object object;
3747{
3748 return (OVERLAYP (object) ? Qt : Qnil);
3749}
3750
acac2700 3751DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
7ee72033 3752 doc: /* Create a new overlay with range BEG to END in BUFFER.
018ba359
PJ
3753If omitted, BUFFER defaults to the current buffer.
3754BEG and END may be integers or markers.
a625ee20
RS
3755The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3756for the front of the overlay advance when text is inserted there
63af6055 3757\(which means the text *is not* included in the overlay).
a625ee20
RS
3758The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3759for the rear of the overlay advance when text is inserted there
63af6055 3760\(which means the text *is* included in the overlay). */)
7ee72033 3761 (beg, end, buffer, front_advance, rear_advance)
5c4f68f1 3762 Lisp_Object beg, end, buffer;
acac2700 3763 Lisp_Object front_advance, rear_advance;
2eec3b4e
RS
3764{
3765 Lisp_Object overlay;
5c4f68f1 3766 struct buffer *b;
2eec3b4e 3767
5c4f68f1 3768 if (NILP (buffer))
67180c6a 3769 XSETBUFFER (buffer, current_buffer);
883047b9 3770 else
b7826503 3771 CHECK_BUFFER (buffer);
883047b9
JB
3772 if (MARKERP (beg)
3773 && ! EQ (Fmarker_buffer (beg), buffer))
3774 error ("Marker points into wrong buffer");
3775 if (MARKERP (end)
3776 && ! EQ (Fmarker_buffer (end), buffer))
3777 error ("Marker points into wrong buffer");
2eec3b4e 3778
b7826503
PJ
3779 CHECK_NUMBER_COERCE_MARKER (beg);
3780 CHECK_NUMBER_COERCE_MARKER (end);
5c4f68f1 3781
883047b9 3782 if (XINT (beg) > XINT (end))
5c4f68f1 3783 {
c99fc30f
KH
3784 Lisp_Object temp;
3785 temp = beg; beg = end; end = temp;
5c4f68f1 3786 }
883047b9
JB
3787
3788 b = XBUFFER (buffer);
3789
3790 beg = Fset_marker (Fmake_marker (), beg, buffer);
3791 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 3792
acac2700
RS
3793 if (!NILP (front_advance))
3794 XMARKER (beg)->insertion_type = 1;
3795 if (!NILP (rear_advance))
3796 XMARKER (end)->insertion_type = 1;
597dd755 3797
48e2e3ba 3798 overlay = allocate_misc ();
89ca3e1b 3799 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
48e2e3ba
KH
3800 XOVERLAY (overlay)->start = beg;
3801 XOVERLAY (overlay)->end = end;
3802 XOVERLAY (overlay)->plist = Qnil;
2410d73a 3803 XOVERLAY (overlay)->next = NULL;
2eec3b4e 3804
177c0ea7 3805 /* Put the new overlay on the wrong list. */
2eec3b4e 3806 end = OVERLAY_END (overlay);
c2d5b10f 3807 if (OVERLAY_POSITION (end) < b->overlay_center)
2410d73a
SM
3808 {
3809 if (b->overlays_after)
3810 XOVERLAY (overlay)->next = b->overlays_after;
3811 b->overlays_after = XOVERLAY (overlay);
3812 }
2eec3b4e 3813 else
2410d73a
SM
3814 {
3815 if (b->overlays_before)
3816 XOVERLAY (overlay)->next = b->overlays_before;
3817 b->overlays_before = XOVERLAY (overlay);
3818 }
2eec3b4e
RS
3819
3820 /* This puts it in the right list, and in the right order. */
c2d5b10f 3821 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 3822
b61982dd
JB
3823 /* We don't need to redisplay the region covered by the overlay, because
3824 the overlay has no properties at the moment. */
3825
2eec3b4e
RS
3826 return overlay;
3827}
876aa27c
RS
3828\f
3829/* Mark a section of BUF as needing redisplay because of overlays changes. */
3830
3831static void
3832modify_overlay (buf, start, end)
3833 struct buffer *buf;
c2d5b10f 3834 EMACS_INT start, end;
876aa27c 3835{
876aa27c
RS
3836 if (start > end)
3837 {
3838 int temp = start;
26f545d7
GM
3839 start = end;
3840 end = temp;
876aa27c
RS
3841 }
3842
481b5054 3843 BUF_COMPUTE_UNCHANGED (buf, start, end);
177c0ea7 3844
876aa27c
RS
3845 /* If this is a buffer not in the selected window,
3846 we must do other windows. */
3847 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3848 windows_or_buffers_changed = 1;
876aa27c
RS
3849 /* If multiple windows show this buffer, we must do other windows. */
3850 else if (buffer_shared > 1)
3851 windows_or_buffers_changed = 1;
18700091
KS
3852 /* If we modify an overlay at the end of the buffer, we cannot
3853 be sure that window end is still valid. */
3854 else if (end >= ZV && start <= ZV)
3855 windows_or_buffers_changed = 1;
876aa27c 3856
d8b9150f 3857 ++BUF_OVERLAY_MODIFF (buf);
876aa27c 3858}
2eec3b4e 3859
018ba359 3860\f
2e34157c
RS
3861Lisp_Object Fdelete_overlay ();
3862
2410d73a
SM
3863static struct Lisp_Overlay *
3864unchain_overlay (list, overlay)
3865 struct Lisp_Overlay *list, *overlay;
3866{
3867 struct Lisp_Overlay *tmp, *prev;
3868 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3869 if (tmp == overlay)
3870 {
3871 if (prev)
3872 prev->next = tmp->next;
3873 else
3874 list = tmp->next;
3875 overlay->next = NULL;
3876 break;
3877 }
3878 return list;
3879}
3880
5c4f68f1 3881DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
7ee72033 3882 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
018ba359
PJ
3883If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3884If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
7ee72033
MB
3885buffer. */)
3886 (overlay, beg, end, buffer)
5c4f68f1 3887 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 3888{
0a4469c9
RS
3889 struct buffer *b, *ob;
3890 Lisp_Object obuffer;
aed13378 3891 int count = SPECPDL_INDEX ();
5c4f68f1 3892
b7826503 3893 CHECK_OVERLAY (overlay);
5c4f68f1
JB
3894 if (NILP (buffer))
3895 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 3896 if (NILP (buffer))
67180c6a 3897 XSETBUFFER (buffer, current_buffer);
b7826503 3898 CHECK_BUFFER (buffer);
883047b9
JB
3899
3900 if (MARKERP (beg)
3901 && ! EQ (Fmarker_buffer (beg), buffer))
3902 error ("Marker points into wrong buffer");
3903 if (MARKERP (end)
3904 && ! EQ (Fmarker_buffer (end), buffer))
3905 error ("Marker points into wrong buffer");
3906
b7826503
PJ
3907 CHECK_NUMBER_COERCE_MARKER (beg);
3908 CHECK_NUMBER_COERCE_MARKER (end);
b61982dd 3909
9d7608b7
KH
3910 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3911 return Fdelete_overlay (overlay);
0a4469c9 3912
b61982dd
JB
3913 if (XINT (beg) > XINT (end))
3914 {
c99fc30f
KH
3915 Lisp_Object temp;
3916 temp = beg; beg = end; end = temp;
b61982dd
JB
3917 }
3918
9d7608b7
KH
3919 specbind (Qinhibit_quit, Qt);
3920
0a4469c9 3921 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 3922 b = XBUFFER (buffer);
8801a864 3923 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
2eec3b4e 3924
c82ed728 3925 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 3926 if (!EQ (buffer, obuffer))
50760c4a
RS
3927 {
3928 /* Redisplay where the overlay was. */
3929 if (!NILP (obuffer))
3930 {
2e34157c
RS
3931 int o_beg;
3932 int o_end;
50760c4a 3933
80509f2f
RS
3934 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3935 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
50760c4a 3936
2e34157c 3937 modify_overlay (ob, o_beg, o_end);
50760c4a
RS
3938 }
3939
3940 /* Redisplay where the overlay is going to be. */
876aa27c 3941 modify_overlay (b, XINT (beg), XINT (end));
50760c4a 3942 }
c82ed728
JB
3943 else
3944 /* Redisplay the area the overlay has just left, or just enclosed. */
3945 {
2e34157c 3946 int o_beg, o_end;
c82ed728 3947
80509f2f
RS
3948 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3949 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
c82ed728 3950
2e34157c
RS
3951 if (o_beg == XINT (beg))
3952 modify_overlay (b, o_end, XINT (end));
3953 else if (o_end == XINT (end))
3954 modify_overlay (b, o_beg, XINT (beg));
c82ed728
JB
3955 else
3956 {
2e34157c
RS
3957 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3958 if (XINT (end) > o_end) o_end = XINT (end);
3959 modify_overlay (b, o_beg, o_end);
c82ed728
JB
3960 }
3961 }
b61982dd 3962
0a4469c9
RS
3963 if (!NILP (obuffer))
3964 {
2410d73a
SM
3965 ob->overlays_before
3966 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3967 ob->overlays_after
3968 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3969 eassert (XOVERLAY (overlay)->next == NULL);
0a4469c9 3970 }
5c4f68f1
JB
3971
3972 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3973 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e 3974
177c0ea7 3975 /* Put the overlay on the wrong list. */
2eec3b4e 3976 end = OVERLAY_END (overlay);
c2d5b10f 3977 if (OVERLAY_POSITION (end) < b->overlay_center)
2410d73a 3978 {
8f924df7
KH
3979 XOVERLAY (overlay)->next = b->overlays_after;
3980 b->overlays_after = XOVERLAY (overlay);
2410d73a 3981 }
2eec3b4e 3982 else
2410d73a 3983 {
8f924df7
KH
3984 XOVERLAY (overlay)->next = b->overlays_before;
3985 b->overlays_before = XOVERLAY (overlay);
2410d73a 3986 }
2eec3b4e
RS
3987
3988 /* This puts it in the right list, and in the right order. */
c2d5b10f 3989 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 3990
0a4469c9 3991 return unbind_to (count, overlay);
2eec3b4e
RS
3992}
3993
3994DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
7ee72033
MB
3995 doc: /* Delete the overlay OVERLAY from its buffer. */)
3996 (overlay)
5c4f68f1 3997 Lisp_Object overlay;
2eec3b4e 3998{
0a4469c9 3999 Lisp_Object buffer;
5c4f68f1 4000 struct buffer *b;
aed13378 4001 int count = SPECPDL_INDEX ();
5c4f68f1 4002
b7826503 4003 CHECK_OVERLAY (overlay);
52f8ec73 4004
0a4469c9
RS
4005 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4006 if (NILP (buffer))
4007 return Qnil;
4008
4009 b = XBUFFER (buffer);
0a4469c9 4010 specbind (Qinhibit_quit, Qt);
177c0ea7 4011
2410d73a
SM
4012 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
4013 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
4014 eassert (XOVERLAY (overlay)->next == NULL);
876aa27c 4015 modify_overlay (b,
8231a9aa
RS
4016 marker_position (OVERLAY_START (overlay)),
4017 marker_position (OVERLAY_END (overlay)));
3ece337a
JB
4018 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
4019 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4020
e58c389d 4021 /* When deleting an overlay with before or after strings, turn off
26f545d7
GM
4022 display optimizations for the affected buffer, on the basis that
4023 these strings may contain newlines. This is easier to do than to
4024 check for that situation during redisplay. */
4025 if (!windows_or_buffers_changed
4026 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4027 || !NILP (Foverlay_get (overlay, Qafter_string))))
4028 b->prevent_redisplay_optimizations_p = 1;
4029
0a4469c9 4030 return unbind_to (count, Qnil);
2eec3b4e
RS
4031}
4032\f
8ebafa8d
JB
4033/* Overlay dissection functions. */
4034
4035DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
7ee72033
MB
4036 doc: /* Return the position at which OVERLAY starts. */)
4037 (overlay)
8ebafa8d
JB
4038 Lisp_Object overlay;
4039{
b7826503 4040 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4041
4042 return (Fmarker_position (OVERLAY_START (overlay)));
4043}
4044
4045DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
7ee72033
MB
4046 doc: /* Return the position at which OVERLAY ends. */)
4047 (overlay)
8ebafa8d
JB
4048 Lisp_Object overlay;
4049{
b7826503 4050 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4051
4052 return (Fmarker_position (OVERLAY_END (overlay)));
4053}
4054
4055DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
563f7128
LK
4056 doc: /* Return the buffer OVERLAY belongs to.
4057Return nil if OVERLAY has been deleted. */)
7ee72033 4058 (overlay)
8ebafa8d
JB
4059 Lisp_Object overlay;
4060{
b7826503 4061 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4062
4063 return Fmarker_buffer (OVERLAY_START (overlay));
4064}
4065
4066DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
7ee72033 4067 doc: /* Return a list of the properties on OVERLAY.
018ba359 4068This is a copy of OVERLAY's plist; modifying its conses has no effect on
7ee72033
MB
4069OVERLAY. */)
4070 (overlay)
8ebafa8d
JB
4071 Lisp_Object overlay;
4072{
b7826503 4073 CHECK_OVERLAY (overlay);
8ebafa8d 4074
48e2e3ba 4075 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
4076}
4077
4078\f
2eec3b4e 4079DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
aabc29c8 4080 doc: /* Return a list of the overlays that contain the character at POS. */)
7ee72033 4081 (pos)
2eec3b4e
RS
4082 Lisp_Object pos;
4083{
4084 int noverlays;
2eec3b4e
RS
4085 Lisp_Object *overlay_vec;
4086 int len;
4087 Lisp_Object result;
4088
b7826503 4089 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
4090
4091 len = 10;
a9800ae8 4092 /* We can't use alloca here because overlays_at can call xrealloc. */
2eec3b4e
RS
4093 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4094
4095 /* Put all the overlays we want in a vector in overlay_vec.
4096 Store the length in len. */
2a77a7d7 4097 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
0f8b27ea 4098 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
2eec3b4e
RS
4099
4100 /* Make a list of them all. */
4101 result = Flist (noverlays, overlay_vec);
4102
9ac0d9e0 4103 xfree (overlay_vec);
2eec3b4e
RS
4104 return result;
4105}
4106
74514898 4107DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
7ee72033 4108 doc: /* Return a list of the overlays that overlap the region BEG ... END.
018ba359
PJ
4109Overlap means that at least one character is contained within the overlay
4110and also contained within the specified region.
7723e095
MR
4111Empty overlays are included in the result if they are located at BEG,
4112between BEG and END, or at END provided END denotes the position at the
4113end of the buffer. */)
7ee72033 4114 (beg, end)
74514898
RS
4115 Lisp_Object beg, end;
4116{
4117 int noverlays;
4118 Lisp_Object *overlay_vec;
4119 int len;
4120 Lisp_Object result;
4121
b7826503
PJ
4122 CHECK_NUMBER_COERCE_MARKER (beg);
4123 CHECK_NUMBER_COERCE_MARKER (end);
74514898
RS
4124
4125 len = 10;
4126 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4127
4128 /* Put all the overlays we want in a vector in overlay_vec.
4129 Store the length in len. */
4130 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4131 (int *) 0, (int *) 0);
4132
4133 /* Make a list of them all. */
4134 result = Flist (noverlays, overlay_vec);
4135
4136 xfree (overlay_vec);
4137 return result;
4138}
4139
2eec3b4e 4140DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
efc7e75f 4141 1, 1, 0,
7ee72033 4142 doc: /* Return the next position after POS where an overlay starts or ends.
624d2678
RS
4143If there are no overlay boundaries from POS to (point-max),
4144the value is (point-max). */)
7ee72033 4145 (pos)
2eec3b4e
RS
4146 Lisp_Object pos;
4147{
4148 int noverlays;
0f8b27ea 4149 EMACS_INT endpos;
2eec3b4e
RS
4150 Lisp_Object *overlay_vec;
4151 int len;
2eec3b4e
RS
4152 int i;
4153
b7826503 4154 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
4155
4156 len = 10;
4157 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4158
4159 /* Put all the overlays we want in a vector in overlay_vec.
4160 Store the length in len.
4161 endpos gets the position where the next overlay starts. */
2a77a7d7 4162 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
0f8b27ea 4163 &endpos, (EMACS_INT *) 0, 1);
2eec3b4e
RS
4164
4165 /* If any of these overlays ends before endpos,
4166 use its ending point instead. */
4167 for (i = 0; i < noverlays; i++)
4168 {
4169 Lisp_Object oend;
0f8b27ea 4170 EMACS_INT oendpos;
2eec3b4e
RS
4171
4172 oend = OVERLAY_END (overlay_vec[i]);
4173 oendpos = OVERLAY_POSITION (oend);
4174 if (oendpos < endpos)
4175 endpos = oendpos;
1ab256cb
RM
4176 }
4177
9ac0d9e0 4178 xfree (overlay_vec);
2eec3b4e
RS
4179 return make_number (endpos);
4180}
239c932b
RS
4181
4182DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4183 Sprevious_overlay_change, 1, 1, 0,
7ee72033 4184 doc: /* Return the previous position before POS where an overlay starts or ends.
624d2678
RS
4185If there are no overlay boundaries from (point-min) to POS,
4186the value is (point-min). */)
7ee72033 4187 (pos)
239c932b
RS
4188 Lisp_Object pos;
4189{
4190 int noverlays;
0f8b27ea 4191 EMACS_INT prevpos;
239c932b
RS
4192 Lisp_Object *overlay_vec;
4193 int len;
239c932b 4194
b7826503 4195 CHECK_NUMBER_COERCE_MARKER (pos);
239c932b 4196
624bbdc4
RS
4197 /* At beginning of buffer, we know the answer;
4198 avoid bug subtracting 1 below. */
4199 if (XINT (pos) == BEGV)
4200 return pos;
4201
017f0539
GM
4202 len = 10;
4203 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4204
239c932b
RS
4205 /* Put all the overlays we want in a vector in overlay_vec.
4206 Store the length in len.
daa1c109 4207 prevpos gets the position of the previous change. */
2a77a7d7 4208 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
0f8b27ea 4209 (EMACS_INT *) 0, &prevpos, 1);
239c932b 4210
239c932b
RS
4211 xfree (overlay_vec);
4212 return make_number (prevpos);
4213}
2eec3b4e
RS
4214\f
4215/* These functions are for debugging overlays. */
4216
4217DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
7ee72033 4218 doc: /* Return a pair of lists giving all the overlays of the current buffer.
018ba359
PJ
4219The car has all the overlays before the overlay center;
4220the cdr has all the overlays after the overlay center.
4221Recentering overlays moves overlays between these lists.
4222The lists you get are copies, so that changing them has no effect.
7ee72033
MB
4223However, the overlays you get are the real objects that the buffer uses. */)
4224 ()
2eec3b4e 4225{
2410d73a
SM
4226 struct Lisp_Overlay *ol;
4227 Lisp_Object before = Qnil, after = Qnil, tmp;
4228 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4229 {
4230 XSETMISC (tmp, ol);
4231 before = Fcons (tmp, before);
4232 }
4233 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4234 {
4235 XSETMISC (tmp, ol);
4236 after = Fcons (tmp, after);
4237 }
4238 return Fcons (Fnreverse (before), Fnreverse (after));
2eec3b4e
RS
4239}
4240
4241DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
c87426c5
RS
4242 doc: /* Recenter the overlays of the current buffer around position POS.
4243That makes overlay lookup faster for positions near POS (but perhaps slower
4244for positions far away from POS). */)
7ee72033 4245 (pos)
2eec3b4e
RS
4246 Lisp_Object pos;
4247{
b7826503 4248 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e 4249
5c4f68f1 4250 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
4251 return Qnil;
4252}
4253\f
4254DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
7ee72033
MB
4255 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4256 (overlay, prop)
2eec3b4e
RS
4257 Lisp_Object overlay, prop;
4258{
b7826503 4259 CHECK_OVERLAY (overlay);
9a593927 4260 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
2eec3b4e
RS
4261}
4262
4263DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
7ee72033
MB
4264 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4265 (overlay, prop, value)
2eec3b4e
RS
4266 Lisp_Object overlay, prop, value;
4267{
48e2e3ba 4268 Lisp_Object tail, buffer;
9d7608b7 4269 int changed;
2eec3b4e 4270
b7826503 4271 CHECK_OVERLAY (overlay);
b61982dd 4272
274a9425
RS
4273 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4274
48e2e3ba 4275 for (tail = XOVERLAY (overlay)->plist;
7539e11f
KR
4276 CONSP (tail) && CONSP (XCDR (tail));
4277 tail = XCDR (XCDR (tail)))
4278 if (EQ (XCAR (tail), prop))
274a9425 4279 {
7539e11f 4280 changed = !EQ (XCAR (XCDR (tail)), value);
f3fbd155 4281 XSETCAR (XCDR (tail), value);
9d7608b7 4282 goto found;
274a9425 4283 }
9d7608b7
KH
4284 /* It wasn't in the list, so add it to the front. */
4285 changed = !NILP (value);
48e2e3ba
KH
4286 XOVERLAY (overlay)->plist
4287 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
9d7608b7
KH
4288 found:
4289 if (! NILP (buffer))
4290 {
4291 if (changed)
876aa27c 4292 modify_overlay (XBUFFER (buffer),
26f545d7
GM
4293 marker_position (OVERLAY_START (overlay)),
4294 marker_position (OVERLAY_END (overlay)));
9d7608b7
KH
4295 if (EQ (prop, Qevaporate) && ! NILP (value)
4296 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4297 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4298 Fdelete_overlay (overlay);
4299 }
7d63db98 4300
2eec3b4e 4301 return value;
1ab256cb
RM
4302}
4303\f
9115729e
KH
4304/* Subroutine of report_overlay_modification. */
4305
4306/* Lisp vector holding overlay hook functions to call.
4307 Vector elements come in pairs.
4308 Each even-index element is a list of hook functions.
4309 The following odd-index element is the overlay they came from.
4310
4311 Before the buffer change, we fill in this vector
4312 as we call overlay hook functions.
4313 After the buffer change, we get the functions to call from this vector.
4314 This way we always call the same functions before and after the change. */
4315static Lisp_Object last_overlay_modification_hooks;
4316
4317/* Number of elements actually used in last_overlay_modification_hooks. */
4318static int last_overlay_modification_hooks_used;
4319
4320/* Add one functionlist/overlay pair
4321 to the end of last_overlay_modification_hooks. */
4322
4323static void
4324add_overlay_mod_hooklist (functionlist, overlay)
4325 Lisp_Object functionlist, overlay;
4326{
4327 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4328
4329 if (last_overlay_modification_hooks_used == oldsize)
de0280a2
EZ
4330 last_overlay_modification_hooks = larger_vector
4331 (last_overlay_modification_hooks, oldsize * 2, Qnil);
3ae565b3
SM
4332 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4333 functionlist); last_overlay_modification_hooks_used++;
4334 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4335 overlay); last_overlay_modification_hooks_used++;
9115729e
KH
4336}
4337\f
173f2a64
RS
4338/* Run the modification-hooks of overlays that include
4339 any part of the text in START to END.
9115729e
KH
4340 If this change is an insertion, also
4341 run the insert-before-hooks of overlay starting at END,
930a9140
RS
4342 and the insert-after-hooks of overlay ending at START.
4343
4344 This is called both before and after the modification.
4345 AFTER is nonzero when we call after the modification.
4346
9115729e
KH
4347 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4348 When AFTER is nonzero, they are the start position,
4349 the position after the inserted new text,
4350 and the length of deleted or replaced old text. */
173f2a64
RS
4351
4352void
930a9140 4353report_overlay_modification (start, end, after, arg1, arg2, arg3)
173f2a64 4354 Lisp_Object start, end;
930a9140
RS
4355 int after;
4356 Lisp_Object arg1, arg2, arg3;
173f2a64 4357{
2410d73a
SM
4358 Lisp_Object prop, overlay;
4359 struct Lisp_Overlay *tail;
9115729e
KH
4360 /* 1 if this change is an insertion. */
4361 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
a615c6dc 4362 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
55b48893
RS
4363
4364 overlay = Qnil;
2410d73a 4365 tail = NULL;
9115729e 4366
27c6b98e
SM
4367 /* We used to run the functions as soon as we found them and only register
4368 them in last_overlay_modification_hooks for the purpose of the `after'
4369 case. But running elisp code as we traverse the list of overlays is
4370 painful because the list can be modified by the elisp code so we had to
4371 copy at several places. We now simply do a read-only traversal that
4372 only collects the functions to run and we run them afterwards. It's
4373 simpler, especially since all the code was already there. -stef */
4374
a615c6dc 4375 if (!after)
173f2a64 4376 {
a615c6dc
SM
4377 /* We are being called before a change.
4378 Scan the overlays to find the functions to call. */
4379 last_overlay_modification_hooks_used = 0;
2410d73a 4380 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
173f2a64 4381 {
a615c6dc
SM
4382 int startpos, endpos;
4383 Lisp_Object ostart, oend;
4384
2410d73a 4385 XSETMISC (overlay, tail);
a615c6dc
SM
4386
4387 ostart = OVERLAY_START (overlay);
4388 oend = OVERLAY_END (overlay);
4389 endpos = OVERLAY_POSITION (oend);
4390 if (XFASTINT (start) > endpos)
4391 break;
4392 startpos = OVERLAY_POSITION (ostart);
4393 if (insertion && (XFASTINT (start) == startpos
4394 || XFASTINT (end) == startpos))
5fb5aa33 4395 {
a615c6dc
SM
4396 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4397 if (!NILP (prop))
4398 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4399 }
a615c6dc
SM
4400 if (insertion && (XFASTINT (start) == endpos
4401 || XFASTINT (end) == endpos))
5fb5aa33 4402 {
a615c6dc
SM
4403 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4404 if (!NILP (prop))
4405 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4406 }
a615c6dc
SM
4407 /* Test for intersecting intervals. This does the right thing
4408 for both insertion and deletion. */
4409 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4410 {
a615c6dc
SM
4411 prop = Foverlay_get (overlay, Qmodification_hooks);
4412 if (!NILP (prop))
4413 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4414 }
173f2a64 4415 }
60ebfdf3 4416
2410d73a 4417 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
173f2a64 4418 {
a615c6dc
SM
4419 int startpos, endpos;
4420 Lisp_Object ostart, oend;
4421
2410d73a 4422 XSETMISC (overlay, tail);
a615c6dc
SM
4423
4424 ostart = OVERLAY_START (overlay);
4425 oend = OVERLAY_END (overlay);
4426 startpos = OVERLAY_POSITION (ostart);
4427 endpos = OVERLAY_POSITION (oend);
4428 if (XFASTINT (end) < startpos)
4429 break;
4430 if (insertion && (XFASTINT (start) == startpos
4431 || XFASTINT (end) == startpos))
5fb5aa33 4432 {
a615c6dc
SM
4433 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4434 if (!NILP (prop))
4435 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4436 }
a615c6dc
SM
4437 if (insertion && (XFASTINT (start) == endpos
4438 || XFASTINT (end) == endpos))
5fb5aa33 4439 {
a615c6dc
SM
4440 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4441 if (!NILP (prop))
4442 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4443 }
a615c6dc
SM
4444 /* Test for intersecting intervals. This does the right thing
4445 for both insertion and deletion. */
4446 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4447 {
a615c6dc
SM
4448 prop = Foverlay_get (overlay, Qmodification_hooks);
4449 if (!NILP (prop))
4450 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4451 }
173f2a64
RS
4452 }
4453 }
55b48893 4454
a615c6dc
SM
4455 GCPRO4 (overlay, arg1, arg2, arg3);
4456 {
4457 /* Call the functions recorded in last_overlay_modification_hooks.
4458 First copy the vector contents, in case some of these hooks
4459 do subsequent modification of the buffer. */
4460 int size = last_overlay_modification_hooks_used;
4461 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4462 int i;
4463
4464 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4465 copy, size * sizeof (Lisp_Object));
4466 gcpro1.var = copy;
4467 gcpro1.nvars = size;
4468
4469 for (i = 0; i < size;)
4470 {
4471 Lisp_Object prop, overlay;
4472 prop = copy[i++];
4473 overlay = copy[i++];
4474 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4475 }
4476 }
55b48893 4477 UNGCPRO;
173f2a64
RS
4478}
4479
4480static void
930a9140
RS
4481call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4482 Lisp_Object list, overlay;
4483 int after;
4484 Lisp_Object arg1, arg2, arg3;
173f2a64 4485{
930a9140 4486 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9115729e 4487
930a9140 4488 GCPRO4 (list, arg1, arg2, arg3);
9115729e 4489
6d70a280 4490 while (CONSP (list))
173f2a64 4491 {
930a9140 4492 if (NILP (arg3))
6d70a280 4493 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
930a9140 4494 else
6d70a280
SM
4495 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4496 list = XCDR (list);
173f2a64
RS
4497 }
4498 UNGCPRO;
4499}
9d7608b7
KH
4500
4501/* Delete any zero-sized overlays at position POS, if the `evaporate'
4502 property is set. */
4503void
4504evaporate_overlays (pos)
c2d5b10f 4505 EMACS_INT pos;
9d7608b7 4506{
2410d73a
SM
4507 Lisp_Object overlay, hit_list;
4508 struct Lisp_Overlay *tail;
9d7608b7
KH
4509
4510 hit_list = Qnil;
c2d5b10f 4511 if (pos <= current_buffer->overlay_center)
2410d73a 4512 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
9d7608b7
KH
4513 {
4514 int endpos;
2410d73a 4515 XSETMISC (overlay, tail);
9d7608b7
KH
4516 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4517 if (endpos < pos)
4518 break;
4519 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
c3935f9d 4520 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4521 hit_list = Fcons (overlay, hit_list);
4522 }
4523 else
2410d73a 4524 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
9d7608b7
KH
4525 {
4526 int startpos;
2410d73a 4527 XSETMISC (overlay, tail);
9d7608b7
KH
4528 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4529 if (startpos > pos)
4530 break;
4531 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
c3935f9d 4532 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4533 hit_list = Fcons (overlay, hit_list);
4534 }
7539e11f
KR
4535 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4536 Fdelete_overlay (XCAR (hit_list));
9d7608b7 4537}
173f2a64 4538\f
54dfdeb0 4539/* Somebody has tried to store a value with an unacceptable type
1bf08baf
KH
4540 in the slot with offset OFFSET. */
4541
0fa3ba92 4542void
ad97b375
SM
4543buffer_slot_type_mismatch (newval, type)
4544 Lisp_Object newval;
64e16c3c 4545 int type;
0fa3ba92 4546{
ad97b375 4547 Lisp_Object predicate;
177c0ea7 4548
64e16c3c 4549 switch (type)
0fa3ba92 4550 {
ad97b375
SM
4551 case Lisp_Int: predicate = Qintegerp; break;
4552 case Lisp_String: predicate = Qstringp; break;
4553 case Lisp_Symbol: predicate = Qsymbolp; break;
4554 default: abort ();
0fa3ba92
JB
4555 }
4556
ad97b375 4557 wrong_type_argument (predicate, newval);
0fa3ba92 4558}
7c02e886 4559
0fa3ba92 4560\f
b86af064
GM
4561/***********************************************************************
4562 Allocation with mmap
4563 ***********************************************************************/
4564
4565#ifdef USE_MMAP_FOR_BUFFERS
4566
4567#include <sys/types.h>
4568#include <sys/mman.h>
4569
4570#ifndef MAP_ANON
4571#ifdef MAP_ANONYMOUS
4572#define MAP_ANON MAP_ANONYMOUS
4573#else
4574#define MAP_ANON 0
4575#endif
4576#endif
4577
09dfdf85
GM
4578#ifndef MAP_FAILED
4579#define MAP_FAILED ((void *) -1)
4580#endif
4581
b86af064
GM
4582#include <stdio.h>
4583#include <errno.h>
4584
4585#if MAP_ANON == 0
4586#include <fcntl.h>
4587#endif
4588
4589#include "coding.h"
4590
4591
4592/* Memory is allocated in regions which are mapped using mmap(2).
4593 The current implementation lets the system select mapped
4594 addresses; we're not using MAP_FIXED in general, except when
4595 trying to enlarge regions.
4596
4597 Each mapped region starts with a mmap_region structure, the user
4598 area starts after that structure, aligned to MEM_ALIGN.
4599
4600 +-----------------------+
4601 | struct mmap_info + |
4602 | padding |
4603 +-----------------------+
4604 | user data |
4605 | |
4606 | |
4607 +-----------------------+ */
4608
4609struct mmap_region
4610{
4611 /* User-specified size. */
4612 size_t nbytes_specified;
177c0ea7 4613
b86af064
GM
4614 /* Number of bytes mapped */
4615 size_t nbytes_mapped;
4616
4617 /* Pointer to the location holding the address of the memory
4618 allocated with the mmap'd block. The variable actually points
4619 after this structure. */
4620 POINTER_TYPE **var;
4621
4622 /* Next and previous in list of all mmap'd regions. */
4623 struct mmap_region *next, *prev;
4624};
4625
4626/* Doubly-linked list of mmap'd regions. */
4627
4628static struct mmap_region *mmap_regions;
4629
4630/* File descriptor for mmap. If we don't have anonymous mapping,
4631 /dev/zero will be opened on it. */
4632
4633static int mmap_fd;
4634
4635/* Temporary storage for mmap_set_vars, see there. */
4636
4637static struct mmap_region *mmap_regions_1;
4638static int mmap_fd_1;
4639
4640/* Page size on this system. */
4641
4642static int mmap_page_size;
4643
4644/* 1 means mmap has been intialized. */
4645
4646static int mmap_initialized_p;
4647
4648/* Value is X rounded up to the next multiple of N. */
4649
4650#define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4651
4652/* Size of mmap_region structure plus padding. */
4653
4654#define MMAP_REGION_STRUCT_SIZE \
4655 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4656
4657/* Given a pointer P to the start of the user-visible part of a mapped
4658 region, return a pointer to the start of the region. */
4659
4660#define MMAP_REGION(P) \
4661 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4662
4663/* Given a pointer P to the start of a mapped region, return a pointer
4664 to the start of the user-visible part of the region. */
4665
4666#define MMAP_USER_AREA(P) \
4667 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4668
4669#define MEM_ALIGN sizeof (double)
4670
7273faa1
DL
4671/* Predicate returning true if part of the address range [START .. END]
4672 is currently mapped. Used to prevent overwriting an existing
08327b22
GM
4673 memory mapping.
4674
4675 Default is to conservativly assume the address range is occupied by
4676 something else. This can be overridden by system configuration
4677 files if system-specific means to determine this exists. */
4678
4679#ifndef MMAP_ALLOCATED_P
4680#define MMAP_ALLOCATED_P(start, end) 1
4681#endif
4682
b86af064
GM
4683/* Function prototypes. */
4684
4685static int mmap_free_1 P_ ((struct mmap_region *));
4686static int mmap_enlarge P_ ((struct mmap_region *, int));
4687static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4688static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4689static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4690static void mmap_free P_ ((POINTER_TYPE **ptr));
4691static void mmap_init P_ ((void));
4692
4693
4694/* Return a region overlapping address range START...END, or null if
4695 none. END is not including, i.e. the last byte in the range
4696 is at END - 1. */
4697
4698static struct mmap_region *
4699mmap_find (start, end)
4700 POINTER_TYPE *start, *end;
4701{
4702 struct mmap_region *r;
4703 char *s = (char *) start, *e = (char *) end;
177c0ea7 4704
b86af064
GM
4705 for (r = mmap_regions; r; r = r->next)
4706 {
4707 char *rstart = (char *) r;
4708 char *rend = rstart + r->nbytes_mapped;
4709
4710 if (/* First byte of range, i.e. START, in this region? */
4711 (s >= rstart && s < rend)
4712 /* Last byte of range, i.e. END - 1, in this region? */
4713 || (e > rstart && e <= rend)
4714 /* First byte of this region in the range? */
4715 || (rstart >= s && rstart < e)
4716 /* Last byte of this region in the range? */
4717 || (rend > s && rend <= e))
4718 break;
4719 }
4720
4721 return r;
4722}
4723
4724
4725/* Unmap a region. P is a pointer to the start of the user-araa of
4726 the region. Value is non-zero if successful. */
4727
4728static int
4729mmap_free_1 (r)
4730 struct mmap_region *r;
4731{
4732 if (r->next)
4733 r->next->prev = r->prev;
4734 if (r->prev)
4735 r->prev->next = r->next;
4736 else
4737 mmap_regions = r->next;
177c0ea7 4738
1a15cca0 4739 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
b86af064
GM
4740 {
4741 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4742 return 0;
4743 }
4744
4745 return 1;
4746}
4747
4748
4749/* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4750 Value is non-zero if successful. */
4751
4752static int
4753mmap_enlarge (r, npages)
4754 struct mmap_region *r;
4755 int npages;
4756{
4757 char *region_end = (char *) r + r->nbytes_mapped;
4758 size_t nbytes;
4759 int success = 0;
4760
4761 if (npages < 0)
4762 {
4763 /* Unmap pages at the end of the region. */
4764 nbytes = - npages * mmap_page_size;
4765 if (munmap (region_end - nbytes, nbytes) == -1)
4766 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4767 else
4768 {
4769 r->nbytes_mapped -= nbytes;
4770 success = 1;
4771 }
4772 }
4773 else if (npages > 0)
4774 {
b86af064 4775 nbytes = npages * mmap_page_size;
177c0ea7 4776
b86af064
GM
4777 /* Try to map additional pages at the end of the region. We
4778 cannot do this if the address range is already occupied by
4779 something else because mmap deletes any previous mapping.
4780 I'm not sure this is worth doing, let's see. */
08327b22 4781 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
b86af064
GM
4782 {
4783 POINTER_TYPE *p;
177c0ea7 4784
b86af064
GM
4785 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4786 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4787 if (p == MAP_FAILED)
edaa9aed 4788 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
b86af064
GM
4789 else if (p != (POINTER_TYPE *) region_end)
4790 {
4791 /* Kernels are free to choose a different address. In
4792 that case, unmap what we've mapped above; we have
4793 no use for it. */
4794 if (munmap (p, nbytes) == -1)
4795 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4796 }
4797 else
4798 {
4799 r->nbytes_mapped += nbytes;
4800 success = 1;
4801 }
4802 }
4803 }
4804
4805 return success;
4806}
4807
4808
4809/* Set or reset variables holding references to mapped regions. If
4810 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4811 non-zero, set all variables to the start of the user-areas
4812 of mapped regions.
4813
4814 This function is called from Fdump_emacs to ensure that the dumped
4815 Emacs doesn't contain references to memory that won't be mapped
4816 when Emacs starts. */
4817
4818void
4819mmap_set_vars (restore_p)
4820 int restore_p;
4821{
4822 struct mmap_region *r;
4823
4824 if (restore_p)
4825 {
4826 mmap_regions = mmap_regions_1;
4827 mmap_fd = mmap_fd_1;
4828 for (r = mmap_regions; r; r = r->next)
4829 *r->var = MMAP_USER_AREA (r);
4830 }
4831 else
4832 {
4833 for (r = mmap_regions; r; r = r->next)
4834 *r->var = NULL;
4835 mmap_regions_1 = mmap_regions;
4836 mmap_regions = NULL;
4837 mmap_fd_1 = mmap_fd;
4838 mmap_fd = -1;
4839 }
4840}
4841
4842
4843/* Allocate a block of storage large enough to hold NBYTES bytes of
4844 data. A pointer to the data is returned in *VAR. VAR is thus the
4845 address of some variable which will use the data area.
4846
4847 The allocation of 0 bytes is valid.
4848
4849 If we can't allocate the necessary memory, set *VAR to null, and
4850 return null. */
4851
4852static POINTER_TYPE *
4853mmap_alloc (var, nbytes)
4854 POINTER_TYPE **var;
4855 size_t nbytes;
4856{
4857 void *p;
4858 size_t map;
4859
4860 mmap_init ();
4861
4862 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4863 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4864 mmap_fd, 0);
177c0ea7 4865
b86af064
GM
4866 if (p == MAP_FAILED)
4867 {
4868 if (errno != ENOMEM)
4869 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4870 p = NULL;
4871 }
4872 else
4873 {
4874 struct mmap_region *r = (struct mmap_region *) p;
177c0ea7 4875
b86af064
GM
4876 r->nbytes_specified = nbytes;
4877 r->nbytes_mapped = map;
4878 r->var = var;
4879 r->prev = NULL;
4880 r->next = mmap_regions;
4881 if (r->next)
4882 r->next->prev = r;
4883 mmap_regions = r;
177c0ea7 4884
b86af064
GM
4885 p = MMAP_USER_AREA (p);
4886 }
177c0ea7 4887
b86af064
GM
4888 return *var = p;
4889}
4890
4891
4892/* Given a pointer at address VAR to data allocated with mmap_alloc,
4893 resize it to size NBYTES. Change *VAR to reflect the new block,
4894 and return this value. If more memory cannot be allocated, then
4895 leave *VAR unchanged, and return null. */
4896
4897static POINTER_TYPE *
4898mmap_realloc (var, nbytes)
4899 POINTER_TYPE **var;
4900 size_t nbytes;
4901{
4902 POINTER_TYPE *result;
177c0ea7 4903
b86af064
GM
4904 mmap_init ();
4905
4906 if (*var == NULL)
4907 result = mmap_alloc (var, nbytes);
177c0ea7 4908 else if (nbytes == 0)
b86af064
GM
4909 {
4910 mmap_free (var);
4911 result = mmap_alloc (var, nbytes);
4912 }
4913 else
4914 {
4915 struct mmap_region *r = MMAP_REGION (*var);
4916 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
177c0ea7 4917
b86af064
GM
4918 if (room < nbytes)
4919 {
4920 /* Must enlarge. */
4921 POINTER_TYPE *old_ptr = *var;
4922
4923 /* Try to map additional pages at the end of the region.
4924 If that fails, allocate a new region, copy data
4925 from the old region, then free it. */
4926 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4927 / mmap_page_size)))
4928 {
4929 r->nbytes_specified = nbytes;
4930 *var = result = old_ptr;
4931 }
4932 else if (mmap_alloc (var, nbytes))
4933 {
4934 bcopy (old_ptr, *var, r->nbytes_specified);
4935 mmap_free_1 (MMAP_REGION (old_ptr));
4936 result = *var;
4937 r = MMAP_REGION (result);
4938 r->nbytes_specified = nbytes;
4939 }
4940 else
4941 {
4942 *var = old_ptr;
4943 result = NULL;
4944 }
4945 }
4946 else if (room - nbytes >= mmap_page_size)
4947 {
4948 /* Shrinking by at least a page. Let's give some
6bcdeb8c
KR
4949 memory back to the system.
4950
4951 The extra parens are to make the division happens first,
4952 on positive values, so we know it will round towards
4953 zero. */
bb63c5c9 4954 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
b86af064
GM
4955 result = *var;
4956 r->nbytes_specified = nbytes;
4957 }
4958 else
4959 {
4960 /* Leave it alone. */
4961 result = *var;
4962 r->nbytes_specified = nbytes;
4963 }
4964 }
4965
4966 return result;
4967}
4968
4969
4970/* Free a block of relocatable storage whose data is pointed to by
4971 PTR. Store 0 in *PTR to show there's no block allocated. */
4972
4973static void
4974mmap_free (var)
4975 POINTER_TYPE **var;
4976{
4977 mmap_init ();
177c0ea7 4978
b86af064
GM
4979 if (*var)
4980 {
4981 mmap_free_1 (MMAP_REGION (*var));
4982 *var = NULL;
4983 }
4984}
4985
4986
4987/* Perform necessary intializations for the use of mmap. */
4988
4989static void
4990mmap_init ()
4991{
4992#if MAP_ANON == 0
4993 /* The value of mmap_fd is initially 0 in temacs, and -1
4994 in a dumped Emacs. */
4995 if (mmap_fd <= 0)
4996 {
4997 /* No anonymous mmap -- we need the file descriptor. */
4998 mmap_fd = open ("/dev/zero", O_RDONLY);
4999 if (mmap_fd == -1)
5000 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
5001 }
5002#endif /* MAP_ANON == 0 */
5003
5004 if (mmap_initialized_p)
5005 return;
5006 mmap_initialized_p = 1;
177c0ea7 5007
b86af064
GM
5008#if MAP_ANON != 0
5009 mmap_fd = -1;
5010#endif
177c0ea7 5011
b86af064
GM
5012 mmap_page_size = getpagesize ();
5013}
5014
5015#endif /* USE_MMAP_FOR_BUFFERS */
5016
5017
5018\f
5019/***********************************************************************
5020 Buffer-text Allocation
5021 ***********************************************************************/
5022
5023#ifdef REL_ALLOC
5024extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5025extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5026extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5027#endif /* REL_ALLOC */
5028
5029
5030/* Allocate NBYTES bytes for buffer B's text buffer. */
5031
5032static void
5033alloc_buffer_text (b, nbytes)
5034 struct buffer *b;
5035 size_t nbytes;
5036{
5037 POINTER_TYPE *p;
177c0ea7 5038
b86af064
GM
5039 BLOCK_INPUT;
5040#if defined USE_MMAP_FOR_BUFFERS
5041 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5042#elif defined REL_ALLOC
5043 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5044#else
815add84 5045 p = xmalloc (nbytes);
b86af064 5046#endif
177c0ea7 5047
b86af064
GM
5048 if (p == NULL)
5049 {
5050 UNBLOCK_INPUT;
5051 memory_full ();
5052 }
5053
5054 b->text->beg = (unsigned char *) p;
5055 UNBLOCK_INPUT;
5056}
5057
5058/* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5059 shrink it. */
5060
5061void
5371d722 5062enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
b86af064
GM
5063{
5064 POINTER_TYPE *p;
5065 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5066 + delta);
5067 BLOCK_INPUT;
5068#if defined USE_MMAP_FOR_BUFFERS
5069 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5070#elif defined REL_ALLOC
5071 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5072#else
5073 p = xrealloc (b->text->beg, nbytes);
5074#endif
177c0ea7 5075
b86af064
GM
5076 if (p == NULL)
5077 {
5078 UNBLOCK_INPUT;
5079 memory_full ();
5080 }
5081
5082 BUF_BEG_ADDR (b) = (unsigned char *) p;
5083 UNBLOCK_INPUT;
5084}
5085
5086
5087/* Free buffer B's text buffer. */
5088
5089static void
5090free_buffer_text (b)
5091 struct buffer *b;
5092{
5093 BLOCK_INPUT;
5094
5095#if defined USE_MMAP_FOR_BUFFERS
5096 mmap_free ((POINTER_TYPE **) &b->text->beg);
5097#elif defined REL_ALLOC
5098 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5099#else
5100 xfree (b->text->beg);
5101#endif
177c0ea7 5102
b86af064
GM
5103 BUF_BEG_ADDR (b) = NULL;
5104 UNBLOCK_INPUT;
5105}
5106
5107
5108\f
5109/***********************************************************************
5110 Initialization
5111 ***********************************************************************/
5112
dfcf069d 5113void
1ab256cb
RM
5114init_buffer_once ()
5115{
7c02e886
GM
5116 int idx;
5117
5118 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
13de9290 5119
1ab256cb
RM
5120 /* Make sure all markable slots in buffer_defaults
5121 are initialized reasonably, so mark_buffer won't choke. */
5122 reset_buffer (&buffer_defaults);
13de9290 5123 reset_buffer_local_variables (&buffer_defaults, 1);
1ab256cb 5124 reset_buffer (&buffer_local_symbols);
13de9290 5125 reset_buffer_local_variables (&buffer_local_symbols, 1);
336cd056
RS
5126 /* Prevent GC from getting confused. */
5127 buffer_defaults.text = &buffer_defaults.own_text;
5128 buffer_local_symbols.text = &buffer_local_symbols.own_text;
336cd056
RS
5129 BUF_INTERVALS (&buffer_defaults) = 0;
5130 BUF_INTERVALS (&buffer_local_symbols) = 0;
cf00e751 5131 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
67180c6a 5132 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
cf00e751 5133 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
67180c6a 5134 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
5135
5136 /* Set up the default values of various buffer slots. */
5137 /* Must do these before making the first buffer! */
5138
f532dca0 5139 /* real setup is done in bindings.el */
1ab256cb 5140 buffer_defaults.mode_line_format = build_string ("%-");
045dee35 5141 buffer_defaults.header_line_format = Qnil;
1ab256cb
RM
5142 buffer_defaults.abbrev_mode = Qnil;
5143 buffer_defaults.overwrite_mode = Qnil;
5144 buffer_defaults.case_fold_search = Qt;
5145 buffer_defaults.auto_fill_function = Qnil;
5146 buffer_defaults.selective_display = Qnil;
5147#ifndef old
5148 buffer_defaults.selective_display_ellipses = Qt;
5149#endif
5150 buffer_defaults.abbrev_table = Qnil;
5151 buffer_defaults.display_table = Qnil;
1ab256cb 5152 buffer_defaults.undo_list = Qnil;
c48f61ef 5153 buffer_defaults.mark_active = Qnil;
be9aafdd 5154 buffer_defaults.file_format = Qnil;
71ed49fa 5155 buffer_defaults.auto_save_file_format = Qt;
2410d73a
SM
5156 buffer_defaults.overlays_before = NULL;
5157 buffer_defaults.overlays_after = NULL;
c2d5b10f 5158 buffer_defaults.overlay_center = BEG;
1ab256cb 5159
8d7a4592 5160 XSETFASTINT (buffer_defaults.tab_width, 8);
1ab256cb 5161 buffer_defaults.truncate_lines = Qnil;
0858cd02 5162 buffer_defaults.word_wrap = Qnil;
1ab256cb 5163 buffer_defaults.ctl_arrow = Qt;
3b06f880 5164 buffer_defaults.direction_reversed = Qnil;
bb2ec976 5165 buffer_defaults.cursor_type = Qt;
a3bbced0 5166 buffer_defaults.extra_line_spacing = Qnil;
187ccf49 5167 buffer_defaults.cursor_in_non_selected_windows = Qt;
1ab256cb 5168
f7975d07 5169#ifdef DOS_NT
0776cb1b 5170 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
54ad07d3 5171#endif
a1a17b61 5172 buffer_defaults.enable_multibyte_characters = Qt;
c71b5d9b 5173 buffer_defaults.buffer_file_coding_system = Qnil;
8d7a4592
KH
5174 XSETFASTINT (buffer_defaults.fill_column, 70);
5175 XSETFASTINT (buffer_defaults.left_margin, 0);
28e969dd 5176 buffer_defaults.cache_long_line_scans = Qnil;
f6ed2e84 5177 buffer_defaults.file_truename = Qnil;
7962a441 5178 XSETFASTINT (buffer_defaults.display_count, 0);
2ad8731a
KS
5179 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5180 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5181 buffer_defaults.left_fringe_width = Qnil;
5182 buffer_defaults.right_fringe_width = Qnil;
5183 buffer_defaults.fringes_outside_margins = Qnil;
5184 buffer_defaults.scroll_bar_width = Qnil;
5185 buffer_defaults.vertical_scroll_bar_type = Qt;
0552666b 5186 buffer_defaults.indicate_empty_lines = Qnil;
6b61353c 5187 buffer_defaults.indicate_buffer_boundaries = Qnil;
c6a46372
KS
5188 buffer_defaults.fringe_indicator_alist = Qnil;
5189 buffer_defaults.fringe_cursor_alist = Qnil;
0552666b
GM
5190 buffer_defaults.scroll_up_aggressively = Qnil;
5191 buffer_defaults.scroll_down_aggressively = Qnil;
3fd364db 5192 buffer_defaults.display_time = Qnil;
1ab256cb
RM
5193
5194 /* Assign the local-flags to the slots that have default values.
5195 The local flag is a bit that is used in the buffer
5196 to say that it has its own local value for the slot.
5197 The local flag bits are in the local_var_flags slot of the buffer. */
5198
5199 /* Nothing can work if this isn't true */
4d2f1389 5200 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
1ab256cb
RM
5201
5202 /* 0 means not a lisp var, -1 means always local, else mask */
5203 bzero (&buffer_local_flags, sizeof buffer_local_flags);
aab80822
KH
5204 XSETINT (buffer_local_flags.filename, -1);
5205 XSETINT (buffer_local_flags.directory, -1);
5206 XSETINT (buffer_local_flags.backed_up, -1);
5207 XSETINT (buffer_local_flags.save_length, -1);
5208 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5209 XSETINT (buffer_local_flags.read_only, -1);
5210 XSETINT (buffer_local_flags.major_mode, -1);
5211 XSETINT (buffer_local_flags.mode_name, -1);
5212 XSETINT (buffer_local_flags.undo_list, -1);
5213 XSETINT (buffer_local_flags.mark_active, -1);
943e065b 5214 XSETINT (buffer_local_flags.point_before_scroll, -1);
f6ed2e84 5215 XSETINT (buffer_local_flags.file_truename, -1);
3cb719bd 5216 XSETINT (buffer_local_flags.invisibility_spec, -1);
55ac8536 5217 XSETINT (buffer_local_flags.file_format, -1);
71ed49fa 5218 XSETINT (buffer_local_flags.auto_save_file_format, -1);
7962a441 5219 XSETINT (buffer_local_flags.display_count, -1);
3fd364db 5220 XSETINT (buffer_local_flags.display_time, -1);
1bf08baf 5221 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
8d7a4592 5222
7c02e886
GM
5223 idx = 1;
5224 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5225 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5226 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5227 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5228 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5229 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
1ab256cb 5230#ifndef old
7c02e886 5231 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
1ab256cb 5232#endif
7c02e886
GM
5233 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5234 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
0858cd02 5235 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
7c02e886
GM
5236 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5237 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5238 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5239 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5240 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
f7975d07 5241#ifdef DOS_NT
7c02e886 5242 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
13de9290 5243 /* Make this one a permanent local. */
7c02e886 5244 buffer_permanent_local_flags[idx++] = 1;
54ad07d3 5245#endif
7c02e886
GM
5246 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5247 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5248 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5249 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
177c0ea7 5250 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
a1a17b61 5251 /* Make this one a permanent local. */
7c02e886 5252 buffer_permanent_local_flags[idx++] = 1;
2ad8731a
KS
5253 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5254 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5255 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5256 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5257 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5258 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5259 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
7c02e886 5260 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
6b61353c 5261 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
c6a46372
KS
5262 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5263 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
7c02e886
GM
5264 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5265 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5266 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
bd96bd79 5267 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
a3bbced0 5268 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
0124c5bd 5269 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
7c02e886
GM
5270
5271 /* Need more room? */
7313acd0 5272 if (idx >= MAX_PER_BUFFER_VARS)
7c02e886 5273 abort ();
7313acd0 5274 last_per_buffer_idx = idx;
177c0ea7 5275
1ab256cb
RM
5276 Vbuffer_alist = Qnil;
5277 current_buffer = 0;
5278 all_buffers = 0;
5279
5280 QSFundamental = build_string ("Fundamental");
5281
5282 Qfundamental_mode = intern ("fundamental-mode");
5283 buffer_defaults.major_mode = Qfundamental_mode;
5284
5285 Qmode_class = intern ("mode-class");
5286
5287 Qprotected_field = intern ("protected-field");
5288
5289 Qpermanent_local = intern ("permanent-local");
5290
5291 Qkill_buffer_hook = intern ("kill-buffer-hook");
fd6cfe11 5292 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
1ab256cb 5293
48265e61
DL
5294 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5295
1ab256cb 5296 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
000f8083 5297
1ab256cb
RM
5298 /* super-magic invisible buffer */
5299 Vbuffer_alist = Qnil;
5300
ffd56f97 5301 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
7775635d
KH
5302
5303 inhibit_modification_hooks = 0;
1ab256cb
RM
5304}
5305
dfcf069d 5306void
1ab256cb
RM
5307init_buffer ()
5308{
2381d133 5309 char *pwd;
136351b7 5310 Lisp_Object temp;
b639c9be 5311 int len;
1ab256cb 5312
b86af064 5313#ifdef USE_MMAP_FOR_BUFFERS
93c27ef1
GM
5314 {
5315 /* When using the ralloc implementation based on mmap(2), buffer
5316 text pointers will have been set to null in the dumped Emacs.
5317 Map new memory. */
5318 struct buffer *b;
177c0ea7 5319
93c27ef1
GM
5320 for (b = all_buffers; b; b = b->next)
5321 if (b->text->beg == NULL)
b86af064 5322 enlarge_buffer_text (b, 0);
93c27ef1 5323 }
b86af064 5324#endif /* USE_MMAP_FOR_BUFFERS */
177c0ea7 5325
1ab256cb 5326 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3d871c85
RS
5327 if (NILP (buffer_defaults.enable_multibyte_characters))
5328 Fset_buffer_multibyte (Qnil);
2381d133 5329
01537133 5330 pwd = get_current_dir_name ();
a17b5ed1 5331
156bdb41 5332 if (!pwd)
a17b5ed1 5333 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
1ab256cb 5334
1ab256cb
RM
5335 /* Maybe this should really use some standard subroutine
5336 whose definition is filename syntax dependent. */
b639c9be
RF
5337 len = strlen (pwd);
5338 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
f7975d07 5339 {
156bdb41 5340 /* Grow buffer to add directory separator and '\0'. */
b639c9be
RF
5341 pwd = (char *) xrealloc (pwd, len + 2);
5342 pwd[len] = DIRECTORY_SEP;
5343 pwd[len + 1] = '\0';
f7975d07 5344 }
0995fa35 5345
01537133 5346 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
dcd74c5f 5347 if (! NILP (buffer_defaults.enable_multibyte_characters))
f9962371 5348 /* At this moment, we still don't know how to decode the
156bdb41 5349 directory name. So, we keep the bytes in multibyte form so
dcd74c5f
KH
5350 that ENCODE_FILE correctly gets the original bytes. */
5351 current_buffer->directory
5352 = string_to_multibyte (current_buffer->directory);
136351b7 5353
0995fa35
RS
5354 /* Add /: to the front of the name
5355 if it would otherwise be treated as magic. */
5356 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
81ab2e07
KH
5357 if (! NILP (temp)
5358 /* If the default dir is just /, TEMP is non-nil
5359 because of the ange-ftp completion handler.
5360 However, it is not necessary to turn / into /:/.
5361 So avoid doing that. */
d5db4077 5362 && strcmp ("/", SDATA (current_buffer->directory)))
0995fa35
RS
5363 current_buffer->directory
5364 = concat2 (build_string ("/:"), current_buffer->directory);
5365
136351b7
RS
5366 temp = get_minibuffer (0);
5367 XBUFFER (temp)->directory = current_buffer->directory;
01537133
EZ
5368
5369 free (pwd);
1ab256cb
RM
5370}
5371
d6aa1876
SM
5372/* Similar to defvar_lisp but define a variable whose value is the Lisp
5373 Object stored in the current buffer. address is the address of the slot
5374 in the buffer that is current now. */
5375
5376/* TYPE is nil for a general Lisp variable.
5377 An integer specifies a type; then only LIsp values
5378 with that type code are allowed (except that nil is allowed too).
5379 LNAME is the LIsp-level variable name.
5380 VNAME is the name of the buffer slot.
5381 DOC is a dummy where you write the doc string as a comment. */
5382#define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5383 defvar_per_buffer (lname, vname, type, 0)
5384
5385static void
5386defvar_per_buffer (namestring, address, type, doc)
5387 char *namestring;
5388 Lisp_Object *address;
5389 Lisp_Object type;
5390 char *doc;
5391{
5392 Lisp_Object sym, val;
5393 int offset;
5394
5395 sym = intern (namestring);
5396 val = allocate_misc ();
5397 offset = (char *)address - (char *)current_buffer;
5398
5399 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5400 XBUFFER_OBJFWD (val)->offset = offset;
64e16c3c 5401 XBUFFER_OBJFWD (val)->slottype = type;
d6aa1876
SM
5402 SET_SYMBOL_VALUE (sym, val);
5403 PER_BUFFER_SYMBOL (offset) = sym;
d6aa1876
SM
5404
5405 if (PER_BUFFER_IDX (offset) == 0)
5406 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5407 slot of buffer_local_flags */
5408 abort ();
5409}
5410
5411
1ab256cb 5412/* initialize the buffer routines */
dfcf069d 5413void
1ab256cb
RM
5414syms_of_buffer ()
5415{
9115729e
KH
5416 staticpro (&last_overlay_modification_hooks);
5417 last_overlay_modification_hooks
5418 = Fmake_vector (make_number (10), Qnil);
5419
1ab256cb
RM
5420 staticpro (&Vbuffer_defaults);
5421 staticpro (&Vbuffer_local_symbols);
5422 staticpro (&Qfundamental_mode);
5423 staticpro (&Qmode_class);
5424 staticpro (&QSFundamental);
5425 staticpro (&Vbuffer_alist);
5426 staticpro (&Qprotected_field);
5427 staticpro (&Qpermanent_local);
2f7a359d
RS
5428 Qpermanent_local_hook = intern ("permanent-local-hook");
5429 staticpro (&Qpermanent_local_hook);
1ab256cb 5430 staticpro (&Qkill_buffer_hook);
22378665 5431 Qoverlayp = intern ("overlayp");
52f8ec73 5432 staticpro (&Qoverlayp);
9d7608b7
KH
5433 Qevaporate = intern ("evaporate");
5434 staticpro (&Qevaporate);
294d215f 5435 Qmodification_hooks = intern ("modification-hooks");
22378665 5436 staticpro (&Qmodification_hooks);
294d215f 5437 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
22378665 5438 staticpro (&Qinsert_in_front_hooks);
294d215f 5439 Qinsert_behind_hooks = intern ("insert-behind-hooks");
22378665 5440 staticpro (&Qinsert_behind_hooks);
5fe0b67e 5441 Qget_file_buffer = intern ("get-file-buffer");
22378665 5442 staticpro (&Qget_file_buffer);
5985d248
KH
5443 Qpriority = intern ("priority");
5444 staticpro (&Qpriority);
5445 Qwindow = intern ("window");
5446 staticpro (&Qwindow);
bbbe9545
KH
5447 Qbefore_string = intern ("before-string");
5448 staticpro (&Qbefore_string);
5449 Qafter_string = intern ("after-string");
5450 staticpro (&Qafter_string);
22378665
RS
5451 Qfirst_change_hook = intern ("first-change-hook");
5452 staticpro (&Qfirst_change_hook);
5453 Qbefore_change_functions = intern ("before-change-functions");
5454 staticpro (&Qbefore_change_functions);
5455 Qafter_change_functions = intern ("after-change-functions");
5456 staticpro (&Qafter_change_functions);
2f7a359d 5457 /* The next one is initialized in init_buffer_once. */
48265e61 5458 staticpro (&Qucs_set_table_for_input);
1ab256cb 5459
5b20caf0
RS
5460 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5461 staticpro (&Qkill_buffer_query_functions);
5462
1ab256cb
RM
5463 Fput (Qprotected_field, Qerror_conditions,
5464 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5465 Fput (Qprotected_field, Qerror_message,
5466 build_string ("Attempt to modify a protected field"));
5467
5468 /* All these use DEFVAR_LISP_NOPRO because the slots in
5469 buffer_defaults will all be marked via Vbuffer_defaults. */
5470
5471 DEFVAR_LISP_NOPRO ("default-mode-line-format",
7ee72033
MB
5472 &buffer_defaults.mode_line_format,
5473 doc: /* Default value of `mode-line-format' for buffers that don't override it.
018ba359 5474This is the same as (default-value 'mode-line-format). */);
1ab256cb 5475
045dee35 5476 DEFVAR_LISP_NOPRO ("default-header-line-format",
7ee72033
MB
5477 &buffer_defaults.header_line_format,
5478 doc: /* Default value of `header-line-format' for buffers that don't override it.
018ba359 5479This is the same as (default-value 'header-line-format). */);
0552666b 5480
7ee72033
MB
5481 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5482 doc: /* Default value of `cursor-type' for buffers that don't override it.
018ba359 5483This is the same as (default-value 'cursor-type). */);
bd96bd79 5484
a3bbced0 5485 DEFVAR_LISP_NOPRO ("default-line-spacing",
7ee72033
MB
5486 &buffer_defaults.extra_line_spacing,
5487 doc: /* Default value of `line-spacing' for buffers that don't override it.
018ba359 5488This is the same as (default-value 'line-spacing). */);
a3bbced0 5489
187ccf49
KS
5490 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5491 &buffer_defaults.cursor_in_non_selected_windows,
5492 doc: /* Default value of `cursor-in-non-selected-windows'.
5493This is the same as (default-value 'cursor-in-non-selected-windows). */);
5494
1ab256cb 5495 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
efc7e75f 5496 &buffer_defaults.abbrev_mode,
7ee72033 5497 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
018ba359 5498This is the same as (default-value 'abbrev-mode). */);
1ab256cb
RM
5499
5500 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
efc7e75f 5501 &buffer_defaults.ctl_arrow,
7ee72033 5502 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
018ba359 5503This is the same as (default-value 'ctl-arrow). */);
1ab256cb 5504
5e2ad10b
JB
5505 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5506 &buffer_defaults.direction_reversed,
5507 doc: /* Default value of `direction-reversed' for buffers that do not override it.
018ba359 5508This is the same as (default-value 'direction-reversed). */);
177c0ea7 5509
5e2ad10b
JB
5510 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5511 &buffer_defaults.enable_multibyte_characters,
5512 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
018ba359 5513This is the same as (default-value 'enable-multibyte-characters). */);
177c0ea7 5514
5e2ad10b
JB
5515 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5516 &buffer_defaults.buffer_file_coding_system,
5517 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
018ba359 5518This is the same as (default-value 'buffer-file-coding-system). */);
177c0ea7 5519
1ab256cb 5520 DEFVAR_LISP_NOPRO ("default-truncate-lines",
efc7e75f
PJ
5521 &buffer_defaults.truncate_lines,
5522 doc: /* Default value of `truncate-lines' for buffers that do not override it.
018ba359 5523This is the same as (default-value 'truncate-lines). */);
1ab256cb
RM
5524
5525 DEFVAR_LISP_NOPRO ("default-fill-column",
efc7e75f
PJ
5526 &buffer_defaults.fill_column,
5527 doc: /* Default value of `fill-column' for buffers that do not override it.
018ba359 5528This is the same as (default-value 'fill-column). */);
1ab256cb
RM
5529
5530 DEFVAR_LISP_NOPRO ("default-left-margin",
efc7e75f
PJ
5531 &buffer_defaults.left_margin,
5532 doc: /* Default value of `left-margin' for buffers that do not override it.
018ba359 5533This is the same as (default-value 'left-margin). */);
1ab256cb
RM
5534
5535 DEFVAR_LISP_NOPRO ("default-tab-width",
bc0ede35 5536 &buffer_defaults.tab_width,
23625946 5537 doc: /* Default value of `tab-width' for buffers that do not override it.
018ba359 5538This is the same as (default-value 'tab-width). */);
1ab256cb
RM
5539
5540 DEFVAR_LISP_NOPRO ("default-case-fold-search",
efc7e75f
PJ
5541 &buffer_defaults.case_fold_search,
5542 doc: /* Default value of `case-fold-search' for buffers that don't override it.
018ba359 5543This is the same as (default-value 'case-fold-search). */);
1ab256cb 5544
f7975d07 5545#ifdef DOS_NT
177c0ea7 5546 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
7ee72033 5547 &buffer_defaults.buffer_file_type,
efc7e75f 5548 doc: /* Default file type for buffers that do not override it.
018ba359
PJ
5549This is the same as (default-value 'buffer-file-type).
5550The file type is nil for text, t for binary. */);
54ad07d3
RS
5551#endif
5552
0552666b 5553 DEFVAR_LISP_NOPRO ("default-left-margin-width",
2ad8731a 5554 &buffer_defaults.left_margin_cols,
efc7e75f 5555 doc: /* Default value of `left-margin-width' for buffers that don't override it.
018ba359 5556This is the same as (default-value 'left-margin-width). */);
0552666b
GM
5557
5558 DEFVAR_LISP_NOPRO ("default-right-margin-width",
2ad8731a
KS
5559 &buffer_defaults.right_margin_cols,
5560 doc: /* Default value of `right-margin-width' for buffers that don't override it.
018ba359 5561This is the same as (default-value 'right-margin-width). */);
177c0ea7 5562
2ad8731a
KS
5563 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5564 &buffer_defaults.left_fringe_width,
5565 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5566This is the same as (default-value 'left-fringe-width). */);
5567
5568 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5569 &buffer_defaults.right_fringe_width,
5570 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5571This is the same as (default-value 'right-fringe-width). */);
5572
5573 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5574 &buffer_defaults.fringes_outside_margins,
5575 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5576This is the same as (default-value 'fringes-outside-margins). */);
5577
5578 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5579 &buffer_defaults.scroll_bar_width,
5580 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5581This is the same as (default-value 'scroll-bar-width). */);
5582
5583 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5584 &buffer_defaults.vertical_scroll_bar_type,
5585 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5586This is the same as (default-value 'vertical-scroll-bar). */);
5587
0552666b 5588 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
efc7e75f
PJ
5589 &buffer_defaults.indicate_empty_lines,
5590 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
018ba359 5591This is the same as (default-value 'indicate-empty-lines). */);
177c0ea7 5592
6b61353c
KH
5593 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5594 &buffer_defaults.indicate_buffer_boundaries,
5595 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5596This is the same as (default-value 'indicate-buffer-boundaries). */);
5597
c6a46372
KS
5598 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5599 &buffer_defaults.fringe_indicator_alist,
5600 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5601This is the same as (default-value 'fringe-indicator-alist'). */);
5602
5603 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5604 &buffer_defaults.fringe_cursor_alist,
5605 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5606This is the same as (default-value 'fringe-cursor-alist'). */);
5607
0552666b 5608 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
efc7e75f 5609 &buffer_defaults.scroll_up_aggressively,
7614d762
RS
5610 doc: /* Default value of `scroll-up-aggressively'.
5611This value applies in buffers that don't have their own local values.
fc961256 5612This is the same as (default-value 'scroll-up-aggressively). */);
177c0ea7 5613
0552666b 5614 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
efc7e75f 5615 &buffer_defaults.scroll_down_aggressively,
7614d762
RS
5616 doc: /* Default value of `scroll-down-aggressively'.
5617This value applies in buffers that don't have their own local values.
fc961256 5618This is the same as (default-value 'scroll-down-aggressively). */);
177c0ea7 5619
045dee35 5620 DEFVAR_PER_BUFFER ("header-line-format",
018ba359 5621 &current_buffer->header_line_format,
7ee72033 5622 Qnil,
7614d762
RS
5623 doc: /* Analogous to `mode-line-format', but controls the header line.
5624The header line appears, optionally, at the top of a window;
5625the mode line appears at the bottom. */);
177c0ea7 5626
1ab256cb 5627 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
efc7e75f
PJ
5628 Qnil,
5629 doc: /* Template for displaying mode line for current buffer.
018ba359
PJ
5630Each buffer has its own value of this variable.
5631Value may be nil, a string, a symbol or a list or cons cell.
5632A value of nil means don't display a mode line.
5633For a symbol, its value is used (but it is ignored if t or nil).
5634 A string appearing directly as the value of a symbol is processed verbatim
5635 in that the %-constructs below are not recognized.
ed463255 5636 Note that unless the symbol is marked as a `risky-local-variable', all
177c0ea7 5637 properties in any strings, as well as all :eval and :propertize forms
ed463255 5638 in the value of that symbol will be ignored.
018ba359 5639For a list of the form `(:eval FORM)', FORM is evaluated and the result
9c3eecf3
RS
5640 is used as a mode line element. Be careful--FORM should not load any files,
5641 because that can cause an infinite recursion.
ed463255
KS
5642For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5643 with the specified properties PROPS applied.
018ba359
PJ
5644For a list whose car is a symbol, the symbol's value is taken,
5645 and if that is non-nil, the cadr of the list is processed recursively.
5646 Otherwise, the caddr of the list (if there is one) is processed.
5647For a list whose car is a string or list, each element is processed
5648 recursively and the results are effectively concatenated.
5649For a list whose car is an integer, the cdr of the list is processed
5650 and padded (if the number is positive) or truncated (if negative)
5651 to the width specified by that number.
5652A string is printed verbatim in the mode line except for %-constructs:
5653 (%-constructs are allowed when the string is the entire mode-line-format
5654 or when it is found in a cons-cell or a list)
5655 %b -- print buffer name. %f -- print visited file name.
5656 %F -- print frame name.
5657 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5658 %& is like %*, but ignore read-only-ness.
5659 % means buffer is read-only and * means it is modified.
5660 For a modified read-only buffer, %* gives % and %+ gives *.
5661 %s -- print process status. %l -- print the current line number.
5662 %c -- print the current column number (this makes editing slower).
5663 To make the column number update correctly in all cases,
5664 `column-number-mode' must be non-nil.
6b61353c
KH
5665 %i -- print the size of the buffer.
5666 %I -- like %i, but use k, M, G, etc., to abbreviate.
018ba359
PJ
5667 %p -- print percent of buffer above top of window, or Top, Bot or All.
5668 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5669 or print Bottom or All.
018ba359 5670 %n -- print Narrow if appropriate.
dafbe726 5671 %t -- visited file is text or binary (if OS supports this distinction).
47419860 5672 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
018ba359 5673 %Z -- like %z, but including the end-of-line format.
dafbe726 5674 %e -- print error message about full memory.
f7165034
NR
5675 %@ -- print @ or hyphen. @ means that default-directory is on a
5676 remote machine.
018ba359
PJ
5677 %[ -- print one [ for each recursive editing level. %] similar.
5678 %% -- print %. %- -- print infinitely many dashes.
5679Decimal digits after the % specify field width to which to pad. */);
5680
7ee72033 5681 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
efc7e75f 5682 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
0493af6f
JB
5683A value of nil means use current buffer's major mode,
5684provided it is not marked as "special".
557ca2c1
RS
5685
5686When a mode is used by default, `find-file' switches to it
5687before it reads the contents into the buffer and before
5688it finishes setting up the buffer. Thus, the mode and
5689its hooks should not expect certain variables such as
5690`buffer-read-only' and `buffer-file-coding-system' to be set up. */);
1ab256cb
RM
5691
5692 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
7ee72033 5693 make_number (Lisp_Symbol),
efc7e75f 5694 doc: /* Symbol for current buffer's major mode. */);
1ab256cb
RM
5695
5696 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
c01d0677 5697 Qnil,
64a7c220 5698 doc: /* Pretty name of current buffer's major mode.
d224e8c2
GM
5699Usually a string. See `mode-line-format' for other possible forms.
5700Use the function `format-mode-line' to get the value as a string. */);
1ab256cb 5701
d6aa1876
SM
5702 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5703 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5704
7ee72033 5705 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
efc7e75f 5706 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
1ab256cb
RM
5707
5708 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
7ee72033 5709 Qnil,
efc7e75f 5710 doc: /* *Non-nil if searches and matches should ignore case. */);
1ab256cb
RM
5711
5712 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
7ee72033 5713 make_number (Lisp_Int),
1ac5826d 5714 doc: /* *Column beyond which automatic line-wrapping should happen.
f1ccb329 5715Interactively, you can set the buffer local value using \\[set-fill-column]. */);
1ab256cb
RM
5716
5717 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
7ee72033 5718 make_number (Lisp_Int),
fc961256 5719 doc: /* *Column for the default `indent-line-function' to indent to.
018ba359 5720Linefeed indents to this column in Fundamental mode. */);
1ab256cb
RM
5721
5722 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
7ee72033 5723 make_number (Lisp_Int),
efc7e75f 5724 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
1ab256cb 5725
7ee72033 5726 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
efc7e75f 5727 doc: /* *Non-nil means display control chars with uparrow.
018ba359
PJ
5728A value of nil means use backslash and octal digits.
5729This variable does not apply to characters whose display is specified
5730in the current display table (if there is one). */);
1ab256cb 5731
3b06f880 5732 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
1bf08baf 5733 &current_buffer->enable_multibyte_characters,
a9b9a780 5734 Qnil,
efc7e75f 5735 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
018ba359
PJ
5736Otherwise they are regarded as unibyte. This affects the display,
5737file I/O and the behavior of various editing commands.
5738
5739This variable is buffer-local but you cannot set it directly;
5740use the function `set-buffer-multibyte' to change a buffer's representation.
5741Changing its default value with `setq-default' is supported.
5742See also variable `default-enable-multibyte-characters' and Info node
5743`(elisp)Text Representations'. */);
a9b9a780 5744 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
3b06f880 5745
c71b5d9b 5746 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
7ee72033 5747 &current_buffer->buffer_file_coding_system, Qnil,
efc7e75f 5748 doc: /* Coding system to be used for encoding the buffer contents on saving.
018ba359
PJ
5749This variable applies to saving the buffer, and also to `write-region'
5750and other functions that use `write-region'.
5751It does not apply to sending output to subprocesses, however.
5752
5753If this is nil, the buffer is saved without any code conversion
5754unless some coding system is specified in `file-coding-system-alist'
5755for the buffer file.
5756
31a6cb06
EZ
5757If the text to be saved cannot be encoded as specified by this variable,
5758an alternative encoding is selected by `select-safe-coding-system', which see.
5759
018ba359
PJ
5760The variable `coding-system-for-write', if non-nil, overrides this variable.
5761
5762This variable is never applied to a way of decoding a file while reading it. */);
c71b5d9b 5763
3b06f880 5764 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
7ee72033 5765 Qnil,
efc7e75f 5766 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
3b06f880 5767
7ee72033 5768 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
7614d762
RS
5769 doc: /* *Non-nil means do not display continuation lines.
5770Instead, give each line of text just one screen line.
018ba359
PJ
5771
5772Note that this is overridden by the variable
5773`truncate-partial-width-windows' if that variable is non-nil
5774and this buffer is not full-frame width. */);
1ab256cb 5775
0858cd02
CY
5776 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5777 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5778When word-wrapping is on, continuation lines are wrapped at the space
5779or tab character nearest to the right window edge.
5780If nil, continuation lines are wrapped at the right screen edge.
5781
5782This variable has no effect if long lines are truncated (see
eb577e27
GM
5783`truncate-lines' and `truncate-partial-width-windows'). If you use
5784word-wrapping, you might want to reduce the value of
5785`truncate-partial-width-windows', since wrapping can make text readable
5786in narrower windows. */);
0858cd02 5787
f7975d07 5788#ifdef DOS_NT
54ad07d3 5789 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
7ee72033 5790 Qnil,
efc7e75f 5791 doc: /* Non-nil if the visited file is a binary file.
018ba359
PJ
5792This variable is meaningful on MS-DOG and Windows NT.
5793On those systems, it is automatically local in every buffer.
5794On other systems, this variable is normally always nil. */);
54ad07d3
RS
5795#endif
5796
1ab256cb 5797 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
7ee72033 5798 make_number (Lisp_String),
efc7e75f 5799 doc: /* Name of default directory of current buffer. Should end with slash.
018ba359 5800To interactively change the default directory, use command `cd'. */);
1ab256cb
RM
5801
5802 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
7ee72033 5803 Qnil,
efc7e75f 5804 doc: /* Function called (if non-nil) to perform auto-fill.
018ba359
PJ
5805It is called after self-inserting any character specified in
5806the `auto-fill-chars' table.
5807NOTE: This variable is not a hook;
5808its value may not be a list of functions. */);
1ab256cb
RM
5809
5810 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
7ee72033 5811 make_number (Lisp_String),
efc7e75f 5812 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
1ab256cb 5813
f6ed2e84 5814 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
7ee72033 5815 make_number (Lisp_String),
efc7e75f 5816 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
018ba359
PJ
5817The truename of a file is calculated by `file-truename'
5818and then abbreviated with `abbreviate-file-name'. */);
f6ed2e84 5819
1ab256cb 5820 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 5821 &current_buffer->auto_save_file_name,
7ee72033 5822 make_number (Lisp_String),
7614d762
RS
5823 doc: /* Name of file for auto-saving current buffer.
5824If it is nil, that means don't auto-save this buffer. */);
1ab256cb 5825
7ee72033 5826 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
efc7e75f 5827 doc: /* Non-nil if this buffer is read-only. */);
1ab256cb 5828
7ee72033 5829 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
efc7e75f 5830 doc: /* Non-nil if this buffer's file has been backed up.
018ba359 5831Backing up is done before the first time the file is saved. */);
1ab256cb
RM
5832
5833 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
7ee72033 5834 make_number (Lisp_Int),
efc7e75f 5835 doc: /* Length of current buffer when last read in, saved or auto-saved.
018ba359 58360 initially. */);
1ab256cb
RM
5837
5838 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
7ee72033 5839 Qnil,
7614d762 5840 doc: /* Non-nil enables selective display.
a66f285a
JB
5841An integer N as value means display only lines
5842that start with less than N columns of space.
7614d762
RS
5843A value of t means that the character ^M makes itself and
5844all the rest of the line invisible; also, when saving the buffer
5845in a file, save the ^M as a newline. */);
1ab256cb
RM
5846
5847#ifndef old
5848 DEFVAR_PER_BUFFER ("selective-display-ellipses",
d765e3a3 5849 &current_buffer->selective_display_ellipses,
7ee72033 5850 Qnil,
3f676284 5851 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
1ab256cb
RM
5852#endif
5853
7ee72033 5854 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
efc7e75f 5855 doc: /* Non-nil if self-insertion should replace existing text.
018ba359
PJ
5856The value should be one of `overwrite-mode-textual',
5857`overwrite-mode-binary', or nil.
5858If it is `overwrite-mode-textual', self-insertion still
5859inserts at the end of a line, and inserts when point is before a tab,
5860until the tab is filled in.
5861If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5862
54939090 5863 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
7ee72033 5864 Qnil,
efc7e75f 5865 doc: /* Display table that controls display of the contents of current buffer.
018ba359
PJ
5866
5867If this variable is nil, the value of `standard-display-table' is used.
5868Each window can have its own, overriding display table, see
5869`set-window-display-table' and `window-display-table'.
5870
5871The display table is a char-table created with `make-display-table'.
5872A char-table is an array indexed by character codes. Normal array
5873primitives `aref' and `aset' can be used to access elements of a char-table.
5874
5875Each of the char-table elements control how to display the corresponding
5876text character: the element at index C in the table says how to display
5877the character whose code is C. Each element should be a vector of
426a9163
JB
5878characters or nil. The value nil means display the character in the
5879default fashion; otherwise, the characters from the vector are delivered
5880to the screen instead of the original character.
018ba359 5881
5fd11dc8 5882For example, (aset buffer-display-table ?X [?Y]) tells Emacs
adbb3b05 5883to display a capital Y instead of each X character.
018ba359
PJ
5884
5885In addition, a char-table has six extra slots to control the display of:
5886
5887 the end of a truncated screen line (extra-slot 0, a single character);
5888 the end of a continued line (extra-slot 1, a single character);
5889 the escape character used to display character codes in octal
5890 (extra-slot 2, a single character);
5891 the character used as an arrow for control characters (extra-slot 3,
5892 a single character);
5893 the decoration indicating the presence of invisible lines (extra-slot 4,
5894 a vector of characters);
5895 the character used to draw the border between side-by-side windows
5896 (extra-slot 5, a single character).
5897
5898See also the functions `display-table-slot' and `set-display-table-slot'. */);
1ab256cb 5899
2ad8731a 5900 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
7ee72033 5901 Qnil,
efc7e75f 5902 doc: /* *Width of left marginal area for display of a buffer.
018ba359 5903A value of nil means no marginal area. */);
177c0ea7 5904
2ad8731a 5905 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
7ee72033 5906 Qnil,
efc7e75f 5907 doc: /* *Width of right marginal area for display of a buffer.
018ba359 5908A value of nil means no marginal area. */);
177c0ea7 5909
2ad8731a
KS
5910 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5911 Qnil,
5912 doc: /* *Width of this buffer's left fringe (in pixels).
5913A value of 0 means no left fringe is shown in this buffer's window.
5914A value of nil means to use the left fringe width from the window's frame. */);
5915
5916 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5917 Qnil,
5918 doc: /* *Width of this buffer's right fringe (in pixels).
5919A value of 0 means no right fringe is shown in this buffer's window.
5920A value of nil means to use the right fringe width from the window's frame. */);
5921
5922 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5923 Qnil,
5924 doc: /* *Non-nil means to display fringes outside display margins.
5925A value of nil means to display fringes between margins and buffer text. */);
5926
5927 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5928 Qnil,
5929 doc: /* *Width of this buffer's scroll bars in pixels.
5930A value of nil means to use the scroll bar width from the window's frame. */);
5931
5932 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5933 Qnil,
5934 doc: /* *Position of this buffer's vertical scroll bar.
7c6b2007 5935The value takes effect whenever you tell a window to display this buffer;
188577ce 5936for instance, with `set-window-buffer' or when `display-buffer' displays it.
7c6b2007 5937
fc2c8887
RS
5938A value of `left' or `right' means put the vertical scroll bar at that side
5939of the window; a value of nil means don't show any vertical scroll bars.
5940A value of t (the default) means do whatever the window's frame specifies. */);
2ad8731a 5941
0552666b 5942 DEFVAR_PER_BUFFER ("indicate-empty-lines",
7ee72033 5943 &current_buffer->indicate_empty_lines, Qnil,
efc7e75f 5944 doc: /* *Visually indicate empty lines after the buffer end.
018ba359
PJ
5945If non-nil, a bitmap is displayed in the left fringe of a window on
5946window-systems. */);
177c0ea7 5947
6b61353c
KH
5948 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5949 &current_buffer->indicate_buffer_boundaries, Qnil,
5950 doc: /* *Visually indicate buffer boundaries and scrolling.
5951If non-nil, the first and last line of the buffer are marked in the fringe
5952of a window on window-systems with angle bitmaps, or if the window can be
5953scrolled, the top and bottom line of the window are marked with up and down
5954arrow bitmaps.
b2229037
KS
5955
5956If value is a symbol `left' or `right', both angle and arrow bitmaps
79e3497d 5957are displayed in the left or right fringe, resp. Any other value
845a78b4 5958that doesn't look like an alist means display the angle bitmaps in
79e3497d 5959the left fringe but no arrows.
b2229037 5960
79e3497d
RS
5961You can exercise more precise control by using an alist as the
5962value. Each alist element (INDICATOR . POSITION) specifies
5963where to show one of the indicators. INDICATOR is one of `top',
b2229037
KS
5964`bottom', `up', `down', or t, which specifies the default position,
5965and POSITION is one of `left', `right', or nil, meaning do not show
5966this indicator.
5967
5968For example, ((top . left) (t . right)) places the top angle bitmap in
5969left fringe, the bottom angle bitmap in right fringe, and both arrow
6b61353c 5970bitmaps in right fringe. To show just the angle bitmaps in the left
b2229037 5971fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6b61353c 5972
c6a46372
KS
5973 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5974 &current_buffer->fringe_indicator_alist, Qnil,
5975 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5976The value is an alist where each element (INDICATOR . BITMAPS)
5977specifies the fringe bitmaps used to display a specific logical
5978fringe indicator.
5979
5980INDICATOR specifies the logical indicator type which is one of the
5981following symbols: `truncation' , `continuation', `overlay-arrow',
5982`top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5983
5984BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5985the actual bitmap shown in the left or right fringe for the logical
5986indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5987right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5988are used only for the `bottom' and `one-line' indicators when the last
63af6055 5989\(only) line in has no final newline. BITMAPS may also be a single
c6a46372
KS
5990symbol which is used in both left and right fringes. */);
5991
5992 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5993 &current_buffer->fringe_cursor_alist, Qnil,
5994 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5995The value is an alist where each element (CURSOR . BITMAP)
5996specifies the fringe bitmaps used to display a specific logical
5997cursor type in the fringe.
5998
5999CURSOR specifies the logical cursor type which is one of the following
6000symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
6001one is used to show a hollow cursor on narrow lines display lines
6002where the normal hollow cursor will not fit.
6003
6004BITMAP is the corresponding fringe bitmap shown for the logical
6005cursor type. */);
6006
0552666b 6007 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
7ee72033 6008 &current_buffer->scroll_up_aggressively, Qnil,
4e0692c1
RS
6009 doc: /* How far to scroll windows upward.
6010If you move point off the bottom, the window scrolls automatically.
426a9163 6011This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
6012means scroll to center point. A fraction means scroll to put point
6013that fraction of the window's height from the bottom of the window.
d765e3a3
JB
6014When the value is 0.0, point goes at the bottom line, which in the
6015simple case that you moved off with C-f means scrolling just one line.
60161.0 means point goes at the top, so that in that simple case, the
6017window scrolls by a full window height. Meaningful values are
175e9712 6018between 0.0 and 1.0, inclusive. */);
177c0ea7 6019
0552666b 6020 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
7ee72033 6021 &current_buffer->scroll_down_aggressively, Qnil,
4e0692c1
RS
6022 doc: /* How far to scroll windows downward.
6023If you move point off the top, the window scrolls automatically.
426a9163 6024This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
6025means scroll to center point. A fraction means scroll to put point
6026that fraction of the window's height from the top of the window.
d765e3a3
JB
6027When the value is 0.0, point goes at the top line, which in the
6028simple case that you moved off with C-b means scrolling just one line.
60291.0 means point goes at the bottom, so that in that simple case, the
6030window scrolls by a full window height. Meaningful values are
175e9712 6031between 0.0 and 1.0, inclusive. */);
177c0ea7 6032
1ab256cb
RM
6033/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6034 "Don't ask.");
6035*/
1ab256cb 6036
7ee72033
MB
6037 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6038 doc: /* List of functions to call before each text change.
018ba359
PJ
6039Two arguments are passed to each function: the positions of
6040the beginning and end of the range of old text to be changed.
6041\(For an insertion, the beginning and end are at the same place.)
6042No information is given about the length of the text after the change.
6043
6044Buffer changes made while executing the `before-change-functions'
6045don't call any before-change or after-change functions.
6046That's because these variables are temporarily set to nil.
d765e3a3
JB
6047As a result, a hook function cannot straightforwardly alter the
6048value of these variables. See the Emacs Lisp manual for a way of
018ba359
PJ
6049accomplishing an equivalent result by using other variables.
6050
6051If an unhandled error happens in running these functions,
6052the variable's value remains nil. That prevents the error
6053from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
6054 Vbefore_change_functions = Qnil;
6055
7ee72033 6056 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
eacdfade 6057 doc: /* List of functions to call after each text change.
018ba359
PJ
6058Three arguments are passed to each function: the positions of
6059the beginning and end of the range of changed text,
6060and the length in bytes of the pre-change text replaced by that range.
6061\(For an insertion, the pre-change length is zero;
6062for a deletion, that length is the number of bytes deleted,
6063and the post-change beginning and end are at the same place.)
6064
6065Buffer changes made while executing the `after-change-functions'
6066don't call any before-change or after-change functions.
6067That's because these variables are temporarily set to nil.
d765e3a3
JB
6068As a result, a hook function cannot straightforwardly alter the
6069value of these variables. See the Emacs Lisp manual for a way of
018ba359
PJ
6070accomplishing an equivalent result by using other variables.
6071
6072If an unhandled error happens in running these functions,
6073the variable's value remains nil. That prevents the error
6074from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
6075 Vafter_change_functions = Qnil;
6076
7ee72033 6077 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
efc7e75f 6078 doc: /* A list of functions to call before changing a buffer which is unmodified.
018ba359 6079The functions are run using the `run-hooks' function. */);
dbc4e1c1 6080 Vfirst_change_hook = Qnil;
1ab256cb 6081
7ee72033
MB
6082 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6083 doc: /* List of undo entries in current buffer.
018ba359
PJ
6084Recent changes come first; older changes follow newer.
6085
6086An entry (BEG . END) represents an insertion which begins at
6087position BEG and ends at position END.
6088
6089An entry (TEXT . POSITION) represents the deletion of the string TEXT
6090from (abs POSITION). If POSITION is positive, point was at the front
6091of the text being deleted; if negative, point was at the end.
6092
6093An entry (t HIGH . LOW) indicates that the buffer previously had
6094\"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6095of the visited file's modification time, as of that time. If the
6096modification time of the most recent save is different, this entry is
6097obsolete.
6098
6099An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6100was modified between BEG and END. PROPERTY is the property name,
6101and VALUE is the old value.
6102
7405f386
KS
6103An entry (apply FUN-NAME . ARGS) means undo the change with
6104\(apply FUN-NAME ARGS).
6105
6106An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6107in the active region. BEG and END is the range affected by this entry
6108and DELTA is the number of bytes added or deleted in that range by
6109this change.
c6c7dc03 6110
018ba359
PJ
6111An entry (MARKER . DISTANCE) indicates that the marker MARKER
6112was adjusted in position by the offset DISTANCE (an integer).
6113
6114An entry of the form POSITION indicates that point was at the buffer
6115location given by the integer. Undoing an entry of this form places
6116point at POSITION.
6117
b4c4f2f4
JB
6118Entries with value `nil' mark undo boundaries. The undo command treats
6119the changes between two undo boundaries as a single step to be undone.
018ba359
PJ
6120
6121If the value of the variable is t, undo information is not recorded. */);
6122
7ee72033
MB
6123 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6124 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
018ba359 6125
7ee72033
MB
6126 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6127 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
018ba359
PJ
6128
6129Normally, the line-motion functions work by scanning the buffer for
5629f29b
DK
6130newlines. Columnar operations (like `move-to-column' and
6131`compute-motion') also work by scanning the buffer, summing character
018ba359
PJ
6132widths as they go. This works well for ordinary text, but if the
6133buffer's lines are very long (say, more than 500 characters), these
6134motion functions will take longer to execute. Emacs may also take
6135longer to update the display.
6136
5629f29b 6137If `cache-long-line-scans' is non-nil, these motion functions cache the
018ba359
PJ
6138results of their scans, and consult the cache to avoid rescanning
6139regions of the buffer until the text is modified. The caches are most
6140beneficial when they prevent the most searching---that is, when the
6141buffer contains long lines and large regions of characters with the
6142same, fixed screen width.
6143
5629f29b 6144When `cache-long-line-scans' is non-nil, processing short lines will
018ba359
PJ
6145become slightly slower (because of the overhead of consulting the
6146cache), and the caches will use memory roughly proportional to the
6147number of newlines and characters whose screen width varies.
6148
6149The caches require no explicit maintenance; their accuracy is
6150maintained internally by the Emacs primitives. Enabling or disabling
6151the cache should not affect the behavior of any of the motion
6152functions; it should only affect their performance. */);
6153
7ee72033
MB
6154 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6155 doc: /* Value of point before the last series of scroll operations, or nil. */);
018ba359 6156
7ee72033
MB
6157 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6158 doc: /* List of formats to use when saving this buffer.
018ba359 6159Formats are defined by `format-alist'. This variable is
a9b9a780 6160set when a file is visited. */);
be9aafdd 6161
71ed49fa
LT
6162 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6163 &current_buffer->auto_save_file_format, Qnil,
6164 doc: /* *Format in which to write auto-save files.
6165Should be a list of symbols naming formats that are defined in `format-alist'.
6166If it is t, which is the default, auto-save files are written in the
6167same format as a regular save would use. */);
6168
3cb719bd 6169 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
7ee72033
MB
6170 &current_buffer->invisibility_spec, Qnil,
6171 doc: /* Invisibility spec of this buffer.
018ba359
PJ
6172The default is t, which means that text is invisible
6173if it has a non-nil `invisible' property.
6174If the value is a list, a text character is invisible if its `invisible'
b49dd850 6175property is an element in that list (or is a list with members in common).
018ba359
PJ
6176If an element is a cons cell of the form (PROP . ELLIPSIS),
6177then characters with property value PROP are invisible,
6178and they have an ellipsis as well if ELLIPSIS is non-nil. */);
3cb719bd 6179
7962a441 6180 DEFVAR_PER_BUFFER ("buffer-display-count",
7ee72033
MB
6181 &current_buffer->display_count, Qnil,
6182 doc: /* A number incremented each time this buffer is displayed in a window.
018ba359 6183The function `set-window-buffer' increments it. */);
3fd364db
RS
6184
6185 DEFVAR_PER_BUFFER ("buffer-display-time",
7ee72033
MB
6186 &current_buffer->display_time, Qnil,
6187 doc: /* Time stamp updated each time this buffer is displayed in a window.
018ba359
PJ
6188The function `set-window-buffer' updates this variable
6189to the value obtained by calling `current-time'.
6190If the buffer has never been shown in a window, the value is nil. */);
6191
7ee72033 6192 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
526c9df9 6193 doc: /* */);
c48f61ef 6194 Vtransient_mark_mode = Qnil;
526c9df9
CY
6195 /* The docstring is in simple.el. If we put it here, it would be
6196 overwritten when transient-mark-mode is defined using
6197 define-minor-mode. */
c48f61ef 6198
7ee72033
MB
6199 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6200 doc: /* *Non-nil means disregard read-only status of buffers or characters.
018ba359
PJ
6201If the value is t, disregard `buffer-read-only' and all `read-only'
6202text properties. If the value is a list, disregard `buffer-read-only'
6203and disregard a `read-only' text property if the property value
6204is a member of the list. */);
a96b68f1
RS
6205 Vinhibit_read_only = Qnil;
6206
7ee72033 6207 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
f6e22881 6208 doc: /* Cursor to use when this buffer is in the selected window.
018ba359
PJ
6209Values are interpreted as follows:
6210
b8dc613f
JB
6211 t use the cursor specified for the frame
6212 nil don't display a cursor
6213 box display a filled box cursor
6214 hollow display a hollow box cursor
6215 bar display a vertical bar cursor with default width
6216 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6217 hbar display a horizontal bar cursor with default height
b4234f4c 6218 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
b8dc613f 6219 ANYTHING ELSE display a hollow box cursor
cd8d5236
RS
6220
6221When the buffer is displayed in a nonselected window,
6222this variable has no effect; the cursor appears as a hollow box. */);
bb2ec976 6223
a3bbced0 6224 DEFVAR_PER_BUFFER ("line-spacing",
7ee72033
MB
6225 &current_buffer->extra_line_spacing, Qnil,
6226 doc: /* Additional space to put between lines when displaying a buffer.
60ebfdf3
KS
6227The space is measured in pixels, and put below lines on window systems.
6228If value is a floating point number, it specifies the spacing relative
fc961256 6229to the default frame line height. A value of nil means add no extra space. */);
a3bbced0 6230
0124c5bd 6231 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
187ccf49 6232 &current_buffer->cursor_in_non_selected_windows, Qnil,
f6e22881 6233 doc: /* *Cursor type to display in non-selected windows.
fc961256 6234The value t means to use hollow box cursor. See `cursor-type' for other values. */);
0124c5bd 6235
7ee72033 6236 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
f6e22881
JB
6237 doc: /* List of functions called with no args to query before killing a buffer.
6238The buffer being killed will be current while the functions are running.
6239If any of them returns nil, the buffer is not killed. */);
dcdffbf6
RS
6240 Vkill_buffer_query_functions = Qnil;
6241
43ed3b8d
CY
6242 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6243 doc: /* Normal hook run before changing the major mode of a buffer.
6244The function `kill-all-local-variables' runs this before doing anything else. */);
6245 Vchange_major_mode_hook = Qnil;
6246 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6247 staticpro (&Qchange_major_mode_hook);
6248
0dc88e60 6249 defsubr (&Sbuffer_live_p);
1ab256cb
RM
6250 defsubr (&Sbuffer_list);
6251 defsubr (&Sget_buffer);
6252 defsubr (&Sget_file_buffer);
6253 defsubr (&Sget_buffer_create);
336cd056 6254 defsubr (&Smake_indirect_buffer);
01050cb5 6255 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
6256 defsubr (&Sbuffer_name);
6257/*defsubr (&Sbuffer_number);*/
6258 defsubr (&Sbuffer_file_name);
336cd056 6259 defsubr (&Sbuffer_base_buffer);
79aa712d 6260 defsubr (&Sbuffer_local_value);
1ab256cb
RM
6261 defsubr (&Sbuffer_local_variables);
6262 defsubr (&Sbuffer_modified_p);
6263 defsubr (&Sset_buffer_modified_p);
6264 defsubr (&Sbuffer_modified_tick);
3e145152 6265 defsubr (&Sbuffer_chars_modified_tick);
1ab256cb
RM
6266 defsubr (&Srename_buffer);
6267 defsubr (&Sother_buffer);
1ab256cb
RM
6268 defsubr (&Sbuffer_enable_undo);
6269 defsubr (&Skill_buffer);
a9ee7a59 6270 defsubr (&Sset_buffer_major_mode);
1ab256cb 6271 defsubr (&Sswitch_to_buffer);
1ab256cb
RM
6272 defsubr (&Scurrent_buffer);
6273 defsubr (&Sset_buffer);
6274 defsubr (&Sbarf_if_buffer_read_only);
6275 defsubr (&Sbury_buffer);
3ac81adb 6276 defsubr (&Serase_buffer);
13cda5f9 6277 defsubr (&Sbuffer_swap_text);
3ac81adb 6278 defsubr (&Sset_buffer_multibyte);
1ab256cb 6279 defsubr (&Skill_all_local_variables);
2eec3b4e 6280
52f8ec73 6281 defsubr (&Soverlayp);
2eec3b4e
RS
6282 defsubr (&Smake_overlay);
6283 defsubr (&Sdelete_overlay);
6284 defsubr (&Smove_overlay);
8ebafa8d
JB
6285 defsubr (&Soverlay_start);
6286 defsubr (&Soverlay_end);
6287 defsubr (&Soverlay_buffer);
6288 defsubr (&Soverlay_properties);
2eec3b4e 6289 defsubr (&Soverlays_at);
74514898 6290 defsubr (&Soverlays_in);
2eec3b4e 6291 defsubr (&Snext_overlay_change);
239c932b 6292 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
6293 defsubr (&Soverlay_recenter);
6294 defsubr (&Soverlay_lists);
6295 defsubr (&Soverlay_get);
6296 defsubr (&Soverlay_put);
a8c21b48 6297 defsubr (&Srestore_buffer_modified_p);
1ab256cb
RM
6298}
6299
dfcf069d 6300void
1ab256cb
RM
6301keys_of_buffer ()
6302{
6303 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6304 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
6305
6306 /* This must not be in syms_of_buffer, because Qdisabled is not
6307 initialized when that function gets called. */
6308 Fput (intern ("erase-buffer"), Qdisabled, Qt);
1ab256cb 6309}
6b61353c
KH
6310
6311/* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6312 (do not change this comment) */