Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2018-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #include "CLTool.h" 23 : #include "core/CLToolRegister.h" 24 : #include "tools/Tools.h" 25 : #include "config/Config.h" 26 : #include "core/ActionRegister.h" 27 : #include <cstdio> 28 : #include <string> 29 : #include <vector> 30 : #include <iostream> 31 : 32 : namespace PLMD { 33 : namespace cltools { 34 : 35 : //+PLUMEDOC TOOLS completion 36 : /* 37 : Dumps the body of a bash function to be used for auto completion. 38 : 39 : Users will typically not need this command. 40 : See more at \ref BashAutocompletion 41 : 42 : \par Examples 43 : 44 : \verbatim 45 : plumed completion 46 : \endverbatim 47 : 48 : 49 : */ 50 : //+ENDPLUMEDOC 51 : 52 : class Completion: 53 : public CLTool { 54 : public: 55 : static void registerKeywords( Keywords& keys ); 56 : explicit Completion(const CLToolOptions& co ); 57 : int main(FILE* in, FILE*out,Communicator& pc) override; 58 4 : std::string description()const override { 59 4 : return "dump a function usable for programmable completion"; 60 : } 61 : }; 62 : 63 16330 : PLUMED_REGISTER_CLTOOL(Completion,"completion") 64 : 65 5442 : void Completion::registerKeywords( Keywords& keys ) { 66 5442 : CLTool::registerKeywords( keys ); 67 5442 : } 68 : 69 4 : Completion::Completion(const CLToolOptions& co ): 70 4 : CLTool(co) { 71 4 : inputdata=commandline; 72 4 : } 73 : 74 0 : int Completion::main(FILE* in, FILE*out,Communicator& pc) { 75 : static const char completion [] = { 76 : #include "completion.xxd" 77 : // cppcheck-suppress syntaxError 78 : , 0x00 79 : }; 80 : std::fprintf(out,"local cmds=\"help -h --help"); 81 : // Build list of available C++ tools: 82 0 : std::vector<std::string> availableCxx=cltoolRegister().list(); 83 : // Build list of available shell tools: 84 0 : std::vector<std::string> tmp=Tools::ls(std::string(config::getPlumedRoot()+"/scripts")); 85 0 : for(unsigned j=0; j<tmp.size(); ++j) { 86 0 : size_t ff=tmp[j].find(".sh"); 87 0 : if(ff==std::string::npos) { 88 0 : tmp[j].erase(); 89 : } else { 90 0 : tmp[j].erase(ff); 91 : } 92 : } 93 0 : for(unsigned j=0; j<availableCxx.size(); j++) { 94 : std::fprintf(out," %s",availableCxx[j].c_str()); 95 : } 96 0 : for(unsigned j=0; j<tmp.size(); ++j) 97 0 : if(tmp[j].length()>0) { 98 : std::fprintf(out," %s",tmp[j].c_str()); 99 : } 100 : std::fprintf(out,"\"\n"); 101 : 102 0 : for(unsigned j=0; j<availableCxx.size(); j++) { 103 0 : std::string s=availableCxx[j]; 104 : // handle - sign (convert to underscore) 105 : for(;;) { 106 0 : size_t n=s.find("-"); 107 0 : if(n==std::string::npos) { 108 : break; 109 : } 110 0 : s[n]='_'; 111 0 : } 112 : std::fprintf(out,"local cmd_keys_%s=\"",s.c_str()); 113 0 : std::vector<std::string> keys=cltoolRegister().getKeys(availableCxx[j]); 114 0 : for(unsigned k=0; k<keys.size(); k++) { 115 : // handle --help/-h 116 0 : std::string s=keys[k]; 117 : for(;;) { 118 0 : size_t n=s.find("/"); 119 0 : if(n==std::string::npos) { 120 : break; 121 : } 122 0 : s[n]=' '; 123 0 : } 124 : std::fprintf(out," %s",s.c_str()); 125 : } 126 : std::fprintf(out,"\"\n"); 127 0 : } 128 : 129 : std::fprintf(out,"%s\n",completion); 130 0 : std::string name=config::getPlumedProgramName(); 131 : 132 : std::fprintf(out, 133 : "############################################\n" 134 : "## ADD THESE COMMANDS TO YOUR .bashrc FILE:\n" 135 : "############################################\n" 136 : "# _%s() { eval \"$(%s --no-mpi completion 2>/dev/null)\";}\n" 137 : "# complete -F _%s -o default %s\n" 138 : "############################################\n", 139 : name.c_str(),name.c_str(),name.c_str(),name.c_str()); 140 : 141 0 : return 0; 142 0 : } 143 : 144 : } // End of namespace 145 : }