" file:~/.vimrc

"Things that need doing early on
filetype off "for vundle
set nocompatible

set nobackup

"OS Specific
let os = substitute(system('uname'), "\n", "" ,"") "thanks to http://objectmix.com/editors/149466-operating-system-checking-vimrc-files.html#post517594
if os == "Linux"
set guifont=Inconsolata\ Medium\ 10
let twitvim_enable_python = 1
"let twitvim_browser_cmd = '/usr/bin/firefox'
source $VIMRUNTIME/mswin.vim
behave mswin
set clipboard=unnamedplus "Makes ctrl+x actually work with ctrl+v
"Don't have to worry about this at the mo', but want it for reference
"let dns = match(system('grep nameserver /etc/resolv.conf'), "217.169.20")
let g:HAMMER_BROWSER = "firefox"
elseif os == "Darwin"
set clipboard=unnamed
set guifont=Inconsolata:h14
let twitvim_enable_ruby = 1
let twitvim_browser_cmd = "open" "Also, note to self, it's \g to open on OSX
"let dns = match(system('grep nameserver /etc/resolv.conf'), "217.169.20")
elseif os == "NetBSD"
"let dns = 1
"No clipboard on NetBSD
"Since I'm on Linode!
"Hmm, now not always, might need dns/ip specific stuff here
else "Assume windows as uname doesn't work on Windows
let os = "Windows"
set guifont=Consolas:h10 "Nice font Use Inconsolata:h13 on OSX, Linux.
set guifontwide=MingLiU:h10 "For windows to display mixed character sets
"let Tlist_Ctags_Cmd="\"C:\\Program Files\\ctags58\\ctags.exe\"" "Windows only at mo' also not using at the mo'
map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR> "Windows only, fullscreen Vim
source $VIMRUNTIME/mswin.vim
behave mswin
set clipboard=unnamed "Makes ctrl+x actually work with ctrl+v
"let dns = match(system('ipconfig'), "217.169.20")
set rtp+=~/vimfiles/bundle/Vundle.vim/
let path='~/vimfiles/bundle'
call vundle#begin(path)
"From here: http://stackoverflow.com/questions/4331776/change-vim-swap-backup-undo-file-name
if isdirectory($HOME . '/vimfiles/swap') == 0
:silent !mkdir "\%HOME\%/vimfiles/swap"
set dir=~/vimfiles/swap//
"Need another clause here for anything not Widnows
if os != "Windows"
set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#begin()
"From here: http://stackoverflow.com/questions/4331776/change-vim-swap-backup-undo-file-name
if isdirectory($HOME . '/.vim/swap') == 0
:silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
set dir=~/.vim/swap//

"IP Address specific
"(i.e. Home or Work)
"if dns == -1
" "Set proxies, Python urllib2 (TwitVim, Simplenote.vim) will automatically pick
""these up
" if os == "Windows"
" source ~/_vimproxies
" "note to self, format is: let $http_proxy = 'http://<user>:<password>@proxy:port'
" else
" " At the minute no proxy crap for new work
" "source ~/.vimproxies
" endif

" let Vundle manage Vundle
" required!
Bundle 'gmarik/Vundle.vim'
" original repos on github
Bundle 'scrooloose/nerdtree'
Bundle 'scrooloose/nerdcommenter'
Bundle 'tpope/vim-surround'
Bundle 'tpope/vim-fugitive'
Bundle 'altercation/vim-colors-solarized'
Bundle 'mrtazz/simplenote.vim'
"This is pointless as only uses master:
"Plugin 'file:///home/simon/Code/github/mrtazz/simplenote.vim'
Bundle 'Raimondi/delimitMate'
Bundle 'matthias-guenther/hammer.vim'
Bundle 'airblade/vim-gitgutter'
Bundle 'junegunn/vader.vim'
Bundle 'mtvee/vim-fossil'
Bundle 'elixir-lang/vim-elixir'
Bundle 'kien/ctrlp.vim'
"Bundle 'vim-airline/vim-airline'
Bundle 'vim-syntastic/syntastic'
Bundle 'fatih/vim-go'
"Vim Scripts repos
Bundle 'TwitVim'
Bundle 'EasyGrep'
Bundle 'matchit.zip'
"Things I'm not using at the moment
"Bundle 'Vim-R-plugin'
"Bundle 'jcf/vim-latex'
"Bundle 'kchmck/vim-coffee-script'
"Bundle 'sukima/xmledit'
"Not using the below as I think I'll develop bad habits. I.e. I still need to use Vi a lot.
"Bundle 'Lokaltog/vim-easymotion'
"Bundle 'mattn/webapi-vim'
"Bundle 'mattn/gist-vim'
"Similar for completion stuff. My problem is not being able to complete stuff quickly
"Bundle 'garbas/vim-snipmate'
"Bundle 'MarcWeber/vim-addon-mw-utils'
"Bundle 'tomtom/tlib_vim'
"Bundle 'snipmate-snippets'
"Bundle 'netrw.vim'
"Other repos
"Bundle 'git://notmuchmail.org/git/notmuch', {'rtp':'vim/'}

