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