Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.20 KB | None | 0 0
  1. function! SaveAndExecutePython()
  2.     " SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
  3.  
  4.     " save and reload the current file
  5.     silent execute "update | edit"
  6.  
  7.     " get file path of current file
  8.     let s:current_buffer_file_path = expand("%")
  9.  
  10.     let s:output_buffer_name = "Python"
  11.     let s:output_buffer_filetype = "output"
  12.  
  13.     " reuse existing buffer window if it exists otherwise create a new one
  14.     if !exists("s:buf_nr") || !bufexists(s:buf_nr)
  15.         silent execute 'botright new ' . s:output_buffer_name
  16.         let s:buf_nr = bufnr('%')
  17.     elseif bufwinnr(s:buf_nr) == -1
  18.         silent execute 'botright new'
  19.         silent execute s:buf_nr . 'buffer'
  20.     elseif bufwinnr(s:buf_nr) != bufwinnr('%')
  21.         silent execute bufwinnr(s:buf_nr) . 'wincmd w'
  22.     endif
  23.  
  24.     silent execute "setlocal filetype=" . s:output_buffer_filetype
  25.     setlocal bufhidden=delete
  26.     setlocal buftype=nofile
  27.     setlocal noswapfile
  28.     setlocal nobuflisted
  29.     setlocal winfixheight
  30.     setlocal cursorline " make it easy to distinguish
  31.     setlocal nonumber
  32.     setlocal norelativenumber
  33.     setlocal showbreak=""
  34.  
  35.     " clear the buffer
  36.     setlocal noreadonly
  37.     setlocal modifiable
  38.     %delete _
  39.  
  40.     " add the console output
  41.     silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
  42.  
  43.     " resize window to content length
  44.     " Note: This is annoying because if you print a lot of lines then your code buffer is forced to a height of one line every time you run this function.
  45.     "       However without this line the buffer starts off as a default size and if you resize the buffer then it keeps that custom size after repeated runs of this function.
  46.     "       But if you close the output buffer then it returns to using the default size when its recreated
  47.     execute 'resize' . line('$')
  48.  
  49.     " make the buffer non modifiable
  50.     setlocal readonly
  51.     setlocal nomodifiable
  52. endfunction
  53.  
  54. " Bind F5 to save file if modified and execute python script in a buffer.
  55. " Note: This is a normal mode bind because in insert mode F5 would insert a "<F5>" into the text.
  56. nnoremap <silent> <F5> :<C-u>call SaveAndExecutePython()<CR>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement