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.

133 lines
4.7 KiB

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