mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-04 10:41:11 +02:00
20076: improved function using always
This commit is contained in:
parent
d591334e9d
commit
08bd15e282
1 changed files with 94 additions and 93 deletions
|
@ -62,9 +62,8 @@ zstyle -a :mime: mime-types type_files ||
|
||||||
zstyle -a :mime: mailcap cap_files ||
|
zstyle -a :mime: mailcap cap_files ||
|
||||||
cap_files=(~/.mailcap /etc/mailcap)
|
cap_files=(~/.mailcap /etc/mailcap)
|
||||||
|
|
||||||
TRAPEXIT() { unfunction mime-setup-add-type >&/dev/null; return 0; }
|
{
|
||||||
|
mime-setup-add-type() {
|
||||||
mime-setup-add-type() {
|
|
||||||
local type suffix
|
local type suffix
|
||||||
local -a array
|
local -a array
|
||||||
|
|
||||||
|
@ -90,115 +89,117 @@ mime-setup-add-type() {
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Loop through files to find suffixes for MIME types.
|
# Loop through files to find suffixes for MIME types.
|
||||||
# Earlier entries take precedence, so the files need to be listed
|
# Earlier entries take precedence, so the files need to be listed
|
||||||
# with the user's own first. This also means pre-existing
|
# with the user's own first. This also means pre-existing
|
||||||
# values in suffix_type_map are respected.
|
# values in suffix_type_map are respected.
|
||||||
for file in $type_files; do
|
for file in $type_files; do
|
||||||
[[ -r $file ]] || continue
|
[[ -r $file ]] || continue
|
||||||
|
|
||||||
# For once we rely on the fact that read handles continuation
|
# For once we rely on the fact that read handles continuation
|
||||||
# lines ending in backslashes, i.e. there's no -r.
|
# lines ending in backslashes, i.e. there's no -r.
|
||||||
while read line; do
|
while read line; do
|
||||||
# Skip blank or comment lines.
|
# Skip blank or comment lines.
|
||||||
[[ $line = [[:space:]]#(\#*|) ]] && continue
|
[[ $line = [[:space:]]#(\#*|) ]] && continue
|
||||||
|
|
||||||
# There are two types of line you find in MIME type files.
|
# There are two types of line you find in MIME type files.
|
||||||
# The original simple sort contains the type name then suffixes
|
# The original simple sort contains the type name then suffixes
|
||||||
# separated by whitespace. However, Netscape insists
|
# separated by whitespace. However, Netscape insists
|
||||||
# on adding lines with backslash continuation with
|
# on adding lines with backslash continuation with
|
||||||
# key="value" pairs. So we'd better handle both.
|
# key="value" pairs. So we'd better handle both.
|
||||||
if [[ $line = *=* ]]; then
|
if [[ $line = *=* ]]; then
|
||||||
# Gory.
|
# Gory.
|
||||||
# This relies on the fact that a typical entry:
|
# This relies on the fact that a typical entry:
|
||||||
# type=video/x-mpeg2 desc="MPEG2 Video" exts="mpv2,mp2v"
|
# type=video/x-mpeg2 desc="MPEG2 Video" exts="mpv2,mp2v"
|
||||||
# looks like a parameter assignment. However, we really
|
# looks like a parameter assignment. However, we really
|
||||||
# don't want to be screwed up by future extensions,
|
# don't want to be screwed up by future extensions,
|
||||||
# so we split the elements to an array and pick out the
|
# so we split the elements to an array and pick out the
|
||||||
# ones we're interested in.
|
# ones we're interested in.
|
||||||
type= exts=
|
type= exts=
|
||||||
|
|
||||||
# Syntactically split line to preserve quoted words.
|
# Syntactically split line to preserve quoted words.
|
||||||
array=(${(z)line})
|
array=(${(z)line})
|
||||||
for elt in $array; do
|
for elt in $array; do
|
||||||
if [[ $elt = (type|exts)=* ]]; then
|
if [[ $elt = (type|exts)=* ]]; then
|
||||||
eval $elt
|
eval $elt
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# Get extensions by splitting on comma
|
# Get extensions by splitting on comma
|
||||||
array=(${(s.,.)exts})
|
array=(${(s.,.)exts})
|
||||||
|
|
||||||
[[ -n $type ]] && mime-setup-add-type $type $array
|
[[ -n $type ]] && mime-setup-add-type $type $array
|
||||||
else
|
else
|
||||||
# Simple.
|
# Simple.
|
||||||
mime-setup-add-type ${=line}
|
mime-setup-add-type ${=line}
|
||||||
fi
|
fi
|
||||||
done <$file
|
done <$file
|
||||||
done
|
done
|
||||||
|
} always {
|
||||||
|
unfunction mime-setup-add-type >&/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
# Loop through files to find handlers for types.
|
# Loop through files to find handlers for types.
|
||||||
for file in $cap_files; do
|
for file in $cap_files; do
|
||||||
[[ -r $file ]] || continue
|
[[ -r $file ]] || continue
|
||||||
|
|
||||||
# Oh, great. We need to preserve backslashes inside the line,
|
# Oh, great. We need to preserve backslashes inside the line,
|
||||||
# but need to manage continuation lines.
|
# but need to manage continuation lines.
|
||||||
while read -r line; do
|
while read -r line; do
|
||||||
# Skip blank or comment lines.
|
# Skip blank or comment lines.
|
||||||
[[ $line = [[:space:]]#(\#*|) ]] && continue
|
[[ $line = [[:space:]]#(\#*|) ]] && continue
|
||||||
|
|
||||||
while [[ $line = (#b)(*)\\ ]]; do
|
while [[ $line = (#b)(*)\\ ]]; do
|
||||||
line=$match[1]
|
line=$match[1]
|
||||||
read -r line2 || break
|
read -r line2 || break
|
||||||
line+=$line2
|
line+=$line2
|
||||||
done
|
done
|
||||||
|
|
||||||
# Guess what, this file has a completely different format.
|
# Guess what, this file has a completely different format.
|
||||||
# See mailcap(4).
|
# See mailcap(4).
|
||||||
# The biggest unpleasantness here is that the fields are
|
# The biggest unpleasantness here is that the fields are
|
||||||
# delimited by semicolons, but the command field, which
|
# delimited by semicolons, but the command field, which
|
||||||
# is the one we want to extract, may itself contain backslashed
|
# is the one we want to extract, may itself contain backslashed
|
||||||
# semicolons.
|
# semicolons.
|
||||||
if [[ $line = (#b)[[:space:]]#([^[:space:]\;]##)[[:space:]]#\;(*) ]]
|
if [[ $line = (#b)[[:space:]]#([^[:space:]\;]##)[[:space:]]#\;(*) ]]
|
||||||
then
|
then
|
||||||
# this is the only form we can handle, but there's no point
|
# this is the only form we can handle, but there's no point
|
||||||
# issuing a warning for other forms.
|
# issuing a warning for other forms.
|
||||||
type=$match[1]
|
type=$match[1]
|
||||||
line=$match[2]
|
line=$match[2]
|
||||||
# See if it has flags after the command.
|
# See if it has flags after the command.
|
||||||
if [[ $line = (#b)(([^\;\\]|\\\;|\\[^\;])#)\;(*) ]]; then
|
if [[ $line = (#b)(([^\;\\]|\\\;|\\[^\;])#)\;(*) ]]; then
|
||||||
line=$match[1]
|
line=$match[1]
|
||||||
flags=$match[3]
|
flags=$match[3]
|
||||||
else
|
else
|
||||||
flags=
|
flags=
|
||||||
fi
|
fi
|
||||||
# Remove quotes from semicolons
|
# Remove quotes from semicolons
|
||||||
line=${line//\\\;/\;}
|
line=${line//\\\;/\;}
|
||||||
# and remove any surrounding white space --- this might
|
# and remove any surrounding white space --- this might
|
||||||
# make the handler empty.
|
# make the handler empty.
|
||||||
line=${${line##[[:space:]]#}%%[[:space:]]}
|
line=${${line##[[:space:]]#}%%[[:space:]]}
|
||||||
if [[ -z $type_handler_map[$type] ]]; then
|
if [[ -z $type_handler_map[$type] ]]; then
|
||||||
if [[ -n $o_verbose ]]; then
|
if [[ -n $o_verbose ]]; then
|
||||||
print -r "Adding handler for type $type:
|
print -r "Adding handler for type $type:
|
||||||
$line" >&2
|
$line" >&2
|
||||||
fi
|
|
||||||
type_handler_map[$type]=$line
|
|
||||||
type_flags_map[$type]=$flags
|
|
||||||
if [[ -n $flags && -n $o_verbose ]]; then
|
|
||||||
print -r " with flags $flags" >&2
|
|
||||||
fi
|
|
||||||
elif [[ -n $o_verbose ]]; then
|
|
||||||
print -r "Skipping handler for already defined type $type:
|
|
||||||
$line" >&2
|
|
||||||
if [[ -n $flags ]]; then
|
|
||||||
print -r " with flags $flags" >&2
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
done <$file
|
type_handler_map[$type]=$line
|
||||||
|
type_flags_map[$type]=$flags
|
||||||
|
if [[ -n $flags && -n $o_verbose ]]; then
|
||||||
|
print -r " with flags $flags" >&2
|
||||||
|
fi
|
||||||
|
elif [[ -n $o_verbose ]]; then
|
||||||
|
print -r "Skipping handler for already defined type $type:
|
||||||
|
$line" >&2
|
||||||
|
if [[ -n $flags ]]; then
|
||||||
|
print -r " with flags $flags" >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done <$file
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue