Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 227
user3003873 Avatar asked Jan 30 '14 14:01

user3003873


People also ask

How do I execute an external command in Emacs?

Executing External Commands. You can execute an external shell command from within Emacs using `M-!’ (‘shell-command’). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

How do I run a command asynchronously in Emacs?

Running a Shell Command Asynchronously. You can run a shell command asynchronously by adding an ampersand ( &) after it, if you have a UNIX or UNIX-like environment. Here is some EmacsLisp code that modifies ‘shell-command’ to allow many commands to execute asynchronously (and show the command at the top of the buffer):

What is ex-defun in Emacs?

ex – Function ‘ex-defun’ defines an EmacsLisp function that does its job by invoking an external interpreted language run-command – Package ‘run-command’ allows creating dynamic lists of external commands and selecting from them using auto-completion

How does confirm-kill-emacs work in Emacs?

If the value of the variable confirm-kill-emacs is non- nil, C-x C-c assumes that its value is a predicate function, and calls that function. If the result of the function call is non- nil, the session is killed, otherwise Emacs continues to run.


1 Answers

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.

like image 101
juanleon Avatar answered Oct 06 '22 08:10

juanleon