[slim-vim] Error when sourcing ecl-repl.vim

Daniel Trstenjak Daniel.Trstenjak at online.de
Sat Aug 5 13:16:05 CDT 2006


Hi Brad,

On Sat, Aug 05, 2006 at 10:25:11AM -0700, Brad Beveridge wrote:
> I expect so, though I am far from an expert with Vim's scripting
> language.  If you already know how to test for the presence of these
> environment vars, I'd love to get a patch :)

Checking for environment variables in ecl-slim.vim
and ecl-repl.vim added.


Regards,
Daniel
-------------- next part --------------
" ECL repl, by Larry Clapp, larry at theclapp.org
" vim:isk+=-:isk+=*
" License: Lisp LGPL; see http://opensource.franz.com/preamble.html
" I adapted some of this code from my VILisp package, available at
" http://vim.sourceforge.net/script.php?script_id=221.
" Version: 2.0.0
"
" Purpose: Provide a way to interact with the ECL embedded in Vim.
" Overview: Creates a "repl output buffer"; most mappings copy the current
" expression to this buffer, evaluate it, print the output (if any), and the
" result.  Some mappings evaluate the current expression and print the results
" at the bottom of the screen.  (Those may go away; not sure of their
" usefulness.)
" Installation: Source this file.
" Mappings:
" (NOTE: All mappings are global.)
"
" <leader>es - Evaluate S-expression -- prints results at bottom of screen
" <leader>ee - Eval Expression -- Prints results to the repl output buffer.
"
" The es and ee commands make very little sense, mnemonically.  VILisp uses
" es, and I chose ee for the new command because it's close to es and easy to
" type.  Please feel free to remap them and/or suggest changes.
"
" <leader>cs: eval innermost form cursor is in and print result at bottom of
"             screen
" <leader>ce: eval innermost form cursor is in and send to repl output buffer
"
" visual: <leader>ee: eval marked block; send to repl output buffer.
" <leader>ev: eval variable under cursor
"
" <leader>lf - Load current file
" <leader>m1 - (emm-one) Macroexpand-1 the current expression
" <leader>me - macroexpand the current expression
"
" <leader>ri: reindent toplevel expression
" <leader>lw: add current word to 'lispwords' and reindent expression.
"
" Temp/Scratch files: as touched on above, we create two scratch files, one is
" the "repl output buffer", and one is a scratch Lisp buffer for ephemeral
" experiments.  The repl output buffer is used to show the forms evaluated,
" their output, and their values; it's read-only and non-modifiable.  The
" scratch buffer is for one-off experimentation.
"
" These two mappings navigate these two buffers.
"
" <leader>rb: view repl output buffer; splits to the repl output buffer;
"             leaves cursor in current window.
" <leader>lb: split to lisp buffer; leave cursor in Lisp buffer.

let s:standard_output = ''
let s:restart = ''
let s:error = ''
let s:lisp_output = '--lisp-repl-output--'
let s:lisp_input = '--lisp-scratch--'

function! s:Get_newest_file( basename, ... )
  let n = 1
  while n <= a:0
    let newest = a:basename . "." . a:{n}
    let n = n + 1
    if filereadable( newest )
      break
    endif
  endwhile

  let ftime_newest = getftime( newest )
  while n <= a:0
    let newer = a:basename . "." . a:{n}
    let n = n + 1
    if filereadable( newer ) && ftime_newest < getftime( newer )
      let newest = newer
      let ftime_newest = getftime( newer )
    endif
  endwhile

  return newest
endfunction

function! s:Get_file_to_load( basename )
  return s:Get_newest_file( a:basename, "lisp", "fas" )
endfunction

if exists("$ECL_REPL_PATH")
   silent exec "ecl "
     \ . "(load \""
     \ . s:Get_newest_file( $ECL_REPL_PATH . "/ecl-repl", "lisp", "fas" )
     \ . "\")"
else
   echoerr "Environment variable ECL_REPL_PATH isn't set !"
endif

