* bitmaps/README:
[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 if (current_buffer->text->intervals)
2273 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2274 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2275 if (other_buffer->text->intervals)
2276 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2277 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2278
2279 return Qnil;
2280 }
2281
2282 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
2283 1, 1, 0,
2284 doc: /* Set the multibyte flag of the current buffer to FLAG.
2285 If FLAG is t, this makes the buffer a multibyte buffer.
2286 If FLAG is nil, this makes the buffer a single-byte buffer.
2287 In these cases, the buffer contents remain unchanged as a sequence of
2288 bytes but the contents viewed as characters do change.
2289 If FLAG is `to', this makes the buffer a multibyte buffer by changing
2290 all eight-bit bytes to eight-bit characters.
2291 If the multibyte flag was really changed, undo information of the
2292 current buffer is cleared. */)
2293 (flag)
2294 Lisp_Object flag;
2295 {
2296 struct Lisp_Marker *tail, *markers;
2297 struct buffer *other;
2298 int begv, zv;
2299 int narrowed = (BEG != BEGV || Z != ZV);
2300 int modified_p = !NILP (Fbuffer_modified_p (Qnil));
2301 Lisp_Object old_undo = current_buffer->undo_list;
2302 struct gcpro gcpro1;
2303
2304 if (current_buffer->base_buffer)
2305 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2306
2307 /* Do nothing if nothing actually changes. */
2308 if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
2309 return flag;
2310
2311 GCPRO1 (old_undo);
2312
2313 /* Don't record these buffer changes. We will put a special undo entry
2314 instead. */
2315 current_buffer->undo_list = Qt;
2316
2317 /* If the cached position is for this buffer, clear it out. */
2318 clear_charpos_cache (current_buffer);
2319
2320 if (NILP (flag))
2321 begv = BEGV_BYTE, zv = ZV_BYTE;
2322 else
2323 begv = BEGV, zv = ZV;
2324
2325 if (narrowed)
2326 Fwiden ();
2327
2328 if (NILP (flag))
2329 {
2330 int pos, stop;
2331 unsigned char *p;
2332
2333 /* Do this first, so it can use CHAR_TO_BYTE
2334 to calculate the old correspondences. */
2335 set_intervals_multibyte (0);
2336
2337 current_buffer->enable_multibyte_characters = Qnil;
2338
2339 Z = Z_BYTE;
2340 BEGV = BEGV_BYTE;
2341 ZV = ZV_BYTE;
2342 GPT = GPT_BYTE;
2343 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2344
2345
2346 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2347 tail->charpos = tail->bytepos;
2348
2349 /* Convert multibyte form of 8-bit characters to unibyte. */
2350 pos = BEG;
2351 stop = GPT;
2352 p = BEG_ADDR;
2353 while (1)
2354 {
2355 int c, bytes;
2356
2357 if (pos == stop)
2358 {
2359 if (pos == Z)
2360 break;
2361 p = GAP_END_ADDR;
2362 stop = Z;
2363 }
2364 if (ASCII_BYTE_P (*p))
2365 p++, pos++;
2366 else if (CHAR_BYTE8_HEAD_P (*p))
2367 {
2368 c = STRING_CHAR_AND_LENGTH (p, stop - pos, bytes);
2369 /* Delete all bytes for this 8-bit character but the
2370 last one, and change the last one to the charcter
2371 code. */
2372 bytes--;
2373 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2374 p = GAP_END_ADDR;
2375 *p++ = c;
2376 pos++;
2377 if (begv > pos)
2378 begv -= bytes;
2379 if (zv > pos)
2380 zv -= bytes;
2381 stop = Z;
2382 }
2383 else
2384 {
2385 bytes = BYTES_BY_CHAR_HEAD (*p);
2386 p += bytes, pos += bytes;
2387 }
2388 }
2389 if (narrowed)
2390 Fnarrow_to_region (make_number (begv), make_number (zv));
2391 }
2392 else
2393 {
2394 int pt = PT;
2395 int pos, stop;
2396 unsigned char *p, *pend;
2397
2398 /* Be sure not to have a multibyte sequence striding over the GAP.
2399 Ex: We change this: "...abc\302 _GAP_ \241def..."
2400 to: "...abc _GAP_ \302\241def..." */
2401
2402 if (EQ (flag, Qt)
2403 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
2404 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2405 {
2406 unsigned char *p = GPT_ADDR - 1;
2407
2408 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
2409 if (BASE_LEADING_CODE_P (*p))
2410 {
2411 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
2412
2413 move_gap_both (new_gpt, new_gpt);
2414 }
2415 }
2416
2417 /* Make the buffer contents valid as multibyte by converting
2418 8-bit characters to multibyte form. */
2419 pos = BEG;
2420 stop = GPT;
2421 p = BEG_ADDR;
2422 pend = GPT_ADDR;
2423 while (1)
2424 {
2425 int bytes;
2426
2427 if (pos == stop)
2428 {
2429 if (pos == Z)
2430 break;
2431 p = GAP_END_ADDR;
2432 pend = Z_ADDR;
2433 stop = Z;
2434 }
2435
2436 if (ASCII_BYTE_P (*p))
2437 p++, pos++;
2438 else if (EQ (flag, Qt) && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
2439 p += bytes, pos += bytes;
2440 else
2441 {
2442 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
2443 int c;
2444
2445 c = BYTE8_TO_CHAR (*p);
2446 bytes = CHAR_STRING (c, tmp);
2447 *p = tmp[0];
2448 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2449 bytes--;
2450 insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
2451 /* Now the gap is after the just inserted data. */
2452 pos = GPT;
2453 p = GAP_END_ADDR;
2454 if (pos <= begv)
2455 begv += bytes;
2456 if (pos <= zv)
2457 zv += bytes;
2458 if (pos <= pt)
2459 pt += bytes;
2460 pend = Z_ADDR;
2461 stop = Z;
2462 }
2463 }
2464
2465 if (pt != PT)
2466 TEMP_SET_PT (pt);
2467
2468 if (narrowed)
2469 Fnarrow_to_region (make_number (begv), make_number (zv));
2470
2471 /* Do this first, so that chars_in_text asks the right question.
2472 set_intervals_multibyte needs it too. */
2473 current_buffer->enable_multibyte_characters = Qt;
2474
2475 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
2476 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
2477
2478 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
2479
2480 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
2481 if (BEGV_BYTE > GPT_BYTE)
2482 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
2483 else
2484 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
2485
2486 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
2487 if (ZV_BYTE > GPT_BYTE)
2488 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
2489 else
2490 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
2491
2492 {
2493 int pt_byte = advance_to_char_boundary (PT_BYTE);
2494 int pt;
2495
2496 if (pt_byte > GPT_BYTE)
2497 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
2498 else
2499 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
2500 TEMP_SET_PT_BOTH (pt, pt_byte);
2501 }
2502
2503 tail = markers = BUF_MARKERS (current_buffer);
2504
2505 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2506 getting confused by the markers that have not yet been updated.
2507 It is also a signal that it should never create a marker. */
2508 BUF_MARKERS (current_buffer) = NULL;
2509
2510 for (; tail; tail = tail->next)
2511 {
2512 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2513 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
2514 }
2515
2516 /* Make sure no markers were put on the chain
2517 while the chain value was incorrect. */
2518 if (BUF_MARKERS (current_buffer))
2519 abort ();
2520
2521 BUF_MARKERS (current_buffer) = markers;
2522
2523 /* Do this last, so it can calculate the new correspondences
2524 between chars and bytes. */
2525 set_intervals_multibyte (1);
2526 }
2527
2528 if (!EQ (old_undo, Qt))
2529 {
2530 /* Represent all the above changes by a special undo entry. */
2531 extern Lisp_Object Qapply;
2532 current_buffer->undo_list = Fcons (list3 (Qapply,
2533 intern ("set-buffer-multibyte"),
2534 NILP (flag) ? Qt : Qnil),
2535 old_undo);
2536 }
2537
2538 UNGCPRO;
2539
2540 /* Changing the multibyteness of a buffer means that all windows
2541 showing that buffer must be updated thoroughly. */
2542 current_buffer->prevent_redisplay_optimizations_p = 1;
2543 ++windows_or_buffers_changed;
2544
2545 /* Copy this buffer's new multibyte status
2546 into all of its indirect buffers. */
2547 for (other = all_buffers; other; other = other->next)
2548 if (other->base_buffer == current_buffer && !NILP (other->name))
2549 {
2550 other->enable_multibyte_characters
2551 = current_buffer->enable_multibyte_characters;
2552 other->prevent_redisplay_optimizations_p = 1;
2553 }
2554
2555 /* Restore the modifiedness of the buffer. */
2556 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2557 Fset_buffer_modified_p (Qnil);
2558
2559 #ifdef subprocesses
2560 /* Update coding systems of this buffer's process (if any). */
2561 {
2562 Lisp_Object process;
2563
2564 process = Fget_buffer_process (Fcurrent_buffer ());
2565 if (PROCESSP (process))
2566 setup_process_coding_systems (process);
2567 }
2568 #endif /* subprocesses */
2569
2570 return flag;
2571 }
2572 \f
2573 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
2574 0, 0, 0,
2575 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
2576 Most local variable bindings are eliminated so that the default values
2577 become effective once more. Also, the syntax table is set from
2578 `standard-syntax-table', the local keymap is set to nil,
2579 and the abbrev table from `fundamental-mode-abbrev-table'.
2580 This function also forces redisplay of the mode line.
2581
2582 Every function to select a new major mode starts by
2583 calling this function.
2584
2585 As a special exception, local variables whose names have
2586 a non-nil `permanent-local' property are not eliminated by this function.
2587
2588 The first thing this function does is run
2589 the normal hook `change-major-mode-hook'. */)
2590 ()
2591 {
2592 if (!NILP (Vrun_hooks))
2593 call1 (Vrun_hooks, Qchange_major_mode_hook);
2594
2595 /* Make sure none of the bindings in local_var_alist
2596 remain swapped in, in their symbols. */
2597
2598 swap_out_buffer_local_variables (current_buffer);
2599
2600 /* Actually eliminate all local bindings of this buffer. */
2601
2602 reset_buffer_local_variables (current_buffer, 0);
2603
2604 /* Force mode-line redisplay. Useful here because all major mode
2605 commands call this function. */
2606 update_mode_lines++;
2607
2608 return Qnil;
2609 }
2610
2611 /* Make sure no local variables remain set up with buffer B
2612 for their current values. */
2613
2614 static void
2615 swap_out_buffer_local_variables (b)
2616 struct buffer *b;
2617 {
2618 Lisp_Object oalist, alist, sym, tem, buffer;
2619
2620 XSETBUFFER (buffer, b);
2621 oalist = b->local_var_alist;
2622
2623 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2624 {
2625 sym = XCAR (XCAR (alist));
2626
2627 /* Need not do anything if some other buffer's binding is now encached. */
2628 tem = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (sym))->buffer;
2629 if (EQ (tem, buffer))
2630 {
2631 /* Symbol is set up for this buffer's old local value:
2632 swap it out! */
2633 swap_in_global_binding (sym);
2634 }
2635 }
2636 }
2637 \f
2638 /* Find all the overlays in the current buffer that contain position POS.
2639 Return the number found, and store them in a vector in *VEC_PTR.
2640 Store in *LEN_PTR the size allocated for the vector.
2641 Store in *NEXT_PTR the next position after POS where an overlay starts,
2642 or ZV if there are no more overlays between POS and ZV.
2643 Store in *PREV_PTR the previous position before POS where an overlay ends,
2644 or where an overlay starts which ends at or after POS;
2645 or BEGV if there are no such overlays from BEGV to POS.
2646 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2647
2648 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2649 when this function is called.
2650
2651 If EXTEND is non-zero, we make the vector bigger if necessary.
2652 If EXTEND is zero, we never extend the vector,
2653 and we store only as many overlays as will fit.
2654 But we still return the total number of overlays.
2655
2656 If CHANGE_REQ is true, then any position written into *PREV_PTR or
2657 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2658 default (BEGV or ZV). */
2659
2660 int
2661 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr, change_req)
2662 EMACS_INT pos;
2663 int extend;
2664 Lisp_Object **vec_ptr;
2665 int *len_ptr;
2666 EMACS_INT *next_ptr;
2667 EMACS_INT *prev_ptr;
2668 int change_req;
2669 {
2670 Lisp_Object overlay, start, end;
2671 struct Lisp_Overlay *tail;
2672 int idx = 0;
2673 int len = *len_ptr;
2674 Lisp_Object *vec = *vec_ptr;
2675 int next = ZV;
2676 int prev = BEGV;
2677 int inhibit_storing = 0;
2678
2679 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2680 {
2681 int startpos, endpos;
2682
2683 XSETMISC (overlay, tail);
2684
2685 start = OVERLAY_START (overlay);
2686 end = OVERLAY_END (overlay);
2687 endpos = OVERLAY_POSITION (end);
2688 if (endpos < pos)
2689 {
2690 if (prev < endpos)
2691 prev = endpos;
2692 break;
2693 }
2694 startpos = OVERLAY_POSITION (start);
2695 /* This one ends at or after POS
2696 so its start counts for PREV_PTR if it's before POS. */
2697 if (prev < startpos && startpos < pos)
2698 prev = startpos;
2699 if (endpos == pos)
2700 continue;
2701 if (startpos <= pos)
2702 {
2703 if (idx == len)
2704 {
2705 /* The supplied vector is full.
2706 Either make it bigger, or don't store any more in it. */
2707 if (extend)
2708 {
2709 /* Make it work with an initial len == 0. */
2710 len *= 2;
2711 if (len == 0)
2712 len = 4;
2713 *len_ptr = len;
2714 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2715 *vec_ptr = vec;
2716 }
2717 else
2718 inhibit_storing = 1;
2719 }
2720
2721 if (!inhibit_storing)
2722 vec[idx] = overlay;
2723 /* Keep counting overlays even if we can't return them all. */
2724 idx++;
2725 }
2726 else if (startpos < next)
2727 next = startpos;
2728 }
2729
2730 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2731 {
2732 int startpos, endpos;
2733
2734 XSETMISC (overlay, tail);
2735
2736 start = OVERLAY_START (overlay);
2737 end = OVERLAY_END (overlay);
2738 startpos = OVERLAY_POSITION (start);
2739 if (pos < startpos)
2740 {
2741 if (startpos < next)
2742 next = startpos;
2743 break;
2744 }
2745 endpos = OVERLAY_POSITION (end);
2746 if (pos < endpos)
2747 {
2748 if (idx == len)
2749 {
2750 if (extend)
2751 {
2752 /* Make it work with an initial len == 0. */
2753 len *= 2;
2754 if (len == 0)
2755 len = 4;
2756 *len_ptr = len;
2757 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2758 *vec_ptr = vec;
2759 }
2760 else
2761 inhibit_storing = 1;
2762 }
2763
2764 if (!inhibit_storing)
2765 vec[idx] = overlay;
2766 idx++;
2767
2768 if (startpos < pos && startpos > prev)
2769 prev = startpos;
2770 }
2771 else if (endpos < pos && endpos > prev)
2772 prev = endpos;
2773 else if (endpos == pos && startpos > prev
2774 && (!change_req || startpos < pos))
2775 prev = startpos;
2776 }
2777
2778 if (next_ptr)
2779 *next_ptr = next;
2780 if (prev_ptr)
2781 *prev_ptr = prev;
2782 return idx;
2783 }
2784 \f
2785 /* Find all the overlays in the current buffer that overlap the range
2786 BEG-END, or are empty at BEG, or are empty at END provided END
2787 denotes the position at the end of the current buffer.
2788
2789 Return the number found, and store them in a vector in *VEC_PTR.
2790 Store in *LEN_PTR the size allocated for the vector.
2791 Store in *NEXT_PTR the next position after POS where an overlay starts,
2792 or ZV if there are no more overlays.
2793 Store in *PREV_PTR the previous position before POS where an overlay ends,
2794 or BEGV if there are no previous overlays.
2795 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2796
2797 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2798 when this function is called.
2799
2800 If EXTEND is non-zero, we make the vector bigger if necessary.
2801 If EXTEND is zero, we never extend the vector,
2802 and we store only as many overlays as will fit.
2803 But we still return the total number of overlays. */
2804
2805 static int
2806 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2807 int beg, end;
2808 int extend;
2809 Lisp_Object **vec_ptr;
2810 int *len_ptr;
2811 int *next_ptr;
2812 int *prev_ptr;
2813 {
2814 Lisp_Object overlay, ostart, oend;
2815 struct Lisp_Overlay *tail;
2816 int idx = 0;
2817 int len = *len_ptr;
2818 Lisp_Object *vec = *vec_ptr;
2819 int next = ZV;
2820 int prev = BEGV;
2821 int inhibit_storing = 0;
2822 int end_is_Z = end == Z;
2823
2824 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2825 {
2826 int startpos, endpos;
2827
2828 XSETMISC (overlay, tail);
2829
2830 ostart = OVERLAY_START (overlay);
2831 oend = OVERLAY_END (overlay);
2832 endpos = OVERLAY_POSITION (oend);
2833 if (endpos < beg)
2834 {
2835 if (prev < endpos)
2836 prev = endpos;
2837 break;
2838 }
2839 startpos = OVERLAY_POSITION (ostart);
2840 /* Count an interval if it overlaps the range, is empty at the
2841 start of the range, or is empty at END provided END denotes the
2842 end of the buffer. */
2843 if ((beg < endpos && startpos < end)
2844 || (startpos == endpos
2845 && (beg == endpos || (end_is_Z && endpos == end))))
2846 {
2847 if (idx == len)
2848 {
2849 /* The supplied vector is full.
2850 Either make it bigger, or don't store any more in it. */
2851 if (extend)
2852 {
2853 /* Make it work with an initial len == 0. */
2854 len *= 2;
2855 if (len == 0)
2856 len = 4;
2857 *len_ptr = len;
2858 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2859 *vec_ptr = vec;
2860 }
2861 else
2862 inhibit_storing = 1;
2863 }
2864
2865 if (!inhibit_storing)
2866 vec[idx] = overlay;
2867 /* Keep counting overlays even if we can't return them all. */
2868 idx++;
2869 }
2870 else if (startpos < next)
2871 next = startpos;
2872 }
2873
2874 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2875 {
2876 int startpos, endpos;
2877
2878 XSETMISC (overlay, tail);
2879
2880 ostart = OVERLAY_START (overlay);
2881 oend = OVERLAY_END (overlay);
2882 startpos = OVERLAY_POSITION (ostart);
2883 if (end < startpos)
2884 {
2885 if (startpos < next)
2886 next = startpos;
2887 break;
2888 }
2889 endpos = OVERLAY_POSITION (oend);
2890 /* Count an interval if it overlaps the range, is empty at the
2891 start of the range, or is empty at END provided END denotes the
2892 end of the buffer. */
2893 if ((beg < endpos && startpos < end)
2894 || (startpos == endpos
2895 && (beg == endpos || (end_is_Z && endpos == end))))
2896 {
2897 if (idx == len)
2898 {
2899 if (extend)
2900 {
2901 /* Make it work with an initial len == 0. */
2902 len *= 2;
2903 if (len == 0)
2904 len = 4;
2905 *len_ptr = len;
2906 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2907 *vec_ptr = vec;
2908 }
2909 else
2910 inhibit_storing = 1;
2911 }
2912
2913 if (!inhibit_storing)
2914 vec[idx] = overlay;
2915 idx++;
2916 }
2917 else if (endpos < beg && endpos > prev)
2918 prev = endpos;
2919 }
2920
2921 if (next_ptr)
2922 *next_ptr = next;
2923 if (prev_ptr)
2924 *prev_ptr = prev;
2925 return idx;
2926 }
2927
2928
2929 /* Return non-zero if there exists an overlay with a non-nil
2930 `mouse-face' property overlapping OVERLAY. */
2931
2932 int
2933 mouse_face_overlay_overlaps (overlay)
2934 Lisp_Object overlay;
2935 {
2936 int start = OVERLAY_POSITION (OVERLAY_START (overlay));
2937 int end = OVERLAY_POSITION (OVERLAY_END (overlay));
2938 int n, i, size;
2939 Lisp_Object *v, tem;
2940
2941 size = 10;
2942 v = (Lisp_Object *) alloca (size * sizeof *v);
2943 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
2944 if (n > size)
2945 {
2946 v = (Lisp_Object *) alloca (n * sizeof *v);
2947 overlays_in (start, end, 0, &v, &n, NULL, NULL);
2948 }
2949
2950 for (i = 0; i < n; ++i)
2951 if (!EQ (v[i], overlay)
2952 && (tem = Foverlay_get (overlay, Qmouse_face),
2953 !NILP (tem)))
2954 break;
2955
2956 return i < n;
2957 }
2958
2959
2960 \f
2961 /* Fast function to just test if we're at an overlay boundary. */
2962 int
2963 overlay_touches_p (pos)
2964 int pos;
2965 {
2966 Lisp_Object overlay;
2967 struct Lisp_Overlay *tail;
2968
2969 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2970 {
2971 int endpos;
2972
2973 XSETMISC (overlay ,tail);
2974 if (!OVERLAYP (overlay))
2975 abort ();
2976
2977 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2978 if (endpos < pos)
2979 break;
2980 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2981 return 1;
2982 }
2983
2984 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
2985 {
2986 int startpos;
2987
2988 XSETMISC (overlay, tail);
2989 if (!OVERLAYP (overlay))
2990 abort ();
2991
2992 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2993 if (pos < startpos)
2994 break;
2995 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2996 return 1;
2997 }
2998 return 0;
2999 }
3000 \f
3001 struct sortvec
3002 {
3003 Lisp_Object overlay;
3004 int beg, end;
3005 int priority;
3006 };
3007
3008 static int
3009 compare_overlays (v1, v2)
3010 const void *v1, *v2;
3011 {
3012 const struct sortvec *s1 = (const struct sortvec *) v1;
3013 const struct sortvec *s2 = (const struct sortvec *) v2;
3014 if (s1->priority != s2->priority)
3015 return s1->priority - s2->priority;
3016 if (s1->beg != s2->beg)
3017 return s1->beg - s2->beg;
3018 if (s1->end != s2->end)
3019 return s2->end - s1->end;
3020 return 0;
3021 }
3022
3023 /* Sort an array of overlays by priority. The array is modified in place.
3024 The return value is the new size; this may be smaller than the original
3025 size if some of the overlays were invalid or were window-specific. */
3026 int
3027 sort_overlays (overlay_vec, noverlays, w)
3028 Lisp_Object *overlay_vec;
3029 int noverlays;
3030 struct window *w;
3031 {
3032 int i, j;
3033 struct sortvec *sortvec;
3034 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
3035
3036 /* Put the valid and relevant overlays into sortvec. */
3037
3038 for (i = 0, j = 0; i < noverlays; i++)
3039 {
3040 Lisp_Object tem;
3041 Lisp_Object overlay;
3042
3043 overlay = overlay_vec[i];
3044 if (OVERLAY_VALID (overlay)
3045 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3046 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3047 {
3048 /* If we're interested in a specific window, then ignore
3049 overlays that are limited to some other window. */
3050 if (w)
3051 {
3052 Lisp_Object window;
3053
3054 window = Foverlay_get (overlay, Qwindow);
3055 if (WINDOWP (window) && XWINDOW (window) != w)
3056 continue;
3057 }
3058
3059 /* This overlay is good and counts: put it into sortvec. */
3060 sortvec[j].overlay = overlay;
3061 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3062 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3063 tem = Foverlay_get (overlay, Qpriority);
3064 if (INTEGERP (tem))
3065 sortvec[j].priority = XINT (tem);
3066 else
3067 sortvec[j].priority = 0;
3068 j++;
3069 }
3070 }
3071 noverlays = j;
3072
3073 /* Sort the overlays into the proper order: increasing priority. */
3074
3075 if (noverlays > 1)
3076 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3077
3078 for (i = 0; i < noverlays; i++)
3079 overlay_vec[i] = sortvec[i].overlay;
3080 return (noverlays);
3081 }
3082 \f
3083 struct sortstr
3084 {
3085 Lisp_Object string, string2;
3086 int size;
3087 int priority;
3088 };
3089
3090 struct sortstrlist
3091 {
3092 struct sortstr *buf; /* An array that expands as needed; never freed. */
3093 int size; /* Allocated length of that array. */
3094 int used; /* How much of the array is currently in use. */
3095 int bytes; /* Total length of the strings in buf. */
3096 };
3097
3098 /* Buffers for storing information about the overlays touching a given
3099 position. These could be automatic variables in overlay_strings, but
3100 it's more efficient to hold onto the memory instead of repeatedly
3101 allocating and freeing it. */
3102 static struct sortstrlist overlay_heads, overlay_tails;
3103 static unsigned char *overlay_str_buf;
3104
3105 /* Allocated length of overlay_str_buf. */
3106 static int overlay_str_len;
3107
3108 /* A comparison function suitable for passing to qsort. */
3109 static int
3110 cmp_for_strings (as1, as2)
3111 char *as1, *as2;
3112 {
3113 struct sortstr *s1 = (struct sortstr *)as1;
3114 struct sortstr *s2 = (struct sortstr *)as2;
3115 if (s1->size != s2->size)
3116 return s2->size - s1->size;
3117 if (s1->priority != s2->priority)
3118 return s1->priority - s2->priority;
3119 return 0;
3120 }
3121
3122 static void
3123 record_overlay_string (ssl, str, str2, pri, size)
3124 struct sortstrlist *ssl;
3125 Lisp_Object str, str2, pri;
3126 int size;
3127 {
3128 int nbytes;
3129
3130 if (ssl->used == ssl->size)
3131 {
3132 if (ssl->buf)
3133 ssl->size *= 2;
3134 else
3135 ssl->size = 5;
3136 ssl->buf = ((struct sortstr *)
3137 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
3138 }
3139 ssl->buf[ssl->used].string = str;
3140 ssl->buf[ssl->used].string2 = str2;
3141 ssl->buf[ssl->used].size = size;
3142 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3143 ssl->used++;
3144
3145 if (NILP (current_buffer->enable_multibyte_characters))
3146 nbytes = SCHARS (str);
3147 else if (! STRING_MULTIBYTE (str))
3148 nbytes = count_size_as_multibyte (SDATA (str),
3149 SBYTES (str));
3150 else
3151 nbytes = SBYTES (str);
3152
3153 ssl->bytes += nbytes;
3154
3155 if (STRINGP (str2))
3156 {
3157 if (NILP (current_buffer->enable_multibyte_characters))
3158 nbytes = SCHARS (str2);
3159 else if (! STRING_MULTIBYTE (str2))
3160 nbytes = count_size_as_multibyte (SDATA (str2),
3161 SBYTES (str2));
3162 else
3163 nbytes = SBYTES (str2);
3164
3165 ssl->bytes += nbytes;
3166 }
3167 }
3168
3169 /* Return the concatenation of the strings associated with overlays that
3170 begin or end at POS, ignoring overlays that are specific to a window
3171 other than W. The strings are concatenated in the appropriate order:
3172 shorter overlays nest inside longer ones, and higher priority inside
3173 lower. Normally all of the after-strings come first, but zero-sized
3174 overlays have their after-strings ride along with the before-strings
3175 because it would look strange to print them inside-out.
3176
3177 Returns the string length, and stores the contents indirectly through
3178 PSTR, if that variable is non-null. The string may be overwritten by
3179 subsequent calls. */
3180
3181 int
3182 overlay_strings (pos, w, pstr)
3183 EMACS_INT pos;
3184 struct window *w;
3185 unsigned char **pstr;
3186 {
3187 Lisp_Object overlay, window, str;
3188 struct Lisp_Overlay *ov;
3189 int startpos, endpos;
3190 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3191
3192 overlay_heads.used = overlay_heads.bytes = 0;
3193 overlay_tails.used = overlay_tails.bytes = 0;
3194 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
3195 {
3196 XSETMISC (overlay, ov);
3197 eassert (OVERLAYP (overlay));
3198
3199 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3200 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3201 if (endpos < pos)
3202 break;
3203 if (endpos != pos && startpos != pos)
3204 continue;
3205 window = Foverlay_get (overlay, Qwindow);
3206 if (WINDOWP (window) && XWINDOW (window) != w)
3207 continue;
3208 if (startpos == pos
3209 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3210 record_overlay_string (&overlay_heads, str,
3211 (startpos == endpos
3212 ? Foverlay_get (overlay, Qafter_string)
3213 : Qnil),
3214 Foverlay_get (overlay, Qpriority),
3215 endpos - startpos);
3216 else if (endpos == pos
3217 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3218 record_overlay_string (&overlay_tails, str, Qnil,
3219 Foverlay_get (overlay, Qpriority),
3220 endpos - startpos);
3221 }
3222 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
3223 {
3224 XSETMISC (overlay, ov);
3225 eassert (OVERLAYP (overlay));
3226
3227 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3228 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3229 if (startpos > pos)
3230 break;
3231 if (endpos != pos && startpos != pos)
3232 continue;
3233 window = Foverlay_get (overlay, Qwindow);
3234 if (WINDOWP (window) && XWINDOW (window) != w)
3235 continue;
3236 if (startpos == pos
3237 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3238 record_overlay_string (&overlay_heads, str,
3239 (startpos == endpos
3240 ? Foverlay_get (overlay, Qafter_string)
3241 : Qnil),
3242 Foverlay_get (overlay, Qpriority),
3243 endpos - startpos);
3244 else if (endpos == pos
3245 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3246 record_overlay_string (&overlay_tails, str, Qnil,
3247 Foverlay_get (overlay, Qpriority),
3248 endpos - startpos);
3249 }
3250 if (overlay_tails.used > 1)
3251 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3252 cmp_for_strings);
3253 if (overlay_heads.used > 1)
3254 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3255 cmp_for_strings);
3256 if (overlay_heads.bytes || overlay_tails.bytes)
3257 {
3258 Lisp_Object tem;
3259 int i;
3260 unsigned char *p;
3261 int total = overlay_heads.bytes + overlay_tails.bytes;
3262
3263 if (total > overlay_str_len)
3264 {
3265 overlay_str_len = total;
3266 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
3267 total);
3268 }
3269 p = overlay_str_buf;
3270 for (i = overlay_tails.used; --i >= 0;)
3271 {
3272 int nbytes;
3273 tem = overlay_tails.buf[i].string;
3274 nbytes = copy_text (SDATA (tem), p,
3275 SBYTES (tem),
3276 STRING_MULTIBYTE (tem), multibyte);
3277 p += nbytes;
3278 }
3279 for (i = 0; i < overlay_heads.used; ++i)
3280 {
3281 int nbytes;
3282 tem = overlay_heads.buf[i].string;
3283 nbytes = copy_text (SDATA (tem), p,
3284 SBYTES (tem),
3285 STRING_MULTIBYTE (tem), multibyte);
3286 p += nbytes;
3287 tem = overlay_heads.buf[i].string2;
3288 if (STRINGP (tem))
3289 {
3290 nbytes = copy_text (SDATA (tem), p,
3291 SBYTES (tem),
3292 STRING_MULTIBYTE (tem), multibyte);
3293 p += nbytes;
3294 }
3295 }
3296 if (p != overlay_str_buf + total)
3297 abort ();
3298 if (pstr)
3299 *pstr = overlay_str_buf;
3300 return total;
3301 }
3302 return 0;
3303 }
3304 \f
3305 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
3306
3307 void
3308 recenter_overlay_lists (buf, pos)
3309 struct buffer *buf;
3310 EMACS_INT pos;
3311 {
3312 Lisp_Object overlay, beg, end;
3313 struct Lisp_Overlay *prev, *tail, *next;
3314
3315 /* See if anything in overlays_before should move to overlays_after. */
3316
3317 /* We don't strictly need prev in this loop; it should always be nil.
3318 But we use it for symmetry and in case that should cease to be true
3319 with some future change. */
3320 prev = NULL;
3321 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
3322 {
3323 next = tail->next;
3324 XSETMISC (overlay, tail);
3325
3326 /* If the overlay is not valid, get rid of it. */
3327 if (!OVERLAY_VALID (overlay))
3328 #if 1
3329 abort ();
3330 #else
3331 {
3332 /* Splice the cons cell TAIL out of overlays_before. */
3333 if (!NILP (prev))
3334 XCDR (prev) = next;
3335 else
3336 buf->overlays_before = next;
3337 tail = prev;
3338 continue;
3339 }
3340 #endif
3341
3342 beg = OVERLAY_START (overlay);
3343 end = OVERLAY_END (overlay);
3344
3345 if (OVERLAY_POSITION (end) > pos)
3346 {
3347 /* OVERLAY needs to be moved. */
3348 int where = OVERLAY_POSITION (beg);
3349 struct Lisp_Overlay *other, *other_prev;
3350
3351 /* Splice the cons cell TAIL out of overlays_before. */
3352 if (prev)
3353 prev->next = next;
3354 else
3355 buf->overlays_before = next;
3356
3357 /* Search thru overlays_after for where to put it. */
3358 other_prev = NULL;
3359 for (other = buf->overlays_after; other;
3360 other_prev = other, other = other->next)
3361 {
3362 Lisp_Object otherbeg, otheroverlay;
3363
3364 XSETMISC (otheroverlay, other);
3365 eassert (OVERLAY_VALID (otheroverlay));
3366
3367 otherbeg = OVERLAY_START (otheroverlay);
3368 if (OVERLAY_POSITION (otherbeg) >= where)
3369 break;
3370 }
3371
3372 /* Add TAIL to overlays_after before OTHER. */
3373 tail->next = other;
3374 if (other_prev)
3375 other_prev->next = tail;
3376 else
3377 buf->overlays_after = tail;
3378 tail = prev;
3379 }
3380 else
3381 /* We've reached the things that should stay in overlays_before.
3382 All the rest of overlays_before must end even earlier,
3383 so stop now. */
3384 break;
3385 }
3386
3387 /* See if anything in overlays_after should be in overlays_before. */
3388 prev = NULL;
3389 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
3390 {
3391 next = tail->next;
3392 XSETMISC (overlay, tail);
3393
3394 /* If the overlay is not valid, get rid of it. */
3395 if (!OVERLAY_VALID (overlay))
3396 #if 1
3397 abort ();
3398 #else
3399 {
3400 /* Splice the cons cell TAIL out of overlays_after. */
3401 if (!NILP (prev))
3402 XCDR (prev) = next;
3403 else
3404 buf->overlays_after = next;
3405 tail = prev;
3406 continue;
3407 }
3408 #endif
3409
3410 beg = OVERLAY_START (overlay);
3411 end = OVERLAY_END (overlay);
3412
3413 /* Stop looking, when we know that nothing further
3414 can possibly end before POS. */
3415 if (OVERLAY_POSITION (beg) > pos)
3416 break;
3417
3418 if (OVERLAY_POSITION (end) <= pos)
3419 {
3420 /* OVERLAY needs to be moved. */
3421 int where = OVERLAY_POSITION (end);
3422 struct Lisp_Overlay *other, *other_prev;
3423
3424 /* Splice the cons cell TAIL out of overlays_after. */
3425 if (prev)
3426 prev->next = next;
3427 else
3428 buf->overlays_after = next;
3429
3430 /* Search thru overlays_before for where to put it. */
3431 other_prev = NULL;
3432 for (other = buf->overlays_before; other;
3433 other_prev = other, other = other->next)
3434 {
3435 Lisp_Object otherend, otheroverlay;
3436
3437 XSETMISC (otheroverlay, other);
3438 eassert (OVERLAY_VALID (otheroverlay));
3439
3440 otherend = OVERLAY_END (otheroverlay);
3441 if (OVERLAY_POSITION (otherend) <= where)
3442 break;
3443 }
3444
3445 /* Add TAIL to overlays_before before OTHER. */
3446 tail->next = other;
3447 if (other_prev)
3448 other_prev->next = tail;
3449 else
3450 buf->overlays_before = tail;
3451 tail = prev;
3452 }
3453 }
3454
3455 buf->overlay_center = pos;
3456 }
3457
3458 void
3459 adjust_overlays_for_insert (pos, length)
3460 EMACS_INT pos;
3461 EMACS_INT length;
3462 {
3463 /* After an insertion, the lists are still sorted properly,
3464 but we may need to update the value of the overlay center. */
3465 if (current_buffer->overlay_center >= pos)
3466 current_buffer->overlay_center += length;
3467 }
3468
3469 void
3470 adjust_overlays_for_delete (pos, length)
3471 EMACS_INT pos;
3472 EMACS_INT length;
3473 {
3474 if (current_buffer->overlay_center < pos)
3475 /* The deletion was to our right. No change needed; the before- and
3476 after-lists are still consistent. */
3477 ;
3478 else if (current_buffer->overlay_center > pos + length)
3479 /* The deletion was to our left. We need to adjust the center value
3480 to account for the change in position, but the lists are consistent
3481 given the new value. */
3482 current_buffer->overlay_center -= length;
3483 else
3484 /* We're right in the middle. There might be things on the after-list
3485 that now belong on the before-list. Recentering will move them,
3486 and also update the center point. */
3487 recenter_overlay_lists (current_buffer, pos);
3488 }
3489
3490 /* Fix up overlays that were garbled as a result of permuting markers
3491 in the range START through END. Any overlay with at least one
3492 endpoint in this range will need to be unlinked from the overlay
3493 list and reinserted in its proper place.
3494 Such an overlay might even have negative size at this point.
3495 If so, we'll make the overlay empty. */
3496 void
3497 fix_start_end_in_overlays (start, end)
3498 register int start, end;
3499 {
3500 Lisp_Object overlay;
3501 struct Lisp_Overlay *before_list, *after_list;
3502 /* These are either nil, indicating that before_list or after_list
3503 should be assigned, or the cons cell the cdr of which should be
3504 assigned. */
3505 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
3506 /* 'Parent', likewise, indicates a cons cell or
3507 current_buffer->overlays_before or overlays_after, depending
3508 which loop we're in. */
3509 struct Lisp_Overlay *tail, *parent;
3510 int startpos, endpos;
3511
3512 /* This algorithm shifts links around instead of consing and GCing.
3513 The loop invariant is that before_list (resp. after_list) is a
3514 well-formed list except that its last element, the CDR of beforep
3515 (resp. afterp) if beforep (afterp) isn't nil or before_list
3516 (after_list) if it is, is still uninitialized. So it's not a bug
3517 that before_list isn't initialized, although it may look
3518 strange. */
3519 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
3520 {
3521 XSETMISC (overlay, tail);
3522
3523 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3524 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3525
3526 /* If the overlay is backwards, make it empty. */
3527 if (endpos < startpos)
3528 {
3529 startpos = endpos;
3530 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3531 Qnil);
3532 }
3533
3534 if (endpos < start)
3535 break;
3536
3537 if (endpos < end
3538 || (startpos >= start && startpos < end))
3539 {
3540 /* Add it to the end of the wrong list. Later on,
3541 recenter_overlay_lists will move it to the right place. */
3542 if (endpos < current_buffer->overlay_center)
3543 {
3544 if (!afterp)
3545 after_list = tail;
3546 else
3547 afterp->next = tail;
3548 afterp = tail;
3549 }
3550 else
3551 {
3552 if (!beforep)
3553 before_list = tail;
3554 else
3555 beforep->next = tail;
3556 beforep = tail;
3557 }
3558 if (!parent)
3559 current_buffer->overlays_before = tail->next;
3560 else
3561 parent->next = tail->next;
3562 tail = tail->next;
3563 }
3564 else
3565 parent = tail, tail = parent->next;
3566 }
3567 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
3568 {
3569 XSETMISC (overlay, tail);
3570
3571 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3572 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3573
3574 /* If the overlay is backwards, make it empty. */
3575 if (endpos < startpos)
3576 {
3577 startpos = endpos;
3578 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3579 Qnil);
3580 }
3581
3582 if (startpos >= end)
3583 break;
3584
3585 if (startpos >= start
3586 || (endpos >= start && endpos < end))
3587 {
3588 if (endpos < current_buffer->overlay_center)
3589 {
3590 if (!afterp)
3591 after_list = tail;
3592 else
3593 afterp->next = tail;
3594 afterp = tail;
3595 }
3596 else
3597 {
3598 if (!beforep)
3599 before_list = tail;
3600 else
3601 beforep->next = tail;
3602 beforep = tail;
3603 }
3604 if (!parent)
3605 current_buffer->overlays_after = tail->next;
3606 else
3607 parent->next = tail->next;
3608 tail = tail->next;
3609 }
3610 else
3611 parent = tail, tail = parent->next;
3612 }
3613
3614 /* Splice the constructed (wrong) lists into the buffer's lists,
3615 and let the recenter function make it sane again. */
3616 if (beforep)
3617 {
3618 beforep->next = current_buffer->overlays_before;
3619 current_buffer->overlays_before = before_list;
3620 }
3621 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3622
3623 if (afterp)
3624 {
3625 afterp->next = current_buffer->overlays_after;
3626 current_buffer->overlays_after = after_list;
3627 }
3628 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
3629 }
3630
3631 /* We have two types of overlay: the one whose ending marker is
3632 after-insertion-marker (this is the usual case) and the one whose
3633 ending marker is before-insertion-marker. When `overlays_before'
3634 contains overlays of the latter type and the former type in this
3635 order and both overlays end at inserting position, inserting a text
3636 increases only the ending marker of the latter type, which results
3637 in incorrect ordering of `overlays_before'.
3638
3639 This function fixes ordering of overlays in the slot
3640 `overlays_before' of the buffer *BP. Before the insertion, `point'
3641 was at PREV, and now is at POS. */
3642
3643 void
3644 fix_overlays_before (bp, prev, pos)
3645 struct buffer *bp;
3646 EMACS_INT prev, pos;
3647 {
3648 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
3649 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
3650 Lisp_Object tem;
3651 EMACS_INT end;
3652
3653 /* After the insertion, the several overlays may be in incorrect
3654 order. The possibility is that, in the list `overlays_before',
3655 an overlay which ends at POS appears after an overlay which ends
3656 at PREV. Since POS is greater than PREV, we must fix the
3657 ordering of these overlays, by moving overlays ends at POS before
3658 the overlays ends at PREV. */
3659
3660 /* At first, find a place where disordered overlays should be linked
3661 in. It is where an overlay which end before POS exists. (i.e. an
3662 overlay whose ending marker is after-insertion-marker if disorder
3663 exists). */
3664 while (tail
3665 && (XSETMISC (tem, tail),
3666 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
3667 {
3668 parent = tail;
3669 tail = tail->next;
3670 }
3671
3672 /* If we don't find such an overlay,
3673 or the found one ends before PREV,
3674 or the found one is the last one in the list,
3675 we don't have to fix anything. */
3676 if (!tail || end < prev || !tail->next)
3677 return;
3678
3679 right_pair = parent;
3680 parent = tail;
3681 tail = tail->next;
3682
3683 /* Now, end position of overlays in the list TAIL should be before
3684 or equal to PREV. In the loop, an overlay which ends at POS is
3685 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3686 we found an overlay which ends before PREV, the remaining
3687 overlays are in correct order. */
3688 while (tail)
3689 {
3690 XSETMISC (tem, tail);
3691 end = OVERLAY_POSITION (OVERLAY_END (tem));
3692
3693 if (end == pos)
3694 { /* This overlay is disordered. */
3695 struct Lisp_Overlay *found = tail;
3696
3697 /* Unlink the found overlay. */
3698 tail = found->next;
3699 parent->next = tail;
3700 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3701 and link it into the right place. */
3702 if (!right_pair)
3703 {
3704 found->next = bp->overlays_before;
3705 bp->overlays_before = found;
3706 }
3707 else
3708 {
3709 found->next = right_pair->next;
3710 right_pair->next = found;
3711 }
3712 }
3713 else if (end == prev)
3714 {
3715 parent = tail;
3716 tail = tail->next;
3717 }
3718 else /* No more disordered overlay. */
3719 break;
3720 }
3721 }
3722 \f
3723 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
3724 doc: /* Return t if OBJECT is an overlay. */)
3725 (object)
3726 Lisp_Object object;
3727 {
3728 return (OVERLAYP (object) ? Qt : Qnil);
3729 }
3730
3731 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
3732 doc: /* Create a new overlay with range BEG to END in BUFFER.
3733 If omitted, BUFFER defaults to the current buffer.
3734 BEG and END may be integers or markers.
3735 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3736 for the front of the overlay advance when text is inserted there
3737 \(which means the text *is not* included in the overlay).
3738 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3739 for the rear of the overlay advance when text is inserted there
3740 \(which means the text *is* included in the overlay). */)
3741 (beg, end, buffer, front_advance, rear_advance)
3742 Lisp_Object beg, end, buffer;
3743 Lisp_Object front_advance, rear_advance;
3744 {
3745 Lisp_Object overlay;
3746 struct buffer *b;
3747
3748 if (NILP (buffer))
3749 XSETBUFFER (buffer, current_buffer);
3750 else
3751 CHECK_BUFFER (buffer);
3752 if (MARKERP (beg)
3753 && ! EQ (Fmarker_buffer (beg), buffer))
3754 error ("Marker points into wrong buffer");
3755 if (MARKERP (end)
3756 && ! EQ (Fmarker_buffer (end), buffer))
3757 error ("Marker points into wrong buffer");
3758
3759 CHECK_NUMBER_COERCE_MARKER (beg);
3760 CHECK_NUMBER_COERCE_MARKER (end);
3761
3762 if (XINT (beg) > XINT (end))
3763 {
3764 Lisp_Object temp;
3765 temp = beg; beg = end; end = temp;
3766 }
3767
3768 b = XBUFFER (buffer);
3769
3770 beg = Fset_marker (Fmake_marker (), beg, buffer);
3771 end = Fset_marker (Fmake_marker (), end, buffer);
3772
3773 if (!NILP (front_advance))
3774 XMARKER (beg)->insertion_type = 1;
3775 if (!NILP (rear_advance))
3776 XMARKER (end)->insertion_type = 1;
3777
3778 overlay = allocate_misc ();
3779 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
3780 XOVERLAY (overlay)->start = beg;
3781 XOVERLAY (overlay)->end = end;
3782 XOVERLAY (overlay)->plist = Qnil;
3783 XOVERLAY (overlay)->next = NULL;
3784
3785 /* Put the new overlay on the wrong list. */
3786 end = OVERLAY_END (overlay);
3787 if (OVERLAY_POSITION (end) < b->overlay_center)
3788 {
3789 if (b->overlays_after)
3790 XOVERLAY (overlay)->next = b->overlays_after;
3791 b->overlays_after = XOVERLAY (overlay);
3792 }
3793 else
3794 {
3795 if (b->overlays_before)
3796 XOVERLAY (overlay)->next = b->overlays_before;
3797 b->overlays_before = XOVERLAY (overlay);
3798 }
3799
3800 /* This puts it in the right list, and in the right order. */
3801 recenter_overlay_lists (b, b->overlay_center);
3802
3803 /* We don't need to redisplay the region covered by the overlay, because
3804 the overlay has no properties at the moment. */
3805
3806 return overlay;
3807 }
3808 \f
3809 /* Mark a section of BUF as needing redisplay because of overlays changes. */
3810
3811 static void
3812 modify_overlay (buf, start, end)
3813 struct buffer *buf;
3814 EMACS_INT start, end;
3815 {
3816 if (start > end)
3817 {
3818 int temp = start;
3819 start = end;
3820 end = temp;
3821 }
3822
3823 BUF_COMPUTE_UNCHANGED (buf, start, end);
3824
3825 /* If this is a buffer not in the selected window,
3826 we must do other windows. */
3827 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
3828 windows_or_buffers_changed = 1;
3829 /* If multiple windows show this buffer, we must do other windows. */
3830 else if (buffer_shared > 1)
3831 windows_or_buffers_changed = 1;
3832 /* If we modify an overlay at the end of the buffer, we cannot
3833 be sure that window end is still valid. */
3834 else if (end >= ZV && start <= ZV)
3835 windows_or_buffers_changed = 1;
3836
3837 ++BUF_OVERLAY_MODIFF (buf);
3838 }
3839
3840 \f
3841 Lisp_Object Fdelete_overlay ();
3842
3843 static struct Lisp_Overlay *
3844 unchain_overlay (list, overlay)
3845 struct Lisp_Overlay *list, *overlay;
3846 {
3847 struct Lisp_Overlay *tmp, *prev;
3848 for (tmp = list, prev = NULL; tmp; prev = tmp, tmp = tmp->next)
3849 if (tmp == overlay)
3850 {
3851 if (prev)
3852 prev->next = tmp->next;
3853 else
3854 list = tmp->next;
3855 overlay->next = NULL;
3856 break;
3857 }
3858 return list;
3859 }
3860
3861 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3862 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
3863 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3864 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
3865 buffer. */)
3866 (overlay, beg, end, buffer)
3867 Lisp_Object overlay, beg, end, buffer;
3868 {
3869 struct buffer *b, *ob;
3870 Lisp_Object obuffer;
3871 int count = SPECPDL_INDEX ();
3872
3873 CHECK_OVERLAY (overlay);
3874 if (NILP (buffer))
3875 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3876 if (NILP (buffer))
3877 XSETBUFFER (buffer, current_buffer);
3878 CHECK_BUFFER (buffer);
3879
3880 if (MARKERP (beg)
3881 && ! EQ (Fmarker_buffer (beg), buffer))
3882 error ("Marker points into wrong buffer");
3883 if (MARKERP (end)
3884 && ! EQ (Fmarker_buffer (end), buffer))
3885 error ("Marker points into wrong buffer");
3886
3887 CHECK_NUMBER_COERCE_MARKER (beg);
3888 CHECK_NUMBER_COERCE_MARKER (end);
3889
3890 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3891 return Fdelete_overlay (overlay);
3892
3893 if (XINT (beg) > XINT (end))
3894 {
3895 Lisp_Object temp;
3896 temp = beg; beg = end; end = temp;
3897 }
3898
3899 specbind (Qinhibit_quit, Qt);
3900
3901 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3902 b = XBUFFER (buffer);
3903 ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
3904
3905 /* If the overlay has changed buffers, do a thorough redisplay. */
3906 if (!EQ (buffer, obuffer))
3907 {
3908 /* Redisplay where the overlay was. */
3909 if (!NILP (obuffer))
3910 {
3911 int o_beg;
3912 int o_end;
3913
3914 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3915 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3916
3917 modify_overlay (ob, o_beg, o_end);
3918 }
3919
3920 /* Redisplay where the overlay is going to be. */
3921 modify_overlay (b, XINT (beg), XINT (end));
3922 }
3923 else
3924 /* Redisplay the area the overlay has just left, or just enclosed. */
3925 {
3926 int o_beg, o_end;
3927
3928 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3929 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3930
3931 if (o_beg == XINT (beg))
3932 modify_overlay (b, o_end, XINT (end));
3933 else if (o_end == XINT (end))
3934 modify_overlay (b, o_beg, XINT (beg));
3935 else
3936 {
3937 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3938 if (XINT (end) > o_end) o_end = XINT (end);
3939 modify_overlay (b, o_beg, o_end);
3940 }
3941 }
3942
3943 if (!NILP (obuffer))
3944 {
3945 ob->overlays_before
3946 = unchain_overlay (ob->overlays_before, XOVERLAY (overlay));
3947 ob->overlays_after
3948 = unchain_overlay (ob->overlays_after, XOVERLAY (overlay));
3949 eassert (XOVERLAY (overlay)->next == NULL);
3950 }
3951
3952 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3953 Fset_marker (OVERLAY_END (overlay), end, buffer);
3954
3955 /* Put the overlay on the wrong list. */
3956 end = OVERLAY_END (overlay);
3957 if (OVERLAY_POSITION (end) < b->overlay_center)
3958 {
3959 XOVERLAY (overlay)->next = b->overlays_after;
3960 b->overlays_after = XOVERLAY (overlay);
3961 }
3962 else
3963 {
3964 XOVERLAY (overlay)->next = b->overlays_before;
3965 b->overlays_before = XOVERLAY (overlay);
3966 }
3967
3968 /* This puts it in the right list, and in the right order. */
3969 recenter_overlay_lists (b, b->overlay_center);
3970
3971 return unbind_to (count, overlay);
3972 }
3973
3974 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3975 doc: /* Delete the overlay OVERLAY from its buffer. */)
3976 (overlay)
3977 Lisp_Object overlay;
3978 {
3979 Lisp_Object buffer;
3980 struct buffer *b;
3981 int count = SPECPDL_INDEX ();
3982
3983 CHECK_OVERLAY (overlay);
3984
3985 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3986 if (NILP (buffer))
3987 return Qnil;
3988
3989 b = XBUFFER (buffer);
3990 specbind (Qinhibit_quit, Qt);
3991
3992 b->overlays_before = unchain_overlay (b->overlays_before,XOVERLAY (overlay));
3993 b->overlays_after = unchain_overlay (b->overlays_after, XOVERLAY (overlay));
3994 eassert (XOVERLAY (overlay)->next == NULL);
3995 modify_overlay (b,
3996 marker_position (OVERLAY_START (overlay)),
3997 marker_position (OVERLAY_END (overlay)));
3998 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3999 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
4000
4001 /* When deleting an overlay with before or after strings, turn off
4002 display optimizations for the affected buffer, on the basis that
4003 these strings may contain newlines. This is easier to do than to
4004 check for that situation during redisplay. */
4005 if (!windows_or_buffers_changed
4006 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4007 || !NILP (Foverlay_get (overlay, Qafter_string))))
4008 b->prevent_redisplay_optimizations_p = 1;
4009
4010 return unbind_to (count, Qnil);
4011 }
4012 \f
4013 /* Overlay dissection functions. */
4014
4015 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
4016 doc: /* Return the position at which OVERLAY starts. */)
4017 (overlay)
4018 Lisp_Object overlay;
4019 {
4020 CHECK_OVERLAY (overlay);
4021
4022 return (Fmarker_position (OVERLAY_START (overlay)));
4023 }
4024
4025 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
4026 doc: /* Return the position at which OVERLAY ends. */)
4027 (overlay)
4028 Lisp_Object overlay;
4029 {
4030 CHECK_OVERLAY (overlay);
4031
4032 return (Fmarker_position (OVERLAY_END (overlay)));
4033 }
4034
4035 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
4036 doc: /* Return the buffer OVERLAY belongs to.
4037 Return nil if OVERLAY has been deleted. */)
4038 (overlay)
4039 Lisp_Object overlay;
4040 {
4041 CHECK_OVERLAY (overlay);
4042
4043 return Fmarker_buffer (OVERLAY_START (overlay));
4044 }
4045
4046 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
4047 doc: /* Return a list of the properties on OVERLAY.
4048 This is a copy of OVERLAY's plist; modifying its conses has no effect on
4049 OVERLAY. */)
4050 (overlay)
4051 Lisp_Object overlay;
4052 {
4053 CHECK_OVERLAY (overlay);
4054
4055 return Fcopy_sequence (XOVERLAY (overlay)->plist);
4056 }
4057
4058 \f
4059 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
4060 doc: /* Return a list of the overlays that contain the character at POS. */)
4061 (pos)
4062 Lisp_Object pos;
4063 {
4064 int noverlays;
4065 Lisp_Object *overlay_vec;
4066 int len;
4067 Lisp_Object result;
4068
4069 CHECK_NUMBER_COERCE_MARKER (pos);
4070
4071 len = 10;
4072 /* We can't use alloca here because overlays_at can call xrealloc. */
4073 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4074
4075 /* Put all the overlays we want in a vector in overlay_vec.
4076 Store the length in len. */
4077 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4078 (EMACS_INT *) 0, (EMACS_INT *) 0, 0);
4079
4080 /* Make a list of them all. */
4081 result = Flist (noverlays, overlay_vec);
4082
4083 xfree (overlay_vec);
4084 return result;
4085 }
4086
4087 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
4088 doc: /* Return a list of the overlays that overlap the region BEG ... END.
4089 Overlap means that at least one character is contained within the overlay
4090 and also contained within the specified region.
4091 Empty overlays are included in the result if they are located at BEG,
4092 between BEG and END, or at END provided END denotes the position at the
4093 end of the buffer. */)
4094 (beg, end)
4095 Lisp_Object beg, end;
4096 {
4097 int noverlays;
4098 Lisp_Object *overlay_vec;
4099 int len;
4100 Lisp_Object result;
4101
4102 CHECK_NUMBER_COERCE_MARKER (beg);
4103 CHECK_NUMBER_COERCE_MARKER (end);
4104
4105 len = 10;
4106 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4107
4108 /* Put all the overlays we want in a vector in overlay_vec.
4109 Store the length in len. */
4110 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
4111 (int *) 0, (int *) 0);
4112
4113 /* Make a list of them all. */
4114 result = Flist (noverlays, overlay_vec);
4115
4116 xfree (overlay_vec);
4117 return result;
4118 }
4119
4120 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
4121 1, 1, 0,
4122 doc: /* Return the next position after POS where an overlay starts or ends.
4123 If there are no overlay boundaries from POS to (point-max),
4124 the value is (point-max). */)
4125 (pos)
4126 Lisp_Object pos;
4127 {
4128 int noverlays;
4129 EMACS_INT endpos;
4130 Lisp_Object *overlay_vec;
4131 int len;
4132 int i;
4133
4134 CHECK_NUMBER_COERCE_MARKER (pos);
4135
4136 len = 10;
4137 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4138
4139 /* Put all the overlays we want in a vector in overlay_vec.
4140 Store the length in len.
4141 endpos gets the position where the next overlay starts. */
4142 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4143 &endpos, (EMACS_INT *) 0, 1);
4144
4145 /* If any of these overlays ends before endpos,
4146 use its ending point instead. */
4147 for (i = 0; i < noverlays; i++)
4148 {
4149 Lisp_Object oend;
4150 EMACS_INT oendpos;
4151
4152 oend = OVERLAY_END (overlay_vec[i]);
4153 oendpos = OVERLAY_POSITION (oend);
4154 if (oendpos < endpos)
4155 endpos = oendpos;
4156 }
4157
4158 xfree (overlay_vec);
4159 return make_number (endpos);
4160 }
4161
4162 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
4163 Sprevious_overlay_change, 1, 1, 0,
4164 doc: /* Return the previous position before POS where an overlay starts or ends.
4165 If there are no overlay boundaries from (point-min) to POS,
4166 the value is (point-min). */)
4167 (pos)
4168 Lisp_Object pos;
4169 {
4170 int noverlays;
4171 EMACS_INT prevpos;
4172 Lisp_Object *overlay_vec;
4173 int len;
4174
4175 CHECK_NUMBER_COERCE_MARKER (pos);
4176
4177 /* At beginning of buffer, we know the answer;
4178 avoid bug subtracting 1 below. */
4179 if (XINT (pos) == BEGV)
4180 return pos;
4181
4182 len = 10;
4183 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
4184
4185 /* Put all the overlays we want in a vector in overlay_vec.
4186 Store the length in len.
4187 prevpos gets the position of the previous change. */
4188 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
4189 (EMACS_INT *) 0, &prevpos, 1);
4190
4191 xfree (overlay_vec);
4192 return make_number (prevpos);
4193 }
4194 \f
4195 /* These functions are for debugging overlays. */
4196
4197 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
4198 doc: /* Return a pair of lists giving all the overlays of the current buffer.
4199 The car has all the overlays before the overlay center;
4200 the cdr has all the overlays after the overlay center.
4201 Recentering overlays moves overlays between these lists.
4202 The lists you get are copies, so that changing them has no effect.
4203 However, the overlays you get are the real objects that the buffer uses. */)
4204 ()
4205 {
4206 struct Lisp_Overlay *ol;
4207 Lisp_Object before = Qnil, after = Qnil, tmp;
4208 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
4209 {
4210 XSETMISC (tmp, ol);
4211 before = Fcons (tmp, before);
4212 }
4213 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
4214 {
4215 XSETMISC (tmp, ol);
4216 after = Fcons (tmp, after);
4217 }
4218 return Fcons (Fnreverse (before), Fnreverse (after));
4219 }
4220
4221 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
4222 doc: /* Recenter the overlays of the current buffer around position POS.
4223 That makes overlay lookup faster for positions near POS (but perhaps slower
4224 for positions far away from POS). */)
4225 (pos)
4226 Lisp_Object pos;
4227 {
4228 CHECK_NUMBER_COERCE_MARKER (pos);
4229
4230 recenter_overlay_lists (current_buffer, XINT (pos));
4231 return Qnil;
4232 }
4233 \f
4234 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
4235 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
4236 (overlay, prop)
4237 Lisp_Object overlay, prop;
4238 {
4239 CHECK_OVERLAY (overlay);
4240 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
4241 }
4242
4243 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
4244 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. */)
4245 (overlay, prop, value)
4246 Lisp_Object overlay, prop, value;
4247 {
4248 Lisp_Object tail, buffer;
4249 int changed;
4250
4251 CHECK_OVERLAY (overlay);
4252
4253 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4254
4255 for (tail = XOVERLAY (overlay)->plist;
4256 CONSP (tail) && CONSP (XCDR (tail));
4257 tail = XCDR (XCDR (tail)))
4258 if (EQ (XCAR (tail), prop))
4259 {
4260 changed = !EQ (XCAR (XCDR (tail)), value);
4261 XSETCAR (XCDR (tail), value);
4262 goto found;
4263 }
4264 /* It wasn't in the list, so add it to the front. */
4265 changed = !NILP (value);
4266 XOVERLAY (overlay)->plist
4267 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
4268 found:
4269 if (! NILP (buffer))
4270 {
4271 if (changed)
4272 modify_overlay (XBUFFER (buffer),
4273 marker_position (OVERLAY_START (overlay)),
4274 marker_position (OVERLAY_END (overlay)));
4275 if (EQ (prop, Qevaporate) && ! NILP (value)
4276 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4277 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4278 Fdelete_overlay (overlay);
4279 }
4280
4281 return value;
4282 }
4283 \f
4284 /* Subroutine of report_overlay_modification. */
4285
4286 /* Lisp vector holding overlay hook functions to call.
4287 Vector elements come in pairs.
4288 Each even-index element is a list of hook functions.
4289 The following odd-index element is the overlay they came from.
4290
4291 Before the buffer change, we fill in this vector
4292 as we call overlay hook functions.
4293 After the buffer change, we get the functions to call from this vector.
4294 This way we always call the same functions before and after the change. */
4295 static Lisp_Object last_overlay_modification_hooks;
4296
4297 /* Number of elements actually used in last_overlay_modification_hooks. */
4298 static int last_overlay_modification_hooks_used;
4299
4300 /* Add one functionlist/overlay pair
4301 to the end of last_overlay_modification_hooks. */
4302
4303 static void
4304 add_overlay_mod_hooklist (functionlist, overlay)
4305 Lisp_Object functionlist, overlay;
4306 {
4307 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
4308
4309 if (last_overlay_modification_hooks_used == oldsize)
4310 last_overlay_modification_hooks = larger_vector
4311 (last_overlay_modification_hooks, oldsize * 2, Qnil);
4312 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4313 functionlist); last_overlay_modification_hooks_used++;
4314 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4315 overlay); last_overlay_modification_hooks_used++;
4316 }
4317 \f
4318 /* Run the modification-hooks of overlays that include
4319 any part of the text in START to END.
4320 If this change is an insertion, also
4321 run the insert-before-hooks of overlay starting at END,
4322 and the insert-after-hooks of overlay ending at START.
4323
4324 This is called both before and after the modification.
4325 AFTER is nonzero when we call after the modification.
4326
4327 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4328 When AFTER is nonzero, they are the start position,
4329 the position after the inserted new text,
4330 and the length of deleted or replaced old text. */
4331
4332 void
4333 report_overlay_modification (start, end, after, arg1, arg2, arg3)
4334 Lisp_Object start, end;
4335 int after;
4336 Lisp_Object arg1, arg2, arg3;
4337 {
4338 Lisp_Object prop, overlay;
4339 struct Lisp_Overlay *tail;
4340 /* 1 if this change is an insertion. */
4341 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
4342 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4343
4344 overlay = Qnil;
4345 tail = NULL;
4346
4347 /* We used to run the functions as soon as we found them and only register
4348 them in last_overlay_modification_hooks for the purpose of the `after'
4349 case. But running elisp code as we traverse the list of overlays is
4350 painful because the list can be modified by the elisp code so we had to
4351 copy at several places. We now simply do a read-only traversal that
4352 only collects the functions to run and we run them afterwards. It's
4353 simpler, especially since all the code was already there. -stef */
4354
4355 if (!after)
4356 {
4357 /* We are being called before a change.
4358 Scan the overlays to find the functions to call. */
4359 last_overlay_modification_hooks_used = 0;
4360 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4361 {
4362 int startpos, endpos;
4363 Lisp_Object ostart, oend;
4364
4365 XSETMISC (overlay, tail);
4366
4367 ostart = OVERLAY_START (overlay);
4368 oend = OVERLAY_END (overlay);
4369 endpos = OVERLAY_POSITION (oend);
4370 if (XFASTINT (start) > endpos)
4371 break;
4372 startpos = OVERLAY_POSITION (ostart);
4373 if (insertion && (XFASTINT (start) == startpos
4374 || XFASTINT (end) == startpos))
4375 {
4376 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4377 if (!NILP (prop))
4378 add_overlay_mod_hooklist (prop, overlay);
4379 }
4380 if (insertion && (XFASTINT (start) == endpos
4381 || XFASTINT (end) == endpos))
4382 {
4383 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4384 if (!NILP (prop))
4385 add_overlay_mod_hooklist (prop, overlay);
4386 }
4387 /* Test for intersecting intervals. This does the right thing
4388 for both insertion and deletion. */
4389 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4390 {
4391 prop = Foverlay_get (overlay, Qmodification_hooks);
4392 if (!NILP (prop))
4393 add_overlay_mod_hooklist (prop, overlay);
4394 }
4395 }
4396
4397 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4398 {
4399 int startpos, endpos;
4400 Lisp_Object ostart, oend;
4401
4402 XSETMISC (overlay, tail);
4403
4404 ostart = OVERLAY_START (overlay);
4405 oend = OVERLAY_END (overlay);
4406 startpos = OVERLAY_POSITION (ostart);
4407 endpos = OVERLAY_POSITION (oend);
4408 if (XFASTINT (end) < startpos)
4409 break;
4410 if (insertion && (XFASTINT (start) == startpos
4411 || XFASTINT (end) == startpos))
4412 {
4413 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4414 if (!NILP (prop))
4415 add_overlay_mod_hooklist (prop, overlay);
4416 }
4417 if (insertion && (XFASTINT (start) == endpos
4418 || XFASTINT (end) == endpos))
4419 {
4420 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4421 if (!NILP (prop))
4422 add_overlay_mod_hooklist (prop, overlay);
4423 }
4424 /* Test for intersecting intervals. This does the right thing
4425 for both insertion and deletion. */
4426 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
4427 {
4428 prop = Foverlay_get (overlay, Qmodification_hooks);
4429 if (!NILP (prop))
4430 add_overlay_mod_hooklist (prop, overlay);
4431 }
4432 }
4433 }
4434
4435 GCPRO4 (overlay, arg1, arg2, arg3);
4436 {
4437 /* Call the functions recorded in last_overlay_modification_hooks.
4438 First copy the vector contents, in case some of these hooks
4439 do subsequent modification of the buffer. */
4440 int size = last_overlay_modification_hooks_used;
4441 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
4442 int i;
4443
4444 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
4445 copy, size * sizeof (Lisp_Object));
4446 gcpro1.var = copy;
4447 gcpro1.nvars = size;
4448
4449 for (i = 0; i < size;)
4450 {
4451 Lisp_Object prop, overlay;
4452 prop = copy[i++];
4453 overlay = copy[i++];
4454 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
4455 }
4456 }
4457 UNGCPRO;
4458 }
4459
4460 static void
4461 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
4462 Lisp_Object list, overlay;
4463 int after;
4464 Lisp_Object arg1, arg2, arg3;
4465 {
4466 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4467
4468 GCPRO4 (list, arg1, arg2, arg3);
4469
4470 while (CONSP (list))
4471 {
4472 if (NILP (arg3))
4473 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
4474 else
4475 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4476 list = XCDR (list);
4477 }
4478 UNGCPRO;
4479 }
4480
4481 /* Delete any zero-sized overlays at position POS, if the `evaporate'
4482 property is set. */
4483 void
4484 evaporate_overlays (pos)
4485 EMACS_INT pos;
4486 {
4487 Lisp_Object overlay, hit_list;
4488 struct Lisp_Overlay *tail;
4489
4490 hit_list = Qnil;
4491 if (pos <= current_buffer->overlay_center)
4492 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
4493 {
4494 int endpos;
4495 XSETMISC (overlay, tail);
4496 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4497 if (endpos < pos)
4498 break;
4499 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
4500 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4501 hit_list = Fcons (overlay, hit_list);
4502 }
4503 else
4504 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
4505 {
4506 int startpos;
4507 XSETMISC (overlay, tail);
4508 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4509 if (startpos > pos)
4510 break;
4511 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
4512 && ! NILP (Foverlay_get (overlay, Qevaporate)))
4513 hit_list = Fcons (overlay, hit_list);
4514 }
4515 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4516 Fdelete_overlay (XCAR (hit_list));
4517 }
4518 \f
4519 /* Somebody has tried to store a value with an unacceptable type
4520 in the slot with offset OFFSET. */
4521
4522 void
4523 buffer_slot_type_mismatch (newval, type)
4524 Lisp_Object newval;
4525 int type;
4526 {
4527 Lisp_Object predicate;
4528
4529 switch (type)
4530 {
4531 case Lisp_Int: predicate = Qintegerp; break;
4532 case Lisp_String: predicate = Qstringp; break;
4533 case Lisp_Symbol: predicate = Qsymbolp; break;
4534 default: abort ();
4535 }
4536
4537 wrong_type_argument (predicate, newval);
4538 }
4539
4540 \f
4541 /***********************************************************************
4542 Allocation with mmap
4543 ***********************************************************************/
4544
4545 #ifdef USE_MMAP_FOR_BUFFERS
4546
4547 #include <sys/types.h>
4548 #include <sys/mman.h>
4549
4550 #ifndef MAP_ANON
4551 #ifdef MAP_ANONYMOUS
4552 #define MAP_ANON MAP_ANONYMOUS
4553 #else
4554 #define MAP_ANON 0
4555 #endif
4556 #endif
4557
4558 #ifndef MAP_FAILED
4559 #define MAP_FAILED ((void *) -1)
4560 #endif
4561
4562 #include <stdio.h>
4563 #include <errno.h>
4564
4565 #if MAP_ANON == 0
4566 #include <fcntl.h>
4567 #endif
4568
4569 #include "coding.h"
4570
4571
4572 /* Memory is allocated in regions which are mapped using mmap(2).
4573 The current implementation lets the system select mapped
4574 addresses; we're not using MAP_FIXED in general, except when
4575 trying to enlarge regions.
4576
4577 Each mapped region starts with a mmap_region structure, the user
4578 area starts after that structure, aligned to MEM_ALIGN.
4579
4580 +-----------------------+
4581 | struct mmap_info + |
4582 | padding |
4583 +-----------------------+
4584 | user data |
4585 | |
4586 | |
4587 +-----------------------+ */
4588
4589 struct mmap_region
4590 {
4591 /* User-specified size. */
4592 size_t nbytes_specified;
4593
4594 /* Number of bytes mapped */
4595 size_t nbytes_mapped;
4596
4597 /* Pointer to the location holding the address of the memory
4598 allocated with the mmap'd block. The variable actually points
4599 after this structure. */
4600 POINTER_TYPE **var;
4601
4602 /* Next and previous in list of all mmap'd regions. */
4603 struct mmap_region *next, *prev;
4604 };
4605
4606 /* Doubly-linked list of mmap'd regions. */
4607
4608 static struct mmap_region *mmap_regions;
4609
4610 /* File descriptor for mmap. If we don't have anonymous mapping,
4611 /dev/zero will be opened on it. */
4612
4613 static int mmap_fd;
4614
4615 /* Temporary storage for mmap_set_vars, see there. */
4616
4617 static struct mmap_region *mmap_regions_1;
4618 static int mmap_fd_1;
4619
4620 /* Page size on this system. */
4621
4622 static int mmap_page_size;
4623
4624 /* 1 means mmap has been intialized. */
4625
4626 static int mmap_initialized_p;
4627
4628 /* Value is X rounded up to the next multiple of N. */
4629
4630 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4631
4632 /* Size of mmap_region structure plus padding. */
4633
4634 #define MMAP_REGION_STRUCT_SIZE \
4635 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4636
4637 /* Given a pointer P to the start of the user-visible part of a mapped
4638 region, return a pointer to the start of the region. */
4639
4640 #define MMAP_REGION(P) \
4641 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4642
4643 /* Given a pointer P to the start of a mapped region, return a pointer
4644 to the start of the user-visible part of the region. */
4645
4646 #define MMAP_USER_AREA(P) \
4647 ((POINTER_TYPE *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
4648
4649 #define MEM_ALIGN sizeof (double)
4650
4651 /* Predicate returning true if part of the address range [START .. END]
4652 is currently mapped. Used to prevent overwriting an existing
4653 memory mapping.
4654
4655 Default is to conservativly assume the address range is occupied by
4656 something else. This can be overridden by system configuration
4657 files if system-specific means to determine this exists. */
4658
4659 #ifndef MMAP_ALLOCATED_P
4660 #define MMAP_ALLOCATED_P(start, end) 1
4661 #endif
4662
4663 /* Function prototypes. */
4664
4665 static int mmap_free_1 P_ ((struct mmap_region *));
4666 static int mmap_enlarge P_ ((struct mmap_region *, int));
4667 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
4668 static POINTER_TYPE *mmap_alloc P_ ((POINTER_TYPE **, size_t));
4669 static POINTER_TYPE *mmap_realloc P_ ((POINTER_TYPE **, size_t));
4670 static void mmap_free P_ ((POINTER_TYPE **ptr));
4671 static void mmap_init P_ ((void));
4672
4673
4674 /* Return a region overlapping address range START...END, or null if
4675 none. END is not including, i.e. the last byte in the range
4676 is at END - 1. */
4677
4678 static struct mmap_region *
4679 mmap_find (start, end)
4680 POINTER_TYPE *start, *end;
4681 {
4682 struct mmap_region *r;
4683 char *s = (char *) start, *e = (char *) end;
4684
4685 for (r = mmap_regions; r; r = r->next)
4686 {
4687 char *rstart = (char *) r;
4688 char *rend = rstart + r->nbytes_mapped;
4689
4690 if (/* First byte of range, i.e. START, in this region? */
4691 (s >= rstart && s < rend)
4692 /* Last byte of range, i.e. END - 1, in this region? */
4693 || (e > rstart && e <= rend)
4694 /* First byte of this region in the range? */
4695 || (rstart >= s && rstart < e)
4696 /* Last byte of this region in the range? */
4697 || (rend > s && rend <= e))
4698 break;
4699 }
4700
4701 return r;
4702 }
4703
4704
4705 /* Unmap a region. P is a pointer to the start of the user-araa of
4706 the region. Value is non-zero if successful. */
4707
4708 static int
4709 mmap_free_1 (r)
4710 struct mmap_region *r;
4711 {
4712 if (r->next)
4713 r->next->prev = r->prev;
4714 if (r->prev)
4715 r->prev->next = r->next;
4716 else
4717 mmap_regions = r->next;
4718
4719 if (munmap ((POINTER_TYPE *) r, r->nbytes_mapped) == -1)
4720 {
4721 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4722 return 0;
4723 }
4724
4725 return 1;
4726 }
4727
4728
4729 /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
4730 Value is non-zero if successful. */
4731
4732 static int
4733 mmap_enlarge (r, npages)
4734 struct mmap_region *r;
4735 int npages;
4736 {
4737 char *region_end = (char *) r + r->nbytes_mapped;
4738 size_t nbytes;
4739 int success = 0;
4740
4741 if (npages < 0)
4742 {
4743 /* Unmap pages at the end of the region. */
4744 nbytes = - npages * mmap_page_size;
4745 if (munmap (region_end - nbytes, nbytes) == -1)
4746 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4747 else
4748 {
4749 r->nbytes_mapped -= nbytes;
4750 success = 1;
4751 }
4752 }
4753 else if (npages > 0)
4754 {
4755 nbytes = npages * mmap_page_size;
4756
4757 /* Try to map additional pages at the end of the region. We
4758 cannot do this if the address range is already occupied by
4759 something else because mmap deletes any previous mapping.
4760 I'm not sure this is worth doing, let's see. */
4761 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
4762 {
4763 POINTER_TYPE *p;
4764
4765 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4766 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4767 if (p == MAP_FAILED)
4768 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
4769 else if (p != (POINTER_TYPE *) region_end)
4770 {
4771 /* Kernels are free to choose a different address. In
4772 that case, unmap what we've mapped above; we have
4773 no use for it. */
4774 if (munmap (p, nbytes) == -1)
4775 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4776 }
4777 else
4778 {
4779 r->nbytes_mapped += nbytes;
4780 success = 1;
4781 }
4782 }
4783 }
4784
4785 return success;
4786 }
4787
4788
4789 /* Set or reset variables holding references to mapped regions. If
4790 RESTORE_P is zero, set all variables to null. If RESTORE_P is
4791 non-zero, set all variables to the start of the user-areas
4792 of mapped regions.
4793
4794 This function is called from Fdump_emacs to ensure that the dumped
4795 Emacs doesn't contain references to memory that won't be mapped
4796 when Emacs starts. */
4797
4798 void
4799 mmap_set_vars (restore_p)
4800 int restore_p;
4801 {
4802 struct mmap_region *r;
4803
4804 if (restore_p)
4805 {
4806 mmap_regions = mmap_regions_1;
4807 mmap_fd = mmap_fd_1;
4808 for (r = mmap_regions; r; r = r->next)
4809 *r->var = MMAP_USER_AREA (r);
4810 }
4811 else
4812 {
4813 for (r = mmap_regions; r; r = r->next)
4814 *r->var = NULL;
4815 mmap_regions_1 = mmap_regions;
4816 mmap_regions = NULL;
4817 mmap_fd_1 = mmap_fd;
4818 mmap_fd = -1;
4819 }
4820 }
4821
4822
4823 /* Allocate a block of storage large enough to hold NBYTES bytes of
4824 data. A pointer to the data is returned in *VAR. VAR is thus the
4825 address of some variable which will use the data area.
4826
4827 The allocation of 0 bytes is valid.
4828
4829 If we can't allocate the necessary memory, set *VAR to null, and
4830 return null. */
4831
4832 static POINTER_TYPE *
4833 mmap_alloc (var, nbytes)
4834 POINTER_TYPE **var;
4835 size_t nbytes;
4836 {
4837 void *p;
4838 size_t map;
4839
4840 mmap_init ();
4841
4842 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4843 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4844 mmap_fd, 0);
4845
4846 if (p == MAP_FAILED)
4847 {
4848 if (errno != ENOMEM)
4849 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4850 p = NULL;
4851 }
4852 else
4853 {
4854 struct mmap_region *r = (struct mmap_region *) p;
4855
4856 r->nbytes_specified = nbytes;
4857 r->nbytes_mapped = map;
4858 r->var = var;
4859 r->prev = NULL;
4860 r->next = mmap_regions;
4861 if (r->next)
4862 r->next->prev = r;
4863 mmap_regions = r;
4864
4865 p = MMAP_USER_AREA (p);
4866 }
4867
4868 return *var = p;
4869 }
4870
4871
4872 /* Given a pointer at address VAR to data allocated with mmap_alloc,
4873 resize it to size NBYTES. Change *VAR to reflect the new block,
4874 and return this value. If more memory cannot be allocated, then
4875 leave *VAR unchanged, and return null. */
4876
4877 static POINTER_TYPE *
4878 mmap_realloc (var, nbytes)
4879 POINTER_TYPE **var;
4880 size_t nbytes;
4881 {
4882 POINTER_TYPE *result;
4883
4884 mmap_init ();
4885
4886 if (*var == NULL)
4887 result = mmap_alloc (var, nbytes);
4888 else if (nbytes == 0)
4889 {
4890 mmap_free (var);
4891 result = mmap_alloc (var, nbytes);
4892 }
4893 else
4894 {
4895 struct mmap_region *r = MMAP_REGION (*var);
4896 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
4897
4898 if (room < nbytes)
4899 {
4900 /* Must enlarge. */
4901 POINTER_TYPE *old_ptr = *var;
4902
4903 /* Try to map additional pages at the end of the region.
4904 If that fails, allocate a new region, copy data
4905 from the old region, then free it. */
4906 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4907 / mmap_page_size)))
4908 {
4909 r->nbytes_specified = nbytes;
4910 *var = result = old_ptr;
4911 }
4912 else if (mmap_alloc (var, nbytes))
4913 {
4914 bcopy (old_ptr, *var, r->nbytes_specified);
4915 mmap_free_1 (MMAP_REGION (old_ptr));
4916 result = *var;
4917 r = MMAP_REGION (result);
4918 r->nbytes_specified = nbytes;
4919 }
4920 else
4921 {
4922 *var = old_ptr;
4923 result = NULL;
4924 }
4925 }
4926 else if (room - nbytes >= mmap_page_size)
4927 {
4928 /* Shrinking by at least a page. Let's give some
4929 memory back to the system.
4930
4931 The extra parens are to make the division happens first,
4932 on positive values, so we know it will round towards
4933 zero. */
4934 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
4935 result = *var;
4936 r->nbytes_specified = nbytes;
4937 }
4938 else
4939 {
4940 /* Leave it alone. */
4941 result = *var;
4942 r->nbytes_specified = nbytes;
4943 }
4944 }
4945
4946 return result;
4947 }
4948
4949
4950 /* Free a block of relocatable storage whose data is pointed to by
4951 PTR. Store 0 in *PTR to show there's no block allocated. */
4952
4953 static void
4954 mmap_free (var)
4955 POINTER_TYPE **var;
4956 {
4957 mmap_init ();
4958
4959 if (*var)
4960 {
4961 mmap_free_1 (MMAP_REGION (*var));
4962 *var = NULL;
4963 }
4964 }
4965
4966
4967 /* Perform necessary intializations for the use of mmap. */
4968
4969 static void
4970 mmap_init ()
4971 {
4972 #if MAP_ANON == 0
4973 /* The value of mmap_fd is initially 0 in temacs, and -1
4974 in a dumped Emacs. */
4975 if (mmap_fd <= 0)
4976 {
4977 /* No anonymous mmap -- we need the file descriptor. */
4978 mmap_fd = open ("/dev/zero", O_RDONLY);
4979 if (mmap_fd == -1)
4980 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4981 }
4982 #endif /* MAP_ANON == 0 */
4983
4984 if (mmap_initialized_p)
4985 return;
4986 mmap_initialized_p = 1;
4987
4988 #if MAP_ANON != 0
4989 mmap_fd = -1;
4990 #endif
4991
4992 mmap_page_size = getpagesize ();
4993 }
4994
4995 #endif /* USE_MMAP_FOR_BUFFERS */
4996
4997
4998 \f
4999 /***********************************************************************
5000 Buffer-text Allocation
5001 ***********************************************************************/
5002
5003 #ifdef REL_ALLOC
5004 extern POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
5005 extern POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
5006 extern void r_alloc_free P_ ((POINTER_TYPE **ptr));
5007 #endif /* REL_ALLOC */
5008
5009
5010 /* Allocate NBYTES bytes for buffer B's text buffer. */
5011
5012 static void
5013 alloc_buffer_text (b, nbytes)
5014 struct buffer *b;
5015 size_t nbytes;
5016 {
5017 POINTER_TYPE *p;
5018
5019 BLOCK_INPUT;
5020 #if defined USE_MMAP_FOR_BUFFERS
5021 p = mmap_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5022 #elif defined REL_ALLOC
5023 p = r_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5024 #else
5025 p = xmalloc (nbytes);
5026 #endif
5027
5028 if (p == NULL)
5029 {
5030 UNBLOCK_INPUT;
5031 memory_full ();
5032 }
5033
5034 b->text->beg = (unsigned char *) p;
5035 UNBLOCK_INPUT;
5036 }
5037
5038 /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5039 shrink it. */
5040
5041 void
5042 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
5043 {
5044 POINTER_TYPE *p;
5045 size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5046 + delta);
5047 BLOCK_INPUT;
5048 #if defined USE_MMAP_FOR_BUFFERS
5049 p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5050 #elif defined REL_ALLOC
5051 p = r_re_alloc ((POINTER_TYPE **) &b->text->beg, nbytes);
5052 #else
5053 p = xrealloc (b->text->beg, nbytes);
5054 #endif
5055
5056 if (p == NULL)
5057 {
5058 UNBLOCK_INPUT;
5059 memory_full ();
5060 }
5061
5062 BUF_BEG_ADDR (b) = (unsigned char *) p;
5063 UNBLOCK_INPUT;
5064 }
5065
5066
5067 /* Free buffer B's text buffer. */
5068
5069 static void
5070 free_buffer_text (b)
5071 struct buffer *b;
5072 {
5073 BLOCK_INPUT;
5074
5075 #if defined USE_MMAP_FOR_BUFFERS
5076 mmap_free ((POINTER_TYPE **) &b->text->beg);
5077 #elif defined REL_ALLOC
5078 r_alloc_free ((POINTER_TYPE **) &b->text->beg);
5079 #else
5080 xfree (b->text->beg);
5081 #endif
5082
5083 BUF_BEG_ADDR (b) = NULL;
5084 UNBLOCK_INPUT;
5085 }
5086
5087
5088 \f
5089 /***********************************************************************
5090 Initialization
5091 ***********************************************************************/
5092
5093 void
5094 init_buffer_once ()
5095 {
5096 int idx;
5097
5098 bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
5099
5100 /* Make sure all markable slots in buffer_defaults
5101 are initialized reasonably, so mark_buffer won't choke. */
5102 reset_buffer (&buffer_defaults);
5103 reset_buffer_local_variables (&buffer_defaults, 1);
5104 reset_buffer (&buffer_local_symbols);
5105 reset_buffer_local_variables (&buffer_local_symbols, 1);
5106 /* Prevent GC from getting confused. */
5107 buffer_defaults.text = &buffer_defaults.own_text;
5108 buffer_local_symbols.text = &buffer_local_symbols.own_text;
5109 BUF_INTERVALS (&buffer_defaults) = 0;
5110 BUF_INTERVALS (&buffer_local_symbols) = 0;
5111 XSETPVECTYPE (&buffer_defaults, PVEC_BUFFER);
5112 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
5113 XSETPVECTYPE (&buffer_local_symbols, PVEC_BUFFER);
5114 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
5115
5116 /* Set up the default values of various buffer slots. */
5117 /* Must do these before making the first buffer! */
5118
5119 /* real setup is done in bindings.el */
5120 buffer_defaults.mode_line_format = build_string ("%-");
5121 buffer_defaults.header_line_format = Qnil;
5122 buffer_defaults.abbrev_mode = Qnil;
5123 buffer_defaults.overwrite_mode = Qnil;
5124 buffer_defaults.case_fold_search = Qt;
5125 buffer_defaults.auto_fill_function = Qnil;
5126 buffer_defaults.selective_display = Qnil;
5127 #ifndef old
5128 buffer_defaults.selective_display_ellipses = Qt;
5129 #endif
5130 buffer_defaults.abbrev_table = Qnil;
5131 buffer_defaults.display_table = Qnil;
5132 buffer_defaults.undo_list = Qnil;
5133 buffer_defaults.mark_active = Qnil;
5134 buffer_defaults.file_format = Qnil;
5135 buffer_defaults.auto_save_file_format = Qt;
5136 buffer_defaults.overlays_before = NULL;
5137 buffer_defaults.overlays_after = NULL;
5138 buffer_defaults.overlay_center = BEG;
5139
5140 XSETFASTINT (buffer_defaults.tab_width, 8);
5141 buffer_defaults.truncate_lines = Qnil;
5142 buffer_defaults.word_wrap = Qnil;
5143 buffer_defaults.ctl_arrow = Qt;
5144 buffer_defaults.direction_reversed = Qnil;
5145 buffer_defaults.cursor_type = Qt;
5146 buffer_defaults.extra_line_spacing = Qnil;
5147 buffer_defaults.cursor_in_non_selected_windows = Qt;
5148
5149 #ifdef DOS_NT
5150 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
5151 #endif
5152 buffer_defaults.enable_multibyte_characters = Qt;
5153 buffer_defaults.buffer_file_coding_system = Qnil;
5154 XSETFASTINT (buffer_defaults.fill_column, 70);
5155 XSETFASTINT (buffer_defaults.left_margin, 0);
5156 buffer_defaults.cache_long_line_scans = Qnil;
5157 buffer_defaults.file_truename = Qnil;
5158 XSETFASTINT (buffer_defaults.display_count, 0);
5159 XSETFASTINT (buffer_defaults.left_margin_cols, 0);
5160 XSETFASTINT (buffer_defaults.right_margin_cols, 0);
5161 buffer_defaults.left_fringe_width = Qnil;
5162 buffer_defaults.right_fringe_width = Qnil;
5163 buffer_defaults.fringes_outside_margins = Qnil;
5164 buffer_defaults.scroll_bar_width = Qnil;
5165 buffer_defaults.vertical_scroll_bar_type = Qt;
5166 buffer_defaults.indicate_empty_lines = Qnil;
5167 buffer_defaults.indicate_buffer_boundaries = Qnil;
5168 buffer_defaults.fringe_indicator_alist = Qnil;
5169 buffer_defaults.fringe_cursor_alist = Qnil;
5170 buffer_defaults.scroll_up_aggressively = Qnil;
5171 buffer_defaults.scroll_down_aggressively = Qnil;
5172 buffer_defaults.display_time = Qnil;
5173
5174 /* Assign the local-flags to the slots that have default values.
5175 The local flag is a bit that is used in the buffer
5176 to say that it has its own local value for the slot.
5177 The local flag bits are in the local_var_flags slot of the buffer. */
5178
5179 /* Nothing can work if this isn't true */
5180 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
5181
5182 /* 0 means not a lisp var, -1 means always local, else mask */
5183 bzero (&buffer_local_flags, sizeof buffer_local_flags);
5184 XSETINT (buffer_local_flags.filename, -1);
5185 XSETINT (buffer_local_flags.directory, -1);
5186 XSETINT (buffer_local_flags.backed_up, -1);
5187 XSETINT (buffer_local_flags.save_length, -1);
5188 XSETINT (buffer_local_flags.auto_save_file_name, -1);
5189 XSETINT (buffer_local_flags.read_only, -1);
5190 XSETINT (buffer_local_flags.major_mode, -1);
5191 XSETINT (buffer_local_flags.mode_name, -1);
5192 XSETINT (buffer_local_flags.undo_list, -1);
5193 XSETINT (buffer_local_flags.mark_active, -1);
5194 XSETINT (buffer_local_flags.point_before_scroll, -1);
5195 XSETINT (buffer_local_flags.file_truename, -1);
5196 XSETINT (buffer_local_flags.invisibility_spec, -1);
5197 XSETINT (buffer_local_flags.file_format, -1);
5198 XSETINT (buffer_local_flags.auto_save_file_format, -1);
5199 XSETINT (buffer_local_flags.display_count, -1);
5200 XSETINT (buffer_local_flags.display_time, -1);
5201 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
5202
5203 idx = 1;
5204 XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
5205 XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
5206 XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
5207 XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
5208 XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
5209 XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
5210 #ifndef old
5211 XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
5212 #endif
5213 XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
5214 XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
5215 XSETFASTINT (buffer_local_flags.word_wrap, idx); ++idx;
5216 XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
5217 XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
5218 XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
5219 XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
5220 XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
5221 #ifdef DOS_NT
5222 XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
5223 /* Make this one a permanent local. */
5224 buffer_permanent_local_flags[idx++] = 1;
5225 #endif
5226 XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
5227 XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
5228 XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
5229 XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
5230 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx);
5231 /* Make this one a permanent local. */
5232 buffer_permanent_local_flags[idx++] = 1;
5233 XSETFASTINT (buffer_local_flags.left_margin_cols, idx); ++idx;
5234 XSETFASTINT (buffer_local_flags.right_margin_cols, idx); ++idx;
5235 XSETFASTINT (buffer_local_flags.left_fringe_width, idx); ++idx;
5236 XSETFASTINT (buffer_local_flags.right_fringe_width, idx); ++idx;
5237 XSETFASTINT (buffer_local_flags.fringes_outside_margins, idx); ++idx;
5238 XSETFASTINT (buffer_local_flags.scroll_bar_width, idx); ++idx;
5239 XSETFASTINT (buffer_local_flags.vertical_scroll_bar_type, idx); ++idx;
5240 XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
5241 XSETFASTINT (buffer_local_flags.indicate_buffer_boundaries, idx); ++idx;
5242 XSETFASTINT (buffer_local_flags.fringe_indicator_alist, idx); ++idx;
5243 XSETFASTINT (buffer_local_flags.fringe_cursor_alist, idx); ++idx;
5244 XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
5245 XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
5246 XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
5247 XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
5248 XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
5249 XSETFASTINT (buffer_local_flags.cursor_in_non_selected_windows, idx); ++idx;
5250
5251 /* Need more room? */
5252 if (idx >= MAX_PER_BUFFER_VARS)
5253 abort ();
5254 last_per_buffer_idx = idx;
5255
5256 Vbuffer_alist = Qnil;
5257 current_buffer = 0;
5258 all_buffers = 0;
5259
5260 QSFundamental = build_string ("Fundamental");
5261
5262 Qfundamental_mode = intern ("fundamental-mode");
5263 buffer_defaults.major_mode = Qfundamental_mode;
5264
5265 Qmode_class = intern ("mode-class");
5266
5267 Qprotected_field = intern ("protected-field");
5268
5269 Qpermanent_local = intern ("permanent-local");
5270
5271 Qkill_buffer_hook = intern ("kill-buffer-hook");
5272 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
5273
5274 Qucs_set_table_for_input = intern ("ucs-set-table-for-input");
5275
5276 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
5277
5278 /* super-magic invisible buffer */
5279 Vbuffer_alist = Qnil;
5280
5281 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5282
5283 inhibit_modification_hooks = 0;
5284 }
5285
5286 void
5287 init_buffer ()
5288 {
5289 char *pwd;
5290 Lisp_Object temp;
5291 int len;
5292
5293 #ifdef USE_MMAP_FOR_BUFFERS
5294 {
5295 /* When using the ralloc implementation based on mmap(2), buffer
5296 text pointers will have been set to null in the dumped Emacs.
5297 Map new memory. */
5298 struct buffer *b;
5299
5300 for (b = all_buffers; b; b = b->next)
5301 if (b->text->beg == NULL)
5302 enlarge_buffer_text (b, 0);
5303 }
5304 #endif /* USE_MMAP_FOR_BUFFERS */
5305
5306 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
5307 if (NILP (buffer_defaults.enable_multibyte_characters))
5308 Fset_buffer_multibyte (Qnil);
5309
5310 pwd = get_current_dir_name ();
5311
5312 if (!pwd)
5313 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
5314
5315 /* Maybe this should really use some standard subroutine
5316 whose definition is filename syntax dependent. */
5317 len = strlen (pwd);
5318 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
5319 {
5320 /* Grow buffer to add directory separator and '\0'. */
5321 pwd = (char *) xrealloc (pwd, len + 2);
5322 pwd[len] = DIRECTORY_SEP;
5323 pwd[len + 1] = '\0';
5324 }
5325
5326 current_buffer->directory = make_unibyte_string (pwd, strlen (pwd));
5327 if (! NILP (buffer_defaults.enable_multibyte_characters))
5328 /* At this moment, we still don't know how to decode the
5329 directory name. So, we keep the bytes in multibyte form so
5330 that ENCODE_FILE correctly gets the original bytes. */
5331 current_buffer->directory
5332 = string_to_multibyte (current_buffer->directory);
5333
5334 /* Add /: to the front of the name
5335 if it would otherwise be treated as magic. */
5336 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
5337 if (! NILP (temp)
5338 /* If the default dir is just /, TEMP is non-nil
5339 because of the ange-ftp completion handler.
5340 However, it is not necessary to turn / into /:/.
5341 So avoid doing that. */
5342 && strcmp ("/", SDATA (current_buffer->directory)))
5343 current_buffer->directory
5344 = concat2 (build_string ("/:"), current_buffer->directory);
5345
5346 temp = get_minibuffer (0);
5347 XBUFFER (temp)->directory = current_buffer->directory;
5348
5349 free (pwd);
5350 }
5351
5352 /* Similar to defvar_lisp but define a variable whose value is the Lisp
5353 Object stored in the current buffer. address is the address of the slot
5354 in the buffer that is current now. */
5355
5356 /* TYPE is nil for a general Lisp variable.
5357 An integer specifies a type; then only LIsp values
5358 with that type code are allowed (except that nil is allowed too).
5359 LNAME is the LIsp-level variable name.
5360 VNAME is the name of the buffer slot.
5361 DOC is a dummy where you write the doc string as a comment. */
5362 #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5363 defvar_per_buffer (lname, vname, type, 0)
5364
5365 static void
5366 defvar_per_buffer (namestring, address, type, doc)
5367 char *namestring;
5368 Lisp_Object *address;
5369 Lisp_Object type;
5370 char *doc;
5371 {
5372 Lisp_Object sym, val;
5373 int offset;
5374
5375 sym = intern (namestring);
5376 val = allocate_misc ();
5377 offset = (char *)address - (char *)current_buffer;
5378
5379 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
5380 XBUFFER_OBJFWD (val)->offset = offset;
5381 XBUFFER_OBJFWD (val)->slottype = type;
5382 SET_SYMBOL_VALUE (sym, val);
5383 PER_BUFFER_SYMBOL (offset) = sym;
5384
5385 if (PER_BUFFER_IDX (offset) == 0)
5386 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5387 slot of buffer_local_flags */
5388 abort ();
5389 }
5390
5391
5392 /* initialize the buffer routines */
5393 void
5394 syms_of_buffer ()
5395 {
5396 staticpro (&last_overlay_modification_hooks);
5397 last_overlay_modification_hooks
5398 = Fmake_vector (make_number (10), Qnil);
5399
5400 staticpro (&Vbuffer_defaults);
5401 staticpro (&Vbuffer_local_symbols);
5402 staticpro (&Qfundamental_mode);
5403 staticpro (&Qmode_class);
5404 staticpro (&QSFundamental);
5405 staticpro (&Vbuffer_alist);
5406 staticpro (&Qprotected_field);
5407 staticpro (&Qpermanent_local);
5408 Qpermanent_local_hook = intern ("permanent-local-hook");
5409 staticpro (&Qpermanent_local_hook);
5410 staticpro (&Qkill_buffer_hook);
5411 Qoverlayp = intern ("overlayp");
5412 staticpro (&Qoverlayp);
5413 Qevaporate = intern ("evaporate");
5414 staticpro (&Qevaporate);
5415 Qmodification_hooks = intern ("modification-hooks");
5416 staticpro (&Qmodification_hooks);
5417 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
5418 staticpro (&Qinsert_in_front_hooks);
5419 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5420 staticpro (&Qinsert_behind_hooks);
5421 Qget_file_buffer = intern ("get-file-buffer");
5422 staticpro (&Qget_file_buffer);
5423 Qpriority = intern ("priority");
5424 staticpro (&Qpriority);
5425 Qwindow = intern ("window");
5426 staticpro (&Qwindow);
5427 Qbefore_string = intern ("before-string");
5428 staticpro (&Qbefore_string);
5429 Qafter_string = intern ("after-string");
5430 staticpro (&Qafter_string);
5431 Qfirst_change_hook = intern ("first-change-hook");
5432 staticpro (&Qfirst_change_hook);
5433 Qbefore_change_functions = intern ("before-change-functions");
5434 staticpro (&Qbefore_change_functions);
5435 Qafter_change_functions = intern ("after-change-functions");
5436 staticpro (&Qafter_change_functions);
5437 /* The next one is initialized in init_buffer_once. */
5438 staticpro (&Qucs_set_table_for_input);
5439
5440 Qkill_buffer_query_functions = intern ("kill-buffer-query-functions");
5441 staticpro (&Qkill_buffer_query_functions);
5442
5443 Fput (Qprotected_field, Qerror_conditions,
5444 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
5445 Fput (Qprotected_field, Qerror_message,
5446 build_string ("Attempt to modify a protected field"));
5447
5448 /* All these use DEFVAR_LISP_NOPRO because the slots in
5449 buffer_defaults will all be marked via Vbuffer_defaults. */
5450
5451 DEFVAR_LISP_NOPRO ("default-mode-line-format",
5452 &buffer_defaults.mode_line_format,
5453 doc: /* Default value of `mode-line-format' for buffers that don't override it.
5454 This is the same as (default-value 'mode-line-format). */);
5455
5456 DEFVAR_LISP_NOPRO ("default-header-line-format",
5457 &buffer_defaults.header_line_format,
5458 doc: /* Default value of `header-line-format' for buffers that don't override it.
5459 This is the same as (default-value 'header-line-format). */);
5460
5461 DEFVAR_LISP_NOPRO ("default-cursor-type", &buffer_defaults.cursor_type,
5462 doc: /* Default value of `cursor-type' for buffers that don't override it.
5463 This is the same as (default-value 'cursor-type). */);
5464
5465 DEFVAR_LISP_NOPRO ("default-line-spacing",
5466 &buffer_defaults.extra_line_spacing,
5467 doc: /* Default value of `line-spacing' for buffers that don't override it.
5468 This is the same as (default-value 'line-spacing). */);
5469
5470 DEFVAR_LISP_NOPRO ("default-cursor-in-non-selected-windows",
5471 &buffer_defaults.cursor_in_non_selected_windows,
5472 doc: /* Default value of `cursor-in-non-selected-windows'.
5473 This is the same as (default-value 'cursor-in-non-selected-windows). */);
5474
5475 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
5476 &buffer_defaults.abbrev_mode,
5477 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
5478 This is the same as (default-value 'abbrev-mode). */);
5479
5480 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
5481 &buffer_defaults.ctl_arrow,
5482 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
5483 This is the same as (default-value 'ctl-arrow). */);
5484
5485 DEFVAR_LISP_NOPRO ("default-direction-reversed",
5486 &buffer_defaults.direction_reversed,
5487 doc: /* Default value of `direction-reversed' for buffers that do not override it.
5488 This is the same as (default-value 'direction-reversed). */);
5489
5490 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
5491 &buffer_defaults.enable_multibyte_characters,
5492 doc: /* *Default value of `enable-multibyte-characters' for buffers not overriding it.
5493 This is the same as (default-value 'enable-multibyte-characters). */);
5494
5495 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
5496 &buffer_defaults.buffer_file_coding_system,
5497 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
5498 This is the same as (default-value 'buffer-file-coding-system). */);
5499
5500 DEFVAR_LISP_NOPRO ("default-truncate-lines",
5501 &buffer_defaults.truncate_lines,
5502 doc: /* Default value of `truncate-lines' for buffers that do not override it.
5503 This is the same as (default-value 'truncate-lines). */);
5504
5505 DEFVAR_LISP_NOPRO ("default-fill-column",
5506 &buffer_defaults.fill_column,
5507 doc: /* Default value of `fill-column' for buffers that do not override it.
5508 This is the same as (default-value 'fill-column). */);
5509
5510 DEFVAR_LISP_NOPRO ("default-left-margin",
5511 &buffer_defaults.left_margin,
5512 doc: /* Default value of `left-margin' for buffers that do not override it.
5513 This is the same as (default-value 'left-margin). */);
5514
5515 DEFVAR_LISP_NOPRO ("default-tab-width",
5516 &buffer_defaults.tab_width,
5517 doc: /* Default value of `tab-width' for buffers that do not override it.
5518 This is the same as (default-value 'tab-width). */);
5519
5520 DEFVAR_LISP_NOPRO ("default-case-fold-search",
5521 &buffer_defaults.case_fold_search,
5522 doc: /* Default value of `case-fold-search' for buffers that don't override it.
5523 This is the same as (default-value 'case-fold-search). */);
5524
5525 #ifdef DOS_NT
5526 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
5527 &buffer_defaults.buffer_file_type,
5528 doc: /* Default file type for buffers that do not override it.
5529 This is the same as (default-value 'buffer-file-type).
5530 The file type is nil for text, t for binary. */);
5531 #endif
5532
5533 DEFVAR_LISP_NOPRO ("default-left-margin-width",
5534 &buffer_defaults.left_margin_cols,
5535 doc: /* Default value of `left-margin-width' for buffers that don't override it.
5536 This is the same as (default-value 'left-margin-width). */);
5537
5538 DEFVAR_LISP_NOPRO ("default-right-margin-width",
5539 &buffer_defaults.right_margin_cols,
5540 doc: /* Default value of `right-margin-width' for buffers that don't override it.
5541 This is the same as (default-value 'right-margin-width). */);
5542
5543 DEFVAR_LISP_NOPRO ("default-left-fringe-width",
5544 &buffer_defaults.left_fringe_width,
5545 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
5546 This is the same as (default-value 'left-fringe-width). */);
5547
5548 DEFVAR_LISP_NOPRO ("default-right-fringe-width",
5549 &buffer_defaults.right_fringe_width,
5550 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
5551 This is the same as (default-value 'right-fringe-width). */);
5552
5553 DEFVAR_LISP_NOPRO ("default-fringes-outside-margins",
5554 &buffer_defaults.fringes_outside_margins,
5555 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
5556 This is the same as (default-value 'fringes-outside-margins). */);
5557
5558 DEFVAR_LISP_NOPRO ("default-scroll-bar-width",
5559 &buffer_defaults.scroll_bar_width,
5560 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
5561 This is the same as (default-value 'scroll-bar-width). */);
5562
5563 DEFVAR_LISP_NOPRO ("default-vertical-scroll-bar",
5564 &buffer_defaults.vertical_scroll_bar_type,
5565 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
5566 This is the same as (default-value 'vertical-scroll-bar). */);
5567
5568 DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
5569 &buffer_defaults.indicate_empty_lines,
5570 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
5571 This is the same as (default-value 'indicate-empty-lines). */);
5572
5573 DEFVAR_LISP_NOPRO ("default-indicate-buffer-boundaries",
5574 &buffer_defaults.indicate_buffer_boundaries,
5575 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
5576 This is the same as (default-value 'indicate-buffer-boundaries). */);
5577
5578 DEFVAR_LISP_NOPRO ("default-fringe-indicator-alist",
5579 &buffer_defaults.fringe_indicator_alist,
5580 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
5581 This is the same as (default-value 'fringe-indicator-alist'). */);
5582
5583 DEFVAR_LISP_NOPRO ("default-fringe-cursor-alist",
5584 &buffer_defaults.fringe_cursor_alist,
5585 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
5586 This is the same as (default-value 'fringe-cursor-alist'). */);
5587
5588 DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
5589 &buffer_defaults.scroll_up_aggressively,
5590 doc: /* Default value of `scroll-up-aggressively'.
5591 This value applies in buffers that don't have their own local values.
5592 This is the same as (default-value 'scroll-up-aggressively). */);
5593
5594 DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
5595 &buffer_defaults.scroll_down_aggressively,
5596 doc: /* Default value of `scroll-down-aggressively'.
5597 This value applies in buffers that don't have their own local values.
5598 This is the same as (default-value 'scroll-down-aggressively). */);
5599
5600 DEFVAR_PER_BUFFER ("header-line-format",
5601 &current_buffer->header_line_format,
5602 Qnil,
5603 doc: /* Analogous to `mode-line-format', but controls the header line.
5604 The header line appears, optionally, at the top of a window;
5605 the mode line appears at the bottom. */);
5606
5607 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
5608 Qnil,
5609 doc: /* Template for displaying mode line for current buffer.
5610 Each buffer has its own value of this variable.
5611 Value may be nil, a string, a symbol or a list or cons cell.
5612 A value of nil means don't display a mode line.
5613 For a symbol, its value is used (but it is ignored if t or nil).
5614 A string appearing directly as the value of a symbol is processed verbatim
5615 in that the %-constructs below are not recognized.
5616 Note that unless the symbol is marked as a `risky-local-variable', all
5617 properties in any strings, as well as all :eval and :propertize forms
5618 in the value of that symbol will be ignored.
5619 For a list of the form `(:eval FORM)', FORM is evaluated and the result
5620 is used as a mode line element. Be careful--FORM should not load any files,
5621 because that can cause an infinite recursion.
5622 For a list of the form `(:propertize ELT PROPS...)', ELT is displayed
5623 with the specified properties PROPS applied.
5624 For a list whose car is a symbol, the symbol's value is taken,
5625 and if that is non-nil, the cadr of the list is processed recursively.
5626 Otherwise, the caddr of the list (if there is one) is processed.
5627 For a list whose car is a string or list, each element is processed
5628 recursively and the results are effectively concatenated.
5629 For a list whose car is an integer, the cdr of the list is processed
5630 and padded (if the number is positive) or truncated (if negative)
5631 to the width specified by that number.
5632 A string is printed verbatim in the mode line except for %-constructs:
5633 (%-constructs are allowed when the string is the entire mode-line-format
5634 or when it is found in a cons-cell or a list)
5635 %b -- print buffer name. %f -- print visited file name.
5636 %F -- print frame name.
5637 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5638 %& is like %*, but ignore read-only-ness.
5639 % means buffer is read-only and * means it is modified.
5640 For a modified read-only buffer, %* gives % and %+ gives *.
5641 %s -- print process status. %l -- print the current line number.
5642 %c -- print the current column number (this makes editing slower).
5643 To make the column number update correctly in all cases,
5644 `column-number-mode' must be non-nil.
5645 %i -- print the size of the buffer.
5646 %I -- like %i, but use k, M, G, etc., to abbreviate.
5647 %p -- print percent of buffer above top of window, or Top, Bot or All.
5648 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5649 or print Bottom or All.
5650 %n -- print Narrow if appropriate.
5651 %t -- visited file is text or binary (if OS supports this distinction).
5652 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
5653 %Z -- like %z, but including the end-of-line format.
5654 %e -- print error message about full memory.
5655 %@ -- print @ or hyphen. @ means that default-directory is on a
5656 remote machine.
5657 %[ -- print one [ for each recursive editing level. %] similar.
5658 %% -- print %. %- -- print infinitely many dashes.
5659 Decimal digits after the % specify field width to which to pad. */);
5660
5661 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
5662 doc: /* *Major mode for new buffers. Defaults to `fundamental-mode'.
5663 A value of nil means use current buffer's major mode,
5664 provided it is not marked as "special".
5665
5666 When a mode is used by default, `find-file' switches to it
5667 before it reads the contents into the buffer and before
5668 it finishes setting up the buffer. Thus, the mode and
5669 its hooks should not expect certain variables such as
5670 `buffer-read-only' and `buffer-file-coding-system' to be set up. */);
5671
5672 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
5673 make_number (Lisp_Symbol),
5674 doc: /* Symbol for current buffer's major mode. */);
5675
5676 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
5677 Qnil,
5678 doc: /* Pretty name of current buffer's major mode.
5679 Usually a string. See `mode-line-format' for other possible forms.
5680 Use the function `format-mode-line' to get the value as a string. */);
5681
5682 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
5683 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5684
5685 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
5686 doc: /* Non-nil turns on automatic expansion of abbrevs as they are inserted. */);
5687
5688 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
5689 Qnil,
5690 doc: /* *Non-nil if searches and matches should ignore case. */);
5691
5692 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
5693 make_number (Lisp_Int),
5694 doc: /* *Column beyond which automatic line-wrapping should happen.
5695 Interactively, you can set the buffer local value using \\[set-fill-column]. */);
5696
5697 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
5698 make_number (Lisp_Int),
5699 doc: /* *Column for the default `indent-line-function' to indent to.
5700 Linefeed indents to this column in Fundamental mode. */);
5701
5702 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
5703 make_number (Lisp_Int),
5704 doc: /* *Distance between tab stops (for display of tab characters), in columns. */);
5705
5706 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
5707 doc: /* *Non-nil means display control chars with uparrow.
5708 A value of nil means use backslash and octal digits.
5709 This variable does not apply to characters whose display is specified
5710 in the current display table (if there is one). */);
5711
5712 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
5713 &current_buffer->enable_multibyte_characters,
5714 Qnil,
5715 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
5716 Otherwise they are regarded as unibyte. This affects the display,
5717 file I/O and the behavior of various editing commands.
5718
5719 This variable is buffer-local but you cannot set it directly;
5720 use the function `set-buffer-multibyte' to change a buffer's representation.
5721 Changing its default value with `setq-default' is supported.
5722 See also variable `default-enable-multibyte-characters' and Info node
5723 `(elisp)Text Representations'. */);
5724 XSYMBOL (intern ("enable-multibyte-characters"))->constant = 1;
5725
5726 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
5727 &current_buffer->buffer_file_coding_system, Qnil,
5728 doc: /* Coding system to be used for encoding the buffer contents on saving.
5729 This variable applies to saving the buffer, and also to `write-region'
5730 and other functions that use `write-region'.
5731 It does not apply to sending output to subprocesses, however.
5732
5733 If this is nil, the buffer is saved without any code conversion
5734 unless some coding system is specified in `file-coding-system-alist'
5735 for the buffer file.
5736
5737 If the text to be saved cannot be encoded as specified by this variable,
5738 an alternative encoding is selected by `select-safe-coding-system', which see.
5739
5740 The variable `coding-system-for-write', if non-nil, overrides this variable.
5741
5742 This variable is never applied to a way of decoding a file while reading it. */);
5743
5744 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
5745 Qnil,
5746 doc: /* *Non-nil means lines in the buffer are displayed right to left. */);
5747
5748 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
5749 doc: /* *Non-nil means do not display continuation lines.
5750 Instead, give each line of text just one screen line.
5751
5752 Note that this is overridden by the variable
5753 `truncate-partial-width-windows' if that variable is non-nil
5754 and this buffer is not full-frame width. */);
5755
5756 DEFVAR_PER_BUFFER ("word-wrap", &current_buffer->word_wrap, Qnil,
5757 doc: /* *Non-nil means to use word-wrapping for continuation lines.
5758 When word-wrapping is on, continuation lines are wrapped at the space
5759 or tab character nearest to the right window edge.
5760 If nil, continuation lines are wrapped at the right screen edge.
5761
5762 This variable has no effect if long lines are truncated (see
5763 `truncate-lines' and `truncate-partial-width-windows'). */);
5764
5765 #ifdef DOS_NT
5766 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
5767 Qnil,
5768 doc: /* Non-nil if the visited file is a binary file.
5769 This variable is meaningful on MS-DOG and Windows NT.
5770 On those systems, it is automatically local in every buffer.
5771 On other systems, this variable is normally always nil. */);
5772 #endif
5773
5774 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
5775 make_number (Lisp_String),
5776 doc: /* Name of default directory of current buffer. Should end with slash.
5777 To interactively change the default directory, use command `cd'. */);
5778
5779 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
5780 Qnil,
5781 doc: /* Function called (if non-nil) to perform auto-fill.
5782 It is called after self-inserting any character specified in
5783 the `auto-fill-chars' table.
5784 NOTE: This variable is not a hook;
5785 its value may not be a list of functions. */);
5786
5787 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
5788 make_number (Lisp_String),
5789 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
5790
5791 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
5792 make_number (Lisp_String),
5793 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
5794 The truename of a file is calculated by `file-truename'
5795 and then abbreviated with `abbreviate-file-name'. */);
5796
5797 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
5798 &current_buffer->auto_save_file_name,
5799 make_number (Lisp_String),
5800 doc: /* Name of file for auto-saving current buffer.
5801 If it is nil, that means don't auto-save this buffer. */);
5802
5803 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
5804 doc: /* Non-nil if this buffer is read-only. */);
5805
5806 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
5807 doc: /* Non-nil if this buffer's file has been backed up.
5808 Backing up is done before the first time the file is saved. */);
5809
5810 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
5811 make_number (Lisp_Int),
5812 doc: /* Length of current buffer when last read in, saved or auto-saved.
5813 0 initially. */);
5814
5815 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
5816 Qnil,
5817 doc: /* Non-nil enables selective display.
5818 An integer N as value means display only lines
5819 that start with less than N columns of space.
5820 A value of t means that the character ^M makes itself and
5821 all the rest of the line invisible; also, when saving the buffer
5822 in a file, save the ^M as a newline. */);
5823
5824 #ifndef old
5825 DEFVAR_PER_BUFFER ("selective-display-ellipses",
5826 &current_buffer->selective_display_ellipses,
5827 Qnil,
5828 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
5829 #endif
5830
5831 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
5832 doc: /* Non-nil if self-insertion should replace existing text.
5833 The value should be one of `overwrite-mode-textual',
5834 `overwrite-mode-binary', or nil.
5835 If it is `overwrite-mode-textual', self-insertion still
5836 inserts at the end of a line, and inserts when point is before a tab,
5837 until the tab is filled in.
5838 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5839
5840 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5841 Qnil,
5842 doc: /* Display table that controls display of the contents of current buffer.
5843
5844 If this variable is nil, the value of `standard-display-table' is used.
5845 Each window can have its own, overriding display table, see
5846 `set-window-display-table' and `window-display-table'.
5847
5848 The display table is a char-table created with `make-display-table'.
5849 A char-table is an array indexed by character codes. Normal array
5850 primitives `aref' and `aset' can be used to access elements of a char-table.
5851
5852 Each of the char-table elements control how to display the corresponding
5853 text character: the element at index C in the table says how to display
5854 the character whose code is C. Each element should be a vector of
5855 characters or nil. The value nil means display the character in the
5856 default fashion; otherwise, the characters from the vector are delivered
5857 to the screen instead of the original character.
5858
5859 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
5860 to display a capital Y instead of each X character.
5861
5862 In addition, a char-table has six extra slots to control the display of:
5863
5864 the end of a truncated screen line (extra-slot 0, a single character);
5865 the end of a continued line (extra-slot 1, a single character);
5866 the escape character used to display character codes in octal
5867 (extra-slot 2, a single character);
5868 the character used as an arrow for control characters (extra-slot 3,
5869 a single character);
5870 the decoration indicating the presence of invisible lines (extra-slot 4,
5871 a vector of characters);
5872 the character used to draw the border between side-by-side windows
5873 (extra-slot 5, a single character).
5874
5875 See also the functions `display-table-slot' and `set-display-table-slot'. */);
5876
5877 DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_cols,
5878 Qnil,
5879 doc: /* *Width of left marginal area for display of a buffer.
5880 A value of nil means no marginal area. */);
5881
5882 DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_cols,
5883 Qnil,
5884 doc: /* *Width of right marginal area for display of a buffer.
5885 A value of nil means no marginal area. */);
5886
5887 DEFVAR_PER_BUFFER ("left-fringe-width", &current_buffer->left_fringe_width,
5888 Qnil,
5889 doc: /* *Width of this buffer's left fringe (in pixels).
5890 A value of 0 means no left fringe is shown in this buffer's window.
5891 A value of nil means to use the left fringe width from the window's frame. */);
5892
5893 DEFVAR_PER_BUFFER ("right-fringe-width", &current_buffer->right_fringe_width,
5894 Qnil,
5895 doc: /* *Width of this buffer's right fringe (in pixels).
5896 A value of 0 means no right fringe is shown in this buffer's window.
5897 A value of nil means to use the right fringe width from the window's frame. */);
5898
5899 DEFVAR_PER_BUFFER ("fringes-outside-margins", &current_buffer->fringes_outside_margins,
5900 Qnil,
5901 doc: /* *Non-nil means to display fringes outside display margins.
5902 A value of nil means to display fringes between margins and buffer text. */);
5903
5904 DEFVAR_PER_BUFFER ("scroll-bar-width", &current_buffer->scroll_bar_width,
5905 Qnil,
5906 doc: /* *Width of this buffer's scroll bars in pixels.
5907 A value of nil means to use the scroll bar width from the window's frame. */);
5908
5909 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &current_buffer->vertical_scroll_bar_type,
5910 Qnil,
5911 doc: /* *Position of this buffer's vertical scroll bar.
5912 The value takes effect whenever you tell a window to display this buffer;
5913 for instance, with `set-window-buffer' or when `display-buffer' displays it.
5914
5915 A value of `left' or `right' means put the vertical scroll bar at that side
5916 of the window; a value of nil means don't show any vertical scroll bars.
5917 A value of t (the default) means do whatever the window's frame specifies. */);
5918
5919 DEFVAR_PER_BUFFER ("indicate-empty-lines",
5920 &current_buffer->indicate_empty_lines, Qnil,
5921 doc: /* *Visually indicate empty lines after the buffer end.
5922 If non-nil, a bitmap is displayed in the left fringe of a window on
5923 window-systems. */);
5924
5925 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
5926 &current_buffer->indicate_buffer_boundaries, Qnil,
5927 doc: /* *Visually indicate buffer boundaries and scrolling.
5928 If non-nil, the first and last line of the buffer are marked in the fringe
5929 of a window on window-systems with angle bitmaps, or if the window can be
5930 scrolled, the top and bottom line of the window are marked with up and down
5931 arrow bitmaps.
5932
5933 If value is a symbol `left' or `right', both angle and arrow bitmaps
5934 are displayed in the left or right fringe, resp. Any other value
5935 that doesn't look like an alist means display the angle bitmaps in
5936 the left fringe but no arrows.
5937
5938 You can exercise more precise control by using an alist as the
5939 value. Each alist element (INDICATOR . POSITION) specifies
5940 where to show one of the indicators. INDICATOR is one of `top',
5941 `bottom', `up', `down', or t, which specifies the default position,
5942 and POSITION is one of `left', `right', or nil, meaning do not show
5943 this indicator.
5944
5945 For example, ((top . left) (t . right)) places the top angle bitmap in
5946 left fringe, the bottom angle bitmap in right fringe, and both arrow
5947 bitmaps in right fringe. To show just the angle bitmaps in the left
5948 fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
5949
5950 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
5951 &current_buffer->fringe_indicator_alist, Qnil,
5952 doc: /* *Mapping from logical to physical fringe indicator bitmaps.
5953 The value is an alist where each element (INDICATOR . BITMAPS)
5954 specifies the fringe bitmaps used to display a specific logical
5955 fringe indicator.
5956
5957 INDICATOR specifies the logical indicator type which is one of the
5958 following symbols: `truncation' , `continuation', `overlay-arrow',
5959 `top', `bottom', `up', `down', `one-line', `empty-line', or `unknown'.
5960
5961 BITMAPS is list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
5962 the actual bitmap shown in the left or right fringe for the logical
5963 indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5964 right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
5965 are used only for the `bottom' and `one-line' indicators when the last
5966 \(only) line in has no final newline. BITMAPS may also be a single
5967 symbol which is used in both left and right fringes. */);
5968
5969 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
5970 &current_buffer->fringe_cursor_alist, Qnil,
5971 doc: /* *Mapping from logical to physical fringe cursor bitmaps.
5972 The value is an alist where each element (CURSOR . BITMAP)
5973 specifies the fringe bitmaps used to display a specific logical
5974 cursor type in the fringe.
5975
5976 CURSOR specifies the logical cursor type which is one of the following
5977 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5978 one is used to show a hollow cursor on narrow lines display lines
5979 where the normal hollow cursor will not fit.
5980
5981 BITMAP is the corresponding fringe bitmap shown for the logical
5982 cursor type. */);
5983
5984 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
5985 &current_buffer->scroll_up_aggressively, Qnil,
5986 doc: /* How far to scroll windows upward.
5987 If you move point off the bottom, the window scrolls automatically.
5988 This variable controls how far it scrolls. The value nil, the default,
5989 means scroll to center point. A fraction means scroll to put point
5990 that fraction of the window's height from the bottom of the window.
5991 When the value is 0.0, point goes at the bottom line, which in the simple
5992 case that you moved off with C-f means scrolling just one line. 1.0 means
5993 point goes at the top, so that in that simple case, the window
5994 scrolls by a full window height. Meaningful values are
5995 between 0.0 and 1.0, inclusive. */);
5996
5997 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
5998 &current_buffer->scroll_down_aggressively, Qnil,
5999 doc: /* How far to scroll windows downward.
6000 If you move point off the top, the window scrolls automatically.
6001 This variable controls how far it scrolls. The value nil, the default,
6002 means scroll to center point. A fraction means scroll to put point
6003 that fraction of the window's height from the top of the window.
6004 When the value is 0.0, point goes at the top line, which in the simple
6005 case that you moved off with C-b means scrolling just one line. 1.0 means
6006 point goes at the bottom, so that in that simple case, the window
6007 scrolls by a full window height. Meaningful values are
6008 between 0.0 and 1.0, inclusive. */);
6009
6010 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6011 "Don't ask.");
6012 */
6013
6014 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
6015 doc: /* List of functions to call before each text change.
6016 Two arguments are passed to each function: the positions of
6017 the beginning and end of the range of old text to be changed.
6018 \(For an insertion, the beginning and end are at the same place.)
6019 No information is given about the length of the text after the change.
6020
6021 Buffer changes made while executing the `before-change-functions'
6022 don't call any before-change or after-change functions.
6023 That's because these variables are temporarily set to nil.
6024 As a result, a hook function cannot straightforwardly alter the value of
6025 these variables. See the Emacs Lisp manual for a way of
6026 accomplishing an equivalent result by using other variables.
6027
6028 If an unhandled error happens in running these functions,
6029 the variable's value remains nil. That prevents the error
6030 from happening repeatedly and making Emacs nonfunctional. */);
6031 Vbefore_change_functions = Qnil;
6032
6033 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
6034 doc: /* List of functions to call after each text change.
6035 Three arguments are passed to each function: the positions of
6036 the beginning and end of the range of changed text,
6037 and the length in bytes of the pre-change text replaced by that range.
6038 \(For an insertion, the pre-change length is zero;
6039 for a deletion, that length is the number of bytes deleted,
6040 and the post-change beginning and end are at the same place.)
6041
6042 Buffer changes made while executing the `after-change-functions'
6043 don't call any before-change or after-change functions.
6044 That's because these variables are temporarily set to nil.
6045 As a result, a hook function cannot straightforwardly alter the value of
6046 these variables. See the Emacs Lisp manual for a way of
6047 accomplishing an equivalent result by using other variables.
6048
6049 If an unhandled error happens in running these functions,
6050 the variable's value remains nil. That prevents the error
6051 from happening repeatedly and making Emacs nonfunctional. */);
6052 Vafter_change_functions = Qnil;
6053
6054 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
6055 doc: /* A list of functions to call before changing a buffer which is unmodified.
6056 The functions are run using the `run-hooks' function. */);
6057 Vfirst_change_hook = Qnil;
6058
6059 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
6060 doc: /* List of undo entries in current buffer.
6061 Recent changes come first; older changes follow newer.
6062
6063 An entry (BEG . END) represents an insertion which begins at
6064 position BEG and ends at position END.
6065
6066 An entry (TEXT . POSITION) represents the deletion of the string TEXT
6067 from (abs POSITION). If POSITION is positive, point was at the front
6068 of the text being deleted; if negative, point was at the end.
6069
6070 An entry (t HIGH . LOW) indicates that the buffer previously had
6071 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions
6072 of the visited file's modification time, as of that time. If the
6073 modification time of the most recent save is different, this entry is
6074 obsolete.
6075
6076 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6077 was modified between BEG and END. PROPERTY is the property name,
6078 and VALUE is the old value.
6079
6080 An entry (apply FUN-NAME . ARGS) means undo the change with
6081 \(apply FUN-NAME ARGS).
6082
6083 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6084 in the active region. BEG and END is the range affected by this entry
6085 and DELTA is the number of bytes added or deleted in that range by
6086 this change.
6087
6088 An entry (MARKER . DISTANCE) indicates that the marker MARKER
6089 was adjusted in position by the offset DISTANCE (an integer).
6090
6091 An entry of the form POSITION indicates that point was at the buffer
6092 location given by the integer. Undoing an entry of this form places
6093 point at POSITION.
6094
6095 Entries with value `nil' mark undo boundaries. The undo command treats
6096 the changes between two undo boundaries as a single step to be undone.
6097
6098 If the value of the variable is t, undo information is not recorded. */);
6099
6100 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
6101 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
6102
6103 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
6104 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
6105
6106 Normally, the line-motion functions work by scanning the buffer for
6107 newlines. Columnar operations (like `move-to-column' and
6108 `compute-motion') also work by scanning the buffer, summing character
6109 widths as they go. This works well for ordinary text, but if the
6110 buffer's lines are very long (say, more than 500 characters), these
6111 motion functions will take longer to execute. Emacs may also take
6112 longer to update the display.
6113
6114 If `cache-long-line-scans' is non-nil, these motion functions cache the
6115 results of their scans, and consult the cache to avoid rescanning
6116 regions of the buffer until the text is modified. The caches are most
6117 beneficial when they prevent the most searching---that is, when the
6118 buffer contains long lines and large regions of characters with the
6119 same, fixed screen width.
6120
6121 When `cache-long-line-scans' is non-nil, processing short lines will
6122 become slightly slower (because of the overhead of consulting the
6123 cache), and the caches will use memory roughly proportional to the
6124 number of newlines and characters whose screen width varies.
6125
6126 The caches require no explicit maintenance; their accuracy is
6127 maintained internally by the Emacs primitives. Enabling or disabling
6128 the cache should not affect the behavior of any of the motion
6129 functions; it should only affect their performance. */);
6130
6131 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
6132 doc: /* Value of point before the last series of scroll operations, or nil. */);
6133
6134 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
6135 doc: /* List of formats to use when saving this buffer.
6136 Formats are defined by `format-alist'. This variable is
6137 set when a file is visited. */);
6138
6139 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
6140 &current_buffer->auto_save_file_format, Qnil,
6141 doc: /* *Format in which to write auto-save files.
6142 Should be a list of symbols naming formats that are defined in `format-alist'.
6143 If it is t, which is the default, auto-save files are written in the
6144 same format as a regular save would use. */);
6145
6146 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
6147 &current_buffer->invisibility_spec, Qnil,
6148 doc: /* Invisibility spec of this buffer.
6149 The default is t, which means that text is invisible
6150 if it has a non-nil `invisible' property.
6151 If the value is a list, a text character is invisible if its `invisible'
6152 property is an element in that list (or is a list with members in common).
6153 If an element is a cons cell of the form (PROP . ELLIPSIS),
6154 then characters with property value PROP are invisible,
6155 and they have an ellipsis as well if ELLIPSIS is non-nil. */);
6156
6157 DEFVAR_PER_BUFFER ("buffer-display-count",
6158 &current_buffer->display_count, Qnil,
6159 doc: /* A number incremented each time this buffer is displayed in a window.
6160 The function `set-window-buffer' increments it. */);
6161
6162 DEFVAR_PER_BUFFER ("buffer-display-time",
6163 &current_buffer->display_time, Qnil,
6164 doc: /* Time stamp updated each time this buffer is displayed in a window.
6165 The function `set-window-buffer' updates this variable
6166 to the value obtained by calling `current-time'.
6167 If the buffer has never been shown in a window, the value is nil. */);
6168
6169 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
6170 doc: /* */);
6171 Vtransient_mark_mode = Qnil;
6172 /* The docstring is in simple.el. If we put it here, it would be
6173 overwritten when transient-mark-mode is defined using
6174 define-minor-mode. */
6175
6176 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
6177 doc: /* *Non-nil means disregard read-only status of buffers or characters.
6178 If the value is t, disregard `buffer-read-only' and all `read-only'
6179 text properties. If the value is a list, disregard `buffer-read-only'
6180 and disregard a `read-only' text property if the property value
6181 is a member of the list. */);
6182 Vinhibit_read_only = Qnil;
6183
6184 DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
6185 doc: /* Cursor to use when this buffer is in the selected window.
6186 Values are interpreted as follows:
6187
6188 t use the cursor specified for the frame
6189 nil don't display a cursor
6190 box display a filled box cursor
6191 hollow display a hollow box cursor
6192 bar display a vertical bar cursor with default width
6193 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6194 hbar display a horizontal bar cursor with default height
6195 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
6196 ANYTHING ELSE display a hollow box cursor
6197
6198 When the buffer is displayed in a nonselected window,
6199 this variable has no effect; the cursor appears as a hollow box. */);
6200
6201 DEFVAR_PER_BUFFER ("line-spacing",
6202 &current_buffer->extra_line_spacing, Qnil,
6203 doc: /* Additional space to put between lines when displaying a buffer.
6204 The space is measured in pixels, and put below lines on window systems.
6205 If value is a floating point number, it specifies the spacing relative
6206 to the default frame line height. A value of nil means add no extra space. */);
6207
6208 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
6209 &current_buffer->cursor_in_non_selected_windows, Qnil,
6210 doc: /* *Cursor type to display in non-selected windows.
6211 The value t means to use hollow box cursor. See `cursor-type' for other values. */);
6212
6213 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
6214 doc: /* List of functions called with no args to query before killing a buffer.
6215 The buffer being killed will be current while the functions are running.
6216 If any of them returns nil, the buffer is not killed. */);
6217 Vkill_buffer_query_functions = Qnil;
6218
6219 DEFVAR_LISP ("change-major-mode-hook", &Vchange_major_mode_hook,
6220 doc: /* Normal hook run before changing the major mode of a buffer.
6221 The function `kill-all-local-variables' runs this before doing anything else. */);
6222 Vchange_major_mode_hook = Qnil;
6223 Qchange_major_mode_hook = intern ("change-major-mode-hook");
6224 staticpro (&Qchange_major_mode_hook);
6225
6226 defsubr (&Sbuffer_live_p);
6227 defsubr (&Sbuffer_list);
6228 defsubr (&Sget_buffer);
6229 defsubr (&Sget_file_buffer);
6230 defsubr (&Sget_buffer_create);
6231 defsubr (&Smake_indirect_buffer);
6232 defsubr (&Sgenerate_new_buffer_name);
6233 defsubr (&Sbuffer_name);
6234 /*defsubr (&Sbuffer_number);*/
6235 defsubr (&Sbuffer_file_name);
6236 defsubr (&Sbuffer_base_buffer);
6237 defsubr (&Sbuffer_local_value);
6238 defsubr (&Sbuffer_local_variables);
6239 defsubr (&Sbuffer_modified_p);
6240 defsubr (&Sset_buffer_modified_p);
6241 defsubr (&Sbuffer_modified_tick);
6242 defsubr (&Sbuffer_chars_modified_tick);
6243 defsubr (&Srename_buffer);
6244 defsubr (&Sother_buffer);
6245 defsubr (&Sbuffer_enable_undo);
6246 defsubr (&Skill_buffer);
6247 defsubr (&Sset_buffer_major_mode);
6248 defsubr (&Sswitch_to_buffer);
6249 defsubr (&Scurrent_buffer);
6250 defsubr (&Sset_buffer);
6251 defsubr (&Sbarf_if_buffer_read_only);
6252 defsubr (&Sbury_buffer);
6253 defsubr (&Serase_buffer);
6254 defsubr (&Sbuffer_swap_text);
6255 defsubr (&Sset_buffer_multibyte);
6256 defsubr (&Skill_all_local_variables);
6257
6258 defsubr (&Soverlayp);
6259 defsubr (&Smake_overlay);
6260 defsubr (&Sdelete_overlay);
6261 defsubr (&Smove_overlay);
6262 defsubr (&Soverlay_start);
6263 defsubr (&Soverlay_end);
6264 defsubr (&Soverlay_buffer);
6265 defsubr (&Soverlay_properties);
6266 defsubr (&Soverlays_at);
6267 defsubr (&Soverlays_in);
6268 defsubr (&Snext_overlay_change);
6269 defsubr (&Sprevious_overlay_change);
6270 defsubr (&Soverlay_recenter);
6271 defsubr (&Soverlay_lists);
6272 defsubr (&Soverlay_get);
6273 defsubr (&Soverlay_put);
6274 defsubr (&Srestore_buffer_modified_p);
6275 }
6276
6277 void
6278 keys_of_buffer ()
6279 {
6280 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6281 initial_define_key (control_x_map, 'k', "kill-buffer");
6282
6283 /* This must not be in syms_of_buffer, because Qdisabled is not
6284 initialized when that function gets called. */
6285 Fput (intern ("erase-buffer"), Qdisabled, Qt);
6286 }
6287
6288 /* arch-tag: e48569bf-69a9-4b65-a23b-8e68769436e1
6289 (do not change this comment) */