neovm-core 0.0.1

Core runtime structures for NeoVM
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
//! Directory and file attribute builtins for the Elisp interpreter.
//!
//! Provides dired-related primitives:
//! - `directory-files-and-attributes`
//! - `file-name-completion`, `file-name-all-completions`
//! - `file-attributes`, `file-attributes-lessp`
//! - `system-users`, `system-groups`

use super::error::{EvalResult, Flow, signal};
use super::eval::Context;
use super::intern::{intern, resolve_sym};
use super::value::*;
use std::collections::{HashMap, VecDeque};
#[cfg(unix)]
use std::ffi::{CStr, CString};
use std::fs;
use std::io::ErrorKind;

// ---------------------------------------------------------------------------
// Argument helpers
// ---------------------------------------------------------------------------

fn expect_range_args(name: &str, args: &[Value], min: usize, max: usize) -> Result<(), Flow> {
    if args.len() < min || args.len() > max {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_string(_name: &str, value: &Value) -> Result<String, Flow> {
    match value.kind() {
        ValueKind::String => Ok(value.as_str().unwrap().to_owned()),
        other => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("stringp"), *value],
        )),
    }
}

/// Ensure a directory path ends with '/'.
fn ensure_trailing_slash(dir: &str) -> String {
    if dir.ends_with('/') {
        dir.to_string()
    } else {
        format!("{}/", dir)
    }
}

fn file_error_symbol(kind: ErrorKind) -> &'static str {
    match kind {
        ErrorKind::NotFound => "file-missing",
        ErrorKind::AlreadyExists => "file-already-exists",
        ErrorKind::PermissionDenied => "permission-denied",
        _ => "file-error",
    }
}

fn signal_file_io(action: &str, path: &str, err: std::io::Error) -> Flow {
    signal(
        file_error_symbol(err.kind()),
        vec![
            Value::string(action),
            Value::string(err.to_string()),
            Value::string(path),
        ],
    )
}

#[cfg(unix)]
fn read_directory_names(dir: &str) -> Result<Vec<String>, Flow> {
    let dir_cstr = CString::new(dir).map_err(|_| {
        signal(
            "file-error",
            vec![
                Value::string("Opening directory"),
                Value::string("path contains interior NUL"),
                Value::string(dir),
            ],
        )
    })?;
    let dirp = unsafe { libc::opendir(dir_cstr.as_ptr()) };
    if dirp.is_null() {
        return Err(signal_file_io(
            "Opening directory",
            dir,
            std::io::Error::last_os_error(),
        ));
    }

    let mut names = Vec::new();
    loop {
        let entry = unsafe { libc::readdir(dirp) };
        if entry.is_null() {
            break;
        }
        let raw_name = unsafe { CStr::from_ptr((*entry).d_name.as_ptr()) };
        names.push(raw_name.to_string_lossy().into_owned());
    }

    let _ = unsafe { libc::closedir(dirp) };
    Ok(names)
}

#[cfg(not(unix))]
fn read_directory_names(dir: &str) -> Result<Vec<String>, Flow> {
    let entries = fs::read_dir(dir).map_err(|e| signal_file_io("Opening directory", dir, e))?;
    let mut names = vec![".".to_string(), "..".to_string()];
    for entry in entries {
        let entry = entry.map_err(|e| signal_file_io("Reading directory entry", dir, e))?;
        names.push(entry.file_name().to_string_lossy().into_owned());
    }
    Ok(names)
}

fn parse_wholenump_count(arg: Option<&Value>) -> Result<Option<usize>, Flow> {
    match arg {
        Some(v) if v.is_fixnum() && v.as_fixnum().unwrap() >= 0 => {
            Ok(Some(v.as_fixnum().unwrap() as usize))
        }
        Some(v) if v.is_fixnum() => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("wholenump"), *v],
        )),
        Some(v) if v.is_truthy() => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("wholenump"), *v],
        )),
        _ => Ok(None),
    }
}

// ---------------------------------------------------------------------------
// Time helpers
// ---------------------------------------------------------------------------

/// Convert UNIX seconds + nanoseconds to Emacs (HIGH LOW USEC PSEC) format.
fn time_to_emacs_tuple(secs: i64, nanos: i64) -> Value {
    let mut s = secs;
    let mut ns = nanos;
    if ns < 0 {
        let borrow = ((-ns) + 999_999_999) / 1_000_000_000;
        s -= borrow;
        ns += borrow * 1_000_000_000;
    } else if ns >= 1_000_000_000 {
        s += ns / 1_000_000_000;
        ns %= 1_000_000_000;
    }

    let high = s >> 16;
    let low = s & 0xFFFF;
    let usec = ns / 1_000;
    let psec = (ns % 1_000) * 1_000;

    Value::list(vec![
        Value::fixnum(high),
        Value::fixnum(low),
        Value::fixnum(usec),
        Value::fixnum(psec),
    ])
}

