BBEdit Text Filters in Perl 6

I’ve been writing BBEdit Text Filters in Perl 6. This isn’t revolutionary and I’ve made no great hack. It’s not a hidden part of the BBEdit docs, and it’s not a feature unique to my favorite editor. You don’t even need to use Perl 6; any unix interpreter will do. Your editor can probably do the same thing, and maybe even with the same programs.

In your …/Application Support/BBEdit/Text Filters, drop in any sort of Unix script you like (mine is actually in my Dropbox folder and thus shared across my network, a feature from BBEdit 10). It uses the shebang line to run the program just as it does if you run it from the command line. This text filter gets the text you selected in the editor. Whatever you print to standard output replaces the selected text.

I wanted to turn a string into the codepoint numbers and names that represent their characters. Here’s that in Perl 6:

#!/Applications/Rakudo/bin/perl6

$*IN
	.lines
	.map: {
		eager ($_.ords Z, $_.uninames).map: {
			printf "%04x %s\n", $_.list
			}
		}

This turns text such as “Hamadryas” into this:

0048 LATIN CAPITAL LETTER H
0061 LATIN SMALL LETTER A
006d LATIN SMALL LETTER M
0061 LATIN SMALL LETTER A
0064 LATIN SMALL LETTER D
0072 LATIN SMALL LETTER R
0079 LATIN SMALL LETTER Y
0061 LATIN SMALL LETTER A
0073 LATIN SMALL LETTER S

There’s another text filter I like. I use it to align = and => (or anything that starts with = in code:

my $longest = 0;
my @lines;

for $*IN.lines -> $line {
	my @columns = $line.split( / \s+ '=' \S+ \s+ /, 2 );
	$longest max= @columns.[0].chars;
	push @lines, @columns;
	}

for @lines -> $line {
	printf "%-{$longest}s => %s\n", $line.list;
	}

Notice that max=. That’s a binary assignment with the max operator. That’s like $longest = $longest max @columns.[0].chars.

Now I need to figure out why BBEdit won’t allow me to assign a keyboard shortcut to it.

2 comments

  1. I prefer

    my @lines = $*IN.lines┬╗.split: / \s+ '=' \S+ \s+/, 2;
    my $longest = max @lines[*;0]┬╗.chars;
    printf "%-{$longest}s => %s\n", |$_ for @lines
    

Leave a Reply

Your email address will not be published. Required fields are marked *