1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs fry help.markup kernel make quotations
4 sequences splitting tr ui.gestures unicode.case unicode.categories
12 PREDICATE: listener-command < word +listener+ word-prop ;
14 GENERIC: invoke-command ( target command -- )
16 GENERIC: command-name ( command -- str )
18 TUPLE: command-map blurb commands ;
20 GENERIC: command-description ( command -- str/f )
22 GENERIC: command-word ( command -- word )
24 : <command-map> ( blurb commands -- command-map )
25 { } like command-map boa ;
27 : commands ( class -- hash )
28 dup "commands" word-prop [ ] [
29 H{ } clone [ "commands" set-word-prop ] keep
32 TR: convert-command-name "-" " " ;
34 : (command-name) ( string -- newstring )
35 convert-command-name >title ;
37 : get-command-at ( group class -- command-map )
40 : command-map-row ( gesture command -- seq )
45 [ command-word <$link> , ]
46 [ command-description , ]
51 : command-map. ( alist -- )
52 [ command-map-row ] { } assoc>map
53 { "Shortcut" "Command" "Word" "Notes" }
54 [ \ $strong swap ] { } map>assoc prefix
57 : $command-map ( element -- )
58 [ second (command-name) " commands" append $heading ]
60 first2 swap get-command-at
61 [ blurb>> print-element ] [ commands>> command-map. ] bi
64 : $command ( element -- )
65 reverse first3 get-command-at
66 commands>> value-at gesture>string
69 : command-gestures ( class -- hash )
74 [ '[ _ invoke-command ] swap ,, ] assoc-each
78 : update-gestures ( class -- )
79 dup command-gestures set-gestures ;
81 : define-command-map ( class group blurb pairs -- )
83 swap pick commands set-at
86 M: word command-name ( word -- str )
88 "com-" ?head drop "." ?tail drop
89 dup first Letter? [ rest ] unless
92 M: word command-description ( word -- str )
93 +description+ word-prop ;
95 : default-flags ( -- assoc )
96 H{ { +nullary+ f } { +listener+ f } { +description+ f } } ;
98 : define-command ( word hash -- )
99 default-flags swap assoc-union
100 '[ _ assoc-union ] change-props drop ;
102 : command-quot ( target command -- quot )
103 [ 1quotation ] [ +nullary+ word-prop ] bi
104 [ nip ] [ curry ] if ;
106 M: word invoke-command ( target command -- )
107 command-quot call( -- ) ;
109 M: word command-word ;
111 M: f invoke-command ( target command -- ) 2drop ;