Skip to content

Instantly share code, notes, and snippets.

@thatlittlegit
Last active January 11, 2020 04:03
Show Gist options
  • Save thatlittlegit/f6578e1aca3bbfa9f6f795f2250d955c to your computer and use it in GitHub Desktop.
Save thatlittlegit/f6578e1aca3bbfa9f6f795f2250d955c to your computer and use it in GitHub Desktop.
A small attempt at making an ed(1) recreation in Perl.
.TH TROPE 1 "Feburary 2019" "trope" "Trope Manual"
.SH NAME
trope \- the true programmer's text editor
.SH SYNOPSIS
.B trope
[options] [--] [input file]
.SH DESCRIPTION
.B trope
is an
.BR ed (1)
compatible text editor written in
.BR perl (1).
It is designed to support some features of modern derivatives like
.BR vi (1)
while still keeping the minimalism from
.BR ed (1).
It will support opening the current buffer in another window (via a
.BR fifo (7)
file), a simple macro system (a la
.BR vi (1))
and a way to run
.BR perl (1)
scripts from the input line. Readline support is also expected.
.SH OPTIONS
.TP
.BR \-v ", " \-\-version
Prints the current version of the
.B trope
editor.
.TP
.BR \-h ", " \-\-help
Prints usage information. (Generated by
.BR Getopt::Lucid (3pm).)
.TP
.BR \-p ", " \-\-prompt =\fIPROMPT\fP
Sets the prompt.
.TP
.BR \-s ", " \-\-silent
Disables output of byte counts, and \fI!\fP after a command has executed.
.TP
.BR \-G ", " \-\-gio
Use GIO for all filesystem interaction: this means protocols are supported (i.e.
.I https://kernel.org/
would be a valid filename.) This interaction is done via
.BR Glib (3pm).
This option takes precedent over
.BR TROPE_USE_GIO .
.SH ENVIRONMENT
.TP
.BR TROPE_USE_GIO
Use GIO for all filesystem interaction: see
.BR \-\-gio .
# trope: Main file for the Trope project.
#
# (C) 2019 thatlittlegit. Licensed under the MIT License.
use Getopt::Lucid qw( :all );
use Term::ReadLine;
use File::Temp;
use Switch;
use Scalar::Util qw(looks_like_number);
# basic option specifications with aliases
@specs = (
Switch("help|h"),
Switch("version|v"),
Param("prompt|p")->default(""),
Switch("silent|s"),
Switch("gio|G")
);
$opt = Getopt::Lucid->getopt( \@specs )->validate;
my $prompt = $opt->get_prompt;
my $filename = @ARGV[0];
if (!defined($filename)) {
$filename = File::Temp->new()->filename;
}
open (my $fh, "<", $filename) or warn "Failed to open file";
my $filelen = 0;
my @buffer = ("");
foreach (<$fh>) {
push (@buffer, $_);
$filelen = $filelen + length;
}
my $currentline = scalar(@buffer) - 1;
print $filelen . "\n";
if (!defined($fh)) {
$fh = File::Temp->new();
}
$term = new Term::ReadLine 'trope';
while ( defined ($_ = $term->readline($prompt)) ) {
chomp;
if (looks_like_number($_)) {
if ("$_" =~ m/\+[0-9]+/) {
s/^\+//;
print $_ . "\n";
$currentline = $currentline + $_;
} elsif ($_ > (scalar @buffer) or $_ == 0 or "$_" =~ m/[^0-9]+/) {
print "?\n";
} else {
$currentline = $_;
}
} else {
switch ($_) {
case "." { print $buffer[$currentline]; }
case "\$" { print $buffer[scalar @buffer - 1]; }
else { print "?\n" }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment