summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-org-focus.el
blob: 39c1aa4d30b42983c127fccf6735c1a3fe814889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; jao-org-focus.el --- focusing on org subtrees  -*- lexical-binding: t; -*-

;; Copyright (C) 2025, 2026  Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <mail@jao.io>
;; Keywords: docs

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(require 'org)
(require 'cl-lib)

(defvar-local jao-org-focus--parent nil)
(defvar-local jao-org-focus--section nil)

;;; focus on subtree
(defun jao-org-focus ()
  "Pop creatingly to an indirect buffer focused on the encloing subtree.

When invoked on an indirect buffer, pops back to its base."
  (interactive)
  (if-let* ((b (get-buffer (or jao-org-focus--parent ""))))
      (pop-to-buffer b)
    (when-let* ((elem (org-element-at-point))
                (header (if (eq 'headline (org-element-type elem))
                            elem
                          (org-previous-visible-heading 1)
                          (org-element-at-point)))
                (title (org-element-property :title header))
                (parent (buffer-name))
                (bname (format "%s [%s]" title parent)))
      (if-let* ((b (get-buffer bname)))
          (pop-to-buffer b)
        (clone-indirect-buffer bname t)
        (org-focus-mode -1)
        (org-focus-child-mode)
        (setq jao-org-focus--parent parent
              jao-org-focus--section title)
        (org-narrow-to-subtree)
        (show-subtree)
        (count-words (point-min) (point-max))))))

(defun jao-org-focus-redisplay ()
  "Redisplay a focused buffer.

Useful when its parent has been reorganised and the narrowing is out of
sync."
  (interactive)
  (when-let* ((title jao-org-focus--section))
    (widen)
    (goto-char (point-min))
    (when (re-search-forward (format "\\*+ %s" title) nil t)
      (org-narrow-to-subtree)
      (goto-char (point-min)))))

(defun jao-org-focus-redisplay-children ()
  "Find focused children and redisplay them."
  (interactive)
  (dolist (b (jao-org-focus-list))
    (with-current-buffer b
      (save-excursion (jao-org-focus-redisplay)))))

(defun jao-org-focus-list (&optional any-parent)
  "List of buffers that are focusing on a subtree of this one or its parent."
  (let ((n (or jao-org-focus--parent (buffer-name))))
    (seq-filter (lambda (b)
                  (let ((p (buffer-local-value 'jao-org-focus--parent b)))
                    (and p (or any-parent (string= n p)))))
                (buffer-list))))

(defvar jao-org-focus--focused-history nil)

(defun jao-org-focus-switch (arg)
  "Read with completion a focused child and pop to it.

With arg, offer to switch to all children, regardless of their parent."
  (interactive "P")
  (let ((fl (mapcar 'buffer-name (jao-org-focus-list arg))))
    (unless fl (error "No focused children"))
    (pop-to-buffer
     (completing-read "Focused child: " fl
                      nil t nil 'jao-org-focus--focused-history))))

(defun jao-org-focus--count-words (raw-word-list)
  (cl-loop with result = nil
           for elt in raw-word-list
           do (cl-incf (cdr (or (assoc elt result)
                                (first (push (cons elt 0) result)))))
           finally return (sort result
                                (lambda (a b) (string< (car a) (car b))))))

(defun jao-org-focus-word-stats ()
  (interactive)
  (let* ((words (split-string (downcase (buffer-string)) "\\W+" t))
         (word-list (jao-org-focus--count-words words)))
    (with-current-buffer (get-buffer-create "*word-statistics*")
      (erase-buffer)
      (insert "| word | occurences |
               |-----------+------------|\n")
      (dolist (elt word-list)
        (insert (format "| %s | %d |\n" (car elt) (cdr elt))))
      (org-mode)
      (indent-region (point-min) (point-max))
      (goto-char 100)
      (org-cycle)
      (org-table-sort-lines nil ?N)))
  (pop-to-buffer "*word-statistics*"))

(defvar jao-org-focus-consult-buffer-source
  `(:name "Focus buffers"
    :category jao-org-focus-buffers
    :action switch-to-buffer
    :hidden t
    :narrow ,(cons ?o "focus")
    :history jao-org-focus--focused-history
    :items ,(lambda () (mapcar 'buffer-name (jao-org-focus-list t)))))

(define-minor-mode org-focus-mode
  "A mode where keeping track of focused children is on by default."
  :lighter " ◎"
  :keymap '(("\C-cl" . jao-org-focus-switch)
            ("\C-cR" . jao-org-focus-redisplay)
            ("\C-co" . jao-org-focus)
            ("\C-cw" . count-words)
            ("\C-cW" . jao-org-focus-word-stats))
  (if org-focus-mode
      (add-hook 'after-save-hook #'jao-org-focus-redisplay-children nil t)
    (remove-hook 'after-save-hook #'jao-org-focus-redisplay-children t)))

(define-minor-mode org-focus-child-mode
  "A mode for the children of a focused org buffer."
  :lighter " ◉"
  :keymap org-focus-mode-map)


(provide 'jao-org-focus)
;;; jao-org-focus.el ends here