]> git.madduck.net Git - etc/neomutt.git/blob - .config/neomutt/bgview

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:

buildmimetree.py: provide a default visitor_fn (debug)
[etc/neomutt.git] / .config / neomutt / bgview
1 #!/bin/sh
2
3 #exec 2>> /tmp/bgrun.stderr
4 #set -x
5
6 SELF="${0##*/}"
7
8 TEMPDIR=
9 TEMPRUNDIR=.tempdir-run.d
10 cleanup() {
11   if [ -d $TEMPRUNDIR ]; then
12     local TEMPDIR; TEMPDIR="$PWD"
13     notify_output
14     cd /
15     rm -rf "$TEMPDIR"
16   fi
17   trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
18 }
19 trap cleanup 1 2 3 4 5 6 7 8 10 11 12 13 14 15
20
21 enter_tempdir() {
22   if [ -d $TEMPRUNDIR ]; then
23     return
24
25   else
26     if [ -z "${TMPDIR:-}" ]; then
27       TMPDIR=/tmp
28     fi
29     for i in $LOGNAME volatile; do
30       if [ -d "${TMPDIR}/$i" ]; then
31         TMPDIR="${TMPDIR}/$i"
32         break
33       fi
34     done
35     cd $(mktemp -dp "$TMPDIR" mutt.XXXXXXXXXX)
36     mkdir $TEMPRUNDIR
37   fi
38 }
39
40 notify() {
41   if [ -x "$(command -v awesome-client)" ]; then
42     local stdout stderr escaped output
43     stdout="${2:-}"
44     stderr="${3:-}"
45     for i in stdout stderr; do
46       if eval test -s $TEMPRUNDIR/output.$i; then
47         escaped=$(eval sed -e 's,\",\\\",g' $TEMPRUNDIR/output.$i)
48         output="${output:+$output
49 }${i}:
50 $escaped"
51       fi
52     done
53     [ -n "${escaped:-}" ] || return
54     awesome-client <<-_eof
55         local naughty = require("naughty")
56         naughty.notify({ preset = naughty.config.presets.low,
57                 title  = "${1:-Output from mutt/$SELF}",
58                 text   = [[$output]]
59                 })
60         _eof
61   fi
62 }
63
64 notify_output() {
65   [ -d $TEMPRUNDIR ] || return
66   local stdout stderr anything
67
68   for i in stdout stderr; do
69     if [ -s "$TEMPRUNDIR/output.$i" ]; then
70       eval $i="'$TEMPRUNDIR/output.$i'"
71     else
72       eval $i=/dev/null
73     fi
74   done
75
76   notify "Output from mutt/$SELF" $stdout $stderr
77 }
78
79 guess_extension() {
80   python3 -c "import mimetypes; print(mimetypes.guess_extension('$1'))"
81 }
82
83 get_file() {
84   local t
85   if [ -z "$1" ]; then
86     t=$(mktemp -p "$PWD" tmp.XXXXXXXXXX$(guess_extension "$MIMETYPE"))
87     cat > "$t"
88     echo "$t"
89   else
90     t="$(echo -n ${1##*/} | sed -re 's![^[:alnum:],.@%^+=_-]!_!gi')"
91     ln "$1" "$t" 2>/dev/null || cp "$1" "$t"
92     echo "$PWD/$t"
93   fi
94 }
95
96 MIMETYPE= FILENAME= VIEWER= DELAY=1
97 state=
98 for arg in "$@"; do
99   case "$state/$arg" in
100
101     (/-t) state=t;;
102     (t/*) MIMETYPE="$arg"; state=;;
103
104     (/-f) state=f;;
105     (f/*) FILENAME="$arg"; state=;;
106
107     (/-d) state=d;;
108     (d/*) DELAY="$arg"; state=;;
109
110     (/-v) state=v;;
111     (v/*) VIEWER="$arg"; state=;;
112
113     (*)
114       echo >&2 "E: Invalid argument: $i"
115       exit 1
116       ;;
117
118   esac
119 done
120
121 launch_viewer() {
122   local filename; filename="$1"
123   if [ -z "$VIEWER" ]; then
124     [ -n "${2:-}" ] && filename="${2}:${1}"
125     run-mailcap "$filename" > $TEMPRUNDIR/output.stdout 2> $TEMPRUNDIR/output.stderr
126   else
127     $VIEWER "$filename" > $TEMPRUNDIR/output.stdout 2> $TEMPRUNDIR/output.stderr
128   fi
129 }
130
131 case "$SELF" in
132   (bgview)
133     # make a copy of the file, then launch a shell process in the background
134     # to divert to run-mailcap, after which the temporary directory gets
135     # cleaned up.
136     enter_tempdir
137     FILE="$(get_file "$FILENAME")"
138     (
139       ts=$(($(date +%s) + $DELAY))
140       launch_viewer "$FILE" "$MIMETYPE"
141       while [ $(date +%s) -lt $ts ]; do sleep 1; done
142       cleanup
143     ) &
144     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
145     ;;
146   (bgview-fifo)
147     # hack to stay around until the viewer has read the file: make a fifo and
148     # wait for the cat process to finish writing to it, which means that it
149     # must have been consumed on the other end.
150     enter_tempdir
151     FILE="$(get_file "$FILENAME")"
152     FIFO="fifo-${FILE##*/}"
153     mkfifo "$FIFO"
154     cat "$FILE" > "$FIFO" &
155     # For some reason, we do have to write a tempfile and cannot seem to
156     # redirect stdin directly to the fifo, i.e. this does not work instead of
157     # the previous three lines:
158     ## cat > "$FIFO" &
159     launch_viewer "$FIFO" "$MIMETYPE"
160     wait
161     cleanup
162     ;;
163   (bgview-delay)
164     # hack to stay around until the file hasn't been accessed for a few
165     # seconds, so that we can clean up. This is for cases when the FIFO method
166     # doesn't work, because e.g. Firefox randomly chooses it needs to read
167     # HTML files twice.
168     enter_tempdir
169     FILE="$(get_file "$FILENAME")"
170     touch "$FILE"
171     (launch_viewer "$FILE" "$MIMETYPE") &
172     (
173       while [ $(($(stat -c%X "$FILE") + $DELAY)) -gt $(date +%s) ]; do
174         sleep 1
175       done
176       cleanup
177     ) &
178     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
179     ;;
180 esac