function! s:Goto_pos4( bufnum, winline, line, column )
  exec "silent hide bu" a:bufnum
  exec "normal! " . a:line . "G" . a:column . "|"
  if a:winline > 0
    if a:winline < winline()
      let scroll = winline() - a:winline
      exec "normal" scroll "\<c-e>"
    elseif a:winline > winline()
      let scroll = a:winline - winline()
      exec "normal" scroll "\<c-y>"
    endif
  endif
endfunction

function! s:Goto_pos1( pos )
  exec a:pos
endfunction

function! s:Get_pos( ... )
  let where = "."
  if a:0 > 0
    let where = a:1
  endif

  " if 'where' is e.g. "'a" and "mark a" isn't set, return '' immediately
  if line( where ) == 0
    return ''
  endif

  " what buffer are we in?
  let bufnum = bufnr( "%" )

  " get current position
  let winline = winline()
  if where != "."
    let winline = -1
  endif
  let column = virtcol( where )
  let line = line( where )
  let pos = bufnum . "," . winline . "," . line . "," . column
  let pos = "call s:Goto_pos4( " . pos . " )"

  return( pos )
endfunction

function! s:Scroll_repl()
  ecl (ecl-repl:scroll-repl)
endfunction

function! s:In_comment_or_string()
  let line = getline( "." )
  if line =~ '[";]'
    let cur_syn_name = synIDattr( synID( line("."), col("."), 0 ), "name" )
    if cur_syn_name == "lispComment"
      \ || cur_syn_name == "lispString"
      return 1
    endif
  endif
  return 0
endfunction

function! s:Find_paren( ... )
  let flags = ''
  if a:0 > 0
    let flags = a:1
  endif

  call searchpair( "(", '', ")", flags . "W", "s:In_comment_or_string()" )
endfunction

function! s:Remove_trailing_newline( string )
  let result = a:string
  if result =~ '\n$'
    let result = substitute( result, '^\(.*\)\n$', '\1', '' )
  endif
  return result
endfunction

function! s:Delete_leading_whitespace( expr )
  return substitute( a:expr, "\n\\s\\+", "\n", "g" )
endfunction

function! s:Make_string( s )
  return '"' . escape( a:s, '"' ) . '"'
endfunction

function! s:ECL_funcall( func, ... )
  let result = ""
  let f = "ecl (vim:setq \"result\" (" . a:func . " "
  let n = 1
  while n <= a:0
    let f = f . a:{n} . " "
    let n = n + 1
  endwhile
  let f = f . "))"
  exec f
  echo result
  return result
endfunction

function! s:Get_cur_expr( repeat, ... )
  return s:ECL_funcall( "ecl-repl:get-cur-expr", s:Num_to_t_or_nil( a:repeat ) )
endfunction

function! s:Move_to_toplevel()
  call s:Find_paren( "rb" )
endfunction

function! s:Reindent_current()
  let pos = s:Get_pos()
  silent exec "normal! =:call s:Find_paren()\<cr>"
  call s:Goto_pos1( pos )
endfunction

function! <sid>Reindent_toplevel()
  let pos = s:Get_pos()
  call s:Move_to_toplevel()
  call s:Reindent_current()
  call s:Goto_pos1( pos )
endfunction

function! <sid>Add_lispword_and_reindent_toplevel()
  exec "set lispwords+=" . expand( "<cword>" )
  call <sid>Reindent_toplevel()
endfunction

function! s:Num_to_t_or_nil( n )
  if a:n
    return "t"
  else
    return "nil"
  endif
endfunction

function! s:Append_to_buffer( text, bufnr, reindent, fresh_line )
  call s:ECL_funcall( "ecl-repl:append-and-reindent",
		      \ s:Make_string( a:text ),
		      \ a:bufnr - 1,
		      \ s:Num_to_t_or_nil( a:reindent ),
		      \ s:Num_to_t_or_nil( a:fresh_line ) )
endfunction

