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