factor/basis/help/help.factor

186 lines
4.7 KiB
Factor
Raw Normal View History

2008-02-08 02:08:52 -05:00
! Copyright (C) 2005, 2008 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
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 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
] [
[ name>> ]
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 ( topic -- )
[
[ help-path [ \ $links "Up:" $navigation-row ] unless-empty ]
[ prev-article [ 1array \ $long-link "Prev:" $navigation-row ] when* ]
[ next-article [ 1array \ $long-link "Next:" $navigation-row ] when* ]
tri
] { } make [ $table ] unless-empty ;
2007-09-20 18:09:08 -04:00
: $title ( topic -- )
title-style get [
title-style get [
2008-12-20 18:32:38 -05:00
[ ($title) ]
[ help-path-style get [ $navigation-table ] with-style ] bi
2007-09-20 18:09:08 -04:00
] with-nesting
] with-style nl ;
2008-11-20 21:34:49 -05:00
: print-topic ( topic -- )
>link
2008-12-20 18:32:38 -05:00
last-element off
[ $title ] [ article-content print-content nl ] bi ;
2007-09-20 18:09:08 -04:00
2008-11-20 21:34:49 -05:00
SYMBOL: help-hook
help-hook global [ [ print-topic ] or ] change-at
: help ( topic -- )
help-hook get call ;
2007-09-20 18:09:08 -04:00
: about ( vocab -- )
2008-02-08 02:08:52 -05:00
dup require
2008-12-20 18:32:38 -05:00
dup vocab [ ] [ no-vocab ] ?if
dup vocab-help [ help ] [
2007-09-20 18:09:08 -04:00
"The " write vocab-name write
" vocabulary does not define a main help article." print
"To define one, refer to \\ ABOUT: help" print
] ?if ;
: ($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 -- )
2008-09-06 20:13:59 -04:00
first call [ ($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 -- )
dup articles get key? [
dup unxref-article
dup articles get delete-at
] when drop ;
: add-article ( article name -- )
[ remove-article ] keep
[ articles get set-at ] keep
xref-article ;
: remove-word-help ( word -- )
dup word-help [ dup unxref-article ] when
f "help" set-word-prop ;
: set-word-help ( content word -- )
[ remove-word-help ] keep
[ swap "help" set-word-prop ] keep
xref-article ;