ZSH: How to create a new ZLE widget

ZLE (Zsh Line Editor) is the component of zsh which reveice all keybaord events. Though ZLE have several builtin components, it's easy to create new ones. For example, I always dreamed about a widget to apply a regexp to whole buffer except the command. Here the code :


autoload -U read-from-minibuffer

# Define a new function which apply a regexp
# to all words but the first one (the command)
regex-edit () {
        # REPLY will be filled by read-from-minibuffer
        local REPLY words
        words=(${=BUFFER})
        read-from-minibuffer "Regexp:"
        if [ -n $REPLY ]; then
                # Yes I know, it's a bit ugly, but how apply a regex
                # in pure zsh ?
                BUFFER="$words[1] $(echo $words[2,${#words}] | sed ${REPLY})"
        fi
}

# Create a new ZLE widget
zle -N regex-edit

# And finally bind it
bindkey "^[e" regex-edit

Example :

% echo toto (press ESC-e)
Regexp: s/o/a/g
% echo tata