doc/manual.xml.head |  35 +++++++++++++++++++++++++++++++++++
 globals.h           |   1 +
 init.h              |   2 ++
 muttlib.c           |  21 +++++++++++++++++++--
 4 files changed, 57 insertions(+), 2 deletions(-)


# HG changeset patch
# User David Champion <[email protected]>
# Date 1472605104 25200
#      Tue Aug 30 17:58:24 2016 -0700
# Node ID cec6937f382432b0116ae04b3362a5fc7c91eb29
# Parent  5e166b4c0f39461986404467b14941c5856f6d7e
Adds mailboxrx (and unmailboxrx) command.

Mailboxrx allows you to define a regular expression and replacement
template for mailbox names.  Wherever status_format's %f is expanded,
the folder name will be matched in turn against each mailboxrx
expression.  If any expression matches, a replacement of the mailbox
name will be performed, with backreference expansion.

Example:
mailboxrx imaps://(.*)@imap.gmail.com/ %1@gmail:%R

This makes any Google Mail IMAP mailbox look like username@gmail:folder.
%1 expands to the first parenthesized subexpression in the regex.  %L is
all text to the left of the matching expression, and %R is all text to
the right of the matching expression.


mailboxrx (pop|imap)s?://.*\.([^.]+).[^.]+/ %2:%R
This more generally simplifies POP or IMAP mailbox display, reducing it
to "domain:folder" where "domain" is the DNS domain of the server minus
the top-level domain (.com, .net, etc).


Mailboxrx commands are stackable.  On top of the above, you could add:
mailboxrx earthlink %Leln%R

This would replace "earthlink" with "eln" regardless of where it appears
in the mailbox name.

diff --git a/doc/manual.xml.head b/doc/manual.xml.head
--- a/doc/manual.xml.head
+++ b/doc/manual.xml.head
@@ -6137,6 +6137,41 @@
 </screen>
 </example>
 
+<para>
+Similarly, <literal>mailboxrx</literal> lets you replace mailbox names in the
+display, which can be useful if they are very long -- for example, mailboxes
+deep in your filesystem, or IMAP URLs with a lot of prefatory junk.
+</para>
+
+<cmdsynopsis>
+<command>mailboxrx</command>
+<arg choice="plain">
+<replaceable class="parameter">pattern</replaceable>
+</arg>
+<arg choice="plain">
+<replaceable class="parameter">replacement</replaceable>
+</arg>
+
+<command>unmailboxrx</command>
+<arg choice="plain">
+<replaceable class="parameter">pattern</replaceable>
+</arg>
+</cmdsynopsis>
+
+<example id="ex-mailboxrx">
+<title>Mailbox Munging</title>
+<screen>
+# Shorten my home directory path
+mailboxrx '/home/dgc' '~/%R'
+
+# Shorten my Mail folder
+mailboxrx '/home/dgc/[mM]ail/' '=%R'
+
+# Shorten IMAP
+mailboxrx 'imaps?://[^/]*/' 'imap:%R'
+</screen>
+</example>
+
 </sect1>
 
 <sect1 id="new-mail">
diff --git a/globals.h b/globals.h
--- a/globals.h
+++ b/globals.h
@@ -185,6 +185,7 @@
 WHERE REPLACE_LIST *SpamList INITVAL(0);
 WHERE RX_LIST *NoSpamList INITVAL(0);
 WHERE REPLACE_LIST *SubjectRxList INITVAL(0);
+WHERE REPLACE_LIST *MailboxRxList INITVAL(0);
 
 
 /* bit vector for boolean variables */
diff --git a/init.h b/init.h
--- a/init.h
+++ b/init.h
@@ -3940,6 +3940,8 @@
   { "macro",           mutt_parse_macro,       0 },
   { "mailboxes",       mutt_parse_mailboxes,   MUTT_MAILBOXES },
   { "unmailboxes",     mutt_parse_mailboxes,   MUTT_UNMAILBOXES },
+  { "mailboxrx",    parse_replace_list, UL &MailboxRxList },
+  { "unmailboxrx",  parse_unreplace_list, UL &MailboxRxList },
   { "mailto_allow",    parse_list,             UL &MailtoAllow },
   { "unmailto_allow",  parse_unlist,           UL &MailtoAllow },
   { "message-hook",    mutt_parse_hook,        MUTT_MESSAGEHOOK },
diff --git a/muttlib.c b/muttlib.c
--- a/muttlib.c
+++ b/muttlib.c
@@ -826,10 +826,24 @@
   }
 }
 
+static void apply_mailboxrx (char *s, size_t buflen)
+{
+  char *repl;
+
+  repl = mutt_apply_replace (NULL, 0, s, MailboxRxList);
+  if (repl)
+  {
+    strncpy(s, repl, buflen-1);
+    s[buflen-1] = '\0';
+    FREE(&repl);
+  }
+}
+
 /* collapse the pathname using ~ or = when possible */
-void mutt_pretty_mailbox (char *s, size_t buflen)
+void mutt_pretty_mailbox (char *buf, size_t buflen)
 {
-  char *p = s, *q = s;
+  char *s = buf;
+  char *p = buf, *q = buf;
   size_t len;
   url_scheme_t scheme;
   char tmp[PATH_MAX];
@@ -840,6 +854,7 @@
   if (scheme == U_IMAP || scheme == U_IMAPS)
   {
     imap_pretty_mailbox (s);
+    apply_mailboxrx (buf, buflen);
     return;
   }
 #endif
@@ -895,6 +910,8 @@
     *s++ = '~';
     memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
   }
+
+  apply_mailboxrx (buf, buflen);
 }
 
 void mutt_pretty_size (char *s, size_t len, LOFF_T n)

Reply via email to