Provide :Last

Closes #7.
This commit is contained in:
Tim Pope 2013-01-13 10:49:54 -05:00
parent d2bc102487
commit 105b01d7d0
2 changed files with 109 additions and 5 deletions

View File

@ -161,6 +161,12 @@ cqc Bring up a |command-line-window| for code to
cq{motion} Bring up a |command-line-window| with text indicated
by {motion} prepopulated.
*foreplay-:Last*
:Last Open the result of the last evaluation in the preview
window. Use :2Last to get the next-to-last result,
and so on. Once the window is open, cycle to older
and newer entries with |:lprevious| and |:lnext|.
And insert mode:
*foreplay-i_CTRL-R_(*

View File

@ -316,7 +316,13 @@ endfunction
function! s:client() abort
silent doautocmd User ForeplayPreConnect
let buf = exists('s:input') ? s:input : '%'
if exists('s:input')
let buf = s:input
elseif has_key(s:qffiles, expand('%:p'))
let buf = s:qffiles[expand('%:p')].buffer
else
let buf = '%'
endif
let root = simplify(fnamemodify(bufname(buf), ':p:s?[\/]$??'))
let previous = ""
while root !=# previous
@ -337,7 +343,13 @@ function! foreplay#local_client(...)
if !a:0
silent doautocmd User ForeplayPreConnect
endif
let buf = exists('s:input') ? s:input : '%'
if exists('s:input')
let buf = s:input
elseif has_key(s:qffiles, expand('%:p'))
let buf = s:qffiles[expand('%:p')].buffer
else
let buf = '%'
endif
for repl in s:repls
if repl.includes_file(fnamemodify(bufname(buf), ':p'))
return repl
@ -424,13 +436,66 @@ function! s:eval(expr, ...) abort
return client.eval(a:expr, options)
endfunction
function! s:temp_response(response) abort
let output = []
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
return {'filename': a:entry.tempfile, 'text': a:entry.code}
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
function! foreplay#eval(expr) abort
let response = s:eval(a:expr, {'session': 1})
if !empty(get(response, 'value', ''))
call insert(s:history, {'buffer': bufnr(''), 'code': a:expr, 'ns': foreplay#ns(), 'response': response})
endif
if len(s:history) > &history
call remove(s:history, &history, -1)
endif
if !empty(get(response, 'stacktrace', []))
call setloclist(0, foreplay#quickfix_for(response.stacktrace))
lopen
wincmd p
let nr = 0
if has_key(s:qffiles, expand('%:p'))
let nr = winbufnr(s:qffiles[expand('%:p')].buffer)
endif
if nr != -1
call setloclist(nr, foreplay#quickfix_for(response.stacktrace))
lopen
wincmd p
endif
endif
call s:output_response(response)
@ -668,8 +733,30 @@ nnoremap <Plug>ForeplayPrompt :exe <SID>inputeval()<CR>
noremap! <Plug>ForeplayRecall <C-R>=<SID>recall()<CR>
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
function! s:setup_eval() abort
command! -buffer -bang -range=0 -nargs=? -complete=customlist,foreplay#eval_complete Eval :exe s:Eval(<bang>0, <line1>, <line2>, <count>, <q-args>)
command! -buffer -bang -bar -count=1 Last exe s:Last(<bang>0, <count>)
nmap <buffer> cp <Plug>ForeplayPrint
nmap <buffer> cpp <Plug>ForeplayPrintab
@ -686,6 +773,11 @@ function! s:setup_eval() abort
map! <buffer> <C-R>( <Plug>ForeplayRecall
endfunction
function! s:setup_historical()
set readonly nomodifiable
nnoremap <buffer><silent>q :bdelete<CR>
endfunction
function! s:cmdwinenter()
setlocal filetype=clojure
endfunction
@ -697,6 +789,9 @@ endfunction
augroup foreplay_eval
autocmd!
autocmd FileType clojure call s:setup_eval()
autocmd BufReadPost * if has_key(s:qffiles, expand('<afile>:p')) |
\ call s:setup_historical() |
\ endif
autocmd CmdWinEnter @ if exists('s:input') | call s:cmdwinenter() | endif
autocmd CmdWinLeave @ if exists('s:input') | call s:cmdwinleave() | endif
augroup END
@ -891,6 +986,9 @@ function! foreplay#ns() abort
if ns !=# ''
return ns
endif
if has_key(s:qffiles, expand('%:p'))
return s:qffiles[expand('%:p')].ns
endif
let path = s:buffer_path()
return s:tons(path ==# '' ? 'user' : path)
endfunction