6e29c2725e1cdacb33f9090fce579ab03f422982
[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                 if (current->buffer)
822                         cursor_pos = g_utf8_strlen(current->buffer, -1);
823                 else
824                         cursor_pos = 0;
825         }
826         | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
827         {
828                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
829                 if (tmp) {
830                         INSERT(tmp);
831                         g_free(tmp);
832                 }
833         }
834         | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
835         {
836                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
837                 if (tmp) {
838                         INSERT(tmp);
839                         g_free(tmp);
840                 }
841         }
842         | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
843         {
844                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
845                 if (tmp) {
846                         INSERT(tmp);
847                         g_free(tmp);
848                 }
849         };
850
851 query:
852         QUERY_DATE
853         {
854                 add_visibility(msginfo->date != NULL);
855         }
856         OPARENT quote_fmt CPARENT
857         {
858                 remove_visibility();
859         }
860         | QUERY_FROM
861         {
862                 add_visibility(msginfo->from != NULL);
863         }
864         OPARENT quote_fmt CPARENT
865         {
866                 remove_visibility();
867         }
868         | QUERY_FULLNAME
869         {
870                 add_visibility(msginfo->fromname != NULL);
871         }
872         OPARENT quote_fmt CPARENT
873         {
874                 remove_visibility();
875         }
876         | QUERY_SUBJECT
877         {
878                 add_visibility(msginfo->subject != NULL);
879         }
880         OPARENT quote_fmt CPARENT
881         {
882                 remove_visibility();
883         }
884         | QUERY_TO
885         {
886                 add_visibility(msginfo->to != NULL);
887         }
888         OPARENT quote_fmt CPARENT
889         {
890                 remove_visibility();
891         }
892         | QUERY_NEWSGROUPS
893         {
894                 add_visibility(msginfo->newsgroups != NULL);
895         }
896         OPARENT quote_fmt CPARENT
897         {
898                 remove_visibility();
899         }
900         | QUERY_MESSAGEID
901         {
902                 add_visibility(msginfo->msgid != NULL);
903         }
904         OPARENT quote_fmt CPARENT
905         {
906                 remove_visibility();
907         }
908         | QUERY_CC
909         {
910                 add_visibility(msginfo->cc != NULL);
911         }
912         OPARENT quote_fmt CPARENT
913         {
914                 remove_visibility();
915         }
916         | QUERY_REFERENCES
917         {
918                 gboolean found;
919                 GSList *item;
920
921                 found = (msginfo->inreplyto != NULL);
922                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
923                         if (item->data)
924                                 found = TRUE;
925                 add_visibility(found == TRUE);
926         }
927         OPARENT quote_fmt CPARENT
928         {
929                 remove_visibility();
930         }
931         | QUERY_ACCOUNT_FULL_NAME
932         {
933                 add_visibility(account != NULL && account->name != NULL);
934         }
935         OPARENT quote_fmt CPARENT
936         {
937                 remove_visibility();
938         }
939         | QUERY_ACCOUNT_ORGANIZATION
940         {
941                 add_visibility(account != NULL && account->organization != NULL);
942         }
943         OPARENT quote_fmt CPARENT
944         {
945                 remove_visibility();
946         }
947         | QUERY_ACCOUNT_DICT
948         {
949 #ifdef USE_ASPELL
950                 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
951                                 account->default_dictionary != NULL && *account->default_dictionary != '\0');
952 #else
953                 add_visibility(FALSE);
954 #endif
955         }
956         OPARENT quote_fmt CPARENT
957         {
958                 remove_visibility();
959         }
960         | QUERY_DICT
961         {
962 #ifdef USE_ASPELL
963                 add_visibility(*default_dictionary != '\0');
964 #else
965                 add_visibility(FALSE);
966 #endif
967         }
968         OPARENT quote_fmt CPARENT
969         {
970                 remove_visibility();
971         }
972         | QUERY_CC_FOUND_IN_ADDRESSBOOK
973         {
974                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
975                 add_visibility(tmp != NULL && *tmp != '\0');
976                 g_free(tmp);
977         }
978         OPARENT quote_fmt CPARENT
979         {
980                 remove_visibility();
981         }
982         | QUERY_FROM_FOUND_IN_ADDRESSBOOK
983         {
984                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
985                 add_visibility(tmp != NULL && *tmp != '\0');
986                 g_free(tmp);
987         }
988         OPARENT quote_fmt CPARENT
989         {
990                 remove_visibility();
991         }
992         | QUERY_TO_FOUND_IN_ADDRESSBOOK
993         {
994                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
995                 add_visibility(tmp != NULL && *tmp != '\0');
996                 g_free(tmp);
997         }
998         OPARENT quote_fmt CPARENT
999         {
1000                 remove_visibility();
1001         };
1002
1003 query_not:
1004         QUERY_NOT_DATE
1005         {
1006                 add_visibility(msginfo->date == NULL);
1007         }
1008         OPARENT quote_fmt CPARENT
1009         {
1010                 remove_visibility();
1011         }
1012         | QUERY_NOT_FROM
1013         {
1014                 add_visibility(msginfo->from == NULL);
1015         }
1016         OPARENT quote_fmt CPARENT
1017         {
1018                 remove_visibility();
1019         }
1020         | QUERY_NOT_FULLNAME
1021         {
1022                 add_visibility(msginfo->fromname == NULL);
1023         }
1024         OPARENT quote_fmt CPARENT
1025         {
1026                 remove_visibility();
1027         }
1028         | QUERY_NOT_SUBJECT
1029         {
1030                 add_visibility(msginfo->subject == NULL);
1031         }
1032         OPARENT quote_fmt CPARENT
1033         {
1034                 remove_visibility();
1035         }
1036         | QUERY_NOT_TO
1037         {
1038                 add_visibility(msginfo->to == NULL);
1039         }
1040         OPARENT quote_fmt CPARENT
1041         {
1042                 remove_visibility();
1043         }
1044         | QUERY_NOT_NEWSGROUPS
1045         {
1046                 add_visibility(msginfo->newsgroups == NULL);
1047         }
1048         OPARENT quote_fmt CPARENT
1049         {
1050                 remove_visibility();
1051         }
1052         | QUERY_NOT_MESSAGEID
1053         {
1054                 add_visibility(msginfo->msgid == NULL);
1055         }
1056         OPARENT quote_fmt CPARENT
1057         {
1058                 remove_visibility();
1059         }
1060         | QUERY_NOT_CC
1061         {
1062                 add_visibility(msginfo->cc == NULL);
1063         }
1064         OPARENT quote_fmt CPARENT
1065         {
1066                 remove_visibility();
1067         }
1068         | QUERY_NOT_REFERENCES
1069         {
1070                 gboolean found;
1071                 GSList *item;
1072
1073                 found = (msginfo->inreplyto != NULL);
1074                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1075                         if (item->data)
1076                                 found = TRUE;
1077                 add_visibility(found == FALSE);
1078         }
1079         OPARENT quote_fmt CPARENT
1080         {
1081                 remove_visibility();
1082         }
1083         | QUERY_NOT_ACCOUNT_FULL_NAME
1084         {
1085                 add_visibility(account == NULL || account->name == NULL);
1086         }
1087         OPARENT quote_fmt CPARENT
1088         {
1089                 remove_visibility();
1090         }
1091         | QUERY_NOT_ACCOUNT_ORGANIZATION
1092         {
1093                 add_visibility(account == NULL || account->organization == NULL);
1094         }
1095         OPARENT quote_fmt CPARENT
1096         {
1097                 remove_visibility();
1098         }
1099         | QUERY_NOT_ACCOUNT_DICT
1100         {
1101 #ifdef USE_ASPELL
1102                 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1103                                 || *account->default_dictionary == '\0');
1104 #else
1105                 add_visibility(FALSE);
1106 #endif
1107         }
1108         OPARENT quote_fmt CPARENT
1109         {
1110                 remove_visibility();
1111         }
1112         | QUERY_NOT_DICT
1113         {
1114 #ifdef USE_ASPELL
1115                 add_visibility(*default_dictionary == '\0');
1116 #else
1117                 add_visibility(FALSE);
1118 #endif
1119         }
1120         OPARENT quote_fmt CPARENT
1121         {
1122                 remove_visibility();
1123         }
1124         | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1125         {
1126                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1127                 add_visibility(tmp == NULL || *tmp == '\0');
1128                 g_free(tmp);
1129         }
1130         OPARENT quote_fmt CPARENT
1131         {
1132                 remove_visibility();
1133         }
1134         | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1135         {
1136                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1137                 add_visibility(tmp == NULL || *tmp == '\0');
1138                 g_free(tmp);
1139         }
1140         OPARENT quote_fmt CPARENT
1141         {
1142                 remove_visibility();
1143         }
1144         | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1145         {
1146                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1147                 add_visibility(tmp == NULL || *tmp == '\0');
1148                 g_free(tmp);
1149         }
1150         OPARENT quote_fmt CPARENT
1151         {
1152                 remove_visibility();
1153         };
1154
1155 insert:
1156         INSERT_FILE
1157         {
1158                 current = &sub_expr;
1159                 clear_buffer();
1160         }
1161         OPARENT sub_expr CPARENT
1162         {
1163                 current = &main_expr;
1164                 if (!dry_run) {
1165                         quote_fmt_insert_file(sub_expr.buffer);
1166                 }
1167         }
1168         | INSERT_PROGRAMOUTPUT
1169         {
1170                 current = &sub_expr;
1171                 clear_buffer();
1172         }
1173         OPARENT sub_expr CPARENT
1174         {
1175                 current = &main_expr;
1176                 if (!dry_run) {
1177                         quote_fmt_insert_program_output(sub_expr.buffer);
1178                 }
1179         }
1180         | INSERT_USERINPUT
1181         {
1182                 current = &sub_expr;
1183                 clear_buffer();
1184         }
1185         OPARENT sub_expr CPARENT
1186         {
1187                 current = &main_expr;
1188                 if (!dry_run) {
1189                         quote_fmt_insert_user_input(sub_expr.buffer);
1190                 }
1191         };