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