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