Subject: 23.2.90; ansi-term mode does not properly handle keystrokes with M- and other modifiers

From: Nelson Elhage <[email-masked]>
Date: Fri, 04 Mar 2011 14:36:17 -0500

* Run M-x ansi-term
* Type a key combination including Meta and either Ctrl or Shift.
* The shell inside ansi-term will receive it with the control or shift
  dropped.

  You can verify this by e.g. trying C-v C-M-a into bash; The expected
  behavior is that bash prints ^[ and then moves to the beginning of the
  line; Inside ansi-term, it prints ^[a, since the C- gets dropped.

  This even happens with "shift", so M-D gets sent as "ESC d".

The bug is caused by `term-send-raw-meta`, which sends an ESC followed
by (essentially) (event-basic-type last-input-event), which also strips
all the other modifiers, not just M-./

The following patch fixes the issue for me, but I am not sure it is
correct in general.

diff --git a/lisp/term.el b/lisp/term.el
index 2223ff2..28a4aa6 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -1218,7 +1218,7 @@ without any interpretation."
          (setq tmp (get char 'ascii-character))
          (when tmp
            (setq char tmp)))))
-    (setq char (event-basic-type char))
+    (setq char (logand char (lognot ?\M-\^@)))
     (term-send-raw-string (if (and (numberp char)
                                   (> char 127)
                                   (< char 256))

In GNU Emacs 23.2.90.1 (x86_64-unknown-linux-gnu, X toolkit, Xaw3d scroll bars)
 of 2010-11-09 on psychotique
Windowing system distributor `The X.Org Foundation', version 11.0.10706000
configured using `configure  '--prefix' '/home/nelhage/sw/emacs' '--with-x-toolkit=lucid''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.utf8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  blink-cursor-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
M-x n <backspace> a n s o <tab> <backspace> i <tab> 
t <tab> <return> <return> l s <return> C-v C-M-d <help-echo> 
<M-backspace> <M-backspace> f o o b a r C-a C-v C-M-d 
<help-echo> C-a C-k M-D C-v M-D M-x C-x b <return> 
C-x C-g M-x r e p o r t SPC b u <tab> <return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.

Load-path shadows:
None found.

Features:
(shadow sort mail-extr message sendmail regexp-opt ecomplete rfc822 mml
easymenu mml-sec password-cache mm-decode mm-bodies mm-encode mailcap
mail-parse rfc2231 rfc2047 rfc2045 qp ietf-drums mailabbrev nnheader
gnus-util netrc time-date mm-util mail-prsvr gmm-utils wid-edit
mailheader canlock sha1 hex-util hashcash mail-utils emacsbug term
disp-table ehelp electric ring tooltip ediff-hook vc-hooks
lisp-float-type mwheel x-win x-dnd font-setting tool-bar dnd fontset
image fringe lisp-mode register page menu-bar rfn-eshadow timer select
scroll-bar mldrag mouse jit-lock font-lock syntax facemenu font-core
frame cham georgian utf-8-lang misc-lang vietnamese tibetan thai
tai-viet lao korean japanese hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese case-table epa-hook jka-cmpr-hook help
simple abbrev loaddefs button minibuffer faces cus-face files
text-properties overlay md5 base64 format env code-pages mule custom
widget hashtable-print-readable backquote make-network-process dbusbind
system-font-setting font-render-setting x-toolkit x multi-tty emacs)