commit:     075761b394201951e2948be8730f8cb8e7fc8122
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Jun 11 11:26:16 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Jun 11 11:36:13 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=075761b3

Account for the buggy CHA implementation in Emacs

In Emacs, M-x term opens an "eterm-color" terminal, whose
implementation of the CHA (ECMA-48 CSI) sequence suffers from an
off-by-one error. Work around it. Note that Eterm does not have this
same bug.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 functions.sh.in | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 6f1ee75..d2baf5d 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -335,15 +335,24 @@ _eend()
                # Calculate the column at which the indicator may be printed.
                indent=$(( genfun_cols - genfun_x - 6 ))
 
+               # In Emacs, M-x term opens an "eterm-color" terminal, whose
+               # implementation of the CHA (ECMA-48 CSI) sequence suffers from
+               # an off-by-one error.
+               if [ "${INSIDE_EMACS}" ] && [ "${TERM}" = "eterm-color" ]; then
+                       offset=-1
+               else
+                       offset=0
+               fi
+
                # Determine whether the cursor needs to be repositioned.
                if [ "${indent}" -gt 0 ]; then
                        # Use CHA (ECMA-48 CSI) to move the cursor to the right.
-                       printf '\033[%dG' "$(( genfun_x + indent ))"
+                       printf '\033[%dG' "$(( genfun_x + indent + offset ))"
                elif [ "${indent}" -lt 0 ]; then
                        # The indent is negative, meaning that there is not
                        # enough room. Arrange for the indicator to be printed
                        # on the next line instead.
-                       printf '\n\033[%dG' "$(( genfun_cols - 6 ))"
+                       printf '\n\033[%dG' "$(( genfun_cols - 6 + offset ))"
                fi
 
                # Finally, print the indicator.

Reply via email to