2

From the package Neotree, I want to bind a key, that moves to the upper directory.

The upper directory is always on the top with .. (up a dir). So I go to the first line of the neotree buffer, and call neotree-enter to move to the upper directory. As a true Emacs'er, I figured out I need to modify it to my own preference.

So I created a function:

(defun neotree-go-to-upper-directory ()
    (interactive)
    (evil-goto-first-line)
    (neotree-enter)
)

(evil-define-key 'normal neotree-mode-map (kbd "h")'neotree-go-to-upper-directory)

After pressing h in NeoTree buffer, the cursor moves only to the top. But nothing happens. Then I call M-x neotree-enter, but still nothing happens.

When trace back into the source code of neotree-enter with C-h f then following the link, it's defined as:

(defun neotree-enter (&optional arg)
    "NeoTree typical open event.
    ARG are the same as `neo-open-file'."
    (interactive "P")
    (neo-buffer--execute arg 'neo-open-file 'neo-open-dir))

So I modified my function with adding the arguments as parameters:

(defun neotree-go-to-upper-directory (arg)
    (interactive "P")
    (evil-goto-first-line)
    (neo-open-dir arg)
)

Re-evaluated and calling neotree-go-to-upper-directory again inside Neotree buffer, but still nothing happens. Something I went wrong. But can anyone tell me where my code went wrong? It would be appreciated.

EDIT: For clarifity, as Lawlist suggested, I'm interested in a neotree solution. But some knowledge of Elisp would be also nice, because I'm struggling to understand why the function works for Neotree as well, and not in my case.

I got an working answer below:

(defun neotree-go-to-upper-directory ()
    "Go to the parent directory in the NeoTree buffer."
    (interactive)
    (neotree-dir ".."))

But unfortunately, this is not always working and I can't figure out what the cause of this is. For example, I enter a directory with neotree-enter and I want to call the function, then nothing will happen. It will be only working if I call neotree-change-root-directory then calling the function afterwards, it seems. And not when I do neotree-enter

3
  • If you compile your function with M-u M-x compile-defun once your function is called, you can follow the code execution inside the debugger. This might give you some insight as to what's going on, what code is run and which doesn't (but you think it should).
    – wvxvw
    Jan 22, 2016 at 22:27
  • 1
    Are you more interested in a neotree solution, or do you just want to better understand generally how arguments work in conjunction with the output of previously called functions that are designed to return a result? Not all arguments to functions can be functions -- in most cases, you call the function you want the value of first and store the result in a let-bound variable and pass that result to whatever function you want to call thereafter.
    – lawlist
    Jan 22, 2016 at 23:50
  • I think the title of this question is not very clear in its current state. You don't really want to know how to pass the argument, you want to know why it doesn't work. And, above that, you want to be able to step a directory up in neotree using a single command. Perhaps it would be more clear if you renamed it?
    – PythonNut
    Jan 28, 2016 at 22:35

2 Answers 2

2
+50

Another possible solution:

(defun neotree-go-to-upper-directory ()
    (interactive)
    (evil-goto-first-line)
    (call-interactively #'neotree-enter))

(evil-define-key 'normal neotree-mode-map (kbd "h")'neotree-go-to-upper-directory)

call-interactively invokes the function exactly as it would have been with a keybinding, so it should work, in theory that is.


Okay, so the .. (up a dir) line is actually a button, which temporarily overrides RET while the point is over it. In that light, the solution is:

(defun neotree-go-to-upper-directory ()
    (interactive)
    (evil-goto-first-line)
    (call-interactively #'push-button))

(evil-define-key 'normal neotree-mode-map (kbd "h")'neotree-go-to-upper-directory)
6
  • I tried that also before. It moves the cursor indeed to the first line of the Neotree buffer, but it will not invoke the neotree-enter. Perhaps it's related with my configuration? I will try it soon with emacs -Q option.
    – ReneFroger
    Jan 27, 2016 at 9:30
  • I tested it with emacs -Q option, with only a empty Evil configuration and Neotree package. And your code snippet doesn't work. It will moves the cursor to the top, but it don't changes the directory.
    – ReneFroger
    Jan 28, 2016 at 19:42
  • @ReneFroger Interesting... I will take a look at this later.
    – PythonNut
    Jan 28, 2016 at 21:18
  • @ReneFroger Ah. I see what's going on now. Please see my edit.
    – PythonNut
    Jan 28, 2016 at 22:36
  • Nice... how could you find it out?! I never heard of the push-button feature. How did you solved it?
    – ReneFroger
    Jan 29, 2016 at 18:21
1

From the package Neotree, I want to bind a key, that moves to the upper directory.

Here's what I came up with after a bit of exploration.

(defun neotree-go-to-upper-directory ()
  "Go to the parent directory in the NeoTree buffer."
  (interactive)
  (neotree-dir ".."))

Please edit the question to make it clear if this is what you were looking for (see @lawlists's comment above).

1
  • Thanks for your reply, but unfortunately, your solution seems not working sometimes. For example, I enter a directory with neotree-enter and I want to call your function, then nothing will happen. It will be only working if I call neotree-change-root-directory then calling your function afterwards, it seems. And not when I do neotree-enter. And I will edit the question, thanks for the suggestion.
    – ReneFroger
    Jan 23, 2016 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.