/// Get UNIX seconds + nanoseconds from SystemTime.
#[cfg(not(unix))]
fn system_time_to_secs_nanos(time: std::time::SystemTime) -> Option<(i64, i64)> {
    let d = time.duration_since(std::time::UNIX_EPOCH).ok()?;
    Some((d.as_secs() as i64, d.subsec_nanos() as i64))
}

#[cfg(unix)]
fn uid_to_name(uid: u32) -> Option<String> {
    unsafe {
        let mut pwd: libc::passwd = std::mem::zeroed();
        let mut result: *mut libc::passwd = std::ptr::null_mut();
        let mut buf_len = 1024usize;

        loop {
            let mut buf = vec![0u8; buf_len];
            let rc = libc::getpwuid_r(uid, &mut pwd, buf.as_mut_ptr().cast(), buf_len, &mut result);

            if rc == 0 {
                if result.is_null() || pwd.pw_name.is_null() {
                    return None;
                }
                return Some(CStr::from_ptr(pwd.pw_name).to_string_lossy().into_owned());
            }

            if rc == libc::ERANGE && buf_len < (1 << 20) {
                buf_len *= 2;
                continue;
            }

            return None;
        }
    }
}

#[cfg(unix)]
fn gid_to_name(gid: u32) -> Option<String> {
    unsafe {
        let mut grp: libc::group = std::mem::zeroed();
        let mut result: *mut libc::group = std::ptr::null_mut();
        let mut buf_len = 1024usize;

        loop {
            let mut buf = vec![0u8; buf_len];
            let rc = libc::getgrgid_r(gid, &mut grp, buf.as_mut_ptr().cast(), buf_len, &mut result);

            if rc == 0 {
                if result.is_null() || grp.gr_name.is_null() {
                    return None;
                }
                return Some(CStr::from_ptr(grp.gr_name).to_string_lossy().into_owned());
            }

            if rc == libc::ERANGE && buf_len < (1 << 20) {
                buf_len *= 2;
                continue;
            }

            return None;
        }
    }
}

// ---------------------------------------------------------------------------
// file-attributes core
// ---------------------------------------------------------------------------

