buffer.h 45.8 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Header file for the buffer manipulation primitives.
Glenn Morris's avatar
Glenn Morris committed
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software Foundation,
4
Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20

21 22 23
#ifndef EMACS_BUFFER_H
#define EMACS_BUFFER_H

24 25
#include <sys/types.h>
#include <time.h>
Jim Blandy's avatar
Jim Blandy committed
26

27 28 29
#include "character.h"
#include "lisp.h"

30 31
INLINE_HEADER_BEGIN

32
/* Accessing the parameters of the current buffer.  */
33

34 35
/* These macros come in pairs, one for the char position
   and one for the byte position.  */
Jim Blandy's avatar
Jim Blandy committed
36

37
/* Position of beginning of buffer.  */
Jim Blandy's avatar
Jim Blandy committed
38
#define BEG (1)
39
#define BEG_BYTE (BEG)
Jim Blandy's avatar
Jim Blandy committed
40

41
/* Position of beginning of accessible range of buffer.  */
42
#define BEGV (current_buffer->begv)
43
#define BEGV_BYTE (current_buffer->begv_byte)
Jim Blandy's avatar
Jim Blandy committed
44

45
/* Position of point in buffer.  The "+ 0" makes this
46
   not an l-value, so you can't assign to it.  Use SET_PT instead.  */
47
#define PT (current_buffer->pt + 0)
48
#define PT_BYTE (current_buffer->pt_byte + 0)
Jim Blandy's avatar
Jim Blandy committed
49

50
/* Position of gap in buffer.  */
51
#define GPT (current_buffer->text->gpt)
52
#define GPT_BYTE (current_buffer->text->gpt_byte)
Jim Blandy's avatar
Jim Blandy committed
53

54
/* Position of end of accessible range of buffer.  */
55
#define ZV (current_buffer->zv)
56
#define ZV_BYTE (current_buffer->zv_byte)
Jim Blandy's avatar
Jim Blandy committed
57

58
/* Position of end of buffer.  */
59
#define Z (current_buffer->text->z)
60
#define Z_BYTE (current_buffer->text->z_byte)
Jim Blandy's avatar
Jim Blandy committed
61

62
/* Macros for the addresses of places in the buffer.  */
Jim Blandy's avatar
Jim Blandy committed
63

64
/* Address of beginning of buffer.  */
65
#define BEG_ADDR (current_buffer->text->beg)
Jim Blandy's avatar
Jim Blandy committed
66

67
/* Address of beginning of accessible range of buffer.  */
68
#define BEGV_ADDR (BYTE_POS_ADDR (current_buffer->begv_byte))
Jim Blandy's avatar
Jim Blandy committed
69

70
/* Address of point in buffer.  */
71
#define PT_ADDR (BYTE_POS_ADDR (current_buffer->pt_byte))
Jim Blandy's avatar
Jim Blandy committed
72

73
/* Address of beginning of gap in buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
74
#define GPT_ADDR (current_buffer->text->beg + current_buffer->text->gpt_byte - BEG_BYTE)
Jim Blandy's avatar
Jim Blandy committed
75 76

/* Address of end of gap in buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
77
#define GAP_END_ADDR (current_buffer->text->beg + current_buffer->text->gpt_byte + current_buffer->text->gap_size - BEG_BYTE)
Jim Blandy's avatar
Jim Blandy committed
78

79
/* Address of end of accessible range of buffer.  */
80
#define ZV_ADDR (BYTE_POS_ADDR (current_buffer->zv_byte))
81

82
/* Address of end of buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
83
#define Z_ADDR (current_buffer->text->beg + current_buffer->text->gap_size + current_buffer->text->z_byte - BEG_BYTE)
Jim Blandy's avatar
Jim Blandy committed
84 85

/* Size of gap.  */
86
#define GAP_SIZE (current_buffer->text->gap_size)
Jim Blandy's avatar
Jim Blandy committed
87

88 89 90
/* Modification count.  */
#define MODIFF (current_buffer->text->modiff)

91 92 93
/* Character modification count.  */
#define CHARS_MODIFF (current_buffer->text->chars_modiff)

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* Overlay modification count.  */
#define OVERLAY_MODIFF (current_buffer->text->overlay_modiff)

/* Modification count as of last visit or save.  */
#define SAVE_MODIFF (current_buffer->text->save_modiff)

/* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
   the max (resp. min) p such that

   BYTE_POS_ADDR (p) - BYTE_POS_ADDR (n) == p - n       */

#define BUFFER_CEILING_OF(BYTEPOS) \
  (((BYTEPOS) < GPT_BYTE && GPT < ZV ? GPT_BYTE : ZV_BYTE) - 1)
#define BUFFER_FLOOR_OF(BYTEPOS) \
  (BEGV <= GPT && GPT_BYTE <= (BYTEPOS) ? GPT_BYTE : BEGV_BYTE)

/* Similar macros to operate on a specified buffer.
Jim Blandy's avatar
Jim Blandy committed
111 112
   Note that many of these evaluate the buffer argument more than once.  */

113
/* Position of beginning of buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
114 115
#define BUF_BEG(buf) (BEG)
#define BUF_BEG_BYTE(buf) (BEG_BYTE)
Jim Blandy's avatar
Jim Blandy committed
116

117 118
/* The BUF_BEGV[_BYTE], BUF_ZV[_BYTE], and BUF_PT[_BYTE] macros cannot
   be used for assignment; use SET_BUF_* macros below for that.  */
119

120
/* Position of beginning of accessible range of buffer.  */
121 122 123 124 125 126 127 128 129
#define BUF_BEGV(buf)					\
   (buf == current_buffer ? BEGV			\
    : NILP (BVAR (buf, begv_marker)) ? buf->begv	\
    : marker_position (BVAR (buf, begv_marker)))

#define BUF_BEGV_BYTE(buf)				\
   (buf == current_buffer ? BEGV_BYTE			\
    : NILP (BVAR (buf, begv_marker)) ? buf->begv_byte	\
    : marker_byte_position (BVAR (buf, begv_marker)))
Jim Blandy's avatar
Jim Blandy committed
130

131
/* Position of point in buffer.  */
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#define BUF_PT(buf)					\
   (buf == current_buffer ? PT				\
    : NILP (BVAR (buf, pt_marker)) ? buf->pt		\
    : marker_position (BVAR (buf, pt_marker)))

#define BUF_PT_BYTE(buf)				\
   (buf == current_buffer ? PT_BYTE			\
    : NILP (BVAR (buf, pt_marker)) ? buf->pt_byte	\
    : marker_byte_position (BVAR (buf, pt_marker)))

/* Position of end of accessible range of buffer.  */
#define BUF_ZV(buf)					\
   (buf == current_buffer ? ZV				\
    : NILP (BVAR (buf, zv_marker)) ? buf->zv		\
    : marker_position (BVAR (buf, zv_marker)))

#define BUF_ZV_BYTE(buf)				\
   (buf == current_buffer ? ZV_BYTE			\
    : NILP (BVAR (buf, zv_marker)) ? buf->zv_byte	\
    : marker_byte_position (BVAR (buf, zv_marker)))
Jim Blandy's avatar
Jim Blandy committed
152

153
/* Position of gap in buffer.  */
154
#define BUF_GPT(buf) ((buf)->text->gpt)
155
#define BUF_GPT_BYTE(buf) ((buf)->text->gpt_byte)
Jim Blandy's avatar
Jim Blandy committed
156

157
/* Position of end of buffer.  */
158
#define BUF_Z(buf) ((buf)->text->z)
159 160 161 162 163 164
#define BUF_Z_BYTE(buf) ((buf)->text->z_byte)

/* Address of beginning of buffer.  */
#define BUF_BEG_ADDR(buf) ((buf)->text->beg)

/* Address of beginning of gap of buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
165
#define BUF_GPT_ADDR(buf) ((buf)->text->beg + (buf)->text->gpt_byte - BEG_BYTE)
166 167

/* Address of end of buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
168
#define BUF_Z_ADDR(buf) ((buf)->text->beg + (buf)->text->gap_size + (buf)->text->z_byte - BEG_BYTE)
169 170

/* Address of end of gap in buffer.  */
Stefan Monnier's avatar
Stefan Monnier committed
171
#define BUF_GAP_END_ADDR(buf) ((buf)->text->beg + (buf)->text->gpt_byte + (buf)->text->gap_size - BEG_BYTE)
Jim Blandy's avatar
Jim Blandy committed
172

173 174 175
/* Size of gap.  */
#define BUF_GAP_SIZE(buf) ((buf)->text->gap_size)

Jim Blandy's avatar
Jim Blandy committed
176
/* Modification count.  */
177 178
#define BUF_MODIFF(buf) ((buf)->text->modiff)

179 180 181
/* Character modification count.  */
#define BUF_CHARS_MODIFF(buf) ((buf)->text->chars_modiff)

182 183 184
/* Modification count as of last visit or save.  */
#define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)

185 186 187
/* Overlay modification count.  */
#define BUF_OVERLAY_MODIFF(buf) ((buf)->text->overlay_modiff)

188 189 190 191
/* Modification count as of last auto-save.  */
/* FIXME: should we move this into ->text->auto_save_modiff?  */
#define BUF_AUTOSAVE_MODIFF(buf) ((buf)->auto_save_modified)

192 193 194
/* Compaction count.  */
#define BUF_COMPACT(buf) ((buf)->text->compact)

195 196
/* Marker chain of buffer.  */
#define BUF_MARKERS(buf) ((buf)->text->markers)
Gerd Moellmann's avatar
Gerd Moellmann committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

#define BUF_UNCHANGED_MODIFIED(buf) \
  ((buf)->text->unchanged_modified)

#define BUF_OVERLAY_UNCHANGED_MODIFIED(buf) \
  ((buf)->text->overlay_unchanged_modified)
#define BUF_BEG_UNCHANGED(buf) ((buf)->text->beg_unchanged)
#define BUF_END_UNCHANGED(buf) ((buf)->text->end_unchanged)

#define UNCHANGED_MODIFIED \
  BUF_UNCHANGED_MODIFIED (current_buffer)
#define OVERLAY_UNCHANGED_MODIFIED \
  BUF_OVERLAY_UNCHANGED_MODIFIED (current_buffer)
#define BEG_UNCHANGED BUF_BEG_UNCHANGED (current_buffer)
#define END_UNCHANGED BUF_END_UNCHANGED (current_buffer)

