" --MORE-- を表示しない
"set nomore
" showmodeにしていると、PHPの引数表示が上書きされてしまう
set noshowmode
" 検索 -------------------------------------------------------------
set ignorecase
set smartcase
set incsearch
set hlsearch
set wrapscan
set keywordprg=man\ -S\ 2:3\ -a
" vgにより「:grep console」でカレントディレクトリから大文字小文字を区別せず検索できる
set grepprg=vg\ --from-vim
set tags+=*.tags,tags;,file-tags;
" マウス -----------------------------------------------------------
set mouse=
"set ttymouse=xterm

if has('persistent_undo')
  set undodir=~/.vimundo
  let undodir = expand('~/.vimundo')
  if !isdirectory(undodir)
    call mkdir(undodir)
    echomsg "ディレクトリ " . undodir . " を作成しました"
  endif
endif


"=============================================================================
"   マッピング  Mappings
"=============================================================================
" カーソル移動 ----------------------------------------------------- 
noremap <silent> j gj
noremap <silent> k gk
noremap <silent> <C-j> 3j
noremap <silent> <C-k> 3k
nnoremap <silent> w :<C-u>call ForwardWord(1)<CR>
nnoremap <silent> b :<C-u>call BackwardWord(1)<CR>
nnoremap <silent> } :<C-u>call ForwardParagraph()<CR>
onoremap <silent> } :<C-u>call ForwardParagraph()<CR>
xnoremap <silent> } <Esc>:<C-u>call ForwardParagraph()<CR>mzgv`z
nnoremap <silent> [[ :<C-u>let scrolloff_old = &scrolloff<CR>:set scrolloff=6<CR>[[:let &scrolloff = scrolloff_old<CR>
nnoremap <silent> ]] :<C-u>let scrolloff_old = &scrolloff<CR>:set scrolloff=6<CR>]]:let &scrolloff = scrolloff_old<CR>
vnoremap <Esc> <Esc>`<
xnoremap <silent> y y`>
noremap <silent> ) /)\\|;\\|\\./e<CR>:call _RemoveLastSearchHistory()<CR>
noremap <silent> ( /)\\|;\\|\\./e<CR>:call _RemoveLastSearchHistory()<CR>
inoremap <silent><C-a> <Esc>I
inoremap <silent> <expr><C-e> pumvisible() ? "\<C-e>" : "\<End>"
inoremap <silent> <C-@> <C-x><C-o>
inoremap <silent> <C-l> <Esc>g~awgi
vnoremap <silent> <C-l> g~
nnoremap <silent> n :<C-u>call _SearchNext("n")<CR>
nnoremap <silent> N :<C-u>call _SearchNext("N")<CR>
nnoremap <silent> <C-x><C-o> <C-i>

function! _SearchNext(dir)
  let line = line(".")
  let wrapped = 0
  set scrolloff=4
  if a:dir ==# "n"
    try
      exe "normal! " . v:count1 . "n"
    catch /E486/
      call _Echo("ErrorMsg", "E486: Pattern not found: " . @/)
      return
    endtry
    if line(".") < line
      let wrapped = 1
    endif
  else
    try
      exe "normal! " . v:count1 . "N"
    catch /E486/
      call _Echo("ErrorMsg", "E486: Pattern not found: " . @/)
      return
    endtry
    if line(".") > line
      let wrapped = 1
    endif
  endif
  set scrolloff=0
  if wrapped
    call _Echo("WarningMsg", "Search wrapped")
  else
    echo @/
  endif
endfunction


" 編集 -------------------------------------------------------------
" タグを閉じる
" smartindent, cindentによる「#でインデント削除」を無効化する
noremap <silent> # :call _ToggleCommentSelection()<CR>
inoremap <C-z> <C-o>:set paste<CR><C-r>"<C-o>:set nopaste<CR>
inoremap <C-b> <left>
inoremap <C-f> <right>
inoremap <C-d> <Del>
" 選択範囲の単語を置換
xnoremap s y:%s@\V\<<C-R>"\>@@g<Left><Left>
nnoremap <silent> d<CR> :call delete_blank_lines#DeleteBlankLines()<CR>
" <C-u> をアンドゥできるようにする。insert.jax 参照
inoremap <C-u> <C-g>u<C-u>
inoremap <C-w> <C-g>u<C-w>
onoremap <space> /\S<CR>:call _RemoveLastSearchHistory()<CR>
onoremap q /["',.{}()[\]<>]<CR>:call _RemoveLastSearchHistory()<CR>
onoremap s /["']<CR><C-o>:call _RemoveLastSearchHistory()<CR>
nnoremap vis :<C-u>call _SelectString('i')<CR>
nnoremap vas :<C-u>call _SelectString('a')<CR>
nnoremap dis :<C-u>call _SelectString('i')<CR>d
nnoremap das :<C-u>call _SelectString('a')<CR>d
nnoremap cis :<C-u>call _SelectString('i')<CR>c
nnoremap cas :<C-u>call _SelectString('a')<CR>c

" ビジュアルモードで選択する
function! _Visual(start, end)
  call setpos('.', a:end)
  normal! mz
  call setpos('.', a:start)
  normal! v`z
endfunction
nnoremap vs v/["']/e-1<CR>v:nohl<CR>gv
" カレント行、または選択範囲に全角と半角の間にスペースを入れる
nnoremap <silent> <space>J V:InsertSpaceBetweenHankakuZenkaku<CR>
xnoremap <silent> <space>J <Esc>:InsertSpaceBetweenHankakuZenkaku<CR>
nnoremap <C-x>p :<C-u>r ~/tmp/screen-hardcopy.txt<CR>
inoreab shmain <Esc>:%d<CR>:0r~/.vim/_template.sh<CR>:set ft=sh<CR>
inoreab rubymain <Esc>:%d<CR>:0r~/.vim/_template.rb<CR>:set ft=ruby<CR>
"inoremap <C-j> <Esc>/\(break;\\|{\\|}\)\s*$<CR>:noh<CR>o
inoreab date@ <C-r>=strftime("%Y-%m-%d")<CR>
inoreab time@ <C-r>=strftime("%H:%M")<CR>
inoreab datetime@ <C-r>=strftime("%Y-%m-%d %H:%M")<CR>
noreab alerT alert
noreab printF printf
noreab fpf fprintf(stderr,);<Left><Left>
noreab transtate translate
