Skip to content

Commit 92e90a1

Browse files
dctuckerchrisbra
authored andcommitted
patch 9.1.0058: Cannot map Super Keys in GTK UI
Problem: Cannot map Super Keys in GTK UI (Casey Tucker) Solution: Enable Super Key mappings in GTK using <D-Key> (Casey Tucker) As a developer who works in both Mac and Linux using the same keyboard, it can be frustrating having to remember different key combinations or having to rely on system utilities to remap keys. This change allows `<D-z>` `<D-x>` `<D-c>` `<D-v>` etc. to be recognized by the `map` commands, along with the `<D-S-...>` shifted variants. ```vimrc if has('gui_gtk') nnoremap <D-z> u nnoremap <D-S-Z> <C-r> vnoremap <D-x> "+d vnoremap <D-c> "+y cnoremap <D-v> <C-R>+ inoremap <D-v> <C-o>"+gP nnoremap <D-v> "+P vnoremap <D-v> "-d"+P nnoremap <D-s> :w<CR> inoremap <D-s> <C-o>:w<CR> nnoremap <D-w> :q<CR> nnoremap <D-q> :qa<CR> nnoremap <D-t> :tabe<CR> nnoremap <D-S-T> :vs#<CR><C-w>T nnoremap <D-a> ggVG vnoremap <D-a> <ESC>ggVG inoremap <D-a> <ESC>ggVG nnoremap <D-f> / nnoremap <D-g> n nnoremap <D-S-G> N vnoremap <D-x> "+x endif ``` closes: #12698 Signed-off-by: Casey Tucker <dctucker@hotmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 6a02eb0 commit 92e90a1

11 files changed

Lines changed: 64 additions & 19 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Jan 23
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Jan 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3470,7 +3470,7 @@ getcharmod() *getcharmod()*
34703470
32 mouse double click
34713471
64 mouse triple click
34723472
96 mouse quadruple click (== 32 + 64)
3473-
128 command (Macintosh only)
3473+
128 command (Mac) or super (GTK)
34743474
Only the modifiers that have not been included in the
34753475
character itself are obtained. Thus Shift-a results in "A"
34763476
without a modifier. Returns 0 if no modifiers are used.

runtime/doc/intro.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*intro.txt* For Vim version 9.1. Last change: 2023 Nov 18
1+
*intro.txt* For Vim version 9.1. Last change: 2024 Jan 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -475,7 +475,7 @@ notation meaning equivalent decimal value(s) ~
475475
<C-...> control-key *control* *ctrl* *<C-*
476476
<M-...> alt-key or meta-key *meta* *alt* *<M-*
477477
<A-...> same as <M-...> *<A-*
478-
<D-...> command-key (Macintosh only) *<D-*
478+
<D-...> command-key (Mac) / super (GTK) *<D-*
479479
<t_xx> key with "xx" entry in termcap
480480
-----------------------------------------------------------------------
481481

runtime/doc/map.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*map.txt* For Vim version 9.1. Last change: 2024 Jan 04
1+
*map.txt* For Vim version 9.1. Last change: 2024 Jan 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -21,9 +21,10 @@ manual.
2121
1.9 Using mappings |map-typing|
2222
1.10 Mapping alt-keys |:map-alt-keys|
2323
1.11 Mapping meta-keys |:map-meta-keys|
24-
1.12 Mapping in modifyOtherKeys mode |modifyOtherKeys|
25-
1.13 Mapping with Kitty keyboard protocol |kitty-keyboard-protocol|
26-
1.14 Mapping an operator |:map-operator|
24+
1.12 Mapping super-keys or command keys |:map-super-keys|
25+
1.13 Mapping in modifyOtherKeys mode |modifyOtherKeys|
26+
1.14 Mapping with Kitty keyboard protocol |kitty-keyboard-protocol|
27+
1.15 Mapping an operator |:map-operator|
2728
2. Abbreviations |abbreviations|
2829
3. Local mappings and functions |script-local|
2930
4. User-defined commands |user-commands|
@@ -985,8 +986,17 @@ For the Meta modifier the "T" character is used. For example, to map Meta-b
985986
in Insert mode: >
986987
:imap <T-b> terrible
987988
989+
1.12 MAPPING SUPER-KEYS or COMMAND-KEYS *:map-super-keys* *:map-cmd-key*
988990

