2014-02-05 09:57:11 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-05-30 10:03:35 +02:00
|
|
|
: "${CM_ONESHOT=0}"
|
|
|
|
: "${CM_OWN_CLIPBOARD=1}"
|
|
|
|
: "${CM_DEBUG=0}"
|
2017-10-24 19:14:09 +02:00
|
|
|
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
|
2017-10-24 17:14:06 +02:00
|
|
|
: "${CM_MAX_CLIPS=1000}"
|
2018-02-20 12:31:08 +01:00
|
|
|
|
|
|
|
# Shellcheck is mistaken here, this is used later as lowercase.
|
|
|
|
# shellcheck disable=SC2153
|
2018-02-19 19:21:44 +01:00
|
|
|
: "${CM_SELECTIONS=clipboard primary}"
|
2017-05-30 10:03:35 +02:00
|
|
|
|
2018-02-20 11:29:23 +01:00
|
|
|
major_version=5
|
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
|
2017-02-18 02:39:25 +01:00
|
|
|
lock_file=$cache_dir/lock
|
2018-02-20 10:29:49 +01:00
|
|
|
lock_timeout=2
|
2018-02-06 00:49:41 +01:00
|
|
|
has_clipnotify=0
|
2017-01-11 12:33:14 +01:00
|
|
|
|
2018-02-19 19:21:44 +01:00
|
|
|
# This comes from the environment, so we rely on word splitting.
|
|
|
|
# shellcheck disable=SC2206
|
|
|
|
cm_selections=( $CM_SELECTIONS )
|
|
|
|
|
|
|
|
|
2017-10-29 03:04:37 +01:00
|
|
|
xsel_log=/dev/null
|
|
|
|
for file in /proc/self/fd/2 /dev/stderr; do
|
2017-10-29 11:40:38 +01:00
|
|
|
[[ -f "$file" ]] || continue
|
|
|
|
# In Linux, it's not possible to write to a socket represented by a file
|
|
|
|
# (for example, /dev/stderr or /proc/self/fd/2). See issue #54.
|
2018-05-09 14:02:54 +02:00
|
|
|
[[ -f "$(readlink -m "$file")" ]] || continue
|
2017-10-29 03:04:37 +01:00
|
|
|
xsel_log="$file"
|
|
|
|
break
|
|
|
|
done
|
|
|
|
|
2017-03-17 02:14:06 +01:00
|
|
|
_xsel() {
|
2017-10-29 03:04:37 +01:00
|
|
|
timeout 1 xsel --logfile "$xsel_log" "$@"
|
2017-03-17 02:14:06 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-03-19 08:45:36 +01:00
|
|
|
if (( CM_DEBUG )); then
|
2016-11-02 15:49:00 +01:00
|
|
|
printf '%s\n' "$@" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:21:44 +01:00
|
|
|
element_in() {
|
|
|
|
local item element
|
|
|
|
item="$1"
|
|
|
|
for element in "${@:2}"; do
|
|
|
|
if [[ "$item" == "$element" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-05-31 21:01:56 +02:00
|
|
|
if [[ $1 == --help ]] || [[ $1 == -h ]]; then
|
|
|
|
cat << 'EOF'
|
|
|
|
clipmenud is the daemon that collects and caches what's on the clipboard.
|
|
|
|
when you want to select a clip.
|
|
|
|
|
|
|
|
Environment variables:
|
|
|
|
|
|
|
|
- $CM_ONESHOT: run once immediately, do not loop (default: 0)
|
|
|
|
- $CM_DEBUG: turn on debugging output (default: 0)
|
|
|
|
- $CM_OWN_CLIPBOARD: take ownership of the clipboard (default: 1)
|
2017-10-24 17:14:06 +02:00
|
|
|
- $CM_MAX_CLIPS: maximum number of clips to store, 0 for inf (default: 1000)
|
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-02-19 19:21:44 +01:00
|
|
|
- $CM_SELECTIONS: space separated list of the selections to manage (default: "clipboard primary")
|
2017-05-31 21:01:56 +02:00
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
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
|
|
|
|
2018-02-20 11:09:50 +01:00
|
|
|
declare -A last_data
|
2018-02-20 12:03:35 +01:00
|
|
|
declare -A last_filename
|
|
|
|
declare -A last_cache_file_output
|
2018-02-20 11:09:50 +01:00
|
|
|
|
2018-02-06 00:49:41 +01:00
|
|
|
command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1
|
|
|
|
|
2018-02-08 01:55:29 +01:00
|
|
|
if ! (( has_clipnotify )); then
|
|
|
|
echo "WARN: Consider installing clipnotify for better performance." >&2
|
|
|
|
echo "WARN: See https://github.com/cdown/clipnotify." >&2
|
|
|
|
fi
|
|
|
|
|
2017-02-18 02:39:25 +01:00
|
|
|
exec {lock_fd}> "$lock_file"
|
|
|
|
|
2018-02-08 02:04:47 +01:00
|
|
|
sleep_cmd=(sleep "${CM_SLEEP:-0.5}")
|
|
|
|
|
2018-02-06 00:49:41 +01:00
|
|
|
while true; do
|
|
|
|
if ! (( CM_ONESHOT )); then
|
|
|
|
if (( has_clipnotify )); then
|
2018-02-08 02:04:47 +01:00
|
|
|
# Fall back to polling if clipnotify fails
|
|
|
|
clipnotify || "${sleep_cmd[@]}"
|
2018-02-06 00:49:41 +01:00
|
|
|
else
|
|
|
|
# Use old polling method
|
2018-02-08 02:04:47 +01:00
|
|
|
"${sleep_cmd[@]}"
|
2018-02-06 00:49:41 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-02-18 02:39:25 +01:00
|
|
|
if ! flock -x -w "$lock_timeout" "$lock_fd"; then
|
2018-02-20 11:07:58 +01:00
|
|
|
if (( CM_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
|
|
|
|
|
2018-02-19 19:21:44 +01:00
|
|
|
for selection in "${cm_selections[@]}"; do
|
2018-02-20 12:03:35 +01:00
|
|
|
cache_file=${cache_file_prefix}_$selection
|
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
|
|
|
|
2018-02-20 11:09:50 +01:00
|
|
|
if [[ ${last_data[$selection]} == "$data" ]]; then
|
|
|
|
debug 'Skipping as last selection is the same as this one'
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2018-02-20 12:03:35 +01:00
|
|
|
|
2018-02-20 11:38:48 +01: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]}
|
2018-02-20 11:39:30 +01:00
|
|
|
if [[ $possible_partial && $data == "$possible_partial"* ]] ||
|
|
|
|
[[ $possible_partial && $data == *"$possible_partial" ]]; then
|
2018-02-20 11:38:48 +01:00
|
|
|
debug "$possible_partial is a possible partial of $data"
|
|
|
|
debug "Removing ${last_filename[$selection]}"
|
2018-02-20 12:03:35 +01:00
|
|
|
|
|
|
|
previous_size=$(wc -c <<< "${last_cache_file_output[$selection]}")
|
|
|
|
truncate -s -"$previous_size" "$cache_file"
|
|
|
|
|
2018-02-20 11:38:48 +01:00
|
|
|
rm -- "${last_filename[$selection]}"
|
|
|
|
fi
|
|
|
|
|
2017-01-06 15:53:59 +01:00
|
|
|
first_line=$(get_first_line "$data")
|
2017-03-19 08:29:12 +01:00
|
|
|
|
2017-10-24 17:03:02 +02:00
|
|
|
debug "New clipboard entry on $selection selection: \"$first_line\""
|
2017-03-19 08:29:12 +01:00
|
|
|
|
2018-02-21 18:23:07 +01:00
|
|
|
cache_file_output="$(date +%s%N) $first_line"
|
2018-03-09 07:32:43 +01:00
|
|
|
filename="$cache_dir/$(cksum <<< "$first_line")"
|
2018-03-12 00:58:58 +01:00
|
|
|
|
2018-04-19 12:00:21 +02:00
|
|
|
last_data[$selection]=$data
|
|
|
|
last_filename[$selection]=$filename
|
|
|
|
|
2018-03-12 00:58:58 +01:00
|
|
|
# Recover without restart if we deleted the entire clip dir.
|
|
|
|
# It's ok that this only applies to the final directory.
|
|
|
|
# shellcheck disable=SC2174
|
|
|
|
mkdir -p -m0700 "$cache_dir"
|
|
|
|
|
2018-03-09 07:32:43 +01:00
|
|
|
debug "Writing $data to $filename"
|
|
|
|
printf '%s' "$data" > "$filename"
|
2018-02-20 11:09:50 +01:00
|
|
|
|
2018-03-09 07:32:43 +01:00
|
|
|
debug "Writing $cache_file_output to $cache_file"
|
|
|
|
printf '%s\n' "$cache_file_output" >> "$cache_file"
|
2016-11-09 12:38:41 +01:00
|
|
|
|
2018-02-20 12:03:35 +01:00
|
|
|
last_cache_file_output[$selection]=$cache_file_output
|
2017-01-06 13:03:13 +01:00
|
|
|
|
2018-02-20 11:09:24 +01:00
|
|
|
if (( CM_OWN_CLIPBOARD )) && [[ $selection != primary ]] &&
|
|
|
|
element_in clipboard "${cm_selections[@]}"; 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.
|
2018-02-20 11:30:54 +01:00
|
|
|
_xsel -o --clipboard | _xsel -i --clipboard
|
2018-02-20 11:09:24 +01:00
|
|
|
fi
|
|
|
|
|
2018-02-20 11:29:23 +01:00
|
|
|
if (( CM_MAX_CLIPS )) && [[ -f $cache_file ]]; then
|
2017-10-24 22:53:54 +02:00
|
|
|
mapfile -t to_remove < <(
|
|
|
|
head -n -"$CM_MAX_CLIPS" "$cache_file" |
|
2018-02-20 12:36:00 +01:00
|
|
|
while read -r line; do cksum <<< "${line#* }"; done
|
2017-10-24 22:53:54 +02:00
|
|
|
)
|
|
|
|
num_to_remove="${#to_remove[@]}"
|
|
|
|
if (( num_to_remove )); then
|
|
|
|
debug "Removing $num_to_remove old clips"
|
|
|
|
rm -- "${to_remove[@]/#/"$cache_dir/"}"
|
|
|
|
trunc_tmp=$(mktemp)
|
|
|
|
tail -n "$CM_MAX_CLIPS" "$cache_file" | uniq > "$trunc_tmp"
|
|
|
|
mv -- "$trunc_tmp" "$cache_file"
|
|
|
|
fi
|
2017-10-24 17:14:06 +02: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
|
|
|
|
2017-03-19 08:45:36 +01:00
|
|
|
if (( CM_ONESHOT )); then
|
2017-02-18 02:45:06 +01:00
|
|
|
debug 'Oneshot mode enabled, exiting'
|
|
|
|
break
|
|
|
|
fi
|
2014-02-05 09:57:11 +01:00
|
|
|
done
|