if os == "Windows"
call vundle#config#require(g:bundles)
call vundle#end()

"Plugin options
let vimrplugin_term = "st"
let g:tex_flavor='latex'
autocmd BufWritePost *.coffee silent CoffeeMake! -b | copen "let coffee_compile_on_save = 1
let g:SimplenoteVertical = 1
let g:SimplenoteNoteFormat = "%F | %N%>[%T] [%D]"
let g:SimplenoteStrftime = "%a, %b %d %Y %H:%M:%S"
let g:SimplenoteSingleWindow = 1
let g:SimplenoteSortOrder = "pinned, modifydate"
"let g:SimplenoteListSize = 30
"let g:SimplenoteFiletype = "ruby"
let twitvim_force_ssl = 1
set background=dark
colorscheme solarized
let g:solarized_termcolors=16
"For putty:
let g:solarized_termtrans=1
let NERDTreeChDirMode=2

"Cross platform
set backspace=2 " make backspace work like most other apps
syntax on
filetype plugin indent on
set tabstop=4 "Number of spaces that a tab character takes
set softtabstop=4 "after reading the docs, I still don't get all the tab options.
set noexpandtab "I don't want tab characters converted to spaces
set shiftwidth=4 "number of spaces used for each step of auto-indent
set smarttab autoindent "copy indent from current line when starting new line
set listchars=tab:>-,eol:$,trail:.,extends:# "From here: http://www.iovene.com/61/
set list
set enc=utf-8 "Whatever
set number "Turn on line numbers
set numberwidth=5 "Set width of line numbers
highlight LineNr guifg=white guibg=lightgray "Line number colours
"Affects git gutter
highlight clear SignColumn
set wrap "Default to line wrapping
set showtabline=2 "Always show the tab bar in the gui
set incsearch "Search options
set ignorecase
set smartcase
set hlsearch
set wrapscan
set guioptions-=T "remove toolbar
"File encoding in status line: http://vim.wikia.com/wiki/Show_fileencoding_and_bomb_in_the_status_line
set statusline=[%{fugitive#statusline()}]\ [ENCODING=%{(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\",B\":\"\")}]\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ [LINELEN=%{strlen(getline('.'))}]
set laststatus=2
let @/ = "" "Clear any previous search http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting/657484#657484
set fileformats=unix,dos "Let's try to always be unix.
set fileformat=unix
set hidden
set formatoptions+=l "http://stackoverflow.com/questions/744159/word-wrap-in-gvim/744220#744220
set lbr
set modeline
set modelines=2
set wildmenu
set virtualedit=block
set splitbelow
set splitright
set mouse=a
"For better editing performance via ssh.
"let g:loaded_matchparen=1
"Actually, load it and toggle with NoMatchParen/DoMatchParen

"Gvim only
if has("gui_running") "http://vim.1045645.n5.nabble.com/vimrc-options-only-if-running-gvim-td4262009.html
set autochdir

"Keyboard re-mapping, etc
"Basically nowt here
""Since redefining esc is risky, do this instead
inoremap ii <Esc>l
"Clear highlighted search
nmap <silent> <leader>/ :nohlsearch<CR>
nmap gn :tabnew<CR>
nmap gb :bnext<CR>
nmap gB :bprevious<CR>
nmap <C-w><Bar> :vsplit<CR>
nmap <C-w>- :split<CR>
"Below from http://danielmiessler.com/study/vim/
vnoremap . :norm.<CR>
"wrapped movements
nnoremap j gj
nnoremap k gk
nnoremap 0 g0
nnoremap $ g$
nnoremap gj j
nnoremap gk k
nnoremap g0 0
nnoremap g$ $
vnoremap j gj
vnoremap k gk
vnoremap 0 g0
vnoremap $ g$
vnoremap gj j
vnoremap gk k
vnoremap g0 0
vnoremap g$ $
" formd Markdown shortcuts
nmap <leader>fr :%! python2.7 ~/Code/github/drbunsen/formd/formd -r<CR>
nmap <leader>fi :%! python2.7 ~/Code/github/drbunsen/formd/formd -i<CR>"

"Define function to read passwords from .netrc
function! GetCredFromNetrc(machine)
let filename = $HOME.'/.netrc'
let login = ""
let password = ""
if filereadable(filename)
let lines = readfile(filename)
let i = match(lines, "machine ".a:machine)
if lines[i] =~ "login"
"get username and password"
let login = split(split(line[i], "login")[-1], " ")[0]
let password = split(split(lines[i], "password")[-1], " ")[0]
"Assume on next two lines. It will be for me
let login = split(split(lines[i+1], "login")[-1], " ")[0]
let password = split(split(lines[i+2], "password")[-1], " ")[0]
return [login, password]

let g:SimplenoteUsername = GetCredFromNetrc("simple-note.appspot.com")[0]
let g:SimplenotePassword = GetCredFromNetrc("simple-note.appspot.com")[1]

"Custom Functions
"Delete hidden buffers
function! DeleteInactiveBufs()
"From tabpagebuflist help, get a list of all buffers in all tabs
let tablist = []
for i in range(tabpagenr('$'))
call extend(tablist, tabpagebuflist(i + 1))
"Below originally inspired by Hara Krishna Dara and Keith Roberts
let nWipeouts = 0
for i in range(1, bufnr('$'))
if bufexists(i) && !getbufvar(i,"&mod") && index(tablist, i) == -1
"bufno exists AND isn't modified AND isn't in the list of buffers open in windows and tabs
silent exec 'bwipeout' i
let nWipeouts = nWipeouts + 1
echomsg nWipeouts . ' buffer(s) wiped out'
command! Bdi :call DeleteInactiveBufs()

function! IndentOff()
setlocal noautoindent
setlocal nocindent
setlocal nosmartindent
setlocal indentexpr=
command! IndentOff :call IndentOff()

function! ClearProxies() "When not at work or home
"unlet is the proper way
unlet $http_proxy
unlet $https_proxy
command! ClearProxies :call ClearProxies()

function! FormatJSON()
:%!python -m json.tool
command! FormatJSON :call FormatJSON()

function! SmileQIF()
"Delete any blank lines
silent! g/^$/d
"If previous statement reverse the lines
if getline(1)=~"BROUGHT FORWARD"
let linenum = 1
while linenum <= line("$")
let curr_line = getline(linenum)
if curr_line=~"BROUGHT FORWARD" || curr_line=~"LAST STATEMENT"
execute linenum 'delete _'
let linenum = linenum - 1
let parts = split(curr_line, "\t")
let new_line = "D".tlib#string#Strip(tlib#string#Strip(parts[0]))."\t"
if match(parts[2], "£") < 0
"safer than checking for blanks as sometimes it's odd whitespace
let new_line = new_line."T-".strpart(tlib#string#Strip(parts[3]),2)."\t"
let new_line = new_line."T".strpart(tlib#string#Strip(parts[2]),2)."\t"
let new_line = new_line."P".tlib#string#Strip(tlib#string#Strip(parts[1]))."\t\^"
call setline(linenum, new_line)
let linenum = linenum + 1
"Insert Header
let header = "!Type:Bank\t".getline(1)
call setline(1, header)
"Replace tabs with new lines
command! SmileQIF :call SmileQIF()

"Custom highlighting

"From http://www.codeography.com/2010/02/20/making-vim-play-nice-with-jekylls-yaml-front-matter.html
autocmd BufNewFile,BufRead /home/simon/Code/local/atomicules.co.uk/* syntax match jekyllYamlFrontmatter /\%^---\_.\{-}---$/ contains=@Spell

"Custom javac make settings as needed
"function! javac()

" substitute(system('ls ./lib/*'), "\n", ";", "g")
"Notes to self
"Turning AutoClose on or off: AutoCloseOn, etc
"Using pygments
"On whole file:
":%!pygmentize -f html -l ruby -O noclasses,style=colorful,linenos=1
"As that does inline, which is probably what I want.
"On visual selection. Just make selection, then do : and write the
"!pygmentize bit
"Using gpg is same deal. No real need for plugins
":%!gpg -d
":%!gpg -ear who@where.com
"Foldmarker for Sweave code chunks
"And same again for xmllint
":% !xmllint % --format
"Read a differt version of a file from git
":r !git show simperium-using-lib:simplenote/simplenote.py
"Formatting json
":%!python -m json.tool