#+TITLE: bjoterm: the terminal for Bjolang programs * Goal A package for programs that use the terminal directly: a line editor for a shell or a REPL, and full-screen programs. It owns the terminal while it is in raw mode, and gives it back as it found it. *Phase 1 implemented (2026-11-30). Where the code differs from the text below:* - Ctrl-J (byte 10) is ~(KeyChar #\j)~ with ~ctrl?~, not ~KeyEnter~: a REPL can bind it to a new line. Byte 8 is ~KeyBackspace~ with ~ctrl?~. - Bracketed paste is the program's to turn on and off: leaving raw mode writes nothing. - SIGTERM, SIGHUP and SIGQUIT end the process with no ~finally~ and no ~ProcessExit~ (see ~MainExit~), so the glue restores the modes in a handler of its own, and does not cancel the signal. - SIGTSTP in raw mode with ~#:signals? #t~ restores the modes and stops the process with SIGSTOP. - ~decode-input~ is exported: the decoder on bytes, for tests and recorded input. - ~read-event~ is ~#:sync~. Each read polls on a thread of its own (~LongRunning~). An async form for fibers is for phase 2. - Under ~script~, a child that stops makes ~script~ stop itself; the test of stop and continue sends SIGCONT to both. *Phase 2 implemented (2026-11-30): (bjoterm line).* - ~line-read~ returns ~(LineText text)~, ~LineEof~ or ~LineCancelled~. Options of ~make-line-editor~: ~#:completer~, ~#:input-done?~, ~#:history-size~; of ~line-read~: ~#:continuation~. - The keys are those of Emacs and readline, listed at ~line-read~. Up and Down move between the lines of the input, and reach the history only on its first and last line. - The buffer is a vector of grapheme clusters ((std unicode grapheme)). Widths follow ~wcwidth~ with a table of wide ranges; a C0 control shows as ~^X~, a tab as spaces to the next multiple of 8. - The editor breaks lines itself: a line break where the next cluster does not fit, and after a line that ends at the edge. The whole input is drawn again after each key, from the prompt's row. - Each key is a pure step from one state to the next (~line-step~), so ~simulate-line~ tests the keys without a terminal. - History files have one entry per line, with "\n" and "\\" escaped. - Reflow (2026-11-30): each row of the input ends with a line break, so widening changes nothing, and a terminal that does not reflow (xterm) keeps the rows as drawn. A terminal that reflows wraps each row again when it narrows. The editor keeps the widths of the items on each row, and when the width is less than at the last drawing it computes the cursor's row after the wrap (~cursor-row-after-reflow~). - Whether the terminal reflows is a guess from the environment (~terminal-reflows?~); ~#:reflow?~ of ~make-line-editor~ overrides it. An unknown terminal counts as not reflowing: a wrong #f leaves a copy of the input, a wrong #t erases output above it. - Several resizes before one redraw give the same result as one, as the terminal joins its own wraps again. - Not done: input taller than the screen after a reflow. The rows above the top are in the scrollback, and the cursor cannot go up to them. - Tested with ~script~, which does not reflow: the test checks the escape sequences. Not yet tried on a terminal that reflows. *Async, and less C# (2026-11-30).* - ~read-event~ and everything above it work in both colours. The one leaf is ~read-bytes~, a ~defbjouble~: in a bjoroutine it awaits the glue's read, elsewhere it parks the thread on a fiber of the caller's scope. The compiler makes the suspending copies of ~read-event~, ~line-read~ and the editor loop. ~with-raw-mode~ takes a body of either colour (~-?->~), and ~line-read~ passes it a bjoroutine in its suspending copy. - Cancelling the scope around a waiting ~line-read~ ends it, and the modes come back. - Moved to Bjolang: the decoder (~(bjoterm keys)~), the wait for the rest of a sequence, the widths and prompt escapes (~(bjoterm text)~), search and common prefixes. Clusters and the Unicode categories come from ~(std unicode grapheme)~ and ~(std unicode chars)~; only the East Asian wide ranges are a table of our own. The glue is termios, the signals, and ~poll~ and ~read~ of bytes. - Reads of the glue run one at a time: a read waits for the one before it, which ends soon after it is cancelled. - Decision 11 (a C# decoder) is replaced by this. * Decisions 1. *Linux only, for now.* The termios layout, ~TIOCGWINSZ~ and the flag values are those of Linux (x86-64 and arm64 have the same ones). macOS has another layout; it is a later phase. On another system ~terminal-open~ returns ~(Err TerminalUnsupported)~. 2. *stdin and stdout must both be terminals.* Modes are set on fd 0, keys are read from fd 0, and the size is read from fd 1. 3. *Keys are read from fd 0 directly*, with ~poll~ and ~read~, not through the standard input port: the port cannot wait with a timeout, and a lone ESC is told from the start of a sequence by a timeout (50 ms). Input that the port has already buffered is not seen. 4. *.NET's ~Console~ is not used.* ~Console.ReadKey~ and ~Console.In~ make .NET set its own terminal modes, and set them again at SIGCONT and when a child process ends. The runtime's standard ports never touch ~Console.In~, so as long as a program does not, the modes are ours. 5. *Raw mode is scoped.* ~(with-raw-mode term thunk)~ saves the modes, sets raw mode, runs the thunk, and restores the saved modes in a ~finally~: also on an exception, on Ctrl-C or SIGTERM through ~MainExit~, and at process exit (~ProcessExit~). Nested calls are an error. 6. *What raw mode changes*: - off: ~ECHO~, ~ICANON~, ~IEXTEN~, ~ISIG~, ~IXON~, ~ICRNL~, ~BRKINT~, ~INPCK~, ~ISTRIP~; ~CS8~ on; ~VMIN~ 1, ~VTIME~ 0. - ~OPOST~ stays on, so ~println~ still starts the next line at column 0. - ~#:signals? #t~ keeps ~ISIG~: Ctrl-C, Ctrl-Z and Ctrl-\ then send their signals, as in cooked mode. 7. *SIGCONT while raw*: the raw modes are set again, and the next event is ~Resized~, so the program draws its screen again. The registration cancels .NET's own SIGCONT handling, which would set .NET's modes. 8. *Ctrl-Z in raw mode is a key.* ~terminal-suspend~ restores the saved modes, sends SIGTSTP to our process group, and sets raw mode again when the process continues. 9. *Events* are one union: - ~(TermKey key mods)~: ~mods~ is a ~Mods~ record of ~shift?~, ~alt?~, ~ctrl?~. - ~(TermPaste text)~: bracketed paste, when it is on. - ~(TermResized cols rows)~: SIGWINCH, and after SIGCONT. - ~TermEnd~: end of input (the terminal hung up). Keys are ~(KeyChar c)~, ~KeyEnter~, ~KeyTab~, ~KeyBackspace~, ~KeyEscape~, the arrows, ~KeyHome~, ~KeyEnd~, ~KeyPageUp~, ~KeyPageDown~, ~KeyInsert~, ~KeyDelete~, ~(KeyF n)~, and ~(KeyUnknown text)~ for a sequence the decoder does not know. Names carry a prefix, so that a program can import the module without a prefix of its own. 10. *Ctrl and a letter* is ~(KeyChar #\a)~ with ~ctrl?~. Tab, Enter (13 and 10), Backspace (127 and 8) and Escape are their own keys. ESC before a key is Alt. 11. *The decoder is C# glue*, as the read loop is. It returns a flat record (kind, code, mods, text), and Bjolang maps it to the union, as ~(std run)~ maps ~JobStatus~. Tests feed it bytes directly. 12. *Output is strings.* The module returns escape sequences as strings, and the program writes them to stdout. ~terminal-flush~ flushes the standard output port. Nothing is written behind the program's back, except that leaving raw mode turns off what the module turned on (bracketed paste). * Phases - *1. The terminal.* Package, glue, ~terminal-open~, ~with-raw-mode~, ~terminal-size~, ~read-event~ with and without a timeout, the decoder (CSI and SS3 keys with modifiers, Alt, UTF-8, bracketed paste), ~terminal-suspend~, SIGWINCH and SIGCONT, escape sequences for the cursor, clearing, the alternate screen and styles. Tests: the decoder from bytes; raw mode, a key, a resize and restore under ~script~. - *2. The line editor.* A prompt, editing keys (Emacs set), a history, completion through a function the program gives, a multi-line buffer for a REPL, and wide characters (East Asian width, combining marks). - *3. Later.* macOS, mouse events, the kitty keyboard protocol, a screen buffer that writes only what changed.