" Profile startup time
" vim --startuptime vim.log -c q
" vim -c 'r ! cat vim.log| sort -k 2'

" The mapleader has to be set before vundle starts loading all the plugins.
let mapleader=";"

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" NeoBundleInitialisation {{{
    if has('vim_starting')
      set runtimepath+=~/.vim/bundle/neobundle.vim/
    endif

    call neobundle#rc(expand('~/.vim/bundle/'))

    " Let NeoBundle manage NeoBundle
    NeoBundleFetch 'Shougo/neobundle.vim'
    NeoBundle 'Shougo/vimproc', {
          \ 'build' : {
          \     'windows' : 'make -f make_mingw32.mak',
          \     'cygwin' : 'make -f make_cygwin.mak',
          \     'mac' : 'make -f make_mac.mak',
          \     'unix' : 'make -f make_unix.mak',
          \    },
          \ }, "{{{
        let g:neobundle#install_process_timeout = 3000
    "}}}

    " Syntax
    NeoBundle 'itchyny/landscape.vim'
"}}}

" Tabs & indenting {{{
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set softtabstop=4

    au FileType python,ruby setl sw=2 sts=2 et
    au FileType javascript,css,less,sass,scss setl sw=2 sts=2 et
    au FileType php,phtml,html setl sw=4 sts=4 et

    filetype plugin indent on
    filetype plugin on
" }}}

" Colourscheme {{{
    syntax on
    syntax sync fromstart
    colorscheme landscape

    hi LineNr ctermbg=016
    hi MatchParen term=reverse ctermfg=027
" }}}

