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