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

push tempfile commands to history
[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   print -s cd "$REPLY"
24 }
25
26 _cdt() {
27   cdt "$@" >/dev/null
28   zle reset-prompt
29   zle -M "$REPLY"
30 }
31 zle -N _cdt
32 bindkey '\ed' _cdt
33
34 vit() {
35   emulate -L zsh
36   local i tag
37   for i in "$@"; do
38     case "$i" in
39       -) local stdin=1; shift;;
40       .*) if [ -z "${ft:-}" ]; then
41             ft="${i#.}"; shift
42           else
43             zwarn "filetype $ft already specified, skipping: ${i#.}"
44           fi
45           ;;
46       *) if [ -z "${tag:-}" ]; then
47            tag="$i"; shift
48          else
49            zwarn "tag $tag already specified, skipping: $i"
50          fi
51          ;;
52     esac
53   done
54   local file="$(date +'%F-%H%M%S')${tag:+-$tag}"
55   REPLY="${TMPDIR:-/tmp}/$file"
56   [ -n "$stdin" ] && cat >| "$REPLY"
57   sensible-editor +start "$REPLY" </dev/tty >/dev/tty
58   ln -Tsf $file ${TMPDIR:-/tmp}/${tag:-vit}
59   echo "$REPLY"
60   print -s sensible-editor "$REPLY"
61 }
62 _vit() {
63   vit "$@" >/dev/null
64   zle reset-prompt
65   zle -M "$REPLY"
66 }
67 zle -N _vit
68 bindkey '\ef' _vit
69
70 pastebin() {
71   local target basename;
72   basename="${1##*/}"
73   case "$basename" in
74     (????-??-??-*) target="$basename";;
75     (*) target="${(%):-"%D{%F-%H%M%S}"}-$basename";;
76   esac
77   pub "${1}==${target}" 2>&1
78 }
79
80 _pastebinit() {
81   emulate -L zsh
82   [[ -f "$REPLY" ]] || vit paste
83   read -q "yesno?\rShould I paste the file $REPLY? [yN] "
84   zle reset-prompt
85   if [[ ${yesno:-n} == y ]]; then
86     zle -cR "pasting $REPLY …"
87     REPLY=$(pastebin "${REPLY}")
88     zle -M "$REPLY"
89   fi
90 }
91 zle -N _pastebinit
92 bindkey '\ep' _pastebinit
93
94 _copy_reply() {
95   if [[ -n "$REPLY" ]]; then
96     if (( $+commands[xclip] )); then
97       echo -En "$REPLY" | xclip -in
98       zle -M "Copied to primary clipboard: $REPLY"
99     else
100       zle -M "Cannot copy to clipboard, xclip command not found"
101     fi
102   else
103     zle -M "Nothing to copy, \$REPLY is empty"
104   fi
105 }
106 zle -N _copy_reply
107 bindkey '\ec' _copy_reply
108
109 TS() {
110   local topic ts ret script
111   topic="${(j:_:)@}"
112   ts=${(%):-%D{%Y-%m-%d-%H%M}}
113   script="${TMPDIR:-/tmp}/$ts.$$.${topic:-$LOGNAME}.typescript"
114   PS1="
115 %# " RPS1= script -qe -c "zsh -f" -f "$script"
116   ret=$?
117   zinfo "typescript is in $script ."
118   typeset -g REPLY
119   REPLY="$script"
120   if command -v unterm >/dev/null; then
121     REPLY="${script}.txt"
122     unterm "$script" > "$REPLY"
123   zinfo "plain text transcript is in $REPLY ."
124   fi
125   return $ret
126 }
127
128 # vim:ft=zsh