.vimrc

Vimのマイ設定
プラグインっぽいのも中に書いちゃってる。

"-------------------------------------------------------------------------------
" 一般設定
"-------------------------------------------------------------------------------

" マウスのモード
behave mswin

" 非互換モード
set nocompatible

" シェル
set shell=bash.exe\ -i
set noshellslash

" 改行コード
set fileformat=unix
set fileformats=unix,dos,mac

" 行数表示
set number

" インデント設定
set shiftwidth=2
set tabstop=2
set noexpandtab

" 検索結果をハイライト
set hlsearch

" 下の表示
set statusline=%<[%n]%m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).':'.&ff.']'}%y\ %F%=[%{GetB()}]\ %l,%c%V%8P

" 括弧の対応表示
set showmatch

" 自動インデント
set cindent
set autoindent

" バックスペース設定 (インデントやeolを消せるようにする)
set backspace=indent,eol,start

" set list 時の表示
set listchars=tab:>-

" IMをデフォでは働かせないように
set iminsert=0
set imsearch=0

" 常にステータスライン表示
set laststatus=2

" バックアップする
set backup
set backupdir=~/vimfiles/backup/

" 選択モード
set selectmode=

" 保存していないバッファを隠せるようにする
set hidden

" スワップディレクトリ
set directory=~/vimfiles/swap/

" 補完設定
" set complete=.,w,b,u,t,k,U
set complete=.,b,w,u,U,k

" ファイルタイプ検出をオン
filetype plugin indent on

" シンタックス有効
syntax on

" ホームディレクトリに移動
cd ~/

" 入力中のコマンドをステータスに表示する
set showcmd

" 検索文字列が小文字の場合は大文字小文字を区別なく検索する
set ignorecase
set smartcase

" インクリメンタルサーチ
set incsearch

" コマンドラインの行数
set cmdheight=1

" <C-x> <C-a> で使う基数
set nrformats=hex

" 背景色を教える
set background=dark

" スクロール余白
set scrolloff=0

" クリップボード *レジスタ
" set clipboard=unnamed

" ファイルが外部から変更されたときに自動でロードしない
set noautoread

" カレント .vimrc, .exrc などを読まない
set noexrc

" 数値表現から8進数を除く (C-a, C-x)
set nrformats-=octal

" vim7
if v:version >= 700
	" タブを常に表示
	set showtabline=2

	" 自動CD
	set autochdir
endif


"-------------------------------------------------------------------------------
" プラグイン設定
"-------------------------------------------------------------------------------

" 履歴
let MRU_Max_Entries = 30

" ツリー
let g:treeExplVertical = 1
let g:treeExplWinSize = 30

" メモ取り
let g:howm_dir = "~/vimfiles/data/howm/"
" let g:howm_grepprg = "/usr/bin/grep"
" let g:howm_findprg = "/usr/bin/find"

" OutLine Mode
let Tlist_Ctags_Cmd = 'ctags'

" Ruby Syntax
let ruby_minlines = 100 

" RDCodeTools
" let g:rct_completion_use_fri = 1

"-------------------------------------------------------------------------------
" map
"-------------------------------------------------------------------------------

" コマンドモード時にカーソル移動するのに便利ー
cmap <C-a> <Home>
cmap <C-e> <End>
cmap <C-f> <Right>
cmap <C-b> <Left>

" Save like Emacs
imap <C-x><C-s> <ESC>:w<CR>a

" For Enter Back
inoremap <C-b> <Esc>kA

" Buffer
map \x :bdelete<CR>
map \, :bprev<CR>
map \. :noautocmd bnext<CR>
map \bx :bdelete<CR>

" Tab
map \< :tabprev<CR>
map \> :tabnext<CR>
map \te :tabedit 
map \tx :tabclose<CR>
map \tn :tabnew<CR>
map \ta :tab ball<CR>
map \to :tabonly<CR>
map \ts :tabs<CR>

