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