factor/basis/ui/commands/commands.factor

111 lines
2.6 KiB
Factor
Raw Permalink Normal View History

! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs fry help.markup kernel make quotations
sequences splitting tr ui.gestures unicode words ;
2007-09-20 18:09:08 -04:00
IN: ui.commands
SYMBOL: +nullary+
SYMBOL: +listener+
SYMBOL: +description+
2008-03-26 19:23:19 -04:00
PREDICATE: listener-command < word +listener+ word-prop ;
2007-09-20 18:09:08 -04:00
GENERIC: invoke-command ( target command -- )
GENERIC: command-name ( command -- str )
TUPLE: command-map blurb commands ;
2007-09-20 18:09:08 -04:00
GENERIC: command-description ( command -- str/f )
GENERIC: command-word ( command -- word )
: <command-map> ( blurb commands -- command-map )
2015-07-28 22:13:43 -04:00
{ } like command-map boa ;
2007-09-20 18:09:08 -04:00
: commands ( class -- hash )
dup "commands" word-prop [ ] [
H{ } clone [ "commands" set-word-prop ] keep
] ?if ;
TR: convert-command-name "-" " " ;
: (command-name) ( string -- newstring )
convert-command-name >title ;
: get-command-at ( group class -- command-map )
2007-09-20 18:09:08 -04:00
commands at ;
: command-map-row ( gesture command -- seq )
[
[ gesture>string , ]
[
[ command-name , ]
[ command-word <$link> , ]
[ command-description , ]
tri
] bi*
] { } make ;
: command-map. ( alist -- )
[ command-map-row ] { } assoc>map
{ "Shortcut" "Command" "Word" "Notes" }
[ \ $strong swap ] { } map>assoc prefix
$table ;
: $command-map ( element -- )
[ second (command-name) " commands" append $heading ]
[
first2 swap get-command-at
[ blurb>> print-element ] [ commands>> command-map. ] bi
] bi ;
: $command ( element -- )
reverse first3 get-command-at
commands>> value-at gesture>string
$snippet ;
2007-09-20 18:09:08 -04:00
: command-gestures ( class -- hash )
commands values [
[
commands>>
sift-keys
2012-07-19 12:50:09 -04:00
[ '[ _ invoke-command ] swap ,, ] assoc-each
2007-09-20 18:09:08 -04:00
] each
2012-07-19 12:50:09 -04:00
] H{ } make ;
2007-09-20 18:09:08 -04:00
: update-gestures ( class -- )
2012-04-18 20:46:01 -04:00
dup command-gestures set-gestures ;
2007-09-20 18:09:08 -04:00
: define-command-map ( class group blurb pairs -- )
<command-map>
swap pick commands set-at
update-gestures ;
M: word command-name
name>>
"com-" ?head drop "." ?tail drop
dup first Letter? [ rest ] unless
2007-09-20 18:09:08 -04:00
(command-name) ;
M: word command-description
2007-09-20 18:09:08 -04:00
+description+ word-prop ;
: default-flags ( -- assoc )
H{ { +nullary+ f } { +listener+ f } { +description+ f } } ;
: define-command ( word hash -- )
default-flags swap assoc-union
'[ _ assoc-union ] change-props drop ;
2007-09-20 18:09:08 -04:00
: command-quot ( target command -- quot )
2009-02-21 17:42:57 -05:00
[ 1quotation ] [ +nullary+ word-prop ] bi
2007-09-20 18:09:08 -04:00
[ nip ] [ curry ] if ;
M: word invoke-command
command-quot call( -- ) ;
2007-09-20 18:09:08 -04:00
M: word command-word ;
M: f invoke-command 2drop ;