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