Emacs Lisp: How to use interactive (for conditional arguments)?

泪湿孤枕 提交于 2019-12-01 08:08:15
(defun xtest (a &optional b)
  (interactive
   (let (a b)
     (setq a (read-number "First: "))
     (if (< a 2)
         (setq b (read-number "Second: ")))
     (list a b)))

  (message "a is %s b is %s" a b))

Interpret the form in interactive as kind of subprogram which delivers the list of argument values as return value. You can have local variables there with the help of a let-like form.

(defun something-else (a &optional b)
  (interactive
   (let* ((a-local (read-number "First num: "))
          (b-local (when (< a-local 2)
             (read-number "Second num: "))))
     (list a-local b-local)))
  (message "a is %s, b is %s" a b))

In the above example a-local and b-local are variables with names of your choice wich are local to the enclosing let*-form. The star in let* means that the evaluated expression (read-number "First num: ") for a-local is assigned to a-local before the expression (when (< a-local 2) (read-number "Second num: ")) for b-local is evaluated.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!