Scite 强制等宽字体

lua-users home
wiki

此脚本的作用与 Ctrl+F11 (使用等宽字体) 类似,但可以通过 Lua 脚本调用。需要 SciteExtMan。如果您希望使用需要等宽模式的脚本(例如 SciteTicTacToe),但平时使用比例字体,那么这个脚本会非常方便。在初始化一个强制等宽模式的脚本后立即运行 MakeMonospace,用户就不再需要按 Ctrl+F11 了。

稍加修改,就有可能自动让某些文件类型以等宽字体打开。这样就可以在等宽字体和比例字体混合的环境中工作,而无需一直按 Ctrl+F11。

该脚本通过覆盖常规样式属性来强制缓冲区进入等宽模式。它使用 extman 来挂钩 OnSwitchFile


-----------------------------------------------------------------------
-- makes a buffer monospace <khman@users.sf.net> public domain 20060906
-----------------------------------------------------------------------
-- [[
scite_Command('Make Monospace|MakeMonospace|Ctrl+8')

function MakeMonospace()
  local MonoFont, MonoSize = "Courier New", 9
  local SIG = "MakeMonospace"
  local function AllMono()
    for i = 0, 127 do
      editor.StyleFont[i] = MonoFont
      editor.StyleSize[i] = MonoSize
    end
    editor:Colourise(0, -1)
  end
  scite_OnSwitchFile(function() if buffer[SIG] then AllMono() return true end end)
  buffer[SIG] = true
  AllMono()
end
--]]


如果您想从当前编辑器属性中获取等宽字体的名称和大小,可以添加以下内容

  -- retrieve monospace font information
  local StyleMono = {}
  local monoprop = props["font.monospace"]
  for style, value in string.gfind(monoprop, "([^,:]+):([^,]+)") do
    StyleMono[style] = value
  end
  -- grab styles, assuming they are defined
  MonoFont = StyleMono.font
  MonoSize = tonumber(StyleMono.size)


如果您只是想让 Scite 在所有新缓冲区中都以等宽模式启动,可以尝试这个(需要 SciteExtMan

function ToggleMonospace()
    scite.MenuCommand(450)
    return false
end

scite_OnOpen(ToggleMonospace)

要确保新创建的文件以等宽模式启动,可以尝试

function ToggleMonospace()
  -- the buffer table is provided for user data
  if buffer and not buffer["MadeMonospace"] then
    scite.MenuCommand(IDM_MONOFONT)
    buffer["MadeMonospace"] = true
  end
end

function OpenMonospace(filename) 
  if filename ~= "" then 
    ToggleMonospace()
  end
end

-- OnOpen event (with empty filename) is generated when SciTE starts
--  with new file, but not when File->New creates another new file tab.
scite_OnOpen(OpenMonospace)  -- for opening existing file
scite_OnSavePointLeft(ToggleMonospace) -- first character typed in new file

RecentChanges · preferences
编辑 · 历史
最后编辑于 2010年3月9日 上午12:49 GMT (差异)