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