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