" OS
let s:is_win   = has('win32') || has('win64')
let s:is_mac   = has('mac') || system('uname') =~? '^darwin'
let s:is_linux = !s:is_mac && has('unix')

filetype plugin indent on " Required for Neobundle

if has('vim_starting')
  set nocompatible               " Be iMproved
  set runtimepath+=~/.vim/bundle/neobundle.vim/
endif

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

" Let NeoBundle manage NeoBundle
NeoBundleFetch 'Shougo/neobundle.vim'

" Vim libraries
NeoBundle 'L9'
NeoBundle 'vim-jp/vital.vim'
NeoBundle 'Shougo/vimproc.vim', {
      \   'build' : {
      \     'windows' : 'tools\\update-dll-mingw',
      \     'cygwin' : 'make -f make_cygwin.mak',
      \     'mac' : 'make -f make_mac.mak',
      \     'linux' : 'make',
      \     'unix' : 'gmake',
      \   }
      \ }

" Ruby / Rails
NeoBundle 'vim-ruby/vim-ruby'
NeoBundle 'tpope/vim-rails'
NeoBundle 'tpope/vim-bundler'
NeoBundle 'alpaca-tc/neorspec.vim'
NeoBundle 'ngmy/vim-rubocop'
NeoBundle 'tpope/vim-rbenv'
" C++ / Clang
NeoBundle 'vim-jp/cpp-vim'
NeoBundle 'Rip-Rip/clang_complete'
" Erlang / Elixir
NeoBundle 'vim-erlang/vim-erlang-compiler'
NeoBundle 'vim-erlang/vim-erlang-omnicomplete'
NeoBundle 'vim-erlang/vim-erlang-runtime'
NeoBundle 'elixir-lang/vim-elixir'
" Golang
NeoBundle 'jnwhiteh/vim-golang'
" Lua
NeoBundle 'xolox/vim-lua-ftplugin'
NeoBundle 'xolox/vim-lua-inspect'
" JavaScript / CoffeeScript
NeoBundle 'pangloss/vim-javascript'
NeoBundle 'kchmck/vim-coffee-script'
" Shader
NeoBundle 'beyondmarc/hlsl.vim'
NeoBundle 'tikhomirov/vim-glsl'

" Ctags
NeoBundle 'szw/vim-tags'
NeoBundle 'soramugi/auto-ctags.vim'
" RubyMotion
NeoBundle 'rcyrus/snipmate-snippets-rubymotion'
" Groovy/Gradle
NeoBundle 'groovyindent'
NeoBundle 'tfnico/vim-gradle'
NeoBundle 'xuhdev/indent-java.vim'
NeoBundle 'tpope/vim-classpath'
" Git
NeoBundle 'gregsexton/gitv'  " Gitk like
" Syntax check
NeoBundle 'osyo-manga/vim-watchdogs'  "Async syntax checking.
NeoBundle "cohama/vim-hier"
NeoBundle "dannyob/quickfixstatus"
" Text object
NeoBundle 'LeafCage/yankround.vim'
NeoBundle 'h1mesuke/vim-alignta'
" File search
NeoBundle 'FuzzyFinder'
" Execute external command
NeoBundle 'thinca/vim-quickrun'
NeoBundle "osyo-manga/shabadou.vim"

" Other utilities
NeoBundle 'xolox/vim-misc'  " Miscellaneous auto-load Vim scripts
NeoBundle 'kana/vim-fakeclip' " Provide pseudo 'clipboard' registers ( for tmux )
NeoBundle 'surround.vim'
NeoBundle 'matchit.zip'
NeoBundle 'tpope/vim-fugitive'
NeoBundle 'mattn/emmet-vim'
NeoBundle 'tpope/vim-endwise'
NeoBundle 'Lokaltog/vim-easymotion'
NeoBundle 'rstacruz/sparkup', {'rtp': 'vim/'}
NeoBundle 'elzr/vim-json'
NeoBundle 'osyo-manga/vim-over'
NeoBundle 'vim-scripts/grep.vim'
NeoBundle 'scrooloose/nerdtree'
NeoBundle 'itspriddle/vim-marked'
NeoBundle 'tpope/vim-markdown'
NeoBundle 'tpope/vim-dispatch'
NeoBundle 'editorconfig/editorconfig-vim'
"NeoBundle 'airblade/vim-rooter'

