Subject: [PATCH v3 07/15] Add BVAR_OR_DEFAULT macro as a stub

From: Spencer Baugh <[email-masked]>
Date: Thu, 6 May 2021 17:33:38 -0400

For buffer variables without a default, we still use BVAR.  For any
buffer variable with a default, we use BVAR_OR_DEFAULT.  A later
commit will statically enforce this.

This has no functional change in this commit, but syntactically
differentiating between buffer variables with and without defaults
allows us to have different behavior for each kind in later commits.

* src/buffer.h (BVAR_OR_DEFAULT):
Add.
* src/bidi.c (bidi_at_paragraph_end, bidi_paragraph_cache_on_off)
(bidi_find_paragraph_start):
* src/buffer.c (swapfield_defaulted, Fbuffer_swap_text):
* src/buffer.h (SANE_TAB_WIDTH, CHARACTER_WIDTH):
* src/category.c (check_category_table, Fcategory_table)
(char_category_set):
* src/cmds.c (internal_self_insert):
* src/editfns.c (Fcompare_buffer_substrings)
(Fchar_equal):
* src/fileio.c (choose_write_coding_system):
* src/fns.c (extract_data_from_object):
* src/fringe.c (get_logical_cursor_bitmap, get_logical_fringe_bitmap)
(update_window_fringes):
* src/hbfont.c (hbfont_shape):
* src/indent.c (buffer_display_table, width_run_cache_on_off, current_column)
(scan_for_column, compute_motion, vmotion):
* src/msdos.c (IT_frame_up_to_date):
* src/search.c (compile_pattern_1, compile_pattern, looking_at_1)
(string_match_1, newline_cache_on_off, search_command, Fnewline_cache_check):
* src/syntax.c
(update_syntax_table, Fsyntax_table, Fmodify_syntax_entry):
* src/syntax.h
(syntax_property_entry, SETUP_BUFFER_SYNTAX_TABLE):
* src/window.c
(window_display_table, set_window_buffer, window_wants_mode_line)
(window_wants_header_line, window_wants_tab_line):
* src/xdisp.c
(fill_column_indicator_column, default_line_pixel_height, pos_visible_p)
(init_iterator, reseat_to_string, set_message_1)
(text_outside_line_unchanged_p, try_scrolling, try_cursor_movement)
(redisplay_window, try_window_reusing_current_matrix, row_containing_pos)
(try_window_id, display_line, Fcurrent_bidi_paragraph_direction)
(Fbidi_find_overridden_directionality, display_mode_lines, decode_mode_spec)
(display_count_lines, get_window_cursor_type, note_mouse_highlight):
Use BVAR_OR_DEFAULT.
---
 src/bidi.c     |  19 ++++----
 src/buffer.c   |  14 ++++--
 src/buffer.h   |   6 ++-
 src/category.c |   6 +--
 src/cmds.c     |   6 +--
 src/editfns.c  |   4 +-
 src/fileio.c   |   9 ++--
 src/fns.c      |   8 +--
 src/fringe.c   |  21 ++++----
 src/hbfont.c   |   2 +-
 src/indent.c   |  34 ++++++-------
 src/msdos.c    |   6 +--
 src/search.c   |  21 ++++----
 src/syntax.c   |   7 +--
 src/syntax.h   |   5 +-
 src/window.c   |  29 +++++------
 src/xdisp.c    | 130 +++++++++++++++++++++++++------------------------
 17 files changed, 173 insertions(+), 154 deletions(-)

diff --git a/src/bidi.c b/src/bidi.c
index 1413ba6b88..0d0587f5f5 100644
--- a/src/bidi.c
+++ b/src/bidi.c
@@ -1451,12 +1451,12 @@ bidi_at_paragraph_end (ptrdiff_t charpos, ptrdiff_t bytepos)
   Lisp_Object start_re;
   ptrdiff_t val;
 
-  if (STRINGP (BVAR (current_buffer, bidi_paragraph_separate_re)))
-    sep_re = BVAR (current_buffer, bidi_paragraph_separate_re);
+  if (STRINGP (BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_separate_re)))
+    sep_re = BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_separate_re);
   else
     sep_re = paragraph_separate_re;
-  if (STRINGP (BVAR (current_buffer, bidi_paragraph_start_re)))
-    start_re = BVAR (current_buffer, bidi_paragraph_start_re);
+  if (STRINGP (BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_start_re)))
+    start_re = BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_start_re);
   else
     start_re = paragraph_start_re;
 
@@ -1500,10 +1500,10 @@ bidi_paragraph_cache_on_off (void)
      This is because doing so will just make the cache pure overhead,
      since if we turn it on via indirect buffer, it will be
      immediately turned off by its base buffer.  */
-  if (NILP (BVAR (current_buffer, cache_long_scans)))
+  if (NILP (BVAR_OR_DEFAULT (current_buffer, cache_long_scans)))
     {
       if (!indirect_p
-	  || NILP (BVAR (cache_buffer, cache_long_scans)))
+	  || NILP (BVAR_OR_DEFAULT (cache_buffer, cache_long_scans)))
 	{
 	  if (cache_buffer->bidi_paragraph_cache)
 	    {
@@ -1516,7 +1516,7 @@ bidi_paragraph_cache_on_off (void)
   else
     {
       if (!indirect_p
-	  || !NILP (BVAR (cache_buffer, cache_long_scans)))
+	  || !NILP (BVAR_OR_DEFAULT (cache_buffer, cache_long_scans)))
 	{
 	  if (!cache_buffer->bidi_paragraph_cache)
 	    cache_buffer->bidi_paragraph_cache = new_region_cache ();
@@ -1538,9 +1538,8 @@ bidi_paragraph_cache_on_off (void)
 static ptrdiff_t
 bidi_find_paragraph_start (ptrdiff_t pos, ptrdiff_t pos_byte)
 {
-  Lisp_Object re =
-    STRINGP (BVAR (current_buffer, bidi_paragraph_start_re))
-    ? BVAR (current_buffer, bidi_paragraph_start_re)
+  Lisp_Object re = STRINGP (BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_start_re))
+    ? BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_start_re)
     : paragraph_start_re;
   ptrdiff_t limit = ZV, limit_byte = ZV_BYTE;
   struct region_cache *bpc = bidi_paragraph_cache_on_off ();
diff --git a/src/buffer.c b/src/buffer.c
index c75dcbcffb..cc2df67f6c 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2382,6 +2382,12 @@ results, see Info node `(elisp)Swapping Text'.  */)
     bset_##field (other_buffer, BVAR (current_buffer, field));	\
     bset_##field (current_buffer, tmp##field);			\
   } while (0)
+#define swapfield_defaulted(field, type) \
+  do {							\
+    type tmp##field = BVAR_OR_DEFAULT (other_buffer, field);		\
+    bset_##field (other_buffer, BVAR_OR_DEFAULT (current_buffer, field));	\
+    bset_##field (current_buffer, tmp##field);			\
+  } while (0)
 
   swapfield (own_text, struct buffer_text);
   eassert (current_buffer->text == &current_buffer->own_text);
@@ -2415,10 +2421,10 @@ results, see Info node `(elisp)Swapping Text'.  */)
   swapfield_ (mark, Lisp_Object);
   swapfield_ (mark_active, Lisp_Object); /* Belongs with the `mark'.  */
   swapfield_ (enable_multibyte_characters, Lisp_Object);
-  swapfield_ (bidi_display_reordering, Lisp_Object);
-  swapfield_ (bidi_paragraph_direction, Lisp_Object);
-  swapfield_ (bidi_paragraph_separate_re, Lisp_Object);
-  swapfield_ (bidi_paragraph_start_re, Lisp_Object);
+  swapfield_defaulted (bidi_display_reordering, Lisp_Object);
+  swapfield_defaulted (bidi_paragraph_direction, Lisp_Object);
+  swapfield_defaulted (bidi_paragraph_separate_re, Lisp_Object);
+  swapfield_defaulted (bidi_paragraph_start_re, Lisp_Object);
   /* FIXME: Not sure what we should do with these *_marker fields.
      Hopefully they're just nil anyway.  */
   swapfield_ (pt_marker, Lisp_Object);
diff --git a/src/buffer.h b/src/buffer.h
index fff2d27ea0..0af51d3348 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -284,6 +284,8 @@ struct buffer_text
 
 #define BVAR(buf, field) ((buf)->field ## _)
 
+#define BVAR_OR_DEFAULT(buf, field) BVAR (buf, field)
+
 /* Max number of builtin per-buffer variables.  */
 enum { MAX_PER_BUFFER_VARS = 50 };
 
@@ -1556,7 +1558,7 @@ sanitize_tab_width (Lisp_Object width)
 INLINE int
 SANE_TAB_WIDTH (struct buffer *buf)
 {
-  return sanitize_tab_width (BVAR (buf, tab_width));
+  return sanitize_tab_width (BVAR_OR_DEFAULT (buf, tab_width));
 }
 
 /* Return a non-outlandish value for a character width.  */
@@ -1580,7 +1582,7 @@ CHARACTER_WIDTH (int c)
 			(XFIXNUM (CHAR_TABLE_REF (Vchar_width_table, c))))
 	  : c == '\t' ? SANE_TAB_WIDTH (current_buffer)
 	  : c == '\n' ? 0
-	  : !NILP (BVAR (current_buffer, ctl_arrow)) ? 2 : 4);
+	  : !NILP (BVAR_OR_DEFAULT (current_buffer, ctl_arrow)) ? 2 : 4);
 }

