diff --git a/share/completions/make.fish b/share/completions/make.fish index b07f9e511..4170948c2 100644 --- a/share/completions/make.fish +++ b/share/completions/make.fish @@ -1,11 +1,14 @@ # Completions for make function __fish_complete_make_targets - set directory (string replace -r '^make .*(-C ?|--directory[= ]?)([^ ]*) .*$' '$2' -- $argv) - if test $status -eq 0 -a -d $directory - __fish_print_make_targets $directory - else - __fish_print_make_targets + set directory (string replace -r '^make .*(-C ?|--directory(=| +))([^ ]*) .*$' '$3' -- $argv) + if not test $status -eq 0 -a -d $directory + set directory '' end + set file (string replace -r '^make .*(-f ?|--file(=| +))([^ ]*) .*$' '$3' -- $argv) + if not test $status -eq 0 -a -f $file + set file '' + end + __fish_print_make_targets "$directory" "$file" end # This completion reenables file completion on diff --git a/share/functions/__fish_print_make_targets.fish b/share/functions/__fish_print_make_targets.fish index c5640324f..c38e1b130 100644 --- a/share/functions/__fish_print_make_targets.fish +++ b/share/functions/__fish_print_make_targets.fish @@ -1,4 +1,4 @@ -function __fish_print_make_targets --argument directory +function __fish_print_make_targets --argument-names directory file # Since we filter based on localized text, we need to ensure the # text will be using the correct locale. set -lx LC_ALL C @@ -7,6 +7,15 @@ function __fish_print_make_targets --argument directory set directory '.' end + if test -z "$file" + for standard_file in $directory/{GNUmakefile,Makefile,makefile} + if test -f $standard_file + set file $standard_file + break + end + end + end + set -l bsd_make if make -C $directory -pn >/dev/null ^/dev/null set bsd_make 0 @@ -14,15 +23,11 @@ function __fish_print_make_targets --argument directory set bsd_make 1 end - for file in $directory/{GNUmakefile,Makefile,makefile} - if test -f $file - if test "$bsd_make" = 0 - make -C $directory -prRn | awk -v RS= -F: '/^# Files/,/^# Finished Make data base/ {if ($1 !~ "^[#.]") {print $1}}' ^/dev/null - else - make -C $directory -d g1 -rn >/dev/null ^| awk -F, '/^#\*\*\* Input graph:/,/^$/ {if ($1 !~ "^#... ") {gsub(/# /,"",$1); print $1}}' ^/dev/null - end - break - end + if test "$bsd_make" = 0 + # https://stackoverflow.com/a/26339924 + make -C "$directory" -f "$file" -pRrq : ^/dev/null | awk -v RS= -F: '/^# Files/,/^# Finished Make data base/ {if ($1 !~ "^[#.]") {print $1}}' ^/dev/null + else + make -C "$directory" -f "$file" -d g1 -rn >/dev/null ^| awk -F, '/^#\*\*\* Input graph:/,/^$/ {if ($1 !~ "^#... ") {gsub(/# /,"",$1); print $1}}' ^/dev/null end end