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