*** empty log message ***
[bpt/emacs.git] / src / buffer.c
CommitLineData
1ab256cb 1/* Buffer manipulation primitives for GNU Emacs.
8cf9a8c6 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
1ab256cb
RM
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <sys/param.h>
22
23#ifndef MAXPATHLEN
24/* in 4.1, param.h fails to define this. */
25#define MAXPATHLEN 1024
26#endif /* not MAXPATHLEN */
27
1ab256cb
RM
28#include "config.h"
29#include "lisp.h"
30#include "window.h"
31#include "commands.h"
32#include "buffer.h"
33#include "syntax.h"
34#include "indent.h"
35
36struct buffer *current_buffer; /* the current buffer */
37
38/* First buffer in chain of all buffers (in reverse order of creation).
39 Threaded through ->next. */
40
41struct buffer *all_buffers;
42
43/* This structure holds the default values of the buffer-local variables
44 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
45 The default value occupies the same slot in this structure
46 as an individual buffer's value occupies in that buffer.
47 Setting the default value also goes through the alist of buffers
48 and stores into each buffer that does not say it has a local value. */
49
50struct buffer buffer_defaults;
51
52/* A Lisp_Object pointer to the above, used for staticpro */
53
54static Lisp_Object Vbuffer_defaults;
55
56/* This structure marks which slots in a buffer have corresponding
57 default values in buffer_defaults.
58 Each such slot has a nonzero value in this structure.
59 The value has only one nonzero bit.
60
61 When a buffer has its own local value for a slot,
62 the bit for that slot (found in the same slot in this structure)
63 is turned on in the buffer's local_var_flags slot.
64
65 If a slot in this structure is -1, then even though there may
66 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
67 and the corresponding slot in buffer_defaults is not used.
68
69 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
70 but there is a default value which is copied into each buffer.
71
72 If a slot in this structure is negative, then even though there may
73 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
74 and the corresponding slot in buffer_defaults is not used.
75
76 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
77 zero, that is a bug */
78
79struct buffer buffer_local_flags;
80
81/* This structure holds the names of symbols whose values may be
82 buffer-local. It is indexed and accessed in the same way as the above. */
83
84struct buffer buffer_local_symbols;
85/* A Lisp_Object pointer to the above, used for staticpro */
86static Lisp_Object Vbuffer_local_symbols;
87
88/* Nonzero means don't allow modification of protected fields. */
89
90int check_protected_fields;
91
92Lisp_Object Fset_buffer ();
01050cb5 93void set_buffer_internal ();
1ab256cb
RM
94
95/* Alist of all buffer names vs the buffers. */
96/* This used to be a variable, but is no longer,
97 to prevent lossage due to user rplac'ing this alist or its elements. */
98Lisp_Object Vbuffer_alist;
99
100/* Functions to call before and after each text change. */
101Lisp_Object Vbefore_change_function;
102Lisp_Object Vafter_change_function;
103
104/* Function to call before changing an unmodified buffer. */
105Lisp_Object Vfirst_change_function;
106
107Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
108
109Lisp_Object Qprotected_field;
110
111Lisp_Object QSFundamental; /* A string "Fundamental" */
112
113Lisp_Object Qkill_buffer_hook;
114
115/* For debugging; temporary. See set_buffer_internal. */
116/* Lisp_Object Qlisp_mode, Vcheck_symbol; */
117
118nsberror (spec)
119 Lisp_Object spec;
120{
121 if (XTYPE (spec) == Lisp_String)
122 error ("No buffer named %s", XSTRING (spec)->data);
123 error ("Invalid buffer argument");
124}
125\f
126DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 0, 0,
127 "Return a list of all existing live buffers.")
128 ()
129{
130 return Fmapcar (Qcdr, Vbuffer_alist);
131}
132
133DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
134 "Return the buffer named NAME (a string).\n\
135If there is no live buffer named NAME, return nil.\n\
136NAME may also be a buffer; if so, the value is that buffer.")
137 (name)
138 register Lisp_Object name;
139{
140 if (XTYPE (name) == Lisp_Buffer)
141 return name;
142 CHECK_STRING (name, 0);
143
144 return Fcdr (Fassoc (name, Vbuffer_alist));
145}
146
147DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
148 "Return the buffer visiting file FILENAME (a string).\n\
149If there is no such live buffer, return nil.")
150 (filename)
151 register Lisp_Object filename;
152{
153 register Lisp_Object tail, buf, tem;
154 CHECK_STRING (filename, 0);
155 filename = Fexpand_file_name (filename, Qnil);
156
157 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
158 {
159 buf = Fcdr (XCONS (tail)->car);
160 if (XTYPE (buf) != Lisp_Buffer) continue;
161 if (XTYPE (XBUFFER (buf)->filename) != Lisp_String) continue;
162 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
265a9e55 163 if (!NILP (tem))
1ab256cb
RM
164 return buf;
165 }
166 return Qnil;
167}
168
169/* Incremented for each buffer created, to assign the buffer number. */
170int buffer_count;
171
172DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
173 "Return the buffer named NAME, or create such a buffer and return it.\n\
174A new buffer is created if there is no live buffer named NAME.\n\
175If NAME is a buffer instead of a string, then it is the value returned.\n\
176The value is never nil.")
177 (name)
178 register Lisp_Object name;
179{
180 register Lisp_Object buf, function, tem;
181 int count = specpdl_ptr - specpdl;
182 register struct buffer *b;
183
184 buf = Fget_buffer (name);
265a9e55 185 if (!NILP (buf))
1ab256cb
RM
186 return buf;
187
188 b = (struct buffer *) malloc (sizeof (struct buffer));
189 if (!b)
190 memory_full ();
191
192 BUF_GAP_SIZE (b) = 20;
193 BUFFER_ALLOC (BUF_BEG_ADDR (b), BUF_GAP_SIZE (b));
194 if (! BUF_BEG_ADDR (b))
195 memory_full ();
196
197 BUF_PT (b) = 1;
198 BUF_GPT (b) = 1;
199 BUF_BEGV (b) = 1;
200 BUF_ZV (b) = 1;
201 BUF_Z (b) = 1;
202 BUF_MODIFF (b) = 1;
203
204 /* Put this on the chain of all buffers including killed ones. */
205 b->next = all_buffers;
206 all_buffers = b;
207
208 b->mark = Fmake_marker ();
209 /*b->number = make_number (++buffer_count);*/
210 b->name = name;
211 if (XSTRING (name)->data[0] != ' ')
212 b->undo_list = Qnil;
213 else
214 b->undo_list = Qt;
215
216 reset_buffer (b);
217
218 /* Put this in the alist of all live buffers. */
219 XSET (buf, Lisp_Buffer, b);
220 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
221
222 b->mark = Fmake_marker ();
223 b->markers = Qnil;
224 b->name = name;
225
226 function = buffer_defaults.major_mode;
265a9e55 227 if (NILP (function))
1ab256cb
RM
228 {
229 tem = Fget (current_buffer->major_mode, Qmode_class);
230 if (EQ (tem, Qnil))
231 function = current_buffer->major_mode;
232 }
233
265a9e55 234 if (NILP (function) || EQ (function, Qfundamental_mode))
1ab256cb
RM
235 return buf;
236
237 /* To select a nonfundamental mode,
238 select the buffer temporarily and then call the mode function. */
239
240 record_unwind_protect (save_excursion_restore, save_excursion_save ());
241
242 Fset_buffer (buf);
243 call0 (function);
244
245 return unbind_to (count, buf);
246}
247
248/* Reinitialize everything about a buffer except its name and contents. */
249
250void
251reset_buffer (b)
252 register struct buffer *b;
253{
254 b->filename = Qnil;
255 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
256 b->modtime = 0;
257 b->save_modified = 1;
258 b->save_length = 0;
259 b->last_window_start = 1;
260 b->backed_up = Qnil;
261 b->auto_save_modified = 0;
262 b->auto_save_file_name = Qnil;
263 b->read_only = Qnil;
264 b->fieldlist = Qnil;
265 reset_buffer_local_variables(b);
266}
267
268reset_buffer_local_variables(b)
269 register struct buffer *b;
270{
271 register int offset;
272
273 /* Reset the major mode to Fundamental, together with all the
274 things that depend on the major mode.
275 default-major-mode is handled at a higher level.
276 We ignore it here. */
277 b->major_mode = Qfundamental_mode;
278 b->keymap = Qnil;
279 b->abbrev_table = Vfundamental_mode_abbrev_table;
280 b->mode_name = QSFundamental;
281 b->minor_modes = Qnil;
282 b->downcase_table = Vascii_downcase_table;
283 b->upcase_table = Vascii_upcase_table;
284 b->case_canon_table = Vascii_downcase_table;
285 b->case_eqv_table = Vascii_upcase_table;
286#if 0
287 b->sort_table = XSTRING (Vascii_sort_table);
288 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
289#endif /* 0 */
290
291 /* Reset all per-buffer variables to their defaults. */
292 b->local_var_alist = Qnil;
293 b->local_var_flags = 0;
294
295 /* For each slot that has a default value,
296 copy that into the slot. */
297
298 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
299 offset < sizeof (struct buffer);
300 offset += sizeof (Lisp_Object)) /* sizeof int == sizeof Lisp_Object */
301 if (*(int *)(offset + (char *) &buffer_local_flags) > 0
302 || *(int *)(offset + (char *) &buffer_local_flags) == -2)
303 *(Lisp_Object *)(offset + (char *)b) =
304 *(Lisp_Object *)(offset + (char *)&buffer_defaults);
305}
306
01050cb5
RM
307/* We split this away from generate-new-buffer, because rename-buffer
308 and set-visited-file-name ought to be able to use this to really
309 rename the buffer properly. */
310
311DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
1ab256cb 312 1, 1, 0,
01050cb5
RM
313 "Return a string that is the name of no existing buffer based on NAME.\n\
314If there is no live buffer named NAME, then return NAME.\n\
1ab256cb 315Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
01050cb5 316until an unused name is found, and then return that name.")
1ab256cb
RM
317 (name)
318 register Lisp_Object name;
319{
320 register Lisp_Object gentemp, tem;
321 int count;
322 char number[10];
323
324 CHECK_STRING (name, 0);
325
326 tem = Fget_buffer (name);
265a9e55 327 if (NILP (tem))
01050cb5 328 return name;
1ab256cb
RM
329
330 count = 1;
331 while (1)
332 {
333 sprintf (number, "<%d>", ++count);
334 gentemp = concat2 (name, build_string (number));
335 tem = Fget_buffer (gentemp);
265a9e55 336 if (NILP (tem))
01050cb5 337 return gentemp;
1ab256cb
RM
338 }
339}
340
341\f
342DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
343 "Return the name of BUFFER, as a string.\n\
01050cb5 344With no argument or nil as argument, return the name of the current buffer.")
1ab256cb
RM
345 (buffer)
346 register Lisp_Object buffer;
347{
265a9e55 348 if (NILP (buffer))
1ab256cb
RM
349 return current_buffer->name;
350 CHECK_BUFFER (buffer, 0);
351 return XBUFFER (buffer)->name;
352}
353
354DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
355 "Return name of file BUFFER is visiting, or nil if none.\n\
356No argument or nil as argument means use the current buffer.")
357 (buffer)
358 register Lisp_Object buffer;
359{
265a9e55 360 if (NILP (buffer))
1ab256cb
RM
361 return current_buffer->filename;
362 CHECK_BUFFER (buffer, 0);
363 return XBUFFER (buffer)->filename;
364}
365
366DEFUN ("buffer-local-variables", Fbuffer_local_variables,
367 Sbuffer_local_variables, 0, 1, 0,
368 "Return an alist of variables that are buffer-local in BUFFER.\n\
369Each element looks like (SYMBOL . VALUE) and describes one variable.\n\
370Note that storing new VALUEs in these elements doesn't change the variables.\n\
371No argument or nil as argument means use current buffer as BUFFER.")
372 (buffer)
373 register Lisp_Object buffer;
374{
375 register struct buffer *buf;
376 register Lisp_Object val;
377
265a9e55 378 if (NILP (buffer))
1ab256cb
RM
379 buf = current_buffer;
380 else
381 {
382 CHECK_BUFFER (buffer, 0);
383 buf = XBUFFER (buffer);
384 }
385
386 {
387 /* Reference each variable in the alist in our current buffer.
388 If inquiring about the current buffer, this gets the current values,
389 so store them into the alist so the alist is up to date.
390 If inquiring about some other buffer, this swaps out any values
391 for that buffer, making the alist up to date automatically. */
392 register Lisp_Object tem;
393 for (tem = buf->local_var_alist; CONSP (tem); tem = XCONS (tem)->cdr)
394 {
395 Lisp_Object v1 = Fsymbol_value (XCONS (XCONS (tem)->car)->car);
396 if (buf == current_buffer)
397 XCONS (XCONS (tem)->car)->cdr = v1;
398 }
399 }
400
401 /* Make a copy of the alist, to return it. */
402 val = Fcopy_alist (buf->local_var_alist);
403
404 /* Add on all the variables stored in special slots. */
405 {
406 register int offset, mask;
407
408 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
409 offset < sizeof (struct buffer);
410 offset += (sizeof (int))) /* sizeof int == sizeof Lisp_Object */
411 {
412 mask = *(int *)(offset + (char *) &buffer_local_flags);
413 if (mask == -1 || (buf->local_var_flags & mask))
414 if (XTYPE (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols))
415 == Lisp_Symbol)
416 val = Fcons (Fcons (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols),
417 *(Lisp_Object *)(offset + (char *)buf)),
418 val);
419 }
420 }
421 return (val);
422}
423
424\f
425DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
426 0, 1, 0,
427 "Return t if BUFFER was modified since its file was last read or saved.\n\
428No argument or nil as argument means use current buffer as BUFFER.")
429 (buffer)
430 register Lisp_Object buffer;
431{
432 register struct buffer *buf;
265a9e55 433 if (NILP (buffer))
1ab256cb
RM
434 buf = current_buffer;
435 else
436 {
437 CHECK_BUFFER (buffer, 0);
438 buf = XBUFFER (buffer);
439 }
440
441 return buf->save_modified < BUF_MODIFF (buf) ? Qt : Qnil;
442}
443
444DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
445 1, 1, 0,
446 "Mark current buffer as modified or unmodified according to FLAG.\n\
447A non-nil FLAG means mark the buffer modified.")
448 (flag)
449 register Lisp_Object flag;
450{
451 register int already;
452 register Lisp_Object fn;
453
454#ifdef CLASH_DETECTION
455 /* If buffer becoming modified, lock the file.
456 If buffer becoming unmodified, unlock the file. */
457
458 fn = current_buffer->filename;
265a9e55 459 if (!NILP (fn))
1ab256cb
RM
460 {
461 already = current_buffer->save_modified < MODIFF;
265a9e55 462 if (!already && !NILP (flag))
1ab256cb 463 lock_file (fn);
265a9e55 464 else if (already && NILP (flag))
1ab256cb
RM
465 unlock_file (fn);
466 }
467#endif /* CLASH_DETECTION */
468
265a9e55 469 current_buffer->save_modified = NILP (flag) ? MODIFF : 0;
1ab256cb
RM
470 update_mode_lines++;
471 return flag;
472}
473
474DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
475 0, 1, 0,
476 "Return BUFFER's tick counter, incremented for each change in text.\n\
477Each buffer has a tick counter which is incremented each time the text in\n\
478that buffer is changed. It wraps around occasionally.\n\
479No argument or nil as argument means use current buffer as BUFFER.")
480 (buffer)
481 register Lisp_Object buffer;
482{
483 register struct buffer *buf;
265a9e55 484 if (NILP (buffer))
1ab256cb
RM
485 buf = current_buffer;
486 else
487 {
488 CHECK_BUFFER (buffer, 0);
489 buf = XBUFFER (buffer);
490 }
491
492 return make_number (BUF_MODIFF (buf));
493}
494\f
01050cb5 495DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
1ab256cb
RM
496 "sRename buffer (to new name): ",
497 "Change current buffer's name to NEWNAME (a string).\n\
01050cb5
RM
498If second arg DISTINGUISH is nil or omitted, it is an error if a\n\
499buffer named NEWNAME already exists.\n\
500If DISTINGUISH is non-nil, come up with a new name using\n\
501`generate-new-buffer-name'.\n\
502Return the name we actually gave the buffer.\n\
1ab256cb 503This does not change the name of the visited file (if any).")
01050cb5
RM
504 (name, distinguish)
505 register Lisp_Object name, distinguish;
1ab256cb
RM
506{
507 register Lisp_Object tem, buf;
508
509 CHECK_STRING (name, 0);
510 tem = Fget_buffer (name);
01050cb5
RM
511 if (XBUFFER (tem) == current_buffer)
512 return current_buffer->name;
265a9e55 513 if (!NILP (tem))
01050cb5 514 {
265a9e55 515 if (!NILP (distinguish))
01050cb5
RM
516 name = Fgenerate_new_buffer_name (name);
517 else
518 error ("Buffer name \"%s\" is in use", XSTRING (name)->data);
519 }
1ab256cb
RM
520
521 current_buffer->name = name;
522 XSET (buf, Lisp_Buffer, current_buffer);
523 Fsetcar (Frassq (buf, Vbuffer_alist), name);
265a9e55 524 if (NILP (current_buffer->filename) && !NILP (current_buffer->auto_save_file_name))
1ab256cb 525 call0 (intern ("rename-auto-save-file"));
01050cb5 526 return name;
1ab256cb
RM
527}
528
529DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 1, 0,
530 "Return most recently selected buffer other than BUFFER.\n\
531Buffers not visible in windows are preferred to visible buffers.\n\
532If no other buffer exists, the buffer `*scratch*' is returned.\n\
533If BUFFER is omitted or nil, some interesting buffer is returned.")
534 (buffer)
535 register Lisp_Object buffer;
536{
537 register Lisp_Object tail, buf, notsogood, tem;
538 notsogood = Qnil;
539
265a9e55 540 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
541 {
542 buf = Fcdr (Fcar (tail));
543 if (EQ (buf, buffer))
544 continue;
545 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
546 continue;
547 tem = Fget_buffer_window (buf, Qnil);
265a9e55 548 if (NILP (tem))
1ab256cb 549 return buf;
265a9e55 550 if (NILP (notsogood))
1ab256cb
RM
551 notsogood = buf;
552 }
265a9e55 553 if (!NILP (notsogood))
1ab256cb
RM
554 return notsogood;
555 return Fget_buffer_create (build_string ("*scratch*"));
556}
557\f
558DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo, 1,1,
5590,
560 "Make BUFFER stop keeping undo information.")
ffd56f97
JB
561 (buffer)
562 register Lisp_Object buffer;
1ab256cb 563{
ffd56f97
JB
564 Lisp_Object real_buffer;
565
566 if (NILP (buffer))
567 XSET (real_buffer, Lisp_Buffer, current_buffer);
568 else
569 {
570 real_buffer = Fget_buffer (buffer);
571 if (NILP (real_buffer))
572 nsberror (buffer);
573 }
574
575 XBUFFER (real_buffer)->undo_list = Qt;
576
1ab256cb
RM
577 return Qnil;
578}
579
580DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
581 0, 1, "",
582 "Start keeping undo information for buffer BUFFER.\n\
583No argument or nil as argument means do this for the current buffer.")
ffd56f97
JB
584 (buffer)
585 register Lisp_Object buffer;
1ab256cb 586{
ffd56f97 587 Lisp_Object real_buffer;
1ab256cb 588
ffd56f97
JB
589 if (NILP (buffer))
590 XSET (real_buffer, Lisp_Buffer, current_buffer);
1ab256cb
RM
591 else
592 {
ffd56f97
JB
593 real_buffer = Fget_buffer (buffer);
594 if (NILP (real_buffer))
595 nsberror (buffer);
1ab256cb
RM
596 }
597
ffd56f97
JB
598 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
599 XBUFFER (real_buffer)->undo_list = Qnil;
1ab256cb
RM
600
601 return Qnil;
602}
603
604/*
605 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
606Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
607The buffer being killed will be current while the hook is running.\n\
608See `kill-buffer'."
609 */
610DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
611 "Kill the buffer BUFFER.\n\
612The argument may be a buffer or may be the name of a buffer.\n\
613An argument of nil means kill the current buffer.\n\n\
614Value is t if the buffer is actually killed, nil if user says no.\n\n\
615The value of `kill-buffer-hook' (which may be local to that buffer),\n\
616if not void, is a list of functions to be called, with no arguments,\n\
617before the buffer is actually killed. The buffer to be killed is current\n\
618when the hook functions are called.\n\n\
619Any processes that have this buffer as the `process-buffer' are killed\n\
620with `delete-process'.")
621 (bufname)
622 Lisp_Object bufname;
623{
624 Lisp_Object buf;
625 register struct buffer *b;
626 register Lisp_Object tem;
627 register struct Lisp_Marker *m;
628 struct gcpro gcpro1, gcpro2;
629
265a9e55 630 if (NILP (bufname))
1ab256cb
RM
631 buf = Fcurrent_buffer ();
632 else
633 buf = Fget_buffer (bufname);
265a9e55 634 if (NILP (buf))
1ab256cb
RM
635 nsberror (bufname);
636
637 b = XBUFFER (buf);
638
639 /* Query if the buffer is still modified. */
265a9e55 640 if (INTERACTIVE && !NILP (b->filename)
1ab256cb
RM
641 && BUF_MODIFF (b) > b->save_modified)
642 {
643 GCPRO2 (buf, bufname);
644 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
645 XSTRING (b->name)->data));
646 UNGCPRO;
265a9e55 647 if (NILP (tem))
1ab256cb
RM
648 return Qnil;
649 }
650
651 /* Run kill-buffer hook with the buffer to be killed the current buffer. */
652 {
653 register Lisp_Object val;
654 int count = specpdl_ptr - specpdl;
655
656 record_unwind_protect (save_excursion_restore, save_excursion_save ());
657 set_buffer_internal (b);
658 call1 (Vrun_hooks, Qkill_buffer_hook);
659 unbind_to (count, Qnil);
660 }
661
662 /* We have no more questions to ask. Verify that it is valid
663 to kill the buffer. This must be done after the questions
664 since anything can happen within do_yes_or_no_p. */
665
666 /* Don't kill the minibuffer now current. */
667 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
668 return Qnil;
669
265a9e55 670 if (NILP (b->name))
1ab256cb
RM
671 return Qnil;
672
673 /* Make this buffer not be current.
674 In the process, notice if this is the sole visible buffer
675 and give up if so. */
676 if (b == current_buffer)
677 {
678 tem = Fother_buffer (buf);
679 Fset_buffer (tem);
680 if (b == current_buffer)
681 return Qnil;
682 }
683
684 /* Now there is no question: we can kill the buffer. */
685
686#ifdef CLASH_DETECTION
687 /* Unlock this buffer's file, if it is locked. */
688 unlock_buffer (b);
689#endif /* CLASH_DETECTION */
690
1ab256cb 691 kill_buffer_processes (buf);
1ab256cb
RM
692
693 tem = Vinhibit_quit;
694 Vinhibit_quit = Qt;
695 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
696 Freplace_buffer_in_windows (buf);
697 Vinhibit_quit = tem;
698
699 /* Delete any auto-save file. */
700 if (XTYPE (b->auto_save_file_name) == Lisp_String)
701 {
702 Lisp_Object tem;
703 tem = Fsymbol_value (intern ("delete-auto-save-files"));
265a9e55 704 if (! NILP (tem))
1ab256cb
RM
705 unlink (XSTRING (b->auto_save_file_name)->data);
706 }
707
708 /* Unchain all markers of this buffer
709 and leave them pointing nowhere. */
710 for (tem = b->markers; !EQ (tem, Qnil); )
711 {
712 m = XMARKER (tem);
713 m->buffer = 0;
714 tem = m->chain;
715 m->chain = Qnil;
716 }
717 b->markers = Qnil;
718
719 b->name = Qnil;
720 BUFFER_FREE (BUF_BEG_ADDR (b));
721 b->undo_list = Qnil;
722
723 return Qt;
724}
725\f
36a8c287
JB
726/* Move the assoc for buffer BUF to the front of buffer-alist. Since
727 we do this each time BUF is selected visibly, the more recently
728 selected buffers are always closer to the front of the list. This
729 means that other_buffer is more likely to choose a relevant buffer. */
1ab256cb
RM
730
731record_buffer (buf)
732 Lisp_Object buf;
733{
734 register Lisp_Object link, prev;
735
736 prev = Qnil;
737 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
738 {
739 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
740 break;
741 prev = link;
742 }
743
36a8c287
JB
744 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
745 we cannot use Fdelq itself here because it allows quitting. */
1ab256cb 746
265a9e55 747 if (NILP (prev))
1ab256cb
RM
748 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
749 else
750 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
751
752 XCONS(link)->cdr = Vbuffer_alist;
753 Vbuffer_alist = link;
754}
755
756DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
757 "Select buffer BUFFER in the current window.\n\
758BUFFER may be a buffer or a buffer name.\n\
759Optional second arg NORECORD non-nil means\n\
760do not put this buffer at the front of the list of recently selected ones.\n\
761\n\
762WARNING: This is NOT the way to work on another buffer temporarily\n\
763within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
764the window-buffer correspondences.")
765 (bufname, norecord)
766 Lisp_Object bufname, norecord;
767{
768 register Lisp_Object buf;
769 Lisp_Object tem;
770
771 if (EQ (minibuf_window, selected_window))
772 error ("Cannot switch buffers in minibuffer window");
773 tem = Fwindow_dedicated_p (selected_window);
265a9e55 774 if (!NILP (tem))
1ab256cb
RM
775 error ("Cannot switch buffers in a dedicated window");
776
265a9e55 777 if (NILP (bufname))
1ab256cb
RM
778 buf = Fother_buffer (Fcurrent_buffer ());
779 else
780 buf = Fget_buffer_create (bufname);
781 Fset_buffer (buf);
265a9e55 782 if (NILP (norecord))
1ab256cb
RM
783 record_buffer (buf);
784
785 Fset_window_buffer (EQ (selected_window, minibuf_window)
786 ? Fnext_window (minibuf_window, Qnil) : selected_window,
787 buf);
788
789 return Qnil;
790}
791
792DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 2, 0,
793 "Select buffer BUFFER in some window, preferably a different one.\n\
794If BUFFER is nil, then some other buffer is chosen.\n\
795If `pop-up-windows' is non-nil, windows can be split to do this.\n\
796If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
797window even if BUFFER is already visible in the selected window.")
798 (bufname, other)
799 Lisp_Object bufname, other;
800{
801 register Lisp_Object buf;
265a9e55 802 if (NILP (bufname))
1ab256cb
RM
803 buf = Fother_buffer (Fcurrent_buffer ());
804 else
805 buf = Fget_buffer_create (bufname);
806 Fset_buffer (buf);
807 record_buffer (buf);
808 Fselect_window (Fdisplay_buffer (buf, other));
809 return Qnil;
810}
811
812DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
813 "Return the current buffer as a Lisp object.")
814 ()
815{
816 register Lisp_Object buf;
817 XSET (buf, Lisp_Buffer, current_buffer);
818 return buf;
819}
820\f
821/* Set the current buffer to b */
822
823void
824set_buffer_internal (b)
825 register struct buffer *b;
826{
827 register struct buffer *old_buf;
828 register Lisp_Object tail, valcontents;
829 enum Lisp_Type tem;
830
831 if (current_buffer == b)
832 return;
833
834 windows_or_buffers_changed = 1;
835 old_buf = current_buffer;
836 current_buffer = b;
837 last_known_column_point = -1; /* invalidate indentation cache */
838
839 /* Look down buffer's list of local Lisp variables
840 to find and update any that forward into C variables. */
841
265a9e55 842 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
843 {
844 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
845 if ((XTYPE (valcontents) == Lisp_Buffer_Local_Value
846 || XTYPE (valcontents) == Lisp_Some_Buffer_Local_Value)
847 && (tem = XTYPE (XCONS (valcontents)->car),
848 (tem == Lisp_Boolfwd || tem == Lisp_Intfwd
849 || tem == Lisp_Objfwd)))
850 /* Just reference the variable
851 to cause it to become set for this buffer. */
852 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
853 }
854
855 /* Do the same with any others that were local to the previous buffer */
856
857 if (old_buf)
265a9e55 858 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1ab256cb
RM
859 {
860 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
861 if ((XTYPE (valcontents) == Lisp_Buffer_Local_Value
862 || XTYPE (valcontents) == Lisp_Some_Buffer_Local_Value)
863 && (tem = XTYPE (XCONS (valcontents)->car),
864 (tem == Lisp_Boolfwd || tem == Lisp_Intfwd
865 || tem == Lisp_Objfwd)))
866 /* Just reference the variable
867 to cause it to become set for this buffer. */
868 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
869 }
870}
871
872DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
873 "Make the buffer BUFFER current for editing operations.\n\
874BUFFER may be a buffer or the name of an existing buffer.\n\
875See also `save-excursion' when you want to make a buffer current temporarily.\n\
876This function does not display the buffer, so its effect ends\n\
877when the current command terminates.\n\
878Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
879 (bufname)
880 register Lisp_Object bufname;
881{
882 register Lisp_Object buffer;
883 buffer = Fget_buffer (bufname);
265a9e55 884 if (NILP (buffer))
1ab256cb 885 nsberror (bufname);
265a9e55 886 if (NILP (XBUFFER (buffer)->name))
1ab256cb
RM
887 error ("Selecting deleted buffer");
888 set_buffer_internal (XBUFFER (buffer));
889 return buffer;
890}
891\f
892DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
893 Sbarf_if_buffer_read_only, 0, 0, 0,
894 "Signal a `buffer-read-only' error if the current buffer is read-only.")
895 ()
896{
265a9e55 897 while (!NILP (current_buffer->read_only))
1ab256cb
RM
898 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
899 return Qnil;
900}
901
902DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
903 "Put BUFFER at the end of the list of all buffers.\n\
904There it is the least likely candidate for `other-buffer' to return;\n\
528415e7
RS
905thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
906If the argument is nil, bury the current buffer\n\
907and switch to some other buffer in the selected window.")
1ab256cb
RM
908 (buf)
909 register Lisp_Object buf;
910{
911 register Lisp_Object aelt, link;
912
265a9e55 913 if (NILP (buf))
1ab256cb
RM
914 {
915 XSET (buf, Lisp_Buffer, current_buffer);
916 Fswitch_to_buffer (Fother_buffer (buf), Qnil);
917 }
918 else
919 {
920 Lisp_Object buf1;
921
922 buf1 = Fget_buffer (buf);
265a9e55 923 if (NILP (buf1))
1ab256cb
RM
924 nsberror (buf);
925 buf = buf1;
926 }
927
928 aelt = Frassq (buf, Vbuffer_alist);
929 link = Fmemq (aelt, Vbuffer_alist);
930 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
931 XCONS (link)->cdr = Qnil;
932 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
933 return Qnil;
934}
935\f
936DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, 0,
937 "Delete the entire contents of the current buffer.\n\
938Any clipping restriction in effect (see `narrow-to-buffer') is removed,\n\
939so the buffer is truly empty after this.")
940 ()
941{
942 Fwiden ();
943 del_range (BEG, Z);
944 current_buffer->last_window_start = 1;
945 /* Prevent warnings, or suspension of auto saving, that would happen
946 if future size is less than past size. Use of erase-buffer
947 implies that the future text is not really related to the past text. */
948 XFASTINT (current_buffer->save_length) = 0;
949 return Qnil;
950}
951
952validate_region (b, e)
953 register Lisp_Object *b, *e;
954{
955 register int i;
956
957 CHECK_NUMBER_COERCE_MARKER (*b, 0);
958 CHECK_NUMBER_COERCE_MARKER (*e, 1);
959
960 if (XINT (*b) > XINT (*e))
961 {
962 i = XFASTINT (*b); /* This is legit even if *b is < 0 */
963 *b = *e;
964 XFASTINT (*e) = i; /* because this is all we do with i. */
965 }
966
967 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
968 && XINT (*e) <= ZV))
969 args_out_of_range (*b, *e);
970}
971\f
972Lisp_Object
973list_buffers_1 (files)
974 Lisp_Object files;
975{
976 register Lisp_Object tail, tem, buf;
977 Lisp_Object col1, col2, col3, minspace;
978 register struct buffer *old = current_buffer, *b;
979 int desired_point = 0;
980 Lisp_Object other_file_symbol;
981
982 other_file_symbol = intern ("list-buffers-directory");
983
984 XFASTINT (col1) = 19;
985 XFASTINT (col2) = 25;
986 XFASTINT (col3) = 40;
987 XFASTINT (minspace) = 1;
988
989 Fset_buffer (Vstandard_output);
990
991 tail = intern ("Buffer-menu-mode");
992 if (!EQ (tail, current_buffer->major_mode)
265a9e55 993 && (tem = Ffboundp (tail), !NILP (tem)))
1ab256cb
RM
994 call0 (tail);
995 Fbuffer_disable_undo (Vstandard_output);
996 current_buffer->read_only = Qnil;
997
998 write_string ("\
999 MR Buffer Size Mode File\n\
1000 -- ------ ---- ---- ----\n", -1);
1001
265a9e55 1002 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
1003 {
1004 buf = Fcdr (Fcar (tail));
1005 b = XBUFFER (buf);
1006 /* Don't mention the minibuffers. */
1007 if (XSTRING (b->name)->data[0] == ' ')
1008 continue;
1009 /* Optionally don't mention buffers that lack files. */
265a9e55 1010 if (!NILP (files) && NILP (b->filename))
1ab256cb
RM
1011 continue;
1012 /* Identify the current buffer. */
1013 if (b == old)
1014 desired_point = point;
1015 write_string (b == old ? "." : " ", -1);
1016 /* Identify modified buffers */
1017 write_string (BUF_MODIFF (b) > b->save_modified ? "*" : " ", -1);
265a9e55 1018 write_string (NILP (b->read_only) ? " " : "% ", -1);
1ab256cb
RM
1019 Fprinc (b->name, Qnil);
1020 Findent_to (col1, make_number (2));
1021 XFASTINT (tem) = BUF_Z (b) - BUF_BEG (b);
1022 Fprin1 (tem, Qnil);
1023 Findent_to (col2, minspace);
1024 Fprinc (b->mode_name, Qnil);
1025 Findent_to (col3, minspace);
1026
265a9e55 1027 if (!NILP (b->filename))
1ab256cb
RM
1028 Fprinc (b->filename, Qnil);
1029 else
1030 {
1031 /* No visited file; check local value of list-buffers-directory. */
1032 Lisp_Object tem;
1033 set_buffer_internal (b);
1034 tem = Fboundp (other_file_symbol);
265a9e55 1035 if (!NILP (tem))
1ab256cb
RM
1036 {
1037 tem = Fsymbol_value (other_file_symbol);
1038 Fset_buffer (Vstandard_output);
1039 if (XTYPE (tem) == Lisp_String)
1040 Fprinc (tem, Qnil);
1041 }
1042 else
1043 Fset_buffer (Vstandard_output);
1044 }
1045 write_string ("\n", -1);
1046 }
1047
1048 current_buffer->read_only = Qt;
1049 set_buffer_internal (old);
1050/* Foo. This doesn't work since temp_output_buffer_show sets point to 1
1051 if (desired_point)
1052 XBUFFER (Vstandard_output)->text.pointloc = desired_point;
1053 */
1054 return Qnil;
1055}
1056
1057DEFUN ("list-buffers", Flist_buffers, Slist_buffers, 0, 1, "P",
1058 "Display a list of names of existing buffers.\n\
1059The list is displayed in a buffer named `*Buffer List*'.\n\
1060Note that buffers with names starting with spaces are omitted.\n\
1061Non-null optional arg FILES-ONLY means mention only file buffers.\n\
1062\n\
1063The M column contains a * for buffers that are modified.\n\
1064The R column contains a % for buffers that are read-only.")
1065 (files)
1066 Lisp_Object files;
1067{
1068 internal_with_output_to_temp_buffer ("*Buffer List*",
1069 list_buffers_1, files);
1070 return Qnil;
1071}
1072
1073DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1074 0, 0, 0,
1075 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1076Most local variable bindings are eliminated so that the default values\n\
1077become effective once more. Also, the syntax table is set from\n\
1078`standard-syntax-table', the local keymap is set to nil,\n\
1079and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1080This function also forces redisplay of the mode line.\n\
1081\n\
1082Every function to select a new major mode starts by\n\
1083calling this function.\n\n\
1084As a special exception, local variables whose names have\n\
1085a non-nil `permanent-local' property are not eliminated by this function.")
1086 ()
1087{
1088 register Lisp_Object alist, sym, tem;
1089 Lisp_Object oalist;
1090 oalist = current_buffer->local_var_alist;
1091
1092 /* Make sure no local variables remain set up with this buffer
1093 for their current values. */
1094
265a9e55 1095 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1096 {
1097 sym = XCONS (XCONS (alist)->car)->car;
1098
1099 /* Need not do anything if some other buffer's binding is now encached. */
1100 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car;
1101 if (XBUFFER (tem) == current_buffer)
1102 {
1103 /* Symbol is set up for this buffer's old local value.
1104 Set it up for the current buffer with the default value. */
1105
1106 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr;
1107 XCONS (tem)->car = tem;
1108 XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Fcurrent_buffer ();
1109 store_symval_forwarding (sym, XCONS (XSYMBOL (sym)->value)->car,
1110 XCONS (tem)->cdr);
1111 }
1112 }
1113
1114 /* Actually eliminate all local bindings of this buffer. */
1115
1116 reset_buffer_local_variables (current_buffer);
1117
1118 /* Redisplay mode lines; we are changing major mode. */
1119
1120 update_mode_lines++;
1121
1122 /* Any which are supposed to be permanent,
1123 make local again, with the same values they had. */
1124
265a9e55 1125 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1126 {
1127 sym = XCONS (XCONS (alist)->car)->car;
1128 tem = Fget (sym, Qpermanent_local);
265a9e55 1129 if (! NILP (tem))
01050cb5
RM
1130 {
1131 Fmake_local_variable (sym);
1132 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1133 }
1ab256cb
RM
1134 }
1135
1136 /* Force mode-line redisplay. Useful here because all major mode
1137 commands call this function. */
1138 update_mode_lines++;
1139
1140 return Qnil;
1141}
1142\f
1143DEFUN ("region-fields", Fregion_fields, Sregion_fields, 2, 4, "",
1144 "Return list of fields overlapping a given portion of a buffer.\n\
1145The portion is specified by arguments START, END and BUFFER.\n\
1146BUFFER defaults to the current buffer.\n\
1147Optional 4th arg ERROR-CHECK non nil means just report an error\n\
1148if any protected fields overlap this portion.")
1149 (start, end, buffer, error_check)
1150 Lisp_Object start, end, buffer, error_check;
1151{
1152 register int start_loc, end_loc;
1153 Lisp_Object fieldlist;
1154 Lisp_Object collector;
1155
265a9e55 1156 if (NILP (buffer))
1ab256cb
RM
1157 fieldlist = current_buffer->fieldlist;
1158 else
1159 {
1160 CHECK_BUFFER (buffer, 1);
1161 fieldlist = XBUFFER (buffer)->fieldlist;
1162 }
1163
1164 CHECK_NUMBER_COERCE_MARKER (start, 2);
1165 start_loc = XINT (start);
1166
1167 CHECK_NUMBER_COERCE_MARKER (end, 2);
1168 end_loc = XINT (end);
1169
1170 collector = Qnil;
1171
1172 while (XTYPE (fieldlist) == Lisp_Cons)
1173 {
1174 register Lisp_Object field;
1175 register int field_start, field_end;
1176
1177 field = XCONS (fieldlist)->car;
1178 field_start = marker_position (FIELD_START_MARKER (field)) - 1;
1179 field_end = marker_position (FIELD_END_MARKER (field));
1180
1181 if ((start_loc < field_start && end_loc > field_start)
1182 || (start_loc >= field_start && start_loc < field_end))
1183 {
265a9e55 1184 if (!NILP (error_check))
1ab256cb 1185 {
265a9e55 1186 if (!NILP (FIELD_PROTECTED_FLAG (field)))
1ab256cb
RM
1187 {
1188 struct gcpro gcpro1;
1189 GCPRO1 (fieldlist);
1190 Fsignal (Qprotected_field, Fcons (field, Qnil));
1191 UNGCPRO;
1192 }
1193 }
1194 else
1195 collector = Fcons (field, collector);
1196 }
1197
1198 fieldlist = XCONS (fieldlist)->cdr;
1199 }
1200
1201 return collector;
1202}
1203\f
1204init_buffer_once ()
1205{
1206 register Lisp_Object tem;
1207
1208 /* Make sure all markable slots in buffer_defaults
1209 are initialized reasonably, so mark_buffer won't choke. */
1210 reset_buffer (&buffer_defaults);
1211 reset_buffer (&buffer_local_symbols);
1212 XSET (Vbuffer_defaults, Lisp_Buffer, &buffer_defaults);
1213 XSET (Vbuffer_local_symbols, Lisp_Buffer, &buffer_local_symbols);
1214
1215 /* Set up the default values of various buffer slots. */
1216 /* Must do these before making the first buffer! */
1217
1218 /* real setup is done in loaddefs.el */
1219 buffer_defaults.mode_line_format = build_string ("%-");
1220 buffer_defaults.abbrev_mode = Qnil;
1221 buffer_defaults.overwrite_mode = Qnil;
1222 buffer_defaults.case_fold_search = Qt;
1223 buffer_defaults.auto_fill_function = Qnil;
1224 buffer_defaults.selective_display = Qnil;
1225#ifndef old
1226 buffer_defaults.selective_display_ellipses = Qt;
1227#endif
1228 buffer_defaults.abbrev_table = Qnil;
1229 buffer_defaults.display_table = Qnil;
1230 buffer_defaults.fieldlist = Qnil;
1231 buffer_defaults.undo_list = Qnil;
1232
1233 XFASTINT (buffer_defaults.tab_width) = 8;
1234 buffer_defaults.truncate_lines = Qnil;
1235 buffer_defaults.ctl_arrow = Qt;
1236
1237 XFASTINT (buffer_defaults.fill_column) = 70;
1238 XFASTINT (buffer_defaults.left_margin) = 0;
1239
1240 /* Assign the local-flags to the slots that have default values.
1241 The local flag is a bit that is used in the buffer
1242 to say that it has its own local value for the slot.
1243 The local flag bits are in the local_var_flags slot of the buffer. */
1244
1245 /* Nothing can work if this isn't true */
1246 if (sizeof (int) != sizeof (Lisp_Object)) abort ();
1247
1248 /* 0 means not a lisp var, -1 means always local, else mask */
1249 bzero (&buffer_local_flags, sizeof buffer_local_flags);
1250 XFASTINT (buffer_local_flags.filename) = -1;
1251 XFASTINT (buffer_local_flags.directory) = -1;
1252 XFASTINT (buffer_local_flags.backed_up) = -1;
1253 XFASTINT (buffer_local_flags.save_length) = -1;
1254 XFASTINT (buffer_local_flags.auto_save_file_name) = -1;
1255 XFASTINT (buffer_local_flags.read_only) = -1;
1256 XFASTINT (buffer_local_flags.major_mode) = -1;
1257 XFASTINT (buffer_local_flags.mode_name) = -1;
1258 XFASTINT (buffer_local_flags.undo_list) = -1;
1259
1260 XFASTINT (buffer_local_flags.mode_line_format) = 1;
1261 XFASTINT (buffer_local_flags.abbrev_mode) = 2;
1262 XFASTINT (buffer_local_flags.overwrite_mode) = 4;
1263 XFASTINT (buffer_local_flags.case_fold_search) = 8;
1264 XFASTINT (buffer_local_flags.auto_fill_function) = 0x10;
1265 XFASTINT (buffer_local_flags.selective_display) = 0x20;
1266#ifndef old
1267 XFASTINT (buffer_local_flags.selective_display_ellipses) = 0x40;
1268#endif
1269 XFASTINT (buffer_local_flags.tab_width) = 0x80;
1270 XFASTINT (buffer_local_flags.truncate_lines) = 0x100;
1271 XFASTINT (buffer_local_flags.ctl_arrow) = 0x200;
1272 XFASTINT (buffer_local_flags.fill_column) = 0x400;
1273 XFASTINT (buffer_local_flags.left_margin) = 0x800;
1274 XFASTINT (buffer_local_flags.abbrev_table) = 0x1000;
1275 XFASTINT (buffer_local_flags.display_table) = 0x2000;
1276 XFASTINT (buffer_local_flags.fieldlist) = 0x4000;
1277 XFASTINT (buffer_local_flags.syntax_table) = 0x8000;
1278
1279 Vbuffer_alist = Qnil;
1280 current_buffer = 0;
1281 all_buffers = 0;
1282
1283 QSFundamental = build_string ("Fundamental");
1284
1285 Qfundamental_mode = intern ("fundamental-mode");
1286 buffer_defaults.major_mode = Qfundamental_mode;
1287
1288 Qmode_class = intern ("mode-class");
1289
1290 Qprotected_field = intern ("protected-field");
1291
1292 Qpermanent_local = intern ("permanent-local");
1293
1294 Qkill_buffer_hook = intern ("kill-buffer-hook");
1295
1296 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
1297 /* super-magic invisible buffer */
1298 Vbuffer_alist = Qnil;
1299
ffd56f97 1300 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1ab256cb
RM
1301}
1302
1303init_buffer ()
1304{
1305 char buf[MAXPATHLEN+1];
1306
1307 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1308 if (getwd (buf) == 0)
1309 fatal ("`getwd' failed: %s.\n", buf);
1310
1311#ifndef VMS
1312 /* Maybe this should really use some standard subroutine
1313 whose definition is filename syntax dependent. */
1314 if (buf[strlen (buf) - 1] != '/')
1315 strcat (buf, "/");
1316#endif /* not VMS */
1317 current_buffer->directory = build_string (buf);
1318}
1319
1320/* initialize the buffer routines */
1321syms_of_buffer ()
1322{
1323 staticpro (&Vbuffer_defaults);
1324 staticpro (&Vbuffer_local_symbols);
1325 staticpro (&Qfundamental_mode);
1326 staticpro (&Qmode_class);
1327 staticpro (&QSFundamental);
1328 staticpro (&Vbuffer_alist);
1329 staticpro (&Qprotected_field);
1330 staticpro (&Qpermanent_local);
1331 staticpro (&Qkill_buffer_hook);
1332
1333 Fput (Qprotected_field, Qerror_conditions,
1334 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
1335 Fput (Qprotected_field, Qerror_message,
1336 build_string ("Attempt to modify a protected field"));
1337
1338 /* All these use DEFVAR_LISP_NOPRO because the slots in
1339 buffer_defaults will all be marked via Vbuffer_defaults. */
1340
1341 DEFVAR_LISP_NOPRO ("default-mode-line-format",
1342 &buffer_defaults.mode_line_format,
1343 "Default value of `mode-line-format' for buffers that don't override it.\n\
1344This is the same as (default-value 'mode-line-format).");
1345
1346 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
1347 &buffer_defaults.abbrev_mode,
1348 "Default value of `abbrev-mode' for buffers that do not override it.\n\
1349This is the same as (default-value 'abbrev-mode).");
1350
1351 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
1352 &buffer_defaults.ctl_arrow,
1353 "Default value of `ctl-arrow' for buffers that do not override it.\n\
1354This is the same as (default-value 'ctl-arrow).");
1355
1356 DEFVAR_LISP_NOPRO ("default-truncate-lines",
1357 &buffer_defaults.truncate_lines,
1358 "Default value of `truncate-lines' for buffers that do not override it.\n\
1359This is the same as (default-value 'truncate-lines).");
1360
1361 DEFVAR_LISP_NOPRO ("default-fill-column",
1362 &buffer_defaults.fill_column,
1363 "Default value of `fill-column' for buffers that do not override it.\n\
1364This is the same as (default-value 'fill-column).");
1365
1366 DEFVAR_LISP_NOPRO ("default-left-margin",
1367 &buffer_defaults.left_margin,
1368 "Default value of `left-margin' for buffers that do not override it.\n\
1369This is the same as (default-value 'left-margin).");
1370
1371 DEFVAR_LISP_NOPRO ("default-tab-width",
1372 &buffer_defaults.tab_width,
1373 "Default value of `tab-width' for buffers that do not override it.\n\
1374This is the same as (default-value 'tab-width).");
1375
1376 DEFVAR_LISP_NOPRO ("default-case-fold-search",
1377 &buffer_defaults.case_fold_search,
1378 "Default value of `case-fold-search' for buffers that don't override it.\n\
1379This is the same as (default-value 'case-fold-search).");
1380
1381 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format, 0);
1382
1383/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
1384 But make-docfile finds it!
1385 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
1386 "Template for displaying mode line for current buffer.\n\
1387Each buffer has its own value of this variable.\n\
1388Value may be a string, a symbol or a list or cons cell.\n\
1389For a symbol, its value is used (but it is ignored if t or nil).\n\
1390 A string appearing directly as the value of a symbol is processed verbatim\n\
1391 in that the %-constructs below are not recognized.\n\
1392For a list whose car is a symbol, the symbol's value is taken,\n\
1393 and if that is non-nil, the cadr of the list is processed recursively.\n\
1394 Otherwise, the caddr of the list (if there is one) is processed.\n\
1395For a list whose car is a string or list, each element is processed\n\
1396 recursively and the results are effectively concatenated.\n\
1397For a list whose car is an integer, the cdr of the list is processed\n\
1398 and padded (if the number is positive) or truncated (if negative)\n\
1399 to the width specified by that number.\n\
1400A string is printed verbatim in the mode line except for %-constructs:\n\
1401 (%-constructs are allowed when the string is the entire mode-line-format\n\
1402 or when it is found in a cons-cell or a list)\n\
1403 %b -- print buffer name. %f -- print visited file name.\n\
1404 %* -- print *, % or hyphen. %m -- print value of mode-name (obsolete).\n\
1405 %s -- print process status. %M -- print value of global-mode-string. (obs)\n\
1406 %p -- print percent of buffer above top of window, or top, bot or all.\n\
1407 %n -- print Narrow if appropriate.\n\
1408 %[ -- print one [ for each recursive editing level. %] similar.\n\
1409 %% -- print %. %- -- print infinitely many dashes.\n\
1410Decimal digits after the % specify field width to which to pad.");
1411*/
1412
1413 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
1414 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
1415nil here means use current buffer's major mode.");
1416
1417 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
1418 "Symbol for current buffer's major mode.");
1419
1420 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
1421 "Pretty name of current buffer's major mode (a string).");
1422
1423 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode,
1424 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
1425Automatically becomes buffer-local when set in any fashion.");
1426
1427 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
1428 "*Non-nil if searches should ignore case.\n\
1429Automatically becomes buffer-local when set in any fashion.");
1430
1431 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
1432 "*Column beyond which automatic line-wrapping should happen.\n\
1433Automatically becomes buffer-local when set in any fashion.");
1434
1435 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
1436 "*Column for the default indent-line-function to indent to.\n\
1437Linefeed indents to this column in Fundamental mode.\n\
1438Automatically becomes buffer-local when set in any fashion.");
1439
1440 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
1441 "*Distance between tab stops (for display of tab characters), in columns.\n\
1442Automatically becomes buffer-local when set in any fashion.");
1443
1444 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow,
1445 "*Non-nil means display control chars with uparrow.\n\
1446Nil means use backslash and octal digits.\n\
1447Automatically becomes buffer-local when set in any fashion.\n\
1448This variable does not apply to characters whose display is specified\n\
1449in the current display table (if there is one).");
1450
1451 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines,
1452 "*Non-nil means do not display continuation lines;\n\
1453give each line of text one screen line.\n\
1454Automatically becomes buffer-local when set in any fashion.\n\
1455\n\
1456Note that this is overridden by the variable\n\
1457`truncate-partial-width-windows' if that variable is non-nil\n\
1458and this buffer is not full-screen width.");
1459
1460 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
1461 "Name of default directory of current buffer. Should end with slash.\n\
1462Each buffer has its own value of this variable.");
1463
1464 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
1465 "Function called (if non-nil) to perform auto-fill.\n\
1466It is called after self-inserting a space at a column beyond `fill-column'.\n\
1467Each buffer has its own value of this variable.\n\
1468NOTE: This variable is not an ordinary hook;\n\
1469It may not be a list of functions.");
1470
1471 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
1472 "Name of file visited in current buffer, or nil if not visiting a file.\n\
1473Each buffer has its own value of this variable.");
1474
1475 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
1476 &current_buffer->auto_save_file_name,
1477 "Name of file for auto-saving current buffer,\n\
1478or nil if buffer should not be auto-saved.\n\
1479Each buffer has its own value of this variable.");
1480
1481 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only,
1482 "Non-nil if this buffer is read-only.\n\
1483Each buffer has its own value of this variable.");
1484
1485 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up,
1486 "Non-nil if this buffer's file has been backed up.\n\
1487Backing up is done before the first time the file is saved.\n\
1488Each buffer has its own value of this variable.");
1489
1490 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
1491 "Length of current buffer when last read in, saved or auto-saved.\n\
14920 initially.\n\
1493Each buffer has its own value of this variable.");
1494
1495 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
1496 "Non-nil enables selective display:\n\
1497Integer N as value means display only lines\n\
1498 that start with less than n columns of space.\n\
1499A value of t means, after a ^M, all the rest of the line is invisible.\n\
1500 Then ^M's in the file are written into files as newlines.\n\n\
1501Automatically becomes buffer-local when set in any fashion.");
1502
1503#ifndef old
1504 DEFVAR_PER_BUFFER ("selective-display-ellipses",
1505 &current_buffer->selective_display_ellipses,
1506 "t means display ... on previous line when a line is invisible.\n\
1507Automatically becomes buffer-local when set in any fashion.");
1508#endif
1509
1510 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode,
1511 "Non-nil if self-insertion should replace existing text.\n\
1512Automatically becomes buffer-local when set in any fashion.");
1513
1514 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
1515 "Display table that controls display of the contents of current buffer.\n\
1516Automatically becomes buffer-local when set in any fashion.\n\
1517The display table is a vector created with `make-display-table'.\n\
1518The first 256 elements control how to display each possible text character.\n\
1519The value should be a \"rope\" (see `make-rope') or nil;\n\
1520nil means display the character in the default fashion.\n\
1521The remaining five elements are ropes that control the display of\n\
1522 the end of a truncated screen line (element 256);\n\
1523 the end of a continued line (element 257);\n\
1524 the escape character used to display character codes in octal (element 258);\n\
1525 the character used as an arrow for control characters (element 259);\n\
1526 the decoration indicating the presence of invisible lines (element 260).\n\
1527If this variable is nil, the value of `standard-display-table' is used.\n\
1528Each window can have its own, overriding display table.");
1529
1530 DEFVAR_PER_BUFFER ("buffer-field-list", &current_buffer->fieldlist,
1531 "List of fields in the current buffer. See `add-field'.");
1532
1533 DEFVAR_BOOL ("check-protected-fields", check_protected_fields,
1534 "Non-nil means don't allow modification of a protected field.\n\
1535See `add-field'.");
1536 check_protected_fields = 0;
1537
1538/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
1539 "Don't ask.");
1540*/
01050cb5 1541 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
1ab256cb
RM
1542 "Function to call before each text change.\n\
1543Two arguments are passed to the function: the positions of\n\
1544the beginning and end of the range of old text to be changed.\n\
1545\(For an insertion, the beginning and end are at the same place.)\n\
1546No information is given about the length of the text after the change.\n\
1547position of the change\n\
1548\n\
1549While executing the `before-change-function', changes to buffers do not\n\
1550cause calls to any `before-change-function' or `after-change-function'.");
1551 Vbefore_change_function = Qnil;
1552
1553 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
1554 "Function to call after each text change.\n\
1555Three arguments are passed to the function: the positions of\n\
1556the beginning and end of the range of changed text,\n\
1557and the length of the pre-change text replaced by that range.\n\
1558\(For an insertion, the pre-change length is zero;\n\
1559for a deletion, that length is the number of characters deleted,\n\
1560and the post-change beginning and end are at the same place.)\n\
1561\n\
1562While executing the `after-change-function', changes to buffers do not\n\
1563cause calls to any `before-change-function' or `after-change-function'.");
1564 Vafter_change_function = Qnil;
1565
1566 DEFVAR_LISP ("first-change-function", &Vfirst_change_function,
1567 "Function to call before changing a buffer which is unmodified.\n\
1568The function is called, with no arguments, if it is non-nil.");
1569 Vfirst_change_function = Qnil;
1570
1571 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list,
1572 "List of undo entries in current buffer.\n\
1573Recent changes come first; older changes follow newer.\n\
1574\n\
1575An entry (START . END) represents an insertion which begins at\n\
1576position START and ends at position END.\n\
1577\n\
1578An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
1579from (abs POSITION). If POSITION is positive, point was at the front\n\
1580of the text being deleted; if negative, point was at the end.\n\
1581\n\
1582An entry (t HIGHWORD LOWWORD) indicates that the buffer had been\n\
1583previously unmodified. HIGHWORD and LOWWORD are the high and low\n\
158416-bit words of the buffer's modification count at the time. If the\n\
1585modification count of the most recent save is different, this entry is\n\
1586obsolete.\n\
1587\n\
1588nil marks undo boundaries. The undo command treats the changes\n\
1589between two undo boundaries as a single step to be undone.\n\
1590\n\
1591If the value of the variable is t, undo information is not recorded.\n\
1592");
1593
1594 defsubr (&Sbuffer_list);
1595 defsubr (&Sget_buffer);
1596 defsubr (&Sget_file_buffer);
1597 defsubr (&Sget_buffer_create);
01050cb5 1598 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
1599 defsubr (&Sbuffer_name);
1600/*defsubr (&Sbuffer_number);*/
1601 defsubr (&Sbuffer_file_name);
1602 defsubr (&Sbuffer_local_variables);
1603 defsubr (&Sbuffer_modified_p);
1604 defsubr (&Sset_buffer_modified_p);
1605 defsubr (&Sbuffer_modified_tick);
1606 defsubr (&Srename_buffer);
1607 defsubr (&Sother_buffer);
1608 defsubr (&Sbuffer_disable_undo);
1609 defsubr (&Sbuffer_enable_undo);
1610 defsubr (&Skill_buffer);
1611 defsubr (&Serase_buffer);
1612 defsubr (&Sswitch_to_buffer);
1613 defsubr (&Spop_to_buffer);
1614 defsubr (&Scurrent_buffer);
1615 defsubr (&Sset_buffer);
1616 defsubr (&Sbarf_if_buffer_read_only);
1617 defsubr (&Sbury_buffer);
1618 defsubr (&Slist_buffers);
1619 defsubr (&Skill_all_local_variables);
1620 defsubr (&Sregion_fields);
1621}
1622
1623keys_of_buffer ()
1624{
1625 initial_define_key (control_x_map, 'b', "switch-to-buffer");
1626 initial_define_key (control_x_map, 'k', "kill-buffer");
1627 initial_define_key (control_x_map, Ctl ('B'), "list-buffers");
1628}