aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjdhao <jdhao@hotmail.com>2020-11-12 21:45:00 +0800
committerjdhao <jdhao@hotmail.com>2020-11-12 21:45:55 +0800
commitf68fa62e864b1293d74bc23e4fe7b54746203370 (patch)
tree4d235609ce5e5065507cffd51c2e16b5755a1bdd
parent6322fed22155b2c784ddf68bfb3e4b2c6fed09e2 (diff)
Move text object to autoload directory
-rw-r--r--autoload/text_obj.vim54
-rw-r--r--core/mappings.vim59
2 files changed, 56 insertions, 57 deletions
diff --git a/autoload/text_obj.vim b/autoload/text_obj.vim
new file mode 100644
index 0000000..ba7c808
--- /dev/null
+++ b/autoload/text_obj.vim
@@ -0,0 +1,54 @@
+function! text_obj#URL() abort
+ if match(&runtimepath, 'vim-highlighturl') != -1
+ " Note that we use https://github.com/itchyny/vim-highlighturl to get the URL pattern.
+ let url_pattern = highlighturl#default_pattern()
+ else
+ let url_pattern = expand('<cfile>')
+ " Since expand('<cfile>') also works for normal words, we need to check if
+ " this is really URL using heuristics, e.g., URL length.
+ if len(url_pattern) <= 10
+ return
+ endif
+ endif
+
+ " We need to find all possible URL on this line and their start, end idx.
+ " Then find where current cursor is, and decide if cursor is on one of the
+ " URLs.
+ let line_text = getline('.')
+ let url_infos = []
+
+ let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern)
+ while _url !=# ''
+ let url_infos += [[_url, _idx_start+1, _idx_end]]
+ let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern, _idx_end)
+ endwhile
+
+ " echo url_infos
+ " If no URL is found, do nothing.
+ if len(url_infos) == 0
+ return
+ endif
+
+ let [start_col, end_col] = [-1, -1]
+ " If URL is found, find if cursor is on it.
+ let [buf_num, cur_row, cur_col] = getcurpos()[0:2]
+ for url_info in url_infos
+ " echo url_info
+ let [_url, _idx_start, _idx_end] = url_info
+ if cur_col >= _idx_start && cur_col <= _idx_end
+ let start_col = _idx_start
+ let end_col = _idx_end
+ break
+ endif
+ endfor
+
+ " Cursor is not on a URL, do nothing.
+ if start_col == -1
+ return
+ endif
+
+ " Now set the '< and '> mark
+ call setpos("'<", [buf_num, cur_row, start_col, 0])
+ call setpos("'>", [buf_num, cur_row, end_col, 0])
+ normal! gv
+endfunction
diff --git a/core/mappings.vim b/core/mappings.vim
index 81646f7..2aa9723 100644
--- a/core/mappings.vim
+++ b/core/mappings.vim
@@ -211,61 +211,6 @@ nnoremap <Right> <C-W>l
nnoremap <Up> <C-W>k
nnoremap <Down> <C-W>j
-xnoremap <silent> iu :<C-U>call <SID>URLTextObj()<CR>
-onoremap <silent> iu :<C-U>call <SID>URLTextObj()<CR>
-
-function! s:URLTextObj() abort
- if match(&runtimepath, 'vim-highlighturl') != -1
- " Note that we use https://github.com/itchyny/vim-highlighturl to get the URL pattern.
- let url_pattern = highlighturl#default_pattern()
- else
- let url_pattern = expand('<cfile>')
- " Since expand('<cfile>') also works for normal words, we need to check if
- " this is really URL using heuristics, e.g., URL length.
- if len(url_pattern) <= 10
- return
- endif
- endif
-
- " We need to find all possible URL on this line and their start, end idx.
- " Then find where current cursor is, and decide if cursor is on one of the
- " URLs.
- let line_text = getline('.')
- let url_infos = []
-
- let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern)
- while _url !=# ''
- let url_infos += [[_url, _idx_start+1, _idx_end]]
- let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern, _idx_end)
- endwhile
-
- " echo url_infos
- " If no URL is found, do nothing.
- if len(url_infos) == 0
- return
- endif
-
- let [start_col, end_col] = [-1, -1]
- " If URL is found, find if cursor is on it.
- let [buf_num, cur_row, cur_col] = getcurpos()[0:2]
- for url_info in url_infos
- " echo url_info
- let [_url, _idx_start, _idx_end] = url_info
- if cur_col >= _idx_start && cur_col <= _idx_end
- let start_col = _idx_start
- let end_col = _idx_end
- break
- endif
- endfor
-
- " Cursor is not on a URL, do nothing.
- if start_col == -1
- return
- endif
-
- " Now set the '< and '> mark
- call setpos("'<", [buf_num, cur_row, start_col, 0])
- call setpos("'>", [buf_num, cur_row, end_col, 0])
- normal! gv
-endfunction
+xnoremap <silent> iu :<C-U>call text_obj#URL()<CR>
+onoremap <silent> iu :<C-U>call text_obj#URL()<CR>
"}