download

Sign in or create your account | Project List | Help

download Git Source Tree

Root/rargs.vim

1scriptencoding utf-8
2if &cp || exists('g:loaded_rargs')
3    finish
4endif
5let g:loaded_rargs = 1
6
7" escape user configuration
8let s:save_cpo = &cpo
9set cpo&vim
10
11" exist when catch error option. default not exit.
12let s:exit_when_error = '0'
13if exists('g:rargs_exit_when_error')
14    let s:exit_when_error = g:rargs_exit_when_error
15else
16
17" read file at insertat
18function! s:RArgs(insertat,...)
19    " get option and file list.
20    let [l:optlist, l:filelist] = s:GetOpt(a:000)
21
22    " file readable check
23    for l:f in l:filelist
24        let l:hasError = '0'
25        if ! filereadable(l:f)
26            echohl WarningMsg | echo 'rargs.vim Warning : selected file "' . l:f . '" is not readable' | echohl None
27            let l:hasError = '1'
28        endif
29
30        " error is found.
31        if l:hasError !=# '0'
32            if s:exit_when_error !=# '0'
33                return
34            else
35                continue
36            endif
37        endif
38    endfor
39
40    " reverse filelist
41    call reverse(l:filelist)
42
43    for l:f in l:filelist
44        if filereadable(l:f)
45            try
46                let l:cmd = ':' . a:insertat . 'r ' . join(l:optlist, ' ') . ' ' . l:f
47                silent execute l:cmd | " comment
48            catch
49                echohl WarningMsg | echo 'rargs.vim Warning : catch error when reading "' .l:f. '". ' . v:exception | echohl None
50                if s:exit_when_error !=# '0'
51                    return
52                else
53                    continue
54                endif
55            endtry
56        endif
57    endfor
58endfunction
59
60" return file list, and command option.
61" [optionlist, filelist]
62function! s:GetOpt(paramlist)
63    let l:optlist = []
64    let l:filelist = []
65
66    for l:argv in a:paramlist
67        " option, ++ started
68        if strpart(l:argv, 0, 2) == '++'
69            call add(l:optlist, l:argv)
70        " file list
71        else
72            call extend(l:filelist, split(expand(l:argv), '\n'))
73        endif
74    endfor
75
76    return [l:optlist, l:filelist]
77endfunction
78
79command! -narg=* -range=1 -complete=file RArgs :call s:RArgs(<line1>,<f-args>)
80
81" recover user configuration
82let &cpo = s:save_cpo
83finish
84
85==============================================================================
86rargs.vim : 指定した複数のファイルを一度に読み込む
87------------------------------------------------------------------------------
88$VIMRUNTIMEPATH/plugin/rargs.vim
89==============================================================================
90author : 小見 拓
91url : http://nanasi.jp/
92email : mail@nanasi.jp
93version : 2009/09/17 14:00:00
94==============================================================================
95パラメータとして与えたファイルを読み込むコマンド「:RArgs」を定義しています。
96「:RArgs」は「:read」コマンドの拡張で、Unixで良く行われるファイルをまとめる操作、
97
98    cat file1 file2 file3 > some.txt
99
100を書き込まれる側のファイルから制御します。
101
102
103------------------------------------------------------------------------------
104[コマンドフォーマット]
105
106    " コマンドフォーマット
107    :[N]RArgs [++opt] {filename1} [{filename2} {filename3}...]
108
109    [N]
110        テキストを流し込む位置。省略可能。
111
112    [++opt]
113        ファイル読み込みの際に指定するオプション。
114        詳しくは、 ':help ++opt' 参照。
115
116    {filename1} [{filename2} {filename3}...]
117        読み込むファイル。複数指定可能。同じファイル指定可能。
118        ワイルドカード使用可能。
119        Vimのファイル系特殊キーワード('#2', '%')など使用可能。
120
121
122------------------------------------------------------------------------------
123[ファイル指定]
124
125:RArgsコマンドは、ファイル名のリストを受け取りますが、
126単純なファイル名だけでなく、ワイルドカードによるファイル指定や、
127Vimのファイル系の特殊キーワードを使用したファイル指定、
128同じファイルの複数回の読み込みも受け入れられます。
129
130    " 複数ファイルの読み込み
131    :RArgs sample1.txt sample2.txt sample3.txt
132
133    " ワイルドカード
134    :RArgs sample*
135
136    " Vimのファイル系特殊キーワード
137    :RArgs #2 #4 #6<.bak
138
139    " 同じファイルの複数回読み込み
140    :RArgs sample1.txt sample1.txt sample1.txt
141
142    " エンコード、ファイルフォーマット指定
143    :RArgs ++enc=utf-8 ++ff=unix sample1.txt sample2.txt sample3.txt
144
145
146------------------------------------------------------------------------------
147[読み込み位置]
148
149ファイルを読み込む位置は、未指定ならカーソル行、
150指定したなら、その指定行になります。
151
152    " カーソル行に読み込み
153    :RArgs sample1.txt sample2.txt sample3.txt
154
155    " 200行に読み込み
156    :200RArgs sample1.txt sample2.txt sample3.txt
157
158ファイルの先頭に、読み込みファイルを流し込むには、
159行数指定に0を指定してください。
160
161    " ファイルの先頭に読み込んだファイルを流し込む
162    :0RArgs sample1.txt sample2.txt sample3.txt
163
164
165------------------------------------------------------------------------------
166[設定]
167
168'g:rargs_exit_when_error'
169
170    ファイル読み込みの際、エラーが発生した、もしくは、指定したファイルが読み込めない場合、
171    プラグインの処理を中止するかしないかを 'g:rargs_exit_when_error' で設定できます。
172    「デフォルトはエラーがあっても、残りのファイルを継続して処理する」です。
173
174    エラー発見時に、プラグインの処理を中止するには、Vimの設定ファイルで次のように
175    設定してください。
176
177        let g:rargs_exit_when_error = '1'
178
179
180------------------------------------------------------------------------------
181[History]
1822009/09/16 0.1
183    - initial version.
184
1852009/09/17 0.2
186    - add exception handling logic.
187    - ++opt parameter is supported.
188    - option 'g:rargs_exit_when_error' is supported.
189
190
191==============================================================================
192" vim: set ff=unix et ft=vim nowrap :
193

Archive Download this file

Branches:
master