set nocompatible " be improved, required
filetype off     " required

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
  Plugin 'gmarik/Vundle.vim'

  " Main group
  Plugin 'Yggdroot/indentLine'
  Plugin 'vim-airline/vim-airline'
  Plugin 'vim-airline/vim-airline-themes'
  Plugin 'gorodinskiy/vim-coloresque'
  Plugin 'bruno-/vim-husk'
  Plugin 'powerman/vim-plugin-ruscmd'
  Plugin 'tpope/vim-repeat'
  Plugin 'tpope/vim-surround'

  " Navigation
  Plugin 'rking/ag.vim'
  Plugin 'corntrace/bufexplorer'
  Plugin 'kien/ctrlp.vim'
  Plugin 'dockyard/vim-easydir'
  Plugin 'scrooloose/nerdtree'

  " Snippets
  Plugin 'tomtom/tlib_vim'
  Plugin 'MarcWeber/vim-addon-mw-utils'
  Plugin 'garbas/vim-snipmate'

  " Programming
  Plugin 'scrooloose/syntastic'
  Plugin 'vim-scripts/tComment'
  Plugin 'jbgutierrez/vim-partial'
  Plugin 'tpope/vim-rsi'
  Plugin 'elzr/vim-json'

  " Vim text objects

  " Markdown
  Plugin 'tpope/vim-markdown'

  " Color schemes
  Plugin 'cocopon/iceberg.vim'
  Plugin 'junegunn/seoul256.vim'

  " Ruby group
  Plugin 'tpope/vim-endwise'
  Plugin 'slim-template/vim-slim'
  Plugin 'tpope/vim-rails'
  Plugin 'vim-ruby/vim-ruby'
  Plugin 'thoughtbot/vim-rspec'
  Plugin 'sunaku/vim-ruby-minitest'

  " tmux group
  Plugin 'jgdavey/tslime.vim'
  Plugin 'jpalardy/vim-slime'

  " js group
  Plugin 'pangloss/vim-javascript'

  " elexir group
  Plugin 'elixir-lang/vim-elixir'

  " coffee group
  Plugin 'kchmck/vim-coffee-script'

  " crystal group
  Plugin 'rhysd/vim-crystal'

  " julia group
  Plugin 'JuliaLang/julia-vim'

  " clojure group
  Plugin 'guns/vim-clojure-static'
  Plugin 'guns/vim-clojure-highlight'
  Plugin 'tpope/vim-fireplace'
  Plugin 'kien/rainbow_parentheses.vim'
  Plugin 'venantius/vim-eastwood'

  " haskell group
  Plugin 'eagletmt/ghcmod-vim'
  Plugin 'eagletmt/neco-ghc'

  " statistic
  Plugin 'wakatime/vim-wakatime'

  " Racket group
  Plugin 'wlangstroth/vim-racket'

  " test plugin functionality

call vundle#end()

" Load plugins and indentation for file types
if has('autocmd')
  filetype indent plugin on

  " Shortcuts to quickly switch to common file types; handy when using
  " editing abstractions like sudoedit(8)
  nnoremap _cs :setlocal filetype=css<CR>
  nnoremap _ht :setlocal filetype=html<CR>
  nnoremap _sl :setlocal filetype=slim<CR>
  nnoremap _js :setlocal filetype=javascript<CR>
  nnoremap _md :setlocal filetype=markdown<CR>
  nnoremap _rb :setlocal filetype=ruby<CR>
  nnoremap _sh :setlocal filetype=sh<CR>
  nnoremap _vi :setlocal filetype=vim<CR>
endif

" Color Scheme
set guifont=Droid\ Sans\ Mono\ 12
let g:seoul256_background = 236
colo seoul256
colo iceberg

" Use relative line numbers
if exists("&relativenumber")
	set relativenumber
	au BufReadPost * set relativenumber
endif

syntax on

set tags=.git/tags

