[PATCH] emacs/show: add command to view a patch using diff mode

Mark Walters markwalters1009 at gmail.com
Sat Dec 8 18:05:49 PST 2012


From: markwalters1009 at gmail.com

This enables fast navigation between hunks and better highlighting.

The use of notmuch-get-bodypart-internal could be improved; hard
coding to 1 seems to work for the output of git-format-patch. It also
not so nice to call (insert (notmuch-get-bodypart-internal ...)) since the
last thing n-g-b-i does is call buffer-string.

Author: David Bremner <bremner at debian.org>
---

This is a hacked version of which does seem to quit on q. The
difficulty is that diff-mode sets minor-mode-overriding-map-alist to
change the map when the buffer is read-only and it seemed to be
difficult to override without affecting diff-mode maps globally.

This should be safe in that respect. However, I don't know whether it
is allowed to set a keymap to something within a "let" and what
happens to it after we exit the let. I am also not completely sure
about the lexical-let: but it was what diff-mode used to do this
(those two lines are copied from diff-mode)

Best wishes

Mark



 emacs/notmuch-show.el |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 20f8997..e150e7c 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -1694,6 +1694,26 @@ to show, nil otherwise."
     (set-buffer-modified-p nil)
     (view-buffer buf 'kill-buffer-if-not-modified)))
 
+(defun notmuch-show-view-as-patch ()
+  "View the the current message as a patch."
+  (interactive)
+  (let* ((id (notmuch-show-get-message-id))
+	 (subject (concat "Subject: " (notmuch-show-get-subject) "\n"))
+	 (diff-default-read-only t)
+	 (buf (get-buffer-create (concat "*notmuch-patch-" id "*")))
+	 (map (make-sparse-keymap)))
+    (define-key map "q" 'notmuch-kill-this-buffer)
+    (switch-to-buffer buf)
+    (let ((inhibit-read-only t))
+      (erase-buffer)
+      (insert subject)
+      (insert (notmuch-get-bodypart-internal id 1 nil)))
+    (set-buffer-modified-p nil)
+    (diff-mode)
+    (lexical-let ((new-ro-bind (cons 'buffer-read-only map)))
+		 (add-to-list 'minor-mode-overriding-map-alist new-ro-bind))
+    (goto-char (point-min))))
+
 (defun notmuch-show-pipe-message (entire-thread command)
   "Pipe the contents of the current message (or thread) to the given command.
 
-- 
1.7.9.1



More information about the notmuch mailing list