Commits

kien committed 751103a Draft

Use a custom completion function [...]

Improve filename and path escaping
Simplify some conditional expressions
Toggle &imdisable

  • Participants
  • Parent commits 646bf37

Comments (0)

Files changed (5)

autoload/ctrlp.vim

 	cal add(s:prtmaps['PrtBS()'], remove(s:prtmaps['PrtCurLeft()'], 0))
 en
 
-let s:lash = ctrlp#utils#lash()
-
 let s:compare_lim = 3000
 
 let s:ficounts = {}
 	let s:mxheight = max([s:mxheight, 1])
 	let s:glob = s:showhidden ? '.*\|*' : '*'
 	let s:igntype = empty(s:usrign) ? -1 : type(s:usrign)
+	let s:lash = ctrlp#utils#lash()
 	if s:keyloop
 		let [s:lazy, s:glbs['imd']] = [0, 0]
 	en
 endf
 
 fu! ctrlp#clra()
-	let cache_dir = ctrlp#utils#cachedir()
-	if isdirectory(cache_dir)
-		let cache_files = split(s:glbpath(cache_dir, '**', 1), "\n")
+	let cadir = ctrlp#utils#cachedir()
+	if isdirectory(cadir)
+		let cafiles = split(s:glbpath(s:fnesc(cadir, 'g', ','), '**', 1), "\n")
 		let eval = '!isdirectory(v:val) && fnamemodify(v:val, ":t") !~'
 			\ . ' ''\v^<cache>[.a-z]+$|\.log$'''
-		sil! cal map(filter(cache_files, eval), 'delete(v:val)')
+		sil! cal map(filter(cafiles, eval), 'delete(v:val)')
 	en
 	cal ctrlp#clr()
 endf
 		let [lscmd, s:initcwd, g:ctrlp_allfiles] = [s:lsCmd(), s:dyncwd, []]
 		" Get the list of files
 		if empty(lscmd)
-			cal s:GlobPath(s:dyncwd, 0)
+			cal s:GlobPath(s:fnesc(s:dyncwd, 'g', ','), 0)
 		el
 			sil! cal ctrlp#progress('Indexing...')
 			try | cal s:UserCmd(lscmd)
 	cal extend(g:ctrlp_allfiles, dnf[1])
 	if !empty(dnf[0]) && !s:maxf(len(g:ctrlp_allfiles)) && depth <= s:maxdepth
 		sil! cal ctrlp#progress(len(g:ctrlp_allfiles), 1)
-		cal s:GlobPath(join(dnf[0], ','), depth)
+		cal s:GlobPath(join(map(dnf[0], 's:fnesc(v:val, "g", ",")'), ','), depth)
 	en
 endf
 
 		if s:findroot(s:dyncwd, cmd[0], 0, 1) == []
 			retu len(cmd) == 3 ? cmd[2] : ''
 		en
-		let s:vcscmd = s:lash == '\' ? 1 : 0
+		let s:vcscmd = s:lash == '\'
 		retu cmd[1]
 	elsei type(cmd) == 4 && ( has_key(cmd, 'types') || has_key(cmd, 'fallback') )
 		let fndroot = []
 		for pair in cmdtypes
 			if pair[0] == fndroot[0] | brea | en
 		endfo
-		let s:vcscmd = s:lash == '\' ? 1 : 0
+		let s:vcscmd = s:lash == '\'
 		retu pair[1]
 	en
 endf
 
 fu! s:ToggleKeyLoop()
 	let s:keyloop = !s:keyloop
+	if exists('+imd')
+		let &imd = !s:keyloop
+	en
 	if s:keyloop
 		let [&ut, s:lazy] = [0, 0]
 		cal s:KeyLoop()
 		if conds[nopt]
 			if !buflisted(bufnr) | cal s:openfile('bad', va, '', 0) | en
 		el
-			cal s:openfile(cmd, useb ? bufnr : va, tail, ic == 1 ? 1 : 0)
+			cal s:openfile(cmd, useb ? bufnr : va, tail, ic == 1)
 			if jf | if ic == 1
 				let crpos = [tabpagenr(), winnr()]
 			el
 fu! s:dircompl(be, sd)
 	if a:sd == '' | retu [] | en
 	let [be, sd] = a:be == '' ? [s:dyncwd, a:sd] : [a:be, a:be.s:lash(a:be).a:sd]
-	let dirs = ctrlp#rmbasedir(split(globpath(be, a:sd.'*/'), "\n"))
+	let dirs = ctrlp#rmbasedir(split(globpath(s:fnesc(be, 'g', ','), a:sd.'*/'), "\n"))
 	cal filter(dirs, '!match(v:val, escape(sd, ''~$.\''))'
 		\ . ' && v:val !~ ''\v(^|[\/])\.{1,2}[\/]$''')
 	retu dirs
 	endfo
 	retu cmn
 endf
+
+fu! ctrlp#dircompl(...)
+	let [hsl, str] = [match(a:1, '[\/]'), '']
+	let par = substitute(a:1, '[^\/]*$', '', '')
+	let path = !hsl ? par : hsl > 0 ? getcwd().s:lash().par : getcwd()
+	for dir in split(globpath(s:fnesc(path, 'g', ','), '*/'), '\n')
+		let str .= par.split(dir, '[\/]')[-1]."\n"
+	endfo
+	retu str
+endf
 " Misc {{{3
 fu! s:headntail(str)
 	let parts = split(a:str, '[\/]\ze[^\/]\+[\/:]\?$')
 fu! s:findroot(curr, mark, depth, type)
 	let [depth, fnd] = [a:depth + 1, 0]
 	if type(a:mark) == 1
-		let fnd = s:glbpath(a:curr, a:mark, 1) != ''
+		let fnd = s:glbpath(s:fnesc(a:curr, 'g', ','), a:mark, 1) != ''
 	elsei type(a:mark) == 3
 		for markr in a:mark
-			if s:glbpath(a:curr, markr, 1) != '' | let fnd = 1 | brea | en
+			if s:glbpath(s:fnesc(a:curr, 'g', ','), markr, 1) != ''
+				let fnd = 1
+				brea
+			en
 		endfo
 	en
 	if fnd
 
 fu! ctrlp#setdir(path, ...)
 	let cmd = a:0 ? a:1 : 'lc!'
-	sil! exe cmd ctrlp#fnesc(a:path)
+	sil! exe cmd s:fnesc(a:path, 'c')
 	let [s:crfilerel, s:dyncwd] = [fnamemodify(s:crfile, ':.'), getcwd()]
 endf
 " Fallbacks {{{3
 fu! s:glbpath(...)
-	let cond = v:version > 702 || ( v:version == 702 && has('patch051') )
-	retu call('globpath', cond ? a:000 : a:000[:1])
+	retu call('ctrlp#utils#globpath', a:000)
 endf
 
-fu! ctrlp#fnesc(path)
-	retu exists('*fnameescape') ? fnameescape(a:path) : escape(a:path, " %#*?|<\"\n")
+fu! s:fnesc(...)
+	retu call('ctrlp#utils#fnesc', a:000)
 endf
 
 fu! ctrlp#setlcdir()
 endf
 
 fu! s:migemo(str)
-	let str = a:str
-	let dict = s:glbpath(&rtp, printf("dict/%s/migemo-dict", &enc), 1)
+	let [str, rtp] = [a:str, s:fnesc(&rtp, 'g')]
+	let dict = s:glbpath(rtp, printf("dict/%s/migemo-dict", &enc), 1)
 	if !len(dict)
-		let dict = s:glbpath(&rtp, "dict/migemo-dict", 1)
+		let dict = s:glbpath(rtp, "dict/migemo-dict", 1)
 	en
 	if len(dict)
 		let [tokens, str, cmd] = [split(str, '\s'), '', 'cmigemo -v -w %s -d %s']
 endf
 
 fu! s:maxf(len)
-	retu s:maxfiles && a:len > s:maxfiles ? 1 : 0
+	retu s:maxfiles && a:len > s:maxfiles
 endf
 
 fu! s:regexfilter(str)
 	en
 	let cmd = cmd =~ '^tab' ? ctrlp#tabcount().cmd : cmd
 	let j2l = a:0 && a:1[0] ? a:1[1] : 0
-	exe cmd.( a:0 && a:1[0] ? '' : a:tail ) ctrlp#fnesc(a:fid)
+	exe cmd.( a:0 && a:1[0] ? '' : a:tail ) s:fnesc(a:fid, 'f')
 	if j2l
 		cal ctrlp#j2l(j2l)
 	en
 	retu !empty(s:lines) ? s:lines[line('.') - 1] : ''
 endf
 
+fu! ctrlp#getmarkedlist()
+	retu exists('s:marked') ? values(s:marked) : []
+endf
+
 fu! ctrlp#exit()
 	cal s:PrtExit()
 endf

autoload/ctrlp/dir.vim

 	let nr = len(g:ctrlp_alldirs)
 	if !empty(dirs) && !s:max(nr, s:maxfiles) && depth <= s:maxdepth
 		sil! cal ctrlp#progress(nr)
+		cal map(dirs, 'ctrlp#utils#fnesc(v:val, "g", ",")')
 		cal s:globdirs(join(dirs, ','), depth)
 	en
 endf
 
 fu! s:max(len, max)
-	retu a:max && a:len > a:max ? 1 : 0
+	retu a:max && a:len > a:max
 endf
 
 fu! s:nocache()
 	let cafile = cadir.ctrlp#utils#lash().ctrlp#utils#cachefile('dir')
 	if g:ctrlp_newdir || s:nocache() || !filereadable(cafile)
 		let [s:initcwd, g:ctrlp_alldirs] = [s:cwd, []]
-		cal s:globdirs(s:cwd, 0)
+		cal s:globdirs(ctrlp#utils#fnesc(s:cwd, 'g', ','), 0)
 		cal ctrlp#rmbasedir(g:ctrlp_alldirs)
 		if len(g:ctrlp_alldirs) <= s:compare_lim
 			cal sort(g:ctrlp_alldirs, 'ctrlp#complen')

autoload/ctrlp/rtscript.vim

 let s:filecounts = {}
 " Utilities {{{1
 fu! s:nocache()
-	retu !s:caching || ( s:caching > 1 && get(s:filecounts, s:cwd) < s:caching )
+	retu g:ctrlp_newrts ||
+		\ !s:caching || ( s:caching > 1 && get(s:filecounts, s:cwd) < s:caching )
 endf
 " Public {{{1
 fu! ctrlp#rtscript#init(caching)
 	let [s:caching, s:cwd] = [a:caching, getcwd()]
-	if g:ctrlp_newrts || s:nocache()
-		\ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[0] == &rtp )
+	if s:nocache() ||
+		\ !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[0] == &rtp )
 		sil! cal ctrlp#progress('Indexing...')
-		let entries = split(globpath(&rtp, '**/*.*'), "\n")
+		let entries = split(globpath(ctrlp#utils#fnesc(&rtp, 'g'), '**/*.*'), "\n")
 		cal filter(entries, 'count(entries, v:val) == 1')
 		let [entries, echoed] = [ctrlp#dirnfile(entries)[1], 1]
 	el
 		let [entries, results] = g:ctrlp_rtscache[2:3]
 	en
-	if g:ctrlp_newrts || s:nocache()
-		\ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, s:cwd] )
-		if !exists('echoed') | sil! cal ctrlp#progress('Processing...') | en
+	if s:nocache() ||
+		\ !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, s:cwd] )
+		if !exists('echoed')
+			sil! cal ctrlp#progress('Processing...')
+		en
 		let results = map(copy(entries), 'fnamemodify(v:val, '':.'')')
 	en
 	let [g:ctrlp_rtscache, g:ctrlp_newrts] = [[&rtp, s:cwd, entries, results], 0]

autoload/ctrlp/utils.vim

 fu! ctrlp#utils#lash()
 	retu &ssl || !exists('+ssl') ? '/' : '\'
 endf
-let s:lash = ctrlp#utils#lash()
 
 fu! s:lash(...)
 	retu ( a:0 ? a:1 : getcwd() ) !~ '[\/]$' ? s:lash : ''
 endf
 
 fu! ctrlp#utils#opts()
+	let s:lash = ctrlp#utils#lash()
 	let usrhome = $HOME.s:lash($HOME)
 	let cahome = exists('$XDG_CACHE_HOME') ? $XDG_CACHE_HOME : usrhome.'.cache'
-	let s:cache_dir = isdirectory(usrhome.'.ctrlp_cache')
+	let cadir = isdirectory(usrhome.'.ctrlp_cache')
 		\ ? usrhome.'.ctrlp_cache' : cahome.s:lash(cahome).'ctrlp'
 	if exists('g:ctrlp_cache_dir')
