changeset 240:3a6b11d16a2a

Simpler registration for CtrlP extensions.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 12 Dec 2014 16:53:19 -0800
parents ec74791ee042
children e869b2f99c75
files .hgsubstate vim/autoload/ctrlp/autoignore.vim vim/autoload/ctrlp/projectjump.vim vim/autoload/ctrlpext/autoignore.vim vim/vimrc
diffstat 5 files changed, 201 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/.hgsubstate	Thu Dec 11 23:38:57 2014 -0800
+++ b/.hgsubstate	Fri Dec 12 16:53:19 2014 -0800
@@ -5,7 +5,7 @@
 db3707cbd8706f4bb054959ecc5cee82ac45687b vim/bundle/badwolf
 81c6dd7ce3169e5ad9ba92422ba6e1ce5b074e36 vim/bundle/colorschemes
 9c685131a5facfa0d643feca3a61b41c007d8170 vim/bundle/commentary
-84a3a7e264d1079d3abf5f26c78b6df082be35db vim/bundle/ctrlp
+5d2aa8522dfd73a699b77128a310535d9d462061 vim/bundle/ctrlp
 55de0be306844ba8ad63c6e910e9c8f8234a209e vim/bundle/easymotion
 2c8461db084d205903a792a23163faa546f143c9 vim/bundle/fugitive
 eb9fc8676b8959c3c2c95bf6b6e8f0f44317c5c0 vim/bundle/gundo
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vim/autoload/ctrlp/autoignore.vim	Fri Dec 12 16:53:19 2014 -0800
@@ -0,0 +1,108 @@
+" =============================================================================
+" File:          autoload/ctrlp/autoignore.vim
+" Description:   Auto-ignore Extension
+" Author:        Ludovic Chabant <github.com/ludovicchabant>
+" =============================================================================
+
+
+" Global Settings {{{
+
+if !exists('g:ctrlp_autoignore_debug')
+    let g:ctrlp_autoignore_debug = 0
+endif
+
+if !exists('g:ctrlp_autoignore_trace')
+    let g:ctrlp_autoignore_trace = 0
+endif
+
+if exists('g:ctrlp_autoignore_loaded') && g:ctrlp_autoignore_loaded
+            \ && !g:ctrlp_autoignore_debug
+    finish
+endif
+let g:ctrlp_autoignore_loaded = 1
+
+" }}}
+
+" Initialization {{{
+
+if !exists('g:ctrlp_custom_ignore')
+    let g:ctrlp_custom_ignore = {}
+endif
+let g:ctrlp_custom_ignore['func'] = 'ctrlp#autoignore#ignore'
+let g:ctrlp_custom_ignore['func-init'] = 'ctrlp#autoignore#ignore_init'
+
+" }}}
+
+" Internals {{{
+
+function! s:trace(message) abort
+    if g:ctrlp_autoignore_trace
+        echom "ctrlp_autoignore: " . a:message
+    endif
+endfunction
+
+let s:proj_cache = {}
+let s:active_patterns = []
+
+function! s:load_project_patterns(root_dir) abort
+    let l:ign_path = a:root_dir . '/.ctrlpignore'
+    if !filereadable(l:ign_path)
+        call s:trace("No pattern file at: " . l:ign_path)
+        return []
+    endif
+    let l:patterns = []
+    let l:lines = readfile(l:ign_path)
+    for line in l:lines
+        if match(line, '\v^\s*$') >= 0 || match(line, '\v^\s*#') >= 0
+            continue
+        endif
+        let l:matches = matchlist(line, '\v^((dir|file|link)\:)?(.*)')
+        let l:mtype = l:matches[2]
+        let l:mpat = l:matches[3]
+        call add(l:patterns, {'type': l:mtype, 'pat': l:mpat})
+    endfor
+    call s:trace("Loaded " . len(l:patterns) . " patterns from: " . l:ign_path)
+    return l:patterns
+endfunction
+
+function! s:get_project_patterns(root_dir) abort
+    let l:patterns = get(s:proj_cache, a:root_dir)
+    if type(l:patterns) == type([])
+        return l:patterns
+    endif
+
+    call s:trace("Loading patterns for project: " . a:root_dir)
+    let l:loaded = s:load_project_patterns(a:root_dir)
+    let s:proj_cache[a:root_dir] = l:loaded
+    return l:loaded
+endfunction
+
+" The custom ignore function that CtrlP will be using in addition to
+" normal pattern-based matching.
+function! ctrlp#autoignore#ignore(item, type) abort
+    for pat in s:active_patterns
+        if pat['type'] == '' || pat['type'] == a:type
+            if match(a:item, pat['pat']) >= 0
+                return 1
+            endif
+        endif
+    endfor
+    return 0
+endfunction
+
+function! ctrlp#autoignore#ignore_init() abort
+    let l:root = getcwd()
+    let s:active_patterns = s:get_project_patterns(l:root)
+endfunction
+
+" List patterns for a given project's root.
+function! ctrlp#autoignore#get_patterns(root_dir) abort
+    let l:patterns = s:get_project_patterns(a:root_dir)
+    for pat in l:patterns
+        let l:prefix = pat['type'] == '' ? '(all)' : pat['type']
+        echom l:prefix . ':' . pat['pat']
+    endfor
+endfunction
+
+" }}}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vim/autoload/ctrlp/projectjump.vim	Fri Dec 12 16:53:19 2014 -0800
@@ -0,0 +1,88 @@
+" =============================================================================
+" File:          autoload/ctrlp/projectjump.vim
+" Description:   Project Jumper Extension
+" Author:        Ludovic Chabant <github.com/ludovicchabant>
+" =============================================================================
+
+
+" Global Settings {{{
+
+if !exists('g:ctrlp_projectjump_debug')
+    let g:ctrlp_projectjump_debug = 0
+endif
+
+if !exists('g:ctrlp_projectjump_trace')
+    let g:ctrlp_projectjump_trace = 0
+endif
+
+if exists('g:ctrlp_projectjump_loaded') && g:ctrlp_projectjump_loaded
+            \ && !g:ctrlp_projectjump_debug
+    finish
+endif
+let g:ctrlp_projectjump_loaded = 1
+
+if !exists('g:ctrlp_projectjump_roots')
+    let g:ctrlp_projectjump_roots = []
+endif
+
+" }}}
+
+" Init {{{
+
+if !exists('g:ctrlp_ext_vars') 
+    let g:ctrlp_ext_vars = []
+endif
+call add(g:ctrlp_ext_vars, {
+    \ 'init': 'ctrlp#projectjump#init()',
+    \ 'exit': 'ctrlp#projectjump#exit()',
+    \ 'accept': 'ctrlp#projectjump#accept',
+    \ 'lname': 'projectjump',
+    \ 'sname': 'prjmp',
+    \ 'type': 'project'
+    \ })
+let s:ext_id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
+
+command! CtrlPProjectJump call ctrlp#init(s:ext_id)
+
+" }}}
+
+" Callbacks {{{
+
+function! ctrlp#projectjump#init() abort
+    let l:project_roots = []
+    for prj in g:ctrlp_projectjump_roots
+        let l:prj_type = get(prj, 'type', 'project')
+        if l:prj_type == 'project'
+            call add(l:project_roots, prj['path'])
+        elseif l:prj_type == 'parent'
+            let l:subdirs = glob(prj['path'], 0, 1)
+            for sd in l:subdirs
+                call add(l:project_roots, sd)
+            endfor
+        else
+            echoerr "CtrlPProjectJump: Unsupported project root type: ".l:prj_type
+        endif
+    endfor
+    return l:project_roots
+endfunction
+
+function! ctrlp#projectjump#accept(mode, str) abort
+    call ctrlp#exit()
+    call ctrlp#init(0, {'dir': a:str})
+endfunction
+
+function! ctrlp#projectjump#exit() abort
+endfunction
+
+" }}}
+
+" Internals {{{
+
+function! s:trace(message) abort
+    if g:ctrlp_projectjump_trace
+        echom "ctrlp_projectjump: " . a:message
+    endif
+endfunction
+
+" }}}
+
--- a/vim/autoload/ctrlpext/autoignore.vim	Thu Dec 11 23:38:57 2014 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,111 +0,0 @@
-" =============================================================================
-" File:          autoload/ctrlpext/autoignore.vim
-" Description:   Auto-ignore Extension
-" Author:        Ludovic Chabant <github.com/ludovicchabant>
-" =============================================================================
-
-
-" Global Settings {{{
-
-if !exists('g:ctrlp_autoignore_debug')
-    let g:ctrlp_autoignore_debug = 0
-endif
-
-if !exists('g:ctrlp_autoignore_trace')
-    let g:ctrlp_autoignore_trace = 0
-endif
-
-if exists('g:ctrlp_autoignore_loaded') && g:ctrlp_autoignore_loaded
-            \ && !g:ctrlp_autoignore_debug
-    finish
-endif
-let g:ctrlp_autoignore_loaded = 1
-
-" }}}
-
-" Autoload functions {{{
-
-" Call this in your `vimrc` to enable auto-ignore.
-function! ctrlpext#autoignore#init() abort
-    if !exists('g:ctrlp_custom_ignore')
-        let g:ctrlp_custom_ignore = {}
-    endif
-    let g:ctrlp_custom_ignore['func'] = 'ctrlpext#autoignore#ignore'
-    let g:ctrlp_custom_ignore['func-init'] = 'ctrlpext#autoignore#ignore_init'
-endfunction
-
-" List patterns for a given project's root.
-function! ctrlpext#autoignore#get_patterns(root_dir) abort
-    let l:patterns = s:get_project_patterns(a:root_dir)
-    for pat in l:patterns
-        let l:prefix = pat['type'] == '' ? '(all)' : pat['type']
-        echom l:prefix . ':' . pat['pat']
-    endfor
-endfunction
-
-" }}}
-
-" Internals {{{
-
-function! s:trace(message) abort
-    if g:ctrlp_autoignore_trace
-        echom "ctrlp_autoignore: " . a:message
-    endif
-endfunction
-
-let s:proj_cache = {}
-let s:active_patterns = []
-
-function! s:load_project_patterns(root_dir) abort
-    let l:ign_path = a:root_dir . '/.ctrlpignore'
-    if !filereadable(l:ign_path)
-        call s:trace("No pattern file at: " . l:ign_path)
-        return []
-    endif
-    let l:patterns = []
-    let l:lines = readfile(l:ign_path)
-    for line in l:lines
-        if match(line, '\v^\s*$') >= 0 || match(line, '\v^\s*#') >= 0
-            continue
-        endif
-        let l:matches = matchlist(line, '\v^((dir|file|link)\:)?(.*)')
-        let l:mtype = l:matches[2]
-        let l:mpat = l:matches[3]
-        call add(l:patterns, {'type': l:mtype, 'pat': l:mpat})
-    endfor
-    call s:trace("Loaded " . len(l:patterns) . " patterns from: " . l:ign_path)
-    return l:patterns
-endfunction
-
-function! s:get_project_patterns(root_dir) abort
-    let l:patterns = get(s:proj_cache, a:root_dir)
-    if type(l:patterns) == type([])
-        return l:patterns
-    endif
-
-    call s:trace("Loading patterns for project: " . a:root_dir)
-    let l:loaded = s:load_project_patterns(a:root_dir)
-    let s:proj_cache[a:root_dir] = l:loaded
-    return l:loaded
-endfunction
-
-" The custom ignore function that CtrlP will be using in addition to
-" normal pattern-based matching.
-function! ctrlpext#autoignore#ignore(item, type) abort
-    for pat in s:active_patterns
-        if pat['type'] == '' || pat['type'] == a:type
-            if match(a:item, pat['pat']) >= 0
-                return 1
-            endif
-        endif
-    endfor
-    return 0
-endfunction
-
-function! ctrlpext#autoignore#ignore_init() abort
-    let l:root = getcwd()
-    let s:active_patterns = s:get_project_patterns(l:root)
-endfunction
-
-" }}}
-
--- a/vim/vimrc	Thu Dec 11 23:38:57 2014 -0800
+++ b/vim/vimrc	Fri Dec 12 16:53:19 2014 -0800
@@ -234,10 +234,10 @@
 let g:ctrlp_clear_cache_on_ext = 0
 
 " Enable some cool extensions.
-let g:ctrlp_extensions = ['tag', 'buffertag', 'quickfix', 'mixed']
-
-" Initialize other custom extensions.
-call ctrlpext#autoignore#init()
+let g:ctrlp_extensions = [
+            \'tag', 'buffertag', 'quickfix', 'mixed',
+            \'autoignore', 'projectjump'
+            \]
 
 " }}}