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

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