cohama / lexima.vim

Auto close parentheses and repeat by dot dot dot...
995 stars 46 forks source link

Case-sensitive #136

Open aloispichler opened 1 year ago

aloispichler commented 1 year ago

Here, call lexima#add_rule({'char': '(', 'at': '\\Big\%#', 'input_after': '\Big)', 'filetype': 'tex'}) call lexima#add_rule({'char': '(', 'at': '\\big\%#', 'input_after': '\big)', 'filetype': 'tex'}) there seems to be an issue with case-sensitive comparisons, as both expand the same way.

cohama commented 1 year ago

Try to use \c pattern and with_submatch feature, which is like

call lexima#add_rule({'char': '(', 'at': '\c\\\(big\)\%#', 'input_after': '\\\1)', 'filetype': 'tex', 'with_submatch': 1})
aloispichler commented 1 year ago

I did not see \c in the docs, but this works very nicely, indeed! Now, \big( expands with \big) and \Big( with \Big). I use your trick now for the Latex environment \[…\]. Thanks!

A related issue is:

Before          Input      After            Comment
(|)             )          ()|              this works
\[…|\]          \]         \[…\]|           desired
\big(…|\big)    \big)      \big(…\big)|     desired

Overwriting the extension, as above, is the expected, natural behavior. Is there a hidden trick which does that automatically?

cohama commented 1 year ago

\c is a vanilla vim feature, not lexima.vim's (see also :help \\c).

Yes, you do not need hidden tricks but you can write your own rules explicitly.

lexima's leave feature can be used to achieve these rules.

For example,

call lexima#add_rule({'char': '\', 'at': '\%#\\]', 'leave': ']', 'filetype': 'tex'})
call lexima#add_rule({'char': '\', 'at': '\c\%#\\big)', 'leave': ')', 'filetype': 'tex'})

These rule will work as the follows. A single char \ is better to map, but you can change the key what you like.

Before          Input      After
\[…|\]          \          \[…\]|
\big(…|\big)    \          \big(…\big)|

Note that lexima's leave rule can only apply auto input text, see :help lexima-rules-leave.

aloispichler commented 1 year ago

Thank you for the \c and \C – did not know them. Aligato! Tried now to implement the norm in LaTeX:

Before   input   after
\'       |       \|'\|

where ' is the cursor position. I tried

    call lexima#add_rule({'char': '|', 'at': '\\\%#', 'input_after': ' \|', 'filetype': 'tex'})

but this does not seem to work. Perhaps an issue with special characters or escaping?

4513ECHO commented 1 year ago

Using | in lexima.vim is tricky a bit. You have to use <Bar> instead of actual | in char . It is native vim's way. Please see :help map-bar. lexima.vim use char as {lhs} of :imap as we've been told before. But in other places such as at, you have to use actual |. Hmm, I think this should be written in the document.

4513ECHO commented 1 year ago

Or, I think lexima.vim should report user-friendly error when lexima#add_rule() receive invalid char as {lhs}.

aloispichler commented 1 year ago

Thank you, this helps indeed :-) So I thought that <Tab> can be used in the same way as you suggest to leave. So I tried

    call lexima#add_rule({'char': '[',     'at': '\\\%#', 'input': '[   ', 'input_after': ' \]', 'filetype': 'tex'})
    call lexima#add_rule({'char': '\]',    'at': '\\\[', 'leave': ']', 'filetype': 'tex'})
    call lexima#add_rule({'char': '<Tab>', 'at': '\\\[', 'leave': ']', 'filetype': 'tex', 'input': '<Tab>'})
    call lexima#add_rule({'char': '<BS>',  'at': '\\\[\%#\\\]', 'delete': 2, 'filetype': 'tex'})

However, for this to work, there seems to be a hidden trick again…

4513ECHO commented 1 year ago

How doesn't it work? As we've told before, we have to know the difference between String and Regex. ( There is the docuemnt about type of properties in :help lexima-rules. ) Regex have to be escaped when we use some special characters, but String must not be escaped. It is taken as raw string.

aloispichler commented 1 year ago

I know the documentation. Here is another example, where I tried multiple variants to leave by typing <Tab> (as is the standard with UltiSnips, for example).

    call lexima#add_rule({'char': '(',     'at': '\c\\\(big\)\%#', 'input_after': '\\\1)', 'with_submatch': 1})
    call lexima#add_rule({'char': ')',     'at': '\\big(', 'leave': ')'})
    call lexima#add_rule({'char': '<Tab>', 'at': '\\big(', 'leave': ')'})
    call lexima#add_rule({'char': '<BS>',  'at': '\\big(\%#\\big)', 'delete': 5})

The snippets above insert a <Tab> on typing <Tab>, but it is supposed to leave. So far, I did not manage to get this running and your help is highly appreciated.

cohama commented 1 year ago

Hmm, I think this should be written in the document. Or, I think lexima.vim should report user-friendly error when lexima#add_rule() receive invalid char as {lhs}.

Agree. In this case, it would be better to report an error.