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