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