2

It seems by default this can't be done. For example, if I try to activate helm-mini, and then from there activate helm-M-x, I get the error message: Error: Trying to run helm within a running helm session. Is there a good way around this constraint when writing a custom function?

My use case is this: I am trying to write my own function that will try to call helm-mini, but if helm is already running (i.e., if helm-alive-p evaluates to t) it will run helm-projectile-find-file instead. What I have so far is:

(defun es/helm-mini-or-projectile-find-file ()
  (interactive)
  (if (not (helm-alive-p))
      (helm-mini)
    (progn (helm-keyboard-quit)
           (helm-projectile-find-file))))

I am trying to kill helm if it is running before calling helm-projectile-find-file, because, as mentioned above, I can't run a helm command if I am already in the middle of another helm command. However, it seems like helm-keyboard-quit breaks out of the function before helm-projectile-find-file is executed. Or perhaps there is something else wrong with my code. I am still not very proficient at all with emacs lisp. Is there a good way to get around the constraint of no helm commands during a running helm session?

1 Answer 1

4

(helm-run-after-exit FUNCTION &rest ARGS)

helm-run-after-exit exits current helm session, and then runs FUNCTION, so this is what you want:

(defun es/helm-mini-or-projectile-find-file ()
  (interactive)
  (if (helm-alive-p)
      (helm-run-after-exit #'helm-projectile-find-file)
    (helm-mini)))

To call this command from a helm session, you need to bind a key to it since you can't use helm-M-x, e.g.,

(bind-key "C-c C-p" #'es/helm-mini-or-projectile-find-file)

Similarly, if you want to use helm-M-x from arbitrary helm commands, you can wrap helm-M-x with a new command and assign a key from helm-map to it.

(defun my-helm-M-x ()
  (interactive)
  (with-helm-alive-p
    (helm-run-after-exit
     (lambda () (call-interactively #'helm-M-x)))))

(bind-key "M-x" #'my-helm-M-x helm-map)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.