//  Extend layer symbols by DreymaR, for the XKeyboardConfig xkb-data package.
//  2014- OEystein "DreymaR" Bech-Aase (Bech Gadmar until 2021)
//  See "DreymaR's Big Bag of Keyboard Tricks": https://dreymar.colemak.org/layers-extend.html
//  A modular Extend mapping layer like the one I have in Portable Keyboard Layout for Windows
//  - Key names are in /usr/include/X11/keysymdef.h and XF86keysym.h (Removing the XK_ prefix)
//  - <FK##> mapped as XF86 Multimedia keys; shifted versions are implemented (unlike in EPKL for Windows)
//
//  These keys are modified/transposed by holding down the 'extend key' (CapsLock by default):
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |Esc +  |F1 <>  |F2 <<  |F3 >>  |F4 <>  |F5 <>  |F6 <<  |F7 >>  |F8 <>  |F9 <>  |F10<>  |F11<>  |F12<>  |
//  |       | Pause | Rew   | Fwd   | Eject | Refr  | Bri-  | Bri+  | Sleep | WWW   | Mail  | App1  | App2  |
//  | Caps  | Play  | Prev  | Next  | Stop  | Mute  | Vol-  | Vol+  | Media | Home  | Srch  | File  | Calc  |
//  +=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+
//  |` ***  |1      |2      |3      |4      |5      |6      |7      |8      |9      |0      |-      |=      |
//  | *MOn  | F1    | F2    | F3    | F4    | F5    | F6    | F7    | F8    | F9    | F10   | F11   | F12   |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |Tab    |Q €€€  |W ***  |F <<>  |P <>>  |G ***  |J ###  |L ###  |U ###  |Y ###  |; €€€  |[ €€€  |] €€€  |
//  |       | Esc   | *WhUp | BrBck | BrFwd | *MUp  | PgUp  | Home  | Up    | End   | Del   | Esc   | Ins   |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |Caps+  |A +++  |R ***  |S +++  |T +++  |D ***  |H ###  |N ###  |E ###  |I ###  |O €€€  |' €€€  |\ ><>  |
//  | ++++  | Alt   | *WhDn | Shift | Ctrl  | *MDn  | PgDn  | Left  | Down  | Right | Back  | Menu  | BrFav |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |_ €€€  |Z €€€  |X ===  |C ===  |V ===  |B ***  |K ***  |M ***  |, ***  |. ***  |/ €€€  |Spc €  |Entr€  |
//  | Cust  | Undo  | Cut   | Copy  | Paste | *Bt1  | *Bt2  | *Bt3  | *MLe  | *MRi  | Multi | Enter | PrtSc |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  Legend: # Movement; + Modifiers; = GUI edit; * Mouse; <> MultiMedia; € Various commands.
//
//  KeyPad overlay with nav block to the left (lv7-8):
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |` ***  |1      |2      |3      |4 £    |5 €    |6      |7  ⇖   |8  ⇑   |9  ⇗   |0      |-      |=      |
//  | *MOn  |  !    |  @    |  #    |  $    |  %    |  ^    | KP_7  | KP_8  | KP_9  | KP_*  | KP_-  | KP_=  |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |Tab    |Q ###  |W ###  |F ###  |P €€€  |G €€€  |J ###  |L  ⇐   |U  ⇔   |Y  ⇒   |;      |[ {    |] }    |
//  |       | Home  | Up    | End   | Del   | Esc   | PgUp  | KP_4  | KP_5  | KP_6  | KP_+  |  (    |  )    |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |Caps+  |A ###  |R ###  |S ###  |T €€€  |D      |H ###  |N  ⇙   |E  ⇓   |I  ⇘   |O      |' "    |\  |   |
//  | ++++  | Left  | Down  | Right | Back  | K_Lck | PgDn  | KP_1  | KP_2  | KP_3  | K_Ent |  '    |  :    |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//  |_ €€€  |Z €€€  |X ===  |C ===  |V ===  |B ***  |K      |M      |,  ⇕   |.      |/      |Spc    |Entr   |
//  | Cust  | Undo  | Cut   | Copy  | Paste | *Bt1  |  ,    | KP_0  | KP_0  | KP_.  | KP_/  | Spc   | K_Ent |
//  +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
//
//  TODO:
//  - A lv7 modifier to reach the second Extend layer directly.
//      - Test out this:
//        key <RCTL> { [ ISO_Level5_Shift ], actions[Group1] = [ SetMods(mods=LevelThree+LevelFive) ] };
//  
//  NOTE:
//  - A lv7 (Level5+Level3) modifier might be necessary to use the KeyPad level sensibly
//      - One idea is to use AltGr+Caps as a lv7_lock (Caps+AltGr is already lv7_switch)
//  - If a hex pad is needed, the lv7(8?)+KBHDJG -> ABCDEF mappings are one possible solution
//  - There's a KP key named KP_BEGIN; it seems like a hack? Ignored for now.
//  - I've duplicated KP_0 to make it easy to hit. Useful for bigrams with 0. Physical NumPad often has a wide zero.
//  
//  - The Big Bag image has the PointerEnable key on LSGT, but ANSI users don't have that key. Moved it to TLDE.
//  - Autorepeat is lost for keys with actions set. This is probably an XKB bug?
//      - One fix is to run xset afterwards: 'seq -s " r " 8 255 | xargs xset r' supposedly does the trick.
//      - Added the following to each key's symbols[Group1] definition: `repeat=true`
//  - The lv5 mod should be eaten up by xkb but isn't. Must manually clearmod it from all necessary keys.
//  - The first 4 states are defined as `any` (a synonym of `NoSymbol`) to pass-through any existing ones.
//  - KDE seems to have a problem with key redirection. Doesn't KDE respect `clearmods`?
//      - Example: If Ext+Backspace is `Pause` and Ext+O is `Backspace`, KDE may wrongly send `Pause` for Ext+O.
//      - As a result of this problem, the Extend mappings for Backspace and Enter are by default disabled now.


//  +=======+==============================+=======+
//  |=======|       Main Extend Def.       |=======|
//  +=======+==============================+=======+
default partial
xkb_symbols "basic" {
//    name[Group1]= "Extend layer";
//    include "extend(lv5_sk)"    // Extend mappings for special keys: Backspace and Enter. These may not work for all; use with care.
//    include "extend(lv5_fk)"    // The F# key block. Some exclude it as TTY shortcuts are lost. Ext+AltGr+<FK> should work though.
    include "extend(lv5)"       // Extend mappings for all other keys, including Space.
};


partial alphanumeric_keys modifier_keys
xkb_symbols "lv5_sk" {
    key.type[group1]="EIGHT_LEVEL"; // (Caps)Lock state not used on these keys

//  +=======+==============================+=======+
//  |=======|         Special keys         |=======|
//  +=======+==============================+=======+
//  NOTE: Extend-mapping the Backspace and Enter keys doesn't work for all users. Use with care (enable in the include above).
    key <BKSP> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // BackSpace
                    Pause                       , Pause                         ,   // Pause/Break
                    Pause                       , Pause                         ] , // (<BRK> is special)
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<PAUS>,clearmods=LevelFive), Redirect(key=<PAUS>,clearmods=LevelFive),     // <PAUS>
                    NoAction()                  , NoAction()                    ] ,
//  NOTE: Another idea for the Backspace key is Ctrl+Backspace, to delete whole words.
//                    BackSpace                   , BackSpace                   ,   // Ctrl+Backspace (by action)
//                    BackSpace                   , BackSpace                   ] , // If you want Extend+Back to be Ctrl+Back instead
//                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
//      Redirect(key=<BKSP>,mods=Control,clearmods=LevelFive), Redirect(key=<BKSP>,mods=Shift+Control,clearmods=LevelFive),  // Ctrl+Back
//      Redirect(key=<BKSP>,mods=Control,clearmods=LevelFive), Redirect(key=<BKSP>,mods=Shift+Control,clearmods=LevelFive)], // Ctrl+Back
                 repeat=yes     };  // BackSpace        // NOTE: May not work well with KDE?

    key <RTRN> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Return
                   Print                         , Print                        ,   // PrtScr
                   Print                         , Print                        ] , // PrtScr
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<PRSC>,clearmods=LevelFive), Redirect(key=<PRSC>,clearmods=LevelFive),      // <PRSC>
                    NoAction()                    , NoAction()                    ] ,
                 repeat=yes     };  // Return           // NOTE: This mapping breaks the Enter key for some users?
};

partial alphanumeric_keys modifier_keys
xkb_symbols "lv5_fk" {
    key.type[group1]="EIGHT_LEVEL"; // (Caps)Lock state not used on these keys

//  +=======+==============================+=======+
//  |=======|            F1-F12            |=======|
//  +=======+==============================+=======+
//  NOTE: Many use Ctrl+Alt+<FK##> for TTY server control keys. With Extend, you may use Ext+AltGr+<FK##> instead.
    key <FK01> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F01
                    XF86AudioPlay               , XF86AudioPause                ,   // Audio Play/Pause
                    XF86_Switch_VT_1            , Any                           ] , // Virtual Terminal 1
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I208>,clearmods=all), Redirect(key=<I209>,clearmods=all), // <I208>/<I209>(‡‡)
                    SwitchScreen(screen=1,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK02> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F02
                    XF86AudioPrev               , XF86AudioRewind               ,   // Audio Previous/Rewind
                    XF86_Switch_VT_2            , Any                           ] , // Virtual Terminal 2
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I173>,clearmods=all), Redirect(key=<I176>,clearmods=all), // <I173>/<I176>(‡‡)
                    SwitchScreen(screen=2,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK03> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F03
                    XF86AudioNext               , XF86AudioForward              ,   // Audio Next/Forward
                    XF86_Switch_VT_3            , Any                           ] , // Virtual Terminal 3
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I171>,clearmods=all), Redirect(key=<I216>,clearmods=all), // <I171>/<I216>(‡‡)
                    SwitchScreen(screen=3,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK04> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F04
                    XF86AudioStop               , XF86Eject                     ,   // Audio Stop/Eject
                    XF86_Switch_VT_4            , Any                           ] , // Virtual Terminal 4
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I174>,clearmods=all), Redirect(key=<I169>,clearmods=all), // <I174>/<I169>(‡‡)
                    SwitchScreen(screen=4,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK05> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F05
                    XF86AudioMute               , XF86Reload                    ,   // Audio Mute/Reload
                    XF86_Switch_VT_5            , Any                           ] , // Virtual Terminal 5
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<MUTE>,clearmods=all), Redirect(key=<I181>,clearmods=all), // <MUTE>/<I181>(‡‡)
                    SwitchScreen(screen=5,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK06> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F06
                    XF86AudioLowerVolume        , XF86MonBrightnessDown         ,   // Audio Vol- / Brightness-
                    XF86_Switch_VT_6            , Any                           ] , // Virtual Terminal 6
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<VOL->,clearmods=all), Redirect(key=<I232>,clearmods=all), // <VOL->/<I232>(‡‡)
                    SwitchScreen(screen=6,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK07> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F07
                    XF86AudioRaiseVolume        , XF86MonBrightnessUp           ,   // Audio Vol+ / Brightness+
                    XF86_Switch_VT_7            , Any                           ] , // Virtual Terminal 7
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<VOL+>,clearmods=all), Redirect(key=<I233>,clearmods=all), // <VOL+>/<I233>(‡‡)
                    SwitchScreen(screen=7,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK08> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F08
                    XF86AudioMedia              , XF86Sleep                     ,   // MediaPlayer / Sleep
                    XF86_Switch_VT_8            , Any                           ] , // Virtual Terminal 8
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I234>,clearmods=all), Redirect(key=<I150>,clearmods=all), // <I234>/<I150>(‡‡)
                    SwitchScreen(screen=8,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK09> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F09
                    XF86HomePage                , XF86WWW                       ,   // HomePage / Web
                    XF86_Switch_VT_9            , Any                           ] , // Virtual Terminal 9
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I180>,clearmods=all), Redirect(key=<I158>,clearmods=all), // <I180>/<I158>(‡‡)
                    SwitchScreen(screen=9,!same)        , NoAction()            ] ,
                 repeat=yes     };
    key <FK10> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F10
                    XF86Search                  , XF86Mail                      ,   // Search / Mail
                    XF86_Switch_VT_10           , Any                           ] , // Virtual Terminal 10
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I225>,clearmods=all), Redirect(key=<I223>,clearmods=all), // <I225>/<I223>(‡‡)
                    SwitchScreen(screen=10,!same)       , NoAction()            ] ,
                 repeat=yes     };
    key <FK11> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F11
                    XF86Explorer                , XF86Launch1                   ,   // Explorer / App1
                    XF86_Switch_VT_11           , XF86LogGrabInfo               ] , // Virtual Terminal 11 / Debug
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),   // (in inet, XF86Launch3 is <I210>)
                    Redirect(key=<I152>,clearmods=all), Redirect(key=<I156>,clearmods=all), // <I152>/<I156>(‡‡)
                    SwitchScreen(screen=11,!same)       , NoAction()            ] ,
                 repeat=yes     };
    key <FK12> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F12
                    XF86Calculator              , XF86Launch2                   ,   // Calculator / App2
                    XF86_Switch_VT_12           , XF86LogWindowTree             ] , // Virtual Terminal 12 / Debug
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),   // (in inet, XF86Launch4 is <I211>)
                    Redirect(key=<I148>,clearmods=all), Redirect(key=<I157>,clearmods=all), // <I148>/<I157>(‡‡)
                    SwitchScreen(screen=12,!same)       , NoAction()            ] ,
                 repeat=yes     };
};

partial alphanumeric_keys modifier_keys
xkb_symbols "lv5" {
    key.type[group1]="EIGHT_LEVEL_SEMIALPHABETIC"; // Lock affects lv1/5 but not lv3/7 - a bit inflexible but OK I guess

//  +=======+==============================+=======+
//  |=======|          Number Row          |=======|
//  +=======+==============================+=======+
//  NOTE: Earlier, if I set lv5-6 of Esc to Caps_Lock, lv1-4 would be CapsLock PLUS Esc! Is this fixed now?
//    - If not, I might not have to send the Caps_Lock keysymbol as long as the LockMods action is there?
    key <ESC>  { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Esc
                    Caps_Lock                   , Caps_Lock                     ,   // Caps_Lock
                    Caps_Lock                   , Caps_Lock                     ] ,
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    LockMods(mods=Lock)         , LockMods(mods=Lock)           ,   // Lock mod for <CAPS>
                    LockMods(mods=Lock)         , LockMods(mods=Lock)           ] ,
                 repeat=yes     };  // Esc
    key <TLDE> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Tilde/Grave (` ~)
                    Pointer_EnableKeys          , Pointer_EnableKeys            ,   // Ptr_On
                    Pointer_EnableKeys          , Pointer_EnableKeys            ] , // (Enable MouseKeys)
                 repeat=yes     };  // ` ~
    key <AE01> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 1
                    F1          , F1        , exclam        , exclam            ] , // F1   /   !
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK01>,clearmods=LevelFive), Redirect(key=<FK01>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 1
    key <AE02> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 2
                    F2          , F2        , at            , at                ] , // F2   /   @
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK02>,clearmods=LevelFive), Redirect(key=<FK02>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 2
    key <AE03> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 3
                    F3          , F3        , numbersign    , numbersign        ] , // F3   /   #
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK03>,clearmods=LevelFive), Redirect(key=<FK03>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 3
    key <AE04> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 4
                    F4          , F4        , dollar        , sterling          ] , // F4   /   $ £
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK04>,clearmods=LevelFive), Redirect(key=<FK04>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 4
    key <AE05> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 5
                    F5          , F5        , percent       , EuroSign          ] , // F5   /   % €
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK05>,clearmods=LevelFive), Redirect(key=<FK05>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 5
    key <AE06> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 6
                    F6          , F6        , asciicircum   , asciicircum       ] , // F6   /   ^
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK06>,clearmods=LevelFive), Redirect(key=<FK06>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 6
    key <AE07> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 7
                    F7          , F7        , KP_7          , U21D6             ] , // F7   /   KP_7 ↖ ⇖
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK07>,clearmods=LevelFive), Redirect(key=<FK07>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 7
    key <AE08> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 8
                    F8          , F8        , KP_8          , U21D1             ] , // F8   /   KP_8 ↑ ⇑
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK08>,clearmods=LevelFive), Redirect(key=<FK08>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 8
    key <AE09> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 9
                    F9          , F9        , KP_9          , U21D7             ] , // F9   /   KP_9 ↗ ⇗
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK09>,clearmods=LevelFive), Redirect(key=<FK09>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 9
    key <AE10> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // 0
                    F10         , F10       , KP_Multiply   , KP_Multiply       ] , // F10  /   KP_*
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK10>,clearmods=LevelFive), Redirect(key=<FK10>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // 0
    key <AE11> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // - _
                    F11         , F11       , KP_Subtract   , KP_Subtract       ] , // F11  /   KP_-
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK11>,clearmods=LevelFive), Redirect(key=<FK11>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // - _
    key <AE12> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // = +
                    F12         , F12       , KP_Equal      , KP_Equal          ] , // F12  /   KP_=
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<FK12>,clearmods=LevelFive), Redirect(key=<FK12>,clearmods=LevelFive),
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // = +

