+/* trim dest to the length of the longest prefix it shares with src,
+ * returning the length of the trimmed string */
+static int longest_common_prefix (char *dest, const char* src,
+ int start, size_t dlen) {
+ int pos = start;
+
+ while (pos < dlen && dest[pos] && dest[pos] == src[pos])
+ pos++;
+ dest[pos] = '\0';
+
+ return pos;
+}
+
+/* look for IMAP URLs to complete from defined mailboxes. Could be extended
+ * to complete over open connections and account/folder hooks too. */
+static int imap_complete_hosts (char *dest, size_t len) {
+ BUFFY* mailbox;
+ CONNECTION* conn;
+ int rc = -1;
+ int matchlen;
+ int i = 0;
+
+ matchlen = str_len (dest);
+ if (list_empty (Incoming))
+ return (-1);
+ for (i = 0; i < Incoming->length; i++) {
+ mailbox = (BUFFY*) Incoming->data[i];
+ if (!str_ncmp (dest, mailbox->path, matchlen)) {
+ if (rc) {
+ strfcpy (dest, mailbox->path, len);
+ rc = 0;
+ } else
+ longest_common_prefix (dest, mailbox->path, matchlen, len);
+ }
+ }
+
+ for (conn = mutt_socket_head (); conn->next; conn = conn->next) {
+ ciss_url_t url;
+ char urlstr[LONG_STRING];
+
+ if (conn->account.type != M_ACCT_TYPE_IMAP)
+ continue;
+
+ mutt_account_tourl (&conn->account, &url);
+ /* FIXME: how to handle multiple users on the same host? */
+ url.user = NULL;
+ url.path = NULL;
+ url_ciss_tostring (&url, urlstr, sizeof (urlstr), 0);
+ if (!str_ncmp (dest, urlstr, matchlen)) {
+ if (rc) {
+ strfcpy (dest, urlstr, len);
+ rc = 0;
+ } else
+ longest_common_prefix (dest, urlstr, matchlen, len);
+ }
+ }
+
+ return rc;
+}
+