50cc422db461f8f7ed8bb91286dcab5695a8aa93
[dotfiles.git] / emacs.d / config / init-hydra.el
1
2 (require 'hydra)
3
4 ;; misc operation for toggle some style
5 ;; such as toggle line number
6 ;; windows layout restore / maximum
7 ;; 
8
9 ;; misc such as
10 ;; package-list-p, eval-buffer
11
12 (defhydra hydra-helm (global-map "M-c")
13   "Helm"
14   ("j" helm-mini "helm-mini")
15   ("q" nil "quit"))
16 ;;* Examples
17 ;;** Example 1: text scale
18
19   (defhydra hydra-zoom (global-map "<f2>")
20     "zoom"
21     ("g" text-scale-increase "in")
22     ("l" text-scale-decrease "out"))
23
24
25 (global-set-key
26  (kbd "C-M-o")
27 ; (kbd "M-p")
28       (defhydra hydra-window ()
29         "window"
30         ("h" windmove-left)
31         ("j" windmove-down)
32         ("l" windmove-right)
33         ("k" windmove-up)
34         ("v" (lambda ()
35                (interactive)
36                (split-window-right)
37                (windmove-right))
38          "vert")
39         ("x" (lambda ()
40                (interactive)
41                (split-window-below)
42                (windmove-down))
43          "horz")
44         ("o" delete-other-windows "one" :color blue)
45         ("a" ace-window "ace")
46         ("s" ace-swap-window "swap")
47         ("d" ace-delete-window "del")
48         ("i" ace-maximize-window "ace-one" :color blue)
49         ("b" ido-switch-buffer "buf")
50         ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
51         ("i" winner-undo "undo2")
52         ("q" nil "cancel")))
53
54 ;; define C-space start mark 
55
56
57 ;;** Example 2: move window splitter
58
59   (defhydra hydra-splitter (global-map "C-M-s")
60     "splitter"
61     ("h" hydra-move-splitter-left)
62     ("j" hydra-move-splitter-down)
63     ("k" hydra-move-splitter-up)
64     ("l" hydra-move-splitter-right))
65
66 ;;** Example 3: jump to error
67
68   (defhydra hydra-error (global-map "M-g")
69     "goto-error"
70     ("h" first-error "first")
71     ("j" next-error "next")
72     ("k" previous-error "prev")
73     ("v" recenter-top-bottom "recenter")
74     ("q" nil "quit"))
75
76
77
78 ;;** Example 4: toggle rarely used modes
79
80 ;  (defvar whitespace-mode nil)
81 ;  (global-set-key
82 ;   (kbd "C-c C-v")
83 ;   (defhydra hydra-toggle-simple (:color blue)
84 ;     "toggle"
85 ;     ("a" abbrev-mode "abbrev")
86 ;     ("d" toggle-debug-on-error "debug")
87 ;     ("f" auto-fill-mode "fill")
88 ;     ("t" toggle-truncate-lines "truncate")
89 ;     ("w" whitespace-mode "whitespace")
90 ;     ("q" nil "cancel")))
91
92
93
94 ;;** Example 5: mini-vi
95 (defun hydra-vi/pre ()
96   (set-cursor-color "#e52b50"))
97
98 (defun hydra-vi/post ()
99   (set-cursor-color "#ffffff"))
100
101
102   (global-set-key
103    (kbd "C-z")
104    (defhydra hydra-vi (:pre hydra-vi/pre :post hydra-vi/post :color amaranth)
105      "vi"
106      ("l" forward-char)
107      ("h" backward-char)
108      ("j" next-line)
109      ("k" previous-line)
110      ("m" set-mark-command "mark")
111      ("a" move-beginning-of-line "beg")
112      ("e" move-end-of-line "end")
113      ("d" delete-region "del" :color blue)
114      ("y" kill-ring-save "yank" :color blue)
115      ("q" nil "quit")))
116
117
118
119 ;;** Example 6: selective global bind
120
121   (defhydra hydra-next-error (global-map "C-x")
122     "next-error"
123     ("`" next-error "next")
124     ("j" next-error "next" :bind nil)
125     ("k" previous-error "previous" :bind nil))
126
127 ;; This example will bind "C-x `" in `global-map', but it will not
128 ;; bind "C-x j" and "C-x k".
129 ;; You can still "C-x `jjk" though.
130
131 ;;** Example 7: toggle with Ruby-style docstring
132 (defvar whitespace-mode nil)
133 (defhydra hydra-toggle (:color pink)
134   "
135 _a_ abbrev-mode:       %`abbrev-mode
136 _d_ debug-on-error:    %`debug-on-error
137 _f_ auto-fill-mode:    %`auto-fill-function
138 _t_ truncate-lines:    %`truncate-lines
139 _w_ whitespace-mode:   %`whitespace-mode
140
141 "
142   ("a" abbrev-mode nil)
143   ("d" toggle-debug-on-error nil)
144   ("f" auto-fill-mode nil)
145   ("t" toggle-truncate-lines nil)
146   ("w" whitespace-mode nil)
147   ("q" nil "quit"))
148 ;; Recommended binding:
149 ;; (global-set-key (kbd "C-c C-v") 'hydra-toggle/body)
150
151 ;; Here, using e.g. "_a_" translates to "a" with proper face.
152 ;; More interestingly:
153 ;;
154 ;;     "foobar %`abbrev-mode" means roughly (format "foobar %S" abbrev-mode)
155 ;;
156 ;; This means that you actually see the state of the mode that you're changing.
157
158 ;;** Example 8: the whole menu for `Buffer-menu-mode'
159 (defhydra hydra-buffer-menu (:color pink
160                              :hint nil)
161   "
162 ^Mark^             ^Unmark^           ^Actions^          ^Search
163 ^^^^^^^^-----------------------------------------------------------------                        (__)
164 _m_: mark          _u_: unmark        _x_: execute       _R_: re-isearch                         (oo)
165 _s_: save          _U_: unmark up     _b_: bury          _I_: isearch                      /------\\/
166 _d_: delete        ^ ^                _g_: refresh       _O_: multi-occur                 / |    ||
167 _D_: delete up     ^ ^                _T_: files only: % -28`Buffer-menu-files-only^^    *  /\\---/\\
168 _~_: modified      ^ ^                ^ ^                ^^                                 ~~   ~~
169 "
170   ("m" Buffer-menu-mark)
171   ("u" Buffer-menu-unmark)
172   ("U" Buffer-menu-backup-unmark)
173   ("d" Buffer-menu-delete)
174   ("D" Buffer-menu-delete-backwards)
175   ("s" Buffer-menu-save)
176   ("~" Buffer-menu-not-modified)
177   ("x" Buffer-menu-execute)
178   ("b" Buffer-menu-bury)
179   ("g" revert-buffer)
180   ("T" Buffer-menu-toggle-files-only)
181   ("O" Buffer-menu-multi-occur :color blue)
182   ("I" Buffer-menu-isearch-buffers :color blue)
183   ("R" Buffer-menu-isearch-buffers-regexp :color blue)
184   ("c" nil "cancel")
185   ("v" Buffer-menu-select "select" :color blue)
186   ("o" Buffer-menu-other-window "other-window" :color blue)
187   ("q" quit-window "quit" :color blue))
188 ;; Recommended binding:
189 ;; (define-key Buffer-menu-mode-map "." 'hydra-buffer-menu/body)
190
191 ;;** Example 9: s-expressions in the docstring
192 ;; You can inline s-expresssions into the docstring like this:
193 (defvar dired-mode-map)
194 (when (bound-and-true-p hydra-examples-verbatim)
195   (require 'dired)
196   (defhydra hydra-marked-items (dired-mode-map "")
197     "
198 Number of marked items: %(length (dired-get-marked-files))
199 "
200     ("m" dired-mark "mark")))
201
202 ;; This results in the following dynamic docstring:
203 ;;
204 ;;     (format "Number of marked items: %S\n"
205 ;;             (length (dired-get-marked-files)))
206 ;;
207 ;; You can use `format'-style width specs, e.g. % 10(length nil).
208
209 ;;** Example 10: apropos family
210 (defhydra hydra-apropos (:color blue
211                          :hint nil)
212   "
213 _a_propos        _c_ommand
214 _d_ocumentation  _l_ibrary
215 _v_ariable       _u_ser-option
216 ^ ^          valu_e_"
217   ("a" apropos)
218   ("d" apropos-documentation)
219   ("v" apropos-variable)
220   ("c" apropos-command)
221   ("l" apropos-library)
222   ("u" apropos-user-option)
223   ("e" apropos-value))
224 ;; Recommended binding:
225 ;; (global-set-key (kbd "C-c h") 'hydra-apropos/body)
226
227 ;;** Example 11: rectangle-mark-mode
228 (defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
229                            :color pink
230                            :post (deactivate-mark))
231   "
232   ^_k_^     _d_elete    _s_tring
233 _h_   _l_   _o_k        _y_ank
234   ^_j_^     _n_ew-copy  _r_eset
235 ^^^^        _e_xchange  _u_ndo
236 ^^^^        ^ ^         _p_aste
237 "
238   ("h" backward-char nil)
239   ("l" forward-char nil)
240   ("k" previous-line nil)
241   ("j" next-line nil)
242   ("e" hydra-ex-point-mark nil)
243   ("n" copy-rectangle-as-kill nil)
244   ("d" delete-rectangle nil)
245   ("r" (if (region-active-p)
246            (deactivate-mark)
247          (rectangle-mark-mode 1)) nil)
248   ("y" yank-rectangle nil)
249   ("u" undo nil)
250   ("s" string-rectangle nil)
251   ("p" kill-rectangle nil)
252   ("o" nil nil))
253
254 ;; Recommended binding:
255 ;; (global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)
256
257 ;;* Helpers
258 (require 'windmove)
259
260 (defun hydra-move-splitter-left (arg)
261   "Move window splitter left."
262   (interactive "p")
263   (if (let ((windmove-wrap-around))
264         (windmove-find-other-window 'right))
265       (shrink-window-horizontally arg)
266     (enlarge-window-horizontally arg)))
267
268 (defun hydra-move-splitter-right (arg)
269   "Move window splitter right."
270   (interactive "p")
271   (if (let ((windmove-wrap-around))
272         (windmove-find-other-window 'right))
273       (enlarge-window-horizontally arg)
274     (shrink-window-horizontally arg)))
275
276 (defun hydra-move-splitter-up (arg)
277   "Move window splitter up."
278   (interactive "p")
279   (if (let ((windmove-wrap-around))
280         (windmove-find-other-window 'up))
281       (enlarge-window arg)
282     (shrink-window arg)))
283
284 (defun hydra-move-splitter-down (arg)
285   "Move window splitter down."
286   (interactive "p")
287   (if (let ((windmove-wrap-around))
288         (windmove-find-other-window 'up))
289       (shrink-window arg)
290     (enlarge-window arg)))
291
292 (defvar rectangle-mark-mode)
293 (defun hydra-ex-point-mark ()
294   "Exchange point and mark."
295   (interactive)
296   (if rectangle-mark-mode
297       (exchange-point-and-mark)
298     (let ((mk (mark)))
299       (rectangle-mark-mode 1)
300       (goto-char mk))))
301
302 (provide 'init-hydra)