From 84d49f527d9700857d933cac52284173c79afb1d Mon Sep 17 00:00:00 2001
From: Luke Smith <luke@lukesmith.xyz>
Date: Wed, 8 May 2019 11:26:11 -0400
Subject: copy output of command with alt-o

---
 Makefile   |  3 +++
 README.md  | 42 +++++++++++++++++++++---------------------
 config.h   |  3 +++
 st-copyout | 12 ++++++++++++
 st.1       |  5 +++++
 5 files changed, 44 insertions(+), 21 deletions(-)
 create mode 100755 st-copyout

diff --git a/Makefile b/Makefile
index 470ac86..a836907 100644
--- a/Makefile
+++ b/Makefile
@@ -43,7 +43,9 @@ dist: clean
 install: st
 	mkdir -p $(DESTDIR)$(PREFIX)/bin
 	cp -f st $(DESTDIR)$(PREFIX)/bin
+	cp -f st-copyout $(DESTDIR)$(PREFIX)/bin
 	chmod 755 $(DESTDIR)$(PREFIX)/bin/st
+	chmod 755 $(DESTDIR)$(PREFIX)/bin/st-copyout
 	mkdir -p $(DESTDIR)$(MANPREFIX)/man1
 	sed "s/VERSION/$(VERSION)/g" < st.1 > $(DESTDIR)$(MANPREFIX)/man1/st.1
 	chmod 644 $(DESTDIR)$(MANPREFIX)/man1/st.1
@@ -52,6 +54,7 @@ install: st
 
 uninstall:
 	rm -f $(DESTDIR)$(PREFIX)/bin/st
+	rm -f $(DESTDIR)$(PREFIX)/bin/st-copyout
 	rm -f $(DESTDIR)$(MANPREFIX)/man1/st.1
 
 .PHONY: all options clean dist install uninstall
diff --git a/README.md b/README.md
index b1ddc09..f2d131f 100644
--- a/README.md
+++ b/README.md
@@ -1,32 +1,33 @@
 # Luke's build of st - the simple (suckless) terminal
 
