]> git.eshelyaron.com Git - emacs.git/commitdiff
Simplify and generalize 'set-variable'
authorEshel Yaron <me@eshelyaron.com>
Thu, 6 Jun 2024 10:21:06 +0000 (12:21 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 6 Jun 2024 10:21:06 +0000 (12:21 +0200)
lisp/simple.el

index 28642755b0204470af449b07b989256cdde8509f..5a47243d70c8788129fcfc8e1ab21610ec8bbe66 100644 (file)
@@ -9814,65 +9814,41 @@ For a variable defined with `defcustom', it does not pay attention to
 any :set property that the variable might have (if you want that, use
 \\[customize-set-variable] instead).
 
-With a prefix argument, set VARIABLE to VALUE buffer-locally.
+Non-nil optional argument MAKE-LOCAL (interactively, a prefix argument),
+says to set VARIABLE to VALUE buffer-locally.
 
 When called interactively, the user is prompted for VARIABLE and
 then VALUE.  The current value of VARIABLE will be put in the
 minibuffer history so that it can be accessed with \\`M-n', which
 makes it easier to edit it."
   (interactive
-   (let* ((default-var (variable-at-point))
-          (var (if (custom-variable-p default-var)
-                  (read-variable (format-prompt "Set variable" default-var)
-                                 default-var)
-                (read-variable "Set variable: ")))
-         (prop (get var 'variable-interactive))
-          (obsolete (car (get var 'byte-obsolete-variable)))
-         (prompt (format "Set %s %s to value: " var
-                         (cond ((local-variable-p var)
-                                "(buffer-local)")
-                               ((or current-prefix-arg
-                                    (local-variable-if-set-p var))
-                                "buffer-locally")
-                               (t "globally"))))
-         (val (progn
-                 (when obsolete
-                   (message (concat "`%S' is obsolete; "
-                                    (if (symbolp obsolete) "use `%S' instead" "%s"))
-                            var obsolete)
-                   (sit-for 3))
-                 (if prop
-                     ;; Use VAR's `variable-interactive' property
-                     ;; as an interactive spec for prompting.
-                     (call-interactively `(lambda (arg)
-                                            (interactive ,prop)
-                                            arg))
-                   (read-from-minibuffer prompt nil
-                                         read-expression-map t
-                                         'set-variable-value-history
-                                         (format "%S" (symbol-value var)))))))
+   (let* ((vap (variable-at-point))
+          (vap (unless (equal vap 0) (symbol-name vap)))
+          (vas (completing-read (format-prompt "Set variable" vap)
+                                obarray #'boundp 'confirm nil nil vap))
+          (var (if (and vas (not (string-empty-p vas))) (intern vas)
+                 (user-error "You didn't specify a variable")))
+         (prompt (format "Set `%s' %s to value" var
+                         (if (or current-prefix-arg
+                                 (local-variable-if-set-p var))
+                             "buffer-locally"
+                           "globally")))
+          (def (format "%S" (symbol-value var)))
+         (val (read-from-minibuffer
+                (format-prompt
+                 prompt (truncate-string-to-width def 16 nil nil t))
+                nil read-expression-map t
+                'set-variable-value-history def)))
      (list var val current-prefix-arg)))
 
-  (and (custom-variable-p variable)
-       (not (get variable 'custom-type))
-       (custom-load-symbol variable))
-  (let ((type (get variable 'custom-type)))
-    (when type
-      ;; Match with custom type.
-      (require 'cus-edit)
-      (setq type (widget-convert type))
-      (unless (widget-apply type :match value)
-       (user-error "Value `%S' does not match type %S of %S"
-                   value (car type) variable))))
-
-  (if make-local
-      (make-local-variable variable))
+  (if make-local (make-local-variable variable))
 
   (set variable value)
 
   ;; Force a thorough redisplay for the case that the variable
   ;; has an effect on the display, like `tab-width' has.
   (force-mode-line-update))
+
 \f
 ;; Define the major mode for lists of completions.