Scite 處理字串

lua-users home
wiki

此腳本漸增地尋找一個 C 字串(依據 C/C++ 屬性檔案的樣式 6)並選擇性地在字串周圍加上包裝字元,以便可以使用地區設定翻譯。


function SciteProcessString()
  local StringStyle = 6         -- constant: language style for strings
  local function StyleAt(pos) return math.mod(editor.StyleAt[pos], 128) end
  local i = editor.CurrentPos
  while i < editor.Length do
    local sprev, style = StyleAt(i-1), StyleAt(i)
    if sprev ~= StringStyle and style == StringStyle then
      local inserted = false
      ------------------------------------------------------------
      -- insert _( if not present
      ------------------------------------------------------------
      editor:GotoPos(i)
      if i >= 2 and editor:textrange(i-2, i) ~= "_(" then
        editor:BeginUndoAction()
        inserted = true
        editor:AddText("_(")
        i = i + 2
      end
      while i < editor.Length and StyleAt(i) == StringStyle do i = i + 1 end
      ------------------------------------------------------------
      -- insert ) if _( inserted
      ------------------------------------------------------------
      editor:GotoPos(i)
      if inserted then
        editor:AddText(")")
        editor:EndUndoAction()
      end
      break
    end
    i = i + 1
  end
end


以下版本分兩個階段執行操作。在第一個階段,腳本尋找一個字串。在第二個階段,腳本對字串執行操作。使用者可以在第一階段找到字串後將插入點移到其後方,選擇不對某個特定字串執行第二階段操作。這使操作可以選擇性地執行。經過一段練習後,便能快速完成。


function SciteProcessString()
  local StringStyle = 6         -- constant: language style for strings
  local function StyleAt(pos) return math.mod(editor.StyleAt[pos], 128) end
  local function StrStart(pos)
    local sprev, style = StyleAt(pos-1), StyleAt(pos)
    if sprev ~= StringStyle and style == StringStyle then return true end
  end
  local i = editor.CurrentPos
  if StrStart(i) then
    local inserted = false
    ------------------------------------------------------------
    -- insert _( if not present
    ------------------------------------------------------------
    editor:GotoPos(i)
    if i >= 2 and editor:textrange(i-2, i) ~= "_(" then
      editor:BeginUndoAction()
      inserted = true
      editor:AddText("_(")
      i = i + 2
    end
    while i < editor.Length and StyleAt(i) == StringStyle do i = i + 1 end
    ------------------------------------------------------------
    -- insert ) if _( inserted
    ------------------------------------------------------------
    editor:GotoPos(i)
    if inserted then
      editor:AddText(")")
      editor:EndUndoAction()
    end
  else
    while i < editor.Length do
      if StrStart(i) then editor:GotoPos(i) break end
      i = i + 1
    end
  end
end

最近變更 · 喜好設定
編輯 · 歷程
上次編輯時間:2006 年 8 月 31 日下午 8:45 GMT (diff)