summaryrefslogtreecommitdiff
path: root/evil-collection-diff-mode.el
diff options
context:
space:
mode:
authorJames Nguyen <james@jojojames.com>2017-12-06 17:41:09 -0800
committerJames Nguyen <james@jojojames.com>2017-12-06 17:50:37 -0800
commit9eb7d3db0d4b7ffbc6ea6c137b0f2ec21f907d53 (patch)
treefee440d522a6270fca6b4989f654e10da8d293a1 /evil-collection-diff-mode.el
parent4992d5fb0fd310d73ddc8a8301120552eb9a136f (diff)
Change namespace to evil-collection
Diffstat (limited to 'evil-collection-diff-mode.el')
-rw-r--r--evil-collection-diff-mode.el142
1 files changed, 142 insertions, 0 deletions
diff --git a/evil-collection-diff-mode.el b/evil-collection-diff-mode.el
new file mode 100644
index 0000000..29d9bc3
--- /dev/null
+++ b/evil-collection-diff-mode.el
@@ -0,0 +1,142 @@
+;;; evil-collection-diff-mode.el --- Add Evil bindings to diff-mode -*- lexical-binding: t -*-
+
+;; Copyright (C) 2017 Pierre Neidhardt
+
+;; Author: Pierre Neidhardt <ambrevar@gmail.com>
+;; Maintainer: James Nguyen <james@jojojames.com>
+;; Pierre Neidhardt <ambrevar@gmail.com>
+;; URL: https://github.com/jojojames/evil-collection
+;; Version: 0.0.1
+;; Package-Requires: ((emacs "25.1"))
+;; Keywords: evil, diff, tools
+
+;; 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:
+;;
+;; 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.
+;;
+;; See also `evil-collection-diff-toggle-setup'.
+
+;;; Code:
+
+(require 'evil)
+(require 'diff-mode)
+
+(defun evil-collection-diff-read-only-state-switch ()
+ "Make read-only in motion state, writable in normal state."
+ (if buffer-read-only
+ (progn
+ (evil-motion-state)
+ (message "Evil Diff: enter motion state"))
+ (evil-normal-state)
+ (message "Evil Diff: enter normal state")))
+
+(defun evil-collection-diff-toggle-setup ()
+ "Toggle visiting diff buffers in motion state."
+ (interactive)
+ (when (eq major-mode 'diff-mode)
+ (if (memq 'evil-diff-read-only-state-switch read-only-mode-hook)
+ (remove-hook 'read-only-mode-hook 'evil-diff-read-only-state-switch t)
+ (add-hook 'read-only-mode-hook 'evil-diff-read-only-state-switch nil t)
+ (read-only-mode))))
+
+;;; TODO: Report toggle function upstream?
+(defun evil-collection-diff-toggle-context-unified (start end)
+ "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?
+(defun evil-collection-diff-toggle-restrict-view (&optional arg)
+ "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)))
+
+(defun evil-collection-diff-mode-setup ()
+ "Set up `evil' bindings for `diff-mode'."
+
+ (evil-set-initial-state 'diff-mode 'motion)
+
+ (evil-define-key 'normal diff-mode-map
+ ;; motion
+ (kbd "SPC") 'scroll-up-command
+ (kbd "S-SPC") 'scroll-down-command
+ (kbd "[") 'diff-file-prev
+ (kbd "]") 'diff-file-next
+ (kbd "C-j") 'diff-hunk-next
+ (kbd "C-k") 'diff-hunk-prev
+ "gj" 'diff-hunk-next
+ "gk" 'diff-hunk-prev
+
+ "\\" 'read-only-mode) ; magit has "\"
+
+ (evil-define-key 'motion diff-mode-map
+ ;; motion
+ (kbd "SPC") 'scroll-up-command
+ (kbd "S-SPC") 'scroll-down-command
+ (kbd "[") 'diff-file-prev
+ (kbd "]") 'diff-file-next
+ (kbd "C-j") 'diff-hunk-next
+ (kbd "C-k") 'diff-hunk-prev
+ "gj" 'diff-hunk-next
+ "gk" 'diff-hunk-prev
+
+ (kbd "<return>") 'diff-goto-source
+ "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
+ "o" 'evil-diff-toggle-restrict-view
+ "~" 'diff-reverse-direction
+ "s" 'diff-split-hunk
+ "c" 'diff-test-hunk
+ "x" 'evil-collection-diff-toggle-context-unified
+ "#" 'diff-ignore-whitespace-hunk
+
+ "\\" 'read-only-mode)) ; magit has "\"
+
+
+
+(add-hook 'diff-mode-hook 'evil-collection-diff-toggle-setup)
+
+(defun evil-collection-diff-unload-function ()
+ "For `unload-feature'."
+ (remove-hook 'diff-mode-hook 'evil-collection-diff-toggle-setup))
+
+(provide 'evil-collection-diff-mode)
+;;; evil-collection-diff-mode.el ends here