gnus-sum.el (gnus-summary-enter-digest-group): Work for encoding and charset
[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}
c7aa5005
KH
2102
2103/* Set the current buffer to B, and do not set windows_or_buffers_changed.
2104 This is used by redisplay. */
2105
2106void
d3da34e0 2107set_buffer_internal_1 (register struct buffer *b)
c7aa5005
KH
2108{
2109 register struct buffer *old_buf;
ce5b453a 2110 register Lisp_Object tail;
c7aa5005 2111
b86af064 2112#ifdef USE_MMAP_FOR_BUFFERS
684b01ee 2113 if (b->text->beg == NULL)
b86af064
GM
2114 enlarge_buffer_text (b, 0);
2115#endif /* USE_MMAP_FOR_BUFFERS */
177c0ea7 2116
c7aa5005
KH
2117 if (current_buffer == b)
2118 return;
2119
1ab256cb
RM
2120 old_buf = current_buffer;
2121 current_buffer = b;
2122 last_known_column_point = -1; /* invalidate indentation cache */
2123
336cd056
RS
2124 if (old_buf)
2125 {
2126 /* Put the undo list back in the base buffer, so that it appears
2127 that an indirect buffer shares the undo list of its base. */
2128 if (old_buf->base_buffer)
39eb03f1 2129 bset_undo_list (old_buf->base_buffer, BVAR (old_buf, undo_list));
336cd056
RS
2130
2131 /* If the old current buffer has markers to record PT, BEGV and ZV
2132 when it is not current, update them now. */
cffc6f3b 2133 record_buffer_markers (old_buf);
336cd056
RS
2134 }
2135
2136 /* Get the undo list from the base buffer, so that it appears
2137 that an indirect buffer shares the undo list of its base. */
2138 if (b->base_buffer)
39eb03f1 2139 bset_undo_list (b, BVAR (b->base_buffer, undo_list));
336cd056
RS
2140
2141 /* If the new current buffer has markers to record PT, BEGV and ZV
2142 when it is not current, fetch them now. */
cffc6f3b 2143 fetch_buffer_markers (b);
336cd056 2144
1ab256cb
RM
2145 /* Look down buffer's list of local Lisp variables
2146 to find and update any that forward into C variables. */
2147
ce5b453a 2148 do
1ab256cb 2149 {
4b4deea2 2150 for (tail = BVAR (b, local_var_alist); CONSP (tail); tail = XCDR (tail))
ce5b453a
SM
2151 {
2152 Lisp_Object var = XCAR (XCAR (tail));
2153 struct Lisp_Symbol *sym = XSYMBOL (var);
2154 if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */
2155 && SYMBOL_BLV (sym)->fwd)
2156 /* Just reference the variable
2157 to cause it to become set for this buffer. */
2158 Fsymbol_value (var);
2159 }
1ab256cb 2160 }
1ab256cb 2161 /* Do the same with any others that were local to the previous buffer */
ce5b453a 2162 while (b != old_buf && (b = old_buf, b));
1ab256cb
RM
2163}
2164
336cd056 2165/* Switch to buffer B temporarily for redisplay purposes.
bbbe9545 2166 This avoids certain things that don't need to be done within redisplay. */
336cd056
RS
2167
2168void
d3da34e0 2169set_buffer_temp (struct buffer *b)
336cd056
RS
2170{
2171 register struct buffer *old_buf;
2172
2173 if (current_buffer == b)
2174 return;
2175
2176 old_buf = current_buffer;
2177 current_buffer = b;
2178
cffc6f3b
CY
2179 /* If the old current buffer has markers to record PT, BEGV and ZV
2180 when it is not current, update them now. */
2181 record_buffer_markers (old_buf);
336cd056
RS
2182
2183 /* If the new current buffer has markers to record PT, BEGV and ZV
2184 when it is not current, fetch them now. */
cffc6f3b 2185 fetch_buffer_markers (b);
336cd056
RS
2186}
2187
a7ca3326 2188DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
c8804c4f
MR
2189 doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
2190BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See
2191also `save-excursion' when you want to make a buffer current
2192temporarily. This function does not display the buffer, so its effect
2193ends when the current command terminates. Use `switch-to-buffer' or
2194`pop-to-buffer' to switch buffers permanently. */)
5842a27b 2195 (register Lisp_Object buffer_or_name)
1ab256cb 2196{
c8804c4f
MR
2197 register Lisp_Object buffer;
2198 buffer = Fget_buffer (buffer_or_name);
2199 if (NILP (buffer))
2200 nsberror (buffer_or_name);
4b4deea2 2201 if (NILP (BVAR (XBUFFER (buffer), name)))
1ab256cb 2202 error ("Selecting deleted buffer");
c8804c4f
MR
2203 set_buffer_internal (XBUFFER (buffer));
2204 return buffer;
1ab256cb 2205}
d0628b06 2206
66322887 2207/* Set the current buffer to BUFFER provided if it is alive. */
d0628b06
RS
2208
2209Lisp_Object
d3da34e0 2210set_buffer_if_live (Lisp_Object buffer)
d0628b06 2211{
4b4deea2 2212 if (! NILP (BVAR (XBUFFER (buffer), name)))
a3d794a1 2213 set_buffer_internal (XBUFFER (buffer));
d0628b06
RS
2214 return Qnil;
2215}
1ab256cb 2216\f
a7ca3326 2217DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1ab256cb 2218 Sbarf_if_buffer_read_only, 0, 0, 0,
7ee72033 2219 doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */)
5842a27b 2220 (void)
1ab256cb 2221{
4b4deea2 2222 if (!NILP (BVAR (current_buffer, read_only))
a96b68f1 2223 && NILP (Vinhibit_read_only))
4c4dc0b0 2224 xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
1ab256cb
RM
2225 return Qnil;
2226}
1ab256cb 2227\f
a7ca3326 2228DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
7ee72033 2229 doc: /* Delete the entire contents of the current buffer.
018ba359 2230Any narrowing restriction in effect (see `narrow-to-region') is removed,
7ee72033 2231so the buffer is truly empty after this. */)
5842a27b 2232 (void)
1ab256cb
RM
2233{
2234 Fwiden ();
c0d9a0c3
GM
2235
2236 del_range (BEG, Z);
c280bc6a 2237
1ab256cb
RM
2238 current_buffer->last_window_start = 1;
2239 /* Prevent warnings, or suspension of auto saving, that would happen
2240 if future size is less than past size. Use of erase-buffer
2241 implies that the future text is not really related to the past text. */
4b4deea2 2242 XSETFASTINT (BVAR (current_buffer, save_length), 0);
1ab256cb
RM
2243 return Qnil;
2244}
2245
01136e9b 2246void
d3da34e0 2247validate_region (register Lisp_Object *b, register Lisp_Object *e)
1ab256cb 2248{
b7826503
PJ
2249 CHECK_NUMBER_COERCE_MARKER (*b);
2250 CHECK_NUMBER_COERCE_MARKER (*e);
1ab256cb
RM
2251
2252 if (XINT (*b) > XINT (*e))
2253 {
03192067
KH
2254 Lisp_Object tem;
2255 tem = *b; *b = *e; *e = tem;
1ab256cb
RM
2256 }
2257
d311d28c 2258 if (! (BEGV <= XINT (*b) && XINT (*e) <= ZV))
1ab256cb
RM
2259 args_out_of_range (*b, *e);
2260}
2261\f
b05525fa
RS
2262/* Advance BYTE_POS up to a character boundary
2263 and return the adjusted position. */
2264
d311d28c
PE
2265static ptrdiff_t
2266advance_to_char_boundary (ptrdiff_t byte_pos)
b05525fa 2267{
f8449323 2268 int c;
b05525fa 2269
f8449323
RS
2270 if (byte_pos == BEG)
2271 /* Beginning of buffer is always a character boundary. */
6d70a280 2272 return BEG;
f8449323
RS
2273
2274 c = FETCH_BYTE (byte_pos);
2275 if (! CHAR_HEAD_P (c))
b05525fa 2276 {
1be6387d 2277 /* We should advance BYTE_POS only when C is a constituent of a
f8449323 2278 multibyte sequence. */
d311d28c 2279 ptrdiff_t orig_byte_pos = byte_pos;
a9bcded1
KH
2280
2281 do
2282 {
2283 byte_pos--;
2284 c = FETCH_BYTE (byte_pos);
2285 }
2286 while (! CHAR_HEAD_P (c) && byte_pos > BEG);
f8449323 2287 INC_POS (byte_pos);
a9bcded1
KH
2288 if (byte_pos < orig_byte_pos)
2289 byte_pos = orig_byte_pos;
f8449323
RS
2290 /* If C is a constituent of a multibyte sequence, BYTE_POS was
2291 surely advance to the correct character boundary. If C is
2292 not, BYTE_POS was unchanged. */
b05525fa
RS
2293 }
2294
20773569 2295 return byte_pos;
b05525fa
RS
2296}
2297
13cda5f9
SM
2298DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
2299 1, 1, 0,
2300 doc: /* Swap the text between current buffer and BUFFER. */)
5842a27b 2301 (Lisp_Object buffer)
13cda5f9
SM
2302{
2303 struct buffer *other_buffer;
2304 CHECK_BUFFER (buffer);
2305 other_buffer = XBUFFER (buffer);
2306
4b4deea2 2307 if (NILP (BVAR (other_buffer, name)))
409f2919 2308 error ("Cannot swap a dead buffer's text");
dc9cc574 2309
13cda5f9
SM
2310 /* Actually, it probably works just fine.
2311 * if (other_buffer == current_buffer)
2312 * error ("Cannot swap a buffer's text with itself"); */
2313
2314 /* Actually, this may be workable as well, tho probably only if they're
2315 *both* indirect. */
2316 if (other_buffer->base_buffer
2317 || current_buffer->base_buffer)
2318 error ("Cannot swap indirect buffers's text");
2319
2320 { /* This is probably harder to make work. */
2321 struct buffer *other;
52b852c7 2322 FOR_EACH_BUFFER (other)
13cda5f9
SM
2323 if (other->base_buffer == other_buffer
2324 || other->base_buffer == current_buffer)
2325 error ("One of the buffers to swap has indirect buffers");
2326 }
2327
2328#define swapfield(field, type) \
2329 do { \
2330 type tmp##field = other_buffer->field; \
2331 other_buffer->field = current_buffer->field; \
2332 current_buffer->field = tmp##field; \
2333 } while (0)
5d8ea120
TT
2334#define swapfield_(field, type) \
2335 do { \
4b4deea2 2336 type tmp##field = BVAR (other_buffer, field); \
39eb03f1
PE
2337 bset_##field (other_buffer, BVAR (current_buffer, field)); \
2338 bset_##field (current_buffer, tmp##field); \
5d8ea120 2339 } while (0)
13cda5f9
SM
2340
2341 swapfield (own_text, struct buffer_text);
2342 eassert (current_buffer->text == &current_buffer->own_text);
2343 eassert (other_buffer->text == &other_buffer->own_text);
baae5c2d 2344#ifdef REL_ALLOC
261cb4bb
PE
2345 r_alloc_reset_variable ((void **) &current_buffer->own_text.beg,
2346 (void **) &other_buffer->own_text.beg);
2347 r_alloc_reset_variable ((void **) &other_buffer->own_text.beg,
2348 (void **) &current_buffer->own_text.beg);
baae5c2d
JR
2349#endif /* REL_ALLOC */
2350
d311d28c
PE
2351 swapfield (pt, ptrdiff_t);
2352 swapfield (pt_byte, ptrdiff_t);
2353 swapfield (begv, ptrdiff_t);
2354 swapfield (begv_byte, ptrdiff_t);
2355 swapfield (zv, ptrdiff_t);
2356 swapfield (zv_byte, ptrdiff_t);
13cda5f9
SM
2357 eassert (!current_buffer->base_buffer);
2358 eassert (!other_buffer->base_buffer);
372f8ffc 2359 swapfield (indirections, ptrdiff_t);
13cda5f9
SM
2360 current_buffer->clip_changed = 1; other_buffer->clip_changed = 1;
2361 swapfield (newline_cache, struct region_cache *);
2362 swapfield (width_run_cache, struct region_cache *);
2363 current_buffer->prevent_redisplay_optimizations_p = 1;
2364 other_buffer->prevent_redisplay_optimizations_p = 1;
2365 swapfield (overlays_before, struct Lisp_Overlay *);
2366 swapfield (overlays_after, struct Lisp_Overlay *);
d311d28c 2367 swapfield (overlay_center, ptrdiff_t);
5d8ea120
TT
2368 swapfield_ (undo_list, Lisp_Object);
2369 swapfield_ (mark, Lisp_Object);
2370 swapfield_ (enable_multibyte_characters, Lisp_Object);
2371 swapfield_ (bidi_display_reordering, Lisp_Object);
2372 swapfield_ (bidi_paragraph_direction, Lisp_Object);
13cda5f9
SM
2373 /* FIXME: Not sure what we should do with these *_marker fields.
2374 Hopefully they're just nil anyway. */
5d8ea120
TT
2375 swapfield_ (pt_marker, Lisp_Object);
2376 swapfield_ (begv_marker, Lisp_Object);
2377 swapfield_ (zv_marker, Lisp_Object);
39eb03f1
PE
2378 bset_point_before_scroll (current_buffer, Qnil);
2379 bset_point_before_scroll (other_buffer, Qnil);
13cda5f9
SM
2380
2381 current_buffer->text->modiff++; other_buffer->text->modiff++;
2382 current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++;
2383 current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++;
2384 current_buffer->text->beg_unchanged = current_buffer->text->gpt;
2385 current_buffer->text->end_unchanged = current_buffer->text->gpt;
b8ff72fa
SM
2386 other_buffer->text->beg_unchanged = other_buffer->text->gpt;
2387 other_buffer->text->end_unchanged = other_buffer->text->gpt;
13cda5f9
SM
2388 {
2389 struct Lisp_Marker *m;
2390 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
2391 if (m->buffer == other_buffer)
2392 m->buffer = current_buffer;
b8ff72fa
SM
2393 else
2394 /* Since there's no indirect buffer in sight, markers on
2395 BUF_MARKERS(buf) should either be for `buf' or dead. */
2396 eassert (!m->buffer);
13cda5f9
SM
2397 for (m = BUF_MARKERS (other_buffer); m; m = m->next)
2398 if (m->buffer == current_buffer)
2399 m->buffer = other_buffer;
b8ff72fa
SM
2400 else
2401 /* Since there's no indirect buffer in sight, markers on
2402 BUF_MARKERS(buf) should either be for `buf' or dead. */
2403 eassert (!m->buffer);
13cda5f9 2404 }
126f1fc1
SM
2405 { /* Some of the C code expects that w->buffer == w->pointm->buffer.
2406 So since we just swapped the markers between the two buffers, we need
2407 to undo the effect of this swap for window markers. */
2408 Lisp_Object w = Fselected_window (), ws = Qnil;
2409 Lisp_Object buf1, buf2;
2410 XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
2411
2412 while (NILP (Fmemq (w, ws)))
2413 {
2414 ws = Fcons (w, ws);
d3d50620
DA
2415 if (MARKERP (XWINDOW (w)->pointm)
2416 && (EQ (XWINDOW (w)->buffer, buf1)
2417 || EQ (XWINDOW (w)->buffer, buf2)))
2418 Fset_marker (XWINDOW (w)->pointm,
3a45383a 2419 make_number
d3d50620
DA
2420 (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))),
2421 XWINDOW (w)->buffer);
126f1fc1
SM
2422 w = Fnext_window (w, Qt, Qt);
2423 }
2424 }
2425
13cda5f9
SM
2426 if (current_buffer->text->intervals)
2427 (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)),
2428 XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
2429 if (other_buffer->text->intervals)
2430 (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())),
2431 XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
2432
2433 return Qnil;
2434}
2435
a7ca3326 2436DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
3ac81adb 2437 1, 1, 0,
7ee72033 2438 doc: /* Set the multibyte flag of the current buffer to FLAG.
018ba359
PJ
2439If FLAG is t, this makes the buffer a multibyte buffer.
2440If FLAG is nil, this makes the buffer a single-byte buffer.
8f924df7
KH
2441In these cases, the buffer contents remain unchanged as a sequence of
2442bytes but the contents viewed as characters do change.
2443If FLAG is `to', this makes the buffer a multibyte buffer by changing
6b61353c
KH
2444all eight-bit bytes to eight-bit characters.
2445If the multibyte flag was really changed, undo information of the
2446current buffer is cleared. */)
5842a27b 2447 (Lisp_Object flag)
3ac81adb 2448{
65745fad 2449 struct Lisp_Marker *tail, *markers;
abc9d959 2450 struct buffer *other;
d311d28c 2451 ptrdiff_t begv, zv;
37ef52bb
PE
2452 bool narrowed = (BEG != BEGV || Z != ZV);
2453 bool modified_p = !NILP (Fbuffer_modified_p (Qnil));
4b4deea2 2454 Lisp_Object old_undo = BVAR (current_buffer, undo_list);
38babc07 2455 struct gcpro gcpro1;
3ac81adb 2456
6e553d5e
RS
2457 if (current_buffer->base_buffer)
2458 error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
2459
70e77119 2460 /* Do nothing if nothing actually changes. */
4b4deea2 2461 if (NILP (flag) == NILP (BVAR (current_buffer, enable_multibyte_characters)))
70e77119
AS
2462 return flag;
2463
38babc07
KS
2464 GCPRO1 (old_undo);
2465
2466 /* Don't record these buffer changes. We will put a special undo entry
2467 instead. */
39eb03f1 2468 bset_undo_list (current_buffer, Qt);
b05525fa 2469
3ac81adb
RS
2470 /* If the cached position is for this buffer, clear it out. */
2471 clear_charpos_cache (current_buffer);
2472
458c8af4
KH
2473 if (NILP (flag))
2474 begv = BEGV_BYTE, zv = ZV_BYTE;
2475 else
2476 begv = BEGV, zv = ZV;
2477
a9bcded1
KH
2478 if (narrowed)
2479 Fwiden ();
2480
3ac81adb
RS
2481 if (NILP (flag))
2482 {
d311d28c 2483 ptrdiff_t pos, stop;
a9bcded1
KH
2484 unsigned char *p;
2485
3ac81adb
RS
2486 /* Do this first, so it can use CHAR_TO_BYTE
2487 to calculate the old correspondences. */
2488 set_intervals_multibyte (0);
2489
39eb03f1 2490 bset_enable_multibyte_characters (current_buffer, Qnil);
3ac81adb
RS
2491
2492 Z = Z_BYTE;
2493 BEGV = BEGV_BYTE;
2494 ZV = ZV_BYTE;
2495 GPT = GPT_BYTE;
2496 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
2497
60ebfdf3 2498
65745fad
SM
2499 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
2500 tail->charpos = tail->bytepos;
a9bcded1
KH
2501
2502 /* Convert multibyte form of 8-bit characters to unibyte. */
2503 pos = BEG;
2504 stop = GPT;
2505 p = BEG_ADDR;
2506 while (1)
2507 {
2508 int c, bytes;
2509
2510 if (pos == stop)
2511 {
2512 if (pos == Z)
2513 break;
2514 p = GAP_END_ADDR;
2515 stop = Z;
2516 }
8f348ed5
KH
2517 if (ASCII_BYTE_P (*p))
2518 p++, pos++;
2519 else if (CHAR_BYTE8_HEAD_P (*p))
a9bcded1 2520 {
62a6e103 2521 c = STRING_CHAR_AND_LENGTH (p, bytes);
a9bcded1 2522 /* Delete all bytes for this 8-bit character but the
3b59c351 2523 last one, and change the last one to the character
a9bcded1
KH
2524 code. */
2525 bytes--;
2526 del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
2527 p = GAP_END_ADDR;
2528 *p++ = c;
2529 pos++;
2530 if (begv > pos)
2531 begv -= bytes;
2532 if (zv > pos)
2533 zv -= bytes;
2534 stop = Z;
2535 }
8f924df7 2536 else
8f348ed5
KH
2537 {
2538 bytes = BYTES_BY_CHAR_HEAD (*p);
2539 p += bytes, pos += bytes;
2540 }
a9bcded1
KH
2541 }
2542 if (narrowed)
2543 Fnarrow_to_region (make_number (begv), make_number (zv));
3ac81adb
RS
2544 }
2545 else
2546 {
d311d28c
PE
2547 ptrdiff_t pt = PT;
2548 ptrdiff_t pos, stop;
8f348ed5 2549 unsigned char *p, *pend;
a9bcded1 2550
673c57d2 2551 /* Be sure not to have a multibyte sequence striding over the GAP.
8f348ed5
KH
2552 Ex: We change this: "...abc\302 _GAP_ \241def..."
2553 to: "...abc _GAP_ \302\241def..." */
673c57d2 2554
8f924df7 2555 if (EQ (flag, Qt)
a3a303df 2556 && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
673c57d2
KH
2557 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
2558 {
8f54f30a 2559 unsigned char *q = GPT_ADDR - 1;
673c57d2 2560
8f54f30a
PE
2561 while (! CHAR_HEAD_P (*q) && q > BEG_ADDR) q--;
2562 if (LEADING_CODE_P (*q))
673c57d2 2563 {
d311d28c 2564 ptrdiff_t new_gpt = GPT_BYTE - (GPT_ADDR - q);
673c57d2
KH
2565
2566 move_gap_both (new_gpt, new_gpt);
2567 }
2568 }
2569
a9bcded1
KH
2570 /* Make the buffer contents valid as multibyte by converting
2571 8-bit characters to multibyte form. */
2572 pos = BEG;
2573 stop = GPT;
2574 p = BEG_ADDR;
8f348ed5 2575 pend = GPT_ADDR;
a9bcded1
KH
2576 while (1)
2577 {
2578 int bytes;
2579
2580 if (pos == stop)
2581 {
2582 if (pos == Z)
2583 break;
2584 p = GAP_END_ADDR;
8f348ed5 2585 pend = Z_ADDR;
a9bcded1
KH
2586 stop = Z;
2587 }
177c0ea7 2588
a3a303df
KH
2589 if (ASCII_BYTE_P (*p))
2590 p++, pos++;
dcfb9bc4
KH
2591 else if (EQ (flag, Qt)
2592 && ! CHAR_BYTE8_HEAD_P (*p)
2593 && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0)
a9bcded1
KH
2594 p += bytes, pos += bytes;
2595 else
2596 {
2597 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
a3a303df 2598 int c;
a9bcded1 2599
8f924df7 2600 c = BYTE8_TO_CHAR (*p);
8c2fc311 2601 bytes = CHAR_STRING (c, tmp);
a9bcded1
KH
2602 *p = tmp[0];
2603 TEMP_SET_PT_BOTH (pos + 1, pos + 1);
2604 bytes--;
b68864e5 2605 insert_1_both ((char *) tmp + 1, bytes, bytes, 1, 0, 0);
a9bcded1
KH
2606 /* Now the gap is after the just inserted data. */
2607 pos = GPT;
2608 p = GAP_END_ADDR;
2609 if (pos <= begv)
2610 begv += bytes;
2611 if (pos <= zv)
2612 zv += bytes;
2613 if (pos <= pt)
2614 pt += bytes;
31285a8f 2615 pend = Z_ADDR;
a9bcded1
KH
2616 stop = Z;
2617 }
2618 }
2619
2620 if (pt != PT)
2621 TEMP_SET_PT (pt);
2622
2623 if (narrowed)
2624 Fnarrow_to_region (make_number (begv), make_number (zv));
2625
3ac81adb
RS
2626 /* Do this first, so that chars_in_text asks the right question.
2627 set_intervals_multibyte needs it too. */
39eb03f1 2628 bset_enable_multibyte_characters (current_buffer, Qt);
3ac81adb 2629
b05525fa 2630 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
3ac81adb 2631 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
b05525fa 2632
673c57d2 2633 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
b05525fa
RS
2634
2635 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
3ac81adb 2636 if (BEGV_BYTE > GPT_BYTE)
673c57d2 2637 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2638 else
2639 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2640
2641 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
3ac81adb 2642 if (ZV_BYTE > GPT_BYTE)
673c57d2 2643 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
3ac81adb
RS
2644 else
2645 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
b05525fa
RS
2646
2647 {
d311d28c
PE
2648 ptrdiff_t byte = advance_to_char_boundary (PT_BYTE);
2649 ptrdiff_t position;
b05525fa 2650
8f54f30a
PE
2651 if (byte > GPT_BYTE)
2652 position = chars_in_text (GAP_END_ADDR, byte - GPT_BYTE) + GPT;
b05525fa 2653 else
8f54f30a
PE
2654 position = chars_in_text (BEG_ADDR, byte - BEG_BYTE) + BEG;
2655 TEMP_SET_PT_BOTH (position, byte);
b05525fa 2656 }
3ac81adb
RS
2657
2658 tail = markers = BUF_MARKERS (current_buffer);
95fb069b
RS
2659
2660 /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
2661 getting confused by the markers that have not yet been updated.
2662 It is also a signal that it should never create a marker. */
65745fad 2663 BUF_MARKERS (current_buffer) = NULL;
3ac81adb 2664
65745fad 2665 for (; tail; tail = tail->next)
3ac81adb 2666 {
65745fad
SM
2667 tail->bytepos = advance_to_char_boundary (tail->bytepos);
2668 tail->charpos = BYTE_TO_CHAR (tail->bytepos);
3ac81adb 2669 }
b69f9797
RS
2670
2671 /* Make sure no markers were put on the chain
2672 while the chain value was incorrect. */
65745fad 2673 if (BUF_MARKERS (current_buffer))
b69f9797
RS
2674 abort ();
2675
3ac81adb
RS
2676 BUF_MARKERS (current_buffer) = markers;
2677
2678 /* Do this last, so it can calculate the new correspondences
2679 between chars and bytes. */
2680 set_intervals_multibyte (1);
2681 }
2682
38babc07
KS
2683 if (!EQ (old_undo, Qt))
2684 {
2685 /* Represent all the above changes by a special undo entry. */
39eb03f1
PE
2686 bset_undo_list (current_buffer,
2687 Fcons (list3 (Qapply,
2688 intern ("set-buffer-multibyte"),
2689 NILP (flag) ? Qt : Qnil),
2690 old_undo));
38babc07
KS
2691 }
2692
2693 UNGCPRO;
a9bcded1 2694
724b203f
GM
2695 /* Changing the multibyteness of a buffer means that all windows
2696 showing that buffer must be updated thoroughly. */
2697 current_buffer->prevent_redisplay_optimizations_p = 1;
2698 ++windows_or_buffers_changed;
2699
abc9d959
RS
2700 /* Copy this buffer's new multibyte status
2701 into all of its indirect buffers. */
52b852c7 2702 FOR_EACH_BUFFER (other)
4b4deea2 2703 if (other->base_buffer == current_buffer && !NILP (BVAR (other, name)))
724b203f 2704 {
4b4deea2
TT
2705 BVAR (other, enable_multibyte_characters)
2706 = BVAR (current_buffer, enable_multibyte_characters);
724b203f
GM
2707 other->prevent_redisplay_optimizations_p = 1;
2708 }
abc9d959 2709
ed00559d
KH
2710 /* Restore the modifiedness of the buffer. */
2711 if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
2712 Fset_buffer_modified_p (Qnil);
2713
172f9454
KH
2714 /* Update coding systems of this buffer's process (if any). */
2715 {
2716 Lisp_Object process;
2717
2718 process = Fget_buffer_process (Fcurrent_buffer ());
2719 if (PROCESSP (process))
2720 setup_process_coding_systems (process);
2721 }
2722
3ac81adb
RS
2723 return flag;
2724}
2725\f
a7ca3326 2726DEFUN ("kill-all-local-variables", Fkill_all_local_variables,
16a97296 2727 Skill_all_local_variables, 0, 0, 0,
7ee72033 2728 doc: /* Switch to Fundamental mode by killing current buffer's local variables.
018ba359
PJ
2729Most local variable bindings are eliminated so that the default values
2730become effective once more. Also, the syntax table is set from
2731`standard-syntax-table', the local keymap is set to nil,
2732and the abbrev table from `fundamental-mode-abbrev-table'.
2733This function also forces redisplay of the mode line.
2734
2735Every function to select a new major mode starts by
2736calling this function.
2737
2738As a special exception, local variables whose names have
2739a non-nil `permanent-local' property are not eliminated by this function.
2740
2741The first thing this function does is run
7ee72033 2742the normal hook `change-major-mode-hook'. */)
5842a27b 2743 (void)
1ab256cb 2744{
dee091a3 2745 Frun_hooks (1, &Qchange_major_mode_hook);
1ab256cb 2746
3709505e 2747 /* Make sure none of the bindings in local_var_alist
2f3f993b 2748 remain swapped in, in their symbols. */
1ab256cb 2749
2f3f993b 2750 swap_out_buffer_local_variables (current_buffer);
1ab256cb
RM
2751
2752 /* Actually eliminate all local bindings of this buffer. */
2753
13de9290 2754 reset_buffer_local_variables (current_buffer, 0);
1ab256cb 2755
1ab256cb
RM
2756 /* Force mode-line redisplay. Useful here because all major mode
2757 commands call this function. */
2758 update_mode_lines++;
2759
2760 return Qnil;
2761}
2f3f993b
RS
2762
2763/* Make sure no local variables remain set up with buffer B
2764 for their current values. */
2765
2766static void
d3da34e0 2767swap_out_buffer_local_variables (struct buffer *b)
2f3f993b 2768{
ce5b453a 2769 Lisp_Object oalist, alist, buffer;
2f3f993b
RS
2770
2771 XSETBUFFER (buffer, b);
4b4deea2 2772 oalist = BVAR (b, local_var_alist);
2f3f993b 2773
67ee9f6e 2774 for (alist = oalist; CONSP (alist); alist = XCDR (alist))
2f3f993b 2775 {
ce5b453a
SM
2776 Lisp_Object sym = XCAR (XCAR (alist));
2777 eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
2778 /* Need not do anything if some other buffer's binding is
e4769531 2779 now cached. */
ce5b453a 2780 if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
2f3f993b 2781 {
e7c10f83
SM
2782 /* Symbol is set up for this buffer's old local value:
2783 swap it out! */
ce5b453a 2784 swap_in_global_binding (XSYMBOL (sym));
2f3f993b
RS
2785 }
2786 }
2787}
1ab256cb 2788\f
2eec3b4e 2789/* Find all the overlays in the current buffer that contain position POS.
177c0ea7 2790 Return the number found, and store them in a vector in *VEC_PTR.
2eec3b4e 2791 Store in *LEN_PTR the size allocated for the vector.
52f8ec73 2792 Store in *NEXT_PTR the next position after POS where an overlay starts,
624d2678 2793 or ZV if there are no more overlays between POS and ZV.
bbbe9545 2794 Store in *PREV_PTR the previous position before POS where an overlay ends,
413e06a4 2795 or where an overlay starts which ends at or after POS;
624d2678 2796 or BEGV if there are no such overlays from BEGV to POS.
239c932b 2797 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2eec3b4e
RS
2798
2799 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
2800 when this function is called.
2801
37ef52bb
PE
2802 If EXTEND, make the vector bigger if necessary.
2803 If not, never extend the vector,
2804 and store only as many overlays as will fit.
2805 But still return the total number of overlays.
ac869cf7 2806
37ef52bb 2807 If CHANGE_REQ, any position written into *PREV_PTR or
ac869cf7
MB
2808 *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
2809 default (BEGV or ZV). */
2eec3b4e 2810
b081724f 2811ptrdiff_t
37ef52bb 2812overlays_at (EMACS_INT pos, bool extend, Lisp_Object **vec_ptr,
b081724f 2813 ptrdiff_t *len_ptr,
37ef52bb 2814 ptrdiff_t *next_ptr, ptrdiff_t *prev_ptr, bool change_req)
1ab256cb 2815{
2410d73a
SM
2816 Lisp_Object overlay, start, end;
2817 struct Lisp_Overlay *tail;
b081724f
PE
2818 ptrdiff_t idx = 0;
2819 ptrdiff_t len = *len_ptr;
2eec3b4e 2820 Lisp_Object *vec = *vec_ptr;
d311d28c
PE
2821 ptrdiff_t next = ZV;
2822 ptrdiff_t prev = BEGV;
37ef52bb 2823 bool inhibit_storing = 0;
61d54cd5 2824
fd318b54 2825 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
2eec3b4e 2826 {
d311d28c 2827 ptrdiff_t startpos, endpos;
52f8ec73 2828
2410d73a 2829 XSETMISC (overlay, tail);
1ab256cb 2830
2eec3b4e
RS
2831 start = OVERLAY_START (overlay);
2832 end = OVERLAY_END (overlay);
239c932b
RS
2833 endpos = OVERLAY_POSITION (end);
2834 if (endpos < pos)
2835 {
2836 if (prev < endpos)
2837 prev = endpos;
2838 break;
2839 }
413e06a4
RS
2840 startpos = OVERLAY_POSITION (start);
2841 /* This one ends at or after POS
daa1c109 2842 so its start counts for PREV_PTR if it's before POS. */
413e06a4
RS
2843 if (prev < startpos && startpos < pos)
2844 prev = startpos;
239c932b
RS
2845 if (endpos == pos)
2846 continue;
2eec3b4e
RS
2847 if (startpos <= pos)
2848 {
2849 if (idx == len)
2850 {
61d54cd5
RS
2851 /* The supplied vector is full.
2852 Either make it bigger, or don't store any more in it. */
2853 if (extend)
2854 {
0065d054
PE
2855 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
2856 sizeof *vec);
61d54cd5 2857 *vec_ptr = vec;
0065d054 2858 len = *len_ptr;
61d54cd5
RS
2859 }
2860 else
2861 inhibit_storing = 1;
2eec3b4e 2862 }
61d54cd5
RS
2863
2864 if (!inhibit_storing)
2865 vec[idx] = overlay;
2866 /* Keep counting overlays even if we can't return them all. */
2867 idx++;
2eec3b4e
RS
2868 }
2869 else if (startpos < next)
2870 next = startpos;
2871 }
2872
fd318b54 2873 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
1ab256cb 2874 {
d311d28c 2875 ptrdiff_t startpos, endpos;
52f8ec73 2876
2410d73a 2877 XSETMISC (overlay, tail);
2eec3b4e
RS
2878
2879 start = OVERLAY_START (overlay);
2880 end = OVERLAY_END (overlay);
2881 startpos = OVERLAY_POSITION (start);
52f8ec73 2882 if (pos < startpos)
2eec3b4e
RS
2883 {
2884 if (startpos < next)
2885 next = startpos;
2886 break;
2887 }
239c932b
RS
2888 endpos = OVERLAY_POSITION (end);
2889 if (pos < endpos)
2eec3b4e
RS
2890 {
2891 if (idx == len)
2892 {
61d54cd5
RS
2893 if (extend)
2894 {
0065d054
PE
2895 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
2896 sizeof *vec);
61d54cd5 2897 *vec_ptr = vec;
0065d054 2898 len = *len_ptr;
61d54cd5
RS
2899 }
2900 else
2901 inhibit_storing = 1;
2eec3b4e 2902 }
61d54cd5
RS
2903
2904 if (!inhibit_storing)
2905 vec[idx] = overlay;
2906 idx++;
413e06a4
RS
2907
2908 if (startpos < pos && startpos > prev)
2909 prev = startpos;
2eec3b4e 2910 }
239c932b
RS
2911 else if (endpos < pos && endpos > prev)
2912 prev = endpos;
1d5f4c1d
MB
2913 else if (endpos == pos && startpos > prev
2914 && (!change_req || startpos < pos))
413e06a4 2915 prev = startpos;
1ab256cb
RM
2916 }
2917
239c932b
RS
2918 if (next_ptr)
2919 *next_ptr = next;
2920 if (prev_ptr)
2921 *prev_ptr = prev;
2eec3b4e
RS
2922 return idx;
2923}
74514898 2924\f
7723e095
MR
2925/* Find all the overlays in the current buffer that overlap the range
2926 BEG-END, or are empty at BEG, or are empty at END provided END
2927 denotes the position at the end of the current buffer.
2a3eeee7 2928
177c0ea7 2929 Return the number found, and store them in a vector in *VEC_PTR.
74514898
RS
2930 Store in *LEN_PTR the size allocated for the vector.
2931 Store in *NEXT_PTR the next position after POS where an overlay starts,
2932 or ZV if there are no more overlays.
2933 Store in *PREV_PTR the previous position before POS where an overlay ends,
2934 or BEGV if there are no previous overlays.
2935 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2936
2937 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2938 when this function is called.
2939
37ef52bb
PE
2940 If EXTEND, make the vector bigger if necessary.
2941 If not, never extend the vector,
2942 and store only as many overlays as will fit.
2943 But still return the total number of overlays. */
74514898 2944
21514da7 2945static ptrdiff_t
37ef52bb 2946overlays_in (EMACS_INT beg, EMACS_INT end, bool extend,
21514da7 2947 Lisp_Object **vec_ptr, ptrdiff_t *len_ptr,
d311d28c 2948 ptrdiff_t *next_ptr, ptrdiff_t *prev_ptr)
74514898 2949{
2410d73a
SM
2950 Lisp_Object overlay, ostart, oend;
2951 struct Lisp_Overlay *tail;
21514da7
PE
2952 ptrdiff_t idx = 0;
2953 ptrdiff_t len = *len_ptr;
74514898 2954 Lisp_Object *vec = *vec_ptr;
d311d28c
PE
2955 ptrdiff_t next = ZV;
2956 ptrdiff_t prev = BEGV;
37ef52bb
PE
2957 bool inhibit_storing = 0;
2958 bool end_is_Z = end == Z;
74514898 2959
fd318b54 2960 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
74514898 2961 {
d311d28c 2962 ptrdiff_t startpos, endpos;
74514898 2963
2410d73a 2964 XSETMISC (overlay, tail);
74514898
RS
2965
2966 ostart = OVERLAY_START (overlay);
2967 oend = OVERLAY_END (overlay);
2968 endpos = OVERLAY_POSITION (oend);
2969 if (endpos < beg)
2970 {
2971 if (prev < endpos)
2972 prev = endpos;
2973 break;
2974 }
2975 startpos = OVERLAY_POSITION (ostart);
7723e095
MR
2976 /* Count an interval if it overlaps the range, is empty at the
2977 start of the range, or is empty at END provided END denotes the
2978 end of the buffer. */
74514898 2979 if ((beg < endpos && startpos < end)
7723e095
MR
2980 || (startpos == endpos
2981 && (beg == endpos || (end_is_Z && endpos == end))))
74514898
RS
2982 {
2983 if (idx == len)
2984 {
2985 /* The supplied vector is full.
2986 Either make it bigger, or don't store any more in it. */
2987 if (extend)
2988 {
0065d054
PE
2989 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
2990 sizeof *vec);
74514898 2991 *vec_ptr = vec;
0065d054 2992 len = *len_ptr;
74514898
RS
2993 }
2994 else
2995 inhibit_storing = 1;
2996 }
2997
2998 if (!inhibit_storing)
2999 vec[idx] = overlay;
3000 /* Keep counting overlays even if we can't return them all. */
3001 idx++;
3002 }
3003 else if (startpos < next)
3004 next = startpos;
3005 }
3006
fd318b54 3007 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
74514898 3008 {
d311d28c 3009 ptrdiff_t startpos, endpos;
74514898 3010
2410d73a 3011 XSETMISC (overlay, tail);
74514898
RS
3012
3013 ostart = OVERLAY_START (overlay);
3014 oend = OVERLAY_END (overlay);
3015 startpos = OVERLAY_POSITION (ostart);
3016 if (end < startpos)
3017 {
3018 if (startpos < next)
3019 next = startpos;
3020 break;
3021 }
3022 endpos = OVERLAY_POSITION (oend);
7723e095
MR
3023 /* Count an interval if it overlaps the range, is empty at the
3024 start of the range, or is empty at END provided END denotes the
3025 end of the buffer. */
74514898 3026 if ((beg < endpos && startpos < end)
7723e095
MR
3027 || (startpos == endpos
3028 && (beg == endpos || (end_is_Z && endpos == end))))
74514898
RS
3029 {
3030 if (idx == len)
3031 {
3032 if (extend)
3033 {
0065d054
PE
3034 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
3035 sizeof *vec);
74514898 3036 *vec_ptr = vec;
0065d054 3037 len = *len_ptr;
74514898
RS
3038 }
3039 else
3040 inhibit_storing = 1;
3041 }
3042
3043 if (!inhibit_storing)
3044 vec[idx] = overlay;
3045 idx++;
3046 }
3047 else if (endpos < beg && endpos > prev)
3048 prev = endpos;
3049 }
fc04fa47 3050
74514898
RS
3051 if (next_ptr)
3052 *next_ptr = next;
3053 if (prev_ptr)
3054 *prev_ptr = prev;
3055 return idx;
3056}
09a22085
GM
3057
3058
37ef52bb 3059/* Return true if there exists an overlay with a non-nil
09a22085
GM
3060 `mouse-face' property overlapping OVERLAY. */
3061
37ef52bb 3062bool
d3da34e0 3063mouse_face_overlay_overlaps (Lisp_Object overlay)
09a22085 3064{
d311d28c
PE
3065 ptrdiff_t start = OVERLAY_POSITION (OVERLAY_START (overlay));
3066 ptrdiff_t end = OVERLAY_POSITION (OVERLAY_END (overlay));
21514da7 3067 ptrdiff_t n, i, size;
09a22085 3068 Lisp_Object *v, tem;
177c0ea7 3069
bfd8410f 3070 size = 10;
38182d90 3071 v = alloca (size * sizeof *v);
bfd8410f
GM
3072 n = overlays_in (start, end, 0, &v, &size, NULL, NULL);
3073 if (n > size)
09a22085 3074 {
38182d90 3075 v = alloca (n * sizeof *v);
09a22085
GM
3076 overlays_in (start, end, 0, &v, &n, NULL, NULL);
3077 }
3078
3079 for (i = 0; i < n; ++i)
3080 if (!EQ (v[i], overlay)
3081 && (tem = Foverlay_get (overlay, Qmouse_face),
3082 !NILP (tem)))
3083 break;
3084
3085 return i < n;
3086}
3087
3088
74514898 3089\f
fc04fa47 3090/* Fast function to just test if we're at an overlay boundary. */
37ef52bb 3091bool
d311d28c 3092overlay_touches_p (ptrdiff_t pos)
fc04fa47 3093{
2410d73a
SM
3094 Lisp_Object overlay;
3095 struct Lisp_Overlay *tail;
fc04fa47 3096
fd318b54 3097 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
fc04fa47 3098 {
d311d28c 3099 ptrdiff_t endpos;
fc04fa47 3100
2410d73a 3101 XSETMISC (overlay ,tail);
22657b40 3102 eassert (OVERLAYP (overlay));
fc04fa47
KH
3103
3104 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3105 if (endpos < pos)
3106 break;
3107 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
3108 return 1;
3109 }
3110
fd318b54 3111 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
fc04fa47 3112 {
d311d28c 3113 ptrdiff_t startpos;
fc04fa47 3114
2410d73a 3115 XSETMISC (overlay, tail);
22657b40 3116 eassert (OVERLAYP (overlay));
fc04fa47
KH
3117
3118 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3119 if (pos < startpos)
3120 break;
3121 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
3122 return 1;
3123 }
3124 return 0;
3125}
2eec3b4e 3126\f
5985d248
KH
3127struct sortvec
3128{
3129 Lisp_Object overlay;
d311d28c 3130 ptrdiff_t beg, end;
fadf4e30 3131 EMACS_INT priority;
5985d248
KH
3132};
3133
3134static int
d3da34e0 3135compare_overlays (const void *v1, const void *v2)
5985d248 3136{
dfcf069d
AS
3137 const struct sortvec *s1 = (const struct sortvec *) v1;
3138 const struct sortvec *s2 = (const struct sortvec *) v2;
5985d248 3139 if (s1->priority != s2->priority)
c20998a7 3140 return s1->priority < s2->priority ? -1 : 1;
5985d248 3141 if (s1->beg != s2->beg)
c20998a7 3142 return s1->beg < s2->beg ? -1 : 1;
5985d248 3143 if (s1->end != s2->end)
c20998a7 3144 return s2->end < s1->end ? -1 : 1;
cae07000
SM
3145 /* Avoid the non-determinism of qsort by choosing an arbitrary ordering
3146 between "equal" overlays. The result can still change between
3147 invocations of Emacs, but it won't change in the middle of
3148 `find_field' (bug#6830). */
9af5ed87
PE
3149 if (XHASH (s1->overlay) != XHASH (s2->overlay))
3150 return XHASH (s1->overlay) < XHASH (s2->overlay) ? -1 : 1;
5985d248
KH
3151 return 0;
3152}
3153
3154/* Sort an array of overlays by priority. The array is modified in place.
3155 The return value is the new size; this may be smaller than the original
3156 size if some of the overlays were invalid or were window-specific. */
b081724f
PE
3157ptrdiff_t
3158sort_overlays (Lisp_Object *overlay_vec, ptrdiff_t noverlays, struct window *w)
5985d248 3159{
b081724f 3160 ptrdiff_t i, j;
38182d90 3161 struct sortvec *sortvec = alloca (noverlays * sizeof *sortvec);
5985d248
KH
3162
3163 /* Put the valid and relevant overlays into sortvec. */
3164
3165 for (i = 0, j = 0; i < noverlays; i++)
3166 {
0fa767e7 3167 Lisp_Object tem;
c99fc30f 3168 Lisp_Object overlay;
5985d248 3169
c99fc30f 3170 overlay = overlay_vec[i];
22657b40 3171 if (OVERLAYP (overlay)
5985d248
KH
3172 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
3173 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
3174 {
0fa767e7
KH
3175 /* If we're interested in a specific window, then ignore
3176 overlays that are limited to some other window. */
3177 if (w)
5985d248 3178 {
28545e04 3179 Lisp_Object window;
0fa767e7
KH
3180
3181 window = Foverlay_get (overlay, Qwindow);
28545e04 3182 if (WINDOWP (window) && XWINDOW (window) != w)
0fa767e7 3183 continue;
5985d248 3184 }
0fa767e7
KH
3185
3186 /* This overlay is good and counts: put it into sortvec. */
3187 sortvec[j].overlay = overlay;
3188 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3189 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
3190 tem = Foverlay_get (overlay, Qpriority);
3191 if (INTEGERP (tem))
3192 sortvec[j].priority = XINT (tem);
3193 else
3194 sortvec[j].priority = 0;
3195 j++;
5985d248
KH
3196 }
3197 }
3198 noverlays = j;
3199
3200 /* Sort the overlays into the proper order: increasing priority. */
3201
3202 if (noverlays > 1)
3203 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
3204
3205 for (i = 0; i < noverlays; i++)
3206 overlay_vec[i] = sortvec[i].overlay;
3207 return (noverlays);
3208}
3209\f
bbbe9545
KH
3210struct sortstr
3211{
cb26008f 3212 Lisp_Object string, string2;
93cb6be3 3213 ptrdiff_t size;
8961a454 3214 EMACS_INT priority;
bbbe9545
KH
3215};
3216
e8185fa8
KH
3217struct sortstrlist
3218{
3219 struct sortstr *buf; /* An array that expands as needed; never freed. */
67c36fce
PE
3220 ptrdiff_t size; /* Allocated length of that array. */
3221 ptrdiff_t used; /* How much of the array is currently in use. */
0065d054 3222 ptrdiff_t bytes; /* Total length of the strings in buf. */
e8185fa8
KH
3223};
3224
3225/* Buffers for storing information about the overlays touching a given
3226 position. These could be automatic variables in overlay_strings, but
3227 it's more efficient to hold onto the memory instead of repeatedly
3228 allocating and freeing it. */
3229static struct sortstrlist overlay_heads, overlay_tails;
9492daf2 3230static unsigned char *overlay_str_buf;
e8185fa8
KH
3231
3232/* Allocated length of overlay_str_buf. */
fe5c5d37 3233static ptrdiff_t overlay_str_len;
e8185fa8 3234
bbbe9545
KH
3235/* A comparison function suitable for passing to qsort. */
3236static int
d3da34e0 3237cmp_for_strings (const void *as1, const void *as2)
bbbe9545
KH
3238{
3239 struct sortstr *s1 = (struct sortstr *)as1;
3240 struct sortstr *s2 = (struct sortstr *)as2;
3241 if (s1->size != s2->size)
c20998a7 3242 return s2->size < s1->size ? -1 : 1;
bbbe9545 3243 if (s1->priority != s2->priority)
c20998a7 3244 return s1->priority < s2->priority ? -1 : 1;
bbbe9545
KH
3245 return 0;
3246}
3247
e8185fa8 3248static void
93cb6be3
PE
3249record_overlay_string (struct sortstrlist *ssl, Lisp_Object str,
3250 Lisp_Object str2, Lisp_Object pri, ptrdiff_t size)
e8185fa8 3251{
d311d28c 3252 ptrdiff_t nbytes;
43d27a72 3253
e8185fa8 3254 if (ssl->used == ssl->size)
0065d054 3255 ssl->buf = xpalloc (ssl->buf, &ssl->size, 5, -1, sizeof *ssl->buf);
e8185fa8 3256 ssl->buf[ssl->used].string = str;
cb26008f 3257 ssl->buf[ssl->used].string2 = str2;
e8185fa8
KH
3258 ssl->buf[ssl->used].size = size;
3259 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
3260 ssl->used++;
43d27a72 3261
4b4deea2 3262 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
d5db4077 3263 nbytes = SCHARS (str);
43d27a72 3264 else if (! STRING_MULTIBYTE (str))
d5db4077
KR
3265 nbytes = count_size_as_multibyte (SDATA (str),
3266 SBYTES (str));
43d27a72 3267 else
d5db4077 3268 nbytes = SBYTES (str);
43d27a72 3269
0065d054
PE
3270 if (INT_ADD_OVERFLOW (ssl->bytes, nbytes))
3271 memory_full (SIZE_MAX);
43d27a72
RS
3272 ssl->bytes += nbytes;
3273
cb26008f 3274 if (STRINGP (str2))
43d27a72 3275 {
4b4deea2 3276 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
d5db4077 3277 nbytes = SCHARS (str2);
43d27a72 3278 else if (! STRING_MULTIBYTE (str2))
d5db4077
KR
3279 nbytes = count_size_as_multibyte (SDATA (str2),
3280 SBYTES (str2));
43d27a72 3281 else
d5db4077 3282 nbytes = SBYTES (str2);
43d27a72 3283
0065d054
PE
3284 if (INT_ADD_OVERFLOW (ssl->bytes, nbytes))
3285 memory_full (SIZE_MAX);
43d27a72
RS
3286 ssl->bytes += nbytes;
3287 }
e8185fa8 3288}
bbbe9545
KH
3289
3290/* Return the concatenation of the strings associated with overlays that
3291 begin or end at POS, ignoring overlays that are specific to a window
3292 other than W. The strings are concatenated in the appropriate order:
3293 shorter overlays nest inside longer ones, and higher priority inside
cb26008f
KH
3294 lower. Normally all of the after-strings come first, but zero-sized
3295 overlays have their after-strings ride along with the before-strings
3296 because it would look strange to print them inside-out.
3297
3298 Returns the string length, and stores the contents indirectly through
3299 PSTR, if that variable is non-null. The string may be overwritten by
3300 subsequent calls. */
6b5d3b89 3301
d311d28c
PE
3302ptrdiff_t
3303overlay_strings (ptrdiff_t pos, struct window *w, unsigned char **pstr)
bbbe9545 3304{
28545e04 3305 Lisp_Object overlay, window, str;
2410d73a 3306 struct Lisp_Overlay *ov;
d311d28c 3307 ptrdiff_t startpos, endpos;
37ef52bb 3308 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
bbbe9545 3309
e8185fa8
KH
3310 overlay_heads.used = overlay_heads.bytes = 0;
3311 overlay_tails.used = overlay_tails.bytes = 0;
fd318b54 3312 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
bbbe9545 3313 {
2410d73a 3314 XSETMISC (overlay, ov);
c2d5b10f 3315 eassert (OVERLAYP (overlay));
bbbe9545
KH
3316
3317 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3318 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3319 if (endpos < pos)
3320 break;
3321 if (endpos != pos && startpos != pos)
3322 continue;
3323 window = Foverlay_get (overlay, Qwindow);
28545e04 3324 if (WINDOWP (window) && XWINDOW (window) != w)
bbbe9545 3325 continue;
e8185fa8
KH
3326 if (startpos == pos
3327 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3328 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3329 (startpos == endpos
3330 ? Foverlay_get (overlay, Qafter_string)
3331 : Qnil),
3332 Foverlay_get (overlay, Qpriority),
3333 endpos - startpos);
3334 else if (endpos == pos
3335 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3336 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3337 Foverlay_get (overlay, Qpriority),
3338 endpos - startpos);
bbbe9545 3339 }
fd318b54 3340 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
bbbe9545 3341 {
2410d73a
SM
3342 XSETMISC (overlay, ov);
3343 eassert (OVERLAYP (overlay));
bbbe9545
KH
3344
3345 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3346 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3347 if (startpos > pos)
3348 break;
e8185fa8
KH
3349 if (endpos != pos && startpos != pos)
3350 continue;
3351 window = Foverlay_get (overlay, Qwindow);
28545e04 3352 if (WINDOWP (window) && XWINDOW (window) != w)
e8185fa8 3353 continue;
e8185fa8
KH
3354 if (startpos == pos
3355 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
3356 record_overlay_string (&overlay_heads, str,
cb26008f
KH
3357 (startpos == endpos
3358 ? Foverlay_get (overlay, Qafter_string)
3359 : Qnil),
3360 Foverlay_get (overlay, Qpriority),
3361 endpos - startpos);
3362 else if (endpos == pos
3363 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
3364 record_overlay_string (&overlay_tails, str, Qnil,
e8185fa8
KH
3365 Foverlay_get (overlay, Qpriority),
3366 endpos - startpos);
bbbe9545 3367 }
e8185fa8
KH
3368 if (overlay_tails.used > 1)
3369 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
3370 cmp_for_strings);
3371 if (overlay_heads.used > 1)
3372 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
3373 cmp_for_strings);
3374 if (overlay_heads.bytes || overlay_tails.bytes)
bbbe9545 3375 {
e8185fa8 3376 Lisp_Object tem;
d311d28c 3377 ptrdiff_t i;
9f4d7cde 3378 unsigned char *p;
0065d054 3379 ptrdiff_t total;
bbbe9545 3380
0065d054
PE
3381 if (INT_ADD_OVERFLOW (overlay_heads.bytes, overlay_tails.bytes))
3382 memory_full (SIZE_MAX);
3383 total = overlay_heads.bytes + overlay_tails.bytes;
bbbe9545 3384 if (total > overlay_str_len)
0065d054
PE
3385 overlay_str_buf = xpalloc (overlay_str_buf, &overlay_str_len,
3386 total - overlay_str_len, -1, 1);
3387
bbbe9545 3388 p = overlay_str_buf;
e8185fa8 3389 for (i = overlay_tails.used; --i >= 0;)
bbbe9545 3390 {
d311d28c 3391 ptrdiff_t nbytes;
e8185fa8 3392 tem = overlay_tails.buf[i].string;
d5db4077
KR
3393 nbytes = copy_text (SDATA (tem), p,
3394 SBYTES (tem),
43d27a72
RS
3395 STRING_MULTIBYTE (tem), multibyte);
3396 p += nbytes;
bbbe9545 3397 }
e8185fa8 3398 for (i = 0; i < overlay_heads.used; ++i)
bbbe9545 3399 {
d311d28c 3400 ptrdiff_t nbytes;
e8185fa8 3401 tem = overlay_heads.buf[i].string;
d5db4077
KR
3402 nbytes = copy_text (SDATA (tem), p,
3403 SBYTES (tem),
43d27a72
RS
3404 STRING_MULTIBYTE (tem), multibyte);
3405 p += nbytes;
cb26008f
KH
3406 tem = overlay_heads.buf[i].string2;
3407 if (STRINGP (tem))
3408 {
d5db4077
KR
3409 nbytes = copy_text (SDATA (tem), p,
3410 SBYTES (tem),
43d27a72
RS
3411 STRING_MULTIBYTE (tem), multibyte);
3412 p += nbytes;
cb26008f 3413 }
bbbe9545 3414 }
cb26008f
KH
3415 if (p != overlay_str_buf + total)
3416 abort ();
bbbe9545
KH
3417 if (pstr)
3418 *pstr = overlay_str_buf;
e8185fa8 3419 return total;
bbbe9545 3420 }
e8185fa8 3421 return 0;
bbbe9545
KH
3422}
3423\f
5c4f68f1 3424/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 3425
2eec3b4e 3426void
d311d28c 3427recenter_overlay_lists (struct buffer *buf, ptrdiff_t pos)
2eec3b4e 3428{
2410d73a
SM
3429 Lisp_Object overlay, beg, end;
3430 struct Lisp_Overlay *prev, *tail, *next;
2eec3b4e
RS
3431
3432 /* See if anything in overlays_before should move to overlays_after. */
3433
3434 /* We don't strictly need prev in this loop; it should always be nil.
3435 But we use it for symmetry and in case that should cease to be true
3436 with some future change. */
2410d73a 3437 prev = NULL;
fd318b54 3438 for (tail = buf->overlays_before; tail; prev = tail, tail = next)
1ab256cb 3439 {
2410d73a
SM
3440 next = tail->next;
3441 XSETMISC (overlay, tail);
22657b40 3442 eassert (OVERLAYP (overlay));
1ab256cb 3443
2eec3b4e
RS
3444 beg = OVERLAY_START (overlay);
3445 end = OVERLAY_END (overlay);
1ab256cb 3446
2eec3b4e 3447 if (OVERLAY_POSITION (end) > pos)
1ab256cb 3448 {
2eec3b4e 3449 /* OVERLAY needs to be moved. */
d311d28c 3450 ptrdiff_t where = OVERLAY_POSITION (beg);
2410d73a 3451 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3452
3453 /* Splice the cons cell TAIL out of overlays_before. */
2410d73a
SM
3454 if (prev)
3455 prev->next = next;
2eec3b4e 3456 else
0c94c8d6 3457 set_buffer_overlays_before (buf, next);
2eec3b4e
RS
3458
3459 /* Search thru overlays_after for where to put it. */
2410d73a 3460 other_prev = NULL;
fd318b54 3461 for (other = buf->overlays_after; other;
2410d73a 3462 other_prev = other, other = other->next)
1ab256cb 3463 {
6af718a4 3464 Lisp_Object otherbeg, otheroverlay;
2eec3b4e 3465
2410d73a 3466 XSETMISC (otheroverlay, other);
22657b40 3467 eassert (OVERLAYP (otheroverlay));
2eec3b4e
RS
3468
3469 otherbeg = OVERLAY_START (otheroverlay);
3470 if (OVERLAY_POSITION (otherbeg) >= where)
3471 break;
1ab256cb 3472 }
2eec3b4e
RS
3473
3474 /* Add TAIL to overlays_after before OTHER. */
2410d73a
SM
3475 tail->next = other;
3476 if (other_prev)
3477 other_prev->next = tail;
1ab256cb 3478 else
0c94c8d6 3479 set_buffer_overlays_after (buf, tail);
2eec3b4e 3480 tail = prev;
1ab256cb 3481 }
2eec3b4e
RS
3482 else
3483 /* We've reached the things that should stay in overlays_before.
3484 All the rest of overlays_before must end even earlier,
3485 so stop now. */
3486 break;
3487 }
3488
3489 /* See if anything in overlays_after should be in overlays_before. */
2410d73a 3490 prev = NULL;
fd318b54 3491 for (tail = buf->overlays_after; tail; prev = tail, tail = next)
2eec3b4e 3492 {
2410d73a
SM
3493 next = tail->next;
3494 XSETMISC (overlay, tail);
22657b40 3495 eassert (OVERLAYP (overlay));
2eec3b4e
RS
3496
3497 beg = OVERLAY_START (overlay);
3498 end = OVERLAY_END (overlay);
3499
3500 /* Stop looking, when we know that nothing further
3501 can possibly end before POS. */
3502 if (OVERLAY_POSITION (beg) > pos)
3503 break;
3504
3505 if (OVERLAY_POSITION (end) <= pos)
3506 {
3507 /* OVERLAY needs to be moved. */
d311d28c 3508 ptrdiff_t where = OVERLAY_POSITION (end);
2410d73a 3509 struct Lisp_Overlay *other, *other_prev;
2eec3b4e
RS
3510
3511 /* Splice the cons cell TAIL out of overlays_after. */
2410d73a
SM
3512 if (prev)
3513 prev->next = next;
2eec3b4e 3514 else
0c94c8d6 3515 set_buffer_overlays_after (buf, next);
2eec3b4e
RS
3516
3517 /* Search thru overlays_before for where to put it. */
2410d73a 3518 other_prev = NULL;
fd318b54 3519 for (other = buf->overlays_before; other;
2410d73a 3520 other_prev = other, other = other->next)
2eec3b4e
RS
3521 {
3522 Lisp_Object otherend, otheroverlay;
2eec3b4e 3523
2410d73a 3524 XSETMISC (otheroverlay, other);
22657b40 3525 eassert (OVERLAYP (otheroverlay));
2eec3b4e
RS
3526
3527 otherend = OVERLAY_END (otheroverlay);
3528 if (OVERLAY_POSITION (otherend) <= where)
3529 break;
3530 }
3531
3532 /* Add TAIL to overlays_before before OTHER. */
2410d73a
SM
3533 tail->next = other;
3534 if (other_prev)
3535 other_prev->next = tail;
2eec3b4e 3536 else
0c94c8d6 3537 set_buffer_overlays_before (buf, tail);
2eec3b4e
RS
3538 tail = prev;
3539 }
3540 }
3541
c2d5b10f 3542 buf->overlay_center = pos;
2eec3b4e 3543}
2b1bdf65 3544
423cdb46 3545void
d311d28c 3546adjust_overlays_for_insert (ptrdiff_t pos, ptrdiff_t length)
423cdb46
KH
3547{
3548 /* After an insertion, the lists are still sorted properly,
3549 but we may need to update the value of the overlay center. */
c2d5b10f
SM
3550 if (current_buffer->overlay_center >= pos)
3551 current_buffer->overlay_center += length;
423cdb46
KH
3552}
3553
3554void
d311d28c 3555adjust_overlays_for_delete (ptrdiff_t pos, ptrdiff_t length)
423cdb46 3556{
c2d5b10f 3557 if (current_buffer->overlay_center < pos)
423cdb46
KH
3558 /* The deletion was to our right. No change needed; the before- and
3559 after-lists are still consistent. */
3560 ;
d311d28c 3561 else if (current_buffer->overlay_center - pos > length)
423cdb46
KH
3562 /* The deletion was to our left. We need to adjust the center value
3563 to account for the change in position, but the lists are consistent
3564 given the new value. */
c2d5b10f 3565 current_buffer->overlay_center -= length;
423cdb46
KH
3566 else
3567 /* We're right in the middle. There might be things on the after-list
3568 that now belong on the before-list. Recentering will move them,
3569 and also update the center point. */
3570 recenter_overlay_lists (current_buffer, pos);
3571}
3572
2b1bdf65
KH
3573/* Fix up overlays that were garbled as a result of permuting markers
3574 in the range START through END. Any overlay with at least one
3575 endpoint in this range will need to be unlinked from the overlay
3576 list and reinserted in its proper place.
3577 Such an overlay might even have negative size at this point.
6b61353c 3578 If so, we'll make the overlay empty. */
2b1bdf65 3579void
d311d28c 3580fix_start_end_in_overlays (register ptrdiff_t start, register ptrdiff_t end)
2b1bdf65 3581{
6af718a4 3582 Lisp_Object overlay;
5df8f01b
PE
3583 struct Lisp_Overlay *before_list IF_LINT (= NULL);
3584 struct Lisp_Overlay *after_list IF_LINT (= NULL);
1138e742
KR
3585 /* These are either nil, indicating that before_list or after_list
3586 should be assigned, or the cons cell the cdr of which should be
3587 assigned. */
2410d73a 3588 struct Lisp_Overlay *beforep = NULL, *afterp = NULL;
1138e742 3589 /* 'Parent', likewise, indicates a cons cell or
fd318b54 3590 current_buffer->overlays_before or overlays_after, depending
1138e742 3591 which loop we're in. */
2410d73a 3592 struct Lisp_Overlay *tail, *parent;
d311d28c 3593 ptrdiff_t startpos, endpos;
2b1bdf65
KH
3594
3595 /* This algorithm shifts links around instead of consing and GCing.
3596 The loop invariant is that before_list (resp. after_list) is a
1138e742
KR
3597 well-formed list except that its last element, the CDR of beforep
3598 (resp. afterp) if beforep (afterp) isn't nil or before_list
3599 (after_list) if it is, is still uninitialized. So it's not a bug
3600 that before_list isn't initialized, although it may look
3601 strange. */
fd318b54 3602 for (parent = NULL, tail = current_buffer->overlays_before; tail;)
2b1bdf65 3603 {
2410d73a 3604 XSETMISC (overlay, tail);
6b61353c 3605
2b1bdf65 3606 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
6b61353c
KH
3607 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3608
3609 /* If the overlay is backwards, make it empty. */
3610 if (endpos < startpos)
3611 {
3612 startpos = endpos;
3613 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
3614 Qnil);
3615 }
3616
2b1bdf65
KH
3617 if (endpos < start)
3618 break;
60ebfdf3 3619
2b1bdf65
KH
3620 if (endpos < end
3621 || (startpos >= start && startpos < end))
3622 {
2b1bdf65
KH
3623 /* Add it to the end of the wrong list. Later on,
3624 recenter_overlay_lists will move it to the right place. */
c2d5b10f 3625 if (endpos < current_buffer->overlay_center)
2b1bdf65 3626 {
2410d73a 3627 if (!afterp)
1138e742
KR
3628 after_list = tail;
3629 else
2410d73a 3630 afterp->next = tail;
1138e742 3631 afterp = tail;
2b1bdf65
KH
3632 }
3633 else
3634 {
2410d73a 3635 if (!beforep)
1138e742
KR
3636 before_list = tail;
3637 else
2410d73a 3638 beforep->next = tail;
1138e742 3639 beforep = tail;
2b1bdf65 3640 }
2410d73a 3641 if (!parent)
0c94c8d6 3642 set_buffer_overlays_before (current_buffer, tail->next);
1138e742 3643 else
2410d73a
SM
3644 parent->next = tail->next;
3645 tail = tail->next;
2b1bdf65
KH
3646 }
3647 else
2410d73a 3648 parent = tail, tail = parent->next;
2b1bdf65 3649 }
fd318b54 3650 for (parent = NULL, tail = current_buffer->overlays_after; tail;)
2b1bdf65 3651 {
2410d73a 3652 XSETMISC (overlay, tail);
6b61353c 3653
2b1bdf65 3654 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
6b61353c
KH
3655 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3656
3657 /* If the overlay is backwards, make it empty. */
3658 if (endpos < startpos)
3659 {
3660 startpos = endpos;
3661 Fset_marker (OVERLAY_START (overlay), make_number (startpos),
60ebfdf3 3662 Qnil);
6b61353c
KH
3663 }
3664
2b1bdf65
KH
3665 if (startpos >= end)
3666 break;
6b61353c 3667
2b1bdf65
KH
3668 if (startpos >= start
3669 || (endpos >= start && endpos < end))
3670 {
c2d5b10f 3671 if (endpos < current_buffer->overlay_center)
2b1bdf65 3672 {
2410d73a 3673 if (!afterp)
1138e742
KR
3674 after_list = tail;
3675 else
2410d73a 3676 afterp->next = tail;
1138e742 3677 afterp = tail;
2b1bdf65
KH
3678 }
3679 else
3680 {
2410d73a 3681 if (!beforep)
1138e742
KR
3682 before_list = tail;
3683 else
2410d73a 3684 beforep->next = tail;
1138e742 3685 beforep = tail;
2b1bdf65 3686 }
2410d73a 3687 if (!parent)
0c94c8d6 3688 set_buffer_overlays_after (current_buffer, tail->next);
1138e742 3689 else
2410d73a
SM
3690 parent->next = tail->next;
3691 tail = tail->next;
2b1bdf65
KH
3692 }
3693 else
2410d73a 3694 parent = tail, tail = parent->next;
2b1bdf65
KH
3695 }
3696
3697 /* Splice the constructed (wrong) lists into the buffer's lists,
3698 and let the recenter function make it sane again. */
2410d73a 3699 if (beforep)
1138e742 3700 {
fd318b54 3701 beforep->next = current_buffer->overlays_before;
0c94c8d6 3702 set_buffer_overlays_before (current_buffer, before_list);
1138e742 3703 }
c2d5b10f 3704 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3705
2410d73a 3706 if (afterp)
1138e742 3707 {
fd318b54 3708 afterp->next = current_buffer->overlays_after;
0c94c8d6 3709 set_buffer_overlays_after (current_buffer, after_list);
1138e742 3710 }
c2d5b10f 3711 recenter_overlay_lists (current_buffer, current_buffer->overlay_center);
2b1bdf65 3712}
3b06f880
KH
3713
3714/* We have two types of overlay: the one whose ending marker is
3715 after-insertion-marker (this is the usual case) and the one whose
3716 ending marker is before-insertion-marker. When `overlays_before'
3717 contains overlays of the latter type and the former type in this
3718 order and both overlays end at inserting position, inserting a text
3719 increases only the ending marker of the latter type, which results
3720 in incorrect ordering of `overlays_before'.
3721
3722 This function fixes ordering of overlays in the slot
3723 `overlays_before' of the buffer *BP. Before the insertion, `point'
3724 was at PREV, and now is at POS. */
3725
01136e9b 3726void
d311d28c 3727fix_overlays_before (struct buffer *bp, ptrdiff_t prev, ptrdiff_t pos)
3b06f880 3728{
2410d73a 3729 /* If parent is nil, replace overlays_before; otherwise, parent->next. */
fd318b54 3730 struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair;
2410d73a 3731 Lisp_Object tem;
d311d28c 3732 ptrdiff_t end IF_LINT (= 0);
3b06f880
KH
3733
3734 /* After the insertion, the several overlays may be in incorrect
3735 order. The possibility is that, in the list `overlays_before',
3736 an overlay which ends at POS appears after an overlay which ends
3737 at PREV. Since POS is greater than PREV, we must fix the
3738 ordering of these overlays, by moving overlays ends at POS before
3739 the overlays ends at PREV. */
3740
3741 /* At first, find a place where disordered overlays should be linked
3742 in. It is where an overlay which end before POS exists. (i.e. an
3743 overlay whose ending marker is after-insertion-marker if disorder
3744 exists). */
2410d73a
SM
3745 while (tail
3746 && (XSETMISC (tem, tail),
3747 (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos))
1138e742
KR
3748 {
3749 parent = tail;
2410d73a 3750 tail = tail->next;
1138e742 3751 }
3b06f880
KH
3752
3753 /* If we don't find such an overlay,
3754 or the found one ends before PREV,
3755 or the found one is the last one in the list,
3756 we don't have to fix anything. */
f93ad4cf 3757 if (!tail || end < prev || !tail->next)
3b06f880
KH
3758 return;
3759
1138e742
KR
3760 right_pair = parent;
3761 parent = tail;
2410d73a 3762 tail = tail->next;
3b06f880 3763
1138e742 3764 /* Now, end position of overlays in the list TAIL should be before
3b06f880 3765 or equal to PREV. In the loop, an overlay which ends at POS is
1138e742
KR
3766 moved ahead to the place indicated by the CDR of RIGHT_PAIR. If
3767 we found an overlay which ends before PREV, the remaining
3768 overlays are in correct order. */
2410d73a 3769 while (tail)
3b06f880 3770 {
2410d73a
SM
3771 XSETMISC (tem, tail);
3772 end = OVERLAY_POSITION (OVERLAY_END (tem));
3b06f880
KH
3773
3774 if (end == pos)
3775 { /* This overlay is disordered. */
2410d73a 3776 struct Lisp_Overlay *found = tail;
3b06f880
KH
3777
3778 /* Unlink the found overlay. */
2410d73a
SM
3779 tail = found->next;
3780 parent->next = tail;
1138e742
KR
3781 /* Move an overlay at RIGHT_PLACE to the next of the found one,
3782 and link it into the right place. */
2410d73a 3783 if (!right_pair)
1138e742 3784 {
fd318b54 3785 found->next = bp->overlays_before;
0c94c8d6 3786 set_buffer_overlays_before (bp, found);
1138e742
KR
3787 }
3788 else
3789 {
2410d73a
SM
3790 found->next = right_pair->next;
3791 right_pair->next = found;
1138e742 3792 }
3b06f880
KH
3793 }
3794 else if (end == prev)
1138e742
KR
3795 {
3796 parent = tail;
2410d73a 3797 tail = tail->next;
1138e742 3798 }
3b06f880
KH
3799 else /* No more disordered overlay. */
3800 break;
3801 }
3802}
2eec3b4e 3803\f
52f8ec73 3804DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
7ee72033 3805 doc: /* Return t if OBJECT is an overlay. */)
5842a27b 3806 (Lisp_Object object)
52f8ec73
JB
3807{
3808 return (OVERLAYP (object) ? Qt : Qnil);
3809}
3810
acac2700 3811DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
7ee72033 3812 doc: /* Create a new overlay with range BEG to END in BUFFER.
018ba359
PJ
3813If omitted, BUFFER defaults to the current buffer.
3814BEG and END may be integers or markers.
a625ee20
RS
3815The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
3816for the front of the overlay advance when text is inserted there
63af6055 3817\(which means the text *is not* included in the overlay).
a625ee20
RS
3818The fifth arg REAR-ADVANCE, if non-nil, makes the marker
3819for the rear of the overlay advance when text is inserted there
63af6055 3820\(which means the text *is* included in the overlay). */)
5842a27b 3821 (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance)
2eec3b4e
RS
3822{
3823 Lisp_Object overlay;
5c4f68f1 3824 struct buffer *b;
2eec3b4e 3825
5c4f68f1 3826 if (NILP (buffer))
67180c6a 3827 XSETBUFFER (buffer, current_buffer);
883047b9 3828 else
b7826503 3829 CHECK_BUFFER (buffer);
883047b9
JB
3830 if (MARKERP (beg)
3831 && ! EQ (Fmarker_buffer (beg), buffer))
3832 error ("Marker points into wrong buffer");
3833 if (MARKERP (end)
3834 && ! EQ (Fmarker_buffer (end), buffer))
3835 error ("Marker points into wrong buffer");
2eec3b4e 3836
b7826503
PJ
3837 CHECK_NUMBER_COERCE_MARKER (beg);
3838 CHECK_NUMBER_COERCE_MARKER (end);
5c4f68f1 3839
883047b9 3840 if (XINT (beg) > XINT (end))
5c4f68f1 3841 {
c99fc30f
KH
3842 Lisp_Object temp;
3843 temp = beg; beg = end; end = temp;
5c4f68f1 3844 }
883047b9
JB
3845
3846 b = XBUFFER (buffer);
3847
3848 beg = Fset_marker (Fmake_marker (), beg, buffer);
3849 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1 3850
acac2700
RS
3851 if (!NILP (front_advance))
3852 XMARKER (beg)->insertion_type = 1;
3853 if (!NILP (rear_advance))
3854 XMARKER (end)->insertion_type = 1;
597dd755 3855
d7a7fda3 3856 overlay = build_overlay (beg, end, Qnil);
2eec3b4e 3857
177c0ea7 3858 /* Put the new overlay on the wrong list. */
2eec3b4e 3859 end = OVERLAY_END (overlay);
c2d5b10f 3860 if (OVERLAY_POSITION (end) < b->overlay_center)
2410d73a 3861 {
fd318b54
DA
3862 if (b->overlays_after)
3863 XOVERLAY (overlay)->next = b->overlays_after;
0c94c8d6 3864 set_buffer_overlays_after (b, XOVERLAY (overlay));
2410d73a 3865 }
2eec3b4e 3866 else
2410d73a 3867 {
fd318b54
DA
3868 if (b->overlays_before)
3869 XOVERLAY (overlay)->next = b->overlays_before;
0c94c8d6 3870 set_buffer_overlays_before (b, XOVERLAY (overlay));
2410d73a 3871 }
2eec3b4e
RS
3872
3873 /* This puts it in the right list, and in the right order. */
c2d5b10f 3874 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 3875
b61982dd
JB
3876 /* We don't need to redisplay the region covered by the overlay, because
3877 the overlay has no properties at the moment. */
3878
2eec3b4e
RS
3879 return overlay;
3880}
876aa27c
RS
3881\f
3882/* Mark a section of BUF as needing redisplay because of overlays changes. */
3883
3884static void
d311d28c 3885modify_overlay (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
876aa27c 3886{
876aa27c
RS
3887 if (start > end)
3888 {
d311d28c 3889 ptrdiff_t temp = start;
26f545d7
GM
3890 start = end;
3891 end = temp;
876aa27c
RS
3892 }
3893
481b5054 3894 BUF_COMPUTE_UNCHANGED (buf, start, end);
177c0ea7 3895
876aa27c
RS
3896 /* If this is a buffer not in the selected window,
3897 we must do other windows. */
d3d50620 3898 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
876aa27c 3899 windows_or_buffers_changed = 1;
876aa27c
RS
3900 /* If multiple windows show this buffer, we must do other windows. */
3901 else if (buffer_shared > 1)
3902 windows_or_buffers_changed = 1;
18700091
KS
3903 /* If we modify an overlay at the end of the buffer, we cannot
3904 be sure that window end is still valid. */
3905 else if (end >= ZV && start <= ZV)
3906 windows_or_buffers_changed = 1;
876aa27c 3907
d8b9150f 3908 ++BUF_OVERLAY_MODIFF (buf);
876aa27c 3909}
2eec3b4e 3910
7b7ae965
DA
3911/* Remove OVERLAY from LIST. */
3912
2410d73a 3913static struct Lisp_Overlay *
d3da34e0 3914unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay)
2410d73a 3915{
7b7ae965
DA
3916 register struct Lisp_Overlay *tail, **prev = &list;
3917
3918 for (tail = list; tail; prev = &tail->next, tail = *prev)
3919 if (tail == overlay)
2410d73a 3920 {
7b7ae965 3921 *prev = overlay->next;
2410d73a
SM
3922 overlay->next = NULL;
3923 break;
3924 }
3925 return list;
3926}
3927
4cb3e6b3
DA
3928/* Remove OVERLAY from both overlay lists of B. */
3929
3930static void
3931unchain_both (struct buffer *b, Lisp_Object overlay)
3932{
3933 struct Lisp_Overlay *ov = XOVERLAY (overlay);
3934
0c94c8d6
PE
3935 set_buffer_overlays_before (b, unchain_overlay (b->overlays_before, ov));
3936 set_buffer_overlays_after (b, unchain_overlay (b->overlays_after, ov));
fd318b54 3937 eassert (XOVERLAY (overlay)->next == NULL);
4cb3e6b3
DA
3938}
3939
5c4f68f1 3940DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
7ee72033 3941 doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
018ba359
PJ
3942If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
3943If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
7ee72033 3944buffer. */)
5842a27b 3945 (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
2eec3b4e 3946{
471fe23d 3947 struct buffer *b, *ob = 0;
0a4469c9 3948 Lisp_Object obuffer;
d311d28c 3949 ptrdiff_t count = SPECPDL_INDEX ();
471fe23d 3950 ptrdiff_t n_beg, n_end, o_beg IF_LINT (= 0), o_end IF_LINT (= 0);
5c4f68f1 3951
b7826503 3952 CHECK_OVERLAY (overlay);
5c4f68f1
JB
3953 if (NILP (buffer))
3954 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a 3955 if (NILP (buffer))
67180c6a 3956 XSETBUFFER (buffer, current_buffer);
b7826503 3957 CHECK_BUFFER (buffer);
883047b9 3958
471fe23d
TN
3959 if (NILP (Fbuffer_live_p (buffer)))
3960 error ("Attempt to move overlay to a dead buffer");
3961
883047b9
JB
3962 if (MARKERP (beg)
3963 && ! EQ (Fmarker_buffer (beg), buffer))
3964 error ("Marker points into wrong buffer");
3965 if (MARKERP (end)
3966 && ! EQ (Fmarker_buffer (end), buffer))
3967 error ("Marker points into wrong buffer");
3968
b7826503
PJ
3969 CHECK_NUMBER_COERCE_MARKER (beg);
3970 CHECK_NUMBER_COERCE_MARKER (end);
d311d28c 3971
471fe23d 3972 if (XINT (beg) > XINT (end))
02481186 3973 {
471fe23d
TN
3974 Lisp_Object temp;
3975 temp = beg; beg = end; end = temp;
02481186
PE
3976 }
3977
9d7608b7
KH
3978 specbind (Qinhibit_quit, Qt);
3979
0a4469c9 3980 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
79cce3f2 3981 b = XBUFFER (buffer);
471fe23d
TN
3982
3983 if (!NILP (obuffer))
3984 {
3985 ob = XBUFFER (obuffer);
3986
3987 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3988 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3989
4cb3e6b3 3990 unchain_both (ob, overlay);
471fe23d
TN
3991 }
3992
3993 /* Set the overlay boundaries, which may clip them. */
3994 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3995 Fset_marker (OVERLAY_END (overlay), end, buffer);
3996
3997 n_beg = marker_position (OVERLAY_START (overlay));
3998 n_end = marker_position (OVERLAY_END (overlay));
2eec3b4e 3999
c82ed728 4000 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 4001 if (!EQ (buffer, obuffer))
50760c4a
RS
4002 {
4003 /* Redisplay where the overlay was. */
471fe23d
TN
4004 if (ob)
4005 modify_overlay (ob, o_beg, o_end);
50760c4a
RS
4006
4007 /* Redisplay where the overlay is going to be. */
79cce3f2 4008 modify_overlay (b, n_beg, n_end);
50760c4a 4009 }
c82ed728
JB
4010 else
4011 /* Redisplay the area the overlay has just left, or just enclosed. */
4012 {
79cce3f2
PE
4013 if (o_beg == n_beg)
4014 modify_overlay (b, o_end, n_end);
4015 else if (o_end == n_end)
4016 modify_overlay (b, o_beg, n_beg);
c82ed728 4017 else
79cce3f2 4018 modify_overlay (b, min (o_beg, n_beg), max (o_end, n_end));
c82ed728 4019 }
b61982dd 4020
471fe23d
TN
4021 /* Delete the overlay if it is empty after clipping and has the
4022 evaporate property. */
4023 if (n_beg == n_end && !NILP (Foverlay_get (overlay, Qevaporate)))
4024 return unbind_to (count, Fdelete_overlay (overlay));
02481186 4025
471fe23d
TN
4026 /* Put the overlay into the new buffer's overlay lists, first on the
4027 wrong list. */
4028 if (n_end < b->overlay_center)
2410d73a 4029 {
fd318b54 4030 XOVERLAY (overlay)->next = b->overlays_after;
0c94c8d6 4031 set_buffer_overlays_after (b, XOVERLAY (overlay));
2410d73a 4032 }
2eec3b4e 4033 else
2410d73a 4034 {
fd318b54 4035 XOVERLAY (overlay)->next = b->overlays_before;
0c94c8d6 4036 set_buffer_overlays_before (b, XOVERLAY (overlay));
2410d73a 4037 }
2eec3b4e
RS
4038
4039 /* This puts it in the right list, and in the right order. */
c2d5b10f 4040 recenter_overlay_lists (b, b->overlay_center);
2eec3b4e 4041
0a4469c9 4042 return unbind_to (count, overlay);
2eec3b4e
RS
4043}
4044
4045DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
7ee72033 4046 doc: /* Delete the overlay OVERLAY from its buffer. */)
5842a27b 4047 (Lisp_Object overlay)
2eec3b4e 4048{
0a4469c9 4049 Lisp_Object buffer;
5c4f68f1 4050 struct buffer *b;
d311d28c 4051 ptrdiff_t count = SPECPDL_INDEX ();
5c4f68f1 4052
b7826503 4053 CHECK_OVERLAY (overlay);
52f8ec73 4054
0a4469c9
RS
4055 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4056 if (NILP (buffer))
4057 return Qnil;
4058
4059 b = XBUFFER (buffer);
0a4469c9 4060 specbind (Qinhibit_quit, Qt);
177c0ea7 4061
4cb3e6b3 4062 unchain_both (b, overlay);
041a49a6 4063 drop_overlay (b, XOVERLAY (overlay));
3ece337a 4064
e58c389d 4065 /* When deleting an overlay with before or after strings, turn off
26f545d7
GM
4066 display optimizations for the affected buffer, on the basis that
4067 these strings may contain newlines. This is easier to do than to
4068 check for that situation during redisplay. */
4069 if (!windows_or_buffers_changed
4070 && (!NILP (Foverlay_get (overlay, Qbefore_string))
4071 || !NILP (Foverlay_get (overlay, Qafter_string))))
4072 b->prevent_redisplay_optimizations_p = 1;
4073
0a4469c9 4074 return unbind_to (count, Qnil);
2eec3b4e
RS
4075}
4076\f
8ebafa8d
JB
4077/* Overlay dissection functions. */
4078
a7ca3326 4079DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
7ee72033 4080 doc: /* Return the position at which OVERLAY starts. */)
5842a27b 4081 (Lisp_Object overlay)
8ebafa8d 4082{
b7826503 4083 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4084
4085 return (Fmarker_position (OVERLAY_START (overlay)));
4086}
4087
a7ca3326 4088DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
7ee72033 4089 doc: /* Return the position at which OVERLAY ends. */)
5842a27b 4090 (Lisp_Object overlay)
8ebafa8d 4091{
b7826503 4092 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4093
4094 return (Fmarker_position (OVERLAY_END (overlay)));
4095}
4096
4097DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
563f7128
LK
4098 doc: /* Return the buffer OVERLAY belongs to.
4099Return nil if OVERLAY has been deleted. */)
5842a27b 4100 (Lisp_Object overlay)
8ebafa8d 4101{
b7826503 4102 CHECK_OVERLAY (overlay);
8ebafa8d
JB
4103
4104 return Fmarker_buffer (OVERLAY_START (overlay));
4105}
4106
4107DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
7ee72033 4108 doc: /* Return a list of the properties on OVERLAY.
018ba359 4109This is a copy of OVERLAY's plist; modifying its conses has no effect on
7ee72033 4110OVERLAY. */)
5842a27b 4111 (Lisp_Object overlay)
8ebafa8d 4112{
b7826503 4113 CHECK_OVERLAY (overlay);
8ebafa8d 4114
c644523b 4115 return Fcopy_sequence (XOVERLAY (overlay)->plist);
8ebafa8d
JB
4116}
4117
4118\f
2eec3b4e 4119DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
aabc29c8 4120 doc: /* Return a list of the overlays that contain the character at POS. */)
5842a27b 4121 (Lisp_Object pos)
2eec3b4e 4122{
b081724f 4123 ptrdiff_t len, noverlays;
2eec3b4e 4124 Lisp_Object *overlay_vec;
2eec3b4e
RS
4125 Lisp_Object result;
4126
b7826503 4127 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
4128
4129 len = 10;
a9800ae8 4130 /* We can't use alloca here because overlays_at can call xrealloc. */
38182d90 4131 overlay_vec = xmalloc (len * sizeof *overlay_vec);
2eec3b4e
RS
4132
4133 /* Put all the overlays we want in a vector in overlay_vec.
4134 Store the length in len. */
2a77a7d7 4135 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
d311d28c 4136 0, 0, 0);
2eec3b4e
RS
4137
4138 /* Make a list of them all. */
4139 result = Flist (noverlays, overlay_vec);
4140
9ac0d9e0 4141 xfree (overlay_vec);
2eec3b4e
RS
4142 return result;
4143}
4144
74514898 4145DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
7ee72033 4146 doc: /* Return a list of the overlays that overlap the region BEG ... END.
018ba359
PJ
4147Overlap means that at least one character is contained within the overlay
4148and also contained within the specified region.
7723e095
MR
4149Empty overlays are included in the result if they are located at BEG,
4150between BEG and END, or at END provided END denotes the position at the
4151end of the buffer. */)
5842a27b 4152 (Lisp_Object beg, Lisp_Object end)
74514898 4153{
21514da7 4154 ptrdiff_t len, noverlays;
74514898 4155 Lisp_Object *overlay_vec;
74514898
RS
4156 Lisp_Object result;
4157
b7826503
PJ
4158 CHECK_NUMBER_COERCE_MARKER (beg);
4159 CHECK_NUMBER_COERCE_MARKER (end);
74514898
RS
4160
4161 len = 10;
38182d90 4162 overlay_vec = xmalloc (len * sizeof *overlay_vec);
74514898
RS
4163
4164 /* Put all the overlays we want in a vector in overlay_vec.
4165 Store the length in len. */
4166 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
145582a0 4167 NULL, NULL);
74514898
RS
4168
4169 /* Make a list of them all. */
4170 result = Flist (noverlays, overlay_vec);
4171
4172 xfree (overlay_vec);
4173 return result;
4174}
4175
a7ca3326 4176DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
efc7e75f 4177 1, 1, 0,
7ee72033 4178 doc: /* Return the next position after POS where an overlay starts or ends.
624d2678
RS
4179If there are no overlay boundaries from POS to (point-max),
4180the value is (point-max). */)
5842a27b 4181 (Lisp_Object pos)
2eec3b4e 4182{
b081724f 4183 ptrdiff_t i, len, noverlays;
d311d28c 4184 ptrdiff_t endpos;
2eec3b4e 4185 Lisp_Object *overlay_vec;
2eec3b4e 4186
b7826503 4187 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e
RS
4188
4189 len = 10;
38182d90 4190 overlay_vec = xmalloc (len * sizeof *overlay_vec);
2eec3b4e
RS
4191
4192 /* Put all the overlays we want in a vector in overlay_vec.
4193 Store the length in len.
4194 endpos gets the position where the next overlay starts. */
2a77a7d7 4195 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
d311d28c 4196 &endpos, 0, 1);
2eec3b4e
RS
4197
4198 /* If any of these overlays ends before endpos,
4199 use its ending point instead. */
4200 for (i = 0; i < noverlays; i++)
4201 {
4202 Lisp_Object oend;
d311d28c 4203 ptrdiff_t oendpos;
2eec3b4e
RS
4204
4205 oend = OVERLAY_END (overlay_vec[i]);
4206 oendpos = OVERLAY_POSITION (oend);
4207 if (oendpos < endpos)
4208 endpos = oendpos;
1ab256cb
RM
4209 }
4210
9ac0d9e0 4211 xfree (overlay_vec);
2eec3b4e
RS
4212 return make_number (endpos);
4213}
239c932b 4214
a7ca3326 4215DEFUN ("previous-overlay-change", Fprevious_overlay_change,
239c932b 4216 Sprevious_overlay_change, 1, 1, 0,
7ee72033 4217 doc: /* Return the previous position before POS where an overlay starts or ends.
624d2678
RS
4218If there are no overlay boundaries from (point-min) to POS,
4219the value is (point-min). */)
5842a27b 4220 (Lisp_Object pos)
239c932b 4221{
d311d28c 4222 ptrdiff_t prevpos;
239c932b 4223 Lisp_Object *overlay_vec;
b081724f 4224 ptrdiff_t len;
239c932b 4225
b7826503 4226 CHECK_NUMBER_COERCE_MARKER (pos);
239c932b 4227
624bbdc4
RS
4228 /* At beginning of buffer, we know the answer;
4229 avoid bug subtracting 1 below. */
4230 if (XINT (pos) == BEGV)
4231 return pos;
4232
017f0539 4233 len = 10;
38182d90 4234 overlay_vec = xmalloc (len * sizeof *overlay_vec);
017f0539 4235
239c932b
RS
4236 /* Put all the overlays we want in a vector in overlay_vec.
4237 Store the length in len.
daa1c109 4238 prevpos gets the position of the previous change. */
328ab8e7 4239 overlays_at (XINT (pos), 1, &overlay_vec, &len,
d311d28c 4240 0, &prevpos, 1);
239c932b 4241
239c932b
RS
4242 xfree (overlay_vec);
4243 return make_number (prevpos);
4244}
2eec3b4e
RS
4245\f
4246/* These functions are for debugging overlays. */
4247
4248DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
7ee72033 4249 doc: /* Return a pair of lists giving all the overlays of the current buffer.
018ba359
PJ
4250The car has all the overlays before the overlay center;
4251the cdr has all the overlays after the overlay center.
4252Recentering overlays moves overlays between these lists.
4253The lists you get are copies, so that changing them has no effect.
7ee72033 4254However, the overlays you get are the real objects that the buffer uses. */)
5842a27b 4255 (void)
2eec3b4e 4256{
2410d73a
SM
4257 struct Lisp_Overlay *ol;
4258 Lisp_Object before = Qnil, after = Qnil, tmp;
4cb3e6b3 4259
fd318b54 4260 for (ol = current_buffer->overlays_before; ol; ol = ol->next)
2410d73a
SM
4261 {
4262 XSETMISC (tmp, ol);
4263 before = Fcons (tmp, before);
4264 }
fd318b54 4265 for (ol = current_buffer->overlays_after; ol; ol = ol->next)
2410d73a
SM
4266 {
4267 XSETMISC (tmp, ol);
4268 after = Fcons (tmp, after);
4269 }
4cb3e6b3 4270
2410d73a 4271 return Fcons (Fnreverse (before), Fnreverse (after));
2eec3b4e
RS
4272}
4273
4274DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
c87426c5
RS
4275 doc: /* Recenter the overlays of the current buffer around position POS.
4276That makes overlay lookup faster for positions near POS (but perhaps slower
4277for positions far away from POS). */)
5842a27b 4278 (Lisp_Object pos)
2eec3b4e 4279{
d311d28c 4280 ptrdiff_t p;
b7826503 4281 CHECK_NUMBER_COERCE_MARKER (pos);
2eec3b4e 4282
d311d28c
PE
4283 p = clip_to_bounds (PTRDIFF_MIN, XINT (pos), PTRDIFF_MAX);
4284 recenter_overlay_lists (current_buffer, p);
2eec3b4e
RS
4285 return Qnil;
4286}
4287\f
a7ca3326 4288DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
7ee72033 4289 doc: /* Get the property of overlay OVERLAY with property name PROP. */)
5842a27b 4290 (Lisp_Object overlay, Lisp_Object prop)
2eec3b4e 4291{
b7826503 4292 CHECK_OVERLAY (overlay);
c644523b 4293 return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
2eec3b4e
RS
4294}
4295
4296DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
7c301272
LMI
4297 doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE.
4298VALUE will be returned.*/)
5842a27b 4299 (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
2eec3b4e 4300{
48e2e3ba 4301 Lisp_Object tail, buffer;
37ef52bb 4302 bool changed;
2eec3b4e 4303
b7826503 4304 CHECK_OVERLAY (overlay);
b61982dd 4305
274a9425
RS
4306 buffer = Fmarker_buffer (OVERLAY_START (overlay));
4307
c644523b 4308 for (tail = XOVERLAY (overlay)->plist;
7539e11f
KR
4309 CONSP (tail) && CONSP (XCDR (tail));
4310 tail = XCDR (XCDR (tail)))
4311 if (EQ (XCAR (tail), prop))
274a9425 4312 {
7539e11f 4313 changed = !EQ (XCAR (XCDR (tail)), value);
f3fbd155 4314 XSETCAR (XCDR (tail), value);
9d7608b7 4315 goto found;
274a9425 4316 }
9d7608b7
KH
4317 /* It wasn't in the list, so add it to the front. */
4318 changed = !NILP (value);
c644523b
DA
4319 set_overlay_plist
4320 (overlay, Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist)));
9d7608b7
KH
4321 found:
4322 if (! NILP (buffer))
4323 {
4324 if (changed)
876aa27c 4325 modify_overlay (XBUFFER (buffer),
26f545d7
GM
4326 marker_position (OVERLAY_START (overlay)),
4327 marker_position (OVERLAY_END (overlay)));
9d7608b7
KH
4328 if (EQ (prop, Qevaporate) && ! NILP (value)
4329 && (OVERLAY_POSITION (OVERLAY_START (overlay))
4330 == OVERLAY_POSITION (OVERLAY_END (overlay))))
4331 Fdelete_overlay (overlay);
4332 }
7d63db98 4333
2eec3b4e 4334 return value;
1ab256cb
RM
4335}
4336\f
9115729e
KH
4337/* Subroutine of report_overlay_modification. */
4338
4339/* Lisp vector holding overlay hook functions to call.
4340 Vector elements come in pairs.
4341 Each even-index element is a list of hook functions.
4342 The following odd-index element is the overlay they came from.
4343
4344 Before the buffer change, we fill in this vector
4345 as we call overlay hook functions.
4346 After the buffer change, we get the functions to call from this vector.
4347 This way we always call the same functions before and after the change. */
4348static Lisp_Object last_overlay_modification_hooks;
4349
4350/* Number of elements actually used in last_overlay_modification_hooks. */
d311d28c 4351static ptrdiff_t last_overlay_modification_hooks_used;
9115729e
KH
4352
4353/* Add one functionlist/overlay pair
4354 to the end of last_overlay_modification_hooks. */
4355
4356static void
d3da34e0 4357add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
9115729e 4358{
d311d28c 4359 ptrdiff_t oldsize = ASIZE (last_overlay_modification_hooks);
9115729e 4360
d311d28c
PE
4361 if (oldsize - 1 <= last_overlay_modification_hooks_used)
4362 last_overlay_modification_hooks =
4363 larger_vector (last_overlay_modification_hooks, 2, -1);
3ae565b3
SM
4364 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4365 functionlist); last_overlay_modification_hooks_used++;
4366 ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
4367 overlay); last_overlay_modification_hooks_used++;
9115729e
KH
4368}
4369\f
173f2a64
RS
4370/* Run the modification-hooks of overlays that include
4371 any part of the text in START to END.
9115729e
KH
4372 If this change is an insertion, also
4373 run the insert-before-hooks of overlay starting at END,
930a9140
RS
4374 and the insert-after-hooks of overlay ending at START.
4375
4376 This is called both before and after the modification.
37ef52bb 4377 AFTER is true when we call after the modification.
930a9140 4378
9115729e
KH
4379 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
4380 When AFTER is nonzero, they are the start position,
4381 the position after the inserted new text,
4382 and the length of deleted or replaced old text. */
173f2a64
RS
4383
4384void
37ef52bb 4385report_overlay_modification (Lisp_Object start, Lisp_Object end, bool after,
d3da34e0 4386 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
173f2a64 4387{
2410d73a
SM
4388 Lisp_Object prop, overlay;
4389 struct Lisp_Overlay *tail;
37ef52bb
PE
4390 /* True if this change is an insertion. */
4391 bool insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
a615c6dc 4392 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
55b48893
RS
4393
4394 overlay = Qnil;
2410d73a 4395 tail = NULL;
9115729e 4396
27c6b98e
SM
4397 /* We used to run the functions as soon as we found them and only register
4398 them in last_overlay_modification_hooks for the purpose of the `after'
4399 case. But running elisp code as we traverse the list of overlays is
4400 painful because the list can be modified by the elisp code so we had to
4401 copy at several places. We now simply do a read-only traversal that
4402 only collects the functions to run and we run them afterwards. It's
4403 simpler, especially since all the code was already there. -stef */
4404
a615c6dc 4405 if (!after)
173f2a64 4406 {
a615c6dc
SM
4407 /* We are being called before a change.
4408 Scan the overlays to find the functions to call. */
4409 last_overlay_modification_hooks_used = 0;
fd318b54 4410 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
173f2a64 4411 {
d311d28c 4412 ptrdiff_t startpos, endpos;
a615c6dc
SM
4413 Lisp_Object ostart, oend;
4414
2410d73a 4415 XSETMISC (overlay, tail);
a615c6dc
SM
4416
4417 ostart = OVERLAY_START (overlay);
4418 oend = OVERLAY_END (overlay);
4419 endpos = OVERLAY_POSITION (oend);
4420 if (XFASTINT (start) > endpos)
4421 break;
4422 startpos = OVERLAY_POSITION (ostart);
4423 if (insertion && (XFASTINT (start) == startpos
4424 || XFASTINT (end) == startpos))
5fb5aa33 4425 {
a615c6dc
SM
4426 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4427 if (!NILP (prop))
4428 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4429 }
a615c6dc
SM
4430 if (insertion && (XFASTINT (start) == endpos
4431 || XFASTINT (end) == endpos))
5fb5aa33 4432 {
a615c6dc
SM
4433 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4434 if (!NILP (prop))
4435 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4436 }
a615c6dc
SM
4437 /* Test for intersecting intervals. This does the right thing
4438 for both insertion and deletion. */
4439 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4440 {
a615c6dc
SM
4441 prop = Foverlay_get (overlay, Qmodification_hooks);
4442 if (!NILP (prop))
4443 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4444 }
173f2a64 4445 }
60ebfdf3 4446
fd318b54 4447 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
173f2a64 4448 {
d311d28c 4449 ptrdiff_t startpos, endpos;
a615c6dc
SM
4450 Lisp_Object ostart, oend;
4451
2410d73a 4452 XSETMISC (overlay, tail);
a615c6dc
SM
4453
4454 ostart = OVERLAY_START (overlay);
4455 oend = OVERLAY_END (overlay);
4456 startpos = OVERLAY_POSITION (ostart);
4457 endpos = OVERLAY_POSITION (oend);
4458 if (XFASTINT (end) < startpos)
4459 break;
4460 if (insertion && (XFASTINT (start) == startpos
4461 || XFASTINT (end) == startpos))
5fb5aa33 4462 {
a615c6dc
SM
4463 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
4464 if (!NILP (prop))
4465 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4466 }
a615c6dc
SM
4467 if (insertion && (XFASTINT (start) == endpos
4468 || XFASTINT (end) == endpos))
5fb5aa33 4469 {
a615c6dc
SM
4470 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
4471 if (!NILP (prop))
4472 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4473 }
a615c6dc
SM
4474 /* Test for intersecting intervals. This does the right thing
4475 for both insertion and deletion. */
4476 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
5fb5aa33 4477 {
a615c6dc
SM
4478 prop = Foverlay_get (overlay, Qmodification_hooks);
4479 if (!NILP (prop))
4480 add_overlay_mod_hooklist (prop, overlay);
5fb5aa33 4481 }
173f2a64
RS
4482 }
4483 }
55b48893 4484
a615c6dc
SM
4485 GCPRO4 (overlay, arg1, arg2, arg3);
4486 {
4487 /* Call the functions recorded in last_overlay_modification_hooks.
4488 First copy the vector contents, in case some of these hooks
4489 do subsequent modification of the buffer. */
d311d28c 4490 ptrdiff_t size = last_overlay_modification_hooks_used;
38182d90 4491 Lisp_Object *copy = alloca (size * sizeof *copy);
d311d28c 4492 ptrdiff_t i;
a615c6dc 4493
72af86bd 4494 memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
663e2b3f 4495 size * word_size);
a615c6dc
SM
4496 gcpro1.var = copy;
4497 gcpro1.nvars = size;
4498
4499 for (i = 0; i < size;)
4500 {
8f54f30a
PE
4501 Lisp_Object prop_i, overlay_i;
4502 prop_i = copy[i++];
4503 overlay_i = copy[i++];
4504 call_overlay_mod_hooks (prop_i, overlay_i, after, arg1, arg2, arg3);
a615c6dc
SM
4505 }
4506 }
55b48893 4507 UNGCPRO;
173f2a64
RS
4508}
4509
4510static void
37ef52bb 4511call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, bool after,
d3da34e0 4512 Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
173f2a64 4513{
930a9140 4514 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9115729e 4515
930a9140 4516 GCPRO4 (list, arg1, arg2, arg3);
9115729e 4517
6d70a280 4518 while (CONSP (list))
173f2a64 4519 {
930a9140 4520 if (NILP (arg3))
6d70a280 4521 call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
930a9140 4522 else
6d70a280
SM
4523 call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
4524 list = XCDR (list);
173f2a64
RS
4525 }
4526 UNGCPRO;
4527}
9d7608b7
KH
4528
4529/* Delete any zero-sized overlays at position POS, if the `evaporate'
4530 property is set. */
4531void
d311d28c 4532evaporate_overlays (ptrdiff_t pos)
9d7608b7 4533{
2410d73a
SM
4534 Lisp_Object overlay, hit_list;
4535 struct Lisp_Overlay *tail;
9d7608b7
KH
4536
4537 hit_list = Qnil;
c2d5b10f 4538 if (pos <= current_buffer->overlay_center)
fd318b54 4539 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
9d7608b7 4540 {
d311d28c 4541 ptrdiff_t endpos;
2410d73a 4542 XSETMISC (overlay, tail);
9d7608b7
KH
4543 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4544 if (endpos < pos)
4545 break;
4546 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
c3935f9d 4547 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4548 hit_list = Fcons (overlay, hit_list);
4549 }
4550 else
fd318b54 4551 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
9d7608b7 4552 {
d311d28c 4553 ptrdiff_t startpos;
2410d73a 4554 XSETMISC (overlay, tail);
9d7608b7
KH
4555 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
4556 if (startpos > pos)
4557 break;
4558 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
c3935f9d 4559 && ! NILP (Foverlay_get (overlay, Qevaporate)))
9d7608b7
KH
4560 hit_list = Fcons (overlay, hit_list);
4561 }
7539e11f
KR
4562 for (; CONSP (hit_list); hit_list = XCDR (hit_list))
4563 Fdelete_overlay (XCAR (hit_list));
9d7608b7 4564}
173f2a64 4565\f
54dfdeb0 4566/* Somebody has tried to store a value with an unacceptable type
1bf08baf
KH
4567 in the slot with offset OFFSET. */
4568
0fa3ba92 4569void
d3da34e0 4570buffer_slot_type_mismatch (Lisp_Object newval, int type)
0fa3ba92 4571{
ad97b375 4572 Lisp_Object predicate;
177c0ea7 4573
64e16c3c 4574 switch (type)
0fa3ba92 4575 {
2de9f71c 4576 case_Lisp_Int: predicate = Qintegerp; break;
ad97b375
SM
4577 case Lisp_String: predicate = Qstringp; break;
4578 case Lisp_Symbol: predicate = Qsymbolp; break;
4579 default: abort ();
0fa3ba92
JB
4580 }
4581
ad97b375 4582 wrong_type_argument (predicate, newval);
0fa3ba92 4583}
7c02e886 4584
0fa3ba92 4585\f
b86af064
GM
4586/***********************************************************************
4587 Allocation with mmap
4588 ***********************************************************************/
4589
4590#ifdef USE_MMAP_FOR_BUFFERS
4591
4592#include <sys/types.h>
4593#include <sys/mman.h>
4594
4595#ifndef MAP_ANON
4596#ifdef MAP_ANONYMOUS
4597#define MAP_ANON MAP_ANONYMOUS
4598#else
4599#define MAP_ANON 0
4600#endif
4601#endif
4602
09dfdf85
GM
4603#ifndef MAP_FAILED
4604#define MAP_FAILED ((void *) -1)
4605#endif
4606
b86af064 4607#include <stdio.h>
b86af064
GM
4608
4609#if MAP_ANON == 0
4610#include <fcntl.h>
4611#endif
4612
4613#include "coding.h"
4614
4615
4616/* Memory is allocated in regions which are mapped using mmap(2).
4617 The current implementation lets the system select mapped
4618 addresses; we're not using MAP_FIXED in general, except when
4619 trying to enlarge regions.
4620
4621 Each mapped region starts with a mmap_region structure, the user
4622 area starts after that structure, aligned to MEM_ALIGN.
4623
4624 +-----------------------+
4625 | struct mmap_info + |
4626 | padding |
4627 +-----------------------+
4628 | user data |
4629 | |
4630 | |
4631 +-----------------------+ */
4632
4633struct mmap_region
4634{
4635 /* User-specified size. */
4636 size_t nbytes_specified;
177c0ea7 4637
b86af064
GM
4638 /* Number of bytes mapped */
4639 size_t nbytes_mapped;
4640
4641 /* Pointer to the location holding the address of the memory
4642 allocated with the mmap'd block. The variable actually points
4643 after this structure. */
261cb4bb 4644 void **var;
b86af064
GM
4645
4646 /* Next and previous in list of all mmap'd regions. */
4647 struct mmap_region *next, *prev;
4648};
4649
4650/* Doubly-linked list of mmap'd regions. */
4651
4652static struct mmap_region *mmap_regions;
4653
4654/* File descriptor for mmap. If we don't have anonymous mapping,
4655 /dev/zero will be opened on it. */
4656
4657static int mmap_fd;
4658
4659/* Temporary storage for mmap_set_vars, see there. */
4660
4661static struct mmap_region *mmap_regions_1;
4662static int mmap_fd_1;
4663
4664/* Page size on this system. */
4665
4666static int mmap_page_size;
4667
cd1181db 4668/* 1 means mmap has been initialized. */
b86af064 4669
37ef52bb 4670static bool mmap_initialized_p;
b86af064
GM
4671
4672/* Value is X rounded up to the next multiple of N. */
4673
4674#define ROUND(X, N) (((X) + (N) - 1) / (N) * (N))
4675
4676/* Size of mmap_region structure plus padding. */
4677
4678#define MMAP_REGION_STRUCT_SIZE \
4679 ROUND (sizeof (struct mmap_region), MEM_ALIGN)
4680
4681/* Given a pointer P to the start of the user-visible part of a mapped
4682 region, return a pointer to the start of the region. */
4683
4684#define MMAP_REGION(P) \
4685 ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
4686
4687/* Given a pointer P to the start of a mapped region, return a pointer
4688 to the start of the user-visible part of the region. */
4689
4690#define MMAP_USER_AREA(P) \
261cb4bb 4691 ((void *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
b86af064
GM
4692
4693#define MEM_ALIGN sizeof (double)
4694
7273faa1
DL
4695/* Predicate returning true if part of the address range [START .. END]
4696 is currently mapped. Used to prevent overwriting an existing
08327b22
GM
4697 memory mapping.
4698
da6062e6 4699 Default is to conservatively assume the address range is occupied by
08327b22
GM
4700 something else. This can be overridden by system configuration
4701 files if system-specific means to determine this exists. */
4702
4703#ifndef MMAP_ALLOCATED_P
4704#define MMAP_ALLOCATED_P(start, end) 1
4705#endif
4706
53964682 4707/* Perform necessary initializations for the use of mmap. */
b86af064 4708
1dae0f0a
AS
4709static void
4710mmap_init (void)
4711{
4712#if MAP_ANON == 0
4713 /* The value of mmap_fd is initially 0 in temacs, and -1
4714 in a dumped Emacs. */
4715 if (mmap_fd <= 0)
4716 {
4717 /* No anonymous mmap -- we need the file descriptor. */
4718 mmap_fd = open ("/dev/zero", O_RDONLY);
4719 if (mmap_fd == -1)
4720 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
4721 }
4722#endif /* MAP_ANON == 0 */
4723
4724 if (mmap_initialized_p)
4725 return;
4726 mmap_initialized_p = 1;
b86af064 4727
1dae0f0a
AS
4728#if MAP_ANON != 0
4729 mmap_fd = -1;
4730#endif
4731
4732 mmap_page_size = getpagesize ();
4733}
b86af064
GM
4734
4735/* Return a region overlapping address range START...END, or null if
4736 none. END is not including, i.e. the last byte in the range
4737 is at END - 1. */
4738
4739static struct mmap_region *
261cb4bb 4740mmap_find (void *start, void *end)
b86af064
GM
4741{
4742 struct mmap_region *r;
4743 char *s = (char *) start, *e = (char *) end;
177c0ea7 4744
b86af064
GM
4745 for (r = mmap_regions; r; r = r->next)
4746 {
4747 char *rstart = (char *) r;
4748 char *rend = rstart + r->nbytes_mapped;
4749
4750 if (/* First byte of range, i.e. START, in this region? */
4751 (s >= rstart && s < rend)
4752 /* Last byte of range, i.e. END - 1, in this region? */
4753 || (e > rstart && e <= rend)
4754 /* First byte of this region in the range? */
4755 || (rstart >= s && rstart < e)
4756 /* Last byte of this region in the range? */
4757 || (rend > s && rend <= e))
4758 break;
4759 }
4760
4761 return r;
4762}
4763
4764
4765/* Unmap a region. P is a pointer to the start of the user-araa of
37ef52bb 4766 the region. */
b86af064 4767
37ef52bb 4768static void
1dae0f0a 4769mmap_free_1 (struct mmap_region *r)
b86af064
GM
4770{
4771 if (r->next)
4772 r->next->prev = r->prev;
4773 if (r->prev)
4774 r->prev->next = r->next;
4775 else
4776 mmap_regions = r->next;
177c0ea7 4777
261cb4bb 4778 if (munmap (r, r->nbytes_mapped) == -1)
37ef52bb 4779 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
b86af064
GM
4780}
4781
4782
4783/* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R.
37ef52bb 4784 Value is true if successful. */
b86af064 4785
37ef52bb 4786static bool
1dae0f0a 4787mmap_enlarge (struct mmap_region *r, int npages)
b86af064
GM
4788{
4789 char *region_end = (char *) r + r->nbytes_mapped;
4790 size_t nbytes;
37ef52bb 4791 bool success = 0;
b86af064
GM
4792
4793 if (npages < 0)
4794 {
4795 /* Unmap pages at the end of the region. */
4796 nbytes = - npages * mmap_page_size;
4797 if (munmap (region_end - nbytes, nbytes) == -1)
4798 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4799 else
4800 {
4801 r->nbytes_mapped -= nbytes;
4802 success = 1;
4803 }
4804 }
4805 else if (npages > 0)
4806 {
b86af064 4807 nbytes = npages * mmap_page_size;
177c0ea7 4808
b86af064
GM
4809 /* Try to map additional pages at the end of the region. We
4810 cannot do this if the address range is already occupied by
4811 something else because mmap deletes any previous mapping.
4812 I'm not sure this is worth doing, let's see. */
08327b22 4813 if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
b86af064 4814 {
261cb4bb 4815 void *p;
177c0ea7 4816
b86af064
GM
4817 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
4818 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
4819 if (p == MAP_FAILED)
edaa9aed 4820 ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
261cb4bb 4821 else if (p != region_end)
b86af064
GM
4822 {
4823 /* Kernels are free to choose a different address. In
4824 that case, unmap what we've mapped above; we have
4825 no use for it. */
4826 if (munmap (p, nbytes) == -1)
4827 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
4828 }
4829 else
4830 {
4831 r->nbytes_mapped += nbytes;
4832 success = 1;
4833 }
4834 }
4835 }
4836
4837 return success;
4838}
4839
4840
37ef52bb
PE
4841/* Set or reset variables holding references to mapped regions.
4842 If not RESTORE_P, set all variables to null. If RESTORE_P, set all
4843 variables to the start of the user-areas of mapped regions.
b86af064
GM
4844
4845 This function is called from Fdump_emacs to ensure that the dumped
4846 Emacs doesn't contain references to memory that won't be mapped
4847 when Emacs starts. */
4848
4849void
37ef52bb 4850mmap_set_vars (bool restore_p)
b86af064
GM
4851{
4852 struct mmap_region *r;
4853
4854 if (restore_p)
4855 {
4856 mmap_regions = mmap_regions_1;
4857 mmap_fd = mmap_fd_1;
4858 for (r = mmap_regions; r; r = r->next)
4859 *r->var = MMAP_USER_AREA (r);
4860 }
4861 else
4862 {
4863 for (r = mmap_regions; r; r = r->next)
4864 *r->var = NULL;
4865 mmap_regions_1 = mmap_regions;
4866 mmap_regions = NULL;
4867 mmap_fd_1 = mmap_fd;
4868 mmap_fd = -1;
4869 }
4870}
4871
4872
4873/* Allocate a block of storage large enough to hold NBYTES bytes of
4874 data. A pointer to the data is returned in *VAR. VAR is thus the
4875 address of some variable which will use the data area.
4876
4877 The allocation of 0 bytes is valid.
4878
4879 If we can't allocate the necessary memory, set *VAR to null, and
4880 return null. */
4881
261cb4bb
PE
4882static void *
4883mmap_alloc (void **var, size_t nbytes)
b86af064
GM
4884{
4885 void *p;
4886 size_t map;
4887
4888 mmap_init ();
4889
4890 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
4891 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
4892 mmap_fd, 0);
177c0ea7 4893
b86af064
GM
4894 if (p == MAP_FAILED)
4895 {
4896 if (errno != ENOMEM)
4897 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
4898 p = NULL;
4899 }
4900 else
4901 {
4902 struct mmap_region *r = (struct mmap_region *) p;
177c0ea7 4903
b86af064
GM
4904 r->nbytes_specified = nbytes;
4905 r->nbytes_mapped = map;
4906 r->var = var;
4907 r->prev = NULL;
4908 r->next = mmap_regions;
4909 if (r->next)
4910 r->next->prev = r;
4911 mmap_regions = r;
177c0ea7 4912
b86af064
GM
4913 p = MMAP_USER_AREA (p);
4914 }
177c0ea7 4915
b86af064
GM
4916 return *var = p;
4917}
4918
4919
1dae0f0a
AS
4920/* Free a block of relocatable storage whose data is pointed to by
4921 PTR. Store 0 in *PTR to show there's no block allocated. */
4922
4923static void
261cb4bb 4924mmap_free (void **var)
1dae0f0a
AS
4925{
4926 mmap_init ();
4927
4928 if (*var)
4929 {
4930 mmap_free_1 (MMAP_REGION (*var));
4931 *var = NULL;
4932 }
4933}
4934
4935
b86af064
GM
4936/* Given a pointer at address VAR to data allocated with mmap_alloc,
4937 resize it to size NBYTES. Change *VAR to reflect the new block,
4938 and return this value. If more memory cannot be allocated, then
4939 leave *VAR unchanged, and return null. */
4940
261cb4bb
PE
4941static void *
4942mmap_realloc (void **var, size_t nbytes)
b86af064 4943{
261cb4bb 4944 void *result;
177c0ea7 4945
b86af064
GM
4946 mmap_init ();
4947
4948 if (*var == NULL)
4949 result = mmap_alloc (var, nbytes);
177c0ea7 4950 else if (nbytes == 0)
b86af064
GM
4951 {
4952 mmap_free (var);
4953 result = mmap_alloc (var, nbytes);
4954 }
4955 else
4956 {
4957 struct mmap_region *r = MMAP_REGION (*var);
4958 size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
177c0ea7 4959
b86af064
GM
4960 if (room < nbytes)
4961 {
4962 /* Must enlarge. */
261cb4bb 4963 void *old_ptr = *var;
b86af064
GM
4964
4965 /* Try to map additional pages at the end of the region.
4966 If that fails, allocate a new region, copy data
4967 from the old region, then free it. */
4968 if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
4969 / mmap_page_size)))
4970 {
4971 r->nbytes_specified = nbytes;
4972 *var = result = old_ptr;
4973 }
4974 else if (mmap_alloc (var, nbytes))
4975 {
72af86bd 4976 memcpy (*var, old_ptr, r->nbytes_specified);
b86af064
GM
4977 mmap_free_1 (MMAP_REGION (old_ptr));
4978 result = *var;
4979 r = MMAP_REGION (result);
4980 r->nbytes_specified = nbytes;
4981 }
4982 else
4983 {
4984 *var = old_ptr;
4985 result = NULL;
4986 }
4987 }
4988 else if (room - nbytes >= mmap_page_size)
4989 {
4990 /* Shrinking by at least a page. Let's give some
6bcdeb8c
KR
4991 memory back to the system.
4992
4993 The extra parens are to make the division happens first,
4994 on positive values, so we know it will round towards
4995 zero. */
bb63c5c9 4996 mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
b86af064
GM
4997 result = *var;
4998 r->nbytes_specified = nbytes;
4999 }
5000 else
5001 {
5002 /* Leave it alone. */
5003 result = *var;
5004 r->nbytes_specified = nbytes;
5005 }
5006 }
5007
5008 return result;
5009}
5010
5011
b86af064
GM
5012#endif /* USE_MMAP_FOR_BUFFERS */
5013
5014
5015\f
5016/***********************************************************************
5017 Buffer-text Allocation
5018 ***********************************************************************/
5019
b86af064
GM
5020/* Allocate NBYTES bytes for buffer B's text buffer. */
5021
5022static void
fd05c7e9 5023alloc_buffer_text (struct buffer *b, ptrdiff_t nbytes)
b86af064 5024{
261cb4bb 5025 void *p;
177c0ea7 5026
b86af064
GM
5027 BLOCK_INPUT;
5028#if defined USE_MMAP_FOR_BUFFERS
261cb4bb 5029 p = mmap_alloc ((void **) &b->text->beg, nbytes);
b86af064 5030#elif defined REL_ALLOC
261cb4bb 5031 p = r_alloc ((void **) &b->text->beg, nbytes);
b86af064 5032#else
815add84 5033 p = xmalloc (nbytes);
b86af064 5034#endif
177c0ea7 5035
b86af064
GM
5036 if (p == NULL)
5037 {
5038 UNBLOCK_INPUT;
531b0165 5039 memory_full (nbytes);
b86af064
GM
5040 }
5041
5042 b->text->beg = (unsigned char *) p;
5043 UNBLOCK_INPUT;
5044}
5045
5046/* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means
5047 shrink it. */
5048
5049void
d311d28c 5050enlarge_buffer_text (struct buffer *b, ptrdiff_t delta)
b86af064 5051{
261cb4bb 5052 void *p;
fd05c7e9
PE
5053 ptrdiff_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
5054 + delta);
b86af064
GM
5055 BLOCK_INPUT;
5056#if defined USE_MMAP_FOR_BUFFERS
261cb4bb 5057 p = mmap_realloc ((void **) &b->text->beg, nbytes);
b86af064 5058#elif defined REL_ALLOC
261cb4bb 5059 p = r_re_alloc ((void **) &b->text->beg, nbytes);
b86af064
GM
5060#else
5061 p = xrealloc (b->text->beg, nbytes);
5062#endif
177c0ea7 5063
b86af064
GM
5064 if (p == NULL)
5065 {
5066 UNBLOCK_INPUT;
531b0165 5067 memory_full (nbytes);
b86af064
GM
5068 }
5069
5070 BUF_BEG_ADDR (b) = (unsigned char *) p;
5071 UNBLOCK_INPUT;
5072}
5073
5074
5075/* Free buffer B's text buffer. */
5076
5077static void
d3da34e0 5078free_buffer_text (struct buffer *b)
b86af064
GM
5079{
5080 BLOCK_INPUT;
5081
5082#if defined USE_MMAP_FOR_BUFFERS
261cb4bb 5083 mmap_free ((void **) &b->text->beg);
b86af064 5084#elif defined REL_ALLOC
261cb4bb 5085 r_alloc_free ((void **) &b->text->beg);
b86af064
GM
5086#else
5087 xfree (b->text->beg);
5088#endif
177c0ea7 5089
b86af064
GM
5090 BUF_BEG_ADDR (b) = NULL;
5091 UNBLOCK_INPUT;
5092}
5093
5094
5095\f
5096/***********************************************************************
5097 Initialization
5098 ***********************************************************************/
5099
dfcf069d 5100void
d3da34e0 5101init_buffer_once (void)
1ab256cb 5102{
7c02e886 5103 int idx;
3884d954
DA
5104 /* If you add, remove, or reorder Lisp_Objects in a struct buffer, make
5105 sure that this is still correct. Otherwise, mark_vectorlike may not
5106 trace all Lisp_Objects in buffer_defaults and buffer_local_symbols. */
38182d90 5107 const int pvecsize
663e2b3f 5108 = (offsetof (struct buffer, own_text) - header_size) / word_size;
7c02e886 5109
72af86bd 5110 memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
13de9290 5111
1ab256cb
RM
5112 /* Make sure all markable slots in buffer_defaults
5113 are initialized reasonably, so mark_buffer won't choke. */
5114 reset_buffer (&buffer_defaults);
4b4deea2 5115 eassert (EQ (BVAR (&buffer_defaults, name), make_number (0)));
13de9290 5116 reset_buffer_local_variables (&buffer_defaults, 1);
4b4deea2 5117 eassert (EQ (BVAR (&buffer_local_symbols, name), make_number (0)));
1ab256cb 5118 reset_buffer (&buffer_local_symbols);
13de9290 5119 reset_buffer_local_variables (&buffer_local_symbols, 1);
336cd056
RS
5120 /* Prevent GC from getting confused. */
5121 buffer_defaults.text = &buffer_defaults.own_text;
5122 buffer_local_symbols.text = &buffer_local_symbols.own_text;
04e9897c
DA
5123 /* No one will share the text with these buffers, but let's play it safe. */
5124 buffer_defaults.indirections = 0;
5125 buffer_local_symbols.indirections = 0;
0c94c8d6
PE
5126 set_buffer_intervals (&buffer_defaults, NULL);
5127 set_buffer_intervals (&buffer_local_symbols, NULL);
3884d954 5128 XSETPVECTYPESIZE (&buffer_defaults, PVEC_BUFFER, pvecsize);
67180c6a 5129 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3884d954 5130 XSETPVECTYPESIZE (&buffer_local_symbols, PVEC_BUFFER, pvecsize);
67180c6a 5131 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
1ab256cb
RM
5132
5133 /* Set up the default values of various buffer slots. */
5134 /* Must do these before making the first buffer! */
5135
f532dca0 5136 /* real setup is done in bindings.el */
39eb03f1
PE
5137 bset_mode_line_format (&buffer_defaults, build_pure_c_string ("%-"));
5138 bset_header_line_format (&buffer_defaults, Qnil);
5139 bset_abbrev_mode (&buffer_defaults, Qnil);
5140 bset_overwrite_mode (&buffer_defaults, Qnil);
5141 bset_case_fold_search (&buffer_defaults, Qt);
5142 bset_auto_fill_function (&buffer_defaults, Qnil);
5143 bset_selective_display (&buffer_defaults, Qnil);
5144 bset_selective_display_ellipses (&buffer_defaults, Qt);
5145 bset_abbrev_table (&buffer_defaults, Qnil);
5146 bset_display_table (&buffer_defaults, Qnil);
5147 bset_undo_list (&buffer_defaults, Qnil);
5148 bset_mark_active (&buffer_defaults, Qnil);
5149 bset_file_format (&buffer_defaults, Qnil);
5150 bset_auto_save_file_format (&buffer_defaults, Qt);
0c94c8d6
PE
5151 set_buffer_overlays_before (&buffer_defaults, NULL);
5152 set_buffer_overlays_after (&buffer_defaults, NULL);
c2d5b10f 5153 buffer_defaults.overlay_center = BEG;
1ab256cb 5154
4b4deea2 5155 XSETFASTINT (BVAR (&buffer_defaults, tab_width), 8);
39eb03f1
PE
5156 bset_truncate_lines (&buffer_defaults, Qnil);
5157 bset_word_wrap (&buffer_defaults, Qnil);
5158 bset_ctl_arrow (&buffer_defaults, Qt);
5159 bset_bidi_display_reordering (&buffer_defaults, Qt);
5160 bset_bidi_paragraph_direction (&buffer_defaults, Qnil);
5161 bset_cursor_type (&buffer_defaults, Qt);
5162 bset_extra_line_spacing (&buffer_defaults, Qnil);
5163 bset_cursor_in_non_selected_windows (&buffer_defaults, Qt);
5164
5165 bset_enable_multibyte_characters (&buffer_defaults, Qt);
5166 bset_buffer_file_coding_system (&buffer_defaults, Qnil);
4b4deea2
TT
5167 XSETFASTINT (BVAR (&buffer_defaults, fill_column), 70);
5168 XSETFASTINT (BVAR (&buffer_defaults, left_margin), 0);
39eb03f1
PE
5169 bset_cache_long_line_scans (&buffer_defaults, Qnil);
5170 bset_file_truename (&buffer_defaults, Qnil);
4b4deea2
TT
5171 XSETFASTINT (BVAR (&buffer_defaults, display_count), 0);
5172 XSETFASTINT (BVAR (&buffer_defaults, left_margin_cols), 0);
5173 XSETFASTINT (BVAR (&buffer_defaults, right_margin_cols), 0);
39eb03f1
PE
5174 bset_left_fringe_width (&buffer_defaults, Qnil);
5175 bset_right_fringe_width (&buffer_defaults, Qnil);
5176 bset_fringes_outside_margins (&buffer_defaults, Qnil);
5177 bset_scroll_bar_width (&buffer_defaults, Qnil);
5178 bset_vertical_scroll_bar_type (&buffer_defaults, Qt);
5179 bset_indicate_empty_lines (&buffer_defaults, Qnil);
5180 bset_indicate_buffer_boundaries (&buffer_defaults, Qnil);
5181 bset_fringe_indicator_alist (&buffer_defaults, Qnil);
5182 bset_fringe_cursor_alist (&buffer_defaults, Qnil);
5183 bset_scroll_up_aggressively (&buffer_defaults, Qnil);
5184 bset_scroll_down_aggressively (&buffer_defaults, Qnil);
5185 bset_display_time (&buffer_defaults, Qnil);
1ab256cb
RM
5186
5187 /* Assign the local-flags to the slots that have default values.
5188 The local flag is a bit that is used in the buffer
5189 to say that it has its own local value for the slot.
5190 The local flag bits are in the local_var_flags slot of the buffer. */
5191
5192 /* Nothing can work if this isn't true */
663e2b3f 5193 { verify (sizeof (EMACS_INT) == word_size); }
1ab256cb
RM
5194
5195 /* 0 means not a lisp var, -1 means always local, else mask */
72af86bd 5196 memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
39eb03f1
PE
5197 bset_filename (&buffer_local_flags, make_number (-1));
5198 bset_directory (&buffer_local_flags, make_number (-1));
5199 bset_backed_up (&buffer_local_flags, make_number (-1));
5200 bset_save_length (&buffer_local_flags, make_number (-1));
5201 bset_auto_save_file_name (&buffer_local_flags, make_number (-1));
5202 bset_read_only (&buffer_local_flags, make_number (-1));
5203 bset_major_mode (&buffer_local_flags, make_number (-1));
5204 bset_mode_name (&buffer_local_flags, make_number (-1));
5205 bset_undo_list (&buffer_local_flags, make_number (-1));
5206 bset_mark_active (&buffer_local_flags, make_number (-1));
5207 bset_point_before_scroll (&buffer_local_flags, make_number (-1));
5208 bset_file_truename (&buffer_local_flags, make_number (-1));
5209 bset_invisibility_spec (&buffer_local_flags, make_number (-1));
5210 bset_file_format (&buffer_local_flags, make_number (-1));
5211 bset_auto_save_file_format (&buffer_local_flags, make_number (-1));
5212 bset_display_count (&buffer_local_flags, make_number (-1));
5213 bset_display_time (&buffer_local_flags, make_number (-1));
5214 bset_enable_multibyte_characters (&buffer_local_flags, make_number (-1));
8d7a4592 5215
7c02e886 5216 idx = 1;
4b4deea2
TT
5217 XSETFASTINT (BVAR (&buffer_local_flags, mode_line_format), idx); ++idx;
5218 XSETFASTINT (BVAR (&buffer_local_flags, abbrev_mode), idx); ++idx;
5219 XSETFASTINT (BVAR (&buffer_local_flags, overwrite_mode), idx); ++idx;
5220 XSETFASTINT (BVAR (&buffer_local_flags, case_fold_search), idx); ++idx;
5221 XSETFASTINT (BVAR (&buffer_local_flags, auto_fill_function), idx); ++idx;
5222 XSETFASTINT (BVAR (&buffer_local_flags, selective_display), idx); ++idx;
4b4deea2 5223 XSETFASTINT (BVAR (&buffer_local_flags, selective_display_ellipses), idx); ++idx;
4b4deea2
TT
5224 XSETFASTINT (BVAR (&buffer_local_flags, tab_width), idx); ++idx;
5225 XSETFASTINT (BVAR (&buffer_local_flags, truncate_lines), idx); ++idx;
5226 XSETFASTINT (BVAR (&buffer_local_flags, word_wrap), idx); ++idx;
5227 XSETFASTINT (BVAR (&buffer_local_flags, ctl_arrow), idx); ++idx;
5228 XSETFASTINT (BVAR (&buffer_local_flags, fill_column), idx); ++idx;
5229 XSETFASTINT (BVAR (&buffer_local_flags, left_margin), idx); ++idx;
5230 XSETFASTINT (BVAR (&buffer_local_flags, abbrev_table), idx); ++idx;
5231 XSETFASTINT (BVAR (&buffer_local_flags, display_table), idx); ++idx;
4b4deea2
TT
5232 XSETFASTINT (BVAR (&buffer_local_flags, syntax_table), idx); ++idx;
5233 XSETFASTINT (BVAR (&buffer_local_flags, cache_long_line_scans), idx); ++idx;
5234 XSETFASTINT (BVAR (&buffer_local_flags, category_table), idx); ++idx;
5235 XSETFASTINT (BVAR (&buffer_local_flags, bidi_display_reordering), idx); ++idx;
5236 XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_direction), idx); ++idx;
5237 XSETFASTINT (BVAR (&buffer_local_flags, buffer_file_coding_system), idx);
a1a17b61 5238 /* Make this one a permanent local. */
7c02e886 5239 buffer_permanent_local_flags[idx++] = 1;
4b4deea2
TT
5240 XSETFASTINT (BVAR (&buffer_local_flags, left_margin_cols), idx); ++idx;
5241 XSETFASTINT (BVAR (&buffer_local_flags, right_margin_cols), idx); ++idx;
5242 XSETFASTINT (BVAR (&buffer_local_flags, left_fringe_width), idx); ++idx;
5243 XSETFASTINT (BVAR (&buffer_local_flags, right_fringe_width), idx); ++idx;
5244 XSETFASTINT (BVAR (&buffer_local_flags, fringes_outside_margins), idx); ++idx;
5245 XSETFASTINT (BVAR (&buffer_local_flags, scroll_bar_width), idx); ++idx;
5246 XSETFASTINT (BVAR (&buffer_local_flags, vertical_scroll_bar_type), idx); ++idx;
5247 XSETFASTINT (BVAR (&buffer_local_flags, indicate_empty_lines), idx); ++idx;
5248 XSETFASTINT (BVAR (&buffer_local_flags, indicate_buffer_boundaries), idx); ++idx;
5249 XSETFASTINT (BVAR (&buffer_local_flags, fringe_indicator_alist), idx); ++idx;
5250 XSETFASTINT (BVAR (&buffer_local_flags, fringe_cursor_alist), idx); ++idx;
5251 XSETFASTINT (BVAR (&buffer_local_flags, scroll_up_aggressively), idx); ++idx;
5252 XSETFASTINT (BVAR (&buffer_local_flags, scroll_down_aggressively), idx); ++idx;
5253 XSETFASTINT (BVAR (&buffer_local_flags, header_line_format), idx); ++idx;
5254 XSETFASTINT (BVAR (&buffer_local_flags, cursor_type), idx); ++idx;
5255 XSETFASTINT (BVAR (&buffer_local_flags, extra_line_spacing), idx); ++idx;
5256 XSETFASTINT (BVAR (&buffer_local_flags, cursor_in_non_selected_windows), idx); ++idx;
7c02e886
GM
5257
5258 /* Need more room? */
7313acd0 5259 if (idx >= MAX_PER_BUFFER_VARS)
7c02e886 5260 abort ();
7313acd0 5261 last_per_buffer_idx = idx;
177c0ea7 5262
1ab256cb
RM
5263 Vbuffer_alist = Qnil;
5264 current_buffer = 0;
5265 all_buffers = 0;
5266
2a0213a6 5267 QSFundamental = build_pure_c_string ("Fundamental");
1ab256cb 5268
d67b4f80 5269 Qfundamental_mode = intern_c_string ("fundamental-mode");
39eb03f1 5270 bset_major_mode (&buffer_defaults, Qfundamental_mode);
1ab256cb 5271
d67b4f80 5272 Qmode_class = intern_c_string ("mode-class");
1ab256cb 5273
d67b4f80 5274 Qprotected_field = intern_c_string ("protected-field");
1ab256cb 5275
d67b4f80 5276 Qpermanent_local = intern_c_string ("permanent-local");
1ab256cb 5277
d67b4f80 5278 Qkill_buffer_hook = intern_c_string ("kill-buffer-hook");
fd6cfe11 5279 Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
1ab256cb 5280
1ab256cb 5281 /* super-magic invisible buffer */
2a0213a6 5282 Vprin1_to_string_buffer = Fget_buffer_create (build_pure_c_string (" prin1"));
1ab256cb
RM
5283 Vbuffer_alist = Qnil;
5284
2a0213a6 5285 Fset_buffer (Fget_buffer_create (build_pure_c_string ("*scratch*")));
7775635d
KH
5286
5287 inhibit_modification_hooks = 0;
1ab256cb
RM
5288}
5289
dfcf069d 5290void
d3da34e0 5291init_buffer (void)
1ab256cb 5292{
2381d133 5293 char *pwd;
136351b7 5294 Lisp_Object temp;
965d34eb 5295 ptrdiff_t len;
1ab256cb 5296
b86af064 5297#ifdef USE_MMAP_FOR_BUFFERS
93c27ef1
GM
5298 {
5299 /* When using the ralloc implementation based on mmap(2), buffer
5300 text pointers will have been set to null in the dumped Emacs.
5301 Map new memory. */
5302 struct buffer *b;
177c0ea7 5303
52b852c7 5304 FOR_EACH_BUFFER (b)
93c27ef1 5305 if (b->text->beg == NULL)
b86af064 5306 enlarge_buffer_text (b, 0);
93c27ef1 5307 }
b86af064 5308#endif /* USE_MMAP_FOR_BUFFERS */
177c0ea7 5309
1ab256cb 5310 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
4b4deea2 5311 if (NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
3d871c85 5312 Fset_buffer_multibyte (Qnil);
2381d133 5313
01537133 5314 pwd = get_current_dir_name ();
a17b5ed1 5315
156bdb41 5316 if (!pwd)
a17b5ed1 5317 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
1ab256cb 5318
1ab256cb
RM
5319 /* Maybe this should really use some standard subroutine
5320 whose definition is filename syntax dependent. */
b639c9be
RF
5321 len = strlen (pwd);
5322 if (!(IS_DIRECTORY_SEP (pwd[len - 1])))
f7975d07 5323 {
156bdb41 5324 /* Grow buffer to add directory separator and '\0'. */
38182d90 5325 pwd = realloc (pwd, len + 2);
8b146312
AS
5326 if (!pwd)
5327 fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
b639c9be
RF
5328 pwd[len] = DIRECTORY_SEP;
5329 pwd[len + 1] = '\0';
cb1caeaf 5330 len++;
f7975d07 5331 }
0995fa35 5332
39eb03f1 5333 bset_directory (current_buffer, make_unibyte_string (pwd, len));
4b4deea2 5334 if (! NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
f9962371 5335 /* At this moment, we still don't know how to decode the
156bdb41 5336 directory name. So, we keep the bytes in multibyte form so
dcd74c5f 5337 that ENCODE_FILE correctly gets the original bytes. */
39eb03f1
PE
5338 bset_directory
5339 (current_buffer, string_to_multibyte (BVAR (current_buffer, directory)));
136351b7 5340
0995fa35
RS
5341 /* Add /: to the front of the name
5342 if it would otherwise be treated as magic. */
4b4deea2 5343 temp = Ffind_file_name_handler (BVAR (current_buffer, directory), Qt);
81ab2e07
KH
5344 if (! NILP (temp)
5345 /* If the default dir is just /, TEMP is non-nil
5346 because of the ange-ftp completion handler.
5347 However, it is not necessary to turn / into /:/.
5348 So avoid doing that. */
4b4deea2 5349 && strcmp ("/", SSDATA (BVAR (current_buffer, directory))))
39eb03f1
PE
5350 bset_directory
5351 (current_buffer,
5352 concat2 (build_string ("/:"), BVAR (current_buffer, directory)));
0995fa35 5353
136351b7 5354 temp = get_minibuffer (0);
39eb03f1 5355 bset_directory (XBUFFER (temp), BVAR (current_buffer, directory));
01537133
EZ
5356
5357 free (pwd);
1ab256cb
RM
5358}
5359
d6aa1876
SM
5360/* Similar to defvar_lisp but define a variable whose value is the Lisp
5361 Object stored in the current buffer. address is the address of the slot
5362 in the buffer that is current now. */
5363
5364/* TYPE is nil for a general Lisp variable.
ce5b453a 5365 An integer specifies a type; then only Lisp values
d6aa1876 5366 with that type code are allowed (except that nil is allowed too).
ce5b453a 5367 LNAME is the Lisp-level variable name.
d6aa1876
SM
5368 VNAME is the name of the buffer slot.
5369 DOC is a dummy where you write the doc string as a comment. */
ce5b453a
SM
5370#define DEFVAR_PER_BUFFER(lname, vname, type, doc) \
5371 do { \
5372 static struct Lisp_Buffer_Objfwd bo_fwd; \
6b1f9ba4 5373 defvar_per_buffer (&bo_fwd, lname, vname, type); \
ce5b453a 5374 } while (0)
d6aa1876
SM
5375
5376static void
8ea90aa3 5377defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
6b1f9ba4 5378 Lisp_Object *address, Lisp_Object type)
d6aa1876 5379{
ce5b453a 5380 struct Lisp_Symbol *sym;
d6aa1876
SM
5381 int offset;
5382
ce5b453a 5383 sym = XSYMBOL (intern (namestring));
d6aa1876
SM
5384 offset = (char *)address - (char *)current_buffer;
5385
ce5b453a
SM
5386 bo_fwd->type = Lisp_Fwd_Buffer_Obj;
5387 bo_fwd->offset = offset;
5388 bo_fwd->slottype = type;
b9598260 5389 sym->declared_special = 1;
ce5b453a
SM
5390 sym->redirect = SYMBOL_FORWARDED;
5391 {
5392 /* I tried to do the job without a cast, but it seems impossible.
5393 union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */
5394 SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd);
5395 }
5396 XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
d6aa1876
SM
5397
5398 if (PER_BUFFER_IDX (offset) == 0)
5399 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
5400 slot of buffer_local_flags */
5401 abort ();
5402}
5403
5404
1ab256cb 5405/* initialize the buffer routines */
dfcf069d 5406void
d3da34e0 5407syms_of_buffer (void)
1ab256cb 5408{
9115729e
KH
5409 staticpro (&last_overlay_modification_hooks);
5410 last_overlay_modification_hooks
5411 = Fmake_vector (make_number (10), Qnil);
5412
1ab256cb
RM
5413 staticpro (&Vbuffer_defaults);
5414 staticpro (&Vbuffer_local_symbols);
5415 staticpro (&Qfundamental_mode);
5416 staticpro (&Qmode_class);
5417 staticpro (&QSFundamental);
5418 staticpro (&Vbuffer_alist);
5419 staticpro (&Qprotected_field);
5420 staticpro (&Qpermanent_local);
5421 staticpro (&Qkill_buffer_hook);
cd3520a4
JB
5422
5423 DEFSYM (Qpermanent_local_hook, "permanent-local-hook");
5424 DEFSYM (Qoverlayp, "overlayp");
5425 DEFSYM (Qevaporate, "evaporate");
5426 DEFSYM (Qmodification_hooks, "modification-hooks");
5427 DEFSYM (Qinsert_in_front_hooks, "insert-in-front-hooks");
5428 DEFSYM (Qinsert_behind_hooks, "insert-behind-hooks");
5429 DEFSYM (Qget_file_buffer, "get-file-buffer");
5430 DEFSYM (Qpriority, "priority");
5431 DEFSYM (Qbefore_string, "before-string");
5432 DEFSYM (Qafter_string, "after-string");
5433 DEFSYM (Qfirst_change_hook, "first-change-hook");
5434 DEFSYM (Qbefore_change_functions, "before-change-functions");
5435 DEFSYM (Qafter_change_functions, "after-change-functions");
5436 DEFSYM (Qkill_buffer_query_functions, "kill-buffer-query-functions");
5437
1ab256cb 5438 Fput (Qprotected_field, Qerror_conditions,
3438fe21 5439 listn (CONSTYPE_PURE, 2, Qprotected_field, Qerror));
1ab256cb 5440 Fput (Qprotected_field, Qerror_message,
2a0213a6 5441 build_pure_c_string ("Attempt to modify a protected field"));
1ab256cb 5442
422745d0
TT
5443 DEFVAR_BUFFER_DEFAULTS ("default-mode-line-format",
5444 mode_line_format,
5445 doc: /* Default value of `mode-line-format' for buffers that don't override it.
018ba359 5446This is the same as (default-value 'mode-line-format). */);
1ab256cb 5447
422745d0
TT
5448 DEFVAR_BUFFER_DEFAULTS ("default-header-line-format",
5449 header_line_format,
5450 doc: /* Default value of `header-line-format' for buffers that don't override it.
018ba359 5451This is the same as (default-value 'header-line-format). */);
0552666b 5452
422745d0
TT
5453 DEFVAR_BUFFER_DEFAULTS ("default-cursor-type", cursor_type,
5454 doc: /* Default value of `cursor-type' for buffers that don't override it.
018ba359 5455This is the same as (default-value 'cursor-type). */);
bd96bd79 5456
422745d0
TT
5457 DEFVAR_BUFFER_DEFAULTS ("default-line-spacing",
5458 extra_line_spacing,
5459 doc: /* Default value of `line-spacing' for buffers that don't override it.
018ba359 5460This is the same as (default-value 'line-spacing). */);
a3bbced0 5461
422745d0
TT
5462 DEFVAR_BUFFER_DEFAULTS ("default-cursor-in-non-selected-windows",
5463 cursor_in_non_selected_windows,
5464 doc: /* Default value of `cursor-in-non-selected-windows'.
187ccf49
KS
5465This is the same as (default-value 'cursor-in-non-selected-windows). */);
5466
422745d0
TT
5467 DEFVAR_BUFFER_DEFAULTS ("default-abbrev-mode",
5468 abbrev_mode,
5469 doc: /* Default value of `abbrev-mode' for buffers that do not override it.
018ba359 5470This is the same as (default-value 'abbrev-mode). */);
1ab256cb 5471
422745d0
TT
5472 DEFVAR_BUFFER_DEFAULTS ("default-ctl-arrow",
5473 ctl_arrow,
5474 doc: /* Default value of `ctl-arrow' for buffers that do not override it.
018ba359 5475This is the same as (default-value 'ctl-arrow). */);
1ab256cb 5476
422745d0
TT
5477 DEFVAR_BUFFER_DEFAULTS ("default-enable-multibyte-characters",
5478 enable_multibyte_characters,
fb7ada5f 5479 doc: /* Default value of `enable-multibyte-characters' for buffers not overriding it.
018ba359 5480This is the same as (default-value 'enable-multibyte-characters). */);
177c0ea7 5481
422745d0
TT
5482 DEFVAR_BUFFER_DEFAULTS ("default-buffer-file-coding-system",
5483 buffer_file_coding_system,
5484 doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it.
018ba359 5485This is the same as (default-value 'buffer-file-coding-system). */);
177c0ea7 5486
422745d0
TT
5487 DEFVAR_BUFFER_DEFAULTS ("default-truncate-lines",
5488 truncate_lines,
5489 doc: /* Default value of `truncate-lines' for buffers that do not override it.
018ba359 5490This is the same as (default-value 'truncate-lines). */);
1ab256cb 5491
422745d0
TT
5492 DEFVAR_BUFFER_DEFAULTS ("default-fill-column",
5493 fill_column,
5494 doc: /* Default value of `fill-column' for buffers that do not override it.
018ba359 5495This is the same as (default-value 'fill-column). */);
1ab256cb 5496
422745d0
TT
5497 DEFVAR_BUFFER_DEFAULTS ("default-left-margin",
5498 left_margin,
5499 doc: /* Default value of `left-margin' for buffers that do not override it.
018ba359 5500This is the same as (default-value 'left-margin). */);
1ab256cb 5501
422745d0
TT
5502 DEFVAR_BUFFER_DEFAULTS ("default-tab-width",
5503 tab_width,
5504 doc: /* Default value of `tab-width' for buffers that do not override it.
018ba359 5505This is the same as (default-value 'tab-width). */);
1ab256cb 5506
422745d0
TT
5507 DEFVAR_BUFFER_DEFAULTS ("default-case-fold-search",
5508 case_fold_search,
5509 doc: /* Default value of `case-fold-search' for buffers that don't override it.
018ba359 5510This is the same as (default-value 'case-fold-search). */);
1ab256cb 5511
422745d0
TT
5512 DEFVAR_BUFFER_DEFAULTS ("default-left-margin-width",
5513 left_margin_cols,
5514 doc: /* Default value of `left-margin-width' for buffers that don't override it.
018ba359 5515This is the same as (default-value 'left-margin-width). */);
0552666b 5516
422745d0
TT
5517 DEFVAR_BUFFER_DEFAULTS ("default-right-margin-width",
5518 right_margin_cols,
5519 doc: /* Default value of `right-margin-width' for buffers that don't override it.
018ba359 5520This is the same as (default-value 'right-margin-width). */);
177c0ea7 5521
422745d0
TT
5522 DEFVAR_BUFFER_DEFAULTS ("default-left-fringe-width",
5523 left_fringe_width,
5524 doc: /* Default value of `left-fringe-width' for buffers that don't override it.
2ad8731a
KS
5525This is the same as (default-value 'left-fringe-width). */);
5526
422745d0
TT
5527 DEFVAR_BUFFER_DEFAULTS ("default-right-fringe-width",
5528 right_fringe_width,
5529 doc: /* Default value of `right-fringe-width' for buffers that don't override it.
2ad8731a
KS
5530This is the same as (default-value 'right-fringe-width). */);
5531
422745d0
TT
5532 DEFVAR_BUFFER_DEFAULTS ("default-fringes-outside-margins",
5533 fringes_outside_margins,
5534 doc: /* Default value of `fringes-outside-margins' for buffers that don't override it.
2ad8731a
KS
5535This is the same as (default-value 'fringes-outside-margins). */);
5536
422745d0
TT
5537 DEFVAR_BUFFER_DEFAULTS ("default-scroll-bar-width",
5538 scroll_bar_width,
5539 doc: /* Default value of `scroll-bar-width' for buffers that don't override it.
2ad8731a
KS
5540This is the same as (default-value 'scroll-bar-width). */);
5541
422745d0
TT
5542 DEFVAR_BUFFER_DEFAULTS ("default-vertical-scroll-bar",
5543 vertical_scroll_bar_type,
5544 doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it.
2ad8731a
KS
5545This is the same as (default-value 'vertical-scroll-bar). */);
5546
422745d0
TT
5547 DEFVAR_BUFFER_DEFAULTS ("default-indicate-empty-lines",
5548 indicate_empty_lines,
5549 doc: /* Default value of `indicate-empty-lines' for buffers that don't override it.
018ba359 5550This is the same as (default-value 'indicate-empty-lines). */);
177c0ea7 5551
422745d0
TT
5552 DEFVAR_BUFFER_DEFAULTS ("default-indicate-buffer-boundaries",
5553 indicate_buffer_boundaries,
5554 doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it.
6b61353c
KH
5555This is the same as (default-value 'indicate-buffer-boundaries). */);
5556
422745d0
TT
5557 DEFVAR_BUFFER_DEFAULTS ("default-fringe-indicator-alist",
5558 fringe_indicator_alist,
5559 doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it.
c6a46372
KS
5560This is the same as (default-value 'fringe-indicator-alist'). */);
5561
422745d0
TT
5562 DEFVAR_BUFFER_DEFAULTS ("default-fringe-cursor-alist",
5563 fringe_cursor_alist,
5564 doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it.
c6a46372
KS
5565This is the same as (default-value 'fringe-cursor-alist'). */);
5566
422745d0
TT
5567 DEFVAR_BUFFER_DEFAULTS ("default-scroll-up-aggressively",
5568 scroll_up_aggressively,
5569 doc: /* Default value of `scroll-up-aggressively'.
7614d762 5570This value applies in buffers that don't have their own local values.
fc961256 5571This is the same as (default-value 'scroll-up-aggressively). */);
177c0ea7 5572
422745d0
TT
5573 DEFVAR_BUFFER_DEFAULTS ("default-scroll-down-aggressively",
5574 scroll_down_aggressively,
5575 doc: /* Default value of `scroll-down-aggressively'.
7614d762 5576This value applies in buffers that don't have their own local values.
fc961256 5577This is the same as (default-value 'scroll-down-aggressively). */);
177c0ea7 5578
045dee35 5579 DEFVAR_PER_BUFFER ("header-line-format",
4b4deea2 5580 &BVAR (current_buffer, header_line_format),
7ee72033 5581 Qnil,
7614d762
RS
5582 doc: /* Analogous to `mode-line-format', but controls the header line.
5583The header line appears, optionally, at the top of a window;
5584the mode line appears at the bottom. */);
177c0ea7 5585
4b4deea2 5586 DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format),
efc7e75f
PJ
5587 Qnil,
5588 doc: /* Template for displaying mode line for current buffer.
5f2c76c6
CY
5589
5590The value may be nil, a string, a symbol or a list.
5591
018ba359 5592A value of nil means don't display a mode line.
5f2c76c6
CY
5593
5594For any symbol other than t or nil, the symbol's value is processed as
5595 a mode line construct. As a special exception, if that value is a
5596 string, the string is processed verbatim, without handling any
5597 %-constructs (see below). Also, unless the symbol has a non-nil
5598 `risky-local-variable' property, all properties in any strings, as
5599 well as all :eval and :propertize forms in the value, are ignored.
5600
5601A list whose car is a string or list is processed by processing each
5602 of the list elements recursively, as separate mode line constructs,
5603 and concatenating the results.
5604
5605A list of the form `(:eval FORM)' is processed by evaluating FORM and
5606 using the result as a mode line construct. Be careful--FORM should
5607 not load any files, because that can cause an infinite recursion.
5608
5609A list of the form `(:propertize ELT PROPS...)' is processed by
5610 processing ELT as the mode line construct, and adding the text
5611 properties PROPS to the result.
5612
5613A list whose car is a symbol is processed by examining the symbol's
5614 value, and, if that value is non-nil, processing the cadr of the list
5615 recursively; and if that value is nil, processing the caddr of the
5616 list recursively.
5617
5618A list whose car is an integer is processed by processing the cadr of
5619 the list, and padding (if the number is positive) or truncating (if
5620 negative) to the width specified by that number.
5621
018ba359 5622A string is printed verbatim in the mode line except for %-constructs:
018ba359
PJ
5623 %b -- print buffer name. %f -- print visited file name.
5624 %F -- print frame name.
5625 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.
5626 %& is like %*, but ignore read-only-ness.
5627 % means buffer is read-only and * means it is modified.
5628 For a modified read-only buffer, %* gives % and %+ gives *.
5629 %s -- print process status. %l -- print the current line number.
5630 %c -- print the current column number (this makes editing slower).
5631 To make the column number update correctly in all cases,
5632 `column-number-mode' must be non-nil.
6b61353c
KH
5633 %i -- print the size of the buffer.
5634 %I -- like %i, but use k, M, G, etc., to abbreviate.
018ba359
PJ
5635 %p -- print percent of buffer above top of window, or Top, Bot or All.
5636 %P -- print percent of buffer above bottom of window, perhaps plus Top,
5637 or print Bottom or All.
018ba359 5638 %n -- print Narrow if appropriate.
dafbe726 5639 %t -- visited file is text or binary (if OS supports this distinction).
47419860 5640 %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
018ba359 5641 %Z -- like %z, but including the end-of-line format.
dafbe726 5642 %e -- print error message about full memory.
f7165034
NR
5643 %@ -- print @ or hyphen. @ means that default-directory is on a
5644 remote machine.
018ba359
PJ
5645 %[ -- print one [ for each recursive editing level. %] similar.
5646 %% -- print %. %- -- print infinitely many dashes.
5647Decimal digits after the % specify field width to which to pad. */);
5648
422745d0 5649 DEFVAR_BUFFER_DEFAULTS ("default-major-mode", major_mode,
fb7ada5f 5650 doc: /* Value of `major-mode' for new buffers. */);
1ab256cb 5651
4b4deea2 5652 DEFVAR_PER_BUFFER ("major-mode", &BVAR (current_buffer, major_mode),
7ee72033 5653 make_number (Lisp_Symbol),
5a021dd0
GM
5654 doc: /* Symbol for current buffer's major mode.
5655The default value (normally `fundamental-mode') affects new buffers.
5656A value of nil means to use the current buffer's major mode, provided
5657it is not marked as "special".
5658
5659When a mode is used by default, `find-file' switches to it before it
5660reads the contents into the buffer and before it finishes setting up
5661the buffer. Thus, the mode and its hooks should not expect certain
5662variables such as `buffer-read-only' and `buffer-file-coding-system'
5663to be set up. */);
1ab256cb 5664
4b4deea2 5665 DEFVAR_PER_BUFFER ("mode-name", &BVAR (current_buffer, mode_name),
c01d0677 5666 Qnil,
64a7c220 5667 doc: /* Pretty name of current buffer's major mode.
7cb70974
EZ
5668Usually a string, but can use any of the constructs for `mode-line-format',
5669which see.
5670Format with `format-mode-line' to produce a string value. */);
1ab256cb 5671
4b4deea2 5672 DEFVAR_PER_BUFFER ("local-abbrev-table", &BVAR (current_buffer, abbrev_table), Qnil,
d6aa1876
SM
5673 doc: /* Local (mode-specific) abbrev table of current buffer. */);
5674
4b4deea2 5675 DEFVAR_PER_BUFFER ("abbrev-mode", &BVAR (current_buffer, abbrev_mode), Qnil,
9d794026
GM
5676 doc: /* Non-nil if Abbrev mode is enabled.
5677Use the command `abbrev-mode' to change this variable. */);
1ab256cb 5678
4b4deea2 5679 DEFVAR_PER_BUFFER ("case-fold-search", &BVAR (current_buffer, case_fold_search),
7ee72033 5680 Qnil,
fb7ada5f 5681 doc: /* Non-nil if searches and matches should ignore case. */);
1ab256cb 5682
4b4deea2 5683 DEFVAR_PER_BUFFER ("fill-column", &BVAR (current_buffer, fill_column),
d5040d2d 5684 make_number (Lisp_Int0),
fb7ada5f 5685 doc: /* Column beyond which automatic line-wrapping should happen.
f1ccb329 5686Interactively, you can set the buffer local value using \\[set-fill-column]. */);
1ab256cb 5687
4b4deea2 5688 DEFVAR_PER_BUFFER ("left-margin", &BVAR (current_buffer, left_margin),
d5040d2d 5689 make_number (Lisp_Int0),
fb7ada5f 5690 doc: /* Column for the default `indent-line-function' to indent to.
018ba359 5691Linefeed indents to this column in Fundamental mode. */);
1ab256cb 5692
4b4deea2 5693 DEFVAR_PER_BUFFER ("tab-width", &BVAR (current_buffer, tab_width),
d5040d2d 5694 make_number (Lisp_Int0),
fb7ada5f 5695 doc: /* Distance between tab stops (for display of tab characters), in columns.
fde4eb86 5696This should be an integer greater than zero. */);
1ab256cb 5697
4b4deea2 5698 DEFVAR_PER_BUFFER ("ctl-arrow", &BVAR (current_buffer, ctl_arrow), Qnil,
fb7ada5f 5699 doc: /* Non-nil means display control chars with uparrow.
018ba359
PJ
5700A value of nil means use backslash and octal digits.
5701This variable does not apply to characters whose display is specified
5702in the current display table (if there is one). */);
1ab256cb 5703
3b06f880 5704 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
4b4deea2 5705 &BVAR (current_buffer, enable_multibyte_characters),
a9b9a780 5706 Qnil,
efc7e75f 5707 doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
018ba359
PJ
5708Otherwise they are regarded as unibyte. This affects the display,
5709file I/O and the behavior of various editing commands.
5710
5711This variable is buffer-local but you cannot set it directly;
5712use the function `set-buffer-multibyte' to change a buffer's representation.
a66cfb1c 5713See also Info node `(elisp)Text Representations'. */);
d67b4f80 5714 XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
3b06f880 5715
c71b5d9b 5716 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
4b4deea2 5717 &BVAR (current_buffer, buffer_file_coding_system), Qnil,
efc7e75f 5718 doc: /* Coding system to be used for encoding the buffer contents on saving.
018ba359
PJ
5719This variable applies to saving the buffer, and also to `write-region'
5720and other functions that use `write-region'.
5721It does not apply to sending output to subprocesses, however.
5722
5723If this is nil, the buffer is saved without any code conversion
5724unless some coding system is specified in `file-coding-system-alist'
5725for the buffer file.
5726
31a6cb06
EZ
5727If the text to be saved cannot be encoded as specified by this variable,
5728an alternative encoding is selected by `select-safe-coding-system', which see.
5729
018ba359
PJ
5730The variable `coding-system-for-write', if non-nil, overrides this variable.
5731
5732This variable is never applied to a way of decoding a file while reading it. */);
c71b5d9b 5733
f44e260c 5734 DEFVAR_PER_BUFFER ("bidi-display-reordering",
4b4deea2 5735 &BVAR (current_buffer, bidi_display_reordering), Qnil,
938efb77 5736 doc: /* Non-nil means reorder bidirectional text for display in the visual order. */);
3b06f880 5737
6c0cf218 5738 DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
4b4deea2 5739 &BVAR (current_buffer, bidi_paragraph_direction), Qnil,
fb7ada5f 5740 doc: /* If non-nil, forces directionality of text paragraphs in the buffer.
b4bf28b7 5741
b44d9321
EZ
5742If this is nil (the default), the direction of each paragraph is
5743determined by the first strong directional character of its text.
5744The values of `right-to-left' and `left-to-right' override that.
5745Any other value is treated as nil.
b4bf28b7 5746
b44d9321
EZ
5747This variable has no effect unless the buffer's value of
5748\`bidi-display-reordering' is non-nil. */);
5749
4b4deea2 5750 DEFVAR_PER_BUFFER ("truncate-lines", &BVAR (current_buffer, truncate_lines), Qnil,
fb7ada5f 5751 doc: /* Non-nil means do not display continuation lines.
7614d762 5752Instead, give each line of text just one screen line.
018ba359
PJ
5753
5754Note that this is overridden by the variable
5755`truncate-partial-width-windows' if that variable is non-nil
32bbb17c
GM
5756and this buffer is not full-frame width.
5757
5758Minibuffers set this variable to nil. */);
1ab256cb 5759
4b4deea2 5760 DEFVAR_PER_BUFFER ("word-wrap", &BVAR (current_buffer, word_wrap), Qnil,
fb7ada5f 5761 doc: /* Non-nil means to use word-wrapping for continuation lines.
0858cd02
CY
5762When word-wrapping is on, continuation lines are wrapped at the space
5763or tab character nearest to the right window edge.
5764If nil, continuation lines are wrapped at the right screen edge.
5765
5766This variable has no effect if long lines are truncated (see
eb577e27
GM
5767`truncate-lines' and `truncate-partial-width-windows'). If you use
5768word-wrapping, you might want to reduce the value of
5769`truncate-partial-width-windows', since wrapping can make text readable
c5cfcbe0
CY
5770in narrower windows.
5771
5772Instead of setting this variable directly, most users should use
5773Visual Line mode . Visual Line mode, when enabled, sets `word-wrap'
5774to t, and additionally redefines simple editing commands to act on
5775visual lines rather than logical lines. See the documentation of
5776`visual-line-mode'. */);
0858cd02 5777
4b4deea2 5778 DEFVAR_PER_BUFFER ("default-directory", &BVAR (current_buffer, directory),
7ee72033 5779 make_number (Lisp_String),
efc7e75f 5780 doc: /* Name of default directory of current buffer. Should end with slash.
018ba359 5781To interactively change the default directory, use command `cd'. */);
1ab256cb 5782
4b4deea2 5783 DEFVAR_PER_BUFFER ("auto-fill-function", &BVAR (current_buffer, auto_fill_function),
7ee72033 5784 Qnil,
efc7e75f 5785 doc: /* Function called (if non-nil) to perform auto-fill.
018ba359
PJ
5786It is called after self-inserting any character specified in
5787the `auto-fill-chars' table.
5788NOTE: This variable is not a hook;
5789its value may not be a list of functions. */);
1ab256cb 5790
4b4deea2 5791 DEFVAR_PER_BUFFER ("buffer-file-name", &BVAR (current_buffer, filename),
7ee72033 5792 make_number (Lisp_String),
efc7e75f 5793 doc: /* Name of file visited in current buffer, or nil if not visiting a file. */);
1ab256cb 5794
4b4deea2 5795 DEFVAR_PER_BUFFER ("buffer-file-truename", &BVAR (current_buffer, file_truename),
7ee72033 5796 make_number (Lisp_String),
efc7e75f 5797 doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
018ba359
PJ
5798The truename of a file is calculated by `file-truename'
5799and then abbreviated with `abbreviate-file-name'. */);
f6ed2e84 5800
1ab256cb 5801 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
4b4deea2 5802 &BVAR (current_buffer, auto_save_file_name),
7ee72033 5803 make_number (Lisp_String),
7614d762
RS
5804 doc: /* Name of file for auto-saving current buffer.
5805If it is nil, that means don't auto-save this buffer. */);
1ab256cb 5806
4b4deea2 5807 DEFVAR_PER_BUFFER ("buffer-read-only", &BVAR (current_buffer, read_only), Qnil,
efc7e75f 5808 doc: /* Non-nil if this buffer is read-only. */);
1ab256cb 5809
4b4deea2 5810 DEFVAR_PER_BUFFER ("buffer-backed-up", &BVAR (current_buffer, backed_up), Qnil,
efc7e75f 5811 doc: /* Non-nil if this buffer's file has been backed up.
018ba359 5812Backing up is done before the first time the file is saved. */);
1ab256cb 5813
4b4deea2 5814 DEFVAR_PER_BUFFER ("buffer-saved-size", &BVAR (current_buffer, save_length),
d5040d2d 5815 make_number (Lisp_Int0),
efc7e75f 5816 doc: /* Length of current buffer when last read in, saved or auto-saved.
4be941e3
RS
58170 initially.
5818-1 means auto-saving turned off until next real save.
5819
5820If you set this to -2, that means don't turn off auto-saving in this buffer
5821if its text size shrinks. If you use `buffer-swap-text' on a buffer,
5822you probably should set this to -2 in that buffer. */);
1ab256cb 5823
4b4deea2 5824 DEFVAR_PER_BUFFER ("selective-display", &BVAR (current_buffer, selective_display),
7ee72033 5825 Qnil,
7614d762 5826 doc: /* Non-nil enables selective display.
a66f285a
JB
5827An integer N as value means display only lines
5828that start with less than N columns of space.
7614d762
RS
5829A value of t means that the character ^M makes itself and
5830all the rest of the line invisible; also, when saving the buffer
5831in a file, save the ^M as a newline. */);
1ab256cb 5832
1ab256cb 5833 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4b4deea2 5834 &BVAR (current_buffer, selective_display_ellipses),
7ee72033 5835 Qnil,
3f676284 5836 doc: /* Non-nil means display ... on previous line when a line is invisible. */);
1ab256cb 5837
4b4deea2 5838 DEFVAR_PER_BUFFER ("overwrite-mode", &BVAR (current_buffer, overwrite_mode), Qnil,
efc7e75f 5839 doc: /* Non-nil if self-insertion should replace existing text.
018ba359
PJ
5840The value should be one of `overwrite-mode-textual',
5841`overwrite-mode-binary', or nil.
5842If it is `overwrite-mode-textual', self-insertion still
5843inserts at the end of a line, and inserts when point is before a tab,
5844until the tab is filled in.
5845If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */);
5846
4b4deea2 5847 DEFVAR_PER_BUFFER ("buffer-display-table", &BVAR (current_buffer, display_table),
7ee72033 5848 Qnil,
efc7e75f 5849 doc: /* Display table that controls display of the contents of current buffer.
018ba359
PJ
5850
5851If this variable is nil, the value of `standard-display-table' is used.
5852Each window can have its own, overriding display table, see
5853`set-window-display-table' and `window-display-table'.
5854
5855The display table is a char-table created with `make-display-table'.
5856A char-table is an array indexed by character codes. Normal array
5857primitives `aref' and `aset' can be used to access elements of a char-table.
5858
5859Each of the char-table elements control how to display the corresponding
5860text character: the element at index C in the table says how to display
5861the character whose code is C. Each element should be a vector of
426a9163
JB
5862characters or nil. The value nil means display the character in the
5863default fashion; otherwise, the characters from the vector are delivered
5864to the screen instead of the original character.
018ba359 5865
5fd11dc8 5866For example, (aset buffer-display-table ?X [?Y]) tells Emacs
adbb3b05 5867to display a capital Y instead of each X character.
018ba359
PJ
5868
5869In addition, a char-table has six extra slots to control the display of:
5870
5871 the end of a truncated screen line (extra-slot 0, a single character);
5872 the end of a continued line (extra-slot 1, a single character);
5873 the escape character used to display character codes in octal
5874 (extra-slot 2, a single character);
5875 the character used as an arrow for control characters (extra-slot 3,
5876 a single character);
5877 the decoration indicating the presence of invisible lines (extra-slot 4,
5878 a vector of characters);
5879 the character used to draw the border between side-by-side windows
5880 (extra-slot 5, a single character).
5881
5882See also the functions `display-table-slot' and `set-display-table-slot'. */);
1ab256cb 5883
4b4deea2 5884 DEFVAR_PER_BUFFER ("left-margin-width", &BVAR (current_buffer, left_margin_cols),
7ee72033 5885 Qnil,
fb7ada5f 5886 doc: /* Width of left marginal area for display of a buffer.
018ba359 5887A value of nil means no marginal area. */);
177c0ea7 5888
4b4deea2 5889 DEFVAR_PER_BUFFER ("right-margin-width", &BVAR (current_buffer, right_margin_cols),
7ee72033 5890 Qnil,
fb7ada5f 5891 doc: /* Width of right marginal area for display of a buffer.
018ba359 5892A value of nil means no marginal area. */);
177c0ea7 5893
4b4deea2 5894 DEFVAR_PER_BUFFER ("left-fringe-width", &BVAR (current_buffer, left_fringe_width),
2ad8731a 5895 Qnil,
fb7ada5f 5896 doc: /* Width of this buffer's left fringe (in pixels).
2ad8731a
KS
5897A value of 0 means no left fringe is shown in this buffer's window.
5898A value of nil means to use the left fringe width from the window's frame. */);
5899
4b4deea2 5900 DEFVAR_PER_BUFFER ("right-fringe-width", &BVAR (current_buffer, right_fringe_width),
2ad8731a 5901 Qnil,
fb7ada5f 5902 doc: /* Width of this buffer's right fringe (in pixels).
2ad8731a
KS
5903A value of 0 means no right fringe is shown in this buffer's window.
5904A value of nil means to use the right fringe width from the window's frame. */);
5905
4b4deea2 5906 DEFVAR_PER_BUFFER ("fringes-outside-margins", &BVAR (current_buffer, fringes_outside_margins),
2ad8731a 5907 Qnil,
fb7ada5f 5908 doc: /* Non-nil means to display fringes outside display margins.
2ad8731a
KS
5909A value of nil means to display fringes between margins and buffer text. */);
5910
4b4deea2 5911 DEFVAR_PER_BUFFER ("scroll-bar-width", &BVAR (current_buffer, scroll_bar_width),
2ad8731a 5912 Qnil,
fb7ada5f 5913 doc: /* Width of this buffer's scroll bars in pixels.
2ad8731a
KS
5914A value of nil means to use the scroll bar width from the window's frame. */);
5915
4b4deea2 5916 DEFVAR_PER_BUFFER ("vertical-scroll-bar", &BVAR (current_buffer, vertical_scroll_bar_type),
2ad8731a 5917 Qnil,
fb7ada5f 5918 doc: /* Position of this buffer's vertical scroll bar.
7c6b2007 5919The value takes effect whenever you tell a window to display this buffer;
188577ce 5920for instance, with `set-window-buffer' or when `display-buffer' displays it.
7c6b2007 5921
fc2c8887
RS
5922A value of `left' or `right' means put the vertical scroll bar at that side
5923of the window; a value of nil means don't show any vertical scroll bars.
5924A value of t (the default) means do whatever the window's frame specifies. */);
2ad8731a 5925
0552666b 5926 DEFVAR_PER_BUFFER ("indicate-empty-lines",
4b4deea2 5927 &BVAR (current_buffer, indicate_empty_lines), Qnil,
fb7ada5f 5928 doc: /* Visually indicate empty lines after the buffer end.
018ba359
PJ
5929If non-nil, a bitmap is displayed in the left fringe of a window on
5930window-systems. */);
177c0ea7 5931
6b61353c 5932 DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
4b4deea2 5933 &BVAR (current_buffer, indicate_buffer_boundaries), Qnil,
fb7ada5f 5934 doc: /* Visually indicate buffer boundaries and scrolling.
6b61353c
KH
5935If non-nil, the first and last line of the buffer are marked in the fringe
5936of a window on window-systems with angle bitmaps, or if the window can be
5937scrolled, the top and bottom line of the window are marked with up and down
5938arrow bitmaps.
b2229037
KS
5939
5940If value is a symbol `left' or `right', both angle and arrow bitmaps
79e3497d 5941are displayed in the left or right fringe, resp. Any other value
845a78b4 5942that doesn't look like an alist means display the angle bitmaps in
79e3497d 5943the left fringe but no arrows.
b2229037 5944
79e3497d
RS
5945You can exercise more precise control by using an alist as the
5946value. Each alist element (INDICATOR . POSITION) specifies
5947where to show one of the indicators. INDICATOR is one of `top',
b2229037
KS
5948`bottom', `up', `down', or t, which specifies the default position,
5949and POSITION is one of `left', `right', or nil, meaning do not show
5950this indicator.
5951
5952For example, ((top . left) (t . right)) places the top angle bitmap in
5953left fringe, the bottom angle bitmap in right fringe, and both arrow
6b61353c 5954bitmaps in right fringe. To show just the angle bitmaps in the left
b2229037 5955fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */);
6b61353c 5956
c6a46372 5957 DEFVAR_PER_BUFFER ("fringe-indicator-alist",
4b4deea2 5958 &BVAR (current_buffer, fringe_indicator_alist), Qnil,
fb7ada5f 5959 doc: /* Mapping from logical to physical fringe indicator bitmaps.
c6a46372
KS
5960The value is an alist where each element (INDICATOR . BITMAPS)
5961specifies the fringe bitmaps used to display a specific logical
5962fringe indicator.
5963
5964INDICATOR specifies the logical indicator type which is one of the
5965following symbols: `truncation' , `continuation', `overlay-arrow',
14fb5704 5966`top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
c6a46372 5967
14fb5704 5968BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
c6a46372
KS
5969the actual bitmap shown in the left or right fringe for the logical
5970indicator. LEFT and RIGHT are the bitmaps shown in the left and/or
5971right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps
14fb5704
JB
5972are used only for the `bottom' and `top-bottom' indicators when the
5973last (only) line has no final newline. BITMAPS may also be a single
c6a46372
KS
5974symbol which is used in both left and right fringes. */);
5975
5976 DEFVAR_PER_BUFFER ("fringe-cursor-alist",
4b4deea2 5977 &BVAR (current_buffer, fringe_cursor_alist), Qnil,
fb7ada5f 5978 doc: /* Mapping from logical to physical fringe cursor bitmaps.
c6a46372
KS
5979The value is an alist where each element (CURSOR . BITMAP)
5980specifies the fringe bitmaps used to display a specific logical
5981cursor type in the fringe.
5982
5983CURSOR specifies the logical cursor type which is one of the following
5984symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last
5985one is used to show a hollow cursor on narrow lines display lines
5986where the normal hollow cursor will not fit.
5987
5988BITMAP is the corresponding fringe bitmap shown for the logical
5989cursor type. */);
5990
0552666b 5991 DEFVAR_PER_BUFFER ("scroll-up-aggressively",
4b4deea2 5992 &BVAR (current_buffer, scroll_up_aggressively), Qnil,
4e0692c1
RS
5993 doc: /* How far to scroll windows upward.
5994If you move point off the bottom, the window scrolls automatically.
426a9163 5995This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
5996means scroll to center point. A fraction means scroll to put point
5997that fraction of the window's height from the bottom of the window.
d765e3a3
JB
5998When the value is 0.0, point goes at the bottom line, which in the
5999simple case that you moved off with C-f means scrolling just one line.
60001.0 means point goes at the top, so that in that simple case, the
6001window scrolls by a full window height. Meaningful values are
175e9712 6002between 0.0 and 1.0, inclusive. */);
177c0ea7 6003
0552666b 6004 DEFVAR_PER_BUFFER ("scroll-down-aggressively",
4b4deea2 6005 &BVAR (current_buffer, scroll_down_aggressively), Qnil,
4e0692c1
RS
6006 doc: /* How far to scroll windows downward.
6007If you move point off the top, the window scrolls automatically.
426a9163 6008This variable controls how far it scrolls. The value nil, the default,
4e0692c1
RS
6009means scroll to center point. A fraction means scroll to put point
6010that fraction of the window's height from the top of the window.
d765e3a3
JB
6011When the value is 0.0, point goes at the top line, which in the
6012simple case that you moved off with C-b means scrolling just one line.
60131.0 means point goes at the bottom, so that in that simple case, the
6014window scrolls by a full window height. Meaningful values are
175e9712 6015between 0.0 and 1.0, inclusive. */);
177c0ea7 6016
1ab256cb
RM
6017/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
6018 "Don't ask.");
6019*/
1ab256cb 6020
29208e82 6021 DEFVAR_LISP ("before-change-functions", Vbefore_change_functions,
7ee72033 6022 doc: /* List of functions to call before each text change.
018ba359
PJ
6023Two arguments are passed to each function: the positions of
6024the beginning and end of the range of old text to be changed.
6025\(For an insertion, the beginning and end are at the same place.)
6026No information is given about the length of the text after the change.
6027
6028Buffer changes made while executing the `before-change-functions'
6029don't call any before-change or after-change functions.
7b2bf907 6030That's because `inhibit-modification-hooks' is temporarily set non-nil.
018ba359
PJ
6031
6032If an unhandled error happens in running these functions,
6033the variable's value remains nil. That prevents the error
6034from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
6035 Vbefore_change_functions = Qnil;
6036
29208e82 6037 DEFVAR_LISP ("after-change-functions", Vafter_change_functions,
eacdfade 6038 doc: /* List of functions to call after each text change.
018ba359
PJ
6039Three arguments are passed to each function: the positions of
6040the beginning and end of the range of changed text,
6041and the length in bytes of the pre-change text replaced by that range.
6042\(For an insertion, the pre-change length is zero;
6043for a deletion, that length is the number of bytes deleted,
6044and the post-change beginning and end are at the same place.)
6045
6046Buffer changes made while executing the `after-change-functions'
6047don't call any before-change or after-change functions.
7b2bf907 6048That's because `inhibit-modification-hooks' is temporarily set non-nil.
018ba359
PJ
6049
6050If an unhandled error happens in running these functions,
6051the variable's value remains nil. That prevents the error
6052from happening repeatedly and making Emacs nonfunctional. */);
5f079267
RS
6053 Vafter_change_functions = Qnil;
6054
29208e82 6055 DEFVAR_LISP ("first-change-hook", Vfirst_change_hook,
efc7e75f 6056 doc: /* A list of functions to call before changing a buffer which is unmodified.
018ba359 6057The functions are run using the `run-hooks' function. */);
dbc4e1c1 6058 Vfirst_change_hook = Qnil;
1ab256cb 6059
4b4deea2 6060 DEFVAR_PER_BUFFER ("buffer-undo-list", &BVAR (current_buffer, undo_list), Qnil,
7ee72033 6061 doc: /* List of undo entries in current buffer.
018ba359
PJ
6062Recent changes come first; older changes follow newer.
6063
6064An entry (BEG . END) represents an insertion which begins at
6065position BEG and ends at position END.
6066
6067An entry (TEXT . POSITION) represents the deletion of the string TEXT
6068from (abs POSITION). If POSITION is positive, point was at the front
6069of the text being deleted; if negative, point was at the end.
6070
d35af63c
PE
6071An entry (t HIGH LOW USEC PSEC) indicates that the buffer was previously
6072unmodified; (HIGH LOW USEC PSEC) is in the same style as (current-time)
6073and is the visited file's modification time, as of that time. If the
018ba359
PJ
6074modification time of the most recent save is different, this entry is
6075obsolete.
6076
6077An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
6078was modified between BEG and END. PROPERTY is the property name,
6079and VALUE is the old value.
6080
7405f386
KS
6081An entry (apply FUN-NAME . ARGS) means undo the change with
6082\(apply FUN-NAME ARGS).
6083
6084An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
6085in the active region. BEG and END is the range affected by this entry
6086and DELTA is the number of bytes added or deleted in that range by
6087this change.
c6c7dc03 6088
018ba359
PJ
6089An entry (MARKER . DISTANCE) indicates that the marker MARKER
6090was adjusted in position by the offset DISTANCE (an integer).
6091
6092An entry of the form POSITION indicates that point was at the buffer
6093location given by the integer. Undoing an entry of this form places
6094point at POSITION.
6095
b4c4f2f4
JB
6096Entries with value `nil' mark undo boundaries. The undo command treats
6097the changes between two undo boundaries as a single step to be undone.
018ba359
PJ
6098
6099If the value of the variable is t, undo information is not recorded. */);
6100
4b4deea2 6101 DEFVAR_PER_BUFFER ("mark-active", &BVAR (current_buffer, mark_active), Qnil,
7ee72033 6102 doc: /* Non-nil means the mark and region are currently active in this buffer. */);
018ba359 6103
4b4deea2 6104 DEFVAR_PER_BUFFER ("cache-long-line-scans", &BVAR (current_buffer, cache_long_line_scans), Qnil,
7ee72033 6105 doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly.
018ba359
PJ
6106
6107Normally, the line-motion functions work by scanning the buffer for
5629f29b
DK
6108newlines. Columnar operations (like `move-to-column' and
6109`compute-motion') also work by scanning the buffer, summing character
018ba359
PJ
6110widths as they go. This works well for ordinary text, but if the
6111buffer's lines are very long (say, more than 500 characters), these
6112motion functions will take longer to execute. Emacs may also take
6113longer to update the display.
6114
5629f29b 6115If `cache-long-line-scans' is non-nil, these motion functions cache the
018ba359
PJ
6116results of their scans, and consult the cache to avoid rescanning
6117regions of the buffer until the text is modified. The caches are most
6118beneficial when they prevent the most searching---that is, when the
6119buffer contains long lines and large regions of characters with the
6120same, fixed screen width.
6121
5629f29b 6122When `cache-long-line-scans' is non-nil, processing short lines will
018ba359
PJ
6123become slightly slower (because of the overhead of consulting the
6124cache), and the caches will use memory roughly proportional to the
6125number of newlines and characters whose screen width varies.
6126
6127The caches require no explicit maintenance; their accuracy is
6128maintained internally by the Emacs primitives. Enabling or disabling
6129the cache should not affect the behavior of any of the motion
6130functions; it should only affect their performance. */);
6131
4b4deea2 6132 DEFVAR_PER_BUFFER ("point-before-scroll", &BVAR (current_buffer, point_before_scroll), Qnil,
7ee72033 6133 doc: /* Value of point before the last series of scroll operations, or nil. */);
018ba359 6134
4b4deea2 6135 DEFVAR_PER_BUFFER ("buffer-file-format", &BVAR (current_buffer, file_format), Qnil,
7ee72033 6136 doc: /* List of formats to use when saving this buffer.
018ba359 6137Formats are defined by `format-alist'. This variable is
a9b9a780 6138set when a file is visited. */);
be9aafdd 6139
71ed49fa 6140 DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
4b4deea2 6141 &BVAR (current_buffer, auto_save_file_format), Qnil,
fb7ada5f 6142 doc: /* Format in which to write auto-save files.
71ed49fa
LT
6143Should be a list of symbols naming formats that are defined in `format-alist'.
6144If it is t, which is the default, auto-save files are written in the
6145same format as a regular save would use. */);
6146
3cb719bd 6147 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4b4deea2 6148 &BVAR (current_buffer, invisibility_spec), Qnil,
7ee72033 6149 doc: /* Invisibility spec of this buffer.
018ba359
PJ
6150The default is t, which means that text is invisible
6151if it has a non-nil `invisible' property.
6152If the value is a list, a text character is invisible if its `invisible'
b49dd850 6153property is an element in that list (or is a list with members in common).
018ba359
PJ
6154If an element is a cons cell of the form (PROP . ELLIPSIS),
6155then characters with property value PROP are invisible,
6156and they have an ellipsis as well if ELLIPSIS is non-nil. */);
3cb719bd 6157
7962a441 6158 DEFVAR_PER_BUFFER ("buffer-display-count",
4b4deea2 6159 &BVAR (current_buffer, display_count), Qnil,
7ee72033 6160 doc: /* A number incremented each time this buffer is displayed in a window.
018ba359 6161The function `set-window-buffer' increments it. */);
3fd364db
RS
6162
6163 DEFVAR_PER_BUFFER ("buffer-display-time",
4b4deea2 6164 &BVAR (current_buffer, display_time), Qnil,
7ee72033 6165 doc: /* Time stamp updated each time this buffer is displayed in a window.
018ba359
PJ
6166The function `set-window-buffer' updates this variable
6167to the value obtained by calling `current-time'.
6168If the buffer has never been shown in a window, the value is nil. */);
6169
29208e82 6170 DEFVAR_LISP ("transient-mark-mode", Vtransient_mark_mode,
9d794026
GM
6171 doc: /* Non-nil if Transient Mark mode is enabled.
6172See the command `transient-mark-mode' for a description of this minor mode.
6173
6174Non-nil also enables highlighting of the region whenever the mark is active.
6175The variable `highlight-nonselected-windows' controls whether to highlight
6176all windows or just the selected window.
6177
f49d1f52 6178Lisp programs may give this variable certain special values:
9d794026 6179
f49d1f52
SM
6180- A value of `lambda' enables Transient Mark mode temporarily.
6181 It is disabled again after any subsequent action that would
6182 normally deactivate the mark (e.g. buffer modification).
6183
6184- A value of (only . OLDVAL) enables Transient Mark mode
6185 temporarily. After any subsequent point motion command that is
6186 not shift-translated, or any other action that would normally
6187 deactivate the mark (e.g. buffer modification), the value of
6188 `transient-mark-mode' is set to OLDVAL. */);
c48f61ef
RS
6189 Vtransient_mark_mode = Qnil;
6190
29208e82 6191 DEFVAR_LISP ("inhibit-read-only", Vinhibit_read_only,
fb7ada5f 6192 doc: /* Non-nil means disregard read-only status of buffers or characters.
018ba359
PJ
6193If the value is t, disregard `buffer-read-only' and all `read-only'
6194text properties. If the value is a list, disregard `buffer-read-only'
6195and disregard a `read-only' text property if the property value
6196is a member of the list. */);
a96b68f1
RS
6197 Vinhibit_read_only = Qnil;
6198
4b4deea2 6199 DEFVAR_PER_BUFFER ("cursor-type", &BVAR (current_buffer, cursor_type), Qnil,
f6e22881 6200 doc: /* Cursor to use when this buffer is in the selected window.
018ba359
PJ
6201Values are interpreted as follows:
6202
b8dc613f
JB
6203 t use the cursor specified for the frame
6204 nil don't display a cursor
6205 box display a filled box cursor
6206 hollow display a hollow box cursor
6207 bar display a vertical bar cursor with default width
6208 (bar . WIDTH) display a vertical bar cursor with width WIDTH
6209 hbar display a horizontal bar cursor with default height
b4234f4c 6210 (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
b8dc613f 6211 ANYTHING ELSE display a hollow box cursor
cd8d5236 6212
e08b1705
MR
6213When the buffer is displayed in a non-selected window, the
6214cursor's appearance is instead controlled by the variable
6215`cursor-in-non-selected-windows'. */);
bb2ec976 6216
a3bbced0 6217 DEFVAR_PER_BUFFER ("line-spacing",
4b4deea2 6218 &BVAR (current_buffer, extra_line_spacing), Qnil,
7ee72033 6219 doc: /* Additional space to put between lines when displaying a buffer.
3ba010e5
EZ
6220The space is measured in pixels, and put below lines on graphic displays,
6221see `display-graphic-p'.
60ebfdf3 6222If value is a floating point number, it specifies the spacing relative
fc961256 6223to the default frame line height. A value of nil means add no extra space. */);
a3bbced0 6224
0124c5bd 6225 DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
4b4deea2 6226 &BVAR (current_buffer, cursor_in_non_selected_windows), Qnil,
fb7ada5f 6227 doc: /* Non-nil means show a cursor in non-selected windows.
66c6abf0
GM
6228If nil, only shows a cursor in the selected window.
6229If t, displays a cursor related to the usual cursor type
6230\(a solid box becomes hollow, a bar becomes a narrower bar).
6231You can also specify the cursor type as in the `cursor-type' variable.
6232Use Custom to set this variable and update the display." */);
0124c5bd 6233
29208e82 6234 DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions,
f6e22881
JB
6235 doc: /* List of functions called with no args to query before killing a buffer.
6236The buffer being killed will be current while the functions are running.
b7e8d081
MR
6237
6238If any of them returns nil, the buffer is not killed. Functions run by
6239this hook are supposed to not change the current buffer. */);
dcdffbf6
RS
6240 Vkill_buffer_query_functions = Qnil;
6241
29208e82 6242 DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
43ed3b8d
CY
6243 doc: /* Normal hook run before changing the major mode of a buffer.
6244The function `kill-all-local-variables' runs this before doing anything else. */);
6245 Vchange_major_mode_hook = Qnil;
cd3520a4 6246 DEFSYM (Qchange_major_mode_hook, "change-major-mode-hook");
43ed3b8d 6247
9397e56f
MR
6248 DEFVAR_LISP ("buffer-list-update-hook", Vbuffer_list_update_hook,
6249 doc: /* Hook run when the buffer list changes.
6250Functions running this hook are `get-buffer-create',
6251`make-indirect-buffer', `rename-buffer', `kill-buffer',
96a72ee9 6252and `bury-buffer-internal'. */);
9397e56f 6253 Vbuffer_list_update_hook = Qnil;
cd3520a4 6254 DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook");
9397e56f 6255
0dc88e60 6256 defsubr (&Sbuffer_live_p);
1ab256cb
RM
6257 defsubr (&Sbuffer_list);
6258 defsubr (&Sget_buffer);
6259 defsubr (&Sget_file_buffer);
6260 defsubr (&Sget_buffer_create);
336cd056 6261 defsubr (&Smake_indirect_buffer);
01050cb5 6262 defsubr (&Sgenerate_new_buffer_name);
1ab256cb 6263 defsubr (&Sbuffer_name);
1ab256cb 6264 defsubr (&Sbuffer_file_name);
336cd056 6265 defsubr (&Sbuffer_base_buffer);
79aa712d 6266 defsubr (&Sbuffer_local_value);
1ab256cb
RM
6267 defsubr (&Sbuffer_local_variables);
6268 defsubr (&Sbuffer_modified_p);
6269 defsubr (&Sset_buffer_modified_p);
6270 defsubr (&Sbuffer_modified_tick);
3e145152 6271 defsubr (&Sbuffer_chars_modified_tick);
1ab256cb
RM
6272 defsubr (&Srename_buffer);
6273 defsubr (&Sother_buffer);
1ab256cb
RM
6274 defsubr (&Sbuffer_enable_undo);
6275 defsubr (&Skill_buffer);
e4ed06f1 6276 defsubr (&Sbury_buffer_internal);
a9ee7a59 6277 defsubr (&Sset_buffer_major_mode);
1ab256cb
RM
6278 defsubr (&Scurrent_buffer);
6279 defsubr (&Sset_buffer);
6280 defsubr (&Sbarf_if_buffer_read_only);
3ac81adb 6281 defsubr (&Serase_buffer);
13cda5f9 6282 defsubr (&Sbuffer_swap_text);
3ac81adb 6283 defsubr (&Sset_buffer_multibyte);
1ab256cb 6284 defsubr (&Skill_all_local_variables);
2eec3b4e 6285
52f8ec73 6286 defsubr (&Soverlayp);
2eec3b4e
RS
6287 defsubr (&Smake_overlay);
6288 defsubr (&Sdelete_overlay);
6289 defsubr (&Smove_overlay);
8ebafa8d
JB
6290 defsubr (&Soverlay_start);
6291 defsubr (&Soverlay_end);
6292 defsubr (&Soverlay_buffer);
6293 defsubr (&Soverlay_properties);
2eec3b4e 6294 defsubr (&Soverlays_at);
74514898 6295 defsubr (&Soverlays_in);
2eec3b4e 6296 defsubr (&Snext_overlay_change);
239c932b 6297 defsubr (&Sprevious_overlay_change);
2eec3b4e
RS
6298 defsubr (&Soverlay_recenter);
6299 defsubr (&Soverlay_lists);
6300 defsubr (&Soverlay_get);
6301 defsubr (&Soverlay_put);
a8c21b48 6302 defsubr (&Srestore_buffer_modified_p);
1ab256cb
RM
6303}
6304
dfcf069d 6305void
d3da34e0 6306keys_of_buffer (void)
1ab256cb
RM
6307{
6308 initial_define_key (control_x_map, 'b', "switch-to-buffer");
6309 initial_define_key (control_x_map, 'k', "kill-buffer");
4158c17d
RM
6310
6311 /* This must not be in syms_of_buffer, because Qdisabled is not
6312 initialized when that function gets called. */
d67b4f80 6313 Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
1ab256cb 6314}