Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom keymaps #113

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions autoload/ag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,48 @@ if !exists("g:ag_lhandler")
let g:ag_lhandler="botright lopen"
endif

if !exists("g:ag_qmapping_func")
let g:ag_qmapping_func="call ag#AgSetDefaultMappings('c')" " we're using the quickfix window
endif

if !exists("g:ag_lmapping_func")
let g:ag_lmapping_func="call ag#AgSetDefaultMappings('l')" " we're using the location list
endif

if !exists("g:ag_mapping_message")
let g:ag_mapping_message=1
endif

if !exists("g:ag_mapping_message_text")
let g:ag_mapping_message_text="ag.vim keys: q=quit <cr>/e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same"
endif

if !exists("g:ag_working_path_mode")
let g:ag_working_path_mode = 'c'
endif

function! ag#AgSetDefaultMappings(matches_window_prefix)
nnoremap <silent> <buffer> h <C-W><CR><C-w>K
nnoremap <silent> <buffer> H <C-W><CR><C-w>K<C-w>b
nnoremap <silent> <buffer> o <CR>
nnoremap <silent> <buffer> t <C-w><CR><C-w>T
nnoremap <silent> <buffer> T <C-w><CR><C-w>TgT<C-W><C-W>
nnoremap <silent> <buffer> v <C-w><CR><C-w>H<C-W>b<C-W>J<C-W>t

exe 'nnoremap <silent> <buffer> e <CR><C-w><C-w>:' . a:matches_window_prefix .'close<CR>'
exe 'nnoremap <silent> <buffer> go <CR>:' . a:matches_window_prefix . 'open<CR>'
exe 'nnoremap <silent> <buffer> q :' . a:matches_window_prefix . 'close<CR>'

exe 'nnoremap <silent> <buffer> gv :let b:height=winheight(0)<CR><C-w><CR><C-w>H:' . a:matches_window_prefix . 'open<CR><C-w>J:exe printf(":normal %d\<lt>c-w>_", b:height)<CR>'
" Interpretation:
" :let b:height=winheight(0)<CR> Get the height of the quickfix/location list window
" <CR><C-w> Open the current item in a new split
" <C-w>H Slam the newly opened window against the left edge
" :copen<CR> -or- :lopen<CR> Open either the quickfix window or the location list (whichever we were using)
" <C-w>J Slam the quickfix/location list window against the bottom edge
" :exe printf(":normal %d\<lt>c-w>_", b:height)<CR> Restore the quickfix/location list window's height from before we opened the match
endfunction

function! ag#AgBuffer(cmd, args)
let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)')
let l:files = []
Expand Down Expand Up @@ -128,11 +162,11 @@ function! ag#Ag(cmd, args)
if a:cmd =~# '^l' && l:match_count
exe g:ag_lhandler
let l:apply_mappings = g:ag_apply_lmappings
let l:matches_window_prefix = 'l' " we're using the location list
let l:ag_mapping_func = g:ag_lmapping_func
elseif l:match_count
exe g:ag_qhandler
let l:apply_mappings = g:ag_apply_qmappings
let l:matches_window_prefix = 'c' " we're using the quickfix window
let l:ag_mapping_func = g:ag_qmapping_func
endif

" If highlighting is on, highlight the search keyword.
Expand All @@ -145,28 +179,10 @@ function! ag#Ag(cmd, args)

if l:match_count
if l:apply_mappings
nnoremap <silent> <buffer> h <C-W><CR><C-w>K
nnoremap <silent> <buffer> H <C-W><CR><C-w>K<C-w>b
nnoremap <silent> <buffer> o <CR>
nnoremap <silent> <buffer> t <C-w><CR><C-w>T
nnoremap <silent> <buffer> T <C-w><CR><C-w>TgT<C-W><C-W>
nnoremap <silent> <buffer> v <C-w><CR><C-w>H<C-W>b<C-W>J<C-W>t

exe 'nnoremap <silent> <buffer> e <CR><C-w><C-w>:' . l:matches_window_prefix .'close<CR>'
exe 'nnoremap <silent> <buffer> go <CR>:' . l:matches_window_prefix . 'open<CR>'
exe 'nnoremap <silent> <buffer> q :' . l:matches_window_prefix . 'close<CR>'

