commit fedf6575da16e5900c2c4d78e060ee5bedbfe898
parent 7464b527743f5f4bbd15043eaf89c4509a870a18
Author: Miles Alan <m@milesalan.com>
Date: Sun, 11 Aug 2019 21:53:02 -0500
[st][patch] externalpipe-signal: Add patch
Diffstat:
3 files changed, 189 insertions(+), 0 deletions(-)
diff --git a/st.suckless.org/patches/externalpipe-signal/externalpipe_buffer.sh b/st.suckless.org/patches/externalpipe-signal/externalpipe_buffer.sh
@@ -0,0 +1,82 @@
+#!/usr/bin/env sh
+# externalpipe_buffer.sh
+#
+# Description:
+# Script enabling keyboard-based dmenu copy/paste between multiple different
+# st/surf instances. Designed to be used in combination with the surf & st
+# externalpipe-signal patches.
+
+# Upon executing dmenu_copy/dmenu_type, SIGUSR1 is sent to st & surf.
+# St/surf then additionally call this script w/ "{surf,st}_strings_read" arg.
+# This loads up the $BUFFER_FILE (FIFO or file) with the contents filtered
+# for what should appear in dmenu. If using (BUFFER_FILE_FIFO=true), dmenu
+# is then immediatedly invoked reading from $BUFFER_FILE as STDIN; if not
+# using a FIFO, BUFFER_FILE_R_DELAY is waited on before reading file / invoking dmenu.
+
+# Highly reccomended for quick feedback: set BUFFER_FILE_FIFO=true & use dmenu
+# non_blocking_stdin patch: tools.suckless.org/dmenu/patches/non_blocking_stdin/
+#
+# Install:
+# - Add script to $PATH
+# - Use "externalpipe_buffer.sh dmenu_type" as a hotkey (in surf/st/wm).
+# - Use "externalpipe_buffer.sh st_strings_read" as st externalpipe-signal command.
+# - Use "externalpipe_buffer.sh surf_strings_read" as surf externalpipe-signal command.
+#
+# Deps:
+# xmllint, xdotool, xargs, xclip, pkill
+
+# Please adjust:
+BUFFER_FILE=/tmp/content_buffer
+BUFFER_FILE_FIFO=false
+BUFFER_FILE_R_DELAY=500 # Unused if BUFFER_FILE_FIFO=true
+
+function write_to_buffer() {
+ cat >> $BUFFER_FILE &
+}
+function dedupe_and_sort() {
+ sort | uniq | grep . | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2-
+}
+function trigger_sigusr1_and_read_buffer() {
+ if [ $BUFFER_FILE_FIFO == "true" ]
+ then
+ test ! -p $BUFFER_FILE && rm -f $BUFFER_FILE && mkfifo $BUFFER_FILE
+ pkill -USR1 surf
+ pkill -USR1 st
+ else
+ test ! -f $BUFFER_FILE && rm -f $BUFFER_FILE && touch $BUFFER_FILE
+ pkill -USR1 surf
+ pkill -USR1 st
+ echo $BUFFER_FILE_R_DELAY | xargs -IN echo N/1000 | bc -l | xargs sleep
+ fi
+
+ cat $BUFFER_FILE
+ rm $BUFFER_FILE
+}
+function dm() { dmenu "$@" -l 10 -i -w $(xdotool getactivewindow) | sed 's/↵/\r/g'; }
+
+function st_strings_read() {
+ INPUT=$(cat)
+ echo "$(
+ # General Strings, quoted string, and whole lines
+ echo "$INPUT" | grep -Eo '\S+' | tr -d '[:blank:]'
+ echo "$INPUT" | grep -oP '"[^"]+"' | tr -d '"'
+ echo "$INPUT" | sed 's/^ *[0-9]\+.//g' | awk '{$1=$1};1'
+ )" |
+ dedupe_and_sort |
+ write_to_buffer
+}
+function surf_strings_read() {
+ awk '{printf "%sNEWLINE_REPLACE", $0} END {printf "\n"}' |
+ xmllint --html --xpath "//*" - |
+ awk '{ gsub("<[^>]*>", ""); print($0); }' |
+ sed 's/NEWLINE_REPLACE/↵/g' |
+ awk '{ gsub("<[^>]*>",""); print $0 }' |
+ sed 's/</</g' |
+ sed 's/>/>/g' |
+ dedupe_and_sort |
+ write_to_buffer
+}
+function dmenu_copy() { trigger_sigusr1_and_read_buffer | dm -p 'Screen Copy' | xclip -i; }
+function dmenu_type() { trigger_sigusr1_and_read_buffer | dm -p 'Screen Type' | xargs xdotool type --delay 0; }
+
+$1
diff --git a/st.suckless.org/patches/externalpipe-signal/index.md b/st.suckless.org/patches/externalpipe-signal/index.md
@@ -0,0 +1,33 @@
+externalpipe-signal
+===================
+
+Description
+-----------
+
+Run an externalpipe command upon receiving the SIGUSR1 signal. This is helpful
+for supporting externalpipe scripts which work across multiple st instances.
+With the example script you can access a dmenu populated with strings from all
+open st instances.
+
+Apply this patch on top of st [externalpipe](/patches/externalpipe).
+
+Example
+-------
+Add the example script to your `$PATH`:
+- [externalpipe_buffer.sh](externalpipe_buffer.sh)
+
+Add to your `config.h`:
+ char *externalpipe_sigusr1[] = {"/bin/sh", "-c", "externalpipe_buffer.sh st_strings_read"};
+
+Add to your WM as a hotkey:
+ externalpipe_buffer.sh dmenu_type
+
+Download
+--------
+
+* [st-externalpipe-signal-0.8.2.diff](st-externalpipe-signal-0.8.2.diff)
+
+Author
+------
+
+* Miles Alan - m@milesalan.com
diff --git a/st.suckless.org/patches/externalpipe-signal/st-externalpipe-signal-0.8.2.diff b/st.suckless.org/patches/externalpipe-signal/st-externalpipe-signal-0.8.2.diff
@@ -0,0 +1,74 @@
+From df32a82e8a889629ed406ce468f24c35da7e9a03 Mon Sep 17 00:00:00 2001
+From: Miles Alan <m@milesalan.com>
+Date: Sun, 11 Aug 2019 21:33:55 -0500
+Subject: [PATCH] Add handler for SIGUSR1 signal to run an externalpipe command
+
+---
+ config.def.h | 1 +
+ st.c | 14 ++++++++++++++
+ st.h | 1 +
+ 3 files changed, 16 insertions(+)
+
+diff --git a/config.def.h b/config.def.h
+index 0e01717..96d1bdd 100644
+--- a/config.def.h
++++ b/config.def.h
+@@ -1,3 +1,4 @@
++char *externalpipe_sigusr1[] = {"/bin/sh", "-c", "externalpipe_buffer.sh st_strings_read"};
+ /* See LICENSE file for copyright and license details. */
+
+ /*
+diff --git a/st.c b/st.c
+index b8e6077..48cae2e 100644
+--- a/st.c
++++ b/st.c
+@@ -155,6 +155,7 @@ typedef struct {
+ static void execsh(char *, char **);
+ static void stty(char **);
+ static void sigchld(int);
++static void sigusr1(int);
+ static void ttywriteraw(const char *, size_t);
+
+ static void csidump(void);
+@@ -719,6 +720,13 @@ execsh(char *cmd, char **args)
+ _exit(1);
+ }
+
++void
++sigusr1(int unused)
++{
++ static Arg a = {.v = externalpipe_sigusr1};
++ externalpipe(&a);
++}
++
+ void
+ sigchld(int a)
+ {
+@@ -765,6 +773,12 @@ stty(char **args)
+ int
+ ttynew(char *line, char *cmd, char *out, char **args)
+ {
++ static struct sigaction sa;
++ sa.sa_handler = sigusr1;
++ sigemptyset(&sa.sa_mask);
++ sa.sa_flags = SA_RESTART;
++ sigaction(SIGUSR1, &sa, NULL);
++
+ int m, s;
+
+ if (out) {
+diff --git a/st.h b/st.h
+index 38c61c4..794b4ff 100644
+--- a/st.h
++++ b/st.h
+@@ -111,6 +111,7 @@ void *xrealloc(void *, size_t);
+ char *xstrdup(char *);
+
+ /* config.h globals */
++extern char *externalpipe_sigusr1[];
+ extern char *utmp;
+ extern char *stty_args;
+ extern char *vtiden;
+--
+2.19.2
+