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