Subject: [PATCH v4 06/14] Rearrange set_internal for buffer forwarded symbols

From: Spencer Baugh <[email-masked]>
Date: Fri, 7 May 2021 22:08:57 -0400

Previously, when setting buffer-local values for DEFVAR_PER_BUFFER
variables, the call to SET_PER_BUFFER_VALUE_P was far from the call to
set_per_buffer_value, even though they're conceptually tied together.

Now, the two calls are in the same place in store_symval_forwarding,
and we can delete the old call to SET_PER_BUFFER_VALUE_P in
set_internal.

Since we did this, we need to also avoid calling
store_symval_forwarding in set_internal when setting the default value
for a DEFVAR_PER_BUFFER variable.

This improves clarity quite a bit; it also makes it easier to later
merge set_per_buffer_value and SET_PER_BUFFER_VALUE_P together.

* src/data.c (store_symval_forwarding): Call SET_PER_BUFFER_VALUE_P
directly for buffer-forwarded symbols.
(set_internal): Don't call SET_PER_BUFFER_VALUE_P for buffer-forwarded
symbols.  Also don't call store_symval_forwarding when we're setting
the default value for a buffer-forwarded symbol.
---
 src/data.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/data.c b/src/data.c
index ec9fc57048..9d8b722a41 100644
--- a/src/data.c
+++ b/src/data.c
@@ -1320,6 +1320,9 @@ store_symval_forwarding (lispfwd valcontents, Lisp_Object newval,
 	if (buf == NULL)
 	  buf = current_buffer;
 	set_per_buffer_value (buf, offset, newval);
+        int idx = PER_BUFFER_IDX (offset);
+        if (idx > 0)
+	  SET_PER_BUFFER_VALUE_P (buf, idx, 1);
       }
       break;
 
@@ -1593,17 +1596,16 @@ set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
 	struct buffer *buf
 	  = BUFFERP (where) ? XBUFFER (where) : current_buffer;
 	lispfwd innercontents = SYMBOL_FWD (sym);
+        bool should_store = true;
 	if (BUFFER_OBJFWDP (innercontents))
 	  {
 	    int offset = XBUFFER_OBJFWD (innercontents)->offset;
-	    int idx = PER_BUFFER_IDX (offset);
 	    if (bindflag == SET_INTERNAL_SET
-	        && !PER_BUFFER_VALUE_P (buf, offset))
+	        && !PER_BUFFER_VALUE_P (buf, offset)
+		&& let_shadows_buffer_binding_p (sym))
 	      {
-		if (let_shadows_buffer_binding_p (sym))
-		  set_default_internal (symbol, newval, bindflag);
-		else
-		  SET_PER_BUFFER_VALUE_P (buf, idx, 1);
+		set_default_internal (symbol, newval, bindflag);
+		should_store = false;
 	      }
 	  }
 
@@ -1613,7 +1615,7 @@ set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
 	    sym->u.s.redirect = SYMBOL_PLAINVAL;
 	    SET_SYMBOL_VAL (sym, newval);
 	  }
-	else
+	else if (should_store)
 	  store_symval_forwarding (/* sym, */ innercontents, newval, buf);
 	break;
       }
-- 
2.31.1