1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions kernel sequences strings math assocs
4 words generic namespaces assocs quotations splitting
12 PREDICATE: word listener-command +listener+ word-prop ;
14 GENERIC: invoke-command ( target command -- )
16 GENERIC: command-name ( command -- str )
18 TUPLE: command-map blurb ;
20 GENERIC: command-description ( command -- str/f )
22 GENERIC: command-word ( command -- word )
24 : <command-map> ( blurb commands -- command-map )
26 { set-command-map-blurb set-delegate }
27 \ command-map construct ;
29 : commands ( class -- hash )
30 dup "commands" word-prop [ ] [
31 H{ } clone [ "commands" set-word-prop ] keep
34 : command-map ( group class -- command-map )
37 : command-gestures ( class -- hash )
41 [ [ invoke-command ] curry swap set ] assoc-each
45 : update-gestures ( class -- )
46 dup command-gestures "gestures" set-word-prop ;
48 : define-command-map ( class group blurb pairs -- )
50 swap pick commands set-at
53 : (command-name) ( string -- newstring )
54 "-" split " " join unclip ch>upper add* ;
56 M: word command-name ( word -- str )
59 dup first Letter? [ 1 tail ] unless
62 M: word command-description ( word -- str )
63 +description+ word-prop ;
65 : default-flags ( -- assoc )
66 H{ { +nullary+ f } { +listener+ f } { +description+ f } } ;
68 : define-command ( word hash -- )
69 default-flags swap union >r word-props r> update ;
71 : command-quot ( target command -- quot )
72 dup 1quotation swap +nullary+ word-prop
73 [ nip ] [ curry ] if ;
75 M: word invoke-command ( target command -- )
78 M: word command-word ;
80 M: f invoke-command ( target command -- ) 2drop ;
82 : command-string ( gesture command -- string )
85 gesture>string [ " (" % % ")" % ] when*