Skip to content

Instantly share code, notes, and snippets.

@MahouShoujoMivutilde
Last active July 12, 2021 12:18
Show Gist options
  • Save MahouShoujoMivutilde/6a2dea53dc90a51b7566584ac7d3d283 to your computer and use it in GitHub Desktop.
Save MahouShoujoMivutilde/6a2dea53dc90a51b7566584ac7d3d283 to your computer and use it in GitHub Desktop.
lf-keys.py - is the script that allows you to translate latin lf mappings to your non-latin keyboard layout

USAGE

  1. In lf-keys.py edit translists variable according to your needs (see comments).

  2. Concatenate default mappings (goes first) with your mappings (second), and pipe the result into lf-keys.py. E.g. like this:

grep '^map' ~/.config/lf/lfrc | cat ~/.config/lf/lf-defaults - | lf-keys.py > ~/.config/lf-localized-maps
  1. Add source ~/.config/lf-localized-maps to the bottom of your lfrc
map k up
map <c-u> half-up
map <c-b> page-up
map j down
map <c-d> half-down
map <c-f> page-down
map h updir
map l open
map q quit
map gg top
map G bottom
map v invert
map u unselect
map y copy
map d cut
map p paste
map c clear
map <c-l> redraw
map <c-r> reload
map r rename
map f find
map F find-back
map ; find-next
map , find-prev
map / search
map ? search-back
map n search-next
map N search-prev
map m mark-save
map ' mark-load
map " mark-remove
map zh set hidden!
map zr set reverse!
map zn set info
map zs set info size
map zt set info time
map za set info size:time
map sn :set sortby natural; set info
map ss :set sortby size; set info size
map st :set sortby time; set info time
map sa :set sortby atime; set info atime
map sc :set sortby ctime; set info ctime
map se :set sortby ext; set info
map gh cd ~
map e $$EDITOR $f
map i $$PAGER $f
map w $$SHELL
#!/usr/bin/env python3
# lf-keys.py - is the script that allows you to translate latin lf mapping
# to your non-latin keyboard layout (to allow all mappings to work as expected
# regardless of your current layout).
# USAGE
# grep '^map' lfrc | cat lf-defaults - | lf-keys.py
# paste the output below all your mappings in lfrc
# NOTE you will need a file with deafult lf bindings (for hjkl etc...)
from sys import stdin
import re
# maketrans - creates dictionary to translate characters in first line (as keys)
# to characters in second (values)
translists = [
str.maketrans(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;,'.`",
"ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯфисвуапршолдьтщзйкыегмцчняжбэюё"
),
# e.g. above is latin >>> cyrillic
# this will allow lf to interpret Ф as A, ф as a, etc
# if you want to use more that one alternative layout -
# put another maketrans here
# e.g.
# str.maketrans(
# "l", # etc
# "م"
# )
]
def trans(s, translist, inv_translist):
s = s.translate(translist)
if re.search('<.-.>', s, flags=re.IGNORECASE):
# fix ctrl, alt, and f1...
for char in ['c', 'a', 'f']:
mp = f'<{char}-'
mpa = mp.translate(translist)
s = s.replace(mpa, mp)
if special := re.findall(r'<\w+>', s, re.IGNORECASE):
for w in special:
untrans = w.translate(inv_translist)
s = s.replace(w, untrans)
return s
if __name__ == '__main__':
lines = [line.strip() for line in stdin.readlines()]
# skip comments
lines = filter(lambda line:
line != '' and
not re.match(r'^\s*#', line),
lines)
lines = list(lines)
unmapped = set()
for line in lines:
words = line.split()
if len(words) == 2:
unmapped.add(words[1])
for translist in translists:
inv_translist = {v: k for k, v in translist.items()}
for line in lines:
words = line.split()
# don't translate unmapped defaults, <enter>...
keys = words[1]
if keys not in unmapped and not re.match(r'<\w+>', keys):
cmd = f'push {keys}'
new_keys = trans(keys, translist, inv_translist)
if new_keys == keys:
continue
print(' '.join(['map', f'"{new_keys}"', cmd]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment