Keyboard mapping: initialize default shortcuts in `DeviceWorkstation` jv
authorJan Vrany <jan.vrany@fit.cvut.cz>
Sun, 14 May 2017 21:48:34 +0200
branchjv
changeset 8037 d01faa4c1b02
parent 8036 e248f3b96260
child 8038 c7cab63f9ec9
Keyboard mapping: initialize default shortcuts in `DeviceWorkstation` ...rather than in `keyboard.rc` startup script. To make such initialization easy, introduced `#loadKeyboardMapDefinition:`
DeviceWorkstation.st
KeyboardMap.st
--- a/DeviceWorkstation.st	Thu Feb 09 00:17:19 2017 +0000
+++ b/DeviceWorkstation.st	Sun May 14 21:48:34 2017 +0200
@@ -609,6 +609,173 @@
 
 defaultButtonPressTimeForMenu:seconds
     DefaultButtonPressTimeForMenu := seconds
+!
+
+defaultKeyboardMapDefinition
+    "
+     Return a default shortcut bindings. See comment in
+     KeyboardMap >> loadKeyboardMapDefinition: for the description of definition
+     format."
+
+    ^ #(
+"/      Logical             Shortcut        Aliases    Description
+"/      ================================================================================
+
+    (   Cmd                 Alt             (Menu Mode_switch)                              )
+    (   Menu                Cmdz            (Appl CtrlAppl CmdAppl) 'Show context menu'     )
+
+"/  (   Hardcopy            #'Super_R'                                                      )
+    (   Alt                 #'Super_L'      #'Hyper_R'                                      )
+
+    (   UserInterrupt       Break           (#'Cmd.' #'Ctrl.') 'Interrupt - enter debugger' )
+    (   UserAbort           CtrlBreak       (CtrlCancel CtrlPause CmdY) 'Abort - do not enter debugger' )
+    (   FlushInput          CtrlX                       'Flush (trow away) typeahead input' )
+                                    
+"/  (   DestroyView         CmdCtrlX                                                        )
+    (   DestroyTopView      CmdCtrlX                                                        )
+
+"/  (   CloseWindowRequest  CmdF4                       'Close active window'               )
+
+    (   DoIt                Cmdd            Ctrld       'DoIt - evaluate expression'        )
+    (   InspectIt           Cmdi            Ctrlq       'InspectIt - evaluate expression and inspect result')  
+    (   PrintIt             Cmdp            Ctrlp       'PrintIt - evaluate expression and print result')
+    (   ReplaceIt           CmdP                        'Replace selection with result of expression evaluation')
+
+    (   BrowseIt            CmdB            CtrlB       'Evaluate expression and browse result''s class')
+    (   ImplementorsOfIt    CtrlI           CmdI        'Browse implementors of selected selector')
+"/  (   SendersOfIt         CtrlS           CmdS        'Browse senders of selected selector')
+
+
+"/      Logical             Shortcut        Aliases     Description
+"/      ===========================================================
+
+    (   ToggleInsertMode    AltInsert                                                       )
+
+    (   Cut                 Ctrlx           Cmdx        'Cut'                               )
+    (   Copy                Ctrlc           (Cmdc CtrlInsert) 'Copy'                        )
+    (   Paste               Ctrlv           (Cmdv ShiftInsert) 'Paste'                      )
+
+    (   PasteFromHistory    CtrlV           CmdV                                            )
+
+    (   Replace             Cmdr            Ctrlr       'Replace'                           )
+    (   Join                Cmdj            Ctrlj                                           )
+
+    (   Find                Ctrlf           Cmds        'Search (open search dialog)'       )
+    (   FindNext            Cmdf                        'Forward search (selection or previous search pattern)'  )
+    (   FindPrev            Cmdb            Ctrlb       'Backward search (selection or previous search pattern)' )
+    (   SelectAll           Ctrla           CmdA        'Select all'                        )
+    (   GotoLine            Ctrlg           Ctrll       'Go to line'                        )
+    (   SelectWord          Cmdw                        'Select Word under cursor'          )
+    (   Undo                Ctrlz                       'Undo last operation'               )
+    (   Redo                Ctrly                       'Redo last operation'               )
+    (   Accept              Cmda            Ctrls       'Accept / Save (compile or save)'   )
+    (   SaveAs              CmdS            CtrlS       'Save as (a file)'                  )
+    (   Explain             Cmde            Cmdh        'Explain code'                      )
+    (   Format              CmdF                                                            )
+
+
+"/      Logical             Shortcut        Aliases     Description                         )
+"/      ===========================================================
+
+"/  (   DeleteLine          CtrlX                                                           )
+    (   NextPage            Next                                                            )
+    (   PreviousPage        Prior                                                           )
+"/  (   HalfPageUp          Ctrlu                                                           )
+"/  (   HalfPageDown        Ctrld                                                           )
+    (   BeginOfLine         Home                        'Goto begin of line'                )
+    (   BeginOfText         CtrlHome                    'Goto begin of whole text'          )
+
+    (   EndOfLine           End             Ctrle       'Goto end of line'                  )
+    (   EndOfText           CtrlEnd                     'Goto end of whole text'            )
+    (   EndOfWord           CtrlW                       'Goto end of word under cursor'     )
+
+    (   NextWord            CtrlCursorRight Ctrlw       'Move to next word'                 )
+    (   PreviousWord        CtrlCursorLeft              'Move to previous word'             )
+
+    (   ScrollUp            CtrlCursorUp                                                    )
+    (   ScrollDown          CtrlCursorDown                                                  )
+
+    (   SearchMatchingParent  Ctrlm                                                         )
+    (   SelectMatchingParents Cmdm                      'Select up to matching parenthesis' )
+
+    (   SelectToEnd          CtrlE                      'Select from end of text'           )
+    (   SelectFromBeginning  CtrlA                      'Select from beginning of text'     )
+    (   ExpandSelectionByLine Cmdl                                                          )
+    (   ExpandSelectionByWord CmdW                                                          )
+    (   DeleteWordBeforeCursor CtrlBackSpace                                                )
+    (   Return               KP_Enter                                                       )
+    (   Delete               OK_Delete                                                      )
+
+"/      Logical             Shortcut        Aliases     Description
+"/      ===========================================================
+    (   ZoomIn              #'Ctrl+'                                                        )
+    (   ZoomOut             #'Ctrl-'                                                        )
+    (   ZoomInAll           #'Ctrl*'                                                        )
+    (   ZoomOutAll          #'Ctrlunderscore'                                               )
+
+    (   FocusNext           CmdCursorRight  (CmdCursorDown AltCursorRight AltCursorDown CmdTab))
+    (   FocusPrevious       CmdCursorLeft   (CmdCursorUp AltCursorLeft AltCursorUp #'CmdISO_Left_Tab'))
+
+    (   NonInsertingTab     ShiftTab        #'ISO_Left_Tab'                                 )
+    (   BackTab             CtrlTab                                                         )
+"/  (   ToggleTabs          CtrlTab                                                         )
+
+    (   Help                F1                          'Help / Explain code'               )
+    (   Rename              F2                          'Rename'                            )
+    (   CommentSelection    F3                          'Comment selection'                 )
+    (   UncommentSelection  F4                          'Uncomment selection'               )
+"/  (                       F5                                                              )
+    (ConvertSelectionToLowercaseOrUppercaseOrUppercaseFirst                       
+    	                    F6                          'Selection to lowercase/UPPERCASE/UpperCaseFirst (toggle through)' )
+"/  (                       F7                                                              )
+    (   Again               F8                          'Again (repeat last cut/replace)'   )
+    (   UndentBy4           F9                          'Undent by 4 (move block to left)'  )
+    (   IndentBy4           F10                         'Indent by 4 (move block to right)' )
+    (   UndentBy1           F11                         'Undent by 1 (move block to left)'  )
+    (   IndentBy1           F12                         'Indent by 4 (move block to right)' )
+
+    (   ParenthizeSelection #'Cmd('                     'Add parentheses around selection'  )
+    (   UnparenthizeSelection #'Cmd)'   	        'Remove parentheses'                )
+    (   LearnKeyboardMacro  CmdL  		CtrlF2  'Enter/leave learn macro mode (CmdM) to replay macro)' )
+    (   ExecuteKeyboardMacro CmdM               CmdF2   'Execute keyboard Macro (CMD-L to learn)' )
+
+    (   ConvertSelectionToUppercase                       
+    	                    CtrlF6                      'Selection to UPPERCASE'            )    
+    (   ExpandAbbreviation   #'Cmd '                    'Insert Abbreviation (try <ALT-SPACE> behind the string ''it'')')                                    
+    (   CodeCompletion       #'Ctrl '		#'Ctrlspace'				    )
+
+
+"/      Logical             Shortcut        Aliases     Description
+"/      ===========================================================
+
+"/	Following was used only on NON-macOS systems, not sure why. In case it 
+"/      would cause problems, refactor. I cannot test now ans would like to make
+"/      the code simpler. 
+
+    (   $[                  #'Cmd['         #'Alt['                         )
+    (   $]                  #'Cmd]'         #'Alt]'                         )
+    (   $|                  #'Cmd|'         #'Alt|'                         )
+    (   ${                  #'Cmd{'         #'Alt{'                         )
+    (   $}                  #'Cmd}'         #'Alt}'                         )
+    (   $\                  #'Cmd\'         #'Alt\'                         )
+    (   $~                  #'Cmd~'         #'Alt~'                         )
+    (   $@                  #'Cmd@'         #'Alt@'                         )
+    (   $#                  #'Cmd#'         #'Alt#'            	            )
+    
+    )
+    "
+    DeviceWorkstation defaultKeyboardMapDefinition.
+
+    Screen current
+        instVarNamed: #keyboardMap put: nil;
+        initializeKeyboardMap;
+        keyboardMap.
+
+    Screen current keyboardMap. 
+    "
+
+    "Created: / 12-05-2017 / 21:51:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 14-05-2017 / 10:41:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !DeviceWorkstation class methodsFor:'error handling'!
@@ -6120,11 +6287,9 @@
 !
 
 initializeDefaultKeyboardMappingsIn:aKeyboardMap
-    aKeyboardMap bindValue:#Copy          to:#Cmdc.     "copy selection to buffer"
-    aKeyboardMap bindValue:#Cut           to:#Cmdx.     "cut selection into buffer"
-    aKeyboardMap bindValue:#Paste         to:#Cmdv.     "paste buffer or external selection"
-
-    aKeyboardMap bindValue:#UserInterrupt to:#Ctrlc.    "interrupt window process"
+    aKeyboardMap loadKeyboardMapDefinition: self class defaultKeyboardMapDefinition.
+
+    "Modified: / 12-05-2017 / 21:55:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 initializeDefaultValues
--- a/KeyboardMap.st	Thu Feb 09 00:17:19 2017 +0000
+++ b/KeyboardMap.st	Sun May 14 21:48:34 2017 +0200
@@ -74,6 +74,21 @@
     prefix of Alt, the second has Cmd as prefix. Keyboards with only an Alt
     key will will create prefix codes of Cmd for that.
 
+    A convenient way to create initialize a keyboard map is to use
+    #loadKeyboadMapDefinition: as in:
+
+        |m|
+
+        m := Display keyboardMap.
+        m loadKeyboadMapDefinition: #(
+            (Cut    Ctrlx   Cmdx)
+            (Copy   Ctrlc   Cmdc)
+            (Paste  Ctrlv   (Cmdv ShiftInsert)
+        )
+
+    For details about the format of definition array see commebt in
+    KeyboardMap >> #loadKeyboardMapDefinition: .
+
     To remove a mapping, use the same value for both logical and physical key,
     as in:
 
@@ -82,24 +97,14 @@
         m := Display keyboardMap.
         m bindValue:#Cmdd to:#Cmdd.
 
-    In addition to (primary) shortcut mapping (defined by #bindValue:to:) one may also
-    define an alias, kind of secondary mapping. This is to support users coming from other
-    environments with other default shortcuts so 'their' shortcuts just work without need
-    to manually customize Smalltalk/X. Just to be a little friendly to foreigners.
-
-    To define an alias, use #bindAlias:to: as in:
-
-        |m|
-
-        m := Display keyboardMap.
-        m bindAlias:#Paste to:#Shiftnsert.
-
     [see also:]
         WindowEvent WindowSensor WindowGroup
         View DeviceWorkstation
+        KeyboardMap >> #loadKeyboardMapDefinition:
 
     [author:]
         Claus Gittinger
+        Jan Vrany
 "
 ! !
 
@@ -456,6 +461,9 @@
 
     "Created: / 08-02-2017 / 23:43:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 15-05-2017 / 21:29:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+
+    Screen current keyboardMap valueFor: #Alti
+    "Modified (comment): / 14-05-2017 / 10:44:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !KeyboardMap methodsFor:'testing'!
@@ -464,6 +472,60 @@
     ^ true
 ! !
 
+!KeyboardMap methodsFor:'utilities'!
+
+loadKeyboardMapDefinition: definitions
+    "Load keyboard mapping from given `definition`. The `definition` is a collection
+     mapping definitions. Each mapping definition is an array consisting of:
+
+        #( <logical key>     <primary shortcut>  <aliases>?  <description>? )
+
+    where:
+        <logical key>   the symbolic name of the action as used in menu specs
+                        #keyPress:x:y: methods and so on.
+                        Examples: #Copy, #Find, #Undo
+
+        <primary shortcut> the actual shortcut in Smalltalk/X key event notation.
+                        This is the shortcut that will be shown in menus and/or
+                        tooltips and so.
+                        Examples: #Ctrlv (means Ctrl + V), CmdF (means Alt + Shift + F), F10
+
+        <aliases>       an alternative shortcut or array of those. Aliases won't be shown in
+                        menu items but one may define some for user's convenience (for exampple,
+                        define aliases used in other environments so users using both can just
+                        keep using them. Optional.
+                        Examples: #Ctrlv, #(#Cmdv #ShiftInsert)
+
+        <description>   A textual description of the action. This may be used to generate some
+                        form of documentation. Optional.
+                        Examples: 'DoIt - evaluate expression', 'Replace selection with today''s date'
+
+    "
+    definitions do:[:definition | 
+        | logical shortcut aliases|
+
+        logical := definition first.
+        shortcut := definition second.
+        "/ aliases are optional as well as description, so it may happen that third
+        "/ element is description rather than alias. Hence the check.
+        aliases := (definition size > 2) ifTrue:[ definition third ] ifFalse:[ #() ].
+        aliases isSymbol ifTrue:[ aliases := Array with: aliases ].
+        aliases isArray ifFalse:[ aliases := #() ].
+
+        self bindValue: logical to: shortcut.
+        aliases do:[:alias | self bindAlias: logical to: alias ]
+    ].
+
+    "
+    KeyboardMap new loadKeyboardMapDefinition: #( (Paste Ctrlv (Cmdv CtrlInsert)) )
+    KeyboardMap new loadKeyboardMapDefinition: #( (Paste Ctrlv Cmdv) )
+    KeyboardMap new loadKeyboardMapDefinition: #( (Paste Ctrlv 'Paste') )   
+    "
+
+    "Created: / 12-05-2017 / 22:12:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 14-05-2017 / 10:24:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !KeyboardMap class methodsFor:'documentation'!
 
 version