Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Naive port of viack to Perl for less crazy

Fixes the "spaces in matched files" bug [GH #3] while I'm at it, because
it was simple.
  • Loading branch information...
commit 006ed27789c1349cd109e2a72df5cec2a48b7f6b 1 parent 89d8f7b
@tsibley authored
Showing with 36 additions and 19 deletions.
  1. +36 −19 viack
View
55 viack
@@ -1,24 +1,41 @@
-#!/bin/bash
-grabnext=0
-opts=''
-for arg; do
- if [[ $grabnext != 0 || ! "$arg" =~ ^- ]]; then
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+my ($grabnext, @vimopts, @files);
+
+# Extract the ack pattern to use when spawning vim
+for (@ARGV) {
+ if ($grabnext or not /^-/) {
# Try to make vim's magic mode more like Perl's standard regexes
# Magic chars gleaned from :help magic, and the most common problem
# characters are < and >
- pattern=$(echo -n "$arg" | perl -pe 's{([<>=@!%/&])}{\\$1}g')
- opts="+0 +/\\v$pattern"
- break
- elif [[ "$arg" == "--" ]]; then
- grabnext=1
- fi
-done
+ my $vimpat = $_;
+ $vimpat =~ s{([<>=@!%/&])}{\\$1}g;
+ push @vimopts, '+1', '+/\v'.$vimpat;
+ last;
+ }
+ elsif (/^--$/) {
+ $grabnext = 1;
+ }
+}
+
+# Capture output without dealing with shell escaping
+my $pid = open my $kid, "-|";
+die "Can't fork: $!" unless defined $pid;
-files=$(ack -l "$@")
+if ($pid) {
+ @files = <$kid>;
+ chomp for @files;
+ close $kid;
+} else {
+ exec ack => '-l', @ARGV
+ or die "Can't exec ack: $!";
+}
-if [[ $? == 0 ]]; then
- vim "$opts" $files
-else
- echo No matches to open.
- exit 1
-fi
+if (@files) {
+ exec vim => @vimopts, "--", @files
+ or die "Can't exec vim: $!";
+} else {
+ die "No matches to open.\n";
+}

0 comments on commit 006ed27

Please sign in to comment.
Something went wrong with that request. Please try again.