]> git.madduck.net Git - etc/mutt.git/commitdiff

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:

bgrun parametrisation
authormartin f. krafft <madduck@madduck.net>
Fri, 9 Aug 2019 11:18:30 +0000 (23:18 +1200)
committermartin f. krafft <madduck@madduck.net>
Fri, 9 Aug 2019 11:18:30 +0000 (23:18 +1200)
.mutt/bgrun
.mutt/bgview [moved from .mutt/bgrun-fifo with 100% similarity]
.mutt/bgview-delay [new symlink]
.mutt/bgview-fifo [new symlink]
.mutt/mailcap.backgrounding
.mutt/mailcap.editing

index 56c26e5b7a394964020bcb16d866d12c8a13e0b9..3b7bb1573170a6146627f92d70f395b80fd35bb7 100755 (executable)
 #!/bin/sh
 
-SELF="${0##*/}"
+#exec 2>> /tmp/bgrun.stderr
+#set -x
 
-if [ -z "$TMPDIR" ]; then
-  TMPDIR=/tmp
-elif [ -d "${TMPDIR}/volatile" ]; then
-  TMPDIR="${TMPDIR}/volatile"
-fi
-export TMPDIR
-TMPDIR=$(mktemp -dp "$TMPDIR" mutt.XXXXXXXXXX)
+SELF="${0##*/}"
 
+TEMPDIR=
+TEMPDIR_SENTINEL=.tempdir
 cleanup() {
-  cd /
-  echo Cleaning up $TMPDIR… >> $TMPDIR/output.stderr
-  notify_output
-  rm -rf "$TMPDIR"
+  if [ -e $TEMPDIR_SENTINEL ]; then
+    local TEMPDIR; TEMPDIR="$PWD"
+    echo Cleaning up tempdir $TEMPDIR… >> output.stderr
+    notify_output
+    cd /
+    rm -rf "$TEMPDIR"
+  else
+    notify_output
+  fi
   trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
 }
 trap cleanup 1 2 3 4 5 6 7 8 10 11 12 13 14 15
 
-cd "$TMPDIR"
+enter_tempdir() {
+  if [ -e $TEMPDIR_SENTINEL ]; then
+    return
+
+  else
+    if [ -z "${TMPDIR:-}" ]; then
+      TMPDIR=/tmp
+    fi
+    for i in $LOGNAME volatile; do
+      if [ -d "${TMPDIR}/$i" ]; then
+        TMPDIR="${TMPDIR}/$i"
+        break
+      fi
+    done
+    cd $(mktemp -dp "$TMPDIR" mutt.XXXXXXXXXX)
+    touch $TEMPDIR_SENTINEL
+  fi
+}
 
 notify() {
   if [ -x "$(command -v awesome-client)" ]; then
+    local stdout stderr escaped output
+    stdout="${2:-}"
+    stderr="${3:-}"
+    for i in stdout stderr; do
+      if eval test -s output.$i; then
+        anything=1
+        escaped=$(eval sed -e 's,\",\\\",g' output.$i)
+        output="${output:+$output
+}${i}:
+$escaped"
+      fi
+    done
+    [ -n "${escaped:-}" ] || return
     awesome-client <<-_eof
        local naughty = require("naughty")
        naughty.notify({ preset = naughty.config.presets.low,
-               title  = "$3",
-               text   = [[stdout:
-               $(sed -e 's,\",\\\",g' "$1")
-               stderr:
-               $(sed -e 's,\",\\\",g' "$2")]]
+               title  = "${1:-Output from mutt/$SELF}",
+               text   = [[$output]]
                })
        _eof
   fi
 }
 
 notify_output() {
-  if [ -s "$TMPDIR/output.stdout" ] || [ -s "$TMPDIR/output.stderr" ]; then
-    notify $TMPDIR/output.stdout $TMPDIR/output.stderr \
-      "Output from mutt/$SELF on $BASENAME"
-  fi
+  [ -e $TEMPDIR_SENTINEL ] || return
+  local stdout stderr anything
+
+  for i in stdout stderr; do
+    if [ -s "output.$i" ]; then
+      eval $i="'output.$i'"
+    else
+      eval $i=/dev/null
+    fi
+  done
+
+  notify "Output from mutt/$SELF" $stdout $stderr
 }
 
 guess_extension() {
   python -c "import mimetypes; print(mimetypes.guess_extension('$1'))"
 }
 
-launch_viewer() {
-  run-mailcap --action=view "$1":"$2" > output.stdout 2> output.stderr
-}
-
-MIMETYPE="$1"; shift
-
 get_file() {
   local t
   if [ -z "$1" ]; then
-    t=$(tempfile -d . -p mutt -s $(guess_extension "$MIMETYPE"))
+    t=$(TMPDIR="$PWD" tempfile -s $(guess_extension "$MIMETYPE"))
     cat > "$t"
     echo "$t"
   else
     t="$(echo ${1##*/} | sed -re 's![^[:alnum:],.@%^+=_-]!_!gi')"
-    cp "$1" "$t"
+    ln "$1" "$t" 2>/dev/null || cp "$1" "$t"
     echo "$t"
   fi
 }
 
