1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes colors colors.constants
4 combinators combinators.smart compiler.units definitions
5 definitions.icons effects fry generic hash-sets hashtables
6 help.stylesheet help.topics io io.styles kernel locals make math
7 namespaces parser present prettyprint prettyprint.stylesheet
8 quotations see sequences sequences.private sets slots sorting
9 splitting strings urls vectors vocabs vocabs.loader words
11 FROM: prettyprint.sections => with-pprint ;
14 PREDICATE: simple-element < array
15 [ t ] [ first word? not ] if-empty ;
22 : last-span? ( -- ? ) last-element get span eq? ;
23 : last-block? ( -- ? ) last-element get block eq? ;
24 : last-blank-line? ( -- ? ) last-element get blank-line eq? ;
31 : ($blank-line) ( -- )
32 nl nl blank-line last-element namespaces:set ;
35 last-block? [ nl ] when
36 span last-element namespaces:set
39 GENERIC: print-element ( element -- )
41 M: simple-element print-element [ print-element ] each ;
42 M: string print-element [ write ] ($span) ;
43 M: array print-element unclip execute( arg -- ) ;
44 M: word print-element { } swap execute( arg -- ) ;
45 M: effect print-element effect>string print-element ;
46 M: f print-element drop ;
48 : print-element* ( element style -- )
49 [ print-element ] with-style ;
51 : with-default-style ( quot -- )
52 default-style get swap with-nesting ; inline
54 : print-content ( element -- )
55 [ print-element ] with-default-style ;
57 : ($block) ( quot -- )
59 span last-element namespaces:set
61 block last-element namespaces:set ; inline
65 : $snippet ( children -- )
66 [ snippet-style get print-element* ] ($span) ;
68 : $emphasis ( children -- )
69 [ emphasis-style get print-element* ] ($span) ;
71 : $strong ( children -- )
72 [ strong-style get print-element* ] ($span) ;
74 : $url ( children -- )
75 [ ?second ] [ first ] bi [ or ] keep >url [
76 dup present href associate url-style get assoc-union
77 [ write-object ] with-style
81 drop nl last-element get [ nl ] when
82 blank-line last-element namespaces:set ;
85 : ($heading) ( children quot -- )
88 : $heading ( element -- )
89 [ heading-style get print-element* ] ($heading) ;
91 : $subheading ( element -- )
92 [ strong-style get print-element* ] ($heading) ;
94 : ($code-style) ( presentation -- hash )
95 presented associate code-style get assoc-union ;
97 : ($code) ( presentation quot -- )
100 [ ($code-style) ] dip with-nesting
103 : $code ( element -- )
104 "\n" join dup <input> [ write ] ($code) ;
106 : $syntax ( element -- ) "Syntax" $heading $code ;
108 : $description ( element -- )
109 "Word description" $heading print-element ;
111 : $class-description ( element -- )
112 "Class description" $heading print-element ;
114 : $error-description ( element -- )
115 "Error description" $heading print-element ;
117 : $var-description ( element -- )
118 "Variable description" $heading print-element ;
120 : $contract ( element -- )
121 "Generic word contract" $heading print-element ;
123 : $examples ( element -- )
124 "Examples" $heading print-element ;
126 : $example ( element -- )
127 unclip-last [ "\n" join ] dip over <input> [
128 [ print ] [ output-style get format ] bi*
131 : $unchecked-example ( element -- )
132 ! help-lint ignores these.
135 : $markup-example ( element -- )
136 first dup unparse " print-element" append 1array $code
139 : $warning ( element -- )
143 "Warning" $heading print-element
147 : $deprecated ( element -- )
149 deprecated-style get [
151 "This word is deprecated" $heading print-element
156 : $image ( element -- )
157 [ first write-image ] ($span) ;
159 : <$image> ( path -- element )
160 1array \ $image prefix ;
166 : write-link ( string object -- )
167 link-style get [ write-object ] with-style ;
169 : link-icon ( topic -- )
170 definition-icon 1array $image ;
172 : link-text ( topic -- )
173 [ article-name ] keep write-link ;
175 GENERIC: link-long-text ( topic -- )
177 M: topic link-long-text
178 [ article-title ] keep write-link ;
180 GENERIC: link-effect? ( word -- ? )
182 M: parsing-word link-effect? drop f ;
183 M: symbol link-effect? drop f ;
184 M: word link-effect? drop t ;
186 : $effect ( effect -- )
187 effect>string stack-effect-style get format ;
189 M: word link-long-text
190 dup presented associate [
191 [ article-name link-style get format ]
194 bl stack-effect $effect
199 : >topic ( obj -- topic ) dup topic? [ >link ] unless ;
201 : topic-span ( topic quot -- ) [ >topic ] dip ($span) ; inline
203 ERROR: number-of-arguments found required ;
205 : check-first ( seq -- first )
206 dup length 1 = [ length 1 number-of-arguments ] unless
209 : check-first2 ( seq -- first second )
210 dup length 2 = [ length 2 number-of-arguments ] unless
215 : ($link) ( topic -- ) [ link-text ] topic-span ;
217 : $link ( element -- ) check-first ($link) ;
219 : ($long-link) ( topic -- ) [ link-long-text ] topic-span ;
221 : $long-link ( element -- ) check-first ($long-link) ;
223 : ($pretty-link) ( topic -- )
224 [ [ link-icon ] [ drop bl ] [ link-text ] tri ] topic-span ;
226 : $pretty-link ( element -- ) check-first ($pretty-link) ;
228 : ($long-pretty-link) ( topic -- )
229 [ [ link-icon ] [ drop bl ] [ link-long-text ] tri ] topic-span ;
231 : <$pretty-link> ( definition -- element )
232 1array \ $pretty-link prefix ;
234 : ($subsection) ( element quot -- )
236 subsection-style get [ call ] with-style
239 : $subsection* ( topic -- )
241 [ ($long-pretty-link) ] with-scope
244 : $subsections ( children -- )
245 [ $subsection* ] each ($blank-line) ;
247 : $subsection ( element -- )
248 check-first $subsection* ;
250 : ($vocab-link) ( text vocab -- )
251 >vocab-link write-link ;
253 : $vocab-subsection ( element -- )
255 check-first2 dup vocab-help
256 [ 2nip ($long-pretty-link) ]
257 [ [ >vocab-link link-icon bl ] [ ($vocab-link) ] bi ]
261 : $vocab-link ( element -- )
262 check-first [ vocab-name ] keep ($vocab-link) ;
264 : $vocabulary ( element -- )
265 check-first vocabulary>> [
266 "Vocabulary" $heading nl dup ($vocab-link)
269 : (textual-list) ( seq quot sep -- )
270 '[ _ print-element ] swap interleave ; inline
272 : textual-list ( seq quot -- )
273 ", " (textual-list) ; inline
275 : $links ( topics -- )
276 [ [ ($link) ] textual-list ] ($span) ;
278 : $vocab-links ( vocabs -- )
279 [ lookup-vocab ] map $links ;
281 : $breadcrumbs ( topics -- )
282 [ [ ($link) ] " > " (textual-list) ] ($span) ;
284 : $see-also ( topics -- )
285 "See also" $heading $links ;
288 :: update-related-words ( words -- affected-words )
289 words words [| affected word |
290 word "related" [ affected union words ] change-word-prop
293 :: clear-unrelated-words ( words affected-words -- )
294 affected-words words diff
295 [ "related" [ words diff ] change-word-prop ] each ;
297 : notify-related-words ( affected-words -- )
298 fast-set notify-definition-observers ;
302 : related-words ( seq -- )
303 dup update-related-words
304 [ clear-unrelated-words ] [ notify-related-words ] bi ;
306 : $related ( element -- )
307 check-first dup "related" word-prop remove
308 [ $see-also ] unless-empty ;
310 : ($grid) ( style quot -- )
312 table-content-style get [
313 swap [ last-element off call ] tabular-output
317 : $list ( element -- )
321 bullet get write-cell
322 [ print-element ] with-cell
327 : $table ( element -- )
331 [ [ print-element ] with-cell ] each
337 ALIAS: $slot $snippet
339 : $slots ( children -- )
340 [ unclip \ $slot swap 2array prefix ] map $table ;
342 : a/an ( str -- str )
343 [ first ] [ length ] bi 1 =
344 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
346 GENERIC: ($instance) ( element -- )
348 M: word ($instance) dup name>> a/an write bl ($link) ;
350 M: string ($instance) write ;
352 M: array ($instance) print-element ;
354 M: f ($instance) ($link) ;
356 : $instance ( element -- ) first ($instance) ;
360 { 1 [ first ($instance) ] }
361 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
365 [ [ ($instance) ", " print-element ] each ]
366 [ "or " print-element ($instance) ]
371 : $maybe ( element -- )
374 : $quotation ( element -- )
376 { "a " { $link quotation } " with stack effect " }
377 print-element $snippet ;
379 : ($instances) ( element -- )
380 dup word? [ ($link) "s" print-element ] [ print-element ] if ;
382 : $sequence ( element -- )
383 { "a " { $link sequence } " of " } print-element
385 { 1 [ first ($instances) ] }
386 { 2 [ first2 [ ($instances) " or " print-element ] [ ($instances) ] bi* ] }
390 [ [ ($instances) ", " print-element ] each ]
391 [ "or " print-element ($instances) ]
396 : values-row ( seq -- seq )
397 unclip \ $snippet swap present 2array
398 swap dup first word? [ \ $instance prefix ] when 2array ;
400 : $values ( element -- )
401 "Inputs and outputs" $heading
402 [ values-row ] map $table ;
404 : $side-effects ( element -- )
405 "Side effects" $heading "Modifies " print-element
406 [ $snippet ] textual-list ;
408 : $errors ( element -- )
409 "Errors" $heading print-element ;
411 : $notes ( element -- )
412 "Notes" $heading print-element ;
414 : ($see) ( word quot -- )
415 [ code-style get swap with-nesting ] ($block) ; inline
417 : $see ( element -- ) check-first [ see* ] ($see) ;
419 : $synopsis ( element -- ) check-first [ synopsis write ] ($see) ;
421 : $definition ( element -- )
422 "Definition" $heading $see ;
424 : $methods ( element -- )
425 check-first methods [
430 : $value ( object -- )
431 "Variable value" $heading
432 "Current value in global namespace:" print-element
433 check-first dup [ pprint-short ] ($code) ;
435 : $curious ( element -- )
436 "For the curious..." $heading print-element ;
438 : $references ( element -- )
439 "References" $heading
440 unclip print-element [ \ $link swap ] { } map>assoc $list ;
442 : $shuffle ( element -- )
444 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description ;
446 : $complex-shuffle ( element -- )
448 { "The data flow represented by this shuffle word can be more clearly expressed using " { $link "locals" } "." } $deprecated ;
450 : $low-level-note ( children -- )
452 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
454 : $values-x/y ( children -- )
455 drop { { "x" number } { "y" number } } $values ;
457 : $parsing-note ( children -- )
459 "This word should only be called from parsing words."
462 : $io-error ( children -- )
464 "Throws an error if the I/O operation fails." $errors ;
466 : $prettyprinting-note ( children -- )
468 "This word should only be called from inside the "
469 { $link with-pprint } " combinator."
472 : $content ( element -- )
473 first article-content print-content nl ;
475 GENERIC: elements* ( elt-type element -- )
477 M: simple-element elements*
478 [ elements* ] with each ;
480 M: object elements* 2drop ;
483 [ dup first \ $markup-example eq? [ 2drop ] [ [ elements* ] with each ] if ]
484 [ [ first eq? ] keep swap [ , ] [ drop ] if ] 2bi ;
486 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
488 : collect-elements ( element seq -- elements )
489 swap '[ [ _ elements* ] each ] { } make [ rest ] map concat ;
491 : <$link> ( topic -- element )
492 1array \ $link prefix ;
494 : <$snippet> ( str -- element )
495 1array \ $snippet prefix ;
497 : $definition-icons ( element -- )
500 [ [ <$link> ] [ definition-icon-path <$image> ] bi* swap ] assoc-map
501 { f { $strong "Definition class" } } prefix