execute python in a new window

This commit is contained in:
ManjaroOne666 2018-03-30 23:31:10 +01:00
parent 5bd71dd921
commit 2da8339c12
2 changed files with 57 additions and 0 deletions

View File

@ -4,3 +4,6 @@ set shiftwidth=4
set expandtab
set shiftround
set autoindent
nnoremap <silent> <buffer> <F5> :call SaveAndExecutePython()<CR>
" vnoremap <silent> <buffer> <F5> :<C-u>call SaveAndExecutePython()<CR>

View File

@ -138,6 +138,60 @@ function! s:RunShellCommand(cmdline) abort
endfunction
"}}}
" https://stackoverflow.com/a/40195855
function! SaveAndExecutePython()
" SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
" save and reload current file
silent execute "update | edit"
" get file path of current file
let s:current_buffer_file_path = expand("%")
let s:output_buffer_name = "Python"
let s:output_buffer_filetype = "output"
" reuse existing buffer window if it exists otherwise create a new one
if !exists("s:buf_nr") || !bufexists(s:buf_nr)
silent execute 'top new ' . s:output_buffer_name
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
silent execute 'top new'
silent execute s:buf_nr . 'buffer'
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
silent execute bufwinnr(s:buf_nr) . 'wincmd w'
endif
silent execute "setlocal filetype=" . s:output_buffer_filetype
setlocal bufhidden=delete
setlocal buftype=nofile
setlocal noswapfile
setlocal nobuflisted
setlocal winfixheight
setlocal cursorline " make it easy to distinguish
setlocal nonumber
setlocal norelativenumber
setlocal showbreak=""
nnoremap <buffer> q :bdelete<CR>
" clear the buffer
setlocal noreadonly
" setlocal modifiable
%delete _
" add the console output
silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
" resize window to content length
" 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.
" 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.
" But if you close the output buffer then it returns to using the default size when its recreated
"execute 'resize' . line('$')
" make the buffer non modifiable
setlocal readonly
" setlocal nomodifiable
endfunction
" vim: foldmethod=marker