branch: externals/ivy
commit fd4f15753b8e65091c48d37490a36fa887290f1f
Author: Basil L. Contovounesios <[email protected]>
Commit: Basil L. Contovounesios <[email protected]>

    Fix M-x completion-predicate filtering
    
    This decouples keymap definitions from the no-counsel-M-x
    property (via ivy-define-key, which is now marked obsolete).
    
    It also adds completion-predicate/command-modes properties, which
    generalize no-counsel-M-x, in parallel with the latter.
    
    * counsel.el (counsel-irony-callback): No longer declare
    interactive, as this hasn't worked since #1018.
    (counsel-describe-map): Add docstring.
    (counsel-compile-map): Fix docstring.
    (counsel-describe-function, counsel--M-x-externs-predicate)
    (counsel--M-x-make-predicate): Prefer function-get over get for
    function properties.
    (counsel-find-file-map): Move after counsel-find-file actions for
    the benefit of ivy-make-magic-action at load time.  Add docstring.
    (counsel-find-symbol): In addition to counsel-M-x, also remove from
    extra-Ivy M-x completion.
    (counsel--info-lookup-symbol, counsel-git-grep-query-replace)
    (counsel-file-jump-from-find, counsel-up-directory)
    (counsel-down-directory, counsel-find-file-undo)
    (counsel-find-file-from-jump, counsel-cd, counsel-kmacro-kill)
    (counsel-compile-edit-command): Remove from both extra-Ivy M-x and
    counsel-M-x completion.  Most if not all of these commands break
    when called from M-x due to the intervening ivy-read involved.
    (counsel-git-grep-switch-cmd): Remove from extra-Ivy M-x completion.
    (counsel-minibuffer-history): Add minibuffer-mode to command-modes.
    
    * ivy-avy.el (ivy-avy): In addition to counsel-M-x, also remove from
    extra-Ivy M-x completion.
    
    * ivy-hydra.el: Stop setting no-counsel-M-x property on commands
    defined elsewhere; move closer to definition.
    (ivy-minibuffer-grow, ivy-minibuffer-shrink)
    (ivy-hydra--read-action, ivy-hydra--toggle-truncate-lines)
    (ivy-hydra--find-definition): In addition to counsel-M-x, also
    remove from extra-Ivy M-x completion.
    
    * ivy-test.el (ivy-eval): Remove from M-x and counsel-M-x
    completion.
    
    * ivy.el (ivy-switch-buffer, ivy-switch-buffer-other-window)
    (ivy-help): Don't remove from counsel-M-x completion.
    (hydra-ivy/body, ivy-done, ivy-read-action, ivy-dispatching-done)
    (ivy-dispatching-call, ivy-alt-done, ivy-partial-or-done)
    (ivy-immediate-done, ivy-beginning-of-buffer, ivy-end-of-buffer)
    (ivy-scroll-up-command, ivy-scroll-down-command, ivy-next-line)
    (ivy-next-line-or-history, ivy-previous-line)
    (ivy-previous-line-or-history, ivy-toggle-calling, ivy-toggle-ignore)
    (ivy-next-action, ivy-prev-action, ivy-call, ivy-next-line-and-call)
    (ivy-previous-line-and-call, ivy-previous-history-element)
    (ivy-next-history-element, ivy-backward-delete-char, ivy-delete-char)
    (ivy-forward-char, ivy-kill-word, ivy-kill-line, ivy-kill-whole-line)
    (ivy-backward-kill-word, ivy-toggle-regexp-quote, ivy-rotate-sort)
    (ivy-magic-read-file-env, ivy-toggle-case-fold)
    (ivy-switch-buffer-kill, ivy-yank-word, ivy-yank-symbol)
    (ivy-kill-ring-save, ivy-insert-current, ivy-insert-current-full)
    (ivy-rotate-preferred-builders, ivy-toggle-fuzzy)
    (ivy-reverse-i-search-kill, ivy-reverse-i-search)
    (ivy-restrict-to-matches, ivy-occur, ivy-unmark-backward)
    (ivy-toggle-marks): In addition to counsel-M-x, also remove from
    extra-Ivy M-x completion.
    (ivy-occur-toggle-calling, ivy-occur-next-line)
    (ivy-occur-previous-line, ivy-occur-next-error)
    (ivy-occur-delete-candidate, ivy-occur-flush-lines)
    (ivy-occur-revert-buffer, ivy-wgrep-change-to-wgrep-mode)
    (ivy-occur-read-action, ivy-occur-dispatch, ivy-occur-click)
    (ivy-occur-press, ivy-occur-press-and-switch): In addition to
    counsel-M-x, also remove from extra-Occur M-x completion.
    (ivy-partial, ivy-call-and-recenter, ivy-yank-char, ivy-mark)
    (ivy-unmark): Remove from both extra-Ivy M-x and counsel-M-x
    completion.
    (ivy--mouse-done): New function, extracted from...
    (ivy-mouse-done, ivy-mouse-dispatching-done): ...here.  In addition
    to counsel-M-x, also remove from extra-Ivy M-x completion.
    (ivy-hydra-read-action): Autoload without declaring interactive.
    (ivy-define-key): Mark as obsolete.  All uses replaced.
    (ivy--no-M-x): New function.
    (ivy-minibuffer-map, ivy-occur-mode-map): Consistently use kbd,
    except when remapping.
    (ivy-occur-grep-mode-map): Ditto.  Use set-keymap-parent in place of
    copy-keymap.
    (ivy--minibuffer): New variable for identifying Ivy minibuffers.
    (ivy--minibuffer-setup): Set it.
    (ivy--minibuffer-p, ivy--occur-p): New functions.
    (ivy-make-magic-action): Don't generate docstring if corresponding
    action function is not yet defined.  Lift kbd out of closure.  Use
    defalias with uninterned function symbol in place of quoted lambda.
    It prints better in C-h k and doesn't appear in M-x completion.
    (ivy-occur-last): Fix docstring.
    
    * swiper.el (swiper-C-s, swiper-avy, swiper-mc)
    (swiper-toggle-face-matching, swiper--isearch-insert-current)
    (swiper--isearch-kill-ring-save, swiper-isearch-C-r): Remove from
    both extra-Ivy M-x and counsel-M-x completion.
    (swiper-query-replace, swiper-all-query-replace): In addition to
    counsel-M-x, also remove from extra-Ivy M-x completion.
    (swiper-recenter-top-bottom): Remove from extra-Ivy M-x completion.
    (swiper-map): Fix docstring.
---
 counsel.el   |  78 ++++++++-------
 ivy-avy.el   |   7 +-
 ivy-hydra.el |  18 +---
 ivy-test.el  |   3 +-
 ivy.el       | 303 +++++++++++++++++++++++++++++++++++++++--------------------
 swiper.el    |  14 ++-
 6 files changed, 267 insertions(+), 156 deletions(-)

