Skip to content

Commit 5b36cde

Browse files
committed
Use the treemacs API to navigate the OL tree
## Changes Description Instead of navigating the tree with movement commands like next and previous, this commit uses the `treemacs-goto-node` function to achieve the same result with less code and more reliably.
1 parent 2beb672 commit 5b36cde

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

org-ol-tree.el

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,11 @@ The outline chooses the theme based on the following criteria:
312312

313313
(defun org-ol-tree-system--all-the-icons-p ()
314314
"Constant indicating if package all-the-icons is installed."
315-
(declare (side-effect-free t))
316315
(fboundp 'all-the-icons-material))
317316

318317

319318
(defun org-ol-tree-system--evil-p ()
320319
"Constant indicating if package evil is installed."
321-
(declare (side-effect-free t))
322320
(and (fboundp 'evil-define-key)
323321
(fboundp 'evil-window-top)
324322
(fboundp 'evil-window-middle)
@@ -329,7 +327,6 @@ The outline chooses the theme based on the following criteria:
329327
330328
To find out if Emacs is running in GUI mode, we query the variable
331329
`window-system'."
332-
(declare (side-effect-free t))
333330
(member window-system '(x w32 ns)))
334331

335332

@@ -516,47 +513,63 @@ this functions raises a user error."
516513

517514
(defun org-ol-tree-core--root-headline ()
518515
"Return the headline object for the root node of the tree."
519-
(declare (side-effect-free t))
520516
(org-ol-tree-core--node-get :headline (org-ol-tree-core--root-node)))
521517

522518

523519
(defun org-ol-tree-core--current-headline ()
524520
"Return the headline object for the tree node under the cursor.
525521
526522
If cursor is outside a headline node, return nil."
527-
(declare (side-effect-free t))
528523
(org-ol-tree-core--node-get :headline))
529524

530525

531526
;;; --- Treemacs integration ---------------------------------------------------
532527

528+
(defun org-ol-tree-core--root-path ()
529+
"Return the treemacs path to the root node."
530+
'(:custom root))
531+
532+
533+
(defun org-ol-tree-core--section-path (&optional section-id skip-root)
534+
"Return the path list for the given SECTION-ID.
535+
536+
If SECTION-ID is nil, uses the headline id of current node.
537+
538+
If SKIP-ROOT is non nil, it will not include the initial path of
539+
`org-ol-tree-core--root-path'."
540+
(append (unless skip-root (org-ol-tree-core--root-path))
541+
(-reduce-from
542+
(lambda (acc-list elm)
543+
(-snoc acc-list (mapconcat 'identity (-snoc (last acc-list) elm) ".")))
544+
nil
545+
(split-string
546+
(or section-id (org-ol-tree-core--headline-id (org-ol-tree-core--current-headline)))
547+
"\\."))))
548+
549+
533550
(defun org-ol-tree-core--root-node ()
534551
"Return the treemacs button for the root node of the tree."
535-
(declare (side-effect-free t))
536552
(save-excursion
537553
(goto-char (treemacs-project->position (treemacs-project-at-point)))
538554
(org-ol-tree-core--current-node)))
539555

540556

541557
(defun org-ol-tree-core--current-node ()
542558
"Wrapper function around `treemacs-current-button' to allow mocks."
543-
(declare (side-effect-free t))
544559
(treemacs-current-button))
545560

546561

547562
(defun org-ol-tree-core--node-get (property &optional node)
548563
"Wrapper function around `treemacs-button-get' to allow mocks.
549564
550565
Return the value of PROPERTY associated with NODE."
551-
(declare (side-effect-free t))
552566
(treemacs-button-get (or node (org-ol-tree-core--current-node)) property))
553567

554568

555569
(defun org-ol-tree-core--node-put (property value &optional node)
556570
"Wrapper function around `treemacs-button-put' to allow mocks.
557571
558572
Set the VALUE of PROPERTY associated with NODE."
559-
(declare (side-effect-free t))
560573
(treemacs-button-put (or node (org-ol-tree-core--current-node)) property value))
561574

562575

@@ -565,7 +578,7 @@ Set the VALUE of PROPERTY associated with NODE."
565578
(defun org-ol-tree-core--create-dom (&optional buffer-or-name)
566579
"Traverse BUFFER-OR-NAME buffer to create a tree-like structure for headlines.
567580
568-
This function uses the `outline-next-headline' function to traverse the org file
581+
This function uses the `outline-next-heading' function to traverse the org file
569582
and uses the cl-struct `org-ol-tree-core--headline' as node information.
570583
571584
If BUFFER-OR-NAME is nil, uses the `current-buffer'. If the given buffer is not
@@ -768,7 +781,6 @@ displays the Outline buufer into it."
768781
"Return the window displaying the org-ol-tree buffer for the current org file.
769782
770783
Returns nil if no org-ol-tree buffer is visible."
771-
(declare (side-effect-free error-free))
772784
(if org-ol-tree--buffer-p
773785
(selected-window)
774786
(when (buffer-live-p org-ol-tree--buffer)
@@ -778,7 +790,6 @@ Returns nil if no org-ol-tree buffer is visible."
778790
(defun org-ol-tree-ui--visibility ()
779791
"Return whether the current visibility state of the org-ol-tree buffer.
780792
Valid states are 'visible, 'exists and 'none."
781-
(declare (side-effect-free t))
782793
(cond
783794
((org-ol-tree-ui--get-window) 'visible)
784795
((buffer-live-p org-ol-tree--buffer) 'exists)
@@ -790,7 +801,6 @@ Valid states are 'visible, 'exists and 'none."
790801
791802
This function takes in account the value of `org-ol-tree-ui-window-use-pixel'
792803
and if this frame is a graphical frame or not."
793-
(declare (side-effect-free t))
794804
(and (org-ol-tree-system--graphical-frame-p)
795805
org-ol-tree-ui-window-use-pixel))
796806

@@ -1034,53 +1044,23 @@ This function cancels any timer call from `org-ol-tree-action--leftclick'."
10341044

10351045
;;; --- Navigation --------------------------------------------------------------
10361046

1037-
(defun org-ol-tree-action--goto-child (position &optional target-state)
1038-
"Move cursor to the current headline child on POSITION.
1039-
1040-
If TARGET-STATE is passed, this function will make sure the child hading is
1041-
`collapsed' or `expanded', depending on the symbol passed."
1042-
(let* ((ol-state (org-ol-tree-core--node-get :state))
1043-
(headline (org-ol-tree-core--current-headline))
1044-
(children (org-ol-tree-core--headline-children headline)))
1045-
1046-
(unless (and ol-state headline)
1047-
(user-error "Cursor is not on a valid section"))
1048-
1049-
(unless (>= (length children) position)
1050-
(user-error "Section under cursor does not have any children on position %s" position))
1051-
1052-
(when (eq ol-state 'treemacs-org-ol-doc-closed-state)
1053-
(treemacs-expand-org-ol-doc))
1054-
1055-
(when (eq ol-state 'treemacs-org-ol-parent-section-closed-state)
1056-
(treemacs-expand-org-ol-parent-section))
1057-
1058-
(treemacs-next-line position)
1059-
1060-
(when target-state
1061-
(pcase target-state
1062-
('expanded (treemacs-expand-org-ol-parent-section))
1063-
('collapsed (treemacs-collapse-org-ol-parent-section))
1064-
('treemacs-org-ol-parent-section-open-state (treemacs-expand-org-ol-parent-section))
1065-
('treemacs-org-ol-parent-section-closed-state (treemacs-collapse-org-ol-parent-section))
1066-
(_ (user-error "Unrecognized state %s" target-state))))))
1067-
1068-
10691047
(defun org-ol-tree-action--goto-setion (section-id)
10701048
"Move the cursor to the section with id SECTION-ID.
10711049
10721050
This function expands any node necessary to reach the proper section. If a
10731051
section with the given SECTION-ID does not exists, an `user-error' is raised."
1074-
(let* ((path (mapcar 'string-to-number (split-string section-id "\\."))))
1075-
(org-ol-tree-action--goto-root)
1076-
(while (> (length path) 0)
1077-
(org-ol-tree-action--goto-child (pop path)))))
1052+
(let ((current-path (org-ol-tree-core--root-path))
1053+
(target-path (org-ol-tree-core--section-path section-id t)))
1054+
(while target-path
1055+
(setq current-path (append current-path (list (pop target-path))))
1056+
(treemacs-goto-node current-path)))
1057+
(goto-char (point-at-bol)))
10781058

10791059

10801060
(defun org-ol-tree-action--goto-root ()
10811061
"Move the cursor to the outline root node."
10821062
(interactive)
1083-
(treemacs-goto-node '(:custom "0")))
1063+
(treemacs-goto-node (org-ol-tree-core--root-path)))
10841064

10851065

10861066
(defun org-ol-tree-action--goto-last-node ()
@@ -1239,22 +1219,25 @@ for comparison and uses the provided name instead."
12391219

12401220
;;; --- Refresh -----------------------------------------------------------------
12411221

1242-
(defun org-ol-tree-action--refresh (&optional prevent-rebuild)
1222+
(defun org-ol-tree-action--refresh (&optional prevent-rebuild target-section-id)
12431223
"Refresh the Outline tree.
12441224
12451225
If PREVENT-REBUILD is non nil, this function just refresh the buffer content
1246-
without refreshing the base data."
1226+
without refreshing the base data.
1227+
1228+
If TARGET-SECTION-ID is not nil, uses it instead of the current headline id."
12471229
(interactive)
12481230

12491231
(when org-ol-tree--buffer-p
1250-
(unless prevent-rebuild
1251-
(let ((org-ol-tree-core--rebuild-DOM-p t))
1252-
(org-ol-tree-core--doc)))
1232+
(let ((current-section-id
1233+
(or target-section-id
1234+
(org-ol-tree-core--headline-id (org-ol-tree-core--current-headline)))))
1235+
(unless prevent-rebuild
1236+
(org-ol-tree-ui--build-outline-tree))
12531237

1254-
(let ((current-headline (org-ol-tree-core--current-headline)))
12551238
(org-ol-tree-action--goto-root)
12561239
(treemacs-collapse-org-ol-doc)
1257-
(org-ol-tree-action--goto-setion (org-ol-tree-core--headline-id current-headline)))))
1240+
(org-ol-tree-action--goto-setion current-section-id))))
12581241

12591242

12601243
(defun org-ol-tree-action--filewatch-callback (event)
@@ -1272,7 +1255,7 @@ For more information on EVENT, check the documentation of
12721255
(progn
12731256
(when (eq (org-ol-tree-ui--visibility) 'visible)
12741257
(select-window (get-buffer-window ol-buffer))
1275-
(org-ol-tree-action--refresh nil)
1258+
(org-ol-tree-action--refresh)
12761259
(select-window current-window)))
12771260
(ht-remove! org-ol-tree-action--watcher-buffers descriptor)
12781261
(ht-remove! org-ol-tree-action--buffer-watchers ol-buffer)
@@ -1508,7 +1491,7 @@ With a prefix ARG call `org-ol-tree-ui--kill-buffer' instead."
15081491
('exists
15091492
(org-ol-tree-ui--setup-buffer)
15101493
(org-ol-tree-ui--setup-window t)
1511-
(org-ol-tree-action--refresh nil))
1494+
(org-ol-tree-action--refresh))
15121495
('none
15131496
(org-ol-tree-action--init))))
15141497

0 commit comments

Comments
 (0)