Neotree is a package for emacs (available for example on elpa) that displays the directory tree in a themeable tree-buffer. It looks nice but I was looking for a feature that I was missing. A window configuration I frequently use looks like this:
+-------+------------------+ |A |B | A - neotree | | | B - some file | | | | | | | | | +-------+------------------+
Now I am expecting that when I change the buffer in window B I have neotree in window A also show the buffers file. So I rolled up my sleeves and entered following lines of Emacs Lisp into my init.el:
(defun mp:neotree-updater ()
"Hook run on buffer list update."
(interactive)
(when (eq 2 (length (window-list)))
(let* ((wnd-0 (nth 0 (window-list)))
(wnd-1 (nth 1 (window-list)))
(buf-0 (window-buffer wnd-0))
(buf-1 (window-buffer wnd-1))
(neo-buf nil)
(other-buf nil)
(filename nil))
(when (or (eq buf-0 neo-global--buffer)
(eq buf-1 neo-global--buffer))
(progn
(if (eq buf-0 neo-global--buffer)
(setq neo-buf buf-0
other-buf buf-0)
(setq neo-buf buf-1
other-buf buf-0))
(setq filename (buffer-file-name other-buf))
(when filename
(progn
(when (file-exists-p filename)
(message (concat "New filename " filename))
(setq mp:neotree-go-to-dir filename)))))))))
(add-hook 'buffer-list-update-hook 'mp:neotree-updater)
(defun mp:neotree ()
(interactive)
(if mp:neotree-go-to-dir
(progn
(neotree-find mp:neotree-go-to-dir)
(setq mp:neotree-go-to-dir nil))
(neotree)))
Not perfect
, but this way I can bind mp:neotree to „C-c n“ and have neotree jump to the current file when I hit „C-c n“. Since the mp:neotree-updater function is called in buffer-list-update-hook I was running into several recursions until I realized I can set buffer-list-update-hook temporarily to nil and so came up with this function:
(defun mp:neotree-updater ()
(when (eq 2 (length (window-list)))
(let* ((wnd-0 (nth 0 (window-list)))
(wnd-1 (nth 1 (window-list)))
(buf-0 (window-buffer wnd-0))
(buf-1 (window-buffer wnd-1))
(neo-buf nil)
(other-buf nil)
(neo-wnd nil)
(other-wnd nil)
(filename nil)
(neo-buffer (get-buffer " *NeoTree*")))
(when (and neo-buffer
(or (eq buf-0 neo-buffer)
(eq buf-1 neo-buffer)))
(progn
(if (eq buf-0 neo-buffer)
(setq neo-buf buf-0
other-buf buf-1
neo-wnd wnd-0
other-wnd wnd-1)
(setq neo-buf buf-1
other-buf buf-0
neo-wnd wnd-1
other-wnd wnd-0))
(when (not (eq wnd-0 neo-wnd))
(progn
(setq filename (buffer-file-name other-buf))
(when (and filename
(file-exists-p filename))
(progn
(let ((buffer-list-update-hook nil))
(neotree-find filename)
(select-window other-wnd)))))))))))
I have to take care for the case when I actually want to „C-x o“ into the neotree window
, otherwise point will always jump out of the window with (select-window other-wnd).
Neueste Kommentare