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 (差异)