regexp: match at cursor position for \%.l \%.c and \%.v#8497
regexp: match at cursor position for \%.l \%.c and \%.v#8497chrisbra wants to merge 1 commit intovim:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #8497 +/- ##
==========================================
- Coverage 90.02% 89.94% -0.08%
==========================================
Files 149 145 -4
Lines 168044 167043 -1001
==========================================
- Hits 151278 150254 -1024
- Misses 16766 16789 +23
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
This change allows to match the cursor position using the special mark
'.', so that `\%.c` will match the current cursor column, `\%.l` matches the
current cursor line and `\%.v` matches the current virtual column.
This has been wished for at #8179
It also works for the greater than `\%>.` and less than variants `\%<.`
However, the `\%.v` look actually quite ugly. An alternative would be
to omit the `.` altogether and use those variants: `\%>l` so that
when no number is given the cursor position would be automatically be
used.
Would that be better? I don't know.
I think it's better to keep the dot. If we would leave it out, it's too
easy to have an invalid pattern go unnoticed.
+\%>.l Matches above the cursor line.
+\%<.l Matches below the cursor line.
The other way around.
AppVeyor reports an error:
c:\projects\vim\src\regexp_nfa.c(1724) : error C2275: 'long_u' : illegal use of this type as an expression
120 c:\projects\vim\src\vim.h(332) : see declaration of 'long_u'
The other errors are flaky. That popupwin test keeps causing trouble,
hope someone can figure out how to avoid that.
…--
hundred-and-one symptoms of being an internet addict:
78. You find yourself dialing IP numbers on the phone.
/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
Okay, I'll leave it as is. However, please note, Thanks for noticing the other two things. I think I have fixed that now, will push a new version in a minute (after rebasing) |
This change allows to match the cursor position using the special mark '.', so '\%.c' will match the current cursor column, '\%.l' matches the current cursor line and '\%.v' matches the current virtual column. This has been wished for at vim#8179 It also works for the greater than '\%>.' and less than variants '\%<.'
|
Thank you very much for the feature; it's extremely useful. I don't know whether it's possible to implement, but Here is a real example where it's necessary. Suppose you want a mapping to insert the text under the cursor and between backticks on the command-line: Working test: vim9script
'# some comment with a `codespan` we want to insert on the command-line'->setline(1)
search('codespan')
cnoremap <C-R>` <C-R><C-R>=<SID>InsertCodespan()<CR>
def InsertCodespan(): string
var pat: string = '\%(\%<.c\|\%.c\)`\zs[^`]\+\ze`\%>.c'
return getline('.')
->matchstr(pat)
->substitute('^:', '', '')
enddef
feedkeys(":\<C-R>`")But notice this regex in the first line of the function: And in particular, notice the start: We can't just write vim9script
'# some comment with a `codespan` we want to insert on the command-line'->setline(1)
search('`codespan')
cnoremap <C-R>` <C-R><C-R>=<SID>InsertCodespan()<CR>
def InsertCodespan(): string
var pat: string = '\%<.c`\zs[^`]\+\ze`\%>.c'
return getline('.')
->matchstr(pat)
->substitute('^:', '', '')
enddef
feedkeys(":\<C-R>`")With With regards to vim9script
'the quick brown fox jumps over the lazy dog'->setline(1)
substitute/ /\t/g
search('jumps')
normal! h
feedkeys('/\%.v' .. "\<CR>")I guess that's because – internally – When |
Otherwise, we have to write this (which is much harder to read): Example: vim9script
'the quick brown fox jumps over the lazy dog'->setline(1)
substitute/ /\t/g
search('jumps')
normal! h
var vcol: number = virtcol([line('.'), col('.') - 1]) + 1
var pat: string = '\%' .. vcol .. 'v'
feedkeys('/' .. pat .. "\<CR>") |
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
…plicated
Problem: A pattern that matches the cursor position is bit complicated.
Solution: Use a dot to indicate the cursor line and column. (Christian
Brabandt, closes vim/vim#8497, closes vim/vim#8179)
vim/vim@04db26b
Also use `n = ++vcol` in regexp_bt.c as `++vcol` alone fails lint.
This change allows to match the cursor position using the special mark
'.', so that
\%.cwill match the current cursor column,\%.lmatches thecurrent cursor line and
\%.vmatches the current virtual column.This has been wished for at #8179
It also works for the greater than
\%>.and less than variants\%<.However, the
\%.vlook actually quite ugly. An alternative would be to omitthe
.altogether and use those variants:\%>lso that when no number is giventhe cursor position would be automatically be used.
Would that be better? I don't know.