Refactor +vc-gutter-init-maybe-h

Also addresses a few edge cases where git-gutter-mode wasn't later
activated in file-less buffers that are later saved to files.
This commit is contained in:
Henrik Lissner 2021-02-26 21:25:16 -05:00
parent 7e491ce1c0
commit 15f7a26be6

View file

@ -25,19 +25,20 @@ flycheck indicators moved to the right fringe.")
(add-hook! 'find-file-hook
(defun +vc-gutter-init-maybe-h ()
"Enable `git-gutter-mode' in the current buffer.
If the buffer doesn't represent an existing file, `git-gutter-mode's activation
is deferred until the file is saved. Respects `git-gutter:disabled-modes'."
(let ((file-name (buffer-file-name (buffer-base-buffer))))
(when (or +vc-gutter-in-remote-files
(not (file-remote-p (or file-name default-directory))))
(if (null file-name)
(add-hook 'after-save-hook #'+vc-gutter-init-maybe-h nil 'local)
(when (and (vc-backend file-name)
(progn
(require 'git-gutter)
(not (memq major-mode git-gutter:disabled-modes))))
(if (and (display-graphic-p)
(cond
((and (file-remote-p (or file-name default-directory))
(not +vc-gutter-in-remote-files)))
;; If not a valid file, wait until it is written/saved to activate
;; git-gutter.
((not (and file-name (vc-backend file-name)))
(add-hook 'after-save-hook #'+vc-gutter-init-maybe-h nil 'local))
;; Allow git-gutter or git-gutter-fringe to activate based on the type
;; of frame we're in. This allows git-gutter to work for silly geese
;; who open both tty and gui frames from the daemon.
((if (and (display-graphic-p)
(require 'git-gutter-fringe nil t))
(setq-local git-gutter:init-function #'git-gutter-fr:init
git-gutter:view-diff-function #'git-gutter-fr:view-diff-infos
@ -48,7 +49,7 @@ is deferred until the file is saved. Respects `git-gutter:disabled-modes'."
git-gutter:clear-function #'git-gutter:clear-diff-infos
git-gutter:window-width 1))
(git-gutter-mode +1)
(remove-hook 'after-save-hook #'+vc-gutter-init-maybe-h 'local)))))))
(remove-hook 'after-save-hook #'+vc-gutter-init-maybe-h 'local))))))
;; Disable in Org mode, as per syl20bnr/spacemacs#10555 and
;; syohex/emacs-git-gutter#24. Apparently, the mode-enabling function for
@ -72,11 +73,12 @@ is deferred until the file is saved. Respects `git-gutter:disabled-modes'."
(defun +vc-gutter-update-h (&rest _)
"Refresh git-gutter on ESC. Return nil to prevent shadowing other
`doom-escape-hook' hooks."
(when (and git-gutter-mode
(not (memq this-command '(git-gutter:stage-hunk
git-gutter:revert-hunk)))
(not inhibit-redisplay))
(ignore (git-gutter)))))
(unless (or (memq this-command '(git-gutter:stage-hunk
git-gutter:revert-hunk))
inhibit-redisplay)
(ignore (if git-gutter-mode
(git-gutter)
(+vc-gutter-init-maybe-h))))))
;; update git-gutter when using magit commands
(advice-add #'magit-stage-file :after #'+vc-gutter-update-h)
(advice-add #'magit-unstage-file :after #'+vc-gutter-update-h)