#!/usr/bin/env dub
/+ dub.sdl:
name "editor-insert-each"
targetPath "build"
dependency "sparkles:ui" path="../../.."
buildType "checked" {
buildOptions "optimize" "inline" "debugInfo"
}
+/
/// Position-aware insertion shares one editor transaction across several carets.
module (module) insert_each_examplePosition-aware insertion shares one editor transaction across several carets.
insert_each_example;
import (package) stdstd.(module) std.stdioCategory Symbols File handles _popen File isFileHandle openNetwork stderr stdin stdout Reading chunks lines readf readfln readln Writing toFile write writef writefln writeln Misc KeepTerminator LockType StdioException
Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio
is publically imported when importing std.stdio.
There are three layers of I/O:
The lowest layer is the operating system layer. The two main schemes are Windows and Posix.
C's stdio.h which unifies the two operating system schemes.
std.stdio, this module, unifies the various stdio.h implementations into
a high level package for D programs.
Source
std/stdio.d
stdio : (alias template) insert_each_example.writeln = std.stdio.writeln(T...)(T args)Equivalent to write(args, '\n'). Calling writeln without
arguments is valid and just prints a newline to the standard
output.
Example
Reads stdin and writes it to stdout with an argument
counter.
import std.stdio;
void main()
{
string line;
for (size_t count = 0; (line = readln) !is null; count++)
{
writeln("Input ", count, ": ", line);
}
}
writeln;
import (package) sparklessparkles.(package) sparkles.text_buffertext_buffer.(module) sparkles.text_buffer.textText: an immutable UTF-8 text in a persistent summary tree.
Copying a text is O(1) and allocation-free; every "modifying" operation
returns a new text that shares all untouched subtrees with its arguments.
Positions are byte offsets. Lines end before their line feed, or before the
CR of a CR LF pair.
Never in a GC-allocated array. A text counts its references, and
druntime fills a GC array, from an array literal or by appending with ~=,
by blitting its elements without the copy that counts one; the collector
later releases them again. Keep texts, and anything holding one (Edit,
ChangeSet), in a sparkles.base.buffer buffer or a static array.
Specification
docs/specs/text-buffer/SPEC.md §2–3.
text : (struct) sparkles.text_buffer.text.TextAn immutable byte sequence, normally UTF-8, held in a summary tree.
Text is a Regular value: copies are independent, == compares contents,
and copying is one atomic increment. It is the size of one pointer.
Text;
import (package) sparklessparkles.(package) sparkles.uiui.(package) sparkles.ui.editoreditor.(module) sparkles.ui.editor.commandThe editor's command vocabulary (EDC1–EDC11).
Commands are named by effect, not by key, so any key table can bind them
(KEY11). A command is a small pointer-free value: text arrives as a
TextId into the host's input text pool, so a command can sit in a key
table, an event queue or a recording and be compared with ==.
Specification
docs/specs/ui/editor.md §3.
command : (struct) sparkles.ui.editor.command.EditCommandOne editing command.
EditCommand, (enum) sparkles.ui.editor.command.EditKindWhat a command does.
EditKind;
import (package) sparklessparkles.(package) sparkles.uiui.(package) sparkles.ui.editoreditor.(module) sparkles.ui.editor.fieldEditorField: an editor ready to embed as a text field (EDU4).
It owns the editor state, a small pool for the text that keys and pastes
carry, and the target's default key table. A host routes a focused field's
keys and pastes to it and reads back whether the text changed, was
submitted or was cancelled, and whether the field asked for the clipboard.
The kill ring is the host session's when the host provides one (EDY1),
else the field's own.
Specification
docs/specs/ui/editor.md §5–8.
field : (struct) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext)EditorField;
import (package) sparklessparkles.(package) sparkles.uiui.(package) sparkles.ui.editoreditor.(module) sparkles.ui.editor.selectionSelection ranges and the normalized selection set (EDT4).
A range has an anchor that stays and a head that moves; the head is
where the caret is drawn, and a range whose anchor equals its head is a
caret. A selection set holds at least one range, sorted, never overlapping,
with exactly one primary.
Specification
docs/specs/ui/editor.md §1–2.
selection : (struct) sparkles.ui.editor.selection.EditRangeOne selection range.
EditRange, (struct) sparkles.ui.editor.selection.SelectionSetThe editor's ranges: at least one, sorted by start, none overlapping, one
primary. Copying is O(1) until one copy changes (SharedBuffer).
SelectionSet;
void void D main() @safemain() @safe
{
auto (local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield = (struct) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext)EditorField.sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).make(scope const(char)[] text = "", sparkles.ui.editor.state.EditorOptions options = EditorOptions(false, true, IndentOptions(true, cast(ubyte)4u, false), null, Duration(5000000L), null, 0LU), sparkles.ui.editor.keys.EditorTarget target = EditorTarget.terminal) pure nothrow @nogc @safeA field holding text, caret at its end.
make("abc");
(struct) sparkles.ui.editor.selection.EditRangeOne selection range.
EditRange[2] (local variable) sparkles.ui.editor.selection.EditRange[2] caretscarets = [(struct) sparkles.ui.editor.selection.EditRangeOne selection range.
EditRange.sparkles.ui.editor.selection.EditRange sparkles.ui.editor.selection.EditRange.caret(ulong at) pure nothrow @nogc @safeA caret at at.
caret(1), (struct) sparkles.ui.editor.selection.EditRangeOne selection range.
EditRange.sparkles.ui.editor.selection.EditRange sparkles.ui.editor.selection.EditRange.caret(ulong at) pure nothrow @nogc @safeA caret at at.
caret(3)];
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield.(field) sparkles.ui.editor.state.EditorState sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).statestate.(field) sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.state.EditorState.selectionselection = sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.selection.SelectionSet.opAssign(sparkles.ui.editor.selection.SelectionSet p) pure nothrow @nogc return ref @trustedSelectionSet.sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.selection.SelectionSet.of(scope const(sparkles.ui.editor.selection.EditRange)[] ranges, ulong primary) pure nothrow @nogc @safeThe normalized set of ranges, primary naming the primary one among
them. Ranges are sorted by start, stably; two merge into one spanning both
when they share any position other than the single common endpoint of
two non-empty ranges. A merged range keeps the later range's direction
and is primary if either was. An empty ranges gives a caret at 0.
of((local variable) sparkles.ui.editor.selection.EditRange[2] caretscarets[], 0);
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield.sparkles.ui.editor.field.FieldResult sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).insertEach!(string function(in sparkles.text_buffer.text.Text text, ulong position) pure nothrow @nogc @safe)(string function(in sparkles.text_buffer.text.Text text, ulong position) pure nothrow @nogc @safe insertion, core.time.MonoTimeImpl!(ClockType.normal) now = MonoTimeImpl(0L)) pure @nogc @safeCompute inserted UTF-8 per selection from the current text and head position.
All replacements share the editor's protection, length and undo machinery.
insertEach((in (struct) sparkles.text_buffer.text.TextAn immutable byte sequence, normally UTF-8, held in a summary tree.
Text is a Regular value: copies are independent, == compares contents,
and copying is one atomic increment. It is the size of one pointer.
Text (parameter) const(sparkles.text_buffer.text.Text) texttext, (alias) object.size_t = ulongsize_t (parameter) ulong positionposition) @safe pure nothrow @nogc
=> (parameter) ulong positionposition == 1 ? "A" : "B");
void std.stdio.writeln!string(string __param_0) @safeEquivalent to write(args, '\n'). Calling writeln without
arguments is valid and just prints a newline to the standard
output.
Example
Reads stdin and writes it to stdout with an argument
counter.
import std.stdio;
void main()
{
string line;
for (size_t count = 0; (line = readln) !is null; count++)
{
writeln("Input ", count, ": ", line);
}
}
writeln((local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield.string sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).text() const pure nothrow @safeThe text, copied.
text);
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield.sparkles.ui.editor.field.FieldResult sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).run(sparkles.ui.editor.command.EditCommand command, core.time.MonoTimeImpl!(ClockType.normal) now = MonoTimeImpl(0L)) pure nothrow @nogc @safeRun command.
run((struct) sparkles.ui.editor.command.EditCommandOne editing command.
EditCommand.sparkles.ui.editor.command.EditCommand sparkles.ui.editor.command.EditCommand.of(sparkles.ui.editor.command.EditKind kind) pure nothrow @nogc @safeof((enum) sparkles.ui.editor.command.EditKindWhat a command does.
EditKind.(enum value) sparkles.ui.editor.command.EditKind.undo = 29undo));
void std.stdio.writeln!string(string __param_0) @safeEquivalent to write(args, '\n'). Calling writeln without
arguments is valid and just prints a newline to the standard
output.
Example
Reads stdin and writes it to stdout with an argument
counter.
import std.stdio;
void main()
{
string line;
for (size_t count = 0; (line = readln) !is null; count++)
{
writeln("Input ", count, ": ", line);
}
}
writeln((local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) fieldfield.string sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).text() const pure nothrow @safeThe text, copied.
text);
}