zshrc.d/30-functions-ftzle.zrc
changeset 8 3f68f355bf1a
parent 5 5dbc9ebf690c
child 20 4d2cdb660005
--- /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