about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/evil-collection-20180913.209/evil-collection-pdf.el
blob: 9616ee99c27256203f3c29a79b82042af3f63766 (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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
;;; evil-collection-pdf.el --- Evil bindings for pdf-tools  -*- lexical-binding: t -*-

;; Copyright (C) 2017 Pierre Neidhardt

;; Author: Pierre Neidhardt <mail@ambrevar.xyz>
;; Maintainer: James Nguyen <james@jojojames.com>
;; Pierre Neidhardt <mail@ambrevar.xyz>
;; URL: https://github.com/emacs-evil/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: evil, pdf, 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 bindings for pdf-tools.

;;; Code:
(require 'evil-collection)
(require 'pdf-tools nil t)
(require 'pdf-view nil t)

(defconst evil-collection-pdf-maps '(pdf-view-mode-map
                                     pdf-outline-buffer-mode-map
                                     pdf-occur-buffer-mode-map))

(declare-function pdf-view-last-page "pdf-view")
(declare-function pdf-view-first-page "pdf-view")
(declare-function pdf-view-goto-page "pdf-view")
(declare-function pdf-view-previous-line-or-previous-page "pdf-view")
(declare-function pdf-view-next-line-or-next-page "pdf-view")

(defvar pdf-view-mode-map)
(defvar pdf-outline-buffer-mode-map)
(defvar pdf-occur-buffer-mode-map)

(defvar pdf-view-mode-map)
(defvar pdf-outline-buffer-mode-map)
(defvar pdf-occur-buffer-mode-map)

;; TODO: The following 2 functions are workarounds for
;; 'pdf-view-next-line-or-next-page' and
;; 'pdf-view-previous-line-or-previous-page' not playing well with
;; EVIL. The root cause should be found and fixed instead.
;; See https://github.com/emacs-evil/evil-collection/pull/137 for
;; details.
(defun evil-collection-pdf-view-next-line-or-next-page (&optional count)
  "'evil' wrapper include a count argument to `pdf-view-next-line-or-next-page'"
  (interactive "P")
  (if count
      (dotimes (_ count nil)
	(pdf-view-next-line-or-next-page 1))
    (pdf-view-next-line-or-next-page 1)))

(defun evil-collection-pdf-view-previous-line-or-previous-page (&optional count)
  "'evil' wrapper include a count argument to `pdf-view-previous-line-or-previous-page'"
  (interactive "P")
  (if count
      (dotimes (_ count nil)
	(pdf-view-previous-line-or-previous-page 1))
    (pdf-view-previous-line-or-previous-page 1)))

(defun evil-collection-pdf-view-goto-page (&optional page)
  "`evil' wrapper around `pdf-view-last-page'."
  (interactive "P")
  (if page
      (pdf-view-goto-page page)
    (pdf-view-last-page)
    (image-eob)))

(defun evil-collection-pdf-view-goto-first-page (&optional page)
  "`evil' wrapper around `pdf-view-first-page'."
  (interactive "P")
  (if page
      (pdf-view-goto-page page)
    (pdf-view-first-page)
    (image-bob)))

(defun evil-collection-pdf-setup ()
  "Set up `evil' bindings for `pdf-view'."
  (evil-collection-inhibit-insert-state 'pdf-view-mode-map)
  (evil-set-initial-state 'pdf-view-mode 'normal)
  (evil-collection-define-key 'normal 'pdf-view-mode-map
    ;; motion
    (kbd "<return>") 'image-next-line
    "j" 'evil-collection-pdf-view-next-line-or-next-page
    "k" 'evil-collection-pdf-view-previous-line-or-previous-page
    (kbd "SPC") 'pdf-view-scroll-up-or-next-page
    (kbd "S-SPC") 'pdf-view-scroll-down-or-previous-page
    (kbd "<delete>") 'pdf-view-scroll-down-or-previous-page
    (kbd "C-f") 'pdf-view-scroll-up-or-next-page
    (kbd "C-b") 'pdf-view-scroll-down-or-previous-page
    "]" 'pdf-view-next-page-command
    "[" 'pdf-view-previous-page-command
    (kbd "C-j") 'pdf-view-next-page-command
    (kbd "C-k") 'pdf-view-previous-page-command
    "gj" 'pdf-view-next-page-command
    "gk" 'pdf-view-previous-page-command
    (kbd "<next>") 'forward-page
    (kbd "<prior>") 'backward-page
    (kbd "<down>") 'evil-collection-pdf-view-next-line-or-next-page
    (kbd "<up>") 'evil-collection-pdf-view-previous-line-or-previous-page
    "gg" 'evil-collection-pdf-view-goto-first-page
    "G" 'evil-collection-pdf-view-goto-page

    ;; mark
    "'" 'pdf-view-jump-to-register
    "m" 'pdf-view-position-to-register

    ;; zoom
    "+" 'pdf-view-enlarge
    "zi" 'pdf-view-enlarge
    "=" 'pdf-view-enlarge
    "-" 'pdf-view-shrink
    "zo" 'pdf-view-shrink		      
    "0" 'pdf-view-scale-reset
    "z0" 'pdf-view-scale-reset

    ;; TODO: Why are those image-* bindings in pdf-tools?
    "a+" 'image-increase-speed
    "a-" 'image-decrease-speed
    "a0" 'image-reset-speed
    "ar" 'image-reverse-speed
    "F" 'image-goto-frame
    "b" 'image-previous-frame
    "f" 'image-next-frame
    "h" 'image-backward-hscroll
    "^" 'image-bol
    "$" 'image-eol
    "l" 'image-forward-hscroll

    "H" 'pdf-view-fit-height-to-window ; evil-image has "H"
    "P" 'pdf-view-fit-page-to-window
    "W" 'pdf-view-fit-width-to-window ; evil-image has "W"

    ;; refresh
    "gr" 'revert-buffer

    (kbd "<C-down-mouse-1>") 'pdf-view-mouse-extend-region
    (kbd "<M-down-mouse-1>") 'pdf-view-mouse-set-region-rectangle
    (kbd "<down-mouse-1>")  'pdf-view-mouse-set-region

    (kbd "C-c C-c") 'docview-mode
    (kbd "C-c <tab>") 'pdf-view-extract-region-image

    "sb" 'pdf-view-set-slice-from-bounding-box
    "sm" 'pdf-view-set-slice-using-mouse
    "sr" 'pdf-view-reset-slice

    ;; goto
    "gl" 'pdf-view-goto-label

    ;; search
    (kbd "M-s o") 'pdf-occur ; TODO: More Evil bindings?

    "/" 'isearch-forward
    "?" 'isearch-backward
    "n" 'isearch-repeat-forward
    "N" 'isearch-repeat-backward

    "zd" 'pdf-view-dark-minor-mode
    "zm" 'pdf-view-midnight-minor-mode
    "zp" 'pdf-view-printer-minor-mode

    "o" 'pdf-outline

    ;; quit
    "q" 'quit-window
    "Q" 'kill-this-buffer
    "ZQ" 'kill-this-buffer
    "ZZ" 'quit-window)

  (evil-collection-define-key 'visual 'pdf-view-mode-map
    "y" 'pdf-view-kill-ring-save)

  (evil-collection-inhibit-insert-state 'pdf-outline-buffer-mode-map)
  (evil-set-initial-state 'pdf-outline-buffer-mode 'normal)
  (evil-collection-define-key 'normal 'pdf-outline-buffer-mode-map
    ;; open
    (kbd "<return>") 'pdf-outline-follow-link-and-quit
    (kbd "S-<return>") 'pdf-outline-follow-link
    (kbd "M-<return>") 'pdf-outline-display-link
    "go" 'pdf-outline-follow-link
    "." 'pdf-outline-move-to-current-page
    (kbd "SPC") 'pdf-outline-select-pdf-window

    "G" 'pdf-outline-end-of-buffer
    "^" 'pdf-outline-up-heading
    "<" 'pdf-outline-up-heading ; TODO: Don't set this by default?

    "zf" 'pdf-outline-follow-mode ; Helm has "C-c C-f" in Emacs state.

    ;; quit
    (kbd "C-w q") 'pdf-outline-quit-and-kill ; TODO: Do we need to set this? I think not.
    "q" 'quit-window
    "ZQ" 'quit-window
    "ZZ" 'pdf-outline-quit-and-kill)

  (evil-collection-inhibit-insert-state 'pdf-occur-buffer-mode-map)
  (evil-set-initial-state 'pdf-occur-buffer-mode 'normal)
  (evil-collection-define-key 'normal 'pdf-occur-buffer-mode-map
    ;; open
    (kbd "<return>") 'pdf-occur-goto-occurrence
    (kbd "S-<return>") 'pdf-occur-view-occurrence
    (kbd "SPC") 'pdf-occur-view-occurrence
    "gd" 'pdf-occur-goto-occurrence
    "gD" 'pdf-occur-view-occurrence

    "A" 'pdf-occur-tablist-gather-documents
    "D" 'pdf-occur-tablist-do-delete

    ;; sort
    "o" 'tabulated-list-sort
    "O" 'tablist-sort ; TODO: Do we need this?

    ;; refresh
    "G" 'tablist-revert

    "K" 'pdf-occur-abort-search

    ;; mark
    "*m" 'tablist-mark-forward
    "m" 'tablist-mark-forward
    "~" 'tablist-toggle-marks
    "u" 'tablist-unmark-forward
    "U" 'tablist-unmark-all-marks
    "*!" 'tablist-unmark-all-marks
    "*c" 'tablist-change-marks
    "*n" 'tablist-mark-items-numeric
    "*r" 'tablist-mark-items-regexp
    "%"  'tablist-mark-items-regexp

    "a" 'tablist-flag-forward

    ;; "f" 'tablist-find-entry ; TODO: Equivalent to 'pdf-occur-goto-occurrence?
    "r" 'pdf-occur-revert-buffer-with-args
    "d" 'tablist-do-kill-lines
    "x" 'pdf-occur-tablist-do-flagged-delete
    (kbd "<delete>") 'tablist-unmark-backward
    (kbd "S-SPC") 'scroll-down-command
    (kbd "<backtab>") 'tablist-backward-column
    (kbd "C-c C-e") 'tablist-export-csv

    [remap evil-first-non-blank] 'tablist-move-to-major-columnj
    [remap evil-next-line] 'tablist-next-line
    [remap evil-previous-line] 'tablist-previous-line

    ;; filter
    ;; TODO: See if overriding "/" is a good idea.
    "/!" 'tablist-negate-filter
    "//" 'tablist-display-filter
    "/=" 'tablist-push-equal-filter
    "/C" 'tablist-clear-filter
    "/D" 'tablist-delete-named-filter
    "/a" 'tablist-push-named-filter
    "/d" 'tablist-deconstruct-named-filter
    "/e" 'tablist-edit-filter
    "/n" 'tablist-push-numeric-filter
    "/p" 'tablist-pop-filter
    "/r" 'tablist-push-regexp-filter
    "/s" 'tablist-name-current-filter
    "/t" 'tablist-toggle-first-filter-logic
    "/z" 'tablist-suspend-filter

    ;; quit
    "q" 'tablist-quit
    "ZQ" 'tablist-quit
    "ZZ" 'tablist-quit))

(provide 'evil-collection-pdf)
;;; evil-collection-pdf.el ends here