# # Helper functions for temporary files, directories, and pastebining # # Copyright © 1994–2017 martin f. krafft # Released under the terms of the Artistic Licence 2.0 # # Source repository: http://git.madduck.net/v/etc/zsh.git # cdt() { emulate -L zsh local dirname="$(date +'%F-%H%M%S')${1:+-$1}" REPLY="${TMPDIR:-/tmp}/$dirname" mkdir -m700 "$REPLY" if [[ -n "$1" ]]; then local link="${TMPDIR:-/tmp}/${1:-cdt}" ln -Tnsf "$dirname" "$link" builtin cd "$link" else builtin cd "$REPLY" fi echo "$REPLY" print -s cd "$REPLY" } _cdt() { cdt "$@" >/dev/null zle reset-prompt zle -M "$REPLY" } zle -N _cdt bindkey '\ed' _cdt vit() { emulate -L zsh local i tag for i in "$@"; do case "$i" in -) local stdin=1; shift;; .*) if [ -z "${ft:-}" ]; then ft="${i#.}"; shift else zwarn "filetype $ft already specified, skipping: ${i#.}" fi ;; *) if [ -z "${tag:-}" ]; then tag="$i"; shift else zwarn "tag $tag already specified, skipping: $i" fi ;; esac done local file="$(date +'%F-%H%M%S')${tag:+-$tag}" REPLY="${TMPDIR:-/tmp}/$file" [ -n "$stdin" ] && cat >| "$REPLY" sensible-editor +start "$REPLY" /dev/tty ln -Tsf $file ${TMPDIR:-/tmp}/${tag:-vit} echo "$REPLY" print -s sensible-editor "$REPLY" } _vit() { vit "$@" >/dev/null zle reset-prompt zle -M "$REPLY" } zle -N _vit bindkey '\ef' _vit pastebin() { local target basename; basename="${1##*/}" case "$basename" in (????-??-??-*) target="$basename";; (*) target="${(%):-"%D{%F-%H%M%S}"}-$basename";; esac pub "${1}==${target}" 2>&1 } _pastebinit() { emulate -L zsh [[ -f "$REPLY" ]] || vit paste read -q "yesno? Should I paste the file $REPLY? [yN] " zle reset-prompt if [[ ${yesno:-n} == y ]]; then zle -cR "pasting $REPLY …" REPLY=$(pastebin "${REPLY}") zle -M "$REPLY" fi } zle -N _pastebinit bindkey '\ep' _pastebinit _copy_reply() { if [[ -n "$REPLY" ]]; then if (( $+commands[xclip] )); then echo -En "$REPLY" | xclip -in zle -M "Copied to primary clipboard: $REPLY" else zle -M "Cannot copy to clipboard, xclip command not found" fi else zle -M "Nothing to copy, \$REPLY is empty" fi } zle -N _copy_reply bindkey '\ec' _copy_reply TS() { local topic ts ret script topic="${(j:_:)@}" ts=${(%):-%D{%Y-%m-%d-%H%M}} script="${TMPDIR:-/tmp}/$ts.$$.${topic:-$LOGNAME}.typescript" PS1=" %# " RPS1= script -qe -c "zsh -f" -f "$script" ret=$? zinfo "typescript is in $script ." typeset -g REPLY REPLY="$script" if command -v unterm >/dev/null; then REPLY="${script}.txt" unterm "$script" > "$REPLY" zinfo "plain text transcript is in $REPLY ." fi return $ret } # vim:ft=zsh