Changing text direction and GNOME

 So you are writing RTL text (Hebrew, Arabic, Farsi ..) and sometimes you tend to mix some English words in it. In Windows and QT (after enabling in qtconfig) you have to press both Ctrl and Shift keys (both left or right) if you want to change direction for a single line/paragraph/selected text.

In GNOME you have the fabulous Auto Detection - the direction of your paragraph is set by the first character having strong direction. That's cool and works most of the time but what are you supposed to do when it doesn't work. Have no fear - the amazing RLM and LRM characters come to the rescue. RLM and LRM possess superpowers such as strong direction, invisibility and the ability to claim you're following the standards. All you have to do to use these is simply hit the Home key then Right-click to get the context menu then select "Insert unicode control character" (or simply change your national keyboard layout standard to include RLM/LRM) then you have to pray to Alohim/Allah that you will not change your mind later on and be forced to look for it and try to delete it.

I say we can make editing bidi-text easier in GNOME by creating several text actions bound to keyboard shortcuts to handle it. Here are the few actions I had in mind:

  1. A shortcut to change current line/selected text direction, just like windows Ctrl+shift does but insert RLM/LRM when needed.
  2. A shortcut to delete all control characters in the selected text - To be used when you made a big mess in your text and you want to fix it manually all over again.
  3. A shortcut to encapsulate selected text within RLE/LRE and PDF (Pop Directional formatting).
  4. A shortcut to encapsulate selected text whithin RLO/LRO and PDF.
This will probably have to be implemented by a patch to GNOME (GtkEntry and GtkTextView) adding some signals to preform the actions and binding them to some keyboard shortcuts. I don't think there's a special low-level API that will allow implementation of such actions. But I did look for such.

I found a very limited way in which GNOME's Input Method API can provide basic line direction switcher (No. 1 in the above list) and decided, for the sake of pure experiment to try implementing it. I think it's a ugly hack but it works well and better than nothing for now... I guess. It's not perfect at all: it works only for single lines, It might delete selected text (works great when no text is selected) and it will insert two edit actions into the undo buffer so to undo a switch you will have to hit Ctrl+z twice.

The keyboard shortcut I chose for it is Ctrl+Shift+0 or Ctrl+Shift+9 which resembles the location of RLM/LRM in the new Israeli keyboard standard. Using Ctrl+Shift alone (like in QT and Windows did) is no good because I fear it might be used for layout group selection in some layouts.

The Debian packaging and some of the code is base on the awesome Debian package for gtk-im-libthai so I was able to code and package it in a very short time, relativly... for a project with no real future.

So far I tried it only on Ubuntu 11.10+GNOME3+i368/amd64. Seems to work:

If you would like to try it, it's available in my Ubuntu PPA, package name: gtk3-im-rtl (not 'gtk-im-rtl' which is for GNOME2 and seems to be broken). currently compiled only for Ubuntu Oneiric 11.10 . You may also try to download the source and compile for other distros.
But don't you come crying to me if it broke your desktop.
The Input Method is supposed to become default after installation. If not, you'll have to set it by yourself (right click->select input method).

Happy Hackinukkah!!!
.. and merry christmas!

1 comment:

Ofer Hadas said...

In Windows, in the standard Heberw keyboard that comes with the OS, ctrl+] and ctrl+[ represent RLM and LRM, respectively.
As a person that writes math within Hebrew text, I think I use these several hundred times a day on days I'm active on forums.

I think it's a pity that after so many years of computing we still don't have a standard method that allows writing rtk languages as plaintext (that is: correctly right align text, in addition to setting directionality on both paragraph and inline levels.