#!/usr/bin/env perl use strict; my $textwidth = $ARGV[0] // 80; if ($textwidth == 0) { $textwidth = 80; } # TODO: get tabstop from vim as well <17-11-21, yigit> # my $tabstop = ' ' x 4; my $str = do { local $/; }; my $sofar = ""; my $buffer = ""; while ($str =~ m/(.*?)\s(-?-\w+)\s+/g) { my $pos = pos $str; # TODO: use tabstop here instead of 4 <17-11-21, yigit> # if (length("$buffer $sofar ") + $pos < $textwidth - 4) { if ($sofar eq "") { $sofar = "$buffer"; } else { $sofar = "$sofar $buffer "; } $sofar = $sofar . $1; $buffer = $2; } else { print "$sofar \\\n$tabstop$buffer "; $sofar = $1; $buffer = $2; $str = substr $str, $pos; } } print "$sofar $buffer $str";