989-
1.12 MAPPING IN modifyOtherKeys mode *modifyOtherKeys*
991+
The Super modifier is available in GUI mode (when |gui_running| is 1) for
992+
GVim on Linux and MacVim on Mac OS. If you're on a Mac, this represents the
993+
Command key, on Linux with the GTK GUI it represents the Super key.
994+
The character "D" is used for the Super / Command modifier.
995+
996+
For example, to map Command-b in Insert mode: >
997+
:imap <D-b> barritone
998+
999+
1.13 MAPPING IN modifyOtherKeys mode *modifyOtherKeys*
9901000

9911001
Xterm and a few other terminals can be put in a mode where keys with modifiers
9921002
are sent with a special escape code. Vim recognizes these codes and can then
@@ -1048,7 +1058,7 @@ When the 'esckeys' option is off, then modifyOtherKeys will be disabled in
10481058
Insert mode to avoid every key with a modifier causing Insert mode to end.
10491059

10501060

1051-
1.13 MAPPING WITH KITTY KEYBOARD PROTOCOL *kitty-keyboard-protocol*
1061+
1.14 MAPPING WITH KITTY KEYBOARD PROTOCOL *kitty-keyboard-protocol*
10521062

10531063
If the value of 'term' contains "kitty" then Vim will send out an escape
10541064
sequence to enable the Kitty keyboard protocol. This can be changed with the
@@ -1075,7 +1085,7 @@ translated). The meaning of {value}:
10751085
previous state is unknown
10761086

10771087

1078-
1.14 MAPPING AN OPERATOR *:map-operator*
1088+
1.15 MAPPING AN OPERATOR *:map-operator*
10791089

10801090
An operator is used before a {motion} command. To define your own operator
10811091
you must create a mapping that first sets the 'operatorfunc' option and then

runtime/doc/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,7 @@ $quote eval.txt /*$quote*
28732873
:map-arguments map.txt /*:map-arguments*
28742874
:map-buffer map.txt /*:map-buffer*
28752875
:map-cmd map.txt /*:map-cmd*
2876+
:map-cmd-key map.txt /*:map-cmd-key*
28762877
:map-commands map.txt /*:map-commands*
28772878
:map-expression map.txt /*:map-expression*
28782879
:map-local map.txt /*:map-local*
@@ -2885,6 +2886,7 @@ $quote eval.txt /*$quote*
28852886
:map-special map.txt /*:map-special*
28862887
:map-special-chars map.txt /*:map-special-chars*
28872888
:map-special-keys map.txt /*:map-special-keys*
2889+
:map-super-keys map.txt /*:map-super-keys*
28882890
:map-undo map.txt /*:map-undo*
28892891
:map-unique map.txt /*:map-unique*
28902892
:map-verbose map.txt /*:map-verbose*

src/edit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ insert_special(
20232023
* Only use mod_mask for special keys, to avoid things like <S-Space>,
20242024
* unless 'allow_modmask' is TRUE.
20252025
*/
2026-
#ifdef MACOS_X
2026+
#if defined(MACOS_X) || defined(FEAT_GUI_GTK)
20272027
// Command-key never produces a normal key
20282028
if (mod_mask & MOD_MASK_CMD)
20292029
allow_modmask = TRUE;

