-> 06 { 06 / 20 }
vimproc.vim
|http://tokyo.cool.ne.jp/hopper2/
おー。HTTP ライブラリとか書けそー。LL Ring で vim script な LT を!(ぇー
Vimスクリプトでプロセス間通信やソケット通信しちゃうライブラリ。
例えばこのように遊べます...
let sock = g:vimproc.socket_open("www.yahoo.com", 80) call sock.write("GET / HTTP/1.0\r\n\r\n") let res = "" while !sock.eof let res .= sock.read() endwhile call sock.close() for line in split(res, '\r\n\|\r\|\n') " ... endfor
vim script で oop
|おー、それっぽいのができるぽ!継承した関数の上書き方法がまだわからんけど、インスタンスっぽいものが作れたり継承ができたり。
まずはよくあるカウンターのようなもの。グローバル領域汚染してるけどまぁサンプルってことで。
let Counter = {} " initialize function Counter.initialize() let self.count = 0 return extend({}, self) endfunction function Counter.up() let self.count += 1 return self.count endfunction let counter1 = Counter.initialize() let counter2 = Counter.initialize() echo counter1.up() echo counter2.up() echo counter1.up() echo counter2.up() echo counter2.up() echo counter2.up() exit
実行
$ vim --cmd 'so %' counter.vim 1 1 2 2 3 4
次は Template Method パターンぽい物。
let Display = {} " initialize function Display.initialize() return extend({}, self) endfunction function Display.display(str) let self.messages = [] let self.length = strlen(a:str) call self.open() call self.write(a:str) call self.close() echo self.join() endfunction let CharDisplay = {} " inheritance call extend(CharDisplay, Display) function CharDisplay.open() call add(self.messages, '<<') endfunction function CharDisplay.close() call add(self.messages, '>>') endfunction function CharDisplay.write(str) call add(self.messages, a:str) endfunction function CharDisplay.join() return join(self.messages, '') endfunction let StringDisplay = {} " inheritance call extend(StringDisplay, Display) function StringDisplay.open() let str = '+' while strlen(str) < self.length + 3 let str .= '-' endwhile let str .= '+' call add(self.messages, str) endfunction function StringDisplay.close() call self.open() endfunction function StringDisplay.write(str) call add(self.messages, '| ' . a:str . ' |') endfunction function StringDisplay.join() return join(self.messages, "\n") endfunction let char_diaplay = CharDisplay.initialize() let string_diaplay = StringDisplay.initialize() call char_diaplay.display('foo') call string_diaplay.display('foo') call char_diaplay.display('barbaz') call string_diaplay.display('barbaz') exit
実行
$ vim --cmd 'so %' template.vim <<foo>> +-----+ | foo | +-----+ <<barbaz>> +--------+ | barbaz | +--------+
すげーぜ vimscript。例外処理もあるっぽいので後で試す。
コメント
トラックバック - http://subtech.g.hatena.ne.jp/secondlife/20060620