//  +=======+==============================+=======+
//  |=======|          Upper Row           |=======|
//  +=======+==============================+=======+
    key <AD01> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Q
                    Escape                      , Escape                        ,   // Esc
                    Home                        , KP_Home                       ] , // Home
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<ESC>,clearmods=LevelFive) , Redirect(key=<ESC>,clearmods=LevelFive)  ,    // <ESC>
                    Redirect(key=<HOME>,clearmods=LevelFive), Redirect(key=<HOME>,clearmods=LevelFive) ] ,  // <HOME>
                 repeat=yes     };  // Q
    key <AD02> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // W
                    XF86ScrollUp                , XF86ScrollUp                  ,   // ScrollUp(††)
                    Up                          , KP_Up                         ] , //  ↑
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    PtrBtn(button=4)            , PtrBtn(button=4)              ,   // PtrWhBtUp/Ri
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // W                // (ScrlR/L & Btn 6/7 may cause crashes now?!)
    key <AD03> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // E
                    XF86Back                    , XF86Back                      ,   // BrBck
                    End                         , KP_End                        ] , // End
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I166>,clearmods=LevelFive), Redirect(key=<I166>,clearmods=LevelFive),     // <I166>(‡‡)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE E, Cmk F
    key <AD04> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // R
                    XF86Forward                 , XF86Forward                   ,   // BrFwd
                    Delete                      , KP_Delete                     ] , // Del
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I167>,clearmods=LevelFive), Redirect(key=<I167>,clearmods=LevelFive),     // <I167>(‡‡)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE R, Cmk P
    key <AD05> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // T
                    Pointer_Up                  , Pointer_Up                    ,   // PtrUp(††)
                    Escape                      , Escape                        ] , // Esc
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    MovePtr(x=+0,y=-8)   , MovePtr(x=+0,y=-8)                   ,   // PtrUp(††)
                    Redirect(key=<ESC>,clearmods=LevelFive) , Redirect(key=<ESC>,clearmods=LevelFive) ] ,   // <ESC>
                 repeat=yes     };  // QWE T, Cmk G
    key <AD06> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Y
                    Page_Up                     , Page_Up                       ,   // PgUp
                    Page_Up                     , KP_Prior                      ] , // PgUp   / KP_PgUp
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<PGUP>,clearmods=LevelFive), Redirect(key=<PGUP>,clearmods=LevelFive),     // <PGUP>
                    Redirect(key=<PGUP>,clearmods=LevelFive), NoAction()        ] ,
                 repeat=yes     };  // QWE Y, Cmk J
    key <AD07> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // U
                    Home                        , Home                          ,   // Home
                    KP_4                        , U21D0                         ] , // KP_4   /   ← ⇐
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<HOME>,clearmods=LevelFive), Redirect(key=<HOME>,clearmods=LevelFive),     // <HOME>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE U, Cmk L
    key <AD08> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // I
                    Up                          , Up                            ,   //  ↑
                    KP_5                        , U21D4                         ] , // KP_5   /   ↔ ⇔
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<UP>,clearmods=LevelFive)  , Redirect(key=<UP>,clearmods=LevelFive)  ,     // <UP>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE I, Cmk U
    key <AD09> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // O
                    End                         , End                           ,   // End
                    KP_6                        , U21D2                         ] , // KP_6   /   → ⇒
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<END>,clearmods=LevelFive) , Redirect(key=<END>,clearmods=LevelFive) ,     // <END>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE O, Cmk Y
    key <AD10> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // P
                    Delete                      , Delete                        ,   // Del
                    KP_Add                      , KP_Add                        ] , // KP_+
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<DELE>,clearmods=LevelFive), Redirect(key=<DELE>,clearmods=LevelFive),     // <DELE>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE P, Cmk ; :
    key <AD11> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // [ {
                    Escape                      , Escape                        ,   // Esc
                    parenleft                   , braceleft                     ] , //  (
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<ESC>,clearmods=LevelFive) , Redirect(key=<ESC>,clearmods=LevelFive) ,     // <ESC>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // [ {
    key <AD12> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // ] }
                    Insert                      , Insert                        ,   // Ins
                    parenright                  , braceright                    ] , //  )
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<INS>,clearmods=LevelFive) , Redirect(key=<INS>,clearmods=LevelFive) ,     // <INS>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // ] }
    key <BKSL> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // \ |
                    XF86Favorites               , XF86MyComputer                ,   // BrFav
                    colon                       , bar                           ] , //  :
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<I164>,clearmods=all), Redirect(key=<I165>,clearmods=all),     // <I164>/<I165>(‡‡)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // \ |

