The context

I've defined a big set of yasnippet snippets and sometimes I forget the shortcut for those snippets. I've found out recently that the function yas-insert-snippet can be used to insert a snippet by writing its name and the completion list of the function lists all the existing snippets.

I would like to use helm only for selecting an item from the completion list shown by yas-insert-snippet. However, I don't want helm-mode to be enabled because that would change the behavior of all functions that show a completion list (e.g. switch-to-buffer, bookmark-jump, kill-buffer, etc.).

I just want helm-mode to be enabled when executing the function yas-insert-snippet.

I've found that the variable helm-completing-read-handlers-alist can only be used to turn off helm-mode for specific functions but I would need to add all the functions to helm-completing-read-handlers-alist using cons of the form (FUNCTION . nil) if I want helm-mode to be disabled when executing FUNCTION.

The question

How can I toggle helm-mode for an specific function? I assume that helm-mode is disabled by default since helm is not used for all functions.

Additional context

I've tried the following but it doesn't work. I wrote it because I noticed that helm-mode is a variable.

(let ((previous-helm-mode helm-mode))
  (helm-mode 1)
  (call-interactively 'yas-insert-snippet)
  (helm-mode previous-helm-mode))
  • You can't turn the mode off with a nil value. Try setting previous-helm-mode to (or helm-mode 0).
    – jagrg
    Feb 19 at 16:45
  • I didn't know that, thanks for the help! Feb 19 at 18:40

You can use the following function to accomplish that

(defun execute-with-helm (command)
  (if helm-mode
      (call-interactively command))
    (helm-mode 1)
    ;; We call `unwind-protect' to ensure that `helm-mode' is
    ;; disabled even though `command' doesn't complete normally.
    ;; Without `unwind-protect', if the user presses =C-g= while
    ;; `command' is being executed, then the entire function would
    ;; be exited and therefore, `helm-mode' wouldn't be disabled'
    (unwind-protect (call-interactively command)
      (helm-mode -1))))

Then, you can use that function as follows

(execute-with-helm 'switch-to-buffer)
(execute-with-helm 'kill-buffer)
(execute-with-helm 'yas-insert-snippet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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