diff --git a/counsel.el b/counsel.el
index 828f0b8c99..b94ee1180a 100644
--- a/counsel.el
+++ b/counsel.el
@@ -423,11 +423,10 @@ Update the minibuffer with the amount of lines collected 
every
 (defun counsel-irony ()
   "Inline C/C++ completion using Irony."
   (interactive)
-  (irony-completion-candidates-async 'counsel-irony-callback))
+  (irony-completion-candidates-async #'counsel-irony-callback))
 
 (defun counsel-irony-callback (candidates)
   "Callback function for Irony to search among CANDIDATES."
-  (interactive)
   (let* ((symbol-bounds (irony-completion-symbol-bounds))
          (beg (car symbol-bounds))
          (end (cdr symbol-bounds))
@@ -455,7 +454,8 @@ Update the minibuffer with the amount of lines collected 
every
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-.") #'counsel-find-symbol)
     (define-key map (kbd "C-,") #'counsel--info-lookup-symbol)
-    map))
+    map)
+  "Keymap for Counsel commands that describe symbols.")
 
 (ivy-set-actions
  'counsel-describe-variable
@@ -471,12 +471,13 @@ Used by commands `counsel-describe-symbol',
   "Jump to the definition of the current symbol."
   (interactive)
   (ivy-exit-with-action #'counsel--find-symbol))
-(function-put #'counsel-find-symbol 'no-counsel-M-x t)
+(ivy--no-M-x #'counsel-find-symbol #'ivy--minibuffer-p)
 
 (defun counsel--info-lookup-symbol ()
   "Lookup the current symbol in the info docs."
   (interactive)
   (ivy-exit-with-action #'counsel-info-lookup-symbol))
+(ivy--no-M-x #'counsel--info-lookup-symbol #'ivy--minibuffer-p)
 
 (defun counsel--push-xref-marker (&optional m)
   "Compatibility shim for `xref-push-marker-stack'."
@@ -599,7 +600,7 @@ to `ivy-highlight-face'."
     (ivy-read "Describe function: " obarray
               :predicate (lambda (sym)
                            (or (fboundp sym)
-                               (get sym 'function-documentation)))
+                               (function-get sym 'function-documentation)))
               :require-match t
               :history 'counsel-describe-symbol-history
               :keymap counsel-describe-map
@@ -921,7 +922,7 @@ packages are, in order of precedence, `amx' and `smex'."
 (defun counsel--M-x-externs-predicate (cand)
   "Return non-nil if `counsel-M-x' should complete CAND.
 CAND is a string returned by `counsel--M-x-externs'."
-  (not (get (intern cand) 'no-counsel-M-x)))
+  (not (function-get (intern cand) 'no-counsel-M-x)))
 
 (defun counsel--M-x-make-predicate ()
   "Return a predicate for `counsel-M-x' in the current buffer."
@@ -929,8 +930,8 @@ CAND is a string returned by `counsel--M-x-externs'."
   (let ((buf (current-buffer)))
     (lambda (sym)
       (and (commandp sym)
-           (not (get sym 'byte-obsolete-info))
-           (not (get sym 'no-counsel-M-x))
+           (not (function-get sym 'byte-obsolete-info))
+           (not (function-get sym 'no-counsel-M-x))
            (cond ((not (bound-and-true-p read-extended-command-predicate)))
                  ((functionp read-extended-command-predicate)
                   (condition-case-unless-debug err
@@ -1751,6 +1752,7 @@ When CMD is non-nil, prompt for a specific \"git grep\" 
command."
   (unless (ivy-state-dynamic-collection ivy-last)
     (setq ivy--all-candidates
           (all-completions "" #'counsel-git-grep-function))))
+(ivy--no-M-x #'counsel-git-grep-switch-cmd #'ivy--minibuffer-p t)
 
 (defun counsel--normalize-grep-match (str)
   ;; Prepend ./ if necessary:
@@ -1808,6 +1810,7 @@ When CMD is non-nil, prompt for a specific \"git grep\" 
command."
                    (find-file file-name)
                    (goto-char (point-min)))
                  (perform-replace from to t t nil))))))))))
+(ivy--no-M-x #'counsel-git-grep-query-replace #'ivy--minibuffer-p)
 
 ;;;; `counsel-git-stash'
 
@@ -1973,20 +1976,12 @@ currently checked out."
 ;;; File
 ;;;; `counsel-find-file'
 
-(defvar counsel-find-file-map
-  (let ((map (make-sparse-keymap)))
-    (define-key map (kbd "C-DEL") #'counsel-up-directory)
-    (define-key map (kbd "C-<backspace>") #'counsel-up-directory)
-    (define-key map (kbd "`") #'counsel-file-jump-from-find)
-    (define-key map (kbd "C-`") (ivy-make-magic-action #'counsel-find-file 
"b"))
-    (define-key map `[remap ,#'undo] #'counsel-find-file-undo)
-    map))
-
 (defun counsel-file-jump-from-find ()
   "Switch to `counsel-file-jump' from `counsel-find-file'."
   (interactive)
   (ivy-quit-and-run
     (counsel-file-jump ivy-text (ivy-state-directory ivy-last))))
+(ivy--no-M-x #'counsel-file-jump-from-find #'ivy--minibuffer-p)
 
 (when (executable-find "git")
   (add-to-list 'ivy-ffap-url-functions 'counsel-github-url-p)
@@ -2083,18 +2078,29 @@ choose between `yes-or-no-p' and `y-or-n-p'; otherwise 
default to
     (when win (with-selected-window win (ivy--cd dir)))))
 
 (ivy-set-actions
- 'counsel-find-file
- '(("j" find-file-other-window "other window")
-   ("f" find-file-other-frame "other frame")
-   ("b" counsel-find-file-cd-bookmark-action "cd bookmark")
-   ("x" counsel-find-file-extern "open externally")
-   ("r" counsel-find-file-as-root "open as root")
-   ("R" find-file-read-only "read only")
-   ("l" find-file-literally "open literally")
-   ("k" counsel-find-file-delete "delete")
-   ("c" counsel-find-file-copy "copy file")
-   ("m" counsel-find-file-move "move or rename")
-   ("d" counsel-find-file-mkdir-action "mkdir")))
+ #'counsel-find-file
+ `(("j" ,#'find-file-other-window "other window")
+   ("f" ,#'find-file-other-frame "other frame")
+   ("b" ,#'counsel-find-file-cd-bookmark-action "cd bookmark")
+   ("x" ,#'counsel-find-file-extern "open externally")
+   ("r" ,#'counsel-find-file-as-root "open as root")
+   ("R" ,#'find-file-read-only "read only")
+   ("l" ,#'find-file-literally "open literally")
+   ("k" ,#'counsel-find-file-delete "delete")
+   ("c" ,#'counsel-find-file-copy "copy file")
+   ("m" ,#'counsel-find-file-move "move or rename")
+   ("d" ,#'counsel-find-file-mkdir-action "mkdir")))
+
+(defvar counsel-find-file-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-DEL") #'counsel-up-directory)
+    (define-key map (kbd "C-<backspace>") #'counsel-up-directory)
+    (define-key map (kbd "`") #'counsel-file-jump-from-find)
+    ;; Needs to come after "b" action is defined.
+    (define-key map (kbd "C-`") (ivy-make-magic-action #'counsel-find-file 
"b"))
+    (define-key map `[remap ,#'undo] #'counsel-find-file-undo)
+    map)
+  "Keymap used during Counsel file name completion.")
 
 (defcustom counsel-find-file-at-point nil
   "When non-nil, add file-at-point to the list of candidates."
@@ -2338,11 +2344,13 @@ See variable `counsel-up-directory-level'."
         (ivy--cd up-dir)
         (setf (ivy-state-preselect ivy-last)
               (file-name-as-directory (file-name-nondirectory cur-dir)))))))
+(ivy--no-M-x #'counsel-up-directory #'ivy--minibuffer-p)
 
 (defun counsel-down-directory ()
   "Descend into the current directory."
   (interactive)
   (ivy--directory-enter))
+(ivy--no-M-x #'counsel-down-directory #'ivy--minibuffer-p)
 
 (defun counsel-find-file-undo ()
   (interactive)
@@ -2353,6 +2361,7 @@ See variable `counsel-up-directory-level'."
         (when dir
           (ivy--cd dir)))
     (undo)))
+(ivy--no-M-x #'counsel-find-file-undo #'ivy--minibuffer-p)
 
 (defun counsel-at-git-issue-p ()
   "When point is at an issue in a Git-versioned file, return the issue string."
@@ -3049,6 +3058,7 @@ FZF-PROMPT, if non-nil, is passed as `ivy-read' prompt 
argument."
   (interactive)
   (ivy-quit-and-run
     (counsel-find-file ivy-text (ivy-state-directory ivy-last))))
+(ivy--no-M-x #'counsel-find-file-from-jump #'ivy--minibuffer-p)
 
 ;;;###autoload
 (defun counsel-file-jump (&optional initial-input initial-directory)
@@ -3289,6 +3299,7 @@ Works for `counsel-git-grep', `counsel-ag', etc."
          (new-dir (counsel-read-directory-name "cd: " def-dir)))
     (ivy-quit-and-run
       (funcall (ivy-state-caller ivy-last) input new-dir))))
+(ivy--no-M-x #'counsel-cd #'ivy--minibuffer-p)
 
 (defun counsel--grep-smart-case-flag ()
   (if (ivy--case-fold-p ivy-text)
@@ -5096,6 +5107,7 @@ An extra action allows to switch to the process buffer."
                         (delete-minibuffer-contents)
                         (insert (substring-no-properties (car x))))
               :caller 'counsel-minibuffer-history)))
+(ivy--no-M-x #'counsel-minibuffer-history '(minibuffer-mode) t)
 
 ;;;; `counsel-esh-history'
 
@@ -5860,8 +5872,6 @@ You can insert or kill the name of the selected font."
     (define-key map (kbd "C-k") #'counsel-kmacro-kill)
     map))
 
-;; Avoid (declare (modes ...)) warnings in Emacs < 28.
-(function-put #'counsel-kmacro-kill 'command-modes '(minibuffer-mode))
 (defun counsel-kmacro-kill ()
   "Kill the line, or delete the currently selected keyboard macro."
   (interactive)
@@ -5874,6 +5884,7 @@ You can insert or kill the name of the selected font."
       (ivy-state-current ivy-last)
       (ivy-state-collection ivy-last)))
     (ivy--kill-current-candidate)))
+(ivy--no-M-x #'counsel-kmacro-kill #'ivy--minibuffer-p)
 
 (defvar kmacro-counter)
 (defvar kmacro-counter-format-start)
@@ -7006,15 +7017,16 @@ handling for the `counsel-compile' metadata."
     (insert (substring-no-properties
              cmd 0 (and (get-text-property 0 'cmd cmd)
                         (next-single-property-change 0 'cmd cmd))))))
+(ivy--no-M-x #'counsel-compile-edit-command #'ivy--minibuffer-p)
 
-;; Currently the only thing we do is override ivy's default insert
+;; Currently the only thing we do is override Ivy's default insert
 ;; operation which doesn't include the metadata we want.
 (defvar counsel-compile-map
   (let ((map (make-sparse-keymap)))
     (define-key map `[remap ,#'ivy-insert-current]
                 #'counsel-compile-edit-command)
     map)
-  "Additional ivy keybindings during command selection.")
+  "Additional Ivy keybindings during command selection.")
 
 ;;;###autoload
 (defun counsel-compile (&optional dir)
diff --git a/ivy-avy.el b/ivy-avy.el
index 9585f015de..fc86a0879a 100644
--- a/ivy-avy.el
+++ b/ivy-avy.el
@@ -106,10 +106,11 @@
       (while (eq (setq res (avy-process (ivy-avy--candidates))) t))
       (when res
         (ivy-avy--action res)))))
+(ivy--no-M-x #'ivy-avy #'ivy--minibuffer-p)
 
-(function-put #'ivy-avy 'no-counsel-M-x t)
-(unless (lookup-key ivy-minibuffer-map (kbd "C-'"))
-  (define-key ivy-minibuffer-map (kbd "C-'") 'ivy-avy))
+(let ((key (kbd "C-'")))
+  (unless (lookup-key ivy-minibuffer-map key)
+    (define-key ivy-minibuffer-map key #'ivy-avy)))
 (add-to-list 'avy-styles-alist `(ivy-avy . ,ivy-avy-style))
 
 (provide 'ivy-avy)
diff --git a/ivy-hydra.el b/ivy-hydra.el
index dd58a489fb..59f7ff55e5 100644
--- a/ivy-hydra.el
+++ b/ivy-hydra.el
@@ -47,6 +47,7 @@
   (interactive)
   (setq-local max-mini-window-height
               (cl-incf ivy-height)))
+(ivy--no-M-x #'ivy-minibuffer-grow #'ivy--minibuffer-p)
 
 (defun ivy-minibuffer-shrink ()
   "Shrink the minibuffer window by 1 line."
@@ -55,6 +56,7 @@
     (setq-local max-mini-window-height
                 (cl-decf ivy-height))
     (window-resize nil -1)))
+(ivy--no-M-x #'ivy-minibuffer-shrink #'ivy--minibuffer-p)
 
 (defun ivy-hydra--read-action ()
   "Read one of the available actions.
@@ -63,17 +65,20 @@ Like `ivy-read-action', but unaffected by
   (interactive)
   (let ((ivy-read-action-function #'ivy-read-action-by-key))
     (ivy-read-action)))
+(ivy--no-M-x #'ivy-hydra--read-action #'ivy--minibuffer-p)
 
 (defun ivy-hydra--toggle-truncate-lines ()
   "Toggle `truncate-lines'."
   (interactive)
   (setq truncate-lines (not truncate-lines)))
+(ivy--no-M-x #'ivy-hydra--toggle-truncate-lines #'ivy--minibuffer-p)
 
 (defun ivy-hydra--find-definition ()
   "Find the definition of `hydra-ivy'."
   (interactive)
   (ivy-exit-with-action
    (lambda (_) (find-function #'hydra-ivy/body))))
+(ivy--no-M-x #'ivy-hydra--find-definition #'ivy--minibuffer-p)
 
 (defhydra hydra-ivy (:hint nil :color pink)
   "
@@ -115,19 +120,6 @@ _h_ ^+^ _l_ | _d_one      ^ ^  | _o_ops   | _M_: matcher 
%-5s(ivy--matcher-desc)
   ("C" ivy-toggle-case-fold)
   ("U" ivy-occur :exit t)
   ("D" ivy-hydra--find-definition :exit t))
-(dolist (cmd '(;; These commands have a binding here.
-               ivy-hydra--find-definition
-               ivy-hydra--read-action
-               ivy-hydra--toggle-truncate-lines
-               ivy-next-action ivy-prev-action
-               ivy-unmark-backward ivy-toggle-case-fold
-               ivy-minibuffer-grow ivy-minibuffer-shrink
-               ivy-rotate-preferred-builders ivy-toggle-calling
-               ;; No binding.
-               ivy-next-line-or-history ivy-previous-line-or-history
-               ivy-toggle-fuzzy ivy-yank-symbol
-               ivy-occur-next-error))
-  (function-put cmd 'no-counsel-M-x t))
 
 (defvar ivy-dispatching-done-columns 2
   "Number of columns to use if the hint does not fit on one line.")
diff --git a/ivy-test.el b/ivy-test.el
index 6e0a4dfd16..12bfeb0112 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -79,8 +79,9 @@ Since `execute-kbd-macro' doesn't pick up a let-bound 
`default-directory'.")
   (interactive)
   (let ((default-directory (or ivy-eval-dir default-directory)))
     (setq ivy-result (eval ivy-expr))))
+(ivy--no-M-x #'ivy-eval #'ignore)
 
-(global-set-key (kbd "C-c e") 'ivy-eval)
+(global-set-key (kbd "C-c e") #'ivy-eval)
 
 (defvar ivy-test-inhibit-message t)
 
diff --git a/ivy.el b/ivy.el
index 7dff3efc6a..329732b29b 100644
--- a/ivy.el
+++ b/ivy.el
@@ -313,79 +313,89 @@ action functions.")
 
 ;;; Keymap
 
-(autoload 'minibuffer-keyboard-quit "delsel" nil t)
-(autoload 'hydra-ivy/body "ivy-hydra" nil t)
-(autoload 'ivy-hydra-read-action "ivy-hydra" nil t)
-
 (defun ivy-define-key (keymap key def)
   "Forward to (`define-key' KEYMAP KEY DEF).
 Remove DEF from `counsel-M-x' list."
+  (declare (obsolete define-key "0.16.0"))
   (function-put def 'no-counsel-M-x t)
   (define-key keymap key def))
 
+(defun ivy--no-M-x (cmd pred &optional pred-only)
+  "Remove CMD from \\`M-x' completion.
+PRED is either a `completion-predicate' function or a list of
+`command-modes'.  PRED-ONLY nil also excludes CMD from `counsel-M-x'."
+  ;; Avoid unknown defun property warnings with `declare' in Emacs < 28.
+  ;; Test for list rather than function since the latter may not yet be fbound
+  ;; at load time.
+  (let ((prop (if (listp pred) 'command-modes 'completion-predicate)))
+    (function-put cmd prop pred))
+  (unless pred-only
+    (function-put cmd 'no-counsel-M-x t)))
+
+(autoload 'minibuffer-keyboard-quit "delsel" nil t)
+(autoload 'hydra-ivy/body "ivy-hydra" nil t)
+(ivy--no-M-x #'hydra-ivy/body #'ivy--minibuffer-p)
+
 (defvar ivy-minibuffer-map
   (let ((map (make-sparse-keymap)))
-    (ivy-define-key map (kbd "C-m") #'ivy-done)
-    (define-key map [down-mouse-1] #'ignore)
-    (ivy-define-key map [mouse-1] #'ivy-mouse-done)
-    (ivy-define-key map [mouse-3] #'ivy-mouse-dispatching-done)
-    (ivy-define-key map (kbd "C-M-m") #'ivy-call)
-    (ivy-define-key map (kbd "C-j") #'ivy-alt-done)
-    (ivy-define-key map (kbd "C-M-j") #'ivy-immediate-done)
-    (ivy-define-key map (kbd "TAB") #'ivy-partial-or-done)
-    (ivy-define-key map `[remap ,#'next-line] #'ivy-next-line)
-    (ivy-define-key map `[remap ,#'previous-line] #'ivy-previous-line)
-    (ivy-define-key map (kbd "C-r") #'ivy-reverse-i-search)
+    (define-key map (kbd "C-m") #'ivy-done)
+    (define-key map (kbd "<down-mouse-1>") #'ignore)
+    (define-key map (kbd "<mouse-1>") #'ivy-mouse-done)
+    (define-key map (kbd "<mouse-3>") #'ivy-mouse-dispatching-done)
+    (define-key map (kbd "C-M-m") #'ivy-call)
+    (define-key map (kbd "C-j") #'ivy-alt-done)
+    (define-key map (kbd "C-M-j") #'ivy-immediate-done)
+    (define-key map (kbd "TAB") #'ivy-partial-or-done)
+    (define-key map `[remap ,#'next-line] #'ivy-next-line)
+    (define-key map `[remap ,#'previous-line] #'ivy-previous-line)
+    (define-key map (kbd "C-r") #'ivy-reverse-i-search)
     (define-key map (kbd "SPC") #'self-insert-command)
-    (ivy-define-key map `[remap ,#'delete-backward-char]
-                    #'ivy-backward-delete-char)
-    (ivy-define-key map `[remap ,#'backward-delete-char-untabify]
-                    #'ivy-backward-delete-char)
-    (ivy-define-key map `[remap ,#'backward-kill-word] 
#'ivy-backward-kill-word)
-    (ivy-define-key map `[remap ,#'delete-char] #'ivy-delete-char)
-    (ivy-define-key map `[remap ,#'forward-char] #'ivy-forward-char)
-    (ivy-define-key map (kbd "<right>") #'ivy-forward-char)
-    (ivy-define-key map `[remap ,#'kill-word] #'ivy-kill-word)
-    (ivy-define-key map `[remap ,#'beginning-of-buffer]
-                    #'ivy-beginning-of-buffer)
-    (ivy-define-key map `[remap ,#'end-of-buffer] #'ivy-end-of-buffer)
-    (ivy-define-key map (kbd "M-n") #'ivy-next-history-element)
-    (ivy-define-key map (kbd "M-p") #'ivy-previous-history-element)
+    (define-key map `[remap ,#'delete-backward-char] 
#'ivy-backward-delete-char)
+    (define-key map `[remap ,#'backward-delete-char-untabify]
+                #'ivy-backward-delete-char)
+    (define-key map `[remap ,#'backward-kill-word] #'ivy-backward-kill-word)
+    (define-key map `[remap ,#'delete-char] #'ivy-delete-char)
+    (define-key map `[remap ,#'forward-char] #'ivy-forward-char)
+    (define-key map (kbd "<right>") #'ivy-forward-char)
+    (define-key map `[remap ,#'kill-word] #'ivy-kill-word)
+    (define-key map `[remap ,#'beginning-of-buffer] #'ivy-beginning-of-buffer)
+    (define-key map `[remap ,#'end-of-buffer] #'ivy-end-of-buffer)
+    (define-key map (kbd "M-n") #'ivy-next-history-element)
+    (define-key map (kbd "M-p") #'ivy-previous-history-element)
     (define-key map (kbd "C-g") #'minibuffer-keyboard-quit)
-    (ivy-define-key map `[remap ,#'scroll-up-command] #'ivy-scroll-up-command)
-    (ivy-define-key map `[remap ,#'scroll-down-command]
-                    #'ivy-scroll-down-command)
-    (ivy-define-key map (kbd "<next>") #'ivy-scroll-up-command)
-    (ivy-define-key map (kbd "<prior>") #'ivy-scroll-down-command)
-    (ivy-define-key map (kbd "C-v") #'ivy-scroll-up-command)
-    (ivy-define-key map (kbd "M-v") #'ivy-scroll-down-command)
-    (ivy-define-key map (kbd "C-M-n") #'ivy-next-line-and-call)
-    (ivy-define-key map (kbd "C-M-p") #'ivy-previous-line-and-call)
-    (ivy-define-key map (kbd "M-a") #'ivy-toggle-marks)
-    (ivy-define-key map (kbd "M-r") #'ivy-toggle-regexp-quote)
-    (ivy-define-key map (kbd "M-j") #'ivy-yank-word)
-    (ivy-define-key map (kbd "M-i") #'ivy-insert-current)
-    (ivy-define-key map (kbd "C-M-y") #'ivy-insert-current-full)
-    (ivy-define-key map (kbd "C-o") #'hydra-ivy/body)
-    (ivy-define-key map (kbd "M-o") #'ivy-dispatching-done)
-    (ivy-define-key map (kbd "C-M-o") #'ivy-dispatching-call)
-    (ivy-define-key map `[remap ,#'kill-line] #'ivy-kill-line)
-    (ivy-define-key map `[remap ,#'kill-whole-line] #'ivy-kill-whole-line)
-    (ivy-define-key map (kbd "S-SPC") #'ivy-restrict-to-matches)
-    (ivy-define-key map `[remap ,#'kill-ring-save] #'ivy-kill-ring-save)
-    (ivy-define-key map (kbd "C-M-a") #'ivy-read-action)
-    (ivy-define-key map (kbd "C-c C-o") #'ivy-occur)
-    (ivy-define-key map (kbd "C-c C-a") #'ivy-toggle-ignore)
-    (ivy-define-key map (kbd "C-c C-s") #'ivy-rotate-sort)
-    (ivy-define-key map `[remap ,#'describe-mode] #'ivy-help)
-    (ivy-define-key map "$" #'ivy-magic-read-file-env)
+    (define-key map `[remap ,#'scroll-up-command] #'ivy-scroll-up-command)
+    (define-key map `[remap ,#'scroll-down-command] #'ivy-scroll-down-command)
+    (define-key map (kbd "<next>") #'ivy-scroll-up-command)
+    (define-key map (kbd "<prior>") #'ivy-scroll-down-command)
+    (define-key map (kbd "C-v") #'ivy-scroll-up-command)
+    (define-key map (kbd "M-v") #'ivy-scroll-down-command)
+    (define-key map (kbd "C-M-n") #'ivy-next-line-and-call)
+    (define-key map (kbd "C-M-p") #'ivy-previous-line-and-call)
+    (define-key map (kbd "M-a") #'ivy-toggle-marks)
+    (define-key map (kbd "M-r") #'ivy-toggle-regexp-quote)
+    (define-key map (kbd "M-j") #'ivy-yank-word)
+    (define-key map (kbd "M-i") #'ivy-insert-current)
+    (define-key map (kbd "C-M-y") #'ivy-insert-current-full)
+    (define-key map (kbd "C-o") #'hydra-ivy/body)
+    (define-key map (kbd "M-o") #'ivy-dispatching-done)
+    (define-key map (kbd "C-M-o") #'ivy-dispatching-call)
+    (define-key map `[remap ,#'kill-line] #'ivy-kill-line)
+    (define-key map `[remap ,#'kill-whole-line] #'ivy-kill-whole-line)
+    (define-key map (kbd "S-SPC") #'ivy-restrict-to-matches)
+    (define-key map `[remap ,#'kill-ring-save] #'ivy-kill-ring-save)
+    (define-key map (kbd "C-M-a") #'ivy-read-action)
+    (define-key map (kbd "C-c C-o") #'ivy-occur)
+    (define-key map (kbd "C-c C-a") #'ivy-toggle-ignore)
+    (define-key map (kbd "C-c C-s") #'ivy-rotate-sort)
+    (define-key map `[remap ,#'describe-mode] #'ivy-help)
+    (define-key map (kbd "$") #'ivy-magic-read-file-env)
     map)
   "Keymap used in the minibuffer.")
 
 (defvar ivy-mode-map
   (let ((map (make-sparse-keymap)))
-    (ivy-define-key map `[remap ,#'switch-to-buffer] #'ivy-switch-buffer)
-    (ivy-define-key map `[remap ,#'switch-to-buffer-other-window]
+    (define-key map `[remap ,#'switch-to-buffer] #'ivy-switch-buffer)
+    (define-key map `[remap ,#'switch-to-buffer-other-window]
                     #'ivy-switch-buffer-other-window)
     map)
   "Keymap for `ivy-mode'.")
@@ -753,6 +763,7 @@ candidate, not the prompt."
              (setq ivy--prompt-extra " (match required)")
              (insert ivy-text)
              (ivy--exhibit))))))
+(ivy--no-M-x #'ivy-done #'ivy--minibuffer-p)
 
 (defvar ivy-mouse-1-tooltip
   "Exit the minibuffer with the selected candidate."
@@ -795,21 +806,23 @@ candidate, not the prompt."
         offset)
     nil))
 
-(defun ivy-mouse-done (event)
-  (interactive "@e")
+(defun ivy--mouse-done (event done)
+  "Move to minibuffer mouse EVENT and call DONE."
   (let ((offset (ivy-mouse-offset event)))
     (when offset
       (ivy-next-line offset)
       (ivy--exhibit)
-      (ivy-alt-done))))
+      (funcall done))))
+
+(defun ivy-mouse-done (event)
+  (interactive "@e")
+  (ivy--mouse-done event #'ivy-alt-done))
+(ivy--no-M-x #'ivy-mouse-done #'ivy--minibuffer-p)
 
 (defun ivy-mouse-dispatching-done (event)
   (interactive "@e")
-  (let ((offset (ivy-mouse-offset event)))
-    (when offset
-      (ivy-next-line offset)
-      (ivy--exhibit)
-      (ivy-dispatching-done))))
+  (ivy--mouse-done event #'ivy-dispatching-done))
+(ivy--no-M-x #'ivy-mouse-dispatching-done #'ivy--minibuffer-p)
 
 (defcustom ivy-read-action-format-function 'ivy-read-action-format-default
   "Function used to transform the actions list into a docstring."
@@ -876,12 +889,13 @@ where KEY and DOC are strings."
                               (nreverse cols))
                        "\n"))))
 
+(autoload 'ivy-hydra-read-action "ivy-hydra")
 (defcustom ivy-read-action-function #'ivy-read-action-by-key
   "Function used to read an action."
-  :type '(radio
-          (function-item ivy-read-action-by-key)
-          (function-item ivy-read-action-ivy)
-          (function-item ivy-hydra-read-action)))
+  :type `(radio
+          (function-item ,#'ivy-read-action-by-key)
+          (function-item ,#'ivy-read-action-ivy)
+          (function-item ,#'ivy-hydra-read-action)))
 
 (defun ivy-read-action ()
   "Change the action to one of the available ones.
@@ -894,6 +908,7 @@ selection, non-nil otherwise."
         t
       (let ((ivy--directory ivy--directory))
         (funcall ivy-read-action-function actions)))))
+(ivy--no-M-x #'ivy-read-action #'ivy--minibuffer-p)
 
 (defvar set-message-function)
 
@@ -956,6 +971,7 @@ will be called for each element of this list.")
     (when (ivy-read-action)
       (ivy-done)))
   (ivy-shrink-after-dispatching))
+(ivy--no-M-x #'ivy-dispatching-done #'ivy--minibuffer-p)
 
 (defun ivy-dispatching-call ()
   "Select one of the available actions and call `ivy-call'."
@@ -969,6 +985,7 @@ will be called for each element of this list.")
            (ivy-call))
       (ivy-set-action actions)))
   (ivy-shrink-after-dispatching))
+(ivy--no-M-x #'ivy-dispatching-call #'ivy--minibuffer-p)
 
 (defun ivy-build-tramp-name (x)
   "Reconstruct X into a path.
@@ -1011,6 +1028,7 @@ of exiting.  This function is otherwise like `ivy-done'."
            (ivy--directory-done))
           (t
            (ivy-done)))))
+(ivy--no-M-x #'ivy-alt-done #'ivy--minibuffer-p)
 
 (defun ivy--info-alt-done ()
   (if (member (ivy-state-current ivy-last) '("(./)" "(../)"))
@@ -1162,6 +1180,7 @@ If the text hasn't changed as a result, forward to 
`ivy-alt-done'."
          (when (or (eq this-command last-command)
                    (eq ivy--length 1))
            (ivy-alt-done))))))
+(ivy--no-M-x #'ivy-partial-or-done #'ivy--minibuffer-p)
 
 (defun ivy--partial-cd-for-single-directory ()
   (when (and
@@ -1215,6 +1234,7 @@ If the text hasn't changed as a result, forward to 
`ivy-alt-done'."
          (insert ivy-text)
          (ivy--partial-cd-for-single-directory)
          t)))))
+(ivy--no-M-x #'ivy-partial #'ivy--minibuffer-p)
 
 (defvar ivy-completion-beg nil
   "Completion bounds start.")
@@ -1247,6 +1267,7 @@ If the text hasn't changed as a result, forward to 
`ivy-alt-done'."
   (setq ivy-completion-beg ivy-completion-end)
   (setq ivy-exit 'done)
   (exit-minibuffer))
+(ivy--no-M-x #'ivy-immediate-done #'ivy--minibuffer-p)
 
 (defun ivy--restore-session (&optional session)
   "Resume a recorded completion SESSION, if any exists."
@@ -1320,23 +1341,27 @@ if one exists."
   "Select the first completion candidate."
   (interactive)
   (ivy-set-index 0))
+(ivy--no-M-x #'ivy-beginning-of-buffer #'ivy--minibuffer-p)
 
 (defun ivy-end-of-buffer ()
   "Select the last completion candidate."
   (interactive)
   (ivy-set-index (1- ivy--length)))
+(ivy--no-M-x #'ivy-end-of-buffer #'ivy--minibuffer-p)
 
 (defun ivy-scroll-up-command ()
   "Scroll the candidates upward by the minibuffer height."
   (interactive)
   (ivy-set-index (min (1- (+ ivy--index ivy-height))
                       (1- ivy--length))))
+(ivy--no-M-x #'ivy-scroll-up-command #'ivy--minibuffer-p)
 
 (defun ivy-scroll-down-command ()
   "Scroll the candidates downward by the minibuffer height."
   (interactive)
   (ivy-set-index (max (1+ (- ivy--index ivy-height))
                       0)))
+(ivy--no-M-x #'ivy-scroll-down-command #'ivy--minibuffer-p)
 
 (defun ivy-next-line (&optional arg)
   "Move cursor vertically down ARG candidates."
@@ -1348,6 +1373,7 @@ if one exists."
             (ivy-beginning-of-buffer)
           (ivy-set-index (1- ivy--length)))
       (ivy-set-index index))))
+(ivy--no-M-x #'ivy-next-line #'ivy--minibuffer-p)
 
 (defun ivy-next-line-or-history (&optional arg)
   "Move cursor vertically down ARG candidates.
@@ -1357,6 +1383,7 @@ If the input is empty, select the previous history 
element instead."
     (ivy-next-line arg)
     (when (and (string= ivy-text "") (= ivy--index orig-index))
       (ivy-previous-history-element 1))))
+(ivy--no-M-x #'ivy-next-line-or-history #'ivy--minibuffer-p)
 
 (defun ivy-previous-line (&optional arg)
   "Move cursor vertically up ARG candidates."
@@ -1369,6 +1396,7 @@ If the input is empty, select the previous history 
element instead."
             (ivy-end-of-buffer)
           (ivy-set-index min-index))
       (ivy-set-index index))))
+(ivy--no-M-x #'ivy-previous-line #'ivy--minibuffer-p)
 
 (defun ivy-previous-line-or-history (arg)
   "Move cursor vertically up ARG candidates.
@@ -1378,12 +1406,14 @@ If the input is empty, select the previous history 
element instead."
     (ivy-previous-line arg)
     (when (and (string= ivy-text "") (= ivy--index orig-index))
       (ivy-previous-history-element 1))))
+(ivy--no-M-x #'ivy-previous-line-or-history #'ivy--minibuffer-p)
 
 (defun ivy-toggle-calling ()
   "Flip `ivy-calling'."
   (interactive)
   (when (setq ivy-calling (not ivy-calling))
     (ivy-call)))
+(ivy--no-M-x #'ivy-toggle-calling #'ivy--minibuffer-p)
 
 (defun ivy-toggle-ignore ()
   "Toggle user-configured candidate filtering."
@@ -1395,6 +1425,7 @@ If the input is empty, select the previous history 
element instead."
   (setf (ivy-state-ignore ivy-last) ivy-use-ignore)
   ;; invalidate cache
   (setq ivy--old-cands nil))
+(ivy--no-M-x #'ivy-toggle-ignore #'ivy--minibuffer-p)
 
 (defun ivy--get-action (state)
   "Get the action function from STATE."
@@ -1444,6 +1475,7 @@ If the input is empty, select the previous history 
element instead."
             (when ivy-action-wrap
               (setf (car action) 1))
           (cl-incf (car action)))))))
+(ivy--no-M-x #'ivy-next-action #'ivy--minibuffer-p)
 
 (defun ivy-prev-action ()
   "When the current action is a list, scroll it backwards."
@@ -1454,6 +1486,7 @@ If the input is empty, select the previous history 
element instead."
           (when ivy-action-wrap
             (setf (car action) (1- (length action))))
         (cl-decf (car action))))))
+(ivy--no-M-x #'ivy-prev-action #'ivy--minibuffer-p)
 
 (defun ivy-action-name ()
   "Return the name associated with the current action."
@@ -1561,6 +1594,7 @@ See variable `ivy-recursive-restore' for further 
information."
     (if ivy-inhibit-action
         res
       current)))
+(ivy--no-M-x #'ivy-call #'ivy--minibuffer-p)
 
 (defun ivy-call-and-recenter ()
   "Call action and recenter window according to the selected candidate."
@@ -1568,6 +1602,7 @@ See variable `ivy-recursive-restore' for further 
information."
   (ivy-call)
   (with-ivy-window
     (recenter-top-bottom)))
+(ivy--no-M-x #'ivy-call-and-recenter #'ivy--minibuffer-p)
 
 (defun ivy-next-line-and-call (&optional arg)
   "Move cursor vertically down ARG candidates.
@@ -1576,6 +1611,7 @@ Call the permanent action if possible."
   (ivy-next-line arg)
   (ivy--exhibit)
   (ivy-call))
+(ivy--no-M-x #'ivy-next-line-and-call #'ivy--minibuffer-p)
 
 (defun ivy-previous-line-and-call (&optional arg)
   "Move cursor vertically up ARG candidates.
@@ -1584,6 +1620,7 @@ Call the permanent action if possible."
   (ivy-previous-line arg)
   (ivy--exhibit)
   (ivy-call))
+(ivy--no-M-x #'ivy-previous-line-and-call #'ivy--minibuffer-p)
 
 (defun ivy-previous-history-element (arg)
   "Forward to `previous-history-element' with ARG."
@@ -1592,6 +1629,7 @@ Call the permanent action if possible."
   (ivy--cd-maybe)
   (move-end-of-line 1)
   (ivy--maybe-scroll-history))
+(ivy--no-M-x #'ivy-previous-history-element #'ivy--minibuffer-p)
 
 (defun ivy--insert-symbol-boundaries ()
   (undo-boundary)
@@ -1620,6 +1658,7 @@ Call the permanent action if possible."
   (ivy--cd-maybe)
   (move-end-of-line 1)
   (ivy--maybe-scroll-history))
+(ivy--no-M-x #'ivy-next-history-element #'ivy--minibuffer-p)
 
 (defvar ivy-ffap-url-functions nil
   "List of functions that check if the point is on a URL.")
@@ -1705,24 +1744,28 @@ minibuffer."
       (error
        (when ivy-on-del-error-function
          (funcall ivy-on-del-error-function))))))
+(ivy--no-M-x #'ivy-backward-delete-char #'ivy--minibuffer-p)
 
 (defun ivy-delete-char (arg)
   "Forward to `delete-char' ARG."
   (interactive "p")
   (unless (eolp)
     (delete-char arg)))
+(ivy--no-M-x #'ivy-delete-char #'ivy--minibuffer-p)
 
 (defun ivy-forward-char (arg)
   "Forward to `forward-char' ARG."
   (interactive "p")
   (unless (eolp)
     (forward-char arg)))
+(ivy--no-M-x #'ivy-forward-char #'ivy--minibuffer-p)
 
 (defun ivy-kill-word (arg)
   "Forward to `kill-word' ARG."
   (interactive "p")
   (unless (eolp)
     (kill-word arg)))
+(ivy--no-M-x #'ivy-kill-word #'ivy--minibuffer-p)
 
 (defun ivy-kill-line ()
   "Forward to `kill-line'."
@@ -1732,6 +1775,7 @@ minibuffer."
         (kill-region (minibuffer-prompt-end) (point))
         (setq ivy--old-text (current-kill 0 t)))
     (kill-line)))
+(ivy--no-M-x #'ivy-kill-line #'ivy--minibuffer-p)
 
 (defalias 'ivy--pos-eol
   (if (fboundp 'pos-eol)
@@ -1745,6 +1789,7 @@ minibuffer."
   "Forward to `kill-whole-line'."
   (interactive)
   (kill-region (minibuffer-prompt-end) (ivy--pos-eol)))
+(ivy--no-M-x #'ivy-kill-whole-line #'ivy--minibuffer-p)
 
 (defun ivy-backward-kill-word ()
   "Forward to `backward-kill-word'."
@@ -1760,6 +1805,7 @@ minibuffer."
                             last-command)))
         (forward-word -1)
         (kill-region pt (point))))))
+(ivy--no-M-x #'ivy-backward-kill-word #'ivy--minibuffer-p)
 
 (defvar ivy--regexp-quote #'regexp-quote
   "Store the regexp quoting state.")
@@ -1771,6 +1817,7 @@ minibuffer."
   (cl-rotatef ivy--regex-function ivy--regexp-quote)
   (setq ivy--old-text "")
   (setq ivy-regex (funcall ivy--regex-function ivy-text)))
+(ivy--no-M-x #'ivy-toggle-regexp-quote #'ivy--minibuffer-p)
 
 (defcustom ivy-format-functions-alist
   '((t . ivy-format-function-default))
@@ -1854,6 +1901,7 @@ specified for the current collection in
     (when (consp (cdr cell))
       (setcdr cell (nconc (cddr cell) (list (cadr cell))))
       (ivy--reset-state ivy-last))))
+(ivy--no-M-x #'ivy-rotate-sort #'ivy--minibuffer-p)
 
 (defcustom ivy-index-functions-alist
   '((t . ivy-recompute-index-zero))
@@ -3117,6 +3165,9 @@ tries to ensure that it does not change depending on the 
number of candidates."
   "Minibuffer setting for `truncate-lines'."
   :type 'boolean)
 
+(defvar-local ivy--minibuffer nil
+  "Whether the current buffer is an Ivy minibuffer.")
+
 (defun ivy--minibuffer-setup ()
   "Set up Ivy completion in `minibuffer-setup-hook'."
   ;; Guard for --without-x builds where `mwheel' is not preloaded.
@@ -3142,6 +3193,7 @@ tries to ensure that it does not change depending on the 
number of candidates."
   (add-hook 'minibuffer-exit-hook #'ivy--minibuffer-exit nil t)
   (add-hook 'post-command-hook #'ivy--queue-exhibit nil t)
   (add-hook 'window-size-change-functions #'ivy--window-size-changed nil t)
+  (setq ivy--minibuffer t)
   (let ((hook (ivy-alist-setting ivy-hooks-alist)))
     (when (functionp hook)
       (funcall hook))))
@@ -3410,26 +3462,24 @@ Possible choices are 
`ivy-magic-slash-non-match-cd-selected',
                   path))
         (ivy--cd-maybe))
     (insert last-input-event)))
+(ivy--no-M-x #'ivy-magic-read-file-env #'ivy--minibuffer-p)
 
 (defun ivy-make-magic-action (caller key)
   "Return a command that does the equivalent of `ivy-read-action' and KEY.
 This happens only when the input is empty.
 The intention is to bind the result to keys that are typically
 bound to `self-insert-command'."
-  (let* ((alist (assoc key
-                       (plist-get
-                        ivy--actions-list
-                        caller)))
-         (doc (format "%s (`%S')"
-                      (nth 2 alist)
-                      (nth 1 alist))))
-    `(lambda (&optional arg)
-       ,doc
-       (interactive "p")
-       (if (string= "" ivy-text)
-           (execute-kbd-macro
-            (kbd ,(concat "M-o " key)))
-         (self-insert-command arg)))))
+  (let* ((action (assoc key (plist-get ivy--actions-list caller)))
+         (fn (nth 1 action))
+         (desc (nth 2 action))
+         (keyseq (kbd (concat "M-o " key))))
+    (defalias (make-symbol (format "ivy--magic-%s" caller))
+      (lambda (&optional arg)
+        (interactive "p")
+        (if (string= "" ivy-text)
+            (execute-kbd-macro keyseq)
+          (self-insert-command arg)))
+      (and action (format "Action: %s (`%S')." desc fn)))))
 
 (defcustom ivy-magic-tilde t
   "When non-nil, ~ will move home when selecting files.
@@ -3485,10 +3535,18 @@ The function was added in Emacs 26.1.")
    (ivy--format
     (setq ivy--all-candidates cands))))
 
+;; Perhaps only one of `ivy--completing-p' or `ivy--minibuffer-p' is needed,
+;; but they are not equivalent: the former becomes nil after an error.
+
 (defun ivy--completing-p ()
   "Return non-nil if Ivy is completing in the current buffer."
   (memq #'ivy--queue-exhibit post-command-hook))
 
+(defun ivy--minibuffer-p (_cmd buf)
+  "Return non-nil if BUF is an Ivy minibuffer.
+Intended as a `completion-predicate'."
+  (buffer-local-value 'ivy--minibuffer buf))
+
 (defun ivy--exhibit ()
   "Insert Ivy completions display.
 Should be run in the minibuffer."
@@ -3681,6 +3739,7 @@ In any Ivy completion session, the case folding starts 
with
              (or ivy-case-fold-search-default 'auto)))
   ;; Reset cache so that the candidate list updates.
   (setq ivy--old-re nil))
+(ivy--no-M-x #'ivy-toggle-case-fold #'ivy--minibuffer-p)
 
 (defun ivy--re-filter (filter candidates &optional mkpred)
   "Return all CANDIDATES matching FILTER, or nil on error.
@@ -4689,7 +4748,7 @@ BUFFER may be a string or nil."
 
 (defvar ivy-switch-buffer-map
   (let ((map (make-sparse-keymap)))
-    (ivy-define-key map (kbd "C-k") #'ivy-switch-buffer-kill)
+    (define-key map (kbd "C-k") #'ivy-switch-buffer-kill)
     map))
 
 (defun ivy-switch-buffer-kill ()
@@ -4700,6 +4759,7 @@ Otherwise, forward to `ivy-kill-line'."
       (ivy-kill-line)
     (ivy--kill-buffer-action
      (ivy-state-current ivy-last))))
+(ivy--no-M-x #'ivy-switch-buffer-kill #'ivy--minibuffer-p)
 
 (ivy-set-actions
  'ivy-switch-buffer
@@ -4880,6 +4940,7 @@ If optional ARG is non-nil, pull in the next ARG
 words (previous if ARG is negative)."
   (interactive "p")
   (ivy--yank-by #'forward-word arg))
+(ivy--no-M-x #'ivy-yank-word #'ivy--minibuffer-p)
 
 (defun ivy-yank-symbol (&optional arg)
   "Pull next symbol from buffer into search string.
@@ -4887,6 +4948,7 @@ If optional ARG is non-nil, pull in the next ARG
 symbols (previous if ARG is negative)."
   (interactive "p")
   (ivy--yank-by #'forward-symbol (or arg 1)))
+(ivy--no-M-x #'ivy-yank-symbol #'ivy--minibuffer-p)
 
 (defun ivy-yank-char (&optional arg)
   "Pull next character from buffer into search string.
@@ -4894,6 +4956,7 @@ If optional ARG is non-nil, pull in the next ARG
 characters (previous if ARG is negative)."
   (interactive "p")
   (ivy--yank-by #'forward-char arg))
+(ivy--no-M-x #'ivy-yank-char #'ivy--minibuffer-p)
 
 (defvar ivy--pulse-overlay nil
   "Overlay used to highlight yanked word.")
@@ -4945,6 +5008,7 @@ If the region is active, forward to `kill-ring-save' 
instead."
   (if (use-region-p)
       (call-interactively #'kill-ring-save)
     (kill-new (string-join ivy--old-cands "\n"))))
+(ivy--no-M-x #'ivy-kill-ring-save #'ivy--minibuffer-p)
 
 (defun ivy-insert-current ()
   "Make the current candidate into current input.
@@ -4956,11 +5020,13 @@ Don't finish completion."
                   -1)))
     (insert (substring-no-properties
              (ivy-state-current ivy-last) 0 end))))
+(ivy--no-M-x #'ivy-insert-current #'ivy--minibuffer-p)
 
 (defun ivy-insert-current-full ()
   "Insert the current directory into the minibuffer."
   (interactive)
   (insert ivy--directory))
+(ivy--no-M-x #'ivy-insert-current-full #'ivy--minibuffer-p)
 
 (defcustom ivy-preferred-re-builders
   '((ivy--regex-plus . "ivy")
@@ -4979,6 +5045,7 @@ This list can be rotated with 
`ivy-rotate-preferred-builders'."
           (let ((cell (assq ivy--regex-function ivy-preferred-re-builders)))
             (car (or (cadr (memq cell ivy-preferred-re-builders))
                      (car ivy-preferred-re-builders)))))))
+(ivy--no-M-x #'ivy-rotate-preferred-builders #'ivy--minibuffer-p)
 
 (defun ivy-toggle-fuzzy ()
   "Toggle the re builder between `ivy--regex-fuzzy' and `ivy--regex-plus'."
@@ -4987,6 +5054,7 @@ This list can be rotated with 
`ivy-rotate-preferred-builders'."
   (if (eq ivy--regex-function 'ivy--regex-fuzzy)
       (setq ivy--regex-function 'ivy--regex-plus)
     (setq ivy--regex-function 'ivy--regex-fuzzy)))
+(ivy--no-M-x #'ivy-toggle-fuzzy #'ivy--minibuffer-p)
 
 (defun ivy--label-and-delete-dups (entries)
   "Label ENTRIES with history indices."
@@ -5020,10 +5088,11 @@ This list can be rotated with 
`ivy-rotate-preferred-builders'."
              (declare-function ring-remove "ring")
              (ring-remove history (ring-member history current)))))
     (ivy--kill-current-candidate)))
+(ivy--no-M-x #'ivy-reverse-i-search-kill #'ivy--minibuffer-p)
 
 (defvar ivy-reverse-i-search-map
   (let ((map (make-sparse-keymap)))
-    (ivy-define-key map (kbd "C-k") #'ivy-reverse-i-search-kill)
+    (define-key map (kbd "C-k") #'ivy-reverse-i-search-kill)
     map))
 
 (defun ivy-history-contents (history)
@@ -5069,6 +5138,7 @@ You can also delete an element from history with 
\\[ivy-reverse-i-search-kill]."
                            (insert (substring-no-properties (car x)))
                            (ivy--cd-maybe))
                  :caller 'ivy-reverse-i-search)))))
+(ivy--no-M-x #'ivy-reverse-i-search #'ivy--minibuffer-p)
 
 (defun ivy-restrict-to-matches ()
   "Restrict candidates to current input and erase input."
@@ -5088,31 +5158,39 @@ You can also delete an element from history with 
\\[ivy-reverse-i-search-kill]."
         (setf (ivy-state-dynamic-collection ivy-last) nil))
     (setq ivy--all-candidates
           (ivy--filter ivy-text ivy--all-candidates))))
+(ivy--no-M-x #'ivy-restrict-to-matches #'ivy--minibuffer-p)
 
 ;;; Occur
 
+;; Also used by `ivy--occur-p' to identify Ivy Occur buffers
+;; regardless of `major-mode'.
 (defvar-local ivy-occur-last nil
-  "Buffer-local value of `ivy-last'.
-Can't re-use `ivy-last' because using e.g. `swiper' in the same
+  "Buffer-local and Occur-specific value of `ivy-last'.
+Can't reuse `ivy-last' because using, e.g., `swiper' in the same
 buffer would modify `ivy-last'.")
 
+(defun ivy--occur-p (_cmd buf)
+  "Return non-nil if BUF is an Ivy Occur buffer.
+Intended as a `completion-predicate'."
+  (buffer-local-value 'ivy-occur-last buf))
+
 (defvar ivy-occur-mode-map
   (let ((map (make-sparse-keymap)))
-    (ivy-define-key map [mouse-1] #'ivy-occur-click)
-    (ivy-define-key map (kbd "RET") #'ivy-occur-press-and-switch)
-    (ivy-define-key map (kbd "j") #'ivy-occur-next-line)
-    (ivy-define-key map (kbd "k") #'ivy-occur-previous-line)
+    (define-key map (kbd "<mouse-1>") #'ivy-occur-click)
+    (define-key map (kbd "RET") #'ivy-occur-press-and-switch)
+    (define-key map (kbd "j") #'ivy-occur-next-line)
+    (define-key map (kbd "k") #'ivy-occur-previous-line)
     (define-key map (kbd "h") #'backward-char)
     (define-key map (kbd "l") #'forward-char)
-    (ivy-define-key map (kbd "f") #'ivy-occur-press)
-    (ivy-define-key map (kbd "g") #'ivy-occur-revert-buffer)
-    (ivy-define-key map (kbd "a") #'ivy-occur-read-action)
-    (ivy-define-key map (kbd "o") #'ivy-occur-dispatch)
-    (ivy-define-key map (kbd "c") #'ivy-occur-toggle-calling)
+    (define-key map (kbd "f") #'ivy-occur-press)
+    (define-key map (kbd "g") #'ivy-occur-revert-buffer)
+    (define-key map (kbd "a") #'ivy-occur-read-action)
+    (define-key map (kbd "o") #'ivy-occur-dispatch)
+    (define-key map (kbd "c") #'ivy-occur-toggle-calling)
     (define-key map (kbd "q") #'quit-window)
     (define-key map (kbd "R") #'read-only-mode)
-    (ivy-define-key map (kbd "C-d") #'ivy-occur-delete-candidate)
-    (ivy-define-key map (kbd "F") #'ivy-occur-flush-lines)
+    (define-key map (kbd "C-d") #'ivy-occur-delete-candidate)
+    (define-key map (kbd "F") #'ivy-occur-flush-lines)
     map)
   "Keymap for Ivy Occur mode.")
 
@@ -5125,6 +5203,7 @@ buffer would modify `ivy-last'.")
         (ivy-occur-press))
     (setq mode-name "Ivy-Occur"))
   (force-mode-line-update))
+(ivy--no-M-x #'ivy-occur-toggle-calling #'ivy--occur-p)
 
 (defun ivy--find-occur-buffer ()
   (let ((cb (current-buffer)))
@@ -5162,6 +5241,7 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'."
       (ivy--select-occur-buffer)
       (ivy-occur-next-line arg)
       (ivy-occur-press-and-switch))))
+(ivy--no-M-x #'ivy-occur-next-line #'ivy--occur-p)
 
 (defun ivy-occur-previous-line (&optional arg)
   "Move the cursor up ARG lines.
@@ -5180,6 +5260,7 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'."
       (ivy--select-occur-buffer)
       (ivy-occur-previous-line arg)
       (ivy-occur-press-and-switch))))
+(ivy--no-M-x #'ivy-occur-previous-line #'ivy--occur-p)
 
 (defun ivy-occur-next-error (n &optional reset)
   "A `next-error-function' for `ivy-occur-mode'."
@@ -5193,6 +5274,7 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'."
   ;; The window's point overrides the buffer's point every time it's 
redisplayed
   (dolist (window (get-buffer-window-list nil nil t))
     (set-window-point window (point))))
+(ivy--no-M-x #'ivy-occur-next-error #'ivy--occur-p)
 
 (define-derived-mode ivy-occur-mode fundamental-mode "Ivy-Occur"
   "Major mode for output from \\[ivy-occur].
@@ -5201,9 +5283,10 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'."
   (setq-local view-read-only nil))
 
 (defvar ivy-occur-grep-mode-map
-  (let ((map (copy-keymap ivy-occur-mode-map)))
-    (ivy-define-key map (kbd "C-x C-q") 'ivy-wgrep-change-to-wgrep-mode)
-    (ivy-define-key map "w" 'ivy-wgrep-change-to-wgrep-mode)
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map ivy-occur-mode-map)
+    (define-key map (kbd "C-x C-q") #'ivy-wgrep-change-to-wgrep-mode)
+    (define-key map (kbd "w") #'ivy-wgrep-change-to-wgrep-mode)
     map)
   "Keymap for Ivy Occur Grep mode.")
 
@@ -5212,12 +5295,14 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'."
   (let ((inhibit-read-only t))
     (delete-region (line-beginning-position)
                    (1+ (line-end-position)))))
+(ivy--no-M-x #'ivy-occur-delete-candidate #'ivy--occur-p)
 
 (defun ivy-occur-flush-lines ()
   "Delete lines matching regex."
   (interactive)
   (let ((inhibit-read-only t))
-    (call-interactively 'flush-lines)))
+    (call-interactively #'flush-lines)))
+(ivy--no-M-x #'ivy-occur-flush-lines #'ivy--occur-p)
 
 (define-derived-mode ivy-occur-grep-mode grep-mode "Ivy-Occur"
   "Major mode for output from \\[ivy-occur].
@@ -5297,6 +5382,7 @@ There is no limit on the number of *ivy-occur* buffers."
          (pop-to-buffer buffer)
          (setq next-error-last-buffer buffer)
          (setq-local next-error-function #'ivy-occur-next-error))))))
+(ivy--no-M-x #'ivy-occur #'ivy--minibuffer-p)
 
 (defun ivy-occur-revert-buffer ()
   "Refresh the buffer making it up-to date with the collection.
@@ -5323,6 +5409,7 @@ updated original buffer."
       (goto-char (point-min))
       (forward-line (1- line)))
     (setq ivy-occur-last ivy-last)))
+(ivy--no-M-x #'ivy-occur-revert-buffer #'ivy--occur-p)
 
 (declare-function wgrep-change-to-wgrep-mode "ext:wgrep")
 
@@ -5332,12 +5419,14 @@ updated original buffer."
   (if (require 'wgrep nil 'noerror)
       (wgrep-change-to-wgrep-mode)
     (error "Package wgrep isn't installed")))
+(ivy--no-M-x #'ivy-wgrep-change-to-wgrep-mode #'ivy--occur-p)
 
 (defun ivy-occur-read-action ()
   "Select one of the available actions as the current one."
   (interactive)
   (let ((ivy-last ivy-occur-last))
     (ivy-read-action)))
+(ivy--no-M-x #'ivy-occur-read-action #'ivy--occur-p)
 
 (defun ivy-occur-dispatch ()
   "Call one of the available actions on the current item."
@@ -5351,6 +5440,7 @@ updated original buffer."
            (ivy-occur-read-action)
            (ivy-occur-press))
       (setf (ivy-state-action ivy-occur-last) actions))))
+(ivy--no-M-x #'ivy-occur-dispatch #'ivy--occur-p)
 
 (defun ivy-occur-click (event)
   "Execute action for the current candidate.
@@ -5361,6 +5451,7 @@ EVENT gives the mouse position."
     (with-current-buffer (window-buffer window)
       (goto-char pos)
       (ivy-occur-press))))
+(ivy--no-M-x #'ivy-occur-click #'ivy--occur-p)
 
 (declare-function swiper--cleanup "swiper")
 (declare-function swiper--add-overlays "swiper")
@@ -5432,12 +5523,14 @@ EVENT gives the mouse position."
                 (cancel-timer ivy-occur-timer))
               (setq ivy-occur-timer
                     (run-at-time 1.0 nil 'swiper--cleanup))))))))
+(ivy--no-M-x #'ivy-occur-press #'ivy--occur-p)
 
 (defun ivy-occur-press-and-switch ()
   "Execute action for the current candidate and switch window."
   (interactive)
   (ivy-occur-press)
   (select-window (ivy--get-window ivy-occur-last)))
+(ivy--no-M-x #'ivy-occur-press-and-switch #'ivy--occur-p)
 
 (defun ivy--marked-p ()
   (member (ivy-state-current ivy-last) ivy-marked-candidates))
@@ -5474,6 +5567,7 @@ make decisions based on the whole marked list."
   (unless (ivy--marked-p)
     (ivy--mark (ivy-state-current ivy-last)))
   (ivy-next-line))
+(ivy--no-M-x #'ivy-mark #'ivy--minibuffer-p)
 
 (defun ivy-unmark ()
   "Unmark the selected candidate and move to the next one."
@@ -5481,6 +5575,7 @@ make decisions based on the whole marked list."
   (when (ivy--marked-p)
     (ivy--unmark (ivy-state-current ivy-last)))
   (ivy-next-line))
+(ivy--no-M-x #'ivy-unmark #'ivy--minibuffer-p)
 
 (defun ivy-unmark-backward ()
   "Move to the previous candidate and unmark it."
@@ -5489,6 +5584,7 @@ make decisions based on the whole marked list."
   (ivy--exhibit)
   (when (ivy--marked-p)
     (ivy--unmark (ivy-state-current ivy-last))))
+(ivy--no-M-x #'ivy-unmark-backward #'ivy--minibuffer-p)
 
 (defun ivy-toggle-marks ()
   "Toggle mark for all narrowed candidates."
@@ -5497,6 +5593,7 @@ make decisions based on the whole marked list."
     (if (member cand ivy-marked-candidates)
         (ivy--unmark cand)
       (ivy--mark cand))))
+(ivy--no-M-x #'ivy-toggle-marks #'ivy--minibuffer-p)
 
 (defconst ivy-help-file (let ((default-directory
                                (if load-file-name
diff --git a/swiper.el b/swiper.el
index eefb695b6c..e66be4db26 100644
--- a/swiper.el
+++ b/swiper.el
@@ -152,6 +152,7 @@ If the input is empty, select the previous history element 
instead."
   (if (string= ivy-text "")
       (ivy-previous-history-element 1)
     (ivy-next-line arg)))
+(ivy--no-M-x #'swiper-C-s #'ivy--minibuffer-p)
 
 (defvar swiper-map
   (let ((map (make-sparse-keymap)))
@@ -162,7 +163,7 @@ If the input is empty, select the previous history element 
instead."
     (define-key map (kbd "C-7") #'swiper-mc)
     (define-key map (kbd "C-c C-f") #'swiper-toggle-face-matching)
     map)
-  "Keymap for swiper.")
+  "Keymap for `swiper'.")
 
 (defvar swiper--query-replace-overlays nil)
 
@@ -244,10 +245,10 @@ If the input is empty, select the previous history 
element instead."
                        (perform-replace from to
                                         t t nil))))))
            (swiper--query-replace-cleanup)))))
+(ivy--no-M-x #'swiper-query-replace #'ivy--minibuffer-p)
 
 (ivy-configure 'swiper-query-replace
   :update-fn #'swiper--query-replace-updatefn)
-(function-put #'swiper-query-replace 'no-counsel-M-x t)
 
 (defvar inhibit-message)
 
@@ -272,7 +273,7 @@ If the input is empty, select the previous history element 
instead."
                     (goto-char (point-min))
                     (perform-replace from to t t nil)))
              (set-window-configuration wnd-conf))))))))
-(function-put #'swiper-all-query-replace 'no-counsel-M-x t)
+(ivy--no-M-x #'swiper-all-query-replace #'ivy--minibuffer-p)
 
 (defvar avy-all-windows)
 (defvar avy-style)
@@ -391,6 +392,7 @@ If the input is empty, select the previous history element 
instead."
        (let ((swiper-min-highlight 1))
          (swiper--update-input-ivy)))))
   (swiper--avy-goto (swiper--avy-candidate)))
+(ivy--no-M-x #'swiper-avy #'ivy--minibuffer-p)
 
 (declare-function mc/create-fake-cursor-at-point "ext:multiple-cursors-core")
 (declare-function multiple-cursors-mode "ext:multiple-cursors-core")
@@ -414,6 +416,7 @@ Make sure `swiper-mc' is on `mc/cmds-to-run-once' list."
              (when cands
                (mc/create-fake-cursor-at-point))))
          (multiple-cursors-mode 1))))))
+(ivy--no-M-x #'swiper-mc #'ivy--minibuffer-p)
 
 (defvar swiper--current-window-start nil
   "Store `window-start' to restore it later.
@@ -426,6 +429,7 @@ such as `scroll-conservatively' are set to a high value.")
   (with-ivy-window
     (recenter-top-bottom arg)
     (setq swiper--current-window-start (window-start))))
+(ivy--no-M-x #'swiper-recenter-top-bottom #'ivy--minibuffer-p t)
 
 (defvar swiper-font-lock-exclude
   '(Man-mode
@@ -869,6 +873,7 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
             nil
           #'swiper--face-matcher))
   (setq ivy--old-re nil))
+(ivy--no-M-x #'swiper-toggle-face-matching #'ivy--minibuffer-p)
 
 (defun swiper--face-matcher (regexp candidates)
   "Return REGEXP matching CANDIDATES.
@@ -1576,6 +1581,7 @@ Like `ivy-insert-current', but tailored for 
`swiper-isearch'."
   (delete-minibuffer-contents)
   (let ((cur (ivy-state-current ivy-last)))
     (insert (with-ivy-window (swiper--isearch-candidate-string cur)))))
+(ivy--no-M-x #'swiper--isearch-insert-current #'ivy--minibuffer-p)
 
 (defun swiper--isearch-kill-ring-save ()
   "Save the current candidates in the kill ring.
@@ -1586,6 +1592,7 @@ Like `ivy-kill-ring-save', but tailored for 
`swiper-isearch'."
       (call-interactively #'kill-ring-save)
     (kill-new (with-ivy-window
                 (mapconcat #'swiper--line-at-point ivy--old-cands "\n")))))
+(ivy--no-M-x #'swiper--isearch-kill-ring-save #'ivy--minibuffer-p)
 
 (defun swiper-isearch-thing-at-point ()
   "Insert `symbol-at-point' into the minibuffer of `swiper-isearch'.
@@ -1623,6 +1630,7 @@ When the input is empty, browse the search history 
instead."
   (if (string= ivy-text "")
       (ivy-reverse-i-search)
     (ivy-previous-line arg)))
+(ivy--no-M-x #'swiper-isearch-C-r #'ivy--minibuffer-p)
 
 (defvar swiper-isearch-map
   (let ((map (make-sparse-keymap)))

Reply via email to