This adds a function that marks messages unread if they are "seen"
that is a user configurable amount of them has been visible in the
buffer.
---
 emacs/notmuch-show.el |   74 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 5695d95..7d2c5d2 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -231,7 +231,27 @@ be the start and end of the visible portion of the buffer 
and
 should mark the appropriate messages read by applying
 `notmuch-show-mark-read'. This function will be called after
 every user interaction with notmuch."
-  :type 'function
+  :type '(radio (const :format "%h" :doc "Mark current message read.
+
+Marks the message containing point read."
+                      notmuch-show-seen-current-message)
+               (group :format "%v"
+                     (const :inline t :format "" (lambda (start end)))
+                     (list :format "%h %v\n"
+                           :doc "Mark read if enough of the message has been 
visible.
+
+A message is marked read if both the top of the message and a
+point far \"enough\" down in the message have each been visible
+in the buffer at some point. The definition of enough is
+controlled by the following paramater. Seeing the bottom of message is
+always deemed enough. Additionally, it is deemed enough if a
+point n lines into the message has been visible in the window
+where n is this variable if this variable is an integer and n is
+this variable times the height of the window if this variable is
+a float."
+                           (const :inline t :tag "" :format "" 
(notmuch-show-do-seen start end))
+                           (number :tag "Proportion needed" 0.75)))
+               (function))
   :group 'notmuch-show)

 (defmacro with-current-notmuch-show-message (&rest body)
@@ -1576,6 +1596,58 @@ marked as unread, i.e. the tag changes in
     (apply 'notmuch-show-tag-message
           (notmuch-tag-change-list notmuch-show-mark-read-tags unread))))

+(defun notmuch-show-update-seen (top-or-bottom)
+  "Update seen status of current message
+
+Mark that we have seen the TOP-OR-BOTTOM of current message."
+  (let ((current (notmuch-show-get-prop :seen)))
+    (unless (or (eq current 'both) (eq current top-or-bottom))
+      (if (not current)
+         (notmuch-show-set-prop :seen top-or-bottom)
+       (notmuch-show-set-prop :seen 'both)
+       (notmuch-show-mark-read)))))
+
+(defun notmuch-show-do-message-seen (needed start end)
+  "Update seen status for the current message.
+
+A message is seen if both the top and enough of the rest of the
+message have been visible in the buffer.  Enough means either the
+bottom of the message or a point in the message more than
+LINES-NEEDED lines into the message. LINES-NEEDED is
+NEEDED if NEEDED is an integer and NEEDED
+times the current window height if NEEDED is a float."
+  (let* ((lines-needed (if (integerp needed)
+                          needed
+                        (truncate (* needed (window-body-height)))))
+        (top (notmuch-show-message-top))
+        (bottom (notmuch-show-message-bottom)))
+    (when (notmuch-show-message-visible-p)
+      (when (>= top start)
+       (notmuch-show-update-seen 'top))
+      (when (or (<= bottom end)
+               (> (count-screen-lines top end) lines-needed))
+       (notmuch-show-update-seen 'bottom)))))
+
+(defun notmuch-show-do-seen (start end needed)
+  "Update seen status for all messages between start and end.
+
+We mark the top (bottom) of a message seen if the top (enough of
+the rest of the message) respectively have been visible in the
+buffer. See `notmuch-show-do-message-seen` for the definition of
+enough. When both the top and bottom have been seen we mark the
+message read."
+  (save-excursion
+    (goto-char start)
+    (notmuch-show-do-message-seen needed start end)
+    (while (and (< (notmuch-show-message-bottom) end)
+               (notmuch-show-goto-message-next))
+      (notmuch-show-do-message-seen needed start end))
+    ;; This is a work around because emacs gives weird answers for
+    ;; window-end if the buffer ends with invisible text.
+    (when (and (pos-visible-in-window-p (point-max))
+              (notmuch-show-message-visible-p))
+      (notmuch-show-update-seen 'bottom))))
+
 (defun notmuch-show-seen-current-message (start end)
   "Mark the current message read if it is open.

-- 
1.7.10.4

Reply via email to