diff options
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r-- | lib/python/qmk/keymap.py | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index d8495c38b..4ad9ffb59 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py | |||
@@ -1,19 +1,19 @@ | |||
1 | """Functions that help you work with QMK keymaps. | 1 | """Functions that help you work with QMK keymaps. |
2 | """ | 2 | """ |
3 | from pathlib import Path | ||
4 | import json | 3 | import json |
5 | import subprocess | 4 | import subprocess |
6 | import sys | 5 | import sys |
6 | from pathlib import Path | ||
7 | 7 | ||
8 | import argcomplete | ||
9 | from milc import cli | ||
8 | from pygments.lexers.c_cpp import CLexer | 10 | from pygments.lexers.c_cpp import CLexer |
9 | from pygments.token import Token | 11 | from pygments.token import Token |
10 | from pygments import lex | 12 | from pygments import lex |
11 | 13 | ||
12 | from milc import cli | ||
13 | |||
14 | from qmk.keyboard import rules_mk | ||
15 | import qmk.path | 14 | import qmk.path |
16 | import qmk.commands | 15 | import qmk.commands |
16 | from qmk.keyboard import find_keyboard_from_dir, rules_mk | ||
17 | 17 | ||
18 | # The `keymap.c` template to use when a keyboard doesn't have its own | 18 | # The `keymap.c` template to use when a keyboard doesn't have its own |
19 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H | 19 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H |
@@ -74,6 +74,54 @@ def _strip_any(keycode): | |||
74 | return keycode | 74 | return keycode |
75 | 75 | ||
76 | 76 | ||
77 | def find_keymap_from_dir(): | ||
78 | """Returns `(keymap_name, source)` for the directory we're currently in. | ||
79 | |||
80 | """ | ||
81 | relative_cwd = qmk.path.under_qmk_firmware() | ||
82 | |||
83 | if relative_cwd and len(relative_cwd.parts) > 1: | ||
84 | # If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name. | ||
85 | if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts: | ||
86 | current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front | ||
87 | |||
88 | if 'keymaps' in current_path.parts and current_path.name != 'keymaps': | ||
89 | while current_path.parent.name != 'keymaps': | ||
90 | current_path = current_path.parent | ||
91 | |||
92 | return current_path.name, 'keymap_directory' | ||
93 | |||
94 | # If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in | ||
95 | elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd): | ||
96 | return relative_cwd.name, 'layouts_directory' | ||
97 | |||
98 | # If we're in `qmk_firmware/users` guess the name from the userspace they're in | ||
99 | elif relative_cwd.parts[0] == 'users': | ||
100 | # Guess the keymap name based on which userspace they're in | ||
101 | return relative_cwd.parts[1], 'users_directory' | ||
102 | |||
103 | return None, None | ||
104 | |||
105 | |||
106 | def keymap_completer(prefix, action, parser, parsed_args): | ||
107 | """Returns a list of keymaps for tab completion. | ||
108 | """ | ||
109 | try: | ||
110 | if parsed_args.keyboard: | ||
111 | return list_keymaps(parsed_args.keyboard) | ||
112 | |||
113 | keyboard = find_keyboard_from_dir() | ||
114 | |||
115 | if keyboard: | ||
116 | return list_keymaps(keyboard) | ||
117 | |||
118 | except Exception as e: | ||
119 | argcomplete.warn(f'Error: {e.__class__.__name__}: {str(e)}') | ||
120 | return [] | ||
121 | |||
122 | return [] | ||
123 | |||
124 | |||
77 | def is_keymap_dir(keymap, c=True, json=True, additional_files=None): | 125 | def is_keymap_dir(keymap, c=True, json=True, additional_files=None): |
78 | """Return True if Path object `keymap` has a keymap file inside. | 126 | """Return True if Path object `keymap` has a keymap file inside. |
79 | 127 | ||