fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
b980e50ac
master
1# localization: skip(private) 2# 3# Find files ending in any of the non-switch arguments and output them as completions. 4# * --description provides the description that should be part of each generated completion, 5# * --prefix=DIR makes __fish_complete_suffix behave as if it were called in DIR rather than $PWD 6# * --complete=PREFIX only lists files that begin with PREFIX 7# 8# Files matching the above preconditions are printed first then other, non-matching files 9# are printed for fallback purposes. As such, it is imperative that `complete` calls that 10# shell out to `__fish_complete_suffix` are made with a `-k` switch to ensure sort order 11# is preserved. 12function __fish_complete_suffix -d"Complete using files" 13set -l _flag_prefix"" 14# We need to remove '--name=' from the completion. See __fish_complete_directories 15set -l _flag_complete( commandline -ct| string replace -r --'^-[^=]*=' '' ) 16 17argparse 'prefix=' 'description=' 'complete=' --$argv 18 19set -l suff( string escape --style=regex --$argv ) 20set -l files 21 22# Simple and common case: no prefix, just complete normally and sort matching files first. 23if test -z $_flag_prefix 24# Use normal file completions. 25set files( complete -C"__fish_command_without_completions $_flag_complete " ) 26set -l files_with_suffix( string match -r --( string join"|" "^.*" $suff \$ ) $files ) 27set -l directories( string match -r --'^.*/$' $files ) 28set files$files_with_suffix $directories $files 29else 30# Only directories are supported as prefixes, and to use the same logic 31# for both absolute prefixed paths and relative non-prefixed paths, $_flag_prefix 32# must terminate in a `/` if it is present, so it can be unconditionally 33# prefixed to any path to get the desired result. 34if not string match -qr'/$' $_flag_prefix 35set _flag_prefix$_flag_prefix / 36end 37 38# Strip leading ./ as it confuses the detection of base and suffix 39# It is conditionally re-added below. 40set -l base$_flag_prefix ( string replace -r'^("\')?\\./' '' --$_flag_complete | string trim -c'\'"' ) # " make emacs syntax highlighting happy 41 42set -l all 43set -l files_with_suffix 44set -l dirs 45# If $_flag_complete is "./ma" and the file is "main.py", we'll catch that case here, 46# but complete.cpp will not consider it a match, so we have to output the 47# correct form. 48 49# Also do directory completion, since there might be files with the correct 50# suffix in a subdirectory. 51set all$base * 52set files_with_suffix( string match -r --( string join"|" ".*" $suff ) $all ) 53if not string match -qr'/$' --$argv 54set dirs$base */ 55 56# The problem is that we now have each directory included twice in the output, 57# once as `dir` and once as `dir/`. The runtime here is O(n) for n directories 58# in the output, but hopefully since we have only one level (no nested results) 59# it should be fast. The alternative is to shell out to `sort` and remove any 60# duplicate results, but it would have to be a huge `n` to make up for the fork 61# overhead. 62for dirin $dirs 63set all( string match -v( string match -r'(.*)/$' --$dir )[ 2 ] --$all ) 64end 65end 66 67set files$files_with_suffix $dirs $all 68if string match -qr'^\\./' --$_flag_complete 69set files ./$files 70else 71# "Escape" files starting with a literal dash `-` with a `./` 72set files( string replace -r --"^-" "./-" $files ) 73end 74end 75 76if set -q files[ 1 ] 77if string match -qr -- ." $_flag_description " 78set _flag_description"\t $_flag_description " 79end 80if string match -qr -- ." $_flag_prefix " 81set -l prefix( string escape --style=regex --$_flag_prefix ) 82set files( string replace -r --"^ $prefix " "" $files ) 83end 84printf "%s $_flag_description \n" $files 85end 86 87end