2007-08-24 [paul] 2.10.0cvs163
[claws.git] / src / quote_fmt_parse.y
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 %{
21
22 #include "defs.h"
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include <ctype.h>
28
29 #include "procmsg.h"
30 #include "procmime.h"
31 #include "utils.h"
32 #include "codeconv.h"
33 #include "procheader.h"
34 #include "addr_compl.h"
35 #include "gtk/inputdialog.h"
36
37 #include "quote_fmt.h"
38 #include "quote_fmt_lex.h"
39
40 /* decl */
41 /*
42 flex quote_fmt.l
43 bison -p quote_fmt quote_fmt.y
44 */
45
46 int yylex(void);
47
48 static MsgInfo *msginfo = NULL;
49 static PrefsAccount *account = NULL;
50 #ifdef USE_ASPELL
51 static gchar default_dictionary[BUFFSIZE];
52 #endif
53 static gboolean *visible = NULL;
54 static gboolean dry_run = FALSE;
55 static gint maxsize = 0;
56 static gint stacksize = 0;
57 static GHashTable *var_table = NULL;
58
59 typedef struct st_buffer
60 {
61         gchar *buffer;
62         gint bufsize;
63         gint bufmax;
64 } st_buffer;
65
66 static struct st_buffer main_expr = { NULL, 0, 0 };
67 static struct st_buffer sub_expr = { NULL, 0, 0 };
68 static struct st_buffer* current = NULL;
69
70 static const gchar *quote_str = NULL;
71 static const gchar *body = NULL;
72 static gint error = 0;
73
74 static gint cursor_pos = -1;
75
76 extern int quote_fmt_firsttime;
77 extern int line;
78
79 static void add_visibility(gboolean val)
80 {
81         stacksize++;
82         if (maxsize < stacksize) {
83                 maxsize += 128;
84                 visible = g_realloc(visible, maxsize * sizeof(gboolean));
85                 if (visible == NULL)
86                         maxsize = 0;
87         }
88
89         visible[stacksize - 1] = val;
90 }
91
92 static void remove_visibility(void)
93 {
94         stacksize--;
95         if (stacksize < 0) {
96                 g_warning("Error: visibility stack underflow\n");
97                 stacksize = 0;
98         }
99 }
100
101 static void add_buffer(const gchar *s)
102 {
103         gint len;
104
105         if (s == NULL)
106                 return;
107
108         len = strlen(s);
109         if (current->bufsize + len + 1 > current->bufmax) {
110                 if (current->bufmax == 0)
111                         current->bufmax = 128;
112                 while (current->bufsize + len + 1 > current->bufmax)
113                         current->bufmax *= 2;
114                 current->buffer = g_realloc(current->buffer, current->bufmax);
115         }
116         strcpy(current->buffer + current->bufsize, s);
117         current->bufsize += len;
118 }
119
120 static void clear_buffer(void)
121 {
122         if (current->buffer)
123                 *current->buffer = '\0';
124         else
125                 /* force to an empty string, as buffer should not be left unallocated */
126                 add_buffer("");
127         current->bufsize = 0;
128 }
129
130 gchar *quote_fmt_get_buffer(void)
131 {
132         if (current != &main_expr)
133                 g_warning("Error: parser still in sub-expr mode\n");
134
135         if (error != 0)
136                 return NULL;
137         else
138                 return current->buffer;
139 }
140
141 gint quote_fmt_get_line(void)
142 {
143         return line;
144 }
145
146 gint quote_fmt_get_cursor_pos(void)
147 {
148         return cursor_pos;      
149 }
150
151 #define INSERT(buf) \
152         if (stacksize != 0 && visible[stacksize - 1])\
153                 add_buffer(buf); \
154
155 #define INSERT_CHARACTER(chr) \
156         if (stacksize != 0 && visible[stacksize - 1]) { \
157                 gchar tmp[2]; \
158                 tmp[0] = (chr); \
159                 tmp[1] = '\0'; \
160                 add_buffer(tmp); \
161         }
162
163 void quote_fmt_reset_vartable(void)
164 {
165         if (var_table) {
166                 g_hash_table_destroy(var_table);
167                 var_table = NULL;
168         }
169 }
170
171 #ifdef USE_ASPELL
172 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
173                     const gchar *my_body, gboolean my_dry_run,
174                         PrefsAccount *compose_account,
175                         GtkAspell *compose_gtkaspell)
176 #else
177 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
178                     const gchar *my_body, gboolean my_dry_run,
179                         PrefsAccount *compose_account)
180 #endif
181 {
182         quote_str = my_quote_str;
183         body = my_body;
184         msginfo = info;
185         account = compose_account;
186 #ifdef USE_ASPELL
187         gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
188         if (dict)
189                 strncpy2(default_dictionary, dict, sizeof(default_dictionary));
190         else
191                 *default_dictionary = '\0';
192 #endif
193         dry_run = my_dry_run;
194         stacksize = 0;
195         add_visibility(TRUE);
196         main_expr.bufmax = 0;
197         sub_expr.bufmax = 0;
198         current = &main_expr;
199         clear_buffer();
200         error = 0;
201         line = 1;
202
203         if (!var_table)
204                 var_table = g_hash_table_new_full(g_str_hash, g_str_equal, 
205                                 g_free, g_free);
206
207         /*
208          * force LEX initialization
209          */
210         quote_fmt_firsttime = 1;
211         cursor_pos = -1;
212 }
213
214 void quote_fmterror(char *str)
215 {
216         g_warning("Error: %s at line %d\n", str, line);
217         error = 1;
218 }
219
220 int quote_fmtwrap(void)
221 {
222         return 1;
223 }
224
225 static int isseparator(int ch)
226 {
227         return g_ascii_isspace(ch) || ch == '.' || ch == '-';
228 }
229
230 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
231 {
232         char  result[100];
233         char *rptr;
234         char  zone[6];
235         struct tm lt;
236         const char *fptr;
237         const char *zptr;
238
239         if (!msginfo->date)
240                 return;
241         
242         /* 
243          * ALF - GNU C's strftime() has a nice format specifier 
244          * for time zone offset (%z). Non-standard however, so 
245          * emulate it.
246          */
247
248 #define RLEFT (sizeof result) - (rptr - result) 
249 #define STR_SIZE(x) (sizeof (x) - 1)
250
251         zone[0] = 0;
252
253         if (procheader_date_parse_to_tm(msginfo->date, &lt, zone)) {
254                 /*
255                  * break up format string in tiny bits delimited by valid %z's and 
256                  * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
257                  */
258                 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
259                         int         perc;
260                         const char *p;
261                         char       *tmp;
262                         
263                         if (NULL != (zptr = strstr(fptr, "%z"))) {
264                                 /*
265                                  * count nr. of prepended percent chars
266                                  */
267                                 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
268                                         ;
269                                 /*
270                                  * feed to strftime()
271                                  */
272                                 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
273                                 if (tmp) {
274                                         rptr += strftime(rptr, RLEFT, tmp, &lt);
275                                         g_free(tmp);
276                                 }
277                                 /*
278                                  * append time zone offset
279                                  */
280                                 if (zone[0] && perc % 2) 
281                                         rptr += g_snprintf(rptr, RLEFT, "%s", zone);
282                                 fptr = zptr + STR_SIZE("%z");
283                         } else {
284                                 rptr += strftime(rptr, RLEFT, fptr, &lt);
285                                 fptr  = NULL;
286                         }
287                 }
288                 
289                 if (g_utf8_validate(result, -1, NULL)) {
290                         INSERT(result);
291                 } else {
292                         gchar *utf = conv_codeset_strdup(result, 
293                                 conv_get_locale_charset_str_no_utf8(),
294                                 CS_INTERNAL);
295                         if (utf == NULL || 
296                             !g_utf8_validate(utf, -1, NULL)) {
297                                 g_free(utf);
298                                 utf = g_malloc(strlen(result)*2+1);
299                                 conv_localetodisp(utf, 
300                                         strlen(result)*2+1, result);
301                         }
302                         if (g_utf8_validate(utf, -1, NULL)) {
303                                 INSERT(utf);
304                         }
305                         g_free(utf);
306                 }
307         }
308 #undef STR_SIZE                 
309 #undef RLEFT                    
310 }               
311
312 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
313 {
314         guchar *p;
315         gchar *str;
316
317         if (!msginfo->fromname)
318                 return; 
319         
320         p = (guchar*)strchr(msginfo->fromname, ',');
321         if (p != NULL) {
322                 /* fromname is like "Duck, Donald" */
323                 p++;
324                 while (*p && isspace(*p)) p++;
325                 str = alloca(strlen((char *)p) + 1);
326                 if (str != NULL) {
327                         strcpy(str, (char *)p);
328                         INSERT(str);
329                 }
330         } else {
331                 /* fromname is like "Donald Duck" */
332                 str = alloca(strlen(msginfo->fromname) + 1);
333                 if (str != NULL) {
334                         strcpy(str, msginfo->fromname);
335                         p = (guchar *)str;
336                         while (*p && !isspace(*p)) p++;
337                         *p = '\0';
338                         INSERT(str);
339                 }
340         }
341 }
342
343 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
344 {
345         gchar *p;
346         gchar *str;
347
348         /* This probably won't work together very well with Middle
349            names and the like - thth */
350         if (!msginfo->fromname) 
351                 return;
352
353         str = alloca(strlen(msginfo->fromname) + 1);
354         if (str != NULL) {
355                 strcpy(str, msginfo->fromname);
356                 p = strchr(str, ',');
357                 if (p != NULL) {
358                         /* fromname is like "Duck, Donald" */
359                         *p = '\0';
360                         INSERT(str);
361                 } else {
362                         /* fromname is like "Donald Duck" */
363                         p = str;
364                         while (*p && !isspace(*p)) p++;
365                         if (*p) {
366                             /* We found a space. Get first 
367                              none-space char and insert
368                              rest of string from there. */
369                             while (*p && isspace(*p)) p++;
370                             if (*p) {
371                                 INSERT(p);
372                             } else {
373                                 /* If there is no none-space 
374                                  char, just insert whole 
375                                  fromname. */
376                                 INSERT(str);
377                             }
378                         } else {
379                             /* If there is no space, just 
380                              insert whole fromname. */
381                             INSERT(str);
382                         }
383                 }
384         }
385 }
386
387 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
388 {
389 #define MAX_SENDER_INITIAL 20
390         gchar tmp[MAX_SENDER_INITIAL];
391         guchar *p;
392         gchar *cur;
393         gint len = 0;
394
395         if (!msginfo->fromname) 
396                 return;
397
398         p = (guchar *)msginfo->fromname;
399         cur = tmp;
400         while (*p) {
401                 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
402                         *cur = toupper(*p);
403                                 cur++;
404                         len++;
405                         if (len >= MAX_SENDER_INITIAL - 1)
406                                 break;
407                 } else
408                         break;
409                 while (*p && !isseparator(*p)) p++;
410                 while (*p && isseparator(*p)) p++;
411         }
412         *cur = '\0';
413         INSERT(tmp);
414 }
415
416 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
417                                gboolean quoted, gboolean signature,
418                                const gchar *quote_str)
419 {
420         gchar buf[BUFFSIZE];
421         FILE *fp;
422
423         if (!(msginfo->folder || body))
424                 return;
425
426         if (body)
427                 fp = str_open_as_stream(body);
428         else {
429                 if (MSG_IS_ENCRYPTED(msginfo->flags))
430                         fp = procmime_get_first_encrypted_text_content(msginfo);
431                 else
432                         fp = procmime_get_first_text_content(msginfo);
433         }
434
435         if (fp == NULL)
436                 g_warning("Can't get text part\n");
437         else {
438                 while (fgets(buf, sizeof(buf), fp) != NULL) {
439                         strcrchomp(buf);
440                         
441                         if (!signature && strncmp(buf, "-- \n", 4) == 0)
442                                 break;
443                 
444                         if (quoted && quote_str)
445                                 INSERT(quote_str);
446                         
447                         INSERT(buf);
448                 }
449                 fclose(fp);
450         }
451 }
452
453 static void quote_fmt_insert_file(const gchar *filename)
454 {
455         FILE *file;
456         char buffer[256];
457         
458         if ((file = g_fopen(filename, "rb")) != NULL) {
459                 while (fgets(buffer, sizeof(buffer), file)) {
460                         INSERT(buffer);
461                 }
462                 fclose(file);
463         }
464
465 }
466
467 static void quote_fmt_insert_program_output(const gchar *progname)
468 {
469         FILE *file;
470         char buffer[256];
471
472         if ((file = popen(progname, "r")) != NULL) {
473                 while (fgets(buffer, sizeof(buffer), file)) {
474                         INSERT(buffer);
475                 }
476                 pclose(file);
477         }
478 }
479
480 static void quote_fmt_insert_user_input(const gchar *varname)
481 {
482         gchar *buf = NULL;
483         gchar *text = NULL;
484         
485         if (dry_run) 
486                 return;
487
488         if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
489                 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
490                 text = input_dialog(_("Enter variable"), buf, "");
491                 g_free(buf);
492                 if (!text)
493                         return;
494                 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
495         } else {
496                 /* don't free the one in hashtable at the end */
497                 text = g_strdup(text);
498         }
499
500         if (!text)
501                 return;
502         INSERT(text);
503         g_free(text);
504 }
505
506 static gchar *quote_fmt_complete_address(const gchar *addr)
507 {
508         gint count;
509         gchar *res, *tmp, *email_addr;
510         gchar **split;
511
512         debug_print("quote_fmt_complete_address: %s\n", addr);
513         if (addr == NULL)
514                 return NULL;
515
516         /* if addr is a list of message, try the 1st element only */
517         split = g_strsplit(addr, ",", -1);
518         if (!split || !split[0] || *split[0] == '\0') {
519                 g_strfreev(split);
520                 return NULL;
521         }
522
523         Xstrdup_a(email_addr, split[0], return NULL);
524         extract_address(email_addr);
525         if (!*email_addr) {
526                 g_strfreev(split);
527                 return NULL;
528         }
529
530         res = NULL;
531         start_address_completion(NULL);
532         if (1 < (count = complete_address(email_addr))) {
533                 tmp = get_complete_address(1);
534                 res = procheader_get_fromname(tmp);
535                 g_free(tmp);
536         }
537         end_address_completion();
538         g_strfreev(split);
539
540         debug_print("quote_fmt_complete_address: matched %s\n", res);
541         return res;
542 }
543
544 %}
545
546 %union {
547         char chr;
548         char str[256];
549 }
550
551 /* tokens SHOW */
552 %token SHOW_NEWSGROUPS
553 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
554 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
555 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
556 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
557 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
558 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
559 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
560 %token SHOW_ACCOUNT_DICT
561 %token SHOW_DICT SHOW_TAGS
562 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
563 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
564 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
565 /* tokens QUERY */
566 %token QUERY_DATE QUERY_FROM
567 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
568 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
569 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
570 %token QUERY_DICT
571 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
572 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
573 %token QUERY_TO_FOUND_IN_ADDRESSBOOK
574 /* tokens QUERY_NOT */
575 %token QUERY_NOT_DATE QUERY_NOT_FROM
576 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
577 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
578 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
579 %token QUERY_NOT_DICT
580 %token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
581 %token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
582 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
583 /* other tokens */
584 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
585 %token OPARENT CPARENT
586 %token CHARACTER
587 %token SHOW_DATE_EXPR
588 %token SET_CURSOR_POS
589
590 %start quote_fmt
591
592 %token <chr> CHARACTER
593 %type <chr> character
594 %type <str> string
595
596 %%
597
598 quote_fmt:
599         character_or_special_or_insert_or_query_list ;
600
601 sub_expr:
602         character_or_special_list ;
603
604 character_or_special_or_insert_or_query_list:
605         character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
606         | character_or_special_or_insert_or_query ;
607
608 character_or_special_list:
609         character_or_special character_or_special_list
610         | character_or_special ;
611
612 character_or_special_or_insert_or_query:
613         character_or_special
614         | query
615         | query_not
616         | insert ;
617
618 character_or_special:
619         special
620         | character
621         {
622                 INSERT_CHARACTER($1);
623         };
624
625 character:
626         CHARACTER
627         ;
628
629 string:
630         CHARACTER
631         {
632                 $$[0] = $1;
633                 $$[1] = '\0';
634         }
635         | string CHARACTER
636         {
637                 int len;
638                 
639                 strncpy($$, $1, sizeof($$));
640                 $$[sizeof($$) - 1] = '\0';
641                 len = strlen($$);
642                 if (len + 1 < sizeof($$)) {
643                         $$[len + 1] = '\0';
644                         $$[len] = $2;
645                 }
646         };
647
648 special:
649         SHOW_NEWSGROUPS
650         {
651                 if (msginfo->newsgroups)
652                         INSERT(msginfo->newsgroups);
653         }
654         | SHOW_DATE_EXPR OPARENT string CPARENT
655         {
656                 quote_fmt_show_date(msginfo, $3);
657         }
658         | SHOW_DATE
659         {
660                 if (msginfo->date)
661                         INSERT(msginfo->date);
662         }
663         | SHOW_FROM
664         {
665                 if (msginfo->from)
666                         INSERT(msginfo->from);
667         }
668         | SHOW_MAIL_ADDRESS
669         {
670                 if (msginfo->from) {
671                         gchar *stripped_address = g_strdup(msginfo->from);
672                         extract_address(stripped_address);
673                         INSERT(stripped_address);
674                         g_free(stripped_address);
675                 }
676         }
677         | SHOW_FULLNAME
678         {
679                 if (msginfo->fromname)
680                         INSERT(msginfo->fromname);
681         }
682         | SHOW_FIRST_NAME
683         {
684                 quote_fmt_show_first_name(msginfo);
685         }
686         | SHOW_LAST_NAME
687     {
688                 quote_fmt_show_last_name(msginfo);
689         }
690         | SHOW_SENDER_INITIAL
691         {
692                 quote_fmt_show_sender_initial(msginfo);
693         }
694         | SHOW_SUBJECT
695         {
696                 if (msginfo->subject)
697                         INSERT(msginfo->subject);
698         }
699         | SHOW_TO
700         {
701                 if (msginfo->to)
702                         INSERT(msginfo->to);
703         }
704         | SHOW_MESSAGEID
705         {
706                 if (msginfo->msgid)
707                         INSERT(msginfo->msgid);
708         }
709         | SHOW_PERCENT
710         {
711                 INSERT("%");
712         }
713         | SHOW_CC
714         {
715                 if (msginfo->cc)
716                         INSERT(msginfo->cc);
717         }
718         | SHOW_REFERENCES
719         {
720                 GSList *item;
721
722                 INSERT(msginfo->inreplyto);
723                 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
724                         if (item->data)
725                                 INSERT(item->data);
726         }
727         | SHOW_MESSAGE
728         {
729                 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
730         }
731         | SHOW_QUOTED_MESSAGE
732         {
733                 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
734         }
735         | SHOW_MESSAGE_NO_SIGNATURE
736         {
737                 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
738         }
739         | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
740         {
741                 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
742         }
743         | SHOW_ACCOUNT_FULL_NAME
744         {
745                 if (account && account->name)
746                         INSERT(account->name);
747         }
748         | SHOW_ACCOUNT_MAIL_ADDRESS
749         {
750                 if (account && account->address)
751                         INSERT(account->address);
752         }
753         | SHOW_ACCOUNT_NAME
754         {
755                 if (account && account->account_name)
756                         INSERT(account->account_name);
757         }
758         | SHOW_ACCOUNT_ORGANIZATION
759         {
760                 if (account && account->organization)
761                         INSERT(account->organization);
762         }
763         | SHOW_ACCOUNT_DICT
764         {
765 #ifdef USE_ASPELL
766                 if (account && account->enable_default_dictionary) {
767                         gchar *dictname = g_path_get_basename(account->default_dictionary);
768                         INSERT(dictname);
769                         g_free(dictname);
770                 }
771 #endif
772         }
773         | SHOW_DICT
774         {
775 #ifdef USE_ASPELL
776                 INSERT(default_dictionary);
777 #endif
778         }
779         | SHOW_TAGS
780         {
781                 gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
782                 if (tags) {
783                         INSERT(tags);
784                 }
785                 g_free(tags);
786         }
787         | SHOW_BACKSLASH
788         {
789                 INSERT("\\");
790         }
791         | SHOW_TAB
792         {
793                 INSERT("\t");
794         }
795         | SHOW_EOL
796         {
797                 INSERT("\n");
798         }
799         | SHOW_QUESTION_MARK
800         {
801                 INSERT("?");
802         }
803         | SHOW_EXCLAMATION_MARK
804         {
805                 INSERT("!");
806         }
807         | SHOW_PIPE
808         {
809                 INSERT("|");
810         }
811         | SHOW_OPARENT
812         {
813                 INSERT("{");
814         }
815         | SHOW_CPARENT
816         {
817                 INSERT("}");
818         }
819         | SET_CURSOR_POS
820         {
821                 cursor_pos = current->bufsize;
822         }
823         | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
824         {
825                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
826                 if (tmp) {
827                         INSERT(tmp);
828                         g_free(tmp);
829                 }
830         }
831         | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
832         {
833                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
834                 if (tmp) {
835                         INSERT(tmp);
836                         g_free(tmp);
837                 }
838         }
839         | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
840         {
841                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
842                 if (tmp) {
843                         INSERT(tmp);
844                         g_free(tmp);
845                 }
846         };
847
848 query:
849         QUERY_DATE
850         {
851                 add_visibility(msginfo->date != NULL);
852         }
853         OPARENT quote_fmt CPARENT
854         {
855                 remove_visibility();
856         }
857         | QUERY_FROM
858         {
859                 add_visibility(msginfo->from != NULL);
860         }
861         OPARENT quote_fmt CPARENT
862         {
863                 remove_visibility();
864         }
865         | QUERY_FULLNAME
866         {
867                 add_visibility(msginfo->fromname != NULL);
868         }
869         OPARENT quote_fmt CPARENT
870         {
871                 remove_visibility();
872         }
873         | QUERY_SUBJECT
874         {
875                 add_visibility(msginfo->subject != NULL);
876         }
877         OPARENT quote_fmt CPARENT
878         {
879                 remove_visibility();
880         }
881         | QUERY_TO
882         {
883                 add_visibility(msginfo->to != NULL);
884         }
885         OPARENT quote_fmt CPARENT
886         {
887                 remove_visibility();
888         }
889         | QUERY_NEWSGROUPS
890         {
891                 add_visibility(msginfo->newsgroups != NULL);
892         }
893         OPARENT quote_fmt CPARENT
894         {
895                 remove_visibility();
896         }
897         | QUERY_MESSAGEID
898         {
899                 add_visibility(msginfo->msgid != NULL);
900         }
901         OPARENT quote_fmt CPARENT
902         {
903                 remove_visibility();
904         }
905         | QUERY_CC
906         {
907                 add_visibility(msginfo->cc != NULL);
908         }
909         OPARENT quote_fmt CPARENT
910         {
911                 remove_visibility();
912         }
913         | QUERY_REFERENCES
914         {
915                 gboolean found;
916                 GSList *item;
917
918                 found = (msginfo->inreplyto != NULL);
919                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
920                         if (item->data)
921                                 found = TRUE;
922                 add_visibility(found == TRUE);
923         }
924         OPARENT quote_fmt CPARENT
925         {
926                 remove_visibility();
927         }
928         | QUERY_ACCOUNT_FULL_NAME
929         {
930                 add_visibility(account != NULL && account->name != NULL);
931         }
932         OPARENT quote_fmt CPARENT
933         {
934                 remove_visibility();
935         }
936         | QUERY_ACCOUNT_ORGANIZATION
937         {
938                 add_visibility(account != NULL && account->organization != NULL);
939         }
940         OPARENT quote_fmt CPARENT
941         {
942                 remove_visibility();
943         }
944         | QUERY_ACCOUNT_DICT
945         {
946 #ifdef USE_ASPELL
947                 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
948                                 account->default_dictionary != NULL && *account->default_dictionary != '\0');
949 #else
950                 add_visibility(FALSE);
951 #endif
952         }
953         OPARENT quote_fmt CPARENT
954         {
955                 remove_visibility();
956         }
957         | QUERY_DICT
958         {
959 #ifdef USE_ASPELL
960                 add_visibility(*default_dictionary != '\0');
961 #else
962                 add_visibility(FALSE);
963 #endif
964         }
965         OPARENT quote_fmt CPARENT
966         {
967                 remove_visibility();
968         }
969         | QUERY_CC_FOUND_IN_ADDRESSBOOK
970         {
971                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
972                 add_visibility(tmp != NULL && *tmp != '\0');
973                 g_free(tmp);
974         }
975         OPARENT quote_fmt CPARENT
976         {
977                 remove_visibility();
978         }
979         | QUERY_FROM_FOUND_IN_ADDRESSBOOK
980         {
981                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
982                 add_visibility(tmp != NULL && *tmp != '\0');
983                 g_free(tmp);
984         }
985         OPARENT quote_fmt CPARENT
986         {
987                 remove_visibility();
988         }
989         | QUERY_TO_FOUND_IN_ADDRESSBOOK
990         {
991                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
992                 add_visibility(tmp != NULL && *tmp != '\0');
993                 g_free(tmp);
994         }
995         OPARENT quote_fmt CPARENT
996         {
997                 remove_visibility();
998         };
999
1000 query_not:
1001         QUERY_NOT_DATE
1002         {
1003                 add_visibility(msginfo->date == NULL);
1004         }
1005         OPARENT quote_fmt CPARENT
1006         {
1007                 remove_visibility();
1008         }
1009         | QUERY_NOT_FROM
1010         {
1011                 add_visibility(msginfo->from == NULL);
1012         }
1013         OPARENT quote_fmt CPARENT
1014         {
1015                 remove_visibility();
1016         }
1017         | QUERY_NOT_FULLNAME
1018         {
1019                 add_visibility(msginfo->fromname == NULL);
1020         }
1021         OPARENT quote_fmt CPARENT
1022         {
1023                 remove_visibility();
1024         }
1025         | QUERY_NOT_SUBJECT
1026         {
1027                 add_visibility(msginfo->subject == NULL);
1028         }
1029         OPARENT quote_fmt CPARENT
1030         {
1031                 remove_visibility();
1032         }
1033         | QUERY_NOT_TO
1034         {
1035                 add_visibility(msginfo->to == NULL);
1036         }
1037         OPARENT quote_fmt CPARENT
1038         {
1039                 remove_visibility();
1040         }
1041         | QUERY_NOT_NEWSGROUPS
1042         {
1043                 add_visibility(msginfo->newsgroups == NULL);
1044         }
1045         OPARENT quote_fmt CPARENT
1046         {
1047                 remove_visibility();
1048         }
1049         | QUERY_NOT_MESSAGEID
1050         {
1051                 add_visibility(msginfo->msgid == NULL);
1052         }
1053         OPARENT quote_fmt CPARENT
1054         {
1055                 remove_visibility();
1056         }
1057         | QUERY_NOT_CC
1058         {
1059                 add_visibility(msginfo->cc == NULL);
1060         }
1061         OPARENT quote_fmt CPARENT
1062         {
1063                 remove_visibility();
1064         }
1065         | QUERY_NOT_REFERENCES
1066         {
1067                 gboolean found;
1068                 GSList *item;
1069
1070                 found = (msginfo->inreplyto != NULL);
1071                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1072                         if (item->data)
1073                                 found = TRUE;
1074                 add_visibility(found == FALSE);
1075         }
1076         OPARENT quote_fmt CPARENT
1077         {
1078                 remove_visibility();
1079         }
1080         | QUERY_NOT_ACCOUNT_FULL_NAME
1081         {
1082                 add_visibility(account == NULL || account->name == NULL);
1083         }
1084         OPARENT quote_fmt CPARENT
1085         {
1086                 remove_visibility();
1087         }
1088         | QUERY_NOT_ACCOUNT_ORGANIZATION
1089         {
1090                 add_visibility(account == NULL || account->organization == NULL);
1091         }
1092         OPARENT quote_fmt CPARENT
1093         {
1094                 remove_visibility();
1095         }
1096         | QUERY_NOT_ACCOUNT_DICT
1097         {
1098 #ifdef USE_ASPELL
1099                 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1100                                 || *account->default_dictionary == '\0');
1101 #else
1102                 add_visibility(FALSE);
1103 #endif
1104         }
1105         OPARENT quote_fmt CPARENT
1106         {
1107                 remove_visibility();
1108         }
1109         | QUERY_NOT_DICT
1110         {
1111 #ifdef USE_ASPELL
1112                 add_visibility(*default_dictionary == '\0');
1113 #else
1114                 add_visibility(FALSE);
1115 #endif
1116         }
1117         OPARENT quote_fmt CPARENT
1118         {
1119                 remove_visibility();
1120         }
1121         | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1122         {
1123                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1124                 add_visibility(tmp == NULL || *tmp == '\0');
1125                 g_free(tmp);
1126         }
1127         OPARENT quote_fmt CPARENT
1128         {
1129                 remove_visibility();
1130         }
1131         | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1132         {
1133                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1134                 add_visibility(tmp == NULL || *tmp == '\0');
1135                 g_free(tmp);
1136         }
1137         OPARENT quote_fmt CPARENT
1138         {
1139                 remove_visibility();
1140         }
1141         | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1142         {
1143                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1144                 add_visibility(tmp == NULL || *tmp == '\0');
1145                 g_free(tmp);
1146         }
1147         OPARENT quote_fmt CPARENT
1148         {
1149                 remove_visibility();
1150         };
1151
1152 insert:
1153         INSERT_FILE
1154         {
1155                 current = &sub_expr;
1156                 clear_buffer();
1157         }
1158         OPARENT sub_expr CPARENT
1159         {
1160                 current = &main_expr;
1161                 if (!dry_run) {
1162                         quote_fmt_insert_file(sub_expr.buffer);
1163                 }
1164         }
1165         | INSERT_PROGRAMOUTPUT
1166         {
1167                 current = &sub_expr;
1168                 clear_buffer();
1169         }
1170         OPARENT sub_expr CPARENT
1171         {
1172                 current = &main_expr;
1173                 if (!dry_run) {
1174                         quote_fmt_insert_program_output(sub_expr.buffer);
1175                 }
1176         }
1177         | INSERT_USERINPUT
1178         {
1179                 current = &sub_expr;
1180                 clear_buffer();
1181         }
1182         OPARENT sub_expr CPARENT
1183         {
1184                 current = &main_expr;
1185                 if (!dry_run) {
1186                         quote_fmt_insert_user_input(sub_expr.buffer);
1187                 }
1188         };