On Tue, Aug 15, 2023 at 06:03:22AM +0200, Fourhundred Thecat wrote:
> Hello,
> 
> somehow, I got it working, that pdf attachments are opened in external
> viewer (evince). That is very nice.
> 
> But, mutt waits for evince to close, before I can continue working in mutt.
> 
> This prevents me from opening multiple pdf attachments and leave them
> open, while continue working in mutt.
> 
> I guess, I would have to add '&' somewhere, so that evince is opened in
> background ?
> 
> But where?

WARNING: solution posted below is experimental and overkill )))
Just to open PDF in mutt is possible to be done much easier!

But for me defined custom macro "v" in the attach menu working fine
in .muttrc:

  macro attach v |show-on-host<enter>

This pipes the full attachment to the stdin of custom command
"show-on-host". This in turn call another custom command "wmexpl".
This constellation has some history in my configuration and
differentiate if something uses the STDIN and another is taking the
existing filename as argument. I do not use embedded detection of
the file type, done by mutt, and call from my "wmexpl" script
"file" utility, because i like to start the files from command line
outside mutt using the same way/scripts. Just to open PDF in mutt
is possible to be done much easier! For me pdf's are normally
opened with "okular" via "kfmclient exec ..." on KDE. For your case
you would need to add support to open pdf-s via "evince" with
something like (untested):

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  sub run_via_evince
  {
    my ($fname) = (@_);
    system ('evince "' . $fname . '" &') == 0
      or die "failed evince ...: $!";
  }

and modify my PDF handler like this:

-   $ALLOWED=1                if ($FTYPE =~ /.*: PDF document, version [0-9.]+( 
\(password protected\))?(, \d+ page\(?s\)?)?( \(zip deflate encoded\))?$/);
+   $handler=\&run_via_evince if ($FTYPE =~ /.*: PDF document, version [0-9.]+( 
\(password protected\))?(, \d+ page\(?s\)?)?( \(zip deflate encoded\))?$/);

relevant lines from show-on-host:

  #!/usr/bin/perl -w
  use strict;
  use 5.010;
  my $tmp_base_fname = 'show-on-host' . "__guessMeByContent";
  my $tmp_fname = "/tmp/$tmp_base_fname";
  open (MYOUT, '>:raw', $tmp_fname) or die("cant open [$tmp_fname] for write: 
$!");
  while(<>) {
    print MYOUT $_;
  }
  close MYOUT;
  system ("cp", $tmp_fname, "/data/sonstiges");
  system ("wmexpl", "/data/sonstiges/$tmp_base_fname");

relevant lines from wmexpl:

  #!/usr/bin/perl -w
  use strict;
  use 5.010;
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  sub run_via_kfmclient
  {
    my ($fname) = (@_);
    dbg("[run_via_kfmclient]: [$fname]");
    system('kfmclient exec "' . $fname . '"') == 0 
      or die "failed to kfmclient exec ...: $!";
  }

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  sub run_via_oo
  {
    my ($fname) = (@_);
    system ('ooffice "' . $fname . '" &') == 0
      or die "failed ooffice ...: $!";
  }

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  sub guess_handler
  {
    my ($FNAME_CMDLINE) = (@_);
    my $FTYPE = `file "$FNAME_CMDLINE"`;
    dbg ("FTYPE=$FTYPE");
    my $ALLOWED = 0;
    my $handler = undef;

    $ALLOWED=1            if ($FTYPE =~ /.*: PDF document, version [0-9.]+( 
\(password protected\))?(, \d+ page\(?s\)?)?( \(zip deflate encoded\))?$/);
    $handler=\&run_via_oo if ($FTYPE =~ /.*: Microsoft Word [0-9+]+$/);
    $handler=\&run_via_oo if ($FTYPE =~ /.*: Microsoft OOXML$/);
    $ALLOWED=1            if ($FTYPE =~ /[^:]+: JPEG image data,/);
    $ALLOWED=1            if ($FTYPE =~ /[^:]+: PNG image data,/);
    #HTML document, ASCII text, with very long lines (559)
    $ALLOWED=1            if ($FTYPE =~ /[^:]+: HTML document(, ASCII text)?(,( 
UTF-8)? Unicode text)?(, with very long lines( \(\d+\))?)?(, UTF-8 text)?$/); # 
too restrictive?
    $handler=\&run_via_oo if ($FTYPE =~ /[^:]+: Microsoft Excel [0-9+]+$/);
    $handler=\&run_via_oo if ($FTYPE =~ /[^:]+: OpenDocument Spreadsheet$/);
    $handler=\&run_via_oo if ($FTYPE =~ /[^:]+: Microsoft PowerPoint [0-9+]+$/);
    $ALLOWED=1            if ($FTYPE =~ /[^:]+: PostScript document text 
conforming DSC level 3.0$/);

    if ($ALLOWED && !defined($handler)) {
      $handler = \&run_via_kfmclient;
    }

    return ( $handler, $FTYPE );

  }

  sub unix_wmexpl
  {
    my ($FNAME_CMDLINE) = (@_);
    my ($handler,$ftype) = &guess_handler($FNAME_CMDLINE);
    if (defined($handler))
    {
      &{$handler}($FNAME_CMDLINE);
    }
    else
    {
      die "handler is not defined";
    }
  }

  $FNAME_CMDLINE = $ARGV[0]//".";
  &unix_wmexpl($FNAME_CMDLINE);

With best regards, Anton

Reply via email to