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