コマンドの実行はコンソール( ctrl + ` で開ける)で以下を実行する:
view.run_command('example')
Default (OSX).sublime-keymap
等に以下を記述しておくと cmd + ctrl + e
で example コマンドを実行できる:
{ "keys": ["super+ctrl+e"], "command": "example" }
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "foo")
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
self.view.replace(edit, region, 'foo')
複数箇所が選択されているケースがあるので for
でループしてますね。
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
self.view.erase(edit, region)
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
print self.view.substr(region)
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
region_of_line = self.view.line(region)
print self.view.substr(region_of_line)
full_line
を使うと改行も含めて取得できる。
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
region_of_word = self.view.word(region)
print self.view.substr(region_of_word)
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
self.view.sel().clear()
for region in view.find_all('foo'):
view.sel().add(region)
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.status_message("foo")
あまり目立たないですね。
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("foo")
デバッグ用に使えるかな。
Default プラグインで view = self.view
とやっているのをよく見かけたので真似した。
コンソールで dir
関数を使って調べる
dir(view)
とか
dir(sublime)
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
if sublime.ok_cancel_dialog("OK?", "OK"):
print "OK"
else:
print "Cancel"
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.message_dialog("foo")
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.error_message("error!")
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
print sublime.get_clipboard()
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard("foo")
'default_line_ending' という設定値を取得してみる:
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
default_line_ending = self.view.settings().get('default_line_ending')
sublime.message_dialog(default_line_ending)
Example.sublime-settings というファイルを用意し:
{
"foo": "bar"
}
以下のようにして利用できる:
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
SETTINGS = sublime.load_settings("Example.sublime-settings")
def run(self, edit, **args):
sublime.message_dialog(self.SETTINGS.get("foo"))
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings('Preferences.sublime-settings')
settings.set('color_scheme', 'Packages/Color Scheme - Default/Espresso Libre.tmTheme')
sublime.save_settings('Preferences.sublime-settings')
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
for k, v in args.iteritems():
sublime.message_dialog("%s: %s" % (k, v))
XXX.sublime-keymap で以下のようにしてショートカットキーと結びつけることができる:
[
{"keys": ["super+e"], "command": "example", "args": {"title": "foo", "message": "bar"}}
]
コンソール等から以下のようにして呼び出すことができる:
view.run_command('example', {"title": "foo", "message": "bar"})
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.WindowCommand):
def run(self):
# show_input_panel(caption, initial_text, on_done, on_change, on_cancel)
self.window.show_input_panel('caption', '', self.on_done, None, None)
def on_done(self, result):
sublime.message_dialog(result)
sublime_plugin.WindowCommand
クラスを継承してコマンドを作る。
sublime_plugin.TextCommand
とは run
メソッドの引数の数が違う。
on_done
というコールバックメソッドで結果を取得している。
追記: 後で気づいたことだけど、TextCommand でも self.view.window()
として window
を取得できるので、別に WindowCommand
である必要はない
sublime_plugin.EventListener
を継承したクラスを作るらしい。後で調べる。