| 1 | scriptencoding utf-8 |
| 2 | |
| 3 | " 1度スクリプトを読み込んだら、2度目は読み込まない |
| 4 | if &cp || exists("g:loaded_zshr") |
| 5 | finish |
| 6 | endif |
| 7 | let g:loaded_zshr = 1 |
| 8 | |
| 9 | " ユーザの初期設定を逃がす |
| 10 | let s:save_cpo = &cpo |
| 11 | set cpo&vim |
| 12 | |
| 13 | " コマンド定義 |
| 14 | command! -nargs=+ R :call s:R(<f-args>) |
| 15 | |
| 16 | function! s:R(...) |
| 17 | let l:length = len(a:000) |
| 18 | let l:index = 0 |
| 19 | |
| 20 | " 直前のコマンドを取得 |
| 21 | let l:pre_cmd = histget(":", -2) |
| 22 | |
| 23 | " パラメータの数だけコマンドの置換処理を実行 |
| 24 | while l:index < l:length |
| 25 | let l:pre_cmd = s:ReplaceCmd(l:pre_cmd, a:000[l:index]) |
| 26 | let l:index += 1 |
| 27 | endwhile |
| 28 | |
| 29 | " コマンドを実行 |
| 30 | try |
| 31 | execute l:pre_cmd |
| 32 | catch |
| 33 | " コマンド実行に失敗 |
| 34 | echohl ErrorMsg | echo v:exception | echohl None |
| 35 | endtry |
| 36 | |
| 37 | " コマンド実行履歴を追加。連続で:Rコマンドを実行するために必要。 |
| 38 | " 履歴を消す方法で、連続実行可能にすることはおそらく無理。 |
| 39 | call histadd(":", l:pre_cmd) |
| 40 | endfunction |
| 41 | |
| 42 | " コマンドを置換して返す |
| 43 | function! s:ReplaceCmd(cmd, param) |
| 44 | let l:cmd = a:cmd |
| 45 | |
| 46 | let l:matched = match(a:param, '[^=]\+=[^=]\+') |
| 47 | if l:matched > -1 |
| 48 | " 置換パターンを取り出して、置換処理実行 |
| 49 | let l:pre = substitute(a:param, '\([^=]\+\)=\([^=]\+\)', '\1', "") |
| 50 | let l:post = substitute(a:param, '\([^=]\+\)=\([^=]\+\)', '\2', "") |
| 51 | let l:cmd = substitute(l:cmd, l:pre, l:post, "g") |
| 52 | else |
| 53 | " コマンドの呼び出し方が違う |
| 54 | :echohl WarningMsg | echo ":R command syntax error is found." | echohl None |
| 55 | endif |
| 56 | |
| 57 | return l:cmd |
| 58 | endfunction |
| 59 | |
| 60 | " 退避していたユーザのデータをリカバリ |
| 61 | let &cpo = s:save_cpo |
| 62 | " スクリプトはここまで |
| 63 | finish |
| 64 | |
| 65 | ============================================================================== |
| 66 | zshr.vim : 直前に実行したコマンドを少し変更して実行する。zshシェルのrコマンド。 |
| 67 | ------------------------------------------------------------------------------ |
| 68 | $VIMRUNTIMEPATH/plugin/zshr.vim |
| 69 | ============================================================================== |
| 70 | author : 小見 拓 |
| 71 | url : http://nanasi.jp/ |
| 72 | email : mail@nanasi.jp |
| 73 | version : 2009/12/19 16:00:00 |
| 74 | ============================================================================== |
| 75 | 直前に実行したコマンドの一部を少し変更して実行する。 |
| 76 | |
| 77 | :%s/aaabbbccc/xxxyyyzzz/g |
| 78 | としてから、 |
| 79 | |
| 80 | :R a=m |
| 81 | と実行すると、 |
| 82 | |
| 83 | :%s/mmmbbbccc/xxxyyyzzz/g |
| 84 | が実行される。 |
| 85 | 直前に実行したコマンドを置き換えてから、改めて実行する。 |
| 86 | |
| 87 | :R a=m ccc=xyz bb=123 |
| 88 | のように複数の置換パラメータをとれる。 |
| 89 | |
| 90 | ============================================================================== |
| 91 | " vim: set et ft=vim nowrap : |
| 92 | |