aboutsummaryrefslogtreecommitdiffstats
path: root/src/buffer.h
diff options
context:
space:
mode:
authorStefan Monnier2022-09-25 16:15:16 -0400
committerStefan Monnier2022-09-25 16:15:16 -0400
commit650c20f1ca4e07591a727e1cfcc74b3363d15985 (patch)
tree85d11f6437cde22f410c25e0e5f71a3131ebd07d /src/buffer.h
parent8869332684c2302b5ba1ead4568bbc7ba1c0183e (diff)
parent4b85ae6a24380fb67a3315eaec9233f17a872473 (diff)
downloademacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.tar.gz
emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.zip
Merge 'master' into noverlay
Diffstat (limited to 'src/buffer.h')
-rw-r--r--src/buffer.h981
1 files changed, 644 insertions, 337 deletions
diff --git a/src/buffer.h b/src/buffer.h
index ef31ad1ed9d..7d6c693b0f2 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -1,7 +1,6 @@
1/* Header file for the buffer manipulation primitives. 1/* Header file for the buffer manipulation primitives.
2 2
3Copyright (C) 1985-1986, 1993-1995, 1997-2017 Free Software Foundation, 3Copyright (C) 1985-2022 Free Software Foundation, Inc.
4Inc.
5 4
6This file is part of GNU Emacs. 5This file is part of GNU Emacs.
7 6
@@ -32,12 +31,11 @@ INLINE_HEADER_BEGIN
32 31
33/* Accessing the parameters of the current buffer. */ 32/* Accessing the parameters of the current buffer. */
34 33
35/* These macros come in pairs, one for the char position 34/* These constants and macros come in pairs, one for the char position
36 and one for the byte position. */ 35 and one for the byte position. */
37 36
38/* Position of beginning of buffer. */ 37/* Position of beginning of buffer. */
39#define BEG (1) 38enum { BEG = 1, BEG_BYTE = BEG };
40#define BEG_BYTE (BEG)
41 39
42/* Position of beginning of accessible range of buffer. */ 40/* Position of beginning of accessible range of buffer. */
43#define BEGV (current_buffer->begv) 41#define BEGV (current_buffer->begv)
@@ -62,6 +60,14 @@ INLINE_HEADER_BEGIN
62 60
63/* Macros for the addresses of places in the buffer. */ 61/* Macros for the addresses of places in the buffer. */
64 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
65/* Address of beginning of buffer. */ 71/* Address of beginning of buffer. */
66#define BEG_ADDR (current_buffer->text->beg) 72#define BEG_ADDR (current_buffer->text->beg)
67 73
@@ -97,59 +103,7 @@ INLINE_HEADER_BEGIN
97 103
98/* Modification count as of last visit or save. */ 104/* Modification count as of last visit or save. */
99#define SAVE_MODIFF (current_buffer->text->save_modiff) 105#define SAVE_MODIFF (current_buffer->text->save_modiff)
100
101/* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
102 the max (resp. min) p such that
103
104 BYTE_POS_ADDR (p) - BYTE_POS_ADDR (n) == p - n */
105
106#define BUFFER_CEILING_OF(BYTEPOS) \
107 (((BYTEPOS) < GPT_BYTE && GPT < ZV ? GPT_BYTE : ZV_BYTE) - 1)
108#define BUFFER_FLOOR_OF(BYTEPOS) \
109 (BEGV <= GPT && GPT_BYTE <= (BYTEPOS) ? GPT_BYTE : BEGV_BYTE)
110 106
111/* Similar macros to operate on a specified buffer.
112 Note that many of these evaluate the buffer argument more than once. */
113
114/* Position of beginning of buffer. */
115#define BUF_BEG(buf) (BEG)
116#define BUF_BEG_BYTE(buf) (BEG_BYTE)
117
118/* The BUF_BEGV[_BYTE], BUF_ZV[_BYTE], and BUF_PT[_BYTE] macros cannot
119 be used for assignment; use SET_BUF_* macros below for that. */
120
121/* Position of beginning of accessible range of buffer. */
122#define BUF_BEGV(buf) \
123 (buf == current_buffer ? BEGV \
124 : NILP (BVAR (buf, begv_marker)) ? buf->begv \
125 : marker_position (BVAR (buf, begv_marker)))
126
127#define BUF_BEGV_BYTE(buf) \
128 (buf == current_buffer ? BEGV_BYTE \
129 : NILP (BVAR (buf, begv_marker)) ? buf->begv_byte \
130 : marker_byte_position (BVAR (buf, begv_marker)))
131
132/* Position of point in buffer. */
133#define BUF_PT(buf) \
134 (buf == current_buffer ? PT \
135 : NILP (BVAR (buf, pt_marker)) ? buf->pt \
136 : marker_position (BVAR (buf, pt_marker)))
137
138#define BUF_PT_BYTE(buf) \
139 (buf == current_buffer ? PT_BYTE \
140 : NILP (BVAR (buf, pt_marker)) ? buf->pt_byte \
141 : marker_byte_position (BVAR (buf, pt_marker)))
142
143/* Position of end of accessible range of buffer. */
144#define BUF_ZV(buf) \
145 (buf == current_buffer ? ZV \
146 : NILP (BVAR (buf, zv_marker)) ? buf->zv \
147 : marker_position (BVAR (buf, zv_marker)))
148
149#define BUF_ZV_BYTE(buf) \
150 (buf == current_buffer ? ZV_BYTE \
151 : NILP (BVAR (buf, zv_marker)) ? buf->zv_byte \
152 : marker_byte_position (BVAR (buf, zv_marker)))
153 107
154/* Position of gap in buffer. */ 108/* Position of gap in buffer. */
155#define BUF_GPT(buf) ((buf)->text->gpt) 109#define BUF_GPT(buf) ((buf)->text->gpt)
@@ -162,15 +116,6 @@ INLINE_HEADER_BEGIN
162/* Address of beginning of buffer. */ 116/* Address of beginning of buffer. */
163#define BUF_BEG_ADDR(buf) ((buf)->text->beg) 117#define BUF_BEG_ADDR(buf) ((buf)->text->beg)
164 118
165/* Address of beginning of gap of buffer. */
166#define BUF_GPT_ADDR(buf) ((buf)->text->beg + (buf)->text->gpt_byte - BEG_BYTE)
167
168/* Address of end of buffer. */
169#define BUF_Z_ADDR(buf) ((buf)->text->beg + (buf)->text->gap_size + (buf)->text->z_byte - BEG_BYTE)
170
171/* Address of end of gap in buffer. */
172#define BUF_GAP_END_ADDR(buf) ((buf)->text->beg + (buf)->text->gpt_byte + (buf)->text->gap_size - BEG_BYTE)
173
174/* Size of gap. */ 119/* Size of gap. */
175#define BUF_GAP_SIZE(buf) ((buf)->text->gap_size) 120#define BUF_GAP_SIZE(buf) ((buf)->text->gap_size)
176 121
@@ -204,6 +149,9 @@ INLINE_HEADER_BEGIN
204#define BUF_BEG_UNCHANGED(buf) ((buf)->text->beg_unchanged) 149#define BUF_BEG_UNCHANGED(buf) ((buf)->text->beg_unchanged)
205#define BUF_END_UNCHANGED(buf) ((buf)->text->end_unchanged) 150#define BUF_END_UNCHANGED(buf) ((buf)->text->end_unchanged)
206 151
152#define BUF_CHARS_UNCHANGED_MODIFIED(buf) \
153 ((buf)->text->chars_unchanged_modified)
154
207#define UNCHANGED_MODIFIED \ 155#define UNCHANGED_MODIFIED \
208 BUF_UNCHANGED_MODIFIED (current_buffer) 156 BUF_UNCHANGED_MODIFIED (current_buffer)
209#define OVERLAY_UNCHANGED_MODIFIED \ 157#define OVERLAY_UNCHANGED_MODIFIED \
@@ -211,42 +159,10 @@ INLINE_HEADER_BEGIN
211#define BEG_UNCHANGED BUF_BEG_UNCHANGED (current_buffer) 159#define BEG_UNCHANGED BUF_BEG_UNCHANGED (current_buffer)
212#define END_UNCHANGED BUF_END_UNCHANGED (current_buffer) 160#define END_UNCHANGED BUF_END_UNCHANGED (current_buffer)
213 161
214/* Compute how many characters at the top and bottom of BUF are 162#define CHARS_UNCHANGED_MODIFIED \
215 unchanged when the range START..END is modified. This computation 163 BUF_CHARS_UNCHANGED_MODIFIED (current_buffer)
216 must be done each time BUF is modified. */
217
218#define BUF_COMPUTE_UNCHANGED(buf, start, end) \
219 do \
220 { \
221 if (BUF_UNCHANGED_MODIFIED (buf) == BUF_MODIFF (buf) \
222 && (BUF_OVERLAY_UNCHANGED_MODIFIED (buf) \
223 == BUF_OVERLAY_MODIFF (buf))) \
224 { \
225 BUF_BEG_UNCHANGED (buf) = (start) - BUF_BEG (buf); \
226 BUF_END_UNCHANGED (buf) = BUF_Z (buf) - (end); \
227 } \
228 else \
229 { \
230 if (BUF_Z (buf) - (end) < BUF_END_UNCHANGED (buf)) \
231 BUF_END_UNCHANGED (buf) = BUF_Z (buf) - (end); \
232 if ((start) - BUF_BEG (buf) < BUF_BEG_UNCHANGED (buf)) \
233 BUF_BEG_UNCHANGED (buf) = (start) - BUF_BEG (buf); \
234 } \
235 } \
236 while (false)
237
238 164
239/* Macros to set PT in the current buffer, or another buffer. */ 165/* Functions to set PT in the current buffer, or another buffer. */
240
241#define SET_PT(position) (set_point (position))
242#define TEMP_SET_PT(position) (temp_set_point (current_buffer, (position)))
243
244#define SET_PT_BOTH(position, byte) (set_point_both (position, byte))
245#define TEMP_SET_PT_BOTH(position, byte) \
246 (temp_set_point_both (current_buffer, (position), (byte)))
247
248#define BUF_TEMP_SET_PT(buffer, position) \
249 (temp_set_point ((buffer), (position)))
250 166
251extern void set_point (ptrdiff_t); 167extern void set_point (ptrdiff_t);
252extern void temp_set_point (struct buffer *, ptrdiff_t); 168extern void temp_set_point (struct buffer *, ptrdiff_t);
@@ -256,61 +172,32 @@ extern void temp_set_point_both (struct buffer *,
256extern void set_point_from_marker (Lisp_Object); 172extern void set_point_from_marker (Lisp_Object);
257extern void enlarge_buffer_text (struct buffer *, ptrdiff_t); 173extern void enlarge_buffer_text (struct buffer *, ptrdiff_t);
258 174
175INLINE void
176SET_PT (ptrdiff_t position)
177{
178 set_point (position);
179}
180INLINE void
181TEMP_SET_PT (ptrdiff_t position)
182{
183 temp_set_point (current_buffer, position);
184}
185INLINE void
186SET_PT_BOTH (ptrdiff_t position, ptrdiff_t byte)
187{
188 set_point_both (position, byte);
189}
190INLINE void
191TEMP_SET_PT_BOTH (ptrdiff_t position, ptrdiff_t byte)
192{
193 temp_set_point_both (current_buffer, position, byte);
194}
195INLINE void
196BUF_TEMP_SET_PT (struct buffer *buffer, ptrdiff_t position)
197{
198 temp_set_point (buffer, position);
199}
259 200
260/* Macros for setting the BEGV, ZV or PT of a given buffer.
261
262 The ..._BOTH macros take both a charpos and a bytepos,
263 which must correspond to each other.
264
265 The macros without ..._BOTH take just a charpos,
266 and compute the bytepos from it. */
267
268#define SET_BUF_BEGV(buf, charpos) \
269 ((buf)->begv_byte = buf_charpos_to_bytepos ((buf), (charpos)), \
270 (buf)->begv = (charpos))
271
272#define SET_BUF_ZV(buf, charpos) \
273 ((buf)->zv_byte = buf_charpos_to_bytepos ((buf), (charpos)), \
274 (buf)->zv = (charpos))
275
276#define SET_BUF_BEGV_BOTH(buf, charpos, byte) \
277 ((buf)->begv = (charpos), \
278 (buf)->begv_byte = (byte))
279
280#define SET_BUF_ZV_BOTH(buf, charpos, byte) \
281 ((buf)->zv = (charpos), \
282 (buf)->zv_byte = (byte))
283
284#define SET_BUF_PT_BOTH(buf, charpos, byte) \
285 ((buf)->pt = (charpos), \
286 (buf)->pt_byte = (byte))
287
288/* Macros to access a character or byte in the current buffer,
289 or convert between a byte position and an address.
290 These macros do not check that the position is in range. */
291
292/* Access a Lisp position value in POS,
293 and store the charpos in CHARPOS and the bytepos in BYTEPOS. */
294
295#define DECODE_POSITION(charpos, bytepos, pos) \
296 do \
297 { \
298 Lisp_Object __pos = (pos); \
299 if (NUMBERP (__pos)) \
300 { \
301 charpos = __pos; \
302 bytepos = buf_charpos_to_bytepos (current_buffer, __pos); \
303 } \
304 else if (MARKERP (__pos)) \
305 { \
306 charpos = marker_position (__pos); \
307 bytepos = marker_byte_position (__pos); \
308 } \
309 else \
310 wrong_type_argument (Qinteger_or_marker_p, __pos); \
311 } \
312 while (false)
313
314/* Maximum number of bytes in a buffer. 201/* Maximum number of bytes in a buffer.
315 A buffer cannot contain more bytes than a 1-origin fixnum can represent, 202 A buffer cannot contain more bytes than a 1-origin fixnum can represent,
316 nor can it be so large that C pointer arithmetic stops working. 203 nor can it be so large that C pointer arithmetic stops working.
@@ -321,111 +208,21 @@ extern void enlarge_buffer_text (struct buffer *, ptrdiff_t);
321/* Maximum gap size after compact_buffer, in bytes. Also 208/* Maximum gap size after compact_buffer, in bytes. Also
322 used in make_gap_larger to get some extra reserved space. */ 209 used in make_gap_larger to get some extra reserved space. */
323 210
324#define GAP_BYTES_DFL 2000 211enum { GAP_BYTES_DFL = 2000 };
325 212
326/* Minimum gap size after compact_buffer, in bytes. Also 213/* Minimum gap size after compact_buffer, in bytes. Also
327 used in make_gap_smaller to avoid too small gap size. */ 214 used in make_gap_smaller to avoid too small gap size. */
328 215
329#define GAP_BYTES_MIN 20 216enum { GAP_BYTES_MIN = 20 };
330
331/* Return the address of byte position N in current buffer. */
332
333#define BYTE_POS_ADDR(n) \
334 (((n) >= GPT_BYTE ? GAP_SIZE : 0) + (n) + BEG_ADDR - BEG_BYTE)
335
336/* Return the address of char position N. */
337
338#define CHAR_POS_ADDR(n) \
339 (((n) >= GPT ? GAP_SIZE : 0) \
340 + buf_charpos_to_bytepos (current_buffer, n) \
341 + BEG_ADDR - BEG_BYTE)
342 217
343/* Convert a character position to a byte position. */ 218/* For those very rare cases where you may have a "random" pointer into
219 the middle of a multibyte char, this moves to the next boundary. */
220extern ptrdiff_t advance_to_char_boundary (ptrdiff_t byte_pos);
344 221
345#define CHAR_TO_BYTE(charpos) \ 222/* Return the byte at byte position N.
346 (buf_charpos_to_bytepos (current_buffer, charpos)) 223 Do not check that the position is in range. */
347
348/* Convert a byte position to a character position. */
349
350#define BYTE_TO_CHAR(bytepos) \
351 (buf_bytepos_to_charpos (current_buffer, bytepos))
352
353/* Convert PTR, the address of a byte in the buffer, into a byte position. */
354
355#define PTR_BYTE_POS(ptr) \
356 ((ptr) - (current_buffer)->text->beg \
357 - (ptr - (current_buffer)->text->beg <= GPT_BYTE - BEG_BYTE ? 0 : GAP_SIZE) \
358 + BEG_BYTE)
359
360/* Return character at byte position POS. See the caveat WARNING for
361 FETCH_MULTIBYTE_CHAR below. */
362
363#define FETCH_CHAR(pos) \
364 (!NILP (BVAR (current_buffer, enable_multibyte_characters)) \
365 ? FETCH_MULTIBYTE_CHAR ((pos)) \
366 : FETCH_BYTE ((pos)))
367
368/* Return the byte at byte position N. */
369 224
370#define FETCH_BYTE(n) *(BYTE_POS_ADDR ((n))) 225#define FETCH_BYTE(n) *(BYTE_POS_ADDR ((n)))
371
372/* Return character at byte position POS. If the current buffer is unibyte
373 and the character is not ASCII, make the returning character
374 multibyte. */
375
376#define FETCH_CHAR_AS_MULTIBYTE(pos) \
377 (!NILP (BVAR (current_buffer, enable_multibyte_characters)) \
378 ? FETCH_MULTIBYTE_CHAR ((pos)) \
379 : UNIBYTE_TO_CHAR (FETCH_BYTE ((pos))))
380
381
382/* Macros for accessing a character or byte,
383 or converting between byte positions and addresses,
384 in a specified buffer. */
385
386/* Return the address of character at byte position POS in buffer BUF.
387 Note that both arguments can be computed more than once. */
388
389#define BUF_BYTE_ADDRESS(buf, pos) \
390 ((buf)->text->beg + (pos) - BEG_BYTE \
391 + ((pos) >= (buf)->text->gpt_byte ? (buf)->text->gap_size : 0))
392
393/* Return the address of character at char position POS in buffer BUF.
394 Note that both arguments can be computed more than once. */
395
396#define BUF_CHAR_ADDRESS(buf, pos) \
397 ((buf)->text->beg + buf_charpos_to_bytepos ((buf), (pos)) - BEG_BYTE \
398 + ((pos) >= (buf)->text->gpt ? (buf)->text->gap_size : 0))
399
400/* Convert PTR, the address of a char in buffer BUF,
401 into a character position. */
402
403#define BUF_PTR_BYTE_POS(buf, ptr) \
404 ((ptr) - (buf)->text->beg \
405 - (ptr - (buf)->text->beg <= BUF_GPT_BYTE (buf) - BEG_BYTE \
406 ? 0 : BUF_GAP_SIZE ((buf))) \
407 + BEG_BYTE)
408
409/* Return the character at byte position POS in buffer BUF. */
410
411#define BUF_FETCH_CHAR(buf, pos) \
412 (!NILP (buf->enable_multibyte_characters) \
413 ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos)) \
414 : BUF_FETCH_BYTE ((buf), (pos)))
415
416/* Return character at byte position POS in buffer BUF. If BUF is
417 unibyte and the character is not ASCII, make the returning
418 character multibyte. */
419
420#define BUF_FETCH_CHAR_AS_MULTIBYTE(buf, pos) \
421 (! NILP (BVAR ((buf), enable_multibyte_characters)) \
422 ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos)) \
423 : UNIBYTE_TO_CHAR (BUF_FETCH_BYTE ((buf), (pos))))
424
425/* Return the byte at byte position N in buffer BUF. */
426
427#define BUF_FETCH_BYTE(buf, n) \
428 *(BUF_BYTE_ADDRESS ((buf), (n)))
429 226
430/* Define the actual buffer data structures. */ 227/* Define the actual buffer data structures. */
431 228
@@ -445,20 +242,21 @@ struct buffer_text
445 ptrdiff_t gpt_byte; /* Byte pos of gap in buffer. */ 242 ptrdiff_t gpt_byte; /* Byte pos of gap in buffer. */
446 ptrdiff_t z_byte; /* Byte pos of end of buffer. */ 243 ptrdiff_t z_byte; /* Byte pos of end of buffer. */
447 ptrdiff_t gap_size; /* Size of buffer's gap. */ 244 ptrdiff_t gap_size; /* Size of buffer's gap. */
448 EMACS_INT modiff; /* This counts buffer-modification events 245 modiff_count modiff; /* This counts buffer-modification events
449 for this buffer. It is incremented for 246 for this buffer. It is increased
450 each such event, and never otherwise 247 logarithmically to the extent of the
451 changed. */ 248 modification for each such event,
452 EMACS_INT chars_modiff; /* This is modified with character change 249 and never otherwise changed. */
250 modiff_count chars_modiff; /* This is modified with character change
453 events for this buffer. It is set to 251 events for this buffer. It is set to
454 modiff for each such event, and never 252 modiff for each such event, and never
455 otherwise changed. */ 253 otherwise changed. */
456 EMACS_INT save_modiff; /* Previous value of modiff, as of last 254 modiff_count save_modiff; /* Previous value of modiff, as of last
457 time buffer visited or saved a file. */ 255 time buffer visited or saved a file. */
458 256
459 EMACS_INT overlay_modiff; /* Counts modifications to overlays. */ 257 modiff_count overlay_modiff; /* Counts modifications to overlays. */
460 258
461 EMACS_INT compact; /* Set to modiff each time when compact_buffer 259 modiff_count compact; /* Set to modiff each time when compact_buffer
462 is called for this buffer. */ 260 is called for this buffer. */
463 261
464 /* Minimum value of GPT - BEG since last redisplay that finished. */ 262 /* Minimum value of GPT - BEG since last redisplay that finished. */
@@ -469,12 +267,17 @@ struct buffer_text
469 267
470 /* MODIFF as of last redisplay that finished; if it matches MODIFF, 268 /* MODIFF as of last redisplay that finished; if it matches MODIFF,
471 beg_unchanged and end_unchanged contain no useful information. */ 269 beg_unchanged and end_unchanged contain no useful information. */
472 EMACS_INT unchanged_modified; 270 modiff_count unchanged_modified;
473 271
474 /* BUF_OVERLAY_MODIFF of current buffer, as of last redisplay that 272 /* BUF_OVERLAY_MODIFF of current buffer, as of last redisplay that
475 finished; if it matches BUF_OVERLAY_MODIFF, beg_unchanged and 273 finished; if it matches BUF_OVERLAY_MODIFF, beg_unchanged and
476 end_unchanged contain no useful information. */ 274 end_unchanged contain no useful information. */
477 EMACS_INT overlay_unchanged_modified; 275 modiff_count overlay_unchanged_modified;
276
277 /* CHARS_MODIFF as of last redisplay that finished. It's used
278 when we only care about changes in actual buffer text, not in
279 any other kind of changes, like properties etc. */
280 modiff_count chars_unchanged_modified;
478 281
479 /* Properties of this buffer's text. */ 282 /* Properties of this buffer's text. */
480 INTERVAL intervals; 283 INTERVAL intervals;
@@ -501,11 +304,18 @@ struct buffer_text
501 304
502#define BVAR(buf, field) ((buf)->field ## _) 305#define BVAR(buf, field) ((buf)->field ## _)
503 306
307/* Max number of builtin per-buffer variables. */
308enum { MAX_PER_BUFFER_VARS = 50 };
309
310/* Special values for struct buffer.modtime. */
311enum { NONEXISTENT_MODTIME_NSECS = -1 };
312enum { UNKNOWN_MODTIME_NSECS = -2 };
313
504/* This is the structure that the buffer Lisp object points to. */ 314/* This is the structure that the buffer Lisp object points to. */
505 315
506struct buffer 316struct buffer
507{ 317{
508 struct vectorlike_header header; 318 union vectorlike_header header;
509 319
510 /* The name of this buffer. */ 320 /* The name of this buffer. */
511 Lisp_Object name_; 321 Lisp_Object name_;
@@ -548,6 +358,9 @@ struct buffer
548 /* Symbol naming major mode (e.g., lisp-mode). */ 358 /* Symbol naming major mode (e.g., lisp-mode). */
549 Lisp_Object major_mode_; 359 Lisp_Object major_mode_;
550 360
361 /* Symbol listing all currently enabled minor modes. */
362 Lisp_Object local_minor_modes_;
363
551 /* Pretty name of major mode (e.g., "Lisp"). */ 364 /* Pretty name of major mode (e.g., "Lisp"). */
552 Lisp_Object mode_name_; 365 Lisp_Object mode_name_;
553 366
@@ -558,6 +371,10 @@ struct buffer
558 of windows. Nil means don't display that line. */ 371 of windows. Nil means don't display that line. */
559 Lisp_Object header_line_format_; 372 Lisp_Object header_line_format_;
560 373
374 /* Analogous to mode_line_format for the line displayed at the top
375 of windows. Nil means don't display that line. */
376 Lisp_Object tab_line_format_;
377
561 /* Keys that are bound local to this buffer. */ 378 /* Keys that are bound local to this buffer. */
562 Lisp_Object keymap_; 379 Lisp_Object keymap_;
563 380
@@ -625,9 +442,6 @@ struct buffer
625 /* Non-nil means show ... at end of line followed by invisible lines. */ 442 /* Non-nil means show ... at end of line followed by invisible lines. */
626 Lisp_Object selective_display_ellipses_; 443 Lisp_Object selective_display_ellipses_;
627 444
628 /* Alist of (FUNCTION . STRING) for each minor mode enabled in buffer. */
629 Lisp_Object minor_modes_;
630
631 /* t if "self-insertion" should overwrite; `binary' if it should also 445 /* t if "self-insertion" should overwrite; `binary' if it should also
632 overwrite newlines and tabs - for editing executables and the like. */ 446 overwrite newlines and tabs - for editing executables and the like. */
633 Lisp_Object overwrite_mode_; 447 Lisp_Object overwrite_mode_;
@@ -764,8 +578,8 @@ struct buffer
764 See `cursor-type' for other values. */ 578 See `cursor-type' for other values. */
765 Lisp_Object cursor_in_non_selected_windows_; 579 Lisp_Object cursor_in_non_selected_windows_;
766 580
767 /* No more Lisp_Object beyond this point. Except undo_list, 581 /* No more Lisp_Object beyond cursor_in_non_selected_windows_.
768 which is handled specially in Fgarbage_collect. */ 582 Except undo_list, which is handled specially in Fgarbage_collect. */
769 583
770 /* This structure holds the coordinates of the buffer contents 584 /* This structure holds the coordinates of the buffer contents
771 in ordinary buffers. In indirect buffers, this is not used. */ 585 in ordinary buffers. In indirect buffers, this is not used. */
@@ -776,9 +590,6 @@ struct buffer
776 In an indirect buffer, this is the own_text field of another buffer. */ 590 In an indirect buffer, this is the own_text field of another buffer. */
777 struct buffer_text *text; 591 struct buffer_text *text;
778 592
779 /* Next buffer, in chain of all buffers, including killed ones. */
780 struct buffer *next;
781
782 /* Char position of point in buffer. */ 593 /* Char position of point in buffer. */
783 ptrdiff_t pt; 594 ptrdiff_t pt;
784 595
@@ -815,7 +626,6 @@ struct buffer
815 for a buffer-local variable is stored in that variable's slot 626 for a buffer-local variable is stored in that variable's slot
816 in buffer_local_flags as a Lisp integer. If the index is -1, 627 in buffer_local_flags as a Lisp integer. If the index is -1,
817 this means the variable is always local in all buffers. */ 628 this means the variable is always local in all buffers. */
818#define MAX_PER_BUFFER_VARS 50
819 char local_flags[MAX_PER_BUFFER_VARS]; 629 char local_flags[MAX_PER_BUFFER_VARS];
820 630
821 /* Set to the modtime of the visited file when read or written. 631 /* Set to the modtime of the visited file when read or written.
@@ -823,8 +633,6 @@ struct buffer
823 visited file was nonexistent. modtime.tv_nsec == 633 visited file was nonexistent. modtime.tv_nsec ==
824 UNKNOWN_MODTIME_NSECS means visited file modtime unknown; 634 UNKNOWN_MODTIME_NSECS means visited file modtime unknown;
825 in no case complain about any mismatch on next save attempt. */ 635 in no case complain about any mismatch on next save attempt. */
826#define NONEXISTENT_MODTIME_NSECS (-1)
827#define UNKNOWN_MODTIME_NSECS (-2)
828 struct timespec modtime; 636 struct timespec modtime;
829 637
830 /* Size of the file when modtime was set. This is used to detect the 638 /* Size of the file when modtime was set. This is used to detect the
@@ -835,11 +643,11 @@ struct buffer
835 off_t modtime_size; 643 off_t modtime_size;
836 644
837 /* The value of text->modiff at the last auto-save. */ 645 /* The value of text->modiff at the last auto-save. */
838 EMACS_INT auto_save_modified; 646 modiff_count auto_save_modified;
839 647
840 /* The value of text->modiff at the last display error. 648 /* The value of text->modiff at the last display error.
841 Redisplay of this buffer is inhibited until it changes again. */ 649 Redisplay of this buffer is inhibited until it changes again. */
842 EMACS_INT display_error_modiff; 650 modiff_count display_error_modiff;
843 651
844 /* The time at which we detected a failure to auto-save, 652 /* The time at which we detected a failure to auto-save,
845 Or 0 if we didn't have a failure. */ 653 Or 0 if we didn't have a failure. */
@@ -878,6 +686,17 @@ struct buffer
878 /* Non-zero whenever the narrowing is changed in this buffer. */ 686 /* Non-zero whenever the narrowing is changed in this buffer. */
879 bool_bf clip_changed : 1; 687 bool_bf clip_changed : 1;
880 688
689 /* Non-zero for internal or temporary buffers that don't need to
690 run hooks kill-buffer-hook, kill-buffer-query-functions, and
691 buffer-list-update-hook. This is used in coding.c to avoid
692 slowing down en/decoding when a lot of these hooks are
693 defined, as well as by with-temp-buffer, for example. */
694 bool_bf inhibit_buffer_hooks : 1;
695
696 /* Non-zero when the buffer contains long lines and specific
697 display optimizations must be used. */
698 bool_bf long_line_optimizations_p : 1;
699
881 /* The inveral tree containing this buffer's overlays. */ 700 /* The inveral tree containing this buffer's overlays. */
882 struct interval_tree *overlays; 701 struct interval_tree *overlays;
883 702
@@ -913,7 +732,7 @@ INLINE struct buffer *
913XBUFFER (Lisp_Object a) 732XBUFFER (Lisp_Object a)
914{ 733{
915 eassert (BUFFERP (a)); 734 eassert (BUFFERP (a));
916 return XUNTAG (a, Lisp_Vectorlike); 735 return XUNTAG (a, Lisp_Vectorlike, struct buffer);
917} 736}
918 737
919/* Most code should use these functions to set Lisp fields in struct 738/* Most code should use these functions to set Lisp fields in struct
@@ -950,6 +769,16 @@ bset_display_count (struct buffer *b, Lisp_Object val)
950 b->display_count_ = val; 769 b->display_count_ = val;
951} 770}
952INLINE void 771INLINE void
772bset_left_margin_cols (struct buffer *b, Lisp_Object val)
773{
774 b->left_margin_cols_ = val;
775}
776INLINE void
777bset_right_margin_cols (struct buffer *b, Lisp_Object val)
778{
779 b->right_margin_cols_ = val;
780}
781INLINE void
953bset_display_time (struct buffer *b, Lisp_Object val) 782bset_display_time (struct buffer *b, Lisp_Object val)
954{ 783{
955 b->display_time_ = val; 784 b->display_time_ = val;
@@ -1020,60 +849,284 @@ bset_width_table (struct buffer *b, Lisp_Object val)
1020 b->width_table_ = val; 849 b->width_table_ = val;
1021} 850}
1022 851
852/* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
853 the max (resp. min) p such that
854
855 BYTE_POS_ADDR (p) - BYTE_POS_ADDR (n) == p - n */
856
857INLINE ptrdiff_t
858BUFFER_CEILING_OF (ptrdiff_t bytepos)
859{
860 return (bytepos < GPT_BYTE && GPT < ZV ? GPT_BYTE : ZV_BYTE) - 1;
861}
862
863INLINE ptrdiff_t
864BUFFER_FLOOR_OF (ptrdiff_t bytepos)
865{
866 return BEGV <= GPT && GPT_BYTE <= bytepos ? GPT_BYTE : BEGV_BYTE;
867}
868
869/* The BUF_BEGV[_BYTE], BUF_ZV[_BYTE], and BUF_PT[_BYTE] functions cannot
870 be used for assignment; use SET_BUF_* functions below for that. */
871
872/* Position of beginning of accessible range of buffer. */
873INLINE ptrdiff_t
874BUF_BEGV (struct buffer *buf)
875{
876 return (buf == current_buffer ? BEGV
877 : NILP (BVAR (buf, begv_marker)) ? buf->begv
878 : marker_position (BVAR (buf, begv_marker)));
879}
880
881INLINE ptrdiff_t
882BUF_BEGV_BYTE (struct buffer *buf)
883{
884 return (buf == current_buffer ? BEGV_BYTE
885 : NILP (BVAR (buf, begv_marker)) ? buf->begv_byte
886 : marker_byte_position (BVAR (buf, begv_marker)));
887}
888
889/* Position of point in buffer. */
890INLINE ptrdiff_t
891BUF_PT (struct buffer *buf)
892{
893 return (buf == current_buffer ? PT
894 : NILP (BVAR (buf, pt_marker)) ? buf->pt
895 : marker_position (BVAR (buf, pt_marker)));
896}
897
898INLINE ptrdiff_t
899BUF_PT_BYTE (struct buffer *buf)
900{
901 return (buf == current_buffer ? PT_BYTE
902 : NILP (BVAR (buf, pt_marker)) ? buf->pt_byte
903 : marker_byte_position (BVAR (buf, pt_marker)));
904}
905
906/* Position of end of accessible range of buffer. */
907INLINE ptrdiff_t
908BUF_ZV (struct buffer *buf)
909{
910 return (buf == current_buffer ? ZV
911 : NILP (BVAR (buf, zv_marker)) ? buf->zv
912 : marker_position (BVAR (buf, zv_marker)));
913}
914
915INLINE ptrdiff_t
916BUF_ZV_BYTE (struct buffer *buf)
917{
918 return (buf == current_buffer ? ZV_BYTE
919 : NILP (BVAR (buf, zv_marker)) ? buf->zv_byte
920 : marker_byte_position (BVAR (buf, zv_marker)));
921}
922
923/* Similar functions to operate on a specified buffer. */
924
925/* Position of beginning of buffer. */
926INLINE ptrdiff_t
927BUF_BEG (struct buffer *buf)
928{
929 return BEG;
930}
931
932INLINE ptrdiff_t
933BUF_BEG_BYTE (struct buffer *buf)
934{
935 return BEG_BYTE;
936}
937
938/* Address of beginning of gap of buffer. */
939INLINE unsigned char *
940BUF_GPT_ADDR (struct buffer *buf)
941{
942 return buf->text->beg + buf->text->gpt_byte - BEG_BYTE;
943}
944
945/* Address of end of buffer. */
946INLINE unsigned char *
947BUF_Z_ADDR (struct buffer *buf)
948{
949 return buf->text->beg + buf->text->gap_size + buf->text->z_byte - BEG_BYTE;
950}
951
952/* Address of end of gap in buffer. */
953INLINE unsigned char *
954BUF_GAP_END_ADDR (struct buffer *buf)
955{
956 return buf->text->beg + buf->text->gpt_byte + buf->text->gap_size - BEG_BYTE;
957}
958
959/* Compute how many characters at the top and bottom of BUF are
960 unchanged when the range START..END is modified. This computation
961 must be done each time BUF is modified. */
962
963INLINE void
964BUF_COMPUTE_UNCHANGED (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
965{
966 if (BUF_UNCHANGED_MODIFIED (buf) == BUF_MODIFF (buf)
967 && (BUF_OVERLAY_UNCHANGED_MODIFIED (buf)
968 == BUF_OVERLAY_MODIFF (buf)))
969 {
970 buf->text->beg_unchanged = start - BUF_BEG (buf);
971 buf->text->end_unchanged = BUF_Z (buf) - (end);
972 }
973 else
974 {
975 if (BUF_Z (buf) - end < BUF_END_UNCHANGED (buf))
976 buf->text->end_unchanged = BUF_Z (buf) - end;
977 if (start - BUF_BEG (buf) < BUF_BEG_UNCHANGED (buf))
978 buf->text->beg_unchanged = start - BUF_BEG (buf);
979 }
980}
981
982/* Functions for setting the BEGV, ZV or PT of a given buffer.
983
984 The ..._BOTH functions take both a charpos and a bytepos,
985 which must correspond to each other.
986
987 The functions without ..._BOTH take just a charpos,
988 and compute the bytepos from it. */
989
990INLINE void
991SET_BUF_BEGV (struct buffer *buf, ptrdiff_t charpos)
992{
993 buf->begv_byte = buf_charpos_to_bytepos (buf, charpos);
994 buf->begv = charpos;
995}
996
997INLINE void
998SET_BUF_ZV (struct buffer *buf, ptrdiff_t charpos)
999{
1000 buf->zv_byte = buf_charpos_to_bytepos (buf, charpos);
1001 buf->zv = charpos;
1002}
1003
1004INLINE void
1005SET_BUF_BEGV_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
1006{
1007 buf->begv = charpos;
1008 buf->begv_byte = byte;
1009}
1010
1011INLINE void
1012SET_BUF_ZV_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
1013{
1014 buf->zv = charpos;
1015 buf->zv_byte = byte;
1016}
1017
1018INLINE void
1019SET_BUF_PT_BOTH (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t byte)
1020{
1021 buf->pt = charpos;
1022 buf->pt_byte = byte;
1023}
1024
1025/* Functions to access a character or byte in the current buffer,
1026 or convert between a byte position and an address.
1027 These functions do not check that the position is in range. */
1028
1029/* See the important WARNING above about using the 'char *' pointers
1030 returned by these functions. */
1031
1032/* Return the address of byte position N in current buffer. */
1033
1034INLINE unsigned char *
1035BYTE_POS_ADDR (ptrdiff_t n)
1036{
1037 return (n < GPT_BYTE ? 0 : GAP_SIZE) + n + BEG_ADDR - BEG_BYTE;
1038}
1039
1040/* Return the address of char position N. */
1041
1042INLINE unsigned char *
1043CHAR_POS_ADDR (ptrdiff_t n)
1044{
1045 return ((n < GPT ? 0 : GAP_SIZE)
1046 + buf_charpos_to_bytepos (current_buffer, n)
1047 + BEG_ADDR - BEG_BYTE);
1048}
1049
1050/* Convert a character position to a byte position. */
1051
1052INLINE ptrdiff_t
1053CHAR_TO_BYTE (ptrdiff_t charpos)
1054{
1055 return buf_charpos_to_bytepos (current_buffer, charpos);
1056}
1057
1058/* Convert a byte position to a character position. */
1059
1060INLINE ptrdiff_t
1061BYTE_TO_CHAR (ptrdiff_t bytepos)
1062{
1063 return buf_bytepos_to_charpos (current_buffer, bytepos);
1064}
1065
1066/* Convert PTR, the address of a byte in the buffer, into a byte position. */
1067
1068INLINE ptrdiff_t
1069PTR_BYTE_POS (unsigned char const *ptr)
1070{
1071 ptrdiff_t byte = ptr - current_buffer->text->beg;
1072 return byte - (byte <= GPT_BYTE - BEG_BYTE ? 0 : GAP_SIZE) + BEG_BYTE;
1073}
1074
1023/* Number of Lisp_Objects at the beginning of struct buffer. 1075/* Number of Lisp_Objects at the beginning of struct buffer.
1024 If you add, remove, or reorder Lisp_Objects within buffer 1076 If you add, remove, or reorder Lisp_Objects within buffer
1025 structure, make sure that this is still correct. */ 1077 structure, make sure that this is still correct. */
1026 1078
1027#define BUFFER_LISP_SIZE \ 1079enum { BUFFER_LISP_SIZE = PSEUDOVECSIZE (struct buffer,
1028 ((offsetof (struct buffer, own_text) - header_size) / word_size) 1080 cursor_in_non_selected_windows_) };
1029 1081
1030/* Size of the struct buffer part beyond leading Lisp_Objects, in word_size 1082/* Allocated size of the struct buffer part beyond leading
1031 units. Rounding is needed for --with-wide-int configuration. */ 1083 Lisp_Objects, in word_size units. */
1032 1084
1033#define BUFFER_REST_SIZE \ 1085enum { BUFFER_REST_SIZE = VECSIZE (struct buffer) - BUFFER_LISP_SIZE };
1034 ((((sizeof (struct buffer) - offsetof (struct buffer, own_text)) \
1035 + (word_size - 1)) & ~(word_size - 1)) / word_size)
1036 1086
1037/* Initialize the pseudovector header of buffer object. BUFFER_LISP_SIZE 1087/* Initialize the pseudovector header of buffer object. BUFFER_LISP_SIZE
1038 is required for GC, but BUFFER_REST_SIZE is set up just to be consistent 1088 is required for GC, but BUFFER_REST_SIZE is set up just to be consistent
1039 with other pseudovectors. */ 1089 with other pseudovectors. */
1040 1090
1041#define BUFFER_PVEC_INIT(b) \ 1091INLINE void
1042 XSETPVECTYPESIZE (b, PVEC_BUFFER, BUFFER_LISP_SIZE, BUFFER_REST_SIZE) 1092BUFFER_PVEC_INIT (struct buffer *b)
1093{
1094 XSETPVECTYPESIZE (b, PVEC_BUFFER, BUFFER_LISP_SIZE, BUFFER_REST_SIZE);
1095}
1043 1096
1044/* Convenient check whether buffer B is live. */ 1097/* Convenient check whether buffer B is live. */
1045 1098
1046#define BUFFER_LIVE_P(b) (!NILP (BVAR (b, name))) 1099INLINE bool
1100BUFFER_LIVE_P (struct buffer *b)
1101{
1102 return !NILP (BVAR (b, name));
1103}
1047 1104
1048/* Convenient check whether buffer B is hidden (i.e. its name 1105/* Convenient check whether buffer B is hidden (i.e. its name
1049 starts with a space). Caller must ensure that B is live. */ 1106 starts with a space). Caller must ensure that B is live. */
1050 1107
1051#define BUFFER_HIDDEN_P(b) (SREF (BVAR (b, name), 0) == ' ') 1108INLINE bool
1109BUFFER_HIDDEN_P (struct buffer *b)
1110{
1111 return SREF (BVAR (b, name), 0) == ' ';
1112}
1052 1113
1053/* Verify indirection counters. */ 1114/* Verify indirection counters. */
1054 1115
1055#define BUFFER_CHECK_INDIRECTION(b) \ 1116INLINE void
1056 do { \ 1117BUFFER_CHECK_INDIRECTION (struct buffer *b)
1057 if (BUFFER_LIVE_P (b)) \ 1118{
1058 { \ 1119 if (BUFFER_LIVE_P (b))
1059 if (b->base_buffer) \ 1120 {
1060 { \ 1121 if (b->base_buffer)
1061 eassert (b->indirections == -1); \ 1122 {
1062 eassert (b->base_buffer->indirections > 0); \ 1123 eassert (b->indirections == -1);
1063 } \ 1124 eassert (b->base_buffer->indirections > 0);
1064 else \ 1125 }
1065 eassert (b->indirections >= 0); \ 1126 else
1066 } \ 1127 eassert (b->indirections >= 0);
1067 } while (false) 1128 }
1068 1129}
1069/* Chain of all buffers, including killed ones. */
1070
1071extern struct buffer *all_buffers;
1072
1073/* Used to iterate over the chain above. */
1074
1075#define FOR_EACH_BUFFER(b) \
1076 for ((b) = all_buffers; (b); (b) = (b)->next)
1077 1130
1078/* This structure holds the default values of the buffer-local variables 1131/* This structure holds the default values of the buffer-local variables
1079 that have special slots in each buffer. 1132 that have special slots in each buffer.
@@ -1105,7 +1158,15 @@ extern struct buffer buffer_local_flags;
1105 that don't have such names. */ 1158 that don't have such names. */
1106 1159
1107extern struct buffer buffer_local_symbols; 1160extern struct buffer buffer_local_symbols;
1161
1162/* verify_interval_modification saves insertion hooks here
1163 to be run later by report_interval_modification. */
1164extern Lisp_Object interval_insert_behind_hooks;
1165extern Lisp_Object interval_insert_in_front_hooks;
1166
1108 1167
1168extern EMACS_INT fix_position (Lisp_Object);
1169#define CHECK_FIXNUM_COERCE_MARKER(x) ((x) = make_fixnum (fix_position (x)))
1109extern void delete_all_overlays (struct buffer *); 1170extern void delete_all_overlays (struct buffer *);
1110extern void reset_buffer (struct buffer *); 1171extern void reset_buffer (struct buffer *);
1111extern void compact_buffer (struct buffer *); 1172extern void compact_buffer (struct buffer *);
@@ -1164,7 +1225,9 @@ record_unwind_current_buffer (void)
1164 1225
1165/* Get overlays at POSN into array OVERLAYS with NOVERLAYS elements. 1226/* Get overlays at POSN into array OVERLAYS with NOVERLAYS elements.
1166 If NEXTP is non-NULL, return next overlay there. 1227 If NEXTP is non-NULL, return next overlay there.
1167 See overlay_at arg CHANGE_REQ for meaning of CHRQ arg. */ 1228 See overlay_at arg CHANGE_REQ for meaning of CHRQ arg.
1229 This macro might evaluate its args multiple times,
1230 and it treat some args as lvalues. */
1168 1231
1169#define GET_OVERLAYS_AT(posn, overlays, noverlays, next) \ 1232#define GET_OVERLAYS_AT(posn, overlays, noverlays, next) \
1170 do { \ 1233 do { \
@@ -1213,6 +1276,10 @@ buffer_has_overlays (void)
1213 return current_buffer->overlays 1276 return current_buffer->overlays
1214 && (interval_tree_size (current_buffer->overlays) > 0); 1277 && (interval_tree_size (current_buffer->overlays) > 0);
1215} 1278}
1279
1280/* Functions for accessing a character or byte,
1281 or converting between byte positions and addresses,
1282 in a specified buffer. */
1216 1283
1217/* Return character code of multi-byte form at byte position POS. If POS 1284/* Return character code of multi-byte form at byte position POS. If POS
1218 doesn't point the head of valid multi-byte form, only the byte at 1285 doesn't point the head of valid multi-byte form, only the byte at
@@ -1238,6 +1305,80 @@ BUF_FETCH_MULTIBYTE_CHAR (struct buffer *buf, ptrdiff_t pos)
1238 return STRING_CHAR (p); 1305 return STRING_CHAR (p);
1239} 1306}
1240 1307
1308/* Return character at byte position POS.
1309 If the current buffer is unibyte and the character is not ASCII,
1310 make the returning character multibyte. */
1311
1312INLINE int
1313FETCH_CHAR_AS_MULTIBYTE (ptrdiff_t pos)
1314{
1315 return (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1316 ? FETCH_MULTIBYTE_CHAR (pos)
1317 : UNIBYTE_TO_CHAR (FETCH_BYTE (pos)));
1318}
1319
1320/* Return character at byte position POS.
1321 See the caveat WARNING for FETCH_MULTIBYTE_CHAR above. */
1322
1323INLINE int
1324FETCH_CHAR (ptrdiff_t pos)
1325{
1326 return (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1327 ? FETCH_MULTIBYTE_CHAR (pos)
1328 : FETCH_BYTE (pos));
1329}
1330
1331/* Return the address of character at byte position POS in buffer BUF.
1332 Note that both arguments can be computed more than once. */
1333
1334INLINE unsigned char *
1335BUF_BYTE_ADDRESS (struct buffer *buf, ptrdiff_t pos)
1336{
1337 return (buf->text->beg + pos - BEG_BYTE
1338 + (pos < buf->text->gpt_byte ? 0 : buf->text->gap_size));
1339}
1340
1341/* Return the address of character at char position POS in buffer BUF.
1342 Note that both arguments can be computed more than once. */
1343
1344INLINE unsigned char *
1345BUF_CHAR_ADDRESS (struct buffer *buf, ptrdiff_t pos)
1346{
1347 return (buf->text->beg + buf_charpos_to_bytepos (buf, pos) - BEG_BYTE
1348 + (pos < buf->text->gpt ? 0 : buf->text->gap_size));
1349}
1350
1351/* Convert PTR, the address of a char in buffer BUF,
1352 into a character position. */
1353
1354INLINE ptrdiff_t
1355BUF_PTR_BYTE_POS (struct buffer *buf, unsigned char *ptr)
1356{
1357 ptrdiff_t byte = ptr - buf->text->beg;
1358 return (byte - (byte <= BUF_GPT_BYTE (buf) - BEG_BYTE ? 0 : BUF_GAP_SIZE (buf))
1359 + BEG_BYTE);
1360}
1361
1362/* Return the byte at byte position N in buffer BUF. */
1363
1364INLINE unsigned char
1365BUF_FETCH_BYTE (struct buffer *buf, ptrdiff_t n)
1366{
1367 return *BUF_BYTE_ADDRESS (buf, n);
1368}
1369
1370/* Return character at byte position POS in buffer BUF. If BUF is
1371 unibyte and the character is not ASCII, make the returning
1372 character multibyte. */
1373
1374INLINE int
1375BUF_FETCH_CHAR_AS_MULTIBYTE (struct buffer *buf, ptrdiff_t pos)
1376{
1377 return (! NILP (BVAR (buf, enable_multibyte_characters))
1378 ? BUF_FETCH_MULTIBYTE_CHAR (buf, pos)
1379 : UNIBYTE_TO_CHAR (BUF_FETCH_BYTE (buf, pos)));
1380}
1381
1241/* Return number of windows showing B. */ 1382/* Return number of windows showing B. */
1242 1383
1243INLINE int 1384INLINE int
@@ -1348,37 +1489,57 @@ buffer_overlay_iter_narrow (struct buffer *b, ptrdiff_t begin, ptrdiff_t end)
1348/* Return the start of OV in its buffer, or -1 if OV is not associated 1489/* Return the start of OV in its buffer, or -1 if OV is not associated
1349 with any buffer. */ 1490 with any buffer. */
1350 1491
1351#define OVERLAY_START(OV) (overlay_start (XOVERLAY (OV))) 1492INLINE ptrdiff_t
1493OVERLAY_START (Lisp_Object ov)
1494{
1495 return overlay_start (XOVERLAY (ov));
1496}
1352 1497
1353/* Return the end of OV in its buffer, or -1. */ 1498/* Return the end of OV in its buffer, or -1. */
1354 1499
1355#define OVERLAY_END(OV) (overlay_end (XOVERLAY (OV))) 1500INLINE ptrdiff_t
1501OVERLAY_END (Lisp_Object ov)
1502{
1503 return overlay_end (XOVERLAY (ov));
1504}
1356 1505
1357/* Return the plist of overlay OV. */ 1506/* Return the plist of overlay OV. */
1358 1507
1359#define OVERLAY_PLIST(OV) (XOVERLAY (OV)->plist) 1508INLINE Lisp_Object
1509OVERLAY_PLIST (Lisp_Object ov)
1510{
1511 return XOVERLAY (ov)->plist;
1512}
1360 1513
1361/* Return the buffer of overlay OV. */ 1514/* Return the buffer of overlay OV. */
1362 1515
1363#define OVERLAY_BUFFER(OV) (XOVERLAY (OV)->buffer) 1516INLINE struct buffer *
1517OVERLAY_BUFFER (Lisp_Object ov)
1518{
1519 return XOVERLAY (ov)->buffer;
1520}
1364 1521
1365/* Return true, if OV's rear-advance is set. */ 1522/* Return true, if OV's rear-advance is set. */
1366 1523
1367#define OVERLAY_REAR_ADVANCE_P(OV) (XOVERLAY (OV)->interval->rear_advance) 1524INLINE bool
1525OVERLAY_REAR_ADVANCE_P (Lisp_Object ov)
1526{
1527 return XOVERLAY (ov)->interval->rear_advance;
1528}
1368 1529
1369/* Return true, if OV's front-advance is set. */ 1530/* Return true, if OV's front-advance is set. */
1370 1531
1371#define OVERLAY_FRONT_ADVANCE_P(OV) (XOVERLAY (OV)->interval->front_advance) 1532INLINE bool
1533OVERLAY_FRONT_ADVANCE_P (Lisp_Object ov)
1534{
1535 return XOVERLAY (ov)->interval->front_advance;
1536}
1372 1537
1373 1538
1374/*********************************************************************** 1539/***********************************************************************
1375 Buffer-local Variables 1540 Buffer-local Variables
1376 ***********************************************************************/ 1541 ***********************************************************************/
1377 1542
1378/* Number of per-buffer variables used. */
1379
1380extern int last_per_buffer_idx;
1381
1382/* Return the offset in bytes of member VAR of struct buffer 1543/* Return the offset in bytes of member VAR of struct buffer
1383 from the start of a buffer structure. */ 1544 from the start of a buffer structure. */
1384 1545
@@ -1403,23 +1564,27 @@ extern int last_per_buffer_idx;
1403#define PER_BUFFER_VAR_IDX(VAR) \ 1564#define PER_BUFFER_VAR_IDX(VAR) \
1404 PER_BUFFER_IDX (PER_BUFFER_VAR_OFFSET (VAR)) 1565 PER_BUFFER_IDX (PER_BUFFER_VAR_OFFSET (VAR))
1405 1566
1567extern bool valid_per_buffer_idx (int);
1568
1406/* Value is true if the variable with index IDX has a local value 1569/* Value is true if the variable with index IDX has a local value
1407 in buffer B. */ 1570 in buffer B. */
1408 1571
1409#define PER_BUFFER_VALUE_P(B, IDX) \ 1572INLINE bool
1410 (((IDX) < 0 || IDX >= last_per_buffer_idx) \ 1573PER_BUFFER_VALUE_P (struct buffer *b, int idx)
1411 ? (emacs_abort (), false) \ 1574{
1412 : ((B)->local_flags[IDX] != 0)) 1575 eassert (valid_per_buffer_idx (idx));
1576 return b->local_flags[idx];
1577}
1413 1578
1414/* Set whether per-buffer variable with index IDX has a buffer-local 1579/* Set whether per-buffer variable with index IDX has a buffer-local
1415 value in buffer B. VAL zero means it hasn't. */ 1580 value in buffer B. VAL zero means it hasn't. */
1416 1581
1417#define SET_PER_BUFFER_VALUE_P(B, IDX, VAL) \ 1582INLINE void
1418 do { \ 1583SET_PER_BUFFER_VALUE_P (struct buffer *b, int idx, bool val)
1419 if ((IDX) < 0 || (IDX) >= last_per_buffer_idx) \ 1584{
1420 emacs_abort (); \ 1585 eassert (valid_per_buffer_idx (idx));
1421 (B)->local_flags[IDX] = (VAL); \ 1586 b->local_flags[idx] = val;
1422 } while (false) 1587}
1423 1588
1424/* Return the index value of the per-buffer variable at offset OFFSET 1589/* Return the index value of the per-buffer variable at offset OFFSET
1425 in the buffer structure. 1590 in the buffer structure.
@@ -1439,11 +1604,13 @@ extern int last_per_buffer_idx;
1439 new buffer. 1604 new buffer.
1440 1605
1441 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is 1606 If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
1442 zero, that is a bug */ 1607 zero, that is a bug. */
1443 1608
1444 1609INLINE int
1445#define PER_BUFFER_IDX(OFFSET) \ 1610PER_BUFFER_IDX (ptrdiff_t offset)
1446 XINT (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_flags)) 1611{
1612 return XFIXNUM (*(Lisp_Object *) (offset + (char *) &buffer_local_flags));
1613}
1447 1614
1448/* Functions to get and set default value of the per-buffer 1615/* Functions to get and set default value of the per-buffer
1449 variable at offset OFFSET in the buffer structure. */ 1616 variable at offset OFFSET in the buffer structure. */
@@ -1481,7 +1648,7 @@ downcase (int c)
1481{ 1648{
1482 Lisp_Object downcase_table = BVAR (current_buffer, downcase_table); 1649 Lisp_Object downcase_table = BVAR (current_buffer, downcase_table);
1483 Lisp_Object down = CHAR_TABLE_REF (downcase_table, c); 1650 Lisp_Object down = CHAR_TABLE_REF (downcase_table, c);
1484 return NATNUMP (down) ? XFASTINT (down) : c; 1651 return FIXNATP (down) ? XFIXNAT (down) : c;
1485} 1652}
1486 1653
1487/* Upcase a character C, or make no change if that cannot be done. */ 1654/* Upcase a character C, or make no change if that cannot be done. */
@@ -1490,7 +1657,7 @@ upcase (int c)
1490{ 1657{
1491 Lisp_Object upcase_table = BVAR (current_buffer, upcase_table); 1658 Lisp_Object upcase_table = BVAR (current_buffer, upcase_table);
1492 Lisp_Object up = CHAR_TABLE_REF (upcase_table, c); 1659 Lisp_Object up = CHAR_TABLE_REF (upcase_table, c);
1493 return NATNUMP (up) ? XFASTINT (up) : c; 1660 return FIXNATP (up) ? XFIXNAT (up) : c;
1494} 1661}
1495 1662
1496/* True if C is upper case. */ 1663/* True if C is upper case. */
@@ -1507,6 +1674,146 @@ lowercasep (int c)
1507 return !uppercasep (c) && upcase (c) != c; 1674 return !uppercasep (c) && upcase (c) != c;
1508} 1675}
1509 1676
1677/* Return a non-outlandish value for the tab width. */
1678
1679INLINE int
1680sanitize_tab_width (Lisp_Object width)
1681{
1682 return (FIXNUMP (width) && 0 < XFIXNUM (width) && XFIXNUM (width) <= 1000
1683 ? XFIXNUM (width) : 8);
1684}
1685
1686INLINE int
1687SANE_TAB_WIDTH (struct buffer *buf)
1688{
1689 return sanitize_tab_width (BVAR (buf, tab_width));
1690}
1691
1692/* Return a non-outlandish value for a character width. */
1693
1694INLINE int
1695sanitize_char_width (EMACS_INT width)
1696{
1697 return 0 <= width && width <= 1000 ? width : 1000;
1698}
1699
1700/* Return the width of character C. The width is measured by how many
1701 columns C will occupy on the screen when displayed in the current
1702 buffer. The name CHARACTER_WIDTH avoids a collision with <limits.h>
1703 CHAR_WIDTH. */
1704
1705INLINE int
1706CHARACTER_WIDTH (int c)
1707{
1708 return (0x20 <= c && c < 0x7f ? 1
1709 : 0x7f < c ? (sanitize_char_width
1710 (XFIXNUM (CHAR_TABLE_REF (Vchar_width_table, c))))
1711 : c == '\t' ? SANE_TAB_WIDTH (current_buffer)
1712 : c == '\n' ? 0
1713 : !NILP (BVAR (current_buffer, ctl_arrow)) ? 2 : 4);
1714}
1715
1716
1717/* Like fetch_string_char_advance, but fetch character from the current
1718 buffer. */
1719
1720INLINE int
1721fetch_char_advance (ptrdiff_t *charidx, ptrdiff_t *byteidx)
1722{
1723 int output;
1724 ptrdiff_t c = *charidx, b = *byteidx;
1725 c++;
1726 unsigned char *chp = BYTE_POS_ADDR (b);
1727 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1728 {
1729 int chlen;
1730 output = string_char_and_length (chp, &chlen);
1731 b += chlen;
1732 }
1733 else
1734 {
1735 output = *chp;
1736 b++;
1737 }
1738 *charidx = c;
1739 *byteidx = b;
1740 return output;
1741}
1742
1743
1744/* Like fetch_char_advance, but assumes the current buffer is multibyte. */
1745
1746INLINE int
1747fetch_char_advance_no_check (ptrdiff_t *charidx, ptrdiff_t *byteidx)
1748{
1749 int output;
1750 ptrdiff_t c = *charidx, b = *byteidx;
1751 c++;
1752 unsigned char *chp = BYTE_POS_ADDR (b);
1753 int chlen;
1754 output = string_char_and_length (chp, &chlen);
1755 b += chlen;
1756 *charidx = c;
1757 *byteidx = b;
1758 return output;
1759}
1760
1761/* Return the number of bytes in the multibyte character in BUF
1762 that starts at position POS_BYTE. This relies on the fact that
1763 *GPT_ADDR and *Z_ADDR are always accessible and the values are
1764 '\0'. No range checking of POS_BYTE. */
1765
1766INLINE int
1767buf_next_char_len (struct buffer *buf, ptrdiff_t pos_byte)
1768{
1769 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte);
1770 return BYTES_BY_CHAR_HEAD (*chp);
1771}
1772
1773INLINE int
1774next_char_len (ptrdiff_t pos_byte)
1775{
1776 return buf_next_char_len (current_buffer, pos_byte);
1777}
1778
1779/* Return the number of bytes in the multibyte character in BUF just
1780 before POS_BYTE. No range checking of POS_BYTE. */
1781
1782INLINE int
1783buf_prev_char_len (struct buffer *buf, ptrdiff_t pos_byte)
1784{
1785 unsigned char *chp
1786 = (BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE
1787 + (pos_byte <= BUF_GPT_BYTE (buf) ? 0 : BUF_GAP_SIZE (buf)));
1788 return raw_prev_char_len (chp);
1789}
1790
1791INLINE int
1792prev_char_len (ptrdiff_t pos_byte)
1793{
1794 return buf_prev_char_len (current_buffer, pos_byte);
1795}
1796
1797/* Increment both *CHARPOS and *BYTEPOS, each in the appropriate way. */
1798
1799INLINE void
1800inc_both (ptrdiff_t *charpos, ptrdiff_t *bytepos)
1801{
1802 (*charpos)++;
1803 (*bytepos) += (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1804 ? next_char_len (*bytepos) : 1);
1805}
1806
1807/* Decrement both *CHARPOS and *BYTEPOS, each in the appropriate way. */
1808
1809INLINE void
1810dec_both (ptrdiff_t *charpos, ptrdiff_t *bytepos)
1811{
1812 (*charpos)--;
1813 (*bytepos) -= (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1814 ? prev_char_len (*bytepos) : 1);
1815}
1816
1510INLINE_HEADER_END 1817INLINE_HEADER_END
1511 1818
1512int compare_overlays (const void *v1, const void *v2); 1819int compare_overlays (const void *v1, const void *v2);