autoloaded advice to require file before running function - emacs

How come when I autoload some function advice, eg.
;;;###autoload
(advice-add 'eclimd-start :before #'(lambda () (require 'some-library))
and then call eclimd-start, the library from which the advice is autoloaded isn't loaded? I thought I would be able to just use the following to get the file with the settings loaded before calling the function as well,
;;;###autoload
(advice-add 'eclimd-start :before (lambda () nil)
In this case the library contains settings to be used by the function eclimd-start. So, as a reproducible example, it could be
(setq eclimd-default-workspace "/hdd/workspace")
;;;###autoload
(defun my-java-hook () nil)
;;;###autoload
(advice-add 'eclimd-start :before
#'(lambda () (require 'some-library))
(provide 'some-library)
and the autoloads are created as usual into a file loaded at startup.

The effect of the ;;;###autoload is defined in an ad-hoc way depending on the thing it annotates. For function definitions, it turns into a call to the autoload function that will cause the file to be loaded when the annotated function is called, but for pretty much everything else, the annotated sexp is just copied to the autoload file.
So in your case, the ;;;###autoload on my-java-hook causes the autoloads file to contain something like (autoload 'my-java-hook "some-library"), but the same ;;;###autoload you placed in front of the advice will simply cause the autoloads file to contain (advice-add 'eclimd-start ...).
What you can do, tho is:
;;;###autoload
(advice-add 'eclimd-start :before #'my-eclimd-start-advice)
;;;###autoload
(defun my-eclimd-start-advice ()
...blablabla...)

Related

add a hook in prog-mode except a particular mode

i have a function
(defun a--before-test-save-hook()
"Test of before save hook"
(message "foobar"))
and i want to run it in prog-mode except python-mode,
but i have no clue now
and i just add-hook in prog-mode including python-mode
(add-hook 'prog-mode (lambda ()
(add-hook 'before-save-hook 'a-test-before-save-hook t t)))
i have try
(defun a-test-before-save-hook()
"Test of before save hook"
(unless (eq major-mode 'python-mode)
(message "foobar")))
but i want a better try,any solution will be appreciated.
Not sure if it's better, but you could do:
(add-hook 'prog-mode-hook
(lambda ()
(unless (derived-mode-p 'python-mode)
(add-hook 'before-save-hook
#'a-test-before-save-hook t t))))
Of course, my own reflex is to ask "what makes Python special?". The answer might let you replace the (derived-mode-p 'python-mode) test with something that goes more directly at the heart of the issue (e.g. maybe the issue is related to indentation-significance and would hence also apply to Coffeescript and Haskell and maybe you could check electric-indent-inhibit instead).

Emacs: load-file .emacs when saved

I'm new to emacs and lisp.
I wanted to auto-load the dot file when it was saved. Meaning, when I save my .emacs file, it would automatically call load-file on it (thus letting me know right away if I messed up).
But I can't seen to be able to find a comprehensive tutorial on hooks in emacs.
This is what I've come up with:
(defun load-init-after-save ()
"After saving this file, load it"
(if (eq bname this) ('load-file this) (nil))
)
(add-hook 'after-save-hook 'load-init-after-save)
Of course, this is incorrect: bname and this are just placeholders. And I don't want this function to run on all saves, just when the .emacs file is saved.
Does anyone know how to do this? Is there a better, easier way?
The following code loads your .emacs or ~/.emacs.d/init.el file after save:
(defun my-load-user-init-file-after-save ()
(when (string= (file-truename user-init-file)
(file-truename (buffer-file-name)))
(let ((debug-on-error t))
(load (buffer-file-name)))))
(add-hook 'after-save-hook #'my-load-user-init-file-after-save)
Since your intended use is error-checking, the code also enables the debugger while loading the init file, so that you get a nice backtrace in case of errors.
For error checking of your init file you may also find Flycheck useful. It checks your init file on the fly with the byte compiler, highlights any errors and warnings in the buffer, and—optionally—gives you a list of all errors and warnings.
Disclaimer: I'm the maintainer of this library.
Way 1
One way of doing is to install auto-compile-mode from MELPA, and enable it:
(defun my-emacs-lisp-hook ()
(auto-compile-mode 1))
(add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-hook)
Now each time you save an Elisp file that's byte-compiled, it will be
re-compiled. Compilation will usually catch some bad errors.
To compile any Elisp file, select it in dired (C-x d)
and press B (dired-do-byte-compile).
Way 2
Use this custom code I wrote.
(defun test-emacs ()
(interactive)
(require 'async)
(async-start
(lambda () (shell-command-to-string "emacs --batch --eval \"(condition-case e (progn (load \\\"~/.emacs\\\") (message \\\"-OK-\\\")) (error (message \\\"ERROR!\\\") (signal (car e) (cdr e))))\""))
`(lambda (output)
(if (string-match "-OK-" output)
(when ,(called-interactively-p 'any)
(message "All is well"))
(switch-to-buffer-other-window "*startup error*")
(delete-region (point-min) (point-max))
(insert output)
(search-backward "ERROR!")))))
(defun auto-test-emacs ()
(when (eq major-mode 'emacs-lisp-mode))
(test-emacs))
(add-hook 'after-save-hook 'auto-test-emacs)
This will start a new Emacs instance in the background each time you
save a file. If something goes wrong, it will complain.
The second approach uses async.
If you really want to do this just for .emacs user this:
(defun auto-test-emacs ()
(when (and (eq major-mode 'emacs-lisp-mode)
(equal (file-truename user-init-file)
(expand-file-name
buffer-file-truename)))
(test-emacs)))

Why are my mode specific .emacs hooks running for all modes?

I'm running the following code in my .emacs file:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ... )
However, the content in my-hook is running on save even when I'm in a different mode. Am I missing a step?
You should use the LOCAL argument to add-hook, which will make sure that the hook only affects the current buffer:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook nil t))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ...)
I think that calling (add-hook 'after-save-hook 'my-hook) in load-hooks adds the hook to all modes. That is, once that function is called, after-save-hook is modified for every other buffer as well.
I suspect that your hook would not be run unless you open a c file. Try opening some file without having opened any c files and see if anything is run. If it isn't it just means that the function that runs for c files modifies the save hook for everything else.
Tikhon was correct about the 'after-save-hook affecting all modes - I am now relying on a check using the following functions:
(defun in-c-mode? ()
(string= (current-major-mode) "c-mode"))
(defun current-major-mode ()
(with-current-buffer (current-buffer) major-mode))

How to call a function in a user defined function im Emacs?

I use defun to define a function in my .emacs file:
(defun myfun ()
"i am already loaded the color-theme lib"
(color-theme-initialize)
(color-theme-darkblue))
Then I want to use this function in my mode-hook:
(add-hook 'python-mode-hook
'(lambda ()
(myfun)
(...)))
But I got an error saying that the color-theme-initialize function is void.
P.S. my ~/.emacs snippet
(progn (require 'color-theme)
(color-theme-initialize))
(progn (setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(setq indent-line-function 'insert-tab))
(add-hook 'emacs-lisp-mode-hook
'(lambda ()
(color-theme-resolve)
(show-paren-mode t)
(linum-mode t)))
You need to require (or otherwise load) the library which provides color-theme-initialize. I'm guessing (require 'color-theme).
Read the error, it's not your function that cannot be called, but the
function named color-theme-initialize. This function has been
removed some times ago but can still be present in some older version
of color-theme. Thus, check your version of color-theme and see if
there is a function called color-theme-initialize (with
C-hfcolor-theme-initializeRET). If
the function is present then you have to (require 'color-theme),
otherwise you don't need to call this function.

How to run hook depending on file location

I am involved in python project where tabs are used, however i am not using them in every other code i write, it is vital to use them in that particular project. Projects are located in one directory under specific directories. I.E:
\main_folder
\project1
\project2
\project3
...etc
I have couple functions/hooks on file open and save that untabify and tabify whole buffer i work on.
;; My Functions
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun tabify-buffer ()
"Tabify current buffer"
(interactive)
(tabify (point-min) (point-max)))
;; HOOKS
; untabify buffer on open
(add-hook 'find-file-hook 'untabify-buffer)
; tabify on save
(add-hook 'before-save-hook 'tabify-buffer)
If i put it in .emacs file it is run on every .py file i open which is not what i want. What i`d like to have is to have these hooks used only in one particular folder with respective subfolders. Tried .dir_locals but it works only for properties not hooks. I can not use hooks in specific modes (i.e. python-mode) as almost all projects are written in python. To be honest i tried writing elisp conditional save but failed.
A very easy solution is to just add a configuration variable that can be used to disable the hooks. For example:
(defvar tweak-tabs t)
(add-hook 'find-file-hook
(lambda () (when tweak-tabs (untabify (point-min) (point-max)))))
(add-hook 'before-save-hook
(lambda () (when tweak-tabs (tabify (point-min) (point-max)))))
Now you can add a .dir-locals.el file in the relevant directories, setting tweak-tabs to nil, disabling this feature there.
(But another problem is that this is a pretty bad way to deal with tabs. For example, after you save a file you do see the tabs in it.)
Just for the record, to answer the literal question in the title (as I reached this question via a web search): one way to add a hook that depends on file location is to make it a function that checks for buffer-file-name. (Idea from this answer.)
For example, for the exact same problem (turn on tabs only in a particular directory, and leave tabs turned off elsewhere), I'm currently doing something like (after having installed the package smart-tabs-mode with M-x package-install):
(smart-tabs-insinuate 'python) ; This screws up all Python files (inserts tabs)
(add-hook 'python-mode-hook ; So we need to un-screw most of them
(lambda ()
(unless (and (stringp buffer-file-name)
(string-match "specialproject" buffer-file-name))
(setq smart-tabs-mode nil)))
t) ; Add this hook to end of the list
(This is a bit inverted, as smart-tabs-insinuate itself modifies python-mode-hook and then we're modifying it back, but it should do as an example.)