summaryrefslogtreecommitdiff
path: root/dot_config/emacs/lisp/avy-config.el
blob: 50c14cefecf8318daa079332b82eec0d94b6724f (plain)
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
;;; tweaked from karthink's .emacs.d

(use-package avy
  :ensure t
  :commands (avy-goto-word-1 avy-goto-char-2 avy-goto-char-timer)
  :config
  (setq avy-timeout-seconds 0.4)
  (setq avy-keys '(?a ?r ?s ?t ?g ?m ?n ?e ?i ?o ?'
                      ?q ?w ?f ?p ?b ?j ?l ?u ?y ?\; ?-
                   ?z ?x ?c ?d ?v ?k ?h ?, ?. ?/))

  (setq avy-single-candidate-jump nil)
  (setq avy-dispatch-alist '((?m . avy-action-mark)
                             (?$ . avy-action-ispell)
                             (?z . avy-action-zap-to-char)
                             (?  . avy-action-embark)
                             (?= . avy-action-define)
                             (23 . avy-action-zap-to-char)
                             (?c . avy-action-mark-to-char)
                             (?h . avy-action-helpful)
                             (?x . avy-action-exchange)

                             (11 . avy-action-kill-line)
                             (25 . avy-action-yank-line)

                             (?w . avy-action-easy-kill)
                             (?k . avy-action-kill-stay)
                             (?y . avy-action-yank)
                             (?t . avy-action-teleport)

                             (?W . avy-action-copy-whole-line)
                             (?K . avy-action-kill-whole-line)
                             (?Y . avy-action-yank-whole-line)
                             (?T . avy-action-teleport-whole-line)))

  ;; Improved to include transposition by hylophile
  (defun avy-action-easy-kill (pt)
    (unless (require 'easy-kill nil t)
      (user-error "Easy Kill not found, please install."))
    (cl-letf* ((bounds (cond
                        ((use-region-p)
                         (prog1 (cons (region-beginning) (region-end))
                           (deactivate-mark)))
                        ((bounds-of-thing-at-point 'sexp))
                        (t (cons (point) (point)))))
               (transpose-map
                (define-keymap
                  "M-t" (lambda () (interactive "*")
                          (pcase-let ((`(,beg . ,end) (easy-kill--bounds)))
                            (transpose-regions (car bounds) (cdr bounds) beg end
                                               'leave-markers)))))
               ((symbol-function 'easy-kill-activate-keymap)
                (lambda ()
                  (let ((map (easy-kill-map)))
                    (set-transient-map
                     (make-composed-keymap transpose-map map)
                     (lambda ()
                       ;; Prevent any error from activating the keymap forever.
                       (condition-case err
                           (or (and (not (easy-kill-exit-p this-command))
                                    (or (eq this-command
                                            (lookup-key map (this-single-command-keys)))
                                        (let ((cmd (key-binding
                                                    (this-single-command-keys) nil t)))
                                          (command-remapping cmd nil (list map)))))
                               (ignore
                                (easy-kill-destroy-candidate)
                                (unless (or (easy-kill-get mark) (easy-kill-exit-p this-command))
                                  (easy-kill-save-candidate))))
                         (error (message "%s:%s" this-command (error-message-string err))
                                nil)))
                     (lambda ()
                       (let ((dat (ring-ref avy-ring 0)))
                         (select-frame-set-input-focus
                          (window-frame (cdr dat)))
                         (select-window (cdr dat))
                         (goto-char (car dat)))))))))
      (goto-char pt)
      (easy-kill)))

  (defun avy-action-exchange (pt)
  "Exchange sexp at PT with the one at point."
  (set-mark pt)
  (transpose-sexps 0))

  (defun avy-action-helpful (pt)
    (save-excursion
      (goto-char pt)
      (call-interactively #'display-local-help))
    (select-window
     (cdr (ring-ref avy-ring 0)))
    t)

  (defun avy-action-define (pt)
    (cl-letf (((symbol-function 'keyboard-quit)
            #'abort-recursive-edit))
      (save-excursion
        (goto-char pt)
        (dictionary-search-dwim))
      (select-window
       (cdr (ring-ref avy-ring 0))))
    t)

  (defun avy-action-embark (pt)
    (unwind-protect
        (save-excursion
          (goto-char pt)
          (embark-act))
      (select-window
       (cdr (ring-ref avy-ring 0))))
    t)

  (defun avy-action-kill-line (pt)
    (unwind-protect
        (save-excursion
          (goto-char pt)
          (kill-line))
      (avy-resume))
    (select-window
     (cdr (ring-ref avy-ring 0)))
    t)

  (defun avy-action-copy-whole-line (pt)
    (save-excursion
      (goto-char pt)
      (cl-destructuring-bind (start . end)
          (bounds-of-thing-at-point 'line)
        (copy-region-as-kill start end)))
    (select-window
     (cdr
      (ring-ref avy-ring 0)))
    t)

  (defun avy-action-kill-whole-line (pt)
    (unwind-protect
        (save-excursion
          (goto-char pt)
          (kill-whole-line)
          (avy-resume)))
    (select-window
     (cdr
      (ring-ref avy-ring 0)))
    t)

  (defun avy-action-yank-whole-line (pt)
    (avy-action-copy-whole-line pt)
    (save-excursion (yank))
    t)

  (defun avy-action-teleport-whole-line (pt)
    (avy-action-kill-whole-line pt)
    (save-excursion (yank)) t)

  (defun avy-action-mark-to-char (pt)
    (activate-mark)
    (goto-char pt))

  :bind (("C-M-'"   . avy-resume)
         ("C-'"     . avy-goto-char-this-window)
         ("M-j"     . avy-goto-char-timer)
         ("M-s y"   . avy-copy-line)
         ("M-s M-y" . avy-copy-region)
         ("M-s M-k" . avy-kill-whole-line)
         ("M-s j"   . avy-goto-char-2)
         ("M-s M-p" . avy-goto-line-above)
         ("M-s M-n" . avy-goto-line-below)
         ("M-s M-l" . avy-goto-end-of-line)
         ("M-s C-w" . avy-kill-region)
         ("M-s M-w" . avy-kill-ring-save-region)
         ("M-s t"   . avy-move-line)
         ("M-s M-t" . avy-move-region)
         :map isearch-mode-map
         ("C-'"     . avy-isearch)
         ("M-j"     . avy-isearch)))

(provide 'avy-config)