At 17:59 -0700 30 Aug 2016, [email protected] wrote:
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.

This patch has been part of my build for a long time now as well. But, it never really seemed to suit me, since it's just a one-way mapping for display purposes.

Even given the limitations, in my experience it doesn't seem to work properly. The main issue that I can recall is that the defined substitutions are applied to mailboxes in the buffy list when presented at the change-folder prompt, resulting in an invalid value.

I just applied this version to an otherwise clean source, and that still seems to be an issue. It also appears that it needs to be updated with support for the sidebar.

In place of this, I've been using the attached patch which allows custom bidirectional mappings for prefixes similar to, and possibly overriding, the builtin `+` and `=` mappings. This is lacking in documentation, but I've been using this for a couple of years now with only minor changes (including adding sidebar support), and it has served me well.
commit 8ec89e0
Author: Aaron Schrab <[email protected]>
Date:   Mon May 23 20:47:23 2016 -0400

    Add mailbox_prefix command
    
    Allow user to define custom mailbox prefixes similar to, and possibly
    overriding, the builtin `=` and `+`.
    
    These mailbox prefixes will replace the beginning portion of a mailbox
    path when displaying it to a user, and be converted back to the expanded
    path when necessary.
    
    Example:
    
    mailbox_prefix + imaps://[email protected]/

