<<< Date Index >>>     <<< Thread Index >>>

Re: use current folder name as argument to abitrary command



On 04May2009 22:25, David J. Weller-Fahy 
<dave-lists-mutt-users@xxxxxxxxxxxxxxx> wrote:
| * Cameron Simpson <cs@xxxxxxxxxx> [2009-05-02 23:19 -0500]:
[...big snip...]
| The commands I tried follow.
| #v+
| folder-hook +lists* "set my_oldrecord=\$record; set record=^; set 
my_folder=\$record; set record=\$my_oldrecord"
| folder-hook +lists* "push <shell-escape>\"~/.mutt/listbox-to-email.pl 
\$my_folder\"<enter>"
| folder-hook +lists* "push <enter-command>\"source 
~/.mutt/listbox-to-email.rc\"<enter>"
| #v-
[...]
| Some notes about why those particular commands (so I can be corrected):
| 1)  I tried both the source and backtick method of the third folder-hook
|     with no success.
| 2)  I tried using 'push <enter-command>' because I thought it would keep
|     the command being executed and reading the resulting file in
|     sequence (as I understand the order of execution).
| 
| > I'm trying to solve a similar problem to yours, so I'm somewhat
| > interested in this discussion.
| [snip clever use of folder-hook w/macro]
| > The macro is done as a folder hook because it must run _after_ the
| > folder-hook that sets $my_folder, and it must set the macro to a
| > string that has the value of $my_folder in it. You'll notice that the
| > macro does _not_ mention $my_folder:
| [snip help text]
| 
| Indeed, I've used similar things in the past.  I'd never looked into
| exactly what sequence mutt processes the rc files, though, which is why
| this particular problem had me confused for a bit.  Thanks for clearing
| it up (at least the confusion part).
| 
| So, really the only problem I need to solve to make everything work
| without having to patch mutt is expanding a my_* variable (perhaps any
| variable?) within backticks.  As I have time I'm code-diving through the
| sections of init.c dealing with backticks to gain a better understanding
| of what happens, and hopefully I'll figure out what I need to do to get
| variable expansion within backticks (or find out it is not possible).

I have finally found some more time to look at this, and spent a few
hours this morning. You are going to love this charming incantation:

  folder-hook . 'set my_oldrecord=$record; set record=^; set my_folder=$record; 
set record=$my_oldrecord'
  folder-hook . 'macro index Z :push\ \":\\\`cs-mutt-per-folder\ 
$my_folder\\\`\\\<enter\\\>\"<enter>; push Z'

Simple and intuitive, eh? (There are just two folder-hook lines there,
if your mailer folds the lines.)

cs-mutt-per-folder is a shell script of my own then emits muttrc commands,
and takes the folder name as its argument as you desire.

The method by which this was arrived at is as follows:

  - we need $my_folder expanded in the command

  - mutt vars are not expanded inside backticks (because that would
    mangle shell variable use in the shell command)

  - thus we need to construct the shell command a little like this:
      \`cs-mutt-per-folder $my_folder\`
    i.e. literal backticks, so we're not "inside backticks" when
    $my_folder is encountered.

  - thus we need a string in our muttrc subject to this expansion,
    and we need to get something to use it:
      push ":\`cs-mutt-per-folder $my_folder\`<enter>"
    You can see this should push the text:
      :`cs-mutt-per-folder =me`
    if I'm in the folder "=me".

  - for added fun of course, this has to happen inside the folder-hooks.
    We can't just push stuff directly because we need to construct a
    mutt-level ""-quoted string, and also it has to happen in the folder
    hook because we don't have the folder name before then.
  
  - So we define a macro and then push the macro keystroke.
    That leads directly to the deeply nested quotes shown, which I then
    spent a LONG time fiddling with until it actually worked:-(

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

What do _you_ care what other people think?     - Arlene Feynman