diff --git a/etc/bashrc b/etc/bashrc index 623f19affd33e1caa038d6fe26732d62fbc33b77..e2e887ac646048ac52fa5f7ef0845474bc7f4bec 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -175,7 +175,7 @@ then _foamEtc config.sh/aliases # Bash completions - if command -v complete > /dev/null 2>&1 + if [ "${BASH_VERSINFO:-0}" -ge 4 ] then _foamEtc config.sh/bash_completion fi diff --git a/etc/config.csh/complete-wrapper b/etc/config.csh/complete-wrapper index d74e64975d9bf9fadacc88b88113cfc5f495c351..8fbbdffac6766c134c7405aa2374be485d5a9dc7 100644 --- a/etc/config.csh/complete-wrapper +++ b/etc/config.csh/complete-wrapper @@ -25,6 +25,15 @@ #------------------------------------------------------------------------------ [ "$#" -ge 1 ] || exit 1 +# Support '-test' option to check bash version +if [ "$1" = "-test" ] +then + # Uses 'declare -gA' for the implementation + # The '-A' requires bash >= 4.0 and the '-g' requires bash >= 4.2 + [ "${BASH_VERSINFO[0]:-0}${BASH_VERSINFO[1]:-0}" -ge 42 ] + exit $? +fi + # Preload completion cache if [ -f $WM_PROJECT_DIR/etc/config.sh/completion_cache ] then . $WM_PROJECT_DIR/etc/config.sh/completion_cache diff --git a/etc/config.csh/tcsh_completion b/etc/config.csh/tcsh_completion index 51258b70206e740cff1e6f866c3c8186b485d652..9dfcca704650699d808a11aabd486c2678d1b603 100644 --- a/etc/config.csh/tcsh_completion +++ b/etc/config.csh/tcsh_completion @@ -1,25 +1,47 @@ #----------------------------------*-sh-*-------------------------------------- -# Tcsh completions for OpenFOAM applications -# Using bash_completion functions for the hard work +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# This file is part of OpenFOAM, licensed under the GNU General Public License +# <http://www.gnu.org/licenses/>. +# +# File +# etc/config.csh/tcsh_completion +# +# Description +# Tcsh completions for OpenFOAM applications +# Using bash completion for the hard work +# +# Requires +# bash 4.2 or newer +# +#------------------------------------------------------------------------------ if ($?tcsh) then # tcsh only # Remove old completions, which look like: # complete APPNAME 'p,*,`bash $WM_PROJECT_DIR/etc/ ... foreach appName (`complete | sed -ne '/WM_PROJECT/s/\t.*$//p'`) - uncomplete $cleaned + uncomplete $appName end - # Generate completions for predefined directories - foreach dirName ("$FOAM_APPBIN") - if ( ! -d $dirName || ! -f $WM_PROJECT_DIR/etc/config.csh/complete-wrapper ) continue - foreach appName (`find $dirName -maxdepth 1 -executable -type f`) - # Pass explicitly - ## complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t' "${COMMAND_LINE}"`,' - # Pass via environment - complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t'`,' + # Generate completions for predefined directories (if support is possible) + bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper -test + if ($status == 0) then + foreach dirName ("$FOAM_APPBIN") + if ( ! -d $dirName ) continue + foreach appName (`find $dirName -maxdepth 1 -executable -type f`) + # Pass explicitly + ## complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t' "${COMMAND_LINE}"`,' + # Pass via environment + complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t'`,' + end end - end + endif + endif #------------------------------------------------------------------------------ diff --git a/etc/config.sh/bash_completion b/etc/config.sh/bash_completion index 541771c5f1b6a0c7dd0f62a409f34c465728f5ab..8d671ee9f7dcaf8ae1885200b413f4f0c56fd8ef 100644 --- a/etc/config.sh/bash_completion +++ b/etc/config.sh/bash_completion @@ -22,8 +22,24 @@ # Uses # _of_complete_cache_ # +# Requires +# bash 4.2 or newer +# #------------------------------------------------------------------------------ +# Remove old completions (skip for tcsh wrapper), which look like: +# "complete ... -F _of_complete_ APPNAME +if [ -z "$_of_complete_tcsh" ] +then + # For economy, obtain list first + foamOldDirs="$(complete 2>/dev/null | sed -ne 's/^.*-F _of_.* \(..*\)$/\1/p')" + for cleaned in $foamOldDirs + do + complete -r $cleaned 2>/dev/null + done +fi + + # Add completion for command or directory of commands # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ unset -f foamAddCompletion 2>/dev/null @@ -173,26 +189,34 @@ _of_complete_() #------------------------------------------------------------------------------ -# Global associative array (cached options for OpenFOAM applications) -declare -gA _of_complete_cache_; -# Clear existing cache and reassign bash completions. -# But for tcsh wrapper make use of caching and avoid this overhead. -if [ -z "$_of_complete_tcsh" ] +# Uses 'declare -gA' for the implementation +# The '-A' requires bash >= 4.0 and the '-g' requires bash >= 4.2 +if [ "${BASH_VERSINFO[0]:-0}${BASH_VERSINFO[1]:-0}" -ge 42 ] then - _of_complete_cache_=() - - # Remove old completions, which look like: - # "complete ... -F _of_complete_ APPNAME - # For economy, obtain list first - foamOldDirs="$(complete 2>/dev/null | sed -ne 's/^.*-F _of_.* \(..*\)$/\1/p')" - for cleaned in $foamOldDirs - do - complete -r $cleaned 2>/dev/null - done - - # Generate completions for predefined directories - foamAddCompletion $FOAM_APPBIN + # Global associative array (cached options for OpenFOAM applications) + declare -gA _of_complete_cache_; + + # Clear existing cache and reassign bash completions. + # But for tcsh wrapper make use of caching and avoid this overhead. + if [ -z "$_of_complete_tcsh" ] + then + _of_complete_cache_=() + + # Generate completions for predefined directories + foamAddCompletion $FOAM_APPBIN + fi +else + # Bash version is too old. + ## echo "No bash completions - requires bash >= 4.2" 1>&2 + + unset -f foamAddCompletion 2>/dev/null + foamAddCompletion() + { + echo "foamAddCompletion disabled - requires bash >= 4.2" 1>&2 + } + + unset -f _of_complete_ 2>/dev/null fi #------------------------------------------------------------------------------