Option for using custom file opening functions

Refs #232
This commit is contained in:
Kien N 2012-07-21 11:01:39 +07:00
parent 2927ce4eab
commit f14014d0a7
2 changed files with 76 additions and 5 deletions

View File

@ -71,6 +71,7 @@ let [s:pref, s:opts, s:new_opts] = ['g:ctrlp_', {
\ 'max_height': ['s:mxheight', 10],
\ 'max_history': ['s:maxhst', exists('+hi') ? &hi : 20],
\ 'mruf_default_order': ['s:mrudef', 0],
\ 'open_func': ['s:openfunc', {}],
\ 'open_multi': ['s:opmul', '1v'],
\ 'open_new_file': ['s:newfop', 'v'],
\ 'prompt_mappings': ['s:urprtmaps', 0],
@ -909,7 +910,11 @@ fu! s:AcceptSelection(mode)
en
if empty(line) | retu | en
" Do something with it
let actfunc = s:itemtype < 3 ? 'ctrlp#acceptfile' : s:getextvar('accept')
if s:openfunc != {} && has_key(s:openfunc, s:ctype)
let actfunc = s:openfunc[s:ctype]
el
let actfunc = s:itemtype < 3 ? 'ctrlp#acceptfile' : s:getextvar('accept')
en
cal call(actfunc, [a:mode, line])
endf
" - CreateNewFile() {{{1
@ -1008,6 +1013,8 @@ fu! s:OpenMulti(...)
endfo
cal s:remarksigns()
retu s:BuildPrompt(0)
elsei !has_marked && md == 'x'
retu call(s:openfunc[s:ctype], [md, line])
en
if md =~ '\v^c(ancel)?$' | retu | en
let nr = nr == '0' ? ( nopt ? '' : '1' ) : nr
@ -1185,16 +1192,16 @@ fu! ctrlp#statusline()
let max = len(tps) - 1
let nxt = tps[s:walker(max, s:itemtype, 1)][1]
let prv = tps[s:walker(max, s:itemtype, -1)][1]
let item = tps[s:itemtype][0]
let s:ctype = tps[s:itemtype][0]
let focus = s:Focus() ? 'prt' : 'win'
let byfname = s:byfname ? 'file' : 'path'
let marked = s:opmul != '0' ?
\ exists('s:marked') ? ' <'.s:dismrk().'>' : ' <->' : ''
if s:status != {}
let args = [focus, byfname, s:regexp, prv, item, nxt, marked]
let args = [focus, byfname, s:regexp, prv, s:ctype, nxt, marked]
let &l:stl = call(s:status['main'], args)
el
let item = '%#CtrlPMode1# '.item.' %*'
let item = '%#CtrlPMode1# '.s:ctype.' %*'
let focus = '%#CtrlPMode2# '.focus.' %*'
let byfname = '%#CtrlPMode1# '.byfname.' %*'
let regex = s:regexp ? '%#CtrlPMode2# regex %*' : ''
@ -1561,6 +1568,10 @@ fu! s:argmaps(md, i)
\ ['Create a New File', '/[r]eplace', ['r']],
\ ['Open Selected', '/[r]eplace/h[i]dden? Mark [a]ll', ['r', 'i', 'a']],
\ ]
if a:i == 2 && s:openfunc != {} && has_key(s:openfunc, s:ctype)
let roh[2][1] = '/[r]eplace/h[i]dden/e[x]ternal? Mark [a]ll'
let roh[2][2] = ['r', 'i', 'x', 'a']
en
let str = roh[a:i][0].': [t]ab/[v]ertical/[h]orizontal'.roh[a:i][1].'? '
retu s:choices(str, ['t', 'v', 'h'] + roh[a:i][2], 's:argmaps', [a:md, a:i])
endf

View File

@ -75,6 +75,7 @@ Overview:~
|ctrlp_mruf_case_sensitive| MRU files are case sensitive or not.
Advanced options:
|ctrlp_open_func| Use custom file opening functions.
|ctrlp_status_func| Change CtrlP's two statuslines.
|ctrlp_buffer_func| Call custom functions in the CtrlP buffer.
|ctrlp_match_func| Replace the built-in matching algorithm.
@ -454,6 +455,64 @@ MRU entries: >
----------------------------------------
Advanced options:~
*'g:ctrlp_open_func'*
Define a custom function to open the selected file: >
let g:ctrlp_open_func = {}
<
Example: >
let g:ctrlp_open_func = {
\ 'files' : 'Function_Name_1',
\ 'buffers' : 'Function_Name_2',
\ 'mru files' : 'Function_Name_3',
\ }
<
Structure of the functions: >
function! Function_Name(action, line)
" Arguments:
" |
" +- a:action : The opening action:
" | + 'e' : user pressed <cr> (default)
" | + 'h' : user pressed <c-x> (default)
" | + 'v' : user pressed <c-v> (default)
" | + 't' : user pressed <c-t> (default)
" | + 'x' : user used the <c-o> dialog (default) and chose
" | "e[x]ternal".
" |
" +- a:line : The selected line.
endfunction
<
Linux example: open HTML files in the default web browser when <c-t> is pressed
and in Vim otherwise >
function! HTMLOpenFunc(action, line)
if a:action == 't' && fnamemodify(a:line, ':e') =~? '^html\?$'
" Get the filename
let filename = fnameescape(fnamemodify(a:line, ':p'))
" Close CtrlP
call ctrlp#exit()
" Open the file
silent! execute '!xdg-open' filename
elseif a:action == 'x' && fnamemodify(a:line, ':e') !~? '^html\?$'
" Not a HTML file, simulate pressing <c-o> again
call feedkeys("\<c-o>")
else
" Use CtrlP's default file opening function
call call('ctrlp#acceptfile', [a:action, a:line])
endif
endfunction
let g:ctrlp_open_func = { 'files': 'HTMLOpenFunc' }
<
Note: this option does not apply when opening multiple files with <c-z>, <c-o>.
*'g:ctrlp_status_func'*
Use this to customize the statuslines for the CtrlP window: >
let g:ctrlp_status_func = {}
@ -1046,7 +1105,8 @@ Special thanks:~
===============================================================================
CHANGELOG *ctrlp-changelog*
+ New option: |g:ctrlp_tabpage_position|.
+ New options: |g:ctrlp_open_func|.
|g:ctrlp_tabpage_position|.
Before 2012/06/15~