vimtex icon indicating copy to clipboard operation
vimtex copied to clipboard

Toggle different styles of fraction, arrow etc. support request

Open pilgrimlyieu opened this issue 2 years ago • 5 comments

I've checked that the fraction toggle only works for .../... and \frac{...}{...}.

I consider that it should be useful to toggle different styles of fraction such as cycling in \frac{...}{...}, \dfrac{...}{...} and \cfrac{...}{...}.

Besides, this can also apply to arrow toggle, which means we can cycle in ->, \rightarrow, \Rightarrow and so on.

I've tried it through another vim plugin called UltiSnips

snippet code
global

command_mapping = ['🚀', '🚁', '🚂', '🚃', '🚄', '🚅', '🚆', '🚇', '🚈', '🚉', '🚊', '🚋', '🚌', '🚍', '🚎', '🚏']

def choose_next(string, array, length = 0):
    return array[array.index(string) - (length or len(array)) + 1]

def command_cycle(target, commands, bracketnum = 1):
    length = len(commands)
    command_map = command_mapping[:length]
    for i in range(length):
        target = target.replace(commands[i], command_map[i])
    string = list(target)
    depth = brackets = 0
    for i in range(len(string) - 1, -1, -1):
        if string[i] == '}': depth += 1
        elif string[i] == '{': depth -= 1
        elif brackets == bracketnum:
            if not depth:
                try:
                    string[i] = choose_next(string[i], command_map, length)
                    break
                except:
                    pass
        brackets += 0 if depth else 1
    result = ''.join(string)
    for i in range(length):
        result = result.replace(command_map[i], commands[i])
    return result

fractions = ['\\frac', '\\dfrac' ,'\\cfrac']

endglobal

snippet "(\\(frac|dfrac|cfrac)\{.*\}\{.*\})" "Fraction Transformation" r
`!p
snip.rv = command_cycle(match.group(1), fractions, 2)
`
endsnippet

However, this solution is easy-trigger, which means even in case like \frac{...}{...}...{...}<tab> can trigger the transformation.

I've also struggled to use commands like vimtex#delim#get_surrounding to match the brackets via UltiSnips, but cause I'm a new learner so I failed.

I wonder if there is a simple solution that can apply for different cases to achieve my request, Or it can be taken as a feature into later version.

Appreciating your wonderful work!

pilgrimlyieu avatar May 27 '22 12:05 pilgrimlyieu

In my view, we can use vimtex#delim#get_surrounding command to get the position of the matching bracket.

\frac{...}{...<cmd>}</cmd>
-->
\frac{...}<cursor>{</cursor>...}
-->
\frac{...<cmd>}</cmd>{...}
-->
\frac<cursor>{</cursor>...}{...}
-->
<select>\frac</select>{...}{...}
-->
\dfrac{...}{...}<cursor>

for \frac{...}{...} and \dfrac{...}{...}

\bar{...<cmd>}</cmd>
-->
\bar<cursor>{</cursor>...}
-->
<select>\bar</select>{...}
-->
\overline{...}<cursor>

for \bar{...} and \overline{...}

\rightarrow<cursor>
-->
\Rightarrow<cursor>

for \rightarrow and \Rightarrow

pilgrimlyieu avatar May 27 '22 14:05 pilgrimlyieu

I consider that it should be useful to toggle different styles of fraction such as cycling in \frac{...}{...}, \dfrac{...}{...} and \cfrac{...}{...}.

You want something like this:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac', 'dfrac', 'cfrac' ]

where g:vimtex_toggle_fractions specify the various fraction commands you want to toggle between, including the ... / ... (INLINE) variant. This should not be too hard to implement with a default option value similar to the current behaviour:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac' ]

Does this sound like a sensible solution to you?

Besides, this can also apply to arrow toggle, which means we can cycle in ->, \rightarrow, \Rightarrow and so on.

This is something else entirely. UltiSnips is very neat and I use it myself; but I don't use it for toggles like this. It may work for that as well, but it feels a little bit out of scope.

I think the general behaviour of toggling or cyclign between related concepts is captured by some other plugins. At least for the more simple cases, such as the arrows. I suggest you look into one of these:

  • https://github.com/monaqa/dial.nvim
  • https://github.com/zef/vim-cycle
  • https://github.com/AndrewRadev/switch.vim
  • https://www.vim.org/scripts/script.php?script_id=895

I believe all of these should work for you with some configuration (although at least one of these require neovim).

Appreciating your wonderful work!

Glad to hear it! :)

lervag avatar May 28 '22 17:05 lervag

You want something like this:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac', 'dfrac', 'cfrac' ]

where g:vimtex_toggle_fractions specify the various fraction commands you want to toggle between, including the ... / ... (INLINE) variant. This should not be too hard to implement with a default option value similar to the current behaviour:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac' ]

Does this sound like a sensible solution to you?

That's what I want exactly! Thanks you very much!

This is something else entirely. UltiSnips is very neat and I use it myself; but I don't use it for toggles like this. It may work for that as well, but it feels a little bit out of scope.

I think the general behaviour of toggling or cyclign between related concepts is captured by some other plugins. At least for the more simple cases, such as the arrows. I suggest you look into one of these:

  • https://github.com/monaqa/dial.nvim
  • https://github.com/zef/vim-cycle
  • https://github.com/AndrewRadev/switch.vim
  • https://www.vim.org/scripts/script.php?script_id=895

I believe all of these should work for you with some configuration (although at least one of these require neovim).

Thanks, I'll try some of them.

Should I close the issue currently or after the feature is implemented?

pilgrimlyieu avatar May 29 '22 00:05 pilgrimlyieu

Should I close the issue currently or after the feature will be implemented?

Nah, let's leave the issue opened until I get around to implementing this. Won't make promises on when, but I'll look into it when I get the time :)

lervag avatar May 29 '22 09:05 lervag

but I'll look into it when I get the time :)

Thanks!

pilgrimlyieu avatar May 29 '22 09:05 pilgrimlyieu

Sorry, took a while before I got around to it. But I believe this should work as promised now!

lervag avatar Oct 03 '22 21:10 lervag

Great! Appreciating your excellent works!

pilgrimlyieu avatar Oct 04 '22 00:10 pilgrimlyieu