emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

emacs-27 dea3d6a: Fix handling of defcustom :local tag


From: Basil L. Contovounesios
Subject: emacs-27 dea3d6a: Fix handling of defcustom :local tag
Date: Tue, 24 Nov 2020 14:23:39 -0500 (EST)

branch: emacs-27
commit dea3d6aa18e54f0d8d75cd219b511bac5b3c87b1
Author: Basil L. Contovounesios <contovob@tcd.ie>
Commit: Basil L. Contovounesios <contovob@tcd.ie>

    Fix handling of defcustom :local tag
    
    For discussion, see the following emacs-devel thread:
    https://lists.gnu.org/r/emacs-devel/2020-11/msg00734.html
    
    * lisp/custom.el (custom-declare-variable): Delay call to
    make-variable-buffer-local until after user option has been
    initialized with a value.  Otherwise the user option may be
    initialized to nil.
    * test/lisp/custom-tests.el (custom--test-local-option)
    (custom--test-permanent-option): New :local user options.
    (custom-test-local-option): New test for defcustom :local keyword.
---
 lisp/custom.el            | 10 +++++++---
 test/lisp/custom-tests.el | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/lisp/custom.el b/lisp/custom.el
index 885c486..cdfd221 100644
--- a/lisp/custom.el
+++ b/lisp/custom.el
@@ -157,7 +157,9 @@ set to nil, as the value is no longer rogue."
   (if (keywordp doc)
       (error "Doc string is missing"))
   (let ((initialize #'custom-initialize-reset)
-       (requests nil))
+        (requests nil)
+        ;; Whether automatically buffer-local.
+        buffer-local)
     (unless (memq :group args)
       (custom-add-to-group (custom-current-group) symbol 'custom-variable))
     (while args
@@ -183,7 +185,7 @@ set to nil, as the value is no longer rogue."
                 (put symbol 'safe-local-variable value))
                 ((eq keyword :local)
                  (when (memq value '(t permanent))
-                   (make-variable-buffer-local symbol))
+                   (setq buffer-local t))
                  (when (eq value 'permanent)
                    (put symbol 'permanent-local t)))
                ((eq keyword :type)
@@ -205,7 +207,9 @@ set to nil, as the value is no longer rogue."
     (put symbol 'custom-requests requests)
     ;; Do the actual initialization.
     (unless custom-dont-initialize
-      (funcall initialize symbol default)))
+      (funcall initialize symbol default))
+    (when buffer-local
+      (make-variable-buffer-local symbol)))
   (run-hooks 'custom-define-hook)
   symbol)
 
diff --git a/test/lisp/custom-tests.el b/test/lisp/custom-tests.el
index 766e484..e71b791 100644
--- a/test/lisp/custom-tests.el
+++ b/test/lisp/custom-tests.el
@@ -151,4 +151,42 @@
                                (widget-apply field :value-to-internal 
origvalue)
                                "bar"))))))
 
+(defcustom custom--test-local-option 'initial
+  "Buffer-local user option for testing."
+  :group 'emacs
+  :type '(choice (const initial) (const changed))
+  :local t)
+
+(defcustom custom--test-permanent-option 'initial
+  "Permanently local user option for testing."
+  :group 'emacs
+  :type '(choice (const initial) (const changed))
+  :local 'permanent)
+
+(ert-deftest custom-test-local-option ()
+  "Test :local user options."
+  ;; Initial default values.
+  (should (eq custom--test-local-option 'initial))
+  (should (eq custom--test-permanent-option 'initial))
+  (should (eq (default-value 'custom--test-local-option) 'initial))
+  (should (eq (default-value 'custom--test-permanent-option) 'initial))
+  (let ((obuf (current-buffer)))
+    (with-temp-buffer
+      ;; Changed buffer-local values.
+      (setq custom--test-local-option 'changed)
+      (setq custom--test-permanent-option 'changed)
+      (should (eq custom--test-local-option 'changed))
+      (should (eq custom--test-permanent-option 'changed))
+      (should (eq (default-value 'custom--test-local-option) 'initial))
+      (should (eq (default-value 'custom--test-permanent-option) 'initial))
+      (with-current-buffer obuf
+        (should (eq custom--test-local-option 'initial))
+        (should (eq custom--test-permanent-option 'initial)))
+      ;; Permanent variable remains unchanged.
+      (kill-all-local-variables)
+      (should (eq custom--test-local-option 'initial))
+      (should (eq custom--test-permanent-option 'changed))
+      (should (eq (default-value 'custom--test-local-option) 'initial))
+      (should (eq (default-value 'custom--test-permanent-option) 'initial)))))
+
 ;;; custom-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]