factor/basis/help/help.factor

191 lines
4.9 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 assocs classes classes.error
classes.tuple combinators combinators.short-circuit
continuations debugger effects generic help.crossref help.markup
help.stylesheet help.topics io io.styles kernel locals make
namespaces prettyprint sequences sets sorting vocabs words
words.symbol ;
2007-09-20 18:09:08 -04:00
IN: help
GENERIC: word-help* ( word -- content )
: word-help ( word -- content )
dup "help" word-prop [ ] [ word-help* ] ?if ;
M: word word-help*
stack-effect [ in>> ] [ out>> ] bi [
[
dup pair? [
first2 dup effect? [ \ $quotation swap 2array ] when
] [
object
] if [ effect>string ] dip
] { } map>assoc
] bi@ append members \ $values prefix 1array ;
: $predicate ( element -- )
{ { "object" object } { "?" boolean } } $values
[
"Tests if the object is an instance of the " ,
first "predicating" word-prop <$link> ,
" class." ,
] { } make $description ;
M: predicate word-help* \ $predicate swap 2array 1array ;
M: class word-help* drop f ;
2007-12-12 00:32:35 -05:00
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 ] reject
{ "help.home" "handbook" } diff ;
2007-09-20 18:09:08 -04:00
: xref-help ( -- )
all-articles [ xref-article ] each ;
: error? ( word -- ? )
{
[ error-class? ]
[ \ $error-description swap word-help elements empty? not ]
} 1|| ;
2007-09-20 18:09:08 -04:00
: 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 , ]
2015-08-13 20:46:40 -04:00
[ dup global at [ get-global \ $value swap 2array , ] [ drop ] if ]
[ \ $definition swap 2array , ]
} cleave
2007-09-20 18:09:08 -04:00
] { } make ;
M: word article-content (word-help) ;
: 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-table) ( element -- )
2012-07-19 14:24:45 -04:00
help-path-style get table-style [ $table ] with-variable ;
: ($navigation-path) ( topic -- )
help-path-style get [
help-path [ reverse $breadcrumbs ] unless-empty
] with-style ;
: ($navigation-link) ( content element label -- )
[ prefix 1array ] dip prefix , ;
: ($navigation-links) ( topic -- )
2015-08-16 14:38:38 -04:00
help-path-style get [
[
[ prev-article [ 1array \ $long-link "Prev:" ($navigation-link) ] when* ]
[ next-article [ 1array \ $long-link "Next:" ($navigation-link) ] when* ]
bi
] { } make [ ($navigation-table) ] unless-empty
] with-style ;
2007-09-20 18:09:08 -04:00
: $title ( topic -- )
title-style get [
title-style get [
[ ($title) ]
[ ($navigation-path) ]
[ ($navigation-links) ] tri
] 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 nl ] bi ;
2007-09-20 18:09:08 -04:00
2008-11-20 21:34:49 -05:00
SYMBOL: help-hook
help-hook [ [ print-topic ] ] 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 -- )
2016-03-20 17:39:13 -04:00
[ articles get set-at ] keep xref-article ;
2007-09-20 18:09:08 -04:00
: remove-word-help ( word -- )
f "help" set-word-prop ;
: set-word-help ( content word -- )
2016-03-20 17:39:13 -04:00
[ swap "help" set-word-prop ] keep xref-article ;