| 1 |
#!/usr/local/bin/perl -w |
| 2 |
|
| 3 |
############################################################################### |
| 4 |
# Copyright (c) 1999 Greg London |
| 5 |
# All rights reserved. |
| 6 |
# This program is free software. |
| 7 |
# You can redistribute it and/or modify it under the same terms as Perl itself. |
| 8 |
############################################################################### |
| 9 |
|
| 10 |
############################################################################### |
| 11 |
# This is a perl application, called gedi, implementing a text editor. |
| 12 |
# gedi is short for Greg's EDItor. The "g" being pronounced like a "j". |
| 13 |
############################################################################### |
| 14 |
|
| 15 |
|
| 16 |
require 5; |
| 17 |
use locale; |
| 18 |
use strict; |
| 19 |
|
| 20 |
use Tk; |
| 21 |
use Tk::widgets qw(TextEdit); |
| 22 |
use File::Basename; |
| 23 |
|
| 24 |
########################################### |
| 25 |
# check command line parameter. |
| 26 |
# if none, start with file called 'NewFile' |
| 27 |
# if -help, print help |
| 28 |
# if filename, open file or die |
| 29 |
# note, wildcard automatically gets handled by perl interpreter, |
| 30 |
# so that @ARGV contains list of matches. |
| 31 |
########################################### |
| 32 |
|
| 33 |
# Create MainWindow first to handle X11 options. |
| 34 |
my $top = MainWindow->new(); |
| 35 |
|
| 36 |
my $argcount = @ARGV; |
| 37 |
my ($global_filename) = @ARGV; |
| 38 |
|
| 39 |
if ($argcount>1) |
| 40 |
{ |
| 41 |
print "\n"; |
| 42 |
print "ERROR: too many files specified. \n"; |
| 43 |
die "\n"; |
| 44 |
} |
| 45 |
|
| 46 |
if ($argcount == 0) |
| 47 |
{$global_filename = 'NoName';} |
| 48 |
|
| 49 |
if ( |
| 50 |
($global_filename eq 'help') || |
| 51 |
($global_filename eq '-help') || |
| 52 |
($global_filename eq '-h') || |
| 53 |
($global_filename eq '-?') |
| 54 |
) |
| 55 |
{ |
| 56 |
print "\n"; |
| 57 |
print "$0 expects one command line argument: \n"; |
| 58 |
print " the name of the file to edit \n"; |
| 59 |
die "\n"; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
# want FileSelect to use the last used directory as the starting directory |
| 64 |
# store directory in $global_directory. |
| 65 |
my $global_directory = dirname($global_filename); |
| 66 |
|
| 67 |
############################################## |
| 68 |
############################################## |
| 69 |
## input parameters have been filtered. |
| 70 |
## set up three frames to put everything into. |
| 71 |
## menu_frame, text_frame, counter_frame |
| 72 |
############################################## |
| 73 |
############################################## |
| 74 |
# my $menu_frame = $top->Frame->pack(-anchor=>'nw'); |
| 75 |
my $text_frame = $top->Frame->pack |
| 76 |
(-anchor=>'nw', -expand=>'yes', -fill => 'both'); # autosizing |
| 77 |
my $counter_frame = $top->Frame->pack(-anchor=>'nw'); |
| 78 |
|
| 79 |
############################################## |
| 80 |
############################################## |
| 81 |
## now set up text window with contents. |
| 82 |
############################################## |
| 83 |
############################################## |
| 84 |
|
| 85 |
## autosizing is set up such that when the outside window is |
| 86 |
## resized, the text box adjusts to fill everything else in. |
| 87 |
## the text frame and the text window in the frame are both |
| 88 |
## set up for autosizing. |
| 89 |
|
| 90 |
my $textwindow = $text_frame->Scrolled( |
| 91 |
'TextEdit', |
| 92 |
-exportselection => 'true', # 'sel' tag is associated with selections |
| 93 |
# initial height, if it isnt 1, then autosizing fails |
| 94 |
# once window shrinks below height |
| 95 |
# and the line counters go off the screen. |
| 96 |
# seems to be a problem with the Tk::pack command; |
| 97 |
-height => 1, |
| 98 |
-background => 'white', |
| 99 |
-wrap=> 'none', |
| 100 |
-setgrid => 'true', # use this for autosizing |
| 101 |
-scrollbars =>'se') |
| 102 |
-> pack(-expand => 'yes' , -fill => 'both'); # autosizing |
| 103 |
|
| 104 |
#$textwindow->FileName($global_filename); |
| 105 |
|
| 106 |
|
| 107 |
$top->protocol('WM_DELETE_WINDOW'=> |
| 108 |
sub{$textwindow->ConfirmExit;} |
| 109 |
); |
| 110 |
|
| 111 |
$SIG{INT} = sub {$textwindow->ConfirmExit;}; |
| 112 |
|
| 113 |
############################################## |
| 114 |
############################################## |
| 115 |
## set up current line number display |
| 116 |
############################################## |
| 117 |
############################################## |
| 118 |
my $current_line_label = $counter_frame |
| 119 |
-> Label(-text=>'line: 1') |
| 120 |
-> grid(-row=>1,-column=>1, -sticky=>'nw' ); |
| 121 |
|
| 122 |
my $total_line_label = $counter_frame |
| 123 |
-> Label(-text=>'total lines: 1') |
| 124 |
-> grid(-row=>2,-column=>1, -sticky=>'nw' ); |
| 125 |
|
| 126 |
my $current_column_label = $counter_frame |
| 127 |
-> Label(-text=>'column: 0') |
| 128 |
-> grid(-row=>3,-column=>1, -sticky=>'nw' ); |
| 129 |
|
| 130 |
my $insert_overstrike_mode_label = $counter_frame |
| 131 |
-> Label(-text=>' ') |
| 132 |
-> grid(-row=>5,-column=>1, -sticky=>'nw' ); |
| 133 |
|
| 134 |
sub update_indicators |
| 135 |
{ |
| 136 |
my ($line,$column)= split(/\./,$textwindow->index('insert')); |
| 137 |
$current_line_label->configure (-text=> "line: $line"); |
| 138 |
$current_column_label->configure (-text=> "column: $column"); |
| 139 |
|
| 140 |
my ($last_line,$last_col) = split(/\./,$textwindow->index('end')); |
| 141 |
$total_line_label->configure (-text=> "total lines: $last_line"); |
| 142 |
|
| 143 |
my $mode = $textwindow->OverstrikeMode; |
| 144 |
my $overstrke_insert='Insert Mode'; |
| 145 |
if ($mode) |
| 146 |
{$overstrke_insert='Overstrike Mode';} |
| 147 |
$insert_overstrike_mode_label->configure |
| 148 |
(-text=> "$overstrke_insert"); |
| 149 |
|
| 150 |
my $filename = $textwindow->FileName; |
| 151 |
$filename = 'NoName' unless(defined($filename)); |
| 152 |
my $edit_flag=''; |
| 153 |
if($textwindow->numberChanges) |
| 154 |
{$edit_flag='edited';} |
| 155 |
$top->configure(-title => "Gedi $edit_flag $filename"); |
| 156 |
$textwindow->idletasks; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
$textwindow->SetGUICallbacks ( |
| 161 |
[ |
| 162 |
\&update_indicators, |
| 163 |
sub{$textwindow->HighlightAllPairsBracketingCursor} |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
|
| 168 |
############################################## |
| 169 |
############################################## |
| 170 |
# call back functions |
| 171 |
############################################## |
| 172 |
############################################## |
| 173 |
|
| 174 |
######################################################################## |
| 175 |
my $about_pop_up_reference; |
| 176 |
sub about_pop_up |
| 177 |
{ |
| 178 |
my $name = ref($about_pop_up_reference); |
| 179 |
if (defined($about_pop_up_reference)) |
| 180 |
{ |
| 181 |
$about_pop_up_reference->raise; |
| 182 |
$about_pop_up_reference->focus; |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
my $pop = $top->Toplevel(); |
| 187 |
$pop->title("About"); |
| 188 |
|
| 189 |
$pop->Label(-text=>"Gedi (Gregs EDItor)")->pack(); |
| 190 |
$pop->Label(-text=>"Ver. 1.0")->pack(); |
| 191 |
$pop->Label(-text=>"Copyright 1999")->pack(); |
| 192 |
$pop->Label(-text=>"Greg London")->pack(); |
| 193 |
$pop->Label(-text=>"All Rights Reserved.")->pack(); |
| 194 |
$pop->Label(-text=>"This program is free software.")->pack(); |
| 195 |
$pop->Label(-text=>"You can redistribute it and/or")->pack(); |
| 196 |
$pop->Label(-text=>"modify it under the same terms")->pack(); |
| 197 |
$pop->Label(-text=>"as Perl itself.")->pack(); |
| 198 |
$pop->Label(-text=>"Special Thanks to")->pack(); |
| 199 |
$pop->Label(-text=>"Nick Ing-Simmons.")->pack(); |
| 200 |
|
| 201 |
my $button_ok = $pop->Button(-text=>'OK', |
| 202 |
-command => sub {$pop->destroy(); |
| 203 |
$about_pop_up_reference = undef; |
| 204 |
} ) |
| 205 |
->pack(); |
| 206 |
$pop->resizable('no','no'); |
| 207 |
$about_pop_up_reference = $pop; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
############################################## |
| 212 |
############################################## |
| 213 |
## now set up menu bar |
| 214 |
############################################## |
| 215 |
############################################## |
| 216 |
|
| 217 |
my $menu = $textwindow->menu; |
| 218 |
$top->configure(-menu => $menu); |
| 219 |
|
| 220 |
############################################## |
| 221 |
# help menu |
| 222 |
############################################## |
| 223 |
my $help_menu = $menu->cascade(-label=>'~Help', -tearoff => 0, -menuitems => [ |
| 224 |
[Command => 'A~bout', -command => \&about_pop_up] |
| 225 |
]); |
| 226 |
|
| 227 |
############################################## |
| 228 |
# debug menu |
| 229 |
############################################## |
| 230 |
|
| 231 |
if (0) |
| 232 |
{ |
| 233 |
my $debug_menu = $menu->cascade(-label=>'debug', -underline=>0); |
| 234 |
|
| 235 |
|
| 236 |
$debug_menu->command(-label => 'Tag names', -underline=> 0 , |
| 237 |
-command => |
| 238 |
sub{ |
| 239 |
my @tags = $textwindow->tagNames(); |
| 240 |
print " @tags\n"; |
| 241 |
|
| 242 |
foreach my $tag (@tags) |
| 243 |
{ |
| 244 |
my @ranges = $textwindow->tagRanges($tag); |
| 245 |
print "tag: $tag ranges: @ranges \n"; |
| 246 |
} |
| 247 |
|
| 248 |
print "\n\n\n"; |
| 249 |
my @marks = $textwindow->markNames; |
| 250 |
print " @marks \n"; |
| 251 |
foreach my $mark (@marks) |
| 252 |
{ |
| 253 |
my $mark_location = $textwindow->index($mark); |
| 254 |
print "$mark is at $mark_location\n"; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
print "\n\n\n"; |
| 259 |
my @dump = $textwindow->dump ( '-tag', '1.0', '465.0' ); |
| 260 |
print "@dump \n"; |
| 261 |
|
| 262 |
print "\n\n\n"; |
| 263 |
print "showing tops children:"; |
| 264 |
my @children = $top->children(); |
| 265 |
print "@children\n"; |
| 266 |
|
| 267 |
foreach my $child (@children) |
| 268 |
{ |
| 269 |
my $junk = ref($child); |
| 270 |
print "ref of $child is $junk \n"; |
| 271 |
} |
| 272 |
|
| 273 |
my $overstrike = $textwindow->OverstrikeMode; |
| 274 |
print "Overstrike is $overstrike \n"; |
| 275 |
|
| 276 |
$textwindow->dump_array($textwindow); |
| 277 |
}); |
| 278 |
} |
| 279 |
|
| 280 |
############################################## |
| 281 |
# set the window to a normal size and set the minimum size |
| 282 |
$top->minsize(30,1); |
| 283 |
$top->geometry("80x24"); |
| 284 |
|
| 285 |
############################################################################# |
| 286 |
############################################################################# |
| 287 |
############################################################################# |
| 288 |
############################################################################# |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
############################################## |
| 294 |
## this line for debug |
| 295 |
## $top->bind('<Key>', [sub{print "ARGS: @_\n";}, Ev('k'), Ev('K') ] ); |
| 296 |
|
| 297 |
########################################## |
| 298 |
## fill the text window with initial file. |
| 299 |
|
| 300 |
if ($argcount) |
| 301 |
{ |
| 302 |
if (-e $global_filename) # if it doesn't exist, make it empty |
| 303 |
{ |
| 304 |
# it may be a big file, draw the window, and then load it |
| 305 |
# so that we know something is happening. |
| 306 |
$top->update; |
| 307 |
$textwindow->Load($global_filename); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
############################################## |
| 313 |
$textwindow->CallNextGUICallback; |
| 314 |
|
| 315 |
MainLoop(); |