4 ;; misc operation for toggle some style
5 ;; such as toggle line number
6 ;; windows layout restore / maximum
9 (defhydra hydra-zoom (global-map "<f2>")
11 ("g" text-scale-increase "in")
12 ("l" text-scale-decrease "out"))
15 (defhydra hydra-misc (:exit t)
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))
25 (defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
26 (global-set-key (kbd "M-c") 'hydra-misc/body)
31 (defhydra hydra-window ()
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")
56 ;(defun pl-last-winner ()
59 ;;(defhydra hydra-launcher (:color blue :columns 2)
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"))
67 ;; define C-space start mark
70 ;;** Example 2: move window splitter
72 (defhydra hydra-splitter (global-map "C-M-s")
74 ("h" hydra-move-splitter-left)
75 ("j" hydra-move-splitter-down)
76 ("k" hydra-move-splitter-up)
77 ("l" hydra-move-splitter-right))
79 ;;** Example 3: jump to error
81 (defhydra hydra-error (global-map "M-g")
83 ("h" first-error "first")
84 ("j" next-error "next")
85 ("k" previous-error "prev")
86 ("v" recenter-top-bottom "recenter")
93 ;;** Example 5: mini-vi
94 (defun hydra-vi/pre ()
95 (set-cursor-color "#e52b50"))
97 (defun hydra-vi/post ()
98 (set-cursor-color "#ffffff"))
103 ; (defhydra hydra-vi (:pre hydra-vi/pre :post hydra-vi/post :color amaranth)
106 ; ("h" backward-char)
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)
118 ;;** Example 6: selective global bind
120 (defhydra hydra-next-error (global-map "C-x")
122 ("`" next-error "next")
123 ("j" next-error "next" :bind nil)
124 ("k" previous-error "previous" :bind nil))
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.
130 ;;** Example 7: toggle with Ruby-style docstring
131 (defvar whitespace-mode nil)
132 (defhydra hydra-toggle (:color pink)
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
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)
147 ;; Recommended binding:
148 ;; (global-set-key (kbd "C-c C-v") 'hydra-toggle/body)
150 ;; Here, using e.g. "_a_" translates to "a" with proper face.
151 ;; More interestingly:
153 ;; "foobar %`abbrev-mode" means roughly (format "foobar %S" abbrev-mode)
155 ;; This means that you actually see the state of the mode that you're changing.
157 ;;** Example 8: the whole menu for `Buffer-menu-mode'
158 (defhydra hydra-buffer-menu (:color pink
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 ^ ^ ^ ^ ^^ ~~ ~~
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)
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)
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)
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)
195 (defhydra hydra-marked-items (dired-mode-map "")
197 Number of marked items: %(length (dired-get-marked-files))
199 ("m" dired-mark "mark")))
201 ;; This results in the following dynamic docstring:
203 ;; (format "Number of marked items: %S\n"
204 ;; (length (dired-get-marked-files)))
206 ;; You can use `format'-style width specs, e.g. % 10(length nil).
208 ;;** Example 10: apropos family
209 (defhydra hydra-apropos (:color blue
213 _d_ocumentation _l_ibrary
214 _v_ariable _u_ser-option
217 ("d" apropos-documentation)
218 ("v" apropos-variable)
219 ("c" apropos-command)
220 ("l" apropos-library)
221 ("u" apropos-user-option)
223 ;; Recommended binding:
224 ;; (global-set-key (kbd "C-c h") 'hydra-apropos/body)
226 ;;** Example 11: rectangle-mark-mode
227 (defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
229 :post (deactivate-mark))
231 ^_k_^ _d_elete _s_tring
233 ^_j_^ _n_ew-copy _r_eset
234 ^^^^ _e_xchange _u_ndo
237 ("h" backward-char nil)
238 ("l" forward-char nil)
239 ("k" previous-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)
246 (rectangle-mark-mode 1)) nil)
247 ("y" yank-rectangle nil)
249 ("s" string-rectangle nil)
250 ("p" kill-rectangle nil)
253 ;; Recommended binding:
254 ;; (global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)
259 (defun hydra-move-splitter-left (arg)
260 "Move window splitter left."
262 (if (let ((windmove-wrap-around))
263 (windmove-find-other-window 'right))
264 (shrink-window-horizontally arg)
265 (enlarge-window-horizontally arg)))
267 (defun hydra-move-splitter-right (arg)
268 "Move window splitter right."
270 (if (let ((windmove-wrap-around))
271 (windmove-find-other-window 'right))
272 (enlarge-window-horizontally arg)
273 (shrink-window-horizontally arg)))
275 (defun hydra-move-splitter-up (arg)
276 "Move window splitter up."
278 (if (let ((windmove-wrap-around))
279 (windmove-find-other-window 'up))
281 (shrink-window arg)))
283 (defun hydra-move-splitter-down (arg)
284 "Move window splitter down."
286 (if (let ((windmove-wrap-around))
287 (windmove-find-other-window 'up))
289 (enlarge-window arg)))
291 (defvar rectangle-mark-mode)
292 (defun hydra-ex-point-mark ()
293 "Exchange point and mark."
295 (if rectangle-mark-mode
296 (exchange-point-and-mark)
298 (rectangle-mark-mode 1)
301 (provide 'init-hydra)
304 ;;** Example 4: toggle rarely used modes
306 ; (defvar whitespace-mode nil)
309 ; (defhydra hydra-toggle-simple (:color blue)
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")))