zshrc.d/30-functions-ftzle.zrc
changeset 8 3f68f355bf1a
parent 5 5dbc9ebf690c
child 20 4d2cdb660005
equal deleted inserted replaced
7:c032d39eafcf 8:3f68f355bf1a
       
     1 
       
     2 # This code comes from Frank Terbeck setup
       
     3 # https://dev.0x50.de/projects/ftzsh/
       
     4 # Thanks a lot!
       
     5 
       
     6 # ==========
       
     7 autoload -U accept-line && accept-line
       
     8 
       
     9 #zstyle ':acceptline:empty' call_default false
       
    10 zstyle ':acceptline:*' nocompwarn   true
       
    11 
       
    12 
       
    13 function turn_dots_into_cd() {
       
    14     local buf="$1"
       
    15     buf='cd '
       
    16     for (( i = 1; i <= ${#${cmdline[1]}}; i++ )); do
       
    17         buf="${buf}../"
       
    18     done
       
    19     buf=${buf%/}
       
    20     REPLY="$buf"
       
    21 }
       
    22 
       
    23 function zle_cd_back() {
       
    24     local REPLY
       
    25     setopt extendedglob
       
    26     if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == .# ]] ; then
       
    27         turn_dots_into_cd "${BUFFER}"
       
    28         BUFFER="$REPLY"
       
    29     fi
       
    30 }
       
    31 
       
    32 #function zle_dir_stack() {
       
    33 #    if (( ${#cmdline} == 1 )); then
       
    34 #        case ${cmdline[1]} in
       
    35 #        ((+|-)(|<->))
       
    36 #            BUFFER="cd $BUFFER"
       
    37 #            [[ ${cmdline[1]} == + ]] && BUFFER="${BUFFER}0"
       
    38 #            ;;
       
    39 #        ((#b)-([lcvp]))
       
    40 #            BUFFER="dirs -${match[1]}"
       
    41 #            ;;
       
    42 #        esac
       
    43 #    fi
       
    44 #}
       
    45 
       
    46 zle -N zle_cd_back
       
    47 #zle -N zle_dir_stack
       
    48 
       
    49 #zstyle ':acceptline:preprocess' actions zle_cd_back zle_dir_stack
       
    50 zstyle ':acceptline:preprocess' actions zle_cd_back
       
    51 
       
    52 # ==========
       
    53 
       
    54 zle -C ft-complete-tilde complete-word _generic
       
    55 zstyle ':completion:ft-complete-tilde:*' completer _tilde _expand _complete _ignored _approximate
       
    56 
       
    57 
       
    58 # See http://bewatermyfriend.org/p/2011/013/
       
    59 # "."  will get you 1 directory up,
       
    60 # ".." will get you 2 directories up, etc.
       
    61 function ft-complete-dots() {
       
    62     # Turns ".." into "cd ../../" puts the cursor behind the last `/'
       
    63     # and calls the completion system on that buffer.
       
    64     local REPLY
       
    65     turn_dots_into_cd "$BUFFER"
       
    66     BUFFER="$REPLY"/
       
    67     CURSOR="${#BUFFER}"
       
    68     zle complete-word
       
    69 }
       
    70 zle -N ft-complete-dots
       
    71 
       
    72 function ft-complete-dirstack() {
       
    73     BUFFER="cd $BUFFER"
       
    74     CURSOR="${#BUFFER}"
       
    75     zle complete-word
       
    76 }
       
    77 zle -N ft-complete-dirstack
       
    78 
       
    79 zstyle ':zle:ft-complete:tilde' widget ft-complete-tilde
       
    80 zstyle ':zle:ft-complete:dots' widget ft-complete-dots
       
    81 zstyle ':zle:ft-complete:dirstack' widget ft-complete-dirstack
       
    82 zstyle ':zle:ft-complete:empty' action dot-slash-complete
       
    83 
       
    84 # ==========
       
    85 
       
    86 function ft-complete() {
       
    87     setopt extendedglob localoptions
       
    88     local action context widget word
       
    89     local -a cmdline
       
    90 
       
    91     if [[ -z ${BUFFER} ]]; then
       
    92         context=empty
       
    93         zstyle -s ":zle:ft-complete:${context}" action action || action=empty
       
    94         zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
       
    95         case ${action} in
       
    96             dot-slash-complete)
       
    97                 BUFFER='./'
       
    98                 CURSOR=2
       
    99                 zle ${widget} -w
       
   100                 ;;
       
   101             empty)
       
   102                 ;;
       
   103             *)
       
   104                 zle ${widget} -w
       
   105                 ;;
       
   106         esac
       
   107 
       
   108         return 0
       
   109     fi
       
   110 
       
   111     cmdline=( ${(z)BUFFER} )
       
   112     if (( ${#cmdline} == 1 )); then
       
   113         case ${cmdline[1]} in
       
   114         (.#)
       
   115             context=dots
       
   116             ;;
       
   117         ((-|+)(|<->))
       
   118             context=dirstack
       
   119             ;;
       
   120         (*)
       
   121             context=oneword
       
   122             ;;
       
   123         esac
       
   124         if [[ ${context} != oneword ]]; then
       
   125             zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
       
   126             zle ${widget} -w
       
   127             return 0
       
   128         fi
       
   129     fi
       
   130 
       
   131     word=${LBUFFER##* }
       
   132     if [[ ${word} == \~* ]] ; then
       
   133         context=tilde
       
   134         zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
       
   135         zle ${widget} -w
       
   136         return 0
       
   137     fi
       
   138     context=default
       
   139     zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
       
   140     zle ${widget} -w
       
   141     return 0
       
   142 }
       
   143 zle -N ft-complete
       
   144 bindkey '^I' ft-complete
       
   145 
       
   146 # ==========
       
   147 
       
   148 
       
   149 # This one could be very useful
       
   150 function backward-kill-to-slash() {
       
   151     local WORDCHARS="${WORDCHARS:s,/,} \\\'"
       
   152     [[ $BUFFER != */* ]] && return
       
   153     [[ $LBUFFER == [^/]##/ ]] && return
       
   154     zle backward-kill-word
       
   155 }
       
   156 zle -N backward-kill-to-slash