(syms_of_callproc) [MSDOS]: New lisp variable: binary-process.
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
c6c5df7f 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993
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
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
2381d133
JB
22#include <sys/types.h>
23#include <sys/stat.h>
1ab256cb
RM
24#include <sys/param.h>
25
26#ifndef MAXPATHLEN
27/* in 4.1, param.h fails to define this. */
28#define MAXPATHLEN 1024
29#endif /* not MAXPATHLEN */
30
18160b98 31#include <config.h>
1ab256cb 32#include "lisp.h"
21cf4cf8 33#include "intervals.h"
1ab256cb
RM
34#include "window.h"
35#include "commands.h"
36#include "buffer.h"
1ab256cb 37#include "indent.h"
d014bf88 38#include "blockinput.h"
1ab256cb
RM
39
40struct buffer *current_buffer; /* the current buffer */
41
42/* First buffer in chain of all buffers (in reverse order of creation).
43 Threaded through ->next. */
44
45struct buffer *all_buffers;
46
47/* This structure holds the default values of the buffer-local variables
48 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
49 The default value occupies the same slot in this structure
50 as an individual buffer's value occupies in that buffer.
51 Setting the default value also goes through the alist of buffers
52 and stores into each buffer that does not say it has a local value. */
53
54struct buffer buffer_defaults;
55
56/* A Lisp_Object pointer to the above, used for staticpro */
57
58static Lisp_Object Vbuffer_defaults;
59
60/* This structure marks which slots in a buffer have corresponding
61 default values in buffer_defaults.
62 Each such slot has a nonzero value in this structure.
63 The value has only one nonzero bit.
64
65 When a buffer has its own local value for a slot,
66 the bit for that slot (found in the same slot in this structure)
67 is turned on in the buffer's local_var_flags slot.
68
69 If a slot in this structure is -1, then even though there may
70 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
71 and the corresponding slot in buffer_defaults is not used.
72
73 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
74 but there is a default value which is copied into each buffer.
75
76 If a slot in this structure is negative, then even though there may
77 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
78 and the corresponding slot in buffer_defaults is not used.
79
80 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
81 zero, that is a bug */
82
83struct buffer buffer_local_flags;
84
85/* This structure holds the names of symbols whose values may be
86 buffer-local. It is indexed and accessed in the same way as the above. */
87
88struct buffer buffer_local_symbols;
89/* A Lisp_Object pointer to the above, used for staticpro */
90static Lisp_Object Vbuffer_local_symbols;
91
0fa3ba92
JB
92/* This structure holds the required types for the values in the
93 buffer-local slots. If a slot contains Qnil, then the
94 corresponding buffer slot may contain a value of any type. If a
95 slot contains an integer, then prospective values' tags must be
96 equal to that integer. When a tag does not match, the function
97 buffer_slot_type_mismatch will signal an error. */
98struct buffer buffer_local_types;
99
1ab256cb 100Lisp_Object Fset_buffer ();
01050cb5 101void set_buffer_internal ();
173f2a64 102static void call_overlay_mod_hooks ();
1ab256cb
RM
103
104/* Alist of all buffer names vs the buffers. */
105/* This used to be a variable, but is no longer,
106 to prevent lossage due to user rplac'ing this alist or its elements. */
107Lisp_Object Vbuffer_alist;
108
109/* Functions to call before and after each text change. */
110Lisp_Object Vbefore_change_function;
111Lisp_Object Vafter_change_function;
112
c48f61ef
RS
113Lisp_Object Vtransient_mark_mode;
114
a96b68f1
RS
115/* t means ignore all read-only text properties.
116 A list means ignore such a property if its value is a member of the list.
117 Any non-nil value means ignore buffer-read-only. */
118Lisp_Object Vinhibit_read_only;
119
dbc4e1c1
JB
120/* List of functions to call before changing an unmodified buffer. */
121Lisp_Object Vfirst_change_hook;
122Lisp_Object Qfirst_change_hook;
1ab256cb
RM
123
124Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
125
126Lisp_Object Qprotected_field;
127
128Lisp_Object QSFundamental; /* A string "Fundamental" */
129
130Lisp_Object Qkill_buffer_hook;
131
5fe0b67e
RS
132Lisp_Object Qget_file_buffer;
133
52f8ec73
JB
134Lisp_Object Qoverlayp;
135
294d215f
RS
136Lisp_Object Qmodification_hooks;
137Lisp_Object Qinsert_in_front_hooks;
138Lisp_Object Qinsert_behind_hooks;
139
1ab256cb
RM
140/* For debugging; temporary. See set_buffer_internal. */
141/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
142
143nsberror (spec)
144 Lisp_Object spec;
145{
146 if (XTYPE (spec) == Lisp_String)
147 error ("No buffer named %s", XSTRING (spec)->data);
148 error ("Invalid buffer argument");
149}
150\f
151DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 0, 0,
152 "Return a list of all existing live buffers.")
153 ()
154{
155 return Fmapcar (Qcdr, Vbuffer_alist);
156}
157
158DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
159 "Return the buffer named NAME (a string).\n\
160If there is no live buffer named NAME, return nil.\n\
161NAME may also be a buffer; if so, the value is that buffer.")
162 (name)
163 register Lisp_Object name;
164{
165 if (XTYPE (name) == Lisp_Buffer)
166 return name;
167 CHECK_STRING (name, 0);
168
169 return Fcdr (Fassoc (name, Vbuffer_alist));
170}
171
172DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
173 "Return the buffer visiting file FILENAME (a string).\n\
174If there is no such live buffer, return nil.")
175 (filename)
176 register Lisp_Object filename;
177{
178 register Lisp_Object tail, buf, tem;
5fe0b67e
RS
179 Lisp_Object handler;
180
1ab256cb
RM
181 CHECK_STRING (filename, 0);
182 filename = Fexpand_file_name (filename, Qnil);
183
5fe0b67e
RS
184 /* If the file name has special constructs in it,
185 call the corresponding file handler. */
186 handler = Ffind_file_name_handler (filename);
187 if (!NILP (handler))
188 return call2 (handler, Qget_file_buffer, filename);
189
1ab256cb
RM
190 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
191 {
192 buf = Fcdr (XCONS (tail)->car);
193 if (XTYPE (buf) != Lisp_Buffer) continue;
194 if (XTYPE (XBUFFER (buf)->filename) != Lisp_String) continue;
195 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 196 if (!NILP (tem))
1ab256cb
RM
197 return buf;
198 }
199 return Qnil;
200}
201
202/* Incremented for each buffer created, to assign the buffer number. */
203int buffer_count;
204
205DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
206 "Return the buffer named NAME, or create such a buffer and return it.\n\
207A new buffer is created if there is no live buffer named NAME.\n\
b44895bc 208If NAME starts with a space, the new buffer does not keep undo information.\n\
1ab256cb
RM
209If NAME is a buffer instead of a string, then it is the value returned.\n\
210The value is never nil.")
211 (name)
212 register Lisp_Object name;
213{
214 register Lisp_Object buf, function, tem;
215 int count = specpdl_ptr - specpdl;
216 register struct buffer *b;
217
218 buf = Fget_buffer (name);
265a9e55 219 if (!NILP (buf))
1ab256cb
RM
220 return buf;
221
9ac0d9e0 222 b = (struct buffer *) xmalloc (sizeof (struct buffer));
1ab256cb
RM
223
224 BUF_GAP_SIZE (b) = 20;
9ac0d9e0 225 BLOCK_INPUT;
1ab256cb 226 BUFFER_ALLOC (BUF_BEG_ADDR (b), BUF_GAP_SIZE (b));
9ac0d9e0 227 UNBLOCK_INPUT;
1ab256cb
RM
228 if (! BUF_BEG_ADDR (b))
229 memory_full ();
230
231 BUF_PT (b) = 1;
232 BUF_GPT (b) = 1;
233 BUF_BEGV (b) = 1;
234 BUF_ZV (b) = 1;
235 BUF_Z (b) = 1;
236 BUF_MODIFF (b) = 1;
237
238 /* Put this on the chain of all buffers including killed ones. */
239 b->next = all_buffers;
240 all_buffers = b;
241
242 b->mark = Fmake_marker ();
243 /*b->number = make_number (++buffer_count);*/
244 b->name = name;
245 if (XSTRING (name)->data[0] != ' ')
246 b->undo_list = Qnil;
247 else
248 b->undo_list = Qt;
249
250 reset_buffer (b);
251
252 /* Put this in the alist of all live buffers. */
253 XSET (buf, Lisp_Buffer, b);
254 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
255
256 b->mark = Fmake_marker ();
257 b->markers = Qnil;
258 b->name = name;
259
260 function = buffer_defaults.major_mode;
265a9e55 261 if (NILP (function))
1ab256cb
RM
262 {
263 tem = Fget (current_buffer->major_mode, Qmode_class);
264 if (EQ (tem, Qnil))
265 function = current_buffer->major_mode;
266 }
267
265a9e55 268 if (NILP (function) || EQ (function, Qfundamental_mode))
1ab256cb
RM
269 return buf;
270
271 /* To select a nonfundamental mode,
272 select the buffer temporarily and then call the mode function. */
273
274 record_unwind_protect (save_excursion_restore, save_excursion_save ());
275
276 Fset_buffer (buf);
277 call0 (function);
278
279 return unbind_to (count, buf);
280}
281
282/* Reinitialize everything about a buffer except its name and contents. */
283
284void
285reset_buffer (b)
286 register struct buffer *b;
287{
288 b->filename = Qnil;
289 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
290 b->modtime = 0;
291 b->save_modified = 1;
291026b5 292 XFASTINT (b->save_length) = 0;
1ab256cb
RM
293 b->last_window_start = 1;
294 b->backed_up = Qnil;
295 b->auto_save_modified = 0;
296 b->auto_save_file_name = Qnil;
297 b->read_only = Qnil;
2eec3b4e
RS
298 b->overlays_before = Qnil;
299 b->overlays_after = Qnil;
300 XFASTINT (b->overlay_center) = 1;
dfda7a7f 301 b->mark_active = Qnil;
33f7013e
JA
302
303 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
304 INITIALIZE_INTERVAL (b, NULL_INTERVAL);
305
1ab256cb
RM
306 reset_buffer_local_variables(b);
307}
308
c48f61ef 309reset_buffer_local_variables (b)
1ab256cb
RM
310 register struct buffer *b;
311{
312 register int offset;
313
314 /* Reset the major mode to Fundamental, together with all the
315 things that depend on the major mode.
316 default-major-mode is handled at a higher level.
317 We ignore it here. */
318 b->major_mode = Qfundamental_mode;
319 b->keymap = Qnil;
320 b->abbrev_table = Vfundamental_mode_abbrev_table;
321 b->mode_name = QSFundamental;
322 b->minor_modes = Qnil;
323 b->downcase_table = Vascii_downcase_table;
324 b->upcase_table = Vascii_upcase_table;
325 b->case_canon_table = Vascii_downcase_table;
326 b->case_eqv_table = Vascii_upcase_table;
327#if 0
328 b->sort_table = XSTRING (Vascii_sort_table);
329 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
330#endif /* 0 */
331
332 /* Reset all per-buffer variables to their defaults. */
333 b->local_var_alist = Qnil;
334 b->local_var_flags = 0;
335
336 /* For each slot that has a default value,
337 copy that into the slot. */
338
339 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
340 offset < sizeof (struct buffer);
341 offset += sizeof (Lisp_Object)) /* sizeof int == sizeof Lisp_Object */
342 if (*(int *)(offset + (char *) &buffer_local_flags) > 0
343 || *(int *)(offset + (char *) &buffer_local_flags) == -2)
344 *(Lisp_Object *)(offset + (char *)b) =
345 *(Lisp_Object *)(offset + (char *)&buffer_defaults);
346}
347
01050cb5
RM
348/* We split this away from generate-new-buffer, because rename-buffer
349 and set-visited-file-name ought to be able to use this to really
350 rename the buffer properly. */
351
352DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
c273e647 353 1, 2, 0,
01050cb5
RM
354 "Return a string that is the name of no existing buffer based on NAME.\n\
355If there is no live buffer named NAME, then return NAME.\n\
1ab256cb 356Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
c273e647 357until an unused name is found, and then return that name.\n\
03bdd54c 358Optional second argument IGNORE specifies a name that is okay to use\n\
c273e647 359\(if it is in the sequence to be tried)\n\
e8b3a22d 360even if a buffer with that name exists.")
c273e647
RS
361 (name, ignore)
362 register Lisp_Object name, ignore;
1ab256cb
RM
363{
364 register Lisp_Object gentemp, tem;
365 int count;
366 char number[10];
367
368 CHECK_STRING (name, 0);
369
370 tem = Fget_buffer (name);
265a9e55 371 if (NILP (tem))
01050cb5 372 return name;
1ab256cb
RM
373
374 count = 1;
375 while (1)
376 {
377 sprintf (number, "<%d>", ++count);
378 gentemp = concat2 (name, build_string (number));
638e4fc3 379 tem = Fstring_equal (gentemp, ignore);
c273e647
RS
380 if (!NILP (tem))
381 return gentemp;
1ab256cb 382 tem = Fget_buffer (gentemp);
265a9e55 383 if (NILP (tem))
01050cb5 384 return gentemp;
1ab256cb
RM
385 }
386}
387
388\f
389DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
390 "Return the name of BUFFER, as a string.\n\
01050cb5 391With no argument or nil as argument, return the name of the current buffer.")
1ab256cb
RM
392 (buffer)
393 register Lisp_Object buffer;
394{
265a9e55 395 if (NILP (buffer))
1ab256cb
RM
396 return current_buffer->name;
397 CHECK_BUFFER (buffer, 0);
398 return XBUFFER (buffer)->name;
399}
400
401DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
402 "Return name of file BUFFER is visiting, or nil if none.\n\
403No argument or nil as argument means use the current buffer.")
404 (buffer)
405 register Lisp_Object buffer;
406{
265a9e55 407 if (NILP (buffer))
1ab256cb
RM
408 return current_buffer->filename;
409 CHECK_BUFFER (buffer, 0);
410 return XBUFFER (buffer)->filename;
411}
412
413DEFUN ("buffer-local-variables", Fbuffer_local_variables,
414 Sbuffer_local_variables, 0, 1, 0,
415 "Return an alist of variables that are buffer-local in BUFFER.\n\
553defa4
RS
416Most elements look like (SYMBOL . VALUE), describing one variable.\n\
417For a symbol that is locally unbound, just the symbol appears in the value.\n\
1ab256cb
RM
418Note that storing new VALUEs in these elements doesn't change the variables.\n\
419No argument or nil as argument means use current buffer as BUFFER.")
420 (buffer)
421 register Lisp_Object buffer;
422{
423 register struct buffer *buf;
553defa4 424 register Lisp_Object result;
1ab256cb 425
265a9e55 426 if (NILP (buffer))
1ab256cb
RM
427 buf = current_buffer;
428 else
429 {
430 CHECK_BUFFER (buffer, 0);
431 buf = XBUFFER (buffer);
432 }
433
553defa4
RS
434 result = Qnil;
435
1ab256cb
RM
436 {
437 /* Reference each variable in the alist in our current buffer.
438 If inquiring about the current buffer, this gets the current values,
439 so store them into the alist so the alist is up to date.
440 If inquiring about some other buffer, this swaps out any values
441 for that buffer, making the alist up to date automatically. */
553defa4
RS
442 register Lisp_Object tail;
443 for (tail = buf->local_var_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1ab256cb 444 {
553defa4
RS
445 Lisp_Object val, elt;
446
447 elt = XCONS (tail)->car;
448
1ab256cb 449 if (buf == current_buffer)
553defa4
RS
450 val = find_symbol_value (XCONS (elt)->car);
451 else
452 val = XCONS (elt)->cdr;
453
454 /* If symbol is unbound, put just the symbol in the list. */
455 if (EQ (val, Qunbound))
456 result = Fcons (XCONS (elt)->car, result);
457 /* Otherwise, put (symbol . value) in the list. */
458 else
459 result = Fcons (Fcons (XCONS (elt)->car, val), result);
1ab256cb
RM
460 }
461 }
462
1ab256cb
RM
463 /* Add on all the variables stored in special slots. */
464 {
465 register int offset, mask;
466
467 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
468 offset < sizeof (struct buffer);
469 offset += (sizeof (int))) /* sizeof int == sizeof Lisp_Object */
470 {
471 mask = *(int *)(offset + (char *) &buffer_local_flags);
472 if (mask == -1 || (buf->local_var_flags & mask))
473 if (XTYPE (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols))
474 == Lisp_Symbol)
553defa4
RS
475 result = Fcons (Fcons (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols),
476 *(Lisp_Object *)(offset + (char *)buf)),
477 result);
1ab256cb
RM
478 }
479 }
553defa4
RS
480
481 return result;
1ab256cb
RM
482}
483
484\f
485DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
486 0, 1, 0,
487 "Return t if BUFFER was modified since its file was last read or saved.\n\
488No argument or nil as argument means use current buffer as BUFFER.")
489 (buffer)
490 register Lisp_Object buffer;
491{
492 register struct buffer *buf;
265a9e55 493 if (NILP (buffer))
1ab256cb
RM
494 buf = current_buffer;
495 else
496 {
497 CHECK_BUFFER (buffer, 0);
498 buf = XBUFFER (buffer);
499 }
500
501 return buf->save_modified < BUF_MODIFF (buf) ? Qt : Qnil;
502}
503
504DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
505 1, 1, 0,
506 "Mark current buffer as modified or unmodified according to FLAG.\n\
507A non-nil FLAG means mark the buffer modified.")
508 (flag)
509 register Lisp_Object flag;
510{
511 register int already;
512 register Lisp_Object fn;
513
514#ifdef CLASH_DETECTION
515 /* If buffer becoming modified, lock the file.
516 If buffer becoming unmodified, unlock the file. */
517
518 fn = current_buffer->filename;
265a9e55 519 if (!NILP (fn))
1ab256cb
RM
520 {
521 already = current_buffer->save_modified < MODIFF;
265a9e55 522 if (!already && !NILP (flag))
1ab256cb 523 lock_file (fn);
265a9e55 524 else if (already && NILP (flag))
1ab256cb
RM
525 unlock_file (fn);
526 }
527#endif /* CLASH_DETECTION */
528
265a9e55 529 current_buffer->save_modified = NILP (flag) ? MODIFF : 0;
1ab256cb
RM
530 update_mode_lines++;
531 return flag;
532}
533
534DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
535 0, 1, 0,
536 "Return BUFFER's tick counter, incremented for each change in text.\n\
537Each buffer has a tick counter which is incremented each time the text in\n\
538that buffer is changed. It wraps around occasionally.\n\
539No argument or nil as argument means use current buffer as BUFFER.")
540 (buffer)
541 register Lisp_Object buffer;
542{
543 register struct buffer *buf;
265a9e55 544 if (NILP (buffer))
1ab256cb
RM
545 buf = current_buffer;
546 else
547 {
548 CHECK_BUFFER (buffer, 0);
549 buf = XBUFFER (buffer);
550 }
551
552 return make_number (BUF_MODIFF (buf));
553}
554\f
01050cb5 555DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
4c7e5f09 556 "sRename buffer (to new name): \nP",
1ab256cb 557 "Change current buffer's name to NEWNAME (a string).\n\
3bd779aa 558If second arg UNIQUE is nil or omitted, it is an error if a\n\
01050cb5 559buffer named NEWNAME already exists.\n\
3bd779aa 560If UNIQUE is non-nil, come up with a new name using\n\
01050cb5 561`generate-new-buffer-name'.\n\
3bd779aa
RS
562Interactively, you can set UNIQUE with a prefix argument.\n\
563We return the name we actually gave the buffer.\n\
1ab256cb 564This does not change the name of the visited file (if any).")
3bd779aa
RS
565 (name, unique)
566 register Lisp_Object name, unique;
1ab256cb
RM
567{
568 register Lisp_Object tem, buf;
569
570 CHECK_STRING (name, 0);
571 tem = Fget_buffer (name);
c059b5ea
RM
572 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
573 the buffer automatically so you can create another with the original name.
574 It makes UNIQUE equivalent to
575 (rename-buffer (generate-new-buffer-name NAME)). */
576 if (NILP (unique) && XBUFFER (tem) == current_buffer)
01050cb5 577 return current_buffer->name;
265a9e55 578 if (!NILP (tem))
01050cb5 579 {
3bd779aa 580 if (!NILP (unique))
c273e647 581 name = Fgenerate_new_buffer_name (name, current_buffer->name);
01050cb5
RM
582 else
583 error ("Buffer name \"%s\" is in use", XSTRING (name)->data);
584 }
1ab256cb
RM
585
586 current_buffer->name = name;
76f590d7
JB
587
588 /* Catch redisplay's attention. Unless we do this, the mode lines for
589 any windows displaying current_buffer will stay unchanged. */
590 update_mode_lines++;
591
1ab256cb
RM
592 XSET (buf, Lisp_Buffer, current_buffer);
593 Fsetcar (Frassq (buf, Vbuffer_alist), name);
265a9e55 594 if (NILP (current_buffer->filename) && !NILP (current_buffer->auto_save_file_name))
1ab256cb 595 call0 (intern ("rename-auto-save-file"));
01050cb5 596 return name;
1ab256cb
RM
597}
598
a0ebb746 599DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
1ab256cb 600 "Return most recently selected buffer other than BUFFER.\n\
a0ebb746
JB
601Buffers not visible in windows are preferred to visible buffers,\n\
602unless optional second argument VISIBLE-OK is non-nil.\n\
1ab256cb
RM
603If no other buffer exists, the buffer `*scratch*' is returned.\n\
604If BUFFER is omitted or nil, some interesting buffer is returned.")
a0ebb746
JB
605 (buffer, visible_ok)
606 register Lisp_Object buffer, visible_ok;
1ab256cb
RM
607{
608 register Lisp_Object tail, buf, notsogood, tem;
609 notsogood = Qnil;
610
265a9e55 611 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
612 {
613 buf = Fcdr (Fcar (tail));
614 if (EQ (buf, buffer))
615 continue;
616 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
617 continue;
a0ebb746 618 if (NILP (visible_ok))
db732e5a 619 tem = Fget_buffer_window (buf, Qt);
a0ebb746
JB
620 else
621 tem = Qnil;
265a9e55 622 if (NILP (tem))
1ab256cb 623 return buf;
265a9e55 624 if (NILP (notsogood))
1ab256cb
RM
625 notsogood = buf;
626 }
265a9e55 627 if (!NILP (notsogood))
1ab256cb
RM
628 return notsogood;
629 return Fget_buffer_create (build_string ("*scratch*"));
630}
631\f
5b8bcf48 632DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo, 0, 1,
1ab256cb 6330,
5b8bcf48
RS
634 "Make BUFFER stop keeping undo information.\n\
635No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
636 (buffer)
637 register Lisp_Object buffer;
1ab256cb 638{
ffd56f97
JB
639 Lisp_Object real_buffer;
640
641 if (NILP (buffer))
642 XSET (real_buffer, Lisp_Buffer, current_buffer);
643 else
644 {
645 real_buffer = Fget_buffer (buffer);
646 if (NILP (real_buffer))
647 nsberror (buffer);
648 }
649
650 XBUFFER (real_buffer)->undo_list = Qt;
651
1ab256cb
RM
652 return Qnil;
653}
654
655DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
656 0, 1, "",
657 "Start keeping undo information for buffer BUFFER.\n\
658No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
659 (buffer)
660 register Lisp_Object buffer;
1ab256cb 661{
ffd56f97 662 Lisp_Object real_buffer;
1ab256cb 663
ffd56f97
JB
664 if (NILP (buffer))
665 XSET (real_buffer, Lisp_Buffer, current_buffer);
1ab256cb
RM
666 else
667 {
ffd56f97
JB
668 real_buffer = Fget_buffer (buffer);
669 if (NILP (real_buffer))
670 nsberror (buffer);
1ab256cb
RM
671 }
672
ffd56f97
JB
673 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
674 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
675
676 return Qnil;
677}
678
679/*
680 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
681Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
682The buffer being killed will be current while the hook is running.\n\
683See `kill-buffer'."
684 */
685DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
686 "Kill the buffer BUFFER.\n\
687The argument may be a buffer or may be the name of a buffer.\n\
688An argument of nil means kill the current buffer.\n\n\
689Value is t if the buffer is actually killed, nil if user says no.\n\n\
690The value of `kill-buffer-hook' (which may be local to that buffer),\n\
691if not void, is a list of functions to be called, with no arguments,\n\
692before the buffer is actually killed. The buffer to be killed is current\n\
693when the hook functions are called.\n\n\
694Any processes that have this buffer as the `process-buffer' are killed\n\
695with `delete-process'.")
696 (bufname)
697 Lisp_Object bufname;
698{
699 Lisp_Object buf;
700 register struct buffer *b;
701 register Lisp_Object tem;
702 register struct Lisp_Marker *m;
703 struct gcpro gcpro1, gcpro2;
704
265a9e55 705 if (NILP (bufname))
1ab256cb
RM
706 buf = Fcurrent_buffer ();
707 else
708 buf = Fget_buffer (bufname);
265a9e55 709 if (NILP (buf))
1ab256cb
RM
710 nsberror (bufname);
711
712 b = XBUFFER (buf);
713
714 /* Query if the buffer is still modified. */
265a9e55 715 if (INTERACTIVE && !NILP (b->filename)
1ab256cb
RM
716 && BUF_MODIFF (b) > b->save_modified)
717 {
718 GCPRO2 (buf, bufname);
719 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
720 XSTRING (b->name)->data));
721 UNGCPRO;
265a9e55 722 if (NILP (tem))
1ab256cb
RM
723 return Qnil;
724 }
725
726 /* Run kill-buffer hook with the buffer to be killed the current buffer. */
727 {
728 register Lisp_Object val;
729 int count = specpdl_ptr - specpdl;
730
731 record_unwind_protect (save_excursion_restore, save_excursion_save ());
732 set_buffer_internal (b);
733 call1 (Vrun_hooks, Qkill_buffer_hook);
734 unbind_to (count, Qnil);
735 }
736
737 /* We have no more questions to ask. Verify that it is valid
738 to kill the buffer. This must be done after the questions
739 since anything can happen within do_yes_or_no_p. */
740
741 /* Don't kill the minibuffer now current. */
742 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
743 return Qnil;
744
265a9e55 745 if (NILP (b->name))
1ab256cb
RM
746 return Qnil;
747
748 /* Make this buffer not be current.
749 In the process, notice if this is the sole visible buffer
750 and give up if so. */
751 if (b == current_buffer)
752 {
172a9c1f 753 tem = Fother_buffer (buf, Qnil);
1ab256cb
RM
754 Fset_buffer (tem);
755 if (b == current_buffer)
756 return Qnil;
757 }
758
759 /* Now there is no question: we can kill the buffer. */
760
761#ifdef CLASH_DETECTION
762 /* Unlock this buffer's file, if it is locked. */
763 unlock_buffer (b);
764#endif /* CLASH_DETECTION */
765
1ab256cb 766 kill_buffer_processes (buf);
1ab256cb
RM
767
768 tem = Vinhibit_quit;
769 Vinhibit_quit = Qt;
770 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
771 Freplace_buffer_in_windows (buf);
772 Vinhibit_quit = tem;
773
774 /* Delete any auto-save file. */
775 if (XTYPE (b->auto_save_file_name) == Lisp_String)
776 {
777 Lisp_Object tem;
778 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 779 if (! NILP (tem))
1ab256cb
RM
780 unlink (XSTRING (b->auto_save_file_name)->data);
781 }
782
783 /* Unchain all markers of this buffer
784 and leave them pointing nowhere. */
785 for (tem = b->markers; !EQ (tem, Qnil); )
786 {
787 m = XMARKER (tem);
788 m->buffer = 0;
789 tem = m->chain;
790 m->chain = Qnil;
791 }
792 b->markers = Qnil;
793
33f7013e
JA
794 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
795 INITIALIZE_INTERVAL (b, NULL_INTERVAL);
796 /* Perhaps we should explicitly free the interval tree here... */
797
1ab256cb 798 b->name = Qnil;
9ac0d9e0 799 BLOCK_INPUT;
1ab256cb 800 BUFFER_FREE (BUF_BEG_ADDR (b));
9ac0d9e0 801 UNBLOCK_INPUT;
1ab256cb
RM
802 b->undo_list = Qnil;
803
804 return Qt;
805}
806\f
36a8c287
JB
807/* Move the assoc for buffer BUF to the front of buffer-alist. Since
808 we do this each time BUF is selected visibly, the more recently
809 selected buffers are always closer to the front of the list. This
810 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb
RM
811
812record_buffer (buf)
813 Lisp_Object buf;
814{
815 register Lisp_Object link, prev;
816
817 prev = Qnil;
818 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
819 {
820 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
821 break;
822 prev = link;
823 }
824
36a8c287
JB
825 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
826 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 827
265a9e55 828 if (NILP (prev))
1ab256cb
RM
829 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
830 else
831 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
832
833 XCONS(link)->cdr = Vbuffer_alist;
834 Vbuffer_alist = link;
835}
836
837DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
838 "Select buffer BUFFER in the current window.\n\
839BUFFER may be a buffer or a buffer name.\n\
840Optional second arg NORECORD non-nil means\n\
841do not put this buffer at the front of the list of recently selected ones.\n\
842\n\
843WARNING: This is NOT the way to work on another buffer temporarily\n\
844within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
845the window-buffer correspondences.")
846 (bufname, norecord)
847 Lisp_Object bufname, norecord;
848{
849 register Lisp_Object buf;
850 Lisp_Object tem;
851
852 if (EQ (minibuf_window, selected_window))
853 error ("Cannot switch buffers in minibuffer window");
854 tem = Fwindow_dedicated_p (selected_window);
265a9e55 855 if (!NILP (tem))
1ab256cb
RM
856 error ("Cannot switch buffers in a dedicated window");
857
265a9e55 858 if (NILP (bufname))
172a9c1f 859 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb
RM
860 else
861 buf = Fget_buffer_create (bufname);
862 Fset_buffer (buf);
265a9e55 863 if (NILP (norecord))
1ab256cb
RM
864 record_buffer (buf);
865
866 Fset_window_buffer (EQ (selected_window, minibuf_window)
5fcd022d
JB
867 ? Fnext_window (minibuf_window, Qnil, Qnil)
868 : selected_window,
1ab256cb
RM
869 buf);
870
e8b3a22d 871 return buf;
1ab256cb
RM
872}
873
874DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 2, 0,
875 "Select buffer BUFFER in some window, preferably a different one.\n\
876If BUFFER is nil, then some other buffer is chosen.\n\
877If `pop-up-windows' is non-nil, windows can be split to do this.\n\
878If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
879window even if BUFFER is already visible in the selected window.")
880 (bufname, other)
881 Lisp_Object bufname, other;
882{
883 register Lisp_Object buf;
265a9e55 884 if (NILP (bufname))
172a9c1f 885 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1ab256cb
RM
886 else
887 buf = Fget_buffer_create (bufname);
888 Fset_buffer (buf);
889 record_buffer (buf);
890 Fselect_window (Fdisplay_buffer (buf, other));
e8b3a22d 891 return buf;
1ab256cb
RM
892}
893
894DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
895 "Return the current buffer as a Lisp object.")
896 ()
897{
898 register Lisp_Object buf;
899 XSET (buf, Lisp_Buffer, current_buffer);
900 return buf;
901}
902\f
903/* Set the current buffer to b */
904
905void
906set_buffer_internal (b)
907 register struct buffer *b;
908{
909 register struct buffer *old_buf;
910 register Lisp_Object tail, valcontents;
911 enum Lisp_Type tem;
912
913 if (current_buffer == b)
914 return;
915
916 windows_or_buffers_changed = 1;
917 old_buf = current_buffer;
918 current_buffer = b;
919 last_known_column_point = -1; /* invalidate indentation cache */
920
921 /* Look down buffer's list of local Lisp variables
922 to find and update any that forward into C variables. */
923
265a9e55 924 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
925 {
926 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
927 if ((XTYPE (valcontents) == Lisp_Buffer_Local_Value
928 || XTYPE (valcontents) == Lisp_Some_Buffer_Local_Value)
929 && (tem = XTYPE (XCONS (valcontents)->car),
930 (tem == Lisp_Boolfwd || tem == Lisp_Intfwd
931 || tem == Lisp_Objfwd)))
932 /* Just reference the variable
933 to cause it to become set for this buffer. */
934 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
935 }
936
937 /* Do the same with any others that were local to the previous buffer */
938
939 if (old_buf)
265a9e55 940 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
941 {
942 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
943 if ((XTYPE (valcontents) == Lisp_Buffer_Local_Value
944 || XTYPE (valcontents) == Lisp_Some_Buffer_Local_Value)
945 && (tem = XTYPE (XCONS (valcontents)->car),
946 (tem == Lisp_Boolfwd || tem == Lisp_Intfwd
947 || tem == Lisp_Objfwd)))
948 /* Just reference the variable
949 to cause it to become set for this buffer. */
950 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
951 }
952}
953
954DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
955 "Make the buffer BUFFER current for editing operations.\n\
956BUFFER may be a buffer or the name of an existing buffer.\n\
957See also `save-excursion' when you want to make a buffer current temporarily.\n\
958This function does not display the buffer, so its effect ends\n\
959when the current command terminates.\n\
960Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
961 (bufname)
962 register Lisp_Object bufname;
963{
964 register Lisp_Object buffer;
965 buffer = Fget_buffer (bufname);
265a9e55 966 if (NILP (buffer))
1ab256cb 967 nsberror (bufname);
265a9e55 968 if (NILP (XBUFFER (buffer)->name))
1ab256cb
RM
969 error ("Selecting deleted buffer");
970 set_buffer_internal (XBUFFER (buffer));
971 return buffer;
972}
973\f
974DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
975 Sbarf_if_buffer_read_only, 0, 0, 0,
976 "Signal a `buffer-read-only' error if the current buffer is read-only.")
977 ()
978{
a96b68f1
RS
979 if (!NILP (current_buffer->read_only)
980 && NILP (Vinhibit_read_only))
1ab256cb
RM
981 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
982 return Qnil;
983}
984
985DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
986 "Put BUFFER at the end of the list of all buffers.\n\
987There it is the least likely candidate for `other-buffer' to return;\n\
528415e7 988thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
a5611885
JB
989If BUFFER is nil or omitted, bury the current buffer.\n\
990Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
991selected window if it is displayed there.")
1ab256cb
RM
992 (buf)
993 register Lisp_Object buf;
994{
b271272a 995 /* Figure out what buffer we're going to bury. */
265a9e55 996 if (NILP (buf))
a5611885
JB
997 {
998 XSET (buf, Lisp_Buffer, current_buffer);
999
1000 /* If we're burying the current buffer, unshow it. */
5fcd022d 1001 Fswitch_to_buffer (Fother_buffer (buf, Qnil), Qnil);
a5611885 1002 }
1ab256cb
RM
1003 else
1004 {
1005 Lisp_Object buf1;
1006
1007 buf1 = Fget_buffer (buf);
265a9e55 1008 if (NILP (buf1))
1ab256cb
RM
1009 nsberror (buf);
1010 buf = buf1;
b271272a
JB
1011 }
1012
a5611885 1013 /* Move buf to the end of the buffer list. */
b271272a
JB
1014 {
1015 register Lisp_Object aelt, link;
1016
1017 aelt = Frassq (buf, Vbuffer_alist);
1018 link = Fmemq (aelt, Vbuffer_alist);
1019 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1020 XCONS (link)->cdr = Qnil;
1021 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1022 }
1ab256cb 1023
1ab256cb
RM
1024 return Qnil;
1025}
1026\f
c922bc55 1027DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1ab256cb 1028 "Delete the entire contents of the current buffer.\n\
2950a20e 1029Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1ab256cb
RM
1030so the buffer is truly empty after this.")
1031 ()
1032{
1033 Fwiden ();
1034 del_range (BEG, Z);
1035 current_buffer->last_window_start = 1;
1036 /* Prevent warnings, or suspension of auto saving, that would happen
1037 if future size is less than past size. Use of erase-buffer
1038 implies that the future text is not really related to the past text. */
1039 XFASTINT (current_buffer->save_length) = 0;
1040 return Qnil;
1041}
1042
1043validate_region (b, e)
1044 register Lisp_Object *b, *e;
1045{
1046 register int i;
1047
1048 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1049 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1050
1051 if (XINT (*b) > XINT (*e))
1052 {
1053 i = XFASTINT (*b); /* This is legit even if *b is < 0 */
1054 *b = *e;
1055 XFASTINT (*e) = i; /* because this is all we do with i. */
1056 }
1057
1058 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1059 && XINT (*e) <= ZV))
1060 args_out_of_range (*b, *e);
1061}
1062\f
1063Lisp_Object
1064list_buffers_1 (files)
1065 Lisp_Object files;
1066{
1067 register Lisp_Object tail, tem, buf;
1068 Lisp_Object col1, col2, col3, minspace;
1069 register struct buffer *old = current_buffer, *b;
718cde7d 1070 Lisp_Object desired_point;
1ab256cb
RM
1071 Lisp_Object other_file_symbol;
1072
718cde7d 1073 desired_point = Qnil;
1ab256cb
RM
1074 other_file_symbol = intern ("list-buffers-directory");
1075
1076 XFASTINT (col1) = 19;
1077 XFASTINT (col2) = 25;
1078 XFASTINT (col3) = 40;
1079 XFASTINT (minspace) = 1;
1080
1081 Fset_buffer (Vstandard_output);
1082
1083 tail = intern ("Buffer-menu-mode");
1084 if (!EQ (tail, current_buffer->major_mode)
265a9e55 1085 && (tem = Ffboundp (tail), !NILP (tem)))
1ab256cb
RM
1086 call0 (tail);
1087 Fbuffer_disable_undo (Vstandard_output);
1088 current_buffer->read_only = Qnil;
1089
1090 write_string ("\
1091 MR Buffer Size Mode File\n\
1092 -- ------ ---- ---- ----\n", -1);
1093
265a9e55 1094 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
1095 {
1096 buf = Fcdr (Fcar (tail));
1097 b = XBUFFER (buf);
1098 /* Don't mention the minibuffers. */
1099 if (XSTRING (b->name)->data[0] == ' ')
1100 continue;
1101 /* Optionally don't mention buffers that lack files. */
265a9e55 1102 if (!NILP (files) && NILP (b->filename))
1ab256cb
RM
1103 continue;
1104 /* Identify the current buffer. */
1105 if (b == old)
718cde7d 1106 XFASTINT (desired_point) = point;
1ab256cb
RM
1107 write_string (b == old ? "." : " ", -1);
1108 /* Identify modified buffers */
1109 write_string (BUF_MODIFF (b) > b->save_modified ? "*" : " ", -1);
265a9e55 1110 write_string (NILP (b->read_only) ? " " : "% ", -1);
1ab256cb
RM
1111 Fprinc (b->name, Qnil);
1112 Findent_to (col1, make_number (2));
1113 XFASTINT (tem) = BUF_Z (b) - BUF_BEG (b);
1114 Fprin1 (tem, Qnil);
1115 Findent_to (col2, minspace);
1116 Fprinc (b->mode_name, Qnil);
1117 Findent_to (col3, minspace);
1118
265a9e55 1119 if (!NILP (b->filename))
1ab256cb
RM
1120 Fprinc (b->filename, Qnil);
1121 else
1122 {
1123 /* No visited file; check local value of list-buffers-directory. */
1124 Lisp_Object tem;
1125 set_buffer_internal (b);
1126 tem = Fboundp (other_file_symbol);
265a9e55 1127 if (!NILP (tem))
1ab256cb
RM
1128 {
1129 tem = Fsymbol_value (other_file_symbol);
1130 Fset_buffer (Vstandard_output);
1131 if (XTYPE (tem) == Lisp_String)
1132 Fprinc (tem, Qnil);
1133 }
1134 else
1135 Fset_buffer (Vstandard_output);
1136 }
1137 write_string ("\n", -1);
1138 }
1139
1140 current_buffer->read_only = Qt;
1141 set_buffer_internal (old);
718cde7d 1142 return desired_point;
1ab256cb
RM
1143}
1144
1145DEFUN ("list-buffers", Flist_buffers, Slist_buffers, 0, 1, "P",
1146 "Display a list of names of existing buffers.\n\
1147The list is displayed in a buffer named `*Buffer List*'.\n\
1148Note that buffers with names starting with spaces are omitted.\n\
1149Non-null optional arg FILES-ONLY means mention only file buffers.\n\
1150\n\
1151The M column contains a * for buffers that are modified.\n\
1152The R column contains a % for buffers that are read-only.")
1153 (files)
1154 Lisp_Object files;
1155{
e87f38ef
JB
1156 Lisp_Object desired_point;
1157
fd2dab90
RS
1158 desired_point
1159 = internal_with_output_to_temp_buffer ("*Buffer List*",
1160 list_buffers_1, files);
e87f38ef 1161
718cde7d
JB
1162 if (NUMBERP (desired_point))
1163 {
1164 int count = specpdl_ptr - specpdl;
1165 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
1166 Fset_buffer (build_string ("*Buffer List*"));
1167 SET_PT (XINT (desired_point));
1168 return unbind_to (count, Qnil);
1169 }
fd2dab90 1170 return Qnil;
1ab256cb
RM
1171}
1172
1173DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1174 0, 0, 0,
1175 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1176Most local variable bindings are eliminated so that the default values\n\
1177become effective once more. Also, the syntax table is set from\n\
1178`standard-syntax-table', the local keymap is set to nil,\n\
1179and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1180This function also forces redisplay of the mode line.\n\
1181\n\
1182Every function to select a new major mode starts by\n\
1183calling this function.\n\n\
1184As a special exception, local variables whose names have\n\
1185a non-nil `permanent-local' property are not eliminated by this function.")
1186 ()
1187{
1188 register Lisp_Object alist, sym, tem;
1189 Lisp_Object oalist;
1190 oalist = current_buffer->local_var_alist;
1191
1192 /* Make sure no local variables remain set up with this buffer
1193 for their current values. */
1194
265a9e55 1195 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1196 {
1197 sym = XCONS (XCONS (alist)->car)->car;
1198
1199 /* Need not do anything if some other buffer's binding is now encached. */
1200 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car;
1201 if (XBUFFER (tem) == current_buffer)
1202 {
1203 /* Symbol is set up for this buffer's old local value.
1204 Set it up for the current buffer with the default value. */
1205
1206 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr;
542143d7
RS
1207 /* Store the symbol's current value into the alist entry
1208 it is currently set up for. This is so that, if the
1209 local is marked permanent, and we make it local again below,
1210 we don't lose the value. */
1211 XCONS (XCONS (tem)->car)->cdr = XCONS (XSYMBOL (sym)->value)->car;
1212 /* Switch to the symbol's default-value alist entry. */
1ab256cb 1213 XCONS (tem)->car = tem;
542143d7 1214 /* Mark it as current for the current buffer. */
1ab256cb 1215 XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Fcurrent_buffer ();
542143d7 1216 /* Store the current value into any forwarding in the symbol. */
1ab256cb
RM
1217 store_symval_forwarding (sym, XCONS (XSYMBOL (sym)->value)->car,
1218 XCONS (tem)->cdr);
1219 }
1220 }
1221
1222 /* Actually eliminate all local bindings of this buffer. */
1223
1224 reset_buffer_local_variables (current_buffer);
1225
1226 /* Redisplay mode lines; we are changing major mode. */
1227
1228 update_mode_lines++;
1229
1230 /* Any which are supposed to be permanent,
1231 make local again, with the same values they had. */
1232
265a9e55 1233 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1234 {
1235 sym = XCONS (XCONS (alist)->car)->car;
1236 tem = Fget (sym, Qpermanent_local);
265a9e55 1237 if (! NILP (tem))
01050cb5
RM
1238 {
1239 Fmake_local_variable (sym);
1240 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1241 }
1ab256cb
RM
1242 }
1243
1244 /* Force mode-line redisplay. Useful here because all major mode
1245 commands call this function. */
1246 update_mode_lines++;
1247
1248 return Qnil;
1249}
1250\f
2eec3b4e
RS
1251/* Find all the overlays in the current buffer that contain position POS.
1252 Return the number found, and store them in a vector in *VEC_PTR.
1253 Store in *LEN_PTR the size allocated for the vector.
52f8ec73
JB
1254 Store in *NEXT_PTR the next position after POS where an overlay starts,
1255 or ZV if there are no more overlays.
2eec3b4e
RS
1256
1257 *VEC_PTR and *LEN_PTR should contain a valid vector and size
61d54cd5
RS
1258 when this function is called.
1259
1260 If EXTEND is non-zero, we make the vector bigger if necessary.
1261 If EXTEND is zero, we never extend the vector,
1262 and we store only as many overlays as will fit.
1263 But we still return the total number of overlays. */
2eec3b4e
RS
1264
1265int
61d54cd5 1266overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr)
2eec3b4e 1267 int pos;
61d54cd5 1268 int extend;
2eec3b4e
RS
1269 Lisp_Object **vec_ptr;
1270 int *len_ptr;
1271 int *next_ptr;
1ab256cb 1272{
2eec3b4e
RS
1273 Lisp_Object tail, overlay, start, end, result;
1274 int idx = 0;
1275 int len = *len_ptr;
1276 Lisp_Object *vec = *vec_ptr;
1277 int next = ZV;
61d54cd5
RS
1278 int inhibit_storing = 0;
1279
2eec3b4e
RS
1280 for (tail = current_buffer->overlays_before;
1281 CONSP (tail);
1282 tail = XCONS (tail)->cdr)
1283 {
52f8ec73
JB
1284 int startpos;
1285
2eec3b4e
RS
1286 overlay = XCONS (tail)->car;
1287 if (! OVERLAY_VALID (overlay))
52f8ec73 1288 abort ();
1ab256cb 1289
2eec3b4e
RS
1290 start = OVERLAY_START (overlay);
1291 end = OVERLAY_END (overlay);
1292 if (OVERLAY_POSITION (end) <= pos)
1293 break;
1294 startpos = OVERLAY_POSITION (start);
1295 if (startpos <= pos)
1296 {
1297 if (idx == len)
1298 {
61d54cd5
RS
1299 /* The supplied vector is full.
1300 Either make it bigger, or don't store any more in it. */
1301 if (extend)
1302 {
1303 *len_ptr = len *= 2;
1304 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1305 *vec_ptr = vec;
1306 }
1307 else
1308 inhibit_storing = 1;
2eec3b4e 1309 }
61d54cd5
RS
1310
1311 if (!inhibit_storing)
1312 vec[idx] = overlay;
1313 /* Keep counting overlays even if we can't return them all. */
1314 idx++;
2eec3b4e
RS
1315 }
1316 else if (startpos < next)
1317 next = startpos;
1318 }
1319
1320 for (tail = current_buffer->overlays_after;
1321 CONSP (tail);
1322 tail = XCONS (tail)->cdr)
1ab256cb 1323 {
52f8ec73
JB
1324 int startpos;
1325
2eec3b4e
RS
1326 overlay = XCONS (tail)->car;
1327 if (! OVERLAY_VALID (overlay))
52f8ec73 1328 abort ();
2eec3b4e
RS
1329
1330 start = OVERLAY_START (overlay);
1331 end = OVERLAY_END (overlay);
1332 startpos = OVERLAY_POSITION (start);
52f8ec73 1333 if (pos < startpos)
2eec3b4e
RS
1334 {
1335 if (startpos < next)
1336 next = startpos;
1337 break;
1338 }
52f8ec73 1339 if (pos < OVERLAY_POSITION (end))
2eec3b4e
RS
1340 {
1341 if (idx == len)
1342 {
61d54cd5
RS
1343 if (extend)
1344 {
1345 *len_ptr = len *= 2;
1346 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1347 *vec_ptr = vec;
1348 }
1349 else
1350 inhibit_storing = 1;
2eec3b4e 1351 }
61d54cd5
RS
1352
1353 if (!inhibit_storing)
1354 vec[idx] = overlay;
1355 idx++;
2eec3b4e 1356 }
1ab256cb
RM
1357 }
1358
2eec3b4e
RS
1359 *next_ptr = next;
1360 return idx;
1361}
1362\f
5c4f68f1 1363/* Shift overlays in BUF's overlay lists, to center the lists at POS. */
1ab256cb 1364
2eec3b4e 1365void
5c4f68f1
JB
1366recenter_overlay_lists (buf, pos)
1367 struct buffer *buf;
2eec3b4e
RS
1368 int pos;
1369{
1370 Lisp_Object overlay, tail, next, prev, beg, end;
1371
1372 /* See if anything in overlays_before should move to overlays_after. */
1373
1374 /* We don't strictly need prev in this loop; it should always be nil.
1375 But we use it for symmetry and in case that should cease to be true
1376 with some future change. */
1377 prev = Qnil;
5c4f68f1 1378 for (tail = buf->overlays_before;
2eec3b4e
RS
1379 CONSP (tail);
1380 prev = tail, tail = next)
1ab256cb 1381 {
2eec3b4e
RS
1382 next = XCONS (tail)->cdr;
1383 overlay = XCONS (tail)->car;
1384
1385 /* If the overlay is not valid, get rid of it. */
1386 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
1387#if 1
1388 abort ();
1389#else
2eec3b4e
RS
1390 {
1391 /* Splice the cons cell TAIL out of overlays_before. */
1392 if (!NILP (prev))
1393 XCONS (prev)->cdr = next;
1394 else
5c4f68f1 1395 buf->overlays_before = next;
2eec3b4e
RS
1396 tail = prev;
1397 continue;
1398 }
52f8ec73 1399#endif
1ab256cb 1400
2eec3b4e
RS
1401 beg = OVERLAY_START (overlay);
1402 end = OVERLAY_END (overlay);
1ab256cb 1403
2eec3b4e 1404 if (OVERLAY_POSITION (end) > pos)
1ab256cb 1405 {
2eec3b4e
RS
1406 /* OVERLAY needs to be moved. */
1407 int where = OVERLAY_POSITION (beg);
1408 Lisp_Object other, other_prev;
1409
1410 /* Splice the cons cell TAIL out of overlays_before. */
1411 if (!NILP (prev))
1412 XCONS (prev)->cdr = next;
1413 else
5c4f68f1 1414 buf->overlays_before = next;
2eec3b4e
RS
1415
1416 /* Search thru overlays_after for where to put it. */
1417 other_prev = Qnil;
5c4f68f1 1418 for (other = buf->overlays_after;
2eec3b4e
RS
1419 CONSP (other);
1420 other_prev = other, other = XCONS (other)->cdr)
1ab256cb 1421 {
2eec3b4e
RS
1422 Lisp_Object otherbeg, otheroverlay, follower;
1423 int win;
1424
1425 otheroverlay = XCONS (other)->car;
1426 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 1427 abort ();
2eec3b4e
RS
1428
1429 otherbeg = OVERLAY_START (otheroverlay);
1430 if (OVERLAY_POSITION (otherbeg) >= where)
1431 break;
1ab256cb 1432 }
2eec3b4e
RS
1433
1434 /* Add TAIL to overlays_after before OTHER. */
1435 XCONS (tail)->cdr = other;
1436 if (!NILP (other_prev))
1437 XCONS (other_prev)->cdr = tail;
1ab256cb 1438 else
5c4f68f1 1439 buf->overlays_after = tail;
2eec3b4e 1440 tail = prev;
1ab256cb 1441 }
2eec3b4e
RS
1442 else
1443 /* We've reached the things that should stay in overlays_before.
1444 All the rest of overlays_before must end even earlier,
1445 so stop now. */
1446 break;
1447 }
1448
1449 /* See if anything in overlays_after should be in overlays_before. */
1450 prev = Qnil;
5c4f68f1 1451 for (tail = buf->overlays_after;
2eec3b4e
RS
1452 CONSP (tail);
1453 prev = tail, tail = next)
1454 {
1455 next = XCONS (tail)->cdr;
1456 overlay = XCONS (tail)->car;
1457
1458 /* If the overlay is not valid, get rid of it. */
1459 if (!OVERLAY_VALID (overlay))
52f8ec73
JB
1460#if 1
1461 abort ();
1462#else
2eec3b4e
RS
1463 {
1464 /* Splice the cons cell TAIL out of overlays_after. */
1465 if (!NILP (prev))
1466 XCONS (prev)->cdr = next;
1467 else
5c4f68f1 1468 buf->overlays_after = next;
2eec3b4e
RS
1469 tail = prev;
1470 continue;
1471 }
52f8ec73 1472#endif
2eec3b4e
RS
1473
1474 beg = OVERLAY_START (overlay);
1475 end = OVERLAY_END (overlay);
1476
1477 /* Stop looking, when we know that nothing further
1478 can possibly end before POS. */
1479 if (OVERLAY_POSITION (beg) > pos)
1480 break;
1481
1482 if (OVERLAY_POSITION (end) <= pos)
1483 {
1484 /* OVERLAY needs to be moved. */
1485 int where = OVERLAY_POSITION (end);
1486 Lisp_Object other, other_prev;
1487
1488 /* Splice the cons cell TAIL out of overlays_after. */
1489 if (!NILP (prev))
1490 XCONS (prev)->cdr = next;
1491 else
5c4f68f1 1492 buf->overlays_after = next;
2eec3b4e
RS
1493
1494 /* Search thru overlays_before for where to put it. */
1495 other_prev = Qnil;
5c4f68f1 1496 for (other = buf->overlays_before;
2eec3b4e
RS
1497 CONSP (other);
1498 other_prev = other, other = XCONS (other)->cdr)
1499 {
1500 Lisp_Object otherend, otheroverlay;
1501 int win;
1502
1503 otheroverlay = XCONS (other)->car;
1504 if (! OVERLAY_VALID (otheroverlay))
52f8ec73 1505 abort ();
2eec3b4e
RS
1506
1507 otherend = OVERLAY_END (otheroverlay);
1508 if (OVERLAY_POSITION (otherend) <= where)
1509 break;
1510 }
1511
1512 /* Add TAIL to overlays_before before OTHER. */
1513 XCONS (tail)->cdr = other;
1514 if (!NILP (other_prev))
1515 XCONS (other_prev)->cdr = tail;
1516 else
5c4f68f1 1517 buf->overlays_before = tail;
2eec3b4e
RS
1518 tail = prev;
1519 }
1520 }
1521
5c4f68f1 1522 XFASTINT (buf->overlay_center) = pos;
2eec3b4e
RS
1523}
1524\f
52f8ec73
JB
1525DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
1526 "Return t if OBJECT is an overlay.")
1527 (object)
1528 Lisp_Object object;
1529{
1530 return (OVERLAYP (object) ? Qt : Qnil);
1531}
1532
5c4f68f1
JB
1533DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 3, 0,
1534 "Create a new overlay with range BEG to END in BUFFER.\n\
1535If omitted, BUFFER defaults to the current buffer.\n\
2eec3b4e 1536BEG and END may be integers or markers.")
5c4f68f1
JB
1537 (beg, end, buffer)
1538 Lisp_Object beg, end, buffer;
2eec3b4e
RS
1539{
1540 Lisp_Object overlay;
5c4f68f1 1541 struct buffer *b;
2eec3b4e 1542
5c4f68f1
JB
1543 if (NILP (buffer))
1544 XSET (buffer, Lisp_Buffer, current_buffer);
883047b9
JB
1545 else
1546 CHECK_BUFFER (buffer, 2);
1547 if (MARKERP (beg)
1548 && ! EQ (Fmarker_buffer (beg), buffer))
1549 error ("Marker points into wrong buffer");
1550 if (MARKERP (end)
1551 && ! EQ (Fmarker_buffer (end), buffer))
1552 error ("Marker points into wrong buffer");
2eec3b4e 1553
883047b9
JB
1554 CHECK_NUMBER_COERCE_MARKER (beg, 1);
1555 CHECK_NUMBER_COERCE_MARKER (end, 1);
5c4f68f1 1556
883047b9 1557 if (XINT (beg) > XINT (end))
5c4f68f1 1558 {
883047b9
JB
1559 Lisp_Object temp = beg;
1560 beg = end; end = temp;
5c4f68f1 1561 }
883047b9
JB
1562
1563 b = XBUFFER (buffer);
1564
1565 beg = Fset_marker (Fmake_marker (), beg, buffer);
1566 end = Fset_marker (Fmake_marker (), end, buffer);
5c4f68f1
JB
1567
1568 overlay = Fcons (Fcons (beg, end), Qnil);
52f8ec73 1569 XSETTYPE (overlay, Lisp_Overlay);
2eec3b4e
RS
1570
1571 /* Put the new overlay on the wrong list. */
1572 end = OVERLAY_END (overlay);
5c4f68f1
JB
1573 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
1574 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 1575 else
5c4f68f1 1576 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
1577
1578 /* This puts it in the right list, and in the right order. */
5c4f68f1 1579 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 1580
b61982dd
JB
1581 /* We don't need to redisplay the region covered by the overlay, because
1582 the overlay has no properties at the moment. */
1583
2eec3b4e
RS
1584 return overlay;
1585}
1586
5c4f68f1
JB
1587DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
1588 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3ece337a
JB
1589If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
1590If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
1591buffer.")
5c4f68f1
JB
1592 (overlay, beg, end, buffer)
1593 Lisp_Object overlay, beg, end, buffer;
2eec3b4e 1594{
0a4469c9
RS
1595 struct buffer *b, *ob;
1596 Lisp_Object obuffer;
1597 int count = specpdl_ptr - specpdl;
5c4f68f1 1598
52f8ec73 1599 CHECK_OVERLAY (overlay, 0);
5c4f68f1
JB
1600 if (NILP (buffer))
1601 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3ece337a
JB
1602 if (NILP (buffer))
1603 XSET (buffer, Lisp_Buffer, current_buffer);
5c4f68f1 1604 CHECK_BUFFER (buffer, 3);
883047b9
JB
1605
1606 if (MARKERP (beg)
1607 && ! EQ (Fmarker_buffer (beg), buffer))
1608 error ("Marker points into wrong buffer");
1609 if (MARKERP (end)
1610 && ! EQ (Fmarker_buffer (end), buffer))
1611 error ("Marker points into wrong buffer");
1612
b61982dd
JB
1613 CHECK_NUMBER_COERCE_MARKER (beg, 1);
1614 CHECK_NUMBER_COERCE_MARKER (end, 1);
1615
0a4469c9
RS
1616 specbind (Qinhibit_quit, Qt);
1617
b61982dd
JB
1618 if (XINT (beg) > XINT (end))
1619 {
1620 Lisp_Object temp = beg;
1621 beg = end; end = temp;
1622 }
1623
0a4469c9 1624 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
5c4f68f1 1625 b = XBUFFER (buffer);
0a4469c9 1626 ob = XBUFFER (obuffer);
2eec3b4e 1627
c82ed728 1628 /* If the overlay has changed buffers, do a thorough redisplay. */
0a4469c9 1629 if (!EQ (buffer, obuffer))
c82ed728
JB
1630 windows_or_buffers_changed = 1;
1631 else
1632 /* Redisplay the area the overlay has just left, or just enclosed. */
1633 {
be8b1c6b
RS
1634 Lisp_Object o_beg;
1635 Lisp_Object o_end;
c82ed728
JB
1636 int change_beg, change_end;
1637
be8b1c6b
RS
1638 o_beg = OVERLAY_START (overlay);
1639 o_end = OVERLAY_END (overlay);
c82ed728
JB
1640 o_beg = OVERLAY_POSITION (o_beg);
1641 o_end = OVERLAY_POSITION (o_end);
1642
1643 if (XINT (o_beg) == XINT (beg))
1644 redisplay_region (b, XINT (o_end), XINT (end));
1645 else if (XINT (o_end) == XINT (end))
1646 redisplay_region (b, XINT (o_beg), XINT (beg));
1647 else
1648 {
1649 if (XINT (beg) < XINT (o_beg)) o_beg = beg;
1650 if (XINT (end) > XINT (o_end)) o_end = end;
1651 redisplay_region (b, XINT (o_beg), XINT (o_end));
1652 }
1653 }
b61982dd 1654
0a4469c9
RS
1655 if (!NILP (obuffer))
1656 {
1657 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
1658 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
1659 }
5c4f68f1
JB
1660
1661 Fset_marker (OVERLAY_START (overlay), beg, buffer);
1662 Fset_marker (OVERLAY_END (overlay), end, buffer);
2eec3b4e
RS
1663
1664 /* Put the overlay on the wrong list. */
1665 end = OVERLAY_END (overlay);
5c4f68f1
JB
1666 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
1667 b->overlays_after = Fcons (overlay, b->overlays_after);
2eec3b4e 1668 else
5c4f68f1 1669 b->overlays_before = Fcons (overlay, b->overlays_before);
2eec3b4e
RS
1670
1671 /* This puts it in the right list, and in the right order. */
5c4f68f1 1672 recenter_overlay_lists (b, XINT (b->overlay_center));
2eec3b4e 1673
0a4469c9 1674 return unbind_to (count, overlay);
2eec3b4e
RS
1675}
1676
1677DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
5c4f68f1 1678 "Delete the overlay OVERLAY from its buffer.")
2eec3b4e 1679 (overlay)
5c4f68f1 1680 Lisp_Object overlay;
2eec3b4e 1681{
0a4469c9 1682 Lisp_Object buffer;
5c4f68f1 1683 struct buffer *b;
0a4469c9 1684 int count = specpdl_ptr - specpdl;
5c4f68f1 1685
52f8ec73
JB
1686 CHECK_OVERLAY (overlay, 0);
1687
0a4469c9
RS
1688 buffer = Fmarker_buffer (OVERLAY_START (overlay));
1689 if (NILP (buffer))
1690 return Qnil;
1691
1692 b = XBUFFER (buffer);
1693
1694 specbind (Qinhibit_quit, Qt);
5c4f68f1
JB
1695
1696 b->overlays_before = Fdelq (overlay, b->overlays_before);
1697 b->overlays_after = Fdelq (overlay, b->overlays_after);
1698
b61982dd 1699 redisplay_region (b,
a927f5c9
RS
1700 marker_position (OVERLAY_START (overlay)),
1701 marker_position (OVERLAY_END (overlay)));
b61982dd 1702
3ece337a
JB
1703 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
1704 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
1705
0a4469c9 1706 return unbind_to (count, Qnil);
2eec3b4e
RS
1707}
1708\f
8ebafa8d
JB
1709/* Overlay dissection functions. */
1710
1711DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
1712 "Return the position at which OVERLAY starts.")
1713 (overlay)
1714 Lisp_Object overlay;
1715{
1716 CHECK_OVERLAY (overlay, 0);
1717
1718 return (Fmarker_position (OVERLAY_START (overlay)));
1719}
1720
1721DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
1722 "Return the position at which OVERLAY ends.")
1723 (overlay)
1724 Lisp_Object overlay;
1725{
1726 CHECK_OVERLAY (overlay, 0);
1727
1728 return (Fmarker_position (OVERLAY_END (overlay)));
1729}
1730
1731DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
1732 "Return the buffer OVERLAY belongs to.")
1733 (overlay)
1734 Lisp_Object overlay;
1735{
1736 CHECK_OVERLAY (overlay, 0);
1737
1738 return Fmarker_buffer (OVERLAY_START (overlay));
1739}
1740
1741DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
1742 "Return a list of the properties on OVERLAY.\n\
1743This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
1744OVERLAY.")
1745 (overlay)
1746 Lisp_Object overlay;
1747{
1748 CHECK_OVERLAY (overlay, 0);
1749
1750 return Fcopy_sequence (Fcdr_safe (XCONS (overlay)->cdr));
1751}
1752
1753\f
2eec3b4e 1754DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
eb8c3be9 1755 "Return a list of the overlays that contain position POS.")
2eec3b4e
RS
1756 (pos)
1757 Lisp_Object pos;
1758{
1759 int noverlays;
1760 int endpos;
1761 Lisp_Object *overlay_vec;
1762 int len;
1763 Lisp_Object result;
1764
1765 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1766
1767 len = 10;
1768 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
1769
1770 /* Put all the overlays we want in a vector in overlay_vec.
1771 Store the length in len. */
61d54cd5 1772 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &endpos);
2eec3b4e
RS
1773
1774 /* Make a list of them all. */
1775 result = Flist (noverlays, overlay_vec);
1776
9ac0d9e0 1777 xfree (overlay_vec);
2eec3b4e
RS
1778 return result;
1779}
1780
1781DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
1782 1, 1, 0,
1783 "Return the next position after POS where an overlay starts or ends.")
1784 (pos)
1785 Lisp_Object pos;
1786{
1787 int noverlays;
1788 int endpos;
1789 Lisp_Object *overlay_vec;
1790 int len;
1791 Lisp_Object result;
1792 int i;
1793
1794 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1795
1796 len = 10;
1797 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
1798
1799 /* Put all the overlays we want in a vector in overlay_vec.
1800 Store the length in len.
1801 endpos gets the position where the next overlay starts. */
61d54cd5 1802 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &endpos);
2eec3b4e
RS
1803
1804 /* If any of these overlays ends before endpos,
1805 use its ending point instead. */
1806 for (i = 0; i < noverlays; i++)
1807 {
1808 Lisp_Object oend;
1809 int oendpos;
1810
1811 oend = OVERLAY_END (overlay_vec[i]);
1812 oendpos = OVERLAY_POSITION (oend);
1813 if (oendpos < endpos)
1814 endpos = oendpos;
1ab256cb
RM
1815 }
1816
9ac0d9e0 1817 xfree (overlay_vec);
2eec3b4e
RS
1818 return make_number (endpos);
1819}
1820\f
1821/* These functions are for debugging overlays. */
1822
1823DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
1824 "Return a pair of lists giving all the overlays of the current buffer.\n\
1825The car has all the overlays before the overlay center;\n\
1826the cdr has all the overlays before the overlay center.\n\
1827Recentering overlays moves overlays between these lists.\n\
1828The lists you get are copies, so that changing them has no effect.\n\
1829However, the overlays you get are the real objects that the buffer uses.")
1830 ()
1831{
1832 Lisp_Object before, after;
1833 before = current_buffer->overlays_before;
1834 if (CONSP (before))
1835 before = Fcopy_sequence (before);
1836 after = current_buffer->overlays_after;
1837 if (CONSP (after))
1838 after = Fcopy_sequence (after);
1839
1840 return Fcons (before, after);
1841}
1842
1843DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
1844 "Recenter the overlays of the current buffer around position POS.")
1845 (pos)
1846 Lisp_Object pos;
1847{
1848 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1849
5c4f68f1 1850 recenter_overlay_lists (current_buffer, XINT (pos));
2eec3b4e
RS
1851 return Qnil;
1852}
1853\f
1854DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
1855 "Get the property of overlay OVERLAY with property name NAME.")
1856 (overlay, prop)
1857 Lisp_Object overlay, prop;
1858{
1859 Lisp_Object plist;
52f8ec73
JB
1860
1861 CHECK_OVERLAY (overlay, 0);
1862
1863 for (plist = Fcdr_safe (XCONS (overlay)->cdr);
2eec3b4e
RS
1864 CONSP (plist) && CONSP (XCONS (plist)->cdr);
1865 plist = XCONS (XCONS (plist)->cdr)->cdr)
1866 {
1867 if (EQ (XCONS (plist)->car, prop))
1868 return XCONS (XCONS (plist)->cdr)->car;
1869 }
52f8ec73
JB
1870
1871 return Qnil;
2eec3b4e
RS
1872}
1873
1874DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
1875 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
1876 (overlay, prop, value)
1877 Lisp_Object overlay, prop, value;
1878{
1879 Lisp_Object plist, tail;
1880
52f8ec73 1881 CHECK_OVERLAY (overlay, 0);
b61982dd 1882
25d16451
RS
1883 tail = Fmarker_buffer (OVERLAY_START (overlay));
1884 if (! NILP (tail))
1885 redisplay_region (XMARKER (OVERLAY_START (overlay))->buffer,
1886 marker_position (OVERLAY_START (overlay)),
1887 marker_position (OVERLAY_END (overlay)));
b61982dd 1888
52f8ec73 1889 plist = Fcdr_safe (XCONS (overlay)->cdr);
2eec3b4e
RS
1890
1891 for (tail = plist;
1892 CONSP (tail) && CONSP (XCONS (tail)->cdr);
1893 tail = XCONS (XCONS (tail)->cdr)->cdr)
1894 {
1895 if (EQ (XCONS (tail)->car, prop))
1896 return XCONS (XCONS (tail)->cdr)->car = value;
1897 }
1898
1899 if (! CONSP (XCONS (overlay)->cdr))
1900 XCONS (overlay)->cdr = Fcons (Qnil, Qnil);
1901
1902 XCONS (XCONS (overlay)->cdr)->cdr
1903 = Fcons (prop, Fcons (value, plist));
1904
1905 return value;
1ab256cb
RM
1906}
1907\f
173f2a64
RS
1908/* Run the modification-hooks of overlays that include
1909 any part of the text in START to END.
1910 Run the insert-before-hooks of overlay starting at END,
1911 and the insert-after-hooks of overlay ending at START. */
1912
1913void
1914verify_overlay_modification (start, end)
1915 Lisp_Object start, end;
1916{
1917 Lisp_Object prop, overlay, tail;
1918 int insertion = EQ (start, end);
1919
1920 for (tail = current_buffer->overlays_before;
1921 CONSP (tail);
1922 tail = XCONS (tail)->cdr)
1923 {
1924 int startpos, endpos;
be8b1c6b 1925 Lisp_Object ostart, oend;
173f2a64
RS
1926
1927 overlay = XCONS (tail)->car;
1928
1929 ostart = OVERLAY_START (overlay);
1930 oend = OVERLAY_END (overlay);
1931 endpos = OVERLAY_POSITION (oend);
1932 if (XFASTINT (start) > endpos)
1933 break;
1934 startpos = OVERLAY_POSITION (ostart);
1935 if (XFASTINT (end) == startpos && insertion)
1936 {
1937 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
1938 call_overlay_mod_hooks (prop, overlay, start, end);
1939 }
1940 if (XFASTINT (start) == endpos && insertion)
1941 {
1942 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
1943 call_overlay_mod_hooks (prop, overlay, start, end);
1944 }
1945 if (insertion
1946 ? (XFASTINT (start) > startpos && XFASTINT (end) < endpos)
1947 : (XFASTINT (start) >= startpos && XFASTINT (end) <= endpos))
1948 {
1949 prop = Foverlay_get (overlay, Qmodification_hooks);
1950 call_overlay_mod_hooks (prop, overlay, start, end);
1951 }
1952 }
1953
1954 for (tail = current_buffer->overlays_after;
1955 CONSP (tail);
1956 tail = XCONS (tail)->cdr)
1957 {
1958 int startpos, endpos;
be8b1c6b 1959 Lisp_Object ostart, oend;
173f2a64
RS
1960
1961 overlay = XCONS (tail)->car;
1962
1963 ostart = OVERLAY_START (overlay);
1964 oend = OVERLAY_END (overlay);
1965 startpos = OVERLAY_POSITION (ostart);
cdf0b096 1966 endpos = OVERLAY_POSITION (oend);
173f2a64
RS
1967 if (XFASTINT (end) < startpos)
1968 break;
1969 if (XFASTINT (end) == startpos && insertion)
1970 {
1971 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
1972 call_overlay_mod_hooks (prop, overlay, start, end);
1973 }
1974 if (XFASTINT (start) == endpos && insertion)
1975 {
1976 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
1977 call_overlay_mod_hooks (prop, overlay, start, end);
1978 }
1979 if (insertion
1980 ? (XFASTINT (start) > startpos && XFASTINT (end) < endpos)
1981 : (XFASTINT (start) >= startpos && XFASTINT (end) <= endpos))
1982 {
1983 prop = Foverlay_get (overlay, Qmodification_hooks);
1984 call_overlay_mod_hooks (prop, overlay, start, end);
1985 }
1986 }
1987}
1988
1989static void
1990call_overlay_mod_hooks (list, overlay, start, end)
1991 Lisp_Object list, overlay, start, end;
1992{
1993 struct gcpro gcpro1;
1994 GCPRO1 (list);
1995 while (!NILP (list))
1996 {
1997 call3 (Fcar (list), overlay, start, end);
1998 list = Fcdr (list);
1999 }
2000 UNGCPRO;
2001}
2002\f
0fa3ba92
JB
2003/* Somebody has tried to store NEWVAL into the buffer-local slot with
2004 offset XUINT (valcontents), and NEWVAL has an unacceptable type. */
2005void
2006buffer_slot_type_mismatch (valcontents, newval)
2007 Lisp_Object valcontents, newval;
2008{
2009 unsigned int offset = XUINT (valcontents);
5fcd022d 2010 unsigned char *symbol_name =
0fa3ba92
JB
2011 (XSYMBOL (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols))
2012 ->name->data);
2013 char *type_name;
2014
2015 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
2016 {
2017 case Lisp_Int: type_name = "integers"; break;
2018 case Lisp_String: type_name = "strings"; break;
2019 case Lisp_Marker: type_name = "markers"; break;
2020 case Lisp_Symbol: type_name = "symbols"; break;
2021 case Lisp_Cons: type_name = "lists"; break;
5fcd022d 2022 case Lisp_Vector: type_name = "vectors"; break;
0fa3ba92
JB
2023 default:
2024 abort ();
2025 }
2026
2027 error ("only %s should be stored in the buffer-local variable %s",
2028 type_name, symbol_name);
2029}
2030\f
1ab256cb
RM
2031init_buffer_once ()
2032{
2033 register Lisp_Object tem;
2034
2035 /* Make sure all markable slots in buffer_defaults
2036 are initialized reasonably, so mark_buffer won't choke. */
2037 reset_buffer (&buffer_defaults);
2038 reset_buffer (&buffer_local_symbols);
2039 XSET (Vbuffer_defaults, Lisp_Buffer, &buffer_defaults);
2040 XSET (Vbuffer_local_symbols, Lisp_Buffer, &buffer_local_symbols);
2041
2042 /* Set up the default values of various buffer slots. */
2043 /* Must do these before making the first buffer! */
2044
2045 /* real setup is done in loaddefs.el */
2046 buffer_defaults.mode_line_format = build_string ("%-");
2047 buffer_defaults.abbrev_mode = Qnil;
2048 buffer_defaults.overwrite_mode = Qnil;
2049 buffer_defaults.case_fold_search = Qt;
2050 buffer_defaults.auto_fill_function = Qnil;
2051 buffer_defaults.selective_display = Qnil;
2052#ifndef old
2053 buffer_defaults.selective_display_ellipses = Qt;
2054#endif
2055 buffer_defaults.abbrev_table = Qnil;
2056 buffer_defaults.display_table = Qnil;
1ab256cb 2057 buffer_defaults.undo_list = Qnil;
c48f61ef 2058 buffer_defaults.mark_active = Qnil;
2eec3b4e
RS
2059 buffer_defaults.overlays_before = Qnil;
2060 buffer_defaults.overlays_after = Qnil;
2061 XFASTINT (buffer_defaults.overlay_center) = 1;
1ab256cb
RM
2062
2063 XFASTINT (buffer_defaults.tab_width) = 8;
2064 buffer_defaults.truncate_lines = Qnil;
2065 buffer_defaults.ctl_arrow = Qt;
2066
2067 XFASTINT (buffer_defaults.fill_column) = 70;
2068 XFASTINT (buffer_defaults.left_margin) = 0;
2069
2070 /* Assign the local-flags to the slots that have default values.
2071 The local flag is a bit that is used in the buffer
2072 to say that it has its own local value for the slot.
2073 The local flag bits are in the local_var_flags slot of the buffer. */
2074
2075 /* Nothing can work if this isn't true */
2076 if (sizeof (int) != sizeof (Lisp_Object)) abort ();
2077
2078 /* 0 means not a lisp var, -1 means always local, else mask */
2079 bzero (&buffer_local_flags, sizeof buffer_local_flags);
2080 XFASTINT (buffer_local_flags.filename) = -1;
2081 XFASTINT (buffer_local_flags.directory) = -1;
2082 XFASTINT (buffer_local_flags.backed_up) = -1;
2083 XFASTINT (buffer_local_flags.save_length) = -1;
2084 XFASTINT (buffer_local_flags.auto_save_file_name) = -1;
2085 XFASTINT (buffer_local_flags.read_only) = -1;
2086 XFASTINT (buffer_local_flags.major_mode) = -1;
2087 XFASTINT (buffer_local_flags.mode_name) = -1;
2088 XFASTINT (buffer_local_flags.undo_list) = -1;
c48f61ef 2089 XFASTINT (buffer_local_flags.mark_active) = -1;
1ab256cb
RM
2090
2091 XFASTINT (buffer_local_flags.mode_line_format) = 1;
2092 XFASTINT (buffer_local_flags.abbrev_mode) = 2;
2093 XFASTINT (buffer_local_flags.overwrite_mode) = 4;
2094 XFASTINT (buffer_local_flags.case_fold_search) = 8;
2095 XFASTINT (buffer_local_flags.auto_fill_function) = 0x10;
2096 XFASTINT (buffer_local_flags.selective_display) = 0x20;
2097#ifndef old
2098 XFASTINT (buffer_local_flags.selective_display_ellipses) = 0x40;
2099#endif
2100 XFASTINT (buffer_local_flags.tab_width) = 0x80;
2101 XFASTINT (buffer_local_flags.truncate_lines) = 0x100;
2102 XFASTINT (buffer_local_flags.ctl_arrow) = 0x200;
2103 XFASTINT (buffer_local_flags.fill_column) = 0x400;
2104 XFASTINT (buffer_local_flags.left_margin) = 0x800;
2105 XFASTINT (buffer_local_flags.abbrev_table) = 0x1000;
2106 XFASTINT (buffer_local_flags.display_table) = 0x2000;
1ab256cb
RM
2107 XFASTINT (buffer_local_flags.syntax_table) = 0x8000;
2108
2109 Vbuffer_alist = Qnil;
2110 current_buffer = 0;
2111 all_buffers = 0;
2112
2113 QSFundamental = build_string ("Fundamental");
2114
2115 Qfundamental_mode = intern ("fundamental-mode");
2116 buffer_defaults.major_mode = Qfundamental_mode;
2117
2118 Qmode_class = intern ("mode-class");
2119
2120 Qprotected_field = intern ("protected-field");
2121
2122 Qpermanent_local = intern ("permanent-local");
2123
2124 Qkill_buffer_hook = intern ("kill-buffer-hook");
2125
2126 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
2127 /* super-magic invisible buffer */
2128 Vbuffer_alist = Qnil;
2129
ffd56f97 2130 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1ab256cb
RM
2131}
2132
2133init_buffer ()
2134{
2135 char buf[MAXPATHLEN+1];
2381d133
JB
2136 char *pwd;
2137 struct stat dotstat, pwdstat;
136351b7 2138 Lisp_Object temp;
1ab256cb
RM
2139
2140 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
2381d133
JB
2141
2142 /* If PWD is accurate, use it instead of calling getwd. This is faster
2143 when PWD is right, and may avoid a fatal error. */
2144 if ((pwd = getenv ("PWD")) != 0 && *pwd == '/'
2145 && stat (pwd, &pwdstat) == 0
2146 && stat (".", &dotstat) == 0
2147 && dotstat.st_ino == pwdstat.st_ino
2148 && dotstat.st_dev == pwdstat.st_dev
2149 && strlen (pwd) < MAXPATHLEN)
2150 strcpy (buf, pwd);
2151 else if (getwd (buf) == 0)
1ab256cb
RM
2152 fatal ("`getwd' failed: %s.\n", buf);
2153
2154#ifndef VMS
2155 /* Maybe this should really use some standard subroutine
2156 whose definition is filename syntax dependent. */
2157 if (buf[strlen (buf) - 1] != '/')
2158 strcat (buf, "/");
2159#endif /* not VMS */
2160 current_buffer->directory = build_string (buf);
136351b7
RS
2161
2162 temp = get_minibuffer (0);
2163 XBUFFER (temp)->directory = current_buffer->directory;
1ab256cb
RM
2164}
2165
2166/* initialize the buffer routines */
2167syms_of_buffer ()
2168{
188d4d11
RM
2169 extern Lisp_Object Qdisabled;
2170
1ab256cb
RM
2171 staticpro (&Vbuffer_defaults);
2172 staticpro (&Vbuffer_local_symbols);
2173 staticpro (&Qfundamental_mode);
2174 staticpro (&Qmode_class);
2175 staticpro (&QSFundamental);
2176 staticpro (&Vbuffer_alist);
2177 staticpro (&Qprotected_field);
2178 staticpro (&Qpermanent_local);
2179 staticpro (&Qkill_buffer_hook);
52f8ec73 2180 staticpro (&Qoverlayp);
294d215f
RS
2181 staticpro (&Qmodification_hooks);
2182 Qmodification_hooks = intern ("modification-hooks");
2183 staticpro (&Qinsert_in_front_hooks);
2184 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
2185 staticpro (&Qinsert_behind_hooks);
2186 Qinsert_behind_hooks = intern ("insert-behind-hooks");
5fe0b67e
RS
2187 staticpro (&Qget_file_buffer);
2188 Qget_file_buffer = intern ("get-file-buffer");
52f8ec73
JB
2189
2190 Qoverlayp = intern ("overlayp");
1ab256cb
RM
2191
2192 Fput (Qprotected_field, Qerror_conditions,
2193 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
2194 Fput (Qprotected_field, Qerror_message,
2195 build_string ("Attempt to modify a protected field"));
2196
188d4d11
RM
2197 Fput (intern ("erase-buffer"), Qdisabled, Qt);
2198
1ab256cb
RM
2199 /* All these use DEFVAR_LISP_NOPRO because the slots in
2200 buffer_defaults will all be marked via Vbuffer_defaults. */
2201
2202 DEFVAR_LISP_NOPRO ("default-mode-line-format",
2203 &buffer_defaults.mode_line_format,
2204 "Default value of `mode-line-format' for buffers that don't override it.\n\
2205This is the same as (default-value 'mode-line-format).");
2206
2207 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
2208 &buffer_defaults.abbrev_mode,
2209 "Default value of `abbrev-mode' for buffers that do not override it.\n\
2210This is the same as (default-value 'abbrev-mode).");
2211
2212 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
2213 &buffer_defaults.ctl_arrow,
2214 "Default value of `ctl-arrow' for buffers that do not override it.\n\
2215This is the same as (default-value 'ctl-arrow).");
2216
2217 DEFVAR_LISP_NOPRO ("default-truncate-lines",
2218 &buffer_defaults.truncate_lines,
2219 "Default value of `truncate-lines' for buffers that do not override it.\n\
2220This is the same as (default-value 'truncate-lines).");
2221
2222 DEFVAR_LISP_NOPRO ("default-fill-column",
2223 &buffer_defaults.fill_column,
2224 "Default value of `fill-column' for buffers that do not override it.\n\
2225This is the same as (default-value 'fill-column).");
2226
2227 DEFVAR_LISP_NOPRO ("default-left-margin",
2228 &buffer_defaults.left_margin,
2229 "Default value of `left-margin' for buffers that do not override it.\n\
2230This is the same as (default-value 'left-margin).");
2231
2232 DEFVAR_LISP_NOPRO ("default-tab-width",
2233 &buffer_defaults.tab_width,
2234 "Default value of `tab-width' for buffers that do not override it.\n\
2235This is the same as (default-value 'tab-width).");
2236
2237 DEFVAR_LISP_NOPRO ("default-case-fold-search",
2238 &buffer_defaults.case_fold_search,
2239 "Default value of `case-fold-search' for buffers that don't override it.\n\
2240This is the same as (default-value 'case-fold-search).");
2241
0fa3ba92
JB
2242 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
2243 Qnil, 0);
1ab256cb
RM
2244
2245/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
2246 But make-docfile finds it!
2247 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
bec44fd6 2248 Qnil,
1ab256cb
RM
2249 "Template for displaying mode line for current buffer.\n\
2250Each buffer has its own value of this variable.\n\
2251Value may be a string, a symbol or a list or cons cell.\n\
2252For a symbol, its value is used (but it is ignored if t or nil).\n\
2253 A string appearing directly as the value of a symbol is processed verbatim\n\
2254 in that the %-constructs below are not recognized.\n\
2255For a list whose car is a symbol, the symbol's value is taken,\n\
2256 and if that is non-nil, the cadr of the list is processed recursively.\n\
2257 Otherwise, the caddr of the list (if there is one) is processed.\n\
2258For a list whose car is a string or list, each element is processed\n\
2259 recursively and the results are effectively concatenated.\n\
2260For a list whose car is an integer, the cdr of the list is processed\n\
2261 and padded (if the number is positive) or truncated (if negative)\n\
2262 to the width specified by that number.\n\
2263A string is printed verbatim in the mode line except for %-constructs:\n\
2264 (%-constructs are allowed when the string is the entire mode-line-format\n\
2265 or when it is found in a cons-cell or a list)\n\
2266 %b -- print buffer name. %f -- print visited file name.\n\
2267 %* -- print *, % or hyphen. %m -- print value of mode-name (obsolete).\n\
a97c374a 2268 %s -- print process status. %l -- print the current line number.\n\
1ab256cb
RM
2269 %p -- print percent of buffer above top of window, or top, bot or all.\n\
2270 %n -- print Narrow if appropriate.\n\
2271 %[ -- print one [ for each recursive editing level. %] similar.\n\
2272 %% -- print %. %- -- print infinitely many dashes.\n\
2273Decimal digits after the % specify field width to which to pad.");
2274*/
2275
2276 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
2277 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
2278nil here means use current buffer's major mode.");
2279
2280 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
0fa3ba92 2281 make_number (Lisp_Symbol),
1ab256cb
RM
2282 "Symbol for current buffer's major mode.");
2283
2284 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
0fa3ba92 2285 make_number (Lisp_String),
1ab256cb
RM
2286 "Pretty name of current buffer's major mode (a string).");
2287
0fa3ba92 2288 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
1ab256cb
RM
2289 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
2290Automatically becomes buffer-local when set in any fashion.");
2291
2292 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
0fa3ba92 2293 Qnil,
1ab256cb
RM
2294 "*Non-nil if searches should ignore case.\n\
2295Automatically becomes buffer-local when set in any fashion.");
2296
2297 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
0fa3ba92 2298 make_number (Lisp_Int),
1ab256cb
RM
2299 "*Column beyond which automatic line-wrapping should happen.\n\
2300Automatically becomes buffer-local when set in any fashion.");
2301
2302 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
0fa3ba92 2303 make_number (Lisp_Int),
1ab256cb
RM
2304 "*Column for the default indent-line-function to indent to.\n\
2305Linefeed indents to this column in Fundamental mode.\n\
2306Automatically becomes buffer-local when set in any fashion.");
2307
2308 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
0fa3ba92 2309 make_number (Lisp_Int),
1ab256cb
RM
2310 "*Distance between tab stops (for display of tab characters), in columns.\n\
2311Automatically becomes buffer-local when set in any fashion.");
2312
0fa3ba92 2313 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
1ab256cb
RM
2314 "*Non-nil means display control chars with uparrow.\n\
2315Nil means use backslash and octal digits.\n\
2316Automatically becomes buffer-local when set in any fashion.\n\
2317This variable does not apply to characters whose display is specified\n\
2318in the current display table (if there is one).");
2319
0fa3ba92 2320 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
1ab256cb
RM
2321 "*Non-nil means do not display continuation lines;\n\
2322give each line of text one screen line.\n\
2323Automatically becomes buffer-local when set in any fashion.\n\
2324\n\
2325Note that this is overridden by the variable\n\
2326`truncate-partial-width-windows' if that variable is non-nil\n\
502b9b64 2327and this buffer is not full-frame width.");
1ab256cb
RM
2328
2329 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
0fa3ba92 2330 make_number (Lisp_String),
1ab256cb
RM
2331 "Name of default directory of current buffer. Should end with slash.\n\
2332Each buffer has its own value of this variable.");
2333
2334 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
0fa3ba92 2335 Qnil,
1ab256cb
RM
2336 "Function called (if non-nil) to perform auto-fill.\n\
2337It is called after self-inserting a space at a column beyond `fill-column'.\n\
2338Each buffer has its own value of this variable.\n\
2339NOTE: This variable is not an ordinary hook;\n\
2340It may not be a list of functions.");
2341
2342 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
0fa3ba92 2343 make_number (Lisp_String),
1ab256cb
RM
2344 "Name of file visited in current buffer, or nil if not visiting a file.\n\
2345Each buffer has its own value of this variable.");
2346
2347 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
3f5fcd47 2348 &current_buffer->auto_save_file_name,
0fa3ba92 2349 make_number (Lisp_String),
1ab256cb
RM
2350 "Name of file for auto-saving current buffer,\n\
2351or nil if buffer should not be auto-saved.\n\
2352Each buffer has its own value of this variable.");
2353
0fa3ba92 2354 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
1ab256cb
RM
2355 "Non-nil if this buffer is read-only.\n\
2356Each buffer has its own value of this variable.");
2357
0fa3ba92 2358 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
1ab256cb
RM
2359 "Non-nil if this buffer's file has been backed up.\n\
2360Backing up is done before the first time the file is saved.\n\
2361Each buffer has its own value of this variable.");
2362
2363 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
0fa3ba92 2364 make_number (Lisp_Int),
1ab256cb
RM
2365 "Length of current buffer when last read in, saved or auto-saved.\n\
23660 initially.\n\
2367Each buffer has its own value of this variable.");
2368
2369 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
0fa3ba92 2370 Qnil,
1ab256cb
RM
2371 "Non-nil enables selective display:\n\
2372Integer N as value means display only lines\n\
2373 that start with less than n columns of space.\n\
2374A value of t means, after a ^M, all the rest of the line is invisible.\n\
2375 Then ^M's in the file are written into files as newlines.\n\n\
2376Automatically becomes buffer-local when set in any fashion.");
2377
2378#ifndef old
2379 DEFVAR_PER_BUFFER ("selective-display-ellipses",
2380 &current_buffer->selective_display_ellipses,
0fa3ba92 2381 Qnil,
1ab256cb
RM
2382 "t means display ... on previous line when a line is invisible.\n\
2383Automatically becomes buffer-local when set in any fashion.");
2384#endif
2385
0fa3ba92 2386 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
1ab256cb 2387 "Non-nil if self-insertion should replace existing text.\n\
6bbb0d4a
JB
2388If non-nil and not `overwrite-mode-binary', self-insertion still\n\
2389inserts at the end of a line, and inserts when point is before a tab,\n\
2e94b813 2390until the tab is filled in.\n\
6bbb0d4a 2391If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
1ab256cb
RM
2392Automatically becomes buffer-local when set in any fashion.");
2393
54939090
RS
2394#if 0 /* The doc string is too long for some compilers,
2395 but make-docfile can find it in this comment. */
1ab256cb 2396 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
5d305367 2397 Qnil,
1ab256cb
RM
2398 "Display table that controls display of the contents of current buffer.\n\
2399Automatically becomes buffer-local when set in any fashion.\n\
2400The display table is a vector created with `make-display-table'.\n\
2401The first 256 elements control how to display each possible text character.\n\
6158b3b0 2402Each value should be a vector of characters or nil;\n\
1ab256cb 2403nil means display the character in the default fashion.\n\
6158b3b0
JB
2404The remaining five elements control the display of\n\
2405 the end of a truncated screen line (element 256, a single character);\n\
2406 the end of a continued line (element 257, a single character);\n\
2407 the escape character used to display character codes in octal\n\
2408 (element 258, a single character);\n\
2409 the character used as an arrow for control characters (element 259,\n\
2410 a single character);\n\
2411 the decoration indicating the presence of invisible lines (element 260,\n\
2412 a vector of characters).\n\
1ab256cb
RM
2413If this variable is nil, the value of `standard-display-table' is used.\n\
2414Each window can have its own, overriding display table.");
54939090
RS
2415#endif
2416 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
2417 Qnil, "");
1ab256cb 2418
1ab256cb
RM
2419/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
2420 "Don't ask.");
2421*/
01050cb5 2422 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
1ab256cb
RM
2423 "Function to call before each text change.\n\
2424Two arguments are passed to the function: the positions of\n\
2425the beginning and end of the range of old text to be changed.\n\
2426\(For an insertion, the beginning and end are at the same place.)\n\
2427No information is given about the length of the text after the change.\n\
2428position of the change\n\
2429\n\
2430While executing the `before-change-function', changes to buffers do not\n\
2431cause calls to any `before-change-function' or `after-change-function'.");
2432 Vbefore_change_function = Qnil;
2433
2434 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
2435 "Function to call after each text change.\n\
2436Three arguments are passed to the function: the positions of\n\
2437the beginning and end of the range of changed text,\n\
2438and the length of the pre-change text replaced by that range.\n\
2439\(For an insertion, the pre-change length is zero;\n\
2440for a deletion, that length is the number of characters deleted,\n\
2441and the post-change beginning and end are at the same place.)\n\
2442\n\
2443While executing the `after-change-function', changes to buffers do not\n\
2444cause calls to any `before-change-function' or `after-change-function'.");
2445 Vafter_change_function = Qnil;
2446
dbc4e1c1
JB
2447 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
2448 "A list of functions to call before changing a buffer which is unmodified.\n\
2449The functions are run using the `run-hooks' function.");
2450 Vfirst_change_hook = Qnil;
2451 Qfirst_change_hook = intern ("first-change-hook");
2452 staticpro (&Qfirst_change_hook);
1ab256cb 2453
54939090
RS
2454#if 0 /* The doc string is too long for some compilers,
2455 but make-docfile can find it in this comment. */
3f5fcd47 2456 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
1ab256cb
RM
2457 "List of undo entries in current buffer.\n\
2458Recent changes come first; older changes follow newer.\n\
2459\n\
2460An entry (START . END) represents an insertion which begins at\n\
2461position START and ends at position END.\n\
2462\n\
2463An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
2464from (abs POSITION). If POSITION is positive, point was at the front\n\
2465of the text being deleted; if negative, point was at the end.\n\
2466\n\
2467An entry (t HIGHWORD LOWWORD) indicates that the buffer had been\n\
2468previously unmodified. HIGHWORD and LOWWORD are the high and low\n\
246916-bit words of the buffer's modification count at the time. If the\n\
2470modification count of the most recent save is different, this entry is\n\
2471obsolete.\n\
2472\n\
483c1fd3
RS
2473An entry (nil PROP VAL BEG . END) indicates that a text property\n\
2474was modified between BEG and END. PROP is the property name,\n\
2475and VAL is the old value.\n\
2476\n\
bec44fd6
JB
2477An entry of the form POSITION indicates that point was at the buffer\n\
2478location given by the integer. Undoing an entry of this form places\n\
2479point at POSITION.\n\
2480\n\
1ab256cb
RM
2481nil marks undo boundaries. The undo command treats the changes\n\
2482between two undo boundaries as a single step to be undone.\n\
2483\n\
bec44fd6 2484If the value of the variable is t, undo information is not recorded.");
54939090
RS
2485#endif
2486 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
2487 "");
1ab256cb 2488
c48f61ef
RS
2489 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
2490 "Non-nil means the mark and region are currently active in this buffer.\n\
2491Automatically local in all buffers.");
2492
2493 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
2494 "*Non-nil means deactivate the mark when the buffer contents change.");
2495 Vtransient_mark_mode = Qnil;
2496
0a4469c9 2497 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
a96b68f1
RS
2498 "*Non-nil means disregard read-only status of buffers or characters.\n\
2499If the value is t, disregard `buffer-read-only' and all `read-only'\n\
2500text properties. If the value is a list, disregard `buffer-read-only'\n\
2501and disregard a `read-only' text property if the property value\n\
2502is a member of the list.");
2503 Vinhibit_read_only = Qnil;
2504
1ab256cb
RM
2505 defsubr (&Sbuffer_list);
2506 defsubr (&Sget_buffer);
2507 defsubr (&Sget_file_buffer);
2508 defsubr (&Sget_buffer_create);
01050cb5 2509 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
2510 defsubr (&Sbuffer_name);
2511/*defsubr (&Sbuffer_number);*/
2512 defsubr (&Sbuffer_file_name);
2513 defsubr (&Sbuffer_local_variables);
2514 defsubr (&Sbuffer_modified_p);
2515 defsubr (&Sset_buffer_modified_p);
2516 defsubr (&Sbuffer_modified_tick);
2517 defsubr (&Srename_buffer);
2518 defsubr (&Sother_buffer);
2519 defsubr (&Sbuffer_disable_undo);
2520 defsubr (&Sbuffer_enable_undo);
2521 defsubr (&Skill_buffer);
2522 defsubr (&Serase_buffer);
2523 defsubr (&Sswitch_to_buffer);
2524 defsubr (&Spop_to_buffer);
2525 defsubr (&Scurrent_buffer);
2526 defsubr (&Sset_buffer);
2527 defsubr (&Sbarf_if_buffer_read_only);
2528 defsubr (&Sbury_buffer);
2529 defsubr (&Slist_buffers);
2530 defsubr (&Skill_all_local_variables);
2eec3b4e 2531
52f8ec73 2532 defsubr (&Soverlayp);
2eec3b4e
RS
2533 defsubr (&Smake_overlay);
2534 defsubr (&Sdelete_overlay);
2535 defsubr (&Smove_overlay);
8ebafa8d
JB
2536 defsubr (&Soverlay_start);
2537 defsubr (&Soverlay_end);
2538 defsubr (&Soverlay_buffer);
2539 defsubr (&Soverlay_properties);
2eec3b4e
RS
2540 defsubr (&Soverlays_at);
2541 defsubr (&Snext_overlay_change);
2542 defsubr (&Soverlay_recenter);
2543 defsubr (&Soverlay_lists);
2544 defsubr (&Soverlay_get);
2545 defsubr (&Soverlay_put);
1ab256cb
RM
2546}
2547
2548keys_of_buffer ()
2549{
2550 initial_define_key (control_x_map, 'b', "switch-to-buffer");
2551 initial_define_key (control_x_map, 'k', "kill-buffer");
2552 initial_define_key (control_x_map, Ctl ('B'), "list-buffers");
2553}