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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
//! Special groups:
//! {file}
//! {directory}
//! {parent}
//! {other-panel-file}
//! {other-panel-directory}
//! {other-panel-parent}
use {
super::{ExternalExecutionMode, VerbInvocation},
crate::{
app::*,
display::W,
errors::{ConfError, ProgramError},
launchable::Launchable,
path,
path_anchor::PathAnchor,
},
regex::{Captures, Regex},
std::{
collections::HashMap,
fs::OpenOptions,
io::Write,
path::{Path, PathBuf},
},
};
fn path_to_string(path: &Path, for_shell: bool) -> String {
if for_shell {
path::escape_for_shell(path)
} else {
path.to_string_lossy().to_string()
}
}
lazy_static! {
static ref GROUP: Regex = Regex::new(r"\{([^{}:]+)(?::([^{}:]+))?\}").unwrap();
}
/// Definition of how the user input should be interpreted
/// to be executed in an external command.
#[derive(Debug, Clone)]
pub struct ExternalExecution {
/// pattern of how the command is supposed to be typed in the input
invocation_pattern: VerbInvocation,
/// a regex to read the arguments in the user input
args_parser: Option<Regex>,
/// the pattern which will result in an exectuable string when
/// completed with the args
pub exec_pattern: String,
/// how the external process must be launched
pub exec_mode: ExternalExecutionMode,
/// contain the type of selection in case there's only one arg
/// and it's a path (when it's not None, the user can type ctrl-P
/// to select the argument in another panel)
pub arg_selection_type: Option<SelectionType>,
pub arg_anchor: PathAnchor,
/// whether the working dir of the external process must be set
/// to the current directory
pub set_working_dir: bool,
/// whether we need to have a secondary panel for execution
/// (which is the case when an invocation has {other-panel-file})
pub need_another_panel: bool,
}
impl ExternalExecution {
pub fn new(
invocation_str: &str,
execution_str: &str,
exec_mode: ExternalExecutionMode,
) -> Result<Self, ConfError> {
let invocation_pattern = VerbInvocation::from(invocation_str);
let mut args_parser = None;
let mut arg_selection_type = None;
let mut arg_anchor = PathAnchor::Unspecified;
let mut need_another_panel = false;
if let Some(args) = &invocation_pattern.args {
let spec = GROUP.replace_all(args, r"(?P<$1>.+)");
let spec = format!("^{}$", spec);
args_parser = match Regex::new(&spec) {
Ok(regex) => Some(regex),
Err(_) => {
return Err(ConfError::InvalidVerbInvocation { invocation: spec });
}
};
if let Some(group) = GROUP.find(args) {
if group.start() == 0 && group.end() == args.len() {
// there's one group, covering the whole args
arg_selection_type = Some(SelectionType::Any);
let group_str = group.as_str();
if group_str.ends_with("path-from-parent}") {
arg_anchor = PathAnchor::Parent;
} else if group_str.ends_with("path-from-directory}") {
arg_anchor = PathAnchor::Directory;
}
}
}
}
for group in GROUP.find_iter(execution_str) {
if group.as_str().starts_with("{other-panel-") {
need_another_panel = true;
}
}
Ok(Self {
invocation_pattern,
args_parser,
exec_pattern: execution_str.to_string(),
exec_mode,
arg_selection_type,
arg_anchor,
need_another_panel,
set_working_dir: false,
})
}
pub fn name(&self) -> &str {
&self.invocation_pattern.name
}
/// Assuming the verb has been matched, check whether the arguments
/// are OK according to the regex. Return none when there's no problem
/// and return the error to display if arguments don't match
pub fn check_args(
&self,
invocation: &VerbInvocation,
other_path: &Option<PathBuf>,
) -> Option<String> {
if self.need_another_panel && other_path.is_none() {
return Some("This verb needs exactly two panels".to_string());
}
match (&invocation.args, &self.args_parser) {
(None, None) => None,
(None, Some(ref regex)) => {
if regex.is_match("") {
None
} else {
Some(self.invocation_pattern.to_string_for_name(&invocation.name))
}
}
(Some(ref s), Some(ref regex)) => {
if regex.is_match(&s) {
None
} else {
Some(self.invocation_pattern.to_string_for_name(&invocation.name))
}
}
(Some(_), None) => Some(format!("{} doesn't take arguments", invocation.name)),
}
}
/// build the map which will be used to replace braced parts (i.e. like {part}) in
/// the execution pattern
fn replacement_map(
&self,
sel: Selection<'_>,
other_file: &Option<PathBuf>,
args: &Option<String>,
for_shell: bool,
) -> HashMap<String, String> {
let mut map = HashMap::new();
let file = sel.path;
// first we add the replacements computed from the given path
let parent = file.parent().unwrap_or(file); // when there's no parent... we take file
let file_str = path_to_string(file, for_shell);
let parent_str = path_to_string(parent, for_shell);
map.insert("line".to_string(), sel.line.to_string());
map.insert("file".to_string(), file_str.to_string());
map.insert("parent".to_string(), parent_str.to_string());
let dir_str = if file.is_dir() { file_str } else { parent_str };
map.insert("directory".to_string(), dir_str);
if self.need_another_panel {
if let Some(other_file) = other_file {
let other_parent = other_file.parent().unwrap_or(other_file);
let other_file_str = path_to_string(other_file, for_shell);
let other_parent_str = path_to_string(other_parent, for_shell);
map.insert("other-panel-file".to_string(), other_file_str.to_string());
map.insert("other-panel-parent".to_string(), other_parent_str.to_string());
let other_dir_str = if other_file.is_dir() { other_file_str } else { other_parent_str };
map.insert("other-panel-directory".to_string(), other_dir_str);
}
}
// then the ones computed from the user input
let default_args;
let args = match args {
Some(s) => s,
None => {
// empty args are useful when the args_parser contains
// an optional group
default_args = String::new();
&default_args
}
};
if let Some(r) = &self.args_parser {
if let Some(input_cap) = r.captures(&args) {
for name in r.capture_names().flatten() {
if let Some(c) = input_cap.name(name) {
map.insert(name.to_string(), c.as_str().to_string());
}
}
}
}
map
}
pub fn to_cmd_result(
&self,
w: &mut W,
sel: Selection<'_>,
other_file: &Option<PathBuf>,
args: &Option<String>,
con: &AppContext,
) -> Result<AppStateCmdResult, ProgramError> {
if self.exec_mode.is_from_shell() {
self.exec_from_shell_cmd_result(sel, other_file, args, con)
} else {
self.exec_cmd_result(w, sel, other_file, args)
}
}
/// build the cmd result as an executable which will be called from shell
fn exec_from_shell_cmd_result(
&self,
sel: Selection<'_>,
other_file: &Option<PathBuf>,
args: &Option<String>,
con: &AppContext,
) -> Result
|