+MIMETYPE= FILENAME= VIEWER= DELAY=1
+state=
+for arg in "$@"; do
+  case "$state/$arg" in
+
+    (/-t) state=t;;
+    (t/*) MIMETYPE="$arg"; state=;;
+
+    (/-f) state=f;;
+    (f/*) FILENAME="$arg"; state=;;
+
+    (/-d) state=d;;
+    (d/*) DELAY="$arg"; state=;;
+
+    (/-v) state=v;;
+    (v/*) VIEWER="$arg"; state=;;
+
+    (*)
+      echo >&2 "E: Invalid argument: $i"
+      exit 1
+      ;;
+
+  esac
+done
+
+launch_viewer() {
+  local filename; filename="$1"
+  if [ -z "$VIEWER" ]; then
+    [ -n "${2:-}" ] && filename="${2}:${1}"
+    run-mailcap "$filename" > output.stdout 2> output.stderr
+  else
+    $VIEWER "$filename" > output.stdout 2> output.stderr
+  fi
+}
+
 case "$SELF" in
-  (bgrun)
+  (bgview)
     # make a copy of the file, then launch a shell process in the background
     # to divert to run-mailcap, after which the temporary directory gets
     # cleaned up.
-    FILE="$(get_file "${1:-}")"
+    enter_tempdir
+    FILE="$(get_file "$FILENAME")"
     (
-      launch_viewer "$MIMETYPE" "$FILE"
-      sleep 1
+      ts=$(($(date +%s) + $DELAY))
+      launch_viewer "$FILE" "$MIMETYPE"
+      while [ $(date +%s) -lt $ts ]; do sleep $DELAY; done
       cleanup
     ) &
     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
     ;;
-  (bgrun-fifo)
+  (bgview-fifo)
     # hack to stay around until the viewer has read the file: make a fifo and
     # wait for the cat process to finish writing to it, which means that it
     # must have been consumed on the other end.
-    FILE="$(get_file "${1:-}")"
-    FIFO="${FILE%/*}/fifo-${FILE##*/}"
+    enter_tempdir
+    FILE="$(get_file "$FILENAME")"
+    FIFO="fifo-${FILE##*/}"
     mkfifo "$FIFO"
     cat "$FILE" > "$FIFO" &
     # For some reason, we do have to write a tempfile and cannot seem to
     # redirect stdin directly to the fifo, i.e. this does not work instead of
     # the previous three lines:
     ## cat > "$FIFO" &
-    launch_viewer "$MIMETYPE" "${FIFO}"
+    launch_viewer "$FIFO" "$MIMETYPE"
     wait
     cleanup
     ;;
-  (bgrun-delay)
-    # hack to stay around for a fixed period of time after the viewer process
-    # returns control to the caller, so that we can clean up. This is for
-    # cases when the FIFO method doesn't work, because e.g. Firefox randomly
-    # chooses it needs to read HTML files twice.
-    FILE="$(get_file "${1:-}")"
-    (launch_viewer "$MIMETYPE" "${FILE}") &
+  (bgview-delay)
+    # hack to stay around until the file hasn't been accessed for a few
+    # seconds, so that we can clean up. This is for cases when the FIFO method
+    # doesn't work, because e.g. Firefox randomly chooses it needs to read
+    # HTML files twice.
+    enter_tempdir
+    FILE="$(get_file "$FILENAME")"
+    touch "$FILE"
+    (launch_viewer "$FILE" "$MIMETYPE") &
     (
-      sleep 1m
+      while [ $(($(stat -c%X "$FILE") + $DELAY)) -gt $(date +%s) ]; do
+        sleep $DELAY
+      done
       echo Cleaning up $TMPDIR… > $TMPDIR/output.stderr
       cleanup
     ) &
similarity index 100%
rename from .mutt/bgrun-fifo
rename to .mutt/bgview
diff --git a/.mutt/bgview-delay b/.mutt/bgview-delay
new file mode 120000 (symlink)
index 0000000..b07e7aa
--- /dev/null
@@ -0,0 +1 @@
+bgrun
\ No newline at end of file
diff --git a/.mutt/bgview-fifo b/.mutt/bgview-fifo
new file mode 120000 (symlink)
index 0000000..b07e7aa
--- /dev/null
@@ -0,0 +1 @@
+bgrun
\ No newline at end of file
index 22f6884c22aad733ec3f2d31ae38beeb0bbe5544..b47a8bc3ab75ad028026b963d2c322bd2ef19e88 100644 (file)
@@ -1,8 +1,8 @@
 #
-# Mutt mailcap for backgrounding 
+# Mutt mailcap for backgrounding
 
-text/html; ~/.mutt/bgrun-delay %t; description=Open file in browser via FIFO
-application/*; ~/.mutt/bgrun %t %s; description=Backgrounded run of run-mailcap
-image/*; ~/.mutt/bgrun %t %s; description=Backgrounded run of run-mailcap
-audio/*; ~/.mutt/bgrun %t %s; description=Backgrounded run of run-mailcap
+text/html; ~/.mutt/bgview-delay -t %t; description=Backgrounded run of run-mailcap with delayed cleanup
+application/*; ~/.mutt/bgview -t %t; description=Backgrounded run of run-mailcap
+image/*; ~/.mutt/bgview -t %t; description=Backgrounded run of run-mailcap
+audio/*; ~/.mutt/bgview -t %t; description=Backgrounded run of run-mailcap
 #multipart/related; ~/.mutt/viewmailattachments; description=Backgrounded loading of HTML message with related parts
index 6e94104f60855fa26e2b4b6953026cf5c553952c..f2e48d569b7fa957f777d86299ad702cbfe19364 100644 (file)
@@ -1,2 +1,3 @@
-application/*; chmod +w %s && run-mailcap --action=edit %t:%s
-image/*; chmod +w %s && run-mailcap --action=edit %t:%s
+application/pdf; ~/.mutt/bgview -t %t -v 'pdftool --file'
+application/*; ~/.mutt/bgview -t %t
+image/*; ~/.mutt/bgview -t %t