+# Load a few more functions and tie them to widgets, so they can be bound:
+
+function zrcautozle() {
+ emulate -L zsh
+ local fnc=$1
+ zrcautoload $fnc && zle -N $fnc
+}
+
+function zrcgotwidget() {
+ (( ${+widgets[$i]} ))
+}
+
+function zrcgotkeymap() {
+ [[ -n ${(M)keymaps:#$1} ]]
+}
+
+zrcautozle insert-files
+zrcautozle edit-command-line
+zrcautozle insert-unicode-char
+if zrcautoload history-search-end; then
+ zle -N history-beginning-search-backward-end history-search-end
+ zle -N history-beginning-search-forward-end history-search-end
+fi
+zle -C hist-complete complete-word _generic
+zstyle ':completion:hist-complete:*' completer _history
+
+# The actual terminal setup hooks and bindkey-calls:
+
+# An array to note missing features to ease diagnosis in case of problems.
+typeset -ga grml_missing_features
+
+function zrcbindkey() {
+ if (( ARGC && ${+widgets[${argv[-1]}]} )); then
+ bindkey "$@"
+ fi
+}
+
+function bind2maps () {
+ local i sequence widget
+ local -a maps
+
+ while [[ "$1" != "--" ]]; do
+ maps+=( "$1" )
+ shift
+ done
+ shift
+
+ sequence="${key[$1]}"
+ widget="$2"
+
+ [[ -z "$sequence" ]] && return 1
+
+ for i in "${maps[@]}"; do
+ zrcbindkey -M "$i" "$sequence" "$widget"
+ done
+}
+
+if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
+ function zle-smkx () {
+ printf '%s' ${terminfo[smkx]}
+ }
+ function zle-rmkx () {
+ printf '%s' ${terminfo[rmkx]}
+ }
+ function zle-line-init () {
+ zle-smkx
+ }
+ function zle-line-finish () {
+ zle-rmkx
+ }
+ zle -N zle-line-init
+ zle -N zle-line-finish
+else
+ for i in {s,r}mkx; do
+ (( ${+terminfo[$i]} )) || grml_missing_features+=($i)
+ done
+ unset i
+fi
+
+typeset -A key
+key=(
+ Home "${terminfo[khome]}"
+ End "${terminfo[kend]}"
+ Insert "${terminfo[kich1]}"
+ Delete "${terminfo[kdch1]}"
+ Up "${terminfo[kcuu1]}"
+ Down "${terminfo[kcud1]}"
+ Left "${terminfo[kcub1]}"
+ Right "${terminfo[kcuf1]}"
+ PageUp "${terminfo[kpp]}"
+ PageDown "${terminfo[knp]}"
+ BackTab "${terminfo[kcbt]}"
+)
+
+bind2maps emacs -- Home beginning-of-somewhere
+bind2maps viins vicmd -- Home vi-beginning-of-line
+bind2maps emacs -- End end-of-somewhere
+bind2maps viins vicmd -- End vi-end-of-line
+bind2maps emacs viins -- Insert overwrite-mode
+bind2maps vicmd -- Insert vi-insert
+bind2maps emacs -- Delete delete-char
+bind2maps viins vicmd -- Delete vi-delete-char
+bind2maps emacs viins vicmd -- Up up-line-or-search
+bind2maps emacs viins vicmd -- Down down-line-or-search
+bind2maps emacs -- Left backward-char
+bind2maps viins vicmd -- Left vi-backward-char
+bind2maps emacs -- Right forward-char
+bind2maps viins vicmd -- Right vi-forward-char
+bind2maps viins vicmd -- Right vi-forward-char
+
+if zrcgotkeymap menuselect; then
+ bind2maps menuselect -- BackTab reverse-menu-complete
+
+ #k# menu selection: pick item but stay in the menu
+ zrcbindkey -M menuselect '\e^M' accept-and-menu-complete
+ # also use + and INSERT since it's easier to press repeatedly
+ zrcbindkey -M menuselect "+" accept-and-menu-complete
+ zrcbindkey -M menuselect "^[[2~" accept-and-menu-complete
+
+ # accept a completion and try to complete again by using menu
+ # completion; very useful with completing directories
+ # by using 'undo' one's got a simple file browser
+ zrcbindkey -M menuselect '^o' accept-and-infer-next-history
+fi
+
+#k# Display list of abbreviations that expand when followed by ,.
+zrcbindkey ",." zleiab
+zrcbindkey '^xb' help-show-abk
+zrcbindkey '^xM' inplaceMkDirs
+#k# display help for keybindings and ZLE
+zrcbindkey '^xz' help-zle
+
+#k# Insert files and test globbing
+zrcbindkey "^xf" insert-files # C-x-f
+
+#k# Edit the current line in \kbd{\$EDITOR}
+zrcbindkey '\ee' edit-command-line
+
+## use Ctrl-left-arrow and Ctrl-right-arrow for jumping to word-beginnings on the CL
+zrcbindkey "\e[5C" forward-word
+zrcbindkey "\e[5D" backward-word
+zrcbindkey "\e[1;5C" forward-word
+zrcbindkey "\e[1;5D" backward-word
+## the same for alt-left-arrow and alt-right-arrow
+zrcbindkey '^[[1;3C' forward-word
+zrcbindkey '^[[1;3D' backward-word
+
+#k# search history backward for entry beginning with typed text
+zrcbindkey '^xp' history-beginning-search-backward-end
+#k# search history forward for entry beginning with typed text
+zrcbindkey '^xP' history-beginning-search-forward-end
+#k# search history backward for entry beginning with typed text
+zrcbindkey "\e[5~" history-beginning-search-backward-end # PageUp
+#k# search history forward for entry beginning with typed text
+zrcbindkey "\e[6~" history-beginning-search-forward-end # PageDown
+
+# insert unicode character
+# usage example: 'ctrl-x i' 00A7 'ctrl-x i' will give you an ยง
+# See for example http://unicode.org/charts/ for unicode characters code
+#k# Insert Unicode character
+zrcbindkey '^xi' insert-unicode-char
+
+#m# k Shift-tab Perform backwards menu completion
+if [[ -n "$terminfo[kcbt]" ]]; then
+ zrcbindkey "$terminfo[kcbt]" reverse-menu-complete
+elif [[ -n "$terminfo[cbt]" ]]; then # required for GNU screen
+ zrcbindkey "$terminfo[cbt]" reverse-menu-complete
+fi
+
+#k# Toggle abbreviation expansion on/off
+zrcbindkey '^xA' grml_toggle_abbrev
+zrcbindkey "^x^h" commit-to-history
+
+#k# Kill left-side word or everything up to next slash
+zrcbindkey '\ev' slash-backward-kill-word
+#k# Kill left-side word or everything up to next slash
+zrcbindkey '\e^h' slash-backward-kill-word
+#k# Kill left-side word or everything up to next slash
+zrcbindkey '\e^?' slash-backward-kill-word
+
+# use the new *-pattern-* widgets for incremental history search
+if zrcgotwidget history-incremental-pattern-search-backward; then
+ zrcbindkey '^r' history-incremental-pattern-search-backward
+ zrcbindkey '^s' history-incremental-pattern-search-forward
+fi
+
+# Do history expansion on space:
+zrcbindkey ' ' magic-space
+#k# Trigger menu-complete
+zrcbindkey '\ei' menu-complete # menu completion via esc-i
+#k# Insert a timestamp on the command line (yyyy-mm-dd)
+zrcbindkey '^ed' insert-datestamp
+#k# Insert last typed word
+zrcbindkey "\em" insert-last-typed-word
+#k# A smart shortcut for \kbd{fg<enter>}
+zrcbindkey '^z' grml-zsh-fg
+#k# prepend the current command with "sudo"
+zrcbindkey "^os" sudo-command-line
+#k# jump to after first word (for adding options)
+zrcbindkey '^x1' jump_after_first_word
+#k# complete word from history with menu
+zrcbindkey "^x^x" hist-complete
+
+# autoloading
+
+zrcautoload zmv
+zrcautoload zed
+
+# we don't want to quote/espace URLs on our own...
+# if autoload -U url-quote-magic ; then
+# zle -N self-insert url-quote-magic
+# zstyle ':url-quote-magic:*' url-metas '*?[]^()~#{}='
+# else
+# print 'Notice: no url-quote-magic available :('
+# fi
+alias url-quote='autoload -U url-quote-magic ; zle -N self-insert url-quote-magic'
+
+#m# k ESC-h Call \kbd{run-help} for the 1st word on the command line
+alias run-help >&/dev/null && unalias run-help
+for rh in run-help{,-git,-svk,-svn}; do
+ zrcautoload $rh
+done; unset rh
+
+# command not found handling
+
+(( ${COMMAND_NOT_FOUND} == 1 )) &&
+function command_not_found_handler() {
+ emulate -L zsh
+ if [[ -x ${GRML_ZSH_CNF_HANDLER} ]] ; then
+ ${GRML_ZSH_CNF_HANDLER} $1
+ fi
+ return 1
+}
+