インタビュー記事作成に使えるSublimeText3のプラグインを作った
はじめに
前回の記事の続きでインタビュー記事作成等に使えるST3のプラグインを作りました。
あらかじめ登録しておいた『〇〇「」』をショートカットキーで
呼び出すことのできるプラグインです。
ショートカットキーで登録してある『〇〇「」』を呼び出して、
セリフを入力していくことも可能ですし、
以下のようにセリフだけ書かれた会話があった場合、
特定の行にカーソルがあればショートカットキーで、
誰のセリフかを記載することができます。
お元気ですか? 元気です。ありがとう。あなたはどうですか? 私も元気です
↓
ジョン「お元気ですか?」 メアリー「元気です。ありがとう。あなたはどうですか?」 ジョン「私も元気です」
プログラム
前回の記事でプラグインの作り方(5分ぐらいで作れます)を書いたのでそれを参考に導入できます。
モジュール部分のソースは以下の通りです。
user1_nameのhoge部分を変更して使ってください。
import sublime import sublime_plugin user1_name = 'hoge1' user2_name = 'hoge2' user3_name = 'hoge3' user4_name = 'hoge4' class User1Command(sublime_plugin.TextCommand): def run(self, edit): vw = self.view # Selection オブジェクトを取得 sel = vw.sel() # Selection から Region オブジェクトを取得 reg = sel[0] # line() で Region を行単位に拡大する reg = vw.line(reg) ine_text = self.view.substr(reg) #文字列加工 replace = user1_name + '「' + ine_text + '」' # カーソルが置かれている行を置き換え vw.replace(edit, reg, replace) #カーソル位置計算 num = str(reg)[1:len(str(reg))-1].split(",") carsol = int(num[1]) + len(str(user1_name)) + 1 #カーソル移動 point = int(carsol) self.view.sel().clear() region = sublime.Region(point) self.view.sel().add(region) self.view.show(point) class User2Command(sublime_plugin.TextCommand): def run(self, edit): vw = self.view # Selection オブジェクトを取得 sel = vw.sel() # Selection から Region オブジェクトを取得 reg = sel[0] # line() で Region を行単位に拡大する reg = vw.line(reg) ine_text = self.view.substr(reg) #文字列加工 replace = user2_name + '「' + ine_text + '」' # カーソルが置かれている行を置き換え vw.replace(edit, reg, replace) #カーソル位置計算 num = str(reg)[1:len(str(reg))-1].split(",") carsol = int(num[1]) + len(str(user2_name)) + 1 #カーソル移動 point = int(carsol) self.view.sel().clear() region = sublime.Region(point) self.view.sel().add(region) self.view.show(point) class User3Command(sublime_plugin.TextCommand): def run(self, edit): vw = self.view # Selection オブジェクトを取得 sel = vw.sel() # Selection から Region オブジェクトを取得 reg = sel[0] # line() で Region を行単位に拡大する reg = vw.line(reg) ine_text = self.view.substr(reg) #文字列加工 replace = user3_name + '「' + ine_text + '」' # カーソルが置かれている行を置き換え vw.replace(edit, reg, replace) #カーソル位置計算 num = str(reg)[1:len(str(reg))-1].split(",") carsol = int(num[1]) + len(str(user3_name)) + 1 #カーソル移動 point = int(carsol) self.view.sel().clear() region = sublime.Region(point) self.view.sel().add(region) self.view.show(point) class User4Command(sublime_plugin.TextCommand): def run(self, edit): vw = self.view # Selection オブジェクトを取得 sel = vw.sel() # Selection から Region オブジェクトを取得 reg = sel[0] # line() で Region を行単位に拡大する reg = vw.line(reg) ine_text = self.view.substr(reg) #文字列加工 replace = user4_name + '「' + ine_text + '」' # カーソルが置かれている行を置き換え vw.replace(edit, reg, replace) #カーソル位置計算 num = str(reg)[1:len(str(reg))-1].split(",") carsol = int(num[1]) + len(str(user4_name)) + 1 #カーソル移動 point = int(carsol) self.view.sel().clear() region = sublime.Region(point) self.view.sel().add(region) self.view.show(point)
キーバインドの設定は以下のようにしました。
これも、自分の使いやすいショートカットに変更して使ってください。
[ { "keys": ["super+_"], "command": "user1" }, { "keys": ["super+/"], "command": "user2" }, { "keys": ["super+."], "command": "user3" }, { "keys": ["super+,"], "command": "user4" }, ]
おわり
これくらいの簡単なプラグインなら10分ぐらいで作れちゃうんで、
ST3はpython好きにはもってこいのテキストエディタですね。