Discussion:
Conditional macro?
Alex Sa
2018-12-06 15:16:09 UTC
Permalink
Hi!


Is it possible to set up a macro that would behave differently
dependent on the current folder?

Let's say I want to to limit messages in the "[Gmail]/All Mail" folder
to display only the new ones. So, if I'm not in "All Mail", that would
be something like this:

<change-folder>=[Gmail]/All Mail<Enter><limit>~U<Enter>

However, If I'm IN this "All Mail" folder, then, of course, I don't
want to re-read all these thousands of emails again, and want to
execute "<limit>~U<Enter>" immediately.

Is it doable? :)


Thanks!
--
Alex
Francesco Ariis
2018-12-06 16:15:05 UTC
Permalink
Hello Alex,
Post by Alex Sa
Is it possible to set up a macro that would behave differently
dependent on the current folder?
Never tried it myself, but I recall this:

https://gitlab.com/muttmua/mutt/wikis/ConfigTricks

(specifically, the "(ab)use "macros" as variables."; maybe you can
"set" the variable once you enter a folder and then use it
with your macro).

Maybe it can be of help in your case?
-F
Alex Sa
2018-12-06 16:26:48 UTC
Permalink
Thanks, Francesco!

It's a bit of re-write for all the "changing folders" macroses that I
have previously set up but it seems like this trick indeed can help me
:)

Thanks once again!
--
Alex
Post by Francesco Ariis
Hello Alex,
Post by Alex Sa
Is it possible to set up a macro that would behave differently
dependent on the current folder?
https://gitlab.com/muttmua/mutt/wikis/ConfigTricks
(specifically, the "(ab)use "macros" as variables."; maybe you can
"set" the variable once you enter a folder and then use it
with your macro).
Maybe it can be of help in your case?
-F
--
--
Alex
Paul Hoffman
2018-12-06 16:30:52 UTC
Permalink
Post by Alex Sa
Is it possible to set up a macro that would behave differently
dependent on the current folder?
Yes, using a folder hook to bind a key (or key sequence) to different
actions based on the folder. For example:

# In most folders, pressing L limits to new messages
folder-hook . "macro index L '<limit>~N<Enter>'"

# In [Gmail]/All Mail, pressing L limits to unread messages
folder-hook '^\[Gmail\]\/All Mail$' "macro index L '<limit>~U<Enter>'"

Or, if you just want to limit what's shown when you enter a folder, this
might work:

# By default, show all messages
folder-hook . "<limit>.<Enter>"

# In [Gmail]/All Mail, show only unread messages
folder-hook '^\[Gmail\]\/All Mail$' "<limit>~U<Enter>"

Does that help? I may have misunderstood what you're trying to do.

Paul.
--
Paul Hoffman <***@nkuitse.com>
Alex Sa
2018-12-06 17:00:36 UTC
Permalink
Paul, that's just brilliant, thank you :)

folder-hooks were always somehow overlooked by me but now I see the potential :)

Thanks once again!
--
Alex
Post by Paul Hoffman
Post by Alex Sa
Is it possible to set up a macro that would behave differently
dependent on the current folder?
Yes, using a folder hook to bind a key (or key sequence) to different
# In most folders, pressing L limits to new messages
folder-hook . "macro index L '<limit>~N<Enter>'"
# In [Gmail]/All Mail, pressing L limits to unread messages
folder-hook '^\[Gmail\]\/All Mail$' "macro index L '<limit>~U<Enter>'"
Or, if you just want to limit what's shown when you enter a folder, this
# By default, show all messages
folder-hook . "<limit>.<Enter>"
# In [Gmail]/All Mail, show only unread messages
folder-hook '^\[Gmail\]\/All Mail$' "<limit>~U<Enter>"
Does that help? I may have misunderstood what you're trying to do.
Paul.
--
--
--
Alex
Michael Wagner
2018-12-07 14:47:05 UTC
Permalink
Post by Paul Hoffman
Post by Alex Sa
Is it possible to set up a macro that would behave differently
dependent on the current folder?
Or, if you just want to limit what's shown when you enter a folder, this
# By default, show all messages
folder-hook . "<limit>.<Enter>"
# In [Gmail]/All Mail, show only unread messages
folder-hook '^\[Gmail\]\/All Mail$' "<limit>~U<Enter>"
Hello Paul,

