Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: False alarm (nonomatch and cshnullglob)



On Jul 23,  7:35pm, Zoltan Hidvegi wrote:
} Subject: Re: False alarm (nonomatch and cshnullglob)
}
} > Someone previously suggested that options should be heirarchical, or
} > somehow otherwise related, so that (e.g.) "setopt nonomatch" would
} > automatically imply "unsetopt nullglob cshnullglob".
} 
} Perhaps that would be nice but I'm not sure it worth the extra code it adds.

Appended is an implementation using zsh functions to wrap the builtin
setopt/unsetopt commands.

} It would not be difficult to add the setopt option=value syntax.  Options
} are stored in a char array so this can be done without changing the array.
} But I still do not see how can such a feature be used.

The idea would be that you'd combine options like globdots, shglob,
ignorebraces, extendedglob, noglob, etc. into a single option (e.g.)
globstyle, and then you'd

    setopt globstyle=(globdots shglob ignorebraces)

You could also have options with multiple mutually-exclusive values,
e.g.

    setopt nomatchstyle=(csh)	# Possible values are csh, null, none

Since you now can't (e.g.) set one glob action without also setting the
rest of globstyle, you *know* that you aren't trying to use extendedglob
when noglob is set, and so forth.

----- 8< ----- snip ----- 8< ----- snip -----
#! /usr/local/bin/zsh
# Wrapper for setopt/unsetopt
# Put this in a file named "setopt" directory in your FPATH,
# link the name "unsetopt" to that file as well, and autoload
# both setopt and unsetopt.  Do this before setting any options.

local kshop ksharray localop monitor zle name value

[[ -o ksharrays ]] && ksharray=on || ksharray=off
[[ -o kshoptionprint ]] && kshop=on || kshop=off
[[ -o localoptions ]] && localop=on || localop=off
[[ -o monitor ]] && monitor=on || monitor=off
[[ -o zle ]] && zle=on || zle=off

builtin setopt localoptions kshoptionprint
builtin unsetopt ksharrays

options=()
typeset -xU options

builtin setopt | while read name value
do
    case $name in
    Current)		continue;;
    ksharrays)		value=$ksharray;;
    kshoptionprint)	value=$kshop;;
    localoptions)	value=$localop;;
    monitor)		value=$monitor;;
    zle)		value=$zle;;
    esac

    options=($options $name)

    # This array stores the default settings, just in case ...
    # I haven't come up with a use for this yet, except maybe
    # to get at the parent's value of "monitor" and "zle" even
    # in subshells.
    settings[$options[(i)$name]]=$value
done

# There is no case where an option A should always be on when
# another option B is off.  However:
#
# on_opts	option A should be on when B is on
# off_opts	option A should be off when B is on
# co_opts	option A should be off when B is off

off_opts[$options[(i)automenu]]=menucomplete
co_opts[$options[(i)correct]]=correctall
on_opts[$options[(i)correctall]]=correct
off_opts[$options[(i)cshjunkiehistory]]=nobanghist
off_opts[$options[(i)cshnullglob]]="nonomatch nullglob"
off_opts[$options[(i)extendedglob]]=noglob
off_otps[$options[(i)globassign]]=noglob
off_opts[$options[(i)globcomplete]]=noglob
off_opts[$options[(i)globdots]]=noglob
off_opts[$options[(i)globsubst]]=noglob
co_opts[$options[(i)hashcmds]]="hashdirs hashlistall"
on_opts[$options[(i)hashdirs]]=hashcmds
on_opts[$options[(i)hashlistall]]=hashcmds
#off_opts[$options[(i)ignorebraces]]=rcexpandparam
off_opts[$options[(i)menucomplete]]=automenu
co_opts[$options[(i)monitor]]=notify
#off_opts[$options[(i)nobanghist]]=cshjunkiehistory
#off_opts[$options[(i)noglob]]="extendedglob globassign globcomplete globdots globsubst numericglobsort shglob"
off_opts[$options[(i)nonomatch]]="cshnullglob nullglob"
on_opts[$options[(i)notify]]=monitor
off_opts[$options[(i)nullglob]]="cshnullglob nonomatch"
off_opts[$options[(i)numericglobsort]]=noglob
off_opts[$options[(i)rcexpandparam]]=ignorebraces
#off_opts[$options[(i)shglob]]=noglob
on_opts[$options[(i)singlelinezle]]=zle
co_opts[$options[(i)zle]]=singlelinezle

function dosetopt() {
    builtin unsetopt localoptions
    [[ -n "${off_opts[${options[(i)$1]}]}" ]] &&
	eval unsetopt ${off_opts[${options[(i)$1]}]}
    [[ -n "${on_opts[${options[(i)$1]}]}" ]] &&
	eval setopt ${on_opts[${options[(i)$1]}]}
    builtin setopt $1
}
function dounsetopt() {
    builtin unsetopt localoptions
    [[ -n "${co_opts[${options[(i)$1]}]}" ]] &&
	eval builtin unsetopt ${co_opts[${options[(i)$1]}]}
    builtin unsetopt $1
}

function setopt() {
    builtin unsetopt localoptions
    [[ $# -eq 0 ]] && { builtin setopt ; return $? }
    for i; do dosetopt $i; done
}
function unsetopt() {
    builtin unsetopt localoptions
    [[ $# -eq 0 ]] && return
    for i; do dounsetopt $i; done
}
----- 8< ----- snip ----- 8< ----- snip -----

-- 
Bart Schaefer                             Brass Lantern Enterprises
http://www.well.com/user/barts            http://www.nbn.com/people/lantern

New male in /home/schaefer:
>N  2 Justin William Schaefer  Sat May 11 03:43  53/4040  "Happy Birthday"




Messages sorted by: Reverse Date, Date, Thread, Author