Loading ...
Sorry, an error occurred while loading the content.

Re: Arbitrary Sort Script...

Expand Messages
  • Suresh Govindachar
    Jonathan Ploudre wrote on 2 Jan 2005 16:11:57 -0800: that he has been using a perl script to sort text documents within the BBEdit editor; and is curious
    Message 1 of 4 , Jan 2, 2005
    • 0 Attachment
      Jonathan Ploudre wrote on 2 Jan 2005 16:11:57 -0800:

      that he has been using a perl script to sort text
      documents within the "BBEdit" editor; and is curious
      about doing the same work from within the VIM editor.
      His perl script was provided by an acquaintance of his;
      he is a family doctor, not a programmer, but has had a
      semester of programming.

      Good news! VIM supports scripts written in perl. It is a
      simple task to take your perl script and a few lines of glue
      code so that it can re-organize ("sort") text in a buffer.

      It really is very easy, but I don't have the time to do it
      for you. Perhaps looking at the following might help you
      (and/or your acquaintance) get started:

      Vim's manual on using perl scripts:
      help perl-using
      An example:
      http://www.vim.org/scripts/script.php?script_id=1082
      A tip (that might be somewhat confusing since it gets
      corrected in the notes written toward the end of the page)
      http://www.vim.org/tips/tip.php?tip_id=140

      --Suresh
    • Stefan Kell
      Hello Jonathan, your perl skript is written as a filter and can be used inside vim without problems. Take a look a the help file with :help ! inside vim.
      Message 2 of 4 , Jan 2, 2005
      • 0 Attachment
        Hello Jonathan,

        your perl skript is written as a filter and can be used inside vim without
        problems. Take a look a the help file with ":help !" inside vim.

        With Windows you will have to install a version of perl, perhaps from
        ActiveState: "http://www.activestate.com/Products/ActivePerl/". There is a
        skript included to convert a perl-skript to a bat-file so that you can use
        the skript similiar to UNIX.

        The embedded perl interpreter, mentioned in an outher post, has to be
        compiled into vim, which might not be in your version of vim. You can
        check this with ":version" inside vim. But you don't need this for your
        filter.

        Regards

        Stefan Kell

        On Sun, 2 Jan 2005, Jonathan Ploudre wrote:

        > Hey There,
        >
        > I'm new to Vim. I'm a family doctor so I write a lot of notes to keep
        > track of what patients tell me. Patients tell me things in their own
        > order but doctors have a set organization of how they write notes. So,
        > my goal is to take notes as the patient talks and later have them
        > auto-sort into the right order.
        >
        > I've been using BBEdit on my mac to do text editing but the problem is
        > that my current solution is not portable. And I get forced to use
        > Windows at work. I think Vim is a great option since it's powerful like
        > BBEdit but I can use it on Windows or FreeBSD or a Mac.
        >
        > An acquaintance of mine wrote a short perl script for BBEdit to help do
        > an arbitrary sort based on the way I wanted to do things. Basically, I
        > make a tag on each line ended by a colon. It's sorta like e-mail
        > headers. If there is not a tag on the line, it's assumed to be related
        > to the previous tagged line. Near the end the script does some
        > formatting. Actually, I'd rather it didn't do that but before I change
        > it, let me ask my question.
        >
        > My question: Is it possible to re-write the script to work inside Vim?
        >
        > I'm not a programmer (1 semester at college) but if it's doable, maybe
        > I could learn enough to make it work. But I'd love to know that it was
        > feasable before trying it out. Nothing in the FAQ or on the premade
        > scripts looked useful.
        >
        > Jonathan Ploudre
        >
        > Here's the source of the perl script:
        >
        > **********
        >
        > #!/usr/bin/perl -w
        > #
        > # Quick and Dirty note sorter
        > #
        > # Andy Law - 30th September 2004
        > # (Andy.Law@...)
        > # No warranty, etc. Use at your own risk.
        >
        > use strict;
        >
        > #
        > # List of recognised keywords. The values assigned determine the order
        > # in which they are sorted. Values need not be consecutive. The blank
        > one
        > # at the start is a catch-all block. You can designate a block to be in
        > this
        > # section by using a : on its own on the line
        > #
        > my %list = (
        > '' => 0,
        > 'CC' => 1,
        > 'HPI' => 2,
        > 'ROS' => 3,
        > 'MedHx' => 4,
        > 'SurgHx' => 5,
        > 'FamHx' => 6,
        > 'SocHx' => 7,
        > 'DevHx' => 8,
        > 'ObHx' => 9,
        > 'PEX' => 10,
        > 'Dx' => 11,
        > 'Rx' => 12
        > );
        >
        > # what type of record are we storing currently
        > my $currentState = '';
        > my %storage = ();
        >
        >
        > # for each line in turn, throw away the line ending.
        > while (defined (my $line = <>)) {
        > chomp( $line);
        >
        > # look for a series of zero or more non-colons, followed by a colon,
        > # followed by optional whitespace, followed by some optional
        > non-whitespace
        > # Ignore leading whitespace on the line
        > # Preserve the non-colon, non-whitespace before the colon and anything
        > else after
        > # it too.
        > if ($line =~ m/^\s*([^\:]*)\:\s*(.*)$/) {
        >
        > # If the line looks like it has a tag, then chek it against our list of
        > tags
        > # If it is valid, then record that we are now in that tag's state and
        > replace
        > # the value of line so that it was as if the line didn't have the tag.
        > If there
        > # is nothing but whitespace left in the line after that, then move to
        > the next line
        > if (defined $list{$1}) {
        > $currentState = $1;
        > $line = $2;
        > if ($line =~ m/^\s*$/) {
        > next;
        > }
        > }
        > }
        >
        >
        > # If we have not seen anything under this state before, then create an
        > array
        > # under the storage hash keyed with the tag. If we have seen something
        > under
        > # this tag before then push this entry onto the array.
        > if (! defined $storage{$currentState}) {
        > $storage{$currentState} = [ $line ];
        > }
        > else {
        > push ( @{$storage{$currentState}}, $line);
        > }
        > }
        >
        >
        > # Go thorough the storage hash, tag by tag in the order dictated by the
        > # values in the list hash. For each one in turn, if the tag is not the
        > # Blank tag, then print it out and underline it. Then print out each
        > line in
        > # turn from the storage (separated with new lines)
        > foreach my $key ( sort { $list{$a} <=> $list{$b} } keys %storage) {
        > if ($key ne '') {
        > print "\n", $key, "\n";
        > print "-"x(length $key), "\n";
        > }
        > print join( "\n", @{$storage{$key}}), "\n";
        > }
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.