//  +=======+==============================+=======+
//  |=======|          Middle Row          |=======|
//  +=======+==============================+=======+
    key <AC01> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // A
                    Alt_L                       , Alt_L                         ,   // <LALT>
                    Left                        , KP_Left                       ] , //  ←
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    SetMods(mods=Alt)           , SetMods(mods=Alt)             ,   // Alt mod
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE A, Cmk A
    key <AC02> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // S
                    XF86ScrollDown              , XF86ScrollDown                ,   // ScrlDown(††)
                    Down                        , KP_Down                       ] , //  ↓
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    PtrBtn(button=5)            , PtrBtn(button=5)              ,   // PtrWhBtDn/Le
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE S, Cmk R     // (ScrlR/L & Btn 6/7 may cause crashes now?!)
    key <AC03> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // D
                    Shift_L                     , Shift_L                       ,   // <LFSH>
                    Right                       , KP_Right             ], //  →
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    SetMods(mods=Shift)         , SetMods(mods=Shift)           ,   // Shift mod
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE D, Cmk S
    key <AC04> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // F
                    Control_L                   , Control_L                     ,   // <LCTL>
                    BackSpace                   , BackSpace                     ] , // Back
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    SetMods(mods=Control)       , SetMods(mods=Shift+Control)   ,   // Ctrl mod
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE F, Cmk T
    key <AC05> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // G
                    Pointer_Down                , Pointer_Down                  ,   // PtrDn(††)
                    Num_Lock                    , Num_Lock                      ] , // NumLock
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    MovePtr(x=+0,y=+8)          , MovePtr(x=+0,y=+8)            ,   // PtrDn(††)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE G, Cmk D
    key <AC06> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // H
                    Page_Down                   , Page_Down                     ,   // PgDn
                    Page_Down                   , KP_Next                       ] , // PgDn   / KP_PgDn
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<PGDN>,clearmods=LevelFive), Redirect(key=<PGDN>,clearmods=LevelFive),     // <PGDN>
                    Redirect(key=<PGDN>,clearmods=LevelFive), NoAction()        ] ,
                 repeat=yes     };  // H
    key <AC07> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // J
                    Left                        , Left                          ,   //  ←
                    KP_1                        , U21D9                         ] , // KP_1   /   ↙ ⇙
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<LEFT>,clearmods=LevelFive), Redirect(key=<LEFT>,clearmods=LevelFive),     // <LEFT>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE J, Cmk N
    key <AC08> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // K
                    Down                        , Down                          ,   //  ↓
                    KP_2                        , U21D3                         ] , // KP_2   /   ↓ ⇓
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<DOWN>,clearmods=LevelFive), Redirect(key=<DOWN>,clearmods=LevelFive),     // <DOWN>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE K, Cmk E
    key <AC09> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // L
                    Right                       , Right                         ,   //  →
                    KP_3                        , U21D8                         ] , // KP_3   /   ↘ ⇘
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<RGHT>,clearmods=LevelFive), Redirect(key=<RGHT>,clearmods=LevelFive),     // <RGHT>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE L, Cmk I
    key <AC10> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // ; :
                    BackSpace                   , BackSpace                     ,   // Back
                    KP_Enter                    , KP_Enter                      ] , // KP_Enter
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<BKSP>,clearmods=LevelFive), Redirect(key=<BKSP>,clearmods=LevelFive),     // <BKSP>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE ;, Cmk O
    key <AC11> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // ' "
                    Menu                        , Menu                          ,   // Menu
                    apostrophe                  , quotedbl                      ] , // ' "
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<COMP>,clearmods=LevelFive), Redirect(key=<COMP>,clearmods=LevelFive), //  <COMP>/<MENU>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // ' "

