From 2a50a3ef3ccf269cdbaa5d5eccf2697a7f3a65ce Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 04:59:54 -0500 Subject: [PATCH 1/6] Use VT100-style alternate screen for display; init more variables. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- kilo.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/kilo.c b/kilo.c index 0d8aef4e..13e34a87 100644 --- a/kilo.c +++ b/kilo.c @@ -101,6 +101,7 @@ struct editorConfig { int screencols; /* Number of cols that we can show */ int numrows; /* Number of rows */ int rawmode; /* Is terminal raw mode enabled? */ + int altscr; /* Is terminal alternate-screen selected? */ erow *row; /* Rows */ int dirty; /* File modified but not saved. */ char *filename; /* Currently open filename */ @@ -212,6 +213,26 @@ void disableRawMode(int fd) { /* Called at exit to avoid remaining in raw mode. */ void editorAtExit(void) { disableRawMode(STDIN_FILENO); + + /* Disable alternate screen. */ + if( E.altscr ) + { + /* "?47l" ~1978 VT100 DECSET magic. "?1049l" is similar xterm magic */ + /* from... some indeterminate time, possibly even before X Windows */ + /* existed. */ + /* To instead enable, use 'h' instead of 'l': note that */ + /* initEditor() does the enabling already. */ + const char altscren[] = "\x1b[?47l"; + const int altscren_len = sizeof( altscren ); + if (write(STDOUT_FILENO, "\x1b[?47l", altscren_len) != altscren_len) { + perror("Unable to deselect the alternate screen display buffer"); + perror("please type" ); + perror(" echo -e \"\\e[?47l\""); + perror("and then hit your enter key" ); + exit(1); + } + E.altscr = 0; + } } /* Raw mode: 1960 magic shit. */ @@ -1279,11 +1300,26 @@ void initEditor(void) { E.cy = 0; E.rowoff = 0; E.coloff = 0; + E.screenrows = 0; + E.screencols = 0; E.numrows = 0; + E.rawmode = 0; + E.altscr = 0; E.row = NULL; E.dirty = 0; E.filename = NULL; E.syntax = NULL; + if( !E.altscr ) + { + /* Activate alternate screen. To disable, use 'l' instead of 'h'. */ + const char altscren[] = "\x1b[?47h\n"; + const int altscren_len = sizeof( altscren ); + if ( write(STDOUT_FILENO, altscren, altscren_len) != altscren_len) { + perror("Unable to select the alternate screen display buffer"); + exit(1); + } + E.altscr = 1; + } updateWindowSize(); signal(SIGWINCH, handleSigWinCh); } From 60e3c4d65fe9855a974f255b0a5a8ee06c464754 Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 05:19:48 -0500 Subject: [PATCH 2/6] Restore previous context on exit. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- kilo.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index 13e34a87..051f0c7c 100644 --- a/kilo.c +++ b/kilo.c @@ -222,12 +222,12 @@ void editorAtExit(void) { /* existed. */ /* To instead enable, use 'h' instead of 'l': note that */ /* initEditor() does the enabling already. */ - const char altscren[] = "\x1b[?47l"; + const char altscren[] = "\x1b[?1049l"; const int altscren_len = sizeof( altscren ); - if (write(STDOUT_FILENO, "\x1b[?47l", altscren_len) != altscren_len) { + if (write(STDOUT_FILENO, altscren, altscren_len) != altscren_len) { perror("Unable to deselect the alternate screen display buffer"); perror("please type" ); - perror(" echo -e \"\\e[?47l\""); + perror(" echo -e \"\\e[?1049l\""); perror("and then hit your enter key" ); exit(1); } @@ -1312,7 +1312,7 @@ void initEditor(void) { if( !E.altscr ) { /* Activate alternate screen. To disable, use 'l' instead of 'h'. */ - const char altscren[] = "\x1b[?47h\n"; + const char altscren[] = "\x1b[?1049h\n"; const int altscren_len = sizeof( altscren ); if ( write(STDOUT_FILENO, altscren, altscren_len) != altscren_len) { perror("Unable to select the alternate screen display buffer"); From 3a275ea926a995687ce111547d9669dcac20326a Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 05:36:36 -0500 Subject: [PATCH 3/6] Make alternate-screen usage optional. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- kilo.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/kilo.c b/kilo.c index 051f0c7c..5b61bfb7 100644 --- a/kilo.c +++ b/kilo.c @@ -94,6 +94,8 @@ typedef struct hlcolor { } hlcolor; struct editorConfig { + int no_altscr; /* Forbid usage of the alternate-screen. */ + int cx,cy; /* Cursor x and y position in characters */ int rowoff; /* Offset of row displayed. */ int coloff; /* Offset of column displayed. */ @@ -1309,7 +1311,7 @@ void initEditor(void) { E.dirty = 0; E.filename = NULL; E.syntax = NULL; - if( !E.altscr ) + if( !E.altscr && !E.no_altscr ) { /* Activate alternate screen. To disable, use 'l' instead of 'h'. */ const char altscren[] = "\x1b[?1049h\n"; @@ -1325,11 +1327,25 @@ void initEditor(void) { } int main(int argc, char **argv) { - if (argc != 2) { - fprintf(stderr,"Usage: kilo \n"); + const char noaltscr_opt[] = "--no-alt-screen"; + if (argc < 2 || argc > 3) { + fprintf(stderr,"Usage: kilo [%s]\n",noaltscr_opt); exit(1); } + if(argc == 3) { + /* Surpress usage of the alternate screen: useful if you */ + /* want to keep info displayed on exit. */ + if( strcmp( noaltscr_opt, argv[2] ) != 0 ) { + perror("Unfamiliar option:"); + fprintf(stderr," %s",argv[2]); + exit(1); + } + E.no_altscr = 1; + } else { + E.no_altscr = 0; + } + initEditor(); editorSelectSyntaxHighlight(argv[1]); editorOpen(argv[1]); From 0ecd37de1f89cde0b42926150605eb831575c1f9 Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 05:58:53 -0500 Subject: [PATCH 4/6] Fix formating for --no-alt-screen, so the shell resumes at the bottom. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- kilo.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kilo.c b/kilo.c index 5b61bfb7..916d45c1 100644 --- a/kilo.c +++ b/kilo.c @@ -217,8 +217,7 @@ void editorAtExit(void) { disableRawMode(STDIN_FILENO); /* Disable alternate screen. */ - if( E.altscr ) - { + if( E.altscr ) { /* "?47l" ~1978 VT100 DECSET magic. "?1049l" is similar xterm magic */ /* from... some indeterminate time, possibly even before X Windows */ /* existed. */ @@ -234,6 +233,11 @@ void editorAtExit(void) { exit(1); } E.altscr = 0; + } else if( E.no_altscr ) { + /* If we aren't using the alternate-screen, move the cursor to the */ + /* end of the screen and force a line-advance instead, to prepare */ + /* for the return to the CLI. */ + printf("\x1b[%d;%dH\n\n",E.screenrows+1,E.screencols+1); } } From 169fc796255efc2c3650dc97e351c8ebf4b7dd26 Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 06:34:14 -0500 Subject: [PATCH 5/6] Perform cursory check of TERM env var; update TODO. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- TODO | 1 - kilo.c | 18 +++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index 95ae28b9..0005b937 100644 --- a/TODO +++ b/TODO @@ -6,5 +6,4 @@ IMPORTANT MAYBE === -* Send alternate screen sequences if TERM=xterm: "\033[?1049h" and "\033[?1049l" * Improve internals to be more understandable. diff --git a/kilo.c b/kilo.c index 916d45c1..89091d6f 100644 --- a/kilo.c +++ b/kilo.c @@ -1317,14 +1317,18 @@ void initEditor(void) { E.syntax = NULL; if( !E.altscr && !E.no_altscr ) { - /* Activate alternate screen. To disable, use 'l' instead of 'h'. */ - const char altscren[] = "\x1b[?1049h\n"; - const int altscren_len = sizeof( altscren ); - if ( write(STDOUT_FILENO, altscren, altscren_len) != altscren_len) { - perror("Unable to select the alternate screen display buffer"); - exit(1); + char *termstr = getenv( "TERM" ); + if( termstr && strstr( termstr, "xterm" ) ) + { + /* Activate alternate screen. To disable, use 'l' instead of 'h'. */ + const char altscren[] = "\x1b[?1049h\n"; + const int altscren_len = sizeof( altscren ); + if ( write(STDOUT_FILENO, altscren, altscren_len) != altscren_len) { + perror("Unable to select the alternate screen display buffer"); + exit(1); + } + E.altscr = 1; } - E.altscr = 1; } updateWindowSize(); signal(SIGWINCH, handleSigWinCh); From 5cc0706f3043ab51f9c125bdc10e6c11e28bbace Mon Sep 17 00:00:00 2001 From: Jam555 <3349478+jam555@users.noreply.github.com> Date: Tue, 13 May 2025 06:37:55 -0500 Subject: [PATCH 6/6] Create contributor & maintainer files. I (Jam555) hereby license the contributions that, as of 13/May/2025, that I have made to the Kilo Text Editor under the the BSD 2 clause license. Signed-off-by: Jam555 <3349478+jam555@users.noreply.github.com> --- CONTRIBUTORS.txt | 14 ++++++++++++++ MAINTAINERS.txt | 7 +++++++ 2 files changed, 21 insertions(+) create mode 100644 CONTRIBUTORS.txt create mode 100644 MAINTAINERS.txt diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt new file mode 100644 index 00000000..e59c3fd7 --- /dev/null +++ b/CONTRIBUTORS.txt @@ -0,0 +1,14 @@ +# This is the list of the Kilo text editor's contributors. This file +# is styled after the "humans.txt" style for websites. +# +# This does not necessarily list everyone who has contributed code. +# To see the full list of contributors, see the revision history in +# source control. For the list of active maintainers, see +# MAINTAINERS.txt + + Maintainer: Salvatore Sanfilippo + Contact: + + Alt-screen contributor: jam555 + Contact: <3349478+jam555@users.noreply.github.com> + From: USA diff --git a/MAINTAINERS.txt b/MAINTAINERS.txt new file mode 100644 index 00000000..292b6ec1 --- /dev/null +++ b/MAINTAINERS.txt @@ -0,0 +1,7 @@ +# This is the list of the Kilo text editor's maintainers. +# +# This does not necessarily list everyone who has contributed code. +# To see the full list of contributors, see the revision history in +# source control. + +Salvatore Sanfilippo