Created
February 9, 2024 20:48
URL text object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr> | |
xnoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr> | |
onoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr> | |
xnoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr> | |
function! s:UrlTextObject() | |
let saved_view = winsaveview() | |
let saved_end_pos = getpos("'b") | |
defer setpos("'e", saved_end_pos) | |
let cursor_col = col('.') | |
let url_pattern = highlighturl#default_pattern() | |
let [line, end_col] = searchpos(url_pattern, 'Wce', line('.')) | |
if line <= 0 | |
" No match found on the line: | |
call winrestview(saved_view) | |
return | |
endif | |
call setpos("'e", getpos('.')) | |
let [_, start_col] = searchpos(url_pattern, 'Wbc', line('.')) | |
if start_col > cursor_col | |
" The cursor was not on the URL | |
call winrestview(saved_view) | |
return | |
endif | |
normal! v`e | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment