commit c21c6ec3a3d283e54c8329ed4dcb254d66750f4b
Author: Miles Alan <[email protected]>
Date:   Sun Aug 11 22:07:29 2019 -0500

    [surf][patch] externalpipe-signal: Add patch

diff --git 
a/surf.suckless.org/patches/externalpipe-signal/externalpipe_buffer.sh 
b/surf.suckless.org/patches/externalpipe-signal/externalpipe_buffer.sh
new file mode 100755
index 00000000..3cb0175a
--- /dev/null
+++ b/surf.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/↵/
/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 "
"}' |
+    xmllint --html --xpath "//*" - |
+    awk '{ gsub("<[^>]*>", ""); print($0); }' |
+    sed 's/NEWLINE_REPLACE/↵/g' |
+    awk '{ gsub("<[^>]*>",""); print $0 }' |
+    sed 's/&lt;/</g' |
+    sed 's/&gt;/>/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/surf.suckless.org/patches/externalpipe-signal/index.md 
b/surf.suckless.org/patches/externalpipe-signal/index.md
new file mode 100644
index 00000000..2a5084fc
--- /dev/null
+++ b/surf.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 surf instances.
+With the example script you can access a dmenu populated with the contents of
+all tags contents of all open surf windows for directly pasting.
+
+Apply this patch on top of surf [externalpipe](/patches/externalpipe).
+
+Example
+-------
+Add the example script to your `$PATH`:
+- [externalpipe_buffer.sh](externalpipe_buffer.sh)
+
+Add to your `config.h`:
+           static char *externalpipe_sigusr1[] = {"/bin/sh", "-c", 
"externalpipe_buffer.sh surf_strings_read"};
+
+Add to your WM as a hotkey:
+           externalpipe_buffer.sh dmenu_type
+
+Download
+--------
+
+* [surf-externalpipe-signal-2.0.diff](surf-externalpipe-signal-2.0.diff)
+
+Author
+------
+
+* Miles Alan - [email protected]
diff --git 
a/surf.suckless.org/patches/externalpipe-signal/surf-externalpipe-signal-2.0.diff
 
b/surf.suckless.org/patches/externalpipe-signal/surf-externalpipe-signal-2.0.diff
new file mode 100644
index 00000000..647db7cb
--- /dev/null
+++ 
b/surf.suckless.org/patches/externalpipe-signal/surf-externalpipe-signal-2.0.diff
@@ -0,0 +1,66 @@
+From 84a41d036329c7599024b7cb0f613400d7484cec Mon Sep 17 00:00:00 2001
+From: Miles Alan <[email protected]>
+Date: Sun, 11 Aug 2019 21:36:58 -0500
+Subject: [PATCH] Add handler for SIGUSR1 signal to run an externalpipe command
+
+---
+ config.def.h |  1 +
+ surf.c       | 16 ++++++++++++++++
+ 2 files changed, 17 insertions(+)
+
+diff --git a/config.def.h b/config.def.h
+index 6d3135e..a7363d9 100644
+--- a/config.def.h
++++ b/config.def.h
+@@ -5,6 +5,7 @@ static char *scriptfile     = "~/.surf/script.js";
+ static char *styledir       = "~/.surf/styles/";
+ static char *cachedir       = "~/.surf/cache/";
+ static char *cookiefile     = "~/.surf/cookies.txt";
++static char *externalpipe_sigusr1[] = {"/bin/sh", "-c", 
"externalpipe_buffer.sh surf_strings_read"};
+ 
+ /* Webkit default features */
+ static Parameter defconfig[ParameterLast] = {
+diff --git a/surf.c b/surf.c
+index 93a1629..0e402ca 100644
+--- a/surf.c
++++ b/surf.c
+@@ -146,6 +146,7 @@ static void die(const char *errstr, ...);
+ static void setup(void);
+ static void sigchld(int unused);
+ static void sighup(int unused);
++static void sigusr1(int unused);
+ static char *buildfile(const char *path);
+ static char *buildpath(const char *path);
+ static const char *getuserhomedir(const char *user);
+@@ -338,6 +339,15 @@ sigchld(int unused)
+               ;
+ }
+ 
++void
++sigusr1(int unused)
++{
++  static Arg a = {.v = externalpipe_sigusr1};
++      Client *c;
++      for (c = clients; c; c = c->next)
++              externalpipe(c, &a);
++}
++
+ void
+ sighup(int unused)
+ {
+@@ -1757,6 +1767,12 @@ main(int argc, char *argv[])
+       c = newclient(NULL);
+       showview(NULL, c);
+ 
++      struct sigaction sa;
++      sa.sa_handler = sigusr1;
++      sigemptyset(&sa.sa_mask);
++      sa.sa_flags = SA_RESTART;
++      sigaction(SIGUSR1, &sa, NULL);
++
+       loaduri(c, &arg);
+       updatetitle(c);
+ 
+-- 
+2.19.2
+


Reply via email to