;;** Example 4: toggle rarely used modes
(when (bound-and-true-p hydra-examples-verbatim)
+ (defvar whitespace-mode nil)
(global-set-key
(kbd "C-c C-v")
- (defhydra hydra-toggle (:color blue)
+ (defhydra hydra-toggle-simple (:color blue)
"toggle"
("a" abbrev-mode "abbrev")
("d" toggle-debug-on-error "debug")
("w" whitespace-mode "whitespace")
("q" nil "cancel"))))
-;; Note that in this case, `defhydra' returns the `hydra-toggle/body'
+;; Note that in this case, `defhydra' returns the `hydra-toggle-simple/body'
;; symbol, which is then passed to `global-set-key'.
;;
;; Another new thing is that both the keymap and the body prefix are
;; bind "C-x j" and "C-x k".
;; You can still "C-x `jjk" though.
-;;* Windmove helpers
+;;** Example 7: toggle with Ruby-style docstring
+(defvar whitespace-mode nil)
+(defhydra hydra-toggle (:color pink)
+ "
+_a_ abbrev-mode: %`abbrev-mode
+_d_ debug-on-error: %`debug-on-error
+_f_ auto-fill-mode: %`auto-fill-function
+_t_ truncate-lines: %`truncate-lines
+_w_ whitespace-mode: %`whitespace-mode
+
+"
+ ("a" abbrev-mode nil)
+ ("d" toggle-debug-on-error nil)
+ ("f" auto-fill-mode nil)
+ ("t" toggle-truncate-lines nil)
+ ("w" whitespace-mode nil)
+ ("q" nil "quit"))
+;; Recommended binding:
+;; (global-set-key (kbd "C-c C-v") 'hydra-toggle/body)
+
+;; Here, using e.g. "_a_" translates to "a" with proper face.
+;; More interestingly:
+;;
+;; "foobar %`abbrev-mode" means roughly (format "foobar %S" abbrev-mode)
+;;
+;; This means that you actually see the state of the mode that you're changing.
+
+;;** Example 8: the whole menu for `Buffer-menu-mode'
+(defhydra hydra-buffer-menu (:color pink
+ :hint nil)
+ "
+^Mark^ ^Unmark^ ^Actions^ ^Search
+^^^^^^^^----------------------------------------------------------------- (__)
+_m_: mark _u_: unmark _x_: execute _R_: re-isearch (oo)
+_s_: save _U_: unmark up _b_: bury _I_: isearch /------\\/
+_d_: delete ^ ^ _g_: refresh _O_: multi-occur / | ||
+_D_: delete up ^ ^ _T_: files only: % -28`Buffer-menu-files-only^^ * /\\---/\\
+_~_: modified ^ ^ ^ ^ ^^ ~~ ~~
+"
+ ("m" Buffer-menu-mark)
+ ("u" Buffer-menu-unmark)
+ ("U" Buffer-menu-backup-unmark)
+ ("d" Buffer-menu-delete)
+ ("D" Buffer-menu-delete-backwards)
+ ("s" Buffer-menu-save)
+ ("~" Buffer-menu-not-modified)
+ ("x" Buffer-menu-execute)
+ ("b" Buffer-menu-bury)
+ ("g" revert-buffer)
+ ("T" Buffer-menu-toggle-files-only)
+ ("O" Buffer-menu-multi-occur :color blue)
+ ("I" Buffer-menu-isearch-buffers :color blue)
+ ("R" Buffer-menu-isearch-buffers-regexp :color blue)
+ ("c" nil "cancel")
+ ("v" Buffer-menu-select "select" :color blue)
+ ("o" Buffer-menu-other-window "other-window" :color blue)
+ ("q" quit-window "quit" :color blue))
+;; Recommended binding:
+;; (define-key Buffer-menu-mode-map "." 'hydra-buffer-menu/body)
+
+;;** Example 9: s-expressions in the docstring
+;; You can inline s-expresssions into the docstring like this:
+(defvar dired-mode-map)
+(declare-function dired-mark "dired")
+(when (bound-and-true-p hydra-examples-verbatim)
+ (require 'dired)
+ (defhydra hydra-marked-items (dired-mode-map "")
+ "
+Number of marked items: %(length (dired-get-marked-files))
+"
+ ("m" dired-mark "mark")))
+
+;; This results in the following dynamic docstring:
+;;
+;; (format "Number of marked items: %S\n"
+;; (length (dired-get-marked-files)))
+;;
+;; You can use `format'-style width specs, e.g. % 10(length nil).
+
+;;** Example 10: apropos family
+(defhydra hydra-apropos (:color blue
+ :hint nil)
+ "
+_a_propos _c_ommand
+_d_ocumentation _l_ibrary
+_v_ariable _u_ser-option
+^ ^ valu_e_"
+ ("a" apropos)
+ ("d" apropos-documentation)
+ ("v" apropos-variable)
+ ("c" apropos-command)
+ ("l" apropos-library)
+ ("u" apropos-user-option)
+ ("e" apropos-value))
+;; Recommended binding:
+;; (global-set-key (kbd "C-c h") 'hydra-apropos/body)
+
+;;** Example 11: rectangle-mark-mode
+(require 'rect)
+(defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
+ :color pink
+ :post (deactivate-mark))
+ "
+ ^_k_^ _d_elete _s_tring
+_h_ _l_ _o_k _y_ank
+ ^_j_^ _n_ew-copy _r_eset
+^^^^ _e_xchange _u_ndo
+^^^^ ^ ^ _p_aste
+"
+ ("h" rectangle-backward-char nil)
+ ("l" rectangle-forward-char nil)
+ ("k" rectangle-previous-line nil)
+ ("j" rectangle-next-line nil)
+ ("e" hydra-ex-point-mark nil)
+ ("n" copy-rectangle-as-kill nil)
+ ("d" delete-rectangle nil)
+ ("r" (if (region-active-p)
+ (deactivate-mark)
+ (rectangle-mark-mode 1)) nil)
+ ("y" yank-rectangle nil)
+ ("u" undo nil)
+ ("s" string-rectangle nil)
+ ("p" kill-rectangle nil)
+ ("o" nil nil))
+
+;; Recommended binding:
+;; (global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)
+
+;;* Helpers
(require 'windmove)
(defun hydra-move-splitter-left (arg)
(shrink-window arg)
(enlarge-window arg)))
-;;* Obsoletes
-(defvar hydra-example-text-scale
- '(("g" text-scale-increase "zoom in")
- ("l" text-scale-decrease "zoom out"))
- "A two-headed hydra for text scale manipulation.")
-(make-obsolete-variable
- 'hydra-example-text-scale
- "Don't use `hydra-example-text-scale', just write your own
-`defhydra' using hydra-examples.el as a template"
- "0.9.0")
-
-(defvar hydra-example-move-window-splitter
- '(("h" hydra-move-splitter-left)
- ("j" hydra-move-splitter-down)
- ("k" hydra-move-splitter-up)
- ("l" hydra-move-splitter-right))
- "A four-headed hydra for the window splitter manipulation.
-Works best if you have not more than 4 windows.")
-(make-obsolete-variable
- 'hydra-example-move-window-splitter
- "Don't use `hydra-example-move-window-splitter', just write your own
-`defhydra' using hydra-examples.el as a template"
- "0.9.0")
-
-(defvar hydra-example-goto-error
- '(("h" first-error "first")
- ("j" next-error "next")
- ("k" previous-error "prev"))
- "A three-headed hydra for jumping between \"errors\".
-Useful for e.g. `occur', `rgrep' and the like.")
-(make-obsolete-variable
- 'hydra-example-goto-error
- "Don't use `hydra-example-goto-error', just write your own
-`defhydra' using hydra-examples.el as a template"
- "0.9.0")
-
-(defvar hydra-example-windmove
- '(("h" windmove-left)
- ("j" windmove-down)
- ("k" windmove-up)
- ("l" windmove-right))
- "A four-headed hydra for `windmove'.")
-(make-obsolete-variable
- 'hydra-example-windmove
- "Don't use `hydra-example-windmove', just write your own
-`defhydra' using hydra-examples.el as a template"
- "0.9.0")
+(defvar rectangle-mark-mode)
+(defun hydra-ex-point-mark ()
+ "Exchange point and mark."
+ (interactive)
+ (if rectangle-mark-mode
+ (rectangle-exchange-point-and-mark)
+ (let ((mk (mark)))
+ (rectangle-mark-mode 1)
+ (goto-char mk))))
(provide 'hydra-examples)
;;; hydra-examples.el ends here