From b63da1d89e183cfa3741aedc76062d1446fb0a66 Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Mon, 26 Nov 2007 02:45:23 -0600 Subject: [PATCH] Insert Date and Time --- moo/mooedit/menu.cfg | 15 ++++ moo/moopython/plugins/Makefile.am | 5 +- .../plugins/lib/insert_date_and_time.py | 88 +++++++++++++++++++ moo/moopython/plugins/{ => lib}/pyconsole.py | 0 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 moo/moopython/plugins/lib/insert_date_and_time.py rename moo/moopython/plugins/{ => lib}/pyconsole.py (100%) diff --git a/moo/mooedit/menu.cfg b/moo/mooedit/menu.cfg index 898cc581..9eda3eac 100644 --- a/moo/mooedit/menu.cfg +++ b/moo/mooedit/menu.cfg @@ -124,3 +124,18 @@ accel=M Left() Select(-1) end + +[tool] +id=InsertDateAndTime +type=python +name=Insert Date and Time +options=need-doc + # insert_date_and_time.py is installed in + # $prefix/lib/moo/plugins/lib + from insert_date_and_time import get_format + import time + + fmt = get_format(window) + if fmt is not None: + buffer.insert_at_cursor(time.strftime(fmt)) + diff --git a/moo/moopython/plugins/Makefile.am b/moo/moopython/plugins/Makefile.am index 54af81bb..9b6fd13a 100644 --- a/moo/moopython/plugins/Makefile.am +++ b/moo/moopython/plugins/Makefile.am @@ -29,8 +29,11 @@ moopython_plugins_DATA = \ $(ini_files) \ $(plugins) +plugins_lib_DATA = \ + lib/pyconsole.py \ + lib/insert_date_and_time.py + nobase_plugins_lib_DATA = \ - pyconsole.py \ medit/__init__.py \ medit/runpython.py diff --git a/moo/moopython/plugins/lib/insert_date_and_time.py b/moo/moopython/plugins/lib/insert_date_and_time.py new file mode 100644 index 00000000..e02ac1c3 --- /dev/null +++ b/moo/moopython/plugins/lib/insert_date_and_time.py @@ -0,0 +1,88 @@ +import gtk +import time +import moo + +# List of formats is shamelessly stolen from gedit +formats = [ + "%c", + "%x", + "%X", + "%x %X", + "%Y-%m-%d %H:%M:%S", + "%a %b %d %H:%M:%S %Z %Y", + "%a %b %d %H:%M:%S %Y", + "%a %d %b %Y %H:%M:%S %Z", + "%a %d %b %Y %H:%M:%S", + "%d/%m/%Y", + "%d/%m/%y", + "%D", + "%A %d %B %Y", + "%A %B %d %Y", + "%Y-%m-%d", + "%d %B %Y", + "%B %d, %Y", + "%A %b %d", + "%H:%M:%S", + "%H:%M", + "%I:%M:%S %p", + "%I:%M %p", + "%H.%M.%S", + "%H.%M", + "%I.%M.%S %p", + "%I.%M %p", + "%d/%m/%Y %H:%M:%S", + "%d/%m/%y %H:%M:%S", +] + +moo.utils.prefs_new_key_string('Tools/InsertDateAndTime', '%c') + +def populate_tree_view(treeview): + model = gtk.ListStore(str, str) + curtime = time.localtime() + default_iter = None + default_fmt = moo.utils.prefs_get_string('Tools/InsertDateAndTime') + + for fmt in formats: + iter = model.append([time.strftime(fmt, curtime), fmt]) + if default_fmt == fmt: + default_iter = iter + + cell = gtk.CellRendererText() + column = gtk.TreeViewColumn(None, cell, text=0) + treeview.append_column(column) + treeview.set_model(model) + if default_iter is not None: + treeview.get_selection().select_iter(default_iter) + +def get_format_from_list(treeview): + model, row = treeview.get_selection().get_selected() + fmt = model[row][1] + moo.utils.prefs_set_string('Tools/InsertDateAndTime', fmt) + return fmt + +def get_format(parent=None): + window = gtk.Dialog("Select Format", parent, 0, + (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, + gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) + window.set_size_request(250, 300) + swindow = gtk.ScrolledWindow() + treeview = gtk.TreeView() + swindow.add(treeview) + swindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) + swindow.show_all() + window.vbox.pack_start(swindow) + + populate_tree_view(treeview) + treeview.set_headers_visible(False) + treeview.connect('row-activated', lambda tv, path, clmn, dlg: + dlg.response(gtk.RESPONSE_ACCEPT), window) + + fmt = None + if window.run() == gtk.RESPONSE_ACCEPT: + fmt = get_format_from_list(treeview) + + window.destroy() + return fmt + +if __name__ == '__main__': + print get_format() diff --git a/moo/moopython/plugins/pyconsole.py b/moo/moopython/plugins/lib/pyconsole.py similarity index 100% rename from moo/moopython/plugins/pyconsole.py rename to moo/moopython/plugins/lib/pyconsole.py