evil-collection/evil-collection-diff-mode.el

148 lines
4.9 KiB
EmacsLisp
Raw Normal View History

2017-12-07 02:41:09 +01:00
;;; evil-collection-diff-mode.el --- Add Evil bindings to diff-mode -*- lexical-binding: t -*-
2017-11-06 11:50:42 +01:00
;; Copyright (C) 2017 Pierre Neidhardt
2018-08-27 10:41:51 +02:00
;; Author: Pierre Neidhardt <mail@ambrevar.xyz>
;; Maintainer: James Nguyen <james@jojojames.com>
2018-08-27 10:41:51 +02:00
;; Pierre Neidhardt <mail@ambrevar.xyz>
2018-03-02 02:22:38 +01:00
;; URL: https://github.com/emacs-evil/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: evil, diff, tools
2017-11-06 11:50:42 +01:00
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
2017-12-07 02:41:09 +01:00
;; Evil-Collection-Diff re-uses the read-only particularity of `diff-mode':
;; When the buffer is read-only, enter motion state
;; and manipulate the diffs with simple bindings.
;; When the buffer is writage, use normal/insert states with some Evil-specific
;; keys to ease navigation.
;;
2017-12-07 02:41:09 +01:00
;; See also `evil-collection-diff-toggle-setup'.
2017-11-06 11:50:42 +01:00
;;; Code:
(require 'evil-collection)
2017-11-06 11:50:42 +01:00
(require 'diff-mode)
(defconst evil-collection-diff-mode-maps '(diff-mode-map))
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-read-only-state-switch ()
2017-11-06 11:50:42 +01:00
"Make read-only in motion state, writable in normal state."
(when (eq major-mode 'diff-mode)
(if buffer-read-only
(evil-motion-state)
(evil-normal-state))))
2017-11-06 11:50:42 +01:00
;;;###autoload
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-toggle-setup ()
2017-11-06 11:50:42 +01:00
"Toggle visiting diff buffers in motion state."
(interactive)
(when (eq major-mode 'diff-mode)
2017-12-07 10:57:39 +01:00
(if (memq 'evil-collection-diff-read-only-state-switch read-only-mode-hook)
(remove-hook 'read-only-mode-hook 'evil-collection-diff-read-only-state-switch t)
(add-hook 'read-only-mode-hook 'evil-collection-diff-read-only-state-switch nil t))))
2017-11-06 11:50:42 +01:00
;;; TODO: Report toggle function upstream?
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-toggle-context-unified (start end)
2017-11-06 11:50:42 +01:00
"Toggle between context and unified views.
START and END are either taken from the region (if a prefix arg is given) or
else cover the whole buffer."
(interactive (if (or current-prefix-arg (use-region-p))
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
;; There seems to be no way to know whether we are in context or unified views.
;; Workaround: assume that point-max will change. This is brittle.
(let ((old-point-max (point-max)))
(diff-unified->context start end)
(when (= old-point-max (point-max))
(diff-context->unified start end))))
;;; TODO: Report toggle function upstream?
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-toggle-restrict-view (&optional arg)
2017-11-06 11:50:42 +01:00
"Toggle the restriction of the view to the current hunk.
When restricting and if the prefix ARG is given, restrict the view to the
current file instead."
(interactive "P")
(if (buffer-narrowed-p)
(widen)
(diff-restrict-view arg)))
;;;###autoload
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-mode-setup ()
"Set up `evil' bindings for `diff-mode'."
2017-11-28 10:16:53 +01:00
;; Don't switch to read-only/motion state by default as this can interfere
;; with other modes which require a writable buffer, e.g. magit.
(evil-set-initial-state 'diff-mode 'normal)
2017-11-28 10:16:53 +01:00
(evil-collection-define-key 'normal 'diff-mode-map
2017-11-06 11:50:42 +01:00
;; motion
(kbd "SPC") 'scroll-up-command
(kbd "S-SPC") 'scroll-down-command
2019-03-16 18:54:12 +01:00
(kbd "[[") 'diff-file-prev
(kbd "]]") 'diff-file-next
2017-11-06 11:50:42 +01:00
(kbd "C-j") 'diff-hunk-next
(kbd "C-k") 'diff-hunk-prev
2017-11-23 16:28:46 +01:00
"gj" 'diff-hunk-next
"gk" 'diff-hunk-prev
2017-11-06 11:50:42 +01:00
"q" 'quit-window
2017-11-06 11:50:42 +01:00
"\\" 'read-only-mode) ; magit has "\"
(evil-collection-define-key 'motion 'diff-mode-map
2017-11-06 11:50:42 +01:00
;; motion
(kbd "SPC") 'scroll-up-command
(kbd "S-SPC") 'scroll-down-command
2019-03-16 18:54:12 +01:00
(kbd "[[") 'diff-file-prev
(kbd "]]") 'diff-file-next
2017-11-06 11:50:42 +01:00
(kbd "C-j") 'diff-hunk-next
(kbd "C-k") 'diff-hunk-prev
2017-11-23 16:28:46 +01:00
"gj" 'diff-hunk-next
"gk" 'diff-hunk-prev
2017-11-06 11:50:42 +01:00
2019-06-06 08:03:54 +02:00
(kbd "RET") 'diff-goto-source
2017-11-06 11:50:42 +01:00
"A" 'diff-add-change-log-entries-other-window
"a" 'diff-apply-hunk
"*" 'diff-refine-hunk
"D" 'diff-file-kill
"d" 'diff-hunk-kill
"ge" 'diff-ediff-patch
"i" 'next-error-follow-minor-mode
2017-12-07 10:57:39 +01:00
"o" 'evil-collection-diff-toggle-restrict-view
2017-11-06 11:50:42 +01:00
"~" 'diff-reverse-direction
"s" 'diff-split-hunk
"c" 'diff-test-hunk
2017-12-07 02:41:09 +01:00
"x" 'evil-collection-diff-toggle-context-unified
2017-11-06 11:50:42 +01:00
"#" 'diff-ignore-whitespace-hunk
"\\" 'read-only-mode)) ; magit has "\"
2017-12-07 02:41:09 +01:00
(add-hook 'diff-mode-hook 'evil-collection-diff-toggle-setup)
2017-11-06 11:50:42 +01:00
2017-12-07 02:41:09 +01:00
(defun evil-collection-diff-unload-function ()
2017-11-06 11:50:42 +01:00
"For `unload-feature'."
2017-12-07 02:41:09 +01:00
(remove-hook 'diff-mode-hook 'evil-collection-diff-toggle-setup))
2017-11-06 11:50:42 +01:00
2017-12-07 02:41:09 +01:00
(provide 'evil-collection-diff-mode)
;;; evil-collection-diff-mode.el ends here