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 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 0d8aef4e..89091d6f 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. */ @@ -101,6 +103,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 +215,30 @@ 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[?1049l"; + const int altscren_len = sizeof( altscren ); + 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[?1049l\""); + perror("and then hit your enter key" ); + 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); + } } /* Raw mode: 1960 magic shit. */ @@ -1279,21 +1306,54 @@ 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 && !E.no_altscr ) + { + 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; + } + } updateWindowSize(); signal(SIGWINCH, handleSigWinCh); } 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]);