Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Scite: new packages and useful lua scripts

In my olden days, I would use Notepad.exe on a non-free OS, and it had a nice feature where you could press F5 to insert the datestamp. I used to even implement that hotkey in Notepad++.

I finally bothered to research if I could implement this in scite. And, when you compile scite with lua enabled, you can use lua user scripts to accomplish this! I have built a new rpm (sources and made a new fork of the Debian dpkg (sources).

I fixed up a few of the function calls in the lua bits so the package can compile against lua 5.4.4! I also enabled lua in the rpm release of scite, because it was previously disabled.

So back to the topic of the user scripts, I included my lua script in my package directly, and added a few hotkeys in the SciTEGlobal.properties file.

ext.lua.startup.script=$(SciteDefaultHome)/stackrpms.lua
command.name.8.*=Emit &UTF8 Unicode
command.subsystem.8.*=3
command.8.*=emitUnicode
command.mode.8.*=savebefore:no
command.shortcut.8.*=Ctrl+U
command.name.9.*=Insert &Date
command.subsystem.9.*=3
command.9.*=InsertDate
command.mode.9.*=savebefore:no
command.shortcut.9.*=F5
command.name.10.*=Insert Date iso8601
command.subsystem.10.*=3
command.10.*=InsertDateIso
command.mode.10.*=savebefore:no
command.shortcut.10.*=Shift+F5

The logic in the lua script for the emit unicode was ripped 100% from http://lua-users.org/wiki/SciteUnicodeInput.

I spent some time tweaking my timestamp functions, and this is the result.

-- Reference: http://lua-users.org/wiki/SciteInsertDate
-- Tags used by os.date:
--   %a abbreviated weekday name (e.g., Wed)
--   %A full weekday name (e.g., Wednesday)
--   %b abbreviated month name (e.g., Sep)
--   %B full month name (e.g., September)
--   %c date and time (e.g., 09/16/98 23:48:10)
--   %d day of the month (16) [01-31]
--   %H hour, using a 24-hour clock (23) [00-23]
--   %I hour, using a 12-hour clock (11) [01-12]
--   %M minute (48) [00-59]
--   %m month (09) [01-12]
--   %p either "am" or "pm" (pm)
--   %S second (10) [00-61]
--   %w weekday (3) [0-6 = Sunday-Saturday]
--   %x date (e.g., 09/16/98)
--   %X time (e.g., 23:48:10)
--   %Y full year (1998)
--   %y two-digit year (98) [00-99]
--   %% the character '%'
function InsertDate()
  -- I use 1-7 for dow, 1=Sunday. How unfortunate that it is one off from glibc %w value.
  local dow = tostring(os.date("%w") + 1)
  local d1 = os.date("%Y-%m-%d-")
  local d2 = os.date(" %H:%M")
  editor:AddText(d1..dow..d2)
end
function InsertDateIso()
  local date_string = os.date("%Y-%m-%dT%T")
  editor:AddText(date_string)
end

And now, these functions are available on those hotkeys in any of my installations of scite!

Comments