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