~charles/dotfiles

5afa5161712ec5f97616935454d0100158eb1ceb — Charles A. Daniels 11 months ago 76c15fd
make typora kb shortcuts idempotent
M lib/gnome-fast-shortcut/LICENSE => lib/gnome-fast-shortcut/LICENSE +1 -0
@@ 1,5 1,6 @@
MIT License

Copyright (c) 2022 Charles Daniels
Copyright (c) 2022 Damiano Bonaccorsi

Permission is hereby granted, free of charge, to any person obtaining a copy

M lib/gnome-fast-shortcut/create-kb-shortcut.py => lib/gnome-fast-shortcut/create-kb-shortcut.py +13 -10
@@ 1,6 1,12 @@
import subprocess
import argparse

# I forked this off of the upstream
# https://github.com/ozerodb/gnome-fast-shortcut at
# e174123f005e8552df4ea1cdc436740c4e09a70c to implement the following changes:
#
# * The --name value is now also used as the the key binding ID in gsettings,
#   this allows this script to be used in an idempotent way.

def _get_args():
    parser = argparse.ArgumentParser()


@@ 41,7 47,6 @@ def _main():
    key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings"
    subkey1 = key.replace(" ", ".")[:-1] + ":"
    item_s = "/" + key.replace(" ", "/").replace(".", "/") + "/"
    firstname = "custom"

    # get the current list of custom shortcuts
    get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")


@@ 51,17 56,15 @@ def _main():
    command_result = array_str.lstrip("@as")
    current = eval(command_result)

    # make sure the additional keybinding mention is no duplicate
    n = 1
    while True:
        new = item_s + firstname + str(n) + "/"
        if new in current:
            n = n + 1
        else:
            break
    new = item_s + args.name + "/"

    # add the new keybinding to the list
    current.append(new)
    if new not in current:
        # note that we explicitly overwrite in the custom-keybindings
        # subfolder, so if the reference to the entry in that subfolder is
        # already in current, we don't have to remove and replace, we can just
        # do nothing
        current.append(new)

    # create the shortcut, set the name, command and shortcut key
    cmd0 = "gsettings set " + key + ' "' + str(current) + '"'

A modules/typora-integration-x11/overlay/typora-insert-date => modules/typora-integration-x11/overlay/typora-insert-date +14 -0
@@ 0,0 1,14 @@
#!/bin/sh

set -e
set -u

WINDOW_TITLE="$(xdotool getactivewindow getwindowname)"

if ! echo "$WINDOW_TITLE" | egrep -q 'Typora$' ; then
	notify-send "foreground window is not Typora"
	exit 1
fi

sleep 1
xdotool type "$(date +'%Y-%m-%d %H:%M')"

M modules/typora-integration-x11/setup.sh => modules/typora-integration-x11/setup.sh +1 -0
@@ 8,4 8,5 @@ cd "$(dirname "$0")"
if [ -x "$(which gsettings)" ] ; then
	echo "gsettings detected, setting up Typora shortcuts..."
	python3 "$DOTFILES_LIB_DIR/gnome-fast-shortcut/create-kb-shortcut.py" --name="typora_copy_link" --command="$HOME/.local/helpers/typora-copy-link" --binding "<Primary><Shift>l"
	python3 "$DOTFILES_LIB_DIR/gnome-fast-shortcut/create-kb-shortcut.py" --name="typora_insert_date" --command="$HOME/.local/helpers/typora-insert-date" --binding "<Primary>semicolon"
fi