;; notmuch-hello.el --- welcome to notmuch, a frontend
;;
;; Copyright © David Edmondson
;;
;; This file is part of Notmuch.
;;
;; Notmuch 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.
;;
;; Notmuch 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 Notmuch. If not, see .
;;
;; Authors: David Edmondson
(require 'widget)
(require 'notmuch-lib)
(defvar notmuch-hello-searches-to-save 10)
(defvar notmuch-hello-search-width 60)
(defvar notmuch-hello-recent-searches nil)
(defun notmuch-hello-save-search (search)
(if (not (memq search notmuch-hello-recent-searches))
(push search notmuch-hello-recent-searches))
(if (> (length notmuch-hello-recent-searches)
notmuch-hello-searches-to-save)
(setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
(defun notmuch-hello-trim (search)
"Trim whitespace."
(if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
(match-string 1 search)
search))
(defun notmuch-hello-search (search)
(let ((search (notmuch-hello-trim search)))
(notmuch-hello-save-search search)
(notmuch-search search)))
(defun notmuch-hello ()
(interactive)
(switch-to-buffer "*notmuch-hello*")
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer))
(let ((all (overlay-lists)))
;; Delete all the overlays.
(mapcar 'delete-overlay (car all))
(mapcar 'delete-overlay (cdr all)))
(widget-insert "\nWelcome to notmuch.\n\n")
(let ((start (point)))
(widget-insert "Search: ")
(widget-create 'editable-field
:size notmuch-hello-search-width
:action (lambda (widget &rest ignore)
(let ((search (widget-value widget)))
(notmuch-hello-search search))))
(widget-insert "\n")
(indent-rigidly start (point) 4))
(when notmuch-hello-recent-searches
(widget-insert "\nRecent searches:\n\n")
(let ((start (point)))
(mapcar '(lambda (search)
(widget-create 'editable-field
:size notmuch-hello-search-width
:action (lambda (widget &rest ignore)
(let ((search (widget-value widget)))
(notmuch-hello-search search)))
search)
(widget-insert "\n"))
notmuch-hello-recent-searches)
(indent-rigidly start (point) 4)))
(widget-insert "\nFolders:\n\n")
(let ((start (point)))
(mapcar '(lambda (folder)
(let ((w (widget-create 'push-button
:notify (lambda (widget &rest ignore)
(notmuch-search (widget-get widget :notmuch-search-terms)))
"open")))
(widget-put w :notmuch-search-terms (cdr folder)))
(widget-insert (format " %6s %s\n" (notmuch-folder-count (cdr folder)) (car folder))))
notmuch-folders)
(indent-rigidly start (point) 4))
(widget-insert "\nAll tags:\n\n")
(let ((start (point)))
(mapcar '(lambda (tag)
(let ((w (widget-create 'push-button
:notify (lambda (widget &rest ignore)
(notmuch-search (widget-get widget :notmuch-search-terms)))
"open")))
(widget-put w :notmuch-search-terms (concat "tag:" tag)))
(widget-insert (format " %6s %s\n" (notmuch-folder-count
(concat "tag:" tag))
tag)))
(process-lines notmuch-command "search-tags"))
(indent-rigidly start (point) 4))
(use-local-map widget-keymap)
(local-set-key "=" 'notmuch-hello)
(local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
(widget-setup)
;; Always put point inside the `search' widget, which we know is
;; first.
(goto-char (point-min))
(widget-forward 1))