Elijah has asked for the wisdom of the Perl Monks concerning the following question:

I want to code a line numbering on my text editor on the left hand side of the text in the same Scrolled Text widget. I have thought about this for a few days and first thought about creating a binding to the <enter> key but this would only work on new files and not loaded files already existing on the hard disk. I then thought about doing something with the (\n) new line character but thought there has to be a better way.

I also need the line numbering to be trasparent to a file save. I am not even sure if it is possible to have this 4 character column be part of the same scrolled widget and also be formatted as a different color background.

If you want to see my full source code you can download it from www.perlskripts.com and get a better idea of what I am already working with.

Any help is appreciated.

  • Comment on Create a graphical line numbering for my text editor.

Replies are listed 'Best First'.
Re: Create a graphical line numbering for my text editor.
by PodMaster (Abbot) on Jan 07, 2004 at 00:38 UTC
    That can be tough (wrapping this, wrapping that, woosh). You should definetly add a status bar which displays current line/column. As for the line numbers, the easy way would be to just add a read-only text widget on the left :) something like
    # # [0001][#!/usr/bin/perl ][^] # [0002][ ][ ] # [0003][=head1 NAME ][ ] # [0004][ ][ ] # [....][...........................][ ] # # In pEdit version 0.7 (beta release) # replace the # # my $t = $mw->Scrolled("Text", ... # # line with the following my $foy = $mw->Frame->pack( -fill => 'both', -expand => 1, ); my $linenumbers = $foy->ROText( -width => 4, -font => ['Courier New', '10'], -background => '#c0c0c0', )->pack( -pady => 0, -padx => 0, -ipady => 0, -ipadx => 0, -side => 'left', -fill=> 'y', -expand =>0, );#Tk::ROText $linenumbers->insert('end', join "\n", map {sprintf "%4d", $_ } 1 .. 1 +20); my $t = $foy->Text( -font => ['Courier New', '10'], )->pack( -pady => 0, -padx => 0, -ipady => 0, -ipadx => 0, -side => 'right', -fill => 'both', -expand => 1, ); my $scroll = $t->Scrollbar( # -orient => 'vertical', -command => sub { my(@foo)= @_; $t->yview(@foo); $linenumbers->yview(@foo); }, ); $t->configure( -yscrollcommand => [ set => $scroll ], ); $linenumbers->configure( -yscrollcommand => [ set => $scroll ], ); $scroll->pack( -side => 'right', -fill => 'y', );

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Yeah I want to code a status bar too to show the current line and character number that the cursor is at but am having a difficult time discovering how to tell what line and character number the cursor is at.

      Edit: Yes something like that. It would need to scroll with the main Text widget though and increase the more lines put in and decrease with lines taken away. It is a good start though.

      I was just reading the Scrollbar.pm too trying to get an idea on how to tell where the cursor is and how the module does it. It uses some subroutines called "ScrlByUnits" to move it and I assume it tracks it also. Maybe I can use these subs to report back their results or mimic them in my script.

        Remember how I mentioned gedi? Just see what gedi does ... see $text->index(index) and THE INSERTION CURSOR parts of the Tk::Text documentation.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.