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