about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/evil-collection-20180720.526/evil-collection-diff-mode.el
blob: aeb480cfdb9925f35ef3ebc5f006d037d4fe98f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;;; 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/emacs-evil/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-collection)
(require 'diff-mode)

(defconst evil-collection-diff-mode-maps '(diff-mode-map))

(defun evil-collection-diff-read-only-state-switch ()
  "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))))

(defun evil-collection-diff-toggle-setup ()
  "Toggle visiting diff buffers in motion state."
  (interactive)
  (when (eq major-mode 'diff-mode)
    (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))))

;;; 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'."

  ;; 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)

  (evil-collection-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-collection-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-collection-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