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