edward

An extensible POSIX-compatible implementation of the ed(1) text editor

git clone https://git.8pit.net/edward.git

 1(define prompt "")
 2(define silent? #f)
 3
 4(define (err msg)
 5  (let ((port (current-error-port)))
 6    (display "edward: " port)
 7    (display msg port)
 8    (newline port)))
 9
10(define prompt-opt
11  (option
12    '(#\p "prompt") #t #f
13    (lambda (o n x vals)
14      (set! prompt x)
15      vals)))
16
17(define silent-opt
18  (option
19    '(#\s "silent") #f #f
20    (lambda (o n x vals)
21      (set! silent? #t)
22      vals)))
23
24(define (parse-args args flags)
25  (reverse
26    (args-fold
27      args
28      flags
29      (lambda (o n x vals)
30        (error "unrecognized option" n))
31      cons
32      '())))
33
34(define (run-editor filename)
35  (let ((editor (make-text-editor exec-edit filename prompt silent?)))
36    (editor-start editor (parse-cmd))))
37
38;;> Entry point of the edward text editor. This function parses the
39;;> provided command line arguments `args` and starts the
40;;> read-eval-print loop. This command should therefore only be called
41;;> after registering all editor commands.
42
43(define (edward-main . args)
44  (let* ((flags (list prompt-opt silent-opt))
45         (argv  (if (null? args) (command-line) args))
46         (files (cdr (parse-args argv flags))))
47    (if prompt
48      (case (length files)
49        ((1)  (run-editor (car files)))
50        ((0)  (run-editor ""))
51        (else (err "specify one file or no files")))
52      (err "missing prompt option argument"))))