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