" System gvimrc file for Mac OS X
" Author:  Benji Fisher <benji@member.AMS.org>
" Last modified:  10 February 2003

" If you want to get more Mac-like behavior, leave the "finish" line
" commented out.  For vim-standard menus, uncomment this line so that the rest
" of the file will be ignored.

" finish

" TODO:  Decide whether to leave it in non-compatible mode.
" For now, save and restore.
let s:save_cpo = &cpo
set cpo&vim

" Define mac-standard menu items and keyboard shortcuts.

aunmenu &File.&New
amenu 10.325 &File.&New<Tab><D-n> <D-n>
nmap <D-n> :confirm enew<CR>
vmap <D-n> <Esc><D-n>gv
imap <D-n> <C-O><D-n>
cmap <D-n> <C-C><D-n>
omap <D-n> <Esc><D-n>
aunmenu &File.&Open\.\.\.
amenu 10.310 &File.&Open\.\.\.<Tab><D-o> <D-o>
nmap <D-o> :browse confirm e<CR>
vmap <D-o> <Esc><D-o>gv
imap <D-o> <C-O><D-o>
cmap <D-o> <C-C><D-o>
omap <D-o> <Esc><D-o>
aunmenu &File.&Close
amenu 10.330 &File.&Close<Tab><D-w> <D-w>
nmap <silent> <D-w> :if winheight(2) < 0 <Bar>
	\   confirm enew <Bar>
	\ else <Bar>
	\   confirm close <Bar>
	\ endif<CR>
vmap <D-w> <Esc><D-w>gv
imap <D-w> <C-O><D-w>
cmap <D-w> <C-C><D-w>
omap <D-w> <Esc><D-w>
aunmenu &File.&Save
amenu 10.340 &File.&Save<Tab><D-s> <D-s>
nmap <silent> <D-s> :if expand("%") == ""<Bar>browse confirm w<Bar>
	\ else<Bar>confirm w<Bar>endif<CR>
vmap <D-s> <Esc><D-s>gv
imap <D-s> <C-O><D-s>
cmap <D-s> <C-C><D-s>
omap <D-s> <Esc><D-s>
aunmenu &File.Save\ &As\.\.\.
amenu 10.350 &File.Save\ &As\.\.\.<Tab><D-S>	<D-S-s>
nmap <D-S-s> :browse confirm saveas<CR>
vmap <D-S-s> <Esc><D-s>gv
imap <D-S-s> <C-O><D-s>
cmap <D-S-s> <C-C><D-s>
omap <D-S-s> <Esc><D-s>
" map <D-p> ??
" <D-q> is handled by the OS.
"amenu File.Quit<Tab><D-q> <D-q>
"nmap <D-q> :confirm qall<CR>
"vmap <D-q> <Esc><D-q>gv
"imap <D-q> <C-O><D-q>
"cmap <D-q> <C-C><D-q>
"omap <D-q> <Esc><D-q>
" From the Edit menu of SimpleText:
aunmenu &Edit.&Undo
amenu 20.310 &Edit.&Undo<Tab><D-z> <D-z>
nmap <D-z> u
vmap <D-z> <Esc><D-z>gv
imap <D-z> <C-O><D-z>
cmap <D-z> <C-C><D-z>
omap <D-z> <Esc><D-z>
vunmenu &Edit.Cu&t
vmenu 20.340 &Edit.Cu&t<Tab><D-x> <D-x>
vmap <D-x> "+x
vunmenu &Edit.&Copy
vmenu 20.350 &Edit.&Copy<Tab><D-c> <D-c>
vmap <D-c> "+y
cunmenu &Edit.&Copy
cmenu 20.350 &Edit.&Copy<Tab><D-c> <D-c>
cmap <D-c> <C-Y>
aunmenu &Edit.&Paste
nmenu 20.360 &Edit.&Paste<Tab><D-v> <D-v>
nmap <D-v> "+gP
cmenu	     &Edit.&Paste<Tab><D-v> <D-v>
cmap <D-v> <C-R>+
vmenu	     &Edit.&Paste<Tab><D-v> <D-v>
imenu	     &Edit.&Paste<Tab><D-v> <D-v>
if has("virtualedit")
  vmap <D-v> "-c<Esc><SID>Paste
  imap <D-v> <Esc><SID>Pastegi
else
  vmap <D-v> "-c<Esc>gix<Esc><SID>Paste"_x
  imap <D-v> x<Esc><SID>Paste"_s
