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