(init_buffer): Call Fset_buffer_multibyte for *scratch*.
[bpt/emacs.git] / src / buffer.c
1 /* Buffer manipulation primitives for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26 #include <errno.h>
27
28 extern int errno;
29
30 #ifndef MAXPATHLEN
31 /* in 4.1, param.h fails to define this. */
32 #define MAXPATHLEN 1024
33 #endif /* not MAXPATHLEN */
34
35 #include <config.h>
36 #include "lisp.h"
37 #include "intervals.h"
38 #include "window.h"
39 #include "commands.h"
40 #include "buffer.h"
41 #include "charset.h"
42 #include "region-cache.h"
43 #include "indent.h"
44 #include "blockinput.h"
45 #include "frame.h"
46
47 struct buffer *current_buffer; /* the current buffer */
48
49 /* First buffer in chain of all buffers (in reverse order of creation).
50 Threaded through ->next. */
51
52 struct buffer *all_buffers;
53
54 /* This structure holds the default values of the buffer-local variables
55 defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
56 The default value occupies the same slot in this structure
57 as an individual buffer's value occupies in that buffer.
58 Setting the default value also goes through the alist of buffers
59 and stores into each buffer that does not say it has a local value. */
60
61 struct buffer buffer_defaults;
62
63 /* A Lisp_Object pointer to the above, used for staticpro */
64
65 static Lisp_Object Vbuffer_defaults;
66
67 /* This structure marks which slots in a buffer have corresponding
68 default values in buffer_defaults.
69 Each such slot has a nonzero value in this structure.
70 The value has only one nonzero bit.
71
72 When a buffer has its own local value for a slot,
73 the bit for that slot (found in the same slot in this structure)
74 is turned on in the buffer's local_var_flags slot.
75
76 If a slot in this structure is -1, then even though there may
77 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
78 and the corresponding slot in buffer_defaults is not used.
79
80 If a slot is -2, then there is no DEFVAR_PER_BUFFER for it,
81 but there is a default value which is copied into each buffer.
82
83 If a slot in this structure is negative, then even though there may
84 be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
85 and the corresponding slot in buffer_defaults is not used.
86
87 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
88 zero, that is a bug */
89
90 struct buffer buffer_local_flags;
91
92 /* This structure holds the names of symbols whose values may be
93 buffer-local. It is indexed and accessed in the same way as the above. */
94
95 struct buffer buffer_local_symbols;
96 /* A Lisp_Object pointer to the above, used for staticpro */
97 static Lisp_Object Vbuffer_local_symbols;
98
99 /* This structure holds the required types for the values in the
100 buffer-local slots. If a slot contains Qnil, then the
101 corresponding buffer slot may contain a value of any type. If a
102 slot contains an integer, then prospective values' tags must be
103 equal to that integer (except nil is always allowed).
104 When a tag does not match, the function
105 buffer_slot_type_mismatch will signal an error.
106
107 If a slot here contains -1, the corresponding variable is read-only. */
108 struct buffer buffer_local_types;
109
110 /* Flags indicating which built-in buffer-local variables
111 are permanent locals. */
112 static int buffer_permanent_local_flags;
113
114 Lisp_Object Fset_buffer ();
115 void set_buffer_internal ();
116 void set_buffer_internal_1 ();
117 static void call_overlay_mod_hooks ();
118 static void swap_out_buffer_local_variables ();
119 static void reset_buffer_local_variables ();
120
121 /* Alist of all buffer names vs the buffers. */
122 /* This used to be a variable, but is no longer,
123 to prevent lossage due to user rplac'ing this alist or its elements. */
124 Lisp_Object Vbuffer_alist;
125
126 /* Functions to call before and after each text change. */
127 Lisp_Object Vbefore_change_function;
128 Lisp_Object Vafter_change_function;
129 Lisp_Object Vbefore_change_functions;
130 Lisp_Object Vafter_change_functions;
131
132 Lisp_Object Vtransient_mark_mode;
133
134 /* t means ignore all read-only text properties.
135 A list means ignore such a property if its value is a member of the list.
136 Any non-nil value means ignore buffer-read-only. */
137 Lisp_Object Vinhibit_read_only;
138
139 /* List of functions to call that can query about killing a buffer.
140 If any of these functions returns nil, we don't kill it. */
141 Lisp_Object Vkill_buffer_query_functions;
142
143 /* List of functions to call before changing an unmodified buffer. */
144 Lisp_Object Vfirst_change_hook;
145
146 Lisp_Object Qfirst_change_hook;
147 Lisp_Object Qbefore_change_functions;
148 Lisp_Object Qafter_change_functions;
149
150 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
151
152 Lisp_Object Qprotected_field;
153
154 Lisp_Object QSFundamental; /* A string "Fundamental" */
155
156 Lisp_Object Qkill_buffer_hook;
157
158 Lisp_Object Qget_file_buffer;
159
160 Lisp_Object Qoverlayp;
161
162 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
163
164 Lisp_Object Qmodification_hooks;
165 Lisp_Object Qinsert_in_front_hooks;
166 Lisp_Object Qinsert_behind_hooks;
167
168 /* For debugging; temporary. See set_buffer_internal. */
169 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
170
171 void
172 nsberror (spec)
173 Lisp_Object spec;
174 {
175 if (STRINGP (spec))
176 error ("No buffer named %s", XSTRING (spec)->data);
177 error ("Invalid buffer argument");
178 }
179 \f
180 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
181 "Return non-nil if OBJECT is a buffer which has not been killed.\n\
182 Value is nil if OBJECT is not a buffer or if it has been killed.")
183 (object)
184 Lisp_Object object;
185 {
186 return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
187 ? Qt : Qnil);
188 }
189
190 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
191 "Return a list of all existing live buffers.\n\
192 If the optional arg FRAME is a frame, we return that frame's buffer list.")
193 (frame)
194 Lisp_Object frame;
195 {
196 Lisp_Object framelist, general;
197 general = Fmapcar (Qcdr, Vbuffer_alist);
198
199 if (FRAMEP (frame))
200 {
201 Lisp_Object tail;
202
203 CHECK_FRAME (frame, 1);
204
205 framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
206
207 /* Remove from GENERAL any buffer that duplicates one in FRAMELIST. */
208 tail = framelist;
209 while (! NILP (tail))
210 {
211 general = Fdelq (XCONS (tail)->car, general);
212 tail = XCONS (tail)->cdr;
213 }
214 return nconc2 (framelist, general);
215 }
216
217 return general;
218 }
219
220 /* Like Fassoc, but use Fstring_equal to compare
221 (which ignores text properties),
222 and don't ever QUIT. */
223
224 static Lisp_Object
225 assoc_ignore_text_properties (key, list)
226 register Lisp_Object key;
227 Lisp_Object list;
228 {
229 register Lisp_Object tail;
230 for (tail = list; !NILP (tail); tail = Fcdr (tail))
231 {
232 register Lisp_Object elt, tem;
233 elt = Fcar (tail);
234 tem = Fstring_equal (Fcar (elt), key);
235 if (!NILP (tem))
236 return elt;
237 }
238 return Qnil;
239 }
240
241 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
242 "Return the buffer named NAME (a string).\n\
243 If there is no live buffer named NAME, return nil.\n\
244 NAME may also be a buffer; if so, the value is that buffer.")
245 (name)
246 register Lisp_Object name;
247 {
248 if (BUFFERP (name))
249 return name;
250 CHECK_STRING (name, 0);
251
252 return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
253 }
254
255 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
256 "Return the buffer visiting file FILENAME (a string).\n\
257 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.\n\
258 If there is no such live buffer, return nil.\n\
259 See also `find-buffer-visiting'.")
260 (filename)
261 register Lisp_Object filename;
262 {
263 register Lisp_Object tail, buf, tem;
264 Lisp_Object handler;
265
266 CHECK_STRING (filename, 0);
267 filename = Fexpand_file_name (filename, Qnil);
268
269 /* If the file name has special constructs in it,
270 call the corresponding file handler. */
271 handler = Ffind_file_name_handler (filename, Qget_file_buffer);
272 if (!NILP (handler))
273 return call2 (handler, Qget_file_buffer, filename);
274
275 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
276 {
277 buf = Fcdr (XCONS (tail)->car);
278 if (!BUFFERP (buf)) continue;
279 if (!STRINGP (XBUFFER (buf)->filename)) continue;
280 tem = Fstring_equal (XBUFFER (buf)->filename, filename);
281 if (!NILP (tem))
282 return buf;
283 }
284 return Qnil;
285 }
286
287 Lisp_Object
288 get_truename_buffer (filename)
289 register Lisp_Object filename;
290 {
291 register Lisp_Object tail, buf, tem;
292
293 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
294 {
295 buf = Fcdr (XCONS (tail)->car);
296 if (!BUFFERP (buf)) continue;
297 if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
298 tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
299 if (!NILP (tem))
300 return buf;
301 }
302 return Qnil;
303 }
304
305 /* Incremented for each buffer created, to assign the buffer number. */
306 int buffer_count;
307
308 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
309 "Return the buffer named NAME, or create such a buffer and return it.\n\
310 A new buffer is created if there is no live buffer named NAME.\n\
311 If NAME starts with a space, the new buffer does not keep undo information.\n\
312 If NAME is a buffer instead of a string, then it is the value returned.\n\
313 The value is never nil.")
314 (name)
315 register Lisp_Object name;
316 {
317 register Lisp_Object buf;
318 register struct buffer *b;
319
320 buf = Fget_buffer (name);
321 if (!NILP (buf))
322 return buf;
323
324 if (XSTRING (name)->size == 0)
325 error ("Empty string for buffer name is not allowed");
326
327 b = (struct buffer *) xmalloc (sizeof (struct buffer));
328
329 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
330
331 /* An ordinary buffer uses its own struct buffer_text. */
332 b->text = &b->own_text;
333 b->base_buffer = 0;
334
335 BUF_GAP_SIZE (b) = 20;
336 BLOCK_INPUT;
337 /* We allocate extra 1-byte at the tail and keep it always '\0' for
338 anchoring a search. */
339 BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
340 UNBLOCK_INPUT;
341 if (! BUF_BEG_ADDR (b))
342 buffer_memory_full ();
343
344 BUF_PT (b) = 1;
345 BUF_GPT (b) = 1;
346 BUF_BEGV (b) = 1;
347 BUF_ZV (b) = 1;
348 BUF_Z (b) = 1;
349 BUF_PT_BYTE (b) = 1;
350 BUF_GPT_BYTE (b) = 1;
351 BUF_BEGV_BYTE (b) = 1;
352 BUF_ZV_BYTE (b) = 1;
353 BUF_Z_BYTE (b) = 1;
354 BUF_MODIFF (b) = 1;
355 BUF_OVERLAY_MODIFF (b) = 1;
356 BUF_SAVE_MODIFF (b) = 1;
357 BUF_INTERVALS (b) = 0;
358 *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */
359
360 b->newline_cache = 0;
361 b->width_run_cache = 0;
362 b->width_table = Qnil;
363
364 /* Put this on the chain of all buffers including killed ones. */
365 b->next = all_buffers;
366 all_buffers = b;
367
368 /* An ordinary buffer normally doesn't need markers
369 to handle BEGV and ZV. */
370 b->pt_marker = Qnil;
371 b->begv_marker = Qnil;
372 b->zv_marker = Qnil;
373
374 name = Fcopy_sequence (name);
375 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
376 b->name = name;
377
378 if (XSTRING (name)->data[0] != ' ')
379 b->undo_list = Qnil;
380 else
381 b->undo_list = Qt;
382
383 reset_buffer (b);
384 reset_buffer_local_variables (b, 1);
385
386 /* Put this in the alist of all live buffers. */
387 XSETBUFFER (buf, b);
388 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
389
390 b->mark = Fmake_marker ();
391 BUF_MARKERS (b) = Qnil;
392 b->name = name;
393 return buf;
394 }
395
396 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
397 "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
398 "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
399 BASE-BUFFER should be an existing buffer (or buffer name).\n\
400 NAME should be a string which is not the name of an existing buffer.")
401 (base_buffer, name)
402 register Lisp_Object base_buffer, name;
403 {
404 register Lisp_Object buf;
405 register struct buffer *b;
406
407 buf = Fget_buffer (name);
408 if (!NILP (buf))
409 error ("Buffer name `%s' is in use", XSTRING (name)->data);
410
411 base_buffer = Fget_buffer (base_buffer);
412 if (NILP (base_buffer))
413 error ("No such buffer: `%s'",
414 XSTRING (XBUFFER (base_buffer)->name)->data);
415
416 if (XSTRING (name)->size == 0)
417 error ("Empty string for buffer name is not allowed");
418
419 b = (struct buffer *) xmalloc (sizeof (struct buffer));
420
421 b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
422
423 if (XBUFFER (base_buffer)->base_buffer)
424 b->base_buffer = XBUFFER (base_buffer)->base_buffer;
425 else
426 b->base_buffer = XBUFFER (base_buffer);
427
428 /* Use the base buffer's text object. */
429 b->text = b->base_buffer->text;
430
431 BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
432 BUF_ZV (b) = BUF_ZV (b->base_buffer);
433 BUF_PT (b) = BUF_PT (b->base_buffer);
434 BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
435 BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
436 BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
437
438 b->newline_cache = 0;
439 b->width_run_cache = 0;
440 b->width_table = Qnil;
441
442 /* Put this on the chain of all buffers including killed ones. */
443 b->next = all_buffers;
444 all_buffers = b;
445
446 name = Fcopy_sequence (name);
447 INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
448 b->name = name;
449
450 reset_buffer (b);
451 reset_buffer_local_variables (b, 1);
452
453 /* Put this in the alist of all live buffers. */
454 XSETBUFFER (buf, b);
455 Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
456
457 b->mark = Fmake_marker ();
458 b->name = name;
459
460 /* Make sure the base buffer has markers for its narrowing. */
461 if (NILP (b->base_buffer->pt_marker))
462 {
463 b->base_buffer->pt_marker = Fmake_marker ();
464 set_marker_both (b->base_buffer->pt_marker, base_buffer,
465 BUF_PT (b->base_buffer),
466 BUF_PT_BYTE (b->base_buffer));
467 }
468 if (NILP (b->base_buffer->begv_marker))
469 {
470 b->base_buffer->begv_marker = Fmake_marker ();
471 set_marker_both (b->base_buffer->begv_marker, base_buffer,
472 BUF_BEGV (b->base_buffer),
473 BUF_BEGV_BYTE (b->base_buffer));
474 }
475 if (NILP (b->base_buffer->zv_marker))
476 {
477 b->base_buffer->zv_marker = Fmake_marker ();
478 set_marker_both (b->base_buffer->zv_marker, base_buffer,
479 BUF_ZV (b->base_buffer),
480 BUF_ZV_BYTE (b->base_buffer));
481 XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
482 }
483
484 /* Give the indirect buffer markers for its narrowing. */
485 b->pt_marker = Fmake_marker ();
486 set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
487 b->begv_marker = Fmake_marker ();
488 set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
489 b->zv_marker = Fmake_marker ();
490 set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
491 XMARKER (b->zv_marker)->insertion_type = 1;
492
493 return buf;
494 }
495
496 /* Reinitialize everything about a buffer except its name and contents
497 and local variables. */
498
499 void
500 reset_buffer (b)
501 register struct buffer *b;
502 {
503 b->filename = Qnil;
504 b->file_truename = Qnil;
505 b->directory = (current_buffer) ? current_buffer->directory : Qnil;
506 b->modtime = 0;
507 XSETFASTINT (b->save_length, 0);
508 b->last_window_start = 1;
509 /* It is more conservative to start out "changed" than "unchanged". */
510 b->clip_changed = 1;
511 b->backed_up = Qnil;
512 b->auto_save_modified = 0;
513 b->auto_save_failure_time = -1;
514 b->auto_save_file_name = Qnil;
515 b->read_only = Qnil;
516 b->overlays_before = Qnil;
517 b->overlays_after = Qnil;
518 XSETFASTINT (b->overlay_center, 1);
519 b->mark_active = Qnil;
520 b->point_before_scroll = Qnil;
521 b->file_format = Qnil;
522 b->last_selected_window = Qnil;
523 XSETINT (b->display_count, 0);
524 b->extra2 = Qnil;
525 b->extra3 = Qnil;
526 b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
527 }
528
529 /* Reset buffer B's local variables info.
530 Don't use this on a buffer that has already been in use;
531 it does not treat permanent locals consistently.
532 Instead, use Fkill_all_local_variables.
533
534 If PERMANENT_TOO is 1, then we reset permanent built-in
535 buffer-local variables. If PERMANENT_TOO is 0,
536 we preserve those. */
537
538 static void
539 reset_buffer_local_variables (b, permanent_too)
540 register struct buffer *b;
541 int permanent_too;
542 {
543 register int offset;
544 int dont_reset;
545
546 /* Decide which built-in local variables to reset. */
547 if (permanent_too)
548 dont_reset = 0;
549 else
550 dont_reset = buffer_permanent_local_flags;
551
552 /* Reset the major mode to Fundamental, together with all the
553 things that depend on the major mode.
554 default-major-mode is handled at a higher level.
555 We ignore it here. */
556 b->major_mode = Qfundamental_mode;
557 b->keymap = Qnil;
558 b->abbrev_table = Vfundamental_mode_abbrev_table;
559 b->mode_name = QSFundamental;
560 b->minor_modes = Qnil;
561
562 /* If the standard case table has been altered and invalidated,
563 fix up its insides first. */
564 if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
565 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
566 && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
567 Fset_standard_case_table (Vascii_downcase_table);
568
569 b->downcase_table = Vascii_downcase_table;
570 b->upcase_table = XCHAR_TABLE (Vascii_downcase_table)->extras[0];
571 b->case_canon_table = XCHAR_TABLE (Vascii_downcase_table)->extras[1];
572 b->case_eqv_table = XCHAR_TABLE (Vascii_downcase_table)->extras[2];
573 b->invisibility_spec = Qt;
574 #ifndef DOS_NT
575 b->buffer_file_type = Qnil;
576 #endif
577
578 #if 0
579 b->sort_table = XSTRING (Vascii_sort_table);
580 b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
581 #endif /* 0 */
582
583 /* Reset all (or most) per-buffer variables to their defaults. */
584 b->local_var_alist = Qnil;
585 b->local_var_flags &= dont_reset;
586
587 /* For each slot that has a default value,
588 copy that into the slot. */
589
590 for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
591 offset < sizeof (struct buffer);
592 offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
593 {
594 int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
595 if ((flag > 0
596 /* Don't reset a permanent local. */
597 && ! (dont_reset & flag))
598 || flag == -2)
599 *(Lisp_Object *)(offset + (char *)b)
600 = *(Lisp_Object *)(offset + (char *)&buffer_defaults);
601 }
602 }
603
604 /* We split this away from generate-new-buffer, because rename-buffer
605 and set-visited-file-name ought to be able to use this to really
606 rename the buffer properly. */
607
608 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, Sgenerate_new_buffer_name,
609 1, 2, 0,
610 "Return a string that is the name of no existing buffer based on NAME.\n\
611 If there is no live buffer named NAME, then return NAME.\n\
612 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER\n\
613 until an unused name is found, and then return that name.\n\
614 Optional second argument IGNORE specifies a name that is okay to use\n\
615 \(if it is in the sequence to be tried)\n\
616 even if a buffer with that name exists.")
617 (name, ignore)
618 register Lisp_Object name, ignore;
619 {
620 register Lisp_Object gentemp, tem;
621 int count;
622 char number[10];
623
624 CHECK_STRING (name, 0);
625
626 tem = Fget_buffer (name);
627 if (NILP (tem))
628 return name;
629
630 count = 1;
631 while (1)
632 {
633 sprintf (number, "<%d>", ++count);
634 gentemp = concat2 (name, build_string (number));
635 tem = Fstring_equal (gentemp, ignore);
636 if (!NILP (tem))
637 return gentemp;
638 tem = Fget_buffer (gentemp);
639 if (NILP (tem))
640 return gentemp;
641 }
642 }
643
644 \f
645 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
646 "Return the name of BUFFER, as a string.\n\
647 With no argument or nil as argument, return the name of the current buffer.")
648 (buffer)
649 register Lisp_Object buffer;
650 {
651 if (NILP (buffer))
652 return current_buffer->name;
653 CHECK_BUFFER (buffer, 0);
654 return XBUFFER (buffer)->name;
655 }
656
657 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
658 "Return name of file BUFFER is visiting, or nil if none.\n\
659 No argument or nil as argument means use the current buffer.")
660 (buffer)
661 register Lisp_Object buffer;
662 {
663 if (NILP (buffer))
664 return current_buffer->filename;
665 CHECK_BUFFER (buffer, 0);
666 return XBUFFER (buffer)->filename;
667 }
668
669 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
670 0, 1, 0,
671 "Return the base buffer of indirect buffer BUFFER.\n\
672 If BUFFER is not indirect, return nil.")
673 (buffer)
674 register Lisp_Object buffer;
675 {
676 struct buffer *base;
677 Lisp_Object base_buffer;
678
679 if (NILP (buffer))
680 base = current_buffer->base_buffer;
681 else
682 {
683 CHECK_BUFFER (buffer, 0);
684 base = XBUFFER (buffer)->base_buffer;
685 }
686
687 if (! base)
688 return Qnil;
689 XSETBUFFER (base_buffer, base);
690 return base_buffer;
691 }
692
693 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
694 Sbuffer_local_variables, 0, 1, 0,
695 "Return an alist of variables that are buffer-local in BUFFER.\n\
696 Most elements look like (SYMBOL . VALUE), describing one variable.\n\
697 For a symbol that is locally unbound, just the symbol appears in the value.\n\
698 Note that storing new VALUEs in these elements doesn't change the variables.\n\
699 No argument or nil as argument means use current buffer as BUFFER.")
700 (buffer)
701 register Lisp_Object buffer;
702 {
703 register struct buffer *buf;
704 register Lisp_Object result;
705
706 if (NILP (buffer))
707 buf = current_buffer;
708 else
709 {
710 CHECK_BUFFER (buffer, 0);
711 buf = XBUFFER (buffer);
712 }
713
714 result = Qnil;
715
716 {
717 register Lisp_Object tail;
718 for (tail = buf->local_var_alist; CONSP (tail); tail = XCONS (tail)->cdr)
719 {
720 Lisp_Object val, elt;
721
722 elt = XCONS (tail)->car;
723
724 /* Reference each variable in the alist in buf.
725 If inquiring about the current buffer, this gets the current values,
726 so store them into the alist so the alist is up to date.
727 If inquiring about some other buffer, this swaps out any values
728 for that buffer, making the alist up to date automatically. */
729 val = find_symbol_value (XCONS (elt)->car);
730 /* Use the current buffer value only if buf is the current buffer. */
731 if (buf != current_buffer)
732 val = XCONS (elt)->cdr;
733
734 /* If symbol is unbound, put just the symbol in the list. */
735 if (EQ (val, Qunbound))
736 result = Fcons (XCONS (elt)->car, result);
737 /* Otherwise, put (symbol . value) in the list. */
738 else
739 result = Fcons (Fcons (XCONS (elt)->car, val), result);
740 }
741 }
742
743 /* Add on all the variables stored in special slots. */
744 {
745 register int offset, mask;
746
747 for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
748 offset < sizeof (struct buffer);
749 offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
750 {
751 mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
752 if (mask == -1 || (buf->local_var_flags & mask))
753 if (SYMBOLP (*(Lisp_Object *)(offset
754 + (char *)&buffer_local_symbols)))
755 result = Fcons (Fcons (*((Lisp_Object *)
756 (offset + (char *)&buffer_local_symbols)),
757 *(Lisp_Object *)(offset + (char *)buf)),
758 result);
759 }
760 }
761
762 return result;
763 }
764
765 \f
766 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
767 0, 1, 0,
768 "Return t if BUFFER was modified since its file was last read or saved.\n\
769 No argument or nil as argument means use current buffer as BUFFER.")
770 (buffer)
771 register Lisp_Object buffer;
772 {
773 register struct buffer *buf;
774 if (NILP (buffer))
775 buf = current_buffer;
776 else
777 {
778 CHECK_BUFFER (buffer, 0);
779 buf = XBUFFER (buffer);
780 }
781
782 return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
783 }
784
785 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
786 1, 1, 0,
787 "Mark current buffer as modified or unmodified according to FLAG.\n\
788 A non-nil FLAG means mark the buffer modified.")
789 (flag)
790 register Lisp_Object flag;
791 {
792 register int already;
793 register Lisp_Object fn;
794
795 #ifdef CLASH_DETECTION
796 /* If buffer becoming modified, lock the file.
797 If buffer becoming unmodified, unlock the file. */
798
799 fn = current_buffer->file_truename;
800 if (!NILP (fn))
801 {
802 already = SAVE_MODIFF < MODIFF;
803 if (!already && !NILP (flag))
804 lock_file (fn);
805 else if (already && NILP (flag))
806 unlock_file (fn);
807 }
808 #endif /* CLASH_DETECTION */
809
810 SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
811 update_mode_lines++;
812 return flag;
813 }
814
815 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
816 0, 1, 0,
817 "Return BUFFER's tick counter, incremented for each change in text.\n\
818 Each buffer has a tick counter which is incremented each time the text in\n\
819 that buffer is changed. It wraps around occasionally.\n\
820 No argument or nil as argument means use current buffer as BUFFER.")
821 (buffer)
822 register Lisp_Object buffer;
823 {
824 register struct buffer *buf;
825 if (NILP (buffer))
826 buf = current_buffer;
827 else
828 {
829 CHECK_BUFFER (buffer, 0);
830 buf = XBUFFER (buffer);
831 }
832
833 return make_number (BUF_MODIFF (buf));
834 }
835 \f
836 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
837 "sRename buffer (to new name): \nP",
838 "Change current buffer's name to NEWNAME (a string).\n\
839 If second arg UNIQUE is nil or omitted, it is an error if a\n\
840 buffer named NEWNAME already exists.\n\
841 If UNIQUE is non-nil, come up with a new name using\n\
842 `generate-new-buffer-name'.\n\
843 Interactively, you can set UNIQUE with a prefix argument.\n\
844 We return the name we actually gave the buffer.\n\
845 This does not change the name of the visited file (if any).")
846 (newname, unique)
847 register Lisp_Object newname, unique;
848 {
849 register Lisp_Object tem, buf;
850
851 CHECK_STRING (newname, 0);
852
853 if (XSTRING (newname)->size == 0)
854 error ("Empty string is invalid as a buffer name");
855
856 tem = Fget_buffer (newname);
857 /* Don't short-circuit if UNIQUE is t. That is a useful way to rename
858 the buffer automatically so you can create another with the original name.
859 It makes UNIQUE equivalent to
860 (rename-buffer (generate-new-buffer-name NEWNAME)). */
861 if (NILP (unique) && XBUFFER (tem) == current_buffer)
862 return current_buffer->name;
863 if (!NILP (tem))
864 {
865 if (!NILP (unique))
866 newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
867 else
868 error ("Buffer name `%s' is in use", XSTRING (newname)->data);
869 }
870
871 current_buffer->name = newname;
872
873 /* Catch redisplay's attention. Unless we do this, the mode lines for
874 any windows displaying current_buffer will stay unchanged. */
875 update_mode_lines++;
876
877 XSETBUFFER (buf, current_buffer);
878 Fsetcar (Frassq (buf, Vbuffer_alist), newname);
879 if (NILP (current_buffer->filename)
880 && !NILP (current_buffer->auto_save_file_name))
881 call0 (intern ("rename-auto-save-file"));
882 /* Refetch since that last call may have done GC. */
883 return current_buffer->name;
884 }
885
886 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
887 "Return most recently selected buffer other than BUFFER.\n\
888 Buffers not visible in windows are preferred to visible buffers,\n\
889 unless optional second argument VISIBLE-OK is non-nil.\n\
890 If no other buffer exists, the buffer `*scratch*' is returned.\n\
891 If BUFFER is omitted or nil, some interesting buffer is returned.")
892 (buffer, visible_ok)
893 register Lisp_Object buffer, visible_ok;
894 {
895 Lisp_Object Fset_buffer_major_mode ();
896 register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
897 notsogood = Qnil;
898
899 tail = Vbuffer_alist;
900 pred = frame_buffer_predicate ();
901
902 /* Consider buffers that have been seen in the selected frame
903 before other buffers. */
904
905 tem = frame_buffer_list ();
906 add_ons = Qnil;
907 while (CONSP (tem))
908 {
909 if (BUFFERP (XCONS (tem)->car))
910 add_ons = Fcons (Fcons (Qnil, XCONS (tem)->car), add_ons);
911 tem = XCONS (tem)->cdr;
912 }
913 tail = nconc2 (Fnreverse (add_ons), tail);
914
915 for (; !NILP (tail); tail = Fcdr (tail))
916 {
917 buf = Fcdr (Fcar (tail));
918 if (EQ (buf, buffer))
919 continue;
920 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
921 continue;
922 /* If the selected frame has a buffer_predicate,
923 disregard buffers that don't fit the predicate. */
924 if (!NILP (pred))
925 {
926 tem = call1 (pred, buf);
927 if (NILP (tem))
928 continue;
929 }
930
931 if (NILP (visible_ok))
932 tem = Fget_buffer_window (buf, Qt);
933 else
934 tem = Qnil;
935 if (NILP (tem))
936 return buf;
937 if (NILP (notsogood))
938 notsogood = buf;
939 }
940 if (!NILP (notsogood))
941 return notsogood;
942 buf = Fget_buffer_create (build_string ("*scratch*"));
943 Fset_buffer_major_mode (buf);
944 return buf;
945 }
946 \f
947 DEFUN ("buffer-disable-undo", Fbuffer_disable_undo, Sbuffer_disable_undo,
948 0, 1, "",
949 "Make BUFFER stop keeping undo information.\n\
950 No argument or nil as argument means do this for the current buffer.")
951 (buffer)
952 register Lisp_Object buffer;
953 {
954 Lisp_Object real_buffer;
955
956 if (NILP (buffer))
957 XSETBUFFER (real_buffer, current_buffer);
958 else
959 {
960 real_buffer = Fget_buffer (buffer);
961 if (NILP (real_buffer))
962 nsberror (buffer);
963 }
964
965 XBUFFER (real_buffer)->undo_list = Qt;
966
967 return Qnil;
968 }
969
970 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
971 0, 1, "",
972 "Start keeping undo information for buffer BUFFER.\n\
973 No argument or nil as argument means do this for the current buffer.")
974 (buffer)
975 register Lisp_Object buffer;
976 {
977 Lisp_Object real_buffer;
978
979 if (NILP (buffer))
980 XSETBUFFER (real_buffer, current_buffer);
981 else
982 {
983 real_buffer = Fget_buffer (buffer);
984 if (NILP (real_buffer))
985 nsberror (buffer);
986 }
987
988 if (EQ (XBUFFER (real_buffer)->undo_list, Qt))
989 XBUFFER (real_buffer)->undo_list = Qnil;
990
991 return Qnil;
992 }
993
994 /*
995 DEFVAR_LISP ("kill-buffer-hook", no_cell, "\
996 Hook to be run (by `run-hooks', which see) when a buffer is killed.\n\
997 The buffer being killed will be current while the hook is running.\n\
998 See `kill-buffer'."
999 */
1000 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 1, 1, "bKill buffer: ",
1001 "Kill the buffer BUFFER.\n\
1002 The argument may be a buffer or may be the name of a buffer.\n\
1003 An argument of nil means kill the current buffer.\n\n\
1004 Value is t if the buffer is actually killed, nil if user says no.\n\n\
1005 The value of `kill-buffer-hook' (which may be local to that buffer),\n\
1006 if not void, is a list of functions to be called, with no arguments,\n\
1007 before the buffer is actually killed. The buffer to be killed is current\n\
1008 when the hook functions are called.\n\n\
1009 Any processes that have this buffer as the `process-buffer' are killed\n\
1010 with SIGHUP.")
1011 (buffer)
1012 Lisp_Object buffer;
1013 {
1014 Lisp_Object buf;
1015 register struct buffer *b;
1016 register Lisp_Object tem;
1017 register struct Lisp_Marker *m;
1018 struct gcpro gcpro1, gcpro2;
1019
1020 if (NILP (buffer))
1021 buf = Fcurrent_buffer ();
1022 else
1023 buf = Fget_buffer (buffer);
1024 if (NILP (buf))
1025 nsberror (buffer);
1026
1027 b = XBUFFER (buf);
1028
1029 /* Avoid trouble for buffer already dead. */
1030 if (NILP (b->name))
1031 return Qnil;
1032
1033 /* Query if the buffer is still modified. */
1034 if (INTERACTIVE && !NILP (b->filename)
1035 && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
1036 {
1037 GCPRO1 (buf);
1038 tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
1039 XSTRING (b->name)->data));
1040 UNGCPRO;
1041 if (NILP (tem))
1042 return Qnil;
1043 }
1044
1045 /* Run hooks with the buffer to be killed the current buffer. */
1046 {
1047 register Lisp_Object val;
1048 int count = specpdl_ptr - specpdl;
1049 Lisp_Object list;
1050
1051 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1052 set_buffer_internal (b);
1053
1054 /* First run the query functions; if any query is answered no,
1055 don't kill the buffer. */
1056 for (list = Vkill_buffer_query_functions; !NILP (list); list = Fcdr (list))
1057 {
1058 tem = call0 (Fcar (list));
1059 if (NILP (tem))
1060 return unbind_to (count, Qnil);
1061 }
1062
1063 /* Then run the hooks. */
1064 if (!NILP (Vrun_hooks))
1065 call1 (Vrun_hooks, Qkill_buffer_hook);
1066 unbind_to (count, Qnil);
1067 }
1068
1069 /* We have no more questions to ask. Verify that it is valid
1070 to kill the buffer. This must be done after the questions
1071 since anything can happen within do_yes_or_no_p. */
1072
1073 /* Don't kill the minibuffer now current. */
1074 if (EQ (buf, XWINDOW (minibuf_window)->buffer))
1075 return Qnil;
1076
1077 if (NILP (b->name))
1078 return Qnil;
1079
1080 /* When we kill a base buffer, kill all its indirect buffers.
1081 We do it at this stage so nothing terrible happens if they
1082 ask questions or their hooks get errors. */
1083 if (! b->base_buffer)
1084 {
1085 struct buffer *other;
1086
1087 GCPRO1 (buf);
1088
1089 for (other = all_buffers; other; other = other->next)
1090 /* all_buffers contains dead buffers too;
1091 don't re-kill them. */
1092 if (other->base_buffer == b && !NILP (other->name))
1093 {
1094 Lisp_Object buf;
1095 XSETBUFFER (buf, other);
1096 Fkill_buffer (buf);
1097 }
1098
1099 UNGCPRO;
1100 }
1101
1102 /* Make this buffer not be current.
1103 In the process, notice if this is the sole visible buffer
1104 and give up if so. */
1105 if (b == current_buffer)
1106 {
1107 tem = Fother_buffer (buf, Qnil);
1108 Fset_buffer (tem);
1109 if (b == current_buffer)
1110 return Qnil;
1111 }
1112
1113 /* Now there is no question: we can kill the buffer. */
1114
1115 #ifdef CLASH_DETECTION
1116 /* Unlock this buffer's file, if it is locked. */
1117 unlock_buffer (b);
1118 #endif /* CLASH_DETECTION */
1119
1120 kill_buffer_processes (buf);
1121
1122 tem = Vinhibit_quit;
1123 Vinhibit_quit = Qt;
1124 replace_buffer_in_all_windows (buf);
1125 Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
1126 frames_discard_buffer (buf);
1127 Vinhibit_quit = tem;
1128
1129 /* Delete any auto-save file, if we saved it in this session. */
1130 if (STRINGP (b->auto_save_file_name)
1131 && b->auto_save_modified != 0
1132 && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
1133 {
1134 Lisp_Object tem;
1135 tem = Fsymbol_value (intern ("delete-auto-save-files"));
1136 if (! NILP (tem))
1137 internal_delete_file (b->auto_save_file_name);
1138 }
1139
1140 if (b->base_buffer)
1141 {
1142 /* Unchain all markers that belong to this indirect buffer.
1143 Don't unchain the markers that belong to the base buffer
1144 or its other indirect buffers. */
1145 for (tem = BUF_MARKERS (b); !NILP (tem); )
1146 {
1147 Lisp_Object next;
1148 m = XMARKER (tem);
1149 next = m->chain;
1150 if (m->buffer == b)
1151 unchain_marker (tem);
1152 tem = next;
1153 }
1154 }
1155 else
1156 {
1157 /* Unchain all markers of this buffer and its indirect buffers.
1158 and leave them pointing nowhere. */
1159 for (tem = BUF_MARKERS (b); !NILP (tem); )
1160 {
1161 m = XMARKER (tem);
1162 m->buffer = 0;
1163 tem = m->chain;
1164 m->chain = Qnil;
1165 }
1166 BUF_MARKERS (b) = Qnil;
1167
1168 #ifdef USE_TEXT_PROPERTIES
1169 BUF_INTERVALS (b) = NULL_INTERVAL;
1170 #endif
1171
1172 /* Perhaps we should explicitly free the interval tree here... */
1173 }
1174
1175 /* Reset the local variables, so that this buffer's local values
1176 won't be protected from GC. They would be protected
1177 if they happened to remain encached in their symbols.
1178 This gets rid of them for certain. */
1179 swap_out_buffer_local_variables (b);
1180 reset_buffer_local_variables (b, 1);
1181
1182 b->name = Qnil;
1183
1184 BLOCK_INPUT;
1185 if (! b->base_buffer)
1186 BUFFER_FREE (BUF_BEG_ADDR (b));
1187
1188 if (b->newline_cache)
1189 {
1190 free_region_cache (b->newline_cache);
1191 b->newline_cache = 0;
1192 }
1193 if (b->width_run_cache)
1194 {
1195 free_region_cache (b->width_run_cache);
1196 b->width_run_cache = 0;
1197 }
1198 b->width_table = Qnil;
1199 UNBLOCK_INPUT;
1200 b->undo_list = Qnil;
1201
1202 return Qt;
1203 }
1204 \f
1205 /* Move the assoc for buffer BUF to the front of buffer-alist. Since
1206 we do this each time BUF is selected visibly, the more recently
1207 selected buffers are always closer to the front of the list. This
1208 means that other_buffer is more likely to choose a relevant buffer. */
1209
1210 void
1211 record_buffer (buf)
1212 Lisp_Object buf;
1213 {
1214 register Lisp_Object link, prev;
1215
1216 prev = Qnil;
1217 for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
1218 {
1219 if (EQ (XCONS (XCONS (link)->car)->cdr, buf))
1220 break;
1221 prev = link;
1222 }
1223
1224 /* Effectively do Vbuffer_alist = Fdelq (link, Vbuffer_alist);
1225 we cannot use Fdelq itself here because it allows quitting. */
1226
1227 if (NILP (prev))
1228 Vbuffer_alist = XCONS (Vbuffer_alist)->cdr;
1229 else
1230 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1231
1232 XCONS (link)->cdr = Vbuffer_alist;
1233 Vbuffer_alist = link;
1234
1235 /* Now move this buffer to the front of frame_buffer_list also. */
1236
1237 prev = Qnil;
1238 for (link = frame_buffer_list (); CONSP (link); link = XCONS (link)->cdr)
1239 {
1240 if (EQ (XCONS (link)->car, buf))
1241 break;
1242 prev = link;
1243 }
1244
1245 /* Effectively do delq. */
1246
1247 if (CONSP (link))
1248 {
1249 if (NILP (prev))
1250 set_frame_buffer_list (XCONS (frame_buffer_list ())->cdr);
1251 else
1252 XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
1253
1254 XCONS (link)->cdr = frame_buffer_list ();
1255 set_frame_buffer_list (link);
1256 }
1257 else
1258 set_frame_buffer_list (Fcons (buf, frame_buffer_list ()));
1259 }
1260
1261 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
1262 "Set an appropriate major mode for BUFFER, according to `default-major-mode'.\n\
1263 Use this function before selecting the buffer, since it may need to inspect\n\
1264 the current buffer's major mode.")
1265 (buffer)
1266 Lisp_Object buffer;
1267 {
1268 int count;
1269 Lisp_Object function;
1270
1271 function = buffer_defaults.major_mode;
1272 if (NILP (function) && NILP (Fget (current_buffer->major_mode, Qmode_class)))
1273 function = current_buffer->major_mode;
1274
1275 if (NILP (function) || EQ (function, Qfundamental_mode))
1276 return Qnil;
1277
1278 count = specpdl_ptr - specpdl;
1279
1280 /* To select a nonfundamental mode,
1281 select the buffer temporarily and then call the mode function. */
1282
1283 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1284
1285 Fset_buffer (buffer);
1286 call0 (function);
1287
1288 return unbind_to (count, Qnil);
1289 }
1290
1291 DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
1292 "Select buffer BUFFER in the current window.\n\
1293 BUFFER may be a buffer or a buffer name.\n\
1294 Optional second arg NORECORD non-nil means\n\
1295 do not put this buffer at the front of the list of recently selected ones.\n\
1296 \n\
1297 WARNING: This is NOT the way to work on another buffer temporarily\n\
1298 within a Lisp program! Use `set-buffer' instead. That avoids messing with\n\
1299 the window-buffer correspondences.")
1300 (buffer, norecord)
1301 Lisp_Object buffer, norecord;
1302 {
1303 register Lisp_Object buf;
1304 Lisp_Object tem;
1305
1306 if (EQ (minibuf_window, selected_window))
1307 error ("Cannot switch buffers in minibuffer window");
1308 tem = Fwindow_dedicated_p (selected_window);
1309 if (!NILP (tem))
1310 error ("Cannot switch buffers in a dedicated window");
1311
1312 if (NILP (buffer))
1313 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1314 else
1315 {
1316 buf = Fget_buffer (buffer);
1317 if (NILP (buf))
1318 {
1319 buf = Fget_buffer_create (buffer);
1320 Fset_buffer_major_mode (buf);
1321 }
1322 }
1323 Fset_buffer (buf);
1324 if (NILP (norecord))
1325 record_buffer (buf);
1326
1327 Fset_window_buffer (EQ (selected_window, minibuf_window)
1328 ? Fnext_window (minibuf_window, Qnil, Qnil)
1329 : selected_window,
1330 buf);
1331
1332 return buf;
1333 }
1334
1335 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
1336 "Select buffer BUFFER in some window, preferably a different one.\n\
1337 If BUFFER is nil, then some other buffer is chosen.\n\
1338 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
1339 If optional second arg OTHER-WINDOW is non-nil, insist on finding another\n\
1340 window even if BUFFER is already visible in the selected window.\n\
1341 This uses the function `display-buffer' as a subroutine; see the documentation\n\
1342 of `display-buffer' for additional customization information.\n\
1343 \n\
1344 Optional third arg NORECORD non-nil means\n\
1345 do not put this buffer at the front of the list of recently selected ones.")
1346 (buffer, other_window, norecord)
1347 Lisp_Object buffer, other_window, norecord;
1348 {
1349 register Lisp_Object buf;
1350 if (NILP (buffer))
1351 buf = Fother_buffer (Fcurrent_buffer (), Qnil);
1352 else
1353 {
1354 buf = Fget_buffer (buffer);
1355 if (NILP (buf))
1356 {
1357 buf = Fget_buffer_create (buffer);
1358 Fset_buffer_major_mode (buf);
1359 }
1360 }
1361 Fset_buffer (buf);
1362 if (NILP (norecord))
1363 record_buffer (buf);
1364 Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
1365 return buf;
1366 }
1367
1368 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
1369 "Return the current buffer as a Lisp object.")
1370 ()
1371 {
1372 register Lisp_Object buf;
1373 XSETBUFFER (buf, current_buffer);
1374 return buf;
1375 }
1376 \f
1377 /* Set the current buffer to B. */
1378
1379 void
1380 set_buffer_internal (b)
1381 register struct buffer *b;
1382 {
1383 register struct buffer *old_buf;
1384 register Lisp_Object tail, valcontents;
1385 Lisp_Object tem;
1386
1387 if (current_buffer == b)
1388 return;
1389
1390 windows_or_buffers_changed = 1;
1391 set_buffer_internal_1 (b);
1392 }
1393
1394 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
1395 This is used by redisplay. */
1396
1397 void
1398 set_buffer_internal_1 (b)
1399 register struct buffer *b;
1400 {
1401 register struct buffer *old_buf;
1402 register Lisp_Object tail, valcontents;
1403 Lisp_Object tem;
1404
1405 if (current_buffer == b)
1406 return;
1407
1408 old_buf = current_buffer;
1409 current_buffer = b;
1410 last_known_column_point = -1; /* invalidate indentation cache */
1411
1412 if (old_buf)
1413 {
1414 /* Put the undo list back in the base buffer, so that it appears
1415 that an indirect buffer shares the undo list of its base. */
1416 if (old_buf->base_buffer)
1417 old_buf->base_buffer->undo_list = old_buf->undo_list;
1418
1419 /* If the old current buffer has markers to record PT, BEGV and ZV
1420 when it is not current, update them now. */
1421 if (! NILP (old_buf->pt_marker))
1422 {
1423 Lisp_Object obuf;
1424 XSETBUFFER (obuf, old_buf);
1425 set_marker_both (old_buf->pt_marker, obuf,
1426 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1427 }
1428 if (! NILP (old_buf->begv_marker))
1429 {
1430 Lisp_Object obuf;
1431 XSETBUFFER (obuf, old_buf);
1432 set_marker_both (old_buf->begv_marker, obuf,
1433 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1434 }
1435 if (! NILP (old_buf->zv_marker))
1436 {
1437 Lisp_Object obuf;
1438 XSETBUFFER (obuf, old_buf);
1439 set_marker_both (old_buf->zv_marker, obuf,
1440 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1441 }
1442 }
1443
1444 /* Get the undo list from the base buffer, so that it appears
1445 that an indirect buffer shares the undo list of its base. */
1446 if (b->base_buffer)
1447 b->undo_list = b->base_buffer->undo_list;
1448
1449 /* If the new current buffer has markers to record PT, BEGV and ZV
1450 when it is not current, fetch them now. */
1451 if (! NILP (b->pt_marker))
1452 {
1453 BUF_PT (b) = marker_position (b->pt_marker);
1454 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1455 }
1456 if (! NILP (b->begv_marker))
1457 {
1458 BUF_BEGV (b) = marker_position (b->begv_marker);
1459 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1460 }
1461 if (! NILP (b->zv_marker))
1462 {
1463 BUF_ZV (b) = marker_position (b->zv_marker);
1464 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1465 }
1466
1467 /* Look down buffer's list of local Lisp variables
1468 to find and update any that forward into C variables. */
1469
1470 for (tail = b->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1471 {
1472 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
1473 if ((BUFFER_LOCAL_VALUEP (valcontents)
1474 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1475 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1476 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1477 /* Just reference the variable
1478 to cause it to become set for this buffer. */
1479 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1480 }
1481
1482 /* Do the same with any others that were local to the previous buffer */
1483
1484 if (old_buf)
1485 for (tail = old_buf->local_var_alist; !NILP (tail); tail = XCONS (tail)->cdr)
1486 {
1487 valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
1488 if ((BUFFER_LOCAL_VALUEP (valcontents)
1489 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1490 && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1491 (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
1492 /* Just reference the variable
1493 to cause it to become set for this buffer. */
1494 Fsymbol_value (XCONS (XCONS (tail)->car)->car);
1495 }
1496 }
1497
1498 /* Switch to buffer B temporarily for redisplay purposes.
1499 This avoids certain things that don't need to be done within redisplay. */
1500
1501 void
1502 set_buffer_temp (b)
1503 struct buffer *b;
1504 {
1505 register struct buffer *old_buf;
1506
1507 if (current_buffer == b)
1508 return;
1509
1510 old_buf = current_buffer;
1511 current_buffer = b;
1512
1513 if (old_buf)
1514 {
1515 /* If the old current buffer has markers to record PT, BEGV and ZV
1516 when it is not current, update them now. */
1517 if (! NILP (old_buf->pt_marker))
1518 {
1519 Lisp_Object obuf;
1520 XSETBUFFER (obuf, old_buf);
1521 set_marker_both (old_buf->pt_marker, obuf,
1522 BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
1523 }
1524 if (! NILP (old_buf->begv_marker))
1525 {
1526 Lisp_Object obuf;
1527 XSETBUFFER (obuf, old_buf);
1528 set_marker_both (old_buf->begv_marker, obuf,
1529 BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
1530 }
1531 if (! NILP (old_buf->zv_marker))
1532 {
1533 Lisp_Object obuf;
1534 XSETBUFFER (obuf, old_buf);
1535 set_marker_both (old_buf->zv_marker, obuf,
1536 BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
1537 }
1538 }
1539
1540 /* If the new current buffer has markers to record PT, BEGV and ZV
1541 when it is not current, fetch them now. */
1542 if (! NILP (b->pt_marker))
1543 {
1544 BUF_PT (b) = marker_position (b->pt_marker);
1545 BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
1546 }
1547 if (! NILP (b->begv_marker))
1548 {
1549 BUF_BEGV (b) = marker_position (b->begv_marker);
1550 BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
1551 }
1552 if (! NILP (b->zv_marker))
1553 {
1554 BUF_ZV (b) = marker_position (b->zv_marker);
1555 BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
1556 }
1557 }
1558
1559 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
1560 "Make the buffer BUFFER current for editing operations.\n\
1561 BUFFER may be a buffer or the name of an existing buffer.\n\
1562 See also `save-excursion' when you want to make a buffer current temporarily.\n\
1563 This function does not display the buffer, so its effect ends\n\
1564 when the current command terminates.\n\
1565 Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
1566 (buffer)
1567 register Lisp_Object buffer;
1568 {
1569 register Lisp_Object buf;
1570 buf = Fget_buffer (buffer);
1571 if (NILP (buf))
1572 nsberror (buffer);
1573 if (NILP (XBUFFER (buf)->name))
1574 error ("Selecting deleted buffer");
1575 set_buffer_internal (XBUFFER (buf));
1576 return buf;
1577 }
1578
1579 /* Set the current buffer to BUFFER provided it is alive. */
1580
1581 Lisp_Object
1582 set_buffer_if_live (buffer)
1583 Lisp_Object buffer;
1584 {
1585 if (! NILP (XBUFFER (buffer)->name))
1586 Fset_buffer (buffer);
1587 return Qnil;
1588 }
1589 \f
1590 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
1591 Sbarf_if_buffer_read_only, 0, 0, 0,
1592 "Signal a `buffer-read-only' error if the current buffer is read-only.")
1593 ()
1594 {
1595 if (!NILP (current_buffer->read_only)
1596 && NILP (Vinhibit_read_only))
1597 Fsignal (Qbuffer_read_only, (Fcons (Fcurrent_buffer (), Qnil)));
1598 return Qnil;
1599 }
1600
1601 DEFUN ("bury-buffer", Fbury_buffer, Sbury_buffer, 0, 1, "",
1602 "Put BUFFER at the end of the list of all buffers.\n\
1603 There it is the least likely candidate for `other-buffer' to return;\n\
1604 thus, the least likely buffer for \\[switch-to-buffer] to select by default.\n\
1605 If BUFFER is nil or omitted, bury the current buffer.\n\
1606 Also, if BUFFER is nil or omitted, remove the current buffer from the\n\
1607 selected window if it is displayed there.")
1608 (buffer)
1609 register Lisp_Object buffer;
1610 {
1611 /* Figure out what buffer we're going to bury. */
1612 if (NILP (buffer))
1613 {
1614 XSETBUFFER (buffer, current_buffer);
1615
1616 /* If we're burying the current buffer, unshow it. */
1617 Fswitch_to_buffer (Fother_buffer (buffer, Qnil), Qnil);
1618 }
1619 else
1620 {
1621 Lisp_Object buf1;
1622
1623 buf1 = Fget_buffer (buffer);
1624 if (NILP (buf1))
1625 nsberror (buffer);
1626 buffer = buf1;
1627 }
1628
1629 /* Move buffer to the end of the buffer list. */
1630 {
1631 register Lisp_Object aelt, link;
1632
1633 aelt = Frassq (buffer, Vbuffer_alist);
1634 link = Fmemq (aelt, Vbuffer_alist);
1635 Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
1636 XCONS (link)->cdr = Qnil;
1637 Vbuffer_alist = nconc2 (Vbuffer_alist, link);
1638 }
1639
1640 frames_bury_buffer (buffer);
1641
1642 return Qnil;
1643 }
1644 \f
1645 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
1646 "Delete the entire contents of the current buffer.\n\
1647 Any narrowing restriction in effect (see `narrow-to-region') is removed,\n\
1648 so the buffer is truly empty after this.")
1649 ()
1650 {
1651 Fwiden ();
1652 del_range (BEG, Z);
1653 current_buffer->last_window_start = 1;
1654 /* Prevent warnings, or suspension of auto saving, that would happen
1655 if future size is less than past size. Use of erase-buffer
1656 implies that the future text is not really related to the past text. */
1657 XSETFASTINT (current_buffer->save_length, 0);
1658 return Qnil;
1659 }
1660
1661 void
1662 validate_region (b, e)
1663 register Lisp_Object *b, *e;
1664 {
1665 CHECK_NUMBER_COERCE_MARKER (*b, 0);
1666 CHECK_NUMBER_COERCE_MARKER (*e, 1);
1667
1668 if (XINT (*b) > XINT (*e))
1669 {
1670 Lisp_Object tem;
1671 tem = *b; *b = *e; *e = tem;
1672 }
1673
1674 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1675 && XINT (*e) <= ZV))
1676 args_out_of_range (*b, *e);
1677 }
1678 \f
1679 /* Advance BYTE_POS up to a character boundary
1680 and return the adjusted position. */
1681
1682 static int
1683 advance_to_char_boundary (byte_pos)
1684 int byte_pos;
1685 {
1686 int c;
1687
1688 if (byte_pos == BEG)
1689 /* Beginning of buffer is always a character boundary. */
1690 return 1;
1691
1692 c = FETCH_BYTE (byte_pos);
1693 if (! CHAR_HEAD_P (c))
1694 {
1695 /* We should advance BYTE_POS only when C is a constituen of a
1696 multibyte sequence. */
1697 DEC_POS (byte_pos);
1698 INC_POS (byte_pos);
1699 /* If C is a constituent of a multibyte sequence, BYTE_POS was
1700 surely advance to the correct character boundary. If C is
1701 not, BYTE_POS was unchanged. */
1702 }
1703
1704 return byte_pos;
1705 }
1706
1707 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1708 1, 1, 0,
1709 "Set the multibyte flag of the current buffer to FLAG.\n\
1710 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1711 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1712 The buffer contents remain unchanged as a sequence of bytes\n\
1713 but the contents viewed as characters do change.")
1714 (flag)
1715 Lisp_Object flag;
1716 {
1717 Lisp_Object tail, markers;
1718
1719 /* It would be better to update the list,
1720 but this is good enough for now. */
1721 if (! EQ (current_buffer->undo_list, Qt))
1722 current_buffer->undo_list = Qnil;
1723
1724 /* If the cached position is for this buffer, clear it out. */
1725 clear_charpos_cache (current_buffer);
1726
1727 if (NILP (flag))
1728 {
1729 /* Do this first, so it can use CHAR_TO_BYTE
1730 to calculate the old correspondences. */
1731 set_intervals_multibyte (0);
1732
1733 current_buffer->enable_multibyte_characters = Qnil;
1734
1735 Z = Z_BYTE;
1736 BEGV = BEGV_BYTE;
1737 ZV = ZV_BYTE;
1738 GPT = GPT_BYTE;
1739 TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
1740
1741 tail = BUF_MARKERS (current_buffer);
1742 while (XSYMBOL (tail) != XSYMBOL (Qnil))
1743 {
1744 XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
1745 tail = XMARKER (tail)->chain;
1746 }
1747 }
1748 else
1749 {
1750 /* Be sure not to have a multibyte sequence striding over the GAP.
1751 Ex: We change this: "...abc\201\241\241 _GAP_ \241\241\241..."
1752 to: "...abc _GAP_ \201\241\241\241\241\241..." */
1753
1754 if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
1755 && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
1756 {
1757 unsigned char *p = GPT_ADDR - 1;
1758
1759 while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
1760 if (BASE_LEADING_CODE_P (*p))
1761 {
1762 int new_gpt = GPT_BYTE - (GPT_ADDR - p);
1763
1764 move_gap_both (new_gpt, new_gpt);
1765 }
1766 }
1767
1768 /* Do this first, so that chars_in_text asks the right question.
1769 set_intervals_multibyte needs it too. */
1770 current_buffer->enable_multibyte_characters = Qt;
1771
1772 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
1773 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
1774
1775 Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
1776
1777 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
1778 if (BEGV_BYTE > GPT_BYTE)
1779 BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
1780 else
1781 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
1782
1783 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
1784 if (ZV_BYTE > GPT_BYTE)
1785 ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
1786 else
1787 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
1788
1789 {
1790 int pt_byte = advance_to_char_boundary (PT_BYTE);
1791 int pt;
1792
1793 if (pt_byte > GPT_BYTE)
1794 pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
1795 else
1796 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
1797 TEMP_SET_PT_BOTH (pt, pt_byte);
1798 }
1799
1800 tail = markers = BUF_MARKERS (current_buffer);
1801 BUF_MARKERS (current_buffer) = Qnil;
1802
1803 while (XSYMBOL (tail) != XSYMBOL (Qnil))
1804 {
1805 XMARKER (tail)->bytepos
1806 = advance_to_char_boundary (XMARKER (tail)->bytepos);
1807 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
1808
1809 tail = XMARKER (tail)->chain;
1810 }
1811 BUF_MARKERS (current_buffer) = markers;
1812
1813 /* Do this last, so it can calculate the new correspondences
1814 between chars and bytes. */
1815 set_intervals_multibyte (1);
1816 }
1817
1818 return flag;
1819 }
1820 \f
1821 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
1822 0, 0, 0,
1823 "Switch to Fundamental mode by killing current buffer's local variables.\n\
1824 Most local variable bindings are eliminated so that the default values\n\
1825 become effective once more. Also, the syntax table is set from\n\
1826 `standard-syntax-table', the local keymap is set to nil,\n\
1827 and the abbrev table from `fundamental-mode-abbrev-table'.\n\
1828 This function also forces redisplay of the mode line.\n\
1829 \n\
1830 Every function to select a new major mode starts by\n\
1831 calling this function.\n\n\
1832 As a special exception, local variables whose names have\n\
1833 a non-nil `permanent-local' property are not eliminated by this function.\n\
1834 \n\
1835 The first thing this function does is run\n\
1836 the normal hook `change-major-mode-hook'.")
1837 ()
1838 {
1839 register Lisp_Object alist, sym, tem;
1840 Lisp_Object oalist;
1841
1842 if (!NILP (Vrun_hooks))
1843 call1 (Vrun_hooks, intern ("change-major-mode-hook"));
1844 oalist = current_buffer->local_var_alist;
1845
1846 /* Make sure none of the bindings in oalist
1847 remain swapped in, in their symbols. */
1848
1849 swap_out_buffer_local_variables (current_buffer);
1850
1851 /* Actually eliminate all local bindings of this buffer. */
1852
1853 reset_buffer_local_variables (current_buffer, 0);
1854
1855 /* Redisplay mode lines; we are changing major mode. */
1856
1857 update_mode_lines++;
1858
1859 /* Any which are supposed to be permanent,
1860 make local again, with the same values they had. */
1861
1862 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1863 {
1864 sym = XCONS (XCONS (alist)->car)->car;
1865 tem = Fget (sym, Qpermanent_local);
1866 if (! NILP (tem))
1867 {
1868 Fmake_local_variable (sym);
1869 Fset (sym, XCONS (XCONS (alist)->car)->cdr);
1870 }
1871 }
1872
1873 /* Force mode-line redisplay. Useful here because all major mode
1874 commands call this function. */
1875 update_mode_lines++;
1876
1877 return Qnil;
1878 }
1879
1880 /* Make sure no local variables remain set up with buffer B
1881 for their current values. */
1882
1883 static void
1884 swap_out_buffer_local_variables (b)
1885 struct buffer *b;
1886 {
1887 Lisp_Object oalist, alist, sym, tem, buffer;
1888
1889 XSETBUFFER (buffer, b);
1890 oalist = b->local_var_alist;
1891
1892 for (alist = oalist; !NILP (alist); alist = XCONS (alist)->cdr)
1893 {
1894 sym = XCONS (XCONS (alist)->car)->car;
1895
1896 /* Need not do anything if some other buffer's binding is now encached. */
1897 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
1898 if (XBUFFER (tem) == current_buffer)
1899 {
1900 /* Symbol is set up for this buffer's old local value.
1901 Set it up for the current buffer with the default value. */
1902
1903 tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr;
1904 /* Store the symbol's current value into the alist entry
1905 it is currently set up for. This is so that, if the
1906 local is marked permanent, and we make it local again
1907 later in Fkill_all_local_variables, we don't lose the value. */
1908 XCONS (XCONS (tem)->car)->cdr
1909 = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
1910 /* Switch to the symbol's default-value alist entry. */
1911 XCONS (tem)->car = tem;
1912 /* Mark it as current for buffer B. */
1913 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
1914 /* Store the current value into any forwarding in the symbol. */
1915 store_symval_forwarding (sym,
1916 XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
1917 XCONS (tem)->cdr);
1918 }
1919 }
1920 }
1921 \f
1922 /* Find all the overlays in the current buffer that contain position POS.
1923 Return the number found, and store them in a vector in *VEC_PTR.
1924 Store in *LEN_PTR the size allocated for the vector.
1925 Store in *NEXT_PTR the next position after POS where an overlay starts,
1926 or ZV if there are no more overlays.
1927 Store in *PREV_PTR the previous position before POS where an overlay ends,
1928 or BEGV if there are no previous overlays.
1929 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
1930
1931 *VEC_PTR and *LEN_PTR should contain a valid vector and size
1932 when this function is called.
1933
1934 If EXTEND is non-zero, we make the vector bigger if necessary.
1935 If EXTEND is zero, we never extend the vector,
1936 and we store only as many overlays as will fit.
1937 But we still return the total number of overlays. */
1938
1939 int
1940 overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
1941 int pos;
1942 int extend;
1943 Lisp_Object **vec_ptr;
1944 int *len_ptr;
1945 int *next_ptr;
1946 int *prev_ptr;
1947 {
1948 Lisp_Object tail, overlay, start, end, result;
1949 int idx = 0;
1950 int len = *len_ptr;
1951 Lisp_Object *vec = *vec_ptr;
1952 int next = ZV;
1953 int prev = BEGV;
1954 int inhibit_storing = 0;
1955
1956 for (tail = current_buffer->overlays_before;
1957 GC_CONSP (tail);
1958 tail = XCONS (tail)->cdr)
1959 {
1960 int startpos, endpos;
1961
1962 overlay = XCONS (tail)->car;
1963
1964 start = OVERLAY_START (overlay);
1965 end = OVERLAY_END (overlay);
1966 endpos = OVERLAY_POSITION (end);
1967 if (endpos < pos)
1968 {
1969 if (prev < endpos)
1970 prev = endpos;
1971 break;
1972 }
1973 if (endpos == pos)
1974 continue;
1975 startpos = OVERLAY_POSITION (start);
1976 if (startpos <= pos)
1977 {
1978 if (idx == len)
1979 {
1980 /* The supplied vector is full.
1981 Either make it bigger, or don't store any more in it. */
1982 if (extend)
1983 {
1984 *len_ptr = len *= 2;
1985 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
1986 *vec_ptr = vec;
1987 }
1988 else
1989 inhibit_storing = 1;
1990 }
1991
1992 if (!inhibit_storing)
1993 vec[idx] = overlay;
1994 /* Keep counting overlays even if we can't return them all. */
1995 idx++;
1996 }
1997 else if (startpos < next)
1998 next = startpos;
1999 }
2000
2001 for (tail = current_buffer->overlays_after;
2002 GC_CONSP (tail);
2003 tail = XCONS (tail)->cdr)
2004 {
2005 int startpos, endpos;
2006
2007 overlay = XCONS (tail)->car;
2008
2009 start = OVERLAY_START (overlay);
2010 end = OVERLAY_END (overlay);
2011 startpos = OVERLAY_POSITION (start);
2012 if (pos < startpos)
2013 {
2014 if (startpos < next)
2015 next = startpos;
2016 break;
2017 }
2018 endpos = OVERLAY_POSITION (end);
2019 if (pos < endpos)
2020 {
2021 if (idx == len)
2022 {
2023 if (extend)
2024 {
2025 *len_ptr = len *= 2;
2026 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2027 *vec_ptr = vec;
2028 }
2029 else
2030 inhibit_storing = 1;
2031 }
2032
2033 if (!inhibit_storing)
2034 vec[idx] = overlay;
2035 idx++;
2036 }
2037 else if (endpos < pos && endpos > prev)
2038 prev = endpos;
2039 }
2040
2041 if (next_ptr)
2042 *next_ptr = next;
2043 if (prev_ptr)
2044 *prev_ptr = prev;
2045 return idx;
2046 }
2047 \f
2048 /* Find all the overlays in the current buffer that overlap the range BEG-END
2049 or are empty at BEG.
2050
2051 Return the number found, and store them in a vector in *VEC_PTR.
2052 Store in *LEN_PTR the size allocated for the vector.
2053 Store in *NEXT_PTR the next position after POS where an overlay starts,
2054 or ZV if there are no more overlays.
2055 Store in *PREV_PTR the previous position before POS where an overlay ends,
2056 or BEGV if there are no previous overlays.
2057 NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
2058
2059 *VEC_PTR and *LEN_PTR should contain a valid vector and size
2060 when this function is called.
2061
2062 If EXTEND is non-zero, we make the vector bigger if necessary.
2063 If EXTEND is zero, we never extend the vector,
2064 and we store only as many overlays as will fit.
2065 But we still return the total number of overlays. */
2066
2067 int
2068 overlays_in (beg, end, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
2069 int beg, end;
2070 int extend;
2071 Lisp_Object **vec_ptr;
2072 int *len_ptr;
2073 int *next_ptr;
2074 int *prev_ptr;
2075 {
2076 Lisp_Object tail, overlay, ostart, oend, result;
2077 int idx = 0;
2078 int len = *len_ptr;
2079 Lisp_Object *vec = *vec_ptr;
2080 int next = ZV;
2081 int prev = BEGV;
2082 int inhibit_storing = 0;
2083
2084 for (tail = current_buffer->overlays_before;
2085 GC_CONSP (tail);
2086 tail = XCONS (tail)->cdr)
2087 {
2088 int startpos, endpos;
2089
2090 overlay = XCONS (tail)->car;
2091
2092 ostart = OVERLAY_START (overlay);
2093 oend = OVERLAY_END (overlay);
2094 endpos = OVERLAY_POSITION (oend);
2095 if (endpos < beg)
2096 {
2097 if (prev < endpos)
2098 prev = endpos;
2099 break;
2100 }
2101 startpos = OVERLAY_POSITION (ostart);
2102 /* Count an interval if it either overlaps the range
2103 or is empty at the start of the range. */
2104 if ((beg < endpos && startpos < end)
2105 || (startpos == endpos && beg == endpos))
2106 {
2107 if (idx == len)
2108 {
2109 /* The supplied vector is full.
2110 Either make it bigger, or don't store any more in it. */
2111 if (extend)
2112 {
2113 *len_ptr = len *= 2;
2114 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2115 *vec_ptr = vec;
2116 }
2117 else
2118 inhibit_storing = 1;
2119 }
2120
2121 if (!inhibit_storing)
2122 vec[idx] = overlay;
2123 /* Keep counting overlays even if we can't return them all. */
2124 idx++;
2125 }
2126 else if (startpos < next)
2127 next = startpos;
2128 }
2129
2130 for (tail = current_buffer->overlays_after;
2131 GC_CONSP (tail);
2132 tail = XCONS (tail)->cdr)
2133 {
2134 int startpos, endpos;
2135
2136 overlay = XCONS (tail)->car;
2137
2138 ostart = OVERLAY_START (overlay);
2139 oend = OVERLAY_END (overlay);
2140 startpos = OVERLAY_POSITION (ostart);
2141 if (end < startpos)
2142 {
2143 if (startpos < next)
2144 next = startpos;
2145 break;
2146 }
2147 endpos = OVERLAY_POSITION (oend);
2148 /* Count an interval if it either overlaps the range
2149 or is empty at the start of the range. */
2150 if ((beg < endpos && startpos < end)
2151 || (startpos == endpos && beg == endpos))
2152 {
2153 if (idx == len)
2154 {
2155 if (extend)
2156 {
2157 *len_ptr = len *= 2;
2158 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
2159 *vec_ptr = vec;
2160 }
2161 else
2162 inhibit_storing = 1;
2163 }
2164
2165 if (!inhibit_storing)
2166 vec[idx] = overlay;
2167 idx++;
2168 }
2169 else if (endpos < beg && endpos > prev)
2170 prev = endpos;
2171 }
2172
2173 if (next_ptr)
2174 *next_ptr = next;
2175 if (prev_ptr)
2176 *prev_ptr = prev;
2177 return idx;
2178 }
2179 \f
2180 /* Fast function to just test if we're at an overlay boundary. */
2181 int
2182 overlay_touches_p (pos)
2183 int pos;
2184 {
2185 Lisp_Object tail, overlay;
2186
2187 for (tail = current_buffer->overlays_before; GC_CONSP (tail);
2188 tail = XCONS (tail)->cdr)
2189 {
2190 int endpos;
2191
2192 overlay = XCONS (tail)->car;
2193 if (!GC_OVERLAYP (overlay))
2194 abort ();
2195
2196 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2197 if (endpos < pos)
2198 break;
2199 if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos)
2200 return 1;
2201 }
2202
2203 for (tail = current_buffer->overlays_after; GC_CONSP (tail);
2204 tail = XCONS (tail)->cdr)
2205 {
2206 int startpos;
2207
2208 overlay = XCONS (tail)->car;
2209 if (!GC_OVERLAYP (overlay))
2210 abort ();
2211
2212 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2213 if (pos < startpos)
2214 break;
2215 if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos)
2216 return 1;
2217 }
2218 return 0;
2219 }
2220 \f
2221 struct sortvec
2222 {
2223 Lisp_Object overlay;
2224 int beg, end;
2225 int priority;
2226 };
2227
2228 static int
2229 compare_overlays (s1, s2)
2230 struct sortvec *s1, *s2;
2231 {
2232 if (s1->priority != s2->priority)
2233 return s1->priority - s2->priority;
2234 if (s1->beg != s2->beg)
2235 return s1->beg - s2->beg;
2236 if (s1->end != s2->end)
2237 return s2->end - s1->end;
2238 return 0;
2239 }
2240
2241 /* Sort an array of overlays by priority. The array is modified in place.
2242 The return value is the new size; this may be smaller than the original
2243 size if some of the overlays were invalid or were window-specific. */
2244 int
2245 sort_overlays (overlay_vec, noverlays, w)
2246 Lisp_Object *overlay_vec;
2247 int noverlays;
2248 struct window *w;
2249 {
2250 int i, j;
2251 struct sortvec *sortvec;
2252 sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
2253
2254 /* Put the valid and relevant overlays into sortvec. */
2255
2256 for (i = 0, j = 0; i < noverlays; i++)
2257 {
2258 Lisp_Object tem;
2259 Lisp_Object overlay;
2260
2261 overlay = overlay_vec[i];
2262 if (OVERLAY_VALID (overlay)
2263 && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
2264 && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
2265 {
2266 /* If we're interested in a specific window, then ignore
2267 overlays that are limited to some other window. */
2268 if (w)
2269 {
2270 Lisp_Object window;
2271
2272 window = Foverlay_get (overlay, Qwindow);
2273 if (WINDOWP (window) && XWINDOW (window) != w)
2274 continue;
2275 }
2276
2277 /* This overlay is good and counts: put it into sortvec. */
2278 sortvec[j].overlay = overlay;
2279 sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay));
2280 sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay));
2281 tem = Foverlay_get (overlay, Qpriority);
2282 if (INTEGERP (tem))
2283 sortvec[j].priority = XINT (tem);
2284 else
2285 sortvec[j].priority = 0;
2286 j++;
2287 }
2288 }
2289 noverlays = j;
2290
2291 /* Sort the overlays into the proper order: increasing priority. */
2292
2293 if (noverlays > 1)
2294 qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
2295
2296 for (i = 0; i < noverlays; i++)
2297 overlay_vec[i] = sortvec[i].overlay;
2298 return (noverlays);
2299 }
2300 \f
2301 struct sortstr
2302 {
2303 Lisp_Object string, string2;
2304 int size;
2305 int priority;
2306 };
2307
2308 struct sortstrlist
2309 {
2310 struct sortstr *buf; /* An array that expands as needed; never freed. */
2311 int size; /* Allocated length of that array. */
2312 int used; /* How much of the array is currently in use. */
2313 int bytes; /* Total length of the strings in buf. */
2314 };
2315
2316 /* Buffers for storing information about the overlays touching a given
2317 position. These could be automatic variables in overlay_strings, but
2318 it's more efficient to hold onto the memory instead of repeatedly
2319 allocating and freeing it. */
2320 static struct sortstrlist overlay_heads, overlay_tails;
2321 static unsigned char *overlay_str_buf;
2322
2323 /* Allocated length of overlay_str_buf. */
2324 static int overlay_str_len;
2325
2326 /* A comparison function suitable for passing to qsort. */
2327 static int
2328 cmp_for_strings (as1, as2)
2329 char *as1, *as2;
2330 {
2331 struct sortstr *s1 = (struct sortstr *)as1;
2332 struct sortstr *s2 = (struct sortstr *)as2;
2333 if (s1->size != s2->size)
2334 return s2->size - s1->size;
2335 if (s1->priority != s2->priority)
2336 return s1->priority - s2->priority;
2337 return 0;
2338 }
2339
2340 static void
2341 record_overlay_string (ssl, str, str2, pri, size)
2342 struct sortstrlist *ssl;
2343 Lisp_Object str, str2, pri;
2344 int size;
2345 {
2346 int nbytes;
2347
2348 if (ssl->used == ssl->size)
2349 {
2350 if (ssl->buf)
2351 ssl->size *= 2;
2352 else
2353 ssl->size = 5;
2354 ssl->buf = ((struct sortstr *)
2355 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
2356 }
2357 ssl->buf[ssl->used].string = str;
2358 ssl->buf[ssl->used].string2 = str2;
2359 ssl->buf[ssl->used].size = size;
2360 ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
2361 ssl->used++;
2362
2363 if (NILP (current_buffer->enable_multibyte_characters))
2364 nbytes = XSTRING (str)->size;
2365 else if (! STRING_MULTIBYTE (str))
2366 nbytes = count_size_as_multibyte (XSTRING (str)->data,
2367 XSTRING (str)->size_byte);
2368 else
2369 nbytes = XSTRING (str)->size_byte;
2370
2371 ssl->bytes += nbytes;
2372
2373 if (STRINGP (str2))
2374 {
2375 if (NILP (current_buffer->enable_multibyte_characters))
2376 nbytes = XSTRING (str2)->size;
2377 else if (! STRING_MULTIBYTE (str2))
2378 nbytes = count_size_as_multibyte (XSTRING (str2)->data,
2379 XSTRING (str2)->size_byte);
2380 else
2381 nbytes = XSTRING (str2)->size_byte;
2382
2383 ssl->bytes += nbytes;
2384 }
2385 }
2386
2387 /* Return the concatenation of the strings associated with overlays that
2388 begin or end at POS, ignoring overlays that are specific to a window
2389 other than W. The strings are concatenated in the appropriate order:
2390 shorter overlays nest inside longer ones, and higher priority inside
2391 lower. Normally all of the after-strings come first, but zero-sized
2392 overlays have their after-strings ride along with the before-strings
2393 because it would look strange to print them inside-out.
2394
2395 Returns the string length, and stores the contents indirectly through
2396 PSTR, if that variable is non-null. The string may be overwritten by
2397 subsequent calls. */
2398
2399 int
2400 overlay_strings (pos, w, pstr)
2401 int pos;
2402 struct window *w;
2403 unsigned char **pstr;
2404 {
2405 Lisp_Object ov, overlay, window, str;
2406 int startpos, endpos;
2407 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
2408
2409 overlay_heads.used = overlay_heads.bytes = 0;
2410 overlay_tails.used = overlay_tails.bytes = 0;
2411 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCONS (ov)->cdr)
2412 {
2413 overlay = XCONS (ov)->car;
2414 if (!OVERLAYP (overlay))
2415 abort ();
2416
2417 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2418 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2419 if (endpos < pos)
2420 break;
2421 if (endpos != pos && startpos != pos)
2422 continue;
2423 window = Foverlay_get (overlay, Qwindow);
2424 if (WINDOWP (window) && XWINDOW (window) != w)
2425 continue;
2426 if (startpos == pos
2427 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2428 record_overlay_string (&overlay_heads, str,
2429 (startpos == endpos
2430 ? Foverlay_get (overlay, Qafter_string)
2431 : Qnil),
2432 Foverlay_get (overlay, Qpriority),
2433 endpos - startpos);
2434 else if (endpos == pos
2435 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2436 record_overlay_string (&overlay_tails, str, Qnil,
2437 Foverlay_get (overlay, Qpriority),
2438 endpos - startpos);
2439 }
2440 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCONS (ov)->cdr)
2441 {
2442 overlay = XCONS (ov)->car;
2443 if (!OVERLAYP (overlay))
2444 abort ();
2445
2446 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2447 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2448 if (startpos > pos)
2449 break;
2450 if (endpos != pos && startpos != pos)
2451 continue;
2452 window = Foverlay_get (overlay, Qwindow);
2453 if (WINDOWP (window) && XWINDOW (window) != w)
2454 continue;
2455 if (startpos == pos
2456 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
2457 record_overlay_string (&overlay_heads, str,
2458 (startpos == endpos
2459 ? Foverlay_get (overlay, Qafter_string)
2460 : Qnil),
2461 Foverlay_get (overlay, Qpriority),
2462 endpos - startpos);
2463 else if (endpos == pos
2464 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
2465 record_overlay_string (&overlay_tails, str, Qnil,
2466 Foverlay_get (overlay, Qpriority),
2467 endpos - startpos);
2468 }
2469 if (overlay_tails.used > 1)
2470 qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
2471 cmp_for_strings);
2472 if (overlay_heads.used > 1)
2473 qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
2474 cmp_for_strings);
2475 if (overlay_heads.bytes || overlay_tails.bytes)
2476 {
2477 Lisp_Object tem;
2478 int i;
2479 unsigned char *p;
2480 int total = overlay_heads.bytes + overlay_tails.bytes;
2481
2482 if (total > overlay_str_len)
2483 {
2484 overlay_str_len = total;
2485 overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
2486 total);
2487 }
2488 p = overlay_str_buf;
2489 for (i = overlay_tails.used; --i >= 0;)
2490 {
2491 int nbytes;
2492 tem = overlay_tails.buf[i].string;
2493 nbytes = copy_text (XSTRING (tem)->data, p, XSTRING (tem)->size_byte,
2494 STRING_MULTIBYTE (tem), multibyte);
2495 p += nbytes;
2496 }
2497 for (i = 0; i < overlay_heads.used; ++i)
2498 {
2499 int nbytes;
2500 tem = overlay_heads.buf[i].string;
2501 nbytes = copy_text (XSTRING (tem)->data, p, XSTRING (tem)->size_byte,
2502 STRING_MULTIBYTE (tem), multibyte);
2503 p += nbytes;
2504 tem = overlay_heads.buf[i].string2;
2505 if (STRINGP (tem))
2506 {
2507 nbytes = copy_text (XSTRING (tem)->data, p,
2508 XSTRING (tem)->size_byte,
2509 STRING_MULTIBYTE (tem), multibyte);
2510 p += nbytes;
2511 }
2512 }
2513 if (p != overlay_str_buf + total)
2514 abort ();
2515 if (pstr)
2516 *pstr = overlay_str_buf;
2517 return total;
2518 }
2519 return 0;
2520 }
2521 \f
2522 /* Shift overlays in BUF's overlay lists, to center the lists at POS. */
2523
2524 void
2525 recenter_overlay_lists (buf, pos)
2526 struct buffer *buf;
2527 int pos;
2528 {
2529 Lisp_Object overlay, tail, next, prev, beg, end;
2530
2531 /* See if anything in overlays_before should move to overlays_after. */
2532
2533 /* We don't strictly need prev in this loop; it should always be nil.
2534 But we use it for symmetry and in case that should cease to be true
2535 with some future change. */
2536 prev = Qnil;
2537 for (tail = buf->overlays_before;
2538 CONSP (tail);
2539 prev = tail, tail = next)
2540 {
2541 next = XCONS (tail)->cdr;
2542 overlay = XCONS (tail)->car;
2543
2544 /* If the overlay is not valid, get rid of it. */
2545 if (!OVERLAY_VALID (overlay))
2546 #if 1
2547 abort ();
2548 #else
2549 {
2550 /* Splice the cons cell TAIL out of overlays_before. */
2551 if (!NILP (prev))
2552 XCONS (prev)->cdr = next;
2553 else
2554 buf->overlays_before = next;
2555 tail = prev;
2556 continue;
2557 }
2558 #endif
2559
2560 beg = OVERLAY_START (overlay);
2561 end = OVERLAY_END (overlay);
2562
2563 if (OVERLAY_POSITION (end) > pos)
2564 {
2565 /* OVERLAY needs to be moved. */
2566 int where = OVERLAY_POSITION (beg);
2567 Lisp_Object other, other_prev;
2568
2569 /* Splice the cons cell TAIL out of overlays_before. */
2570 if (!NILP (prev))
2571 XCONS (prev)->cdr = next;
2572 else
2573 buf->overlays_before = next;
2574
2575 /* Search thru overlays_after for where to put it. */
2576 other_prev = Qnil;
2577 for (other = buf->overlays_after;
2578 CONSP (other);
2579 other_prev = other, other = XCONS (other)->cdr)
2580 {
2581 Lisp_Object otherbeg, otheroverlay, follower;
2582 int win;
2583
2584 otheroverlay = XCONS (other)->car;
2585 if (! OVERLAY_VALID (otheroverlay))
2586 abort ();
2587
2588 otherbeg = OVERLAY_START (otheroverlay);
2589 if (OVERLAY_POSITION (otherbeg) >= where)
2590 break;
2591 }
2592
2593 /* Add TAIL to overlays_after before OTHER. */
2594 XCONS (tail)->cdr = other;
2595 if (!NILP (other_prev))
2596 XCONS (other_prev)->cdr = tail;
2597 else
2598 buf->overlays_after = tail;
2599 tail = prev;
2600 }
2601 else
2602 /* We've reached the things that should stay in overlays_before.
2603 All the rest of overlays_before must end even earlier,
2604 so stop now. */
2605 break;
2606 }
2607
2608 /* See if anything in overlays_after should be in overlays_before. */
2609 prev = Qnil;
2610 for (tail = buf->overlays_after;
2611 CONSP (tail);
2612 prev = tail, tail = next)
2613 {
2614 next = XCONS (tail)->cdr;
2615 overlay = XCONS (tail)->car;
2616
2617 /* If the overlay is not valid, get rid of it. */
2618 if (!OVERLAY_VALID (overlay))
2619 #if 1
2620 abort ();
2621 #else
2622 {
2623 /* Splice the cons cell TAIL out of overlays_after. */
2624 if (!NILP (prev))
2625 XCONS (prev)->cdr = next;
2626 else
2627 buf->overlays_after = next;
2628 tail = prev;
2629 continue;
2630 }
2631 #endif
2632
2633 beg = OVERLAY_START (overlay);
2634 end = OVERLAY_END (overlay);
2635
2636 /* Stop looking, when we know that nothing further
2637 can possibly end before POS. */
2638 if (OVERLAY_POSITION (beg) > pos)
2639 break;
2640
2641 if (OVERLAY_POSITION (end) <= pos)
2642 {
2643 /* OVERLAY needs to be moved. */
2644 int where = OVERLAY_POSITION (end);
2645 Lisp_Object other, other_prev;
2646
2647 /* Splice the cons cell TAIL out of overlays_after. */
2648 if (!NILP (prev))
2649 XCONS (prev)->cdr = next;
2650 else
2651 buf->overlays_after = next;
2652
2653 /* Search thru overlays_before for where to put it. */
2654 other_prev = Qnil;
2655 for (other = buf->overlays_before;
2656 CONSP (other);
2657 other_prev = other, other = XCONS (other)->cdr)
2658 {
2659 Lisp_Object otherend, otheroverlay;
2660 int win;
2661
2662 otheroverlay = XCONS (other)->car;
2663 if (! OVERLAY_VALID (otheroverlay))
2664 abort ();
2665
2666 otherend = OVERLAY_END (otheroverlay);
2667 if (OVERLAY_POSITION (otherend) <= where)
2668 break;
2669 }
2670
2671 /* Add TAIL to overlays_before before OTHER. */
2672 XCONS (tail)->cdr = other;
2673 if (!NILP (other_prev))
2674 XCONS (other_prev)->cdr = tail;
2675 else
2676 buf->overlays_before = tail;
2677 tail = prev;
2678 }
2679 }
2680
2681 XSETFASTINT (buf->overlay_center, pos);
2682 }
2683
2684 void
2685 adjust_overlays_for_insert (pos, length)
2686 int pos;
2687 int length;
2688 {
2689 /* After an insertion, the lists are still sorted properly,
2690 but we may need to update the value of the overlay center. */
2691 if (XFASTINT (current_buffer->overlay_center) >= pos)
2692 XSETFASTINT (current_buffer->overlay_center,
2693 XFASTINT (current_buffer->overlay_center) + length);
2694 }
2695
2696 void
2697 adjust_overlays_for_delete (pos, length)
2698 int pos;
2699 int length;
2700 {
2701 if (XFASTINT (current_buffer->overlay_center) < pos)
2702 /* The deletion was to our right. No change needed; the before- and
2703 after-lists are still consistent. */
2704 ;
2705 else if (XFASTINT (current_buffer->overlay_center) > pos + length)
2706 /* The deletion was to our left. We need to adjust the center value
2707 to account for the change in position, but the lists are consistent
2708 given the new value. */
2709 XSETFASTINT (current_buffer->overlay_center,
2710 XFASTINT (current_buffer->overlay_center) - length);
2711 else
2712 /* We're right in the middle. There might be things on the after-list
2713 that now belong on the before-list. Recentering will move them,
2714 and also update the center point. */
2715 recenter_overlay_lists (current_buffer, pos);
2716 }
2717
2718 /* Fix up overlays that were garbled as a result of permuting markers
2719 in the range START through END. Any overlay with at least one
2720 endpoint in this range will need to be unlinked from the overlay
2721 list and reinserted in its proper place.
2722 Such an overlay might even have negative size at this point.
2723 If so, we'll reverse the endpoints. Can you think of anything
2724 better to do in this situation? */
2725 void
2726 fix_overlays_in_range (start, end)
2727 register int start, end;
2728 {
2729 Lisp_Object tem, overlay;
2730 Lisp_Object before_list, after_list;
2731 Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
2732 int startpos, endpos;
2733
2734 /* This algorithm shifts links around instead of consing and GCing.
2735 The loop invariant is that before_list (resp. after_list) is a
2736 well-formed list except that its last element, the one that
2737 *pbefore (resp. *pafter) points to, is still uninitialized.
2738 So it's not a bug that before_list isn't initialized, although
2739 it may look strange. */
2740 for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
2741 {
2742 overlay = XCONS (*ptail)->car;
2743 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2744 if (endpos < start)
2745 break;
2746 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2747 if (endpos < end
2748 || (startpos >= start && startpos < end))
2749 {
2750 /* If the overlay is backwards, fix that now. */
2751 if (startpos > endpos)
2752 {
2753 int tem;
2754 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
2755 Qnil);
2756 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
2757 Qnil);
2758 tem = startpos; startpos = endpos; endpos = tem;
2759 }
2760 /* Add it to the end of the wrong list. Later on,
2761 recenter_overlay_lists will move it to the right place. */
2762 if (endpos < XINT (current_buffer->overlay_center))
2763 {
2764 *pafter = *ptail;
2765 pafter = &XCONS (*ptail)->cdr;
2766 }
2767 else
2768 {
2769 *pbefore = *ptail;
2770 pbefore = &XCONS (*ptail)->cdr;
2771 }
2772 *ptail = XCONS (*ptail)->cdr;
2773 }
2774 else
2775 ptail = &XCONS (*ptail)->cdr;
2776 }
2777 for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
2778 {
2779 overlay = XCONS (*ptail)->car;
2780 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
2781 if (startpos >= end)
2782 break;
2783 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
2784 if (startpos >= start
2785 || (endpos >= start && endpos < end))
2786 {
2787 if (startpos > endpos)
2788 {
2789 int tem;
2790 Fset_marker (OVERLAY_START (overlay), make_number (endpos),
2791 Qnil);
2792 Fset_marker (OVERLAY_END (overlay), make_number (startpos),
2793 Qnil);
2794 tem = startpos; startpos = endpos; endpos = tem;
2795 }
2796 if (endpos < XINT (current_buffer->overlay_center))
2797 {
2798 *pafter = *ptail;
2799 pafter = &XCONS (*ptail)->cdr;
2800 }
2801 else
2802 {
2803 *pbefore = *ptail;
2804 pbefore = &XCONS (*ptail)->cdr;
2805 }
2806 *ptail = XCONS (*ptail)->cdr;
2807 }
2808 else
2809 ptail = &XCONS (*ptail)->cdr;
2810 }
2811
2812 /* Splice the constructed (wrong) lists into the buffer's lists,
2813 and let the recenter function make it sane again. */
2814 *pbefore = current_buffer->overlays_before;
2815 current_buffer->overlays_before = before_list;
2816 recenter_overlay_lists (current_buffer,
2817 XINT (current_buffer->overlay_center));
2818
2819 *pafter = current_buffer->overlays_after;
2820 current_buffer->overlays_after = after_list;
2821 recenter_overlay_lists (current_buffer,
2822 XINT (current_buffer->overlay_center));
2823 }
2824
2825 /* We have two types of overlay: the one whose ending marker is
2826 after-insertion-marker (this is the usual case) and the one whose
2827 ending marker is before-insertion-marker. When `overlays_before'
2828 contains overlays of the latter type and the former type in this
2829 order and both overlays end at inserting position, inserting a text
2830 increases only the ending marker of the latter type, which results
2831 in incorrect ordering of `overlays_before'.
2832
2833 This function fixes ordering of overlays in the slot
2834 `overlays_before' of the buffer *BP. Before the insertion, `point'
2835 was at PREV, and now is at POS. */
2836
2837 void
2838 fix_overlays_before (bp, prev, pos)
2839 struct buffer *bp;
2840 int prev, pos;
2841 {
2842 Lisp_Object *tailp = &bp->overlays_before;
2843 Lisp_Object *right_place;
2844 int end;
2845
2846 /* After the insertion, the several overlays may be in incorrect
2847 order. The possibility is that, in the list `overlays_before',
2848 an overlay which ends at POS appears after an overlay which ends
2849 at PREV. Since POS is greater than PREV, we must fix the
2850 ordering of these overlays, by moving overlays ends at POS before
2851 the overlays ends at PREV. */
2852
2853 /* At first, find a place where disordered overlays should be linked
2854 in. It is where an overlay which end before POS exists. (i.e. an
2855 overlay whose ending marker is after-insertion-marker if disorder
2856 exists). */
2857 while (!NILP (*tailp)
2858 && ((end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car)))
2859 >= pos))
2860 tailp = &XCONS (*tailp)->cdr;
2861
2862 /* If we don't find such an overlay,
2863 or the found one ends before PREV,
2864 or the found one is the last one in the list,
2865 we don't have to fix anything. */
2866 if (NILP (*tailp)
2867 || end < prev
2868 || NILP (XCONS (*tailp)->cdr))
2869 return;
2870
2871 right_place = tailp;
2872 tailp = &XCONS (*tailp)->cdr;
2873
2874 /* Now, end position of overlays in the list *TAILP should be before
2875 or equal to PREV. In the loop, an overlay which ends at POS is
2876 moved ahead to the place pointed by RIGHT_PLACE. If we found an
2877 overlay which ends before PREV, the remaining overlays are in
2878 correct order. */
2879 while (!NILP (*tailp))
2880 {
2881 end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car));
2882
2883 if (end == pos)
2884 { /* This overlay is disordered. */
2885 Lisp_Object found = *tailp;
2886
2887 /* Unlink the found overlay. */
2888 *tailp = XCONS (found)->cdr;
2889 /* Move an overlay at RIGHT_PLACE to the next of the found one. */
2890 XCONS (found)->cdr = *right_place;
2891 /* Link it into the right place. */
2892 *right_place = found;
2893 }
2894 else if (end == prev)
2895 tailp = &XCONS (*tailp)->cdr;
2896 else /* No more disordered overlay. */
2897 break;
2898 }
2899 }
2900 \f
2901 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
2902 "Return t if OBJECT is an overlay.")
2903 (object)
2904 Lisp_Object object;
2905 {
2906 return (OVERLAYP (object) ? Qt : Qnil);
2907 }
2908
2909 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
2910 "Create a new overlay with range BEG to END in BUFFER.\n\
2911 If omitted, BUFFER defaults to the current buffer.\n\
2912 BEG and END may be integers or markers.\n\
2913 The fourth arg FRONT-ADVANCE, if non-nil, makes the\n\
2914 front delimiter advance when text is inserted there.\n\
2915 The fifth arg REAR-ADVANCE, if non-nil, makes the\n\
2916 rear delimiter advance when text is inserted there.")
2917 (beg, end, buffer, front_advance, rear_advance)
2918 Lisp_Object beg, end, buffer;
2919 Lisp_Object front_advance, rear_advance;
2920 {
2921 Lisp_Object overlay;
2922 struct buffer *b;
2923
2924 if (NILP (buffer))
2925 XSETBUFFER (buffer, current_buffer);
2926 else
2927 CHECK_BUFFER (buffer, 2);
2928 if (MARKERP (beg)
2929 && ! EQ (Fmarker_buffer (beg), buffer))
2930 error ("Marker points into wrong buffer");
2931 if (MARKERP (end)
2932 && ! EQ (Fmarker_buffer (end), buffer))
2933 error ("Marker points into wrong buffer");
2934
2935 CHECK_NUMBER_COERCE_MARKER (beg, 1);
2936 CHECK_NUMBER_COERCE_MARKER (end, 1);
2937
2938 if (XINT (beg) > XINT (end))
2939 {
2940 Lisp_Object temp;
2941 temp = beg; beg = end; end = temp;
2942 }
2943
2944 b = XBUFFER (buffer);
2945
2946 beg = Fset_marker (Fmake_marker (), beg, buffer);
2947 end = Fset_marker (Fmake_marker (), end, buffer);
2948
2949 if (!NILP (front_advance))
2950 XMARKER (beg)->insertion_type = 1;
2951 if (!NILP (rear_advance))
2952 XMARKER (end)->insertion_type = 1;
2953
2954 overlay = allocate_misc ();
2955 XMISCTYPE (overlay) = Lisp_Misc_Overlay;
2956 XOVERLAY (overlay)->start = beg;
2957 XOVERLAY (overlay)->end = end;
2958 XOVERLAY (overlay)->plist = Qnil;
2959
2960 /* Put the new overlay on the wrong list. */
2961 end = OVERLAY_END (overlay);
2962 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
2963 b->overlays_after = Fcons (overlay, b->overlays_after);
2964 else
2965 b->overlays_before = Fcons (overlay, b->overlays_before);
2966
2967 /* This puts it in the right list, and in the right order. */
2968 recenter_overlay_lists (b, XINT (b->overlay_center));
2969
2970 /* We don't need to redisplay the region covered by the overlay, because
2971 the overlay has no properties at the moment. */
2972
2973 return overlay;
2974 }
2975 \f
2976 /* Mark a section of BUF as needing redisplay because of overlays changes. */
2977
2978 static void
2979 modify_overlay (buf, start, end)
2980 struct buffer *buf;
2981 int start, end;
2982 {
2983 if (start == end)
2984 return;
2985
2986 if (start > end)
2987 {
2988 int temp = start;
2989 start = end; end = temp;
2990 }
2991
2992 /* If this is a buffer not in the selected window,
2993 we must do other windows. */
2994 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2995 windows_or_buffers_changed = 1;
2996 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2997 else if (buf != current_buffer)
2998 windows_or_buffers_changed = 1;
2999 /* If multiple windows show this buffer, we must do other windows. */
3000 else if (buffer_shared > 1)
3001 windows_or_buffers_changed = 1;
3002 else
3003 {
3004 if (unchanged_modified == MODIFF
3005 && overlay_unchanged_modified == OVERLAY_MODIFF)
3006 {
3007 beg_unchanged = start - BEG;
3008 end_unchanged = Z - end;
3009 }
3010 else
3011 {
3012 if (Z - end < end_unchanged)
3013 end_unchanged = Z - end;
3014 if (start - BEG < beg_unchanged)
3015 beg_unchanged = start - BEG;
3016 }
3017 }
3018
3019 ++BUF_OVERLAY_MODIFF (buf);
3020 }
3021
3022 \f\f
3023 Lisp_Object Fdelete_overlay ();
3024
3025 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
3026 "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
3027 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
3028 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current\n\
3029 buffer.")
3030 (overlay, beg, end, buffer)
3031 Lisp_Object overlay, beg, end, buffer;
3032 {
3033 struct buffer *b, *ob;
3034 Lisp_Object obuffer;
3035 int count = specpdl_ptr - specpdl;
3036
3037 CHECK_OVERLAY (overlay, 0);
3038 if (NILP (buffer))
3039 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3040 if (NILP (buffer))
3041 XSETBUFFER (buffer, current_buffer);
3042 CHECK_BUFFER (buffer, 3);
3043
3044 if (MARKERP (beg)
3045 && ! EQ (Fmarker_buffer (beg), buffer))
3046 error ("Marker points into wrong buffer");
3047 if (MARKERP (end)
3048 && ! EQ (Fmarker_buffer (end), buffer))
3049 error ("Marker points into wrong buffer");
3050
3051 CHECK_NUMBER_COERCE_MARKER (beg, 1);
3052 CHECK_NUMBER_COERCE_MARKER (end, 1);
3053
3054 if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
3055 return Fdelete_overlay (overlay);
3056
3057 if (XINT (beg) > XINT (end))
3058 {
3059 Lisp_Object temp;
3060 temp = beg; beg = end; end = temp;
3061 }
3062
3063 specbind (Qinhibit_quit, Qt);
3064
3065 obuffer = Fmarker_buffer (OVERLAY_START (overlay));
3066 b = XBUFFER (buffer);
3067 ob = XBUFFER (obuffer);
3068
3069 /* If the overlay has changed buffers, do a thorough redisplay. */
3070 if (!EQ (buffer, obuffer))
3071 {
3072 /* Redisplay where the overlay was. */
3073 if (!NILP (obuffer))
3074 {
3075 int o_beg;
3076 int o_end;
3077
3078 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3079 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3080
3081 modify_overlay (ob, o_beg, o_end);
3082 }
3083
3084 /* Redisplay where the overlay is going to be. */
3085 modify_overlay (b, XINT (beg), XINT (end));
3086 }
3087 else
3088 /* Redisplay the area the overlay has just left, or just enclosed. */
3089 {
3090 int o_beg, o_end;
3091 int change_beg, change_end;
3092
3093 o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
3094 o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
3095
3096 if (o_beg == XINT (beg))
3097 modify_overlay (b, o_end, XINT (end));
3098 else if (o_end == XINT (end))
3099 modify_overlay (b, o_beg, XINT (beg));
3100 else
3101 {
3102 if (XINT (beg) < o_beg) o_beg = XINT (beg);
3103 if (XINT (end) > o_end) o_end = XINT (end);
3104 modify_overlay (b, o_beg, o_end);
3105 }
3106 }
3107
3108 if (!NILP (obuffer))
3109 {
3110 ob->overlays_before = Fdelq (overlay, ob->overlays_before);
3111 ob->overlays_after = Fdelq (overlay, ob->overlays_after);
3112 }
3113
3114 Fset_marker (OVERLAY_START (overlay), beg, buffer);
3115 Fset_marker (OVERLAY_END (overlay), end, buffer);
3116
3117 /* Put the overlay on the wrong list. */
3118 end = OVERLAY_END (overlay);
3119 if (OVERLAY_POSITION (end) < XINT (b->overlay_center))
3120 b->overlays_after = Fcons (overlay, b->overlays_after);
3121 else
3122 b->overlays_before = Fcons (overlay, b->overlays_before);
3123
3124 /* This puts it in the right list, and in the right order. */
3125 recenter_overlay_lists (b, XINT (b->overlay_center));
3126
3127 return unbind_to (count, overlay);
3128 }
3129
3130 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
3131 "Delete the overlay OVERLAY from its buffer.")
3132 (overlay)
3133 Lisp_Object overlay;
3134 {
3135 Lisp_Object buffer;
3136 struct buffer *b;
3137 int count = specpdl_ptr - specpdl;
3138
3139 CHECK_OVERLAY (overlay, 0);
3140
3141 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3142 if (NILP (buffer))
3143 return Qnil;
3144
3145 b = XBUFFER (buffer);
3146
3147 specbind (Qinhibit_quit, Qt);
3148
3149 b->overlays_before = Fdelq (overlay, b->overlays_before);
3150 b->overlays_after = Fdelq (overlay, b->overlays_after);
3151
3152 modify_overlay (b,
3153 marker_position (OVERLAY_START (overlay)),
3154 marker_position (OVERLAY_END (overlay)));
3155
3156 Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
3157 Fset_marker (OVERLAY_END (overlay), Qnil, Qnil);
3158
3159 return unbind_to (count, Qnil);
3160 }
3161 \f
3162 /* Overlay dissection functions. */
3163
3164 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
3165 "Return the position at which OVERLAY starts.")
3166 (overlay)
3167 Lisp_Object overlay;
3168 {
3169 CHECK_OVERLAY (overlay, 0);
3170
3171 return (Fmarker_position (OVERLAY_START (overlay)));
3172 }
3173
3174 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
3175 "Return the position at which OVERLAY ends.")
3176 (overlay)
3177 Lisp_Object overlay;
3178 {
3179 CHECK_OVERLAY (overlay, 0);
3180
3181 return (Fmarker_position (OVERLAY_END (overlay)));
3182 }
3183
3184 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
3185 "Return the buffer OVERLAY belongs to.")
3186 (overlay)
3187 Lisp_Object overlay;
3188 {
3189 CHECK_OVERLAY (overlay, 0);
3190
3191 return Fmarker_buffer (OVERLAY_START (overlay));
3192 }
3193
3194 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
3195 "Return a list of the properties on OVERLAY.\n\
3196 This is a copy of OVERLAY's plist; modifying its conses has no effect on\n\
3197 OVERLAY.")
3198 (overlay)
3199 Lisp_Object overlay;
3200 {
3201 CHECK_OVERLAY (overlay, 0);
3202
3203 return Fcopy_sequence (XOVERLAY (overlay)->plist);
3204 }
3205
3206 \f
3207 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
3208 "Return a list of the overlays that contain position POS.")
3209 (pos)
3210 Lisp_Object pos;
3211 {
3212 int noverlays;
3213 Lisp_Object *overlay_vec;
3214 int len;
3215 Lisp_Object result;
3216
3217 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3218
3219 len = 10;
3220 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3221
3222 /* Put all the overlays we want in a vector in overlay_vec.
3223 Store the length in len. */
3224 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3225 (int *) 0, (int *) 0);
3226
3227 /* Make a list of them all. */
3228 result = Flist (noverlays, overlay_vec);
3229
3230 xfree (overlay_vec);
3231 return result;
3232 }
3233
3234 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
3235 "Return a list of the overlays that overlap the region BEG ... END.\n\
3236 Overlap means that at least one character is contained within the overlay\n\
3237 and also contained within the specified region.\n\
3238 Empty overlays are included in the result if they are located at BEG\n\
3239 or between BEG and END.")
3240 (beg, end)
3241 Lisp_Object beg, end;
3242 {
3243 int noverlays;
3244 Lisp_Object *overlay_vec;
3245 int len;
3246 Lisp_Object result;
3247
3248 CHECK_NUMBER_COERCE_MARKER (beg, 0);
3249 CHECK_NUMBER_COERCE_MARKER (end, 0);
3250
3251 len = 10;
3252 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3253
3254 /* Put all the overlays we want in a vector in overlay_vec.
3255 Store the length in len. */
3256 noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len,
3257 (int *) 0, (int *) 0);
3258
3259 /* Make a list of them all. */
3260 result = Flist (noverlays, overlay_vec);
3261
3262 xfree (overlay_vec);
3263 return result;
3264 }
3265
3266 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
3267 1, 1, 0,
3268 "Return the next position after POS where an overlay starts or ends.\n\
3269 If there are no more overlay boundaries after POS, return (point-max).")
3270 (pos)
3271 Lisp_Object pos;
3272 {
3273 int noverlays;
3274 int endpos;
3275 Lisp_Object *overlay_vec;
3276 int len;
3277 int i;
3278
3279 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3280
3281 len = 10;
3282 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3283
3284 /* Put all the overlays we want in a vector in overlay_vec.
3285 Store the length in len.
3286 endpos gets the position where the next overlay starts. */
3287 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3288 &endpos, (int *) 0);
3289
3290 /* If any of these overlays ends before endpos,
3291 use its ending point instead. */
3292 for (i = 0; i < noverlays; i++)
3293 {
3294 Lisp_Object oend;
3295 int oendpos;
3296
3297 oend = OVERLAY_END (overlay_vec[i]);
3298 oendpos = OVERLAY_POSITION (oend);
3299 if (oendpos < endpos)
3300 endpos = oendpos;
3301 }
3302
3303 xfree (overlay_vec);
3304 return make_number (endpos);
3305 }
3306
3307 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
3308 Sprevious_overlay_change, 1, 1, 0,
3309 "Return the previous position before POS where an overlay starts or ends.\n\
3310 If there are no more overlay boundaries before POS, return (point-min).")
3311 (pos)
3312 Lisp_Object pos;
3313 {
3314 int noverlays;
3315 int prevpos;
3316 Lisp_Object *overlay_vec;
3317 int len;
3318 int i;
3319 Lisp_Object tail;
3320
3321 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3322
3323 len = 10;
3324 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
3325
3326 /* At beginning of buffer, we know the answer;
3327 avoid bug subtracting 1 below. */
3328 if (XINT (pos) == BEGV)
3329 return pos;
3330
3331 /* Put all the overlays we want in a vector in overlay_vec.
3332 Store the length in len.
3333 prevpos gets the position of an overlay end. */
3334 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
3335 (int *) 0, &prevpos);
3336
3337 /* If any of these overlays starts after prevpos,
3338 maybe use its starting point instead. */
3339 for (i = 0; i < noverlays; i++)
3340 {
3341 Lisp_Object ostart;
3342 int ostartpos;
3343
3344 ostart = OVERLAY_START (overlay_vec[i]);
3345 ostartpos = OVERLAY_POSITION (ostart);
3346 if (ostartpos > prevpos && ostartpos < XINT (pos))
3347 prevpos = ostartpos;
3348 }
3349
3350 /* If any overlay ends at pos, consider its starting point too. */
3351 for (tail = current_buffer->overlays_before;
3352 GC_CONSP (tail);
3353 tail = XCONS (tail)->cdr)
3354 {
3355 Lisp_Object overlay, ostart;
3356 int ostartpos;
3357
3358 overlay = XCONS (tail)->car;
3359
3360 ostart = OVERLAY_START (overlay);
3361 ostartpos = OVERLAY_POSITION (ostart);
3362 if (ostartpos > prevpos && ostartpos < XINT (pos))
3363 prevpos = ostartpos;
3364 }
3365
3366 xfree (overlay_vec);
3367 return make_number (prevpos);
3368 }
3369 \f
3370 /* These functions are for debugging overlays. */
3371
3372 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
3373 "Return a pair of lists giving all the overlays of the current buffer.\n\
3374 The car has all the overlays before the overlay center;\n\
3375 the cdr has all the overlays after the overlay center.\n\
3376 Recentering overlays moves overlays between these lists.\n\
3377 The lists you get are copies, so that changing them has no effect.\n\
3378 However, the overlays you get are the real objects that the buffer uses.")
3379 ()
3380 {
3381 Lisp_Object before, after;
3382 before = current_buffer->overlays_before;
3383 if (CONSP (before))
3384 before = Fcopy_sequence (before);
3385 after = current_buffer->overlays_after;
3386 if (CONSP (after))
3387 after = Fcopy_sequence (after);
3388
3389 return Fcons (before, after);
3390 }
3391
3392 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
3393 "Recenter the overlays of the current buffer around position POS.")
3394 (pos)
3395 Lisp_Object pos;
3396 {
3397 CHECK_NUMBER_COERCE_MARKER (pos, 0);
3398
3399 recenter_overlay_lists (current_buffer, XINT (pos));
3400 return Qnil;
3401 }
3402 \f
3403 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
3404 "Get the property of overlay OVERLAY with property name PROP.")
3405 (overlay, prop)
3406 Lisp_Object overlay, prop;
3407 {
3408 Lisp_Object plist, fallback;
3409
3410 CHECK_OVERLAY (overlay, 0);
3411
3412 fallback = Qnil;
3413
3414 for (plist = XOVERLAY (overlay)->plist;
3415 CONSP (plist) && CONSP (XCONS (plist)->cdr);
3416 plist = XCONS (XCONS (plist)->cdr)->cdr)
3417 {
3418 if (EQ (XCONS (plist)->car, prop))
3419 return XCONS (XCONS (plist)->cdr)->car;
3420 else if (EQ (XCONS (plist)->car, Qcategory))
3421 {
3422 Lisp_Object tem;
3423 tem = Fcar (Fcdr (plist));
3424 if (SYMBOLP (tem))
3425 fallback = Fget (tem, prop);
3426 }
3427 }
3428
3429 return fallback;
3430 }
3431
3432 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
3433 "Set one property of overlay OVERLAY: give property PROP value VALUE.")
3434 (overlay, prop, value)
3435 Lisp_Object overlay, prop, value;
3436 {
3437 Lisp_Object tail, buffer;
3438 int changed;
3439
3440 CHECK_OVERLAY (overlay, 0);
3441
3442 buffer = Fmarker_buffer (OVERLAY_START (overlay));
3443
3444 for (tail = XOVERLAY (overlay)->plist;
3445 CONSP (tail) && CONSP (XCONS (tail)->cdr);
3446 tail = XCONS (XCONS (tail)->cdr)->cdr)
3447 if (EQ (XCONS (tail)->car, prop))
3448 {
3449 changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
3450 XCONS (XCONS (tail)->cdr)->car = value;
3451 goto found;
3452 }
3453 /* It wasn't in the list, so add it to the front. */
3454 changed = !NILP (value);
3455 XOVERLAY (overlay)->plist
3456 = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
3457 found:
3458 if (! NILP (buffer))
3459 {
3460 if (changed)
3461 modify_overlay (XBUFFER (buffer),
3462 marker_position (OVERLAY_START (overlay)),
3463 marker_position (OVERLAY_END (overlay)));
3464 if (EQ (prop, Qevaporate) && ! NILP (value)
3465 && (OVERLAY_POSITION (OVERLAY_START (overlay))
3466 == OVERLAY_POSITION (OVERLAY_END (overlay))))
3467 Fdelete_overlay (overlay);
3468 }
3469 return value;
3470 }
3471 \f
3472 /* Subroutine of report_overlay_modification. */
3473
3474 /* Lisp vector holding overlay hook functions to call.
3475 Vector elements come in pairs.
3476 Each even-index element is a list of hook functions.
3477 The following odd-index element is the overlay they came from.
3478
3479 Before the buffer change, we fill in this vector
3480 as we call overlay hook functions.
3481 After the buffer change, we get the functions to call from this vector.
3482 This way we always call the same functions before and after the change. */
3483 static Lisp_Object last_overlay_modification_hooks;
3484
3485 /* Number of elements actually used in last_overlay_modification_hooks. */
3486 static int last_overlay_modification_hooks_used;
3487
3488 /* Add one functionlist/overlay pair
3489 to the end of last_overlay_modification_hooks. */
3490
3491 static void
3492 add_overlay_mod_hooklist (functionlist, overlay)
3493 Lisp_Object functionlist, overlay;
3494 {
3495 int oldsize = XVECTOR (last_overlay_modification_hooks)->size;
3496
3497 if (last_overlay_modification_hooks_used == oldsize)
3498 {
3499 Lisp_Object old;
3500 old = last_overlay_modification_hooks;
3501 last_overlay_modification_hooks
3502 = Fmake_vector (make_number (oldsize * 2), Qnil);
3503 bcopy (XVECTOR (old)->contents,
3504 XVECTOR (last_overlay_modification_hooks)->contents,
3505 sizeof (Lisp_Object) * oldsize);
3506 }
3507 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = functionlist;
3508 XVECTOR (last_overlay_modification_hooks)->contents[last_overlay_modification_hooks_used++] = overlay;
3509 }
3510 \f
3511 /* Run the modification-hooks of overlays that include
3512 any part of the text in START to END.
3513 If this change is an insertion, also
3514 run the insert-before-hooks of overlay starting at END,
3515 and the insert-after-hooks of overlay ending at START.
3516
3517 This is called both before and after the modification.
3518 AFTER is nonzero when we call after the modification.
3519
3520 ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
3521 When AFTER is nonzero, they are the start position,
3522 the position after the inserted new text,
3523 and the length of deleted or replaced old text. */
3524
3525 void
3526 report_overlay_modification (start, end, after, arg1, arg2, arg3)
3527 Lisp_Object start, end;
3528 int after;
3529 Lisp_Object arg1, arg2, arg3;
3530 {
3531 Lisp_Object prop, overlay, tail;
3532 /* 1 if this change is an insertion. */
3533 int insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end));
3534 int tail_copied;
3535 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3536
3537 overlay = Qnil;
3538 tail = Qnil;
3539 GCPRO5 (overlay, tail, arg1, arg2, arg3);
3540
3541 if (after)
3542 {
3543 /* Call the functions recorded in last_overlay_modification_hooks
3544 rather than scanning the overlays again.
3545 First copy the vector contents, in case some of these hooks
3546 do subsequent modification of the buffer. */
3547 int size = last_overlay_modification_hooks_used;
3548 Lisp_Object *copy = (Lisp_Object *) alloca (size * sizeof (Lisp_Object));
3549 int i;
3550
3551 bcopy (XVECTOR (last_overlay_modification_hooks)->contents,
3552 copy, size * sizeof (Lisp_Object));
3553 gcpro1.var = copy;
3554 gcpro1.nvars = size;
3555
3556 for (i = 0; i < size;)
3557 {
3558 Lisp_Object prop, overlay;
3559 prop = copy[i++];
3560 overlay = copy[i++];
3561 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3562 }
3563 UNGCPRO;
3564 return;
3565 }
3566
3567 /* We are being called before a change.
3568 Scan the overlays to find the functions to call. */
3569 last_overlay_modification_hooks_used = 0;
3570 tail_copied = 0;
3571 for (tail = current_buffer->overlays_before;
3572 CONSP (tail);
3573 tail = XCONS (tail)->cdr)
3574 {
3575 int startpos, endpos;
3576 Lisp_Object ostart, oend;
3577
3578 overlay = XCONS (tail)->car;
3579
3580 ostart = OVERLAY_START (overlay);
3581 oend = OVERLAY_END (overlay);
3582 endpos = OVERLAY_POSITION (oend);
3583 if (XFASTINT (start) > endpos)
3584 break;
3585 startpos = OVERLAY_POSITION (ostart);
3586 if (insertion && (XFASTINT (start) == startpos
3587 || XFASTINT (end) == startpos))
3588 {
3589 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3590 if (!NILP (prop))
3591 {
3592 /* Copy TAIL in case the hook recenters the overlay lists. */
3593 if (!tail_copied)
3594 tail = Fcopy_sequence (tail);
3595 tail_copied = 1;
3596 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3597 }
3598 }
3599 if (insertion && (XFASTINT (start) == endpos
3600 || XFASTINT (end) == endpos))
3601 {
3602 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3603 if (!NILP (prop))
3604 {
3605 if (!tail_copied)
3606 tail = Fcopy_sequence (tail);
3607 tail_copied = 1;
3608 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3609 }
3610 }
3611 /* Test for intersecting intervals. This does the right thing
3612 for both insertion and deletion. */
3613 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3614 {
3615 prop = Foverlay_get (overlay, Qmodification_hooks);
3616 if (!NILP (prop))
3617 {
3618 if (!tail_copied)
3619 tail = Fcopy_sequence (tail);
3620 tail_copied = 1;
3621 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3622 }
3623 }
3624 }
3625
3626 tail_copied = 0;
3627 for (tail = current_buffer->overlays_after;
3628 CONSP (tail);
3629 tail = XCONS (tail)->cdr)
3630 {
3631 int startpos, endpos;
3632 Lisp_Object ostart, oend;
3633
3634 overlay = XCONS (tail)->car;
3635
3636 ostart = OVERLAY_START (overlay);
3637 oend = OVERLAY_END (overlay);
3638 startpos = OVERLAY_POSITION (ostart);
3639 endpos = OVERLAY_POSITION (oend);
3640 if (XFASTINT (end) < startpos)
3641 break;
3642 if (insertion && (XFASTINT (start) == startpos
3643 || XFASTINT (end) == startpos))
3644 {
3645 prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
3646 if (!NILP (prop))
3647 {
3648 if (!tail_copied)
3649 tail = Fcopy_sequence (tail);
3650 tail_copied = 1;
3651 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3652 }
3653 }
3654 if (insertion && (XFASTINT (start) == endpos
3655 || XFASTINT (end) == endpos))
3656 {
3657 prop = Foverlay_get (overlay, Qinsert_behind_hooks);
3658 if (!NILP (prop))
3659 {
3660 if (!tail_copied)
3661 tail = Fcopy_sequence (tail);
3662 tail_copied = 1;
3663 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3664 }
3665 }
3666 /* Test for intersecting intervals. This does the right thing
3667 for both insertion and deletion. */
3668 if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
3669 {
3670 prop = Foverlay_get (overlay, Qmodification_hooks);
3671 if (!NILP (prop))
3672 {
3673 if (!tail_copied)
3674 tail = Fcopy_sequence (tail);
3675 tail_copied = 1;
3676 call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
3677 }
3678 }
3679 }
3680
3681 UNGCPRO;
3682 }
3683
3684 static void
3685 call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
3686 Lisp_Object list, overlay;
3687 int after;
3688 Lisp_Object arg1, arg2, arg3;
3689 {
3690 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3691
3692 GCPRO4 (list, arg1, arg2, arg3);
3693 if (! after)
3694 add_overlay_mod_hooklist (list, overlay);
3695
3696 while (!NILP (list))
3697 {
3698 if (NILP (arg3))
3699 call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
3700 else
3701 call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
3702 list = Fcdr (list);
3703 }
3704 UNGCPRO;
3705 }
3706
3707 /* Delete any zero-sized overlays at position POS, if the `evaporate'
3708 property is set. */
3709 void
3710 evaporate_overlays (pos)
3711 int pos;
3712 {
3713 Lisp_Object tail, overlay, hit_list;
3714
3715 hit_list = Qnil;
3716 if (pos <= XFASTINT (current_buffer->overlay_center))
3717 for (tail = current_buffer->overlays_before; CONSP (tail);
3718 tail = XCONS (tail)->cdr)
3719 {
3720 int endpos;
3721 overlay = XCONS (tail)->car;
3722 endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
3723 if (endpos < pos)
3724 break;
3725 if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
3726 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3727 hit_list = Fcons (overlay, hit_list);
3728 }
3729 else
3730 for (tail = current_buffer->overlays_after; CONSP (tail);
3731 tail = XCONS (tail)->cdr)
3732 {
3733 int startpos;
3734 overlay = XCONS (tail)->car;
3735 startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
3736 if (startpos > pos)
3737 break;
3738 if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
3739 && ! NILP (Foverlay_get (overlay, Qevaporate)))
3740 hit_list = Fcons (overlay, hit_list);
3741 }
3742 for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
3743 Fdelete_overlay (XCONS (hit_list)->car);
3744 }
3745 \f
3746 /* Somebody has tried to store a value with an unacceptable type
3747 in the slot with offset OFFSET. */
3748
3749 void
3750 buffer_slot_type_mismatch (offset)
3751 int offset;
3752 {
3753 Lisp_Object sym;
3754 char *type_name;
3755 sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
3756 switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
3757 {
3758 case Lisp_Int: type_name = "integers"; break;
3759 case Lisp_String: type_name = "strings"; break;
3760 case Lisp_Symbol: type_name = "symbols"; break;
3761
3762 default:
3763 abort ();
3764 }
3765
3766 error ("Only %s should be stored in the buffer-local variable %s",
3767 type_name, XSYMBOL (sym)->name->data);
3768 }
3769 \f
3770 init_buffer_once ()
3771 {
3772 register Lisp_Object tem;
3773
3774 buffer_permanent_local_flags = 0;
3775
3776 /* Make sure all markable slots in buffer_defaults
3777 are initialized reasonably, so mark_buffer won't choke. */
3778 reset_buffer (&buffer_defaults);
3779 reset_buffer_local_variables (&buffer_defaults, 1);
3780 reset_buffer (&buffer_local_symbols);
3781 reset_buffer_local_variables (&buffer_local_symbols, 1);
3782 /* Prevent GC from getting confused. */
3783 buffer_defaults.text = &buffer_defaults.own_text;
3784 buffer_local_symbols.text = &buffer_local_symbols.own_text;
3785 #ifdef USE_TEXT_PROPERTIES
3786 BUF_INTERVALS (&buffer_defaults) = 0;
3787 BUF_INTERVALS (&buffer_local_symbols) = 0;
3788 #endif
3789 XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
3790 XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
3791
3792 /* Set up the default values of various buffer slots. */
3793 /* Must do these before making the first buffer! */
3794
3795 /* real setup is done in loaddefs.el */
3796 buffer_defaults.mode_line_format = build_string ("%-");
3797 buffer_defaults.abbrev_mode = Qnil;
3798 buffer_defaults.overwrite_mode = Qnil;
3799 buffer_defaults.case_fold_search = Qt;
3800 buffer_defaults.auto_fill_function = Qnil;
3801 buffer_defaults.selective_display = Qnil;
3802 #ifndef old
3803 buffer_defaults.selective_display_ellipses = Qt;
3804 #endif
3805 buffer_defaults.abbrev_table = Qnil;
3806 buffer_defaults.display_table = Qnil;
3807 buffer_defaults.undo_list = Qnil;
3808 buffer_defaults.mark_active = Qnil;
3809 buffer_defaults.file_format = Qnil;
3810 buffer_defaults.overlays_before = Qnil;
3811 buffer_defaults.overlays_after = Qnil;
3812 XSETFASTINT (buffer_defaults.overlay_center, BEG);
3813
3814 XSETFASTINT (buffer_defaults.tab_width, 8);
3815 buffer_defaults.truncate_lines = Qnil;
3816 buffer_defaults.ctl_arrow = Qt;
3817 buffer_defaults.direction_reversed = Qnil;
3818
3819 #ifdef DOS_NT
3820 buffer_defaults.buffer_file_type = Qnil; /* TEXT */
3821 #endif
3822 buffer_defaults.enable_multibyte_characters = Qt;
3823 buffer_defaults.buffer_file_coding_system = Qnil;
3824 XSETFASTINT (buffer_defaults.fill_column, 70);
3825 XSETFASTINT (buffer_defaults.left_margin, 0);
3826 buffer_defaults.cache_long_line_scans = Qnil;
3827 buffer_defaults.file_truename = Qnil;
3828 XSETFASTINT (buffer_defaults.display_count, 0);
3829
3830 /* Assign the local-flags to the slots that have default values.
3831 The local flag is a bit that is used in the buffer
3832 to say that it has its own local value for the slot.
3833 The local flag bits are in the local_var_flags slot of the buffer. */
3834
3835 /* Nothing can work if this isn't true */
3836 if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
3837
3838 /* 0 means not a lisp var, -1 means always local, else mask */
3839 bzero (&buffer_local_flags, sizeof buffer_local_flags);
3840 XSETINT (buffer_local_flags.filename, -1);
3841 XSETINT (buffer_local_flags.directory, -1);
3842 XSETINT (buffer_local_flags.backed_up, -1);
3843 XSETINT (buffer_local_flags.save_length, -1);
3844 XSETINT (buffer_local_flags.auto_save_file_name, -1);
3845 XSETINT (buffer_local_flags.read_only, -1);
3846 XSETINT (buffer_local_flags.major_mode, -1);
3847 XSETINT (buffer_local_flags.mode_name, -1);
3848 XSETINT (buffer_local_flags.undo_list, -1);
3849 XSETINT (buffer_local_flags.mark_active, -1);
3850 XSETINT (buffer_local_flags.point_before_scroll, -1);
3851 XSETINT (buffer_local_flags.file_truename, -1);
3852 XSETINT (buffer_local_flags.invisibility_spec, -1);
3853 XSETINT (buffer_local_flags.file_format, -1);
3854 XSETINT (buffer_local_flags.display_count, -1);
3855 XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
3856
3857 XSETFASTINT (buffer_local_flags.mode_line_format, 1);
3858 XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
3859 XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
3860 XSETFASTINT (buffer_local_flags.case_fold_search, 8);
3861 XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
3862 XSETFASTINT (buffer_local_flags.selective_display, 0x20);
3863 #ifndef old
3864 XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
3865 #endif
3866 XSETFASTINT (buffer_local_flags.tab_width, 0x80);
3867 XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
3868 XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
3869 XSETFASTINT (buffer_local_flags.fill_column, 0x400);
3870 XSETFASTINT (buffer_local_flags.left_margin, 0x800);
3871 XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
3872 XSETFASTINT (buffer_local_flags.display_table, 0x2000);
3873 #ifdef DOS_NT
3874 XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
3875 /* Make this one a permanent local. */
3876 buffer_permanent_local_flags |= 0x4000;
3877 #endif
3878 XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
3879 XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
3880 XSETFASTINT (buffer_local_flags.category_table, 0x20000);
3881 XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
3882 XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x80000);
3883 /* Make this one a permanent local. */
3884 buffer_permanent_local_flags |= 0x80000;
3885
3886 Vbuffer_alist = Qnil;
3887 current_buffer = 0;
3888 all_buffers = 0;
3889
3890 QSFundamental = build_string ("Fundamental");
3891
3892 Qfundamental_mode = intern ("fundamental-mode");
3893 buffer_defaults.major_mode = Qfundamental_mode;
3894
3895 Qmode_class = intern ("mode-class");
3896
3897 Qprotected_field = intern ("protected-field");
3898
3899 Qpermanent_local = intern ("permanent-local");
3900
3901 Qkill_buffer_hook = intern ("kill-buffer-hook");
3902
3903 Vprin1_to_string_buffer = Fget_buffer_create (build_string (" prin1"));
3904
3905 /* super-magic invisible buffer */
3906 Vbuffer_alist = Qnil;
3907
3908 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3909 }
3910
3911 init_buffer ()
3912 {
3913 char buf[MAXPATHLEN+1];
3914 char *pwd;
3915 struct stat dotstat, pwdstat;
3916 Lisp_Object temp;
3917 int rc;
3918
3919 Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
3920 if (NILP (buffer_defaults.enable_multibyte_characters))
3921 Fset_buffer_multibyte (Qnil);
3922
3923 /* If PWD is accurate, use it instead of calling getwd. This is faster
3924 when PWD is right, and may avoid a fatal error. */
3925 if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
3926 && stat (pwd, &pwdstat) == 0
3927 && stat (".", &dotstat) == 0
3928 && dotstat.st_ino == pwdstat.st_ino
3929 && dotstat.st_dev == pwdstat.st_dev
3930 && strlen (pwd) < MAXPATHLEN)
3931 strcpy (buf, pwd);
3932 #ifdef HAVE_GETCWD
3933 else if (getcwd (buf, MAXPATHLEN+1) == 0)
3934 fatal ("`getcwd' failed: %s\n", strerror (errno));
3935 #else
3936 else if (getwd (buf) == 0)
3937 fatal ("`getwd' failed: %s\n", buf);
3938 #endif
3939
3940 #ifndef VMS
3941 /* Maybe this should really use some standard subroutine
3942 whose definition is filename syntax dependent. */
3943 rc = strlen (buf);
3944 if (!(IS_DIRECTORY_SEP (buf[rc - 1])))
3945 {
3946 buf[rc] = DIRECTORY_SEP;
3947 buf[rc + 1] = '\0';
3948 }
3949 #endif /* not VMS */
3950
3951 current_buffer->directory = build_string (buf);
3952
3953 /* Add /: to the front of the name
3954 if it would otherwise be treated as magic. */
3955 temp = Ffind_file_name_handler (current_buffer->directory, Qt);
3956 if (! NILP (temp)
3957 /* If the default dir is just /, TEMP is non-nil
3958 because of the ange-ftp completion handler.
3959 However, it is not necessary to turn / into /:/.
3960 So avoid doing that. */
3961 && strcmp ("/", XSTRING (current_buffer->directory)->data))
3962 current_buffer->directory
3963 = concat2 (build_string ("/:"), current_buffer->directory);
3964
3965 temp = get_minibuffer (0);
3966 XBUFFER (temp)->directory = current_buffer->directory;
3967 }
3968
3969 /* initialize the buffer routines */
3970 syms_of_buffer ()
3971 {
3972 extern Lisp_Object Qdisabled;
3973
3974 staticpro (&last_overlay_modification_hooks);
3975 last_overlay_modification_hooks
3976 = Fmake_vector (make_number (10), Qnil);
3977
3978 staticpro (&Vbuffer_defaults);
3979 staticpro (&Vbuffer_local_symbols);
3980 staticpro (&Qfundamental_mode);
3981 staticpro (&Qmode_class);
3982 staticpro (&QSFundamental);
3983 staticpro (&Vbuffer_alist);
3984 staticpro (&Qprotected_field);
3985 staticpro (&Qpermanent_local);
3986 staticpro (&Qkill_buffer_hook);
3987 Qoverlayp = intern ("overlayp");
3988 staticpro (&Qoverlayp);
3989 Qevaporate = intern ("evaporate");
3990 staticpro (&Qevaporate);
3991 Qmodification_hooks = intern ("modification-hooks");
3992 staticpro (&Qmodification_hooks);
3993 Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
3994 staticpro (&Qinsert_in_front_hooks);
3995 Qinsert_behind_hooks = intern ("insert-behind-hooks");
3996 staticpro (&Qinsert_behind_hooks);
3997 Qget_file_buffer = intern ("get-file-buffer");
3998 staticpro (&Qget_file_buffer);
3999 Qpriority = intern ("priority");
4000 staticpro (&Qpriority);
4001 Qwindow = intern ("window");
4002 staticpro (&Qwindow);
4003 Qbefore_string = intern ("before-string");
4004 staticpro (&Qbefore_string);
4005 Qafter_string = intern ("after-string");
4006 staticpro (&Qafter_string);
4007 Qfirst_change_hook = intern ("first-change-hook");
4008 staticpro (&Qfirst_change_hook);
4009 Qbefore_change_functions = intern ("before-change-functions");
4010 staticpro (&Qbefore_change_functions);
4011 Qafter_change_functions = intern ("after-change-functions");
4012 staticpro (&Qafter_change_functions);
4013
4014 Fput (Qprotected_field, Qerror_conditions,
4015 Fcons (Qprotected_field, Fcons (Qerror, Qnil)));
4016 Fput (Qprotected_field, Qerror_message,
4017 build_string ("Attempt to modify a protected field"));
4018
4019 /* All these use DEFVAR_LISP_NOPRO because the slots in
4020 buffer_defaults will all be marked via Vbuffer_defaults. */
4021
4022 DEFVAR_LISP_NOPRO ("default-mode-line-format",
4023 &buffer_defaults.mode_line_format,
4024 "Default value of `mode-line-format' for buffers that don't override it.\n\
4025 This is the same as (default-value 'mode-line-format).");
4026
4027 DEFVAR_LISP_NOPRO ("default-abbrev-mode",
4028 &buffer_defaults.abbrev_mode,
4029 "Default value of `abbrev-mode' for buffers that do not override it.\n\
4030 This is the same as (default-value 'abbrev-mode).");
4031
4032 DEFVAR_LISP_NOPRO ("default-ctl-arrow",
4033 &buffer_defaults.ctl_arrow,
4034 "Default value of `ctl-arrow' for buffers that do not override it.\n\
4035 This is the same as (default-value 'ctl-arrow).");
4036
4037 DEFVAR_LISP_NOPRO ("default-direction-reversed",
4038 &buffer_defaults.direction_reversed,
4039 "Default value of `direction_reversed' for buffers that do not override it.\n\
4040 This is the same as (default-value 'direction-reversed).");
4041
4042 DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
4043 &buffer_defaults.enable_multibyte_characters,
4044 "Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
4045 This is the same as (default-value 'enable-multibyte-characters).");
4046
4047 DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
4048 &buffer_defaults.buffer_file_coding_system,
4049 "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
4050 This is the same as (default-value 'buffer-file-coding-system).");
4051
4052 DEFVAR_LISP_NOPRO ("default-truncate-lines",
4053 &buffer_defaults.truncate_lines,
4054 "Default value of `truncate-lines' for buffers that do not override it.\n\
4055 This is the same as (default-value 'truncate-lines).");
4056
4057 DEFVAR_LISP_NOPRO ("default-fill-column",
4058 &buffer_defaults.fill_column,
4059 "Default value of `fill-column' for buffers that do not override it.\n\
4060 This is the same as (default-value 'fill-column).");
4061
4062 DEFVAR_LISP_NOPRO ("default-left-margin",
4063 &buffer_defaults.left_margin,
4064 "Default value of `left-margin' for buffers that do not override it.\n\
4065 This is the same as (default-value 'left-margin).");
4066
4067 DEFVAR_LISP_NOPRO ("default-tab-width",
4068 &buffer_defaults.tab_width,
4069 "Default value of `tab-width' for buffers that do not override it.\n\
4070 This is the same as (default-value 'tab-width).");
4071
4072 DEFVAR_LISP_NOPRO ("default-case-fold-search",
4073 &buffer_defaults.case_fold_search,
4074 "Default value of `case-fold-search' for buffers that don't override it.\n\
4075 This is the same as (default-value 'case-fold-search).");
4076
4077 #ifdef DOS_NT
4078 DEFVAR_LISP_NOPRO ("default-buffer-file-type",
4079 &buffer_defaults.buffer_file_type,
4080 "Default file type for buffers that do not override it.\n\
4081 This is the same as (default-value 'buffer-file-type).\n\
4082 The file type is nil for text, t for binary.");
4083 #endif
4084
4085 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
4086 Qnil, 0);
4087
4088 /* This doc string is too long for cpp; cpp dies if it isn't in a comment.
4089 But make-docfile finds it!
4090 DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format,
4091 Qnil,
4092 "Template for displaying mode line for current buffer.\n\
4093 Each buffer has its own value of this variable.\n\
4094 Value may be a string, a symbol or a list or cons cell.\n\
4095 For a symbol, its value is used (but it is ignored if t or nil).\n\
4096 A string appearing directly as the value of a symbol is processed verbatim\n\
4097 in that the %-constructs below are not recognized.\n\
4098 For a list whose car is a symbol, the symbol's value is taken,\n\
4099 and if that is non-nil, the cadr of the list is processed recursively.\n\
4100 Otherwise, the caddr of the list (if there is one) is processed.\n\
4101 For a list whose car is a string or list, each element is processed\n\
4102 recursively and the results are effectively concatenated.\n\
4103 For a list whose car is an integer, the cdr of the list is processed\n\
4104 and padded (if the number is positive) or truncated (if negative)\n\
4105 to the width specified by that number.\n\
4106 A string is printed verbatim in the mode line except for %-constructs:\n\
4107 (%-constructs are allowed when the string is the entire mode-line-format\n\
4108 or when it is found in a cons-cell or a list)\n\
4109 %b -- print buffer name. %f -- print visited file name.\n\
4110 %F -- print frame name.\n\
4111 %* -- print %, * or hyphen. %+ -- print *, % or hyphen.\n\
4112 % means buffer is read-only and * means it is modified.\n\
4113 For a modified read-only buffer, %* gives % and %+ gives *.\n\
4114 %s -- print process status. %l -- print the current line number.\n\
4115 %c -- print the current column number (this makes editing slower).\n\
4116 To make the column number update correctly in all cases,\n\
4117 `column-number-mode' must be non-nil.\n\
4118 %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
4119 %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
4120 or print Bottom or All.\n\
4121 %n -- print Narrow if appropriate.\n\
4122 %t -- print T if file is text, B if binary.\n\
4123 %[ -- print one [ for each recursive editing level. %] similar.\n\
4124 %% -- print %. %- -- print infinitely many dashes.\n\
4125 Decimal digits after the % specify field width to which to pad.");
4126 */
4127
4128 DEFVAR_LISP_NOPRO ("default-major-mode", &buffer_defaults.major_mode,
4129 "*Major mode for new buffers. Defaults to `fundamental-mode'.\n\
4130 nil here means use current buffer's major mode.");
4131
4132 DEFVAR_PER_BUFFER ("major-mode", &current_buffer->major_mode,
4133 make_number (Lisp_Symbol),
4134 "Symbol for current buffer's major mode.");
4135
4136 DEFVAR_PER_BUFFER ("mode-name", &current_buffer->mode_name,
4137 make_number (Lisp_String),
4138 "Pretty name of current buffer's major mode (a string).");
4139
4140 DEFVAR_PER_BUFFER ("abbrev-mode", &current_buffer->abbrev_mode, Qnil,
4141 "Non-nil turns on automatic expansion of abbrevs as they are inserted.\n\
4142 Automatically becomes buffer-local when set in any fashion.");
4143
4144 DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
4145 Qnil,
4146 "*Non-nil if searches should ignore case.\n\
4147 Automatically becomes buffer-local when set in any fashion.");
4148
4149 DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
4150 make_number (Lisp_Int),
4151 "*Column beyond which automatic line-wrapping should happen.\n\
4152 Automatically becomes buffer-local when set in any fashion.");
4153
4154 DEFVAR_PER_BUFFER ("left-margin", &current_buffer->left_margin,
4155 make_number (Lisp_Int),
4156 "*Column for the default indent-line-function to indent to.\n\
4157 Linefeed indents to this column in Fundamental mode.\n\
4158 Automatically becomes buffer-local when set in any fashion.");
4159
4160 DEFVAR_PER_BUFFER ("tab-width", &current_buffer->tab_width,
4161 make_number (Lisp_Int),
4162 "*Distance between tab stops (for display of tab characters), in columns.\n\
4163 Automatically becomes buffer-local when set in any fashion.");
4164
4165 DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
4166 "*Non-nil means display control chars with uparrow.\n\
4167 A value of nil means use backslash and octal digits.\n\
4168 Automatically becomes buffer-local when set in any fashion.\n\
4169 This variable does not apply to characters whose display is specified\n\
4170 in the current display table (if there is one).");
4171
4172 DEFVAR_PER_BUFFER ("enable-multibyte-characters",
4173 &current_buffer->enable_multibyte_characters,
4174 make_number (-1),
4175 "*Non-nil means the buffer contents are regarded as multi-byte form\n\
4176 of characters, not a binary code. This affects the display, file I/O,\n\
4177 and behaviors of various editing commands.");
4178
4179 DEFVAR_PER_BUFFER ("buffer-file-coding-system",
4180 &current_buffer->buffer_file_coding_system, Qnil,
4181 "Coding system to be used for encoding the buffer contents on saving.\n\
4182 If it is nil, the buffer is saved without any code conversion unless\n\
4183 some coding system is specified in file-coding-system-alist\n\
4184 for the buffer file.\n\
4185 \n\
4186 This variable is never applied to a way of decoding\n\
4187 a file while reading it.");
4188
4189 DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
4190 Qnil,
4191 "*Non-nil means lines in the buffer are displayed right to left.");
4192
4193 DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
4194 "*Non-nil means do not display continuation lines;\n\
4195 give each line of text one screen line.\n\
4196 Automatically becomes buffer-local when set in any fashion.\n\
4197 \n\
4198 Note that this is overridden by the variable\n\
4199 `truncate-partial-width-windows' if that variable is non-nil\n\
4200 and this buffer is not full-frame width.");
4201
4202 #ifdef DOS_NT
4203 DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
4204 Qnil,
4205 "Non-nil if the visited file is a binary file.\n\
4206 This variable is meaningful on MS-DOG and Windows NT.\n\
4207 On those systems, it is automatically local in every buffer.\n\
4208 On other systems, this variable is normally always nil.");
4209 #endif
4210
4211 DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
4212 make_number (Lisp_String),
4213 "Name of default directory of current buffer. Should end with slash.\n\
4214 Each buffer has its own value of this variable.");
4215
4216 DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
4217 Qnil,
4218 "Function called (if non-nil) to perform auto-fill.\n\
4219 It is called after self-inserting a space or newline.\n\
4220 Each buffer has its own value of this variable.\n\
4221 NOTE: This variable is not a hook;\n\
4222 its value may not be a list of functions.");
4223
4224 DEFVAR_PER_BUFFER ("buffer-file-name", &current_buffer->filename,
4225 make_number (Lisp_String),
4226 "Name of file visited in current buffer, or nil if not visiting a file.\n\
4227 Each buffer has its own value of this variable.");
4228
4229 DEFVAR_PER_BUFFER ("buffer-file-truename", &current_buffer->file_truename,
4230 make_number (Lisp_String),
4231 "Abbreviated truename of file visited in current buffer, or nil if none.\n\
4232 The truename of a file is calculated by `file-truename'\n\
4233 and then abbreviated with `abbreviate-file-name'.\n\
4234 Each buffer has its own value of this variable.");
4235
4236 DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
4237 &current_buffer->auto_save_file_name,
4238 make_number (Lisp_String),
4239 "Name of file for auto-saving current buffer,\n\
4240 or nil if buffer should not be auto-saved.\n\
4241 Each buffer has its own value of this variable.");
4242
4243 DEFVAR_PER_BUFFER ("buffer-read-only", &current_buffer->read_only, Qnil,
4244 "Non-nil if this buffer is read-only.\n\
4245 Each buffer has its own value of this variable.");
4246
4247 DEFVAR_PER_BUFFER ("buffer-backed-up", &current_buffer->backed_up, Qnil,
4248 "Non-nil if this buffer's file has been backed up.\n\
4249 Backing up is done before the first time the file is saved.\n\
4250 Each buffer has its own value of this variable.");
4251
4252 DEFVAR_PER_BUFFER ("buffer-saved-size", &current_buffer->save_length,
4253 make_number (Lisp_Int),
4254 "Length of current buffer when last read in, saved or auto-saved.\n\
4255 0 initially.\n\
4256 Each buffer has its own value of this variable.");
4257
4258 DEFVAR_PER_BUFFER ("selective-display", &current_buffer->selective_display,
4259 Qnil,
4260 "Non-nil enables selective display:\n\
4261 Integer N as value means display only lines\n\
4262 that start with less than n columns of space.\n\
4263 A value of t means, after a ^M, all the rest of the line is invisible.\n\
4264 Then ^M's in the file are written into files as newlines.\n\n\
4265 Automatically becomes buffer-local when set in any fashion.");
4266
4267 #ifndef old
4268 DEFVAR_PER_BUFFER ("selective-display-ellipses",
4269 &current_buffer->selective_display_ellipses,
4270 Qnil,
4271 "t means display ... on previous line when a line is invisible.\n\
4272 Automatically becomes buffer-local when set in any fashion.");
4273 #endif
4274
4275 DEFVAR_PER_BUFFER ("overwrite-mode", &current_buffer->overwrite_mode, Qnil,
4276 "Non-nil if self-insertion should replace existing text.\n\
4277 The value should be one of `overwrite-mode-textual',\n\
4278 `overwrite-mode-binary', or nil.\n\
4279 If it is `overwrite-mode-textual', self-insertion still\n\
4280 inserts at the end of a line, and inserts when point is before a tab,\n\
4281 until the tab is filled in.\n\
4282 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.\n\
4283 Automatically becomes buffer-local when set in any fashion.");
4284
4285 #if 0 /* The doc string is too long for some compilers,
4286 but make-docfile can find it in this comment. */
4287 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4288 Qnil,
4289 "Display table that controls display of the contents of current buffer.\n\
4290 Automatically becomes buffer-local when set in any fashion.\n\
4291 The display table is a char-table created with `make-display-table'.\n\
4292 The ordinary char-table elements control how to display each possible text\n\
4293 character. Each value should be a vector of characters or nil;\n\
4294 nil means display the character in the default fashion.\n\
4295 There are six extra slots to control the display of\n\
4296 the end of a truncated screen line (extra-slot 0, a single character);\n\
4297 the end of a continued line (extra-slot 1, a single character);\n\
4298 the escape character used to display character codes in octal\n\
4299 (extra-slot 2, a single character);\n\
4300 the character used as an arrow for control characters (extra-slot 3,\n\
4301 a single character);\n\
4302 the decoration indicating the presence of invisible lines (extra-slot 4,\n\
4303 a vector of characters);\n\
4304 the character used to draw the border between side-by-side windows\n\
4305 (extra-slot 5, a single character).\n\
4306 See also the functions `display-table-slot' and `set-display-table-slot'.\n\
4307 If this variable is nil, the value of `standard-display-table' is used.\n\
4308 Each window can have its own, overriding display table.");
4309 #endif
4310 DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
4311 Qnil, 0);
4312
4313 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
4314 "Don't ask.");
4315 */
4316 DEFVAR_LISP ("before-change-function", &Vbefore_change_function,
4317 "If non-nil, a function to call before each text change (obsolete).\n\
4318 Two arguments are passed to the function: the positions of\n\
4319 the beginning and end of the range of old text to be changed.\n\
4320 \(For an insertion, the beginning and end are at the same place.)\n\
4321 No information is given about the length of the text after the change.\n\
4322 \n\
4323 Buffer changes made while executing the `before-change-function'\n\
4324 don't call any before-change or after-change functions.\n\
4325 That's because these variables are temporarily set to nil.\n\
4326 As a result, a hook function cannot straightforwardly alter the value of\n\
4327 these variables. See the Emacs Lisp manual for a way of\n\
4328 accomplishing an equivalent result by using other variables.\n\n\
4329 This variable is obsolete; use `before-change-functions' instead.");
4330 Vbefore_change_function = Qnil;
4331
4332 DEFVAR_LISP ("after-change-function", &Vafter_change_function,
4333 "If non-nil, a Function to call after each text change (obsolete).\n\
4334 Three arguments are passed to the function: the positions of\n\
4335 the beginning and end of the range of changed text,\n\
4336 and the length of the pre-change text replaced by that range.\n\
4337 \(For an insertion, the pre-change length is zero;\n\
4338 for a deletion, that length is the number of bytes deleted,\n\
4339 and the post-change beginning and end are at the same place.)\n\
4340 \n\
4341 Buffer changes made while executing the `after-change-function'\n\
4342 don't call any before-change or after-change functions.\n\
4343 That's because these variables are temporarily set to nil.\n\
4344 As a result, a hook function cannot straightforwardly alter the value of\n\
4345 these variables. See the Emacs Lisp manual for a way of\n\
4346 accomplishing an equivalent result by using other variables.\n\n\
4347 This variable is obsolete; use `after-change-functions' instead.");
4348 Vafter_change_function = Qnil;
4349
4350 DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
4351 "List of functions to call before each text change.\n\
4352 Two arguments are passed to each function: the positions of\n\
4353 the beginning and end of the range of old text to be changed.\n\
4354 \(For an insertion, the beginning and end are at the same place.)\n\
4355 No information is given about the length of the text after the change.\n\
4356 \n\
4357 Buffer changes made while executing the `before-change-functions'\n\
4358 don't call any before-change or after-change functions.\n\
4359 That's because these variables are temporarily set to nil.\n\
4360 As a result, a hook function cannot straightforwardly alter the value of\n\
4361 these variables. See the Emacs Lisp manual for a way of\n\
4362 accomplishing an equivalent result by using other variables.");
4363 Vbefore_change_functions = Qnil;
4364
4365 DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
4366 "List of function to call after each text change.\n\
4367 Three arguments are passed to each function: the positions of\n\
4368 the beginning and end of the range of changed text,\n\
4369 and the length in bytes of the pre-change text replaced by that range.\n\
4370 \(For an insertion, the pre-change length is zero;\n\
4371 for a deletion, that length is the number of bytes deleted,\n\
4372 and the post-change beginning and end are at the same place.)\n\
4373 \n\
4374 Buffer changes made while executing the `after-change-functions'\n\
4375 don't call any before-change or after-change functions.\n\
4376 That's because these variables are temporarily set to nil.\n\
4377 As a result, a hook function cannot straightforwardly alter the value of\n\
4378 these variables. See the Emacs Lisp manual for a way of\n\
4379 accomplishing an equivalent result by using other variables.");
4380
4381 Vafter_change_functions = Qnil;
4382
4383 DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
4384 "A list of functions to call before changing a buffer which is unmodified.\n\
4385 The functions are run using the `run-hooks' function.");
4386 Vfirst_change_hook = Qnil;
4387
4388 #if 0 /* The doc string is too long for some compilers,
4389 but make-docfile can find it in this comment. */
4390 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4391 "List of undo entries in current buffer.\n\
4392 Recent changes come first; older changes follow newer.\n\
4393 \n\
4394 An entry (BEG . END) represents an insertion which begins at\n\
4395 position BEG and ends at position END.\n\
4396 \n\
4397 An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
4398 from (abs POSITION). If POSITION is positive, point was at the front\n\
4399 of the text being deleted; if negative, point was at the end.\n\
4400 \n\
4401 An entry (t HIGH . LOW) indicates that the buffer previously had\n\
4402 \"unmodified\" status. HIGH and LOW are the high and low 16-bit portions\n\
4403 of the visited file's modification time, as of that time. If the\n\
4404 modification time of the most recent save is different, this entry is\n\
4405 obsolete.\n\
4406 \n\
4407 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
4408 was modified between BEG and END. PROPERTY is the property name,\n\
4409 and VALUE is the old value.\n\
4410 \n\
4411 An entry (MARKER . DISTANCE) indicates that the marker MARKER\n\
4412 was adjusted in position by the offset DISTANCE (an integer).\n\
4413 \n\
4414 An entry of the form POSITION indicates that point was at the buffer\n\
4415 location given by the integer. Undoing an entry of this form places\n\
4416 point at POSITION.\n\
4417 \n\
4418 nil marks undo boundaries. The undo command treats the changes\n\
4419 between two undo boundaries as a single step to be undone.\n\
4420 \n\
4421 If the value of the variable is t, undo information is not recorded.");
4422 #endif
4423 DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
4424 0);
4425
4426 DEFVAR_PER_BUFFER ("mark-active", &current_buffer->mark_active, Qnil,
4427 "Non-nil means the mark and region are currently active in this buffer.\n\
4428 Automatically local in all buffers.");
4429
4430 DEFVAR_PER_BUFFER ("cache-long-line-scans", &current_buffer->cache_long_line_scans, Qnil,
4431 "Non-nil means that Emacs should use caches to handle long lines more quickly.\n\
4432 This variable is buffer-local, in all buffers.\n\
4433 \n\
4434 Normally, the line-motion functions work by scanning the buffer for\n\
4435 newlines. Columnar operations (like move-to-column and\n\
4436 compute-motion) also work by scanning the buffer, summing character\n\
4437 widths as they go. This works well for ordinary text, but if the\n\
4438 buffer's lines are very long (say, more than 500 characters), these\n\
4439 motion functions will take longer to execute. Emacs may also take\n\
4440 longer to update the display.\n\
4441 \n\
4442 If cache-long-line-scans is non-nil, these motion functions cache the\n\
4443 results of their scans, and consult the cache to avoid rescanning\n\
4444 regions of the buffer until the text is modified. The caches are most\n\
4445 beneficial when they prevent the most searching---that is, when the\n\
4446 buffer contains long lines and large regions of characters with the\n\
4447 same, fixed screen width.\n\
4448 \n\
4449 When cache-long-line-scans is non-nil, processing short lines will\n\
4450 become slightly slower (because of the overhead of consulting the\n\
4451 cache), and the caches will use memory roughly proportional to the\n\
4452 number of newlines and characters whose screen width varies.\n\
4453 \n\
4454 The caches require no explicit maintenance; their accuracy is\n\
4455 maintained internally by the Emacs primitives. Enabling or disabling\n\
4456 the cache should not affect the behavior of any of the motion\n\
4457 functions; it should only affect their performance.");
4458
4459 DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
4460 "Value of point before the last series of scroll operations, or nil.");
4461
4462 DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
4463 "List of formats to use when saving this buffer.\n\
4464 Formats are defined by `format-alist'. This variable is\n\
4465 set when a file is visited. Automatically local in all buffers.");
4466
4467 DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
4468 &current_buffer->invisibility_spec, Qnil,
4469 "Invisibility spec of this buffer.\n\
4470 The default is t, which means that text is invisible\n\
4471 if it has a non-nil `invisible' property.\n\
4472 If the value is a list, a text character is invisible if its `invisible'\n\
4473 property is an element in that list.\n\
4474 If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
4475 then characters with property value PROP are invisible,\n\
4476 and they have an ellipsis as well if ELLIPSIS is non-nil.");
4477
4478 DEFVAR_PER_BUFFER ("buffer-display-count",
4479 &current_buffer->display_count, Qnil,
4480 "A number incremented each time the buffer is displayed in a window.");
4481
4482 DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
4483 "*Non-nil means deactivate the mark when the buffer contents change.\n\
4484 Non-nil also enables highlighting of the region whenever the mark is active.\n\
4485 The variable `highlight-nonselected-windows' controls whether to highlight\n\
4486 all windows or just the selected window.");
4487 Vtransient_mark_mode = Qnil;
4488
4489 DEFVAR_LISP ("inhibit-read-only", &Vinhibit_read_only,
4490 "*Non-nil means disregard read-only status of buffers or characters.\n\
4491 If the value is t, disregard `buffer-read-only' and all `read-only'\n\
4492 text properties. If the value is a list, disregard `buffer-read-only'\n\
4493 and disregard a `read-only' text property if the property value\n\
4494 is a member of the list.");
4495 Vinhibit_read_only = Qnil;
4496
4497 DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
4498 "List of functions called with no args to query before killing a buffer.");
4499 Vkill_buffer_query_functions = Qnil;
4500
4501 defsubr (&Sbuffer_live_p);
4502 defsubr (&Sbuffer_list);
4503 defsubr (&Sget_buffer);
4504 defsubr (&Sget_file_buffer);
4505 defsubr (&Sget_buffer_create);
4506 defsubr (&Smake_indirect_buffer);
4507 defsubr (&Sgenerate_new_buffer_name);
4508 defsubr (&Sbuffer_name);
4509 /*defsubr (&Sbuffer_number);*/
4510 defsubr (&Sbuffer_file_name);
4511 defsubr (&Sbuffer_base_buffer);
4512 defsubr (&Sbuffer_local_variables);
4513 defsubr (&Sbuffer_modified_p);
4514 defsubr (&Sset_buffer_modified_p);
4515 defsubr (&Sbuffer_modified_tick);
4516 defsubr (&Srename_buffer);
4517 defsubr (&Sother_buffer);
4518 defsubr (&Sbuffer_disable_undo);
4519 defsubr (&Sbuffer_enable_undo);
4520 defsubr (&Skill_buffer);
4521 defsubr (&Sset_buffer_major_mode);
4522 defsubr (&Sswitch_to_buffer);
4523 defsubr (&Spop_to_buffer);
4524 defsubr (&Scurrent_buffer);
4525 defsubr (&Sset_buffer);
4526 defsubr (&Sbarf_if_buffer_read_only);
4527 defsubr (&Sbury_buffer);
4528 defsubr (&Serase_buffer);
4529 defsubr (&Sset_buffer_multibyte);
4530 defsubr (&Skill_all_local_variables);
4531
4532 defsubr (&Soverlayp);
4533 defsubr (&Smake_overlay);
4534 defsubr (&Sdelete_overlay);
4535 defsubr (&Smove_overlay);
4536 defsubr (&Soverlay_start);
4537 defsubr (&Soverlay_end);
4538 defsubr (&Soverlay_buffer);
4539 defsubr (&Soverlay_properties);
4540 defsubr (&Soverlays_at);
4541 defsubr (&Soverlays_in);
4542 defsubr (&Snext_overlay_change);
4543 defsubr (&Sprevious_overlay_change);
4544 defsubr (&Soverlay_recenter);
4545 defsubr (&Soverlay_lists);
4546 defsubr (&Soverlay_get);
4547 defsubr (&Soverlay_put);
4548 }
4549
4550 keys_of_buffer ()
4551 {
4552 initial_define_key (control_x_map, 'b', "switch-to-buffer");
4553 initial_define_key (control_x_map, 'k', "kill-buffer");
4554
4555 /* This must not be in syms_of_buffer, because Qdisabled is not
4556 initialized when that function gets called. */
4557 Fput (intern ("erase-buffer"), Qdisabled, Qt);
4558 }