//  +=======+==============================+=======+
//  |=======|          Lower Row           |=======|
//  +=======+==============================+=======+
    key <LSGT> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // < >
                    Delete                      , Delete                        ,   // User's choice (Del for now)
                    Delete                      , Delete                        ] , // --"--
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    Redirect(key=<DELE>,clearmods=LevelFive), Redirect(key=<DELE>,clearmods=LevelFive),     // <DELE>
                    Redirect(key=<DELE>,clearmods=LevelFive), Redirect(key=<DELE>)          ] ,
                 repeat=yes     };  // < >
    key <AB01> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Z
                    Undo                        , Redo                          ,   // Undo/Redo
                    Undo                        , Redo                          ] , // Undo/Redo
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
      Redirect(key=<AB01>,mods=Control,clearmods=LevelFive), Redirect(key=<AB01>,mods=Shift+Control,clearmods=LevelFive),  // Ctrl+Z
      Redirect(key=<AB01>,mods=Control,clearmods=LevelFive), Redirect(key=<AB01>,mods=Shift+Control,clearmods=LevelFive)], // Ctrl+Z
//                    Redirect(key=<UNDO>)        , Redirect(key=<UNDO>) ]        ,   // <UNDO>(‡‡) (not working?)
                 repeat=yes     };  // Z
    key <AB02> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // X
                    XF86Cut                     , XF86Cut                       ,   // Cut(§§)
                    XF86Cut                     , XF86Cut                       ] , // Cut(§§)
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
      Redirect(key=<AB02>,mods=Control,clearmods=LevelFive), Redirect(key=<AB02>,mods=Shift+Control,clearmods=LevelFive),  // Ctrl+X
      Redirect(key=<AB02>,mods=Control,clearmods=LevelFive), Redirect(key=<AB02>,mods=Shift+Control,clearmods=LevelFive)], // Ctrl+X
