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