-
Notifications
You must be signed in to change notification settings - Fork 0
/
kpad.pl
executable file
·485 lines (394 loc) · 14.7 KB
/
kpad.pl
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env perl
# KAKE PAD (kPad) version 5.2 Bug Fix Release #2
# Please note that KAKE PAD depends on Tk and some bugs
# are due to Tk and not KAKE PAD itself. No promises on how
# good or bad activestate Tk is.
# A few Tk modules need to be declared for PDK purposes
use Tk;
use Tk::TextUndo;
use Tk::DialogBox;
use File::Glob;
use File::Find;
use FileHandle;
use LWP::Simple;
use CGI;
#most of the above is only put in so I can compile to stand alone exe
#If you use as script keep the last two and use Tk;
# Below is the init section
# A few thing to do is check OS to see if its Windows or not
# I still don't know what I smoked when I wrote this but this section is important
($ar) = @ARGV; #Let the program do its thing with the arguments
# Bah check OS
# Just to make sure test $0, this is a work around for a minor bug
# Detect : a trade mark of a DOS system
# The setion below is so odd and complex vodoo that is quite nessacry
# yes I can't spell, only code
if($^O ne "Win32") {
if($0 =~/\//g){
@hd = split "/", $0;
$hdl = pop(@hd); # Knock the filename off the array we don't need it
$basedir = join('/', @hd);
}else{
$basedir = ".";
}
}else{
$basedir = ".";
}
$main = MainWindow->new(-title=> "kPad"); #Generates the main window
# Notice I create the menu bar as a frame not a Tk::Menu menubar, this makes things easier
$menubar = $main->Frame()->pack(-side => "top", -fill => "x");
#Below I define all the DialogBoxes,note they can be globaly used
$about = $main->DialogBox(-title=>"About...",-buttons=>["OK"]); #Creates the about Dialog
$aabout = $about->add("Label",-text=>"kPad - Perl to the max!\n by Paul Malcher\nVersion 5.2.1 Bug Fix Release\n")->pack; #Adds a label to $about
$kweabt = $main->DialogBox(-title=>"About kWebedit...",-buttons=>["OK"]); #Creates the About kWebedit Dialog
$akweabt =$kweabt->add("Label",-text=>"kWebedit v.2.0\nby Paul Malcher\nBased on kWebedit v.1.0.3 by Chris Litwin ")->pack; #That's the about for kWebedit.
$help = $main->DialogBox(-title=>"Help Topics",-buttons=>["OK"]); #Creates Help Dialog
$ahelp = $help->add("Label",-text=>"Help topics for KPAD\nWell, this is a text/file editor mainly meant for scripting and programming use.
Like notepad but made for the programmer.")->pack;
$nsave = $main->DialogBox(-title=>"Warning File Has changed!",-buttons=>["Save","Exit"]);
$ansave = $nsave->add("Label",-text=>"The documents contents have changed since you opened.\nDo you wish to save?.")->pack;
$nimp = $main->DialogBox(-title=>"Non-implementation Error",-buttons=>["OK"]);
$animp = $nimp->add("Label",-text=>"This function is not yet implemented!")->pack;
$fdisc = $main->DialogBox(-title=>"Disclaimer...",-buttons=>["OK"]);
$afdisc = $fdisc->add("Label",-text=>"Please note that some websites have their contents protected by copyright law.\nUse source that doesn't belong to you responsibly. :)")->pack;
$fetch = $main->DialogBox(-title =>'HTML Source Fetch',-buttons=>["OK"]);
$afetch = $fetch->add("Label",-text=>'Fetch what:')->pack;
$bfetch = $fetch->add("Entry",-text=>'http://')->pack;
$dummy = $main->DialogBox(-title=>'Dummy Box');
$adummy = $dummy->add("Text")->pack;
$ftapp = $main->DialogBox(-title =>'File Has Changed!',-buttons=>["Yes","No"]);
$aftapp = $ftapp->add("Label", -text=>"File contents have changed, save now?!")->pack;
$track = "init";
# Begin new Kpad 4.0 features
# Plugin/Macros or whatever you want to call them
# First we find and autoload plugin, yes we use grep, makes life good
opendir(DIR, $basedir) or warn ("Cannot open current directory! Autoload Aborted!");
my @contents = readdir(DIR);
closedir(DIR);
# Links may be ignored completely:
# No hidden files and ".." directories:
@contents = grep {!/^\./} @contents;
# Get files:
my @files = grep {-f} @contents;
# no dirs or hidden files
my @plugins = grep {/\.kpd/} @contents;
my @plugins = grep {!/[\~]/} @plugins; # This line is just for kedit backup files
# Print wow that was easy and it worked, any file with a .kpd extension is assummed to be a plugin
# Now I got to load them into an array and then added each to a list box
$pls = 0;
# Heck with lets do it all in one loop
foreach(@plugins) {
open pin,"<$basedir/@plugins[$pls]";
@gn = split "::" , <pin>;
if(@gn[2] eq "auto"){
@n[$pls] = "auto";
}else{
@n[$pls] = @gn[1];
}
$pls++;
}
# determin the number of plugins, so we can size the list accordingly
$nop = scalar(@n); #notice @n does not get shortend, this is important later on
foreach(@n) {
if($_ eq "auto"){
$nop--; # make sure auto plugins are not listed
}
}
# Build the menu with list box
$plugin = $main->DialogBox(-title=>'Macro Execution Menu',-buttons=>["Close"]);
$bplugin = $plugin->add("Label",-text=>'Double Click To Execute Macro')->pack;
$aplugin = $plugin->Listbox("-width"=>40, "-height"=> $nop)->pack;
foreach(@n) {
if($_ eq "auto"){
$arun = 0;
}else{
$aplugin->insert('end', "$_");
}
}
$aplugin->bind('<Double-1>' , \&eplugin); # Plugin name now can be different from the file name
$filemenu = $menubar->Menubutton(-text => 'File', -underline => 0,-tearoff => 0)->pack(-side=>'left'); #This puts
#the file button on the frame used for the menu bar
#Below are the commands for that button
#note How I included the subs into the command function
$filemenu->command(-label => 'New',-command => sub{
$text->delete('1.0','end');
});
$filemenu->command(-label => 'Open',-command => sub{
$text->delete('1.0','end');
my $types = [
['Perl Scripts', '.pl'],
['All Files', '*', ],
];
$open = $main->getOpenFile(-filetypes=>$types);
#open FILE, "<$open"; #took weeks to get this right,its there so te whole file loads correctly
# and only 3 sec to comment out for the 5.0 release
$text->Load($open);
$text ->pack;
$track = $text->get('1.0','end');
});
$filemenu->command(-label => 'Save',-command => sub{
$data = $text->get('1.0','end'); #Saving for widget to file is a piece of cake
if($ar eq ""){
$text->Save($open);
# Easy indeed
}else{
$text->Save($ar);
$track = $text->get('1.0','end');
}
});
$filemenu->command(-label => 'Save As',-command => sub{
#my $types = [['All Files', '*', ],];
my $types = [
['Perl Scripts', '.pl' ],
['All Files', '.*', ],
];
my $save = $main->getSaveFile(-filetypes=>$types);
$text->Save($save);
$track = $text->get('1.0','end');
$open = $save;
});
$filemenu->separator;
$filemenu->command(-label => 'Exit',-command => sub{
tapp();
});
$editmenu = $menubar->Menubutton(-text => 'Edit', -underline => 0,-tearoff => 0)->pack(-side=>'left');
$editmenu->command(-label => 'Undo',-command => sub{
my ($w) = @_;
$text->undo;
});
$editmenu->command(-label => 'Redo',-command => sub{
my ($w) = @_;
$text->redo;
});
$editmenu->separator;
$editmenu->command(-label => 'Cut',-command => sub{
my ($w) = @_;
$text->Column_Copy_or_Cut(1);
});
$editmenu->command(-label => 'Copy',-command => sub{
my ($w) = @_;
$text->Column_Copy_or_Cut(0);
});
$editmenu->command(-label => 'Paste',-command => sub{
$text->clipboardColumnPaste();
});
$editmenu->separator;
$editmenu->command(-label => 'Select All',-command => sub{
$text->selectAll();
});
$editmenu->command(-label => 'Unselect All',-command => sub{
$text->unselectAll();
});
$editmenu->separator;
$editmenu->command(-label => 'Find',-command => sub{
$text->findandreplacepopup(1);
});
$editmenu->command(-label => 'Find and Replace',-command => sub{
$text->findandreplacepopup(0);
});
$viewmenu = $menubar->Menubutton(-text=>'View',-underline => 0,-tearoff => 0)->pack(-side=>'left');
$vm = $viewmenu->cascade(-label => 'Wrap',-underline => 0,-tearoff => 0);
$vm->radiobutton(-label => "Word", -command => sub { $text->configure(-wrap => 'word'); } );
$vm->radiobutton(-label => "Char",-command => sub { $text->configure(-wrap => 'char'); } );
$vm->radiobutton(-label => "None",-command => sub { $text->configure(-wrap => 'none'); } );
$toolsmenu = $menubar->Menubutton(-text => 'Tools', -underline => 0,-tearoff => 0)->pack(-side=>'left');
$toolsmenu->command(-label => 'Goto Line',-command => sub{
$text->GotoLineNumberPopUp();
});
$toolsmenu->command(-label => 'Which Line?',-command => sub{
$text->WhatLineNumberPopUp();
});
$htmlmenu = $menubar->Menubutton(-text => 'HTML', -underline => 0,-tearoff => 0)->pack(-side=>'left');
$htmlmenu->command(-label => 'Basic HTML',-command => sub{
$cgi = new CGI;
$text->delete('1.0','end');
$text->insert('end', $cgi->start_html("your title here"));
$text->insert('end', "\nYour content here\n\n");
$text->insert('end', $cgi->end_html);
#$text->insert('end', "<head> \n");
#$text->insert('end', "<title>Your Title Here</title> \n");
#$text->insert('end', "</head> \n");
#$text->insert('end', "<body> \n");
#$text->insert('end', "Your Content Here! \n");
#$text->insert('end', "</body> \n");
#$text->insert('end', "</html> \n");
});
$htmlmenu->command(-label => 'Basic CSS2 (IE 5.5+ only)',-command => sub{
#$text->delete('1.0','end');
$text->insert('insert', "<style type=text/css> \n");
$text->insert('insert', "<!-- \n");
$text->insert('insert', "body{ \n");
$text->insert('insert', "font-family: [Font(s), multiple speparated by commas] ; \n");
$text->insert('insert', "font-size: [size, add pt for points or px for pixels] ; \n");
$text->insert('insert', "background: [background, add # to the front for HEX]; \n");
$text->insert('insert', "color: [Same as above]; \n");
$text->insert('insert', "scrollbar-face-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-shadow-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-highlight-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-3dlight-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-darkshadow-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-track-color: [Same as above]; \n");
$text->insert('insert', "scrollbar-arrow-color: #466587; \n");
$text->insert('insert', "} \n");
$text->insert('insert', "a{ \n");
$text->insert('insert', "color: [Color, add # for hex]; \n");
$text->insert('insert', "text-decoration:[This can be none, underline, or overline]; \n");
$text->insert('insert', "} \n");
$text->insert('insert', "a:hover{ \n");
$text->insert('insert', "color: [Same as above]; \n");
$text->insert('insert', "text-decoration: [Same as above]; \n");
$text->insert('insert', "} \n");
$text->insert('insert', "--> \n");
$text->insert('insert', "</style> \n");
});
$htmlmenu->command(-label => 'Definition List',-command =>sub{
#$text->delete('1.0','end');
$text->insert('insert', "<dl> \n");
$text->insert('insert', "<dt>Definition Term here..add as many of this and the next line as needed.</dt> \n");
$text->insert('insert', "<dd>Defintion of Term here</dd> \n");
$text->insert('insert', "</dl> \n");
});
$htmlmenu->command(-label => 'Fetch source code...',-command => sub{$fdisc->Show;
$fetch->Show;
$htm = $bfetch->get;
$contents = get($htm);
open ttt, ">temp.dat";
print ttt "$contents";
close ttt;
open FILE, "<temp.dat"; #took weeks to get this right,its there so te whole file loads correctly
$text->delete('1.0','end');
while (! eof FILE){
$text->insert('end',FILE -> getline);
}
close FILE;
unlink(<temp.dat>);
$text ->pack;
$track = $text->get('1.0','end');
});
$pluginmenu = $menubar->Menubutton(-text => 'Macros', -underline => 0,-tearoff => 0)->pack(-side=>'left');
$pluginmenu->command(-label => 'Execute Macro',-command => sub{$plugin->Show;});
$aboutmenu = $menubar->Menubutton(-text => 'Help', -underline => 0,-tearoff => 0)->pack(-side=>'left');
$aboutmenu->command(-label => 'Help Topics...',-command => sub{$help->Show;});
$aboutmenu->command(-label => 'About KPAD...',-command => sub{$about->Show;});
$aboutmenu->command(-label => 'About kWebedit...',-command => sub{$kweabt->Show;});
# Text widget and configs
$text = $main->Scrolled(TextUndo,-scrollbars=>'osoe',-background=>'white', -wrap => 'word')->pack(-fill=>'both',-expand=>1); #Scrolled Text
#widget that adapts to the size of the window
$main->protocol('WM_DELETE_WINDOW', \&tapp);
if($track eq "init"){
$track = $text->get('1.0','end');
}
#->OnDestroy(\&tapp);
#$statbar = $main->Frame()->pack(-side => "top", -fill => "x"); #Notice I create the menu bar as
#$filem = $statbar->Menubutton(-text => 'Test', -underline => 0,-tearoff => 0)->pack(-side=>'left'); #This puts
#$filem->command(-label => 'New',-command => sub{
#$text->configure(-scrollbars=>'se');
#});
if($ar ne ""){
#open FILE, "<$ar"; #took weeks to get this right,its there so te whole file loads correctly
#while (! eof FILE){
#$text->insert('end',FILE -> getline);
$text->Load($ar);
$track = $text->get('1.0','end');
#}
#close FILE;
$text ->pack;
$track = $text->get('1.0','end');
}
sub eplugin { # Plugin executor, non-auto
$v = $aplugin->get('active');
# Fix for plugin vs. filename fix
# @plugins @n
$fp = 0;
while(@n[$fp] ne $v){ # assume the names in @plugin match with @n
# which they will unless you screw with the way plugins are handled
$fp++;
}
$v = @plugins[$fp];
# Hope it works
open pe, "<$basedir/$v"; # presto it does
# The same fucking bug in Tk again, yes the one that took weeks to work around
# I got to do this the hard way
$adummy->delete('1.0','end');
while (! eof pe){
$adummy->insert('end', pe -> getline);
}
$tdata = $adummy->get('2.0','end'); # this is the only way to load an entire plugin into a var the right way, fuck
eval ( $tdata );
if($@){ # Only way to to make so it can trap multiple errors without the app having a fatal error itself
$error = $@;
&merr($error);
}
}
sub aeplugin { # Auto plugin executor
$apc = 0;
while(@n[$apc] ne ""){
if(@n[$apc] eq "auto"){
$v = @plugins[$apc];
# Hope it works
open pe, "<$basedir/$v"; # presto it does
# The same fucking bug in Tk again, yes the one that took weeks to work around
# I got to do this the hard way
$adummy->delete('1.0','end');
while (! eof pe){
$adummy->insert('end', pe -> getline);
}
$tdata = $adummy->get('2.0','end'); # this is the only way to load an entire plugin into a var the right way, fuck
eval ( $tdata );
if($@){ # Only way to to make so it can trap multiple errors without the app having a fatal error itself
$error = $@;
&merr($error);
}
}
$apc++;
}
$arun = 1;
}
sub merr { # merr, macro/plugins error
$merr = $main->DialogBox(-title =>'Macro Error',-buttons=>["OK"]);
$amerr = $merr->add("Label", -text=>"Error: $error")->pack;
$merr->Show;
undef $merr;
}
if($arun eq "0"){
&aeplugin();
}
sub tapp { # shutdown handler
# $ar
# $open
$curd = $text->get('1.0','end');
#chomp($curd);
#chomp($track);
#if(!$curd){
#exit(0);
#}
if($curd ne $track){
$result = $ftapp->Show;
if($result eq "No"){
exit(0);
}
if($open){
$text->Save($open);
$saved = 1;
}
if($save){
$text->Save($save);
$saved = 1;
}
if($ar){
$text->Save($ar);
$saved = 1;
}else{
if($saved ne "1"){
my $types = [
['Perl Scripts', '.pl' ],
['All Files', '.*', ],
];
my $save = $main->getSaveFile(-filetypes=>$types);
$text->Save($save);
}
}
}
exit(0);
}
MainLoop; #The main processing loop