Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

One button text processing

by nysus (Parson)
on Jul 15, 2003 at 03:50 UTC ( [id://274270]=CUFP: print w/replies, xml ) Need Help??

Greetings, Monks,

Here's a short but powerful Tk program that runs on Win32 systems but could easily be exported to any OS, I'm sure.

The program takes any text copied to the clipboard and instantly reformats it for you. For example, you highlight some text in any application than hit CTRL-C. Then you immediately hit CTRL-V and ouila, the text is processed any way you want. This program will allow you to instantly strip out HTML tags, remove comments from code, and eliminate just about any repetitive text processing task that you can think of.

Currently, the program is set up to instantly change the case of text sent to the clipboard. Easily switch between lowercase, uppercase, mixed case, and sentence case.

Special thanks to bobn for informing me about the repeat method that made this code possible. However, if anyone has a more elegant solution, please let me know. Also, please let me know if you see any glaring errors or suggestions for improvement. Here's the stuff:

use strict; use Win32::Clipboard; use Win32::API; use Tk; # Windows constants my ($OnTop, $NoTop, $Top) = (-1, -2, 0); my ($SWP_NOMOVE, $SWP_NOSIZE) = (2, 1); # 'always on top' state my $isOnTop = $NoTop; # Create a Win32::API object for SetWindowPos my $SetWindowPos = new Win32::API("user32", "SetWindowPos", ['N','N',' +N','N','N','N','N'], 'N'); # and one for FindWindow my $FindWindow = new Win32::API("user32", "FindWindow", ['P','P'], 'N' +); my $mode = 0; my $old_contents; my $clip = Win32::Clipboard(); my $button_pressed; my $toggle_mode = 0; my $main = new MainWindow; my $toggle_button = $main->Checkbutton(-text => "Turn on toggle mode", -onvalue => 1, -offvalue => 0, -variable => \$toggle_mode, -command => sub { !($toggle_mode) } ) ->pack(-side => "bottom", expand => "yes", -fill => "x"); my $aotCBut = $main->Checkbutton(-text => "Always on top", -onvalue => $OnTop, -offvalue => $NoTop, -variable => \$isOnTop, -command => sub { # get a handle to the toplevel window containing our Perl/Tk a +pp my $class = "TkTopLevel"; my $name = $main->title; my $NULL = 0; my $topHwnd = $FindWindow->Call($class, $name); if ($topHwnd != $NULL) { # change 'always on top' state $SetWindowPos->Call($topHwnd, $isOnTop, 0, 0, 0, 0, $SWP_N +OMOVE | $SWP_NOSIZE); }; }) ->pack(-side => "bottom", expand => "yes", -fill => "x"); $main->Label(-text => 'Clipboard Case Changer' )->pack; $main->Button(-text => 'To Uppercase', -command => sub { &change_mode(1) } )->pack; $main->Button(-text => 'To Lowercase', -command => sub { &change_mode(2) } )->pack; $main->Button(-text => 'To Sentence Case', -command => sub { &change_mode(3) } )->pack; $main->Button(-text => 'To Mixed Case', -command => sub { &change_mode(4) } )->pack; $main->Button(-text => 'Off', -command => sub { &change_mode(0) } )->pack; $main->repeat(50 => \&run); MainLoop; sub change_mode { print @_; $button_pressed = 1; $mode = shift; } sub run { my $new_contents = $clip->Get(); if ((($new_contents ne $old_contents) && $mode) || $button_pressed +) { if ($mode == 1) { $new_contents = uc($new_contents); } elsif ($mode == 2) { $new_contents = lc($new_contents); } elsif ($mode == 3) { $new_contents =~ tr/A-Z/a-z/; $new_contents =~ s/(\.\s+\b|^\s*\b)(.)/$1 . uc($2)/xge; } elsif ($mode == 4) { $new_contents =~ tr/A-Z/a-z/; $new_contents =~ s/\b(.)/uc($1)/ge; } $clip->Set($new_contents); $old_contents = $new_contents; $button_pressed = 0; $mode = 0 if !($toggle_mode); } }

Update: Code has been modified to allow it to remain "Always on Top" of the other windows. Many thanks to kevin_i_orourke who figured out how to do it many moons ago and posted his results on PerlMonks.

Update to update: OK, last modification, I swear. I need to get to sleep anyway. This is a simple modification to the code but rather hard to explain. I created a (badly named) "toggle mode." When in toggle mode, the last kind of case change you selected stays in effect until you either click "off" or deselect the "toggle mode" check box. In other words, when in toggle mode, all text copied to the clipboard is automatically reformatted. When not in toggle mode, the user must click one of the buttons before text will be reformatted.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: One button text processing
by tilly (Archbishop) on Jul 15, 2003 at 06:44 UTC
    Various comments.

    1. You are synchronizing two parts of your code with numerical codes. These will become problematic to keep in sync. It is better to build one data structure with all of the necessary information and then loop through it a couple of times to produce both data structures, thereby keeping like stuff together. For instance have an array with elements like
      ['To Mixed Case', sub { tr/A-Z/a-z/; s/\b(.)/uc($1)/ge; }],
      and then use the first field as the text, and build up a function lookup hash used in the run() function instead of your current case. (Pass the text to the run() function and then there is no synchronization needed.)
    2. Note that above I used $_. Since you are likely to write many cases, and commands default to $_, for this special purpose program I think that $_ makes sense.
    3. You are limiting the user to only the commands that you have thought of and pre-built. I would recommend adding in commands where the execution of the command depends on a text field where the user can enter something more sophisticated - for instance their own substitution, arbitrary code, the name of a file to save the clipboard in and so on. Note that this will make your Tk display necessarily more complex, and complicates the first suggestion that I made. Solving that is left as an exercise to the reader...
    4. You will eventually run across the fact that Win32::Clipboard leaves line endings as \r\n. It is convenient to convert \r\n into \n on the way into the run() function, and convert \n back into \r\n on the way out.
    5. Your assumption that this can be modified for other operating systems is only partially correct. The *nix model of having selecting text be the same as putting it into the clipboard makes using a tool like the above be inconvenient for in place editing. You select text, edit the clipboard, then you have to go back and paste it, then reselect, and then delete the original. Ugh. You would be better advised to use hooks in your editor.
    6. In fact my final suggestion is to just learn your editor better. I say this because a clipboard editor (command line, but on the same principle as the above) was the first useful program that I wrote for myself in Perl. I learned this program, with the result that for a long time I used my homegrown tool rather than learning my editor better. But long-term, I think that learning the editor is likely to be more productive.
    I hope that this feedback proves useful to you.
      Tilly,

      Thanks for the tips, bro. I especially like the first one, if I understand you correctly. Instead of using arbitrary numbers to match code, just use a hash. Very neat. I'll try that out.

      As far as learn my editor better, well, one of the major reasons I wrote this program is because I do web design with Dreamweaver which does not have change of case functionality. Also, I use UltraEdit in Windows which also has limited text processing capabilities. It's no Vim or Emacs as far as shortcut keys go. However, UE works very well as a cheap IDE, allowing me to run test code and debug Perl programs very quickly. I tried using Vim (which I used fairly extensively when I used Linux) in Windows the other night but found it a huge pain to use just to test some simple code compared to UE. And I don't really do enough programming to be able to take full advantage of a Vim or Emacs. I've written maybe 6 to 10 small Perl programs in the last year. So by the time it's time for me to write another program, I've all but forgotten what what the shortcut keys are. It gets very frustrating having to look up the simplest keystrokes over and over. But I'm certainly open to suggestions for a better IDE/editor for Perl on Windows if you can recommend one.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

        Also, I use UltraEdit in Windows which also has limited text processing capabilities

        I beg to differ on that one...I am using version 10 and I find it to be as feature rich as I need. No Emacs sure but still a professional editor...what can't you automate?

      • Upper case: Alt F5
      • Capitalise: F5
      • Advanced->configuration->key mapping
      • ...

        Having said that, I like your utility.. :-)

        For more editor choices than you can shake a stick at, see Code Editors and Development Environments. But still I think that bm underscored the value in learning your existing editor better.

        However you're right that this will not help you with Dreamweaver. And from experience I can tell you that editing your text with a small Perl utility will work, and gives you lots of opportunity to learn Perl better. Whether or not this is more useful to you than learning editors in general is a judgement call.

        nysus,

        UltraEdit looks very much like http://www.chami.com/html-kit/

        May be that you should try it (FREE). It has some macro stuff that looks like JavaScript and there is a lot of modules to extend functionality as much as you want.

        Anyway, I am fond of Xemacs. But HTML-KIT has also a module that helps editing Perl scripts, checks syntax and runs perl scripts...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found