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