Treemacs 怎样能做到打开文件后,隐藏自身窗口?

之前使用 Nerdtree 的时候记得可以设置为这样:左侧文件树内的文件在主窗口中打开的同时,文件树窗口自动关闭。

我看了一下 treemacs 的说明,好像没这个功能。应该重写 treemacs-RET-action 这个地方可以做到,但是不懂应该怎么去定义。

加个 treemacs-RET-action 的 advice。在 advice 中把 treemacs 窗口关闭。

相关定义 treemacs/treemacs-customization.el at 2.8 · Alexander-Miller/treemacs · GitHub

我这样试了不行,是不是想的太简单了

(defun dotspacemacs/user-config ()

(advice-add 'treemacs-default-visit-action :after #'treemacs-quit)

)

M-x customize-group <RET> treemacs <RET>

找到 Treemacs Ret Actions Config 配置项,里面有这样一段描述:

Defines the behaviour of ‘treemacs-RET-action’.

Each alist element maps from a button state to the function that should be used for that state. The list of all possible button states is defined in ‘treemacs-valid-button-states’. Possible values are all treemacs-visit-node-* functions as well as ‘treemacs-toggle-node’ for simple open/close actions, though in general you can use any function that accepts the prefix arg as its single argument.

To keep the alist clean changes should not be made directly, but with ‘treemacs-define-RET-action’, for example like this: (treemacs-define-RET-action 'file-node-closed #’treemacs-visit-node-ace)

根据它的建议,可以这样写:

(treemacs-define-RET-action 'file-node-closed
                            #'treemacs-visit-node-close-treemacs)

相对于打开文件关闭 treemacs 窗口,我更需要将文件打开到最近打开的窗口,那么在配置文件中添加:

(treemacs-define-RET-action 'file-node-closed
                            #'treemacs-visit-node-in-most-recently-used-window)

如果需要将文件打开到最近打开的窗口,同时关闭 treemacs 窗口,那么创建一个函数,将需要的操作放在函数里:

(treemacs-define-RET-action 'file-node-closed
                            #'(lambda(&optional arg)
                                (treemacs-visit-node-in-most-recently-used-window)
                                (delete-window (treemacs-get-local-window))))
2 个赞