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