Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.7 KiB

  1. #!/usr/bin/env bash
  2. : "${CM_ONESHOT=0}"
  3. : "${CM_OWN_CLIPBOARD=0}"
  4. : "${CM_SYNC_PRIMARY_TO_CLIPBOARD=0}"
  5. : "${CM_DEBUG=0}"
  6. : "${CM_MAX_CLIPS:=1000}"
  7. # Buffer to batch to avoid calling too much. Only used if CM_MAX_CLIPS >0.
  8. CM_MAX_CLIPS_THRESH=$(( CM_MAX_CLIPS + 10 ))
  9. : "${CM_SELECTIONS:=clipboard primary}"
  10. read -r -a selections <<< "$CM_SELECTIONS"
  11. cache_dir=$(clipctl cache-dir)
  12. cache_file=$cache_dir/line_cache
  13. status_file=$cache_dir/status
  14. # lock_file: lock for *one* iteration of clipboard capture/propagation
  15. # session_lock_file: lock to prevent multiple clipmenud daemons
  16. lock_file=$cache_dir/lock
  17. session_lock_file=$cache_dir/session_lock
  18. lock_timeout=2
  19. has_xdotool=0
  20. _xsel() { timeout 1 xsel --logfile /dev/null "$@"; }
  21. error() { printf 'ERROR: %s\n' "${1?}" >&2; }
  22. info() { printf 'INFO: %s\n' "${1?}"; }
  23. die() {
  24. error "${2?}"
  25. exit "${1?}"
  26. }
  27. make_line_cksums() { while read -r line; do cksum <<< "${line#* }"; done; }
  28. get_first_line() {
  29. data=${1?}
  30. # We look for the first line matching regex /./ here because we want the
  31. # first line that can provide reasonable context to the user.
  32. awk -v limit=300 '
  33. BEGIN { printed = 0; }
  34. printed == 0 && NF {
  35. $0 = substr($0, 0, limit);
  36. printf("%s", $0);
  37. printed = 1;
  38. }
  39. END {
  40. if (NR > 1)
  41. printf(" (%d lines)", NR);
  42. printf("\n");
  43. }' <<< "$data"
  44. }
  45. debug() { (( CM_DEBUG )) && printf '%s\n' "$@" >&2; }
  46. sig_disable() {
  47. info "Received disable signal, suspending clipboard capture"
  48. _CM_DISABLED=1
  49. _CM_FIRST_DISABLE=1
  50. echo "disabled" > "$status_file"
  51. [[ -v _CM_CLIPNOTIFY_PID ]] && kill "$_CM_CLIPNOTIFY_PID"
  52. }
  53. sig_enable() {
  54. if ! (( _CM_DISABLED )); then
  55. info "Received enable signal but we're not disabled, so doing nothing"
  56. return
  57. fi
  58. # Still store the last data so we don't end up eventually putting it in the
  59. # clipboard if it wasn't changed
  60. for selection in "${selections[@]}"; do
  61. data=$(_xsel -o --"$selection"; printf x)
  62. last_data_sel[$selection]=${data%x}
  63. done
  64. info "Received enable signal, resuming clipboard capture"
  65. _CM_DISABLED=0
  66. echo "enabled" > "$status_file"
  67. }
  68. kill_background_jobs() {
  69. # While we usually _are_, there are no guarantees that we're the process
  70. # group leader. As such, all we can do is look at the pending jobs. Bash
  71. # avoids a subshell here, so the job list is in the right shell.
  72. local bg
  73. bg=$(jobs -p)
  74. # Don't log `kill' failures, since with KillMode=control-group, we're
  75. # racing with init.
  76. [[ $bg ]] && kill -- "$bg" 2>/dev/null
  77. }
  78. if [[ $1 == --help ]] || [[ $1 == -h ]]; then
  79. cat << 'EOF'
  80. clipmenud collects and caches what's on the clipboard. You can manage its
  81. operation with clipctl.
  82. Environment variables:
  83. - $CM_DEBUG: turn on debugging output (default: 0)
  84. - $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
  85. - $CM_MAX_CLIPS: soft maximum number of clips to store, 0 for inf. At $CM_MAX_CLIPS + 10, the number of clips is reduced to $CM_MAX_CLIPS (default: 1000)
  86. - $CM_ONESHOT: run once immediately, do not loop (default: 0)
  87. - $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)
  88. - $CM_SELECTIONS: space separated list of the selections to manage (default: "clipboard primary")
  89. - $CM_SYNC_PRIMARY_TO_CLIPBOARD: sync selections from primary to clipboard immediately (default: 0)
  90. - $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)
  91. EOF
  92. exit 0
  93. fi
  94. [[ $DISPLAY ]] || die 2 'The X display is unset, is your X server running?'
  95. # It's ok that this only applies to the final directory.
  96. # shellcheck disable=SC2174
  97. mkdir -p -m0700 "$cache_dir"
  98. echo "enabled" > "$status_file"
  99. exec {session_lock_fd}> "$session_lock_file"
  100. flock -x -n "$session_lock_fd" ||
  101. die 2 "Can't lock session file -- is another clipmenud running?"
  102. declare -A last_data_sel
  103. declare -A updated_sel
  104. command -v clipnotify >/dev/null 2>&1 || die 2 "clipnotify not in PATH"
  105. command -v xdotool >/dev/null 2>&1 && has_xdotool=1
  106. if [[ $CM_IGNORE_WINDOW ]] && ! (( has_xdotool )); then
  107. echo "WARN: CM_IGNORE_WINDOW does not work without xdotool, which is not installed" >&2
  108. fi
  109. exec {lock_fd}> "$lock_file"
  110. trap sig_disable USR1
  111. trap sig_enable USR2
  112. trap 'trap - INT TERM EXIT; kill_background_jobs; exit 0' INT TERM EXIT
  113. while true; do
  114. if ! (( CM_ONESHOT )); then
  115. # Make sure we're interruptible for the sig_{en,dis}able traps
  116. clipnotify &
  117. _CM_CLIPNOTIFY_PID="$!"
  118. wait "$_CM_CLIPNOTIFY_PID"
  119. fi
  120. if (( _CM_DISABLED )); then
  121. # The first one will just be from interrupting `wait`, so don't print
  122. if (( _CM_FIRST_DISABLE )); then
  123. unset _CM_FIRST_DISABLE
  124. else
  125. info "Got a clipboard notification, but we are disabled, skipping"
  126. fi
  127. continue
  128. fi
  129. if [[ $CM_IGNORE_WINDOW ]] && (( has_xdotool )); then
  130. windowname="$(xdotool getactivewindow getwindowname)"
  131. if [[ "$windowname" =~ $CM_IGNORE_WINDOW ]]; then
  132. debug "ignoring clipboard because windowname \"$windowname\" matches \"${CM_IGNORE_WINDOW}\""
  133. continue
  134. fi
  135. fi
  136. if ! flock -x -w "$lock_timeout" "$lock_fd"; then
  137. if (( CM_ONESHOT )); then
  138. die 1 "Timed out waiting for lock"
  139. else
  140. error "Timed out waiting for lock, skipping this iteration"
  141. continue
  142. fi
  143. fi
  144. for selection in "${selections[@]}"; do
  145. echo $selection
  146. updated_sel[$selection]=0
  147. data=$(_xsel -o --"$selection"; printf x)
  148. data=${data%x} # avoid trailing newlines being stripped
  149. echo $data
  150. [[ $data == *[^[:space:]]* ]] || continue
  151. [[ $last_data == "$data" ]] && continue
  152. [[ ${last_data_sel[$selection]} == "$data" ]] && continue
  153. if [[ $last_data && $data == "$last_data"* ]] ||
  154. [[ $last_data && $data == *"$last_data" ]]; then
  155. # Don't actually remove the file yet, because it might be
  156. # referenced by an older entry. These will be dealt with at vacuum.
  157. debug "$selection: $last_data is a possible partial of $data"
  158. previous_size=$(wc -c <<< "$last_cache_file_output")
  159. truncate -s -"$previous_size" "$cache_file"
  160. fi
  161. first_line=$(get_first_line "$data")
  162. kill -52 $(pidof dwmblocks)
  163. debug "New clipboard entry on $selection selection: \"$first_line\""
  164. cache_file_output="$(date +%s%N) $first_line"
  165. filename="$cache_dir/$(cksum <<< "$first_line")"
  166. last_cache_file_output=$cache_file_output
  167. last_data=$data
  168. last_data_sel[$selection]=$data
  169. updated_sel[$selection]=1
  170. debug "Writing $data to $filename"
  171. printf '%s' "$data" > "$filename"
  172. debug "Writing $cache_file_output to $cache_file"
  173. printf '%s\n' "$cache_file_output" >> "$cache_file"
  174. if (( CM_OWN_CLIPBOARD )) && [[ $selection == clipboard ]]; then
  175. # Only clipboard, since apps like urxvt will unhilight for PRIMARY
  176. _xsel -o --clipboard | _xsel -i --clipboard
  177. fi
  178. done
  179. if (( CM_SYNC_PRIMARY_TO_CLIPBOARD )) && (( updated_sel[primary] )); then
  180. _xsel -o --primary | _xsel -i --clipboard
  181. fi
  182. # The cache file may not exist if this is the first run and data is skipped
  183. if (( CM_MAX_CLIPS )) && [[ -f "$cache_file" ]] && (( "$(wc -l < "$cache_file")" > CM_MAX_CLIPS_THRESH )); then
  184. info "Trimming clip cache to CM_MAX_CLIPS ($CM_MAX_CLIPS)"
  185. trunc_tmp=$(mktemp)
  186. tail -n "$CM_MAX_CLIPS" "$cache_file" | uniq > "$trunc_tmp"
  187. mv -- "$trunc_tmp" "$cache_file"
  188. # Vacuum up unreferenced clips. They may either have been
  189. # unreferenced by the above CM_MAX_CLIPS code, or they may be old
  190. # possible partials.
  191. declare -A cksums
  192. while IFS= read -r line; do
  193. cksum=$(cksum <<< "$line")
  194. cksums["$cksum"]="$line"
  195. done < <(cut -d' ' -f2- < "$cache_file")
  196. num_vacuumed=0
  197. for file in "$cache_dir"/[012346789]*; do
  198. cksum=${file##*/}
  199. if [[ ${cksums["$cksum"]-_missing_} == _missing_ ]]; then
  200. debug "Vacuuming due to lack of reference: $file"
  201. (( ++num_vacuumed ))
  202. rm -- "$file"
  203. fi
  204. done
  205. unset cksums
  206. info "Vacuumed $num_vacuumed clip files."
  207. fi
  208. flock -u "$lock_fd"
  209. (( CM_ONESHOT )) && break
  210. done