unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Akib Azmain Turja via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 57607@debbugs.gnu.org
Cc: gerd.moellmann@gmail.com
Subject: bug#57607: Feature request: Use the character cell on bottom-right corner of a terminal
Date: Mon, 03 Oct 2022 14:31:53 +0600	[thread overview]
Message-ID: <87r0zpi9za.fsf@disroot.org> (raw)
In-Reply-To: <87tu5i9kbg.fsf@disroot.org> (Akib Azmain Turja via's message of "Thu, 08 Sep 2022 17:28:35 +0600")


[-- Attachment #1.1: Type: text/plain, Size: 978 bytes --]

Akib Azmain Turja via "Bug reports for GNU Emacs, the Swiss army knife
of text editors" <bug-gnu-emacs@gnu.org> writes:

> Gerd Möllmann <gerd.moellmann@gmail.com> writes:
>
>> Akib Azmain Turja <akib@disroot.org> writes:
>>
>>> Gerd Möllmann <gerd.moellmann@gmail.com> writes:
>>>
>>> I mean, what files to change in order to do this?  I think it's
>>> tty_write_glyphs and tty_write_glyphs_with_face in term.c.
>>
>> Are you interested in implementing something?  You could re-open this
>> bug then, no problem.  Or are you maybe just evaluating if you'd like to
>> do it?  Please give a hint.
>
> Yes, I'm interested, because I think it would help me understand how
> Emacs renders text in terminal, which would help me to implement child
> frame in terminal.  But I don't have the time to do it right now.  I
> hope I can start doing this by late October.
>

I have implemented it.  Implementing it was easier than I thought.  Here
is the patch:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: Patch --]
[-- Type: text/x-patch, Size: 11656 bytes --]

From 72af00d8610bdc194062b246aab0e38f22174aeb Mon Sep 17 00:00:00 2001
From: Akib Azmain Turja <akib@disroot.org>
Date: Mon, 3 Oct 2022 14:18:59 +0600
Subject: [PATCH] Write on the bottom-right character cell on terminal

Correctly write on the bottom-right character cell on text terminals
if the terminal allows to do this.  Also fixes bug#57728.

* src/term.c (tty_write_glyphs, tty_write_glyphs_with_face): Write on
the bottom-right character cell if the terminal supports it.

* src/term.c (glyph_on_char_cell_before_last): New static variable to
hold the current glyph on the character cell before the cell on the
bottom-right corner.
---
 src/term.c | 266 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 225 insertions(+), 41 deletions(-)

diff --git a/src/term.c b/src/term.c
index 2e43d892..283a65c4 100644
--- a/src/term.c
+++ b/src/term.c
@@ -718,7 +718,7 @@ encode_terminal_code (struct glyph *src, int src_len,
   return (encode_terminal_dst);
 }
 
-
+static struct glyph glyph_on_char_cell_before_last;
 
 /* An implementation of write_glyphs for termcap frames. */
 
@@ -727,7 +727,7 @@ tty_write_glyphs (struct frame *f, struct glyph *string, int len)
 {
   unsigned char *conversion_buffer;
   struct coding_system *coding;
-  int n, stringlen;
+  int n, stringlen, write_on_last_cell;
 
   struct tty_display_info *tty = FRAME_TTY (f);
 
@@ -737,55 +737,158 @@ tty_write_glyphs (struct frame *f, struct glyph *string, int len)
   /* Don't dare write in last column of bottom line, if Auto-Wrap,
      since that would scroll the whole frame on some terminals.  */
 
-  if (AutoWrap (tty)
-      && curY (tty) + 1 == FRAME_TOTAL_LINES (f)
-      && (curX (tty) + len) == FRAME_COLS (f))
+  write_on_last_cell = (AutoWrap (tty)
+			&& curY (tty) + 1 == FRAME_TOTAL_LINES (f)
+			&& (curX (tty) + len) == FRAME_COLS (f));
+  if (write_on_last_cell)
     len --;
-  if (len <= 0)
-    return;
 
-  cmplus (tty, len);
+  if (FRAME_COLS (f) == 1
+      || !(tty->TS_ins_char || tty->TS_ins_multi_chars
+	   || (tty->TS_insert_mode && tty->TS_end_insert_mode)))
+    write_on_last_cell = false;
+
+  if (len <= 0 && !write_on_last_cell)
+    return;
 
   /* If terminal_coding does any conversion, use it, otherwise use
      safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
      because it always return 1 if the member src_multibyte is 1.  */
-  coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
+  coding = ((FRAME_TERMINAL_CODING (f)->common_flags
+	     & CODING_REQUIRE_ENCODING_MASK)
 	    ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
   /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
      the tail.  */
   coding->mode &= ~CODING_MODE_LAST_BLOCK;
 
-  for (stringlen = len; stringlen != 0; stringlen -= n)
+  if (len > 0)
     {
-      /* Identify a run of glyphs with the same face.  */
-      int face_id = string->face_id;
+      cmplus (tty, len);
+
+      if (write_on_last_cell)
+	glyph_on_char_cell_before_last = string[len - 1];
+
+      for (stringlen = len; stringlen != 0; stringlen -= n)
+	{
+	  /* Identify a run of glyphs with the same face.  */
+	  int face_id = string->face_id;
+
+	  for (n = 1; n < stringlen; ++n)
+	    if (string[n].face_id != face_id)
+	      break;
+
+	  /* Turn appearance modes of the face of the run on.  */
+	  tty_highlight_if_desired (tty);
+	  turn_on_face (f, face_id);
 
-      for (n = 1; n < stringlen; ++n)
-	if (string[n].face_id != face_id)
-	  break;
+	  if (n == stringlen && !write_on_last_cell)
+	    /* This is the last run.  */
+	    coding->mode |= CODING_MODE_LAST_BLOCK;
+	  conversion_buffer = encode_terminal_code (string, n,
+						    coding);
+	  if (coding->produced > 0)
+	    {
+	      block_input ();
+	      fwrite (conversion_buffer, 1, coding->produced,
+		      tty->output);
+	      clearerr (tty->output);
+	      if (tty->termscript)
+		fwrite (conversion_buffer, 1, coding->produced,
+			tty->termscript);
+	      unblock_input ();
+	    }
+	  string += n;
 
-      /* Turn appearance modes of the face of the run on.  */
+	  /* Turn appearance modes off.  */
+	  turn_off_face (f, face_id);
+	  tty_turn_off_highlight (tty);
+	}
+    }
+
+  /* Write on the bottom-right corner. */
+  if (write_on_last_cell)
+    {
+      /* Go to the previous position. */
+      cmgoto (tty, curY (tty), curX (tty) - 1);
+      cmplus (tty, 1);
+
+      int face_id = string->face_id;
+
+      /* Turn appearance modes of the face.  */
       tty_highlight_if_desired (tty);
       turn_on_face (f, face_id);
 
-      if (n == stringlen)
-	/* This is the last run.  */
-	coding->mode |= CODING_MODE_LAST_BLOCK;
-      conversion_buffer = encode_terminal_code (string, n, coding);
+      conversion_buffer = encode_terminal_code (string, 1, coding);
       if (coding->produced > 0)
 	{
 	  block_input ();
-	  fwrite (conversion_buffer, 1, coding->produced, tty->output);
+	  fwrite (conversion_buffer, 1, coding->produced,
+		  tty->output);
 	  clearerr (tty->output);
 	  if (tty->termscript)
-	    fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
+	    fwrite (conversion_buffer, 1, coding->produced,
+		    tty->termscript);
+	  unblock_input ();
+	}
+
+      /* Go to the previous position. */
+      cmgoto (tty, curY (tty), curX (tty) - 1);
+      cmplus (tty, 1);
+
+      /* Arrange that after writing the next glyph, the glyph on the
+         current character cell is somehow pushed to the bottom-right
+	 corner. */
+      if (tty->TS_ins_char)
+	{
+	  OUTPUT1 (tty, tty->TS_ins_char);
+	}
+      else if (tty->TS_ins_multi_chars)
+	{
+	  char *buf = tparam (tty->TS_ins_multi_chars, 0, 0, 1, 0,
+			      0, 0);
+	  OUTPUT1 (tty, buf);
+	  xfree (buf);
+	}
+      else
+	{
+	  tty_turn_on_insert (tty);
+	}
+
+      if (face_id != glyph_on_char_cell_before_last.face_id)
+	{
+
+	  /* Turn appearance modes of the face.  */
+	  turn_off_face (f, face_id);
+	  tty_turn_off_highlight (tty);
+
+	  face_id = glyph_on_char_cell_before_last.face_id;
+
+	  tty_highlight_if_desired (tty);
+	  turn_on_face (f, face_id);
+	}
+
+      /* This is the last run.  */
+      coding->mode |= CODING_MODE_LAST_BLOCK;
+      conversion_buffer
+	= encode_terminal_code (&glyph_on_char_cell_before_last, 1,
+				coding);
+      if (coding->produced > 0)
+	{
+	  block_input ();
+	  fwrite (conversion_buffer, 1, coding->produced,
+		  tty->output);
+	  clearerr (tty->output);
+	  if (tty->termscript)
+	    fwrite (conversion_buffer, 1, coding->produced,
+		    tty->termscript);
 	  unblock_input ();
 	}
-      string += n;
 
       /* Turn appearance modes off.  */
       turn_off_face (f, face_id);
       tty_turn_off_highlight (tty);
+
+      tty_turn_off_insert (tty);
     }
 
   cmcheckmagic (tty);
@@ -799,6 +902,7 @@ tty_write_glyphs_with_face (register struct frame *f, register struct glyph *str
 {
   unsigned char *conversion_buffer;
   struct coding_system *coding;
+  int write_on_last_cell;
 
   struct tty_display_info *tty = FRAME_TTY (f);
 
@@ -808,38 +912,118 @@ tty_write_glyphs_with_face (register struct frame *f, register struct glyph *str
   /* Don't dare write in last column of bottom line, if Auto-Wrap,
      since that would scroll the whole frame on some terminals.  */
 
-  if (AutoWrap (tty)
-      && curY (tty) + 1 == FRAME_TOTAL_LINES (f)
-      && (curX (tty) + len) == FRAME_COLS (f))
+  write_on_last_cell = (AutoWrap (tty)
+			&& curY (tty) + 1 == FRAME_TOTAL_LINES (f)
+			&& (curX (tty) + len) == FRAME_COLS (f));
+  if (write_on_last_cell)
     len --;
-  if (len <= 0)
-    return;
 
-  cmplus (tty, len);
+  if (FRAME_COLS (f) == 1
+      || !(tty->TS_ins_char || tty->TS_ins_multi_chars
+	   || (tty->TS_insert_mode && tty->TS_end_insert_mode)))
+    write_on_last_cell = false;
+
+  if (len <= 0 && !write_on_last_cell)
+    return;
 
   /* If terminal_coding does any conversion, use it, otherwise use
      safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
      because it always return 1 if the member src_multibyte is 1.  */
-  coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
+  coding = ((FRAME_TERMINAL_CODING (f)->common_flags
+	     & CODING_REQUIRE_ENCODING_MASK)
 	    ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
-  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
-     the tail.  */
+
+  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only
+     at the tail.  */
   coding->mode &= ~CODING_MODE_LAST_BLOCK;
 
   /* Turn appearance modes of the face.  */
   tty_highlight_if_desired (tty);
   turn_on_face (f, face_id);
 
-  coding->mode |= CODING_MODE_LAST_BLOCK;
-  conversion_buffer = encode_terminal_code (string, len, coding);
-  if (coding->produced > 0)
+  if (len > 0)
+    {
+      cmplus (tty, len);
+      if (write_on_last_cell)
+	glyph_on_char_cell_before_last = string[len - 1];
+      else
+	coding->mode |= CODING_MODE_LAST_BLOCK;
+
+      conversion_buffer = encode_terminal_code (string, len, coding);
+      if (coding->produced > 0)
+	{
+	  block_input ();
+	  fwrite (conversion_buffer, 1, coding->produced,
+		  tty->output);
+	  clearerr (tty->output);
+	  if (tty->termscript)
+	    fwrite (conversion_buffer, 1, coding->produced,
+		    tty->termscript);
+	  unblock_input ();
+	}
+    }
+
+  /* Write on the bottom-right corner. */
+  if (write_on_last_cell)
     {
-      block_input ();
-      fwrite (conversion_buffer, 1, coding->produced, tty->output);
-      clearerr (tty->output);
-      if (tty->termscript)
-	fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
-      unblock_input ();
+      /* Go to the previous position. */
+      cmgoto (tty, curY (tty), curX (tty) - 1);
+      cmplus (tty, 1);
+
+      conversion_buffer = encode_terminal_code (string, 1, coding);
+      if (coding->produced > 0)
+	{
+	  block_input ();
+	  fwrite (conversion_buffer, 1, coding->produced,
+		  tty->output);
+	  clearerr (tty->output);
+	  if (tty->termscript)
+	    fwrite (conversion_buffer, 1, coding->produced,
+		    tty->termscript);
+	  unblock_input ();
+	}
+
+      /* Go to the previous position. */
+      cmgoto (tty, curY (tty), curX (tty) - 1);
+      cmplus (tty, 1);
+
+      /* Arrange that after writing the next glyph, the glyph on the
+         current character cell is somehow pushed to the bottom-right
+	 corner. */
+      if (tty->TS_ins_char)
+	{
+	  OUTPUT1 (tty, tty->TS_ins_char);
+	}
+      else if (tty->TS_ins_multi_chars)
+	{
+	  char *buf = tparam (tty->TS_ins_multi_chars, 0, 0, 1,
+			      0, 0, 0);
+	  OUTPUT1 (tty, buf);
+	  xfree (buf);
+	}
+      else
+	{
+	  tty_turn_on_insert (tty);
+	}
+
+      /* This is the last run.  */
+      coding->mode |= CODING_MODE_LAST_BLOCK;
+      conversion_buffer
+	= encode_terminal_code (&glyph_on_char_cell_before_last, 1,
+				coding);
+      if (coding->produced > 0)
+	{
+	  block_input ();
+	  fwrite (conversion_buffer, 1, coding->produced,
+		  tty->output);
+	  clearerr (tty->output);
+	  if (tty->termscript)
+	    fwrite (conversion_buffer, 1, coding->produced,
+		    tty->termscript);
+	  unblock_input ();
+	}
+
+      tty_turn_off_insert (tty);
     }
 
   /* Turn appearance modes off.  */
-- 
2.37.1


[-- Attachment #1.3: Type: text/plain, Size: 757 bytes --]


>>
>> The places you mention sound right.  It's probably all in term.c.  But
>> I'd like to add, that in principle all places writing to the terminal
>> have to be checked at least.
>>
>> And corner cases have to be taken into consideration :-).  Pulling
>> something out of thin air: What happens if we write RGRG to the
>> bottom-right corner, where R is one red char, and G is 1 green char,
>> say.

I tried "RGRG\", where "R" is red, "G" is green, and "\" is the (white)
continuation glyph.  Looks like this patch also fixes bug#57728.

-- 
Akib Azmain Turja

Find me on Mastodon at @akib@hostux.social.

This message is signed by me with my GnuPG key.  Its fingerprint is:

    7001 8CE5 819F 17A3 BBA6  66AF E74F 0EFA 922A E7F5

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

  parent reply	other threads:[~2022-10-03  8:31 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87wnakl5md.fsf@disroot.org>
2022-09-06  6:59 ` bug#57607: Feature request: Use the character cell on bottom-right corner of a terminal Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-06  8:03   ` Gerd Möllmann
2022-09-06 10:07     ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-06 12:00       ` Gerd Möllmann
2022-09-06 15:10         ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-07  4:58           ` Gerd Möllmann
2022-09-07 12:46             ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-07 12:58               ` Gerd Möllmann
2022-09-07 15:39             ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-07 17:26             ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-08  5:49               ` Gerd Möllmann
2022-09-08 11:28                 ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-08 12:35                   ` Gerd Möllmann
2022-09-08 14:47                     ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-11  9:00                       ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-11 10:08                         ` Gerd Möllmann
2022-09-11 10:39                           ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-03  8:31                   ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-10-03 19:41                     ` Eli Zaretskii
2022-10-04  7:22                       ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-04  8:07                         ` Eli Zaretskii
2022-10-04 12:00                           ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-04 12:54                             ` Eli Zaretskii
2022-10-07 13:10                               ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-07 19:30                                 ` Eli Zaretskii
2022-10-04 13:05                             ` Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0zpi9za.fsf@disroot.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=57607@debbugs.gnu.org \
    --cc=akib@disroot.org \
    --cc=gerd.moellmann@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).