diff --git a/mcfly.bash b/mcfly.bash index 3f8b2d9..d71e193 100644 --- a/mcfly.bash +++ b/mcfly.bash @@ -6,6 +6,9 @@ function mcfly_initialize { # Ensure stdin is a tty [[ -t 0 ]] || return 0 + # Ensure an interactive shell + [[ $- =~ .*i.* ]] || return 0 + # Avoid loading this file more than once [[ "${__MCFLY_LOADED-}" != "loaded" ]] || return 0 __MCFLY_LOADED="loaded" @@ -113,30 +116,28 @@ function mcfly_initialize { PROMPT_COMMAND="mcfly_prompt_command;${PROMPT_COMMAND#;}" fi - # If this is an interactive shell, take ownership of ctrl-r. - if [[ $- =~ .*i.* ]]; then - if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then - # shellcheck disable=SC2016 - if [[ ${MCFLY_BASH_USE_TIOCSTI-} = 1 ]]; then - bind -x '"\C-r": "mcfly_search_with_tiocsti"' - else - # Bind ctrl+r to 2 keystrokes, the first one is used to search in McFly, the second one is used to run the command (if mcfly_search binds it to accept-line). - bind -x "\"$MCFLY_BASH_SEARCH_KEYBINDING\":\"mcfly_search\"" - bind "\"\C-r\":\"$MCFLY_BASH_SEARCH_KEYBINDING$MCFLY_BASH_ACCEPT_LINE_KEYBINDING\"" - fi + # Take ownership of ctrl-r. + if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then + # shellcheck disable=SC2016 + if [[ ${MCFLY_BASH_USE_TIOCSTI-} = 1 ]]; then + bind -x '"\C-r": "mcfly_search_with_tiocsti"' + else + # Bind ctrl+r to 2 keystrokes, the first one is used to search in McFly, the second one is used to run the command (if mcfly_search binds it to accept-line). + bind -x "\"$MCFLY_BASH_SEARCH_KEYBINDING\":\"mcfly_search\"" + bind "\"\C-r\":\"$MCFLY_BASH_SEARCH_KEYBINDING$MCFLY_BASH_ACCEPT_LINE_KEYBINDING\"" + fi + else + # The logic here is: + # 1. Jump to the beginning of the edit buffer, add 'mcfly: ', and comment out the current line. We comment out the line + # to ensure that all possible special characters, including backticks, are ignored. This commented out line will + # end up as the most recent entry in the $MCFLY_HISTORY file. + # 2. Type "mcfly search" and then run the command. McFly will pull the last line from the $MCFLY_HISTORY file, + # which should be the commented-out search from step #1. It will then remove that line from the history file and + # render the search UI pre-filled with it. + if set -o | grep "vi " | grep -q on; then + bind '"\C-r": "\e0i#mcfly: \e\C-m mcfly search\C-m"' else - # The logic here is: - # 1. Jump to the beginning of the edit buffer, add 'mcfly: ', and comment out the current line. We comment out the line - # to ensure that all possible special characters, including backticks, are ignored. This commented out line will - # end up as the most recent entry in the $MCFLY_HISTORY file. - # 2. Type "mcfly search" and then run the command. McFly will pull the last line from the $MCFLY_HISTORY file, - # which should be the commented-out search from step #1. It will then remove that line from the history file and - # render the search UI pre-filled with it. - if set -o | grep "vi " | grep -q on; then - bind '"\C-r": "\e0i#mcfly: \e\C-m mcfly search\C-m"' - else - bind '"\C-r": "\C-amcfly: \e# mcfly search\C-m"' - fi + bind '"\C-r": "\C-amcfly: \e# mcfly search\C-m"' fi fi } diff --git a/mcfly.fish b/mcfly.fish index ca6330a..7180d1f 100644 --- a/mcfly.fish +++ b/mcfly.fish @@ -4,47 +4,48 @@ if test "$__MCFLY_LOADED" != "loaded" set -g __MCFLY_LOADED "loaded" - # Note: we only use the history file for the session when this file was sourced. - # Would have to reset this before calling mcfly if you want commands from another session later. - if not set -q MCFLY_HISTFILE - set -gx MCFLY_HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history - end - if not test -r "$MCFLY_HISTFILE" - echo "McFly: $MCFLY_HISTFILE does not exist or is not readable. Please fix this or set MCFLY_HISTFILE to something else before using McFly." >&2 - exit 1 - end + # If this is an interactive shell + if status is-interactive + # Note: we only use the history file for the session when this file was sourced. + # Would have to reset this before calling mcfly if you want commands from another session later. + if not set -q MCFLY_HISTFILE + set -gx MCFLY_HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history + end + if not test -r "$MCFLY_HISTFILE" + echo "McFly: $MCFLY_HISTFILE does not exist or is not readable. Please fix this or set MCFLY_HISTFILE to something else before using McFly." >&2 + exit 1 + end - # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session. - set -gx MCFLY_SESSION_ID (dd if=/dev/urandom bs=256 count=1 2>/dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24) + # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session. + set -gx MCFLY_SESSION_ID (dd if=/dev/urandom bs=256 count=1 2>/dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24) - # Find the binary - set -q MCFLY_PATH; or set -l MCFLY_PATH (command which mcfly) - if test -z "$MCFLY_PATH"; or test "$MCFLY_PATH" = "mcfly not found" - echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.fish" - exit 1 - end - # We don't need a MCFLY_HISTORY file because we can get the last command in fish_postexec. - set -gx MCFLY_HISTORY /dev/null - set -g __MCFLY_CMD $MCFLY_PATH --mcfly_history $MCFLY_HISTORY --history_format fish + # Find the binary + set -q MCFLY_PATH; or set -l MCFLY_PATH (command which mcfly) + if test -z "$MCFLY_PATH"; or test "$MCFLY_PATH" = "mcfly not found" + echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.fish" + exit 1 + end + # We don't need a MCFLY_HISTORY file because we can get the last command in fish_postexec. + set -gx MCFLY_HISTORY /dev/null + set -g __MCFLY_CMD $MCFLY_PATH --mcfly_history $MCFLY_HISTORY --history_format fish - function __mcfly_save_old_pwd -d 'Save PWD before running command' -e fish_preexec - set -g __MCFLY_OLD_PWD "$PWD" - end + function __mcfly_save_old_pwd -d 'Save PWD before running command' -e fish_preexec + set -g __MCFLY_OLD_PWD "$PWD" + end - function __mcfly_add_command -d 'Add run commands to McFly database' -e fish_postexec - # Retain return code of last command before we lose it - set -l last_status $status - # Check for the private mode - test -n "$fish_private_mode"; and return - # Handle first call of this function after sourcing mcfly.fish, when the old PWD won't be set - set -q __MCFLY_OLD_PWD; or set -g __MCFLY_OLD_PWD "$PWD" + function __mcfly_add_command -d 'Add run commands to McFly database' -e fish_postexec + # Retain return code of last command before we lose it + set -l last_status $status + # Check for the private mode + test -n "$fish_private_mode"; and return + # Handle first call of this function after sourcing mcfly.fish, when the old PWD won't be set + set -q __MCFLY_OLD_PWD; or set -g __MCFLY_OLD_PWD "$PWD" - test -n "$MCFLY_DEBUG"; and echo mcfly.fish: Run eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]' - eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]' - end + test -n "$MCFLY_DEBUG"; and echo mcfly.fish: Run eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]' + eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]' + end - # If this is an interactive shell, set up key binding functions. - if status is-interactive + # Set up key binding functions. function __mcfly-history-widget -d "Search command history with McFly" set tmpdir $TMPDIR if test -z "$tmpdir" diff --git a/mcfly.zsh b/mcfly.zsh index 5ef8b9f..d78af49 100644 --- a/mcfly.zsh +++ b/mcfly.zsh @@ -1,7 +1,7 @@ #!/bin/zsh () { - # Ensure stdin is a tty + # Ensure an interactive shell [[ -o interactive ]] || return 0 # Setup MCFLY_HISTFILE and make sure it exists. @@ -71,34 +71,32 @@ [ -n "$MCFLY_DEBUG" ] && echo "mcfly_exit_logger already in zshexit_functions, skipping" fi - # If this is an interactive shell, take ownership of ctrl-r. - if [[ $- =~ .*i.* ]]; then - mcfly-history-widget() { - () { - echoti rmkx - exec