]>
Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/ack
3 # This file, ack, is generated code.
4 # Please DO NOT EDIT or send patches for it.
6 # Please take a look at the source from
7 # http://github.com/petdance/ack
8 # and submit patches against the individual files
15 our $VERSION = '1.94';
16 # Check http://betterthangrep.com/ for updates
18 # These are all our globals.
22 if ( $App::Ack
::VERSION
ne $main::VERSION
) {
23 App
::Ack
::die( "Program/library version mismatch\n\t$0 is $main::VERSION\n\t$INC{'App/Ack.pm'} is $App::Ack::VERSION" );
26 # Do preliminary arg checking;
27 my $env_is_usable = 1;
29 last if ( $_ eq '--' );
31 # Priorities! Get the --thpppt checking out of the way.
32 /^--th[pt]+t+$/ && App
::Ack
::_thpppt
($_);
34 # See if we want to ignore the environment. (Don't tell Al Gore.)
35 if ( /^--(no)?env$/ ) {
36 $env_is_usable = defined $1 ? 0 : 1;
39 if ( $env_is_usable ) {
40 unshift( @ARGV, App
::Ack
::read_ackrc
() );
43 my @keys = ( 'ACKRC', grep { /^ACK_/ } keys %ENV );
46 App
::Ack
::load_colors
();
48 if ( exists $ENV{ACK_SWITCHES
} ) {
49 App
::Ack
::warn( 'ACK_SWITCHES is no longer supported. Use ACK_OPTIONS.' );
53 App
::Ack
::show_help
();
61 my $opt = App
::Ack
::get_command_line_options
();
63 $| = 1 if $opt->{flush
}; # Unbuffer the output if flush mode
65 if ( App
::Ack
::input_from_pipe
() ) {
66 # We're going into filter mode
68 $opt->{$_} and App
::Ack
::die( "Can't use -$_ when acting as a filter." );
70 $opt->{show_filename
} = 0;
71 $opt->{regex
} = App
::Ack
::build_regex
( defined $opt->{regex
} ? $opt->{regex
} : shift @ARGV, $opt );
72 if ( my $nargs = @ARGV ) {
73 my $s = $nargs == 1 ? '' : 's';
74 App
::Ack
::warn( "Ignoring $nargs argument$s on the command-line while acting as a filter." );
77 my $res = App
::Ack
::Resource
::Basic-
>new( '-' );
79 if ( $opt->{count
} ) {
80 $nmatches = App
::Ack
::search_and_list
( $res, $opt );
84 $nmatches = App
::Ack
::search_resource
( $res, $opt );
87 App
::Ack
::exit_from_ack
( $nmatches );
90 my $file_matching = $opt->{f
} || $opt->{lines
};
91 if ( $file_matching ) {
92 App
::Ack
::die( "Can't specify both a regex ($opt->{regex}) and use one of --line, -f or -g." ) if $opt->{regex
};
95 $opt->{regex
} = App
::Ack
::build_regex
( defined $opt->{regex
} ? $opt->{regex
} : shift @ARGV, $opt );
98 # check that all regexes do compile fine
99 App
::Ack
::check_regex
( $_ ) for ( $opt->{regex
}, $opt->{G
} );
101 my $what = App
::Ack
::get_starting_points
( \
@ARGV, $opt );
102 my $iter = App
::Ack
::get_iterator
( $what, $opt );
103 App
::Ack
::filetype_setup
();
107 App
::Ack
::set_up_pager
( $opt->{pager
} ) if defined $opt->{pager
};
109 $nmatches = App
::Ack
::print_files
( $iter, $opt );
111 elsif ( $opt->{l
} || $opt->{count
} ) {
112 $nmatches = App
::Ack
::print_files_with_matches
( $iter, $opt );
115 $nmatches = App
::Ack
::print_matches
( $iter, $opt );
118 App
::Ack
::exit_from_ack
( $nmatches );
123 ack - grep-like text finder
127 ack [options] PATTERN [FILE...]
128 ack -f [options] [DIRECTORY...]
132 Ack is designed as a replacement for 99% of the uses of F<grep>.
134 Ack searches the named input FILEs (or standard input if no files are
135 named, or the file name - is given) for lines containing a match to the
136 given PATTERN. By default, ack prints the matching lines.
138 Ack can also list files that would be searched, without actually searching
139 them, to let you take advantage of ack's file-type filtering capabilities.
141 =head1 FILE SELECTION
143 I<ack> is intelligent about the files it searches. It knows about
144 certain file types, based on both the extension on the file and,
145 in some cases, the contents of the file. These selections can be
146 made with the B<--type> option.
148 With no file selections, I<ack> only searches files of types that
149 it recognizes. If you have a file called F<foo.wango>, and I<ack>
150 doesn't know what a .wango file is, I<ack> won't search it.
152 The B<-a> option tells I<ack> to select all files, regardless of
155 Some files will never be selected by I<ack>, even with B<-a>,
160 =item * Backup files: Files matching F<#*#> or ending with F<~>.
162 =item * Coredumps: Files matching F<core.\d+>
166 However, I<ack> always searches the files given on the command line,
167 no matter what type. Furthermore, by specifying the B<-u> option all
168 files will be searched.
170 =head1 DIRECTORY SELECTION
172 I<ack> descends through the directory tree of the starting directories
173 specified. However, it will ignore the shadow directories used by
174 many version control systems, and the build directories used by the
175 Perl MakeMaker system. You may add or remove a directory from this
176 list with the B<--[no]ignore-dir> option. The option may be repeated
177 to add/remove multiple directories from the ignore list.
179 For a complete list of directories that do not get searched, run
182 =head1 WHEN TO USE GREP
184 I<ack> trumps I<grep> as an everyday tool 99% of the time, but don't
185 throw I<grep> away, because there are times you'll still need it.
187 E.g., searching through huge files looking for regexes that can be
188 expressed with I<grep> syntax should be quicker with I<grep>.
190 If your script or parent program uses I<grep> C<--quiet> or
191 C<--silent> or needs exit 2 on IO error, use I<grep>.
197 =item B<-a>, B<--all>
199 Operate on all files, regardless of type (but still skip directories
200 like F<blib>, F<CVS>, etc.)
202 =item B<-A I<NUM>>, B<--after-context=I<NUM>>
204 Print I<NUM> lines of trailing context after matching lines.
206 =item B<-B I<NUM>>, B<--before-context=I<NUM>>
208 Print I<NUM> lines of leading context before matching lines.
210 =item B<-C [I<NUM>]>, B<--context[=I<NUM>]>
212 Print I<NUM> lines (default 2) of context around matching lines.
214 =item B<-c>, B<--count>
216 Suppress normal output; instead print a count of matching lines for
217 each input file. If B<-l> is in effect, it will only show the
218 number of lines for each file that has lines matching. Without
219 B<-l>, some line counts may be zeroes.
221 If combined with B<-h> (B<--no-filename>) ack outputs only one total count.
223 =item B<--color>, B<--nocolor>
225 B<--color> highlights the matching text. B<--nocolor> supresses
226 the color. This is on by default unless the output is redirected.
228 On Windows, this option is off by default unless the
229 L<Win32::Console::ANSI> module is installed or the C<ACK_PAGER_COLOR>
230 environment variable is used.
232 =item B<--color-filename=I<color>>
234 Sets the color to be used for filenames.
236 =item B<--color-match=I<color>>
238 Sets the color to be used for matches.
240 =item B<--color-lineno=I<color>>
242 Sets the color to be used for line numbers.
246 Show the column number of the first match. This is helpful for editors
247 that can place your cursor at a given position.
249 =item B<--env>, B<--noenv>
251 B<--noenv> disables all environment processing. No F<.ackrc> is read
252 and all environment variables are ignored. By default, F<ack> considers
253 F<.ackrc> and settings in the environment.
257 B<--flush> flushes output immediately. This is off by default
258 unless ack is running interactively (when output goes to a pipe
263 Only print the files that would be searched, without actually doing
264 any searching. PATTERN must not be specified, or it will be taken as
267 =item B<--follow>, B<--nofollow>
269 Follow or don't follow symlinks, other than whatever starting files
270 or directories were specified on the command line.
272 This is off by default.
276 Only paths matching I<REGEX> are included in the search. The entire
277 path and filename are matched against I<REGEX>, and I<REGEX> is a
278 Perl regular expression, not a shell glob.
280 The options B<-i>, B<-w>, B<-v>, and B<-Q> do not apply to this I<REGEX>.
284 Print files where the relative path + filename matches I<REGEX>. This option is
285 a convenience shortcut for B<-f> B<-G I<REGEX>>.
287 The options B<-i>, B<-w>, B<-v>, and B<-Q> do not apply to this I<REGEX>.
289 =item B<--group>, B<--nogroup>
291 B<--group> groups matches by file name with. This is the default when
294 B<--nogroup> prints one result per line, like grep. This is the default
295 when output is redirected.
297 =item B<-H>, B<--with-filename>
299 Print the filename for each match.
301 =item B<-h>, B<--no-filename>
303 Suppress the prefixing of filenames on output when multiple files are
308 Print a short help statement.
310 =item B<-i>, B<--ignore-case>
312 Ignore case in the search strings.
314 This applies only to the PATTERN, not to the regexes given for the B<-g>
317 =item B<--[no]ignore-dir=I<DIRNAME>>
319 Ignore directory (as CVS, .svn, etc are ignored). May be used multiple times
320 to ignore multiple directories. For example, mason users may wish to include
321 B<--ignore-dir=data>. The B<--noignore-dir> option allows users to search
322 directories which would normally be ignored (perhaps to research the contents
323 of F<.svn/props> directories).
325 The I<DIRNAME> must always be a simple directory name. Nested directories like
326 F<foo/bar> are NOT supported. You would need to specify B<--ignore-dir=foo> and
327 then no files from any foo directory are taken into account by ack unless given
328 explicitly on the command line.
330 =item B<--line=I<NUM>>
332 Only print line I<NUM> of each file. Multiple lines can be given with multiple
333 B<--line> options or as a comma separated list (B<--line=3,5,7>). B<--line=4-7>
334 also works. The lines are always output in ascending order, no matter the
335 order given on the command line.
337 =item B<-l>, B<--files-with-matches>
339 Only print the filenames of matching files, instead of the matching text.
341 =item B<-L>, B<--files-without-matches>
343 Only print the filenames of files that do I<NOT> match. This is equivalent
344 to specifying B<-l> and B<-v>.
346 =item B<--match I<REGEX>>
348 Specify the I<REGEX> explicitly. This is helpful if you don't want to put the
349 regex as your first argument, e.g. when executing multiple searches over the
352 # search for foo and bar in given files
353 ack file1 t/file* --match foo
354 ack file1 t/file* --match bar
356 =item B<-m=I<NUM>>, B<--max-count=I<NUM>>
358 Stop reading a file after I<NUM> matches.
362 Print this manual page.
364 =item B<-n>, B<--no-recurse>
366 No descending into subdirectories.
370 Show only the part of each line matching PATTERN (turns off text
373 =item B<--output=I<expr>>
375 Output the evaluation of I<expr> for each line (turns off text
378 =item B<--pager=I<program>>
380 Direct ack's output through I<program>. This can also be specified
381 via the C<ACK_PAGER> and C<ACK_PAGER_COLOR> environment variables.
383 Using --pager does not suppress grouping and coloring like piping
384 output on the command-line does.
388 Prints all lines, whether or not they match the expression. Highlighting
389 will still work, though, so it can be used to highlight matches while
390 still seeing the entire file, as in:
392 # Watch a log file, and highlight a certain IP address
393 $ tail -f ~/access.log | ack --passthru 123.45.67.89
397 Only works in conjunction with -f, -g, -l or -c (filename output). The filenames
398 are output separated with a null byte instead of the usual newline. This is
399 helpful when dealing with filenames that contain whitespace, e.g.
401 # remove all files of type html
402 ack -f --html --print0 | xargs -0 rm -f
404 =item B<-Q>, B<--literal>
406 Quote all metacharacters in PATTERN, it is treated as a literal.
408 This applies only to the PATTERN, not to the regexes given for the B<-g>
411 =item B<-r>, B<-R>, B<--recurse>
413 Recurse into sub-directories. This is the default and just here for
414 compatibility with grep. You can also use it for turning B<--no-recurse> off.
416 =item B<--smart-case>, B<--no-smart-case>
418 Ignore case in the search strings if PATTERN contains no uppercase
419 characters. This is similar to C<smartcase> in vim. This option is
422 B<-i> always overrides this option.
424 This applies only to the PATTERN, not to the regexes given for the
425 B<-g> and B<-G> options.
427 =item B<--sort-files>
429 Sorts the found files lexically. Use this if you want your file
430 listings to be deterministic between runs of I<ack>.
432 =item B<--show-types>
434 Outputs the filetypes that ack associates with each file.
436 Works with B<-f> and B<-g> options.
440 Display the all-important Bill The Cat logo. Note that the exact
441 spelling of B<--thpppppt> is not important. It's checked against
442 a regular expression.
444 =item B<--type=TYPE>, B<--type=noTYPE>
446 Specify the types of files to include or exclude from a search.
447 TYPE is a filetype, like I<perl> or I<xml>. B<--type=perl> can
448 also be specified as B<--perl>, and B<--type=noperl> can be done
451 If a file is of both type "foo" and "bar", specifying --foo and
452 --nobar will exclude the file, because an exclusion takes precedence
455 Type specifications can be repeated and are ORed together.
457 See I<ack --help=types> for a list of valid types.
459 =item B<--type-add I<TYPE>=I<.EXTENSION>[,I<.EXT2>[,...]]>
461 Files with the given EXTENSION(s) are recognized as being of (the
462 existing) type TYPE. See also L</"Defining your own types">.
465 =item B<--type-set I<TYPE>=I<.EXTENSION>[,I<.EXT2>[,...]]>
467 Files with the given EXTENSION(s) are recognized as being of type
468 TYPE. This replaces an existing definition for type TYPE. See also
469 L</"Defining your own types">.
471 =item B<-u>, B<--unrestricted>
473 All files and directories (including blib/, core.*, ...) are searched,
474 nothing is skipped. When both B<-u> and B<--ignore-dir> are used, the
475 B<--ignore-dir> option has no effect.
477 =item B<-v>, B<--invert-match>
479 Invert match: select non-matching lines
481 This applies only to the PATTERN, not to the regexes given for the B<-g>
486 Display version and copyright information.
488 =item B<-w>, B<--word-regexp>
490 Force PATTERN to match only whole words. The PATTERN is wrapped with
491 C<\b> metacharacters.
493 This applies only to the PATTERN, not to the regexes given for the B<-g>
498 Stops after reporting first match of any kind. This is different
499 from B<--max-count=1> or B<-m1>, where only one match per file is
500 shown. Also, B<-1> works with B<-f> and B<-g>, where B<-m> does
505 =head1 THE .ackrc FILE
507 The F<.ackrc> file contains command-line options that are prepended
508 to the command line before processing. Multiple options may live
509 on multiple lines. Lines beginning with a # are ignored. A F<.ackrc>
510 might look like this:
512 # Always sort the files
515 # Always color, even if piping to a another program
518 # Use "less -r" as my pager
521 Note that arguments with spaces in them do not need to be quoted,
522 as they are not interpreted by the shell. Basically, each I<line>
523 in the F<.ackrc> file is interpreted as one element of C<@ARGV>.
525 F<ack> looks in your home directory for the F<.ackrc>. You can
526 specify another location with the F<ACKRC> variable, below.
528 If B<--noenv> is specified on the command line, the F<.ackrc> file
531 =head1 Defining your own types
533 ack allows you to define your own types in addition to the predefined
534 types. This is done with command line options that are best put into
535 an F<.ackrc> file - then you do not have to define your types over and
536 over again. In the following examples the options will always be shown
537 on one command line so that they can be easily copy & pasted.
539 I<ack --perl foo> searches for foo in all perl files. I<ack --help=types>
540 tells you, that perl files are files ending
541 in .pl, .pm, .pod or .t. So what if you would like to include .xs
542 files as well when searching for --perl files? I<ack --type-add perl=.xs --perl foo>
543 does this for you. B<--type-add> appends
544 additional extensions to an existing type.
546 If you want to define a new type, or completely redefine an existing
547 type, then use B<--type-set>. I<ack --type-set
548 eiffel=.e,.eiffel> defines the type I<eiffel> to include files with
549 the extensions .e or .eiffel. So to search for all eiffel files
550 containing the word Bertrand use I<ack --type-set eiffel=.e,.eiffel --eiffel Bertrand>.
551 As usual, you can also write B<--type=eiffel>
552 instead of B<--eiffel>. Negation also works, so B<--noeiffel> excludes
553 all eiffel files from a search. Redefining also works: I<ack --type-set cc=.c,.h>
554 and I<.xs> files no longer belong to the type I<cc>.
556 When defining your own types in the F<.ackrc> file you have to use
559 --type-set=eiffel=.e,.eiffel
561 or writing on separate lines
566 The following does B<NOT> work in the F<.ackrc> file:
568 --type-set eiffel=.e,.eiffel
571 In order to see all currently defined types, use I<--help types>, e.g.
572 I<ack --type-set backup=.bak --type-add perl=.perl --help types>
580 The types 'skipped', 'make', 'binary' and 'text' are considered "builtin" and
585 The shebang line recognition of the types 'perl', 'ruby', 'php', 'python',
586 'shell' and 'xml' cannot be redefined by I<--type-set>, it is always
587 active. However, the shebang line is only examined for files where the
588 extension is not recognised. Therefore it is possible to say
589 I<ack --type-set perl=.perl --type-set foo=.pl,.pm,.pod,.t --perl --nofoo> and
590 only find your shiny new I<.perl> files (and all files with unrecognized extension
591 and perl on the shebang line).
595 =head1 ENVIRONMENT VARIABLES
597 For commonly-used ack options, environment variables can make life much easier.
598 These variables are ignored if B<--noenv> is specified on the command line.
604 Specifies the location of the F<.ackrc> file. If this file doesn't
605 exist, F<ack> looks in the default location.
609 This variable specifies default options to be placed in front of
610 any explicit options on the command line.
612 =item ACK_COLOR_FILENAME
614 Specifies the color of the filename when it's printed in B<--group>
615 mode. By default, it's "bold green".
617 The recognized attributes are clear, reset, dark, bold, underline,
618 underscore, blink, reverse, concealed black, red, green, yellow,
619 blue, magenta, on_black, on_red, on_green, on_yellow, on_blue,
620 on_magenta, on_cyan, and on_white. Case is not significant.
621 Underline and underscore are equivalent, as are clear and reset.
622 The color alone sets the foreground color, and on_color sets the
625 This option can also be set with B<--color-filename>.
627 =item ACK_COLOR_MATCH
629 Specifies the color of the matching text when printed in B<--color>
630 mode. By default, it's "black on_yellow".
632 This option can also be set with B<--color-match>.
634 See B<ACK_COLOR_FILENAME> for the color specifications.
636 =item ACK_COLOR_LINENO
638 Specifies the color of the line number when printed in B<--color>
639 mode. By default, it's "bold yellow".
641 This option can also be set with B<--color-lineno>.
643 See B<ACK_COLOR_FILENAME> for the color specifications.
647 Specifies a pager program, such as C<more>, C<less> or C<most>, to which
648 ack will send its output.
650 Using C<ACK_PAGER> does not suppress grouping and coloring like
651 piping output on the command-line does, except that on Windows
652 ack will assume that C<ACK_PAGER> does not support color.
654 C<ACK_PAGER_COLOR> overrides C<ACK_PAGER> if both are specified.
656 =item ACK_PAGER_COLOR
658 Specifies a pager program that understands ANSI color sequences.
659 Using C<ACK_PAGER_COLOR> does not suppress grouping and coloring
660 like piping output on the command-line does.
662 If you are not on Windows, you never need to use C<ACK_PAGER_COLOR>.
666 =head1 ACK & OTHER TOOLS
668 =head2 Vim integration
670 F<ack> integrates easily with the Vim text editor. Set this in your
671 F<.vimrc> to use F<ack> instead of F<grep>:
675 That examples uses C<-a> to search through all files, but you may
676 use other default flags. Now you can search with F<ack> and easily
677 step through the results in Vim:
681 =head2 Emacs integration
683 Phil Jackson put together an F<ack.el> extension that "provides a
684 simple compilation mode ... has the ability to guess what files you
685 want to search for based on the major-mode."
687 L<http://www.shellarchive.co.uk/content/emacs.html>
689 =head2 TextMate integration
691 Pedro Melo is a TextMate user who writes "I spend my day mostly
692 inside TextMate, and the built-in find-in-project sucks with large
693 projects. So I hacked a TextMate command that was using find +
694 grep to use ack. The result is the Search in Project with ack, and
695 you can find it here:
696 L<http://www.simplicidade.org/notes/archives/2008/03/search_in_proje.html>"
698 =head2 Shell and Return Code
700 For greater compatibility with I<grep>, I<ack> in normal use returns
701 shell return or exit code of 0 only if something is found and 1 if
704 (Shell exit code 1 is C<$?=256> in perl with C<system> or backticks.)
706 The I<grep> code 2 for errors is not used.
708 If C<-f> or C<-g> are specified, then 0 is returned if at least one
709 file is found. If no files are found, then 1 is returned.
713 =head1 DEBUGGING ACK PROBLEMS
715 If ack gives you output you're not expecting, start with a few simple steps.
717 =head2 Use B<--noenv>
719 Your environment variables and F<.ackrc> may be doing things you're
720 not expecting, or forgotten you specified. Use B<--noenv> to ignore
721 your environment and F<.ackrc>.
723 =head2 Use B<-f> to see what files you're scanning
725 The reason I created B<-f> in the first place was as a debugging
726 tool. If ack is not finding matches you think it should find, run
727 F<ack -f> to see what files are being checked.
731 =head2 Use the F<.ackrc> file.
733 The F<.ackrc> is the place to put all your options you use most of
734 the time but don't want to remember. Put all your --type-add and
735 --type-set definitions in it. If you like --smart-case, set it
736 there, too. I also set --sort-files there.
738 =head2 Use F<-f> for working with big codesets
740 Ack does more than search files. C<ack -f --perl> will create a
741 list of all the Perl files in a tree, ideal for sending into F<xargs>.
744 # Change all "this" to "that" in all Perl files in a tree.
745 ack -f --perl | xargs perl -p -i -e's/this/that/g'
749 perl -p -i -e's/this/thatg/' $(ack -f --perl)
751 =head2 Use F<-Q> when in doubt about metacharacters
753 If you're searching for something with a regular expression
754 metacharacter, most often a period in a filename or IP address, add
755 the -Q to avoid false positives without all the backslashing. See
756 the following example for more...
758 =head2 Use ack to watch log files
760 Here's one I used the other day to find trouble spots for a website
761 visitor. The user had a problem loading F<troublesome.gif>, so I
762 took the access log and scanned it with ack twice.
764 ack -Q aa.bb.cc.dd /path/to/access.log | ack -Q -B5 troublesome.gif
766 The first ack finds only the lines in the Apache log for the given
767 IP. The second finds the match on my troublesome GIF, and shows
768 the previous five lines from the log in each case.
770 =head2 Share your knowledge
772 Join the ack-users mailing list. Send me your tips and I may add
777 =head2 Why isn't ack finding a match in (some file)?
779 Probably because it's of a type that ack doesn't recognize. ack's
780 searching behavior is driven by filetype. B<If ack doesn't know
781 what kind of file it is, ack ignores the file.>
783 Use the C<-f> switch to see a list of files that ack will search
786 If you want ack to search files that it doesn't recognize, use the
789 If you want ack to search every file, even ones that it always
790 ignores like coredumps and backup files, use the C<-u> switch.
792 =head2 Why does ack ignore unknown files by default?
794 ack is designed by a programmer, for programmers, for searching
795 large trees of code. Most codebases have a lot files in them which
796 aren't source files (like compiled object files, source control
797 metadata, etc), and grep wastes a lot of time searching through all
798 of those as well and returning matches from those files.
800 That's why ack's behavior of not searching things it doesn't recognize
801 is one of its greatest strengths: the speed you get from only
802 searching the things that you want to be looking at.
804 =head2 Wouldn't it be great if F<ack> did search & replace?
806 No, ack will always be read-only. Perl has a perfectly good way
807 to do search & replace in files, using the C<-i>, C<-p> and C<-n>
810 You can certainly use ack to select your files to update. For
811 example, to change all "foo" to "bar" in all PHP files, you can do
812 this from the Unix shell:
814 $ perl -i -p -e's/foo/bar/g' $(ack -f --php)
816 =head2 Can you make ack recognize F<.xyz> files?
818 That's an enhancement. Please see the section in the manual about
821 =head2 There's already a program/package called ack.
825 =head2 Why is it called ack if it's called ack-grep?
827 The name of the program is "ack". Some packagers have called it
828 "ack-grep" when creating packages because there's already a package
829 out there called "ack" that has nothing to do with this ack.
831 I suggest you make a symlink named F<ack> that points to F<ack-grep>
832 because one of the crucial benefits of ack is having a name that's
833 so short and simple to type.
835 To do that, run this with F<sudo> or as root:
837 ln -s /usr/bin/ack-grep /usr/bin/ack
839 =head2 What does F<ack> mean?
841 Nothing. I wanted a name that was easy to type and that you could
842 pronounce as a single syllable.
844 =head2 Can I do multi-line regexes?
846 No, ack does not support regexes that match multiple lines. Doing
847 so would require reading in the entire file at a time.
849 If you want to see lines near your match, use the C<--A>, C<--B>
850 and C<--C> switches for displaying context.
854 Andy Lester, C<< <andy at petdance.com> >>
858 Please report any bugs or feature requests to the issues list at
859 Github: L<http://github.com/petdance/ack/issues>
863 All enhancement requests MUST first be posted to the ack-users
864 mailing list at L<http://groups.google.com/group/ack-users>. I
865 will not consider a request without it first getting seen by other
866 ack users. This includes requests for new filetypes.
868 There is a list of enhancements I want to make to F<ack> in the ack
869 issues list at Github: L<http://github.com/petdance/ack/issues>
871 Patches are always welcome, but patches with tests get the most
876 Support for and information about F<ack> can be found at:
880 =item * The ack homepage
882 L<http://betterthangrep.com/>
884 =item * The ack issues list at Github
886 L<http://github.com/petdance/ack/issues>
888 =item * AnnoCPAN: Annotated CPAN documentation
890 L<http://annocpan.org/dist/ack>
894 L<http://cpanratings.perl.org/d/ack>
898 L<http://search.cpan.org/dist/ack>
900 =item * Git source repository
902 L<http://github.com/petdance/ack>
906 =head1 ACKNOWLEDGEMENTS
908 How appropriate to have I<ack>nowledgements!
910 Thanks to everyone who has contributed to ack in any way, including
931 Christopher J. Madsen,
943 GE<aacute>bor SzabE<oacute>,
946 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason,
950 Mark Leighton Fisher,
961 Ask BjE<oslash>rn Hansen,
974 =head1 COPYRIGHT & LICENSE
976 Copyright 2005-2010 Andy Lester.
978 This program is free software; you can redistribute it and/or modify
979 it under the terms of the Artistic License v2.0.
988 our $VERSION = '1.06';
995 our $name; # name of the current file
996 our $dir; # dir of the current file
1003 file_filter
=> undef,
1004 descend_filter
=> undef,
1005 error_handler
=> sub { CORE
::die @_ },
1006 sort_files
=> undef,
1007 follow_symlinks
=> 1,
1009 %skip_dirs = map {($_,1)} (File
::Spec-
>curdir, File
::Spec-
>updir);
1014 ($_[0] eq __PACKAGE__
) && die 'File::Next::files must not be invoked as File::Next->files';
1016 my ($parms,@queue) = _setup
( \
%files_defaults, @_ );
1017 my $filter = $parms->{file_filter
};
1021 my ($dir,$file,$fullpath) = splice( @queue, 0, 3 );
1022 if ( -f
$fullpath ) {
1025 local $File::Next
::dir
= $dir;
1026 local $File::Next
::name
= $fullpath;
1027 next if not $filter->();
1029 return wantarray ? ($dir,$file,$fullpath) : $fullpath;
1032 unshift( @queue, _candidate_files
( $parms, $fullpath ) );
1046 sub sort_standard
($$) { return $_[0]->[1] cmp $_[1]->[1] }
1047 sub sort_reverse
($$) { return $_[1]->[1] cmp $_[0]->[1] }
1052 my @parts = split( /\//, $path );
1054 return $path if @parts < 2;
1056 return File
::Spec-
>catfile( @parts );
1062 my $defaults = shift;
1063 my $passed_parms = ref $_[0] eq 'HASH' ? {%{+shift}} : {}; # copy parm hash
1065 my %passed_parms = %{$passed_parms};
1068 for my $key ( keys %{$defaults} ) {
1070 exists $passed_parms{$key}
1071 ? delete $passed_parms{$key}
1072 : $defaults->{$key};
1075 # Any leftover keys are bogus
1076 for my $badkey ( keys %passed_parms ) {
1077 my $sub = (caller(1))[3];
1078 $parms->{error_handler
}->( "Invalid option passed to $sub(): $badkey" );
1081 # If it's not a code ref, assume standard sort
1082 if ( $parms->{sort_files
} && ( ref($parms->{sort_files
}) ne 'CODE' ) ) {
1083 $parms->{sort_files
} = \
&sort_standard
;
1088 my $start = reslash
( $_ );
1090 push @queue, ($start,undef,$start);
1093 push @queue, (undef,$start,$start);
1097 return ($parms,@queue);
1101 sub _candidate_files
{
1106 if ( !opendir $dh, $dir ) {
1107 $parms->{error_handler
}->( "$dir: $!" );
1112 my $descend_filter = $parms->{descend_filter
};
1113 my $follow_symlinks = $parms->{follow_symlinks
};
1114 my $sort_sub = $parms->{sort_files
};
1116 for my $file ( grep { !exists $skip_dirs{$_} } readdir $dh ) {
1119 # Only do directory checking if we have a descend_filter
1120 my $fullpath = File
::Spec-
>catdir( $dir, $file );
1121 if ( !$follow_symlinks ) {
1122 next if -l
$fullpath;
1126 if ( $descend_filter ) {
1127 if ( $has_stat ? (-d _
) : (-d
$fullpath) ) {
1128 local $File::Next
::dir
= $fullpath;
1130 next if not $descend_filter->();
1134 push( @newfiles, [ $dir, $file, $fullpath ] );
1137 push( @newfiles, $dir, $file, $fullpath );
1143 return map { @{$_} } sort $sort_sub @newfiles;
1150 1; # End of File::Next
1163 $COPYRIGHT = 'Copyright 2005-2010 Andy Lester.';
1178 our $input_from_pipe;
1179 our $output_to_pipe;
1186 use File
::Glob
':glob';
1187 use Getopt
::Long
();
1192 '.cdv' => 'Codeville',
1193 '~.dep' => 'Interface Builder',
1194 '~.dot' => 'Interface Builder',
1195 '~.nib' => 'Interface Builder',
1196 '~.plst' => 'Interface Builder',
1198 '.hg' => 'Mercurial',
1200 '.svn' => 'Subversion',
1202 blib
=> 'Perl module building',
1207 _sgbak
=> 'Vault/Fortress',
1208 'autom4te.cache' => 'autoconf',
1209 'cover_db' => 'Devel::Cover',
1210 _build
=> 'Module::Build',
1214 actionscript
=> [qw( as mxml )],
1215 ada
=> [qw( ada adb ads )],
1216 asm
=> [qw( asm s )],
1217 batch
=> [qw( bat cmd )],
1218 binary
=> q{Binary files, as defined by Perl's -B op (default: off)},
1219 cc
=> [qw( c h xs )],
1220 cfmx
=> [qw( cfc cfm cfml )],
1221 clojure
=> [qw( clj )],
1222 cpp
=> [qw( cpp cc cxx m hpp hh h hxx )],
1223 csharp
=> [qw( cs )],
1225 delphi
=> [qw( pas int dfm nfm dof dpk dproj groupproj bdsgroup bdsproj )],
1226 elisp
=> [qw( el )],
1227 erlang
=> [qw( erl hrl )],
1228 fortran
=> [qw( f f77 f90 f95 f03 for ftn fpp )],
1230 groovy
=> [qw( groovy gtmpl gpp grunit )],
1231 haskell
=> [qw( hs lhs )],
1233 html
=> [qw( htm html shtml xhtml )],
1234 java
=> [qw( java properties )],
1236 jsp
=> [qw( jsp jspx jhtm jhtml )],
1237 lisp
=> [qw( lisp lsp )],
1239 make
=> q{Makefiles (including *.mk and *.mak)},
1240 mason
=> [qw( mas mhtml mpl mtxt )],
1241 objc
=> [qw( m h )],
1242 objcpp
=> [qw( mm h )],
1243 ocaml
=> [qw( ml mli )],
1244 parrot
=> [qw( pir pasm pmc ops pod pg tg )],
1245 perl
=> [qw( pl pm pod t )],
1246 php
=> [qw( php phpt php3 php4 php5 phtml)],
1247 plone
=> [qw( pt cpt metadata cpy py )],
1248 python
=> [qw( py )],
1249 rake
=> q{Rakefiles},
1250 ruby
=> [qw( rb rhtml rjs rxml erb rake spec )],
1251 scala
=> [qw( scala )],
1252 scheme
=> [qw( scm ss )],
1253 shell
=> [qw( sh bash csh tcsh ksh zsh )],
1254 skipped
=> q{Files, but not directories, normally skipped by ack (default: off)},
1255 smalltalk
=> [qw( st )],
1256 sql
=> [qw( sql ctl )],
1257 tcl
=> [qw( tcl itcl itk )],
1258 tex
=> [qw( tex cls sty )],
1259 text
=> q{Text files, as defined by Perl's -T op (default: off)},
1260 tt
=> [qw( tt tt2 ttml )],
1261 vb
=> [qw( bas cls frm ctl vb resx )],
1262 verilog
=> [qw( v vh sv )],
1263 vhdl
=> [qw( vhd vhdl )],
1265 yaml
=> [qw( yaml yml )],
1266 xml
=> [qw( xml dtd xsl xslt ent )],
1269 while ( my ($type,$exts) = each %mappings ) {
1271 for my $ext ( @{$exts} ) {
1272 push( @{$types{$ext}}, $type );
1276 # add manually Makefile extensions
1277 push @{$types{$_}}, 'make' for qw{ mk mak };
1279 # These have to be checked before any filehandle diddling.
1280 $output_to_pipe = not -t
*STDOUT
;
1281 $input_from_pipe = -p STDIN
;
1283 $is_cygwin = ($^O eq 'cygwin');
1284 $is_windows = ($^O =~ /MSWin32/);
1285 $dir_sep_chars = $is_windows ? quotemeta( '\\/' ) : quotemeta( File
::Spec-
>catfile( '', '' ) );
1290 my @files = ( $ENV{ACKRC
} );
1293 ? ( $ENV{HOME
}, $ENV{USERPROFILE
} )
1294 : ( '~', $ENV{HOME
} );
1295 for my $dir ( grep { defined } @dirs ) {
1296 for my $file ( '.ackrc', '_ackrc' ) {
1297 push( @files, bsd_glob
( "$dir/$file", GLOB_TILDE
) );
1300 for my $filename ( @files ) {
1301 if ( defined $filename && -e
$filename ) {
1302 open( my $fh, '<', $filename ) or App
::Ack
::die( "$filename: $!\n" );
1303 my @lines = grep { /./ && !/^\s*#/ } <$fh>;
1305 close $fh or App
::Ack
::die( "$filename: $!\n" );
1307 # get rid of leading and trailing whitespaces
1321 sub get_command_line_options
{
1323 pager
=> $ENV{ACK_PAGER_COLOR
} || $ENV{ACK_PAGER
},
1326 my $getopt_specs = {
1327 1 => sub { $opt{1} = $opt{m
} = 1 },
1328 'A|after-context=i' => \
$opt{after_context
},
1329 'B|before-context=i' => \
$opt{before_context
},
1330 'C|context:i' => sub { shift; my $val = shift; $opt{before_context
} = $opt{after_context
} = ($val || 2) },
1331 'a|all-types' => \
$opt{all
},
1332 'break!' => \
$opt{break},
1334 'color|colour!' => \
$opt{color
},
1335 'color-match=s' => \
$ENV{ACK_COLOR_MATCH
},
1336 'color-filename=s' => \
$ENV{ACK_COLOR_FILENAME
},
1337 'color-lineno=s' => \
$ENV{ACK_COLOR_LINENO
},
1338 'column!' => \
$opt{column
},
1339 count
=> \
$opt{count
},
1340 'env!' => sub { }, # ignore this option, it is handled beforehand
1342 flush
=> \
$opt{flush
},
1343 'follow!' => \
$opt{follow
},
1344 'g=s' => sub { shift; $opt{G
} = shift; $opt{f
} = 1 },
1346 'group!' => sub { shift; $opt{heading
} = $opt{break} = shift },
1347 'heading!' => \
$opt{heading
},
1348 'h|no-filename' => \
$opt{h
},
1349 'H|with-filename' => \
$opt{H
},
1350 'i|ignore-case' => \
$opt{i
},
1351 'invert-file-match' => \
$opt{invert_file_match
},
1352 'lines=s' => sub { shift; my $val = shift; push @{$opt{lines
}}, $val },
1353 'l|files-with-matches' => \
$opt{l
},
1354 'L|files-without-matches' => sub { $opt{l
} = $opt{v
} = 1 },
1355 'm|max-count=i' => \
$opt{m
},
1356 'match=s' => \
$opt{regex
},
1357 'n|no-recurse' => \
$opt{n
},
1358 o
=> sub { $opt{output
} = '$&' },
1359 'output=s' => \
$opt{output
},
1360 'pager=s' => \
$opt{pager
},
1361 'nopager' => sub { $opt{pager
} = undef },
1362 'passthru' => \
$opt{passthru
},
1363 'print0' => \
$opt{print0
},
1364 'Q|literal' => \
$opt{Q
},
1365 'r|R|recurse' => sub { $opt{n
} = 0 },
1366 'show-types' => \
$opt{show_types
},
1367 'smart-case!' => \
$opt{smart_case
},
1368 'sort-files' => \
$opt{sort_files
},
1369 'u|unrestricted' => \
$opt{u
},
1370 'v|invert-match' => \
$opt{v
},
1371 'w|word-regexp' => \
$opt{w
},
1373 'ignore-dirs=s' => sub { shift; my $dir = remove_dir_sep
( shift ); $ignore_dirs{$dir} = '--ignore-dirs' },
1374 'noignore-dirs=s' => sub { shift; my $dir = remove_dir_sep
( shift ); delete $ignore_dirs{$dir} },
1376 'version' => sub { print_version_statement
(); exit; },
1377 'help|?:s' => sub { shift; show_help
(@_); exit; },
1378 'help-types'=> sub { show_help_types
(); exit; },
1381 Pod
::Usage
::pod2usage
({
1388 # Whatever --type=xxx they specify, set it manually in the hash
1391 my $wanted = ($type =~ s/^no//) ? 0 : 1; # must not be undef later
1393 if ( exists $type_wanted{ $type } ) {
1394 $type_wanted{ $type } = $wanted;
1397 App
::Ack
::die( qq{Unknown --type "$type"} );
1402 # Stick any default switches at the beginning, so they can be overridden
1403 # by the command line switches.
1404 unshift @ARGV, split( ' ', $ENV{ACK_OPTIONS
} ) if defined $ENV{ACK_OPTIONS
};
1406 # first pass through options, looking for type definitions
1407 def_types_from_ARGV
();
1409 for my $i ( filetypes_supported
() ) {
1410 $getopt_specs->{ "$i!" } = \
$type_wanted{ $i };
1414 my $parser = Getopt
::Long
::Parser-
>new();
1415 $parser->configure( 'bundling', 'no_ignore_case', );
1416 $parser->getoptions( %{$getopt_specs} ) or
1417 App
::Ack
::die( 'See ack --help, ack --help-types or ack --man for options.' );
1419 my $to_screen = not output_to_pipe
();
1422 color
=> $to_screen,
1424 break => $to_screen,
1425 heading
=> $to_screen,
1426 before_context
=> 0,
1429 if ( $is_windows && $defaults{color
} && not $ENV{ACK_PAGER_COLOR
} ) {
1430 if ( $ENV{ACK_PAGER
} || not eval { require Win32
::Console
::ANSI
} ) {
1431 $defaults{color
} = 0;
1434 if ( $to_screen && $ENV{ACK_PAGER_COLOR
} ) {
1435 $defaults{color
} = 1;
1438 while ( my ($key,$value) = each %defaults ) {
1439 if ( not defined $opt{$key} ) {
1440 $opt{$key} = $value;
1444 if ( defined $opt{m
} && $opt{m
} <= 0 ) {
1445 App
::Ack
::die( '-m must be greater than zero' );
1448 for ( qw( before_context after_context ) ) {
1449 if ( defined $opt{$_} && $opt{$_} < 0 ) {
1450 App
::Ack
::die( "--$_ may not be negative" );
1454 if ( defined( my $val = $opt{output
} ) ) {
1455 $opt{output
} = eval qq[ sub { "$val" } ];
1457 if ( defined( my $l = $opt{lines
} ) ) {
1458 # --line=1 --line=5 is equivalent to --line=1,5
1459 my @lines = split( /,/, join( ',', @{$l} ) );
1461 # --line=1-3 is equivalent to --line=1,2,3
1465 my ($from, $to) = split /-/, $_;
1466 if ( $from > $to ) {
1467 App
::Ack
::warn( "ignoring --line=$from-$to" );
1471 @ret = ( $from .. $to );
1482 @uniq{ @lines } = ();
1483 $opt{lines
} = [ sort { $a <=> $b } keys %uniq ]; # numerical sort and each line occurs only once!
1486 # happens if there are only ignored --line directives
1487 App
::Ack
::die( 'All --line options are invalid.' );
1495 sub def_types_from_ARGV
{
1498 my $parser = Getopt
::Long
::Parser-
>new();
1499 # pass_through => leave unrecognized command line arguments alone
1500 # no_auto_abbrev => otherwise -c is expanded and not left alone
1501 $parser->configure( 'no_ignore_case', 'pass_through', 'no_auto_abbrev' );
1502 $parser->getoptions(
1503 'type-set=s' => sub { shift; push @typedef, ['c', shift] },
1504 'type-add=s' => sub { shift; push @typedef, ['a', shift] },
1505 ) or App
::Ack
::die( 'See ack --help or ack --man for options.' );
1507 for my $td (@typedef) {
1508 my ($type, $ext) = split /=/, $td->[1];
1510 if ( $td->[0] eq 'c' ) {
1512 if ( exists $mappings{$type} ) {
1513 # can't redefine types 'make', 'skipped', 'text' and 'binary'
1514 App
::Ack
::die( qq{--type-set: Builtin type "$type" cannot be changed.} )
1515 if ref $mappings{$type} ne 'ARRAY';
1523 # can't append to types 'make', 'skipped', 'text' and 'binary'
1524 App
::Ack
::die( qq{--type-add: Builtin type "$type" cannot be changed.} )
1525 if exists $mappings{$type} && ref $mappings{$type} ne 'ARRAY';
1527 App
::Ack
::warn( qq{--type-add: Type "$type" does not exist, creating with "$ext" ...} )
1528 unless exists $mappings{$type};
1531 my @exts = split /,/, $ext;
1534 if ( !exists $mappings{$type} || ref($mappings{$type}) eq 'ARRAY' ) {
1535 push @{$mappings{$type}}, @exts;
1536 for my $e ( @exts ) {
1537 push @{$types{$e}}, $type;
1541 App
::Ack
::die( qq{Cannot append to type "$type".} );
1552 App
::Ack
::die( qq{Internal error: Cannot delete builtin type "$type".} )
1553 unless ref $mappings{$type} eq 'ARRAY';
1555 delete $mappings{$type};
1556 delete $type_wanted{$type};
1557 for my $ext ( keys %types ) {
1558 $types{$ext} = [ grep { $_ ne $type } @{$types{$ext}} ];
1563 sub ignoredir_filter
{
1564 return !exists $ignore_dirs{$_} && !exists $ignore_dirs{$File::Next
::dir
};
1568 sub remove_dir_sep
{
1570 $path =~ s/[$dir_sep_chars]$//;
1576 use constant TEXT
=> 'text';
1579 my $filename = shift;
1581 my $basename = $filename;
1582 $basename =~ s{.*[$dir_sep_chars]}{};
1584 return 'skipped' unless is_searchable
( $basename );
1586 my $lc_basename = lc $basename;
1587 return ('make',TEXT
) if $lc_basename eq 'makefile' || $lc_basename eq 'gnumakefile';
1588 return ('rake','ruby',TEXT
) if $lc_basename eq 'rakefile';
1590 # If there's an extension, look it up
1591 if ( $filename =~ m{\.([^\.$dir_sep_chars]+)$}o ) {
1592 my $ref = $types{lc $1};
1593 return (@{$ref},TEXT
) if $ref;
1596 # At this point, we can't tell from just the name. Now we have to
1597 # open it and look inside.
1599 return unless -e
$filename;
1600 # From Elliot Shank:
1601 # I can't see any reason that -r would fail on these-- the ACLs look
1602 # fine, and no program has any of them open, so the busted Windows
1603 # file locking model isn't getting in there. If I comment the if
1604 # statement out, everything works fine
1605 # So, for cygwin, don't bother trying to check for readability.
1606 if ( !$is_cygwin ) {
1607 if ( !-r
$filename ) {
1608 App
::Ack
::warn( "$filename: Permission denied" );
1613 return 'binary' if -B
$filename;
1615 # If there's no extension, or we don't recognize it, check the shebang line
1617 if ( !open( $fh, '<', $filename ) ) {
1618 App
::Ack
::warn( "$filename: $!" );
1624 if ( $header =~ /^#!/ ) {
1625 return ($1,TEXT
) if $header =~ /\b(ruby|p(?:erl|hp|ython))\b/;
1626 return ('shell',TEXT
) if $header =~ /\b(?:ba|t?c|k|z)?sh\b/;
1629 return ('xml',TEXT
) if $header =~ /\Q<?xml /i;
1637 my $filename = shift;
1639 # If these are updated, update the --help message
1640 return if $filename =~ /[.]bak$/;
1641 return if $filename =~ /~$/;
1642 return if $filename =~ m{^#.*#$}o;
1643 return if $filename =~ m{^core\.\d+$}o;
1644 return if $filename =~ m{[._].*\.swp$}o;
1654 defined $str or App
::Ack
::die( 'No regular expression found.' );
1656 $str = quotemeta( $str ) if $opt->{Q
};
1658 $str = "\\b$str" if $str =~ /^\w/;
1659 $str = "$str\\b" if $str =~ /\w$/;
1662 my $regex_is_lc = $str eq lc $str;
1663 if ( $opt->{i
} || ($opt->{smart_case
} && $regex_is_lc) ) {
1674 return unless defined $regex;
1676 eval { qr/$regex/ };
1678 (my $error = $@) =~ s/ at \S+ line \d+.*//;
1680 App
::Ack
::die( "Invalid regex '$regex':\n $error" );
1690 return CORE
::warn( _my_program
(), ': ', @_, "\n" );
1695 return CORE
::die( _my_program
(), ': ', @_, "\n" );
1699 require File
::Basename
;
1700 return File
::Basename
::basename
( $0 );
1705 sub filetypes_supported
{
1706 return keys %mappings;
1710 my $y = q{_ /|,\\'!.x',=(www)=, U };
1711 $y =~ tr/,x!w/\nOo_/;
1716 my $y = _get_thpppt
();
1717 App
::Ack
::print( "$y ack $_[0]!\n" );
1723 $str =~ s/[^a-z]//g;
1730 my $help_arg = shift || 0;
1732 return show_help_types
() if $help_arg =~ /^types?/;
1734 my $ignore_dirs = _listify
( sort { _key
($a) cmp _key
($b) } keys %ignore_dirs );
1736 App
::Ack
::print( <<"END_OF_HELP" );
1737 Usage: ack [OPTION]... PATTERN [FILE]
1739 Search for PATTERN in each source file in the tree from cwd on down.
1740 If [FILES] is specified, then only those files/directories are checked.
1741 ack may also search STDIN, but only if no FILE are specified, or if
1742 one of FILES is "-".
1744 Default switches may be specified in ACK_OPTIONS environment variable or
1745 an .ackrc file. If you want no dependency on the environment, turn it
1748 Example: ack -i select
1751 -i, --ignore-case Ignore case distinctions in PATTERN
1752 --[no]smart-case Ignore case distinctions in PATTERN,
1753 only if PATTERN contains no upper case
1754 Ignored if -i is specified
1755 -v, --invert-match Invert match: select non-matching lines
1756 -w, --word-regexp Force PATTERN to match only whole words
1757 -Q, --literal Quote all metacharacters; PATTERN is literal
1760 --line=NUM Only print line(s) NUM of each file
1761 -l, --files-with-matches
1762 Only print filenames containing matches
1763 -L, --files-without-matches
1764 Only print filenames with no matches
1765 -o Show only the part of a line matching PATTERN
1766 (turns off text highlighting)
1767 --passthru Print all lines, whether matching or not
1768 --output=expr Output the evaluation of expr for each line
1769 (turns off text highlighting)
1770 --match PATTERN Specify PATTERN explicitly.
1771 -m, --max-count=NUM Stop searching in each file after NUM matches
1772 -1 Stop searching after one match of any kind
1773 -H, --with-filename Print the filename for each match
1774 -h, --no-filename Suppress the prefixing filename on output
1775 -c, --count Show number of lines matching per file
1776 --column Show the column number of the first match
1778 -A NUM, --after-context=NUM
1779 Print NUM lines of trailing context after matching
1781 -B NUM, --before-context=NUM
1782 Print NUM lines of leading context before matching
1784 -C [NUM], --context[=NUM]
1785 Print NUM lines (default 2) of output context.
1787 --print0 Print null byte as separator between filenames,
1788 only works with -f, -g, -l, -L or -c.
1791 --pager=COMMAND Pipes all ack output through COMMAND. For example,
1792 --pager="less -R". Ignored if output is redirected.
1793 --nopager Do not send output through a pager. Cancels any
1794 setting in ~/.ackrc, ACK_PAGER or ACK_PAGER_COLOR.
1795 --[no]heading Print a filename heading above each file's results.
1796 (default: on when used interactively)
1797 --[no]break Print a break between results from different files.
1798 (default: on when used interactively)
1799 --group Same as --heading --break
1800 --nogroup Same as --noheading --nobreak
1801 --[no]color Highlight the matching text (default: on unless
1802 output is redirected, or on Windows)
1803 --[no]colour Same as --[no]color
1804 --color-filename=COLOR
1806 --color-lineno=COLOR Set the color for filenames, matches, and line numbers.
1807 --flush Flush output immediately, even when ack is used
1808 non-interactively (when output goes to a pipe or
1812 -f Only print the files found, without searching.
1813 The PATTERN must not be specified.
1814 -g REGEX Same as -f, but only print files matching REGEX.
1815 --sort-files Sort the found files lexically.
1816 --invert-file-match Print/search handle files that do not match -g/-G.
1817 --show-types Show which types each file has.
1819 File inclusion/exclusion:
1820 -a, --all-types All file types searched;
1821 Ignores CVS, .svn and other ignored directories
1822 -u, --unrestricted All files and directories searched
1823 --[no]ignore-dir=name Add/Remove directory from the list of ignored dirs
1824 -r, -R, --recurse Recurse into subdirectories (ack's default behavior)
1825 -n, --no-recurse No descending into subdirectories
1826 -G REGEX Only search files that match REGEX
1828 --perl Include only Perl files.
1829 --type=perl Include only Perl files.
1830 --noperl Exclude Perl files.
1831 --type=noperl Exclude Perl files.
1832 See "ack --help type" for supported filetypes.
1834 --type-set TYPE=.EXTENSION[,.EXT2[,...]]
1835 Files with the given EXTENSION(s) are recognized as
1836 being of type TYPE. This replaces an existing
1837 definition for type TYPE.
1838 --type-add TYPE=.EXTENSION[,.EXT2[,...]]
1839 Files with the given EXTENSION(s) are recognized as
1840 being of (the existing) type TYPE
1842 --[no]follow Follow symlinks. Default is off.
1844 Directories ignored by default:
1847 Files not checked for type:
1848 /~\$/ - Unix backup files
1849 /#.+#\$/ - Emacs swap files
1850 /[._].*\\.swp\$/ - Vi(m) swap files
1851 /core\\.\\d+\$/ - core dumps
1854 --noenv Ignore environment variables and ~/.ackrc
1857 --version Display version & copyright
1858 --thpppt Bill the Cat
1860 Exit status is 0 if match, 1 if no match.
1862 This is version $VERSION of ack.
1870 sub show_help_types
{
1871 App
::Ack
::print( <<'END_OF_HELP' );
1872 Usage: ack [OPTION]... PATTERN [FILES]
1874 The following is the list of filetypes supported by ack. You can
1875 specify a file type with the --type=TYPE format, or the --TYPE
1876 format. For example, both --type=perl and --perl work.
1878 Note that some extensions may appear in multiple types. For example,
1879 .pod files are both Perl and Parrot.
1883 my @types = filetypes_supported
();
1886 $maxlen = length if $maxlen < length;
1888 for my $type ( sort @types ) {
1889 next if $type =~ /^-/; # Stuff to not show
1890 my $ext_list = $mappings{$type};
1892 if ( ref $ext_list ) {
1893 $ext_list = join( ' ', map { ".$_" } @{$ext_list} );
1895 App
::Ack
::print( sprintf( " --[no]%-*.*s %s\n", $maxlen, $maxlen, $type, $ext_list ) );
1904 return '' if !@whats;
1906 my $end = pop @whats;
1907 my $str = @whats ? join( ', ', @whats ) . " and $end" : $end;
1911 $Text::Wrap
::columns
= 75;
1912 return Text
::Wrap
::wrap
( '', ' ', $str );
1916 sub get_version_statement
{
1919 my $copyright = get_copyright
();
1920 my $this_perl = $Config::Config
{perlpath
};
1922 my $ext = $Config::Config
{_exe
};
1923 $this_perl .= $ext unless $this_perl =~ m/$ext$/i;
1925 my $ver = sprintf( '%vd', $^V );
1927 return <<"END_OF_VERSION";
1929 Running under Perl $ver at $this_perl
1933 This program is free software. You may modify or distribute it
1934 under the terms of the Artistic License v2.0.
1939 sub print_version_statement
{
1940 App
::Ack
::print( get_version_statement
() );
1952 eval 'use Term::ANSIColor ()';
1954 $ENV{ACK_COLOR_MATCH
} ||= 'black on_yellow';
1955 $ENV{ACK_COLOR_FILENAME
} ||= 'bold green';
1956 $ENV{ACK_COLOR_LINENO
} ||= 'bold yellow';
1962 sub is_interesting
{
1967 for my $type ( filetypes
( $File::Next
::name
) ) {
1968 if ( defined $type_wanted{$type} ) {
1969 if ( $type_wanted{$type} ) {
1983 # print subs added in order to make it easy for a third party
1984 # module (such as App::Wack) to redefine the display methods
1985 # and show the results in a different way.
1986 sub print { print {$fh} @_ }
1987 sub print_first_filename
{ App
::Ack
::print( $_[0], "\n" ) }
1988 sub print_blank_line
{ App
::Ack
::print( "\n" ) }
1989 sub print_separator
{ App
::Ack
::print( "--\n" ) }
1990 sub print_filename
{ App
::Ack
::print( $_[0], $_[1] ) }
1991 sub print_line_no
{ App
::Ack
::print( $_[0], $_[1] ) }
1992 sub print_column_no
{ App
::Ack
::print( $_[0], $_[1] ) }
1994 my $filename = shift;
1995 my $nmatches = shift;
1998 my $show_filename = shift;
2000 if ($show_filename) {
2001 App
::Ack
::print( $filename );
2002 App
::Ack
::print( ':', $nmatches ) if $count;
2005 App
::Ack
::print( $nmatches ) if $count;
2007 App
::Ack
::print( $ors );
2011 my $filename = shift;
2013 my $show_filename = shift;
2015 if ($show_filename) {
2016 App
::Ack
::print( $filename, ':0', $ors );
2019 App
::Ack
::print( '0', $ors );
2028 my $display_filename;
2032 my $last_output_line; # number of the last line that has been output
2033 my $any_output; # has there been any output for the current file yet
2034 my $context_overall_output_count; # has there been any output at all
2036 sub search_resource
{
2040 $filename = $res->name();
2043 my $passthru = $opt->{passthru
};
2044 my $max = $opt->{m
};
2047 $display_filename = undef;
2049 # for --line processing
2052 if ( defined $opt->{lines
} ) {
2054 @lines = ( @{$opt->{lines
}}, -1 );
2055 undef $regex; # Don't match when printing matching line
2058 $regex = qr/$opt->{regex}/;
2061 # for context processing
2062 $last_output_line = -1;
2064 my $before_context = $opt->{before_context
};
2065 my $after_context = $opt->{after_context
};
2067 $keep_context = ($before_context || $after_context) && !$passthru;
2070 my $before_starts_at_line;
2071 my $after = 0; # number of lines still to print after a match
2073 while ( $res->next_text ) {
2074 # XXX Optimize away the case when there are no more @lines to find.
2075 # XXX $has_lines, $passthru and $v never change. Optimize.
2077 ? $. != $lines[0] # $lines[0] should be a scalar
2078 : $v ? m/$regex/ : !m/$regex/ ) {
2080 App
::Ack
::print( $_ );
2084 if ( $keep_context ) {
2086 print_match_or_context
( $opt, 0, $., $-[0], $+[0], $_ );
2089 elsif ( $before_context ) {
2091 if ( @before >= $before_context ) {
2093 ++$before_starts_at_line;
2097 $before_starts_at_line = $.;
2101 last if $max && ( $nmatches >= $max ) && !$after;
2108 # print an empty line as a divider before first line in each file (not before the first file)
2109 if ( !$any_output && $opt->{show_filename
} && $opt->{break} && defined( $context_overall_output_count ) ) {
2110 App
::Ack
::print_blank_line
();
2113 shift @lines if $has_lines;
2115 if ( $res->is_binary ) {
2116 App
::Ack
::print( "Binary file $filename matches\n" );
2119 if ( $keep_context ) {
2121 print_match_or_context
( $opt, 0, $before_starts_at_line, $-[0], $+[0], @before );
2123 $before_starts_at_line = 0;
2125 if ( $max && $nmatches > $max ) {
2129 $after = $after_context;
2132 print_match_or_context
( $opt, 1, $., $-[0], $+[0], $_ );
2134 last if $max && ( $nmatches >= $max ) && !$after;
2138 } # search_resource()
2142 sub print_match_or_context
{
2143 my $opt = shift; # opts array
2144 my $is_match = shift; # is there a match on the line?
2145 my $line_no = shift;
2146 my $match_start = shift;
2147 my $match_end = shift;
2149 my $color = $opt->{color
};
2150 my $heading = $opt->{heading
};
2151 my $show_filename = $opt->{show_filename
};
2152 my $show_column = $opt->{column
};
2154 if ( $show_filename ) {
2155 if ( not defined $display_filename ) {
2158 ? Term
::ANSIColor
::colored
( $filename, $ENV{ACK_COLOR_FILENAME
} )
2160 if ( $heading && !$any_output ) {
2161 App
::Ack
::print_first_filename
($display_filename);
2166 my $sep = $is_match ? ':' : '-';
2167 my $output_func = $opt->{output
};
2169 if ( $keep_context && !$output_func ) {
2170 if ( ( $last_output_line != $line_no - 1 ) &&
2171 ( $any_output || ( !$heading && defined( $context_overall_output_count ) ) ) ) {
2172 App
::Ack
::print_separator
();
2174 # to ensure separators between different files when --noheading
2176 $last_output_line = $line_no;
2179 if ( $show_filename ) {
2180 App
::Ack
::print_filename
($display_filename, $sep) if not $heading;
2181 my $display_line_no =
2183 ? Term
::ANSIColor
::colored
( $line_no, $ENV{ACK_COLOR_LINENO
} )
2185 App
::Ack
::print_line_no
($display_line_no, $sep);
2188 if ( $output_func ) {
2189 while ( /$regex/go ) {
2190 App
::Ack
::print( $output_func->() . "\n" );
2194 if ( $color && $is_match && $regex &&
2195 s/$regex/Term::ANSIColor::colored( substr($_, $-[0], $+[0] - $-[0]), $ENV{ACK_COLOR_MATCH} )/eg ) {
2196 # At the end of the line reset the color and remove newline
2197 s/[\r\n]*\z/\e[0m\e[K/;
2200 # remove any kind of newline at the end of the line
2203 if ( $show_column ) {
2204 App
::Ack
::print_column_no
( $match_start+1, $sep );
2206 App
::Ack
::print($_ . "\n");
2209 ++$context_overall_output_count;
2214 } # print_match_or_context()
2216 } # scope around search_resource() and print_match_or_context()
2219 TOTAL_COUNT_SCOPE
: {
2222 sub get_total_count
{
2223 return $total_count;
2226 sub reset_total_count
{
2231 sub search_and_list
{
2236 my $count = $opt->{count
};
2237 my $ors = $opt->{print0
} ? "\0" : "\n"; # output record separator
2238 my $show_filename = $opt->{show_filename
};
2240 my $regex = qr/$opt->{regex}/;
2243 while ( $res->next_text ) {
2245 return 0 unless $count;
2253 while ( $res->next_text ) {
2261 if ( $opt->{show_total
} ) {
2262 $total_count += $nmatches;
2266 App
::Ack
::print_count
( $res->name, $nmatches, $ors, $count, $show_filename );
2268 elsif ( $count && !$opt->{l
} ) {
2269 App
::Ack
::print_count0
( $res->name, $ors, $show_filename );
2273 return $nmatches ? 1 : 0;
2274 } # search_and_list()
2276 } # scope around $total_count
2280 sub filetypes_supported_set
{
2281 return grep { defined $type_wanted{$_} && ($type_wanted{$_} == 1) } filetypes_supported
();
2290 my $ors = $opt->{print0
} ? "\0" : "\n";
2293 while ( defined ( my $file = $iter->() ) ) {
2294 App
::Ack
::print $file, $opt->{show_types
} ? " => " . join( ',', filetypes
( $file ) ) : (), $ors;
2303 sub print_files_with_matches
{
2307 # if we have -l and only 1 file given on command line (this means
2308 # show_filename is set to 0), we want to see the filename nevertheless
2309 $opt->{show_filename
} = 1 if $opt->{l
};
2311 $opt->{show_filename
} = 0 if $opt->{h
};
2312 $opt->{show_filename
} = 1 if $opt->{H
};
2314 # abuse options to hand in the show_total parameter to search_and_list
2315 $opt->{show_total
} = $opt->{count
} && !$opt->{show_filename
};
2316 reset_total_count
();
2319 while ( defined ( my $filename = $iter->() ) ) {
2320 my $repo = App
::Ack
::Repository
::Basic-
>new( $filename );
2322 while ( $res = $repo->next_resource() ) {
2323 $nmatches += search_and_list
( $res, $opt );
2325 last if $nmatches && $opt->{1};
2330 if ( $nmatches && $opt->{show_total
} ) {
2331 App
::Ack
::print_count
('', get_total_count
(), "\n", 1, 0 )
2342 $opt->{show_filename
} = 0 if $opt->{h
};
2343 $opt->{show_filename
} = 1 if $opt->{H
};
2346 while ( defined ( my $filename = $iter->() ) ) {
2348 my $tarballs_work = 0;
2349 if ( $tarballs_work && $filename =~ /\.tar\.gz$/ ) {
2350 App
::Ack
::die( 'Not working here yet' );
2351 require App
::Ack
::Repository
::Tar
; # XXX Error checking
2352 $repo = App
::Ack
::Repository
::Tar-
>new( $filename );
2355 $repo = App
::Ack
::Repository
::Basic-
>new( $filename );
2359 while ( my $res = $repo->next_resource() ) {
2360 my $needs_line_scan;
2361 if ( $opt->{regex
} && !$opt->{passthru
} ) {
2362 $needs_line_scan = $res->needs_line_scan( $opt );
2363 if ( $needs_line_scan ) {
2368 $needs_line_scan = 1;
2370 if ( $needs_line_scan ) {
2371 $nmatches += search_resource
( $res, $opt );
2375 last if $nmatches && $opt->{1};
2382 sub filetype_setup
{
2383 my $filetypes_supported_set = filetypes_supported_set
();
2384 # If anyone says --no-whatever, we assume all other types must be on.
2385 if ( !$filetypes_supported_set ) {
2386 for my $i ( keys %type_wanted ) {
2387 $type_wanted{$i} = 1 unless ( defined( $type_wanted{$i} ) || $i eq 'binary' || $i eq 'text' || $i eq 'skipped' );
2394 EXPAND_FILENAMES_SCOPE
: {
2397 sub expand_filenames
{
2403 foreach my $pattern ( @{$argv} ) {
2404 my @results = bsd_glob
( $pattern );
2406 if (@results == 0) {
2407 @results = $pattern; # Glob didn't match, pass it thru unchanged
2409 elsif ( (@results > 1) or ($results[0] ne $pattern) ) {
2410 if (not defined $filter) {
2411 eval 'require Win32::File;';
2416 $filter = Win32
::File
::HIDDEN
()|Win32
::File
::SYSTEM
();
2418 } # end unless we've tried to load Win32::File
2420 # Filter out hidden and system files:
2421 @results = grep { not(Win32
::File
::GetAttributes
($_, $attr) and $attr & $filter) } @results;
2422 App
::Ack
::warn( "$pattern: Matched only hidden files" ) unless @results;
2423 } # end if we can filter by file attributes
2424 } # end elsif this pattern got expanded
2426 push @files, @results;
2427 } # end foreach pattern
2430 } # end expand_filenames
2431 } # EXPAND_FILENAMES_SCOPE
2435 sub get_starting_points
{
2442 @what = @{ $is_windows ? expand_filenames
($argv) : $argv };
2443 $_ = File
::Next
::reslash
( $_ ) for @what;
2445 # Show filenames unless we've specified one single file
2446 $opt->{show_filename
} = (@what > 1) || (!-f
$what[0]);
2449 @what = '.'; # Assume current directory
2450 $opt->{show_filename
} = 1;
2453 for my $start_point (@what) {
2454 App
::Ack
::warn( "$start_point: No such file or directory" ) unless -e
$start_point;
2460 my ( $target, $expression, $invert_flag ) = @_;
2462 if ( $invert_flag ) {
2463 return $target !~ $expression;
2466 return $target =~ $expression;
2475 # Starting points are always searched, no matter what
2476 my %starting_point = map { ($_ => 1) } @{$what};
2478 my $g_regex = defined $opt->{G
} ? qr/$opt->{G}/ : undef;
2483 = $opt->{u
} ? sub { _match
( $File::Next
::name
, qr/$g_regex/, $opt->{invert_file_match
} ) } # XXX Maybe this should be a 1, no?
2484 : $opt->{all
} ? sub { $starting_point{ $File::Next
::name
} || ( _match
( $File::Next
::name
, qr/$g_regex/, $opt->{invert_file_match
} ) && is_searchable
( $_ ) ) }
2485 : sub { $starting_point{ $File::Next
::name
} || ( _match
( $File::Next
::name
, qr/$g_regex/, $opt->{invert_file_match
} ) && is_interesting
( @ _
) ) }
2490 = $opt->{u
} ? sub {1}
2491 : $opt->{all
} ? sub { $starting_point{ $File::Next
::name
} || is_searchable
( $_ ) }
2492 : sub { $starting_point{ $File::Next
::name
} || is_interesting
( @_ ) }
2497 = $opt->{n
} ? sub {0}
2498 : $opt->{u
} ? sub {1}
2499 : \
&ignoredir_filter
;
2502 File
::Next
::files
( {
2503 file_filter
=> $file_filter,
2504 descend_filter
=> $descend_filter,
2505 error_handler
=> sub { my $msg = shift; App
::Ack
::warn( $msg ) },
2506 sort_files
=> $opt->{sort_files
},
2507 follow_symlinks
=> $opt->{follow
},
2514 my $command = shift;
2516 return if App
::Ack
::output_to_pipe
();
2519 if ( not open( $pager, '|-', $command ) ) {
2520 App
::Ack
::die( qq{Unable to pipe to pager "$command": $!} );
2528 sub input_from_pipe
{
2529 return $input_from_pipe;
2534 sub output_to_pipe
{
2535 return $output_to_pipe;
2540 my $nmatches = shift;
2542 my $rc = $nmatches ? 0 : 1;
2548 1; # End of App::Ack
2549 package App
::Ack
::Repository
;
2557 Carp
::confess
( 'Must be overloaded' );
2576 package App
::Ack
::Resource
;
2584 Carp
::confess
( 'Must be overloaded' );
2604 sub needs_line_scan
{
2624 package App
::Ack
::Plugin
::Basic
;
2628 package App
::Ack
::Resource
::Basic
;
2635 our @ISA = qw( App::Ack::Resource );
2640 my $filename = shift;
2643 filename
=> $filename,
2645 could_be_binary
=> undef,
2650 if ( $self->{filename
} eq '-' ) {
2651 $self->{fh
} = *STDIN
;
2652 $self->{could_be_binary
} = 0;
2655 if ( !open( $self->{fh
}, '<', $self->{filename
} ) ) {
2656 App
::Ack
::warn( "$self->{filename}: $!" );
2659 $self->{could_be_binary
} = 1;
2669 return $self->{filename
};
2676 if ( $self->{could_be_binary
} ) {
2677 return -B
$self->{filename
};
2685 sub needs_line_scan
{
2689 return 1 if $opt->{v
};
2691 my $size = -s
$self->{fh
};
2695 elsif ( $size > 100_000 ) {
2700 my $rc = sysread( $self->{fh
}, $buffer, $size );
2701 if ( not defined $rc ) {
2702 App
::Ack
::warn( "$self->{filename}: $!" );
2705 return 0 unless $rc && ( $rc == $size );
2707 my $regex = $opt->{regex
};
2708 return $buffer =~ /$regex/m;
2715 seek( $self->{fh
}, 0, 0 )
2716 or App
::Ack
::warn( "$self->{filename}: $!" );
2723 if ( defined ($_ = readline $_[0]->{fh
}) ) {
2724 $. = ++$_[0]->{line
};
2735 if ( not close $self->{fh
} ) {
2736 App
::Ack
::warn( $self->name() . ": $!" );
2742 package App
::Ack
::Repository
::Basic
;
2745 our @ISA = qw( App::Ack::Repository );
2753 my $filename = shift;
2756 filename
=> $filename,
2767 return if $self->{nexted
};
2768 $self->{nexted
} = 1;
2770 return App
::Ack
::Resource
::Basic-
>new( $self->{filename
} );