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