Emacs Lisp error "Wrong type argument: commandp" - emacs

What is wrong with the following code:
(defun test
(interactive)
(message "hello"))
(global-set-key '[f4] 'test)
When evaluating this with eval-region and then pressing F4 I get the error:
Wrong type argument: commandp, test

You are missing the argument list of your test function, so Emacs interprets the (interactive) form as the arglist. Thus you have defined a non-interactive function of 1 argument instead of interactive command of no arguments.
What you want is:
(defun test ()
"My command test"
(interactive)
(message "hello"))
Lessons learned:
Always add a doc string - if you did, Emacs would have complained
Use elint (comes with Emacs, try C-h a elint RET).

Related

how to use occur in global-set-key

I'd like to list subroutine and function in fortran.
I think occur-mode looks promising but have to give regexp as a argument whenever I want to get list.
So, I want to make a key binding for automating listing as follows
(global-set-key (kbd "<f6>") (lambda () (occur '^[ ]*\(subroutine\|function\)')))
but I got error as
Wrong type argument: commandp, (lambda nil (occur (quote subroutine)))
I am not familiar Emacs Lisp. Also, it would helpful if I could get this function with neotree.

emacs function won't work, yet there is no error message

This is my first attempt at creating a function for Emacs.
I created this simple function:
(defun open-init-file ()
"Opens the ~/.emacs.d/init.el file"
(find-file user-init-file))
I put it in:
~/.emacs.d/init.el
I then ran "eval-buffer".
I got no error message. And yet I can not call this function. If I type M-x and type "open-init-file" I am told that there is no function matching that name.
Even if I put this function in its own buffer, put the cursor at the end, and run eval-last-sexp, this function still is not see as existing in Emacs.
And yet, if I put this in a buffer:
(find-file user-init-file)
and if I put the cursor at the end of this and then run eval-last-sexp, then this works as I expect.
What am I doing wrong?
You should make and interactive function (a.k.a. a command)
(defun open-init-file ()
"Opens the ~/.emacs.d/init.el file"
(interactive)
(find-file user-init-file))
As yordom mentioned, you need to do:
(defun open-init-file ()
"Opens the ~/.emacs.d/init.el file"
(interactive)
(find-file user-init-file))
The reason you can't find open-init-file when calling M-x is that you've defined a function, not a command. All commands are functions but not the other way around. :)

Emacs interactive command getting "wrong number of arguments"

I have this function in my .emacs file that I thought used to work properly but now doesn't:
(defun insert-date ()
(interactive "i")
(insert (format-time-string "%Y-%m-%d")))
Emacs gives the error when I issue M-x insert-date:
call-interactively: Wrong number of arguments: (lambda nil (interactive "i") (insert (format-time-string "%Y-%m-%d"))), 1
I thought the i argument to interactive tells Emacs to ignore any arguments because none are expected. What is wrong with the command that prevents me from using it like I think it should be used?
The texinfo documentation says:
Just `(interactive)' means pass no args when calling interactively.
You are perhaps confusing an argument that is always nil with no arguments at all. The parameter i would be useful in cases where you want to ignore a particular parameter interactively, passing nil in that case.
There is also more information in the emacs manual: 20.2.1 Using interactive

How to know when to use parenthesis

I am reading through An Introduction to Programming in Emacs Lisp, and I see the following:
This code works:
(message "The name of this buffer is: %s." (buffer-name))
while this one fails:
(message "The name of this buffer is: %s." buffer-name)
However, this code works:
(message "The value of fill-column is %d." fill-column)
while this one fails:
(message "The value of fill-column is %d." (fill-column))
My question is why? What is the difference between buffer-name and fill-column? How do I know when to use parentheses?
Simply put - buffer-name is a function (that returns a string) and fill-column is a variable (that evaluates to an integer).
Functions calls in all Lisp dialects should be surrounded by parentheses.
To see details about a function in Emacs press C-h f function-name RET.
To see details about a variable in Emacs press C-h v variable-name RET.

"Wrong type argument: commandp" error when binding a lambda to a key

I am getting a "Wrong type argument: commandp, (lambda nil (forward-line 5))" here.
(global-set-key [?\M-n] (lambda () (forward-line 5)))
What is the error? I'm fairly sure it's simple & I'm missing something obvious.
global-set-key expects an interactive command. (lambda () (interactive) (forward-line 5)) ought to work.
By the way, C-h f commandp is a pretty good starting point for errors like that.
The correct form should be this -
(global-set-key (kbd "M-n") (lambda () (interactive) (forward-line 5)))
The problem was that you forgot to put (interactive) (as brendan mentioned).
By the way, you will notice that I used the (kbd) function for specifying the key-binding. That function is immensely useful since you can put the key-bindings almost literally.
I've also seen this error on a new machine where I am using my usual .emacs file but haven't installed my packages, and the command to be executed is in one of those packages. (Because a command that can't be executed definitely isn't interactive!)