joaotavora / yasnippet

A template system for Emacs
http://joaotavora.github.io/yasnippet/
2.78k stars 311 forks source link

Expanding inside org-mode code blocks (in evil-mode) #1017

Open DiogoFerrari opened 5 years ago

DiogoFerrari commented 5 years ago

Is it possible to make it work inside org-mode code blocks, and use the native (code block language) expansion?

npostavs commented 5 years ago

Yes, it's possible. However it currently doesn't work in text-mode blocks for org 9 and above.

https://github.com/joaotavora/yasnippet/issues/852#issuecomment-483014208

DiogoFerrari commented 5 years ago

I found out why it isn't expanding. I use evil-mode. If I turn it off, then it works ok and expands natively. If I enable evil-mode, then it stops working. Any clue about how to fix this?

npostavs commented 5 years ago

Oh, I'm not sure. All three packages are doing some fancy stuff with the keybindings, so having them interact correctly is tricky.

jonlee836 commented 5 years ago

@DiogoFerrari

evil-mode uses

(indent-according-to-mode) which causes all sorts of unwanted behavior while used in org-mode

https://github.com/emacs-evil/evil/blob/874beba2cb243c325eca08fb7badff567f3c9494/evil-commands.el

mohkale commented 4 years ago

@DiogoFerrari I'm using evil and I haven't faced any issues. Are you expanding from evil-normal-state or evil-insert-state and could you share what command is run when you press enter?

If it's evil-ret-and-indent try rebinding it to evil-ret or simply nil. In my case return runs newline-and-indent.

DiogoFerrari commented 4 years ago

Somehow, tab within code blocks triggers org-cycle. If I run yas-expand within the code block, it does nothing, it does not matter the evil state (normal, insert).

When I press enter, it runs evil-ret.

mohkale commented 4 years ago

Well the default binding for tab in org-mode is org-cycle; though evil generally overrides it with state based bindings. Though I don't think evil binds anything on the tab key, so it doesn't override it for your states. You may be able to force evil to use yas-expand in insert state by setting <insert-state> <tab>.

for example, with general:

(general-define-key
  :states 'insert
  "<tab>" 'yas-expand)

overrides my global binding of tab and also my org-mode binding of tab.

Alternatively you could consider binding another (less popular) key to yas-expand. I've got it bound to C-i with M-i bound to aya-expand and C-M-i bound to ivy-yasnippet.

If your having issues with the available snippets in org code blocks, you can run org-edit-special to open the block in a new buffer in the same mode as the code being edited. This should guarantee the correct snippets are available (& I believe is the recommended way of editing code blocks).

mohkale commented 4 years ago

Can you share a GIF of the issue your having and how running in emacs state doesn't have such an issue. I'm still not quite sure what the problem is.

jinheebaek commented 2 years ago

I recently had the same issue. I found that tab within code blocks triggered org-cycle, which in turn executes a tab-binded command in org-edit-special buffer. In my case, the problem was that, in this buffer, evil state was changed to the default evil-normal-state, which probably shifted the position of point.

I could fix the issue either by modifying org-babel-do-in-edit-buffer macro to run a command in evil-insert-state, or simply by setting evil-default-state to insert for org-src-mode with (add-hook 'org-src-mode-hook 'evil-insert-state)