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