src/gui_gtk_x11.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,14 @@ modifiers_gdk2vim(guint state)
11191119
if (state & GDK_MOD1_MASK)
11201120
modifiers |= MOD_MASK_ALT;
11211121
#if GTK_CHECK_VERSION(2,10,0)
1122-
if (state & GDK_SUPER_MASK)
1122+
if (state & GDK_META_MASK)
11231123
modifiers |= MOD_MASK_META;
1124-
#endif
1124+
if (state & GDK_SUPER_MASK)
1125+
modifiers |= MOD_MASK_CMD;
1126+
#else
11251127
if (state & GDK_MOD4_MASK)
1126-
modifiers |= MOD_MASK_META;
1128+
modifiers |= MOD_MASK_CMD;
1129+
#endif
11271130

11281131
return modifiers;
11291132
}

src/gui_xim.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,9 @@ xim_reset(void)
10631063
int
10641064
xim_queue_key_press_event(GdkEventKey *event, int down)
10651065
{
1066+
#ifdef FEAT_GUI_GTK
1067+
if (event->state & GDK_SUPER_MASK) return FALSE;
1068+
#endif
10661069
if (down)
10671070
{
10681071
// Workaround GTK2 XIM 'feature' that always converts keypad keys to

src/keymap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ enum key_extra
500500
#define MOD_MASK_2CLICK 0x20 // use MOD_MASK_MULTI_CLICK
501501
#define MOD_MASK_3CLICK 0x40 // use MOD_MASK_MULTI_CLICK
502502
#define MOD_MASK_4CLICK 0x60 // use MOD_MASK_MULTI_CLICK
503-
#ifdef MACOS_X
504-
# define MOD_MASK_CMD 0x80
503+
#if defined(MACOS_X) || defined(FEAT_GUI_GTK)
504+
# define MOD_MASK_CMD 0x80 // aka SUPER
505505
#endif
506506

507507
#define MOD_MASK_MULTI_CLICK (MOD_MASK_2CLICK|MOD_MASK_3CLICK|MOD_MASK_4CLICK)

src/misc2.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ static struct modmasktable
817817
{MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
818818
{MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
819819
{MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
820-
#ifdef MACOS_X
820+
#if defined(MACOS_X) || defined(FEAT_GUI_GTK)
821821
{MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
822822
#endif
823823
// 'A' must be the last one
@@ -1130,7 +1130,11 @@ simplify_key(int key, int *modifiers)
11301130
int key0;
11311131
int key1;
11321132

1133-
if (!(*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)))
1133+
if (!(*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
1134+
#ifdef FEAT_GUI_GTK
1135+
| MOD_MASK_CMD
1136+
#endif
1137+
)))
11341138
return key;
11351139

11361140
// TAB is a special case
@@ -1582,6 +1586,9 @@ may_remove_shift_modifier(int modifiers, int key)
15821586
{
15831587
if ((modifiers == MOD_MASK_SHIFT
15841588
|| modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1589+
#ifdef FEAT_GUI_GTK
1590+
|| modifiers == (MOD_MASK_SHIFT | MOD_MASK_CMD)
1591+
#endif
15851592
|| modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
15861593
&& ((key >= '!' && key <= '/')
15871594
|| (key >= ':' && key <= 'Z')

src/testdir/test_mapping.vim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,24 @@ func Test_map_meta_multibyte()
247247
iunmap <M-á>
248248
endfunc
249249

250+
func Test_map_super_quotes()
251+
if has('gui_gtk') || has('gui_gtk3') || has("macos")
252+
imap <D-"> foo
253+
call feedkeys("Go-\<*D-\">-\<Esc>", "xt")
254+
call assert_equal("-foo-", getline('$'))
255+
set nomodified
256+
iunmap <D-">
257+
endif
258+
endfunc
259+
260+
func Test_map_super_multibyte()
261+
if has('gui_gtk') || has('gui_gtk3') || has("macos")
262+
imap <D-á> foo
263+
call assert_match('i <D-á>\s*foo', execute('imap'))
264+
iunmap <D-á>
265+
endif
266+
endfunc
267+
250268
func Test_abbr_after_line_join()
251269
new
252270
abbr foo bar

0 commit comments

Comments
 (0)