# sublime插件開發教程(附源碼)
### 1.背景
雖然可能大神門在編輯器方面都比較偏向于vim之類的自由度更高的工具,但是從我個人來講sublime這樣的插件安裝更方便的工具還是比較得心應手的。之前用sublime寫英語作文,但是沒有一個比較好用的timer,Package_Control里面的track_timer不能實時顯示時間,所以博主就自己動手,寫了這個插件,可以實時timer,記錄時間。效果如下圖,
### 2.使用
使用起來很方便,只要把下載好的sublime-timer文件夾放在下圖這個路徑下即可。
可以用快捷鍵方便的對timer進行操作:
~~~
"control+alt+t": start timer
"control+alt+p": pause or stop timer
"control+alt+z": make zero
~~~
### 3.制作過程
#### (1)環境
開發sublime插件用到的是python語言,因為要用到sublime內置的sublime和sublime_plugin庫,所以debug和調試都應該在sublime里面。
下面的鏈接是sublime的庫得參數信息: http://www.sublimetext.com/docs/2/api_reference.html
#### (2)自帶example
如果不習慣看開發文檔,可以參考下以下example的開發(下面參考自 http://www.welefen.com/how-to-develop-sublime-text-plugin.html )。
1、通過Tools -> New Plugin...來打開一個初始化的插件編輯文件,它將有如下的內容:
~~~
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
~~~
2、通過Preferences -> Browse Packages...打開Packages文件夾,在該文件夾下建立個子文件夾,名字為你想開發的插件名字,如:KeymapManager。回到插件開發的初始化編輯器頁面,通過 ctrl+s (Windows/Linux) or cmd+s (OS X)保存這個文件,并放到你建立的子文件夾下,文件名如:KeymapManager.py
3、通過 ctrl+` 快捷鍵打開SublimeText的控制臺,執行如下的命令:
`view.run_command('example')`
如果你在當前文件最前面看到插入了Hello, Word!,那表明插件執行成功了。
4、ExampleCommand名字改為你想要的插件名字,如: KeymapmanagerCommand,然后就可以開發該插件對應的功能了。
5、通過官方的API文檔查找你需要的接口,文檔見: http://www.sublimetext.com/docs/2/api_reference.html
(3)sublime-timer
這個就是我開發的sublime-timer,比example會復雜一些。大家可以參照以下代碼:
~~~
import sublime, sublime_plugin
import threading
import time
i=0
class timer(threading.Thread): #The timer class is derived from the class threading.Thread
def __init__(self, num, interval):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False
def run(self): #Overwrite run() method, put what you want the thread do here
global i
while not self.thread_stop:
sublime.set_timeout(write_time,1)
i+=1
time.sleep(self.interval)
def pause(self):
self.thread_stop = True
def zero(self):
global i
i=0
thread1 = timer(1, 1)
class gtimerCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread=timer(1,1)
if thread1.isAlive():
live=True
else:
thread.start()
thread1=thread
class gtimerpauseCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread1.pause()
class gtimerzeroCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread1.zero()
def write_time():
sublime.status_message(time_manage(i))
def time_manage(time_number):
time_str='time:'+str(time_number/60)+'min '+str(time_number%60)+'s'
return time_str
~~~
三個command class,分別對應著上面提到的三個快捷鍵,這個對應關系可以在另外的keymap文件中定義,大家可以把整個項目clone下來就看到了。
(4)發布
如果你做好了一個個性插件想讓更多的朋友使用的話可以試試以下兩種途徑。
1.可以給 https://github.com/SublimeText 發email
2.可以給 https://github.com/wbond/package_control_channel pull issue(有一個文檔,流程比較麻煩)
項目地址: https://github.com/jimenbian/sublime-timer (fork完別忘了給個star)
好了,看到這里大家應該已經對插件制作有些了解了,動起手來吧!
> 原文:http://blog.csdn.net/buptgshengod/article/details/40001171
- 序言
- 安裝
- 漢化
- 快捷鍵 Win
- 快捷鍵 MAC
- Features(功能)
- Alignment
- 設置字體及字體大小
- 自定制代碼片段
- 快速補全
- HTML
- CSS
- 安裝插件
- Emmet
- JsMinifier
- Sublime CodeIntel
- Bracket Highlighter
- CSScomb CSS
- SublimeTmpl
- SideBarEnhancements
- ColorPicker
- Tag
- Clipboard History
- GBK to UTF8
- SFTP
- WordPress
- PHPTidy
- YUI Compressor
- SublimeLinter
- ConvertToUTF8
- AutoFileName
- IMESupport
- FileDiffs
- View In Browser
- HTML-CSS-JS Prettify
- 插件開發
- sublime插件開發教程(附源碼)
- 插入當前時間插件
- 自定義插件
- Sublime-thinkphp
- 其他技術
- 添加右鍵打開的操作
- 激活
- Sublime Text3 3103 激活碼
- 常見錯誤
- an error occurred installing package control
- Sublime Text 全局搜索Ctrl+Shift+F快捷鍵不能用