1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-07 09:21:18 +02:00

29908: make MIME functions handle stacked suffixes

This commit is contained in:
Peter Stephenson 2011-11-18 09:52:50 +00:00
parent cf4e27a129
commit 7c4a811134
4 changed files with 84 additions and 17 deletions

View file

@ -1,3 +1,10 @@
2011-11-18 Peter Stephenson <pws@csr.com>
* 29908: Doc/Zsh/contrib.yo, Functions/MIME/.distfiles,
Functions/MIME/zsh-mime-contexts, Functions/MIME/zsh-mime-handler:
make MIME functions handle contexts with stacked suffixes such
as .pdf.gz.
2011-11-17 Peter Stephenson <pws@csr.com> 2011-11-17 Peter Stephenson <pws@csr.com>
* Jun T.: 29907: Src/Modules/pcre.c: remove declaration of * Jun T.: 29907: Src/Modules/pcre.c: remove declaration of
@ -15582,5 +15589,5 @@
***************************************************** *****************************************************
* This is used by the shell to define $ZSH_PATCHLEVEL * This is used by the shell to define $ZSH_PATCHLEVEL
* $Revision: 1.5500 $ * $Revision: 1.5501 $
***************************************************** *****************************************************

View file

@ -2803,6 +2803,23 @@ start with tt(:mime:), with additional components in some cases.
It is recommended that a trailing tt(*) (suitably quoted) be appended It is recommended that a trailing tt(*) (suitably quoted) be appended
to style patterns in case the system is extended in future. Some to style patterns in case the system is extended in future. Some
examples are given below. examples are given below.
For files that have multiple suffixes, e.g. tt(.pdf.gz), where the
context includes the suffix it will be looked up starting with the
longest possible suffix until a match for the style is found.
For example, if tt(.pdf.gz) produces a match for the handler, that
will be used; otherwise the handler for tt(.gz) will be used. Note
that, owing to the way suffix aliases work, it is always required that
there be a handler for the shortest possible suffix, so in this example
tt(.pdf.gz) can only be handled if tt(.gz) is also handled (though
not necessarily in the same way). Alternatively, if no handling
for tt(.gz) on its own is needed, simply adding the command
example(alias -s gz=zsh-mime-handler)
to the initialisation code is sufficient; tt(.gz) will not be handled
on its own, but may be in combination with other suffixes.
startitem() startitem()
kindex(current-shell, MIME style) kindex(current-shell, MIME style)
item(tt(current-shell))( item(tt(current-shell))(

View file

@ -1,4 +1,7 @@
DISTFILES_SRC=' DISTFILES_SRC='
.distfiles .distfiles
zsh-mime-setup zsh-mime-handler pick-web-browser pick-web-browser
zsh-mime-contexts
zsh-mime-handler
zsh-mime-setup
' '

View file

@ -34,6 +34,8 @@ setopt extendedglob cbases nullglob $autocd
# We need zformat from zsh/zutil for %s replacement. # We need zformat from zsh/zutil for %s replacement.
zmodload -i zsh/zutil zmodload -i zsh/zutil
autoload -Uz zsh-mime-contexts
# Look for options. Because of the way this is usually invoked, # Look for options. Because of the way this is usually invoked,
# (there is always a command to be handled), only handle options # (there is always a command to be handled), only handle options
# up to second last argument. # up to second last argument.
@ -62,12 +64,15 @@ shift $(( OPTIND - 1 ))
# just as well pass them all down. However, we just take the # just as well pass them all down. However, we just take the
# suffix from the first since that's what invoked us via suffix -s. # suffix from the first since that's what invoked us via suffix -s.
local suffix context local suffix s
local -a match mbegin mend local -a match mbegin mend
[[ $1 = (#b)*.([^.]##) ]] || return 1 suffix=${1:t}
suffix=${(L)match[1]} if [[ $suffix != *.* ]]; then
context=":mime:.${suffix}:" "No suffix in command: $1" >&2
return 1
fi
suffix=${suffix#*.}
local handler flags no_sh no_bg arg local handler flags no_sh no_bg arg
integer i integer i
@ -77,11 +82,11 @@ local -a exec_asis hand_nonex
# despite being called for interpretation by the mime handler. # despite being called for interpretation by the mime handler.
# Defaults to executable files, which ensures that they are executed as # Defaults to executable files, which ensures that they are executed as
# they are, even if they have a suffix. # they are, even if they have a suffix.
zstyle -a $context execute-as-is exec_asis || exec_asis=('*(*)' '*(/)') zsh-mime-contexts -a $suffix execute-as-is exec_asis || exec_asis=('*(*)' '*(/)')
# Set to a list of patterns for which the handler will be used even # Set to a list of patterns for which the handler will be used even
# if the file doesn't exist on the disk. # if the file doesn't exist on the disk.
zstyle -a $context handle-nonexistent hand_nonex || zsh-mime-contexts -a $suffix handle-nonexistent hand_nonex ||
hand_nonex=('[[:alpha:]]#:/*') hand_nonex=('[[:alpha:]]#:/*')
local pattern local pattern
@ -92,9 +97,9 @@ local -a files
# actual file or its directory. # actual file or its directory.
local dir local dir
local -a filepath local -a filepath
if zstyle -t $context find-file-in-path && [[ $1 != /* ]] && if zsh-mime-contexts -t $suffix find-file-in-path && [[ $1 != /* ]] &&
[[ $1 != */* || -o pathdirs ]]; then [[ $1 != */* || -o pathdirs ]]; then
zstyle -a $context file-path filepath || filepath=($path) zsh-mime-contexts -a $suffix file-path filepath || filepath=($path)
for dir in $filepath; do for dir in $filepath; do
if [[ -e $dir/$1 ]]; then if [[ -e $dir/$1 ]]; then
1=$dir/$1 1=$dir/$1
@ -153,19 +158,54 @@ if [[ ! -e $1 ]]; then
fi fi
fi fi
zstyle -s $context handler handler || if ! zsh-mime-contexts -s $suffix handler handler; then
handler="${zsh_mime_handlers[$suffix]}" # Look for handler starting with longest suffix match.
zstyle -s $context flags flags || # Typically we'd only get a match for the shortest, but don't assume so.
flags="${zsh_mime_flags[$suffix]}" s=$suffix
while true; do
handler="${zsh_mime_handlers[$s]}"
if [[ -n $handler ]]; then
break
fi
if [[ $s = *.* ]]; then
s=${s#*.}
else
break
fi
done
if [[ -z $handler ]]; then
if [[ $suffix = *.* ]]; then
print "No handler specified for suffix .$suffix or any final part" >&2
else
print "No handler specified for suffix .$suffix" >&2
fi
return 1
fi
fi
if ! zsh-mime-contexts -s $suffix flags flags; then
# Same again for flags.
s=$suffix
while true; do
flags="${zsh_mime_flags[$suffix]}"
if [[ -n $flags ]]; then
break
fi
if [[ $s = *.* ]]; then
s=${s#*.}
else
break
fi
done
fi
# Set to yes if we use eval instead of sh -c for complicated mailcap lines # Set to yes if we use eval instead of sh -c for complicated mailcap lines
# Can possibly break some mailcap entries which expect sh compatibility, # Can possibly break some mailcap entries which expect sh compatibility,
# but is faster, as a new process is not spawned. # but is faster, as a new process is not spawned.
zstyle -t $context current-shell && no_sh=yes zsh-mime-contexts -t $suffix current-shell && no_sh=yes
# Set to yes if the process shouldn't be backgrounded even if it doesn't need a # Set to yes if the process shouldn't be backgrounded even if it doesn't need a
# terminal and display is set. # terminal and display is set.
zstyle -t $context never-background && no_bg=yes zsh-mime-contexts -t $suffix never-background && no_bg=yes
local hasmeta stdin local hasmeta stdin
@ -241,7 +281,7 @@ if [[ $flags = *copiousoutput* ]]; then
# We need to page the output. # We need to page the output.
# Careful in case PAGER is a set of commands and arguments. # Careful in case PAGER is a set of commands and arguments.
local -a pager local -a pager
zstyle -a $context pager pager || pager=(${=PAGER:-more}) zsh-mime-contexts -a $suffix pager pager || pager=(${=PAGER:-more})
if [[ -n $stdin ]]; then if [[ -n $stdin ]]; then
cat $argv | $execargs | $pager cat $argv | $execargs | $pager
else else