]> gitweb.factorcode.org Git - factor.git/blob - misc/vim/plugin/factor.vim
1a27b5a1ca26822b3f10c4c72b1f1ad4f82aafe2
[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 nmap <Leader>fv :FactorVocab<SPACE>
5 nmap <Leader>fn :NewFactorVocab<SPACE>
6
7 if !exists("g:FactorRoot")
8     let g:FactorRoot = "~/factor"
9 endif
10
11 if !exists("g:FactorVocabRoots")
12     let g:FactorVocabRoots = ["core", "basis", "extra", "work"]
13 endif
14
15 if !exists("g:FactorNewVocabRoot")
16     let g:FactorNewVocabRoot = "work"
17 endif
18
19 command! -nargs=1 -complete=customlist,FactorCompleteVocab FactorVocab :call GoToFactorVocab("<args>")
20 command! -nargs=1 -complete=customlist,FactorCompleteVocab NewFactorVocab :call MakeFactorVocab("<args>")
21 command! FactorVocabImpl  :call GoToFactorVocabImpl()
22 command! FactorVocabDocs  :call GoToFactorVocabDocs()
23 command! FactorVocabTests :call GoToFactorVocabTests()
24
25 function! FactorVocabRoot(root)
26     let cwd = getcwd()
27     exe "lcd " fnameescape(g:FactorRoot)
28     let vocabroot = fnamemodify(a:root, ":p")
29     exe "lcd " fnameescape(cwd)
30     return vocabroot
31 endfunction
32
33 function! s:unique(list)
34     let dict = {}
35     for value in a:list
36         let dict[value] = 1
37     endfor
38     return sort(keys(dict))
39 endfunction
40
41 function! FactorCompleteVocab(arglead, cmdline, cursorpos)
42     let vocabs = []
43     let vocablead = substitute(a:arglead, "\\.", "/", "g")
44     for root in g:FactorVocabRoots
45         let vocabroot = FactorVocabRoot(root)
46         let newvocabs = globpath(vocabroot, vocablead . "*")
47         if newvocabs != ""
48             let newvocabsl = split(newvocabs, "\n")
49             let newvocabsl = filter(newvocabsl, 'getftype(v:val) == "dir"')
50             let newvocabsl = map(newvocabsl, 'substitute(v:val, "^\\V" . escape(vocabroot, "\\"), "\\1", "g")')
51             let vocabs += newvocabsl
52         endif
53     endfor
54     let vocabs = s:unique(vocabs)
55     let vocabs = map(vocabs, 'substitute(v:val, "/\\|\\\\", ".", "g")')
56     return vocabs
57 endfunction
58
59 function! FactorVocabFile(root, vocab, mustexist)
60     let vocabpath = substitute(a:vocab, "\\.", "/", "g")
61     let vocabfile = FactorVocabRoot(a:root) . vocabpath . "/" . fnamemodify(vocabpath, ":t") . ".factor"
62
63     if !a:mustexist || getftype(vocabfile) != ""
64         return vocabfile
65     else
66         return ""
67     endif
68 endfunction
69
70 function! GoToFactorVocab(vocab)
71     for root in g:FactorVocabRoots
72         let vocabfile = FactorVocabFile(root, a:vocab, 1)
73         if vocabfile != ""
74             exe "edit " fnameescape(vocabfile)
75             return
76         endif
77     endfor
78     echo "Vocabulary " vocab " not found"
79 endfunction
80
81 function! MakeFactorVocab(vocab)
82     let vocabfile = FactorVocabFile(g:FactorNewVocabRoot, a:vocab, 0)
83     echo vocabfile
84     let vocabdir = fnamemodify(vocabfile, ":h")
85     echo vocabdir
86     exe "!mkdir -p " shellescape(vocabdir)
87     exe "edit " fnameescape(vocabfile)
88 endfunction
89
90 function! FactorFileBase()
91     let filename = expand("%:r")
92     let filename = substitute(filename, "-docs", "", "")
93     let filename = substitute(filename, "-tests", "", "")
94     return filename
95 endfunction
96
97 function! GoToFactorVocabImpl()
98     exe "edit " fnameescape(FactorFileBase() . ".factor")
99 endfunction
100
101 function! GoToFactorVocabDocs()
102     exe "edit " fnameescape(FactorFileBase() . "-docs.factor")
103 endfunction
104
105 function! GoToFactorVocabTests()
106     exe "edit " fnameescape(FactorFileBase() . "-tests.factor")
107 endfunction