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.

161 lines
5.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. ;;; ivy-hydra.el --- Additional key bindings for Ivy -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2015-2021 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
  4. ;; URL: https://github.com/abo-abo/swiper
  5. ;; Package-Version: 0.13.4
  6. ;; Package-Commit: 8cf3f1821cbd1c266296bbd5e59582ae6b8b90a6
  7. ;; Version: 0.13.4
  8. ;; Package-Requires: ((emacs "24.5") (ivy "0.13.4") (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. (defun ivy-minibuffer-grow ()
  35. "Grow the minibuffer window by 1 line."
  36. (interactive)
  37. (setq-local max-mini-window-height
  38. (cl-incf ivy-height)))
  39. (defun ivy-minibuffer-shrink ()
  40. "Shrink the minibuffer window by 1 line."
  41. (interactive)
  42. (when (> ivy-height 2)
  43. (setq-local max-mini-window-height
  44. (cl-decf ivy-height))
  45. (window-resize nil -1)))
  46. (defhydra hydra-ivy (:hint nil :color pink)
  47. "
  48. ^ ^ ^ ^ ^ ^ | ^Call^ ^ ^ | ^Cancel^ | ^Options^ | Action _w_/_s_/_a_: %-14s(ivy-action-name)
  49. ^-^-^-^-^-^-+-^-^---------^-^--+-^-^------+-^-^-------+-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------
  50. ^ ^ _k_ ^ ^ | _f_ollow occ_U_r | _i_nsert | _c_: calling %-5s(if ivy-calling \"on\" \"off\") _C_ase-fold: %-10`ivy-case-fold-search
  51. _h_ ^+^ _l_ | _d_one ^ ^ | _o_ops | _M_: matcher %-5s(ivy--matcher-desc)^^^^^^^^^^^^ _T_runcate: %-11`truncate-lines
  52. ^ ^ _j_ ^ ^ | _g_o ^ ^ | ^ ^ | _<_/_>_: shrink/grow^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _D_efinition of this menu
  53. "
  54. ;; arrows
  55. ("h" ivy-beginning-of-buffer)
  56. ("j" ivy-next-line)
  57. ("k" ivy-previous-line)
  58. ("l" ivy-end-of-buffer)
  59. ;; mark
  60. ("m" ivy-mark)
  61. ("u" ivy-unmark)
  62. ("DEL" ivy-unmark-backward)
  63. ("t" ivy-toggle-marks)
  64. ;; actions
  65. ("o" keyboard-escape-quit :exit t)
  66. ("r" ivy-dispatching-done :exit t)
  67. ("C-g" keyboard-escape-quit :exit t)
  68. ("i" nil)
  69. ("C-o" nil)
  70. ("f" ivy-alt-done :exit nil)
  71. ("C-j" ivy-alt-done :exit nil)
  72. ("d" ivy-done :exit t)
  73. ("g" ivy-call)
  74. ("C-m" ivy-done :exit t)
  75. ("c" ivy-toggle-calling)
  76. ("M" ivy-rotate-preferred-builders)
  77. (">" ivy-minibuffer-grow)
  78. ("<" ivy-minibuffer-shrink)
  79. ("w" ivy-prev-action)
  80. ("s" ivy-next-action)
  81. ("a" (let ((ivy-read-action-function #'ivy-read-action-by-key))
  82. (ivy-read-action)))
  83. ("T" (setq truncate-lines (not truncate-lines)))
  84. ("C" ivy-toggle-case-fold)
  85. ("U" ivy-occur :exit t)
  86. ("D" (ivy-exit-with-action
  87. (lambda (_) (find-function 'hydra-ivy/body)))
  88. :exit t))
  89. (dolist (sym '(
  90. ;; these cmds have a binding here
  91. ivy-next-action ivy-prev-action
  92. ivy-unmark-backward ivy-toggle-case-fold
  93. ivy-minibuffer-grow ivy-minibuffer-shrink
  94. ivy-rotate-preferred-builders ivy-toggle-calling
  95. ;; no binding
  96. ivy-next-line-or-history ivy-previous-line-or-history
  97. ivy-toggle-fuzzy ivy-yank-symbol
  98. ivy-occur-next-error))
  99. (put sym 'no-counsel-M-x t))
  100. (defvar ivy-dispatching-done-columns 2
  101. "Number of columns to use if the hint does not fit on one line.")
  102. (defvar ivy-dispatching-done-idle nil
  103. "When non-nil, the hint will be delayed by this many seconds.")
  104. (defvar ivy-dispatching-done-hydra-exit-keys '(("M-o" nil "back")
  105. ("C-g" nil))
  106. "Keys that can be used to exit `ivy-hydra-read-action'.")
  107. (defun ivy-hydra-read-action (actions)
  108. "Select one of the available actions and call `ivy-done'."
  109. (let* ((extra-actions ivy-dispatching-done-hydra-exit-keys)
  110. (doc (concat "action: "
  111. (mapconcat
  112. (lambda (x) (format "[%s] %s" (nth 0 x) (nth 2 x)))
  113. (append (cdr actions)
  114. extra-actions) ", ")))
  115. (estimated-len (length doc))
  116. (n-columns (if (> estimated-len (window-width))
  117. ivy-dispatching-done-columns
  118. nil))
  119. (i 0))
  120. (if (null (ivy--actionp actions))
  121. (ivy-done)
  122. (funcall
  123. (eval
  124. `(defhydra ivy-read-action (:color teal :columns ,n-columns :idle ,ivy-dispatching-done-idle)
  125. "action"
  126. ,@(mapcar (lambda (x)
  127. (list (nth 0 x)
  128. `(progn
  129. (let ((prev-idx (car (ivy-state-action ivy-last))))
  130. (setcar (ivy-state-action ivy-last) ,(cl-incf i))
  131. ,@(if (eq ivy-exit 'ivy-dispatching-done)
  132. '((ivy-done))
  133. '((ivy-call)
  134. (setcar (ivy-state-action ivy-last) prev-idx)))))
  135. (nth 2 x)))
  136. (cdr actions))
  137. ,@extra-actions)))
  138. nil)))
  139. (provide 'ivy-hydra)
  140. ;;; ivy-hydra.el ends here