Scite Word Select

lua-users home
wiki

快速設定

這段指令碼讓你能選取游標下的字詞。這類似許多文字編輯器中的編輯/選取字詞指令。

首先,將下列碼放入你的 Lua 啟動檔

function isWordChar(char)
    local strChar = string.char(char)
    local beginIndex = string.find(strChar, '%w')
    if beginIndex ~= nil then
        return true
    end
    if strChar == '_' then
        return true
    end
    return false
end

function SelectWord()
    local beginPos = editor.CurrentPos
    local endPos = beginPos
    while isWordChar(editor.CharAt[beginPos-1]) do
        beginPos = beginPos - 1
    end
    while isWordChar(editor.CharAt[endPos]) do
        endPos = endPos + 1
    end
    if beginPos ~= endPos then
        editor.SelectionStart = beginPos
        editor.SelectionEnd   = endPos
    end
end

之後,你需要為 SelectWord 繫結一個捷徑鍵。在你的屬性檔案裡,將以下碼取代為未使用過的指令編號 13。此外,不要猶豫,使用任何你喜歡的捷徑,比如說 Ctrl+J。

command.name.13.*=Select Word
command.mode.13.*=subsystem:lua,savebefore:no,groupundo
command.shortcut.13.*=Ctrl+J
command.13.*=SelectWord

解釋

演算法相當簡單。我們有兩個變數,分別會是字詞的開頭與結尾位置。一開始,它們相等,並指向游標位置。我們將 beginPos 向左移動(遞減它),並將 endPos 向右移動(遞增它),直到遇到字詞的邊界。然後我們使用這些變數來設定編輯器的選取範圍。

MocanuCristian


最近異動 · 喜好設定
編輯 · 歷程
上次編輯時間為 2006 年 8 月 31 日下午 8:50 GMT (差異)