]> gitweb.factorcode.org Git - factor.git/blob - misc/vim/plugin/factor.vim
completion for :FactorVocab vim function
[factor.git] / misc / vim / plugin / factor.vim
1 nmap <silent> <Leader>fi :FactorVocabImpl<CR>
2 nmap <silent> <Leader>fd :FactorVocabDocs<CR>
3 nmap <silent> <Leader>ft :FactorVocabTests<CR>
4
5 let g:FactorRoot = "/Users/joe/Documents/Code/others/factor"
6 let g:FactorVocabRoots = ["core", "basis", "extra", "work", "/Users/joe/Documents/Code/Factor"]
7
8 command! -nargs=1 -complete=custom,FactorCompleteVocab FactorVocab :call GoToFactorVocab("<args>")
9 command! FactorVocabImpl  :call GoToFactorVocabImpl()
10 command! FactorVocabDocs  :call GoToFactorVocabDocs()
11 command! FactorVocabTests :call GoToFactorVocabTests()
12
13 function! FactorVocabRoot(root)
14     let cwd = getcwd()
15     exe "lcd " fnameescape(g:FactorRoot)
16     let vocabroot = fnamemodify(a:root, ":p")
17     exe "lcd " fnameescape(cwd)
18     return vocabroot
19 endfunction
20
21 function! FactorCompleteVocab(arglead, cmdline, cursorpos)
22     let vocabs = ""
23     let vocablead = substitute(a:arglead, "\\.", "/", "g")
24     for root in g:FactorVocabRoots
25         let vocabroot = FactorVocabRoot(root)
26         let newvocabs = globpath(vocabroot, vocablead . "*")
27         if newvocabs != ""
28             let newvocabs = substitute(newvocabs, "\\(^\\|\\n\\)\\V" . escape(vocabroot, "\\"), "\\1", "g")
29             let newvocabs = substitute(newvocabs, "/\\|\\\\", ".", "g")
30             let vocabs .= newvocabs . "\n"
31         endif
32     endfor
33     return vocabs
34 endfunction
35
36 function! FactorVocabFile(root, vocab)
37     let vocabpath = substitute(a:vocab, "\\.", "/", "g")
38     let vocabfile = FactorVocabRoot(a:root) . vocabpath . "/" . fnamemodify(vocabpath, ":t") . ".factor"
39     
40     if getftype(vocabfile) != ""
41         return vocabfile
42     else
43         return ""
44     endif
45 endfunction
46
47 function! GoToFactorVocab(vocab)
48     for root in g:FactorVocabRoots
49         let vocabfile = FactorVocabFile(root, a:vocab)
50         if vocabfile != ""
51             exe "edit " fnameescape(vocabfile)
52             return
53         endif
54     endfor
55     echo "Vocabulary " vocab " not found"
56 endfunction
57
58 function! FactorFileBase()
59     let filename = expand("%:r")
60     let filename = substitute(filename, "-docs", "", "")
61     let filename = substitute(filename, "-tests", "", "")
62     return filename
63 endfunction
64
65 function! GoToFactorVocabImpl()
66     exe "edit " fnameescape(FactorFileBase() . ".factor")
67 endfunction
68
69 function! GoToFactorVocabDocs()
70     exe "edit " fnameescape(FactorFileBase() . "-docs.factor")
71 endfunction
72
73 function! GoToFactorVocabTests()
74     exe "edit " fnameescape(FactorFileBase() . "-tests.factor")
75 endfunction