Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

elisp question:keyboard-translate with hyper fail?

253 views
Skip to first unread message

Xah Lee

unread,
Mar 11, 2011, 6:00:17 AM3/11/11
to
when i try to do this:

(keyboard-translate ?\H-3 ?•) ; set Hyper+3 to type unicode bullet
char

i got this error

Debugger entered--Lisp error: (wrong-type-argument characterp
16777332)
keyboard-translate(16777332 8226)
eval((keyboard-translate 16777332 8226))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)

anyone know why? using Ctrl works fine, e.g.

(keyboard-translate ?\C-3 ?•)

i double checked and the syntax for hyper ?\H- seems correct.

Xah

Deniz Dogan

unread,
Mar 11, 2011, 7:01:09 AM3/11/11
to Xah Lee, help-gn...@gnu.org
2011/3/11 Xah Lee <xah...@gmail.com>:

(keyboard-translate ?\C-3 ?•) does not work for me.

Debugger entered--Lisp error: (wrong-type-argument characterp 67108915)
keyboard-translate(67108915 8226)
eval((keyboard-translate 67108915 8226))


eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)

`keyboard-translate' only works with characters and neither C-3 nor
H-3 make up characters as far as I know.

--
Deniz Dogan

Xah Lee

unread,
Mar 11, 2011, 5:11:52 PM3/11/11
to
On Mar 11, 4:01 am, Deniz Dogan <deniz.a.m.do...@gmail.com> wrote:
> > i double checked and the syntax for hyper ?\H- seems correct.
>
> (keyboard-translate ?\C-3 ?•) does not work for me.
>
> Debugger entered--Lisp error: (wrong-type-argument characterp 67108915)
>   keyboard-translate(67108915 8226)
>   eval((keyboard-translate 67108915 8226))
>   eval-last-sexp-1(nil)
>   eval-last-sexp(nil)
>   call-interactively(eval-last-sexp nil nil)
>
> `keyboard-translate' only works with characters and neither C-3 nor
> H-3 make up characters as far as I know.

that's strange.

i have the following and they work.

;; Swap “Ctrl+x” and “Ctrl+t”, so it's easier to type on Dvorak layout
(keyboard-translate ?\C-t ?\C-x)
(keyboard-translate ?\C-x ?\C-t)

the ?\C-3 doesn't work (my mistake), but this works
(keyboard-translate ?\C-t ?•)

so maybe with a letter char it works, however, the following still
doesn't work
(keyboard-translate ?\H-t ?•)

Xah

Tim X

unread,
Mar 11, 2011, 7:26:53 PM3/11/11
to
Xah Lee <xah...@gmail.com> writes:

Are you sure your keyboard is actually generating the keycode you think
it is? I remember on some (unix) based systems, I had to used xmodmap in
order to get the hyper key working.

--
tcross (at) rapttech dot com dot au

Stefan Monnier

unread,
Mar 11, 2011, 9:54:46 PM3/11/11
to
> ;; Swap “Ctrl+x” and “Ctrl+t”, so it's easier to type on Dvorak layout
> (keyboard-translate ?\C-t ?\C-x)
> (keyboard-translate ?\C-x ?\C-t)

There are 32 Ctrl+letter combinations which are characters, and the
above two are among them. These are exceptional special cases due
to history. My general recommendation is to not use keyboard-translate
but key-translation-map or function-key-map, which work on arbitrary
key sequences rather only on single-char events.


Stefan

Le Wang

unread,
Mar 11, 2011, 11:51:56 PM3/11/11
to Stefan Monnier, help-gn...@gnu.org

I got these exact keyboard-translate lines from Xah's blog.  They don't work reliably on Windows.  Sometimes after switching back to Emacs from another program, and my first input to Emacs is C-j, C-j still registers instead of C-x.

How would I use key-translation-map to do the swap?

(define-key key-translation-map [(control x)] [(control j)])
(define-key key-translation-map [(control j)] [(control x)])

C-j seems regixter as C-x.  But C-x acting like a prefix key, waiting for the next input.
 

       Stefan




--
Le

Xah Lee

unread,
Mar 12, 2011, 2:07:06 AM3/12/11
to

thanks to all.

perhaps the doc can be improved.

i just read up the elisp doc and it actually gives a C- example.

-- Function: keyboard-translate from to
This function modifies `keyboard-translate-table' to translate
character code FROM into character code TO. It creates the
keyboard translate table if necessary.

Here's an example of using the `keyboard-translate-table' to make
`C-x', `C-c' and `C-v' perform the cut, copy and paste operations:

