Skip to content

Commit

Permalink
Add visual mode investigate on selected text
Browse files Browse the repository at this point in the history
Select visual text based on http://stackoverflow.com/a/6271254/889972
  • Loading branch information
zhuochun committed Dec 20, 2015
1 parent fcb254c commit b3d9a0e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ By default investigate is mapped to `gK`. If you want to set up your
own mapping you should use something like this:

```
nnoremap <leader>K :call investigate#Investigate()<CR>
nnoremap <leader>K :call investigate#Investigate('n')<CR>
vnoremap <leader>K :call investigate#Investigate('v')<CR>
```

With this mapping, using <leader>K when your cursor is on a specific
Expand Down
21 changes: 19 additions & 2 deletions autoload/investigate.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,32 @@ function! s:BuildCommand(filetype, word)
endfunction
" }}}

" Get selected text from buffer ------ {{{
function! s:get_selected_text()
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
let text = substitute(join(lines, "\n"), '[\n\r]\+', ' ', 'g')
return substitute(text, '^\s*\|\s*$', '', 'g')
endfunction
"}}}

" The actual open command for mapping ------ {{{
function! investigate#Investigate()
function! investigate#Investigate(mode)
let l:filetype = &filetype
if empty(l:filetype)
echomsg "You must set your filetype to look up documentation"
return
endif

let l:word = expand("<cword>")
if a:mode ==# 'n'
let l:word = expand("<cword>")
else
let l:word = s:get_selected_text()
endif

if empty(l:word)
echomsg "Put your cursor over a word to look up it's documentation"
return
Expand Down
3 changes: 2 additions & 1 deletion plugin/investigate.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ if filereadable(g:investigate_local_filename)
endif

if !hasmapto("investigate#Investigate()") && empty(mapcheck("gK", "n"))
nnoremap gK :call investigate#Investigate()<CR>
nnoremap gK :call investigate#Investigate('n')<CR>
vnoremap gK :call investigate#Investigate('v')<CR>
endif

0 comments on commit b3d9a0e

Please sign in to comment.