Klimi's new dotfiles with stow.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

393 wiersze
13 KiB

5 lat temu
  1. ;;; hydra-examples.el --- Some applications for Hydra
  2. ;; Copyright (C) 2015 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; These are the sample Hydras.
  18. ;;
  19. ;; If you want to use them plainly, set `hydra-examples-verbatim' to t
  20. ;; before requiring this file. But it's probably better to only look
  21. ;; at them and use them as templates for building your own.
  22. ;;; Code:
  23. (require 'hydra)
  24. ;;* Examples
  25. ;;** Example 1: text scale
  26. (when (bound-and-true-p hydra-examples-verbatim)
  27. (defhydra hydra-zoom (global-map "<f2>")
  28. "zoom"
  29. ("g" text-scale-increase "in")
  30. ("l" text-scale-decrease "out")))
  31. ;; This example generates three commands:
  32. ;;
  33. ;; `hydra-zoom/text-scale-increase'
  34. ;; `hydra-zoom/text-scale-decrease'
  35. ;; `hydra-zoom/body'
  36. ;;
  37. ;; In addition, two of them are bound like this:
  38. ;;
  39. ;; (global-set-key (kbd "<f2> g") 'hydra-zoom/text-scale-increase)
  40. ;; (global-set-key (kbd "<f2> l") 'hydra-zoom/text-scale-decrease)
  41. ;;
  42. ;; Note that you can substitute `global-map' with e.g. `emacs-lisp-mode-map' if you need.
  43. ;; The functions generated will be the same, except the binding code will change to:
  44. ;;
  45. ;; (define-key emacs-lisp-mode-map [f2 103]
  46. ;; (function hydra-zoom/text-scale-increase))
  47. ;; (define-key emacs-lisp-mode-map [f2 108]
  48. ;; (function hydra-zoom/text-scale-decrease))
  49. ;;** Example 2: move window splitter
  50. (when (bound-and-true-p hydra-examples-verbatim)
  51. (defhydra hydra-splitter (global-map "C-M-s")
  52. "splitter"
  53. ("h" hydra-move-splitter-left)
  54. ("j" hydra-move-splitter-down)
  55. ("k" hydra-move-splitter-up)
  56. ("l" hydra-move-splitter-right)))
  57. ;;** Example 3: jump to error
  58. (when (bound-and-true-p hydra-examples-verbatim)
  59. (defhydra hydra-error (global-map "M-g")
  60. "goto-error"
  61. ("h" first-error "first")
  62. ("j" next-error "next")
  63. ("k" previous-error "prev")
  64. ("v" recenter-top-bottom "recenter")
  65. ("q" nil "quit")))
  66. ;; This example introduces only one new thing: since the command
  67. ;; passed to the "q" head is nil, it will quit the Hydra without doing
  68. ;; anything. Heads that quit the Hydra instead of continuing are
  69. ;; referred to as having blue :color. All the other heads have red
  70. ;; :color, unless other is specified.
  71. ;;** Example 4: toggle rarely used modes
  72. (when (bound-and-true-p hydra-examples-verbatim)
  73. (defvar whitespace-mode nil)
  74. (global-set-key
  75. (kbd "C-c C-v")
  76. (defhydra hydra-toggle-simple (:color blue)
  77. "toggle"
  78. ("a" abbrev-mode "abbrev")
  79. ("d" toggle-debug-on-error "debug")
  80. ("f" auto-fill-mode "fill")
  81. ("t" toggle-truncate-lines "truncate")
  82. ("w" whitespace-mode "whitespace")
  83. ("q" nil "cancel"))))
  84. ;; Note that in this case, `defhydra' returns the `hydra-toggle-simple/body'
  85. ;; symbol, which is then passed to `global-set-key'.
  86. ;;
  87. ;; Another new thing is that both the keymap and the body prefix are
  88. ;; skipped. This means that `defhydra' will bind nothing - that's why
  89. ;; `global-set-key' is necessary.
  90. ;;
  91. ;; One more new thing is that you can assign a :color to the body. All
  92. ;; heads will inherit this color. The code above is very much equivalent to:
  93. ;;
  94. ;; (global-set-key (kbd "C-c C-v a") 'abbrev-mode)
  95. ;; (global-set-key (kbd "C-c C-v d") 'toggle-debug-on-error)
  96. ;;
  97. ;; The differences are:
  98. ;;
  99. ;; * You get a hint immediately after "C-c C-v"
  100. ;; * You can cancel and call a command immediately, e.g. "C-c C-v C-n"
  101. ;; is equivalent to "C-n" with Hydra approach, while it will error
  102. ;; that "C-c C-v C-n" isn't bound with the usual approach.
  103. ;;** Example 5: mini-vi
  104. (defun hydra-vi/pre ()
  105. (set-cursor-color "#e52b50"))
  106. (defun hydra-vi/post ()
  107. (set-cursor-color "#ffffff"))
  108. (when (bound-and-true-p hydra-examples-verbatim)
  109. (global-set-key
  110. (kbd "C-z")
  111. (defhydra hydra-vi (:pre hydra-vi/pre :post hydra-vi/post :color amaranth)
  112. "vi"
  113. ("l" forward-char)
  114. ("h" backward-char)
  115. ("j" next-line)
  116. ("k" previous-line)
  117. ("m" set-mark-command "mark")
  118. ("a" move-beginning-of-line "beg")
  119. ("e" move-end-of-line "end")
  120. ("d" delete-region "del" :color blue)
  121. ("y" kill-ring-save "yank" :color blue)
  122. ("q" nil "quit")))
  123. (hydra-set-property 'hydra-vi :verbosity 1))
  124. ;; This example introduces :color amaranth. It's similar to red,
  125. ;; except while you can quit red with any binding which isn't a Hydra
  126. ;; head, you can quit amaranth only with a blue head. So you can quit
  127. ;; this mode only with "d", "y", "q" or "C-g".
  128. ;;
  129. ;; Another novelty are the :pre and :post handlers. :pre will be
  130. ;; called before each command, while :post will be called when the
  131. ;; Hydra quits. In this case, they're used to override the cursor
  132. ;; color while Hydra is active.
  133. ;;** Example 6: selective global bind
  134. (when (bound-and-true-p hydra-examples-verbatim)
  135. (defhydra hydra-next-error (global-map "C-x")
  136. "next-error"
  137. ("`" next-error "next")
  138. ("j" next-error "next" :bind nil)
  139. ("k" previous-error "previous" :bind nil)))
  140. ;; This example will bind "C-x `" in `global-map', but it will not
  141. ;; bind "C-x j" and "C-x k".
  142. ;; You can still "C-x `jjk" though.
  143. ;;** Example 7: toggle with Ruby-style docstring
  144. (defvar whitespace-mode nil)
  145. (defhydra hydra-toggle (:color pink)
  146. "
  147. _a_ abbrev-mode: %`abbrev-mode
  148. _d_ debug-on-error: %`debug-on-error
  149. _f_ auto-fill-mode: %`auto-fill-function
  150. _t_ truncate-lines: %`truncate-lines
  151. _w_ whitespace-mode: %`whitespace-mode
  152. "
  153. ("a" abbrev-mode nil)
  154. ("d" toggle-debug-on-error nil)
  155. ("f" auto-fill-mode nil)
  156. ("t" toggle-truncate-lines nil)
  157. ("w" whitespace-mode nil)
  158. ("q" nil "quit"))
  159. ;; Recommended binding:
  160. ;; (global-set-key (kbd "C-c C-v") 'hydra-toggle/body)
  161. ;; Here, using e.g. "_a_" translates to "a" with proper face.
  162. ;; More interestingly:
  163. ;;
  164. ;; "foobar %`abbrev-mode" means roughly (format "foobar %S" abbrev-mode)
  165. ;;
  166. ;; This means that you actually see the state of the mode that you're changing.
  167. ;;** Example 8: the whole menu for `Buffer-menu-mode'
  168. (defhydra hydra-buffer-menu (:color pink
  169. :hint nil)
  170. "
  171. ^Mark^ ^Unmark^ ^Actions^ ^Search
  172. ^^^^^^^^----------------------------------------------------------------- (__)
  173. _m_: mark _u_: unmark _x_: execute _R_: re-isearch (oo)
  174. _s_: save _U_: unmark up _b_: bury _I_: isearch /------\\/
  175. _d_: delete ^ ^ _g_: refresh _O_: multi-occur / | ||
  176. _D_: delete up ^ ^ _T_: files only: % -28`Buffer-menu-files-only^^ * /\\---/\\
  177. _~_: modified ^ ^ ^ ^ ^^ ~~ ~~
  178. "
  179. ("m" Buffer-menu-mark)
  180. ("u" Buffer-menu-unmark)
  181. ("U" Buffer-menu-backup-unmark)
  182. ("d" Buffer-menu-delete)
  183. ("D" Buffer-menu-delete-backwards)
  184. ("s" Buffer-menu-save)
  185. ("~" Buffer-menu-not-modified)
  186. ("x" Buffer-menu-execute)
  187. ("b" Buffer-menu-bury)
  188. ("g" revert-buffer)
  189. ("T" Buffer-menu-toggle-files-only)
  190. ("O" Buffer-menu-multi-occur :color blue)
  191. ("I" Buffer-menu-isearch-buffers :color blue)
  192. ("R" Buffer-menu-isearch-buffers-regexp :color blue)
  193. ("c" nil "cancel")
  194. ("v" Buffer-menu-select "select" :color blue)
  195. ("o" Buffer-menu-other-window "other-window" :color blue)
  196. ("q" quit-window "quit" :color blue))
  197. ;; Recommended binding:
  198. ;; (define-key Buffer-menu-mode-map "." 'hydra-buffer-menu/body)
  199. ;;** Example 9: s-expressions in the docstring
  200. ;; You can inline s-expresssions into the docstring like this:
  201. (defvar dired-mode-map)
  202. (declare-function dired-mark "dired")
  203. (when (bound-and-true-p hydra-examples-verbatim)
  204. (require 'dired)
  205. (defhydra hydra-marked-items (dired-mode-map "")
  206. "
  207. Number of marked items: %(length (dired-get-marked-files))
  208. "
  209. ("m" dired-mark "mark")))
  210. ;; This results in the following dynamic docstring:
  211. ;;
  212. ;; (format "Number of marked items: %S\n"
  213. ;; (length (dired-get-marked-files)))
  214. ;;
  215. ;; You can use `format'-style width specs, e.g. % 10(length nil).
  216. ;;** Example 10: apropos family
  217. (defhydra hydra-apropos (:color blue
  218. :hint nil)
  219. "
  220. _a_propos _c_ommand
  221. _d_ocumentation _l_ibrary
  222. _v_ariable _u_ser-option
  223. ^ ^ valu_e_"
  224. ("a" apropos)
  225. ("d" apropos-documentation)
  226. ("v" apropos-variable)
  227. ("c" apropos-command)
  228. ("l" apropos-library)
  229. ("u" apropos-user-option)
  230. ("e" apropos-value))
  231. ;; Recommended binding:
  232. ;; (global-set-key (kbd "C-c h") 'hydra-apropos/body)
  233. ;;** Example 11: rectangle-mark-mode
  234. (require 'rect)
  235. (defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
  236. :color pink
  237. :post (deactivate-mark))
  238. "
  239. ^_k_^ _d_elete _s_tring
  240. _h_ _l_ _o_k _y_ank
  241. ^_j_^ _n_ew-copy _r_eset
  242. ^^^^ _e_xchange _u_ndo
  243. ^^^^ ^ ^ _x_kill
  244. "
  245. ("h" rectangle-backward-char nil)
  246. ("l" rectangle-forward-char nil)
  247. ("k" rectangle-previous-line nil)
  248. ("j" rectangle-next-line nil)
  249. ("e" hydra-ex-point-mark nil)
  250. ("n" copy-rectangle-as-kill nil)
  251. ("d" delete-rectangle nil)
  252. ("r" (if (region-active-p)
  253. (deactivate-mark)
  254. (rectangle-mark-mode 1)) nil)
  255. ("y" yank-rectangle nil)
  256. ("u" undo nil)
  257. ("s" string-rectangle nil)
  258. ("x" kill-rectangle nil)
  259. ("o" nil nil))
  260. ;; Recommended binding:
  261. ;; (global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)
  262. ;;** Example 12: org-agenda-view
  263. (defun org-agenda-cts ()
  264. (and (eq major-mode 'org-agenda-mode)
  265. (let ((args (get-text-property
  266. (min (1- (point-max)) (point))
  267. 'org-last-args)))
  268. (nth 2 args))))
  269. (defhydra hydra-org-agenda-view (:hint none)
  270. "
  271. _d_: ?d? day _g_: time grid=?g? _a_: arch-trees
  272. _w_: ?w? week _[_: inactive _A_: arch-files
  273. _t_: ?t? fortnight _f_: follow=?f? _r_: clock report=?r?
  274. _m_: ?m? month _e_: entry text=?e? _D_: include diary=?D?
  275. _y_: ?y? year _q_: quit _L__l__c_: log = ?l?"
  276. ("SPC" org-agenda-reset-view)
  277. ("d" org-agenda-day-view (if (eq 'day (org-agenda-cts)) "[x]" "[ ]"))
  278. ("w" org-agenda-week-view (if (eq 'week (org-agenda-cts)) "[x]" "[ ]"))
  279. ("t" org-agenda-fortnight-view (if (eq 'fortnight (org-agenda-cts)) "[x]" "[ ]"))
  280. ("m" org-agenda-month-view (if (eq 'month (org-agenda-cts)) "[x]" "[ ]"))
  281. ("y" org-agenda-year-view (if (eq 'year (org-agenda-cts)) "[x]" "[ ]"))
  282. ("l" org-agenda-log-mode (format "% -3S" org-agenda-show-log))
  283. ("L" (org-agenda-log-mode '(4)))
  284. ("c" (org-agenda-log-mode 'clockcheck))
  285. ("f" org-agenda-follow-mode (format "% -3S" org-agenda-follow-mode))
  286. ("a" org-agenda-archives-mode)
  287. ("A" (org-agenda-archives-mode 'files))
  288. ("r" org-agenda-clockreport-mode (format "% -3S" org-agenda-clockreport-mode))
  289. ("e" org-agenda-entry-text-mode (format "% -3S" org-agenda-entry-text-mode))
  290. ("g" org-agenda-toggle-time-grid (format "% -3S" org-agenda-use-time-grid))
  291. ("D" org-agenda-toggle-diary (format "% -3S" org-agenda-include-diary))
  292. ("!" org-agenda-toggle-deadlines)
  293. ("[" (let ((org-agenda-include-inactive-timestamps t))
  294. (org-agenda-check-type t 'timeline 'agenda)
  295. (org-agenda-redo)
  296. (message "Display now includes inactive timestamps as well")))
  297. ("q" (message "Abort") :exit t)
  298. ("v" nil))
  299. ;; Recommended binding:
  300. ;; (define-key org-agenda-mode-map "v" 'hydra-org-agenda-view/body)
  301. ;;** Example 13: automatic columns
  302. (defhydra hydra-movement ()
  303. ("j" next-line "down" :column "Vertical")
  304. ("k" previous-line "up")
  305. ("l" forward-char "forward" :column "Horizontal")
  306. ("h" backward-char "back"))
  307. ;;* Helpers
  308. (require 'windmove)
  309. (defun hydra-move-splitter-left (arg)
  310. "Move window splitter left."
  311. (interactive "p")
  312. (if (let ((windmove-wrap-around))
  313. (windmove-find-other-window 'right))
  314. (shrink-window-horizontally arg)
  315. (enlarge-window-horizontally arg)))
  316. (defun hydra-move-splitter-right (arg)
  317. "Move window splitter right."
  318. (interactive "p")
  319. (if (let ((windmove-wrap-around))
  320. (windmove-find-other-window 'right))
  321. (enlarge-window-horizontally arg)
  322. (shrink-window-horizontally arg)))
  323. (defun hydra-move-splitter-up (arg)
  324. "Move window splitter up."
  325. (interactive "p")
  326. (if (let ((windmove-wrap-around))
  327. (windmove-find-other-window 'up))
  328. (enlarge-window arg)
  329. (shrink-window arg)))
  330. (defun hydra-move-splitter-down (arg)
  331. "Move window splitter down."
  332. (interactive "p")
  333. (if (let ((windmove-wrap-around))
  334. (windmove-find-other-window 'up))
  335. (shrink-window arg)
  336. (enlarge-window arg)))
  337. (defvar rectangle-mark-mode)
  338. (defun hydra-ex-point-mark ()
  339. "Exchange point and mark."
  340. (interactive)
  341. (if rectangle-mark-mode
  342. (rectangle-exchange-point-and-mark)
  343. (let ((mk (mark)))
  344. (rectangle-mark-mode 1)
  345. (goto-char mk))))
  346. (provide 'hydra-examples)
  347. ;; Local Variables:
  348. ;; no-byte-compile: t
  349. ;; End:
  350. ;;; hydra-examples.el ends here