aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/keymap.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r--lib/python/qmk/keymap.py56
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"""
3from pathlib import Path
4import json 3import json
5import subprocess 4import subprocess
6import sys 5import sys
6from pathlib import Path
7 7
8import argcomplete
9from milc import cli
8from pygments.lexers.c_cpp import CLexer 10from pygments.lexers.c_cpp import CLexer
9from pygments.token import Token 11from pygments.token import Token
10from pygments import lex 12from pygments import lex
11 13
12from milc import cli
13
14from qmk.keyboard import rules_mk
15import qmk.path 14import qmk.path
16import qmk.commands 15import qmk.commands
16from 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
19DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H 19DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
@@ -74,6 +74,54 @@ def _strip_any(keycode):
74 return keycode 74 return keycode
75 75
76 76
77def 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
106def 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
77def is_keymap_dir(keymap, c=True, json=True, additional_files=None): 125def 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