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