Files

59 lines
2.4 KiB
EmacsLisp

;;; git.el --- Magit, set up for Evil -*- lexical-binding: t; -*-
;;; Commentary:
;; Loaded from init.el, which must set `evil-want-keybinding' to nil before
;; Evil loads (Evil Collection needs it).
;;
;; :git Magit status for the current repo, full frame.
;; q puts your previous window layout back.
;; :git ARGS Run `git ARGS' from the repo root, e.g. :git log --oneline
;;
;; In Magit (Evil Collection layout):
;; j/k move, TAB folds, s stage, u unstage, x discard, c c commit,
;; p push, F pull, l log, d diff, b branch, z stash, gr refresh, q quit,
;; ? lists every command.
;; Commit message: opens in insert state; ZZ or C-c C-c commits,
;; ZQ or C-c C-k cancels.
;; Ctrl+hjkl, C-f and C-b keep their global meaning in Magit buffers.
;;; Code:
(use-package magit
:defer t
:custom
;; Status takes the whole frame; q restores the previous window layout.
(magit-display-buffer-function #'magit-display-buffer-fullframe-status-v1)
(magit-bury-buffer-function #'magit-restore-window-configuration))
;; Vim-style keys for Magit, its section folding, and the commit message buffer.
;; (magit magit-submodule) is the form Evil Collection's own default list uses.
(with-eval-after-load 'evil-collection
;; Evil Collection puts section jumps on C-j/C-k and page scrolling on C-f/C-b.
;; Unbind them so your window and Consult keys show through. Sibling sections
;; are still on ] and [, and scrolling on C-d/C-u.
(add-hook 'evil-collection-setup-hook
(lambda (mode &rest _)
(when (eq mode 'magit)
(evil-define-key 'normal magit-mode-map
(kbd "C-j") nil
(kbd "C-k") nil
(kbd "C-f") nil
(kbd "C-b") nil))))
(evil-collection-init '((magit magit-submodule) magit-section with-editor)))
;; Commit messages start in insert state. Added after git-commit loads so its
;; default hook functions are kept.
(with-eval-after-load 'git-commit
(add-hook 'git-commit-setup-hook #'evil-insert-state))
;; :git opens Magit; :git ARGS runs a git command.
(evil-define-command my/git (args)
"Open Magit status, or run `git ARGS' from the repository root."
(interactive "<a>")
(if (and args (string-match-p "[^[:space:]]" args))
(magit-git-command-topdir (concat "git " args))
(magit-status)))
(evil-ex-define-cmd "git" #'my/git)
;;; git.el ends here