| 1 | #!/bin/sh |
| 2 | |
| 3 | # Example for $XDG_CONFIG_HOME/sxiv/exec/key-handler |
| 4 | # Called by sxiv(1) after the external prefix key (C-x by default) is pressed. |
| 5 | # The next key combo is passed as its first argument. Passed via stdin are the |
| 6 | # images to act upon, one path per line: all marked images, if in thumbnail |
| 7 | # mode and at least one image has been marked, otherwise the current image. |
| 8 | # sxiv(1) blocks until this script terminates. It then checks which images |
| 9 | # have been modified and reloads them. |
| 10 | |
| 11 | # The key combo argument has the following form: "[C-][M-][S-]KEY", |
| 12 | # where C/M/S indicate Ctrl/Meta(Alt)/Shift modifier states and KEY is the X |
| 13 | # keysym as listed in /usr/include/X11/keysymdef.h without the "XK_" prefix. |
| 14 | |
| 15 | readonly KEY="$1"; |
| 16 | readonly TAGFILE="$HOME/.config/sxiv/tags" |
| 17 | readonly TMPFILE="/tmp/sxiv.$$" |
| 18 | |
| 19 | rotate() { |
| 20 | degree="$1" |
| 21 | tr '\n' \0' | xargs -0 realpath | sort | uniq | while read file; do |
| 22 | case "$(file -b -i "$file")" in |
| 23 | image/jpeg*) jpegtran -rotate "$degree" -copy all -outfile "$file" "$file" ;; |
| 24 | *) mogrify -rotate "$degree" "$file" ;; |
| 25 | esac |
| 26 | done |
| 27 | } |
| 28 | |
| 29 | tag_add() { |
| 30 | >>"$TAGFILE" |
| 31 | tags=$(dmenu <"$TAGFILE" | tr '\n' ',') |
| 32 | [ -z "$tags" ] && return |
| 33 | iptckwed -i -a "$tags" |
| 34 | echo -n "$tags" | tr ',' '\n' | sort - "$TAGFILE" | uniq >"$TAGFILE.new" |
| 35 | mv -f "$TAGFILE"{.new,} |
| 36 | } |
| 37 | |
| 38 | tag_del() { |
| 39 | cat >"$TMPFILE" |
| 40 | tags=$(iptckwed -iql <"$TMPFILE" | cut -f 2 | tr ',' '\n' | sort | uniq | dmenu | tr '\n' ',') |
| 41 | [ -z "$tags" ] && return |
| 42 | iptckwed -i -r "$tags" <"$TMPFILE" |
| 43 | rm -f "$TMPFILE" |
| 44 | } |
| 45 | |
| 46 | case "$KEY" in |
| 47 | "C-c") tr '\n' ' ' | xsel -i ;; |
| 48 | "C-e") while read file; do urxvt -bg "#444" -fg "#eee" -sl 0 -title "$file" -e sh -c "exiv2 pr -q -pa '$file' | less" & done ;; |
| 49 | "C-g") tr '\n' '\0' | xargs -0 gimp & ;; |
| 50 | "C-comma") rotate 270 ;; |
| 51 | "C-period") rotate 90 ;; |
| 52 | "C-slash") rotate 180 ;; |
| 53 | "C-t") tag_add ;; |
| 54 | "M-T") tag_del ;; |
| 55 | esac |
| 56 | |