From 1dcdb21db618055134cd611f4f5918f6d00a5df0 Mon Sep 17 00:00:00 2001 From: Edwin Steiner Date: Fri, 9 Jun 2023 09:46:50 +0200 Subject: [PATCH] [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 --- autoload/fzf/vim.vim | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 05b8586..d0248b5 100755 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -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