-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
easy-kill-aj.el
124 lines (110 loc) · 4.58 KB
/
easy-kill-aj.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
;;; easy-kill-aj.el --- ace-jump integration for easy-kill.
;; Author: Akinori MUSHA <[email protected]>
;; URL: https://github.com/knu/easy-kill-extras.el
;; Created: 3 Mar 2015
;; Package-Requires: ((easy-kill "0.9.4") (ace-jump-mode "1.0"))
;; Keywords: killing, convenience
;; Copyright (c) 2015-2016 Akinori MUSHA
;;
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
;; SUCH DAMAGE.
;;; Commentary:
;;
;; This tweak allows using ace-jump commands within
;; easy-mark/easy-kill as measures for selection.
;;
;; This library is part of the easy-kill-extras package and not meant
;; to be loaded standalone.
;;; Code:
(require 'easy-kill)
;;;###autoload
(defcustom easy-kill-ace-jump-enable-p t
"If non-nil, ace-jump commands can be used in easy-kill/easy-mark mode for selection."
:type 'boolean
:group 'easy-kill-extras)
(defvar easy-kill-aj--easy-command nil)
(defvar easy-kill-aj--original-pos nil)
(defun easy-kill-aj--save-state ()
(if (and easy-kill-ace-jump-enable-p
easy-kill-candidate
(eq (easy-kill-get buffer)
(current-buffer)))
(progn
(easy-kill-abort)
(setq easy-kill-aj--easy-command (if (easy-kill-get mark) 'easy-mark 'easy-kill)
easy-kill-aj--original-pos (point)))
(easy-kill-aj--clear-state)))
(defun easy-kill-aj--clear-state ()
(setq easy-kill-aj--easy-command nil
easy-kill-aj--original-pos nil))
(defun easy-kill-aj--after-jump (command orig-pos line-mode)
(let* ((pos (point))
(backward (> orig-pos pos))
(beg orig-pos)
(end (if line-mode pos
(if backward pos (1+ pos)))))
(goto-char orig-pos)
(funcall command)
(if line-mode
(let ((lines (count-lines beg end)))
(easy-kill-thing 'line (if backward (- 1 lines)
(1- lines))))
(easy-kill-adjust-candidate
(if backward 'string-to-char-backward 'string-to-char-forward)
beg end)
(overlay-put easy-kill-candidate 'zap-char (char-after pos))
(overlay-put easy-kill-candidate 'zap-pos pos))
(easy-kill-aj--clear-state)))
(dolist (mode '(ace-jump-char-mode ace-jump-word-mode ace-jump-line-mode))
(eval
`(defadvice ,mode (around easy-kill-extras activate)
(easy-kill-aj--save-state)
(let ((command easy-kill-aj--easy-command)
(orig-pos easy-kill-aj--original-pos))
ad-do-it
(and orig-pos
(/= (point) orig-pos)
;; jumped directly
(easy-kill-aj--after-jump command orig-pos
,(eq mode 'ace-jump-line-mode)))))))
(defadvice ace-jump-move (around easy-kill-extras activate)
(let ((command easy-kill-aj--easy-command)
(orig-pos easy-kill-aj--original-pos)
(line-mode (eq ace-jump-current-mode 'ace-jump-line-mode)))
ad-do-it
(cond
((null command)
;; not called in easy-mark/easy-kill
)
(easy-kill-aj--easy-command
;; ace-jump-done not called
(and (not ace-jump-mode) ;; not in ace-jump-mode (=branch mode)
orig-pos
(/= (point) orig-pos)
(easy-kill-aj--after-jump command orig-pos line-mode)))
(t
(easy-kill-aj--after-jump command orig-pos line-mode)))))
(defadvice ace-jump-done (after easy-kill-extras activate)
(easy-kill-aj--clear-state))
(provide 'easy-kill-aj)
;;; easy-kill-aj.el ends here