Skip to content

Instantly share code, notes, and snippets.

@syohex
Created December 26, 2011 15:29
Show Gist options
  • Save syohex/1521407 to your computer and use it in GitHub Desktop.
Save syohex/1521407 to your computer and use it in GitHub Desktop.
show git diff with color in Emacs
(defun git-diff ()
(interactive)
(let ((filename (buffer-file-name (current-buffer)))
(git-buf "*my/git*"))
(if (get-buffer git-buf)
(kill-buffer git-buf))
(with-current-buffer (get-buffer-create git-buf)
(setq buffer-read-only nil)
(erase-buffer)
(call-process "git" nil t nil "diff" filename)
(goto-char (point-min))
(while (re-search-forward "^\\(\\(\\+\\)[^+]\\|\\(-\\)[^-]\\)" nil t)
(let ((cur-point (or (match-beginning 2) (match-beginning 3)))
(color (if (match-beginning 2)
"green"
"deep pink")))
(end-of-line)
(add-text-properties cur-point (point)
`(face ((foreground-color . ,color))))))
(goto-char (point-min))
(while (re-search-forward "^\\(\\+\\+\\+\\|---\\)" nil t)
(let ((cur-point (match-beginning 1)))
(end-of-line)
(add-text-properties cur-point (point)
'(face ((weight . bold))))))
(goto-char (point-min))
(setq buffer-read-only t))
(pop-to-buffer git-buf)
(view-mode)))
(push '("*my/git*" :height 20) popwin:special-display-config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment