diff options
Diffstat (limited to 'git-email.el')
-rw-r--r-- | git-email.el | 63 |
1 files changed, 41 insertions, 22 deletions
diff --git a/git-email.el b/git-email.el index 150b8a7..282c1cc 100644 --- a/git-email.el +++ b/git-email.el @@ -41,8 +41,6 @@ ;;; TODO: -;; * Add proper syntax highlighting to diffs in the message buffer. - ;;; Code: (require 'project) @@ -144,15 +142,6 @@ If none of the functions return non-nil value, (declare-function log-view-current-entry "log-view.el") ;;;; Get files to send -(defun git-email--extract-diff (patch-file) - "Extract the diff from PATCH-FILE." - (with-temp-buffer - (insert-file-contents patch-file) - (goto-char (point-min)) - (buffer-substring-no-properties - (- (re-search-forward "\n\n") 1) - (point-max)))) - (defun git-email--check-file (file) "Check if FILE is a patch." (if (and (file-readable-p file) @@ -222,6 +211,36 @@ If the header is not found, return an empty string." `(,header ,(git-email--extract-header header))) git-email-headers))) +(defun git-email--extract-diff (patch-file) + "Extract the diff from PATCH-FILE." + (with-temp-buffer + (insert-file-contents patch-file) + (goto-char (point-min)) + (buffer-substring + (- (re-search-forward "\n\n") 1) + (point-max)))) + +;; See https://emacs.stackexchange.com/a/5408 +(defun git-email--fontify-diff (text) + "Fontify TEXT as diffs in `message-mode'." + (with-temp-buffer + (erase-buffer) + (insert text) + (delay-mode-hooks (diff-mode)) + (font-lock-default-function 'diff-mode) + (font-lock-default-fontify-region (point-min) + (point-max) + nil) + (buffer-string))) + +(defun git-email--fontify-using-faces (text) + (let ((pos 0)) + (while (setq next (next-single-property-change pos 'face text)) + (put-text-property pos next 'font-lock-face (get-text-property pos 'face text) text) + (setq pos next)) + (add-text-properties 0 (length text) '(fontified t) text) + text)) + (defun git-email--remove-subject (header) "Remove HEADER if it is the subject." (not (string-equal (symbol-name (car header)) "subject"))) @@ -244,17 +263,17 @@ them into the message buffer." "to" (substring sendemail-to 13 -1))) ; Remove newline (diff (git-email--extract-diff patch-file))) - (funcall git-email-compose-email-function to (cadr (assoc 'subject used-headers)) + (funcall git-email-compose-email-function to ;; Remove 'subject' header, otherwise two subject headers will be ;; inserted. + (cadr (assoc 'subject used-headers)) (seq-filter 'git-email--remove-subject used-headers)) - (goto-char (point-min)) ;; Insert diff at the beginning of the body (let ((body (or (re-search-forward "<#part \\(encrypt\\|sign\\)=.*mime>" nil t) (re-search-forward "--text follows this line--" nil t)))) (goto-char (+ body 1)) (save-excursion - (insert diff))) + (insert (git-email--fontify-using-faces (git-email--fontify-diff diff))))) ;; Jump to subject if it is a cover letter (when (re-search-backward "\\*\\*\\* SUBJECT HERE \\*\\*\\*" nil t) (kill-line)))) @@ -266,11 +285,11 @@ them into the message buffer." (let* ((default-directory (cdr (project-current))) ;; Last element is an empty string (revs (split-string - (shell-command-to-string - (concat - git-email-revision-command - (int-to-string git-email-revision-limit))) - "\n")) + (shell-command-to-string + (concat + git-email-revision-command + (int-to-string git-email-revision-limit))) + "\n")) ;; Sort the candidates correctly. ;; See https://emacs.stackexchange.com/a/41808. (sorted-revs @@ -294,9 +313,9 @@ arguments to give to 'git format-patch'. By default, the arguments in `git-email-format-patch-default-args' will be used." (interactive "P") (let* ((rev (or (seq-some (lambda (fn) - (funcall fn)) - git-email-get-revision-functions) - (git-email--minibuffer-get-revision))) + (funcall fn)) + git-email-get-revision-functions) + (git-email--minibuffer-get-revision))) ;; Extra arguments. (args (if args (apply |