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 : You typically will not need to use this command it is involved during complation and is 40 : used to construct everything that is required for the autocompletion functionality that 41 : is described [here](module_cltools.md). 42 : 43 : ## Examples 44 : 45 : ```plumed 46 : plumed completion 47 : ``` 48 : 49 : 50 : */ 51 : //+ENDPLUMEDOC 52 : 53 : class Completion: 54 : public CLTool { 55 : public: 56 : static void registerKeywords( Keywords& keys ); 57 : explicit Completion(const CLToolOptions& co ); 58 : int main(FILE* in, FILE*out,Communicator& pc) override; 59 5 : std::string description()const override { 60 5 : return "dump a function usable for programmable completion"; 61 : } 62 : }; 63 : 64 16259 : PLUMED_REGISTER_CLTOOL(Completion,"completion") 65 : 66 5418 : void Completion::registerKeywords( Keywords& keys ) { 67 5418 : CLTool::registerKeywords( keys ); 68 5418 : } 69 : 70 5 : Completion::Completion(const CLToolOptions& co ): 71 5 : CLTool(co) { 72 5 : inputdata=commandline; 73 5 : } 74 : 75 0 : int Completion::main(FILE* in, FILE*out,Communicator& pc) { 76 : static const char completion [] = { 77 : #include "completion.xxd" 78 : // cppcheck-suppress syntaxError 79 : , 0x00 80 : }; 81 : std::fprintf(out,"local cmds=\"help -h --help"); 82 : // Build list of available C++ tools: 83 0 : std::vector<std::string> availableCxx=cltoolRegister().list(); 84 : // Build list of available shell tools: 85 0 : std::vector<std::string> tmp=Tools::ls(std::string(config::getPlumedRoot()+"/scripts")); 86 0 : for(unsigned j=0; j<tmp.size(); ++j) { 87 0 : size_t ff=tmp[j].find(".sh"); 88 0 : if(ff==std::string::npos) { 89 0 : tmp[j].erase(); 90 : } else { 91 0 : tmp[j].erase(ff); 92 : } 93 : } 94 0 : for(unsigned j=0; j<availableCxx.size(); j++) { 95 : std::fprintf(out," %s",availableCxx[j].c_str()); 96 : } 97 0 : for(unsigned j=0; j<tmp.size(); ++j) 98 0 : if(tmp[j].length()>0) { 99 : std::fprintf(out," %s",tmp[j].c_str()); 100 : } 101 : std::fprintf(out,"\"\n"); 102 : 103 0 : for(unsigned j=0; j<availableCxx.size(); j++) { 104 0 : std::string s=availableCxx[j]; 105 : // handle - sign (convert to underscore) 106 : for(;;) { 107 0 : size_t n=s.find("-"); 108 0 : if(n==std::string::npos) { 109 : break; 110 : } 111 0 : s[n]='_'; 112 0 : } 113 : std::fprintf(out,"local cmd_keys_%s=\"",s.c_str()); 114 0 : std::vector<std::string> keys=cltoolRegister().getKeys(availableCxx[j]); 115 0 : for(unsigned k=0; k<keys.size(); k++) { 116 : // handle --help/-h 117 0 : std::string s=keys[k]; 118 : for(;;) { 119 0 : size_t n=s.find("/"); 120 0 : if(n==std::string::npos) { 121 : break; 122 : } 123 0 : s[n]=' '; 124 0 : } 125 : std::fprintf(out," %s",s.c_str()); 126 : } 127 : std::fprintf(out,"\"\n"); 128 0 : } 129 : 130 : std::fprintf(out,"%s\n",completion); 131 0 : std::string name=config::getPlumedProgramName(); 132 : 133 : std::fprintf(out, 134 : "############################################\n" 135 : "## ADD THESE COMMANDS TO YOUR .bashrc FILE:\n" 136 : "############################################\n" 137 : "# _%s() { eval \"$(%s --no-mpi completion 2>/dev/null)\";}\n" 138 : "# complete -F _%s -o default %s\n" 139 : "############################################\n", 140 : name.c_str(),name.c_str(),name.c_str(),name.c_str()); 141 : 142 0 : return 0; 143 0 : } 144 : 145 : } // End of namespace 146 : }