Michael Siegel
2018-10-04 08:46:38 UTC
Hello,
I'm out of ideas on how to solve a problem in an auto-completion script
I've written. The problem is with the behavior of 'complete -o
filenames'.
Here's the script:
# dbm_completion_bash
#
# Auto-completion for dbm in Bash
_dbm_complete_bash() {
local IFS=$'\n'
local dbm_dir="$HOME/.dbm"
# Only perform bookmark name completion on the first argument to dbm,
# except when that first argument is '-d' (the delete switch). In that
# case, perform bookmark name completion on any given argument (in
# effect: on any argument supplied to '-d').
if [ "${#COMP_WORDS[@]}" -lt 3 ] || [ "${COMP_WORDS[1]}" = '-d' ]
then
# Complete the current word, based on file names in $dbm_dir, and
# cut the directory prefix.
COMPREPLY=($(compgen -f -- "$dbm_dir/${COMP_WORDS[COMP_CWORD]}" | \
cut -d '/' -f 5))
fi
}
complete -o filenames -F _dbm_complete_bash dbm
# -o filenames: Perform escaping of shell special characters.
To give some background: dbm is a simple directory bookmarking system
for the shell, implemented as a set of shell functions that are sourced
in ~/.bashrc. The completion script given above is sourced in the file
containing the dbm function set. Bookmarks are simply symbolic links
inside ~/.dbm.
Running dbm without arguments will simply display the existing bookmarks
and their destinations in the file system:
~$ dbm
dbm -> /home/msi/devel/shell/dbm
msiism -> /home/msi/devel/www/msiism
sand box -> /home/msi/devel/sandbox
To jump to a bookmarked location, you'd invoke dbm with the respective
bookmark name as an argument. Having that argument completed when you
hit <Tab> generally works, except for one particular situation. And that
is when there is a directory of the same name as the completed bookmark
name in the current working directory.
In that case, the shell will add a slash to the name, as if it were
completing directory names within $PWD:
~/devel/sandbox/probe$ ls
a_file msiism/ tests/
~/devel/sandbox/probe$ dbm msi<Tab> # Results in 'dbm msiism/'
~/devel/sandbox/probe$
Now, the Bash Reference Manual does say that '-o filename' will "perform
any filename-specific processing"[1], "like adding a slash to directory
names"[1] and such. What I don't understand is why it is still adding
the slash when completion is based on file names in $dbm_dir instead of
$PWD.
Any ideas?
Thanks,
msi
[1]
https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html
I'm out of ideas on how to solve a problem in an auto-completion script
I've written. The problem is with the behavior of 'complete -o
filenames'.
Here's the script:
# dbm_completion_bash
#
# Auto-completion for dbm in Bash
_dbm_complete_bash() {
local IFS=$'\n'
local dbm_dir="$HOME/.dbm"
# Only perform bookmark name completion on the first argument to dbm,
# except when that first argument is '-d' (the delete switch). In that
# case, perform bookmark name completion on any given argument (in
# effect: on any argument supplied to '-d').
if [ "${#COMP_WORDS[@]}" -lt 3 ] || [ "${COMP_WORDS[1]}" = '-d' ]
then
# Complete the current word, based on file names in $dbm_dir, and
# cut the directory prefix.
COMPREPLY=($(compgen -f -- "$dbm_dir/${COMP_WORDS[COMP_CWORD]}" | \
cut -d '/' -f 5))
fi
}
complete -o filenames -F _dbm_complete_bash dbm
# -o filenames: Perform escaping of shell special characters.
To give some background: dbm is a simple directory bookmarking system
for the shell, implemented as a set of shell functions that are sourced
in ~/.bashrc. The completion script given above is sourced in the file
containing the dbm function set. Bookmarks are simply symbolic links
inside ~/.dbm.
Running dbm without arguments will simply display the existing bookmarks
and their destinations in the file system:
~$ dbm
dbm -> /home/msi/devel/shell/dbm
msiism -> /home/msi/devel/www/msiism
sand box -> /home/msi/devel/sandbox
To jump to a bookmarked location, you'd invoke dbm with the respective
bookmark name as an argument. Having that argument completed when you
hit <Tab> generally works, except for one particular situation. And that
is when there is a directory of the same name as the completed bookmark
name in the current working directory.
In that case, the shell will add a slash to the name, as if it were
completing directory names within $PWD:
~/devel/sandbox/probe$ ls
a_file msiism/ tests/
~/devel/sandbox/probe$ dbm msi<Tab> # Results in 'dbm msiism/'
~/devel/sandbox/probe$
Now, the Bash Reference Manual does say that '-o filename' will "perform
any filename-specific processing"[1], "like adding a slash to directory
names"[1] and such. What I don't understand is why it is still adding
the slash when completion is based on file names in $dbm_dir instead of
$PWD.
Any ideas?
Thanks,
msi
[1]
https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html