2014-07-04 20:24:46 +00:00
|
|
|
" fireplace.vim - Clojure REPL support
|
2013-01-16 01:04:46 +00:00
|
|
|
" Maintainer: Tim Pope <http://tpo.pe/>
|
2014-07-04 20:50:18 +00:00
|
|
|
" Version: 1.0
|
2014-07-04 21:00:21 +00:00
|
|
|
" GetLatestVimScripts: 4978 1 :AutoInstall: fireplace.vim
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
if exists("g:loaded_fireplace") || v:version < 700 || &cp
|
2012-12-04 06:06:22 +00:00
|
|
|
finish
|
|
|
|
endif
|
2013-03-12 19:37:37 +00:00
|
|
|
let g:loaded_fireplace = 1
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: File type
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_file_type
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
|
|
|
autocmd BufNewFile,BufReadPost *.clj setfiletype clojure
|
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Escaping
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:str(string) abort
|
2013-01-01 00:53:54 +00:00
|
|
|
return '"' . escape(a:string, '"\') . '"'
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:qsym(symbol) abort
|
2013-02-10 18:25:45 +00:00
|
|
|
if a:symbol =~# '^[[:alnum:]?*!+/=<>.:-]\+$'
|
|
|
|
return "'".a:symbol
|
|
|
|
else
|
|
|
|
return '(symbol '.s:str(a:symbol).')'
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:to_ns(path) abort
|
|
|
|
return tr(substitute(a:path, '\.\w\+$', '', ''), '\/_', '..-')
|
|
|
|
endfunction
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Completion
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2012-12-26 00:01:51 +00:00
|
|
|
let s:jar_contents = {}
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#jar_contents(path) abort
|
2013-01-16 00:02:10 +00:00
|
|
|
if !exists('s:zipinfo')
|
2014-01-12 01:15:45 +00:00
|
|
|
if executable('zipinfo')
|
|
|
|
let s:zipinfo = 'zipinfo -1 '
|
|
|
|
elseif executable('python')
|
|
|
|
let s:zipinfo = 'python -c '.shellescape('import zipfile, sys; print chr(10).join(zipfile.ZipFile(sys.argv[1]).namelist())').' '
|
|
|
|
else
|
|
|
|
let s:zipinfo = ''
|
|
|
|
endif
|
2013-01-16 00:02:10 +00:00
|
|
|
endif
|
2014-01-12 01:15:45 +00:00
|
|
|
|
|
|
|
if !has_key(s:jar_contents, a:path) && has('python')
|
|
|
|
python import vim, zipfile
|
|
|
|
python vim.command("let s:jar_contents[a:path] = split('" + "\n".join(zipfile.ZipFile(vim.eval('a:path')).namelist()) + "', \"\n\")")
|
|
|
|
elseif !has_key(s:jar_contents, a:path) && !empty(s:zipinfo)
|
|
|
|
let s:jar_contents[a:path] = split(system(s:zipinfo.shellescape(a:path)), "\n")
|
2012-12-26 00:01:51 +00:00
|
|
|
if v:shell_error
|
2014-01-12 01:15:45 +00:00
|
|
|
let s:jar_contents[a:path] = []
|
2012-12-26 00:01:51 +00:00
|
|
|
endif
|
|
|
|
endif
|
2014-01-12 01:15:45 +00:00
|
|
|
|
2012-12-26 00:01:51 +00:00
|
|
|
return copy(get(s:jar_contents, a:path, []))
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#eval_complete(A, L, P) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
let prefix = matchstr(a:A, '\%(.* \|^\)\%(#\=[\[{('']\)*')
|
|
|
|
let keyword = a:A[strlen(prefix) : -1]
|
2013-03-12 19:37:37 +00:00
|
|
|
return sort(map(fireplace#omnicomplete(0, keyword), 'prefix . v:val.word'))
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#ns_complete(A, L, P) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
let matches = []
|
2014-01-16 02:36:58 +00:00
|
|
|
for dir in fireplace#path()
|
2012-12-26 00:01:51 +00:00
|
|
|
if dir =~# '\.jar$'
|
2013-03-12 19:37:37 +00:00
|
|
|
let files = filter(fireplace#jar_contents(dir), 'v:val =~# "\\.clj$"')
|
2012-12-05 18:16:10 +00:00
|
|
|
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
|
2013-02-10 18:25:45 +00:00
|
|
|
return filter(map(matches, 's:to_ns(v:val)'), 'a:A ==# "" || a:A ==# v:val[0 : strlen(a:A)-1]')
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#omnicomplete(findstart, base) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
if a:findstart
|
|
|
|
let line = getline('.')[0 : col('.')-2]
|
|
|
|
return col('.') - strlen(matchstr(line, '\k\+$')) - 1
|
|
|
|
else
|
|
|
|
try
|
2014-04-13 00:30:55 +00:00
|
|
|
|
|
|
|
if fireplace#op_available('complete')
|
|
|
|
let response = fireplace#message({'op': 'complete', 'symbol': a:base})
|
|
|
|
if type(get(response[0], 'value')) == type([])
|
|
|
|
if type(get(response[0].value, 0)) == type([])
|
|
|
|
return map(response[0].value[0], '{"word": v:val}')
|
|
|
|
elseif type(get(response[0].value, 0)) == type('')
|
|
|
|
return map(response[0].value, '{"word": v:val}')
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2014-02-15 00:34:49 +00:00
|
|
|
let omnifier = '(fn [[k v]] (let [{:keys [arglists] :as m} (meta v)]' .
|
|
|
|
\ ' {:word k :menu (pr-str (or arglists (symbol ""))) :info (str (when arglists (str arglists "\n")) " " (:doc m)) :kind (if arglists "f" "v")}))'
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
let ns = fireplace#ns()
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
let [aliases, namespaces, maps] = fireplace#evalparse(
|
2012-12-04 06:06:22 +00:00
|
|
|
\ '[(ns-aliases '.s:qsym(ns).') (all-ns) '.
|
|
|
|
\ '(sort-by :word (map '.omnifier.' (ns-map '.s:qsym(ns).')))]')
|
|
|
|
|
|
|
|
if a:base =~# '^[^/]*/[^/]*$'
|
2012-12-26 15:17:18 +00:00
|
|
|
let ns = matchstr(a:base, '^.*\ze/')
|
|
|
|
let prefix = ns . '/'
|
|
|
|
let ns = get(aliases, ns, ns)
|
|
|
|
let keyword = matchstr(a:base, '.*/\zs.*')
|
2013-03-12 19:37:37 +00:00
|
|
|
let results = fireplace#evalparse(
|
2012-12-26 15:17:18 +00:00
|
|
|
\ '(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
|
2012-12-26 15:17:18 +00:00
|
|
|
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([])
|
2012-12-26 15:17:18 +00:00
|
|
|
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
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_completion
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2013-03-12 19:37:37 +00:00
|
|
|
autocmd FileType clojure setlocal omnifunc=fireplace#omnicomplete
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: REPL client
|
2012-12-04 06:06:22 +00:00
|
|
|
|
|
|
|
let s:repl = {"requires": {}}
|
|
|
|
|
|
|
|
if !exists('s:repls')
|
|
|
|
let s:repls = []
|
|
|
|
let s:repl_paths = {}
|
2014-04-04 01:03:26 +00:00
|
|
|
let s:repl_portfiles = {}
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
|
2014-01-11 03:49:25 +00:00
|
|
|
function! s:repl.user_ns() abort
|
|
|
|
return 'user'
|
|
|
|
endfunction
|
|
|
|
|
2013-01-04 00:10:38 +00:00
|
|
|
function! s:repl.path() dict abort
|
|
|
|
return self.connection.path()
|
|
|
|
endfunction
|
|
|
|
|
2014-01-17 22:51:14 +00:00
|
|
|
function! s:conn_try(connection, function, ...) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
try
|
2014-01-17 22:51:14 +00:00
|
|
|
return call(a:connection[a:function], a:000, a:connection)
|
2013-01-24 21:36:09 +00:00
|
|
|
catch /^\w\+ Connection Error:/
|
2014-01-17 22:51:14 +00:00
|
|
|
call s:unregister_connection(a:connection)
|
2012-12-04 06:06:22 +00:00
|
|
|
throw v:exception
|
|
|
|
endtry
|
2014-01-12 05:48:43 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repl.eval(expr, options) dict abort
|
2014-01-16 04:37:41 +00:00
|
|
|
if has_key(a:options, 'ns') && a:options.ns !=# self.user_ns()
|
|
|
|
let error = self.preload(a:options.ns)
|
|
|
|
if !empty(error)
|
|
|
|
return error
|
|
|
|
endif
|
|
|
|
endif
|
2014-01-17 22:51:14 +00:00
|
|
|
return s:conn_try(self.connection, 'eval', a:expr, a:options)
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-16 04:37:41 +00:00
|
|
|
function! s:repl.message(payload, ...) dict abort
|
|
|
|
if has_key(a:payload, 'ns') && a:payload.ns !=# self.user_ns()
|
2014-01-19 04:25:44 +00:00
|
|
|
let ignored_error = self.preload(a:payload.ns)
|
2014-01-16 04:37:41 +00:00
|
|
|
endif
|
2014-01-17 22:51:14 +00:00
|
|
|
return call('s:conn_try', [self.connection, 'message', a:payload] + a:000, self)
|
2014-01-09 05:52:25 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-16 04:37:41 +00:00
|
|
|
function! s:repl.preload(lib) dict abort
|
2014-01-12 04:43:08 +00:00
|
|
|
if !empty(a:lib) && a:lib !=# self.user_ns() && !get(self.requires, a:lib)
|
2012-12-05 01:15:33 +00:00
|
|
|
let reload = has_key(self.requires, a:lib) ? ' :reload' : ''
|
|
|
|
let self.requires[a:lib] = 0
|
2014-01-17 22:51:14 +00:00
|
|
|
let clone = s:conn_try(self.connection, 'clone')
|
2014-06-27 17:30:12 +00:00
|
|
|
if self.user_ns() ==# 'user'
|
|
|
|
let qsym = s:qsym(a:lib)
|
|
|
|
let expr = '(when-not (find-ns '.qsym.') (try'
|
|
|
|
\ . ' (#''clojure.core/load-one '.qsym.' true true)'
|
|
|
|
\ . ' (catch Exception e (when-not (find-ns '.qsym.') (throw e)))))'
|
|
|
|
else
|
|
|
|
let expr = '(ns '.self.user_ns().' (:require '.a:lib.reload.'))'
|
|
|
|
endif
|
2014-01-12 05:13:32 +00:00
|
|
|
try
|
2014-06-27 17:30:12 +00:00
|
|
|
let result = clone.eval(expr, {'ns': self.user_ns()})
|
2014-01-12 05:13:32 +00:00
|
|
|
finally
|
|
|
|
call clone.close()
|
|
|
|
endtry
|
2012-12-15 20:41:33 +00:00
|
|
|
let self.requires[a:lib] = !has_key(result, 'ex')
|
2013-03-06 22:15:57 +00:00
|
|
|
if has_key(result, 'ex')
|
2014-01-12 05:13:32 +00:00
|
|
|
return result
|
2013-03-06 22:15:57 +00:00
|
|
|
endif
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
2014-01-12 05:13:32 +00:00
|
|
|
return {}
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-18 00:37:46 +00:00
|
|
|
let s:piggieback = copy(s:repl)
|
|
|
|
|
2014-01-19 22:56:32 +00:00
|
|
|
function! s:repl.piggieback(arg, ...) abort
|
|
|
|
if a:0 && a:1
|
|
|
|
if len(self.piggiebacks)
|
|
|
|
call remove(self.piggiebacks, 0)
|
|
|
|
endif
|
|
|
|
return {}
|
|
|
|
endif
|
2014-05-13 22:13:13 +00:00
|
|
|
|
|
|
|
let connection = s:conn_try(self.connection, 'clone')
|
2014-01-19 22:56:32 +00:00
|
|
|
if empty(a:arg)
|
|
|
|
let arg = ''
|
2014-05-13 22:13:13 +00:00
|
|
|
elseif a:arg =~# '^\d\{1,5}$'
|
|
|
|
call connection.eval("(require 'cljs.repl.browser)")
|
|
|
|
let port = matchstr(a:arg, '^\d\{1,5}$')
|
|
|
|
let arg = ' :repl-env (cljs.repl.browser/repl-env :port '.port.')'
|
2014-01-19 22:56:32 +00:00
|
|
|
else
|
|
|
|
let arg = ' :repl-env ' . a:arg
|
|
|
|
endif
|
|
|
|
let response = connection.eval('(cemerick.piggieback/cljs-repl'.arg.')')
|
|
|
|
|
|
|
|
if empty(get(response, 'ex'))
|
|
|
|
call insert(self.piggiebacks, extend({'connection': connection}, deepcopy(s:piggieback)))
|
|
|
|
return {}
|
|
|
|
endif
|
|
|
|
call connection.close()
|
|
|
|
return response
|
|
|
|
endfunction
|
|
|
|
|
2014-01-18 00:37:46 +00:00
|
|
|
function! s:piggieback.user_ns() abort
|
|
|
|
return 'cljs.user'
|
|
|
|
endfunction
|
|
|
|
|
2014-01-18 03:16:18 +00:00
|
|
|
function! s:piggieback.eval(expr, options) abort
|
|
|
|
let options = copy(a:options)
|
|
|
|
if has_key(options, 'file_path')
|
|
|
|
call remove(options, 'file_path')
|
|
|
|
endif
|
|
|
|
return call(s:repl.eval, [a:expr, options], self)
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:register_connection(conn, ...) abort
|
2014-01-18 00:37:46 +00:00
|
|
|
call insert(s:repls, extend({'connection': a:conn, 'piggiebacks': []}, deepcopy(s:repl)))
|
2012-12-04 06:06:22 +00:00
|
|
|
if a:0 && a:1 !=# ''
|
|
|
|
let s:repl_paths[a:1] = s:repls[0]
|
|
|
|
endif
|
|
|
|
return s:repls[0]
|
|
|
|
endfunction
|
|
|
|
|
2014-01-17 09:24:46 +00:00
|
|
|
function! s:unregister_connection(conn) abort
|
|
|
|
call filter(s:repl_paths, 'v:val.connection.transport isnot# a:conn.transport')
|
|
|
|
call filter(s:repls, 'v:val.connection.transport isnot# a:conn.transport')
|
2014-04-04 01:03:26 +00:00
|
|
|
call filter(s:repl_portfiles, 'v:val.connection.transport isnot# a:conn.transport')
|
|
|
|
endfunction
|
|
|
|
|
2014-04-11 03:25:59 +00:00
|
|
|
function! fireplace#register_port_file(portfile, ...) abort
|
2014-04-04 01:03:26 +00:00
|
|
|
let old = get(s:repl_portfiles, a:portfile, {})
|
|
|
|
if has_key(old, 'time') && getftime(a:portfile) !=# old.time
|
|
|
|
call s:unregister_connection(old.connection)
|
|
|
|
let old = {}
|
|
|
|
endif
|
|
|
|
if empty(old) && getfsize(a:portfile) > 0
|
|
|
|
let port = matchstr(readfile(a:portfile, 'b', 1)[0], '\d\+')
|
|
|
|
try
|
2014-06-27 15:17:13 +00:00
|
|
|
let conn = fireplace#nrepl_connection#open(port)
|
2014-07-31 04:57:15 +00:00
|
|
|
let s:repl_portfiles[a:portfile] = {
|
|
|
|
\ 'time': getftime(a:portfile),
|
|
|
|
\ 'connection': conn}
|
2014-04-04 01:03:26 +00:00
|
|
|
call s:register_connection(conn, a:0 ? a:1 : '')
|
|
|
|
return conn
|
|
|
|
catch /^nREPL Connection Error:/
|
|
|
|
if &verbose
|
|
|
|
echohl WarningMSG
|
|
|
|
echomsg v:exception
|
|
|
|
echohl None
|
|
|
|
endif
|
|
|
|
return {}
|
|
|
|
endtry
|
|
|
|
else
|
|
|
|
return get(old, 'connection', {})
|
|
|
|
endif
|
2014-01-17 09:24:46 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: :Connect
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-01-06 16:38:37 +00:00
|
|
|
command! -bar -complete=customlist,s:connect_complete -nargs=* FireplaceConnect :exe s:Connect(<f-args>)
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! fireplace#input_host_port() abort
|
2012-12-04 06:06:22 +00:00
|
|
|
let arg = input('Host> ', 'localhost')
|
|
|
|
if arg ==# ''
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
echo "\n"
|
|
|
|
let arg .= ':' . input('Port> ')
|
|
|
|
if arg =~# ':$'
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
echo "\n"
|
|
|
|
return arg
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:protos() abort
|
2014-07-01 13:19:32 +00:00
|
|
|
return map(split(globpath(&runtimepath, 'autoload/fireplace/*_connection.vim'), "\n"), 'fnamemodify(v:val, ":t")[0:-16]')
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:connect_complete(A, L, P) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
let proto = matchstr(a:A, '\w\+\ze://')
|
|
|
|
if proto ==# ''
|
|
|
|
let options = map(s:protos(), 'v:val."://"')
|
|
|
|
else
|
|
|
|
let rest = matchstr(a:A, '://\zs.*')
|
|
|
|
try
|
2014-06-27 15:17:13 +00:00
|
|
|
let options = fireplace#{proto}_connection#complete(rest)
|
2012-12-04 06:06:22 +00:00
|
|
|
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
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:Connect(...) abort
|
2014-01-06 16:38:37 +00:00
|
|
|
if (a:0 ? a:1 : '') =~# '^\w\+://'
|
2014-01-06 17:49:12 +00:00
|
|
|
let [proto, arg] = split(a:1, '://')
|
2014-06-29 22:54:07 +00:00
|
|
|
elseif (a:0 ? a:1 : '') =~# '^\%([[:alnum:].-]\+:\)\=\d\+$'
|
|
|
|
let [proto, arg] = ['nrepl', a:1]
|
2014-01-06 16:38:37 +00:00
|
|
|
elseif a:0
|
2012-12-04 06:06:22 +00:00
|
|
|
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
|
2014-06-29 22:53:41 +00:00
|
|
|
let arg = fireplace#{proto}_connection#prompt()
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
try
|
2014-06-29 22:53:41 +00:00
|
|
|
let connection = fireplace#{proto}_connection#open(arg)
|
2012-12-04 06:06:22 +00:00
|
|
|
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
|
2012-12-10 22:24:28 +00:00
|
|
|
let path = fnamemodify(exists('b:java_root') ? b:java_root : fnamemodify(expand('%'), ':p:s?.*\zs[\/]src[\/].*??'), ':~')
|
2014-01-06 16:38:37 +00:00
|
|
|
let root = a:0 > 1 ? expand(a:2) : input('Scope connection to: ', path, 'dir')
|
2013-02-14 05:12:45 +00:00
|
|
|
if root !=# '' && root !=# '-'
|
|
|
|
let s:repl_paths[fnamemodify(root, ':p:s?.\zs[\/]$??')] = client
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2014-01-19 22:56:32 +00:00
|
|
|
function! s:piggieback(arg, remove) abort
|
|
|
|
let response = fireplace#platform().piggieback(a:arg, a:remove)
|
|
|
|
call s:output_response(response)
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_connect
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-06-29 23:03:49 +00:00
|
|
|
autocmd FileType clojure command! -buffer -bar -complete=customlist,s:connect_complete -nargs=*
|
|
|
|
\ Connect FireplaceConnect <args>
|
|
|
|
autocmd FileType clojure command! -buffer -bang -complete=customlist,fireplace#eval_complete -nargs=*
|
|
|
|
\ Piggieback call s:piggieback(<q-args>, <bang>0)
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Java runner
|
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()
|
|
|
|
|
2014-01-17 10:03:09 +00:00
|
|
|
function! s:spawning_eval(classpath, expr, ns) abort
|
|
|
|
if a:ns !=# '' && a:ns !=# 'user'
|
|
|
|
let ns = '(require '.s:qsym(a:ns).') (in-ns '.s:qsym(a: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'
|
2014-01-17 10:03:09 +00:00
|
|
|
let command = java_cmd.' -cp '.shellescape(a:classpath).' clojure.main -e ' .
|
2013-01-07 00:04:06 +00:00
|
|
|
\ shellescape(
|
2014-04-13 00:41:09 +00:00
|
|
|
\ '(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' .
|
2014-05-02 02:26:53 +00:00
|
|
|
\ ' (require ''clojure.repl ''clojure.java.javadoc) '.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' .
|
2014-04-13 00:41:09 +00:00
|
|
|
\ ' (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)')
|
2014-01-12 18:34:52 +00:00
|
|
|
let captured = system(command)
|
2012-12-15 06:32:08 +00:00
|
|
|
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)
|
2014-05-05 04:38:30 +00:00
|
|
|
if empty(result.ex)
|
|
|
|
let result.status = ['done']
|
|
|
|
else
|
|
|
|
let result.status = ['eval-error', 'done']
|
|
|
|
endif
|
2012-12-26 01:26:45 +00:00
|
|
|
call filter(result, '!empty(v:val)')
|
|
|
|
if v:shell_error && get(result, 'ex', '') ==# ''
|
2014-01-12 18:34:52 +00:00
|
|
|
throw 'Error running Java: '.get(split(captured, "\n"), -1, '')
|
2012-12-04 06:06:22 +00:00
|
|
|
else
|
2012-12-15 06:32:08 +00:00
|
|
|
return result
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-01-17 10:03:09 +00:00
|
|
|
let s:oneoff = {}
|
|
|
|
|
|
|
|
function! s:oneoff.user_ns() abort
|
|
|
|
return 'user'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:oneoff.path() dict abort
|
2014-04-09 16:43:35 +00:00
|
|
|
return self._path
|
2014-01-17 10:03:09 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:oneoff.eval(expr, options) dict abort
|
2014-04-03 23:36:10 +00:00
|
|
|
if !empty(get(a:options, 'session', 1))
|
|
|
|
throw 'Fireplace: no live REPL connection'
|
2014-01-17 10:03:09 +00:00
|
|
|
endif
|
2014-05-05 04:38:30 +00:00
|
|
|
let result = s:spawning_eval(join(self.path(), has('win32') ? ';' : ':'),
|
|
|
|
\ a:expr, get(a:options, 'ns', self.user_ns()))
|
|
|
|
if has_key(a:options, 'id')
|
|
|
|
let result.id = a:options.id
|
|
|
|
endif
|
|
|
|
return result
|
2014-01-17 10:03:09 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-19 22:56:32 +00:00
|
|
|
function! s:oneoff.message(...) abort
|
2014-04-03 23:36:10 +00:00
|
|
|
throw 'Fireplace: no live REPL connection'
|
2014-01-09 05:52:25 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-19 22:56:32 +00:00
|
|
|
let s:oneoff.piggieback = s:oneoff.message
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Client
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-01-11 03:49:25 +00:00
|
|
|
function! s:buf() abort
|
2013-01-13 15:49:54 +00:00
|
|
|
if exists('s:input')
|
2014-01-11 03:49:25 +00:00
|
|
|
return s:input
|
2013-01-13 15:49:54 +00:00
|
|
|
elseif has_key(s:qffiles, expand('%:p'))
|
2014-01-11 03:49:25 +00:00
|
|
|
return s:qffiles[expand('%:p')].buffer
|
2013-01-13 15:49:54 +00:00
|
|
|
else
|
2014-01-11 03:49:25 +00:00
|
|
|
return '%'
|
2013-01-13 15:49:54 +00:00
|
|
|
endif
|
2014-01-11 03:49:25 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-13 00:54:08 +00:00
|
|
|
function! s:includes_file(file, path) abort
|
|
|
|
let file = substitute(a:file, '\C^zipfile:\(.*\)::', '\1/', '')
|
|
|
|
let file = substitute(file, '\C^fugitive:[\/][\/]\(.*\)\.git[\/][\/][^\/]\+[\/]', '\1', '')
|
|
|
|
for path in a:path
|
|
|
|
if file[0 : len(path)-1] ==? path
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2014-04-09 16:43:35 +00:00
|
|
|
function! s:path_extract(path)
|
2014-04-09 17:20:13 +00:00
|
|
|
let path = []
|
|
|
|
if a:path =~# '\.jar'
|
|
|
|
for elem in split(substitute(a:path, ',$', '', ''), ',')
|
|
|
|
if elem ==# ''
|
|
|
|
let path += ['.']
|
|
|
|
else
|
|
|
|
let path += split(glob(substitute(elem, '\\\ze[\\ ,]', '', 'g'), 1), "\n")
|
|
|
|
endif
|
|
|
|
endfor
|
2014-04-09 16:43:35 +00:00
|
|
|
endif
|
2014-04-09 17:20:13 +00:00
|
|
|
return path
|
2014-04-09 16:43:35 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-12 18:58:38 +00:00
|
|
|
function! fireplace#path(...) abort
|
|
|
|
let buf = a:0 ? a:1 : s:buf()
|
|
|
|
for repl in s:repls
|
2014-01-13 00:54:08 +00:00
|
|
|
if s:includes_file(fnamemodify(bufname(buf), ':p'), repl.path())
|
2014-01-12 18:58:38 +00:00
|
|
|
return repl.path()
|
|
|
|
endif
|
|
|
|
endfor
|
2014-04-09 16:43:35 +00:00
|
|
|
return s:path_extract(getbufvar(buf, '&path'))
|
2014-01-12 18:58:38 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-18 00:37:46 +00:00
|
|
|
function! fireplace#platform(...) abort
|
2014-04-04 01:03:26 +00:00
|
|
|
for [k, v] in items(s:repl_portfiles)
|
|
|
|
if getftime(k) != v.time
|
|
|
|
call s:unregister_connection(v.connection)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2014-04-04 01:09:00 +00:00
|
|
|
let portfile = findfile('.nrepl-port', '.;')
|
2014-04-04 01:03:26 +00:00
|
|
|
if !empty(portfile)
|
2014-04-11 03:25:59 +00:00
|
|
|
call fireplace#register_port_file(portfile, fnamemodify(portfile, ':h'))
|
2014-04-04 01:03:26 +00:00
|
|
|
endif
|
2014-01-11 03:49:25 +00:00
|
|
|
silent doautocmd User FireplacePreConnect
|
2014-04-04 01:03:26 +00:00
|
|
|
|
2014-01-11 03:49:25 +00:00
|
|
|
let buf = a:0 ? a:1 : s:buf()
|
2012-12-04 06:06:22 +00:00
|
|
|
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
|
|
|
|
for repl in s:repls
|
2014-01-16 00:25:55 +00:00
|
|
|
if s:includes_file(fnamemodify(bufname(buf), ':p'), repl.path())
|
2012-12-04 06:06:22 +00:00
|
|
|
return repl
|
|
|
|
endif
|
|
|
|
endfor
|
2014-04-09 16:43:35 +00:00
|
|
|
let path = s:path_extract(getbufvar(buf, '&path'))
|
|
|
|
if !empty(path) && fnamemodify(bufname(buf), ':e') =~# '^cljx\=$'
|
|
|
|
return extend({'_path': path, 'nr': bufnr(buf)}, s:oneoff)
|
2013-01-06 22:59:40 +00:00
|
|
|
endif
|
2014-04-03 23:36:10 +00:00
|
|
|
throw 'Fireplace: :Connect to a REPL or install classpath.vim'
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-18 00:37:46 +00:00
|
|
|
function! fireplace#client(...) abort
|
|
|
|
let buf = a:0 ? a:1 : s:buf()
|
|
|
|
let client = fireplace#platform(buf)
|
|
|
|
if fnamemodify(bufname(buf), ':e') ==# 'cljs'
|
|
|
|
if !has_key(client, 'connection')
|
2014-04-03 23:36:10 +00:00
|
|
|
throw 'Fireplace: no live REPL connection'
|
2014-01-18 00:37:46 +00:00
|
|
|
endif
|
|
|
|
if empty(client.piggiebacks)
|
2014-01-19 22:56:32 +00:00
|
|
|
let result = client.piggieback('')
|
2014-01-18 00:37:46 +00:00
|
|
|
if has_key(result, 'ex')
|
|
|
|
return result
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return client.piggiebacks[0]
|
|
|
|
endif
|
|
|
|
return client
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! fireplace#message(payload, ...) abort
|
2014-01-16 02:36:58 +00:00
|
|
|
let client = fireplace#client()
|
2014-01-09 05:52:25 +00:00
|
|
|
let payload = copy(a:payload)
|
|
|
|
if !has_key(payload, 'ns')
|
|
|
|
let payload.ns = fireplace#ns()
|
2014-04-13 00:09:26 +00:00
|
|
|
elseif empty(payload.ns)
|
|
|
|
unlet payload.ns
|
2014-01-09 05:52:25 +00:00
|
|
|
endif
|
2014-01-10 03:41:51 +00:00
|
|
|
return call(client.message, [payload] + a:000, client)
|
2014-01-09 05:52:25 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-04-13 00:06:12 +00:00
|
|
|
function! fireplace#op_available(op) abort
|
2014-04-13 01:49:45 +00:00
|
|
|
try
|
|
|
|
let client = fireplace#platform()
|
|
|
|
if has_key(client, 'connection')
|
|
|
|
return client.connection.has_op(a:op)
|
|
|
|
endif
|
|
|
|
catch /^Fireplace: :Connect to a REPL/
|
|
|
|
endtry
|
2014-04-13 00:06:12 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-09 01:05:46 +00:00
|
|
|
function! fireplace#findresource(resource, ...) abort
|
2013-01-08 03:41:04 +00:00
|
|
|
if a:resource ==# ''
|
|
|
|
return ''
|
|
|
|
endif
|
2014-01-15 23:14:32 +00:00
|
|
|
let resource = a:resource
|
|
|
|
if a:0 > 2 && type(a:3) == type([])
|
|
|
|
let suffixes = a:3
|
|
|
|
else
|
|
|
|
let suffixes = [''] + split(get(a:000, 2, ''), ',')
|
2013-01-08 03:41:04 +00:00
|
|
|
endif
|
2014-01-15 23:14:32 +00:00
|
|
|
for dir in a:0 ? a:1 : fireplace#path()
|
|
|
|
for suffix in suffixes
|
|
|
|
if fnamemodify(dir, ':e') ==# 'jar' && index(fireplace#jar_contents(dir), resource . suffix) >= 0
|
|
|
|
return 'zipfile:' . dir . '::' . resource . suffix
|
|
|
|
elseif filereadable(dir . '/' . resource . suffix)
|
|
|
|
return dir . (exists('+shellslash') && !&shellslash ? '\' : '/') . resource . suffix
|
|
|
|
endif
|
|
|
|
endfor
|
2013-01-08 03:41:04 +00:00
|
|
|
endfor
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2013-01-05 03:26:50 +00:00
|
|
|
function! s:output_response(response) abort
|
2014-03-20 17:11:10 +00:00
|
|
|
let substitution_pat = '\e\[[0-9;]*m\|\r\|\n$'
|
2013-01-05 03:26:50 +00:00
|
|
|
if get(a:response, 'err', '') !=# ''
|
2012-12-15 06:32:08 +00:00
|
|
|
echohl ErrorMSG
|
2014-03-20 17:11:10 +00:00
|
|
|
echo substitute(a:response.err, substitution_pat, '', 'g')
|
2012-12-15 06:32:08 +00:00
|
|
|
echohl NONE
|
|
|
|
endif
|
2013-01-05 03:26:50 +00:00
|
|
|
if get(a:response, 'out', '') !=# ''
|
2014-03-20 17:11:10 +00:00
|
|
|
echo substitute(a:response.out, substitution_pat, '', 'g')
|
2013-01-05 03:26:50 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:eval(expr, ...) abort
|
|
|
|
let options = a:0 ? copy(a:1) : {}
|
2014-01-16 02:36:58 +00:00
|
|
|
let client = fireplace#client()
|
2013-01-05 03:26:50 +00:00
|
|
|
if !has_key(options, 'ns')
|
2013-03-12 19:37:37 +00:00
|
|
|
let options.ns = fireplace#ns()
|
2012-12-15 06:32:08 +00:00
|
|
|
endif
|
2013-01-05 03:26:50 +00:00
|
|
|
return client.eval(a:expr, options)
|
|
|
|
endfunction
|
|
|
|
|
2013-01-13 15:49:54 +00:00
|
|
|
function! s:temp_response(response) abort
|
|
|
|
let output = []
|
2013-10-17 19:06:54 +00:00
|
|
|
if get(a:response, 'err', '') !=# ''
|
|
|
|
let output = map(split(a:response.err, "\n"), '";!!".v:val')
|
|
|
|
endif
|
2013-01-13 15:49:54 +00:00
|
|
|
if get(a:response, 'out', '') !=# ''
|
|
|
|
let output = map(split(a:response.out, "\n"), '";".v:val')
|
|
|
|
endif
|
|
|
|
if has_key(a:response, 'value')
|
|
|
|
let output += [a:response.value]
|
|
|
|
endif
|
|
|
|
let temp = tempname().'.clj'
|
|
|
|
call writefile(output, temp)
|
|
|
|
return temp
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
if !exists('s:history')
|
|
|
|
let s:history = []
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists('s:qffiles')
|
|
|
|
let s:qffiles = {}
|
|
|
|
endif
|
|
|
|
|
|
|
|
function! s:qfentry(entry) abort
|
|
|
|
if !has_key(a:entry, 'tempfile')
|
|
|
|
let a:entry.tempfile = s:temp_response(a:entry.response)
|
|
|
|
endif
|
|
|
|
let s:qffiles[a:entry.tempfile] = a:entry
|
2013-04-11 02:33:48 +00:00
|
|
|
return {'filename': a:entry.tempfile, 'text': a:entry.code, 'type': 'E'}
|
2013-01-13 15:49:54 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:qfhistory() abort
|
|
|
|
let list = []
|
|
|
|
for entry in reverse(s:history)
|
|
|
|
if !has_key(entry, 'tempfile')
|
|
|
|
let entry.tempfile = s:temp_response(entry.response)
|
|
|
|
endif
|
|
|
|
call extend(list, [s:qfentry(entry)])
|
|
|
|
endfor
|
|
|
|
return list
|
|
|
|
endfunction
|
|
|
|
|
2014-01-08 00:46:40 +00:00
|
|
|
function! fireplace#session_eval(expr, ...) abort
|
2013-12-30 19:19:11 +00:00
|
|
|
let response = s:eval(a:expr, a:0 ? a:1 : {})
|
2013-01-05 03:26:50 +00:00
|
|
|
|
2013-10-17 19:06:54 +00:00
|
|
|
if !empty(get(response, 'value', '')) || !empty(get(response, 'err', ''))
|
2013-03-12 19:37:37 +00:00
|
|
|
call insert(s:history, {'buffer': bufnr(''), 'code': a:expr, 'ns': fireplace#ns(), 'response': response})
|
2013-01-13 15:49:54 +00:00
|
|
|
endif
|
|
|
|
if len(s:history) > &history
|
|
|
|
call remove(s:history, &history, -1)
|
|
|
|
endif
|
|
|
|
|
2013-01-08 03:41:04 +00:00
|
|
|
if !empty(get(response, 'stacktrace', []))
|
2013-01-13 15:49:54 +00:00
|
|
|
let nr = 0
|
|
|
|
if has_key(s:qffiles, expand('%:p'))
|
|
|
|
let nr = winbufnr(s:qffiles[expand('%:p')].buffer)
|
|
|
|
endif
|
|
|
|
if nr != -1
|
2013-03-12 19:37:37 +00:00
|
|
|
call setloclist(nr, fireplace#quickfix_for(response.stacktrace))
|
2013-01-13 15:49:54 +00:00
|
|
|
endif
|
2013-01-08 03:41:04 +00:00
|
|
|
endif
|
|
|
|
|
2013-01-24 02:25:39 +00:00
|
|
|
call s:output_response(response)
|
|
|
|
|
2013-01-05 03:26:50 +00:00
|
|
|
if get(response, 'ex', '') !=# ''
|
|
|
|
let err = 'Clojure: '.response.ex
|
|
|
|
elseif has_key(response, 'value')
|
|
|
|
return response.value
|
2012-12-15 06:32:08 +00:00
|
|
|
else
|
2013-03-12 19:37:37 +00:00
|
|
|
let err = 'fireplace.vim: Something went wrong: '.string(response)
|
2012-12-15 06:32:08 +00:00
|
|
|
endif
|
|
|
|
throw err
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-12 19:58:28 +00:00
|
|
|
function! fireplace#eval(...) abort
|
|
|
|
return call('fireplace#session_eval', a:000)
|
|
|
|
endfunction
|
|
|
|
|
2014-01-01 01:04:33 +00:00
|
|
|
function! fireplace#echo_session_eval(expr, ...) abort
|
2013-01-07 06:55:08 +00:00
|
|
|
try
|
2014-01-01 01:04:33 +00:00
|
|
|
echo fireplace#session_eval(a:expr, a:0 ? a:1 : {})
|
2013-01-07 06:55:08 +00:00
|
|
|
catch /^Clojure:/
|
2014-04-03 23:36:10 +00:00
|
|
|
catch
|
|
|
|
echohl ErrorMSG
|
|
|
|
echomsg v:exception
|
|
|
|
echohl NONE
|
2013-01-07 06:55:08 +00:00
|
|
|
endtry
|
2013-01-05 04:35:08 +00:00
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#evalprint(expr) abort
|
|
|
|
return fireplace#echo_session_eval(a:expr)
|
2013-02-08 05:32:52 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-21 10:08:55 +00:00
|
|
|
function! fireplace#macroexpand(fn, form) abort
|
2013-08-05 23:17:33 +00:00
|
|
|
return fireplace#evalprint('('.a:fn.' (quote '.a:form.'))')
|
2013-04-21 10:08:55 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
let g:fireplace#reader =
|
2013-02-08 15:26:14 +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))) "]")' .
|
|
|
|
\ ' (true? x) "1"' .
|
|
|
|
\ ' (false? x) "0"' .
|
|
|
|
\ ' (number? x) (pr-str x)' .
|
|
|
|
\ ' (keyword? x) (pr-str (name x))' .
|
|
|
|
\ ' :else (pr-str (str x)))) %s))'
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#evalparse(expr, ...) abort
|
2013-01-19 21:30:55 +00:00
|
|
|
let options = extend({'session': 0}, a:0 ? a:1 : {})
|
2013-03-12 19:37:37 +00:00
|
|
|
let response = s:eval(printf(g:fireplace#reader, a:expr), options)
|
2013-01-05 03:26:50 +00:00
|
|
|
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
|
2013-03-12 19:37:37 +00:00
|
|
|
let err = 'fireplace.vim: Something went wrong: '.string(response)
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
2013-01-05 03:26:50 +00:00
|
|
|
throw err
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-07-04 21:14:10 +00:00
|
|
|
function! fireplace#query(expr, ...) abort
|
|
|
|
return firepalce#evalparse(a:expr, a:0 ? a:1 : {})
|
|
|
|
endfunction
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Quickfix
|
2014-04-14 04:22:43 +00:00
|
|
|
|
|
|
|
function! s:qfmassage(line, path) abort
|
|
|
|
let entry = {'text': a:line}
|
|
|
|
let match = matchlist(a:line, '\(\S\+\)\s\=(\(\S\+\))')
|
|
|
|
if !empty(match)
|
|
|
|
let [_, class, file; __] = match
|
|
|
|
if file =~# '^NO_SOURCE_FILE:' || file !~# ':'
|
|
|
|
let entry.resource = ''
|
|
|
|
let entry.lnum = 0
|
|
|
|
else
|
|
|
|
let truncated = substitute(class, '\.[A-Za-z0-9_]\+\%([$/].*\)$', '', '')
|
|
|
|
let entry.resource = tr(truncated, '.', '/').'/'.split(file, ':')[0]
|
|
|
|
let entry.lnum = split(file, ':')[-1]
|
|
|
|
endif
|
|
|
|
let entry.filename = fireplace#findresource(entry.resource, a:path)
|
|
|
|
if empty(entry.filename)
|
|
|
|
let entry.lnum = 0
|
|
|
|
else
|
|
|
|
let entry.text = class
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return entry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! fireplace#quickfix_for(stacktrace) abort
|
|
|
|
let path = fireplace#path()
|
|
|
|
return map(copy(a:stacktrace), 's:qfmassage(v:val, path)')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:massage_quickfix() abort
|
|
|
|
let p = substitute(matchstr(','.&errorformat, ',classpath\zs\%(\\.\|[^\,]\)*'), '\\\ze[\,%]', '', 'g')
|
|
|
|
if empty(p)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let path = p[0] ==# ',' ? s:path_extract(p[1:-1]) : split(p[1:-1], p[0])
|
|
|
|
let qflist = getqflist()
|
|
|
|
for entry in qflist
|
|
|
|
call extend(entry, s:qfmassage(get(entry, 'text', ''), path))
|
|
|
|
endfor
|
|
|
|
call setqflist(qflist, 'replace')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fireplace_quickfix
|
|
|
|
autocmd!
|
|
|
|
autocmd QuickFixCmdPost make,cfile,cgetfile call s:massage_quickfix()
|
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Eval
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
let fireplace#skip = 'synIDattr(synID(line("."),col("."),1),"name") =~? "comment\\|string\\|char"'
|
2012-12-04 06:06:22 +00:00
|
|
|
|
|
|
|
function! s:opfunc(type) abort
|
|
|
|
let sel_save = &selection
|
|
|
|
let cb_save = &clipboard
|
|
|
|
let reg_save = @@
|
|
|
|
try
|
|
|
|
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
|
2014-03-30 00:51:14 +00:00
|
|
|
if type(a:type) == type(0)
|
|
|
|
let open = '[[{(]'
|
|
|
|
let close = '[]})]'
|
|
|
|
if getline('.')[col('.')-1] =~# close
|
|
|
|
let [line1, col1] = searchpairpos(open, '', close, 'bn', g:fireplace#skip)
|
|
|
|
let [line2, col2] = [line('.'), col('.')]
|
|
|
|
else
|
|
|
|
let [line1, col1] = searchpairpos(open, '', close, 'bcn', g:fireplace#skip)
|
|
|
|
let [line2, col2] = searchpairpos(open, '', close, 'n', g:fireplace#skip)
|
|
|
|
endif
|
|
|
|
while col1 > 1 && getline(line1)[col1-2] =~# '[#''`~@]'
|
|
|
|
let col1 -= 1
|
|
|
|
endwhile
|
|
|
|
call setpos("'[", [0, line1, col1, 0])
|
|
|
|
call setpos("']", [0, line2, col2, 0])
|
|
|
|
silent exe "normal! `[v`]y"
|
|
|
|
elseif a:type =~# '^.$'
|
2012-12-04 06:06:22 +00:00
|
|
|
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'
|
2013-03-12 19:37:37 +00:00
|
|
|
call searchpair('(','',')', 'Wbcr', g:fireplace#skip)
|
2012-12-04 06:06:22 +00:00
|
|
|
silent exe "normal! vaby"
|
|
|
|
else
|
|
|
|
silent exe "normal! `[v`]y"
|
|
|
|
endif
|
2012-12-09 19:54:11 +00:00
|
|
|
redraw
|
2014-03-30 00:51:14 +00:00
|
|
|
return repeat("\n", line("'<")-1) . repeat(" ", col("'<")-1) . @@
|
2012-12-04 06:06:22 +00:00
|
|
|
finally
|
|
|
|
let @@ = reg_save
|
|
|
|
let &selection = sel_save
|
|
|
|
let &clipboard = cb_save
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:filterop(type) abort
|
|
|
|
let reg_save = @@
|
2014-04-03 20:23:13 +00:00
|
|
|
let sel_save = &selection
|
|
|
|
let cb_save = &clipboard
|
2012-12-04 06:06:22 +00:00
|
|
|
try
|
2014-04-03 20:23:13 +00:00
|
|
|
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
|
2012-12-04 06:06:22 +00:00
|
|
|
let expr = s:opfunc(a:type)
|
2014-04-24 03:16:02 +00:00
|
|
|
let @@ = fireplace#session_eval(matchstr(expr, '^\n\+').expr).matchstr(expr, '\n\+$')
|
2012-12-04 06:06:22 +00:00
|
|
|
if @@ !~# '^\n*$'
|
|
|
|
normal! gvp
|
|
|
|
endif
|
|
|
|
catch /^Clojure:/
|
|
|
|
return ''
|
|
|
|
finally
|
|
|
|
let @@ = reg_save
|
2014-04-03 20:23:13 +00:00
|
|
|
let &selection = sel_save
|
|
|
|
let &clipboard = cb_save
|
2012-12-04 06:06:22 +00:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2013-04-21 10:08:55 +00:00
|
|
|
function! s:macroexpandop(type) abort
|
2013-08-05 23:17:33 +00:00
|
|
|
call fireplace#macroexpand("clojure.walk/macroexpand-all", s:opfunc(a:type))
|
2013-04-21 10:08:55 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:macroexpand1op(type) abort
|
2014-04-13 01:04:25 +00:00
|
|
|
call fireplace#macroexpand("macroexpand-1", s:opfunc(a:type))
|
2013-04-21 10:08:55 +00:00
|
|
|
endfunction
|
|
|
|
|
2012-12-04 06:06:22 +00:00
|
|
|
function! s:printop(type) abort
|
2012-12-08 00:27:24 +00:00
|
|
|
let s:todo = s:opfunc(a:type)
|
2013-03-12 19:37:37 +00:00
|
|
|
call feedkeys("\<Plug>FireplacePrintLast")
|
2012-12-08 00:27:24 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:print_last() abort
|
2014-03-30 00:51:14 +00:00
|
|
|
call fireplace#echo_session_eval(s:todo, {'file_path': s:buffer_path()})
|
2012-12-08 00:27:24 +00:00
|
|
|
return ''
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:editop(type) abort
|
2014-05-20 07:50:31 +00:00
|
|
|
call feedkeys(eval('"\'.&cedit.'"') . "\<Home>", 'n')
|
2014-03-30 00:51:14 +00:00
|
|
|
let input = s:input(substitute(substitute(substitute(
|
|
|
|
\ s:opfunc(a:type), "\s*;[^\n\"]*\\%(\n\\@=\\|$\\)", '', 'g'),
|
|
|
|
\ '\n\+\s*', ' ', 'g'),
|
|
|
|
\ '^\s*', '', ''))
|
2013-01-05 04:35:08 +00:00
|
|
|
if input !=# ''
|
2013-03-12 19:37:37 +00:00
|
|
|
call fireplace#echo_session_eval(input)
|
2013-01-05 04:35:08 +00:00
|
|
|
endif
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:Eval(bang, line1, line2, count, args) abort
|
2013-12-30 19:19:11 +00:00
|
|
|
let options = {}
|
2012-12-04 06:06:22 +00:00
|
|
|
if a:args !=# ''
|
|
|
|
let expr = a:args
|
|
|
|
else
|
|
|
|
if a:count ==# 0
|
2014-01-08 06:35:01 +00:00
|
|
|
let open = '[[{(]'
|
|
|
|
let close = '[]})]'
|
|
|
|
let [line1, col1] = searchpairpos(open, '', close, 'bcrn', g:fireplace#skip)
|
|
|
|
let [line2, col2] = searchpairpos(open, '', close, 'rn', g:fireplace#skip)
|
2014-01-09 19:32:16 +00:00
|
|
|
if !line1 && !line2
|
|
|
|
let [line1, col1] = searchpairpos(open, '', close, 'brn', g:fireplace#skip)
|
|
|
|
let [line2, col2] = searchpairpos(open, '', close, 'crn', g:fireplace#skip)
|
|
|
|
endif
|
2014-01-08 06:35:01 +00:00
|
|
|
while col1 > 1 && getline(line1)[col1-2] =~# '[#''`~@]'
|
|
|
|
let col1 -= 1
|
|
|
|
endwhile
|
2012-12-04 06:06:22 +00:00
|
|
|
else
|
|
|
|
let line1 = a:line1
|
|
|
|
let line2 = a:line2
|
2014-01-08 06:35:01 +00:00
|
|
|
let col1 = 1
|
|
|
|
let col2 = strlen(getline(line2))
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
if !line1 || !line2
|
|
|
|
return ''
|
|
|
|
endif
|
2013-12-30 19:19:11 +00:00
|
|
|
let options.file_path = s:buffer_path()
|
2014-01-08 06:35:01 +00:00
|
|
|
let expr = repeat("\n", line1-1).repeat(" ", col1-1)
|
|
|
|
if line1 == line2
|
|
|
|
let expr .= getline(line1)[col1-1 : col2-1]
|
|
|
|
else
|
2014-03-30 00:51:14 +00:00
|
|
|
let expr .= getline(line1)[col1-1 : -1] . "\n"
|
|
|
|
\ . join(map(getline(line1+1, line2-1), 'v:val . "\n"'))
|
|
|
|
\ . getline(line2)[0 : col2-1]
|
2014-01-08 06:35:01 +00:00
|
|
|
endif
|
2012-12-04 06:06:22 +00:00
|
|
|
if a:bang
|
|
|
|
exe line1.','.line2.'delete _'
|
|
|
|
endif
|
|
|
|
endif
|
2013-01-05 04:35:08 +00:00
|
|
|
if a:bang
|
|
|
|
try
|
2014-01-12 06:04:03 +00:00
|
|
|
let result = fireplace#session_eval(expr, options)
|
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
|
2013-01-05 04:35:08 +00:00
|
|
|
catch /^Clojure:/
|
|
|
|
endtry
|
|
|
|
else
|
2014-01-01 01:04:33 +00:00
|
|
|
call fireplace#echo_session_eval(expr, options)
|
2013-01-05 04:35:08 +00:00
|
|
|
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.
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:actually_input(...) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
return call(function('input'), a:000)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:input(default) abort
|
2013-03-12 19:37:37 +00:00
|
|
|
if !exists('g:FIREPLACE_HISTORY') || type(g:FIREPLACE_HISTORY) != type([])
|
|
|
|
unlet! g:FIREPLACE_HISTORY
|
|
|
|
let g:FIREPLACE_HISTORY = []
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
try
|
|
|
|
let s:input = bufnr('%')
|
2013-03-12 19:37:37 +00:00
|
|
|
let s:oldhist = s:histswap(g:FIREPLACE_HISTORY)
|
|
|
|
return s:actually_input(fireplace#ns().'=> ', a:default, 'customlist,fireplace#eval_complete')
|
2012-12-04 06:06:22 +00:00
|
|
|
finally
|
|
|
|
unlet! s:input
|
|
|
|
if exists('s:oldhist')
|
2013-03-12 19:37:37 +00:00
|
|
|
let g:FIREPLACE_HISTORY = s:histswap(s:oldhist)
|
2012-12-04 06:06:22 +00:00
|
|
|
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 !=# ''
|
2013-03-12 19:37:37 +00:00
|
|
|
call fireplace#echo_session_eval(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
|
2013-03-12 19:37:37 +00:00
|
|
|
return fireplace#session_eval(input)
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
catch /^Clojure:/
|
|
|
|
return ''
|
|
|
|
finally
|
|
|
|
silent! cunmap )
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2013-01-04 14:35:32 +00:00
|
|
|
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
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nnoremap <silent> <Plug>FireplacePrintLast :exe <SID>print_last()<CR>
|
|
|
|
nnoremap <silent> <Plug>FireplacePrint :<C-U>set opfunc=<SID>printop<CR>g@
|
|
|
|
xnoremap <silent> <Plug>FireplacePrint :<C-U>call <SID>printop(visualmode())<CR>
|
2014-03-30 00:51:14 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceCountPrint :<C-U>call <SID>printop(v:count)<CR>
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceFilter :<C-U>set opfunc=<SID>filterop<CR>g@
|
|
|
|
xnoremap <silent> <Plug>FireplaceFilter :<C-U>call <SID>filterop(visualmode())<CR>
|
2014-03-30 00:51:14 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceCountFilter :<C-U>call <SID>filterop(v:count)<CR>
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-04-21 10:08:55 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceMacroExpand :<C-U>set opfunc=<SID>macroexpandop<CR>g@
|
|
|
|
xnoremap <silent> <Plug>FireplaceMacroExpand :<C-U>call <SID>macroexpandop(visualmode())<CR>
|
2014-03-30 00:51:14 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceCountMacroExpand :<C-U>call <SID>macroexpandop(v:count)<CR>
|
|
|
|
nnoremap <silent> <Plug>Fireplace1MacroExpand :<C-U>set opfunc=<SID>macroexpand1op<CR>g@
|
|
|
|
xnoremap <silent> <Plug>Fireplace1MacroExpand :<C-U>call <SID>macroexpand1op(visualmode())<CR>
|
|
|
|
nnoremap <silent> <Plug>Fireplace1MacroExpand :<C-U>call <SID>macroexpand1op(v:count)<CR>
|
2013-04-21 10:08:55 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceEdit :<C-U>set opfunc=<SID>editop<CR>g@
|
|
|
|
xnoremap <silent> <Plug>FireplaceEdit :<C-U>call <SID>editop(visualmode())<CR>
|
2014-03-30 00:51:14 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceCountEdit :<C-U>call <SID>editop(v:count)<CR>
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nnoremap <Plug>FireplacePrompt :exe <SID>inputeval()<CR>
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
noremap! <Plug>FireplaceRecall <C-R>=<SID>recall()<CR>
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-01-13 15:49:54 +00:00
|
|
|
function! s:Last(bang, count) abort
|
|
|
|
if len(s:history) < a:count
|
|
|
|
return 'echoerr "History entry not found"'
|
|
|
|
endif
|
|
|
|
let history = s:qfhistory()
|
|
|
|
let last = s:qfhistory()[a:count-1]
|
|
|
|
execute 'pedit '.last.filename
|
|
|
|
if !&previewwindow
|
|
|
|
let nr = winnr()
|
|
|
|
wincmd p
|
|
|
|
wincmd P
|
|
|
|
endif
|
|
|
|
call setloclist(0, history)
|
|
|
|
silent exe 'llast '.(len(history)-a:count+1)
|
|
|
|
if exists('nr') && a:bang
|
|
|
|
wincmd p
|
|
|
|
exe nr.'wincmd w'
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2014-07-02 22:33:23 +00:00
|
|
|
function! s:set_up_eval() abort
|
2013-03-12 19:37:37 +00:00
|
|
|
command! -buffer -bang -range=0 -nargs=? -complete=customlist,fireplace#eval_complete Eval :exe s:Eval(<bang>0, <line1>, <line2>, <count>, <q-args>)
|
2013-01-13 15:49:54 +00:00
|
|
|
command! -buffer -bang -bar -count=1 Last exe s:Last(<bang>0, <count>)
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-07-03 16:28:23 +00:00
|
|
|
if get(g:, 'fireplace_no_maps') | return | endif
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nmap <buffer> cp <Plug>FireplacePrint
|
2013-12-26 16:16:44 +00:00
|
|
|
nmap <buffer> cpp <Plug>FireplaceCountPrint
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nmap <buffer> c! <Plug>FireplaceFilter
|
2014-03-30 00:51:14 +00:00
|
|
|
nmap <buffer> c!! <Plug>FireplaceCountFilter
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-04-21 10:08:55 +00:00
|
|
|
nmap <buffer> cm <Plug>FireplaceMacroExpand
|
2014-03-30 00:51:14 +00:00
|
|
|
nmap <buffer> cmm <Plug>FireplaceCountMacroExpand
|
|
|
|
nmap <buffer> c1m <Plug>Fireplace1MacroExpand
|
|
|
|
nmap <buffer> c1mm <Plug>FireplaceCount1MacroExpand
|
2013-04-21 10:08:55 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nmap <buffer> cq <Plug>FireplaceEdit
|
2014-03-30 00:51:14 +00:00
|
|
|
nmap <buffer> cqq <Plug>FireplaceCountEdit
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nmap <buffer> cqp <Plug>FireplacePrompt
|
|
|
|
exe 'nmap <buffer> cqc <Plug>FireplacePrompt' . &cedit . 'i'
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
map! <buffer> <C-R>( <Plug>FireplaceRecall
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-07-02 22:33:23 +00:00
|
|
|
function! s:set_up_historical() abort
|
2013-04-01 17:09:19 +00:00
|
|
|
setlocal readonly nomodifiable
|
2013-01-13 15:49:54 +00:00
|
|
|
nnoremap <buffer><silent>q :bdelete<CR>
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:cmdwinenter() abort
|
2012-12-04 06:06:22 +00:00
|
|
|
setlocal filetype=clojure
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:cmdwinleave() abort
|
2012-12-04 06:06:22 +00:00
|
|
|
setlocal filetype< omnifunc<
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_eval
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-07-02 22:33:23 +00:00
|
|
|
autocmd FileType clojure call s:set_up_eval()
|
2013-04-11 02:33:48 +00:00
|
|
|
autocmd BufReadPost * if has_key(s:qffiles, expand('<amatch>:p')) |
|
2014-07-02 22:33:23 +00:00
|
|
|
\ call s:set_up_historical() |
|
2013-01-13 15:49:54 +00:00
|
|
|
\ endif
|
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
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: :Require
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-04-04 04:47:46 +00:00
|
|
|
function! s:Require(bang, echo, ns) abort
|
2014-05-04 22:09:56 +00:00
|
|
|
if &autowrite || &autowriteall
|
|
|
|
silent! wall
|
|
|
|
endif
|
2014-01-19 04:26:27 +00:00
|
|
|
if expand('%:e') ==# 'cljs'
|
|
|
|
let cmd = '(load-file '.s:str(tr(a:ns ==# '' ? fireplace#ns() : a:ns, '-.', '_/').'.cljs').')'
|
|
|
|
else
|
2014-04-13 00:38:33 +00:00
|
|
|
let cmd = ('(require '.s:qsym(a:ns ==# '' ? fireplace#ns() : a:ns).' :reload'.(a:bang ? '-all' : '').')')
|
2014-01-19 04:26:27 +00:00
|
|
|
endif
|
2014-04-04 04:47:46 +00:00
|
|
|
if a:echo
|
|
|
|
echo cmd
|
|
|
|
endif
|
2012-12-04 06:06:22 +00:00
|
|
|
try
|
2014-04-13 00:38:33 +00:00
|
|
|
call fireplace#session_eval(cmd, {'ns': fireplace#client().user_ns()})
|
2012-12-04 06:06:22 +00:00
|
|
|
return ''
|
|
|
|
catch /^Clojure:.*/
|
|
|
|
return ''
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2014-07-02 22:33:23 +00:00
|
|
|
function! s:set_up_require() abort
|
2014-04-04 04:47:46 +00:00
|
|
|
command! -buffer -bar -bang -complete=customlist,fireplace#ns_complete -nargs=? Require :exe s:Require(<bang>0, 1, <q-args>)
|
2014-07-03 16:28:23 +00:00
|
|
|
|
|
|
|
if get(g:, 'fireplace_no_maps') | return | endif
|
2014-04-29 03:01:17 +00:00
|
|
|
nnoremap <silent><buffer> cpr :if expand('%:e') ==# 'cljs'<Bar>Require<Bar>else<Bar>RunTests<Bar>endif<CR>
|
2012-12-08 01:15:48 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_require
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-07-02 22:33:23 +00:00
|
|
|
autocmd FileType clojure call s:set_up_require()
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Go to source
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2014-04-20 22:55:37 +00:00
|
|
|
function! fireplace#info(symbol) abort
|
|
|
|
if fireplace#op_available('info')
|
|
|
|
let response = fireplace#message({'op': 'info', 'symbol': a:symbol})[0]
|
|
|
|
if type(get(response, 'value')) == type({})
|
|
|
|
return response.value
|
2014-01-15 23:14:32 +00:00
|
|
|
endif
|
2013-01-19 21:30:55 +00:00
|
|
|
endif
|
2014-04-20 22:55:37 +00:00
|
|
|
let cmd =
|
|
|
|
\ '(if-let [m (meta (resolve ' . s:qsym(a:symbol) .'))]'
|
|
|
|
\ . ' {:name (:name m)'
|
|
|
|
\ . ' :ns (:ns m)'
|
|
|
|
\ . ' :resource (:file m)'
|
|
|
|
\ . ' :line (:line m)'
|
|
|
|
\ . ' :doc (:doc m)'
|
|
|
|
\ . ' :arglists-str (str (:arglists m))}'
|
|
|
|
\ . ' {})'
|
|
|
|
return fireplace#evalparse(cmd)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! fireplace#source(symbol) abort
|
|
|
|
let info = fireplace#info(a:symbol)
|
|
|
|
|
|
|
|
let file = ''
|
|
|
|
if !empty(get(info, 'resource'))
|
|
|
|
let file = fireplace#findresource(info.resource)
|
|
|
|
elseif get(info, 'file') =~# '^/\|^\w:\\' && filereadable(info.file)
|
|
|
|
let file = info.file
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !empty(file) && !empty(get(info, 'line'))
|
|
|
|
return '+' . info.line . ' ' . fnameescape(file)
|
|
|
|
endif
|
2014-01-15 23:14:32 +00:00
|
|
|
return ''
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:Edit(cmd, keyword) abort
|
2013-04-11 02:33:48 +00:00
|
|
|
try
|
2014-01-15 23:14:32 +00:00
|
|
|
if a:keyword =~# '^\k\+[/.]$'
|
2013-04-11 02:33:48 +00:00
|
|
|
let location = fireplace#findfile(a:keyword[0: -2])
|
|
|
|
elseif a:keyword =~# '^\k\+\.[^/.]\+$'
|
|
|
|
let location = fireplace#findfile(a:keyword)
|
|
|
|
else
|
|
|
|
let location = fireplace#source(a:keyword)
|
|
|
|
endif
|
|
|
|
catch /^Clojure:/
|
|
|
|
return ''
|
|
|
|
endtry
|
2012-12-04 06:06:22 +00:00
|
|
|
if location !=# ''
|
2014-01-15 23:14:32 +00:00
|
|
|
if matchstr(location, '^+\d\+ \zs.*') ==# fnameescape(expand('%:p')) && a:cmd ==# 'edit'
|
2014-04-03 22:23:24 +00:00
|
|
|
normal! m'
|
2012-12-04 06:06:22 +00:00
|
|
|
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-03-12 19:37:37 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceDjump :<C-U>exe <SID>Edit('edit', expand('<cword>'))<CR>
|
|
|
|
nnoremap <silent> <Plug>FireplaceDsplit :<C-U>exe <SID>Edit('split', expand('<cword>'))<CR>
|
|
|
|
nnoremap <silent> <Plug>FireplaceDtabjump :<C-U>exe <SID>Edit('tabedit', expand('<cword>'))<CR>
|
2013-01-03 05:10:11 +00:00
|
|
|
|
2014-07-02 22:38:49 +00:00
|
|
|
function! s:set_up_source() abort
|
|
|
|
setlocal define=^\\s*(def\\w*
|
|
|
|
command! -bar -buffer -nargs=1 -complete=customlist,fireplace#eval_complete Djump :exe s:Edit('edit', <q-args>)
|
|
|
|
command! -bar -buffer -nargs=1 -complete=customlist,fireplace#eval_complete Dsplit :exe s:Edit('split', <q-args>)
|
2014-07-03 16:28:23 +00:00
|
|
|
|
|
|
|
if get(g:, 'fireplace_no_maps') | return | endif
|
2014-07-02 22:38:49 +00:00
|
|
|
nmap <buffer> [<C-D> <Plug>FireplaceDjump
|
|
|
|
nmap <buffer> ]<C-D> <Plug>FireplaceDjump
|
|
|
|
nmap <buffer> <C-W><C-D> <Plug>FireplaceDsplit
|
|
|
|
nmap <buffer> <C-W>d <Plug>FireplaceDsplit
|
|
|
|
nmap <buffer> <C-W>gd <Plug>FireplaceDtabjump
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_source
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-07-02 22:38:49 +00:00
|
|
|
autocmd FileType clojure call s:set_up_source()
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Go to file
|
2012-12-04 06:06:22 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! fireplace#findfile(path) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
let path = a:path
|
2014-01-15 23:14:32 +00:00
|
|
|
if a:path !~# '/'
|
|
|
|
let path = tr(a:path, '.-', '/_')
|
|
|
|
else
|
2014-07-03 14:02:34 +00:00
|
|
|
let path = substitute(a:path, '^/', '', '')
|
2014-01-12 06:01:08 +00:00
|
|
|
endif
|
2014-01-15 23:14:32 +00:00
|
|
|
let resource = fireplace#findresource(path, fireplace#path(), 0, &suffixesadd)
|
|
|
|
if !empty(resource)
|
|
|
|
return resource
|
|
|
|
elseif fnamemodify(a:path, ':p') ==# a:path && filereadable(a:path)
|
|
|
|
return path
|
|
|
|
elseif a:path[0] !=# '/' && filereadable(expand('%:h') . '/' . path)
|
|
|
|
return expand('%:h') . '/' . path
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
2014-01-15 23:14:32 +00:00
|
|
|
return ''
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GF(cmd, file) abort
|
2014-07-03 16:30:32 +00:00
|
|
|
if a:file =~# '^\w[[:alnum:]_/]*$' &&
|
|
|
|
\ synIDattr(synID(line("."),col("."),1),"name") =~# 'String'
|
|
|
|
let file = substitute(expand('%:p'), '[^\/:]*$', '', '').a:file.'.'.expand('%:e')
|
|
|
|
elseif a:file =~# '^[^/]*/[^/.]*$' && a:file =~# '^\k\+$'
|
2012-12-04 06:06:22 +00:00
|
|
|
let [file, jump] = split(a:file, "/")
|
2014-01-15 23:14:32 +00:00
|
|
|
if file !~# '\.'
|
|
|
|
try
|
|
|
|
let file = fireplace#evalparse('((ns-aliases *ns*) '.s:qsym(file).' '.s:qsym(file).')')
|
|
|
|
catch /^Clojure:/
|
|
|
|
endtry
|
|
|
|
endif
|
2014-07-03 16:30:32 +00:00
|
|
|
let file = fireplace#findfile(file)
|
2012-12-04 06:06:22 +00:00
|
|
|
else
|
2014-07-03 16:30:32 +00:00
|
|
|
let file = fireplace#findfile(a:file)
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
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
|
|
|
|
|
2014-07-02 22:44:20 +00:00
|
|
|
nnoremap <silent> <Plug>FireplaceEditFile :<C-U>exe <SID>GF('edit', expand('<cfile>'))<CR>
|
|
|
|
nnoremap <silent> <Plug>FireplaceSplitFile :<C-U>exe <SID>GF('split', expand('<cfile>'))<CR>
|
|
|
|
nnoremap <silent> <Plug>FireplaceTabeditFile :<C-U>exe <SID>GF('tabedit', expand('<cfile>'))<CR>
|
|
|
|
|
2014-07-02 22:38:49 +00:00
|
|
|
function! s:set_up_go_to_file() abort
|
2014-07-03 13:52:12 +00:00
|
|
|
if expand('%:e') ==# 'cljs'
|
|
|
|
setlocal suffixesadd=.cljs,.cljx,.clj,.java
|
|
|
|
else
|
|
|
|
setlocal suffixesadd=.clj,.cljx,.cljs,.java
|
|
|
|
endif
|
2014-07-03 16:28:23 +00:00
|
|
|
|
|
|
|
if get(g:, 'fireplace_no_maps') | return | endif
|
2014-07-02 22:44:20 +00:00
|
|
|
nmap <buffer> gf <Plug>FireplaceEditFile
|
|
|
|
nmap <buffer> <C-W>f <Plug>FireplaceSplitFile
|
|
|
|
nmap <buffer> <C-W><C-F> <Plug>FireplaceSplitFile
|
|
|
|
nmap <buffer> <C-W>gf <Plug>FireplaceTabeditFile
|
2014-07-02 22:38:49 +00:00
|
|
|
endfunction
|
2014-07-02 22:33:23 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_go_to_file
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-07-02 22:38:49 +00:00
|
|
|
autocmd FileType clojure call s:set_up_go_to_file()
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Documentation
|
2012-12-04 06:06:22 +00:00
|
|
|
|
|
|
|
function! s:buffer_path(...) abort
|
2014-01-11 04:31:47 +00:00
|
|
|
let buffer = a:0 ? a:1 : s:buf()
|
2012-12-04 06:06:22 +00:00
|
|
|
if getbufvar(buffer, '&buftype') =~# '^no'
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
let path = substitute(fnamemodify(bufname(buffer), ':p'), '\C^zipfile:\(.*\)::', '\1/', '')
|
2014-01-17 10:25:03 +00:00
|
|
|
for dir in fireplace#path(buffer)
|
|
|
|
if dir !=# '' && path[0 : strlen(dir)-1] ==# dir && path[strlen(dir)] =~# '[\/]'
|
|
|
|
return path[strlen(dir)+1:-1]
|
|
|
|
endif
|
|
|
|
endfor
|
2012-12-04 06:06:22 +00:00
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2014-01-11 04:31:47 +00:00
|
|
|
function! fireplace#ns(...) abort
|
|
|
|
let buffer = a:0 ? a:1 : s:buf()
|
2014-02-05 15:19:01 +00:00
|
|
|
if !empty(getbufvar(buffer, 'fireplace_ns'))
|
2014-01-11 04:31:47 +00:00
|
|
|
return getbufvar(buffer, 'fireplace_ns')
|
2014-01-01 22:15:35 +00:00
|
|
|
endif
|
2014-01-11 04:31:47 +00:00
|
|
|
let head = getbufline(buffer, 1, 500)
|
|
|
|
let blank = '^\s*\%(;.*\)\=$'
|
|
|
|
call filter(head, 'v:val !~# blank')
|
2013-04-09 11:07:21 +00:00
|
|
|
let keyword_group = '[A-Za-z0-9_?*!+/=<>.-]'
|
2014-01-11 04:31:47 +00:00
|
|
|
let lines = join(head[0:49], ' ')
|
2013-01-09 05:07:24 +00:00
|
|
|
let lines = substitute(lines, '"\%(\\.\|[^"]\)*"\|\\.', '', 'g')
|
2013-01-03 23:45:30 +00:00
|
|
|
let lines = substitute(lines, '\^\={[^{}]*}', '', '')
|
2013-04-09 11:07:21 +00:00
|
|
|
let lines = substitute(lines, '\^:'.keyword_group.'\+', '', 'g')
|
|
|
|
let ns = matchstr(lines, '\C^(\s*\%(in-ns\s*''\|ns\s\+\)\zs'.keyword_group.'\+\ze')
|
2012-12-15 21:26:31 +00:00
|
|
|
if ns !=# ''
|
|
|
|
return ns
|
|
|
|
endif
|
2014-01-11 04:31:47 +00:00
|
|
|
let path = s:buffer_path(buffer)
|
|
|
|
return s:to_ns(path ==# '' ? fireplace#client(buffer).user_ns() : 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
|
|
|
try
|
2014-05-02 02:26:53 +00:00
|
|
|
let response = s:eval('('.a:ns.'/'.a:macro.' '.a:arg.')', {'session': 0})
|
2014-04-09 17:23:19 +00:00
|
|
|
call s:output_response(response)
|
2012-12-04 06:06:22 +00:00
|
|
|
catch /^Clojure:/
|
|
|
|
catch /.*/
|
|
|
|
echohl ErrorMSG
|
|
|
|
echo v:exception
|
|
|
|
echohl None
|
|
|
|
endtry
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:inputlist(label, entries) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
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
|
|
|
|
|
2014-05-02 02:26:53 +00:00
|
|
|
function! s:Doc(symbol) abort
|
|
|
|
let info = fireplace#info(a:symbol)
|
|
|
|
if has_key(info, 'ns') && has_key(info, 'name')
|
|
|
|
echo info.ns . '/' . info.name
|
|
|
|
endif
|
|
|
|
if get(info, 'arglists-str', 'nil') !=# 'nil'
|
|
|
|
echo info['arglists-str']
|
|
|
|
endif
|
|
|
|
if !empty(get(info, 'doc', ''))
|
|
|
|
echo ' ' . info.doc
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 03:58:43 +00:00
|
|
|
function! s:K() abort
|
2012-12-15 00:21:42 +00:00
|
|
|
let word = expand('<cword>')
|
2012-12-29 16:26:24 +00:00
|
|
|
let java_candidate = matchstr(word, '^\%(\w\+\.\)*\u\l\w*\ze\%(\.\|\/\w\+\)\=$')
|
2012-12-15 00:21:42 +00:00
|
|
|
if java_candidate !=# ''
|
|
|
|
return 'Javadoc '.java_candidate
|
|
|
|
else
|
|
|
|
return 'Doc '.word
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
nnoremap <Plug>FireplaceK :<C-R>=<SID>K()<CR><CR>
|
|
|
|
nnoremap <Plug>FireplaceSource :Source <C-R><C-W><CR>
|
2013-01-03 05:10:11 +00:00
|
|
|
|
2014-07-02 22:38:49 +00:00
|
|
|
function! s:set_up_doc() abort
|
|
|
|
command! -buffer -nargs=1 FindDoc :exe s:Lookup('clojure.repl', 'find-doc', printf('#"%s"', <q-args>))
|
|
|
|
command! -buffer -bar -nargs=1 Javadoc :exe s:Lookup('clojure.java.javadoc', 'javadoc', <q-args>)
|
|
|
|
command! -buffer -bar -nargs=1 -complete=customlist,fireplace#eval_complete Doc :exe s:Doc(<q-args>)
|
|
|
|
command! -buffer -bar -nargs=1 -complete=customlist,fireplace#eval_complete Source :exe s:Lookup('clojure.repl', 'source', <q-args>)
|
|
|
|
setlocal keywordprg=:Doc
|
2014-07-03 16:28:23 +00:00
|
|
|
|
|
|
|
if get(g:, 'fireplace_no_maps') | return | endif
|
2014-07-02 22:38:49 +00:00
|
|
|
if empty(mapcheck('K', 'n'))
|
|
|
|
nmap <buffer> K <Plug>FireplaceK
|
|
|
|
endif
|
|
|
|
nmap <buffer> [d <Plug>FireplaceSource
|
|
|
|
nmap <buffer> ]d <Plug>FireplaceSource
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
augroup fireplace_doc
|
2012-12-04 06:06:22 +00:00
|
|
|
autocmd!
|
2014-07-02 22:38:49 +00:00
|
|
|
autocmd FileType clojure call s:set_up_doc()
|
2012-12-04 06:06:22 +00:00
|
|
|
augroup END
|
|
|
|
|
2014-06-27 13:46:21 +00:00
|
|
|
" Section: Tests
|
2014-04-04 03:54:20 +00:00
|
|
|
|
2014-05-05 05:27:10 +00:00
|
|
|
function! fireplace#capture_test_run(expr, ...) abort
|
2014-07-13 03:17:30 +00:00
|
|
|
let expr = '(try'
|
|
|
|
\ . ' (require ''clojure.test)'
|
|
|
|
\ . ' (binding [clojure.test/report (fn [m]'
|
2014-04-04 03:54:20 +00:00
|
|
|
\ . ' (case (:type m)'
|
|
|
|
\ . ' (:fail :error)'
|
2014-04-13 00:28:21 +00:00
|
|
|
\ . ' (let [{file :file line :line test :name} (meta (last clojure.test/*testing-vars*))]'
|
2014-04-04 03:54:20 +00:00
|
|
|
\ . ' (clojure.test/with-test-out'
|
2014-04-13 00:28:21 +00:00
|
|
|
\ . ' (println (clojure.string/join "\t" [file line (name (:type m)) test]))'
|
2014-04-04 03:54:20 +00:00
|
|
|
\ . ' (when (seq clojure.test/*testing-contexts*) (println (clojure.test/testing-contexts-str)))'
|
|
|
|
\ . ' (when-let [message (:message m)] (println message))'
|
|
|
|
\ . ' (println "expected:" (pr-str (:expected m)))'
|
|
|
|
\ . ' (println " actual:" (pr-str (:actual m)))))'
|
|
|
|
\ . ' ((.getRawRoot #''clojure.test/report) m)))]'
|
2014-05-05 05:27:10 +00:00
|
|
|
\ . ' ' . (a:0 ? a:1 : '') . a:expr . ')'
|
2014-05-05 05:23:30 +00:00
|
|
|
\ . ' (catch Exception e'
|
|
|
|
\ . ' (println (str e))'
|
|
|
|
\ . ' (println (clojure.string/join "\n" (.getStackTrace e)))))'
|
2014-04-04 03:54:20 +00:00
|
|
|
let qflist = []
|
2014-04-14 05:55:16 +00:00
|
|
|
let response = s:eval(expr, {'session': 0})
|
2014-04-04 03:54:20 +00:00
|
|
|
if !has_key(response, 'out')
|
2014-04-29 03:01:17 +00:00
|
|
|
call setqflist(fireplace#quickfix_for(get(response, 'stacktrace', [])))
|
2014-04-04 03:54:20 +00:00
|
|
|
return s:output_response(response)
|
|
|
|
endif
|
|
|
|
for line in split(response.out, "\n")
|
|
|
|
if line =~# '\t.*\t.*\t'
|
2014-05-05 05:23:30 +00:00
|
|
|
let entry = {'text': line}
|
2014-04-04 03:54:20 +00:00
|
|
|
let [resource, lnum, type, name] = split(line, "\t", 1)
|
|
|
|
let entry.lnum = lnum
|
|
|
|
let entry.type = (type ==# 'fail' ? 'W' : 'E')
|
|
|
|
let entry.text = name
|
|
|
|
if resource ==# 'NO_SOURCE_FILE'
|
|
|
|
let resource = ''
|
|
|
|
let entry.lnum = 0
|
|
|
|
endif
|
|
|
|
let entry.filename = fireplace#findresource(resource, fireplace#path())
|
|
|
|
if empty(entry.filename)
|
|
|
|
let entry.lnum = 0
|
|
|
|
endif
|
2014-05-05 05:23:30 +00:00
|
|
|
else
|
|
|
|
let entry = s:qfmassage(line, fireplace#path())
|
2014-04-04 03:54:20 +00:00
|
|
|
endif
|
|
|
|
call add(qflist, entry)
|
|
|
|
endfor
|
|
|
|
call setqflist(qflist)
|
2014-05-05 05:27:10 +00:00
|
|
|
let was_qf = &buftype ==# 'quickfix'
|
|
|
|
botright cwindow
|
|
|
|
if &buftype ==# 'quickfix' && !was_qf
|
|
|
|
wincmd p
|
|
|
|
endif
|
|
|
|
for winnr in range(1, winnr('$'))
|
|
|
|
if getwinvar(winnr, '&buftype') ==# 'quickfix'
|
|
|
|
call setwinvar(winnr, 'quickfix_title', a:expr)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
endfor
|
2014-04-04 03:54:20 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-05-05 02:42:58 +00:00
|
|
|
function! s:RunTests(bang, ...) abort
|
2014-05-04 22:09:56 +00:00
|
|
|
if &autowrite || &autowriteall
|
|
|
|
silent! wall
|
|
|
|
endif
|
2014-07-02 23:18:05 +00:00
|
|
|
let reqs = map(copy(a:000), '"''".v:val')
|
|
|
|
let pre = '(clojure.core/require '.join(empty(a:000) ? ["'".fireplace#ns()] : reqs, ' ').' :reload) '
|
|
|
|
let expr = join(['(clojure.test/run-tests'] + reqs, ' ').')'
|
|
|
|
call fireplace#capture_test_run(expr, pre)
|
|
|
|
echo expr
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:RunAllTests(bang, ...) abort
|
|
|
|
if a:0
|
2014-04-04 03:54:20 +00:00
|
|
|
let expr = '(clojure.test/run-all-tests #"'.join(a:000, '|').'")'
|
|
|
|
else
|
2014-07-02 23:18:05 +00:00
|
|
|
let expr = '(clojure.test/run-all-tests)'
|
2014-04-04 03:54:20 +00:00
|
|
|
endif
|
2014-07-02 23:18:05 +00:00
|
|
|
call fireplace#capture_test_run(expr)
|
2014-05-05 02:42:58 +00:00
|
|
|
echo expr
|
2014-04-04 03:54:20 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-07-02 23:18:05 +00:00
|
|
|
function! s:set_up_tests() abort
|
|
|
|
command! -buffer -bar -bang -nargs=*
|
2014-04-04 04:47:46 +00:00
|
|
|
\ -complete=customlist,fireplace#ns_complete RunTests
|
2014-05-05 02:42:58 +00:00
|
|
|
\ call s:RunTests(<bang>0, <f-args>)
|
2014-07-02 23:18:05 +00:00
|
|
|
command! -buffer -bang -nargs=* RunAllTests
|
|
|
|
\ call s:RunAllTests(<bang>0, <f-args>)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fireplace_tests
|
|
|
|
autocmd!
|
|
|
|
autocmd FileType clojure call s:set_up_tests()
|
2014-04-04 03:54:20 +00:00
|
|
|
augroup END
|