/* Compute how many characters at the top and bottom of BUF are
   unchanged when the range START..END is modified.  This computation
   must be done each time BUF is modified.  */

#define BUF_COMPUTE_UNCHANGED(buf, start, end)				\
  do									\
    {									\
220 221 222
      if (BUF_UNCHANGED_MODIFIED (buf) == BUF_MODIFF (buf)		\
	  && (BUF_OVERLAY_UNCHANGED_MODIFIED (buf)			\
	      == BUF_OVERLAY_MODIFF (buf)))				\
Gerd Moellmann's avatar
Gerd Moellmann committed
223 224 225 226 227 228 229 230 231 232 233 234
	{								\
	  BUF_BEG_UNCHANGED (buf) = (start) - BUF_BEG (buf);		\
	  BUF_END_UNCHANGED (buf) = BUF_Z (buf) - (end);		\
	}								\
      else								\
	{								\
	  if (BUF_Z (buf) - (end) < BUF_END_UNCHANGED (buf))		\
	    BUF_END_UNCHANGED (buf) = BUF_Z (buf) - (end);		\
	  if ((start) - BUF_BEG (buf) < BUF_BEG_UNCHANGED (buf))	\
	    BUF_BEG_UNCHANGED (buf) = (start) - BUF_BEG (buf);		\
	}								\
    }									\
235
  while (false)
236

237

Pavel Janík's avatar
Pavel Janík committed
238
/* Macros to set PT in the current buffer, or another buffer.  */
Jim Blandy's avatar
Jim Blandy committed
239

240
#define SET_PT(position) (set_point (position))
241
#define TEMP_SET_PT(position) (temp_set_point (current_buffer, (position)))
Jim Blandy's avatar
Jim Blandy committed
242

243
#define SET_PT_BOTH(position, byte) (set_point_both (position, byte))
244 245
#define TEMP_SET_PT_BOTH(position, byte) \
  (temp_set_point_both (current_buffer, (position), (byte)))
246

247 248
#define BUF_TEMP_SET_PT(buffer, position) \
  (temp_set_point ((buffer), (position)))
249

250 251 252
extern void set_point (ptrdiff_t);
extern void temp_set_point (struct buffer *, ptrdiff_t);
extern void set_point_both (ptrdiff_t, ptrdiff_t);
253
extern void temp_set_point_both (struct buffer *,
254
				 ptrdiff_t, ptrdiff_t);
255
extern void set_point_from_marker (Lisp_Object);
256
extern void enlarge_buffer_text (struct buffer *, ptrdiff_t);
Jim Blandy's avatar
Jim Blandy committed
257

258 259 260 261 262 263 264 265

/* Macros for setting the BEGV, ZV or PT of a given buffer.

   The ..._BOTH macros take both a charpos and a bytepos,
   which must correspond to each other.

   The macros without ..._BOTH take just a charpos,
   and compute the bytepos from it.  */
Jim Blandy's avatar
Jim Blandy committed
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
#define SET_BUF_BEGV(buf, charpos)				 \
  ((buf)->begv_byte = buf_charpos_to_bytepos ((buf), (charpos)), \
   (buf)->begv = (charpos))

#define SET_BUF_ZV(buf, charpos)				\
  ((buf)->zv_byte = buf_charpos_to_bytepos ((buf), (charpos)),	\
   (buf)->zv = (charpos))

#define SET_BUF_BEGV_BOTH(buf, charpos, byte)		\
  ((buf)->begv = (charpos),				\
   (buf)->begv_byte = (byte))

#define SET_BUF_ZV_BOTH(buf, charpos, byte)		\
  ((buf)->zv = (charpos),				\
   (buf)->zv_byte = (byte))

#define SET_BUF_PT_BOTH(buf, charpos, byte)		\
  ((buf)->pt = (charpos),				\
   (buf)->pt_byte = (byte))

/* Macros to access a character or byte in the current buffer,
   or convert between a byte position and an address.
   These macros do not check that the position is in range.  */

/* Access a Lisp position value in POS,
Eli Zaretskii's avatar
Eli Zaretskii committed
292
   and store the charpos in CHARPOS and the bytepos in BYTEPOS.  */
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
#define DECODE_POSITION(charpos, bytepos, pos)				\
  do									\
    {									\
      Lisp_Object __pos = (pos);					\
      if (NUMBERP (__pos))						\
	{								\
	  charpos = __pos;						\
	  bytepos = buf_charpos_to_bytepos (current_buffer, __pos);	\
	}								\
      else if (MARKERP (__pos))						\
	{								\
	  charpos = marker_position (__pos);				\
	  bytepos = marker_byte_position (__pos);			\
	}								\
      else								\
	wrong_type_argument (Qinteger_or_marker_p, __pos);		\
    }									\
311
  while (false)
312

313 314
/* Maximum number of bytes in a buffer.
   A buffer cannot contain more bytes than a 1-origin fixnum can represent,
315 316 317 318
   nor can it be so large that C pointer arithmetic stops working.
   The ptrdiff_t cast ensures that this is signed, not unsigned.  */
#define BUF_BYTES_MAX \
  (ptrdiff_t) min (MOST_POSITIVE_FIXNUM - 1, min (SIZE_MAX, PTRDIFF_MAX))
319

320 321 322 323 324 325 326 327 328 329
/* Maximum gap size after compact_buffer, in bytes.  Also
   used in make_gap_larger to get some extra reserved space.  */

#define GAP_BYTES_DFL 2000

/* Minimum gap size after compact_buffer, in bytes.  Also
   used in make_gap_smaller to avoid too small gap size.  */

#define GAP_BYTES_MIN 20

330 331 332
/* Return the address of byte position N in current buffer.  */

#define BYTE_POS_ADDR(n) \
Stefan Monnier's avatar
Stefan Monnier committed
333
  (((n) >= GPT_BYTE ? GAP_SIZE : 0) + (n) + BEG_ADDR - BEG_BYTE)
334 335 336 337 338 339

/* Return the address of char position N.  */

#define CHAR_POS_ADDR(n)			\
  (((n) >= GPT ? GAP_SIZE : 0)			\
   + buf_charpos_to_bytepos (current_buffer, n)	\
Stefan Monnier's avatar
Stefan Monnier committed
340
   + BEG_ADDR - BEG_BYTE)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

/* Convert a character position to a byte position.  */

#define CHAR_TO_BYTE(charpos)			\
  (buf_charpos_to_bytepos (current_buffer, charpos))

/* Convert a byte position to a character position.  */

#define BYTE_TO_CHAR(bytepos)			\
  (buf_bytepos_to_charpos (current_buffer, bytepos))

/* Convert PTR, the address of a byte in the buffer, into a byte position.  */

#define PTR_BYTE_POS(ptr) \
((ptr) - (current_buffer)->text->beg					    \
356
 - (ptr - (current_buffer)->text->beg <= GPT_BYTE - BEG_BYTE ? 0 : GAP_SIZE) \
Stefan Monnier's avatar
Stefan Monnier committed
357
 + BEG_BYTE)
358

359 360
/* Return character at byte position POS.  See the caveat WARNING for
   FETCH_MULTIBYTE_CHAR below.  */
361 362

#define FETCH_CHAR(pos)				      	\
Tom Tromey's avatar
Tom Tromey committed
363
  (!NILP (BVAR (current_buffer, enable_multibyte_characters))	\
364 365 366 367 368 369 370
   ? FETCH_MULTIBYTE_CHAR ((pos))		      	\
   : FETCH_BYTE ((pos)))

/* Return the byte at byte position N.  */

#define FETCH_BYTE(n) *(BYTE_POS_ADDR ((n)))

371
/* Return character at byte position POS.  If the current buffer is unibyte
372 373 374 375
   and the character is not ASCII, make the returning character
   multibyte.  */

#define FETCH_CHAR_AS_MULTIBYTE(pos)			\
Tom Tromey's avatar
Tom Tromey committed
376
  (!NILP (BVAR (current_buffer, enable_multibyte_characters))	\
377
   ? FETCH_MULTIBYTE_CHAR ((pos))			\
378
   : UNIBYTE_TO_CHAR (FETCH_BYTE ((pos))))
379

380 381 382 383 384

/* Macros for accessing a character or byte,
   or converting between byte positions and addresses,
   in a specified buffer.  */

385
/* Return the address of character at byte position POS in buffer BUF.
Jim Blandy's avatar
Jim Blandy committed
386
   Note that both arguments can be computed more than once.  */
387 388

#define BUF_BYTE_ADDRESS(buf, pos) \
Stefan Monnier's avatar
Stefan Monnier committed
389
((buf)->text->beg + (pos) - BEG_BYTE		\
390 391
 + ((pos) >= (buf)->text->gpt_byte ? (buf)->text->gap_size : 0))

392
/* Return the address of character at char position POS in buffer BUF.
393 394 395
   Note that both arguments can be computed more than once.  */

#define BUF_CHAR_ADDRESS(buf, pos) \
Stefan Monnier's avatar
Stefan Monnier committed
396
((buf)->text->beg + buf_charpos_to_bytepos ((buf), (pos)) - BEG_BYTE	\
397
 + ((pos) >= (buf)->text->gpt ? (buf)->text->gap_size : 0))
Jim Blandy's avatar
Jim Blandy committed
398

399 400
/* Convert PTR, the address of a char in buffer BUF,
   into a character position.  */
401

402 403
#define BUF_PTR_BYTE_POS(buf, ptr)				\
((ptr) - (buf)->text->beg					\
404
 - (ptr - (buf)->text->beg <= BUF_GPT_BYTE (buf) - BEG_BYTE	\
405
    ? 0 : BUF_GAP_SIZE ((buf)))					\
Stefan Monnier's avatar
Stefan Monnier committed
406
 + BEG_BYTE)
407 408 409 410 411 412 413 414 415 416 417 418

/* Return the character at byte position POS in buffer BUF.   */

#define BUF_FETCH_CHAR(buf, pos)	      	\
  (!NILP (buf->enable_multibyte_characters)	\
   ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos))    \
   : BUF_FETCH_BYTE ((buf), (pos)))

/* Return the byte at byte position N in buffer BUF.   */

#define BUF_FETCH_BYTE(buf, n) \
  *(BUF_BYTE_ADDRESS ((buf), (n)))
Jim Blandy's avatar
Jim Blandy committed
419

420 421 422 423 424
/* Define the actual buffer data structures.  */