/// Build the Emacs-compatible file-attributes list for a path.
///
/// Returns:
///   (TYPE NLINKS UID GID ATIME MTIME CTIME SIZE MODE GID-CHANGEP INODE DEVICE)
///
/// TYPE is:
///   t        for a directory
///   nil      for a regular file
///   string   for a symlink (the link target)
///
/// Times are in Emacs (HIGH LOW) format.
/// If ID-FORMAT is 'string, UID/GID are returned as strings; otherwise integers.
fn build_file_attributes(filename: &str, id_format_string: bool) -> Option<Value> {
    // Use symlink_metadata first to detect symlinks.
    let sym_meta = fs::symlink_metadata(filename).ok()?;

    // Determine file type.
    let file_type = if sym_meta.file_type().is_symlink() {
        // Read the symlink target.
        match fs::read_link(filename) {
            Ok(target) => Value::string(target.to_string_lossy().into_owned()),
            Err(_) => Value::string(""),
        }
    } else if sym_meta.is_dir() {
        Value::T
    } else {
        Value::NIL
    };

    // For symlinks, get the target metadata for size etc; fall back to symlink meta.
    let meta = if sym_meta.file_type().is_symlink() {
        fs::metadata(filename).unwrap_or_else(|_| sym_meta.clone())
    } else {
        sym_meta.clone()
    };

    // Number of hard links.
    #[cfg(unix)]
    let nlinks = {
        use std::os::unix::fs::MetadataExt;
        Value::fixnum(sym_meta.nlink() as i64)
    };
    #[cfg(not(unix))]
    let nlinks = Value::fixnum(1);

    // UID / GID.
    #[cfg(unix)]
    let (uid_val, gid_val) = {
        use std::os::unix::fs::MetadataExt;
        let uid = sym_meta.uid();
        let gid = sym_meta.gid();
        if id_format_string {
            (
                Value::string(uid_to_name(uid).unwrap_or_else(|| uid.to_string())),
                Value::string(gid_to_name(gid).unwrap_or_else(|| gid.to_string())),
            )
        } else {
            (Value::fixnum(uid as i64), Value::fixnum(gid as i64))
        }
    };
    #[cfg(not(unix))]
    let (uid_val, gid_val) = if id_format_string {
        (Value::string("0"), Value::string("0"))
    } else {
        (Value::fixnum(0), Value::fixnum(0))
    };

    // Access time.
    #[cfg(unix)]
    let atime = {
        use std::os::unix::fs::MetadataExt;
        time_to_emacs_tuple(sym_meta.atime(), sym_meta.atime_nsec())
    };
    #[cfg(not(unix))]
    let atime = meta
        .accessed()
        .ok()
        .and_then(system_time_to_secs_nanos)
        .map(|(secs, nanos)| time_to_emacs_tuple(secs, nanos))
        .unwrap_or(Value::NIL);

    // Modification time.
    #[cfg(unix)]
    let mtime = {
        use std::os::unix::fs::MetadataExt;
        time_to_emacs_tuple(meta.mtime(), meta.mtime_nsec())
    };
    #[cfg(not(unix))]
    let mtime = meta
        .modified()
        .ok()
        .and_then(system_time_to_secs_nanos)
        .map(|(secs, nanos)| time_to_emacs_tuple(secs, nanos))
        .unwrap_or(Value::NIL);

    // Status change time (ctime on Unix, creation time on other platforms).
    #[cfg(unix)]
    let ctime = {
        use std::os::unix::fs::MetadataExt;
        time_to_emacs_tuple(sym_meta.ctime(), sym_meta.ctime_nsec())
    };
    #[cfg(not(unix))]
    let ctime = meta
        .created()
        .ok()
        .and_then(system_time_to_secs_nanos)
        .map(|(secs, nanos)| time_to_emacs_tuple(secs, nanos))
        .unwrap_or(Value::NIL);

    // Size.
    let size = Value::fixnum(meta.len() as i64);

    // Mode string (like "drwxr-xr-x").
    #[cfg(unix)]
    let mode = {
        use std::os::unix::fs::PermissionsExt;
        let mode_bits = sym_meta.permissions().mode();
        Value::string(format_mode_string(mode_bits, &sym_meta))
    };
    #[cfg(not(unix))]
    let mode = Value::string(if meta.is_dir() {
        "drwxr-xr-x"
    } else {
        "-rw-r--r--"
    });

    // GID-CHANGEP: Emacs commonly reports t on Unix filesystems.
    #[cfg(unix)]
    let gid_changep = Value::T;
    #[cfg(not(unix))]
    let gid_changep = Value::NIL;

    // Inode.
    #[cfg(unix)]
    let inode = {
        use std::os::unix::fs::MetadataExt;
        Value::fixnum(sym_meta.ino() as i64)
    };
    #[cfg(not(unix))]
    let inode = Value::fixnum(0);

    // Device.
    #[cfg(unix)]
    let device = {
        use crate::emacs_core::value::ValueKind;
        use std::os::unix::fs::MetadataExt;
        Value::fixnum(sym_meta.dev() as i64)
    };
    #[cfg(not(unix))]
    let device = Value::fixnum(0);

    Some(Value::list(vec![
        file_type,
        nlinks,
        uid_val,
        gid_val,
        atime,
        mtime,
        ctime,
        size,
        mode,
        gid_changep,
        inode,
        device,
    ]))
}

/// Format a Unix file mode string like "drwxr-xr-x" or "-rw-r--r--".
#[cfg(unix)]
fn format_mode_string(mode: u32, meta: &fs::Metadata) -> String {
    let mut s = String::with_capacity(10);

    // File type character.
    if meta.file_type().is_symlink() {
        s.push('l');
    } else if meta.is_dir() {
        s.push('d');
    } else {
        s.push('-');
    }

    // Owner permissions.
    s.push(if mode & 0o400 != 0 { 'r' } else { '-' });
    s.push(if mode & 0o200 != 0 { 'w' } else { '-' });
    s.push(if mode & 0o4000 != 0 {
        if mode & 0o100 != 0 { 's' } else { 'S' }
    } else if mode & 0o100 != 0 {
        'x'
    } else {
        '-'
    });

    // Group permissions.
    s.push(if mode & 0o040 != 0 { 'r' } else { '-' });
    s.push(if mode & 0o020 != 0 { 'w' } else { '-' });
    s.push(if mode & 0o2000 != 0 {
        if mode & 0o010 != 0 { 's' } else { 'S' }
    } else if mode & 0o010 != 0 {
        'x'
    } else {
        '-'
    });

    // Other permissions.
    s.push(if mode & 0o004 != 0 { 'r' } else { '-' });
    s.push(if mode & 0o002 != 0 { 'w' } else { '-' });
    s.push(if mode & 0o1000 != 0 {
        if mode & 0o001 != 0 { 't' } else { 'T' }
    } else if mode & 0o001 != 0 {
        'x'
    } else {
        '-'
    });

    s
}

