aboutsummaryrefslogtreecommitdiffstats
path: root/.zsh.functions
diff options
context:
space:
mode:
authorDavid Runge <dave@sleepmap.de>2016-05-08 02:59:18 +0200
committerDavid Runge <dave@sleepmap.de>2016-05-08 02:59:18 +0200
commit05db802e973abc999ec4ec4f4e41087c649df027 (patch)
treec76e27b42c0355be3eb339aa4175a700f48d6ff8 /.zsh.functions
parent00c54a6e6f21dca4487041cf8106df23adcd43be (diff)
downloaddotfiles-05db802e973abc999ec4ec4f4e41087c649df027.tar.gz
dotfiles-05db802e973abc999ec4ec4f4e41087c649df027.tar.bz2
dotfiles-05db802e973abc999ec4ec4f4e41087c649df027.tar.xz
dotfiles-05db802e973abc999ec4ec4f4e41087c649df027.zip
.zsh.functions/*: Adding functions from several zprezto modules.
Diffstat (limited to '.zsh.functions')
-rw-r--r--.zsh.functions/01_editor.zsh124
-rw-r--r--.zsh.functions/02_utility.zsh104
-rw-r--r--.zsh.functions/0_terminal.zsh74
3 files changed, 302 insertions, 0 deletions
diff --git a/.zsh.functions/01_editor.zsh b/.zsh.functions/01_editor.zsh
new file mode 100644
index 0000000..1484a92
--- /dev/null
+++ b/.zsh.functions/01_editor.zsh
@@ -0,0 +1,124 @@
+# Exposes information about the Zsh Line Editor via the $editor_info associative
+# array.
+function editor-info {
+ # Clean up previous $editor_info.
+ unset editor_info
+ typeset -gA editor_info
+
+ if [[ "$KEYMAP" == 'vicmd' ]]; then
+ zstyle -s ':editor:info:keymap:alternate' format 'REPLY'
+ editor_info[keymap]="$REPLY"
+ else
+ zstyle -s ':editor:info:keymap:primary' format 'REPLY'
+ editor_info[keymap]="$REPLY"
+
+ if [[ "$ZLE_STATE" == *overwrite* ]]; then
+ zstyle -s ':editor:info:keymap:primary:overwrite' format 'REPLY'
+ editor_info[overwrite]="$REPLY"
+ else
+ zstyle -s ':editor:info:keymap:primary:insert' format 'REPLY'
+ editor_info[overwrite]="$REPLY"
+ fi
+ fi
+
+ unset REPLY
+
+ zle reset-prompt
+ zle -R
+}
+zle -N editor-info
+
+# Updates editor information when the keymap changes.
+function zle-keymap-select {
+ zle editor-info
+}
+zle -N zle-keymap-select
+
+# Enables terminal application mode and updates editor information.
+function zle-line-init {
+ # The terminal must be in application mode when ZLE is active for $terminfo
+ # values to be valid.
+ if (( $+terminfo[smkx] )); then
+ # Enable terminal application mode.
+ echoti smkx
+ fi
+
+ # Update editor information.
+ zle editor-info
+}
+zle -N zle-line-init
+
+# Disables terminal application mode and updates editor information.
+function zle-line-finish {
+ # The terminal must be in application mode when ZLE is active for $terminfo
+ # values to be valid.
+ if (( $+terminfo[rmkx] )); then
+ # Disable terminal application mode.
+ echoti rmkx
+ fi
+
+ # Update editor information.
+ zle editor-info
+}
+zle -N zle-line-finish
+
+# Toggles emacs overwrite mode and updates editor information.
+function overwrite-mode {
+ zle .overwrite-mode
+ zle editor-info
+}
+zle -N overwrite-mode
+
+# Enters vi insert mode and updates editor information.
+function vi-insert {
+ zle .vi-insert
+ zle editor-info
+}
+zle -N vi-insert
+
+# Moves to the first non-blank character then enters vi insert mode and updates
+# editor information.
+function vi-insert-bol {
+ zle .vi-insert-bol
+ zle editor-info
+}
+zle -N vi-insert-bol
+
+# Enters vi replace mode and updates editor information.
+function vi-replace {
+ zle .vi-replace
+ zle editor-info
+}
+zle -N vi-replace
+
+# Expands .... to ../..
+function expand-dot-to-parent-directory-path {
+ if [[ $LBUFFER = *.. ]]; then
+ LBUFFER+='/..'
+ else
+ LBUFFER+='.'
+ fi
+}
+zle -N expand-dot-to-parent-directory-path
+
+# Displays an indicator when completing.
+function expand-or-complete-with-indicator {
+ local indicator
+ zstyle -s ':editor:info:completing' format 'indicator'
+ print -Pn "$indicator"
+ zle expand-or-complete
+ zle redisplay
+}
+zle -N expand-or-complete-with-indicator
+
+# Inserts 'sudo ' at the beginning of the line.
+function prepend-sudo {
+ if [[ "$BUFFER" != su(do|)\ * ]]; then
+ BUFFER="sudo $BUFFER"
+ (( CURSOR += 5 ))
+ fi
+}
+zle -N prepend-sudo
+
+# Reset to default key bindings.
+bindkey -d
diff --git a/.zsh.functions/02_utility.zsh b/.zsh.functions/02_utility.zsh
new file mode 100644
index 0000000..c08ce10
--- /dev/null
+++ b/.zsh.functions/02_utility.zsh
@@ -0,0 +1,104 @@
+# Makes a directory and changes to it.
+function mkdcd {
+ [[ -n "$1" ]] && mkdir -p "$1" && builtin cd "$1"
+}
+
+# Changes to a directory and lists its contents.
+function cdls {
+ builtin cd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Pushes an entry onto the directory stack and lists its contents.
+function pushdls {
+ builtin pushd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Pops an entry off the directory stack and lists its contents.
+function popdls {
+ builtin popd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Prints columns 1 2 3 ... n.
+function slit {
+ awk "{ print ${(j:,:):-\$${^@}} }"
+}
+
+# Finds files and executes a command on them.
+function find-exec {
+ find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
+}
+
+# Displays user owned processes status.
+function psu {
+ ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}"
+}
+
+# Highlights diff output.
+function diff {
+ if (( $+commands[colordiff] )); then
+ command diff --unified "$@" | colordiff --difftype diffu
+ elif (( $+commands[git] )); then
+ git --no-pager diff --color=auto --no-ext-diff --no-index "$@"
+ else
+ command diff --unified "$@"
+ fi
+}
+
+# Highlights make output.
+function make {
+ if (( $+commands[colormake] )); then
+ colormake "$@"
+ else
+ command make "$@"
+ fi
+}
+
+# Highlights wdiff output.
+function wdiff {
+ if (( $+commands[wdiff] )); then
+ command wdiff \
+ --avoid-wraps \
+ --start-delete="$(print -n $FG[red])" \
+ --end-delete="$(print -n $FG[none])" \
+ --start-insert="$(print -n $FG[green])" \
+ --end-insert="$(print -n $FG[none])" \
+ "$@" \
+ | sed 's/^\(@@\( [+-][[:digit:]]*,[[:digit:]]*\)\{2\} @@\)$/;5;6m\10m/g'
+ elif (( $+commands[git] )); then
+ git --no-pager diff --color=auto --no-ext-diff --no-index --color-words "$@"
+ else
+ command wdiff "$@"
+ fi
+}
+
+# Lists the contents of archives.
+function lsarchive {
+ while (( $# > 0 )); do
+ if [[ ! -s "$1" ]]; then
+ print "$0: file not valid: $1" >&2
+ shift
+ continue
+ fi
+ case "$1:l" in
+ (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
+ (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
+ (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
+ && tar --xz -t${verbose:+v}f "$1" \
+ || xzcat "$1" | tar t${verbose:+v}f - ;;
+ (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
+ && tar --lzma -t${verbose:+v}f "$1" \
+ || lzcat "$1" | tar x${verbose:+v}f - ;;
+ (*.tar) tar t${verbose:+v}f "$1" ;;
+ (*.zip) unzip -l${verbose:+v} "$1" ;;
+ (*.rar) unrar &> /dev/null \
+ && unrar ${${verbose:+v}:-l} "$1" \
+ || rar ${${verbose:+v}:-l} "$1" ;;
+ (*.7z) 7za l "$1" ;;
+ (*)
+ print "$0: cannot list: $1" >&2
+ success=1
+ ;;
+ esac
+ shift
+ done
+}
diff --git a/.zsh.functions/0_terminal.zsh b/.zsh.functions/0_terminal.zsh
new file mode 100644
index 0000000..fd2ad16
--- /dev/null
+++ b/.zsh.functions/0_terminal.zsh
@@ -0,0 +1,74 @@
+# Terminal functions
+#
+
+# Sets the terminal or terminal multiplexer window title.
+function set-window-title {
+ local title_format{,ted}
+ zstyle -s ':prezto:module:terminal:window-title' format 'title_format' || title_format="%s"
+ zformat -f title_formatted "$title_format" "s:$argv"
+
+ if [[ "$TERM" == screen* ]]; then
+ title_format="\ek%s\e\\"
+ else
+ title_format="\e]2;%s\a"
+ fi
+
+ printf "$title_format" "${(V%)title_formatted}"
+}
+
+# Sets the terminal tab title.
+function set-tab-title {
+ local title_format{,ted}
+ zstyle -s ':prezto:module:terminal:tab-title' format 'title_format' || title_format="%s"
+ zformat -f title_formatted "$title_format" "s:$argv"
+
+ printf "\e]1;%s\a" ${(V%)title_formatted}
+}
+
+
+# Sets the tab and window titles with a given command.
+function _terminal-set-titles-with-command {
+ emulate -L zsh
+ setopt EXTENDED_GLOB
+
+ # Get the command name that is under job control.
+ if [[ "${2[(w)1]}" == (fg|%*)(\;|) ]]; then
+ # Get the job name, and, if missing, set it to the default %+.
+ local job_name="${${2[(wr)%*(\;|)]}:-%+}"
+
+ # Make a local copy for use in the subshell.
+ local -A jobtexts_from_parent_shell
+ jobtexts_from_parent_shell=(${(kv)jobtexts})
+
+ jobs "$job_name" 2>/dev/null > >(
+ read index discarded
+ # The index is already surrounded by brackets: [1].
+ _terminal-set-titles-with-command "${(e):-\$jobtexts_from_parent_shell$index}"
+ )
+ else
+ # Set the command name, or in the case of sudo or ssh, the next command.
+ local cmd="${${2[(wr)^(*=*|sudo|ssh|-*)]}:t}"
+ local truncated_cmd="${cmd/(#m)?(#c15,)/${MATCH[1,12]}...}"
+ unset MATCH
+
+ set-window-title "$cmd"
+ set-tab-title "$truncated_cmd"
+ fi
+}
+
+
+# Sets the tab and window titles with a given path.
+function _terminal-set-titles-with-path {
+ emulate -L zsh
+ setopt EXTENDED_GLOB
+
+ local absolute_path="${${1:a}:-$PWD}"
+ local abbreviated_path="${absolute_path/#$HOME/~}"
+ local truncated_path="${abbreviated_path/(#m)?(#c15,)/...${MATCH[-12,-1]}}"
+ unset MATCH
+
+ set-window-title "$abbreviated_path"
+ set-tab-title "$truncated_path"
+}
+
+