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