Xref: feenix.metronet.com alt.sources:1339
Path: feenix.metronet.com!news.utdallas.edu!wupost!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!fauern!faulrs!jat!not-for-mail
From: [email protected] (Joachim Astel)
Newsgroups: alt.sources
Subject: tsh-1.0 - tabulator shifter in Perl
Date: 7 Jun 1993 11:53:27 +0200
Organization: Mail/News Server Leinburg
Lines: 72
Message-ID: <[email protected]>
NNTP-Posting-Host: jat.jat.sub.org

Suppose you've set a shiftwidth of 4 within vi, and you're missing the
'<<' and '>>' operations, because your vi is rather dumb about these
settings. So here's a small perl program (or hack ;-)) doing that task.

In "vi", you can mark the beginning a text block with >>> ma <<< and
pipe it to tsh by typing >>> !'atsh <<< at the end of your text block.

As a feature, spaces are shortened to tabs (but only at the beginning
of a line). So "tsh" without arguments is a mode that elliminates
redundant whitespace, and replaces it by the tabulator character...

Instead of >>> !'atsh <<< you could also type >>> !'atsh 1 <<< or give
any other option (see the usage at the beginning of the perl script).

       tsh 1   - shifts to the right
       tsh 2   - shifts to the right twice
       tsh 3   - shifts to the right three times
       [... and so on]

       tsh -1  - shifts to the left
       tsh -2  - shifts to the twice
       [... and so on]

This program is in the public domain. Have fun....
And please flame me if there's already another program named "tsh" out there.

#!/usr/bin/perl
# tsh.pl - tab shifter V1.0 written 1993 by Joachim Astel <[email protected]>
# Usage:
#       tsh [-sw=4] [-ts=8] [-+]shlshr [ file(s) ... ]

$shiftwidth = 4;
$tabstop = 8;
$shlshr = 0;

foreach $arg (@ARGV) {
   if ($arg =~ /^-ts=?(.*)/) {
       $tabstop = $1; shift;
   } elsif ($arg =~ /^-sw=?(.*)/) {
       $shiftwidth = $1; shift;
   } elsif ($arg =~ /^\+?(\d+)$/) {
       $shlshr = $1; shift;
   } elsif ($arg =~ /^-(\d+)$/) {
       $shlshr = -$1; shift;
   } else {
       break;
   }
}

while (<>) {
   $pos = 0;
   do {
       $oldpos = $pos;
       if (s/^ //) { # space
               $pos++;
       } elsif (s/^\t//) { # tab
               $pos = int (($pos + $tabstop) / $tabstop) * $tabstop;
       }
   } while ($oldpos != $pos);

   $pos += ($shlshr * $shiftwidth);

   while ($pos - $tabstop >= 0) {
       print "\t"; $pos -= $tabstop;
   }
   print ' ' x $pos if $pos > 0;
   print $_;
}
--
| E-MAIL: [email protected], [email protected] | v  y |
|   ADDR: Joachim Astel, Wiesenweg 4, <D-91227> W-8566 Leinburg | ,Oo. |
|    TEL: (0 91 20) 18 99 02 [from abroad: (+49) 9120 18 99 02] | (73) |