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

make (all) surrounding delimiters large #2313

Closed
ces42 opened this issue Jan 30, 2022 · 7 comments
Closed

make (all) surrounding delimiters large #2313

ces42 opened this issue Jan 30, 2022 · 7 comments

Comments

@ces42
Copy link
Contributor

ces42 commented Jan 30, 2022

Is your feature request related to a problem? Please describe it.
Using \left and \right can be a bit of a chore. Especially if you start writing a formula with lots of delimiters and only notice halfway in that there is a "tall" expression in there.

Describe the solution you'd like
It would be quite helpful to have a function (and probably also a normal mode mapping) that finds all math delimiters surrounding the cursor and makes them \left/\right delimiters if they are not already. E.g.

\exp \left( \int_0^1 f(x) dx + G(1 + T[x - \frac{x_0}{2}| ] ) \right)

(| is supposed to be the cursor position) would become

\exp \left( \int_0^1 f(x) dx + G\left(1 + T\left[x - \frac{x_0}{2} \right] \right) \right)

The use case I have in mind is binding this to some key combination in insert mode which I could press after writing a fraction/sum/integral so that I don't have to worry about manual \left/\right again. Even better, I could integrate this into my snippets to make it 100% automatic.

Describe alternatives you've considered
Currently I can do $tsd$ but this will only change the innermost delimiter. It's also a toggle so I can't just have my snippets call it automatically.

@lervag
Copy link
Owner

lervag commented Jan 31, 2022

Interesting suggestion. It should be possible. Let me play with it and see what I can do.

lervag added a commit that referenced this issue Jan 31, 2022
@lervag
Copy link
Owner

lervag commented Jan 31, 2022

I've added a function now. You can test it by calling it directly or mapping it, e.g.:

nnoremap <f8> :<c-u>call vimtex#delim#toggle_modifier_all()<cr>

I may make a mapping or command interface for this, but it would be useful with some feedback first. And perhaps a suggestion of default map and/or command name.

@ces42
Copy link
Contributor Author

ces42 commented Jan 31, 2022

Right now this is going into an infinite loop when I apply it in $a\left( b \right)$ with the cursor on the b.

@lervag
Copy link
Owner

lervag commented Jan 31, 2022

Sorry, that should be fixed now.

@ces42
Copy link
Contributor Author

ces42 commented Jan 31, 2022

Awesome, this is going into my snippets file right away.

Two remarks:

  • The name seems confusing to me because this does not act as a toggle
  • Right now it adds an item to the undo history even when nothing happens (which surprises me because I would thing that the only thing in the method that would make vim add to the undo history

The latter can be avoided by the following patch

diff --git a/autoload/vimtex/delim.vim b/autoload/vimtex/delim.vim
index 56e4702..5778f16 100644
--- a/autoload/vimtex/delim.vim
+++ b/autoload/vimtex/delim.vim
@@ -124,7 +124,6 @@ endfunction
 
 " }}}1
 function! vimtex#delim#toggle_modifier_all() abort " {{{1
-  call vimtex#util#undostore()
 
   " Use syntax highlights to detect region math region
   let l:ww = &whichwrap
@@ -139,6 +138,7 @@ function! vimtex#delim#toggle_modifier_all() abort " {{{1
   call vimtex#pos#set_cursor(l:cursor)
 
 
+  let l:undostore = v:false
   while v:true
     let [l:open, l:close] = vimtex#delim#get_surrounding('delim_modq_math')
     if empty(l:open) || vimtex#pos#val(l:open) <= l:startval
@@ -148,6 +148,11 @@ function! vimtex#delim#toggle_modifier_all() abort " {{{1
     call vimtex#pos#set_cursor(vimtex#pos#prev(l:open))
     if !empty(l:open.mod) | continue | endif
 
+    if !l:undostore
+      call vimtex#util#undostore()
+      let l:undostore = v:true
+    endif
+
     " Add close modifier
     let line = getline(l:close.lnum)
     let line = strpart(line, 0, l:close.cnum - 1)

lervag added a commit that referenced this issue Feb 1, 2022
@lervag
Copy link
Owner

lervag commented Feb 1, 2022

The name seems confusing to me because this does not act as a toggle

Agreed. Do you have a suggestion? How about vimtex#delim#add_modifiers? Do you have an opinion on default mapping and/or command name, e.g. :VimtexDelimAddModifiers? Perhaps a mapping suffices?

Right now it adds an item to the undo history even when nothing happens (which surprises me because I would thing that the only thing in the method that would make vim add to the undo history

Thanks, fixed now.

lervag added a commit that referenced this issue Feb 1, 2022
@lervag
Copy link
Owner

lervag commented Feb 1, 2022

I've implemented this fully now, but please, feel free to comment the naming and default map. I don't mind changing it if you have a better idea.

@lervag lervag closed this as completed Feb 1, 2022
lervag added a commit that referenced this issue Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants