summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2017-04-12 23:41:51 -0400
committerKyle Meyer <kyle@kyleam.com>2017-04-12 23:45:32 -0400
commitf01dffe56c2e59791c357ed36b9f1113109b681e (patch)
tree51e0399ebf527600134b0fcc177d7efa6eb1a545 /lisp
parent19cc41a822c7bb8632a9d823714a6d5c2ca3a93d (diff)
downloademacs.d-f01dffe56c2e59791c357ed36b9f1113109b681e.tar.gz
Add mode for reviewing diffs
Diffstat (limited to 'lisp')
-rw-r--r--lisp/km-diff.el90
1 files changed, 90 insertions, 0 deletions
diff --git a/lisp/km-diff.el b/lisp/km-diff.el
index 9c2b143..9b92f7a 100644
--- a/lisp/km-diff.el
+++ b/lisp/km-diff.el
@@ -75,6 +75,96 @@
(when km/ediff-previous-window-config
(set-window-configuration km/ediff-previous-window-config)
(setq km/ediff-previous-window-config nil)))
+
+;;; Diff review
+
+(defvar km/diff-review-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map (kbd "C-w") #'km/diff-review-copy-comment)
+ map)
+ "Keymap for Diff Review mode.")
+
+(defvar km/diff-review-font-lock-keywords
+ '(("^:.*$" . 'font-lock-function-name-face)))
+
+;;;###autoload
+(define-minor-mode km/diff-review-mode
+ "Minor mode for reviewing diffs.
+
+When this mode is turned on
+
+ * `auto-fill-mode' is enabled
+
+ * the buffer is editable
+
+ * \":\" is set as the comment character
+
+ * typing \\<km/diff-review-mode-map>\\[km/diff-review-copy-comment] \
+will copy and the current comment,
+ minus the comment character"
+ nil
+ " DiffRev"
+ km/diff-review-mode-map
+ (if km/diff-review-mode
+ (progn
+ (auto-fill-mode)
+ (set (make-local-variable 'comment-start) ": ")
+ (setq fill-column (+ 2 fill-column))
+ (font-lock-add-keywords nil km/diff-review-font-lock-keywords)
+ (setq buffer-read-only nil)
+ (if (not diff-update-on-the-fly)
+ (remove-hook 'write-contents-functions 'diff-write-contents-hooks t)
+ (remove-hook 'after-change-functions 'diff-after-change-function t)
+ (remove-hook 'post-command-hook 'diff-post-command-hook t)))
+ (font-lock-remove-keywords nil km/diff-review-font-lock-keywords)
+ (diff-mode))
+ (when font-lock-mode
+ (font-lock-flush)))
+
+(defun km/diff-review-copy-comment ()
+ (interactive)
+ (if (or (use-region-p)
+ (not (eq ?: (char-after (point-at-bol)))))
+ (call-interactively #'kill-region)
+ (let ((beg (save-excursion (goto-char (point-at-bol))
+ (if (bobp)
+ (point)
+ (while (and (eq ?: (char-after))
+ (not (bobp)))
+ (forward-line -1))
+ (forward-line)
+ (point))))
+ (end (save-excursion (goto-char (point-at-bol))
+ (while (and (eq ?: (char-after))
+ (not (eobp)))
+ (forward-line 1))
+ (forward-line -1)
+ (point-at-eol))))
+ (message
+ "%s"
+ (kill-new
+ (thread-last
+ (buffer-substring-no-properties beg end)
+ (replace-regexp-in-string "^: ?" "")
+ (replace-regexp-in-string "\\`\\s-+" "")
+ (replace-regexp-in-string "\\s-+\\'" "")))))))
+
+(defun km/diff-review-as-mail ()
+ (interactive)
+ (let ((contents (buffer-substring-no-properties (point-min) (point-max))))
+ (with-current-buffer (get-buffer-create "*Diff Review Mail*")
+ (erase-buffer)
+ (insert contents)
+ (goto-char (point-min))
+ (while (not (eobp))
+ (insert "> ")
+ (forward-line 1))
+ (goto-char (point-min))
+ (while (re-search-forward "> : ?" nil t)
+ (replace-match ""))
+ (message-mode)
+ (set-buffer-modified-p nil)
+ (pop-to-buffer (current-buffer)))))
(provide 'km-diff)
;;; km-diff.el ends here