forked from emacs-evil/evil-ediff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-ediff.el
201 lines (174 loc) · 7.88 KB
/
evil-ediff.el
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
;;; evil-ediff.el --- Make ediff a little evil
;; Copyright (C) 2015 Justin Burkett
;; Author: Justin Burkett <justin@burkett.cc>
;; Package-Requires: ((evil "1.2.3"))
;; Homepage: https://github.com/justbur/evil-ediff
;; Version: 0
;; 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:
;; Make ediff a little evil. This configures ediff to be a little more friendly
;; users of vim-like keybindings. Consult the help buffer (=?=) for more info.
;; Here's a table describing the bindings
;; | Command | Original Binding | Evil-ediff |
;; |-----------------------------+------------------+-------------|
;; | ediff-jump-to-difference | j | d |
;; | ediff-previous-difference | p,DEL | C-k,N,p,DEL |
;; | ediff-next-difference | n,SPC | C-j,n,SPC |
;; | jump to first difference | 1j | gg (or 1d) |
;; | jump to last difference | N/A | G |
;; | ediff-next-difference | n,SPC | C-j,n,SPC |
;; | scroll down 1 line | C-u 1 v | j |
;; | scroll up 1 line | C-u 1 V | k |
;; | scroll down half page | v,C-v | C-d,v,C-v |
;; | scroll up half page | V,M-v | C-u,V,M-v |
;; | ediff-suspend | z | C-z |
;; | scroll left | > | zh |
;; | scroll right | < | zl |
;; | copy B region to A's region | b | h |
;; | copy A region to B's region | a | l |
;; Not implemented yet
;; | restore old diff | rarb | u |
;;; Code:
(require 'evil)
(require 'ediff)
(defvar evil-ediff-initial-state-backup (evil-initial-state 'ediff-mode))
(defvar evil-ediff-long-help-message-compare2-backup ediff-long-help-message-compare2)
(defvar evil-ediff-long-help-message-compare3-backup ediff-long-help-message-compare3)
(defvar evil-ediff-long-help-message-narrow2-backup ediff-long-help-message-narrow2)
(defvar evil-ediff-long-help-message-word-backup ediff-long-help-message-word-mode)
(defvar evil-ediff-long-help-message-merge-backup ediff-long-help-message-merge)
(defvar evil-ediff-long-help-message-head-backup ediff-long-help-message-head)
(defvar evil-ediff-long-help-message-tail-backup ediff-long-help-message-tail)
(defvar evil-ediff-help-changed nil)
(defun evil-ediff-adjust-help ()
"Adjust long help messages to reflect evil-ediff bindings."
(unless evil-ediff-help-changed
(dolist (msg '(ediff-long-help-message-compare2
ediff-long-help-message-compare3
ediff-long-help-message-narrow2
ediff-long-help-message-word-mode
ediff-long-help-message-merge
ediff-long-help-message-head
ediff-long-help-message-tail))
(dolist (chng '( ;;("^" . " ")
("p,DEL -previous diff " . "C-k,p -previous diff ")
("n,SPC -next diff " . "C-j,n -next diff ")
(" j -jump to diff " . " d -jump to diff ")
(" h -highlighting " . " H -highlighting ")
(" </> -scroll lt/rt " . "zh/zl -scroll lt/rt ")
(" v/V -scroll up/dn " . "C-u/d -scroll up/dn ")
(" z/q -suspend/quit " . "C-z/q -quit/suspend ")))
(setf (symbol-value msg)
(replace-regexp-in-string (car chng) (cdr chng) (symbol-value msg))))))
(setq evil-ediff-help-changed t))
(defun evil-ediff-scroll-left (&optional arg)
"Scroll left."
(interactive "P")
(let ((last-command-event ?>))
(ediff-scroll-horizontally arg)))
(defun evil-ediff-scroll-right (&optional arg)
"Scroll right."
(interactive "P")
(let ((last-command-event ?<))
(ediff-scroll-horizontally arg)))
(defun evil-ediff-scroll-up (&optional arg)
"Scroll up by half of a page."
(interactive "P")
(let ((last-command-event ?V))
(ediff-scroll-vertically arg)))
(defun evil-ediff-scroll-down (&optional arg)
"Scroll down by half of a page."
(interactive "P")
(let ((last-command-event ?v))
(ediff-scroll-vertically arg)))
(defun evil-ediff-scroll-down-1 ()
"Scroll down by a line."
(interactive)
(let ((last-command-event ?v))
(ediff-scroll-vertically 1)))
(defun evil-ediff-scroll-up-1 ()
"Scroll down by a line."
(interactive)
(let ((last-command-event ?V))
(ediff-scroll-vertically 1)))
(defun evil-ediff-first-difference ()
"Jump to first difference."
(interactive)
(ediff-jump-to-difference 1))
(defun evil-ediff-last-difference ()
"Jump to last difference."
(interactive)
(ediff-jump-to-difference ediff-number-of-differences))
;; (defun evil-ediff-restore-diff ()
;; "Restore the copy of current region."
;; (interactive)
;; (ediff-restore-diff nil ?a)
;; (ediff-restore-diff nil ?b))
(defvar evil-ediff-bindings
'(("d" . ediff-jump-to-difference)
("H" . ediff-toggle-hilit)
("j" . evil-ediff-scroll-down-1)
("k" . evil-ediff-scroll-up-1)
("\C-j" . ediff-next-difference)
("\C-k" . ediff-previous-difference)
("N" . ediff-previous-difference)
("gg" . evil-ediff-first-difference)
("G" . evil-ediff-last-difference)
("\C-d" . evil-ediff-scroll-down)
("\C-u" . evil-ediff-scroll-up)
("\C-z" . ediff-suspend)
("z" . nil)
("zl" . evil-ediff-scroll-right)
("zh" . evil-ediff-scroll-left)
;; Not working yet
;; ("u" . evil-ediff-restore-diff)
)
"Alist of bindings changed/added in evil-ediff.")
(defun evil-ediff-startup-hook ()
"Place evil-ediff bindings in `ediff-mode-map'."
(evil-make-overriding-map ediff-mode-map 'normal)
(dolist (entry evil-ediff-bindings)
(define-key ediff-mode-map (car entry) (cdr entry)))
(unless (or ediff-3way-comparison-job
(eq ediff-split-window-function 'split-window-vertically))
(define-key ediff-mode-map "l" 'ediff-copy-A-to-B)
(define-key ediff-mode-map "h" 'ediff-copy-B-to-A))
(evil-normalize-keymaps)
nil)
;;;###autoload
(defun evil-ediff-init ()
"Initialize evil-ediff."
(interactive)
(evil-set-initial-state 'ediff-mode 'normal)
(add-hook 'ediff-startup-hook 'evil-ediff-startup-hook)
(evil-ediff-adjust-help))
(evil-ediff-init)
(defun evil-ediff-revert ()
"Revert changes made by evil-ediff."
(interactive)
(evil-set-initial-state 'ediff-mode evil-ediff-initial-state-backup)
(unless evil-ediff-help-changed
(dolist (msg
'((ediff-long-help-message-compare2 . ediff-long-help-message-compare2-backup)
(ediff-long-help-message-compare3 . ediff-long-help-message-compare3-backup)
(ediff-long-help-message-narrow2 . ediff-long-help-message-narrow2-backup)
(ediff-long-help-message-word-mode . ediff-long-help-message-word-mode-backup)
(ediff-long-help-message-merge . ediff-long-help-message-merge-backup)
(ediff-long-help-message-head . ediff-long-help-message-head-backup)
(ediff-long-help-message-tail . ediff-long-help-message-tail-backup)))
(setf (symbol-value (car msg)) (symbol-value (cdr msg)))))
(setq evil-ediff-help-changed nil)
(remove-hook 'ediff-startup-hook 'evil-ediff-startup-hook))
(provide 'evil-ediff)
;;; evil-ediff.el ends here