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