"Pedro's VIM config (tweaked everyday)

"load pathogen pliugin manager
execute pathogen#infect() 
call pathogen#helptags()
" due to some issues while loading plugins, filetype is set to off

"----had to switch to NEO Bundle just here because of nerdtree git
 " Note: Skip initialization for vim-tiny or vim-small.
 if 0 | endif
 if &compatible
   set nocompatible             
 endif
 " Required:
 set runtimepath^=~/.vim/bundle/neobundle.vim/
 " Required:
 call neobundle#begin(expand('~/.vim/bundle/'))
 " Required:
 NeoBundleFetch 'Shougo/neobundle.vim'
NeoBundle 'scrooloose/nerdtree'
NeoBundle 'Xuyuanp/nerdtree-git-plugin'
call neobundle#end()
" Required:
filetype plugin indent on
 NeoBundleCheck
"---------------------------------------
filetype off

" set favorite color scheme
colorscheme solarized

" turns syntatic highlighting on
syntax on

" displays a powerline line status line in bottom
set laststatus=2
let g:airline_powerline_fonts = 1

"allows backspace to delete chars in inser mode
:set backspace=indent,eol,start 

"allows jedi to not freakout with splits
"let g:jedi#use_splits_not_buffers = "left"

" For crontab files I am disabling the local edit file, crontabs must be
" edited in place (otherwise:error)
autocmd filetype crontab setlocal nobackup nowritebackup

" Added the ctrl+n as a shortcut for nerdtree file explorer
map <C-n> :NERDTreeToggle<CR>

let g:NERDTreeIndicatorMapCustom = {
    \ "Modified"  : "✹",
    \ "Staged"    : "c",
    \ "Untracked" : "u",
    \ "Renamed"   : "➜",
    \ "Unmerged"  : "‡",
    \ "Deleted"   : "✖",
    \ "Dirty"     : "✗",
    \ "Clean"     : "✔︎",
    \ "Unknown"   : "?"
    \ }

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

" Changed the way new splits are added (bottom for horizontal, or right for vertical)
set splitbelow
set splitright

" Syntastic custom 
"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline+=%*

"let g:syntastic_always_populate_loc_list = 1
"let g:syntastic_auto_loc_list = 1
"let g:syntastic_check_on_open = 0
"let g:syntastic_check_on_wq = 0

" Arduino-vim, i prefer ino right now to ano and I made my script to launch
" serial in iTerm in a tab lower to vi (so neat!)
let g:vim_arduino_ino_cmd = 'ino'
let g:vim_arduino_serial_terminal_script = 'vim-arduino-serial-iterm'

" Added a keymap to opening a new terminal in a small split under the current
" one
map <C-B> :vsp<CR>:VimShell<CR>

" Added a keymap to know what time it is.
map <C-T> :echo 'Current time is ' . strftime('%c')<CR>

" how's my battery doing (MAC OSX)
map <C-L>  :echo 'battery life:' . system("pmset -g batt")<CR>

" Programs a FPGA by invoking the icestorm commands
map <C-F><C-U> :call FPGACompileAndUpload(1)<CR>
map <C-F><C-F> :call FPGACompileAndUpload(0)<CR>

"CTRL -S to save
" If the current buffer has never been saved, it will have no name,
" call the file browser to save it, otherwise just save it.
command -nargs=0 -bar Update if &modified 
                           \|    if empty(bufname('%'))
                           \|        browse confirm write
                           \|    else
                           \|        confirm write
                           \|    endif
                           \|endif
"map <C-s> :<C-u>Update<CR>
inoremap <c-s> <c-o>:Update<CR>
"this also required "stty -ixon" in my bashrc file

" Solving issue with pastemode with autoident, map Ctrl P to the paste mode
nnoremap <C-P> :set invpaste paste?<CR>
set pastetoggle=<C-P>
"set showmode

"Pythonian life:
" Comment #
vnoremap <silent> # :s#^#\##<cr>:noh<cr>
" Uncommend -#
vnoremap <silent> -# :s#^\###<cr>:noh<cr>

" Maximize the current split, then come back to structure later
nnoremap <C-W>O :call MaximizeToggle()<CR>
nnoremap <C-W>o :call MaximizeToggle()<CR>
nnoremap <C-W><C-O> :call MaximizeToggle()<CR>

" Make all splits go horizontal
nnoremap <C-_> :windo wincmd K<cr>
" Make all splits go vertical
nnoremap <bar> :windo wincmd H<cr>

function! MaximizeToggle()
  if exists("s:maximize_session")
    exec "source " . s:maximize_session
    call delete(s:maximize_session)
    unlet s:maximize_session
    let &hidden=s:maximize_hidden_save
    unlet s:maximize_hidden_save
  else
    let s:maximize_hidden_save = &hidden
    let s:maximize_session = tempname()
    set hidden
    exec "mksession! " . s:maximize_session
    only
  endif
endfunction

function! CharCount()
"	exec !wc -m %
"endfunction
    let countchar = system("wc -m " . bufname("%"))
    let formated_count = "char count is:" . countchar
    vsplit __CharCount__
    normal! ggdG
    setlocal buftype=nofile
    call append(0, split(formated_count, '\v\n'))
endfunction

" TODO: get module name directly from parsing file
function! FPGACompileAndUpload(upload)
    set ignorecase
    if expand('%:e') == "v"
        write "saves the current file
        let file = expand('%:r') "grabs filename without extension (not needed for now)
        echo 'Cleaning ' . system("rm -f " . file . ".blif " . file . "*.txt " . file . "*.bin")
        echo "Synthesizing " . system("yosys -q -p 'synth_ice40 -top top -blif " . file .".blif' " . file . ".v")
        echo "Mapping to hardware " . system("arachne-pnr -q -d 1k -o " . file . ".txt -p icestick.pcf " . file . ".blif")
        echo "Packing binary " . system("icepack ". file . ".txt " . file . ".bin")
        if a:upload == 1
            echo 'Uploading ' . system("iceprog " . file . ".bin")
        endif 
    else
        echo "File not a Verilog file, will not continue."
    endif
endfunction


"nnoremap <C-M> :call CharCount()<CR>
"map <C-W> :call CharCount()<cr>

" missing: users to get my name
" change vim shell prompt to show current dir and prompt itself
" let g:vimshell_user_prompt = 'fnamemodify(getcwd(), "pedro$")'
" let g:vimshell_user_prompt = 'fnamemodify(getcwd(), ":~")'
let g:vimshell_user_prompt = 'fnamemodify("","")'

" tabs to 4 spaces
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

" force .md to markdown instead of modula2 (note this won't be needed in
" vi=>7.4)
autocmd BufNewFile,BufRead *.md set filetype=markdown

filetype plugin indent on
syntax on