// ---------------------------------------------------------------------------
// Pure builtins
// ---------------------------------------------------------------------------

/// Context-backed variant of `directory-files-and-attributes`.
/// Resolves relative DIRECTORY against dynamic/default `default-directory`.
pub(crate) fn builtin_directory_files_and_attributes(
    eval: &mut Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_range_args("directory-files-and-attributes", &args, 1, 6)?;
    let dir = super::fileio::resolve_filename_in_state(
        &eval.obarray,
        &[],
        &eval.buffers,
        &expect_string("directory-files-and-attributes", &args[0])?,
    );
    directory_files_and_attributes_with_dir(&args, dir)
}

fn directory_files_and_attributes_with_dir(args: &[Value], dir: String) -> EvalResult {
    let full_name = args.get(1).is_some_and(|v| v.is_truthy());
    let match_regexp = match args.get(2) {
        Some(v) if v.is_truthy() => Some(expect_string("directory-files-and-attributes", v)?),
        _ => None,
    };
    let nosort = args.get(3).is_some_and(|v| v.is_truthy());
    // GNU Emacs: return string names unless ID-FORMAT is nil or 'integer.
    let id_format_string = args
        .get(4)
        .is_some_and(|v| v.is_truthy() && v.as_symbol_name().map_or(true, |s| s != "integer"));
    let count = parse_wholenump_count(args.get(5))?;
    if count == Some(0) {
        return Ok(Value::NIL);
    }

    let names = read_directory_names(&dir)?;

    let dir_with_slash = ensure_trailing_slash(&dir);
    let mut items: VecDeque<(String, String)> = VecDeque::new();
    let mut remaining = count.unwrap_or(usize::MAX);
    for name in names {
        if let Some(pattern) = match_regexp.as_deref() {
            let mut throwaway = None;
            let matched = super::regex::string_match_full_with_case_fold(
                pattern,
                &name,
                0,
                false,
                &mut throwaway,
            )
            .map_err(|msg| {
                signal(
                    "invalid-regexp",
                    vec![Value::string(format!(
                        "Invalid regexp \"{}\": {}",
                        pattern, msg
                    ))],
                )
            })?;
            if matched.is_none() {
                continue;
            }
        }

        let full_path = format!("{}{}", dir_with_slash, name);
        let display_name = if full_name { full_path.clone() } else { name };
        items.push_front((display_name, full_path));

        if remaining != usize::MAX {
            remaining -= 1;
            if remaining == 0 {
                break;
            }
        }
    }

    let mut items: Vec<(String, String)> = items.into_iter().collect();
    // Sort unless NOSORT is non-nil.
    if !nosort {
        items.sort_by(|a, b| a.0.cmp(&b.0));
    }

    // Build result list of (NAME . ATTRIBUTES) cons cells.
    let result: Vec<Value> = items
        .into_iter()
        .map(|(display_name, full_path)| {
            let attrs = build_file_attributes(&full_path, id_format_string).unwrap_or(Value::NIL);
            Value::cons(Value::string(display_name), attrs)
        })
        .collect();

    Ok(Value::list(result))
}

/// Context-backed variant of `file-name-completion`.
/// This supports arbitrary callable predicates and matches Emacs behavior of
/// binding `default-directory` to DIRECTORY while predicate is invoked.
pub(crate) fn builtin_file_name_completion(eval: &mut Context, args: Vec<Value>) -> EvalResult {
    let plan = prepare_file_name_completion_in_state(&eval.obarray, &[], &eval.buffers, &args)?;
    let predicate = args.get(2);
    finish_file_name_completion_with_eval_predicate(
        eval,
        predicate,
        plan.directory,
        plan.file,
        plan.completions,
        plan.ignore_case,
    )
}

/// Context-backed variant of `file-name-all-completions`.
/// Resolves relative DIRECTORY against dynamic/default `default-directory`.
pub(crate) fn builtin_file_name_all_completions(
    eval: &mut Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_range_args("file-name-all-completions", &args, 2, 2)?;

    let file = expect_string("file-name-all-completions", &args[0])?;
    let directory = super::fileio::resolve_filename_in_state(
        &eval.obarray,
        &[],
        &eval.buffers,
        &expect_string("file-name-all-completions", &args[1])?,
    );
    if file.contains('/') {
        return Ok(Value::NIL);
    }
    let ignore_case = get_completion_ignore_case(&eval.obarray);
    // GNU Emacs: file-name-all-completions does NOT filter by
    // completion-ignored-extensions (the "all_flag" path).
    let completions = collect_file_name_completions(&file, &directory, ignore_case)?;
    Ok(Value::list(
        completions.into_iter().map(Value::string).collect(),
    ))
}

