| 
									
										
										
										
											2008-07-11 01:01:22 -04:00
										 |  |  | ! Copyright (C) 2006, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2015-12-04 07:29:29 -05:00
										 |  |  | USING: accessors assocs fry help.markup kernel make quotations | 
					
						
							| 
									
										
										
										
											2016-03-31 02:29:48 -04:00
										 |  |  | 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 )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-11 01:01:22 -04:00
										 |  |  | 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-04 07:29:29 -05:00
										 |  |  | TR: convert-command-name "-" " " ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (command-name) ( string -- newstring )
 | 
					
						
							|  |  |  |     convert-command-name >title ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-21 03:03:27 -04:00
										 |  |  | : get-command-at ( group class -- command-map )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     commands at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-04 07:29:29 -05:00
										 |  |  | : 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 [ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-07-11 01:01:22 -04:00
										 |  |  |             commands>> | 
					
						
							| 
									
										
										
										
											2012-08-24 01:36:10 -04:00
										 |  |  |             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 ( word -- str )
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |     name>> | 
					
						
							| 
									
										
										
										
											2009-01-31 00:52:02 -05:00
										 |  |  |     "com-" ?head drop "." ?tail drop
 | 
					
						
							| 
									
										
										
										
											2008-04-26 03:01:43 -04:00
										 |  |  |     dup first Letter? [ rest ] unless
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     (command-name) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word command-description ( word -- str )
 | 
					
						
							|  |  |  |     +description+ word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : default-flags ( -- assoc )
 | 
					
						
							|  |  |  |     H{ { +nullary+ f } { +listener+ f } { +description+ f } } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-command ( word hash -- )
 | 
					
						
							| 
									
										
										
										
											2010-02-03 08:55:00 -05:00
										 |  |  |     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 ( target command -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:49:48 -05:00
										 |  |  |     command-quot call( -- ) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: word command-word ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-18 20:46:01 -04:00
										 |  |  | M: f invoke-command ( target command -- ) 2drop ;
 |