Discussion:
Function keys in PuTTY and Emacs
(too old to reply)
Paul Kaletta
2011-08-15 11:39:42 UTC
Permalink
Hi,

I'm using PuTTY 0.61 to ssh into a Solaris 8 box. In Emacs I have F1
through F12 mapped to macros that I use frequently. The F-keys work
flawlessly in Midnight-Commander, but I somehow I can't convince Emacs
to interpret them correctly.

$TERM is set to xterm, and infocmp gives

# Reconstructed via infocmp from file: /usr/share/lib/terminfo/x/
xterm
xterm|vs100|xterm terminal emulator,
am, km, mir, msgr, xenl,
cols#80, it#8, lines#65,
acsc=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m, bold=\E[1m, clear=\E[H\E[2J,
cr=\r, csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=\b,
cud=\E[%p1%dB, cud1=\n, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M,
ed=\E[J, el=\E[K, el1=\E[1K$<3>, enacs=\E(B\E)0,
home=\E[H, ht=\t, hts=\EH, ich=\E[%p1%d@, ich1=\E[@,
il=\E[%p1%dL, il1=\E[L, ind=\n, ka1=\EOq, ka3=\EOs,
kb2=\EOr, kbs=\b, kc1=\EOp, kc3=\EOn, kcub1=\EOD,
kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA, kent=\EOM,
kf0=\E[21~, kf1=\E[11~, kf10=\EOx, kf2=\E[12~,
kf3=\E[13~, kf4=\E[14~, kf5=\E[15~, kf6=\E[17~,
kf7=\E[18~, kf8=\E[19~, kf9=\E[20~, rc=\E8, rev=\E[7m,
ri=\EM, rmacs=^O, rmkx=\E[?1l\E>, rmso=\E[m,
rmul=\E[m,
rs1=\E>\E[1;3;4;5;6l\E[?7h\E[m\E[r\E[2J\E[H, rs2=@,
sc=\E7,
sgr=\E[0%?%p1%p6%|%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;m
%?%p9%t^N%e^O%;,
sgr0=\E[m, smacs=^N, smkx=\E[?1h\E=, smso=\E[7m,
smul=\E[4m, tbc=\E[3g,

when I type "cat -v" and press F1 through F12 I get
^[[11~^[[12~^[[13~^[[14~^[[15~^[[17~^[[18~^[[19~^[[20~^[[21~^[[23~^[[24~

as one would expect. When I press the F-keys, Emacs interprets the
key-press as M-[ followed by the respective suffix. For example, if I
press F1, Emacs interprets that as M-[ followed by 11~. First, it
executes line-to-top, the function which is mapped to M-[, and then
inserts 11~ into the current buffer.

I have tried setting TERM to various strings for which entries in
terminfo exist, and I have always adjusted the settings for the F-keys
in the PuTTY configuration accordingly. Midnight Commander always
works as I would expect, but Emacs doesn't. I managed to get it to
interpet F1-to-F4 correctly, the keys beyond F5 stayed broken. I do
not remember anymore how I did that. It seems that, if I start GNU
screen inside PuTTY, the terminal is set into a different mode, and
PuTTY starts sending different key-codes for the F-keys (screen sets
TERM to vt100, which putty seems to notice).

Best regards,
Paul
Stefan Monnier
2011-08-15 18:21:33 UTC
Permalink
Post by Paul Kaletta
I'm using PuTTY 0.61 to ssh into a Solaris 8 box. In Emacs I have F1
through F12 mapped to macros that I use frequently. The F-keys work
flawlessly in Midnight-Commander, but I somehow I can't convince Emacs
to interpret them correctly.
$TERM is set to xterm, and infocmp gives
# Reconstructed via infocmp from file: /usr/share/lib/terminfo/x/
xterm
xterm|vs100|xterm terminal emulator,
am, km, mir, msgr, xenl,
cols#80, it#8, lines#65,
acsc=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m, bold=\E[1m, clear=\E[H\E[2J,
cr=\r, csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=\b,
cud=\E[%p1%dB, cud1=\n, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M,
ed=\E[J, el=\E[K, el1=\E[1K$<3>, enacs=\E(B\E)0,
il=\E[%p1%dL, il1=\E[L, ind=\n, ka1=\EOq, ka3=\EOs,
kb2=\EOr, kbs=\b, kc1=\EOp, kc3=\EOn, kcub1=\EOD,
kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA, kent=\EOM,
kf0=\E[21~, kf1=\E[11~, kf10=\EOx, kf2=\E[12~,
kf3=\E[13~, kf4=\E[14~, kf5=\E[15~, kf6=\E[17~,
kf7=\E[18~, kf8=\E[19~, kf9=\E[20~, rc=\E8, rev=\E[7m,
ri=\EM, rmacs=^O, rmkx=\E[?1l\E>, rmso=\E[m,
rmul=\E[m,
sc=\E7,
sgr=\E[0%?%p1%p6%|%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;m
%?%p9%t^N%e^O%;,
sgr0=\E[m, smacs=^N, smkx=\E[?1h\E=, smso=\E[7m,
smul=\E[4m, tbc=\E[3g,
when I type "cat -v" and press F1 through F12 I get
^[[11~^[[12~^[[13~^[[14~^[[15~^[[17~^[[18~^[[19~^[[20~^[[21~^[[23~^[[24~
You can also use C-h l (after hitting the F<n> keys) to see what Emacs
has received.

I'd expect Emacs to do the right thing here, without any extra setup.
So my crystal ball tells me to first check "ldd /path/to/emacs" to make
sure that your Emacs indeed uses terminfo.


Stefan
unfrostedpoptart
2011-08-15 20:30:41 UTC
Permalink
Post by Stefan Monnier
You can also use C-h l (after hitting the F<n> keys) to see what Emacs
has received.
Cool - never knew about that one. Very useful! I've always used C-h k and then hit the key I wanted to know about.
Paul Kaletta
2011-08-16 10:13:59 UTC
Permalink
Post by Stefan Monnier
You can also use C-h l (after hitting the F<n> keys) to see what Emacs
has received.
If I press F1 and then C-h l, the log contains

ESC [ 1 1 ~ C-h l

This is what I would expect. However Emacs fails to interpret this
sequence as F1. Is there a way to figure out which specific sequence
the editor expects for a given key?
Post by Stefan Monnier
I'd expect Emacs to do the right thing here, without any extra setup.
So my crystal ball tells me to first check "ldd /path/to/emacs" to make
sure that your Emacs indeed uses terminfo.
Both emacs and mc seem to be using the same version of (n)curses, so
I'm not sure what exactly the issue is.

% ldd /opt/TWWfsw/emacs22/bin/emacs
libgtk-x11-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/libgtk-
x11-2.0.so.0
libgdk-x11-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/libgdk-
x11-2.0.so.0
libXext.so.0 => /usr/openwin/lib/libXext.so.0
libX11.so.4 => /usr/openwin/lib/libX11.so.4
libatk-1.0.so.0 => /opt/TWWfsw/libatk122/lib/
libatk-1.0.so.0
libgdk_pixbuf-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/
libgdk_pixbuf-2.0.so.0
libm.so.1 => /usr/lib/libm.so.1
libpangocairo-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpangocairo-1.0.so.0
libcairo.so.2 => /opt/TWWfsw/libcairo16/lib/
libcairo.so.2
libpangoft2-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpangoft2-1.0.so.0
libpango-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpango-1.0.so.0
libfreetype.so.6 => /opt/TWWfsw/libttf23/lib/
libfreetype.so.6
libfontconfig.so.1 => /opt/TWWfsw/fcpackage26/lib/
libfontconfig.so.1
libgobject-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libgobject-2.0.so.0
libgmodule-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libgmodule-2.0.so.0
libdl.so.1 => /usr/lib/libdl.so.1
libglib-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libglib-2.0.so.0
libintl.so.8 => /opt/TWWfsw/gettext017/lib/libintl.so.8
libpthread.so.1 => /usr/lib/libpthread.so.1
libSM.so.6 => /usr/openwin/lib/libSM.so.6
libICE.so.6 => /usr/openwin/lib/libICE.so.6
libtiff.so.3 => /opt/TWWfsw/libtiff38/lib/libtiff.so.3
libjpeg.so.62 => /opt/TWWfsw/jpeg/lib/libjpeg.so.62
libpng.so.2 => /opt/TWWfsw/libpng12/lib/libpng.so.2
libz.so.2 => /opt/TWWfsw/libz12/lib/libz.so.2
libungif.so.4 => /opt/TWWfsw/libungif/lib/libungif.so.
4
libXpm.so.4.11 => /opt/TWWfsw/xpm/lib/libXpm.so.4.11
libsocket.so.1 => /usr/lib/libsocket.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libkstat.so.1 => /usr/lib/libkstat.so.1
libncurses.so.5 => /opt/TWWfsw/ncurses56/lib/
libncurses.so.5
libc.so.1 => /usr/lib/libc.so.1
libXrender.so.1 => /opt/TWWfsw/fcpackage26/lib/
libXrender.so.1
libpixman-1.so.0 => /opt/TWWfsw/libcairo16/lib/
libpixman-1.so.0
libiconv.so.2 => /opt/TWWfsw/libiconv112/lib/
libiconv.so.2
libexpat.so.1 => /opt/TWWfsw/libexpat20/lib/
libexpat.so.1
libmp.so.2 => /usr/lib/libmp.so.2
libthread.so.1 => /usr/lib/libthread.so.1
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1

% ldd /opt/TWWfsw/bin/mc
libintl.so.8 => /opt/TWWfsw/gettext017/lib/libintl.so.8
libiconv.so.2 => /opt/TWWfsw/libiconv112/lib/
libiconv.so.2
libc.so.1 => /usr/lib/libc.so.1
libglib-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libglib-2.0.so.0
libncurses.so.5 => /opt/TWWfsw/ncurses56/lib/
libncurses.so.5
libnsl.so.1 => /usr/lib/libnsl.so.1
libsocket.so.1 => /usr/lib/libsocket.so.1
libgen.so.1 => /usr/lib/libgen.so.1
libdl.so.1 => /usr/lib/libdl.so.1
libmp.so.2 => /usr/lib/libmp.so.2
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1

Anyway. I downloaded the sources of the very same version of Emacs
(22.3.1) from the GNU ftp site, configured it without X, compiled, and
installed it into my home directory.

% ldd ~/bin/sol64/bin/emacs
libsocket.so.1 => /usr/lib/libsocket.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libkstat.so.1 => /usr/lib/libkstat.so.1
libcurses.so.1 => /usr/lib/libcurses.so.1
libm.so.1 => /usr/lib/libm.so.1
libc.so.1 => /usr/lib/libc.so.1
libdl.so.1 => /usr/lib/libdl.so.1
libmp.so.2 => /usr/lib/libmp.so.2
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1

Apparently, the configure script found a different version of curses
and used that, and my private Emacs binary recognizes the F-keys as
expected!

If I press F1 in my private version and then C-h l, the log contains

ESC [ 1 1 ~ C-h l

again. But this time Emacs interprets that as I would expect. I'd
prefer to use my site's default version of Emacs if possible, but
something seems to be wrong with it... Do you perhaps know a way to
make it handle "ESC [" correctly?

Best regards,
Paul
Javier
2011-08-16 13:13:55 UTC
Permalink
Try to use cat -v in the command line, and see how it sees the key
combinations

For some key combination Shift+Up I see something like this:

^[O2A

Then I write into ~/.emacs

;;;; \e is escape character '^['
(define-key function-key-map "\eO2A" '[S-up])
(global-set-key [S-up] 'windmove-up)

That should do the trick. If not try another terminal instead of putty,
such as the xterm that comes with cygwin. This kind of escape
sequences stuff is overcomplicated and some terminals behave differently
to another.

HTH.
Post by Paul Kaletta
Post by Stefan Monnier
You can also use C-h l (after hitting the F<n> keys) to see what Emacs
has received.
If I press F1 and then C-h l, the log contains
ESC [ 1 1 ~ C-h l
This is what I would expect. However Emacs fails to interpret this
sequence as F1. Is there a way to figure out which specific sequence
the editor expects for a given key?
Post by Stefan Monnier
I'd expect Emacs to do the right thing here, without any extra setup.
So my crystal ball tells me to first check "ldd /path/to/emacs" to make
sure that your Emacs indeed uses terminfo.
Both emacs and mc seem to be using the same version of (n)curses, so
I'm not sure what exactly the issue is.
% ldd /opt/TWWfsw/emacs22/bin/emacs
libgtk-x11-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/libgtk-
x11-2.0.so.0
libgdk-x11-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/libgdk-
x11-2.0.so.0
libXext.so.0 => /usr/openwin/lib/libXext.so.0
libX11.so.4 => /usr/openwin/lib/libX11.so.4
libatk-1.0.so.0 => /opt/TWWfsw/libatk122/lib/
libatk-1.0.so.0
libgdk_pixbuf-2.0.so.0 => /opt/TWWfsw/libgtk+212/lib/
libgdk_pixbuf-2.0.so.0
libm.so.1 => /usr/lib/libm.so.1
libpangocairo-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpangocairo-1.0.so.0
libcairo.so.2 => /opt/TWWfsw/libcairo16/lib/
libcairo.so.2
libpangoft2-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpangoft2-1.0.so.0
libpango-1.0.so.0 => /opt/TWWfsw/libpango120/lib/
libpango-1.0.so.0
libfreetype.so.6 => /opt/TWWfsw/libttf23/lib/
libfreetype.so.6
libfontconfig.so.1 => /opt/TWWfsw/fcpackage26/lib/
libfontconfig.so.1
libgobject-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libgobject-2.0.so.0
libgmodule-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libgmodule-2.0.so.0
libdl.so.1 => /usr/lib/libdl.so.1
libglib-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libglib-2.0.so.0
libintl.so.8 => /opt/TWWfsw/gettext017/lib/libintl.so.8
libpthread.so.1 => /usr/lib/libpthread.so.1
libSM.so.6 => /usr/openwin/lib/libSM.so.6
libICE.so.6 => /usr/openwin/lib/libICE.so.6
libtiff.so.3 => /opt/TWWfsw/libtiff38/lib/libtiff.so.3
libjpeg.so.62 => /opt/TWWfsw/jpeg/lib/libjpeg.so.62
libpng.so.2 => /opt/TWWfsw/libpng12/lib/libpng.so.2
libz.so.2 => /opt/TWWfsw/libz12/lib/libz.so.2
libungif.so.4 => /opt/TWWfsw/libungif/lib/libungif.so.
4
libXpm.so.4.11 => /opt/TWWfsw/xpm/lib/libXpm.so.4.11
libsocket.so.1 => /usr/lib/libsocket.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libkstat.so.1 => /usr/lib/libkstat.so.1
libncurses.so.5 => /opt/TWWfsw/ncurses56/lib/
libncurses.so.5
libc.so.1 => /usr/lib/libc.so.1
libXrender.so.1 => /opt/TWWfsw/fcpackage26/lib/
libXrender.so.1
libpixman-1.so.0 => /opt/TWWfsw/libcairo16/lib/
libpixman-1.so.0
libiconv.so.2 => /opt/TWWfsw/libiconv112/lib/
libiconv.so.2
libexpat.so.1 => /opt/TWWfsw/libexpat20/lib/
libexpat.so.1
libmp.so.2 => /usr/lib/libmp.so.2
libthread.so.1 => /usr/lib/libthread.so.1
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1
% ldd /opt/TWWfsw/bin/mc
libintl.so.8 => /opt/TWWfsw/gettext017/lib/libintl.so.8
libiconv.so.2 => /opt/TWWfsw/libiconv112/lib/
libiconv.so.2
libc.so.1 => /usr/lib/libc.so.1
libglib-2.0.so.0 => /opt/TWWfsw/libglib216/lib/
libglib-2.0.so.0
libncurses.so.5 => /opt/TWWfsw/ncurses56/lib/
libncurses.so.5
libnsl.so.1 => /usr/lib/libnsl.so.1
libsocket.so.1 => /usr/lib/libsocket.so.1
libgen.so.1 => /usr/lib/libgen.so.1
libdl.so.1 => /usr/lib/libdl.so.1
libmp.so.2 => /usr/lib/libmp.so.2
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1
Anyway. I downloaded the sources of the very same version of Emacs
(22.3.1) from the GNU ftp site, configured it without X, compiled, and
installed it into my home directory.
% ldd ~/bin/sol64/bin/emacs
libsocket.so.1 => /usr/lib/libsocket.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libkstat.so.1 => /usr/lib/libkstat.so.1
libcurses.so.1 => /usr/lib/libcurses.so.1
libm.so.1 => /usr/lib/libm.so.1
libc.so.1 => /usr/lib/libc.so.1
libdl.so.1 => /usr/lib/libdl.so.1
libmp.so.2 => /usr/lib/libmp.so.2
/usr/platform/SUNW,Sun-Fire-280R/lib/libc_psr.so.1
Apparently, the configure script found a different version of curses
and used that, and my private Emacs binary recognizes the F-keys as
expected!
If I press F1 in my private version and then C-h l, the log contains
ESC [ 1 1 ~ C-h l
again. But this time Emacs interprets that as I would expect. I'd
prefer to use my site's default version of Emacs if possible, but
something seems to be wrong with it... Do you perhaps know a way to
make it handle "ESC [" correctly?
Best regards,
Paul
Stefan Monnier
2011-08-16 14:35:59 UTC
Permalink
Post by Paul Kaletta
If I press F1 in my private version and then C-h l, the log contains
ESC [ 1 1 ~ C-h l
again. But this time Emacs interprets that as I would expect. I'd
prefer to use my site's default version of Emacs if possible, but
something seems to be wrong with it... Do you perhaps know a way to
make it handle "ESC [" correctly?
(define-key input-decode-map "\e[11~" [f1])

Plus all the others for all the other keys you care about.


Stefan

Continue reading on narkive:
Loading...