1

Example, I want to define a package-open-homepage command, which reuses C-h P (describe-package)'s "interactive" form. I don't want to copy-and-paste because it is too long. The following

(eval
 `(defun package-open-homepage (pkg)
    "Open homepage of pkg, or do nothing if none."
    ,(interactive-form 'describe-package)
    (when-let ((desc (cadr (assq pkg package-alist)))
               (extras (package-desc-extras desc))
               (homepage (cdr (assoc :url extras))))
      (browse-url homepage))))

works, but I never use eval before, is this use case the reason that why eval is needed? and is there any other way without using eval?

By the way, I already know (put 'package-open-homepage 'interactive-form (interactive-form 'describe-package)) works.

3
  • 1
    If you know about the interactive-form symbol property, then you have an answer already. You should at least post that as an answer to your own question.
    – phils
    Commented Nov 8, 2016 at 12:48
  • advice-eval-interactive-spec from nadvice can be used to evaluate interactive forms. It uses eval internally, too. You can use it as a more general solution to evaluate interactive specs.
    – clemera
    Commented Nov 8, 2016 at 13:16
  • Note that, as of 2022-ish and Emacs 29, commit bffc4cb39d made it so that commandp will now raise an error if a command has an interactive-form property. There was a question about this in bug #55925, but as of this writing that bug has never been addressed.
    – Dale
    Commented Mar 25 at 7:09

1 Answer 1

2

Your own solution (setting the symbol property) is a perfectly fine way to go IMHO.

If you don't care about interactive forms with string literal values (the following won't work with those), your own interactive form could eval the other command's interactive form at run-time:

(defun foo (&rest args)
  (interactive
   (eval (cadr (interactive-form 'COMMAND))))
  (message "%S" args))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.