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