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