add the functions needed for some keybindings
authorJoerg Jaspert <joerg@debian.org>
Fri, 5 Apr 2013 06:07:21 +0000 (08:07 +0200)
committerJoerg Jaspert <joerg@debian.org>
Fri, 5 Apr 2013 06:07:21 +0000 (08:07 +0200)
.zsh/functions/accept-line [new file with mode: 0644]
.zsh/functions/beginning-or-end-of-somewhere [new file with mode: 0644]
.zsh/functions/define-pipe-widget [new file with mode: 0644]
.zsh/functions/inplaceMkDirs [new file with mode: 0644]
.zsh/functions/slash-backward-kill-word [new file with mode: 0644]

diff --git a/.zsh/functions/accept-line b/.zsh/functions/accept-line
new file mode 100644 (file)
index 0000000..eae4af2
--- /dev/null
@@ -0,0 +1,131 @@
+# -*- mode:sh -*-
+### a generic accept-line wrapper
+### distributed under the same terms as zsh itself.
+
+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
diff --git a/.zsh/functions/beginning-or-end-of-somewhere b/.zsh/functions/beginning-or-end-of-somewhere
new file mode 100644 (file)
index 0000000..c4d7dab
--- /dev/null
@@ -0,0 +1,16 @@
+# -*- mode:sh -*-
+
+## beginning-of-line OR beginning-of-buffer OR beginning of history
+## by: Bart Schaefer <schaefer@brasslantern.com>, Bernhard Tittelbach
+beginning-or-end-of-somewhere() {
+    local hno=$HISTNO
+    if [[ ( "${LBUFFER[-1]}" == $'\n' && "${WIDGET}" == beginning-of* ) || \
+        ( "${RBUFFER[1]}" == $'\n' && "${WIDGET}" == end-of* ) ]]; then
+        zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
+    else
+        zle .${WIDGET:s/somewhere/line-hist/} "$@"
+        if (( HISTNO != hno )); then
+            zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
+        fi
+    fi
+}
diff --git a/.zsh/functions/define-pipe-widget b/.zsh/functions/define-pipe-widget
new file mode 100644 (file)
index 0000000..95e2c91
--- /dev/null
@@ -0,0 +1,99 @@
+# -*- mode:sh -*-
+
+# Widget from Max Mikhanosha posted to zsh-users list at
+# http://www.zsh.org/mla/users/2008/msg00708.html
+
+# define-pipe-widget <widget-name> "template1" "template2" ....
+#
+# Defines "pipe widget" templates. Its hard to describe the best way
+# is through an example:
+#
+# define-pipe-widget insert_grep  "grep \"@@@\"" "grep -i \"@@@\"" "grep @@@" 
+# define-pipe-widget insert_head "head" "head\n"
+#
+# bindkey "\M-g" insert_grep
+# bindkey "\M-h" insert_head
+#
+# Now pressing Alt-G will insert (| represents cursor position)
+#
+# On empty command line: grep "|"
+#
+# On non-empty command line: <old cmdline> | grep "|"
+#
+# Pressing Alt-G one more time cycles through choices
+# ie:
+#  ... |  grep "|"
+#  ... | grep -i "|"
+#  ... | grep
+#  
+# If template has \n in the end, then it will be auto-accepted:
+# 
+#      Alt-H does: ... | head <cursor here>
+#  2nd Alt-H does  ... | head<Enter>
+#
+declare -gA pipe_widgets
+function define-pipe-widget () {
+    local var=$1 
+    local templates=_${var}_templates
+    declare -ga $templates
+    shift
+    set -A $templates $@
+    zle -N $var insert-pipe-command
+}
+
+insert-pipe-command () {
+    emulate -L zsh
+    local var=$WIDGET
+    local templates=_${var}_templates
+    local before after auto_accept same patnum
+
+    set nomatch
+    # see if command line is same as in our last invocation
+    if [[ $CURSOR == ${pipe_widgets[cursor_$var]} 
+                && $HISTNO == $pipe_widgets[histno_$var] 
+                && $BUFFER == $pipe_widgets[buffer_$var] ]] ; then
+        (( patnum = ++pipe_widgets[patnum_$var] ))
+        # wrap around
+        if [[ $patnum -gt ${#${(P)templates}}  ]] ; then
+            (( patnum = pipe_widgets[patnum_$var] = 1 ))
+        fi
+        BUFFER=$pipe_widgets[buffer_before_$var]
+        CURSOR=$pipe_widgets[cursor_before_$var]
+    else
+       # start from scratch
+       (( patnum = pipe_widgets[patnum_$var] = 1 ))
+       pipe_widgets[buffer_before_$var]=$BUFFER
+       pipe_widgets[cursor_before_$var]=$CURSOR
+    fi
+    local tmp=${${(P)templates}[$patnum]}
+    if [[ $tmp == *\\n ]] ; then
+        auto_accept=1
+        tmp=$tmp[1,-3]
+    fi
+    # cursor in the end if not specified
+    if [[ $tmp != *@@@* ]] ; then
+        tmp="${tmp}@@@"
+    fi
+    before=${tmp%@@@*}
+    after=${tmp#*@@@}
+    if [[ -n ${LBUFFER## *} ]] ; then
+        RBUFFER+=" | "
+    else
+        if [[ $after == '' && $before[-1] != " " ]] ; then
+            before+=" "
+        fi
+        auto_accept=
+    fi
+    RBUFFER+=$before$after
+    CURSOR=$(( $#BUFFER - $#after))
+    # incase we were in vi mode
+    builtin zle vi-insert 
+    if [[ $auto_accept == 1 ]] ; then
+        builtin zle accept-line
+    fi
+    pipe_widgets[histno_$var]=$HISTNO
+    pipe_widgets[buffer_$var]=$BUFFER
+    pipe_widgets[cursor_$var]=$CURSOR
+}
+
+define-pipe-widget "$@"
diff --git a/.zsh/functions/inplaceMkDirs b/.zsh/functions/inplaceMkDirs
new file mode 100644 (file)
index 0000000..2ac2f19
--- /dev/null
@@ -0,0 +1,33 @@
+# -*- mode:sh -*-
+#f5# Create directory under cursor or the selected area
+inplaceMkDirs() {
+    # Press ctrl-xM to create the directory under the cursor or the selected area.
+    # To select an area press ctrl-@ or ctrl-space and use the cursor.
+    # Use case: you type "mv abc ~/testa/testb/testc/" and remember that the
+    # directory does not exist yet -> press ctrl-XM and problem solved
+    local PATHTOMKDIR
+    if ((REGION_ACTIVE==1)); then
+        local F=$MARK T=$CURSOR
+        if [[ $F -gt $T ]]; then
+            F=${CURSOR}
+            T=${MARK}
+        fi
+        # get marked area from buffer and eliminate whitespace
+        PATHTOMKDIR=${BUFFER[F+1,T]%%[[:space:]]##}
+        PATHTOMKDIR=${PATHTOMKDIR##[[:space:]]##}
+    else
+        local bufwords iword
+        bufwords=(${(z)LBUFFER})
+        iword=${#bufwords}
+        bufwords=(${(z)BUFFER})
+        PATHTOMKDIR="${(Q)bufwords[iword]}"
+    fi
+    [[ -z "${PATHTOMKDIR}" ]] && return 1
+    PATHTOMKDIR=${~PATHTOMKDIR}
+    if [[ -e "${PATHTOMKDIR}" ]]; then
+        zle -M " path already exists, doing nothing"
+    else
+        zle -M "$(mkdir -p -v "${PATHTOMKDIR}")"
+        zle end-of-line
+    fi
+}
diff --git a/.zsh/functions/slash-backward-kill-word b/.zsh/functions/slash-backward-kill-word
new file mode 100644 (file)
index 0000000..de42a4a
--- /dev/null
@@ -0,0 +1,7 @@
+# -*- mode:sh -*-
+# only slash should be considered as a word separator:
+slash-backward-kill-word() {
+    local WORDCHARS="${WORDCHARS:s@/@}"
+    # zle backward-word
+    zle backward-kill-word
+}