]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/hydra/hydra.el
Merge commit '1380f8c1a51f31514125a08f632d017cdfb8e26e' from hydra
[gnu-emacs-elpa] / packages / hydra / hydra.el
index 86f488327d2bc49cc6f19f3fa52ed9d778661a30..f50cbf86f091393b9246b14afc3b6522f4e0eae7 100644 (file)
@@ -5,7 +5,7 @@
 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
 ;; Maintainer: Oleh Krehel <ohwoeowho@gmail.com>
 ;; URL: https://github.com/abo-abo/hydra
-;; Version: 0.13.1
+;; Version: 0.13.2
 ;; Keywords: bindings
 ;; Package-Requires: ((cl-lib "0.5"))
 
 (defvar hydra-curr-foreign-keys nil
   "The current :foreign-keys behavior.")
 
+(defvar hydra-deactivate nil
+  "If a Hydra head sets this to t, exit the Hydra even if the
+  head wasn't designated for exiting.")
+
 (defun hydra-set-transient-map (keymap on-exit &optional foreign-keys)
   "Set KEYMAP to the highest priority.
 
@@ -99,11 +103,13 @@ that isn't in KEYMAP is called:
 nil: deactivate KEYMAP and run the command.
 run: keep KEYMAP and run the command.
 warn: keep KEYMAP and issue a warning instead of running the command."
-  (setq hydra-curr-map keymap)
-  (setq hydra-curr-on-exit on-exit)
-  (setq hydra-curr-foreign-keys foreign-keys)
-  (add-hook 'pre-command-hook 'hydra--clearfun)
-  (internal-push-keymap keymap 'overriding-terminal-local-map))
+  (if hydra-deactivate
+      (hydra-keyboard-quit)
+    (setq hydra-curr-map keymap)
+    (setq hydra-curr-on-exit on-exit)
+    (setq hydra-curr-foreign-keys foreign-keys)
+    (add-hook 'pre-command-hook 'hydra--clearfun)
+    (internal-push-keymap keymap 'overriding-terminal-local-map)))
 
 (defun hydra--clearfun ()
   "Disable the current Hydra unless `this-command' is a head."
@@ -123,17 +129,23 @@ warn: keep KEYMAP and issue a warning instead of running the command."
 (defvar hydra--ignore nil
   "When non-nil, don't call `hydra-curr-on-exit'")
 
+(defvar hydra--input-method-function nil
+  "Store overridden `input-method-function' here.")
+
 (defun hydra-disable ()
   "Disable the current Hydra."
+  (setq hydra-deactivate nil)
   (remove-hook 'pre-command-hook 'hydra--clearfun)
   (dolist (frame (frame-list))
     (with-selected-frame frame
       (when overriding-terminal-local-map
         (internal-pop-keymap hydra-curr-map 'overriding-terminal-local-map)
         (unless hydra--ignore
-          (when hydra--input-method-function
-            (setq input-method-function hydra--input-method-function)
-            (setq hydra--input-method-function nil))
+          (if (fboundp 'remove-function)
+              (remove-function input-method-function #'hydra--imf)
+            (when hydra--input-method-function
+              (setq input-method-function hydra--input-method-function)
+              (setq hydra--input-method-function nil)))
           (when hydra-curr-on-exit
             (let ((on-exit hydra-curr-on-exit))
               (setq hydra-curr-on-exit nil)
@@ -186,6 +198,11 @@ warn: keep KEYMAP and issue a warning instead of running the command."
   "Default `format'-style specifier for _a_  syntax in docstrings.
 When nil, you can specify your own at each location like this: _ 5a_.")
 
+(make-obsolete-variable
+ 'hydra-key-format-spec
+ "Since the docstrings are aligned by hand anyway, this isn't very useful."
+ "0.13.1")
+
 (defface hydra-face-red
     '((t (:foreground "#FF0000" :bold t)))
   "Red Hydra heads don't exit the Hydra.
@@ -209,7 +226,7 @@ Exitable only through a blue head.")
 
 (defface hydra-face-teal
     '((t (:foreground "#367588" :bold t)))
-  "Teal body has blue heads an warns on intercepting non-heads.
+  "Teal body has blue heads and warns on intercepting non-heads.
 Exitable only through a blue head.")
 
 ;;* Fontification
@@ -358,15 +375,16 @@ Return DEFAULT if PROP is not in H."
        ((blue teal) t)
        (t nil)))))
 
-(defvar hydra--input-method-function nil
-  "Store overridden `input-method-function' here.")
+(defalias 'hydra--imf #'list)
 
 (defun hydra-default-pre ()
   "Default setup that happens in each head before :pre."
   (when (eq input-method-function 'key-chord-input-method)
-    (unless hydra--input-method-function
-      (setq hydra--input-method-function input-method-function)
-      (setq input-method-function nil))))
+    (if (fboundp 'add-function)
+        (add-function :override input-method-function #'hydra--imf)
+      (unless hydra--input-method-function
+        (setq hydra--input-method-function input-method-function)
+        (setq input-method-function nil)))))
 
 (defvar hydra-timeout-timer (timer-create)
   "Timer for `hydra-timeout'.")
@@ -374,18 +392,21 @@ Return DEFAULT if PROP is not in H."
 (defvar hydra-message-timer (timer-create)
   "Timer for the hint.")
 
+(defvar hydra--work-around-dedicated t
+  "When non-nil, assume there's no bug in `pop-to-buffer'
+  selecting a dedicated window.")
+
 (defun hydra-keyboard-quit ()
   "Quitting function similar to `keyboard-quit'."
   (interactive)
   (hydra-disable)
   (cancel-timer hydra-timeout-timer)
   (cancel-timer hydra-message-timer)
-  (if hydra-lv
-      (when (window-live-p lv-wnd)
-        (let ((buf (window-buffer lv-wnd)))
-          (delete-window lv-wnd)
-          (kill-buffer buf)))
-    (message ""))
+  (unless (and hydra--ignore
+               (null hydra--work-around-dedicated))
+   (if hydra-lv
+       (lv-delete-window)
+     (message "")))
   nil)
 
 (defun hydra--hint (body heads)
@@ -431,7 +452,7 @@ HEAD's binding is returned as a string with a colored face."
               (run 'pink)
               (t 'red)))))
     (when (and (null (cadr head))
-               (not (eq head-color 'blue)))
+               (not head-exit))
       (hydra--complain "nil cmd can only be blue"))
     (propertize (car head) 'face
                 (cl-case head-color
@@ -467,7 +488,7 @@ The expressions can be auto-expanded according to NAME."
         offset)
     (while (setq start
                  (string-match
-                  "\\(?:%\\( ?-?[0-9]*s?\\)\\(`[a-z-A-Z/0-9]+\\|(\\)\\)\\|\\(?:_\\( ?-?[0-9]*\\)\\([[:alnum:]-~.,;:/|?<>={}*+#]+\\)_\\)"
+                  "\\(?:%\\( ?-?[0-9]*s?\\)\\(`[a-z-A-Z/0-9]+\\|(\\)\\)\\|\\(?:_\\( ?-?[0-9]*?\\)\\([-[:alnum:] ~.,;:/|?<>={}*+#]+?\\)_\\)"
                   docstring start))
       (cond ((eq ?_ (aref (match-string 0 docstring) 0))
              (let* ((key (match-string 4 docstring))
@@ -509,8 +530,9 @@ The expressions can be auto-expanded according to NAME."
 
 (defun hydra--complain (format-string &rest args)
   "Forward to (`message' FORMAT-STRING ARGS) unless `hydra-verbose' is nil."
-  (when hydra-verbose
-    (apply #'warn format-string args)))
+  (if hydra-verbose
+      (apply #'error format-string args)
+    (apply #'message format-string args)))
 
 (defun hydra--doc (body-key body-name heads)
   "Generate a part of Hydra docstring.
@@ -694,9 +716,8 @@ JOINER is a function similar to `concat'."
            strs))
    "\n"))
 
-(defcustom hydra-cell-format "% -20s %% -8`%s"
-  "The default format for docstring cells."
-  :type 'string)
+(defvar hydra-cell-format "% -20s %% -8`%s"
+  "The default format for docstring cells.")
 
 (defun hydra--table (names rows cols &optional cell-formats)
   "Format a `format'-style table from variables in NAMES.
@@ -817,7 +838,7 @@ result of `defhydra'."
          (setq docstring "hydra")))
   (when (keywordp (car body))
     (setq body (cons nil (cons nil body))))
-  (condition-case err
+  (condition-case-unless-debug err
       (let* ((keymap (copy-keymap hydra-base-map))
              (keymap-name (intern (format "%S/keymap" name)))
              (body-name (intern (format "%S/body" name)))
@@ -951,9 +972,7 @@ result of `defhydra'."
                (or body-body-pre body-pre) body-before-exit
                '(setq prefix-arg current-prefix-arg)))))
     (error
-     (if debug-on-error
-         (signal (car err) (cdr err))
-       (message "Error in defhydra %S: %s" name (cdr err)))
+     (hydra--complain "Error in defhydra %S: %s" name (cdr err))
      nil)))
 
 (defmacro defhydradio (name _body &rest heads)
@@ -1017,10 +1036,11 @@ DOC defaults to TOGGLE-NAME split and capitalized."
                    0
                  i)))))
 
-(provide 'hydra)
+;; Local Variables:
+;; outline-regexp: ";;\\([;*]+ [^\s\t\n]\\|###autoload\\)\\|("
+;; indent-tabs-mode: nil
+;; End:
 
-;;; Local Variables:
-;;; outline-regexp: ";;\\*+"
-;;; End:
+(provide 'hydra)
 
 ;;; hydra.el ends here