" NeoBundle {{{

    " Visual
    NeoBundle 'vim-scripts/jumphl.vim', "{{{
        autocmd VimEnter * DoJumpHl " Highlight line after jump
    "}}}
    NeoBundle 'ivyl/vim-bling'
    NeoBundle 'kana/vim-narrow'
    NeoBundle 'qstrahl/vim-matchmaker', "{{{
        let g:matchmaker_enable_startup = 0

        nnoremap <space>m :MatchmakerToggle<CR>
    "}}}

    " Indenting
    NeoBundle '2072/PHP-Indenting-for-VIm'
    NeoBundle 'pangloss/vim-javascript'
    NeoBundle 'editorconfig/editorconfig-vim'

    " Syntax
    NeoBundle 'groenewege/vim-less', {'autoload':{'filetypes':['less']}}, "{{{
        au BufNewFile,BufRead *.less setf less
    "}}}
    NeoBundle 'ntpeters/vim-better-whitespace', " {{{
        autocmd FileType c,cpp,css,less,sass,scss,java,php,ruby,puppet,python,javascript,vim,sh,nginx,ant,xml autocmd BufWritePre <buffer> StripWhitespace
    "}}}
    NeoBundle 'hail2u/vim-css3-syntax'
    NeoBundle 'cakebaker/scss-syntax.vim', {'autoload':{'filetypes':['scss','sass']}}
    NeoBundle 'ap/vim-css-color', {'autoload':{'filetypes':['css','scss','sass','less','styl']}}, "{{{
        let g:cssColorVimDoNotMessMyUpdatetime = 1
    "}}}
    NeoBundle 'Glench/Vim-Jinja2-Syntax', "{{{
        autocmd BufRead,BufNewFile *.nunjucks setlocal filetype=jinja
    "}}}
    NeoBundle 'othree/html5.vim', {'autoload':{'filetypes':['html', 'jinja2', 'phtml']}}
    NeoBundle 'dbakker/vim-md-noerror'
    NeoBundle 'rodjek/vim-puppet', "{{{
        au BufNewFile,BufRead *.pp setf puppet
    "}}}
    NeoBundle 'evanmiller/nginx-vim-syntax'
    NeoBundle 'maksimr/vim-jsbeautify', {'autoload':{'filetypes':['javascript']}} "{{{
    let g:config_Beautifier =  {
                \'js' : {
                \   'indent_char': ' ',
                \   'indent_size': 2
                \}
                \}
    nnoremap <leader>fjs :call JsBeautify()<cr>
    "}}}
    NeoBundleLazy 'jelera/vim-javascript-syntax', {'autoload':{'filetypes':['javascript']}}

    " Images
    NeoBundle 'tpope/vim-afterimage'

    " Completion
    NeoBundle 'vim-scripts/dbext.vim'
    NeoBundle 'marijnh/tern_for_vim', {
        \ 'build': {
        \   'others': 'npm install'
        \}}
    NeoBundle 'Shougo/neocomplete.vim', "{{{
        " brew install vim --with-python --with-ruby --with-perl --with-lua --with-tcl
        " brew install macvim --with-cscope --with-lua --override-system-vim
        set completeopt-=preview

        let g:acp_enableAtStartup = 0
        let g:neocomplete#enable_at_startup = 0
        let g:neocomplete#enable_smart_case = 1
        let g:neocomplete#sources#syntax#min_keyword_length = 3
        let g:neocomplete#lock_buffer_name_pattern = '\*ku\*'
        let g:neocomplete#data_directory='~/.vim/.cache/neocomplete'

        " Define dictionary.
        let g:neocomplete#sources#dictionary#dictionaries = {
            \ 'default' : '',
            \ 'vimshell' : $HOME.'/.vimshell_hist',
            \ 'scheme' : $HOME.'/.gosh_completions'
            \ }

        if !exists('g:neocomplete#keyword_patterns')
            let g:neocomplete#keyword_patterns = {}
        endif
        let g:neocomplete#keyword_patterns['default'] = '\h\w*'

        " Plugin key-mappings.
        inoremap <expr><C-g> neocomplete#undo_completion()
        inoremap <expr><C-l> neocomplete#complete_common_string()

        inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
        inoremap <expr><C-h> neocomplete#smart_close_popup()."\<C-h>"
        inoremap <expr><BS> neocomplete#smart_close_popup()."\<C-h>"
        inoremap <expr><C-y>  neocomplete#close_popup()
        inoremap <expr><C-e>  neocomplete#cancel_popup()
        inoremap <expr><C-Space> neocomplete#start_manual_complete('omni')

        autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
        autocmd FileType php set omnifunc=phpcomplete#CompletePHP
        autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags

        autocmd FileType javascript setlocal omnifunc=tern#Complete

        autocmd FileType coffee setlocal omnifunc=javascriptcomplete#CompleteJS
        autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
        autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags
    " }}}

    " Usability
    NeoBundle 'tpope/vim-commentary'
    NeoBundle 'AndrewRadev/splitjoin.vim', "{{{
        nmap gS :SplitjoinSplit<cr>
        nmap gJ :SplitjoinJoin<cr>
    " }}}
    NeoBundle 'inkarkat/closetag.vim'

    " Documentation
    NeoBundle 'heavenshell/vim-jsdoc', "{{{
        let g:jsdoc_allow_input_prompt = 1
        let g:jsdoc_input_description = 1
        let g:jsdoc_additional_descriptions = 1
        let g:jsdoc_return = 1
        let g:jsdoc_return_type = 1
        let g:jsdoc_return_description = 1
        let g:jsdoc_default_mapping = 0 " default: 1 Set value to 0 to turn off default mapping of :JsDoc

        nnoremap <space>j :JsDoc<cr>
    "}}}

    " Motion
    NeoBundle 'Lokaltog/vim-easymotion'
    NeoBundle 'dbakker/vim-paragraph-motion'
    NeoBundle 'matze/vim-move', "{{{
        " <C-k>   Move current line/selections up
        " <C-j>   Move current line/selections down
        let g:move_key_modifier = 'C'
    "}}}

    " Features
    NeoBundle 'mhinz/vim-startify', "{{{
        let g:startify_show_sessions = 1
        let g:startify_list_order = ['sessions', 'bookmarks', 'dir', 'files']
        let g:startify_session_dir = expand('~/.vim/.cache/unite/session')
        let g:startify_change_to_dir = 1

        autocmd FileType startify setlocal nocursorline
        autocmd VimEnter *
                \ if !argc() |
                \   Startify |
                \   NERDTree |
                \   execute "normal \<c-w>w" |
                \ endif

        hi StartifyBracket ctermfg=100
        hi StartifyNumber  ctermfg=215
        hi StartifyPath    ctermfg=245
        hi StartifySlash   ctermfg=240
    " }}}

    " Status bar
    NeoBundle 'bling/vim-airline', "{{{
        set laststatus=2
        let g:airline_powerline_fonts = 1
        let g:airline_theme = 'airlineish'
    " }}}
    NeoBundle 'paranoida/vim-airlineish'

    " Syntax
    NeoBundle 'luochen1990/rainbow', "{{{
        let g:rainbow_active = 0

        nnoremap <space>r :RainbowToggle<CR>
    "}}}
    NeoBundle 'scrooloose/syntastic', "{{{
        " Show / hide location list
        noremap <silent><leader>lc :lcl<CR>
        noremap <silent><leader>lo :lw<CR>

        noremap <F6> :SyntasticCheck<CR>

        let g:syntastic_mode_map = { 'mode': 'active',
                                    \ 'active_filetypes': ['javascript', 'php'],
                                    \ 'passive_filetypes': ['css', 'html', 'xhtml', 'scss', 'sass'] }

        let g:syntastic_check_on_open = 0
        let g:syntastic_enable_signs = 1
        let g:syntastic_auto_jump = 1
        let g:syntastic_auto_loc_list = 0
        let g:syntastic_always_populate_loc_list = 1
        let g:syntastic_enable_highlighting = 1
        let g:syntastic_echo_current_error  = 1

        let g:syntastic_javascript_checkers=['jshint']
        let g:syntastic_php_checkers=['php']
        let g:syntastic_css_checkers=['csslint']
    "}}}
    NeoBundle 'Shougo/unite-outline', "{{{
        nnoremap <space>o :Unite outline<CR>
    "}}}
    NeoBundle 'Shougo/unite-session'
    NeoBundle 'kien/ctrlp.vim', "{{{
        let g:ctrlp_extensions = ['funky']
        let g:ctrlp_match_window_bottom = 0
        let g:ctrlp_match_window_reversed = 0

        " let g:ctrlp_dont_split = 'netrw'

        nnoremap <space>p :CtrlP<cr>
        nnoremap <space>b :CtrlPBuffer<cr>
    "}}}
    NeoBundle 'tacahiroy/ctrlp-funky', "{{{
        nnoremap <space>o :CtrlPFunky<cr>
    "}}}
    NeoBundle 'vim-scripts/YankRing.vim', "{{{
        nnoremap <space>y :YRShow<cr>
    "}}}
    NeoBundle 'luochen1990/select-and-search', "{{{
        let g:select_and_search_active = 1
    "}}}
    NeoBundle 'Shougo/unite.vim', "{{{
        let g:unite_data_directory = expand('~/.vim/.cache/unite')
        let g:unite_enable_start_insert=0
        let g:unite_source_rec_max_cache_files=5000
        let g:unite_prompt='» '

        " CTRL-P
        " nnoremap <space>p :Unite -start-insert file_rec/async<cr>
        " call unite#filters#matcher_default#use(['matcher_fuzzy'])
        " call unite#set_profile('files', 'smartcase', 1)

        " Searching - brew install the_silver_searcher
        let g:unite_source_grep_command='ag'
        let g:unite_source_grep_default_opts='--nogroup --nocolor --column --ignore .sass-cache'
        nnoremap <space>/ :<C-u>Unite -buffer-name=search grep:.<cr>

        " Session
        let g:unite_source_session_enable_auto_save = 1
        nnoremap <F4> :<C-u>UniteSessionSave<space>
        noremap <space>s :Unite -quick-match -buffer-name=sessions session<CR>

        " Custom mappings for the unite buffer
        autocmd FileType unite call s:unite_settings()
        function! s:unite_settings()

            " Enable navigation with control-j and control-k in insert mode
            imap <silent><buffer> <C-j> <Plug>(unite_select_next_line)
            imap <silent><buffer> <C-k> <Plug>(unite_select_previous_line)

            " Enable opening items in splits or tab
            imap <silent><buffer><expr> <C-x> unite#do_action('split')
            imap <silent><buffer><expr> <C-v> unite#do_action('vsplit')
            imap <silent><buffer><expr> <C-t> unite#do_action('tabopen')

            nmap <buffer> <ESC> <Plug>(unite_exit)

        endfunction

    " }}}
    NeoBundle 'sjl/gundo.vim', "{{{
        nnoremap <F5> :GundoToggle<CR>
        let g:gundo_right = 0
        let g:gundo_preview_bottom = 1
        let g:gundo_close_on_revert = 1
    " }}}
    NeoBundle 'terryma/vim-expand-region'
    NeoBundle 'kris89/vim-multiple-cursors'
    NeoBundle 'hlissner/vim-multiedit'

    " Utility
    NeoBundle 't9md/vim-choosewin', "{{{
        let g:choosewin_overlay_enable = 1
        nmap  <tab> :ChooseWin<CR>

        " workaround for overlay font broken on mutibyte buffer.
        let g:choosewin_overlay_clear_multibyte = 1

        " tmux like overlay color
        let g:choosewin_color_overlay = {
                    \ 'gui': ['DodgerBlue3', 'DodgerBlue3' ],
                    \ 'cterm': [ 25, 25 ]
                    \ }
        let g:choosewin_color_overlay_current = {
                    \ 'gui': ['firebrick1', 'firebrick1' ],
                    \ 'cterm': [ 124, 124 ]
                    \ }

        let g:choosewin_blink_on_land      = 0 " dont' blink at land
        let g:choosewin_statusline_replace = 0 " don't replace statusline
        let g:choosewin_tabline_replace    = 0 " don't replace tabline
        "}}}
    NeoBundle 'justinmk/vim-gtfo'
    NeoBundle 'scrooloose/nerdtree', "{{{
        nmap <space>f :NERDTreeToggle<CR>
        nmap <space>n :NERDTreeFind<CR>
        autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

        let NERDTreeShowLineNumbers=1
    "}}}
    NeoBundle 'tpope/vim-unimpaired'
    NeoBundle 'AndrewRadev/inline_edit.vim'
    NeoBundle 'tpope/vim-surround'
    NeoBundle 'tpope/vim-eunuch'
    NeoBundle 'tpope/vim-repeat'
    NeoBundle 'vim-scripts/bufkill.vim', "{{{
        nnoremap <space>d :BW<CR>
    "}}}
    NeoBundle 'Raimondi/delimitMate', "{{{
        let delimitMate_expand_cr = 0
        let delimitMate_jump_expansion = 0
    "}}}
    NeoBundle 'tsaleh/vim-matchit'

    " VCS
    NeoBundle 'tommcdo/vim-fugitive-blame-ext'
    NeoBundle 'tpope/vim-fugitive', "{{{
        noremap <silent> <leader>gs :Gstatus<CR>
        nnoremap <silent> <leader>gd :Gdiff<CR>
        nnoremap <silent> <leader>gc :Gcommit<CR>
        nnoremap <silent> <leader>gb :Gblame<CR>
        nnoremap <silent> <leader>gl :Glog<CR>
        nnoremap <silent> <leader>gp :Git push<CR>
        nnoremap <silent> <leader>gw :Gwrite<CR>
        nnoremap <silent> <leader>gr :Gremove<CR>
        autocmd FileType gitcommit nmap <buffer> U :Git checkout -- <C-r><C-g><CR>
        autocmd BufReadPost fugitive://* set bufhidden=delete
    "}}}

    NeoBundleCheck
