vim-textobj-user icon indicating copy to clipboard operation
vim-textobj-user copied to clipboard

'*pattern*': [a, b] does not work when a and b are equal

Open rbonvall opened this issue 11 years ago • 4 comments

I'm creating text objects for LaTeX code. In LaTeX, inline equations can be typed \(like this\) or $like this$. I tried to create text objects for both with the following configuration:

\  'paren-math': {
\     '*pattern*': ['\\(', '\\)'],
\     'select-a': 'a\',
\     'select-i': 'i\',
\   },
\  'dollar-math': {
\     '*pattern*': ['[$]', '[$]'],
\     'select-a': 'a$',
\     'select-i': 'i$',
\   },

but only the first one worked. I also tried '*pattern*: '[$][^$]*[$]', also with no positive results.

Am I missing something? Is there another simple way to create text objects where the delimiters are the same (like the ones vim has for quoted text) without defining the selection functions?

rbonvall avatar Mar 10 '13 06:03 rbonvall

Thank you for the report. It is a known issue. See also #2.

\  'dollar-math': {
\     '*pattern*': ['[$]', '[$]'],
\     'select-a': 'a$',
\     'select-i': 'i$',
\   },

but only the first one worked. I also tried '*pattern*: '[$][^$]*[$]', also with no positive results.

select-a and select-i imply that a pair of regular expressions is given to *pattern*, while select implies that a single regular expression is given to *pattern*. So, if you want to avoid using functions, try the following definitions instead:

\   'dollar-math-a': {
\     '*pattern*': '\$[^$]*\$',
\     'select': 'a$',
\   },
\   'dollar-math-i': {
\     '*pattern*': '\$\zs[^$]*\ze\$',
\     'select': 'i$',
\   },

But it's better to support such usage. The function to select a region for '*pattern*': [begin, end] is textobj#user#select_pair. Do you have any idea to improve it?

kana avatar Mar 11 '13 12:03 kana

Hi @kana, thanks for providing a workaround. I'll take a look at the source code to see if I can fix the select_part funcion.

rbonvall avatar Mar 11 '13 18:03 rbonvall

vim-textobj-user internally uses searchpairpos() to find a region surrounded by a pair of patterns. It is a special kind of search function, because it takes care of nested structures. And it always fails if given patterns are equivalent. I looked into its implementation, and I guess that it is an intentional behavior to avoid getting stuck.

So that it seems to be necessary to implement a searchpairpos()-like function which also works fine with equivalent patterns.

kana avatar Mar 16 '13 12:03 kana

Perhaps it would be better to change the whole algorithm used in textobj#user#select_pair() instead of sticking to searchpairpos(). Anyway, I'll look into the problem more.

kana avatar Mar 16 '13 12:03 kana