fn collect_file_name_completions(
    file: &str,
    directory: &str,
    ignore_case: bool,
) -> Result<Vec<String>, Flow> {
    let names = read_directory_names(directory)?;
    let mut completions = VecDeque::new();

    for name in names {
        let matches = if ignore_case {
            name.to_lowercase().starts_with(&file.to_lowercase())
        } else {
            name.starts_with(file)
        };
        if !matches {
            continue;
        }

        let full_path = std::path::Path::new(directory).join(&name);
        if full_path.is_dir() {
            completions.push_front(format!("{}/", name));
        } else {
            completions.push_front(name);
        }
    }

    Ok(completions.into_iter().collect())
}

/// Extract the list of ignored extensions from the `completion-ignored-extensions` variable.
fn get_ignored_extensions(obarray: &super::symbol::Obarray) -> Vec<String> {
    let Some(val) = obarray.symbol_value("completion-ignored-extensions") else {
        return Vec::new();
    };
    let val = *val;
    let Some(items) = list_to_vec(&val) else {
        return Vec::new();
    };
    items.into_iter().filter_map(|v| v.as_str_owned()).collect()
}

/// Check whether `completion-ignore-case` is truthy.
fn get_completion_ignore_case(obarray: &super::symbol::Obarray) -> bool {
    obarray
        .symbol_value("completion-ignore-case")
        .is_some_and(|v| v.is_truthy())
}

/// Apply `completion-ignored-extensions` filtering to a set of completions.
///
/// This follows GNU Emacs semantics:
/// - If a file name (not exact match with FILE) ends with an ignored extension,
///   it can be excluded.
/// - If a directory name ends with an ignored extension that itself ends in '/',
///   it can be excluded.
/// - "." and ".." directories are always excludable.
/// - If there is at least one non-excludable match, all excludable matches are
///   dropped. If ALL matches are excludable, they are all kept (the "includeall"
///   fallback).
fn filter_by_ignored_extensions(
    file: &str,
    completions: Vec<String>,
    ignored_extensions: &[String],
    ignore_case: bool,
) -> Vec<String> {
    if completions.is_empty() {
        return completions;
    }

    let file_len = file.len();

    // Classify each completion as excludable or not.
    let mut classified: Vec<(String, bool)> = Vec::with_capacity(completions.len());
    for comp in completions {
        let is_dir = comp.ends_with('/');
        // The base name (without trailing '/' for directories)
        let base = if is_dir {
            &comp[..comp.len() - 1]
        } else {
            comp.as_str()
        };

        let mut can_exclude = false;

        // "." and ".." are always excludable
        if base == "." || base == ".." {
            can_exclude = true;
        } else if base.len() > file_len {
            // Only check ignored-extensions when the name is longer than FILE
            // (i.e., not an exact match).
            for ext in ignored_extensions {
                if is_dir {
                    // For directories, only match extensions that end in '/'.
                    if !ext.ends_with('/') {
                        continue;
                    }
                    let ext_base = &ext[..ext.len() - 1]; // strip trailing '/'
                    if ext_base.is_empty() {
                        continue;
                    }
                    let matches = if ignore_case {
                        base.to_lowercase().ends_with(&ext_base.to_lowercase())
                    } else {
                        base.ends_with(ext_base)
                    };
                    if matches {
                        can_exclude = true;
                        break;
                    }
                } else {
                    // For files, match extensions (which should not end in '/').
                    if ext.ends_with('/') {
                        continue;
                    }
                    let matches = if ignore_case {
                        base.to_lowercase().ends_with(&ext.to_lowercase())
                    } else {
                        base.ends_with(ext.as_str())
                    };
                    if matches {
                        can_exclude = true;
                        break;
                    }
                }
            }
        }

        classified.push((comp, can_exclude));
    }

    // GNU Emacs "includeall" logic:
    // If there's at least one non-excludable match, drop all excludable ones.
    // Otherwise (all are excludable), keep them all.
    let has_non_excludable = classified.iter().any(|(_, excl)| !excl);

    if has_non_excludable {
        classified
            .into_iter()
            .filter(|(_, excl)| !excl)
            .map(|(comp, _)| comp)
            .collect()
    } else {
        classified.into_iter().map(|(comp, _)| comp).collect()
    }
}

pub(crate) struct FileNameCompletionPlan {
    pub(crate) file: String,
    pub(crate) directory: String,
    pub(crate) completions: Vec<String>,
    pub(crate) ignore_case: bool,
}

pub(crate) fn prepare_file_name_completion_in_state(
    obarray: &super::symbol::Obarray,
    dynamic: &[OrderedRuntimeBindingMap],
    buffers: &crate::buffer::BufferManager,
    args: &[Value],
) -> Result<FileNameCompletionPlan, Flow> {
    expect_range_args("file-name-completion", args, 2, 3)?;

    let file = expect_string("file-name-completion", &args[0])?;
    let directory = super::fileio::resolve_filename_in_state(
        obarray,
        dynamic,
        buffers,
        &expect_string("file-name-completion", &args[1])?,
    );
    let ignore_case = get_completion_ignore_case(obarray);
    let ignored_extensions = get_ignored_extensions(obarray);
    let completions = if file.contains('/') {
        Vec::new()
    } else {
        let raw = collect_file_name_completions(&file, &directory, ignore_case)?;
        // Apply completion-ignored-extensions filtering for file-name-completion
        // (but not for file-name-all-completions, per GNU Emacs).
        filter_by_ignored_extensions(&file, raw, &ignored_extensions, ignore_case)
    };

    Ok(FileNameCompletionPlan {
        file,
        directory,
        completions,
        ignore_case,
    })
}

pub(crate) fn finish_file_name_completion_with_eval_predicate(
    eval: &mut Context,
    predicate: Option<&Value>,
    directory: String,
    file: String,
    completions: Vec<String>,
    ignore_case: bool,
) -> EvalResult {
    let Some(predicate) = predicate.copied() else {
        return Ok(resolve_file_name_completion(
            &file,
            completions,
            ignore_case,
        ));
    };
    if predicate.is_nil() {
        return Ok(resolve_file_name_completion(
            &file,
            completions,
            ignore_case,
        ));
    }

    let use_absolute_path = predicate_uses_absolute_file_argument(&eval.obarray, &predicate);
    let bound_directory = directory.clone();
    finish_file_name_completion_with_callable_predicate(
        use_absolute_path,
        directory,
        file,
        completions,
        ignore_case,
        |predicate_arg| {
            with_default_directory_binding(eval, bound_directory.as_str(), |eval| {
                eval.apply(predicate, vec![predicate_arg])
            })
        },
    )
}

pub(crate) fn predicate_uses_absolute_file_argument(
    obarray: &super::symbol::Obarray,
    predicate: &Value,
) -> bool {
    let Some(symbol) = predicate_callable_name(predicate) else {
        return false;
    };
    obarray.symbol_function(symbol).is_none() && is_builtin_path_predicate(symbol)
}

pub(crate) fn finish_file_name_completion_with_callable_predicate(
    use_absolute_path: bool,
    directory: String,
    file: String,
    completions: Vec<String>,
    ignore_case: bool,
    mut predicate_call: impl FnMut(Value) -> Result<Value, Flow>,
) -> EvalResult {
    let completions = filter_completions_by_callable_predicate(
        use_absolute_path,
        directory.as_str(),
        completions,
        |predicate_arg| predicate_call(predicate_arg),
    )?;
    Ok(resolve_file_name_completion(
        &file,
        completions,
        ignore_case,
    ))
}

fn resolve_file_name_completion(file: &str, completions: Vec<String>, ignore_case: bool) -> Value {
    if completions.is_empty() {
        return Value::NIL;
    }

    let filtered = filter_completion_candidates(file, completions);
    if filtered.is_empty() {
        return Value::string(file);
    }

    // If there is exactly one completion and it matches FILE exactly, return t.
    // For directory candidates ending in '/', Emacs returns the completion
    // string when FILE lacks the trailing slash (e.g. ".." -> "../").
    if filtered.len() == 1 {
        let comp = &filtered[0];
        let eq = if ignore_case {
            comp.eq_ignore_ascii_case(file)
        } else {
            comp == file
        };
        if eq {
            return Value::T;
        }
        return Value::string(comp.clone());
    }

    // Find the longest common prefix among completions.
    // When completion-ignore-case is set, use case-insensitive comparison
    // but preserve the case of the first match (which GNU Emacs refines to
    // prefer the match whose case matches the input).
    let mut prefix = filtered[0].clone();
    for comp in &filtered[1..] {
        let common_len = if ignore_case {
            prefix
                .chars()
                .zip(comp.chars())
                .take_while(|(a, b)| a.to_lowercase().eq(b.to_lowercase()))
                .count()
        } else {
            prefix
                .chars()
                .zip(comp.chars())
                .take_while(|(a, b)| a == b)
                .count()
        };
        prefix.truncate(
            prefix
                .char_indices()
                .nth(common_len)
                .map(|(i, _)| i)
                .unwrap_or(prefix.len()),
        );
    }

    // If the prefix equals the input exactly and there are multiple matches,
    // return the prefix (Emacs returns what was typed if ambiguous but valid prefix).
    Value::string(prefix)
}

