From f0d6a38a7fcb730bf5e903d6f59248dea33edb8b Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Sun, 18 Mar 2018 17:16:06 +0100
Subject: [PATCH] Added completion for shell commands

Slow implementation, calls scripts everytime
---
 patches/patch.sh             |  6 ++++++
 scripts/config.sh            |  4 ++++
 scripts/mklib.sh             |  5 +++++
 scripts/newcv.sh             |  5 +++++
 scripts/partial_tempering.sh |  5 +++++
 scripts/vim2html.sh          |  5 +++++
 src/cltools/Completion.cpp   | 27 +++++++++++++++++++++++++++
 7 files changed, 57 insertions(+)

diff --git a/patches/patch.sh b/patches/patch.sh
index 633bd7301..1158207a8 100755
--- a/patches/patch.sh
+++ b/patches/patch.sh
@@ -75,6 +75,7 @@ do
     (--list-engines|-l) test -n "$action" && multiple_actions=yes ; action=list ;;
     (--info|-i)         test -n "$action" && multiple_actions=yes ; action=info ;;
     (--new=*)           test -n "$action" && multiple_actions=yes ; action=new ; newpatch="${prefix_option#--new=}" ;;
+    (--options)         test -n "$action" && multiple_actions=yes ; action=options ;;
     (--description)     echo "patch an MD engine" ; exit ;;
     (--engine=*) engine="${prefix_option#--engine=}" ;;
     (--mdroot=*) mdroot="${prefix_option#--mdroot=}" ;;
@@ -114,6 +115,11 @@ if [ -z "$action" ] ; then
   exit
 fi
 
+if [ "$action" = options ] ; then
+  echo "--help -h --patch -p --save -s --save-originals --revert -R -r --list-engines -l --info -i --new --options --description --engine --mdroot --mode --diff --engine -e --mdroot --root --diff -d --mode -m --new -n --static --shared --runtime --force -f --quiet -q"
+  exit 0
+fi
+
 test -n "$quiet" || echo "PLUMED patching tool"
 test -n "$quiet" || echo
 if [ -z "$PLUMED_ROOT" ]
diff --git a/scripts/config.sh b/scripts/config.sh
index 727d3e8ca..47526f0c2 100755
--- a/scripts/config.sh
+++ b/scripts/config.sh
@@ -40,6 +40,10 @@ do
     echo "inquire plumed about how it was configure"
     exit 0
     ;;
+  (--options)
+    echo "--help -h --description --options --quiet -q --version -v show has module"
+    exit 0
+    ;;
   (--quiet|-q) quiet=yes ;;
   (--version|-v) action=version ;;
   (show)
diff --git a/scripts/mklib.sh b/scripts/mklib.sh
index 2612a488a..55f7c4004 100755
--- a/scripts/mklib.sh
+++ b/scripts/mklib.sh
@@ -7,6 +7,11 @@ if [ "$1" = --description ] ; then
   exit 0
 fi
 
+if [ "$1" = --options ] ; then
+  echo "--description"
+  exit 0
+fi
+
 if [ $# != 1 ] || [[ "$1" != *.cpp ]] ;
 then
   echo "ERROR"
diff --git a/scripts/newcv.sh b/scripts/newcv.sh
index b3b573739..727c90a93 100755
--- a/scripts/newcv.sh
+++ b/scripts/newcv.sh
@@ -5,6 +5,11 @@ if [ "$1" = --description ] ; then
   exit 0
 fi
 
+if [ "$1" = --options ] ; then
+  echo "--description"
+  exit 0
+fi
+
 if [ $# != 2 ] ;
 then
   echo "ERROR"
diff --git a/scripts/partial_tempering.sh b/scripts/partial_tempering.sh
index d4ba66985..b90aead9b 100755
--- a/scripts/partial_tempering.sh
+++ b/scripts/partial_tempering.sh
@@ -4,6 +4,11 @@ if [ "$1" = --description ] ; then
   exit 0
 fi
 
+if [ "$1" = --options ] ; then
+  echo "--description --gromacs4 --help -h"
+  exit 0
+fi
+
 if [ "$1" = --help ] || [ "$1" = -h ] ; then
   cat <<EOF
 Usage:
diff --git a/scripts/vim2html.sh b/scripts/vim2html.sh
index a9f6a43cb..0c727e622 100755
--- a/scripts/vim2html.sh
+++ b/scripts/vim2html.sh
@@ -7,6 +7,11 @@ if [ "$1" = --description ] ; then
   exit 0
 fi
 
+if [ "$1" = --options ] ; then
+  echo "--description --options --annotate-syntax --pdf --crop --fs --colors"
+  exit 0
+fi
+
 if [ "$1" = --help ] ; then
 cat << EOF
 Usage:
diff --git a/src/cltools/Completion.cpp b/src/cltools/Completion.cpp
index ccc3b4cf6..087b213f7 100644
--- a/src/cltools/Completion.cpp
+++ b/src/cltools/Completion.cpp
@@ -117,6 +117,33 @@ int Completion::main(FILE* in, FILE*out,Communicator& pc) {
     fprintf(out,"\"\n");
   }
 
+  for(unsigned j=0; j<tmp.size(); j++) {
+    std::string s=tmp[j];
+// handle - sign (convert to underscore)
+    for(;;) {
+      size_t n=s.find("-");
+      if(n==std::string::npos) break;
+      s[n]='_';
+    }
+    fprintf(out,"local cmd_keys_%s=\"",s.c_str());
+    std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"/scripts/"+s+".sh\" --options";
+    FILE *fp=popen(cmd.c_str(),"r");
+    std::string line,manual;
+    while(Tools::getline(fp,line))manual+=line;
+    pclose(fp);
+    std::vector<std::string> keys=Tools::getWords(manual);
+    for(unsigned k=0; k<keys.size(); k++) {
+// handle --help/-h
+      std::string s=keys[k];
+      for(;;) {
+        size_t n=s.find("/");
+        if(n==std::string::npos) break;
+        s[n]=' ';
+      }
+      fprintf(out," %s",s.c_str());
+    }
+    fprintf(out,"\"\n");
+  }
   fprintf(out,"%s\n",completion);
   std::string name=config::getPlumedProgramName();
 
-- 
GitLab