On Sun, Oct 25, 2020 at 11:38:01PM +0000, Globe Trotter via Mutt-users 
<mutt-users@mutt.org> wrote:

> On Sunday, October 25, 2020, 5:53:20 PM CDT, raf <m...@raf.org> wrote: 
> 
> On Sun, Oct 25, 2020 at 12:18:26AM +0000, Globe Trotter via Mutt-users 
> <mutt-users@mutt.org> wrote:
> 
> > On Saturday, 24 October at 22:54, Mutt Users wrote:
> > 
> > >> Lots of people send me mail in HTML format (even though I do not
> > >> like it). I have the following set up in my .mailcap:
> > >> text/html; w3m -I %{charset} -T text/html; copiousoutput
> > >> so it converts things using w3m more or less okay, however, I
> > >> am wondering is it possible to have an option for viewing using
> > >> midori/firefox for the cases where w3m is not enough?
> > 
> > > I've got this in my mailcap:
> > 
> > > text/html; w3m -I %{charset} -T text/html -dump; copiousoutput; print = 
> > > qutebrowser %s; nametemplate=%s.html
> > 
> > > To open an email in w3m press Enter, in qutebrowser p or whatever
> > > print is bound to. You can adapt this for firefox.
> > 
> > > Hope that helps.
> > > Regards Wim
> > 
> > This solution does exactly what I want. I wanted to use midori, so I
> > replaced qutebrowser with midori and it works.
> > 
> > Is it possible to have multiple options? So, in case midori did not
> > cut it, I would use firefox instead?
> > 
> > TIA!
> 
> Rather than invoking actual browsers directly, you
> could write a script that presented you with a list of
> options, and you enter your choice. That way, you could
> have more that two options. If always having to select
> a browser manually is too inefficient, you could
> continue to invoke w3m when viewing html, and invoke
> this menu-based browser chooser when "printing" html.
> 
> cheers,
> raf
> 
> Thanks, where would this option be presented? While I press "p"?

I think that would be best, so that you can decide
whether or not you want to manually select a browser.
By only invoking this script for "printing" the
attachment, you can still access the easier default
behaviour when viewing the attachment. But it probably
means you can't print the attachment, unless you add
that as one of the choices.

> Is the script in mutt or a bash/python/perl/etc script?

It would be a separate script referred to in your .mailcap
file. Here's a sample implementation:

In your ~/.mailcap (or whatever your mailcap_path is set to):

  text/html; w3m -I %{charset} -T text/html -dump; copiousoutput; print = 
select-browser %s; nametemplate=%s.html

Attached is the command select-browser which would need
to be in your $PATH. Note that it doesn't support all
of the % expansions that mailcap(5) supports. It only
handles the %s filename place holder, but that's
probably fine for web browsers. Adding support for all
the others would make its appearance in the mailcap
file a lot uglier.

cheers,
raf

#!/usr/bin/env perl
use warnings;
use strict;

# select-browser - Let user select a browser manually (via ~/.mailcap entry)

# The order in which browsers are presented to the user
my @order = qw(w3m qutebrowser firefox);

# Browser labels and commands
# Warning: Commands must contain the %s filename place holder.
# Warning: No other mailcap(5) expansions are supported.
# Warning: Doesn't handle filenames containing double quotes.
my %browsers = (
        w3m => 'w3m -T text/html -dump "%s"',
        qutebrowser => 'qutebrowser "%s"',
        firefox => 'firefox "%s"'
);

# Check command line
die "usage: $0 filename.html\n" unless @ARGV;
my $filename = $ARGV[0];

# Prompt the user
$| = 1;
print("Browsers:\n");
printf("  %s: %s\n", $_ + 1, $order[$_]) for (0..$#order);
print("Enter selection: ");
chomp(my $response = <STDIN>);
die("$0: Invalid selection: $response (Must be between 1 and @{[scalar 
@order]})\n")
        unless $response =~ /^\d+$/ && $response >= 1 && $response <= @order;

# Execute the selected browser
exec sprintf $browsers{$order[$response - 1]}, $filename;

# vi:set ts=4 sw=4:

Reply via email to