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