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