" write dos2unix
" set ff=unix
" fuer vi
" Version: 07.09.2000

" dark background
se background=dark

" disable Highlight Searchpattern -- annoying
se nohlsearch

" automatisches Syntax highlithig
if has("syntax") 
  syntax on
endif

" automatisch einruecken
set autoindent

" suchen case-insenitiv
set ignorecase

" Zeige (line/row) unten rechts
set ruler

" shell to start with !
" set shell=sh

" zeige passende klammern
set showmatch

" We're using vim not vi
set nocompatible

" anzeige INSERT/REPLACE/...
set showmode

" einrueckung
set shiftwidth=3
set tabstop=8

" join lines in insert mode
set backspace=2

" automatical write
set autowrite

" listchars when in ':se list' mode
set listchars=tab:>.,eol:\$,trail:\|

" when a line is wrapped display the following at the beginning
" its not possible to display something where the wrap happens
set showbreak=<-\|

" Override the 'ignorecase' option if the search pattern contains upper
" case characters.
set smartcase

" no autoindent and stuff like that will be performed when in paste mode
" toggle between paste and non-paste mode
set pastetoggle=<C-X>

" saves the orig file as NAME.tibit-vim
map <C-A> :se pm=.tibit-vim<CR>

" ident with <tab>
map <TAB> ==
" comment in/out
"map ci 03x$3x0
"map co I/* <C-C>A */<C-C>0
ab // /* <C-O>$ */<C-C>0


" map home; end
"map [F $
"map [H 0
"imap [F <c-c>A
"imap [H <c-c>I

cnoremap <C-A> <Home>
cnoremap <C-F> <Right>
cnoremap <C-B> <Left>
cnoremap <Esc>b <S-Left>
cnoremap <Esc>f <S-Right>

" Yank from current position to end of line
nmap Y y$

" Delete trailingspace
nmap <M-d> :%s/\s\+$//<CR>
vmap <M-d> :s/\s\+$//<CR>

" Plain text formatting
nmap <M-f> :%!fmt<CR>
vmap <M-f> !fmt<CR>

" Sort buffer
nmap <M-s> :%!sort<CR>
vmap <M-s> !sort<CR>

" Spell check (english)
nmap <M-e> :!spell -x -denglish '%'<CR>
vmap <M-e> !spell -denglish -l<CR>

" Spell check (german)
nmap <M-g> :w<CR>:!ispell -d german -T latin1 -w "δόφΔάΦί" '%' <CR>:e %<CR>
nmap <M-g> :!ispell -d deutsch -w "δόφΔάΦί" '%' <CR><CR>

if has("autocmd")
  " nomodline for mail&news
  au BufNewFile,BufReadPost .followup,snd.*,.letter,.article,.article.[0-9]\+,pico.[0-9]\+,mutt*[0-9] se tw=64 nomodeline
  " special files I don't want autoindent
  au BufNewFile,BufReadPost *quotes*,*jokes*,*interesting* se noautoindent

  " Settings for programming in C
  au BufNewFile,BufReadPost *.c,*.C,*.h,*.cc se cindent shiftwidth=8 tabstop=8 bg=light visualbell
endif

ab MARK /* ****************************************************************** */
ab MSnip ----------------snip--------------------------------snip----------------k
" HTML 

if has("viminfo")
  set viminfo='50,\"1000,:100,%
endif

map <F9> :!cc -Wall -Wstrict-prototypes -pedantic -g -o %:r.exe % <CR>
map <F10> :!c++ -Wall -Wstrict-prototypes -pedantic -g -o %:r.exe % <CR>
map <F11> :!./%:r.exe <CR>
map <F12> :make %:r <CR>

" **************** Functions and their mappings **********************
" don't source Functions for "plain vi"
if has("eval") 
" adjust the quoting marks of current line

function! Quote ()
    let line = getline (line ("."))
    " remove all trailing blanks
    let line = substitute (line, '\s\+$', '', '')
    let level = 0
    " search for the first quoting mark
    " (assuming that WE quoted correctly with a '>'-character)
    let index = matchend (line, '^>')
    while index >= 0
        let level = level + 1
        " just keep the part after the quoting mark
        let line = strpart (line, index, strlen (line))
        " search for the next quoting mark
                                                      "new!
        let index = matchend (line, '^\s*\(\u\u\)\{0,1\}[>|:;]')
    endwhile
    " now all quote marks are away
    if level > 0
        " remove a possible remaining leading blank
        let index = matchend (line, '^ ')
        if index >=0
            let line = strpart (line, index, strlen (line))
        endif
        " rebuild the new line
        let newline = line
        " is there anything left at all?
        if strlen (newline) > 0
            let newline = " " . newline
            while level > 1                           "new!
                let newline = ">" . newline
                let level = level - 1
            endwhile
            let newline = " " . newline               "new!
            let newline = ">" . newline               "new!
            let level = level - 1                     "new!
        endif
	let newline = substitute (newline, "^>\ \ *", ">\ ", "")
        let @x = newline
        execute "normal! D\"xp"
    endif
