Andreas Krennmair:
[apps/madmutt.git] / complete.c
index d8355a7..fcb9269 100644 (file)
@@ -14,7 +14,7 @@
  *     You should have received a copy of the GNU General Public License
  *     along with this program; if not, write to the Free Software
  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
- */ 
+ */
 
 #if HAVE_CONFIG_H
 # include "config.h"
@@ -45,10 +45,11 @@ int mutt_complete (char *s, size_t slen)
   char *p;
   DIR *dirp = NULL;
   struct dirent *de;
-  int i ,init=0;
+  int i, init = 0;
   size_t len;
   char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
   char filepart[_POSIX_PATH_MAX];
+
 #ifdef USE_IMAP
   char imap_path[LONG_STRING];
 #endif
@@ -56,8 +57,7 @@ int mutt_complete (char *s, size_t slen)
   dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
 
 #ifdef USE_NNTP
-  if (option (OPTNEWS))
-  {
+  if (option (OPTNEWS)) {
     LIST *l = CurrentNewsSrv->list;
 
     strfcpy (filepart, s, sizeof (filepart));
@@ -66,46 +66,37 @@ int mutt_complete (char *s, size_t slen)
      * special case to handle when there is no filepart yet.
      * find the first subscribed newsgroup
      */
-    if ((len = mutt_strlen (filepart)) == 0)
-    {
-      for (; l; l = l->next)
-      {
-       NNTP_DATA *data = (NNTP_DATA *)l->data;
-
-       if (data && data->subscribed)
-       {
-         strfcpy (filepart, data->group, sizeof (filepart));
-         init++;
-         l = l->next;
-         break;
-       }
+    if ((len = mutt_strlen (filepart)) == 0) {
+      for (; l; l = l->next) {
+        NNTP_DATA *data = (NNTP_DATA *) l->data;
+
+        if (data && data->subscribed) {
+          strfcpy (filepart, data->group, sizeof (filepart));
+          init++;
+          l = l->next;
+          break;
+        }
       }
     }
 
-    for (; l; l = l->next)
-    {
-      NNTP_DATA *data = (NNTP_DATA *)l->data;
+    for (; l; l = l->next) {
+      NNTP_DATA *data = (NNTP_DATA *) l->data;
 
       if (data && data->subscribed &&
-         mutt_strncmp (data->group, filepart, len) == 0)
-      {
-       if (init)
-       {
-         for (i = 0; filepart[i] && data->group[i]; i++)
-         {
-           if (filepart[i] != data->group[i])
-           {
-             filepart[i] = 0;
-             break;
-           }
-         }
-         filepart[i] = 0;
-       }
-       else
-       {
-         strfcpy (filepart, data->group, sizeof (filepart));
-         init = 1;
-       }
+          mutt_strncmp (data->group, filepart, len) == 0) {
+        if (init) {
+          for (i = 0; filepart[i] && data->group[i]; i++) {
+            if (filepart[i] != data->group[i]) {
+              filepart[i] = 0;
+              break;
+            }
+          }
+          filepart[i] = 0;
+        }
+        else {
+          strfcpy (filepart, data->group, sizeof (filepart));
+          init = 1;
+        }
       }
     }
 
@@ -117,37 +108,35 @@ int mutt_complete (char *s, size_t slen)
 
 #ifdef USE_IMAP
   /* we can use '/' as a delimiter, imap_complete rewrites it */
-  if (*s == '=' || *s == '+' || *s == '!')
-  {
+  if (*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 (imap_path, p, s + 1, sizeof (imap_path));
   }
   else
-    strfcpy (imap_path, s, sizeof(imap_path));
+    strfcpy (imap_path, s, sizeof (imap_path));
 
   if (mx_is_imap (imap_path))
     return imap_complete (s, slen, imap_path);
 #endif
-  
-  if (*s == '=' || *s == '+' || *s == '!')
-  {
+
+  if (*s == '=' || *s == '+' || *s == '!') {
     dirpart[0] = *s;
     dirpart[1] = 0;
     if (*s == '!')
       strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
     else
       strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
-    if ((p = strrchr (s, '/')))
-    {
+    if ((p = strrchr (s, '/'))) {
       char buf[_POSIX_PATH_MAX];
+
       *p++ = 0;
       mutt_concat_path (buf, exp_dirpart, s + 1, sizeof (buf));
       strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
-      snprintf (buf, sizeof (buf), "%s%s/", dirpart, s+1);
+      snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
       strfcpy (dirpart, buf, sizeof (dirpart));
       strfcpy (filepart, p, sizeof (filepart));
     }
@@ -155,33 +144,28 @@ int mutt_complete (char *s, size_t slen)
       strfcpy (filepart, s + 1, sizeof (filepart));
     dirp = opendir (exp_dirpart);
   }
-  else
-  {
-    if ((p = strrchr (s, '/')))
-    {
-      if (p == s) /* absolute path */
-      {
-       p = s + 1;
-       strfcpy (dirpart, "/", sizeof (dirpart));
-       exp_dirpart[0] = 0;
-       strfcpy (filepart, p, sizeof (filepart));
-       dirp = opendir (dirpart);
+  else {
+    if ((p = strrchr (s, '/'))) {
+      if (p == s) {             /* absolute path */
+        p = s + 1;
+        strfcpy (dirpart, "/", sizeof (dirpart));
+        exp_dirpart[0] = 0;
+        strfcpy (filepart, p, sizeof (filepart));
+        dirp = opendir (dirpart);
       }
-      else
-      {
-       *p = 0;
-       len = (size_t)(p - s);
-       strncpy (dirpart, s, len);
-       dirpart[len]=0;
-       p++;
-       strfcpy (filepart, p, sizeof (filepart));
-       strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
-       mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
-       dirp = opendir (exp_dirpart);
+      else {
+        *p = 0;
+        len = (size_t) (p - s);
+        strncpy (dirpart, s, len);
+        dirpart[len] = 0;
+        p++;
+        strfcpy (filepart, p, sizeof (filepart));
+        strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
+        mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
+        dirp = opendir (exp_dirpart);
       }
     }
-    else
-    {
+    else {
       /* no directory name, so assume current directory. */
       dirpart[0] = 0;
       strfcpy (filepart, s, sizeof (filepart));
@@ -189,9 +173,10 @@ int mutt_complete (char *s, size_t slen)
     }
   }
 
-  if (dirp == NULL)
-  {
-    dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
+  if (dirp == NULL) {
+    dprint (1,
+            (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart,
+             strerror (errno), errno));
     return (-1);
   }
 
@@ -199,64 +184,55 @@ int mutt_complete (char *s, size_t slen)
    * special case to handle when there is no filepart yet.  find the first
    * file/directory which is not ``.'' or ``..''
    */
-  if ((len = mutt_strlen (filepart)) == 0)
-  {
-    while ((de = readdir (dirp)) != NULL)
-    {
-      if (mutt_strcmp (".", de->d_name) != 0 && mutt_strcmp ("..", de->d_name) != 0)
-      {
-       strfcpy (filepart, de->d_name, sizeof (filepart));
-       init++;
-       break;
+  if ((len = mutt_strlen (filepart)) == 0) {
+    while ((de = readdir (dirp)) != NULL) {
+      if (mutt_strcmp (".", de->d_name) != 0
+          && mutt_strcmp ("..", de->d_name) != 0) {
+        strfcpy (filepart, de->d_name, sizeof (filepart));
+        init++;
+        break;
       }
     }
   }
 
-  while ((de = readdir (dirp)) != NULL)
-  {
-    if (mutt_strncmp (de->d_name, filepart, len) == 0)
-    {
-      if (init)
-      {
-       for (i=0; filepart[i] && de->d_name[i]; i++)
-       {
-         if (filepart[i] != de->d_name[i])
-         {
-           filepart[i] = 0;
-           break;
-         }
-       }
-       filepart[i] = 0;
+  while ((de = readdir (dirp)) != NULL) {
+    if (mutt_strncmp (de->d_name, filepart, len) == 0) {
+      if (init) {
+        for (i = 0; filepart[i] && de->d_name[i]; i++) {
+          if (filepart[i] != de->d_name[i]) {
+            filepart[i] = 0;
+            break;
+          }
+        }
+        filepart[i] = 0;
       }
-      else
-      {
-       char buf[_POSIX_PATH_MAX];
-       struct stat st;
-
-       strfcpy (filepart, de->d_name, sizeof(filepart));
-
-       /* check to see if it is a directory */
-       if (dirpart[0])
-       {
-         strfcpy (buf, exp_dirpart, sizeof (buf));
-         strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf));
-       }
-       else
-         buf[0] = 0;
-       strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf));
-       if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
-         strfcpy (filepart + strlen (filepart), "/",
-                  sizeof (filepart) - strlen (filepart));
-       init = 1;
+      else {
+        char buf[_POSIX_PATH_MAX];
+        struct stat st;
+
+        strfcpy (filepart, de->d_name, sizeof (filepart));
+
+        /* check to see if it is a directory */
+        if (dirpart[0]) {
+          strfcpy (buf, exp_dirpart, sizeof (buf));
+          strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf));
+        }
+        else
+          buf[0] = 0;
+        strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf));
+        if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
+          strfcpy (filepart + strlen (filepart), "/",
+                   sizeof (filepart) - strlen (filepart));
+        init = 1;
       }
     }
   }
   closedir (dirp);
 
-  if (dirpart[0])
-  {
+  if (dirpart[0]) {
     strfcpy (s, dirpart, slen);
-    if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '=' && dirpart[0] != '+')
+    if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '='
+        && dirpart[0] != '+')
       strfcpy (s + strlen (s), "/", slen - strlen (s));
     strfcpy (s + strlen (s), filepart, slen - strlen (s));
   }