2006-02-08 17:20:05 +08:00
|
|
|
#
|
|
|
|
# Find files that complete $argv[1], has the suffix $argv[2], and
|
2015-07-23 14:22:27 +08:00
|
|
|
# output them as completions with the optional description $argv[3] Both
|
2007-01-30 23:26:31 +08:00
|
|
|
# $argv[1] and $argv[3] are optional, if only one is specified, it is
|
2018-11-08 09:02:17 +08:00
|
|
|
# assumed to be the argument to complete. If $argv[4] is present, it is
|
|
|
|
# treated as a prefix for the path, i.e. in lieu of $PWD.
|
2006-02-08 17:20:05 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
function __fish_complete_suffix -d "Complete using files"
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# Variable declarations
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
set -l comp
|
|
|
|
set -l suff
|
|
|
|
set -l desc
|
|
|
|
set -l files
|
2018-11-09 06:17:47 +08:00
|
|
|
set -l prefix ""
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
switch (count $argv)
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
case 1
|
|
|
|
set comp (commandline -ct)
|
|
|
|
set suff $argv
|
2019-01-22 14:55:56 +08:00
|
|
|
set desc ""
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
case 2
|
|
|
|
set comp $argv[1]
|
|
|
|
set suff $argv[2]
|
2019-01-22 14:55:56 +08:00
|
|
|
set desc ""
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
case 3
|
|
|
|
set comp $argv[1]
|
|
|
|
set suff $argv[2]
|
|
|
|
set desc $argv[3]
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2018-11-08 09:02:17 +08:00
|
|
|
case 4
|
|
|
|
set comp $argv[1]
|
|
|
|
set suff $argv[2]
|
|
|
|
set desc $argv[3]
|
|
|
|
set prefix $argv[4]
|
|
|
|
|
|
|
|
# Only directories are supported as prefixes, and to use the same logic
|
|
|
|
# for both absolute prefixed paths and relative non-prefixed paths, $prefix
|
|
|
|
# must terminate in a `/` if it is present, so it can be unconditionally
|
|
|
|
# prefixed to any path to get the desired result.
|
|
|
|
if not string match -qr '/$' $prefix
|
|
|
|
set prefix $prefix/
|
|
|
|
end
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2007-01-30 23:26:31 +08:00
|
|
|
|
2018-04-30 00:17:35 +08:00
|
|
|
# Strip leading ./ as it confuses the detection of base and suffix
|
|
|
|
# It is conditionally re-added below.
|
2018-11-08 09:02:17 +08:00
|
|
|
set base $prefix(string replace -r '^("\')?\\./' '' -- $comp | string trim -c '\'"') # " make emacs syntax highlighting happy
|
2018-04-30 00:17:35 +08:00
|
|
|
|
2018-06-19 10:32:05 +08:00
|
|
|
set -l all
|
|
|
|
set -l dirs
|
2018-04-30 00:17:35 +08:00
|
|
|
# If $comp is "./ma" and the file is "main.py", we'll catch that case here,
|
|
|
|
# but complete.cpp will not consider it a match, so we have to output the
|
|
|
|
# correct form.
|
2018-06-19 10:32:05 +08:00
|
|
|
|
|
|
|
# Also do directory completion, since there might be files with the correct
|
2019-12-29 00:02:31 +08:00
|
|
|
# suffix in a subdirectory.
|
|
|
|
set all $base*$suff
|
2018-06-19 10:32:05 +08:00
|
|
|
if not string match -qr '/$' -- $suff
|
2019-12-29 00:02:31 +08:00
|
|
|
set dirs $base*/
|
2018-06-19 10:32:05 +08:00
|
|
|
|
|
|
|
# The problem is that we now have each directory included twice in the output,
|
|
|
|
# once as `dir` and once as `dir/`. The runtime here is O(n) for n directories
|
|
|
|
# in the output, but hopefully since we have only one level (no nested results)
|
|
|
|
# it should be fast. The alternative is to shell out to `sort` and remove any
|
|
|
|
# duplicate results, but it would have to be a huge `n` to make up for the fork
|
|
|
|
# overhead.
|
|
|
|
for dir in $dirs
|
|
|
|
set all (string match -v (string match -r '(.*)/$' -- $dir)[2] -- $all)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-31 03:31:45 +08:00
|
|
|
set files $all $dirs
|
2018-05-21 01:30:07 +08:00
|
|
|
if string match -qr '^\\./' -- $comp
|
2018-06-19 10:32:05 +08:00
|
|
|
set files ./$files
|
2019-12-19 03:13:23 +08:00
|
|
|
else
|
|
|
|
# "Escape" files starting with a literal dash `-` with a `./`
|
|
|
|
set files (string replace -r -- "^-" "./-" $files)
|
2018-05-20 13:38:17 +08:00
|
|
|
end
|
|
|
|
|
2018-06-19 10:32:05 +08:00
|
|
|
if set -q files[1]
|
2018-11-08 09:02:17 +08:00
|
|
|
if string match -qr -- . "$desc"
|
2019-05-05 18:09:25 +08:00
|
|
|
set desc "\t$desc"
|
2018-11-08 09:02:17 +08:00
|
|
|
end
|
|
|
|
if string match -qr -- . "$prefix"
|
|
|
|
# Ideally, only replace in the beginning of the string, but we have no
|
|
|
|
# way of doing a pcre2 escape so we can use a regex replace instead
|
|
|
|
set files (string replace $prefix "" $files)
|
2018-05-21 03:35:51 +08:00
|
|
|
end
|
2019-12-29 00:02:31 +08:00
|
|
|
printf "%s$desc\n" $files
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
|
|
|
|
end
|