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.

  • 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 Nov 8 '16 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 Nov 8 '16 at 13:16
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, privacy policy and cookie policy

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