download

Sign in or create your account | Project List | Help

download Git Source Tree

Root/head.vim

1:scriptencoding utf-8
2" 1度スクリプトを読み込んだら、2度目は読み込まない
3:if exists('g:loaded_head')
4    :finish
5:endif
6:let g:loaded_head = 1
7
8" g:head_file_splitterオプションが設定されていたら、、
9" そのオプションの値を使用する
10" g:head_file_splitterオプションが設定されていなかったら、、
11" スクリプトで定義したデフォルト値を使用する
12:if exists('g:head_file_splitter')
13    :let s:spc = g:head_file_splitter
14:else
15    :if has('win32')
16        :let s:spc = ";"
17    :else
18        :let s:spc = ":"
19    :endif
20:endif
21
22" g:head_display_linesオプションが設定されていたら、その値を使用する
23" 設定されていなかったら、スクリプトのデフォルト値を使用する
24:if exists('g:head_display_lines')
25    :let s:viewsize = g:head_display_lines
26:else
27    :let s:viewsize = 10
28:endif
29
30" 渡された値で、s:viewsizeを更新する
31:function! s:UpdateViewSize(size)
32    :let s:viewsize = a:size
33:endfunction
34
35" head:sample.txt、もしくは、head;sample.txt
36" というファイル名を渡されるので、
37" このファイルの上の方だけを読み込む
38:function! s:HeadRead(path)
39    " <afile>ならexpand()で展開する
40    " そうでないなら渡されたファイル名を使用する
41    :if a:path == "<afile>"
42        :let l:file = expand(a:path)
43    :else
44        :let l:file = a:path
45    :endif
46
47    " 実際に読む込むべきファイルのパスを取得
48    :let l:prot = matchstr(l:file,'^\(head\)\ze' . s:spc)
49    :if l:prot != ''
50        :let l:file = strpart(l:file, strlen(l:prot) + 1)
51    :endif
52
53    " ファイルを読み込んで、バッファにセット
54    :0,$d
55    :call setline(1,"foo")
56    :let l:lines = readfile(l:file, "", s:viewsize)
57    :let l:i = 0
58    :while l:i < len(l:lines)
59        :call setline(l:i + 2, l:lines[l:i])
60        :let l:i += 1
61    :endwhile
62    :1d
63
64    " 編集中ではない状態にする
65    :set nomodified
66    " ファイルタイプを再判定
67    :filetype detect
68:endfunction
69
70" head:sample.txt、もしくは、head;sample.txt
71" というファイル名を渡されるので、
72" このファイルの上の方だけをバッファのテキストで更新する
73:function! s:HeadWrite(path)
74    " <afile>ならexpand()で展開する
75    " そうでないなら渡されたファイル名を使用する
76    :if a:path == "<afile>"
77        :let l:file = expand(a:path)
78    :else
79        :let l:file = a:path
80    :endif
81
82    " 実際に読む込むべきファイルのパスを取得
83    :let l:prot = matchstr(l:file,'^\(head\)\ze' . s:spc)
84    :if l:prot != ''
85        :let l:file = strpart(l:file, strlen(l:prot) + 1)
86    :endif
87
88    " 更新しても良いか、ユーザに問い合わせ
89    :let choice = confirm("Are you sure, update '".l:file."' head ?", "&Update\n&Cancel")
90    :if choice == 1
91        " 全行読み込んで、ファイルの上の方だけ更新
92        " 最後に編集中状態をクリア。編集していない状態にする。
93        :let l:lines = filereadable(l:file)? readfile(l:file): []
94        :if len(l:lines) > s:viewsize
95            :let l:i = 0
96            :while l:i < s:viewsize
97                :unlet l:lines[l:i- 1]
98                :let l:i += 1
99            :endwhile
100
101            :let l:curbufs = getline(0, line("$"))
102            :call extend(l:curbufs, l:lines)
103            :call writefile(l:curbufs, l:file)
104            :set nomodified
105        :else
106            :call writefile(getline(0, line("$")), l:file)
107            :set nomodified
108        :endif
109        :return
110    :else
111        :return
112    :endif
113:endfunction
114
115" tail:sample.txt、もしくは、tail;sample.txt
116" というファイル名を渡されるので、
117" このファイルの下の方だけをバッファに読み込む。
118:function! s:TailRead(path)
119    " <afile>ならexpand()で展開する
120    " そうでないなら渡されたファイル名を使用する
121    :if a:path == "<afile>"
122        :let l:file = expand(a:path)
123    :else
124        :let l:file = a:path
125    :endif
126
127    " 実際に読む込むべきファイルのパスを取得
128    :let l:prot = matchstr(l:file,'^\(tail\)\ze' . s:spc)
129    :if l:prot != ''
130        :let l:file = strpart(l:file, strlen(l:prot) + 1)
131    :endif
132
133    " ファイルを下の方だけ読み込んで、バッファにセットする
134    :0,$d
135    :call setline(1,"foo")
136    :let l:lines = readfile(l:file)
137    :if len(l:lines) > s:viewsize
138        :let l:i = 0
139        :while l:i < s:viewsize
140            :call setline(l:i + 2, l:lines[len(l:lines) - s:viewsize + l:i])
141            :let l:i += 1
142        :endwhile
143    :else
144        :let l:i = 0
145        :while l:i < len(l:lines)
146            :call setline(l:i + 2, l:lines[l:i])
147            :let l:i += 1
148        :endwhile
149    :endif
150    :1d
151
152    " 編集中ではない状態にする
153    :set nomodified
154    " ファイルタイプを再判定
155    :filetype detect
156:endfunction
157
158" tail:sample.txt、もしくは、tail;sample.txt
159" というファイル名を渡されるので、
160" このファイルの下の方だけをバッファのテキストで更新する
161:function! s:TailWrite(path)
162    " <afile>ならexpand()で展開する
163    " そうでないなら渡されたファイル名を使用する
164    :if a:path == "<afile>"
165        :let l:file = expand(a:path)
166    :else
167        :let l:file = a:path
168    :endif
169
170    " 実際に読む込むべきファイルのパスを取得
171    :let l:prot = matchstr(l:file,'^\(tail\)\ze' . s:spc)
172    :if l:prot != ''
173        :let l:file = strpart(l:file, strlen(l:prot) + 1)
174    :endif
175
176    " 更新しても良いか、ユーザに問い合わせ
177    :let choice = confirm("Are you sure, update '".l:file."' tail ?", "&Update\n&Cancel")
178    :if choice == 1
179        " 全行読み込んで、ファイルの下の方だけ更新
180        " 最後に編集中状態をクリア。編集していない状態にする。
181        :let l:lines = filereadable(l:file)? readfile(l:file): []
182        :if len(l:lines) > s:viewsize
183            :let l:i = 0
184            :while l:i < s:viewsize
185                :unlet l:lines[len(l:lines) - 1]
186                :let l:i += 1
187            :endwhile
188            :call extend(l:lines, getline(0, line("$")))
189            :call writefile(l:lines, l:file)
190            :set nomodified
191        :else
192            :call writefile(getline(0, line("$")), l:file)
193            :set nomodified
194        :endif
195        :return
196    :else
197        :return
198    :endif
199:endfunction
200
201" autocmdのグループを定義
202" head、tailで始まるファイルは、他のスクリプトに操作させたくないので、
203" autocmd!で、設定されたautocmdを消してしまう。
204:augroup Head
205    :autocmd!
206    :execute ":autocmd BufReadCmd head" .s:spc. "*,head" .s:spc. "*/* HeadRead <afile>"
207    :execute ":autocmd FileReadCmd head" .s:spc. "*,head" .s:spc. "*/* HeadRead <afile>"
208    :execute ":autocmd BufWriteCmd head" .s:spc. "*,head" .s:spc. "*/* HeadWrite <afile>"
209    :execute ":autocmd FileWriteCmd head" .s:spc. "*,head" .s:spc. "*/* HeadWrite <afile>"
210:augroup END
211:augroup Tail
212    :autocmd!
213    :execute ":autocmd BufReadCmd tail" .s:spc. "*,tail" .s:spc. "*/* TailRead <afile>"
214    :execute ":autocmd FileReadCmd tail" .s:spc. "*,tail" .s:spc. "*/* TailRead <afile>"
215    :execute ":autocmd BufWriteCmd tail" .s:spc. "*,tail" .s:spc. "*/* TailWrite <afile>"
216    :execute ":autocmd FileWriteCmd tail" .s:spc. "*,tail" .s:spc. "*/* TailWrite <afile>"
217:augroup END
218
219" ファイルの読み書き用のコマンドを定義
220:command! -nargs=1 HeadRead :call s:HeadRead(<f-args>)
221:command! -nargs=1 HeadWrite :call s:HeadWrite(<f-args>)
222:command! -nargs=1 TailRead :call s:TailRead(<f-args>)
223:command! -nargs=1 TailWrite :call s:TailWrite(<f-args>)
224
225" プロパティ更新用のコマンドを定義
226:command! -nargs=1 Head :call s:UpdateViewSize(<f-args>)
227
228" スクリプトはここまで
229:finish
230
231==============================================================================
232head.vim : ファイルの上か下、限定された行数のみ読み込むスクリプト
233------------------------------------------------------------------------------
234$VIMRUNTIMEPATH/plugin/head.vim
235==============================================================================
236author : 小見 拓
237url : http://nanasi.jp/
238email : mail@nanasi.jp
239version : 2009/12/19 16:00:00
240==============================================================================
241"head;"(Windows以外ではhead:)をファイル名に付けると、
242ファイルの上から10行のみを読み込み、
243"tail;"(Windows以外ではtail:)をファイル名に付けると、
244ファイルの下から10行のみを読み込む。
245sudo.vimのような処理を行う。
246
247(Windows)
248:e head;sample.txt
249:r head;sample.txt
250:w head;sample.txt
251:e tail;sample.txt
252:r tail;sample.txt
253:w tail;sample.txt
254
255(Windows以外)
256:e head:sample.txt
257:r head:sample.txt
258:w head:sample.txt
259:e tail:sample.txt
260:r tail:sample.txt
261:w tail:sample.txt
262
263==============================================================================
264" vim: set et nowrap ft=vim :
265

Archive Download this file

Branches:
master