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