diff --git a/PATCHES b/PATCHES
index e69de29..902d717 100644
--- a/PATCHES
+++ b/PATCHES
@@ -0,0 +1 @@
+ats.mailbox_prefix.1
diff --git a/complete.c b/complete.c
index d0ee4af..e724677 100644
--- a/complete.c
+++ b/complete.c
@@ -39,36 +39,52 @@
  */
 int mutt_complete (char *s, size_t slen)
 {
-  char *p;
+  char *p, *q;
   DIR *dirp = NULL;
   struct dirent *de;
   int i ,init=0;
   size_t len;
   char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
   char filepart[_POSIX_PATH_MAX];
+  mailbox_prefix_t *prefix;
+  char exp_path[LONG_STRING];
+
+  prefix = MailboxPrefixList;
+  while (prefix) {
+    len = strlen(prefix->shortcut);
+    if (!strncmp (s, prefix->shortcut, len)) {
+      p = prefix->prefix;
+      q = exp_path;
+      while (*p) *q++ = *p++;
+      p = s + len;
+      while (*p) *q++ = *p++;
+      *q = 0;
+      strfcpy (s, exp_path, slen);
+      break;
+    }
+    prefix = prefix->next;
+  }
+
 #ifdef USE_IMAP
-  char imap_path[LONG_STRING];
-
   dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
-
   /* we can use '/' as a delimiter, imap_complete rewrites it */
-  if (*s == '=' || *s == '+' || *s == '!')
+  if (!prefix && (*s == '=' || *s == '+' || *s == '!'))
   {
     if (*s == '!')
       p = NONULL (Spoolfile);
     else
       p = NONULL (Maildir);
 
-    mutt_concat_path (imap_path, p, s+1, sizeof (imap_path));
+    mutt_concat_path (exp_path, p, s+1, sizeof (exp_path));
   }
   else
-    strfcpy (imap_path, s, sizeof(imap_path));
+    strfcpy (exp_path, s, sizeof(exp_path));
 
-  if (mx_is_imap (imap_path))
-    return imap_complete (s, slen, imap_path);
+  if (mx_is_imap (exp_path))
+    return imap_complete (s, slen, exp_path);
 #endif
   
-  if (*s == '=' || *s == '+' || *s == '!')
+  if (!prefix && (*s == '=' || *s == '+' || *s == '!'))
   {
     dirpart[0] = *s;
     dirpart[1] = 0;
diff --git a/globals.h b/globals.h
index 7be9fff..710b1f2 100644
--- a/globals.h
+++ b/globals.h
@@ -183,7 +183,7 @@ WHERE RX_LIST *SubscribedLists INITVAL(0);
 WHERE RX_LIST *UnSubscribedLists INITVAL(0);
 WHERE SPAM_LIST *SpamList INITVAL(0);
 WHERE RX_LIST *NoSpamList INITVAL(0);
-
+WHERE mailbox_prefix_t *MailboxPrefixList INITVAL(0);
 
 /* bit vector for boolean variables */
 #ifdef MAIN_C
diff --git a/init.c b/init.c
index 23021a7..c7efb41 100644
--- a/init.c
+++ b/init.c
@@ -797,6 +797,39 @@ static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err
   return 0;
 }
 
+static int mutt_parse_mailbox_prefix (BUFFER *b, BUFFER *s, unsigned long data, BUFFER *err)
+{
+  mailbox_prefix_t *mb_prefix, *prev;
+
+  mb_prefix = safe_malloc(sizeof(*mb_prefix));
+  mb_prefix->next = NULL;
+
+  mutt_extract_token (b, s, 0);
+  mb_prefix->shortcut = safe_malloc(b->dsize);
+  strfcpy(mb_prefix->shortcut, b->data, b->dsize);
+
+  mutt_extract_token (b, s, 0);
+  mb_prefix->prefix = safe_malloc(b->dsize);
+  strfcpy(mb_prefix->prefix, b->data, b->dsize);
+
+  if (MailboxPrefixList) {
+    prev = MailboxPrefixList;
+    while (prev->next) {
+      prev = prev->next;
+    }
+    prev->next = mb_prefix;
+  }
+  else
+    MailboxPrefixList = mb_prefix;
+
+  return 0;
+bail:
+  FREE(&mb_prefix->shortcut);
+  FREE(&mb_prefix->prefix);
+  FREE(&mb_prefix);
+  return -1;
+}
+
 static int parse_lists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
 {
   group_context_t *gc = NULL;
diff --git a/init.h b/init.h
index 344464d..24b1d5e 100644
--- a/init.h
+++ b/init.h
@@ -3866,6 +3866,7 @@ static int parse_subscribe (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unsubscribe (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_attachments (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unattachments (BUFFER *, BUFFER *, unsigned long, BUFFER *);
+static int mutt_parse_mailbox_prefix (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 
 
 static int parse_alternates (BUFFER *, BUFFER *, unsigned long, BUFFER *);
@@ -3912,6 +3913,7 @@ const struct command_t Commands[] = {
   { "ignore",		parse_ignore,		0 },
   { "lists",		parse_lists,		0 },
   { "macro",		mutt_parse_macro,	0 },
+  { "mailbox_prefix",	mutt_parse_mailbox_prefix, 0 },
   { "mailboxes",	mutt_parse_mailboxes,	MUTT_MAILBOXES },
   { "unmailboxes",	mutt_parse_mailboxes,	MUTT_UNMAILBOXES },
   { "mailto_allow",	parse_list,		UL &MailtoAllow },
diff --git a/mutt.h b/mutt.h
index 2cf55b1..f021537 100644
--- a/mutt.h
+++ b/mutt.h
@@ -1004,6 +1004,13 @@ typedef struct
 
 #define MUTT_PARTS_TOPLEVEL	(1<<0)	/* is the top-level part */
 
+typedef struct mailbox_prefix_t
+{
+  char *shortcut;
+  char *prefix;
+  struct mailbox_prefix_t *next;
+} mailbox_prefix_t;
+
 #include "ascii.h"
 #include "protos.h"
 #include "lib.h"
diff --git a/muttlib.c b/muttlib.c
index 5254a48..4847075 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -385,6 +385,8 @@ char *_mutt_expand_path (char *s, size_t slen, int rx)
   char q[_POSIX_PATH_MAX] = "";
   char tmp[_POSIX_PATH_MAX];
   char *t;
+  int pfx_len;
+  mailbox_prefix_t *prefix;
 
   char *tail = ""; 
 
@@ -393,6 +395,16 @@ char *_mutt_expand_path (char *s, size_t slen, int rx)
   do 
   {
     recurse = 0;
+    prefix = MailboxPrefixList;
+    while (prefix) {
+      pfx_len = strlen(prefix->shortcut);
+      if (!strncmp(s, prefix->shortcut, pfx_len)) {
+	strfcpy (p, prefix->prefix, sizeof (p));
+	tail = s + pfx_len;
+	goto tail;
+      }
+      prefix = prefix->next;
+    }
 
     switch (*s)
     {
@@ -526,6 +538,7 @@ char *_mutt_expand_path (char *s, size_t slen, int rx)
       }
     }
 
+tail:
     if (rx && *p && !recurse)
     {
       mutt_rx_sanitize_string (q, sizeof (q), p);
@@ -810,8 +823,24 @@ void mutt_pretty_mailbox (char *s, size_t buflen)
   char *p = s, *q = s;
   size_t len;
   url_scheme_t scheme;
+  mailbox_prefix_t *prefix;
   char tmp[PATH_MAX];
 
+  prefix = MailboxPrefixList;
+  while (prefix) {
+    len = strlen (prefix->prefix);
+    if (!strncmp (prefix->prefix, s, len)) {
+      p = prefix->shortcut;
+      while (*p) *q++ = *p++;
+      p = s+len;
+      while (*p) *q++ = *p++;
+      *q = 0;
+      p = q = s;
+      break;
+    }
+    prefix = prefix->next;
+  }
+
   scheme = url_check_scheme (s);
 
 #ifdef USE_IMAP
diff --git a/sidebar.c b/sidebar.c
index 5b7edaf..cd15140 100644
--- a/sidebar.c
+++ b/sidebar.c
@@ -82,6 +82,7 @@ static const char *cb_format_str(char *dest, size_t destlen, size_t col, int col
   SBENTRY *sbe = (SBENTRY *) data;
   unsigned int optional;
   char fmt[STRING];
+  char box[STRING];
 
   if (!sbe || !dest)
     return src;
@@ -99,7 +100,9 @@ static const char *cb_format_str(char *dest, size_t destlen, size_t col, int col
   switch (op)
   {
     case 'B':
-      mutt_format_s (dest, destlen, prefix, sbe->box);
+      strfcpy (box, sbe->box, sizeof (box));
+      mutt_pretty_mailbox(box, sizeof(box));
+      mutt_format_s (dest, destlen, prefix, box);
       break;
 
     case 'd':

Attachment: signature.asc
Description: PGP signature

Reply via email to