]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/20-tempfile+dir_functions

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

declare LOGCHECK global
[etc/zsh.git] / .zsh / zshrc / 20-tempfile+dir_functions
1 #
2 # Helper functions for temporary files, directories, and pastebining
3 #
4 # Copyright © 1994–2017 martin f. krafft <madduck@madduck.net>
5 # Released under the terms of the Artistic Licence 2.0
6 #
7 # Source repository: http://git.madduck.net/v/etc/zsh.git
8 #
9
10 cdt() {
11   emulate -L zsh
12   local dirname="$(date +'%F-%H%M%S')${1:+-$1}"
13   REPLY="${TMPDIR:-/tmp}/$dirname"
14   mkdir -m700 "$REPLY"
15   if [[ -n "$1" ]]; then
16     local link="${TMPDIR:-/tmp}/${1:-cdt}"
17     ln -Tnsf "$dirname" "$link"
18     builtin cd "$link"
19   else
20     builtin cd "$REPLY"
21   fi
22   echo "$REPLY"
23 }
24
25 _cdt() {
26   cdt "$@"
27   zle reset-prompt
28   zle -M "$REPLY"
29 }
30 zle -N _cdt
31 bindkey '\ed' _cdt
32
33 vit() {
34   emulate -L zsh
35   local i tag
36   for i in "$@"; do
37     case "$i" in
38       -) local stdin=1; shift;;
39       *) if [ -z "${tag:-}" ]; then
40            tag="$i"; shift
41          else
42            zwarn "tag $tag already specified, skipping: $i"
43          fi
44          ;;
45     esac
46   done
47   local file="$(date +'%F-%H%M%S')${tag:+-$tag}"
48   REPLY="${TMPDIR:-/tmp}/$file"
49   [ -n "$stdin" ] && cat >| "$REPLY"
50   sensible-editor +start "$REPLY" </dev/tty >/dev/tty
51   ln -Tsf $file ${TMPDIR:-/tmp}/${tag:-vit}
52   echo "$REPLY"
53 }
54 _vit() {
55   vit "$@" >/dev/null
56   zle reset-prompt
57   zle -M "$REPLY"
58 }
59 zle -N _vit
60 bindkey '\ef' _vit
61
62 pastebin() {
63   local target basename;
64   basename="${1##*/}"
65   case "$basename" in
66     (????-??-??-*) target="$basename";;
67     (*) target="${(%):-"%D{%F-%H%M%S}"}-$basename";;
68   esac
69   pub "${1}==${target}" 2>&1
70 }
71
72 _pastebinit() {
73   emulate -L zsh
74   [[ -f "$REPLY" ]] || vit paste
75   read -q "yesno?\rShould I paste the file $REPLY? [yN] "
76   zle reset-prompt
77   if [[ ${yesno:-n} == y ]]; then
78     zle -cR "pasting $REPLY …"
79     REPLY=$(pastebin "${REPLY}")
80     zle -M "$REPLY"
81   fi
82 }
83 zle -N _pastebinit
84 bindkey '\ep' _pastebinit
85
86 _copy_reply() {
87   if [[ -n "$REPLY" ]]; then
88     if (( $+commands[xclip] )); then
89       echo -En "$REPLY" | xclip -in
90       zle -M "Copied to primary clipboard: $REPLY"
91     else
92       zle -M "Cannot copy to clipboard, xclip command not found"
93     fi
94   else
95     zle -M "Nothing to copy, \$REPLY is empty"
96   fi
97 }
98 zle -N _copy_reply
99 bindkey '\ec' _copy_reply
100
101 TS() {
102   local topic ts ret script
103   topic="${(j:_:)@}"
104   ts=${(%):-%D{%Y-%m-%d-%H%M}}
105   script="${TMPDIR:-/tmp}/$ts.$$.${topic:-$LOGNAME}.typescript"
106   PS1="
107 %# " RPS1= script -qe -c "zsh -f" -f "$script"
108   ret=$?
109   zinfo "typescript is in $script ."
110   typeset -g REPLY
111   REPLY="$script"
112   if command -v unterm >/dev/null; then
113     REPLY="${script}.txt"
114     unterm "$script" > "$REPLY"
115   zinfo "plain text transcript is in $REPLY ."
116   fi
117   return $ret
118 }
119
120 # vim:ft=zsh