[texworks] Entering Unicode characters directly

Jonathan Kew jfkthame at googlemail.com
Sun Jun 6 19:51:24 CEST 2010


On 6 Jun 2010, at 17:57, Joel C. Salomon wrote:

> I am used to entering Unicode characters, e.g. "…" U+2026 Ellipsis, by
> hitting ctrl+shift+U then typing the Unicode value.  This works in
> almost all applications I use on my Ubuntu Linux box, but not in TeXworks.
> 
> Does anyone know why this is so, or how to work-around the problem?

Only a guess, but perhaps this may be a feature of applications using the GTK interface, whereas TW is built with Qt. (Does that input method work in KDE applications?)

> 
> (Can a script be written to imitate this functionality?)

I'd suggest writing a script that examines the text immediately before the cursor, and if it can be interpreted as a hexadecimal Unicode value, replaces it with the specified character. That shouldn't be hard. Something like this should get you started:

  var hexUSV = /^[0-9A-Fa-f]{4}$/;
  if (TW.target.selectionLength == 0) {
    var orig = TW.target.selectionStart;
    var start = orig - 4;
    if (start >= 0) {
      TW.target.selectRange(start, 4);
      var txt = TW.target.selection;
      if (hexUSV.test(txt)) {
        var usv = parseInt(txt,16);
        TW.target.insertText(String.fromCharCode(usv));
      } else {
        TW.target.selectRange(orig, 0);
      }
    }
  }

This looks at the 4 characters before the cursor, and converts them to a single Unicode character if they are all hex digits. Otherwise it does nothing. If you assign this to a shortcut such as Ctrl-U, then you can type a 4-digit code value and then hit Ctrl-U to convert it.

Left as an exercise for the reader: enhance this to support the full Unicode range up to U+10FFFF, remembering that you'll need to generate surrogate pairs for non-BMP codepoints.

Another possible enhancement: if there's a range of text selected, then search for all valid strings of hex digits within the selected text and convert them all.

(Code above is typed in Mail, so please excuse any typos/syntax errors.... corrections welcome!)

HTH,

JK




More information about the texworks mailing list