On 2023-05-03 12:54:45, Ranjan Maitra <mlmai...@gmx.com> spake thus:
On Wed May03'23 09:59:17AM, Will Yardley wrote:
From: Will Yardley <mutt-us...@veggiechinese.net>
Date: Wed, 3 May 2023 09:59:17 -0700
To: mutt-users@mutt.org
Subject: Re: restrict folder hook to specific folder(s)

On Wed, May 03, 2023 at 11:27:05AM -0500, Ranjan Maitra wrote:

> I have a folder called "R". There are other folders that have "R" in > their name. However, when I set up a
>
> folder-hook '(R)'
>
> it seems to apply to all folders with R in it, with embarrassing > consequences. How do I make sure that the hook applies to the R folder > and nothing else?

I've not used folder-hooks myself, but it looks like you may have to anchor one or both sides of the regex to get the behavior you want.
http://www.mutt.org/doc/manual/#mailbox-hook

has some examples.

and
http://www.mutt.org/doc/manual/#regexp
has some info on the regex syntax


Thanks for this! I am a little lost on how to use these syntax for my specific 
use case. I was looking for a way to have mailhooks only apply to specific 
named folders.

I tried giving the complete pathname:

/home/user/Mail/R

but this is cumbersome to put for all the folders, one by one, and also this 
does not specifically exclude out folders such as /home/user/Mail/Rsomething

Is there a way to have a specific hook for only a specific folder?

Many thanks and best wishes,
Ranjan

Hi Ranjan,

To have your folder apply apply only to the 'R' mailbox, try:

    folder-hook '(^/home/user/Mail/R$)'   ...

OR, even better (assuming $folder expands to '/home/user/Mail' (as it probably does, since the default is documented to be "~/Mail")):

    folder-hook '+(R$)'   ...

OR, even better still (to allow the rule match explicitly when mailbox compression may or may not be in effect:

    folder-hook '+(R($|[.]gz$))'   ...


Explanation:
------------
You can have your folder-hook apply to only specific mailboxes, but the regular expression used to match the mailbox needs to be restrictive enough to match uniquely. As you found:

    folder-hook '(R)' ...

matches any mailbox with a capital letter 'R' in it, anywhere. What
may be less obvious is that it is matching on the full path to the
mailbox, so if your $HOME directory were named /home/Ranjan, and the
default setting of Mutt's $folder variable consequently expanded to:

    /home/Ranjan/Mail

then the above folder-hook would match every mailbox beneath that
location.

The syntax:

    folder-hook '+(R)' ...

causes an implicit anchoring on the left side due to the "mailbox shortcut expansion" of the initial '+' character, so matches any mailbox in $folder whose name starts with a 'R' (rather than any mailbox path that happens to contain an 'R').

Using the '+' mailbox shortcut expansion also allows your regex to focus on just the part that matches the actual mailbox (mbox file or Maildir directory) name, and avoid explicitly spelling out '/home/user/Mail/' at the start of each.

Tightening that example further:

    folder-hook '+(R$)' ...

adds an anchor to the right side, so matches only a mailbox named 'R' that exists in $folder. That may work fine if the mailbox is in Maildir format, or in uncompressed mbox format, but would not work for a compressed mbox format mailbox named 'R.gz'. To match that, as well (which would avoid needing to change the folder-hook when switching between compressed or non-compressed mailbox setups), a regex alternation via the '|' character can be used to strictly match a mailbox named either 'R' or 'R.gz' (and still only in $folder due to the left-side implied anchor of '+'):

    folder-hook '+(R($|[.]gz$))'   ...

One other thing to consider is that it is possible for multiple different folder-hooks can match a given mailbox regex, and each will be executed in the order in which it appears. Any visible effect of the folder-hooks will be that of the last one invoked, which will be the last one that matched, in the order in which they appear in your Mutt config file(s). Therefore, the hooks should generally be ordered from least-specific to most-specific. E.g.,:

    folder-hook '+(.*R)'           'set index_format="HOOK-FOR-R-ANYWHERE: %2C | %Z 
[%d] %-30.30F (%-4.4c) %s"'
    folder-hook '+(RR($|[.]gz$))'  'set index_format="HOOK-FOR-RR: %2C | %Z [%d] 
%-30.30F (%-4.4c) %s"'
    folder-hook '+(R($|[.]gz$))'   'set index_format="HOOK-FOR-R: %2C | %Z [%d] 
%-30.30F (%-4.4c) %s"'

HTH,
-Al

--
a l a n   d.   s a l e w s k i
ads@salewski.email
salew...@att.net
https://github.com/salewski

Reply via email to