endif
aunmenu &Edit.&Select\ All
amenu 20.400 &Edit.Select\ &All<Tab><D-a>	<D-a>
nmap <silent> <D-a> :if &slm != ""<Bar>exe ":norm gggH<C-O>G"<Bar>
	\ else<Bar>exe ":norm ggVG"<Bar>endif<CR>
vmap <D-a> <Esc><D-a>
imap <D-a> <Esc><D-a>
cmap <D-a> <C-C><D-a>
omap <D-a> <Esc><D-a>
aunmenu &Edit.&Find
amenu 20.410 &Edit.&Find<Tab><D-f>	<D-f>
nmap <D-f> /
vmap <D-f> <Esc><D-f>
imap <D-f> <Esc><D-f>
cmap <D-f> <C-C><D-f>
omap <D-f> <Esc><D-f>
amenu 20.420 &Edit.&Next<Tab><D-g>	<D-g>
nmap <D-g> n
vmap <D-g> <Esc><D-g>
imap <D-g> <C-O><D-g>
cmap <D-g> <C-C><D-g>
omap <D-g> <Esc><D-g>
vmenu <silent> 20.420 &Edit.Find\ &Selection "9y/<C-R>9<CR>
	\:if strlen(@9) > 1 <Bar>
	\ exe 'norm!v/' . @9 . '/e' . "<Bslash><lt>CR>"<Bar>
	\ else <Bar> exe "norm!v" <Bar> endif<CR>

" Copied from $VIMRUNTIME/menu.vim:
" Pasting blockwise and linewise selections is not possible in Insert and
" Visual mode without the +virtualedit feature.  They are pasted as if they
" were characterwise instead.  Add to that some tricks to leave the cursor in
" the right position, also for "gi".
if has("virtualedit")
  nnoremap <silent> <script> <SID>Paste :call <SID>Paste()<CR>
  func! <SID>Paste()
    let ove = &ve
    set ve=all
    normal `^
    if @+ != ''
      normal "+gP
    endif
    let c = col(".")
    normal i
    if col(".") < c	" compensate for i<ESC> moving the cursor left
      normal l
    endif
    let &ve = ove
  endfunc
else
  nnoremap <silent> <script> <SID>Paste "=@+.'xy'<CR>gPFx"_2x
endif

" Add a Preferences... menu item to the Edit menu.
amenu Edit.-SepPrefs- :
amenu Edit.Preferences\.\.\. :call <SID>Preferences()<CR>

fun! <SID>Preferences()
	let prompt = "Choose one of the following:"
	let prompt = prompt . "\r1 Set options for the current session."
	let prompt = prompt . "\r2 Edit ~/.vimrc for future sessions."
	let prompt = prompt . "\r3 Do 1 and 2, in split windows."
	let prompt = prompt . "\r4 Edit ~/.gvimrc for future sessions."
	let response = inputdialog(prompt, "1")
	if response == 1
		options
	elseif response == 2
		call s:EditVimrc("vimrc")
	elseif response == 3
		call s:EditVimrc()
		options
	elseif response == 4
		call s:EditVimrc("gvimrc")
	else
		echohl WarningMsg
		echo "Sorry:  1 or 2 or 3 or 4 expected."
		echohl None
	endif
endfun

fun! s:EditVimrc(rcfile)
	if filewritable($HOME . "/." . a:rcfile)
		let vimrc = $HOME . "/." . a:rcfile
	elseif filewritable($HOME . "/_" . a:rcfile)
		let vimrc = $HOME . "/_" . a:rcfile
	else
		let prompt = "Create a vimrc file in your home directory:"
		let prompt = prompt . "\r." . a:rcfile . " is the Vim standard."
		let prompt = prompt . "\r_" . a:rcfile . " is visible from Finder."
		let prompt = prompt . "\rLeave blank to cancel:"
		let vimrc = inputdialog(prompt, "." . a:rcfile)
		if vimrc == ""
			return
		endif
		let vimrc = $HOME . "/" . vimrc
		let prompt = "Start with " a:rcfile . "_example.vim ?"
		let template = inputdialog(prompt, "Yes")
	endif
	execute "split" vimrc
	if exists("template") && template ==? "Yes"
		execute "read" $VIMRUNTIME . "/" . a:rcfile . "_example.vim"
		0put='\" Copied from $VIMRUNTIME/' . a:rcfile . '_example.vim'
	endif
endfun

let &cpo = s:save_cpo
