-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRVClean_behaviour.livecodescript
6576 lines (6040 loc) · 235 KB
/
RVClean_behaviour.livecodescript
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
script "RVClean_behaviour"
global \
gTextStart, \
gTextEnd, \
now_selected, \
to_highlight, \
highlight_group, \
\
--COmments on 13.11.19
search_category, \
separator, \
citation_style, \
the_journal, \
the_view, \
view_style, \
the_file, the_directory, file_root, file_extension, \
gUndoHistory, \
edited, \
undo_count, \
gConnectionID, gTableName, the_choice, gWholeTable, gColumns, \
gTableName, \
gPatternTableName, \
gBugTableName, \
\
gColumns, \
filter_name, \
search_name, \
text_before, \
text_before_option, \
search_string, \
gFull_search_string, \
text_after, \
text_after_option, \
replace_string, \
search_zone, \
search_category, \
ignore_case, \
whole_word, \
Multiline, \
filter_ID, \
gHiliteFrom, \
gHiliteTo, \
gPreviousHiliteTo, \
gPreviousHiliteFrom, \
gCompactScroll, \
gStart, \
gEnd, \
gSortedFilterList, \
gColorLength, \
gEndLength, \
gColorTagRegex, \
gEndTagRegex, \
gSearchGroup, \
gSearchField, \
gReplaceField, \
gSearchMode, \
gSelectionChars, \
gPatternsA, \
sPatternColumns, \
gBugVariables, \
gDoNotAbbreviate, \
gActiveField
global \
citation_text_color, \
heading_text_color, \
citation_text_style, \
heading_text_style, \
original_bg_color, \
replace_bg_color, \
gGoogleDataSheet
local sNumberFound, \
sStarTag, \
sWhiteStar, \
s_Start, \
s_End, \
sBeginTagRegex, \
sEndTagRegex, \
sText, \
sCleanUpPatterns, \
sOriginalTextColor, \
sSelectionStart, \
sSelectionEnd, \
sCleanUpItem, \
sReplacementTextColor, \
sCleanUpColors, \
sNowProcessing, \
sPatterns, \
sScopes, \
sScopeTagA, \
sPatternTagA, \
sCurrentScope, \
sCurrentScopeTag, \
sScopeBeginTag, \
sScopeEndTag, \
sPublishers, \
sDiacritics, \
sJournals, \
sPrefixes, \
sFillerWords, \
sCountries, \
sPatternsA, \
sPatternOrder, \
sTagsInPrelims, \
sHeadersA, \
sSurnames
global sPatternColumns
local sRefs, \
sOriginalRefs, \
sCleanedRefs, \
sRefsNoDiacritic, \
sTheTop, \
sTheBottom, \
\
sOption, \
sKey, \
sRef_type, \
sAuthors, \
sAtitle, \
sJtitle, \
sVolume, \
sIssue, \
sYear, \
sYearSuffix, \
sEditors, \
sEditortag, \
sFpage, \
sLpage, \
sBibchap, \
sDOI, \
sPbibdoi, \
sPost, \
sArxiv, \
sOtitle, \
sMisc, \
sBktitle, \
sBktitle_cont, \
sBkstitle, \
sPublisher, \
sEdition, \
sReport, \
sOrg, \
sPlace, \
sRefItemsA, \
sSuspectMark, \
sSuspectJnlMark
local sRegexp, \
sExtracted, \
sAu_order, \
sAu_order_1st, \
sAu_Case, \
sAu_Minor_Sep_1st, \
sAu_Minor_Sep, \
sAu_Major_Sep, \
sAu_Prefix, \
sAu_Suffix
-- Comment added for test
-- Kaveh's comments for testing
on openstack
put the millisecs into ms
if the short name of this stack is not "RVClean" then
-- must be a substack
exit openstack
end if
-- ask "Please enter you name"
ensure_locked
set the cursor to watch
choose browse tool
read_settings
put "——————————————————————————————————————————————————————————" \
into separator
put "" into gUndoHistory
put fld "undo_s" of stack settings into no_of_undo_s
put 1 into undo_count
check_for_undo
get_remote_prefs
_define_color_tags
put "https://docs.google.com/spreadsheets/d/1gJhRbnCqApsVEZ74apcT6sWlN3fZKhZdHBbdfr5Yk2Q/edit#gid=1454732811"\
into gGoogleDataSheet
import_data
put empty into s_Start
put empty into s_End
put "255,0,0" into sOriginalTextColor
put "1,71,250" into sReplacementTextColor
-- get mk_getprefs() -- read prefs from local system
put "(" & \
"[①②③④⑤⑥]" & \
sStarTag & \
")" \
into sBeginTagRegex
put "(" & \
sStarTag & \
"[①②③④⑤⑥]" & \
")" \
into sEndTagRegex
put "•£•" into sSuspectMark
put "•€•" into sSuspectJnlMark
save_regex_for_fields
-- À-ÿ: accented characters
-- Α-϶: greek characters. NB "Α" is not normal A
-- just copied letters from this page:
-- https://en.wikipedia.org/wiki/Greek_and_Coptic
-- and it seems to work
set the cWordChars of fld Unstructured to "[-–.@:/0-9A-Za-zÀ-ÿΑ-϶]"
put sorted_filter_list() into gSortedFilterList
start using font file "/Users/kaveh/Library/Fonts/Entypo.ttf"
start using font file "/Users/kaveh/Library/Fonts/FontAwesome.ttf"
--
put the long name of group "Search and replace" into gSearchGroup
put the long name of fld "Search" of gSearchGroup into gSearchField
put the long name of fld "Replace" of gSearchGroup into gReplaceField
--
-- get apply_filter_tags("Search text")
-- get apply_filter_tags("Replace text")
-- send update_patterns to stack "patterns"
define_pattern_columns
define_bug_columns
set the label of btn "User" to it
-- update_patterns
-- do_find
unlock screen
-- answer the millisecs - ms
end openstack
on _define_color_tags
put "①,②,③,④,⑤,⑥,⑦,⑧,⑨,⑩,⑪,⑫,⑬,⑭,⑮,⑯" \
into sPatternTagA
split sPatternTagA by comma -- make it into an array
put "ⓐ,ⓑ,ⓒ,ⓓ,ⓔ,ⓕ,ⓖ,ⓗ,ⓘ,ⓙ,ⓚ,ⓛ,ⓜ,ⓝ,ⓞ,ⓟ,ⓠ,ⓡ,ⓢ,ⓣ,ⓤ,ⓥ,ⓦ,ⓧ,ⓨ,ⓩ" \
into sScopeTagA
split sScopeTagA by comma -- make it into an array
put "★" into sStarTag
put "☆" into sWhiteStar
end _define_color_tags
on define_pattern_columns
put "Pattern_name", \
"Ref_type", \
"Example", \
"Search_pattern", \
"Extracted", \
"Au_order_1st", \
"Au_order", \
"Au_case", \
"Au_minor_sep_1st", \
"Au_minor_sep", \
"Au_major_sep", \
"Au_prefix", \
"Au_suffix" \
into sPatternColumns
end define_pattern_columns
on define_bug_columns
put "bug_ID", \
"file_name", \
"ref_key", \
"Description", \
"User", \
"the_Date", \
"Feedback_type", \
"bug_status" \
into gBugVariables
end define_bug_columns
----------------------------------------------------------
--------------------- Begin pref file section ------------
----------------------------------------------------------
function mkprefsfolder
-- for full explanation see:
-- http://newsletters.livecode.com/october/issue99/newsletter3.php
--
## Check the platform to decide where to store the file(s)
switch the platform
case "MacOS"
## The folder "Preferences" in the current users
# "Library" folder:
put specialFolderPath("preferences") into spfp
break
case "Win32"
## Everything >= Win2000
if the systemversion contains "NT" then
put specialFolderPath(26) into spfp
else
## Win95, 96, 97, 98, 98ME and 99 :-)
## Should be something like C:/System
## But I am not sure...
put specialfolderpath("system") into spfp
end if
break
default
## Linux
## The current users HOME folder
put $HOME into spfp
break
end switch
if there is not a folder (spfp & "/RVClean") then
create folder (spfp & "/RVClean" )
end if
## Return the path including trailing SLASH, ready for use!
return (spfp & "/RVClean/")
end mkprefsfolder
function mk_getprefs
## This function will return the content of our previously
# saved prefs file or EMPTY,
## if the file has not yet been saved!
put mkprefsfolder() into tPath
if there is a file (tPath & "search_items.txt") then
return url("file:" & tPath & "search_items.txt")
else
## not yet saved
return empty
end if
end mk_getprefs
function mk_setprefs tPrefs
## This finction will write the complete prefs to the above
# mentioned file:
## This could also be a HANDLER but making it a finction we can
# check if the writing
## of the text file did not encounter any error!
put mkprefsfolder() into tPath
put "Lorem ipsum dolor sit amet" into tPrefs
## We write the prefs "en bloc" to the file
put tPrefs into url("file:" & tPath & "search_items.txt")
## This will be empty if everything is OK, but will contain
# a hint on what might have gone wrong
return the result
end mk_setprefs
on write_to_file pFileName pText
put mkprefsfolder() into tPath
put pText into url("file:" & tPath & pFileName)
end write_to_file
on open_the_file pPath
-- put my_path into pPath
set the itemdelimiter to "/"
put pPath into the_directory
put empty into the last item of the_directory
put last item of pPath into the_file --e.g. FLM123.tex
put the_file into file_root
put replacetext( \
file_root, \
"(\d.+)" , \
""\
) \
into tJournal
put toUpper(tJournal) into tJournal
set the label of btn "Journal" to tJournal
delete char -1 to -4 of file_root -- remove extension
-- put replacetext(the_file, "(\.tex)", "") into file_root -- e.g. FLM123
put replacetext(the_file, "(^.+\.)", "") into file_extension
-- following line ensures unicode text recognized
open file pPath for "utf-8" text read
read from file pPath until EOF
if file_extension is "html" or \
file_extension is "htm" \
then
-- set the htmltext of fld "Main text" to the htmltext of it
else if file_extension is "rtf" then
-- set the rtftext of fld "Main text" to the rtftext of it
else
set the cOriginalText of fld "Main text" to it
put it into fld "Main text"
put it into fld "Unstructured"
put empty into fld "Structured"
end if
put the_file into fld "File name"
put the_directory into fld "Directory"
show_full_text
set the label of btn "View option" to "Full text"
-- style_text
reset_undo
check_for_undo
write_undo_file
write_file_data
-- _style_for_clean_up
end open_the_file
on write_file_data
put the number of lines of fld "Main text" into fld "lines"
put the number of chars of fld "Main text" into fld "chars"
end write_file_data
on ensure_locked
if the lockscreen is not true then lock screen
end ensure_locked
on remove_all_styles
ensure_locked
put the vscroll of fld "Main text" into tScroll
put remove_isolation_tags(fld "Main text") into fld "Main text"
set the vscroll of fld "Main text" to tScroll
-- unlock screen
end remove_all_styles
on update_view
put the millisecs into ms
ensure_locked
if hilite of btn "Compact view" is true then
show_compact_text
else
show_full_text
end if
put the htmltext of fld "Main text" into fld "temp"
-- unlock screen
end update_view
on update_view_style
ensure_locked
if view_style is "Plain" then
remove_all_styles
else
if view_style is "Highlighted" then
highlight_text
end if
end if
-- unlock screen
end update_view_style
function remove_isolation_tags pText
put the cIsolateBeginTag of group "Filters" into tIsolateBeginTag
put the cIsolateEndTag of group "Filters" into tIsolateEndTag
replace tIsolateEndTag with "" in pText
replace tIsolateBeginTag with "" in pText
return pText
end remove_isolation_tags
on show_full_text
put the selectedChunk of fld "Main text" into tChunk
set the lockText of fld "Main text" to false
put the seconds into temp
put the milliseconds into time_now
-- remember selection and apply to full text view
-- put word 2 of the selectedChunk of fld "Main text" into gTextStart
-- put word 4 of the selectedChunk of fld "Main text" into gTextEnd
-- hide fld "replacement"
put the number of lines of fld "Main text" into total_lines
set the hidden of line 1 to - 1 of fld "Main text" to false
if separator is not empty \
and the hilite of btn "Use separator" is true then
replace return & separator with "" in fld "Main text" \
of stack "RVClean" preserving styles
end if
set the borderwidth of line 1 to -1 of fld "main text" to empty
set the spaceabove of line 1 to -1 of fld "main text" to empty
if tChunk is not empty then select tChunk
end show_full_text
on select_me
get the cSelectedText of fld "Main text"
if it is not empty then
select it
end if
end select_me
on show_compact_text
put the selectedChunk of fld "Main text" into tChunk
set the cSelectedText of fld "Main text" to tChunk
set the hidden of line 1 to -1 of fld "Main text" to true
hide fld "replacement" of stack "RVClean"
put the seconds into temp
-- for use of styledText see Kaveh's question and the replies here:
-- http://lists.runrev.com/pipermail/use-livecode/2018-November/251753.html
put the milliseconds into time_now
put empty into s
set linedelimiter to return & "<p"
put the htmltext of fld "Main text" into ht
replace "<p hidden" with "<p" in ht
put return before ht -- to ensure first line is done right
repeat for each line L in ht
if offset("color=",L)>0 then
put return & "<p" & L after s
if separator is not empty and \
the highlight of btn "Use separator" is true \
then
put return & separator & return after s
end if
else put "<p hidden" & L after s
end repeat
put empty into char 1 of ht -- remove the return
set htmltext of fld "Main text" to s
put the milliseconds - time_now into time_to_hide
put "Hide time: " & time_to_hide into fld "time to show"
--
set the borderwidth of line 1 to -1 of fld "main text" to 1
set the spaceabove of line 1 to -1 of fld "main text" to 2
send "select_me" to me in .01 seconds
--set the locktext of fld "Main text" to true
-- ideally we need a better way of setting scroll
-- so text is same position when going from expanded text
-- set the scroll of fld "Main text" to gCompactScroll
end show_compact_text
on write_undo_file
put the ticks into current_time
put current_time & "," & return before gUndoHistory
put file_root & "-" & current_time & "." & file_extension into tempfile
put the_directory &"undo/" into backup_directory
put backup_directory & tempfile into backup_file
if there is not a folder backup_directory then
create folder backup_directory
end if
open file backup_file for "utf-8" text write
put the htmltext of fld "Main text" into tText
write tText to file backup_file
close file backup_file
set the cText_has_changed of fld "Main text" to false
end write_undo_file
on undo_edit
if undo_count > the number of items of gUndoHistory -2 then
-- return at end of undo count makes it one line longer
exit undo_edit
end if
put undo_count +1 into undo_count
open_undo_file
hide fld "replacement"
end undo_edit
on redo_edit
if undo_count < 2 then
exit redo_edit
end if
put undo_count -1 into undo_count
open_undo_file
hide fld "replacement"
end redo_edit
on open_undo_file
-- to avoid several undo commands queuing up when
-- "u" or "r" are held down:
lock messages
put the vscroll of fld "Main text" into tScroll
get item undo_count of gUndoHistory
put replacetext(it, return, "") into it
put file_root & "-" & it & "." & file_extension into tempfile
put the_directory &"undo/"& tempfile into to_retrieve
open file to_retrieve for read
read from file to_retrieve until EOF
set the htmltext of fld "Main text" to it
-- if view_style is "highlighted" then
-- highlight_text
-- end if
-- if hilite of btn "Compact view" is true then
-- show_compact_text
-- end if
set the vscroll of fld "Main text" to tScroll
end open_undo_file
on check_for_undo
-- exit check_for_undo
if the cText_has_changed of fld "Main text" then
write_undo_file
end if
send "check_for_undo" to me in 5 seconds
end check_for_undo
on reset_undo
put 0 into undo_count
put empty into gUndoHistory
end reset_undo
on mouseup
if the short name of the owner of the target is "To highlight" then
if the highlight of btn "Highlighted" is true then
highlight_text
end if
pass mouseup
end if
if the short name of \
the owner of the target is "Items to clean up" then
_style_for_clean_up
end if
end mouseup
function convert_field_to_regexp pFilter, pText
-- only use function if "Regexp" is not checked
if the hilite of btn "Regexp" of group pFilter is not true then
put force_to_regexp(pText) into pText
end if
return pText
end convert_field_to_regexp
function force_to_regexp pText
put replacetext(pText, "(\\)", "\\") into pText
put replacetext(pText, "(\[)", "\[") into pText
put replacetext(pText, "(\])", "\]") into pText
put replacetext(pText, "(\{)", "\{") into pText
put replacetext(pText, "(\})", "\}") into pText
put replacetext(pText, "(\.)", "\.") into pText
put replacetext(pText, "(\()", "\(") into pText
put replacetext(pText, "(\))", "\)") into pText
put replacetext(pText, "(\+)", "\+") into pText
put replacetext(pText, "(\-)", "\-") into pText
put replacetext(pText, "(\^)", "\^") into pText
put replacetext(pText, "(\$)", "\$") into pText
put replacetext(pText, "(\*)", "\*") into pText
put replacetext(pText, "(\?)", "\?") into pText
put replacetext(pText, "(\|)", "\|") into pText
return pText
end force_to_regexp
on structure_references
extract_references
_structure_refs_just_imported
set the cNowProcessing of group "Structuring" to "References"
end structure_references
on extract_references
put empty into tIgnore
put empty into tExtractStart
put empty into tExtractEnd
put fld "Main text" into tText
get matchchunk(tText, \
"(?s)((?<=\n)\s*\\begin{thebibliography}.+?(?=\n))", \ -- "(\\bibitem)", \
tIgnore, \
tExtractStart)
get matchchunk(tText, \
"(?s)((?<=\n)\s*\\end{thebibliography})", \
tExtractEnd, \
tIgnore)
put tExtractStart +1 into tExtractStart
put tExtractEnd - 1 into tExtractEnd
set the cExtractStart of fld "Main text" to tExtractStart
set the cExtractEnd of fld "Main text" to tExtractEnd
put char tExtractStart to tExtractEnd of tText into tText
-- put return before \indent (some refs don't have double return)
put _insert_return_before_slash_indent(tText) into tText
-- if no key then remove \bibitem so new key generated
put _remove_bibitem_if_no_key(tText) into tText
put _multiple_returns_to_three_returns(tText) into fld "Unstructured"
show_structuring
end extract_references
function _remove_bibitem_if_no_key pText
put replacetext(pText, "(?is)(\\bibitem(\[[^\[\]]+?\])?\s*({\s*})\n)", "") into pText
return pText
end _remove_bibitem_if_no_key
function _insert_return_before_slash_indent pText
put replacetext(pText, "(?s)(\n\\indent)", return&return & "\indent") into pText
return pText
end _insert_return_before_slash_indent
function _remove_leading_returns pText
put replacetext(pText, "(?s)(^\n+)", "") into pText
return pText
end _remove_leading_returns
on show_structuring
show group "Structuring"
-- set the label of btn "Structuring" to "Hide structuring"
end show_structuring
on hide_structuring
hide group "Structuring"
-- set the label of btn "Structuring" to "Hide structuring"
end hide_structuring
on dragDrop
put the dragData into my_path
get matchText( \
my_path, \
"(...$)", \
file_extension\
)
if file_extension is not "tex" and \
file_extension is not "txt" then
answer "Please import a .tex or .txt file"
exit dragDrop
end if
open_the_file my_path
-- refresh
end dragDrop
function convert_to_hex pMyColor
-- see https://forums.livecode.com/viewtopic.php?t=17143
put empty into tMyHexColor
repeat with x = 1 to the number of items in pMyColor
put baseconvert(item x of pMyColor,10,16) into tHex
if the number of chars in tHex < 2 then
put "0" before tHex
end if
put tHex after tMyHexColor
end repeat
put "#" before tMyHexColor
return tMyHexColor
end convert_to_hex
function wordWrapped pMaxLength
local tWrappedText
-- copied from: http://bit.ly/2AggXzk
/*
This function has been modified by Kaveh so that line wrapping is done,
but only on portions not highlighted. So by highlighting all
display equations we can ensure that equations are not hard wrapped,
and long lines are kept as they are.
*/
if pMaxLength is empty or pMaxLength is not a number then
## use a default value if maxLength is not provided
put 75 into pMaxLength
end if
if pMaxLength is not an integer then
## truncate it
put trunc(pMaxLength) into pMaxLength
end if
put fld "Main text" into tText
put the htmltext of fld "Main text" into tHTML
put 0 into tLine_number
repeat for each line tLine in tText
put tLine_number + 1 into tLine_number
put line tLine_number of tHTML into tHTML_line
if (length(tLine) <= pMaxLength) \
OR (offset(" bgcolor=",line tLine_number of tHTML)>0) \
then
## the line is shorter than the maximum,
## OR there is some bgcolor in line
put tLine & return after tWrappedText
else if matchtext(tLine, "^ *%") is true \
then
## comment line and it is a tex file. We must always be
## aware not all files are tex so the handlers have to
## be general
put tLine & return after tWrappedText
else
## tLine is longer than the maximum
repeat until tLine is empty
## try to break the line at a space
repeat with x = pMaxLength down to 1
if char x of tLine is space or \
length(tLine) < pMaxLength then
put (char 1 to x-1 of tLine) & \
return after tWrappedText
delete char 1 to x of tLine
exit repeat
end if
end repeat
if x = 1 then
## no spaces in the first pMaxLength chars
## so we break it at that number of characters
put char 1 to pMaxLength of tLine & return after tWrappedText
delete char 1 to pMaxLength of tLine
end if
end repeat
end if
end repeat
return tWrappedText
end wordWrapped
on do_live_search
ensure_locked
-- if there is a selection, we can reset that selection after search
put the selectedChunk of fld "Main text" into tChunk
put the milliseconds into tTimeNow
if the cText_has_changed of fld "Main text" is false then
-- no key pressed since handler was last called
exit do_live_search
end if
if the label of btn "View option" is "Full text" then
save_scroll
end if
--
put (fld "Type delay" of stack "settings") * 1000 into tTypeDelay
if (tTimeNow - the cWhen_text_changed of fld "Main text") \
> tTypeDelay then
do_find
set the cText_has_changed of fld "Main text" to false
else
-- check regularly to see if time to search
put fld "Find delay" of stack "settings" into tFindDelay
send "do_live_search" to me in tFindDelay seconds
end if
if the label of btn "view option" is "Full text" then
reset_scroll
end if
if tChunk is not empty then
select tChunk
end if
-- unlock screen
end do_live_search
--on make_me_active
-- /**
-- For fields: "Search", "Text before", "Text after"
-- note 3 levels of grouping. if this grouping level changes
-- then Handler will not work, so need to take care. Perhaps there is a better
-- way than hard coding exact level of grouping
-- **/
-- put the short name of the owner \
-- of the owner \
-- of the owner of target \
-- into gActiveFilter
-- put the short name of the target into gActiveSearchField
--end make_me_active
--on mark_me_for_edit
-- put the short name of the owner \
-- of the owner \
-- of the owner of target \
-- into gFilterBeingEdited
-- put the short name of the target into gFieldBeingEdited
-- put gFilterBeingEdited into gActiveFilter
-- put gFieldBeingEdited into gActiveSearchField
--end mark_me_for_edit
function style_text pText
-- insert tags e.g.
-- ①★dolor★①
-- but do not put tags at start and end of each para
put pText into tStyledText
-- remove temporary tags used to isolate filters
-- put remove_isolation_tags(tStyledText) into tStyledText
-- put remove_confusing_tags(tStyledText) into tStyledText
--
put the millisecs into ms
repeat for each line L in gSortedFilterList
put L into tFilter
if fld "search" of group tFilter is empty then
put empty into fld "Count" of group tFilter
put empty into fld "Milliseconds" of group tFilter
next repeat
end if
put highlight_search_item(tStyledText, tFilter) into tStyledText
end repeat
set the cAppliedChanges of fld "Search" of gSearchGroup to tStyledText
set the cStyledText of fld "Main text" to tStyledText
put tStyledText into fld "temp found"
put the millisecs - ms into fld "styled"
return tStyledText
end style_text
function focus_view pStyledText
put gSortedFilterList into tList
put the label of btn "View option" into tViewOption
switch tViewOption
case "Full text"
return pStyledText
break
case "Isolate last filter"
put line -1 of tList into tFilter
put isolate_filter(tFilter, pStyledText) into pText
break
case "Isolate last two filters"
if the number of lines of gSortedFilterList is 1 then
set the label of btn "View option" to "Isolate last filter"
put line -1 of tList into tFilter
else
put line -2 of tList into tFilter
end if
put isolate_filter(tFilter, pStyledText) into pText
break
end switch
return pText
end focus_view
function styled_to_html pText
put pText into tStyledText
-- put close_tags_for_each_line(tStyledText) into tStyledText
put convert_temp_Tags_to_html(tStyledText) into tHTMLText
-- put convert_search_tags_to_html(tHTMLText) into tHTMLText
-- put convert_replace_tags_to_html(tHTMLText) into tHTMLText
put p_tags_insert(tHTMLText) into tHTMLText
if the label of btn "View option" is not "Full text" then
end if
return tHTMLText
end styled_to_html
on do_find
ensure_locked
lock messages
if gSortedFilterList is empty then
-- nothing to highlight
put fld "Main text" into fld "Main text" -- remove styles
set the cStyledText of fld "Main text" to fld "Main text"
exit do_find
end if
if is_all_regex_valid() is false then
exit do_find
end if
if the visible of grp "Search and replace" is true then
put the cUnappliedChanges of gSearchGroup into tStyledText
-- put the long name of fld "isolate" into tTextField
else
put style_text(fld "Main text") into tStyledText
-- put the long name of fld "Main text" into tTextField
end if
put focus_view(tStyledText) into tFocusedText
put tFocusedText into fld "temp found"
put styled_to_html(tFocusedText) into tHTMLText
-- if label of btn "View option" is "Show paras with highlights" then
-- show_compact_text
--end if
put tHTMLText into fld "temp"
--
-- if gSearchMode is true then
if the visible of grp "Search and replace" is true then
-- always show "isolate"
set the HTMLText of fld "isolate" to tHTMLText
show fld "isolate"
else
if the label of btn "View option" is "Full text" then
set the HTMLText of fld "Main text" to tHTMLText
hide fld "isolate"
else
set the HTMLText of fld "isolate" to tHTMLText
show fld "isolate"
end if
end if
-- unlock screen
end do_find
function is_all_regex_valid
-- check all fields have valid regex
repeat for each line L in gSortedFilterList
put L into tFilter
if fld "search" of group L is empty then next repeat
put the cFullRegex of group L into tSearch
if is_regex_correct(tSearch) is false then
remove_all_styles
return false
end if
end repeat
return true
end is_all_regex_valid
on save_text_and_styledText
-- put the cOriginalText of fld "Main text" into tText
-- put style_text(tText) into tStyledText
-- set the cStyledText of fld "Main text" to tStyledText
end save_text_and_styledText
function styled_to_plain_text pText
put replacetext(pText, gColorTagRegex, "") into pText
put replacetext(pText, gEndTagRegex, "") into pText
return pText
end styled_to_plain_text
case "Show paras with highlights"
show_compact_text
break
function insert_hidden_html_tags pText
put empty into tResult
repeat for each line L in pText
if offset("••showthisline••", L) = 0 then
replace "<p>" with "<p hidden>" in L
end if
put L & return after tResult
end repeat
replace "••showthisline••" with "" in tResult
delete last char of tResult
return tResult
end insert_hidden_html_tags
function remove_isolate_remainder_tags pText
put the cIsolateBeginTag of group "Filters" into tIsolateBeginTag