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