From 6825c302dc892cb757d644055184defe3f3121b1 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 20 Feb 2018 10:07:58 +0000 Subject: [PATCH 1/5] Revert "Remove CM_ONESHOT-specific logic in flock failure checks" This reverts commit 728d242d3c196a3fb2ed99c388581eab8edf3b6e. --- clipmenud | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/clipmenud b/clipmenud index f3f4e52..d4c4fb9 100755 --- a/clipmenud +++ b/clipmenud @@ -142,8 +142,14 @@ while true; do fi if ! flock -x -w "$lock_timeout" "$lock_fd"; then - printf 'ERROR: %s\n' 'Timed out waiting for lock' >&2 - exit 1 + 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 fi for selection in "${cm_selections[@]}"; do From 9a52f7ddba821da6d2b84cca398a483851953af5 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 20 Feb 2018 10:08:13 +0000 Subject: [PATCH 2/5] Revert "Make sure we don't break #34 in new pre-ownership" This reverts commit fe6986110197aa73f22fdcbc4f4991f8350ee8b3. --- clipmenud | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clipmenud b/clipmenud index d4c4fb9..b0efe22 100755 --- a/clipmenud +++ b/clipmenud @@ -125,10 +125,7 @@ while true; do # 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 need to check if the clipboard is empty to mitigate #34. - data=$(_xsel -o --clipboard; printf x) - [[ $data != x ]] && _xsel -i --clipboard <<< "${data%x}" + _xsel -o --clipboard | _xsel -i --clipboard fi if ! (( CM_ONESHOT )); then From 9c436084f26e9d15188b153b1b36780fe4e17502 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 20 Feb 2018 10:08:20 +0000 Subject: [PATCH 3/5] Revert "Use xsel -o/-i instead of -k" This reverts commit 47eb3d182c22e4ab2584562ef9dd215df43796dd. --- clipmenud | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clipmenud b/clipmenud index b0efe22..644d1c3 100755 --- a/clipmenud +++ b/clipmenud @@ -125,7 +125,7 @@ while true; do # 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. - _xsel -o --clipboard | _xsel -i --clipboard + _xsel -k --clipboard fi if ! (( CM_ONESHOT )); then From 52b144d970f1a2aae73059fde52e8e614b5bc3e1 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 20 Feb 2018 10:09:24 +0000 Subject: [PATCH 4/5] Revert "Take clipboard ownership prior to clipnotify" This reverts commit eb7d2b94817e8b66f62dc372a204a78a12e3ef0f. --- clipmenud | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/clipmenud b/clipmenud index 644d1c3..b7508e4 100755 --- a/clipmenud +++ b/clipmenud @@ -115,19 +115,6 @@ exec {lock_fd}> "$lock_file" sleep_cmd=(sleep "${CM_SLEEP:-0.5}") while true; do - # We need to take ownership synchronously before we run `clipnotify` as - # otherwise we could enter an infinite loop. - if (( CM_OWN_CLIPBOARD )) && 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. - _xsel -k --clipboard - fi - if ! (( CM_ONESHOT )); then if (( has_clipnotify )); then # Fall back to polling if clipnotify fails @@ -177,6 +164,22 @@ while true; do debug "Writing $first_line to $cache_file" printf '%s\n' "$first_line" >> "$cache_file" + 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. + _xsel -k --"$selection" + fi + if (( CM_MAX_CLIPS )); then mapfile -t to_remove < <( head -n -"$CM_MAX_CLIPS" "$cache_file" | From 7b4267868e6a7881e5f80509211d3a9a0411fb68 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 20 Feb 2018 10:09:50 +0000 Subject: [PATCH 5/5] Revert "Remove last_data checks" This reverts commit 55407ba3f622c99b1efafbe6b6371680f6abd8f6. --- clipmenud | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/clipmenud b/clipmenud index b7508e4..f02c272 100755 --- a/clipmenud +++ b/clipmenud @@ -103,6 +103,8 @@ fi # shellcheck disable=SC2174 mkdir -p -m0700 "$cache_dir" +declare -A last_data + command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1 if ! (( has_clipnotify )); then @@ -153,16 +155,29 @@ while true; do continue fi + if [[ ${last_data[$selection]} == "$data" ]]; then + debug 'Skipping as last selection is the same as this one' + continue + fi + + last_data[$selection]=$data + first_line=$(get_first_line "$data") debug "New clipboard entry on $selection selection: \"$first_line\"" - filename="$cache_dir/$(cksum <<< "$first_line")" - debug "Writing $data to $filename" - printf '%s' "$data" > "$filename" + # Without checking ${last_data[any]}, we often double write since both + # selections get the same content + if [[ ${last_data[any]} != "$data" ]]; then + filename="$cache_dir/$(cksum <<< "$first_line")" + debug "Writing $data to $filename" + printf '%s' "$data" > "$filename" - debug "Writing $first_line to $cache_file" - printf '%s\n' "$first_line" >> "$cache_file" + debug "Writing $first_line to $cache_file" + printf '%s\n' "$first_line" >> "$cache_file" + fi + + last_data[any]=$data if (( CM_OWN_CLIPBOARD )) && [[ $selection != primary ]] && element_in clipboard "${cm_selections[@]}"; then