zsh-workers
 help / color / mirror / code / Atom feed
* contrib: auto-correct, cd-expand
@ 2005-08-27  0:42 Karl Chen
  0 siblings, 0 replies; only message in thread
From: Karl Chen @ 2005-08-27  0:42 UTC (permalink / raw)
  To: Zsh Devel List

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


Here are some interactive functions I've been happily using for a
while.

quarl-auto-correct: automatically correct common typos.
- E.g. "c d" => "cd ", "cxat " => "cat "
- This is much more powerful than aliases; evolved out of separate
  functions I had for fixing "cv sup", "sv nup", "find.", etc.

quarl-cd-expand: auto-expand "cd foo" to "cd <pwd>/foo"
- Useful if you often reuse "cd" history.

Since I have multiple scripts that want to modify behavior of the
same widgets, I have:

quarl-add-hook: allow multiple "hooks" to widgets (in the style of
Emacs hooks).

To use, put files in fpath and run:

    autoload -U quarl-auto-correct-on && quarl-auto-correct-on
    autoload -U quarl-cd-expand-on && quarl-cd-expand-on


[-- Attachment #2: quarl-auto-correct-on --]
[-- Type: application/octet-stream, Size: 1533 bytes --]

# -*- sh -*-
# $Id: quarl-auto-correct-on 7757 2005-02-20 00:45:33Z quarl $

# quarl's command-line auto-correction facility

# quarl 2005-01-01 initial version

autoload quarl-add-hook

quarl-auto-correct-on() {
    quarl-add-hook after self-insert q-auto-correct
}

typeset -g -A q_auto_corrections
q-add-auto-correction() {
    q_auto_corrections[$1]=$2
}

q-init-auto-correct() {
    q-add-auto-correction 'autolaod ' 'autoload '
    q-add-auto-correction 'c d' 'cd '
    q-add-auto-correction 'csv ' 'cvs '
    q-add-auto-correction 'cv s' 'cvs '
    q-add-auto-correction 'cxat ' 'cat '
    q-add-auto-correction 'dc ' 'cd '
    q-add-auto-correction 'ech o' 'echo '
    q-add-auto-correction 'ech0 ' 'echo '
    q-add-auto-correction 'find. ' 'find . '
    q-add-auto-correction 'kilall ' 'killall '
    q-add-auto-correction 'pign ' 'ping '
    q-add-auto-correction 'quota-v' 'quota -v'
    q-add-auto-correction 'sl ' 'ls '
    q-add-auto-correction 'snv ' 'svn '
    q-add-auto-correction 'sv n' 'svn '
    q-add-auto-correction 'cd.' 'cd .'
    q-add-auto-correction 'cd/' 'cd /'
    q-add-auto-correction 'lss ' 'less '
    q-add-auto-correction 'lses ' 'less '
    q-add-auto-correction 'elss ' 'less '
    q-add-auto-correction 'ess ' 'less '
}

q-auto-correct() {
    [[ -z $RBUFFER ]] || return;

    repl=$q_auto_corrections[$LBUFFER]
    if [[ -n $repl ]]; then
        zle -M "auto-correct: '$LBUFFER' => '$repl'"
        LBUFFER=$repl
    fi
}

q-init-auto-correct

[[ -o kshautoload ]] || quarl-auto-correct-on

[-- Attachment #3: quarl-cd-expand-on --]
[-- Type: application/octet-stream, Size: 651 bytes --]

# -*- sh -*-
# $Id: quarl-cd-expand-on 8361 2005-07-27 23:29:57Z quarl $

# Expand "cd foo" to "cd $PWD/foo".  Useful for making history more usable
# later.

# quarl 2005-01-01 initial version
# quarl 2005-07-27 bugfix for spaces in directories

autoload quarl-add-self-insert-hook

quarl-cd-expand-on() {
    quarl-add-hook after self-insert q-cd-expand
}

q-cd-expand() {
    [[ -z $RBUFFER ]] || return;

    local mpwd=${(q)PWD%/}

    if [[ $LBUFFER == cd\ [^/~=\$\|\`\ \'] ]]; then
        LBUFFER="cd $mpwd/$LBUFFER[-1]"
    elif [[ $LBUFFER == cd\ $PWD/cd\  ]]; then
        LBUFFER="cd "
    fi
}

[[ -o kshautoload ]] || quarl-cd-expand-on

[-- Attachment #4: quarl-add-hook --]
[-- Type: application/octet-stream, Size: 1620 bytes --]

# -*- sh -*-
# $Id: quarl-add-hook 7550 2005-01-21 01:37:45Z quarl $

# Allow multiple hooks to a specified widget.

# Synopsis:
#   quarl-add-hook after self-insert foo
#   quarl-remove-hook after self-insert foo

# Before-hooks should return 1 if they don't want the real widget executed.

# quarl 2005-01-01 initial version

quarl-add-hook() {
    if [[ $# != 3 || ! ($1 == before || $1 == after) ]]; then
        print -u2 "syntax: quarl-add-hook [before|after] widget-name funcname"
        return 1
    fi

    typeset -g -a $(q-make-hook-name before $2)
    typeset -g -a $(q-make-hook-name after $2)
    zle -N $2 q-run-with-hooks

    q-add-to-list $(q-make-hook-name $1 $2) $3
}

quarl-remove-hook() {
    if [[ $# != 3 || ! ($1 == before || $1 == after) ]]; then
        print -u2 "syntax: quarl-remove-hook [before|after] widget-name funcname"
        return 1
    fi

    q-remove-from-list $(q-make-hook-name $1 $2) $3
}

q-make-hook-name() {
    echo q_${2:gs/-/_/}_$1_hooks
}

q-add-to-list() {
    # eval $1+=$2
    eval "$1=(\$$1 $2)"
}

q-remove-from-list() {
    local -A result
    for hook in ${(P)1}; do
        if [[ $hook != $2 ]]; then
            #result+=$hook
            result=($result $hook)
        fi
    done
    eval $1=($result)
}

q-run-hooks() {
    # returns 0 if all of the hooks returned 0
    hookname=$(q-make-hook-name $1 $2)
    retval=0
    for hook in ${(P)hookname} ; do
        $hook || retval=1
    done
    return $retval
}

q-run-with-hooks()
{
    q-run-hooks before $WIDGET && zle .$WIDGET
    q-run-hooks after $WIDGET
}

[[ -o kshautoload ]] || quarl-add-hook $@

[-- Attachment #5: Type: text/plain, Size: 28 bytes --]



-- 
Karl 2005-08-26 17:31

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-08-27  0:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-27  0:42 contrib: auto-correct, cd-expand Karl Chen

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