Re: [PATCH 4/8] bash: Add space after unique command name is completed.

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH 4/8] bash: Add space after unique command name is completed.

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:42:52

"Shawn O. Pearce" [off-list ref] writes:
+__gitcomp ()
+{
+	local all c s=$'\n' IFS=' '$'\t'$'\n'
+	for c in $1; do
+		case "$c" in
+		--*=*) all="$all$c$s" ;;
+		*)     all="$all$c $s" ;;
+		esac
+	done
+	IFS=$s
+	COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
+	return
+}
+
I do not understand what is going on here.  Care to explain?

 (1) "for c in $1": because $1 is not dquoted, it is split into
     word with IFS -- which IFS applies to this splitting?  The
     local one you hand-define to the typical " \t\n" value?

 (2) when this function returns, what IFS value does the caller
     would have?  Is the "local" used to avoid screwing up the
     caller?

Re: [PATCH 4/8] bash: Add space after unique command name is completed.

From: Shawn O. Pearce <hidden>
Date: 2016-06-15 22:42:52

Junio C Hamano [off-list ref] wrote:
"Shawn O. Pearce" [off-list ref] writes:
quoted
+__gitcomp ()
+{
+	local all c s=$'\n' IFS=' '$'\t'$'\n'
+	for c in $1; do
+		case "$c" in
+		--*=*) all="$all$c$s" ;;
+		*)     all="$all$c $s" ;;
+		esac
+	done
+	IFS=$s
+	COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
+	return
+}
+
I do not understand what is going on here.  Care to explain?

 (1) "for c in $1": because $1 is not dquoted, it is split into
     word with IFS -- which IFS applies to this splitting?  The
     local one you hand-define to the typical " \t\n" value?
Correct.
 
 (2) when this function returns, what IFS value does the caller
     would have?  Is the "local" used to avoid screwing up the
     caller?
Correct.


Basically at the time I'm forming 'all' I want $1 split based on
normal splitting rules, as I'm using space, tag and LF within the
script in some places for the first parameter to __gitcomp.

But when I run compgen I need IFS to be just LF ($s) so that $all
is split only on LF and not on spaces, as spaces were added to $all
after each unique option.  This way the trailing space is part of
the unique option, and bash will tack in the trailing space for me,
despite having -o nospace enabled.

-- 
Shawn.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help