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