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