[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: elisp question:keyboard-translate with hyper fail?

From: Xah Lee
Subject: Re: elisp question:keyboard-translate with hyper fail?
Date: Fri, 18 Mar 2011 06:56:56 -0700 (PDT)
User-agent: G2/1.0

just wrote up a little blog on why i wanted to use key-translation in
this thread. Might be interesting.

〈Emacs: Remapping Keys Using key-translation-map〉

Emacs: Remapping Keys Using key-translation-map

Xah Lee, 2011-03-17

This page shows you how to remap keys to input unicode symbols using
the elisp function “key-translation-map”, and discuss some related

Recently i wrote a article on How to Create a APL or Math Symbols
Keyboard Layout. In the article, i described several ways to remap
keys on keyboard to input symbols for Mac, Windows, Linux. Some are OS-
wide thru OS's user-configurable mechanisms. Some are using key-macro
software for that OS. And there's emacs for just within emacs. I've
used them all in the past 2 decades, but in the past few years, i find
the most practical and flexible way is just emacs. Because: ① 99% of
my typing are done in emacs. So, i don't really need them to be OS-
wide. ② Each method of remapping keys all have their own limitations
(e.g. on Mac and Windows, there's no way to remap some key
combinations.), but within emacs it has the most support on remapping
keys that has less restrictions than using OS's methods.

Here's interesting thing i discovered recently.

Using “global-set-key” or “key-translation-map” to Insert Unicode Char

If you want to define a key combo for typing a unicode char such as
math symbol λ, you can do it using “global-set-key” or “key-

Example of using “global-set-key”:

(global-set-key (kbd "H-3") (lambda () (interactive) (insert "λ"))) ;
【Hyper+3】 insert λ char
(global-set-key (kbd "H-3") "λ") ; 【Hyper+3】 insert λ char. This is a
builtin macro for above.

(In this example we use Hyper key, but Ctrl or Meta works too. You can
make the Win key or the Menu key act as Hyper. To set up, see: Emacs:
How to define Hyper & Super Keys.)

Example of using “key-translation-map”:

(define-key key-translation-map (kbd "H-3") (kbd "λ")) ; 【Hyper+3】
insert λ char

There are some advantage and disadvantages in either way.

Problem with “global-set-key”

If you use “global-set-key”, then when you do interactive search 【Ctrl
+s】, then when you type 【Hyper+3】, it'll exit the search. So this
means, if you use unicode heavily in your files, then you lose the
ability to isearch them. (you can work-around by typing it in the
buffer first, put cursor on it, start isearch, 【C-w】 to select the
char. Then when done, delete that symbol you inserted for this

Using “key-translation-map” doesn't have this problem.

Problem with “key-translation-map”

The problem with “key-translation-map” is this: Suppose you don't type
the backtick (`) char often, and you press 【C-x o】 “delete-other-
windows” ~50 times more often. So, to increase the efficiency of key
use on keyboard, you remap backtick to do “delete-other-windows” and
set 【Hyper+`】 for inputting the backtick. The elisp code looks like

(global-set-key (kbd "`") 'someCommand) ; set ` to someCommand
(define-key key-translation-map (kbd "H-`") (kbd "`")) ; insert
backtick char by 【Hyper+`】

The problem is, when you type 【Hyper+`】, it'll actually call the
command bound to backtick.

So, this means, the “key-translation-map” is great for symbol input
but is not good if you want to use it as a way to remap buttons.

Note: there's the function “keyboard-translate”. However, it is
designed to translate character only. So, key combination isn't a
character and you can't use it for Hyper combination. Using (define-
key key-translation-map …) is more versatile. (Due to historical
reasons, “keyboard-translate” does work for some Ctrl combination key.
(thanks to Stefan Monnier and Deniz Dogan for this tip. (Source

Who Need to Use Unicode That Often?

Well, i do, a lot, all over, in my writings, as well in coding.

For example, bullet (•), “curly quote”, dash (—), angle bracket for
〈article title〉 and 《book title》 (See: Intro to Chinese Punctuation
with Computer Language Syntax Perspectives.), and i use 【lenticular
bracket】 to mark key combinations, 「corner bracket」 to mark computer
code, and i use FULLWIDTH AMPERSAND (&) for avoid html entity
complexity (See: HTML Entities, Ampersand, Unicode, Semantics.) , and
lots others.

Using proper symbols decreases ambiguity at syntax level. For example,
the asterisk (*) can mean lots of things. But a dedicated bullet “•”
carries a precise semantics.

For coding, some languages heavily use math symbols (e.g. APL,
Mathematica). For functional languages such as Haskell, Scheme Lisp,
you can setup for example “λ” to mean “lambda”, “≠” to mean “!=”, “⊕”
for user-defined operators, etc. You can also use symbols for variable
names in emacs lisp, Javascript, Java (e.g. “α”). See:

    * Unicode Support in Ruby, Perl, Python, javascript, Java, Emacs
Lisp, Mathematica
    * Problems of Symbol Congestion in Computer Languages (ASCII Jam;
Unicode; Fortress)
    * How Mathematica does Unicode?
    * Unicode Popularity On Web


reply via email to

[Prev in Thread] Current Thread [Next in Thread]