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