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 hashtables help.stylesheet
6 help.topics io io.styles kernel locals make math namespaces
7 parser present prettyprint prettyprint.stylesheet quotations
8 see sequences sets slots sorting splitting strings urls vectors
9 vocabs vocabs.loader words words.symbol ;
10 FROM: prettyprint.sections => with-pprint ;
11 FROM: namespaces => set ;
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 set ;
35 last-block? [ nl ] when
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: f print-element drop ;
47 : print-element* ( element style -- )
48 [ print-element ] with-style ;
50 : with-default-style ( quot -- )
51 default-span-style get [
52 default-block-style get swap with-nesting
55 : print-content ( element -- )
56 [ print-element ] with-default-style ;
58 : ($block) ( quot -- )
62 block last-element set ; inline
66 : $snippet ( children -- )
67 [ snippet-style get print-element* ] ($span) ;
72 : $emphasis ( children -- )
73 [ emphasis-style get print-element* ] ($span) ;
75 : $strong ( children -- )
76 [ strong-style get print-element* ] ($span) ;
78 : $url ( children -- )
80 dup present href associate url-style get assoc-union
81 [ write-object ] with-style
85 drop nl last-element get [ nl ] when
86 blank-line last-element set ;
89 : ($heading) ( children quot -- )
92 : $heading ( element -- )
93 [ heading-style get print-element* ] ($heading) ;
95 : $subheading ( element -- )
96 [ strong-style get print-element* ] ($heading) ;
98 : ($code-style) ( presentation -- hash )
99 presented associate code-style get assoc-union ;
101 : ($code) ( presentation quot -- )
103 code-char-style get [
105 [ ($code-style) ] dip with-nesting
109 : $code ( element -- )
110 "\n" join dup <input> [ write ] ($code) ;
112 : $syntax ( element -- ) "Syntax" $heading $code ;
114 : $description ( element -- )
115 "Word description" $heading print-element ;
117 : $class-description ( element -- )
118 "Class description" $heading print-element ;
120 : $error-description ( element -- )
121 "Error description" $heading print-element ;
123 : $var-description ( element -- )
124 "Variable description" $heading print-element ;
126 : $contract ( element -- )
127 "Generic word contract" $heading print-element ;
129 : $examples ( element -- )
130 "Examples" $heading print-element ;
132 : $example ( element -- )
133 1 cut* [ "\n" join ] bi@ over <input> [
134 [ print ] [ output-style get format ] bi*
137 : $unchecked-example ( element -- )
138 #! help-lint ignores these.
141 : $markup-example ( element -- )
142 first dup unparse " print-element" append 1array $code
145 : $warning ( element -- )
149 "Warning" $heading print-element
153 : $deprecated ( element -- )
155 deprecated-style get [
157 "This word is deprecated" $heading print-element
162 : $image ( element -- )
163 [ first write-image ] ($span) ;
165 : <$image> ( path -- element )
166 1array \ $image prefix ;
172 : write-link ( string object -- )
173 link-style get [ write-object ] with-style ;
175 : link-icon ( topic -- )
176 definition-icon 1array $image ;
178 : link-text ( topic -- )
179 [ article-name ] keep write-link ;
181 GENERIC: link-long-text ( topic -- )
183 M: topic link-long-text
184 [ article-title ] keep write-link ;
186 GENERIC: link-effect? ( word -- ? )
188 M: parsing-word link-effect? drop f ;
189 M: symbol link-effect? drop f ;
190 M: word link-effect? drop t ;
192 : $effect ( effect -- )
193 effect>string stack-effect-style get format ;
195 M: word link-long-text
196 dup presented associate [
197 [ article-name link-style get format ]
200 bl stack-effect $effect
205 : >topic ( obj -- topic ) dup topic? [ >link ] unless ;
207 : topic-span ( topic quot -- ) [ >topic ] dip ($span) ; inline
211 : ($link) ( topic -- ) [ link-text ] topic-span ;
212 : $link ( element -- ) first ($link) ;
214 : ($long-link) ( topic -- ) [ link-long-text ] topic-span ;
215 : $long-link ( element -- ) first ($long-link) ;
217 : ($pretty-link) ( topic -- )
218 [ [ link-icon ] [ drop bl ] [ link-text ] tri ] topic-span ;
219 : $pretty-link ( element -- ) first ($pretty-link) ;
221 : ($long-pretty-link) ( topic -- )
222 [ [ link-icon ] [ drop bl ] [ link-long-text ] tri ] topic-span ;
224 : <$pretty-link> ( definition -- element )
225 1array \ $pretty-link prefix ;
227 : ($subsection) ( element quot -- )
229 subsection-style get [ call ] with-style
232 : $subsection* ( topic -- )
234 [ ($long-pretty-link) ] with-scope
237 : $subsections ( children -- )
238 [ $subsection* ] each ($blank-line) ;
240 : $subsection ( element -- )
243 : ($vocab-link) ( text vocab -- )
244 >vocab-link write-link ;
246 : $vocab-subsection ( element -- )
248 first2 dup vocab-help
249 [ 2nip ($long-pretty-link) ]
250 [ [ >vocab-link link-icon bl ] [ ($vocab-link) ] bi ]
254 : $vocab-link ( element -- )
255 first dup vocab-name swap ($vocab-link) ;
257 : $vocabulary ( element -- )
259 "Vocabulary" $heading nl dup ($vocab-link)
262 : (textual-list) ( seq quot sep -- )
263 '[ _ print-element ] swap interleave ; inline
265 : textual-list ( seq quot -- )
266 ", " (textual-list) ; inline
268 : $links ( topics -- )
269 [ [ ($link) ] textual-list ] ($span) ;
271 : $vocab-links ( vocabs -- )
272 [ lookup-vocab ] map $links ;
274 : $breadcrumbs ( topics -- )
275 [ [ ($link) ] " > " (textual-list) ] ($span) ;
277 : $see-also ( topics -- )
278 "See also" $heading $links ;
281 :: update-related-words ( words -- affected-words )
282 words words [| affected word |
283 word "related" [ affected union words ] change-word-prop
286 :: clear-unrelated-words ( words affected-words -- )
287 affected-words words diff
288 [ "related" [ words diff ] change-word-prop ] each ;
290 : notify-related-words ( affected-words -- )
291 [ dup associate notify-definition-observers ] each ;
294 : related-words ( seq -- )
295 dup update-related-words
296 [ clear-unrelated-words ] [ notify-related-words ] bi ;
298 : $related ( element -- )
299 first dup "related" word-prop remove
300 [ $see-also ] unless-empty ;
302 : ($grid) ( style quot -- )
304 table-content-style get [
305 swap [ last-element off call ] tabular-output
309 : $list ( element -- )
313 bullet get write-cell
314 [ print-element ] with-cell
319 : $table ( element -- )
323 [ [ print-element ] with-cell ] each
328 : a/an ( str -- str )
329 [ first ] [ length ] bi 1 =
330 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
332 GENERIC: ($instance) ( element -- )
335 dup name>> a/an write bl ($link) ;
337 M: string ($instance)
343 : $instance ( element -- ) first ($instance) ;
347 { 1 [ first ($instance) ] }
348 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
352 [ [ ($instance) ", " print-element ] each ]
353 [ "or " print-element ($instance) ]
358 : $maybe ( element -- )
361 : $quotation ( element -- )
362 { "a " { $link quotation } " with stack effect " } print-element
365 : values-row ( seq -- seq )
366 unclip \ $snippet swap present 2array
367 swap dup first word? [ \ $instance prefix ] when 2array ;
369 : $values ( element -- )
370 "Inputs and outputs" $heading
371 [ values-row ] map $table ;
373 : $side-effects ( element -- )
374 "Side effects" $heading "Modifies " print-element
375 [ $snippet ] textual-list ;
377 : $errors ( element -- )
378 "Errors" $heading print-element ;
380 : $notes ( element -- )
381 "Notes" $heading print-element ;
383 : ($see) ( word quot -- )
385 code-char-style get [
386 code-style get swap with-nesting
390 : $see ( element -- ) first [ see* ] ($see) ;
392 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
394 : $definition ( element -- )
395 "Definition" $heading $see ;
397 : $methods ( element -- )
403 : $value ( object -- )
404 "Variable value" $heading
405 "Current value in global namespace:" print-element
406 first dup [ pprint-short ] ($code) ;
408 : $curious ( element -- )
409 "For the curious..." $heading print-element ;
411 : $references ( element -- )
412 "References" $heading
413 unclip print-element [ \ $link swap ] { } map>assoc $list ;
415 : $shuffle ( element -- )
417 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description ;
419 : $complex-shuffle ( element -- )
421 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description
422 { "The data flow represented by this shuffle word can be more clearly expressed using " { $link "locals" } "." } $deprecated ;
424 : $low-level-note ( children -- )
426 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
428 : $values-x/y ( children -- )
429 drop { { "x" number } { "y" number } } $values ;
431 : $parsing-note ( children -- )
433 "This word should only be called from parsing words."
436 : $io-error ( children -- )
438 "Throws an error if the I/O operation fails." $errors ;
440 : $prettyprinting-note ( children -- )
442 "This word should only be called from inside the "
443 { $link with-pprint } " combinator."
446 GENERIC: elements* ( elt-type element -- )
448 M: simple-element elements*
449 [ elements* ] with each ;
451 M: object elements* 2drop ;
454 [ dup first \ $markup-example eq? [ 2drop ] [ [ elements* ] with each ] if ]
455 [ [ first eq? ] keep swap [ , ] [ drop ] if ] 2bi ;
457 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
459 : collect-elements ( element seq -- elements )
460 swap '[ _ elements [ rest ] map concat ] gather ;
462 : <$link> ( topic -- element )
463 1array \ $link prefix ;
465 : <$snippet> ( str -- element )
466 1array \ $snippet prefix ;
468 : $definition-icons ( element -- )
471 [ [ <$link> ] [ definition-icon-path <$image> ] bi* swap ] assoc-map
472 { "" "Definition class" } prefix