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