From c83b424248dcc27d89dfb02bf4eb323f33c8fc34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 28 Mar 2022 00:44:33 +0200 Subject: [PATCH] Add e to open-in-emacs using emacsclient. --- emacs_client.py | 8 ++++++++ main.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 emacs_client.py diff --git a/emacs_client.py b/emacs_client.py new file mode 100644 index 0000000..d7c685a --- /dev/null +++ b/emacs_client.py @@ -0,0 +1,8 @@ +import subprocess + +def navigate_emacs_to_id(item_id): + item_id = item_id.replace('"', '\\"') + return subprocess.check_call([ + 'emacsclient', '-e', '(org-id-goto "{}")'.format(item_id)], + stdout=subprocess.DEVNULL, + ) diff --git a/main.py b/main.py index 6a9596f..a3ba921 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import logging import threading import task_manager +import emacs_client APP_TITLE = "Org-Convergence" DOCS_PATH = os.environ["ORG_PATH"] @@ -24,6 +25,9 @@ gi.require_version(namespace='Adw', version='1') from gi.repository import Gtk, Polkit, GObject, Gio, Adw, Gdk class MainWindow(Gtk.Window): + __gsignals__ = { + "open-in-emacs": (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, (object, )), + } ## Setup def __init__(self, *, title, application, task_manager, with_titlebar=True): @@ -50,6 +54,7 @@ class MainWindow(Gtk.Window): self.task_list = Gtk.ListBox(name='task-list') self.scrollview.set_child(self.task_list) + self.item_rows = [] # self.main_box.props.valign = Gtk.Align.CENTER # self.main_box.props.halign = Gtk.Align.CENTER @@ -63,6 +68,19 @@ class MainWindow(Gtk.Window): self.on_task_list_ready, ) + ## Keyboard shortcuts + def open_in_emacs(self, *args): + row = self.task_list.get_selected_row() + if row is None: + return + item = self.item_rows[row.get_index()] + item_id = item.id + if item_id is None: + logging.warning("No ID found for item: {}".format(item)) + return + emacs_client.navigate_emacs_to_id(item_id) + + ## Rendering def build_agenda_task_row(self, task): row = Gtk.ListBoxRow() @@ -100,9 +118,11 @@ class MainWindow(Gtk.Window): def on_task_list_update(self, new_rows): for item in new_rows.with_hour: self.task_list.append(self.build_agenda_task_row(item)) + self.item_rows.append(item) for item in new_rows.no_hour: self.task_list.append(self.build_agenda_task_row(item)) + self.item_rows.append(item) def on_task_list_ready(self, success): self.on_ready() @@ -140,6 +160,15 @@ class Application(Gtk.Application): # PinePhone screen is 720x1440 (portrait) but, has 2x pixel density win.set_default_size(360, 720) + ## Load shortcuts + # Open in emacs + action = Gio.SimpleAction.new("open-in-emacs", None) + action.connect("activate", win.open_in_emacs) + self.add_action(action) + + self.set_accels_for_action('app.open-in-emacs', ["e"]) + + win.present()