root/src/buffer.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. CHECK_OVERLAY
  2. fix_position
  3. bset_abbrev_mode
  4. bset_abbrev_table
  5. bset_auto_fill_function
  6. bset_auto_save_file_format
  7. bset_auto_save_file_name
  8. bset_backed_up
  9. bset_begv_marker
  10. bset_bidi_display_reordering
  11. bset_bidi_paragraph_start_re
  12. bset_bidi_paragraph_separate_re
  13. bset_buffer_file_coding_system
  14. bset_case_fold_search
  15. bset_ctl_arrow
  16. bset_cursor_in_non_selected_windows
  17. bset_cursor_type
  18. bset_display_table
  19. bset_extra_line_spacing
  20. bset_ts_parser_list
  21. bset_file_format
  22. bset_file_truename
  23. bset_fringe_cursor_alist
  24. bset_fringe_indicator_alist
  25. bset_fringes_outside_margins
  26. bset_header_line_format
  27. bset_tab_line_format
  28. bset_indicate_buffer_boundaries
  29. bset_indicate_empty_lines
  30. bset_invisibility_spec
  31. bset_left_fringe_width
  32. bset_major_mode
  33. bset_local_minor_modes
  34. bset_mark
  35. bset_mode_line_format
  36. bset_mode_name
  37. bset_name
  38. bset_overwrite_mode
  39. bset_pt_marker
  40. bset_right_fringe_width
  41. bset_save_length
  42. bset_scroll_bar_width
  43. bset_scroll_bar_height
  44. bset_scroll_down_aggressively
  45. bset_scroll_up_aggressively
  46. bset_selective_display
  47. bset_selective_display_ellipses
  48. bset_vertical_scroll_bar_type
  49. bset_horizontal_scroll_bar_type
  50. bset_word_wrap
  51. bset_zv_marker
  52. nsberror
  53. DEFUN
  54. DEFUN
  55. assoc_ignore_text_properties
  56. DEFUN
  57. DEFUN
  58. get_truename_buffer
  59. run_buffer_list_update_hook
  60. add_buffer_overlay
  61. copy_overlays
  62. valid_per_buffer_idx
  63. clone_per_buffer_values
  64. record_buffer_markers
  65. fetch_buffer_markers
  66. remove_buffer_overlay
  67. drop_overlay
  68. delete_all_overlays
  69. free_buffer_overlays
  70. set_overlays_multibyte
  71. reset_buffer
  72. reset_buffer_local_variables
  73. DEFUN
  74. DEFUN
  75. DEFUN
  76. buffer_local_value
  77. buffer_lisp_local_variables
  78. buffer_local_variables_1
  79. DEFUN
  80. DEFUN
  81. DEFUN
  82. DEFUN
  83. DEFUN
  84. DEFUN
  85. DEFUN
  86. candidate_buffer
  87. other_buffer_safely
  88. DEFUN
  89. compact_buffer
  90. DEFUN
  91. record_buffer
  92. DEFUN
  93. DEFUN
  94. DEFUN
  95. set_buffer_internal_1
  96. set_buffer_internal_2
  97. set_buffer_temp
  98. DEFUN
  99. restore_buffer
  100. set_buffer_if_live
  101. DEFUN
  102. DEFUN
  103. validate_region
  104. advance_to_char_boundary
  105. swap_buffer_overlays
  106. DEFUN
  107. DEFUN
  108. DEFUN
  109. overlays_in
  110. overlays_at
  111. next_overlay_change
  112. previous_overlay_change
  113. mouse_face_overlay_overlaps
  114. disable_line_numbers_overlay_at_eob
  115. overlay_touches_p
  116. compare_overlays
  117. make_sortvec_item
  118. sort_overlays
  119. cmp_for_strings
  120. record_overlay_string
  121. overlay_strings
  122. adjust_overlays_for_insert
  123. adjust_overlays_for_delete_in_buffer
  124. adjust_overlays_for_delete
  125. DEFUN
  126. modify_overlay
  127. DEFUN
  128. DEFUN
  129. DEFUN
  130. DEFUN
  131. DEFUN
  132. DEFUN
  133. DEFUN
  134. DEFUN
  135. DEFUN
  136. DEFUN
  137. add_overlay_mod_hooklist
  138. report_overlay_modification
  139. call_overlay_mod_hooks
  140. mmap_init
  141. mmap_free_1
  142. mmap_enlarge
  143. mmap_alloc
  144. mmap_free
  145. mmap_realloc
  146. alloc_buffer_text
  147. enlarge_buffer_text
  148. free_buffer_text
  149. init_buffer_once
  150. init_buffer
  151. defvar_per_buffer
  152. make_lispy_itree_node
  153. overlay_tree
  154. DEFUN
  155. syms_of_buffer

     1 /* Buffer manipulation primitives for GNU Emacs.
     2 
     3 Copyright (C) 1985-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 #include <config.h>
    21 
    22 #include <sys/types.h>
    23 #include <sys/stat.h>
    24 #include <sys/param.h>
    25 #include <errno.h>
    26 #include <stdio.h>
    27 #include <stdlib.h>
    28 #include <unistd.h>
    29 
    30 #include <verify.h>
    31 
    32 #include "lisp.h"
    33 #include "intervals.h"
    34 #include "process.h"
    35 #include "systime.h"
    36 #include "window.h"
    37 #include "commands.h"
    38 #include "character.h"
    39 #include "buffer.h"
    40 #include "region-cache.h"
    41 #include "indent.h"
    42 #include "blockinput.h"
    43 #include "keymap.h"
    44 #include "frame.h"
    45 #include "xwidget.h"
    46 #include "itree.h"
    47 #include "pdumper.h"
    48 
    49 #ifdef WINDOWSNT
    50 #include "w32heap.h"            /* for mmap_* */
    51 #endif
    52 
    53 /* Work around GCC bug 109847
    54    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109847
    55    which causes GCC to mistakenly complain about
    56    AUTO_STRING with "*scratch*".  */
    57 #if GNUC_PREREQ (13, 0, 0)
    58 # pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
    59 #endif
    60 
    61 /* This structure holds the default values of the buffer-local variables
    62    defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
    63    The default value occupies the same slot in this structure
    64    as an individual buffer's value occupies in that buffer.
    65    Setting the default value also goes through the alist of buffers
    66    and stores into each buffer that does not say it has a local value.  */
    67 
    68 struct buffer buffer_defaults;
    69 
    70 /* This structure marks which slots in a buffer have corresponding
    71    default values in buffer_defaults.
    72    Each such slot has a value in this structure.
    73    The value is a positive Lisp integer that must be smaller than
    74    MAX_PER_BUFFER_VARS.
    75 
    76    When a buffer has its own local value for a slot,
    77    the entry for that slot (found in the same slot in this structure)
    78    is turned on in the buffer's local_flags array.
    79 
    80    If a slot in this structure is -1, then even though there may
    81    be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
    82    and the corresponding slot in buffer_defaults is not used.
    83 
    84    If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
    85    zero, that is a bug.  */
    86 
    87 struct buffer buffer_local_flags;
    88 
    89 /* This structure holds the names of symbols whose values may be
    90    buffer-local.  It is indexed and accessed in the same way as the above.  */
    91 
    92 struct buffer buffer_local_symbols;
    93 
    94 /* Return the symbol of the per-buffer variable at offset OFFSET in
    95    the buffer structure.  */
    96 
    97 #define PER_BUFFER_SYMBOL(OFFSET) \
    98       (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
    99 
   100 /* Maximum length of an overlay vector.  */
   101 #define OVERLAY_COUNT_MAX                                               \
   102   ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM,                               \
   103                     min (PTRDIFF_MAX, SIZE_MAX) / word_size))
   104 
   105 /* Flags indicating which built-in buffer-local variables
   106    are permanent locals.  */
   107 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
   108 
   109 /* Number of per-buffer variables used.  */
   110 
   111 static int last_per_buffer_idx;
   112 
   113 static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
   114                                     bool after, Lisp_Object arg1,
   115                                     Lisp_Object arg2, Lisp_Object arg3);
   116 static void reset_buffer_local_variables (struct buffer *, bool);
   117 
   118 /* Alist of all buffer names vs the buffers.  This used to be
   119    a Lisp-visible variable, but is no longer, to prevent lossage
   120    due to user rplac'ing this alist or its elements.  */
   121 Lisp_Object Vbuffer_alist;
   122 
   123 static Lisp_Object QSFundamental;       /* A string "Fundamental".  */
   124 
   125 static void alloc_buffer_text (struct buffer *, ptrdiff_t);
   126 static void free_buffer_text (struct buffer *b);
   127 static void copy_overlays (struct buffer *, struct buffer *);
   128 static void modify_overlay (struct buffer *, ptrdiff_t, ptrdiff_t);
   129 static Lisp_Object buffer_lisp_local_variables (struct buffer *, bool);
   130 static Lisp_Object buffer_local_variables_1 (struct buffer *buf, int offset, Lisp_Object sym);
   131 
   132 static void
   133 CHECK_OVERLAY (Lisp_Object x)
   134 {
   135   CHECK_TYPE (OVERLAYP (x), Qoverlayp, x);
   136 }
   137 
   138 /* Convert the position POS to an EMACS_INT that fits in a fixnum.
   139    Yield POS's value if POS is already a fixnum, POS's marker position
   140    if POS is a marker, and MOST_NEGATIVE_FIXNUM or
   141    MOST_POSITIVE_FIXNUM if POS is a negative or positive bignum.
   142    Signal an error if POS is not of the proper form.  */
   143 
   144 EMACS_INT
   145 fix_position (Lisp_Object pos)
   146 {
   147   if (FIXNUMP (pos))
   148     return XFIXNUM (pos);
   149   if (MARKERP (pos))
   150     return marker_position (pos);
   151   CHECK_TYPE (BIGNUMP (pos), Qinteger_or_marker_p, pos);
   152   return !NILP (Fnatnump (pos)) ? MOST_POSITIVE_FIXNUM : MOST_NEGATIVE_FIXNUM;
   153 }
   154 
   155 /* These setters are used only in this file, so they can be private.
   156    The public setters are inline functions defined in buffer.h.  */
   157 static void
   158 bset_abbrev_mode (struct buffer *b, Lisp_Object val)
   159 {
   160   b->abbrev_mode_ = val;
   161 }
   162 static void
   163 bset_abbrev_table (struct buffer *b, Lisp_Object val)
   164 {
   165   b->abbrev_table_ = val;
   166 }
   167 static void
   168 bset_auto_fill_function (struct buffer *b, Lisp_Object val)
   169 {
   170   b->auto_fill_function_ = val;
   171 }
   172 static void
   173 bset_auto_save_file_format (struct buffer *b, Lisp_Object val)
   174 {
   175   b->auto_save_file_format_ = val;
   176 }
   177 static void
   178 bset_auto_save_file_name (struct buffer *b, Lisp_Object val)
   179 {
   180   b->auto_save_file_name_ = val;
   181 }
   182 static void
   183 bset_backed_up (struct buffer *b, Lisp_Object val)
   184 {
   185   b->backed_up_ = val;
   186 }
   187 static void
   188 bset_begv_marker (struct buffer *b, Lisp_Object val)
   189 {
   190   b->begv_marker_ = val;
   191 }
   192 static void
   193 bset_bidi_display_reordering (struct buffer *b, Lisp_Object val)
   194 {
   195   b->bidi_display_reordering_ = val;
   196 }
   197 static void
   198 bset_bidi_paragraph_start_re (struct buffer *b, Lisp_Object val)
   199 {
   200   b->bidi_paragraph_start_re_ = val;
   201 }
   202 static void
   203 bset_bidi_paragraph_separate_re (struct buffer *b, Lisp_Object val)
   204 {
   205   b->bidi_paragraph_separate_re_ = val;
   206 }
   207 static void
   208 bset_buffer_file_coding_system (struct buffer *b, Lisp_Object val)
   209 {
   210   b->buffer_file_coding_system_ = val;
   211 }
   212 static void
   213 bset_case_fold_search (struct buffer *b, Lisp_Object val)
   214 {
   215   b->case_fold_search_ = val;
   216 }
   217 static void
   218 bset_ctl_arrow (struct buffer *b, Lisp_Object val)
   219 {
   220   b->ctl_arrow_ = val;
   221 }
   222 static void
   223 bset_cursor_in_non_selected_windows (struct buffer *b, Lisp_Object val)
   224 {
   225   b->cursor_in_non_selected_windows_ = val;
   226 }
   227 static void
   228 bset_cursor_type (struct buffer *b, Lisp_Object val)
   229 {
   230   b->cursor_type_ = val;
   231 }
   232 static void
   233 bset_display_table (struct buffer *b, Lisp_Object val)
   234 {
   235   b->display_table_ = val;
   236 }
   237 static void
   238 bset_extra_line_spacing (struct buffer *b, Lisp_Object val)
   239 {
   240   b->extra_line_spacing_ = val;
   241 }
   242 #ifdef HAVE_TREE_SITTER
   243 static void
   244 bset_ts_parser_list (struct buffer *b, Lisp_Object val)
   245 {
   246   b->ts_parser_list_ = val;
   247 }
   248 #endif
   249 static void
   250 bset_file_format (struct buffer *b, Lisp_Object val)
   251 {
   252   b->file_format_ = val;
   253 }
   254 static void
   255 bset_file_truename (struct buffer *b, Lisp_Object val)
   256 {
   257   b->file_truename_ = val;
   258 }
   259 static void
   260 bset_fringe_cursor_alist (struct buffer *b, Lisp_Object val)
   261 {
   262   b->fringe_cursor_alist_ = val;
   263 }
   264 static void
   265 bset_fringe_indicator_alist (struct buffer *b, Lisp_Object val)
   266 {
   267   b->fringe_indicator_alist_ = val;
   268 }
   269 static void
   270 bset_fringes_outside_margins (struct buffer *b, Lisp_Object val)
   271 {
   272   b->fringes_outside_margins_ = val;
   273 }
   274 static void
   275 bset_header_line_format (struct buffer *b, Lisp_Object val)
   276 {
   277   b->header_line_format_ = val;
   278 }
   279 static void
   280 bset_tab_line_format (struct buffer *b, Lisp_Object val)
   281 {
   282   b->tab_line_format_ = val;
   283 }
   284 static void
   285 bset_indicate_buffer_boundaries (struct buffer *b, Lisp_Object val)
   286 {
   287   b->indicate_buffer_boundaries_ = val;
   288 }
   289 static void
   290 bset_indicate_empty_lines (struct buffer *b, Lisp_Object val)
   291 {
   292   b->indicate_empty_lines_ = val;
   293 }
   294 static void
   295 bset_invisibility_spec (struct buffer *b, Lisp_Object val)
   296 {
   297   b->invisibility_spec_ = val;
   298 }
   299 static void
   300 bset_left_fringe_width (struct buffer *b, Lisp_Object val)
   301 {
   302   b->left_fringe_width_ = val;
   303 }
   304 static void
   305 bset_major_mode (struct buffer *b, Lisp_Object val)
   306 {
   307   b->major_mode_ = val;
   308 }
   309 static void
   310 bset_local_minor_modes (struct buffer *b, Lisp_Object val)
   311 {
   312   b->local_minor_modes_ = val;
   313 }
   314 static void
   315 bset_mark (struct buffer *b, Lisp_Object val)
   316 {
   317   b->mark_ = val;
   318 }
   319 static void
   320 bset_mode_line_format (struct buffer *b, Lisp_Object val)
   321 {
   322   b->mode_line_format_ = val;
   323 }
   324 static void
   325 bset_mode_name (struct buffer *b, Lisp_Object val)
   326 {
   327   b->mode_name_ = val;
   328 }
   329 static void
   330 bset_name (struct buffer *b, Lisp_Object val)
   331 {
   332   b->name_ = val;
   333 }
   334 static void
   335 bset_overwrite_mode (struct buffer *b, Lisp_Object val)
   336 {
   337   b->overwrite_mode_ = val;
   338 }
   339 static void
   340 bset_pt_marker (struct buffer *b, Lisp_Object val)
   341 {
   342   b->pt_marker_ = val;
   343 }
   344 static void
   345 bset_right_fringe_width (struct buffer *b, Lisp_Object val)
   346 {
   347   b->right_fringe_width_ = val;
   348 }
   349 static void
   350 bset_save_length (struct buffer *b, Lisp_Object val)
   351 {
   352   b->save_length_ = val;
   353 }
   354 static void
   355 bset_scroll_bar_width (struct buffer *b, Lisp_Object val)
   356 {
   357   b->scroll_bar_width_ = val;
   358 }
   359 static void
   360 bset_scroll_bar_height (struct buffer *b, Lisp_Object val)
   361 {
   362   b->scroll_bar_height_ = val;
   363 }
   364 static void
   365 bset_scroll_down_aggressively (struct buffer *b, Lisp_Object val)
   366 {
   367   b->scroll_down_aggressively_ = val;
   368 }
   369 static void
   370 bset_scroll_up_aggressively (struct buffer *b, Lisp_Object val)
   371 {
   372   b->scroll_up_aggressively_ = val;
   373 }
   374 static void
   375 bset_selective_display (struct buffer *b, Lisp_Object val)
   376 {
   377   b->selective_display_ = val;
   378 }
   379 static void
   380 bset_selective_display_ellipses (struct buffer *b, Lisp_Object val)
   381 {
   382   b->selective_display_ellipses_ = val;
   383 }
   384 static void
   385 bset_vertical_scroll_bar_type (struct buffer *b, Lisp_Object val)
   386 {
   387   b->vertical_scroll_bar_type_ = val;
   388 }
   389 static void
   390 bset_horizontal_scroll_bar_type (struct buffer *b, Lisp_Object val)
   391 {
   392   b->horizontal_scroll_bar_type_ = val;
   393 }
   394 static void
   395 bset_word_wrap (struct buffer *b, Lisp_Object val)
   396 {
   397   b->word_wrap_ = val;
   398 }
   399 static void
   400 bset_zv_marker (struct buffer *b, Lisp_Object val)
   401 {
   402   b->zv_marker_ = val;
   403 }
   404 
   405 void
   406 nsberror (Lisp_Object spec)
   407 {
   408   if (STRINGP (spec))
   409     error ("No buffer named %s", SDATA (spec));
   410   error ("Invalid buffer argument");
   411 }
   412 
   413 DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
   414        doc: /* Return t if OBJECT is a buffer which has not been killed.
   415 Value is nil if OBJECT is not a buffer or if it has been killed.  */)
   416   (Lisp_Object object)
   417 {
   418   return ((BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object)))
   419           ? Qt : Qnil);
   420 }
   421 
   422 DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
   423        doc: /* Return a list of all live buffers.
   424 If the optional arg FRAME is a frame, return the buffer list in the
   425 proper order for that frame: the buffers shown in FRAME come first,
   426 followed by the rest of the buffers.  */)
   427   (Lisp_Object frame)
   428 {
   429   Lisp_Object general;
   430   general = Fmapcar (Qcdr, Vbuffer_alist);
   431 
   432   if (FRAMEP (frame))
   433     {
   434       Lisp_Object framelist, prevlist, tail;
   435 
   436       framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
   437       prevlist = Fnreverse (Fcopy_sequence
   438                             (XFRAME (frame)->buried_buffer_list));
   439 
   440       /* Remove from GENERAL any buffer that duplicates one in
   441          FRAMELIST or PREVLIST.  */
   442       tail = framelist;
   443       while (CONSP (tail))
   444         {
   445           general = Fdelq (XCAR (tail), general);
   446           tail = XCDR (tail);
   447         }
   448       tail = prevlist;
   449       while (CONSP (tail))
   450         {
   451           general = Fdelq (XCAR (tail), general);
   452           tail = XCDR (tail);
   453         }
   454 
   455       return CALLN (Fnconc, framelist, general, prevlist);
   456     }
   457   else
   458     return general;
   459 }
   460 
   461 /* Like Fassoc, but use Fstring_equal to compare
   462    (which ignores text properties), and don't ever quit.  */
   463 
   464 static Lisp_Object
   465 assoc_ignore_text_properties (Lisp_Object key, Lisp_Object list)
   466 {
   467   Lisp_Object tail;
   468   for (tail = list; CONSP (tail); tail = XCDR (tail))
   469     {
   470       Lisp_Object elt = XCAR (tail);
   471       if (!NILP (Fstring_equal (Fcar (elt), key)))
   472         return elt;
   473     }
   474   return Qnil;
   475 }
   476 
   477 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
   478        doc: /* Return the buffer named BUFFER-OR-NAME.
   479 BUFFER-OR-NAME must be either a string or a buffer.  If BUFFER-OR-NAME
   480 is a string and there is no buffer with that name, return nil.  If
   481 BUFFER-OR-NAME is a buffer, return it as given.  */)
   482   (register Lisp_Object buffer_or_name)
   483 {
   484   if (BUFFERP (buffer_or_name))
   485     return buffer_or_name;
   486   CHECK_STRING (buffer_or_name);
   487 
   488   return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
   489 }
   490 
   491 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
   492        doc: /* Return the buffer visiting file FILENAME (a string).
   493 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
   494 If there is no such live buffer, return nil.
   495 See also `find-buffer-visiting'.  */)
   496   (register Lisp_Object filename)
   497 {
   498   register Lisp_Object tail, buf, handler;
   499 
   500   CHECK_STRING (filename);
   501   filename = Fexpand_file_name (filename, Qnil);
   502 
   503   /* If the file name has special constructs in it,
   504      call the corresponding file name handler.  */
   505   handler = Ffind_file_name_handler (filename, Qget_file_buffer);
   506   if (!NILP (handler))
   507     {
   508       Lisp_Object handled_buf = call2 (handler, Qget_file_buffer,
   509                                        filename);
   510       return BUFFERP (handled_buf) ? handled_buf : Qnil;
   511     }
   512 
   513   FOR_EACH_LIVE_BUFFER (tail, buf)
   514     {
   515       if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue;
   516       if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), filename), filename)))
   517         return buf;
   518     }
   519   return Qnil;
   520 }
   521 
   522 Lisp_Object
   523 get_truename_buffer (register Lisp_Object filename)
   524 {
   525   register Lisp_Object tail, buf;
   526 
   527   FOR_EACH_LIVE_BUFFER (tail, buf)
   528     {
   529       if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
   530       if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename)))
   531         return buf;
   532     }
   533   return Qnil;
   534 }
   535 
   536 /* Run buffer-list-update-hook if Vrun_hooks is non-nil and BUF does
   537    not have buffer hooks inhibited.  */
   538 
   539 static void
   540 run_buffer_list_update_hook (struct buffer *buf)
   541 {
   542   eassert (buf);
   543   if (! (NILP (Vrun_hooks) || buf->inhibit_buffer_hooks))
   544     call1 (Vrun_hooks, Qbuffer_list_update_hook);
   545 }
   546 
   547 DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 2, 0,
   548        doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
   549 If BUFFER-OR-NAME is a string and a live buffer with that name exists,
   550 return that buffer.  If no such buffer exists, create a new buffer with
   551 that name and return it.
   552 
   553 If BUFFER-OR-NAME starts with a space, the new buffer does not keep undo
   554 information.  If optional argument INHIBIT-BUFFER-HOOKS is non-nil, the
   555 new buffer does not run the hooks `kill-buffer-hook',
   556 `kill-buffer-query-functions', and `buffer-list-update-hook'.  This
   557 avoids slowing down internal or temporary buffers that are never
   558 presented to users or passed on to other applications.
   559 
   560 If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
   561 even if it is dead.  The return value is never nil.  */)
   562   (register Lisp_Object buffer_or_name, Lisp_Object inhibit_buffer_hooks)
   563 {
   564   register Lisp_Object buffer, name;
   565   register struct buffer *b;
   566 
   567   buffer = Fget_buffer (buffer_or_name);
   568   if (!NILP (buffer))
   569     return buffer;
   570 
   571   if (SCHARS (buffer_or_name) == 0)
   572     error ("Empty string for buffer name is not allowed");
   573 
   574   b = allocate_buffer ();
   575 
   576   /* An ordinary buffer uses its own struct buffer_text.  */
   577   b->text = &b->own_text;
   578   b->base_buffer = NULL;
   579   /* No one shares the text with us now.  */
   580   b->indirections = 0;
   581   /* No one shows us now.  */
   582   b->window_count = 0;
   583 
   584   memset (&b->local_flags, 0, sizeof (b->local_flags));
   585 
   586   BUF_GAP_SIZE (b) = 20;
   587   block_input ();
   588   /* We allocate extra 1-byte at the tail and keep it always '\0' for
   589      anchoring a search.  */
   590   alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
   591   unblock_input ();
   592   if (! BUF_BEG_ADDR (b))
   593     buffer_memory_full (BUF_GAP_SIZE (b) + 1);
   594 
   595   b->pt = BEG;
   596   b->begv = BEG;
   597   b->zv = BEG;
   598   b->pt_byte = BEG_BYTE;
   599   b->begv_byte = BEG_BYTE;
   600   b->zv_byte = BEG_BYTE;
   601 
   602   BUF_GPT (b) = BEG;
   603   BUF_GPT_BYTE (b) = BEG_BYTE;
   604 
   605   BUF_Z (b) = BEG;
   606   BUF_Z_BYTE (b) = BEG_BYTE;
   607   BUF_MODIFF (b) = 1;
   608   BUF_CHARS_MODIFF (b) = 1;
   609   BUF_OVERLAY_MODIFF (b) = 1;
   610   BUF_SAVE_MODIFF (b) = 1;
   611   BUF_COMPACT (b) = 1;
   612   set_buffer_intervals (b, NULL);
   613   BUF_UNCHANGED_MODIFIED (b) = 1;
   614   BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
   615   BUF_END_UNCHANGED (b) = 0;
   616   BUF_BEG_UNCHANGED (b) = 0;
   617   *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
   618   b->text->inhibit_shrinking = false;
   619   b->text->redisplay = false;
   620 
   621   b->newline_cache = 0;
   622   b->width_run_cache = 0;
   623   b->bidi_paragraph_cache = 0;
   624   bset_width_table (b, Qnil);
   625   b->prevent_redisplay_optimizations_p = 1;
   626 
   627   /* An ordinary buffer normally doesn't need markers
   628      to handle BEGV and ZV.  */
   629   bset_pt_marker (b, Qnil);
   630   bset_begv_marker (b, Qnil);
   631   bset_zv_marker (b, Qnil);
   632 
   633   name = Fcopy_sequence (buffer_or_name);
   634   set_string_intervals (name, NULL);
   635   bset_name (b, name);
   636 
   637   b->inhibit_buffer_hooks = !NILP (inhibit_buffer_hooks);
   638   bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
   639 
   640   reset_buffer (b);
   641   reset_buffer_local_variables (b, 1);
   642 
   643   bset_mark (b, Fmake_marker ());
   644   BUF_MARKERS (b) = NULL;
   645 
   646   /* Put this in the alist of all live buffers.  */
   647   XSETBUFFER (buffer, b);
   648   Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
   649 
   650   run_buffer_list_update_hook (b);
   651 
   652   return buffer;
   653 }
   654 
   655 static void
   656 add_buffer_overlay (struct buffer *b, struct Lisp_Overlay *ov,
   657                     ptrdiff_t begin, ptrdiff_t end)
   658 {
   659   eassert (! ov->buffer);
   660   if (! b->overlays)
   661     b->overlays = itree_create ();
   662   ov->buffer = b;
   663   itree_insert (b->overlays, ov->interval, begin, end);
   664 }
   665 
   666 /* Copy overlays of buffer FROM to buffer TO.  */
   667 
   668 static void
   669 copy_overlays (struct buffer *from, struct buffer *to)
   670 {
   671   eassert (to && ! to->overlays);
   672   struct itree_node *node;
   673 
   674   ITREE_FOREACH (node, from->overlays, PTRDIFF_MIN, PTRDIFF_MAX, ASCENDING)
   675     {
   676       Lisp_Object ov = node->data;
   677       Lisp_Object copy = build_overlay (node->front_advance,
   678                                         node->rear_advance,
   679                                         Fcopy_sequence (OVERLAY_PLIST (ov)));
   680       add_buffer_overlay (to, XOVERLAY (copy), node->begin, node->end);
   681     }
   682 }
   683 
   684 bool
   685 valid_per_buffer_idx (int idx)
   686 {
   687   return 0 <= idx && idx < last_per_buffer_idx;
   688 }
   689 
   690 /* Clone per-buffer values of buffer FROM.
   691 
   692    Buffer TO gets the same per-buffer values as FROM, with the
   693    following exceptions: (1) TO's name is left untouched, (2) markers
   694    are copied and made to refer to TO, and (3) overlay lists are
   695    copied.  */
   696 
   697 static void
   698 clone_per_buffer_values (struct buffer *from, struct buffer *to)
   699 {
   700   int offset;
   701 
   702   FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
   703     {
   704       Lisp_Object obj;
   705 
   706       /* Don't touch the `name' which should be unique for every buffer.  */
   707       if (offset == PER_BUFFER_VAR_OFFSET (name))
   708         continue;
   709 
   710       obj = per_buffer_value (from, offset);
   711       if (MARKERP (obj) && XMARKER (obj)->buffer == from)
   712         {
   713           struct Lisp_Marker *m = XMARKER (obj);
   714 
   715           obj = build_marker (to, m->charpos, m->bytepos);
   716           XMARKER (obj)->insertion_type = m->insertion_type;
   717         }
   718 
   719       set_per_buffer_value (to, offset, obj);
   720     }
   721 
   722   memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
   723 
   724   copy_overlays (from, to);
   725 
   726   /* Get (a copy of) the alist of Lisp-level local variables of FROM
   727      and install that in TO.  */
   728   bset_local_var_alist (to, buffer_lisp_local_variables (from, 1));
   729 }
   730 
   731 
   732 /* If buffer B has markers to record PT, BEGV and ZV when it is not
   733    current, update these markers.  */
   734 
   735 static void
   736 record_buffer_markers (struct buffer *b)
   737 {
   738   if (! NILP (BVAR (b, pt_marker)))
   739     {
   740       Lisp_Object buffer;
   741 
   742       eassert (!NILP (BVAR (b, begv_marker)));
   743       eassert (!NILP (BVAR (b, zv_marker)));
   744 
   745       XSETBUFFER (buffer, b);
   746       set_marker_both (BVAR (b, pt_marker), buffer, b->pt, b->pt_byte);
   747       set_marker_both (BVAR (b, begv_marker), buffer, b->begv, b->begv_byte);
   748       set_marker_both (BVAR (b, zv_marker), buffer, b->zv, b->zv_byte);
   749     }
   750 }
   751 
   752 
   753 /* If buffer B has markers to record PT, BEGV and ZV when it is not
   754    current, fetch these values into B->begv etc.  */
   755 
   756 static void
   757 fetch_buffer_markers (struct buffer *b)
   758 {
   759   if (! NILP (BVAR (b, pt_marker)))
   760     {
   761       Lisp_Object m;
   762 
   763       eassert (!NILP (BVAR (b, begv_marker)));
   764       eassert (!NILP (BVAR (b, zv_marker)));
   765 
   766       m = BVAR (b, pt_marker);
   767       SET_BUF_PT_BOTH (b, marker_position (m), marker_byte_position (m));
   768 
   769       m = BVAR (b, begv_marker);
   770       SET_BUF_BEGV_BOTH (b, marker_position (m), marker_byte_position (m));
   771 
   772       m = BVAR (b, zv_marker);
   773       SET_BUF_ZV_BOTH (b, marker_position (m), marker_byte_position (m));
   774     }
   775 }
   776 
   777 
   778 DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
   779        2, 4,
   780        "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
   781        doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
   782 BASE-BUFFER should be a live buffer, or the name of an existing buffer.
   783 
   784 NAME should be a string which is not the name of an existing buffer.
   785 Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
   786 such as major and minor modes, in the indirect buffer.
   787 
   788 CLONE nil means the indirect buffer's state is reset to default values.
   789 
   790 If optional argument INHIBIT-BUFFER-HOOKS is non-nil, the new buffer
   791 does not run the hooks `kill-buffer-hook',
   792 `kill-buffer-query-functions', and `buffer-list-update-hook'.  */)
   793   (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone,
   794    Lisp_Object inhibit_buffer_hooks)
   795 {
   796   Lisp_Object buf, tem;
   797   struct buffer *b;
   798 
   799   CHECK_STRING (name);
   800   buf = Fget_buffer (name);
   801   if (!NILP (buf))
   802     error ("Buffer name `%s' is in use", SDATA (name));
   803 
   804   tem = base_buffer;
   805   base_buffer = Fget_buffer (base_buffer);
   806   if (NILP (base_buffer))
   807     error ("No such buffer: `%s'", SDATA (tem));
   808   if (!BUFFER_LIVE_P (XBUFFER (base_buffer)))
   809     error ("Base buffer has been killed");
   810 
   811   if (SCHARS (name) == 0)
   812     error ("Empty string for buffer name is not allowed");
   813 
   814   b = allocate_buffer ();
   815 
   816   /* No double indirection - if base buffer is indirect,
   817      new buffer becomes an indirect to base's base.  */
   818   b->base_buffer = (XBUFFER (base_buffer)->base_buffer
   819                     ? XBUFFER (base_buffer)->base_buffer
   820                     : XBUFFER (base_buffer));
   821 
   822   /* Use the base buffer's text object.  */
   823   b->text = b->base_buffer->text;
   824   /* We have no own text.  */
   825   b->indirections = -1;
   826   /* Notify base buffer that we share the text now.  */
   827   b->base_buffer->indirections++;
   828   /* Always -1 for an indirect buffer.  */
   829   b->window_count = -1;
   830 
   831   memset (&b->local_flags, 0, sizeof (b->local_flags));
   832 
   833   b->pt = b->base_buffer->pt;
   834   b->begv = b->base_buffer->begv;
   835   b->zv = b->base_buffer->zv;
   836   b->pt_byte = b->base_buffer->pt_byte;
   837   b->begv_byte = b->base_buffer->begv_byte;
   838   b->zv_byte = b->base_buffer->zv_byte;
   839   b->inhibit_buffer_hooks = !NILP (inhibit_buffer_hooks);
   840 
   841   b->newline_cache = 0;
   842   b->width_run_cache = 0;
   843   b->bidi_paragraph_cache = 0;
   844   bset_width_table (b, Qnil);
   845 
   846   name = Fcopy_sequence (name);
   847   set_string_intervals (name, NULL);
   848   bset_name (b, name);
   849 
   850   /* An indirect buffer shares undo list of its base (Bug#18180).  */
   851   bset_undo_list (b, BVAR (b->base_buffer, undo_list));
   852 
   853   reset_buffer (b);
   854   reset_buffer_local_variables (b, 1);
   855 
   856   /* Put this in the alist of all live buffers.  */
   857   XSETBUFFER (buf, b);
   858   Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buf)));
   859 
   860   bset_mark (b, Fmake_marker ());
   861 
   862   /* The multibyte status belongs to the base buffer.  */
   863   bset_enable_multibyte_characters
   864     (b, BVAR (b->base_buffer, enable_multibyte_characters));
   865 
   866   /* Make sure the base buffer has markers for its narrowing.  */
   867   if (NILP (BVAR (b->base_buffer, pt_marker)))
   868     {
   869       eassert (NILP (BVAR (b->base_buffer, begv_marker)));
   870       eassert (NILP (BVAR (b->base_buffer, zv_marker)));
   871 
   872       bset_pt_marker (b->base_buffer,
   873                       build_marker (b->base_buffer, b->base_buffer->pt,
   874                                     b->base_buffer->pt_byte));
   875 
   876       bset_begv_marker (b->base_buffer,
   877                         build_marker (b->base_buffer, b->base_buffer->begv,
   878                                       b->base_buffer->begv_byte));
   879 
   880       bset_zv_marker (b->base_buffer,
   881                       build_marker (b->base_buffer, b->base_buffer->zv,
   882                                     b->base_buffer->zv_byte));
   883 
   884       XMARKER (BVAR (b->base_buffer, zv_marker))->insertion_type = 1;
   885     }
   886 
   887   if (NILP (clone))
   888     {
   889       /* Give the indirect buffer markers for its narrowing.  */
   890       bset_pt_marker (b, build_marker (b, b->pt, b->pt_byte));
   891       bset_begv_marker (b, build_marker (b, b->begv, b->begv_byte));
   892       bset_zv_marker (b, build_marker (b, b->zv, b->zv_byte));
   893       XMARKER (BVAR (b, zv_marker))->insertion_type = 1;
   894     }
   895   else
   896     {
   897       struct buffer *old_b = current_buffer;
   898 
   899       clone_per_buffer_values (b->base_buffer, b);
   900       bset_filename (b, Qnil);
   901       bset_file_truename (b, Qnil);
   902       bset_display_count (b, make_fixnum (0));
   903       bset_backed_up (b, Qnil);
   904       bset_local_minor_modes (b, Qnil);
   905       bset_auto_save_file_name (b, Qnil);
   906       set_buffer_internal_1 (b);
   907       Fset (intern ("buffer-save-without-query"), Qnil);
   908       Fset (intern ("buffer-file-number"), Qnil);
   909       if (!NILP (Flocal_variable_p (Qbuffer_stale_function, base_buffer)))
   910         Fkill_local_variable (Qbuffer_stale_function);
   911       /* Cloned buffers need extra setup, to do things such as deep
   912          variable copies for list variables that might be mangled due
   913          to destructive operations in the indirect buffer. */
   914       run_hook (Qclone_indirect_buffer_hook);
   915       set_buffer_internal_1 (old_b);
   916     }
   917 
   918   run_buffer_list_update_hook (b);
   919 
   920   return buf;
   921 }
   922 
   923 static void
   924 remove_buffer_overlay (struct buffer *b, struct Lisp_Overlay *ov)
   925 {
   926   eassert (b->overlays);
   927   eassert (ov->buffer == b);
   928   itree_remove (ov->buffer->overlays, ov->interval);
   929   ov->buffer = NULL;
   930 }
   931 
   932 /* Mark OV as no longer associated with its buffer.  */
   933 
   934 static void
   935 drop_overlay (struct Lisp_Overlay *ov)
   936 {
   937   if (! ov->buffer)
   938     return;
   939 
   940   modify_overlay (ov->buffer, overlay_start (ov), overlay_end (ov));
   941   remove_buffer_overlay (ov->buffer, ov);
   942 }
   943 
   944 /* Delete all overlays of B and reset its overlay lists.  */
   945 
   946 void
   947 delete_all_overlays (struct buffer *b)
   948 {
   949   struct itree_node *node;
   950 
   951   if (! b->overlays)
   952     return;
   953 
   954   /* The general rule is that the tree cannot be modified from within
   955      ITREE_FOREACH, but here we bend this rule a little because we know
   956      that the POST_ORDER iterator will not need to look at `node` again.  */
   957   ITREE_FOREACH (node, b->overlays, PTRDIFF_MIN, PTRDIFF_MAX, POST_ORDER)
   958     {
   959       modify_overlay (b, node->begin, node->end);
   960       XOVERLAY (node->data)->buffer = NULL;
   961       node->parent = NULL;
   962       node->left = NULL;
   963       node->right = NULL;
   964     }
   965   itree_clear (b->overlays);
   966 }
   967 
   968 static void
   969 free_buffer_overlays (struct buffer *b)
   970 {
   971   /* Actually this does not free any overlay, but the tree only.  --ap */
   972   if (b->overlays)
   973     {
   974       itree_destroy (b->overlays);
   975       b->overlays = NULL;
   976     }
   977 }
   978 
   979 /* Adjust the position of overlays in the current buffer according to
   980    MULTIBYTE.
   981 
   982    Assume that positions currently correspond to byte positions, if
   983    MULTIBYTE is true and to character positions if not.
   984 */
   985 
   986 static void
   987 set_overlays_multibyte (bool multibyte)
   988 {
   989   if (! current_buffer->overlays || Z == Z_BYTE)
   990     return;
   991 
   992   struct itree_node **nodes = NULL;
   993   struct itree_tree *tree = current_buffer->overlays;
   994   const intmax_t size = itree_size (tree);
   995 
   996   /* We can't use `itree_node_set_region` at the same time
   997      as we iterate over the itree, so we need an auxiliary storage
   998      to keep the list of nodes.  */
   999   USE_SAFE_ALLOCA;
  1000   SAFE_NALLOCA (nodes, 1, size);
  1001   {
  1002     struct itree_node *node, **cursor = nodes;
  1003     ITREE_FOREACH (node, tree, PTRDIFF_MIN, PTRDIFF_MAX, ASCENDING)
  1004       *(cursor++) = node;
  1005   }
  1006 
  1007   for (int i = 0; i < size; ++i, ++nodes)
  1008     {
  1009       struct itree_node * const node = *nodes;
  1010 
  1011       if (multibyte)
  1012         {
  1013           ptrdiff_t begin = itree_node_begin (tree, node);
  1014           ptrdiff_t end = itree_node_end (tree, node);
  1015 
  1016           /* This models the behavior of markers.  (The behavior of
  1017              text-intervals differs slightly.) */
  1018           while (begin < Z_BYTE
  1019                  && !CHAR_HEAD_P (FETCH_BYTE (begin)))
  1020             begin++;
  1021           while (end < Z_BYTE
  1022                  && !CHAR_HEAD_P (FETCH_BYTE (end)))
  1023             end++;
  1024           itree_node_set_region (tree, node, BYTE_TO_CHAR (begin),
  1025                                     BYTE_TO_CHAR (end));
  1026         }
  1027       else
  1028         {
  1029           itree_node_set_region (tree, node, CHAR_TO_BYTE (node->begin),
  1030                                     CHAR_TO_BYTE (node->end));
  1031         }
  1032     }
  1033   SAFE_FREE ();
  1034 }
  1035 
  1036 /* Reinitialize everything about a buffer except its name and contents
  1037    and local variables.
  1038    If called on an already-initialized buffer, the list of overlays
  1039    should be deleted before calling this function, otherwise we end up
  1040    with overlays that claim to belong to the buffer but the buffer
  1041    claims it doesn't belong to it.  */
  1042 
  1043 void
  1044 reset_buffer (register struct buffer *b)
  1045 {
  1046   bset_filename (b, Qnil);
  1047   bset_file_truename (b, Qnil);
  1048   bset_directory (b, current_buffer ? BVAR (current_buffer, directory) : Qnil);
  1049   b->modtime = make_timespec (0, UNKNOWN_MODTIME_NSECS);
  1050   b->modtime_size = -1;
  1051   XSETFASTINT (BVAR (b, save_length), 0);
  1052   b->last_window_start = 1;
  1053   /* It is more conservative to start out "changed" than "unchanged".  */
  1054   b->clip_changed = 0;
  1055   b->prevent_redisplay_optimizations_p = 1;
  1056   b->long_line_optimizations_p = 0;
  1057   bset_backed_up (b, Qnil);
  1058   bset_local_minor_modes (b, Qnil);
  1059   BUF_AUTOSAVE_MODIFF (b) = 0;
  1060   b->auto_save_failure_time = 0;
  1061   bset_auto_save_file_name (b, Qnil);
  1062   bset_read_only (b, Qnil);
  1063   b->overlays = NULL;
  1064   bset_mark_active (b, Qnil);
  1065   bset_point_before_scroll (b, Qnil);
  1066   bset_file_format (b, Qnil);
  1067   bset_auto_save_file_format (b, Qt);
  1068   bset_last_selected_window (b, Qnil);
  1069   bset_display_count (b, make_fixnum (0));
  1070   bset_display_time (b, Qnil);
  1071   bset_enable_multibyte_characters
  1072     (b, BVAR (&buffer_defaults, enable_multibyte_characters));
  1073   bset_cursor_type (b, BVAR (&buffer_defaults, cursor_type));
  1074   bset_extra_line_spacing (b, BVAR (&buffer_defaults, extra_line_spacing));
  1075 #ifdef HAVE_TREE_SITTER
  1076   bset_ts_parser_list (b, Qnil);
  1077 #endif
  1078 
  1079   b->display_error_modiff = 0;
  1080 }
  1081 
  1082 /* Reset buffer B's local variables info.
  1083    Don't use this on a buffer that has already been in use;
  1084    it does not treat permanent locals consistently.
  1085    Instead, use Fkill_all_local_variables.
  1086 
  1087    If PERMANENT_TOO, reset permanent buffer-local variables.
  1088    If not, preserve those.  */
  1089 
  1090 static void
  1091 reset_buffer_local_variables (struct buffer *b, bool permanent_too)
  1092 {
  1093   int offset, i;
  1094 
  1095   /* Reset the major mode to Fundamental, together with all the
  1096      things that depend on the major mode.
  1097      default-major-mode is handled at a higher level.
  1098      We ignore it here.  */
  1099   bset_major_mode (b, Qfundamental_mode);
  1100   bset_keymap (b, Qnil);
  1101   bset_mode_name (b, QSFundamental);
  1102 
  1103   /* If the standard case table has been altered and invalidated,
  1104      fix up its insides first.  */
  1105   if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0])
  1106          && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1])
  1107          && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2])))
  1108     Fset_standard_case_table (Vascii_downcase_table);
  1109 
  1110   bset_downcase_table (b, Vascii_downcase_table);
  1111   bset_upcase_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[0]);
  1112   bset_case_canon_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[1]);
  1113   bset_case_eqv_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[2]);
  1114   bset_invisibility_spec (b, Qt);
  1115 
  1116   /* Reset all (or most) per-buffer variables to their defaults.  */
  1117   if (permanent_too)
  1118     bset_local_var_alist (b, Qnil);
  1119   else
  1120     {
  1121       Lisp_Object tmp, last = Qnil;
  1122       Lisp_Object buffer;
  1123       XSETBUFFER (buffer, b);
  1124 
  1125       for (tmp = BVAR (b, local_var_alist); CONSP (tmp); tmp = XCDR (tmp))
  1126         {
  1127           Lisp_Object local_var = XCAR (XCAR (tmp));
  1128           Lisp_Object prop = Fget (local_var, Qpermanent_local);
  1129           Lisp_Object sym = local_var;
  1130 
  1131           /* Watchers are run *before* modifying the var.  */
  1132           if (XSYMBOL (local_var)->u.s.trapped_write == SYMBOL_TRAPPED_WRITE)
  1133             notify_variable_watchers (local_var, Qnil,
  1134                                       Qmakunbound, Fcurrent_buffer ());
  1135 
  1136           eassert (XSYMBOL (sym)->u.s.redirect == SYMBOL_LOCALIZED);
  1137           /* Need not do anything if some other buffer's binding is
  1138              now cached.  */
  1139           if (BASE_EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
  1140             {
  1141               /* Symbol is set up for this buffer's old local value:
  1142                  swap it out!  */
  1143               swap_in_global_binding (XSYMBOL (sym));
  1144             }
  1145 
  1146           if (!NILP (prop))
  1147             {
  1148               /* If permanent-local, keep it.  */
  1149               last = tmp;
  1150               if (EQ (prop, Qpermanent_local_hook))
  1151                 {
  1152                   /* This is a partially permanent hook variable.
  1153                      Preserve only the elements that want to be preserved.  */
  1154                   Lisp_Object list, newlist;
  1155                   list = XCDR (XCAR (tmp));
  1156                   if (!CONSP (list))
  1157                     newlist = list;
  1158                   else
  1159                     for (newlist = Qnil; CONSP (list); list = XCDR (list))
  1160                       {
  1161                         Lisp_Object elt = XCAR (list);
  1162                         /* Preserve element ELT if it's t, or if it is a
  1163                            function with a `permanent-local-hook'
  1164                            property. */
  1165                         if (EQ (elt, Qt)
  1166                             || (SYMBOLP (elt)
  1167                                 && !NILP (Fget (elt, Qpermanent_local_hook))))
  1168                           newlist = Fcons (elt, newlist);
  1169                       }
  1170                   newlist = Fnreverse (newlist);
  1171                   if (XSYMBOL (local_var)->u.s.trapped_write
  1172                       == SYMBOL_TRAPPED_WRITE)
  1173                     notify_variable_watchers (local_var, newlist,
  1174                                               Qmakunbound, Fcurrent_buffer ());
  1175                   XSETCDR (XCAR (tmp), newlist);
  1176                   continue; /* Don't do variable write trapping twice.  */
  1177                 }
  1178             }
  1179           /* Delete this local variable.  */
  1180           else if (NILP (last))
  1181             bset_local_var_alist (b, XCDR (tmp));
  1182           else
  1183             XSETCDR (last, XCDR (tmp));
  1184         }
  1185     }
  1186 
  1187   for (i = 0; i < last_per_buffer_idx; ++i)
  1188     if (permanent_too || buffer_permanent_local_flags[i] == 0)
  1189       SET_PER_BUFFER_VALUE_P (b, i, 0);
  1190 
  1191   /* For each slot that has a default value, copy that into the slot.  */
  1192   FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
  1193     {
  1194       int idx = PER_BUFFER_IDX (offset);
  1195       if ((idx > 0
  1196            && (permanent_too
  1197                || buffer_permanent_local_flags[idx] == 0)))
  1198         set_per_buffer_value (b, offset, per_buffer_default (offset));
  1199     }
  1200 }
  1201 
  1202 /* We split this away from generate-new-buffer, because rename-buffer
  1203    and set-visited-file-name ought to be able to use this to really
  1204    rename the buffer properly.  */
  1205 
  1206 DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name,
  1207        Sgenerate_new_buffer_name, 1, 2, 0,
  1208        doc: /* Return a string that is the name of no existing buffer based on NAME.
  1209 If there is no live buffer named NAME, then return NAME.
  1210 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
  1211 \(starting at 2) until an unused name is found, and then return that name.
  1212 Optional second argument IGNORE specifies a name that is okay to use (if
  1213 it is in the sequence to be tried) even if a buffer with that name exists.
  1214 
  1215 If NAME begins with a space (i.e., a buffer that is not normally
  1216 visible to users), then if buffer NAME already exists a random number
  1217 is first appended to NAME, to speed up finding a non-existent buffer.  */)
  1218   (Lisp_Object name, Lisp_Object ignore)
  1219 {
  1220   Lisp_Object genbase;
  1221 
  1222   CHECK_STRING (name);
  1223 
  1224   if ((!NILP (ignore) && !NILP (Fstring_equal (name, ignore)))
  1225       || NILP (Fget_buffer (name)))
  1226     return name;
  1227 
  1228   if (SREF (name, 0) != ' ') /* See bug#1229.  */
  1229     genbase = name;
  1230   else
  1231     {
  1232       enum { bug_52711 = true };  /* https://bugs.gnu.org/57211 */
  1233       char number[bug_52711 ? INT_BUFSIZE_BOUND (int) + 1 : sizeof "-999999"];
  1234       EMACS_INT r = get_random ();
  1235       eassume (0 <= r);
  1236       int i = r % 1000000;
  1237       AUTO_STRING_WITH_LEN (lnumber, number, sprintf (number, "-%d", i));
  1238       genbase = concat2 (name, lnumber);
  1239       if (NILP (Fget_buffer (genbase)))
  1240         return genbase;
  1241     }
  1242 
  1243   for (ptrdiff_t count = 2; ; count++)
  1244     {
  1245       char number[INT_BUFSIZE_BOUND (ptrdiff_t) + sizeof "<>"];
  1246       AUTO_STRING_WITH_LEN (lnumber, number,
  1247                             sprintf (number, "<%"pD"d>", count));
  1248       Lisp_Object gentemp = concat2 (genbase, lnumber);
  1249       if (!NILP (Fstring_equal (gentemp, ignore))
  1250           || NILP (Fget_buffer (gentemp)))
  1251         return gentemp;
  1252     }
  1253 }
  1254 
  1255 
  1256 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
  1257        doc: /* Return the name of BUFFER, as a string.
  1258 BUFFER defaults to the current buffer.
  1259 Return nil if BUFFER has been killed.  */)
  1260   (register Lisp_Object buffer)
  1261 {
  1262   return BVAR (decode_buffer (buffer), name);
  1263 }
  1264 
  1265 DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0,
  1266        doc: /* Return name of file BUFFER is visiting, or nil if none.
  1267 No argument or nil as argument means use the current buffer.  */)
  1268   (register Lisp_Object buffer)
  1269 {
  1270   return BVAR (decode_buffer (buffer), filename);
  1271 }
  1272 
  1273 DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
  1274        0, 1, 0,
  1275        doc: /* Return the base buffer of indirect buffer BUFFER.
  1276 If BUFFER is not indirect, return nil.
  1277 BUFFER defaults to the current buffer.  */)
  1278   (register Lisp_Object buffer)
  1279 {
  1280   struct buffer *base = decode_buffer (buffer)->base_buffer;
  1281   return base ? (XSETBUFFER (buffer, base), buffer) : Qnil;
  1282 }
  1283 
  1284 DEFUN ("buffer-local-value", Fbuffer_local_value,
  1285        Sbuffer_local_value, 2, 2, 0,
  1286        doc: /* Return the value of VARIABLE in BUFFER.
  1287 If VARIABLE does not have a buffer-local binding in BUFFER, the value
  1288 is the default binding of the variable.  */)
  1289   (register Lisp_Object variable, register Lisp_Object buffer)
  1290 {
  1291   register Lisp_Object result = buffer_local_value (variable, buffer);
  1292 
  1293   if (BASE_EQ (result, Qunbound))
  1294     xsignal1 (Qvoid_variable, variable);
  1295 
  1296   return result;
  1297 }
  1298 
  1299 
  1300 /* Like Fbuffer_local_value, but return Qunbound if the variable is
  1301    locally unbound.  */
  1302 
  1303 Lisp_Object
  1304 buffer_local_value (Lisp_Object variable, Lisp_Object buffer)
  1305 {
  1306   register struct buffer *buf;
  1307   register Lisp_Object result;
  1308   struct Lisp_Symbol *sym;
  1309 
  1310   CHECK_SYMBOL (variable);
  1311   CHECK_BUFFER (buffer);
  1312   buf = XBUFFER (buffer);
  1313   sym = XSYMBOL (variable);
  1314 
  1315  start:
  1316   switch (sym->u.s.redirect)
  1317     {
  1318     case SYMBOL_VARALIAS: sym = SYMBOL_ALIAS (sym); goto start;
  1319     case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break;
  1320     case SYMBOL_LOCALIZED:
  1321       { /* Look in local_var_alist.  */
  1322         struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
  1323         XSETSYMBOL (variable, sym); /* Update In case of aliasing.  */
  1324         result = assq_no_quit (variable, BVAR (buf, local_var_alist));
  1325         if (!NILP (result))
  1326           {
  1327             if (blv->fwd.fwdptr)
  1328               { /* What binding is loaded right now?  */
  1329                 Lisp_Object current_alist_element = blv->valcell;
  1330 
  1331                 /* The value of the currently loaded binding is not
  1332                    stored in it, but rather in the realvalue slot.
  1333                    Store that value into the binding it belongs to
  1334                    in case that is the one we are about to use.  */
  1335 
  1336                 XSETCDR (current_alist_element,
  1337                          do_symval_forwarding (blv->fwd));
  1338               }
  1339             /* Now get the (perhaps updated) value out of the binding.  */
  1340             result = XCDR (result);
  1341           }
  1342         else
  1343           result = Fdefault_value (variable);
  1344         break;
  1345       }
  1346     case SYMBOL_FORWARDED:
  1347       {
  1348         lispfwd fwd = SYMBOL_FWD (sym);
  1349         if (BUFFER_OBJFWDP (fwd))
  1350           result = per_buffer_value (buf, XBUFFER_OBJFWD (fwd)->offset);
  1351         else
  1352           result = Fdefault_value (variable);
  1353         break;
  1354       }
  1355     default: emacs_abort ();
  1356     }
  1357 
  1358   return result;
  1359 }
  1360 
  1361 /* Return an alist of the Lisp-level buffer-local bindings of
  1362    buffer BUF.  That is, don't include the variables maintained
  1363    in special slots in the buffer object.
  1364    If not CLONE, replace elements of the form (VAR . unbound)
  1365    by VAR.  */
  1366 
  1367 static Lisp_Object
  1368 buffer_lisp_local_variables (struct buffer *buf, bool clone)
  1369 {
  1370   Lisp_Object result = Qnil;
  1371   Lisp_Object tail;
  1372   for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
  1373     {
  1374       Lisp_Object val, elt;
  1375 
  1376       elt = XCAR (tail);
  1377 
  1378       /* Reference each variable in the alist in buf.
  1379          If inquiring about the current buffer, this gets the current values,
  1380          so store them into the alist so the alist is up to date.
  1381          If inquiring about some other buffer, this swaps out any values
  1382          for that buffer, making the alist up to date automatically.  */
  1383       val = find_symbol_value (XCAR (elt));
  1384       /* Use the current buffer value only if buf is the current buffer.  */
  1385       if (buf != current_buffer)
  1386         val = XCDR (elt);
  1387 
  1388       result = Fcons (!clone && BASE_EQ (val, Qunbound)
  1389                       ? XCAR (elt)
  1390                       : Fcons (XCAR (elt), val),
  1391                       result);
  1392     }
  1393 
  1394   return result;
  1395 }
  1396 
  1397 
  1398 /* If the variable at position index OFFSET in buffer BUF has a
  1399    buffer-local value, return (name . value).  If SYM is non-nil,
  1400    it replaces name.  */
  1401 
  1402 static Lisp_Object
  1403 buffer_local_variables_1 (struct buffer *buf, int offset, Lisp_Object sym)
  1404 {
  1405   int idx = PER_BUFFER_IDX (offset);
  1406   if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
  1407       && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
  1408     {
  1409       sym = NILP (sym) ? PER_BUFFER_SYMBOL (offset) : sym;
  1410       Lisp_Object val = per_buffer_value (buf, offset);
  1411       return BASE_EQ (val, Qunbound) ? sym : Fcons (sym, val);
  1412     }
  1413   return Qnil;
  1414 }
  1415 
  1416 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
  1417        Sbuffer_local_variables, 0, 1, 0,
  1418        doc: /* Return an alist of variables that are buffer-local in BUFFER.
  1419 Most elements look like (SYMBOL . VALUE), describing one variable.
  1420 For a symbol that is locally unbound, just the symbol appears in the value.
  1421 Note that storing new VALUEs in these elements doesn't change the variables.
  1422 No argument or nil as argument means use current buffer as BUFFER.  */)
  1423   (Lisp_Object buffer)
  1424 {
  1425   struct buffer *buf = decode_buffer (buffer);
  1426   Lisp_Object result = buffer_lisp_local_variables (buf, 0);
  1427   Lisp_Object tem;
  1428 
  1429   /* Add on all the variables stored in special slots.  */
  1430   {
  1431     int offset;
  1432 
  1433     FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
  1434       {
  1435         tem = buffer_local_variables_1 (buf, offset, Qnil);
  1436         if (!NILP (tem))
  1437           result = Fcons (tem, result);
  1438       }
  1439   }
  1440 
  1441   tem = buffer_local_variables_1 (buf, PER_BUFFER_VAR_OFFSET (undo_list),
  1442                                   intern ("buffer-undo-list"));
  1443   if (!NILP (tem))
  1444     result = Fcons (tem, result);
  1445 
  1446   return result;
  1447 }
  1448 
  1449 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
  1450        0, 1, 0,
  1451        doc: /* Return non-nil if BUFFER was modified since its file was last read or saved.
  1452 No argument or nil as argument means use current buffer as BUFFER.
  1453 
  1454 If BUFFER was autosaved since it was last modified, this function
  1455 returns the symbol `autosaved'.  */)
  1456   (Lisp_Object buffer)
  1457 {
  1458   struct buffer *buf = decode_buffer (buffer);
  1459   if (BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf))
  1460     {
  1461       if (BUF_AUTOSAVE_MODIFF (buf) == BUF_MODIFF (buf))
  1462         return Qautosaved;
  1463       else
  1464         return Qt;
  1465     }
  1466   else
  1467     return Qnil;
  1468 }
  1469 
  1470 DEFUN ("force-mode-line-update", Fforce_mode_line_update,
  1471        Sforce_mode_line_update, 0, 1, 0,
  1472        doc: /* Force redisplay of the current buffer's mode line and header line.
  1473 With optional non-nil ALL, force redisplay of all mode lines, tab lines and
  1474 header lines.  This function also forces recomputation of the
  1475 menu bar menus and the frame title.  */)
  1476      (Lisp_Object all)
  1477 {
  1478   if (!NILP (all))
  1479     {
  1480       update_mode_lines = 10;
  1481       /* FIXME: This can't be right.  */
  1482       current_buffer->prevent_redisplay_optimizations_p = true;
  1483     }
  1484   else if (buffer_window_count (current_buffer))
  1485     {
  1486       bset_update_mode_line (current_buffer);
  1487       current_buffer->prevent_redisplay_optimizations_p = true;
  1488     }
  1489   return all;
  1490 }
  1491 
  1492 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
  1493        1, 1, 0,
  1494        doc: /* Mark current buffer as modified or unmodified according to FLAG.
  1495 A non-nil FLAG means mark the buffer modified.
  1496 In addition, this function unconditionally forces redisplay of the
  1497 mode lines of the windows that display the current buffer, and also
  1498 locks or unlocks the file visited by the buffer, depending on whether
  1499 the function's argument is non-nil, but only if both `buffer-file-name'
  1500 and `buffer-file-truename' are non-nil.  */)
  1501   (Lisp_Object flag)
  1502 {
  1503   Frestore_buffer_modified_p (flag);
  1504 
  1505   /* Set update_mode_lines only if buffer is displayed in some window.
  1506      Packages like jit-lock or lazy-lock preserve a buffer's modified
  1507      state by recording/restoring the state around blocks of code.
  1508      Setting update_mode_lines makes redisplay consider all windows
  1509      (on all frames).  Stealth fontification of buffers not displayed
  1510      would incur additional redisplay costs if we'd set
  1511      update_modes_lines unconditionally.
  1512 
  1513      Ideally, I think there should be another mechanism for fontifying
  1514      buffers without "modifying" buffers, or redisplay should be
  1515      smarter about updating the `*' in mode lines.  --gerd  */
  1516   return Fforce_mode_line_update (Qnil);
  1517 }
  1518 
  1519 DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
  1520        Srestore_buffer_modified_p, 1, 1, 0,
  1521        doc: /* Like `set-buffer-modified-p', but doesn't redisplay buffer's mode line.
  1522 A nil FLAG means to mark the buffer as unmodified.  A non-nil FLAG
  1523 means mark the buffer as modified.  A special value of `autosaved'
  1524 will mark the buffer as modified and also as autosaved since it was
  1525 last modified.
  1526 
  1527 This function also locks or unlocks the file visited by the buffer,
  1528 if both `buffer-file-truename' and `buffer-file-name' are non-nil.
  1529 
  1530 It is not ensured that mode lines will be updated to show the modified
  1531 state of the current buffer.  Use with care.  */)
  1532   (Lisp_Object flag)
  1533 {
  1534 
  1535   /* If buffer becoming modified, lock the file.
  1536      If buffer becoming unmodified, unlock the file.  */
  1537 
  1538   struct buffer *b = current_buffer->base_buffer
  1539     ? current_buffer->base_buffer
  1540     : current_buffer;
  1541 
  1542   if (!inhibit_modification_hooks)
  1543     {
  1544       Lisp_Object fn = BVAR (b, file_truename);
  1545       /* Test buffer-file-name so that binding it to nil is effective.  */
  1546       if (!NILP (fn) && ! NILP (BVAR (b, filename)))
  1547         {
  1548           bool already = SAVE_MODIFF < MODIFF;
  1549           if (!already && !NILP (flag))
  1550             Flock_file (fn);
  1551           else if (already && NILP (flag))
  1552             Funlock_file (fn);
  1553         }
  1554     }
  1555 
  1556   /* Here we have a problem.  SAVE_MODIFF is used here to encode
  1557      buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as
  1558      recent-auto-save-p (as SAVE_MODIFF<auto_save_modified).  So if we
  1559      modify SAVE_MODIFF to affect one, we may affect the other
  1560      as well.
  1561      E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but
  1562      if SAVE_MODIFF<auto_save_modified that means we risk changing
  1563      recent-auto-save-p from t to nil.
  1564      Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
  1565      we risk changing recent-auto-save-p from nil to t.  */
  1566   if (NILP (flag))
  1567     /* This unavoidably sets recent-auto-save-p to nil.  */
  1568     SAVE_MODIFF = MODIFF;
  1569   else
  1570     {
  1571       /* If SAVE_MODIFF == auto_save_modified == MODIFF, we can either
  1572          decrease SAVE_MODIFF and auto_save_modified or increase
  1573          MODIFF.  */
  1574       if (SAVE_MODIFF >= MODIFF)
  1575         SAVE_MODIFF = modiff_incr (&MODIFF, 1);
  1576       if (EQ (flag, Qautosaved))
  1577         BUF_AUTOSAVE_MODIFF (b) = MODIFF;
  1578     }
  1579   return flag;
  1580 }
  1581 
  1582 DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
  1583        0, 1, 0,
  1584        doc: /* Return BUFFER's tick counter, incremented for each change in text.
  1585 Each buffer has a tick counter which is incremented each time the
  1586 text in that buffer is changed.  No argument or nil as argument means
  1587 use current buffer as BUFFER.  */)
  1588   (Lisp_Object buffer)
  1589 {
  1590   return modiff_to_integer (BUF_MODIFF (decode_buffer (buffer)));
  1591 }
  1592 
  1593 DEFUN ("internal--set-buffer-modified-tick",
  1594        Finternal__set_buffer_modified_tick, Sinternal__set_buffer_modified_tick,
  1595        1, 2, 0,
  1596        doc: /* Set BUFFER's tick counter to TICK.
  1597 No argument or nil as argument means use current buffer as BUFFER.  */)
  1598   (Lisp_Object tick, Lisp_Object buffer)
  1599 {
  1600   CHECK_FIXNUM (tick);
  1601   BUF_MODIFF (decode_buffer (buffer)) = XFIXNUM (tick);
  1602   return Qnil;
  1603 }
  1604 
  1605 DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
  1606        Sbuffer_chars_modified_tick, 0, 1, 0,
  1607        doc: /* Return BUFFER's character-change tick counter.
  1608 Each buffer has a character-change tick counter, which is set to the
  1609 value of the buffer's tick counter (see `buffer-modified-tick'), each
  1610 time text in that buffer is inserted or deleted.  By comparing the
  1611 values returned by two individual calls of `buffer-chars-modified-tick',
  1612 you can tell whether a character change occurred in that buffer in
  1613 between these calls.  No argument or nil as argument means use current
  1614 buffer as BUFFER.  */)
  1615   (Lisp_Object buffer)
  1616 {
  1617   return modiff_to_integer (BUF_CHARS_MODIFF (decode_buffer (buffer)));
  1618 }
  1619 
  1620 DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
  1621        "(list (read-string \"Rename buffer (to new name): \" \
  1622               nil 'buffer-name-history (buffer-name (current-buffer))) \
  1623               current-prefix-arg)",
  1624        doc: /* Change current buffer's name to NEWNAME (a string).
  1625 If second arg UNIQUE is nil or omitted, it is an error if a
  1626 buffer named NEWNAME already exists.
  1627 If UNIQUE is non-nil, come up with a new name using
  1628 `generate-new-buffer-name'.
  1629 Interactively, you can set UNIQUE with a prefix argument.
  1630 We return the name we actually gave the buffer.
  1631 This does not change the name of the visited file (if any).  */)
  1632   (register Lisp_Object newname, Lisp_Object unique)
  1633 {
  1634   register Lisp_Object tem, buf;
  1635   Lisp_Object requestedname = newname;
  1636 
  1637   CHECK_STRING (newname);
  1638 
  1639   if (SCHARS (newname) == 0)
  1640     error ("Empty string is invalid as a buffer name");
  1641 
  1642   tem = Fget_buffer (newname);
  1643   if (!NILP (tem))
  1644     {
  1645       /* Don't short-circuit if UNIQUE is t.  That is a useful way to
  1646          rename the buffer automatically so you can create another
  1647          with the original name.  It makes UNIQUE equivalent to
  1648          (rename-buffer (generate-new-buffer-name NEWNAME)).  */
  1649       if (NILP (unique) && XBUFFER (tem) == current_buffer)
  1650         return BVAR (current_buffer, name);
  1651       if (!NILP (unique))
  1652         newname = Fgenerate_new_buffer_name (newname,
  1653                                              BVAR (current_buffer, name));
  1654       else
  1655         error ("Buffer name `%s' is in use", SDATA (newname));
  1656     }
  1657 
  1658   bset_name (current_buffer, newname);
  1659 
  1660   /* Catch redisplay's attention.  Unless we do this, the mode lines for
  1661      any windows displaying current_buffer will stay unchanged.  */
  1662   bset_update_mode_line (current_buffer);
  1663 
  1664   XSETBUFFER (buf, current_buffer);
  1665   Fsetcar (Frassq (buf, Vbuffer_alist), newname);
  1666   if (NILP (BVAR (current_buffer, filename))
  1667       && !NILP (BVAR (current_buffer, auto_save_file_name)))
  1668     call0 (intern ("rename-auto-save-file"));
  1669 
  1670   run_buffer_list_update_hook (current_buffer);
  1671 
  1672   call2 (intern ("uniquify--rename-buffer-advice"),
  1673          requestedname, unique);
  1674 
  1675   /* Refetch since that last call may have done GC.  */
  1676   return BVAR (current_buffer, name);
  1677 }
  1678 
  1679 /* True if B can be used as 'other-than-BUFFER' buffer.  */
  1680 
  1681 static bool
  1682 candidate_buffer (Lisp_Object b, Lisp_Object buffer)
  1683 {
  1684   return (BUFFERP (b) && !BASE_EQ (b, buffer)
  1685           && BUFFER_LIVE_P (XBUFFER (b))
  1686           && !BUFFER_HIDDEN_P (XBUFFER (b)));
  1687 }
  1688 
  1689 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
  1690        doc: /* Return most recently selected buffer other than BUFFER.
  1691 Buffers not visible in windows are preferred to visible buffers, unless
  1692 optional second argument VISIBLE-OK is non-nil.  Ignore the argument
  1693 BUFFER unless it denotes a live buffer.  If the optional third argument
  1694 FRAME specifies a live frame, then use that frame's buffer list instead
  1695 of the selected frame's buffer list.
  1696 
  1697 The buffer is found by scanning the selected or specified frame's buffer
  1698 list first, followed by the list of all buffers.  If no other buffer
  1699 exists, return the buffer `*scratch*' (creating it if necessary).  */)
  1700   (Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame)
  1701 {
  1702   struct frame *f = decode_live_frame (frame);
  1703   Lisp_Object tail = f->buffer_list, pred = f->buffer_predicate;
  1704   Lisp_Object buf, notsogood = Qnil;
  1705 
  1706   /* Consider buffers that have been seen in the frame first.  */
  1707   for (; CONSP (tail); tail = XCDR (tail))
  1708     {
  1709       buf = XCAR (tail);
  1710       if (candidate_buffer (buf, buffer)
  1711           /* If the frame has a buffer_predicate, disregard buffers that
  1712              don't fit the predicate.  */
  1713           && (NILP (pred) || !NILP (call1 (pred, buf))))
  1714         {
  1715           if (!NILP (visible_ok)
  1716               || NILP (Fget_buffer_window (buf, Qvisible)))
  1717             return buf;
  1718           else if (NILP (notsogood))
  1719             notsogood = buf;
  1720         }
  1721     }
  1722 
  1723   /* Consider alist of all buffers next.  */
  1724   FOR_EACH_LIVE_BUFFER (tail, buf)
  1725     {
  1726       if (candidate_buffer (buf, buffer)
  1727           /* If the frame has a buffer_predicate, disregard buffers that
  1728              don't fit the predicate.  */
  1729           && (NILP (pred) || !NILP (call1 (pred, buf))))
  1730         {
  1731           if (!NILP (visible_ok)
  1732               || NILP (Fget_buffer_window (buf, Qvisible)))
  1733             return buf;
  1734           else if (NILP (notsogood))
  1735             notsogood = buf;
  1736         }
  1737     }
  1738 
  1739   if (!NILP (notsogood))
  1740     return notsogood;
  1741   else
  1742     return safe_call (1, Qget_scratch_buffer_create);
  1743 }
  1744 
  1745 /* The following function is a safe variant of Fother_buffer: It doesn't
  1746    pay attention to any frame-local buffer lists, doesn't care about
  1747    visibility of buffers, and doesn't evaluate any frame predicates.  */
  1748 
  1749 Lisp_Object
  1750 other_buffer_safely (Lisp_Object buffer)
  1751 {
  1752   Lisp_Object tail, buf;
  1753 
  1754   FOR_EACH_LIVE_BUFFER (tail, buf)
  1755     if (candidate_buffer (buf, buffer))
  1756       return buf;
  1757 
  1758   /* This function must return a valid buffer, since it is frequently
  1759      our last line of defense in the face of the expected buffers
  1760      becoming dead under our feet.  safe_call below could return nil
  1761      if recreating *scratch* in Lisp, which does some fancy stuff,
  1762      signals an error in some weird use case.  */
  1763   buf = safe_call (1, Qget_scratch_buffer_create);
  1764   if (NILP (buf))
  1765     {
  1766       AUTO_STRING (scratch, "*scratch*");
  1767       buf = Fget_buffer_create (scratch, Qnil);
  1768       Fset_buffer_major_mode (buf);
  1769     }
  1770   return buf;
  1771 }
  1772 
  1773 DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo,
  1774        0, 1, "",
  1775        doc: /* Start keeping undo information for buffer BUFFER.
  1776 No argument or nil as argument means do this for the current buffer.  */)
  1777   (register Lisp_Object buffer)
  1778 {
  1779   Lisp_Object real_buffer;
  1780 
  1781   if (NILP (buffer))
  1782     XSETBUFFER (real_buffer, current_buffer);
  1783   else
  1784     {
  1785       real_buffer = Fget_buffer (buffer);
  1786       if (NILP (real_buffer))
  1787         nsberror (buffer);
  1788     }
  1789 
  1790   if (EQ (BVAR (XBUFFER (real_buffer), undo_list), Qt))
  1791     bset_undo_list (XBUFFER (real_buffer), Qnil);
  1792 
  1793   return Qnil;
  1794 }
  1795 
  1796 /* Truncate undo list and shrink the gap of BUFFER.  */
  1797 
  1798 void
  1799 compact_buffer (struct buffer *buffer)
  1800 {
  1801   BUFFER_CHECK_INDIRECTION (buffer);
  1802 
  1803   /* Skip dead buffers, indirect buffers and buffers
  1804      which aren't changed since last compaction.  */
  1805   if (BUFFER_LIVE_P (buffer)
  1806       && (buffer->base_buffer == NULL)
  1807       && (BUF_COMPACT (buffer) != BUF_MODIFF (buffer)))
  1808     {
  1809       /* If a buffer's undo list is Qt, that means that undo is
  1810          turned off in that buffer.  Calling truncate_undo_list on
  1811          Qt tends to return NULL, which effectively turns undo back on.
  1812          So don't call truncate_undo_list if undo_list is Qt.  */
  1813       if (!EQ (BVAR(buffer, undo_list), Qt))
  1814         truncate_undo_list (buffer);
  1815 
  1816       /* Shrink buffer gaps.  */
  1817       if (!buffer->text->inhibit_shrinking)
  1818         {
  1819           /* If a buffer's gap size is more than 10% of the buffer
  1820              size, or larger than GAP_BYTES_DFL bytes, then shrink it
  1821              accordingly.  Keep a minimum size of GAP_BYTES_MIN bytes.  */
  1822           ptrdiff_t size = clip_to_bounds (GAP_BYTES_MIN,
  1823                                            BUF_Z_BYTE (buffer) / 10,
  1824                                            GAP_BYTES_DFL);
  1825           if (BUF_GAP_SIZE (buffer) > size)
  1826             make_gap_1 (buffer, -(BUF_GAP_SIZE (buffer) - size));
  1827         }
  1828       BUF_COMPACT (buffer) = BUF_MODIFF (buffer);
  1829     }
  1830 }
  1831 
  1832 DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ",
  1833        doc: /* Kill the buffer specified by BUFFER-OR-NAME.
  1834 The argument may be a buffer or the name of an existing buffer.
  1835 Argument nil or omitted means kill the current buffer.  Return t if the
  1836 buffer is actually killed, nil otherwise.
  1837 
  1838 The functions in `kill-buffer-query-functions' are called with the
  1839 buffer to be killed as the current buffer.  If any of them returns nil,
  1840 the buffer is not killed.  The hook `kill-buffer-hook' is run before the
  1841 buffer is actually killed.  The buffer being killed will be current
  1842 while the hook is running.  Functions called by any of these hooks are
  1843 supposed to not change the current buffer.  Neither hook is run for
  1844 internal or temporary buffers created by `get-buffer-create' or
  1845 `generate-new-buffer' with argument INHIBIT-BUFFER-HOOKS non-nil.
  1846 
  1847 Any processes that have this buffer as the `process-buffer' are killed
  1848 with SIGHUP.  This function calls `replace-buffer-in-windows' for
  1849 cleaning up all windows currently displaying the buffer to be killed. */)
  1850   (Lisp_Object buffer_or_name)
  1851 {
  1852   Lisp_Object buffer;
  1853   struct buffer *b;
  1854   Lisp_Object tem;
  1855   struct Lisp_Marker *m;
  1856 
  1857   if (NILP (buffer_or_name))
  1858     buffer = Fcurrent_buffer ();
  1859   else
  1860     buffer = Fget_buffer (buffer_or_name);
  1861   if (NILP (buffer))
  1862     nsberror (buffer_or_name);
  1863 
  1864   b = XBUFFER (buffer);
  1865 
  1866   /* Avoid trouble for buffer already dead.  */
  1867   if (!BUFFER_LIVE_P (b))
  1868     return Qnil;
  1869 
  1870   if (thread_check_current_buffer (b))
  1871     return Qnil;
  1872 
  1873   /* Run hooks with the buffer to be killed as the current buffer.  */
  1874   {
  1875     specpdl_ref count = SPECPDL_INDEX ();
  1876     bool modified;
  1877 
  1878     record_unwind_protect_excursion ();
  1879     set_buffer_internal (b);
  1880 
  1881     /* First run the query functions; if any query is answered no,
  1882        don't kill the buffer.  */
  1883     if (!b->inhibit_buffer_hooks)
  1884       {
  1885         tem = CALLN (Frun_hook_with_args_until_failure,
  1886                      Qkill_buffer_query_functions);
  1887         if (NILP (tem))
  1888           return unbind_to (count, Qnil);
  1889       }
  1890 
  1891     /* Is this a modified buffer that's visiting a file? */
  1892     modified = !NILP (BVAR (b, filename))
  1893       && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
  1894 
  1895     /* Query if the buffer is still modified.  */
  1896     if (INTERACTIVE && modified)
  1897       {
  1898         /* Ask whether to kill the buffer, and exit if the user says
  1899            "no".  */
  1900         if (NILP (call1 (Qkill_buffer__possibly_save, buffer)))
  1901           return unbind_to (count, Qnil);
  1902         /* Recheck modified.  */
  1903         modified = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
  1904       }
  1905 
  1906     /* Delete the autosave file, if requested. */
  1907     if (modified
  1908         && kill_buffer_delete_auto_save_files
  1909         && delete_auto_save_files
  1910         && !NILP (Frecent_auto_save_p ())
  1911         && STRINGP (BVAR (b, auto_save_file_name))
  1912         && !NILP (Ffile_exists_p (BVAR (b, auto_save_file_name)))
  1913         /* If `auto-save-visited-mode' is on, then we're auto-saving
  1914            to the visited file -- don't delete it.. */
  1915         && NILP (Fstring_equal (BVAR (b, auto_save_file_name),
  1916                                 BVAR (b, filename))))
  1917       {
  1918         tem = do_yes_or_no_p (build_string ("Delete auto-save file? "));
  1919         if (!NILP (tem))
  1920           call0 (intern ("delete-auto-save-file-if-necessary"));
  1921       }
  1922 
  1923     /* If the hooks have killed the buffer, exit now.  */
  1924     if (!BUFFER_LIVE_P (b))
  1925       return unbind_to (count, Qt);
  1926 
  1927     /* Then run the hooks.  */
  1928     if (!b->inhibit_buffer_hooks)
  1929       run_hook (Qkill_buffer_hook);
  1930     unbind_to (count, Qnil);
  1931   }
  1932 
  1933   /* If the hooks have killed the buffer, exit now.  */
  1934   if (!BUFFER_LIVE_P (b))
  1935     return Qt;
  1936 
  1937   /* We have no more questions to ask.  Verify that it is valid
  1938      to kill the buffer.  This must be done after the questions
  1939      since anything can happen within do_yes_or_no_p.  */
  1940 
  1941   /* Don't kill the minibuffer now current.  */
  1942   if (BASE_EQ (buffer, XWINDOW (minibuf_window)->contents))
  1943     return Qnil;
  1944 
  1945   /* When we kill an ordinary buffer which shares its buffer text
  1946      with indirect buffer(s), we must kill indirect buffer(s) too.
  1947      We do it at this stage so nothing terrible happens if they
  1948      ask questions or their hooks get errors.  */
  1949   if (!b->base_buffer && b->indirections > 0)
  1950     {
  1951       Lisp_Object tail, other;
  1952 
  1953       FOR_EACH_LIVE_BUFFER (tail, other)
  1954         if (XBUFFER (other)->base_buffer == b)
  1955           Fkill_buffer (other);
  1956 
  1957       /* Exit if we now have killed the base buffer (Bug#11665).  */
  1958       if (!BUFFER_LIVE_P (b))
  1959         return Qt;
  1960     }
  1961 
  1962   /* Run replace_buffer_in_windows before making another buffer current
  1963      since set-window-buffer-start-and-point will refuse to make another
  1964      buffer current if the selected window does not show the current
  1965      buffer (bug#10114).  */
  1966   replace_buffer_in_windows (buffer);
  1967 
  1968   /* Exit if replacing the buffer in windows has killed our buffer.  */
  1969   if (!BUFFER_LIVE_P (b))
  1970     return Qt;
  1971 
  1972   /* Make this buffer not be current.  Exit if it is the sole visible
  1973      buffer.  */
  1974   if (b == current_buffer)
  1975     {
  1976       tem = Fother_buffer (buffer, Qnil, Qnil);
  1977       Fset_buffer (tem);
  1978       if (b == current_buffer)
  1979         return Qnil;
  1980     }
  1981 
  1982   /* If the buffer now current is shown in the minibuffer and our buffer
  1983      is the sole other buffer give up.  */
  1984   XSETBUFFER (tem, current_buffer);
  1985   if (EQ (tem, XWINDOW (minibuf_window)->contents)
  1986       && BASE_EQ (buffer, Fother_buffer (buffer, Qnil, Qnil)))
  1987     return Qnil;
  1988 
  1989   /* Now there is no question: we can kill the buffer.  */
  1990 
  1991   /* Unlock this buffer's file, if it is locked.  */
  1992   unlock_buffer (b);
  1993 
  1994   kill_buffer_processes (buffer);
  1995   kill_buffer_xwidgets (buffer);
  1996 
  1997   /* Killing buffer processes may run sentinels which may have killed
  1998      our buffer.  */
  1999   if (!BUFFER_LIVE_P (b))
  2000     return Qt;
  2001 
  2002   /* These may run Lisp code and into infinite loops (if someone
  2003      insisted on circular lists) so allow quitting here.  */
  2004   frames_discard_buffer (buffer);
  2005 
  2006   clear_charpos_cache (b);
  2007 
  2008   tem = Vinhibit_quit;
  2009   Vinhibit_quit = Qt;
  2010   /* Once the buffer is removed from Vbuffer_alist, its undo_list field is
  2011      not traced by the GC in the same way.  So set it to nil early.  */
  2012   bset_undo_list (b, Qnil);
  2013   /* Remove the buffer from the list of all buffers.  */
  2014   Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist);
  2015   /* If replace_buffer_in_windows didn't do its job fix that now.  */
  2016   replace_buffer_in_windows_safely (buffer);
  2017   Vinhibit_quit = tem;
  2018 
  2019   if (b->base_buffer)
  2020     {
  2021       INTERVAL i;
  2022       /* Unchain all markers that belong to this indirect buffer.
  2023          Don't unchain the markers that belong to the base buffer
  2024          or its other indirect buffers.  */
  2025       struct Lisp_Marker **mp = &BUF_MARKERS (b);
  2026       while ((m = *mp))
  2027         {
  2028           if (m->buffer == b)
  2029             {
  2030               m->buffer = NULL;
  2031               *mp = m->next;
  2032             }
  2033           else
  2034             mp = &m->next;
  2035         }
  2036       /* Intervals should be owned by the base buffer (Bug#16502).  */
  2037       i = buffer_intervals (b);
  2038       if (i)
  2039         {
  2040           Lisp_Object owner;
  2041           XSETBUFFER (owner, b->base_buffer);
  2042           set_interval_object (i, owner);
  2043         }
  2044     }
  2045   else
  2046     {
  2047       /* Unchain all markers of this buffer and its indirect buffers.
  2048          and leave them pointing nowhere.  */
  2049       for (m = BUF_MARKERS (b); m; )
  2050         {
  2051           struct Lisp_Marker *next = m->next;
  2052           m->buffer = 0;
  2053           m->next = NULL;
  2054           m = next;
  2055         }
  2056       BUF_MARKERS (b) = NULL;
  2057       set_buffer_intervals (b, NULL);
  2058 
  2059       /* Perhaps we should explicitly free the interval tree here...  */
  2060     }
  2061   delete_all_overlays (b);
  2062   free_buffer_overlays (b);
  2063 
  2064   /* Reset the local variables, so that this buffer's local values
  2065      won't be protected from GC.  They would be protected
  2066      if they happened to remain cached in their symbols.
  2067      This gets rid of them for certain.  */
  2068   reset_buffer_local_variables (b, 1);
  2069 
  2070   bset_name (b, Qnil);
  2071 
  2072   block_input ();
  2073   if (b->base_buffer)
  2074     {
  2075       /* Notify our base buffer that we don't share the text anymore.  */
  2076       eassert (b->indirections == -1);
  2077       b->base_buffer->indirections--;
  2078       eassert (b->base_buffer->indirections >= 0);
  2079       /* Make sure that we wasn't confused.  */
  2080       eassert (b->window_count == -1);
  2081     }
  2082   else
  2083     {
  2084       /* Make sure that no one shows us.  */
  2085       eassert (b->window_count == 0);
  2086       /* No one shares our buffer text, can free it.  */
  2087       free_buffer_text (b);
  2088     }
  2089 
  2090   if (b->newline_cache)
  2091     {
  2092       free_region_cache (b->newline_cache);
  2093       b->newline_cache = 0;
  2094     }
  2095   if (b->width_run_cache)
  2096     {
  2097       free_region_cache (b->width_run_cache);
  2098       b->width_run_cache = 0;
  2099     }
  2100   if (b->bidi_paragraph_cache)
  2101     {
  2102       free_region_cache (b->bidi_paragraph_cache);
  2103       b->bidi_paragraph_cache = 0;
  2104     }
  2105   bset_width_table (b, Qnil);
  2106   unblock_input ();
  2107 
  2108   run_buffer_list_update_hook (b);
  2109 
  2110   return Qt;
  2111 }
  2112 
  2113 /* Move association for BUFFER to the front of buffer (a)lists.  Since
  2114    we do this each time BUFFER is selected visibly, the more recently
  2115    selected buffers are always closer to the front of those lists.  This
  2116    means that other_buffer is more likely to choose a relevant buffer.
  2117 
  2118    Note that this moves BUFFER to the front of the buffer lists of the
  2119    selected frame even if BUFFER is not shown there.  If BUFFER is not
  2120    shown in the selected frame, consider the present behavior a feature.
  2121    `select-window' gets this right since it shows BUFFER in the selected
  2122    window when calling us.  */
  2123 
  2124 void
  2125 record_buffer (Lisp_Object buffer)
  2126 {
  2127   Lisp_Object aelt, aelt_cons, tem;
  2128   register struct frame *f = XFRAME (selected_frame);
  2129 
  2130   CHECK_BUFFER (buffer);
  2131 
  2132   /* Update Vbuffer_alist (we know that it has an entry for BUFFER).
  2133      Don't allow quitting since this might leave the buffer list in an
  2134      inconsistent state.  */
  2135   tem = Vinhibit_quit;
  2136   Vinhibit_quit = Qt;
  2137   aelt = Frassq (buffer, Vbuffer_alist);
  2138   aelt_cons = Fmemq (aelt, Vbuffer_alist);
  2139   Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
  2140   XSETCDR (aelt_cons, Vbuffer_alist);
  2141   Vbuffer_alist = aelt_cons;
  2142   Vinhibit_quit = tem;
  2143 
  2144   /* Update buffer list of selected frame.  */
  2145   fset_buffer_list (f, Fcons (buffer, Fdelq (buffer, f->buffer_list)));
  2146   fset_buried_buffer_list (f, Fdelq (buffer, f->buried_buffer_list));
  2147 
  2148   run_buffer_list_update_hook (XBUFFER (buffer));
  2149 }
  2150 
  2151 
  2152 /* Move BUFFER to the end of the buffer (a)lists.  Do nothing if the
  2153    buffer is killed.  For the selected frame's buffer list this moves
  2154    BUFFER to its end even if it was never shown in that frame.  If
  2155    this happens we have a feature, hence `bury-buffer-internal' should be
  2156    called only when BUFFER was shown in the selected frame.  */
  2157 
  2158 DEFUN ("bury-buffer-internal", Fbury_buffer_internal, Sbury_buffer_internal,
  2159        1, 1, 0,
  2160        doc: /* Move BUFFER to the end of the buffer list.  */)
  2161   (Lisp_Object buffer)
  2162 {
  2163   Lisp_Object aelt, aelt_cons, tem;
  2164   register struct frame *f = XFRAME (selected_frame);
  2165 
  2166   CHECK_BUFFER (buffer);
  2167 
  2168   /* Update Vbuffer_alist (we know that it has an entry for BUFFER).
  2169      Don't allow quitting since this might leave the buffer list in an
  2170      inconsistent state.  */
  2171   tem = Vinhibit_quit;
  2172   Vinhibit_quit = Qt;
  2173   aelt = Frassq (buffer, Vbuffer_alist);
  2174   aelt_cons = Fmemq (aelt, Vbuffer_alist);
  2175   Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
  2176   XSETCDR (aelt_cons, Qnil);
  2177   Vbuffer_alist = nconc2 (Vbuffer_alist, aelt_cons);
  2178   Vinhibit_quit = tem;
  2179 
  2180   /* Update buffer lists of selected frame.  */
  2181   fset_buffer_list (f, Fdelq (buffer, f->buffer_list));
  2182   fset_buried_buffer_list
  2183     (f, Fcons (buffer, Fdelq (buffer, f->buried_buffer_list)));
  2184 
  2185   run_buffer_list_update_hook (XBUFFER (buffer));
  2186 
  2187   return Qnil;
  2188 }
  2189 
  2190 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
  2191        doc: /* Set an appropriate major mode for BUFFER.
  2192 For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode
  2193 according to the default value of `major-mode'.
  2194 Use this function before selecting the buffer, since it may need to inspect
  2195 the current buffer's major mode.  */)
  2196   (Lisp_Object buffer)
  2197 {
  2198   Lisp_Object function;
  2199 
  2200   CHECK_BUFFER (buffer);
  2201 
  2202   if (!BUFFER_LIVE_P (XBUFFER (buffer)))
  2203     error ("Attempt to set major mode for a dead buffer");
  2204 
  2205   if (strcmp (SSDATA (BVAR (XBUFFER (buffer), name)), "*scratch*") == 0)
  2206     function = find_symbol_value (intern ("initial-major-mode"));
  2207   else
  2208     {
  2209       function = BVAR (&buffer_defaults, major_mode);
  2210       if (NILP (function)
  2211           && NILP (Fget (BVAR (current_buffer, major_mode), Qmode_class)))
  2212         function = BVAR (current_buffer, major_mode);
  2213     }
  2214 
  2215   if (NILP (function)) /* If function is `fundamental-mode', allow it to run
  2216                           so that `run-mode-hooks' and thus
  2217                           `hack-local-variables' get run. */
  2218     return Qnil;
  2219 
  2220   specpdl_ref count = SPECPDL_INDEX ();
  2221 
  2222   /* To select a nonfundamental mode,
  2223      select the buffer temporarily and then call the mode function.  */
  2224 
  2225   record_unwind_current_buffer ();
  2226 
  2227   Fset_buffer (buffer);
  2228   call0 (function);
  2229 
  2230   return unbind_to (count, Qnil);
  2231 }
  2232 
  2233 DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
  2234        doc: /* Return the current buffer as a Lisp object.  */)
  2235   (void)
  2236 {
  2237   register Lisp_Object buf;
  2238   XSETBUFFER (buf, current_buffer);
  2239   return buf;
  2240 }
  2241 
  2242 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
  2243    This is used by redisplay.  */
  2244 
  2245 void
  2246 set_buffer_internal_1 (register struct buffer *b)
  2247 {
  2248 #ifdef USE_MMAP_FOR_BUFFERS
  2249   if (b->text->beg == NULL)
  2250     enlarge_buffer_text (b, 0);
  2251 #endif /* USE_MMAP_FOR_BUFFERS */
  2252 
  2253   if (current_buffer == b)
  2254     return;
  2255 
  2256   set_buffer_internal_2 (b);
  2257 }
  2258 
  2259 /* Like set_buffer_internal_1, but doesn't check whether B is already
  2260    the current buffer.  Called upon switch of the current thread, see
  2261    post_acquire_global_lock.  */
  2262 void set_buffer_internal_2 (register struct buffer *b)
  2263 {
  2264   register struct buffer *old_buf;
  2265   register Lisp_Object tail;
  2266 
  2267   BUFFER_CHECK_INDIRECTION (b);
  2268 
  2269   old_buf = current_buffer;
  2270   current_buffer = b;
  2271   last_known_column_point = -1;   /* Invalidate indentation cache.  */
  2272 
  2273   if (old_buf)
  2274     {
  2275       /* Put the undo list back in the base buffer, so that it appears
  2276          that an indirect buffer shares the undo list of its base.  */
  2277       if (old_buf->base_buffer)
  2278         bset_undo_list (old_buf->base_buffer, BVAR (old_buf, undo_list));
  2279 
  2280       /* If the old current buffer has markers to record PT, BEGV and ZV
  2281          when it is not current, update them now.  */
  2282       record_buffer_markers (old_buf);
  2283     }
  2284 
  2285   /* Get the undo list from the base buffer, so that it appears
  2286      that an indirect buffer shares the undo list of its base.  */
  2287   if (b->base_buffer)
  2288     bset_undo_list (b, BVAR (b->base_buffer, undo_list));
  2289 
  2290   /* If the new current buffer has markers to record PT, BEGV and ZV
  2291      when it is not current, fetch them now.  */
  2292   fetch_buffer_markers (b);
  2293 
  2294   /* Look down buffer's list of local Lisp variables
  2295      to find and update any that forward into C variables.  */
  2296 
  2297   do
  2298     {
  2299       for (tail = BVAR (b, local_var_alist); CONSP (tail); tail = XCDR (tail))
  2300         {
  2301           Lisp_Object var = XCAR (XCAR (tail));
  2302           struct Lisp_Symbol *sym = XSYMBOL (var);
  2303           if (sym->u.s.redirect == SYMBOL_LOCALIZED /* Just to be sure.  */
  2304               && SYMBOL_BLV (sym)->fwd.fwdptr)
  2305             /* Just reference the variable
  2306                to cause it to become set for this buffer.  */
  2307             Fsymbol_value (var);
  2308         }
  2309     }
  2310   /* Do the same with any others that were local to the previous buffer */
  2311   while (b != old_buf && (b = old_buf, b));
  2312 }
  2313 
  2314 /* Switch to buffer B temporarily for redisplay purposes.
  2315    This avoids certain things that don't need to be done within redisplay.  */
  2316 
  2317 void
  2318 set_buffer_temp (struct buffer *b)
  2319 {
  2320   register struct buffer *old_buf;
  2321 
  2322   if (current_buffer == b)
  2323     return;
  2324 
  2325   old_buf = current_buffer;
  2326   current_buffer = b;
  2327 
  2328   /* If the old current buffer has markers to record PT, BEGV and ZV
  2329      when it is not current, update them now.  */
  2330   record_buffer_markers (old_buf);
  2331 
  2332   /* If the new current buffer has markers to record PT, BEGV and ZV
  2333      when it is not current, fetch them now.  */
  2334   fetch_buffer_markers (b);
  2335 }
  2336 
  2337 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
  2338        doc: /* Make buffer BUFFER-OR-NAME current for editing operations.
  2339 BUFFER-OR-NAME may be a buffer or the name of an existing buffer.
  2340 See also `with-current-buffer' when you want to make a buffer current
  2341 temporarily.  This function does not display the buffer, so its effect
  2342 ends when the current command terminates.  Use `switch-to-buffer' or
  2343 `pop-to-buffer' to switch buffers permanently.
  2344 The return value is the buffer made current.  */)
  2345   (register Lisp_Object buffer_or_name)
  2346 {
  2347   register Lisp_Object buffer;
  2348   buffer = Fget_buffer (buffer_or_name);
  2349   if (NILP (buffer))
  2350     nsberror (buffer_or_name);
  2351   if (!BUFFER_LIVE_P (XBUFFER (buffer)))
  2352     error ("Selecting deleted buffer");
  2353   set_buffer_internal (XBUFFER (buffer));
  2354   return buffer;
  2355 }
  2356 
  2357 void
  2358 restore_buffer (Lisp_Object buffer_or_name)
  2359 {
  2360   Fset_buffer (buffer_or_name);
  2361 }
  2362 
  2363 /* Set the current buffer to BUFFER provided if it is alive.  */
  2364 
  2365 void
  2366 set_buffer_if_live (Lisp_Object buffer)
  2367 {
  2368   if (BUFFER_LIVE_P (XBUFFER (buffer)))
  2369     set_buffer_internal (XBUFFER (buffer));
  2370 }
  2371 
  2372 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
  2373                                    Sbarf_if_buffer_read_only, 0, 1, 0,
  2374        doc: /* Signal a `buffer-read-only' error if the current buffer is read-only.
  2375 If the text under POSITION (which defaults to point) has the
  2376 `inhibit-read-only' text property set, the error will not be raised.  */)
  2377   (Lisp_Object position)
  2378 {
  2379   if (NILP (position))
  2380     XSETFASTINT (position, PT);
  2381   else
  2382     CHECK_FIXNUM (position);
  2383 
  2384   if (!NILP (BVAR (current_buffer, read_only))
  2385       && NILP (Vinhibit_read_only)
  2386       && NILP (Fget_text_property (position, Qinhibit_read_only, Qnil)))
  2387     xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
  2388   return Qnil;
  2389 }
  2390 
  2391 DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
  2392        doc: /* Delete the entire contents of the current buffer.
  2393 Any narrowing restriction in effect (see `narrow-to-region') is removed,
  2394 so the buffer is truly empty after this.  */)
  2395   (void)
  2396 {
  2397   labeled_restrictions_remove_in_current_buffer ();
  2398   Fwiden ();
  2399 
  2400   del_range (BEG, Z);
  2401 
  2402   current_buffer->last_window_start = 1;
  2403   /* Prevent warnings, or suspension of auto saving, that would happen
  2404      if future size is less than past size.  Use of erase-buffer
  2405      implies that the future text is not really related to the past text.  */
  2406   XSETFASTINT (BVAR (current_buffer, save_length), 0);
  2407   return Qnil;
  2408 }
  2409 
  2410 void
  2411 validate_region (Lisp_Object *b, Lisp_Object *e)
  2412 {
  2413   EMACS_INT beg = fix_position (*b), end = fix_position (*e);
  2414 
  2415   if (end < beg)
  2416     {
  2417       EMACS_INT tem = beg;  beg = end;  end = tem;
  2418     }
  2419 
  2420   if (! (BEGV <= beg && end <= ZV))
  2421     args_out_of_range_3 (Fcurrent_buffer (), *b, *e);
  2422 
  2423   *b = make_fixnum (beg);
  2424   *e = make_fixnum (end);
  2425 }
  2426 
  2427 /* Advance BYTE_POS up to a character boundary
  2428    and return the adjusted position.  */
  2429 
  2430 ptrdiff_t
  2431 advance_to_char_boundary (ptrdiff_t byte_pos)
  2432 {
  2433   int c;
  2434 
  2435   if (byte_pos == BEG)
  2436     /* Beginning of buffer is always a character boundary.  */
  2437     return BEG;
  2438 
  2439   c = FETCH_BYTE (byte_pos);
  2440   if (! CHAR_HEAD_P (c))
  2441     {
  2442       /* We should advance BYTE_POS only when C is a constituent of a
  2443          multibyte sequence.  */
  2444       ptrdiff_t orig_byte_pos = byte_pos;
  2445 
  2446       do
  2447         {
  2448           byte_pos--;
  2449           c = FETCH_BYTE (byte_pos);
  2450         }
  2451       while (! CHAR_HEAD_P (c) && byte_pos > BEG);
  2452       byte_pos += next_char_len (byte_pos);
  2453       if (byte_pos < orig_byte_pos)
  2454         byte_pos = orig_byte_pos;
  2455       /* If C is a constituent of a multibyte sequence, BYTE_POS was
  2456          surely advance to the correct character boundary.  If C is
  2457          not, BYTE_POS was unchanged.  */
  2458     }
  2459 
  2460   return byte_pos;
  2461 }
  2462 
  2463 static void
  2464 swap_buffer_overlays (struct buffer *buffer, struct buffer *other)
  2465 {
  2466   struct itree_node *node;
  2467 
  2468   ITREE_FOREACH (node, buffer->overlays, PTRDIFF_MIN, PTRDIFF_MAX, ASCENDING)
  2469     XOVERLAY (node->data)->buffer = other;
  2470 
  2471   ITREE_FOREACH (node, other->overlays, PTRDIFF_MIN, PTRDIFF_MAX, ASCENDING)
  2472     XOVERLAY (node->data)->buffer = buffer;
  2473 
  2474   /* Swap the interval trees. */
  2475   void *tmp = buffer->overlays;
  2476   buffer->overlays = other->overlays;
  2477   other->overlays = tmp;
  2478 }
  2479 
  2480 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
  2481        1, 1, 0,
  2482        doc: /* Swap the text between current buffer and BUFFER.
  2483 Using this function from `save-excursion' might produce surprising
  2484 results, see Info node `(elisp)Swapping Text'.  */)
  2485   (Lisp_Object buffer)
  2486 {
  2487   struct buffer *other_buffer;
  2488   CHECK_BUFFER (buffer);
  2489   other_buffer = XBUFFER (buffer);
  2490 
  2491   if (!BUFFER_LIVE_P (other_buffer))
  2492     error ("Cannot swap a dead buffer's text");
  2493 
  2494   /* Actually, it probably works just fine.
  2495    * if (other_buffer == current_buffer)
  2496    *   error ("Cannot swap a buffer's text with itself"); */
  2497 
  2498   /* Actually, this may be workable as well, tho probably only if they're
  2499      *both* indirect.  */
  2500   if (other_buffer->base_buffer
  2501       || current_buffer->base_buffer)
  2502     error ("Cannot swap indirect buffers's text");
  2503 
  2504   { /* This is probably harder to make work.  */
  2505     Lisp_Object tail, other;
  2506     FOR_EACH_LIVE_BUFFER (tail, other)
  2507       if (XBUFFER (other)->base_buffer == other_buffer
  2508           || XBUFFER (other)->base_buffer == current_buffer)
  2509         error ("One of the buffers to swap has indirect buffers");
  2510   }
  2511 
  2512 #define swapfield(field, type) \
  2513   do {                                                  \
  2514     type tmp##field = other_buffer->field;              \
  2515     other_buffer->field = current_buffer->field;        \
  2516     current_buffer->field = tmp##field;                 \
  2517   } while (0)
  2518 #define swapfield_(field, type) \
  2519   do {                                                  \
  2520     type tmp##field = BVAR (other_buffer, field);               \
  2521     bset_##field (other_buffer, BVAR (current_buffer, field));  \
  2522     bset_##field (current_buffer, tmp##field);                  \
  2523   } while (0)
  2524 
  2525   swapfield (own_text, struct buffer_text);
  2526   eassert (current_buffer->text == &current_buffer->own_text);
  2527   eassert (other_buffer->text == &other_buffer->own_text);
  2528 #ifdef REL_ALLOC
  2529   r_alloc_reset_variable ((void **) &current_buffer->own_text.beg,
  2530                           (void **) &other_buffer->own_text.beg);
  2531   r_alloc_reset_variable ((void **) &other_buffer->own_text.beg,
  2532                           (void **) &current_buffer->own_text.beg);
  2533 #endif /* REL_ALLOC */
  2534 
  2535   swapfield (pt, ptrdiff_t);
  2536   swapfield (pt_byte, ptrdiff_t);
  2537   swapfield (begv, ptrdiff_t);
  2538   swapfield (begv_byte, ptrdiff_t);
  2539   swapfield (zv, ptrdiff_t);
  2540   swapfield (zv_byte, ptrdiff_t);
  2541   eassert (!current_buffer->base_buffer);
  2542   eassert (!other_buffer->base_buffer);
  2543   swapfield (indirections, ptrdiff_t);
  2544   current_buffer->clip_changed = 1;     other_buffer->clip_changed = 1;
  2545   swapfield (newline_cache, struct region_cache *);
  2546   swapfield (width_run_cache, struct region_cache *);
  2547   swapfield (bidi_paragraph_cache, struct region_cache *);
  2548   current_buffer->prevent_redisplay_optimizations_p = 1;
  2549   other_buffer->prevent_redisplay_optimizations_p = 1;
  2550   swapfield (long_line_optimizations_p, bool_bf);
  2551   swapfield_ (undo_list, Lisp_Object);
  2552   swapfield_ (mark, Lisp_Object);
  2553   swapfield_ (mark_active, Lisp_Object); /* Belongs with the `mark'.  */
  2554   swapfield_ (enable_multibyte_characters, Lisp_Object);
  2555   swapfield_ (bidi_display_reordering, Lisp_Object);
  2556   swapfield_ (bidi_paragraph_direction, Lisp_Object);
  2557   swapfield_ (bidi_paragraph_separate_re, Lisp_Object);
  2558   swapfield_ (bidi_paragraph_start_re, Lisp_Object);
  2559   /* FIXME: Not sure what we should do with these *_marker fields.
  2560      Hopefully they're just nil anyway.  */
  2561   swapfield_ (pt_marker, Lisp_Object);
  2562   swapfield_ (begv_marker, Lisp_Object);
  2563   swapfield_ (zv_marker, Lisp_Object);
  2564   bset_point_before_scroll (current_buffer, Qnil);
  2565   bset_point_before_scroll (other_buffer, Qnil);
  2566 
  2567   modiff_incr (&current_buffer->text->modiff, 1);
  2568   modiff_incr (&other_buffer->text->modiff, 1);
  2569   modiff_incr (&current_buffer->text->chars_modiff, 1);
  2570   modiff_incr (&other_buffer->text->chars_modiff, 1);
  2571   modiff_incr (&current_buffer->text->overlay_modiff, 1);
  2572   modiff_incr (&other_buffer->text->overlay_modiff, 1);
  2573   current_buffer->text->beg_unchanged = current_buffer->text->gpt;
  2574   current_buffer->text->end_unchanged = current_buffer->text->gpt;
  2575   other_buffer->text->beg_unchanged = other_buffer->text->gpt;
  2576   other_buffer->text->end_unchanged = other_buffer->text->gpt;
  2577   swap_buffer_overlays (current_buffer, other_buffer);
  2578   {
  2579     struct Lisp_Marker *m;
  2580     for (m = BUF_MARKERS (current_buffer); m; m = m->next)
  2581       if (m->buffer == other_buffer)
  2582         m->buffer = current_buffer;
  2583       else
  2584         /* Since there's no indirect buffer in sight, markers on
  2585            BUF_MARKERS(buf) should either be for `buf' or dead.  */
  2586         eassert (!m->buffer);
  2587     for (m = BUF_MARKERS (other_buffer); m; m = m->next)
  2588       if (m->buffer == current_buffer)
  2589         m->buffer = other_buffer;
  2590       else
  2591         /* Since there's no indirect buffer in sight, markers on
  2592            BUF_MARKERS(buf) should either be for `buf' or dead.  */
  2593         eassert (!m->buffer);
  2594   }
  2595   { /* Some of the C code expects that both window markers of a
  2596        live window points to that window's buffer.  So since we
  2597        just swapped the markers between the two buffers, we need
  2598        to undo the effect of this swap for window markers.  */
  2599     Lisp_Object w = selected_window, ws = Qnil;
  2600     Lisp_Object buf1, buf2;
  2601     XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer);
  2602 
  2603     while (NILP (Fmemq (w, ws)))
  2604       {
  2605         ws = Fcons (w, ws);
  2606         if (MARKERP (XWINDOW (w)->pointm)
  2607             && (BASE_EQ (XWINDOW (w)->contents, buf1)
  2608                 || BASE_EQ (XWINDOW (w)->contents, buf2)))
  2609           Fset_marker (XWINDOW (w)->pointm,
  2610                        make_fixnum
  2611                        (BUF_BEGV (XBUFFER (XWINDOW (w)->contents))),
  2612                        XWINDOW (w)->contents);
  2613         /* Blindly copied from pointm part.  */
  2614         if (MARKERP (XWINDOW (w)->old_pointm)
  2615             && (BASE_EQ (XWINDOW (w)->contents, buf1)
  2616                 || BASE_EQ (XWINDOW (w)->contents, buf2)))
  2617           Fset_marker (XWINDOW (w)->old_pointm,
  2618                        make_fixnum
  2619                        (BUF_BEGV (XBUFFER (XWINDOW (w)->contents))),
  2620                        XWINDOW (w)->contents);
  2621         if (MARKERP (XWINDOW (w)->start)
  2622             && (BASE_EQ (XWINDOW (w)->contents, buf1)
  2623                 || BASE_EQ (XWINDOW (w)->contents, buf2)))
  2624           Fset_marker (XWINDOW (w)->start,
  2625                        make_fixnum
  2626                        (XBUFFER (XWINDOW (w)->contents)->last_window_start),
  2627                        XWINDOW (w)->contents);
  2628         w = Fnext_window (w, Qt, Qt);
  2629       }
  2630   }
  2631 
  2632   if (current_buffer->text->intervals)
  2633     (eassert (BASE_EQ (current_buffer->text->intervals->up.obj, buffer)),
  2634      XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer));
  2635   if (other_buffer->text->intervals)
  2636     (eassert (BASE_EQ (other_buffer->text->intervals->up.obj,
  2637                        Fcurrent_buffer ())),
  2638      XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer));
  2639 
  2640   return Qnil;
  2641 }
  2642 
  2643 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
  2644        1, 1, 0,
  2645        doc: /* Set the multibyte flag of the current buffer to FLAG.
  2646 If FLAG is t, this makes the buffer a multibyte buffer.
  2647 If FLAG is nil, this makes the buffer a single-byte buffer.
  2648 In these cases, the buffer contents remain unchanged as a sequence of
  2649 bytes but the contents viewed as characters do change.
  2650 If FLAG is `to', this makes the buffer a multibyte buffer by changing
  2651 all eight-bit bytes to eight-bit characters.
  2652 If the multibyte flag was really changed, undo information of the
  2653 current buffer is cleared.  */)
  2654   (Lisp_Object flag)
  2655 {
  2656   struct Lisp_Marker *tail, *markers;
  2657   Lisp_Object btail, other;
  2658   ptrdiff_t begv, zv;
  2659   bool narrowed = (BEG != BEGV || Z != ZV);
  2660   bool modified_p = !NILP (Fbuffer_modified_p (Qnil));
  2661   Lisp_Object old_undo = BVAR (current_buffer, undo_list);
  2662 
  2663   if (current_buffer->base_buffer)
  2664     error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
  2665 
  2666   /* Do nothing if nothing actually changes.  */
  2667   if (NILP (flag) == NILP (BVAR (current_buffer, enable_multibyte_characters)))
  2668     return flag;
  2669 
  2670   /* Don't record these buffer changes.  We will put a special undo entry
  2671      instead.  */
  2672   bset_undo_list (current_buffer, Qt);
  2673 
  2674   /* If the cached position is for this buffer, clear it out.  */
  2675   clear_charpos_cache (current_buffer);
  2676 
  2677   if (NILP (flag))
  2678     begv = BEGV_BYTE, zv = ZV_BYTE;
  2679   else
  2680     begv = BEGV, zv = ZV;
  2681 
  2682   if (narrowed)
  2683     error ("Changing multibyteness in a narrowed buffer");
  2684 
  2685   invalidate_buffer_caches (current_buffer, BEGV, ZV);
  2686 
  2687   if (NILP (flag))
  2688     {
  2689       ptrdiff_t pos, stop;
  2690       unsigned char *p;
  2691 
  2692       /* Do this first, so it can use CHAR_TO_BYTE
  2693          to calculate the old correspondences.  */
  2694       set_intervals_multibyte (false);
  2695       set_overlays_multibyte (false);
  2696 
  2697       bset_enable_multibyte_characters (current_buffer, Qnil);
  2698 
  2699       Z = Z_BYTE;
  2700       BEGV = BEGV_BYTE;
  2701       ZV = ZV_BYTE;
  2702       GPT = GPT_BYTE;
  2703       TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
  2704 
  2705 
  2706       for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
  2707         tail->charpos = tail->bytepos;
  2708 
  2709       /* Convert multibyte form of 8-bit characters to unibyte.  */
  2710       pos = BEG;
  2711       stop = GPT;
  2712       p = BEG_ADDR;
  2713       while (1)
  2714         {
  2715           if (pos == stop)
  2716             {
  2717               if (pos == Z)
  2718                 break;
  2719               p = GAP_END_ADDR;
  2720               stop = Z;
  2721             }
  2722           if (ASCII_CHAR_P (*p))
  2723             p++, pos++;
  2724           else if (CHAR_BYTE8_HEAD_P (*p))
  2725             {
  2726               int bytes, c = string_char_and_length (p, &bytes);
  2727               /* Delete all bytes for this 8-bit character but the
  2728                  last one, and change the last one to the character
  2729                  code.  */
  2730               bytes--;
  2731               del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
  2732               p = GAP_END_ADDR;
  2733               *p++ = c;
  2734               pos++;
  2735               if (begv > pos)
  2736                 begv -= bytes;
  2737               if (zv > pos)
  2738                 zv -= bytes;
  2739               stop = Z;
  2740             }
  2741           else
  2742             {
  2743               int bytes = BYTES_BY_CHAR_HEAD (*p);
  2744               p += bytes, pos += bytes;
  2745             }
  2746         }
  2747     }
  2748   else
  2749     {
  2750       ptrdiff_t pt = PT;
  2751       ptrdiff_t pos, stop;
  2752       unsigned char *p, *pend;
  2753 
  2754       /* Be sure not to have a multibyte sequence striding over the GAP.
  2755          Ex: We change this: "...abc\302 _GAP_ \241def..."
  2756              to: "...abc _GAP_ \302\241def..."  */
  2757 
  2758       if (EQ (flag, Qt)
  2759           && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
  2760           && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
  2761         {
  2762           unsigned char *q = GPT_ADDR - 1;
  2763 
  2764           while (! CHAR_HEAD_P (*q) && q > BEG_ADDR) q--;
  2765           if (LEADING_CODE_P (*q))
  2766             {
  2767               ptrdiff_t new_gpt = GPT_BYTE - (GPT_ADDR - q);
  2768 
  2769               move_gap_both (new_gpt, new_gpt);
  2770             }
  2771         }
  2772 
  2773       /* Make the buffer contents valid as multibyte by converting
  2774          8-bit characters to multibyte form.  */
  2775       pos = BEG;
  2776       stop = GPT;
  2777       p = BEG_ADDR;
  2778       pend = GPT_ADDR;
  2779       while (1)
  2780         {
  2781           int bytes;
  2782 
  2783           if (pos == stop)
  2784             {
  2785               if (pos == Z)
  2786                 break;
  2787               p = GAP_END_ADDR;
  2788               pend = Z_ADDR;
  2789               stop = Z;
  2790             }
  2791 
  2792           if (ASCII_CHAR_P (*p))
  2793             p++, pos++;
  2794           else if (EQ (flag, Qt)
  2795                    && 0 < (bytes = multibyte_length (p, pend, true, false)))
  2796             p += bytes, pos += bytes;
  2797           else
  2798             {
  2799               unsigned char tmp[MAX_MULTIBYTE_LENGTH];
  2800               int c;
  2801 
  2802               c = BYTE8_TO_CHAR (*p);
  2803               bytes = CHAR_STRING (c, tmp);
  2804               *p = tmp[0];
  2805               TEMP_SET_PT_BOTH (pos + 1, pos + 1);
  2806               bytes--;
  2807               insert_1_both ((char *) tmp + 1, bytes, bytes, 1, 0, 0);
  2808               /* Now the gap is after the just inserted data.  */
  2809               pos = GPT;
  2810               p = GAP_END_ADDR;
  2811               if (pos <= begv)
  2812                 begv += bytes;
  2813               if (pos <= zv)
  2814                 zv += bytes;
  2815               if (pos <= pt)
  2816                 pt += bytes;
  2817               pend = Z_ADDR;
  2818               stop = Z;
  2819             }
  2820         }
  2821 
  2822       if (pt != PT)
  2823         TEMP_SET_PT (pt);
  2824 
  2825       /* Do this first, so that chars_in_text asks the right question.
  2826          set_intervals_multibyte needs it too.  */
  2827       bset_enable_multibyte_characters (current_buffer, Qt);
  2828 
  2829       GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
  2830       GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
  2831 
  2832       Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
  2833 
  2834       BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
  2835       if (BEGV_BYTE > GPT_BYTE)
  2836         BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
  2837       else
  2838         BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
  2839 
  2840       ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
  2841       if (ZV_BYTE > GPT_BYTE)
  2842         ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
  2843       else
  2844         ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
  2845 
  2846       {
  2847         ptrdiff_t byte = advance_to_char_boundary (PT_BYTE);
  2848         ptrdiff_t position;
  2849 
  2850         if (byte > GPT_BYTE)
  2851           position = chars_in_text (GAP_END_ADDR, byte - GPT_BYTE) + GPT;
  2852         else
  2853           position = chars_in_text (BEG_ADDR, byte - BEG_BYTE) + BEG;
  2854         TEMP_SET_PT_BOTH (position, byte);
  2855       }
  2856 
  2857       tail = markers = BUF_MARKERS (current_buffer);
  2858 
  2859       /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
  2860          getting confused by the markers that have not yet been updated.
  2861          It is also a signal that it should never create a marker.  */
  2862       BUF_MARKERS (current_buffer) = NULL;
  2863 
  2864       for (; tail; tail = tail->next)
  2865         {
  2866           tail->bytepos = advance_to_char_boundary (tail->bytepos);
  2867           tail->charpos = BYTE_TO_CHAR (tail->bytepos);
  2868         }
  2869 
  2870       /* Make sure no markers were put on the chain
  2871          while the chain value was incorrect.  */
  2872       if (BUF_MARKERS (current_buffer))
  2873         emacs_abort ();
  2874 
  2875       BUF_MARKERS (current_buffer) = markers;
  2876 
  2877       /* Do this last, so it can calculate the new correspondences
  2878          between chars and bytes.  */
  2879       /* FIXME: Is it worth the trouble, really?  Couldn't we just throw
  2880          away all the text-properties instead of trying to guess how
  2881          to adjust them?  AFAICT the result is not reliable anyway.  */
  2882       set_intervals_multibyte (true);
  2883       set_overlays_multibyte (true);
  2884     }
  2885 
  2886   if (!EQ (old_undo, Qt))
  2887     {
  2888       /* Represent all the above changes by a special undo entry.  */
  2889       bset_undo_list (current_buffer,
  2890                       Fcons (list3 (Qapply,
  2891                                     intern ("set-buffer-multibyte"),
  2892                                     NILP (flag) ? Qt : Qnil),
  2893                              old_undo));
  2894     }
  2895 
  2896   current_buffer->prevent_redisplay_optimizations_p = 1;
  2897 
  2898   /* If buffer is shown in a window, let redisplay consider other windows.  */
  2899   if (buffer_window_count (current_buffer))
  2900     windows_or_buffers_changed = 10;
  2901 
  2902   /* Copy this buffer's new multibyte status
  2903      into all of its indirect buffers.  */
  2904   FOR_EACH_LIVE_BUFFER (btail, other)
  2905     {
  2906       struct buffer *o = XBUFFER (other);
  2907       if (o->base_buffer == current_buffer && BUFFER_LIVE_P (o))
  2908         {
  2909           BVAR (o, enable_multibyte_characters)
  2910             = BVAR (current_buffer, enable_multibyte_characters);
  2911           o->prevent_redisplay_optimizations_p = true;
  2912         }
  2913     }
  2914 
  2915   /* Restore the modifiedness of the buffer.  */
  2916   if (!modified_p && !NILP (Fbuffer_modified_p (Qnil)))
  2917     Fset_buffer_modified_p (Qnil);
  2918 
  2919   /* Update coding systems of this buffer's process (if any).  */
  2920   {
  2921     Lisp_Object process;
  2922 
  2923     process = Fget_buffer_process (Fcurrent_buffer ());
  2924     if (PROCESSP (process))
  2925       setup_process_coding_systems (process);
  2926   }
  2927 
  2928   return flag;
  2929 }
  2930 
  2931 DEFUN ("kill-all-local-variables", Fkill_all_local_variables,
  2932        Skill_all_local_variables, 0, 1, 0,
  2933        doc: /* Switch to Fundamental mode by killing current buffer's local variables.
  2934 Most local variable bindings are eliminated so that the default values
  2935 become effective once more.  Also, the syntax table is set from
  2936 `standard-syntax-table', the local keymap is set to nil,
  2937 and the abbrev table from `fundamental-mode-abbrev-table'.
  2938 This function also forces redisplay of the mode line.
  2939 
  2940 Every function to select a new major mode starts by
  2941 calling this function.
  2942 
  2943 As a special exception, local variables whose names have a non-nil
  2944 `permanent-local' property are not eliminated by this function.  If
  2945 the optional KILL-PERMANENT argument is non-nil, clear out these local
  2946 variables, too.
  2947 
  2948 The first thing this function does is run
  2949 the normal hook `change-major-mode-hook'.  */)
  2950   (Lisp_Object kill_permanent)
  2951 {
  2952   run_hook (Qchange_major_mode_hook);
  2953 
  2954   /* Actually eliminate all local bindings of this buffer.  */
  2955 
  2956   reset_buffer_local_variables (current_buffer, !NILP (kill_permanent));
  2957 
  2958   /* Force mode-line redisplay.  Useful here because all major mode
  2959      commands call this function.  */
  2960   bset_update_mode_line (current_buffer);
  2961 
  2962   return Qnil;
  2963 }
  2964 
  2965 
  2966 /* Find all the overlays in the current buffer that overlap the range
  2967    [BEG, END).
  2968 
  2969    If EMPTY is true, include empty overlays in that range and also at
  2970    END, provided END denotes the position at the end of the accessible
  2971    part of the buffer.
  2972 
  2973    If TRAILING is true, include overlays that begin at END, provided
  2974    END denotes the position at the end of the accessible part of the
  2975    buffer.
  2976 
  2977    Return the number found, and store them in a vector in *VEC_PTR.
  2978    Store in *LEN_PTR the size allocated for the vector.
  2979    Store in *NEXT_PTR the next position after POS where an overlay starts,
  2980      or ZV if there are no more overlays.
  2981    NEXT_PTR may be 0, meaning don't store that info.
  2982 
  2983    *VEC_PTR and *LEN_PTR should contain a valid vector and size
  2984    when this function is called.
  2985 
  2986    If EXTEND, make the vector bigger if necessary.  If not, never
  2987    extend the vector, and store only as many overlays as will fit.
  2988    But still return the total number of overlays.
  2989 */
  2990 
  2991 ptrdiff_t
  2992 overlays_in (ptrdiff_t beg, ptrdiff_t end, bool extend,
  2993              Lisp_Object **vec_ptr, ptrdiff_t *len_ptr,
  2994              bool empty, bool trailing,
  2995              ptrdiff_t *next_ptr)
  2996 {
  2997   ptrdiff_t idx = 0;
  2998   ptrdiff_t len = *len_ptr;
  2999   ptrdiff_t next = ZV;
  3000   Lisp_Object *vec = *vec_ptr;
  3001   struct itree_node *node;
  3002 
  3003   /* Extend the search range if overlays beginning at ZV are
  3004      wanted.  */
  3005   ptrdiff_t search_end = ZV;
  3006   if (end >= ZV && (empty || trailing))
  3007     ++search_end;
  3008 
  3009   ITREE_FOREACH (node, current_buffer->overlays, beg, search_end,
  3010                  ASCENDING)
  3011     {
  3012       if (node->begin > end)
  3013         {
  3014           next = min (next, node->begin);
  3015           break;
  3016         }
  3017       else if (node->begin == end)
  3018         {
  3019           next = node->begin;
  3020           if ((! empty || end < ZV) && beg < end)
  3021             break;
  3022           if (empty && node->begin != node->end)
  3023             continue;
  3024         }
  3025 
  3026       if (! empty && node->begin == node->end)
  3027         continue;
  3028 
  3029       if (extend && idx == len)
  3030         {
  3031           vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
  3032                          sizeof *vec);
  3033           *vec_ptr = vec;
  3034           len = *len_ptr;
  3035         }
  3036       if (idx < len)
  3037         vec[idx] = node->data;
  3038       /* Keep counting overlays even if we can't return them all.  */
  3039       idx++;
  3040     }
  3041   if (next_ptr)
  3042     *next_ptr = next ? next : ZV;
  3043 
  3044   return idx;
  3045 }
  3046 
  3047 /* Find all non-empty overlays in the current buffer that contain
  3048    position POS.
  3049 
  3050    See overlays_in for the meaning of the arguments.
  3051   */
  3052 
  3053 ptrdiff_t
  3054 overlays_at (ptrdiff_t pos, bool extend,
  3055              Lisp_Object **vec_ptr, ptrdiff_t *len_ptr,
  3056              ptrdiff_t *next_ptr)
  3057 {
  3058   return overlays_in (pos, pos + 1, extend, vec_ptr, len_ptr,
  3059                       false, true, next_ptr);
  3060 }
  3061 
  3062 ptrdiff_t
  3063 next_overlay_change (ptrdiff_t pos)
  3064 {
  3065   ptrdiff_t next = ZV;
  3066   struct itree_node *node;
  3067 
  3068   ITREE_FOREACH (node, current_buffer->overlays, pos, next, ASCENDING)
  3069     {
  3070       if (node->begin > pos)
  3071         {
  3072           /* If we reach this branch, node->begin must be the least upper bound
  3073              of pos, because the search is limited to [pos,next) . */
  3074           eassert (node->begin < next);
  3075           next = node->begin;
  3076           break;
  3077         }
  3078       else if (node->begin < node->end && node->end < next)
  3079         {
  3080           next = node->end;
  3081           ITREE_FOREACH_NARROW (pos, next);
  3082         }
  3083     }
  3084 
  3085   return next;
  3086 }
  3087 
  3088 ptrdiff_t
  3089 previous_overlay_change (ptrdiff_t pos)
  3090 {
  3091   struct itree_node *node;
  3092   ptrdiff_t prev = BEGV;
  3093 
  3094   ITREE_FOREACH (node, current_buffer->overlays, prev, pos, DESCENDING)
  3095     {
  3096       if (node->end < pos)
  3097         prev = node->end;
  3098       else
  3099         prev = max (prev, node->begin);
  3100       ITREE_FOREACH_NARROW (prev, pos);
  3101     }
  3102 
  3103   return prev;
  3104 }
  3105 
  3106 /* Return true if there exists an overlay with a non-nil
  3107    `mouse-face' property overlapping OVERLAY.  */
  3108 
  3109 bool
  3110 mouse_face_overlay_overlaps (Lisp_Object overlay)
  3111 {
  3112   ptrdiff_t start = OVERLAY_START (overlay);
  3113   ptrdiff_t end = OVERLAY_END (overlay);
  3114   ptrdiff_t n, i, size;
  3115   Lisp_Object *v, tem;
  3116   Lisp_Object vbuf[10];
  3117   USE_SAFE_ALLOCA;
  3118 
  3119   size = ARRAYELTS (vbuf);
  3120   v = vbuf;
  3121   n = overlays_in (start, end, 0, &v, &size, true, false, NULL);
  3122   if (n > size)
  3123     {
  3124       SAFE_NALLOCA (v, 1, n);
  3125       overlays_in (start, end, 0, &v, &n, true, false, NULL);
  3126     }
  3127 
  3128   for (i = 0; i < n; ++i)
  3129     if (!EQ (v[i], overlay)
  3130         && (tem = Foverlay_get (overlay, Qmouse_face),
  3131             !NILP (tem)))
  3132       break;
  3133 
  3134   SAFE_FREE ();
  3135   return i < n;
  3136 }
  3137 
  3138 /* Return the value of the 'display-line-numbers-disable' property at
  3139    EOB, if there's an overlay at ZV with a non-nil value of that property.  */
  3140 Lisp_Object
  3141 disable_line_numbers_overlay_at_eob (void)
  3142 {
  3143   ptrdiff_t n, i, size;
  3144   Lisp_Object *v, tem = Qnil;
  3145   Lisp_Object vbuf[10];
  3146   USE_SAFE_ALLOCA;
  3147 
  3148   size = ARRAYELTS (vbuf);
  3149   v = vbuf;
  3150   n = overlays_in (ZV, ZV, 0, &v, &size, false, false, NULL);
  3151   if (n > size)
  3152     {
  3153       SAFE_NALLOCA (v, 1, n);
  3154       overlays_in (ZV, ZV, 0, &v, &n, false, false, NULL);
  3155     }
  3156 
  3157   for (i = 0; i < n; ++i)
  3158     if ((tem = Foverlay_get (v[i], Qdisplay_line_numbers_disable),
  3159          !NILP (tem)))
  3160       break;
  3161 
  3162   SAFE_FREE ();
  3163   return tem;
  3164 }
  3165 
  3166 
  3167 /* Fast function to just test if we're at an overlay boundary.
  3168 
  3169    Returns true if some overlay starts or ends (or both) at POS,
  3170 */
  3171 bool
  3172 overlay_touches_p (ptrdiff_t pos)
  3173 {
  3174   struct itree_node *node;
  3175 
  3176   /* We need to find overlays ending in pos, as well as empty ones at
  3177      pos. */
  3178   ITREE_FOREACH (node, current_buffer->overlays, pos - 1, pos + 1, DESCENDING)
  3179     if (node->begin == pos || node->end == pos)
  3180       return true;
  3181   return false;
  3182 }
  3183 
  3184 
  3185 int
  3186 compare_overlays (const void *v1, const void *v2)
  3187 {
  3188   const struct sortvec *s1 = v1;
  3189   const struct sortvec *s2 = v2;
  3190   /* Return 1 if s1 should take precedence, -1 if v2 should take precedence,
  3191      and 0 if they're equal.  */
  3192   if (s1->priority != s2->priority)
  3193     return s1->priority < s2->priority ? -1 : 1;
  3194   /* If the priority is equal, give precedence to the one not covered by the
  3195      other.  If neither covers the other, obey spriority.  */
  3196   else if (s1->beg < s2->beg)
  3197     return (s1->end < s2->end && s1->spriority > s2->spriority ? 1 : -1);
  3198   else if (s1->beg > s2->beg)
  3199     return (s1->end > s2->end && s1->spriority < s2->spriority ? -1 : 1);
  3200   else if (s1->end != s2->end)
  3201     return s2->end < s1->end ? -1 : 1;
  3202   else if (s1->spriority != s2->spriority)
  3203     return (s1->spriority < s2->spriority ? -1 : 1);
  3204   else if (EQ (s1->overlay, s2->overlay))
  3205     return 0;
  3206   else
  3207     /* Avoid the non-determinism of qsort by choosing an arbitrary ordering
  3208        between "equal" overlays.  The result can still change between
  3209        invocations of Emacs, but it won't change in the middle of
  3210        `find_field' (bug#6830).  */
  3211     return XLI (s1->overlay) < XLI (s2->overlay) ? -1 : 1;
  3212 }
  3213 
  3214 void
  3215 make_sortvec_item (struct sortvec *item, Lisp_Object overlay)
  3216 {
  3217   Lisp_Object tem;
  3218   /* This overlay is good and counts: put it into sortvec.  */
  3219   item->overlay = overlay;
  3220   item->beg = OVERLAY_START (overlay);
  3221   item->end = OVERLAY_END (overlay);
  3222   tem = Foverlay_get (overlay, Qpriority);
  3223   if (NILP (tem))
  3224     {
  3225       item->priority = 0;
  3226       item->spriority = 0;
  3227     }
  3228   else if (FIXNUMP (tem))
  3229     {
  3230       item->priority = XFIXNUM (tem);
  3231       item->spriority = 0;
  3232     }
  3233   else if (CONSP (tem))
  3234     {
  3235       Lisp_Object car = XCAR (tem);
  3236       Lisp_Object cdr = XCDR (tem);
  3237       item->priority  = FIXNUMP (car) ? XFIXNUM (car) : 0;
  3238       item->spriority = FIXNUMP (cdr) ? XFIXNUM (cdr) : 0;
  3239     }
  3240 }
  3241 /* Sort an array of overlays by priority.  The array is modified in place.
  3242    The return value is the new size; this may be smaller than the original
  3243    size if some of the overlays were invalid or were window-specific.  */
  3244 ptrdiff_t
  3245 sort_overlays (Lisp_Object *overlay_vec, ptrdiff_t noverlays, struct window *w)
  3246 {
  3247   ptrdiff_t i, j;
  3248   USE_SAFE_ALLOCA;
  3249   struct sortvec *sortvec;
  3250 
  3251   SAFE_NALLOCA (sortvec, 1, noverlays);
  3252 
  3253   /* Put the valid and relevant overlays into sortvec.  */
  3254 
  3255   for (i = 0, j = 0; i < noverlays; i++)
  3256     {
  3257       Lisp_Object overlay;
  3258 
  3259       overlay = overlay_vec[i];
  3260       if (OVERLAYP (overlay)
  3261           && OVERLAY_START (overlay) > 0
  3262           && OVERLAY_END (overlay) > 0)
  3263         {
  3264           /* If we're interested in a specific window, then ignore
  3265              overlays that are limited to some other window.  */
  3266           if (w && ! overlay_matches_window (w, overlay))
  3267             continue;
  3268           make_sortvec_item (sortvec + j, overlay);
  3269           j++;
  3270         }
  3271     }
  3272   noverlays = j;
  3273 
  3274   /* Sort the overlays into the proper order: increasing priority.  */
  3275 
  3276   if (noverlays > 1)
  3277     qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays);
  3278 
  3279   for (i = 0; i < noverlays; i++)
  3280     overlay_vec[i] = sortvec[i].overlay;
  3281 
  3282   SAFE_FREE ();
  3283   return (noverlays);
  3284 }
  3285 
  3286 struct sortstr
  3287 {
  3288   Lisp_Object string, string2;
  3289   ptrdiff_t size;
  3290   EMACS_INT priority;
  3291 };
  3292 
  3293 struct sortstrlist
  3294 {
  3295   struct sortstr *buf;  /* An array that expands as needed; never freed.  */
  3296   ptrdiff_t size;       /* Allocated length of that array.  */
  3297   ptrdiff_t used;       /* How much of the array is currently in use.  */
  3298   ptrdiff_t bytes;      /* Total length of the strings in buf.  */
  3299 };
  3300 
  3301 /* Buffers for storing information about the overlays touching a given
  3302    position.  These could be automatic variables in overlay_strings, but
  3303    it's more efficient to hold onto the memory instead of repeatedly
  3304    allocating and freeing it.  */
  3305 static struct sortstrlist overlay_heads, overlay_tails;
  3306 static unsigned char *overlay_str_buf;
  3307 
  3308 /* Allocated length of overlay_str_buf.  */
  3309 static ptrdiff_t overlay_str_len;
  3310 
  3311 /* A comparison function suitable for passing to qsort.  */
  3312 static int
  3313 cmp_for_strings (const void *as1, const void *as2)
  3314 {
  3315   struct sortstr const *s1 = as1;
  3316   struct sortstr const *s2 = as2;
  3317   if (s1->size != s2->size)
  3318     return s2->size < s1->size ? -1 : 1;
  3319   if (s1->priority != s2->priority)
  3320     return s1->priority < s2->priority ? -1 : 1;
  3321   return 0;
  3322 }
  3323 
  3324 static void
  3325 record_overlay_string (struct sortstrlist *ssl, Lisp_Object str,
  3326                        Lisp_Object str2, Lisp_Object pri, ptrdiff_t size)
  3327 {
  3328   ptrdiff_t nbytes;
  3329 
  3330   if (ssl->used == ssl->size)
  3331     ssl->buf = xpalloc (ssl->buf, &ssl->size, 5, -1, sizeof *ssl->buf);
  3332   ssl->buf[ssl->used].string = str;
  3333   ssl->buf[ssl->used].string2 = str2;
  3334   ssl->buf[ssl->used].size = size;
  3335   ssl->buf[ssl->used].priority = (FIXNUMP (pri) ? XFIXNUM (pri) : 0);
  3336   ssl->used++;
  3337 
  3338   if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
  3339     nbytes = SCHARS (str);
  3340   else if (! STRING_MULTIBYTE (str))
  3341     nbytes = count_size_as_multibyte (SDATA (str),
  3342                                       SBYTES (str));
  3343   else
  3344     nbytes = SBYTES (str);
  3345 
  3346   if (ckd_add (&nbytes, nbytes, ssl->bytes))
  3347     memory_full (SIZE_MAX);
  3348   ssl->bytes = nbytes;
  3349 
  3350   if (STRINGP (str2))
  3351     {
  3352       if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
  3353         nbytes = SCHARS (str2);
  3354       else if (! STRING_MULTIBYTE (str2))
  3355         nbytes = count_size_as_multibyte (SDATA (str2),
  3356                                           SBYTES (str2));
  3357       else
  3358         nbytes = SBYTES (str2);
  3359 
  3360       if (ckd_add (&nbytes, nbytes, ssl->bytes))
  3361         memory_full (SIZE_MAX);
  3362       ssl->bytes = nbytes;
  3363     }
  3364 }
  3365 
  3366 /* Concatenate the strings associated with overlays that begin or end
  3367    at POS, ignoring overlays that are specific to windows other than W.
  3368    The strings are concatenated in the appropriate order: shorter
  3369    overlays nest inside longer ones, and higher priority inside lower.
  3370    Normally all of the after-strings come first, but zero-sized
  3371    overlays have their after-strings ride along with the
  3372    before-strings because it would look strange to print them
  3373    inside-out.
  3374 
  3375    Returns the concatenated string's length, and return the pointer to
  3376    that string via PSTR, if that variable is non-NULL.  The storage of
  3377    the concatenated strings may be overwritten by subsequent calls.  */
  3378 
  3379 ptrdiff_t
  3380 overlay_strings (ptrdiff_t pos, struct window *w, unsigned char **pstr)
  3381 {
  3382   bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
  3383   struct itree_node *node;
  3384 
  3385   overlay_heads.used = overlay_heads.bytes = 0;
  3386   overlay_tails.used = overlay_tails.bytes = 0;
  3387 
  3388   ITREE_FOREACH (node, current_buffer->overlays, pos - 1, pos + 1, DESCENDING)
  3389     {
  3390       Lisp_Object overlay = node->data;
  3391       eassert (OVERLAYP (overlay));
  3392 
  3393       ptrdiff_t startpos = node->begin;
  3394       ptrdiff_t endpos = node->end;
  3395 
  3396       if (endpos != pos && startpos != pos)
  3397         continue;
  3398       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  3399       if (WINDOWP (window) && XWINDOW (window) != w)
  3400         continue;
  3401       Lisp_Object str;
  3402       /* FIXME: Are we really sure that `record_overlay_string` can
  3403          never cause a non-local exit?  */
  3404       if (startpos == pos
  3405           && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str)))
  3406         record_overlay_string (&overlay_heads, str,
  3407                                (startpos == endpos
  3408                                 ? Foverlay_get (overlay, Qafter_string)
  3409                                 : Qnil),
  3410                                Foverlay_get (overlay, Qpriority),
  3411                                endpos - startpos);
  3412       else if (endpos == pos
  3413           && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str)))
  3414         record_overlay_string (&overlay_tails, str, Qnil,
  3415                                Foverlay_get (overlay, Qpriority),
  3416                                endpos - startpos);
  3417     }
  3418 
  3419   if (overlay_tails.used > 1)
  3420     qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr),
  3421            cmp_for_strings);
  3422   if (overlay_heads.used > 1)
  3423     qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr),
  3424            cmp_for_strings);
  3425   if (overlay_heads.bytes || overlay_tails.bytes)
  3426     {
  3427       Lisp_Object tem;
  3428       ptrdiff_t i;
  3429       unsigned char *p;
  3430       ptrdiff_t total;
  3431 
  3432       if (ckd_add (&total, overlay_heads.bytes, overlay_tails.bytes))
  3433         memory_full (SIZE_MAX);
  3434       if (total > overlay_str_len)
  3435         overlay_str_buf = xpalloc (overlay_str_buf, &overlay_str_len,
  3436                                    total - overlay_str_len, -1, 1);
  3437 
  3438       p = overlay_str_buf;
  3439       for (i = overlay_tails.used; --i >= 0;)
  3440         {
  3441           ptrdiff_t nbytes;
  3442           tem = overlay_tails.buf[i].string;
  3443           nbytes = copy_text (SDATA (tem), p,
  3444                               SBYTES (tem),
  3445                               STRING_MULTIBYTE (tem), multibyte);
  3446           p += nbytes;
  3447         }
  3448       for (i = 0; i < overlay_heads.used; ++i)
  3449         {
  3450           ptrdiff_t nbytes;
  3451           tem = overlay_heads.buf[i].string;
  3452           nbytes = copy_text (SDATA (tem), p,
  3453                               SBYTES (tem),
  3454                               STRING_MULTIBYTE (tem), multibyte);
  3455           p += nbytes;
  3456           tem = overlay_heads.buf[i].string2;
  3457           if (STRINGP (tem))
  3458             {
  3459               nbytes = copy_text (SDATA (tem), p,
  3460                                   SBYTES (tem),
  3461                                   STRING_MULTIBYTE (tem), multibyte);
  3462               p += nbytes;
  3463             }
  3464         }
  3465       if (p != overlay_str_buf + total)
  3466         emacs_abort ();
  3467       if (pstr)
  3468         *pstr = overlay_str_buf;
  3469       return total;
  3470     }
  3471   return 0;
  3472 }
  3473 
  3474 
  3475 void
  3476 adjust_overlays_for_insert (ptrdiff_t pos, ptrdiff_t length, bool before_markers)
  3477 {
  3478   if (!current_buffer->indirections)
  3479     itree_insert_gap (current_buffer->overlays, pos, length, before_markers);
  3480   else
  3481     {
  3482       struct buffer *base = current_buffer->base_buffer
  3483                             ? current_buffer->base_buffer
  3484                             : current_buffer;
  3485       Lisp_Object tail, other;
  3486       itree_insert_gap (base->overlays, pos, length, before_markers);
  3487       FOR_EACH_LIVE_BUFFER (tail, other)
  3488         if (XBUFFER (other)->base_buffer == base)
  3489           itree_insert_gap (XBUFFER (other)->overlays, pos, length,
  3490                             before_markers);
  3491     }
  3492 }
  3493 
  3494 static void
  3495 adjust_overlays_for_delete_in_buffer (struct buffer * buf,
  3496                                       ptrdiff_t pos, ptrdiff_t length)
  3497 {
  3498   Lisp_Object hit_list = Qnil;
  3499   struct itree_node *node;
  3500 
  3501   /* Ideally, the evaporate check would be done directly within
  3502      `itree_delete_gap`, but that code isn't supposed to know about overlays,
  3503      only about `itree_node`s, so it would break an abstraction boundary.  */
  3504   itree_delete_gap (buf->overlays, pos, length);
  3505 
  3506   /* Delete any zero-sized overlays at position POS, if the `evaporate'
  3507      property is set.  */
  3508 
  3509   ITREE_FOREACH (node, buf->overlays, pos, pos, ASCENDING)
  3510     {
  3511       if (node->end == pos && node->begin == pos
  3512           && ! NILP (Foverlay_get (node->data, Qevaporate)))
  3513         hit_list = Fcons (node->data, hit_list);
  3514     }
  3515 
  3516   for (; CONSP (hit_list); hit_list = XCDR (hit_list))
  3517     Fdelete_overlay (XCAR (hit_list));
  3518 }
  3519 
  3520 void
  3521 adjust_overlays_for_delete (ptrdiff_t pos, ptrdiff_t length)
  3522 {
  3523   if (!current_buffer->indirections)
  3524     adjust_overlays_for_delete_in_buffer (current_buffer, pos, length);
  3525   else
  3526     {
  3527       struct buffer *base = current_buffer->base_buffer
  3528                             ? current_buffer->base_buffer
  3529                             : current_buffer;
  3530       Lisp_Object tail, other;
  3531       adjust_overlays_for_delete_in_buffer (base, pos, length);
  3532       FOR_EACH_LIVE_BUFFER (tail, other)
  3533         if (XBUFFER (other)->base_buffer == base)
  3534           adjust_overlays_for_delete_in_buffer (XBUFFER (other), pos, length);
  3535     }
  3536 }
  3537 
  3538 
  3539 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
  3540        doc: /* Return t if OBJECT is an overlay.  */)
  3541   (Lisp_Object object)
  3542 {
  3543   return (OVERLAYP (object) ? Qt : Qnil);
  3544 }
  3545 
  3546 DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
  3547        doc: /* Create a new overlay with range BEG to END in BUFFER and return it.
  3548 If omitted, BUFFER defaults to the current buffer.
  3549 BEG and END may be integers or markers.
  3550 The fourth arg FRONT-ADVANCE, if non-nil, makes the marker
  3551 for the front of the overlay advance when text is inserted there
  3552 \(which means the text *is not* included in the overlay).
  3553 The fifth arg REAR-ADVANCE, if non-nil, makes the marker
  3554 for the rear of the overlay advance when text is inserted there
  3555 \(which means the text *is* included in the overlay).  */)
  3556   (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer,
  3557    Lisp_Object front_advance, Lisp_Object rear_advance)
  3558 {
  3559   Lisp_Object ov;
  3560   struct buffer *b;
  3561 
  3562   if (NILP (buffer))
  3563     XSETBUFFER (buffer, current_buffer);
  3564   else
  3565     CHECK_BUFFER (buffer);
  3566 
  3567   b = XBUFFER (buffer);
  3568   if (! BUFFER_LIVE_P (b))
  3569     error ("Attempt to create overlay in a dead buffer");
  3570 
  3571   if (MARKERP (beg) && !BASE_EQ (Fmarker_buffer (beg), buffer))
  3572     signal_error ("Marker points into wrong buffer", beg);
  3573   if (MARKERP (end) && !BASE_EQ (Fmarker_buffer (end), buffer))
  3574     signal_error ("Marker points into wrong buffer", end);
  3575 
  3576   CHECK_FIXNUM_COERCE_MARKER (beg);
  3577   CHECK_FIXNUM_COERCE_MARKER (end);
  3578 
  3579   if (XFIXNUM (beg) > XFIXNUM (end))
  3580     {
  3581       Lisp_Object temp;
  3582       temp = beg; beg = end; end = temp;
  3583     }
  3584 
  3585   ptrdiff_t obeg = clip_to_bounds (BUF_BEG (b), XFIXNUM (beg), BUF_Z (b));
  3586   ptrdiff_t oend = clip_to_bounds (obeg, XFIXNUM (end), BUF_Z (b));
  3587   ov = build_overlay (! NILP (front_advance),
  3588                       ! NILP (rear_advance), Qnil);
  3589   add_buffer_overlay (b, XOVERLAY (ov), obeg, oend);
  3590 
  3591   /* We don't need to redisplay the region covered by the overlay, because
  3592      the overlay has no properties at the moment.  */
  3593 
  3594   return ov;
  3595 }
  3596 
  3597 /* Mark a section of BUF as needing redisplay because of overlays changes.  */
  3598 
  3599 static void
  3600 modify_overlay (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
  3601 {
  3602   if (start > end)
  3603     {
  3604       ptrdiff_t temp = start;
  3605       start = end;
  3606       end = temp;
  3607     }
  3608 
  3609   BUF_COMPUTE_UNCHANGED (buf, start, end);
  3610 
  3611   bset_redisplay (buf);
  3612 
  3613   modiff_incr (&BUF_OVERLAY_MODIFF (buf), 1);
  3614 }
  3615 
  3616 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
  3617        doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER.
  3618 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
  3619 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
  3620 buffer.  */)
  3621   (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer)
  3622 {
  3623   struct buffer *b, *ob = 0;
  3624   Lisp_Object obuffer;
  3625   specpdl_ref count = SPECPDL_INDEX ();
  3626   ptrdiff_t o_beg UNINIT, o_end UNINIT;
  3627 
  3628   CHECK_OVERLAY (overlay);
  3629   if (NILP (buffer))
  3630     buffer = Foverlay_buffer (overlay);
  3631   if (NILP (buffer))
  3632     XSETBUFFER (buffer, current_buffer);
  3633   CHECK_BUFFER (buffer);
  3634 
  3635   if (NILP (Fbuffer_live_p (buffer)))
  3636     error ("Attempt to move overlay to a dead buffer");
  3637 
  3638   if (MARKERP (beg) && !BASE_EQ (Fmarker_buffer (beg), buffer))
  3639     signal_error ("Marker points into wrong buffer", beg);
  3640   if (MARKERP (end) && !BASE_EQ (Fmarker_buffer (end), buffer))
  3641     signal_error ("Marker points into wrong buffer", end);
  3642 
  3643   CHECK_FIXNUM_COERCE_MARKER (beg);
  3644   CHECK_FIXNUM_COERCE_MARKER (end);
  3645 
  3646   if (XFIXNUM (beg) > XFIXNUM (end))
  3647     {
  3648       Lisp_Object temp;
  3649       temp = beg; beg = end; end = temp;
  3650     }
  3651 
  3652   specbind (Qinhibit_quit, Qt); /* FIXME: Why?  */
  3653 
  3654   obuffer = Foverlay_buffer (overlay);
  3655   b = XBUFFER (buffer);
  3656 
  3657   ptrdiff_t n_beg = clip_to_bounds (BUF_BEG (b), XFIXNUM (beg), BUF_Z (b));
  3658   ptrdiff_t n_end = clip_to_bounds (n_beg, XFIXNUM (end), BUF_Z (b));
  3659 
  3660   if (!NILP (obuffer))
  3661     {
  3662       ob = XBUFFER (obuffer);
  3663 
  3664       o_beg = OVERLAY_START (overlay);
  3665       o_end = OVERLAY_END (overlay);
  3666     }
  3667 
  3668   if (! BASE_EQ (buffer, obuffer))
  3669     {
  3670       if (! NILP (obuffer))
  3671         remove_buffer_overlay (XBUFFER (obuffer), XOVERLAY (overlay));
  3672       add_buffer_overlay (XBUFFER (buffer), XOVERLAY (overlay), n_beg, n_end);
  3673     }
  3674   else
  3675     itree_node_set_region (b->overlays, XOVERLAY (overlay)->interval,
  3676                               n_beg, n_end);
  3677 
  3678   /* If the overlay has changed buffers, do a thorough redisplay.  */
  3679   if (!BASE_EQ (buffer, obuffer))
  3680     {
  3681       /* Redisplay where the overlay was.  */
  3682       if (ob)
  3683         modify_overlay (ob, o_beg, o_end);
  3684 
  3685       /* Redisplay where the overlay is going to be.  */
  3686       modify_overlay (b, n_beg, n_end);
  3687     }
  3688   else
  3689     /* Redisplay the area the overlay has just left, or just enclosed.  */
  3690     {
  3691       if (o_beg == n_beg)
  3692         modify_overlay (b, o_end, n_end);
  3693       else if (o_end == n_end)
  3694         modify_overlay (b, o_beg, n_beg);
  3695       else
  3696         modify_overlay (b, min (o_beg, n_beg), max (o_end, n_end));
  3697     }
  3698 
  3699   /* Delete the overlay if it is empty after clipping and has the
  3700      evaporate property.  */
  3701   if (n_beg == n_end && !NILP (Foverlay_get (overlay, Qevaporate)))
  3702     { /* We used to call `Fdelete_overlay' here, but it causes problems:
  3703          - At this stage, `overlay' is not included in its buffer's lists
  3704            of overlays (the data-structure is in an inconsistent state),
  3705            contrary to `Fdelete_overlay's assumptions.
  3706          - Most of the work done by Fdelete_overlay has already been done
  3707            here for other reasons.  */
  3708       drop_overlay (XOVERLAY (overlay));
  3709       return unbind_to (count, overlay);
  3710     }
  3711 
  3712   return unbind_to (count, overlay);
  3713 }
  3714 
  3715 DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
  3716        doc: /* Delete the overlay OVERLAY from its buffer.  */)
  3717   (Lisp_Object overlay)
  3718 {
  3719   struct buffer *b;
  3720   specpdl_ref count = SPECPDL_INDEX ();
  3721 
  3722   CHECK_OVERLAY (overlay);
  3723 
  3724   b = OVERLAY_BUFFER (overlay);
  3725   if (! b)
  3726     return Qnil;
  3727 
  3728   specbind (Qinhibit_quit, Qt);
  3729 
  3730   drop_overlay (XOVERLAY (overlay));
  3731 
  3732   /* When deleting an overlay with before or after strings, turn off
  3733      display optimizations for the affected buffer, on the basis that
  3734      these strings may contain newlines.  This is easier to do than to
  3735      check for that situation during redisplay.  */
  3736   if (!windows_or_buffers_changed
  3737       && (!NILP (Foverlay_get (overlay, Qbefore_string))
  3738           || !NILP (Foverlay_get (overlay, Qafter_string))))
  3739     b->prevent_redisplay_optimizations_p = 1;
  3740 
  3741   return unbind_to (count, Qnil);
  3742 }
  3743 
  3744 DEFUN ("delete-all-overlays", Fdelete_all_overlays, Sdelete_all_overlays, 0, 1, 0,
  3745        doc: /* Delete all overlays of BUFFER.
  3746 BUFFER omitted or nil means delete all overlays of the current
  3747 buffer.  */)
  3748   (Lisp_Object buffer)
  3749 {
  3750   delete_all_overlays (decode_buffer (buffer));
  3751   return Qnil;
  3752 }
  3753 
  3754 /* Overlay dissection functions.  */
  3755 
  3756 DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0,
  3757        doc: /* Return the position at which OVERLAY starts.  */)
  3758   (Lisp_Object overlay)
  3759 {
  3760   CHECK_OVERLAY (overlay);
  3761   if (! OVERLAY_BUFFER (overlay))
  3762     return Qnil;
  3763 
  3764   return make_fixnum (OVERLAY_START (overlay));
  3765 }
  3766 
  3767 DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0,
  3768        doc: /* Return the position at which OVERLAY ends.  */)
  3769   (Lisp_Object overlay)
  3770 {
  3771   CHECK_OVERLAY (overlay);
  3772   if (! OVERLAY_BUFFER (overlay))
  3773     return Qnil;
  3774 
  3775   return make_fixnum (OVERLAY_END (overlay));
  3776 }
  3777 
  3778 DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0,
  3779        doc: /* Return the buffer OVERLAY belongs to.
  3780 Return nil if OVERLAY has been deleted.  */)
  3781   (Lisp_Object overlay)
  3782 {
  3783   Lisp_Object buffer;
  3784 
  3785   CHECK_OVERLAY (overlay);
  3786 
  3787   if (! OVERLAY_BUFFER (overlay))
  3788     return Qnil;
  3789 
  3790   XSETBUFFER (buffer, OVERLAY_BUFFER (overlay));
  3791 
  3792   return buffer;
  3793 }
  3794 
  3795 DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0,
  3796        doc: /* Return a list of the properties on OVERLAY.
  3797 This is a copy of OVERLAY's plist; modifying its conses has no effect on
  3798 OVERLAY.  */)
  3799   (Lisp_Object overlay)
  3800 {
  3801   CHECK_OVERLAY (overlay);
  3802 
  3803   return Fcopy_sequence (OVERLAY_PLIST (overlay));
  3804 }
  3805 
  3806 
  3807 DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 2, 0,
  3808        doc: /* Return a list of the overlays that contain the character at POS.
  3809 If SORTED is non-nil, then sort them by decreasing priority.
  3810 
  3811 Zero-length overlays that start and stop at POS are not included in
  3812 the return value.  Instead use `overlays-in' if those overlays are of
  3813 interest.  */)
  3814   (Lisp_Object pos, Lisp_Object sorted)
  3815 {
  3816   ptrdiff_t len, noverlays;
  3817   Lisp_Object *overlay_vec;
  3818   Lisp_Object result;
  3819 
  3820   CHECK_FIXNUM_COERCE_MARKER (pos);
  3821 
  3822   if (!buffer_has_overlays ())
  3823     return Qnil;
  3824 
  3825   len = 10;
  3826   /* We can't use alloca here because overlays_at can call xrealloc.  */
  3827   overlay_vec = xmalloc (len * sizeof *overlay_vec);
  3828 
  3829   /* Put all the overlays we want in a vector in overlay_vec.
  3830      Store the length in len.  */
  3831   noverlays = overlays_at (XFIXNUM (pos), true, &overlay_vec, &len, NULL);
  3832 
  3833   if (!NILP (sorted))
  3834     noverlays = sort_overlays (overlay_vec, noverlays,
  3835                                WINDOWP (sorted) ? XWINDOW (sorted) : NULL);
  3836 
  3837   /* Make a list of them all.  */
  3838   result = Flist (noverlays, overlay_vec);
  3839 
  3840   /* The doc string says the list should be in decreasing order of
  3841      priority, so we reverse the list, because sort_overlays sorts in
  3842      the increasing order of priority.  */
  3843   if (!NILP (sorted))
  3844     result = Fnreverse (result);
  3845 
  3846   xfree (overlay_vec);
  3847   return result;
  3848 }
  3849 
  3850 DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0,
  3851        doc: /* Return a list of the overlays that overlap the region BEG ... END.
  3852 Overlap means that at least one character is contained within the overlay
  3853 and also contained within the specified region.
  3854 
  3855 Empty overlays are included in the result if they are located at BEG,
  3856 between BEG and END, or at END provided END denotes the position at the
  3857 end of the accessible part of the buffer.
  3858 
  3859 The resulting list of overlays is in an arbitrary unpredictable order.  */)
  3860   (Lisp_Object beg, Lisp_Object end)
  3861 {
  3862   ptrdiff_t len, noverlays;
  3863   Lisp_Object *overlay_vec;
  3864   Lisp_Object result;
  3865 
  3866   CHECK_FIXNUM_COERCE_MARKER (beg);
  3867   CHECK_FIXNUM_COERCE_MARKER (end);
  3868 
  3869   if (!buffer_has_overlays ())
  3870     return Qnil;
  3871 
  3872   len = 10;
  3873   overlay_vec = xmalloc (len * sizeof *overlay_vec);
  3874 
  3875   /* Put all the overlays we want in a vector in overlay_vec.
  3876      Store the length in len.  */
  3877   noverlays = overlays_in (XFIXNUM (beg), XFIXNUM (end), 1, &overlay_vec, &len,
  3878                            true, false, NULL);
  3879 
  3880   /* Make a list of them all.  */
  3881   result = Flist (noverlays, overlay_vec);
  3882 
  3883   xfree (overlay_vec);
  3884   return result;
  3885 }
  3886 
  3887 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
  3888        1, 1, 0,
  3889        doc: /* Return the next position after POS where an overlay starts or ends.
  3890 If there are no overlay boundaries from POS to (point-max),
  3891 the value is (point-max).  */)
  3892   (Lisp_Object pos)
  3893 {
  3894   CHECK_FIXNUM_COERCE_MARKER (pos);
  3895 
  3896   if (!buffer_has_overlays ())
  3897     return make_fixnum (ZV);
  3898 
  3899   return make_fixnum (next_overlay_change (XFIXNUM (pos)));
  3900 }
  3901 
  3902 DEFUN ("previous-overlay-change", Fprevious_overlay_change,
  3903        Sprevious_overlay_change, 1, 1, 0,
  3904        doc: /* Return the previous position before POS where an overlay starts or ends.
  3905 If there are no overlay boundaries from (point-min) to POS,
  3906 the value is (point-min).  */)
  3907   (Lisp_Object pos)
  3908 {
  3909 
  3910   CHECK_FIXNUM_COERCE_MARKER (pos);
  3911 
  3912   if (!buffer_has_overlays ())
  3913     return make_fixnum (BEGV);
  3914 
  3915   return make_fixnum (previous_overlay_change (XFIXNUM (pos)));
  3916 }
  3917 
  3918 
  3919 /* These functions are for debugging overlays.  */
  3920 
  3921 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
  3922        doc: /* Return a list giving all the overlays of the current buffer.
  3923 
  3924 For backward compatibility, the value is actually a list that
  3925 holds another list; the overlays are in the inner list.
  3926 The list you get is a copy, so that changing it has no effect.
  3927 However, the overlays you get are the real objects that the buffer uses. */)
  3928   (void)
  3929 {
  3930   Lisp_Object overlays = Qnil;
  3931   struct itree_node *node;
  3932 
  3933   ITREE_FOREACH (node, current_buffer->overlays, BEG, Z, DESCENDING)
  3934     overlays = Fcons (node->data, overlays);
  3935 
  3936   return Fcons (overlays, Qnil);
  3937 }
  3938 
  3939 DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0,
  3940        doc: /* Recenter the overlays of the current buffer around position POS.
  3941 That makes overlay lookup faster for positions near POS (but perhaps slower
  3942 for positions far away from POS).
  3943 
  3944 Since Emacs 29.1, this function is a no-op, because the implementation
  3945 of overlays changed and their lookup is now fast regardless of their
  3946 position in the buffer.  In particular, this function no longer affects
  3947 the value returned by `overlay-lists'.  */)
  3948   (Lisp_Object pos)
  3949 {
  3950   CHECK_FIXNUM_COERCE_MARKER (pos);
  3951   /* Noop */
  3952   return Qnil;
  3953 }
  3954 
  3955 DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
  3956        doc: /* Get the property of overlay OVERLAY with property name PROP.  */)
  3957   (Lisp_Object overlay, Lisp_Object prop)
  3958 {
  3959   CHECK_OVERLAY (overlay);
  3960   return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0);
  3961 }
  3962 
  3963 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
  3964        doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE.
  3965 VALUE will be returned.*/)
  3966   (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
  3967 {
  3968   Lisp_Object tail;
  3969   struct buffer *b;
  3970   bool changed;
  3971 
  3972   CHECK_OVERLAY (overlay);
  3973 
  3974   b = OVERLAY_BUFFER (overlay);
  3975 
  3976   for (tail = XOVERLAY (overlay)->plist;
  3977        CONSP (tail) && CONSP (XCDR (tail));
  3978        tail = XCDR (XCDR (tail)))
  3979     if (EQ (XCAR (tail), prop))
  3980       {
  3981         changed = !EQ (XCAR (XCDR (tail)), value);
  3982         XSETCAR (XCDR (tail), value);
  3983         goto found;
  3984       }
  3985   /* It wasn't in the list, so add it to the front.  */
  3986   changed = !NILP (value);
  3987   set_overlay_plist
  3988     (overlay, Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist)));
  3989  found:
  3990   if (b)
  3991     {
  3992       if (changed)
  3993         modify_overlay (b, OVERLAY_START (overlay),
  3994                         OVERLAY_END (overlay));
  3995       if (EQ (prop, Qevaporate) && ! NILP (value)
  3996           && (OVERLAY_START (overlay)
  3997               == OVERLAY_END (overlay)))
  3998         Fdelete_overlay (overlay);
  3999     }
  4000 
  4001   return value;
  4002 }
  4003 
  4004 /* Subroutine of report_overlay_modification.  */
  4005 
  4006 /* Lisp vector holding overlay hook functions to call.
  4007    Vector elements come in pairs.
  4008    Each even-index element is a list of hook functions.
  4009    The following odd-index element is the overlay they came from.
  4010 
  4011    Before the buffer change, we fill in this vector
  4012    as we call overlay hook functions.
  4013    After the buffer change, we get the functions to call from this vector.
  4014    This way we always call the same functions before and after the change.  */
  4015 static Lisp_Object last_overlay_modification_hooks;
  4016 
  4017 /* Number of elements actually used in last_overlay_modification_hooks.  */
  4018 static ptrdiff_t last_overlay_modification_hooks_used;
  4019 
  4020 /* Add one functionlist/overlay pair
  4021    to the end of last_overlay_modification_hooks.  */
  4022 
  4023 static void
  4024 add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay)
  4025 {
  4026   ptrdiff_t oldsize = ASIZE (last_overlay_modification_hooks);
  4027 
  4028   if (oldsize - 1 <= last_overlay_modification_hooks_used)
  4029     last_overlay_modification_hooks =
  4030       larger_vector (last_overlay_modification_hooks, 2, -1);
  4031   ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
  4032         functionlist); last_overlay_modification_hooks_used++;
  4033   ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used,
  4034         overlay);      last_overlay_modification_hooks_used++;
  4035 }
  4036 
  4037 /* Run the modification-hooks of overlays that include
  4038    any part of the text in START to END.
  4039    If this change is an insertion, also
  4040    run the insert-before-hooks of overlay starting at END,
  4041    and the insert-after-hooks of overlay ending at START.
  4042 
  4043    This is called both before and after the modification.
  4044    AFTER is true when we call after the modification.
  4045 
  4046    ARG1, ARG2, ARG3 are arguments to pass to the hook functions.
  4047    When AFTER is nonzero, they are the start position,
  4048    the position after the inserted new text,
  4049    and the length of deleted or replaced old text.  */
  4050 
  4051 void
  4052 report_overlay_modification (Lisp_Object start, Lisp_Object end, bool after,
  4053                              Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
  4054 {
  4055   /* True if this change is an insertion.  */
  4056   bool insertion = (after ? XFIXNAT (arg3) == 0 : EQ (start, end));
  4057 
  4058   /* We used to run the functions as soon as we found them and only register
  4059      them in last_overlay_modification_hooks for the purpose of the `after'
  4060      case.  But running elisp code as we traverse the list of overlays is
  4061      painful because the list can be modified by the elisp code so we had to
  4062      copy at several places.  We now simply do a read-only traversal that
  4063      only collects the functions to run and we run them afterwards.  It's
  4064      simpler, especially since all the code was already there.  -stef  */
  4065 
  4066   if (!after)
  4067     {
  4068       struct itree_node *node;
  4069       EMACS_INT begin_arg = XFIXNUM (start);
  4070       EMACS_INT end_arg = XFIXNUM (end);
  4071       /* We are being called before a change.
  4072          Scan the overlays to find the functions to call.  */
  4073       last_overlay_modification_hooks_used = 0;
  4074 
  4075       if (! current_buffer->overlays)
  4076         return;
  4077       ITREE_FOREACH (node, current_buffer->overlays,
  4078                      begin_arg - (insertion ? 1 : 0),
  4079                      end_arg   + (insertion ? 1 : 0),
  4080                      ASCENDING)
  4081         {
  4082           Lisp_Object overlay = node->data;
  4083           ptrdiff_t obegin = OVERLAY_START (overlay);
  4084           ptrdiff_t oend = OVERLAY_END (overlay);
  4085 
  4086           if (insertion && (begin_arg == obegin
  4087                             || end_arg == obegin))
  4088             {
  4089               Lisp_Object prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
  4090               if (!NILP (prop))
  4091                 add_overlay_mod_hooklist (prop, overlay);
  4092             }
  4093           if (insertion && (begin_arg == oend
  4094                             || end_arg == oend))
  4095             {
  4096               Lisp_Object prop = Foverlay_get (overlay, Qinsert_behind_hooks);
  4097               if (!NILP (prop))
  4098                 add_overlay_mod_hooklist (prop, overlay);
  4099             }
  4100           /* Test for intersecting intervals.  This does the right thing
  4101              for both insertion and deletion.  */
  4102           if (! insertion || (end_arg > obegin && begin_arg < oend))
  4103             {
  4104               Lisp_Object prop = Foverlay_get (overlay, Qmodification_hooks);
  4105               if (!NILP (prop))
  4106                 add_overlay_mod_hooklist (prop, overlay);
  4107             }
  4108         }
  4109     }
  4110   {
  4111     /* Call the functions recorded in last_overlay_modification_hooks.
  4112        First copy the vector contents, in case some of these hooks
  4113        do subsequent modification of the buffer.  */
  4114     ptrdiff_t size = last_overlay_modification_hooks_used;
  4115     Lisp_Object *copy;
  4116     ptrdiff_t i;
  4117 
  4118     USE_SAFE_ALLOCA;
  4119     SAFE_ALLOCA_LISP (copy, size);
  4120     memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents,
  4121             size * word_size);
  4122 
  4123     for (i = 0; i < size;)
  4124       {
  4125         Lisp_Object prop_i, overlay_i;
  4126         prop_i = copy[i++];
  4127         overlay_i = copy[i++];
  4128         /* It is possible that the recorded overlay has been deleted
  4129            (which makes its markers' buffers be nil), or that (due to
  4130            some bug) it belongs to a different buffer.  Only run this
  4131            hook if the overlay belongs to the current buffer.  */
  4132         if (OVERLAY_BUFFER (overlay_i) == current_buffer)
  4133           call_overlay_mod_hooks (prop_i, overlay_i, after, arg1, arg2, arg3);
  4134       }
  4135 
  4136     SAFE_FREE ();
  4137   }
  4138 }
  4139 
  4140 static void
  4141 call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, bool after,
  4142                         Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
  4143 {
  4144   while (CONSP (list))
  4145     {
  4146       if (NILP (arg3))
  4147         call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2);
  4148       else
  4149         call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
  4150       list = XCDR (list);
  4151     }
  4152 }
  4153 
  4154 /***********************************************************************
  4155                          Allocation with mmap
  4156  ***********************************************************************/
  4157 
  4158 /* Note: WINDOWSNT implements this stuff on w32heap.c.  */
  4159 #if defined USE_MMAP_FOR_BUFFERS && !defined WINDOWSNT
  4160 
  4161 #include <sys/mman.h>
  4162 
  4163 #ifndef MAP_ANON
  4164 #ifdef MAP_ANONYMOUS
  4165 #define MAP_ANON MAP_ANONYMOUS
  4166 #else
  4167 #define MAP_ANON 0
  4168 #endif
  4169 #endif
  4170 
  4171 #ifndef MAP_FAILED
  4172 #define MAP_FAILED ((void *) -1)
  4173 #endif
  4174 
  4175 #if MAP_ANON == 0
  4176 #include <fcntl.h>
  4177 #endif
  4178 
  4179 
  4180 /* Memory is allocated in regions which are mapped using mmap(2).
  4181    The current implementation lets the system select mapped
  4182    addresses;  we're not using MAP_FIXED in general, except when
  4183    trying to enlarge regions.
  4184 
  4185    Each mapped region starts with a mmap_region structure, the user
  4186    area starts after that structure, aligned to MEM_ALIGN.
  4187 
  4188         +-----------------------+
  4189         | struct mmap_info +    |
  4190         | padding               |
  4191         +-----------------------+
  4192         | user data             |
  4193         |                       |
  4194         |                       |
  4195         +-----------------------+  */
  4196 
  4197 struct mmap_region
  4198 {
  4199   /* User-specified size.  */
  4200   size_t nbytes_specified;
  4201 
  4202   /* Number of bytes mapped */
  4203   size_t nbytes_mapped;
  4204 
  4205   /* Pointer to the location holding the address of the memory
  4206      allocated with the mmap'd block.  The variable actually points
  4207      after this structure.  */
  4208   void **var;
  4209 
  4210   /* Next and previous in list of all mmap'd regions.  */
  4211   struct mmap_region *next, *prev;
  4212 };
  4213 
  4214 /* Doubly-linked list of mmap'd regions.  */
  4215 
  4216 static struct mmap_region *mmap_regions;
  4217 
  4218 /* File descriptor for mmap.  If we don't have anonymous mapping,
  4219    /dev/zero will be opened on it.  */
  4220 
  4221 static int mmap_fd;
  4222 
  4223 /* Page size on this system.  */
  4224 
  4225 static int mmap_page_size;
  4226 
  4227 /* 1 means mmap has been initialized.  */
  4228 
  4229 static bool mmap_initialized_p;
  4230 
  4231 /* Value is X rounded up to the next multiple of N.  */
  4232 
  4233 #define ROUND(X, N)     (((X) + (N) - 1) / (N) * (N))
  4234 
  4235 /* Size of mmap_region structure plus padding.  */
  4236 
  4237 #define MMAP_REGION_STRUCT_SIZE \
  4238      ROUND (sizeof (struct mmap_region), MEM_ALIGN)
  4239 
  4240 /* Given a pointer P to the start of the user-visible part of a mapped
  4241    region, return a pointer to the start of the region.  */
  4242 
  4243 #define MMAP_REGION(P) \
  4244      ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE))
  4245 
  4246 /* Given a pointer P to the start of a mapped region, return a pointer
  4247    to the start of the user-visible part of the region.  */
  4248 
  4249 #define MMAP_USER_AREA(P) \
  4250      ((void *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE))
  4251 
  4252 #define MEM_ALIGN       sizeof (double)
  4253 
  4254 /* Predicate returning true if part of the address range [START .. END]
  4255    is currently mapped.  Used to prevent overwriting an existing
  4256    memory mapping.
  4257 
  4258    Default is to conservatively assume the address range is occupied by
  4259    something else.  This can be overridden by system configuration
  4260    files if system-specific means to determine this exists.  */
  4261 
  4262 #ifndef MMAP_ALLOCATED_P
  4263 #define MMAP_ALLOCATED_P(start, end) 1
  4264 #endif
  4265 
  4266 /* Perform necessary initializations for the use of mmap.  */
  4267 
  4268 static void
  4269 mmap_init (void)
  4270 {
  4271 #if MAP_ANON == 0
  4272   /* The value of mmap_fd is initially 0 in temacs, and -1
  4273      in a dumped Emacs.  */
  4274   if (mmap_fd <= 0)
  4275     {
  4276       /* No anonymous mmap -- we need the file descriptor.  */
  4277       mmap_fd = emacs_open_noquit ("/dev/zero", O_RDONLY, 0);
  4278       if (mmap_fd == -1)
  4279         fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
  4280     }
  4281 #endif /* MAP_ANON == 0 */
  4282 
  4283   if (mmap_initialized_p)
  4284     return;
  4285   mmap_initialized_p = 1;
  4286 
  4287 #if MAP_ANON != 0
  4288   mmap_fd = -1;
  4289 #endif
  4290 
  4291   mmap_page_size = getpagesize ();
  4292 }
  4293 
  4294 /* Unmap a region.  P is a pointer to the start of the user-araa of
  4295    the region.  */
  4296 
  4297 static void
  4298 mmap_free_1 (struct mmap_region *r)
  4299 {
  4300   if (r->next)
  4301     r->next->prev = r->prev;
  4302   if (r->prev)
  4303     r->prev->next = r->next;
  4304   else
  4305     mmap_regions = r->next;
  4306 
  4307   if (munmap (r, r->nbytes_mapped) == -1)
  4308     fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
  4309 }
  4310 
  4311 
  4312 /* Enlarge region R by NPAGES pages.  NPAGES < 0 means shrink R.
  4313    Value is true if successful.  */
  4314 
  4315 static bool
  4316 mmap_enlarge (struct mmap_region *r, int npages)
  4317 {
  4318   char *region_end = (char *) r + r->nbytes_mapped;
  4319   size_t nbytes;
  4320   bool success = 0;
  4321 
  4322   if (npages < 0)
  4323     {
  4324       /* Unmap pages at the end of the region.  */
  4325       nbytes = - npages * mmap_page_size;
  4326       if (munmap (region_end - nbytes, nbytes) == -1)
  4327         fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
  4328       else
  4329         {
  4330           r->nbytes_mapped -= nbytes;
  4331           success = 1;
  4332         }
  4333     }
  4334   else if (npages > 0)
  4335     {
  4336       nbytes = npages * mmap_page_size;
  4337 
  4338       /* Try to map additional pages at the end of the region.  We
  4339          cannot do this if the address range is already occupied by
  4340          something else because mmap deletes any previous mapping.
  4341          I'm not sure this is worth doing, let's see.  */
  4342       if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes))
  4343         {
  4344           void *p;
  4345 
  4346           p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE,
  4347                     MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0);
  4348           if (p == MAP_FAILED)
  4349             ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */
  4350           else if (p != region_end)
  4351             {
  4352               /* Kernels are free to choose a different address.  In
  4353                  that case, unmap what we've mapped above; we have
  4354                  no use for it.  */
  4355               if (munmap (p, nbytes) == -1)
  4356                 fprintf (stderr, "munmap: %s\n", emacs_strerror (errno));
  4357             }
  4358           else
  4359             {
  4360               r->nbytes_mapped += nbytes;
  4361               success = 1;
  4362             }
  4363         }
  4364     }
  4365 
  4366   return success;
  4367 }
  4368 
  4369 
  4370 /* Allocate a block of storage large enough to hold NBYTES bytes of
  4371    data.  A pointer to the data is returned in *VAR.  VAR is thus the
  4372    address of some variable which will use the data area.
  4373 
  4374    The allocation of 0 bytes is valid.
  4375 
  4376    If we can't allocate the necessary memory, set *VAR to null, and
  4377    return null.  */
  4378 
  4379 static void *
  4380 mmap_alloc (void **var, size_t nbytes)
  4381 {
  4382   void *p;
  4383   size_t map;
  4384 
  4385   mmap_init ();
  4386 
  4387   map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size);
  4388   p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
  4389             mmap_fd, 0);
  4390 
  4391   if (p == MAP_FAILED)
  4392     {
  4393       if (errno != ENOMEM)
  4394         fprintf (stderr, "mmap: %s\n", emacs_strerror (errno));
  4395       p = NULL;
  4396     }
  4397   else
  4398     {
  4399       struct mmap_region *r = p;
  4400 
  4401       r->nbytes_specified = nbytes;
  4402       r->nbytes_mapped = map;
  4403       r->var = var;
  4404       r->prev = NULL;
  4405       r->next = mmap_regions;
  4406       if (r->next)
  4407         r->next->prev = r;
  4408       mmap_regions = r;
  4409 
  4410       p = MMAP_USER_AREA (p);
  4411     }
  4412 
  4413   return *var = p;
  4414 }
  4415 
  4416 
  4417 /* Free a block of relocatable storage whose data is pointed to by
  4418    PTR.  Store 0 in *PTR to show there's no block allocated.  */
  4419 
  4420 static void
  4421 mmap_free (void **var)
  4422 {
  4423   mmap_init ();
  4424 
  4425   if (*var)
  4426     {
  4427       mmap_free_1 (MMAP_REGION (*var));
  4428       *var = NULL;
  4429     }
  4430 }
  4431 
  4432 
  4433 /* Given a pointer at address VAR to data allocated with mmap_alloc,
  4434    resize it to size NBYTES.  Change *VAR to reflect the new block,
  4435    and return this value.  If more memory cannot be allocated, then
  4436    leave *VAR unchanged, and return null.  */
  4437 
  4438 static void *
  4439 mmap_realloc (void **var, size_t nbytes)
  4440 {
  4441   void *result;
  4442 
  4443   mmap_init ();
  4444 
  4445   if (*var == NULL)
  4446     result = mmap_alloc (var, nbytes);
  4447   else if (nbytes == 0)
  4448     {
  4449       mmap_free (var);
  4450       result = mmap_alloc (var, nbytes);
  4451     }
  4452   else
  4453     {
  4454       struct mmap_region *r = MMAP_REGION (*var);
  4455       size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE;
  4456 
  4457       if (room < nbytes)
  4458         {
  4459           /* Must enlarge.  */
  4460           void *old_ptr = *var;
  4461 
  4462           /* Try to map additional pages at the end of the region.
  4463              If that fails, allocate a new region,  copy data
  4464              from the old region, then free it.  */
  4465           if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size)
  4466                                 / mmap_page_size)))
  4467             {
  4468               r->nbytes_specified = nbytes;
  4469               *var = result = old_ptr;
  4470             }
  4471           else if (mmap_alloc (var, nbytes))
  4472             {
  4473               memcpy (*var, old_ptr, r->nbytes_specified);
  4474               mmap_free_1 (MMAP_REGION (old_ptr));
  4475               result = *var;
  4476               r = MMAP_REGION (result);
  4477               r->nbytes_specified = nbytes;
  4478             }
  4479           else
  4480             {
  4481               *var = old_ptr;
  4482               result = NULL;
  4483             }
  4484         }
  4485       else if (room - nbytes >= mmap_page_size)
  4486         {
  4487           /* Shrinking by at least a page.  Let's give some
  4488              memory back to the system.
  4489 
  4490              The extra parens are to make the division happens first,
  4491              on positive values, so we know it will round towards
  4492              zero.  */
  4493           mmap_enlarge (r, - ((room - nbytes) / mmap_page_size));
  4494           result = *var;
  4495           r->nbytes_specified = nbytes;
  4496         }
  4497       else
  4498         {
  4499           /* Leave it alone.  */
  4500           result = *var;
  4501           r->nbytes_specified = nbytes;
  4502         }
  4503     }
  4504 
  4505   return result;
  4506 }
  4507 
  4508 
  4509 #endif /* USE_MMAP_FOR_BUFFERS */
  4510 
  4511 
  4512 
  4513 /***********************************************************************
  4514                             Buffer-text Allocation
  4515  ***********************************************************************/
  4516 
  4517 /* Allocate NBYTES bytes for buffer B's text buffer.  */
  4518 
  4519 static void
  4520 alloc_buffer_text (struct buffer *b, ptrdiff_t nbytes)
  4521 {
  4522   void *p;
  4523 
  4524   block_input ();
  4525 #if defined USE_MMAP_FOR_BUFFERS
  4526   p = mmap_alloc ((void **) &b->text->beg, nbytes);
  4527 #elif defined REL_ALLOC
  4528   p = r_alloc ((void **) &b->text->beg, nbytes);
  4529 #else
  4530   p = xmalloc (nbytes);
  4531 #endif
  4532 
  4533   if (p == NULL)
  4534     {
  4535       unblock_input ();
  4536       memory_full (nbytes);
  4537     }
  4538 
  4539   b->text->beg = p;
  4540   unblock_input ();
  4541 }
  4542 
  4543 /* Enlarge buffer B's text buffer by DELTA bytes.  DELTA < 0 means
  4544    shrink it.  */
  4545 
  4546 void
  4547 enlarge_buffer_text (struct buffer *b, ptrdiff_t delta)
  4548 {
  4549   block_input ();
  4550   void *p;
  4551   unsigned char *old_beg = b->text->beg;
  4552   ptrdiff_t old_nbytes =
  4553     BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1;
  4554   ptrdiff_t new_nbytes = old_nbytes + delta;
  4555 
  4556   if (pdumper_object_p (old_beg))
  4557     b->text->beg = NULL;
  4558   else
  4559     old_beg = NULL;
  4560 
  4561 #if defined USE_MMAP_FOR_BUFFERS
  4562   p = mmap_realloc ((void **) &b->text->beg, new_nbytes);
  4563 #elif defined REL_ALLOC
  4564   p = r_re_alloc ((void **) &b->text->beg, new_nbytes);
  4565 #else
  4566   p = xrealloc (b->text->beg, new_nbytes);
  4567 #endif
  4568   __lsan_ignore_object (p);
  4569 
  4570   if (p == NULL)
  4571     {
  4572       if (old_beg)
  4573         b->text->beg = old_beg;
  4574       unblock_input ();
  4575       memory_full (new_nbytes);
  4576     }
  4577 
  4578   if (old_beg)
  4579     memcpy (p, old_beg, min (old_nbytes, new_nbytes));
  4580 
  4581   BUF_BEG_ADDR (b) = p;
  4582   unblock_input ();
  4583 }
  4584 
  4585 
  4586 /* Free buffer B's text buffer.  */
  4587 
  4588 static void
  4589 free_buffer_text (struct buffer *b)
  4590 {
  4591   block_input ();
  4592 
  4593   if (!pdumper_object_p (b->text->beg))
  4594     {
  4595 #if defined USE_MMAP_FOR_BUFFERS
  4596       mmap_free ((void **) &b->text->beg);
  4597 #elif defined REL_ALLOC
  4598       r_alloc_free ((void **) &b->text->beg);
  4599 #else
  4600       xfree (b->text->beg);
  4601 #endif
  4602     }
  4603 
  4604   BUF_BEG_ADDR (b) = NULL;
  4605   unblock_input ();
  4606 }
  4607 
  4608 
  4609 
  4610 /***********************************************************************
  4611                             Initialization
  4612  ***********************************************************************/
  4613 void
  4614 init_buffer_once (void)
  4615 {
  4616   /* TODO: clean up the buffer-local machinery.  Right now,
  4617      we have:
  4618 
  4619      buffer_defaults: default values of buffer-locals
  4620      buffer_local_flags: metadata
  4621      buffer_permanent_local_flags: metadata
  4622      buffer_local_symbols: metadata
  4623 
  4624      There must be a simpler way to store the metadata.
  4625   */
  4626 
  4627   int idx;
  4628 
  4629   /* Items flagged permanent get an explicit permanent-local property
  4630      added in bindings.el, for clarity.  */
  4631   PDUMPER_REMEMBER_SCALAR (buffer_permanent_local_flags);
  4632   memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags);
  4633 
  4634   /* 0 means not a lisp var, -1 means always local, else mask.  */
  4635   memset (&buffer_local_flags, 0, sizeof buffer_local_flags);
  4636   bset_filename (&buffer_local_flags, make_fixnum (-1));
  4637   bset_directory (&buffer_local_flags, make_fixnum (-1));
  4638   bset_backed_up (&buffer_local_flags, make_fixnum (-1));
  4639   bset_save_length (&buffer_local_flags, make_fixnum (-1));
  4640   bset_auto_save_file_name (&buffer_local_flags, make_fixnum (-1));
  4641   bset_read_only (&buffer_local_flags, make_fixnum (-1));
  4642   bset_major_mode (&buffer_local_flags, make_fixnum (-1));
  4643   bset_local_minor_modes (&buffer_local_flags, make_fixnum (-1));
  4644   bset_mode_name (&buffer_local_flags, make_fixnum (-1));
  4645   bset_undo_list (&buffer_local_flags, make_fixnum (-1));
  4646   bset_mark_active (&buffer_local_flags, make_fixnum (-1));
  4647   bset_point_before_scroll (&buffer_local_flags, make_fixnum (-1));
  4648   bset_file_truename (&buffer_local_flags, make_fixnum (-1));
  4649   bset_invisibility_spec (&buffer_local_flags, make_fixnum (-1));
  4650   bset_file_format (&buffer_local_flags, make_fixnum (-1));
  4651   bset_auto_save_file_format (&buffer_local_flags, make_fixnum (-1));
  4652   bset_display_count (&buffer_local_flags, make_fixnum (-1));
  4653   bset_display_time (&buffer_local_flags, make_fixnum (-1));
  4654   bset_enable_multibyte_characters (&buffer_local_flags, make_fixnum (-1));
  4655 
  4656   /* These used to be stuck at 0 by default, but now that the all-zero value
  4657      means Qnil, we have to initialize them explicitly.  */
  4658   bset_name (&buffer_local_flags, make_fixnum (0));
  4659   bset_mark (&buffer_local_flags, make_fixnum (0));
  4660   bset_local_var_alist (&buffer_local_flags, make_fixnum (0));
  4661   bset_keymap (&buffer_local_flags, make_fixnum (0));
  4662   bset_downcase_table (&buffer_local_flags, make_fixnum (0));
  4663   bset_upcase_table (&buffer_local_flags, make_fixnum (0));
  4664   bset_case_canon_table (&buffer_local_flags, make_fixnum (0));
  4665   bset_case_eqv_table (&buffer_local_flags, make_fixnum (0));
  4666   bset_width_table (&buffer_local_flags, make_fixnum (0));
  4667   bset_pt_marker (&buffer_local_flags, make_fixnum (0));
  4668   bset_begv_marker (&buffer_local_flags, make_fixnum (0));
  4669   bset_zv_marker (&buffer_local_flags, make_fixnum (0));
  4670   bset_last_selected_window (&buffer_local_flags, make_fixnum (0));
  4671 
  4672   idx = 1;
  4673   XSETFASTINT (BVAR (&buffer_local_flags, mode_line_format), idx); ++idx;
  4674   XSETFASTINT (BVAR (&buffer_local_flags, abbrev_mode), idx); ++idx;
  4675   XSETFASTINT (BVAR (&buffer_local_flags, overwrite_mode), idx); ++idx;
  4676   XSETFASTINT (BVAR (&buffer_local_flags, case_fold_search), idx); ++idx;
  4677   XSETFASTINT (BVAR (&buffer_local_flags, auto_fill_function), idx); ++idx;
  4678   XSETFASTINT (BVAR (&buffer_local_flags, selective_display), idx); ++idx;
  4679   XSETFASTINT (BVAR (&buffer_local_flags, selective_display_ellipses), idx); ++idx;
  4680   XSETFASTINT (BVAR (&buffer_local_flags, tab_width), idx); ++idx;
  4681   XSETFASTINT (BVAR (&buffer_local_flags, truncate_lines), idx);
  4682   /* Make this one a permanent local.  */
  4683   buffer_permanent_local_flags[idx++] = 1;
  4684   XSETFASTINT (BVAR (&buffer_local_flags, word_wrap), idx); ++idx;
  4685   XSETFASTINT (BVAR (&buffer_local_flags, ctl_arrow), idx); ++idx;
  4686   XSETFASTINT (BVAR (&buffer_local_flags, fill_column), idx); ++idx;
  4687   XSETFASTINT (BVAR (&buffer_local_flags, left_margin), idx); ++idx;
  4688   XSETFASTINT (BVAR (&buffer_local_flags, abbrev_table), idx); ++idx;
  4689   XSETFASTINT (BVAR (&buffer_local_flags, display_table), idx); ++idx;
  4690   XSETFASTINT (BVAR (&buffer_local_flags, syntax_table), idx); ++idx;
  4691   XSETFASTINT (BVAR (&buffer_local_flags, cache_long_scans), idx); ++idx;
  4692   XSETFASTINT (BVAR (&buffer_local_flags, category_table), idx); ++idx;
  4693   XSETFASTINT (BVAR (&buffer_local_flags, bidi_display_reordering), idx); ++idx;
  4694   XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_direction), idx); ++idx;
  4695   XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_separate_re), idx); ++idx;
  4696   XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_start_re), idx); ++idx;
  4697   XSETFASTINT (BVAR (&buffer_local_flags, buffer_file_coding_system), idx);
  4698   /* Make this one a permanent local.  */
  4699   buffer_permanent_local_flags[idx++] = 1;
  4700   XSETFASTINT (BVAR (&buffer_local_flags, left_margin_cols), idx); ++idx;
  4701   XSETFASTINT (BVAR (&buffer_local_flags, right_margin_cols), idx); ++idx;
  4702   XSETFASTINT (BVAR (&buffer_local_flags, left_fringe_width), idx); ++idx;
  4703   XSETFASTINT (BVAR (&buffer_local_flags, right_fringe_width), idx); ++idx;
  4704   XSETFASTINT (BVAR (&buffer_local_flags, fringes_outside_margins), idx); ++idx;
  4705   XSETFASTINT (BVAR (&buffer_local_flags, scroll_bar_width), idx); ++idx;
  4706   XSETFASTINT (BVAR (&buffer_local_flags, scroll_bar_height), idx); ++idx;
  4707   XSETFASTINT (BVAR (&buffer_local_flags, vertical_scroll_bar_type), idx); ++idx;
  4708   XSETFASTINT (BVAR (&buffer_local_flags, horizontal_scroll_bar_type), idx); ++idx;
  4709   XSETFASTINT (BVAR (&buffer_local_flags, indicate_empty_lines), idx); ++idx;
  4710   XSETFASTINT (BVAR (&buffer_local_flags, indicate_buffer_boundaries), idx); ++idx;
  4711   XSETFASTINT (BVAR (&buffer_local_flags, fringe_indicator_alist), idx); ++idx;
  4712   XSETFASTINT (BVAR (&buffer_local_flags, fringe_cursor_alist), idx); ++idx;
  4713   XSETFASTINT (BVAR (&buffer_local_flags, scroll_up_aggressively), idx); ++idx;
  4714   XSETFASTINT (BVAR (&buffer_local_flags, scroll_down_aggressively), idx); ++idx;
  4715   XSETFASTINT (BVAR (&buffer_local_flags, header_line_format), idx); ++idx;
  4716   XSETFASTINT (BVAR (&buffer_local_flags, tab_line_format), idx); ++idx;
  4717   XSETFASTINT (BVAR (&buffer_local_flags, cursor_type), idx); ++idx;
  4718   XSETFASTINT (BVAR (&buffer_local_flags, extra_line_spacing), idx); ++idx;
  4719 #ifdef HAVE_TREE_SITTER
  4720   XSETFASTINT (BVAR (&buffer_local_flags, ts_parser_list), idx); ++idx;
  4721 #endif
  4722   XSETFASTINT (BVAR (&buffer_local_flags, text_conversion_style), idx); ++idx;
  4723   XSETFASTINT (BVAR (&buffer_local_flags, cursor_in_non_selected_windows), idx); ++idx;
  4724 
  4725   /* buffer_local_flags contains no pointers, so it's safe to treat it
  4726      as a blob for pdumper.  */
  4727   PDUMPER_REMEMBER_SCALAR (buffer_local_flags);
  4728 
  4729   /* Need more room? */
  4730   if (idx >= MAX_PER_BUFFER_VARS)
  4731     emacs_abort ();
  4732   last_per_buffer_idx = idx;
  4733   PDUMPER_REMEMBER_SCALAR (last_per_buffer_idx);
  4734 
  4735   /* Make sure all markable slots in buffer_defaults
  4736      are initialized reasonably, so mark_buffer won't choke.  */
  4737   reset_buffer (&buffer_defaults);
  4738   eassert (NILP (BVAR (&buffer_defaults, name)));
  4739   reset_buffer_local_variables (&buffer_defaults, 1);
  4740   eassert (NILP (BVAR (&buffer_local_symbols, name)));
  4741   reset_buffer (&buffer_local_symbols);
  4742   reset_buffer_local_variables (&buffer_local_symbols, 1);
  4743   /* Prevent GC from getting confused.  */
  4744   buffer_defaults.text = &buffer_defaults.own_text;
  4745   buffer_local_symbols.text = &buffer_local_symbols.own_text;
  4746   /* No one will share the text with these buffers, but let's play it safe.  */
  4747   buffer_defaults.indirections = 0;
  4748   buffer_local_symbols.indirections = 0;
  4749   /* Likewise no one will display them.  */
  4750   buffer_defaults.window_count = 0;
  4751   buffer_local_symbols.window_count = 0;
  4752   set_buffer_intervals (&buffer_defaults, NULL);
  4753   set_buffer_intervals (&buffer_local_symbols, NULL);
  4754   /* This is not strictly necessary, but let's make them initialized.  */
  4755   bset_name (&buffer_defaults, build_pure_c_string (" *buffer-defaults*"));
  4756   bset_name (&buffer_local_symbols, build_pure_c_string (" *buffer-local-symbols*"));
  4757   BUFFER_PVEC_INIT (&buffer_defaults);
  4758   BUFFER_PVEC_INIT (&buffer_local_symbols);
  4759 
  4760   /* Set up the default values of various buffer slots.  */
  4761   /* Must do these before making the first buffer! */
  4762 
  4763   /* real setup is done in bindings.el */
  4764   bset_mode_line_format (&buffer_defaults, build_pure_c_string ("%-"));
  4765   bset_header_line_format (&buffer_defaults, Qnil);
  4766   bset_tab_line_format (&buffer_defaults, Qnil);
  4767   bset_abbrev_mode (&buffer_defaults, Qnil);
  4768   bset_overwrite_mode (&buffer_defaults, Qnil);
  4769   bset_case_fold_search (&buffer_defaults, Qt);
  4770   bset_auto_fill_function (&buffer_defaults, Qnil);
  4771   bset_selective_display (&buffer_defaults, Qnil);
  4772   bset_selective_display_ellipses (&buffer_defaults, Qt);
  4773   bset_abbrev_table (&buffer_defaults, Qnil);
  4774   bset_display_table (&buffer_defaults, Qnil);
  4775   bset_undo_list (&buffer_defaults, Qnil);
  4776   bset_mark_active (&buffer_defaults, Qnil);
  4777   bset_file_format (&buffer_defaults, Qnil);
  4778   bset_auto_save_file_format (&buffer_defaults, Qt);
  4779   buffer_defaults.overlays = NULL;
  4780 
  4781   XSETFASTINT (BVAR (&buffer_defaults, tab_width), 8);
  4782   bset_truncate_lines (&buffer_defaults, Qnil);
  4783   bset_word_wrap (&buffer_defaults, Qnil);
  4784   bset_ctl_arrow (&buffer_defaults, Qt);
  4785   bset_bidi_display_reordering (&buffer_defaults, Qt);
  4786   bset_bidi_paragraph_direction (&buffer_defaults, Qnil);
  4787   bset_bidi_paragraph_start_re (&buffer_defaults, Qnil);
  4788   bset_bidi_paragraph_separate_re (&buffer_defaults, Qnil);
  4789   bset_cursor_type (&buffer_defaults, Qt);
  4790   bset_extra_line_spacing (&buffer_defaults, Qnil);
  4791 #ifdef HAVE_TREE_SITTER
  4792   bset_ts_parser_list (&buffer_defaults, Qnil);
  4793 #endif
  4794   bset_text_conversion_style (&buffer_defaults, Qnil);
  4795   bset_cursor_in_non_selected_windows (&buffer_defaults, Qt);
  4796 
  4797   bset_enable_multibyte_characters (&buffer_defaults, Qt);
  4798   bset_buffer_file_coding_system (&buffer_defaults, Qnil);
  4799   XSETFASTINT (BVAR (&buffer_defaults, fill_column), 70);
  4800   XSETFASTINT (BVAR (&buffer_defaults, left_margin), 0);
  4801   bset_cache_long_scans (&buffer_defaults, Qt);
  4802   bset_file_truename (&buffer_defaults, Qnil);
  4803   XSETFASTINT (BVAR (&buffer_defaults, display_count), 0);
  4804   XSETFASTINT (BVAR (&buffer_defaults, left_margin_cols), 0);
  4805   XSETFASTINT (BVAR (&buffer_defaults, right_margin_cols), 0);
  4806   bset_left_fringe_width (&buffer_defaults, Qnil);
  4807   bset_right_fringe_width (&buffer_defaults, Qnil);
  4808   bset_fringes_outside_margins (&buffer_defaults, Qnil);
  4809   bset_scroll_bar_width (&buffer_defaults, Qnil);
  4810   bset_scroll_bar_height (&buffer_defaults, Qnil);
  4811   bset_vertical_scroll_bar_type (&buffer_defaults, Qt);
  4812   bset_horizontal_scroll_bar_type (&buffer_defaults, Qt);
  4813   bset_indicate_empty_lines (&buffer_defaults, Qnil);
  4814   bset_indicate_buffer_boundaries (&buffer_defaults, Qnil);
  4815   bset_fringe_indicator_alist (&buffer_defaults, Qnil);
  4816   bset_fringe_cursor_alist (&buffer_defaults, Qnil);
  4817   bset_scroll_up_aggressively (&buffer_defaults, Qnil);
  4818   bset_scroll_down_aggressively (&buffer_defaults, Qnil);
  4819   bset_display_time (&buffer_defaults, Qnil);
  4820 
  4821   /* Assign the local-flags to the slots that have default values.
  4822      The local flag is a bit that is used in the buffer
  4823      to say that it has its own local value for the slot.
  4824      The local flag bits are in the local_var_flags slot of the buffer.  */
  4825 
  4826   /* Nothing can work if this isn't true.  */
  4827   { verify (sizeof (EMACS_INT) == word_size); }
  4828 
  4829   Vbuffer_alist = Qnil;
  4830   current_buffer = 0;
  4831   pdumper_remember_lv_ptr_raw (&current_buffer, Lisp_Vectorlike);
  4832 
  4833   QSFundamental = build_pure_c_string ("Fundamental");
  4834 
  4835   DEFSYM (Qfundamental_mode, "fundamental-mode");
  4836   bset_major_mode (&buffer_defaults, Qfundamental_mode);
  4837 
  4838   DEFSYM (Qmode_class, "mode-class");
  4839   DEFSYM (Qprotected_field, "protected-field");
  4840 
  4841   DEFSYM (Qpermanent_local, "permanent-local");
  4842   DEFSYM (Qkill_buffer_hook, "kill-buffer-hook");
  4843   Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
  4844 
  4845   /* Super-magic invisible buffer.  */
  4846   Vprin1_to_string_buffer =
  4847     Fget_buffer_create (build_pure_c_string (" prin1"), Qt);
  4848   Vbuffer_alist = Qnil;
  4849 
  4850   Fset_buffer (Fget_buffer_create (build_pure_c_string ("*scratch*"), Qnil));
  4851 
  4852   inhibit_modification_hooks = 0;
  4853 }
  4854 
  4855 void
  4856 init_buffer (void)
  4857 {
  4858   Lisp_Object temp;
  4859 
  4860 #ifdef USE_MMAP_FOR_BUFFERS
  4861   if (dumped_with_unexec_p ())
  4862     {
  4863       Lisp_Object tail, buffer;
  4864 
  4865 #ifndef WINDOWSNT
  4866       /* These must be reset in the dumped Emacs, to avoid stale
  4867          references to mmap'ed memory from before the dump.
  4868 
  4869          WINDOWSNT doesn't need this because it doesn't track mmap'ed
  4870          regions by hand (see w32heap.c, which uses system APIs for
  4871          that purpose), and thus doesn't use mmap_regions.  */
  4872       mmap_regions = NULL;
  4873       mmap_fd = -1;
  4874 #endif
  4875 
  4876       /* The dumped buffers reference addresses of buffer text
  4877          recorded by temacs, that cannot be used by the dumped Emacs.
  4878          We map new memory for their text here.
  4879 
  4880          Implementation notes: the buffers we carry from temacs are:
  4881          " prin1", "*scratch*", " *Minibuf-0*", "*Messages*", and
  4882          " *code-conversion-work*".  They are created by
  4883          init_buffer_once and init_window_once (which are not called
  4884          in the dumped Emacs), and by the first call to coding.c
  4885          routines.  Since FOR_EACH_LIVE_BUFFER only walks the buffers
  4886          in Vbuffer_alist, any buffer we carry from temacs that is
  4887          not in the alist (a.k.a. "magic invisible buffers") should
  4888          be handled here explicitly.  */
  4889       FOR_EACH_LIVE_BUFFER (tail, buffer)
  4890         {
  4891           struct buffer *b = XBUFFER (buffer);
  4892           b->text->beg = NULL;
  4893           enlarge_buffer_text (b, 0);
  4894         }
  4895       /* The " prin1" buffer is not in Vbuffer_alist.  */
  4896       XBUFFER (Vprin1_to_string_buffer)->text->beg = NULL;
  4897       enlarge_buffer_text (XBUFFER (Vprin1_to_string_buffer), 0);
  4898     }
  4899 #endif /* USE_MMAP_FOR_BUFFERS */
  4900 
  4901   AUTO_STRING (scratch, "*scratch*");
  4902   Fset_buffer (Fget_buffer_create (scratch, Qnil));
  4903   if (NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
  4904     Fset_buffer_multibyte (Qnil);
  4905 
  4906   char const *pwd = emacs_wd;
  4907 
  4908   if (!pwd)
  4909     {
  4910       fprintf (stderr, "Error getting directory: %s\n",
  4911                emacs_strerror (errno));
  4912       bset_directory (current_buffer, Qnil);
  4913     }
  4914   else
  4915     {
  4916       /* Maybe this should really use some standard subroutine
  4917          whose definition is filename syntax dependent.  */
  4918       ptrdiff_t len = strlen (pwd);
  4919       bool add_slash = ! IS_DIRECTORY_SEP (pwd[len - 1]);
  4920 
  4921       /* At this moment, we still don't know how to decode the directory
  4922          name.  So, we keep the bytes in unibyte form so that file I/O
  4923          routines correctly get the original bytes.  */
  4924       Lisp_Object dirname = make_unibyte_string (pwd, len + add_slash);
  4925       if (add_slash)
  4926         SSET (dirname, len, DIRECTORY_SEP);
  4927       bset_directory (current_buffer, dirname);
  4928 
  4929       /* Add /: to the front of the name
  4930          if it would otherwise be treated as magic.  */
  4931       temp = Ffind_file_name_handler (BVAR (current_buffer, directory), Qt);
  4932       if (! NILP (temp)
  4933           /* If the default dir is just /, TEMP is non-nil
  4934              because of the ange-ftp completion handler.
  4935              However, it is not necessary to turn / into /:/.
  4936              So avoid doing that.  */
  4937           && strcmp ("/", SSDATA (BVAR (current_buffer, directory))))
  4938         {
  4939           AUTO_STRING (slash_colon, "/:");
  4940           bset_directory (current_buffer,
  4941                           concat2 (slash_colon,
  4942                                    BVAR (current_buffer, directory)));
  4943         }
  4944     }
  4945 
  4946   temp = get_minibuffer (0);
  4947   bset_directory (XBUFFER (temp), BVAR (current_buffer, directory));
  4948 }
  4949 
  4950 /* Similar to defvar_lisp but define a variable whose value is the
  4951    Lisp_Object stored in the current buffer.  LNAME is the Lisp-level
  4952    variable name.  VNAME is the name of the buffer slot.  PREDICATE
  4953    is nil for a general Lisp variable.  If PREDICATE is non-nil, then
  4954    only Lisp values that satisfies the PREDICATE are allowed (except
  4955    that nil is allowed too).  DOC is a dummy where you write the doc
  4956    string as a comment.  */
  4957 
  4958 #define DEFVAR_PER_BUFFER(lname, vname, predicate, doc)         \
  4959   do {                                                          \
  4960     static struct Lisp_Buffer_Objfwd bo_fwd;                    \
  4961     defvar_per_buffer (&bo_fwd, lname, vname, predicate);       \
  4962   } while (0)
  4963 
  4964 static void
  4965 defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring,
  4966                    Lisp_Object *address, Lisp_Object predicate)
  4967 {
  4968   struct Lisp_Symbol *sym;
  4969   int offset;
  4970 
  4971   sym = XSYMBOL (intern (namestring));
  4972   offset = (char *)address - (char *)current_buffer;
  4973 
  4974   bo_fwd->type = Lisp_Fwd_Buffer_Obj;
  4975   bo_fwd->offset = offset;
  4976   bo_fwd->predicate = predicate;
  4977   sym->u.s.declared_special = true;
  4978   sym->u.s.redirect = SYMBOL_FORWARDED;
  4979   SET_SYMBOL_FWD (sym, bo_fwd);
  4980   XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym);
  4981 
  4982   if (PER_BUFFER_IDX (offset) == 0)
  4983     /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
  4984        slot of buffer_local_flags.  */
  4985     emacs_abort ();
  4986 }
  4987 
  4988 #ifdef ITREE_DEBUG
  4989 static Lisp_Object
  4990 make_lispy_itree_node (const struct itree_node *node)
  4991 {
  4992   return listn (12,
  4993                 intern (":begin"),
  4994                 make_fixnum (node->begin),
  4995                 intern (":end"),
  4996                 make_fixnum (node->end),
  4997                 intern (":limit"),
  4998                 make_fixnum (node->limit),
  4999                 intern (":offset"),
  5000                 make_fixnum (node->offset),
  5001                 intern (":rear-advance"),
  5002                 node->rear_advance ? Qt : Qnil,
  5003                 intern (":front-advance"),
  5004                 node->front_advance ? Qt : Qnil);
  5005 }
  5006 
  5007 static Lisp_Object
  5008 overlay_tree (const struct itree_tree *tree,
  5009               const struct itree_node *node)
  5010 {
  5011   if (node == ITREE_NULL)
  5012     return Qnil;
  5013   return list3 (make_lispy_itree_node (node),
  5014                 overlay_tree (tree, node->left),
  5015                 overlay_tree (tree, node->right));
  5016 }
  5017 
  5018 DEFUN ("overlay-tree", Foverlay_tree, Soverlay_tree, 0, 1, 0,
  5019        doc: /* Get the overlay tree for BUFFER.  */)
  5020      (Lisp_Object buffer)
  5021 {
  5022   struct buffer *b = decode_buffer (buffer);
  5023   if (! b->overlays)
  5024     return Qnil;
  5025   return overlay_tree (b->overlays, b->overlays->root);
  5026 }
  5027 #endif
  5028 
  5029 
  5030 
  5031 /* Initialize the buffer routines.  */
  5032 void
  5033 syms_of_buffer (void)
  5034 {
  5035   staticpro (&last_overlay_modification_hooks);
  5036   last_overlay_modification_hooks = make_nil_vector (10);
  5037 
  5038   staticpro (&QSFundamental);
  5039   staticpro (&Vbuffer_alist);
  5040 
  5041   DEFSYM (Qchoice, "choice");
  5042   DEFSYM (Qleft, "left");
  5043   DEFSYM (Qright, "right");
  5044   DEFSYM (Qrange, "range");
  5045 
  5046   DEFSYM (Qpermanent_local_hook, "permanent-local-hook");
  5047   DEFSYM (Qoverlayp, "overlayp");
  5048   DEFSYM (Qevaporate, "evaporate");
  5049   DEFSYM (Qmodification_hooks, "modification-hooks");
  5050   DEFSYM (Qinsert_in_front_hooks, "insert-in-front-hooks");
  5051   DEFSYM (Qinsert_behind_hooks, "insert-behind-hooks");
  5052   DEFSYM (Qget_file_buffer, "get-file-buffer");
  5053   DEFSYM (Qpriority, "priority");
  5054   DEFSYM (Qbefore_string, "before-string");
  5055   DEFSYM (Qafter_string, "after-string");
  5056   DEFSYM (Qfirst_change_hook, "first-change-hook");
  5057   DEFSYM (Qbefore_change_functions, "before-change-functions");
  5058   DEFSYM (Qafter_change_functions, "after-change-functions");
  5059   DEFSYM (Qkill_buffer_query_functions, "kill-buffer-query-functions");
  5060   DEFSYM (Qget_scratch_buffer_create, "get-scratch-buffer-create");
  5061 
  5062   DEFSYM (Qvertical_scroll_bar, "vertical-scroll-bar");
  5063   Fput (Qvertical_scroll_bar, Qchoice, list4 (Qnil, Qt, Qleft, Qright));
  5064   DEFSYM (Qhorizontal_scroll_bar, "horizontal-scroll-bar");
  5065 
  5066   DEFSYM (Qfraction, "fraction");
  5067   Fput (Qfraction, Qrange, Fcons (make_float (0.0), make_float (1.0)));
  5068 
  5069   DEFSYM (Qoverwrite_mode, "overwrite-mode");
  5070   Fput (Qoverwrite_mode, Qchoice,
  5071         list3 (Qnil, intern ("overwrite-mode-textual"),
  5072                Qoverwrite_mode_binary));
  5073 
  5074   Fput (Qprotected_field, Qerror_conditions,
  5075         pure_list (Qprotected_field, Qerror));
  5076   Fput (Qprotected_field, Qerror_message,
  5077         build_pure_c_string ("Attempt to modify a protected field"));
  5078 
  5079   DEFSYM (Qclone_indirect_buffer_hook, "clone-indirect-buffer-hook");
  5080 
  5081   DEFVAR_PER_BUFFER ("tab-line-format",
  5082                      &BVAR (current_buffer, tab_line_format),
  5083                      Qnil,
  5084                      doc: /* Analogous to `mode-line-format', but controls the tab line.
  5085 The tab line appears, optionally, at the top of a window;
  5086 the mode line appears at the bottom.  */);
  5087 
  5088   DEFVAR_PER_BUFFER ("header-line-format",
  5089                      &BVAR (current_buffer, header_line_format),
  5090                      Qnil,
  5091                      doc: /* Analogous to `mode-line-format', but controls the header line.
  5092 The header line appears, optionally, at the top of a window; the mode
  5093 line appears at the bottom.
  5094 
  5095 Also see `header-line-indent-mode' if `display-line-numbers-mode' is
  5096 turned on and header-line text should be aligned with buffer text.  */);
  5097 
  5098   DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format),
  5099                      Qnil,
  5100                      doc: /* Template for displaying mode line for a window's buffer.
  5101 
  5102 The value may be nil, a string, a symbol or a list.
  5103 
  5104 A value of nil means don't display a mode line.
  5105 
  5106 For any symbol other than t or nil, the symbol's value is processed as
  5107  a mode line construct.  As a special exception, if that value is a
  5108  string, the string is processed verbatim, without handling any
  5109  %-constructs (see below).  Also, unless the symbol has a non-nil
  5110  `risky-local-variable' property, all properties in any strings, as
  5111  well as all :eval and :propertize forms in the value, are ignored.
  5112 
  5113 When the value is processed, the window's buffer is temporarily the
  5114 current buffer.
  5115 
  5116 A list whose car is a string or list is processed by processing each
  5117  of the list elements recursively, as separate mode line constructs,
  5118  and concatenating the results.
  5119 
  5120 A list of the form `(:eval FORM)' is processed by evaluating FORM and
  5121  using the result as a mode line construct.  Be careful--FORM should
  5122  not load any files, because that can cause an infinite recursion.
  5123 
  5124 A list of the form `(:propertize ELT PROPS...)' is processed by
  5125  processing ELT as the mode line construct, and adding the text
  5126  properties PROPS to the result.
  5127 
  5128 A list whose car is a symbol is processed by examining the symbol's
  5129  value, and, if that value is non-nil, processing the cadr of the list
  5130  recursively; and if that value is nil, processing the caddr of the
  5131  list recursively.
  5132 
  5133 A list whose car is an integer is processed by processing the cadr of
  5134  the list, and padding (if the number is positive) or truncating (if
  5135  negative) to the width specified by that number.
  5136 
  5137 A string is printed verbatim in the mode line except for %-constructs:
  5138   %b -- print buffer name.
  5139   %c -- print the current column number (this makes editing slower).
  5140         Columns are numbered starting from the left margin, and the
  5141         leftmost column is displayed as zero.
  5142         To make the column number update correctly in all cases,
  5143         `column-number-mode' must be non-nil.
  5144   %C -- Like %c, but the leftmost column is displayed as one.
  5145   %e -- print error message about full memory.
  5146   %f -- print visited file name.
  5147   %F -- print frame name.
  5148   %i -- print the size of the buffer.
  5149   %I -- like %i, but use k, M, G, etc., to abbreviate.
  5150   %l -- print the current line number.
  5151   %n -- print Narrow if appropriate.
  5152   %o -- print percent of window travel through buffer, or Top, Bot or All.
  5153   %p -- print percent of buffer above top of window, or Top, Bot or All.
  5154   %P -- print percent of buffer above bottom of window, perhaps plus Top,
  5155         or print Bottom or All.
  5156   %q -- print percent of buffer above both the top and the bottom of the
  5157         window, separated by ‘-’, or ‘All’.
  5158   %s -- print process status.
  5159   %z -- print mnemonics of keyboard, terminal, and buffer coding systems.
  5160   %Z -- like %z, but including the end-of-line format.
  5161   %& -- print * if the buffer is modified, otherwise hyphen.
  5162   %+ -- print *, % or hyphen (modified, read-only, neither).
  5163   %* -- print %, * or hyphen (read-only, modified, neither).
  5164         For a modified read-only buffer, %+ prints * and %* prints %.
  5165   %@ -- print @ if default-directory is on a remote machine, else hyphen.
  5166   %[ -- print one [ for each recursive editing level.
  5167   %] -- print one ] for each recursive editing level.
  5168   %- -- print enough dashes to fill the mode line.
  5169   %% -- print %.
  5170 Decimal digits after the % specify field width to which to pad.  */);
  5171 
  5172   DEFVAR_PER_BUFFER ("major-mode", &BVAR (current_buffer, major_mode),
  5173                      Qsymbolp,
  5174                      doc: /* Symbol for current buffer's major mode.
  5175 The default value (normally `fundamental-mode') affects new buffers.
  5176 A value of nil means to use the current buffer's major mode, provided
  5177 it is not marked as "special".  */);
  5178 
  5179   DEFVAR_PER_BUFFER ("local-minor-modes",
  5180                      &BVAR (current_buffer, local_minor_modes),
  5181                      Qnil,
  5182                      doc: /* Minor modes currently active in the current buffer.
  5183 This is a list of symbols, or nil if there are no minor modes active.  */);
  5184 
  5185   DEFVAR_PER_BUFFER ("mode-name", &BVAR (current_buffer, mode_name),
  5186                      Qnil,
  5187                      doc: /* Pretty name of current buffer's major mode.
  5188 Usually a string, but can use any of the constructs for `mode-line-format',
  5189 which see.
  5190 Format with `format-mode-line' to produce a string value.  */);
  5191 
  5192   DEFVAR_PER_BUFFER ("local-abbrev-table", &BVAR (current_buffer, abbrev_table), Qnil,
  5193                      doc: /* Local (mode-specific) abbrev table of current buffer.  */);
  5194 
  5195   DEFVAR_PER_BUFFER ("abbrev-mode", &BVAR (current_buffer, abbrev_mode), Qnil,
  5196                      doc: /*  Non-nil if Abbrev mode is enabled.
  5197 Use the command `abbrev-mode' to change this variable.  */);
  5198 
  5199   DEFVAR_PER_BUFFER ("case-fold-search", &BVAR (current_buffer, case_fold_search),
  5200                      Qnil,
  5201                      doc: /* Non-nil if searches and matches should ignore case.  */);
  5202 
  5203   DEFVAR_PER_BUFFER ("fill-column", &BVAR (current_buffer, fill_column),
  5204                      Qintegerp,
  5205                      doc: /* Column beyond which automatic line-wrapping should happen.
  5206 It is used by filling commands, such as `fill-region' and `fill-paragraph',
  5207 and by `auto-fill-mode', which see.
  5208 See also `current-fill-column'.
  5209 Interactively, you can set the buffer local value using \\[set-fill-column].  */);
  5210 
  5211   DEFVAR_PER_BUFFER ("left-margin", &BVAR (current_buffer, left_margin),
  5212                      Qintegerp,
  5213                      doc: /* Column for the default `indent-line-function' to indent to.
  5214 Linefeed indents to this column in Fundamental mode.  */);
  5215 
  5216   DEFVAR_PER_BUFFER ("tab-width", &BVAR (current_buffer, tab_width),
  5217                      Qintegerp,
  5218                      doc: /* Distance between tab stops (for display of tab characters), in columns.
  5219 This controls the width of a TAB character on display.
  5220 The value should be a positive integer.
  5221 Note that this variable doesn't necessarily affect the size of the
  5222 indentation step.  However, if the major mode's indentation facility
  5223 inserts one or more TAB characters, this variable will affect the
  5224 indentation step as well, even if `indent-tabs-mode' is non-nil.  */);
  5225 
  5226   DEFVAR_PER_BUFFER ("ctl-arrow", &BVAR (current_buffer, ctl_arrow), Qnil,
  5227                      doc: /* Non-nil means display control chars with uparrow `^'.
  5228 A value of nil means use backslash `\\' and octal digits.
  5229 This variable does not apply to characters whose display is specified in
  5230 the current display table (if there is one; see `standard-display-table').  */);
  5231 
  5232   DEFVAR_PER_BUFFER ("enable-multibyte-characters",
  5233                      &BVAR (current_buffer, enable_multibyte_characters),
  5234                      Qnil,
  5235                      doc: /* Non-nil means the buffer contents are regarded as multi-byte characters.
  5236 Otherwise they are regarded as unibyte.  This affects the display,
  5237 file I/O and the behavior of various editing commands.
  5238 
  5239 This variable is buffer-local but you cannot set it directly;
  5240 use the function `set-buffer-multibyte' to change a buffer's representation.
  5241 To prevent any attempts to set it or make it buffer-local, Emacs will
  5242 signal an error in those cases.
  5243 See also Info node `(elisp)Text Representations'.  */);
  5244   make_symbol_constant (intern_c_string ("enable-multibyte-characters"));
  5245 
  5246   DEFVAR_PER_BUFFER ("buffer-file-coding-system",
  5247                      &BVAR (current_buffer, buffer_file_coding_system), Qnil,
  5248                      doc: /* Coding system to be used for encoding the buffer contents on saving.
  5249 This variable applies to saving the buffer, and also to `write-region'
  5250 and other functions that use `write-region'.
  5251 It does not apply to sending output to subprocesses, however.
  5252 
  5253 If this is nil, the buffer is saved without any code conversion
  5254 unless some coding system is specified in `file-coding-system-alist'
  5255 for the buffer file.
  5256 
  5257 If the text to be saved cannot be encoded as specified by this variable,
  5258 an alternative encoding is selected by `select-safe-coding-system', which see.
  5259 
  5260 The variable `coding-system-for-write', if non-nil, overrides this variable.
  5261 
  5262 This variable is never applied to a way of decoding a file while reading it.  */);
  5263 
  5264   DEFVAR_PER_BUFFER ("bidi-display-reordering",
  5265                      &BVAR (current_buffer, bidi_display_reordering), Qnil,
  5266                      doc: /* Non-nil means reorder bidirectional text for display in the visual order.
  5267 Setting this to nil is intended for use in debugging the display code.
  5268 Don't set to nil in normal sessions, as that is not supported.
  5269 See also `bidi-paragraph-direction'; setting that non-nil might
  5270 speed up redisplay.  */);
  5271 
  5272   DEFVAR_PER_BUFFER ("bidi-paragraph-start-re",
  5273                      &BVAR (current_buffer, bidi_paragraph_start_re), Qnil,
  5274                      doc: /* If non-nil, a regexp matching a line that starts OR separates paragraphs.
  5275 
  5276 The value of nil means to use empty lines as lines that start and
  5277 separate paragraphs.
  5278 
  5279 When Emacs displays bidirectional text, it by default computes
  5280 the base paragraph direction separately for each paragraph.
  5281 Setting this variable changes the places where paragraph base
  5282 direction is recomputed.
  5283 
  5284 The regexp is always matched after a newline, so it is best to
  5285 anchor it by beginning it with a "^".
  5286 
  5287 If you change the value of this variable, be sure to change
  5288 the value of `bidi-paragraph-separate-re' accordingly.  For
  5289 example, to have a single newline behave as a paragraph separator,
  5290 set both these variables to "^".
  5291 
  5292 See also `bidi-paragraph-direction'.  */);
  5293 
  5294   DEFVAR_PER_BUFFER ("bidi-paragraph-separate-re",
  5295                      &BVAR (current_buffer, bidi_paragraph_separate_re), Qnil,
  5296                      doc: /* If non-nil, a regexp matching a line that separates paragraphs.
  5297 
  5298 The value of nil means to use empty lines as paragraph separators.
  5299 
  5300 When Emacs displays bidirectional text, it by default computes
  5301 the base paragraph direction separately for each paragraph.
  5302 Setting this variable changes the places where paragraph base
  5303 direction is recomputed.
  5304 
  5305 The regexp is always matched after a newline, so it is best to
  5306 anchor it by beginning it with a "^".
  5307 
  5308 If you change the value of this variable, be sure to change
  5309 the value of `bidi-paragraph-start-re' accordingly.  For
  5310 example, to have a single newline behave as a paragraph separator,
  5311 set both these variables to "^".
  5312 
  5313 See also `bidi-paragraph-direction'.  */);
  5314 
  5315   DEFVAR_PER_BUFFER ("bidi-paragraph-direction",
  5316                      &BVAR (current_buffer, bidi_paragraph_direction), Qnil,
  5317                      doc: /* If non-nil, forces directionality of text paragraphs in the buffer.
  5318 
  5319 If this is nil (the default), the direction of each paragraph is
  5320 determined by the first strong directional character of its text.
  5321 The values of `right-to-left' and `left-to-right' override that.
  5322 Any other value is treated as nil.
  5323 
  5324 This variable has no effect unless the buffer's value of
  5325 `bidi-display-reordering' is non-nil.  */);
  5326 
  5327  DEFVAR_PER_BUFFER ("truncate-lines", &BVAR (current_buffer, truncate_lines), Qnil,
  5328                      doc: /* Non-nil means do not display continuation lines.
  5329 Instead, give each line of text just one screen line.
  5330 
  5331 Note that this is overridden by the variable
  5332 `truncate-partial-width-windows' if that variable is non-nil
  5333 and this buffer is not full-frame width.
  5334 
  5335 Minibuffers set this variable to nil.
  5336 
  5337 Don't set this to a non-nil value when `visual-line-mode' is
  5338 turned on, as it could produce confusing results.   */);
  5339 
  5340   DEFVAR_PER_BUFFER ("word-wrap", &BVAR (current_buffer, word_wrap), Qnil,
  5341                      doc: /* Non-nil means to use word-wrapping for continuation lines.
  5342 When word-wrapping is on, continuation lines are wrapped at the space
  5343 or tab character nearest to the right window edge.
  5344 If nil, continuation lines are wrapped at the right screen edge.
  5345 
  5346 This variable has no effect if long lines are truncated (see
  5347 `truncate-lines' and `truncate-partial-width-windows').  If you use
  5348 word-wrapping, you might want to reduce the value of
  5349 `truncate-partial-width-windows', since wrapping can make text readable
  5350 in narrower windows.
  5351 
  5352 Instead of setting this variable directly, most users should use
  5353 Visual Line mode.  Visual Line mode, when enabled, sets `word-wrap'
  5354 to t, and additionally redefines simple editing commands to act on
  5355 visual lines rather than logical lines.  See the documentation of
  5356 `visual-line-mode'.  */);
  5357 
  5358   DEFVAR_PER_BUFFER ("default-directory", &BVAR (current_buffer, directory),
  5359                      Qstringp,
  5360                      doc: /* Name of default directory of current buffer.
  5361 It should be an absolute directory name; on GNU and Unix systems,
  5362 these names start with `/' or `~' and end with `/'.
  5363 To interactively change the default directory, use command `cd'. */);
  5364 
  5365   DEFVAR_PER_BUFFER ("auto-fill-function", &BVAR (current_buffer, auto_fill_function),
  5366                      Qnil,
  5367                      doc: /* Function called (if non-nil) to perform auto-fill.
  5368 It is called after self-inserting any character specified in
  5369 the `auto-fill-chars' table.
  5370 NOTE: This variable is not a hook;
  5371 its value may not be a list of functions.  */);
  5372 
  5373   DEFVAR_PER_BUFFER ("buffer-file-name", &BVAR (current_buffer, filename),
  5374                      Qstringp,
  5375                      doc: /* Name of file visited in current buffer, or nil if not visiting a file.
  5376 This should be an absolute file name.  */);
  5377 
  5378   DEFVAR_PER_BUFFER ("buffer-file-truename", &BVAR (current_buffer, file_truename),
  5379                      Qstringp,
  5380                      doc: /* Abbreviated truename of file visited in current buffer, or nil if none.
  5381 The truename of a file is calculated by `file-truename'
  5382 and then abbreviated with `abbreviate-file-name'.  */);
  5383 
  5384   DEFVAR_PER_BUFFER ("buffer-auto-save-file-name",
  5385                      &BVAR (current_buffer, auto_save_file_name),
  5386                      Qstringp,
  5387                      doc: /* Name of file for auto-saving current buffer.
  5388 If it is nil, that means don't auto-save this buffer.  */);
  5389 
  5390   DEFVAR_PER_BUFFER ("buffer-read-only", &BVAR (current_buffer, read_only), Qnil,
  5391                      doc: /* Non-nil if this buffer is read-only.  */);
  5392 
  5393   DEFVAR_PER_BUFFER ("buffer-backed-up", &BVAR (current_buffer, backed_up), Qnil,
  5394                      doc: /* Non-nil if this buffer's file has been backed up.
  5395 Backing up is done before the first time the file is saved.  */);
  5396 
  5397   DEFVAR_PER_BUFFER ("buffer-saved-size", &BVAR (current_buffer, save_length),
  5398                      Qintegerp,
  5399                      doc: /* Length of current buffer when last read in, saved or auto-saved.
  5400 0 initially.
  5401 -1 means auto-saving turned off until next real save.
  5402 
  5403 If you set this to -2, that means don't turn off auto-saving in this buffer
  5404 if its text size shrinks.   If you use `buffer-swap-text' on a buffer,
  5405 you probably should set this to -2 in that buffer.  */);
  5406 
  5407   DEFVAR_PER_BUFFER ("selective-display", &BVAR (current_buffer, selective_display),
  5408                      Qnil,
  5409                      doc: /* Non-nil enables selective display.
  5410 
  5411 An integer N as value means display only lines
  5412 that start with less than N columns of space.
  5413 
  5414 A value of t means that the character ^M makes itself and
  5415 all the rest of the line invisible; also, when saving the buffer
  5416 in a file, save the ^M as a newline.  This usage is obsolete; use
  5417 overlays or text properties instead.  */);
  5418 
  5419   DEFVAR_PER_BUFFER ("selective-display-ellipses",
  5420                      &BVAR (current_buffer, selective_display_ellipses),
  5421                      Qnil,
  5422                      doc: /* Non-nil means display ... on previous line when a line is invisible.  */);
  5423 
  5424   DEFVAR_PER_BUFFER ("overwrite-mode", &BVAR (current_buffer, overwrite_mode),
  5425                      Qoverwrite_mode,
  5426                      doc: /* Non-nil if self-insertion should replace existing text.
  5427 The value should be one of `overwrite-mode-textual',
  5428 `overwrite-mode-binary', or nil.
  5429 If it is `overwrite-mode-textual', self-insertion still
  5430 inserts at the end of a line, and inserts when point is before a tab,
  5431 until the tab is filled in.
  5432 If `overwrite-mode-binary', self-insertion replaces newlines and tabs too.  */);
  5433 
  5434   DEFVAR_PER_BUFFER ("buffer-display-table", &BVAR (current_buffer, display_table),
  5435                      Qnil,
  5436                      doc: /* Display table that controls display of the contents of current buffer.
  5437 
  5438 If this variable is nil, the value of `standard-display-table' is used.
  5439 Each window can have its own, overriding display table, see
  5440 `set-window-display-table' and `window-display-table'.
  5441 
  5442 The display table is a char-table created with `make-display-table'.
  5443 A char-table is an array indexed by character codes.  Normal array
  5444 primitives `aref' and `aset' can be used to access elements of a char-table.
  5445 
  5446 Each of the char-table elements control how to display the corresponding
  5447 text character: the element at index C in the table says how to display
  5448 the character whose code is C.  Each element should be a vector of
  5449 characters or nil.  The value nil means display the character in the
  5450 default fashion; otherwise, the characters from the vector are delivered
  5451 to the screen instead of the original character.
  5452 
  5453 For example, (aset buffer-display-table ?X [?Y]) tells Emacs
  5454 to display a capital Y instead of each X character.
  5455 
  5456 In addition, a char-table has six extra slots to control the display of:
  5457 
  5458   the end of a truncated screen line (extra-slot 0, a single character);
  5459   the end of a continued line (extra-slot 1, a single character);
  5460   the escape character used to display character codes in octal
  5461     (extra-slot 2, a single character);
  5462   the character used as an arrow for control characters (extra-slot 3,
  5463     a single character);
  5464   the decoration indicating the presence of invisible lines (extra-slot 4,
  5465     a vector of characters);
  5466   the character used to draw the border between side-by-side windows
  5467     (extra-slot 5, a single character).
  5468 
  5469 See also the functions `display-table-slot' and `set-display-table-slot'.  */);
  5470 
  5471   DEFVAR_PER_BUFFER ("left-margin-width", &BVAR (current_buffer, left_margin_cols),
  5472                      Qintegerp,
  5473                      doc: /* Width in columns of left marginal area for display of a buffer.
  5474 A value of nil means no marginal area.
  5475 
  5476 Setting this variable does not take effect until a new buffer is displayed
  5477 in a window.  To make the change take effect, call `set-window-buffer'.  */);
  5478 
  5479   DEFVAR_PER_BUFFER ("right-margin-width", &BVAR (current_buffer, right_margin_cols),
  5480                      Qintegerp,
  5481                      doc: /* Width in columns of right marginal area for display of a buffer.
  5482 A value of nil means no marginal area.
  5483 
  5484 Setting this variable does not take effect until a new buffer is displayed
  5485 in a window.  To make the change take effect, call `set-window-buffer'.  */);
  5486 
  5487   DEFVAR_PER_BUFFER ("left-fringe-width", &BVAR (current_buffer, left_fringe_width),
  5488                      Qintegerp,
  5489                      doc: /* Width of this buffer's left fringe (in pixels).
  5490 A value of 0 means no left fringe is shown in this buffer's window.
  5491 A value of nil means to use the left fringe width from the window's frame.
  5492 
  5493 Setting this variable does not take effect until a new buffer is displayed
  5494 in a window.  To make the change take effect, call `set-window-buffer'.  */);
  5495 
  5496   DEFVAR_PER_BUFFER ("right-fringe-width", &BVAR (current_buffer, right_fringe_width),
  5497                      Qintegerp,
  5498                      doc: /* Width of this buffer's right fringe (in pixels).
  5499 A value of 0 means no right fringe is shown in this buffer's window.
  5500 A value of nil means to use the right fringe width from the window's frame.
  5501 
  5502 Setting this variable does not take effect until a new buffer is displayed
  5503 in a window.  To make the change take effect, call `set-window-buffer'.  */);
  5504 
  5505   DEFVAR_PER_BUFFER ("fringes-outside-margins", &BVAR (current_buffer, fringes_outside_margins),
  5506                      Qnil,
  5507                      doc: /* Non-nil means to display fringes outside display margins.
  5508 A value of nil means to display fringes between margins and buffer text.
  5509 
  5510 Setting this variable does not take effect until a new buffer is displayed
  5511 in a window.  To make the change take effect, call `set-window-buffer'.  */);
  5512 
  5513   DEFVAR_PER_BUFFER ("scroll-bar-width", &BVAR (current_buffer, scroll_bar_width),
  5514                      Qintegerp,
  5515                      doc: /* Width of this buffer's vertical scroll bars in pixels.
  5516 A value of nil means to use the scroll bar width from the window's frame.  */);
  5517 
  5518   DEFVAR_PER_BUFFER ("scroll-bar-height", &BVAR (current_buffer, scroll_bar_height),
  5519                      Qintegerp,
  5520                      doc: /* Height of this buffer's horizontal scroll bars in pixels.
  5521 A value of nil means to use the scroll bar height from the window's frame.  */);
  5522 
  5523   DEFVAR_PER_BUFFER ("vertical-scroll-bar", &BVAR (current_buffer, vertical_scroll_bar_type),
  5524                      Qvertical_scroll_bar,
  5525                      doc: /* Position of this buffer's vertical scroll bar.
  5526 The value takes effect whenever you tell a window to display this buffer;
  5527 for instance, with `set-window-buffer' or when `display-buffer' displays it.
  5528 
  5529 A value of `left' or `right' means put the vertical scroll bar at that side
  5530 of the window; a value of nil means don't show any vertical scroll bars.
  5531 A value of t (the default) means do whatever the window's frame specifies.  */);
  5532 
  5533   DEFVAR_PER_BUFFER ("horizontal-scroll-bar", &BVAR (current_buffer, horizontal_scroll_bar_type),
  5534                      Qnil,
  5535                      doc: /* Position of this buffer's horizontal scroll bar.
  5536 The value takes effect whenever you tell a window to display this buffer;
  5537 for instance, with `set-window-buffer' or when `display-buffer' displays it.
  5538 
  5539 A value of `bottom' means put the horizontal scroll bar at the bottom of
  5540 the window; a value of nil means don't show any horizontal scroll bars.
  5541 A value of t (the default) means do whatever the window's frame
  5542 specifies.  */);
  5543 
  5544   DEFVAR_PER_BUFFER ("indicate-empty-lines",
  5545                      &BVAR (current_buffer, indicate_empty_lines), Qnil,
  5546                      doc: /* Visually indicate unused ("empty") screen lines after the buffer end.
  5547 If non-nil, a bitmap is displayed in the left fringe of a window
  5548 on graphical displays for each screen line that doesn't correspond
  5549 to any buffer text.  */);
  5550 
  5551   DEFVAR_PER_BUFFER ("indicate-buffer-boundaries",
  5552                      &BVAR (current_buffer, indicate_buffer_boundaries), Qnil,
  5553                      doc: /* Visually indicate buffer boundaries and scrolling.
  5554 If non-nil, the first and last line of the buffer are marked in the fringe
  5555 of a window on graphical displays with angle bitmaps, or if the window can be
  5556 scrolled, the top and bottom line of the window are marked with up and down
  5557 arrow bitmaps.
  5558 
  5559 If value is a symbol `left' or `right', both angle and arrow bitmaps
  5560 are displayed in the left or right fringe, resp.  Any other value
  5561 that doesn't look like an alist means display the angle bitmaps in
  5562 the left fringe but no arrows.
  5563 
  5564 You can exercise more precise control by using an alist as the
  5565 value.  Each alist element (INDICATOR . POSITION) specifies
  5566 where to show one of the indicators.  INDICATOR is one of `top',
  5567 `bottom', `up', `down', or t, which specifies the default position,
  5568 and POSITION is one of `left', `right', or nil, meaning do not show
  5569 this indicator.
  5570 
  5571 For example, ((top . left) (t . right)) places the top angle bitmap in
  5572 left fringe, the bottom angle bitmap in right fringe, and both arrow
  5573 bitmaps in right fringe.  To show just the angle bitmaps in the left
  5574 fringe, but no arrow bitmaps, use ((top .  left) (bottom . left)).  */);
  5575 
  5576   DEFVAR_PER_BUFFER ("fringe-indicator-alist",
  5577                      &BVAR (current_buffer, fringe_indicator_alist), Qnil,
  5578                      doc: /* Mapping from logical to physical fringe indicator bitmaps.
  5579 The value is an alist where each element (INDICATOR . BITMAPS)
  5580 specifies the fringe bitmaps used to display a specific logical
  5581 fringe indicator.
  5582 
  5583 INDICATOR specifies the logical indicator type which is one of the
  5584 following symbols: `truncation' , `continuation', `overlay-arrow',
  5585 `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'.
  5586 
  5587 BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies
  5588 the actual bitmap shown in the left or right fringe for the logical
  5589 indicator.  LEFT and RIGHT are the bitmaps shown in the left and/or
  5590 right fringe for the specific indicator.  The LEFT1 or RIGHT1 bitmaps
  5591 are used only for the `bottom' and `top-bottom' indicators when the
  5592 last (only) line has no final newline.  BITMAPS may also be a single
  5593 symbol which is used in both left and right fringes.  */);
  5594 
  5595   DEFVAR_PER_BUFFER ("fringe-cursor-alist",
  5596                      &BVAR (current_buffer, fringe_cursor_alist), Qnil,
  5597                      doc: /* Mapping from logical to physical fringe cursor bitmaps.
  5598 The value is an alist where each element (CURSOR . BITMAP)
  5599 specifies the fringe bitmaps used to display a specific logical
  5600 cursor type in the fringe.
  5601 
  5602 CURSOR specifies the logical cursor type which is one of the following
  5603 symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'.  The last
  5604 one is used to show a hollow cursor on narrow lines display lines
  5605 where the normal hollow cursor will not fit.
  5606 
  5607 BITMAP is the corresponding fringe bitmap shown for the logical
  5608 cursor type.  */);
  5609 
  5610   DEFVAR_PER_BUFFER ("scroll-up-aggressively",
  5611                      &BVAR (current_buffer, scroll_up_aggressively), Qfraction,
  5612                      doc: /* How far to scroll windows upward.
  5613 If you move point off the bottom, the window scrolls automatically.
  5614 This variable controls how far it scrolls.  The value nil, the default,
  5615 means scroll to center point.  A fraction means scroll to put point
  5616 that fraction of the window's height from the bottom of the window.
  5617 When the value is 0.0, point goes at the bottom line, which in the
  5618 simple case that you moved off with C-f means scrolling just one line.
  5619 1.0 means point goes at the top, so that in that simple case, the
  5620 window scrolls by a full window height.  Meaningful values are
  5621 between 0.0 and 1.0, inclusive.  */);
  5622 
  5623   DEFVAR_PER_BUFFER ("scroll-down-aggressively",
  5624                      &BVAR (current_buffer, scroll_down_aggressively), Qfraction,
  5625                      doc: /* How far to scroll windows downward.
  5626 If you move point off the top, the window scrolls automatically.
  5627 This variable controls how far it scrolls.  The value nil, the default,
  5628 means scroll to center point.  A fraction means scroll to put point
  5629 that fraction of the window's height from the top of the window.
  5630 When the value is 0.0, point goes at the top line, which in the
  5631 simple case that you moved off with C-b means scrolling just one line.
  5632 1.0 means point goes at the bottom, so that in that simple case, the
  5633 window scrolls by a full window height.  Meaningful values are
  5634 between 0.0 and 1.0, inclusive.  */);
  5635 
  5636   DEFVAR_LISP ("before-change-functions", Vbefore_change_functions,
  5637                doc: /* List of functions to call before each text change.
  5638 Two arguments are passed to each function: the positions of
  5639 the beginning and end of the range of old text to be changed.
  5640 \(For an insertion, the beginning and end are at the same place.)
  5641 No information is given about the length of the text after the change.
  5642 
  5643 Buffer changes made while executing the `before-change-functions'
  5644 don't call any before-change or after-change functions.
  5645 That's because `inhibit-modification-hooks' is temporarily set non-nil.
  5646 
  5647 If an unhandled error happens in running these functions,
  5648 the variable's value remains nil.  That prevents the error
  5649 from happening repeatedly and making Emacs nonfunctional.  */);
  5650   Vbefore_change_functions = Qnil;
  5651 
  5652   DEFVAR_LISP ("after-change-functions", Vafter_change_functions,
  5653                doc: /* List of functions to call after each text change.
  5654 Three arguments are passed to each function: the positions of
  5655 the beginning and end of the range of changed text,
  5656 and the length in chars of the pre-change text replaced by that range.
  5657 \(For an insertion, the pre-change length is zero;
  5658 for a deletion, that length is the number of chars deleted,
  5659 and the post-change beginning and end are at the same place.)
  5660 
  5661 Buffer changes made while executing the `after-change-functions'
  5662 don't call any before-change or after-change functions.
  5663 That's because `inhibit-modification-hooks' is temporarily set non-nil.
  5664 
  5665 If an unhandled error happens in running these functions,
  5666 the variable's value remains nil.  That prevents the error
  5667 from happening repeatedly and making Emacs nonfunctional.  */);
  5668   Vafter_change_functions = Qnil;
  5669 
  5670   DEFVAR_LISP ("first-change-hook", Vfirst_change_hook,
  5671                doc: /* A list of functions to call before changing a buffer which is unmodified.
  5672 The functions are run using the `run-hooks' function.  */);
  5673   Vfirst_change_hook = Qnil;
  5674 
  5675   DEFVAR_PER_BUFFER ("buffer-undo-list", &BVAR (current_buffer, undo_list), Qnil,
  5676                      doc: /* List of undo entries in current buffer.
  5677 Recent changes come first; older changes follow newer.
  5678 
  5679 An entry (BEG . END) represents an insertion which begins at
  5680 position BEG and ends at position END.
  5681 
  5682 An entry (TEXT . POSITION) represents the deletion of the string TEXT
  5683 from (abs POSITION).  If POSITION is positive, point was at the front
  5684 of the text being deleted; if negative, point was at the end.
  5685 
  5686 An entry (t . TIMESTAMP), where TIMESTAMP is in the style of
  5687 `current-time', indicates that the buffer was previously unmodified;
  5688 TIMESTAMP is the visited file's modification time, as of that time.
  5689 If the modification time of the most recent save is different, this
  5690 entry is obsolete.
  5691 
  5692 An entry (t . 0) means the buffer was previously unmodified but
  5693 its time stamp was unknown because it was not associated with a file.
  5694 An entry (t . -1) is similar, except that it means the buffer's visited
  5695 file did not exist.
  5696 
  5697 An entry (nil PROPERTY VALUE BEG . END) indicates that a text property
  5698 was modified between BEG and END.  PROPERTY is the property name,
  5699 and VALUE is the old value.
  5700 
  5701 An entry (apply FUN-NAME . ARGS) means undo the change with
  5702 \(apply FUN-NAME ARGS).
  5703 
  5704 An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo
  5705 in the active region.  BEG and END is the range affected by this entry
  5706 and DELTA is the number of characters added or deleted in that range by
  5707 this change.
  5708 
  5709 An entry (MARKER . DISTANCE) indicates that the marker MARKER
  5710 was adjusted in position by the offset DISTANCE (an integer).
  5711 
  5712 An entry of the form POSITION indicates that point was at the buffer
  5713 location given by the integer.  Undoing an entry of this form places
  5714 point at POSITION.
  5715 
  5716 Entries with value nil mark undo boundaries.  The undo command treats
  5717 the changes between two undo boundaries as a single step to be undone.
  5718 
  5719 If the value of the variable is t, undo information is not recorded.  */);
  5720 
  5721   DEFVAR_PER_BUFFER ("mark-active", &BVAR (current_buffer, mark_active), Qnil,
  5722                      doc: /* Non-nil means the mark and region are currently active in this buffer.  */);
  5723 
  5724   DEFVAR_PER_BUFFER ("cache-long-scans", &BVAR (current_buffer, cache_long_scans), Qnil,
  5725                      doc: /* Non-nil means that Emacs should use caches in attempt to speedup buffer scans.
  5726 
  5727 There is no reason to set this to nil except for debugging purposes.
  5728 
  5729 Normally, the line-motion functions work by scanning the buffer for
  5730 newlines.  Columnar operations (like `move-to-column' and
  5731 `compute-motion') also work by scanning the buffer, summing character
  5732 widths as they go.  This works well for ordinary text, but if the
  5733 buffer's lines are very long (say, more than 500 characters), these
  5734 motion functions will take longer to execute.  Emacs may also take
  5735 longer to update the display.
  5736 
  5737 If `cache-long-scans' is non-nil, these motion functions cache the
  5738 results of their scans, and consult the cache to avoid rescanning
  5739 regions of the buffer until the text is modified.  The caches are most
  5740 beneficial when they prevent the most searching---that is, when the
  5741 buffer contains long lines and large regions of characters with the
  5742 same, fixed screen width.
  5743 
  5744 When `cache-long-scans' is non-nil, processing short lines will
  5745 become slightly slower (because of the overhead of consulting the
  5746 cache), and the caches will use memory roughly proportional to the
  5747 number of newlines and characters whose screen width varies.
  5748 
  5749 Bidirectional editing also requires buffer scans to find paragraph
  5750 separators.  If you have large paragraphs or no paragraph separators
  5751 at all, these scans may be slow.  If `cache-long-scans' is non-nil,
  5752 results of these scans are cached.  This doesn't help too much if
  5753 paragraphs are of the reasonable (few thousands of characters) size.
  5754 
  5755 The caches require no explicit maintenance; their accuracy is
  5756 maintained internally by the Emacs primitives.  Enabling or disabling
  5757 the cache should not affect the behavior of any of the motion
  5758 functions; it should only affect their performance.  */);
  5759 
  5760   DEFVAR_PER_BUFFER ("point-before-scroll", &BVAR (current_buffer, point_before_scroll), Qnil,
  5761                      doc: /* Value of point before the last series of scroll operations, or nil.  */);
  5762 
  5763   DEFVAR_PER_BUFFER ("buffer-file-format", &BVAR (current_buffer, file_format), Qnil,
  5764                      doc: /* List of formats to use when saving this buffer.
  5765 Formats are defined by `format-alist'.  This variable is
  5766 set when a file is visited.  */);
  5767 
  5768   DEFVAR_PER_BUFFER ("buffer-auto-save-file-format",
  5769                      &BVAR (current_buffer, auto_save_file_format), Qnil,
  5770                      doc: /* Format in which to write auto-save files.
  5771 Should be a list of symbols naming formats that are defined in `format-alist'.
  5772 If it is t, which is the default, auto-save files are written in the
  5773 same format as a regular save would use.  */);
  5774 
  5775   DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
  5776                      &BVAR (current_buffer, invisibility_spec), Qnil,
  5777                      doc: /* Invisibility spec of this buffer.
  5778 The default is t, which means that text is invisible if it has a non-nil
  5779 `invisible' property.
  5780 This variable can also be a list.  The list can have two kinds of elements:
  5781 `ATOM' and `(ATOM . ELLIPSIS)'.  A text character is invisible if its
  5782 `invisible' property is `ATOM', or has an `invisible' property that is a list
  5783 that contains `ATOM'.
  5784 If the `(ATOM . ELLIPSIS)' form is used, and `ELLIPSIS' is non-nil, an
  5785 ellipsis will be displayed after the invisible characters.
  5786 Setting this variable is very fast, much faster than scanning all the text in
  5787 the buffer looking for properties to change.  */);
  5788 
  5789   DEFVAR_PER_BUFFER ("buffer-display-count",
  5790                      &BVAR (current_buffer, display_count), Qintegerp,
  5791                      doc: /* A number incremented each time this buffer is displayed in a window.
  5792 The function `set-window-buffer' increments it.  */);
  5793 
  5794   DEFVAR_PER_BUFFER ("buffer-display-time",
  5795                      &BVAR (current_buffer, display_time), Qnil,
  5796                      doc: /* Time stamp updated each time this buffer is displayed in a window.
  5797 The function `set-window-buffer' updates this variable
  5798 to the value obtained by calling `current-time'.
  5799 If the buffer has never been shown in a window, the value is nil.  */);
  5800 
  5801   DEFVAR_LISP ("transient-mark-mode", Vtransient_mark_mode,
  5802                doc: /*  Non-nil if Transient Mark mode is enabled.
  5803 See the command `transient-mark-mode' for a description of this minor mode.
  5804 
  5805 Non-nil also enables highlighting of the region whenever the mark is active.
  5806 The region is highlighted with the `region' face.
  5807 The variable `highlight-nonselected-windows' controls whether to highlight
  5808 all windows or just the selected window.
  5809 
  5810 Lisp programs may give this variable certain special values:
  5811 
  5812 - The symbol `lambda' enables Transient Mark mode temporarily.
  5813   The mode is disabled again after any subsequent action that would
  5814   normally deactivate the mark (e.g. buffer modification).
  5815 
  5816 - The pair (only . OLDVAL) enables Transient Mark mode
  5817   temporarily.  After any subsequent point motion command that is
  5818   not shift-translated, or any other action that would normally
  5819   deactivate the mark (e.g. buffer modification), the value of
  5820   `transient-mark-mode' is set to OLDVAL.  */);
  5821   Vtransient_mark_mode = Qnil;
  5822 
  5823   DEFVAR_LISP ("inhibit-read-only", Vinhibit_read_only,
  5824                doc: /* Non-nil means disregard read-only status of buffers or characters.
  5825 A non-nil value that is a list means disregard `buffer-read-only' status,
  5826 and disregard a `read-only' text property if the property value is a
  5827 member of the list.  Any other non-nil value means disregard `buffer-read-only'
  5828 and all `read-only' text properties.  */);
  5829   Vinhibit_read_only = Qnil;
  5830 
  5831   DEFVAR_PER_BUFFER ("cursor-type", &BVAR (current_buffer, cursor_type), Qnil,
  5832                      doc: /* Cursor to use when this buffer is in the selected window.
  5833 Values are interpreted as follows:
  5834 
  5835   t               use the cursor specified for the frame
  5836   nil             don't display a cursor
  5837   box             display a filled box cursor
  5838   (box . SIZE)    display a filled box cursor, but make it
  5839                   hollow if cursor is under masked image larger than
  5840                   SIZE pixels in either dimension.
  5841   hollow          display a hollow box cursor
  5842   bar             display a vertical bar cursor with default width
  5843   (bar . WIDTH)   display a vertical bar cursor with width WIDTH
  5844   hbar            display a horizontal bar cursor with default height
  5845   (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT
  5846   ANYTHING ELSE   display a hollow box cursor
  5847 
  5848 WIDTH and HEIGHT can't exceed the frame's canonical character size.
  5849 
  5850 When the buffer is displayed in a non-selected window, the
  5851 cursor's appearance is instead controlled by the variable
  5852 `cursor-in-non-selected-windows'.  */);
  5853 
  5854   DEFVAR_PER_BUFFER ("line-spacing",
  5855                      &BVAR (current_buffer, extra_line_spacing), Qnumberp,
  5856                      doc: /* Additional space to put between lines when displaying a buffer.
  5857 The space is measured in pixels, and put below lines on graphic displays,
  5858 see `display-graphic-p'.
  5859 If value is a floating point number, it specifies the spacing relative
  5860 to the default frame line height.  A value of nil means add no extra space.  */);
  5861 
  5862   DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows",
  5863                      &BVAR (current_buffer, cursor_in_non_selected_windows), Qnil,
  5864                      doc: /* Non-nil means show a cursor in non-selected windows.
  5865 If nil, only shows a cursor in the selected window.
  5866 If t, displays a cursor related to the usual cursor type
  5867 \(a solid box becomes hollow, a bar becomes a narrower bar).
  5868 You can also specify the cursor type as in the `cursor-type' variable.
  5869 Use Custom to set this variable and update the display.  */);
  5870 
  5871   /* While this is defined here, each *term.c module must implement
  5872      the logic itself.  */
  5873 
  5874   DEFVAR_PER_BUFFER ("text-conversion-style", &BVAR (current_buffer,
  5875                                                      text_conversion_style),
  5876                      Qnil,
  5877     doc: /* How the on screen keyboard's input method should insert in this buffer.
  5878 When nil, the input method will be disabled and an ordinary keyboard
  5879 will be displayed in its place.
  5880 When the symbol `action', the input method will insert text directly, but
  5881 will send `return' key events instead of inserting new line characters.
  5882 Any other value means that the input method will insert text directly.
  5883 
  5884 If you need to make non-buffer local changes to this variable, use
  5885 `overriding-text-conversion-style', which see.
  5886 
  5887 This variable does not take immediate effect when set; rather, it
  5888 takes effect upon the next redisplay after the selected window or
  5889 buffer changes.  */);
  5890 
  5891   DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions,
  5892                doc: /* List of functions called with no args to query before killing a buffer.
  5893 The buffer being killed will be current while the functions are running.
  5894 See `kill-buffer'.
  5895 
  5896 If any of them returns nil, the buffer is not killed.  Functions run by
  5897 this hook are supposed to not change the current buffer.
  5898 
  5899 This hook is not run for internal or temporary buffers created by
  5900 `get-buffer-create' or `generate-new-buffer' with argument
  5901 INHIBIT-BUFFER-HOOKS non-nil.  */);
  5902   Vkill_buffer_query_functions = Qnil;
  5903 
  5904   DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
  5905                doc: /* Normal hook run before changing the major mode of a buffer.
  5906 The function `kill-all-local-variables' runs this before doing anything else.  */);
  5907   Vchange_major_mode_hook = Qnil;
  5908   DEFSYM (Qchange_major_mode_hook, "change-major-mode-hook");
  5909 
  5910   DEFVAR_LISP ("buffer-list-update-hook", Vbuffer_list_update_hook,
  5911                doc: /* Hook run when the buffer list changes.
  5912 Functions (implicitly) running this hook are `get-buffer-create',
  5913 `make-indirect-buffer', `rename-buffer', `kill-buffer', `bury-buffer'
  5914 and `select-window'.  This hook is not run for internal or temporary
  5915 buffers created by `get-buffer-create' or `generate-new-buffer' with
  5916 argument INHIBIT-BUFFER-HOOKS non-nil.
  5917 
  5918 Functions run by this hook should avoid calling `select-window' with a
  5919 nil NORECORD argument since it may lead to infinite recursion.  */);
  5920   Vbuffer_list_update_hook = Qnil;
  5921   DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook");
  5922 
  5923   DEFVAR_BOOL ("kill-buffer-delete-auto-save-files",
  5924                kill_buffer_delete_auto_save_files,
  5925                doc: /* If non-nil, offer to delete any autosave file when killing a buffer.
  5926 
  5927 If `delete-auto-save-files' is nil, any autosave deletion is inhibited.  */);
  5928   kill_buffer_delete_auto_save_files = 0;
  5929 
  5930   DEFVAR_BOOL ("delete-auto-save-files", delete_auto_save_files,
  5931                doc: /* Non-nil means delete auto-save file when a buffer is saved.
  5932 This is the default.  If nil, auto-save file deletion is inhibited.  */);
  5933   delete_auto_save_files = 1;
  5934 
  5935   DEFVAR_LISP ("clone-indirect-buffer-hook", Vclone_indirect_buffer_hook,
  5936                doc: /* Normal hook to run in the new buffer at the end of `make-indirect-buffer'.
  5937 
  5938 Since `clone-indirect-buffer' calls `make-indirect-buffer', this hook
  5939 will run for `clone-indirect-buffer' calls as well.  */);
  5940   Vclone_indirect_buffer_hook = Qnil;
  5941 
  5942   DEFVAR_LISP ("long-line-threshold", Vlong_line_threshold,
  5943     doc: /* Line length above which to use redisplay shortcuts.
  5944 
  5945 The value should be a positive integer or nil.
  5946 If the value is an integer, shortcuts in the display code intended
  5947 to speed up redisplay for long lines will automatically be enabled
  5948 in buffers which contain one or more lines whose length is above
  5949 this threshold.
  5950 If nil, these display shortcuts will always remain disabled.
  5951 
  5952 There is no reason to change that value except for debugging purposes.  */);
  5953   XSETFASTINT (Vlong_line_threshold, 50000);
  5954 
  5955   DEFVAR_INT ("long-line-optimizations-region-size",
  5956               long_line_optimizations_region_size,
  5957               doc: /* Region size for narrowing in buffers with long lines.
  5958 
  5959 This variable has effect only in buffers in which
  5960 `long-line-optimizations-p' is non-nil.  For performance reasons, in
  5961 such buffers, the `fontification-functions', `pre-command-hook' and
  5962 `post-command-hook' hooks are executed on a narrowed buffer around
  5963 point, as if they were called in a `with-restriction' form with a label.
  5964 This variable specifies the size of the narrowed region around point.
  5965 
  5966 To disable that narrowing, set this variable to 0.
  5967 
  5968 See also `long-line-optimizations-bol-search-limit'.
  5969 
  5970 There is no reason to change that value except for debugging purposes.  */);
  5971   long_line_optimizations_region_size = 500000;
  5972 
  5973   DEFVAR_INT ("long-line-optimizations-bol-search-limit",
  5974               long_line_optimizations_bol_search_limit,
  5975               doc: /* Limit for beginning of line search in buffers with long lines.
  5976 
  5977 This variable has effect only in buffers in which
  5978 `long-line-optimizations-p' is non-nil.  For performance reasons, in
  5979 such buffers, the `fontification-functions', `pre-command-hook' and
  5980 `post-command-hook' hooks are executed on a narrowed buffer around
  5981 point, as if they were called in a `with-restriction' form with a label.
  5982 The variable `long-line-optimizations-region-size' specifies the
  5983 size of the narrowed region around point.  This variable, which should
  5984 be a small integer, specifies the number of characters by which that
  5985 region can be extended backwards to make it start at the beginning of
  5986 a line.
  5987 
  5988 There is no reason to change that value except for debugging purposes.  */);
  5989   long_line_optimizations_bol_search_limit = 128;
  5990 
  5991   DEFVAR_INT ("large-hscroll-threshold", large_hscroll_threshold,
  5992     doc: /* Horizontal scroll of truncated lines above which to use redisplay shortcuts.
  5993 
  5994 The value should be a positive integer.
  5995 
  5996 Shortcuts in the display code intended to speed up redisplay for long
  5997 and truncated lines will automatically be enabled when a line's
  5998 horizontal scroll amount is or about to become larger than the value
  5999 of this variable.
  6000 
  6001 This variable has effect only in buffers which contain one or more
  6002 lines whose length is above `long-line-threshold', which see.
  6003 To disable redisplay shortcuts for long truncated line, set this
  6004 variable to `most-positive-fixnum'.
  6005 
  6006 There is no reason to change that value except for debugging purposes.  */);
  6007   large_hscroll_threshold = 10000;
  6008 
  6009   defsubr (&Sbuffer_live_p);
  6010   defsubr (&Sbuffer_list);
  6011   defsubr (&Sget_buffer);
  6012   defsubr (&Sget_file_buffer);
  6013   defsubr (&Sget_buffer_create);
  6014   defsubr (&Smake_indirect_buffer);
  6015   defsubr (&Sgenerate_new_buffer_name);
  6016   defsubr (&Sbuffer_name);
  6017   defsubr (&Sbuffer_file_name);
  6018   defsubr (&Sbuffer_base_buffer);
  6019   defsubr (&Sbuffer_local_value);
  6020   defsubr (&Sbuffer_local_variables);
  6021   defsubr (&Sbuffer_modified_p);
  6022   defsubr (&Sforce_mode_line_update);
  6023   defsubr (&Sset_buffer_modified_p);
  6024   defsubr (&Sbuffer_modified_tick);
  6025   defsubr (&Sinternal__set_buffer_modified_tick);
  6026   defsubr (&Sbuffer_chars_modified_tick);
  6027   defsubr (&Srename_buffer);
  6028   defsubr (&Sother_buffer);
  6029   defsubr (&Sbuffer_enable_undo);
  6030   defsubr (&Skill_buffer);
  6031   defsubr (&Sbury_buffer_internal);
  6032   defsubr (&Sset_buffer_major_mode);
  6033   defsubr (&Scurrent_buffer);
  6034   defsubr (&Sset_buffer);
  6035   defsubr (&Sbarf_if_buffer_read_only);
  6036   defsubr (&Serase_buffer);
  6037   defsubr (&Sbuffer_swap_text);
  6038   defsubr (&Sset_buffer_multibyte);
  6039   defsubr (&Skill_all_local_variables);
  6040 
  6041   defsubr (&Soverlayp);
  6042   defsubr (&Smake_overlay);
  6043   defsubr (&Sdelete_overlay);
  6044   defsubr (&Sdelete_all_overlays);
  6045   defsubr (&Smove_overlay);
  6046   defsubr (&Soverlay_start);
  6047   defsubr (&Soverlay_end);
  6048   defsubr (&Soverlay_buffer);
  6049   defsubr (&Soverlay_properties);
  6050   defsubr (&Soverlays_at);
  6051   defsubr (&Soverlays_in);
  6052   defsubr (&Snext_overlay_change);
  6053   defsubr (&Sprevious_overlay_change);
  6054   defsubr (&Soverlay_recenter);
  6055   defsubr (&Soverlay_lists);
  6056   defsubr (&Soverlay_get);
  6057   defsubr (&Soverlay_put);
  6058   defsubr (&Srestore_buffer_modified_p);
  6059 
  6060   DEFSYM (Qautosaved, "autosaved");
  6061 
  6062 #ifdef ITREE_DEBUG
  6063   defsubr (&Soverlay_tree);
  6064 #endif
  6065 
  6066   DEFSYM (Qkill_buffer__possibly_save, "kill-buffer--possibly-save");
  6067 
  6068   DEFSYM (Qbuffer_stale_function, "buffer-stale-function");
  6069 
  6070   Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
  6071 }

/* [<][>][^][v][top][bottom][index][help] */