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