vim/bundle/ooze/plugin/ooze.vim @ 9062792cfda3

More
author Steve Losh <steve@stevelosh.com>
date Tue, 01 Mar 2016 12:31:04 +0000
parents 8a1376b77b80
children b69dc77f379c
" vim: set foldmethod=indent
" TODO: 
" Unfuck folding
" Disassembly mapping
" in-package horseshit
" error handling
" multi-packet messages
"

function! s:IsString(a)
    return type(a:a) == 1
endfunction

function! s:GetString(msg, key, extra)
    let val = get(a:msg, a:key)
    if s:IsString(val)
        return val . a:extra
    else
        return ''
    endif
endfunction

let g:ooze_scratch_buffer_name = '__OozeScratch__'
function! s:OpenOozeScratch(contents)
    echom "test"
    echom bufname('%')
    if bufname('%') != g:ooze_scratch_buffer_name
        wincmd s
        execute "edit " . g:ooze_scratch_buffer_name
    endif

    set filetype=lisp
    setlocal foldlevel=99
    setlocal buftype=nofile
    setlocal bufhidden=hide
    setlocal noswapfile
    setlocal buflisted

    setlocal noreadonly
    normal! ggdG
    call append(0, a:contents)
    setlocal readonly
endfunction

function! s:HandleMessage(msg)
    let moutput = s:GetString(a:msg, 'macroexpand-1', "")
    if moutput != ''
        call s:OpenOozeScratch(split(moutput, "\n"))
        return
    endif

    let output = ''
    let output .= s:GetString(a:msg, 'stdout', "")
    let output .= s:GetString(a:msg, 'stdout', "")
    let output .= s:GetString(a:msg, 'stderr', "")
    let output .= s:GetString(a:msg, 'value', "")

    let output .= s:GetString(a:msg, 'error', "\n\n")
    let output .= s:GetString(a:msg, 'original', "\n\n")
    let output .= s:GetString(a:msg, 'backtrace', "\n")

    let output .= s:GetString(a:msg, 'function-arglist', "\n\n")
    let output .= s:GetString(a:msg, 'function-docstring', "\n")
    if output != ''
        echo output
    endif
endfunction

function! s:HandleData(data)
    for msg in bencode#BdecodeAll(a:data)
        call s:HandleMessage(msg)
    endfor
endfunction

silent! function s:JobHandler(job_id, data, event)
    if a:event == 'stdout'
        call s:HandleData(join(a:data, "\n"))
    elseif a:event == 'stderr'
        1
    else
        1
    endif
endfunction

let s:callbacks = {
\ 'on_stdout': function('s:JobHandler'),
\ 'on_stderr': function('s:JobHandler'),
\ 'on_exit': function('s:JobHandler')
\ }

if !exists("g:ooze_connection")
    let g:ooze_connection = 0
endif

function! OozeDisconnect()
    if g:ooze_connection
        call jobstop(g:ooze_connection)
        let g:ooze_connection = 0
    endif
endfunction

function! OozeConnect()
    if g:ooze_connection
        call OozeDisconnect()
    endif
    let g:ooze_connection = jobstart(['nc', 'localhost', '8675'], s:callbacks)
endfunction

function! OozeMacroexpand(form)
    if !g:ooze_connection
        throw "Not connected!"
    endif
    let msg = {"op": "macroexpand", "form": a:form}
    call jobsend(g:ooze_connection, bencode#Bencode(msg))
endfunction

function! OozeMacroexpandSelection()
    let z = @z
    normal! gv"zy
    call OozeMacroexpand(@z)
    let @z = z
endfunction

function! OozeDocument(symbol)
    if !g:ooze_connection
        throw "Not connected!"
    endif
    let msg = {"op": "documentation", "symbol": a:symbol}
    call jobsend(g:ooze_connection, bencode#Bencode(msg))
endfunction

function! OozeDocumentSelection()
    let z = @z
    normal! gv"zy
    call OozeDocument(@z)
    let @z = z
endfunction

function! OozeEval(code)
    if !g:ooze_connection
        throw "Not connected!"
    endif
    let msg = {"op": "eval", "code": a:code}
    call jobsend(g:ooze_connection, bencode#Bencode(msg))
endfunction

function! OozeEvalSelection()
    let z = @z
    normal! gv"zy
    call OozeEval(@z)
    let @z = z
endfunction

function! OozeLoad(path)
    if !g:ooze_connection
        throw "Not connected!"
    endif
    let msg = {"op": "load-file", "path": a:path}
    call jobsend(g:ooze_connection, bencode#Bencode(msg))
endfunction

function! OozeLoadCurrent()
    call OozeLoad(expand('%:p'))
endfunction

function! OozeMapKeys()
    nnoremap <buffer> <localleader>C :call OozeConnect()<cr>
    nnoremap <buffer> <localleader>K :call OozeDisconnect()<cr>

    nnoremap <buffer> <localleader>E :call OozeEval(input("? "))<cr>
    vnoremap <buffer> <localleader>e :<c-u>call OozeEvalSelection()<cr>
    nnoremap <buffer> <localleader>e mz:silent normal! l<cr>:call PareditFindDefunBck()<cr>vab:<c-u>call OozeEvalSelection()<cr>`z
    nnoremap <buffer> <localleader>f mzvab:<c-u>call OozeEvalSelection()<cr>`z

    nnoremap <buffer> <localleader>D :call OozeDocument(input("? "))<cr>
    " nnoremap <buffer> <localleader>d mzviw:<c-u>call OozeDocumentSelection()<cr>`z
    inoremap <buffer> <silent> <c-d> <esc>mzbviw:<c-u>call OozeDocumentSelection()<cr>`za
    nnoremap <buffer> M mzviw:<c-u>call OozeDocumentSelection()<cr>`z

    nnoremap <buffer> <localleader>M :call OozeMacroexpand(input("? "))<cr>
    nnoremap <buffer> <localleader>m mzvab:<c-u>call OozeMacroexpandSelection()<cr>`z

    nnoremap <buffer> <localleader>r :call OozeLoadCurrent()<cr>
endfunction

augroup ooze_dev
    au!
    autocmd BufWritePost ooze.vim source %
augroup END