compsys 0.8.15

Zsh-compatible completion system for zshrs
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Complete library of zsh completion system functions
//!
//! This module implements ALL library functions documented in zshcompsys(1).
//! Command-specific completions (_git, _docker, etc.) remain as shell code.

use crate::base::{CompleterResult, MainCompleteState};
use crate::compcore::CompletionState;
use crate::completion::{Completion, CompletionFlags};
use std::collections::HashMap;
use std::path::Path;

// =============================================================================
// Missing functions from zshcompsys man page
// =============================================================================

/// _absolute_command_paths - Complete commands with absolute paths
pub fn absolute_command_paths(state: &mut CompletionState) -> bool {
    let prefix = state.params.prefix.clone();

    // Search PATH for executables
    if let Ok(path_var) = std::env::var("PATH") {
        state.begin_group("commands", true);

        for dir in path_var.split(':') {
            if let Ok(entries) = std::fs::read_dir(dir) {
                for entry in entries.flatten() {
                    let name = entry.file_name();
                    let name_str = name.to_string_lossy();

                    if name_str.starts_with(&prefix) {
                        // Return absolute path
                        let full_path = entry.path();
                        if is_executable(&full_path) {
                            state.add_match(
                                Completion::new(full_path.to_string_lossy().to_string()),
                                Some("commands"),
                            );
                        }
                    }
                }
            }
        }

        state.end_group();
        state.nmatches > 0
    } else {
        false
    }
}

/// _canonical_paths - Complete canonical (resolved) paths
pub fn canonical_paths(
    state: &mut CompletionState,
    tag: &str,
    description: &str,
    paths: &[String],
) -> bool {
    let prefix = state.params.prefix.clone();

    state.begin_group(tag, true);
    if !description.is_empty() {
        state.add_explanation(description.to_string(), Some(tag));
    }

    for path in paths {
        if let Ok(canonical) = std::fs::canonicalize(path) {
            let canonical_str = canonical.to_string_lossy().to_string();
            if canonical_str.starts_with(&prefix) {
                state.add_match(Completion::new(canonical_str), Some(tag));
            }
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// _cmdambivalent - Handle commands that can be run with or without arguments
pub fn cmdambivalent(state: &mut MainCompleteState) -> bool {
    // If no arguments yet, complete as command
    if state.comp.params.current <= 1 {
        command_names(&mut state.comp, false)
    } else {
        // Otherwise use normal completion
        true
    }
}

/// _cmdstring - Complete a command string (for eval, etc.)
pub fn cmdstring(state: &mut CompletionState) -> bool {
    // Complete as if it were a command line
    command_names(state, false)
}

/// _command_names - Complete command names
pub fn command_names(state: &mut CompletionState, externals_only: bool) -> bool {
    let prefix = state.params.prefix.clone();

    state.begin_group("commands", true);

    // External commands from PATH
    if let Ok(path_var) = std::env::var("PATH") {
        for dir in path_var.split(':') {
            if let Ok(entries) = std::fs::read_dir(dir) {
                for entry in entries.flatten() {
                    let name = entry.file_name();
                    let name_str = name.to_string_lossy();

                    if name_str.starts_with(&prefix) && is_executable(&entry.path()) {
                        state.add_match(Completion::new(name_str.to_string()), Some("commands"));
                    }
                }
            }
        }
    }

    if !externals_only {
        // Would also add builtins, aliases, functions
        // These come from the shell state
    }

    state.end_group();
    state.nmatches > 0
}

/// _comp_caller_options - Get options from calling context
pub fn comp_caller_options() -> HashMap<String, bool> {
    // Returns shell options that were set when completion was invoked
    // This is stored in $_comp_caller_options in zsh
    HashMap::new()
}

/// _comp_priv_prefix - Prefix for privilege escalation (sudo, doas, etc.)
pub fn comp_priv_prefix() -> Vec<String> {
    // Returns the privilege prefix if any
    Vec::new()
}

/// _completers - List active completers
pub fn completers(state: &MainCompleteState, print_current: bool) -> Vec<String> {
    if print_current {
        vec![state.ctx.completer.clone()]
    } else {
        state.completers.clone()
    }
}

/// _default - Default completion (files)
pub fn default_complete(state: &mut CompletionState) -> bool {
    crate::files::files_execute(state, &crate::files::FilesOpts::default())
}

/// _dir_list - Complete colon-separated directory list
pub fn dir_list(
    state: &mut CompletionState,
    separator: Option<&str>,
    strip_trailing: bool,
) -> bool {
    let sep = separator.unwrap_or(":");
    let prefix = state.params.prefix.clone();

    // Handle the last component after separator
    let (base, current) = if let Some(pos) = prefix.rfind(sep) {
        (&prefix[..pos + sep.len()], &prefix[pos + sep.len()..])
    } else {
        ("", prefix.as_str())
    };

    // Complete directories
    let dir_to_scan = if current.contains('/') {
        let pos = current.rfind('/').unwrap();
        &current[..pos + 1]
    } else {
        "."
    };

    if let Ok(entries) = std::fs::read_dir(dir_to_scan) {
        state.begin_group("directories", true);

        for entry in entries.flatten() {
            if entry.path().is_dir() {
                let name = entry.file_name();
                let name_str = name.to_string_lossy();
                let full = if dir_to_scan == "." {
                    name_str.to_string()
                } else {
                    format!("{}{}", dir_to_scan, name_str)
                };

                if full.starts_with(current) {
                    let mut comp_str = format!("{}{}", base, full);
                    if !strip_trailing {
                        comp_str.push('/');
                    }
                    let mut comp = Completion::new(comp_str);
                    comp.flags |= CompletionFlags::NOSPACE;
                    state.add_match(comp, Some("directories"));
                }
            }
        }

        state.end_group();
    }

    state.nmatches > 0
}

/// _email_addresses - Complete email addresses
pub fn email_addresses(state: &mut CompletionState, complete_struc: bool) -> bool {
    let prefix = state.params.prefix.clone();

    // Try to read from common sources
    let mut addresses = Vec::new();

    // ~/.mailrc
    if let Ok(home) = std::env::var("HOME") {
        let mailrc = format!("{}/.mailrc", home);
        if let Ok(content) = std::fs::read_to_string(&mailrc) {
            for line in content.lines() {
                if line.starts_with("alias ") {
                    let parts: Vec<&str> = line.split_whitespace().collect();
                    if parts.len() >= 3 {
                        addresses.push(parts[2].to_string());
                    }
                }
            }
        }
    }

    state.begin_group("email-addresses", true);

    for addr in &addresses {
        if addr.starts_with(&prefix) {
            let comp = if complete_struc && !addr.contains('<') {
                Completion::new(format!("<{}>", addr))
            } else {
                Completion::new(addr.clone())
            };
            state.add_match(comp, Some("email-addresses"));
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// _gnu_generic - Generic GNU-style option completion from --help
pub fn gnu_generic(state: &mut CompletionState, command: &str) -> bool {
    let prefix = state.params.prefix.clone();

    // Run command --help and parse options
    let output = std::process::Command::new(command).arg("--help").output();

    let help_text = match output {
        Ok(out) => {
            let stdout = String::from_utf8_lossy(&out.stdout);
            let stderr = String::from_utf8_lossy(&out.stderr);
            format!("{}{}", stdout, stderr)
        }
        Err(_) => return false,
    };

    state.begin_group("options", true);

    // Parse --option and -o patterns
    for line in help_text.lines() {
        let line = line.trim();

        // Match patterns like: --option, -o, --option=ARG
        let mut i = 0;
        while i < line.len() {
            if line[i..].starts_with("--") {
                let start = i;
                i += 2;
                while i < line.len() {
                    let c = line.chars().nth(i).unwrap_or(' ');
                    if c.is_alphanumeric() || c == '-' || c == '_' {
                        i += 1;
                    } else {
                        break;
                    }
                }
                let opt = &line[start..i];
                if opt.len() > 2 && opt.starts_with(&prefix) {
                    // Check for =ARG
                    let has_arg = line[i..].starts_with("=") || line[i..].starts_with("[=");
                    let mut comp = Completion::new(opt.to_string());
                    if has_arg {
                        comp.suf = Some("=".to_string());
                        comp.flags |= CompletionFlags::NOSPACE;
                    }
                    state.add_match(comp, Some("options"));
                }
            } else if line[i..].starts_with("-") && !line[i..].starts_with("--") {
                let start = i;
                i += 1;
                if i < line.len()
                    && line
                        .chars()
                        .nth(i)
                        .map(|c| c.is_alphanumeric())
                        .unwrap_or(false)
                {
                    i += 1;
                    let opt = &line[start..i];
                    if opt.starts_with(&prefix) {
                        state.add_match(Completion::new(opt.to_string()), Some("options"));
                    }
                }
            } else {
                i += 1;
            }
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// _options - Complete shell options
pub fn options(state: &mut CompletionState, shell_options: &[(&str, bool)]) -> bool {
    let prefix = state.params.prefix.clone();

    state.begin_group("options", true);

    for (opt, is_set) in shell_options {
        if opt.starts_with(&prefix) {
            let mut comp = Completion::new(opt.to_string());
            comp.disp = Some(format!(
                "{} ({})",
                opt,
                if *is_set { "set" } else { "unset" }
            ));
            state.add_match(comp, Some("options"));
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// _options_set - Complete currently set options
pub fn options_set(state: &mut CompletionState, shell_options: &[(&str, bool)]) -> bool {
    let set_opts: Vec<(&str, bool)> = shell_options
        .iter()
        .filter(|(_, is_set)| *is_set)
        .copied()
        .collect();
    options(state, &set_opts)
}

/// _options_unset - Complete currently unset options
pub fn options_unset(state: &mut CompletionState, shell_options: &[(&str, bool)]) -> bool {
    let unset_opts: Vec<(&str, bool)> = shell_options
        .iter()
        .filter(|(_, is_set)| !*is_set)
        .copied()
        .collect();
    options(state, &unset_opts)
}

/// _parameters - Complete parameter (variable) names
pub fn parameters(state: &mut CompletionState, params: &HashMap<String, String>) -> bool {
    let prefix = state.params.prefix.clone();

    state.begin_group("parameters", true);

    for (name, _value) in params {
        if name.starts_with(&prefix) {
            state.add_match(Completion::new(name.clone()), Some("parameters"));
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// _path_files - Complete files with path handling
pub fn path_files(state: &mut CompletionState, opts: &PathFilesOpts) -> bool {
    let prefix = state.params.prefix.clone();

    // Determine directory to search
    let (dir, file_prefix) = if let Some(sep) = prefix.rfind('/') {
        (prefix[..sep + 1].to_string(), &prefix[sep + 1..])
    } else {
        (".".to_string(), prefix.as_str())
    };

    // Handle -W (search in specific directories)
    let search_dirs = if let Some(ref dirs) = opts.search_dirs {
        dirs.clone()
    } else {
        vec![dir.clone()]
    };

    state.begin_group(opts.tag.as_deref().unwrap_or("files"), true);

    for search_dir in &search_dirs {
        let full_dir = if search_dir.ends_with('/') {
            format!("{}{}", search_dir, dir.trim_start_matches("./"))
        } else {
            search_dir.clone()
        };

        if let Ok(entries) = std::fs::read_dir(&full_dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let name_str = name.to_string_lossy();

                if !name_str.starts_with(file_prefix) {
                    continue;
                }

                // Apply glob filter
                if let Some(ref glob) = opts.glob {
                    if !glob_matches(glob, &name_str) && !entry.path().is_dir() {
                        continue;
                    }
                }

                // Apply ignore patterns
                if let Some(ref ignore) = opts.ignore {
                    if glob_matches(ignore, &name_str) {
                        continue;
                    }
                }

                let is_dir = entry.path().is_dir();

                // Filter by type
                if opts.dirs_only && !is_dir {
                    continue;
                }
                if opts.files_only && is_dir {
                    continue;
                }

                let full_path = if dir == "." {
                    name_str.to_string()
                } else {
                    format!("{}{}", dir, name_str)
                };

                let mut comp = Completion::new(full_path);

                // Set file mode character for LS_COLORS coloring
                if is_dir {
                    comp.modec = '/';
                    comp.suf = Some("/".to_string());
                    comp.flags |= CompletionFlags::NOSPACE;
                } else if entry.path().is_symlink() {
                    comp.modec = '@';
                } else {
                    #[cfg(unix)]
                    {
                        use std::os::unix::fs::PermissionsExt;
                        if let Ok(meta) = entry.metadata() {
                            if meta.permissions().mode() & 0o111 != 0 {
                                comp.modec = '*';
                            }
                        }
                    }
                }

                // Apply prefix/suffix
                if let Some(ref p) = opts.prefix {
                    comp.pre = Some(p.clone());
                }
                if let Some(ref s) = opts.suffix {
                    comp.suf = Some(s.clone());
                }

                state.add_match(comp, opts.tag.as_deref());
            }
        }
    }

    state.end_group();
    state.nmatches > 0
}

/// Options for _path_files
#[derive(Default)]
pub struct PathFilesOpts {
    pub glob: Option<String>,
    pub ignore: Option<String>,
    pub prefix: Option<String>,
    pub suffix: Option<String>,
    pub search_dirs: Option<Vec<String>>,
    pub dirs_only: bool,
    pub files_only: bool,
    pub tag: Option<String>,
}

/// _precommand - Complete after a precommand (sudo, nohup, etc.)
pub fn precommand(state: &mut MainCompleteState) -> bool {
    // Skip the precommand and complete as normal command
    if state.comp.params.current > 1 {
        // Treat rest as command line
        matches!(
            crate::base::normal_complete(state),
            CompleterResult::Matched
        )
    } else {
        false
    }
}

/// _tilde_files - Complete files with tilde expansion
pub fn tilde_files(state: &mut CompletionState) -> bool {
    let prefix = state.params.prefix.clone();

    if prefix.starts_with('~') {
        // Expand tilde
        if let Ok(home) = std::env::var("HOME") {
            let expanded = if prefix == "~" {
                home.clone()
            } else if prefix.starts_with("~/") {
                format!("{}{}", home, &prefix[1..])
            } else {
                // ~user form - would need to look up user
                return false;
            };

            // Update state prefix for completion
            let old_prefix = state.params.prefix.clone();
            state.params.prefix = expanded;
            state.params.iprefix = "~".to_string();

            let result = crate::files::files_execute(state, &crate::files::FilesOpts::default());

            // Restore
            state.params.prefix = old_prefix;
            state.params.iprefix.clear();

            return result;
        }
    }

    false
}

/// _widgets - Complete widget names
pub fn widgets(state: &mut CompletionState, widgets: &[String], pattern: Option<&str>) -> bool {
    let prefix = state.params.prefix.clone();

    state.begin_group("widgets", true);

    for widget in widgets {
        if !widget.starts_with(&prefix) {
            continue;
        }

        if let Some(pat) = pattern {
            if !glob_matches(pat, widget) {
                continue;
            }
        }

        state.add_match(Completion::new(widget.clone()), Some("widgets"));
    }

    state.end_group();
    state.nmatches > 0
}

// =============================================================================
// Helper functions
// =============================================================================

fn is_executable(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(meta) = path.metadata() {
            let mode = meta.permissions().mode();
            return mode & 0o111 != 0;
        }
    }
    #[cfg(not(unix))]
    {
        // On non-Unix, check for common executable extensions
        if let Some(ext) = path.extension() {
            let ext = ext.to_string_lossy().to_lowercase();
            return matches!(ext.as_str(), "exe" | "bat" | "cmd" | "com");
        }
    }
    false
}

fn glob_matches(pattern: &str, text: &str) -> bool {
    let pattern_chars: Vec<char> = pattern.chars().collect();
    let text_chars: Vec<char> = text.chars().collect();
    glob_match_helper(&pattern_chars, &text_chars)
}

fn glob_match_helper(pattern: &[char], text: &[char]) -> bool {
    match (pattern.first(), text.first()) {
        (None, None) => true,
        (Some('*'), _) => {
            glob_match_helper(&pattern[1..], text)
                || (!text.is_empty() && glob_match_helper(pattern, &text[1..]))
        }
        (Some('?'), Some(_)) => glob_match_helper(&pattern[1..], &text[1..]),
        (Some(p), Some(t)) if p == t => glob_match_helper(&pattern[1..], &text[1..]),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_glob_matches() {
        assert!(glob_matches("*.rs", "main.rs"));
        assert!(glob_matches("_*", "_git"));
        assert!(!glob_matches("*.rs", "main.txt"));
    }

    #[test]
    fn test_is_executable() {
        // /bin/ls should be executable
        assert!(is_executable(Path::new("/bin/ls")) || is_executable(Path::new("/usr/bin/ls")));
    }
}