From b9ed52e3684e49b9b00ef08b29111758cc4777ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 26 May 2021 01:36:03 +0200 Subject: Add main 'mum' script --- src/mum | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 src/mum diff --git a/src/mum b/src/mum new file mode 100755 index 0000000..3c7d0b0 --- /dev/null +++ b/src/mum @@ -0,0 +1,121 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw/switch/; +no warnings qw/experimental::smartmatch/; + +use Data::Dumper; + +# Open TTY for reading and writing + +open my $tty, '+<:unix', '/dev/tty' or die "Could not open /dev/tty: $!"; + +# Define range syntax + +my @range; # range (one/two references) +our %ref; # current reference in range +my $d = qr{ + (?(DEFINE) + (? (?{ @range = () }) + ( (?&ref) + | (?&ref),(?&ref) + | (?&pattern) + ) + (?{ push @range, \%ref }) + ) + (? ( (\d+) (?{ local %ref = (line => $^N) }) + | (\.|\$) (?{ local %ref = (spec => $^N) }) + | ' ([a-z]) (?{ local %ref = (mark => $^N) }) + | /((\\/|[^/])*)/ (?{ local %ref = (next => $^N) }) + | \?((\\\?|[^?])*)\? (?{ local %ref = (prev => $^N) }) + ) + ( ([+-] \d+) (?{ local %ref = (%ref, plus => $^N) }) + )? + ) + (? g/((\\/|[^/])*)/ (?{ local %ref = (patt => $^N) }) + ) + ) +}x; + +# Define program state + +my $message = 1; # selected message + +# Run program + +while () { + print $tty "$message& "; + + # Read next command from user + + my $cmd = <$tty>; + $cmd = '' if not $cmd; + chomp $cmd; + $cmd =~ s/^\s+|\s+$//g; + + # Parse user-given command + + for ($cmd) { + # q + if (/^q$/) { + exit; + } + + # range without command + elsif (/^(?&range) \Z $d/x) { + print Dumper(@range), "\n"; + # select last message in range + print "range without command\n"; + } + + # +/- (++/--) + elsif (/^(? \d*) (? (\+\+?|--?)) \Z/x) { + for ($+{dir}) { + $message += $+{num} || 1 if /\+/; + $message -= $+{num} || 1 if /-/; + goto h if length($_) > 1; + } + } + + # c + elsif (/^c\s+(.*)/) { + chdir $1; + } + + # h + elsif (/^ (?&range)? h \Z $d/x) { +h: + print "h\n"; + } + + # p + elsif (/^ (?&range)? p \Z $d/x) { + print Dumper(@range), "\n"; + print "p\n"; + } + + # | + elsif (/^ (?&range)? \| (?.+) $d/x) { + print "|\n"; + } + + # ! + elsif (/^!/) { + $cmd =~ s/!//; + system $cmd; + print "!\n"; + } + + # empty command + elsif (/^$/) { + $message++; + } + + # unrecognized command + else { + warn "unrecognized command syntax\n"; + next; + } + } +} -- cgit v1.2.3