" }}}

" General settings {{{
    set number
    set cindent

    set re=1

    " a - terse messages (like [+] instead of [Modified])
    " t - truncate file names
    " I - no intro message when starting vim fileless
    " T - truncate long messages to avoid having to hit a key
    set shortmess=atIT

    set nowrap                      " Do not wrap lines by default
    set autoread                    " Reload files changed outside vim
    " set colorcolumn=80

    set history=1000                " remember more commands and search history
    set undolevels=1000             " remember more undo levels
    "set lazyredraw
    set ttyfast " u got a fast terminal

    " No need to show mode due to Powerline
    set noshowmode

    " Explicitly set encoding to utf-8
    set encoding=utf-8

    " Prevents MatchParen from loading, which can cause slowdown
    " let g:loaded_matchparen=1

    " This makes vim act like all other editors, buffers can :u
    " exist in the background without being in a window.
    " http://items.sjbach.com/319/configuring-vim-right
    set hidden

    " fix delete fail on os x http://vim.wikia.com/wiki/backspace_and_delete_problems
    set backspace=indent,eol,start

    " Do not highlight current line
    set nocursorline
    set nocursorcolumn

    set fcs=vert:│ " Solid line for vsplit separator

    " Select all
    nmap <space>a ggVG<CR>

" }}}

