vim-operator-surround icon indicating copy to clipboard operation
vim-operator-surround copied to clipboard

Backslash blocks don't work well

Open thomasahle opened this issue 8 years ago • 9 comments

I would like to have a \langle ... \rangle surround for use in latex. Hence I have defined:

let g:operator#surround#blocks = {
\ '-' : [
\   {'block': ['\langle', '\rangle'], 'motionwise': ['char', 'line', 'block'], 'keys': ['<', '>']},
\ ] }

And the following block with vim-textobj-user:

call textobj#user#plugin('latex', {
\   'code': {'pattern': ['\\langle\>', '\\rangle\>'], 'select-a': 'a<', 'select-i': 'i<' },
\ })

This allows me to change () to \langle\rangle using sra(<. However I cannot change back. That is sra<( beeps and does nothing. If I change to 'block': ['\\langle', '\\rangle'] I can do sra<(, but then the original command inserts the wrong thing \\langle\\rangle

I seems to me this could be fixed by making sure we always search literaly, rather than using regex, when changing or deleting a surround.

thomasahle avatar Oct 06 '15 14:10 thomasahle

A workaround appears to be adding an extra surround for searching:

let g:operator#surround#blocks = {
\ '-' : [
\  {'block': ['\langle', '\rangle'], 'motionwise': ['char', 'line', 'block'], 'keys': ['<','>']},
\  {'block': ['\\langle', '\\rangle'], 'motionwise': ['char', 'line', 'block'], 'keys': []},
\ ] }

thomasahle avatar Oct 06 '15 14:10 thomasahle

Hi.

Strings in block are used as regular expression here. So backslash must be escaped with extra backslash. I think below works.

let g:operator#surround#blocks = {
\ '-' : [
\   {'block': ['\\langle', '\\rangle'], 'motionwise': ['char', 'line', 'block'], 'keys': ['<', '>']},
\ ] }

rhysd avatar Oct 08 '15 02:10 rhysd

['\\langle\>', '\\rangle\>'] may be better as you mentioned.

rhysd avatar Oct 08 '15 03:10 rhysd

The problem with this one seems to be when inserting the brackets. Here it is not used as a regular expression, but inserted verbatim, right?

thomasahle avatar Oct 08 '15 06:10 thomasahle

Ah, I understood. That's true. I'll consider how to resolve this issue..

rhysd avatar Oct 08 '15 06:10 rhysd

I think, if searching could be made non regular expression, that would solve it. Likely it's not really needed if this hasnthasn't been a problem before.

thomasahle avatar Oct 08 '15 06:10 thomasahle

Exactly the solution resolves this issue. However, it breaks other block definitions which rely on regular expression. I think it is good to enable to define inserted block text explicitly as below.

let g:operator#surround#blocks = {
\ '-' : [
\   {
\     'block': ['\\langle', '\\rangle'],
\     'block_text': ['\langle', '\rangle'],
\     'motionwise': ['char', 'line', 'block'],
\     'keys': ['<', '>']},
\ ] }

What do you think?

rhysd avatar Oct 09 '15 09:10 rhysd

That would certainly work :-) I guess I just can't imagine any cases where you'd want to insert a regular expression verbatim as a bracket.

On Fri, Oct 9, 2015, 11:03 AM Linda_pp [email protected] wrote:

Exactly the solution resolves this issue. However, it breaks other block definitions which rely on regular expression. I think it is good to enable to define inserted block explicitly as below.

let g:operator#surround#blocks = {\ '-' : [\ {\ 'block': ['\langle', '\rangle'],

\ 'block_text': ['\langle', '\rangle'],

\ 'motionwise': ['char', 'line', 'block'],\ 'keys': ['<', '>']},\ ] }

What do you think?

— Reply to this email directly or view it on GitHub https://github.com/rhysd/vim-operator-surround/issues/25#issuecomment-146806760 .

thomasahle avatar Oct 09 '15 09:10 thomasahle

e.g. HTML tag

rhysd avatar Oct 09 '15 10:10 rhysd