/* This data structure describes the actual text contents of a buffer.
   It is shared between indirect buffers and their base buffer.  */

Jim Blandy's avatar
Jim Blandy committed
425 426
struct buffer_text
  {
427 428 429 430 431
    /* Actual address of buffer contents.  If REL_ALLOC is defined,
       this address might change when blocks are relocated which can
       e.g. happen when malloc is called.  So, don't pass a pointer
       into a buffer's text to functions that malloc.  */
    unsigned char *beg;
432

433 434 435 436 437 438
    ptrdiff_t gpt;		/* Char pos of gap in buffer.  */
    ptrdiff_t z;		/* Char pos of end of buffer.  */
    ptrdiff_t gpt_byte;		/* Byte pos of gap in buffer.  */
    ptrdiff_t z_byte;		/* Byte pos of end of buffer.  */
    ptrdiff_t gap_size;		/* Size of buffer's gap.  */
    EMACS_INT modiff;		/* This counts buffer-modification events
Jim Blandy's avatar
Jim Blandy committed
439 440 441
				   for this buffer.  It is incremented for
				   each such event, and never otherwise
				   changed.  */
442
    EMACS_INT chars_modiff;	/* This is modified with character change
443 444 445
				   events for this buffer.  It is set to
				   modiff for each such event, and never
				   otherwise changed.  */
446
    EMACS_INT save_modiff;	/* Previous value of modiff, as of last
447 448
				   time buffer visited or saved a file.  */

449
    EMACS_INT overlay_modiff;	/* Counts modifications to overlays.  */
450

Dmitry Antipov's avatar
Dmitry Antipov committed
451 452 453
    EMACS_INT compact;		/* Set to modiff each time when compact_buffer
				   is called for this buffer.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
454
    /* Minimum value of GPT - BEG since last redisplay that finished.  */
455
    ptrdiff_t beg_unchanged;
Gerd Moellmann's avatar
Gerd Moellmann committed
456 457

    /* Minimum value of Z - GPT since last redisplay that finished.  */
458
    ptrdiff_t end_unchanged;
Gerd Moellmann's avatar
Gerd Moellmann committed
459 460 461

    /* MODIFF as of last redisplay that finished; if it matches MODIFF,
       beg_unchanged and end_unchanged contain no useful information.  */
462
    EMACS_INT unchanged_modified;
Gerd Moellmann's avatar
Gerd Moellmann committed
463 464 465 466

    /* BUF_OVERLAY_MODIFF of current buffer, as of last redisplay that
       finished; if it matches BUF_OVERLAY_MODIFF, beg_unchanged and
       end_unchanged contain no useful information.  */
467
    EMACS_INT overlay_unchanged_modified;
Gerd Moellmann's avatar
Gerd Moellmann committed
468

Dave Love's avatar
Dave Love committed
469 470
    /* Properties of this buffer's text.  */
    INTERVAL intervals;
471 472 473 474

    /* The markers that refer to this buffer.
       This is actually a single marker ---
       successive elements in its marker `chain'
475 476 477 478
       are the other markers referring to this buffer.
       This is a singly linked unordered list, which means that it's
       very cheap to add a marker to the list and it's also very cheap
       to move a marker within a buffer.  */
479
    struct Lisp_Marker *markers;
480

481
    /* Usually false.  Temporarily true in decode_coding_gap to
482
       prevent Fgarbage_collect from shrinking the gap and losing
483
       not-yet-decoded bytes.  */
484
    bool_bf inhibit_shrinking : 1;
485 486

    /* True if it needs to be redisplayed.  */
487
    bool_bf redisplay : 1;
Jim Blandy's avatar
Jim Blandy committed
488 489
  };

490
/* Most code should use this macro to access Lisp fields in struct buffer.  */
491

492
#define BVAR(buf, field) ((buf)->field ## _)
493

494 495
/* This is the structure that the buffer Lisp object points to.  */

