factor/basis/help/help.factor

182 lines
4.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays io io.styles kernel namespaces make
parser prettyprint sequences words words.symbol assocs
definitions generic quotations effects slots continuations
classes.tuple debugger combinators vocabs help.stylesheet
help.topics help.crossref help.markup sorting classes
2009-03-16 21:11:36 -04:00
vocabs.loader ;
2007-09-20 18:09:08 -04:00
IN: help
GENERIC: word-help* ( word -- content )
: word-help ( word -- content )
dup "help" word-prop [ ] [
dup word-help* dup
[ swap 2array 1array ] [ 2drop f ] if
] ?if ;
2007-12-12 00:32:35 -05:00
: $predicate ( element -- )
{ { "object" object } { "?" "a boolean" } } $values
[
"Tests if the object is an instance of the " ,
2008-11-22 04:22:19 -05:00
first "predicating" word-prop <$link> ,
2007-12-12 00:32:35 -05:00
" class." ,
] { } make $description ;
2007-09-20 18:09:08 -04:00
M: word word-help* drop f ;
2007-12-12 00:32:35 -05:00
M: predicate word-help* drop \ $predicate ;
2007-09-20 18:09:08 -04:00
: all-articles ( -- seq )
articles get keys
all-words [ word-help ] filter append ;
2007-09-20 18:09:08 -04:00
: orphan-articles ( -- seq )
articles get keys
[ article-parent not ] filter ;
2007-09-20 18:09:08 -04:00
: xref-help ( -- )
all-articles [ xref-article ] each ;
: error? ( word -- ? )
\ $error-description swap word-help elements empty? not ;
: sort-articles ( seq -- newseq )
[ dup article-title ] { } map>assoc sort-values keys ;
2007-09-20 18:09:08 -04:00
: all-errors ( -- seq )
all-words [ error? ] filter sort-articles ;
2007-09-20 18:09:08 -04:00
M: word valid-article? drop t ;
M: word article-name name>> ;
2007-09-20 18:09:08 -04:00
M: word article-title
2008-06-08 16:32:55 -04:00
dup [ parsing-word? ] [ symbol? ] bi or [
name>>
2007-09-20 18:09:08 -04:00
] [
[ unparse ]
2008-06-08 17:47:20 -04:00
[ stack-effect [ effect>string " " prepend ] [ "" ] if* ] bi
2008-06-08 16:32:55 -04:00
append
2007-09-20 18:09:08 -04:00
] if ;
<PRIVATE
: (word-help) ( word -- element )
2007-09-20 18:09:08 -04:00
[
{
[ \ $vocabulary swap 2array , ]
[ word-help % ]
[ \ $related swap 2array , ]
[ get-global [ \ $value swap 2array , ] when* ]
[ \ $definition swap 2array , ]
} cleave
2007-09-20 18:09:08 -04:00
] { } make ;
M: word article-content (word-help) ;
<PRIVATE
: word-with-methods ( word -- elements )
[
[ (word-help) % ]
[ \ $methods swap 2array , ]
bi
] { } make ;
PRIVATE>
M: generic article-content word-with-methods ;
M: class article-content word-with-methods ;
2007-09-20 18:09:08 -04:00
M: word article-parent "help-parent" word-prop ;
M: word set-article-parent swap "help-parent" set-word-prop ;
2008-12-20 18:32:38 -05:00
: ($title) ( topic -- )
[ [ article-title ] [ >link ] bi write-object ] ($block) ;
: $navigation-row ( content element label -- )
[ prefix 1array ] dip prefix , ;
: ($navigation-table) ( element -- )
help-path-style get table-style set [ $table ] with-scope ;
2008-12-20 18:32:38 -05:00
: $navigation-table ( topic -- )
[
[ prev-article [ 1array \ $long-link "Prev:" $navigation-row ] when* ]
[ next-article [ 1array \ $long-link "Next:" $navigation-row ] when* ]
bi
] { } make [ ($navigation-table) ] unless-empty ;
2007-09-20 18:09:08 -04:00
: ($navigation) ( topic -- )
help-path-style get [
[ help-path [ reverse $breadcrumbs ] unless-empty ]
[ $navigation-table ] bi
] with-style ;
2007-09-20 18:09:08 -04:00
: $title ( topic -- )
title-style get [
title-style get [
[ ($title) ] [ ($navigation) ] bi
] with-nesting
] with-style ;
2007-09-20 18:09:08 -04:00
2008-11-20 21:34:49 -05:00
: print-topic ( topic -- )
>link
2008-12-20 18:32:38 -05:00
last-element off
[ $title ] [ ($blank-line) article-content print-content ] bi ;
2007-09-20 18:09:08 -04:00
2008-11-20 21:34:49 -05:00
SYMBOL: help-hook
2009-11-10 22:48:00 -05:00
help-hook [ [ print-topic nl ] ] initialize
2008-11-20 21:34:49 -05:00
: help ( topic -- )
2009-02-09 02:47:31 -05:00
help-hook get call( topic -- ) ;
2008-11-20 21:34:49 -05:00
2007-09-20 18:09:08 -04:00
: ($index) ( articles -- )
2008-03-11 20:51:58 -04:00
sort-articles [ \ $subsection swap 2array ] map print-element ;
2007-09-20 18:09:08 -04:00
: $index ( element -- )
2009-03-17 03:19:50 -04:00
first call( -- seq ) [ ($index) ] unless-empty ;
2007-09-20 18:09:08 -04:00
: $about ( element -- )
first vocab-help [ 1array $subsection ] when* ;
2008-06-08 16:32:55 -04:00
: :help-debugger ( -- )
2008-02-27 20:24:50 -05:00
nl
"Debugger commands:" print
nl
2008-02-29 20:10:30 -05:00
":s - data stack at error time" print
":r - retain stack at error time" print
":c - call stack at error time" print
2008-02-27 20:24:50 -05:00
":edit - jump to source location (parse errors only)" print
2008-02-29 20:10:30 -05:00
":get ( var -- value ) accesses variables at time of the error" print
2008-03-16 04:43:30 -04:00
":vars - list all variables at error time" print ;
2008-02-27 20:24:50 -05:00
2008-11-24 13:29:24 -05:00
: (:help) ( error -- )
error-help [ help ] [ "No help for this error. " print ] if*
2008-06-08 16:32:55 -04:00
:help-debugger ;
2007-09-20 18:09:08 -04:00
2008-11-24 13:29:24 -05:00
: :help ( -- )
error get (:help) ;
2007-09-20 18:09:08 -04:00
: remove-article ( name -- )
articles get delete-at ;
2007-09-20 18:09:08 -04:00
: add-article ( article name -- )
[ remove-article ] keep
[ articles get set-at ] keep
xref-article ;
: remove-word-help ( word -- )
f "help" set-word-prop ;
: set-word-help ( content word -- )
[ remove-word-help ] keep
[ swap "help" set-word-prop ] keep
xref-article ;