From baa2119b5f2fba12640d0d580ab6a57aea9f1667 Mon Sep 17 00:00:00 2001 From: Nikolas Weger Date: Wed, 23 Aug 2017 10:58:34 +0200 Subject: [PATCH] changed folder layout --- cfg/zsh/cfg.d/export.zsh | 24 + cfg/zsh/cfg.d/ohmy.zsh | 3 + cfg/zsh/cfg.d/source.d/00-highlighting.zsh | 275 +++++++ cfg/zsh/cfg.d/source.d/01-autosuggest.zsh | 692 ++++++++++++++++++ cfg/zsh/cfg.d/source.d/alias-os.zsh | 155 ++++ cfg/zsh/cfg.d/source.d/alias-systemd.zsh | 8 + cfg/zsh/cfg.d/source.d/function-cnf.zsh | 2 + cfg/zsh/cfg.d/source.d/function-dircolors.zsh | 1 + cfg/zsh/cfg.d/source.d/function-extract.zsh | 72 ++ cfg/zsh/cfg.d/source.d/function-fzf.zsh | 2 + cfg/zsh/cfg.d/source.d/function-git.zsh | 221 ++++++ cfg/zsh/cfg.d/source.d/function-gitignore.zsh | 53 ++ cfg/zsh/cfg.d/source.d/function-tmux.zsh | 99 +++ cfg/zsh/cfg.d/source.d/function-workfix.zsh | 3 + cfg/zsh/cfg.d/source.zsh | 9 + 15 files changed, 1619 insertions(+) create mode 100644 cfg/zsh/cfg.d/export.zsh create mode 100644 cfg/zsh/cfg.d/ohmy.zsh create mode 100644 cfg/zsh/cfg.d/source.d/00-highlighting.zsh create mode 100644 cfg/zsh/cfg.d/source.d/01-autosuggest.zsh create mode 100644 cfg/zsh/cfg.d/source.d/alias-os.zsh create mode 100644 cfg/zsh/cfg.d/source.d/alias-systemd.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-cnf.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-dircolors.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-extract.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-fzf.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-git.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-gitignore.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-tmux.zsh create mode 100644 cfg/zsh/cfg.d/source.d/function-workfix.zsh create mode 100644 cfg/zsh/cfg.d/source.zsh diff --git a/cfg/zsh/cfg.d/export.zsh b/cfg/zsh/cfg.d/export.zsh new file mode 100644 index 0000000..cc720aa --- /dev/null +++ b/cfg/zsh/cfg.d/export.zsh @@ -0,0 +1,24 @@ +# For Dotfiles +export DOTFLS="${HOME}/dotfiles" +export DOTLIB="${DOTFLS}/lib" +export DOTBIN="${DOTFLS}/bin" +export DOTCFG="${DOTFLS}/cfg" +export DOTCFGD="${DOTCFG}/zsh/cfg.d" + +# Path +path+=(${DOTBIN}) +path+=(/opt/android-sdk/build-tools/26.0.1) +path+=(${HOME}/.gem/ruby/2.4.0/bin) +path+=(${DOTLIB}/welder/bin) +path+=(${DOTLIB}/basher/bin) +export PATH + +# Editor Setting +export EDITOR='vim' + +# Completions Paths +fpath+=(${DOTLIB}/completions/src) +fpath+=(${DOTLIB}/local) + +# Reload all Prompts +autoload -U promptinit && promptinit diff --git a/cfg/zsh/cfg.d/ohmy.zsh b/cfg/zsh/cfg.d/ohmy.zsh new file mode 100644 index 0000000..784620a --- /dev/null +++ b/cfg/zsh/cfg.d/ohmy.zsh @@ -0,0 +1,3 @@ +export ZSH="${DOTLIB}/ohmyzsh" +DISABLE_AUTO_UPDATE="true" +source ${ZSH}/oh-my-zsh.sh diff --git a/cfg/zsh/cfg.d/source.d/00-highlighting.zsh b/cfg/zsh/cfg.d/source.d/00-highlighting.zsh new file mode 100644 index 0000000..4faa24a --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/00-highlighting.zsh @@ -0,0 +1,275 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2016 zsh-syntax-highlighting contributors +# Copyright (c) 2017 Sebastian Gniazdowski (modifications) +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + + +# Set $ZERO to the expected value, regardless of functionargzero. +typeset -g ZERO=${(%):-%N} + +# Invokes each highlighter that needs updating. +# This function is supposed to be called whenever the ZLE state changes. +_zsh_highlight() +{ + # Store the previous command return code to restore it whatever happens. + local ret=$? + + # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains. + # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'. + if [[ $WIDGET == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then + region_highlight=() + return $ret + fi + + setopt localoptions warncreateglobal noksharrays + local REPLY # don't leak $REPLY into global scope + local -a reply + + # Do not highlight if there are more than 300 chars in the buffer. It's most + # likely a pasted command or a huge list of files in that case.. + [[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret + + # Do not highlight if there are pending inputs (copy/paste). + [[ $PENDING -gt 0 ]] && return $ret + + # Reset region highlight to build it from scratch + # may need to remove path_prefix highlighting when the line ends + if [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_buffer_modified; then + -fast-highlight-init + -fast-highlight-process && region_highlight=( $reply ) || region_highlight=() + fi + + { + local cache_place + local -a region_highlight_copy + + # Re-apply zle_highlight settings + + # region + if (( REGION_ACTIVE == 1 )); then + _zsh_highlight_apply_zle_highlight region standout "$MARK" "$CURSOR" + elif (( REGION_ACTIVE == 2 )); then + () { + local needle=$'\n' + integer min max + if (( MARK > CURSOR )) ; then + min=$CURSOR max=$MARK + else + min=$MARK max=$CURSOR + fi + (( min = ${${BUFFER[1,$min]}[(I)$needle]} )) + (( max += ${${BUFFER:($max-1)}[(i)$needle]} - 1 )) + _zsh_highlight_apply_zle_highlight region standout "$min" "$max" + } + fi + + # yank / paste (zsh-5.1.1 and newer) + (( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END" + + # isearch + (( $+ISEARCHMATCH_ACTIVE )) && (( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END" + + # suffix + (( $+SUFFIX_ACTIVE )) && (( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END" + + return $ret + + } always { + typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER="$BUFFER" + typeset -g _ZSH_HIGHLIGHT_PRIOR_RACTIVE="$REGION_ACTIVE" + typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR + } +} + +# Apply highlighting based on entries in the zle_highlight array. +# This function takes four arguments: +# 1. The exact entry (no patterns) in the zle_highlight array: +# region, paste, isearch, or suffix +# 2. The default highlighting that should be applied if the entry is unset +# 3. and 4. Two integer values describing the beginning and end of the +# range. The order does not matter. +_zsh_highlight_apply_zle_highlight() { + local entry="$1" default="$2" + integer first="$3" second="$4" + + # read the relevant entry from zle_highlight + local region="${zle_highlight[(r)${entry}:*]}" + + if [[ -z "$region" ]]; then + # entry not specified at all, use default value + region=$default + else + # strip prefix + region="${region#${entry}:}" + + # no highlighting when set to the empty string or to 'none' + if [[ -z "$region" ]] || [[ "$region" == none ]]; then + return + fi + fi + + integer start end + if (( first < second )); then + start=$first end=$second + else + start=$second end=$first + fi + region_highlight+=("$start $end $region") +} + + +# ------------------------------------------------------------------------------------------------- +# API/utility functions for highlighters +# ------------------------------------------------------------------------------------------------- + +# Whether the command line buffer has been modified or not. +# +# Returns 0 if the buffer has changed since _zsh_highlight was last called. +_zsh_highlight_buffer_modified() +{ + [[ "${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-}" != "$BUFFER" ]] || [[ "$REGION_ACTIVE" != "$_ZSH_HIGHLIGHT_PRIOR_RACTIVE" ]] || { _zsh_highlight_cursor_moved && [[ "$REGION_ACTIVE" = 1 || "$REGION_ACTIVE" = 2 ]] } +} + +# Whether the cursor has moved or not. +# +# Returns 0 if the cursor has moved since _zsh_highlight was last called. +_zsh_highlight_cursor_moved() +{ + [[ -n $CURSOR ]] && [[ -n ${_ZSH_HIGHLIGHT_PRIOR_CURSOR-} ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR)) +} + +# ------------------------------------------------------------------------------------------------- +# Setup functions +# ------------------------------------------------------------------------------------------------- + +# Helper for _zsh_highlight_bind_widgets +# $1 is name of widget to call +_zsh_highlight_call_widget() +{ + builtin zle "$@" && _zsh_highlight +} + +# Rebind all ZLE widgets to make them invoke _zsh_highlights. +_zsh_highlight_bind_widgets() +{ + setopt localoptions noksharrays + typeset -F SECONDS + local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once + + # Load ZSH module zsh/zleparameter, needed to override user defined widgets. + zmodload zsh/zleparameter 2>/dev/null || { + print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' + return 1 + } + + # Override ZLE widgets to make them invoke _zsh_highlight. + local -U widgets_to_bind + widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank)}) + + # Always wrap special zle-line-finish widget. This is needed to decide if the + # current line ends and special highlighting logic needs to be applied. + # E.g. remove cursor imprint, don't highlight partial paths, ... + widgets_to_bind+=(zle-line-finish) + + # Always wrap special zle-isearch-update widget to be notified of updates in isearch. + # This is needed because we need to disable highlighting in that case. + widgets_to_bind+=(zle-isearch-update) + + local cur_widget + for cur_widget in $widgets_to_bind; do + case $widgets[$cur_widget] in + + # Already rebound event: do nothing. + user:_zsh_highlight_widget_*);; + + # The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function + # definition time is used. + # + # We can't use ${0/_zsh_highlight_widget_} because these widgets are always invoked with + # NO_function_argzero, regardless of the option's setting here. + + # User defined widget: override and rebind old one with prefix "orig-". + user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:} + eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; + + # Completion widget: override and rebind old one with prefix "orig-". + completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]} + eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; + + # Builtin widget: override and make it call the builtin ".widget". + builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; + + # Incomplete or nonexistent widget: Bind to z-sy-h directly. + *) + if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then + _zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight } + zle -N $cur_widget _zsh_highlight_widget_$cur_widget + else + # Default: unhandled case. + print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}" + fi + esac + done +} + +# ------------------------------------------------------------------------------------------------- +# Setup +# ------------------------------------------------------------------------------------------------- + +# Try binding widgets. +_zsh_highlight_bind_widgets || { + print -r -- >&2 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.' + return 1 +} + +# Reset scratch variables when commandline is done. +_zsh_highlight_preexec_hook() +{ + typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER= + typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=0 +} + +autoload -U add-zsh-hook +add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || { + print -r -- >&2 'zsh-syntax-highlighting: failed loading add-zsh-hook.' +} + +ZSH_HIGHLIGHT_MAXLENGTH=10000 + +# Load zsh/parameter module if available +zmodload zsh/parameter 2>/dev/null + +autoload -U is-at-least +source "${DOTLIB}/highlight" + +[[ "${+termcap[Co]}" = 1 && "${termcap[Co]}" = "256" ]] && FAST_HIGHLIGHT_STYLES[variable]="fg=112" + +-fast-highlight-fill-option-variables diff --git a/cfg/zsh/cfg.d/source.d/01-autosuggest.zsh b/cfg/zsh/cfg.d/source.d/01-autosuggest.zsh new file mode 100644 index 0000000..0bd96a3 --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/01-autosuggest.zsh @@ -0,0 +1,692 @@ +# Fish-like fast/unobtrusive autosuggestions for zsh. +# https://github.com/zsh-users/zsh-autosuggestions +# v0.4.0 +# Copyright (c) 2013 Thiago de Arruda +# Copyright (c) 2016-2017 Eric Freese +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. + +#--------------------------------------------------------------------# +# Setup # +#--------------------------------------------------------------------# + +# Precmd hooks for initializing the library and starting pty's +autoload -Uz add-zsh-hook + +# Asynchronous suggestions are generated in a pty +zmodload zsh/zpty + +#--------------------------------------------------------------------# +# Global Configuration Variables # +#--------------------------------------------------------------------# + +# Color to use when highlighting suggestion +# Uses format of `region_highlight` +# More info: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets +ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' + +# Prefix to use when saving original versions of bound widgets +ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig- + +ZSH_AUTOSUGGEST_STRATEGY=default + +# Widgets that clear the suggestion +ZSH_AUTOSUGGEST_CLEAR_WIDGETS=( + history-search-forward + history-search-backward + history-beginning-search-forward + history-beginning-search-backward + history-substring-search-up + history-substring-search-down + up-line-or-history + down-line-or-history + accept-line +) + +# Widgets that accept the entire suggestion +ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=( + forward-char + end-of-line + vi-forward-char + vi-end-of-line + vi-add-eol +) + +# Widgets that accept the entire suggestion and execute it +ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=( +) + +# Widgets that accept the suggestion as far as the cursor moves +ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=( + forward-word + vi-forward-word + vi-forward-word-end + vi-forward-blank-word + vi-forward-blank-word-end +) + +# Widgets that should be ignored (globbing supported but must be escaped) +ZSH_AUTOSUGGEST_IGNORE_WIDGETS=( + orig-\* + beep + run-help + set-local-history + which-command + yank +) + +# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound. +ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE= + +# Pty name for calculating autosuggestions asynchronously +ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty + +#--------------------------------------------------------------------# +# Utility Functions # +#--------------------------------------------------------------------# + +_zsh_autosuggest_escape_command() { + setopt localoptions EXTENDED_GLOB + + # Escape special chars in the string (requires EXTENDED_GLOB) + echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}" +} + +#--------------------------------------------------------------------# +# Feature Detection # +#--------------------------------------------------------------------# + +_zsh_autosuggest_feature_detect_zpty_returns_fd() { + typeset -g _ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD + typeset -h REPLY + + zpty zsh_autosuggest_feature_detect '{ zshexit() { kill -KILL $$; sleep 1 } }' + + if (( REPLY )); then + _ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD=1 + else + _ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD=0 + fi + + zpty -d zsh_autosuggest_feature_detect +} + +#--------------------------------------------------------------------# +# Widget Helpers # +#--------------------------------------------------------------------# + +_zsh_autosuggest_incr_bind_count() { + if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then + ((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]++)) + else + _ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=1 + fi + + bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1] +} + +_zsh_autosuggest_get_bind_count() { + if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then + bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1] + else + bind_count=0 + fi +} + +# Bind a single widget to an autosuggest widget, saving a reference to the original widget +_zsh_autosuggest_bind_widget() { + typeset -gA _ZSH_AUTOSUGGEST_BIND_COUNTS + + local widget=$1 + local autosuggest_action=$2 + local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX + + local -i bind_count + + # Save a reference to the original widget + case $widgets[$widget] in + # Already bound + user:_zsh_autosuggest_(bound|orig)_*);; + + # User-defined widget + user:*) + _zsh_autosuggest_incr_bind_count $widget + zle -N $prefix${bind_count}-$widget ${widgets[$widget]#*:} + ;; + + # Built-in widget + builtin) + _zsh_autosuggest_incr_bind_count $widget + eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }" + zle -N $prefix${bind_count}-$widget _zsh_autosuggest_orig_$widget + ;; + + # Completion widget + completion:*) + _zsh_autosuggest_incr_bind_count $widget + eval "zle -C $prefix${bind_count}-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}" + ;; + esac + + _zsh_autosuggest_get_bind_count $widget + + # Pass the original widget's name explicitly into the autosuggest + # function. Use this passed in widget name to call the original + # widget instead of relying on the $WIDGET variable being set + # correctly. $WIDGET cannot be trusted because other plugins call + # zle without the `-w` flag (e.g. `zle self-insert` instead of + # `zle self-insert -w`). + eval "_zsh_autosuggest_bound_${bind_count}_${(q)widget}() { + _zsh_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@ + }" + + # Create the bound widget + zle -N $widget _zsh_autosuggest_bound_${bind_count}_$widget +} + +# Map all configured widgets to the right autosuggest widgets +_zsh_autosuggest_bind_widgets() { + local widget + local ignore_widgets + + ignore_widgets=( + .\* + _\* + zle-\* + autosuggest-\* + $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\* + $ZSH_AUTOSUGGEST_IGNORE_WIDGETS + ) + + # Find every widget we might want to bind and bind it appropriately + for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do + if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then + _zsh_autosuggest_bind_widget $widget clear + elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then + _zsh_autosuggest_bind_widget $widget accept + elif [ ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]; then + _zsh_autosuggest_bind_widget $widget execute + elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then + _zsh_autosuggest_bind_widget $widget partial_accept + else + # Assume any unspecified widget might modify the buffer + _zsh_autosuggest_bind_widget $widget modify + fi + done +} + +# Given the name of an original widget and args, invoke it, if it exists +_zsh_autosuggest_invoke_original_widget() { + # Do nothing unless called with at least one arg + [ $# -gt 0 ] || return + + local original_widget_name="$1" + + shift + + if [ $widgets[$original_widget_name] ]; then + zle $original_widget_name -- $@ + fi +} + +#--------------------------------------------------------------------# +# Highlighting # +#--------------------------------------------------------------------# + +# If there was a highlight, remove it +_zsh_autosuggest_highlight_reset() { + typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT + + if [ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]; then + region_highlight=("${(@)region_highlight:#$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT}") + unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT + fi +} + +# If there's a suggestion, highlight it +_zsh_autosuggest_highlight_apply() { + typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT + + if [ $#POSTDISPLAY -gt 0 ]; then + _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" + region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT") + else + unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT + fi +} + +#--------------------------------------------------------------------# +# Autosuggest Widget Implementations # +#--------------------------------------------------------------------# + +# Disable suggestions +_zsh_autosuggest_disable() { + typeset -g _ZSH_AUTOSUGGEST_DISABLED + _zsh_autosuggest_clear +} + +# Enable suggestions +_zsh_autosuggest_enable() { + unset _ZSH_AUTOSUGGEST_DISABLED + + if [ $#BUFFER -gt 0 ]; then + _zsh_autosuggest_fetch + fi +} + +# Toggle suggestions (enable/disable) +_zsh_autosuggest_toggle() { + if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then + _zsh_autosuggest_enable + else + _zsh_autosuggest_disable + fi +} + +# Clear the suggestion +_zsh_autosuggest_clear() { + # Remove the suggestion + unset POSTDISPLAY + + _zsh_autosuggest_invoke_original_widget $@ +} + +# Modify the buffer and get a new suggestion +_zsh_autosuggest_modify() { + local -i retval + + # Only added to zsh very recently + local -i KEYS_QUEUED_COUNT + + # Save the contents of the buffer/postdisplay + local orig_buffer="$BUFFER" + local orig_postdisplay="$POSTDISPLAY" + + # Clear suggestion while waiting for next one + unset POSTDISPLAY + + # Original widget may modify the buffer + _zsh_autosuggest_invoke_original_widget $@ + retval=$? + + # Don't fetch a new suggestion if there's more input to be read immediately + if [[ $PENDING > 0 ]] || [[ $KEYS_QUEUED_COUNT > 0 ]]; then + return $retval + fi + + # Optimize if manually typing in the suggestion + if [ $#BUFFER -gt $#orig_buffer ]; then + local added=${BUFFER#$orig_buffer} + + # If the string added matches the beginning of the postdisplay + if [ "$added" = "${orig_postdisplay:0:$#added}" ]; then + POSTDISPLAY="${orig_postdisplay:$#added}" + return $retval + fi + fi + + # Don't fetch a new suggestion if the buffer hasn't changed + if [ "$BUFFER" = "$orig_buffer" ]; then + POSTDISPLAY="$orig_postdisplay" + return $retval + fi + + # Bail out if suggestions are disabled + if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then + return $? + fi + + # Get a new suggestion if the buffer is not empty after modification + if [ $#BUFFER -gt 0 ]; then + if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then + _zsh_autosuggest_fetch + fi + fi + + return $retval +} + +# Fetch a new suggestion based on what's currently in the buffer +_zsh_autosuggest_fetch() { + if zpty -t "$ZSH_AUTOSUGGEST_ASYNC_PTY_NAME" &>/dev/null; then + _zsh_autosuggest_async_request "$BUFFER" + else + local suggestion + _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$BUFFER" + _zsh_autosuggest_suggest "$suggestion" + fi +} + +# Offer a suggestion +_zsh_autosuggest_suggest() { + local suggestion="$1" + + if [ -n "$suggestion" ] && [ $#BUFFER -gt 0 ]; then + POSTDISPLAY="${suggestion#$BUFFER}" + else + unset POSTDISPLAY + fi +} + +# Accept the entire suggestion +_zsh_autosuggest_accept() { + local -i max_cursor_pos=$#BUFFER + + # When vicmd keymap is active, the cursor can't move all the way + # to the end of the buffer + if [ "$KEYMAP" = "vicmd" ]; then + max_cursor_pos=$((max_cursor_pos - 1)) + fi + + # Only accept if the cursor is at the end of the buffer + if [ $CURSOR -eq $max_cursor_pos ]; then + # Add the suggestion to the buffer + BUFFER="$BUFFER$POSTDISPLAY" + + # Remove the suggestion + unset POSTDISPLAY + + # Move the cursor to the end of the buffer + CURSOR=${#BUFFER} + fi + + _zsh_autosuggest_invoke_original_widget $@ +} + +# Accept the entire suggestion and execute it +_zsh_autosuggest_execute() { + # Add the suggestion to the buffer + BUFFER="$BUFFER$POSTDISPLAY" + + # Remove the suggestion + unset POSTDISPLAY + + # Call the original `accept-line` to handle syntax highlighting or + # other potential custom behavior + _zsh_autosuggest_invoke_original_widget "accept-line" +} + +# Partially accept the suggestion +_zsh_autosuggest_partial_accept() { + local -i retval + + # Save the contents of the buffer so we can restore later if needed + local original_buffer="$BUFFER" + + # Temporarily accept the suggestion. + BUFFER="$BUFFER$POSTDISPLAY" + + # Original widget moves the cursor + _zsh_autosuggest_invoke_original_widget $@ + retval=$? + + # If we've moved past the end of the original buffer + if [ $CURSOR -gt $#original_buffer ]; then + # Set POSTDISPLAY to text right of the cursor + POSTDISPLAY="$RBUFFER" + + # Clip the buffer at the cursor + BUFFER="$LBUFFER" + else + # Restore the original buffer + BUFFER="$original_buffer" + fi + + return $retval +} + +for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do + eval "_zsh_autosuggest_widget_$action() { + local -i retval + + _zsh_autosuggest_highlight_reset + + _zsh_autosuggest_$action \$@ + retval=\$? + + _zsh_autosuggest_highlight_apply + + zle -R + + return \$retval + }" +done + +zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch +zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest +zle -N autosuggest-accept _zsh_autosuggest_widget_accept +zle -N autosuggest-clear _zsh_autosuggest_widget_clear +zle -N autosuggest-execute _zsh_autosuggest_widget_execute +zle -N autosuggest-enable _zsh_autosuggest_widget_enable +zle -N autosuggest-disable _zsh_autosuggest_widget_disable +zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle + +#--------------------------------------------------------------------# +# Default Suggestion Strategy # +#--------------------------------------------------------------------# +# Suggests the most recent history item that matches the given +# prefix. +# + +_zsh_autosuggest_strategy_default() { + # Reset options to defaults and enable LOCAL_OPTIONS + emulate -L zsh + + # Enable globbing flags so that we can use (#m) + setopt EXTENDED_GLOB + + # Escape backslashes and all of the glob operators so we can use + # this string as a pattern to search the $history associative array. + # - (#m) globbing flag enables setting references for match data + local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" + + # Get the history items that match + # - (r) subscript flag makes the pattern match on values + suggestion="${history[(r)$prefix*]}" + +} + +#--------------------------------------------------------------------# +# Match Previous Command Suggestion Strategy # +#--------------------------------------------------------------------# +# Suggests the most recent history item that matches the given +# prefix and whose preceding history item also matches the most +# recently executed command. +# +# For example, suppose your history has the following entries: +# - pwd +# - ls foo +# - ls bar +# - pwd +# +# Given the history list above, when you type 'ls', the suggestion +# will be 'ls foo' rather than 'ls bar' because your most recently +# executed command (pwd) was previously followed by 'ls foo'. +# +# Note that this strategy won't work as expected with ZSH options that don't +# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or +# `HIST_EXPIRE_DUPS_FIRST`. + +_zsh_autosuggest_strategy_match_prev_cmd() { + local prefix="${1//(#m)[\\()\[\]|*?~]/\\$MATCH}" + + # Get all history event numbers that correspond to history + # entries that match pattern $prefix* + local history_match_keys + history_match_keys=(${(k)history[(R)$prefix*]}) + + # By default we use the first history number (most recent history entry) + local histkey="${history_match_keys[1]}" + + # Get the previously executed command + local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" + + # Iterate up to the first 200 history event numbers that match $prefix + for key in "${(@)history_match_keys[1,200]}"; do + # Stop if we ran out of history + [[ $key -gt 1 ]] || break + + # See if the history entry preceding the suggestion matches the + # previous command, and use it if it does + if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then + histkey="$key" + break + fi + done + + # Give back the matched history entry + suggestion="$history[$histkey]" +} + +#--------------------------------------------------------------------# +# Async # +#--------------------------------------------------------------------# + +# Zpty process is spawned running this function +_zsh_autosuggest_async_server() { + emulate -R zsh + + # There is a bug in zpty module (fixed in zsh/master) by which a + # zpty that exits will kill all zpty processes that were forked + # before it. Here we set up a zsh exit hook to SIGKILL the zpty + # process immediately, before it has a chance to kill any other + # zpty processes. + zshexit() { + kill -KILL $$ + sleep 1 # Block for long enough for the signal to come through + } + + # Output only newlines (not carriage return + newline) + stty -onlcr + + # Silence any error messages + exec 2>/dev/null + + local strategy=$1 + local last_pid + + while IFS='' read -r -d $'\0' query; do + # Kill last bg process + kill -KILL $last_pid &>/dev/null + + # Run suggestion search in the background + ( + local suggestion + _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$query" + echo -n -E "$suggestion"$'\0' + ) & + + last_pid=$! + done +} + +_zsh_autosuggest_async_request() { + # Write the query to the zpty process to fetch a suggestion + zpty -w -n $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "${1}"$'\0' +} + +# Called when new data is ready to be read from the pty +# First arg will be fd ready for reading +# Second arg will be passed in case of error +_zsh_autosuggest_async_response() { + setopt LOCAL_OPTIONS EXTENDED_GLOB + + local suggestion + + zpty -rt $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME suggestion '*'$'\0' 2>/dev/null + zle autosuggest-suggest -- "${suggestion%%$'\0'##}" +} + +_zsh_autosuggest_async_pty_create() { + # With newer versions of zsh, REPLY stores the fd to read from + typeset -h REPLY + + # If we won't get a fd back from zpty, try to guess it + if [ $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD -eq 0 ]; then + integer -l zptyfd + exec {zptyfd}>&1 # Open a new file descriptor (above 10). + exec {zptyfd}>&- # Close it so it's free to be used by zpty. + fi + + # Fork a zpty process running the server function + zpty -b $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "_zsh_autosuggest_async_server _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" + + # Store the fd so we can remove the handler later + if (( REPLY )); then + _ZSH_AUTOSUGGEST_PTY_FD=$REPLY + else + _ZSH_AUTOSUGGEST_PTY_FD=$zptyfd + fi + + # Set up input handler from the zpty + zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_response +} + +_zsh_autosuggest_async_pty_destroy() { + if zpty -t $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null; then + # Remove the input handler + zle -F $_ZSH_AUTOSUGGEST_PTY_FD &>/dev/null + + # Destroy the zpty + zpty -d $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null + fi +} + +_zsh_autosuggest_async_pty_recreate() { + _zsh_autosuggest_async_pty_destroy + _zsh_autosuggest_async_pty_create +} + +_zsh_autosuggest_async_start() { + typeset -g _ZSH_AUTOSUGGEST_PTY_FD + + _zsh_autosuggest_feature_detect_zpty_returns_fd + _zsh_autosuggest_async_pty_recreate + + # We recreate the pty to get a fresh list of history events + add-zsh-hook precmd _zsh_autosuggest_async_pty_recreate +} + +#--------------------------------------------------------------------# +# Start # +#--------------------------------------------------------------------# + +# Start the autosuggestion widgets +_zsh_autosuggest_start() { + add-zsh-hook -d precmd _zsh_autosuggest_start + + _zsh_autosuggest_bind_widgets + + # Re-bind widgets on every precmd to ensure we wrap other wrappers. + # Specifically, highlighting breaks if our widgets are wrapped by + # zsh-syntax-highlighting widgets. This also allows modifications + # to the widget list variables to take effect on the next precmd. + add-zsh-hook precmd _zsh_autosuggest_bind_widgets + + if [ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]; then + _zsh_autosuggest_async_start + fi +} + +# Start the autosuggestion widgets on the next precmd +add-zsh-hook precmd _zsh_autosuggest_start + diff --git a/cfg/zsh/cfg.d/source.d/alias-os.zsh b/cfg/zsh/cfg.d/source.d/alias-os.zsh new file mode 100644 index 0000000..e8564bf --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/alias-os.zsh @@ -0,0 +1,155 @@ +# Aliases for OS +if type pacman &>/dev/null; then + if (( $+commands[yaourt] )); then + alias yaconf='yaourt -C' + alias yaupg='yaourt -Syua' + alias yasu='yaourt -Syua --noconfirm' + alias yain='yaourt -S' + alias yains='yaourt -U' + alias yare='yaourt -R' + alias yarem='yaourt -Rns' + alias yarep='yaourt -Si' + alias yareps='yaourt -Ss' + alias yaloc='yaourt -Qi' + alias yalocs='yaourt -Qs' + alias yalst='yaourt -Qe' + alias yaorph='yaourt -Qtd' + alias yainsd='yaourt -S --asdeps' + alias yamir='yaourt -Syy' + alias yaupd='yaourt -Sy' + fi + + alias pacupg='sudo pacman -Syu' + alias pacin='sudo pacman -S' + alias pacins='sudo pacman -U' + alias pacre='sudo pacman -R' + alias pacrem='sudo pacman -Rns' + alias pacrep='pacman -Si' + alias pacreps='pacman -Ss' + alias pacloc='pacman -Qi' + alias paclocs='pacman -Qs' + alias pacinsd='sudo pacman -S --asdeps' + alias pacmir='sudo pacman -Syy' + alias paclsorphans='sudo pacman -Qdt' + alias pacrmorphans='sudo pacman -Rs $(pacman -Qtdq)' + alias pacfileupg='sudo pacman -Fy' + alias pacfiles='pacman tFs' + alias pacupd='sudo pacman -Sy' + + paclist() { + LC_ALL=C pacman -Qei $(pacman -Qu | cut -d " " -f 1) | \ + awk 'BEGIN {FS=":"} /^Name/{printf("\033[1;36m%s\033[1;37m", $2)} /^Description/{print $2}' + } + + pacdisowned() { + emulate -L zsh + + tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$ + db=$tmp/db + fs=$tmp/fs + + mkdir "$tmp" + trap 'rm -rf "$tmp"' EXIT + + pacman -Qlq | sort -u > "$db" + + find /bin /etc /lib /sbin /usr ! -name lost+found \( -type d -printf '%p/\n' -o -print \) | sort > "$fs" + + comm -23 "$fs" "$db" + } + + pacmanallkeys() { + emulate -L zsh + curl -s https://www.archlinux.org/people/{developers,trustedusers}/ | \ + awk -F\" '(/pgp.mit.edu/) { sub(/.*search=0x/,""); print $1}' | xargs sudo pacman-key --recv-keys + } + + pacmansignkeys() { + emulate -L zsh + for key in $*; do + sudo pacman-key --recv-keys $key + sudo pacman-key --lsign-key $key + printf 'trust\n3\n' | sudo gpg --homedir /etc/pacman.d/gnupg --no-permission-warning --command-fd 0 --edit-key $key + done + } +fi + +if type apt-get &>/dev/null; then + apt_pref='apt-get' + apt_upgr='upgrade' + + if [[ -e $( which -p sudo 2>&1 ) ]]; then use_sudo=1; fi + + alias age='apt-get' + alias acs="apt-cache search" + alias afs='apt-file search --regexp' + alias asrc='apt-get source' + alias app='apt-cache policy' + + alias aac='sudo apt-get autoclean' + alias abd='sudo apt-get build-dep' + alias ac='sudo apt-get clean' + alias ad='sudo apt-get update' + alias adg='sudo apt-get update && sudo apt-get upgrade' + alias adu='sudo apt-get update && sudo apt-get dist-upgrade' + alias afu='sudo apt-file update' + alias ag='sudo apt-get upgrade' + alias ai='sudo apt-get install' + alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | "' xargs sudo apt-get install' + alias ap='sudo apt-get purge' + alias ar='sudo apt-get remove' + alias ads='sudo apt-get dselect-upgrade' + alias dia='sudo dpkg -i ./*.deb' + alias di='sudo dpkg -i' + + apt_pref_compdef() { + local f fb + f="_apt_pref_${2}" + + eval "function ${f}() { + shift words; + service=\"apt-get\"; + words=(\"apt-get\" '$2' \$words); + ((CURRENT++)) + _apt + }" + compdef "$f" "$1" + } + + apt_pref_compdef aac "autoclean" + apt_pref_compdef abd "build-dep" + apt_pref_compdef ac "clean" + apt_pref_compdef ad "update" + apt_pref_compdef afu "update" + apt_pref_compdef ag "upgrade" + apt_pref_compdef ai "install" + apt_pref_compdef ail "install" + apt_pref_compdef ap "purge" + apt_pref_compdef ar "remove" + apt_pref_compdef ads "dselect-upgrade" + + apt-history () { + case "$1" in + install) + zgrep --no-filename 'install ' $(ls -rt /var/log/dpkg*) + ;; + upgrade|remove) + zgrep --no-filename $1 $(ls -rt /var/log/dpkg*) + ;; + rollback) + zgrep --no-filename upgrade $(ls -rt /var/log/dpkg*) | grep "$2" -A10000000 | grep "$3" -B10000000 | awk '{print $4"="$5}' + ;; + list) + zgrep --no-filename '' $(ls -rt /var/log/dpkg*) + ;; + *) + echo "Parameters:" + echo " install - Lists all packages that have been installed." + echo " upgrade - Lists all packages that have been upgraded." + echo " remove - Lists all packages that have been removed." + echo " rollback - Lists rollback information." + echo " list - Lists all contents of dpkg logs." + ;; + esac + } +fi diff --git a/cfg/zsh/cfg.d/source.d/alias-systemd.zsh b/cfg/zsh/cfg.d/source.d/alias-systemd.zsh new file mode 100644 index 0000000..ed22bc5 --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/alias-systemd.zsh @@ -0,0 +1,8 @@ +cmds=(status show start stop reload restart enable disable daemon-reload) + +for cmd in $cmds; do + alias sc-$cmd="sudo systemctl $cmd" +done + +alias sc-enablenow="sc-enable --now" +alias sc-disablenow="sc-disable --now" diff --git a/cfg/zsh/cfg.d/source.d/function-cnf.zsh b/cfg/zsh/cfg.d/source.d/function-cnf.zsh new file mode 100644 index 0000000..2a1d84d --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-cnf.zsh @@ -0,0 +1,2 @@ +[[ -e /etc/zsh_command_not_found ]] && source /etc/zsh_command_not_found +[[ -e /usr/share/doc/pkgfile/command-not-found.zsh ]] && source /usr/share/doc/pkgfile/command-not-found.zsh diff --git a/cfg/zsh/cfg.d/source.d/function-dircolors.zsh b/cfg/zsh/cfg.d/source.d/function-dircolors.zsh new file mode 100644 index 0000000..749c30d --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-dircolors.zsh @@ -0,0 +1 @@ +eval `dircolors ${DOTCFG}/dir/cfg` diff --git a/cfg/zsh/cfg.d/source.d/function-extract.zsh b/cfg/zsh/cfg.d/source.d/function-extract.zsh new file mode 100644 index 0000000..87d2023 --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-extract.zsh @@ -0,0 +1,72 @@ +alias x=extract + +extract() { + local remove_archive + local success + local extract_dir + + if (( $# == 0 )); then + cat <<-'EOF' >&2 + Usage: extract [-option] [file ...] + + Options: + -r, --remove Remove archive. + EOF + fi + + remove_archive=1 + if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then + remove_archive=0 + shift + fi + + while (( $# > 0 )); do + if [[ ! -f "$1" ]]; then + echo "extract: '$1' is not a valid file" >&2 + shift + continue + fi + + success=0 + extract_dir="${1:t:r}" + case "$1" in + (*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$1" | tar xv } || tar zxvf "$1" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;; + (*.tar.xz|*.txz) + tar --xz --help &> /dev/null \ + && tar --xz -xvf "$1" \ + || xzcat "$1" | tar xvf - ;; + (*.tar.zma|*.tlz) + tar --lzma --help &> /dev/null \ + && tar --lzma -xvf "$1" \ + || lzcat "$1" | tar xvf - ;; + (*.tar) tar xvf "$1" ;; + (*.gz) (( $+commands[pigz] )) && pigz -d "$1" || gunzip "$1" ;; + (*.bz2) bunzip2 "$1" ;; + (*.xz) unxz "$1" ;; + (*.lzma) unlzma "$1" ;; + (*.Z) uncompress "$1" ;; + (*.zip|*.war|*.jar|*.sublime-package|*.ipsw|*.xpi|*.apk) unzip "$1" -d $extract_dir ;; + (*.rar) unrar x -ad "$1" ;; + (*.7z) 7za x "$1" ;; + (*.deb) + mkdir -p "$extract_dir/control" + mkdir -p "$extract_dir/data" + cd "$extract_dir"; ar vx "../${1}" > /dev/null + cd control; tar xzvf ../control.tar.gz + cd ../data; extract ../data.tar.* + cd ..; rm *.tar.* debian-binary + cd .. + ;; + (*) + echo "extract: '$1' cannot be extracted" >&2 + success=1 + ;; + esac + + (( success = $success > 0 ? $success : $? )) + (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1" + shift + done +} + diff --git a/cfg/zsh/cfg.d/source.d/function-fzf.zsh b/cfg/zsh/cfg.d/source.d/function-fzf.zsh new file mode 100644 index 0000000..693fbae --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-fzf.zsh @@ -0,0 +1,2 @@ +[[ $- == *i* ]] && source "/usr/share/fzf/completion.zsh" 2> /dev/null +source "/usr/share/fzf/key-bindings.zsh" diff --git a/cfg/zsh/cfg.d/source.d/function-git.zsh b/cfg/zsh/cfg.d/source.d/function-git.zsh new file mode 100644 index 0000000..15e3a82 --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-git.zsh @@ -0,0 +1,221 @@ +zstyle -s ":vcs_info:git:*:-all-" "command" _omz_git_git_cmd +: ${_omz_git_git_cmd:=git} + +function current_branch() { + git_current_branch +} +function current_repository() { + if ! $_omz_git_git_cmd rev-parse --is-inside-work-tree &> /dev/null; then + return + fi + echo $($_omz_git_git_cmd remote -v | cut -d':' -f 2) +} +function _git_log_prettily(){ + if ! [ -z $1 ]; then + git log --pretty=$1 + fi +} +function work_in_progress() { + if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then + echo "WIP!!" + fi +} + +alias g='git' +alias ga='git add' +alias gaa='git add --all' +alias gapa='git add --patch' +alias gau='git add --update' +alias gb='git branch' +alias gba='git branch -a' +alias gbd='git branch -d' +alias gbda='git branch --no-color --merged | command grep -vE "^(\*|\s*(master|develop|dev)\s*$)" | command xargs -n 1 git branch -d' +alias gbl='git blame -b -w' +alias gbnm='git branch --no-merged' +alias gbr='git branch --remote' +alias gbs='git bisect' +alias gbsb='git bisect bad' +alias gbsg='git bisect good' +alias gbsr='git bisect reset' +alias gbss='git bisect start' + +alias gc='git commit -v' +alias gc!='git commit -v --amend' +alias gcn!='git commit -v --no-edit --amend' +alias gca='git commit -v -a' +alias gca!='git commit -v -a --amend' +alias gcan!='git commit -v -a --no-edit --amend' +alias gcans!='git commit -v -a -s --no-edit --amend' +alias gcam='git commit -a -m' +alias gcsm='git commit -s -m' +alias gcb='git checkout -b' +alias gcf='git config --list' +alias gcl='git clone --recursive' +alias gclean='git clean -fd' +alias gpristine='git reset --hard && git clean -dfx' +alias gcm='git checkout master' +alias gcd='git checkout develop' +alias gcmsg='git commit -m' +alias gco='git checkout' +alias gcount='git shortlog -sn' +compdef _git gcount +alias gcp='git cherry-pick' +alias gcpa='git cherry-pick --abort' +alias gcpc='git cherry-pick --continue' +alias gcs='git commit -S' + +alias gd='git diff' +alias gdca='git diff --cached' +alias gdct='git describe --tags `git rev-list --tags --max-count=1`' +alias gdt='git diff-tree --no-commit-id --name-only -r' +alias gdw='git diff --word-diff' +gdv() { git diff -w "$@" | view - } +compdef _git gdv=git-diff + +alias gf='git fetch' +alias gfa='git fetch --all --prune' +alias gfo='git fetch origin' + +function gfg() { git ls-files | grep $@ } +compdef _grep gfg + +alias gg='git gui citool' +alias gga='git gui citool --amend' + +ggf() { + [[ "$#" != 1 ]] && local b="$(git_current_branch)" + git push --force origin "${b:=$1}" +} +compdef _git ggf=git-checkout + +ggl() { + if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then + git pull origin "${*}" + else + [[ "$#" == 0 ]] && local b="$(git_current_branch)" + git pull origin "${b:=$1}" + fi +} +compdef _git ggl=git-checkout + +ggp() { + if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then + git push origin "${*}" + else + [[ "$#" == 0 ]] && local b="$(git_current_branch)" + git push origin "${b:=$1}" + fi +} +compdef _git ggp=git-checkout + +ggpnp() { + if [[ "$#" == 0 ]]; then + ggl && ggp + else + ggl "${*}" && ggp "${*}" + fi +} +compdef _git ggpnp=git-checkout + +ggu() { + [[ "$#" != 1 ]] && local b="$(git_current_branch)" + git pull --rebase origin "${b:=$1}" +} +compdef _git ggu=git-checkout + +alias ggpur='ggu' +compdef _git ggpur=git-checkout + +alias ggpull='git pull origin $(git_current_branch)' +compdef _git ggpull=git-checkout + +alias ggpush='git push origin $(git_current_branch)' +compdef _git ggpush=git-checkout + +alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)' +alias gpsup='git push --set-upstream origin $(git_current_branch)' + +alias ghh='git help' + +alias gignore='git update-index --assume-unchanged' +alias gignored='git ls-files -v | grep "^[[:lower:]]"' +alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk' +compdef _git git-svn-dcommit-push=git + +alias gk='\gitk --all --branches' +compdef _git gk='gitk' +alias gke='\gitk --all $(git log -g --pretty=%h)' +compdef _git gke='gitk' + +alias gl='git pull' +alias glg='git log --stat' +alias glgp='git log --stat -p' +alias glgg='git log --graph' +alias glgga='git log --graph --decorate --all' +alias glgm='git log --graph --max-count=10' +alias glo='git log --oneline --decorate' +alias glol="git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" +alias glola="git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all" +alias glog='git log --oneline --decorate --graph' +alias gloga='git log --oneline --decorate --graph --all' +alias glp="_git_log_prettily" +compdef _git glp=git-log + +alias gm='git merge' +alias gmom='git merge origin/master' +alias gmt='git mergetool --no-prompt' +alias gmtvim='git mergetool --no-prompt --tool=vimdiff' +alias gmum='git merge upstream/master' + +alias gp='git push' +alias gpd='git push --dry-run' +alias gpoat='git push origin --all && git push origin --tags' +compdef _git gpoat=git-push +alias gpu='git push upstream' +alias gpv='git push -v' + +alias gr='git remote' +alias gra='git remote add' +alias grb='git rebase' +alias grba='git rebase --abort' +alias grbc='git rebase --continue' +alias grbi='git rebase -i' +alias grbm='git rebase master' +alias grbs='git rebase --skip' +alias grh='git reset HEAD' +alias grhh='git reset HEAD --hard' +alias grmv='git remote rename' +alias grrm='git remote remove' +alias grset='git remote set-url' +alias grt='cd $(git rev-parse --show-toplevel || echo ".")' +alias gru='git reset --' +alias grup='git remote update' +alias grv='git remote -v' + +alias gsb='git status -sb' +alias gsd='git svn dcommit' +alias gsi='git submodule init' +alias gsps='git show --pretty=short --show-signature' +alias gsr='git svn rebase' +alias gss='git status -s' +alias gst='git status' +alias gsta='git stash save' +alias gstaa='git stash apply' +alias gstc='git stash clear' +alias gstd='git stash drop' +alias gstl='git stash list' +alias gstp='git stash pop' +alias gsts='git stash show --text' +alias gsu='git submodule update' + +alias gts='git tag -s' +alias gtv='git tag | sort -V' + +alias gunignore='git update-index --no-assume-unchanged' +alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1' +alias gup='git pull --rebase' +alias gupv='git pull --rebase -v' +alias glum='git pull upstream master' + +alias gwch='git whatchanged -p --abbrev-commit --pretty=medium' +alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify -m "--wip-- [skip ci]"' diff --git a/cfg/zsh/cfg.d/source.d/function-gitignore.zsh b/cfg/zsh/cfg.d/source.d/function-gitignore.zsh new file mode 100644 index 0000000..a0c74fc --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-gitignore.zsh @@ -0,0 +1,53 @@ +export ZSH_PLUGIN_GITIGNORE_PATH=$(dirname $0) +export ZSH_PLUGIN_GITIGNORE_TEMPLATE_PATHS=${DOTLIB}/gitignore + +function gie () { ${EDITOR} .gitignore } + +function gi() { + if [[ $# -eq 0 ]]; then + cat .gitignore + return 0 + fi + + for t in $*; do + get_gitignore_template $t + done +} + +function gii() { + if [[ $# -eq 0 ]]; then + cat .gitignore + return 0 + fi + + gi $* >>! .gitignore +} + +function get_gitignore_template() { + for tpath in ${(@s/:/)ZSH_PLUGIN_GITIGNORE_TEMPLATE_PATHS}; do; + local file=$(find $tpath -iname "$1.gitignore") + if [[ ! -z $file ]]; then + comment=$(basename $file | sed -e 's/.gitignore$//') + echo + echo "### $comment" + cat $file + break; + fi + done; +} + +_gitignore_get_template_list() { + (for tpath in ${(@s/:/)ZSH_PLUGIN_GITIGNORE_TEMPLATE_PATHS}; do; find $tpath -type f -name "*.gitignore"; done) \ + | xargs -n 1 basename \ + | sed -e 's/.gitignore$//' -e 's/\(.*\)/\L\1/' \ + | sort -u +} + +_gitignore () { + compset -P '*,' + compadd -S '' `_gitignore_get_template_list` +} + +compdef _gitignore gi +compdef _gitignore gii + diff --git a/cfg/zsh/cfg.d/source.d/function-tmux.zsh b/cfg/zsh/cfg.d/source.d/function-tmux.zsh new file mode 100644 index 0000000..22ae08b --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-tmux.zsh @@ -0,0 +1,99 @@ +# +# Aliases +# + +alias ta='tmux attach -t' +alias tad='tmux attach -d -t' +alias ts='tmux new-session -s' +alias tl='tmux list-sessions' +alias tksv='tmux kill-server' +alias tkss='tmux kill-session -t' + +# Only run if tmux is actually installed +if which tmux &> /dev/null + then + # Configuration variables + # + # Automatically start tmux + [[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false + # Only autostart once. If set to false, tmux will attempt to + # autostart every time your zsh configs are reloaded. + [[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true + # Automatically connect to a previous session if it exists + [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true + # Automatically close the terminal when tmux exits + [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART + # Set term to screen or screen-256color based on current terminal support + [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true + # Set '-CC' option for iTerm2 tmux integration + [[ -n "$ZSH_TMUX_ITERM2" ]] || ZSH_TMUX_ITERM2=false + # The TERM to use for non-256 color terminals. + # Tmux states this should be screen, but you may need to change it on + # systems without the proper terminfo + [[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITHOUT_256COLOR="screen" + # The TERM to use for 256 color terminals. + # Tmux states this should be screen-256color, but you may need to change it on + # systems without the proper terminfo + [[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color" + + + # Get the absolute path to the current directory + local zsh_tmux_plugin_path=${DOTCFG}/tmux + + # Determine if the terminal supports 256 colors + if [[ `tput colors` == "256" ]] + then + export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR + else + export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR + fi + + # Set the correct local config file to use. + if [[ "$ZSH_TMUX_ITERM2" == "false" ]] && [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]] + then + #use this when they have a ~/.tmux.conf + export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf" + else + #use this when they don't have a ~/.tmux.conf + export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf" + fi + + # Wrapper function for tmux. + function _zsh_tmux_plugin_run() + { + # We have other arguments, just run them + if [[ -n "$@" ]] + then + \tmux $@ + # Try to connect to an existing session. + elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] + then + \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` attach || \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session + [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit + # Just run tmux, fixing the TERM variable if requested. + else + \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` + [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit + fi + } + + # Use the completions for tmux for our function + compdef _tmux _zsh_tmux_plugin_run + + # Alias tmux to our wrapper function. + alias tmux=_zsh_tmux_plugin_run + + # Autostart if not already in tmux and enabled. + if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]] + then + # Actually don't autostart if we already did and multiple autostarts are disabled. + if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]] + then + export ZSH_TMUX_AUTOSTARTED=true + _zsh_tmux_plugin_run + fi + fi +else + print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." +fi + diff --git a/cfg/zsh/cfg.d/source.d/function-workfix.zsh b/cfg/zsh/cfg.d/source.d/function-workfix.zsh new file mode 100644 index 0000000..30ed9c5 --- /dev/null +++ b/cfg/zsh/cfg.d/source.d/function-workfix.zsh @@ -0,0 +1,3 @@ +# Fix MobaXterm Home/End key with ZSH +bindkey '^[[H' beginning-of-line +bindkey '^[[F' end-of-line diff --git a/cfg/zsh/cfg.d/source.zsh b/cfg/zsh/cfg.d/source.zsh new file mode 100644 index 0000000..f3eefdd --- /dev/null +++ b/cfg/zsh/cfg.d/source.zsh @@ -0,0 +1,9 @@ +alias rm='rm -i' +alias cp='cp -i' +alias mv='mv -i' +alias la='ls -lAh' +clearall () { clear; printf '\033[3J' } + +for file in ${DOTCFGD}/source.d/*.zsh; do + source $file +done