From eb997019c18d025431242ac7369fc6a7e1543a25 Mon Sep 17 00:00:00 2001
From: Jason Hood <jadoxa@yahoo.com.au>
Date: Sun, 3 Dec 2017 12:50:13 +1000
Subject: [PATCH] Add IND, NEL and RI

These controls always operate on the buffer, in keeping with LF.
---
 ANSI.c        | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--
 ansicon.c     |  2 +-
 readme.txt    | 12 +++++---
 sequences.txt |  4 +++
 4 files changed, 88 insertions(+), 7 deletions(-)

diff --git a/ANSI.c b/ANSI.c
index ee16b21..f4217df 100644
--- a/ANSI.c
+++ b/ANSI.c
@@ -152,7 +152,7 @@
     remove wcstok, avoiding potential interference with the host;
     similarly, use a private heap instead of malloc.
 
-  v1.80, 26 October to 30 November, 2017:
+  v1.80, 26 October to 3 December, 2017:
     fix unloading;
     revert back to (re)storing buffer cursor position;
     increase cache to five handles;
@@ -165,7 +165,8 @@
     use intermediate byte '+' to use buffer, not window;
     ESC followed by a control character will display that character;
     added palette sequences;
-    change the scan lines in the graphics set to their actual Unicode chars.
+    change the scan lines in the graphics set to their actual Unicode chars;
+    added IND, NEL & RI (using buffer, in keeping with LF).
 */
 
 #include "ansicon.h"
@@ -1418,6 +1419,61 @@ void InterpretEscSeq( void )
   }
 }
 
+
+void ScrollDown( void )
+{
+  CONSOLE_SCREEN_BUFFER_INFO Info;
+  SMALL_RECT Rect;
+  COORD      Pos;
+  CHAR_INFO  CharInfo;
+
+  GetConsoleScreenBufferInfo( hConOut, &Info );
+  if (CUR.Y == LAST)
+  {
+    Rect.Left = LEFT;
+    Rect.Right = RIGHT;
+    Rect.Top = 1;
+    Rect.Bottom = LAST;
+    Pos.X = Pos.Y = 0;
+    CharInfo.Char.UnicodeChar = ' ';
+    CharInfo.Attributes = Info.wAttributes;
+    ScrollConsoleScreenBuffer( hConOut, &Rect, NULL, Pos, &CharInfo );
+  }
+  else
+  {
+    ++CUR.Y;
+    SetConsoleCursorPosition( hConOut, CUR );
+  }
+}
+
+void ScrollUp( void )
+{
+  CONSOLE_SCREEN_BUFFER_INFO Info;
+  SMALL_RECT Rect;
+  COORD      Pos;
+  CHAR_INFO  CharInfo;
+
+  GetConsoleScreenBufferInfo( hConOut, &Info );
+  if (CUR.Y == 0)
+  {
+    Rect.Left = LEFT;
+    Rect.Right = RIGHT;
+    Rect.Top = 0;
+    Rect.Bottom = LAST - 1;
+    Pos.X = 0;
+    Pos.Y = 1;
+    CharInfo.Char.UnicodeChar = ' ';
+    CharInfo.Attributes = Info.wAttributes;
+    ScrollConsoleScreenBuffer( hConOut, &Rect, NULL, Pos, &CharInfo );
+  }
+  else
+  {
+    --CUR.Y;
+    SetConsoleCursorPosition( hConOut, CUR );
+  }
+}
+
+
 DWORD WINAPI BellThread( LPVOID param )
 {
   // XP doesn't support SND_SENTRY, so if it fails, try without.
@@ -1491,6 +1547,23 @@ ParseAndPrintString( HANDLE hDev,
 	suffix2 = c;
       else if (suffix2 != 0)
 	state = 1;
+      else if (c == 'E')        // NEL Next Line
+      {
+	PushBuffer( '\n' );
+	state = 1;
+      }
+      else if (c == 'D')        // IND Index
+      {
+	FlushBuffer();
+	ScrollDown();
+	state = 1;
+      }
+      else if (c == 'M')        // RI  Reverse Index
+      {
+	FlushBuffer();
+	ScrollUp();
+        state = 1;
+      }
       else if (c == '[' ||      // CSI Control Sequence Introducer
 	       c == ']')        // OSC Operating System Command
       {
diff --git a/ansicon.c b/ansicon.c
index 5cc29fb..4871428 100644
--- a/ansicon.c
+++ b/ansicon.c
@@ -91,7 +91,7 @@
     use -pu to unload from the parent.
 */
 
-#define PDATE L"30 November, 2017"
+#define PDATE L"3 December, 2017"
 
 #include "ansicon.h"
 #include "version.h"
diff --git a/readme.txt b/readme.txt
index fa5599b..6b061e0 100644
--- a/readme.txt
+++ b/readme.txt
@@ -189,9 +189,12 @@ Sequences Recognised
 	\e[#;#f 	HVP	Character And Line Position
 	\e[#@		ICH	Insert Character
 	\e[#L		IL	Insert Line
+	\eD		IND	Index
 	SI		LS0	Locking-shift Zero (see below)
 	SO		LS1	Locking-shift One
+	\eE		NEL	Next Line
 	\e[#b		REP	Repeat
+	\eM		RI	Reverse Index
 	\e[#;#;#m	SGR	Select Graphic Rendition
 	\e[#d		VPA	Line Position Absolute
 	\e[#k		VPB	Line Position Backward
@@ -304,7 +307,7 @@ Version History
 
     Legend: + added, - bug-fixed, * changed.
 
-    1.80 - 30 November, 2017:
+    1.80 - 3 December, 2017:
     - fix unloading;
     - fix -e et al when redirecting to CON;
     - hook CreateFile and CreateConsoleScreenBuffer to force read/write access
@@ -319,7 +322,8 @@ Version History
     + added Play Sound DECPS;
     + added '+' intermediate byte to use the buffer, rather than the window;
     + added palette sequences;
-    + added -pu to unload from the parent.
+    + added -pu to unload from the parent;
+    + added IND, NEL and RI.
 
     1.72 - 24 December, 2015:
     - handle STD_OUTPUT_HANDLE & STD_ERROR_HANDLE in WriteFile;
@@ -547,5 +551,5 @@ Distribution
     in LICENSE.txt.
 
 
-=============================
-Jason Hood, 30 November, 2017.
+============================
+Jason Hood, 3 December, 2017.
diff --git a/sequences.txt b/sequences.txt
index a13fb3a..4c94b72 100644
--- a/sequences.txt
+++ b/sequences.txt
@@ -71,6 +71,10 @@ the Windows default beep (but only if it's not already playing).
 [b	repeat the previous character
 [#b	repeat the previous character # times
 
+D	move cursor down one line (scroll if necessary; always uses buffer)
+E	same as LF
+M	move cursor up one line (scroll if necessary; always uses buffer)
+
 [A	move cursor up one line
 [#A	move cursor up # lines
 [B	move cursor down one line