Fix bugs with inappropriate mixing of Lisp_Object with int.
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
99a8b2d2 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997
c922bc55 3 Free Software Foundation, Inc.
1ab256cb
RM
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
c922bc55 9the Free Software Foundation; either version 2, or (at your option)
1ab256cb
RM
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
1ab256cb
RM
21
22
2381d133
JB
23#include <sys/types.h>
24#include <sys/stat.h>
1ab256cb
RM
25#include <sys/param.h>
26
27#ifndef MAXPATHLEN
28/* in 4.1, param.h fails to define this. */
29#define MAXPATHLEN 1024
30#endif /* not MAXPATHLEN */
31
18160b98 32#include <config.h>
1ab256cb 33#include "lisp.h"
21cf4cf8 34#include "intervals.h"
1ab256cb
RM
35#include "window.h"
36#include "commands.h"
37#include "buffer.h"
3b06f880 38#include "charset.h"
28e969dd 39#include "region-cache.h"
1ab256cb 40#include "indent.h"
d014bf88 41#include "blockinput.h"
08460cd4 42#include "frame.h"
1ab256cb
RM
43
44struct buffer *current_buffer; /* the current buffer */
45
46/* First buffer in chain of all buffers (in reverse order of creation).
47 Threaded through ->next. */
48
49struct buffer *all_buffers;
50
51/* This structure holds the default values of the buffer-local variables
52 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
53 The default value occupies the same slot in this structure
54 as an individual buffer's value occupies in that buffer.
55 Setting the default value also goes through the alist of buffers
56 and stores into each buffer that does not say it has a local value. */
57
58struct buffer buffer_defaults;
59
60/* A Lisp_Object pointer to the above, used for staticpro */
61
62static Lisp_Object Vbuffer_defaults;
63
64/* This structure marks which slots in a buffer have corresponding
65 default values in buffer_defaults.
66 Each such slot has a nonzero value in this structure.
67 The value has only one nonzero bit.
68
69 When a buffer has its own local value for a slot,
70 the bit for that slot (found in the same slot in this structure)
71 is turned on in the buffer's local_var_flags slot.
72
73 If a slot in this structure is -1, then even though there may
74 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
75 and the corresponding slot in buffer_defaults is not used.
76
77 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
78 but there is a default value which is copied into each buffer.
79
80 If a slot in this structure is negative, then even though there may
81 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
82 and the corresponding slot in buffer_defaults is not used.
83
84 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
85 zero, that is a bug */
86
87struct buffer buffer_local_flags;
88
89/* This structure holds the names of symbols whose values may be
90 buffer-local. It is indexed and accessed in the same way as the above. */
91
92struct buffer buffer_local_symbols;
93/* A Lisp_Object pointer to the above, used for staticpro */
94static Lisp_Object Vbuffer_local_symbols;
95
0fa3ba92
JB
96/* This structure holds the required types for the values in the
97 buffer-local slots. If a slot contains Qnil, then the
98 corresponding buffer slot may contain a value of any type. If a
99 slot contains an integer, then prospective values' tags must be
100 equal to that integer. When a tag does not match, the function
101 buffer_slot_type_mismatch will signal an error. */
102struct buffer buffer_local_types;
103
13de9290
RS
104/* Flags indicating which built-in buffer-local variables
105 are permanent locals. */
106static int buffer_permanent_local_flags;
107
1ab256cb 108Lisp_Object Fset_buffer ();
01050cb5 109void set_buffer_internal ();
c7aa5005 110void set_buffer_internal_1 ();
173f2a64 111static void call_overlay_mod_hooks ();
2f3f993b 112static void swap_out_buffer_local_variables ();
13de9290 113static void reset_buffer_local_variables ();
1ab256cb
RM
114
115/* Alist of all buffer names vs the buffers. */
116/* This used to be a variable, but is no longer,
117 to prevent lossage due to user rplac'ing this alist or its elements. */
118Lisp_Object Vbuffer_alist;
119
120/* Functions to call before and after each text change. */
121Lisp_Object Vbefore_change_function;
122Lisp_Object Vafter_change_function;
5f079267
RS
123Lisp_Object Vbefore_change_functions;
124Lisp_Object Vafter_change_functions;
1ab256cb 125
c48f61ef
RS
126Lisp_Object Vtransient_mark_mode;
127
a96b68f1
RS
128/* t means ignore all read-only text properties.
129 A list means ignore such a property if its value is a member of the list.
130 Any non-nil value means ignore buffer-read-only. */
131Lisp_Object Vinhibit_read_only;
132
dcdffbf6
RS
133/* List of functions to call that can query about killing a buffer.
134 If any of these functions returns nil, we don't kill it. */
135Lisp_Object Vkill_buffer_query_functions;
136
dbc4e1c1
JB
137/* List of functions to call before changing an unmodified buffer. */
138Lisp_Object Vfirst_change_hook;
22378665 139
dbc4e1c1 140Lisp_Object Qfirst_change_hook;
22378665
RS
141Lisp_Object Qbefore_change_functions;
142Lisp_Object Qafter_change_functions;
1ab256cb
RM
143
144Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
145
146Lisp_Object Qprotected_field;
147
148Lisp_Object QSFundamental; /* A string "Fundamental" */
149
150Lisp_Object Qkill_buffer_hook;
151
5fe0b67e
RS
152Lisp_Object Qget_file_buffer;
153
52f8ec73
JB
154Lisp_Object Qoverlayp;
155
bbbe9545 156Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
5985d248 157
294d215f
RS
158Lisp_Object Qmodification_hooks;
159Lisp_Object Qinsert_in_front_hooks;
160Lisp_Object Qinsert_behind_hooks;
161
1ab256cb
RM
162/* For debugging; temporary. See set_buffer_internal. */
163/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
164
165nsberror (spec)
166 Lisp_Object spec;
167{
a7a60ce9 168 if (STRINGP (spec))
1ab256cb
RM
169 error ("No buffer named %s", XSTRING (spec)->data);
170 error ("Invalid buffer argument");
171}
172\f
0dc88e60
RS
173DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
174 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
175Value is nil if OBJECT is not a buffer or if it has been killed.")
176 (object)
177 Lisp_Object object;
178{
179 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
180 ? Qt : Qnil);
181}
182
08460cd4
RS
183DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
184 "Return a list of all existing live buffers.\n\
185If the optional arg FRAME is a frame, we return that frame's buffer list.")
186 (frame)
187 Lisp_Object frame;
1ab256cb 188{
08460cd4
RS
189 Lisp_Object framelist, general;
190 general = Fmapcar (Qcdr, Vbuffer_alist);
191
192 if (FRAMEP (frame))
193 {
194 Lisp_Object tail;
195
196 CHECK_FRAME (frame, 1);
197
198 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
199
200 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
201 tail = framelist;
202 while (! NILP (tail))
203 {
204 general = Fdelq (XCONS (tail)->car, general);
205 tail = XCONS (tail)->cdr;
206 }
207 return nconc2 (framelist, general);
208 }
209
210 return general;
1ab256cb
RM
211}
212
04ae1b48
RS
213/* Like Fassoc, but use Fstring_equal to compare
214 (which ignores text properties),
215 and don't ever QUIT. */
216
217static Lisp_Object
218assoc_ignore_text_properties (key, list)
219 register Lisp_Object key;
220 Lisp_Object list;
221{
222 register Lisp_Object tail;
223 for (tail = list; !NILP (tail); tail = Fcdr (tail))
224 {
225 register Lisp_Object elt, tem;
226 elt = Fcar (tail);
227 tem = Fstring_equal (Fcar (elt), key);
228 if (!NILP (tem))
229 return elt;
230 }
231 return Qnil;
232}
233
1ab256cb
RM
234DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
235 "Return the buffer named NAME (a string).\n\
236If there is no live buffer named NAME, return nil.\n\
237NAME may also be a buffer; if so, the value is that buffer.")
238 (name)
239 register Lisp_Object name;
240{
a7a60ce9 241 if (BUFFERP (name))
1ab256cb
RM
242 return name;
243 CHECK_STRING (name, 0);
244
04ae1b48 245 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
1ab256cb
RM
246}
247
248DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
249 "Return the buffer visiting file FILENAME (a string).\n\
92194d02 250The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
11da5363
RS
251If there is no such live buffer, return nil.\n\
252See also `find-buffer-visiting'.")
1ab256cb
RM
253 (filename)
254 register Lisp_Object filename;
255{
256 register Lisp_Object tail, buf, tem;
5fe0b67e
RS
257 Lisp_Object handler;
258
1ab256cb
RM
259 CHECK_STRING (filename, 0);
260 filename = Fexpand_file_name (filename, Qnil);
261
5fe0b67e
RS
262 /* If the file name has special constructs in it,
263 call the corresponding file handler. */
a617e913 264 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
5fe0b67e
RS
265 if (!NILP (handler))
266 return call2 (handler, Qget_file_buffer, filename);
267
1ab256cb
RM
268 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
269 {
270 buf = Fcdr (XCONS (tail)->car);
a7a60ce9
KH
271 if (!BUFFERP (buf)) continue;
272 if (!STRINGP (XBUFFER (buf)->filename)) continue;
1ab256cb 273 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 274 if (!NILP (tem))
1ab256cb
RM
275 return buf;
276 }
277 return Qnil;
278}
279
52e01189
RS
280Lisp_Object
281get_truename_buffer (filename)
282 register Lisp_Object filename;
283{
284 register Lisp_Object tail, buf, tem;
285
286 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
287 {
288 buf = Fcdr (XCONS (tail)->car);
289 if (!BUFFERP (buf)) continue;
290 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
291 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
292 if (!NILP (tem))
293 return buf;
294 }
295 return Qnil;
296}
297
1ab256cb
RM
298/* Incremented for each buffer created, to assign the buffer number. */
299int buffer_count;
300
301DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
302 "Return the buffer named NAME, or create such a buffer and return it.\n\
303A new buffer is created if there is no live buffer named NAME.\n\
b44895bc 304If NAME starts with a space, the new buffer does not keep undo information.\n\
1ab256cb
RM
305If NAME is a buffer instead of a string, then it is the value returned.\n\
306The value is never nil.")
307 (name)
308 register Lisp_Object name;
309{
a9ee7a59 310 register Lisp_Object buf;
1ab256cb
RM
311 register struct buffer *b;
312
313 buf = Fget_buffer (name);
265a9e55 314 if (!NILP (buf))
1ab256cb
RM
315 return buf;
316
31cd83e9
KH
317 if (XSTRING (name)->size == 0)
318 error ("Empty string for buffer name is not allowed");
319
9ac0d9e0 320 b = (struct buffer *) xmalloc (sizeof (struct buffer));
1ab256cb 321
336cd056
RS
322 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
323
324 /* An ordinary buffer uses its own struct buffer_text. */
325 b->text = &b->own_text;
326 b->base_buffer = 0;
327
1ab256cb 328 BUF_GAP_SIZE (b) = 20;
9ac0d9e0 329 BLOCK_INPUT;
3b06f880
KH
330 /* We allocate extra 1-byte at the tail and keep it always '\0' for
331 anchoring a search. */
332 BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
9ac0d9e0 333 UNBLOCK_INPUT;
1ab256cb 334 if (! BUF_BEG_ADDR (b))
81841847 335 buffer_memory_full ();
1ab256cb
RM
336
337 BUF_PT (b) = 1;
338 BUF_GPT (b) = 1;
339 BUF_BEGV (b) = 1;
340 BUF_ZV (b) = 1;
341 BUF_Z (b) = 1;
342 BUF_MODIFF (b) = 1;
2509d356 343 BUF_OVERLAY_MODIFF (b) = 1;
336cd056
RS
344 BUF_SAVE_MODIFF (b) = 1;
345 BUF_INTERVALS (b) = 0;
3b06f880 346 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
1ab256cb 347
28e969dd
JB
348 b->newline_cache = 0;
349 b->width_run_cache = 0;
350 b->width_table = Qnil;
351
1ab256cb
RM
352 /* Put this on the chain of all buffers including killed ones. */
353 b->next = all_buffers;
354 all_buffers = b;
355
336cd056
RS
356 /* An ordinary buffer normally doesn't need markers
357 to handle BEGV and ZV. */
358 b->pt_marker = Qnil;
359 b->begv_marker = Qnil;
360 b->zv_marker = Qnil;
04ae1b48
RS
361
362 name = Fcopy_sequence (name);
363 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
1ab256cb 364 b->name = name;
04ae1b48 365
1ab256cb
RM
366 if (XSTRING (name)->data[0] != ' ')
367 b->undo_list = Qnil;
368 else
369 b->undo_list = Qt;
370
371 reset_buffer (b);
13de9290 372 reset_buffer_local_variables (b, 1);
1ab256cb
RM
373
374 /* Put this in the alist of all live buffers. */
67180c6a 375 XSETBUFFER (buf, b);
1ab256cb
RM
376 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
377
378 b->mark = Fmake_marker ();
336cd056
RS
379 BUF_MARKERS (b) = Qnil;
380 b->name = name;
381 return buf;
382}
383
a2428fa2 384DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
193c3837 385 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
a2428fa2
EN
386 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
387BASE-BUFFER should be an existing buffer (or buffer name).\n\
9e552710
RS
388NAME should be a string which is not the name of an existing buffer.")
389 (base_buffer, name)
390 register Lisp_Object base_buffer, name;
336cd056
RS
391{
392 register Lisp_Object buf;
393 register struct buffer *b;
394
395 buf = Fget_buffer (name);
396 if (!NILP (buf))
397 error ("Buffer name `%s' is in use", XSTRING (name)->data);
398
399 base_buffer = Fget_buffer (base_buffer);
400 if (NILP (base_buffer))
401 error ("No such buffer: `%s'",
402 XSTRING (XBUFFER (base_buffer)->name)->data);
403
404 if (XSTRING (name)->size == 0)
405 error ("Empty string for buffer name is not allowed");
406
407 b = (struct buffer *) xmalloc (sizeof (struct buffer));
408
409 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
410
411 if (XBUFFER (base_buffer)->base_buffer)
412 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
413 else
414 b->base_buffer = XBUFFER (base_buffer);
415
416 /* Use the base buffer's text object. */
417 b->text = b->base_buffer->text;
418
419 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
420 BUF_ZV (b) = BUF_ZV (b->base_buffer);
421 BUF_PT (b) = BUF_PT (b->base_buffer);
422
423 b->newline_cache = 0;
424 b->width_run_cache = 0;
425 b->width_table = Qnil;
426
427 /* Put this on the chain of all buffers including killed ones. */
428 b->next = all_buffers;
429 all_buffers = b;
430
431 name = Fcopy_sequence (name);
432 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
433 b->name = name;
434
435 reset_buffer (b);
13de9290 436 reset_buffer_local_variables (b, 1);
336cd056
RS
437
438 /* Put this in the alist of all live buffers. */
439 XSETBUFFER (buf, b);
440 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
441
442 b->mark = Fmake_marker ();
1ab256cb 443 b->name = name;
336cd056
RS
444
445 /* Make sure the base buffer has markers for its narrowing. */
446 if (NILP (b->base_buffer->pt_marker))
447 {
448 b->base_buffer->pt_marker = Fmake_marker ();
449 Fset_marker (b->base_buffer->pt_marker,
450 make_number (BUF_PT (b->base_buffer)), base_buffer);
451 }
452 if (NILP (b->base_buffer->begv_marker))
453 {
454 b->base_buffer->begv_marker = Fmake_marker ();
455 Fset_marker (b->base_buffer->begv_marker,
456 make_number (BUF_BEGV (b->base_buffer)), base_buffer);
457 }
458 if (NILP (b->base_buffer->zv_marker))
459 {
460 b->base_buffer->zv_marker = Fmake_marker ();
461 Fset_marker (b->base_buffer->zv_marker,
462 make_number (BUF_ZV (b->base_buffer)), base_buffer);
26d84681 463 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
336cd056
RS
464 }
465
466 /* Give the indirect buffer markers for its narrowing. */
ea064aa0
RS
467 b->pt_marker = Fmake_marker ();
468 Fset_marker (b->pt_marker, make_number (BUF_PT (b)), buf);
469 b->begv_marker = Fmake_marker ();
470 Fset_marker (b->begv_marker, make_number (BUF_BEGV (b)), buf);
471 b->zv_marker = Fmake_marker ();
472 Fset_marker (b->zv_marker, make_number (BUF_ZV (b)), buf);
473
26d84681 474 XMARKER (b->zv_marker)->insertion_type = 1;
336cd056 475
a9ee7a59 476 return buf;
1ab256cb
RM
477}
478
bcd40520
RS
479/* Reinitialize everything about a buffer except its name and contents
480 and local variables. */
1ab256cb
RM
481
482void
483reset_buffer (b)
484 register struct buffer *b;
485{
486 b->filename = Qnil;
f6ed2e84 487 b->file_truename = Qnil;
1ab256cb
RM
488 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
489 b->modtime = 0;
8d7a4592 490 XSETFASTINT (b->save_length, 0);
1ab256cb 491 b->last_window_start = 1;
8b264726
RS
492 /* It is more conservative to start out "changed" than "unchanged". */
493 b->clip_changed = 1;
1ab256cb
RM
494 b->backed_up = Qnil;
495 b->auto_save_modified = 0;
84f6bcba 496 b->auto_save_failure_time = -1;
1ab256cb
RM
497 b->auto_save_file_name = Qnil;
498 b->read_only = Qnil;
2eec3b4e
RS
499 b->overlays_before = Qnil;
500 b->overlays_after = Qnil;
8d7a4592 501 XSETFASTINT (b->overlay_center, 1);
dfda7a7f 502 b->mark_active = Qnil;
943e065b 503 b->point_before_scroll = Qnil;
be9aafdd 504 b->file_format = Qnil;
0dc6f165 505 b->last_selected_window = Qnil;
7962a441 506 XSETINT (b->display_count, 0);
fb2030e3
RS
507 b->extra2 = Qnil;
508 b->extra3 = Qnil;
1ab256cb
RM
509}
510
bcd40520
RS
511/* Reset buffer B's local variables info.
512 Don't use this on a buffer that has already been in use;
513 it does not treat permanent locals consistently.
13de9290
RS
514 Instead, use Fkill_all_local_variables.
515
516 If PERMANENT_TOO is 1, then we reset permanent built-in
517 buffer-local variables. If PERMANENT_TOO is 0,
518 we preserve those. */
bcd40520 519
13de9290
RS
520static void
521reset_buffer_local_variables (b, permanent_too)
1ab256cb 522 register struct buffer *b;
13de9290 523 int permanent_too;
1ab256cb
RM
524{
525 register int offset;
13de9290
RS
526 int dont_reset;
527
528 /* Decide which built-in local variables to reset. */
529 if (permanent_too)
530 dont_reset = 0;
531 else
532 dont_reset = buffer_permanent_local_flags;
1ab256cb
RM
533
534 /* Reset the major mode to Fundamental, together with all the
535 things that depend on the major mode.
536 default-major-mode is handled at a higher level.
537 We ignore it here. */
538 b->major_mode = Qfundamental_mode;
539 b->keymap = Qnil;
540 b->abbrev_table = Vfundamental_mode_abbrev_table;
541 b->mode_name = QSFundamental;
542 b->minor_modes = Qnil;
3446af9c
RS
543
544 /* If the standard case table has been altered and invalidated,
545 fix up its insides first. */
546 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
547 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
548 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
549 Fset_standard_case_table (Vascii_downcase_table);
550
1ab256cb 551 b->downcase_table = Vascii_downcase_table;
1e9b6335
RS
552 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
553 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
554 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
3cb719bd 555 b->invisibility_spec = Qt;
2e716096
RS
556#ifndef DOS_NT
557 b->buffer_file_type = Qnil;
558#endif
3cb719bd 559
1ab256cb
RM
560#if 0
561 b->sort_table = XSTRING (Vascii_sort_table);
562 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
563#endif /* 0 */
564
13de9290 565 /* Reset all (or most) per-buffer variables to their defaults. */
1ab256cb 566 b->local_var_alist = Qnil;
13de9290 567 b->local_var_flags &= dont_reset;
1ab256cb
RM
568
569 /* For each slot that has a default value,
570 copy that into the slot. */
571
572 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
573 offset < sizeof (struct buffer);
4d2f1389 574 offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
aab80822
KH
575 {
576 int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
13de9290
RS
577 if ((flag > 0
578 /* Don't reset a permanent local. */
579 && ! (dont_reset & flag))
580 || flag == -2)
581 *(Lisp_Object *)(offset + (char *)b)
582 = *(Lisp_Object *)(offset + (char *)&buffer_defaults);
aab80822 583 }
1ab256cb
RM
584}
585
01050cb5
RM
586/* We split this away from generate-new-buffer, because rename-buffer
587 and set-visited-file-name ought to be able to use this to really
588 rename the buffer properly. */
589
590DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
c273e647 591 1, 2, 0,
01050cb5
RM
592 "Return a string that is the name of no existing buffer based on NAME.\n\
593If there is no live buffer named NAME, then return NAME.\n\
1ab256cb 594Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
c273e647 595until an unused name is found, and then return that name.\n\
03bdd54c 596Optional second argument IGNORE specifies a name that is okay to use\n\
c273e647 597\(if it is in the sequence to be tried)\n\
e8b3a22d 598even if a buffer with that name exists.")
c273e647
RS
599 (name, ignore)
600 register Lisp_Object name, ignore;
1ab256cb
RM
601{
602 register Lisp_Object gentemp, tem;
603 int count;
604 char number[10];
605
606 CHECK_STRING (name, 0);
607
608 tem = Fget_buffer (name);
265a9e55 609 if (NILP (tem))
01050cb5 610 return name;
1ab256cb
RM
611
612 count = 1;
613 while (1)
614 {
615 sprintf (number, "<%d>", ++count);
616 gentemp = concat2 (name, build_string (number));
638e4fc3 617 tem = Fstring_equal (gentemp, ignore);
c273e647
RS
618 if (!NILP (tem))
619 return gentemp;
1ab256cb 620 tem = Fget_buffer (gentemp);
265a9e55 621 if (NILP (tem))
01050cb5 622 return gentemp;
1ab256cb
RM
623 }
624}
625
626\f
627DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
628 "Return the name of BUFFER, as a string.\n\
01050cb5 629With no argument or nil as argument, return the name of the current buffer.")
1ab256cb
RM
630 (buffer)
631 register Lisp_Object buffer;
632{
265a9e55 633 if (NILP (buffer))
1ab256cb
RM
634 return current_buffer->name;
635 CHECK_BUFFER (buffer, 0);
636 return XBUFFER (buffer)->name;
637}
638
639DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
640 "Return name of file BUFFER is visiting, or nil if none.\n\
641No argument or nil as argument means use the current buffer.")
642 (buffer)
643 register Lisp_Object buffer;
644{
265a9e55 645 if (NILP (buffer))
1ab256cb
RM
646 return current_buffer->filename;
647 CHECK_BUFFER (buffer, 0);
648 return XBUFFER (buffer)->filename;
649}
650
336cd056
RS
651DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
652 0, 1, 0,
653 "Return the base buffer of indirect buffer BUFFER.\n\
654If BUFFER is not indirect, return nil.")
655 (buffer)
656 register Lisp_Object buffer;
657{
658 struct buffer *base;
659 Lisp_Object base_buffer;
660
661 if (NILP (buffer))
662 base = current_buffer->base_buffer;
663 else
664 {
665 CHECK_BUFFER (buffer, 0);
666 base = XBUFFER (buffer)->base_buffer;
667 }
668
669 if (! base)
670 return Qnil;
671 XSETBUFFER (base_buffer, base);
672 return base_buffer;
673}
674
1ab256cb
RM
675DEFUN ("buffer-local-variables", Fbuffer_local_variables,
676 Sbuffer_local_variables, 0, 1, 0,
677 "Return an alist of variables that are buffer-local in BUFFER.\n\
553defa4
RS
678Most elements look like (SYMBOL . VALUE), describing one variable.\n\
679For a symbol that is locally unbound, just the symbol appears in the value.\n\
1ab256cb
RM
680Note that storing new VALUEs in these elements doesn't change the variables.\n\
681No argument or nil as argument means use current buffer as BUFFER.")
682 (buffer)
683 register Lisp_Object buffer;
684{
685 register struct buffer *buf;
553defa4 686 register Lisp_Object result;
1ab256cb 687
265a9e55 688 if (NILP (buffer))
1ab256cb
RM
689 buf = current_buffer;
690 else
691 {
692 CHECK_BUFFER (buffer, 0);
693 buf = XBUFFER (buffer);
694 }
695
553defa4
RS
696 result = Qnil;
697
1ab256cb 698 {
553defa4
RS
699 register Lisp_Object tail;
700 for (tail = buf->local_var_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1ab256cb 701 {
553defa4
RS
702 Lisp_Object val, elt;
703
704 elt = XCONS (tail)->car;
705
e0585c64
RS
706 /* Reference each variable in the alist in buf.
707 If inquiring about the current buffer, this gets the current values,
708 so store them into the alist so the alist is up to date.
709 If inquiring about some other buffer, this swaps out any values
710 for that buffer, making the alist up to date automatically. */
711 val = find_symbol_value (XCONS (elt)->car);
712 /* Use the current buffer value only if buf is the current buffer. */
713 if (buf != current_buffer)
553defa4
RS
714 val = XCONS (elt)->cdr;
715
716 /* If symbol is unbound, put just the symbol in the list. */
717 if (EQ (val, Qunbound))
718 result = Fcons (XCONS (elt)->car, result);
719 /* Otherwise, put (symbol . value) in the list. */
720 else
721 result = Fcons (Fcons (XCONS (elt)->car, val), result);
1ab256cb
RM
722 }
723 }
724
1ab256cb
RM
725 /* Add on all the variables stored in special slots. */
726 {
727 register int offset, mask;
728
729 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
730 offset < sizeof (struct buffer);
4d2f1389 731 offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
1ab256cb 732 {
aab80822 733 mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
1ab256cb 734 if (mask == -1 || (buf->local_var_flags & mask))
aab80822
KH
735 if (SYMBOLP (*(Lisp_Object *)(offset
736 + (char *)&buffer_local_symbols)))
737 result = Fcons (Fcons (*((Lisp_Object *)
738 (offset + (char *)&buffer_local_symbols)),
553defa4
RS
739 *(Lisp_Object *)(offset + (char *)buf)),
740 result);
1ab256cb
RM
741 }
742 }
553defa4
RS
743
744 return result;
1ab256cb
RM
745}
746
747\f
748DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
749 0, 1, 0,
750 "Return t if BUFFER was modified since its file was last read or saved.\n\
751No argument or nil as argument means use current buffer as BUFFER.")
752 (buffer)
753 register Lisp_Object buffer;
754{
755 register struct buffer *buf;
265a9e55 756 if (NILP (buffer))
1ab256cb
RM
757 buf = current_buffer;
758 else
759 {
760 CHECK_BUFFER (buffer, 0);
761 buf = XBUFFER (buffer);
762 }
763
336cd056 764 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1ab256cb
RM
765}
766
767DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
768 1, 1, 0,
769 "Mark current buffer as modified or unmodified according to FLAG.\n\
770A non-nil FLAG means mark the buffer modified.")
771 (flag)
772 register Lisp_Object flag;
773{
774 register int already;
775 register Lisp_Object fn;
776
777#ifdef CLASH_DETECTION
778 /* If buffer becoming modified, lock the file.
779 If buffer becoming unmodified, unlock the file. */
780
60f4dd23 781 fn = current_buffer->file_truename;
265a9e55 782 if (!NILP (fn))
1ab256cb 783 {
336cd056 784 already = SAVE_MODIFF < MODIFF;
265a9e55 785 if (!already && !NILP (flag))
1ab256cb 786 lock_file (fn);
265a9e55 787 else if (already && NILP (flag))
1ab256cb
RM
788 unlock_file (fn);
789 }
790#endif /* CLASH_DETECTION */
791
336cd056 792 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1ab256cb
RM
793 update_mode_lines++;
794 return flag;
795}
796
797DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
798 0, 1, 0,
799 "Return BUFFER's tick counter, incremented for each change in text.\n\
800Each buffer has a tick counter which is incremented each time the text in\n\
801that buffer is changed. It wraps around occasionally.\n\
802No argument or nil as argument means use current buffer as BUFFER.")
803 (buffer)
804 register Lisp_Object buffer;
805{
806 register struct buffer *buf;
265a9e55 807 if (NILP (buffer))
1ab256cb
RM
808 buf = current_buffer;
809 else
810 {
811 CHECK_BUFFER (buffer, 0);
812 buf = XBUFFER (buffer);
813 }
814
815 return make_number (BUF_MODIFF (buf));
816}
817\f
01050cb5 818DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
4c7e5f09 819 "sRename buffer (to new name): \nP",
1ab256cb 820 "Change current buffer's name to NEWNAME (a string).\n\
3bd779aa 821If second arg UNIQUE is nil or omitted, it is an error if a\n\
01050cb5 822buffer named NEWNAME already exists.\n\
3bd779aa 823If UNIQUE is non-nil, come up with a new name using\n\
01050cb5 824`generate-new-buffer-name'.\n\
3bd779aa
RS
825Interactively, you can set UNIQUE with a prefix argument.\n\
826We return the name we actually gave the buffer.\n\
1ab256cb 827This does not change the name of the visited file (if any).")
489c043a
RS
828 (newname, unique)
829 register Lisp_Object newname, unique;
1ab256cb
RM
830{
831 register Lisp_Object tem, buf;
832
489c043a 833 CHECK_STRING (newname, 0);
d59698c4 834
489c043a 835 if (XSTRING (newname)->size == 0)
d59698c4
RS
836 error ("Empty string is invalid as a buffer name");
837
489c043a 838 tem = Fget_buffer (newname);
c059b5ea
RM
839 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
840 the buffer automatically so you can create another with the original name.
841 It makes UNIQUE equivalent to
489c043a 842 (rename-buffer (generate-new-buffer-name NEWNAME)). */
c059b5ea 843 if (NILP (unique) && XBUFFER (tem) == current_buffer)
fb5eba9c 844 return current_buffer->name;
265a9e55 845 if (!NILP (tem))
01050cb5 846 {
3bd779aa 847 if (!NILP (unique))
489c043a 848 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
01050cb5 849 else
489c043a 850 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
01050cb5 851 }
1ab256cb 852
489c043a 853 current_buffer->name = newname;
76f590d7
JB
854
855 /* Catch redisplay's attention. Unless we do this, the mode lines for
856 any windows displaying current_buffer will stay unchanged. */
857 update_mode_lines++;
858
67180c6a 859 XSETBUFFER (buf, current_buffer);
489c043a 860 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
cf058e49
KH
861 if (NILP (current_buffer->filename)
862 && !NILP (current_buffer->auto_save_file_name))
1ab256cb 863 call0 (intern ("rename-auto-save-file"));
fb5eba9c
RS
864 /* Refetch since that last call may have done GC. */
865 return current_buffer->name;
1ab256cb
RM
866}
867
a0ebb746 868DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
1ab256cb 869 "Return most recently selected buffer other than BUFFER.\n\
a0ebb746
JB
870Buffers not visible in windows are preferred to visible buffers,\n\
871unless optional second argument VISIBLE-OK is non-nil.\n\
1ab256cb
RM
872If no other buffer exists, the buffer `*scratch*' is returned.\n\
873If BUFFER is omitted or nil, some interesting buffer is returned.")
a0ebb746
JB
874 (buffer, visible_ok)
875 register Lisp_Object buffer, visible_ok;
1ab256cb 876{
89132f25 877 Lisp_Object Fset_buffer_major_mode ();
7962a441 878 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
1ab256cb
RM
879 notsogood = Qnil;
880
7962a441
RS
881 tail = Vbuffer_alist;
882 pred = frame_buffer_predicate ();
883
884 /* Consider buffers that have been seen in the selected frame
885 before other buffers. */
886
887 tem = frame_buffer_list ();
888 add_ons = Qnil;
889 while (CONSP (tem))
890 {
891 if (BUFFERP (XCONS (tem)->car))
892 add_ons = Fcons (Fcons (Qnil, XCONS (tem)->car), add_ons);
893 tem = XCONS (tem)->cdr;
894 }
895 tail = nconc2 (Fnreverse (add_ons), tail);
896
897 for (; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
898 {
899 buf = Fcdr (Fcar (tail));
900 if (EQ (buf, buffer))
901 continue;
902 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
903 continue;
04ae1b48
RS
904 /* If the selected frame has a buffer_predicate,
905 disregard buffers that don't fit the predicate. */
7962a441 906 if (!NILP (pred))
04ae1b48 907 {
7962a441 908 tem = call1 (pred, buf);
04ae1b48
RS
909 if (NILP (tem))
910 continue;
911 }
04ae1b48 912
a0ebb746 913 if (NILP (visible_ok))
db732e5a 914 tem = Fget_buffer_window (buf, Qt);
a0ebb746
JB
915 else
916 tem = Qnil;
265a9e55 917 if (NILP (tem))
1ab256cb 918 return buf;
265a9e55 919 if (NILP (notsogood))
1ab256cb
RM
920 notsogood = buf;
921 }
265a9e55 922 if (!NILP (notsogood))
1ab256cb 923 return notsogood;
89132f25
KH
924 buf = Fget_buffer_create (build_string ("*scratch*"));
925 Fset_buffer_major_mode (buf);
926 return buf;
1ab256cb
RM
927}
928\f
316784fb
KH
929DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
930 0, 1, "",
5b8bcf48
RS
931 "Make BUFFER stop keeping undo information.\n\
932No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
933 (buffer)
934 register Lisp_Object buffer;
1ab256cb 935{
ffd56f97
JB
936 Lisp_Object real_buffer;
937
938 if (NILP (buffer))
67180c6a 939 XSETBUFFER (real_buffer, current_buffer);
ffd56f97
JB
940 else
941 {
942 real_buffer = Fget_buffer (buffer);
943 if (NILP (real_buffer))
944 nsberror (buffer);
945 }
946
947 XBUFFER (real_buffer)->undo_list = Qt;
948
1ab256cb
RM
949 return Qnil;
950}
951
952DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
953 0, 1, "",
954 "Start keeping undo information for buffer BUFFER.\n\
955No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
956 (buffer)
957 register Lisp_Object buffer;
1ab256cb 958{
ffd56f97 959 Lisp_Object real_buffer;
1ab256cb 960
ffd56f97 961 if (NILP (buffer))
67180c6a 962 XSETBUFFER (real_buffer, current_buffer);
1ab256cb
RM
963 else
964 {
ffd56f97
JB
965 real_buffer = Fget_buffer (buffer);
966 if (NILP (real_buffer))
967 nsberror (buffer);
1ab256cb
RM
968 }
969
ffd56f97
JB
970 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
971 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
972
973 return Qnil;
974}
975
976/*
977 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
978Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
979The buffer being killed will be current while the hook is running.\n\
980See `kill-buffer'."
981 */
982DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
983 "Kill the buffer BUFFER.\n\
984The argument may be a buffer or may be the name of a buffer.\n\
985An argument of nil means kill the current buffer.\n\n\
986Value is t if the buffer is actually killed, nil if user says no.\n\n\
987The value of `kill-buffer-hook' (which may be local to that buffer),\n\
988if not void, is a list of functions to be called, with no arguments,\n\
989before the buffer is actually killed. The buffer to be killed is current\n\
990when the hook functions are called.\n\n\
991Any processes that have this buffer as the `process-buffer' are killed\n\
b64d7442 992with SIGHUP.")
a25f13ae
KH
993 (buffer)
994 Lisp_Object buffer;
1ab256cb
RM
995{
996 Lisp_Object buf;
997 register struct buffer *b;
998 register Lisp_Object tem;
999 register struct Lisp_Marker *m;
1000 struct gcpro gcpro1, gcpro2;
1001
a25f13ae 1002 if (NILP (buffer))
1ab256cb
RM
1003 buf = Fcurrent_buffer ();
1004 else
a25f13ae 1005 buf = Fget_buffer (buffer);
265a9e55 1006 if (NILP (buf))
a25f13ae 1007 nsberror (buffer);
1ab256cb
RM
1008
1009 b = XBUFFER (buf);
1010
4a4a9db5
KH
1011 /* Avoid trouble for buffer already dead. */
1012 if (NILP (b->name))
1013 return Qnil;
1014
1ab256cb 1015 /* Query if the buffer is still modified. */
265a9e55 1016 if (INTERACTIVE && !NILP (b->filename)
336cd056 1017 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1ab256cb 1018 {
a25f13ae 1019 GCPRO1 (buf);
1ab256cb
RM
1020 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1021 XSTRING (b->name)->data));
1022 UNGCPRO;
265a9e55 1023 if (NILP (tem))
1ab256cb
RM
1024 return Qnil;
1025 }
1026
dcdffbf6 1027 /* Run hooks with the buffer to be killed the current buffer. */
1ab256cb
RM
1028 {
1029 register Lisp_Object val;
1030 int count = specpdl_ptr - specpdl;
dcdffbf6 1031 Lisp_Object list;
1ab256cb
RM
1032
1033 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1034 set_buffer_internal (b);
dcdffbf6
RS
1035
1036 /* First run the query functions; if any query is answered no,
1037 don't kill the buffer. */
1038 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1039 {
1040 tem = call0 (Fcar (list));
1041 if (NILP (tem))
1042 return unbind_to (count, Qnil);
1043 }
1044
1045 /* Then run the hooks. */
fd186f07
RS
1046 if (!NILP (Vrun_hooks))
1047 call1 (Vrun_hooks, Qkill_buffer_hook);
1ab256cb
RM
1048 unbind_to (count, Qnil);
1049 }
1050
1051 /* We have no more questions to ask. Verify that it is valid
1052 to kill the buffer. This must be done after the questions
1053 since anything can happen within do_yes_or_no_p. */
1054
1055 /* Don't kill the minibuffer now current. */
1056 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1057 return Qnil;
1058
265a9e55 1059 if (NILP (b->name))
1ab256cb
RM
1060 return Qnil;
1061
336cd056
RS
1062 /* When we kill a base buffer, kill all its indirect buffers.
1063 We do it at this stage so nothing terrible happens if they
1064 ask questions or their hooks get errors. */
1065 if (! b->base_buffer)
1066 {
1067 struct buffer *other;
1068
1069 GCPRO1 (buf);
1070
1071 for (other = all_buffers; other; other = other->next)
4a4a9db5
KH
1072 /* all_buffers contains dead buffers too;
1073 don't re-kill them. */
1074 if (other->base_buffer == b && !NILP (other->name))
336cd056
RS
1075 {
1076 Lisp_Object buf;
1077 XSETBUFFER (buf, other);
1078 Fkill_buffer (buf);
1079 }
1080
1081 UNGCPRO;
1082 }
1083
1ab256cb
RM
1084 /* Make this buffer not be current.
1085 In the process, notice if this is the sole visible buffer
1086 and give up if so. */
1087 if (b == current_buffer)
1088 {
172a9c1f 1089 tem = Fother_buffer (buf, Qnil);
1ab256cb
RM
1090 Fset_buffer (tem);
1091 if (b == current_buffer)
1092 return Qnil;
1093 }
1094
1095 /* Now there is no question: we can kill the buffer. */
1096
1097#ifdef CLASH_DETECTION
1098 /* Unlock this buffer's file, if it is locked. */
1099 unlock_buffer (b);
1100#endif /* CLASH_DETECTION */
1101
1ab256cb 1102 kill_buffer_processes (buf);
1ab256cb
RM
1103
1104 tem = Vinhibit_quit;
1105 Vinhibit_quit = Qt;
00550f94 1106 replace_buffer_in_all_windows (buf);
b26dd9cb 1107 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
7962a441 1108 frames_discard_buffer (buf);
1ab256cb
RM
1109 Vinhibit_quit = tem;
1110
9b59d6d0 1111 /* Delete any auto-save file, if we saved it in this session. */
a7a60ce9 1112 if (STRINGP (b->auto_save_file_name)
e95a0b39
RS
1113 && b->auto_save_modified != 0
1114 && SAVE_MODIFF < b->auto_save_modified)
1ab256cb
RM
1115 {
1116 Lisp_Object tem;
1117 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 1118 if (! NILP (tem))
cbb6a418 1119 internal_delete_file (b->auto_save_file_name);
1ab256cb
RM
1120 }
1121
4a4a9db5
KH
1122 if (b->base_buffer)
1123 {
1124 /* Unchain all markers that belong to this indirect buffer.
1125 Don't unchain the markers that belong to the base buffer
1126 or its other indirect buffers. */
1127 for (tem = BUF_MARKERS (b); !NILP (tem); )
1128 {
1129 Lisp_Object next;
1130 m = XMARKER (tem);
1131 next = m->chain;
1132 if (m->buffer == b)
1133 unchain_marker (tem);
1134 tem = next;
1135 }
1136 }
1137 else
1ab256cb 1138 {
4a4a9db5 1139 /* Unchain all markers of this buffer and its indirect buffers.
336cd056 1140 and leave them pointing nowhere. */
4a4a9db5 1141 for (tem = BUF_MARKERS (b); !NILP (tem); )
336cd056
RS
1142 {
1143 m = XMARKER (tem);
1144 m->buffer = 0;
1145 tem = m->chain;
1146 m->chain = Qnil;
1147 }
1148 BUF_MARKERS (b) = Qnil;
1ab256cb 1149
336cd056
RS
1150#ifdef USE_TEXT_PROPERTIES
1151 BUF_INTERVALS (b) = NULL_INTERVAL;
1152#endif
1153
1154 /* Perhaps we should explicitly free the interval tree here... */
1155 }
33f7013e 1156
2f3f993b
RS
1157 /* Reset the local variables, so that this buffer's local values
1158 won't be protected from GC. They would be protected
1159 if they happened to remain encached in their symbols.
1160 This gets rid of them for certain. */
1161 swap_out_buffer_local_variables (b);
13de9290 1162 reset_buffer_local_variables (b, 1);
2f3f993b 1163
1ab256cb 1164 b->name = Qnil;
336cd056 1165
9ac0d9e0 1166 BLOCK_INPUT;
336cd056
RS
1167 if (! b->base_buffer)
1168 BUFFER_FREE (BUF_BEG_ADDR (b));
1169
28e969dd
JB
1170 if (b->newline_cache)
1171 {
1172 free_region_cache (b->newline_cache);
1173 b->newline_cache = 0;
1174 }
1175 if (b->width_run_cache)
1176 {
1177 free_region_cache (b->width_run_cache);
1178 b->width_run_cache = 0;
1179 }
1180 b->width_table = Qnil;
9ac0d9e0 1181 UNBLOCK_INPUT;
1ab256cb
RM
1182 b->undo_list = Qnil;
1183
1184 return Qt;
1185}
1186\f
36a8c287
JB
1187/* Move the assoc for buffer BUF to the front of buffer-alist. Since
1188 we do this each time BUF is selected visibly, the more recently
1189 selected buffers are always closer to the front of the list. This
1190 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb
RM
1191
1192record_buffer (buf)
1193 Lisp_Object buf;
1194{
1195 register Lisp_Object link, prev;
1196
1197 prev = Qnil;
1198 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
1199 {
1200 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
1201 break;
1202 prev = link;
1203 }
1204
36a8c287
JB
1205 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1206 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 1207
265a9e55 1208 if (NILP (prev))
1ab256cb
RM
1209 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
1210 else
1211 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1212
7962a441 1213 XCONS (link)->cdr = Vbuffer_alist;
1ab256cb 1214 Vbuffer_alist = link;
7962a441
RS
1215
1216 /* Now move this buffer to the front of frame_buffer_list also. */
1217
1218 prev = Qnil;
1219 for (link = frame_buffer_list (); CONSP (link); link = XCONS (link)->cdr)
1220 {
1221 if (EQ (XCONS (link)->car, buf))
1222 break;
1223 prev = link;
1224 }
1225
1226 /* Effectively do delq. */
1227
1228 if (CONSP (link))
1229 {
1230 if (NILP (prev))
1231 set_frame_buffer_list (XCONS (frame_buffer_list ())->cdr);
1232 else
1233 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1234
1235 XCONS (link)->cdr = frame_buffer_list ();
1236 set_frame_buffer_list (link);
1237 }
1238 else
1239 set_frame_buffer_list (Fcons (buf, frame_buffer_list ()));
1ab256cb
RM
1240}
1241
a9ee7a59
KH
1242DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1243 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1244Use this function before selecting the buffer, since it may need to inspect\n\
1245the current buffer's major mode.")
a2428fa2
EN
1246 (buffer)
1247 Lisp_Object buffer;
a9ee7a59
KH
1248{
1249 int count;
1250 Lisp_Object function;
1251
1252 function = buffer_defaults.major_mode;
1253 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1254 function = current_buffer->major_mode;
1255
1256 if (NILP (function) || EQ (function, Qfundamental_mode))
1257 return Qnil;
1258
1259 count = specpdl_ptr - specpdl;
1260
1261 /* To select a nonfundamental mode,
1262 select the buffer temporarily and then call the mode function. */
1263
1264 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1265
a2428fa2 1266 Fset_buffer (buffer);
a9ee7a59
KH
1267 call0 (function);
1268
1269 return unbind_to (count, Qnil);
1270}
1271
1ab256cb
RM
1272DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1273 "Select buffer BUFFER in the current window.\n\
1274BUFFER may be a buffer or a buffer name.\n\
1275Optional second arg NORECORD non-nil means\n\
1276do not put this buffer at the front of the list of recently selected ones.\n\
1277\n\
1278WARNING: This is NOT the way to work on another buffer temporarily\n\
1279within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1280the window-buffer correspondences.")
a25f13ae
KH
1281 (buffer, norecord)
1282 Lisp_Object buffer, norecord;
1ab256cb
RM
1283{
1284 register Lisp_Object buf;
1285 Lisp_Object tem;
1286
1287 if (EQ (minibuf_window, selected_window))
1288 error ("Cannot switch buffers in minibuffer window");
1289 tem = Fwindow_dedicated_p (selected_window);
265a9e55 1290 if (!NILP (tem))
1ab256cb
RM
1291 error ("Cannot switch buffers in a dedicated window");
1292
a25f13ae 1293 if (NILP (buffer))
172a9c1f 1294 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb 1295 else
a9ee7a59 1296 {
a25f13ae 1297 buf = Fget_buffer (buffer);
a9ee7a59
KH
1298 if (NILP (buf))
1299 {
a25f13ae 1300 buf = Fget_buffer_create (buffer);
a9ee7a59
KH
1301 Fset_buffer_major_mode (buf);
1302 }
1303 }
1ab256cb 1304 Fset_buffer (buf);
265a9e55 1305 if (NILP (norecord))
1ab256cb
RM
1306 record_buffer (buf);
1307
1308 Fset_window_buffer (EQ (selected_window, minibuf_window)
5fcd022d
JB
1309 ? Fnext_window (minibuf_window, Qnil, Qnil)
1310 : selected_window,
1ab256cb
RM
1311 buf);
1312
cd0c235a 1313 return buf;
1ab256cb
RM
1314}
1315
cd0c235a 1316DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1ab256cb
RM
1317 "Select buffer BUFFER in some window, preferably a different one.\n\
1318If BUFFER is nil, then some other buffer is chosen.\n\
1319If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1320If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
405615e5
RS
1321window even if BUFFER is already visible in the selected window.\n\
1322This uses the function `display-buffer' as a subroutine; see the documentation\n\
6d12711f
RS
1323of `display-buffer' for additional customization information.\n\
1324\n\
1325Optional third arg NORECORD non-nil means\n\
1326do not put this buffer at the front of the list of recently selected ones.")
1327 (buffer, other_window, norecord)
1328 Lisp_Object buffer, other_window, norecord;
1ab256cb
RM
1329{
1330 register Lisp_Object buf;
a25f13ae 1331 if (NILP (buffer))
172a9c1f 1332 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb 1333 else
7c2087ee 1334 {
a25f13ae 1335 buf = Fget_buffer (buffer);
7c2087ee
RS
1336 if (NILP (buf))
1337 {
a25f13ae 1338 buf = Fget_buffer_create (buffer);
7c2087ee
RS
1339 Fset_buffer_major_mode (buf);
1340 }
1341 }
1ab256cb 1342 Fset_buffer (buf);
6d12711f
RS
1343 if (NILP (norecord))
1344 record_buffer (buf);
a2428fa2 1345 Fselect_window (Fdisplay_buffer (buf, other_window));
e8b3a22d 1346 return buf;
1ab256cb
RM
1347}
1348
1349DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1350 "Return the current buffer as a Lisp object.")
1351 ()
1352{
1353 register Lisp_Object buf;
67180c6a 1354 XSETBUFFER (buf, current_buffer);
1ab256cb
RM
1355 return buf;
1356}
1357\f
c7aa5005 1358/* Set the current buffer to B. */
1ab256cb
RM
1359
1360void
1361set_buffer_internal (b)
1362 register struct buffer *b;
1363{
1364 register struct buffer *old_buf;
1365 register Lisp_Object tail, valcontents;
a7a60ce9 1366 Lisp_Object tem;
1ab256cb
RM
1367
1368 if (current_buffer == b)
1369 return;
1370
1371 windows_or_buffers_changed = 1;
c7aa5005
KH
1372 set_buffer_internal_1 (b);
1373}
1374
1375/* Set the current buffer to B, and do not set windows_or_buffers_changed.
1376 This is used by redisplay. */
1377
1378void
1379set_buffer_internal_1 (b)
1380 register struct buffer *b;
1381{
1382 register struct buffer *old_buf;
1383 register Lisp_Object tail, valcontents;
1384 Lisp_Object tem;
1385
1386 if (current_buffer == b)
1387 return;
1388
1ab256cb
RM
1389 old_buf = current_buffer;
1390 current_buffer = b;
1391 last_known_column_point = -1; /* invalidate indentation cache */
1392
336cd056
RS
1393 if (old_buf)
1394 {
1395 /* Put the undo list back in the base buffer, so that it appears
1396 that an indirect buffer shares the undo list of its base. */
1397 if (old_buf->base_buffer)
1398 old_buf->base_buffer->undo_list = old_buf->undo_list;
1399
1400 /* If the old current buffer has markers to record PT, BEGV and ZV
1401 when it is not current, update them now. */
1402 if (! NILP (old_buf->pt_marker))
1403 {
1404 Lisp_Object obuf;
1405 XSETBUFFER (obuf, old_buf);
1406 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1407 }
1408 if (! NILP (old_buf->begv_marker))
1409 {
1410 Lisp_Object obuf;
1411 XSETBUFFER (obuf, old_buf);
1412 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1413 }
1414 if (! NILP (old_buf->zv_marker))
1415 {
1416 Lisp_Object obuf;
1417 XSETBUFFER (obuf, old_buf);
1418 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1419 }
1420 }
1421
1422 /* Get the undo list from the base buffer, so that it appears
1423 that an indirect buffer shares the undo list of its base. */
1424 if (b->base_buffer)
1425 b->undo_list = b->base_buffer->undo_list;
1426
1427 /* If the new current buffer has markers to record PT, BEGV and ZV
1428 when it is not current, fetch them now. */
1429 if (! NILP (b->pt_marker))
1430 BUF_PT (b) = marker_position (b->pt_marker);
1431 if (! NILP (b->begv_marker))
1432 BUF_BEGV (b) = marker_position (b->begv_marker);
1433 if (! NILP (b->zv_marker))
1434 BUF_ZV (b) = marker_position (b->zv_marker);
1435
1ab256cb
RM
1436 /* Look down buffer's list of local Lisp variables
1437 to find and update any that forward into C variables. */
1438
265a9e55 1439 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
1440 {
1441 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
a7a60ce9
KH
1442 if ((BUFFER_LOCAL_VALUEP (valcontents)
1443 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
fdc6e516 1444 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
a7a60ce9 1445 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1446 /* Just reference the variable
1447 to cause it to become set for this buffer. */
1448 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1449 }
1450
1451 /* Do the same with any others that were local to the previous buffer */
1452
1453 if (old_buf)
265a9e55 1454 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
1455 {
1456 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
a7a60ce9
KH
1457 if ((BUFFER_LOCAL_VALUEP (valcontents)
1458 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
fdc6e516 1459 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
a7a60ce9 1460 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1461 /* Just reference the variable
1462 to cause it to become set for this buffer. */
1463 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1464 }
1465}
1466
336cd056 1467/* Switch to buffer B temporarily for redisplay purposes.
bbbe9545 1468 This avoids certain things that don't need to be done within redisplay. */
336cd056
RS
1469
1470void
1471set_buffer_temp (b)
1472 struct buffer *b;
1473{
1474 register struct buffer *old_buf;
1475
1476 if (current_buffer == b)
1477 return;
1478
1479 old_buf = current_buffer;
1480 current_buffer = b;
1481
1482 if (old_buf)
1483 {
1484 /* If the old current buffer has markers to record PT, BEGV and ZV
1485 when it is not current, update them now. */
1486 if (! NILP (old_buf->pt_marker))
1487 {
1488 Lisp_Object obuf;
1489 XSETBUFFER (obuf, old_buf);
1490 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1491 }
1492 if (! NILP (old_buf->begv_marker))
1493 {
1494 Lisp_Object obuf;
1495 XSETBUFFER (obuf, old_buf);
1496 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1497 }
1498 if (! NILP (old_buf->zv_marker))
1499 {
1500 Lisp_Object obuf;
1501 XSETBUFFER (obuf, old_buf);
1502 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1503 }
1504 }
1505
1506 /* If the new current buffer has markers to record PT, BEGV and ZV
1507 when it is not current, fetch them now. */
1508 if (! NILP (b->pt_marker))
1509 BUF_PT (b) = marker_position (b->pt_marker);
1510 if (! NILP (b->begv_marker))
1511 BUF_BEGV (b) = marker_position (b->begv_marker);
1512 if (! NILP (b->zv_marker))
1513 BUF_ZV (b) = marker_position (b->zv_marker);
1514}
1515
1ab256cb
RM
1516DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1517 "Make the buffer BUFFER current for editing operations.\n\
1518BUFFER may be a buffer or the name of an existing buffer.\n\
1519See also `save-excursion' when you want to make a buffer current temporarily.\n\
1520This function does not display the buffer, so its effect ends\n\
1521when the current command terminates.\n\
1522Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
a25f13ae
KH
1523 (buffer)
1524 register Lisp_Object buffer;
1ab256cb 1525{
a25f13ae
KH
1526 register Lisp_Object buf;
1527 buf = Fget_buffer (buffer);
1528 if (NILP (buf))
1529 nsberror (buffer);
1530 if (NILP (XBUFFER (buf)->name))
1ab256cb 1531 error ("Selecting deleted buffer");
a25f13ae
KH
1532 set_buffer_internal (XBUFFER (buf));
1533 return buf;
1ab256cb
RM
1534}
1535\f
1536DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1537 Sbarf_if_buffer_read_only, 0, 0, 0,
1538 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1539 ()
1540{
a96b68f1
RS
1541 if (!NILP (current_buffer->read_only)
1542 && NILP (Vinhibit_read_only))
1ab256cb
RM
1543 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1544 return Qnil;
1545}
1546
1547DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1548 "Put BUFFER at the end of the list of all buffers.\n\
1549There it is the least likely candidate for `other-buffer' to return;\n\
528415e7 1550thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
a5611885
JB
1551If BUFFER is nil or omitted, bury the current buffer.\n\
1552Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1553selected window if it is displayed there.")
a2428fa2
EN
1554 (buffer)
1555 register Lisp_Object buffer;
1ab256cb 1556{
b271272a 1557 /* Figure out what buffer we're going to bury. */
a2428fa2 1558 if (NILP (buffer))
a5611885 1559 {
a2428fa2 1560 XSETBUFFER (buffer, current_buffer);
0a63b212
RS
1561
1562 /* If we're burying the current buffer, unshow it. */
1563 Fswitch_to_buffer (Fother_buffer (buffer, Qnil), Qnil);
a5611885 1564 }
1ab256cb
RM
1565 else
1566 {
1567 Lisp_Object buf1;
1568
a2428fa2 1569 buf1 = Fget_buffer (buffer);
265a9e55 1570 if (NILP (buf1))
a2428fa2
EN
1571 nsberror (buffer);
1572 buffer = buf1;
b271272a
JB
1573 }
1574
a2428fa2 1575 /* Move buffer to the end of the buffer list. */
b271272a
JB
1576 {
1577 register Lisp_Object aelt, link;
1578
a2428fa2 1579 aelt = Frassq (buffer, Vbuffer_alist);
b271272a
JB
1580 link = Fmemq (aelt, Vbuffer_alist);
1581 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1582 XCONS (link)->cdr = Qnil;
1583 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1584 }
1ab256cb 1585
dcb26650 1586 frames_bury_buffer (buffer);
dec989eb 1587
1ab256cb
RM
1588 return Qnil;
1589}
1590\f
c922bc55 1591DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1ab256cb 1592 "Delete the entire contents of the current buffer.\n\
2950a20e 1593Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1ab256cb
RM
1594so the buffer is truly empty after this.")
1595 ()
1596{
1597 Fwiden ();
1598 del_range (BEG, Z);
1599 current_buffer->last_window_start = 1;
1600 /* Prevent warnings, or suspension of auto saving, that would happen
1601 if future size is less than past size. Use of erase-buffer
1602 implies that the future text is not really related to the past text. */
8d7a4592 1603 XSETFASTINT (current_buffer->save_length, 0);
1ab256cb
RM
1604 return Qnil;
1605}
1606
1607validate_region (b, e)
1608 register Lisp_Object *b, *e;
1609{
1ab256cb
RM
1610 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1611 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1612
1613 if (XINT (*b) > XINT (*e))
1614 {
03192067
KH
1615 Lisp_Object tem;
1616 tem = *b; *b = *e; *e = tem;
1ab256cb
RM
1617 }
1618
1619 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1620 && XINT (*e) <= ZV))
1621 args_out_of_range (*b, *e);
1622}
1623\f
1ab256cb
RM
1624DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1625 0, 0, 0,
1626 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1627Most local variable bindings are eliminated so that the default values\n\
1628become effective once more. Also, the syntax table is set from\n\
1629`standard-syntax-table', the local keymap is set to nil,\n\
1630and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1631This function also forces redisplay of the mode line.\n\
1632\n\
1633Every function to select a new major mode starts by\n\
1634calling this function.\n\n\
1635As a special exception, local variables whose names have\n\
c5a15222
RS
1636a non-nil `permanent-local' property are not eliminated by this function.\n\
1637\n\
1638The first thing this function does is run\n\
1639the normal hook `change-major-mode-hook'.")
1ab256cb
RM
1640 ()
1641{
1642 register Lisp_Object alist, sym, tem;
1643 Lisp_Object oalist;
7410477a 1644
fd186f07
RS
1645 if (!NILP (Vrun_hooks))
1646 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1ab256cb
RM
1647 oalist = current_buffer->local_var_alist;
1648
2f3f993b
RS
1649 /* Make sure none of the bindings in oalist
1650 remain swapped in, in their symbols. */
1ab256cb 1651
2f3f993b 1652 swap_out_buffer_local_variables (current_buffer);
1ab256cb
RM
1653
1654 /* Actually eliminate all local bindings of this buffer. */
1655
13de9290 1656 reset_buffer_local_variables (current_buffer, 0);
1ab256cb
RM
1657
1658 /* Redisplay mode lines; we are changing major mode. */
1659
1660 update_mode_lines++;
1661
1662 /* Any which are supposed to be permanent,
1663 make local again, with the same values they had. */
1664
265a9e55 1665 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1666 {
1667 sym = XCONS (XCONS (alist)->car)->car;
1668 tem = Fget (sym, Qpermanent_local);
265a9e55 1669 if (! NILP (tem))
01050cb5
RM
1670 {
1671 Fmake_local_variable (sym);
1672 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1673 }
1ab256cb
RM
1674 }
1675
1676 /* Force mode-line redisplay. Useful here because all major mode
1677 commands call this function. */
1678 update_mode_lines++;
1679
1680 return Qnil;
1681}
2f3f993b
RS
1682
1683/* Make sure no local variables remain set up with buffer B
1684 for their current values. */
1685
1686static void
1687swap_out_buffer_local_variables (b)
1688 struct buffer *b;
1689{
1690 Lisp_Object oalist, alist, sym, tem, buffer;
1691
1692 XSETBUFFER (buffer, b);
1693 oalist = b->local_var_alist;
1694
1695 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1696 {
1697 sym = XCONS (XCONS (alist)->car)->car;
1698
1699 /* Need not do anything if some other buffer's binding is now encached. */
1700 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car;
1701 if (XBUFFER (tem) == current_buffer)
1702 {
1703 /* Symbol is set up for this buffer's old local value.
1704 Set it up for the current buffer with the default value. */
1705
1706 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->cdr;
1707 /* Store the symbol's current value into the alist entry
1708 it is currently set up for. This is so that, if the
1709 local is marked permanent, and we make it local again
1710 later in Fkill_all_local_variables, we don't lose the value. */
1711 XCONS (XCONS (tem)->car)->cdr
1712 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car);
1713 /* Switch to the symbol's default-value alist entry. */
1714 XCONS (tem)->car = tem;
1715 /* Mark it as current for buffer B. */
1716 XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car
1717 = buffer;
1718 /* Store the current value into any forwarding in the symbol. */
1719 store_symval_forwarding (sym, XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car,
1720 XCONS (tem)->cdr);
1721 }
1722 }
1723}
1ab256cb 1724\f
2eec3b4e
RS
1725/* Find all the overlays in the current buffer that contain position POS.
1726 Return the number found, and store them in a vector in *VEC_PTR.
1727 Store in *LEN_PTR the size allocated for the vector.
52f8ec73
JB
1728 Store in *NEXT_PTR the next position after POS where an overlay starts,
1729 or ZV if there are no more overlays.
bbbe9545 1730 Store in *PREV_PTR the previous position before POS where an overlay ends,
239c932b
RS
1731 or BEGV if there are no previous overlays.
1732 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
1733
1734 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
1735 when this function is called.
1736
1737 If EXTEND is non-zero, we make the vector bigger if necessary.
1738 If EXTEND is zero, we never extend the vector,
1739 and we store only as many overlays as will fit.
1740 But we still return the total number of overlays. */
2eec3b4e
RS
1741
1742int
239c932b 1743overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2eec3b4e 1744 int pos;
61d54cd5 1745 int extend;
2eec3b4e
RS
1746 Lisp_Object **vec_ptr;
1747 int *len_ptr;
1748 int *next_ptr;
239c932b 1749 int *prev_ptr;
1ab256cb 1750{
2eec3b4e
RS
1751 Lisp_Object tail, overlay, start, end, result;
1752 int idx = 0;
1753 int len = *len_ptr;
1754 Lisp_Object *vec = *vec_ptr;
1755 int next = ZV;
239c932b 1756 int prev = BEGV;
61d54cd5
RS
1757 int inhibit_storing = 0;
1758
2eec3b4e 1759 for (tail = current_buffer->overlays_before;
8fc0589a 1760 GC_CONSP (tail);
2eec3b4e
RS
1761 tail = XCONS (tail)->cdr)
1762 {
239c932b 1763 int startpos, endpos;
52f8ec73 1764
2eec3b4e 1765 overlay = XCONS (tail)->car;
1ab256cb 1766
2eec3b4e
RS
1767 start = OVERLAY_START (overlay);
1768 end = OVERLAY_END (overlay);
239c932b
RS
1769 endpos = OVERLAY_POSITION (end);
1770 if (endpos < pos)
1771 {
1772 if (prev < endpos)
1773 prev = endpos;
1774 break;
1775 }
1776 if (endpos == pos)
1777 continue;
2eec3b4e
RS
1778 startpos = OVERLAY_POSITION (start);
1779 if (startpos <= pos)
1780 {
1781 if (idx == len)
1782 {
61d54cd5
RS
1783 /* The supplied vector is full.
1784 Either make it bigger, or don't store any more in it. */
1785 if (extend)
1786 {
1787 *len_ptr = len *= 2;
1788 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1789 *vec_ptr = vec;
1790 }
1791 else
1792 inhibit_storing = 1;
2eec3b4e 1793 }
61d54cd5
RS
1794
1795 if (!inhibit_storing)
1796 vec[idx] = overlay;
1797 /* Keep counting overlays even if we can't return them all. */
1798 idx++;
2eec3b4e
RS
1799 }
1800 else if (startpos < next)
1801 next = startpos;
1802 }
1803
1804 for (tail = current_buffer->overlays_after;
8fc0589a 1805 GC_CONSP (tail);
2eec3b4e 1806 tail = XCONS (tail)->cdr)
1ab256cb 1807 {
239c932b 1808 int startpos, endpos;
52f8ec73 1809
2eec3b4e 1810 overlay = XCONS (tail)->car;
2eec3b4e
RS
1811
1812 start = OVERLAY_START (overlay);
1813 end = OVERLAY_END (overlay);
1814 startpos = OVERLAY_POSITION (start);
52f8ec73 1815 if (pos < startpos)
2eec3b4e
RS
1816 {
1817 if (startpos < next)
1818 next = startpos;
1819 break;
1820 }
239c932b
RS
1821 endpos = OVERLAY_POSITION (end);
1822 if (pos < endpos)
2eec3b4e
RS
1823 {
1824 if (idx == len)
1825 {
61d54cd5
RS
1826 if (extend)
1827 {
1828 *len_ptr = len *= 2;
1829 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1830 *vec_ptr = vec;
1831 }
1832 else
1833 inhibit_storing = 1;
2eec3b4e 1834 }
61d54cd5
RS
1835
1836 if (!inhibit_storing)
1837 vec[idx] = overlay;
1838 idx++;
2eec3b4e 1839 }
239c932b
RS
1840 else if (endpos < pos && endpos > prev)
1841 prev = endpos;
1ab256cb
RM
1842 }
1843
239c932b
RS
1844 if (next_ptr)
1845 *next_ptr = next;
1846 if (prev_ptr)
1847 *prev_ptr = prev;
2eec3b4e
RS
1848 return idx;
1849}
74514898
RS
1850\f
1851/* Find all the overlays in the current buffer that overlap the range BEG-END
2a3eeee7
RS
1852 or are empty at BEG.
1853
74514898
RS
1854 Return the number found, and store them in a vector in *VEC_PTR.
1855 Store in *LEN_PTR the size allocated for the vector.
1856 Store in *NEXT_PTR the next position after POS where an overlay starts,
1857 or ZV if there are no more overlays.
1858 Store in *PREV_PTR the previous position before POS where an overlay ends,
1859 or BEGV if there are no previous overlays.
1860 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
1861
1862 *VEC_PTR and *LEN_PTR should contain a valid vector and size
1863 when this function is called.
1864
1865 If EXTEND is non-zero, we make the vector bigger if necessary.
1866 If EXTEND is zero, we never extend the vector,
1867 and we store only as many overlays as will fit.
1868 But we still return the total number of overlays. */
1869
1870int
1871overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
1872 int beg, end;
1873 int extend;
1874 Lisp_Object **vec_ptr;
1875 int *len_ptr;
1876 int *next_ptr;
1877 int *prev_ptr;
1878{
1879 Lisp_Object tail, overlay, ostart, oend, result;
1880 int idx = 0;
1881 int len = *len_ptr;
1882 Lisp_Object *vec = *vec_ptr;
1883 int next = ZV;
1884 int prev = BEGV;
1885 int inhibit_storing = 0;
1886
1887 for (tail = current_buffer->overlays_before;
1888 GC_CONSP (tail);
1889 tail = XCONS (tail)->cdr)
1890 {
1891 int startpos, endpos;
1892
1893 overlay = XCONS (tail)->car;
1894
1895 ostart = OVERLAY_START (overlay);
1896 oend = OVERLAY_END (overlay);
1897 endpos = OVERLAY_POSITION (oend);
1898 if (endpos < beg)
1899 {
1900 if (prev < endpos)
1901 prev = endpos;
1902 break;
1903 }
1904 startpos = OVERLAY_POSITION (ostart);
1905 /* Count an interval if it either overlaps the range
2a3eeee7 1906 or is empty at the start of the range. */
74514898 1907 if ((beg < endpos && startpos < end)
2a3eeee7 1908 || (startpos == endpos && beg == endpos))
74514898
RS
1909 {
1910 if (idx == len)
1911 {
1912 /* The supplied vector is full.
1913 Either make it bigger, or don't store any more in it. */
1914 if (extend)
1915 {
1916 *len_ptr = len *= 2;
1917 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1918 *vec_ptr = vec;
1919 }
1920 else
1921 inhibit_storing = 1;
1922 }
1923
1924 if (!inhibit_storing)
1925 vec[idx] = overlay;
1926 /* Keep counting overlays even if we can't return them all. */
1927 idx++;
1928 }
1929 else if (startpos < next)
1930 next = startpos;
1931 }
1932
1933 for (tail = current_buffer->overlays_after;
1934 GC_CONSP (tail);
1935 tail = XCONS (tail)->cdr)
1936 {
1937 int startpos, endpos;
1938
1939 overlay = XCONS (tail)->car;
1940
1941 ostart = OVERLAY_START (overlay);
1942 oend = OVERLAY_END (overlay);
1943 startpos = OVERLAY_POSITION (ostart);
1944 if (end < startpos)
1945 {
1946 if (startpos < next)
1947 next = startpos;
1948 break;
1949 }
1950 endpos = OVERLAY_POSITION (oend);
2a3eeee7
RS
1951 /* Count an interval if it either overlaps the range
1952 or is empty at the start of the range. */
74514898 1953 if ((beg < endpos && startpos < end)
2a3eeee7 1954 || (startpos == endpos && beg == endpos))
74514898
RS
1955 {
1956 if (idx == len)
1957 {
1958 if (extend)
1959 {
1960 *len_ptr = len *= 2;
1961 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1962 *vec_ptr = vec;
1963 }
1964 else
1965 inhibit_storing = 1;
1966 }
1967
1968 if (!inhibit_storing)
1969 vec[idx] = overlay;
1970 idx++;
1971 }
1972 else if (endpos < beg && endpos > prev)
1973 prev = endpos;
1974 }
fc04fa47 1975
74514898
RS
1976 if (next_ptr)
1977 *next_ptr = next;
1978 if (prev_ptr)
1979 *prev_ptr = prev;
1980 return idx;
1981}
1982\f
fc04fa47
KH
1983/* Fast function to just test if we're at an overlay boundary. */
1984int
1985overlay_touches_p (pos)
1986 int pos;
1987{
1988 Lisp_Object tail, overlay;
1989
1990 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
1991 tail = XCONS (tail)->cdr)
1992 {
1993 int endpos;
1994
1995 overlay = XCONS (tail)->car;
1996 if (!GC_OVERLAYP (overlay))
1997 abort ();
1998
1999 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2000 if (endpos < pos)
2001 break;
2002 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2003 return 1;
2004 }
2005
2006 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2007 tail = XCONS (tail)->cdr)
2008 {
2009 int startpos;
2010
2011 overlay = XCONS (tail)->car;
2012 if (!GC_OVERLAYP (overlay))
2013 abort ();
2014
2015 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2016 if (pos < startpos)
2017 break;
2018 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2019 return 1;
2020 }
2021 return 0;
2022}
2eec3b4e 2023\f
5985d248
KH
2024struct sortvec
2025{
2026 Lisp_Object overlay;
2027 int beg, end;
2028 int priority;
2029};
2030
2031static int
2032compare_overlays (s1, s2)
2033 struct sortvec *s1, *s2;
2034{
2035 if (s1->priority != s2->priority)
2036 return s1->priority - s2->priority;
2037 if (s1->beg != s2->beg)
2038 return s1->beg - s2->beg;
2039 if (s1->end != s2->end)
2040 return s2->end - s1->end;
2041 return 0;
2042}
2043
2044/* Sort an array of overlays by priority. The array is modified in place.
2045 The return value is the new size; this may be smaller than the original
2046 size if some of the overlays were invalid or were window-specific. */
2047int
2048sort_overlays (overlay_vec, noverlays, w)
2049 Lisp_Object *overlay_vec;
2050 int noverlays;
2051 struct window *w;
2052{
2053 int i, j;
2054 struct sortvec *sortvec;
2055 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2056
2057 /* Put the valid and relevant overlays into sortvec. */
2058
2059 for (i = 0, j = 0; i < noverlays; i++)
2060 {
0fa767e7 2061 Lisp_Object tem;
c99fc30f 2062 Lisp_Object overlay;
5985d248 2063
c99fc30f 2064 overlay = overlay_vec[i];
5985d248
KH
2065 if (OVERLAY_VALID (overlay)
2066 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2067 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2068 {
0fa767e7
KH
2069 /* If we're interested in a specific window, then ignore
2070 overlays that are limited to some other window. */
2071 if (w)
5985d248 2072 {
0fa767e7
KH
2073 Lisp_Object window;
2074
2075 window = Foverlay_get (overlay, Qwindow);
a7a60ce9 2076 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 2077 continue;
5985d248 2078 }
0fa767e7
KH
2079
2080 /* This overlay is good and counts: put it into sortvec. */
2081 sortvec[j].overlay = overlay;
2082 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2083 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2084 tem = Foverlay_get (overlay, Qpriority);
2085 if (INTEGERP (tem))
2086 sortvec[j].priority = XINT (tem);
2087 else
2088 sortvec[j].priority = 0;
2089 j++;
5985d248
KH
2090 }
2091 }
2092 noverlays = j;
2093
2094 /* Sort the overlays into the proper order: increasing priority. */
2095
2096 if (noverlays > 1)
2097 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2098
2099 for (i = 0; i < noverlays; i++)
2100 overlay_vec[i] = sortvec[i].overlay;
2101 return (noverlays);
2102}
2103\f
bbbe9545
KH
2104struct sortstr
2105{
cb26008f 2106 Lisp_Object string, string2;
bbbe9545
KH
2107 int size;
2108 int priority;
2109};
2110
e8185fa8
KH
2111struct sortstrlist
2112{
2113 struct sortstr *buf; /* An array that expands as needed; never freed. */
2114 int size; /* Allocated length of that array. */
2115 int used; /* How much of the array is currently in use. */
2116 int bytes; /* Total length of the strings in buf. */
2117};
2118
2119/* Buffers for storing information about the overlays touching a given
2120 position. These could be automatic variables in overlay_strings, but
2121 it's more efficient to hold onto the memory instead of repeatedly
2122 allocating and freeing it. */
2123static struct sortstrlist overlay_heads, overlay_tails;
9492daf2 2124static unsigned char *overlay_str_buf;
e8185fa8
KH
2125
2126/* Allocated length of overlay_str_buf. */
2127static int overlay_str_len;
2128
bbbe9545
KH
2129/* A comparison function suitable for passing to qsort. */
2130static int
2131cmp_for_strings (as1, as2)
2132 char *as1, *as2;
2133{
2134 struct sortstr *s1 = (struct sortstr *)as1;
2135 struct sortstr *s2 = (struct sortstr *)as2;
2136 if (s1->size != s2->size)
2137 return s2->size - s1->size;
2138 if (s1->priority != s2->priority)
2139 return s1->priority - s2->priority;
2140 return 0;
2141}
2142
e8185fa8 2143static void
cb26008f 2144record_overlay_string (ssl, str, str2, pri, size)
e8185fa8 2145 struct sortstrlist *ssl;
cb26008f 2146 Lisp_Object str, str2, pri;
e8185fa8
KH
2147 int size;
2148{
2149 if (ssl->used == ssl->size)
2150 {
2151 if (ssl->buf)
2152 ssl->size *= 2;
2153 else
2154 ssl->size = 5;
2155 ssl->buf = ((struct sortstr *)
2156 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2157 }
2158 ssl->buf[ssl->used].string = str;
cb26008f 2159 ssl->buf[ssl->used].string2 = str2;
e8185fa8
KH
2160 ssl->buf[ssl->used].size = size;
2161 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2162 ssl->used++;
2163 ssl->bytes += XSTRING (str)->size;
cb26008f
KH
2164 if (STRINGP (str2))
2165 ssl->bytes += XSTRING (str2)->size;
e8185fa8 2166}
bbbe9545
KH
2167
2168/* Return the concatenation of the strings associated with overlays that
2169 begin or end at POS, ignoring overlays that are specific to a window
2170 other than W. The strings are concatenated in the appropriate order:
2171 shorter overlays nest inside longer ones, and higher priority inside
cb26008f
KH
2172 lower. Normally all of the after-strings come first, but zero-sized
2173 overlays have their after-strings ride along with the before-strings
2174 because it would look strange to print them inside-out.
2175
2176 Returns the string length, and stores the contents indirectly through
2177 PSTR, if that variable is non-null. The string may be overwritten by
2178 subsequent calls. */
6b5d3b89 2179
bbbe9545
KH
2180int
2181overlay_strings (pos, w, pstr)
2182 int pos;
2183 struct window *w;
6b5d3b89 2184 unsigned char **pstr;
bbbe9545 2185{
e8185fa8 2186 Lisp_Object ov, overlay, window, str;
bbbe9545
KH
2187 int startpos, endpos;
2188
e8185fa8
KH
2189 overlay_heads.used = overlay_heads.bytes = 0;
2190 overlay_tails.used = overlay_tails.bytes = 0;
bbbe9545
KH
2191 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCONS (ov)->cdr)
2192 {
2193 overlay = XCONS (ov)->car;
2194 if (!OVERLAYP (overlay))
2195 abort ();
2196
2197 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2198 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2199 if (endpos < pos)
2200 break;
2201 if (endpos != pos && startpos != pos)
2202 continue;
2203 window = Foverlay_get (overlay, Qwindow);
2204 if (WINDOWP (window) && XWINDOW (window) != w)
2205 continue;
e8185fa8
KH
2206 if (startpos == pos
2207 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2208 record_overlay_string (&overlay_heads, str,
cb26008f
KH
2209 (startpos == endpos
2210 ? Foverlay_get (overlay, Qafter_string)
2211 : Qnil),
2212 Foverlay_get (overlay, Qpriority),
2213 endpos - startpos);
2214 else if (endpos == pos
2215 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2216 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
2217 Foverlay_get (overlay, Qpriority),
2218 endpos - startpos);
bbbe9545
KH
2219 }
2220 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCONS (ov)->cdr)
2221 {
2222 overlay = XCONS (ov)->car;
2223 if (!OVERLAYP (overlay))
2224 abort ();
2225
2226 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2227 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2228 if (startpos > pos)
2229 break;
e8185fa8
KH
2230 if (endpos != pos && startpos != pos)
2231 continue;
2232 window = Foverlay_get (overlay, Qwindow);
2233 if (WINDOWP (window) && XWINDOW (window) != w)
2234 continue;
e8185fa8
KH
2235 if (startpos == pos
2236 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2237 record_overlay_string (&overlay_heads, str,
cb26008f
KH
2238 (startpos == endpos
2239 ? Foverlay_get (overlay, Qafter_string)
2240 : Qnil),
2241 Foverlay_get (overlay, Qpriority),
2242 endpos - startpos);
2243 else if (endpos == pos
2244 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2245 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
2246 Foverlay_get (overlay, Qpriority),
2247 endpos - startpos);
bbbe9545 2248 }
e8185fa8
KH
2249 if (overlay_tails.used > 1)
2250 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2251 cmp_for_strings);
2252 if (overlay_heads.used > 1)
2253 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2254 cmp_for_strings);
2255 if (overlay_heads.bytes || overlay_tails.bytes)
bbbe9545 2256 {
e8185fa8 2257 Lisp_Object tem;
bbbe9545 2258 int i;
9f4d7cde 2259 unsigned char *p;
e8185fa8 2260 int total = overlay_heads.bytes + overlay_tails.bytes;
bbbe9545
KH
2261
2262 if (total > overlay_str_len)
9f4d7cde
RS
2263 {
2264 overlay_str_len = total;
2265 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2266 total);
2267 }
bbbe9545 2268 p = overlay_str_buf;
e8185fa8 2269 for (i = overlay_tails.used; --i >= 0;)
bbbe9545 2270 {
e8185fa8 2271 tem = overlay_tails.buf[i].string;
bbbe9545
KH
2272 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2273 p += XSTRING (tem)->size;
2274 }
e8185fa8 2275 for (i = 0; i < overlay_heads.used; ++i)
bbbe9545 2276 {
e8185fa8 2277 tem = overlay_heads.buf[i].string;
bbbe9545
KH
2278 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2279 p += XSTRING (tem)->size;
cb26008f
KH
2280 tem = overlay_heads.buf[i].string2;
2281 if (STRINGP (tem))
2282 {
2283 bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
2284 p += XSTRING (tem)->size;
2285 }
bbbe9545 2286 }
cb26008f
KH
2287 if (p != overlay_str_buf + total)
2288 abort ();
bbbe9545
KH
2289 if (pstr)
2290 *pstr = overlay_str_buf;
e8185fa8 2291 return total;
bbbe9545 2292 }
e8185fa8 2293 return 0;
bbbe9545
KH
2294}
2295\f
5c4f68f1 2296/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 2297
2eec3b4e 2298void
5c4f68f1
JB
2299recenter_overlay_lists (buf, pos)
2300 struct buffer *buf;
2eec3b4e
RS
2301 int pos;
2302{
2303 Lisp_Object overlay, tail, next, prev, beg, end;
2304
2305 /* See if anything in overlays_before should move to overlays_after. */
2306
2307 /* We don't strictly need prev in this loop; it should always be nil.
2308 But we use it for symmetry and in case that should cease to be true
2309 with some future change. */
2310 prev = Qnil;
5c4f68f1 2311 for (tail = buf->overlays_before;
2eec3b4e
RS
2312 CONSP (tail);
2313 prev = tail, tail = next)
1ab256cb 2314 {
2eec3b4e
RS
2315 next = XCONS (tail)->cdr;
2316 overlay = XCONS (tail)->car;
2317
2318 /* If the overlay is not valid, get rid of it. */
2319 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
2320#if 1
2321 abort ();
2322#else
2eec3b4e
RS
2323 {
2324 /* Splice the cons cell TAIL out of overlays_before. */
2325 if (!NILP (prev))
2326 XCONS (prev)->cdr = next;
2327 else
5c4f68f1 2328 buf->overlays_before = next;
2eec3b4e
RS
2329 tail = prev;
2330 continue;
2331 }
52f8ec73 2332#endif
1ab256cb 2333
2eec3b4e
RS
2334 beg = OVERLAY_START (overlay);
2335 end = OVERLAY_END (overlay);
1ab256cb 2336
2eec3b4e 2337 if (OVERLAY_POSITION (end) > pos)
1ab256cb 2338 {
2eec3b4e
RS
2339 /* OVERLAY needs to be moved. */
2340 int where = OVERLAY_POSITION (beg);
2341 Lisp_Object other, other_prev;
2342
2343 /* Splice the cons cell TAIL out of overlays_before. */
2344 if (!NILP (prev))
2345 XCONS (prev)->cdr = next;
2346 else
5c4f68f1 2347 buf->overlays_before = next;
2eec3b4e
RS
2348
2349 /* Search thru overlays_after for where to put it. */
2350 other_prev = Qnil;
5c4f68f1 2351 for (other = buf->overlays_after;
2eec3b4e
RS
2352 CONSP (other);
2353 other_prev = other, other = XCONS (other)->cdr)
1ab256cb 2354 {
2eec3b4e
RS
2355 Lisp_Object otherbeg, otheroverlay, follower;
2356 int win;
2357
2358 otheroverlay = XCONS (other)->car;
2359 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 2360 abort ();
2eec3b4e
RS
2361
2362 otherbeg = OVERLAY_START (otheroverlay);
2363 if (OVERLAY_POSITION (otherbeg) >= where)
2364 break;
1ab256cb 2365 }
2eec3b4e
RS
2366
2367 /* Add TAIL to overlays_after before OTHER. */
2368 XCONS (tail)->cdr = other;
2369 if (!NILP (other_prev))
2370 XCONS (other_prev)->cdr = tail;
1ab256cb 2371 else
5c4f68f1 2372 buf->overlays_after = tail;
2eec3b4e 2373 tail = prev;
1ab256cb 2374 }
2eec3b4e
RS
2375 else
2376 /* We've reached the things that should stay in overlays_before.
2377 All the rest of overlays_before must end even earlier,
2378 so stop now. */
2379 break;
2380 }
2381
2382 /* See if anything in overlays_after should be in overlays_before. */
2383 prev = Qnil;
5c4f68f1 2384 for (tail = buf->overlays_after;
2eec3b4e
RS
2385 CONSP (tail);
2386 prev = tail, tail = next)
2387 {
2388 next = XCONS (tail)->cdr;
2389 overlay = XCONS (tail)->car;
2390
2391 /* If the overlay is not valid, get rid of it. */
2392 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
2393#if 1
2394 abort ();
2395#else
2eec3b4e
RS
2396 {
2397 /* Splice the cons cell TAIL out of overlays_after. */
2398 if (!NILP (prev))
2399 XCONS (prev)->cdr = next;
2400 else
5c4f68f1 2401 buf->overlays_after = next;
2eec3b4e
RS
2402 tail = prev;
2403 continue;
2404 }
52f8ec73 2405#endif
2eec3b4e
RS
2406
2407 beg = OVERLAY_START (overlay);
2408 end = OVERLAY_END (overlay);
2409
2410 /* Stop looking, when we know that nothing further
2411 can possibly end before POS. */
2412 if (OVERLAY_POSITION (beg) > pos)
2413 break;
2414
2415 if (OVERLAY_POSITION (end) <= pos)
2416 {
2417 /* OVERLAY needs to be moved. */
2418 int where = OVERLAY_POSITION (end);
2419 Lisp_Object other, other_prev;
2420
2421 /* Splice the cons cell TAIL out of overlays_after. */
2422 if (!NILP (prev))
2423 XCONS (prev)->cdr = next;
2424 else
5c4f68f1 2425 buf->overlays_after = next;
2eec3b4e
RS
2426
2427 /* Search thru overlays_before for where to put it. */
2428 other_prev = Qnil;
5c4f68f1 2429 for (other = buf->overlays_before;
2eec3b4e
RS
2430 CONSP (other);
2431 other_prev = other, other = XCONS (other)->cdr)
2432 {
2433 Lisp_Object otherend, otheroverlay;
2434 int win;
2435
2436 otheroverlay = XCONS (other)->car;
2437 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 2438 abort ();
2eec3b4e
RS
2439
2440 otherend = OVERLAY_END (otheroverlay);
2441 if (OVERLAY_POSITION (otherend) <= where)
2442 break;
2443 }
2444
2445 /* Add TAIL to overlays_before before OTHER. */
2446 XCONS (tail)->cdr = other;
2447 if (!NILP (other_prev))
2448 XCONS (other_prev)->cdr = tail;
2449 else
5c4f68f1 2450 buf->overlays_before = tail;
2eec3b4e
RS
2451 tail = prev;
2452 }
2453 }
2454
8d7a4592 2455 XSETFASTINT (buf->overlay_center, pos);
2eec3b4e 2456}
2b1bdf65 2457
423cdb46
KH
2458void
2459adjust_overlays_for_insert (pos, length)
2460 int pos;
2461 int length;
2462{
2463 /* After an insertion, the lists are still sorted properly,
2464 but we may need to update the value of the overlay center. */
2465 if (XFASTINT (current_buffer->overlay_center) >= pos)
2466 XSETFASTINT (current_buffer->overlay_center,
2467 XFASTINT (current_buffer->overlay_center) + length);
2468}
2469
2470void
2471adjust_overlays_for_delete (pos, length)
2472 int pos;
2473 int length;
2474{
2475 if (XFASTINT (current_buffer->overlay_center) < pos)
2476 /* The deletion was to our right. No change needed; the before- and
2477 after-lists are still consistent. */
2478 ;
2479 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
2480 /* The deletion was to our left. We need to adjust the center value
2481 to account for the change in position, but the lists are consistent
2482 given the new value. */
2483 XSETFASTINT (current_buffer->overlay_center,
2484 XFASTINT (current_buffer->overlay_center) - length);
2485 else
2486 /* We're right in the middle. There might be things on the after-list
2487 that now belong on the before-list. Recentering will move them,
2488 and also update the center point. */
2489 recenter_overlay_lists (current_buffer, pos);
2490}
2491
2b1bdf65
KH
2492/* Fix up overlays that were garbled as a result of permuting markers
2493 in the range START through END. Any overlay with at least one
2494 endpoint in this range will need to be unlinked from the overlay
2495 list and reinserted in its proper place.
2496 Such an overlay might even have negative size at this point.
2497 If so, we'll reverse the endpoints. Can you think of anything
2498 better to do in this situation? */
2499void
2500fix_overlays_in_range (start, end)
2501 register int start, end;
2502{
2503 Lisp_Object tem, overlay;
2504 Lisp_Object before_list, after_list;
2505 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
2506 int startpos, endpos;
2507
2508 /* This algorithm shifts links around instead of consing and GCing.
2509 The loop invariant is that before_list (resp. after_list) is a
2510 well-formed list except that its last element, the one that
2511 *pbefore (resp. *pafter) points to, is still uninitialized.
2512 So it's not a bug that before_list isn't initialized, although
2513 it may look strange. */
2514 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
2515 {
2516 overlay = XCONS (*ptail)->car;
2517 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2518 if (endpos < start)
2519 break;
2520 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2521 if (endpos < end
2522 || (startpos >= start && startpos < end))
2523 {
2524 /* If the overlay is backwards, fix that now. */
2525 if (startpos > endpos)
2526 {
2527 int tem;
2528 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2529 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2530 tem = startpos; startpos = endpos; endpos = tem;
2531 }
2532 /* Add it to the end of the wrong list. Later on,
2533 recenter_overlay_lists will move it to the right place. */
2534 if (endpos < XINT (current_buffer->overlay_center))
2535 {
2536 *pafter = *ptail;
2537 pafter = &XCONS (*ptail)->cdr;
2538 }
2539 else
2540 {
2541 *pbefore = *ptail;
2542 pbefore = &XCONS (*ptail)->cdr;
2543 }
2544 *ptail = XCONS (*ptail)->cdr;
2545 }
2546 else
2547 ptail = &XCONS (*ptail)->cdr;
2548 }
2549 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
2550 {
2551 overlay = XCONS (*ptail)->car;
2552 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2553 if (startpos >= end)
2554 break;
2555 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2556 if (startpos >= start
2557 || (endpos >= start && endpos < end))
2558 {
2559 if (startpos > endpos)
2560 {
2561 int tem;
2562 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
2563 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
2564 tem = startpos; startpos = endpos; endpos = tem;
2565 }
2566 if (endpos < XINT (current_buffer->overlay_center))
2567 {
2568 *pafter = *ptail;
2569 pafter = &XCONS (*ptail)->cdr;
2570 }
2571 else
2572 {
2573 *pbefore = *ptail;
2574 pbefore = &XCONS (*ptail)->cdr;
2575 }
2576 *ptail = XCONS (*ptail)->cdr;
2577 }
2578 else
2579 ptail = &XCONS (*ptail)->cdr;
2580 }
2581
2582 /* Splice the constructed (wrong) lists into the buffer's lists,
2583 and let the recenter function make it sane again. */
2584 *pbefore = current_buffer->overlays_before;
2585 current_buffer->overlays_before = before_list;
2586 recenter_overlay_lists (current_buffer,
2587 XINT (current_buffer->overlay_center));
2588
2589 *pafter = current_buffer->overlays_after;
2590 current_buffer->overlays_after = after_list;
2591 recenter_overlay_lists (current_buffer,
2592 XINT (current_buffer->overlay_center));
2593}
3b06f880
KH
2594
2595/* We have two types of overlay: the one whose ending marker is
2596 after-insertion-marker (this is the usual case) and the one whose
2597 ending marker is before-insertion-marker. When `overlays_before'
2598 contains overlays of the latter type and the former type in this
2599 order and both overlays end at inserting position, inserting a text
2600 increases only the ending marker of the latter type, which results
2601 in incorrect ordering of `overlays_before'.
2602
2603 This function fixes ordering of overlays in the slot
2604 `overlays_before' of the buffer *BP. Before the insertion, `point'
2605 was at PREV, and now is at POS. */
2606
2607fix_overlays_before (bp, prev, pos)
2608 struct buffer *bp;
2609 int prev, pos;
2610{
2611 Lisp_Object *tailp = &bp->overlays_before;
2612 Lisp_Object *right_place;
2613 int end;
2614
2615 /* After the insertion, the several overlays may be in incorrect
2616 order. The possibility is that, in the list `overlays_before',
2617 an overlay which ends at POS appears after an overlay which ends
2618 at PREV. Since POS is greater than PREV, we must fix the
2619 ordering of these overlays, by moving overlays ends at POS before
2620 the overlays ends at PREV. */
2621
2622 /* At first, find a place where disordered overlays should be linked
2623 in. It is where an overlay which end before POS exists. (i.e. an
2624 overlay whose ending marker is after-insertion-marker if disorder
2625 exists). */
2626 while (!NILP (*tailp)
2627 && ((end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car)))
2628 >= pos))
2629 tailp = &XCONS (*tailp)->cdr;
2630
2631 /* If we don't find such an overlay,
2632 or the found one ends before PREV,
2633 or the found one is the last one in the list,
2634 we don't have to fix anything. */
2635 if (NILP (*tailp)
2636 || end < prev
2637 || NILP (XCONS (*tailp)->cdr))
2638 return;
2639
2640 right_place = tailp;
2641 tailp = &XCONS (*tailp)->cdr;
2642
2643 /* Now, end position of overlays in the list *TAILP should be before
2644 or equal to PREV. In the loop, an overlay which ends at POS is
2645 moved ahead to the place pointed by RIGHT_PLACE. If we found an
2646 overlay which ends before PREV, the remaining overlays are in
2647 correct order. */
2648 while (!NILP (*tailp))
2649 {
2650 end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car));
2651
2652 if (end == pos)
2653 { /* This overlay is disordered. */
2654 Lisp_Object found = *tailp;
2655
2656 /* Unlink the found overlay. */
2657 *tailp = XCONS (found)->cdr;
2658 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
2659 XCONS (found)->cdr = *right_place;
2660 /* Link it into the right place. */
2661 *right_place = found;
2662 }
2663 else if (end == prev)
2664 tailp = &XCONS (*tailp)->cdr;
2665 else /* No more disordered overlay. */
2666 break;
2667 }
2668}
2eec3b4e 2669\f
52f8ec73
JB
2670DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
2671 "Return t if OBJECT is an overlay.")
2672 (object)
2673 Lisp_Object object;
2674{
2675 return (OVERLAYP (object) ? Qt : Qnil);
2676}
2677
acac2700 2678DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
5c4f68f1
JB
2679 "Create a new overlay with range BEG to END in BUFFER.\n\
2680If omitted, BUFFER defaults to the current buffer.\n\
acac2700
RS
2681BEG and END may be integers or markers.\n\
2682The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
2683front delimiter advance when text is inserted there.\n\
2684The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
2685rear delimiter advance when text is inserted there.")
2686 (beg, end, buffer, front_advance, rear_advance)
5c4f68f1 2687 Lisp_Object beg, end, buffer;
acac2700 2688 Lisp_Object front_advance, rear_advance;
2eec3b4e
RS
2689{
2690 Lisp_Object overlay;
5c4f68f1 2691 struct buffer *b;
2eec3b4e 2692
5c4f68f1 2693 if (NILP (buffer))
67180c6a 2694 XSETBUFFER (buffer, current_buffer);
883047b9
JB
2695 else
2696 CHECK_BUFFER (buffer, 2);
2697 if (MARKERP (beg)
2698 && ! EQ (Fmarker_buffer (beg), buffer))
2699 error ("Marker points into wrong buffer");
2700 if (MARKERP (end)
2701 && ! EQ (Fmarker_buffer (end), buffer))
2702 error ("Marker points into wrong buffer");
2eec3b4e 2703
883047b9
JB
2704 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2705 CHECK_NUMBER_COERCE_MARKER (end, 1);
5c4f68f1 2706
883047b9 2707 if (XINT (beg) > XINT (end))
5c4f68f1 2708 {
c99fc30f
KH
2709 Lisp_Object temp;
2710 temp = beg; beg = end; end = temp;
5c4f68f1 2711 }
883047b9
JB
2712
2713 b = XBUFFER (buffer);
2714
2715 beg = Fset_marker (Fmake_marker (), beg, buffer);
2716 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 2717
acac2700
RS
2718 if (!NILP (front_advance))
2719 XMARKER (beg)->insertion_type = 1;
2720 if (!NILP (rear_advance))
2721 XMARKER (end)->insertion_type = 1;
597dd755 2722
48e2e3ba 2723 overlay = allocate_misc ();
89ca3e1b 2724 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
48e2e3ba
KH
2725 XOVERLAY (overlay)->start = beg;
2726 XOVERLAY (overlay)->end = end;
2727 XOVERLAY (overlay)->plist = Qnil;
2eec3b4e
RS
2728
2729 /* Put the new overlay on the wrong list. */
2730 end = OVERLAY_END (overlay);
5c4f68f1
JB
2731 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2732 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 2733 else
5c4f68f1 2734 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
2735
2736 /* This puts it in the right list, and in the right order. */
5c4f68f1 2737 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 2738
b61982dd
JB
2739 /* We don't need to redisplay the region covered by the overlay, because
2740 the overlay has no properties at the moment. */
2741
2eec3b4e
RS
2742 return overlay;
2743}
876aa27c
RS
2744\f
2745/* Mark a section of BUF as needing redisplay because of overlays changes. */
2746
2747static void
2748modify_overlay (buf, start, end)
2749 struct buffer *buf;
2750 int start, end;
2751{
2752 if (start == end)
2753 return;
2754
2755 if (start > end)
2756 {
2757 int temp = start;
2758 start = end; end = temp;
2759 }
2760
2761 /* If this is a buffer not in the selected window,
2762 we must do other windows. */
2763 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2764 windows_or_buffers_changed = 1;
2765 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2766 else if (buf != current_buffer)
2767 windows_or_buffers_changed = 1;
2768 /* If multiple windows show this buffer, we must do other windows. */
2769 else if (buffer_shared > 1)
2770 windows_or_buffers_changed = 1;
2771 else
2772 {
2773 if (unchanged_modified == MODIFF
2774 && overlay_unchanged_modified == OVERLAY_MODIFF)
2775 {
2776 beg_unchanged = start - BEG;
2777 end_unchanged = Z - end;
2778 }
2779 else
2780 {
2781 if (Z - end < end_unchanged)
2782 end_unchanged = Z - end;
2783 if (start - BEG < beg_unchanged)
2784 beg_unchanged = start - BEG;
2785 }
2786 }
2787
2788 ++OVERLAY_MODIFF;
2789}
2eec3b4e 2790
876aa27c 2791\f\f
5c4f68f1
JB
2792DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
2793 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3ece337a
JB
2794If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
2795If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
2796buffer.")
5c4f68f1
JB
2797 (overlay, beg, end, buffer)
2798 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 2799{
0a4469c9
RS
2800 struct buffer *b, *ob;
2801 Lisp_Object obuffer;
2802 int count = specpdl_ptr - specpdl;
5c4f68f1 2803
52f8ec73 2804 CHECK_OVERLAY (overlay, 0);
5c4f68f1
JB
2805 if (NILP (buffer))
2806 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 2807 if (NILP (buffer))
67180c6a 2808 XSETBUFFER (buffer, current_buffer);
5c4f68f1 2809 CHECK_BUFFER (buffer, 3);
883047b9
JB
2810
2811 if (MARKERP (beg)
2812 && ! EQ (Fmarker_buffer (beg), buffer))
2813 error ("Marker points into wrong buffer");
2814 if (MARKERP (end)
2815 && ! EQ (Fmarker_buffer (end), buffer))
2816 error ("Marker points into wrong buffer");
2817
b61982dd
JB
2818 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2819 CHECK_NUMBER_COERCE_MARKER (end, 1);
2820
9d7608b7
KH
2821 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
2822 return Fdelete_overlay (overlay);
0a4469c9 2823
b61982dd
JB
2824 if (XINT (beg) > XINT (end))
2825 {
c99fc30f
KH
2826 Lisp_Object temp;
2827 temp = beg; beg = end; end = temp;
b61982dd
JB
2828 }
2829
9d7608b7
KH
2830 specbind (Qinhibit_quit, Qt);
2831
0a4469c9 2832 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 2833 b = XBUFFER (buffer);
0a4469c9 2834 ob = XBUFFER (obuffer);
2eec3b4e 2835
c82ed728 2836 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 2837 if (!EQ (buffer, obuffer))
50760c4a
RS
2838 {
2839 /* Redisplay where the overlay was. */
2840 if (!NILP (obuffer))
2841 {
2842 Lisp_Object o_beg;
2843 Lisp_Object o_end;
2844
80509f2f
RS
2845 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2846 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
50760c4a 2847
876aa27c 2848 modify_overlay (ob, XINT (o_beg), XINT (o_end));
50760c4a
RS
2849 }
2850
2851 /* Redisplay where the overlay is going to be. */
876aa27c 2852 modify_overlay (b, XINT (beg), XINT (end));
50760c4a 2853 }
c82ed728
JB
2854 else
2855 /* Redisplay the area the overlay has just left, or just enclosed. */
2856 {
be8b1c6b
RS
2857 Lisp_Object o_beg;
2858 Lisp_Object o_end;
c82ed728
JB
2859 int change_beg, change_end;
2860
80509f2f
RS
2861 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2862 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
c82ed728
JB
2863
2864 if (XINT (o_beg) == XINT (beg))
876aa27c 2865 modify_overlay (b, XINT (o_end), XINT (end));
c82ed728 2866 else if (XINT (o_end) == XINT (end))
876aa27c 2867 modify_overlay (b, XINT (o_beg), XINT (beg));
c82ed728
JB
2868 else
2869 {
2870 if (XINT (beg) < XINT (o_beg)) o_beg = beg;
2871 if (XINT (end) > XINT (o_end)) o_end = end;
876aa27c 2872 modify_overlay (b, XINT (o_beg), XINT (o_end));
c82ed728
JB
2873 }
2874 }
b61982dd 2875
0a4469c9
RS
2876 if (!NILP (obuffer))
2877 {
2878 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
2879 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
2880 }
5c4f68f1
JB
2881
2882 Fset_marker (OVERLAY_START (overlay), beg, buffer);
2883 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e
RS
2884
2885 /* Put the overlay on the wrong list. */
2886 end = OVERLAY_END (overlay);
5c4f68f1
JB
2887 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2888 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 2889 else
5c4f68f1 2890 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
2891
2892 /* This puts it in the right list, and in the right order. */
5c4f68f1 2893 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 2894
0a4469c9 2895 return unbind_to (count, overlay);
2eec3b4e
RS
2896}
2897
2898DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
5c4f68f1 2899 "Delete the overlay OVERLAY from its buffer.")
2eec3b4e 2900 (overlay)
5c4f68f1 2901 Lisp_Object overlay;
2eec3b4e 2902{
0a4469c9 2903 Lisp_Object buffer;
5c4f68f1 2904 struct buffer *b;
0a4469c9 2905 int count = specpdl_ptr - specpdl;
5c4f68f1 2906
52f8ec73
JB
2907 CHECK_OVERLAY (overlay, 0);
2908
0a4469c9
RS
2909 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2910 if (NILP (buffer))
2911 return Qnil;
2912
2913 b = XBUFFER (buffer);
2914
2915 specbind (Qinhibit_quit, Qt);
5c4f68f1
JB
2916
2917 b->overlays_before = Fdelq (overlay, b->overlays_before);
2918 b->overlays_after = Fdelq (overlay, b->overlays_after);
2919
876aa27c 2920 modify_overlay (b,
a927f5c9
RS
2921 marker_position (OVERLAY_START (overlay)),
2922 marker_position (OVERLAY_END (overlay)));
b61982dd 2923
3ece337a
JB
2924 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
2925 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
2926
0a4469c9 2927 return unbind_to (count, Qnil);
2eec3b4e
RS
2928}
2929\f
8ebafa8d
JB
2930/* Overlay dissection functions. */
2931
2932DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
2933 "Return the position at which OVERLAY starts.")
2934 (overlay)
2935 Lisp_Object overlay;
2936{
2937 CHECK_OVERLAY (overlay, 0);
2938
2939 return (Fmarker_position (OVERLAY_START (overlay)));
2940}
2941
2942DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
2943 "Return the position at which OVERLAY ends.")
2944 (overlay)
2945 Lisp_Object overlay;
2946{
2947 CHECK_OVERLAY (overlay, 0);
2948
2949 return (Fmarker_position (OVERLAY_END (overlay)));
2950}
2951
2952DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
2953 "Return the buffer OVERLAY belongs to.")
2954 (overlay)
2955 Lisp_Object overlay;
2956{
2957 CHECK_OVERLAY (overlay, 0);
2958
2959 return Fmarker_buffer (OVERLAY_START (overlay));
2960}
2961
2962DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
2963 "Return a list of the properties on OVERLAY.\n\
2964This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
2965OVERLAY.")
2966 (overlay)
2967 Lisp_Object overlay;
2968{
2969 CHECK_OVERLAY (overlay, 0);
2970
48e2e3ba 2971 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
2972}
2973
2974\f
2eec3b4e 2975DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
eb8c3be9 2976 "Return a list of the overlays that contain position POS.")
2eec3b4e
RS
2977 (pos)
2978 Lisp_Object pos;
2979{
2980 int noverlays;
2eec3b4e
RS
2981 Lisp_Object *overlay_vec;
2982 int len;
2983 Lisp_Object result;
2984
2985 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2986
2987 len = 10;
2988 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2989
2990 /* Put all the overlays we want in a vector in overlay_vec.
2991 Store the length in len. */
2a77a7d7
RS
2992 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
2993 (int *) 0, (int *) 0);
2eec3b4e
RS
2994
2995 /* Make a list of them all. */
2996 result = Flist (noverlays, overlay_vec);
2997
9ac0d9e0 2998 xfree (overlay_vec);
2eec3b4e
RS
2999 return result;
3000}
3001
74514898 3002DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
2a3eeee7
RS
3003 "Return a list of the overlays that overlap the region BEG ... END.\n\
3004Overlap means that at least one character is contained within the overlay\n\
3005and also contained within the specified region.\n\
3006Empty overlays are included in the result if they are located at BEG\n\
3007or between BEG and END.")
74514898
RS
3008 (beg, end)
3009 Lisp_Object beg, end;
3010{
3011 int noverlays;
3012 Lisp_Object *overlay_vec;
3013 int len;
3014 Lisp_Object result;
3015
3016 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3017 CHECK_NUMBER_COERCE_MARKER (end, 0);
3018
3019 len = 10;
3020 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3021
3022 /* Put all the overlays we want in a vector in overlay_vec.
3023 Store the length in len. */
3024 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3025 (int *) 0, (int *) 0);
3026
3027 /* Make a list of them all. */
3028 result = Flist (noverlays, overlay_vec);
3029
3030 xfree (overlay_vec);
3031 return result;
3032}
3033
2eec3b4e
RS
3034DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3035 1, 1, 0,
bbe20e81
KH
3036 "Return the next position after POS where an overlay starts or ends.\n\
3037If there are no more overlay boundaries after POS, return (point-max).")
2eec3b4e
RS
3038 (pos)
3039 Lisp_Object pos;
3040{
3041 int noverlays;
3042 int endpos;
3043 Lisp_Object *overlay_vec;
3044 int len;
2eec3b4e
RS
3045 int i;
3046
3047 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3048
3049 len = 10;
3050 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3051
3052 /* Put all the overlays we want in a vector in overlay_vec.
3053 Store the length in len.
3054 endpos gets the position where the next overlay starts. */
2a77a7d7
RS
3055 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3056 &endpos, (int *) 0);
2eec3b4e
RS
3057
3058 /* If any of these overlays ends before endpos,
3059 use its ending point instead. */
3060 for (i = 0; i < noverlays; i++)
3061 {
3062 Lisp_Object oend;
3063 int oendpos;
3064
3065 oend = OVERLAY_END (overlay_vec[i]);
3066 oendpos = OVERLAY_POSITION (oend);
3067 if (oendpos < endpos)
3068 endpos = oendpos;
1ab256cb
RM
3069 }
3070
9ac0d9e0 3071 xfree (overlay_vec);
2eec3b4e
RS
3072 return make_number (endpos);
3073}
239c932b
RS
3074
3075DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3076 Sprevious_overlay_change, 1, 1, 0,
3077 "Return the previous position before POS where an overlay starts or ends.\n\
624bbdc4 3078If there are no more overlay boundaries before POS, return (point-min).")
239c932b
RS
3079 (pos)
3080 Lisp_Object pos;
3081{
3082 int noverlays;
3083 int prevpos;
3084 Lisp_Object *overlay_vec;
3085 int len;
3086 int i;
624bbdc4 3087 Lisp_Object tail;
239c932b
RS
3088
3089 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3090
3091 len = 10;
3092 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3093
624bbdc4
RS
3094 /* At beginning of buffer, we know the answer;
3095 avoid bug subtracting 1 below. */
3096 if (XINT (pos) == BEGV)
3097 return pos;
3098
239c932b
RS
3099 /* Put all the overlays we want in a vector in overlay_vec.
3100 Store the length in len.
3101 prevpos gets the position of an overlay end. */
2a77a7d7
RS
3102 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3103 (int *) 0, &prevpos);
239c932b 3104
624bbdc4 3105 /* If any of these overlays starts after prevpos,
239c932b
RS
3106 maybe use its starting point instead. */
3107 for (i = 0; i < noverlays; i++)
3108 {
3109 Lisp_Object ostart;
3110 int ostartpos;
3111
3112 ostart = OVERLAY_START (overlay_vec[i]);
3113 ostartpos = OVERLAY_POSITION (ostart);
3114 if (ostartpos > prevpos && ostartpos < XINT (pos))
3115 prevpos = ostartpos;
3116 }
3117
624bbdc4
RS
3118 /* If any overlay ends at pos, consider its starting point too. */
3119 for (tail = current_buffer->overlays_before;
3120 GC_CONSP (tail);
3121 tail = XCONS (tail)->cdr)
3122 {
3123 Lisp_Object overlay, ostart;
3124 int ostartpos;
3125
3126 overlay = XCONS (tail)->car;
3127
3128 ostart = OVERLAY_START (overlay);
3129 ostartpos = OVERLAY_POSITION (ostart);
3130 if (ostartpos > prevpos && ostartpos < XINT (pos))
3131 prevpos = ostartpos;
3132 }
3133
239c932b
RS
3134 xfree (overlay_vec);
3135 return make_number (prevpos);
3136}
2eec3b4e
RS
3137\f
3138/* These functions are for debugging overlays. */
3139
3140DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3141 "Return a pair of lists giving all the overlays of the current buffer.\n\
3142The car has all the overlays before the overlay center;\n\
bbe20e81 3143the cdr has all the overlays after the overlay center.\n\
2eec3b4e
RS
3144Recentering overlays moves overlays between these lists.\n\
3145The lists you get are copies, so that changing them has no effect.\n\
3146However, the overlays you get are the real objects that the buffer uses.")
3147 ()
3148{
3149 Lisp_Object before, after;
3150 before = current_buffer->overlays_before;
3151 if (CONSP (before))
3152 before = Fcopy_sequence (before);
3153 after = current_buffer->overlays_after;
3154 if (CONSP (after))
3155 after = Fcopy_sequence (after);
3156
3157 return Fcons (before, after);
3158}
3159
3160DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3161 "Recenter the overlays of the current buffer around position POS.")
3162 (pos)
3163 Lisp_Object pos;
3164{
3165 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3166
5c4f68f1 3167 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
3168 return Qnil;
3169}
3170\f
3171DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
a2428fa2 3172 "Get the property of overlay OVERLAY with property name PROP.")
2eec3b4e
RS
3173 (overlay, prop)
3174 Lisp_Object overlay, prop;
3175{
cab4777e 3176 Lisp_Object plist, fallback;
52f8ec73
JB
3177
3178 CHECK_OVERLAY (overlay, 0);
3179
cab4777e
RS
3180 fallback = Qnil;
3181
48e2e3ba 3182 for (plist = XOVERLAY (overlay)->plist;
2eec3b4e
RS
3183 CONSP (plist) && CONSP (XCONS (plist)->cdr);
3184 plist = XCONS (XCONS (plist)->cdr)->cdr)
3185 {
3186 if (EQ (XCONS (plist)->car, prop))
3187 return XCONS (XCONS (plist)->cdr)->car;
cab4777e
RS
3188 else if (EQ (XCONS (plist)->car, Qcategory))
3189 {
3190 Lisp_Object tem;
3191 tem = Fcar (Fcdr (plist));
3192 if (SYMBOLP (tem))
3193 fallback = Fget (tem, prop);
3194 }
2eec3b4e 3195 }
52f8ec73 3196
cab4777e 3197 return fallback;
2eec3b4e
RS
3198}
3199
3200DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3201 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3202 (overlay, prop, value)
3203 Lisp_Object overlay, prop, value;
3204{
48e2e3ba 3205 Lisp_Object tail, buffer;
9d7608b7 3206 int changed;
2eec3b4e 3207
52f8ec73 3208 CHECK_OVERLAY (overlay, 0);
b61982dd 3209
274a9425
RS
3210 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3211
48e2e3ba 3212 for (tail = XOVERLAY (overlay)->plist;
2eec3b4e
RS
3213 CONSP (tail) && CONSP (XCONS (tail)->cdr);
3214 tail = XCONS (XCONS (tail)->cdr)->cdr)
274a9425
RS
3215 if (EQ (XCONS (tail)->car, prop))
3216 {
9d7608b7
KH
3217 changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
3218 XCONS (XCONS (tail)->cdr)->car = value;
3219 goto found;
274a9425 3220 }
9d7608b7
KH
3221 /* It wasn't in the list, so add it to the front. */
3222 changed = !NILP (value);
48e2e3ba
KH
3223 XOVERLAY (overlay)->plist
3224 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
9d7608b7
KH
3225 found:
3226 if (! NILP (buffer))
3227 {
3228 if (changed)
876aa27c 3229 modify_overlay (XBUFFER (buffer),
9d7608b7
KH
3230 marker_position (OVERLAY_START (overlay)),
3231 marker_position (OVERLAY_END (overlay)));
3232 if (EQ (prop, Qevaporate) && ! NILP (value)
3233 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3234 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3235 Fdelete_overlay (overlay);
3236 }
2eec3b4e 3237 return value;
1ab256cb
RM
3238}
3239\f
9115729e
KH
3240/* Subroutine of report_overlay_modification. */
3241
3242/* Lisp vector holding overlay hook functions to call.
3243 Vector elements come in pairs.
3244 Each even-index element is a list of hook functions.
3245 The following odd-index element is the overlay they came from.
3246
3247 Before the buffer change, we fill in this vector
3248 as we call overlay hook functions.
3249 After the buffer change, we get the functions to call from this vector.
3250 This way we always call the same functions before and after the change. */
3251static Lisp_Object last_overlay_modification_hooks;
3252
3253/* Number of elements actually used in last_overlay_modification_hooks. */
3254static int last_overlay_modification_hooks_used;
3255
3256/* Add one functionlist/overlay pair
3257 to the end of last_overlay_modification_hooks. */
3258
3259static void
3260add_overlay_mod_hooklist (functionlist, overlay)
3261 Lisp_Object functionlist, overlay;
3262{
3263 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3264
3265 if (last_overlay_modification_hooks_used == oldsize)
3266 {
3267 Lisp_Object old;
3268 old = last_overlay_modification_hooks;
3269 last_overlay_modification_hooks
3270 = Fmake_vector (make_number (oldsize * 2), Qnil);
0b1f1b09
RS
3271 bcopy (XVECTOR (old)->contents,
3272 XVECTOR (last_overlay_modification_hooks)->contents,
9115729e
KH
3273 sizeof (Lisp_Object) * oldsize);
3274 }
3275 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3276 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3277}
3278\f
173f2a64
RS
3279/* Run the modification-hooks of overlays that include
3280 any part of the text in START to END.
9115729e
KH
3281 If this change is an insertion, also
3282 run the insert-before-hooks of overlay starting at END,
930a9140
RS
3283 and the insert-after-hooks of overlay ending at START.
3284
3285 This is called both before and after the modification.
3286 AFTER is nonzero when we call after the modification.
3287
9115729e
KH
3288 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3289 When AFTER is nonzero, they are the start position,
3290 the position after the inserted new text,
3291 and the length of deleted or replaced old text. */
173f2a64
RS
3292
3293void
930a9140 3294report_overlay_modification (start, end, after, arg1, arg2, arg3)
173f2a64 3295 Lisp_Object start, end;
930a9140
RS
3296 int after;
3297 Lisp_Object arg1, arg2, arg3;
173f2a64
RS
3298{
3299 Lisp_Object prop, overlay, tail;
9115729e
KH
3300 /* 1 if this change is an insertion. */
3301 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
55b48893 3302 int tail_copied;
930a9140 3303 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
55b48893
RS
3304
3305 overlay = Qnil;
3306 tail = Qnil;
930a9140 3307 GCPRO5 (overlay, tail, arg1, arg2, arg3);
173f2a64 3308
9115729e
KH
3309 if (after)
3310 {
3311 /* Call the functions recorded in last_overlay_modification_hooks
3312 rather than scanning the overlays again.
3313 First copy the vector contents, in case some of these hooks
3314 do subsequent modification of the buffer. */
3315 int size = last_overlay_modification_hooks_used;
3316 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3317 int i;
3318
3319 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3320 copy, size * sizeof (Lisp_Object));
3321 gcpro1.var = copy;
3322 gcpro1.nvars = size;
3323
3324 for (i = 0; i < size;)
3325 {
3326 Lisp_Object prop, overlay;
3327 prop = copy[i++];
3328 overlay = copy[i++];
3329 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3330 }
3331 UNGCPRO;
3332 return;
3333 }
3334
3335 /* We are being called before a change.
3336 Scan the overlays to find the functions to call. */
3337 last_overlay_modification_hooks_used = 0;
55b48893 3338 tail_copied = 0;
173f2a64
RS
3339 for (tail = current_buffer->overlays_before;
3340 CONSP (tail);
3341 tail = XCONS (tail)->cdr)
3342 {
3343 int startpos, endpos;
be8b1c6b 3344 Lisp_Object ostart, oend;
173f2a64
RS
3345
3346 overlay = XCONS (tail)->car;
3347
3348 ostart = OVERLAY_START (overlay);
3349 oend = OVERLAY_END (overlay);
3350 endpos = OVERLAY_POSITION (oend);
3351 if (XFASTINT (start) > endpos)
3352 break;
3353 startpos = OVERLAY_POSITION (ostart);
9115729e
KH
3354 if (insertion && (XFASTINT (start) == startpos
3355 || XFASTINT (end) == startpos))
173f2a64
RS
3356 {
3357 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
3358 if (!NILP (prop))
3359 {
3360 /* Copy TAIL in case the hook recenters the overlay lists. */
55b48893
RS
3361 if (!tail_copied)
3362 tail = Fcopy_sequence (tail);
3363 tail_copied = 1;
930a9140 3364 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3365 }
173f2a64 3366 }
9115729e
KH
3367 if (insertion && (XFASTINT (start) == endpos
3368 || XFASTINT (end) == endpos))
173f2a64
RS
3369 {
3370 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
3371 if (!NILP (prop))
3372 {
55b48893
RS
3373 if (!tail_copied)
3374 tail = Fcopy_sequence (tail);
3375 tail_copied = 1;
930a9140 3376 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3377 }
173f2a64 3378 }
3bd13e92
KH
3379 /* Test for intersecting intervals. This does the right thing
3380 for both insertion and deletion. */
3381 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
3382 {
3383 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
3384 if (!NILP (prop))
3385 {
55b48893
RS
3386 if (!tail_copied)
3387 tail = Fcopy_sequence (tail);
3388 tail_copied = 1;
930a9140 3389 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3390 }
173f2a64
RS
3391 }
3392 }
3393
55b48893 3394 tail_copied = 0;
173f2a64
RS
3395 for (tail = current_buffer->overlays_after;
3396 CONSP (tail);
3397 tail = XCONS (tail)->cdr)
3398 {
3399 int startpos, endpos;
be8b1c6b 3400 Lisp_Object ostart, oend;
173f2a64
RS
3401
3402 overlay = XCONS (tail)->car;
3403
3404 ostart = OVERLAY_START (overlay);
3405 oend = OVERLAY_END (overlay);
3406 startpos = OVERLAY_POSITION (ostart);
cdf0b096 3407 endpos = OVERLAY_POSITION (oend);
173f2a64
RS
3408 if (XFASTINT (end) < startpos)
3409 break;
9115729e
KH
3410 if (insertion && (XFASTINT (start) == startpos
3411 || XFASTINT (end) == startpos))
173f2a64
RS
3412 {
3413 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
3414 if (!NILP (prop))
3415 {
55b48893
RS
3416 if (!tail_copied)
3417 tail = Fcopy_sequence (tail);
3418 tail_copied = 1;
930a9140 3419 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3420 }
173f2a64 3421 }
9115729e
KH
3422 if (insertion && (XFASTINT (start) == endpos
3423 || XFASTINT (end) == endpos))
173f2a64
RS
3424 {
3425 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
3426 if (!NILP (prop))
3427 {
55b48893
RS
3428 if (!tail_copied)
3429 tail = Fcopy_sequence (tail);
3430 tail_copied = 1;
930a9140 3431 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3432 }
173f2a64 3433 }
3bd13e92
KH
3434 /* Test for intersecting intervals. This does the right thing
3435 for both insertion and deletion. */
3436 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
3437 {
3438 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
3439 if (!NILP (prop))
3440 {
55b48893
RS
3441 if (!tail_copied)
3442 tail = Fcopy_sequence (tail);
3443 tail_copied = 1;
930a9140 3444 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 3445 }
173f2a64
RS
3446 }
3447 }
55b48893
RS
3448
3449 UNGCPRO;
173f2a64
RS
3450}
3451
3452static void
930a9140
RS
3453call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3454 Lisp_Object list, overlay;
3455 int after;
3456 Lisp_Object arg1, arg2, arg3;
173f2a64 3457{
930a9140 3458 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9115729e 3459
930a9140 3460 GCPRO4 (list, arg1, arg2, arg3);
9115729e
KH
3461 if (! after)
3462 add_overlay_mod_hooklist (list, overlay);
3463
173f2a64
RS
3464 while (!NILP (list))
3465 {
930a9140
RS
3466 if (NILP (arg3))
3467 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3468 else
3469 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
173f2a64
RS
3470 list = Fcdr (list);
3471 }
3472 UNGCPRO;
3473}
9d7608b7
KH
3474
3475/* Delete any zero-sized overlays at position POS, if the `evaporate'
3476 property is set. */
3477void
3478evaporate_overlays (pos)
3479 int pos;
3480{
3481 Lisp_Object tail, overlay, hit_list;
3482
3483 hit_list = Qnil;
3484 if (pos <= XFASTINT (current_buffer->overlay_center))
3485 for (tail = current_buffer->overlays_before; CONSP (tail);
3486 tail = XCONS (tail)->cdr)
3487 {
3488 int endpos;
3489 overlay = XCONS (tail)->car;
3490 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3491 if (endpos < pos)
3492 break;
3493 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
c3935f9d 3494 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
3495 hit_list = Fcons (overlay, hit_list);
3496 }
3497 else
3498 for (tail = current_buffer->overlays_after; CONSP (tail);
3499 tail = XCONS (tail)->cdr)
3500 {
3501 int startpos;
889bf329 3502 overlay = XCONS (tail)->car;
9d7608b7
KH
3503 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3504 if (startpos > pos)
3505 break;
3506 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
c3935f9d 3507 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
3508 hit_list = Fcons (overlay, hit_list);
3509 }
3510 for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
3511 Fdelete_overlay (XCONS (hit_list)->car);
3512}
173f2a64 3513\f
54dfdeb0
KH
3514/* Somebody has tried to store a value with an unacceptable type
3515 into the buffer-local slot with offset OFFSET. */
0fa3ba92 3516void
54dfdeb0
KH
3517buffer_slot_type_mismatch (offset)
3518 int offset;
0fa3ba92 3519{
54dfdeb0 3520 Lisp_Object sym;
0fa3ba92 3521 char *type_name;
54dfdeb0 3522 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
0fa3ba92
JB
3523 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
3524 {
3525 case Lisp_Int: type_name = "integers"; break;
3526 case Lisp_String: type_name = "strings"; break;
0fa3ba92 3527 case Lisp_Symbol: type_name = "symbols"; break;
0fa3ba92
JB
3528 default:
3529 abort ();
3530 }
3531
3532 error ("only %s should be stored in the buffer-local variable %s",
54dfdeb0 3533 type_name, XSYMBOL (sym)->name->data);
0fa3ba92
JB
3534}
3535\f
1ab256cb
RM
3536init_buffer_once ()
3537{
3538 register Lisp_Object tem;
3539
13de9290
RS
3540 buffer_permanent_local_flags = 0;
3541
1ab256cb
RM
3542 /* Make sure all markable slots in buffer_defaults
3543 are initialized reasonably, so mark_buffer won't choke. */
3544 reset_buffer (&buffer_defaults);
13de9290 3545 reset_buffer_local_variables (&buffer_defaults, 1);
1ab256cb 3546 reset_buffer (&buffer_local_symbols);
13de9290 3547 reset_buffer_local_variables (&buffer_local_symbols, 1);
336cd056
RS
3548 /* Prevent GC from getting confused. */
3549 buffer_defaults.text = &buffer_defaults.own_text;
3550 buffer_local_symbols.text = &buffer_local_symbols.own_text;
3551#ifdef USE_TEXT_PROPERTIES
3552 BUF_INTERVALS (&buffer_defaults) = 0;
3553 BUF_INTERVALS (&buffer_local_symbols) = 0;
3554#endif
67180c6a
KH
3555 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3556 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
3557
3558 /* Set up the default values of various buffer slots. */
3559 /* Must do these before making the first buffer! */
3560
3561 /* real setup is done in loaddefs.el */
3562 buffer_defaults.mode_line_format = build_string ("%-");
3563 buffer_defaults.abbrev_mode = Qnil;
3564 buffer_defaults.overwrite_mode = Qnil;
3565 buffer_defaults.case_fold_search = Qt;
3566 buffer_defaults.auto_fill_function = Qnil;
3567 buffer_defaults.selective_display = Qnil;
3568#ifndef old
3569 buffer_defaults.selective_display_ellipses = Qt;
3570#endif
3571 buffer_defaults.abbrev_table = Qnil;
3572 buffer_defaults.display_table = Qnil;
1ab256cb 3573 buffer_defaults.undo_list = Qnil;
c48f61ef 3574 buffer_defaults.mark_active = Qnil;
be9aafdd 3575 buffer_defaults.file_format = Qnil;
2eec3b4e
RS
3576 buffer_defaults.overlays_before = Qnil;
3577 buffer_defaults.overlays_after = Qnil;
bbbe9545 3578 XSETFASTINT (buffer_defaults.overlay_center, BEG);
1ab256cb 3579
8d7a4592 3580 XSETFASTINT (buffer_defaults.tab_width, 8);
1ab256cb
RM
3581 buffer_defaults.truncate_lines = Qnil;
3582 buffer_defaults.ctl_arrow = Qt;
3b06f880 3583 buffer_defaults.direction_reversed = Qnil;
1ab256cb 3584
f7975d07 3585#ifdef DOS_NT
0776cb1b 3586 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
54ad07d3 3587#endif
a1a17b61 3588 buffer_defaults.enable_multibyte_characters = Qt;
c71b5d9b 3589 buffer_defaults.buffer_file_coding_system = Qnil;
8d7a4592
KH
3590 XSETFASTINT (buffer_defaults.fill_column, 70);
3591 XSETFASTINT (buffer_defaults.left_margin, 0);
28e969dd 3592 buffer_defaults.cache_long_line_scans = Qnil;
f6ed2e84 3593 buffer_defaults.file_truename = Qnil;
7962a441 3594 XSETFASTINT (buffer_defaults.display_count, 0);
1ab256cb
RM
3595
3596 /* Assign the local-flags to the slots that have default values.
3597 The local flag is a bit that is used in the buffer
3598 to say that it has its own local value for the slot.
3599 The local flag bits are in the local_var_flags slot of the buffer. */
3600
3601 /* Nothing can work if this isn't true */
4d2f1389 3602 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
1ab256cb
RM
3603
3604 /* 0 means not a lisp var, -1 means always local, else mask */
3605 bzero (&buffer_local_flags, sizeof buffer_local_flags);
aab80822
KH
3606 XSETINT (buffer_local_flags.filename, -1);
3607 XSETINT (buffer_local_flags.directory, -1);
3608 XSETINT (buffer_local_flags.backed_up, -1);
3609 XSETINT (buffer_local_flags.save_length, -1);
3610 XSETINT (buffer_local_flags.auto_save_file_name, -1);
3611 XSETINT (buffer_local_flags.read_only, -1);
3612 XSETINT (buffer_local_flags.major_mode, -1);
3613 XSETINT (buffer_local_flags.mode_name, -1);
3614 XSETINT (buffer_local_flags.undo_list, -1);
3615 XSETINT (buffer_local_flags.mark_active, -1);
943e065b 3616 XSETINT (buffer_local_flags.point_before_scroll, -1);
f6ed2e84 3617 XSETINT (buffer_local_flags.file_truename, -1);
3cb719bd 3618 XSETINT (buffer_local_flags.invisibility_spec, -1);
55ac8536 3619 XSETINT (buffer_local_flags.file_format, -1);
7962a441 3620 XSETINT (buffer_local_flags.display_count, -1);
8d7a4592
KH
3621
3622 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
3623 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
3624 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
3625 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
3626 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
3627 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
1ab256cb 3628#ifndef old
8d7a4592 3629 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
1ab256cb 3630#endif
8d7a4592
KH
3631 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
3632 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
3633 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
3634 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
3635 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
3636 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
3637 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
f7975d07 3638#ifdef DOS_NT
8d7a4592 3639 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
13de9290
RS
3640 /* Make this one a permanent local. */
3641 buffer_permanent_local_flags |= 0x4000;
54ad07d3 3642#endif
2e716096
RS
3643 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
3644 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
3b06f880
KH
3645 XSETFASTINT (buffer_local_flags.category_table, 0x20000);
3646 XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
a1a17b61
KH
3647 XSETFASTINT (buffer_local_flags.enable_multibyte_characters, 0x80000);
3648 /* Make this one a permanent local. */
3649 buffer_permanent_local_flags |= 0x80000;
c71b5d9b
KH
3650 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x100000);
3651 /* Make this one a permanent local. */
3652 buffer_permanent_local_flags |= 0x100000;
3653
1ab256cb
RM
3654 Vbuffer_alist = Qnil;
3655 current_buffer = 0;
3656 all_buffers = 0;
3657
3658 QSFundamental = build_string ("Fundamental");
3659
3660 Qfundamental_mode = intern ("fundamental-mode");
3661 buffer_defaults.major_mode = Qfundamental_mode;
3662
3663 Qmode_class = intern ("mode-class");
3664
3665 Qprotected_field = intern ("protected-field");
3666
3667 Qpermanent_local = intern ("permanent-local");
3668
3669 Qkill_buffer_hook = intern ("kill-buffer-hook");
3670
3671 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
000f8083 3672
1ab256cb
RM
3673 /* super-magic invisible buffer */
3674 Vbuffer_alist = Qnil;
3675
ffd56f97 3676 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1ab256cb
RM
3677}
3678
3679init_buffer ()
3680{
3681 char buf[MAXPATHLEN+1];
2381d133
JB
3682 char *pwd;
3683 struct stat dotstat, pwdstat;
136351b7 3684 Lisp_Object temp;
f7975d07 3685 int rc;
1ab256cb
RM
3686
3687 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
2381d133
JB
3688
3689 /* If PWD is accurate, use it instead of calling getwd. This is faster
3690 when PWD is right, and may avoid a fatal error. */
f7975d07 3691 if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
2381d133
JB
3692 && stat (pwd, &pwdstat) == 0
3693 && stat (".", &dotstat) == 0
3694 && dotstat.st_ino == pwdstat.st_ino
3695 && dotstat.st_dev == pwdstat.st_dev
3696 && strlen (pwd) < MAXPATHLEN)
3697 strcpy (buf, pwd);
3698 else if (getwd (buf) == 0)
cf1e6391 3699 fatal ("`getwd' failed: %s\n", buf);
1ab256cb
RM
3700
3701#ifndef VMS
3702 /* Maybe this should really use some standard subroutine
3703 whose definition is filename syntax dependent. */
f7975d07
RS
3704 rc = strlen (buf);
3705 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
3706 {
3707 buf[rc] = DIRECTORY_SEP;
3708 buf[rc + 1] = '\0';
3709 }
1ab256cb 3710#endif /* not VMS */
0995fa35 3711
1ab256cb 3712 current_buffer->directory = build_string (buf);
136351b7 3713
0995fa35
RS
3714 /* Add /: to the front of the name
3715 if it would otherwise be treated as magic. */
3716 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
3717 if (! NILP (temp))
3718 current_buffer->directory
3719 = concat2 (build_string ("/:"), current_buffer->directory);
3720
136351b7
RS
3721 temp = get_minibuffer (0);
3722 XBUFFER (temp)->directory = current_buffer->directory;
1ab256cb
RM
3723}
3724
3725/* initialize the buffer routines */
3726syms_of_buffer ()
3727{
188d4d11
RM
3728 extern Lisp_Object Qdisabled;
3729
9115729e
KH
3730 staticpro (&last_overlay_modification_hooks);
3731 last_overlay_modification_hooks
3732 = Fmake_vector (make_number (10), Qnil);
3733
1ab256cb
RM
3734 staticpro (&Vbuffer_defaults);
3735 staticpro (&Vbuffer_local_symbols);
3736 staticpro (&Qfundamental_mode);
3737 staticpro (&Qmode_class);
3738 staticpro (&QSFundamental);
3739 staticpro (&Vbuffer_alist);
3740 staticpro (&Qprotected_field);
3741 staticpro (&Qpermanent_local);
3742 staticpro (&Qkill_buffer_hook);
22378665 3743 Qoverlayp = intern ("overlayp");
52f8ec73 3744 staticpro (&Qoverlayp);
9d7608b7
KH
3745 Qevaporate = intern ("evaporate");
3746 staticpro (&Qevaporate);
294d215f 3747 Qmodification_hooks = intern ("modification-hooks");
22378665 3748 staticpro (&Qmodification_hooks);
294d215f 3749 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
22378665 3750 staticpro (&Qinsert_in_front_hooks);
294d215f 3751 Qinsert_behind_hooks = intern ("insert-behind-hooks");
22378665 3752 staticpro (&Qinsert_behind_hooks);
5fe0b67e 3753 Qget_file_buffer = intern ("get-file-buffer");
22378665 3754 staticpro (&Qget_file_buffer);
5985d248
KH
3755 Qpriority = intern ("priority");
3756 staticpro (&Qpriority);
3757 Qwindow = intern ("window");
3758 staticpro (&Qwindow);
bbbe9545
KH
3759 Qbefore_string = intern ("before-string");
3760 staticpro (&Qbefore_string);
3761 Qafter_string = intern ("after-string");
3762 staticpro (&Qafter_string);
22378665
RS
3763 Qfirst_change_hook = intern ("first-change-hook");
3764 staticpro (&Qfirst_change_hook);
3765 Qbefore_change_functions = intern ("before-change-functions");
3766 staticpro (&Qbefore_change_functions);
3767 Qafter_change_functions = intern ("after-change-functions");
3768 staticpro (&Qafter_change_functions);
1ab256cb
RM
3769
3770 Fput (Qprotected_field, Qerror_conditions,
3771 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
3772 Fput (Qprotected_field, Qerror_message,
3773 build_string ("Attempt to modify a protected field"));
3774
3775 /* All these use DEFVAR_LISP_NOPRO because the slots in
3776 buffer_defaults will all be marked via Vbuffer_defaults. */
3777
3778 DEFVAR_LISP_NOPRO ("default-mode-line-format",
3779 &buffer_defaults.mode_line_format,
3780 "Default value of `mode-line-format' for buffers that don't override it.\n\
3781This is the same as (default-value 'mode-line-format).");
3782
3783 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
3784 &buffer_defaults.abbrev_mode,
3785 "Default value of `abbrev-mode' for buffers that do not override it.\n\
3786This is the same as (default-value 'abbrev-mode).");
3787
3788 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
3789 &buffer_defaults.ctl_arrow,
3790 "Default value of `ctl-arrow' for buffers that do not override it.\n\
3791This is the same as (default-value 'ctl-arrow).");
3792
3b06f880
KH
3793 DEFVAR_LISP_NOPRO ("default-direction-reversed",
3794 &buffer_defaults.direction_reversed,
3795 "Default value of `direction_reversed' for buffers that do not override it.\n\
3796 This is the same as (default-value 'direction-reversed).");
3797
a1a17b61
KH
3798 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
3799 &buffer_defaults.enable_multibyte_characters,
3800 "Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
3801 This is the same as (default-value 'enable-multibyte-characters).");
3802
c71b5d9b
KH
3803 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
3804 &buffer_defaults.buffer_file_coding_system,
3805 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
3806 This is the same as (default-value 'buffer-file-coding-system).");
3807
1ab256cb
RM
3808 DEFVAR_LISP_NOPRO ("default-truncate-lines",
3809 &buffer_defaults.truncate_lines,
3810 "Default value of `truncate-lines' for buffers that do not override it.\n\
3811This is the same as (default-value 'truncate-lines).");
3812
3813 DEFVAR_LISP_NOPRO ("default-fill-column",
3814 &buffer_defaults.fill_column,
3815 "Default value of `fill-column' for buffers that do not override it.\n\
3816This is the same as (default-value 'fill-column).");
3817
3818 DEFVAR_LISP_NOPRO ("default-left-margin",
3819 &buffer_defaults.left_margin,
3820 "Default value of `left-margin' for buffers that do not override it.\n\
3821This is the same as (default-value 'left-margin).");
3822
3823 DEFVAR_LISP_NOPRO ("default-tab-width",
3824 &buffer_defaults.tab_width,
3825 "Default value of `tab-width' for buffers that do not override it.\n\
3826This is the same as (default-value 'tab-width).");
3827
3828 DEFVAR_LISP_NOPRO ("default-case-fold-search",
3829 &buffer_defaults.case_fold_search,
3830 "Default value of `case-fold-search' for buffers that don't override it.\n\
3831This is the same as (default-value 'case-fold-search).");
3832
f7975d07 3833#ifdef DOS_NT
54ad07d3
RS
3834 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
3835 &buffer_defaults.buffer_file_type,
3836 "Default file type for buffers that do not override it.\n\
3837This is the same as (default-value 'buffer-file-type).\n\
3838The file type is nil for text, t for binary.");
3839#endif
3840
0fa3ba92
JB
3841 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
3842 Qnil, 0);
1ab256cb
RM
3843
3844/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
3845 But make-docfile finds it!
3846 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
bec44fd6 3847 Qnil,
1ab256cb
RM
3848 "Template for displaying mode line for current buffer.\n\
3849Each buffer has its own value of this variable.\n\
3850Value may be a string, a symbol or a list or cons cell.\n\
3851For a symbol, its value is used (but it is ignored if t or nil).\n\
3852 A string appearing directly as the value of a symbol is processed verbatim\n\
3853 in that the %-constructs below are not recognized.\n\
3854For a list whose car is a symbol, the symbol's value is taken,\n\
3855 and if that is non-nil, the cadr of the list is processed recursively.\n\
3856 Otherwise, the caddr of the list (if there is one) is processed.\n\
3857For a list whose car is a string or list, each element is processed\n\
3858 recursively and the results are effectively concatenated.\n\
3859For a list whose car is an integer, the cdr of the list is processed\n\
3860 and padded (if the number is positive) or truncated (if negative)\n\
3861 to the width specified by that number.\n\
3862A string is printed verbatim in the mode line except for %-constructs:\n\
3863 (%-constructs are allowed when the string is the entire mode-line-format\n\
3864 or when it is found in a cons-cell or a list)\n\
3865 %b -- print buffer name. %f -- print visited file name.\n\
c2ff34f7
RS
3866 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
3867 % means buffer is read-only and * means it is modified.\n\
3868 For a modified read-only buffer, %* gives % and %+ gives *.\n\
a97c374a 3869 %s -- print process status. %l -- print the current line number.\n\
07924294 3870 %c -- print the current column number (this makes editing slower).\n\
dd24e6a6 3871 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
9d130ffc 3872 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
dd24e6a6 3873 or print Bottom or All.\n\
1ab256cb 3874 %n -- print Narrow if appropriate.\n\
b77087c5 3875 %t -- print T if file is text, B if binary.\n\
1ab256cb
RM
3876 %[ -- print one [ for each recursive editing level. %] similar.\n\
3877 %% -- print %. %- -- print infinitely many dashes.\n\
3878Decimal digits after the % specify field width to which to pad.");
3879*/
3880
3881 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
3882 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
3883nil here means use current buffer's major mode.");
3884
3885 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
0fa3ba92 3886 make_number (Lisp_Symbol),
1ab256cb
RM
3887 "Symbol for current buffer's major mode.");
3888
3889 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
0fa3ba92 3890 make_number (Lisp_String),
1ab256cb
RM
3891 "Pretty name of current buffer's major mode (a string).");
3892
0fa3ba92 3893 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
1ab256cb
RM
3894 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
3895Automatically becomes buffer-local when set in any fashion.");
3896
3897 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
0fa3ba92 3898 Qnil,
1ab256cb
RM
3899 "*Non-nil if searches should ignore case.\n\
3900Automatically becomes buffer-local when set in any fashion.");
3901
3902 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
0fa3ba92 3903 make_number (Lisp_Int),
1ab256cb
RM
3904 "*Column beyond which automatic line-wrapping should happen.\n\
3905Automatically becomes buffer-local when set in any fashion.");
3906
3907 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
0fa3ba92 3908 make_number (Lisp_Int),
1ab256cb
RM
3909 "*Column for the default indent-line-function to indent to.\n\
3910Linefeed indents to this column in Fundamental mode.\n\
3911Automatically becomes buffer-local when set in any fashion.");
3912
3913 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
0fa3ba92 3914 make_number (Lisp_Int),
1ab256cb
RM
3915 "*Distance between tab stops (for display of tab characters), in columns.\n\
3916Automatically becomes buffer-local when set in any fashion.");
3917
0fa3ba92 3918 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
1ab256cb 3919 "*Non-nil means display control chars with uparrow.\n\
6a99d31d 3920A value of nil means use backslash and octal digits.\n\
1ab256cb
RM
3921Automatically becomes buffer-local when set in any fashion.\n\
3922This variable does not apply to characters whose display is specified\n\
3923in the current display table (if there is one).");
3924
3b06f880
KH
3925 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
3926 &current_buffer->enable_multibyte_characters, Qnil,
c807f767 3927 "*Non-nil means the buffer contents are regarded as multi-byte form\n\
3b06f880
KH
3928of characters, not a binary code. This affects the display, file I/O,\n\
3929and behaviors of various editing commands.");
3930
c71b5d9b
KH
3931 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
3932 &current_buffer->buffer_file_coding_system, Qnil,
3933 "Coding system to be used for encoding the buffer contents on saving.\n\
3934If it is nil, the buffer is saved without any code conversion unless\n\
3935some coding system is specified in file-coding-system-alist\n\
3936for the buffer file.\n\
3937\n\
3938This variable is never applied to a way of decoding\n\
3939a file while reading it.");
3940
3b06f880
KH
3941 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
3942 Qnil,
3943 "*Non-nil means lines in the buffer are displayed right to left.");
3944
0fa3ba92 3945 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
1ab256cb
RM
3946 "*Non-nil means do not display continuation lines;\n\
3947give each line of text one screen line.\n\
3948Automatically becomes buffer-local when set in any fashion.\n\
3949\n\
3950Note that this is overridden by the variable\n\
3951`truncate-partial-width-windows' if that variable is non-nil\n\
502b9b64 3952and this buffer is not full-frame width.");
1ab256cb 3953
f7975d07 3954#ifdef DOS_NT
54ad07d3
RS
3955 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
3956 Qnil,
006d3d34
RS
3957 "Non-nil if the visited file is a binary file.\n\
3958This variable is meaningful on MS-DOG and Windows NT.\n\
3959On those systems, it is automatically local in every buffer.\n\
e0585c64 3960On other systems, this variable is normally always nil.");
54ad07d3
RS
3961#endif
3962
1ab256cb 3963 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
0fa3ba92 3964 make_number (Lisp_String),
1ab256cb
RM
3965 "Name of default directory of current buffer. Should end with slash.\n\
3966Each buffer has its own value of this variable.");
3967
3968 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
0fa3ba92 3969 Qnil,
1ab256cb 3970 "Function called (if non-nil) to perform auto-fill.\n\
54158e68 3971It is called after self-inserting a space or newline.\n\
1ab256cb 3972Each buffer has its own value of this variable.\n\
54158e68
KH
3973NOTE: This variable is not a hook;\n\
3974its value may not be a list of functions.");
1ab256cb
RM
3975
3976 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
0fa3ba92 3977 make_number (Lisp_String),
1ab256cb
RM
3978 "Name of file visited in current buffer, or nil if not visiting a file.\n\
3979Each buffer has its own value of this variable.");
3980
f6ed2e84
RS
3981 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
3982 make_number (Lisp_String),
bb4c204e 3983 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
b1c03e64
RS
3984The truename of a file is calculated by `file-truename'\n\
3985and then abbreviated with `abbreviate-file-name'.\n\
f6ed2e84
RS
3986Each buffer has its own value of this variable.");
3987
1ab256cb 3988 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 3989 &current_buffer->auto_save_file_name,
0fa3ba92 3990 make_number (Lisp_String),
1ab256cb
RM
3991 "Name of file for auto-saving current buffer,\n\
3992or nil if buffer should not be auto-saved.\n\
3993Each buffer has its own value of this variable.");
3994
0fa3ba92 3995 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
1ab256cb
RM
3996 "Non-nil if this buffer is read-only.\n\
3997Each buffer has its own value of this variable.");
3998
0fa3ba92 3999 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
1ab256cb
RM
4000 "Non-nil if this buffer's file has been backed up.\n\
4001Backing up is done before the first time the file is saved.\n\
4002Each buffer has its own value of this variable.");
4003
4004 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
0fa3ba92 4005 make_number (Lisp_Int),
1ab256cb
RM
4006 "Length of current buffer when last read in, saved or auto-saved.\n\
40070 initially.\n\
4008Each buffer has its own value of this variable.");
4009
4010 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
0fa3ba92 4011 Qnil,
1ab256cb
RM
4012 "Non-nil enables selective display:\n\
4013Integer N as value means display only lines\n\
4014 that start with less than n columns of space.\n\
4015A value of t means, after a ^M, all the rest of the line is invisible.\n\
4016 Then ^M's in the file are written into files as newlines.\n\n\
4017Automatically becomes buffer-local when set in any fashion.");
4018
4019#ifndef old
4020 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4021 &current_buffer->selective_display_ellipses,
0fa3ba92 4022 Qnil,
1ab256cb
RM
4023 "t means display ... on previous line when a line is invisible.\n\
4024Automatically becomes buffer-local when set in any fashion.");
4025#endif
4026
0fa3ba92 4027 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
1ab256cb 4028 "Non-nil if self-insertion should replace existing text.\n\
5e05d0a5
RS
4029The value should be one of `overwrite-mode-textual',\n\
4030`overwrite-mode-binary', or nil.\n\
4031If it is `overwrite-mode-textual', self-insertion still\n\
6bbb0d4a 4032inserts at the end of a line, and inserts when point is before a tab,\n\
2e94b813 4033until the tab is filled in.\n\
6bbb0d4a 4034If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
1ab256cb
RM
4035Automatically becomes buffer-local when set in any fashion.");
4036
54939090
RS
4037#if 0 /* The doc string is too long for some compilers,
4038 but make-docfile can find it in this comment. */
1ab256cb 4039 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5d305367 4040 Qnil,
1ab256cb
RM
4041 "Display table that controls display of the contents of current buffer.\n\
4042Automatically becomes buffer-local when set in any fashion.\n\
6fdc249f
EN
4043The display table is a char-table created with `make-display-table'.\n\
4044The ordinary char-table elements control how to display each possible text\n\
4045character. Each value should be a vector of characters or nil;\n\
1ab256cb 4046nil means display the character in the default fashion.\n\
6fdc249f
EN
4047There are six extra slots to control the display of\n\
4048 the end of a truncated screen line (extra-slot 0, a single character);\n\
4049 the end of a continued line (extra-slot 1, a single character);\n\
6158b3b0 4050 the escape character used to display character codes in octal\n\
6fdc249f
EN
4051 (extra-slot 2, a single character);\n\
4052 the character used as an arrow for control characters (extra-slot 3,\n\
6158b3b0 4053 a single character);\n\
6fdc249f 4054 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
a45e35e1
JB
4055 a vector of characters);\n\
4056 the character used to draw the border between side-by-side windows\n\
6fdc249f
EN
4057 (extra-slot 5, a single character).\n\
4058See also the functions `display-table-slot' and `set-display-table-slot'.\n\
1ab256cb
RM
4059If this variable is nil, the value of `standard-display-table' is used.\n\
4060Each window can have its own, overriding display table.");
54939090
RS
4061#endif
4062 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
de15914a 4063 Qnil, 0);
1ab256cb 4064
1ab256cb
RM
4065/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4066 "Don't ask.");
4067*/
01050cb5 4068 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
f0724bcb 4069 "If non-nil, a function to call before each text change (obsolete).\n\
1ab256cb
RM
4070Two arguments are passed to the function: the positions of\n\
4071the beginning and end of the range of old text to be changed.\n\
4072\(For an insertion, the beginning and end are at the same place.)\n\
4073No information is given about the length of the text after the change.\n\
1ab256cb 4074\n\
5f079267 4075Buffer changes made while executing the `before-change-function'\n\
b86344d0
RS
4076don't call any before-change or after-change functions.\n\
4077That's because these variables are temporarily set to nil.\n\
4078As a result, a hook function cannot straightforwardly alter the value of\n\
4079these variables. See the Emacs Lisp manual for a way of\n\
f0724bcb
RS
4080accomplishing an equivalent result by using other variables.\n\n\
4081This variable is obsolete; use `before-change-functions' instead.");
1ab256cb
RM
4082 Vbefore_change_function = Qnil;
4083
4084 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
f0724bcb 4085 "If non-nil, a Function to call after each text change (obsolete).\n\
1ab256cb
RM
4086Three arguments are passed to the function: the positions of\n\
4087the beginning and end of the range of changed text,\n\
4088and the length of the pre-change text replaced by that range.\n\
4089\(For an insertion, the pre-change length is zero;\n\
4090for a deletion, that length is the number of characters deleted,\n\
4091and the post-change beginning and end are at the same place.)\n\
4092\n\
5f079267 4093Buffer changes made while executing the `after-change-function'\n\
b86344d0
RS
4094don't call any before-change or after-change functions.\n\
4095That's because these variables are temporarily set to nil.\n\
4096As a result, a hook function cannot straightforwardly alter the value of\n\
4097these variables. See the Emacs Lisp manual for a way of\n\
f0724bcb
RS
4098accomplishing an equivalent result by using other variables.\n\n\
4099This variable is obsolete; use `after-change-functions' instead.");
1ab256cb
RM
4100 Vafter_change_function = Qnil;
4101
5f079267
RS
4102 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
4103 "List of functions to call before each text change.\n\
4104Two arguments are passed to each function: the positions of\n\
4105the beginning and end of the range of old text to be changed.\n\
4106\(For an insertion, the beginning and end are at the same place.)\n\
4107No information is given about the length of the text after the change.\n\
5f079267
RS
4108\n\
4109Buffer changes made while executing the `before-change-functions'\n\
b86344d0
RS
4110don't call any before-change or after-change functions.\n\
4111That's because these variables are temporarily set to nil.\n\
4112As a result, a hook function cannot straightforwardly alter the value of\n\
4113these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 4114accomplishing an equivalent result by using other variables.");
5f079267
RS
4115 Vbefore_change_functions = Qnil;
4116
4117 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
4118 "List of function to call after each text change.\n\
4119Three arguments are passed to each function: the positions of\n\
4120the beginning and end of the range of changed text,\n\
4121and the length of the pre-change text replaced by that range.\n\
4122\(For an insertion, the pre-change length is zero;\n\
4123for a deletion, that length is the number of characters deleted,\n\
4124and the post-change beginning and end are at the same place.)\n\
4125\n\
4126Buffer changes made while executing the `after-change-functions'\n\
b86344d0
RS
4127don't call any before-change or after-change functions.\n\
4128That's because these variables are temporarily set to nil.\n\
4129As a result, a hook function cannot straightforwardly alter the value of\n\
4130these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 4131accomplishing an equivalent result by using other variables.");
b86344d0 4132
5f079267
RS
4133 Vafter_change_functions = Qnil;
4134
dbc4e1c1
JB
4135 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
4136 "A list of functions to call before changing a buffer which is unmodified.\n\
4137The functions are run using the `run-hooks' function.");
4138 Vfirst_change_hook = Qnil;
1ab256cb 4139
54939090
RS
4140#if 0 /* The doc string is too long for some compilers,
4141 but make-docfile can find it in this comment. */
3f5fcd47 4142 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
1ab256cb
RM
4143 "List of undo entries in current buffer.\n\
4144Recent changes come first; older changes follow newer.\n\
4145\n\
630f4018
KH
4146An entry (BEG . END) represents an insertion which begins at\n\
4147position BEG and ends at position END.\n\
1ab256cb
RM
4148\n\
4149An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4150from (abs POSITION). If POSITION is positive, point was at the front\n\
4151of the text being deleted; if negative, point was at the end.\n\
4152\n\
6c0df54a
RS
4153An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4154\"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4155of the visited file's modification time, as of that time. If the\n\
4156modification time of the most recent save is different, this entry is\n\
1ab256cb
RM
4157obsolete.\n\
4158\n\
6c0df54a
RS
4159An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4160was modified between BEG and END. PROPERTY is the property name,\n\
4161and VALUE is the old value.\n\
483c1fd3 4162\n\
da1c183c
RS
4163An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4164was adjusted in position by the offset DISTANCE (an integer).\n\
4165\n\
bec44fd6
JB
4166An entry of the form POSITION indicates that point was at the buffer\n\
4167location given by the integer. Undoing an entry of this form places\n\
4168point at POSITION.\n\
4169\n\
1ab256cb
RM
4170nil marks undo boundaries. The undo command treats the changes\n\
4171between two undo boundaries as a single step to be undone.\n\
4172\n\
bec44fd6 4173If the value of the variable is t, undo information is not recorded.");
54939090
RS
4174#endif
4175 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
de15914a 4176 0);
1ab256cb 4177
c48f61ef
RS
4178 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
4179 "Non-nil means the mark and region are currently active in this buffer.\n\
4180Automatically local in all buffers.");
4181
28e969dd 4182 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
f0c5b712
JB
4183 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4184This variable is buffer-local, in all buffers.\n\
28e969dd 4185\n\
f0c5b712
JB
4186Normally, the line-motion functions work by scanning the buffer for\n\
4187newlines. Columnar operations (like move-to-column and\n\
4188compute-motion) also work by scanning the buffer, summing character\n\
4189widths as they go. This works well for ordinary text, but if the\n\
28e969dd 4190buffer's lines are very long (say, more than 500 characters), these\n\
f0c5b712
JB
4191motion functions will take longer to execute. Emacs may also take\n\
4192longer to update the display.\n\
28e969dd 4193\n\
f0c5b712
JB
4194If cache-long-line-scans is non-nil, these motion functions cache the\n\
4195results of their scans, and consult the cache to avoid rescanning\n\
4196regions of the buffer until the text is modified. The caches are most\n\
4197beneficial when they prevent the most searching---that is, when the\n\
4198buffer contains long lines and large regions of characters with the\n\
4199same, fixed screen width.\n\
28e969dd 4200\n\
f0c5b712
JB
4201When cache-long-line-scans is non-nil, processing short lines will\n\
4202become slightly slower (because of the overhead of consulting the\n\
4203cache), and the caches will use memory roughly proportional to the\n\
4204number of newlines and characters whose screen width varies.\n\
4205\n\
4206The caches require no explicit maintenance; their accuracy is\n\
4207maintained internally by the Emacs primitives. Enabling or disabling\n\
4208the cache should not affect the behavior of any of the motion\n\
4209functions; it should only affect their performance.");
28e969dd 4210
943e065b
RS
4211 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
4212 "Value of point before the last series of scroll operations, or nil.");
4213
be9aafdd
BG
4214 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4215 "List of formats to use when saving this buffer.\n\
4216Formats are defined by `format-alist'. This variable is\n\
4217set when a file is visited. Automatically local in all buffers.");
4218
3cb719bd
RS
4219 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4220 &current_buffer->invisibility_spec, Qnil,
4221 "Invisibility spec of this buffer.\n\
4222The default is t, which means that text is invisible\n\
4223if it has a non-nil `invisible' property.\n\
4224If the value is a list, a text character is invisible if its `invisible'\n\
4225property is an element in that list.\n\
554216ad
KH
4226If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4227then characters with property value PROP are invisible,\n\
3cb719bd
RS
4228and they have an ellipsis as well if ELLIPSIS is non-nil.");
4229
7962a441
RS
4230 DEFVAR_PER_BUFFER ("buffer-display-count",
4231 &current_buffer->display_count, Qnil,
4232 "A number incremented each time the buffer is displayed in a window.");
4233
c48f61ef 4234 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
319c537c
RS
4235 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4236Non-nil also enables highlighting of the region whenever the mark is active.\n\
4237The variable `highlight-nonselected-windows' controls whether to highlight\n\
4238all windows or just the selected window.");
c48f61ef
RS
4239 Vtransient_mark_mode = Qnil;
4240
0a4469c9 4241 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
a96b68f1
RS
4242 "*Non-nil means disregard read-only status of buffers or characters.\n\
4243If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4244text properties. If the value is a list, disregard `buffer-read-only'\n\
4245and disregard a `read-only' text property if the property value\n\
4246is a member of the list.");
4247 Vinhibit_read_only = Qnil;
4248
dcdffbf6
RS
4249 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4250 "List of functions called with no args to query before killing a buffer.");
4251 Vkill_buffer_query_functions = Qnil;
4252
0dc88e60 4253 defsubr (&Sbuffer_live_p);
1ab256cb
RM
4254 defsubr (&Sbuffer_list);
4255 defsubr (&Sget_buffer);
4256 defsubr (&Sget_file_buffer);
4257 defsubr (&Sget_buffer_create);
336cd056 4258 defsubr (&Smake_indirect_buffer);
01050cb5 4259 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
4260 defsubr (&Sbuffer_name);
4261/*defsubr (&Sbuffer_number);*/
4262 defsubr (&Sbuffer_file_name);
336cd056 4263 defsubr (&Sbuffer_base_buffer);
1ab256cb
RM
4264 defsubr (&Sbuffer_local_variables);
4265 defsubr (&Sbuffer_modified_p);
4266 defsubr (&Sset_buffer_modified_p);
4267 defsubr (&Sbuffer_modified_tick);
4268 defsubr (&Srename_buffer);
4269 defsubr (&Sother_buffer);
4270 defsubr (&Sbuffer_disable_undo);
4271 defsubr (&Sbuffer_enable_undo);
4272 defsubr (&Skill_buffer);
4273 defsubr (&Serase_buffer);
a9ee7a59 4274 defsubr (&Sset_buffer_major_mode);
1ab256cb
RM
4275 defsubr (&Sswitch_to_buffer);
4276 defsubr (&Spop_to_buffer);
4277 defsubr (&Scurrent_buffer);
4278 defsubr (&Sset_buffer);
4279 defsubr (&Sbarf_if_buffer_read_only);
4280 defsubr (&Sbury_buffer);
1ab256cb 4281 defsubr (&Skill_all_local_variables);
2eec3b4e 4282
52f8ec73 4283 defsubr (&Soverlayp);
2eec3b4e
RS
4284 defsubr (&Smake_overlay);
4285 defsubr (&Sdelete_overlay);
4286 defsubr (&Smove_overlay);
8ebafa8d
JB
4287 defsubr (&Soverlay_start);
4288 defsubr (&Soverlay_end);
4289 defsubr (&Soverlay_buffer);
4290 defsubr (&Soverlay_properties);
2eec3b4e 4291 defsubr (&Soverlays_at);
74514898 4292 defsubr (&Soverlays_in);
2eec3b4e 4293 defsubr (&Snext_overlay_change);
239c932b 4294 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
4295 defsubr (&Soverlay_recenter);
4296 defsubr (&Soverlay_lists);
4297 defsubr (&Soverlay_get);
4298 defsubr (&Soverlay_put);
1ab256cb
RM
4299}
4300
4301keys_of_buffer ()
4302{
4303 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4304 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
4305
4306 /* This must not be in syms_of_buffer, because Qdisabled is not
4307 initialized when that function gets called. */
4308 Fput (intern ("erase-buffer"), Qdisabled, Qt);
1ab256cb 4309}