Commit 2fbac4d8 2fbac4d8dd83b0badc8d06388de4847563e29c5f by Sergey Poznyakoff

Improved handling of --select option

1 parent bf42e7a2
Showing 1 changed file with 104 additions and 18 deletions
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
41 #include <mailutils/url.h> 41 #include <mailutils/url.h>
42 #include <mailutils/nls.h> 42 #include <mailutils/nls.h>
43 #include <mailutils/tls.h> 43 #include <mailutils/tls.h>
44 #include <mailutils/error.h>
45 #include <mailutils/mutil.h>
44 46
45 static char* show_field; 47 static char* show_field;
46 static int show_to; 48 static int show_to;
...@@ -65,6 +67,104 @@ static int action (observer_t, size_t); ...@@ -65,6 +67,104 @@ static int action (observer_t, size_t);
65 const char *program_version = "frm (" PACKAGE_STRING ")"; 67 const char *program_version = "frm (" PACKAGE_STRING ")";
66 static char doc[] = N_("GNU frm -- display From: lines"); 68 static char doc[] = N_("GNU frm -- display From: lines");
67 69
70 static struct attr_tab {
71 char *name; /* Attribute name */
72 int code; /* Corresponding IS_.* flag */
73 size_t len; /* Minimum abbreviation length */
74 } attr_tab[] = {
75 /* TRANSLATORS: See comment marked [frm status] below */
76 { N_("new"), IS_NEW, 0 },
77 /* TRANSLATORS: See comment marked [frm status] below */
78 { N_("old"), IS_OLD, 0 },
79 /* TRANSLATORS: See comment marked [frm status] below */
80 { N_("unread"), IS_OLD, 0 },
81 /* TRANSLATORS: See comment marked [frm status] below */
82 { N_("read"), IS_READ, 0 },
83 { NULL }
84 };
85
86 static char attr_help[] =
87 /* TRANSLATORS: [frm status]
88
89 1) Please make sure the words "new", "unread", "old" and
90 "read" are translated exactly as in four preceeding messages.
91
92 2) If possible, select such translations for these words, that
93 differ by the first letter.
94 */
95 N_("Select messages with the specific attribute. STATUS is one \
96 of the following: new, unread, old (same as unread) or read. Any unambiguous \
97 abbreviation of those is also accepted.");
98
99
100 /* Attribute table handling */
101
102 /* Prepares the table for use: computes minimum abbreviation lengths */
103 static void
104 prepare_attrs (void)
105 {
106 struct attr_tab *p, *q;
107
108 for (p = attr_tab; p->name; p++)
109 {
110 const char *name = gettext (p->name);
111 size_t len = strlen (name);
112 size_t n = 1;
113
114 for (q = attr_tab; q->name; q++)
115 {
116 if (p != q)
117 {
118 const char *str = gettext (q->name);
119 size_t slen = strlen (str);
120
121 if (memcmp (name, str, n) == 0)
122 {
123 for (n++;
124 memcmp (name, str, n) == 0
125 && n < len
126 && n < slen; n++)
127 ;
128
129 q->len = n < slen ? n : slen;
130 }
131 }
132 }
133 p->len = n < len ? n : len;
134 }
135 }
136
137 /* Translates the textual status representation to the corresponding
138 IS_.* flag */
139 static int
140 decode_attr (char *arg)
141 {
142 struct attr_tab *p;
143 int len = strlen (arg);
144 int pretendents = 0;
145
146 for (p = attr_tab; p->name; p++)
147 {
148 const char *str = gettext (p->name);
149 if (str[0] == arg[0])
150 {
151 if (len < p->len)
152 pretendents++;
153 else if (len > strlen (str))
154 continue;
155 if (memcmp (str, arg, p->len) == 0)
156 return p->code;
157 }
158 }
159 if (pretendents)
160 mu_error (_("%s: ambiguous abbreviation"), arg);
161 else
162 mu_error (_("%s: unknown attribute"), arg);
163 return 0;
164 }
165
166
167
68 static struct argp_option options[] = { 168 static struct argp_option options[] = {
69 {"debug", 'd', NULL, 0, N_("Enable debugging output"), 0}, 169 {"debug", 'd', NULL, 0, N_("Enable debugging output"), 0},
70 {"field", 'f', N_("NAME"), 0, N_("Header field to display"), 0}, 170 {"field", 'f', N_("NAME"), 0, N_("Header field to display"), 0},
...@@ -73,8 +173,7 @@ static struct argp_option options[] = { ...@@ -73,8 +173,7 @@ static struct argp_option options[] = {
73 {"Quiet", 'Q', NULL, 0, N_("Very quiet"), 0}, 173 {"Quiet", 'Q', NULL, 0, N_("Very quiet"), 0},
74 {"query", 'q', NULL, 0, N_("Print a message if unread mail"), 0}, 174 {"query", 'q', NULL, 0, N_("Print a message if unread mail"), 0},
75 {"summary",'S', NULL, 0, N_("Print a summary of messages"), 0}, 175 {"summary",'S', NULL, 0, N_("Print a summary of messages"), 0},
76 {"status", 's', "[nor]",0, 176 {"status", 's', N_("STATUS"), 0, attr_help, 0},
77 N_("Select message with the specific attribute: [n]ew, [r]ead, [u]nread."), 0 },
78 {"align", 't', NULL, 0, N_("Try to align"), 0}, 177 {"align", 't', NULL, 0, N_("Try to align"), 0},
79 {0, 0, 0, 0} 178 {0, 0, 0, 0}
80 }; 179 };
...@@ -123,22 +222,7 @@ parse_opt (int key, char *arg, struct argp_state *state) ...@@ -123,22 +222,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
123 break; 222 break;
124 223
125 case 's': 224 case 's':
126 if (optarg) 225 select_attribute = decode_attr (optarg);
127 switch (*optarg)
128 {
129 case 'r':
130 select_attribute |= IS_READ;
131 break;
132
133 case 'o':
134 select_attribute |= IS_OLD;
135 break;
136
137 case 'n':
138 select_attribute |= IS_NEW;
139 break;
140
141 }
142 break; 226 break;
143 227
144 case 't': 228 case 't':
...@@ -366,6 +450,8 @@ main (int argc, char **argv) ...@@ -366,6 +450,8 @@ main (int argc, char **argv)
366 /* Native Language Support */ 450 /* Native Language Support */
367 mu_init_nls (); 451 mu_init_nls ();
368 452
453 prepare_attrs ();
454
369 mu_argp_init (program_version, NULL); 455 mu_argp_init (program_version, NULL);
370 #ifdef WITH_TLS 456 #ifdef WITH_TLS
371 mu_tls_init_client_argp (); 457 mu_tls_init_client_argp ();
......