Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand with no trigger key #998

Open
bumpketeer opened this issue Apr 24, 2019 · 16 comments · May be fixed by #1048
Open

expand with no trigger key #998

bumpketeer opened this issue Apr 24, 2019 · 16 comments · May be fixed by #1048
Labels

Comments

@bumpketeer
Copy link

No description provided.

@bumpketeer
Copy link
Author

Is there a way to expand snippets without pressing tab or any trigger key at all?

@npostavs
Copy link
Collaborator

I guess you could try something like

(defun my-yas-try-expanding ()
  (when yas-minor-mode (yas-expand)))
(add-hook 'post-command-hook #'my-yas-try-expanding)

@korsbo
Copy link

korsbo commented May 20, 2019

I'm coming from vim and UltiSnips where you can specify in the snippet definition whether a particular snippet should be auto-expanded. Would it be feasible to add such a feature here?

I like auto-expansion, but making it global would either limit one's ability to write "normal" text or it would demand some really complex snippet keys lest it is accidentally typed (e.g. auto-expanding the latex figure snippet "fig" would make it hard to write plain text).

@npostavs
Copy link
Collaborator

npostavs commented May 21, 2019

@korsbo You can use the condition system for this. Something along the lines of

(defun my-yas-try-expanding-auto-snippets ()
  (when yas-minor-mode
    (let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
      (yas-expand))))
(add-hook 'post-command-hook #'my-yas-try-expanding-auto-snippets)

Then put # condition: 'auto in the yasnippets you want to be able to expand automatically.

@korsbo
Copy link

korsbo commented May 21, 2019

Nice one, thanks!

@earvingad
Copy link

earvingad commented May 26, 2019

(defun my-yas-try-expanding-auto-snippets ()
  (when yas-minor-mode
    (let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
      (yas-expand))))
(add-hook 'post-command-hook #'my-yas-try-expanding-auto-snippets)

Then put # condition: auto in the yasnippets you want to be able to expand automatically.

I am trying this and getting
[yas] Error in condition evaluation: Symbol’s value as variable is void: auto
# condition: auto is already in the snippet I want to spand.

EDIT:
I think problem was syntax. Changed
require-snippet-condition . auto
to
require-snippet-condition.auto
and it worked. Thanks!

@npostavs
Copy link
Collaborator

I am trying this and getting
[yas] Error in condition evaluation: Symbol’s value as variable is void: auto
# condition: auto is already in the snippet I want to spand.

Sorry, I made a mistake, it should be #condition: 'auto (with a quote), in the snippets to expand.

require-snippet-condition.auto
and it worked.

If this works, it's only by accident.

@earvingad
Copy link

earvingad commented May 26, 2019

Sorry, I made a mistake, it should be #condition: 'auto (with a quote), in the snippets to expand.

Aaah that fixes everything.

If this works, it's only by accident.

Yes, it was working but still displaying the error in logs, but working.

@korsbo
Copy link

korsbo commented May 28, 2019

Just in case anyone else may find it useful: I had trouble using this in spacemacs since yas-minor-mode does not seem to be immediately defined. My fix was just a minor tweak of the above, inserted to the user-config function.

  (defun my-yas-try-expanding-auto-snippets ()
    (when (and (boundp 'yas-minor-mode) yas-minor-mode)
      (let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
        (yas-expand))))
  (add-hook 'post-command-hook #'my-yas-try-expanding-auto-snippets)

@matthuszagh
Copy link

I'm finding this very useful. Could this be added to the main codebase rather than as a user configuration?

@npostavs
Copy link
Collaborator

npostavs commented Apr 2, 2020

I'm finding this very useful. Could this be added to the main codebase rather than as a user configuration?

Pending in #1048. I'll probably make a minor bug fix release in a month or so, before merging that.

@maikol-solis
Copy link

Just in case anyone else may find it useful: I had trouble using this in spacemacs since yas-minor-mode does not seem to be immediately defined. My fix was just a minor tweak of the above, inserted to the user-config function.

  (defun my-yas-try-expanding-auto-snippets ()
    (when (and (boundp 'yas-minor-mode) yas-minor-mode)
      (let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
        (yas-expand))))
  (add-hook 'post-command-hook #'my-yas-try-expanding-auto-snippets)

Thanks for this super useful function.

Just a question. How do you could expand snippet only in certain modes? (e.g. issue #990 )

In the mentioned issue, the example is having something like this

# -*- mode: snippet -*-
# name: Parenthesis
# binding: ()
# key: ()
# --
\left($1\right)

and expand this text

The parenthesis are $()$

into this

The parenthesis are $\left(<CURSOR HERE>\right)$

However, the idea is to do it typing no other character apart from ().

Thanks for your help.

@matthuszagh
Copy link

matthuszagh commented Jun 11, 2020

@maikol-solis here's what I use. I think it's what you're looking for.

# -*- mode: snippet -*-
# name: parentheses
# key: ()
# condition: (and (texmathp) 'auto)
# --
\left($1\right)$0

I have this filed under snippets/latex-mode so that it's expanded in latex mode only when editing a math environment. Note that you can also get this to work in other modes selectively. For instance, I have a .yas-parents file for org-mode so that this also works when I'm editing latex math in an org buffer.

This an example of using the condition system.

Also, a small nitpick. In the issue you reference you ask about only expanding inside a math environment. However, your recent question asks about only expanding within certain modes. To say mode is a bit misleading here. I don't believe there is any mode associated with being inside a tex math environment (though correct me if I'm wrong), texmathp is simply a function that identifies whether you're inside a tex math enviro. If your question were truly about mode-specific expansion, you'd want to look at the organization section of the documentation, but I don't think that's the part that confuses you.

@maikol-solis
Copy link

maikol-solis commented Jun 11, 2020

@maikol-solis here's what I use. I think it's what you're looking for.

# -*- mode: snippet -*-
# name: parentheses
# key: ()
# condition: (and (texmathp) 'auto)
# --
\left($1\right)$0

I have this filed under snippets/latex-mode so that it's expanded in latex mode only when editing a math environment. Note that you can also get this to work in other modes selectively. For instance, I have a .yas-parents file for org-mode so that this also works when I'm editing latex math in an org buffer.

This an example of using the condition system.

Brilliant! This is exactly what I want it.

Also, a small nitpick. In the issue you reference you ask about only expanding inside a math environment. However, your recent question asks about only expanding within certain modes. To say mode is a bit misleading here. I don't believe there is any mode associated with being inside a tex math environment (though correct me if I'm wrong), texmathp is simply a function that identifies whether you're inside a tex math enviro. If your question were truly about mode-specific expansion, you'd want to look at the organization section of the documentation, but I don't think that's the part that confuses you.

You are completely right. The idea is auto expand the triggers only when there is a math environment (inline or displayed) inside a latex document.

Your solution is exactly what I intend to do.

Thanks for the help.

@maikol-solis
Copy link

I found a minor glitch with this feature.

#1048 (comment)

@theflysurfer
Copy link

Hello. This little function is very interesting.
If I put it in my init.el, I get an error :
Error in post-command-hook (my-yas-try-expanding-auto-snippets): (void-variable yas-minor-mode)

Whereas if i force the evaluation once it is fully started, I get no error.

Do I have to add something, like waiting for something to be loaded before evaluating the function ?

Thanks for the help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants