On 30Jul2023 15:59, Nicolas George <geo...@nsup.org> wrote:
I want to use an external command to decide in what mailbox to save
messages.
When I hit ‘s’ with its default action of save-message, mutt asks
“Save to mailbox ('?' for list): =ml/debian”, with the default being
chosen from save-hooks or from the author of the mail.

What I want is that it invokes an external command to set the default.
The idea is that the external command has access to an index of my
mailboxes and implements heuristics of my choosing to store the mail,
but I can still use Mutt's user interface to make a different decision.
[...]
Except it does not work in two ways:
- the backticks are evaluated once when loading the configuration, they
 need to be evaluated when saving the message, each time;

Yes. For something to run lateryou would need to quote them (to defer their interpretation). But it won't help with save-hook because it does not, itself, run a command.

I take it that you can't easily express what you want with a collection of save hooks in the right order with clever patterns.

- the command in the backticks need access to the content of the mail.

I am considering a solution based on a macro that pipes the message to
the command and generate a temporary configuration file, then sources it
then saves normally.

Yes, this is the kind of approach you'd need.

Also, I would like to avoid any solution that would slow down Mutt when
I am not trying to save a message. A message-hook invoking guess_mailbox
would probably not be acceptable, for example.

Go the macro, rebinding the "s" keystroke. That way it only fires when you try to save. The <pipe-message> command can be used to feed the message content to your script. Some macro which went:

    <pipe-message>your_script.sh >mutt-commands.txt<enter>
    <enter-command>source mutt-commands.txt<enter>
    <save-message>

where your_script.sh read the message contents and then wrote a save-hook command to mutt-commands.txt (obviously that should be a better scratch filename), eg:

    save-hook . your-desired-folder-name-here

_Or_ you could have your command write the folder name to a well known file eg ~/var/mutt/save-folder and have the macro run:

    <enter-command>save-hook . `cat ~/var/mutt/save-folder`</enter>

which feels a bit safer (it won't run arbitrary commands), so a macro like:

    <pipe-message>your_script.sh >~/var/mutt/save-folder<enter>
    <enter-command>save-hook . `cat ~/var/mutt/save-folder`</enter>
    <save-message>

All on one line. And the whole macro should be quoted of course, as it includes special chars (the backticks):

    macro s "<pipe-message>.......<save-message>" "save message with special 
sauce"

Note the macro description as the fourth string - it shows in the "?" menu.

Something along those lines, anyway.

Cheers,
Cameron Simpson <c...@cskk.id.au>

Reply via email to