From f01dffe56c2e59791c357ed36b9f1113109b681e Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Wed, 12 Apr 2017 23:41:51 -0400 Subject: Add mode for reviewing diffs --- lisp/km-diff.el | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) 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-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 -- cgit v1.2.3