change gtk_scrolled_window_set_policy from
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtktreeview.h>
51
52 #include <gtk/gtkdnd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #include <time.h>
61 /* #include <sys/utsname.h> */
62 #include <stdlib.h>
63 #include <sys/wait.h>
64 #include <signal.h>
65 #include <errno.h>
66 #include <libgen.h>
67
68 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
69 #  include <wchar.h>
70 #  include <wctype.h>
71 #endif
72
73 #include "intl.h"
74 #include "main.h"
75 #include "mainwindow.h"
76 #include "compose.h"
77 #include "addressbook.h"
78 #include "folderview.h"
79 #include "procmsg.h"
80 #include "menu.h"
81 #include "stock_pixmap.h"
82 #include "send_message.h"
83 #include "imap.h"
84 #include "news.h"
85 #include "customheader.h"
86 #include "prefs_common.h"
87 #include "prefs_account.h"
88 #include "action.h"
89 #include "account.h"
90 #include "filesel.h"
91 #include "procheader.h"
92 #include "procmime.h"
93 #include "statusbar.h"
94 #include "about.h"
95 #include "base64.h"
96 #include "quoted-printable.h"
97 #include "codeconv.h"
98 #include "utils.h"
99 #include "gtkutils.h"
100 #include "socket.h"
101 #include "alertpanel.h"
102 #include "manage_window.h"
103 #include "gtkshruler.h"
104 #include "folder.h"
105 #include "addr_compl.h"
106 #include "quote_fmt.h"
107 #include "template.h"
108 #include "undo.h"
109 #include "foldersel.h"
110 #include "toolbar.h"
111
112 #if USE_GPGME
113 #  include "rfc2015.h"
114 #endif
115
116 typedef enum
117 {
118         COL_MIMETYPE = 0,
119         COL_SIZE     = 1,
120         COL_NAME     = 2
121 } AttachColumnPos;
122
123 #define N_ATTACH_COLS           3
124
125 typedef enum
126 {
127         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
128         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
129         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
130         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
131         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
132         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
133         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
134         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
135         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
136         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
137         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
138         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
139         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
140         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
141         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END
142 } ComposeCallAdvancedAction;
143
144 typedef enum
145 {
146         PRIORITY_HIGHEST = 1,
147         PRIORITY_HIGH,
148         PRIORITY_NORMAL,
149         PRIORITY_LOW,
150         PRIORITY_LOWEST
151 } PriorityLevel;
152
153 typedef enum
154 {
155         COMPOSE_INSERT_SUCCESS,
156         COMPOSE_INSERT_READ_ERROR,
157         COMPOSE_INSERT_INVALID_CHARACTER,
158         COMPOSE_INSERT_NO_FILE
159 } ComposeInsertResult;
160
161 #define B64_LINE_SIZE           57
162 #define B64_BUFFSIZE            77
163
164 #define MAX_REFERENCES_LEN      999
165
166 static GdkColor quote_color = {0, 0, 0, 0xbfff};
167
168 static GList *compose_list = NULL;
169
170 Compose *compose_generic_new                    (PrefsAccount   *account,
171                                                  const gchar    *to,
172                                                  FolderItem     *item,
173                                                  GPtrArray      *attach_files,
174                                                  GList          *listAddress );
175
176 static Compose *compose_create                  (PrefsAccount   *account,
177                                                  ComposeMode     mode);
178
179 static GtkWidget *compose_account_option_menu_create
180                                                 (Compose        *compose);
181 static void compose_set_template_menu           (Compose        *compose);
182 static void compose_template_apply              (Compose        *compose,
183                                                  Template       *tmpl,
184                                                  gboolean        replace);
185 static void compose_destroy                     (Compose        *compose);
186
187 static void compose_entries_set                 (Compose        *compose,
188                                                  const gchar    *mailto);
189 static gint compose_parse_header                (Compose        *compose,
190                                                  MsgInfo        *msginfo);
191 static gchar *compose_parse_references          (const gchar    *ref,
192                                                  const gchar    *msgid);
193
194 static gchar *compose_quote_fmt                 (Compose        *compose,
195                                                  MsgInfo        *msginfo,
196                                                  const gchar    *fmt,
197                                                  const gchar    *qmark,
198                                                  const gchar    *body);
199
200 static void compose_reply_set_entry             (Compose        *compose,
201                                                  MsgInfo        *msginfo,
202                                                  gboolean        to_all,
203                                                  gboolean        to_ml,
204                                                  gboolean        to_sender,
205                                                  gboolean
206                                                  followup_and_reply_to);
207 static void compose_reedit_set_entry            (Compose        *compose,
208                                                  MsgInfo        *msginfo);
209 static void compose_insert_sig                  (Compose        *compose,
210                                                  gboolean        replace);
211 static gchar *compose_get_signature_str         (Compose        *compose);
212 static ComposeInsertResult compose_insert_file  (Compose        *compose,
213                                                  const gchar    *file);
214 static void compose_attach_append               (Compose        *compose,
215                                                  const gchar    *file,
216                                                  const gchar    *type,
217                                                  const gchar    *content_type);
218 static void compose_attach_parts                (Compose        *compose,
219                                                  MsgInfo        *msginfo);
220 static void compose_wrap_line                   (Compose        *compose);
221 static void compose_wrap_line_all               (Compose        *compose);
222 static void compose_wrap_line_all_full          (Compose        *compose,
223                                                  gboolean        autowrap);
224 static void compose_set_title                   (Compose        *compose);
225 static void compose_select_account              (Compose        *compose,
226                                                  PrefsAccount   *account,
227                                                  gboolean        init);
228
229 static PrefsAccount *compose_current_mail_account(void);
230 /* static gint compose_send                     (Compose        *compose); */
231 static gboolean compose_check_for_valid_recipient
232                                                 (Compose        *compose);
233 static gboolean compose_check_entries           (Compose        *compose,
234                                                  gboolean       check_subject);
235 static gint compose_write_to_file               (Compose        *compose,
236                                                  const gchar    *file,
237                                                  gboolean        is_draft);
238 static gint compose_write_body_to_file          (Compose        *compose,
239                                                  const gchar    *file);
240 static gint compose_remove_reedit_target        (Compose        *compose);
241 void compose_remove_draft                       (Compose        *compose);
242 static gint compose_queue                       (Compose        *compose,
243                                                  gint           *msgnum,
244                                                  FolderItem     **item);
245 static gint compose_queue_sub                   (Compose        *compose,
246                                                  gint           *msgnum,
247                                                  FolderItem     **item,
248                                                  gboolean       check_subject);
249 static void compose_write_attach                (Compose        *compose,
250                                                  FILE           *fp);
251 static gint compose_write_headers               (Compose        *compose,
252                                                  FILE           *fp,
253                                                  const gchar    *charset,
254                                                  EncodingType    encoding,
255                                                  gboolean        is_draft);
256
257 static void compose_convert_header              (gchar          *dest,
258                                                  gint            len,
259                                                  gchar          *src,
260                                                  gint            header_len,
261                                                  gboolean        addr_field);
262 static void compose_generate_msgid              (gchar          *buf,
263                                                  gint            len);
264
265 static void compose_attach_info_free            (AttachInfo     *ainfo);
266 static void compose_attach_remove_selected      (Compose        *compose);
267
268 static void compose_attach_property             (Compose        *compose);
269 static void compose_attach_property_create      (gboolean       *cancelled);
270 static void attach_property_ok                  (GtkWidget      *widget,
271                                                  gboolean       *cancelled);
272 static void attach_property_cancel              (GtkWidget      *widget,
273                                                  gboolean       *cancelled);
274 static gint attach_property_delete_event        (GtkWidget      *widget,
275                                                  GdkEventAny    *event,
276                                                  gboolean       *cancelled);
277 static gboolean attach_property_key_pressed     (GtkWidget      *widget,
278                                                  GdkEventKey    *event,
279                                                  gboolean       *cancelled);
280
281 static void compose_exec_ext_editor             (Compose           *compose);
282 static gint compose_exec_ext_editor_real        (const gchar       *file);
283 static gboolean compose_ext_editor_kill         (Compose           *compose);
284 static void compose_input_cb                    (gpointer           data,
285                                                  gint               source,
286                                                  GdkInputCondition  condition);
287 static void compose_set_ext_editor_sensitive    (Compose           *compose,
288                                                  gboolean           sensitive);
289
290 static void compose_undo_state_changed          (UndoMain       *undostruct,
291                                                  gint            undo_state,
292                                                  gint            redo_state,
293                                                  gpointer        data);
294
295 static gint calc_cursor_xpos    (GtkTextView    *text,
296                                  gint            extra,
297                                  gint            char_width);
298
299 static void compose_create_header_entry (Compose *compose);
300 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
301 static void compose_update_priority_menu_item(Compose * compose);
302
303 static void compose_add_field_list      ( Compose *compose,
304                                           GList *listAddress );
305
306 /* callback functions */
307
308 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
309                                          GtkAllocation  *allocation,
310                                          GtkSHRuler     *shruler);
311 static void account_activated           (GtkMenuItem    *menuitem,
312                                          gpointer        data);
313 static void attach_selected             (GtkCList       *clist,
314                                          gint            row,
315                                          gint            column,
316                                          GdkEvent       *event,
317                                          gpointer        data);
318 static gboolean attach_button_pressed   (GtkWidget      *widget,
319                                          GdkEventButton *event,
320                                          gpointer        data);
321 static gboolean attach_key_pressed      (GtkWidget      *widget,
322                                          GdkEventKey    *event,
323                                          gpointer        data);
324
325 static void compose_send_cb             (gpointer        data,
326                                          guint           action,
327                                          GtkWidget      *widget);
328 static void compose_send_later_cb       (gpointer        data,
329                                          guint           action,
330                                          GtkWidget      *widget);
331
332 static void compose_draft_cb            (gpointer        data,
333                                          guint           action,
334                                          GtkWidget      *widget);
335
336 static void compose_attach_cb           (gpointer        data,
337                                          guint           action,
338                                          GtkWidget      *widget);
339 static void compose_insert_file_cb      (gpointer        data,
340                                          guint           action,
341                                          GtkWidget      *widget);
342 static void compose_insert_sig_cb       (gpointer        data,
343                                          guint           action,
344                                          GtkWidget      *widget);
345
346 static void compose_close_cb            (gpointer        data,
347                                          guint           action,
348                                          GtkWidget      *widget);
349
350 static void compose_address_cb          (gpointer        data,
351                                          guint           action,
352                                          GtkWidget      *widget);
353 static void compose_template_activate_cb(GtkWidget      *widget,
354                                          gpointer        data);
355
356 static void compose_ext_editor_cb       (gpointer        data,
357                                          guint           action,
358                                          GtkWidget      *widget);
359
360 static gint compose_delete_cb           (GtkWidget      *widget,
361                                          GdkEventAny    *event,
362                                          gpointer        data);
363 static void compose_destroy_cb          (GtkWidget      *widget,
364                                          Compose        *compose);
365
366 static void compose_undo_cb             (Compose        *compose);
367 static void compose_redo_cb             (Compose        *compose);
368 static void compose_cut_cb              (Compose        *compose);
369 static void compose_copy_cb             (Compose        *compose);
370 static void compose_paste_cb            (Compose        *compose);
371 static void compose_paste_as_quote_cb   (Compose        *compose);
372 static void compose_allsel_cb           (Compose        *compose);
373
374 static void compose_advanced_action_cb  (Compose                   *compose,
375                                          ComposeCallAdvancedAction  action);
376
377 static gboolean compose_grab_focus_cb   (GtkWidget      *widget,
378                                          Compose        *compose);
379 static gboolean compose_grab_focus_before_cb    (GtkWidget      *widget,
380                                          Compose        *compose);
381
382 static void compose_changed_cb          (GtkTextBuffer  *textbuf,
383                                          Compose        *compose);
384
385 static void compose_toggle_autowrap_cb  (gpointer        data,
386                                          guint           action,
387                                          GtkWidget      *widget);
388
389 #if 0
390 static void compose_toggle_to_cb        (gpointer        data,
391                                          guint           action,
392                                          GtkWidget      *widget);
393 static void compose_toggle_cc_cb        (gpointer        data,
394                                          guint           action,
395                                          GtkWidget      *widget);
396 static void compose_toggle_bcc_cb       (gpointer        data,
397                                          guint           action,
398                                          GtkWidget      *widget);
399 static void compose_toggle_replyto_cb   (gpointer        data,
400                                          guint           action,
401                                          GtkWidget      *widget);
402 static void compose_toggle_followupto_cb(gpointer        data,
403                                          guint           action,
404                                          GtkWidget      *widget);
405 static void compose_toggle_attach_cb    (gpointer        data,
406                                          guint           action,
407                                          GtkWidget      *widget);
408 #endif
409 static void compose_toggle_ruler_cb     (gpointer        data,
410                                          guint           action,
411                                          GtkWidget      *widget);
412 #if USE_GPGME
413 static void compose_toggle_sign_cb      (gpointer        data,
414                                          guint           action,
415                                          GtkWidget      *widget);
416 static void compose_toggle_encrypt_cb   (gpointer        data,
417                                          guint           action,
418                                          GtkWidget      *widget);
419 static void compose_set_gnupg_mode_cb   (gpointer        data,
420                                          guint           action,
421                                          GtkWidget      *widget);
422 static void compose_update_gnupg_mode_menu_item(Compose * compose);
423 static void activate_gnupg_mode         (Compose *compose, 
424                                          PrefsAccount *account);
425 #endif
426 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
427                                              GtkWidget *widget);
428 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
429                                              GtkWidget *widget);
430 static void compose_set_priority_cb     (gpointer        data,
431                                          guint           action,
432                                          GtkWidget      *widget);
433
434 static void compose_attach_drag_received_cb (GtkWidget          *widget,
435                                              GdkDragContext     *drag_context,
436                                              gint                x,
437                                              gint                y,
438                                              GtkSelectionData   *data,
439                                              guint               info,
440                                              guint               time,
441                                              gpointer            user_data);
442 static void compose_insert_drag_received_cb (GtkWidget          *widget,
443                                              GdkDragContext     *drag_context,
444                                              gint                x,
445                                              gint                y,
446                                              GtkSelectionData   *data,
447                                              guint               info,
448                                              guint               time,
449                                              gpointer            user_data);
450
451 #if 0
452 static void to_activated                (GtkWidget      *widget,
453                                          Compose        *compose);
454 static void newsgroups_activated        (GtkWidget      *widget,
455                                          Compose        *compose);
456 static void cc_activated                (GtkWidget      *widget,
457                                          Compose        *compose);
458 static void bcc_activated               (GtkWidget      *widget,
459                                          Compose        *compose);
460 static void replyto_activated           (GtkWidget      *widget,
461                                          Compose        *compose);
462 static void followupto_activated        (GtkWidget      *widget,
463                                          Compose        *compose);
464 static void subject_activated           (GtkWidget      *widget,
465                                          Compose        *compose);
466 #endif
467
468 static void text_activated              (GtkWidget      *widget,
469                                          Compose        *compose);
470 static void text_inserted               (GtkTextBuffer  *buffer,
471                                          GtkTextIter    *iter,
472                                          const gchar    *text,
473                                          gint            len,
474                                          Compose        *compose);
475 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
476                                   gboolean to_all, gboolean to_ml,
477                                   gboolean to_sender,
478                                   gboolean followup_and_reply_to,
479                                   const gchar *body);
480
481 gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
482                                             ComposeHeaderEntry *headerentry);
483 gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
484                                             GdkEventKey        *event,
485                                             ComposeHeaderEntry *headerentry);
486 static gboolean compose_headerentry_button_pressed (GtkWidget *entry, 
487                                                     GdkEventButton *event,
488                                                     gpointer data);
489
490 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
491
492 #if USE_ASPELL
493 static void compose_check_all              (Compose *compose);
494 static void compose_highlight_all          (Compose *compose);
495 static void compose_check_backwards        (Compose *compose);
496 static void compose_check_forwards_go      (Compose *compose);
497 #endif
498
499 static gboolean compose_send_control_enter      (Compose        *compose);
500 static gint compose_defer_auto_save_draft       (Compose        *compose);
501 static PrefsAccount *compose_guess_forward_account_from_msginfo (MsgInfo *msginfo);
502
503 static GtkItemFactoryEntry compose_popup_entries[] =
504 {
505         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
506         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
507         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
508         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
509 };
510
511 static GtkItemFactoryEntry compose_entries[] =
512 {
513         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
514         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
515         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
516         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig_cb,  0, NULL},
517         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
518         {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
519
520         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
521         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
522         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
523         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
524         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
525         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
526         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
527         {N_("/_Edit/Paste as _quotation"),
528                                         NULL, compose_paste_as_quote_cb, 0, NULL},
529         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
530         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
531         {N_("/_Edit/A_dvanced/Move a character backward"),
532                                         "<control>B",
533                                         compose_advanced_action_cb,
534                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
535                                         NULL},
536         {N_("/_Edit/A_dvanced/Move a character forward"),
537                                         "<control>F",
538                                         compose_advanced_action_cb,
539                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
540                                         NULL},
541         {N_("/_Edit/A_dvanced/Move a word backward"),
542                                         NULL, /* "<alt>B" */
543                                         compose_advanced_action_cb,
544                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
545                                         NULL},
546         {N_("/_Edit/A_dvanced/Move a word forward"),
547                                         NULL, /* "<alt>F" */
548                                         compose_advanced_action_cb,
549                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
550                                         NULL},
551         {N_("/_Edit/A_dvanced/Move to beginning of line"),
552                                         NULL, /* "<control>A" */
553                                         compose_advanced_action_cb,
554                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
555                                         NULL},
556         {N_("/_Edit/A_dvanced/Move to end of line"),
557                                         "<control>E",
558                                         compose_advanced_action_cb,
559                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
560                                         NULL},
561         {N_("/_Edit/A_dvanced/Move to previous line"),
562                                         "<control>P",
563                                         compose_advanced_action_cb,
564                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
565                                         NULL},
566         {N_("/_Edit/A_dvanced/Move to next line"),
567                                         "<control>N",
568                                         compose_advanced_action_cb,
569                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
570                                         NULL},
571         {N_("/_Edit/A_dvanced/Delete a character backward"),
572                                         "<control>H",
573                                         compose_advanced_action_cb,
574                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
575                                         NULL},
576         {N_("/_Edit/A_dvanced/Delete a character forward"),
577                                         "<control>D",
578                                         compose_advanced_action_cb,
579                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
580                                         NULL},
581         {N_("/_Edit/A_dvanced/Delete a word backward"),
582                                         NULL, /* "<control>W" */
583                                         compose_advanced_action_cb,
584                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
585                                         NULL},
586         {N_("/_Edit/A_dvanced/Delete a word forward"),
587                                         NULL, /* "<alt>D", */
588                                         compose_advanced_action_cb,
589                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
590                                         NULL},
591         {N_("/_Edit/A_dvanced/Delete line"),
592                                         "<control>U",
593                                         compose_advanced_action_cb,
594                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
595                                         NULL},
596         {N_("/_Edit/A_dvanced/Delete entire line"),
597                                         NULL,
598                                         compose_advanced_action_cb,
599                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
600                                         NULL},
601         {N_("/_Edit/A_dvanced/Delete to end of line"),
602                                         "<control>K",
603                                         compose_advanced_action_cb,
604                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END,
605                                         NULL},
606         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
607         {N_("/_Edit/_Wrap current paragraph"),
608                                         "<control>L", compose_wrap_line, 0, NULL},
609         {N_("/_Edit/Wrap all long _lines"),
610                                         "<control><alt>L", compose_wrap_line_all, 0, NULL},
611         {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
612         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
613         {N_("/_Edit/Edit with e_xternal editor"),
614                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
615 #if USE_ASPELL
616         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
617         {N_("/_Spelling/_Check all or check selection"),
618                                         NULL, compose_check_all, 0, NULL},
619         {N_("/_Spelling/_Highlight all misspelled words"),
620                                         NULL, compose_highlight_all, 0, NULL},
621         {N_("/_Spelling/Check _backwards misspelled word"),
622                                         NULL, compose_check_backwards , 0, NULL},
623         {N_("/_Spelling/_Forward to next misspelled word"),
624                                         NULL, compose_check_forwards_go, 0, NULL},
625         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
626         {N_("/_Spelling/_Spelling Configuration"),
627                                         NULL, NULL, 0, "<Branch>"},
628 #endif
629 #if 0 /* NEW COMPOSE GUI */
630         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
631         {N_("/_View/_To"),              NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
632         {N_("/_View/_Cc"),              NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
633         {N_("/_View/_Bcc"),             NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
634         {N_("/_View/_Reply to"),        NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
635         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
636         {N_("/_View/_Followup to"),     NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
637         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
638         {N_("/_View/R_uler"),           NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
639         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
640         {N_("/_View/_Attachment"),      NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
641 #endif
642         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
643         {N_("/_Message/_Send"),         "<control>Return",
644                                         compose_send_cb, 0, NULL},
645         {N_("/_Message/Send _later"),   "<shift><control>S",
646                                         compose_send_later_cb,  0, NULL},
647         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
648         {N_("/_Message/Save to _draft folder"),
649                                         "<shift><control>D", compose_draft_cb, 0, NULL},
650         {N_("/_Message/Save and _keep editing"),
651                                         "<control>S", compose_draft_cb, 1, NULL},
652 #if 0 /* NEW COMPOSE GUI */
653         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
654         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
655         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
656         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
657         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
658         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
659         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
660         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
661         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
662 #endif
663 #if USE_GPGME
664         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
665         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
666         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
667         {N_("/_Message/Mode"),          NULL, NULL,   0, "<Branch>"},
668         {N_("/_Message/Mode/MIME"),     NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_DETACH, "<RadioItem>"},   
669         {N_("/_Message/Mode/Inline"),   NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_INLINE, "/Message/Mode/MIME"},    
670 #endif /* USE_GPGME */
671         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
672         {N_("/_Message/_Priority"),     NULL,           NULL,   0, "<Branch>"},
673         {N_("/_Message/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
674         {N_("/_Message/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
675         {N_("/_Message/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
676         {N_("/_Message/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
677         {N_("/_Message/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
678         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
679         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
680         {N_("/_Message/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
681         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
682         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
683         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
684         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
685         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
686         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
687         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
688 };
689
690 static GtkTargetEntry compose_mime_types[] =
691 {
692         {"text/uri-list", 0, 0}
693 };
694
695 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
696                      GPtrArray *attach_files)
697 {
698         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
699 }
700
701 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
702 {
703         return compose_generic_new(account, NULL, item, NULL, NULL);
704 }
705
706 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
707 {
708         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
709 }
710
711 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
712                              GPtrArray *attach_files, GList *listAddress )
713 {
714         Compose *compose;
715         GtkTextView *textview;
716         GtkTextBuffer *textbuf;
717         GtkTextIter iter;
718         GtkItemFactory *ifactory;
719
720         if (item && item->prefs && item->prefs->enable_default_account)
721                 account = account_find_from_id(item->prefs->default_account);
722
723         if (!account) account = cur_account;
724         g_return_val_if_fail(account != NULL, NULL);
725
726         compose = compose_create(account, COMPOSE_NEW);
727         ifactory = gtk_item_factory_from_widget(compose->menubar);
728
729         compose->replyinfo = NULL;
730         compose->fwdinfo   = NULL;
731
732         textview = GTK_TEXT_VIEW(compose->text);
733         textbuf = gtk_text_view_get_buffer(textview);
734
735         undo_block(compose->undostruct);
736 #ifdef USE_ASPELL
737         if (item && item->prefs && item->prefs->enable_default_dictionary &&
738             compose->gtkaspell) 
739                 gtkaspell_change_dict(compose->gtkaspell, 
740                     item->prefs->default_dictionary);
741 #endif
742
743         if (account->auto_sig)
744                 compose_insert_sig(compose, FALSE);
745         gtk_text_buffer_get_start_iter(textbuf, &iter);
746         gtk_text_buffer_place_cursor(textbuf, &iter);
747
748         if (account->protocol != A_NNTP) {
749                 if (mailto && *mailto != '\0') {
750                         compose_entries_set(compose, mailto);
751
752                 } else if (item && item->prefs->enable_default_to) {
753                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
754                         compose_entry_mark_default_to(compose, item->prefs->default_to);
755                 }
756                 if (item && item->ret_rcpt) {
757                         menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
758                 }
759         } else {
760                 if (mailto) {
761                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
762                 }
763                 /*
764                  * CLAWS: just don't allow return receipt request, even if the user
765                  * may want to send an email. simple but foolproof.
766                  */
767                 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE); 
768         }
769         compose_add_field_list( compose, listAddress );
770
771         if (attach_files) {
772                 gint i;
773                 gchar *file;
774
775                 for (i = 0; i < attach_files->len; i++) {
776                         file = g_ptr_array_index(attach_files, i);
777                         compose_attach_append(compose, file, file, NULL);
778                 }
779         }
780
781         compose_show_first_last_header(compose, TRUE);
782
783         /* Set save folder */
784         if (item && item->prefs && item->prefs->save_copy_to_folder) {
785                 gchar *folderidentifier;
786
787                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
788                 folderidentifier = folder_item_get_identifier(item);
789                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
790                 g_free(folderidentifier);
791         }
792         
793         gtk_widget_grab_focus(compose->header_last->entry);
794
795         undo_unblock(compose->undostruct);
796
797         if (prefs_common.auto_exteditor)
798                 compose_exec_ext_editor(compose);
799
800         return compose;
801 }
802
803 /*
804 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
805                                            const gchar *followupto, gchar * to)
806 {
807         Compose *compose;
808
809         if (!account) account = cur_account;
810         g_return_val_if_fail(account != NULL, NULL);
811         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
812
813         compose = compose_create(account, COMPOSE_NEW);
814
815         if (prefs_common.auto_sig)
816                 compose_insert_sig(compose);
817         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
818         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
819
820         compose_entry_append(compose, to, COMPOSE_TO);
821         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
822         gtk_widget_grab_focus(compose->subject_entry);
823
824         return compose;
825 }
826 */
827
828 void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
829 {
830         MsgInfo *msginfo;
831         guint list_len;
832
833         g_return_if_fail(msginfo_list != NULL);
834
835         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
836         g_return_if_fail(msginfo != NULL);
837
838         list_len = g_slist_length(msginfo_list);
839
840         switch (mode) {
841         case COMPOSE_REPLY:
842                 compose_reply(msginfo, prefs_common.reply_with_quote,
843                               FALSE, prefs_common.default_reply_list, FALSE, body);
844                 break;
845         case COMPOSE_REPLY_WITH_QUOTE:
846                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
847                 break;
848         case COMPOSE_REPLY_WITHOUT_QUOTE:
849                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
850                 break;
851         case COMPOSE_REPLY_TO_SENDER:
852                 compose_reply(msginfo, prefs_common.reply_with_quote,
853                               FALSE, FALSE, TRUE, body);
854                 break;
855         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
856                 compose_followup_and_reply_to(msginfo,
857                                               prefs_common.reply_with_quote,
858                                               FALSE, FALSE, body);
859                 break;
860         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
861                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
862                 break;
863         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
864                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
865                 break;
866         case COMPOSE_REPLY_TO_ALL:
867                 compose_reply(msginfo, prefs_common.reply_with_quote,
868                               TRUE, FALSE, FALSE, body);
869                 break;
870         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
871                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
872                 break;
873         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
874                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
875                 break;
876         case COMPOSE_REPLY_TO_LIST:
877                 compose_reply(msginfo, prefs_common.reply_with_quote,
878                               FALSE, TRUE, FALSE, body);
879                 break;
880         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
881                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
882                 break;
883         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
884                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
885                 break;
886         case COMPOSE_FORWARD:
887                 if (prefs_common.forward_as_attachment) {
888                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
889                         return;
890                 } else {
891                         compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
892                         return;
893                 }
894                 break;
895         case COMPOSE_FORWARD_INLINE:
896                 /* check if we reply to more than one Message */
897                 if (list_len == 1) {
898                         compose_forward(NULL, msginfo, FALSE, body, FALSE);
899                         break;
900                 } 
901                 /* more messages FALL THROUGH */
902         case COMPOSE_FORWARD_AS_ATTACH:
903                 compose_forward_multiple(NULL, msginfo_list);
904                 break;
905         case COMPOSE_REDIRECT:
906                 compose_redirect(NULL, msginfo);
907                 break;
908         default:
909                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
910         }
911 }
912
913 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
914                    gboolean to_ml, gboolean to_sender, 
915                    const gchar *body)
916 {
917         compose_generic_reply(msginfo, quote, to_all, to_ml, 
918                               to_sender, FALSE, body);
919 }
920
921 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
922                                    gboolean to_all,
923                                    gboolean to_sender,
924                                    const gchar *body)
925 {
926         compose_generic_reply(msginfo, quote, to_all, FALSE, 
927                               to_sender, TRUE, body);
928 }
929
930 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
931                                   gboolean to_all, gboolean to_ml,
932                                   gboolean to_sender,
933                                   gboolean followup_and_reply_to,
934                                   const gchar *body)
935 {
936         GtkItemFactory *ifactory;
937         Compose *compose;
938         PrefsAccount *account = NULL;
939         PrefsAccount *reply_account;
940         GtkTextView *textview;
941         GtkTextBuffer *textbuf;
942         GtkTextIter iter;
943         int cursor_pos;
944
945         g_return_if_fail(msginfo != NULL);
946         g_return_if_fail(msginfo->folder != NULL);
947
948         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
949         
950         g_return_if_fail(account != NULL);
951
952         if (to_sender && account->protocol == A_NNTP &&
953             !followup_and_reply_to) {
954                 reply_account =
955                         account_find_from_address(account->address);
956                 if (!reply_account)
957                         reply_account = compose_current_mail_account();
958                 if (!reply_account)
959                         return;
960         } else
961                 reply_account = account;
962
963         compose = compose_create(account, COMPOSE_REPLY);
964         ifactory = gtk_item_factory_from_widget(compose->menubar);
965
966         menu_set_active(ifactory, "/Message/Remove references", FALSE);
967         menu_set_sensitive(ifactory, "/Message/Remove references", TRUE);
968
969         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
970         if (!compose->replyinfo)
971                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
972
973         if (msginfo->folder && msginfo->folder->ret_rcpt)
974                 menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
975
976         /* Set save folder */
977         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
978                 gchar *folderidentifier;
979
980                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
981                 folderidentifier = folder_item_get_identifier(msginfo->folder);
982                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
983                 g_free(folderidentifier);
984         }
985
986         if (compose_parse_header(compose, msginfo) < 0) return;
987         compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
988                                 to_sender, followup_and_reply_to);
989         compose_show_first_last_header(compose, TRUE);
990
991         textview = (GTK_TEXT_VIEW(compose->text));
992         textbuf = gtk_text_view_get_buffer(textview);
993         
994         undo_block(compose->undostruct);
995 #ifdef USE_ASPELL
996         if (msginfo->folder && msginfo->folder->prefs && 
997             msginfo->folder->prefs && 
998             msginfo->folder->prefs->enable_default_dictionary &&
999             compose->gtkaspell)
1000                 gtkaspell_change_dict(compose->gtkaspell, 
1001                     msginfo->folder->prefs->default_dictionary);
1002 #endif
1003
1004         if (quote) {
1005                 gchar *qmark;
1006
1007                 if (prefs_common.quotemark && *prefs_common.quotemark)
1008                         qmark = prefs_common.quotemark;
1009                 else
1010                         qmark = "> ";
1011
1012                 compose_quote_fmt(compose, compose->replyinfo,
1013                                   prefs_common.quotefmt,
1014                                   qmark, body);
1015         }
1016
1017         if (account->auto_sig)
1018                 compose_insert_sig(compose, FALSE);
1019
1020         if (quote && prefs_common.linewrap_quote)
1021                 compose_wrap_line_all(compose);
1022
1023         cursor_pos = quote_fmt_get_cursor_pos();
1024         gtk_text_buffer_get_start_iter(textbuf, &iter);
1025         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cursor_pos);
1026         gtk_text_buffer_place_cursor(textbuf, &iter);
1027
1028         if (quote && prefs_common.linewrap_quote) {
1029                 compose_wrap_line_all(compose);
1030                 gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1031         }
1032
1033         gtk_widget_grab_focus(compose->text);
1034
1035         undo_unblock(compose->undostruct);
1036
1037         if (prefs_common.auto_exteditor)
1038                 compose_exec_ext_editor(compose);
1039 }
1040
1041 #define INSERT_FW_HEADER(var, hdr) \
1042 if (msginfo->var && *msginfo->var) { \
1043         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1044         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1045         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1046 }
1047
1048 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1049                          gboolean as_attach, const gchar *body,
1050                          gboolean no_extedit)
1051 {
1052         Compose *compose;
1053         GtkTextView *textview;
1054         GtkTextBuffer *textbuf;
1055         GtkTextIter iter;
1056
1057         g_return_val_if_fail(msginfo != NULL, NULL);
1058         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1059
1060         if (!account && 
1061             !(account = compose_guess_forward_account_from_msginfo
1062                                 (msginfo)))
1063                 account = cur_account;
1064
1065         compose = compose_create(account, COMPOSE_FORWARD);
1066
1067         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1068         if (!compose->fwdinfo)
1069                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1070
1071         if (msginfo->subject && *msginfo->subject) {
1072                 gchar *buf, *buf2, *p;
1073
1074                 buf = p = g_strdup(msginfo->subject);
1075                 p += subject_get_prefix_length(p);
1076                 memmove(buf, p, strlen(p) + 1);
1077
1078                 buf2 = g_strdup_printf("Fw: %s", buf);
1079                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1080                 
1081                 g_free(buf);
1082                 g_free(buf2);
1083         }
1084
1085         textview = GTK_TEXT_VIEW(compose->text);
1086         textbuf = gtk_text_view_get_buffer(textview);
1087
1088         if (as_attach) {
1089                 gchar *msgfile;
1090
1091                 msgfile = procmsg_get_message_file_path(msginfo);
1092                 if (!is_file_exist(msgfile))
1093                         g_warning("%s: file not exist\n", msgfile);
1094                 else
1095                         compose_attach_append(compose, msgfile, msgfile,
1096                                               "message/rfc822");
1097
1098                 g_free(msgfile);
1099         } else {
1100                 gchar *qmark;
1101                 MsgInfo *full_msginfo;
1102
1103                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1104                 if (!full_msginfo)
1105                         full_msginfo = procmsg_msginfo_copy(msginfo);
1106
1107                 if (prefs_common.fw_quotemark &&
1108                     *prefs_common.fw_quotemark)
1109                         qmark = prefs_common.fw_quotemark;
1110                 else
1111                         qmark = "> ";
1112
1113                 compose_quote_fmt(compose, full_msginfo,
1114                                   prefs_common.fw_quotefmt,
1115                                   qmark, body);
1116                 compose_attach_parts(compose, msginfo);
1117
1118                 procmsg_msginfo_free(full_msginfo);
1119         }
1120
1121         if (account->auto_sig)
1122                 compose_insert_sig(compose, FALSE);
1123
1124         if (prefs_common.linewrap_quote)
1125                 compose_wrap_line_all(compose);
1126
1127         gtk_text_buffer_get_start_iter(textbuf, &iter);
1128         gtk_text_buffer_place_cursor(textbuf, &iter);
1129
1130 #if 0 /* NEW COMPOSE GUI */
1131         if (account->protocol != A_NNTP)
1132                 gtk_widget_grab_focus(compose->to_entry);
1133         else
1134                 gtk_widget_grab_focus(compose->newsgroups_entry);
1135 #endif
1136         gtk_widget_grab_focus(compose->header_last->entry);
1137
1138         if (!no_extedit && prefs_common.auto_exteditor)
1139                 compose_exec_ext_editor(compose);
1140         
1141         /*save folder*/
1142         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1143                 gchar *folderidentifier;
1144
1145                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1146                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1147                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1148                 g_free(folderidentifier);
1149         }
1150
1151         return compose;
1152 }
1153
1154 #undef INSERT_FW_HEADER
1155
1156 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1157 {
1158         Compose *compose;
1159         GtkTextView *textview;
1160         GtkTextBuffer *textbuf;
1161         GtkTextIter iter;
1162         GSList *msginfo;
1163         gchar *msgfile;
1164
1165         g_return_val_if_fail(msginfo_list != NULL, NULL);
1166
1167         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1168                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1169                         return NULL;
1170
1171         /* guess account from first selected message */
1172         if (!account && 
1173             !(account = compose_guess_forward_account_from_msginfo
1174                                 (msginfo_list->data)))
1175                 account = cur_account;
1176
1177         g_return_val_if_fail(account != NULL, NULL);
1178
1179         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1180                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1181                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1182         }
1183
1184         compose = compose_create(account, COMPOSE_FORWARD);
1185
1186         textview = GTK_TEXT_VIEW(compose->text);
1187         textbuf = gtk_text_view_get_buffer(textview);
1188
1189         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1190                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1191                 if (!is_file_exist(msgfile))
1192                         g_warning("%s: file not exist\n", msgfile);
1193                 else
1194                         compose_attach_append(compose, msgfile, msgfile,
1195                                 "message/rfc822");
1196                 g_free(msgfile);
1197         }
1198
1199         if (account->auto_sig)
1200                 compose_insert_sig(compose, FALSE);
1201
1202         if (prefs_common.linewrap_quote)
1203                 compose_wrap_line_all(compose);
1204
1205         gtk_text_buffer_get_start_iter(textbuf, &iter);
1206         gtk_text_buffer_place_cursor(textbuf, &iter);
1207
1208         gtk_widget_grab_focus(compose->header_last->entry);
1209         
1210 #if 0 /* NEW COMPOSE GUI */
1211         if (account->protocol != A_NNTP)
1212                 gtk_widget_grab_focus(compose->to_entry);
1213         else
1214                 gtk_widget_grab_focus(compose->newsgroups_entry);
1215 #endif
1216
1217         return compose;
1218 }
1219
1220 void compose_reedit(MsgInfo *msginfo)
1221 {
1222         Compose *compose;
1223         PrefsAccount *account = NULL;
1224         GtkTextView *textview;
1225         GtkTextBuffer *textbuf;
1226         GtkTextMark *mark;
1227         GtkTextIter iter;
1228         FILE *fp;
1229         gchar buf[BUFFSIZE];
1230
1231         g_return_if_fail(msginfo != NULL);
1232         g_return_if_fail(msginfo->folder != NULL);
1233
1234         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
1235                 gchar queueheader_buf[BUFFSIZE];
1236                 gint id;
1237
1238                 /* Select Account from queue headers */
1239                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1240                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1241                         id = atoi(&queueheader_buf[22]);
1242                         account = account_find_from_id(id);
1243                 }
1244                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1245                                              sizeof(queueheader_buf), "NAID:")) {
1246                         id = atoi(&queueheader_buf[5]);
1247                         account = account_find_from_id(id);
1248                 }
1249                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1250                                                     sizeof(queueheader_buf), "MAID:")) {
1251                         id = atoi(&queueheader_buf[5]);
1252                         account = account_find_from_id(id);
1253                 }
1254                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1255                                                                 sizeof(queueheader_buf), "S:")) {
1256                         account = account_find_from_address(queueheader_buf);
1257                 }
1258         } else 
1259                 account = msginfo->folder->folder->account;
1260
1261         if (!account && prefs_common.reedit_account_autosel) {
1262                 gchar from[BUFFSIZE];
1263                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1264                         extract_address(from);
1265                         account = account_find_from_address(from);
1266                 }
1267         }
1268         if (!account) account = cur_account;
1269         g_return_if_fail(account != NULL);
1270
1271         compose = compose_create(account, COMPOSE_REEDIT);
1272         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1273
1274         if (msginfo->folder->stype == F_QUEUE
1275         ||  msginfo->folder->stype == F_DRAFT) {
1276                 gchar queueheader_buf[BUFFSIZE];
1277
1278                 /* Set message save folder */
1279                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1280                         gint startpos = 0;
1281
1282                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1283                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1284                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1285                 }
1286         }
1287         
1288         if (compose_parse_header(compose, msginfo) < 0) return;
1289         compose_reedit_set_entry(compose, msginfo);
1290
1291         textview = GTK_TEXT_VIEW(compose->text);
1292         textbuf = gtk_text_view_get_buffer(textview);
1293         mark = gtk_text_buffer_get_insert(textbuf);
1294         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1295
1296         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1297                                         G_CALLBACK(compose_changed_cb),
1298                                         compose);
1299
1300         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1301                 g_warning("Can't get text part\n");
1302         else {
1303                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1304                         strcrchomp(buf);
1305                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1306                 }
1307                 fclose(fp);
1308         }
1309         
1310         compose_attach_parts(compose, msginfo);
1311
1312         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1313                                         G_CALLBACK(compose_changed_cb),
1314                                         compose);
1315
1316         gtk_widget_grab_focus(compose->text);
1317
1318         if (prefs_common.auto_exteditor)
1319                 compose_exec_ext_editor(compose);
1320 }
1321
1322 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1323 {
1324         Compose *compose;
1325         gchar *filename;
1326         GtkItemFactory *ifactory;
1327         FolderItem *item;
1328
1329         g_return_val_if_fail(msginfo != NULL, NULL);
1330
1331         if (!account)
1332                 account = account_get_reply_account(msginfo,
1333                                         prefs_common.reply_account_autosel);
1334         g_return_val_if_fail(account != NULL, NULL);
1335
1336         compose = compose_create(account, COMPOSE_REDIRECT);
1337         ifactory = gtk_item_factory_from_widget(compose->menubar);
1338
1339         compose->replyinfo = NULL;
1340         compose->fwdinfo = NULL;
1341
1342         compose_show_first_last_header(compose, TRUE);
1343
1344         gtk_widget_grab_focus(compose->header_last->entry);
1345
1346         filename = procmsg_get_message_file(msginfo);
1347         if (filename == NULL)
1348                 return NULL;
1349
1350         compose->redirect_filename = filename;
1351         
1352         /* Set save folder */
1353         item = msginfo->folder;
1354         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1355                 gchar *folderidentifier;
1356
1357                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1358                 folderidentifier = folder_item_get_identifier(item);
1359                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1360                 g_free(folderidentifier);
1361         }
1362
1363         compose_attach_parts(compose, msginfo);
1364
1365         if (msginfo->subject)
1366                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1367                                    msginfo->subject);
1368         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1369
1370         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1371         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1372
1373         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1374         menu_set_sensitive(ifactory, "/Add...", FALSE);
1375         menu_set_sensitive(ifactory, "/Remove", FALSE);
1376         menu_set_sensitive(ifactory, "/Property...", FALSE);
1377
1378         ifactory = gtk_item_factory_from_widget(compose->menubar);
1379         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1380         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1381         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1382         menu_set_sensitive(ifactory, "/Edit", FALSE);
1383         menu_set_sensitive(ifactory, "/Message/Save to draft folder", FALSE);
1384         menu_set_sensitive(ifactory, "/Message/Save and keep editing", FALSE);
1385 #if USE_GPGME
1386         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1387         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1388         menu_set_sensitive(ifactory, "/Message/Mode/MIME", FALSE);
1389         menu_set_sensitive(ifactory, "/Message/Mode/Inline", FALSE);
1390 #endif
1391         menu_set_sensitive(ifactory, "/Message/Priority", FALSE);
1392         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1393         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1394         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1395         
1396         gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1397         gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1398         gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1399         gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1400         gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1401         gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
1402         gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
1403
1404         return compose;
1405 }
1406
1407 GList *compose_get_compose_list(void)
1408 {
1409         return compose_list;
1410 }
1411
1412 void compose_entry_append(Compose *compose, const gchar *address,
1413                           ComposeEntryType type)
1414 {
1415         gchar *header;
1416
1417         if (!address || *address == '\0') return;
1418
1419 #if 0 /* NEW COMPOSE GUI */
1420         switch (type) {
1421         case COMPOSE_CC:
1422                 entry = GTK_ENTRY(compose->cc_entry);
1423                 break;
1424         case COMPOSE_BCC:
1425                 entry = GTK_ENTRY(compose->bcc_entry);
1426                 break;
1427         case COMPOSE_NEWSGROUPS:
1428                 entry = GTK_ENTRY(compose->newsgroups_entry);
1429                 break;
1430         case COMPOSE_TO:
1431         default:
1432                 entry = GTK_ENTRY(compose->to_entry);
1433                 break;
1434         }
1435
1436         text = gtk_entry_get_text(entry);
1437         if (*text != '\0')
1438                 gtk_entry_append_text(entry, ", ");
1439         gtk_entry_append_text(entry, address);
1440 #endif
1441
1442         switch (type) {
1443         case COMPOSE_CC:
1444                 header = N_("Cc:");
1445                 break;
1446         case COMPOSE_BCC:
1447                 header = N_("Bcc:");
1448                 break;
1449         case COMPOSE_REPLYTO:
1450                 header = N_("Reply-To:");
1451                 break;
1452         case COMPOSE_NEWSGROUPS:
1453                 header = N_("Newsgroups:");
1454                 break;
1455         case COMPOSE_FOLLOWUPTO:
1456                 header = N_( "Followup-To:");
1457                 break;
1458         case COMPOSE_TO:
1459         default:
1460                 header = N_("To:");
1461                 break;
1462         }
1463         header = prefs_common.trans_hdr ? gettext(header) : header;
1464
1465         compose_add_header_entry(compose, header, (gchar *)address);
1466 }
1467
1468 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
1469 {
1470         static GtkStyle *bold_style = NULL;
1471         static GdkColor bold_color;
1472         GSList *h_list;
1473         GtkEntry *entry;
1474                 
1475         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
1476                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
1477                 if (gtk_entry_get_text(entry) && 
1478                     !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1479                         gtk_widget_ensure_style(GTK_WIDGET(entry));
1480                         if (!bold_style) {
1481                                 PangoFontDescription *font_desc = NULL;
1482                                 gtkut_convert_int_to_gdk_color
1483                                         (prefs_common.color_new, &bold_color);
1484                                 bold_style = gtk_style_copy(gtk_widget_get_style
1485                                         (GTK_WIDGET(entry)));
1486                                 if (BOLD_FONT)
1487                                         font_desc = pango_font_description_from_string
1488                                                         (BOLD_FONT);
1489                                 if (font_desc) {
1490                                         if (bold_style->font_desc)
1491                                                 pango_font_description_free
1492                                                         (bold_style->font_desc);
1493                                         bold_style->font_desc = font_desc;
1494                                 }
1495                                 bold_style->fg[GTK_STATE_NORMAL] = bold_color;
1496                         }
1497                         gtk_widget_set_style(GTK_WIDGET(entry), bold_style);
1498                 }
1499         }
1500 }
1501
1502 void compose_toolbar_cb(gint action, gpointer data)
1503 {
1504         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1505         Compose *compose = (Compose*)toolbar_item->parent;
1506         
1507         g_return_if_fail(compose != NULL);
1508
1509         switch(action) {
1510         case A_SEND:
1511                 compose_send_cb(compose, 0, NULL);
1512                 break;
1513         case A_SENDL:
1514                 compose_send_later_cb(compose, 0, NULL);
1515                 break;
1516         case A_DRAFT:
1517                 compose_draft_cb(compose, 0, NULL);
1518                 break;
1519         case A_INSERT:
1520                 compose_insert_file_cb(compose, 0, NULL);
1521                 break;
1522         case A_ATTACH:
1523                 compose_attach_cb(compose, 0, NULL);
1524                 break;
1525         case A_SIG:
1526                 compose_insert_sig(compose, FALSE);
1527                 break;
1528         case A_EXTEDITOR:
1529                 compose_ext_editor_cb(compose, 0, NULL);
1530                 break;
1531         case A_LINEWRAP_CURRENT:
1532                 compose_wrap_line(compose);
1533                 break;
1534         case A_LINEWRAP_ALL:
1535                 compose_wrap_line_all(compose);
1536                 break;
1537         case A_ADDRBOOK:
1538                 compose_address_cb(compose, 0, NULL);
1539                 break;
1540 #ifdef USE_ASPELL
1541         case A_CHECK_SPELLING:
1542                 compose_check_all(compose);
1543                 break;
1544 #endif
1545         default:
1546                 break;
1547         }
1548 }
1549
1550 static void compose_entries_set(Compose *compose, const gchar *mailto)
1551 {
1552         gchar *to = NULL;
1553         gchar *cc = NULL;
1554         gchar *bcc = NULL;
1555         gchar *subject = NULL;
1556         gchar *body = NULL;
1557
1558         scan_mailto_url(mailto, &to, &cc, &bcc, &subject, &body);
1559
1560         if (to)
1561                 compose_entry_append(compose, to, COMPOSE_TO);
1562         if (cc)
1563                 compose_entry_append(compose, cc, COMPOSE_CC);
1564         if (bcc)
1565                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1566         if (subject)
1567                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1568         if (body) {
1569                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1570                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1571                 GtkTextMark *mark;
1572                 GtkTextIter iter;
1573
1574                 mark = gtk_text_buffer_get_insert(buffer);
1575                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1576
1577                 gtk_text_buffer_insert(buffer, &iter, body, -1);
1578                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1579         }
1580
1581         g_free(to);
1582         g_free(cc);
1583         g_free(bcc);
1584         g_free(subject);
1585         g_free(body);
1586 }
1587
1588 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1589 {
1590         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1591                                        {"Cc:",          NULL, TRUE},
1592                                        {"References:",  NULL, FALSE},
1593                                        {"Bcc:",         NULL, TRUE},
1594                                        {"Newsgroups:",  NULL, TRUE},
1595                                        {"Followup-To:", NULL, TRUE},
1596                                        {"List-Post:",   NULL, FALSE},
1597                                        {"X-Priority:",  NULL, FALSE},
1598                                        {NULL,           NULL, FALSE}};
1599
1600         enum
1601         {
1602                 H_REPLY_TO      = 0,
1603                 H_CC            = 1,
1604                 H_REFERENCES    = 2,
1605                 H_BCC           = 3,
1606                 H_NEWSGROUPS    = 4,
1607                 H_FOLLOWUP_TO   = 5,
1608                 H_LIST_POST     = 6,
1609                 H_X_PRIORITY    = 7
1610         };
1611
1612         FILE *fp;
1613
1614         g_return_val_if_fail(msginfo != NULL, -1);
1615
1616         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1617         procheader_get_header_fields(fp, hentry);
1618         fclose(fp);
1619
1620         if (hentry[H_REPLY_TO].body != NULL) {
1621                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1622                 compose->replyto = hentry[H_REPLY_TO].body;
1623                 hentry[H_REPLY_TO].body = NULL;
1624         }
1625         if (hentry[H_CC].body != NULL) {
1626                 conv_unmime_header_overwrite(hentry[H_CC].body);
1627                 compose->cc = hentry[H_CC].body;
1628                 hentry[H_CC].body = NULL;
1629         }
1630         if (hentry[H_REFERENCES].body != NULL) {
1631                 if (compose->mode == COMPOSE_REEDIT)
1632                         compose->references = hentry[H_REFERENCES].body;
1633                 else {
1634                         compose->references = compose_parse_references
1635                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1636                         g_free(hentry[H_REFERENCES].body);
1637                 }
1638                 hentry[H_REFERENCES].body = NULL;
1639         }
1640         if (hentry[H_BCC].body != NULL) {
1641                 if (compose->mode == COMPOSE_REEDIT) {
1642                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1643                         compose->bcc = hentry[H_BCC].body;
1644                 } else
1645                         g_free(hentry[H_BCC].body);
1646                 hentry[H_BCC].body = NULL;
1647         }
1648         if (hentry[H_NEWSGROUPS].body != NULL) {
1649                 conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
1650                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1651                 hentry[H_NEWSGROUPS].body = NULL;
1652         }
1653         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1654                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1655                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1656                 hentry[H_FOLLOWUP_TO].body = NULL;
1657         }
1658         if (hentry[H_LIST_POST].body != NULL) {
1659                 gchar *to = NULL;
1660
1661                 extract_address(hentry[H_LIST_POST].body);
1662                 if (hentry[H_LIST_POST].body[0] != '\0') {
1663                         scan_mailto_url(hentry[H_LIST_POST].body,
1664                                         &to, NULL, NULL, NULL, NULL);
1665                         if (to) {
1666                                 g_free(compose->ml_post);
1667                                 compose->ml_post = to;
1668                         }
1669                 }
1670                 g_free(hentry[H_LIST_POST].body);
1671                 hentry[H_LIST_POST].body = NULL;
1672         }
1673
1674         /* CLAWS - X-Priority */
1675         if (compose->mode == COMPOSE_REEDIT)
1676                 if (hentry[H_X_PRIORITY].body != NULL) {
1677                         gint priority;
1678                         
1679                         priority = atoi(hentry[H_X_PRIORITY].body);
1680                         g_free(hentry[H_X_PRIORITY].body);
1681                         
1682                         hentry[H_X_PRIORITY].body = NULL;
1683                         
1684                         if (priority < PRIORITY_HIGHEST || 
1685                             priority > PRIORITY_LOWEST)
1686                                 priority = PRIORITY_NORMAL;
1687                         
1688                         compose->priority =  priority;
1689                 }
1690  
1691         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1692                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1693         else if (compose->mode != COMPOSE_REEDIT &&
1694                  msginfo->msgid && *msginfo->msgid) {
1695                 compose->inreplyto = g_strdup(msginfo->msgid);
1696
1697                 if (!compose->references) {
1698                         if (msginfo->inreplyto && *msginfo->inreplyto)
1699                                 compose->references =
1700                                         g_strdup_printf("<%s>\n\t<%s>",
1701                                                         msginfo->inreplyto,
1702                                                         msginfo->msgid);
1703                         else
1704                                 compose->references =
1705                                         g_strconcat("<", msginfo->msgid, ">",
1706                                                     NULL);
1707                 }
1708         }
1709
1710         return 0;
1711 }
1712
1713 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1714 {
1715         GSList *ref_id_list, *cur;
1716         GString *new_ref;
1717         gchar *new_ref_str;
1718
1719         ref_id_list = references_list_append(NULL, ref);
1720         if (!ref_id_list) return NULL;
1721         if (msgid && *msgid)
1722                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1723
1724         for (;;) {
1725                 gint len = 0;
1726
1727                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1728                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1729                         len += strlen((gchar *)cur->data) + 5;
1730
1731                 if (len > MAX_REFERENCES_LEN) {
1732                         /* remove second message-ID */
1733                         if (ref_id_list && ref_id_list->next &&
1734                             ref_id_list->next->next) {
1735                                 g_free(ref_id_list->next->data);
1736                                 ref_id_list = g_slist_remove
1737                                         (ref_id_list, ref_id_list->next->data);
1738                         } else {
1739                                 slist_free_strings(ref_id_list);
1740                                 g_slist_free(ref_id_list);
1741                                 return NULL;
1742                         }
1743                 } else
1744                         break;
1745         }
1746
1747         new_ref = g_string_new("");
1748         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1749                 if (new_ref->len > 0)
1750                         g_string_append(new_ref, "\n\t");
1751                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1752         }
1753
1754         slist_free_strings(ref_id_list);
1755         g_slist_free(ref_id_list);
1756
1757         new_ref_str = new_ref->str;
1758         g_string_free(new_ref, FALSE);
1759
1760         return new_ref_str;
1761 }
1762
1763 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1764                                 const gchar *fmt, const gchar *qmark,
1765                                 const gchar *body)
1766 {
1767         static MsgInfo dummyinfo;
1768         gchar *quote_str = NULL;
1769         gchar *buf;
1770         gchar *p, *lastp;
1771         gint len;
1772         const gchar *trimmed_body = body;
1773         
1774         if (!msginfo)
1775                 msginfo = &dummyinfo;
1776
1777         if (qmark != NULL) {
1778                 quote_fmt_init(msginfo, NULL, NULL);
1779                 quote_fmt_scan_string(qmark);
1780                 quote_fmt_parse();
1781
1782                 buf = quote_fmt_get_buffer();
1783                 if (buf == NULL)
1784                         alertpanel_error(_("Quote mark format error."));
1785                 else
1786                         Xstrdup_a(quote_str, buf, return NULL)
1787         }
1788
1789         if (fmt && *fmt != '\0') {
1790                 while (trimmed_body && strlen(trimmed_body) > 1
1791                         && trimmed_body[0]=='\n')
1792                         *trimmed_body++;
1793
1794                 quote_fmt_init(msginfo, quote_str, trimmed_body);
1795                 quote_fmt_scan_string(fmt);
1796                 quote_fmt_parse();
1797
1798                 buf = quote_fmt_get_buffer();
1799                 if (buf == NULL) {
1800                         alertpanel_error(_("Message reply/forward format error."));
1801                         return NULL;
1802                 }
1803         } else
1804                 buf = "";
1805
1806         for (p = buf; *p != '\0'; ) {
1807                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1808                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1809                 GtkTextMark *mark;
1810                 GtkTextIter iter;
1811
1812                 g_signal_handlers_block_by_func(G_OBJECT(buffer),
1813                                         G_CALLBACK(compose_changed_cb),
1814                                         compose);
1815                 g_signal_handlers_block_by_func(G_OBJECT(buffer),
1816                                         G_CALLBACK(text_inserted),
1817                                         compose);
1818                 
1819                 mark = gtk_text_buffer_get_insert(buffer);
1820                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1821
1822                 lastp = strchr(p, '\n');
1823                 len = lastp ? lastp - p + 1 : -1;
1824
1825                 gtk_text_buffer_insert(buffer, &iter, p, len);
1826
1827                 g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
1828                                         G_CALLBACK(compose_changed_cb),
1829                                         compose);
1830                 g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
1831                                         G_CALLBACK(text_inserted),
1832                                         compose);
1833                 
1834                 if (lastp)
1835                         p = lastp + 1;
1836                 else
1837                         break;
1838         }
1839
1840         return buf;
1841 }
1842
1843 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1844                                     gboolean to_all, gboolean to_ml,
1845                                     gboolean to_sender,
1846                                     gboolean followup_and_reply_to)
1847 {
1848         GSList *cc_list = NULL;
1849         GSList *cur;
1850         gchar *from = NULL;
1851         gchar *replyto = NULL;
1852         GHashTable *to_table;
1853
1854         g_return_if_fail(compose->account != NULL);
1855         g_return_if_fail(msginfo != NULL);
1856
1857         if (compose->account->protocol != A_NNTP) {
1858                 if (!compose->replyto && to_ml && compose->ml_post
1859                     && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
1860                         compose_entry_append(compose,
1861                                            compose->ml_post,
1862                                            COMPOSE_TO);
1863                 else if (!(to_all || to_sender)
1864                          && msginfo->folder
1865                          && msginfo->folder->prefs->enable_default_reply_to) {
1866                         compose_entry_append(compose,
1867                             msginfo->folder->prefs->default_reply_to,
1868                             COMPOSE_TO);
1869                 } else
1870                         compose_entry_append(compose,
1871                                  (compose->replyto && !to_sender)
1872                                   ? compose->replyto :
1873                                   msginfo->from ? msginfo->from : "",
1874                                   COMPOSE_TO);
1875         } else {
1876                 if (to_sender || (compose->followup_to && 
1877                         !strncmp(compose->followup_to, "poster", 6)))
1878                         compose_entry_append
1879                                 (compose, 
1880                                  (compose->replyto ? compose->replyto :
1881                                         msginfo->from ? msginfo->from : ""),
1882                                  COMPOSE_TO);
1883                                  
1884                 else if (followup_and_reply_to || to_all) {
1885                         compose_entry_append
1886                                 (compose,
1887                                  (compose->replyto ? compose->replyto :
1888                                  msginfo->from ? msginfo->from : ""),
1889                                  COMPOSE_TO);                           
1890                 
1891                         compose_entry_append
1892                                 (compose,
1893                                  compose->followup_to ? compose->followup_to :
1894                                  compose->newsgroups ? compose->newsgroups : "",
1895                                  COMPOSE_NEWSGROUPS);
1896                 } 
1897                 else 
1898                         compose_entry_append
1899                                 (compose,
1900                                  compose->followup_to ? compose->followup_to :
1901                                  compose->newsgroups ? compose->newsgroups : "",
1902                                  COMPOSE_NEWSGROUPS);
1903         }
1904
1905         if (msginfo->subject && *msginfo->subject) {
1906                 gchar *buf, *buf2;
1907                 guchar *p;
1908
1909                 buf = p = g_strdup(msginfo->subject);
1910                 p += subject_get_prefix_length(p);
1911                 memmove(buf, p, strlen(p) + 1);
1912
1913                 buf2 = g_strdup_printf("Re: %s", buf);
1914                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1915
1916                 g_free(buf2);
1917                 g_free(buf);
1918         } else
1919                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1920
1921         if (to_ml && compose->ml_post) return;
1922         if (!to_all || compose->account->protocol == A_NNTP) return;
1923
1924         if (compose->replyto) {
1925                 Xstrdup_a(replyto, compose->replyto, return);
1926                 extract_address(replyto);
1927         }
1928         if (msginfo->from) {
1929                 Xstrdup_a(from, msginfo->from, return);
1930                 extract_address(from);
1931         }
1932
1933         if (replyto && from)
1934                 cc_list = address_list_append_with_comments(cc_list, from);
1935         if (to_all && msginfo->folder && 
1936             msginfo->folder->prefs->enable_default_reply_to)
1937                 cc_list = address_list_append_with_comments(cc_list,
1938                                 msginfo->folder->prefs->default_reply_to);
1939         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
1940         cc_list = address_list_append_with_comments(cc_list, compose->cc);
1941
1942         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1943         if (replyto)
1944                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
1945         if (compose->account)
1946                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
1947                                     GINT_TO_POINTER(1));
1948
1949         /* remove address on To: and that of current account */
1950         for (cur = cc_list; cur != NULL; ) {
1951                 GSList *next = cur->next;
1952                 gchar *addr;
1953
1954                 addr = g_strdup(cur->data);
1955                 extract_address(addr);
1956
1957                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
1958                         cc_list = g_slist_remove(cc_list, cur->data);
1959                 else
1960                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
1961
1962                 cur = next;
1963         }
1964         hash_free_strings(to_table);
1965         g_hash_table_destroy(to_table);
1966
1967         if (cc_list) {
1968                 for (cur = cc_list; cur != NULL; cur = cur->next)
1969                         compose_entry_append(compose, (gchar *)cur->data,
1970                                              COMPOSE_CC);
1971                 slist_free_strings(cc_list);
1972                 g_slist_free(cc_list);
1973         }
1974
1975 }
1976
1977 #define SET_ENTRY(entry, str) \
1978 { \
1979         if (str && *str) \
1980                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1981 }
1982
1983 #define SET_ADDRESS(type, str) \
1984 { \
1985         if (str && *str) \
1986                 compose_entry_append(compose, str, type); \
1987 }
1988
1989 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1990 {
1991         g_return_if_fail(msginfo != NULL);
1992
1993         SET_ENTRY(subject_entry, msginfo->subject);
1994         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1995         SET_ADDRESS(COMPOSE_CC, compose->cc);
1996         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1997         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1998         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
1999         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
2000
2001         compose_update_priority_menu_item(compose);
2002 #if USE_GPGME   
2003         compose_update_gnupg_mode_menu_item(compose);
2004 #endif
2005         compose_show_first_last_header(compose, TRUE);
2006
2007 }
2008
2009 #undef SET_ENTRY
2010 #undef SET_ADDRESS
2011
2012 static void compose_insert_sig(Compose *compose, gboolean replace)
2013 {
2014         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2015         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2016         GtkTextMark *mark;
2017         GtkTextIter iter;
2018         gint cur_pos;
2019
2020         
2021         g_return_if_fail(compose->account != NULL);
2022
2023         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2024                                         G_CALLBACK(compose_changed_cb),
2025                                         compose);
2026         
2027         mark = gtk_text_buffer_get_insert(buffer);
2028         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2029         cur_pos = gtk_text_iter_get_offset (&iter);
2030
2031         gtk_text_buffer_get_end_iter(buffer, &iter);
2032
2033         if (replace && compose->sig_str) {
2034                 gboolean found;
2035                 GtkTextIter first_iter, start_iter, end_iter;
2036
2037                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2038
2039                 if (compose->sig_str[0] == '\0')
2040                         found = FALSE;
2041                 else
2042                         found = gtk_text_iter_forward_search(&first_iter,
2043                                                              compose->sig_str,
2044                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2045                                                              &start_iter, &end_iter,
2046                                                              NULL);
2047
2048                 if (found) {
2049                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2050                         iter = start_iter;
2051                 }
2052         }
2053
2054         g_free(compose->sig_str);
2055         compose->sig_str = compose_get_signature_str(compose);
2056         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2057                 compose->sig_str = g_strdup("");
2058
2059         gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2060
2061         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2062                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2063
2064         gtk_text_buffer_get_iter_at_offset (buffer, &iter, cur_pos);
2065         gtk_text_buffer_place_cursor (buffer, &iter);
2066
2067         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2068                                         G_CALLBACK(compose_changed_cb),
2069                                         compose);
2070         
2071 }
2072
2073 static gchar *compose_get_signature_str(Compose *compose)
2074 {
2075         gchar *sig_body = NULL;
2076         gchar *sig_str = NULL;
2077
2078         g_return_val_if_fail(compose->account != NULL, NULL);
2079
2080         if (!compose->account->sig_path)
2081                 return NULL;
2082
2083         if (compose->account->sig_type == SIG_FILE) {
2084                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2085                         g_warning("can't open signature file: %s\n",
2086                                   compose->account->sig_path);
2087                         return NULL;
2088                 }
2089         }
2090
2091         if (compose->account->sig_type == SIG_COMMAND)
2092                 sig_body = get_command_output(compose->account->sig_path);
2093         else {
2094                 gchar *tmp;
2095
2096                 tmp = file_read_to_str(compose->account->sig_path);
2097                 if (!tmp)
2098                         return NULL;
2099                 sig_body = normalize_newlines(tmp);
2100                 g_free(tmp);
2101         }
2102
2103         if (compose->account->sig_sep)
2104                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2105                                       NULL);
2106         else
2107                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2108
2109         g_free(sig_body);
2110         
2111         return sig_str;
2112 }
2113
2114 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2115 {
2116         GtkTextView *text;
2117         GtkTextBuffer *buffer;
2118         GtkTextMark *mark;
2119         GtkTextIter iter;
2120         gchar buf[BUFFSIZE];
2121         gint len;
2122         FILE *fp;
2123         gboolean badtxt = FALSE;
2124
2125         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
2126
2127         if ((fp = fopen(file, "rb")) == NULL) {
2128                 FILE_OP_ERROR(file, "fopen");
2129                 return COMPOSE_INSERT_READ_ERROR;
2130         }
2131
2132         text = GTK_TEXT_VIEW(compose->text);
2133         buffer = gtk_text_view_get_buffer(text);
2134         mark = gtk_text_buffer_get_insert(buffer);
2135         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2136
2137         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2138                                         G_CALLBACK(text_inserted),
2139                                         compose);
2140
2141         while (fgets(buf, sizeof(buf), fp) != NULL) {
2142                 const gchar *cur_encoding = conv_get_current_charset_str();
2143                 gchar *str = conv_codeset_strdup(buf, cur_encoding, CS_UTF_8);
2144
2145                 if (!str) continue;
2146
2147                 /* strip <CR> if DOS/Windows file,
2148                    replace <CR> with <LF> if Macintosh file. */
2149                 strcrchomp(str);
2150                 len = strlen(str);
2151                 if (len > 0 && str[len - 1] != '\n') {
2152                         while (--len >= 0)
2153                                 if (str[len] == '\r') str[len] = '\n';
2154                 }
2155                 gtk_text_buffer_insert(buffer, &iter, str, -1);
2156
2157                 g_free (str);
2158         }
2159
2160         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2161                                           G_CALLBACK(text_inserted),
2162                                           compose);
2163
2164         fclose(fp);
2165
2166         if (badtxt)
2167                 return COMPOSE_INSERT_INVALID_CHARACTER;
2168         else 
2169                 return COMPOSE_INSERT_SUCCESS;
2170 }
2171
2172 static void compose_attach_append(Compose *compose, const gchar *file,
2173                                   const gchar *filename,
2174                                   const gchar *content_type)
2175 {
2176         AttachInfo *ainfo;
2177         gchar *text[N_ATTACH_COLS];
2178         FILE *fp;
2179         off_t size;
2180         gint row;
2181
2182         if (!is_file_exist(file)) {
2183                 g_warning("File %s doesn't exist\n", file);
2184                 return;
2185         }
2186         if ((size = get_file_size(file)) < 0) {
2187                 g_warning("Can't get file size of %s\n", file);
2188                 return;
2189         }
2190         if (size == 0) {
2191                 alertpanel_notice(_("File %s is empty."), file);
2192                 return;
2193         }
2194         if ((fp = fopen(file, "rb")) == NULL) {
2195                 alertpanel_error(_("Can't read %s."), file);
2196                 return;
2197         }
2198         fclose(fp);
2199
2200 #if 0 /* NEW COMPOSE GUI */
2201         if (!compose->use_attach) {
2202                 GtkItemFactory *ifactory;
2203
2204                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2205                 menu_set_active(ifactory, "/View/Attachment", TRUE);
2206         }
2207 #endif
2208         ainfo = g_new0(AttachInfo, 1);
2209         ainfo->file = g_strdup(file);
2210
2211         if (content_type) {
2212                 ainfo->content_type = g_strdup(content_type);
2213                 if (!strcasecmp(content_type, "message/rfc822")) {
2214                         MsgInfo *msginfo;
2215                         MsgFlags flags = {0, 0};
2216                         const gchar *name;
2217
2218                         if (procmime_get_encoding_for_file(file) == ENC_7BIT)
2219                                 ainfo->encoding = ENC_7BIT;
2220                         else
2221                                 ainfo->encoding = ENC_8BIT;
2222
2223                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
2224                         if (msginfo && msginfo->subject)
2225                                 name = msginfo->subject;
2226                         else
2227                                 name = g_basename(filename ? filename : file);
2228
2229                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
2230
2231                         procmsg_msginfo_free(msginfo);
2232                 } else {
2233                         if (!g_strncasecmp(content_type, "text", 4))
2234                                 ainfo->encoding =
2235                                         procmime_get_encoding_for_file(file);
2236                         else
2237                                 ainfo->encoding = ENC_BASE64;
2238                         ainfo->name = g_strdup
2239                                 (g_basename(filename ? filename : file));
2240                 }
2241         } else {
2242                 ainfo->content_type = procmime_get_mime_type(file);
2243                 if (!ainfo->content_type) {
2244                         ainfo->content_type =
2245                                 g_strdup("application/octet-stream");
2246                         ainfo->encoding = ENC_BASE64;
2247                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
2248                         ainfo->encoding = procmime_get_encoding_for_file(file);
2249                 else
2250                         ainfo->encoding = ENC_BASE64;
2251                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
2252         }
2253         ainfo->size = size;
2254
2255         text[COL_MIMETYPE] = ainfo->content_type;
2256         text[COL_SIZE] = to_human_readable(size);
2257         text[COL_NAME] = ainfo->name;
2258
2259         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2260         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2261 }
2262
2263 #ifdef USE_GPGME
2264 static void compose_use_signing(Compose *compose, gboolean use_signing)
2265 {
2266         GtkItemFactory *ifactory;
2267         GtkWidget *menuitem = NULL;
2268
2269         compose->use_signing = use_signing;
2270         ifactory = gtk_item_factory_from_widget(compose->menubar);
2271         menuitem = gtk_item_factory_get_item
2272                 (ifactory, "/Message/Sign");
2273
2274         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2275                                        use_signing);
2276         compose_update_gnupg_mode_menu_item(compose);
2277 }
2278 #endif /* USE_GPGME */
2279
2280 #define NEXT_PART_NOT_CHILD(info)  \
2281 {  \
2282         node = info->node;  \
2283         while (node->children)  \
2284                 node = g_node_last_child(node);  \
2285         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
2286 }
2287
2288 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2289 {
2290         MimeInfo *mimeinfo;
2291         MimeInfo *child;
2292         MimeInfo *firsttext = NULL;
2293         MimeInfo *encrypted = NULL;
2294         GNode    *node;
2295         gchar *outfile;
2296         const gchar *partname = NULL;
2297
2298         mimeinfo = procmime_scan_message(msginfo);
2299         if (!mimeinfo) return;
2300
2301         if (mimeinfo->node->children == NULL) {
2302                 procmime_mimeinfo_free_all(mimeinfo);
2303                 return;
2304         }
2305
2306         /* find first content part */
2307         child = (MimeInfo *) mimeinfo->node->children->data;
2308         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
2309                 child = (MimeInfo *)child->node->children->data;
2310
2311         if (child->type == MIMETYPE_TEXT) {
2312                 firsttext = child;
2313                 debug_print("First text part found\n");
2314         } else if (compose->mode == COMPOSE_REEDIT &&
2315                  child->type == MIMETYPE_APPLICATION &&
2316                  !strcasecmp(child->subtype, "pgp-encrypted")) {
2317                 AlertValue val;
2318                 val = alertpanel(_("Encrypted message"),
2319                                  _("Cannot re-edit an encrypted message. \n"
2320                                    "Discard encrypted part?"),
2321                                  _("Yes"), _("No"), NULL);
2322                 if (val == G_ALERTDEFAULT) 
2323                         encrypted = (MimeInfo *)child->node->parent->data;
2324         }
2325      
2326         child = (MimeInfo *) mimeinfo->node->children->data;
2327         while (child != NULL) {
2328                 if (child == encrypted) {
2329                         /* skip this part of tree */
2330                         NEXT_PART_NOT_CHILD(child);
2331                         continue;
2332                 }
2333
2334                 if (child->type == MIMETYPE_MULTIPART) {
2335                         /* get the actual content */
2336                         child = procmime_mimeinfo_next(child);
2337                         continue;
2338                 }
2339                     
2340                 if (child == firsttext) {
2341                         child = procmime_mimeinfo_next(child);
2342                         continue;
2343                 }
2344
2345                 if ((compose->mode == COMPOSE_REEDIT || 
2346                      compose->mode == COMPOSE_FORWARD_INLINE ||
2347                      compose->mode == COMPOSE_FORWARD )  &&
2348                     (child->type == MIMETYPE_APPLICATION) && 
2349                     !strcmp(child->subtype, "pgp-signature")) {
2350 #ifdef USE_GPGME
2351                         if (compose->mode == COMPOSE_REEDIT) {
2352                                 compose->gnupg_mode  = GNUPG_MODE_DETACH;
2353                                 compose_use_signing(compose, TRUE);
2354                         }
2355 #endif
2356                         child = procmime_mimeinfo_next(child);
2357                         continue;
2358                 }
2359                 outfile = procmime_get_tmp_file_name(child);
2360                 if (procmime_get_part(outfile, child) < 0)
2361                         g_warning("Can't get the part of multipart message.");
2362                 else {
2363                         gchar *content_type;
2364
2365                         content_type = g_strdup_printf("%s/%s", procmime_get_type_str(child->type), child->subtype);
2366                         partname = procmime_mimeinfo_get_parameter(child, "name");
2367                         if (partname == NULL)
2368                                 partname = "";
2369                         compose_attach_append(compose, outfile, 
2370                                               partname, content_type);
2371                         g_free(content_type);
2372                 }
2373                 g_free(outfile);
2374                 NEXT_PART_NOT_CHILD(child);
2375         }
2376         procmime_mimeinfo_free_all(mimeinfo);
2377 }
2378
2379   
2380 #define CHAR_BUF_SIZE 8
2381 #undef NEXT_PART_NOT_CHILD
2382
2383 #define GET_CHAR(iter_p, buf, len)                                           \
2384 {                                                                            \
2385         GtkTextIter end_iter;                                                \
2386         gchar *tmp;                                                          \
2387         end_iter = *iter_p;                                                  \
2388         gtk_text_iter_forward_char(&end_iter);                               \
2389         tmp = gtk_text_buffer_get_text(textbuf, iter_p, &end_iter, FALSE);   \
2390         if (tmp) {                                                           \
2391                 glong items_read, items_witten;                              \
2392                 GError *error = NULL;                                        \
2393                 gunichar *wide_char;                                         \
2394                 strncpy2(buf, tmp, CHAR_BUF_SIZE);                           \
2395                 wide_char = g_utf8_to_ucs4(tmp, -1,                          \
2396                                            &items_read, &items_witten,       \
2397                                            &error);                          \
2398                 if (error != NULL) {                                         \
2399                         g_warning("%s\n", error->message);                   \
2400                         g_error_free(error);                                 \
2401                 }                                                            \
2402                 len = wide_char && g_unichar_iswide(*wide_char) ? 2 : 1;     \
2403                 g_free(wide_char);                                           \
2404         } else {                                                             \
2405                 buf[0] = '\0';                                               \
2406                 len = 1;                                                     \
2407         }                                                                    \
2408         g_free(tmp);                                                         \
2409 }
2410
2411 #define DISP_WIDTH(len) \
2412         ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
2413          (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
2414
2415 #define SPACE_CHARS     " \t"
2416
2417 static void compose_wrap_line(Compose *compose)
2418 {
2419         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2420         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2421         GtkTextMark *mark;
2422         GtkTextIter insert_iter, iter;
2423         gint ch_len, last_ch_len;
2424         gchar cbuf[CHAR_BUF_SIZE], last_ch;
2425         guint text_len;
2426         gint p_start, p_end;
2427         gint line_pos, cur_pos;
2428         gint line_len, cur_len;
2429         gboolean line_end, quoted;
2430
2431         text_len = gtk_text_buffer_get_char_count(textbuf);
2432         mark = gtk_text_buffer_get_insert(textbuf);
2433         gtk_text_buffer_get_iter_at_mark(textbuf, &insert_iter, mark);
2434         cur_pos = gtk_text_iter_get_offset(&insert_iter);
2435         GET_CHAR(&insert_iter, cbuf, ch_len);
2436         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2437                 GtkTextIter prev_iter;
2438                 if (cur_pos == 0)
2439                         return; /* on the paragraph mark */
2440                 prev_iter = insert_iter;
2441                 gtk_text_iter_backward_char(&prev_iter);
2442                 GET_CHAR(&prev_iter, cbuf, ch_len);
2443                 if (ch_len == 1 && *cbuf == '\n')
2444                         return; /* on the paragraph mark */
2445         }
2446
2447         /* find paragraph start. */
2448         line_end = quoted = FALSE;
2449         for (iter = insert_iter; gtk_text_iter_backward_char(&iter);) {
2450                 GET_CHAR(&iter, cbuf, ch_len);
2451                 if (ch_len == 1 && *cbuf == '\n') {
2452                         if (quoted)
2453                                 return; /* quoted part */
2454                         if (line_end) {
2455                                 gtk_text_iter_forward_chars(&iter, 2);
2456                                 break;
2457                         }
2458                         line_end = TRUE;
2459                 } else {
2460                         if (ch_len == 1 
2461                             && strchr(prefs_common.quote_chars, *cbuf))
2462                                 quoted = 1;
2463                         else if (ch_len != 1 || !isspace(*(guchar *)cbuf))
2464                                 quoted = 0;
2465
2466                         line_end = FALSE;
2467                 }
2468         }
2469         p_start = gtk_text_iter_get_offset(&iter);
2470
2471         /* find paragraph end. */
2472         line_end = FALSE;
2473         for (iter = insert_iter; gtk_text_iter_forward_char(&iter);) {
2474                 GET_CHAR(&iter, cbuf, ch_len);
2475                 if (ch_len == 1 && *cbuf == '\n') {
2476                         if (line_end) {
2477                                 p_end -= 1;
2478                                 gtk_text_iter_backward_char(&iter);
2479                                 break;
2480                         }
2481                         line_end = TRUE;
2482                 } else {
2483                         if (line_end && ch_len == 1 &&
2484                             strchr(prefs_common.quote_chars, *cbuf))
2485                                 return; /* quoted part */
2486
2487                         line_end = FALSE;
2488                 }
2489         }
2490         p_end = gtk_text_iter_get_offset(&iter);
2491
2492         if (p_end >= text_len)
2493                 p_end = text_len;
2494
2495         if (p_start >= p_end)
2496                 return;
2497
2498         line_len = cur_len = 0;
2499         last_ch_len = 0;
2500         last_ch = '\0';
2501         line_pos = p_start;
2502         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2503                 gboolean space = FALSE;
2504
2505                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2506
2507                 GET_CHAR(&iter, cbuf, ch_len);
2508
2509                 if (ch_len < 0) {
2510                         cbuf[0] = '\0';
2511                         ch_len = 1;
2512                 }
2513
2514                 if (ch_len == 1 && isspace(*(guchar *)cbuf))
2515                         space = 1;
2516
2517                 if (ch_len == 1 && *cbuf == '\n') {
2518                         gboolean replace = FALSE;
2519                         GtkTextIter next_iter = iter;
2520
2521                         gtk_text_iter_forward_char(&next_iter);
2522
2523                         if (last_ch_len == 1 && !isspace((guchar)last_ch)) {
2524                                 if (cur_pos + 1 < p_end) {
2525                                         GET_CHAR(&next_iter, cbuf, ch_len);
2526                                         if (ch_len == 1 &&
2527                                             !isspace(*(guchar *)cbuf))
2528                                                 replace = TRUE;
2529                                 }
2530                         }
2531                         gtk_text_buffer_delete(textbuf, &iter, &next_iter);
2532                         if (replace) {
2533                                 gtk_text_buffer_insert(textbuf, &iter, " ", 1);
2534                                 space = TRUE;
2535                         }
2536                         else {
2537                                 p_end--;
2538                                 cur_pos--;
2539                                 gtk_text_buffer_get_iter_at_offset
2540                                         (textbuf, &iter, cur_pos);
2541                                 continue;
2542                         }
2543                 }
2544
2545                 last_ch_len = ch_len;
2546                 last_ch = *cbuf;
2547
2548                 if (space) {
2549                         line_pos = cur_pos + 1;
2550                         line_len = cur_len + ch_len;
2551                 }
2552
2553                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
2554
2555                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2556                     line_len > 0) {
2557                         gint tlen = ch_len;
2558                         GtkTextIter prev_iter = iter;
2559
2560                         gtk_text_iter_backward_char(&prev_iter);
2561
2562                         GET_CHAR(&prev_iter, cbuf, ch_len);
2563                         if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2564                                 gtk_text_buffer_delete(textbuf, &prev_iter, &iter);
2565                                 iter = prev_iter;
2566                                 p_end--;
2567                                 cur_pos--;
2568                                 line_pos--;
2569                                 cur_len--;
2570                                 line_len--;
2571                         }
2572                         ch_len = tlen;
2573
2574                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
2575                         p_end++;
2576                         cur_pos++;
2577                         line_pos++;
2578                         cur_len = cur_len - line_len + ch_len;
2579                         line_len = 0;
2580                         continue;
2581                 }
2582
2583                 if (ch_len > 1) {
2584                         line_pos = cur_pos + 1;
2585                         line_len = cur_len + ch_len;
2586                 }
2587                 cur_len += ch_len;
2588         }
2589 }
2590
2591 #undef WRAP_DEBUG
2592 #ifdef WRAP_DEBUG
2593 /* Darko: used when I debug wrapping */
2594 void dump_text(GtkTextBuffer textbuf, int pos, int tlen, int breakoncr)
2595 {
2596         gint i, clen;
2597         gchar cbuf[CHAR_BUF_SIZE];
2598
2599         printf("%d [", pos);
2600         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
2601         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
2602         for (; gtk_text_iter_forward_char(&iter) &&
2603                      gtk_text_iter_compare(&iter, &end_iter) < 0;)
2604                 GET_CHAR(&iter, cbuf, clen);
2605                 if (clen < 0) break;
2606                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2607                         break;
2608                 fwrite(cbuf, clen, 1, stdout);
2609         }
2610         printf("]\n");
2611 }
2612 #endif
2613
2614 typedef enum {
2615         WAIT_FOR_SPACE,
2616         WAIT_FOR_INDENT_CHAR,
2617         WAIT_FOR_INDENT_CHAR_OR_SPACE
2618 } IndentState;
2619
2620 /* return indent length, we allow:
2621    > followed by spaces/tabs
2622    | followed by spaces/tabs
2623    uppercase characters immediately followed by >,
2624    and the repeating sequences of the above */
2625 /* return indent length */
2626 static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos, guint text_len)
2627 {
2628         guint i_len = 0;
2629         guint i, ch_len, alnum_cnt = 0;
2630         IndentState state = WAIT_FOR_INDENT_CHAR;
2631         gchar cbuf[CHAR_BUF_SIZE];
2632         gboolean is_space;
2633         gboolean is_indent;
2634
2635         if (prefs_common.quote_chars == NULL) {
2636                 return 0 ;
2637         }
2638
2639         for (i = start_pos; i < text_len; i++) {
2640                 GtkTextIter iter;
2641
2642                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, i);
2643                 GET_CHAR(&iter, cbuf, ch_len);
2644                 if (ch_len > 1)
2645                         break;
2646
2647                 if (cbuf[0] == '\n')
2648                         break;
2649
2650                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2651                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2652
2653                 switch (state) {
2654                 case WAIT_FOR_SPACE:
2655                         if (is_space == FALSE)
2656                                 goto out;
2657                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2658                         break;
2659                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2660                         if (is_indent == FALSE && is_space == FALSE &&
2661                             !isupper((guchar)cbuf[0]))
2662                                 goto out;
2663                         if (is_space == TRUE) {
2664                                 alnum_cnt = 0;
2665                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2666                         } else if (is_indent == TRUE) {
2667                                 alnum_cnt = 0;
2668                                 state = WAIT_FOR_SPACE;
2669                         } else {
2670                                 alnum_cnt++;
2671                                 state = WAIT_FOR_INDENT_CHAR;
2672                         }
2673                         break;
2674                 case WAIT_FOR_INDENT_CHAR:
2675                         if (is_indent == FALSE && !isupper((guchar)cbuf[0]))
2676                                 goto out;
2677                         if (is_indent == TRUE) {
2678                                 if (alnum_cnt > 0 
2679                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2680                                         goto out;
2681                                 alnum_cnt = 0;
2682                                 state = WAIT_FOR_SPACE;
2683                         } else {
2684                                 alnum_cnt++;
2685                         }
2686                         break;
2687                 }
2688
2689                 i_len++;
2690         }
2691
2692 out:
2693         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2694                 i_len -= alnum_cnt;
2695
2696         return i_len;
2697 }
2698
2699 /* insert quotation string when line was wrapped */
2700 static guint ins_quote(GtkTextBuffer *textbuf, GtkTextIter *iter,
2701                        guint indent_len,
2702                        guint prev_line_pos, guint text_len,
2703                        gchar *quote_fmt)
2704 {
2705         guint ins_len = 0;
2706
2707         if (indent_len) {
2708                 GtkTextIter iter1, iter2;
2709                 gchar *text;
2710
2711                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter1,
2712                                                    prev_line_pos);
2713                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter2,
2714                                                    prev_line_pos + indent_len);
2715                 text = gtk_text_buffer_get_text(textbuf, &iter1, &iter2, FALSE);
2716                 if (!text) return 0;
2717
2718                 gtk_text_buffer_insert(textbuf, iter, text, -1);
2719                 ins_len = g_utf8_strlen(text, -1);
2720
2721                 g_free(text);
2722         }
2723
2724         return ins_len;
2725 }
2726
2727 static gboolean is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, guint start_pos) 
2728 {
2729         char *text = NULL;
2730         GtkTextIter iter; 
2731         GtkTextIter end_iter;
2732         if (!compose->account->sig_sep)
2733                 return FALSE;
2734         
2735         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, start_pos+1);
2736         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
2737                 start_pos+strlen(compose->account->sig_sep)+1);
2738
2739         if (!strcmp(gtk_text_iter_get_text(&iter, &end_iter),
2740                         compose->account->sig_sep)) {
2741                 /* check \n */
2742                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
2743                         start_pos+strlen(compose->account->sig_sep)+1);
2744                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
2745                         start_pos+strlen(compose->account->sig_sep)+2);
2746
2747                 if (!strcmp(gtk_text_iter_get_text(&iter, &end_iter),"\n"));
2748                         return TRUE;
2749                 
2750
2751         }
2752
2753         return FALSE;
2754 }
2755
2756 /* check if we should join the next line */
2757 static gboolean join_next_line_is_needed(GtkTextBuffer *textbuf,
2758                                          guint start_pos, guint tlen,
2759                                          guint prev_ilen, gboolean autowrap)
2760 {
2761         guint indent_len, ch_len;
2762         gboolean do_join = FALSE;
2763         gchar cbuf[CHAR_BUF_SIZE];
2764
2765         indent_len = get_indent_length(textbuf, start_pos, tlen);
2766
2767         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2768                 GtkTextIter iter;
2769                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
2770                                                    start_pos + indent_len);
2771                 GET_CHAR(&iter, cbuf, ch_len);
2772                 
2773                 if (ch_len > 0 && (cbuf[0] != '\n'))
2774                         do_join = TRUE;
2775         }
2776
2777         return do_join;
2778 }
2779
2780 static void compose_wrap_line_all(Compose *compose)
2781 {
2782         compose_wrap_line_all_full(compose, FALSE);
2783 }
2784
2785 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2786 {
2787         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2788         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2789         GtkTextIter iter, end_iter;
2790         guint tlen;
2791         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2792         gint line_len = 0, cur_len = 0;
2793         gint ch_len;
2794         gboolean is_new_line = TRUE, do_delete = FALSE;
2795         guint i_len = 0;
2796         gboolean linewrap_quote = TRUE;
2797         gboolean set_editable_pos = FALSE;
2798         gint editable_pos = 0;
2799         guint linewrap_len = prefs_common.linewrap_len;
2800         gchar *qfmt = prefs_common.quotemark;
2801         gchar cbuf[CHAR_BUF_SIZE];
2802         GtkTextMark *cursor_mark = gtk_text_buffer_get_insert(textbuf);
2803         
2804         tlen = gtk_text_buffer_get_char_count(textbuf);
2805
2806         for (; cur_pos < tlen; cur_pos++) {
2807                 /* mark position of new line - needed for quotation wrap */
2808                 if (is_new_line) {
2809                         if (linewrap_quote)
2810                                 i_len = get_indent_length(textbuf, cur_pos, tlen);
2811
2812                         is_new_line = FALSE;
2813                         p_pos = cur_pos;
2814                 }
2815
2816                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2817                 GET_CHAR(&iter, cbuf, ch_len);
2818
2819                 /* fix line length for tabs */
2820                 if (ch_len == 1 && *cbuf == '\t') {
2821                         guint tab_width = 8;
2822                         guint tab_offset = line_len % tab_width;
2823
2824                         line_len += tab_width - tab_offset - 1;
2825                         cur_len = line_len;
2826                 }
2827
2828                 /* we have encountered line break */
2829                 if (ch_len == 1 && *cbuf == '\n') {
2830                         gint clen;
2831                         gchar cb[CHAR_BUF_SIZE];
2832
2833                         /* should we join the next line */
2834                         if ((autowrap || i_len != cur_len) && do_delete &&
2835                         !is_sig_separator(compose, textbuf, cur_pos+i_len) &&
2836                             join_next_line_is_needed
2837                                 (textbuf, cur_pos + 1, tlen, i_len, autowrap)) {
2838                                 do_delete = TRUE;
2839                         } else
2840                                 do_delete = FALSE;
2841
2842                         /* skip delete if it is continuous URL */
2843                         if (do_delete && (line_pos - p_pos <= i_len) &&
2844                             gtkut_text_buffer_is_uri_string(textbuf, line_pos,
2845                                                             tlen))
2846                                 do_delete = FALSE;
2847
2848                         /* should we delete to perform smart wrapping */
2849                         if (line_len < linewrap_len && do_delete) {
2850                                 /* get rid of newline */
2851                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2852                                                                    &iter,
2853                                                                    cur_pos);
2854                                 end_iter = iter;
2855                                 gtk_text_iter_forward_char(&end_iter);
2856                                 gtk_text_buffer_delete(textbuf, &iter, &end_iter);
2857                                 tlen--;
2858
2859                                 /* if text starts with quote fmt or with
2860                                    indent string, delete them */
2861                                 if (i_len) {
2862                                         guint ilen;
2863                                         ilen =  gtkut_text_buffer_str_compare_n
2864                                                 (textbuf, cur_pos, p_pos, i_len,
2865                                                  tlen);
2866                                         if (ilen) {
2867                                                 end_iter = iter;
2868                                                 gtk_text_iter_forward_chars
2869                                                         (&end_iter, ilen);
2870                                                 gtk_text_buffer_delete(textbuf,
2871                                                                        &iter,
2872                                                                        &end_iter);
2873                                                 tlen -= ilen;
2874                                         }
2875                                 }
2876
2877                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2878                                                                    &iter,
2879                                                                    cur_pos);
2880                                 GET_CHAR(&iter, cb, clen);
2881
2882                                 /* insert space if it's alphanumeric */
2883                                 if ((cur_pos != line_pos) &&
2884                                     ((clen > 1) || isalnum((guchar)cb[0]))) {
2885                                         GtkTextIter cursor_iter;
2886                                         gboolean go_back = FALSE;
2887                                         gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
2888                                         if (gtk_text_iter_get_offset(&iter) ==
2889                                                  gtk_text_iter_get_offset(&cursor_iter))
2890                                                 go_back = TRUE;
2891                                         
2892                                         gtk_text_buffer_insert(textbuf, &iter,
2893                                                                " ", 1);
2894                                         if (go_back) {
2895                                                 gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
2896                                                 gtk_text_iter_backward_chars(&cursor_iter, 1);
2897                                                 gtk_text_buffer_place_cursor(textbuf, &cursor_iter);
2898                                         }
2899                                         tlen++;
2900                                 }
2901
2902                                 /* and start over with current line */
2903                                 cur_pos = p_pos - 1;
2904                                 line_pos = cur_pos;
2905                                 line_len = cur_len = 0;
2906                                 do_delete = FALSE;
2907                                 is_new_line = TRUE;
2908 #ifdef WRAP_DEBUG
2909                                 g_print("after delete l_pos=");
2910                                 dump_text(text, line_pos, tlen, 1);
2911 #endif
2912                                 /* move beginning of line if we are on LF */
2913                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2914                                                                    &iter,
2915                                                                    line_pos);
2916                                 GET_CHAR(&iter, cb, clen);
2917                                 if (clen == 1 && *cb == '\n')
2918                                         line_pos++;
2919 #ifdef WRAP_DEBUG
2920                                 g_print("new line_pos=%d\n", line_pos);
2921 #endif
2922
2923                                 continue;
2924                         }
2925
2926                         /* mark new line beginning */
2927                         line_pos = cur_pos + 1;
2928                         line_len = cur_len = 0;
2929                         do_delete = FALSE;
2930                         is_new_line = TRUE;
2931                         continue;
2932                 }
2933
2934                 if (ch_len < 0) {
2935                         cbuf[0] = '\0';
2936                         ch_len = 1;
2937                 }
2938
2939                 /* possible line break */
2940                 if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2941                         line_pos = cur_pos + 1;
2942                         line_len = cur_len + ch_len;
2943                 }
2944
2945                 /* are we over wrapping length set in preferences ? */
2946                 if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
2947                         gint clen;
2948
2949 #ifdef WRAP_DEBUG
2950                         g_print("should wrap cur_pos=%d ", cur_pos);
2951                         dump_text(text, p_pos, tlen, 1);
2952                         dump_text(text, line_pos, tlen, 1);
2953 #endif
2954                         /* force wrapping if it is one long word but not URL */
2955                         if (line_pos - p_pos <= i_len)
2956                                 if (!gtkut_text_buffer_is_uri_string
2957                                     (textbuf, line_pos, tlen))
2958                                         line_pos = cur_pos - 1;
2959 #ifdef WRAP_DEBUG
2960                         g_print("new line_pos=%d\n", line_pos);
2961 #endif
2962
2963                         gtk_text_buffer_get_iter_at_offset(textbuf,
2964                                                            &iter,
2965                                                            line_pos - 1);
2966                         GET_CHAR(&iter, cbuf, clen);
2967
2968                         /* if next character is space delete it */
2969                         if (clen == 1 && isspace(*(guchar *)cbuf)) {
2970                                 if (p_pos + i_len != line_pos ||
2971                                     !gtkut_text_buffer_is_uri_string
2972                                         (textbuf, line_pos, tlen)) {
2973                                         /* workaround for correct cursor
2974                                            position */
2975                                         if (set_editable_pos == FALSE) {
2976                                                 GtkTextMark *ins = gtk_text_buffer_get_insert(textbuf);
2977                                                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, ins);
2978                                                 editable_pos = gtk_text_iter_get_offset(&iter);
2979                                                 if (editable_pos == line_pos)
2980                                                         set_editable_pos = TRUE;
2981                                         }
2982                                         gtk_text_buffer_get_iter_at_offset(textbuf,
2983                                                            &iter,
2984                                                            line_pos-1);
2985                                         gtk_text_buffer_get_iter_at_offset(textbuf,
2986                                                            &end_iter,
2987                                                            line_pos);
2988                                         gtk_text_buffer_delete(textbuf, &iter, &end_iter);
2989                                         //gtk_stext_set_point(text, line_pos);
2990                                         //gtk_stext_backward_delete(text, 1);
2991                                         tlen--;
2992                                         cur_pos--;
2993                                         line_pos--;
2994                                         cur_len--;
2995                                         line_len--;
2996                                 }
2997                         }
2998
2999                         /* if it is URL at beginning of line don't wrap */
3000                         if (p_pos + i_len == line_pos &&
3001                             gtkut_text_buffer_is_uri_string(textbuf, line_pos, tlen)) {
3002 #ifdef WRAP_DEBUG
3003                                 g_print("found URL at ");
3004                                 dump_text(text, line_pos, tlen, 1);
3005 #endif
3006                                 continue;
3007                         }
3008
3009                         /* insert CR */
3010                         gtk_text_buffer_get_iter_at_offset(textbuf,
3011                                                            &iter,
3012                                                            line_pos);
3013                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
3014                         //gtk_stext_set_point(text, line_pos);
3015                         //gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
3016                         //gtk_stext_compact_buffer(text);
3017                         tlen++;
3018                         line_pos++;
3019                         /* for loop will increase it */
3020                         cur_pos = line_pos - 1;
3021                         /* start over with current line */
3022                         is_new_line = TRUE;
3023                         line_len = cur_len = 0;
3024                         if (autowrap || i_len > 0) {
3025                                 do_delete = TRUE;
3026                         } else
3027                                 do_delete = FALSE;
3028 #ifdef WRAP_DEBUG
3029                         g_print("after CR insert ");
3030                         dump_text(text, line_pos, tlen, 1);
3031                         dump_text(text, cur_pos, tlen, 1);
3032 #endif
3033
3034                         /* should we insert quotation ? */
3035                         if (linewrap_quote && i_len) {
3036                                 /* only if line is not already quoted  */
3037                                 if (!gtkut_text_buffer_str_compare
3038                                         (textbuf, line_pos, tlen, qfmt)) {
3039                                         guint ins_len;
3040                                         
3041                                         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
3042                                         
3043                                         if (line_pos - p_pos > i_len) {
3044                                                 ins_len = ins_quote
3045                                                         (textbuf, &iter, i_len, p_pos,
3046                                                          tlen, qfmt);
3047                                                 tlen += ins_len;
3048                                         }
3049 #ifdef WRAP_DEBUG
3050                                         g_print("after quote insert ");
3051                                         dump_text(text, line_pos, tlen, 1);
3052 #endif
3053                                 }
3054                         }
3055                         continue;
3056                 }
3057
3058                 if (ch_len > 1) {
3059                         line_pos = cur_pos + 1;
3060                         line_len = cur_len + ch_len;
3061                 }
3062                 /* advance to next character in buffer */
3063                 cur_len += ch_len;
3064         }
3065
3066         if (set_editable_pos && editable_pos <= tlen) {
3067                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, editable_pos);
3068                 gtk_text_buffer_place_cursor(textbuf, &iter);
3069         }
3070 }
3071
3072 #undef GET_CHAR
3073 #undef CHAR_BUF_SIZE
3074
3075 static void compose_set_title(Compose *compose)
3076 {
3077         gchar *str;
3078         gchar *edited;
3079
3080         edited = compose->modified ? _(" [Edited]") : "";
3081         if (compose->account && compose->account->address)
3082                 str = g_strdup_printf(_("%s - Compose message%s"),
3083                                       compose->account->address, edited);
3084         else
3085                 str = g_strdup_printf(_("Compose message%s"), edited);
3086         gtk_window_set_title(GTK_WINDOW(compose->window), str);
3087         g_free(str);
3088 }
3089
3090 /**
3091  * compose_current_mail_account:
3092  * 
3093  * Find a current mail account (the currently selected account, or the
3094  * default account, if a news account is currently selected).  If a
3095  * mail account cannot be found, display an error message.
3096  * 
3097  * Return value: Mail account, or NULL if not found.
3098  **/
3099 static PrefsAccount *
3100 compose_current_mail_account(void)
3101 {
3102         PrefsAccount *ac;
3103
3104         if (cur_account && cur_account->protocol != A_NNTP)
3105                 ac = cur_account;
3106         else {
3107                 ac = account_get_default();
3108                 if (!ac || ac->protocol == A_NNTP) {
3109                         alertpanel_error(_("Account for sending mail is not specified.\n"
3110                                            "Please select a mail account before sending."));
3111                         return NULL;
3112                 }
3113         }
3114         return ac;
3115 }
3116
3117 static void compose_select_account(Compose *compose, PrefsAccount *account,
3118                                    gboolean init)
3119 {
3120         GtkItemFactory *ifactory;
3121
3122         g_return_if_fail(account != NULL);
3123
3124         compose->account = account;
3125
3126         compose_set_title(compose);
3127
3128         ifactory = gtk_item_factory_from_widget(compose->menubar);
3129 #if 0
3130         if (account->protocol == A_NNTP) {
3131                 gtk_widget_show(compose->newsgroups_hbox);
3132                 gtk_widget_show(compose->newsgroups_entry);
3133                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
3134                 compose->use_newsgroups = TRUE;
3135
3136                 menu_set_active(ifactory, "/View/To", FALSE);
3137                 menu_set_sensitive(ifactory, "/View/To", TRUE);
3138                 menu_set_active(ifactory, "/View/Cc", FALSE);
3139                 menu_set_sensitive(ifactory, "/View/Cc", TRUE);
3140                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
3141         } else {
3142                 gtk_widget_hide(compose->newsgroups_hbox);
3143                 gtk_widget_hide(compose->newsgroups_entry);
3144                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
3145                 gtk_widget_queue_resize(compose->table_vbox);
3146                 compose->use_newsgroups = FALSE;
3147
3148                 menu_set_active(ifactory, "/View/To", TRUE);
3149                 menu_set_sensitive(ifactory, "/View/To", FALSE);