From b361fc40532a9808c99f497336239b6fe4b41d5f Mon Sep 17 00:00:00 2001 From: jdhao Date: Thu, 12 Nov 2020 00:22:51 +0800 Subject: [PATCH] feat: add iu text object for URL (no au provided) Usage: select current URL: viu yank current URL: yiu delete/change current URL: diu/ciu --- core/mappings.vim | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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 "}