download

Sign in or create your account | Project List | Help

download Git Source Tree

Root/zshr.vim

1scriptencoding utf-8
2
3" 1度スクリプトを読み込んだら、2度目は読み込まない
4if &cp || exists("g:loaded_zshr")
5    finish
6endif
7let g:loaded_zshr = 1
8
9" ユーザの初期設定を逃がす
10let s:save_cpo = &cpo
11set cpo&vim
12
13" コマンド定義
14command! -nargs=+ R :call s:R(<f-args>)
15
16function! 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)
40endfunction
41
42" コマンドを置換して返す
43function! 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
58endfunction
59
60" 退避していたユーザのデータをリカバリ
61let &cpo = s:save_cpo
62" スクリプトはここまで
63finish
64
65==============================================================================
66zshr.vim : 直前に実行したコマンドを少し変更して実行する。zshシェルのrコマンド。
67------------------------------------------------------------------------------
68$VIMRUNTIMEPATH/plugin/zshr.vim
69==============================================================================
70author : 小見 拓
71url : http://nanasi.jp/
72email : mail@nanasi.jp
73version : 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

Archive Download this file

Branches:
master