let &runtimepath=$MFEXT_HOME."/opt/vim/config/vim,".$MFEXT_HOME."/opt/python3_vim/share/vim/vim91"

" Use Vim settings and not vi ones
" (must be first)
set nocompatible

" Do not try to connect to the X server (otherwise vim can be slow in case of  X11Forwarding)
" See https://stackoverflow.com/questions/14635295/vim-takes-a-very-long-time-to-start-up
set clipboard=exclude:.*

" Disable gitgutter if git is not an executable in the path
 if executable('git')
   let g:gitgutter_enabled = 1
 else
   let g:gitgutter_git_executable = '/bin/true'
   let g:gitgutter_enabled = 0
 endif

" pathogen
let g:pathogen_disabled = []
execute pathogen#infect()

let g:ale_emit_conflict_warnings = 0

" Syntax coloring
syntax on

" Encoding
set encoding=utf-8

" file type and indent autodetection
filetype on
filetype plugin indent on

if $TERM == "rxvt-unicode"
  " Style (solarized-dark)
  set background=light
  colorscheme solarized
endif

" Search
set hlsearch "highlight search
set incsearch "incremental searching
set showmatch
set gdefault

"searches are case insensitive... unless they contain at least one capital letter
set ignorecase
set smartcase

" Line numbers
set number
set numberwidth=3

" Misc
set title                   " windows title
"set cursorline              " have a line indicate the cursor location
set ruler                   " show the cursor position all the time
set nostartofline           " Avoid moving cursor to BOL when jumping around
set virtualedit=block       " Let cursor move past the last char in <C-v> mode
set scrolloff=5             " Keep 3 context lines above and below the cursor
set backspace=2             " Allow backspacing over autoindent, EOL, and BOL
set showmatch               " Briefly jump to a paren once it's balanced
set wrap
set textwidth=0             " Prevent VIM from automatically inserting line breaks
set wrapmargin=0            " Prevent VIM from automatically inserting line breaks
set colorcolumn=80
set linebreak               " don't wrap textin the middle of a word
set nolist                  " list disables linebreak
set autoindent              " always set autoindenting on
set smartindent             " use smart indent if there is no indent file
set tabstop=4               " <tab> inserts 4 spaces
set shiftwidth=4            " but an indent level is 2 spaces wide.
set softtabstop=4           " <BS> over an autoindent deletes both spaces.
set expandtab               " Use spaces, not tabs, for autoindent/tab key.
set shiftround              " rounds indent to a multiple of shiftwidth
set matchpairs+=<:>         " show matching <> (html mainly) as well
set foldmethod=indent       " allow us to fold on indents
set foldlevel=99            " don't fold by default
"set mouse=a                 " mouse
"set relativenumber

" leader
let mapleader = ","
"nnoremap <leader>a :Ack
nnoremap <leader>v V` " reselect the text that was just pasted
nnoremap <leader>w <C-w>v<C-w>l " split a vertical split and switch over to it
nnoremap <leader>s <C-w>s<C-w>j " split a horizontal split and switch over to it
nnoremap <leader>h <C-w>h
nnoremap <leader>l <C-w>l
nnoremap <leader>j <C-w>j
nnoremap <leader>k <C-w>k
nnoremap <leader>q <C-w>q

" don't outdent hashes
inoremap # #

" ctrlp
set runtimepath^=~/.vim/bundle/ctrlp.vim
let g:ctrlp_map = '<leader>p'
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_working_path_mode = 'ra'
let g:ctrlp_root_markers = ['.ctrlp', '.git', '.directories', '.gitignore']
set wildignore+=*.pyc,*.pyo,*.egg-info,*.o,*.so,*.a,*.egg
let g:ctrlp_custom_ignore = {
  \ 'dir': '\/build\/'
  \ }

" airline
set laststatus=2
if $TERM == "rxvt-unicode"
  let g:airline_theme='solarized'
endif
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_nr_show = 1
let g:airline#extensions#syntastic#enabled = 1
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
nnoremap <leader>c <Esc>:SyntasticToggleMode<CR>:SyntasticCheck<CR>
let g:syntastic_mode_map = { 'mode': 'passive' }


" vim-markdown
let g:vim_markdown_folding_disabled=1

" ack
set grepprg=ack
let g:grep_cmd_opts = '--noheading'

" vimdiff wrap
" http://stackoverflow.com/questions/16840433/forcing-vimdiff-to-wrap-lines
autocmd FilterWritePre * if &diff | setlocal wrap< | endif

let g:ale_sign_column_always = 1
let g:black_linelength = 79
let g:ale_python_pylint_options = "--errors-only"
let g:ale_python_flake8_args = "--ignore D100,D101,D102,D103,D104,D105,D107,W503"

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
map <leader>e :NERDTreeToggle<CR>
map <leader>t :TagbarToggle<CR>
map <leader>b :Black<CR>
autocmd FileType python setlocal omnifunc=jedi#completions
let g:jedi#popup_on_dot = 0
let g:jedi#show_call_signatures = 0
let g:jedi#usages_command = "<leader>n"
let g:jedi#documentation_command = "<leader>x"
let g:jedi#goto_command = "<leader>d"
let g:jedi#usages_command = "<leader>u"
" Using <C-b> for omnicompletion
inoremap <silent> <buffer> <C-b> <c-x><c-o>

fun! <SID>StripTrailingWhitespaces()
    let l = line(".")
    let c = col(".")
    %s/\s\+$//e
    call cursor(l, c)
endfun
fun! <SID>TrimEndLines()
     let save_cursor = getpos(".")
     :silent! %s#\($\n\s*\)\+\%$##
     call setpos('.', save_cursor)
endfun
fun! <SID>RemoveNBSpaces()
    let l = line(".")
    let c = col(".")
    silent! %s/\%u00A0/ /g
    call cursor(l, c)
endfun

let g:syntastic_c_checkers=['make']
let g:syntastic_check_on_open = 0
let g:syntastic_check_on_wq = 0


autocmd FileType * autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()
autocmd FileType * autocmd BufWritePre <buffer> :call <SID>TrimEndLines()
autocmd FileType * autocmd BufWritePre <buffer> :call <SID>RemoveNBSpaces()
autocmd BufRead,BufNewFile *.md setlocal spell spelllang=en_us
set complete+=kspell

" disable modifyOtherKeys
" https://github.com/metwork-framework/mfextaddon_vim/issues/63
let &t_TI = ""
let &t_TE = ""

" disable keyprotocol
" https://github.com/metwork-framework/mfextaddon_vim/issues/72
set keyprotocol=
let &term = &term

