牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

CotEditer で行頭にナンバリングするスクリプトを Ruby で書いてみたメモ

(MacOSX10.3, Ruby1.9.3, CotEditer1.3.1)

行頭が『-』『+』『1.』の場合、ナンバリングするスクリプト

For example

行頭が『-』の場合の実行結果
- hoge
- fuga
- foo
- bar

#=> 1. hoge
#=> 2. fuga
#=> 3. foo
#=> 4. bar
行頭が『-』『+』『1.』(数字+ピリオド)の混在場合の実行結果
- hoge
1. fuga
+ foo
- bar

#=> 1. hoge
#=> 2. fuga
#=> 3. foo
#=> 4. bar

コード

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# %%%{CotEditorXInput=Selection}%%%
# %%%{CotEditorXOutput=ReplaceSelection}%%%

def counter(n)
  lambda { |i| n += i }
end

count = counter(0)

while gets
  print $_.sub(/^ *?\d+.(?= )|^ *?\-(?= )|^ *?\+(?= )/, "#{count.call(1)}.")
end

exit

メモ

スクリプトの書き方は
CotEditer/ScriptMenu の中の『_aboutScriptFolder.rtf』とか『SampleScript』に書いてある。書き終わってから気づいた。