zim-wiki team mailing list archive
-
zim-wiki team
-
Mailing list archive
-
Message #01789
Inserting the time with a hotkey
I use Zim to keep daily journals sometimes. I like to timestamp
entries. I want to be able to press Ctrl+T to insert the current
time, bolded. Seems simple enough, right?
But I have been through quite a rigamarole trying to make this happen.
I made a shell script that uses xdotool to send the Ctrl+B keystrokes
and type the current time in, and I bound that to Meta+T in KDE, but
this has a race condition in which the script runs before I release
Meta, and Meta ends up remaining pressed, causing my typing to
activate other hotkeys. This can cause unfortunate results if I type
quickly.
I tried in vain to find a terminal app to simply check the state of
modifier keys. If my script could wait until Meta is released, it
would be fine. Finally I found the source to a very simple C program
that uses linux/input.h functions to check the state of modifier keys.
I'm not a C programmer, but I was able to modify it slightly and
compile it to just watch the LEFTMETA key. It works great, except
that /dev/input/event* are not world-readable by default, so I have to
either run it as sudo (which is not suitable), or change the
permissions of the keyboard event device on every system at every
boot.
Then I tried to find an example of C code using Xlib to check the
status of keyboard keys, because using Xlib wouldn't require root
permissions. But all I could find was that Xlib is arcane even for
experienced C programmers.
Finally I returned to an idea I had a long time ago of adding a hotkey
in Zim to do it, like Ctrl+T. I dug around and found the code for the
Ctrl+D insert-date-time dialog in pageview.py. I copied the
InsertDateDialog class, copied the insert_date function, and added an
entry to the menu.xml file (I replaced "date" with "time"). I don't
want a dialog to open when this happens--I only want it to insert the
current time, bolded, when I press Ctrl+T.
I am so close to having it work. It took some trial and error, and a
few "Looks like you discovered a bug" dialogs, but finally I cut
everything out of the InsertTimeDialog (formerly InsertDateDialog)
class except this in __init__:
class InsertTimeDialog(Dialog):
def __init__(self, ui, buffer):
buffer.insert_at_cursor("**"+datetime.datetime.now().strftime('%H:%M')+"**")
But now, instead of "discovering a bug", when I press Ctrl+T and
activate these few lines of code, Zim instantly crashes, and there is
no terminal output at all, not even from Python!
I think it's Gtk causing the crash because I took out the
Dialog-related code. So I uncommented the lines in that class and dug
around some more and discovered toggle_format_strong(). But I don't
know how to call that from InsertTimeDialog. I tried using
buffer.toggle_format('strong'), but I get "AttributeError:
'TextBuffer' object has no attribute 'toggle_format'".
I'm so close. Please help. :)
Follow ups