edward.ed.editor

This library implements a text editor object which tracks an internal state that can be modified through defined editor operations. These operations operate on a pair of line numbers. POSIX ed addresses can be translated to a pair of line numbers using address translation procedures described below.

Index

Table of contents

Text Editor Object

Object for storing, inspecting, and modifying the editor state.

procedure make-text-editor

Create a new text editor object on a given (potentially) empty filename string. If non-empty, an edit-proc needs to be provided which implements the E command to initially read the file. Furthermore, a prompt string must be provided and it must be indicated whether the editor should start in silent? mode.

(make-text-editor edit-proc filename prompt silent?)

procedure text-editor?

Predicate which returns true if the given object was created using make-text-editor.

(text-editor? obj)

procedure text-editor-filename

Returns the name of the file that is currently being edited.

(text-editor-filename text-editor)

procedure text-editor-filename-set!

Change the file that is currently being edited.

(text-editor-filename-set! text-editor new-value)

procedure text-editor-line

Returns the current line in the internal editor buffer.

(text-editor-line text-editor)

procedure text-editor-error

Returns a string representing the last encountered error message.

(text-editor-error text-editor)

procedure text-editor-prevcmd

Returns the symbol of the command previously executed by the editor, on #f if no previous command was executed.

(text-editor-prevcmd text-editor)

procedure text-editor-last-cmd-set!

Update the last shell command executed via the shell escape editor command.

(text-editor-last-cmd-set! text-editor new-value)

procedure text-editor-modified?

Predicate which returns true if the current file has been modified since the last write to a file (i.e. has unwritten data).

(text-editor-modified? text-editor)

procedure text-editor-modified-set!

Modify the modified state of the current file. Set this to #t if the file has been modified.

(text-editor-modified-set! text-editor new-value)

procedure text-editor-help?

Predicate which returns true if help mode is activated (H command).

(text-editor-help? text-editor)

procedure text-editor-help-set!

Enable help mode by passing a truth value to this procedure.

(text-editor-help-set! text-editor new-value)

Editor Interface

High-level text editor interfaces.

procedure editor-start

Start the read-eval-print loop (REPL) of the editor. Within the REPL, command parsing is performed using the given cmd-parser.

(editor-start editor cmd-parser)

procedure editor-interactive

Run an interactive command within the text editor. The command is parsed using the provided cmd-parser.

(editor-interactive editor cmd-parser)

procedure editor-toggle-prompt!

Toggle visibility of the REPL prompt.

(editor-toggle-prompt! editor)

procedure editor-shell-cmd

Returns the last executed shell command or raises an error if none.

(editor-shell-cmd editor)

procedure editor-make-regex

Build a new regex object and handle regex syntax errors as editor errors. If the provided pattern is empty, the last used pattern is re-used, if there is no last-used pattern an editor error is raised.

(editor-make-regex editor pattern)

procedure editor-restr

Access a replacement string in the editor context. If the provided replacement string subst is 'previous-replace then the previously used replacement string is returned or an editor error is raised if there is no previous replacement string. Otherwise, (if subst is a string) then the previous replacement is updated and subst is returned.

(editor-restr editor subst)

constant editor-filename

Return the currently configured filename, if no default is given it is an error if no filename is configured for the given editor.

editor-filename

procedure editor-verbose

Print objs, but only if the editor is not in silent mode.

(editor-verbose editor . objs)

procedure editor-error

Print ? optionally followed by msg, if the editor is in help mode. If standard input does not refer to a terminal device, the editor terminates with a non-zero exit status.

(editor-error editor msg)

procedure editor-raise

Raise an R7RS editor error exception with the given msg. This error is caught by an error handler and causes the msg to be printed using the editor-error procedure.

(editor-raise msg)

procedure editor-reset!

Reset all file-specific state of the editor.

(editor-reset! editor)

procedure editor-mark-line

Create an editor mark named mark which refers to the given line.

(editor-mark-line editor line mark)

procedure editor-goto!

