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