diff options
| author | Basil L. Contovounesios | 2020-11-18 12:53:03 +0000 |
|---|---|---|
| committer | Basil L. Contovounesios | 2020-11-24 16:50:37 +0000 |
| commit | dea3d6aa18e54f0d8d75cd219b511bac5b3c87b1 (patch) | |
| tree | f88602aab719cf200a3c265c3a893c415aa23c14 | |
| parent | b2ee6650243ed2777f3a6c400f194f770f00da6f (diff) | |
| download | emacs-dea3d6aa18e54f0d8d75cd219b511bac5b3c87b1.tar.gz emacs-dea3d6aa18e54f0d8d75cd219b511bac5b3c87b1.zip | |
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.
| -rw-r--r-- | lisp/custom.el | 10 | ||||
| -rw-r--r-- | test/lisp/custom-tests.el | 38 |
2 files changed, 45 insertions, 3 deletions
diff --git a/lisp/custom.el b/lisp/custom.el index 885c486c5e4..cdfd2212169 100644 --- a/lisp/custom.el +++ b/lisp/custom.el | |||
| @@ -157,7 +157,9 @@ set to nil, as the value is no longer rogue." | |||
| 157 | (if (keywordp doc) | 157 | (if (keywordp doc) |
| 158 | (error "Doc string is missing")) | 158 | (error "Doc string is missing")) |
| 159 | (let ((initialize #'custom-initialize-reset) | 159 | (let ((initialize #'custom-initialize-reset) |
| 160 | (requests nil)) | 160 | (requests nil) |
| 161 | ;; Whether automatically buffer-local. | ||
| 162 | buffer-local) | ||
| 161 | (unless (memq :group args) | 163 | (unless (memq :group args) |
| 162 | (custom-add-to-group (custom-current-group) symbol 'custom-variable)) | 164 | (custom-add-to-group (custom-current-group) symbol 'custom-variable)) |
| 163 | (while args | 165 | (while args |
| @@ -183,7 +185,7 @@ set to nil, as the value is no longer rogue." | |||
| 183 | (put symbol 'safe-local-variable value)) | 185 | (put symbol 'safe-local-variable value)) |
| 184 | ((eq keyword :local) | 186 | ((eq keyword :local) |
| 185 | (when (memq value '(t permanent)) | 187 | (when (memq value '(t permanent)) |
| 186 | (make-variable-buffer-local symbol)) | 188 | (setq buffer-local t)) |
| 187 | (when (eq value 'permanent) | 189 | (when (eq value 'permanent) |
| 188 | (put symbol 'permanent-local t))) | 190 | (put symbol 'permanent-local t))) |
| 189 | ((eq keyword :type) | 191 | ((eq keyword :type) |
| @@ -205,7 +207,9 @@ set to nil, as the value is no longer rogue." | |||
| 205 | (put symbol 'custom-requests requests) | 207 | (put symbol 'custom-requests requests) |
| 206 | ;; Do the actual initialization. | 208 | ;; Do the actual initialization. |
| 207 | (unless custom-dont-initialize | 209 | (unless custom-dont-initialize |
| 208 | (funcall initialize symbol default))) | 210 | (funcall initialize symbol default)) |
| 211 | (when buffer-local | ||
| 212 | (make-variable-buffer-local symbol))) | ||
| 209 | (run-hooks 'custom-define-hook) | 213 | (run-hooks 'custom-define-hook) |
| 210 | symbol) | 214 | symbol) |
| 211 | 215 | ||
diff --git a/test/lisp/custom-tests.el b/test/lisp/custom-tests.el index 766e4844988..e71b7913f06 100644 --- a/test/lisp/custom-tests.el +++ b/test/lisp/custom-tests.el | |||
| @@ -151,4 +151,42 @@ | |||
| 151 | (widget-apply field :value-to-internal origvalue) | 151 | (widget-apply field :value-to-internal origvalue) |
| 152 | "bar")))))) | 152 | "bar")))))) |
| 153 | 153 | ||
| 154 | (defcustom custom--test-local-option 'initial | ||
| 155 | "Buffer-local user option for testing." | ||
| 156 | :group 'emacs | ||
| 157 | :type '(choice (const initial) (const changed)) | ||
| 158 | :local t) | ||
| 159 | |||
| 160 | (defcustom custom--test-permanent-option 'initial | ||
| 161 | "Permanently local user option for testing." | ||
| 162 | :group 'emacs | ||
| 163 | :type '(choice (const initial) (const changed)) | ||
| 164 | :local 'permanent) | ||
| 165 | |||
| 166 | (ert-deftest custom-test-local-option () | ||
| 167 | "Test :local user options." | ||
| 168 | ;; Initial default values. | ||
| 169 | (should (eq custom--test-local-option 'initial)) | ||
| 170 | (should (eq custom--test-permanent-option 'initial)) | ||
| 171 | (should (eq (default-value 'custom--test-local-option) 'initial)) | ||
| 172 | (should (eq (default-value 'custom--test-permanent-option) 'initial)) | ||
| 173 | (let ((obuf (current-buffer))) | ||
| 174 | (with-temp-buffer | ||
| 175 | ;; Changed buffer-local values. | ||
| 176 | (setq custom--test-local-option 'changed) | ||
| 177 | (setq custom--test-permanent-option 'changed) | ||
| 178 | (should (eq custom--test-local-option 'changed)) | ||
| 179 | (should (eq custom--test-permanent-option 'changed)) | ||
| 180 | (should (eq (default-value 'custom--test-local-option) 'initial)) | ||
| 181 | (should (eq (default-value 'custom--test-permanent-option) 'initial)) | ||
| 182 | (with-current-buffer obuf | ||
| 183 | (should (eq custom--test-local-option 'initial)) | ||
| 184 | (should (eq custom--test-permanent-option 'initial))) | ||
| 185 | ;; Permanent variable remains unchanged. | ||
| 186 | (kill-all-local-variables) | ||
| 187 | (should (eq custom--test-local-option 'initial)) | ||
| 188 | (should (eq custom--test-permanent-option 'changed)) | ||
| 189 | (should (eq (default-value 'custom--test-local-option) 'initial)) | ||
| 190 | (should (eq (default-value 'custom--test-permanent-option) 'initial))))) | ||
| 191 | |||
| 154 | ;;; custom-tests.el ends here | 192 | ;;; custom-tests.el ends here |