aboutsummaryrefslogtreecommitdiff
path: root/core/mappings.vim
diff options
context:
space:
mode:
authorjdhao <jdhao@hotmail.com>2020-11-12 00:22:51 +0800
committerjdhao <jdhao@hotmail.com>2020-11-12 00:22:51 +0800
commitb361fc40532a9808c99f497336239b6fe4b41d5f (patch)
tree6b82ac05c0b299cb224ca18014de7e64e8473942 /core/mappings.vim
parent3178f89c2f17c940fcb412c0e6634224172abfe8 (diff)
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
Diffstat (limited to 'core/mappings.vim')
-rw-r--r--core/mappings.vim31
1 files changed, 31 insertions, 0 deletions
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 <Left> <C-W>h
nnoremap <Right> <C-W>l
nnoremap <Up> <C-W>k
nnoremap <Down> <C-W>j
+
+vnoremap <silent> iu :<C-U>call <SID>URLTextObj()<CR>
+onoremap <silent> iu :<C-U>call <SID>URLTextObj()<CR>
+
+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
"}