Situation
I would like to have a function, that when called, creates a new file/buffer in a specific directory, indepent of the current directory. But with the same file name as the current file that the buffer is visiting.
So I created the following:
(defun change-to-foobar-directory ()
(interactive)
(setq buffer-temp-name (buffer-name))
(kill-this-buffer)
(find-file (concat "C:\\foobar\\" buffer-temp-name)))
This works great.
When I have the buffer foobar.el
in directory C:\another-dir
, and I call change-to-foobar-directory
, then I get the buffer
C:\foobar\foobar.el
. The difference is that I'm editing a buffer in another directory (C:\foobar
).
Problem
This solution works great so far. When I add that function to a hook, for example:
(add-hook 'emacs-lisp-mode-hook 'change-to-foobar-directory)
And when I'm in the file another-file.c
, I create a new buffer foobar.el
with the Evil command :e foobar.el
(which stands for edit the file foobar.el in the same directory of buffer, even when the file doesn't exist yet, then a new buffer will be created).
Because the emacs.lisp-mode
gets activated, the function change-to-foobar-directory
should be called.
But I get the same file another-file.c
instead editing the file/buffer C:\foobar\foorbar.el
. So it seems with the hook, change-to-foobar-directory
will not work properly. I'm not sure what caused this. When I call that command interactively, it will work properly.
Any idea what caused this?