endfunction

map ,q mq:%call Quote ()<CR>`q

function! Fract (divide, diviso)
  " I don't like writing a:
  let divident = a:divide
  let divisor = a:diviso
  " undefined result, so ..
  if divisor == 0
    return "false"
  endif
  let maxdepth = 10
  let curdepth = 0
  let part = (divident/divisor)
  let str = part . "."
  let divident = (divident - part * divisor)*10
  while (curdepth <= maxdepth)
    let part = (divident/divisor)
    let divident = ( divident - (part * divisor)) * 10
    let str = str . part
    let curdepth = curdepth + 1
  endwhile
  return str
endfunction

function! CountQuote ()
  " find first non-header line
  let linenr = 1
  while getline(linenr) != ""
    let linenr = linenr + 1
  endwhile
  let quotes = 0
  let other = 0
  let quotechars = ">|:"
  let mylinenr = linenr
  let myline = getline(mylinenr)
  let lastlinenr = line("$")
  while mylinenr != lastlinenr
    if match(myline, "[" . quotechars . "]") == -1
      let other = other + 1
    else
      let quotes = quotes + 1
    endif
    " get next line
    let mylinenr = mylinenr + 1
    let myline = getline (mylinenr)
  endwhile
  echo "Quoted lines: " . quotes . "   Other: " . other . "   Aspect: " . Fract(other, quotes)
endfunction

map ,c :call CountQuote ()<CR>

" filenam is the buffer we should insert
function! Blockquote (filenam)
  let buname = bufname(a:filenam)
  " remember current buffer
  let curbuf = bufnr("%")
  " change to buffer specified as argument
  exe "buffer " bufnr(buname)
  let @x=",----[ " . buname . " ]\n"
  let linnr = 1
  let lastln = line("$")
  while linnr <= lastln
    let line = getline(linnr)
    " remove all trailing blanks
    let line = substitute (line, '\s\+$', '', '')
    " put a "| " at beginning of line
    let line = substitute (line, '^', '| ', '')
    let linnr = linnr + 1
    " append register x with line
    let @x = @x . line . "\n"
  endwhile
  let @x = @x . "`----\n"
  " change back to original buffer
  exe "buffer " curbuf
  exe "normal! ^\"xp"
endfunction

" man page in window
function! Miw()
  " save register x
  let save_x = @x
  " yank word under cursor in register x
  execute "normal! \"xyiW"
  let mname = substitute(@x, '\W*\(\w\+\)\((\(\w\+\))\)*.*', '\3 \1', "")
  let wonum = substitute(mname, '.* ', "", "")
  let woname = substitute(mname, ' .*', "", "")
  let inp = input("Section [".woname."]? ")
"  if inp == ""
"    execute "new " . wonum . ".man"
"    " read manpage in buffer; the sed is to remove multible blank lines
"    execute "r !man " . mname . " | col -b | sed '/^\$/{\;N\;/^\\n\$/D\;}'"
"  else
    " read manpage in buffer; the sed is to remove multible blank lines
    execute "new " . wonum . inp . ".man"
    execute "r !man " . inp ." ". wonum . " | col -b | sed '/^\$/{\;N\;/^\\n\$/D\;}'"
"  endif  
  " yank two lines to register x
  execute "normal! 1G\"x2yy"
  " if no manpage, close buffer
  if -1 != match (@x, '\(What manual page do you want?\|No manual entry for\|No entry for\)')
    quit!
    echo "I didn't found anything!"
  endif
  " restore saved register x
  let @x = save_x
endfunction

map ,k :call Miw()<CR><CR><CR>


function! Mgrep ()
  " get the sting under cursor
  let mstring = expand("<cword>")
  let @x = system("grep -n ". mstring . " * 2>/dev/null")

  execute "new GREP\\ results\\ for\\ \\\"".mstring."\\\""

  if 0 == match (@x, '$')
    echo "I didn't found anything!"
    q!
    return
  endif  
  
  execute "normal! \"xP"
  se ro
endfunction

map ,G :call Mgrep()<CR>


function! Mgrep2 ()
  sp
  gr -r <cword> *
endfunction

map ,g :call Mgrep2()<CR>

" these are pretty cool, the let you go to next error or grep result
map <M-n> :cn<CR>
map <M-p> :cp<CR>

function! MDiff()
  let mstring = expand("%")
  let mmstr = "diff -u ". mstring .".tibit-vim  " . mstring
  echo mmstr
  new "Diff"
  let @x = system(mmstr)
  execute "normal! \"xP"
  so $VIMRUNTIME/syntax/diff.vim
  se ro
endfunction

map ,d :call MDiff() <CR><CR>
map <M-w> :quit!<CR>
  
" endif for has("eval")
endif

