-
Notifications
You must be signed in to change notification settings - Fork 5
/
help.scm
117 lines (110 loc) · 4.2 KB
/
help.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
; Scheme 9 from Empty Space, Function Library
; By Nils M Holm, 2009-2012
; Placed in the Public Domain
;
; (help) ==> unspecific
; (help symbol | string) ==> unspecific
; (apropos) ==> list
; (apropos symbol | string) ==> list
;
; (load-from-library "help.scm")
;
; Display the synopsis of the given procedure or keyword. When
; SYMBOL is described in R4RS, produce its R4RS entry, otherwise
; display a S9FES-specific summary. When no argument is passed
; to HELP, it explains itself.
;
; APROPOS returns a list of all procedure names for which help
; pages exist. When an argument is passed to APROPOS, its output
; is limited to topics whose name contains the argument.
;
; The *LINES-PER-PAGE* variable controls the number of lines
; to be printed by HELP before prompting.
;
; (Example): (help 'symbol?) ==> unspecific
;
; Output: R4RS 6.4 (symbol? object) ==> boolean
;
; Returns #T if OBJECT is a symbol, otherwise returns #F.
;
; (symbol? 'foo) ==> #t
; (symbol? (car '(a b))) ==> #t
; (symbol? "bar") ==> #f
; (symbol? 'nil) ==> #t
; (symbol? '()) ==> #f
; (symbol? #f) ==> #f
(load-from-library "name-to-file-name.scm")
(load-from-library "read-line.scm")
(load-from-library "string-find.scm")
(load-from-library "remove.scm")
(load-from-library "mergesort.scm")
(define *lines-per-page* 20)
(define help
(let ((name->file-name name->file-name))
(lambda sym
(define (more? tty)
(display "; ----- more (q = quit) -----")
(let ((s (read-line tty)))
(if (eof-object? s)
(begin (newline)
#t)
(not (string-find "q" s)))))
(define (show-file file)
(read-line)
(newline)
(let ((tty (current-input-port)))
(with-input-from-file file
(lambda ()
(let print ((line (read-line))
(lno 1))
(cond ((eof-object? line)
(newline))
((and (not (zero? *lines-per-page*))
(= lno *lines-per-page*))
(if (more? tty)
(print line 0)))
(else
(display line)
(newline)
(print (read-line) (+ 1 lno)))))))))
(let* ((name (cond ((null? sym)
"help")
((symbol? (car sym))
(symbol->string (car sym)))
((string? (car sym))
(car sym))
(else
(error "help: expected string or symbol, got"
(car sym)))))
(name (name->file-name name)))
(cond ((locate-file (string-append "help/" name))
=> show-file)
(else
(error "help: could not find help page" name)))))))
(define apropos
(let ((name->file-name name->file-name))
(lambda sym
(let* ((name (cond ((null? sym)
"")
((symbol? (car sym))
(symbol->string (car sym)))
((string? (car sym))
(car sym))
(else
(error "apropos: expected string or symbol, got"
(car sym))))))
(mergesort
(lambda (a b)
(string<=? (symbol->string a)
(symbol->string b)))
(remp null?
(map (lambda (x)
(let ((s (symbol->string x)))
(if (and (string-find name s)
(locate-file
(string-append
"help/"
(name->file-name s))))
x
'())))
(symbols))))))))