I have tested this and when I change the folder the error message is

<limit>.<Enter>: unknown command

Is there another variable that I must set?

Tia Michael
--
Old MacDonald had a computer with an EIE I/O
Kevin J. McCarthy
2018-12-07 17:57:38 UTC
Permalink
Post by Michael Wagner
I have tested this and when I change the folder the error message is
<limit>.<Enter>: unknown command
You'll need to use the push command to execute functions inside a folder
hook:
http://www.mutt.org/doc/manual/#push

However, the push command is a bit tricky in this case. The pushed
functions (e.g., <limit>) aren't run immediately: they are stored up and
run *after* all the folder-hooks are done.

They are stored up in a "stack", like a stack of plates. Each new push
adds a "plate" (i.e. function), to the top of the stack.

Then, after the folder-hooks have all run, the functions are executed
one by one starting with the top "plate".

To summarize, the *folder-hooks* are run in the order they occur in your
muttrc. But if those hooks use the push command, the pushed *functions*
end up being run in reverse order (because the last function pushed is
on the top of the stack).

This example is fine, because the "." folder-hook runs first setting the
default macro. The override for "All Mail" runs second and will
overwrite the first macro:

# In most folders, pressing L limits to new messages
folder-hook . "macro index L '<limit>~N<Enter>'"

# In [Gmail]/All Mail, pressing L limits to unread messages
folder-hook '^\[Gmail\]\/All Mail$' "macro index L '<limit>~U<Enter>'"


However, in this case I have reversed the order of the folder hooks:

# In [Gmail]/All Mail, show only unread messages
folder-hook '^\[Gmail\]\/All Mail$' "push <limit>~U<Enter>"

# By default, show all messages
folder-hook . "push <limit>~A<Enter>"

The push command is run immediately, but the string it pushes is run
*after* all the folder-hooks finish. So in the case, you would want the
default "<limit>.<Enter>" to be run first. Then for "All Mail" you
would want the "<limit>~U<Enter>" to be run afterwards.

Sorry for the long post, but I hope that helps a rather confusing topic.
--
Kevin J. McCarthy
GPG Fingerprint: 8975 A9B3 3AA3 7910 385C 5308 ADEF 7684 8031 6BDA
Michael Wagner
2018-12-07 20:06:15 UTC
Permalink
Post by Kevin J. McCarthy
Post by Michael Wagner
I have tested this and when I change the folder the error message is
<limit>.<Enter>: unknown command
You'll need to use the push command to execute functions inside a folder
http://www.mutt.org/doc/manual/#push
However, the push command is a bit tricky in this case. The pushed functions
(e.g., <limit>) aren't run immediately: they are stored up and run *after* all
the folder-hooks are done.
They are stored up in a "stack", like a stack of plates. Each new push adds a
"plate" (i.e. function), to the top of the stack.
Then, after the folder-hooks have all run, the functions are executed one by
one starting with the top "plate".
To summarize, the *folder-hooks* are run in the order they occur in your
muttrc. But if those hooks use the push command, the pushed *functions* end
up being run in reverse order (because the last function pushed is on the top
of the stack).
This example is fine, because the "." folder-hook runs first setting the
default macro. The override for "All Mail" runs second and will overwrite the
# In most folders, pressing L limits to new messages
folder-hook . "macro index L '<limit>~N<Enter>'"
# In [Gmail]/All Mail, pressing L limits to unread messages
folder-hook '^\[Gmail\]\/All Mail$' "macro index L '<limit>~U<Enter>'"
# In [Gmail]/All Mail, show only unread messages
folder-hook '^\[Gmail\]\/All Mail$' "push <limit>~U<Enter>"
# By default, show all messages
folder-hook . "push <limit>~A<Enter>"
The push command is run immediately, but the string it pushes is run *after*
all the folder-hooks finish. So in the case, you would want the default
"<limit>.<Enter>" to be run first. Then for "All Mail" you would want the
"<limit>~U<Enter>" to be run afterwards.
Sorry for the long post, but I hope that helps a rather confusing topic.
Hello Kevin,

thanks for the expantation, now I got it.

Michael
--
BOFH excuse #229:

wrong polarity of neutron flow
Loading...