From 81037a6063397fded0e6574a03747503deb6b10d Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 2 Aug 2017 16:54:47 +0200
Subject: [PATCH] ENH: add tcsh completion functionality (issue #551)

- currently no cleanup of completions when deactivating an OpenFOAM
  tcsh environment

- tab completion with directories adds a space after the slash, which
  makes navigation a bit annoying.
---
 etc/config.csh/complete        | 59 ++++++++++++++++++++++++++++++++++
 etc/config.csh/tcsh_completion | 17 ++++++++++
 etc/cshrc                      |  1 +
 3 files changed, 77 insertions(+)
 create mode 100644 etc/config.csh/complete
 create mode 100644 etc/config.csh/tcsh_completion

diff --git a/etc/config.csh/complete b/etc/config.csh/complete
new file mode 100644
index 0000000000..88bd4facd8
--- /dev/null
+++ b/etc/config.csh/complete
@@ -0,0 +1,59 @@
+#!bash
+# A bash -*- sh -*- adapter for re-using OpenFOAM bash completions with tcsh
+#
+# Called with appName and COMMAND_LINE
+#
+# Source the bash completions
+. $WM_PROJECT_DIR/etc/config.sh/bash_completion
+
+appName=$1
+
+# Ensure COMP_LINE is available for bash function
+if [ "$#" -eq 2 ]
+then
+    COMP_LINE=$2
+else
+    COMP_LINE=$COMMAND_LINE
+fi
+
+# Remove the colon as a completion separator because tcsh cannot handle it
+COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
+
+# Set COMP_WORDS in a way that can be handled by the bash script.
+COMP_WORDS=($COMP_LINE)
+
+# The cursor is at the end of parameter #1.
+# We must check for a space as the last character which will
+# tell us that the previous word is complete and the cursor
+# is on the next word.
+if [ "${COMP_LINE: -1}" = " " ]
+then
+    # The last character is a space, so our location is at the end
+    # of the command-line array
+    COMP_CWORD=${#COMP_WORDS[@]}
+else
+    # The last character is not a space, so our location is on the
+    # last word of the command-line array, so we must decrement the
+    # count by 1
+    COMP_CWORD=$((${#COMP_WORDS[@]}-1))
+fi
+
+# bash completions are "complete ... -F _of_APPNAME APPNAME
+_of_${appName} \
+    "$appName" "${COMP_WORDS[COMP_CWORD]}" "${COMP_WORDS[COMP_CWORD-1]}"
+
+# Need slash on the end of directories for tcsh
+reply=($(for i in ${COMPREPLY[@]}
+    do
+        if [ -d "$i" -a "${i#/}" = "$i" ]
+        then
+            echo "$i/"
+        else
+            echo "$i"
+        fi
+    done
+))
+
+echo ${reply[@]}
+
+#------------------------------------------------------------------------------
diff --git a/etc/config.csh/tcsh_completion b/etc/config.csh/tcsh_completion
new file mode 100644
index 0000000000..520d2624bf
--- /dev/null
+++ b/etc/config.csh/tcsh_completion
@@ -0,0 +1,17 @@
+#----------------------------------*-sh-*--------------------------------------
+# Tcsh completions for OpenFOAM applications
+# Using bash_completion functions for the hard work
+
+if ($?tcsh) then  # tcsh only
+    if ( -f $WM_PROJECT_DIR/etc/config.sh/bash_completion \
+      && -f $WM_PROJECT_DIR/etc/config.csh/complete) then
+        foreach appName (`sed -ne 's/^.*&& complete.* //p' $WM_PROJECT_DIR/etc/config.sh/bash_completion`)
+            # Pass explicitly
+            ## complete $appName 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete '$appName' "${COMMAND_LINE}"`,'
+            # Pass via environment
+            complete $appName 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete '$appName'`,'
+        end
+    endif
+endif
+
+#------------------------------------------------------------------------------
diff --git a/etc/cshrc b/etc/cshrc
index 6c42ebdf61..7b8da52c26 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -219,6 +219,7 @@ _foamEtc config.csh/settings
 
 if ($?prompt) then  # Interactive shell
     _foamEtc config.csh/aliases
+    _foamEtc config.csh/tcsh_completion
 endif
 
 
-- 
GitLab