2014-02-05 09:57:11 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-02-17 17:55:48 +01:00
|
|
|
major_version=3
|
|
|
|
cache_dir=/tmp/clipmenu.$major_version.$USER/
|
2017-01-11 12:33:14 +01:00
|
|
|
cache_file=$cache_dir/line_cache
|
2017-02-18 02:39:25 +01:00
|
|
|
lock_file=$cache_dir/lock
|
|
|
|
lock_timeout=2
|
2017-01-11 12:33:14 +01:00
|
|
|
|
2017-03-17 02:14:06 +01:00
|
|
|
_xsel() {
|
|
|
|
xsel --logfile /dev/null "$@"
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:03:13 +01:00
|
|
|
get_first_line() {
|
|
|
|
# Args:
|
|
|
|
# - $1, the file or data
|
|
|
|
# - $2, optional, the line length limit
|
|
|
|
|
|
|
|
data=${1?}
|
|
|
|
line_length_limit=${2-300}
|
|
|
|
|
|
|
|
# We look for the first line matching regex /./ here because we want the
|
|
|
|
# first line that can provide reasonable context to the user. That is, if
|
|
|
|
# you have 5 leading lines of whitespace, displaying " (6 lines)" is much
|
|
|
|
# less useful than displaying "foo (6 lines)", where "foo" is the first
|
|
|
|
# line in the entry with actionable context.
|
|
|
|
awk -v limit="$line_length_limit" '
|
|
|
|
BEGIN { printed = 0; }
|
|
|
|
|
|
|
|
printed == 0 && NF {
|
|
|
|
$0 = substr($0, 0, limit);
|
|
|
|
printf("%s", $0);
|
|
|
|
printed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
|
|
|
if (NR > 1) {
|
|
|
|
print " (" NR " lines)";
|
|
|
|
} else {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}' <<< "$data"
|
2016-11-02 15:49:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
debug() {
|
|
|
|
if (( DEBUG )); then
|
|
|
|
printf '%s\n' "$@" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-06-22 17:50:10 +02:00
|
|
|
# It's ok that this only applies to the final directory.
|
|
|
|
# shellcheck disable=SC2174
|
2015-07-28 05:16:13 +02:00
|
|
|
mkdir -p -m0700 "$cache_dir"
|
2014-02-05 09:57:11 +01:00
|
|
|
|
2014-02-05 11:51:48 +01:00
|
|
|
declare -A last_data
|
2015-10-07 20:03:23 +02:00
|
|
|
declare -A last_filename
|
2014-02-05 11:51:48 +01:00
|
|
|
|
2017-02-18 02:39:25 +01:00
|
|
|
exec {lock_fd}> "$lock_file"
|
|
|
|
|
2015-09-06 02:43:38 +02:00
|
|
|
while sleep "${CLIPMENUD_SLEEP:-0.5}"; do
|
2017-02-18 02:39:25 +01:00
|
|
|
if ! flock -x -w "$lock_timeout" "$lock_fd"; then
|
2017-02-18 02:45:06 +01:00
|
|
|
if (( ONESHOT )); then
|
|
|
|
printf 'ERROR: %s\n' 'Timed out waiting for lock' >&2
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
printf 'ERROR: %s\n' \
|
|
|
|
'Timed out waiting for lock, skipping this run' >&2
|
|
|
|
continue
|
|
|
|
fi
|
2017-02-18 02:39:25 +01:00
|
|
|
fi
|
|
|
|
|
2014-02-05 10:45:23 +01:00
|
|
|
for selection in clipboard primary; do
|
2017-03-17 02:14:06 +01:00
|
|
|
data=$(_xsel -o --"$selection"; printf x)
|
2015-02-08 18:36:06 +01:00
|
|
|
|
2016-11-02 15:49:00 +01:00
|
|
|
debug "Data before stripping: $data"
|
|
|
|
|
2015-07-28 05:19:41 +02:00
|
|
|
# We add and remove the x so that trailing newlines are not stripped.
|
|
|
|
# Otherwise, they would be stripped by the very nature of how POSIX
|
|
|
|
# defines command substitution.
|
2014-02-05 10:45:23 +01:00
|
|
|
data=${data%x}
|
2014-02-05 11:02:39 +01:00
|
|
|
|
2016-11-02 15:49:00 +01:00
|
|
|
debug "Data after stripping: $data"
|
|
|
|
|
2016-11-07 09:45:31 +01:00
|
|
|
if [[ $data != *[^[:space:]]* ]]; then
|
2016-11-02 15:49:00 +01:00
|
|
|
debug "Skipping as clipboard is only blank"
|
|
|
|
continue
|
|
|
|
fi
|
2014-02-05 11:02:39 +01:00
|
|
|
|
2016-11-02 15:49:00 +01:00
|
|
|
if [[ ${last_data[$selection]} == "$data" ]]; then
|
|
|
|
debug 'Skipping as last selection is the same as this one'
|
|
|
|
continue
|
|
|
|
fi
|
2015-10-07 20:03:23 +02:00
|
|
|
|
|
|
|
# If we were in the middle of doing a selection when the previous poll
|
|
|
|
# ran, then we may have got a partial clip.
|
|
|
|
possible_partial=${last_data[$selection]}
|
|
|
|
if [[ $possible_partial && $data == "$possible_partial"* ]]; then
|
2016-11-02 15:49:00 +01:00
|
|
|
debug "$possible_partial is a possible partial of $data"
|
|
|
|
debug "Removing ${last_filename[$selection]}"
|
2015-10-07 20:03:23 +02:00
|
|
|
rm -- "${last_filename[$selection]}"
|
|
|
|
fi
|
2014-02-05 11:51:48 +01:00
|
|
|
|
2015-10-07 20:03:23 +02:00
|
|
|
last_data[$selection]=$data
|
|
|
|
last_filename[$selection]=$filename
|
2014-02-05 10:45:23 +01:00
|
|
|
|
2017-01-06 15:53:59 +01:00
|
|
|
first_line=$(get_first_line "$data")
|
|
|
|
filename="$cache_dir/$(cksum <<< "$first_line")"
|
2016-11-02 15:49:00 +01:00
|
|
|
debug "Writing $data to $filename"
|
2015-10-07 20:03:23 +02:00
|
|
|
printf '%s' "$data" > "$filename"
|
2016-11-09 12:38:41 +01:00
|
|
|
|
2017-01-06 15:53:59 +01:00
|
|
|
debug "Writing $first_line to $cache_file"
|
|
|
|
printf '%s\n' "$first_line" >> "$cache_file"
|
2017-01-06 13:03:13 +01:00
|
|
|
|
2016-11-09 12:38:41 +01:00
|
|
|
if ! (( NO_OWN_CLIPBOARD )) && [[ $selection != primary ]]; then
|
|
|
|
# Take ownership of the clipboard, in case the original application
|
|
|
|
# is unable to serve the clipboard request (due to being suspended,
|
|
|
|
# etc).
|
|
|
|
#
|
|
|
|
# Primary is excluded from the change of ownership as applications
|
|
|
|
# sometimes act up if clipboard focus is taken away from them --
|
|
|
|
# for example, urxvt will unhilight text, which is undesirable.
|
|
|
|
#
|
|
|
|
# We can't colocate this with the above copying code because
|
|
|
|
# https://github.com/cdown/clipmenu/issues/34 requires knowing if
|
|
|
|
# we would skip first.
|
2017-03-17 02:14:06 +01:00
|
|
|
_xsel -o --"$selection" | _xsel -i --"$selection"
|
2016-11-09 12:38:41 +01:00
|
|
|
fi
|
2014-02-05 10:45:23 +01:00
|
|
|
done
|
2017-02-18 02:39:25 +01:00
|
|
|
|
|
|
|
flock -u "$lock_fd"
|
2017-02-18 02:45:06 +01:00
|
|
|
|
|
|
|
if (( ONESHOT )); then
|
|
|
|
debug 'Oneshot mode enabled, exiting'
|
|
|
|
break
|
|
|
|
fi
|
2014-02-05 09:57:11 +01:00
|
|
|
done
|