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