fn filter_completion_candidates(file: &str, completions: Vec<String>) -> Vec<String> {
    completions
        .into_iter()
        .filter(|c| c != "./")
        .filter(|c| file.starts_with("..") || c != "../")
        .collect()
}

fn filter_completions_by_symbol_predicate(
    eval: &mut Context,
    predicate: Option<&Value>,
    directory: &str,
    completions: Vec<String>,
) -> Result<Vec<String>, Flow> {
    let Some(predicate) = predicate else {
        return Ok(completions);
    };
    if predicate.is_nil() {
        return Ok(completions);
    }
    let Some(symbol) = predicate_callable_name(predicate) else {
        // Cannot evaluate lambda/object predicates via dispatch_subr.
        return Ok(completions);
    };

    let mut filtered = Vec::new();
    for candidate in completions {
        if symbol_predicate_matches_candidate(eval, symbol, directory, &candidate)? {
            filtered.push(candidate);
        }
    }
    Ok(filtered)
}

fn symbol_predicate_matches_candidate(
    eval: &mut Context,
    symbol: &str,
    directory: &str,
    candidate: &str,
) -> Result<bool, Flow> {
    if let Some(result) = eval.dispatch_subr(symbol, vec![Value::string(candidate)]) {
        let result = result?;
        if result.is_truthy() || !is_builtin_path_predicate(symbol) {
            return Ok(result.is_truthy());
        }

        let absolute = std::path::Path::new(directory).join(candidate);
        let absolute = absolute.to_string_lossy().into_owned();
        if let Some(result) = eval.dispatch_subr(symbol, vec![Value::string(absolute)]) {
            return Ok(result?.is_truthy());
        }
        return Ok(false);
    }

    // Fallback: try absolute path to make path predicates useful.
    let absolute = std::path::Path::new(directory).join(candidate);
    let absolute = absolute.to_string_lossy().into_owned();
    if let Some(result) = eval.dispatch_subr(symbol, vec![Value::string(absolute)]) {
        return Ok(result?.is_truthy());
    }

    // Preserve current behavior for unknown/non-callable predicates.
    Ok(true)
}

fn filter_completions_by_callable_predicate(
    use_absolute_path: bool,
    directory: &str,
    completions: Vec<String>,
    mut predicate_call: impl FnMut(Value) -> Result<Value, Flow>,
) -> Result<Vec<String>, Flow> {
    let mut filtered = Vec::new();
    for candidate in completions {
        let predicate_arg =
            predicate_argument_for_callable_predicate(use_absolute_path, directory, &candidate);
        let keep = predicate_call(predicate_arg)?.is_truthy();
        if keep {
            filtered.push(candidate);
        }
    }
    Ok(filtered)
}

fn with_default_directory_binding<T>(
    eval: &mut Context,
    directory: &str,
    f: impl FnOnce(&mut Context) -> Result<T, Flow>,
) -> Result<T, Flow> {
    let count = eval.specpdl.len();
    eval.specbind(intern("default-directory"), Value::string(directory));
    let result = f(eval);
    eval.unbind_to(count);
    result
}

fn predicate_argument_for_callable_predicate(
    use_absolute_path: bool,
    directory: &str,
    candidate: &str,
) -> Value {
    if use_absolute_path {
        let absolute = std::path::Path::new(directory).join(candidate);
        return Value::string(absolute.to_string_lossy().into_owned());
    }

    Value::string(candidate)
}

fn is_builtin_path_predicate(name: &str) -> bool {
    matches!(
        name,
        "file-directory-p"
            | "file-exists-p"
            | "file-readable-p"
            | "file-writable-p"
            | "file-regular-p"
            | "file-symlink-p"
            | "file-executable-p"
    )
}

fn predicate_callable_name(predicate: &Value) -> Option<&str> {
    match predicate.kind() {
        ValueKind::Symbol(id) => Some(resolve_sym(id)),
        ValueKind::Veclike(VecLikeType::Subr) => {
            let id = predicate.as_subr_id().unwrap();
            Some(resolve_sym(id))
        }
        _ => None,
    }
}

