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