2014-02-05 10:12:34 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-02-17 17:55:48 +01:00
|
|
|
major_version=3
|
|
|
|
|
2015-08-22 20:40:59 +02:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2017-02-17 17:55:48 +01:00
|
|
|
cache_dir=/tmp/clipmenu.$major_version.$USER
|
2017-01-06 15:53:59 +01:00
|
|
|
cache_file=$cache_dir/line_cache
|
2014-02-05 10:12:34 +01:00
|
|
|
|
2017-03-19 08:45:36 +01:00
|
|
|
: "${CM_LAUNCHER=dmenu}"
|
2017-03-19 08:40:19 +01:00
|
|
|
|
2017-03-19 09:00:27 +01:00
|
|
|
if [[ "$CM_LAUNCHER" == rofi ]]; then
|
|
|
|
# rofi supports dmenu-like arguments through the -dmenu flag
|
|
|
|
set -- -dmenu "$@"
|
|
|
|
fi
|
|
|
|
|
2017-03-19 08:52:55 +01:00
|
|
|
if [[ $1 == --help ]]; then
|
|
|
|
cat << EOF
|
|
|
|
clipmenu is a simple clipboard manager using dmenu and xsel. Launch this
|
|
|
|
when you want to select a clip.
|
|
|
|
|
|
|
|
All arguments are passed through to dmenu itself.
|
|
|
|
|
|
|
|
Environment variables:
|
|
|
|
|
|
|
|
- \$CM_LAUNCHER: specify a dmenu-compatible launcher (default: dmenu)
|
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2015-09-06 14:59:07 +02:00
|
|
|
# It's okay to hardcode `-l 8` here as a sensible default without checking
|
|
|
|
# whether `-l` is also in "$@", because the way that dmenu works allows a later
|
|
|
|
# argument to override an earlier one. That is, if the user passes in `-l`, our
|
|
|
|
# one will be ignored.
|
2017-03-19 08:40:19 +01:00
|
|
|
chosen_line=$(tac "$cache_file" | awk '!seen[$0]++' | "$CM_LAUNCHER" -l 8 "$@")
|
2014-02-05 10:16:42 +01:00
|
|
|
|
|
|
|
[[ $chosen_line ]] || exit 1
|
|
|
|
|
2017-01-06 15:53:59 +01:00
|
|
|
file=$cache_dir/$(cksum <<< "$chosen_line")
|
|
|
|
|
|
|
|
if ! [[ -f "$file" ]]; then
|
2017-01-06 15:02:47 +01:00
|
|
|
# We didn't find this in cache
|
2017-01-06 18:28:31 +01:00
|
|
|
printf 'FATAL: %s not in cache\n' "$chosen_line" >&2
|
2017-01-06 15:02:47 +01:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2014-02-05 10:48:03 +01:00
|
|
|
for selection in clipboard primary; do
|
2017-03-19 08:31:01 +01:00
|
|
|
xsel --logfile /dev/stderr -i --"$selection" < "$file"
|
2014-02-05 10:48:03 +01:00
|
|
|
done
|