-		let s:cache_dir = expand(g:ctrlp_cache_dir, 1)
-		if isdirectory(s:cache_dir.s:lash(s:cache_dir).'.ctrlp_cache')
-			let s:cache_dir = s:cache_dir.s:lash(s:cache_dir).'.ctrlp_cache'
+		let cadir = expand(g:ctrlp_cache_dir, 1)
+		if isdirectory(cadir.s:lash(cadir).'.ctrlp_cache')
+			let cadir = cadir.s:lash(cadir).'.ctrlp_cache'
 		en
 	en
+	let s:cache_dir = cadir
 endf
 cal ctrlp#utils#opts()
+
+let s:wig_cond = v:version > 702 || ( v:version == 702 && has('patch051') )
 " Files and Directories {{{1
 fu! ctrlp#utils#cachedir()
 	retu s:cache_dir
 endf
 
 fu! ctrlp#utils#glob(...)
-	let cond = v:version > 702 || ( v:version == 702 && has('patch051') )
-	retu call('glob', cond ? a:000 : [a:1])
+	let path = ctrlp#utils#fnesc(a:1, 'g')
+	retu s:wig_cond ? glob(path, a:2) : glob(path)
+endf
+
+fu! ctrlp#utils#globpath(...)
+	retu call('globpath', s:wig_cond ? a:000 : a:000[:1])
+endf
+
+fu! ctrlp#utils#fnesc(path, type, ...)
+	if exists('*fnameescape')
+		if exists('+ssl')
+			if a:type == 'c'
+				let path = escape(a:path, '%#')
+			elsei a:type == 'f'
+				let path = fnameescape(a:path)
+			elsei a:type == 'g'
+				let path = escape(a:path, '?*')
+			en
+			let path = substitute(path, '[', '[[]', 'g')
+		el
+			let path = fnameescape(a:path)
+		en
+	el
+		if exists('+ssl')
+			if a:type == 'c'
+				let path = escape(a:path, '%#')
+			elsei a:type == 'f'
+				let path = escape(a:path, " \t\n%#*?|<\"")
+			elsei a:type == 'g'
+				let path = escape(a:path, '?*')
+			en
+			let path = substitute(path, '[', '[[]', 'g')
+		el
+			let path = escape(a:path, " \t\n*?[{`$\\%#'\"|!<")
+		en
+	en
+	retu a:0 ? escape(path, a:1) : path
 endf
 "}}}
 
 if !exists('g:ctrlp_map') | let g:ctrlp_map = '<c-p>' | en
 if !exists('g:ctrlp_cmd') | let g:ctrlp_cmd = 'CtrlP' | en
 
-com! -n=? -com=dir CtrlP         cal ctrlp#init(0, { 'dir': <q-args> })
-com! -n=? -com=dir CtrlPMRUFiles cal ctrlp#init(2, { 'dir': <q-args> })
+com! -n=? -com=custom,ctrlp#dircompl CtrlP
+	\ cal ctrlp#init(0, { 'dir': <q-args> })
+
+com! -n=? -com=custom,ctrlp#dircompl CtrlPMRUFiles
+	\ cal ctrlp#init(2, { 'dir': <q-args> })
 
 com! -bar CtrlPBuffer   cal ctrlp#init(1)
 com! -n=? CtrlPLastMode cal ctrlp#init(-1, { 'args': <q-args> })
 com! -bar CtrlPTag      cal ctrlp#init(ctrlp#tag#id())
 com! -bar CtrlPQuickfix cal ctrlp#init(ctrlp#quickfix#id())
 
-com! -n=? -com=dir CtrlPDir
+com! -n=? -com=custom,ctrlp#dircompl CtrlPDir
 	\ cal ctrlp#init(ctrlp#dir#id(), { 'dir': <q-args> })
 
 com! -n=? -com=buffer CtrlPBufTag
 com! -bar CtrlPMixed       cal ctrlp#init(ctrlp#mixed#id())
 com! -bar CtrlPBookmarkDir cal ctrlp#init(ctrlp#bookmarkdir#id())
 
-com! -n=? -com=dir CtrlPBookmarkDirAdd
+com! -n=? -com=custom,ctrlp#dircompl CtrlPBookmarkDirAdd
 	\ cal ctrlp#call('ctrlp#bookmarkdir#add', <q-args>)
 
 " vim:ts=2:sw=2:sts=2