JimB's changes since January 18th
[bpt/emacs.git] / src / buffer.h
1 /* Header file for the buffer manipulation primitives.
2 Copyright (C) 1985, 1986, 1990, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #ifdef lint
22 #include "undo.h"
23 #endif /* lint */
24
25
26 #ifdef USE_TEXT_PROPERTIES
27 #define SET_PT(position) (set_point ((position), current_buffer))
28 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
29
30 #define BUF_SET_PT(buffer, position) (set_point ((position), (buffer)))
31 #define BUF_TEMP_SET_PT(buffer, position) (temp_set_point ((position), (buffer)))
32
33 #else /* don't support text properties */
34
35 #define SET_PT(position) (current_buffer->text.pt = (position))
36 #define TEMP_SET_PT(position) (current_buffer->text.pt = (position))
37
38 #define BUF_SET_PT(buffer, position) (buffer->text.pt = (position))
39 #define BUF_TEMP_SET_PT(buffer, position) (buffer->text.pt = (position))
40 #endif /* don't support text properties */
41
42 /* Character position of beginning of buffer. */
43 #define BEG (1)
44
45 /* Character position of beginning of accessible range of buffer. */
46 #define BEGV (current_buffer->text.begv)
47
48 /* Character position of point in buffer. The "+ 0" makes this
49 not an l-value, so you can't assign to it. Use SET_PT instead. */
50 #define PT (current_buffer->text.pt + 0)
51
52 /* Character position of gap in buffer. */
53 #define GPT (current_buffer->text.gpt)
54
55 /* Character position of end of accessible range of buffer. */
56 #define ZV (current_buffer->text.zv)
57
58 /* Character position of end of buffer. */
59 #define Z (current_buffer->text.z)
60
61 /* Modification count. */
62 #define MODIFF (current_buffer->text.modiff)
63
64 /* Address of beginning of buffer. */
65 #define BEG_ADDR (current_buffer->text.beg)
66
67 /* Address of beginning of accessible range of buffer. */
68 #define BEGV_ADDR (&FETCH_CHAR (current_buffer->text.begv))
69
70 /* Address of point in buffer. */
71 #define PT_ADDR (&FETCH_CHAR (current_buffer->text.pt))
72
73 /* Address of beginning of gap in buffer. */
74 #define GPT_ADDR (current_buffer->text.beg + current_buffer->text.gpt - 1)
75
76 /* Address of end of gap in buffer. */
77 #define GAP_END_ADDR (current_buffer->text.beg + current_buffer->text.gpt + current_buffer->text.gap_size - 1)
78
79 /* Address of end of accessible range of buffer. */
80 #define ZV_ADDR (&FETCH_CHAR (current_buffer->text.zv))
81
82 /* Size of gap. */
83 #define GAP_SIZE (current_buffer->text.gap_size)
84
85 /* Now similar macros for a specified buffer.
86 Note that many of these evaluate the buffer argument more than once. */
87
88 /* Character position of beginning of buffer. */
89 #define BUF_BEG(buf) (1)
90
91 /* Character position of beginning of accessible range of buffer. */
92 #define BUF_BEGV(buf) ((buf)->text.begv)
93
94 /* Character position of point in buffer. */
95 #define BUF_PT(buf) ((buf)->text.pt)
96
97 /* Character position of gap in buffer. */
98 #define BUF_GPT(buf) ((buf)->text.gpt)
99
100 /* Character position of end of accessible range of buffer. */
101 #define BUF_ZV(buf) ((buf)->text.zv)
102
103 /* Character position of end of buffer. */
104 #define BUF_Z(buf) ((buf)->text.z)
105
106 /* Modification count. */
107 #define BUF_MODIFF(buf) ((buf)->text.modiff)
108
109 /* Address of beginning of buffer. */
110 #define BUF_BEG_ADDR(buf) ((buf)->text.beg)
111
112 /* Macro for setting the value of BUF_ZV (BUF) to VALUE,
113 by varying the end of the accessible region. */
114 #define SET_BUF_ZV(buf, value) ((buf)->text.zv = (value))
115 #define SET_BUF_PT(buf, value) ((buf)->text.pt = (value))
116
117 /* Size of gap. */
118 #define BUF_GAP_SIZE(buf) ((buf)->text.gap_size)
119
120 /* Return the address of character at position POS in buffer BUF.
121 Note that both arguments can be computed more than once. */
122 #define BUF_CHAR_ADDRESS(buf, pos) \
123 ((buf)->text.beg + (pos) - 1 \
124 + ((pos) >= (buf)->text.gpt ? (buf)->text.gap_size : 0))
125
126 /* Convert the address of a char in the buffer into a character position. */
127 #define PTR_CHAR_POS(ptr) \
128 ((ptr) - (current_buffer)->text.beg \
129 - (ptr - (current_buffer)->text.beg < (unsigned) GPT ? 0 : GAP_SIZE) \
130 + 1)
131 \f
132 struct buffer_text
133 {
134 unsigned char *beg; /* Actual address of buffer contents. */
135 int begv; /* Index of beginning of accessible range. */
136 int pt; /* Position of point in buffer. */
137 int gpt; /* Index of gap in buffer. */
138 int zv; /* Index of end of accessible range. */
139 int z; /* Index of end of buffer. */
140 int gap_size; /* Size of buffer's gap */
141 int modiff; /* This counts buffer-modification events
142 for this buffer. It is incremented for
143 each such event, and never otherwise
144 changed. */
145 };
146
147 struct buffer
148 {
149 /* Everything before the `name' slot must be of a non-Lisp_Object type,
150 and every slot after `name' must be a Lisp_Object.
151
152 Check out mark_buffer (alloc.c) to see why.
153 */
154
155 /* This structure holds the coordinates of the buffer contents. */
156 struct buffer_text text;
157 /* Next buffer, in chain of all buffers including killed buffers.
158 This chain is used only for garbage collection, in order to
159 collect killed buffers properly. */
160 struct buffer *next;
161 /* Flags saying which DEFVAR_PER_BUFFER variables
162 are local to this buffer. */
163 int local_var_flags;
164 /* Value of text.modiff as of when visited file was read or written. */
165 int save_modified;
166 /* Set to the modtime of the visited file when read or written.
167 -1 means visited file was nonexistent.
168 0 means visited file modtime unknown; in no case complain
169 about any mismatch on next save attempt. */
170 int modtime;
171 /* the value of text.modiff at the last auto-save. */
172 int auto_save_modified;
173 /* Position in buffer at which display started
174 the last time this buffer was displayed */
175 int last_window_start;
176
177 /* Properties of this buffer's text -- conditionally compiled. */
178 DECLARE_INTERVALS
179
180 /* This is a special exception -- as this slot should not be
181 marked by gc_sweep, and as it is not lisp-accessible as
182 a local variable -- so we regard it as not really being of type
183 Lisp_Object */
184 /* the markers that refer to this buffer.
185 This is actually a single marker ---
186 successive elements in its marker `chain'
187 are the other markers referring to this
188 buffer */
189 Lisp_Object markers;
190
191
192 /* Everything from here down must be a Lisp_Object */
193
194
195 /* the name of this buffer */
196 Lisp_Object name;
197 /* Nuked: buffer number, assigned when buffer made Lisp_Object number;*/
198 /* the name of the file associated with this buffer */
199 Lisp_Object filename;
200 /* Dir for expanding relative pathnames */
201 Lisp_Object directory;
202 /* true iff this buffer has been been backed
203 up (if you write to its associated file
204 and it hasn't been backed up, then a
205 backup will be made) */
206 /* This isn't really used by the C code, so could be deleted. */
207 Lisp_Object backed_up;
208 /* Length of file when last read or saved. */
209 Lisp_Object save_length;
210 /* file name used for auto-saving this buffer */
211 Lisp_Object auto_save_file_name;
212 /* Non-nil if buffer read-only */
213 Lisp_Object read_only;
214 /* "The mark"; no longer allowed to be nil */
215 Lisp_Object mark;
216
217 /* Alist of elements (SYMBOL . VALUE-IN-THIS-BUFFER)
218 for all per-buffer variables of this buffer. */
219 Lisp_Object local_var_alist;
220
221
222 /* Symbol naming major mode (eg lisp-mode) */
223 Lisp_Object major_mode;
224 /* Pretty name of major mode (eg "Lisp") */
225 Lisp_Object mode_name;
226 /* Format string for mode line */
227 Lisp_Object mode_line_format;
228
229 /* Keys that are bound local to this buffer */
230 Lisp_Object keymap;
231 /* This buffer's local abbrev table */
232 Lisp_Object abbrev_table;
233 /* This buffer's syntax table. */
234 Lisp_Object syntax_table;
235
236 /* Values of several buffer-local variables */
237 /* tab-width is buffer-local so that redisplay can find it
238 in buffers that are not current */
239 Lisp_Object case_fold_search;
240 Lisp_Object tab_width;
241 Lisp_Object fill_column;
242 Lisp_Object left_margin;
243 /* Function to call when insert space past fill column */
244 Lisp_Object auto_fill_function;
245
246 /* String of length 256 mapping each char to its lower-case version. */
247 Lisp_Object downcase_table;
248 /* String of length 256 mapping each char to its upper-case version. */
249 Lisp_Object upcase_table;
250
251 /* Non-nil means do not display continuation lines */
252 Lisp_Object truncate_lines;
253 /* Non-nil means display ctl chars with uparrow */
254 Lisp_Object ctl_arrow;
255 /* Non-nil means do selective display;
256 See doc string in syms_of_buffer (buffer.c) for details. */
257 Lisp_Object selective_display;
258 #ifndef old
259 /* Non-nil means show ... at end of line followed by invisible lines. */
260 Lisp_Object selective_display_ellipses;
261 #endif
262 /* Alist of (FUNCTION . STRING) for each minor mode enabled in buffer. */
263 Lisp_Object minor_modes;
264 /* t if "self-insertion" should overwrite */
265 Lisp_Object overwrite_mode;
266 /* non-nil means abbrev mode is on. Expand abbrevs automatically. */
267 Lisp_Object abbrev_mode;
268 /* Display table to use for text in this buffer. */
269 Lisp_Object display_table;
270 /* Translate table for case-folding search. */
271 Lisp_Object case_canon_table;
272 /* Inverse translate (equivalence class) table for case-folding search. */
273 Lisp_Object case_eqv_table;
274 /* Changes in the buffer are recorded here for undo.
275 t means don't record anything. */
276 Lisp_Object undo_list;
277
278 /* List of fields in this buffer. */
279 Lisp_Object fieldlist;
280 };
281
282 extern struct buffer *current_buffer;
283
284 /* This structure holds the default values of the buffer-local variables
285 defined with DefBufferLispVar, that have special slots in each buffer.
286 The default value occupies the same slot in this structure
287 as an individual buffer's value occupies in that buffer.
288 Setting the default value also goes through the alist of buffers
289 and stores into each buffer that does not say it has a local value. */
290
291 extern struct buffer buffer_defaults;
292
293 /* This structure marks which slots in a buffer have corresponding
294 default values in buffer_defaults.
295 Each such slot has a nonzero value in this structure.
296 The value has only one nonzero bit.
297
298 When a buffer has its own local value for a slot,
299 the bit for that slot (found in the same slot in this structure)
300 is turned on in the buffer's local_var_flags slot.
301
302 If a slot in this structure is zero, then even though there may
303 be a DefBufferLispVar for the slot, there is no default valuefeor it;
304 and the corresponding slot in buffer_defaults is not used. */
305
306 extern struct buffer buffer_local_flags;
307
308 /* For each buffer slot, this points to the Lisp symbol name
309 for that slot in the current buffer. It is 0 for slots
310 that don't have such names. */
311
312 extern struct buffer buffer_local_symbols;
313
314 /* This structure holds the required types for the values in the
315 buffer-local slots. If a slot contains Qnil, then the
316 corresponding buffer slot may contain a value of any type. If a
317 slot contains an integer, then prospective values' tags must be
318 equal to that integer. When a tag does not match, the function
319 buffer_slot_type_mismatch will signal an error. The value Qnil may
320 always be safely stored in any slot. */
321 extern struct buffer buffer_local_types;
322
323 /* Point in the current buffer. */
324
325 #define point (current_buffer->text.pt + 0)
326
327 /* Return character at position n. No range checking */
328 #define FETCH_CHAR(n) *(((n)>= GPT ? GAP_SIZE : 0) + (n) + BEG_ADDR - 1)
329
330 /* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
331 the max (resp. min) p such that
332
333 &FETCH_CHAR (p) - &FETCH_CHAR (n) == p - n */
334
335 #define BUFFER_CEILING_OF(n) (((n) < GPT && GPT < ZV ? GPT : ZV) - 1)
336 #define BUFFER_FLOOR_OF(n) (BEGV <= GPT && GPT <= (n) ? GPT : BEGV)
337
338 extern void reset_buffer ();
339
340 /* Functions to call before and after each text change. */
341 extern Lisp_Object Vbefore_change_function;
342 extern Lisp_Object Vafter_change_function;
343 extern Lisp_Object Vfirst_change_hook;
344 extern Lisp_Object Qfirst_change_hook;
345
346 /* Fields.
347
348 A field is like a marker but it defines a region rather than a
349 point. Like a marker, a field is asocated with a buffer.
350 The field mechanism uses the marker mechanism in the
351 sense that its start and end points are maintained as markers
352 updated in the usual way as the buffer changes.
353
354 A field can be protected or unprotected. If it is protected,
355 no modifications can be made that affect the field in its buffer,
356 when protected field checking is enabled.
357
358 Each field also contains an alist, in which you can store
359 whatever you like. */
360
361 /* Slots in a field: */
362
363 #define FIELD_BUFFER(f) (XVECTOR(f)->contents[1])
364 #define FIELD_START_MARKER(f) (XVECTOR(f)->contents[2])
365 #define FIELD_END_MARKER(f) (XVECTOR(f)->contents[3])
366 #define FIELD_PROTECTED_FLAG(f) (XVECTOR(f)->contents[4])
367 #define FIELD_ALIST(f) (XVECTOR(f)->contents[5])
368
369 /* Allocation of buffer data. */
370 #ifdef REL_ALLOC
371 #define BUFFER_ALLOC(data,size) ((unsigned char *) r_alloc (&data, (size)))
372 #define BUFFER_REALLOC(data,size) ((unsigned char *) r_re_alloc (&data, (size)))
373 #define BUFFER_FREE(data) (r_alloc_free (&data))
374 #define R_ALLOC_DECLARE(var,data) (r_alloc_declare (&var, (data)))
375 #else
376 #define BUFFER_ALLOC(data,size) (data = (unsigned char *) malloc ((size)))
377 #define BUFFER_REALLOC(data,size) ((unsigned char *) realloc ((data), (size)))
378 #define BUFFER_FREE(data) (free ((data)))
379 #define R_ALLOC_DECLARE(var,data)
380 #endif
381
382 /* VAX C is non-ANSI wrt extern declarations and requires the struct
383 re_pattern_buffer to completely defined for searchbuf's declaration. */
384 #ifdef VMS
385 #include "regex.h"
386 #endif /* VMS */
387
388 /* A search buffer, with a fastmap allocated and ready to go. */
389 extern struct re_pattern_buffer searchbuf;
390
391 extern Lisp_Object Fbuffer_name ();
392 extern Lisp_Object Fget_file_buffer ();