Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: simple text editor

by jdporter (Paladin)
on Nov 06, 2005 at 20:28 UTC ( [id://506149]=note: print w/replies, xml ) Need Help??


in reply to simple text editor

Cool! That has inspired me to write a "minimal" Tk editor. This punts on everything from file reading/writing to appearance control.

=pod This program reads text from "stdin" and writes to "stdout" on exit. It can be used to do in-place editing of a file by executing with perl + -i.bak If you exit via 'OK' or 'Cancel', the process exit code will be 0 or 1 +, respectively. If you exit with 'Cancel' and are NOT running with perl -i, the text w +ill not be printed to "stdout". Examples: % foo | perl stream_editor_tk | bar # foo produces text; # stream_editor_tk is this program; # bar consumes text. % perl -i.bak stream_editor_tk some_doc.txt # edit a file in place. =cut use Tk; use strict; use warnings; my $orig = $_ = do { local $/; <> }; my $code = 1; my $mw = new MainWindow( -title => 'Edit' ); my $fr = $mw->Frame( -border => 2, -relief => 'raised' )->pack( -side +=> 'bottom', -fill => 'both', ); my $text = $mw->Scrolled( 'Text', -wrap => 'none' )->pack( -side => 'b +ottom', -fill => 'both', -expand => 1 ); $fr->Button( -text => 'Reset', -command => sub { $text->delete( '1.0', + 'end' ); $text->insert( end => $_ ) } )->place( -relx => 0.25, -anch +or => 'n', ); my $ok = $fr->Button( -text => 'OK', -command => sub { print $text->ge +t( '1.0', 'end' ); $code = 0; $mw->destroy } )->place( -relx => 0.5, +-anchor => 'n', ); $fr->Button( -text => 'Cancel', -command => sub { defined $^I and prin +t $orig; $mw->destroy } )->place( -relx => 0.75, -anchor => 'n', ); $mw->after( 100, sub { $fr->configure( -height => $ok->height + 5 ); } + ); $text->insert( end => $_ ); MainLoop; exit $code;
We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: simple text editor
by sauoq (Abbot) on Nov 06, 2005 at 20:41 UTC
    That has inspired me to write a "minimal" Tk editor.

    You can probably get somewhat closer to "minimal" (depending on your def'n) using Tk::TextUndo. Something like this will get you most of the way there:

    perl -MTk -MTk::TextUndo -e "(tkinit)->Scrolled('TextUndo')->pack;Main +Loop"
    Right click for a menu. Some menu items require implementation.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^2: simple text editor
by chanio (Priest) on Nov 25, 2005 at 05:10 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://506149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found