Given that my perl skills are much better than my lisp skills, I cheated and wrote a perl wrapper for mdfind that filters and sorts the results for me. The code is below – feel free to use and modify and share. To use this, save the code to a file called bjm-mdfind
in your $PATH
, make sure it has executable permissions set, and modify the lisp code above to use it:
;; Function to be called by counsel-spotlight
;;
;; mdfind is the command-line interface to spotlight
;;
;; The onlyin option limits results to my home directory
;; and directories below that
;;
;; N.B. below this is replaced with a custom perl wrapper to sort
;; and filter the mdfind results
(defun counsel-mdfind-function (string &rest _unused)
"Issue mdfind for STRING."
(if (< (length string) 4)
(counsel-more-chars 4)
(counsel--async-command
(format "bjm-mdfind '%s'" string))
;;(format "mdfind -onlyin ~/ '%s'" string))
nil))
Here is the perl code:
#!/usr/bin/perl -w
###############################################################################
# bjm-mdfind
#
# wrapper for mdfind to filter and sort results
# written by Ben Maughan http://pragmaticemacs.com/
#
# $Id: bjm-mdfind,v 1.2 2015/08/06 11:21:37 bjm Exp $
###############################################################################
use strict;
use File::Basename;
use Getopt::Long;
my $scriptname = basename($0); # Strip away the leading directory names
my $runtime = localtime;
##########################################################################
# customise these options #
##########################################################################
# limit search to this dir (recursively)
my $dir="~/";
# preferred order of file extensions
# others will appear later
my @order=(".org",".tex",".el",".txt",".dat",".pdf");
# exclude files matching these strings
my @exclude=("Library/Caches","Application Support");
###############################################################################
# Handle command line arguments
my $help;
my $man;
my $v=0; #set default verbosity
my $version = defined ((split / /, q/$Revision: 1.2 $/)[1]) ? (split / /, q/$Revision: 1.2 $/)[1] : 0;
my @args=@ARGV;
my $nargs=1; #number of required command line args
&Getopt::Long::Configure( 'bundling' );
GetOptions(
'help|h' => \$help,
'man|m' => \$man,
'verbose|v=i' => \$v,
) or die "ERROR: Invalid command line option $!";
if ($help||$man||$#ARGV<$nargs-1) { #print help
# Load Pod::Usage only if needed.
require "Pod/Usage.pm";
import Pod::Usage;
pod2usage(VERBOSE => 1) if $help;
pod2usage(VERBOSE => 2) if $man;
pod2usage(VERBOSE => 0, -message => "ERROR: not enough arguments use --help or --man for more help") if $#ARGV<0;
pod2usage(VERBOSE => 0, -message => "ERROR: not enough arguments - your input was:\n $scriptname @args") if $#ARGV<$nargs-1;
}
#check input
my $string=$ARGV[0];
###############################################################################
# Main part of program
if ($v > 0) {
print <<EOF
------------------------$scriptname version $version------------------
Invocation was:
$scriptname @args
Runtime $runtime
EOF
}
chomp(my @out=`mdfind -onlyin $dir $string`);
## sort and filter
##print "$order[2]\n\n";
##filter
my $exc=join "|", @exclude;
@out = grep !/$exc/, @out;
##sort
my @out2;
foreach my $ext (@order) {
my @tmp = grep /$ext$/, @out;
print "###$ext\n###@tmp\n\n" if $v>1;
push @out2, @tmp;
}
##everything else
my $inv=join "\$|", @order;
print "###$inv\n" if $v>1;
my @rest = grep !/$inv$/, @out;
print "###@rest\n" if $v>1;
push @out2, @rest;
##join
my $out=join "\n", @out2;
##print
print "$out\n";
#report successful completion
print "$scriptname completed successfully\n\n" if $v>0;
###############################################################################
# POD documentation
=head1 NAME
bjm-mdfind
=head1 SYNOPSIS
B<bjm-mdfind> [options] string
=head1 DESCRIPTION
wrapper for mdfind to filter and sort results
string - search query
=head1 OPTIONS
=over 4
=item B<-h, --help>
Prints out a brief help message.
=item B<-m, --man>
Prints out detailed help.
=item B<-v, --verbosity>
Control the amount of output B<(Default = 1)>
=back
=head1 VERSION
This is $RCSfile: bjm-mdfind,v $ $Revision: 1.2 $
=head1 AUTHOR
Ben Maughan <benmaughan@gmail.com>
=cut
###############################################################################
# $Log: bjm-mdfind,v $
# Revision 1.2 2015/08/06 11:21:37 bjm
# Summary: added URL
#
# Revision 1.1 2015/08/06 11:17:37 bjm
# Initial revision
#