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

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. :)

Related

Run function before exit emacs

I want such feature in org-mode: before exiting emacs (while org-mode is running) it asks me: "Do you want to run function vc-dir before exit?"
I tried this:
(add-hook 'kill-emacs-hook 'vc-dir)
But it errors: "wrong number of arguments"
also tried as found here:
(defadvice save-buffers-kill-emacs (before update-mod-flag activate)
(vc-dir))
The same error.
So how to make it work in easy way: vc-dir runs always on exit.
Or how to make it work with warning message (the best way)?
Thanks!
vc-dir takes an argument (the "dir").
So you can do:
(add-hook 'kill-emacs-hook (lambda () (vc-dir "your-dir-here")))
Of course this won't stop emacs from exiting: vc-dir opens a buffer but does not "wait" for user input. For the interactive approach you want you can do this:
(add-hook 'kill-emacs-query-functions
(lambda ()
(if (y-or-n-p "Do you want to run function vc-dir before exit?")
(progn
(vc-dir "your-directory")
nil)
t)))
Change "your-directory" by default-directory if you want to use the last visited buffer as vc-directory.
How about trying that function, which asks for confirmation before running vc-dir:
(defun my-vc-check-onexit ()
(interactive)
(let ((doquit (read-from-minibuffer "Do you want to run vcs? ")))
(if (string-equal doquit "y") (vc-dir "~/my/dir"))
))
and bound it to the hook.
note: it may not be good elisp ;)

wrong type argument: stringp, nil

Before now I've just been cutting and pasting code into my .emacs file, but then I decided to add some maven functionality to emacs. Now, I don't see how I was able to mess this up, but last night I kept getting the error I put in the title when I run M-x jarl-mvn-exec. I slept on it, and came back the next day but I'm still not getting anywhere.
(defun jarl-get-pom ()
(concat (locate-dominating-file
(buffer-file-name
(current-buffer))
"pom.xml")
"pom.xml"))
(defun jarl-visit-pom ()
(interactive)
(find-file (jarl-get-pom)))
(defun jarl-mvn-exec ()
(interactive)
(switch-to-buffer (get-buffer-create "maven"))
(start-process-shell-command "mvn-exec" "maven" "mvn" "-f" (jarl-get-pom) "compile")
(start-process-shell-command "mvn-exec" "maven" "mvn" "-f" (jarl-get-pom) "exec:exec"))
You'll need to provide more information to be sure. Try setting
(setq debug-on-error t)
which will give you a stack trace showing what function is complaining about the string being nil.
My guess is that buffer-file-name is returning nil, and that's where the problem lies (not all buffers have file names). Check out the debugging section of An Introduction To Programming in Emacs Lisp, or the debugging section of the Emacs Lisp manual.
The secret to finding a problem in your init file is not a secret: binary search.
Use comment-region to comment out half your init file, then 3/4, 7/8,... It is very quick to identify the problem. comment-region also uncomments: C-h f comment-region RET.

Invalid function warning in Emacs

I wanted to get rid of that automatic "splash screen" that Emacs visits (called GNU Emacs). I added the following line to my .emacs file:
(add-hook 'after-init-hook '(kill-buffer "GNU Emacs"))
Well, it works, but I get the following warning message in the echo area:
"Invalid function: (kill-buffer "GNU Emacs")
I don't see what's invalid. Anyone know?
Thanks,
P.S. I'm sure a better approach would be to get Emacs to just not visit the GNU Emacs in the first place, but I haven't figured out how to do that (maybe something in the default.el file?)
Take a look at the variable inhibit-startup-screen.
(setq inhibit-startup-screen t)
The function add-hook expects a function as its second argument; '(kill-buffer ...) evaluates to a list, which is not a function. One way to turn it into a function is to use the lambda operator:
(add-hook 'after-init-hook (lambda () (kill-buffer "GNU Emacs")))
(setq inhibit-default-init 1) is one way to do it. Didn't it work for you?

About the fix for the interference between Company mode and Yasnippet

Emacs wiki says:
Company does interfere with
Yasnippet’s native behaviour. Here’s a
quick fix:
http://gist.github.com/265010
The code is the following:
(define-key company-active-map "\t" 'company-yasnippet-or-completion)
(defun company-yasnippet-or-completion ()
(interactive)
(if (yas/expansion-at-point)
(progn (company-abort)
(yas/expand))
(company-complete-common)))
(defun yas/expansion-at-point ()
"Tested with v0.6.1. Extracted from `yas/expand-1'"
(first (yas/current-key)))
I placed that code in my .emacs and the following message appeared:
Warning (initialization): An error occurred while loading `c:/Documents and Settings/Alex.AUTOINSTALL.001/Application Data/.emacs.elc':
Symbol's value as variable is void: company-active-map
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
Do I have to place the fix code inside a YASnippet's .el file?
or in my .emacs (which throws me an error)?
The snippet you mentioned doesn't work any more anyway.
Here's a snippet that you can use instead:
(defun company-yasnippet-or-completion ()
(interactive)
(let ((yas-fallback-behavior nil))
(unless (yas-expand)
(call-interactively #'company-complete-common))))
To make sure that this is called instead of company-complete-common, use
(add-hook 'company-mode-hook (lambda ()
(substitute-key-definition 'company-complete-common
'company-yasnippet-or-completion
company-active-map)))
Background: This locally changes the value of yas-fallback-behaviour, which causes yas to call company-complete-common if no completion is found.
That sounds like a problem with the load-path. The symbol value being void means that emacs can't find a definition for it - most likely because the file containing its definition has not been loaded yet.
You may try adding something like this in your .emacs (before the error-causing code):
;; where ~/.emacs.d/ is the path to a directory containing
;; additional library code you want emacs to load
(add-to-list 'load-path "~/.emacs.d/")

function to call same shell command in dired

i'd like to be able to call the same shell command on the marked files in dired without the need for emacs to prompt the command input as the command will always be the same. in particular, the command is "open" (for mac os x).
i tried to hack the function dired-do-shell-command in dired-aux.el but i don't understand the interactive line.
at the end of the day, i'd like to be able to bind this function to C-o for dired-mode so that i don't have to use mac os x's Finder to navigate files and open them. this will allow me to move to emacs entirely.
thanks.
(defun dired-open ()
(interactive)
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg)))
(define-key dired-mode-map (kbd "C-o") 'dired-open)
Edit:
We may use save-window-excursion to protect the existing window configuration from being messed up by the output buffer:
(defun dired-open ()
(interactive)
(save-window-excursion
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg))))