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

Re: Bart's urlglobber question [url-magic-space]



On Oct 3,  5:55pm, Bart Schaefer wrote:
}
} Another improvement that occurred to me is to have it be aware of and
} work with the urlglobber function, so that instead of rewriting the
} local-ftp URLs it just quotes them and lets urlglobber deal with it.

Here's something for zsh-workers to think about:  There's no good way
to "stack" functions like this one so that, e.g., you could use it at
the same time as insert-and-predict.

---- 8< ---- snip ---- 8< ----
# Functions to make it easier to type URLs as command line arguments.  As
# you type, the input character is analyzed and, if it may need quoting,
# the current word is checked for a URI schema.  If one is found and the
# current word is not already in quotes, a backslash is inserted before
# the input character.

# As an exception, glob characters in FTP URLs with local file paths are
# not quoted; instead it is assumed that the current command is aliased
# to use the urlglobber helper function.

# Setup:
#	autoload -U url-quote-magic
#	zle -N self-insert url-quote-magic

# TODO:
#	Add zstyles for which URI schema use local globbing.
#	Add zstyles for which characters to quote in each scheme.
#	Automatically recognize commands aliased to urlglobber.
#	Turn this on at colon, and off again at space or accept.
#	Use compsys for nested quoting analysis (e.g., eval).

typeset -gH __url_globs='*?[]^(|)~#{}=' __url_seps=';&<>!'

function urlglobber {
    local -a args globbed
    local arg command="$1"
    shift
    for arg
    do
	case "${arg}" in
	(ftp:/(|/localhost)/*)
	    globbed=( ${~${arg##ftp://(localhost|)}} )
	    args[$#args+1]=( "${(M)arg##ftp://(localhost|)}${(@)^globbed}" )
	    ;;
	((http(|s)|ftp):*) args[${#args}+1]="$arg";;
	(*) args[${#args}+1]=( ${~arg} );;
	esac
    done
    "$command" "${(@)args}"
}
alias globurl='noglob urlglobber'

function url-quote-magic {
    local qkey="${(q)KEYS}"
    if [[ "$KEYS" != "$qkey" ]]
    then
	local lbuf="$LBUFFER$qkey"
	if [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
	then
	    local words
	    words=("${(@Q)${(q)=LBUFFER}}")
	    case "$words[-1]" in
	    (*[\'\"]*) ;;
	    (ftp:/(|/localhost)/*)
		[[ "$__url_seps" == *"$KEYS"* ]] &&
		    LBUFFER="$LBUFFER\\" ;;
	    ((http(|s)|ftp):*)
		[[ "$__url_seps$__url_globs" == *"$KEYS"* ]] &&
		    LBUFFER="$LBUFFER\\" ;;
	    esac
	fi
    fi
    zle .self-insert
}
zle -N self-insert url-quote-magic

# Handle zsh autoloading conventions

[[ -o kshautoload ]] || url-quote-magic "$@"
---- 8< ---- snip ---- 8< ----

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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