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:
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
s.DocumentEnd()
oldDocLoad(doc)
pypn.glue.onDocLoad = docLoad
Just drop this code in a file called dotlog.py 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.
Hi, Simon. Thanks again for this. I tried it, but for some reason it does not work. Is it necessary to do something, like enabling scripts or something like that?
I created dotlog.py, put it in [PN dir]\scripts (which I created, it wasn’t there), closed PN, run PN, open a log, and nothing happened.
Cheers,
Guillermo
Hi Guillermo, have you installed PyPN? See here:
http://pnotepad.org/docs/howto/install_pypn
Note that when running pn –findexts on Vista you may need to do so from an admin command prompt.
Right. Thanks. Why did I think that it was included by default? I’ll do it tomorrow. It will sure work as you say.
Regards,
Guillermo
Simon, I had forgotten to mention this, and I was reminded by the recent 840-devel build. I wasn’t able to have this work. I have Python 2.52, and PyPN for it(pypn-0.9.839-py25.zip). The scripts windows does ot list the dotlog script. It does list the Hello and Text ones, and they work fine. I don’t speak Python, so I wasn’t able to modify your dotlog.py
I have deleted the dotlog.py, since there is a twinkling of the windows when starting PN, which makes me suspect there are some error messages from Python trying to come through… By the way, it’s XP Pro.
Why didn’t you use Perl?;-)
I continue using PN for my fortran, perl, html and general text editing… (not latex, though…)
Hi, the dotlog script isn’t meant to show up in that window, as it’s not something you need to run manually. It just looks at every file you open automatically and checks if it needs to do its stuff.
If python is showing error messages, they should show up in the output window – I’d be interested to see them.
Thanks for the new build!
> the dotlog script isn’t meant to show up in that window,
OK.
> as it’s not something you need to run manually. It just looks at every file you open automatically
Certainly. I understand that.
> If python is showing error messages,
No messages in the output window (which works, the Hello script writes there).
I installed the new build and there are two changes from my previous report: 1) no more flickering of the screen when loading a file with the .LOG line. 2) the script is doing something, since the cursor is sent to the end of the file (as expected). No date insertion, though. This is strange…
I will update to Python 2.6 and let you know if anything changes. Not today.
Cheers. Thanks again.
Update to Python 2.6: PN gives an error when starting (can’t find python26.dll). I tried the default installation for Python, same. I removed pypn (for 2.6, b853) and reinstalled, same. I reinstalled PN b853, same.
I reverted to Python 2.5.2. No error. Scripts work. .LOG does not insert date (but moves to the EOF). BUT I REALIZED THAT THERE IS PROBABLY AN ERROR IN THE SCRIPT!! I wish I knew some Python to solve this myself. Look at the following situations, which I found trying to minimalize the behavior:
1. The logfile contains the text .LOG in the first line, plus some lines below (either empty, or with text. In this case, the dotlog script does not work.
2. The logfile contains JUST the text .LOG and nothing else, NOT EVEN A NEWLINE at the end of the first (and only) line. In this case, the dotlog script works as expected, inserting a blank line, the date, another blank line, and moving the cursor to this last!!
There is surely a trivial bug in the sript…
Guillermo