Update ..* commands
authorMikael Berthe <mikael@lilotux.net>
Sun, 15 Jul 2012 01:17:55 +0200
changeset 8 3f68f355bf1a
parent 7 c032d39eafcf
child 9 d7843f267d79
Update ..* commands Code from Frank Terbeck (https://dev.0x50.de/projects/ftzsh/).
functions/accept-line
zshrc
zshrc.d/30-functions-ftupdir.zrc
zshrc.d/30-functions-ftzle.zrc
zshrc.d/60-vi-showmode.zrc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions/accept-line	Sun Jul 15 01:17:55 2012 +0200
@@ -0,0 +1,133 @@
+### a generic accept-line wrapper
+### distributed under the same terms as zsh itself.
+#
+# (Mikael: copied from Frank Terbeck repository
+# at https://dev.0x50.de/projects/ftzsh/repository)
+
+function Accept-Line() {
+    setopt localoptions noksharrays
+    local -a subs
+    local -xi aldone
+    local sub
+    local alcontext=${1:-$alcontext}
+
+    zstyle -a ":acceptline:${alcontext}" actions subs
+
+    (( ${#subs} < 1 )) && return 0
+
+    (( aldone = 0 ))
+    for sub in ${subs} ; do
+        [[ ${sub} == 'accept-line' ]] && sub='.accept-line'
+        zle ${sub}
+
+        (( aldone > 0 )) && break
+    done
+}
+
+function Accept-Line-getdefault() {
+    local default_action
+
+    zstyle -s ":acceptline:${alcontext}" default_action default_action
+    case ${default_action} in
+        ((accept-line|))
+            REPLY=".accept-line"
+            ;;
+        (*)
+            REPLY=${default_action}
+            ;;
+    esac
+}
+
+function Accept-Line-HandleContext() {
+    local REPLY
+
+    zle Accept-Line
+    Accept-Line-getdefault
+    default_action="${REPLY}"
+    zstyle -T ":acceptline:${alcontext}" call_default \
+        && zle ${default_action}
+}
+
+function accept-line() {
+    setopt localoptions noksharrays
+    local -ax cmdline
+    local -x alcontext
+    local buf com fname format msg default_action
+
+    alcontext='default'
+    buf="${BUFFER}"
+    cmdline=(${(z)BUFFER})
+    com="${cmdline[1]}"
+    fname="_${com}"
+
+    Accept-Line 'preprocess'
+
+    zstyle -t ":acceptline:${alcontext}" rehash \
+        && [[ -z ${commands[$com]} ]]           \
+        && rehash
+
+    if    [[ -n ${com}               ]] \
+       && [[ -n ${reswords[(r)$com]} ]] \
+       || [[ -n ${aliases[$com]}     ]] \
+       || [[ -n ${functions[$com]}   ]] \
+       || [[ -n ${builtins[$com]}    ]] \
+       || [[ -n ${commands[$com]}    ]] ; then
+
+        # there is something sensible to execute, just do it.
+        alcontext='normal'
+        Accept-Line-HandleContext
+
+        return
+    fi
+
+    if    [[ -o correct              ]] \
+       || [[ -o correctall           ]] \
+       && [[ -n ${functions[$fname]} ]] ; then
+
+        # nothing there to execute but there is a function called
+        # _command_name; a completion widget. Makes no sense to
+        # call it on the commandline, but the correct{,all} options
+        # will ask for it nevertheless, so warn the user.
+        if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
+            # Okay, we warned the user before, he called us again,
+            # so have it his way.
+            alcontext='force'
+            Accept-Line-HandleContext
+
+            return
+        fi
+
+        if zstyle -t ":acceptline:${alcontext}" nocompwarn ; then
+            alcontext='normal'
+            Accept-Line-HandleContext
+        else
+            # prepare warning message for the user, configurable via zstyle.
+            zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
+
+            if [[ -z ${msg} ]] ; then
+                msg="%c will not execute and completion %f exists."
+            fi
+
+            zformat -f msg "${msg}" "c:${com}" "f:${fname}"
+
+            zle -M -- "${msg}"
+        fi
+        return
+    elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
+        # If we are here, the commandline contains something that is not
+        # executable, which is neither subject to _command_name correction
+        # and is not empty. might be a variable assignment
+        alcontext='misc'
+        Accept-Line-HandleContext
+
+        return
+    fi
+
+    # If we got this far, the commandline only contains whitespace, or is empty.
+    alcontext='empty'
+    Accept-Line-HandleContext
+}
+
+zle -N accept-line
+zle -N Accept-Line
+zle -N Accept-Line-HandleContext
--- a/zshrc	Sun Jul 15 01:13:18 2012 +0200
+++ b/zshrc	Sun Jul 15 01:17:55 2012 +0200
@@ -290,6 +290,8 @@
 # Source configuration files #
 ##############################
 
+fpath=($ZDOTDIR/functions $fpath)
+
 for rc in $ZDOTDIR/zshrc.d/??-*.zrc; do
     [[ -r ${rc} ]] && source "$rc"
 done
--- a/zshrc.d/30-functions-ftupdir.zrc	Sun Jul 15 01:13:18 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-
-# Customized version from Frank Terbeck' scripts
-# http://bewatermyfriend.org/p/2011/013/
-# "."   will display the current working directory
-# ".."  will get you 1 directory up,
-# "..." will get you 2 directories up, etc.
-function ft_accept_line_cd_up() {
-    setopt local_options extended_glob
-    local -a cmdline
-    cmdline=( ${(z)BUFFER} )
-    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == "." ]] ; then
-        BUFFER="pwd"
-        zle ".$WIDGET"
-        return
-    fi
-    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == ..## ]] ; then
-        BUFFER='cd '
-        for (( i = 2; i <= ${#${cmdline[1]}}; i++ )); do
-            BUFFER="${BUFFER}../"
-        done
-        BUFFER=${BUFFER%/}
-    fi
-    zle ".$WIDGET"
-}
-zle -N accept-line ft_accept_line_cd_up
-
-# 2nd part, completion support...
-function ft_complete_with_dots() {
-    setopt local_options extended_glob
-    local -a cmdline
-    cmdline=( ${(z)BUFFER} )
-    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == ..## ]] ; then
-        BUFFER='cd '
-        for (( i = 2; i <= ${#${cmdline[1]}}; i++ )); do
-            BUFFER="${BUFFER}../"
-        done
-        CURSOR=${#BUFFER}
-    fi
-    zle ".$WIDGET"
-}
-# Disabling for now as I don't know how to fallback to automatic correction...
-#zle -N expand-or-complete ft_complete_with_dots
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zshrc.d/30-functions-ftzle.zrc	Sun Jul 15 01:17:55 2012 +0200
@@ -0,0 +1,156 @@
+
+# This code comes from Frank Terbeck setup
+# https://dev.0x50.de/projects/ftzsh/
+# Thanks a lot!
+
+# ==========
+autoload -U accept-line && accept-line
+
+#zstyle ':acceptline:empty' call_default false
+zstyle ':acceptline:*' nocompwarn   true
+
+
+function turn_dots_into_cd() {
+    local buf="$1"
+    buf='cd '
+    for (( i = 1; i <= ${#${cmdline[1]}}; i++ )); do
+        buf="${buf}../"
+    done
+    buf=${buf%/}
+    REPLY="$buf"
+}
+
+function zle_cd_back() {
+    local REPLY
+    setopt extendedglob
+    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == .# ]] ; then
+        turn_dots_into_cd "${BUFFER}"
+        BUFFER="$REPLY"
+    fi
+}
+
+#function zle_dir_stack() {
+#    if (( ${#cmdline} == 1 )); then
+#        case ${cmdline[1]} in
+#        ((+|-)(|<->))
+#            BUFFER="cd $BUFFER"
+#            [[ ${cmdline[1]} == + ]] && BUFFER="${BUFFER}0"
+#            ;;
+#        ((#b)-([lcvp]))
+#            BUFFER="dirs -${match[1]}"
+#            ;;
+#        esac
+#    fi
+#}
+
+zle -N zle_cd_back
+#zle -N zle_dir_stack
+
+#zstyle ':acceptline:preprocess' actions zle_cd_back zle_dir_stack
+zstyle ':acceptline:preprocess' actions zle_cd_back
+
+# ==========
+
+zle -C ft-complete-tilde complete-word _generic
+zstyle ':completion:ft-complete-tilde:*' completer _tilde _expand _complete _ignored _approximate
+
+
+# See http://bewatermyfriend.org/p/2011/013/
+# "."  will get you 1 directory up,
+# ".." will get you 2 directories up, etc.
+function ft-complete-dots() {
+    # Turns ".." into "cd ../../" puts the cursor behind the last `/'
+    # and calls the completion system on that buffer.
+    local REPLY
+    turn_dots_into_cd "$BUFFER"
+    BUFFER="$REPLY"/
+    CURSOR="${#BUFFER}"
+    zle complete-word
+}
+zle -N ft-complete-dots
+
+function ft-complete-dirstack() {
+    BUFFER="cd $BUFFER"
+    CURSOR="${#BUFFER}"
+    zle complete-word
+}
+zle -N ft-complete-dirstack
+
+zstyle ':zle:ft-complete:tilde' widget ft-complete-tilde
+zstyle ':zle:ft-complete:dots' widget ft-complete-dots
+zstyle ':zle:ft-complete:dirstack' widget ft-complete-dirstack
+zstyle ':zle:ft-complete:empty' action dot-slash-complete
+
+# ==========
+
+function ft-complete() {
+    setopt extendedglob localoptions
+    local action context widget word
+    local -a cmdline
+
+    if [[ -z ${BUFFER} ]]; then
+        context=empty
+        zstyle -s ":zle:ft-complete:${context}" action action || action=empty
+        zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
+        case ${action} in
+            dot-slash-complete)
+                BUFFER='./'
+                CURSOR=2
+                zle ${widget} -w
+                ;;
+            empty)
+                ;;
+            *)
+                zle ${widget} -w
+                ;;
+        esac
+
+        return 0
+    fi
+
+    cmdline=( ${(z)BUFFER} )
+    if (( ${#cmdline} == 1 )); then
+        case ${cmdline[1]} in
+        (.#)
+            context=dots
+            ;;
+        ((-|+)(|<->))
+            context=dirstack
+            ;;
+        (*)
+            context=oneword
+            ;;
+        esac
+        if [[ ${context} != oneword ]]; then
+            zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
+            zle ${widget} -w
+            return 0
+        fi
+    fi
+
+    word=${LBUFFER##* }
+    if [[ ${word} == \~* ]] ; then
+        context=tilde
+        zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
+        zle ${widget} -w
+        return 0
+    fi
+    context=default
+    zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
+    zle ${widget} -w
+    return 0
+}
+zle -N ft-complete
+bindkey '^I' ft-complete
+
+# ==========
+
+
+# This one could be very useful
+function backward-kill-to-slash() {
+    local WORDCHARS="${WORDCHARS:s,/,} \\\'"
+    [[ $BUFFER != */* ]] && return
+    [[ $LBUFFER == [^/]##/ ]] && return
+    zle backward-kill-word
+}
+zle -N backward-kill-to-slash
--- a/zshrc.d/60-vi-showmode.zrc	Sun Jul 15 01:13:18 2012 +0200
+++ b/zshrc.d/60-vi-showmode.zrc	Sun Jul 15 01:17:55 2012 +0200
@@ -231,6 +231,10 @@
 
 bindkey -M viins -s '^X^H' 'hash -r\n'
 
+# These bindings depend on the ftzle script
+bindkey -M viins "^X^W" backward-kill-to-slash
+bindkey '^I' ft-complete
+
 
 if [[ -z ${TERM:#screen} ]]; then
   bindkey -M viins '[1~'  beginning-of-line   # Home