Skip to content

Commit

Permalink
[Treelib] Add option to define visit-actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Miller committed Jan 25, 2024
1 parent 529876d commit ca2f600
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.elc
/.cask/
/performance-test/
/performance-test/
/Extensions.el
1 change: 1 addition & 0 deletions Changelog.org
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Made it possible to disbale workspace with a ~COMMENT~ directive
- Added option to sort alphabetic-numerically (as with ~string-version-lessp~)
- Added ~:on-expand~ and ~:on-collapse~ options to treelib nodes
- Added options to define visit-actions in extensions api.
** v3.1
- Added ~treemacs-create-workspace-from-project~ command
- Added ~treemacs-project-follow-into-home~ option
Expand Down
14 changes: 12 additions & 2 deletions Extensions.org
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ We will also define a command to open a buffer using RET:
(pop-to-buffer buffer))))
#+END_SRC

And another command to visit buffers via the ~treemacs-visit-node-***~ family of commands:

#+BEGIN_SRC emacs-lisp
(defun treemacs-showcase-visit-buffer-action (btn)
(let ((buffer (treemacs-safe-button-get btn :buffer)))
(when (buffer-live-p buffer)
(pop-to-buffer buffer))))
#+END_SRC

** Defining Node Types

Now comes the interesting part, we will use treemacs' api to tell it how we want our new trees to look, how they should
Expand Down Expand Up @@ -130,8 +139,8 @@ called with ~btn~ as their parameter.

Finally all that's left is to define the leaves of our tree - the nodes for the individual buffers.

Nothing new is happening here, we merely save the buffers in a text property so the command to open them that we have
defined above can use that information.
Nothing new is happening here, we merely save the buffers in a text property so the commands to open and visit them that
we have defined above can use that information.

#+BEGIN_SRC emacs-lisp
(treemacs-define-leaf-node-type showcase-buffer-leaf
Expand All @@ -140,6 +149,7 @@ defined above can use that information.
'face 'font-lock-string-face)
:key item
:more-properties `(:buffer ,item)
:visit-action #'treemacs-showcase-visit-buffer-action
:ret-action #'treemacs-showcase-RET-buffer-action)
#+END_SRC

Expand Down
23 changes: 18 additions & 5 deletions src/elisp/treemacs-treelib.el
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ Also pass additional DATA to predicate function.")
open-icon
closed-icon
child-type
;; visit-action
ret-action
visit-action
double-click-action
no-tab?
variadic?
Expand Down Expand Up @@ -314,6 +314,13 @@ argument is given RET will do the same as TAB. The function is called with a
single argument - the prefix arg - and must be able to handle both a closed and
and expanded node state.
VISIT-ACTION is a function that is called when a node is to be opened with a
command like `treemacs-visit-node-ace'. It is called with the current `btn' and
must be able to handle both an open and a closed state. It will most likely be
called in a window that is not the one where the button resides, so if you need
to extract text properties from the button you to must use
`treemacs-safe-button-get', e.g. \(treemacs-safe-button-get btn :path\).
DOUBLE-CLICK-ACTION is similar to RET-ACTION, but will be called without any
arguments. There is no default click behaviour, if no DOUBLE-CLICK-ACTION is
given then treemacs will do nothing for double-clicks.
Expand Down Expand Up @@ -384,6 +391,10 @@ argument."
(treemacs-define-RET-action ',closed-state ,(or ret-action (if no-tab? '#'ignore '#'treemacs-expand-extension-node)))
(treemacs-define-RET-action ',open-state ,(or ret-action (if no-tab? '#'ignore '#'treemacs-collapse-extension-node)))

(when ,visit-action
(put ',open-state :treemacs-visit-action ,visit-action)
(put ',closed-state :treemacs-visit-action ,visit-action))

(add-to-list 'treemacs--extension-registry (cons ',closed-state ,struct-name))
(add-to-list 'treemacs--extension-registry (cons ',open-state ,struct-name))

Expand All @@ -401,12 +412,13 @@ argument."
key
more-properties
ret-action
visit-action
double-click-action)

"Define a type of node that is a leaf and cannot be further expanded.
The NAME, ICON, LABEL and KEY arguments are mandatory.
MORE-PROPERTIES, RET-ACTION and DOUBLE-CLICK-ACTION are optional.
MORE-PROPERTIES, RET-ACTION, VISIT-ACTION and DOUBLE-CLICK-ACTION are optional.
For a detailed description of all arguments see
`treemacs-do-define-extension-type'."
Expand All @@ -420,9 +432,10 @@ For a detailed description of all arguments see
`(treemacs-do-define-extension-type ,name
:key ,key
:label ,label
:more-properties (nconc '(:leaf t) ,more-properties)
:more-properties (append '(:leaf t) ,more-properties)
:closed-icon ,icon
:ret-action ,ret-action
:visit-action ,visit-action
:double-click-action ,double-click-action
:no-tab? t
:children (lambda () (error "Called :children of leaf node"))
Expand Down Expand Up @@ -900,7 +913,7 @@ ITEMS: List<Any>"
:parent btn
:parent-path parent-path
:parent-dom-node parent-dom-node
:more-properties (nconc `(:item ,item) (funcall properties-fn btn item))
:more-properties (append `(:item ,item) (funcall properties-fn btn item))
:icon (funcall closed-icon-fn btn item)
:state child-state
:key (funcall key-fn btn item)
Expand Down Expand Up @@ -966,7 +979,7 @@ EXPAND-DEPTH: Int"
:parent-path parent-path
:parent-dom-node parent-dom-node
:more-properties
(nconc `(:item ,item)
(append `(:item ,item)
`(:project ,(treemacs-project->create!
:name (funcall label-fn btn item)
:path path
Expand Down

0 comments on commit ca2f600

Please sign in to comment.