From eaf55e9af967a6840234267a6df9bbb225f2c2d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bert=20M=C3=BCnnich?= <ber.t@posteo.de>
Date: Wed, 20 Aug 2014 11:34:05 +0200
Subject: [PATCH] Added actions for tag addition & removal to key-handler

---
 Makefile         |  2 +-
 exec/key-handler | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 04915b6..a00d366 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = git-20140818
+VERSION = git-20140820
 
 PREFIX    = /usr/local
 MANPREFIX = $(PREFIX)/share/man
diff --git a/exec/key-handler b/exec/key-handler
index ba1f513..12e82ab 100644
--- a/exec/key-handler
+++ b/exec/key-handler
@@ -11,6 +11,8 @@
 # where C/M/S indicate Ctrl/Meta(Alt)/Shift modifier states and KEY is the X
 # keysym as listed in /usr/include/X11/keysymdef.h without the "XK_" prefix.
 
+readonly TAGFILE="$HOME/.config/sxiv/tags"
+
 rotate() {
 	case "$(file -b -i "$2")" in
 	image/jpeg*) jpegtran -rotate "$1" -copy all -outfile "$2" "$2" ;;
@@ -18,6 +20,21 @@ rotate() {
 	esac
 }
 
+tag_add() {
+	>>"$TAGFILE"
+	tags=$(dmenu <"$TAGFILE" | tr '\n' ',')
+	[ -z "$tags" ] && return
+	iptckwed -a "$tags" "$@"
+	echo -n "$tags" | tr ',' '\n' | sort - "$TAGFILE" | uniq >"$TAGFILE.new"
+	mv -f "$TAGFILE"{.new,}
+}
+
+tag_del() {
+	tags=$(iptckwed -ql "$@" | cut -f 2 | tr ',' '\n' | sort | uniq | dmenu | tr '\n' ',')
+	[ -z "$tags" ] && return
+	iptckwed -r "$tags" "$@"
+}
+
 key="$1"
 shift
 
@@ -28,5 +45,7 @@ case "$key" in
 "C-comma")  for file in "$@"; do rotate 270 "$file"; done ;;
 "C-period") for file in "$@"; do rotate  90 "$file"; done ;;
 "C-slash")  for file in "$@"; do rotate 180 "$file"; done ;;
+"C-t")      tag_add "$@" ;;
+"M-T")      tag_del "$@" ;;
 esac