0

Below what the *Messages* buffer tells me:

Loading init.el (source)...
interactive-aware-fun called-interactively-p : nil
command-execute: Wrong type argument: commandp, interactive-aware-fun

and an excerpt from the init.el file:

(defun interactive-aware-fun ()
    (message "interactive-aware-fun called-interactively-p : %s" (called-interactively-p))
)
(interactive-aware-fun)
(global-set-key (kbd "<f9>") 'interactive-aware-fun)

Why does a F9 press give me the message:

command-execute: Wrong type argument: commandp, interactive-aware-fun

?

9
  • How about adding (interactive) before the (message ...) inside the function named interactive-aware-fun?
    – lawlist
    Commented Apr 14, 2023 at 23:27
  • "3.3 Make a Function Interactive": gnu.org/software/emacs/manual/html_node/eintr/Interactive.html
    – lawlist
    Commented Apr 14, 2023 at 23:36
  • @lawlist or Claudio: Please post that as an answer. Q&A need to stand on their own. Comments aren't searchable, and they can be deleted at any time. Thx.
    – Drew
    Commented Apr 15, 2023 at 2:18
  • The error was produced by command-execute, so the first thing to do is C-h f command-execute. E.g. try (command-execute 'interactive-aware-fun)`.
    – NickD
    Commented Apr 15, 2023 at 2:44
  • 1
    It's a useful reminder. It's very often that users of the site, especially new ones, try to use comments beyond what they're for in SE. You included, BTW. ;-) Compare your commenting in your Q&A with others. It's good to understand that any comments can disappear any time. As long as the long stream of comments here remains, my comments here about not relying on comments are useful, IMO. ;-)
    – Drew
    Commented Apr 15, 2023 at 14:46

1 Answer 1

0

The way you define a function is important when this function has to be called by command-execute what would be the case if this function is invoked by a user pressing a keyboard key.

When invoked directly from within elisp code the function can run as you have specified it, but to avoid the error message you have experienced you need to mark the function as interactive by using the directive (interactive) as first statement after the documentation string in the function definition. .

For more details, see 3.3 Make a Function Interactive or visit the Info node in Emacs with M-:(info "(eintr) Interactive") and the answer to the question "Why can't I bind my function to a key or call it with M-x?".  

By the way, knowing how it comes that the error is raised you can "fool" the detection mechanism of an actual interactive call and simulate it from elisp code by running this function using command-execute.

Run the code below to see that the function will report that it was called interactively when actually called from elisp script and not by user pressing a key:

(defun interactive-aware-fun ()
  "This function can detect if it was called from script or invoked by 
a keypress using 'called-interactively-p' "
    (interactive); required, else if called by a keypress there will be
    ; ^ an error message:  command-execute : Wrong type argument: commandp, interactive-aware-fun
    (message "interactive-aware-fun called-interactively-p : %s" (called-interactively-p))
)

(interactive-aware-fun)                              ;;      gives nil
(global-set-key (kbd "<f9>") 'interactive-aware-fun) ;; [F9] gives  t
(command-execute 'interactive-aware-fun)             ;;      gives  t

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