Passing functions as arguments to emacs lisp

I'm not sure exactly how emacs lisp handles function objects, is there a hidden argument that I can't see when I have this, then when I click C-c p

it gives an error Wrong type argument: commandp, load-cenet-files

I don't know lisp at all.

(defun load-cenet-files ()
  (load-file "~/.emacs.d/cedet/common/cedet.elc")
  (require 'semantic-gcc)
)

(global-set-key (kbd "C-c p") '(load-cenet-files)) 

      

+3


source to share


2 answers


Emacs distinguishes between functions and commands โ€” the latter are a special type of function, namely, those that can be invoked interactively by the user. The error message Wrong type argument: commandp, load-cenet-files

informs you that a point in the code is waiting for a command, but has received something else. commandp

is a predicate function that checks if its argument is a command; here he tested load-cenet-files

and found it was not a command, so barfed.

You can include a function in a command by declaring it interactive. You do this by adding the declaration (interactive)

as the first line after (defun function-name (args)

. Note that this (interactive)

is a special construct, this is not a function call, but a declaration.

(defun load-cenet-files ()
  (interactive)
  (load-file "~/.emacs.d/cedet/common/cedet.elc")
  (require 'semantic-gcc)
)

      



Once you have included a function in a command, you can call it via M-x function-name

. Also, if you want to associate a function with a keyboard shortcut, it must be a command. One last thing, which is why you see this error message: you bound a function load-cenet-files

to C-c p, but it's a function, not a command. When you paste (interactive)

you should be fine.

Finally, it seems somewhat unusual that you are trying to associate this functionality with a keyboard shortcut. Could you just put load-file

it require

in your .emacs file too? Or, if you don't want files to be downloaded globally, attach them to a specific receipt mode ?

+4


source


An interactive function is required http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/Interactive.html#Interactive , which will potentially allow the user to inject parameters into your function. If you don't want to, something like this might work:



(defun load-cenet-files ()
  (interactive)
  (load-file "~/.emacs.d/cedet/common/cedet.elc")
  (require 'semantic-gcc)
)

(global-set-key (kbd "C-c p") 'load-cenet-files) 

      

+3


source







All Articles