" Short
map <Space> :noautocmd bnext<CR>
map <C-Space> :bprev<CR>
map <Up> :tabprev<CR>
map <Down> :tabnext<CR>
map <Left> :bprev<CR>
map <Right> :noautocmd bnext<CR>

" Yank Ring
map \y :YRShow<CR>

" IncSearch
vnoremap <C-S> y:%s/<C-R>"//g<Left><Left> 

" 検索時に結果が中央に来るようにする
nmap n nzz
nmap N Nzz
nmap * *zz
nmap # #zz
nmap g* g*zz
nmap g# g#zz

" 改行
map \<CR> o<ESC>

" 検索ハイライト切り替え
map \\hl :set hlsearch!<CR>
map \\li :set list!<CR>

" コピペモード
map \\text :map p :put^V^V
Go^V^V^[<CR>:w<CR>

" スペルチェッカ
map \\spell :lcd<CR>:w!<CR>:!aspell --lang=en --ignore-case -c %<CR>:e! %<CR>

" 適当なショートカット
map \\vrc :e ~/_vimrc<CR>
map \\gvrc :e ~/_gvimrc<CR>
map \\memo :e ~/.vimemo<CR>

" ESC
" imap <C-k> <ESC>

" 挿入モードでの移動
imap <C-a> <Home>
imap <C-e> <End>
imap <C-f> <Right>
imap <C-b> <Left>

" カーソルラインの切り替え
map \\cl :set cursorline!<CR>

" 読みモード
map \\read :set scrolloff=666<CR>:highlight Cursor guifg=NONE guibg=NONE<CR>

" Omni Completion
imap <C-Space> <C-x><C-o><C-p>

" lookupfile.vim
nmap <unique> <silent> <C-S> :LUBufs<CR>
let g:LookupFile_AlwaysAcceptFirst=1
let g:LookupFile_PreserveLastPattern=0
let g:LookupFile_AllowNewFiles=0



"-------------------------------------------------------------------------------
" コマンド
"-------------------------------------------------------------------------------

" vimrc編集
command! Rc :e ~/_vimrc
command! Grc :e ~/_gvimrc

" 再エンコード
command! -nargs=1 Reenco :e ++enc=<args>

" 日付挿入
command! Date normal o<ESC>!!date<CR>==
map \date :Date<CR>

" シェルのスクリプト
command! Shell :call InitShell()



"-------------------------------------------------------------------------------
" Auto Command
"-------------------------------------------------------------------------------

autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g`\"" | endif
autocmd BufNewFile,BufRead *.as set ft=javascript
autocmd BufNewFile,BufRead *.pxd set ft=xml
autocmd BufNewFile,BufRead */mover/temp/*.txt set fenc=cp932



"-------------------------------------------------------------------------------
" その他関数
"-------------------------------------------------------------------------------

function! Scratch ()
	:new
	:set buftype=nowrite
endfunction
command! Scratch :call Scratch()



"--------------------------------------------------------------------------------
" Encoding
"--------------------------------------------------------------------------------

if &encoding !=# 'utf-8'
  set encoding=japan
endif

set fileencoding=japan

if has('iconv')
  let s:enc_euc = 'euc-jp'
  let s:enc_jis = 'iso-2022-jp'
  " iconvがJISX0213に対応しているかをチェック
  if iconv("\x87\x64\x87\x6a", 'cp932', 'euc-jisx0213') ==# "\xad\xc5\xad\xcb"
    let s:enc_euc = 'euc-jisx0213'
    let s:enc_jis = 'iso-2022-jp-3'
  endif
  " fileencodingsを構築
  if &encoding ==# 'utf-8'
    let s:fileencodings_default = &fileencodings
    let &fileencodings = s:enc_jis .','. s:enc_euc .',cp932'
    let &fileencodings = &fileencodings .','. s:fileencodings_default
    unlet s:fileencodings_default
  else
    let &fileencodings = &fileencodings .','. s:enc_jis
    set fileencodings+=utf-8,ucs-2le,ucs-2
    if &encoding =~# '^euc-\%(jp\|jisx0213\)$'
      set fileencodings+=cp932
      set fileencodings-=euc-jp
      set fileencodings-=euc-jisx0213
      let &encoding = s:enc_euc
    else
      let &fileencodings = &fileencodings .','. s:enc_euc
    endif
  endif
  unlet s:enc_euc
  unlet s:enc_jis
endif



"-------------------------------------------------------------------------------
" はてなパクリ
"-------------------------------------------------------------------------------

function! GetB()
  let c = matchstr(getline('.'), '.', col('.') - 1)
  let c = iconv(c, &enc, &fenc)
  return String2Hex(c)
endfunction
" :help eval-examples
" The function Nr2Hex() returns the Hex string of a number.
func! Nr2Hex(nr)
  let n = a:nr
  let r = ""
  while n
    let r = '0123456789ABCDEF'[n % 16] . r
    let n = n / 16
  endwhile
  return r
endfunc
" The function String2Hex() converts each character in a string to a two
" character Hex string.
func! String2Hex(str)
  let out = ''
  let ix = 0
  while ix < strlen(a:str)
    let out = out . Nr2Hex(char2nr(a:str[ix]))
    let ix = ix + 1
  endwhile
  return out
endfunc

"ステータスラインに文字コードと改行文字を表示する
set statusline=%<[%n]%m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).':'.&ff.']'}%y\ %f%=[%{GetB()}]\ %l,%c%V%8P



"--------------------------------------------------------------------------------
" テンプレ自動設定
"--------------------------------------------------------------------------------

function! LoadTemplate()
	if &filetype == ''
		return
	endif
	if getfsize(bufname("")) > 10
		return
	endif
	let filename = $HOME . 'vimfiles/template/' . &filetype . '.txt'
	if !filereadable(filename)
		return
	endif
	execute 'read ' . filename
	go 1
	delete
	normal G
	set fileencoding=cp932
endfunction

autocmd BufNewFile,BufRead * :call LoadTemplate()



"--------------------------------------------------------------------------------
" タブラインの表示
"--------------------------------------------------------------------------------

if v:version >= 700

	function! TabLine()
		let res = ''
		let curtab = tabpagenr()
		let i = 1
		for i in range(1, tabpagenr('$'))
			let res .= ((i == curtab) ? '%#TabLineSel#' : '%#TabLine#')
			let res .= i . '-' . tabpagewinnr(i, '$') . ':'
			let res .= substitute(bufname(tabpagebuflist(i)[0]), '.\+\/', '', 'g')
			let res .= ' '
			let i += 1
		exe | endfor
		let res .= '%#TabLineFill#'
		return res
	endfunction

	set tabline=%!TabLine()

endif


 
"--------------------------------------------------------------------------------
" 折りたたみの保存
"--------------------------------------------------------------------------------

set viewdir=~/vimfiles/view
au BufWritePost * mkview
autocmd BufReadPost * loadview



"--------------------------------------------------------------------------------
" Temp
"--------------------------------------------------------------------------------

function! RemoveBrackets ()
	ruby <<
		$KCODE = 'sjis'
		buf = VIM::Buffer.current
		buf.count.times do
			|lc|
			lc += 1
			ls = buf[lc]
			ls.gsub! /[\[\(【(].+?[\]\))】]/, ''
			ls.gsub! /^\s+/, ''
			buf[lc] = ls
		end
.
endfunction

"--------------------------------------------------------------------------------
" Fix netrw buffer
"--------------------------------------------------------------------------------

"autocmd BufReadCmd file://*,?:/*/ :exe ":nnoremap <buffer> <space> :bwipeout<cr>"
"autocmd BufReadCmd file://*,?:/*/ :setlocal bufhidden=wipe 


"--------------------------------------------------------------------------------
" Misc
"--------------------------------------------------------------------------------

let mapleader = ","

function! Size ()
	let r = 0
	for i in range(1, line('$'))
		if match(getline(i), "^#")
			let r += len(getline(i))
		endif
	endfor
	echo r
endfunction
map \\size :call Size()<CR>