Search code examples
emacsspacemacs

How do you define and call a function in Spacemacs?


I've defined a emacs / lisp function within defun dotspacemacs/user-config () like so:

(defun clientdir ()
"docstring"
neotree-dir "~/Projects/Clients"
)

How do I execute it?


Solution

  • That function will evaluate the neotree-dir variable and discard the result, then evaluate the "~/Projects/Clients" string and return it.

    i.e. Your function unconditionally returns the value "~/Projects/Clients" (unless neotree-dir is not bound as a variable, in which case it will trigger an error).

    I am guessing that you wanted to call a function called neotree-dir and pass it "~/Projects/Clients" as an argument? That would look like this: (neotree-dir "~/Projects/Clients")

    If you want to call the function interactively you must declare it as an interactive function:

    (defun clientdir ()
      "Invoke `neotree-dir' on ~/Projects/Clients"
      (interactive)
      (neotree-dir "~/Projects/Clients"))
    

    You can then call it with M-x clientdir RET, or bind it to a key sequence, etc...