(keyboard-translate ?\C-x 'control-x)
(keyboard-translate ?\C-c 'control-c)
(keyboard-translate ?\C-v 'control-v)
(global-set-key [control-x] 'kill-region)
(global-set-key [control-c] 'kill-ring-save)
(global-set-key [control-v] 'yank)

also note it uses the syntax 「'control-x」. I don't quite understand
it...

should it be a bug report?

Xah

Xah Lee

unread,
Mar 12, 2011, 2:22:14 AM3/12/11
to
ugh, just tried this and still doesn't work.

(define-key key-translation-map [?\H-8] ?\◇)

am giving up at this point.

in case anyone's interested, i'm trying to make hyper key do unicode
symbols.
Ι have it all working by
(global-set-key (kbd "H-3") (lambda () (interactive) (insert "•"))) ;
bullet

but yesterday i got smart and thought i'll re-write them using
keyboard-translate instead. (just had a major refactoring of
my .emacs.)

possibly i'll file a bug report. Thanks for the helpful answers.

Xah

Deniz Dogan

unread,
Mar 12, 2011, 6:59:44 AM3/12/11
to Xah Lee, help-gn...@gnu.org
2011/3/12 Xah Lee <xah...@gmail.com>:

The examples with C-x, C-c and C-v are valid. The keys C-a up to C-z
and probably a few others (Stefan said there are 32 of them) are all
characters. C-3 however is not a character.

I don't think it's a bug in the documentation, but maybe it should
clarify this matter?

--
Deniz Dogan

Stefan Monnier

unread,
Mar 12, 2011, 4:16:15 PM3/12/11
to Le Wang, help-gn...@gnu.org
> I got these exact keyboard-translate lines from Xah's blog. They don't work
> reliably on Windows. Sometimes after switching back to Emacs from another
> program, and my first input to Emacs is C-j, C-j still registers instead of
> C-x.

Sounds like a bug (even sounds like one that's already been reported but
that we're having trouble tracking down). But I can't find it in the
bug database, so maybe it's something different. So please M-x
report-emacs-bug an include as much info as possible to help us
reproduce the bug.

> How would I use key-translation-map to do the swap?

> (define-key key-translation-map [(control x)] [(control j)])
> (define-key key-translation-map [(control j)] [(control x)])

Yes.

> C-j seems regixter as C-x. But C-x acting like a prefix key, waiting for
> the next input.

I think that's because of function-key-map's remapping of C-x @ S <key>
to S-<key>. If you add (define-key function-key-map [?\C-x] nil), it
might work (it does for me).


Stefan

Le Wang

unread,
Mar 12, 2011, 11:31:49 PM3/12/11
to Stefan Monnier, help-gn...@gnu.org
On Sun, Mar 13, 2011 at 5:16 AM, Stefan Monnier <mon...@iro.umontreal.ca> wrote:
> I got these exact keyboard-translate lines from Xah's blog.  They don't work
> reliably on Windows.  Sometimes after switching back to Emacs from another
> program, and my first input to Emacs is C-j, C-j still registers instead of
> C-x.

Sounds like a bug (even sounds like one that's already been reported but
that we're having trouble tracking down).  But I can't find it in the
bug database, so maybe it's something different.  So please M-x
report-emacs-bug an include as much info as possible to help us
reproduce the bug.
> How would I use key-translation-map to do the swap?

> (define-key key-translation-map [(control x)] [(control j)])
> (define-key key-translation-map [(control j)] [(control x)])

Yes.

> C-j seems regixter as C-x.  But C-x acting like a prefix key, waiting for
> the next input.

I think that's because of function-key-map's remapping of C-x @ S <key>
to S-<key>.  If you add (define-key function-key-map [?\C-x] nil), it
might work (it does for me).

These 3 lines seem to do the swapping I want.

(define-key key-translation-map [(control x)] [(control j)])
(define-key key-translation-map [(control j)] [(control x)])
(define-key function-key-map [(control x)] nil)

Hopefully this will work reliably.

Thanks!
 

       Stefan



--
Le

Stefan Monnier

unread,
Mar 14, 2011, 10:17:33 AM3/14/11
to
> ugh, just tried this and still doesn't work.
> (define-key key-translation-map [?\H-8] ?\◇)

key-translation-map maps key sequences to key sequences, so try

(define-key key-translation-map [?\H-8] [?\◇])

> Ι have it all working by


> (global-set-key (kbd "H-3") (lambda () (interactive) (insert "•"))) ;
> bullet

You can also do

(global-set-key (kbd "H-3") "•")


Stefan

Xah Lee

unread,
Mar 14, 2011, 4:32:51 PM3/14/11
to
On Mar 14, 7:17 am, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > ugh, just tried this and still doesn't work.
> > (define-key key-translation-map [?\H-8] ?\◇)
>
> key-translation-map maps key sequences to key sequences, so try
>
>   (define-key key-translation-map [?\H-8] [?\◇])

Thanks! That worked.

Xah

Xah Lee

unread,
Mar 18, 2011, 9:56:56 AM3/18/11
to

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〉
http://xahlee.org/emacs/emacs_key-translation-map.html

────────────────────
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
issues.

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-
translation-map”.

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
purpose.)

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
this:

(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
groups.google.com)))

────────────────────
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

Xah

rusi

unread,
Mar 19, 2011, 12:09:53 PM3/19/11
to

Xah your unicode has not worked out -- at least in this post :-)
[Though you probably cant see that I cant see (most of) your
characters]

Xah Lee

unread,
Mar 19, 2011, 8:31:27 PM3/19/11
to
α λ — “ ” • … ≠ ⊕ ① ② ─ 〈 〉 《 》 「 」 【 】 &

> Xah your unicode has not worked out -- at least in this post :-)
> [Though you probably cant see that I cant see (most of) your
> characters]

do a screenshot. I'd love to see it.

if you use Google Chrome, and view in google groups here
http://groups.google.com/group/gnu.emacs.help/msg/0e5cb199a07c1235

i think all char will show.

as you know... you'll need the font, and the app will need to support
unicode, and the news server didn't screw up the encoding...

i have on my site a blog of a selection of fonts i found to be best
for unicode. For linux, that's DejaVu.

Xah

rusi

unread,
Mar 19, 2011, 10:51:52 PM3/19/11
to
On Mar 20, 5:31 am, Xah Lee <xah...@gmail.com> wrote:
> α λ — “ ” • … ≠ ⊕ ① ② ─ 〈 〉 《 》 「 」 【 】 &
>
> > Xah your unicode has not worked out -- at least in this post :-)
> > [Though you probably cant see that I cant see (most of) your
> > characters]
>
> do a screenshot. I'd love to see it.

Seems ok now -- must have been a transient problem with googlegroups)
These【 】were showing as unicode boxes IIRC

0 new messages