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.
Object for storing, inspecting, and modifying the editor state.
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?)
Predicate which returns true if the given object was created using make-text-editor.
(text-editor? obj)
Returns the name of the file that is currently being edited.
(text-editor-filename text-editor)
Change the file that is currently being edited.
(text-editor-filename-set! text-editor new-value)
Returns the current line in the internal editor buffer.
(text-editor-line text-editor)
Returns a string representing the last encountered error message.
(text-editor-error text-editor)
Returns the symbol of the command previously executed by
the editor, on #f
if no previous command was executed.
(text-editor-prevcmd text-editor)
Update the last shell command executed via the shell escape editor command.
(text-editor-last-cmd-set! text-editor new-value)
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)
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)
Predicate which returns true if help mode is activated (H
command).
(text-editor-help? text-editor)
Enable help mode by passing a truth value to this procedure.
(text-editor-help-set! text-editor new-value)
High-level text editor interfaces.
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)
Run an interactive command within the text editor.
The command is parsed using the provided cmd-parser
.
(editor-interactive editor cmd-parser)
Toggle visibility of the REPL prompt.
(editor-toggle-prompt! editor)
Returns the last executed shell command or raises an error if none.
(editor-shell-cmd editor)
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)
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)
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
Print objs
, but only if the editor is not in silent mode.
(editor-verbose editor . objs)
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)
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)
Reset all file-specific state of the editor.
(editor-reset! editor)
Create an editor mark named mark
which refers to the given line
.
(editor-mark-line editor line mark)
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)
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)
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)
Predicate which returns true if the given line
is within
the range specified by lines
.
(editor-in-range? editor lines line)
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).
Undo the last operation on the buffer.
(editor-undo! editor)
Returns amount of lines in the buffer.
(editor-lines editor)
Returns list of line numbers for given lines.
(editor-line-numbers lines)
Append the text at the current address. Returns line number of last inserted line.
(editor-append! editor line text)
Replace text of given lines with given data. Returns line number of last inserted line.
(editor-replace! editor lines data)
Join given lines to single line. Return value is undefined.
(editor-join! editor lines)
Remove given lines. Return value is undefined.
(editor-remove! editor lines)
Move given lines
to given destination dest-line
.
Returns the address of the last inserted line.
(editor-move! editor lines dest-line)
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.
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
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)
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)
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.
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)
Predicate which returns true if the given obj
was created using
the make-cmd procedure.
(editor-cmd? obj)
Retrieve additional arguments defined for this command. The returned list value does not include the editor object.
(cmd-args editor-command)
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)
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)
Execute a list of commands using given editor state.
(editor-exec-cmdlist editor cmd-pairs)