Page 1 of 1

Date and Time stamping

Posted: 21 July 2006, 5:49 AM
by victorf
I have been a TextPad user for many years. There is a neat feature in this editor that I would like to have in the IDE editor. In the Edit Menu, Edit > Insert > . . . there are choices for inserting Time, Short Date or Long Date into the file currently being edited. I use this often to date and timestamp files. Here are examples of each of them:
8:44 AM
7/21/2006
Friday, July 21, 2006
Is it possible to get this sort of thing in the IDE editor?

Any enlightenment will be appreciated.

Vic

Posted: 21 July 2006, 10:15 AM
by dkinzer
You can add this functionality with a bit of effort. The SciTE editor incorporates the LUA scripting language which can be used to implement this, and many other, add ons.

Step 1: Create a LUA file. We'll call it my.lua but the name can be anything. Add the following to the file:

Code: Select all

-- Insert the current time into the current buffer at the current position.
-- The format string for os.date() is identical to that of the C function strftime().
function insert_time()
    editor:InsertText(-1, os.date('%I:%M %p'))
end

-- Insert the current date, in short form, into the current buffer at the current position.
function insert_short_date()
    editor:InsertText(-1, os.date('%m/%d/%Y'))
end

-- Insert the current date, in long form, into the current buffer at the current position.
function insert_long_date()
    editor:InsertText(-1, os.date('%A, %B %d, %Y'))
end
I wrote these LUA functions to produce the date and time in the formats that you requested. The format may be changed by modifing the string parameter that is passed to os.date(). The string may contain normal characters and escape sequences (%A, %B, etc.) that are identical in semantics to those for the C function strftime().

Save this file in the SciTE User Home directory (the same directory where the User Options file is located). For Win2K, XP and later, this will be in a sub-directory of the "Documents and Settings" folder designated by the username of the logged-on user. If all else fails, load the User Options file (see step 2) and note its path prefix.

Step 2: Load the SciTE User Options file (Options menu, Open User Options File) and add the following at the end of the file:

Code: Select all

# load the custom LUA functions
ext.lua.startup.script=$(SciteUserHome)/my.lua

# command to insert the date (short form)
command.name.12.*=Insert Short Date
command.12.*=insert_short_date
command.subsystem.12.*=3
command.mode.12.*=savebefore:no
command.shortcut.12.*=Ctrl+q

# command to insert the date (long form)
command.name.13.*=Insert Long Date
command.13.*=insert_long_date
command.subsystem.13.*=3
command.mode.13.*=savebefore:no
command.shortcut.13.*=Ctrl+r

# command to insert the time
command.name.14.*=Insert Time
command.14.*=insert_time
command.subsystem.14.*=3
command.mode.14.*=savebefore:no
command.shortcut.14.*=Ctrl+s
The first line above loads the LUA file created in Step 1 above. If necessary, change the name of the file to match. The other three blocks add entries to the Tools menu. The number of each command (here 12, 13 and 14) can be any number but they must be unique among the added commands (up to 50). The string that appears on the Tools menu appears on the command.name.NN line. The LUA function to invoke appears on the command.NN line. The value for command.subsystem.NN must be 3 to designate it as a LUA function invocation. The command.shortcut.NN line assigns a keyboard shortcut for the command. This line may be omitted or commented out if no keyboard shortcut is desired. The shortcuts that I chose here might replace other useful commands. If so, change them to meet your needs. Also, note that commands 0 through 9 automatically are assigned keyboard shortcuts ctrl-0 through ctrl-9 if you don't specify a shortcut key.

After these changes are made, the IDE must be shut down and restarted. When it is run again, you should see the new entries on the Tools menu.

More information on using LUA with SciTE can be found at http://lua-users.org/wiki/UsingLuaWithScite and elsewhere on the Internet.