diff --git a/src/category.c b/src/category.c
index ec8f61f7f0..522f4da697 100644
--- a/src/category.c
+++ b/src/category.c
@@ -180,7 +180,7 @@ static Lisp_Object
 check_category_table (Lisp_Object table)
 {
   if (NILP (table))
-    return BVAR (current_buffer, category_table);
+    return BVAR_OR_DEFAULT (current_buffer, category_table);
   CHECK_TYPE (!NILP (Fcategory_table_p (table)), Qcategory_table_p, table);
   return table;
 }
@@ -190,7 +190,7 @@ DEFUN ("category-table", Fcategory_table, Scategory_table, 0, 0, 0,
 This is the one specified by the current buffer.  */)
   (void)
 {
-  return BVAR (current_buffer, category_table);
+  return BVAR_OR_DEFAULT (current_buffer, category_table);
 }
 
 DEFUN ("standard-category-table", Fstandard_category_table,
@@ -281,7 +281,7 @@ Return TABLE.  */)
 Lisp_Object
 char_category_set (int c)
 {
-  return CHAR_TABLE_REF (BVAR (current_buffer, category_table), c);
+  return CHAR_TABLE_REF (BVAR_OR_DEFAULT (current_buffer, category_table), c);
 }
 
 DEFUN ("char-category-set", Fchar_category_set, Schar_category_set, 1, 1, 0,
diff --git a/src/cmds.c b/src/cmds.c
index c8a96d918c..a355142480 100644
--- a/src/cmds.c
+++ b/src/cmds.c
@@ -320,7 +320,7 @@ internal_self_insert (int c, EMACS_INT n)
   ptrdiff_t chars_to_delete = 0;
   ptrdiff_t spaces_to_insert = 0;
 
-  overwrite = BVAR (current_buffer, overwrite_mode);
+  overwrite = BVAR_OR_DEFAULT (current_buffer, overwrite_mode);
   if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions))
     hairy = 1;
 
@@ -406,7 +406,7 @@ internal_self_insert (int c, EMACS_INT n)
 
   synt = SYNTAX (c);
 
-  if (!NILP (BVAR (current_buffer, abbrev_mode))
+  if (!NILP (BVAR_OR_DEFAULT (current_buffer, abbrev_mode))
       && synt != Sword
       && NILP (BVAR (current_buffer, read_only))
       && PT > BEGV
@@ -474,7 +474,7 @@ internal_self_insert (int c, EMACS_INT n)
   if ((CHAR_TABLE_P (Vauto_fill_chars)
        ? !NILP (CHAR_TABLE_REF (Vauto_fill_chars, c))
        : (c == ' ' || c == '\n'))
-      && !NILP (BVAR (current_buffer, auto_fill_function)))
+      && !NILP (BVAR_OR_DEFAULT (current_buffer, auto_fill_function)))
     {
       Lisp_Object auto_fill_result;
 
diff --git a/src/editfns.c b/src/editfns.c
index 04b8e85d9c..01e56843a6 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1769,7 +1769,7 @@ determines whether case is significant or ignored.  */)
   register EMACS_INT begp1, endp1, begp2, endp2, temp;
   register struct buffer *bp1, *bp2;
   register Lisp_Object trt
-    = (!NILP (BVAR (current_buffer, case_fold_search))
+    = (!NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search))
        ? BVAR (current_buffer, case_canon_table) : Qnil);
   ptrdiff_t chars = 0;
   ptrdiff_t i1, i2, i1_byte, i2_byte;
@@ -4022,7 +4022,7 @@ Case is ignored if `case-fold-search' is non-nil in the current buffer.  */)
 
   if (XFIXNUM (c1) == XFIXNUM (c2))
     return Qt;
-  if (NILP (BVAR (current_buffer, case_fold_search)))
+  if (NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search)))
     return Qnil;
 
   i1 = XFIXNAT (c1);
diff --git a/src/fileio.c b/src/fileio.c
index 741e297d29..4ee3b293a1 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -4919,7 +4919,7 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
       bool using_default_coding = 0;
       bool force_raw_text = 0;
 
-      val = BVAR (current_buffer, buffer_file_coding_system);
+      val = BVAR_OR_DEFAULT (current_buffer, buffer_file_coding_system);
       if (NILP (val)
 	  || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
 	{
@@ -4942,7 +4942,7 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
 	{
 	  /* If we still have not decided a coding system, use the
 	     current buffer's value of buffer-file-coding-system.  */
-	  val = BVAR (current_buffer, buffer_file_coding_system);
+	  val = BVAR_OR_DEFAULT (current_buffer, buffer_file_coding_system);
 	  using_default_coding = 1;
 	}
 
@@ -4974,7 +4974,8 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
 	 format, we use that of `buffer-file-coding-system'.  */
       if (! using_default_coding)
 	{
-	  Lisp_Object dflt = BVAR (&buffer_defaults, buffer_file_coding_system);
+	  Lisp_Object dflt = BVAR_OR_DEFAULT (&buffer_defaults,
+                                             buffer_file_coding_system);
 
 	  if (! NILP (dflt))
 	    val = coding_inherit_eol_type (val, dflt);
@@ -4989,7 +4990,7 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
   val = coding_inherit_eol_type (val, eol_parent);
   setup_coding_system (val, coding);
 
-  if (!STRINGP (start) && EQ (Qt, BVAR (current_buffer, selective_display)))
+  if (!STRINGP (start) && EQ (Qt, BVAR_OR_DEFAULT (current_buffer, selective_display)))
     coding->mode |= CODING_MODE_SELECTIVE_DISPLAY;
   return val;
 }
diff --git a/src/fns.c b/src/fns.c
index 41429c8863..4ac9481325 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5416,7 +5416,8 @@ extract_data_from_object (Lisp_Object spec,
 	    {
 	      bool force_raw_text = false;
 
-	      coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
+	      coding_system = BVAR_OR_DEFAULT (XBUFFER(object),
+                                              buffer_file_coding_system);
 	      if (NILP (coding_system)
 		  || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
 		{
@@ -5437,11 +5438,12 @@ extract_data_from_object (Lisp_Object spec,
 		}
 
 	      if (NILP (coding_system)
-		  && !NILP (BVAR (XBUFFER (object), buffer_file_coding_system)))
+		  && !NILP (BVAR_OR_DEFAULT (XBUFFER(object), buffer_file_coding_system)))
 		{
 		  /* If we still have not decided a coding system, use the
 		     default value of buffer-file-coding-system.  */
-		  coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
+		  coding_system = BVAR_OR_DEFAULT (XBUFFER(object),
+                                                  buffer_file_coding_system);
 		}
 
 	      if (!force_raw_text
diff --git a/src/fringe.c b/src/fringe.c
index 65c9a84ac9..91f06a1291 100644
--- a/src/fringe.c
+++ b/src/fringe.c
@@ -697,7 +697,8 @@ get_logical_cursor_bitmap (struct window *w, Lisp_Object cursor)
 {
   Lisp_Object cmap, bm = Qnil;
 
-  if ((cmap = BVAR (XBUFFER (w->contents), fringe_cursor_alist)), !NILP (cmap))
+  if ((cmap = BVAR_OR_DEFAULT (XBUFFER(w->contents), fringe_cursor_alist)),
+      !NILP (cmap))
     {
       bm = Fassq (cursor, cmap);
       if (CONSP (bm))
@@ -707,9 +708,9 @@ get_logical_cursor_bitmap (struct window *w, Lisp_Object cursor)
 	  return lookup_fringe_bitmap (bm);
 	}
     }
-  if (EQ (cmap, BVAR (&buffer_defaults, fringe_cursor_alist)))
+  if (EQ (cmap, BVAR_OR_DEFAULT (&buffer_defaults, fringe_cursor_alist)))
     return NO_FRINGE_BITMAP;
-  bm = Fassq (cursor, BVAR (&buffer_defaults, fringe_cursor_alist));
+  bm = Fassq (cursor, BVAR_OR_DEFAULT (&buffer_defaults, fringe_cursor_alist));
   if (!CONSP (bm) || ((bm = XCDR (bm)), NILP (bm)))
     return NO_FRINGE_BITMAP;
   return lookup_fringe_bitmap (bm);
@@ -734,7 +735,8 @@ get_logical_fringe_bitmap (struct window *w, Lisp_Object bitmap, int right_p, in
      If partial, lookup partial bitmap in default value if not found here.
      If not partial, or no partial spec is present, use non-partial bitmap.  */
 
-  if ((cmap = BVAR (XBUFFER (w->contents), fringe_indicator_alist)), !NILP (cmap))
+  if ((cmap = BVAR_OR_DEFAULT (XBUFFER(w->contents), fringe_indicator_alist)),
+      !NILP (cmap))
     {
       bm1 = Fassq (bitmap, cmap);
       if (CONSP (bm1))
@@ -768,10 +770,11 @@ get_logical_fringe_bitmap (struct window *w, Lisp_Object bitmap, int right_p, in
 	}
     }
 
-  if (!EQ (cmap, BVAR (&buffer_defaults, fringe_indicator_alist))
-      && !NILP (BVAR (&buffer_defaults, fringe_indicator_alist)))
+  if (!EQ (cmap, BVAR_OR_DEFAULT (&buffer_defaults, fringe_indicator_alist))
+      && !NILP (BVAR_OR_DEFAULT (&buffer_defaults, fringe_indicator_alist)))
     {
-      bm2 = Fassq (bitmap, BVAR (&buffer_defaults, fringe_indicator_alist));
+      bm2 = Fassq (bitmap,
+                   BVAR_OR_DEFAULT (&buffer_defaults, fringe_indicator_alist));
       if (CONSP (bm2))
 	{
 	  if ((bm2 = XCDR (bm2)), !NILP (bm2))
@@ -970,7 +973,7 @@ update_window_fringes (struct window *w, bool keep_current_p)
     return 0;
 
   if (!MINI_WINDOW_P (w)
-      && (ind = BVAR (XBUFFER (w->contents), indicate_buffer_boundaries), !NILP (ind)))
+      && (ind = BVAR_OR_DEFAULT (XBUFFER (w->contents), indicate_buffer_boundaries), !NILP (ind)))
     {
       if (EQ (ind, Qleft) || EQ (ind, Qright))
 	boundary_top = boundary_bot = arrow_top = arrow_bot = ind;
@@ -1031,7 +1034,7 @@ update_window_fringes (struct window *w, bool keep_current_p)
 	}
     }
 
-  empty_pos = BVAR (XBUFFER (w->contents), indicate_empty_lines);
+  empty_pos = BVAR_OR_DEFAULT (XBUFFER (w->contents), indicate_empty_lines);
   if (!NILP (empty_pos) && !EQ (empty_pos, Qright))
     empty_pos = WINDOW_LEFT_FRINGE_WIDTH (w) == 0 ? Qright : Qleft;
 
diff --git a/src/hbfont.c b/src/hbfont.c
index e9f4085b1a..5f8f4d2445 100644
--- a/src/hbfont.c
+++ b/src/hbfont.c
@@ -443,7 +443,7 @@ hbfont_shape (Lisp_Object lgstring, Lisp_Object direction)
       /* If they bind bidi-display-reordering to nil, the DIRECTION
 	 they provide is meaningless, and we should let HarfBuzz guess
 	 the real direction.  */
-      && !NILP (BVAR (current_buffer, bidi_display_reordering)))
+      && !NILP (BVAR_OR_DEFAULT (current_buffer, bidi_display_reordering)))
     {
       hb_direction_t dir = HB_DIRECTION_LTR;
       if (EQ (direction, QL2R))
diff --git a/src/indent.c b/src/indent.c
index 6246b544fb..d30b5b3ad9 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -60,7 +60,7 @@ buffer_display_table (void)
 {
   Lisp_Object thisbuf;
 
-  thisbuf = BVAR (current_buffer, display_table);
+  thisbuf = BVAR_OR_DEFAULT (current_buffer, display_table);
   if (DISP_TABLE_P (thisbuf))
     return XCHAR_TABLE (thisbuf);
   if (DISP_TABLE_P (Vstandard_display_table))
@@ -153,13 +153,13 @@ width_run_cache_on_off (void)
       indirect_p = true;
     }
 
-  if (NILP (BVAR (current_buffer, cache_long_scans))
+  if (NILP (BVAR_OR_DEFAULT (current_buffer, cache_long_scans))
       /* And, for the moment, this feature doesn't work on multibyte
          characters.  */
       || !NILP (BVAR (current_buffer, enable_multibyte_characters)))
     {
       if (!indirect_p
-	  || NILP (BVAR (cache_buffer, cache_long_scans))
+	  || NILP (BVAR_OR_DEFAULT (cache_buffer, cache_long_scans))
 	  || !NILP (BVAR (cache_buffer, enable_multibyte_characters)))
 	{
 	  /* It should be off.  */
@@ -175,7 +175,7 @@ width_run_cache_on_off (void)
   else
     {
       if (!indirect_p
-	  || (!NILP (BVAR (cache_buffer, cache_long_scans))
+	  || (!NILP (BVAR_OR_DEFAULT (cache_buffer, cache_long_scans))
 	      && NILP (BVAR (cache_buffer, enable_multibyte_characters))))
 	{
 	  /* It should be on.  */
@@ -334,7 +334,7 @@ current_column (void)
   ptrdiff_t post_tab;
   int c;
   int tab_width = SANE_TAB_WIDTH (current_buffer);
-  bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
+  bool ctl_arrow = !NILP (BVAR_OR_DEFAULT (current_buffer, ctl_arrow));
   struct Lisp_Char_Table *dp = buffer_display_table ();
 
   if (PT == last_known_column_point
@@ -416,7 +416,7 @@ current_column (void)
 	    col++;
 	  else if (c == '\n'
 		   || (c == '\r'
-		       && EQ (BVAR (current_buffer, selective_display), Qt)))
+		       && EQ (BVAR_OR_DEFAULT (current_buffer, selective_display), Qt)))
 	    {
 	      ptr++;
 	      goto start_of_line_found;
@@ -531,7 +531,7 @@ scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol,
 		 ptrdiff_t *prevpos, ptrdiff_t *prevbpos, ptrdiff_t *prevcol)
 {
   int tab_width = SANE_TAB_WIDTH (current_buffer);
-  bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
+  bool ctl_arrow = !NILP (BVAR_OR_DEFAULT (current_buffer, ctl_arrow));
   struct Lisp_Char_Table *dp = buffer_display_table ();
   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
   struct composition_it cmp_it;
@@ -652,7 +652,7 @@ scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol,
 
 	      if (c == '\n')
 		goto endloop;
-	      if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
+	      if (c == '\r' && EQ (BVAR_OR_DEFAULT (current_buffer, selective_display), Qt))
 		goto endloop;
 	      if (c == '\t')
 		{
@@ -670,7 +670,7 @@ scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol,
 
 	  if (c == '\n')
 	    goto endloop;
-	  if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
+	  if (c == '\r' && EQ (BVAR_OR_DEFAULT (current_buffer, selective_display), Qt))
 	    goto endloop;
 	  if (c == '\t')
 	    {
@@ -1131,12 +1131,12 @@ compute_motion (ptrdiff_t from, ptrdiff_t frombyte, EMACS_INT fromvpos,
   ptrdiff_t pos_byte;
   int c = 0;
   int tab_width = SANE_TAB_WIDTH (current_buffer);
-  bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
+  bool ctl_arrow = !NILP (BVAR_OR_DEFAULT (current_buffer, ctl_arrow));
   struct Lisp_Char_Table *dp = window_display_table (win);
   EMACS_INT selective
-    = (FIXNUMP (BVAR (current_buffer, selective_display))
-       ? XFIXNUM (BVAR (current_buffer, selective_display))
-       : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
+    = (FIXNUMP (BVAR_OR_DEFAULT (current_buffer, selective_display))
+       ? XFIXNUM (BVAR_OR_DEFAULT (current_buffer, selective_display))
+       : !NILP (BVAR_OR_DEFAULT (current_buffer, selective_display)) ? -1 : 0);
   ptrdiff_t selective_rlen
     = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
        ? ASIZE (DISP_INVIS_VECTOR (dp)) : 0);
@@ -1352,7 +1352,7 @@ compute_motion (ptrdiff_t from, ptrdiff_t frombyte, EMACS_INT fromvpos,
 	    }
 
 	  if (hscroll || truncate
-	      || !NILP (BVAR (current_buffer, truncate_lines)))
+	      || !NILP (BVAR_OR_DEFAULT (current_buffer, truncate_lines)))
 	    {
 	      /* Truncating: skip to newline, unless we are already past
                  TO (we need to go back below).  */
@@ -1837,10 +1837,10 @@ vmotion (ptrdiff_t from, ptrdiff_t from_byte,
   register ptrdiff_t first;
   ptrdiff_t lmargin = hscroll > 0 ? 1 - hscroll : 0;
   ptrdiff_t selective
-    = (FIXNUMP (BVAR (current_buffer, selective_display))
-       ? clip_to_bounds (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
+    = (FIXNUMP (BVAR_OR_DEFAULT (current_buffer, selective_display))
+       ? clip_to_bounds (-1, XFIXNUM (BVAR_OR_DEFAULT (current_buffer, selective_display)),
 			 PTRDIFF_MAX)
-       : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
+       : !NILP (BVAR_OR_DEFAULT (current_buffer, selective_display)) ? -1 : 0);
   Lisp_Object window;
   bool did_motion;
   /* This is the object we use for fetching character properties.  */
diff --git a/src/msdos.c b/src/msdos.c
index 5da01c9e7c..e3426d9403 100644
--- a/src/msdos.c
+++ b/src/msdos.c
@@ -1321,12 +1321,12 @@ IT_frame_up_to_date (struct frame *f)
     {
       struct buffer *b = XBUFFER (sw->contents);
 
-      if (EQ (BVAR (b,cursor_type), Qt))
+      if (EQ (BVAR_OR_DEFAULT (b, cursor_type), Qt))
 	new_cursor = frame_desired_cursor;
-      else if (NILP (BVAR (b, cursor_type))) /* nil means no cursor */
+      else if (NILP (BVAR_OR_DEFAULT (b, cursor_type))) /* nil means no cursor */
 	new_cursor = Fcons (Qbar, make_fixnum (0));
       else
-	new_cursor = BVAR (b, cursor_type);
+	new_cursor = BVAR_OR_DEFAULT (b, cursor_type);
     }
 
   IT_set_cursor_type (f, new_cursor);
diff --git a/src/search.c b/src/search.c
index c757bf3d1f..f15059635c 100644
--- a/src/search.c
+++ b/src/search.c
@@ -125,7 +125,8 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 
   /* If the compiled pattern hard codes some of the contents of the
      syntax-table, it can only be reused with *this* syntax table.  */
-  cp->syntax_table = cp->buf.used_syntax ? BVAR (current_buffer, syntax_table) : Qt;
+  cp->syntax_table = cp->buf.used_syntax ? BVAR_OR_DEFAULT (current_buffer,
+                                                           syntax_table) : Qt;
 
   if (val)
     xsignal1 (Qinvalid_regexp, build_string (val));
@@ -219,7 +220,7 @@ compile_pattern (Lisp_Object pattern, struct re_registers *regp,
 	  && EQ (cp->buf.translate, translate)
 	  && cp->posix == posix
 	  && (EQ (cp->syntax_table, Qt)
-	      || EQ (cp->syntax_table, BVAR (current_buffer, syntax_table)))
+	      || EQ (cp->syntax_table, BVAR_OR_DEFAULT (current_buffer, syntax_table)))
 	  && !NILP (Fequal (cp->f_whitespace_regexp, Vsearch_spaces_regexp))
 	  && cp->buf.charset_unibyte == charset_unibyte)
 	break;
@@ -282,7 +283,7 @@ looking_at_1 (Lisp_Object string, bool posix)
   struct regexp_cache *cache_entry = compile_pattern (
     string,
     preserve_match_data ? &search_regs : NULL,
-    (!NILP (BVAR (current_buffer, case_fold_search))
+    (!NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search))
      ? BVAR (current_buffer, case_canon_table) : Qnil),
     posix,
     !NILP (BVAR (current_buffer, enable_multibyte_characters)));
@@ -401,7 +402,7 @@ string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
   bufp = &compile_pattern (regexp,
                            (NILP (Vinhibit_changing_match_data)
                             ? &search_regs : NULL),
-                           (!NILP (BVAR (current_buffer, case_fold_search))
+                           (!NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search))
                             ? BVAR (current_buffer, case_canon_table) : Qnil),
                            posix,
                            STRING_MULTIBYTE (string))->buf;
@@ -592,10 +593,10 @@ newline_cache_on_off (struct buffer *buf)
      This is because doing so will just make the cache pure overhead,
      since if we turn it on via indirect buffer, it will be
      immediately turned off by its base buffer.  */
-  if (NILP (BVAR (buf, cache_long_scans)))
+  if (NILP (BVAR_OR_DEFAULT (buf, cache_long_scans)))
     {
       if (!indirect_p
-	  || NILP (BVAR (base_buf, cache_long_scans)))
+	  || NILP (BVAR_OR_DEFAULT (base_buf, cache_long_scans)))
 	{
 	  /* It should be off.  */
 	  if (base_buf->newline_cache)
@@ -609,7 +610,7 @@ newline_cache_on_off (struct buffer *buf)
   else
     {
       if (!indirect_p
-	  || !NILP (BVAR (base_buf, cache_long_scans)))
+	  || !NILP (BVAR_OR_DEFAULT (base_buf, cache_long_scans)))
 	{
 	  /* It should be on.  */
 	  if (base_buf->newline_cache == 0)
@@ -1048,10 +1049,10 @@ search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror,
 			 BVAR (current_buffer, case_eqv_table));
 
   np = search_buffer (string, PT, PT_BYTE, lim, lim_byte, n, RE,
-		      (!NILP (BVAR (current_buffer, case_fold_search))
+		      (!NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search))
 		       ? BVAR (current_buffer, case_canon_table)
 		       : Qnil),
-		      (!NILP (BVAR (current_buffer, case_fold_search))
+		      (!NILP (BVAR_OR_DEFAULT (current_buffer, case_fold_search))
 		       ? BVAR (current_buffer, case_eqv_table)
 		       : Qnil),
 		      posix);
@@ -3275,7 +3276,7 @@ the buffer.  If the buffer doesn't have a cache, the value is nil.  */)
     buf = buf->base_buffer;
 
   /* If the buffer doesn't have a newline cache, return nil.  */
-  if (NILP (BVAR (buf, cache_long_scans))
+  if (NILP (BVAR_OR_DEFAULT (buf, cache_long_scans))
       || buf->newline_cache == NULL)
     return Qnil;
 
diff --git a/src/syntax.c b/src/syntax.c
index 9fbf88535f..6ea92beb7c 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -416,7 +416,8 @@ update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
       else
 	{
 	  gl_state.use_global = 0;
-	  gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
+	  gl_state.current_syntax_table = BVAR_OR_DEFAULT (current_buffer,
+                                                          syntax_table);
 	}
     }
 
@@ -998,7 +999,7 @@ DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
 This is the one specified by the current buffer.  */)
   (void)
 {
-  return BVAR (current_buffer, syntax_table);
+  return BVAR_OR_DEFAULT (current_buffer, syntax_table);
 }
 
 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
@@ -1254,7 +1255,7 @@ usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE)  */)
     CHECK_CHARACTER (c);
 
   if (NILP (syntax_table))
-    syntax_table = BVAR (current_buffer, syntax_table);
+    syntax_table = BVAR_OR_DEFAULT (current_buffer, syntax_table);
   else
     check_syntax_table (syntax_table);
 
diff --git a/src/syntax.h b/src/syntax.h
index 66ee139a96..c89797ea13 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -103,7 +103,7 @@ syntax_property_entry (int c, bool via_property)
     return (gl_state.use_global
 	    ? gl_state.global_code
 	    : CHAR_TABLE_REF (gl_state.current_syntax_table, c));
-  return CHAR_TABLE_REF (BVAR (current_buffer, syntax_table), c);
+  return CHAR_TABLE_REF (BVAR_OR_DEFAULT (current_buffer, syntax_table), c);
 }
 INLINE Lisp_Object
 SYNTAX_ENTRY (int c)
@@ -212,7 +212,8 @@ SETUP_BUFFER_SYNTAX_TABLE (void)
 {
   gl_state.use_global = false;
   gl_state.e_property_truncated = false;
-  gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
+  gl_state.current_syntax_table = BVAR_OR_DEFAULT (current_buffer,
+                                                  syntax_table);
 }
 
 extern ptrdiff_t scan_words (ptrdiff_t, EMACS_INT);
diff --git a/src/window.c b/src/window.c
index 0a14eca58f..b85f758679 100644
--- a/src/window.c
+++ b/src/window.c
@@ -2315,8 +2315,8 @@ window_display_table (struct window *w)
     {
       struct buffer *b = XBUFFER (w->contents);
 
-      if (DISP_TABLE_P (BVAR (b, display_table)))
-	dp = XCHAR_TABLE (BVAR (b, display_table));
+      if (DISP_TABLE_P (BVAR_OR_DEFAULT (b, display_table)))
+	dp = XCHAR_TABLE (BVAR_OR_DEFAULT (b, display_table));
       else if (DISP_TABLE_P (Vstandard_display_table))
 	dp = XCHAR_TABLE (Vstandard_display_table);
     }
@@ -4044,17 +4044,18 @@ set_window_buffer (Lisp_Object window, Lisp_Object buffer,
       /* Set fringes and scroll bars from buffer unless they have been
 	 declared as persistent.  */
       if (!w->fringes_persistent)
-	set_window_fringes (w, BVAR (b, left_fringe_width),
-			    BVAR (b, right_fringe_width),
-			    BVAR (b, fringes_outside_margins), Qnil);
+	set_window_fringes (w, BVAR_OR_DEFAULT (b, left_fringe_width),
+			    BVAR_OR_DEFAULT (b, right_fringe_width),
+			    BVAR_OR_DEFAULT (b, fringes_outside_margins), Qnil);
       if (!w->scroll_bars_persistent)
-	set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
-				BVAR (b, vertical_scroll_bar_type),
-				BVAR (b, scroll_bar_height),
-				BVAR (b, horizontal_scroll_bar_type), Qnil);
+	set_window_scroll_bars (w, BVAR_OR_DEFAULT (b, scroll_bar_width),
+				BVAR_OR_DEFAULT (b, vertical_scroll_bar_type),
+				BVAR_OR_DEFAULT (b, scroll_bar_height),
+				BVAR_OR_DEFAULT (b, horizontal_scroll_bar_type),
+				Qnil);
       /* Set left and right marginal area width from buffer.  */
-      set_window_margins (w, BVAR (b, left_margin_cols),
-			  BVAR (b, right_margin_cols));
+      set_window_margins (w, BVAR_OR_DEFAULT (b, left_margin_cols),
+			  BVAR_OR_DEFAULT (b, right_margin_cols));
       apply_window_adjustment (w);
     }
 
@@ -5372,7 +5373,7 @@ window_wants_mode_line (struct window *w)
 	  && !WINDOW_PSEUDO_P (w)
 	  && !EQ (window_mode_line_format, Qnone)
 	  && (!NILP (window_mode_line_format)
-	      || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), mode_line_format)))
+	      || !NILP (BVAR_OR_DEFAULT (XBUFFER(WINDOW_BUFFER(w)), mode_line_format)))
 	  && WINDOW_PIXEL_HEIGHT (w) > WINDOW_FRAME_LINE_HEIGHT (w));
 }
 
@@ -5401,7 +5402,7 @@ window_wants_header_line (struct window *w)
 	  && !WINDOW_PSEUDO_P (w)
 	  && !EQ (window_header_line_format, Qnone)
 	  && (!NILP (window_header_line_format)
-	      || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), header_line_format)))
+	      || !NILP (BVAR_OR_DEFAULT (XBUFFER(WINDOW_BUFFER(w)), header_line_format)))
 	  && (WINDOW_PIXEL_HEIGHT (w)
 	      > (window_wants_mode_line (w)
 		 ? 2 * WINDOW_FRAME_LINE_HEIGHT (w)
@@ -5435,7 +5436,7 @@ window_wants_tab_line (struct window *w)
 	  && !WINDOW_PSEUDO_P (w)
 	  && !EQ (window_tab_line_format, Qnone)
 	  && (!NILP (window_tab_line_format)
-	      || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), tab_line_format)))
+	      || !NILP (BVAR_OR_DEFAULT (XBUFFER(WINDOW_BUFFER(w)), tab_line_format)))
 	  && (WINDOW_PIXEL_HEIGHT (w)
 	      > (((window_wants_mode_line (w) ? 1 : 0)
 		  + (window_wants_header_line (w) ? 1 : 0)
diff --git a/src/xdisp.c b/src/xdisp.c
index 23b4ba5c39..07f3939cbe 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -595,7 +595,7 @@ fill_column_indicator_column (struct it *it, int char_width)
       && CHARACTERP (Vdisplay_fill_column_indicator_character))
     {
       Lisp_Object col = (EQ (Vdisplay_fill_column_indicator_column, Qt)
-			 ? BVAR (current_buffer, fill_column)
+			 ? BVAR_OR_DEFAULT (current_buffer, fill_column)
 			 : Vdisplay_fill_column_indicator_column);
 
       /* The stretch width needs to consider the latter
@@ -1543,10 +1543,10 @@ default_line_pixel_height (struct window *w)
   if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
     {
       struct buffer *b = XBUFFER (w->contents);
-      Lisp_Object val = BVAR (b, extra_line_spacing);
+      Lisp_Object val = BVAR_OR_DEFAULT (b, extra_line_spacing);
 
       if (NILP (val))
-	val = BVAR (&buffer_defaults, extra_line_spacing);
+	val = BVAR_OR_DEFAULT (&buffer_defaults, extra_line_spacing);
       if (!NILP (val))
 	{
 	  if (RANGED_FIXNUMP (0, val, INT_MAX))
@@ -1684,7 +1684,7 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
       w->mode_line_height
 	= display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
 			     NILP (window_mode_line_format)
-			     ? BVAR (current_buffer, mode_line_format)
+			     ? BVAR_OR_DEFAULT (current_buffer, mode_line_format)
 			     : window_mode_line_format);
     }
 
@@ -1696,7 +1696,7 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
       w->tab_line_height
 	= display_mode_line (w, TAB_LINE_FACE_ID,
 			     NILP (window_tab_line_format)
-			     ? BVAR (current_buffer, tab_line_format)
+			     ? BVAR_OR_DEFAULT (current_buffer, tab_line_format)
 			     : window_tab_line_format);
     }
 
@@ -1708,7 +1708,7 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
       w->header_line_height
 	= display_mode_line (w, HEADER_LINE_FACE_ID,
 			     NILP (window_header_line_format)
-			     ? BVAR (current_buffer, header_line_format)
+			     ? BVAR_OR_DEFAULT (current_buffer, header_line_format)
 			     : window_header_line_format);
     }
 
@@ -3205,10 +3205,10 @@ init_iterator (struct it *it, struct window *w,
   if (base_face_id == DEFAULT_FACE_ID
       && FRAME_WINDOW_P (it->f))
     {
-      if (FIXNATP (BVAR (current_buffer, extra_line_spacing)))
-	it->extra_line_spacing = XFIXNAT (BVAR (current_buffer, extra_line_spacing));
-      else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
-	it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
+      if (FIXNATP (BVAR_OR_DEFAULT (current_buffer, extra_line_spacing)))
+	it->extra_line_spacing = XFIXNAT (BVAR_OR_DEFAULT (current_buffer, extra_line_spacing));
+      else if (FLOATP (BVAR_OR_DEFAULT (current_buffer, extra_line_spacing)))
+	it->extra_line_spacing = (XFLOAT_DATA (BVAR_OR_DEFAULT (current_buffer, extra_line_spacing))
 				  * FRAME_LINE_HEIGHT (it->f));
       else if (it->f->extra_line_spacing > 0)
 	it->extra_line_spacing = it->f->extra_line_spacing;
@@ -3226,19 +3226,19 @@ init_iterator (struct it *it, struct window *w,
   it->override_ascent = -1;
 
   /* Are control characters displayed as `^C'?  */
-  it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
+  it->ctl_arrow_p = !NILP (BVAR_OR_DEFAULT (current_buffer, ctl_arrow));
 
   /* -1 means everything between a CR and the following line end
      is invisible.  >0 means lines indented more than this value are
      invisible.  */
-  it->selective = (FIXNUMP (BVAR (current_buffer, selective_display))
+  it->selective = (FIXNUMP (BVAR_OR_DEFAULT (current_buffer, selective_display))
 		   ? (clip_to_bounds
-		      (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
+		      (-1, XFIXNUM (BVAR_OR_DEFAULT (current_buffer, selective_display)),
 		       PTRDIFF_MAX))
-		   : (!NILP (BVAR (current_buffer, selective_display))
+		   : (!NILP (BVAR_OR_DEFAULT (current_buffer, selective_display))
 		      ? -1 : 0));
   it->selective_display_ellipsis_p
-    = !NILP (BVAR (current_buffer, selective_display_ellipses));
+    = !NILP (BVAR_OR_DEFAULT (current_buffer, selective_display_ellipses));
 
   /* Display table to use.  */
   it->dp = window_display_table (w);
@@ -3270,8 +3270,8 @@ init_iterator (struct it *it, struct window *w,
 	      /* PXW: Shall we do something about this?  */
 	      && (XFIXNUM (Vtruncate_partial_width_windows)
 		  <= WINDOW_TOTAL_COLS (it->w))))
-      && NILP (BVAR (current_buffer, truncate_lines)))
-    it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
+      && NILP (BVAR_OR_DEFAULT (current_buffer, truncate_lines)))
+    it->line_wrap = NILP (BVAR_OR_DEFAULT (current_buffer, word_wrap))
       ? WINDOW_WRAP : WORD_WRAP;
 
   /* Get dimensions of truncation and continuation glyphs.  These are
@@ -3416,7 +3416,7 @@ init_iterator (struct it *it, struct window *w,
 	 available.  */
       it->bidi_p =
 	!redisplay__inhibit_bidi
-	&& !NILP (BVAR (current_buffer, bidi_display_reordering))
+	&& !NILP (BVAR_OR_DEFAULT (current_buffer, bidi_display_reordering))
 	&& it->multibyte_p;
 
       /* If we are to reorder bidirectional text, init the bidi
@@ -3438,10 +3438,10 @@ init_iterator (struct it *it, struct window *w,
 	    }
 	  /* Note the paragraph direction that this buffer wants to
 	     use.  */
-	  if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
+	  if (EQ (BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_direction),
 		  Qleft_to_right))
 	    it->paragraph_embedding = L2R;
-	  else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
+	  else if (EQ (BVAR_OR_DEFAULT (current_buffer, bidi_paragraph_direction),
 		       Qright_to_left))
 	    it->paragraph_embedding = R2L;
 	  else
@@ -7208,7 +7208,7 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
      not yet available.  */
   it->bidi_p =
     !redisplay__inhibit_bidi
-    && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
+    && !NILP (BVAR_OR_DEFAULT (&buffer_defaults, bidi_display_reordering));
 
   if (s == NULL)
     {
@@ -12140,7 +12140,7 @@ set_message_1 (void *a1, Lisp_Object string)
     Fset_buffer_multibyte (Qt);
 
   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
-  if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
+  if (!NILP (BVAR_OR_DEFAULT (current_buffer, bidi_display_reordering)))
     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 
   /* Insert new message at BEG.  */
@@ -15187,8 +15187,8 @@ text_outside_line_unchanged_p (struct window *w,
       /* If selective display, can't optimize if changes start at the
 	 beginning of the line.  */
       if (unchanged_p
-	  && FIXNUMP (BVAR (current_buffer, selective_display))
-	  && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
+	  && FIXNUMP (BVAR_OR_DEFAULT (current_buffer, selective_display))
+	  && XFIXNUM (BVAR_OR_DEFAULT (current_buffer, selective_display)) > 0
 	  && (BEG_UNCHANGED < start || GPT <= start))
 	unchanged_p = false;
 
@@ -15216,8 +15216,8 @@ text_outside_line_unchanged_p (struct window *w,
 	 require redisplaying the whole paragraph.  It might be worthwhile
 	 to find the paragraph limits and widen the range of redisplayed
 	 lines to that, but for now just give up this optimization.  */
-      if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
-	  && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
+      if (!NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering))
+	  && NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_paragraph_direction)))
 	unchanged_p = false;
     }
 
