SciTE は、lua 言語によるマクロをサポートしています。 ところがこれが結構とっつきにくい。
ここでは、SciTE のマクロを作るためのとっかかりになるような 情報を紹介します。
まず、マクロスクリプト ~.lua というファイルが書けたとしましょう。 これをどうやってエディタに読み込ませるか、という話です。
それには、設定ファイルにこう記述します:
ext.lua.startup.script=C:/Programs/SciTE/macros/all.lua extension.*.html;*.htm=C:/Programs/SciTE/macros/html.lua extension.*.tex=C:/Programs/SciTE/macros/TeX.lua command.name.10.*=電卓 command.10.*=Macro_Calc command.shortcut.10.*=Shift+Enter command.subsystem.10.*=3 command.save.before.10.*=2 command.name.49.*.html;*.htm=タグ補完 command.49.*.html;*.htm=CompleteTag command.shortcut.49.*.html;*.htm=Alt+. command.subsystem.49.*.html;*.htm=3 command.save.before.49.*.html;*.htm=2 command.name.48.*.tex=タグ補完 command.48.*.tex=CompleteTagTeX command.shortcut.48.*.tex=Alt+] command.subsystem.48.*.tex=3 command.save.before.48.*.tex=2
あくまでも設定例です。(というか僕が使っているものそのまんまです)
ext.lua.startup.script には、エディタ起動時に実行するスクリプトを 書きます。このスクリプトで、たとえばキー入力を乗っ取ることができますし、 そこまで行かなくても後で実行するための関数を定義しておくことができます。
個別の拡張子のファイルを編集しているときだけ欲しいスクリプトは extension.*.html;*.htm などに記述します。
そうしてスクリプトを読み込んだ後、コマンドを定義していきます。
たとえば「電卓」と銘打った奴は
たとえば電卓マクロはこんな感じです:
function Macro_Calc() -- 実行する式を得る local sStatements, lenStatements = editor:GetSelText() local lnBegin if lenStatements > 0 then lnBegin = editor:LineFromPosition( math.min(editor.SelectionStart, editor.SelectionEnd) ) else sStatements = editor:GetCurLine() lnBegin = editor:LineFromPosition(editor.CurrentPos) end -- Lua では、 "1+1" だけみたいなのは完全な式でない -- そこで、完全な形式のステートメントかどうか確かめてみる sStatements = " " .. sStatements .. " " if not sStatements:match("[^<>~=]=[^=]") and not sStatements:match("%send%s") and not sStatements:match("%srepeat%s") and not sStatements:match("%slocal%s") then -- sStatements が完全なステートメントだとすると、 -- ここにくるのは純粋な関数呼び出しだけ。 -- sStatements が非式のとき、 -- それは prefixexp だと勝手に仮定。 -- するとこの仮定の元では、"local x = sStatements" は式。 sStatements = "local __Calc_result__ =" .. sStatements .. "if __Calc_result__ ~= nil then " .. "editor:AddText(tostring(__Calc_result__)) end" end -- 行数合わせ sStatements = ("\n"):rep(lnBegin) .. sStatements -- 実行する前に、カーソルを次の行へ editor:LineEnd() editor:NewLine() local function PrintError(sMsg) print(sMsg:gsub("^%[.-%]", "calc")) end -- 実行 local fn, sMsg = loadstring(sStatements, "") if not fn then PrintError(sMsg) return end local status, sMsg = pcall(fn) if not status then PrintError(sMsg) return end end
editor:という部分で、エディタを操っています。
こういう風に関数を記述したスクリプトを作っておいて、 そのパスを SciTE の設定ファイルに書くわけですね。
ちなみにこの電卓は、1+math.sqrt(2)[割り当てたキー] で 2.41421356 を表示する機能のほか、 lua スクリプトの部分をエディタ上で選択して[割り当てたキー]を押すとその場で実行できるという 機能も持っています。マクロ機能をテストするのに便利です。
「マクロを書きたい」と思っている人なら、 後はもう editor オブジェクトのマニュアルさえあればご自分で作れると思います。
ではマニュアルの場所は…というと、公式にはありません。多分。 スクリプトで拡張する際の概要 ならあるのですが……。
というわけでSciTE のマクロ関数マニュアルを生成するスクリプト を書きました。パブリックドメインです。
python iface-to-doc.py TOP_DIR_OF_SCITE_SRC
(TOP_DIR_OF_SCITE_SRC は、scite/ と scintilla/ 両方のディレクトリが入っているディレクトリ) のように起動すると、HTMLファイルを吐きます。
Ver.3.03 の時点ではこんなのになります: Spurious manual of SciTE Lua。 注意: スクリプトは煮ても焼いても構いませんが、 この出力 (Spurious manual of Scite Lua) はソースの中のファイルを読んで生成したものですから、 ライセンス(リンク先末尾)にしたがってお使いください。