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