Scite Make Monospace

lua-users home

This script works like Ctrl+F11 (Use Monospace Font) except that it can be called by Lua scripts. SciteExtMan is required. This script is convenient if a user wants to use scripts that need monospace mode (such as SciteTicTacToe) but otherwise uses proportional fonts at other times. By running MakeMonospace right after initializing a monospace-mandatory script, the user no longer has to hit Ctrl+F11.

With a bit more code, it should be possible to automatically make certain file types open in monospace. This would allow for a mixed monospace-proportional font environment without the hassle of hitting Ctrl+F11 all the time.

The script forces a buffer into monospace mode by overriding the usual style properties. It uses extman to hook to OnSwitchFile.

-- makes a buffer monospace <> 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
    editor:Colourise(0, -1)
  scite_OnSwitchFile(function() if buffer[SIG] then AllMono() return true end end)
  buffer[SIG] = true

If you want to get the monospace font name and size from the current editor properties, you can add the following:

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

If you just want scite to start all new buffers in monospace mode, try this (requires SciteExtMan):

function ToggleMonospace()
    return false


To ensure newly created files start in monospace mode, try

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

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

-- 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
edit · history
Last edited March 9, 2010 5:49 am GMT (diff)