-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs.html
More file actions
1218 lines (1087 loc) · 37.2 KB
/
cs.html
File metadata and controls
1218 lines (1087 loc) · 37.2 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>CodeScriber 2.5</title> <!-- CodeScriber 2.5 -->
<link rel="stylesheet" href="cs.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.37.1/ace.min.js"
integrity="sha512-39pMYj4We1YvsGIsTFctdfIdkqFG09F5lVUM4NzwUgE20xuBHdPWI2Ra4AShMX4qMdwTN2vfb9ATOdkoMU512Q=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.37.1/ext-language_tools.min.js"
integrity="sha512-WPFoecnG6+TbAah6uWLOr+/36IDeXpa9klWh+SFWRzQeVC6x/n7rzODbzctFYL7rqxaDK2pbKj3psH7sws70Ng=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="tags.js"></script> <!-- run names, enclosures, Zen tags -->
<script src="myJS.js"></script> <!-- A JQuery-ish library -->
<script src="filemodes.js"></script> <!-- Ace syntax theme library -->
</head>
<!--
Please practice care when modifing any code in this file.
Always save a backup copy before making any changes.
-->
<body>
<!-- <img src="images/KO.gif" id="LOAD" style="display:none;position:absolute;top:5;left:5;" /> -->
<div class="container"> <!-- Menus -->
<div class="buttons">
<ul>
<li>
<a href="#">File</a>
<ul class="dropdown-content">
<li><a onclick="openRecent();" title="Ctrl-R">Recent</a></li>
<li><a onclick="openFile();" title="Ctrl-O">Open</a></li>
<li><a onclick="newDocument();" title="Ctrl-N">New</a></li>
<li><a onclick="save();" title="Ctrl-Shft-S">Save-As</a></li>
<li><a onclick="quicksaveDocument();" title="Ctrl-S">Save</a></li>
<li><a onclick="exitApp();" title="Ctrl-Q">E X I T</a></li>
</ul>
</li>
<li>
<a href="#">Tools</a>
<ul class="dropdown-content">
<li><a onclick="execTerm();" title="open terminal">Terminal</a></li>
<li><a onclick="execFMgr();" title="open file manager">Files</a></li>
<li><a onclick="execBrowser();" title="open in browser">Browser</a></li>
<li><a onclick="mdCurrent();" title="View HTML">Markdown</a></li>
<li><a onclick="viewZenTags();" title="Alt-X">Insert Zen</a></li>
<li><a onclick="findfile();" title="search filesystem">Find file</a></li>
<li><a onclick="findfunc();" title="Ctrl-Alt-F">Functions</a></li>
<li><a onclick="getcolor();" title="color picker">Pic color</a></li>
<li><a onclick="deletebk();" title="Remove backups in current dir">DEL bkups</a></li>
<li><em><a onclick="execI();" id="RU1" class="rux" title="Ctrl-Alt-1">Run 1</a></em></li>
<li><em><a onclick="execII();" id="RU2" class="rux" title="Ctrl-Alt-2">Run 2</a></em></li>
<li><em><a onclick="execIII();" id="RU3" class="rux" title="Ctrl-Alt-3">Run 3</a></em></li>
<li><em><a onclick="execIV();" id="RU4" class="rux" title="Ctrl-Alt-4">Run 4</a></em></li>
</ul>
</li>
<li>
<a href="#">Options</a>
<ul class="dropdown-content">
<li><a onclick="openOptionFile('options.ini');"
title="Edit the options.ini file">Options</a></li>
<li><a onclick="relaunch();"
title="re-launch CodeScriber">Re-Launch</a></li>
<li><a onclick="openOptionFile('snip.txt');"
title="New zen code snipit">New Zen</a></li>
<li><a onclick="openOptionFile('tags.js');"
title="Zen snipits and other tags">Tags</a></li>
<li><a onclick="openOptionFile('filemodes.js');"
title="Edit File Modes">File Modes</a></li>
<li><a onclick="loadSelected('ailog.md');"
title="Ctrl-L Open AI log">AI log</a></li>
<li><a onclick="toggleWrap();"
title="Ctrl-Alt-W Toggle Wrap mode">Wrapping</a></li>
</ul>
</li>
<li>
<a href="#">Help</a>
<ul class="dropdown-content">
<li><a onclick="showMessageBox(msg);">About</a></li>
<li><a href="CSdoc.html"
target="_blank">Documentation</a></li>
<li><a onclick="shortcuts();"
title="Ctrl-Alt-H">KB Shortcuts</a></li>
<li><a onclick="showMessageBox(ai_msg);"
title="How To">Use AI</a></li>
</ul>
</li>
</ul>
</div>
<div class="input-container">
<input type=text id="FN">
</div>
</div>
<!-- Context Menu -->
<div id="customContextMenu" class="context-menu">
<div class="context-menu-item">Copy</div>
<div class="context-menu-item">Save</div>
<div class="context-menu-item">Open</div>
<div class="context-menu-item">Recent</div>
<em><div class="context-menu-item">Terminal</div></em>
<em><div class="context-menu-item">Files</div></em>
<!--<em><div class="context-menu-item">Browser</div></em>-->
<!--<em><div class="context-menu-item">Markdown</div></em>-->
<!--<em><div class="context-menu-item">Functions</div></em>-->
</div>
<!-- Special Messagebox -->
<div id="overlay1" class="overlay"> <!-- Hidden -->
<div class="messageBox">
<p id="messageBoxText"></p>
<button id="msg_close_1" class="closeBtn" onclick="close_msgbox();">Close</button>
</div>
</div>
<!-- 'folder' bar -->
<div id="FLDR" class="folder-container" title="right-click to close folder"></div>
<!-- Ace Editor -->
<div id="editor"></div>
<!-- ==================================================================== -->
<script>
// get theme from URL querystring
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const thm = urlParams.get('theme'); // options.ini setting
const aicon = urlParams.get('aiconfirm'); // options.ini setting
const theme = "ace/theme/" + thm;
var contextMenu = JS.doq('#customContextMenu');
var filename = "";
var gvar = {
oFN : null,
saved : true,
wrap: false,
onMsgWait: false,
last_pre_tag : "",
last_pst_tag : "",
sep: "/",
bmk_inx: 0,
abmk: [],
rows: 50, // rows bookmarks per session (not saved on exit)
cols: 0,
cinx: 0, // current session inx
sinx: 0, // last session inx
sesn: [],
sefn: []
};
// bookmarks array
gvar.abmk = Array(gvar.rows).fill(0).map(() => Array(gvar.cols).fill(0));
gvar.oFN = JS.doq("#FN"); // object for the filename input field
var editor = ace.edit("editor"); // the editor instance
/*
* ___ _____ ____
* / _ |_______ / ___/__ ___ / _(_)__ ___ _________
* / __ / __/ -_) / /__/ _ \/ _ \/ _/ / _ `/ // / __/ -_)
* /_/ |_\__/\__/ \___/\___/_//_/_//_/\_, /\_,_/_/ \__/
* /___/
*/
//
editor.setTheme(theme); // theme is set in options.ini
// SEE CSS #editor FOR FONT STYLING
editor.$blockScrolling = Infinity; // ace debug suggestion
editor.setShowPrintMargin(false);
editor.session.setTabSize(4);
editor.session.setUseSoftTabs(true); // use spaces instead of tab chars
editor.session.setUseWrapMode(gvar.wrap);
editor.setOption("highlightActiveLine", false); // or false
editor.setOptions({
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
enableSnippets: false
});
function shortcuts() {
ace.config.loadModule("ace/ext/keybinding_menu", function(module) {
module.init(editor);
editor.showKeyboardShortcuts();
});
}
///////// ADD (ACE) CUSTOM HOT KEYS //////////
editor.commands.addCommand({
name: 'Word Wrap',
bindKey: {win: 'Ctrl-Alt-W', mac: 'Command-Alt-W'},
exec: function(editor) {
toggleWrap();
},
readOnly: false // Do NOT apply this command in readOnly mode
});
editor.commands.addCommand({
name: 'Open AI Log File',
bindKey: {win: 'Ctrl-L', mac: 'Command-L'},
exec: function(editor) {
loadSelected('ailog.md');
},
readOnly: false // Do NOT apply this command in readOnly mode
});
editor.commands.addCommand({
name: 'HTML Break',
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: function(editor) {
editor.insert("<br>");
},
readOnly: false // Do NOT apply this command in readOnly mode
});
editor.commands.addCommand({
name: 'HTML nbsp',
bindKey: {win: 'Ctrl-Space', mac: 'Command-Space'},
exec: function(editor) {
editor.insert(" ");
},
readOnly: false
});
editor.commands.addCommand({
name: 'Insert Date',
bindKey: {win: 'Alt-t', mac: 'Alt-t'},
exec: function(editor) {
editor.insert(JS.getMDY());
},
readOnly: false
});
// add command to lazy-load keybinding_menu extension
editor.commands.addCommand({
name: "show all hotkeys",
bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
exec: function(editor) {
shortcuts();
}
});
editor.commands.addCommand({
name: "Show selected spelling",
bindKey: {win: "Ctrl-Alt-c", mac: "Command-Alt-c"},
exec: function(editor) {
getWords();
}
});
editor.commands.addCommand({
name: "EXEC external 1",
bindKey: {win: "Ctrl-Alt-1", mac: "Command-Alt-1"},
exec: function(editor) {
execI();
}
});
editor.commands.addCommand({
name: "EXEC external 2",
bindKey: {win: "Ctrl-Alt-2", mac: "Command-Alt-2"},
exec: function(editor) {
execII();
}
});
editor.commands.addCommand({
name: "EXEC external 3",
bindKey: {win: "Ctrl-Alt-3", mac: "Command-Alt-3"},
exec: function(editor) {
execIII();
}
});
editor.commands.addCommand({
name: "EXEC external 4",
bindKey: {win: "Ctrl-Alt-4", mac: "Command-Alt-4"},
exec: function(editor) {
execIV();
}
});
// ZenTags
editor.commands.addCommand({
name: 'Zen Snipit Insert',
bindKey: {win: 'Alt-Z', mac: 'Alt-Z'},
exec: function(editor) {
zentag();
},
readOnly: true // false if this command should not apply in readOnly mode
});
editor.commands.addCommand({
name: 'Repeat last enclosure',
bindKey: {win: 'Alt-w', mac: 'Alt-w'},
exec: function(editor) {
tagSurround(gvar.last_pre_tag, gvar.last_pst_tag);
},
readOnly: false
});
editor.commands.addCommand({
name: 'Alter Enclosure',
bindKey: {win: 'Alt-e', mac: 'Alt-e'},
exec: function(editor) {
setup_tag();
},
readOnly: false
});
/*
bookmarking:
F3 => goto next bookmark
Shift-F3 ==> clear bookmarks
Ctrl-GutterMouseDown ==> set bookmark
*/
editor.on("guttermousedown", function(e){
if (e.domEvent.ctrlKey) {
console.log("gvar.cinx " + gvar.cinx)
gvar.abmk[gvar.cinx].unshift(e.getDocumentPosition().row);
}
});
editor.commands.addCommand({
name: 'Goto Bookmark',
bindKey: {win: 'F3', mac: 'F3'},
exec: function(editor) {
if (gvar.abmk[gvar.cinx].length == 0) return;
gvar.bmk_inx++;
if (gvar.bmk_inx > gvar.rows) {
alert("too many bookmarks/folders");
return;
}
if (gvar.bmk_inx >= gvar.abmk[gvar.cinx].length) {
gvar.bmk_inx = 0
}
editor.gotoLine(gvar.abmk[gvar.cinx][gvar.bmk_inx] + 1, 0, true);
},
readOnly: false
});
editor.commands.addCommand({
name: 'Clear Bookmarks',
bindKey: {win: 'Shift-F3', mac: 'Shift-F3'},
exec: function(editor) {
if (confirm("Clear Bookmarks")) {
gvar.abmk[gvar.cinx] = [];
gvar.bmk_inx = 0;
}
readOnly: false
}
});
editor.commands.addCommand({
name: "Refresh File",
bindKey: {win: "Ctrl-I", mac: "Command-I"},
exec: function(editor) {
refreshFile();
}
});
// Non editor events
editor.commands.addCommand({
name: "Exit App",
bindKey: {win: "Ctrl-Q", mac: "Command-Q"},
exec: function(editor) {
exitApp();
}
});
editor.commands.addCommand({
name: "Open Recent Files",
bindKey: {win: "Ctrl-R", mac: "Command-R"},
exec: function(editor) {
openRecent();
}
});
editor.commands.addCommand({
name: "Open File",
bindKey: {win: "Ctrl-O", mac: "Command-O"},
exec: function(editor) {
openFile();
}
});
editor.commands.addCommand({
name: "New Document",
bindKey: {win: "Ctrl-N", mac: "Command-N"},
exec: function(editor) {
newDocument();
}
});
editor.commands.addCommand({
name: "Function List",
bindKey: {win: "Ctrl-Alt-F", mac: "Command-Alt-F"},
exec: function(editor) {
findfunc();
}
});
editor.commands.addCommand({
name: "Send AI prompt",
bindKey: {win: "Ctrl-G", mac: "Command-G"},
exec: function(editor) {
gogpt();
}
});
editor.commands.addCommand({
name: "Save File (quick)",
bindKey: {win: "Ctrl-S", mac: "Command-S"},
exec: function(editor) {
quicksaveDocument();
}
});
editor.commands.addCommand({
name: "Save-As File",
bindKey: {win: "Ctrl-Shift-S", mac: "Command-Shift-S"},
exec: function(editor) {
save();
}
});
editor.commands.addCommand({
name: "Open Seleted URL",
bindKey: {win: "Alt-U", mac: "Alt-U"},
exec: function(editor) {
getSelectedURL();
}
});
editor.commands.addCommand({
name: "List Zen Snipits",
bindKey: {win: "Alt-X", mac: "Alt-X"},
exec: function(editor) {
viewZenTags();
}
});
editor.commands.addCommand({
name: "View Enclosures",
bindKey: {win: "Ctrl-Alt-X", mac: "Command-Alt-X"},
exec: function(editor) {
viewStags();
}
});
////////////////////////////////////////////////////////////////////////////////
// the following toggles the 'save' button if the text is "dirty" or "saved"
editor.on("input", function() {
// gvar.saved = editor.session.getUndoManager().isClean();
gvar.saved = gvar.sesn[gvar.cinx].getUndoManager().isClean();
});
/* Context-Menu Event Handlers */
editor.container.addEventListener('contextmenu', function(event) {
event.preventDefault();
contextMenu.style.top = event.clientY - 170 + 'px';
contextMenu.style.left = event.clientX + 'px';
contextMenu.style.display = 'block';
});
document.addEventListener('click', function() {
contextMenu.style.display = 'none';
});
contextMenu.addEventListener('click', function(event) {
var target = event.target;
if (target.classList.contains('context-menu-item')) {
contextMenu.style.display = 'none';
mItem = target.innerText;
switch (mItem) {
case "Copy":
copySelectedText();
break;
case "Open":
openFile();
break;
case "Terminal":
execTerm();
break;
case "Files":
execFMgr();
break;
// case "Browser":
// execBrowser();
// break;
// case "Markdown":
// mdCurrent();
// break;
case "Recent":
openRecent();
break;
// case "Save-As":
// Save();
// break;
case "Save":
quicksaveDocument();
break;
}
}
});
/***
Enhance the Alt-z code insertion
***/
function insert_code(inx, bRemove = true) {
// inx should contain the key to look for
if (bRemove) {
editor.removeWordLeft();
}
if (atags[inx] !== undefined) {
editor.insert(atags[inx]); // targ is a valid key value
} else { // stag not a valid key value so do this instead
editor.insert("<" + inx + "></" + inx + ">");
}
}
function zentag() {
let wsp = [9,10,32, NaN]; // tab, LF, space
let inx = 0;
let code = 0;
let txtbuf = "";
let targ = "";
inx = editor.session.doc.positionToIndex(editor.selection.getCursor());
txtbuf = editor.getValue();
code = txtbuf.charCodeAt(inx);
if (!wsp.includes(code)) return; // must be at right whitespace boundary
inx -= 1;
if (inx < 0) return;
code = txtbuf.charCodeAt(inx);
while (!wsp.includes(code)) {
//console.log(inx);
targ = String.fromCharCode(code) + targ;
inx -= 1;
if (inx >= 0)
code = txtbuf.charCodeAt(inx);
else
break;
}
insert_code(targ);
}
/***
* _ _
* _ __ _ ___ __ _____| |____ _(_)_____ __ __
* | '_ \ || \ V V / -_) '_ \ V / / -_) V V /
* | .__/\_, |\_/\_/\___|_.__/\_/|_\___|\_/\_/
* |_| |__/
Javascript pywebview API functions connect to Python */
function getFN() {
window.pywebview.api.getFileName().then(content => {
setFileName(content);
if (content.endsWith("ailog.md")) {
const lastLine = editor.session.getLength(); // total lines (1-based for gotoLine)
editor.gotoLine(lastLine, 1, true);
}
});
}
function openFile() { // executes python/tkinter code
// changed to load multiple docs at once
// uses openCmdFile to load docs
window.pywebview.api.open_file();
}
function refreshFile() { // executes python/tkinter code
// uses openCmdFile to load docs
openCmdFile();
}
function save_File(content) { // Save-As
window.pywebview.api.save_file(content).then(content => {
if (content == "") return;
openCmdFile();
});
}
function quicksaveFile(content) { // executes python/tkinter code
window.pywebview.api.quick_save_file(content).then(content => {
});
}
function quick_save_file_check(content) {
window.pywebview.api.check_new_to_overwrite(content).then(content => {
finish_new_file(content);
});
}
function on_close() {
window.pywebview.api.onClose(); // immediate close no ask
}
function update_current_file(content) {
window.pywebview.api.set_current_file(content);
}
function open_url(content) {
// python to open selected URL
window.pywebview.api.open_URL(content);
}
function relaunch() {
/* close and re-open CodeScriber */
let r = confirm("wish to relaunch CodeScriber?");
if (r == "") return;
window.pywebview.api.reLaunch();
}
function openCmdFile() {
window.pywebview.api.getCmdFile().then(content => {
loadDocument(content);
});
}
function mdCurrent() {
window.pywebview.api.execMarkdown(); // open md in browser
}
function execFMgr() {
window.pywebview.api.execFileMgr(); // open file manager
}
function execTerm() {
window.pywebview.api.execTerminal(); // open terminal
}
function execBrowser() {
window.pywebview.api.execWebbrowser(); // open current in browser
}
function deletebk() {
window.pywebview.api.delete_backups();
}
function execI() {
window.pywebview.api.exec1(); // open
}
function execII() {
window.pywebview.api.exec2(); // open
}
function execIII() {
window.pywebview.api.exec3(); // open
}
function execIV() {
window.pywebview.api.exec4(); // open
}
function setup_snipit(content) { // add new snipit to tags.js
window.pywebview.api.addsnipit(content).then(content => {
});
}
function openOptionFile(loadfilename) { // option file to open is content
window.pywebview.api.openSelected(loadfilename).then(content => {
showMessageBox("<br><br>If this file is modified,<br>Restart CodeScriber to activate.")
loadDocument(content);
});
}
function DropOpenFile(filename) {
/* gets the string of possible fullpaths from python */
window.pywebview.api.on_file_drop(filename).then(content => {
// content is the csv string of possible paths/files
//JS.tod("#LOAD", "none");
selectFile(content);
});
}
function doSpellCheck(content) {
window.pywebview.api.open_spellcheck(content).then(content => {
showSpelling(content);
});
}
function openRecent() {
/* gets the Recent Files csv string from python */
window.pywebview.api.returnRecents().then(content => {
selectFile(content);
});
}
function loadSelected(content) {
/* python reads the selected file ( from selectFile() )
and returns its content to be loaded into Ace editor */
window.pywebview.api.openSelected(content).then(content => {
JS.doq('#overlay1').style.display = 'none';
loadDocument(content);
});
}
function getRunMenuNames() {
/* at startup get run menu name tags from options.ini
The returned content will be a CSV string. */
window.pywebview.api.getRunNames().then(content => {
setRunMenuNames(content);
});
}
function gogpt() {
/* Ctrl-g => get an openAI response to this prompt
and load it into a 'new' tab */
if (aicon.toLowerCase() == "yes") { // based on options.ini
const r = confirm("Submit prompt to AI?");
if (!r) return; // user hit 'Cancel' ?
}
let selection = editor.getSelectedText();
let selectedText = selection.toString();
if (selectedText == "") {
selectedText = editor.getValue();
} else {
newDocument(); // need new document with a selection
}
editor.setValue("Processing . . .\n")
window.pywebview.api.gptAccess(selectedText).then(content => {
content = selectedText + "\n------------\n" + content;
//loadDocument(content);
editor.setValue(content);
});
}
/* -------------------- End Javascript pywebview API functions ------------------------- */
/* handler functions working with pywebview functions */
function selectFile(content) {
/* presents an array of fullpaths for user selection
used by openRecent and DropOpenFile */
let x = 0;
let sfile = "";
let msg = "";
let files = content.split(",");
for (x=0; x < files.length; x++) {
sfile = convertPath(files[x]);
msg += `<a onclick="loadSelected('${sfile}')">${sfile}</a><br>`;
}
showMessageBox(msg);
}
function convertPath(filePath) { // for Windows
// replace backslashes with slashes (see selectFile())
return filePath.replace(/\\/g, '/');
}
function showSpelling(content) {
/* executed on return from python
which generated spelling correctioins from
the selected text */
if (content.trim() == "<br>") {
content = "All Good! :)"
}
showMessageBox(content);
}
function findfile() {
// begin the find local file process
filename = prompt("enter the filename to find:")
DropOpenFile(filename)
}
function golinenumber(lnum) {
// comes here from findfunc and showMessageBox
editor.gotoLine(lnum + 1);
JS.doq('#overlay1').style.display = 'none';
editor.focus();
}
function isFunctionDeclaration(line) {
// language patterns to determine function declarations
const patterns = {
javascript: /^\s*(function\s+\w+\s*\(.*\)\s*{)/, // e.g., function foo() { ... }
python: /^\s*def\s+\w+\s*\(.*\)\s*:/, // e.g., def foo(): ...
c: /^\s*\w+(\s+\*+)?\s+\w+\s*\(.*\)\s*{/, // e.g., int foo() { ... }
java: /^\s*(public|protected|private|static|\s)*\s*\w+(\s+\*+)?\s+\w+\s*\(.*\)\s*{/, // e.g., public void foo() { ... }
ruby: /^\s*def\s+\w+\s*(\(.*\))?\s*$/, // e.g., def foo() ... or def foo(args) ...
go: /^\s*func\s+\w+\s*\(.*\)\s*{/, // e.g., func foo() { ... }
};
// Check against each pattern
for (const lang in patterns) {
const regex = patterns[lang];
if (regex.test(line)) {
return true;
}
}
// If no pattern matches, it's not a function declaration
return false;
}
function findfunc() {
/* make a "list" of "function" lines from the editor
and display them, not sorted, for selection by the user */
var msg = "";
var line = "";
var funcName = "";
var linenbr = 0;
var p = 0;
const lines = editor.getValue().split("\n");
for (let line of lines) {
line = line.trim();
if (isFunctionDeclaration(line) === true) {
p = line.indexOf("(")
if (p === -1) {
funcName = line;
} else {
funcName = line.substring(0, p);
}
msg += `<a onclick="golinenumber(${linenbr});">${funcName}</a><br>\n`;
}
linenbr++;
}
showMessageBox(msg); // display the function links
}
function updateColor() {
// Get the selected color from the color input
var color = document.getElementById("colorInput").value;
navigator.clipboard.writeText(color);
JS.htm("#COLOR", color + " saved to clipboard");
}
function getcolor() {
// displays an HTML5 color input in the MesssageBox and waits for Close button
var msg = "<h2>Color Picker</h2>";
msg += "<input type='color' id='colorInput' value='#000000' onchange='updateColor()'>";
msg += "<br><br><a id='COLOR'></a><br>";
gvar.onMsgWait = true;
showMessageBox(msg);
}
function finish_new_file(newfn) {
// callback from quick_save_file_check
if (newfn == "yes") {
alert("WARNING!\n\nThis will overwrite an existing file!\n\nOperation Canceled.");
} else {
gvar.sinx++;
gvar.sesn[gvar.sinx] = ace.createEditSession("");
editor.setSession(gvar.sesn[gvar.sinx]);
setFileName(newfn);
update_current_file(newfn);
}
}
function newDocument() {
// load a blank unknown (new) document
var newfn = "";
newfn = prompt("Enter new file name for current path,\nor, enter Fullpath.");
if (newfn == null) return; // cancelled
if (newfn == "") newfn = "untitled"; // user hit OK with no new filename
quick_save_file_check(newfn); // check for existing file ...
}
function loadDocument(text) {
// all file opening starts here
if (text == "") {
newDocument();
return;
}
gvar.sinx++; // only place where sinx is changed!
gvar.sesn[gvar.sinx] = ace.createEditSession(text.split("\n"));
editor.setSession(gvar.sesn[gvar.sinx]);
getFN();
}
function setSaved() {
// resets the boolean variable for state of editor (clean/dirty)
gvar.saved = gvar.sesn[gvar.cinx].setUndoManager(new ace.UndoManager());
}
function setFileName(fn) {
/* works with loadDocument() & newDocument */
if (fn !== "") {
gvar.sefn[gvar.sinx] = fn; // file name for tab switching
setFileMode(fn); // set theme and file mode
editor.clearSelection();
JS.val("#FN", fn);
addFolder(fn, gvar.sinx); // create tab and store file info
editor.focus();
setSaved();
}
}
function save() {
// Save-As
let text = editor.getValue();
save_File(text);
}
function quicksaveDocument() {
if (JS.val("#FN") == "") {
save(); // Save-As for new file
return;
}
let text = editor.getValue();
if (text.startsWith("# Enter a trigger word and")) {
setup_snipit(text);
setSaved();
return;
}
quicksaveFile(text); // quicksaveFile save the file with current name
setSaved();
}
// Function to copy selected text to clipboard
function copySelectedText() {
var selectedText = editor.getSelectedText();
if (selectedText) {
navigator.clipboard.writeText(selectedText);
//alert("selected: " + selectedText.length + " characters.");
} else {
alert('No text selected');
}
}
function tagSurround(prefix, suffix) {
// performs the text encloser of current selection
// Get the current selection
let selectionRange = editor.getSelectionRange();
let selectedText = editor.getSession().getTextRange(selectionRange);
// Define the text to surround the selection
let newText = prefix + selectedText + suffix;
// Replace the selected text with the new text
editor.getSession().replace(selectionRange, newText);
}
function setup_tag() {
// Alt-e comes here for temporary enclosure setup
let inx = 0;
let tags = prompt("Enter Pre, Post, inx\nexample:\n <a href=''>, </a>, 2");
if (tags === null) return;
const t = tags.split(",");
let pre_tag = t[0];
let pst_tag = t[1];
inx = t[2];
if (pre_tag === null) return;
if (inx < 0 || inx > 9) return;
stag[inx] = pre_tag + "," + pst_tag;
showMessageBox("Ctrl-" + inx + " temporarily changed");
}
function copytext() {
let selectedText = editor.getSelectedText();
if (selectedText) {
send_to_clipboard(selectedText);
}
}
function convertHTML(str) {
// convert symbols for viewStags
const symbols = {
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'"
};
return str.replace(/([&<>\"'])/g, match => symbols[match]);
}
function viewStags() {
// view text enclosures
let x = 0;
let msg = "<strong>Text Enclosing</strong>:<br><br>";
for (x=0; x < 10; x++) {
msg += "Ctrl-" + x + " - " + convertHTML(stag[x]) + "<br>";
}
msg += "<br>Repeat with Alt-W<br>Edit in Options --> Tags";
showMessageBox(msg);
}
function viewZenTags() {
// view all Zen tag names
let str_atags = "ZEN TAGS:<br><br>";
Object.keys(atags).forEach(function(key) {
// str_atags += key + " | ";
str_atags += `<a onclick='insert_code("${key}", false)'>${key}</a>`;
});
showMessageBox(str_atags);
}
function showMessageBox(mtext) {
// Function to show the message box
JS.htm('#messageBoxText', mtext);
JS.doq('#overlay1').style.display = 'block';
}
function close_msgbox() {
// messageBox Close button clicked
JS.doq('#overlay1').style.display = 'none';
gvar.onMsgWait = false;
editor.focus()
}