function! <sid>Eval_and_send_to_repl( echo_expr, repeat, ... )
  ecl (ecl-repl:eval-and-send-to-repl)
endfunction

function! <sid>Eval_block() range
  let old_z = @z
  exec a:firstline . "," . a:lastline . " yank z"
  let expr = s:Remove_trailing_newline( @z )
  let @z = old_z
  call <sid>Eval_and_send_to_repl( 1, 0, expr )
endfunction

function! <sid>Macroexpand_1()
  call <sid>Eval_and_send_to_repl( 1, 0, "(macroexpand-1 '" . s:Get_cur_expr( 0 ) . ")" )
endfunction

function! <sid>Macroexpand()
  call <sid>Eval_and_send_to_repl( 1, 0, "(macroexpand '" . s:Get_cur_expr( 0 ) . ")" )
endfunction

function! s:New_tmp_lisp_buffer( name, modifiable )
  let cur_buf = bufnr( "%" )
  silent exec "hide edit" a:name

  let bufnr = bufnr( "%" )
  silent setlocal bufhidden=hide buftype=nofile filetype=lisp noswapfile
  let &l:modifiable = a:modifiable

  exec "silent hide buffer" cur_buf

  return bufnr
endfunction

function! <sid>View_repl_buffer()
  exec "sb" s:repl_buffer_nr
  wincmd p
endfunction

function! <sid>Split_to_lisp_buffer()
  exec "sb" s:lisp_buffer_nr
endfunction

function! <sid>Load_file( ... )
  if a:0 == 0
    let basename = expand( "%:r" )
  else
    let basename = a:1
  endif
  let load_file = s:Get_file_to_load( basename )
  call <sid>Eval_and_send_to_repl( 1, 0, '(load "' . load_file .  '")' )
endfunction

function! <sid>Compile_file()
  call <sid>Eval_and_send_to_repl( 1, 0, '(compile-file "' . expand( "%" ) . '")' )
endfunction

function! <sid>Compile_and_load_file()
  call <sid>Compile_file()
  call <sid>Load_file()
endfunction

let s:repl_buffer_nr = s:New_tmp_lisp_buffer( s:lisp_output, 0 )
let s:lisp_buffer_nr = s:New_tmp_lisp_buffer( s:lisp_input, 1 )
ecl (defparameter ecl-repl::*repl-buffer-nr* (1- (vim:var "s:repl_buffer_nr")))
ecl (defparameter ecl-repl::*lisp-buffer-nr* (1- (vim:var "s:lisp_buffer_nr")))
ecl (setf ecl-repl::*s-lisp-output* (vim:var "s:lisp_output"))

function! ECL_var( var )
  let result = ""
  exec "ecl (vim:setq \"result\" " . a:var . ")"
  return result
endfunction

" Eval Expression
nmap <Leader>ee :call <sid>Eval_and_send_to_repl( 1, 1 )<cr>
vmap <Leader>ee :call <sid>Eval_block()<cr>

" Eval Var
nmap <leader>ev :call <sid>Eval_and_send_to_repl( 1, 0, expand( "<cword>" ) )<cr>

" Current Expression
nmap <leader>ce :call <sid>Eval_and_send_to_repl( 1, 0 )<cr>

" Define Function
nmap <leader>df :call <sid>Eval_and_send_to_repl( 0, 1 )<cr>

nmap <Leader>lf :call <sid>Load_file()<cr>

nmap <Leader>m1 :call <sid>Macroexpand_1()<cr>
nmap <Leader>me :call <sid>Macroexpand()<cr>

nmap <Leader>ri :call <sid>Reindent_toplevel()<cr>
nmap <Leader>lw :call <sid>Add_lispword_and_reindent_toplevel()<cr>

nmap <Leader>rb :call <sid>View_repl_buffer()<cr>
nmap <Leader>lb :call <sid>Split_to_lisp_buffer()<cr>

nmap <Leader>cf :call <sid>Compile_file()<cr>
nmap <Leader>clf :call <sid>Compile_and_load_file()<cr>

