Skip to content

Commit ee6d116

Browse files
committed
Use WHEN and UNLESS instead of one-armed IF.
1 parent 0059f44 commit ee6d116

File tree

4 files changed

+77
-46
lines changed

4 files changed

+77
-46
lines changed

s48-packages.scm

+17
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
read-byte
3434
tty-info tty-info:output-speed
3535
uname uname:os-name
36+
(when :syntax)
37+
(unless :syntax)
3638
(with-current-input-port :syntax)
3739
(with-current-output-port :syntax))
3840
(open (modify ascii (rename (ascii->char integer->char)
@@ -47,6 +49,21 @@
4749
(subset util (unspecific)))
4850
(for-syntax (open scheme i/o-internal))
4951
(begin
52+
53+
(define-syntax unless
54+
(syntax-rules ()
55+
((unless predicate action0 . actions)
56+
(if predicate
57+
#f
58+
(begin action0 . actions)))))
59+
60+
(define-syntax when
61+
(syntax-rules ()
62+
((when predicate action0 . actions)
63+
(if predicate
64+
(begin action0 . actions)
65+
#f))))
66+
5067
(define ignore (unspecific))
5168
(define (tty-info port) ignore)
5269
(define (uname) ignore)

scsh-packages.scm

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
(table-set! hash-table-set!)))
1616
threads)
1717
(begin
18+
(define-syntax unless
19+
(syntax-rules ()
20+
((unless predicate action0 . actions)
21+
(if predicate
22+
#f
23+
(begin action0 . actions)))))
24+
25+
(define-syntax when
26+
(syntax-rules ()
27+
((when predicate action0 . actions)
28+
(if predicate
29+
(begin action0 . actions)
30+
#f))))
31+
1832
(define (read-byte . args)
1933
(let-optionals args ((s (current-input-port)))
2034
(let ((value (read-char s)))

terminfo-capabilities.scm

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
;;; see tigetflag, tigetnum, tigetstr
77
(define (terminal:capability terminal name)
88
(let ((capability (hash-table-ref *capabilities* name)))
9-
(if (or (null? terminal)
10-
(not (terminal? terminal)))
11-
(error "Invalid terminfo object"))
12-
(if (null? capability)
13-
(error "Capability does not exist:" name))
9+
(when (or (null? terminal)
10+
(not (terminal? terminal)))
11+
(error "Invalid terminfo object"))
12+
(when (null? capability)
13+
(error "Capability does not exist:" name))
1414
(let* ((accessor (car capability))
1515
(index (cdr capability))
1616
(table (accessor terminal)))

terminfo.scm

+41-41
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@
4949

5050
(define (open-terminfo-file name)
5151
(let loop ((dirs *terminfo-directories*))
52-
(if (not (null? dirs))
53-
(let* ((basedir (car dirs))
54-
(initial (terminfo-directory-prefix name))
55-
(file (path-list->file-name (list basedir initial name))))
56-
(cond ((file-not-exists? file)
57-
(error "Cannot find terminfo named " name))
58-
((file-readable? file) (open-input-file file))
59-
(else (loop (cdr dirs))))))))
52+
(unless (null? dirs)
53+
(let* ((basedir (car dirs))
54+
(initial (terminfo-directory-prefix name))
55+
(file (path-list->file-name (list basedir initial name))))
56+
(cond ((file-not-exists? file)
57+
(error "Cannot find terminfo named " name))
58+
((file-readable? file) (open-input-file file))
59+
(else (loop (cdr dirs))))))))
6060

6161
(define (read-short . args)
6262
(let-optionals args ((s (current-input-port)))
@@ -89,9 +89,9 @@
8989
(else (loop (+ 1 j)))))))
9090

9191
(define (read-padding s lines)
92-
(if (not (and (char=? #\$ (string-ref s 0))
93-
(char=? #\< (string-ref s 1))))
94-
(error "Invalid input"))
92+
(unless (and (char=? #\$ (string-ref s 0))
93+
(char=? #\< (string-ref s 1)))
94+
(error "Invalid input"))
9595
(let loop ((time 0)
9696
(force #f)
9797
(i 2))
@@ -119,25 +119,25 @@
119119
(with-current-output-port output-port
120120
(let loop ((i 0)
121121
(len (string-length s)))
122-
(if (< i len)
123-
(let ((c (string-ref s i)))
124-
(cond
125-
((and (char=? c #\$) (number? (string-index s #\>)))
126-
(let ((substr (substring s i (+ 1 (string-index s #\>))))
127-
(rate (baud-rate output-port)))
128-
(let-values (((time force)
129-
(read-padding substr lines-affected)))
130-
(if (or force (eq? #t (xon-xoff)))
131-
(if (eq? #t (no-pad-char))
132-
(sleep (/ time 10000.0))
133-
(do ((i 0 (+ i 1)))
134-
((>= i (ceiling (/ (* rate time) 100000))))
135-
(write-char (char-padding))
136-
(loop (+ 1 i) len)))
137-
(loop (+ 1 i) len)))))
138-
(else (begin
139-
(write-char c)
140-
(loop (+ 1 i) len))))))))))
122+
(when (< i len)
123+
(let ((c (string-ref s i)))
124+
(cond
125+
((and (char=? c #\$) (number? (string-index s #\>)))
126+
(let ((substr (substring s i (+ 1 (string-index s #\>))))
127+
(rate (baud-rate output-port)))
128+
(let-values (((time force)
129+
(read-padding substr lines-affected)))
130+
(if (or force (eq? #t (xon-xoff)))
131+
(if (eq? #t (no-pad-char))
132+
(sleep (/ time 10000.0))
133+
(do ((i 0 (+ i 1)))
134+
((>= i (ceiling (/ (* rate time) 100000))))
135+
(write-char (char-padding))
136+
(loop (+ 1 i) len)))
137+
(loop (+ 1 i) len)))))
138+
(else (begin
139+
(write-char c)
140+
(loop (+ 1 i) len))))))))))
141141

142142
;;;
143143
;;; See Table 7.3, _Unix_Curses_Explained_, p.101
@@ -377,26 +377,26 @@
377377
(numbers (make-vector sznumbers -1))
378378
(strings (make-vector szstrings -1))
379379
(stringtable (make-string szstringtable)))
380-
(if (not (= magic #o432))
381-
(error "This is not well-formed"))
380+
(unless (= magic #o432)
381+
(error "This is not well-formed"))
382382
(do ((i 0 (+ i 1))) ((>= i szbooleans))
383383
(vector-set! booleans i (not (zero? (read-byte)))))
384-
(if (odd? (+ sznames szbooleans))
385-
(read-byte))
384+
(when (odd? (+ sznames szbooleans))
385+
(read-byte))
386386
(do ((i 0 (+ i 1))) ((>= i sznumbers))
387387
(vector-set! numbers i (read-short)))
388388
(do ((i 0 (+ i 1))) ((>= i szstrings))
389389
(vector-set! strings i (read-short)))
390390
(do ((i 0 (+ i 1))) ((>= i szstringtable))
391391
(string-set! stringtable i (read-char)))
392392
(do ((i 0 (+ i 1))) ((>= i szstrings))
393-
(if (positive? (vector-ref strings i))
394-
(let* ((start (vector-ref strings i))
395-
(end (string-index stringtable
396-
(integer->char 0)
397-
start szstringtable))
398-
(substr (substring stringtable start end)))
399-
(vector-set! strings i substr))))
393+
(when (positive? (vector-ref strings i))
394+
(let* ((start (vector-ref strings i))
395+
(end (string-index stringtable
396+
(integer->char 0)
397+
start szstringtable))
398+
(substr (substring stringtable start end)))
399+
(vector-set! strings i substr))))
400400
(make-terminal console-output-port names booleans numbers strings))))
401401

402402
(define (setup-terminal . args)

0 commit comments

Comments
 (0)