nne

No-nonsense editor under 777 SLOC of ANSI C
git clone git://git.luxferre.top/nne.git
Log | Files | Refs | README | LICENSE

commit b4b44fa814bdbac1f692379b1ae31264b7b140ab
parent 095793af4ba0e80d28646628d0b94385ce514f6e
Author: luxferre <lux@ferre>
Date:   Fri, 28 Jul 2023 13:58:17 +0300

Added some more compatibility modes

Diffstat:
MREADME.md | 5+++--
Mnne.c | 16++++++++++------
2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md @@ -37,10 +37,11 @@ _Note: syntax highlighing and (visual) line numbering are not implemented for tw Just run the usual CLI compilation process (replace `cc` with the C compiler of your choice and adjust flags if required): ``` -cc -std=c89 -Os -O2 -s nne.c -o nne [-DNNE_IOBUFSZ=n] [-DNNE_TABWIDTH=m] +cc -std=c89 -Os -O2 -s nne.c -o nne [-DNNE_IOBUFSZ=n] [-DNNE_TABWIDTH=m] [-DNNE_NO_ALTBUF] ``` - `NNE_IOBUFSZ` defines the internal command buffers size (in characters, default 1000) - `NNE_TABWIDTH` defines the amount of spaces that a tabulation key/char represents +- `NNE_NO_ALTBUF` disables the use of advanced terminal sequence like alternate buffer switching, cursor hiding and UTF-8 mode enforcement (this macro is useful on some older terminals/OSes that don't support these sequences and can't correctly process them) The GCC and Clang build flags are the same as above. Below are tested examples for some other C compilers. @@ -120,7 +121,7 @@ Help |`mod h` |Displays on-screen help (press Return to Well, there are several reasons why nne was created: -1. **Minimum overhead**. A text editor is the most important tool on every system, and it's crucial that it does not itself get in the way in terms of resource consumption. Most well-known and established text editors, however, are already bloated beyond repair, up to the point that x86_64 static builds of Vim and Vis against musl libc are sized 3433600 and 644288 bytes respectively. And these are only two examples. On top of that, their codebase already is so large that it cannot be easily (or at all) maintained by a single person. On the contrary, nne weighs around 68k bytes when statically linked with musl, and the sub-1000 SLOC limit (that actually turned out to be sub-770) makes it easy to comprehend by anyone familiar with ANSI C whoever will be reading its source code. +1. **Minimum overhead**. A text editor is the most important tool on every system, and it's crucial that it does not itself get in the way in terms of resource consumption. Most well-known and established text editors, however, are already bloated beyond repair, up to the point that x86_64 static builds of Vim and Vis against musl libc are sized 3433600 and 644288 bytes respectively. And these are only two examples. On top of that, their codebase already is so large that it cannot be easily (or at all) maintained by a single person. On the contrary, nne weighs around 68k bytes when statically linked with musl, and the sub-1000 SLOC limit (that actually turned out to be sub-777) makes it easy to comprehend by anyone familiar with ANSI C whoever will be reading its source code. 2. **Maximum portability**. This editor is designed to be source-compatible with any POSIX environment and with any architecture a POSIX environment can run on. There is no OS-specific code and no external dependencies. You don't need to find or build any libtermkey, terminfo, ncurses and other nonsense for the target architecture you want to compile nne for. It also doesn't require a specific build system: just a simple command line to compile a single file. By the way, it also doesn't contain any compiler-specific quirks: any C89-compatible compiler can build nne binary in a POSIX environment (GCC, Clang, zig cc, tcc). 3. **Maximum freedom**. Public domain deserves a decent lightweight text editor, just like it deserves SQLite, oksh and pdpmake. Besides mg (whose portability is questionable as of now), vce (that can't into UTF-8) and ue (that is straight up unusable on modern terminals), there were no notable text editors released into public domain. diff --git a/nne.c b/nne.c @@ -34,19 +34,23 @@ #ifndef SIGWINCH #define SIGWINCH 28 #endif -#ifndef IUTF8 -#define IUTF8 0x4000 -#endif /* terminal control macros (constants) */ #define ERESET "\x1b[0m" /* reset the styling */ #define CLS "\x1b[2J" /* clear the entire screen */ #define LINECLR "\x1b[2K" /* clear the current line */ #define CURRESET "\x1b[0;0H" /* reset the visual cursor */ +#ifdef NNE_NO_ALTBUF /* can be defined for legacy terminals */ +#define CURSHOW "" +#define CURHIDE "" +#define ALTBUFON "" +#define ALTBUFOFF "" +#else #define CURSHOW "\x1b[?25h" /* show the cursor */ #define CURHIDE "\x1b[?25l" /* hide the cursor */ -#define ALTBUFON "\x1b[?47h" /* turn on alternate screen */ +#define ALTBUFON "\x1b[?47h\x1b%G" /* turn on alternate screen + UTF-8 */ #define ALTBUFOFF "\x1b[?47l" /* turn off alternate screen */ +#endif /* terminal control macros (sprintf templates) */ #define CURSET "\x1b[%03u;%03uH" /* set the cursor position (line;col) */ @@ -847,7 +851,7 @@ int nne_action(int key) { int main(int argc, char* argv[]) { /* editor entry point */ /* use the alternative screen buffer and enable UTF-8 */ - nnputs(ALTBUFON CLS "\x1b%G\x1b[?7h"); + nnputs(ALTBUFON CLS "\x1b[?7h"); /* prepare screen */ tcgetattr(0, &tty_opts_backup); atexit(&cleanup); @@ -860,7 +864,7 @@ int main(int argc, char* argv[]) { /* editor entry point */ tty_opts_raw.c_cflag |= CS8; tty_opts_raw.c_cc[VMIN] = 0; tty_opts_raw.c_cc[VTIME] = 1; - tty_opts_raw.c_iflag |= IUTF8; + tty_opts_raw.c_iflag |= 0x4000; /* IUTF8 */ tcsetattr(0, TCSANOW, &tty_opts_raw); nne_scrbuf = malloc(0); /* allocate the minimum for screen */ resizehandler(SIGWINCH); /* populate the dimensions now */