Subject: bug#20484: EMACS=t Joy and Happiness

From: [email-masked] (Phillip Lord)
Date: Fri, 25 May 2018 23:49:51 +0100

Paul Eggert <[email-masked]> writes:

> On 05/24/2018 01:46 PM, Phillip Lord wrote:
>> I would like to propose that we now remove this on master,
>>
>
> Given the compatibility concerns expressed, how about the attached
> patch instead? The idea is to remove the need to setenv EMACS with
> newer Bashes, while still setting EMACS for older Bashes, for backward
> compatibility.
>
> From f7efdc4d21c22a81398b06cdc58144ec2f9c7697 Mon Sep 17 00:00:00 2001
> From: Paul Eggert <[email-masked]>
> Date: Fri, 25 May 2018 13:25:51 -0700
> Subject: [PROPOSED] =?UTF-8?q?Don=E2=80=99t=20set=20EMACS=3Dt=20if=20Bash?=
>  =?UTF-8?q?=20is=204.4=20or=20newer?=
> MIME-Version: 1.0
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 8bit
>
> * lisp/term.el (term--bash-needs-EMACS-status): New var.
> (term--bash-needs-EMACSp): New function.
> (term-exec-1): Use it instead of always setting EMACS.
> ---
>  lisp/term.el | 31 +++++++++++++++++++++++++------
>  1 file changed, 25 insertions(+), 6 deletions(-)
>
> diff --git a/lisp/term.el b/lisp/term.el
> index 017b0221ec..fa43774ae8 100644
> --- a/lisp/term.el
> +++ b/lisp/term.el
> @@ -1489,6 +1489,26 @@ term-termcap-format
>    ;; don't define :te=\\E[2J\\E[?47l\\E8:ti=\\E7\\E[?47h\
>    "Termcap capabilities supported.")
>  
> +;; This is for backwards compatibility with Bash 4.3 and earlier.
> +;; Remove this hack once Bash 4.4-or-later is reasonably universal, because
> +;; it slows down execution slightly, just before the first subshell.
> +(defvar term--bash-needs-EMACS-status nil
> +  "43 if Bash is so old that it needs EMACS set.
> +Some other integer if Bash is new or not in use.
> +Nil if unknown.")
> +(defun term--bash-needs-EMACSp ()
> +  "t if Bash is old, nil if it is new or not in use."
> +  (unless term--bash-needs-EMACS-status
> +    (let ((process-environment (copy-sequence process-environment)))
> +      (setenv "BASH_ENV")
> +      (setq term--bash-needs-EMACS-status
> +            (condition-case nil
> +                (call-process
> +                 "bash" nil nil nil "-c"
> +                 "case $BASH_VERSION in [0123].*|4.[0123].*) exit 43;; esac")
> +              (error 0)))))
> +  (eq 43 term--bash-needs-EMACS-status))
> +
>  ;; This auxiliary function cranks up the process for term-exec in
>  ;; the appropriate environment.
>  
> @@ -1506,12 +1526,6 @@ term-exec-1
>  	   (format term-termcap-format "TERMCAP="
>  		   term-term-name term-height term-width)
>  
> -	   ;; This is for backwards compatibility with Bash 4.3 and earlier.
> -	   ;; Remove this hack once Bash 4.4-or-later is common, because
> -	   ;; it breaks './configure' of some packages that expect it to
> -	   ;; say where to find EMACS.
> -	   (format "EMACS=%s (term:%s)" emacs-version term-protocol-version)
> -
>  	   (format "INSIDE_EMACS=%s,term:%s" emacs-version term-protocol-version)
>  	   (format "LINES=%d" term-height)
>  	   (format "COLUMNS=%d" term-width))
> @@ -1523,6 +1537,11 @@ term-exec-1
>  	;; escape codes, so we need to see the raw output.  We will have to
>  	;; do the decoding by hand on the parts that are made of chars.
>  	(coding-system-for-read 'binary))
> +    (when (term--bash-needs-EMACSp)
> +      (setq process-environment
> +            (cons
> +             (format "EMACS=%s (term:%s)" emacs-version term-protocol-version)
> +             process-environment)))
>      (apply 'start-process name buffer
>  	   "/bin/sh" "-c"
>  	   (format "stty -nl echo rows %d columns %d sane 2>/dev/null;\

It's a solution, afaict. However, one which is more complex than we
already have. The problem with RHEL probably only effects weird people
like you (and me!) who use a stock bash on an old RHEL, then compile the
latest Emacs. And us weird people could probably circumvent the problem
with term anyway.

You, Stefan and Eli have far more experience here clearly. Happy with
what ever you decide.

Phil