2018-10-31 09:12:09 +01:00
#!/usr/bin/env bash
2014-02-05 09:57:11 +01:00
2017-05-30 10:03:35 +02:00
: "${CM_ONESHOT=0}"
2020-03-11 15:57:46 +01:00
: "${CM_OWN_CLIPBOARD=0}"
2017-05-30 10:03:35 +02:00
: "${CM_DEBUG=0}"
2017-10-24 19:14:09 +02:00
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
2018-02-20 12:31:08 +01:00
2020-03-23 18:14:01 +01:00
# Buffer to batch to avoid calling too much. Only used if CM_MAX_CLIPS >0.
2020-03-23 18:20:15 +01:00
: "${CM_MAX_CLIPS=1000}"
2019-07-09 23:37:25 +02:00
CM_MAX_CLIPS_THRESH=$(( CM_MAX_CLIPS + 100 ))
2018-02-19 19:21:44 +01:00
: "${CM_SELECTIONS=clipboard primary}"
2020-03-23 18:20:15 +01:00
read -r -a selections <<< "$CM_SELECTIONS"
2017-05-30 10:03:35 +02:00
2020-03-23 13:28:37 +01:00
major_version=6
2017-10-24 19:14:09 +02:00
cache_dir=$CM_DIR/clipmenu.$major_version.$USER/
2020-03-23 13:28:37 +01:00
cache_file=$cache_dir/line_cache
2019-05-01 17:22:48 +02:00
2020-03-23 18:20:15 +01:00
# lock_file: lock for *one* iteration of clipboard capture/propagation
# session_lock_file: lock to prevent multiple clipmenud daemons
2017-02-18 02:39:25 +01:00
lock_file=$cache_dir/lock
2019-05-01 17:22:48 +02:00
session_lock_file=$cache_dir/session_lock
2018-02-20 10:29:49 +01:00
lock_timeout=2
2018-11-08 17:39:18 +01:00
has_xdotool=0
2017-01-11 12:33:14 +01:00
2020-03-23 18:50:43 +01:00
_xsel() { timeout 1 xsel --logfile /dev/null "$@"; }
2019-07-09 23:28:16 +02:00
2020-03-23 18:14:01 +01:00
error() { printf 'ERROR: %s\n' "${1?}" >&2; }
info() { printf 'INFO: %s\n' "${1?}"; }
2019-05-01 17:26:01 +02:00
die() {
error "${2?}"
exit "${1?}"
}
2020-03-23 18:14:01 +01:00
make_line_cksums() { while read -r line; do cksum <<< "${line#* }"; done; }
2019-07-09 23:28:26 +02:00
2017-01-06 13:03:13 +01:00
get_first_line() {
data=${1?}
# We look for the first line matching regex /./ here because we want the
2020-03-23 18:14:01 +01:00
# first line that can provide reasonable context to the user.
2020-03-23 18:40:13 +01:00
awk -v limit=300 '
2017-01-06 13:03:13 +01:00
BEGIN { printed = 0; }
printed == 0 && NF {
$0 = substr($0, 0, limit);
printf("%s", $0);
printed = 1;
}
END {
2020-03-23 18:45:33 +01:00
if (NR > 1)
printf(" (%d lines)", NR);
printf("\n");
2017-01-06 13:03:13 +01:00
}' <<< "$data"
2016-11-02 15:49:00 +01:00
}
2020-03-23 18:14:01 +01:00
debug() { (( CM_DEBUG )) && printf '%s\n' "$@" >&2; }
2016-11-02 15:49:00 +01:00
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_DEBUG: turn on debugging output (default: 0)
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)
2020-03-07 13:34:46 +01:00
- $CM_MAX_CLIPS: soft maximum number of clips to store, 0 for inf. At $CM_MAX_CLIPS + 100, the number of clips is reduced to $CM_MAX_CLIPS (default: 1000)
2018-08-21 21:39:17 +02:00
- $CM_ONESHOT: run once immediately, do not loop (default: 0)
2020-03-11 15:57:46 +01:00
- $CM_OWN_CLIPBOARD: take ownership of the clipboard. Note: this may cause missed copies if some other application also handles the clipboard directly (default: 0)
2018-02-19 19:21:44 +01:00
- $CM_SELECTIONS: space separated list of the selections to manage (default: "clipboard primary")
2018-11-08 17:39:18 +01:00
- $CM_IGNORE_WINDOW: disable recording the clipboard in windows where the windowname matches the given regex (e.g. a password manager), do not ignore any windows if unset or empty (default: unset)
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
2019-05-01 17:22:48 +02:00
exec {session_lock_fd}> "$session_lock_file"
2019-05-01 17:27:24 +02:00
flock -x -n "$session_lock_fd" ||
die 2 "Can't lock session file -- is another clipmenud running?"
2019-05-01 17:22:48 +02:00
2018-02-20 11:09:50 +01:00
declare -A last_data
2018-02-20 12:03:35 +01:00
declare -A last_cache_file_output
2018-02-20 11:09:50 +01:00
2020-03-23 18:05:59 +01:00
command -v clipnotify >/dev/null 2>&1 || die 2 "clipnotify not in PATH"
2018-11-08 17:39:18 +01:00
command -v xdotool >/dev/null 2>&1 && has_xdotool=1
if [[ $CM_IGNORE_WINDOW ]] && ! (( has_xdotool )); then
echo "WARN: CM_IGNORE_WINDOW does not work without xdotool, which is not installed" >&2
fi
2017-02-18 02:39:25 +01:00
exec {lock_fd}> "$lock_file"
2018-02-06 00:49:41 +01:00
while true; do
2020-03-23 18:05:59 +01:00
(( CM_ONESHOT )) || clipnotify
2018-02-06 00:49:41 +01:00
2018-11-08 17:39:18 +01:00
if [[ $CM_IGNORE_WINDOW ]] && (( has_xdotool )); then
windowname="$(xdotool getactivewindow getwindowname)"
if [[ "$windowname" =~ $CM_IGNORE_WINDOW ]]; then
debug "ignoring clipboard because windowname \"$windowname\" matches \"${CM_IGNORE_WINDOW}\""
continue
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
2019-05-01 17:26:01 +02:00
die 1 "Timed out waiting for lock"
2018-02-20 11:07:58 +01:00
else
2019-05-01 17:26:01 +02:00
error "Timed out waiting for lock, skipping this iteration"
2018-02-20 11:07:58 +01:00
continue
fi
2017-02-18 02:39:25 +01:00
fi
2020-03-23 18:20:15 +01:00
for selection in "${selections[@]}"; do
2017-03-17 02:14:06 +01:00
data=$(_xsel -o --"$selection"; printf x)
2020-03-23 18:23:36 +01:00
data=${data%x} # avoid trailing newlines being stripped
2014-02-05 11:02:39 +01:00
2020-03-23 18:23:36 +01:00
[[ $data == *[^[:space:]]* ]] || continue
[[ ${last_data[$selection]} == "$data" ]] && continue
2018-02-20 11:09:50 +01:00
2018-02-20 11:38:48 +01:00
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
2020-03-23 18:10:43 +01:00
# Don't actually remove the file yet, because it might be
# referenced by an older entry. These will be dealt with at vacuum.
2020-03-23 17:01:11 +01:00
debug "$selection: $possible_partial is a possible partial of $data"
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
fi
2017-01-06 15:53:59 +01:00
first_line=$(get_first_line "$data")
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")"
2020-03-23 18:20:15 +01:00
last_cache_file_output[$selection]=$cache_file_output
2018-04-19 12:00:21 +02:00
last_data[$selection]=$data
2018-03-09 07:32:43 +01:00
debug "Writing $data to $filename"
printf '%s' "$data" > "$filename"
debug "Writing $cache_file_output to $cache_file"
printf '%s\n' "$cache_file_output" >> "$cache_file"
2016-11-09 12:38:41 +01:00
2020-03-23 18:02:59 +01:00
if (( CM_OWN_CLIPBOARD )) && [[ $selection == clipboard ]]; then
2020-03-23 18:15:52 +01:00
# Only clipboard, since apps like urxvt will unhilight for PRIMARY
2018-02-20 11:30:54 +01:00
_xsel -o --clipboard | _xsel -i --clipboard
2018-02-20 11:09:24 +01:00
fi
2014-02-05 10:45:23 +01:00
done
2017-02-18 02:39:25 +01:00
2020-03-23 18:55:06 +01:00
if (( CM_MAX_CLIPS )) && (( "$(wc -l < "$cache_file")" > CM_MAX_CLIPS_THRESH )); then
2020-03-23 16:59:42 +01:00
info "Trimming clip cache to CM_MAX_CLIPS ($CM_MAX_CLIPS)"
trunc_tmp=$(mktemp)
tail -n "$CM_MAX_CLIPS" "$cache_file" | uniq > "$trunc_tmp"
mv -- "$trunc_tmp" "$cache_file"
# Vacuum up unreferenced clips. They may either have been
# unreferenced by the above CM_MAX_CLIPS code, or they may be old
# possible partials.
2020-03-23 17:25:00 +01:00
debug "Vacuuming unreferenced clip files"
declare -A cksums
2020-03-23 16:59:42 +01:00
while IFS= read -r line; do
cksum=$(cksum <<< "$line")
cksums["$cksum"]="$line"
done < <(cut -d' ' -f2- < "$cache_file")
2020-03-23 17:25:00 +01:00
num_vacuumed=0
2020-03-23 16:59:42 +01:00
for file in "$cache_dir"/[012346789]*; do
cksum=${file##*/}
2020-03-23 18:52:57 +01:00
if [[ ${cksums["$cksum"]-_missing_} == _missing_ ]]; then
2020-03-23 16:59:42 +01:00
debug "Vacuuming due to lack of reference: $file"
2020-03-23 17:25:00 +01:00
(( ++num_vacuumed ))
2020-03-23 16:59:42 +01:00
rm -- "$file"
fi
done
2020-03-23 17:25:00 +01:00
unset cksums
info "Vacuumed $num_vacuumed clip files."
2020-03-23 16:59:42 +01:00
fi
2017-02-18 02:39:25 +01:00
flock -u "$lock_fd"
2017-02-18 02:45:06 +01:00
2020-03-23 18:41:50 +01:00
(( CM_ONESHOT )) && break
2014-02-05 09:57:11 +01:00
done