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