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