Scite Lua 提示 |
|
這是使用 SciteExtMan 腳本管理員的一個小腳本,而且是下載範例中的一個。
它使用 extman scite_OnOutputLine
事件來取得輸入在 SciTE 的輸出窗格中的行,並使用 loadstring
編譯它們。它給了你一個非常實用的方法來互動式評估 Lua 表達式。可以定義單行程式。
Scite/Lua > = 10 + 20 30 > print 'hello dolly' hello dolly > function showkeys(t) for i in t do print(i) end end > showkeys(table) setn insert getn foreachi foreach sort remove concat >
事件即使你沒有看自己從事 Lua 程式設計,這個 Lua 提示也可用作一個智慧型的計算器。
所有全域函式都可用。一個函式 load
已提供,會從一個檔案載入 Lua 程式碼,如果沒有引數呼叫它,它會使用目前的緩衝區。函式 edit
會在一個新的緩衝區開啟指定的檔案。有一個快顯式歷程記錄清單可用,使用 工具 | 上一個命令
,即 Ctrl-Alt-P
。
它對學習 SciTE 和 Scintilla API 特別有用,因為 editor
和 output
窗格物件都是直接存取的。你可以在撰寫一個完整的腳本之前測試一個特定的操作。
> = output.Length 405 > output:AppendText('hello, world!\n') hello, world! >
SciTE Lua 環境對期待全域物件能存活整個階段的人而言,會發生令人不快的驚喜。如果你切換 SciTE 中的緩衝區,全域環境會被清除。只有在 Lua 啟動腳本載入的檔案中最初建立的全域物件會被保留。
-- prompt.lua scite_Command('Last Command|do_command_list|Ctrl+Alt+P') local prompt = '> ' local history_len = 4 local prompt_len = string.len(prompt) print 'Scite/Lua' trace(prompt) function load(file) if not file then file = props['FilePath'] end dofile(file) end function edit(file) scite.Open(file) end local sub = string.sub local commands = {} local function strip_prompt(line) if sub(line,1,prompt_len) == prompt then line = sub(line,prompt_len+1) end return line end scite_OnOutputLine (function (line) line = strip_prompt(line) table.insert(commands,1,line) if table.getn(commands) > history_len then table.remove(commands,history_len+1) end if sub(line,1,1) == '=' then line = 'print('..sub(line,2)..')' end local f,err = loadstring(line,'local') if not f then print(err) else local ok,res = pcall(f) if ok then if res then print('result= '..res) end else print(res) end end trace(prompt) return true end) function insert_command(cmd) output:AppendText(cmd) output:GotoPos(output.Length) end function do_command_list() if table.getn(commands) > 1 then scite_UserListShow(commands,1,insert_command) end end