diff --git a/core/mappings.vim b/core/mappings.vim index 26bb3f3..92c3f7d 100644 --- a/core/mappings.vim +++ b/core/mappings.vim @@ -210,4 +210,35 @@ nnoremap h nnoremap l nnoremap k nnoremap j + +vnoremap iu :call URLTextObj() +onoremap iu :call URLTextObj() + +function! s:URLTextObj() abort + " Note that we use https://github.com/itchyny/vim-highlighturl to get the URL pattern. + let url_pattern = highlighturl#default_pattern() + " Note that the index starts at 0, end_idx is index of the character left of + " the pattern. + let [url, start_idx, end_idx] = matchstrpos(getline('.'), url_pattern) + if start_idx == -1 + return + endif + + let cursor_col = getcurpos()[2] + let cur_row = line('.') + " Adjust the idx to actual column number. + let start_col = start_idx + 1 + let end_col = end_idx + + " Only active it when cursor is on the URL, otherwise, do nothing. + if cursor_col < start_col || cursor_col > end_idx + return + endif + + let bufnum = bufnr() + " now set the '< and '> mark + call setpos("'<", [bufnum, cur_row, start_col, 0]) + call setpos("'>", [bufnum, cur_row, end_col, 0]) + normal! gv +endfunction "}