-The [suckless terminal (st)](https://st.suckless.org/) with some additional features:
+The [suckless terminal (st)](https://st.suckless.org/) with some additional features that make it literally the best terminal emulator ever:
+
+## Unique features (using dmenu)
+
++ *follow urls* by pressing `alt-l`
++ *copy urls* in the same way with `alt-y`
++ *copy the output of commands* with `alt-o`
+
+## Bindings for
+
++ *scrollback* with `alt-↑/↓` or `alt-pageup/down` or `shift` while scrolling the mouse
++ OR *vim-bindings*: scroll up/down in history with `alt-k` and `alt-j`. Faster with `alt-u`/`alt-d`.
++ *zoom/change font size*: same bindings as above, but holding down shift as well. `alt-home` returns to default
++ *copy text* with `alt-c`, *paste* is `alt-v` or `shift-insert`
+
+## Pretty stuff
 
 + Compatibility with `Xresources` and `pywal` for dynamic colors. The `Xdefaults` file shows a usage example.
 + Default [gruvbox](https://github.com/morhetz/gruvbox) colors otherwise.
-+ Transparency/alpha, which is also adjustable from `~/.Xresources`.
++ Transparency/alpha, which is also adjustable from your `Xresources`.
 + Default font is system "mono" at 16pt, meaning the font will match your system font.
-+ Very useful keybinds including:
-	+ Copy is alt-c, paste is alt-v or alt-p pastes from primary selection
-	+ Alt-l feeds all urls on screen to dmenu, so they user can choose and
-	  follow one (requires dmenu installed).
-	+ Alt-y does the same as above, but copies the url with xclip.
-	+ Zoom in/out or increase font size with Alt+Shift+k/j or u/d for larger intervals.
-	+ Hold alt and press either ↑/↓ or the vim keys k/j to move up/down in the terminal.
-	+ Shift+Mouse wheel do the same.
-	+ Alt-u and Alt-d scroll back/forward in history a page at a time.
-	+ Alt-PageUp and Alt-PageDown will do the same.
+
+## Other st patches
+
 + Vertcenter
 + Scrollback
 + updated to latest version 0.8.2
 
-The following additional bindings were added before I forked this:
-
-+ Scroll through history -- Shift+PageUp/PageDown or Shift+Mouse wheel
-+ Increase/decrease font size -- Shift+Alt+PageUp/PageDown
-+ Return to default font size -- Alt+Home
-+ Paste -- Shift+Insert
-
 ## Installation for newbs
 
 ```
@@ -56,8 +57,7 @@ For example, you can define your desired fonts, transparency or colors:
 ...
 ```
 
-The `alpha` value (for transparency) goes from `0` (transparent) to `1`
-(opaque).
+The `alpha` value (for transparency) goes from `0` (transparent) to `1` (opaque).
 
 ### Colors
 
diff --git a/config.h b/config.h
index be9a952..737be12 100644
--- a/config.h
+++ b/config.h
@@ -217,6 +217,8 @@ static char *copyurlcmd[] = { "/bin/sh", "-c",
     "sed 's/.*│//g' | tr -d '\n' | grep -aEo '((http|https)://|www\\.)[a-zA-Z0-9./&?=_-]*' | uniq | sed 's/^www./http:\\/\\/www\\./g' | dmenu -p 'Copy which url?' -l 10 | tr -d '\n' | xclip -selection clipboard",
     "externalpipe", NULL };
 
+static char *copyoutput[] = { "/bin/sh", "-c", "st-copyout", "externalpipe", NULL };
+
 static Shortcut shortcuts[] = {
 	/* mask                 keysym          function        argument */
 	{ XK_ANY_MOD,           XK_Break,       sendbreak,      {.i =  0} },
@@ -250,6 +252,7 @@ static Shortcut shortcuts[] = {
 	{ TERMMOD,              XK_D,           zoom,           {.f = -2} },
 	{ MODKEY,               XK_l,           externalpipe,   {.v = openurlcmd } },
 	{ MODKEY,               XK_y,           externalpipe,   {.v = copyurlcmd } },
+	{ MODKEY,               XK_o,           externalpipe,   {.v = copyoutput } },
 };
 
 /*
diff --git a/st-copyout b/st-copyout
new file mode 100755
index 0000000..e40fedd
--- /dev/null
+++ b/st-copyout
@@ -0,0 +1,12 @@
+#!/bin/sh
+# Using external pipe with st, give a dmenu prompt of recent commands,
+# allowing the user to copy the output of one.
+# xclip required for this script.
+# By Jaywalker and Luke
+tmpfile=$(mktemp /tmp/st-cmd-output.XXXXXX)
+trap 'rm "$tmpfile"' 0 1 15
+sed -n "w $tmpfile"
+ps1="$(grep "\S" "$tmpfile" | tail -n 1 | cut -d' ' -f1)"
+chosen="$(grep -F "$ps1" "$tmpfile" | sed '$ d' | tac | dmenu -p "Copy which command's output?" -i -l 10 | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
+eps1="$(echo "$ps1" | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
+awk "/^$chosen$/{p=1;print;next} p&&/^$eps1/{p=0};p" "$tmpfile" | xclip -selection clipboard
diff --git a/st.1 b/st.1
index 97cafc7..57c35f4 100644
--- a/st.1
+++ b/st.1
@@ -152,6 +152,11 @@ Show dmenu menu of all URLs on screen and choose one to open.
 .B Alt-y
 Show dmenu menu of all URLs on screen and choose one to copy.
 .TP
+.B Alt-o
+Show dmenu menu of all recently run commands and copy the output of the chosen command to the clipboard.
+.I xclip
+required.
+.TP
 .B Break
 Send a break in the serial line.
 Break key is obtained in PC keyboards
-- 
cgit v1.2.3