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

vim-a-like vi mode status bar



Hi all

I have thought it would be nice to have a vim-a-like indicator of
whether or not you are insert or command mode when using the vi line
editting mode for a long time now. In a burst of enthusiasm I have put
something together over the last couple of days (attached). However it's
not quite as nice as it might have been and I have run into a few
annoyances along the way.

Initially I was planning to have the indicator in the prompt. However
AFAICT once the prompt has been displayed it is cached, so even if you
for zsh to redraw it (I tried various things like "zle redisplay", "zle
push-input; zle get-line", "zle -R") it just redraws the old one. Did I
miss a way to do this?

Is there a reason why it shouldn't be possible to pipe something into
source? Or another way to do it without a temporary file?

With the script attached, if I paste a command in then only the first
character is visible until I press ^R. If I type something else first
then it works fine (even if I first delete what I typed!). Perhaps this
is a bug?

I also couldn't find a way to set the statusbar after accepting a
command. zsh -M complains it's not being called from a widget if I call
it from precmd.

Finally I would welcome any comments/criticisms on the code!


Thanks
Ian

# Provides you with a vim-a-like vi-mode status bar showing what mode
# you are in.
# To be called from .zshrc

# Author: Ian Lynagh <igloo@xxxxxxxx>

# Licence: GPL 2
# Copyright: Ian Lynagh <igloo@xxxxxxxx> 2002

# Should we show "<insert>" or "" for insert mode?
alias statusbar-show-insert="zstyle :vi-mode-statusbar: show-insert yes"
alias statusbar-no-show-insert="zstyle :vi-mode-statusbar: show-insert no"
alias statusbar-test-show-insert="zstyle -t :vi-mode-statusbar: show-insert"
statusbar-no-show-insert

# What mode are we now in?
alias statusbar-now-insert="zstyle :vi-mode-statusbar: now-insert yes"
alias statusbar-now-command="zstyle :vi-mode-statusbar: now-insert no"
alias statusbar-test-now-insert="zstyle -t :vi-mode-statusbar: now-insert"

# What mode were we in last time?
alias statusbar-last-insert="zstyle :vi-mode-statusbar: last-insert yes"
alias statusbar-last-command="zstyle :vi-mode-statusbar: last-insert no"
alias statusbar-test-last-insert="zstyle -t :vi-mode-statusbar: last-insert"

# Helper function
function stat-statusbar-update {
    if statusbar-test-now-insert
    then
        if statusbar-test-show-insert
        then
            zle -M "<insert>"
        else
            if ! statusbar-test-last-insert
            then
                zle -M ""
            fi
        fi
        statusbar-last-insert
    else
        zle -M "<command>"
        statusbar-last-command
    fi
}

# Before each prompt reset
function precmd {
    statusbar-now-insert
    statusbar-last-command
}

# Wrap each widget

# This makes a new ~/.zshrc.stat.functions
function gen-stat-functions {
    local EXTRA CHANGE
    rm ~/.zshrc.stat.functions
    zle -l -a | grep "^[a-zA-Z]" | while read WIDGET
    do
        UPDATE="stat-statusbar-update"
        CHANGE=""
        case "$WIDGET" in
            vi-(add-(eol|next)|insert(|-bol)|change-(eol|whole-line) \
               |open-line-(above|below)|replace) \
           |accept-(and-(hold|infer-next-history)|line(|-and-down-history)) \
           |get-line|pound-insert|push-(input|line(|-or-edit)) \
           |send-break|run-help|which-command)
                CHANGE="statusbar-now-insert"
            ;;
            vi-cmd-mode)
                echo foo
                CHANGE="statusbar-now-command"
            ;;
            vi-change)
                CHANGE="statusbar-now-insert"
                UPDATE=""
            ;;
        esac
        echo "
function stat-$WIDGET {
    zle .$WIDGET
    $CHANGE
    $UPDATE
}
zle -N stat-$WIDGET stat-$WIDGET
zle -A stat-$WIDGET      $WIDGET" >> ~/.zshrc.stat.functions
    done
}

# Don't bother updating it automagically - that's just asking for it!
# Just . whatever was last created
. ~/.zshrc.stat.functions

# Use vi mode
bindkey -v



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