*** 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\
905thus, the least likely buffer for \\[switch-to-buffer] to select by default.")
906 (buf)
907 register Lisp_Object buf;
908{
909 register Lisp_Object aelt, link;
910
265a9e55 911 if (NILP (buf))
1ab256cb
RM
912 {
913 XSET (buf, Lisp_Buffer, current_buffer);
914 Fswitch_to_buffer (Fother_buffer (buf), Qnil);
915 }
916 else
917 {
918 Lisp_Object buf1;
919
920 buf1 = Fget_buffer (buf);
265a9e55 921 if (NILP (buf1))
1ab256cb
RM
922 nsberror (buf);
923 buf = buf1;
924 }
925
926 aelt = Frassq (buf, Vbuffer_alist);
927 link = Fmemq (aelt, Vbuffer_alist);
928 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
929 XCONS (link)->cdr = Qnil;
930 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
931 return Qnil;
932}
933\f
934DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, 0,
935 "Delete the entire contents of the current buffer.\n\
936Any clipping restriction in effect (see `narrow-to-buffer') is removed,\n\
937so the buffer is truly empty after this.")
938 ()
939{
940 Fwiden ();
941 del_range (BEG, Z);
942 current_buffer->last_window_start = 1;
943 /* Prevent warnings, or suspension of auto saving, that would happen
944 if future size is less than past size. Use of erase-buffer
945 implies that the future text is not really related to the past text. */
946 XFASTINT (current_buffer->save_length) = 0;
947 return Qnil;
948}
949
950validate_region (b, e)
951 register Lisp_Object *b, *e;
952{
953 register int i;
954
955 CHECK_NUMBER_COERCE_MARKER (*b, 0);
956 CHECK_NUMBER_COERCE_MARKER (*e, 1);
957
958 if (XINT (*b) > XINT (*e))
959 {
960 i = XFASTINT (*b); /* This is legit even if *b is < 0 */
961 *b = *e;
962 XFASTINT (*e) = i; /* because this is all we do with i. */
963 }
964
965 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
966 && XINT (*e) <= ZV))
967 args_out_of_range (*b, *e);
968}
969\f
970Lisp_Object
971list_buffers_1 (files)
972 Lisp_Object files;
973{
974 register Lisp_Object tail, tem, buf;
975 Lisp_Object col1, col2, col3, minspace;
976 register struct buffer *old = current_buffer, *b;
977 int desired_point = 0;
978 Lisp_Object other_file_symbol;
979
980 other_file_symbol = intern ("list-buffers-directory");
981
982 XFASTINT (col1) = 19;
983 XFASTINT (col2) = 25;
984 XFASTINT (col3) = 40;
985 XFASTINT (minspace) = 1;
986
987 Fset_buffer (Vstandard_output);
988
989 tail = intern ("Buffer-menu-mode");
990 if (!EQ (tail, current_buffer->major_mode)
265a9e55 991 && (tem = Ffboundp (tail), !NILP (tem)))
1ab256cb
RM
992 call0 (tail);
993 Fbuffer_disable_undo (Vstandard_output);
994 current_buffer->read_only = Qnil;
995
996 write_string ("\
997 MR Buffer Size Mode File\n\
998 -- ------ ---- ---- ----\n", -1);
999
265a9e55 1000 for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
1ab256cb
RM
1001 {
1002 buf = Fcdr (Fcar (tail));
1003 b = XBUFFER (buf);
1004 /* Don't mention the minibuffers. */
1005 if (XSTRING (b->name)->data[0] == ' ')
1006 continue;
1007 /* Optionally don't mention buffers that lack files. */
265a9e55 1008 if (!NILP (files) && NILP (b->filename))
1ab256cb
RM
1009 continue;
1010 /* Identify the current buffer. */
1011 if (b == old)
1012 desired_point = point;
1013 write_string (b == old ? "." : " ", -1);
1014 /* Identify modified buffers */
1015 write_string (BUF_MODIFF (b) > b->save_modified ? "*" : " ", -1);
265a9e55 1016 write_string (NILP (b->read_only) ? " " : "% ", -1);
1ab256cb
RM
1017 Fprinc (b->name, Qnil);
1018 Findent_to (col1, make_number (2));
1019 XFASTINT (tem) = BUF_Z (b) - BUF_BEG (b);
1020 Fprin1 (tem, Qnil);
1021 Findent_to (col2, minspace);
1022 Fprinc (b->mode_name, Qnil);
1023 Findent_to (col3, minspace);
1024
265a9e55 1025 if (!NILP (b->filename))
1ab256cb
RM
1026 Fprinc (b->filename, Qnil);
1027 else
1028 {
1029 /* No visited file; check local value of list-buffers-directory. */
1030 Lisp_Object tem;
1031 set_buffer_internal (b);
1032 tem = Fboundp (other_file_symbol);
265a9e55 1033 if (!NILP (tem))
1ab256cb
RM
1034 {
1035 tem = Fsymbol_value (other_file_symbol);
1036 Fset_buffer (Vstandard_output);
1037 if (XTYPE (tem) == Lisp_String)
1038 Fprinc (tem, Qnil);
1039 }
1040 else
1041 Fset_buffer (Vstandard_output);
1042 }
1043 write_string ("\n", -1);
1044 }
1045
1046 current_buffer->read_only = Qt;
1047 set_buffer_internal (old);
1048/* Foo. This doesn't work since temp_output_buffer_show sets point to 1
1049 if (desired_point)
1050 XBUFFER (Vstandard_output)->text.pointloc = desired_point;
1051 */
1052 return Qnil;
1053}
1054
1055DEFUN ("list-buffers", Flist_buffers, Slist_buffers, 0, 1, "P",
1056 "Display a list of names of existing buffers.\n\
1057The list is displayed in a buffer named `*Buffer List*'.\n\
1058Note that buffers with names starting with spaces are omitted.\n\
1059Non-null optional arg FILES-ONLY means mention only file buffers.\n\
1060\n\
1061The M column contains a * for buffers that are modified.\n\
1062The R column contains a % for buffers that are read-only.")
1063 (files)
1064 Lisp_Object files;
1065{
1066 internal_with_output_to_temp_buffer ("*Buffer List*",
1067 list_buffers_1, files);
1068 return Qnil;
1069}
1070
1071DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1072 0, 0, 0,
1073 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1074Most local variable bindings are eliminated so that the default values\n\
1075become effective once more. Also, the syntax table is set from\n\
1076`standard-syntax-table', the local keymap is set to nil,\n\
1077and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1078This function also forces redisplay of the mode line.\n\
1079\n\
1080Every function to select a new major mode starts by\n\
1081calling this function.\n\n\
1082As a special exception, local variables whose names have\n\
1083a non-nil `permanent-local' property are not eliminated by this function.")
1084 ()
1085{
1086 register Lisp_Object alist, sym, tem;
1087 Lisp_Object oalist;
1088 oalist = current_buffer->local_var_alist;
1089
1090 /* Make sure no local variables remain set up with this buffer
1091 for their current values. */
1092
265a9e55 1093 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1094 {
1095 sym = XCONS (XCONS (alist)->car)->car;
1096
1097 /* Need not do anything if some other buffer's binding is now encached. */
1098 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car;
1099 if (XBUFFER (tem) == current_buffer)
1100 {
1101 /* Symbol is set up for this buffer's old local value.
1102 Set it up for the current buffer with the default value. */
1103
1104 tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr;
1105 XCONS (tem)->car = tem;
1106 XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Fcurrent_buffer ();
1107 store_symval_forwarding (sym, XCONS (XSYMBOL (sym)->value)->car,
1108 XCONS (tem)->cdr);
1109 }
1110 }
1111
1112 /* Actually eliminate all local bindings of this buffer. */
1113
1114 reset_buffer_local_variables (current_buffer);
1115
1116 /* Redisplay mode lines; we are changing major mode. */
1117
1118 update_mode_lines++;
1119
1120 /* Any which are supposed to be permanent,
1121 make local again, with the same values they had. */
1122
265a9e55 1123 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1ab256cb
RM
1124 {
1125 sym = XCONS (XCONS (alist)->car)->car;
1126 tem = Fget (sym, Qpermanent_local);
265a9e55 1127 if (! NILP (tem))
01050cb5
RM
1128 {
1129 Fmake_local_variable (sym);
1130 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1131 }
1ab256cb
RM
1132 }
1133
1134 /* Force mode-line redisplay. Useful here because all major mode
1135 commands call this function. */
1136 update_mode_lines++;
1137
1138 return Qnil;
1139}
1140\f
1141DEFUN ("region-fields", Fregion_fields, Sregion_fields, 2, 4, "",
1142 "Return list of fields overlapping a given portion of a buffer.\n\
1143The portion is specified by arguments START, END and BUFFER.\n\
1144BUFFER defaults to the current buffer.\n\
1145Optional 4th arg ERROR-CHECK non nil means just report an error\n\
1146if any protected fields overlap this portion.")
1147 (start, end, buffer, error_check)
1148 Lisp_Object start, end, buffer, error_check;
1149{
1150 register int start_loc, end_loc;
1151 Lisp_Object fieldlist;
1152 Lisp_Object collector;
1153
265a9e55 1154 if (NILP (buffer))
1ab256cb
RM
1155 fieldlist = current_buffer->fieldlist;
1156 else
1157 {
1158 CHECK_BUFFER (buffer, 1);
1159 fieldlist = XBUFFER (buffer)->fieldlist;
1160 }
1161
1162 CHECK_NUMBER_COERCE_MARKER (start, 2);
1163 start_loc = XINT (start);
1164
1165 CHECK_NUMBER_COERCE_MARKER (end, 2);
1166 end_loc = XINT (end);
1167
1168 collector = Qnil;
1169
1170 while (XTYPE (fieldlist) == Lisp_Cons)
1171 {
1172 register Lisp_Object field;
1173 register int field_start, field_end;
1174
1175 field = XCONS (fieldlist)->car;
1176 field_start = marker_position (FIELD_START_MARKER (field)) - 1;
1177 field_end = marker_position (FIELD_END_MARKER (field));
1178
1179 if ((start_loc < field_start && end_loc > field_start)
1180 || (start_loc >= field_start && start_loc < field_end))
1181 {
265a9e55 1182 if (!NILP (error_check))
1ab256cb 1183 {
265a9e55 1184 if (!NILP (FIELD_PROTECTED_FLAG (field)))
1ab256cb
RM
1185 {
1186 struct gcpro gcpro1;
1187 GCPRO1 (fieldlist);
1188 Fsignal (Qprotected_field, Fcons (field, Qnil));
1189 UNGCPRO;
1190 }
1191 }
1192 else
1193 collector = Fcons (field, collector);
1194 }
1195
1196 fieldlist = XCONS (fieldlist)->cdr;
1197 }
1198
1199 return collector;
1200}
1201\f
1202init_buffer_once ()
1203{
1204 register Lisp_Object tem;
1205
1206 /* Make sure all markable slots in buffer_defaults
1207 are initialized reasonably, so mark_buffer won't choke. */
1208 reset_buffer (&buffer_defaults);
1209 reset_buffer (&buffer_local_symbols);
1210 XSET (Vbuffer_defaults, Lisp_Buffer, &buffer_defaults);
1211 XSET (Vbuffer_local_symbols, Lisp_Buffer, &buffer_local_symbols);
1212
1213 /* Set up the default values of various buffer slots. */
1214 /* Must do these before making the first buffer! */
1215
1216 /* real setup is done in loaddefs.el */
1217 buffer_defaults.mode_line_format = build_string ("%-");
1218 buffer_defaults.abbrev_mode = Qnil;
1219 buffer_defaults.overwrite_mode = Qnil;
1220 buffer_defaults.case_fold_search = Qt;
1221 buffer_defaults.auto_fill_function = Qnil;
1222 buffer_defaults.selective_display = Qnil;
1223#ifndef old
1224 buffer_defaults.selective_display_ellipses = Qt;
1225#endif
1226 buffer_defaults.abbrev_table = Qnil;
1227 buffer_defaults.display_table = Qnil;
1228 buffer_defaults.fieldlist = Qnil;
1229 buffer_defaults.undo_list = Qnil;
1230
1231 XFASTINT (buffer_defaults.tab_width) = 8;
1232 buffer_defaults.truncate_lines = Qnil;
1233 buffer_defaults.ctl_arrow = Qt;
1234
1235 XFASTINT (buffer_defaults.fill_column) = 70;
1236 XFASTINT (buffer_defaults.left_margin) = 0;
1237
1238 /* Assign the local-flags to the slots that have default values.
1239 The local flag is a bit that is used in the buffer
1240 to say that it has its own local value for the slot.
1241 The local flag bits are in the local_var_flags slot of the buffer. */
1242
1243 /* Nothing can work if this isn't true */
1244 if (sizeof (int) != sizeof (Lisp_Object)) abort ();
1245
1246 /* 0 means not a lisp var, -1 means always local, else mask */
1247 bzero (&buffer_local_flags, sizeof buffer_local_flags);
1248 XFASTINT (buffer_local_flags.filename) = -1;
1249 XFASTINT (buffer_local_flags.directory) = -1;
1250 XFASTINT (buffer_local_flags.backed_up) = -1;
1251 XFASTINT (buffer_local_flags.save_length) = -1;
1252 XFASTINT (buffer_local_flags.auto_save_file_name) = -1;
1253 XFASTINT (buffer_local_flags.read_only) = -1;
1254 XFASTINT (buffer_local_flags.major_mode) = -1;
1255 XFASTINT (buffer_local_flags.mode_name) = -1;
1256 XFASTINT (buffer_local_flags.undo_list) = -1;
1257
1258 XFASTINT (buffer_local_flags.mode_line_format) = 1;
1259 XFASTINT (buffer_local_flags.abbrev_mode) = 2;
1260 XFASTINT (buffer_local_flags.overwrite_mode) = 4;
1261 XFASTINT (buffer_local_flags.case_fold_search) = 8;
1262 XFASTINT (buffer_local_flags.auto_fill_function) = 0x10;
1263 XFASTINT (buffer_local_flags.selective_display) = 0x20;
1264#ifndef old
1265 XFASTINT (buffer_local_flags.selective_display_ellipses) = 0x40;
1266#endif
1267 XFASTINT (buffer_local_flags.tab_width) = 0x80;
1268 XFASTINT (buffer_local_flags.truncate_lines) = 0x100;
1269 XFASTINT (buffer_local_flags.ctl_arrow) = 0x200;
1270 XFASTINT (buffer_local_flags.fill_column) = 0x400;
1271 XFASTINT (buffer_local_flags.left_margin) = 0x800;
1272 XFASTINT (buffer_local_flags.abbrev_table) = 0x1000;
1273 XFASTINT (buffer_local_flags.display_table) = 0x2000;
1274 XFASTINT (buffer_local_flags.fieldlist) = 0x4000;
1275 XFASTINT (buffer_local_flags.syntax_table) = 0x8000;
1276
1277 Vbuffer_alist = Qnil;
1278 current_buffer = 0;
1279 all_buffers = 0;
1280
1281 QSFundamental = build_string ("Fundamental");
1282
1283 Qfundamental_mode = intern ("fundamental-mode");
1284 buffer_defaults.major_mode = Qfundamental_mode;
1285
1286 Qmode_class = intern ("mode-class");
1287
1288 Qprotected_field = intern ("protected-field");
1289
1290 Qpermanent_local = intern ("permanent-local");
1291
1292 Qkill_buffer_hook = intern ("kill-buffer-hook");
1293
1294 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
1295 /* super-magic invisible buffer */
1296 Vbuffer_alist = Qnil;
1297
ffd56f97 1298 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1ab256cb
RM
1299}
1300
1301init_buffer ()
1302{
1303 char buf[MAXPATHLEN+1];
1304
1305 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
1306 if (getwd (buf) == 0)
1307 fatal ("`getwd' failed: %s.\n", buf);
1308
1309#ifndef VMS
1310 /* Maybe this should really use some standard subroutine
1311 whose definition is filename syntax dependent. */
1312 if (buf[strlen (buf) - 1] != '/')
1313 strcat (buf, "/");
1314#endif /* not VMS */
1315 current_buffer->directory = build_string (buf);
1316}
1317
1318/* initialize the buffer routines */
1319syms_of_buffer ()
1320{
1321 staticpro (&Vbuffer_defaults);
1322 staticpro (&Vbuffer_local_symbols);
1323 staticpro (&Qfundamental_mode);
1324 staticpro (&Qmode_class);
1325 staticpro (&QSFundamental);
1326 staticpro (&Vbuffer_alist);
1327 staticpro (&Qprotected_field);
1328 staticpro (&Qpermanent_local);
1329 staticpro (&Qkill_buffer_hook);
1330
1331 Fput (Qprotected_field, Qerror_conditions,
1332 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
1333 Fput (Qprotected_field, Qerror_message,
1334 build_string ("Attempt to modify a protected field"));
1335
1336 /* All these use DEFVAR_LISP_NOPRO because the slots in
1337 buffer_defaults will all be marked via Vbuffer_defaults. */
1338
1339 DEFVAR_LISP_NOPRO ("default-mode-line-format",
1340 &buffer_defaults.mode_line_format,
1341 "Default value of `mode-line-format' for buffers that don't override it.\n\
1342This is the same as (default-value 'mode-line-format).");
1343
1344 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
1345 &buffer_defaults.abbrev_mode,
1346 "Default value of `abbrev-mode' for buffers that do not override it.\n\
1347This is the same as (default-value 'abbrev-mode).");
1348
1349 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
1350 &buffer_defaults.ctl_arrow,
1351 "Default value of `ctl-arrow' for buffers that do not override it.\n\
1352This is the same as (default-value 'ctl-arrow).");
1353
1354 DEFVAR_LISP_NOPRO ("default-truncate-lines",
1355 &buffer_defaults.truncate_lines,
1356 "Default value of `truncate-lines' for buffers that do not override it.\n\
1357This is the same as (default-value 'truncate-lines).");
1358
1359 DEFVAR_LISP_NOPRO ("default-fill-column",
1360 &buffer_defaults.fill_column,
1361 "Default value of `fill-column' for buffers that do not override it.\n\
1362This is the same as (default-value 'fill-column).");
1363
1364 DEFVAR_LISP_NOPRO ("default-left-margin",
1365 &buffer_defaults.left_margin,
1366 "Default value of `left-margin' for buffers that do not override it.\n\
1367This is the same as (default-value 'left-margin).");
1368
1369 DEFVAR_LISP_NOPRO ("default-tab-width",
1370 &buffer_defaults.tab_width,
1371 "Default value of `tab-width' for buffers that do not override it.\n\
1372This is the same as (default-value 'tab-width).");
1373
1374 DEFVAR_LISP_NOPRO ("default-case-fold-search",
1375 &buffer_defaults.case_fold_search,
1376 "Default value of `case-fold-search' for buffers that don't override it.\n\
1377This is the same as (default-value 'case-fold-search).");
1378
1379 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format, 0);
1380
1381/* This doc string is too long for cpp; cpp dies if it isn't in a comment.
1382 But make-docfile finds it!
1383 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
1384 "Template for displaying mode line for current buffer.\n\
1385Each buffer has its own value of this variable.\n\
1386Value may be a string, a symbol or a list or cons cell.\n\
1387For a symbol, its value is used (but it is ignored if t or nil).\n\
1388 A string appearing directly as the value of a symbol is processed verbatim\n\
1389 in that the %-constructs below are not recognized.\n\
1390For a list whose car is a symbol, the symbol's value is taken,\n\
1391 and if that is non-nil, the cadr of the list is processed recursively.\n\
1392 Otherwise, the caddr of the list (if there is one) is processed.\n\
1393For a list whose car is a string or list, each element is processed\n\
1394 recursively and the results are effectively concatenated.\n\
1395For a list whose car is an integer, the cdr of the list is processed\n\
1396 and padded (if the number is positive) or truncated (if negative)\n\
1397 to the width specified by that number.\n\
1398A string is printed verbatim in the mode line except for %-constructs:\n\
1399 (%-constructs are allowed when the string is the entire mode-line-format\n\
1400 or when it is found in a cons-cell or a list)\n\
1401 %b -- print buffer name. %f -- print visited file name.\n\
1402 %* -- print *, % or hyphen. %m -- print value of mode-name (obsolete).\n\
1403 %s -- print process status. %M -- print value of global-mode-string. (obs)\n\
1404 %p -- print percent of buffer above top of window, or top, bot or all.\n\
1405 %n -- print Narrow if appropriate.\n\
1406 %[ -- print one [ for each recursive editing level. %] similar.\n\
1407 %% -- print %. %- -- print infinitely many dashes.\n\
1408Decimal digits after the % specify field width to which to pad.");
1409*/
1410
1411 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
1412 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
1413nil here means use current buffer's major mode.");
1414
1415 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
1416 "Symbol for current buffer's major mode.");
1417
1418 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
1419 "Pretty name of current buffer's major mode (a string).");
1420
1421 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode,
1422 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
1423Automatically becomes buffer-local when set in any fashion.");
1424
1425 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
1426 "*Non-nil if searches should ignore case.\n\
1427Automatically becomes buffer-local when set in any fashion.");
1428
1429 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
1430 "*Column beyond which automatic line-wrapping should happen.\n\
1431Automatically becomes buffer-local when set in any fashion.");
1432
1433 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
1434 "*Column for the default indent-line-function to indent to.\n\
1435Linefeed indents to this column in Fundamental mode.\n\
1436Automatically becomes buffer-local when set in any fashion.");
1437
1438 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
1439 "*Distance between tab stops (for display of tab characters), in columns.\n\
1440Automatically becomes buffer-local when set in any fashion.");
1441
1442 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow,
1443 "*Non-nil means display control chars with uparrow.\n\
1444Nil means use backslash and octal digits.\n\
1445Automatically becomes buffer-local when set in any fashion.\n\
1446This variable does not apply to characters whose display is specified\n\
1447in the current display table (if there is one).");
1448
1449 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines,
1450 "*Non-nil means do not display continuation lines;\n\
1451give each line of text one screen line.\n\
1452Automatically becomes buffer-local when set in any fashion.\n\
1453\n\
1454Note that this is overridden by the variable\n\
1455`truncate-partial-width-windows' if that variable is non-nil\n\
1456and this buffer is not full-screen width.");
1457
1458 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
1459 "Name of default directory of current buffer. Should end with slash.\n\
1460Each buffer has its own value of this variable.");
1461
1462 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
1463 "Function called (if non-nil) to perform auto-fill.\n\
1464It is called after self-inserting a space at a column beyond `fill-column'.\n\
1465Each buffer has its own value of this variable.\n\
1466NOTE: This variable is not an ordinary hook;\n\
1467It may not be a list of functions.");
1468
1469 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
1470 "Name of file visited in current buffer, or nil if not visiting a file.\n\
1471Each buffer has its own value of this variable.");
1472
1473 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
1474 &current_buffer->auto_save_file_name,
1475 "Name of file for auto-saving current buffer,\n\
1476or nil if buffer should not be auto-saved.\n\
1477Each buffer has its own value of this variable.");
1478
1479 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only,
1480 "Non-nil if this buffer is read-only.\n\
1481Each buffer has its own value of this variable.");
1482
1483 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up,
1484 "Non-nil if this buffer's file has been backed up.\n\
1485Backing up is done before the first time the file is saved.\n\
1486Each buffer has its own value of this variable.");
1487
1488 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
1489 "Length of current buffer when last read in, saved or auto-saved.\n\
14900 initially.\n\
1491Each buffer has its own value of this variable.");
1492
1493 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
1494 "Non-nil enables selective display:\n\
1495Integer N as value means display only lines\n\
1496 that start with less than n columns of space.\n\
1497A value of t means, after a ^M, all the rest of the line is invisible.\n\
1498 Then ^M's in the file are written into files as newlines.\n\n\
1499Automatically becomes buffer-local when set in any fashion.");
1500
1501#ifndef old
1502 DEFVAR_PER_BUFFER ("selective-display-ellipses",
1503 &current_buffer->selective_display_ellipses,
1504 "t means display ... on previous line when a line is invisible.\n\
1505Automatically becomes buffer-local when set in any fashion.");
1506#endif
1507
1508 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode,
1509 "Non-nil if self-insertion should replace existing text.\n\
1510Automatically becomes buffer-local when set in any fashion.");
1511
1512 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
1513 "Display table that controls display of the contents of current buffer.\n\
1514Automatically becomes buffer-local when set in any fashion.\n\
1515The display table is a vector created with `make-display-table'.\n\
1516The first 256 elements control how to display each possible text character.\n\
1517The value should be a \"rope\" (see `make-rope') or nil;\n\
1518nil means display the character in the default fashion.\n\
1519The remaining five elements are ropes that control the display of\n\
1520 the end of a truncated screen line (element 256);\n\
1521 the end of a continued line (element 257);\n\
1522 the escape character used to display character codes in octal (element 258);\n\
1523 the character used as an arrow for control characters (element 259);\n\
1524 the decoration indicating the presence of invisible lines (element 260).\n\
1525If this variable is nil, the value of `standard-display-table' is used.\n\
1526Each window can have its own, overriding display table.");
1527
1528 DEFVAR_PER_BUFFER ("buffer-field-list", &current_buffer->fieldlist,
1529 "List of fields in the current buffer. See `add-field'.");
1530
1531 DEFVAR_BOOL ("check-protected-fields", check_protected_fields,
1532 "Non-nil means don't allow modification of a protected field.\n\
1533See `add-field'.");
1534 check_protected_fields = 0;
1535
1536/*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
1537 "Don't ask.");
1538*/
01050cb5 1539 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
1ab256cb
RM
1540 "Function to call before each text change.\n\
1541Two arguments are passed to the function: the positions of\n\
1542the beginning and end of the range of old text to be changed.\n\
1543\(For an insertion, the beginning and end are at the same place.)\n\
1544No information is given about the length of the text after the change.\n\
1545position of the change\n\
1546\n\
1547While executing the `before-change-function', changes to buffers do not\n\
1548cause calls to any `before-change-function' or `after-change-function'.");
1549 Vbefore_change_function = Qnil;
1550
1551 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
1552 "Function to call after each text change.\n\
1553Three arguments are passed to the function: the positions of\n\
1554the beginning and end of the range of changed text,\n\
1555and the length of the pre-change text replaced by that range.\n\
1556\(For an insertion, the pre-change length is zero;\n\
1557for a deletion, that length is the number of characters deleted,\n\
1558and the post-change beginning and end are at the same place.)\n\
1559\n\
1560While executing the `after-change-function', changes to buffers do not\n\
1561cause calls to any `before-change-function' or `after-change-function'.");
1562 Vafter_change_function = Qnil;
1563
1564 DEFVAR_LISP ("first-change-function", &Vfirst_change_function,
1565 "Function to call before changing a buffer which is unmodified.\n\
1566The function is called, with no arguments, if it is non-nil.");
1567 Vfirst_change_function = Qnil;
1568
1569 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list,
1570 "List of undo entries in current buffer.\n\
1571Recent changes come first; older changes follow newer.\n\
1572\n\
1573An entry (START . END) represents an insertion which begins at\n\
1574position START and ends at position END.\n\
1575\n\
1576An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
1577from (abs POSITION). If POSITION is positive, point was at the front\n\
1578of the text being deleted; if negative, point was at the end.\n\
1579\n\
1580An entry (t HIGHWORD LOWWORD) indicates that the buffer had been\n\
1581previously unmodified. HIGHWORD and LOWWORD are the high and low\n\
158216-bit words of the buffer's modification count at the time. If the\n\
1583modification count of the most recent save is different, this entry is\n\
1584obsolete.\n\
1585\n\
1586nil marks undo boundaries. The undo command treats the changes\n\
1587between two undo boundaries as a single step to be undone.\n\
1588\n\
1589If the value of the variable is t, undo information is not recorded.\n\
1590");
1591
1592 defsubr (&Sbuffer_list);
1593 defsubr (&Sget_buffer);
1594 defsubr (&Sget_file_buffer);
1595 defsubr (&Sget_buffer_create);
01050cb5 1596 defsubr (&Sgenerate_new_buffer_name);
1ab256cb
RM
1597 defsubr (&Sbuffer_name);
1598/*defsubr (&Sbuffer_number);*/
1599 defsubr (&Sbuffer_file_name);
1600 defsubr (&Sbuffer_local_variables);
1601 defsubr (&Sbuffer_modified_p);
1602 defsubr (&Sset_buffer_modified_p);
1603 defsubr (&Sbuffer_modified_tick);
1604 defsubr (&Srename_buffer);
1605 defsubr (&Sother_buffer);
1606 defsubr (&Sbuffer_disable_undo);
1607 defsubr (&Sbuffer_enable_undo);
1608 defsubr (&Skill_buffer);
1609 defsubr (&Serase_buffer);
1610 defsubr (&Sswitch_to_buffer);
1611 defsubr (&Spop_to_buffer);
1612 defsubr (&Scurrent_buffer);
1613 defsubr (&Sset_buffer);
1614 defsubr (&Sbarf_if_buffer_read_only);
1615 defsubr (&Sbury_buffer);
1616 defsubr (&Slist_buffers);
1617 defsubr (&Skill_all_local_variables);
1618 defsubr (&Sregion_fields);
1619}
1620
1621keys_of_buffer ()
1622{
1623 initial_define_key (control_x_map, 'b', "switch-to-buffer");
1624 initial_define_key (control_x_map, 'k', "kill-buffer");
1625 initial_define_key (control_x_map, Ctl ('B'), "list-buffers");
1626}