zsh-users
 help / color / mirror / code / Atom feed
From: Steve Talley <stephen.talley@sun.com>
To: Ian Lynagh <igloo@earth.li>
Cc: zsh-users@sunsite.dk
Subject: Re: vim-a-like vi mode status bar
Date: Wed, 20 Feb 2002 15:12:56 -0700	[thread overview]
Message-ID: <20020220151256.I26794@thpppt> (raw)
In-Reply-To: <20020216172000.GA892@stu163.keble.ox.ac.uk>; from igloo@earth.li on Sat, Feb 16, 2002 at 05:20:00PM +0000

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

I have a mode line that appears below the command line (attached).  It
has a bug here and there, but is fairly solid and pretty simple.

Steve

Ian Lynagh wrote:

> 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: zshmode --]
[-- Type: text/plain, Size: 2387 bytes --]

#
# Set vi mode status bar
#

#
# Reads until the given character has been entered.
#
readuntil () {
    typeset a
    while [ "$a" != "$1" ]
    do
        read -E -k 1 a
    done
}

#
# If the $SHOWMODE variable is set, displays the vi mode, specified by
# the $VIMODE variable, under the current command line.
# 
# Arguments:
#
#   1 (optional): Beyond normal calculations, the number of additional
#   lines to move down before printing the mode.  Defaults to zero.
#
showmode() {
    typeset movedown
    typeset row

    # Get number of lines down to print mode
    movedown=$(($(echo "$RBUFFER" | wc -l) + ${1:-0}))
    
    # Get current row position
    echo -n "\e[6n"
    row="${${$(readuntil R)#*\[}%;*}"
    
    # Are we at the bottom of the terminal?
    if [ $((row+movedown)) -gt "$LINES" ]
    then
        # Scroll terminal up one line
        echo -n "\e[1S"
        
        # Move cursor up one line
        echo -n "\e[1A"
    fi
    
    # Save cursor position
    echo -n "\e[s"
    
    # Move cursor to start of line $movedown lines down
    echo -n "\e[$movedown;E"
    
    # Change font attributes
    echo -n "\e[1m"
    
    # Has a mode been set?
    if [ -n "$VIMODE" ]
    then
        # Print mode line
        echo -n "-- $VIMODE -- "
    else
        # Clear mode line
        echo -n "\e[0K"
    fi

    # Restore font
    echo -n "\e[0m"
    
    # Restore cursor position
    echo -n "\e[u"
}

clearmode() {
    VIMODE= showmode
}

#
# Temporary function to extend built-in widgets to display mode.
#
#   1: The name of the widget.
#
#   2: The mode string.
#
#   3 (optional): Beyond normal calculations, the number of additional
#   lines to move down before printing the mode.  Defaults to zero.
#
makemodal () {
    # Create new function
    eval "$1() { zle .'$1'; ${2:+VIMODE='$2'}; showmode $3 }"

    # Create new widget
    zle -N "$1"
}

# Extend widgets
makemodal vi-add-eol           INSERT
makemodal vi-add-next          INSERT
makemodal vi-change            INSERT
makemodal vi-change-eol        INSERT
makemodal vi-change-whole-line INSERT
makemodal vi-insert            INSERT
makemodal vi-insert-bol        INSERT
makemodal vi-open-line-above   INSERT
makemodal vi-substitute        INSERT
makemodal vi-open-line-below   INSERT 1
makemodal vi-replace           REPLACE
makemodal vi-cmd-mode          NORMAL

unfunction makemodal

      parent reply	other threads:[~2002-02-20 22:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-16 17:20 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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020220151256.I26794@thpppt \
    --to=stephen.talley@sun.com \
    --cc=igloo@earth.li \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).