exe 'nnoremap <silent> <buffer> gv :let b:height=winheight(0)<CR><C-w><CR><C-w>H:' . l:matches_window_prefix . 'open<CR><C-w>J:exe printf(":normal %d\<lt>c-w>_", b:height)<CR>'
" Interpretation:
" :let b:height=winheight(0)<CR> Get the height of the quickfix/location list window
" <CR><C-w> Open the current item in a new split
" <C-w>H Slam the newly opened window against the left edge
" :copen<CR> -or- :lopen<CR> Open either the quickfix window or the location list (whichever we were using)
" <C-w>J Slam the quickfix/location list window against the bottom edge
" :exe printf(":normal %d\<lt>c-w>_", b:height)<CR> Restore the quickfix/location list window's height from before we opened the match

if g:ag_mapping_message && l:apply_mappings
echom "ag.vim keys: q=quit <cr>/e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same"
exe l:ag_mapping_func

if g:ag_mapping_message
echom g:ag_mapping_message_text
endif
endif
else
Expand Down
24 changes: 22 additions & 2 deletions doc/ag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,36 @@ window is opened, or what size it is. Example: >
let g:ag_qhandler="copen 20"
<

*g:ag_lmapping_func*
A custom command used to set key mappings for the location list. Default:
"call ag#AgSetDefaultMappings('l')". Example: >
let g:ag_qhandler="call AgSetMyCustomMappings()"
<


*g:ag_qmapping_func*
A custom command used to set key mappings for the quickfix window. Default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use a map, like ctrlp does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against doing that, but wouldn't that require two keymaps? One for quickfix window mode and one for location list mode? The current implementation allows for a function that can take arguments and avoid duplicate code.

Again, I'm not against switching over to something like that, since ack.vim seems to do a similar thing. I'm actually interested in the logistics.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have three optional sections. Global mappings, location list
mappings, and quickfix mappings. The most specific mappings would take
precedence, falling back to default values if we didn't find a specific
mapping or group of mappings.
On Oct 14, 2015 19:27, "Sai Kalidindi" notifications@github.com wrote:

In doc/ag.txt
#113 (comment):

@@ -138,16 +138,36 @@ window is opened, or what size it is. Example: >
let g:ag_qhandler="copen 20"
<

  •                                                       _g:ag_lmapping_func_
    
    +A custom command used to set key mappings for the location list. Default:
    +"call ag#AgSetDefaultMappings('l')". Example: >
  • let g:ag_qhandler="call AgSetMyCustomMappings()"
    +<
  •                                                       _g:ag_qmapping_func_
    
    +A custom command used to set key mappings for the quickfix window. Default:

I'm not against doing that, but wouldn't that require two keymaps? One for
quickfix window mode and one for location list mode? The current
implementation allows for a function that can take arguments and avoid
duplicate code.

Again, I'm not against switching over to something like that, since
ack.vim seems to do a similar thing. I'm actually interested in the
logistics.


Reply to this email directly or view it on GitHub
https://github.com/rking/ag.vim/pull/113/files#r42071618.

"call ag#AgSetDefaultMappings('c')". Example: >
let g:ag_qhandler="call AgSetMyCustomMappings()"
<
*g:ag_mapping_message*
Whether or not to show the message explaining the extra mappings that are
added to the results list this plugin populates. This message is not shown if
the mappings are not applied (see |g:ag_apply_qmappings| and
|g:ag_apply_lmappings| for more info. Default 1. Example: >
|g:ag_apply_lmappings| for more info). Default 1. Example: >
let g:ag_mapping_message=0
<

*g:ag_mapping_message_text*
A custom message string to display when the results list window opens
(see |g:ag_mapping_message| and for more info). Default "ag.vim keys: q=quit
<cr>/e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same".
Example: >
let g:ag_mapping_message="Press q to close results list"
<

==============================================================================
MAPPINGS *ag-mappings*
DEFAULT MAPPINGS *ag-mappings*

The following keyboard shortcuts are available in the quickfix window:

Expand Down