//                    Redirect(key=<CUT>)         , Redirect(key=<CUT>)           ] , // <CUT>(‡‡) (not working since Ub14.04?)
                 repeat=yes     };  // X
    key <AB03> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // C
                    XF86Copy                    , XF86Copy                      ,   // Copy(§§)
                    XF86Copy                    , XF86Copy                      ] , // Copy(§§)
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
      Redirect(key=<AB03>,mods=Control,clearmods=LevelFive), Redirect(key=<AB03>,mods=Shift+Control,clearmods=LevelFive),  // Ctrl+C
      Redirect(key=<AB03>,mods=Control,clearmods=LevelFive), Redirect(key=<AB03>,mods=Shift+Control,clearmods=LevelFive)], // Ctrl+C
//                    Redirect(key=<COPY>)        , Redirect(key=<COPY>)          ] , // <COPY>(‡‡) (not working since Ub14.04?)
                 repeat=yes     };  // C
    key <AB04> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // V
                    XF86Paste                   , XF86Paste                     ,   // Paste(§§)
                    XF86Paste                   , XF86Paste                     ] , // Paste(§§)
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
      Redirect(key=<AB04>,mods=Control,clearmods=LevelFive), Redirect(key=<AB04>,mods=Shift+Control,clearmods=LevelFive),  // Ctrl+V
      Redirect(key=<AB04>,mods=Control,clearmods=LevelFive), Redirect(key=<AB04>,mods=Shift+Control,clearmods=LevelFive)], // Ctrl+V
