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