Skip to content

Script for correcting text typed in a wrong layout and stateless layout switching in X11 and GNOME

License

Notifications You must be signed in to change notification settings

roadkell/puny-switcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

In other languages: Русский

Puny Switcher

Script for correcting text typed in a wrong layout and stateless layout switching in X11 and GNOME.

Highlights

  • Correcting layout of a single word / line up to cursor / current selection
  • Stateless layout switching: separate hotkey for each layout
  • Clipboard content is preserved
  • No keylogging
  • No keyboard manipulation
  • No modifier watching
  • No key remapping
  • Has alternative function implementations for non-GNOME and non-systemd environments

Installation

  • Install GNOME extension for programmatical layout switching (repository).
  • Install xsel for X selection and clipboard manipulation (repository).
  • Install kanata (in particular, kanata_cmd_allowed version that allows command execution is required) or any other keyboard remapper with support for macros and external command execution.
  • Provide kanata access to input subsystem, as described here.
  • Download the script and save it into ~/.local/bin/. Examine its contents and modify if needed.
  • Permit its execution:
chmod +x ~/.local/bin/puny-switcher.sh
  • Download the kanata configuration file and save it into $XDG_CONFIG_HOME/kanata/ (usually ~/.config/kanata/). Examine and modify it if needed.
  • Download the systemd unit file and save it into $XDG_CONFIG_HOME/systemd/user/ (usually ~/.config/systemd/user/) to run kanata as a service.
  • Launch the service and check its status:
systemctl --user daemon-reload
systemctl --user enable --now kanata@puny-switcher.service
systemctl --user status kanata@puny-switcher.service

Configuration

Default keys, as defined in kanata configuration file:

  • tap left Shift: switch to first layout
  • tap right Shift: swtch to second layout
  • double-tap any Shift: convert a single word and switch layout
  • triple-tap any Shift: convert a line up to the cursor and switch layout
  • hold Shift: default Shift behaviour (after a timeout)
  • Shift+any key: default Shift behaviour (immediately)
  • Pause: convert a single word and switch layout
  • Сtrl+Pause: convert a line up to the cursor and switch layout
  • Shift+Pause: convert selection and switch layout

They can be redefined in defsrc section of the config. For example, you can use PrtSc key instead of Pause by replacing pause with sys. Available key names can be found in str_to_oscode and default_mappings functions in kanata source.

Commands

puny-switcher.sh command [LAYOUT]

commands:
	get					print list of available layouts, current layout & its characters
	set LAYOUT_NAME		switch layout by name (us|ru)
	iset LAYOUT_INDEX	switch layout by index (0|1)
	switch				switch to another layout
	convert				convert selected text and store result in clipboard
	restore				restore clipboard after conversion

Downsides

  • Depends on 3rd-party software
  • Doesn't work in terminal emulators yet
  • Currently limited to English-Russian layout pair
  • Doesn't account for layout variants (Dvorak, Typewriter, etc.) yet
  • Untested in non-GNOME / non-systemd environments

Alternatives

If you prefer an all-in-one solution for retyping text typed in a wrong layout, you can try xswitcher. Other similar projects like xneur have long been abandoned. Also, most of them don't work in GNOME due to its strict security policy.

On the other hand, if you already have a favourite keyboard remapper (or just prefer a modular approach), it is likely possible that it can be set up to work with Puny Switcher script. Here are some popular ones. KMonad, for example, has a config file format similar to kanata. Imporantly, it must support external command execution to be able to invoke puny-switcher.sh with arguments. If you manage to adapt the provided config to another remapper, I'll gladly include it into the project.

GNOME is complicated in regards to programmatical layout switching. g3kb-switch seems to be the only extension besides Shyriiwook that works in GNOME 41+ at the moment. If GNOME support is not required, there is no need for an extension — setxkbmap command is enough. Functions named *-nognome in the script are alternative implementations for non-GNOME environments.

xsel can be replaced with xclip for clipboard manipulation, if you prefer one over another. Just modify commands and their arguments in puny-switcher.sh to work with it.

Similarlly, gdbus command can be used instead of busctl, if needed: there are ready-made alternative function implementations in puny-switcher.sh for gdbus. Just modify the source to use them instead of default ones.

Important

Queer Svit is a black queer-run independent team of volunteers from all over the world. With your support we help LGBTQ+ and BAME people affected by the war and/or political repressions get to safety by providing consultations, purchasing tickets, finding free accommodation and providing humanitarian aid.

‌‌Just like any other catastrophe, war affects the most those who are most vulnerable, including LGBTQ+ and BAME people while at the same time their troubles are often rendered invisible. But because our own team comprises LGBTQ+ and BAME people we see the specific challenges our beneficiaries face and understand how to help them.

‌Your donations make a difference; Queer Svit is run in large part on small donations from individual donors. We are grateful for any and all help to continue our work — thank you!

License

Hippocratic License 3.0 (HL3-CORE)

About

Script for correcting text typed in a wrong layout and stateless layout switching in X11 and GNOME

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages