diff --git a/clipea/clipea.zsh b/clipea/clipea.zsh index 2b8bc0a..c48542b 100755 --- a/clipea/clipea.zsh +++ b/clipea/clipea.zsh @@ -7,18 +7,28 @@ if [[ $ZSH_EVAL_CONTEXT != 'toplevel:file' ]]; then return 1 2>/dev/null || exit 1 fi -DIR="$(dirname -- "$0")" +CLIPEA_TMP_FILE=$(mktemp) -TMP_FILE=$(mktemp) +# https://stackoverflow.com/questions/9901210/bash-source0-equivalent-in-zsh +CLIPEA_SCRIPT_DIR=$(dirname $(readlink -f ${(%):-%x})) -# Execute the PHP script with an environment variable -CLIPEA_CMD_OUTPUT_FILE="$TMP_FILE" "$(which clipea)" "$@" +CLIPEA_PYTHON= + +CLIPEA_PATH=$(which clipea) + +# Run clipea from the current dir if possible +if [[ -f $CLIPEA_SCRIPT_DIR/__main__.py ]]; then + CLIPEA_PATH=$CLIPEA_SCRIPT_DIR + CLIPEA_PYTHON="$(which python3 || which python)" +fi + +# Execute clipea with an environment variable +CLIPEA_CMD_OUTPUT_FILE="$CLIPEA_TMP_FILE" $CLIPEA_PYTHON "$CLIPEA_PATH" "$@" # Read the command to be placed on the Zsh command line -commandToPlace=$(< "$TMP_FILE") +CLIPEA_COMMAND_TO_PLACE=$(< "$CLIPEA_TMP_FILE") # Place it on the Zsh command line -print -rz "$commandToPlace" +print -rz "$CLIPEA_COMMAND_TO_PLACE" -# Remove the temp file -rm "$TMP_FILE" +rm "$CLIPEA_TMP_FILE"