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

RecentChanges · preferences
编辑 · 历史
最后编辑于 2006 年 8 月 31 日 下午 2:45 GMT (差异)