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