Subject: 23.2; term.el: After running stty -opost, printing \n\r inserts trailing spaces
From: Thomas Fitzsimmons <[email-masked]>
To: [email-masked]
Date: Wed, 17 Nov 2010 15:00:57 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

This bug report will be sent to the Free Software Foundation,
not to your local site managers!
Please write in English if possible, because the Emacs maintainers
usually do not have translators to read other languages for them.

Your bug report will be posted to the [email-masked] mailing list,
and to the gnu.emacs.bug news group.

Please describe exactly what actions triggered the bug
and the precise symptoms of the bug.  If you can, give
a recipe starting from `emacs -Q':

$ emacs -Q
Create term-trailing-spaces.c:
#include <stdio.h>

int main (int argc, char* argv[])
{
  int i = 0;
  while (i < 10)
    {
      printf ("aaaaaaaaaaaaaaaaaaaaaaa\n\rbbbbbbbbbbbb\n\r");
      i++;
    }
  return 0;
}

M-x term
Run program: /bin/bash
RET
C-c M-x eval-expression
(setq show-trailing-whitespace t)
$ gcc -o term-trailing-spaces term-trailing-spaces.c
$ stty -opost
$ ./term-trailing-spaces
Trailing spaces are shown in red.
$ stty opost
$ ./term-trailing-spaces
No trailing spaces are shown.

I'm using serial-term to connect to an embedded Linux board's serial
console.  During Linux boot the terminal doesn't post-process output
(equivalent to stty -opost) so long-line/shorter-line combinations like
in the test case result in spaces being appended to the end of the
buffer.  This is OK in char mode but in line mode the spaces stack up at
the end of the buffer and interfere with terminal I/O.  If I
accidentally leave term-mode in line mode during boot I have to kill the
buffer and restart serial-term.

I traced through term-emulate-terminal to:

   ((eq char ?\r)  ;; (terminfo: cr)
    (term-vertical-motion 0)
    (setq term-current-column term-start-line-column))

term-vertical-motion expands to vertical-motion and it's that call that
results in the spaces being inserted in the buffer.

I worked around the issue by removing, after-the-fact, spaces inserted
by the (vertical-motion 0) call.  A more efficient solution would be to
prevent vertical-motion from inserting the spaces in the first place,
but that happens deep within Emacs somewhere.  I'm hoping a terminal
expert can suggest where to look.

If Emacs crashed, and you have the Emacs process in the gdb debugger,
please include the output from the following gdb commands:
    `bt full' and `xbacktrace'.
For information about debugging Emacs, please read the file
/usr/share/emacs/23.2/etc/DEBUG.

In GNU Emacs 23.2.1 (i386-redhat-linux-gnu, GTK+ Version 2.20.1)
 of 2010-06-03 on x86-07.phx2.fedoraproject.org
Windowing system distributor `Fedora Project', version 11.0.10800000
configured using `configure  '--build=i386-redhat-linux-gnu' '--host=i386-redhat-linux-gnu' '--program-prefix=' '--disable-dependency-tracking' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--with-dbus' '--with-gif' '--with-jpeg' '--with-png' '--with-rsvg' '--with-tiff' '--with-xft' '--with-xpm' '--with-x-toolkit=gtk' 'build_alias=i386-redhat-linux-gnu' 'host_alias=i386-redhat-linux-gnu' 'CFLAGS=-DMAIL_USE_LOCKF -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables''

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: @im=none
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Term

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
  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:
o C-g M-x t e r m <return> <return> C-c M-x C-g C-x 
o C-x C-c C-g C-g C-x k C-g C-c M-x M-p C-k s w i t 
<tab> <backspace> <backspace> <backspace> <backspace> 
<backspace> <backspace> <backspace> <backspace> <backspace> 
<backspace> k i l l - b u <tab> <return> <return> y 
e s <return> C-g C-g M-x t e r m <return> <return> 
C-c M-x e v a l - e x p <tab> <return> ( s e t q SPC 
s h o w - t r a i l i n g - s h i w <backspace> <backspace> 
<backspace> <backspace> w h i t e s p a c e SPC t ) 
<return> g c c SPC - o SPC t e r m - t r a i l i n 
g - s p a c e s SPC t e r m - t r a i l i n g - s p 
a <tab> C-a C-k c d SPC s o u <tab> t e r m <tab> <return> 
g c c SPC - o SPC t e r m - t r a i l i <tab> SPC t 
e r m - t r a i l i <tab> . c <return> s t t y SPC 
- o p o s t <return> . / t e r m - t r a i l i <tab> 
<return> s t t y SPC o p o s t <return> . / t e r m 
- t r a i l i n g - s p a <tab> <return> . / t e r 
m - t r a i l i <tab> <return> C-c M-x r e p o r t 
- e m <tab> <return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Quit [5 times]
t

Load-path shadows:
/usr/share/emacs/site-lisp/flim/md4 hides /usr/share/emacs/23.2/lisp/md4
/usr/share/emacs/site-lisp/flim/sha1 hides /usr/share/emacs/23.2/lisp/sha1
/usr/share/emacs/site-lisp/flim/hex-util hides /usr/share/emacs/23.2/lisp/hex-util
/usr/share/emacs/site-lisp/flim/ntlm hides /usr/share/emacs/23.2/lisp/net/ntlm
/usr/share/emacs/site-lisp/flim/sasl-ntlm hides /usr/share/emacs/23.2/lisp/net/sasl-ntlm
/usr/share/emacs/site-lisp/flim/hmac-def hides /usr/share/emacs/23.2/lisp/net/hmac-def
/usr/share/emacs/site-lisp/flim/hmac-md5 hides /usr/share/emacs/23.2/lisp/net/hmac-md5
/usr/share/emacs/site-lisp/flim/sasl-digest hides /usr/share/emacs/23.2/lisp/net/sasl-digest
/usr/share/emacs/site-lisp/flim/sasl-cram hides /usr/share/emacs/23.2/lisp/net/sasl-cram
/usr/share/emacs/site-lisp/flim/sasl hides /usr/share/emacs/23.2/lisp/net/sasl

Features:
(shadow sort mail-extr message idna 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 sha1-el 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 gtk x-toolkit x multi-tty emacs)