root/trunk/src/buffer.c

Revision 4220, 190.6 kB (checked in by miyoshi, 8 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to native
Line 
1 /* Buffer manipulation primitives for GNU Emacs.
2    Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994,
3                  1995, 1997, 1998, 1999, 2000, 2001, 2002,
4                  2003, 2004, 2005, 2006, 2007, 2008
5                  Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING.  If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/param.h>
29 #include <errno.h>
30 #include <stdio.h>
31
32 #ifndef USE_CRT_DLL
33 extern int errno;
34 #endif
35
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "lisp.h"
42 #include "intervals.h"
43 #include "window.h"
44 #include "commands.h"
45 #include "buffer.h"
46 #include "charset.h"
47 #include "region-cache.h"
48 #include "indent.h"
49 #include "blockinput.h"
50 #include "keyboard.h"
51 #include "keymap.h"
52 #include "frame.h"
53
54 struct buffer *current_buffer;          /* the current buffer */
55
56 /* First buffer in chain of all buffers (in reverse order of creation).
57    Threaded through ->next.  */
58
59 struct buffer *all_buffers;
60
61 /* This structure holds the default values of the buffer-local variables
62    defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
63    The default value occupies the same slot in this structure
64    as an individual buffer's value occupies in that buffer.
65    Setting the default value also goes through the alist of buffers
66    and stores into each buffer that does not say it has a local value.  */
67
68 DECL_ALIGN (struct buffer, buffer_defaults);
69
70 /* A Lisp_Object pointer to the above, used for staticpro */
71
72 static Lisp_Object Vbuffer_defaults;
73
74 /* This structure marks which slots in a buffer have corresponding
75    default values in buffer_defaults.
76    Each such slot has a nonzero value in this structure.
77    The value has only one nonzero bit.
78
79    When a buffer has its own local value for a slot,
80    the entry for that slot (found in the same slot in this structure)
81    is turned on in the buffer's local_flags array.
82
83    If a slot in this structure is -1, 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 is -2, then there is no DEFVAR_PER_BUFFER for it,
88    but there is a default value which is copied into each buffer.
89
90    If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
91    zero, that is a bug */
92
93 struct buffer buffer_local_flags;
94
95 /* This structure holds the names of symbols whose values may be
96    buffer-local.  It is indexed and accessed in the same way as the above. */
97
98 DECL_ALIGN (struct buffer, buffer_local_symbols);
99
100 /* A Lisp_Object pointer to the above, used for staticpro */
101 static Lisp_Object Vbuffer_local_symbols;
102
103 /* This structure holds the required types for the values in the
104    buffer-local slots.  If a slot contains Qnil, then the
105    corresponding buffer slot may contain a value of any type.  If a
106    slot contains an integer, then prospective values' tags must be
107    equal to that integer (except nil is always allowed).
108    When a tag does not match, the function
109    buffer_slot_type_mismatch will signal an error.
110
111    If a slot here contains -1, the corresponding variable is read-only.  */
112 struct buffer buffer_local_types;
113
114 /* Flags indicating which built-in buffer-local variables
115    are permanent locals.  */
116 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
117
118 /* Number of per-buffer variables used.  */
119
120 int last_per_buffer_idx;
121
122 Lisp_Object Fset_buffer ();
123 void set_buffer_internal ();
124 void set_buffer_internal_1 ();
125 static void call_overlay_mod_hooks ();
126 static void swap_out_buffer_local_variables ();
127 static void reset_buffer_local_variables ();
128
129 /* Alist of all buffer names vs the buffers. */
130 /* This used to be a variable, but is no longer,
131  to prevent lossage due to user rplac'ing this alist or its elements.  */
132 Lisp_Object Vbuffer_alist;
133
134 /* Functions to call before and after each text change. */
135 Lisp_Object Vbefore_change_functions;
136 Lisp_Object Vafter_change_functions;
137
138 Lisp_Object Vtransient_mark_mode;
139
140 /* t means ignore all read-only text properties.
141    A list means ignore such a property if its value is a member of the list.
142    Any non-nil value means ignore buffer-read-only.  */
143 Lisp_Object Vinhibit_read_only;
144
145 /* List of functions to call that can query about killing a buffer.
146    If any of these functions returns nil, we don't kill it.  */
147 Lisp_Object Vkill_buffer_query_functions;
148 Lisp_Object Qkill_buffer_query_functions;
149
150 /* Hook run before changing a major mode.  */
151 Lisp_Object Vchange_major_mode_hook, Qchange_major_mode_hook;
152
153 /* List of functions to call before changing an unmodified buffer.  */
154 Lisp_Object Vfirst_change_hook;
155
156 Lisp_Object Qfirst_change_hook;
157 Lisp_Object Qbefore_change_functions;
158 Lisp_Object Qafter_change_functions;
159 Lisp_Object Qucs_set_table_for_input;
160
161 /* If nonzero, all modification hooks are suppressed.  */
162 int inhibit_modification_hooks;
163
164 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
165
166 Lisp_Object Qprotected_field;
167
168 Lisp_Object QSFundamental;      /* A string "Fundamental" */
169
170 Lisp_Object Qkill_buffer_hook;
171
172 Lisp_Object Qget_file_buffer;
173
174 Lisp_Object Qoverlayp;
175
176 Lisp_Object Qpriority, Qwindow, Qevaporate, Qbefore_string, Qafter_string;
177
178 Lisp_Object Qmodification_hooks;
179 Lisp_Object Qinsert_in_front_hooks;
180 Lisp_Object Qinsert_behind_hooks;
181
182 static void alloc_buffer_text P_ ((struct buffer *, size_t));
183 static void free_buffer_text P_ ((struct buffer *b));
184 static struct Lisp_Overlay * copy_overlays P_ ((struct buffer *, struct Lisp_Overlay *));
185 static void modify_overlay P_ ((struct buffer *, EMACS_INT, EMACS_INT));
186 static Lisp_Object buffer_lisp_local_variables P_ ((struct buffer *));
187
188
189 /* For debugging; temporary.  See set_buffer_internal.  */
190 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
191
192 void
193 nsberror (spec)
194      Lisp_Object spec;
195 {
196   if (STRINGP (spec))
197     error ("No buffer named %s", SDATA (spec));
198   error ("Invalid buffer argument");
199 }
200
201 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
202        doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
203 Value is nil if OBJECT is not a buffer or if it has been killed.  */)
204      (object)
205      Lisp_Object object;
206 {
207   return ((BUFFERP (object) && ! NILP (XBUFFER (object)->name))
208           ? Qt : Qnil);
209 }
210
211 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
212        doc: /* Return a list of all existing live buffers.
213 If the optional arg FRAME is a frame, we return the buffer list
214 in the proper order for that frame: the buffers in FRAME's `buffer-list'
215 frame parameter come first, followed by the rest of the buffers.  */)
216      (frame)
217      Lisp_Object frame;
218 {
219   Lisp_Object framelist, general;
220   general = Fmapcar (Qcdr, Vbuffer_alist);
221
222   if (FRAMEP (frame))
223     {
224       Lisp_Object tail;
225
226       CHECK_FRAME (frame);
227
228       framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
229
230       /* Remove from GENERAL any buffer that duplicates one in FRAMELIST.  */
231       tail = framelist;
232       while (! NILP (tail))
233         {
234           general = Fdelq (XCAR (tail), general);
235           tail = XCDR (tail);
236         }
237       return nconc2 (framelist, general);
238     }
239
240   return general;
241 }
242
243 /* Like Fassoc, but use Fstring_equal to compare
244    (which ignores text properties),
245    and don't ever QUIT.  */
246
247 static Lisp_Object
248 assoc_ignore_text_properties (key, list)
249      register Lisp_Object key;
250      Lisp_Object list;
251 {
252   register Lisp_Object tail;
253   for (tail = list; CONSP (tail); tail = XCDR (tail))
254     {
255       register Lisp_Object elt, tem;
256       elt = XCAR (tail);
257       tem = Fstring_equal (Fcar (elt), key);
258       if (!NILP (tem))
259         return elt;
260     }
261   return Qnil;
262 }
263
264 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
265        doc: /* Return the buffer named NAME (a string).
266 If there is no live buffer named NAME, return nil.
267 NAME may also be a buffer; if so, the value is that buffer.  */)
268      (name)
269      register Lisp_Object name;
270 {
271   if (BUFFERP (name))
272     return name;
273   CHECK_STRING (name);
274
275   return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
276 }
277
278 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
279        doc: /* Return the buffer visiting file FILENAME (a string).
280 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
281 If there is no such live buffer, return nil.
282 See also `find-buffer-visiting'.  */)
283      (filename)
284      register Lisp_Object filename;
285 {
286   register Lisp_Object tail, buf, tem;
287   Lisp_Object handler;
288
289   CHECK_STRING (filename);
290   filename = Fexpand_file_name (filename, Qnil);
291
292   /* If the file name has special constructs in it,
293      call the corresponding file handler.  */
294   handler = Ffind_file_name_handler (filename, Qget_file_buffer);
295   if (!NILP (handler))
296     return call2 (handler, Qget_file_buffer, filename);
297
298   for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
299     {
300       buf = Fcdr (XCAR (tail));
301       if (!BUFFERP (buf)) continue;
302       if (!STRINGP (XBUFFER (buf)->filename)) continue;
303       tem = Fstring_equal (XBUFFER (buf)->filename, filename);
304       if (!NILP (tem))
305         return buf;
306     }
307   return Qnil;
308 }
309
310 Lisp_Object
311 get_truename_buffer (filename)
312      register Lisp_Object filename;
313 {
314   register Lisp_Object tail, buf, tem;
315
316   for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
317     {
318       buf = Fcdr (XCAR (tail));
319       if (!BUFFERP (buf)) continue;
320       if (!STRINGP (XBUFFER (buf)->file_truename)) continue;
321       tem = Fstring_equal (XBUFFER (buf)->file_truename, filename);
322       if (!NILP (tem))
323         return buf;
324     }
325   return Qnil;
326 }
327
328 /* Incremented for each buffer created, to assign the buffer number. */
329 int buffer_count;
330
331 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
332        doc: /* Return the buffer named NAME, or create such a buffer and return it.
333 A new buffer is created if there is no live buffer named NAME.
334 If NAME starts with a space, the new buffer does not keep undo information.
335 If NAME is a buffer instead of a string, then it is the value returned.
336 The value is never nil.  */)
337      (name)
338      register Lisp_Object name;
339 {
340   register Lisp_Object buf;
341   register struct buffer *b;
342
343   buf = Fget_buffer (name);
344   if (!NILP (buf))
345     return buf;
346
347   if (SCHARS (name) == 0)
348     error ("Empty string for buffer name is not allowed");
349
350   b = (struct buffer *) allocate_buffer ();
351
352   b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
353
354   /* An ordinary buffer uses its own struct buffer_text.  */
355   b->text = &b->own_text;
356   b->base_buffer = 0;
357
358   BUF_GAP_SIZE (b) = 20;
359   BLOCK_INPUT;
360   /* We allocate extra 1-byte at the tail and keep it always '\0' for
361      anchoring a search.  */
362   alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
363   UNBLOCK_INPUT;
364   if (! BUF_BEG_ADDR (b))
365     buffer_memory_full ();
366
367   BUF_PT (b) = BEG;
368   BUF_GPT (b) = BEG;
369   BUF_BEGV (b) = BEG;
370   BUF_ZV (b) = BEG;
371   BUF_Z (b) = BEG;
372   BUF_PT_BYTE (b) = BEG_BYTE;
373   BUF_GPT_BYTE (b) = BEG_BYTE;
374   BUF_BEGV_BYTE (b) = BEG_BYTE;
375   BUF_ZV_BYTE (b) = BEG_BYTE;
376   BUF_Z_BYTE (b) = BEG_BYTE;
377   BUF_MODIFF (b) = 1;
378   BUF_CHARS_MODIFF (b) = 1;
379   BUF_OVERLAY_MODIFF (b) = 1;
380   BUF_SAVE_MODIFF (b) = 1;
381   BUF_INTERVALS (b) = 0;
382   BUF_UNCHANGED_MODIFIED (b) = 1;
383   BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
384   BUF_END_UNCHANGED (b) = 0;
385   BUF_BEG_UNCHANGED (b) = 0;
386   *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
387
388   b->newline_cache = 0;
389   b->width_run_cache = 0;
390   b->width_table = Qnil;
391   b->prevent_redisplay_optimizations_p = 1;
392
393   /* Put this on the chain of all buffers including killed ones.  */
394   b->next = all_buffers;
395   all_buffers = b;
396
397   /* An ordinary buffer normally doesn't need markers
398      to handle BEGV and ZV.  */
399   b->pt_marker = Qnil;
400   b->begv_marker = Qnil;
401   b->zv_marker = Qnil;
402
403   name = Fcopy_sequence (name);
404   STRING_SET_INTERVALS (name, NULL_INTERVAL);
405   b->name = name;
406
407   if (SREF (name, 0) != ' ')
408     b->undo_list = Qnil;
409   else
410     b->undo_list = Qt;
411
412   reset_buffer (b);
413   reset_buffer_local_variables (b, 1);
414
415   b->mark = Fmake_marker ();
416   BUF_MARKERS (b) = NULL;
417   b->name = name;
418
419   /* Put this in the alist of all live buffers.  */
420   XSETBUFFER (buf, b);
421   Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
422
423   /* An error in calling the function here (should someone redefine it)
424      can lead to infinite regress until you run out of stack.  rms
425      says that's not worth protecting against.  */
426   if (!NILP (Ffboundp (Qucs_set_table_for_input))
427 #ifdef MEADOW
428       /* When current_buffer is NULL, the following function call
429          causes a segmentation fault in find_symbol_value()@data.c.
430          This is a hack and further investigation is needed. */
431       && current_buffer != 0
432 #endif
433       )
434     /* buf is on buffer-alist, so no gcpro.  */
435     call1 (Qucs_set_table_for_input, buf);
436
437   return buf;
438 }
439
440
441 /* Return a list of overlays which is a copy of the overlay list
442    LIST, but for buffer B.  */
443
444 static struct Lisp_Overlay *
445 copy_overlays (b, list)
446      struct buffer *b;
447      struct Lisp_Overlay *list;
448 {
449   Lisp_Object buffer;
450   struct Lisp_Overlay *result = NULL, *tail = NULL;
451
452   XSETBUFFER (buffer, b);
453
454   for (; list; list = list->next)
455     {
456       Lisp_Object overlay, start, end, old_overlay;
457       EMACS_INT charpos;
458
459       XSETMISC (old_overlay, list);
460       charpos = marker_position (OVERLAY_START (old_overlay));
461       start = Fmake_marker ();
462       Fset_marker (start, make_number (charpos), buffer);
463       XMARKER (start)->insertion_type
464         = XMARKER (OVERLAY_START (old_overlay))->insertion_type;
465
466       charpos = marker_position (OVERLAY_END (old_overlay));
467       end = Fmake_marker ();
468       Fset_marker (end, make_number (charpos), buffer);
469       XMARKER (end)->insertion_type
470         = XMARKER (OVERLAY_END (old_overlay))->insertion_type;
471
472       overlay = allocate_misc ();
473       XMISCTYPE (overlay) = Lisp_Misc_Overlay;
474       OVERLAY_START (overlay) = start;
475       OVERLAY_END (overlay) = end;
476       OVERLAY_PLIST (overlay) = Fcopy_sequence (OVERLAY_PLIST (old_overlay));
477       XOVERLAY (overlay)->next = NULL;
478
479       if (tail)
480         tail = tail->next = XOVERLAY (overlay);
481       else
482         result = tail = XOVERLAY (overlay);
483     }
484
485   return result;
486 }
487
488
489 /* Clone per-buffer values of buffer FROM.
490
491    Buffer TO gets the same per-buffer values as FROM, with the
492    following exceptions: (1) TO's name is left untouched, (2) markers
493    are copied and made to refer to TO, and (3) overlay lists are
494    copied.  */
495
496 static void
497 clone_per_buffer_values (from, to)
498      struct buffer *from, *to;
499 {
500   Lisp_Object to_buffer;
501   int offset;
502
503   XSETBUFFER (to_buffer, to);
504
505   for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
506        offset < sizeof *to;
507        offset += sizeof (Lisp_Object))
508     {
509       Lisp_Object obj;
510
511       obj = PER_BUFFER_VALUE (from, offset);
512       if (MARKERP (obj))
513         {
514           struct Lisp_Marker *m = XMARKER (obj);
515           obj = Fmake_marker ();
516           XMARKER (obj)->insertion_type = m->insertion_type;
517           set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
518         }
519
520       PER_BUFFER_VALUE (to, offset) = obj;
521     }
522
523   bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
524
525   to->overlays_before = copy_overlays (to, from->overlays_before);
526   to->overlays_after = copy_overlays (to, from->overlays_after);
527
528   /* Get (a copy of) the alist of Lisp-level local variables of FROM
529      and install that in TO.  */
530   to->local_var_alist = buffer_lisp_local_variables (from);
531 }
532
533 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
534        2, 3,
535        "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
536        doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
537 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
538 NAME should be a string which is not the name of an existing buffer.
539 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
540 such as major and minor modes, in the indirect buffer.
541 CLONE nil means the indirect buffer's state is reset to default values.  */)
542      (base_buffer, name, clone)
543      Lisp_Object base_buffer, name, clone;
544 {
545   Lisp_Object buf, tem;
546   struct buffer *b;
547
548   CHECK_STRING (name);
549   buf = Fget_buffer (name);
550   if (!NILP (buf))
551     error ("Buffer name `%s' is in use", SDATA (name));
552
553   tem = base_buffer;
554   base_buffer = Fget_buffer (base_buffer);
555   if (NILP (base_buffer))
556     error ("No such buffer: `%s'", SDATA (tem));
557   if (NILP (XBUFFER (base_buffer)->name))
558     error ("Base buffer has been killed");
559
560   if (SCHARS (name) == 0)
561     error ("Empty string for buffer name is not allowed");
562
563   b = (struct buffer *) allocate_buffer ();
564   b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
565
566   if (XBUFFER (base_buffer)->base_buffer)
567     b->base_buffer = XBUFFER (base_buffer)->base_buffer;
568   else
569     b->base_buffer = XBUFFER (base_buffer);
570
571   /* Use the base buffer's text object.  */
572   b->text = b->base_buffer->text;
573
574   BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
575   BUF_ZV (b) = BUF_ZV (b->base_buffer);
576   BUF_PT (b) = BUF_PT (b->base_buffer);
577   BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
578   BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
579   BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
580
581   b->newline_cache = 0;
582   b->width_run_cache = 0;
583   b->width_table = Qnil;
584
585   /* Put this on the chain of all buffers including killed ones.  */
586   b->next = all_buffers;
587   all_buffers = b;
588
589   name = Fcopy_sequence (name);
590   STRING_SET_INTERVALS (name, NULL_INTERVAL);
591   b->name = name;
592
593   reset_buffer (b);
594   reset_buffer_local_variables (b, 1);
595
596