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