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

Re: zsh-4.04 and w3m browser

On Aug 28,  3:25pm, Bart Schaefer wrote:
} Minimally tested, but seems to work:

Actually there was a bug, which is that even with ${var##pat} it's not
necessarily the case that the longest pattern will be matched when the
pattern contains alternatives with (|) -- because that form tries each
alternative in order, and stops when one matches.

So the right formulation is (only two of the `|' have moved):

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

However, I had another thought about a possible approach to handling URLs
in shell input:

---- 8< ---- snip ---- 8< ----
function url-magic-space {
    local words
    case "$words[-1]" in
    (*[\'\"]*) ;;
	local left="${(qqM)${words[-1]}##ftp://(localhost|)}"
	local right="${${words[-1]}##ftp://(localhost|)}"
	words[-1]="$left"'"${(f)^$(print -lr -- '"$right"')}"' ;;
    (http(|s)|ftp):*) words[-1]="${(qq)words[-1]}" ;;
    LBUFFER="${(j: :)words}" 
    zle self-insert		# Or  zle magic-space  if you prefer ...
zle -N url-magic-space
bindkey ' ' url-magic-space
---- 8< ---- snip ---- 8< ----

When SPACE is pressed, the widget above rewrites e.g.

    zsh% echo ftp://localhost/~/*


    zsh% echo ftp://localhost"${(f)^$(print -lr -- ~/*)}" 

which should have the desired end result (unless you have file names with
embedded newlines), even if it's not so readable.

It also turns

    zsh% lynx http://somewhere.com/something.cgi?x=1&y=2&so=on


    zsh% lynx 'http://somewhere.com/something.cgi?x=1&y=2&so=on' 

(that is, it adds the quotes for you) and it is clever enough to try not
to mess with a word that already uses quotes (though that too might be
improved a little bit with a lot of effort).

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