[Helptags] Remove dependencies of grep and sort (#1482)

* Remove dependency of fzf#vim#helptags on grep.

The grep command was used here only to dump file contents prefixed
with the filename and a colon. Since the function generates a temporary
Perl script anyway, it is simpler to do this directly in the Perl
script. This removes the dependency on grep and makes it easier to get
:Helptags to work on Windows.

A subsequent commit will also move the sorting of the helptags into the
Perl script. I did not do it in this commit, since the OS's "sort"
command can give a different sort order than Perl's sort function,
so I'm not sure what is preferred here.

Note: 'use autodie' is recommended over 'use Fatal' but the autodie
module has only been in Perl core since v5.10.1 (2009-08-22) while
Fatal is in core since 1996. The three-argument open requires v5.6
(2000).

* Remove dependency of fzf#vim#helptags on external sort command.

This moves the sorting of helptags into the generated Perl script and
thereby removes the dependency on an external "sort" command.

Note that Perl's sort function used here may give a different sort
order than the OS's sort command. (It does so for me on Windows but
I actually like Perl's ASCII sort order better, anyway.)

---------

Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
This commit is contained in:
Edwin Steiner
2023-06-09 09:46:50 +02:00
committed by GitHub
parent 5d87ac1fe8
commit 1dcdb21db6

View File

@@ -1223,8 +1223,8 @@ function! s:helptag_sink(line)
endfunction
function! fzf#vim#helptags(...)
if !executable('grep') || !executable('perl')
return s:warn('Helptags command requires grep and perl')
if !executable('perl')
return s:warn('Helptags command requires perl')
endif
let sorted = sort(split(globpath(&runtimepath, 'doc/tags', 1), '\n'))
let tags = exists('*uniq') ? uniq(sorted) : fzf#vim#_uniq(sorted)
@@ -1234,10 +1234,9 @@ function! fzf#vim#helptags(...)
endif
let s:helptags_script = tempname()
call writefile(['/('.(s:is_win ? '^[A-Z]:[\/\\].*?[^:]' : '.*?').'):(.*?)\t(.*?)\t(.*)/; printf(qq('.s:green('%-40s', 'Label').'\t%s\t%s\t%s\n), $2, $3, $1, $4)'], s:helptags_script)
call writefile(['use Fatal qw(open close); for my $filename (@ARGV) { open(my $file,q(<),$filename); while (<$file>) { /(.*?)\t(.*?)\t(.*)/; push @lines, sprintf(qq('.s:green('%-40s', 'Label').'\t%s\t%s\t%s\n), $1, $2, $filename, $3); } close($file); } print for sort @lines;'], s:helptags_script)
return s:fzf('helptags', {
\ 'source': 'grep --with-filename ".*" '.join(map(tags, 'fzf#shellescape(v:val)')).
\ ' | perl -n '.fzf#shellescape(s:helptags_script).' | sort',
\ 'source': 'perl '.fzf#shellescape(s:helptags_script).' '.join(map(tags, 'fzf#shellescape(v:val)')),
\ 'sink': s:function('s:helptag_sink'),
\ 'options': ['--ansi', '+m', '--tiebreak=begin', '--with-nth', '..3']}, a:000)
endfunction