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