Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unix (bash; for X envs) script #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ The AHK script's creation is automated with a corresponding Python script.
This process can also be useful for text that is entered through
other switched keyboard layouts, such as Cyrillic or Hebrew.
Contributions through pull requests are welcomed!

## Unix

In the [unix/] directory there's a similar in operation bash script for
X server environments. The script depends on `xsel` and optionally `xdotool`.
If `xdotool` isn't in `$PATH`, the XA_PRIMARY (selection text) will be read,
and the result will be saved in XA_CLIPBOARD requiring ^v to replace text.
If `xdotool` is found, ^c will be issued, then XA_CLIPBOARD will
be modified with new text and ^v will be issued at the end.
Rather `xsel`, `xclip` can be used instead.
Then at the tool's call modify argument `-b` to `--selection clipboard`,
if there is `xdotool` or append `--selection primary` if there isn't.
For Wayland environments, `xdotool` can be replaced with `ydotool`
(environment independent) and for clipboard `wl-clipboard` can be used.
94 changes: 94 additions & 0 deletions unix/greek-latin-fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
#
# Copyright 2020 Diomidis Spinellis
# Copyright 2021 Konstantinos Foutzopoulos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Generate an Bash script for correcting Greek or Latin text
entered using the wrong keyboard layout.
Depends on:
`xsel` for clipboard manipulation
(optional) `xdotool` for key inputs
"""


def single_key_replace(source, target):
"""Create a set of replacements for single characters"""
for i in range(0, len(source)):
print(f' str="${{str//{source[i]}/{target[i]}}}"')


def dead_key_replace(dead, source, target):
"""Create a set of replacements for the specified dead key characters"""
for i in range(0, len(source)):
print(f' str="${{str//{dead}{source[i]}/{target[i]}}}"')


def main():
"""
Generate the Bash script on the standard output
"""
# Include `sleep` else `xdotool` gets stuck
print("""#!/usr/bin/env bash
# Ctrl-Alt-G: Fix Greek text entered on Latin keyboard setting
# Auto-generated by greek-latin-fix.py
if ! type xsel; then
notify-send "greek-latin-fix failed" "missing xsel"
exit 1
fi
if type xdotool; then
sleep 0.2
xdotool key ctrl+c
str="$(xsel -b -o)"
else
str="$(xsel -o)"
fi
if [[ "$str" =~ ^[A-Za-z:\;\ ]+$ ]]; then""")

latin_no_shift = 'abcdefghijklmnopqrstuvwxyz'
greek_no_shift = 'αβψδεφγηιξκλμνοπ;ρστθωςχυζ'

latin_shift = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
greek_shift = 'ΑΒΨΔΕΦΓΗΙΞΚΛΜΝΟΠ;ΡΣΤΘΩΣΧΥΖ'

# Replace English with Greek
dead_key_replace(";", 'aehioyv', 'άέήίόύώ')
dead_key_replace(";", 'AEHIOYV', 'ΆΈΉΊΌΎΏ')
dead_key_replace(":", 'iy', 'ϊϋ')
dead_key_replace(":", 'IY', 'ΪΫ')

single_key_replace(latin_no_shift, greek_no_shift)
single_key_replace(latin_shift, greek_shift)

print("else")
# Replace Greek with English

single_key_replace(greek_no_shift, latin_no_shift)
single_key_replace(greek_shift, latin_shift)

# `xdotool` can type ascii text but utf-8 requires escaping
print("""\
fi
xsel -b -i <<< "$str"
if type xdotool; then
sleep 0.2
xdotool key ctrl+v
fi
""")


if __name__ == "__main__":
main()
145 changes: 145 additions & 0 deletions unix/greek-latin-fix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env bash
# Ctrl-Alt-G: Fix Greek text entered on Latin keyboard setting
# Auto-generated by greek-latin-fix.py
if ! type xsel; then
notify-send "greek-latin-fix failed" "missing xsel"
exit 1
fi
if type xdotool; then
sleep 0.2
xdotool key ctrl+c
str="$(xsel -b -o)"
else
str="$(xsel -o)"
fi
if [[ "$str" =~ ^[A-Za-z:\;\ ]+$ ]]; then
str="${str//;a/ά}"
str="${str//;e/έ}"
str="${str//;h/ή}"
str="${str//;i/ί}"
str="${str//;o/ό}"
str="${str//;y/ύ}"
str="${str//;v/ώ}"
str="${str//;A/Ά}"
str="${str//;E/Έ}"
str="${str//;H/Ή}"
str="${str//;I/Ί}"
str="${str//;O/Ό}"
str="${str//;Y/Ύ}"
str="${str//;V/Ώ}"
str="${str//:i/ϊ}"
str="${str//:y/ϋ}"
str="${str//:I/Ϊ}"
str="${str//:Y/Ϋ}"
str="${str//a/α}"
str="${str//b/β}"
str="${str//c/ψ}"
str="${str//d/δ}"
str="${str//e/ε}"
str="${str//f/φ}"
str="${str//g/γ}"
str="${str//h/η}"
str="${str//i/ι}"
str="${str//j/ξ}"
str="${str//k/κ}"
str="${str//l/λ}"
str="${str//m/μ}"
str="${str//n/ν}"
str="${str//o/ο}"
str="${str//p/π}"
str="${str//q/;}"
str="${str//r/ρ}"
str="${str//s/σ}"
str="${str//t/τ}"
str="${str//u/θ}"
str="${str//v/ω}"
str="${str//w/ς}"
str="${str//x/χ}"
str="${str//y/υ}"
str="${str//z/ζ}"
str="${str//A/Α}"
str="${str//B/Β}"
str="${str//C/Ψ}"
str="${str//D/Δ}"
str="${str//E/Ε}"
str="${str//F/Φ}"
str="${str//G/Γ}"
str="${str//H/Η}"
str="${str//I/Ι}"
str="${str//J/Ξ}"
str="${str//K/Κ}"
str="${str//L/Λ}"
str="${str//M/Μ}"
str="${str//N/Ν}"
str="${str//O/Ο}"
str="${str//P/Π}"
str="${str//Q/;}"
str="${str//R/Ρ}"
str="${str//S/Σ}"
str="${str//T/Τ}"
str="${str//U/Θ}"
str="${str//V/Ω}"
str="${str//W/Σ}"
str="${str//X/Χ}"
str="${str//Y/Υ}"
str="${str//Z/Ζ}"
else
str="${str//α/a}"
str="${str//β/b}"
str="${str//ψ/c}"
str="${str//δ/d}"
str="${str//ε/e}"
str="${str//φ/f}"
str="${str//γ/g}"
str="${str//η/h}"
str="${str//ι/i}"
str="${str//ξ/j}"
str="${str//κ/k}"
str="${str//λ/l}"
str="${str//μ/m}"
str="${str//ν/n}"
str="${str//ο/o}"
str="${str//π/p}"
str="${str//;/q}"
str="${str//ρ/r}"
str="${str//σ/s}"
str="${str//τ/t}"
str="${str//θ/u}"
str="${str//ω/v}"
str="${str//ς/w}"
str="${str//χ/x}"
str="${str//υ/y}"
str="${str//ζ/z}"
str="${str//Α/A}"
str="${str//Β/B}"
str="${str//Ψ/C}"
str="${str//Δ/D}"
str="${str//Ε/E}"
str="${str//Φ/F}"
str="${str//Γ/G}"
str="${str//Η/H}"
str="${str//Ι/I}"
str="${str//Ξ/J}"
str="${str//Κ/K}"
str="${str//Λ/L}"
str="${str//Μ/M}"
str="${str//Ν/N}"
str="${str//Ο/O}"
str="${str//Π/P}"
str="${str//;/Q}"
str="${str//Ρ/R}"
str="${str//Σ/S}"
str="${str//Τ/T}"
str="${str//Θ/U}"
str="${str//Ω/V}"
str="${str//Σ/W}"
str="${str//Χ/X}"
str="${str//Υ/Y}"
str="${str//Ζ/Z}"
fi
xsel -b -i <<< "$str"
if type xdotool; then
sleep 0.2
xdotool key ctrl+v
fi