Subject: [PATCH] don't export LINES and COLUMNS env vars in term to fix ncurses applications

From: Matthew Leach <[email-masked]>
Date: Mon, 30 Sep 2019 20:59:04 +0100

Hi all,

The attached patch removes the exporting of the LINES and COLUMNS
environment variables in term-mode.  Exporting these variables causes
issues for ncurses applications.  For example, when running:

emacs -Q
M-x term
<ret> (to select /bin/bash)
htop (to run htop)

and resizing the window (especially making it smaller) can make the
program impossible to read.

The ncurses code uses an ioctl() initially to get the current size[1]
which returns a correct result. However, since we exported the above
environment variables these values are discarded in favour of the (now
stale) values of LINES and COLUMNS. Emacs makes no attempt to update
these variables when the window is resized. Therefore, ncurses assumes
the window size is the same when it has actually changed.

Note that executing:

$ echo $LINES

from a bash shell actually doesn't get the LINES environment variable as
can be seen with:

$ printenv | grep LINES

Whenever $LINES is accessed, bash translates this into an ioctl() and
returns the result. Therefore, removing these variables shouldn't
prevent any shell scripts from accessing these variables.

[1]: ncurses/ncurses/tinfo/lib_setup.c (_nc_get_screensize)

>From 20b1adff1c32cfeeba1cf3101b05d5c76037206d Mon Sep 17 00:00:00 2001
From: Matthew Leach <[email-masked]>
Date: Mon, 30 Sep 2019 20:35:54 +0100
Subject: [PATCH] don't export LINES and COLUMNS env vars to term

* lisp/term.el (term-exec-1): Remove the exporting of the LINES and
COLUMNS environment variables to the terminal process.
---
 lisp/term.el | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/lisp/term.el b/lisp/term.el
index 66ae470239a..e309bd802d5 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -1550,9 +1550,7 @@ term-exec-1
 	   (format term-termcap-format "TERMCAP="
 		   term-term-name term-height term-width)
 
-	   (format "INSIDE_EMACS=%s,term:%s" emacs-version term-protocol-version)
-	   (format "LINES=%d" term-height)
-	   (format "COLUMNS=%d" term-width))
+	   (format "INSIDE_EMACS=%s,term:%s" emacs-version term-protocol-version))
 	  process-environment))
 	(process-connection-type t)
 	;; We should suppress conversion of end-of-line format.
-- 
2.23.0

-- 
Matt