" Joke
NeoBundle 'mattn/webapi-vim'

NeoBundle 'hail2u/vim-css3-syntax', {
      \ 'autoload' : {
      \   'filetypes' : ['css']
      \ }
      \}

NeoBundleLazy 'alpaca-tc/alpaca_tags', {
      \ 'depends': ['Shougo/vimproc'],
      \ 'autoload' : {
      \   'commands' : [
      \     { 'name' : 'AlpacaTagsBundle', 'complete': 'customlist,alpaca_tags#complete_source' },
      \     { 'name' : 'AlpacaTagsUpdate', 'complete': 'customlist,alpaca_tags#complete_source' },
      \     'AlpacaTagsSet', 'AlpacaTagsCleanCache', 'AlpacaTagsEnable', 'AlpacaTagsDisable', 'AlpacaTagsKillProcess', 'AlpacaTagsProcessStatus',
      \ ],
      \ }}

" if_luaが有効ならneocompleteを使う
NeoBundle has('lua') ? 'Shougo/neocomplete' : 'Shougo/neocomplcache'

" Unite and plugins
NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/neomru.vim'
NeoBundle 'ujihisa/unite-colorscheme'
NeoBundle 'h1mesuke/unite-outline'

" vimrc Benchmark
NeoBundle 'mattn/benchvimrc-vim'

" Color schemes
"" molokai only!
NeoBundle 'tomasr/molokai'

" TextObject
NeoBundle 'kana/vim-textobj-user'
NeoBundle 'nelstrom/vim-textobj-rubyblock'      " rr:all blocks, ro:definitions, rl:loop, rc:control, rd:do for ruby blocks
NeoBundle 'deris/vim-textobj-enclosedsyntax'    " aq, iq for treat enclosed syntax
NeoBundle 'kana/vim-textobj-indent'             " al, il for indent block
NeoBundle 'mattn/vim-textobj-url'               " au, iu for url
"NeoBundle 'mjbrownie/html-textobjects'          " Html textobject
NeoBundle 'osyo-manga/vim-textobj-multiblock'   " Unite (, [, { to ab, ib, and multiple block
NeoBundle 'osyo-manga/vim-textobj-multitextobj' " To gather multiple textobject into one textobject

" misc
NeoBundle 'deris/vim-duzzle'

" Non github repos
NeoBundle 'git://git.wincent.com/command-t.git'
" Non git repos
NeoBundle 'http://svn.macports.org/repository/macports/contrib/mpvim/'

"-- Installation check.
NeoBundleCheck

"-------------------- Plugin settings --------------------
"--- Switch.vim
so ~/.vim/plugin/switch.vim

"--- neocomplete or neocomplcache
so ~/.vim/plugin/neocomplete.vim

"--- Unite
so ~/.vim/plugin/unite.vim

"--- TextObject
so ~/.vim/plugin/textobject.vim

"--- quickrun
so ~/.vim/plugin/quickrun.vim

"--- css/sass/less
so ~/.vim/plugin/css.vim

"--- AlpacaTags
so ~/.vim/plugin/alpaca_tags.vim

"--- json
let g:vim_json_syntax_conceal = 1
let g:vim_json_syntax_concealcursor = 1

"--- Alignta
xnoremap al :Alignta<Space>

"--- vim-over
nnoremap <silent> <Leader>m :OverCommandLine<CR>%s/

"--- yankround
"" keymaps
nmap p <Plug>(yankround-p)
xmap p <Plug>(yankround-p)
nmap P <Plug>(yankround-P)
nmap gp <Plug>(yankround-gp)
xmap gp <Plug>(yankround-gp)
nmap gP <Plug>(yankround-gP)
nmap <C-p> <Plug>(yankround-prev)
nmap <C-n> <Plug>(yankround-next)

