Add <Ctrl>e to open-in-emacs using emacsclient.

This commit is contained in:
Sergio Martínez Portela 2022-03-28 00:44:33 +02:00
parent 6c22d19c6c
commit c83b424248
2 changed files with 37 additions and 0 deletions

8
emacs_client.py Normal file
View File

@ -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,
)

29
main.py
View File

@ -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', ["<Ctrl>e"])
win.present()