" _vimrc -- my vimrc as of 2006
" @Author:      Jose Quesada (mailto:quesadaj@gmail.com)
" @Website:     http://www.andrew.cmu.edu/~jquesada
" @License:     GPL: http://www.gnu.org/copyleft/gpl.html
" @Created:     05-Apr-2006.
" @Last Change: 05-Apr-2006.

" To use it, copy it to
"     for Unix and OS/2:  ~/.vimrc
"   for MS-DOS and Win32:  $VIM\_vimrc

"""""""""""""""""""""""""""""""""""""""""""""""""""
" settings
"""""""""""""""""""""""""""""""""""""""""""""""""""
" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set textwidth=80        " 80 columns wide text is good

set nocompatible
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time
set showcmd             " display incomplete commands
set showmode            " display the mode you are in everytime
set mouse=a             " mouse works in all modes
set incsearch           " do incremental searching
set showmatch           " balancing brackets like in emacs
set wrap!               " linewrapping off
set ignorecase          " evident
set nostartofline       "to keep the cursor at the same horizontal location when
"set expandtab          " Expand <Tab> to spaces in Insert mode
set tabstop=3           " number of spaces that a <Tab> in the file counts for
set laststatus=1        " show status line only if there are more than one windows
set shiftwidth=3        " size of tabs by default
set backup              "  backUps!!
set title               "see filename in Xterm title
set guifont=Fixedsys:h11:w6 "enlarge fonts for high resolutions!
imap <F10> <esc>/\%81c   " Highlight all characters at column #81 (all longer than 80 characters)
imap ZZ <esc>:wq!<cr>   " ZZ saves and leaves as well in insert mode
let Tlist_Ctags_Cmd = '/usr/local/bin/ctags' "  CTAGS

set wildignore=*.o,*.obj,*.bak,*.exe

" configure TOhtml
" NOTE: it interaracts with TSkeleton, it doesn't work for now
let html_use_css =1

" noremap ;; ;
" map ; :
" " edit command-line
" map q; q:
"
" use a single dir for swapfiles
" set dir=e:\\jose\\swapFileVim

" reload docs
" helptags $VIM/vimfiles/doc

" WIKIFIED behavior
" move in CamelCaseWords from C to W e.g. using c-arrows

nnoremap <silent><C-Left>  :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
nnoremap <silent><C-Right> :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
inoremap <silent><C-Left>  <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
inoremap <silent><C-Right> <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>


" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
" outline
"" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

" do not work for me
" Alt-shift-keyboardPlus  open current fold                  *tvo-<M-S-kPlus>*
"  Alt-shift-keyboardMinus close current fold

   " defaults:
   let no_otl_maps=0
   let no_otl_insert_maps=0

   " no bold headers
   let otl_bold_headers=0
   " support viki (learn)
" enable local links to other files, easily tranformed into html
" requires viki.vim
" for outlines mainly; any other file ok too since it's minor
" autocmd FileType otl      call VikiMinorMode(1)
   let otl_use_viki=1

   " leave and enter in the position we left, with folding, etc remembered
   au BufWinLeave *.otl mkview
   au BufWinEnter *.otl silent loadview
   let maplocalleader = ","

" TODO get highlighting for R, test tcomments

" Use CTRL-T for opening new tabs
noremap     <C-T>               :tabnew<CR>
vnoremap    <C-T>               <C-C>:tabnew<CR>
inoremap    <C-T>               <C-O>:tabnew<CR>

" ALTERNATIVE USES OF TAB
" idea from emacs: tab anywhere in the line does esc + V + =
" TODO

" tab completion
" if at the begining of the line, tab, otherwise completion c-p (nice !)
function InsertTabWrapper()
      let col = col('.') - 1
      if !col || getline('.')[col - 1] !~ '\k'
          return "\<tab>"
      else
          return "\<c-p>"
      endif
endfunction

"then define the appropriate mapping:
inoremap <tab> <c-r>=InsertTabWrapper()<cr>


" reload personal list of spelling
":mkspell C:/Program\ Files/Vim/vimfiles/spell/en.latin1.add

" note: create unique list from telefonos.txt and stick it in my personal
" speller
"
" activate spelling
""""""""""""""""""""""""""""
if has("spell")
  " turn spelling on by default
  set spell

  " toggle spelling with F4 key
  map <F4> :set spell!<CR><Bar>:echo "Spell Check: " . strpart("OffOn", 3 * &spell, 3)<CR>

  " they were using white on white
  highlight PmenuSel ctermfg=black ctermbg=lightgray

  " limit it to just the top 10 items
  set sps=best,10
endif

" activate spelling
map <F4> :set spell!<CR><Bar>:echo "Spell Check: " . strpart("OffOn", 3 * &spell, 3)<CR>

" highlight the current line
if version >= 700
   set cursorline
   "set cursorcolumn
endif

" fundamental to interact with other programs:
" insert hard \r in the long lines; this is done in visual mode by gq

" and the reverse (to spit into word easily, for example)
map <F6> :s/\n/ /g<CR>

" this is to work with extremely long lines, (small fonts, no wrap)

function! Smallfonts()
   setlocal textwidth=0
   set guifont=Andale_Mono:h7:cANSI
   set columns=196
   set lines=62
endfunction

map <F5> : call Smallfonts()<CR>

" tip: load actionscript syntax highlighting
" source c:/Program\ Files/vim/vimfiles\syntax\actionscript.vim
au BufNewFile,BufRead *.as setf actionscript
""""""""""""""""""""""""""""""""""""""""""""""""""""
" syntax highligting prefs. and keyboard shortcuts
""""""""""""""""""""""""""""""""""""""""""""""""""""
map <S-tab> :b #<cr>                         " !!alternate buffers


" CTRL R calculates math operations
ino <C-E> <C-O>yiW<End>=<C-R>=<C-R>0<CR>

" dont see ^M again
autocmd BufRead * silent! %s/[\r \t]\+$//

" search for more than one word, selection using visual
vmap <silent> g/    y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>

"  MAKE IT EASY TO UPDATE/RELOAD_vimrc
if has ("win32")
   map ,s :source c:/Program\ Files/vim/_vimrc<CR>
   map ,v :e c:/Program\ Files/vim/_vimrc<CR>
   set backupdir =c:/Program\ Files/vim/backup "where to put backup files, default in the same di
   colors peachpuff
   set guioptions-=T
   set guicursor+=a:blinkon0 "prevent cursor blinking
else

   map ,v :e $HOME/.vimrc                       "  MAKE   IT EASY TO UPDATE/RELOAD_
   map ,h :so $VIMRUNTIME/syntax/hitest.vim     " test syntax
   set backupdir =~/vim62/backup "where to put backup files, default in the same di
endif
 " ths
 " Use CTRL-D for saving, also in Insert mode
noremap <C-D>           :update<CR>
vnoremap <C-D>          <C-C>:update<CR>
inoremap <C-D>          <C-O>:update<CR>

" TIP to export the syntax highlighting to html, run in command mode:
" :runtime! syntax/2html.vim

" Note abbreviations for time
iab Cdate <C-R>=strftime(" %d.%m.%y - %H")<CR>
iab _lm  <C-R>=" Last Modified: "  . strftime( " %a %b %d %H:%M:%S %Y" )<CR>

" make arrow keys work in visual mode
vmap <ESC>OA k
vmap <ESC>OB j
vmap <ESC>OC l
vmap <ESC>OD h
" simulate shift-arrows (select block in windows) with control-arrows
inoremap <ESC>[A <C-O>vk
vnoremap <ESC>[A k
inoremap <ESC>[B <C-O>vj
vnoremap <ESC>[B j
inoremap <ESC>[C <C-O>vl
vnoremap <ESC>[C l
inoremap <ESC>[D <C-O>vh
vnoremap <ESC>[D h
""""""""""
"  vim editor itself
"""""""""""
highlight lineNr ctermfg=black
"""""""""
"  c highlighting stuff
""""""""""
autocmd FileType c highlight Statement ctermfg=red
autocmd FileType c highlight Comment ctermfg=darkGrey
autocmd FileType c highlight String ctermfg=green
""""""""""
" vim highlighting Stuff
""""""""""
highlight vimCommand ctermfg=red
highlight vimComment ctermfg=darkGrey
highlight vimOper ctermfg=black
""""""""""
" Perl Stuff
""""""""""
" MAKE
" use CTRL+F to make
" use CTRL+E to go to next error
" ues CTRL+D to save
" ALL THOSE KEYS ARE IN VERY CLOSE AND CONVENIENT LOCATIONS IF YOU ARE
" LEFT-HANDLED

"  better highlighting
let perl_want_scope_in_variables=1
let perl_extended_vars=1
let perl_include_pod=1


""""""""""
" Perl Stuff
""""""""""
"  Better highlight for perl comments

set makeprg= "E:/Program\ Files/vim/vim62/compiler/vimparse.pl\ %\ $*"
set errorformat=%f:%l:%m


" activate folding for perl
if version >= 600
        let perl_fold = 1
        " let perl_fold_blocks = 1
endif
"---------------------------------------

""""""""""""
" important behave like windows
""""""""""""
source $VIMRUNTIME/mswin.vim

" Comments
 " very simple commentify; for fancier stuff use TComment (mapped as toggle for
 " <c-_><c-_>.
 " --------------------"

function! SemicolonComment()
map - :s/^/\; /<CR>
map _ :s/^\s*\; \=//<CR>
set comments=:\;
endfunction
autocmd FileType ahk    call SemicolonComment() " TCommentDefineType('ahk', '; %s') doesn't work


" -------------------------------------------------
" generic config (came with vimrc_example)
" -------------------------------------------------


" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
  finish
endif

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

if has("vms")
   set nobackup      " do not keep a backup file, use versions instead
else
   set backup     " keep a backup file
endif

" Don't use Ex mode, use Q for formatting
map Q gq




" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
   syntax on
   set hlsearch
   colors peachpuff
   set guifont=Fixedsys:h11:w6
   set guioptions-=T "remove the toolbar, it is useless
   set guicursor+=a:blinkon0 "prevent cursor blinking
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

   " Enable file type detection.
   " Use the default filetype settings, so that mail gets 'tw' set to 72,
   " 'cindent' is on in C files, etc.
   " Also load indent files, to automatically do language-dependent indenting.
   filetype plugin indent on

   autocmd BufEnter * :syntax sync fromstart "  ensure every file does syntax hi
   autocmd BufEnter * :lcd %:p:h "  switch to current dir

   " Put these in an autocmd group, so that we can delete them easily.
   augroup vimrcEx
      au!

      " For all text files set 'textwidth' to 78 characters.
      autocmd FileType text setlocal textwidth=80

      " When editing a file, always jump to the last known cursor position.
      " Don't do it when the position is invalid or when inside an event handler
      " (happens when dropping a file on gvim).
      autocmd BufReadPost *
          \ if line("'\"") > 0 && line("'\"") <= line("$") |
          \   exe "normal g`\"" |
          \ endif

   augroup END
else
   " Copy indent from current line when starting a new line (typing <CR>
   set autoindent    " always set autoindenting on
endif " has("autocmd")

"-------------------------------------------------------------------------------
" tskeleton support
" makes headers in comments for files *.r *.txt with author, etc
" it also has some code templates a la
"-------------------------------------------------------------------------------
" the function TSkeletonIncreaseRevisionNumber is suspect of creating trouble
" with otl and others
"    autocmd BufWritePre * call TSkeletonIncreaseRevisionNumber()
let g:tskelUserName        = "Jose Quesada"
let g:tskelUserEmail       = "quesada AT gmail.com"
let g:tskelUserWWW         = "http://www.andrew.cmu.edu/~jquesada"
let g:tskelLicense         = "GPL: http://www.gnu.org/copyleft/gpl.html"


let g:tskelDir = 'C:/Program Files/Vim/vimfiles/skeletons/' "
autocmd BufNewFile *.r TSkeletonSetup r.r
"autocmd BufNewFile *.otl TSkeletonSetup outline.otl
autocmd BufNewFile *.rb TSkeletonSetup ruby.rb

" TODO try snippetsEmu.vim. it seems to be an emulation of textMate (osX)

"-------------------------------------------------------------------------------
" perl-support.vim
"-------------------------------------------------------------------------------

let g:Perl_AuthorName      = "Jose Quesada"
let g:Perl_AuthorRef       = ""
let g:Perl_Email           = "quesada AT gmail.com"
let g:Perl_Company         = "Sussex University"
let g:Perl_Project         = ""
let g:Perl_CopyrightHolder = "Jose Quesada"

let g:Perl_Dictionary_File = $HOME."/.vim/wordlists/perl.list"


" ----------  Insert header into new PERL files  ----------
" ----------  Insert header into new PERL files  ----------
if has("autocmd")
  autocmd BufNewFile  *.\(pl\|pm\)         call Perl_CommentTemplates('header')
endif " has("autocmd")

" Window resizing mappings /*{{{*/
nnoremap <S-Up>     :normal <c-r>=Resize('+')<cr><cr>
nnoremap <S-Down>   :normal <c-r>=Resize('-')<cr><cr>
nnoremap <S-Left>   :normal <c-r>=Resize('<')<cr><cr>
nnoremap <S-Right>  :normal <c-r>=Resize('>')<cr><cr>

" Map F6 for gvim window resizing
" Put this snippet of code in you .vimrc for nice window resizing.
" Press F1 key to toggle between the three settings .

nmap <F6>    :call ResizeWindow()<CR>
imap <F6>    <ESC><F6>a   " for insert mode

function! ResizeWindow()
    if (has("gui"))
        if s:selectedsize == 1
            let s:selectedsize = 2
    set nonumber
            set columns=88  " 88 is exactly 80 with :set number
            set lines=35
        elseif s:selectedsize == 2
    set nonumber
            let s:selectedsize = 3
            set columns=98
            set lines=45
        else  " old school console goodness
            let s:selectedsize = 1
    set nonumber
            set columns=80
            set lines=25
        endif
    endif
endfunction
let s:selectedsize=2
call ResizeWindow()