notmuch.el: Hide email headers by default.
[notmuch] / notmuch.el
index 00a03c0ed7c1ea979f417ecc8a346b0a064bd2ef..e98a30850fdb95fc35a33e0e51ccd1050e37993b 100644 (file)
@@ -33,6 +33,7 @@
 
 (defvar notmuch-show-mode-map
   (let ((map (make-sparse-keymap)))
+    (define-key map "h" 'notmuch-show-toggle-headers-visible)
     (define-key map "n" 'notmuch-show-next-message)
     (define-key map "p" 'notmuch-show-previous-message)
     (define-key map "q" 'kill-this-buffer)
 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
 
 (defvar notmuch-show-message-begin-regexp "\fmessage{")
+(defvar notmuch-show-message-end-regexp   "\fmessage}")
+(defvar notmuch-show-header-begin-regexp  "\fheader{")
+(defvar notmuch-show-header-end-regexp    "\fheader}")
+
+(defvar notmuch-show-headers-visible t)
 
 (defun notmuch-show-next-message ()
   "Advance point to the beginning of the next message in the buffer."
   (beginning-of-line)
   (recenter 0))
 
+(defun notmuch-show-markup-this-header ()
+  (if (re-search-forward notmuch-show-header-begin-regexp nil t)
+      (progn
+       (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
+                    'invisible 'notmuch-show-marker)
+       (next-line 1)
+       (beginning-of-line)
+       (let ((beg (point)))
+         (if (re-search-forward notmuch-show-header-end-regexp nil t)
+             (progn
+               (overlay-put (make-overlay beg (match-beginning 0))
+                            'invisible 'notmuch-show-header)
+               (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
+                            'invisible 'notmuch-show-marker)))))
+    (goto-char (point-max))))
+
+(defun notmuch-show-markup-headers ()
+  (save-excursion
+    (goto-char (point-min))
+    (while (not (eobp))
+      (notmuch-show-markup-this-header))))
+
+(defun notmuch-show-toggle-headers-visible ()
+  "Toggle visibility of header fields"
+  (interactive)
+  (if notmuch-show-headers-visible
+      (progn
+       (add-to-invisibility-spec 'notmuch-show-header)
+       (set 'notmuch-show-headers-visible nil)
+       ; Need to force the redisplay for some reason
+       (force-window-update)
+       (redisplay t))
+    (remove-from-invisibility-spec 'notmuch-show-header)
+    (set 'notmuch-show-headers-visible t)
+    (force-window-update)
+    (redisplay t)))
+
 ;;;###autoload
 (defun notmuch-show-mode ()
   "Major mode for handling the output of \"notmuch show\""
   (interactive)
   (kill-all-local-variables)
+  (set (make-local-variable 'notmuch-show-headers-visible) t)
+  (notmuch-show-toggle-headers-visible)
+  (add-to-invisibility-spec 'notmuch-show-marker)
   (use-local-map notmuch-show-mode-map)
   (setq major-mode 'notmuch-show-mode
        mode-name "notmuch-show")
       (goto-char (point-min))
       (save-excursion
        (call-process "notmuch" nil t nil "show" thread-id)
+       (notmuch-show-markup-headers)
        )
       )))