use Tk; use Tk::BrowseEntry; use Tk::Dialog; my $note = "This is the 'Subject' area. Usually, you will paste something in here. Below is some code that fires when you click the 'stringilize' button. It modifies the text in this area. Change it to whatever you like. Errors in your code (if there are any), will be inserted here. "; my $def_code =<get('1.0','end') ); for (\@subject_lines) { # Do something, e.g. # tr/a-z/A-Z/; } \$subject_text->delete('1.0', 'end'); \$subject_text->insert( 'end', join("\\n", \@subject_lines) ); EOC my $snippets_dir = 'snippets'; my $mainw = MainWindow->new(-title=>'Stringilizer'); my $tags; my $dia = $mainw->Dialog( -title =>'Tag snippet', -default_button=>'Save' ); $dia->add( 'LabEntry', -textvariable=>\$tags, -label=>'Enter tags to save snippet' )-> pack(-side => 'left')-> configure(-relief=>'groove'); $dia->protocol("WM_DELETE_WINDOW" => sub {} ); my $frame1 = $mainw->Frame->pack( -side => 'top', -fill => 'x' ); $frame1->Button( -text => "exit", -command => sub { $mainw->destroy; } )-> pack(-side => 'left')-> configure(-relief=>'groove'); $frame1->Button(-text => "stringilize", -command => \&stringilize_exec )-> pack(-side => 'left')-> configure(-relief=>'groove'); $frame1->Button(-text => "save", -command => \&save_snippet )-> pack(-side => 'left')-> configure(-relief=>'groove'); $code_text = $mainw->Scrolled("Text")-> pack(-side => 'bottom', -fill => 'both', -expand => 1); $subject_text = $mainw->Scrolled("Text")-> pack(-side => 'bottom', -fill => 'both', -expand => 1); $subject_text->configure(-height => 12, -scrollbars=>'se'); $code_text->configure(-height => 18, -scrollbars=>'se'); $subject_text->insert('end', $note); $code_text->insert('end', $def_code); my $snippet; my $be = $frame1->BrowseEntry( -label=>'snippets', -browsecmd=> \&load_snippet, -variable=> \$snippet ); load_snippet_tags(); $be->pack( -side => 'left')->configure( -relief=>'groove'); MainLoop; sub stringilize_exec { # Put the code area text into variable my $code = $code_text->get("1.0", 'end'); eval($code); # Report error if ($@) { $subject_text->insert('1.0', "ERROR: $@\n"); } } sub load_snippet_tags { $be->delete('0', 'end'); if (!-e $snippets_dir and !-d $snippets_dir) { mkdir $snippets_dir; return; } opendir(DIR, $snippets_dir) || die "$!"; my @files = grep { /txt$/i && -f "$snippets_dir\/$_" } readdir(DIR); closedir(DIR); for (@files) { my $f = $_; $f =~ s/\.txt$//; $be->insert('end', $f); } } sub load_snippet { my $file = $snippet . '.txt'; open(F, "$snippets_dir\/$file") or die "$! $snippets_dir\/$file"; my $s; while(){ $s .= $_ } close(F); $code_text->delete('1.0', 'end'); $code_text->insert('end', $s); } sub save_snippet { my $ans = $dia->Show; if ($tags ne '') { $tags =~ s/\s/_/g; $tags =~ s/\W//g; $tags =~ s/_/ /g; open(F, ">$snippets_dir\/$tags\.txt") or die "$!"; print F $code_text->get('1.0','end'); close(F); load_snippet_tags(); undef($tags); } }