-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder.scm
42 lines (32 loc) · 1.25 KB
/
finder.scm
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
;;; -*- Mode: Scheme; Character-encoding: utf-8; -*-
;;; Copyright (C) 2005-2020 beingmeta, inc. All rights reserved
;;; Copyright (C) 2020-2022 Kenneth Haase ([email protected]).
(in-module 'finder)
(use-module '{webtools texttools varconfig regex logger})
(module-export! '{finder})
(define (find-matches dir spec (opts #f))
(filter-choices (path (getfiles dir))
(let ((base (basename path)))
(cond ((regex? spec)
(regex/search spec (basename path)))
((string? spec)
(cond ((has-prefix spec "*") (has-suffix base (slice spec 1)))
((has-suffix spec "*") (has-prefix base (slice spec 0 -1)))
(else (search spec base))))
((or (pair? spec) (vector? spec))
(textsearch spec base)
(or (pair? spec) (vector? spec)))
(else (filename-match file base spec opts))))))
(define (filename-match file base spec opts)
#f)
(define (finder dir match (opts #f) (root #f))
(cond ((not root))
((eq? root #t)
(set! root (getopt opts 'root (abspath dir))))
((string? root))
(else (irritant root '|InvalidRootArg|)))
(when (and (string? root) (not (has-suffix root "/")))
(set! root (glom root "/")))
(if root
(strip-prefix (find-matches dir match opts) root)
(find-matches dir match opts)))