(unchain_marker): Allow differing buffers
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
3cb719bd 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995
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
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
2381d133
JB
22#include <sys/types.h>
23#include <sys/stat.h>
1ab256cb
RM
24#include <sys/param.h>
25
26#ifndef MAXPATHLEN
27/* in 4.1, param.h fails to define this. */
28#define MAXPATHLEN 1024
29#endif /* not MAXPATHLEN */
30
18160b98 31#include <config.h>
1ab256cb 32#include "lisp.h"
21cf4cf8 33#include "intervals.h"
1ab256cb
RM
34#include "window.h"
35#include "commands.h"
36#include "buffer.h"
28e969dd 37#include "region-cache.h"
1ab256cb 38#include "indent.h"
d014bf88 39#include "blockinput.h"
1ab256cb
RM
40
41struct buffer *current_buffer; /* the current buffer */
42
43/* First buffer in chain of all buffers (in reverse order of creation).
44 Threaded through ->next. */
45
46struct buffer *all_buffers;
47
48/* This structure holds the default values of the buffer-local variables
49 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
50 The default value occupies the same slot in this structure
51 as an individual buffer's value occupies in that buffer.
52 Setting the default value also goes through the alist of buffers
53 and stores into each buffer that does not say it has a local value. */
54
55struct buffer buffer_defaults;
56
57/* A Lisp_Object pointer to the above, used for staticpro */
58
59static Lisp_Object Vbuffer_defaults;
60
61/* This structure marks which slots in a buffer have corresponding
62 default values in buffer_defaults.
63 Each such slot has a nonzero value in this structure.
64 The value has only one nonzero bit.
65
66 When a buffer has its own local value for a slot,
67 the bit for that slot (found in the same slot in this structure)
68 is turned on in the buffer's local_var_flags slot.
69
70 If a slot in this structure is -1, then even though there may
71 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
72 and the corresponding slot in buffer_defaults is not used.
73
74 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
75 but there is a default value which is copied into each buffer.
76
77 If a slot in this structure is negative, then even though there may
78 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
79 and the corresponding slot in buffer_defaults is not used.
80
81 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
82 zero, that is a bug */
83
84struct buffer buffer_local_flags;
85
86/* This structure holds the names of symbols whose values may be
87 buffer-local. It is indexed and accessed in the same way as the above. */
88
89struct buffer buffer_local_symbols;
90/* A Lisp_Object pointer to the above, used for staticpro */
91static Lisp_Object Vbuffer_local_symbols;
92
0fa3ba92
JB
93/* This structure holds the required types for the values in the
94 buffer-local slots. If a slot contains Qnil, then the
95 corresponding buffer slot may contain a value of any type. If a
96 slot contains an integer, then prospective values' tags must be
97 equal to that integer. When a tag does not match, the function
98 buffer_slot_type_mismatch will signal an error. */
99struct buffer buffer_local_types;
100
1ab256cb 101Lisp_Object Fset_buffer ();
01050cb5 102void set_buffer_internal ();
173f2a64 103static void call_overlay_mod_hooks ();
1ab256cb
RM
104
105/* Alist of all buffer names vs the buffers. */
106/* This used to be a variable, but is no longer,
107 to prevent lossage due to user rplac'ing this alist or its elements. */
108Lisp_Object Vbuffer_alist;
109
110/* Functions to call before and after each text change. */
111Lisp_Object Vbefore_change_function;
112Lisp_Object Vafter_change_function;
5f079267
RS
113Lisp_Object Vbefore_change_functions;
114Lisp_Object Vafter_change_functions;
1ab256cb 115
c48f61ef
RS
116Lisp_Object Vtransient_mark_mode;
117
a96b68f1
RS
118/* t means ignore all read-only text properties.
119 A list means ignore such a property if its value is a member of the list.
120 Any non-nil value means ignore buffer-read-only. */
121Lisp_Object Vinhibit_read_only;
122
dcdffbf6
RS
123/* List of functions to call that can query about killing a buffer.
124 If any of these functions returns nil, we don't kill it. */
125Lisp_Object Vkill_buffer_query_functions;
126
dbc4e1c1
JB
127/* List of functions to call before changing an unmodified buffer. */
128Lisp_Object Vfirst_change_hook;
129Lisp_Object Qfirst_change_hook;
1ab256cb
RM
130
131Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
132
133Lisp_Object Qprotected_field;
134
135Lisp_Object QSFundamental; /* A string "Fundamental" */
136
137Lisp_Object Qkill_buffer_hook;
138
5fe0b67e
RS
139Lisp_Object Qget_file_buffer;
140
52f8ec73
JB
141Lisp_Object Qoverlayp;
142
9d7608b7 143Lisp_Object Qpriority, Qwindow, Qevaporate;
5985d248 144
294d215f
RS
145Lisp_Object Qmodification_hooks;
146Lisp_Object Qinsert_in_front_hooks;
147Lisp_Object Qinsert_behind_hooks;
148
1ab256cb
RM
149/* For debugging; temporary. See set_buffer_internal. */
150/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
151
152nsberror (spec)
153 Lisp_Object spec;
154{
a7a60ce9 155 if (STRINGP (spec))
1ab256cb
RM
156 error ("No buffer named %s", XSTRING (spec)->data);
157 error ("Invalid buffer argument");
158}
159\f
160DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 0, 0,
161 "Return a list of all existing live buffers.")
162 ()
163{
164 return Fmapcar (Qcdr, Vbuffer_alist);
165}
166
04ae1b48
RS
167/* Like Fassoc, but use Fstring_equal to compare
168 (which ignores text properties),
169 and don't ever QUIT. */
170
171static Lisp_Object
172assoc_ignore_text_properties (key, list)
173 register Lisp_Object key;
174 Lisp_Object list;
175{
176 register Lisp_Object tail;
177 for (tail = list; !NILP (tail); tail = Fcdr (tail))
178 {
179 register Lisp_Object elt, tem;
180 elt = Fcar (tail);
181 tem = Fstring_equal (Fcar (elt), key);
182 if (!NILP (tem))
183 return elt;
184 }
185 return Qnil;
186}
187
1ab256cb
RM
188DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
189 "Return the buffer named NAME (a string).\n\
190If there is no live buffer named NAME, return nil.\n\
191NAME may also be a buffer; if so, the value is that buffer.")
192 (name)
193 register Lisp_Object name;
194{
a7a60ce9 195 if (BUFFERP (name))
1ab256cb
RM
196 return name;
197 CHECK_STRING (name, 0);
198
04ae1b48 199 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
1ab256cb
RM
200}
201
202DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
203 "Return the buffer visiting file FILENAME (a string).\n\
92194d02 204The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
11da5363
RS
205If there is no such live buffer, return nil.\n\
206See also `find-buffer-visiting'.")
1ab256cb
RM
207 (filename)
208 register Lisp_Object filename;
209{
210 register Lisp_Object tail, buf, tem;
5fe0b67e
RS
211 Lisp_Object handler;
212
1ab256cb
RM
213 CHECK_STRING (filename, 0);
214 filename = Fexpand_file_name (filename, Qnil);
215
5fe0b67e
RS
216 /* If the file name has special constructs in it,
217 call the corresponding file handler. */
a617e913 218 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
5fe0b67e
RS
219 if (!NILP (handler))
220 return call2 (handler, Qget_file_buffer, filename);
221
1ab256cb
RM
222 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
223 {
224 buf = Fcdr (XCONS (tail)->car);
a7a60ce9
KH
225 if (!BUFFERP (buf)) continue;
226 if (!STRINGP (XBUFFER (buf)->filename)) continue;
1ab256cb 227 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 228 if (!NILP (tem))
1ab256cb
RM
229 return buf;
230 }
231 return Qnil;
232}
233
234/* Incremented for each buffer created, to assign the buffer number. */
235int buffer_count;
236
237DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
238 "Return the buffer named NAME, or create such a buffer and return it.\n\
239A new buffer is created if there is no live buffer named NAME.\n\
b44895bc 240If NAME starts with a space, the new buffer does not keep undo information.\n\
1ab256cb
RM
241If NAME is a buffer instead of a string, then it is the value returned.\n\
242The value is never nil.")
243 (name)
244 register Lisp_Object name;
245{
a9ee7a59 246 register Lisp_Object buf;
1ab256cb
RM
247 register struct buffer *b;
248
249 buf = Fget_buffer (name);
265a9e55 250 if (!NILP (buf))
1ab256cb
RM
251 return buf;
252
31cd83e9
KH
253 if (XSTRING (name)->size == 0)
254 error ("Empty string for buffer name is not allowed");
255
9ac0d9e0 256 b = (struct buffer *) xmalloc (sizeof (struct buffer));
1ab256cb 257
336cd056
RS
258 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
259
260 /* An ordinary buffer uses its own struct buffer_text. */
261 b->text = &b->own_text;
262 b->base_buffer = 0;
263
1ab256cb 264 BUF_GAP_SIZE (b) = 20;
9ac0d9e0 265 BLOCK_INPUT;
1ab256cb 266 BUFFER_ALLOC (BUF_BEG_ADDR (b), BUF_GAP_SIZE (b));
9ac0d9e0 267 UNBLOCK_INPUT;
1ab256cb 268 if (! BUF_BEG_ADDR (b))
81841847 269 buffer_memory_full ();
1ab256cb
RM
270
271 BUF_PT (b) = 1;
272 BUF_GPT (b) = 1;
273 BUF_BEGV (b) = 1;
274 BUF_ZV (b) = 1;
275 BUF_Z (b) = 1;
276 BUF_MODIFF (b) = 1;
336cd056
RS
277 BUF_SAVE_MODIFF (b) = 1;
278 BUF_INTERVALS (b) = 0;
1ab256cb 279
28e969dd
JB
280 b->newline_cache = 0;
281 b->width_run_cache = 0;
282 b->width_table = Qnil;
283
1ab256cb
RM
284 /* Put this on the chain of all buffers including killed ones. */
285 b->next = all_buffers;
286 all_buffers = b;
287
336cd056
RS
288 /* An ordinary buffer normally doesn't need markers
289 to handle BEGV and ZV. */
290 b->pt_marker = Qnil;
291 b->begv_marker = Qnil;
292 b->zv_marker = Qnil;
04ae1b48
RS
293
294 name = Fcopy_sequence (name);
295 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
1ab256cb 296 b->name = name;
04ae1b48 297
1ab256cb
RM
298 if (XSTRING (name)->data[0] != ' ')
299 b->undo_list = Qnil;
300 else
301 b->undo_list = Qt;
302
303 reset_buffer (b);
bcd40520 304 reset_buffer_local_variables (b);
1ab256cb
RM
305
306 /* Put this in the alist of all live buffers. */
67180c6a 307 XSETBUFFER (buf, b);
1ab256cb
RM
308 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
309
310 b->mark = Fmake_marker ();
336cd056
RS
311 BUF_MARKERS (b) = Qnil;
312 b->name = name;
313 return buf;
314}
315
316DEFUN ("make-indirect-buffer",
317 Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
318 "BMake indirect buffer: \nbIndirect to base buffer: ",
319 "Create and return an indirect buffer named NAME, with base buffer BASE.\n\
320BASE should be an existing buffer (or buffer name).")
321 (name, base_buffer)
322 register Lisp_Object name, base_buffer;
323{
324 register Lisp_Object buf;
325 register struct buffer *b;
326
327 buf = Fget_buffer (name);
328 if (!NILP (buf))
329 error ("Buffer name `%s' is in use", XSTRING (name)->data);
330
331 base_buffer = Fget_buffer (base_buffer);
332 if (NILP (base_buffer))
333 error ("No such buffer: `%s'",
334 XSTRING (XBUFFER (base_buffer)->name)->data);
335
336 if (XSTRING (name)->size == 0)
337 error ("Empty string for buffer name is not allowed");
338
339 b = (struct buffer *) xmalloc (sizeof (struct buffer));
340
341 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
342
343 if (XBUFFER (base_buffer)->base_buffer)
344 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
345 else
346 b->base_buffer = XBUFFER (base_buffer);
347
348 /* Use the base buffer's text object. */
349 b->text = b->base_buffer->text;
350
351 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
352 BUF_ZV (b) = BUF_ZV (b->base_buffer);
353 BUF_PT (b) = BUF_PT (b->base_buffer);
354
355 b->newline_cache = 0;
356 b->width_run_cache = 0;
357 b->width_table = Qnil;
358
359 /* Put this on the chain of all buffers including killed ones. */
360 b->next = all_buffers;
361 all_buffers = b;
362
363 name = Fcopy_sequence (name);
364 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
365 b->name = name;
366
367 reset_buffer (b);
368 reset_buffer_local_variables (b);
369
370 /* Put this in the alist of all live buffers. */
371 XSETBUFFER (buf, b);
372 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
373
374 b->mark = Fmake_marker ();
1ab256cb 375 b->name = name;
336cd056
RS
376
377 /* Make sure the base buffer has markers for its narrowing. */
378 if (NILP (b->base_buffer->pt_marker))
379 {
380 b->base_buffer->pt_marker = Fmake_marker ();
381 Fset_marker (b->base_buffer->pt_marker,
382 make_number (BUF_PT (b->base_buffer)), base_buffer);
383 }
384 if (NILP (b->base_buffer->begv_marker))
385 {
386 b->base_buffer->begv_marker = Fmake_marker ();
387 Fset_marker (b->base_buffer->begv_marker,
388 make_number (BUF_BEGV (b->base_buffer)), base_buffer);
389 }
390 if (NILP (b->base_buffer->zv_marker))
391 {
392 b->base_buffer->zv_marker = Fmake_marker ();
393 Fset_marker (b->base_buffer->zv_marker,
394 make_number (BUF_ZV (b->base_buffer)), base_buffer);
395 }
396
397 /* Give the indirect buffer markers for its narrowing. */
398 b->pt_marker = Fpoint_marker ();
399 b->begv_marker = Fpoint_min_marker ();
400 b->zv_marker = Fpoint_max_marker ();
401
a9ee7a59 402 return buf;
1ab256cb
RM
403}
404
bcd40520
RS
405/* Reinitialize everything about a buffer except its name and contents
406 and local variables. */
1ab256cb
RM
407
408void
409reset_buffer (b)
410 register struct buffer *b;
411{
412 b->filename = Qnil;
f6ed2e84 413 b->file_truename = Qnil;
1ab256cb
RM
414 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
415 b->modtime = 0;
8d7a4592 416 XSETFASTINT (b->save_length, 0);
1ab256cb
RM
417 b->last_window_start = 1;
418 b->backed_up = Qnil;
419 b->auto_save_modified = 0;
84f6bcba 420 b->auto_save_failure_time = -1;
1ab256cb
RM
421 b->auto_save_file_name = Qnil;
422 b->read_only = Qnil;
2eec3b4e
RS
423 b->overlays_before = Qnil;
424 b->overlays_after = Qnil;
8d7a4592 425 XSETFASTINT (b->overlay_center, 1);
dfda7a7f 426 b->mark_active = Qnil;
943e065b 427 b->point_before_scroll = Qnil;
1ab256cb
RM
428}
429
bcd40520
RS
430/* Reset buffer B's local variables info.
431 Don't use this on a buffer that has already been in use;
432 it does not treat permanent locals consistently.
433 Instead, use Fkill_all_local_variables. */
434
c48f61ef 435reset_buffer_local_variables (b)
1ab256cb
RM
436 register struct buffer *b;
437{
438 register int offset;
439
440 /* Reset the major mode to Fundamental, together with all the
441 things that depend on the major mode.
442 default-major-mode is handled at a higher level.
443 We ignore it here. */
444 b->major_mode = Qfundamental_mode;
445 b->keymap = Qnil;
446 b->abbrev_table = Vfundamental_mode_abbrev_table;
447 b->mode_name = QSFundamental;
448 b->minor_modes = Qnil;
449 b->downcase_table = Vascii_downcase_table;
450 b->upcase_table = Vascii_upcase_table;
e9edf33c
RS
451 b->case_canon_table = Vascii_canon_table;
452 b->case_eqv_table = Vascii_eqv_table;
336cd056 453 b->buffer_file_type = Qnil;
3cb719bd
RS
454 b->invisibility_spec = Qt;
455
1ab256cb
RM
456#if 0
457 b->sort_table = XSTRING (Vascii_sort_table);
458 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
459#endif /* 0 */
460
461 /* Reset all per-buffer variables to their defaults. */
462 b->local_var_alist = Qnil;
463 b->local_var_flags = 0;
464
465 /* For each slot that has a default value,
466 copy that into the slot. */
467
468 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
469 offset < sizeof (struct buffer);
4d2f1389 470 offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
aab80822
KH
471 {
472 int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
473 if (flag > 0 || flag == -2)
474 *(Lisp_Object *)(offset + (char *)b) =
475 *(Lisp_Object *)(offset + (char *)&buffer_defaults);
476 }
1ab256cb
RM
477}
478
01050cb5
RM
479/* We split this away from generate-new-buffer, because rename-buffer
480 and set-visited-file-name ought to be able to use this to really
481 rename the buffer properly. */
482
483DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
c273e647 484 1, 2, 0,
01050cb5
RM
485 "Return a string that is the name of no existing buffer based on NAME.\n\
486If there is no live buffer named NAME, then return NAME.\n\
1ab256cb 487Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
c273e647 488until an unused name is found, and then return that name.\n\
03bdd54c 489Optional second argument IGNORE specifies a name that is okay to use\n\
c273e647 490\(if it is in the sequence to be tried)\n\
e8b3a22d 491even if a buffer with that name exists.")
c273e647
RS
492 (name, ignore)
493 register Lisp_Object name, ignore;
1ab256cb
RM
494{
495 register Lisp_Object gentemp, tem;
496 int count;
497 char number[10];
498
499 CHECK_STRING (name, 0);
500
501 tem = Fget_buffer (name);
265a9e55 502 if (NILP (tem))
01050cb5 503 return name;
1ab256cb
RM
504
505 count = 1;
506 while (1)
507 {
508 sprintf (number, "<%d>", ++count);
509 gentemp = concat2 (name, build_string (number));
638e4fc3 510 tem = Fstring_equal (gentemp, ignore);
c273e647
RS
511 if (!NILP (tem))
512 return gentemp;
1ab256cb 513 tem = Fget_buffer (gentemp);
265a9e55 514 if (NILP (tem))
01050cb5 515 return gentemp;
1ab256cb
RM
516 }
517}
518
519\f
520DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
521 "Return the name of BUFFER, as a string.\n\
01050cb5 522With no argument or nil as argument, return the name of the current buffer.")
1ab256cb
RM
523 (buffer)
524 register Lisp_Object buffer;
525{
265a9e55 526 if (NILP (buffer))
1ab256cb
RM
527 return current_buffer->name;
528 CHECK_BUFFER (buffer, 0);
529 return XBUFFER (buffer)->name;
530}
531
532DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
533 "Return name of file BUFFER is visiting, or nil if none.\n\
534No argument or nil as argument means use the current buffer.")
535 (buffer)
536 register Lisp_Object buffer;
537{
265a9e55 538 if (NILP (buffer))
1ab256cb
RM
539 return current_buffer->filename;
540 CHECK_BUFFER (buffer, 0);
541 return XBUFFER (buffer)->filename;
542}
543
336cd056
RS
544DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
545 0, 1, 0,
546 "Return the base buffer of indirect buffer BUFFER.\n\
547If BUFFER is not indirect, return nil.")
548 (buffer)
549 register Lisp_Object buffer;
550{
551 struct buffer *base;
552 Lisp_Object base_buffer;
553
554 if (NILP (buffer))
555 base = current_buffer->base_buffer;
556 else
557 {
558 CHECK_BUFFER (buffer, 0);
559 base = XBUFFER (buffer)->base_buffer;
560 }
561
562 if (! base)
563 return Qnil;
564 XSETBUFFER (base_buffer, base);
565 return base_buffer;
566}
567
1ab256cb
RM
568DEFUN ("buffer-local-variables", Fbuffer_local_variables,
569 Sbuffer_local_variables, 0, 1, 0,
570 "Return an alist of variables that are buffer-local in BUFFER.\n\
553defa4
RS
571Most elements look like (SYMBOL . VALUE), describing one variable.\n\
572For a symbol that is locally unbound, just the symbol appears in the value.\n\
1ab256cb
RM
573Note that storing new VALUEs in these elements doesn't change the variables.\n\
574No argument or nil as argument means use current buffer as BUFFER.")
575 (buffer)
576 register Lisp_Object buffer;
577{
578 register struct buffer *buf;
553defa4 579 register Lisp_Object result;
1ab256cb 580
265a9e55 581 if (NILP (buffer))
1ab256cb
RM
582 buf = current_buffer;
583 else
584 {
585 CHECK_BUFFER (buffer, 0);
586 buf = XBUFFER (buffer);
587 }
588
553defa4
RS
589 result = Qnil;
590
1ab256cb 591 {
553defa4
RS
592 register Lisp_Object tail;
593 for (tail = buf->local_var_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1ab256cb 594 {
553defa4
RS
595 Lisp_Object val, elt;
596
597 elt = XCONS (tail)->car;
598
e0585c64
RS
599 /* Reference each variable in the alist in buf.
600 If inquiring about the current buffer, this gets the current values,
601 so store them into the alist so the alist is up to date.
602 If inquiring about some other buffer, this swaps out any values
603 for that buffer, making the alist up to date automatically. */
604 val = find_symbol_value (XCONS (elt)->car);
605 /* Use the current buffer value only if buf is the current buffer. */
606 if (buf != current_buffer)
553defa4
RS
607 val = XCONS (elt)->cdr;
608
609 /* If symbol is unbound, put just the symbol in the list. */
610 if (EQ (val, Qunbound))
611 result = Fcons (XCONS (elt)->car, result);
612 /* Otherwise, put (symbol . value) in the list. */
613 else
614 result = Fcons (Fcons (XCONS (elt)->car, val), result);
1ab256cb
RM
615 }
616 }
617
1ab256cb
RM
618 /* Add on all the variables stored in special slots. */
619 {
620 register int offset, mask;
621
622 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
623 offset < sizeof (struct buffer);
4d2f1389 624 offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
1ab256cb 625 {
aab80822 626 mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
1ab256cb 627 if (mask == -1 || (buf->local_var_flags & mask))
aab80822
KH
628 if (SYMBOLP (*(Lisp_Object *)(offset
629 + (char *)&buffer_local_symbols)))
630 result = Fcons (Fcons (*((Lisp_Object *)
631 (offset + (char *)&buffer_local_symbols)),
553defa4
RS
632 *(Lisp_Object *)(offset + (char *)buf)),
633 result);
1ab256cb
RM
634 }
635 }
553defa4
RS
636
637 return result;
1ab256cb
RM
638}
639
640\f
641DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
642 0, 1, 0,
643 "Return t if BUFFER was modified since its file was last read or saved.\n\
644No argument or nil as argument means use current buffer as BUFFER.")
645 (buffer)
646 register Lisp_Object buffer;
647{
648 register struct buffer *buf;
265a9e55 649 if (NILP (buffer))
1ab256cb
RM
650 buf = current_buffer;
651 else
652 {
653 CHECK_BUFFER (buffer, 0);
654 buf = XBUFFER (buffer);
655 }
656
336cd056 657 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
1ab256cb
RM
658}
659
660DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
661 1, 1, 0,
662 "Mark current buffer as modified or unmodified according to FLAG.\n\
663A non-nil FLAG means mark the buffer modified.")
664 (flag)
665 register Lisp_Object flag;
666{
667 register int already;
668 register Lisp_Object fn;
669
670#ifdef CLASH_DETECTION
671 /* If buffer becoming modified, lock the file.
672 If buffer becoming unmodified, unlock the file. */
673
674 fn = current_buffer->filename;
265a9e55 675 if (!NILP (fn))
1ab256cb 676 {
336cd056 677 already = SAVE_MODIFF < MODIFF;
265a9e55 678 if (!already && !NILP (flag))
1ab256cb 679 lock_file (fn);
265a9e55 680 else if (already && NILP (flag))
1ab256cb
RM
681 unlock_file (fn);
682 }
683#endif /* CLASH_DETECTION */
684
336cd056 685 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
1ab256cb
RM
686 update_mode_lines++;
687 return flag;
688}
689
690DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
691 0, 1, 0,
692 "Return BUFFER's tick counter, incremented for each change in text.\n\
693Each buffer has a tick counter which is incremented each time the text in\n\
694that buffer is changed. It wraps around occasionally.\n\
695No argument or nil as argument means use current buffer as BUFFER.")
696 (buffer)
697 register Lisp_Object buffer;
698{
699 register struct buffer *buf;
265a9e55 700 if (NILP (buffer))
1ab256cb
RM
701 buf = current_buffer;
702 else
703 {
704 CHECK_BUFFER (buffer, 0);
705 buf = XBUFFER (buffer);
706 }
707
708 return make_number (BUF_MODIFF (buf));
709}
710\f
01050cb5 711DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
4c7e5f09 712 "sRename buffer (to new name): \nP",
1ab256cb 713 "Change current buffer's name to NEWNAME (a string).\n\
3bd779aa 714If second arg UNIQUE is nil or omitted, it is an error if a\n\
01050cb5 715buffer named NEWNAME already exists.\n\
3bd779aa 716If UNIQUE is non-nil, come up with a new name using\n\
01050cb5 717`generate-new-buffer-name'.\n\
3bd779aa
RS
718Interactively, you can set UNIQUE with a prefix argument.\n\
719We return the name we actually gave the buffer.\n\
1ab256cb 720This does not change the name of the visited file (if any).")
489c043a
RS
721 (newname, unique)
722 register Lisp_Object newname, unique;
1ab256cb
RM
723{
724 register Lisp_Object tem, buf;
725
489c043a 726 CHECK_STRING (newname, 0);
d59698c4 727
489c043a 728 if (XSTRING (newname)->size == 0)
d59698c4
RS
729 error ("Empty string is invalid as a buffer name");
730
489c043a 731 tem = Fget_buffer (newname);
c059b5ea
RM
732 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
733 the buffer automatically so you can create another with the original name.
734 It makes UNIQUE equivalent to
489c043a 735 (rename-buffer (generate-new-buffer-name NEWNAME)). */
c059b5ea 736 if (NILP (unique) && XBUFFER (tem) == current_buffer)
fb5eba9c 737 return current_buffer->name;
265a9e55 738 if (!NILP (tem))
01050cb5 739 {
3bd779aa 740 if (!NILP (unique))
489c043a 741 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
01050cb5 742 else
489c043a 743 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
01050cb5 744 }
1ab256cb 745
489c043a 746 current_buffer->name = newname;
76f590d7
JB
747
748 /* Catch redisplay's attention. Unless we do this, the mode lines for
749 any windows displaying current_buffer will stay unchanged. */
750 update_mode_lines++;
751
67180c6a 752 XSETBUFFER (buf, current_buffer);
489c043a 753 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
cf058e49
KH
754 if (NILP (current_buffer->filename)
755 && !NILP (current_buffer->auto_save_file_name))
1ab256cb 756 call0 (intern ("rename-auto-save-file"));
fb5eba9c
RS
757 /* Refetch since that last call may have done GC. */
758 return current_buffer->name;
1ab256cb
RM
759}
760
a0ebb746 761DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
1ab256cb 762 "Return most recently selected buffer other than BUFFER.\n\
a0ebb746
JB
763Buffers not visible in windows are preferred to visible buffers,\n\
764unless optional second argument VISIBLE-OK is non-nil.\n\
1ab256cb
RM
765If no other buffer exists, the buffer `*scratch*' is returned.\n\
766If BUFFER is omitted or nil, some interesting buffer is returned.")
a0ebb746
JB
767 (buffer, visible_ok)
768 register Lisp_Object buffer, visible_ok;
1ab256cb
RM
769{
770 register Lisp_Object tail, buf, notsogood, tem;
771 notsogood = Qnil;
772
265a9e55 773 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
774 {
775 buf = Fcdr (Fcar (tail));
776 if (EQ (buf, buffer))
777 continue;
778 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
779 continue;
04ae1b48
RS
780#ifdef MULTI_FRAME
781 /* If the selected frame has a buffer_predicate,
782 disregard buffers that don't fit the predicate. */
783 tem = frame_buffer_predicate ();
784 if (!NILP (tem))
785 {
786 tem = call1 (tem, buf);
787 if (NILP (tem))
788 continue;
789 }
790#endif
791
a0ebb746 792 if (NILP (visible_ok))
db732e5a 793 tem = Fget_buffer_window (buf, Qt);
a0ebb746
JB
794 else
795 tem = Qnil;
265a9e55 796 if (NILP (tem))
1ab256cb 797 return buf;
265a9e55 798 if (NILP (notsogood))
1ab256cb
RM
799 notsogood = buf;
800 }
265a9e55 801 if (!NILP (notsogood))
1ab256cb
RM
802 return notsogood;
803 return Fget_buffer_create (build_string ("*scratch*"));
804}
805\f
5b8bcf48 806DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo, 0, 1,
1ab256cb 8070,
5b8bcf48
RS
808 "Make BUFFER stop keeping undo information.\n\
809No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
810 (buffer)
811 register Lisp_Object buffer;
1ab256cb 812{
ffd56f97
JB
813 Lisp_Object real_buffer;
814
815 if (NILP (buffer))
67180c6a 816 XSETBUFFER (real_buffer, current_buffer);
ffd56f97
JB
817 else
818 {
819 real_buffer = Fget_buffer (buffer);
820 if (NILP (real_buffer))
821 nsberror (buffer);
822 }
823
824 XBUFFER (real_buffer)->undo_list = Qt;
825
1ab256cb
RM
826 return Qnil;
827}
828
829DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
830 0, 1, "",
831 "Start keeping undo information for buffer BUFFER.\n\
832No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
833 (buffer)
834 register Lisp_Object buffer;
1ab256cb 835{
ffd56f97 836 Lisp_Object real_buffer;
1ab256cb 837
ffd56f97 838 if (NILP (buffer))
67180c6a 839 XSETBUFFER (real_buffer, current_buffer);
1ab256cb
RM
840 else
841 {
ffd56f97
JB
842 real_buffer = Fget_buffer (buffer);
843 if (NILP (real_buffer))
844 nsberror (buffer);
1ab256cb
RM
845 }
846
ffd56f97
JB
847 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
848 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
849
850 return Qnil;
851}
852
853/*
854 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
855Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
856The buffer being killed will be current while the hook is running.\n\
857See `kill-buffer'."
858 */
859DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
860 "Kill the buffer BUFFER.\n\
861The argument may be a buffer or may be the name of a buffer.\n\
862An argument of nil means kill the current buffer.\n\n\
863Value is t if the buffer is actually killed, nil if user says no.\n\n\
864The value of `kill-buffer-hook' (which may be local to that buffer),\n\
865if not void, is a list of functions to be called, with no arguments,\n\
866before the buffer is actually killed. The buffer to be killed is current\n\
867when the hook functions are called.\n\n\
868Any processes that have this buffer as the `process-buffer' are killed\n\
869with `delete-process'.")
870 (bufname)
871 Lisp_Object bufname;
872{
873 Lisp_Object buf;
874 register struct buffer *b;
875 register Lisp_Object tem;
876 register struct Lisp_Marker *m;
877 struct gcpro gcpro1, gcpro2;
878
265a9e55 879 if (NILP (bufname))
1ab256cb
RM
880 buf = Fcurrent_buffer ();
881 else
882 buf = Fget_buffer (bufname);
265a9e55 883 if (NILP (buf))
1ab256cb
RM
884 nsberror (bufname);
885
886 b = XBUFFER (buf);
887
888 /* Query if the buffer is still modified. */
265a9e55 889 if (INTERACTIVE && !NILP (b->filename)
336cd056 890 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1ab256cb
RM
891 {
892 GCPRO2 (buf, bufname);
893 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
894 XSTRING (b->name)->data));
895 UNGCPRO;
265a9e55 896 if (NILP (tem))
1ab256cb
RM
897 return Qnil;
898 }
899
dcdffbf6 900 /* Run hooks with the buffer to be killed the current buffer. */
1ab256cb
RM
901 {
902 register Lisp_Object val;
903 int count = specpdl_ptr - specpdl;
dcdffbf6 904 Lisp_Object list;
1ab256cb
RM
905
906 record_unwind_protect (save_excursion_restore, save_excursion_save ());
907 set_buffer_internal (b);
dcdffbf6
RS
908
909 /* First run the query functions; if any query is answered no,
910 don't kill the buffer. */
911 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
912 {
913 tem = call0 (Fcar (list));
914 if (NILP (tem))
915 return unbind_to (count, Qnil);
916 }
917
918 /* Then run the hooks. */
fd186f07
RS
919 if (!NILP (Vrun_hooks))
920 call1 (Vrun_hooks, Qkill_buffer_hook);
1ab256cb
RM
921 unbind_to (count, Qnil);
922 }
923
924 /* We have no more questions to ask. Verify that it is valid
925 to kill the buffer. This must be done after the questions
926 since anything can happen within do_yes_or_no_p. */
927
928 /* Don't kill the minibuffer now current. */
929 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
930 return Qnil;
931
265a9e55 932 if (NILP (b->name))
1ab256cb
RM
933 return Qnil;
934
336cd056
RS
935 /* When we kill a base buffer, kill all its indirect buffers.
936 We do it at this stage so nothing terrible happens if they
937 ask questions or their hooks get errors. */
938 if (! b->base_buffer)
939 {
940 struct buffer *other;
941
942 GCPRO1 (buf);
943
944 for (other = all_buffers; other; other = other->next)
945 if (other->base_buffer == b)
946 {
947 Lisp_Object buf;
948 XSETBUFFER (buf, other);
949 Fkill_buffer (buf);
950 }
951
952 UNGCPRO;
953 }
954
1ab256cb
RM
955 /* Make this buffer not be current.
956 In the process, notice if this is the sole visible buffer
957 and give up if so. */
958 if (b == current_buffer)
959 {
172a9c1f 960 tem = Fother_buffer (buf, Qnil);
1ab256cb
RM
961 Fset_buffer (tem);
962 if (b == current_buffer)
963 return Qnil;
964 }
965
966 /* Now there is no question: we can kill the buffer. */
967
968#ifdef CLASH_DETECTION
969 /* Unlock this buffer's file, if it is locked. */
970 unlock_buffer (b);
971#endif /* CLASH_DETECTION */
972
1ab256cb 973 kill_buffer_processes (buf);
1ab256cb
RM
974
975 tem = Vinhibit_quit;
976 Vinhibit_quit = Qt;
977 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
978 Freplace_buffer_in_windows (buf);
979 Vinhibit_quit = tem;
980
9b59d6d0 981 /* Delete any auto-save file, if we saved it in this session. */
a7a60ce9 982 if (STRINGP (b->auto_save_file_name)
9b59d6d0 983 && b->auto_save_modified != 0)
1ab256cb
RM
984 {
985 Lisp_Object tem;
986 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 987 if (! NILP (tem))
cbb6a418 988 internal_delete_file (b->auto_save_file_name);
1ab256cb
RM
989 }
990
336cd056 991 if (! b->base_buffer)
1ab256cb 992 {
336cd056
RS
993 /* Unchain all markers of this buffer
994 and leave them pointing nowhere. */
995 for (tem = BUF_MARKERS (b); !EQ (tem, Qnil); )
996 {
997 m = XMARKER (tem);
998 m->buffer = 0;
999 tem = m->chain;
1000 m->chain = Qnil;
1001 }
1002 BUF_MARKERS (b) = Qnil;
1ab256cb 1003
336cd056
RS
1004#ifdef USE_TEXT_PROPERTIES
1005 BUF_INTERVALS (b) = NULL_INTERVAL;
1006#endif
1007
1008 /* Perhaps we should explicitly free the interval tree here... */
1009 }
33f7013e 1010
1ab256cb 1011 b->name = Qnil;
336cd056 1012
9ac0d9e0 1013 BLOCK_INPUT;
336cd056
RS
1014 if (! b->base_buffer)
1015 BUFFER_FREE (BUF_BEG_ADDR (b));
1016
28e969dd
JB
1017 if (b->newline_cache)
1018 {
1019 free_region_cache (b->newline_cache);
1020 b->newline_cache = 0;
1021 }
1022 if (b->width_run_cache)
1023 {
1024 free_region_cache (b->width_run_cache);
1025 b->width_run_cache = 0;
1026 }
1027 b->width_table = Qnil;
9ac0d9e0 1028 UNBLOCK_INPUT;
1ab256cb
RM
1029 b->undo_list = Qnil;
1030
1031 return Qt;
1032}
1033\f
36a8c287
JB
1034/* Move the assoc for buffer BUF to the front of buffer-alist. Since
1035 we do this each time BUF is selected visibly, the more recently
1036 selected buffers are always closer to the front of the list. This
1037 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb
RM
1038
1039record_buffer (buf)
1040 Lisp_Object buf;
1041{
1042 register Lisp_Object link, prev;
1043
1044 prev = Qnil;
1045 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
1046 {
1047 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
1048 break;
1049 prev = link;
1050 }
1051
36a8c287
JB
1052 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1053 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 1054
265a9e55 1055 if (NILP (prev))
1ab256cb
RM
1056 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
1057 else
1058 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1059
1060 XCONS(link)->cdr = Vbuffer_alist;
1061 Vbuffer_alist = link;
1062}
1063
a9ee7a59
KH
1064DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1065 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1066Use this function before selecting the buffer, since it may need to inspect\n\
1067the current buffer's major mode.")
1068 (buf)
1069 Lisp_Object buf;
1070{
1071 int count;
1072 Lisp_Object function;
1073
1074 function = buffer_defaults.major_mode;
1075 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1076 function = current_buffer->major_mode;
1077
1078 if (NILP (function) || EQ (function, Qfundamental_mode))
1079 return Qnil;
1080
1081 count = specpdl_ptr - specpdl;
1082
1083 /* To select a nonfundamental mode,
1084 select the buffer temporarily and then call the mode function. */
1085
1086 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1087
1088 Fset_buffer (buf);
1089 call0 (function);
1090
1091 return unbind_to (count, Qnil);
1092}
1093
1ab256cb
RM
1094DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1095 "Select buffer BUFFER in the current window.\n\
1096BUFFER may be a buffer or a buffer name.\n\
1097Optional second arg NORECORD non-nil means\n\
1098do not put this buffer at the front of the list of recently selected ones.\n\
1099\n\
1100WARNING: This is NOT the way to work on another buffer temporarily\n\
1101within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1102the window-buffer correspondences.")
1103 (bufname, norecord)
1104 Lisp_Object bufname, norecord;
1105{
1106 register Lisp_Object buf;
1107 Lisp_Object tem;
1108
1109 if (EQ (minibuf_window, selected_window))
1110 error ("Cannot switch buffers in minibuffer window");
1111 tem = Fwindow_dedicated_p (selected_window);
265a9e55 1112 if (!NILP (tem))
1ab256cb
RM
1113 error ("Cannot switch buffers in a dedicated window");
1114
265a9e55 1115 if (NILP (bufname))
172a9c1f 1116 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb 1117 else
a9ee7a59
KH
1118 {
1119 buf = Fget_buffer (bufname);
1120 if (NILP (buf))
1121 {
1122 buf = Fget_buffer_create (bufname);
1123 Fset_buffer_major_mode (buf);
1124 }
1125 }
1ab256cb 1126 Fset_buffer (buf);
265a9e55 1127 if (NILP (norecord))
1ab256cb
RM
1128 record_buffer (buf);
1129
1130 Fset_window_buffer (EQ (selected_window, minibuf_window)
5fcd022d
JB
1131 ? Fnext_window (minibuf_window, Qnil, Qnil)
1132 : selected_window,
1ab256cb
RM
1133 buf);
1134
e8b3a22d 1135 return buf;
1ab256cb
RM
1136}
1137
1138DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 2, 0,
1139 "Select buffer BUFFER in some window, preferably a different one.\n\
1140If BUFFER is nil, then some other buffer is chosen.\n\
1141If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1142If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1143window even if BUFFER is already visible in the selected window.")
1144 (bufname, other)
1145 Lisp_Object bufname, other;
1146{
1147 register Lisp_Object buf;
265a9e55 1148 if (NILP (bufname))
172a9c1f 1149 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb 1150 else
7c2087ee
RS
1151 {
1152 buf = Fget_buffer (bufname);
1153 if (NILP (buf))
1154 {
1155 buf = Fget_buffer_create (bufname);
1156 Fset_buffer_major_mode (buf);
1157 }
1158 }
1ab256cb
RM
1159 Fset_buffer (buf);
1160 record_buffer (buf);
1161 Fselect_window (Fdisplay_buffer (buf, other));
e8b3a22d 1162 return buf;
1ab256cb
RM
1163}
1164
1165DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1166 "Return the current buffer as a Lisp object.")
1167 ()
1168{
1169 register Lisp_Object buf;
67180c6a 1170 XSETBUFFER (buf, current_buffer);
1ab256cb
RM
1171 return buf;
1172}
1173\f
1174/* Set the current buffer to b */
1175
1176void
1177set_buffer_internal (b)
1178 register struct buffer *b;
1179{
1180 register struct buffer *old_buf;
1181 register Lisp_Object tail, valcontents;
a7a60ce9 1182 Lisp_Object tem;
1ab256cb
RM
1183
1184 if (current_buffer == b)
1185 return;
1186
1187 windows_or_buffers_changed = 1;
1188 old_buf = current_buffer;
1189 current_buffer = b;
1190 last_known_column_point = -1; /* invalidate indentation cache */
1191
336cd056
RS
1192 if (old_buf)
1193 {
1194 /* Put the undo list back in the base buffer, so that it appears
1195 that an indirect buffer shares the undo list of its base. */
1196 if (old_buf->base_buffer)
1197 old_buf->base_buffer->undo_list = old_buf->undo_list;
1198
1199 /* If the old current buffer has markers to record PT, BEGV and ZV
1200 when it is not current, update them now. */
1201 if (! NILP (old_buf->pt_marker))
1202 {
1203 Lisp_Object obuf;
1204 XSETBUFFER (obuf, old_buf);
1205 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1206 }
1207 if (! NILP (old_buf->begv_marker))
1208 {
1209 Lisp_Object obuf;
1210 XSETBUFFER (obuf, old_buf);
1211 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1212 }
1213 if (! NILP (old_buf->zv_marker))
1214 {
1215 Lisp_Object obuf;
1216 XSETBUFFER (obuf, old_buf);
1217 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1218 }
1219 }
1220
1221 /* Get the undo list from the base buffer, so that it appears
1222 that an indirect buffer shares the undo list of its base. */
1223 if (b->base_buffer)
1224 b->undo_list = b->base_buffer->undo_list;
1225
1226 /* If the new current buffer has markers to record PT, BEGV and ZV
1227 when it is not current, fetch them now. */
1228 if (! NILP (b->pt_marker))
1229 BUF_PT (b) = marker_position (b->pt_marker);
1230 if (! NILP (b->begv_marker))
1231 BUF_BEGV (b) = marker_position (b->begv_marker);
1232 if (! NILP (b->zv_marker))
1233 BUF_ZV (b) = marker_position (b->zv_marker);
1234
1ab256cb
RM
1235 /* Look down buffer's list of local Lisp variables
1236 to find and update any that forward into C variables. */
1237
265a9e55 1238 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
1239 {
1240 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
a7a60ce9
KH
1241 if ((BUFFER_LOCAL_VALUEP (valcontents)
1242 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
fdc6e516 1243 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
a7a60ce9 1244 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1245 /* Just reference the variable
1246 to cause it to become set for this buffer. */
1247 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1248 }
1249
1250 /* Do the same with any others that were local to the previous buffer */
1251
1252 if (old_buf)
265a9e55 1253 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
1254 {
1255 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
a7a60ce9
KH
1256 if ((BUFFER_LOCAL_VALUEP (valcontents)
1257 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
fdc6e516 1258 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
a7a60ce9 1259 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1ab256cb
RM
1260 /* Just reference the variable
1261 to cause it to become set for this buffer. */
1262 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1263 }
1264}
1265
336cd056
RS
1266/* Switch to buffer B temporarily for redisplay purposes.
1267 This avoids certain things thatdon't need to be done within redisplay. */
1268
1269void
1270set_buffer_temp (b)
1271 struct buffer *b;
1272{
1273 register struct buffer *old_buf;
1274
1275 if (current_buffer == b)
1276 return;
1277
1278 old_buf = current_buffer;
1279 current_buffer = b;
1280
1281 if (old_buf)
1282 {
1283 /* If the old current buffer has markers to record PT, BEGV and ZV
1284 when it is not current, update them now. */
1285 if (! NILP (old_buf->pt_marker))
1286 {
1287 Lisp_Object obuf;
1288 XSETBUFFER (obuf, old_buf);
1289 Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
1290 }
1291 if (! NILP (old_buf->begv_marker))
1292 {
1293 Lisp_Object obuf;
1294 XSETBUFFER (obuf, old_buf);
1295 Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
1296 }
1297 if (! NILP (old_buf->zv_marker))
1298 {
1299 Lisp_Object obuf;
1300 XSETBUFFER (obuf, old_buf);
1301 Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
1302 }
1303 }
1304
1305 /* If the new current buffer has markers to record PT, BEGV and ZV
1306 when it is not current, fetch them now. */
1307 if (! NILP (b->pt_marker))
1308 BUF_PT (b) = marker_position (b->pt_marker);
1309 if (! NILP (b->begv_marker))
1310 BUF_BEGV (b) = marker_position (b->begv_marker);
1311 if (! NILP (b->zv_marker))
1312 BUF_ZV (b) = marker_position (b->zv_marker);
1313}
1314
1ab256cb
RM
1315DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1316 "Make the buffer BUFFER current for editing operations.\n\
1317BUFFER may be a buffer or the name of an existing buffer.\n\
1318See also `save-excursion' when you want to make a buffer current temporarily.\n\
1319This function does not display the buffer, so its effect ends\n\
1320when the current command terminates.\n\
1321Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1322 (bufname)
1323 register Lisp_Object bufname;
1324{
1325 register Lisp_Object buffer;
1326 buffer = Fget_buffer (bufname);
265a9e55 1327 if (NILP (buffer))
1ab256cb 1328 nsberror (bufname);
265a9e55 1329 if (NILP (XBUFFER (buffer)->name))
1ab256cb
RM
1330 error ("Selecting deleted buffer");
1331 set_buffer_internal (XBUFFER (buffer));
1332 return buffer;
1333}
1334\f
1335DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1336 Sbarf_if_buffer_read_only, 0, 0, 0,
1337 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1338 ()
1339{
a96b68f1
RS
1340 if (!NILP (current_buffer->read_only)
1341 && NILP (Vinhibit_read_only))
1ab256cb
RM
1342 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1343 return Qnil;
1344}
1345
1346DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1347 "Put BUFFER at the end of the list of all buffers.\n\
1348There it is the least likely candidate for `other-buffer' to return;\n\
528415e7 1349thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
a5611885
JB
1350If BUFFER is nil or omitted, bury the current buffer.\n\
1351Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1352selected window if it is displayed there.")
1ab256cb
RM
1353 (buf)
1354 register Lisp_Object buf;
1355{
b271272a 1356 /* Figure out what buffer we're going to bury. */
265a9e55 1357 if (NILP (buf))
a5611885 1358 {
67180c6a 1359 XSETBUFFER (buf, current_buffer);
a5611885
JB
1360
1361 /* If we're burying the current buffer, unshow it. */
5fcd022d 1362 Fswitch_to_buffer (Fother_buffer (buf, Qnil), Qnil);
a5611885 1363 }
1ab256cb
RM
1364 else
1365 {
1366 Lisp_Object buf1;
1367
1368 buf1 = Fget_buffer (buf);
265a9e55 1369 if (NILP (buf1))
1ab256cb
RM
1370 nsberror (buf);
1371 buf = buf1;
b271272a
JB
1372 }
1373
a5611885 1374 /* Move buf to the end of the buffer list. */
b271272a
JB
1375 {
1376 register Lisp_Object aelt, link;
1377
1378 aelt = Frassq (buf, Vbuffer_alist);
1379 link = Fmemq (aelt, Vbuffer_alist);
1380 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1381 XCONS (link)->cdr = Qnil;
1382 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1383 }
1ab256cb 1384
1ab256cb
RM
1385 return Qnil;
1386}
1387\f
c922bc55 1388DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1ab256cb 1389 "Delete the entire contents of the current buffer.\n\
2950a20e 1390Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1ab256cb
RM
1391so the buffer is truly empty after this.")
1392 ()
1393{
1394 Fwiden ();
1395 del_range (BEG, Z);
1396 current_buffer->last_window_start = 1;
1397 /* Prevent warnings, or suspension of auto saving, that would happen
1398 if future size is less than past size. Use of erase-buffer
1399 implies that the future text is not really related to the past text. */
8d7a4592 1400 XSETFASTINT (current_buffer->save_length, 0);
1ab256cb
RM
1401 return Qnil;
1402}
1403
1404validate_region (b, e)
1405 register Lisp_Object *b, *e;
1406{
1ab256cb
RM
1407 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1408 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1409
1410 if (XINT (*b) > XINT (*e))
1411 {
03192067
KH
1412 Lisp_Object tem;
1413 tem = *b; *b = *e; *e = tem;
1ab256cb
RM
1414 }
1415
1416 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1417 && XINT (*e) <= ZV))
1418 args_out_of_range (*b, *e);
1419}
1420\f
1ab256cb
RM
1421DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1422 0, 0, 0,
1423 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1424Most local variable bindings are eliminated so that the default values\n\
1425become effective once more. Also, the syntax table is set from\n\
1426`standard-syntax-table', the local keymap is set to nil,\n\
1427and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1428This function also forces redisplay of the mode line.\n\
1429\n\
1430Every function to select a new major mode starts by\n\
1431calling this function.\n\n\
1432As a special exception, local variables whose names have\n\
c5a15222
RS
1433a non-nil `permanent-local' property are not eliminated by this function.\n\
1434\n\
1435The first thing this function does is run\n\
1436the normal hook `change-major-mode-hook'.")
1ab256cb
RM
1437 ()
1438{
1439 register Lisp_Object alist, sym, tem;
1440 Lisp_Object oalist;
7410477a 1441
fd186f07
RS
1442 if (!NILP (Vrun_hooks))
1443 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1ab256cb
RM
1444 oalist = current_buffer->local_var_alist;
1445
1446 /* Make sure no local variables remain set up with this buffer
1447 for their current values. */
1448
265a9e55 1449 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1450 {
1451 sym = XCONS (XCONS (alist)->car)->car;
1452
1453 /* Need not do anything if some other buffer's binding is now encached. */
6746ed7a 1454 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car;
1ab256cb
RM
1455 if (XBUFFER (tem) == current_buffer)
1456 {
1457 /* Symbol is set up for this buffer's old local value.
1458 Set it up for the current buffer with the default value. */
1459
6746ed7a 1460 tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->cdr;
542143d7
RS
1461 /* Store the symbol's current value into the alist entry
1462 it is currently set up for. This is so that, if the
1463 local is marked permanent, and we make it local again below,
1464 we don't lose the value. */
2735b685 1465 XCONS (XCONS (tem)->car)->cdr
6746ed7a 1466 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car);
542143d7 1467 /* Switch to the symbol's default-value alist entry. */
1ab256cb 1468 XCONS (tem)->car = tem;
542143d7 1469 /* Mark it as current for the current buffer. */
6746ed7a
KH
1470 XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car
1471 = Fcurrent_buffer ();
542143d7 1472 /* Store the current value into any forwarding in the symbol. */
6746ed7a 1473 store_symval_forwarding (sym, XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car,
1ab256cb
RM
1474 XCONS (tem)->cdr);
1475 }
1476 }
1477
1478 /* Actually eliminate all local bindings of this buffer. */
1479
1480 reset_buffer_local_variables (current_buffer);
1481
1482 /* Redisplay mode lines; we are changing major mode. */
1483
1484 update_mode_lines++;
1485
1486 /* Any which are supposed to be permanent,
1487 make local again, with the same values they had. */
1488
265a9e55 1489 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1490 {
1491 sym = XCONS (XCONS (alist)->car)->car;
1492 tem = Fget (sym, Qpermanent_local);
265a9e55 1493 if (! NILP (tem))
01050cb5
RM
1494 {
1495 Fmake_local_variable (sym);
1496 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1497 }
1ab256cb
RM
1498 }
1499
1500 /* Force mode-line redisplay. Useful here because all major mode
1501 commands call this function. */
1502 update_mode_lines++;
1503
1504 return Qnil;
1505}
1506\f
2eec3b4e
RS
1507/* Find all the overlays in the current buffer that contain position POS.
1508 Return the number found, and store them in a vector in *VEC_PTR.
1509 Store in *LEN_PTR the size allocated for the vector.
52f8ec73
JB
1510 Store in *NEXT_PTR the next position after POS where an overlay starts,
1511 or ZV if there are no more overlays.
239c932b
RS
1512 Store in *PREV_PTR the previous position after POS where an overlay ends,
1513 or BEGV if there are no previous overlays.
1514 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
1515
1516 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
1517 when this function is called.
1518
1519 If EXTEND is non-zero, we make the vector bigger if necessary.
1520 If EXTEND is zero, we never extend the vector,
1521 and we store only as many overlays as will fit.
1522 But we still return the total number of overlays. */
2eec3b4e
RS
1523
1524int
239c932b 1525overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2eec3b4e 1526 int pos;
61d54cd5 1527 int extend;
2eec3b4e
RS
1528 Lisp_Object **vec_ptr;
1529 int *len_ptr;
1530 int *next_ptr;
239c932b 1531 int *prev_ptr;
1ab256cb 1532{
2eec3b4e
RS
1533 Lisp_Object tail, overlay, start, end, result;
1534 int idx = 0;
1535 int len = *len_ptr;
1536 Lisp_Object *vec = *vec_ptr;
1537 int next = ZV;
239c932b 1538 int prev = BEGV;
61d54cd5
RS
1539 int inhibit_storing = 0;
1540
2eec3b4e 1541 for (tail = current_buffer->overlays_before;
8fc0589a 1542 GC_CONSP (tail);
2eec3b4e
RS
1543 tail = XCONS (tail)->cdr)
1544 {
239c932b 1545 int startpos, endpos;
52f8ec73 1546
2eec3b4e 1547 overlay = XCONS (tail)->car;
1ab256cb 1548
2eec3b4e
RS
1549 start = OVERLAY_START (overlay);
1550 end = OVERLAY_END (overlay);
239c932b
RS
1551 endpos = OVERLAY_POSITION (end);
1552 if (endpos < pos)
1553 {
1554 if (prev < endpos)
1555 prev = endpos;
1556 break;
1557 }
1558 if (endpos == pos)
1559 continue;
2eec3b4e
RS
1560 startpos = OVERLAY_POSITION (start);
1561 if (startpos <= pos)
1562 {
1563 if (idx == len)
1564 {
61d54cd5
RS
1565 /* The supplied vector is full.
1566 Either make it bigger, or don't store any more in it. */
1567 if (extend)
1568 {
1569 *len_ptr = len *= 2;
1570 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1571 *vec_ptr = vec;
1572 }
1573 else
1574 inhibit_storing = 1;
2eec3b4e 1575 }
61d54cd5
RS
1576
1577 if (!inhibit_storing)
1578 vec[idx] = overlay;
1579 /* Keep counting overlays even if we can't return them all. */
1580 idx++;
2eec3b4e
RS
1581 }
1582 else if (startpos < next)
1583 next = startpos;
1584 }
1585
1586 for (tail = current_buffer->overlays_after;
8fc0589a 1587 GC_CONSP (tail);
2eec3b4e 1588 tail = XCONS (tail)->cdr)
1ab256cb 1589 {
239c932b 1590 int startpos, endpos;
52f8ec73 1591
2eec3b4e 1592 overlay = XCONS (tail)->car;
2eec3b4e
RS
1593
1594 start = OVERLAY_START (overlay);
1595 end = OVERLAY_END (overlay);
1596 startpos = OVERLAY_POSITION (start);
52f8ec73 1597 if (pos < startpos)
2eec3b4e
RS
1598 {
1599 if (startpos < next)
1600 next = startpos;
1601 break;
1602 }
239c932b
RS
1603 endpos = OVERLAY_POSITION (end);
1604 if (pos < endpos)
2eec3b4e
RS
1605 {
1606 if (idx == len)
1607 {
61d54cd5
RS
1608 if (extend)
1609 {
1610 *len_ptr = len *= 2;
1611 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1612 *vec_ptr = vec;
1613 }
1614 else
1615 inhibit_storing = 1;
2eec3b4e 1616 }
61d54cd5
RS
1617
1618 if (!inhibit_storing)
1619 vec[idx] = overlay;
1620 idx++;
2eec3b4e 1621 }
239c932b
RS
1622 else if (endpos < pos && endpos > prev)
1623 prev = endpos;
1ab256cb
RM
1624 }
1625
239c932b
RS
1626 if (next_ptr)
1627 *next_ptr = next;
1628 if (prev_ptr)
1629 *prev_ptr = prev;
2eec3b4e
RS
1630 return idx;
1631}
1632\f
5985d248
KH
1633struct sortvec
1634{
1635 Lisp_Object overlay;
1636 int beg, end;
1637 int priority;
1638};
1639
1640static int
1641compare_overlays (s1, s2)
1642 struct sortvec *s1, *s2;
1643{
1644 if (s1->priority != s2->priority)
1645 return s1->priority - s2->priority;
1646 if (s1->beg != s2->beg)
1647 return s1->beg - s2->beg;
1648 if (s1->end != s2->end)
1649 return s2->end - s1->end;
1650 return 0;
1651}
1652
1653/* Sort an array of overlays by priority. The array is modified in place.
1654 The return value is the new size; this may be smaller than the original
1655 size if some of the overlays were invalid or were window-specific. */
1656int
1657sort_overlays (overlay_vec, noverlays, w)
1658 Lisp_Object *overlay_vec;
1659 int noverlays;
1660 struct window *w;
1661{
1662 int i, j;
1663 struct sortvec *sortvec;
1664 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
1665
1666 /* Put the valid and relevant overlays into sortvec. */
1667
1668 for (i = 0, j = 0; i < noverlays; i++)
1669 {
0fa767e7 1670 Lisp_Object tem;
c99fc30f 1671 Lisp_Object overlay;
5985d248 1672
c99fc30f 1673 overlay = overlay_vec[i];
5985d248
KH
1674 if (OVERLAY_VALID (overlay)
1675 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
1676 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
1677 {
0fa767e7
KH
1678 /* If we're interested in a specific window, then ignore
1679 overlays that are limited to some other window. */
1680 if (w)
5985d248 1681 {
0fa767e7
KH
1682 Lisp_Object window;
1683
1684 window = Foverlay_get (overlay, Qwindow);
a7a60ce9 1685 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 1686 continue;
5985d248 1687 }
0fa767e7
KH
1688
1689 /* This overlay is good and counts: put it into sortvec. */
1690 sortvec[j].overlay = overlay;
1691 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
1692 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
1693 tem = Foverlay_get (overlay, Qpriority);
1694 if (INTEGERP (tem))
1695 sortvec[j].priority = XINT (tem);
1696 else
1697 sortvec[j].priority = 0;
1698 j++;
5985d248
KH
1699 }
1700 }
1701 noverlays = j;
1702
1703 /* Sort the overlays into the proper order: increasing priority. */
1704
1705 if (noverlays > 1)
1706 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
1707
1708 for (i = 0; i < noverlays; i++)
1709 overlay_vec[i] = sortvec[i].overlay;
1710 return (noverlays);
1711}
1712\f
5c4f68f1 1713/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 1714
2eec3b4e 1715void
5c4f68f1
JB
1716recenter_overlay_lists (buf, pos)
1717 struct buffer *buf;
2eec3b4e
RS
1718 int pos;
1719{
1720 Lisp_Object overlay, tail, next, prev, beg, end;
1721
1722 /* See if anything in overlays_before should move to overlays_after. */
1723
1724 /* We don't strictly need prev in this loop; it should always be nil.
1725 But we use it for symmetry and in case that should cease to be true
1726 with some future change. */
1727 prev = Qnil;
5c4f68f1 1728 for (tail = buf->overlays_before;
2eec3b4e
RS
1729 CONSP (tail);
1730 prev = tail, tail = next)
1ab256cb 1731 {
2eec3b4e
RS
1732 next = XCONS (tail)->cdr;
1733 overlay = XCONS (tail)->car;
1734
1735 /* If the overlay is not valid, get rid of it. */
1736 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
1737#if 1
1738 abort ();
1739#else
2eec3b4e
RS
1740 {
1741 /* Splice the cons cell TAIL out of overlays_before. */
1742 if (!NILP (prev))
1743 XCONS (prev)->cdr = next;
1744 else
5c4f68f1 1745 buf->overlays_before = next;
2eec3b4e
RS
1746 tail = prev;
1747 continue;
1748 }
52f8ec73 1749#endif
1ab256cb 1750
2eec3b4e
RS
1751 beg = OVERLAY_START (overlay);
1752 end = OVERLAY_END (overlay);
1ab256cb 1753
2eec3b4e 1754 if (OVERLAY_POSITION (end) > pos)
1ab256cb 1755 {
2eec3b4e
RS
1756 /* OVERLAY needs to be moved. */
1757 int where = OVERLAY_POSITION (beg);
1758 Lisp_Object other, other_prev;
1759
1760 /* Splice the cons cell TAIL out of overlays_before. */
1761 if (!NILP (prev))
1762 XCONS (prev)->cdr = next;
1763 else
5c4f68f1 1764 buf->overlays_before = next;
2eec3b4e
RS
1765
1766 /* Search thru overlays_after for where to put it. */
1767 other_prev = Qnil;
5c4f68f1 1768 for (other = buf->overlays_after;
2eec3b4e
RS
1769 CONSP (other);
1770 other_prev = other, other = XCONS (other)->cdr)
1ab256cb 1771 {
2eec3b4e
RS
1772 Lisp_Object otherbeg, otheroverlay, follower;
1773 int win;
1774
1775 otheroverlay = XCONS (other)->car;
1776 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 1777 abort ();
2eec3b4e
RS
1778
1779 otherbeg = OVERLAY_START (otheroverlay);
1780 if (OVERLAY_POSITION (otherbeg) >= where)
1781 break;
1ab256cb 1782 }
2eec3b4e
RS
1783
1784 /* Add TAIL to overlays_after before OTHER. */
1785 XCONS (tail)->cdr = other;
1786 if (!NILP (other_prev))
1787 XCONS (other_prev)->cdr = tail;
1ab256cb 1788 else
5c4f68f1 1789 buf->overlays_after = tail;
2eec3b4e 1790 tail = prev;
1ab256cb 1791 }
2eec3b4e
RS
1792 else
1793 /* We've reached the things that should stay in overlays_before.
1794 All the rest of overlays_before must end even earlier,
1795 so stop now. */
1796 break;
1797 }
1798
1799 /* See if anything in overlays_after should be in overlays_before. */
1800 prev = Qnil;
5c4f68f1 1801 for (tail = buf->overlays_after;
2eec3b4e
RS
1802 CONSP (tail);
1803 prev = tail, tail = next)
1804 {
1805 next = XCONS (tail)->cdr;
1806 overlay = XCONS (tail)->car;
1807
1808 /* If the overlay is not valid, get rid of it. */
1809 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
1810#if 1
1811 abort ();
1812#else
2eec3b4e
RS
1813 {
1814 /* Splice the cons cell TAIL out of overlays_after. */
1815 if (!NILP (prev))
1816 XCONS (prev)->cdr = next;
1817 else
5c4f68f1 1818 buf->overlays_after = next;
2eec3b4e
RS
1819 tail = prev;
1820 continue;
1821 }
52f8ec73 1822#endif
2eec3b4e
RS
1823
1824 beg = OVERLAY_START (overlay);
1825 end = OVERLAY_END (overlay);
1826
1827 /* Stop looking, when we know that nothing further
1828 can possibly end before POS. */
1829 if (OVERLAY_POSITION (beg) > pos)
1830 break;
1831
1832 if (OVERLAY_POSITION (end) <= pos)
1833 {
1834 /* OVERLAY needs to be moved. */
1835 int where = OVERLAY_POSITION (end);
1836 Lisp_Object other, other_prev;
1837
1838 /* Splice the cons cell TAIL out of overlays_after. */
1839 if (!NILP (prev))
1840 XCONS (prev)->cdr = next;
1841 else
5c4f68f1 1842 buf->overlays_after = next;
2eec3b4e
RS
1843
1844 /* Search thru overlays_before for where to put it. */
1845 other_prev = Qnil;
5c4f68f1 1846 for (other = buf->overlays_before;
2eec3b4e
RS
1847 CONSP (other);
1848 other_prev = other, other = XCONS (other)->cdr)
1849 {
1850 Lisp_Object otherend, otheroverlay;
1851 int win;
1852
1853 otheroverlay = XCONS (other)->car;
1854 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 1855 abort ();
2eec3b4e
RS
1856
1857 otherend = OVERLAY_END (otheroverlay);
1858 if (OVERLAY_POSITION (otherend) <= where)
1859 break;
1860 }
1861
1862 /* Add TAIL to overlays_before before OTHER. */
1863 XCONS (tail)->cdr = other;
1864 if (!NILP (other_prev))
1865 XCONS (other_prev)->cdr = tail;
1866 else
5c4f68f1 1867 buf->overlays_before = tail;
2eec3b4e
RS
1868 tail = prev;
1869 }
1870 }
1871
8d7a4592 1872 XSETFASTINT (buf->overlay_center, pos);
2eec3b4e 1873}
2b1bdf65
KH
1874
1875/* Fix up overlays that were garbled as a result of permuting markers
1876 in the range START through END. Any overlay with at least one
1877 endpoint in this range will need to be unlinked from the overlay
1878 list and reinserted in its proper place.
1879 Such an overlay might even have negative size at this point.
1880 If so, we'll reverse the endpoints. Can you think of anything
1881 better to do in this situation? */
1882void
1883fix_overlays_in_range (start, end)
1884 register int start, end;
1885{
1886 Lisp_Object tem, overlay;
1887 Lisp_Object before_list, after_list;
1888 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
1889 int startpos, endpos;
1890
1891 /* This algorithm shifts links around instead of consing and GCing.
1892 The loop invariant is that before_list (resp. after_list) is a
1893 well-formed list except that its last element, the one that
1894 *pbefore (resp. *pafter) points to, is still uninitialized.
1895 So it's not a bug that before_list isn't initialized, although
1896 it may look strange. */
1897 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
1898 {
1899 overlay = XCONS (*ptail)->car;
1900 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
1901 if (endpos < start)
1902 break;
1903 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
1904 if (endpos < end
1905 || (startpos >= start && startpos < end))
1906 {
1907 /* If the overlay is backwards, fix that now. */
1908 if (startpos > endpos)
1909 {
1910 int tem;
1911 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
1912 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
1913 tem = startpos; startpos = endpos; endpos = tem;
1914 }
1915 /* Add it to the end of the wrong list. Later on,
1916 recenter_overlay_lists will move it to the right place. */
1917 if (endpos < XINT (current_buffer->overlay_center))
1918 {
1919 *pafter = *ptail;
1920 pafter = &XCONS (*ptail)->cdr;
1921 }
1922 else
1923 {
1924 *pbefore = *ptail;
1925 pbefore = &XCONS (*ptail)->cdr;
1926 }
1927 *ptail = XCONS (*ptail)->cdr;
1928 }
1929 else
1930 ptail = &XCONS (*ptail)->cdr;
1931 }
1932 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
1933 {
1934 overlay = XCONS (*ptail)->car;
1935 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
1936 if (startpos >= end)
1937 break;
1938 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
1939 if (startpos >= start
1940 || (endpos >= start && endpos < end))
1941 {
1942 if (startpos > endpos)
1943 {
1944 int tem;
1945 Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
1946 Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
1947 tem = startpos; startpos = endpos; endpos = tem;
1948 }
1949 if (endpos < XINT (current_buffer->overlay_center))
1950 {
1951 *pafter = *ptail;
1952 pafter = &XCONS (*ptail)->cdr;
1953 }
1954 else
1955 {
1956 *pbefore = *ptail;
1957 pbefore = &XCONS (*ptail)->cdr;
1958 }
1959 *ptail = XCONS (*ptail)->cdr;
1960 }
1961 else
1962 ptail = &XCONS (*ptail)->cdr;
1963 }
1964
1965 /* Splice the constructed (wrong) lists into the buffer's lists,
1966 and let the recenter function make it sane again. */
1967 *pbefore = current_buffer->overlays_before;
1968 current_buffer->overlays_before = before_list;
1969 recenter_overlay_lists (current_buffer,
1970 XINT (current_buffer->overlay_center));
1971
1972 *pafter = current_buffer->overlays_after;
1973 current_buffer->overlays_after = after_list;
1974 recenter_overlay_lists (current_buffer,
1975 XINT (current_buffer->overlay_center));
1976}
2eec3b4e 1977\f
52f8ec73
JB
1978DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
1979 "Return t if OBJECT is an overlay.")
1980 (object)
1981 Lisp_Object object;
1982{
1983 return (OVERLAYP (object) ? Qt : Qnil);
1984}
1985
5c4f68f1
JB
1986DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 3, 0,
1987 "Create a new overlay with range BEG to END in BUFFER.\n\
1988If omitted, BUFFER defaults to the current buffer.\n\
2eec3b4e 1989BEG and END may be integers or markers.")
5c4f68f1
JB
1990 (beg, end, buffer)
1991 Lisp_Object beg, end, buffer;
2eec3b4e
RS
1992{
1993 Lisp_Object overlay;
5c4f68f1 1994 struct buffer *b;
2eec3b4e 1995
5c4f68f1 1996 if (NILP (buffer))
67180c6a 1997 XSETBUFFER (buffer, current_buffer);
883047b9
JB
1998 else
1999 CHECK_BUFFER (buffer, 2);
2000 if (MARKERP (beg)
2001 && ! EQ (Fmarker_buffer (beg), buffer))
2002 error ("Marker points into wrong buffer");
2003 if (MARKERP (end)
2004 && ! EQ (Fmarker_buffer (end), buffer))
2005 error ("Marker points into wrong buffer");
2eec3b4e 2006
883047b9
JB
2007 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2008 CHECK_NUMBER_COERCE_MARKER (end, 1);
5c4f68f1 2009
883047b9 2010 if (XINT (beg) > XINT (end))
5c4f68f1 2011 {
c99fc30f
KH
2012 Lisp_Object temp;
2013 temp = beg; beg = end; end = temp;
5c4f68f1 2014 }
883047b9
JB
2015
2016 b = XBUFFER (buffer);
2017
2018 beg = Fset_marker (Fmake_marker (), beg, buffer);
2019 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 2020
48e2e3ba
KH
2021 overlay = allocate_misc ();
2022 XMISC (overlay)->type = Lisp_Misc_Overlay;
2023 XOVERLAY (overlay)->start = beg;
2024 XOVERLAY (overlay)->end = end;
2025 XOVERLAY (overlay)->plist = Qnil;
2eec3b4e
RS
2026
2027 /* Put the new overlay on the wrong list. */
2028 end = OVERLAY_END (overlay);
5c4f68f1
JB
2029 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2030 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 2031 else
5c4f68f1 2032 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
2033
2034 /* This puts it in the right list, and in the right order. */
5c4f68f1 2035 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 2036
b61982dd
JB
2037 /* We don't need to redisplay the region covered by the overlay, because
2038 the overlay has no properties at the moment. */
2039
2eec3b4e
RS
2040 return overlay;
2041}
2042
5c4f68f1
JB
2043DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
2044 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3ece337a
JB
2045If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
2046If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
2047buffer.")
5c4f68f1
JB
2048 (overlay, beg, end, buffer)
2049 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 2050{
0a4469c9
RS
2051 struct buffer *b, *ob;
2052 Lisp_Object obuffer;
2053 int count = specpdl_ptr - specpdl;
5c4f68f1 2054
52f8ec73 2055 CHECK_OVERLAY (overlay, 0);
5c4f68f1
JB
2056 if (NILP (buffer))
2057 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 2058 if (NILP (buffer))
67180c6a 2059 XSETBUFFER (buffer, current_buffer);
5c4f68f1 2060 CHECK_BUFFER (buffer, 3);
883047b9
JB
2061
2062 if (MARKERP (beg)
2063 && ! EQ (Fmarker_buffer (beg), buffer))
2064 error ("Marker points into wrong buffer");
2065 if (MARKERP (end)
2066 && ! EQ (Fmarker_buffer (end), buffer))
2067 error ("Marker points into wrong buffer");
2068
b61982dd
JB
2069 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2070 CHECK_NUMBER_COERCE_MARKER (end, 1);
2071
9d7608b7
KH
2072 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
2073 return Fdelete_overlay (overlay);
0a4469c9 2074
b61982dd
JB
2075 if (XINT (beg) > XINT (end))
2076 {
c99fc30f
KH
2077 Lisp_Object temp;
2078 temp = beg; beg = end; end = temp;
b61982dd
JB
2079 }
2080
9d7608b7
KH
2081 specbind (Qinhibit_quit, Qt);
2082
0a4469c9 2083 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 2084 b = XBUFFER (buffer);
0a4469c9 2085 ob = XBUFFER (obuffer);
2eec3b4e 2086
c82ed728 2087 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 2088 if (!EQ (buffer, obuffer))
50760c4a
RS
2089 {
2090 /* Redisplay where the overlay was. */
2091 if (!NILP (obuffer))
2092 {
2093 Lisp_Object o_beg;
2094 Lisp_Object o_end;
2095
2096 o_beg = OVERLAY_START (overlay);
2097 o_end = OVERLAY_END (overlay);
2098 o_beg = OVERLAY_POSITION (o_beg);
2099 o_end = OVERLAY_POSITION (o_end);
2100
efd90478 2101 redisplay_region (ob, XINT (o_beg), XINT (o_end));
50760c4a
RS
2102 }
2103
2104 /* Redisplay where the overlay is going to be. */
efd90478 2105 redisplay_region (b, XINT (beg), XINT (end));
50760c4a
RS
2106
2107 /* Don't limit redisplay to the selected window. */
2108 windows_or_buffers_changed = 1;
2109 }
c82ed728
JB
2110 else
2111 /* Redisplay the area the overlay has just left, or just enclosed. */
2112 {
be8b1c6b
RS
2113 Lisp_Object o_beg;
2114 Lisp_Object o_end;
c82ed728
JB
2115 int change_beg, change_end;
2116
be8b1c6b
RS
2117 o_beg = OVERLAY_START (overlay);
2118 o_end = OVERLAY_END (overlay);
c82ed728
JB
2119 o_beg = OVERLAY_POSITION (o_beg);
2120 o_end = OVERLAY_POSITION (o_end);
2121
2122 if (XINT (o_beg) == XINT (beg))
2123 redisplay_region (b, XINT (o_end), XINT (end));
2124 else if (XINT (o_end) == XINT (end))
2125 redisplay_region (b, XINT (o_beg), XINT (beg));
2126 else
2127 {
2128 if (XINT (beg) < XINT (o_beg)) o_beg = beg;
2129 if (XINT (end) > XINT (o_end)) o_end = end;
2130 redisplay_region (b, XINT (o_beg), XINT (o_end));
2131 }
2132 }
b61982dd 2133
0a4469c9
RS
2134 if (!NILP (obuffer))
2135 {
2136 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
2137 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
2138 }
5c4f68f1
JB
2139
2140 Fset_marker (OVERLAY_START (overlay), beg, buffer);
2141 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e
RS
2142
2143 /* Put the overlay on the wrong list. */
2144 end = OVERLAY_END (overlay);
5c4f68f1
JB
2145 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2146 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 2147 else
5c4f68f1 2148 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
2149
2150 /* This puts it in the right list, and in the right order. */
5c4f68f1 2151 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 2152
0a4469c9 2153 return unbind_to (count, overlay);
2eec3b4e
RS
2154}
2155
2156DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
5c4f68f1 2157 "Delete the overlay OVERLAY from its buffer.")
2eec3b4e 2158 (overlay)
5c4f68f1 2159 Lisp_Object overlay;
2eec3b4e 2160{
0a4469c9 2161 Lisp_Object buffer;
5c4f68f1 2162 struct buffer *b;
0a4469c9 2163 int count = specpdl_ptr - specpdl;
5c4f68f1 2164
52f8ec73
JB
2165 CHECK_OVERLAY (overlay, 0);
2166
0a4469c9
RS
2167 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2168 if (NILP (buffer))
2169 return Qnil;
2170
2171 b = XBUFFER (buffer);
2172
2173 specbind (Qinhibit_quit, Qt);
5c4f68f1
JB
2174
2175 b->overlays_before = Fdelq (overlay, b->overlays_before);
2176 b->overlays_after = Fdelq (overlay, b->overlays_after);
2177
b61982dd 2178 redisplay_region (b,
a927f5c9
RS
2179 marker_position (OVERLAY_START (overlay)),
2180 marker_position (OVERLAY_END (overlay)));
b61982dd 2181
3ece337a
JB
2182 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
2183 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
2184
0a4469c9 2185 return unbind_to (count, Qnil);
2eec3b4e
RS
2186}
2187\f
8ebafa8d
JB
2188/* Overlay dissection functions. */
2189
2190DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
2191 "Return the position at which OVERLAY starts.")
2192 (overlay)
2193 Lisp_Object overlay;
2194{
2195 CHECK_OVERLAY (overlay, 0);
2196
2197 return (Fmarker_position (OVERLAY_START (overlay)));
2198}
2199
2200DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
2201 "Return the position at which OVERLAY ends.")
2202 (overlay)
2203 Lisp_Object overlay;
2204{
2205 CHECK_OVERLAY (overlay, 0);
2206
2207 return (Fmarker_position (OVERLAY_END (overlay)));
2208}
2209
2210DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
2211 "Return the buffer OVERLAY belongs to.")
2212 (overlay)
2213 Lisp_Object overlay;
2214{
2215 CHECK_OVERLAY (overlay, 0);
2216
2217 return Fmarker_buffer (OVERLAY_START (overlay));
2218}
2219
2220DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
2221 "Return a list of the properties on OVERLAY.\n\
2222This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
2223OVERLAY.")
2224 (overlay)
2225 Lisp_Object overlay;
2226{
2227 CHECK_OVERLAY (overlay, 0);
2228
48e2e3ba 2229 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
2230}
2231
2232\f
2eec3b4e 2233DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
eb8c3be9 2234 "Return a list of the overlays that contain position POS.")
2eec3b4e
RS
2235 (pos)
2236 Lisp_Object pos;
2237{
2238 int noverlays;
2eec3b4e
RS
2239 Lisp_Object *overlay_vec;
2240 int len;
2241 Lisp_Object result;
2242
2243 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2244
2245 len = 10;
2246 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2247
2248 /* Put all the overlays we want in a vector in overlay_vec.
2249 Store the length in len. */
239c932b 2250 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, NULL, NULL);
2eec3b4e
RS
2251
2252 /* Make a list of them all. */
2253 result = Flist (noverlays, overlay_vec);
2254
9ac0d9e0 2255 xfree (overlay_vec);
2eec3b4e
RS
2256 return result;
2257}
2258
2259DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
2260 1, 1, 0,
bbe20e81
KH
2261 "Return the next position after POS where an overlay starts or ends.\n\
2262If there are no more overlay boundaries after POS, return (point-max).")
2eec3b4e
RS
2263 (pos)
2264 Lisp_Object pos;
2265{
2266 int noverlays;
2267 int endpos;
2268 Lisp_Object *overlay_vec;
2269 int len;
2eec3b4e
RS
2270 int i;
2271
2272 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2273
2274 len = 10;
2275 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2276
2277 /* Put all the overlays we want in a vector in overlay_vec.
2278 Store the length in len.
2279 endpos gets the position where the next overlay starts. */
239c932b 2280 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &endpos, NULL);
2eec3b4e
RS
2281
2282 /* If any of these overlays ends before endpos,
2283 use its ending point instead. */
2284 for (i = 0; i < noverlays; i++)
2285 {
2286 Lisp_Object oend;
2287 int oendpos;
2288
2289 oend = OVERLAY_END (overlay_vec[i]);
2290 oendpos = OVERLAY_POSITION (oend);
2291 if (oendpos < endpos)
2292 endpos = oendpos;
1ab256cb
RM
2293 }
2294
9ac0d9e0 2295 xfree (overlay_vec);
2eec3b4e
RS
2296 return make_number (endpos);
2297}
239c932b
RS
2298
2299DEFUN ("previous-overlay-change", Fprevious_overlay_change,
2300 Sprevious_overlay_change, 1, 1, 0,
2301 "Return the previous position before POS where an overlay starts or ends.\n\
2302If there are no more overlay boundaries after POS, return (point-min).")
2303 (pos)
2304 Lisp_Object pos;
2305{
2306 int noverlays;
2307 int prevpos;
2308 Lisp_Object *overlay_vec;
2309 int len;
2310 int i;
2311
2312 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2313
2314 len = 10;
2315 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2316
2317 /* Put all the overlays we want in a vector in overlay_vec.
2318 Store the length in len.
2319 prevpos gets the position of an overlay end. */
2320 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, NULL, &prevpos);
2321
2322 /* If any of these overlays starts before endpos,
2323 maybe use its starting point instead. */
2324 for (i = 0; i < noverlays; i++)
2325 {
2326 Lisp_Object ostart;
2327 int ostartpos;
2328
2329 ostart = OVERLAY_START (overlay_vec[i]);
2330 ostartpos = OVERLAY_POSITION (ostart);
2331 if (ostartpos > prevpos && ostartpos < XINT (pos))
2332 prevpos = ostartpos;
2333 }
2334
2335 xfree (overlay_vec);
2336 return make_number (prevpos);
2337}
2eec3b4e
RS
2338\f
2339/* These functions are for debugging overlays. */
2340
2341DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
2342 "Return a pair of lists giving all the overlays of the current buffer.\n\
2343The car has all the overlays before the overlay center;\n\
bbe20e81 2344the cdr has all the overlays after the overlay center.\n\
2eec3b4e
RS
2345Recentering overlays moves overlays between these lists.\n\
2346The lists you get are copies, so that changing them has no effect.\n\
2347However, the overlays you get are the real objects that the buffer uses.")
2348 ()
2349{
2350 Lisp_Object before, after;
2351 before = current_buffer->overlays_before;
2352 if (CONSP (before))
2353 before = Fcopy_sequence (before);
2354 after = current_buffer->overlays_after;
2355 if (CONSP (after))
2356 after = Fcopy_sequence (after);
2357
2358 return Fcons (before, after);
2359}
2360
2361DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
2362 "Recenter the overlays of the current buffer around position POS.")
2363 (pos)
2364 Lisp_Object pos;
2365{
2366 CHECK_NUMBER_COERCE_MARKER (pos, 0);
2367
5c4f68f1 2368 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
2369 return Qnil;
2370}
2371\f
2372DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
2373 "Get the property of overlay OVERLAY with property name NAME.")
2374 (overlay, prop)
2375 Lisp_Object overlay, prop;
2376{
cab4777e 2377 Lisp_Object plist, fallback;
52f8ec73
JB
2378
2379 CHECK_OVERLAY (overlay, 0);
2380
cab4777e
RS
2381 fallback = Qnil;
2382
48e2e3ba 2383 for (plist = XOVERLAY (overlay)->plist;
2eec3b4e
RS
2384 CONSP (plist) && CONSP (XCONS (plist)->cdr);
2385 plist = XCONS (XCONS (plist)->cdr)->cdr)
2386 {
2387 if (EQ (XCONS (plist)->car, prop))
2388 return XCONS (XCONS (plist)->cdr)->car;
cab4777e
RS
2389 else if (EQ (XCONS (plist)->car, Qcategory))
2390 {
2391 Lisp_Object tem;
2392 tem = Fcar (Fcdr (plist));
2393 if (SYMBOLP (tem))
2394 fallback = Fget (tem, prop);
2395 }
2eec3b4e 2396 }
52f8ec73 2397
cab4777e 2398 return fallback;
2eec3b4e
RS
2399}
2400
2401DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
2402 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
2403 (overlay, prop, value)
2404 Lisp_Object overlay, prop, value;
2405{
48e2e3ba 2406 Lisp_Object tail, buffer;
9d7608b7 2407 int changed;
2eec3b4e 2408
52f8ec73 2409 CHECK_OVERLAY (overlay, 0);
b61982dd 2410
274a9425
RS
2411 buffer = Fmarker_buffer (OVERLAY_START (overlay));
2412
48e2e3ba 2413 for (tail = XOVERLAY (overlay)->plist;
2eec3b4e
RS
2414 CONSP (tail) && CONSP (XCONS (tail)->cdr);
2415 tail = XCONS (XCONS (tail)->cdr)->cdr)
274a9425
RS
2416 if (EQ (XCONS (tail)->car, prop))
2417 {
9d7608b7
KH
2418 changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
2419 XCONS (XCONS (tail)->cdr)->car = value;
2420 goto found;
274a9425 2421 }
9d7608b7
KH
2422 /* It wasn't in the list, so add it to the front. */
2423 changed = !NILP (value);
48e2e3ba
KH
2424 XOVERLAY (overlay)->plist
2425 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
9d7608b7
KH
2426 found:
2427 if (! NILP (buffer))
2428 {
2429 if (changed)
2430 redisplay_region (XBUFFER (buffer),
2431 marker_position (OVERLAY_START (overlay)),
2432 marker_position (OVERLAY_END (overlay)));
2433 if (EQ (prop, Qevaporate) && ! NILP (value)
2434 && (OVERLAY_POSITION (OVERLAY_START (overlay))
2435 == OVERLAY_POSITION (OVERLAY_END (overlay))))
2436 Fdelete_overlay (overlay);
2437 }
2eec3b4e 2438 return value;
1ab256cb
RM
2439}
2440\f
173f2a64
RS
2441/* Run the modification-hooks of overlays that include
2442 any part of the text in START to END.
2443 Run the insert-before-hooks of overlay starting at END,
930a9140
RS
2444 and the insert-after-hooks of overlay ending at START.
2445
2446 This is called both before and after the modification.
2447 AFTER is nonzero when we call after the modification.
2448
2449 ARG1, ARG2, ARG3 are arguments to pass to the hook functions. */
173f2a64
RS
2450
2451void
930a9140 2452report_overlay_modification (start, end, after, arg1, arg2, arg3)
173f2a64 2453 Lisp_Object start, end;
930a9140
RS
2454 int after;
2455 Lisp_Object arg1, arg2, arg3;
173f2a64
RS
2456{
2457 Lisp_Object prop, overlay, tail;
2458 int insertion = EQ (start, end);
55b48893 2459 int tail_copied;
930a9140 2460 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
55b48893
RS
2461
2462 overlay = Qnil;
2463 tail = Qnil;
930a9140 2464 GCPRO5 (overlay, tail, arg1, arg2, arg3);
173f2a64 2465
55b48893 2466 tail_copied = 0;
173f2a64
RS
2467 for (tail = current_buffer->overlays_before;
2468 CONSP (tail);
2469 tail = XCONS (tail)->cdr)
2470 {
2471 int startpos, endpos;
be8b1c6b 2472 Lisp_Object ostart, oend;
173f2a64
RS
2473
2474 overlay = XCONS (tail)->car;
2475
2476 ostart = OVERLAY_START (overlay);
2477 oend = OVERLAY_END (overlay);
2478 endpos = OVERLAY_POSITION (oend);
2479 if (XFASTINT (start) > endpos)
2480 break;
2481 startpos = OVERLAY_POSITION (ostart);
2482 if (XFASTINT (end) == startpos && insertion)
2483 {
2484 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
2485 if (!NILP (prop))
2486 {
2487 /* Copy TAIL in case the hook recenters the overlay lists. */
55b48893
RS
2488 if (!tail_copied)
2489 tail = Fcopy_sequence (tail);
2490 tail_copied = 1;
930a9140 2491 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2492 }
173f2a64
RS
2493 }
2494 if (XFASTINT (start) == endpos && insertion)
2495 {
2496 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
2497 if (!NILP (prop))
2498 {
55b48893
RS
2499 if (!tail_copied)
2500 tail = Fcopy_sequence (tail);
2501 tail_copied = 1;
930a9140 2502 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2503 }
173f2a64 2504 }
3bd13e92
KH
2505 /* Test for intersecting intervals. This does the right thing
2506 for both insertion and deletion. */
2507 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
2508 {
2509 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
2510 if (!NILP (prop))
2511 {
55b48893
RS
2512 if (!tail_copied)
2513 tail = Fcopy_sequence (tail);
2514 tail_copied = 1;
930a9140 2515 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2516 }
173f2a64
RS
2517 }
2518 }
2519
55b48893 2520 tail_copied = 0;
173f2a64
RS
2521 for (tail = current_buffer->overlays_after;
2522 CONSP (tail);
2523 tail = XCONS (tail)->cdr)
2524 {
2525 int startpos, endpos;
be8b1c6b 2526 Lisp_Object ostart, oend;
173f2a64
RS
2527
2528 overlay = XCONS (tail)->car;
2529
2530 ostart = OVERLAY_START (overlay);
2531 oend = OVERLAY_END (overlay);
2532 startpos = OVERLAY_POSITION (ostart);
cdf0b096 2533 endpos = OVERLAY_POSITION (oend);
173f2a64
RS
2534 if (XFASTINT (end) < startpos)
2535 break;
2536 if (XFASTINT (end) == startpos && insertion)
2537 {
2538 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
5fb5aa33
RS
2539 if (!NILP (prop))
2540 {
55b48893
RS
2541 if (!tail_copied)
2542 tail = Fcopy_sequence (tail);
2543 tail_copied = 1;
930a9140 2544 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2545 }
173f2a64
RS
2546 }
2547 if (XFASTINT (start) == endpos && insertion)
2548 {
2549 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
5fb5aa33
RS
2550 if (!NILP (prop))
2551 {
55b48893
RS
2552 if (!tail_copied)
2553 tail = Fcopy_sequence (tail);
2554 tail_copied = 1;
930a9140 2555 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2556 }
173f2a64 2557 }
3bd13e92
KH
2558 /* Test for intersecting intervals. This does the right thing
2559 for both insertion and deletion. */
2560 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
173f2a64
RS
2561 {
2562 prop = Foverlay_get (overlay, Qmodification_hooks);
5fb5aa33
RS
2563 if (!NILP (prop))
2564 {
55b48893
RS
2565 if (!tail_copied)
2566 tail = Fcopy_sequence (tail);
2567 tail_copied = 1;
930a9140 2568 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
5fb5aa33 2569 }
173f2a64
RS
2570 }
2571 }
55b48893
RS
2572
2573 UNGCPRO;
173f2a64
RS
2574}
2575
2576static void
930a9140
RS
2577call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
2578 Lisp_Object list, overlay;
2579 int after;
2580 Lisp_Object arg1, arg2, arg3;
173f2a64 2581{
930a9140
RS
2582 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2583 GCPRO4 (list, arg1, arg2, arg3);
173f2a64
RS
2584 while (!NILP (list))
2585 {
930a9140
RS
2586 if (NILP (arg3))
2587 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
2588 else
2589 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
173f2a64
RS
2590 list = Fcdr (list);
2591 }
2592 UNGCPRO;
2593}
9d7608b7
KH
2594
2595/* Delete any zero-sized overlays at position POS, if the `evaporate'
2596 property is set. */
2597void
2598evaporate_overlays (pos)
2599 int pos;
2600{
2601 Lisp_Object tail, overlay, hit_list;
2602
2603 hit_list = Qnil;
2604 if (pos <= XFASTINT (current_buffer->overlay_center))
2605 for (tail = current_buffer->overlays_before; CONSP (tail);
2606 tail = XCONS (tail)->cdr)
2607 {
2608 int endpos;
2609 overlay = XCONS (tail)->car;
2610 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2611 if (endpos < pos)
2612 break;
2613 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
2614 && Foverlay_get (overlay, Qevaporate))
2615 hit_list = Fcons (overlay, hit_list);
2616 }
2617 else
2618 for (tail = current_buffer->overlays_after; CONSP (tail);
2619 tail = XCONS (tail)->cdr)
2620 {
2621 int startpos;
889bf329 2622 overlay = XCONS (tail)->car;
9d7608b7
KH
2623 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2624 if (startpos > pos)
2625 break;
2626 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
2627 && Foverlay_get (overlay, Qevaporate))
2628 hit_list = Fcons (overlay, hit_list);
2629 }
2630 for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
2631 Fdelete_overlay (XCONS (hit_list)->car);
2632}
173f2a64 2633\f
54dfdeb0
KH
2634/* Somebody has tried to store a value with an unacceptable type
2635 into the buffer-local slot with offset OFFSET. */
0fa3ba92 2636void
54dfdeb0
KH
2637buffer_slot_type_mismatch (offset)
2638 int offset;
0fa3ba92 2639{
54dfdeb0 2640 Lisp_Object sym;
0fa3ba92 2641 char *type_name;
54dfdeb0 2642 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
0fa3ba92
JB
2643 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
2644 {
2645 case Lisp_Int: type_name = "integers"; break;
2646 case Lisp_String: type_name = "strings"; break;
0fa3ba92 2647 case Lisp_Symbol: type_name = "symbols"; break;
0fa3ba92
JB
2648 default:
2649 abort ();
2650 }
2651
2652 error ("only %s should be stored in the buffer-local variable %s",
54dfdeb0 2653 type_name, XSYMBOL (sym)->name->data);
0fa3ba92
JB
2654}
2655\f
1ab256cb
RM
2656init_buffer_once ()
2657{
2658 register Lisp_Object tem;
2659
2660 /* Make sure all markable slots in buffer_defaults
2661 are initialized reasonably, so mark_buffer won't choke. */
2662 reset_buffer (&buffer_defaults);
bcd40520 2663 reset_buffer_local_variables (&buffer_defaults);
1ab256cb 2664 reset_buffer (&buffer_local_symbols);
bcd40520 2665 reset_buffer_local_variables (&buffer_local_symbols);
336cd056
RS
2666 /* Prevent GC from getting confused. */
2667 buffer_defaults.text = &buffer_defaults.own_text;
2668 buffer_local_symbols.text = &buffer_local_symbols.own_text;
2669#ifdef USE_TEXT_PROPERTIES
2670 BUF_INTERVALS (&buffer_defaults) = 0;
2671 BUF_INTERVALS (&buffer_local_symbols) = 0;
2672#endif
67180c6a
KH
2673 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
2674 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
2675
2676 /* Set up the default values of various buffer slots. */
2677 /* Must do these before making the first buffer! */
2678
2679 /* real setup is done in loaddefs.el */
2680 buffer_defaults.mode_line_format = build_string ("%-");
2681 buffer_defaults.abbrev_mode = Qnil;
2682 buffer_defaults.overwrite_mode = Qnil;
2683 buffer_defaults.case_fold_search = Qt;
2684 buffer_defaults.auto_fill_function = Qnil;
2685 buffer_defaults.selective_display = Qnil;
2686#ifndef old
2687 buffer_defaults.selective_display_ellipses = Qt;
2688#endif
2689 buffer_defaults.abbrev_table = Qnil;
2690 buffer_defaults.display_table = Qnil;
1ab256cb 2691 buffer_defaults.undo_list = Qnil;
c48f61ef 2692 buffer_defaults.mark_active = Qnil;
2eec3b4e
RS
2693 buffer_defaults.overlays_before = Qnil;
2694 buffer_defaults.overlays_after = Qnil;
8d7a4592 2695 XSETFASTINT (buffer_defaults.overlay_center, 1);
1ab256cb 2696
8d7a4592 2697 XSETFASTINT (buffer_defaults.tab_width, 8);
1ab256cb
RM
2698 buffer_defaults.truncate_lines = Qnil;
2699 buffer_defaults.ctl_arrow = Qt;
2700
f7975d07 2701#ifdef DOS_NT
0776cb1b 2702 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
54ad07d3 2703#endif
8d7a4592
KH
2704 XSETFASTINT (buffer_defaults.fill_column, 70);
2705 XSETFASTINT (buffer_defaults.left_margin, 0);
28e969dd 2706 buffer_defaults.cache_long_line_scans = Qnil;
f6ed2e84 2707 buffer_defaults.file_truename = Qnil;
1ab256cb
RM
2708
2709 /* Assign the local-flags to the slots that have default values.
2710 The local flag is a bit that is used in the buffer
2711 to say that it has its own local value for the slot.
2712 The local flag bits are in the local_var_flags slot of the buffer. */
2713
2714 /* Nothing can work if this isn't true */
4d2f1389 2715 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
1ab256cb
RM
2716
2717 /* 0 means not a lisp var, -1 means always local, else mask */
2718 bzero (&buffer_local_flags, sizeof buffer_local_flags);
aab80822
KH
2719 XSETINT (buffer_local_flags.filename, -1);
2720 XSETINT (buffer_local_flags.directory, -1);
2721 XSETINT (buffer_local_flags.backed_up, -1);
2722 XSETINT (buffer_local_flags.save_length, -1);
2723 XSETINT (buffer_local_flags.auto_save_file_name, -1);
2724 XSETINT (buffer_local_flags.read_only, -1);
2725 XSETINT (buffer_local_flags.major_mode, -1);
2726 XSETINT (buffer_local_flags.mode_name, -1);
2727 XSETINT (buffer_local_flags.undo_list, -1);
2728 XSETINT (buffer_local_flags.mark_active, -1);
943e065b 2729 XSETINT (buffer_local_flags.point_before_scroll, -1);
f6ed2e84 2730 XSETINT (buffer_local_flags.file_truename, -1);
3cb719bd 2731 XSETINT (buffer_local_flags.invisibility_spec, -1);
8d7a4592
KH
2732
2733 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
2734 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
2735 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
2736 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
2737 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
2738 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
1ab256cb 2739#ifndef old
8d7a4592 2740 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
1ab256cb 2741#endif
8d7a4592
KH
2742 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
2743 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
2744 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
2745 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
2746 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
2747 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
2748 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
2749 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
28e969dd 2750 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
f7975d07 2751#ifdef DOS_NT
8d7a4592 2752 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
54ad07d3 2753#endif
1ab256cb
RM
2754
2755 Vbuffer_alist = Qnil;
2756 current_buffer = 0;
2757 all_buffers = 0;
2758
2759 QSFundamental = build_string ("Fundamental");
2760
2761 Qfundamental_mode = intern ("fundamental-mode");
2762 buffer_defaults.major_mode = Qfundamental_mode;
2763
2764 Qmode_class = intern ("mode-class");
2765
2766 Qprotected_field = intern ("protected-field");
2767
2768 Qpermanent_local = intern ("permanent-local");
2769
2770 Qkill_buffer_hook = intern ("kill-buffer-hook");
2771
2772 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
2773 /* super-magic invisible buffer */
2774 Vbuffer_alist = Qnil;
2775
ffd56f97 2776 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1ab256cb
RM
2777}
2778
2779init_buffer ()
2780{
2781 char buf[MAXPATHLEN+1];
2381d133
JB
2782 char *pwd;
2783 struct stat dotstat, pwdstat;
136351b7 2784 Lisp_Object temp;
f7975d07 2785 int rc;
1ab256cb
RM
2786
2787 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
2381d133
JB
2788
2789 /* If PWD is accurate, use it instead of calling getwd. This is faster
2790 when PWD is right, and may avoid a fatal error. */
f7975d07 2791 if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
2381d133
JB
2792 && stat (pwd, &pwdstat) == 0
2793 && stat (".", &dotstat) == 0
2794 && dotstat.st_ino == pwdstat.st_ino
2795 && dotstat.st_dev == pwdstat.st_dev
2796 && strlen (pwd) < MAXPATHLEN)
2797 strcpy (buf, pwd);
2798 else if (getwd (buf) == 0)
cf1e6391 2799 fatal ("`getwd' failed: %s\n", buf);
1ab256cb
RM
2800
2801#ifndef VMS
2802 /* Maybe this should really use some standard subroutine
2803 whose definition is filename syntax dependent. */
f7975d07
RS
2804 rc = strlen (buf);
2805 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
2806 {
2807 buf[rc] = DIRECTORY_SEP;
2808 buf[rc + 1] = '\0';
2809 }
1ab256cb
RM
2810#endif /* not VMS */
2811 current_buffer->directory = build_string (buf);
136351b7
RS
2812
2813 temp = get_minibuffer (0);
2814 XBUFFER (temp)->directory = current_buffer->directory;
1ab256cb
RM
2815}
2816
2817/* initialize the buffer routines */
2818syms_of_buffer ()
2819{
188d4d11
RM
2820 extern Lisp_Object Qdisabled;
2821
1ab256cb
RM
2822 staticpro (&Vbuffer_defaults);
2823 staticpro (&Vbuffer_local_symbols);
2824 staticpro (&Qfundamental_mode);
2825 staticpro (&Qmode_class);
2826 staticpro (&QSFundamental);
2827 staticpro (&Vbuffer_alist);
2828 staticpro (&Qprotected_field);
2829 staticpro (&Qpermanent_local);
2830 staticpro (&Qkill_buffer_hook);
52f8ec73 2831 staticpro (&Qoverlayp);
9d7608b7
KH
2832 Qevaporate = intern ("evaporate");
2833 staticpro (&Qevaporate);
294d215f
RS
2834 staticpro (&Qmodification_hooks);
2835 Qmodification_hooks = intern ("modification-hooks");
2836 staticpro (&Qinsert_in_front_hooks);
2837 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
2838 staticpro (&Qinsert_behind_hooks);
2839 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5fe0b67e
RS
2840 staticpro (&Qget_file_buffer);
2841 Qget_file_buffer = intern ("get-file-buffer");
5985d248
KH
2842 Qpriority = intern ("priority");
2843 staticpro (&Qpriority);
2844 Qwindow = intern ("window");
2845 staticpro (&Qwindow);
52f8ec73
JB
2846
2847 Qoverlayp = intern ("overlayp");
1ab256cb
RM
2848
2849 Fput (Qprotected_field, Qerror_conditions,
2850 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
2851 Fput (Qprotected_field, Qerror_message,
2852 build_string ("Attempt to modify a protected field"));
2853
2854 /* All these use DEFVAR_LISP_NOPRO because the slots in
2855 buffer_defaults will all be marked via Vbuffer_defaults. */
2856
2857 DEFVAR_LISP_NOPRO ("default-mode-line-format",
2858 &buffer_defaults.mode_line_format,
2859 "Default value of `mode-line-format' for buffers that don't override it.\n\
2860This is the same as (default-value 'mode-line-format).");
2861
2862 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
2863 &buffer_defaults.abbrev_mode,
2864 "Default value of `abbrev-mode' for buffers that do not override it.\n\
2865This is the same as (default-value 'abbrev-mode).");
2866
2867 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
2868 &buffer_defaults.ctl_arrow,
2869 "Default value of `ctl-arrow' for buffers that do not override it.\n\
2870This is the same as (default-value 'ctl-arrow).");
2871
2872 DEFVAR_LISP_NOPRO ("default-truncate-lines",
2873 &buffer_defaults.truncate_lines,
2874 "Default value of `truncate-lines' for buffers that do not override it.\n\
2875This is the same as (default-value 'truncate-lines).");
2876
2877 DEFVAR_LISP_NOPRO ("default-fill-column",
2878 &buffer_defaults.fill_column,
2879 "Default value of `fill-column' for buffers that do not override it.\n\
2880This is the same as (default-value 'fill-column).");
2881
2882 DEFVAR_LISP_NOPRO ("default-left-margin",
2883 &buffer_defaults.left_margin,
2884 "Default value of `left-margin' for buffers that do not override it.\n\
2885This is the same as (default-value 'left-margin).");
2886
2887 DEFVAR_LISP_NOPRO ("default-tab-width",
2888 &buffer_defaults.tab_width,
2889 "Default value of `tab-width' for buffers that do not override it.\n\
2890This is the same as (default-value 'tab-width).");
2891
2892 DEFVAR_LISP_NOPRO ("default-case-fold-search",
2893 &buffer_defaults.case_fold_search,
2894 "Default value of `case-fold-search' for buffers that don't override it.\n\
2895This is the same as (default-value 'case-fold-search).");
2896
f7975d07 2897#ifdef DOS_NT
54ad07d3
RS
2898 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
2899 &buffer_defaults.buffer_file_type,
2900 "Default file type for buffers that do not override it.\n\
2901This is the same as (default-value 'buffer-file-type).\n\
2902The file type is nil for text, t for binary.");
2903#endif
2904
0fa3ba92
JB
2905 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
2906 Qnil, 0);
1ab256cb
RM
2907
2908/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
2909 But make-docfile finds it!
2910 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
bec44fd6 2911 Qnil,
1ab256cb
RM
2912 "Template for displaying mode line for current buffer.\n\
2913Each buffer has its own value of this variable.\n\
2914Value may be a string, a symbol or a list or cons cell.\n\
2915For a symbol, its value is used (but it is ignored if t or nil).\n\
2916 A string appearing directly as the value of a symbol is processed verbatim\n\
2917 in that the %-constructs below are not recognized.\n\
2918For a list whose car is a symbol, the symbol's value is taken,\n\
2919 and if that is non-nil, the cadr of the list is processed recursively.\n\
2920 Otherwise, the caddr of the list (if there is one) is processed.\n\
2921For a list whose car is a string or list, each element is processed\n\
2922 recursively and the results are effectively concatenated.\n\
2923For a list whose car is an integer, the cdr of the list is processed\n\
2924 and padded (if the number is positive) or truncated (if negative)\n\
2925 to the width specified by that number.\n\
2926A string is printed verbatim in the mode line except for %-constructs:\n\
2927 (%-constructs are allowed when the string is the entire mode-line-format\n\
2928 or when it is found in a cons-cell or a list)\n\
2929 %b -- print buffer name. %f -- print visited file name.\n\
c2ff34f7
RS
2930 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
2931 % means buffer is read-only and * means it is modified.\n\
2932 For a modified read-only buffer, %* gives % and %+ gives *.\n\
a97c374a 2933 %s -- print process status. %l -- print the current line number.\n\
dd24e6a6 2934 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
9d130ffc 2935 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
dd24e6a6 2936 or print Bottom or All.\n\
1ab256cb 2937 %n -- print Narrow if appropriate.\n\
54ad07d3 2938 %t -- print T if files is text, B if binary.\n\
1ab256cb
RM
2939 %[ -- print one [ for each recursive editing level. %] similar.\n\
2940 %% -- print %. %- -- print infinitely many dashes.\n\
2941Decimal digits after the % specify field width to which to pad.");
2942*/
2943
2944 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
2945 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
2946nil here means use current buffer's major mode.");
2947
2948 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
0fa3ba92 2949 make_number (Lisp_Symbol),
1ab256cb
RM
2950 "Symbol for current buffer's major mode.");
2951
2952 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
0fa3ba92 2953 make_number (Lisp_String),
1ab256cb
RM
2954 "Pretty name of current buffer's major mode (a string).");
2955
0fa3ba92 2956 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
1ab256cb
RM
2957 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
2958Automatically becomes buffer-local when set in any fashion.");
2959
2960 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
0fa3ba92 2961 Qnil,
1ab256cb
RM
2962 "*Non-nil if searches should ignore case.\n\
2963Automatically becomes buffer-local when set in any fashion.");
2964
2965 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
0fa3ba92 2966 make_number (Lisp_Int),
1ab256cb
RM
2967 "*Column beyond which automatic line-wrapping should happen.\n\
2968Automatically becomes buffer-local when set in any fashion.");
2969
2970 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
0fa3ba92 2971 make_number (Lisp_Int),
1ab256cb
RM
2972 "*Column for the default indent-line-function to indent to.\n\
2973Linefeed indents to this column in Fundamental mode.\n\
2974Automatically becomes buffer-local when set in any fashion.");
2975
2976 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
0fa3ba92 2977 make_number (Lisp_Int),
1ab256cb
RM
2978 "*Distance between tab stops (for display of tab characters), in columns.\n\
2979Automatically becomes buffer-local when set in any fashion.");
2980
0fa3ba92 2981 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
1ab256cb
RM
2982 "*Non-nil means display control chars with uparrow.\n\
2983Nil means use backslash and octal digits.\n\
2984Automatically becomes buffer-local when set in any fashion.\n\
2985This variable does not apply to characters whose display is specified\n\
2986in the current display table (if there is one).");
2987
0fa3ba92 2988 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
1ab256cb
RM
2989 "*Non-nil means do not display continuation lines;\n\
2990give each line of text one screen line.\n\
2991Automatically becomes buffer-local when set in any fashion.\n\
2992\n\
2993Note that this is overridden by the variable\n\
2994`truncate-partial-width-windows' if that variable is non-nil\n\
502b9b64 2995and this buffer is not full-frame width.");
1ab256cb 2996
f7975d07 2997#ifdef DOS_NT
54ad07d3
RS
2998 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
2999 Qnil,
006d3d34
RS
3000 "Non-nil if the visited file is a binary file.\n\
3001This variable is meaningful on MS-DOG and Windows NT.\n\
3002On those systems, it is automatically local in every buffer.\n\
e0585c64 3003On other systems, this variable is normally always nil.");
54ad07d3
RS
3004#endif
3005
1ab256cb 3006 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
0fa3ba92 3007 make_number (Lisp_String),
1ab256cb
RM
3008 "Name of default directory of current buffer. Should end with slash.\n\
3009Each buffer has its own value of this variable.");
3010
3011 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
0fa3ba92 3012 Qnil,
1ab256cb
RM
3013 "Function called (if non-nil) to perform auto-fill.\n\
3014It is called after self-inserting a space at a column beyond `fill-column'.\n\
3015Each buffer has its own value of this variable.\n\
3016NOTE: This variable is not an ordinary hook;\n\
3017It may not be a list of functions.");
3018
3019 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
0fa3ba92 3020 make_number (Lisp_String),
1ab256cb
RM
3021 "Name of file visited in current buffer, or nil if not visiting a file.\n\
3022Each buffer has its own value of this variable.");
3023
f6ed2e84
RS
3024 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
3025 make_number (Lisp_String),
3026 "Truename of file visited in current buffer, or nil if not visiting a file.\n\
3027The truename of a file is calculated by `file-truename'.\n\
3028Each buffer has its own value of this variable.");
3029
1ab256cb 3030 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 3031 &current_buffer->auto_save_file_name,
0fa3ba92 3032 make_number (Lisp_String),
1ab256cb
RM
3033 "Name of file for auto-saving current buffer,\n\
3034or nil if buffer should not be auto-saved.\n\
3035Each buffer has its own value of this variable.");
3036
0fa3ba92 3037 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
1ab256cb
RM
3038 "Non-nil if this buffer is read-only.\n\
3039Each buffer has its own value of this variable.");
3040
0fa3ba92 3041 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
1ab256cb
RM
3042 "Non-nil if this buffer's file has been backed up.\n\
3043Backing up is done before the first time the file is saved.\n\
3044Each buffer has its own value of this variable.");
3045
3046 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
0fa3ba92 3047 make_number (Lisp_Int),
1ab256cb
RM
3048 "Length of current buffer when last read in, saved or auto-saved.\n\
30490 initially.\n\
3050Each buffer has its own value of this variable.");
3051
3052 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
0fa3ba92 3053 Qnil,
1ab256cb
RM
3054 "Non-nil enables selective display:\n\
3055Integer N as value means display only lines\n\
3056 that start with less than n columns of space.\n\
3057A value of t means, after a ^M, all the rest of the line is invisible.\n\
3058 Then ^M's in the file are written into files as newlines.\n\n\
3059Automatically becomes buffer-local when set in any fashion.");
3060
3061#ifndef old
3062 DEFVAR_PER_BUFFER ("selective-display-ellipses",
3063 &current_buffer->selective_display_ellipses,
0fa3ba92 3064 Qnil,
1ab256cb
RM
3065 "t means display ... on previous line when a line is invisible.\n\
3066Automatically becomes buffer-local when set in any fashion.");
3067#endif
3068
0fa3ba92 3069 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
1ab256cb 3070 "Non-nil if self-insertion should replace existing text.\n\
5e05d0a5
RS
3071The value should be one of `overwrite-mode-textual',\n\
3072`overwrite-mode-binary', or nil.\n\
3073If it is `overwrite-mode-textual', self-insertion still\n\
6bbb0d4a 3074inserts at the end of a line, and inserts when point is before a tab,\n\
2e94b813 3075until the tab is filled in.\n\
6bbb0d4a 3076If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
1ab256cb
RM
3077Automatically becomes buffer-local when set in any fashion.");
3078
54939090
RS
3079#if 0 /* The doc string is too long for some compilers,
3080 but make-docfile can find it in this comment. */
1ab256cb 3081 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5d305367 3082 Qnil,
1ab256cb
RM
3083 "Display table that controls display of the contents of current buffer.\n\
3084Automatically becomes buffer-local when set in any fashion.\n\
3085The display table is a vector created with `make-display-table'.\n\
3086The first 256 elements control how to display each possible text character.\n\
6158b3b0 3087Each value should be a vector of characters or nil;\n\
1ab256cb 3088nil means display the character in the default fashion.\n\
a45e35e1 3089The remaining six elements control the display of\n\
6158b3b0
JB
3090 the end of a truncated screen line (element 256, a single character);\n\
3091 the end of a continued line (element 257, a single character);\n\
3092 the escape character used to display character codes in octal\n\
3093 (element 258, a single character);\n\
3094 the character used as an arrow for control characters (element 259,\n\
3095 a single character);\n\
3096 the decoration indicating the presence of invisible lines (element 260,\n\
a45e35e1
JB
3097 a vector of characters);\n\
3098 the character used to draw the border between side-by-side windows\n\
3402827c 3099 (element 261, a single character).\n\
1ab256cb
RM
3100If this variable is nil, the value of `standard-display-table' is used.\n\
3101Each window can have its own, overriding display table.");
54939090
RS
3102#endif
3103 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
de15914a 3104 Qnil, 0);
1ab256cb 3105
1ab256cb
RM
3106/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
3107 "Don't ask.");
3108*/
01050cb5 3109 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
1ab256cb
RM
3110 "Function to call before each text change.\n\
3111Two arguments are passed to the function: the positions of\n\
3112the beginning and end of the range of old text to be changed.\n\
3113\(For an insertion, the beginning and end are at the same place.)\n\
3114No information is given about the length of the text after the change.\n\
1ab256cb 3115\n\
5f079267 3116Buffer changes made while executing the `before-change-function'\n\
b86344d0
RS
3117don't call any before-change or after-change functions.\n\
3118That's because these variables are temporarily set to nil.\n\
3119As a result, a hook function cannot straightforwardly alter the value of\n\
3120these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 3121accomplishing an equivalent result by using other variables.");
1ab256cb
RM
3122 Vbefore_change_function = Qnil;
3123
3124 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
3125 "Function to call after each text change.\n\
3126Three arguments are passed to the function: the positions of\n\
3127the beginning and end of the range of changed text,\n\
3128and the length of the pre-change text replaced by that range.\n\
3129\(For an insertion, the pre-change length is zero;\n\
3130for a deletion, that length is the number of characters deleted,\n\
3131and the post-change beginning and end are at the same place.)\n\
3132\n\
5f079267 3133Buffer changes made while executing the `after-change-function'\n\
b86344d0
RS
3134don't call any before-change or after-change functions.\n\
3135That's because these variables are temporarily set to nil.\n\
3136As a result, a hook function cannot straightforwardly alter the value of\n\
3137these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 3138accomplishing an equivalent result by using other variables.");
1ab256cb
RM
3139 Vafter_change_function = Qnil;
3140
5f079267
RS
3141 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
3142 "List of functions to call before each text change.\n\
3143Two arguments are passed to each function: the positions of\n\
3144the beginning and end of the range of old text to be changed.\n\
3145\(For an insertion, the beginning and end are at the same place.)\n\
3146No information is given about the length of the text after the change.\n\
5f079267
RS
3147\n\
3148Buffer changes made while executing the `before-change-functions'\n\
b86344d0
RS
3149don't call any before-change or after-change functions.\n\
3150That's because these variables are temporarily set to nil.\n\
3151As a result, a hook function cannot straightforwardly alter the value of\n\
3152these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 3153accomplishing an equivalent result by using other variables.");
5f079267
RS
3154 Vbefore_change_functions = Qnil;
3155
3156 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
3157 "List of function to call after each text change.\n\
3158Three arguments are passed to each function: the positions of\n\
3159the beginning and end of the range of changed text,\n\
3160and the length of the pre-change text replaced by that range.\n\
3161\(For an insertion, the pre-change length is zero;\n\
3162for a deletion, that length is the number of characters deleted,\n\
3163and the post-change beginning and end are at the same place.)\n\
3164\n\
3165Buffer changes made while executing the `after-change-functions'\n\
b86344d0
RS
3166don't call any before-change or after-change functions.\n\
3167That's because these variables are temporarily set to nil.\n\
3168As a result, a hook function cannot straightforwardly alter the value of\n\
3169these variables. See the Emacs Lisp manual for a way of\n\
d59698c4 3170accomplishing an equivalent result by using other variables.");
b86344d0 3171
5f079267
RS
3172 Vafter_change_functions = Qnil;
3173
dbc4e1c1
JB
3174 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
3175 "A list of functions to call before changing a buffer which is unmodified.\n\
3176The functions are run using the `run-hooks' function.");
3177 Vfirst_change_hook = Qnil;
3178 Qfirst_change_hook = intern ("first-change-hook");
3179 staticpro (&Qfirst_change_hook);
1ab256cb 3180
54939090
RS
3181#if 0 /* The doc string is too long for some compilers,
3182 but make-docfile can find it in this comment. */
3f5fcd47 3183 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
1ab256cb
RM
3184 "List of undo entries in current buffer.\n\
3185Recent changes come first; older changes follow newer.\n\
3186\n\
3187An entry (START . END) represents an insertion which begins at\n\
3188position START and ends at position END.\n\
3189\n\
3190An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
3191from (abs POSITION). If POSITION is positive, point was at the front\n\
3192of the text being deleted; if negative, point was at the end.\n\
3193\n\
6c0df54a
RS
3194An entry (t HIGH . LOW) indicates that the buffer previously had\n\
3195\"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
3196of the visited file's modification time, as of that time. If the\n\
3197modification time of the most recent save is different, this entry is\n\
1ab256cb
RM
3198obsolete.\n\
3199\n\
6c0df54a
RS
3200An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
3201was modified between BEG and END. PROPERTY is the property name,\n\
3202and VALUE is the old value.\n\
483c1fd3 3203\n\
bec44fd6
JB
3204An entry of the form POSITION indicates that point was at the buffer\n\
3205location given by the integer. Undoing an entry of this form places\n\
3206point at POSITION.\n\
3207\n\
1ab256cb
RM
3208nil marks undo boundaries. The undo command treats the changes\n\
3209between two undo boundaries as a single step to be undone.\n\
3210\n\
bec44fd6 3211If the value of the variable is t, undo information is not recorded.");
54939090
RS
3212#endif
3213 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
de15914a 3214 0);
1ab256cb 3215
c48f61ef
RS
3216 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
3217 "Non-nil means the mark and region are currently active in this buffer.\n\
3218Automatically local in all buffers.");
3219
28e969dd 3220 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
f0c5b712
JB
3221 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
3222This variable is buffer-local, in all buffers.\n\
28e969dd 3223\n\
f0c5b712
JB
3224Normally, the line-motion functions work by scanning the buffer for\n\
3225newlines. Columnar operations (like move-to-column and\n\
3226compute-motion) also work by scanning the buffer, summing character\n\
3227widths as they go. This works well for ordinary text, but if the\n\
28e969dd 3228buffer's lines are very long (say, more than 500 characters), these\n\
f0c5b712
JB
3229motion functions will take longer to execute. Emacs may also take\n\
3230longer to update the display.\n\
28e969dd 3231\n\
f0c5b712
JB
3232If cache-long-line-scans is non-nil, these motion functions cache the\n\
3233results of their scans, and consult the cache to avoid rescanning\n\
3234regions of the buffer until the text is modified. The caches are most\n\
3235beneficial when they prevent the most searching---that is, when the\n\
3236buffer contains long lines and large regions of characters with the\n\
3237same, fixed screen width.\n\
28e969dd 3238\n\
f0c5b712
JB
3239When cache-long-line-scans is non-nil, processing short lines will\n\
3240become slightly slower (because of the overhead of consulting the\n\
3241cache), and the caches will use memory roughly proportional to the\n\
3242number of newlines and characters whose screen width varies.\n\
3243\n\
3244The caches require no explicit maintenance; their accuracy is\n\
3245maintained internally by the Emacs primitives. Enabling or disabling\n\
3246the cache should not affect the behavior of any of the motion\n\
3247functions; it should only affect their performance.");
28e969dd 3248
943e065b
RS
3249 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
3250 "Value of point before the last series of scroll operations, or nil.");
3251
3cb719bd
RS
3252 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
3253 &current_buffer->invisibility_spec, Qnil,
3254 "Invisibility spec of this buffer.\n\
3255The default is t, which means that text is invisible\n\
3256if it has a non-nil `invisible' property.\n\
3257If the value is a list, a text character is invisible if its `invisible'\n\
3258property is an element in that list.\n\
3259If an element is a cons cell of the for (PROP . ELLIPSIS),\n\
3260then characters with property value PROP is invisible,\n\
3261and they have an ellipsis as well if ELLIPSIS is non-nil.");
3262
c48f61ef
RS
3263 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
3264 "*Non-nil means deactivate the mark when the buffer contents change.");
3265 Vtransient_mark_mode = Qnil;
3266
0a4469c9 3267 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
a96b68f1
RS
3268 "*Non-nil means disregard read-only status of buffers or characters.\n\
3269If the value is t, disregard `buffer-read-only' and all `read-only'\n\
3270text properties. If the value is a list, disregard `buffer-read-only'\n\
3271and disregard a `read-only' text property if the property value\n\
3272is a member of the list.");
3273 Vinhibit_read_only = Qnil;
3274
dcdffbf6
RS
3275 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
3276 "List of functions called with no args to query before killing a buffer.");
3277 Vkill_buffer_query_functions = Qnil;
3278
1ab256cb
RM
3279 defsubr (&Sbuffer_list);
3280 defsubr (&Sget_buffer);
3281 defsubr (&Sget_file_buffer);
3282 defsubr (&Sget_buffer_create);
336cd056 3283 defsubr (&Smake_indirect_buffer);
01050cb5 3284 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
3285 defsubr (&Sbuffer_name);
3286/*defsubr (&Sbuffer_number);*/
3287 defsubr (&Sbuffer_file_name);
336cd056 3288 defsubr (&Sbuffer_base_buffer);
1ab256cb
RM
3289 defsubr (&Sbuffer_local_variables);
3290 defsubr (&Sbuffer_modified_p);
3291 defsubr (&Sset_buffer_modified_p);
3292 defsubr (&Sbuffer_modified_tick);
3293 defsubr (&Srename_buffer);
3294 defsubr (&Sother_buffer);
3295 defsubr (&Sbuffer_disable_undo);
3296 defsubr (&Sbuffer_enable_undo);
3297 defsubr (&Skill_buffer);
3298 defsubr (&Serase_buffer);
a9ee7a59 3299 defsubr (&Sset_buffer_major_mode);
1ab256cb
RM
3300 defsubr (&Sswitch_to_buffer);
3301 defsubr (&Spop_to_buffer);
3302 defsubr (&Scurrent_buffer);
3303 defsubr (&Sset_buffer);
3304 defsubr (&Sbarf_if_buffer_read_only);
3305 defsubr (&Sbury_buffer);
1ab256cb 3306 defsubr (&Skill_all_local_variables);
2eec3b4e 3307
52f8ec73 3308 defsubr (&Soverlayp);
2eec3b4e
RS
3309 defsubr (&Smake_overlay);
3310 defsubr (&Sdelete_overlay);
3311 defsubr (&Smove_overlay);
8ebafa8d
JB
3312 defsubr (&Soverlay_start);
3313 defsubr (&Soverlay_end);
3314 defsubr (&Soverlay_buffer);
3315 defsubr (&Soverlay_properties);
2eec3b4e
RS
3316 defsubr (&Soverlays_at);
3317 defsubr (&Snext_overlay_change);
239c932b 3318 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
3319 defsubr (&Soverlay_recenter);
3320 defsubr (&Soverlay_lists);
3321 defsubr (&Soverlay_get);
3322 defsubr (&Soverlay_put);
1ab256cb
RM
3323}
3324
3325keys_of_buffer ()
3326{
3327 initial_define_key (control_x_map, 'b', "switch-to-buffer");
3328 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
3329
3330 /* This must not be in syms_of_buffer, because Qdisabled is not
3331 initialized when that function gets called. */
3332 Fput (intern ("erase-buffer"), Qdisabled, Qt);
1ab256cb 3333}