summaryrefslogtreecommitdiffhomepage
path: root/attic/elisp/jao-notmuch-tree-fold.el
blob: ef528df93ce332aadc11bbc1e4da20a23e1356a5 (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
;;; jao-notmuch-tree-fold.el --- Show/hide (sub)tress in notmuch-tree  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  jao

;; Author: jao <mail@jao.io>
;; Keywords: mail

;; 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/>.

;;; Commentary:

;;

;;; Code:

(require 'jao-notmuch)


;; Show/hide threads

(defun jao-notmuch--tree-top () (notmuch-tree-get-prop :first))

(defun jao-notmuch--tree-bottom ()
  (let ((line-move-ignore-invisible t))
    (save-excursion
      (when (zerop (forward-line 1))
        (or (not (notmuch-tree-get-message-properties))
            (jao-notmuch--tree-top))))))

(defun jao-notmuch-tree-hide-thread ()
  (interactive)
  (notmuch-tree-thread-top)
  (save-excursion
    (forward-line 1)
    (when (not (jao-notmuch--tree-top))
      (let ((line-move-ignore-invisible nil)
            (inhibit-read-only t)
            (p (point)))
        (unless (notmuch-tree-next-thread-in-tree)
          (forward-line -1))
        (add-text-properties p (point) '(invisible t))))))

(defun jao-notmuch-tree-show-thread ()
  (interactive)
  (when (or (jao-notmuch--tree-top) (invisible-p (point)))
    (let ((line-move-ignore-invisible nil))
      (notmuch-tree-thread-top)
      (let ((inhibit-read-only t)
            (p (point)))
        (notmuch-tree-next-thread-in-tree)
        (remove-text-properties p (point) '(invisible nil))
        (goto-char p)))))

(defun jao-notmuch-tree-show-all ()
  (interactive)
  (let ((inhibit-read-only t))
    (put-text-property (point-min) (point-max) 'invisible nil)))

(defun jao-notmuch-tree-hide-all ()
  (interactive)
  (let ((inhibit-read-only t)
        (line-move-ignore-invisible nil))
      (goto-char (point-min))
      (jao-notmuch-tree-hide-thread)
      (while (notmuch-tree-next-thread-in-tree)
        (jao-notmuch-tree-hide-thread)))
  (goto-char (point-min)))

(defun jao-notmuch-tree-toggle-thread ()
  (interactive)
  (let ((line-move-ignore-invisible nil))
    (forward-line 1)
    (when (jao-notmuch--tree-top)
      (forward-line -1))
    (if (invisible-p (point))
        (jao-notmuch-tree-show-thread)
      (jao-notmuch-tree-hide-thread))))

(defvar notmuch-tree-thread-map
  (let ((m (make-keymap "Thread operations")))
    (define-key m (kbd "TAB") #'jao-notmuch-tree-toggle-thread)
    (define-key m (kbd "t") #'jao-notmuch-tree-toggle-thread)
    (define-key m (kbd "s") #'jao-notmuch-tree-show-thread)
    (define-key m (kbd "S") #'jao-notmuch-tree-show-all)
    (define-key m (kbd "h") #'jao-notmuch-tree-hide-thread)
    (define-key m (kbd "H") #'jao-notmuch-tree-hide-all)
    m))

(defun jao-notmuch--tree-next (prev thread no-exit)
  (let ((line-move-ignore-invisible t))
    (cond ((looking-at-p "^End of search results")
           (unless no-exit
             (notmuch-tree-close-message-window)
             (notmuch-tree-quit)))
          ((jao-notmuch--looking-at-new-p)
           (save-excursion (jao-notmuch-tree-show-thread))
           (notmuch-tree-show-message nil))
          (thread
           (save-excursion (jao-notmuch-tree-hide-thread))
           (when (notmuch-tree-next-thread prev)
             (save-excursion (jao-notmuch-tree-show-thread)))
           (unless (jao-notmuch--looking-at-new-p)
             (notmuch-tree-matching-message prev (not no-exit))))
          ((or (and (not prev) (jao-notmuch--tree-bottom))
               (and prev (jao-notmuch--tree-top)))
           (save-excursion (jao-notmuch-tree-hide-thread))
           (forward-line (if prev -1 1))
           (jao-notmuch--tree-next prev nil no-exit))
          ((notmuch-tree-get-message-id)
           (save-excursion (jao-notmuch-tree-show-thread))
           (notmuch-tree-matching-message prev (not no-exit)))))
  (when (notmuch-tree-get-message-id)
    (notmuch-tree-show-message nil))
  (jao-notmuch--tree-update-buffer-name))

(defun jao-notmuch-tree-next (thread &optional no-exit)
  "Next message or thread in forest, taking care of thread visibility."
  (interactive "P")
  (jao-notmuch--tree-next nil thread no-exit))

(defun jao-notmuch-tree-previous (thread)
  "Previous message or thread in forest, taking care of thread visibility.."
  (interactive "P")
  (jao-notmuch--tree-next t thread t))


(provide 'jao-notmuch-tree-fold)
;;; jao-notmuch-tree-fold.el ends here