citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

clay.vim (4364B)


      1 nmap <Leader>cl :LibClayModule<SPACE>
      2 nmap <Leader>cn :LibClayNewModule<SPACE>
      3 nmap <Leader>cN :LibClayNewModuleDir<SPACE>
      4 nmap <Leader>c0 :LibClayAlternate 0<CR>
      5 nmap <Leader>c1 :LibClayAlternate 1<CR>
      6 nmap <Leader>c2 :LibClayAlternate 2<CR>
      7 nmap <Leader>c3 :LibClayAlternate 3<CR>
      8 nmap <Leader>c4 :LibClayAlternate 4<CR>
      9 nmap <Leader>c5 :LibClayAlternate 5<CR>
     10 nmap <Leader>c6 :LibClayAlternate 6<CR>
     11 nmap <Leader>c7 :LibClayAlternate 7<CR>
     12 nmap <Leader>c8 :LibClayAlternate 8<CR>
     13 nmap <Leader>c9 :LibClayAlternate 9<CR>
     14 
     15 if !exists("g:LibClay")
     16     let g:LibClay = "/usr/local/lib/lib-clay"
     17 endif
     18 
     19 if !exists("g:LibClayAlternates")
     20     let g:LibClayAlternates = ["/usr/local/lib/lib-clay"]
     21 endif
     22 
     23 function! s:unique(list)
     24     let dict = {}
     25     for value in a:list
     26         let dict[value] = 1
     27     endfor
     28     return sort(keys(dict))
     29 endfunction
     30 
     31 function! ClayCompleteLibraryModule(arglead, cmdline, cursorpos)
     32     let modulesl = []
     33     let modulelead = substitute(a:arglead, "\\.", "/", "g")
     34     let modules = globpath(g:LibClay, modulelead . "*")
     35     if modules != ""
     36         let modulesl = split(modules, "\n")
     37         let modulesl = filter(modulesl, 'getftype(v:val) == "dir" || matchstr(v:val, "\\.clay$") != ""')
     38         let modulesl = map(modulesl, 'substitute(v:val, "\\(\\..*\\)\\?\\.clay$", "", "")')
     39         let modulesl = map(modulesl, 'substitute(v:val, "^\\V" . escape(g:LibClay, "\\") . "\\v[/\\\\]", "", "")')
     40         let modulesl = s:unique(modulesl)
     41         let modulesl = map(modulesl, 'substitute(v:val, "/\\|\\\\", ".", "g")')
     42     endif
     43     return modulesl
     44 endfunction
     45 
     46 command! -nargs=1 -complete=customlist,ClayCompleteLibraryModule LibClayModule :call GoToLibClayModule("<args>")
     47 command! -nargs=1 -complete=customlist,ClayCompleteLibraryModule LibClayNewModule :call CreateLibClayModule("<args>")
     48 command! -nargs=1 -complete=customlist,ClayCompleteLibraryModule LibClayNewModuleDir :call CreateLibClayModuleDir("<args>")
     49 command! -nargs=1 LibClayAlternate :call SelectLibClayAlternate(<args>)
     50 
     51 function! ClayModuleFileNames(path)
     52     let names = [a:path . ".clay"]
     53     let oses = ["unix", "windows", "linux", "macosx"]
     54     let cpus = ["x86", "ppc", "arm"]
     55     let bits = ["32", "64"]
     56     for bit in bits
     57         let names += [a:path . "." . bit . ".clay"]
     58     endfor
     59     for cpu in cpus
     60         let names += [a:path . "." . cpu . ".clay"]
     61         for bit in bits
     62             let names += [a:path . "." . cpu . "." . bit . ".clay"]
     63         endfor
     64     endfor
     65     for os in oses
     66         let names += [a:path . "." . os . ".clay"]
     67         for bit in bits
     68             let names += [a:path . "." . os . "." . bit . ".clay"]
     69         endfor
     70         for cpu in cpus
     71             let names += [a:path . "." . os . "." . cpu . ".clay"]
     72             for bit in bits
     73                 let names += [a:path . "." . os . "." . cpu . "." . bit . ".clay"]
     74             endfor
     75         endfor
     76     endfor
     77     return names
     78 endfunction
     79 
     80 function! FindClayModuleFile(path)
     81     let basename = substitute(a:path, "^.*[/\\\\]", "", "")
     82     let searchnames = ClayModuleFileNames(a:path)
     83     let searchnames += ClayModuleFileNames(a:path . "/" . basename)
     84     for name in searchnames
     85         if getftype(name) != ""
     86             return name
     87         endif
     88     endfor
     89     return ""
     90 endfunction
     91 
     92 function! GoToLibClayModule(module)
     93     let modulefile = FindClayModuleFile(g:LibClay . "/" . substitute(a:module, "\\.", "/", "g"))
     94     if modulefile == ""
     95         echo "Library module" modulefile "not found"
     96     else
     97         exe "edit " fnameescape(modulefile)
     98     endif
     99 endfunction
    100 
    101 function! SelectLibClayAlternate(n)
    102     echo "Setting g:LibClay to" get(g:LibClayAlternates, a:n, g:LibClay)
    103     let g:LibClay = get(g:LibClayAlternates, a:n, g:LibClay)
    104 endfunction
    105 
    106 function! CreateLibClayModule(module)
    107     let modulename = g:LibClay . "/" . substitute(a:module, "\\.", "/", "g") . ".clay"
    108     let modulepath = substitute(modulename, "[/\\\\][^/\\\\]*$", "", "")
    109     exe "silent !mkdir -p " shellescape(modulepath)
    110     exe "edit " fnameescape(modulename)
    111 endfunction
    112 
    113 function! CreateLibClayModuleDir(module)
    114     let modulepath = g:LibClay . "/" . substitute(a:module, "\\.", "/", "g")
    115     let basename = substitute(a:module, "^.*\\.", "", "")
    116     exe "silent !mkdir -p " shellescape(modulepath)
    117     exe "edit " fnameescape(modulepath . "/" . basename . ".clay")
    118 endfunction