]> 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:

5288098c0f35d8918ce97c319d97f6bd5f0e93a2
[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   REPLY=$(mktemp -td ${1:-cdt}.XXXXXX)
13   builtin cd $REPLY
14   rm -f ${TMPDIR:-/tmp}/cdt.latest
15   ln -s $REPLY ${TMPDIR:-/tmp}/cdt.latest
16 }
17
18 _cdt() {
19   cdt "$@"
20   zle reset-prompt
21   zle -M "$REPLY"
22 }
23 zle -N _cdt
24 bindkey '\ed' _cdt
25
26 vit() {
27   emulate -L zsh
28   local prefix i
29   for i in "$@"; do
30     case "$i" in
31       -) local stdin=1; shift;;
32       *) if [ -z "${prefix:-}" ]; then
33            prefix="$i"; shift
34          else
35            zwarn "prefix $prefix already specified, skipping: $i"
36          fi
37          ;;
38     esac
39   done
40   REPLY=$(mktemp -t ${prefix:-vit}-XXXXXX.txt)
41   [ -n "$stdin" ] && cat >| $REPLY
42   sensible-editor +start $REPLY </dev/tty >/dev/tty
43   ln -sf $REPLY ${TMPDIR:-/tmp}/vit.latest
44 }
45 _vit() {
46   vit "$@"
47   zle reset-prompt
48   zle -M "$REPLY"
49 }
50 zle -N _vit
51 bindkey '\ef' _vit
52
53 pastebin() {
54   local target="${(%):-"%D{%F-%H%M%S}"}-${1##*/}"
55   pub "${1}==${target}" 2>&1
56 }
57
58 _pastebinit() {
59   emulate -L zsh
60   if [[ -f "$REPLY" ]]; then
61     read -q "yesno?\rShould I paste the file $REPLY? [yN] "
62     zle reset-prompt
63   else
64     vit paste
65     yesno=y
66   fi
67   if [[ ${yesno:-n} == y ]]; then
68     zle -cR "pasting $REPLY …"
69     REPLY=$(pastebin "${REPLY}")
70     zle -M "$REPLY"
71   fi
72 }
73 zle -N _pastebinit
74 bindkey '\ep' _pastebinit
75
76 _copy_reply() {
77   if [[ -n "$REPLY" ]]; then
78     if (( $+commands[xclip] )); then
79       echo -En "$REPLY" | xclip -in
80       zle -M "Copied to primary clipboard: $REPLY"
81     else
82       zle -M "Cannot copy to clipboard, xclip command not found"
83     fi
84   else
85     zle -M "Nothing to copy, \$REPLY is empty"
86   fi
87 }
88 zle -N _copy_reply
89 bindkey '\ec' _copy_reply
90
91 TS() {
92   local topic ret quiet
93   typeset -g REPLY
94   topic="${(j:_:)@}"
95   REPLY="${TMPDIR:-/tmp}/script.${topic:-$LOGNAME}.$$.${(%):-"%D{%Y%m%d.%H%M}"}"
96   PS1="
97 %# " RPS1= script -qe -c "zsh -f" -f "$REPLY"
98   ret=$?
99   zinfo "typescript is in $REPLY ."
100   return $ret
101 }
102
103 # vim:ft=zsh