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