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

Re: 'print -s' in a function



On May 26,  5:19am, Huy Le wrote:
} Subject: 'print -s' in a function
}
} VERSION: zsh2.6beta13
} 
} So, I'm trying the following:
} 
} function fed() {
}   # Allows for the editing of functions in memory
} 
[snip]
} 
}   builtin print -s "function `which $1`"
} 
}   fc
} }
} 
} If you try running this, such as running:
}       fed fed
} you'll notice that what shows up in your editor
} is "fed fed" instead of the contents of fed.
} 
} The problem is that the history doesn't get updated right
} away after the 'print -s' line.  Why?  Could this be a bug?

The history does get updated right away; replace "fc" with "history 1"
and you'll see that the `print -s' output is there.

What doesn't get updated seems to be the history position from which
"fc" starts looking backwards for commands to edit.  Even if you use

	n=$(print -P %\!)
	((n++))
	fc $n

You just get "fc: bad history number" even though an event with that
number shows up in the "history 1" output.

This may be intentional behavior of fc for some other reason -- you
don't normally want a command to edit itself.  Stick a SECOND command
into the history:

	builtin print -s "function `which $1`"
	builtin print -s "echo Dummy command for fed"
	fc

and then it works as you wanted -- and the dummy command never shows
up in the history list, it gets replaced by the result of "fc".

BTW, there's a history quoting bug (beta19).  Try:

zsh% function broken () {
> n=$(print -P %\!)
> }
zsh %
      ^
Type C-p here to edit the last command; note that the ! in %! is not
quoted; you get:

zsh% function broken () {
	n=$(print -P %!)
}

This produces a history-not-found error when you press return.

-- 
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