set iskeyword-=. " use dot for limiter word
set iskeyword+=_
set clipboard=unnamed
set ttyfast " u got a fast terminal
set lazyredraw " to avoid scrolling problems
set synmaxcol=200
set mouse=nv
set hidden
set number
set ts=2
set sw=2
set autoindent
set smartindent
set expandtab
set ignorecase
set hlsearch
set splitbelow " new splits are down
set splitright " new vsplits are to the right
set smartcase
set incsearch
set laststatus=2
set visualbell
set showcmd
set pastetoggle=
set iminsert=0
set linebreak
set completeopt=longest,menuone
set wildmenu
set noerrorbells
set novisualbell
set t_vb=
set t_ut=
set tm=500
set nobackup
set nowb
set noswapfile
set timeoutlen=500
set langmenu=en_US.UTF-8
set encoding=utf-8
set fileencoding=utf-8
set shm+=I                     " Startup message is irritating
set dictionary=/usr/share/dict/words

" Time out on key codes but not mappings.
" Basically this makes terminal Vim work sanely.
set notimeout
set ttimeout
set ttimeoutlen=10

if executable('zsh')
  set shell=zsh
endif

language mes en_US.UTF-8

let g:markdown_fenced_languages = ['javascript', 'ruby', 'sh', 'yaml', 'javascript', 'html', 'vim', 'coffee', 'json', 'diff']

let g:tube_terminal = "iterm"

let g:ruby_path = system('rvm current')

let g:airline#extensions#branch#enabled = 1
let g:airline#extensions#branch#empty_message = ''
let g:airline#extensions#syntastic#enabled = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#show_close_button = 0
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#tabline#left_sep = '|'
let g:airline#extensions#tabline#show_buffers = 0
let g:airline_left_sep = ''
let g:airline_right_sep = ''
let g:airline_theme = 'lucius'

let g:bufExplorerDefaultHelp=0
let g:bufExplorerShowRelativePath=1
let g:bufExplorerSortBy='mru'
let g:bufExplorerSplitRight=0

let g:indentLine_color_term = 239
let g:indentLine_char = '¦'

let g:neocomplete#enable_at_startup = 1

let g:ruby_indent_access_modifier_style = 'outdent'
let ruby_operators = 1

let g:surround_{char2nr('%')} = "%(\r)"
let g:surround_{char2nr('w')} = "%w(\r)"
let g:surround_{char2nr('#')} = "#{\r}"
let g:surround_{char2nr('|')} = "|\r|"

let g:syntastic_enable_signs = 1
let g:syntastic_error_symbol = '✗'
let g:syntastic_warning_symbol = '⚠'
let g:syntastic_disabled_filetypes = ['html', 'slim', 'erb', 'md']

" change log settings
let g:changelog_dateformat = "%c"
let g:changelog_timeformat = "%c"
let g:changelog_username = "Anton Davydov, <antondavydov.o@gmail.com>"

" vim-slime
let g:slime_target = 'tmux'

" vim rspec
let g:rspec_command = 'call Send_to_Tmux("rspec {spec}\n")'
map <Leader>t :call RunCurrentSpecFile()<CR>
map <Leader>a :call RunAllSpecs()<CR>

" Disable Ex mode
map Q <Nop>

nmap <space> <leader>
vmap <space> <leader>

nmap <leader>n :noh<CR>

nnoremap <F1> <nop>

nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
nnoremap gj j
nnoremap gk k
vnoremap gj j
vnoremap gk k

" Quicker window movement
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" experimental mappings------------
nnoremap <tab> >>
nnoremap <s-tab> <<
vnoremap <tab> >
vnoremap <s-tab> <

nnoremap th :tabfirst<CR>
nnoremap tj :tabprev<CR>
nnoremap tk :tabnext<CR>
nnoremap tl :tablast<CR>
nnoremap td :tabclose<CR>
nnoremap tn :tabnew<CR>

nnoremap Q <nop>
nnoremap K <nop>

nmap <leader>o o<ESC>
nmap <leader>O O<ESC>j

