1 /* Header file for the buffer manipulation primitives.
2
3 Copyright (C) 1985-1986, 1993-1995, 1997-2021 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
20
21 #ifndef EMACS_BUFFER_H
22 #define EMACS_BUFFER_H
23
24 #include <sys/types.h>
25 #include <time.h>
26
27 #include "character.h"
28 #include "lisp.h"
29
30 INLINE_HEADER_BEGIN
31
32 /* Accessing the parameters of the current buffer. */
33
34 /* These constants and macros come in pairs, one for the char position
35 and one for the byte position. */
36
37 /* Position of beginning of buffer. */
38 enum { BEG = 1, BEG_BYTE = BEG };
39
40 /* Position of beginning of accessible range of buffer. */
41 #define BEGV (current_buffer->begv)
42 #define BEGV_BYTE (current_buffer->begv_byte)
43
44 /* Position of point in buffer. The "+ 0" makes this
45 not an l-value, so you can't assign to it. Use SET_PT instead. */
46 #define PT (current_buffer->pt + 0)
47 #define PT_BYTE (current_buffer->pt_byte + 0)
48
49 /* Position of gap in buffer. */
50 #define GPT (current_buffer->text->gpt)
51 #define GPT_BYTE (current_buffer->text->gpt_byte)
52
53 /* Position of end of accessible range of buffer. */
54 #define ZV (current_buffer->zv)
55 #define ZV_BYTE (current_buffer->zv_byte)
56
57 /* Position of end of buffer. */
58 #define Z (current_buffer->text->z)
59 #define Z_BYTE (current_buffer->text->z_byte)
60
61 /* Macros for the addresses of places in the buffer. */
62
63 /* WARNING: Use the 'char *' pointers to buffer text with care in code
64 that could GC: GC can relocate buffer text, invalidating such
65 pointers. It is best to use character or byte position instead,
66 delaying the access through BYTE_POS_ADDR etc. pointers to the
67 latest possible moment. If you must use the 'char *' pointers
68 (e.g., for speed), be sure to adjust them after any call that could
69 potentially GC. */
70
71 /* Address of beginning of buffer. */
72 #define BEG_ADDR (current_buffer->text->beg)
73
74 /* Address of beginning of accessible range of buffer. */
75 #define BEGV_ADDR (BYTE_POS_ADDR (current_buffer->begv_byte))
76
77 /* Address of point in buffer. */
78 #define PT_ADDR (BYTE_POS_ADDR (current_buffer->pt_byte))
79
80 /* Address of beginning of gap in buffer. */
81 #define GPT_ADDR (current_buffer->text->beg + current_buffer->text->gpt_byte - BEG_BYTE)
82
83 /* Address of end of gap in buffer. */
84 #define GAP_END_ADDR (current_buffer->text->beg + current_buffer->text->gpt_byte + current_buffer->text->gap_size - BEG_BYTE)
85
86 /* Address of end of accessible range of buffer. */
87 #define ZV_ADDR (BYTE_POS_ADDR (current_buffer->zv_byte))
88
89 /* Address of end of buffer. */
90 #define Z_ADDR (current_buffer->text->beg + current_buffer->text->gap_size + current_buffer->text->z_byte - BEG_BYTE)
91
92 /* Size of gap. */
93 #define GAP_SIZE (current_buffer->text->gap_size)
94
95 /* Modification count. */
96 #define MODIFF (current_buffer->text->modiff)
97
98 /* Character modification count. */
99 #define CHARS_MODIFF (current_buffer->text->chars_modiff)
100
101 /* Overlay modification count. */
102 #define OVERLAY_MODIFF (current_buffer->text->overlay_modiff)
103
104 /* Modification count as of last visit or save. */
105 #define SAVE_MODIFF (current_buffer->text->save_modiff)
106
107
108 /* Position of gap in buffer. */
109 #define BUF_GPT(buf) ((buf)->text->gpt)
110 #define BUF_GPT_BYTE(buf) ((buf)->text->gpt_byte)
111
112 /* Position of end of buffer. */
113 #define BUF_Z(buf) ((buf)->text->z)
114 #define BUF_Z_BYTE(buf) ((buf)->text->z_byte)
115
116 /* Address of beginning of buffer. */
117 #define BUF_BEG_ADDR(buf) ((buf)->text->beg)
118
119 /* Size of gap. */
120 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size)
121
122 /* Modification count. */
123 #define BUF_MODIFF(buf) ((buf)->text->modiff)
124
125 /* Character modification count. */
126 #define BUF_CHARS_MODIFF(buf) ((buf)->text->chars_modiff)
127
128 /* Modification count as of last visit or save. */
129 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
130
131 /* Overlay modification count. */
132 #define BUF_OVERLAY_MODIFF(buf) ((buf)->text->overlay_modiff)
133
134 /* Modification count as of last auto-save. */
135 /* FIXME: should we move this into ->text->auto_save_modiff? */
136 #define BUF_AUTOSAVE_MODIFF(buf) ((buf)->auto_save_modified)
137
138 /* Compaction count. */
139 #define BUF_COMPACT(buf) ((buf)->text->compact)
140
141 /* Marker chain of buffer. */
142 #define BUF_MARKERS(buf) ((buf)->text->markers)
143
144 #define BUF_UNCHANGED_MODIFIED(buf) \
145 ((buf)->text->unchanged_modified)
146
147 #define BUF_OVERLAY_UNCHANGED_MODIFIED(buf) \
148 ((buf)->text->overlay_unchanged_modified)
149 #define BUF_BEG_UNCHANGED(buf) ((buf)->text->beg_unchanged)
150 #define BUF_END_UNCHANGED(buf) ((buf)->text->end_unchanged)
151
152 #define UNCHANGED_MODIFIED \
153 BUF_UNCHANGED_MODIFIED (current_buffer)
154 #define OVERLAY_UNCHANGED_MODIFIED \
155 BUF_OVERLAY_UNCHANGED_MODIFIED (current_buffer)
156 #define BEG_UNCHANGED BUF_BEG_UNCHANGED (current_buffer)
157 #define END_UNCHANGED BUF_END_UNCHANGED (current_buffer)
158
159 /* Functions to set PT in the current buffer, or another buffer. */
160
161 extern void set_point (ptrdiff_t);
162 extern void temp_set_point (struct buffer *, ptrdiff_t);
163 extern void set_point_both (ptrdiff_t, ptrdiff_t);
164 extern void temp_set_point_both (struct buffer *,
165 ptrdiff_t, ptrdiff_t);
166 extern void set_point_from_marker (Lisp_Object);
167 extern void enlarge_buffer_text (struct buffer *, ptrdiff_t);
168
169 INLINE void
SET_PT(ptrdiff_t position)170 SET_PT (ptrdiff_t position)
171 {
172 set_point (position);
173 }
174 INLINE void
TEMP_SET_PT(ptrdiff_t position)175 TEMP_SET_PT (ptrdiff_t position)
176 {
177 temp_set_point (current_buffer, position);
178 }
179 INLINE void
SET_PT_BOTH(ptrdiff_t position,ptrdiff_t byte)180 SET_PT_BOTH (ptrdiff_t position, ptrdiff_t byte)
181 {
182 set_point_both (position, byte);
183 }
184 INLINE void
TEMP_SET_PT_BOTH(ptrdiff_t position,ptrdiff_t byte)185 TEMP_SET_PT_BOTH (ptrdiff_t position, ptrdiff_t byte)
186 {
187 temp_set_point_both (current_buffer, position, byte);
188 }
189 INLINE void
BUF_TEMP_SET_PT(struct buffer * buffer,ptrdiff_t position)190 BUF_TEMP_SET_PT (struct buffer *buffer, ptrdiff_t position)
191 {
192 temp_set_point (buffer, position);
193 }
194
195 /* Maximum number of bytes in a buffer.
196 A buffer cannot contain more bytes than a 1-origin fixnum can represent,
197 nor can it be so large that C pointer arithmetic stops working.
198 The ptrdiff_t cast ensures that this is signed, not unsigned. */
199 #define BUF_BYTES_MAX \
200 (ptrdiff_t) min (MOST_POSITIVE_FIXNUM - 1, min (SIZE_MAX, PTRDIFF_MAX))
201
202 /* Maximum gap size after compact_buffer, in bytes. Also
203 used in make_gap_larger to get some extra reserved space. */
204
205 enum { GAP_BYTES_DFL = 2000 };
206
207 /* Minimum gap size after compact_buffer, in bytes. Also
208 used in make_gap_smaller to avoid too small gap size. */
209
210 enum { GAP_BYTES_MIN = 20 };
211
212 /* For those very rare cases where you may have a "random" pointer into
213 the middle of a multibyte char, this moves to the next boundary. */
214 extern ptrdiff_t advance_to_char_boundary (ptrdiff_t byte_pos);
215
216 /* Return the byte at byte position N.
217 Do not check that the position is in range. */
218
219 #define FETCH_BYTE(n) *(BYTE_POS_ADDR ((n)))
220
221 /* Define the actual buffer data structures. */
222
223 /* This data structure describes the actual text contents of a buffer.
224 It is shared between indirect buffers and their base buffer. */
225
226 struct buffer_text
227 {
228 /* Actual address of buffer contents. If REL_ALLOC is defined,
229 this address might change when blocks are relocated which can
230 e.g. happen when malloc is called. So, don't pass a pointer
231 into a buffer's text to functions that malloc. */
232 unsigned char *beg;
233
234 ptrdiff_t gpt; /* Char pos of gap in buffer. */
235 ptrdiff_t z; /* Char pos of end of buffer. */
236 ptrdiff_t gpt_byte; /* Byte pos of gap in buffer. */
237 ptrdiff_t z_byte; /* Byte pos of end of buffer. */
238 ptrdiff_t gap_size; /* Size of buffer's gap. */
239 modiff_count modiff; /* This counts buffer-modification events
240 for this buffer. It is incremented for
241 each such event, and never otherwise
242 changed. */
243 modiff_count chars_modiff; /* This is modified with character change
244 events for this buffer. It is set to
245 modiff for each such event, and never
246 otherwise changed. */
247 modiff_count save_modiff; /* Previous value of modiff, as of last
248 time buffer visited or saved a file. */
249
250 modiff_count overlay_modiff; /* Counts modifications to overlays. */
251
252 modiff_count compact; /* Set to modiff each time when compact_buffer
253 is called for this buffer. */
254
255 /* Minimum value of GPT - BEG since last redisplay that finished. */
256 ptrdiff_t beg_unchanged;
257
258 /* Minimum value of Z - GPT since last redisplay that finished. */
259 ptrdiff_t end_unchanged;
260
261 /* MODIFF as of last redisplay that finished; if it matches MODIFF,
262 beg_unchanged and end_unchanged contain no useful information. */
263 modiff_count unchanged_modified;
264
265 /* BUF_OVERLAY_MODIFF of current buffer, as of last redisplay that
266 finished; if it matches BUF_OVERLAY_MODIFF, beg_unchanged and
267 end_unchanged contain no useful information. */
268 modiff_count overlay_unchanged_modified;
269
270 /* Properties of this buffer's text. */
271 INTERVAL intervals;
272
273 /* The markers that refer to this buffer.
274 This is actually a single marker ---
275 successive elements in its marker `chain'
276 are the other markers referring to this buffer.
277 This is a singly linked unordered list, which means that it's
278 very cheap to add a marker to the list and it's also very cheap
279 to move a marker within a buffer. */
280 struct Lisp_Marker *markers;
281
282 /* Usually false. Temporarily true in decode_coding_gap to
283 prevent Fgarbage_collect from shrinking the gap and losing
284 not-yet-decoded bytes. */
285 bool_bf inhibit_shrinking : 1;
286
287 /* True if it needs to be redisplayed. */
288 bool_bf redisplay : 1;
289 };
290
291 /* Most code should use this macro to access Lisp fields in struct buffer. */
292
293 #define BVAR(buf, field) ((buf)->field ## _)
294
295 /* Max number of builtin per-buffer variables. */
296 enum { MAX_PER_BUFFER_VARS = 50 };
297
298 /* Special values for struct buffer.modtime. */
299 enum { NONEXISTENT_MODTIME_NSECS = -1 };
300 enum { UNKNOWN_MODTIME_NSECS = -2 };
301
302 /* This is the structure that the buffer Lisp object points to. */
303
304 struct buffer
305 {
306 union vectorlike_header header;
307
308 /* The name of this buffer. */
309 Lisp_Object name_;
310
311 /* The name of the file visited in this buffer, or nil. */
312 Lisp_Object filename_;
313
314 /* Directory for expanding relative file names. */
315 Lisp_Object directory_;
316
317 /* True if this buffer has been backed up (if you write to the visited
318 file and it hasn't been backed up, then a backup will be made). */
319 Lisp_Object backed_up_;
320
321 /* Length of file when last read or saved.
322 -1 means auto saving turned off because buffer shrank a lot.
323 -2 means don't turn off auto saving if buffer shrinks.
324 (That value is used with buffer-swap-text.)
325 This is not in the struct buffer_text
326 because it's not used in indirect buffers at all. */
327 Lisp_Object save_length_;
328
329 /* File name used for auto-saving this buffer.
330 This is not in the struct buffer_text
331 because it's not used in indirect buffers at all. */
332 Lisp_Object auto_save_file_name_;
333
334 /* Non-nil if buffer read-only. */
335 Lisp_Object read_only_;
336
337 /* "The mark". This is a marker which may
338 point into this buffer or may point nowhere. */
339 Lisp_Object mark_;
340
341 /* Alist of elements (SYMBOL . VALUE-IN-THIS-BUFFER) for all
342 per-buffer variables of this buffer. For locally unbound
343 symbols, just the symbol appears as the element. */
344 Lisp_Object local_var_alist_;
345
346 /* Symbol naming major mode (e.g., lisp-mode). */
347 Lisp_Object major_mode_;
348
349 /* Symbol listing all currently enabled minor modes. */
350 Lisp_Object local_minor_modes_;
351
352 /* Pretty name of major mode (e.g., "Lisp"). */
353 Lisp_Object mode_name_;
354
355 /* Mode line element that controls format of mode line. */
356 Lisp_Object mode_line_format_;
357
358 /* Analogous to mode_line_format for the line displayed at the top
359 of windows. Nil means don't display that line. */
360 Lisp_Object header_line_format_;
361
362 /* Analogous to mode_line_format for the line displayed at the top
363 of windows. Nil means don't display that line. */
364 Lisp_Object tab_line_format_;
365
366 /* Keys that are bound local to this buffer. */
367 Lisp_Object keymap_;
368
369 /* This buffer's local abbrev table. */
370 Lisp_Object abbrev_table_;
371
372 /* This buffer's syntax table. */
373 Lisp_Object syntax_table_;
374
375 /* This buffer's category table. */
376 Lisp_Object category_table_;
377
378 /* Values of several buffer-local variables. */
379 /* tab-width is buffer-local so that redisplay can find it
380 in buffers that are not current. */
381 Lisp_Object case_fold_search_;
382 Lisp_Object tab_width_;
383 Lisp_Object fill_column_;
384 Lisp_Object left_margin_;
385
386 /* Function to call when insert space past fill column. */
387 Lisp_Object auto_fill_function_;
388
389 /* Case table for case-conversion in this buffer.
390 This char-table maps each char into its lower-case version. */
391 Lisp_Object downcase_table_;
392
393 /* Char-table mapping each char to its upper-case version. */
394 Lisp_Object upcase_table_;
395
396 /* Char-table for conversion for case-folding search. */
397 Lisp_Object case_canon_table_;
398
399 /* Char-table of equivalences for case-folding search. */
400 Lisp_Object case_eqv_table_;
401
402 /* Non-nil means do not display continuation lines. */
403 Lisp_Object truncate_lines_;
404
405 /* Non-nil means to use word wrapping when displaying continuation lines. */
406 Lisp_Object word_wrap_;
407
408 /* Non-nil means display ctl chars with uparrow. */
409 Lisp_Object ctl_arrow_;
410
411 /* Non-nil means reorder bidirectional text for display in the
412 visual order. */
413 Lisp_Object bidi_display_reordering_;
414
415 /* If non-nil, specifies which direction of text to force in all the
416 paragraphs of the buffer. Nil means determine paragraph
417 direction dynamically for each paragraph. */
418 Lisp_Object bidi_paragraph_direction_;
419
420 /* If non-nil, a regular expression for bidi paragraph separator. */
421 Lisp_Object bidi_paragraph_separate_re_;
422
423 /* If non-nil, a regular expression for bidi paragraph start. */
424 Lisp_Object bidi_paragraph_start_re_;
425
426 /* Non-nil means do selective display;
427 see doc string in syms_of_buffer (buffer.c) for details. */
428 Lisp_Object selective_display_;
429
430 /* Non-nil means show ... at end of line followed by invisible lines. */
431 Lisp_Object selective_display_ellipses_;
432
433 /* t if "self-insertion" should overwrite; `binary' if it should also
434 overwrite newlines and tabs - for editing executables and the like. */
435 Lisp_Object overwrite_mode_;
436
437 /* Non-nil means abbrev mode is on. Expand abbrevs automatically. */
438 Lisp_Object abbrev_mode_;
439
440 /* Display table to use for text in this buffer. */
441 Lisp_Object display_table_;
442
443 /* t means the mark and region are currently active. */
444 Lisp_Object mark_active_;
445
446 /* Non-nil means the buffer contents are regarded as multi-byte
447 form of characters, not a binary code. */
448 Lisp_Object enable_multibyte_characters_;
449
450 /* Coding system to be used for encoding the buffer contents on
451 saving. */
452 Lisp_Object buffer_file_coding_system_;
453
454 /* List of symbols naming the file format used for visited file. */
455 Lisp_Object file_format_;
456
457 /* List of symbols naming the file format used for auto-save file. */
458 Lisp_Object auto_save_file_format_;
459
460 /* True if the newline position cache, width run cache and BIDI paragraph
461 cache are enabled. See search.c, indent.c and bidi.c for details. */
462 Lisp_Object cache_long_scans_;
463
464 /* If the width run cache is enabled, this table contains the
465 character widths width_run_cache (see above) assumes. When we
466 do a thorough redisplay, we compare this against the buffer's
467 current display table to see whether the display table has
468 affected the widths of any characters. If it has, we
469 invalidate the width run cache, and re-initialize width_table. */
470 Lisp_Object width_table_;
471
472 /* In an indirect buffer, or a buffer that is the base of an
473 indirect buffer, this holds a marker that records
474 PT for this buffer when the buffer is not current. */
475 Lisp_Object pt_marker_;
476
477 /* In an indirect buffer, or a buffer that is the base of an
478 indirect buffer, this holds a marker that records
479 BEGV for this buffer when the buffer is not current. */
480 Lisp_Object begv_marker_;
481
482 /* In an indirect buffer, or a buffer that is the base of an
483 indirect buffer, this holds a marker that records
484 ZV for this buffer when the buffer is not current. */
485 Lisp_Object zv_marker_;
486
487 /* This holds the point value before the last scroll operation.
488 Explicitly setting point sets this to nil. */
489 Lisp_Object point_before_scroll_;
490
491 /* Truename of the visited file, or nil. */
492 Lisp_Object file_truename_;
493
494 /* Invisibility spec of this buffer.
495 t => any non-nil `invisible' property means invisible.
496 A list => `invisible' property means invisible
497 if it is memq in that list. */
498 Lisp_Object invisibility_spec_;
499
500 /* This is the last window that was selected with this buffer in it,
501 or nil if that window no longer displays this buffer. */
502 Lisp_Object last_selected_window_;
503
504 /* Incremented each time the buffer is displayed in a window. */
505 Lisp_Object display_count_;
506
507 /* Widths of left and right marginal areas for windows displaying
508 this buffer. */
509 Lisp_Object left_margin_cols_;
510 Lisp_Object right_margin_cols_;
511
512 /* Widths of left and right fringe areas for windows displaying
513 this buffer. */
514 Lisp_Object left_fringe_width_;
515 Lisp_Object right_fringe_width_;
516
517 /* Non-nil means fringes are drawn outside display margins;
518 othersize draw them between margin areas and text. */
519 Lisp_Object fringes_outside_margins_;
520
521 /* Width, height and types of scroll bar areas for windows displaying
522 this buffer. */
523 Lisp_Object scroll_bar_width_;
524 Lisp_Object scroll_bar_height_;
525 Lisp_Object vertical_scroll_bar_type_;
526 Lisp_Object horizontal_scroll_bar_type_;
527
528 /* Non-nil means indicate lines not displaying text (in a style
529 like vi). */
530 Lisp_Object indicate_empty_lines_;
531
532 /* Non-nil means indicate buffer boundaries and scrolling. */
533 Lisp_Object indicate_buffer_boundaries_;
534
535 /* Logical to physical fringe bitmap mappings. */
536 Lisp_Object fringe_indicator_alist_;
537
538 /* Logical to physical cursor bitmap mappings. */
539 Lisp_Object fringe_cursor_alist_;
540
541 /* Time stamp updated each time this buffer is displayed in a window. */
542 Lisp_Object display_time_;
543
544 /* If scrolling the display because point is below the bottom of a
545 window showing this buffer, try to choose a window start so
546 that point ends up this number of lines from the top of the
547 window. Nil means that scrolling method isn't used. */
548 Lisp_Object scroll_up_aggressively_;
549
550 /* If scrolling the display because point is above the top of a
551 window showing this buffer, try to choose a window start so
552 that point ends up this number of lines from the bottom of the
553 window. Nil means that scrolling method isn't used. */
554 Lisp_Object scroll_down_aggressively_;
555
556 /* Desired cursor type in this buffer. See the doc string of
557 per-buffer variable `cursor-type'. */
558 Lisp_Object cursor_type_;
559
560 /* An integer > 0 means put that number of pixels below text lines
561 in the display of this buffer. */
562 Lisp_Object extra_line_spacing_;
563
564 /* Cursor type to display in non-selected windows.
565 t means to use hollow box cursor.
566 See `cursor-type' for other values. */
567 Lisp_Object cursor_in_non_selected_windows_;
568
569 /* No more Lisp_Object beyond cursor_in_non_selected_windows_.
570 Except undo_list, which is handled specially in Fgarbage_collect. */
571
572 /* This structure holds the coordinates of the buffer contents
573 in ordinary buffers. In indirect buffers, this is not used. */
574 struct buffer_text own_text;
575
576 /* This points to the `struct buffer_text' that used for this buffer.
577 In an ordinary buffer, this is the own_text field above.
578 In an indirect buffer, this is the own_text field of another buffer. */
579 struct buffer_text *text;
580
581 /* Char position of point in buffer. */
582 ptrdiff_t pt;
583
584 /* Byte position of point in buffer. */
585 ptrdiff_t pt_byte;
586
587 /* Char position of beginning of accessible range. */
588 ptrdiff_t begv;
589
590 /* Byte position of beginning of accessible range. */
591 ptrdiff_t begv_byte;
592
593 /* Char position of end of accessible range. */
594 ptrdiff_t zv;
595
596 /* Byte position of end of accessible range. */
597 ptrdiff_t zv_byte;
598
599 /* In an indirect buffer, this points to the base buffer.
600 In an ordinary buffer, it is 0. */
601 struct buffer *base_buffer;
602
603 /* In an indirect buffer, this is -1. In an ordinary buffer,
604 it's the number of indirect buffers that share our text;
605 zero means that we're the only owner of this text. */
606 int indirections;
607
608 /* Number of windows showing this buffer. Always -1 for
609 an indirect buffer since it counts as its base buffer. */
610 int window_count;
611
612 /* A non-zero value in slot IDX means that per-buffer variable
613 with index IDX has a local value in this buffer. The index IDX
614 for a buffer-local variable is stored in that variable's slot
615 in buffer_local_flags as a Lisp integer. If the index is -1,
616 this means the variable is always local in all buffers. */
617 char local_flags[MAX_PER_BUFFER_VARS];
618
619 /* Set to the modtime of the visited file when read or written.
620 modtime.tv_nsec == NONEXISTENT_MODTIME_NSECS means
621 visited file was nonexistent. modtime.tv_nsec ==
622 UNKNOWN_MODTIME_NSECS means visited file modtime unknown;
623 in no case complain about any mismatch on next save attempt. */
624 struct timespec modtime;
625
626 /* Size of the file when modtime was set. This is used to detect the
627 case where the file grew while we were reading it, so the modtime
628 is still the same (since it's rounded up to seconds) but we're actually
629 not up-to-date. -1 means the size is unknown. Only meaningful if
630 modtime is actually set. */
631 off_t modtime_size;
632
633 /* The value of text->modiff at the last auto-save. */
634 modiff_count auto_save_modified;
635
636 /* The value of text->modiff at the last display error.
637 Redisplay of this buffer is inhibited until it changes again. */
638 modiff_count display_error_modiff;
639
640 /* The time at which we detected a failure to auto-save,
641 Or 0 if we didn't have a failure. */
642 time_t auto_save_failure_time;
643
644 /* Position in buffer at which display started
645 the last time this buffer was displayed. */
646 ptrdiff_t last_window_start;
647
648 /* If the long line scan cache is enabled (i.e. the buffer-local
649 variable cache-long-line-scans is non-nil), newline_cache
650 points to the newline cache, and width_run_cache points to the
651 width run cache.
652
653 The newline cache records which stretches of the buffer are
654 known *not* to contain newlines, so that they can be skipped
655 quickly when we search for newlines.
656
657 The width run cache records which stretches of the buffer are
658 known to contain characters whose widths are all the same. If
659 the width run cache maps a character to a value > 0, that value is
660 the character's width; if it maps a character to zero, we don't
661 know what its width is. This allows compute_motion to process
662 such regions very quickly, using algebra instead of inspecting
663 each character. See also width_table, below.
664
665 The latter cache is used to speedup bidi_find_paragraph_start. */
666 struct region_cache *newline_cache;
667 struct region_cache *width_run_cache;
668 struct region_cache *bidi_paragraph_cache;
669
670 /* Non-zero means disable redisplay optimizations when rebuilding the glyph
671 matrices (but not when redrawing). */
672 bool_bf prevent_redisplay_optimizations_p : 1;
673
674 /* Non-zero whenever the narrowing is changed in this buffer. */
675 bool_bf clip_changed : 1;
676
677 /* Non-zero for internal or temporary buffers that don't need to
678 run hooks kill-buffer-hook, kill-buffer-query-functions, and
679 buffer-list-update-hook. This is used in coding.c to avoid
680 slowing down en/decoding when a lot of these hooks are
681 defined, as well as by with-temp-buffer, for example. */
682 bool_bf inhibit_buffer_hooks : 1;
683
684 /* List of overlays that end at or before the current center,
685 in order of end-position. */
686 struct Lisp_Overlay *overlays_before;
687
688 /* List of overlays that end after the current center,
689 in order of start-position. */
690 struct Lisp_Overlay *overlays_after;
691
692 /* Position where the overlay lists are centered. */
693 ptrdiff_t overlay_center;
694
695 /* Changes in the buffer are recorded here for undo, and t means
696 don't record anything. This information belongs to the base
697 buffer of an indirect buffer. But we can't store it in the
698 struct buffer_text because local variables have to be right in
699 the struct buffer. So we copy it around in set_buffer_internal. */
700 Lisp_Object undo_list_;
701 };
702
703 INLINE bool
BUFFERP(Lisp_Object a)704 BUFFERP (Lisp_Object a)
705 {
706 return PSEUDOVECTORP (a, PVEC_BUFFER);
707 }
708
709 INLINE void
CHECK_BUFFER(Lisp_Object x)710 CHECK_BUFFER (Lisp_Object x)
711 {
712 CHECK_TYPE (BUFFERP (x), Qbufferp, x);
713 }
714
715 INLINE struct buffer *
XBUFFER(Lisp_Object a)716 XBUFFER (Lisp_Object a)
717 {
718 eassert (BUFFERP (a));
719 return XUNTAG (a, Lisp_Vectorlike, struct buffer);
720 }
721
722 /* Most code should use these functions to set Lisp fields in struct
723 buffer. (Some setters that are private to a single .c file are
724 defined as static in those files.) */
725 INLINE void
bset_bidi_paragraph_direction(struct buffer * b,Lisp_Object val)726 bset_bidi_paragraph_direction (struct buffer *b, Lisp_Object val)
727 {
728 b->bidi_paragraph_direction_ = val;
729 }
730 INLINE void
bset_cache_long_scans(struct buffer * b,Lisp_Object val)731 bset_cache_long_scans (struct buffer *b, Lisp_Object val)
732 {
733 b->cache_long_scans_ = val;
734 }
735 INLINE void
bset_case_canon_table(struct buffer * b,Lisp_Object val)736 bset_case_canon_table (struct buffer *b, Lisp_Object val)
737 {
738 b->case_canon_table_ = val;
739 }
740 INLINE void
bset_case_eqv_table(struct buffer * b,Lisp_Object val)741 bset_case_eqv_table (struct buffer *b, Lisp_Object val)
742 {
743 b->case_eqv_table_ = val;
744 }
745 INLINE void
bset_directory(struct buffer * b,Lisp_Object val)746 bset_directory (struct buffer *b, Lisp_Object val)
747 {
748 b->directory_ = val;
749 }
750 INLINE void
bset_display_count(struct buffer * b,Lisp_Object val)751 bset_display_count (struct buffer *b, Lisp_Object val)
752 {
753 b->display_count_ = val;
754 }
755 INLINE void
bset_left_margin_cols(struct buffer * b,Lisp_Object val)756 bset_left_margin_cols (struct buffer *b, Lisp_Object val)
757 {
758 b->left_margin_cols_ = val;
759 }
760 INLINE void
bset_right_margin_cols(struct buffer * b,Lisp_Object val)761 bset_right_margin_cols (struct buffer *b, Lisp_Object val)
762 {
763 b->right_margin_cols_ = val;
764 }
765 INLINE void
bset_display_time(struct buffer * b,Lisp_Object val)766 bset_display_time (struct buffer *b, Lisp_Object val)
767 {
768 b->display_time_ = val;
769 }
770 INLINE void
bset_downcase_table(struct buffer * b,Lisp_Object val)771 bset_downcase_table (struct buffer *b, Lisp_Object val)
772 {
773 b->downcase_table_ = val;
774 }
775 INLINE void
bset_enable_multibyte_characters(struct buffer * b,Lisp_Object val)776 bset_enable_multibyte_characters (struct buffer *b, Lisp_Object val)
777 {
778 b->enable_multibyte_characters_ = val;
779 }
780 INLINE void
bset_filename(struct buffer * b,Lisp_Object val)781 bset_filename (struct buffer *b, Lisp_Object val)
782 {
783 b->filename_ = val;
784 }
785 INLINE void
bset_keymap(struct buffer * b,Lisp_Object val)786 bset_keymap (struct buffer *b, Lisp_Object val)
787 {
788 b->keymap_ = val;
789 }
790 INLINE void
bset_last_selected_window(struct buffer * b,Lisp_Object val)791 bset_last_selected_window (struct buffer *b, Lisp_Object val)
792 {
793 b->last_selected_window_ = val;
794 }
795 INLINE void
bset_local_var_alist(struct buffer * b,Lisp_Object val)796 bset_local_var_alist (struct buffer *b, Lisp_Object val)
797 {
798 b->local_var_alist_ = val;
799 }
800 INLINE void
bset_mark_active(struct buffer * b,Lisp_Object val)801 bset_mark_active (struct buffer *b, Lisp_Object val)
802 {
803 b->mark_active_ = val;
804 }
805 INLINE void
bset_point_before_scroll(struct buffer * b,Lisp_Object val)806 bset_point_before_scroll (struct buffer *b, Lisp_Object val)
807 {
808 b->point_before_scroll_ = val;
809 }
810 INLINE void
bset_read_only(struct buffer * b,Lisp_Object val)811 bset_read_only (struct buffer *b, Lisp_Object val)
812 {
813 b->read_only_ = val;
814 }
815 INLINE void
bset_truncate_lines(struct buffer * b,Lisp_Object val)816 bset_truncate_lines (struct buffer *b, Lisp_Object val)
817 {
818 b->truncate_lines_ = val;
819 }
820 INLINE void
bset_undo_list(struct buffer * b,Lisp_Object val)821 bset_undo_list (struct buffer *b, Lisp_Object val)
822 {
823 b->undo_list_ = val;
824 }
825 INLINE void
bset_upcase_table(struct buffer * b,Lisp_Object val)826 bset_upcase_table (struct buffer *b, Lisp_Object val)
827 {
828 b->upcase_table_ = val;
829 }
830 INLINE void
bset_width_table(struct buffer * b,Lisp_Object val)831 bset_width_table (struct buffer *b, Lisp_Object val)
832 {
833 b->width_table_ = val;
834 }
835
836 /* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
837 the max (resp. min) p such that
838
839 BYTE_POS_ADDR (p) - BYTE_POS_ADDR (n) == p - n */
840
841 INLINE ptrdiff_t
BUFFER_CEILING_OF(ptrdiff_t bytepos)842 BUFFER_CEILING_OF (ptrdiff_t bytepos)
843 {
844 return (bytepos < GPT_BYTE && GPT < ZV ? GPT_BYTE : ZV_BYTE) - 1;
845 }
846
847 INLINE ptrdiff_t
BUFFER_FLOOR_OF(ptrdiff_t bytepos)848 BUFFER_FLOOR_OF (ptrdiff_t bytepos)
849 {
850 return BEGV <= GPT && GPT_BYTE <= bytepos ? GPT_BYTE : BEGV_BYTE;
851 }
852
853 /* The BUF_BEGV[_BYTE], BUF_ZV[_BYTE], and BUF_PT[_BYTE] functions cannot
854 be used for assignment; use SET_BUF_* functions below for that. */
855
856 /* Position of beginning of accessible range of buffer. */
857 INLINE ptrdiff_t
BUF_BEGV(struct buffer * buf)858 BUF_BEGV (struct buffer *buf)
859 {
860 return (buf == current_buffer ? BEGV
861 : NILP (BVAR (buf, begv_marker)) ? buf->begv
862 : marker_position (BVAR (buf, begv_marker)));
863 }
864
865 INLINE ptrdiff_t
BUF_BEGV_BYTE(struct buffer * buf)866 BUF_BEGV_BYTE (struct buffer *buf)
867 {
868 return (buf == current_buffer ? BEGV_BYTE
869 : NILP (BVAR (buf, begv_marker)) ? buf->begv_byte
870 : marker_byte_position (BVAR (buf, begv_marker)));
871 }
872
873 /* Position of point in buffer. */
874 INLINE ptrdiff_t
BUF_PT(struct buffer * buf)875 BUF_PT (struct buffer *buf)
876 {
877 return (buf == current_buffer ? PT
878 : NILP (BVAR (buf, pt_marker)) ? buf->pt
879 : marker_position (BVAR (buf, pt_marker)));
880 }
881
882 INLINE ptrdiff_t
BUF_PT_BYTE(struct buffer * buf)883 BUF_PT_BYTE (struct buffer *buf)
884 {
885 return (buf == current_buffer ? PT_BYTE
886 : NILP (BVAR (buf, pt_marker)) ? buf->pt_byte
887 : marker_byte_position (BVAR (buf, pt_marker)));
888 }
889
890 /* Position of end of accessible range of buffer. */
891 INLINE ptrdiff_t
BUF_ZV(struct buffer * buf)892 BUF_ZV (struct buffer *buf)
893 {
894 return (buf == current_buffer ? ZV
895 : NILP (BVAR (buf, zv_marker)) ? buf->zv
896 : marker_position (BVAR (buf, zv_marker)));
897 }
898
899 INLINE ptrdiff_t
BUF_ZV_BYTE(struct buffer * buf)900 BUF_ZV_BYTE (struct buffer *buf)
901 {
902 return (buf == current_buffer ? ZV_BYTE
903 : NILP (BVAR (buf, zv_marker)) ? buf->zv_byte
904 : marker_byte_position (BVAR (buf, zv_marker)));
905 }
906
907 /* Similar functions to operate on a specified buffer. */
908
909 /* Position of beginning of buffer. */
910 INLINE ptrdiff_t
BUF_BEG(struct buffer * buf)911 BUF_BEG (struct buffer *buf)
912 {
913 return BEG;
914 }
915
916 INLINE ptrdiff_t
BUF_BEG_BYTE(struct buffer * buf)917 BUF_BEG_BYTE (struct buffer *buf)
918 {
919 return BEG_BYTE;
920 }
921
922 /* Address of beginning of gap of buffer. */
923 INLINE unsigned char *
BUF_GPT_ADDR(struct buffer * buf)924 BUF_GPT_ADDR (struct buffer *buf)
925 {
926 return buf->text->beg + buf->text->gpt_byte - BEG_BYTE;
927 }
928
929 /* Address of end of buffer. */
930 INLINE unsigned char *
BUF_Z_ADDR(struct buffer * buf)931 BUF_Z_ADDR (struct buffer *buf)
932 {
933 return buf->text->beg + buf->text->gap_size + buf->text->z_byte - BEG_BYTE;
934 }
935
936 /* Address of end of gap in buffer. */
937 INLINE unsigned char *
BUF_GAP_END_ADDR(struct buffer * buf)938 BUF_GAP_END_ADDR (struct buffer *buf)
939 {
940 return buf->text->beg + buf->text->gpt_byte + buf->text->gap_size - BEG_BYTE;
941 }
942
943 /* Compute how many characters at the top and bottom of BUF are
944 unchanged when the range START..END is modified. This computation
945 must be done each time BUF is modified. */
946
947 INLINE void
BUF_COMPUTE_UNCHANGED(struct buffer * buf,ptrdiff_t start,ptrdiff_t end)948 BUF_COMPUTE_UNCHANGED (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
949 {
950 if (BUF_UNCHANGED_MODIFIED (buf) == BUF_MODIFF (buf)
951 && (BUF_OVERLAY_UNCHANGED_MODIFIED (buf)
952 == BUF_OVERLAY_MODIFF (buf)))
953 {
954 buf->text->beg_unchanged = start - BUF_BEG (buf);
955 buf->text->end_unchanged = BUF_Z (buf) - (end);
956 }
957 else
958 {
959 if (BUF_Z (buf) - end < BUF_END_UNCHANGED (buf))
960 buf->text->end_unchanged = BUF_Z (buf) - end;
961 if (start - BUF_BEG (buf) < BUF_BEG_UNCHANGED (buf))
962 buf->text->beg_unchanged = start - BUF_BEG (buf);
963 }
964 }
965
966 /* Functions for setting the BEGV, ZV or PT of a given buffer.
967
968 The ..._BOTH functions take both a charpos and a bytepos,
969 which must correspond to each other.
970
971 The functions without ..._BOTH take just a charpos,
972 and compute the bytepos from it. */
973
974 INLINE void
SET_BUF_BEGV(struct buffer * buf,ptrdiff_t charpos)975 SET_BUF_BEGV (struct buffer *buf, ptrdiff_t charpos)
976 {
977 buf->begv_byte = buf_charpos_to_bytepos (buf, charpos);
978 buf->begv = charpos;
979 }
980
981 INLINE void
SET_BUF_ZV(struct buffer * buf,ptrdiff_t charpos)982 SET_BUF_ZV (struct buffer *buf, ptrdiff_t charpos)
983 {
984 buf->zv_byte = buf_charpos_to_bytepos (buf, charpos);
985 buf->zv = charpos;
986 }
987
988 INLINE void
SET_BUF_BEGV_BOTH(struct buffer * buf,ptrdiff_t charpos,ptrdiff_t byte)989 SET_BUF_BEGV_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
990 {
991 buf->begv = charpos;
992 buf->begv_byte = byte;
993 }
994
995 INLINE void
SET_BUF_ZV_BOTH(struct buffer * buf,ptrdiff_t charpos,ptrdiff_t byte)996 SET_BUF_ZV_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
997 {
998 buf->zv = charpos;
999 buf->zv_byte = byte;
1000 }
1001
1002 INLINE void
SET_BUF_PT_BOTH(struct buffer * buf,ptrdiff_t charpos,ptrdiff_t byte)1003 SET_BUF_PT_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
1004 {
1005 buf->pt = charpos;
1006 buf->pt_byte = byte;
1007 }
1008
1009 /* Functions to access a character or byte in the current buffer,
1010 or convert between a byte position and an address.
1011 These functions do not check that the position is in range. */
1012
1013 /* See the important WARNING above about using the 'char *' pointers
1014 returned by these functions. */
1015
1016 /* Return the address of byte position N in current buffer. */
1017
1018 INLINE unsigned char *
BYTE_POS_ADDR(ptrdiff_t n)1019 BYTE_POS_ADDR (ptrdiff_t n)
1020 {
1021 return (n < GPT_BYTE ? 0 : GAP_SIZE) + n + BEG_ADDR - BEG_BYTE;
1022 }
1023
1024 /* Return the address of char position N. */
1025
1026 INLINE unsigned char *
CHAR_POS_ADDR(ptrdiff_t n)1027 CHAR_POS_ADDR (ptrdiff_t n)
1028 {
1029 return ((n < GPT ? 0 : GAP_SIZE)
1030 + buf_charpos_to_bytepos (current_buffer, n)
1031 + BEG_ADDR - BEG_BYTE);
1032 }
1033
1034 /* Convert a character position to a byte position. */
1035
1036 INLINE ptrdiff_t
CHAR_TO_BYTE(ptrdiff_t charpos)1037 CHAR_TO_BYTE (ptrdiff_t charpos)
1038 {
1039 return buf_charpos_to_bytepos (current_buffer, charpos);
1040 }
1041
1042 /* Convert a byte position to a character position. */
1043
1044 INLINE ptrdiff_t
BYTE_TO_CHAR(ptrdiff_t bytepos)1045 BYTE_TO_CHAR (ptrdiff_t bytepos)
1046 {
1047 return buf_bytepos_to_charpos (current_buffer, bytepos);
1048 }
1049
1050 /* Convert PTR, the address of a byte in the buffer, into a byte position. */
1051
1052 INLINE ptrdiff_t
PTR_BYTE_POS(unsigned char const * ptr)1053 PTR_BYTE_POS (unsigned char const *ptr)
1054 {
1055 ptrdiff_t byte = ptr - current_buffer->text->beg;
1056 return byte - (byte <= GPT_BYTE - BEG_BYTE ? 0 : GAP_SIZE) + BEG_BYTE;
1057 }
1058
1059 /* Number of Lisp_Objects at the beginning of struct buffer.
1060 If you add, remove, or reorder Lisp_Objects within buffer
1061 structure, make sure that this is still correct. */
1062
1063 enum { BUFFER_LISP_SIZE = PSEUDOVECSIZE (struct buffer,
1064 cursor_in_non_selected_windows_) };
1065
1066 /* Allocated size of the struct buffer part beyond leading
1067 Lisp_Objects, in word_size units. */
1068
1069 enum { BUFFER_REST_SIZE = VECSIZE (struct buffer) - BUFFER_LISP_SIZE };
1070
1071 /* Initialize the pseudovector header of buffer object. BUFFER_LISP_SIZE
1072 is required for GC, but BUFFER_REST_SIZE is set up just to be consistent
1073 with other pseudovectors. */
1074
1075 INLINE void
BUFFER_PVEC_INIT(struct buffer * b)1076 BUFFER_PVEC_INIT (struct buffer *b)
1077 {
1078 XSETPVECTYPESIZE (b, PVEC_BUFFER, BUFFER_LISP_SIZE, BUFFER_REST_SIZE);
1079 }
1080
1081 /* Convenient check whether buffer B is live. */
1082
1083 INLINE bool
BUFFER_LIVE_P(struct buffer * b)1084 BUFFER_LIVE_P (struct buffer *b)
1085 {
1086 return !NILP (BVAR (b, name));
1087 }
1088
1089 /* Convenient check whether buffer B is hidden (i.e. its name
1090 starts with a space). Caller must ensure that B is live. */
1091
1092 INLINE bool
BUFFER_HIDDEN_P(struct buffer * b)1093 BUFFER_HIDDEN_P (struct buffer *b)
1094 {
1095 return SREF (BVAR (b, name), 0) == ' ';
1096 }
1097
1098 /* Verify indirection counters. */
1099
1100 INLINE void
BUFFER_CHECK_INDIRECTION(struct buffer * b)1101 BUFFER_CHECK_INDIRECTION (struct buffer *b)
1102 {
1103 if (BUFFER_LIVE_P (b))
1104 {
1105 if (b->base_buffer)
1106 {
1107 eassert (b->indirections == -1);
1108 eassert (b->base_buffer->indirections > 0);
1109 }
1110 else
1111 eassert (b->indirections >= 0);
1112 }
1113 }
1114
1115 /* This structure holds the default values of the buffer-local variables
1116 that have special slots in each buffer.
1117 The default value occupies the same slot in this structure
1118 as an individual buffer's value occupies in that buffer.
1119 Setting the default value also goes through the alist of buffers
1120 and stores into each buffer that does not say it has a local value. */
1121
1122 extern struct buffer buffer_defaults;
1123
1124 /* This structure marks which slots in a buffer have corresponding
1125 default values in buffer_defaults.
1126 Each such slot has a nonzero value in this structure.
1127 The value has only one nonzero bit.
1128
1129 When a buffer has its own local value for a slot,
1130 the entry for that slot (found in the same slot in this structure)
1131 is turned on in the buffer's local_flags array.
1132
1133 If a slot in this structure is zero, then even though there may
1134 be a Lisp-level local variable for the slot, it has no default value,
1135 and the corresponding slot in buffer_defaults is not used. */
1136
1137
1138 extern struct buffer buffer_local_flags;
1139
1140 /* For each buffer slot, this points to the Lisp symbol name
1141 for that slot in the current buffer. It is 0 for slots
1142 that don't have such names. */
1143
1144 extern struct buffer buffer_local_symbols;
1145
1146 /* verify_interval_modification saves insertion hooks here
1147 to be run later by report_interval_modification. */
1148 extern Lisp_Object interval_insert_behind_hooks;
1149 extern Lisp_Object interval_insert_in_front_hooks;
1150
1151
1152 extern EMACS_INT fix_position (Lisp_Object);
1153 #define CHECK_FIXNUM_COERCE_MARKER(x) ((x) = make_fixnum (fix_position (x)))
1154 extern void delete_all_overlays (struct buffer *);
1155 extern void reset_buffer (struct buffer *);
1156 extern void compact_buffer (struct buffer *);
1157 extern void evaporate_overlays (ptrdiff_t);
1158 extern ptrdiff_t overlays_at (EMACS_INT, bool, Lisp_Object **,
1159 ptrdiff_t *, ptrdiff_t *, ptrdiff_t *, bool);
1160 extern ptrdiff_t sort_overlays (Lisp_Object *, ptrdiff_t, struct window *);
1161 extern void recenter_overlay_lists (struct buffer *, ptrdiff_t);
1162 extern ptrdiff_t overlay_strings (ptrdiff_t, struct window *, unsigned char **);
1163 extern void validate_region (Lisp_Object *, Lisp_Object *);
1164 extern void set_buffer_internal_1 (struct buffer *);
1165 extern void set_buffer_internal_2 (struct buffer *);
1166 extern void set_buffer_temp (struct buffer *);
1167 extern Lisp_Object buffer_local_value (Lisp_Object, Lisp_Object);
1168 extern void record_buffer (Lisp_Object);
1169 extern void fix_overlays_before (struct buffer *, ptrdiff_t, ptrdiff_t);
1170 extern void mmap_set_vars (bool);
1171 extern void restore_buffer (Lisp_Object);
1172 extern void set_buffer_if_live (Lisp_Object);
1173
1174 /* Return B as a struct buffer pointer, defaulting to the current buffer. */
1175
1176 INLINE struct buffer *
decode_buffer(Lisp_Object b)1177 decode_buffer (Lisp_Object b)
1178 {
1179 return NILP (b) ? current_buffer : (CHECK_BUFFER (b), XBUFFER (b));
1180 }
1181
1182 /* Set the current buffer to B.
1183
1184 We previously set windows_or_buffers_changed here to invalidate
1185 global unchanged information in beg_unchanged and end_unchanged.
1186 This is no longer necessary because we now compute unchanged
1187 information on a buffer-basis. Every action affecting other
1188 windows than the selected one requires a select_window at some
1189 time, and that increments windows_or_buffers_changed. */
1190
1191 INLINE void
set_buffer_internal(struct buffer * b)1192 set_buffer_internal (struct buffer *b)
1193 {
1194 if (current_buffer != b)
1195 set_buffer_internal_1 (b);
1196 }
1197
1198 /* Arrange to go back to the original buffer after the next
1199 call to unbind_to if the original buffer is still alive. */
1200
1201 INLINE void
record_unwind_current_buffer(void)1202 record_unwind_current_buffer (void)
1203 {
1204 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
1205 }
1206
1207 /* Get overlays at POSN into array OVERLAYS with NOVERLAYS elements.
1208 If NEXTP is non-NULL, return next overlay there.
1209 See overlay_at arg CHANGE_REQ for meaning of CHRQ arg.
1210 This macro might evaluate its args multiple times,
1211 and it treat some args as lvalues. */
1212
1213 #define GET_OVERLAYS_AT(posn, overlays, noverlays, nextp, chrq) \
1214 do { \
1215 ptrdiff_t maxlen = 40; \
1216 SAFE_NALLOCA (overlays, 1, maxlen); \
1217 (noverlays) = overlays_at (posn, false, &(overlays), &maxlen, \
1218 nextp, NULL, chrq); \
1219 if ((noverlays) > maxlen) \
1220 { \
1221 maxlen = noverlays; \
1222 SAFE_NALLOCA (overlays, 1, maxlen); \
1223 (noverlays) = overlays_at (posn, false, &(overlays), &maxlen, \
1224 nextp, NULL, chrq); \
1225 } \
1226 } while (false)
1227
1228 extern Lisp_Object Vbuffer_alist;
1229
1230 /* FOR_EACH_LIVE_BUFFER (LIST_VAR, BUF_VAR) followed by a statement is
1231 a `for' loop which iterates over the buffers from Vbuffer_alist. */
1232
1233 #define FOR_EACH_LIVE_BUFFER(list_var, buf_var) \
1234 FOR_EACH_ALIST_VALUE (Vbuffer_alist, list_var, buf_var)
1235
1236 /* Get text properties of B. */
1237
1238 INLINE INTERVAL
buffer_intervals(struct buffer * b)1239 buffer_intervals (struct buffer *b)
1240 {
1241 eassert (b->text != NULL);
1242 return b->text->intervals;
1243 }
1244
1245 /* Set text properties of B to I. */
1246
1247 INLINE void
set_buffer_intervals(struct buffer * b,INTERVAL i)1248 set_buffer_intervals (struct buffer *b, INTERVAL i)
1249 {
1250 eassert (b->text != NULL);
1251 b->text->intervals = i;
1252 }
1253
1254 /* Non-zero if current buffer has overlays. */
1255
1256 INLINE bool
buffer_has_overlays(void)1257 buffer_has_overlays (void)
1258 {
1259 return current_buffer->overlays_before || current_buffer->overlays_after;
1260 }
1261
1262 /* Functions for accessing a character or byte,
1263 or converting between byte positions and addresses,
1264 in a specified buffer. */
1265
1266 /* Return character code of multi-byte form at byte position POS. If POS
1267 doesn't point the head of valid multi-byte form, only the byte at
1268 POS is returned. No range checking. */
1269
1270 INLINE int
FETCH_MULTIBYTE_CHAR(ptrdiff_t pos)1271 FETCH_MULTIBYTE_CHAR (ptrdiff_t pos)
1272 {
1273 unsigned char *p = BYTE_POS_ADDR (pos);
1274 return STRING_CHAR (p);
1275 }
1276
1277 /* Return character code of multi-byte form at byte position POS in BUF.
1278 If POS doesn't point the head of valid multi-byte form, only the byte at
1279 POS is returned. No range checking. */
1280
1281 INLINE int
BUF_FETCH_MULTIBYTE_CHAR(struct buffer * buf,ptrdiff_t pos)1282 BUF_FETCH_MULTIBYTE_CHAR (struct buffer *buf, ptrdiff_t pos)
1283 {
1284 unsigned char *p
1285 = ((pos >= BUF_GPT_BYTE (buf) ? BUF_GAP_SIZE (buf) : 0)
1286 + pos + BUF_BEG_ADDR (buf) - BEG_BYTE);
1287 return STRING_CHAR (p);
1288 }
1289
1290 /* Return character at byte position POS.
1291 If the current buffer is unibyte and the character is not ASCII,
1292 make the returning character multibyte. */
1293
1294 INLINE int
FETCH_CHAR_AS_MULTIBYTE(ptrdiff_t pos)1295 FETCH_CHAR_AS_MULTIBYTE (ptrdiff_t pos)
1296 {
1297 return (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1298 ? FETCH_MULTIBYTE_CHAR (pos)
1299 : UNIBYTE_TO_CHAR (FETCH_BYTE (pos)));
1300 }
1301
1302 /* Return character at byte position POS.
1303 See the caveat WARNING for FETCH_MULTIBYTE_CHAR above. */
1304
1305 INLINE int
FETCH_CHAR(ptrdiff_t pos)1306 FETCH_CHAR (ptrdiff_t pos)
1307 {
1308 return (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1309 ? FETCH_MULTIBYTE_CHAR (pos)
1310 : FETCH_BYTE (pos));
1311 }
1312
1313 /* Return the address of character at byte position POS in buffer BUF.
1314 Note that both arguments can be computed more than once. */
1315
1316 INLINE unsigned char *
BUF_BYTE_ADDRESS(struct buffer * buf,ptrdiff_t pos)1317 BUF_BYTE_ADDRESS (struct buffer *buf, ptrdiff_t pos)
1318 {
1319 return (buf->text->beg + pos - BEG_BYTE
1320 + (pos < buf->text->gpt_byte ? 0 : buf->text->gap_size));
1321 }
1322
1323 /* Return the address of character at char position POS in buffer BUF.
1324 Note that both arguments can be computed more than once. */
1325
1326 INLINE unsigned char *
BUF_CHAR_ADDRESS(struct buffer * buf,ptrdiff_t pos)1327 BUF_CHAR_ADDRESS (struct buffer *buf, ptrdiff_t pos)
1328 {
1329 return (buf->text->beg + buf_charpos_to_bytepos (buf, pos) - BEG_BYTE
1330 + (pos < buf->text->gpt ? 0 : buf->text->gap_size));
1331 }
1332
1333 /* Convert PTR, the address of a char in buffer BUF,
1334 into a character position. */
1335
1336 INLINE ptrdiff_t
BUF_PTR_BYTE_POS(struct buffer * buf,unsigned char * ptr)1337 BUF_PTR_BYTE_POS (struct buffer *buf, unsigned char *ptr)
1338 {
1339 ptrdiff_t byte = ptr - buf->text->beg;
1340 return (byte - (byte <= BUF_GPT_BYTE (buf) - BEG_BYTE ? 0 : BUF_GAP_SIZE (buf))
1341 + BEG_BYTE);
1342 }
1343
1344 /* Return the byte at byte position N in buffer BUF. */
1345
1346 INLINE unsigned char
BUF_FETCH_BYTE(struct buffer * buf,ptrdiff_t n)1347 BUF_FETCH_BYTE (struct buffer *buf, ptrdiff_t n)
1348 {
1349 return *BUF_BYTE_ADDRESS (buf, n);
1350 }
1351
1352 /* Return character at byte position POS in buffer BUF. If BUF is
1353 unibyte and the character is not ASCII, make the returning
1354 character multibyte. */
1355
1356 INLINE int
BUF_FETCH_CHAR_AS_MULTIBYTE(struct buffer * buf,ptrdiff_t pos)1357 BUF_FETCH_CHAR_AS_MULTIBYTE (struct buffer *buf, ptrdiff_t pos)
1358 {
1359 return (! NILP (BVAR (buf, enable_multibyte_characters))
1360 ? BUF_FETCH_MULTIBYTE_CHAR (buf, pos)
1361 : UNIBYTE_TO_CHAR (BUF_FETCH_BYTE (buf, pos)));
1362 }
1363
1364 /* Return number of windows showing B. */
1365
1366 INLINE int
buffer_window_count(struct buffer * b)1367 buffer_window_count (struct buffer *b)
1368 {
1369 if (b->base_buffer)
1370 b = b->base_buffer;
1371 eassert (b->window_count >= 0);
1372 return b->window_count;
1373 }
1374
1375 /* Overlays */
1376
1377 /* Return the marker that stands for where OV starts in the buffer. */
1378
1379 #define OVERLAY_START(OV) XOVERLAY (OV)->start
1380
1381 /* Return the marker that stands for where OV ends in the buffer. */
1382
1383 #define OVERLAY_END(OV) XOVERLAY (OV)->end
1384
1385 /* Return the plist of overlay OV. */
1386
1387 #define OVERLAY_PLIST(OV) XOVERLAY (OV)->plist
1388
1389 /* Return the actual buffer position for the marker P.
1390 We assume you know which buffer it's pointing into. */
1391
1392 INLINE ptrdiff_t
OVERLAY_POSITION(Lisp_Object p)1393 OVERLAY_POSITION (Lisp_Object p)
1394 {
1395 return marker_position (p);
1396 }
1397
1398
1399 /***********************************************************************
1400 Buffer-local Variables
1401 ***********************************************************************/
1402
1403 /* Return the offset in bytes of member VAR of struct buffer
1404 from the start of a buffer structure. */
1405
1406 #define PER_BUFFER_VAR_OFFSET(VAR) \
1407 offsetof (struct buffer, VAR ## _)
1408
1409 /* Used to iterate over normal Lisp_Object fields of struct buffer (all
1410 Lisp_Objects except undo_list). If you add, remove, or reorder
1411 Lisp_Objects in a struct buffer, make sure that this is still correct. */
1412
1413 #define FOR_EACH_PER_BUFFER_OBJECT_AT(offset) \
1414 for (offset = PER_BUFFER_VAR_OFFSET (name); \
1415 offset <= PER_BUFFER_VAR_OFFSET (cursor_in_non_selected_windows); \
1416 offset += word_size)
1417
1418 /* Return the index of buffer-local variable VAR. Each per-buffer
1419 variable has an index > 0 associated with it, except when it always
1420 has buffer-local values, in which case the index is -1. If this is
1421 0, this is a bug and means that the slot of VAR in
1422 buffer_local_flags wasn't initialized. */
1423
1424 #define PER_BUFFER_VAR_IDX(VAR) \
1425 PER_BUFFER_IDX (PER_BUFFER_VAR_OFFSET (VAR))
1426
1427 extern bool valid_per_buffer_idx (int);
1428
1429 /* Value is true if the variable with index IDX has a local value
1430 in buffer B. */
1431
1432 INLINE bool
PER_BUFFER_VALUE_P(struct buffer * b,int idx)1433 PER_BUFFER_VALUE_P (struct buffer *b, int idx)
1434 {
1435 eassert (valid_per_buffer_idx (idx));
1436 return b->local_flags[idx];
1437 }
1438
1439 /* Set whether per-buffer variable with index IDX has a buffer-local
1440 value in buffer B. VAL zero means it hasn't. */
1441
1442 INLINE void
SET_PER_BUFFER_VALUE_P(struct buffer * b,int idx,bool val)1443 SET_PER_BUFFER_VALUE_P (struct buffer *b, int idx, bool val)
1444 {
1445 eassert (valid_per_buffer_idx (idx));
1446 b->local_flags[idx] = val;
1447 }
1448
1449 /* Return the index value of the per-buffer variable at offset OFFSET
1450 in the buffer structure.
1451
1452 If the slot OFFSET has a corresponding default value in
1453 buffer_defaults, the index value is positive and has only one
1454 nonzero bit. When a buffer has its own local value for a slot, the
1455 bit for that slot (found in the same slot in this structure) is
1456 turned on in the buffer's local_flags array.
1457
1458 If the index value is -1, even though there may be a
1459 DEFVAR_PER_BUFFER for the slot, there is no default value for it;
1460 and the corresponding slot in buffer_defaults is not used.
1461
1462 If the index value is -2, then there is no DEFVAR_PER_BUFFER for
1463 the slot, but there is a default value which is copied into each
1464 new buffer.
1465
1466 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
1467 zero, that is a bug. */
1468
1469 INLINE int
PER_BUFFER_IDX(ptrdiff_t offset)1470 PER_BUFFER_IDX (ptrdiff_t offset)
1471 {
1472 return XFIXNUM (*(Lisp_Object *) (offset + (char *) &buffer_local_flags));
1473 }
1474
1475 /* Functions to get and set default value of the per-buffer
1476 variable at offset OFFSET in the buffer structure. */
1477
1478 INLINE Lisp_Object
per_buffer_default(int offset)1479 per_buffer_default (int offset)
1480 {
1481 return *(Lisp_Object *)(offset + (char *) &buffer_defaults);
1482 }
1483
1484 INLINE void
set_per_buffer_default(int offset,Lisp_Object value)1485 set_per_buffer_default (int offset, Lisp_Object value)
1486 {
1487 *(Lisp_Object *)(offset + (char *) &buffer_defaults) = value;
1488 }
1489
1490 /* Functions to get and set buffer-local value of the per-buffer
1491 variable at offset OFFSET in the buffer structure. */
1492
1493 INLINE Lisp_Object
per_buffer_value(struct buffer * b,int offset)1494 per_buffer_value (struct buffer *b, int offset)
1495 {
1496 return *(Lisp_Object *)(offset + (char *) b);
1497 }
1498
1499 INLINE void
set_per_buffer_value(struct buffer * b,int offset,Lisp_Object value)1500 set_per_buffer_value (struct buffer *b, int offset, Lisp_Object value)
1501 {
1502 *(Lisp_Object *)(offset + (char *) b) = value;
1503 }
1504
1505 /* Downcase a character C, or make no change if that cannot be done. */
1506 INLINE int
downcase(int c)1507 downcase (int c)
1508 {
1509 Lisp_Object downcase_table = BVAR (current_buffer, downcase_table);
1510 Lisp_Object down = CHAR_TABLE_REF (downcase_table, c);
1511 return FIXNATP (down) ? XFIXNAT (down) : c;
1512 }
1513
1514 /* Upcase a character C, or make no change if that cannot be done. */
1515 INLINE int
upcase(int c)1516 upcase (int c)
1517 {
1518 Lisp_Object upcase_table = BVAR (current_buffer, upcase_table);
1519 Lisp_Object up = CHAR_TABLE_REF (upcase_table, c);
1520 return FIXNATP (up) ? XFIXNAT (up) : c;
1521 }
1522
1523 /* True if C is upper case. */
1524 INLINE bool
uppercasep(int c)1525 uppercasep (int c)
1526 {
1527 return downcase (c) != c;
1528 }
1529
1530 /* True if C is lower case. */
1531 INLINE bool
lowercasep(int c)1532 lowercasep (int c)
1533 {
1534 return !uppercasep (c) && upcase (c) != c;
1535 }
1536
1537 /* Return a non-outlandish value for the tab width. */
1538
1539 INLINE int
sanitize_tab_width(Lisp_Object width)1540 sanitize_tab_width (Lisp_Object width)
1541 {
1542 return (FIXNUMP (width) && 0 < XFIXNUM (width) && XFIXNUM (width) <= 1000
1543 ? XFIXNUM (width) : 8);
1544 }
1545
1546 INLINE int
SANE_TAB_WIDTH(struct buffer * buf)1547 SANE_TAB_WIDTH (struct buffer *buf)
1548 {
1549 return sanitize_tab_width (BVAR (buf, tab_width));
1550 }
1551
1552 /* Return a non-outlandish value for a character width. */
1553
1554 INLINE int
sanitize_char_width(EMACS_INT width)1555 sanitize_char_width (EMACS_INT width)
1556 {
1557 return 0 <= width && width <= 1000 ? width : 1000;
1558 }
1559
1560 /* Return the width of character C. The width is measured by how many
1561 columns C will occupy on the screen when displayed in the current
1562 buffer. The name CHARACTER_WIDTH avoids a collision with <limits.h>
1563 CHAR_WIDTH. */
1564
1565 INLINE int
CHARACTER_WIDTH(int c)1566 CHARACTER_WIDTH (int c)
1567 {
1568 return (0x20 <= c && c < 0x7f ? 1
1569 : 0x7f < c ? (sanitize_char_width
1570 (XFIXNUM (CHAR_TABLE_REF (Vchar_width_table, c))))
1571 : c == '\t' ? SANE_TAB_WIDTH (current_buffer)
1572 : c == '\n' ? 0
1573 : !NILP (BVAR (current_buffer, ctl_arrow)) ? 2 : 4);
1574 }
1575
1576
1577 /* Like fetch_string_char_advance, but fetch character from the current
1578 buffer. */
1579
1580 INLINE int
fetch_char_advance(ptrdiff_t * charidx,ptrdiff_t * byteidx)1581 fetch_char_advance (ptrdiff_t *charidx, ptrdiff_t *byteidx)
1582 {
1583 int output;
1584 ptrdiff_t c = *charidx, b = *byteidx;
1585 c++;
1586 unsigned char *chp = BYTE_POS_ADDR (b);
1587 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1588 {
1589 int chlen;
1590 output = string_char_and_length (chp, &chlen);
1591 b += chlen;
1592 }
1593 else
1594 {
1595 output = *chp;
1596 b++;
1597 }
1598 *charidx = c;
1599 *byteidx = b;
1600 return output;
1601 }
1602
1603
1604 /* Like fetch_char_advance, but assumes the current buffer is multibyte. */
1605
1606 INLINE int
fetch_char_advance_no_check(ptrdiff_t * charidx,ptrdiff_t * byteidx)1607 fetch_char_advance_no_check (ptrdiff_t *charidx, ptrdiff_t *byteidx)
1608 {
1609 int output;
1610 ptrdiff_t c = *charidx, b = *byteidx;
1611 c++;
1612 unsigned char *chp = BYTE_POS_ADDR (b);
1613 int chlen;
1614 output = string_char_and_length (chp, &chlen);
1615 b += chlen;
1616 *charidx = c;
1617 *byteidx = b;
1618 return output;
1619 }
1620
1621 /* Return the number of bytes in the multibyte character in BUF
1622 that starts at position POS_BYTE. This relies on the fact that
1623 *GPT_ADDR and *Z_ADDR are always accessible and the values are
1624 '\0'. No range checking of POS_BYTE. */
1625
1626 INLINE int
buf_next_char_len(struct buffer * buf,ptrdiff_t pos_byte)1627 buf_next_char_len (struct buffer *buf, ptrdiff_t pos_byte)
1628 {
1629 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte);
1630 return BYTES_BY_CHAR_HEAD (*chp);
1631 }
1632
1633 INLINE int
next_char_len(ptrdiff_t pos_byte)1634 next_char_len (ptrdiff_t pos_byte)
1635 {
1636 return buf_next_char_len (current_buffer, pos_byte);
1637 }
1638
1639 /* Return the number of bytes in the multibyte character in BUF just
1640 before POS_BYTE. No range checking of POS_BYTE. */
1641
1642 INLINE int
buf_prev_char_len(struct buffer * buf,ptrdiff_t pos_byte)1643 buf_prev_char_len (struct buffer *buf, ptrdiff_t pos_byte)
1644 {
1645 unsigned char *chp
1646 = (BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE
1647 + (pos_byte <= BUF_GPT_BYTE (buf) ? 0 : BUF_GAP_SIZE (buf)));
1648 return raw_prev_char_len (chp);
1649 }
1650
1651 INLINE int
prev_char_len(ptrdiff_t pos_byte)1652 prev_char_len (ptrdiff_t pos_byte)
1653 {
1654 return buf_prev_char_len (current_buffer, pos_byte);
1655 }
1656
1657 /* Increment both *CHARPOS and *BYTEPOS, each in the appropriate way. */
1658
1659 INLINE void
inc_both(ptrdiff_t * charpos,ptrdiff_t * bytepos)1660 inc_both (ptrdiff_t *charpos, ptrdiff_t *bytepos)
1661 {
1662 (*charpos)++;
1663 (*bytepos) += (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1664 ? next_char_len (*bytepos) : 1);
1665 }
1666
1667 /* Decrement both *CHARPOS and *BYTEPOS, each in the appropriate way. */
1668
1669 INLINE void
dec_both(ptrdiff_t * charpos,ptrdiff_t * bytepos)1670 dec_both (ptrdiff_t *charpos, ptrdiff_t *bytepos)
1671 {
1672 (*charpos)--;
1673 (*bytepos) -= (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1674 ? prev_char_len (*bytepos) : 1);
1675 }
1676
1677 INLINE_HEADER_END
1678
1679 #endif /* EMACS_BUFFER_H */
1680