Jim Blandy's avatar
Jim Blandy committed
496
struct buffer
497
{
498
  struct vectorlike_header header;
499 500

  /* The name of this buffer.  */
501
  Lisp_Object name_;
502 503

  /* The name of the file visited in this buffer, or nil.  */
504
  Lisp_Object filename_;
505 506

  /* Directory for expanding relative file names.  */
507
  Lisp_Object directory_;
508 509 510

  /* True if this buffer has been backed up (if you write to the visited
     file and it hasn't been backed up, then a backup will be made).  */
511
  Lisp_Object backed_up_;
512

513
  /* Length of file when last read or saved.
514 515 516
     -1 means auto saving turned off because buffer shrank a lot.
     -2 means don't turn off auto saving if buffer shrinks.
       (That value is used with buffer-swap-text.)
517 518
     This is not in the  struct buffer_text
     because it's not used in indirect buffers at all.  */
519
  Lisp_Object save_length_;
520

521 522 523
  /* File name used for auto-saving this buffer.
     This is not in the  struct buffer_text
     because it's not used in indirect buffers at all.  */
524
  Lisp_Object auto_save_file_name_;
525 526

  /* Non-nil if buffer read-only.  */
527
  Lisp_Object read_only_;
528

529 530
  /* "The mark".  This is a marker which may
     point into this buffer or may point nowhere.  */
531
  Lisp_Object mark_;
532

533 534 535
  /* Alist of elements (SYMBOL . VALUE-IN-THIS-BUFFER) for all
     per-buffer variables of this buffer.  For locally unbound
     symbols, just the symbol appears as the element.  */
536
  Lisp_Object local_var_alist_;
537

538
  /* Symbol naming major mode (e.g., lisp-mode).  */
539
  Lisp_Object major_mode_;
540 541

  /* Pretty name of major mode (e.g., "Lisp"). */
542
  Lisp_Object mode_name_;
543

544
  /* Mode line element that controls format of mode line.  */
545
  Lisp_Object mode_line_format_;
546

547 548
  /* Analogous to mode_line_format for the line displayed at the top
     of windows.  Nil means don't display that line.  */
549
  Lisp_Object header_line_format_;
550 551

  /* Keys that are bound local to this buffer.  */
552
  Lisp_Object keymap_;
553

554
  /* This buffer's local abbrev table.  */
555
  Lisp_Object abbrev_table_;
556

557
  /* This buffer's syntax table.  */
558
  Lisp_Object syntax_table_;
559

560
  /* This buffer's category table.  */
561
  Lisp_Object category_table_;
562

Pavel Janík's avatar
Pavel Janík committed
563
  /* Values of several buffer-local variables.  */
564
  /* tab-width is buffer-local so that redisplay can find it
Pavel Janík's avatar
Pavel Janík committed
565
     in buffers that are not current.  */
566 567 568 569
  Lisp_Object case_fold_search_;
  Lisp_Object tab_width_;
  Lisp_Object fill_column_;
  Lisp_Object left_margin_;
570

571
  /* Function to call when insert space past fill column.  */
572
  Lisp_Object auto_fill_function_;
573 574 575

  /* Case table for case-conversion in this buffer.
     This char-table maps each char into its lower-case version.  */
576
  Lisp_Object downcase_table_;
577

578
  /* Char-table mapping each char to its upper-case version.  */
579
  Lisp_Object upcase_table_;
580

581
  /* Char-table for conversion for case-folding search.  */
582
  Lisp_Object case_canon_table_;
583

584
  /* Char-table of equivalences for case-folding search.  */
585
  Lisp_Object case_eqv_table_;
586 587

  /* Non-nil means do not display continuation lines.  */
588
  Lisp_Object truncate_lines_;
589

590
  /* Non-nil means to use word wrapping when displaying continuation lines.  */
591
  Lisp_Object word_wrap_;
592

593
  /* Non-nil means display ctl chars with uparrow.  */
594
  Lisp_Object ctl_arrow_;
595

596 597
  /* Non-nil means reorder bidirectional text for display in the
     visual order.  */
598
  Lisp_Object bidi_display_reordering_;
599

600 601 602
  /* If non-nil, specifies which direction of text to force in all the
     paragraphs of the buffer.  Nil means determine paragraph
     direction dynamically for each paragraph.  */
603
  Lisp_Object bidi_paragraph_direction_;
604

605 606
  /* Non-nil means do selective display;
     see doc string in syms_of_buffer (buffer.c) for details.  */
607
  Lisp_Object selective_display_;
608

609
  /* Non-nil means show ... at end of line followed by invisible lines.  */
610
  Lisp_Object selective_display_ellipses_;
611

612
  /* Alist of (FUNCTION . STRING) for each minor mode enabled in buffer.  */
613
  Lisp_Object minor_modes_;
614

615 616
  /* t if "self-insertion" should overwrite; `binary' if it should also
     overwrite newlines and tabs - for editing executables and the like.  */
617
  Lisp_Object overwrite_mode_;
618 619

  /* Non-nil means abbrev mode is on.  Expand abbrevs automatically.  */
620
  Lisp_Object abbrev_mode_;
621

622
  /* Display table to use for text in this buffer.  */
623
  Lisp_Object display_table_;
624

625
  /* t means the mark and region are currently active.  */
626
  Lisp_Object mark_active_;
627 628 629

  /* Non-nil means the buffer contents are regarded as multi-byte
     form of characters, not a binary code.  */
630
  Lisp_Object enable_multibyte_characters_;
631 632 633

  /* Coding system to be used for encoding the buffer contents on
     saving.  */
634
  Lisp_Object buffer_file_coding_system_;
635

Pavel Janík's avatar
Pavel Janík committed
636
  /* List of symbols naming the file format used for visited file.  */
637
  Lisp_Object file_format_;
638

639
  /* List of symbols naming the file format used for auto-save file.  */
640
  Lisp_Object auto_save_file_format_;
641

642 643
  /* True if the newline position cache, width run cache and BIDI paragraph
     cache are enabled.  See search.c, indent.c and bidi.c for details.  */
644
  Lisp_Object cache_long_scans_;
645 646 647 648 649 650 651

  /* If the width run cache is enabled, this table contains the
     character widths width_run_cache (see above) assumes.  When we
     do a thorough redisplay, we compare this against the buffer's
     current display table to see whether the display table has
     affected the widths of any characters.  If it has, we
     invalidate the width run cache, and re-initialize width_table.  */
652
  Lisp_Object width_table_;
653 654 655 656

  /* In an indirect buffer, or a buffer that is the base of an
     indirect buffer, this holds a marker that records
     PT for this buffer when the buffer is not current.  */
657
  Lisp_Object pt_marker_;
658 659 660 661

  /* In an indirect buffer, or a buffer that is the base of an
     indirect buffer, this holds a marker that records
     BEGV for this buffer when the buffer is not current.  */
662
  Lisp_Object begv_marker_;
663 664 665 666

  /* In an indirect buffer, or a buffer that is the base of an
     indirect buffer, this holds a marker that records
     ZV for this buffer when the buffer is not current.  */
667
  Lisp_Object zv_marker_;
668 669 670

  /* This holds the point value before the last scroll operation.
     Explicitly setting point sets this to nil.  */
671
  Lisp_Object point_before_scroll_;
672 673

  /* Truename of the visited file, or nil.  */
674
  Lisp_Object file_truename_;
675 676 677 678 679

  /* Invisibility spec of this buffer.
     t => any non-nil `invisible' property means invisible.
     A list => `invisible' property means invisible
     if it is memq in that list.  */
680
  Lisp_Object invisibility_spec_;
681 682 683

  /* This is the last window that was selected with this buffer in it,
     or nil if that window no longer displays this buffer.  */
684
  Lisp_Object last_selected_window_;
685 686

  /* Incremented each time the buffer is displayed in a window.  */
687
  Lisp_Object display_count_;
688

689 690
  /* Widths of left and right marginal areas for windows displaying
     this buffer.  */
691 692
  Lisp_Object left_margin_cols_;
  Lisp_Object right_margin_cols_;
693 694 695

  /* Widths of left and right fringe areas for windows displaying
     this buffer.  */
696 697
  Lisp_Object left_fringe_width_;
  Lisp_Object right_fringe_width_;
698 699 700

  /* Non-nil means fringes are drawn outside display margins;
     othersize draw them between margin areas and text.  */
701
  Lisp_Object fringes_outside_margins_;
702

703
  /* Width, height and types of scroll bar areas for windows displaying
704
     this buffer.  */
705 706 707 708
  Lisp_Object scroll_bar_width_;
  Lisp_Object scroll_bar_height_;
  Lisp_Object vertical_scroll_bar_type_;
  Lisp_Object horizontal_scroll_bar_type_;
709 710 711

  /* Non-nil means indicate lines not displaying text (in a style
     like vi).  */
712
  Lisp_Object indicate_empty_lines_;
713

Kenichi Handa's avatar
Kenichi Handa committed
714
  /* Non-nil means indicate buffer boundaries and scrolling.  */
715
  Lisp_Object indicate_buffer_boundaries_;
Kenichi Handa's avatar
Kenichi Handa committed
716

717
  /* Logical to physical fringe bitmap mappings.  */
718
  Lisp_Object fringe_indicator_alist_;
719 720

  /* Logical to physical cursor bitmap mappings.  */
721
  Lisp_Object fringe_cursor_alist_;
722

723
  /* Time stamp updated each time this buffer is displayed in a window.  */
724
  Lisp_Object display_time_;
725 726 727 728 729

  /* If scrolling the display because point is below the bottom of a
     window showing this buffer, try to choose a window start so
     that point ends up this number of lines from the top of the
     window.  Nil means that scrolling method isn't used.  */
730
  Lisp_Object scroll_up_aggressively_;
731

732 733 734 735
  /* If scrolling the display because point is above the top of a
     window showing this buffer, try to choose a window start so
     that point ends up this number of lines from the bottom of the
     window.  Nil means that scrolling method isn't used.  */
736
  Lisp_Object scroll_down_aggressively_;
Gerd Moellmann's avatar
Gerd Moellmann committed
737

738 739
  /* Desired cursor type in this buffer.  See the doc string of
     per-buffer variable `cursor-type'.  */
740
  Lisp_Object cursor_type_;
741 742 743

  /* An integer > 0 means put that number of pixels below text lines
     in the display of this buffer.  */
744
  Lisp_Object extra_line_spacing_;
745

746
  /* Cursor type to display in non-selected windows.
747 748
     t means to use hollow box cursor.
     See `cursor-type' for other values.  */
749
  Lisp_Object cursor_in_non_selected_windows_;
750

751
  /* No more Lisp_Object beyond this point.  Except undo_list,
Paul Eggert's avatar
Paul Eggert committed
752
     which is handled specially in Fgarbage_collect.  */
753 754 755 756 757 758 759 760 761 762

  /* This structure holds the coordinates of the buffer contents
     in ordinary buffers.  In indirect buffers, this is not used.  */
  struct buffer_text own_text;

  /* This points to the `struct buffer_text' that used for this buffer.
     In an ordinary buffer, this is the own_text field above.
     In an indirect buffer, this is the own_text field of another buffer.  */
  struct buffer_text *text;

763 764 765
  /* Next buffer, in chain of all buffers, including killed ones.  */
  struct buffer *next;

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
  /* Char position of point in buffer.  */
  ptrdiff_t pt;

  /* Byte position of point in buffer.  */
  ptrdiff_t pt_byte;

  /* Char position of beginning of accessible range.  */
  ptrdiff_t begv;

  /* Byte position of beginning of accessible range.  */
  ptrdiff_t begv_byte;

  /* Char position of end of accessible range.  */
  ptrdiff_t zv;

  /* Byte position of end of accessible range.  */
  ptrdiff_t zv_byte;

  /* In an indirect buffer, this points to the base buffer.
     In an ordinary buffer, it is 0.  */
  struct buffer *base_buffer;

Dmitry Antipov's avatar
Dmitry Antipov committed
788
  /* In an indirect buffer, this is -1.  In an ordinary buffer,
789
     it's the number of indirect buffers that share our text;
790
     zero means that we're the only owner of this text.  */
791
  int indirections;
792

Dmitry Antipov's avatar
Dmitry Antipov committed
793 794 795 796
  /* Number of windows showing this buffer.  Always -1 for
     an indirect buffer since it counts as its base buffer.  */
  int window_count;

797 798 799 800 801 802 803 804 805
  /* A non-zero value in slot IDX means that per-buffer variable
     with index IDX has a local value in this buffer.  The index IDX
     for a buffer-local variable is stored in that variable's slot
     in buffer_local_flags as a Lisp integer.  If the index is -1,
     this means the variable is always local in all buffers.  */
#define MAX_PER_BUFFER_VARS 50
  char local_flags[MAX_PER_BUFFER_VARS];

  /* Set to the modtime of the visited file when read or written.
806 807
     modtime.tv_nsec == NONEXISTENT_MODTIME_NSECS means
     visited file was nonexistent.  modtime.tv_nsec ==
808 809 810 811
     UNKNOWN_MODTIME_NSECS means visited file modtime unknown;
     in no case complain about any mismatch on next save attempt.  */
#define NONEXISTENT_MODTIME_NSECS (-1)
#define UNKNOWN_MODTIME_NSECS (-2)
812
  struct timespec modtime;
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

  /* Size of the file when modtime was set.  This is used to detect the
     case where the file grew while we were reading it, so the modtime
     is still the same (since it's rounded up to seconds) but we're actually
     not up-to-date.  -1 means the size is unknown.  Only meaningful if
     modtime is actually set.  */
  off_t modtime_size;

  /* The value of text->modiff at the last auto-save.  */
  EMACS_INT auto_save_modified;

  /* The value of text->modiff at the last display error.
     Redisplay of this buffer is inhibited until it changes again.  */
  EMACS_INT display_error_modiff;

  /* The time at which we detected a failure to auto-save,
     Or 0 if we didn't have a failure.  */
  time_t auto_save_failure_time;

  /* Position in buffer at which display started
     the last time this buffer was displayed.  */
  ptrdiff_t last_window_start;

  /* If the long line scan cache is enabled (i.e. the buffer-local
     variable cache-long-line-scans is non-nil), newline_cache
     points to the newline cache, and width_run_cache points to the
     width run cache.

     The newline cache records which stretches of the buffer are
     known *not* to contain newlines, so that they can be skipped
     quickly when we search for newlines.

     The width run cache records which stretches of the buffer are
     known to contain characters whose widths are all the same.  If
     the width run cache maps a character to a value > 0, that value is
     the character's width; if it maps a character to zero, we don't
     know what its width is.  This allows compute_motion to process
     such regions very quickly, using algebra instead of inspecting
851 852 853
     each character.   See also width_table, below.

     The latter cache is used to speedup bidi_find_paragraph_start.  */
854 855
  struct region_cache *newline_cache;
  struct region_cache *width_run_cache;
856
  struct region_cache *bidi_paragraph_cache;
857

858 859
  /* Non-zero means disable redisplay optimizations when rebuilding the glyph
     matrices (but not when redrawing).  */
860
  bool_bf prevent_redisplay_optimizations_p : 1;
861 862

  /* Non-zero whenever the narrowing is changed in this buffer.  */
863
  bool_bf clip_changed : 1;
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

  /* List of overlays that end at or before the current center,
     in order of end-position.  */
  struct Lisp_Overlay *overlays_before;

  /* List of overlays that end after  the current center,
     in order of start-position.  */
  struct Lisp_Overlay *overlays_after;

  /* Position where the overlay lists are centered.  */
  ptrdiff_t overlay_center;

  /* Changes in the buffer are recorded here for undo, and t means
     don't record anything.  This information belongs to the base
     buffer of an indirect buffer.  But we can't store it in the
     struct buffer_text because local variables have to be right in
     the struct buffer. So we copy it around in set_buffer_internal.  */
881
  Lisp_Object undo_list_;
882
};
883

Paul Eggert's avatar
Paul Eggert committed
884
/* Most code should use these functions to set Lisp fields in struct
Paul Eggert's avatar
Paul Eggert committed
885
   buffer.  (Some setters that are private to a single .c file are
886
   defined as static in those files.)  */
Paul Eggert's avatar
Paul Eggert committed
887
INLINE void
Paul Eggert's avatar
Paul Eggert committed
888 889
bset_bidi_paragraph_direction (struct buffer *b, Lisp_Object val)
{
890
  b->bidi_paragraph_direction_ = val;
Paul Eggert's avatar
Paul Eggert committed
891
}
Paul Eggert's avatar
Paul Eggert committed
892
INLINE void
893 894
bset_cache_long_scans (struct buffer *b, Lisp_Object val)
{
895
  b->cache_long_scans_ = val;
896 897
}
INLINE void
Paul Eggert's avatar
Paul Eggert committed
898 899
bset_case_canon_table (struct buffer