//                    Redirect(key=<PAST>)        , Redirect(key=<PAST>)          ] , // <PAST>(‡‡) (not working since Ub14.04?)
                 repeat=yes     };  // V
    key <AB05> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // B
                    Pointer_Button1             , Pointer_Button1               ,   // LBt(††)
                    Pointer_Button1             , Pointer_Button1               ] , // LBt
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    PtrBtn(button=1)            , PtrBtn(button=1)              ,   // PtrLBt
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // B
    key <AB06> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // N
                    Pointer_Button2             , Pointer_Button2               ,   // MBt(††)
                    comma                       , semicolon                     ] , //  , ;
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    PtrBtn(button=2)            , PtrBtn(button=2)              ,   // PtrMBt
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // QWE N, Cmk K
    key <AB07> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // M
                    Pointer_Button3             , Pointer_Button3               ,   // RBt(††)
                    KP_0                        , U21D5                         ] , // KP_0   /   ↕ ⇕
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    PtrBtn(button=3)            , PtrBtn(button=3)              ,   // PtrRBt
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // M
    key <AB08> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // , <
                    Pointer_Left                , Pointer_Left                  ,   // PtrLe(††)
                    KP_0                        , U21D5                         ] , // KP_0   /   ↕ ⇕
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    MovePtr(x=-8,y=+0)          , MovePtr(x=-8,y=+0)            ,   // PtrLe(††)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // , <
    key <AB09> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // . >
                    Pointer_Right               , Pointer_Right                 ,   // PtrRi(††)
                    KP_Decimal                  , colon                         ] , // KP_. :
                 actions[Group1]=[NoAction(), NoAction(), NoAction(), NoAction(),
                    MovePtr(x=+8,y=+0)          , MovePtr(x=+8,y=+0)            ,   // PtrRi(††)
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // . >
    key <AB10> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // / ?
                    Multi_key                   , Multi_key                     ,   // Compose
                    KP_Divide                   , KP_Divide                     ] , // KP_/
                 repeat=yes     };  // / ?

