]> gitweb.factorcode.org Git - factor.git/blob - misc/vim/plugin/factor.vim
Pull in new literals vocab to remove bootstrap circularity
[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=customlist,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! s:unique(list)
27     let dict = {}
28     for value in a:list
29         let dict[value] = 1
30     endfor
31     return sort(keys(dict))
32 endfunction
33
34 function! FactorCompleteVocab(arglead, cmdline, cursorpos)
35     let vocabs = []
36     let vocablead = substitute(a:arglead, "\\.", "/", "g")
37     for root in g:FactorVocabRoots
38         let vocabroot = FactorVocabRoot(root)
39         let newvocabs = globpath(vocabroot, vocablead . "*")
40         if newvocabs != ""
41             let newvocabsl = split(newvocabs, "\n")
42             let newvocabsl = filter(newvocabsl, 'getftype(v:val) == "dir"')
43             let newvocabsl = map(newvocabsl, 'substitute(v:val, "^\\V" . escape(vocabroot, "\\"), "\\1", "g")')
44             let vocabs += newvocabsl
45         endif
46     endfor
47     let vocabs = s:unique(vocabs)
48     let vocabs = map(vocabs, 'substitute(v:val, "/\\|\\\\", ".", "g")')
49     return vocabs
50 endfunction
51
52 function! FactorVocabFile(root, vocab)
53     let vocabpath = substitute(a:vocab, "\\.", "/", "g")
54     let vocabfile = FactorVocabRoot(a:root) . vocabpath . "/" . fnamemodify(vocabpath, ":t") . ".factor"
55     
56     if getftype(vocabfile) != ""
57         return vocabfile
58     else
59         return ""
60     endif
61 endfunction
62
63 function! GoToFactorVocab(vocab)
64     for root in g:FactorVocabRoots
65         let vocabfile = FactorVocabFile(root, a:vocab)
66         if vocabfile != ""
67             exe "edit " fnameescape(vocabfile)
68             return
69         endif
70     endfor
71     echo "Vocabulary " vocab " not found"
72 endfunction
73
74 function! FactorFileBase()
75     let filename = expand("%:r")
76     let filename = substitute(filename, "-docs", "", "")
77     let filename = substitute(filename, "-tests", "", "")
78     return filename
79 endfunction
80
81 function! GoToFactorVocabImpl()
82     exe "edit " fnameescape(FactorFileBase() . ".factor")
83 endfunction
84
85 function! GoToFactorVocabDocs()
86     exe "edit " fnameescape(FactorFileBase() . "-docs.factor")
87 endfunction
88
89 function! GoToFactorVocabTests()
90     exe "edit " fnameescape(FactorFileBase() . "-tests.factor")
91 endfunction