" ================================================
" INITIALIZATION
" ================================================
set nocompatible
set nobackup
set fileencoding=utf8 encoding=utf8
set history=50      " keep 40 lines of command line history
set autoindent      " always set autoindenting on
set incsearch		" do incremental searching
set autoread        " auto read changed files
set showcmd		    " display incomplete commands
set number          " turn on lines number
set ruler           " turn on status line on bottom right corner
" set nofixendofline  " turn off end of line on every file
" set cursorline      " highlight current line
" set cursorcolumn    " highlight current column
" set ff=unix         " set linebreak to return
" set ffs=unix,dos
set backspace=indent,eol,start
set shiftwidth=4 tabstop=4 softtabstop=4 expandtab
let mapleader = ","     " map <leader> to for convenience
autocmd FileType help wincmd L " open help window vertically on the right
filetype plugin indent on

if has("win32")
    source $VIMRUNTIME/mswin.vim
    nnoremap <F11> :call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
    inoremap <F11> <ESC>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>a
endif

if has("win64")
    nnoremap <F11> :call libcallnr("gvimfullscreen_x64.dll", "ToggleFullScreen", 0)<CR>
    inoremap <F11> <ESC>:call libcallnr("gvimfullscreen_x64.dll", "ToggleFullScreen", 0)<CR>a
endif

if has("gui_running")
    set guioptions=''   " remove all extra GUI components
    set mouse=a
    set lines=36 columns=128    " consistent window size
    winpos 200 160
    colorscheme solarized
else
    color monokai
endif

" switch syntax highlighting on, when the terminal has colors
" also switch on highlight the last used search pattern
if &t_Co > 2 || has("gui_running")
    syntax on
    set hlsearch
endif

if has("autocmd")
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif
endif

" ================================================
" KEY BINDINGS
" ================================================
" for manipulating vimrc(valid when vimrc in $HOME dir)
nnoremap <leader>ev :vsplit $MYVIMRC<CR>
nmap <silent> <leader>sv :source $MYVIMRC<CR>

" for managing tabs
nmap <C-T> :tabnew<CR>
imap <C-T> <ESC>:tabnew<CR>
nmap <C-W> :tabclose<CR>
imap <C-W> <ESC>:tabclose<CR>
nmap <C-PageUp> :tabprevious<CR>
imap <C-PageUp> <ESC>:tabprevious<CR>
nmap <C-PageDown> :tabnext<CR>
imap <C-PageDown> <ESC>:tabnext<CR>

" toggle highlight search
nmap <silent> <leader>th :set nohlsearch!<CR>

" treat long lines as break lines
nmap j gj
nmap k gk

" spell check
nmap <silent> <leader>ts :setlocal spell!<CR>
nmap <leader>sn ]s
nmap <leader>sp [s
nmap <leader>sa zg
nmap <leader>ss z=

" shortcuts for common tasks
nmap <F1> :w<CR>
imap <F1> <ESC>:w<CR>
nmap <silent> <leader>q :q<CR>

" simulate emacs in insert mode
inoremap <C-F> <RIGHT>
inoremap <C-B> <LEFT>
inoremap <C-N> <DOWN>
inoremap <C-P> <UP>
inoremap <C-E> <END>
inoremap <C-A> <HOME>

" for jumping to specific window quickly
noremap <C-J> <C-W>j
noremap <C-K> <C-W>k
noremap <C-L> <C-W>l
noremap <C-H> <C-W>h

" increase and decrease window size
noremap <S-F8> <ESC>:vertical resize -2<CR>
noremap <S-F9> <ESC>:vertical resize +2<CR>
noremap <M-S-F8> <ESC>:resize -2<CR>
noremap <M-S-F9> <ESC>:resize +2<CR>

" ================================================
" ABBREVIATIONS
" ================================================
iab xtime <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>
iab xdate <C-R>=strftime("%Y-%m-%d")<CR>

" ================================================
" PLUGINS
" ================================================
" pathogen
execute pathogen#infect()

" nerdtree
let NERDTreeQuitOnOpen = 1
nmap <silent> <F8> :NERDTreeToggle<CR>

" tagbar
nmap <silent> <F9> :TagbarToggle<CR>

" lightline
let g:lightline = {'colorscheme': 'solarized'}

" vim-markdown
au FileType md set filetype=mkd
let g:vim_markdown_folding_disabled=2

" calendar
let g:calendar_monday = 1
if has("win32")
    let g:calendar_diary = "D:/Backup/Calendar"
else
    let g:calendar_diary = "~/.calendar"
endif

" colorizer
nmap <leader>ch :ColorHighlight<CR>
nmap <leader>cc :ColorClear<CR>
let g:colorizer_auto_filetype='css,html,vue'

" nerdcommenter
let g:NERDSpaceDelims = 1 " Add spaces after comment delimiters by default
" let g:NERDCompactSexyComs = 1 " Use compact syntax for prettified multi-line comments
" let g:NERDDefaultAlign = 'left' " Align line-wise comment delimiters flush left instead of following code indentation
" let g:NERDAltDelims_java = 1 " Set a language to use its alternate delimiters by default
" let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } } " Add your own custom formats or override the defaults
" let g:NERDCommentEmptyLines = 1 " Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDTrimTrailingWhitespace = 1 " Enable trimming of trailing whitespace when uncommenting

" vim-table-mode
let g:table_mode_corner='|'
nmap <silent> <leader>ttm :TableModeToggle<CR>

" vim-gitgutter
nmap <silent> <F10> :GitGutterToggle<CR>
imap <silent> <F10> <ESC>:GitGutterToggle<CR>
let g:gitgutter_enabled = 0
let g:gitgutter_highlight_lines = 1

let g:UltiSnipsExpandTrigger="<TAB>"
let g:UltiSnipsJumpForwardTrigger="<TAB>"
let g:UltiSnipsJumpBackwardTrigger="<S-TAB>"

" git
nmap <silent> <F4> :Gblame<CR>

" switch tab width between 4(default) and 2
fun! SetTabWidth()
    if &shiftwidth == 4
        let width = 2
    else
        let width = 4
    endif
    exe "set shiftwidth=" . width . " tabstop=" . width . " softtabstop=" . width . " expandtab"
endfun

" switch font between fixedsys and Consolas
fun! SwitchFont()
    if &guifont == 'fixedsys'
        exec "set guifont=Consolas:h14:cANSI:qDRAFT"
    else
        exec "set guifont=fixedsys"
    endif
endfun

" toggle line/column highlight
fun! SwitchCrossHighlight()
    if &cursorline == 0
        exec "set cursorline"
        exec "set cursorcolumn"
    else
        exec "set nocursorline"
        exec "set nocursorcolumn"
    endif
endfunc

nmap <silent> <F12> :call SetTabWidth()<CR>
nmap <silent> <F10> :call SwitchFont()<CR>
nmap <silent> <F5> :call SwitchCrossHighlight()<CR>

" ================================================
" FILE SPECIFIC CONFIGURATION
" ================================================
" autocmd FileType vue syntax sync fromstart " vue
" let g:html_indent_inctags = "html,head,body" " html
nmap <F2> :silent ! compass compile % <CR><CR>