@@ -17395,8 +17395,8 @@ try_scrolling (Lisp_Object window, bool just_this_one_p,
       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
       scroll_max = scroll_lines * frame_line_height;
     }
-  else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
-	   || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
+  else if (NUMBERP (BVAR_OR_DEFAULT (current_buffer, scroll_down_aggressively))
+	   || NUMBERP (BVAR_OR_DEFAULT (current_buffer, scroll_up_aggressively)))
     /* We're trying to scroll because of aggressive scrolling but no
        scroll_step is set.  Choose an arbitrary one.  */
     scroll_max = 10 * frame_line_height;
@@ -17494,7 +17494,7 @@ try_scrolling (Lisp_Object window, bool just_this_one_p,
 	amount_to_scroll = scroll_max;
       else
 	{
-	  aggressive = BVAR (current_buffer, scroll_up_aggressively);
+	  aggressive = BVAR_OR_DEFAULT (current_buffer, scroll_up_aggressively);
 	  height = WINDOW_BOX_TEXT_HEIGHT (w);
 	  if (NUMBERP (aggressive))
 	    {
@@ -17610,7 +17610,8 @@ try_scrolling (Lisp_Object window, bool just_this_one_p,
 	    amount_to_scroll = scroll_max;
 	  else
 	    {
-	      aggressive = BVAR (current_buffer, scroll_down_aggressively);
+	      aggressive = BVAR_OR_DEFAULT (current_buffer,
+                                           scroll_down_aggressively);
 	      height = WINDOW_BOX_TEXT_HEIGHT (w);
 	      if (NUMBERP (aggressive))
 		{
@@ -17997,7 +17998,7 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp,
 	      must_scroll = true;
 	    }
 	  else if (rc != CURSOR_MOVEMENT_SUCCESS
-		   && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
+		   && !NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering)))
 	    {
 	      struct glyph_row *row1;
 
@@ -18061,7 +18062,7 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp,
 	  else if (scroll_p)
 	    rc = CURSOR_MOVEMENT_MUST_SCROLL;
 	  else if (rc != CURSOR_MOVEMENT_SUCCESS
-		   && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
+		   && !NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering)))
 	    {
 	      /* With bidi-reordered rows, there could be more than
 		 one candidate row whose start and end positions
@@ -18906,8 +18907,8 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
        || 0 < emacs_scroll_step
        || temp_scroll_step
-       || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
-       || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
+       || NUMBERP (BVAR_OR_DEFAULT (current_buffer, scroll_up_aggressively))
+       || NUMBERP (BVAR_OR_DEFAULT (current_buffer, scroll_down_aggressively)))
       && CHARPOS (startp) >= BEGV
       && CHARPOS (startp) <= ZV)
     {
@@ -18981,8 +18982,8 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
       scrolling_up = PT > margin_pos;
       aggressive =
 	scrolling_up
-	? BVAR (current_buffer, scroll_up_aggressively)
-	: BVAR (current_buffer, scroll_down_aggressively);
+	? BVAR_OR_DEFAULT (current_buffer, scroll_up_aggressively)
+	: BVAR_OR_DEFAULT (current_buffer, scroll_down_aggressively);
 
       if (!MINI_WINDOW_P (w)
 	  && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
@@ -19917,7 +19918,7 @@ try_window_reusing_current_matrix (struct window *w)
 		 bidi-reordered glyph rows.  Let set_cursor_from_row
 		 figure out where to put the cursor, and if it fails,
 		 give up.  */
-	      if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
+	      if (!NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering)))
 		{
 		  if (!set_cursor_from_row (w, row, w->current_matrix,
 					    0, 0, 0, 0))
@@ -20236,7 +20237,7 @@ row_containing_pos (struct window *w, ptrdiff_t charpos,
 	{
 	  struct glyph *g;
 
-	  if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
+	  if (NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering))
 	      || (!best_row && !row->continued_p))
 	    return row;
 	  /* In bidi-reordered rows, there could be several rows whose
@@ -20406,7 +20407,7 @@ try_window_id (struct window *w)
      wrapped line can change the wrap position, altering the line
      above it.  It might be worthwhile to handle this more
      intelligently, but for now just redisplay from scratch.  */
-  if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
+  if (!NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), word_wrap)))
     GIVE_UP (21);
 
   /* Under bidi reordering, adding or deleting a character in the
@@ -20417,13 +20418,13 @@ try_window_id (struct window *w)
      to find the paragraph limits and widen the range of redisplayed
      lines to that, but for now just give up this optimization and
      redisplay from scratch.  */
-  if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
-      && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
+  if (!NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_display_reordering))
+      && NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), bidi_paragraph_direction)))
     GIVE_UP (22);
 
   /* Give up if the buffer has line-spacing set, as Lisp-level changes
      to that variable require thorough redisplay.  */
-  if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
+  if (!NILP (BVAR_OR_DEFAULT (XBUFFER(w->contents), extra_line_spacing)))
     GIVE_UP (23);
 
   /* Give up if display-line-numbers is in relative mode, or when the
@@ -23532,7 +23533,7 @@ display_line (struct it *it, int cursor_vpos)
 	      if (!row_has_glyphs)
 		row->displays_text_p = false;
 
-	      if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
+	      if (!NILP (BVAR_OR_DEFAULT (XBUFFER (it->w->contents), indicate_empty_lines))
 		  && (!MINI_WINDOW_P (it->w)))
 		row->indicate_empty_line_p = true;
 	    }
@@ -24306,14 +24307,14 @@ See also `bidi-paragraph-direction'.  */)
       buf = XBUFFER (buffer);
     }
 
-  if (NILP (BVAR (buf, bidi_display_reordering))
+  if (NILP (BVAR_OR_DEFAULT (buf, bidi_display_reordering))
       || NILP (BVAR (buf, enable_multibyte_characters))
       /* When we are loading loadup.el, the character property tables
 	 needed for bidi iteration are not yet available.  */
       || redisplay__inhibit_bidi)
     return Qleft_to_right;
-  else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
-    return BVAR (buf, bidi_paragraph_direction);
+  else if (!NILP (BVAR_OR_DEFAULT (buf, bidi_paragraph_direction)))
+    return BVAR_OR_DEFAULT (buf, bidi_paragraph_direction);
   else
     {
       /* Determine the direction from buffer text.  We could try to
@@ -24457,7 +24458,7 @@ the `bidi-class' property of a character.  */)
     {
       /* Nothing this fancy can happen in unibyte buffers, or in a
 	 buffer that disabled reordering, or if FROM is at EOB.  */
-      if (NILP (BVAR (buf, bidi_display_reordering))
+      if (NILP (BVAR_OR_DEFAULT (buf, bidi_display_reordering))
 	  || NILP (BVAR (buf, enable_multibyte_characters))
 	  /* When we are loading loadup.el, the character property
 	     tables needed for bidi iteration are not yet
@@ -25412,7 +25413,7 @@ display_mode_lines (struct window *w)
       /* Select mode line face based on the real selected window.  */
       display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
 			 NILP (window_mode_line_format)
-			 ? BVAR (current_buffer, mode_line_format)
+			 ? BVAR_OR_DEFAULT (current_buffer, mode_line_format)
 			 : window_mode_line_format);
       ++n;
     }
@@ -25424,7 +25425,7 @@ display_mode_lines (struct window *w)
 
       display_mode_line (w, TAB_LINE_FACE_ID,
 			 NILP (window_tab_line_format)
-			 ? BVAR (current_buffer, tab_line_format)
+			 ? BVAR_OR_DEFAULT (current_buffer, tab_line_format)
 			 : window_tab_line_format);
       ++n;
     }
@@ -25436,7 +25437,7 @@ display_mode_lines (struct window *w)
 
       display_mode_line (w, HEADER_LINE_FACE_ID,
 			 NILP (window_header_line_format)
-			 ? BVAR (current_buffer, header_line_format)
+			 ? BVAR_OR_DEFAULT (current_buffer, header_line_format)
 			 : window_header_line_format);
       ++n;
     }
