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