//  NOTE: The Space key should preserve the Shift state; it's special because both levels have the same symbol.
//    - One might use a key type that has the 'preserve[Shift] = Shift;' property. However, I fix it directly.
    key <SPCE> { symbols[Group1]=[Any       , Any       , Any       , Any       ,   // Space
                    Return                      , Return                        ,   // Enter
                    space                       , nobreakspace                  ] , // Space
                 actions[Group1]=[NoAction(),SetMods(mods=Shift),NoAction(),SetMods(mods=Shift),
                    Redirect(key=<RTRN>,clearmods=LevelFive), Redirect(key=<RTRN>,clearmods=LevelFive),     // <RTRN>
                    NoAction()                  , NoAction()                    ] ,
                 repeat=yes     };  // Space

//  §§: XCV Cut/Copy/Paste keys seem OK in Firefox but not Gedit!? This seems to be a GTK weakness.
//    - Some bind fiddling in ~/.gtkrc-2.0 and ~/.config/gtk-3.0/gtk.css can get this working
//  ††: 'Pointer_'mousing needs enabling! Real KP_ keys work but not the Pointer_ mousing symbols
//    (In the Settings GUI there's a Shift+NumLock option for this, but Pointer_EnableKeys works too)
//  ‡‡: Multimedia keys are in symbols/inet(evdev); this should be enabled (base rules usually handle it)
//  NOTE: RedirectKey alone doesn't work for the #->FK keys; they then act as the remapped FK## (Multimedia)

//  NOTE: I'm not mapping the CAPS key to lv5_switch here, as that should be done separately.
//    - Leave it to the layout/options to decide which key(s) to use as switch(es).
//  key <CAPS> { [ ISO_Level5_Shift , BackSpace , Caps_Lock , Caps_Lock ] };
//  modifier_map Mod3   { ISO_Level5_Shift };
//    include "level3(ralt_switch)"
//    include "level5(caps_switch)"
};

//  NOTE: Alt worked with just the symbol, Shift/Ctrl not. Shift/Control are "real" mods; Alt a "virtual" one.
//    - Adding SetMods() actions to the Shift/Ctrl keys, they now work as they should.

//  NOTE: Lv5+S+T+N (Shift+Ctrl+Left) is too much at once; letting go of S but not T lets the arrows work.
//    - Using one physical modifier and the other Extend mapped (Lv5+S+<LCTL>+N) functions as expected!
//    - It's the same for Lv5+A+T+<PGDN> for tab switching; works only with physical either Ctrl or Alt.