" Sudo {{{
    " w!!: Writes using sudo
    cnoremap w!! w !sudo tee % >/dev/null
" }}}

" Spelling toggle {{{
    " http://yavin4.anshul.info/2006/05/18/spell-check-in-vim-7/
    set spelllang=en_nz
    " Toggle spelling & line highlighting with F7
    map <silent> <F7> :set nospell!<CR>:set nospell?<CR> <bar> :set cursorline!<CR>
" }}}

" Disable arrow keys {{{
    map <up> <nop>
    map <down> <nop>
    map <left> <nop>
    map <right> <nop>
    imap <up> <nop>
    imap <down> <nop>
    imap <left> <nop>
    imap <right> <nop>
" }}}

" Pasting {{{
    nnoremap <f2> :set invpaste paste?<cr>
    set pastetoggle=<f2>            " toggle paste mode
" }}}

" Turn Off Swap Files {{{
    set noswapfile
    set nobackup
    set nowb
" }}}

" Position saving {{{
    " http://amix.dk/vim/vimrc.html
    " Return to last edit position when opening files (You want this!)
    autocmd BufReadPost *
        \ if line("'\"") > 0 && line("'\"") <= line("$") |
        \   exe "normal! g`\"" |
        \ endif
" }}}

" Persistent Undo {{{
    " Keep undo history across sessions, by storing in file.
    silent !mkdir ~/.vim/backups > /dev/null 2>&1
    set undodir=~/.vim/backups
    set undofile
