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