From b73927384608d473d864406105d071c7223ea577 Mon Sep 17 00:00:00 2001 From: Sami Kankaristo Date: Thu, 17 Dec 2020 23:03:47 +0200 Subject: [PATCH 1/3] Preserve exit code from launcher This change is related to https://github.com/cdown/clipmenu/issues/57#issuecomment-740123283. If `clipmenu` "preserves" the exit code from the launcher (exits with the same code as the launcher), the exit code's from Rofi's custom keybindings can be used with `clipmenu`. For example, I'm using the following script bound to `Super+v`: ``` #!/bin/bash trap "exit" INT # Run clipmenu CM_LAUNCHER=rofi clipmenu -p "Paste" -mesg "Use Shift+Delete to delete an item" \ -kb-delete-entry "" -kb-custom-1 "Shift+Delete" \ -kb-accept-alt "" -kb-custom-2 "Shift+Return" exit_code=$? case $exit_code in 0) xdotool key "shift+Insert" ;; 10) clipdel -d ^"$(xsel -b)"$; "$0" ;; *) exit $exit_code ;; esac ``` With the above script, I have 3 different options when running `clipmenu`: - if I press `Return`, the selected item is pasted to where my cursor currently is (a bit hackily with `xdotool`) - if I press `Shift+Return`, the selected item is sent to the clipboard ("default" `clipmenu` behavior) - if I press `Shift+Delete`, the selected item is deleted from `clipmenu`, and the script runs itself again (essentially keeps `clipmenu` open) In order for this to work, the only change needed in `clipmenu` is to preserve the exit codes from the custom keybindings. --- clipmenu | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clipmenu b/clipmenu index fbd883f..69c2672 100755 --- a/clipmenu +++ b/clipmenu @@ -58,6 +58,8 @@ else chosen_line=$(list_clips | "$CM_LAUNCHER" "${launcher_args[@]}" "$@") fi +launcher_exit=$? + [[ $chosen_line ]] || exit 1 file=$cache_dir/$(cksum <<< "$chosen_line") [[ -f "$file" ]] || exit 2 @@ -69,3 +71,5 @@ done if (( CM_OUTPUT_CLIP )); then cat "$file" fi + +exit $launcher_exit From 20fe00567d06125214092b519e9f405f06cfebdf Mon Sep 17 00:00:00 2001 From: Sami Kankaristo Date: Fri, 18 Dec 2020 16:38:36 +0200 Subject: [PATCH 2/3] Only set $launcher_exit if not running rofi-script Only set `$launcher_exit` if not running rofi-script, and only exit using `$launcher_exit` if it has been set. Otherwise, the last line (now with quoting around the variable) becomes `exit ""`, which causes an error. --- clipmenu | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clipmenu b/clipmenu index 69c2672..dae08b1 100755 --- a/clipmenu +++ b/clipmenu @@ -56,10 +56,9 @@ if [[ "$CM_LAUNCHER" == rofi-script ]]; then fi else chosen_line=$(list_clips | "$CM_LAUNCHER" "${launcher_args[@]}" "$@") + launcher_exit=$? fi -launcher_exit=$? - [[ $chosen_line ]] || exit 1 file=$cache_dir/$(cksum <<< "$chosen_line") [[ -f "$file" ]] || exit 2 @@ -72,4 +71,4 @@ if (( CM_OUTPUT_CLIP )); then cat "$file" fi -exit $launcher_exit +[ -n "$launcher_exit" ] && exit "$launcher_exit" From fa9917a9b11cec4a7f7849507e5701a4b6e0b60f Mon Sep 17 00:00:00 2001 From: Sami Kankaristo Date: Sat, 19 Dec 2020 18:22:30 +0200 Subject: [PATCH 3/3] Use default value instead of testing for empty value --- clipmenu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clipmenu b/clipmenu index dae08b1..471bd77 100755 --- a/clipmenu +++ b/clipmenu @@ -71,4 +71,4 @@ if (( CM_OUTPUT_CLIP )); then cat "$file" fi -[ -n "$launcher_exit" ] && exit "$launcher_exit" +exit "${launcher_exit:-"$?"}"