Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.8 KiB

  1. ;;; ivy-hydra.el --- Additional key bindings for Ivy -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
  4. ;; URL: https://github.com/abo-abo/swiper
  5. ;; Package-Version: 0.13.0
  6. ;; Package-Commit: cd634c6f51458f81898ecf2821ac3169cb65a1eb
  7. ;; Version: 0.13.0
  8. ;; Package-Requires: ((emacs "24.5") (ivy "0.13.0") (hydra "0.15.0"))
  9. ;; Keywords: convenience
  10. ;; This file is part of GNU Emacs.
  11. ;; This file is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; For a full copy of the GNU General Public License
  20. ;; see <https://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; This package provides the `hydra-ivy/body' command, which is a
  23. ;; quasi-prefix map, with many useful bindings. These bindings are
  24. ;; shorter than usual, using mostly unprefixed keys.
  25. ;;; Code:
  26. (require 'ivy)
  27. (require 'hydra)
  28. (defun ivy--matcher-desc ()
  29. "Return description of `ivy--regex-function'."
  30. (let ((cell (assq ivy--regex-function ivy-preferred-re-builders)))
  31. (if cell
  32. (cdr cell)
  33. "other")))
  34. (defhydra hydra-ivy (:hint nil
  35. :color pink)
  36. "
  37. ^ ^ ^ ^ ^ ^ | ^Call^ ^ ^ | ^Cancel^ | ^Options^ | Action _w_/_s_/_a_: %-14s(ivy-action-name)
  38. ^-^-^-^-^-^-+-^-^---------^-^--+-^-^------+-^-^-------+-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------
  39. ^ ^ _k_ ^ ^ | _f_ollow occ_U_r | _i_nsert | _c_: calling %-5s(if ivy-calling \"on\" \"off\") _C_ase-fold: %-10`ivy-case-fold-search
  40. _h_ ^+^ _l_ | _d_one ^ ^ | _o_ops | _M_: matcher %-5s(ivy--matcher-desc)^^^^^^^^^^^^ _T_runcate: %-11`truncate-lines
  41. ^ ^ _j_ ^ ^ | _g_o ^ ^ | ^ ^ | _<_/_>_: shrink/grow^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _D_efinition of this menu
  42. "
  43. ;; arrows
  44. ("h" ivy-beginning-of-buffer)
  45. ("j" ivy-next-line)
  46. ("k" ivy-previous-line)
  47. ("l" ivy-end-of-buffer)
  48. ;; mark
  49. ("m" ivy-mark)
  50. ("u" ivy-unmark)
  51. ("DEL" ivy-unmark-backward)
  52. ("t" ivy-toggle-marks)
  53. ;; actions
  54. ("o" keyboard-escape-quit :exit t)
  55. ("r" ivy-dispatching-done-hydra :exit t)
  56. ("C-g" keyboard-escape-quit :exit t)
  57. ("i" nil)
  58. ("C-o" nil)
  59. ("f" ivy-alt-done :exit nil)
  60. ("C-j" ivy-alt-done :exit nil)
  61. ("d" ivy-done :exit t)
  62. ("g" ivy-call)
  63. ("C-m" ivy-done :exit t)
  64. ("c" ivy-toggle-calling)
  65. ("M" ivy-rotate-preferred-builders)
  66. (">" ivy-minibuffer-grow)
  67. ("<" ivy-minibuffer-shrink)
  68. ("w" ivy-prev-action)
  69. ("s" ivy-next-action)
  70. ("a" (let ((ivy-read-action-function #'ivy-read-action-by-key))
  71. (ivy-read-action)))
  72. ("T" (setq truncate-lines (not truncate-lines)))
  73. ("C" ivy-toggle-case-fold)
  74. ("U" ivy-occur :exit t)
  75. ("D" (ivy-exit-with-action
  76. (lambda (_) (find-function 'hydra-ivy/body)))
  77. :exit t))
  78. (defvar ivy-dispatching-done-columns 2
  79. "Number of columns to use if the hint does not fit on one line.")
  80. (defvar ivy-dispatching-done-idle nil
  81. "When non-nil, the hint will be delayed by this many seconds.")
  82. (defvar ivy-dispatching-done-hydra-exit-keys '(("M-o" nil "back")
  83. ("C-g" nil))
  84. "Keys that can be used to exit `ivy-dispatching-done-hydra'.")
  85. (defun ivy-dispatching-done-hydra ()
  86. "Select one of the available actions and call `ivy-done'."
  87. (interactive)
  88. (let* ((actions (ivy-state-action ivy-last))
  89. (extra-actions ivy-dispatching-done-hydra-exit-keys)
  90. (doc (concat "action: "
  91. (mapconcat
  92. (lambda (x) (format "[%s] %s" (nth 0 x) (nth 2 x)))
  93. (append (cdr actions)
  94. extra-actions) ", ")))
  95. (estimated-len (length doc))
  96. (n-columns (if (> estimated-len (window-width))
  97. ivy-dispatching-done-columns
  98. nil))
  99. (i 0))
  100. (if (null (ivy--actionp actions))
  101. (ivy-done)
  102. (funcall
  103. (eval
  104. `(defhydra ivy-read-action (:color teal :columns ,n-columns :idle ,ivy-dispatching-done-idle)
  105. "action"
  106. ,@(mapcar (lambda (x)
  107. (list (nth 0 x)
  108. `(progn
  109. (setcar (ivy-state-action ivy-last) ,(cl-incf i))
  110. (ivy-done))
  111. (nth 2 x)))
  112. (cdr actions))
  113. ,@extra-actions))))))
  114. (setq ivy-read-action-function (lambda (_) (ivy-dispatching-done-hydra)))
  115. (provide 'ivy-hydra)
  116. ;;; ivy-hydra.el ends here