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