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