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