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