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 "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 : { 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 4 : std::string description()const override { 60 4 : return "dump a function usable for programmable completion"; 61 : } 62 : }; 63 : 64 10423 : PLUMED_REGISTER_CLTOOL(Completion,"completion") 65 : 66 3473 : void Completion::registerKeywords( Keywords& keys ) { 67 3473 : CLTool::registerKeywords( keys ); 68 3473 : } 69 : 70 4 : Completion::Completion(const CLToolOptions& co ): 71 4 : CLTool(co) 72 : { 73 4 : inputdata=commandline; 74 4 : } 75 : 76 0 : int Completion::main(FILE* in, FILE*out,Communicator& pc) { 77 : static const char completion [] = { 78 : #include "completion.xxd" 79 : // cppcheck-suppress syntaxError 80 : , 0x00 81 : }; 82 : std::fprintf(out,"local cmds=\"help -h --help"); 83 : // Build list of available C++ tools: 84 0 : std::vector<std::string> availableCxx=cltoolRegister().list(); 85 : // Build list of available shell tools: 86 0 : std::vector<std::string> tmp=Tools::ls(std::string(config::getPlumedRoot()+"/scripts")); 87 0 : for(unsigned j=0; j<tmp.size(); ++j) { 88 0 : size_t ff=tmp[j].find(".sh"); 89 0 : if(ff==std::string::npos) tmp[j].erase(); 90 0 : else tmp[j].erase(ff); 91 : } 92 0 : for(unsigned j=0; j<availableCxx.size(); j++) std::fprintf(out," %s",availableCxx[j].c_str()); 93 0 : for(unsigned j=0; j<tmp.size(); ++j) if(tmp[j].length()>0) std::fprintf(out," %s",tmp[j].c_str()); 94 : std::fprintf(out,"\"\n"); 95 : 96 0 : for(unsigned j=0; j<availableCxx.size(); j++) { 97 0 : std::string s=availableCxx[j]; 98 : // handle - sign (convert to underscore) 99 : for(;;) { 100 0 : size_t n=s.find("-"); 101 0 : if(n==std::string::npos) break; 102 0 : s[n]='_'; 103 0 : } 104 : std::fprintf(out,"local cmd_keys_%s=\"",s.c_str()); 105 0 : std::vector<std::string> keys=cltoolRegister().getKeys(availableCxx[j]); 106 0 : for(unsigned k=0; k<keys.size(); k++) { 107 : // handle --help/-h 108 0 : std::string s=keys[k]; 109 : for(;;) { 110 0 : size_t n=s.find("/"); 111 0 : if(n==std::string::npos) break; 112 0 : s[n]=' '; 113 0 : } 114 : std::fprintf(out," %s",s.c_str()); 115 : } 116 : std::fprintf(out,"\"\n"); 117 0 : } 118 : 119 : std::fprintf(out,"%s\n",completion); 120 0 : std::string name=config::getPlumedProgramName(); 121 : 122 : std::fprintf(out, 123 : "############################################\n" 124 : "## ADD THESE COMMANDS TO YOUR .bashrc FILE:\n" 125 : "############################################\n" 126 : "# _%s() { eval \"$(%s --no-mpi completion 2>/dev/null)\";}\n" 127 : "# complete -F _%s -o default %s\n" 128 : "############################################\n", 129 : name.c_str(),name.c_str(),name.c_str(),name.c_str()); 130 : 131 0 : return 0; 132 0 : } 133 : 134 : } // End of namespace 135 : }