nmap <leader>< <C-w>10<
nmap <leader>> <C-w>10>
nmap <leader>+ <C-w>10+
nmap <leader>- <C-w>10-

nmap <leader>p  :CtrlPBufTag<CR>
nmap <leader>hh :%s/:\([^ ]*\)\(\s*\)=>/\1:/g<CR>

" Replace mapping
nmap <leader>;  :%s:::g<Left><Left><Left>
nmap <leader>;; :%s:::g<Left><Left>

" vim-partial
vmap <Leader>x :PartialExtract<cr>

" NERDTree plugin
nmap <F2> :NERDTree<cr>

cabbrev CSScomb :!csscomb %:p<CR>
cnoreabbrev <expr> W ((getcmdtype() is# ':' && getcmdline() is# 'W')?('w'):('W'))

" bind K to grep word under cursor
nnoremap <leader>f :grep! "\b<C-R><C-W>\b" --ignore-dir log<CR>:cw<CR>

" Use The Silver Searcher https://github.com/ggreer/the_silver_searcher
if executable('ag')
  " Use Ag over Grep
  set grepprg=ag\ --nogroup\ --nocolor
  " Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
  let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
  " ag is fast enough that CtrlP doesn't need to cache
  let g:ctrlp_use_caching = 0
else
  " Fall back to using git ls-files if Ag is not available
  let g:ctrlp_custom_ignore = '\.git$\|\.hg$\|\.svn$'
  let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . --cached --exclude-standard --others']
endif

function! ClearRegisters()
  let regs='abcdefghijklmnopqrstuvwxyz0123456789'
  let i=0
  while (i<strlen(regs))
    exec 'let @'.regs[i].'=""'
    let i=i+1
  endwhile
endfunction
command! ClearRegisters call ClearRegisters()

" Strip trailing whitespace (,ss)
function! StripWhitespace()
  let save_cursor = getpos(".")
  let old_query = getreg('/')
  :%s/\s\+$//e
  call setpos('.', save_cursor)
  call setreg('/', old_query)
endfunction
noremap <leader>ss :call StripWhitespace()<CR>

" rename current file (thanks Gary Bernhardt)
function! RenameFile()
  let old_name = expand('%')
  let new_name = input('New file name: ', expand('%'), 'file')
  if new_name != '' && new_name != old_name
    exec ':saveas ' . new_name
    exec ':silent !rm ' . old_name
    redraw!
  endif
endfunction
command! RENAME call RenameFile()

" Automatic commands
if has("autocmd")
  " Enable file type detection
  filetype on
  " Treat .json files as .js
  autocmd BufNewFile,BufRead *.json setfiletype json syntax=javascript
  autocmd BufNewFile,BufRead *.md set filetype=markdown
endif

autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1
autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1
autocmd FileType ruby,eruby let g:rubycomplete_rails = 1
autocmd FileType css        set omnifunc=csscomplete#CompleteCSS
autocmd Filetype gitcommit  setlocal spell textwidth=72

au BufNewFile,BufRead,BufWrite *.md syntax match Comment /\%^---\_.\{-}---$/

" Turn on spell-checking in markdown and text.
" au BufRead,BufNewFile *.md,*.txt setlocal spell

" Neovim confixg
if has('nvim')
  let $NVIM_TUI_ENABLE_CURSOR_SHAPE=1
  tnoremap <Esc> <c-\><c-n>
  tnoremap <C-[> <c-\><c-n>

  highlight TermCursor ctermfg=red guifg=red
endif

if exists('$TMUX')
  let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
  let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
else
  let &t_SI = "\<Esc>]50;CursorShape=1\x7"
  let &t_EI = "\<Esc>]50;CursorShape=0\x7"
endif

autocmd BufWritePost * set iskeyword-=.
autocmd BufWritePost * set clipboard=unnamed
autocmd BufWritePost * set path+=**

set iskeyword-=. " use dot for limiter word
set iskeyword+=_
set path+=**
set clipboard=unnamed