Skip to content

Commit

Permalink
ed: start breaking up edParse() (#771)
Browse files Browse the repository at this point in the history
* Unhook commands E, e, f, H, h, P, Q and q from the large regex in edParse(); the idea is to remove the large regex entirely
* Commands E, e and f allow zero or one argument; the other commands require no argument
* None of these commands allows an address prefix, e.g. 1,2P is invalid
* When testing against GNU ed I discovered "f FILENAME" should still print the filename when setting it
* Later the remaining commands can be removed from the large regex if calculateLine() function strips address prefix from $_ at start of edParse()

%perl ed # start with empty buffer and no saved filename
P     ---> toggle * prompt
*e a.c   ---> start editing file a.c
197
*H   --->  toggle auto-help
*123d
?
invalid address  ---> error message displays by itself
*H  ---> disable auto-help
*,d   ---> delete all lines
*h  ---> display previous error saved from 123d command
invalid address
*E   ---> reload a.c because I don't want to delete all lines
197
*f  ---> show saved filename
a.c
*f a.c.copy ---> set new filename to write to
a.c.copy
*1,2d ---> delete first two lines
*w ---> write changes to a.c.copy
158
*q
  • Loading branch information
mknos authored Oct 28, 2024
1 parent 612e131 commit 4c33692
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ my @ESC = (
);

my %WANT_FILE = (
'e' => 1,
'E' => 1,
'f' => 1,
'r' => 1,
'w' => 1,
'W' => 1,
Expand Down Expand Up @@ -568,12 +565,9 @@ sub edFilename {
edWarn(E_ADDREXT);
return;
}

if (defined($args[0])) {
$RememberedFilename = $args[0];
return;
}

if (defined($RememberedFilename)) {
print "$RememberedFilename\n";
}
Expand Down Expand Up @@ -909,7 +903,7 @@ sub edParse {
$_ = $found . 'p';
return edParse();
}
if (s/\A(g|v)\///) {
if (s/\A([gv])\///) {
my $invert = $1 eq 'v';
my $end = rindex $_, '/';
return 0 if $end == -1; # g/re/p needs trailing /
Expand All @@ -926,6 +920,18 @@ sub edParse {
$command = 'nop';
return 1;
}
if (s/\A([Eef])//) { # 0-address file commands
$command = $1;
return 0 if m/\A\S/; # space before optional argument
s/\A\s+//;
$args[0] = $_ if length;
return 1;
}
if (s/\A([HhPQq])//) { # 0-address flag commands
$command = $1;
return 0 if m/\S/; # no argument
return 1;
}

my @fields =
(/^(
Expand All @@ -941,7 +947,7 @@ sub edParse {
(([+-])?(\d+))? # [+]num | -num
(([\+]+)|([-^]+))? # + | -
)?
([acdeEfhHijlmnpPqQrstwW=\!])? # command char
([acdijlmnprstwW=\!])? # command char
([a-z])? # command suffix
(\s*)(.+)? # argument (filename, etc.)
)$/x);
Expand Down

0 comments on commit 4c33692

Please sign in to comment.