Skip to content

Conversation

@xuanweishan
Copy link

Bug fix #424

Description

This pull request fix #424. Make user able to use autocomplete with python alias setting

Changes

  1. Add a local variable alias_expanded in /tool/config/config_autocomplete.sh with definition:
    alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/")
  2. Add a condition for assigning variable configure_command
    • If alias_expanded is not empty: configure_command=${alias_expanded} ${COMP_WORDS[1]}
    • If alias_expanded is empty: configure_command=${COMP_WORDS[0] ${COMP_WORDS[1]}}

Tests

  • ./configure [tab] [tab]
  • python configure [tab] [tab] (w/ alias setting: `alias python="python3")
  • python3 configure [tab] [tab]

Copy link
Member

@technic960183 technic960183 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xuanweishan Thanks for your contribution and sorry for the late reply.
I have investigated an edge case where the expanded of the alias itself contains spaces or quotes. (e.g. alias python="'/home/ymhsu/undergrad'\''s research/python'", where there is a folder named undergrad's research in the path.)

The solution is to store the command in an array and use eval to parse the string with complex quoting (without starting a new interactive bash).

I have proposed the modifications (7 lines in total).
Besides the 4 lines in the comments, we still need to replace another 3 lines:
all_options=$(${configure_command} --autocomplete_info=all) with all_options=$(eval "${configure_command[@]}" --autocomplete_info=all)
and 2 lines similarly for sub_options=.

Tests done

With alias python="'/home/ymhsu/undergrad'\''s research/python'":

  • python configure.py --[tab][tab]
  • python configure.py --m[tab][tab] -> print out --machine= --max_patch= --mhd= --model= --mpi=
  • python configure.py --mod[tab] -> complete to --model=
  • python configure.py --model=[tab][tab] -> print out ELBDM HYDRO PAR_ONLY
  • python configure.py --model=H[tab] -> complete to --model=HYDRO
    And the same combination for python3, ./configure.py, and with alias python="'/home/ymhsu/under\"grad'\''s research/python'" for a path with under"grad's research.
    I hope this solution has enough completeness.

configure_filename=${COMP_WORDS[1]}
configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}"
local alias_expanded
alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to rename alias_expanded to alias_def, since the expansion catching will be move to the line with eval using #alias ${COMP_WORDS[0]}=.

Suggested change
alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/")
alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null)

local alias_expanded
alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/")
if [[ -n "$alias_expanded" ]]; then
configure_command="${alias_expanded} ${configure_filename}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
configure_command="${alias_expanded} ${configure_filename}"
eval "configure_command=( ${alias_expanded#alias ${COMP_WORDS[0]}=} ${configure_filename} )"

if [[ -n "$alias_expanded" ]]; then
configure_command="${alias_expanded} ${configure_filename}"
else
configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an array to store configure_command.

Suggested change
configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}"
configure_command=("${COMP_WORDS[0]}" "${COMP_WORDS[1]}")

fi
else
configure_filename=${COMP_WORDS[0]}
configure_command="${COMP_WORDS[0]}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an array to store configure_command.

Suggested change
configure_command="${COMP_WORDS[0]}"
configure_command=("${COMP_WORDS[0]}")

@xuanweishan
Copy link
Author

@technic960183
Thanks for the great suggestion. I've apply your suggestion in latest commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Autocomplete alias command

3 participants