/// Context-backed variant of `file-attributes`.
/// Resolves relative FILENAME against dynamic/default `default-directory`.
pub(crate) fn builtin_file_attributes(eval: &mut Context, args: Vec<Value>) -> EvalResult {
    expect_range_args("file-attributes", &args, 1, 2)?;

    // GNU Emacs (dired.c:1003-1006): If the filename is not a string
    // (e.g., nil from buffer-file-name on a non-file buffer), return nil
    // instead of signaling an error.
    let filename_str = match args[0].as_str() {
        Some(s) => s.to_string(),
        None if args[0].is_nil() => return Ok(Value::NIL),
        None => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("stringp"), args[0]],
            ));
        }
    };
    let filename =
        super::fileio::resolve_filename_in_state(&eval.obarray, &[], &eval.buffers, &filename_str);
    // GNU Emacs: return string names unless ID-FORMAT is nil or 'integer.
    let id_format_string = args
        .get(1)
        .is_some_and(|v| v.is_truthy() && v.as_symbol_name().map_or(true, |s| s != "integer"));

    match build_file_attributes(&filename, id_format_string) {
        Some(attrs) => Ok(attrs),
        None => Ok(Value::NIL),
    }
}

/// (file-attributes-lessp F1 F2)
///
/// Return t if the first element (filename) of F1 is less than that of F2.
/// F1 and F2 are each (NAME . ATTRIBUTES) cons cells as returned by
/// `directory-files-and-attributes`.
pub(crate) fn builtin_file_attributes_lessp(args: Vec<Value>) -> EvalResult {
    expect_range_args("file-attributes-lessp", &args, 2, 2)?;

    let name1 = extract_car_string("file-attributes-lessp", &args[0])?;
    let name2 = extract_car_string("file-attributes-lessp", &args[1])?;

    Ok(Value::bool_val(name1 < name2))
}

/// Extract the car of a cons cell as a string.
fn extract_car_string(_name: &str, val: &Value) -> Result<String, Flow> {
    match val.kind() {
        ValueKind::Cons => {
            let pair_car = val.cons_car();
            let pair_cdr = val.cons_cdr();
            match pair_car.kind() {
                ValueKind::String => Ok(pair_car.as_str().unwrap().to_owned()),
                other => Err(signal(
                    "wrong-type-argument",
                    vec![Value::symbol("stringp"), *val],
                )),
            }
        }
        other => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("consp"), *val],
        )),
    }
}

/// (system-users)
///
/// Return a list of user names on the system.
/// Reads `/etc/passwd` and returns account names in oracle-compatible order.
pub(crate) fn builtin_system_users(args: Vec<Value>) -> EvalResult {
    expect_range_args("system-users", &args, 0, 0)?;

    let mut users = read_colon_file_names(&system_users_passwd_path());
    if users.is_empty() {
        let fallback_user = std::env::var("USER")
            .or_else(|_| std::env::var("LOGNAME"))
            .unwrap_or_else(|_| "unknown".to_string());
        users.push(fallback_user);
    }

    Ok(Value::list(
        users.into_iter().map(Value::string).collect::<Vec<_>>(),
    ))
}

/// (system-groups)
///
/// Return a list of group names on the system.
/// Reads `/etc/group` and returns group names in oracle-compatible order.
pub(crate) fn builtin_system_groups(args: Vec<Value>) -> EvalResult {
    expect_range_args("system-groups", &args, 0, 0)?;
    let groups = read_colon_file_names(&system_groups_path());
    if groups.is_empty() {
        return Ok(Value::NIL);
    }
    Ok(Value::list(
        groups.into_iter().map(Value::string).collect::<Vec<_>>(),
    ))
}

fn parse_colon_file_names(contents: &str) -> Vec<String> {
    let mut names = Vec::new();
    for line in contents.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if let Some((name, _rest)) = trimmed.split_once(':') {
            let name = name.trim();
            if !name.is_empty() {
                names.push(name.to_string());
            }
        }
    }
    // Emacs' output order matches reverse file order.
    names.reverse();
    names
}

fn system_users_passwd_path() -> String {
    "/etc/passwd".to_string()
}

fn system_groups_path() -> String {
    "/etc/group".to_string()
}

fn read_colon_file_names(path: &str) -> Vec<String> {
    match fs::read_to_string(path) {
        Ok(contents) => parse_colon_file_names(&contents),
        Err(_) => Vec::new(),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[path = "dired_test.rs"]
mod tests;