vim-fireplace/plugin/foreplay.vim

1017 lines
29 KiB
VimL
Raw Normal View History

2012-12-04 06:06:22 +00:00
" foreplay.vim - Clojure REPL tease
" Maintainer: Tim Pope <http://tpo.pe>
if exists("g:loaded_foreplay") || v:version < 700 || &cp
finish
endif
let g:loaded_foreplay = 1
" File type {{{1
augroup foreplay_file_type
autocmd!
autocmd BufNewFile,BufReadPost *.clj setfiletype clojure
augroup END
" }}}1
" Escaping {{{1
2012-12-04 06:06:22 +00:00
function! s:str(string)
return '"' . escape(a:string, '"\') . '"'
endfunction
2012-12-04 06:06:22 +00:00
" }}}1
" Completion {{{1
2012-12-26 00:01:51 +00:00
let s:jar_contents = {}
function! foreplay#jar_contents(path) abort
if !has_key(s:jar_contents, a:path) && executable('zipinfo')
let s:jar_contents[a:path] = split(system('zipinfo -1 '.shellescape(a:path)), "\n")
if v:shell_error
return []
endif
endif
return copy(get(s:jar_contents, a:path, []))
endfunction
2012-12-04 06:06:22 +00:00
function! foreplay#eval_complete(A, L, P) abort
let prefix = matchstr(a:A, '\%(.* \|^\)\%(#\=[\[{('']\)*')
let keyword = a:A[strlen(prefix) : -1]
return sort(map(foreplay#omnicomplete(0, keyword), 'prefix . v:val.word'))
endfunction
function! foreplay#ns_complete(A, L, P) abort
let matches = []
for dir in foreplay#client().path()
2012-12-26 00:01:51 +00:00
if dir =~# '\.jar$'
let files = filter(foreplay#jar_contents(dir), 'v:val =~# "\\.clj$"')
else
let files = split(glob(dir."/**/*.clj", 1), "\n")
call map(files, 'v:val[strlen(dir)+1 : -1]')
endif
let matches += files
2012-12-04 06:06:22 +00:00
endfor
return filter(map(matches, 's:tons(v:val)'), 'a:A ==# "" || a:A ==# v:val[0 : strlen(a:A)-1]')
endfunction
function! foreplay#omnicomplete(findstart, base) abort
if a:findstart
let line = getline('.')[0 : col('.')-2]
return col('.') - strlen(matchstr(line, '\k\+$')) - 1
else
try
let omnifier = '(fn [[k v]] (let [m (meta v)]' .
\ ' {:word k :menu (pr-str (:arglists m (symbol ""))) :info (str " " (:doc m)) :kind (if (:arglists m) "f" "v")}))'
let ns = foreplay#ns()
let [aliases, namespaces, maps] = foreplay#evalparse(
\ '[(ns-aliases '.s:qsym(ns).') (all-ns) '.
\ '(sort-by :word (map '.omnifier.' (ns-map '.s:qsym(ns).')))]')
if a:base =~# '^[^/]*/[^/]*$'
let ns = matchstr(a:base, '^.*\ze/')
let prefix = ns . '/'
let ns = get(aliases, ns, ns)
let keyword = matchstr(a:base, '.*/\zs.*')
let results = foreplay#evalparse(
\ '(sort-by :word (map '.omnifier.' (ns-publics '.s:qsym(ns).')))')
for r in results
let r.word = prefix . r.word
2012-12-04 06:06:22 +00:00
endfor
else
let keyword = a:base
let results = maps + map(sort(keys(aliases) + namespaces), '{"word": v:val."/", "kind": "t", "info": ""}')
2012-12-04 06:06:22 +00:00
endif
if type(results) == type([])
return filter(results, 'a:base ==# "" || a:base ==# v:val.word[0 : strlen(a:base)-1]')
2012-12-04 06:06:22 +00:00
else
return []
endif
catch /.*/
return []
endtry
endif
endfunction
augroup foreplay_completion
autocmd!
autocmd FileType clojure setlocal omnifunc=foreplay#omnicomplete
augroup END
" }}}1
" REPL client {{{1
let s:repl = {"requires": {}}
if !exists('s:repls')
let s:repls = []
let s:repl_paths = {}
endif
function! s:qsym(symbol)
if a:symbol =~# '^[[:alnum:]?*!+/=<>.:-]\+$'
return "'".a:symbol
else
return '(symbol "'.escape(a:symbol, '"').'")'
endif
endfunction
function! s:repl.path() dict abort
return self.connection.path()
endfunction
function! s:repl.eval(expr, options) dict abort
2012-12-04 06:06:22 +00:00
try
let result = self.connection.eval(a:expr, a:options)
2012-12-04 06:06:22 +00:00
catch /^\w\+: Connection/
call filter(s:repl_paths, 'v:val isnot self')
call filter(s:repls, 'v:val isnot self')
throw v:exception
endtry
return result
endfunction
function! s:repl.require(lib) dict abort
if a:lib !~# '^\%(user\)\=$' && !get(self.requires, a:lib, 0)
let reload = has_key(self.requires, a:lib) ? ' :reload' : ''
let self.requires[a:lib] = 0
let result = self.eval('(doto '.s:qsym(a:lib).' (require'.reload.') the-ns)', {'ns': 'user', 'session': 0})
2012-12-15 20:41:33 +00:00
let self.requires[a:lib] = !has_key(result, 'ex')
2012-12-04 06:06:22 +00:00
endif
return ''
2012-12-04 06:06:22 +00:00
endfunction
function! s:repl.includes_file(file) dict abort
let file = substitute(a:file, '\C^zipfile:\(.*\)::', '\1/', '')
for path in self.path()
2012-12-04 06:06:22 +00:00
if file[0 : len(path)-1] ==? path
return 1
endif
endfor
endfunction
function! s:register_connection(conn, ...)
call insert(s:repls, extend({'connection': a:conn}, deepcopy(s:repl)))
if a:0 && a:1 !=# ''
let s:repl_paths[a:1] = s:repls[0]
endif
return s:repls[0]
endfunction
" }}}1
" :Connect {{{1
command! -bar -complete=customlist,s:connect_complete -nargs=? ForeplayConnect :exe s:Connect(<q-args>)
function! foreplay#input_host_port()
let arg = input('Host> ', 'localhost')
if arg ==# ''
return ''
endif
echo "\n"
let arg .= ':' . input('Port> ')
if arg =~# ':$'
return ''
endif
echo "\n"
return arg
endfunction
function! s:protos()
return map(split(globpath(&runtimepath, 'autoload/*/foreplay_connection.vim'), "\n"), 'fnamemodify(v:val, ":h:t")')
endfunction
function! s:connect_complete(A, L, P)
let proto = matchstr(a:A, '\w\+\ze://')
if proto ==# ''
let options = map(s:protos(), 'v:val."://"')
else
let rest = matchstr(a:A, '://\zs.*')
try
let options = {proto}#foreplay_connection#complete(rest)
catch /^Vim(let):E117/
let options = ['localhost:']
endtry
call map(options, 'proto."://".v:val')
endif
if a:A !=# ''
call filter(options, 'v:val[0 : strlen(a:A)-1] ==# a:A')
endif
return options
endfunction
function! s:Connect(arg)
if a:arg =~# '^\w\+://'
let [proto, arg] = split(a:arg, '://')
elseif a:arg !=# ''
return 'echoerr '.string('Usage: :Connect proto://...')
else
let protos = s:protos()
if empty(protos)
return 'echoerr '.string('No protocols available')
endif
let proto = s:inputlist('Protocol> ', protos)
if proto ==# ''
return
endif
redraw!
echo ':Connect'
echo 'Protocol> '.proto
let arg = {proto}#foreplay_connection#prompt()
endif
try
let connection = {proto}#foreplay_connection#open(arg)
catch /.*/
return 'echoerr '.string(v:exception)
endtry
if type(connection) !=# type({}) || empty(connection)
return ''
endif
let client = s:register_connection(connection)
echo 'Connected to '.proto.'://'.arg
let path = fnamemodify(exists('b:java_root') ? b:java_root : fnamemodify(expand('%'), ':p:s?.*\zs[\/]src[\/].*??'), ':~')
let root = input('Scope connection to: ', path, 'dir')
2012-12-04 06:06:22 +00:00
if root !=# ''
let s:repl_paths[fnamemodify(root, ':p:s?[\/]$??')] = client
2012-12-04 06:06:22 +00:00
endif
return ''
endfunction
augroup foreplay_connect
autocmd!
autocmd FileType clojure command! -bar -complete=customlist,s:connect_complete -nargs=? Connect :ForeplayConnect <args>
augroup END
" }}}1
" Java runner {{{1
let s:oneoff = {}
function! s:oneoff.path() dict abort
return classpath#split(classpath#from_vim(self.classpath))
endfunction
2012-12-04 06:06:22 +00:00
let s:oneoff_pr = tempname()
let s:oneoff_ex = tempname()
2012-12-26 00:14:45 +00:00
let s:oneoff_stk = tempname()
2012-12-04 06:06:22 +00:00
let s:oneoff_in = tempname()
let s:oneoff_out = tempname()
let s:oneoff_err = tempname()
function! s:oneoff.eval(expr, options) dict abort
if &verbose && get(options, 'session', 1)
2012-12-04 06:06:22 +00:00
echohl WarningMSG
echomsg "No REPL found. Running java clojure.main ..."
echohl None
endif
if a:options.ns !=# '' && a:options.ns !=# 'user'
2013-01-07 01:54:42 +00:00
let ns = '(require '.s:qsym(a:options.ns).') (in-ns '.s:qsym(a:options.ns).') '
2012-12-04 06:06:22 +00:00
else
let ns = ''
endif
call writefile([], s:oneoff_pr, 'b')
call writefile([], s:oneoff_ex, 'b')
2012-12-26 00:14:45 +00:00
call writefile([], s:oneoff_stk, 'b')
2012-12-04 06:06:22 +00:00
call writefile(split('(do '.a:expr.')', "\n"), s:oneoff_in, 'b')
call writefile([], s:oneoff_out, 'b')
call writefile([], s:oneoff_err, 'b')
2013-01-07 00:53:37 +00:00
let java_cmd = exists('$JAVA_CMD') ? $JAVA_CMD : 'java'
let command = java_cmd.' -cp '.shellescape(self.classpath).' clojure.main -e ' .
\ shellescape(
\ '(binding [*out* (java.io.FileWriter. '.s:str(s:oneoff_out).')' .
\ ' *err* (java.io.FileWriter. '.s:str(s:oneoff_err).')]' .
2012-12-04 06:06:22 +00:00
\ ' (try' .
\ ' (require ''clojure.repl) '.ns.'(spit '.s:str(s:oneoff_pr).' (pr-str (eval (read-string (slurp '.s:str(s:oneoff_in).')))))' .
2012-12-04 06:06:22 +00:00
\ ' (catch Exception e' .
\ ' (spit *err* (.toString e))' .
\ ' (spit '.s:str(s:oneoff_ex).' (class e))' .
\ ' (spit '.s:str(s:oneoff_stk).' (apply str (interpose "\n" (.getStackTrace e))))))' .
2012-12-04 06:06:22 +00:00
\ ' nil)')
let wtf = system(command)
let result = {}
let result.value = join(readfile(s:oneoff_pr, 'b'), "\n")
let result.out = join(readfile(s:oneoff_out, 'b'), "\n")
let result.err = join(readfile(s:oneoff_err, 'b'), "\n")
2012-12-26 01:26:45 +00:00
let result.ex = join(readfile(s:oneoff_ex, 'b'), "\n")
2012-12-26 00:14:45 +00:00
let result.stacktrace = readfile(s:oneoff_stk)
2012-12-26 01:26:45 +00:00
call filter(result, '!empty(v:val)')
if v:shell_error && get(result, 'ex', '') ==# ''
2012-12-04 06:06:22 +00:00
throw 'Error running Clojure: '.wtf
else
return result
2012-12-04 06:06:22 +00:00
endif
endfunction
function! s:oneoff.require(symbol)
return ''
endfunction
" }}}1
" Client {{{1
function! s:client() abort
silent doautocmd User ForeplayPreConnect
let buf = exists('s:input') ? s:input : '%'
let root = simplify(fnamemodify(bufname(buf), ':p:s?[\/]$??'))
let previous = ""
while root !=# previous
if has_key(s:repl_paths, root)
return s:repl_paths[root]
endif
let previous = root
let root = fnamemodify(root, ':h')
endwhile
return foreplay#local_client(1)
endfunction
function! foreplay#client() abort
return s:client()
endfunction
function! foreplay#local_client(...)
if !a:0
silent doautocmd User ForeplayPreConnect
endif
let buf = exists('s:input') ? s:input : '%'
for repl in s:repls
if repl.includes_file(fnamemodify(bufname(buf), ':p'))
return repl
endif
endfor
if exists('*classpath#from_vim')
let cp = classpath#from_vim(getbufvar(buf, '&path'))
return extend({'classpath': cp}, s:oneoff)
endif
throw ':Connect to a REPL or install classpath.vim to evaluate code'
2012-12-04 06:06:22 +00:00
endfunction
function! foreplay#findresource(resource) abort
if a:resource ==# ''
return ''
endif
try
let path = foreplay#local_client().path()
catch /^:Connect/
return ''
endtry
let file = findfile(a:resource, escape(join(path, ','), ' '))
if !empty(file)
return file
endif
for jar in path
if fnamemodify(jar, ':e') ==# 'jar' && index(foreplay#jar_contents(jar), a:resource) >= 0
return 'zipfile:' . jar . '::' . a:resource
endif
endfor
return ''
endfunction
function! foreplay#quickfix_for(stacktrace) abort
let qflist = []
for line in a:stacktrace
let match = matchlist(line, '\(.*\)(\(.*\):\(\d\+\))')
let entry = {'text': line}
let [_, class, file, lnum; __] = match
let entry.lnum = lnum
let truncated = substitute(class, '\.[A-Za-z0-9_]\+\%($.*\)$', '', '')
if file == 'NO_SOURCE_FILE'
let entry.resource = ''
else
let entry.resource = tr(truncated, '.', '/').'/'.file
endif
let qflist += [entry]
endfor
let paths = map(copy(qflist), 'foreplay#findresource(v:val.resource)')
let i = 0
for i in range(len(qflist))
if !empty(paths[i])
let qflist[i].filename = paths[i]
else
call remove(qflist[i], 'lnum')
endif
endfor
return qflist
endfunction
function! s:output_response(response) abort
if get(a:response, 'err', '') !=# ''
echohl ErrorMSG
echo substitute(a:response.err, '\n$', '', '')
echohl NONE
endif
if get(a:response, 'out', '') !=# ''
echo substitute(a:response.out, '\n$', '', '')
endif
endfunction
function! s:eval(expr, ...) abort
let options = a:0 ? copy(a:1) : {}
let client = get(options, 'client', s:client())
if !has_key(options, 'ns')
if foreplay#ns() !~# '^\%(user\)$'
call client.require(foreplay#ns())
endif
let options.ns = foreplay#ns()
endif
return client.eval(a:expr, options)
endfunction
function! foreplay#eval(expr) abort
let response = s:eval(a:expr, {'session': 1})
call s:output_response(response)
if !empty(get(response, 'stacktrace', []))
call setloclist(0, foreplay#quickfix_for(response.stacktrace))
endif
if get(response, 'ex', '') !=# ''
let err = 'Clojure: '.response.ex
elseif has_key(response, 'value')
return response.value
else
let err = 'foreplay.vim: Something went wrong: '.string(response)
endif
throw err
2012-12-04 06:06:22 +00:00
endfunction
function! foreplay#evalprint(expr) abort
2013-01-07 06:55:08 +00:00
try
echo foreplay#eval(a:expr)
catch /^Clojure:/
endtry
return ''
endfunction
2012-12-04 06:06:22 +00:00
function! foreplay#evalparse(expr) abort
let response = s:eval(
2012-12-04 06:06:22 +00:00
\ '(symbol ((fn *vimify [x]' .
\ ' (cond' .
\ ' (map? x) (str "{" (apply str (interpose ", " (map (fn [[k v]] (str (*vimify k) ": " (*vimify v))) x))) "}")' .
\ ' (coll? x) (str "[" (apply str (interpose ", " (map *vimify x))) "]")' .
2012-12-04 06:06:22 +00:00
\ ' (number? x) (pr-str x)' .
\ ' (keyword? x) (pr-str (name x))' .
\ ' :else (pr-str (str x)))) '.a:expr.'))',
\ {'session': 0})
call s:output_response(response)
if get(response, 'ex', '') !=# ''
let err = 'Clojure: '.response.ex
elseif has_key(response, 'value')
return empty(response.value) ? '' : eval(response.value)
2012-12-04 06:06:22 +00:00
else
let err = 'foreplay.vim: Something went wrong: '.string(response)
2012-12-04 06:06:22 +00:00
endif
throw err
2012-12-04 06:06:22 +00:00
endfunction
" }}}1
" Eval {{{1
let foreplay#skip = 'synIDattr(synID(line("."),col("."),1),"name") =~? "comment\\|string\\|char"'
function! s:opfunc(type) abort
let sel_save = &selection
let cb_save = &clipboard
let reg_save = @@
try
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
if a:type =~ '^\d\+$'
silent exe 'normal! ^v'.a:type.'$hy'
elseif a:type =~# '^.$'
silent exe "normal! `<" . a:type . "`>y"
elseif a:type ==# 'line'
silent exe "normal! '[V']y"
elseif a:type ==# 'block'
silent exe "normal! `[\<C-V>`]y"
elseif a:type ==# 'outer'
call searchpair('(','',')', 'Wbcr', g:foreplay#skip)
silent exe "normal! vaby"
else
silent exe "normal! `[v`]y"
endif
redraw
2012-12-04 06:06:22 +00:00
return @@
finally
let @@ = reg_save
let &selection = sel_save
let &clipboard = cb_save
endtry
endfunction
function! s:filterop(type) abort
let reg_save = @@
try
let expr = s:opfunc(a:type)
let @@ = matchstr(expr, '^\n\+').foreplay#eval(expr).matchstr(expr, '\n\+$')
2012-12-04 06:06:22 +00:00
if @@ !~# '^\n*$'
normal! gvp
endif
catch /^Clojure:/
return ''
finally
let @@ = reg_save
endtry
endfunction
function! s:printop(type) abort
let s:todo = s:opfunc(a:type)
call feedkeys("\<Plug>ForeplayPrintLast")
endfunction
function! s:print_last() abort
call foreplay#evalprint(s:todo)
return ''
2012-12-04 06:06:22 +00:00
endfunction
function! s:editop(type) abort
call feedkeys(&cedit . "\<Home>", 'n')
let input = s:input(substitute(substitute(s:opfunc(a:type), "\s*;[^\n]*", '', 'g'), '\n\+\s*', ' ', 'g'))
if input !=# ''
call foreplay#evalprint(input)
endif
2012-12-04 06:06:22 +00:00
endfunction
function! s:Eval(bang, line1, line2, count, args) abort
if a:args !=# ''
let expr = a:args
else
if a:count ==# 0
normal! ^
let line1 = searchpair('(','',')', 'bcrn', g:foreplay#skip)
let line2 = searchpair('(','',')', 'rn', g:foreplay#skip)
2012-12-04 06:06:22 +00:00
else
let line1 = a:line1
let line2 = a:line2
endif
if !line1 || !line2
return ''
endif
let expr = join(getline(line1, line2), "\n")
if a:bang
exe line1.','.line2.'delete _'
endif
endif
if a:bang
try
let result = foreplay#eval(expr)
2012-12-04 06:06:22 +00:00
if a:args !=# ''
call append(a:line1, result)
exe a:line1
else
call append(a:line1-1, result)
exe a:line1-1
endif
catch /^Clojure:/
endtry
else
call foreplay#evalprint(expr)
endif
2012-12-04 06:06:22 +00:00
return ''
endfunction
" If we call input() directly inside a try, and the user opens the command
" line window and tries to switch out of it (such as with ctrl-w), Vim will
" crash when the command line window closes. Adding an indirect function call
" works around this.
function! s:actually_input(...)
return call(function('input'), a:000)
endfunction
function! s:input(default) abort
if !exists('g:FOREPLAY_HISTORY') || type(g:FOREPLAY_HISTORY) != type([])
unlet! g:FOREPLAY_HISTORY
2012-12-04 06:06:22 +00:00
let g:FOREPLAY_HISTORY = []
endif
try
let s:input = bufnr('%')
let s:oldhist = s:histswap(g:FOREPLAY_HISTORY)
return s:actually_input(foreplay#ns().'=> ', a:default, 'customlist,foreplay#eval_complete')
finally
unlet! s:input
if exists('s:oldhist')
let g:FOREPLAY_HISTORY = s:histswap(s:oldhist)
endif
endtry
endfunction
function! s:inputclose() abort
let l = substitute(getcmdline(), '"\%(\\.\|[^"]\)*"\|\\.', '', 'g')
let open = len(substitute(l, '[^(]', '', 'g'))
let close = len(substitute(l, '[^)]', '', 'g'))
if open - close == 1
return ")\<CR>"
else
return ")"
endif
endfunction
function! s:inputeval() abort
let input = s:input('')
redraw
2013-01-06 00:54:30 +00:00
if input !=# ''
call foreplay#evalprint(input)
2012-12-04 06:06:22 +00:00
endif
2013-01-06 00:54:30 +00:00
return ''
2012-12-04 06:06:22 +00:00
endfunction
function! s:recall() abort
try
cnoremap <expr> ) <SID>inputclose()
let input = s:input('(')
if input =~# '^(\=$'
return ''
else
return foreplay#eval(input)
endif
catch /^Clojure:/
return ''
finally
silent! cunmap )
endtry
endfunction
function! s:histswap(list) abort
2012-12-04 06:06:22 +00:00
let old = []
for i in range(1, histnr('@') * (histnr('@') > 0))
call extend(old, [histget('@', i)])
endfor
call histdel('@')
for entry in a:list
call histadd('@', entry)
endfor
return old
endfunction
nnoremap <silent> <Plug>ForeplayPrintLast :exe <SID>print_last()<CR>
2012-12-07 04:04:48 +00:00
nnoremap <silent> <Plug>ForeplayPrint :<C-U>set opfunc=<SID>printop<CR>g@
xnoremap <silent> <Plug>ForeplayPrint :<C-U>call <SID>printop(visualmode())<CR>
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nnoremap <silent> <Plug>ForeplayFilter :<C-U>set opfunc=<SID>filterop<CR>g@
xnoremap <silent> <Plug>ForeplayFilter :<C-U>call <SID>filterop(visualmode())<CR>
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nnoremap <silent> <Plug>ForeplayEdit :<C-U>set opfunc=<SID>editop<CR>g@
xnoremap <silent> <Plug>ForeplayEdit :<C-U>call <SID>editop(visualmode())<CR>
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nnoremap <Plug>ForeplayPrompt :exe <SID>inputeval()<CR>
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
noremap! <Plug>ForeplayRecall <C-R>=<SID>recall()<CR>
2012-12-04 06:06:22 +00:00
function! s:setup_eval() abort
2012-12-04 06:06:22 +00:00
command! -buffer -bang -range=0 -nargs=? -complete=customlist,foreplay#eval_complete Eval :exe s:Eval(<bang>0, <line1>, <line2>, <count>, <q-args>)
2012-12-07 04:04:48 +00:00
nmap <buffer> cp <Plug>ForeplayPrint
nmap <buffer> cpp <Plug>ForeplayPrintab
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nmap <buffer> c! <Plug>ForeplayFilter
nmap <buffer> c!! <Plug>ForeplayFilterab
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nmap <buffer> cq <Plug>ForeplayEdit
nmap <buffer> cqq <Plug>ForeplayEditab
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
nmap <buffer> cqp <Plug>ForeplayPrompt
exe 'nmap <buffer> cqc <Plug>ForeplayPrompt' . &cedit . 'i'
2012-12-04 06:06:22 +00:00
2012-12-07 04:04:48 +00:00
map! <buffer> <C-R>( <Plug>ForeplayRecall
2012-12-04 06:06:22 +00:00
endfunction
function! s:cmdwinenter()
setlocal filetype=clojure
endfunction
function! s:cmdwinleave()
setlocal filetype< omnifunc<
endfunction
augroup foreplay_eval
autocmd!
autocmd FileType clojure call s:setup_eval()
2012-12-04 06:06:22 +00:00
autocmd CmdWinEnter @ if exists('s:input') | call s:cmdwinenter() | endif
autocmd CmdWinLeave @ if exists('s:input') | call s:cmdwinleave() | endif
augroup END
" }}}1
" :Require {{{1
function! s:Require(bang, ns)
let cmd = ('(require '.s:qsym(a:ns ==# '' ? foreplay#ns() : a:ns).' :reload'.(a:bang ? '-all' : '').')')
echo cmd
try
call foreplay#eval(cmd)
return ''
catch /^Clojure:.*/
return ''
endtry
endfunction
function! s:setup_require()
command! -buffer -bar -bang -complete=customlist,foreplay#ns_complete -nargs=? Require :exe s:Require(<bang>0, <q-args>)
nnoremap <silent><buffer> cpr :Require<CR>
nnoremap <silent><buffer> cpR :Require!<CR>
endfunction
2012-12-04 06:06:22 +00:00
augroup foreplay_require
autocmd!
autocmd FileType clojure call s:setup_require()
2012-12-04 06:06:22 +00:00
augroup END
" }}}1
" Go to source {{{1
function! foreplay#source(symbol) abort
let options = {'client': foreplay#local_client(), 'ns': foreplay#ns(), 'session': 0}
2012-12-04 06:06:22 +00:00
let cmd =
\ " (when-let [v (resolve " . s:qsym(a:symbol) .')]' .
\ ' (when-let [filepath (:file (meta v))]' .
\ ' (when-let [url (.getResource (clojure.lang.RT/baseLoader) filepath)]' .
\ ' (symbol (str (str "+" (:line (meta v))) " "' .
\ ' (if (= "jar" (.getProtocol url))' .
\ ' (str "zip" (.replaceFirst (.getFile url) "!/" "::"))' .
\ ' (.getFile url)))))))'
let result = get(split(s:eval(cmd, options).value, "\n"), 0, '')
2012-12-04 06:06:22 +00:00
return result ==# 'nil' ? '' : result
endfunction
function! s:Edit(cmd, keyword) abort
if a:keyword =~# '^\k\+/$'
let location = foreplay#findfile(a:keyword[0: -2])
elseif a:keyword =~# '^\k\+\.[^/.]\+$'
let location = foreplay#findfile(a:keyword)
else
let location = foreplay#source(a:keyword)
endif
if location !=# ''
if matchstr(location, '^+\d\+ \zs.*') ==# expand('%:p') && a:cmd ==# 'edit'
return matchstr(location, '\d\+')
else
return a:cmd.' '.location.'|let &l:path = '.string(&l:path)
endif
endif
2012-12-05 05:06:00 +00:00
let v:errmsg = "Couldn't find source for ".a:keyword
2012-12-04 06:06:22 +00:00
return 'echoerr v:errmsg'
endfunction
2013-01-03 05:10:11 +00:00
nnoremap <silent> <Plug>ForeplayDjump :<C-U>exe <SID>Edit('edit', expand('<cword>'))<CR>
nnoremap <silent> <Plug>ForeplayDsplit :<C-U>exe <SID>Edit('split', expand('<cword>'))<CR>
nnoremap <silent> <Plug>ForeplayDtabjump :<C-U>exe <SID>Edit('tabedit', expand('<cword>'))<CR>
2012-12-04 06:06:22 +00:00
augroup foreplay_source
autocmd!
autocmd FileType clojure setlocal includeexpr=tr(v:fname,'.-','/_')
autocmd FileType clojure setlocal suffixesadd=.clj,.java
autocmd FileType clojure setlocal define=^\\s*(def\\w*
autocmd FileType clojure command! -bar -buffer -nargs=1 -complete=customlist,foreplay#eval_complete Djump :exe s:Edit('edit', <q-args>)
autocmd FileType clojure command! -bar -buffer -nargs=1 -complete=customlist,foreplay#eval_complete Dsplit :exe s:Edit('split', <q-args>)
2013-01-03 05:10:11 +00:00
autocmd FileType clojure nmap <buffer> [<C-D> <Plug>ForeplayDjump
autocmd FileType clojure nmap <buffer> ]<C-D> <Plug>ForeplayDjump
autocmd FileType clojure nmap <buffer> <C-W><C-D> <Plug>ForeplayDsplit
autocmd FileType clojure nmap <buffer> <C-W>d <Plug>ForeplayDsplit
autocmd FileType clojure nmap <buffer> <C-W>gd <Plug>ForeplayDtabedit
2012-12-04 06:06:22 +00:00
augroup END
" }}}1
" Go to file {{{1
function! foreplay#findfile(path) abort
let options = {'client': foreplay#local_client(), 'ns': foreplay#ns(), 'session': 0}
2012-12-04 06:06:22 +00:00
let cmd =
\ '(symbol' .
\ ' (or' .
\ ' (when-let [url (.getResource (clojure.lang.RT/baseLoader) %s)]' .
\ ' (if (= "jar" (.getProtocol url))' .
\ ' (str "zip" (.replaceFirst (.getFile url) "!/" "::"))' .
\ ' (.getFile url)))' .
\ ' ""))'
let path = a:path
if path !~# '[/.]' && path =~# '^\k\+$'
let aliascmd = printf(cmd,
\ '(if-let [ns ((ns-aliases *ns*) '.s:qsym(path).')]' .
\ ' (str (.replace (.replace (str (ns-name ns)) "-" "_") "." "/") ".clj")' .
\ ' "'.path.'.clj")')
let result = get(split(s:eval(aliascmd, options).value, "\n"), 0, '')
2012-12-04 06:06:22 +00:00
else
if path !~# '/'
let path = tr(path, '.-', '/_')
endif
if path !~# '\.\w\+$'
let path .= '.clj'
endif
let result = get(split(s:eval(printf(cmd, '"'.escape(path, '"').'"'), options).value, "\n"), 0, '')
2012-12-04 06:06:22 +00:00
endif
if result ==# ''
return findfile(path, &l:path)
else
return result
endif
endfunction
function! s:GF(cmd, file) abort
if a:file =~# '^[^/]*/[^/.]*$' && a:file =~# '^\k\+$'
let [file, jump] = split(a:file, "/")
else
let file = a:file
endif
let file = foreplay#findfile(file)
if file ==# ''
let v:errmsg = "Couldn't find file for ".a:file
return 'echoerr v:errmsg'
endif
return a:cmd .
\ (exists('jump') ? ' +sil!\ djump\ ' . jump : '') .
\ ' ' . fnameescape(file) .
\ '| let &l:path = ' . string(&l:path)
endfunction
augroup foreplay_go_to_file
autocmd!
autocmd FileType clojure nnoremap <silent><buffer> gf :<C-U>exe <SID>GF('edit', expand('<cfile>'))<CR>
autocmd FileType clojure nnoremap <silent><buffer> <C-W>f :<C-U>exe <SID>GF('split', expand('<cfile>'))<CR>
autocmd FileType clojure nnoremap <silent><buffer> <C-W><C-F> :<C-U>exe <SID>GF('split', expand('<cfile>'))<CR>
autocmd FileType clojure nnoremap <silent><buffer> <C-W>gf :<C-U>exe <SID>GF('tabedit', expand('<cfile>'))<CR>
augroup END
" }}}1
" Documentation {{{1
function! s:buffer_path(...) abort
let buffer = a:0 ? a:1 : exists('s:input') ? s:input : '%'
if getbufvar(buffer, '&buftype') =~# '^no'
return ''
endif
let path = substitute(fnamemodify(bufname(buffer), ':p'), '\C^zipfile:\(.*\)::', '\1/', '')
if exists('*classpath#from_vim')
for dir in classpath#split(classpath#from_vim(getbufvar(buffer, '&path')))
if dir !=# '' && path[0 : strlen(dir)-1] ==# dir
return path[strlen(dir)+1:-1]
endif
endfor
endif
2012-12-04 06:06:22 +00:00
return ''
endfunction
function! s:tons(path) abort
return tr(substitute(a:path, '\.\w\+$', '', ''), '\/_', '..-')
endfunction
function! foreplay#ns() abort
let lnum = 1
while lnum < line('$') && getline(lnum) =~# '^\s*\%(;.*\)\=$'
let lnum += 1
endwhile
2013-01-09 05:07:24 +00:00
let lines = join(getline(lnum, lnum+50), ' ')
let lines = substitute(lines, '"\%(\\.\|[^"]\)*"\|\\.', '', 'g')
let lines = substitute(lines, '\^\={[^{}]*}', '', '')
let ns = matchstr(lines, '\C^(\s*\%(in-ns\s*''\|ns\s\+\)\zs[A-Za-z0-9_?*!+/=<>.-]\+\ze')
if ns !=# ''
return ns
endif
2012-12-07 01:44:00 +00:00
let path = s:buffer_path()
return s:tons(path ==# '' ? 'user' : path)
2012-12-04 06:06:22 +00:00
endfunction
2012-12-25 23:56:47 +00:00
function! s:Lookup(ns, macro, arg) abort
2012-12-04 06:06:22 +00:00
" doc is in clojure.core in older Clojure versions
try
2012-12-25 23:56:47 +00:00
call foreplay#eval("(require '".a:ns.") (eval (list (if (ns-resolve 'clojure.core '".a:macro.") 'clojure.core/".a:macro." '".a:ns.'/'.a:macro.") '".a:arg.'))')
2012-12-04 06:06:22 +00:00
catch /^Clojure:/
catch /.*/
echohl ErrorMSG
echo v:exception
echohl None
endtry
return ''
endfunction
function! s:inputlist(label, entries)
let choices = [a:label]
for i in range(len(a:entries))
let choices += [printf('%2d. %s', i+1, a:entries[i])]
endfor
let choice = inputlist(choices)
if choice
return a:entries[choice-1]
else
return ''
endif
endfunction
function! s:Apropos(pattern) abort
if a:pattern =~# '^#\="'
let pattern = a:pattern
elseif a:pattern =~# '^^'
let pattern = '#"' . a:pattern . '"'
else
let pattern = '"' . a:pattern . '"'
endif
2013-01-02 01:09:16 +00:00
let matches = foreplay#evalparse('(clojure.repl/apropos '.pattern.')')
2012-12-04 06:06:22 +00:00
if empty(matches)
return ''
endif
2012-12-25 17:04:07 +00:00
let choice = s:inputlist('Look up docs for:', matches)
2012-12-04 06:06:22 +00:00
if choice !=# ''
return 'echo "\n"|Doc '.choice
else
return ''
endif
endfunction
function! s:K()
let word = expand('<cword>')
let java_candidate = matchstr(word, '^\%(\w\+\.\)*\u\l\w*\ze\%(\.\|\/\w\+\)\=$')
if java_candidate !=# ''
return 'Javadoc '.java_candidate
else
return 'Doc '.word
endif
endfunction
2013-01-03 05:10:11 +00:00
nnoremap <Plug>ForeplayK :<C-R>=<SID>K()<CR><CR>
nnoremap <Plug>ForeplaySource :Source <C-R><C-W><CR>
2012-12-04 06:06:22 +00:00
augroup foreplay_doc
autocmd!
2013-01-03 05:10:11 +00:00
autocmd FileType clojure nmap <buffer> K <Plug>ForeplayK
autocmd FileType clojure nmap <buffer> [d <Plug>ForeplaySource
autocmd FileType clojure nmap <buffer> ]d <Plug>ForeplaySource
2012-12-14 23:08:51 +00:00
autocmd FileType clojure command! -buffer -nargs=1 Apropos :exe s:Apropos(<q-args>)
2012-12-25 23:56:47 +00:00
autocmd FileType clojure command! -buffer -nargs=1 FindDoc :exe s:Lookup('clojure.repl', 'find-doc', printf('#"%s"', <q-args>))
autocmd FileType clojure command! -buffer -bar -nargs=1 Javadoc :exe s:Lookup('clojure.java.javadoc', 'javadoc', <q-args>)
autocmd FileType clojure command! -buffer -bar -nargs=1 -complete=customlist,foreplay#eval_complete Doc :exe s:Lookup('clojure.repl', 'doc', <q-args>)
autocmd FileType clojure command! -buffer -bar -nargs=1 -complete=customlist,foreplay#eval_complete Source :exe s:Lookup('clojure.repl', 'source', <q-args>)
2012-12-04 06:06:22 +00:00
augroup END
" }}}1
" Leiningen {{{1
function! s:hunt(start, anchor) abort
let root = simplify(fnamemodify(a:start, ':p:s?[\/]$??'))
2012-12-09 19:40:27 +00:00
if !isdirectory(fnamemodify(root, ':h'))
return ''
endif
2012-12-04 06:06:22 +00:00
let previous = ""
while root !=# previous
if filereadable(root . '/' . a:anchor) && isdirectory(root . '/src')
2012-12-04 06:06:22 +00:00
return root
endif
let previous = root
let root = fnamemodify(root, ':h')
endwhile
return ''
endfunction
if !exists('s:leiningen_repl_ports')
let s:leiningen_repl_ports = {}
endif
function! s:leiningen_connect()
if !exists('b:leiningen_root')
return
endif
let portfile = b:leiningen_root . '/target/repl-port'
if getfsize(portfile) > 0 && getftime(portfile) !=# get(s:leiningen_repl_ports, b:leiningen_root, -1)
let port = matchstr(readfile(portfile, 'b', 1)[0], '\d\+')
2012-12-04 06:06:22 +00:00
let s:leiningen_repl_ports[b:leiningen_root] = getftime(portfile)
try
call s:register_connection(nrepl#foreplay_connection#open(port), b:leiningen_root)
catch /^nREPL Connection Error:/
2012-12-04 06:06:22 +00:00
call delete(portfile)
endtry
endif
endfunction
function! s:leiningen_init() abort
if !exists('b:leiningen_root')
let root = s:hunt(expand('%:p'), 'project.clj')
if root !=# ''
let b:leiningen_root = root
endif
endif
if !exists('b:leiningen_root')
return
endif
let b:java_root = b:leiningen_root
setlocal makeprg=lein efm=%+G
call s:leiningen_connect()
endfunction
augroup foreplay_leiningen
autocmd!
2012-12-07 04:04:48 +00:00
autocmd User ForeplayPreConnect call s:leiningen_connect()
2012-12-04 06:06:22 +00:00
autocmd FileType clojure call s:leiningen_init()
augroup END
" }}}1
" vim:set et sw=2: