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