" Lisp read/eval/print
command! -nargs=+ Lrep exec "ecl (print " . <q-args> . ")"
nmap <Leader>lp :Lrep (

" Lisp Read/Eval -- send to repl window
command! -nargs=+ Leval call <sid>Eval_and_send_to_repl( 1, 1, <q-args> )

call s:Append_to_buffer(
  \ "ecl-repl.vim (re)loaded at " . strftime( "%c" ) . "\n"
  \   . ECL_var( "ecl-repl::*s-lisp-package*" ) . "> ",
  \ s:repl_buffer_nr,
  \ 0, 1 )
call s:Scroll_repl()

-------------- next part --------------
"silent exec "ecl (load \"" . $VIMRUNTIME . "/if_ecl.lisp\")"
"silent 

function! s:New_tmp_lisp_buffer( name, modifiable )
  if -1 == bufnr( a:name )
    silent exec "new" a:name
  else
    silent exec "sb" bufnr( a:name )
  endif

  let bufnr = bufnr( "%" )
  silent setlocal bufhidden=hide buftype=nofile filetype=lisp noswapfile
  let &l:modifiable = a:modifiable
  close

  return bufnr
endfunction

let g:slime_output_buffer = s:New_tmp_lisp_buffer ("--debug-sv--", 1)
let g:slim_vim_buffer = s:New_tmp_lisp_buffer ("--slim-vim-debug--", 1)
let g:slim_vim_inspect_buffer = s:New_tmp_lisp_buffer ("--slim-vim-inspect--", 1)
let g:slim_vim_repl_buffer = s:New_tmp_lisp_buffer ("--slim-vim-repl--", 1)

"function! ECLSlime_find_paren( recursive, ... )
"  let recurs = ''
"  if a:recursive > 0
"    let recurs = 'r'
"  endif
"
"  let dir = ''
"  if a:0 > 0
"    let dir = a:1
"  endif
"
"  call searchpair( "(", '', ")", dir . recurs . "W", "ECLIn_comment_or_string()" )
"endfunction

"function! ECLIn_comment_or_string()
"  let line = getline( "." )
"  if line =~ '[";]'
"    let cur_syn_name = synIDattr( synID( line("."), col("."), 0 ), "name" )
"    if cur_syn_name == "lispComment"
"      \ || cur_syn_name == "lispString"
"      return 1
"    endif
"  endif
"  return 0
"endfunction

"function! ECLSlime_append_to_buffer( bufnr, text, fresh_line )
"  let text = a:text
"
"  silent exec "sb" a:bufnr
"  "silent normal G$
"  if a:fresh_line && virtcol( "." ) > 1
"    let text = "\n" . text
"  endif
"
"  "let old_modif = &l:modifiable
"  setl modifiable
"
"  let old_paste = &paste
"  set paste
"
"  silent exec "normal GA\<c-r>=text\<cr>"
"  "silent normal G$
"
"  set nopaste
"
"  "let &l:modifiable = old_modif
"  let &paste = old_paste
"
"  silent close
"endfunction


"function! ECLSlime_append_output (text, fresh_line)
"    call ECLSlime_append_to_buffer (g:slime_output_buffer, a:text, a:fresh_line)
"endfunction

function! s:SlimVim_main_mappings ()
    " <Leader>x is eval  (Slime = C-M-x)
    " Mnemonic eXecute
    nmap <buffer> <Leader>x :ecl (vim-slime::slime-eval-defun)<CR>

    " <Leader>X is eval only the next outer form (Slime = C-x C-e)
    " Mnemonic eXecute
    nmap <buffer> <Leader>X :ecl (vim-slime::slime-eval-last-expression)<CR>

    " <Leader>c compile and load file
    nmap <buffer> <Leader>c :ecl (vim-slime::slime-compile-file t)<CR>

    " <Leader>C compile file
    nmap <buffer> <Leader>C :ecl (vim-slime::slime-compile-file nil)<CR>

    " <Leader>sc is connect to a running Swank instance (Slime = M-x slime)
    " Mnemonic SlimeConnect
    nmap <buffer> <Leader>sc :ecl (vim-slime::slime-connect)<CR>

    " <Leader>i is inspect the word under the cursor
    nmap <buffer> <Leader>i :ecl (vim-slime::slime-inspect)<CR>

    " Mapping for echoing the arglist
    imap <buffer> <Space> <Space><Esc>:ecl (vim-slime::slime-echo-current-arglist)<CR>a
endfunction

" need to have 2 status lines
set cmdheight=2

" Stash the original buffer
let s:old_buffer = bufnr ("%")

" Keymappings for the REPL buffer
buffer --slim-vim-repl--
nmap <buffer> <CR> :ecl (vim-slime::repl-handle-enter-key)<CR>
" Mapping for echoing the arglist
"imap <buffer> <Space> <Space><Esc>:ecl (vim-slime::slime-echo-current-arglist)<CR>a

" Keymappings for the debug buffer
buffer --slim-vim-debug--
nmap <buffer> 0 :ecl (slime::sldb-invoke-restart 0)<CR>
nmap <buffer> 1 :ecl (slime::sldb-invoke-restart 1)<CR>
nmap <buffer> 2 :ecl (slime::sldb-invoke-restart 2)<CR>
nmap <buffer> 3 :ecl (slime::sldb-invoke-restart 3)<CR>
nmap <buffer> 4 :ecl (slime::sldb-invoke-restart 4)<CR>
nmap <buffer> 5 :ecl (slime::sldb-invoke-restart 5)<CR>
nmap <buffer> 6 :ecl (slime::sldb-invoke-restart 6)<CR>
nmap <buffer> 7 :ecl (slime::sldb-invoke-restart 7)<CR>
nmap <buffer> 8 :ecl (slime::sldb-invoke-restart 8)<CR>
nmap <buffer> 9 :ecl (slime::sldb-invoke-restart 9)<CR>
nmap <buffer> <CR> :ecl (interface::sldb-handle-enter-key)<CR>

" Inspector mappings
buffer --slim-vim-inspect--
nmap <buffer> <CR> :ecl (interface::inspector-handle-enter-key)<CR>
nmap <buffer> l    :ecl (interface::slime-inspector-pop)<CR>
nmap <buffer> q    :ecl (interface::slime-inspector-quit)<CR>

" Change back to the original buffer
exec "buffer" s:old_buffer

" Mappings below here are very temporary
nmap <buffer> <Space>3 :ecl (vim-slime::slime-connect)<CR>
nmap <buffer> <Space>r0 :ecl (slime::sldb-invoke-restart 0)<CR>
nmap <buffer> <Space>r1 :ecl (slime::sldb-invoke-restart 1)<CR>
nmap <buffer> <Space>r2 :ecl (slime::sldb-invoke-restart 2)<CR>
nmap <buffer> <Space>r3 :ecl (slime::sldb-invoke-restart 3)<CR>

" Only one function at the moment, but more later.
" NOTE: Only do stuff here directly related to ecl-slime, i.e. do not "set
" lisp" or "set syntax=lisp", etc.  If users want to do general Lisp-related
" stuff, they can create their own autocommands in their own .vimrc.
function! s:SlimVim_bufread()
  call s:SlimVim_main_mappings()
endfunction

augroup slim_vim
  au!
  autocmd BufNewfile,BufRead *.lisp,*.lsp,*.cl,*.lml,*.asd call s:SlimVim_bufread()
  autocmd VimLeave * ecl (slime::disconnect)
augroup END

if exists("$ECL_SLIME_PATH")
   let s:cwd = getcwd()
   exec "cd" $ECL_SLIME_PATH
   ecl (load "ecl-slime.lisp")
   exec "cd" s:cwd
else
   echoerr "Environment variable ECL_SLIME_PATH isn't set !"
endif

command! Slime :ecl (vim-slime::slime-connect)<cr>



More information about the slim-vim mailing list