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