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