Skip to content

Instantly share code, notes, and snippets.

@robince
Created July 30, 2010 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robince/501194 to your computer and use it in GitHub Desktop.
Save robince/501194 to your computer and use it in GitHub Desktop.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function Send_to_Screen(text)
if !exists("g:screen_sessionname") || !exists("g:screen_windowname")
call Screen_Vars()
end
execute "python send_with_strip()"
endfunction
function Screen_Session_Names(A,L,P)
return system("screen -ls | awk '/Attached/ {print $1}'")
endfunction
function Screen_Vars()
if !exists("g:screen_sessionname") || !exists("g:screen_windowname")
let g:screen_sessionname = ""
let g:screen_windowname = "0"
end
let g:screen_sessionname = input("session name: ", "", "custom,Screen_Session_Names")
let g:screen_windowname = input("window name: ", g:screen_windowname)
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! s:DefPython()
python << PYTHONEOF
import vim
from subprocess import call
def send_to_screen():
session = vim.eval("g:screen_sessionname")
windowname = vim.eval("g:screen_windowname")
text = vim.eval("a:text")
args = ['screen', '-S', session, '-p', windowname, '-X', 'stuff']
# need to escape anything?
args.append(text + '\n')
ret = call(args)
def send_with_strip():
session = vim.eval("g:screen_sessionname")
windowname = vim.eval("g:screen_windowname")
textin = vim.eval("a:text")
text = strip_text(textin)
args = ['screen', '-S', session, '-p', windowname, '-X', 'stuff']
# need to escape anything?
args.append(text + '\n')
ret = call(args)
def strip_text(text):
lines = text.split('\n')
nonblank = [l for l in lines if l.strip()]
s = nonblank[0]
indent = len(s) - len(s.lstrip(' '))
cleanlines = [l[indent:] for l in nonblank]
# comment character
cc = vim.eval('b:left')
cleanlines = [l for l in cleanlines if l.lstrip()[0] != cc]
outtext = '\n'.join(cleanlines)
s = cleanlines[-1]
# TODO: add an extra \n on reduction in inent
if s[0] == ' ':
# end on an indent so add CR for ipython
outtext += '\n'
return outtext
PYTHONEOF
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call s:DefPython()
vmap <C-c><C-c> "ry :call Send_to_Screen(@r)<CR>
nmap <C-c><C-c> vip<C-c><C-c>
"block (python only)
nmap <C-c><C-b> ]v<C-c><C-c>
nmap <C-c><C-l> "ryy :call Send_to_Screen(@r)<CR>
nmap <C-c>v :call Screen_Vars()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment