Implementing Notepad's .LOG Feature with PyPN

Notepad has a little-known feature where if you start a file with .LOG then every time the file is loaded the current date and time will be appended to the end of the file - allowing you to use a simple text file as a sort of diary.

A feature request came in for this and, pending a decision on whether to support it directly in Programmer’s Notepad, I decided to show how it could be implemented using the latest PyPN bits:

Updated: Fixed a couple of minor bugs thanks to Jeff Rivett:

[sourcecode language=’python’] import scintilla, pn, pypn.glue, time

oldDocLoad = pypn.glue.onDocLoad

def docLoad(doc):
“”” docLoad handler to implement notepad .LOG functionality”””

# Get the edit component:
s = scintilla.Scintilla(doc)

# Get the first line:
lineLength = s.LineLength(0)
text = s.GetText(0, lineLength)

# If we have .LOG then add a blank line and then the date and time
if text.startswith(".LOG"):
    timestr = "\r\n\r\n" + time.asctime(time.localtime()) + "\r\n"
    s.AppendText(len(timestr), timestr)

    # Jump to the end of the document


pypn.glue.onDocLoad = docLoad [/sourcecode]

Just drop this code in a file called under Programmer’s Notepad\scripts and you’ll have the .LOG functionality. This all uses the very latest 2.0.9 unstable bits and the related PyPN build.