Skip to content

Commit eb8ccae

Browse files
committed
make capture groups of patterns usable in programs (fixes #1)
1 parent e903189 commit eb8ccae

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

jaro

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
(if (pair? result) (cadar result) #f))
140140
#f))
141141

142-
(define (fix-command cmd file)
142+
(define (fix-command cmd file pattern)
143143
(define (helper cmd file escape?)
144144
(define (escape f)
145145
(if escape?
@@ -155,6 +155,17 @@
155155
(set! str (substring-replace str "%f" (escape file)))
156156
(set! str (substring-replace str "%F" (escape (path->abs file))))
157157
(set! str (substring-replace str "%u" (escape (path->uri file))))
158+
;; Replace capture groups with %0 %1 %2...
159+
(set! pattern (pattern-exec pattern file))
160+
(when pattern
161+
(for-each
162+
(λ (i) (let ((captured (match:substring pattern i)))
163+
(when captured
164+
(set! str (substring-replace
165+
str
166+
(string-append "%" (number->string i))
167+
captured)))))
168+
(iota (match:count pattern))))
158169
str))))
159170
(helper cmd file (string? cmd)))
160171

@@ -236,31 +247,32 @@
236247
(newline))
237248
(opener file #t)))))
238249

239-
(define* (execute-after exit-code file #:optional on-success on-error)
250+
(define* (execute-after exit-code file pattern #:optional on-success on-error)
240251
(cond
241252
((or (and (number? exit-code)
242253
(= exit-code 0))
243254
(and (not (number? exit-code))
244255
(not (equal? exit-code #f)))) (if on-success
245-
(run-fn-or-system on-success file)
256+
(run-fn-or-system on-success file pattern)
246257
#t))
247-
(on-error (run-fn-or-system on-error file))
258+
(on-error (run-fn-or-system on-error file pattern))
248259
(else #f)))
249260

250-
(define* (run-fn-or-system x file #:optional on-success on-error)
261+
(define* (run-fn-or-system x file pattern #:optional on-success on-error)
262+
(define fixed-command (fix-command x file pattern))
251263
(cond
252264
((procedure? x) (if cold-run
253265
(begin (display "(call) ") (display x) (newline))
254-
(execute-after (x file current-mime) file on-success on-error)))
266+
(execute-after (x file current-mime) file pattern on-success on-error)))
255267
((list? x) (if cold-run
256268
(begin (display "(run) ") (display (string-join x " ")) (newline))
257-
(execute-after (apply system* (fix-command x file)) file on-success on-error)))
269+
(execute-after (apply system* fixed-command) file pattern on-success on-error)))
258270
((string? x) (if cold-run
259271
(begin (display "(run) ") (display x) (newline))
260-
(execute-after (system (fix-command x file)) file on-success on-error)))))
272+
(execute-after (system fixed-command) file pattern on-success on-error)))))
261273

262274
;; TODO: inline this?
263-
(define (execute program term tmux screen standalone on-success on-error rest file)
275+
(define (execute program pattern term tmux screen standalone on-success on-error rest file)
264276
(let ((command program))
265277
(when (and term (not (isatty? (current-output-port))))
266278
(set! command (append-command term program standalone)))
@@ -272,7 +284,7 @@
272284
(when (and runner-program (not (eqv? runner-program #t)))
273285
(set! command runner-program))
274286
(if (not (xor runner-program runner-method))
275-
(run-fn-or-system command file on-success on-error)
287+
(run-fn-or-system command file pattern on-success on-error)
276288
(begin
277289
(display (string-append "No #:"
278290
(symbol->string (keyword->symbol runner-method))
@@ -283,13 +295,18 @@
283295
(define (pattern-exec pattern file)
284296
(define (match-with fn)
285297
(or (if current-mime (fn pattern current-mime) #f)
286-
(string-match pattern file)))
287-
298+
(fn pattern file)))
288299
(cond
289300
((string? pattern) (match-with string-match))
290301
((regexp? pattern) (match-with regexp-exec))
291302
((list? pattern) (any identity (map (λ (ptrn) (pattern-exec ptrn file)) pattern)))))
292303

304+
(define (compile-pattern pattern)
305+
(cond
306+
((string? pattern) (make-regexp pattern))
307+
((regexp? pattern) pattern)
308+
((list? pattern) (map (λ (ptrn) (compile-pattern ptrn)) pattern))))
309+
293310
(define* (assoc #:key
294311
name
295312
program
@@ -307,13 +324,14 @@
307324
#:rest r)
308325
(define x
309326
(lambda* (file #:optional pass-test)
327+
(set! pattern (compile-pattern pattern))
310328
(if (or pass-test (pattern-exec pattern file))
311329
(let ((rest (make-paired r))
312-
(test-result (not (xor test (if test (run-fn-or-system test file) #f)))))
330+
(test-result (not (xor test (if test (run-fn-or-system test file pattern) #f)))))
313331
(when (and (not test-result) on-fail)
314332
(set! program on-fail))
315333
(if (or test-result on-fail)
316-
(let ((exit-status (execute program term tmux screen standalone on-success on-error rest file)))
334+
(let ((exit-status (execute program pattern term tmux screen standalone on-success on-error rest file)))
317335
(if (and (not (equal? exit-status 0)) continue-on-error)
318336
#f
319337
(begin

0 commit comments

Comments
 (0)