@@ -26971,7 +26972,7 @@ decode_mode_spec (struct window *w, register int c, int field_width,
 					 (FRAME_TERMINAL_CODING (f)->id),
 					 p, false);
 	  }
-	p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
+	p = decode_mode_spec_coding (BVAR_OR_DEFAULT (b, buffer_file_coding_system),
 				     p, eol_flag);
 
 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
@@ -27035,8 +27036,8 @@ display_count_lines (ptrdiff_t start_byte,
   /* If we are not in selective display mode,
      check only for newlines.  */
   bool selective_display
-    = (!NILP (BVAR (current_buffer, selective_display))
-       && !FIXNUMP (BVAR (current_buffer, selective_display)));
+    = (!NILP (BVAR_OR_DEFAULT (current_buffer, selective_display))
+       && !FIXNUMP (BVAR_OR_DEFAULT (current_buffer, selective_display)));
 
   if (count > 0)
     {
@@ -31350,13 +31351,14 @@ get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
     {
       if (w == XWINDOW (echo_area_window))
 	{
-	  if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
+	  if (EQ (BVAR_OR_DEFAULT (b, cursor_type), Qt) || NILP (BVAR_OR_DEFAULT (b, cursor_type)))
 	    {
 	      *width = FRAME_CURSOR_WIDTH (f);
 	      return FRAME_DESIRED_CURSOR (f);
 	    }
 	  else
-	    return get_specified_cursor_type (BVAR (b, cursor_type), width);
+	    return get_specified_cursor_type (BVAR_OR_DEFAULT (b, cursor_type),
+                                              width);
 	}
 
       *active_cursor = false;
@@ -31378,23 +31380,24 @@ get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
     }
 
   /* Never display a cursor in a window in which cursor-type is nil.  */
-  if (NILP (BVAR (b, cursor_type)))
+  if (NILP (BVAR_OR_DEFAULT (b, cursor_type)))
     return NO_CURSOR;
 
   /* Get the normal cursor type for this window.  */
-  if (EQ (BVAR (b, cursor_type), Qt))
+  if (EQ (BVAR_OR_DEFAULT (b, cursor_type), Qt))
     {
       cursor_type = FRAME_DESIRED_CURSOR (f);
       *width = FRAME_CURSOR_WIDTH (f);
     }
   else
-    cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
+    cursor_type = get_specified_cursor_type (BVAR_OR_DEFAULT (b, cursor_type),
+                                             width);
 
   /* Use cursor-in-non-selected-windows instead
      for non-selected window or frame.  */
   if (non_selected)
     {
-      alt_cursor = BVAR (b, cursor_in_non_selected_windows);
+      alt_cursor = BVAR_OR_DEFAULT (b, cursor_in_non_selected_windows);
       if (!EQ (Qt, alt_cursor))
 	return get_specified_cursor_type (alt_cursor, width);
       /* t means modify the normal cursor type.  */
@@ -31428,7 +31431,7 @@ get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 		     should cover most of the "tiny" icons people may
 		     use.  */
 		  if (!img->mask
-		      || (CONSP (BVAR (b, cursor_type))
+		      || (CONSP (BVAR_OR_DEFAULT (b, cursor_type))
 			  && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 			  && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 		    cursor_type = HOLLOW_BOX_CURSOR;
@@ -31448,7 +31451,7 @@ get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
   /* Cursor is blinked off, so determine how to "toggle" it.  */
 
   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
-  if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
+  if ((alt_cursor = Fassoc (BVAR_OR_DEFAULT (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
     return get_specified_cursor_type (XCDR (alt_cursor), width);
 
   /* Then see if frame has specified a specific blink off cursor type.  */
@@ -33853,11 +33856,10 @@ note_mouse_highlight (struct frame *f, int x, int y)
 		     necessarily display the character whose position
 		     is the smallest.  */
 		  Lisp_Object lim1
-		    = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
+		    = NILP (BVAR_OR_DEFAULT (XBUFFER(buffer), bidi_display_reordering))
 		    ? Fmarker_position (w->start)
 		    : Qnil;
-		  Lisp_Object lim2
-		    = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
+		  Lisp_Object lim2 = NILP (BVAR_OR_DEFAULT (XBUFFER(buffer), bidi_display_reordering))
 		    ? make_fixnum (BUF_Z (XBUFFER (buffer))
 				   - w->window_end_pos)
 		    : Qnil;
-- 
2.31.1