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

?

Claudio
  • 410
  • 2
  • 11
  • How about adding `(interactive)` before the `(message ...)` inside the function named `interactive-aware-fun`? – lawlist Apr 14 '23 at 23:27
  • "*3.3 Make a Function Interactive*": https://www.gnu.org/software/emacs/manual/html_node/eintr/Interactive.html – lawlist Apr 14 '23 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 Apr 15 '23 at 02: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 Apr 15 '23 at 02:44
  • @Claudio -- please feel free to write up an answer at your convenience. – lawlist Apr 15 '23 at 03:11
  • **Message to future visitors**: the below provided answer is worth to be checked out as it adds more details and consider the specific context of detecting from within a function if it was called interactively or from elisp code than the generic answer about how to avoid the experienced error linked in the hint about being a duplicate. – Claudio Apr 15 '23 at 08:46
  • @Drew what about deleting your comment about lack of an answer? – Claudio Apr 15 '23 at 09:39
  • @lawlist what about deleting your comments I have incorporated into the answer? – Claudio Apr 15 '23 at 09:41
  • 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 Apr 15 '23 at 14:46

1 Answers1

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
Claudio
  • 410
  • 2
  • 11