1## About23This is a POSIX-compatible implementation of the standard Unix text4editor [`ed(1)`][ed posix]. The implementation is written entirely in5[R7RS][r7rs] [CHICKEN Scheme][chicken] and provides a Scheme library6interface for extending the editor with custom commands.78## Status910I am presently not aware of any POSIX.1-2008 conformance issues. The11library interface, for extending `edward` with custom commands, is12usable but still very experimental and subject to change. Refer to13the `TODO.md` file for more information.1415## Design1617This implementation relies on [parser combinators][parser combinators]18as well as [hygienic Scheme macros][hygienic macros] to ease the19implementation of ed commands. Each ed command is defined using a macro20(i.e. similar to how one would normally define procedures in Scheme) and21parsed through provided parser combinators. In a sense, thus employing22[language-oriented programming][language-oriented programming] to23implement ed commands and thereby making this ed implementation very24hackable and easy to extend. The implementation is also split into a25CHICKEN program and various library components which allows defining26custom commands (refer to the library interface documentation below).2728## Installation2930The program can be installed either using [GNU make][gnu make] or31[chicken-install][chicken egg-install].3233### GNU make3435Installation via GNU make does not require any CHICKEN configuration36and should work out-of-the-box. As such, it is especially useful for37packaging purposes. In order to build edward using GNU make run the38following commands:3940 $ make4142To install edward to system-wide directories run the following command:4344 $ make install4546The GNU make installation method only installs the edward binary. It47does not install the library interface. If you want to interact with48the edward library you need to install edward via chicken-install.4950### chicken-install5152Contrary to installation via GNU make, this installation method requires53a properly configured CHICKEN toolchain. If CHICKEN has been configured54correctly, run the following command to install both the library and the55program component:5657 $ chicken-install5859The edward binary will be added to a directory in your `$PATH`,60furthermore the edward library will be available in your CHICKEN library61path. More usage information for the edward library is provided below.6263## Tests6465This repository contains both unit tests and integration tests. The66latter require a reference implementation of a POSIX.1-2008 compatible67ed implementation. Currently, [GNU ed >= 1.20.2][gnu ed] is used for this68purpose.6970Both unit and integration tests can be run using:7172 $ make check7374Optionally, [tmux][tmux web] can be installed to also execute tests for75edward's end-of-file handling in an interactive environment.7677## Usage7879For interactive usage I can highly recommend using this software in80conjunction with a [readline][GNU readline] frontend such as81[rlwrap][rlwrap github]. This enables readline-like keybindings (e.g.82Ctrl+A, Ctrl+W, …) as well as input history support.8384Detailed usage instructions for the `ed(1)` text editor can be found in85the [POSIX documentation][ed posix]. Additionally, a nice introduction86to the editor is provided in the book *The Unix Programming Environment*87by Brian W. Kernighan and Rob Pike (Appendix 1: Editor Summary). In88general, since ed is an old editor which hasn't changed much in the past89centuries, many historic documents explaining its usage are still90applicable today. Most notably, volume 2A of the seventh edition of the91[UNIX Programmer's Manual][unix v7vol2a] contains two documents which92provide a beginner-friendly introduction to the editor: *A Tutorial93Introduction to the UNIX Text Editor* and *Advanced Editing on UNIX*94both written by Brian W. Kernighan.9596## Library Interface9798Apart from an `ed(1)` implementation, `edward` also provides a library99interface for extending the editor with custom commands. While the100implementation provided here focuses solely on POSIX compatibility,101extension to the POSIX standard can be supplied separately using the102library interface. The `edward` library can be used by creating a custom103CHICKEN Scheme program which imports the edward libraries, defines104custom commands through provided hygienic macros, and executes105`(edward-main)` to start the editor. For example, an `edward` variant106which provides a pipe command for passing a range of lines through107a filter can be implemented as follows:108109 (import (scheme base)110 (chicken process)111 (srfi 14)112113 (edward cli)114 (edward util)115 (edward parse)116 (edward ed cmd)117 (edward ed addr)118 (edward ed posix)119 (edward ed editor))120121 ;; Executor for the pipe command122 (define (exec-pipe editor range cmd)123 (let-values (((in out _) (process cmd))124 ((lines) (editor-get-lines editor range)))125 (call-with-port126 out127 (lambda (port) (lines->port lines port)))128 (let ((recv (port->lines in)))129 (close-input-port in)130 (exec-delete editor range)131 (exec-insert editor (car range) (car recv)))))132133 ;; Parser for the pipe command134 (define-file-cmd (pipe exec-pipe (make-range))135 (parse-cmd-char #\|)136 (parse-token (char-set-complement (char-set #\newline))))137138 ;; Start the editor139 (edward-main)140141Save this code in `edward++.scm` and compile it as follows:142143 $ csc -R r7rs edward++.scm144145Drop the resulting `edward++` binary somewhere in your `$PATH` and146invoke it as usual. Naturally, it is possible to define multiple custom147commands. Refer to `lib/ed/posix.scm` for the implementation of editor148commands mandated by the POSIX standard. Additional commands are also149available in a separate [edward-contrib][edward-contrib github]150repository.151152## API Documentation153154The API of the library interface is documented using Scheme source code155comments. An HTML documentation can be generated from these comments156using the [scmdoc][scmdoc github] utility. More information on that157is provided in `doc/README.md`.158159The API documentation is available via: https://files.8pit.net/edward/latest/doc/160161## History162163The existing GNU and BSD ed implementations are both derived from an164implementation written by Andrew Moore in the early-to-mid 1990s. As165such, they still share a lot of code and are potentially subject to the166same bugs [\[1\]][ed history]. To the best of my knowledge, edward is167the first ed implementation which strives to be fully POSIX compatible168but is not derived from Andrew's original implementation.169170## License171172This program includes code from [chibi-scheme][chibi-scheme github]173(`lib/parse/parse.scm`) written by Alex Shinn and licensed under a174BSD-style license. Furthermore, it includes a stripped-down and slightly175modified version of the [SRFI 214][srfi 214] reference implementation176(`lib/buffer/srfi214-minimal.scm`) written by Adam Nelson under MIT.177The program itself is licensed as follows:178179> This program is free software: you can redistribute it and/or modify it180> under the terms of the GNU General Public License as published by the181> Free Software Foundation, either version 3 of the License, or (at your182> option) any later version.183>184> This program is distributed in the hope that it will be useful, but185> WITHOUT ANY WARRANTY; without even the implied warranty of186> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General187> Public License for more details.188>189> You should have received a copy of the GNU General Public License along190> with this program. If not, see <https://www.gnu.org/licenses/>.191192[ed posix]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html193[gnu make]: https://www.gnu.org/software/make/194[chicken]: https://call-cc.org195[chicken egg-install]: https://wiki.call-cc.org/man/5/Extensions#installing-eggs196[chicken matchable]: https://wiki.call-cc.org/eggref/5/matchable197[chicken posix-regex]: https://wiki.call-cc.org/eggref/5/posix-regex198[chicken process signal]: https://api.call-cc.org/5/doc/chicken/process/signal199[gnu ed]: https://www.gnu.org/software/ed/200[srfi]: https://srfi.schemers.org/201[srfi 204]: https://srfi.schemers.org/srfi-204/202[srfi 214]: https://srfi.schemers.org/srfi-214/203[r7rs]: https://small.r7rs.org/204[parser combinators]: https://en.wikipedia.org/wiki/Parser_combinator205[GNU readline]: https://tiswww.cwru.edu/php/chet/readline/rltop.html206[rlwrap github]: https://github.com/hanslub42/rlwrap207[unix v7vol2a]: https://s3.amazonaws.com/plan9-bell-labs/7thEdMan/v7vol2a.pdf208[hygienic macros]: https://doi.org/10.1145/319838.319859209[language-oriented programming]: https://doi.org/10.1145/3127323210[ed history]: https://lists.gnu.org/archive/html/bug-ed/2021-12/msg00001.html211[tmux web]: https://tmux.github.io212[scmdoc github]: https://github.com/nmeum/scmdoc213[edward-contrib github]: https://github.com/nmeum/edward-contrib214[chibi-scheme github]: https://github.com/ashinn/chibi-scheme