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 (add-hook! 'find-file-hook
(defun +vc-gutter-init-maybe-h () (defun +vc-gutter-init-maybe-h ()
"Enable `git-gutter-mode' in the current buffer. "Enable `git-gutter-mode' in the current buffer.
If the buffer doesn't represent an existing file, `git-gutter-mode's activation 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'." is deferred until the file is saved. Respects `git-gutter:disabled-modes'."
(let ((file-name (buffer-file-name (buffer-base-buffer)))) (let ((file-name (buffer-file-name (buffer-base-buffer))))
(when (or +vc-gutter-in-remote-files (cond
(not (file-remote-p (or file-name default-directory)))) ((and (file-remote-p (or file-name default-directory))
(if (null file-name) (not +vc-gutter-in-remote-files)))
(add-hook 'after-save-hook #'+vc-gutter-init-maybe-h nil 'local) ;; If not a valid file, wait until it is written/saved to activate
(when (and (vc-backend file-name) ;; git-gutter.
(progn ((not (and file-name (vc-backend file-name)))
(require 'git-gutter) (add-hook 'after-save-hook #'+vc-gutter-init-maybe-h nil 'local))
(not (memq major-mode git-gutter:disabled-modes)))) ;; Allow git-gutter or git-gutter-fringe to activate based on the type
(if (and (display-graphic-p) ;; 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)) (require 'git-gutter-fringe nil t))
(setq-local git-gutter:init-function #'git-gutter-fr:init (setq-local git-gutter:init-function #'git-gutter-fr:init
git-gutter:view-diff-function #'git-gutter-fr:view-diff-infos 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:clear-function #'git-gutter:clear-diff-infos
git-gutter:window-width 1)) git-gutter:window-width 1))
(git-gutter-mode +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 ;; Disable in Org mode, as per syl20bnr/spacemacs#10555 and
;; syohex/emacs-git-gutter#24. Apparently, the mode-enabling function for ;; 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 _) (defun +vc-gutter-update-h (&rest _)
"Refresh git-gutter on ESC. Return nil to prevent shadowing other "Refresh git-gutter on ESC. Return nil to prevent shadowing other
`doom-escape-hook' hooks." `doom-escape-hook' hooks."
(when (and git-gutter-mode (unless (or (memq this-command '(git-gutter:stage-hunk
(not (memq this-command '(git-gutter:stage-hunk git-gutter:revert-hunk))
git-gutter:revert-hunk))) inhibit-redisplay)
(not inhibit-redisplay)) (ignore (if git-gutter-mode
(ignore (git-gutter))))) (git-gutter)
(+vc-gutter-init-maybe-h))))))
;; update git-gutter when using magit commands ;; update git-gutter when using magit commands
(advice-add #'magit-stage-file :after #'+vc-gutter-update-h) (advice-add #'magit-stage-file :after #'+vc-gutter-update-h)
(advice-add #'magit-unstage-file :after #'+vc-gutter-update-h) (advice-add #'magit-unstage-file :after #'+vc-gutter-update-h)