Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Apr 23, 2020
1 parent 9d57a69 commit dd1c374
Showing 1 changed file with 66 additions and 37 deletions.
103 changes: 66 additions & 37 deletions magit-delta.el
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
;;; magit-delta.el --- Use delta when viewing diffs in Magit -*- lexical-binding: t; -*-
;;; magit-delta.el --- Use Delta when displaying diffs in Magit -*- lexical-binding: t; -*-

;; Author: Dan Davison <[email protected]>
;; URL: https://github.com/dandavison/magit-delta
;; Version: 0.1
;; Package-Requires: ((emacs "24"))

;;; Commentary:

;; Use M-x magit-delta-mode to toggle between using delta, and normal Magit behavior.
;; This package integrates Delta (https://github.com/dandavison/delta) with
;; Magit (https://github.com/magit), so that diffs in Magit are displayed with
;; color highlighting provided by Delta.
;;
;; Use M-x magit-delta-mode to toggle between using Delta, and normal Magit
;; behavior.

;;; Code:

(require 'magit)
(require 'xterm-color)

(defvar magit-delta-delta-executable "delta"
"The delta executable on your system to be used by Magit.")

(defvar magit-delta-light-theme "GitHub"
"The color theme to use when Emacs has a light background.")
(defvar magit-delta-default-light-theme "GitHub"
"The default color theme when Emacs has a light background.")

(defvar magit-delta-dark-theme "Monokai Extended"
"The color theme to use when Emacs has a dark background.")
(defvar magit-delta-default-dark-theme "Monokai Extended"
"The default color theme when Emacs has a dark background.")

(defvar magit-delta-delta-args
`("--max-line-distance" "0.6"
"--24-bit-color" ,(if xterm-color--support-truecolor "always" "never")
"--color-only")
"Delta command line args as a list of strings.
"Delta command line arguments as a list of strings.
If the color theme is not specified using --theme, then it will
be chosen automatically according to whether the current Emacs
frame has a light or dark background. See `magit-delta-default-light-theme' and
`magit-delta-default-dark-theme'.
--color-only is required in order to use delta with magit; it
will be added if not present.")
Expand All @@ -37,19 +47,25 @@ will be added if not present.")
(unless (-intersection '("--theme" "--light" "--dark") args)
(setq args (nconc
(list "--theme"
(if (eq (frame-parameter nil 'background-mode) 'light)
magit-delta-light-theme magit-delta-dark-theme))
(if (eq (frame-parameter nil 'background-mode) 'dark)
magit-delta-default-dark-theme
magit-delta-default-light-theme))
args)))
(unless (member "--color-only" args)
(setq args (cons "--color-only" args)))
args))

(setq magit-diff-preprocess-git-output-always--orig-value nil
magit-diff-preprocess-git-output-function--orig-value nil
magit-delta--magit-diff-refine-hunk--orig-value nil)
(defvar magit-diff-preprocess-git-output-always--orig-value nil)
(defvar magit-diff-preprocess-git-output-function--orig-value nil)
(defvar magit-delta--magit-diff-refine-hunk--orig-value nil)

(eval-when-compile
(defvar magit-diff-preprocess-git-output-always)
(defvar magit-diff-preprocess-git-output-function))

;;;###autoload
(define-minor-mode magit-delta-mode
"Use delta to view diffs in magit.
"Use Delta when displaying diffs in Magit.
https://github.com/dandavison/delta"
:lighter " magit-delta"
Expand All @@ -62,35 +78,48 @@ https://github.com/dandavison/delta"
magit-diff-removed-highlight)))
(cond
(magit-delta-mode
(setq magit-diff-preprocess-git-output-always--orig-value magit-diff-preprocess-git-output-always
magit-diff-preprocess-git-output-always t
(setq magit-diff-preprocess-git-output-always--orig-value
magit-diff-preprocess-git-output-always

magit-diff-preprocess-git-output-always
t

magit-diff-preprocess-git-output-function--orig-value
magit-diff-preprocess-git-output-function

magit-diff-preprocess-git-output-function--orig-value magit-diff-preprocess-git-output-function
magit-diff-preprocess-git-output-function #'magit-delta-call-delta-and-convert-ansi-escape-sequences
magit-diff-preprocess-git-output-function
#'magit-delta-call-delta-and-convert-ansi-escape-sequences

magit-delta--magit-diff-refine-hunk--orig-value magit-diff-refine-hunk
magit-diff-refine-hunk nil
magit-delta--magit-diff-refine-hunk--orig-value
magit-diff-refine-hunk

face-remapping-alist (nconc
(--remove (member (car it) magit-faces-to-override)
face-remapping-alist)
(--map (cons it 'default) magit-faces-to-override))))
magit-diff-refine-hunk
nil

face-remapping-alist
(nconc
(--remove (member (car it) magit-faces-to-override)
face-remapping-alist)
(--map (cons it 'default) magit-faces-to-override))))
('deactivate
(setq magit-diff-preprocess-git-output-always magit-diff-preprocess-git-output-always--orig-value
magit-diff-preprocess-git-output-function magit-diff-preprocess-git-output-function--orig-value
magit-diff-refine-hunk magit-delta--magit-diff-refine-hunk--orig-value
face-remapping-alist (--remove (member (car it) magit-faces-to-override)
face-remapping-alist))))))

(defun magit-delta-delta-args ()
(list
"--theme" (if (eq (frame-parameter nil 'background-mode) 'light)
magit-delta-light-theme magit-delta-dark-theme)
"--max-line-distance" "0.6"
"--24-bit-color" (if xterm-color--support-truecolor "always" "never")
"--color-only"))
(setq magit-diff-preprocess-git-output-always
magit-diff-preprocess-git-output-always--orig-value

magit-diff-preprocess-git-output-function
magit-diff-preprocess-git-output-function--orig-value

magit-diff-refine-hunk
magit-delta--magit-diff-refine-hunk--orig-value

face-remapping-alist
(--remove (member (car it) magit-faces-to-override)
face-remapping-alist))))))

(defun magit-delta-call-delta-and-convert-ansi-escape-sequences ()
"Call delta on buffer contents and convert ANSI escape sequences to overlays.
The input buffer contents are expected to be raw git output. This
function is used as the value of `magit-diff-preprocess-git-output-function'."
(apply #'call-process-region
(point-min) (point-max)
magit-delta-delta-executable t t nil (magit-delta--make-delta-args))
Expand Down

0 comments on commit dd1c374

Please sign in to comment.