intelli_search_key="${INTELLI_SEARCH_HOTKEY:-\C-@}"
intelli_bookmark_key="${INTELLI_BOOKMARK_HOTKEY:-\C-b}"
intelli_variable_key="${INTELLI_VARIABLE_HOTKEY:-\C-l}"
intelli_fix_key="${INTELLI_FIX_HOTKEY:-\C-x}"
function _intelli_exec {
local temp_result_file=$(mktemp)
echo -n "${PS1@P}" | tail -n 1
intelli-shell --extra-line --file-output "$temp_result_file" "$@"
local exit_status=$?
if [[ ! -s "$temp_result_file" ]]; then
echo -n "${PS1@P}" | head -n -1
rm -f "$temp_result_file" 2>/dev/null
return $exit_status
fi
local -a lines
mapfile -t lines < "$temp_result_file"
rm -f "$temp_result_file" 2>/dev/null
local status="${lines[0]}"
local action=""
local command=""
if ((${#lines[@]} > 1)); then
action="${lines[1]}"
fi
if ((${#lines[@]} > 2)); then
printf -v command '%s\n' "${lines[@]:2}"
command="${command%$'\n'}"
fi
if [[ "$status" == "DIRTY" || "$action" == "EXECUTED" || $exit_status -ne 0 ]]; then
if [[ "$status" == "CLEAN" && "$action" != "EXECUTED" ]]; then
echo
fi
echo -n "${PS1@P}" | head -n -1
fi
if [[ "$action" == "REPLACE" ]]; then
READLINE_LINE="$command"
READLINE_POINT=${#command}
else
READLINE_LINE=""
READLINE_POINT=0
fi
printf '\r\033[2K'
}
function _intelli_search {
_intelli_exec search -i "$READLINE_LINE"
}
function _intelli_save {
_intelli_exec new -i "$READLINE_LINE"
}
function _intelli_variable {
_intelli_exec replace -i "$READLINE_LINE"
}
function _intelli_fix {
local hist
hist=$(fc -l -n -5)
_intelli_exec fix --history "$hist" "$READLINE_LINE"
}
if [[ "${INTELLI_SKIP_ESC_BIND:-0}" == "0" ]]; then
bind '"\e": kill-whole-line'
fi
bind -x '"'"$intelli_search_key"'":"_intelli_search"'
bind -x '"'"$intelli_bookmark_key"'":"_intelli_save"'
bind -x '"'"$intelli_variable_key"'":"_intelli_variable"'
bind -x '"'"$intelli_fix_key"'":"_intelli_fix"'
export INTELLI_EXEC_PROMPT=$(printf '%s' "$PS2" | sed 's/\\\[//g; s/\\\]//g')