2018-10-31 09:12:09 +01:00
|
|
|
#!/usr/bin/env bash
|
2014-02-05 10:12:34 +01:00
|
|
|
|
2017-05-30 10:03:35 +02:00
|
|
|
: "${CM_LAUNCHER=dmenu}"
|
2017-10-24 19:14:09 +02:00
|
|
|
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
|
2018-08-21 15:09:25 +02:00
|
|
|
: "${CM_HISTLENGTH=8}"
|
2017-05-30 10:03:35 +02:00
|
|
|
|
2018-02-20 11:29:23 +01:00
|
|
|
major_version=5
|
2017-02-17 17:55:48 +01:00
|
|
|
|
2015-08-22 20:40:59 +02:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2017-10-24 19:14:09 +02:00
|
|
|
cache_dir=$CM_DIR/clipmenu.$major_version.$USER
|
2018-02-20 11:29:23 +01:00
|
|
|
cache_file_prefix=$cache_dir/line_cache
|
2014-02-05 10:12:34 +01:00
|
|
|
|
2017-05-31 21:01:56 +02:00
|
|
|
if [[ $1 == --help ]] || [[ $1 == -h ]]; then
|
|
|
|
cat << 'EOF'
|
2017-03-19 08:52:55 +01:00
|
|
|
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:
|
|
|
|
|
2017-10-24 19:14:09 +02:00
|
|
|
- $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
|
2018-08-21 21:36:54 +02:00
|
|
|
- $CM_HISTLENGTH: specify the number of lines to show in dmenu/rofi (default: 8)
|
2018-08-21 21:39:17 +02:00
|
|
|
- $CM_LAUNCHER: specify a dmenu-compatible launcher (default: dmenu)
|
2017-03-19 08:52:55 +01:00
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2017-05-31 21:01:56 +02:00
|
|
|
if [[ "$CM_LAUNCHER" == rofi ]]; then
|
|
|
|
# rofi supports dmenu-like arguments through the -dmenu flag
|
|
|
|
set -- -dmenu "$@"
|
|
|
|
fi
|
|
|
|
|
2018-03-09 21:50:48 +01:00
|
|
|
list_clips() {
|
|
|
|
cat "$cache_file_prefix"_* /dev/null | LC_ALL=C sort -rnk 1 | cut -d' ' -f2- | awk '!seen[$0]++'
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ "$CM_LAUNCHER" == rofi-script ]]; then
|
|
|
|
if ! (( $# )); then
|
|
|
|
list_clips
|
|
|
|
exit
|
|
|
|
else
|
|
|
|
# https://github.com/koalaman/shellcheck/issues/1141
|
|
|
|
# shellcheck disable=SC2124
|
|
|
|
chosen_line="${@: -1}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
chosen_line=$(
|
2018-08-21 15:09:25 +02:00
|
|
|
list_clips | "$CM_LAUNCHER" -l "${CM_HISTLENGTH}" "$@"
|
2018-03-09 21:50:48 +01:00
|
|
|
)
|
|
|
|
fi
|
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
|
2018-04-10 12:07:01 +02:00
|
|
|
printf 'FATAL: %s not in cache (%s missing)\n' "$chosen_line" "$file" >&2
|
|
|
|
printf 'Please report the following debug information:\n\n' >&2
|
|
|
|
wc -l "$cache_file_prefix"_* >&2
|
|
|
|
grep -nFR "$chosen_line" "$cache_dir" >&2
|
|
|
|
stat "$file" >&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
|
2018-05-10 18:13:46 +02:00
|
|
|
xsel --logfile /dev/null -i --"$selection" < "$file"
|
2014-02-05 10:48:03 +01:00
|
|
|
done
|