Scite Colourise Demo

lua-users home
wiki

這是任意為文字套上色彩的指令碼示範,並在切換緩衝區時仍保留文字色彩。以下示範使用 SciteExtMan,但 extman 本身不是套用色彩技術必要的元件。

請注意,這個方法不能取代適當的詞法分析器介面,因此不應拿來實作基於 Lua 擴充的語法高亮顯示。這是因為任何強制部分緩衝區重新調整樣式的操作,都會導致中斷。不過,這種套用色彩方法可能對不需要「一般」編輯操作的事情很有用。


-----------------------------------------------------------------------
-- demo to provide custom colours and styles to a specific buffer
-- <khman@users.sf.net> public domain 20060906
-----------------------------------------------------------------------
-- [[
local function SetColours(lexer, scheme)
  local function dec(s) return tonumber(s, 16) end
  if lexer then editor.Lexer = lexer end
  for i, style in pairs(scheme) do
    for prop, value  in pairs(style) do
      if (prop == "StyleFore" or prop == "StyleBack")
         and type(value) == "string" then -- convert from string
        local hex, hex, r, g, b =
          string.find(value, "^(%x%x)(%x%x)(%x%x)$")
        value = hex and (dec(r) + dec(g)*256 + dec(b)*65536) or 0
      end
      editor[prop][i] = value
    end--each property
  end--each style
end

scite_Command('ColourTest|ColourTest|Ctrl+8')

local ColourScheme = { -- a sample colour scheme
  [1] = {StyleFore = "800000", StyleBold = true,},
  [2] = {StyleFore = "008000", StyleBack = "E0E0E0", StyleItalic = true,},
}

function ColourTest()
  local SIG = "ColourTest"
  -- colouriser function, used when buffer created or switched
  local function Colourise(n)
    local segment = n * 2
    editor:StartStyling(0, 31)
    for i = 1, 10 do
      editor:SetStyling(segment, 1)
      editor:SetStyling(segment, 2)
    end
  end
  -- recolour text again upon switching buffers
  scite_OnSwitchFile(function()
    if not buffer[SIG] then return end
    SetColours(SCLEX_CONTAINER, ColourScheme)
    Colourise(editor.Length / 100)
    return true
  end)
  -- create buffer, identify it, add some text, colourise
  scite.Open("")
  buffer[SIG] = true;
  SetColours(SCLEX_CONTAINER, ColourScheme)
  for i = 1, 100 do
    editor:AddText("The quick brown fox jumped over the lazy dog.\n")
  end
  Colourise(editor.Length / 100)
  -- since this colouring method breaks upon many operations, it
  -- cannot be used as a language syntax highlighter
  editor.ReadOnly = true
end
--]]

RecentChanges · 偏好設定
編輯 · 歷程記錄
最後編輯時間為 2006 年 9 月 5 日下午 6:12 GMT (diff)