" }}}

" Scrolling {{{
    set scrolloff=8 " Start scrolling when we're 8 lines away from margins
    set sidescrolloff=15
    set sidescroll=1
" }}}

" Search & Replace {{{
    set hlsearch
    set incsearch
    set smartcase
    set ignorecase
    set showmatch

    " Clear search highlight http://statico.github.io/vim.html
    nmap <space>q :nohlsearch<CR>

    " Replace all instances of word under cursor
    nnoremap <space>r :%s/\<<C-r><C-w>\>//g<Left><Left>
" }}}

" Show special characters {{{
    " http://vimcasts.org/episodes/show-invisibles/
    " Shortcut to rapidly toggle `set list`
    set list
    nmap <leader>l :set list!<CR>
    " Use the same symbols as TextMate for tabstops and EOLs
    set listchars=tab:┆\ ,trail:•,extends:❯,precedes:❮
    set shiftround
    set linebreak
    set showbreak=↪
" }}}

" Open preview window at bottom {{{
    set splitbelow
    set splitright
" }}}

" Buffer switching {{{
     map <leader>d :bd<cr> " delete buffer
     " nmap <tab> :bnext<CR>
     " nmap <s-tab> :bnext<CR>
"}}}

" PHP {{{
    au BufNewFile,BufRead *.phtml setf phtml
" }}}

" CSS {{{
    autocmd BufRead,BufNewFile *.css,*.scss,*.less setlocal foldmethod=marker foldmarker={,}
" }}}