zsh-users
 help / color / mirror / code / Atom feed
* vim-a-like vi mode status bar
@ 2002-02-16 17:20 Ian Lynagh
  2002-02-16 17:48 ` Thomas Köhler
  2002-02-20 22:12 ` Steve Talley
  0 siblings, 2 replies; 5+ messages in thread
From: Ian Lynagh @ 2002-02-16 17:20 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1272 bytes --]


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


[-- Attachment #2: zsh-vi-mode-status-bar --]
[-- Type: text/plain, Size: 2772 bytes --]


# 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@earth.li>

# Licence: GPL 2
# Copyright: Ian Lynagh <igloo@earth.li> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-02-20 22:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-16 17:20 vim-a-like vi mode status bar Ian Lynagh
2002-02-16 17:48 ` Thomas Köhler
2002-02-16 20:05   ` Ian Lynagh
2002-02-17 18:57     ` Thomas Köhler
2002-02-20 22:12 ` Steve Talley

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).