Solving a problem from the previous millenium

In a world were most people rely on a webbrowser to manage their emails, and few can even orientate in their file-trees outside of their mouse oriented file "explorer/finder", fewer people, I reckon, are hit by this problem in this millenium than in the previous millenium. (On a side note I'm pleased that smartphones have re-popularised the idea of a having specific program for managing emails).

Nevertheless, I abhor having to stretch for the computer mouse and point it to this and that spot and click. While for browsing the web it is hard to find a better solution then using the mouse, but when reading mail and browsing local files, it should not be necessary. Nor should it be needed when you want to start a program. I guess my standpoint shares some ground with that of the PWMT and suckless projects, the latter from which I use dmenu and slock, both of which are in the debian package suckless-tools.

I much enjoy zathura for reading PDF-files (zathura is released by PWMT). When you keep away from mouse-oriented software, you tend to turn up in the console and in the console you manage email with mutt1and browse file-trees with mc - and if you configure your system to let zathura view PDF-files, then you'll notice two problems:

Others have been annoyed by this, or similar problems, before me, see http://ruiabreu.org/2013-01-07-mutt-and-xdg.html, http://www.spocom.com/users/gjohnson/mutt/. So, what is the solution? The mailcap infrastructure already has the capabilites required to solve the problem - and it had them already from in the orginal RFC 1524 published in 1993,2 so there is no point in writing new commands to do the right thing ( examples of the "write new command"-solution includes http://www.spocom.com/users/gjohnson/mutt/ who published the script mutt_bgrun for this purpose and http://ruiabreu.org/2013-01-07-mutt-and-xdg.html who wrote the program mutt-open for a similar purpose ).

Just fix the mailcap entries. OK, but how?

A suggestion on how the mailcap entry should read is this https://www.win.tue.nl/bcf/linux/software/mutt/mime.php and this is OK, but not perfect, since it creates the temporary file in an un-safe way.

This solution have the following features:

And here it is

# The Right Thing
# If the file resides in /tmp, then assume mutt as put it there, and make a
# full copy of if before forking since mutt will delete the file upon return.
# If the file does not reside in /tmp, use a safely named symlink instead of
# the real file (which name may contain spaces or characters protected by the
# shell) is in its filename or in its path.

application/pdf; { \
TEMPSYMLINK=`mktemp` \; \
rm $TEMPSYMLINK \; \
if test -L '%s' \; then \
  cp -a '%s' $TEMPSYMLINK \; \
else \
  DIR=`dirname '%s'` \; \
  if test `echo -n "$DIR" | cut -b 1-4` = "/tmp" \; then \
    cp '%s' $TEMPSYMLINK \; \
  else \
    FILE=`basename '%s'` \; \
    if test `echo -n $DIR | cut -b 1` = "/" \; then \
      ln -s "${DIR}/${FILE}" $TEMPSYMLINK \; \
    else \
      FULLDIR=`pwd`"/"${DIR} \; \
      ln -s "${FULLDIR}"/"${FILE}" $TEMPSYMLINK \; \
      fi \; \
    fi \; \
  fi \; \
  ( zathura $TEMPSYMLINK >/dev/null 2>&1 \; \
    rm $TEMPSYMLINK ) \
    & \
 } ; test=test -n "$DISPLAY"; description=Portable Document Format; nametemplate=%s.pdf;
application/x-pdf; { \
TEMPSYMLINK=`mktemp` \; \
rm $TEMPSYMLINK \; \
if test -L '%s' \; then \
  cp -a '%s' $TEMPSYMLINK \; \
else \
  DIR=`dirname '%s'` \; \
  if test `echo -n "$DIR" | cut -b 1-4` = "/tmp" \; then \
    cp '%s' $TEMPSYMLINK \; \
  else \
    FILE=`basename '%s'` \; \
    if test `echo -n $DIR | cut -b 1` = "/" \; then \
      ln -s "${DIR}/${FILE}" $TEMPSYMLINK \; \
    else \
      FULLDIR=`pwd`"/"${DIR} \; \
      ln -s "${FULLDIR}"/"${FILE}" $TEMPSYMLINK \; \
      fi \; \
    fi \; \
  fi \; \
  ( zathura $TEMPSYMLINK >/dev/null 2>&1 \; \
    rm $TEMPSYMLINK ) \
    & \
 } ; test=test -n "$DISPLAY"; description=Portable Document Format; nametemplate=%s.pdf;

And, for comparision, here are other mailcap entries, including the system default.

## System default, noisy and not forking
application/pdf; /usr/bin/zathura '%s'; test=test -n "$DISPLAY"; \
  description=Portable Document Format; nametemplate=%s.pdf
application/x-pdf; /usr/bin/zathura '%s'; test=test -n "$DISPLAY"; \
  description=Portable Document Format; nametemplate=%s.pdf

## Forks, and is quiet, but does not work when filename contains protected
## characters
application/pdf; zathura --fork '%s' >/dev/null 2>&1 ; test=test -n "$DISPLAY";\
 description=Portable Document Format; nametemplate=%s.pdf
application/x-pdf; zathura --fork '%s' >/dev/null 2>&1 ; test=test -n \
"$DISPLAY"; description=Portable Document Format; nametemplate=%s.pdf

## Works in terminal but not in mutt
application/pdf; { set -m \; /bin/mv -f -T "%s" "%s.mv" \; ( zathura "%s.mv" \
 >/dev/null 2>&1 \; /bin/rm -f "%s.mv" \; ) & } ; test=test -n "$DISPLAY"; \
description=Portable Document Format; nametemplate=%s.pdf
application/x-pdf; { set -m \; /bin/mv -f -T "%s" "%s.mv" \; ( zathura "%s.mv"\
 >/dev/null 2>&1 \; /bin/rm -f "%s.mv" \; ) & } ; test=test -n "$DISPLAY"; \
description=Portable Document Format; nametemplate=%s.pdf

I have reported this a whishlist bug against zathura in the debian bug tracking system.


1. While mutt and mc are both old programs, they are still being developed, or at least there are bugfix releases. At the time of writing, mc has been developed for almost 20 years, and there is a planned new release 4.8.11, and mutt has been around for 19 years, with the lastest release in october 2013. Compared to EMACS and the X window system, they are still very young though.

2. <rant>The whole system of MIME is a very fine idea and I use it heavily, but it is disturbing that people have come to rely on it to the extent that they tend to claim "my computer cannot open the attached file" when they really just have met an error in the MIME system (often caused by a mis-configuration, e.g. wrong Content-Type). If they would just save the attached file locally, investigate it, or at least just try to open it with a program most likely to able to handle it, they would in many cases have noticed that their "computer" actually could open the file. </rant>

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 12, 2017