summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/clipboard.zsh125
-rw-r--r--lib/completion.zsh3
-rw-r--r--lib/functions.zsh14
-rw-r--r--lib/git.zsh21
-rw-r--r--lib/grep.zsh57
-rw-r--r--lib/misc.zsh7
-rw-r--r--lib/termsupport.zsh16
7 files changed, 146 insertions, 97 deletions
diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh
index 2c93d1bb5..122145f15 100644
--- a/lib/clipboard.zsh
+++ b/lib/clipboard.zsh
@@ -3,10 +3,23 @@
# This file has support for doing system clipboard copy and paste operations
# from the command line in a generic cross-platform fashion.
#
-# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
-# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
-# "system clipboard", and the X Windows `xclip` command must be installed.
-
+# This is uses essentially the same heuristic as neovim, with the additional
+# special support for Cygwin.
+# See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121
+#
+# - pbcopy, pbpaste (macOS)
+# - cygwin (Windows running Cygwin)
+# - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set)
+# - xclip (if $DISPLAY is set)
+# - xsel (if $DISPLAY is set)
+# - lemonade (for SSH) https://github.com/pocke/lemonade
+# - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/
+# - win32yank (Windows)
+# - tmux (if $TMUX is set)
+#
+# Defines two functions, clipcopy and clippaste, based on the detected platform.
+##
+#
# clipcopy - Copy data to clipboard
#
# Usage:
@@ -15,41 +28,8 @@
#
# clipcopy <file> - copies a file's contents to clipboard
#
-function clipcopy() {
- emulate -L zsh
- local file=$1
- if [[ $OSTYPE == darwin* ]]; then
- if [[ -z $file ]]; then
- pbcopy
- else
- cat $file | pbcopy
- fi
- elif [[ $OSTYPE == cygwin* ]]; then
- if [[ -z $file ]]; then
- cat > /dev/clipboard
- else
- cat $file > /dev/clipboard
- fi
- else
- if (( $+commands[xclip] )); then
- if [[ -z $file ]]; then
- xclip -in -selection clipboard
- else
- xclip -in -selection clipboard $file
- fi
- elif (( $+commands[xsel] )); then
- if [[ -z $file ]]; then
- xsel --clipboard --input
- else
- cat "$file" | xsel --clipboard --input
- fi
- else
- print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
- return 1
- fi
- fi
-}
-
+##
+#
# clippaste - "Paste" data from clipboard to stdout
#
# Usage:
@@ -67,20 +47,61 @@ function clipcopy() {
#
# # Paste to a file
# clippaste > file.txt
-function clippaste() {
+#
+function detect-clipboard() {
emulate -L zsh
- if [[ $OSTYPE == darwin* ]]; then
- pbpaste
- elif [[ $OSTYPE == cygwin* ]]; then
- cat /dev/clipboard
+
+ if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
+ function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
+ function clippaste() { pbpaste; }
+ elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
+ function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
+ function clippaste() { cat /dev/clipboard; }
+ elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
+ function clipcopy() { wl-copy < "${1:-/dev/stdin}"; }
+ function clippaste() { wl-paste; }
+ elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
+ function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
+ function clippaste() { xclip -out -selection clipboard; }
+ elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
+ function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; }
+ function clippaste() { xsel --clipboard --output; }
+ elif (( ${+commands[lemonade]} )); then
+ function clipcopy() { lemonade copy < "${1:-/dev/stdin}"; }
+ function clippaste() { lemonade paste; }
+ elif (( ${+commands[doitclient]} )); then
+ function clipcopy() { doitclient wclip < "${1:-/dev/stdin}"; }
+ function clippaste() { doitclient wclip -r; }
+ elif (( ${+commands[win32yank]} )); then
+ function clipcopy() { win32yank -i < "${1:-/dev/stdin}"; }
+ function clippaste() { win32yank -o; }
+ elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then
+ function clipcopy() { termux-clipboard-set "${1:-/dev/stdin}"; }
+ function clippaste() { termux-clipboard-get; }
+ elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then
+ function clipcopy() { tmux load-buffer "${1:--}"; }
+ function clippaste() { tmux save-buffer -; }
+ elif [[ $(uname -r) = *icrosoft* ]]; then
+ function clipcopy() { clip.exe < "${1:-/dev/stdin}"; }
+ function clippaste() { powershell.exe -noprofile -command Get-Clipboard; }
else
- if (( $+commands[xclip] )); then
- xclip -out -selection clipboard
- elif (( $+commands[xsel] )); then
- xsel --clipboard --output
- else
- print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
- return 1
- fi
+ function _retry_clipboard_detection_or_fail() {
+ local clipcmd="${1}"; shift
+ if detect-clipboard; then
+ "${clipcmd}" "$@"
+ else
+ print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
+ return 1
+ fi
+ }
+ function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
+ function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
+ return 1
fi
}
+
+# Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
+# which is not really an error. If the user calls them, they will attempt to redetect
+# (for example, perhaps the user has now installed xclip) and then either print an error
+# or proceed successfully.
+detect-clipboard || true
diff --git a/lib/completion.zsh b/lib/completion.zsh
index c7db2eb7b..c932bc925 100644
--- a/lib/completion.zsh
+++ b/lib/completion.zsh
@@ -71,3 +71,6 @@ if [[ $COMPLETION_WAITING_DOTS = true ]]; then
zle -N expand-or-complete-with-dots
bindkey "^I" expand-or-complete-with-dots
fi
+
+# automatically load bash completion functions
+autoload -Uz bashcompinit && bashcompinit
diff --git a/lib/functions.zsh b/lib/functions.zsh
index 9f8736bd7..f934ab968 100644
--- a/lib/functions.zsh
+++ b/lib/functions.zsh
@@ -3,11 +3,12 @@ function zsh_stats() {
}
function uninstall_oh_my_zsh() {
- env ZSH=$ZSH sh $ZSH/tools/uninstall.sh
+ env ZSH="$ZSH" sh "$ZSH/tools/uninstall.sh"
}
function upgrade_oh_my_zsh() {
- env ZSH=$ZSH sh $ZSH/tools/upgrade.sh
+ env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
+ command rm -rf "$ZSH/log/update.lock"
}
function take() {
@@ -21,7 +22,7 @@ function open_command() {
case "$OSTYPE" in
darwin*) open_cmd='open' ;;
cygwin*) open_cmd='cygstart' ;;
- linux*) ! [[ $(uname -a) =~ "Microsoft" ]] && open_cmd='xdg-open' || {
+ linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
open_cmd='cmd.exe /c start ""'
[[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
} ;;
@@ -31,12 +32,7 @@ function open_command() {
;;
esac
- # don't use nohup on OSX
- if [[ "$OSTYPE" == darwin* ]]; then
- ${=open_cmd} "$@" &>/dev/null
- else
- nohup ${=open_cmd} "$@" &>/dev/null
- fi
+ ${=open_cmd} "$@" &>/dev/null
}
#
diff --git a/lib/git.zsh b/lib/git.zsh
index 640561e97..00cb00b19 100644
--- a/lib/git.zsh
+++ b/lib/git.zsh
@@ -12,11 +12,21 @@ function git_prompt_info() {
function parse_git_dirty() {
local STATUS
local -a FLAGS
- FLAGS=('--porcelain' '--ignore-submodules=dirty')
+ FLAGS=('--porcelain')
if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
FLAGS+='--untracked-files=no'
fi
+ case "$GIT_STATUS_IGNORE_SUBMODULES" in
+ git)
+ # let git decide (this respects per-repo config in .gitmodules)
+ ;;
+ *)
+ # if unset: ignore dirty submodules
+ # other values are passed to --ignore-submodules
+ FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
+ ;;
+ esac
STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
fi
if [[ -n $STATUS ]]; then
@@ -189,3 +199,12 @@ function git_current_user_name() {
function git_current_user_email() {
command git config user.email 2>/dev/null
}
+
+# Output the name of the root directory of the git repository
+# Usage example: $(git_repo_name)
+function git_repo_name() {
+ local repo_path
+ if repo_path="$(git rev-parse --show-toplevel 2>/dev/null)" && [[ -n "$repo_path" ]]; then
+ echo ${repo_path:t}
+ fi
+}
diff --git a/lib/grep.zsh b/lib/grep.zsh
index abc1650a1..a725e0f26 100644
--- a/lib/grep.zsh
+++ b/lib/grep.zsh
@@ -1,28 +1,41 @@
-# is x grep argument available?
-grep-flag-available() {
- echo | grep $1 "" >/dev/null 2>&1
-}
+__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
-GREP_OPTIONS=""
+# See if there's a cache file modified in the last day
+__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
+if [[ -n "$__GREP_ALIAS_CACHES" ]]; then
+ source "$__GREP_CACHE_FILE"
+else
+ grep-flags-available() {
+ command grep "$@" "" &>/dev/null <<< ""
+ }
-# color grep results
-if grep-flag-available --color=auto; then
- GREP_OPTIONS+=" --color=auto"
-fi
+ # Ignore these folders (if the necessary grep flags are available)
+ EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
-# ignore VCS folders (if the necessary grep flags are available)
-VCS_FOLDERS="{.bzr,CVS,.git,.hg,.svn}"
+ # Check for --exclude-dir, otherwise check for --exclude. If --exclude
+ # isn't available, --color won't be either (they were released at the same
+ # time (v2.5): https://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
+ if grep-flags-available --color=auto --exclude-dir=.cvs; then
+ GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
+ elif grep-flags-available --color=auto --exclude=.cvs; then
+ GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
+ fi
-if grep-flag-available --exclude-dir=.cvs; then
- GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
-elif grep-flag-available --exclude=.cvs; then
- GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
-fi
+ if [[ -n "$GREP_OPTIONS" ]]; then
+ # export grep, egrep and fgrep settings
+ alias grep="grep $GREP_OPTIONS"
+ alias egrep="egrep $GREP_OPTIONS"
+ alias fgrep="fgrep $GREP_OPTIONS"
-# export grep settings
-alias grep="grep $GREP_OPTIONS"
+ # write to cache file if cache directory is writable
+ if [[ -w "$ZSH_CACHE_DIR" ]]; then
+ alias -L grep egrep fgrep >| "$__GREP_CACHE_FILE"
+ fi
+ fi
+
+ # Clean up
+ unset GREP_OPTIONS EXC_FOLDERS
+ unfunction grep-flags-available
+fi
-# clean up
-unset GREP_OPTIONS
-unset VCS_FOLDERS
-unfunction grep-flag-available
+unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES
diff --git a/lib/misc.zsh b/lib/misc.zsh
index 40ffa479d..61571afc9 100644
--- a/lib/misc.zsh
+++ b/lib/misc.zsh
@@ -22,7 +22,7 @@ env_default 'PAGER' 'less'
env_default 'LESS' '-R'
## super user alias
-alias _='sudo'
+alias _='sudo '
## more intelligent acking for ubuntu users
if which ack-grep &> /dev/null; then
@@ -31,10 +31,5 @@ else
alias afind='ack -il'
fi
-# only define LC_CTYPE if undefined
-if [[ -z "$LC_CTYPE" && -z "$LC_ALL" ]]; then
- export LC_CTYPE=${LANG%%:*} # pick the first entry from LANG
-fi
-
# recognize comments
setopt interactivecomments
diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index d67223caa..654927a1c 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -105,8 +105,9 @@ function omz_termsupport_preexec {
title '$CMD' '%100>...>$LINE%<<'
}
-precmd_functions+=(omz_termsupport_precmd)
-preexec_functions+=(omz_termsupport_preexec)
+autoload -U add-zsh-hook
+add-zsh-hook precmd omz_termsupport_precmd
+add-zsh-hook preexec omz_termsupport_preexec
# Keep Apple Terminal.app's current working directory updated
@@ -120,16 +121,17 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
function update_terminalapp_cwd() {
emulate -L zsh
- # Percent-encode the pathname.
- local URL_PATH="$(omz_urlencode -P $PWD)"
- [[ $? != 0 ]] && return 1
+ # Percent-encode the host and path names.
+ local URL_HOST URL_PATH
+ URL_HOST="$(omz_urlencode -P $HOST)" || return 1
+ URL_PATH="$(omz_urlencode -P $PWD)" || return 1
# Undocumented Terminal.app-specific control sequence
- printf '\e]7;%s\a' "file://$HOST$URL_PATH"
+ printf '\e]7;%s\a' "file://$URL_HOST$URL_PATH"
}
# Use a precmd hook instead of a chpwd hook to avoid contaminating output
- precmd_functions+=(update_terminalapp_cwd)
+ add-zsh-hook precmd update_terminalapp_cwd
# Run once to get initial cwd set
update_terminalapp_cwd
fi