Move editor cursor to specified line. Line 1 is the first line, specifying 0 as a line moves the cursor before the first line.

(editor-goto! editor line)

procedure editor-get-lnum

Find current line number for a given line in the editor buffer. False is returned if the line does not exist in the editor buffer.

(editor-get-lnum editor line)

procedure editor-get-lines

Return the content of the editor text buffer as a list of lines for the specified line pair lines. The start address of the pair is inclusive while the end address is exclusive.

(editor-get-lines editor lines)

procedure editor-in-range?

Predicate which returns true if the given line is within the range specified by lines.

(editor-in-range? editor lines line)

Editor Operations

Procedure which modify the editor text buffer. Provided operations are wrappers around operations of the internal text editor line buffer and additionally take care of updating the editor state (e.g. the modified state).

procedure editor-undo!

Undo the last operation on the buffer.

(editor-undo! editor)

procedure editor-lines

Returns amount of lines in the buffer.

(editor-lines editor)

procedure editor-line-numbers

Returns list of line numbers for given lines.

(editor-line-numbers lines)

procedure editor-append!

Append the text at the current address. Returns line number of last inserted line.

(editor-append! editor line text)

procedure editor-replace!

Replace text of given lines with given data. Returns line number of last inserted line.

(editor-replace! editor lines data)

procedure editor-join!

Join given lines to single line. Return value is undefined.

(editor-join! editor lines)

procedure editor-remove!

Remove given lines. Return value is undefined.

(editor-remove! editor lines)

procedure editor-move!

Move given lines to given destination dest-line. Returns the address of the last inserted line.

(editor-move! editor lines dest-line)

Address Translation

Procedures for performing address translation. That is, procedures which convert an edward ed addr to a line number (or a pair of line numbers) based on the current editor state. For example, the ed address . would be converted to the current line number (as tracked in the editor object). The resulting address can then be passed to an editor operation.

constant addr->line

Convert a single address (i.e. as created via make-addr) to a single line number. This is a procedure which must be passed the text editor object and an edward ed address as procedure arguments.

addr->line

procedure range->lpair

Convert a range address (i.e. as created via make-range) to a line pair. This procedure does not modify the current editor addresses, even for address range like 5;6.

(range->lpair editor range)

procedure addrlst->lpair

This procedure takes an addrlist, as returned by parse-addrs and an editor object as an argument and returns a concrete line pair for this address. This line pair can then be passed to defined editor commands.

(addrlst->lpair editor lst)

Command Execution

Based on defined editor operations, it is possible to define custom editor commands which combine multiple operations. These commands can then be executed by the users via the REPL spawned by editor-start.

Warning: The procedures documented here provide a low-level interface for defining custom editor commands. However, it is highly discouraged to use this interface directly. Instead, editor commands should be defined through the macros provided by edward ed cmd.

procedure make-cmd

Create a new editor command. The command is identified by a unique symbol and procedure proc which receives an editor object and the given args as procedure arguments.

(make-cmd symbol default-addr proc args)

procedure editor-cmd?

Predicate which returns true if the given obj was created using the make-cmd procedure.

(editor-cmd? obj)

procedure cmd-args

Retrieve additional arguments defined for this command. The returned list value does not include the editor object.

(cmd-args editor-command)

procedure editor-exec

Execute an editor command cmd using the given editor state on the addresses given by addrlst. The given addresses are translated to line addresses internally. If a command should be executed on a line address directly, use the editor-xexec procedure instead.

(editor-exec editor addrlst cmd)

procedure editor-xexec

Execute given cmd using given editor state on the address addr. The address can either be a single line address, a line pair, or an empty list depending on the default address specified for cmd. If the command doesn't specify a default address (i.e. doesn't expect an address argument) then it is an error to pass anything other than the empty list as an addr value to this procedure.

(editor-xexec editor addr cmd)

procedure editor-exec-cmdlist

Execute a list of commands using given editor state.

(editor-exec-cmdlist editor cmd-pairs)