Files
rash-coreutils/private/editor.rkt
T

67 lines
2.0 KiB
Racket

#lang racket/base
(require racket/format
racket/path
racket/string
rackedit)
(provide coreutils-edit
current-coreutils-editor)
(define current-coreutils-editor
(make-parameter
rkdt
(λ (editor)
(unless (procedure? editor)
(raise-argument-error 'current-coreutils-editor "procedure?" editor))
editor)))
(define (arg->path arg)
(cond
[(path? arg) arg]
[(string? arg) (string->path arg)]
[(symbol? arg) (string->path (symbol->string arg))]
[else
(raise-argument-error
'edit
"path, string, or symbol"
arg)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Open one file in the editor configured for rash-coreutils.
; pre : args contains exactly one filename and optionally --wait.
; post : The configured editor has been invoked once. With --wait, the
; command does not return until the editor procedure returns.
; result : (void).
; internals:
; current-coreutils-editor defaults to rackedit's rkdt procedure.
; The --wait flag is translated to rkdt's #:wait? keyword.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (coreutils-edit . args)
(define wait? #f)
(define reversed-paths '())
(for ([arg (in-list args)])
(define text (~a arg))
(cond
[(string=? text "--wait")
(set! wait? #t)]
[(string-prefix? text "-")
(raise-arguments-error 'edit
"unsupported option"
"option" text)]
[else
(set! reversed-paths (cons (arg->path arg) reversed-paths))]))
(define paths (reverse reversed-paths))
(unless (or (= (length paths) 1) (= (length paths) 0))
(raise-arguments-error 'edit
"expected exactly zero or one file"
"arguments" args))
(if (null? paths)
((current-coreutils-editor) #:wait? wait?)
((current-coreutils-editor) (car paths) #:wait? wait?))
(void))