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