{"thread":{"id":"32316","subject":"[PATCH] Add file completion to tcsh git completion.","startedAt":"2012-12-11T21:36:57Z","lastAt":"2012-12-11T21:36:57Z","messageCount":1,"participants":["Marc Khouzam"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"204690","messageId":"E59706EF8DB1D147B15BECA3322E4BDC0384ED@eusaamb103.ericsson.se","threadId":"32316","inReplyTo":null,"subject":"[PATCH] Add file completion to tcsh git completion.","fromName":"Marc Khouzam","fromEmail":"marc.khouzam@ericsson.com","sentAt":"2012-12-11T21:36:57Z","receivedAt":"2012-12-11T21:36:57Z","isPatch":true,"sender":{"key":"marc.khouzam@ericsson.com","avatar":"https://gravatar.com/avatar/de564e23ad14e2945f9f1cdb4d0227c935b5b54c39576a304d47caa3e23dcd33?d=mp&s=160"},"body":"For bash completion, the option '-o bashdefault' is used to indicate\nthat when no other choices are available, file completion should be\nperformed.  Since this option is not available in tcsh, no file\ncompletion is ever performed.  Therefore, commands like 'git add ',\n'git send-email ', etc, require the user to manually type out\nthe file name.  This can be quite annoying.\n\nTo improve the user experience we try to simulate file completion\ndirectly in this script (although not perfectly).\n\nThe known issues with the file completion simulation are:\n- Possible completions are shown with their directory prefix.\n- Completions containing shell variables are not handled.\n- Completions with ~ as the first character are not handled.\n\nSigned-off-by: Marc Khouzam <marc.khouzam@ericsson.com>\n---\n\nBeyond the described fix, this commit also improves minor aspects\nof the script.  Normally I would submit these things in different\ncommits, but I felt it would be wasting the reviewers/maintainer\ntime as they are very minor.  If you do prefer different commits, \njust let me know.  The minor improvements are:\n\t- update header to recommend setting a tcsh variable\n\t- all missing tabs have been added\n\t- use ',' instead of '/' in 'complete' command for users that\n        prefer to hard-code the path which also contains a '/' and\n\t  would cause a conflict.\n\t- unset a variable to avoid polluting the user's env.\n\nThanks\n\nMarc\n\n contrib/completion/git-completion.tcsh | 69 ++++++++++++++++++++++++++--------\n 1 file changed, 54 insertions(+), 15 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.tcsh b/contrib/completion/git-completion.tcsh\nindex 471f47b..8aafb63 100644\n--- a/contrib/completion/git-completion.tcsh\n+++ b/contrib/completion/git-completion.tcsh\n@@ -19,23 +19,26 @@\n #       (e.g. ~/.git-completion.tcsh and ~/.git-completion.bash).\n #    2) Add the following line to your .tcshrc/.cshrc:\n #        source ~/.git-completion.tcsh\n+#    3) For completion similar to bash, it is recommended to also\n+#       add the following line to your .tcshrc/.cshrc:\n+#        set autolist=ambiguous\n+#       It will tell tcsh to list the possible completion choices.\n \n set __git_tcsh_completion_original_script = ${HOME}/.git-completion.bash\n set __git_tcsh_completion_script = ${HOME}/.git-completion.tcsh.bash\n \n # Check that the user put the script in the right place\n if ( ! -e ${__git_tcsh_completion_original_script} ) then\n-       echo \"git-completion.tcsh: Cannot find: ${__git_tcsh_completion_original_script}.  Git completion will not work.\"\n-       exit\n+\techo \"git-completion.tcsh: Cannot find: ${__git_tcsh_completion_original_script}.  Git completion will not work.\"\n+\texit\n endif\n \n cat << EOF > ${__git_tcsh_completion_script}\n #!bash\n #\n # This script is GENERATED and will be overwritten automatically.\n-# Do not modify it directly.  Instead, modify the git-completion.tcsh\n-# script provided by Git core.\n-#\n+# Do not modify it directly.  Instead, modify git-completion.tcsh\n+# and source it again.\n \n source ${__git_tcsh_completion_original_script}\n \n@@ -47,22 +50,58 @@ COMP_WORDS=(\\$2)\n # tell us that the previous word is complete and the cursor\n # is on the next word.\n if [ \"\\${2: -1}\" == \" \" ]; then\n-       # The last character is a space, so our location is at the end\n-       # of the command-line array\n-       COMP_CWORD=\\${#COMP_WORDS[@]}\n+\t# The last character is a space, so our location is at the end\n+\t# of the command-line array\n+\tCOMP_CWORD=\\${#COMP_WORDS[@]}\n else\n-       # The last character is not a space, so our location is on the\n-       # last word of the command-line array, so we must decrement the\n-       # count by 1\n-       COMP_CWORD=\\$((\\${#COMP_WORDS[@]}-1))\n+\t# The last character is not a space, so our location is on the\n+\t# last word of the command-line array, so we must decrement the\n+\t# count by 1\n+\tCOMP_CWORD=\\$((\\${#COMP_WORDS[@]}-1))\n fi\n \n # Call _git() or _gitk() of the bash script, based on the first argument\n _\\${1}\n \n IFS=\\$'\\n'\n-echo \"\\${COMPREPLY[*]}\" | sort | uniq\n+if [ \\${#COMPREPLY[*]} -gt 0 ]; then\n+\techo \"\\${COMPREPLY[*]}\" | sort | uniq\n+else\n+\t# No completions suggested.  In this case, we want tcsh to perform\n+\t# standard file completion.  However, there does not seem to be way\n+\t# to tell tcsh to do that.  To help the user, we try to simulate\n+\t# file completion directly in this script.\n+\t#\n+\t# Known issues:\n+\t#     - Possible completions are shown with their directory prefix.\n+\t#     - Completions containing shell variables are not handled.\n+\t#     - Completions with ~ as the first character are not handled.\n+\n+\t# No file completion should be done unless we are completing beyond\n+\t# the git sub-command.  An improvement on the bash completion :)\n+\tif [ \\${COMP_CWORD} -gt 1 ]; then\n+\t\tTO_COMPLETE=\"\\${COMP_WORDS[\\${COMP_CWORD}]}\"\n+\n+\t\t# We don't support ~ expansion: too tricky.\n+\t\tif [ \"\\${TO_COMPLETE:0:1}\" != \"~\" ]; then\n+\t\t\t# Use ls so as to add the '/' at the end of directories.\n+\t\t\tRESULT=(\\`ls -dp \\${TO_COMPLETE}* 2> /dev/null\\`)\n+\t\t\techo \\${RESULT[*]}\n+\n+\t\t\t# If there is a single completion and it is a directory,\n+\t\t\t# we output it a second time to trick tcsh into not adding a space\n+\t\t\t# after it.\n+\t\t\tif [ \\${#RESULT[*]} -eq 1 ] && [ \"\\${RESULT[0]: -1}\" == \"/\" ]; then\n+\t\t\t\techo \\${RESULT[*]}\n+\t\t\tfi\n+\t\tfi\n+\tfi\n+fi\n+\n EOF\n \n-complete git  'p/*/`bash ${__git_tcsh_completion_script} git \"${COMMAND_LINE}\"`/'\n-complete gitk 'p/*/`bash ${__git_tcsh_completion_script} gitk \"${COMMAND_LINE}\"`/'\n+# Don't need this variable anymore, so don't pollute the users environment\n+unset __git_tcsh_completion_original_script\n+\n+complete git  'p,*,`bash ${__git_tcsh_completion_script} git \"${COMMAND_LINE}\"`,'\n+complete gitk 'p,*,`bash ${__git_tcsh_completion_script} gitk \"${COMMAND_LINE}\"`,'\n-- \n1.8.0.1.g9fe2839\n"}]}