]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch.el
notmuch.el: Override next-line and previous-line to make them reliable.
[notmuch] / notmuch.el
index 9bf272bed473fed9bec6c52ff404da42fa8b259d..a205267ecfed76df05c04ef0861c7999f373c849 100644 (file)
     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
     (define-key map "c" 'notmuch-show-toggle-citations-visible)
     (define-key map "h" 'notmuch-show-toggle-headers-visible)
     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
     (define-key map "c" 'notmuch-show-toggle-citations-visible)
     (define-key map "h" 'notmuch-show-toggle-headers-visible)
-    (define-key map "n" 'notmuch-show-mark-read-then-next-message)
+    (define-key map "n" 'notmuch-show-next-message)
     (define-key map "p" 'notmuch-show-previous-message)
     (define-key map "p" 'notmuch-show-previous-message)
+    (define-key map (kbd "C-n") 'notmuch-show-next-line)
+    (define-key map (kbd "C-p") 'notmuch-show-previous-line)
     (define-key map "q" 'kill-this-buffer)
     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
     (define-key map "x" 'kill-this-buffer)
     (define-key map "q" 'kill-this-buffer)
     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
     (define-key map "x" 'kill-this-buffer)
 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
 
 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
 
+; XXX: This should be a generic function in emacs somewhere, not here
+(defun point-invisible-p ()
+  "Return whether the character at point is invisible.
+
+Here visibility is determined by `buffer-invisibility-spec' and
+the invisible property of any overlays for point. It doesn't have
+anything to do with whether point is currently being displayed
+within the current window."
+  (let ((prop (get-char-property (point) 'invisible)))
+    (if (eq buffer-invisibility-spec t)
+       prop
+      (or (memq prop buffer-invisibility-spec)
+         (assq prop buffer-invisibility-spec)))))
+
+(defun notmuch-show-next-line ()
+  "Like builtin `next-line' but ensuring we end on a visible character.
+
+By advancing forward until reaching a visible character.
+
+Unlike builtin `next-line' this version accepts no arguments."
+  (interactive)
+  (set 'this-command 'next-line)
+  (call-interactively 'next-line)
+  (while (point-invisible-p)
+    (forward-char)))
+
+(defun notmuch-show-previous-line ()
+  "Like builtin `previous-line' but ensuring we end on a visible character.
+
+By advancing forward until reaching a visible character.
+
+Unlike builtin `next-line' this version accepts no arguments."
+  (interactive)
+  (set 'this-command 'previous-line)
+  (call-interactively 'previous-line)
+  (while (point-invisible-p)
+    (forward-char)))
+
 (defun notmuch-show-get-message-id ()
   (save-excursion
     (beginning-of-line)
 (defun notmuch-show-get-message-id ()
   (save-excursion
     (beginning-of-line)
@@ -147,23 +187,36 @@ by searching backward)."
        (error "Not within a valid message."))
     (forward-line 2)))
 
        (error "Not within a valid message."))
     (forward-line 2)))
 
+(defun notmuch-show-last-message-p ()
+  "Predicate testing whether point is within the last message."
+  (save-window-excursion
+    (save-excursion
+      (notmuch-show-move-to-current-message-summary-line)
+      (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
+
+(defun notmuch-show-message-unread-p ()
+  "Preficate testing whether current message is unread."
+  (member "unread" (notmuch-show-get-tags)))
+
 (defun notmuch-show-next-message ()
   "Advance to the beginning of the next message in the buffer.
 
 (defun notmuch-show-next-message ()
   "Advance to the beginning of the next message in the buffer.
 
-Moves to the end of the buffer if already on the last message in
-the buffer."
+Moves to the last visible character of the current message if
+already on the last message in the buffer."
   (interactive)
   (notmuch-show-move-to-current-message-summary-line)
   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
       (notmuch-show-move-to-current-message-summary-line)
   (interactive)
   (notmuch-show-move-to-current-message-summary-line)
   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
       (notmuch-show-move-to-current-message-summary-line)
-    (goto-char (point-max)))
+    (goto-char (- (point-max) 1))
+    (while (point-invisible-p)
+      (backward-char)))
   (recenter 0))
 
 (defun notmuch-show-find-next-message ()
   "Returns the position of the next message in the buffer.
 
   (recenter 0))
 
 (defun notmuch-show-find-next-message ()
   "Returns the position of the next message in the buffer.
 
-Or the beginning of the current message if already within the last
-message in the buffer."
+Or the position of the last visible character of the current
+message if already within the last message in the buffer."
   ; save-excursion doesn't save our window position
   ; save-window-excursion doesn't save point
   ; Looks like we have to use both.
   ; save-excursion doesn't save our window position
   ; save-window-excursion doesn't save point
   ; Looks like we have to use both.
@@ -175,11 +228,14 @@ message in the buffer."
 (defun notmuch-show-next-unread-message ()
   "Advance to the beginning of the next unread message in the buffer.
 
 (defun notmuch-show-next-unread-message ()
   "Advance to the beginning of the next unread message in the buffer.
 
-Does nothing if there are no more unread messages past the
-current point."
-  (while (and (not (eobp))
-             (not (member "unread" (notmuch-show-get-tags))))
-    (notmuch-show-next-message)))
+Moves to the last visible character of the current message if
+there are no more unread messages past the current point."
+  (notmuch-show-next-message)
+  (while (and (not (notmuch-show-last-message-p))
+             (not (notmuch-show-message-unread-p)))
+    (notmuch-show-next-message))
+  (if (not (notmuch-show-message-unread-p))
+      (notmuch-show-next-message)))
 
 (defun notmuch-show-previous-message ()
   "Backup to the beginning of the previous message in the buffer.
 
 (defun notmuch-show-previous-message ()
   "Backup to the beginning of the previous message in the buffer.
@@ -198,11 +254,11 @@ simply move to the beginning of the current message."
          ))
     (recenter 0)))
 
          ))
     (recenter 0)))
 
-(defun notmuch-show-mark-read-then-next-message ()
-  "Remove unread tag from current message, then advance to next message."
+(defun notmuch-show-mark-read-then-next-unread-message ()
+  "Remove unread tag from current message, then advance to next unread message."
   (interactive)
   (notmuch-show-remove-tag "unread")
   (interactive)
   (notmuch-show-remove-tag "unread")
-  (notmuch-show-next-message))
+  (notmuch-show-next-unread-message))
 
 (defun notmuch-show-advance-marking-read-and-archiving ()
   "Advance through buffer, marking read and archiving.
 
 (defun notmuch-show-advance-marking-read-and-archiving ()
   "Advance through buffer, marking read and archiving.
@@ -214,8 +270,8 @@ If the current message in the thread is not yet fully visible,
 scroll by a near screenful to read more of the message.
 
 Otherwise, (the end of the current message is already within the
 scroll by a near screenful to read more of the message.
 
 Otherwise, (the end of the current message is already within the
-current window), remove the \"unread\" tag from the current
-message and advance to the next message.
+current window), remove the \"unread\" tag (if present) from the
+current message and advance to the next message.
 
 Finally, if there is no further message to advance to, and this
 last message is already read, then archive the entire current
 
 Finally, if there is no further message to advance to, and this
 last message is already read, then archive the entire current
@@ -224,14 +280,14 @@ this buffer, and display the next thread from the search from
 which this thread was originally shown."
   (interactive)
   (let ((next (notmuch-show-find-next-message))
 which this thread was originally shown."
   (interactive)
   (let ((next (notmuch-show-find-next-message))
-       (unread (member "unread" (notmuch-show-get-tags))))
-    (if (and (not unread)
-            (equal next (point)))
-       (notmuch-show-archive-thread)
-      (if (and (> next (window-end))
-              (< next (point-max)))
-         (scroll-up nil)
-       (notmuch-show-mark-read-then-next-message)))))
+       (unread (notmuch-show-message-unread-p)))
+    (if (> next (window-end))
+       (scroll-up nil)
+      (if unread
+         (notmuch-show-mark-read-then-next-unread-message)
+       (if (notmuch-show-last-message-p)
+           (notmuch-show-archive-thread)
+         (notmuch-show-next-unread-message))))))
 
 (defun notmuch-show-markup-citations-region (beg end)
   (goto-char beg)
 
 (defun notmuch-show-markup-citations-region (beg end)
   (goto-char beg)
@@ -264,7 +320,7 @@ which this thread was originally shown."
     (re-search-forward notmuch-show-body-end-regexp)
     (let ((end (match-beginning 0)))
       (notmuch-show-markup-citations-region beg end)
     (re-search-forward notmuch-show-body-end-regexp)
     (let ((end (match-beginning 0)))
       (notmuch-show-markup-citations-region beg end)
-      (if (not (member "unread" (notmuch-show-get-tags)))
+      (if (not (notmuch-show-message-unread-p))
          (overlay-put (make-overlay beg end)
                       'invisible 'notmuch-show-body-read)))))
 
          (overlay-put (make-overlay beg end)
                       'invisible 'notmuch-show-body-read)))))
 
@@ -409,7 +465,14 @@ thread from that buffer can be show when done with this one)."
        (call-process "notmuch" nil t nil "show" thread-id)
        (notmuch-show-markup-messages)
        )
        (call-process "notmuch" nil t nil "show" thread-id)
        (notmuch-show-markup-messages)
        )
-      (notmuch-show-next-unread-message)
+      ; Move straight to the first unread message
+      (if (not (notmuch-show-message-unread-p))
+         (progn
+           (notmuch-show-next-unread-message)
+           ; But if there are no unread messages, go back to the
+           ; beginning of the buffer.
+           (if (not (notmuch-show-message-unread-p))
+               (goto-char (point-min)))))
       )))
 
 (defvar notmuch-search-mode-map
       )))
 
 (defvar notmuch-search-mode-map