#!perl # ===TODO=== # # ****FIX THE TEST TO SEE IF THERE IS TEXT IN THE Text WIDGET**** # -cracker proof the Save File As and margin entries bwo -validate # -on a really ambitious and slow day let the user define the Text font, background, etc. use warnings; # use strict; use Tk; require Tk::LabFrame; use Text::Autoformat; use Text::Wrap qw($columns &wrap); my $mw = MainWindow->new(); my $frame1 = $mw -> LabFrame ( -label => "Paste Original Email Here", -padx => 5, -pady => 5, ) -> pack( -fill => "both" ); my $text = $frame1 -> Scrolled("Text", -scrollbars => 'e', -wrap => "word", ) -> pack( -fill => "both"); my $frame2 = $frame1 -> Frame -> pack ( -side => "left", -expand => 1, -fill => "both", ); my $click = $frame2 -> Button ( -text => "Make Reply", -command => sub { if (eval($text -> get("1.0"))) { &flip_lab; my $new_text = &mung_email; $text -> delete ('1.0', 'end'); $text -> insert ('end', $new_text); &flip_click; } } ) -> pack; my $frame3 = $frame1 -> Frame -> pack ( -side => "right", -expand => 1, -fill => "both", ); $frame3 -> Button ( -text => "Clear Text", -command => sub { $text -> delete ('1.0', 'end'); if ($click -> cget(-text) eq "Copy to Clipboard") { &flip_click; &flip_lab; } }, ) -> pack; my $frame4 = $mw -> LabFrame (-label => "Options and Actions") -> pack( -side => "bottom", -fill => "both", ); my $frame5 = $frame4 -> Frame -> pack ( -side => "bottom", -fill => "both", ); $frame5 -> Label ( -text => "Reply Marker" ) -> pack ( -side => "left" ); my $reply_marker = $frame5 -> Entry ( -width => 3 ) -> pack ( -side => "left" ); $reply_marker -> insert ('end', ">"); my $file_action = "Append"; foreach ("Make New", "Append") { $frame5 -> Radiobutton ( -text => "$_", -value => "$_", -variable => \$file_action, ) -> pack( -side => "right" ); } my $file_name_entry; $frame4 -> Button ( -text => "Make .txt File", -command => sub { my $action_marker = ">"; $action_marker = ">>" if ($file_action eq "Append"); if ( $file_name_entry->get() ) { open WRITE, $action_marker . $file_name_entry->get() . "\.txt"; if ($file_action eq "Append") { print WRITE "\n==============================\n"; } print WRITE $text->get("1.0", "end"); close WRITE; } } ) -> pack (-side => "right"); $file_name_entry = $frame4 -> Entry -> pack (-side => "right"); $frame4 -> Label (-text => "Save As") ->pack (-side => "right"); $frame4 -> Label ( -text => "Left Margin" ) -> pack ( -side => "left" ); my $left_margin = $frame4 -> Entry ( -width => 3 ) -> pack (-side => "left"); $left_margin -> insert ('end', 1); $frame4 -> Label ( -text => "Right Margin" ) -> pack ( -side => "left" ); my $right_margin = $frame4 -> Entry ( -width => 3 ) -> pack (-side => "left"); $right_margin -> insert ('end', 60); $text->focus; MainLoop; sub mung_email_with_autoformat { my $rough_draft = autoformat ($text -> get('1.0', 'end'), {left => $left_margin -> get(), right => $right_margin -> get(), }, ); my @lines = split ("\n", $rough_draft); my $final_draft; foreach (@lines) { $final_draft .= $reply_marker->get() . "$_\n"; } return $final_draft; } sub mung_email { $columns = $right_margin -> get(); my $left_space; foreach (1..$left_margin->get()) { $left_space .= " "; } my $rough_draft = $text -> get('1.0', 'end'); $rough_draft =~ s/([^\s])\n/$1 /g; $rough_draft =~ s/\n/\n\n/g; $rough_draft = wrap($left_space, $left_space, $rough_draft); my @lines = split ("\n", $rough_draft); my $final_draft; foreach (@lines) { $final_draft .= $reply_marker->get() . "$_\n"; } return $final_draft; } sub flip_click { if ($click->cget(-text) eq "Make Reply") { $click -> configure ( -text => "Copy to Clipboard", -command => sub { $mw->clipboardClear; $mw->clipboardAppend( $text -> get('1.0', 'end') ); } ); } else { $click -> configure ( -text => "Make Reply", -command => sub { if (eval($text -> get("1.0"))) { &flip_lab; my $new_text = &mung_email; $text -> delete ('1.0', 'end'); $text -> insert ('end', $new_text ); &flip_click; } } ); } } sub flip_lab { if ($frame1 -> cget (-label) eq "Paste Original Email Here") { $frame1 -> configure ( -label => "Type Your Reply" ) } else { $frame1 -> configure ( -label => "Paste Original Email Here" ); } }