"--- ruby
au FileType ruby,eruby setl tags+=~/.vim/tmp/rtags

"---------------------------------------------------------------------
"--- vim general settings
set ambiwidth=double
set backupdir=$HOME/.vim/tmp/
set backupskip=/tmp/*,/private/tmp/*
set directory=$HOME/.vim/tmp/
set viminfo+=n~/.vim/tmp/viminfo.txt
set browsedir=buffer
set nocompatible
set expandtab
set hidden
set list
set listchars=tab:>\ ,extends:<
set number
set showmatch
set smartcase
set whichwrap=b,s,h,l,<,>,[,]
set autochdir

" Search
set incsearch " インクリメンタルサーチ. 1文字入力毎に検索を行う
set ignorecase " 検索パターンに大文字小文字を区別しない
set smartcase " 検索パターンに大文字を含んでいたら大文字小文字を区別する
set hlsearch " 検索結果をハイライト
" ESCキー2度押しでハイライトの切り替え
nnoremap <silent><Esc><Esc> :<C-u>set nohlsearch!<CR>

syntax on
filetype on
filetype indent on
filetype plugin on
set t_Co=256
set enc=utf-8
set fenc=utf-8
set fencs=utf-8,iso-2022-jp,euc-jp,cp932
set fileformats=unix,mac,dos
set ambiwidth=double
set statusline=%<%f\ %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,%c%V%8P

" activate molokai colorscheme
colorscheme molokai

" change the leader key to the ";"
let mapleader = ";"
noremap \ ;

" copy to clipboard when yanked
set clipboard=unnamed

" 表示行単位で移動
nnoremap j gj
nnoremap k gk
nnoremap <Down> gj
nnoremap <Up>   gk

" Tag jump
nnoremap <Leader>h :vsp<CR> :exe("tjump ".expand('<cword>'))<CR>
nnoremap <Leader>k :split<CR> :exe("tjump ".expand('<cword>'))<CR>

" <C-g> show the full file path
nnoremap <C-g> 1<C-g>

" tagsジャンプの時に複数ある時は一覧表示
nnoremap <C-]> g<C-]>

" Change color on status line when input mode
augroup InsertHook
autocmd!
autocmd InsertEnter * highlight StatusLine guifg=#ccdc90 guibg=#2E4340
autocmd InsertLeave * highlight StatusLine guifg=#2E4340 guibg=#ccdc90
augroup END

" Indent length
set autoindent
set smartindent
set smarttab
set tabstop=2
set shiftwidth=2
autocmd BufNewFile,BufRead *\.md set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.c set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.cpp set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.cs set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.java set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.h set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.gradle set tabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *\.*sl set tabstop=4 shiftwidth=4
autocmd FileType json setlocal sw=2 sts=2 ts=2 et
" File type
autocmd BufNewFile,BufRead *\.shader set filetype=glsl
autocmd BufNewFile,BufRead Fastfile set filetype=ruby
autocmd BufNewFile,BufRead Matchfile set filetype=ruby

" Python
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

" Json formatting
command! Jf :execute '%!python -mjson.tool'
  \ | :execute '%!python -c "import re,sys;chr=__builtins__.__dict__.get(\"unichr\", chr);sys.stdout.write(re.sub(r\"\\u[0-9a-f]{4}\", lambda x: chr(int(\"0x\" + x.group(0)[2:], 16)), sys.stdin.read()))"'
  \ | :%s/ \+$//ge
  \ | :set ft=javascript
  \ | :1

" Highlight the trailing whitespace
augroup HighlightTrailingSpaces
  autocmd!
  autocmd VimEnter,WinEnter,ColorScheme * highlight TrailingSpaces term=underline guibg=Red ctermbg=Red
  autocmd VimEnter,WinEnter * match TrailingSpaces /\s\+$/
augroup END