]> git.notmuchmail.org Git - notmuch-wiki/commitdiff
emacstips: add sample code for viewing patches in diff-mode
authorMark Walters <markwalters1009@gmail.com>
Sun, 4 May 2014 08:39:35 +0000 (09:39 +0100)
committerMark Walters <markwalters1009@gmail.com>
Sun, 4 May 2014 08:39:35 +0000 (09:39 +0100)
emacstips.mdwn

index a90911c3e789933bd41445652f44573a7e0e620d..3a1a3dde0b3513b542dd6c071b91044d67c995ca 100644 (file)
@@ -765,3 +765,33 @@ Then
 In general it is nice to have a key for org-links (not just for notmuch). For example
 
     (define-key global-map "\C-cl" 'org-store-link)
+
+## Viewing diffs in notmuch
+
+The following code allows you to view an inline patch in diff-mode
+directly from notmuch. This means that normal diff-mode commands like
+refine, next hunk etc all work.
+
+    (defun my-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))))
+
+and then this function needs to bound into the keymap with something like
+
+    (define-key 'notmuch-show-mode-map "D" 'my-notmuch-show-view-as-patch)