-
Notifications
You must be signed in to change notification settings - Fork 7
/
simc.em
1080 lines (943 loc) · 34.2 KB
/
simc.em
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
/**************************************************************
* Source Insight Macro Collection
*
* File: simc.em
*
* Author: Jia Shi <https://github.com/j5shi>
*
* Version: 0.0.1
*
* License: MIT License
*
* Last Modified: 2014-11-09 17:35:30
*
* Usage:
* 1. Add this file to your project, or to the base project so
* that you just need to do it once for all your projects.
*
* 2. From menu "Options" -> "Key Assignments..." to bind any
* macro functions in this file to keys.
*
* 3. Enjoy the macros!
*
* Copyright (c) 2014-2015, Jia Shi
**************************************************************/
/*-------------------------------------------------------------------------
Automatically insert code snippet.
Currently the following code snippets are supported, but this can
be extened easily:
- if
- else
- for
- while
- do-while
- switch
- case
- default
- main
For example:
1. bind this function to "Tab"
2. type "for" and press "Tab"
3. this is what will be inserted:
for (###; ###; ###)
{
###
}
4. and the first ### pattern is selected.
-------------------------------------------------------------------------*/
macro simcInsertSnippet()
{
var sMacroName; sMacroName = "simcInsertSnippet"
var hWnd
var rSel
var hBuf
var sLine
var iCh
var sIndent
var iChLim
var asciiA
var asciiZ
var cCharUpper
var asciiCharUpper
var rWordinfo
var lnCurrent
var PATTERN; PATTERN = "###"
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
// if no windows is opened, then do nothing.
hWnd = GetCurrentWnd()
if (hWnd == 0)
{
stop
}
else
{
rSel = GetWndSel(hWnd)
hBuf = GetWndBuf(hWnd)
sLine = GetBufLine(hBuf, rSel.lnFirst)
// scan backwords over white space and tab, if any,
// to the ending of a word
iCh = rSel.ichFirst - 1
if (iCh >= 0)
{
while (sLine[iCh] == SPACE || sLine[iCh] == TAB)
{
iCh = iCh - 1
if (iCh < 0)
break
}
}
// scan backwords to start of word
iChLim = iCh + 1
asciiA = AsciiFromChar("A")
asciiZ = AsciiFromChar("Z")
while (iCh >= 0)
{
cCharUpper = toupper(sLine[iCh])
asciiCharUpper = AsciiFromChar(cCharUpper)
if ((asciiCharUpper < asciiA || asciiCharUpper > asciiZ) && !IsNumber(cCharUpper))
break // stop at first non-identifier character
iCh = iCh - 1
}
// parse word just to the left of the cursor
// and store the result to the rWordinfo record
// rWordinfo.szWord = the word string
// rWordinfo.ich = the first ich of the word
// rWordinfo.ichLim = the limit ich of the word
iCh = iCh + 1
rWordinfo = ""
rWordinfo.sWord = strmid(sLine, iCh, iChLim)
rWordinfo.iCh = iCh
rWordinfo.iChLim = iChLim
// generate the indent string, tab will be replaced
// by 4 spaces.
iCh = 0
sIndent = ""
while (sLine[iCh] == SPACE || sLine[iCh] == TAB)
{
if (sLine[iCh] == SPACE)
sIndent = sIndent # SPACE
else if (sLine[iCh] == TAB)
sIndent = sIndent # SPACE # SPACE # SPACE # SPACE
iCh++
}
// select any space / tab between the end
// of the identifier and the cursor and
// replace them with the snippet.
if (rWordinfo.sWord == "if" || rWordinfo.sWord == "while" ||
rWordinfo.sWord == "else" || rWordinfo.sWord == "for" ||
rWordinfo.sWord == "switch" || rWordinfo.sWord == "do" ||
rWordinfo.sWord == "case" || rWordinfo.sWord == "default" )
{
SetBufIns(hBuf, rSel.lnFirst, rWordinfo.iChLim)
rSel.ichFirst = rWordinfo.ichLim
rSel.ichLim = rSel.ichLim
SetWndSel(hWnd, rSel)
}
if (rWordinfo.sWord == "if" || rWordinfo.sWord == "while")
{
SetBufSelText(hBuf, " (@PATTERN@)")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # "}")
}
else if (rWordinfo.sWord == "else")
{
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # "}")
}
else if (rWordinfo.sWord == "for")
{
SetBufSelText(hBuf, " (@PATTERN@; @PATTERN@; @PATTERN@)")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # "}")
}
else if (rWordinfo.sWord == "switch")
{
SetBufSelText(hBuf, " (@PATTERN@)")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " case @PATTERN@:")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 4, sIndent # " break;")
InsBufLine(hBuf, rSel.lnFirst + 5, sIndent # "}")
}
else if (rWordinfo.sWord == "case")
{
SetBufSelText(hBuf, " @PATTERN@:")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " break;")
}
else if (rWordinfo.sWord == "default")
{
SetBufSelText(hBuf, ":")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " break;")
}
else if (rWordinfo.sWord == "do")
{
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " @PATTERN@")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # "} while (@PATTERN@)")
}
else if (rWordinfo.sWord == "main")
{
SetBufSelText(hBuf, "()")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "{")
InsBufLine(hBuf, rSel.lnFirst + 2, sIndent # " return @PATTERN@;")
InsBufLine(hBuf, rSel.lnFirst + 3, sIndent # "}")
}
else if (rWordinfo.sWord == "add")
{
delete_line
InsBufLine(hBuf, rSel.lnFirst, sIndent # "/* Begin Add by: @PATTERN@ PN: @PATTERN@ Dsc: @PATTERN@ */")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "/* End Add PN: @PATTERN@ */")
}
else if (rWordinfo.sWord == "mod")
{
delete_line
InsBufLine(hBuf, rSel.lnFirst, sIndent # "/* Begin Modify by: @PATTERN@ PN: @PATTERN@ Dsc: @PATTERN@ */")
InsBufLine(hBuf, rSel.lnFirst + 1, sIndent # "/* End Modify PN: @PATTERN@ */")
}
else if (rWordinfo.sWord == "del")
{
delete_line
InsBufLine(hBuf, rSel.lnFirst, sIndent # "/* Delete by: @PATTERN@ PN: @PATTERN@ Dsc: @PATTERN@ */")
}
else
{
// incase this macro is associated with
// tab, then it needs to behavior like
// a normal tab when the user doesn't want
// to insert any snippets.
if (CmdFromKey(AsciiFromChar(TAB)) == sMacroName)
{
// insert expanded tab (4 spaces) at the
// begining of each selected line.
rSel = GetWndSel(hWnd)
lnCurrent = rSel.lnFirst
while (lnCurrent <= rSel.lnLast)
{
SetBufIns(hBuf, lnCurrent, rSel.ichFirst)
SetBufSelText(hBuf, SPACE # SPACE # SPACE # SPACE)
lnCurrent++
}
// in case multi-line are selected, keep the lines
// selected after the macro finished executing, this
// has the same behavior as pressing tabs.
if (rSel.lnFirst != rSel.lnLast)
{
rSel.ichLim = rSel.ichLim + 4
SetWndSel(hWnd, rSel)
}
}
stop
}
// select the first PATTERN
rSel.ichFirst = rWordinfo.ichLim
rSel.ichLim = rWordinfo.ichLim
SetWndSel(hWnd, rSel)
LoadSearchPattern(PATTERN, False, False, True)
Search_Forward
}
}
/*-------------------------------------------------------------------------
Close all non-dirty file windows, dirty file windows are those source
file windows with unsaved mofidication.
-------------------------------------------------------------------------*/
macro simcCloseAllNonDirtyWindow()
{
var hWnd; hWnd = GetCurrentWnd()
var hWndNext
var hBuf
while (hWnd != 0)
{
hWndNext = GetNextWnd(hWnd)
hBuf = GetWndBuf(hWnd)
if (!IsBufDirty(hBuf))
CloseBuf(hBuf)
hWnd = hWndNext
}
}
/*-------------------------------------------------------------------------
Comment out the selected lines in single line comments style.
By changing the @TOKEN@ variable in the macro function, user can
replace default comment token from "// " to any other tokens, for
example, "# " for Python.
-------------------------------------------------------------------------*/
macro simcCommentLineOut()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSelOrig; rSelOrig = GetWndSel(hWnd)
var rSelTemp; rSelTemp = rSelOrig
var lnCurrentLine; lnCurrentLine = rSelOrig.lnFirst
var sCurrentLine
var iCurrentLineLen
var iChar
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
var TOKEN; TOKEN = "// "
while (lnCurrentLine <= rSelOrig.lnLast)
{
sCurrentLine = GetBufLine(hBuf, lnCurrentLine)
iCurrentLineLen = GetBufLineLength(hBuf, lnCurrentLine)
iChar = 0
while (iChar < iCurrentLineLen)
{
if (sCurrentLine[iChar] == SPACE || sCurrentLine[iChar] == TAB)
{
iChar++
}
else
{
rSelTemp.lnFirst = lnCurrentLine
rSelTemp.lnLast = lnCurrentLine
rSelTemp.ichFirst = iChar
rSelTemp.ichLim = iChar
SetWndSel(hWnd, rSelTemp)
SetBufSelText(hBuf, TOKEN)
break
}
}
lnCurrentLine++
}
SetWndSel(hWnd, rSelOrig)
}
/*-------------------------------------------------------------------------
Uncomment out the lines which have been commented out in single line
comments style.
By changing the @TOKEN@ variable in the macro function, user can
replace default comment token from "// " to any other tokens, for
example, "# " for Python.
-------------------------------------------------------------------------*/
macro simcUncommentLineOut()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSelOrig; rSelOrig = GetWndSel(hWnd)
var rSelTemp; rSelTemp = rSelOrig
var lnCurrentLine; lnCurrentLine = rSelOrig.lnFirst
var sCurrentLine
var sCurrentLineTemp
var iCurrentLineLen
var iChar
var TOKEN; TOKEN = "// "
var TOKEN_LEN; TOKEN_LEN = strlen(TOKEN)
while (lnCurrentLine <= rSelOrig.lnLast)
{
sCurrentLine = GetBufLine(hBuf, lnCurrentLine)
sCurrentLineTemp = ""
iCurrentLineLen = GetBufLineLength(hBuf, lnCurrentLine)
iChar = 0
while (iChar <= iCurrentLineLen - TOKEN_LEN)
{
if (strmid (sCurrentLine, iChar, iChar+TOKEN_LEN) == TOKEN)
{
rSelTemp.lnFirst = lnCurrentLine
rSelTemp.lnLast = lnCurrentLine
rSelTemp.ichFirst = iChar + TOKEN_LEN
rSelTemp.ichLim = iCurrentLineLen
SetWndSel(hWnd, rSelTemp)
PutBufLine(hBuf, lnCurrentLine, cat(sCurrentLineTemp, strmid(sCurrentLine, rSelTemp.ichFirst, rSelTemp.ichLim)))
break
}
else
{
sCurrentLineTemp = cat(sCurrentLineTemp, sCurrentLine[iChar])
iChar++
continue
}
}
lnCurrentLine++
}
SetWndSel(hWnd, rSelOrig)
}
/*-------------------------------------------------------------------------
Comment out the selected lines in block comments style.
By changing the @iFirstCommLineLen@ user can adjust the number of the
asterisks in the first block comment line. But the final length of the
first block comment line is decided by the longest line in the comment,
if it is longer than default @iFirstCommLineLen@, then its length will
be used as the value of @iFirstCommLineLen@.
-------------------------------------------------------------------------*/
macro simcCommentBlockOut()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSelOrig; rSelOrig = GetWndSel(hWnd)
var lnCurrentLine
var sCurrentLine
var iLineLen
var iFirstCommLineLen; iFirstCommLineLen = 40 // default len of the 1st comment line
// calculate the length of the longest line
lnCurrentLine = rSelOrig.lnFirst
while (lnCurrentLine <= rSelOrig.lnLast)
{
iLineLen = GetBufLineLength(hBuf, lnCurrentLine)
if (iLineLen > iFirstCommLineLen)
iFirstCommLineLen = iLineLen
lnCurrentLine++
}
// insert the first block comment line.
InsBufLine(hBuf, rSelOrig.lnFirst, cat("/", __str_rep("*", iFirstCommLineLen)))
// since an extra line in inserted, rSelOrig became incorrect
// and needs to be fixed
rSelOrig.lnFirst = rSelOrig.lnFirst + 1
rSelOrig.lnLast = rSelOrig.lnLast + 1
lnCurrentLine = rSelOrig.lnFirst
while (lnCurrentLine <= rSelOrig.lnLast)
{
sCurrentLine = GetBufLine(hBuf, lnCurrentLine)
PutBufLine(hBuf, lnCurrentLine, cat(" * ", sCurrentLine))
lnCurrentLine++
}
InsBufLine(hBuf, lnCurrentLine, cat(" ", cat(__str_rep("*", iFirstCommLineLen), "/")))
}
/*-------------------------------------------------------------------------
Uncomment out the selected lines in block comments style.
-------------------------------------------------------------------------*/
macro simcUncommentBlockOut()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSelOrig; rSelOrig = GetWndSel(hWnd)
var lnCurrentLine; lnCurrentLine = rSelOrig.lnFirst
var sCurrentLine
var TOKEN_BEG; TOKEN_BEG = "/*"
var TOKEN_MID; TOKEN_MID = "*"
var TOKEN_END; TOKEN_END = "*/"
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
while lnCurrentLine <= rSelOrig.lnLast
{
sCurrentLine = GetBufLine(hBuf, lnCurrentLine)
if __str_only_contain(sCurrentLine, TOKEN_MID # SPACE # TAB) // performance consideration
{
PutBufLine(hBuf, lnCurrentLine, "")
lnCurrentLine++
}
else if __str_only_contain(sCurrentLine, TOKEN_BEG # TOKEN_END # SPACE # TAB) // performance consideration
{
DelBufLine(hBuf, lnCurrentLine)
if (rSelOrig.lnLast > rSelOrig.lnFirst)
rSelOrig.lnLast = rSelOrig.lnLast - 1 // the first line has been removed, no need to ++
else
lnCurrentLine++
}
else
{
if __str_begin_with(sCurrentLine, TOKEN_BEG)
sCurrentLine = __str_lstrip(sCurrentLine, TOKEN_BEG # TAB # SPACE)
if __str_begin_with(sCurrentLine, TOKEN_MID)
{
sCurrentLine = __str_lstrip(sCurrentLine, TOKEN_MID # TAB # SPACE)
}
if __str_end_with(sCurrentLine, TOKEN_END)
sCurrentLine = __str_rstrip(sCurrentLine, TOKEN_END # TAB # SPACE)
PutBufLine(hBuf, lnCurrentLine, sCurrentLine)
lnCurrentLine++
}
}
}
/*-------------------------------------------------------------------------
Trims white spaces from the ends of the selected lines in the current
file buffer, if the selection is empty, it does the whole file.
-------------------------------------------------------------------------*/
macro simcTrimSpaces()
{
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var lnCurrent; lnCurrent = rSel.lnFirst
while (lnCurrent <= rSel.lnLast)
{
PutBufLine(hBuf, lnCurrent, __str_rstrip(GetBufLine(hBuf, lnCurrent), SPACE # TAB))
lnCurrent++
}
}
/*-------------------------------------------------------------------------
Paste what in the clipboard to every selected line at the position of
the cursor in the first line.
-------------------------------------------------------------------------*/
macro simcBatchInsert()
{
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var rCursor;
var sLineBefore
var sLineAfter
var sClipboard
var lnCurrentLine;lnCurrentLine = rSel.lnFirst
// read content from clipboard
sLineBefore = GetBufLine(hBuf, rSel.lnFirst)
PasteBufLine(hBuf, rSel.lnFirst)
sLineAfter = GetBufLine(hBuf, rSel.lnFirst)
PutBufLine(hBuf, rSel.lnFirst, sLineBefore)
sClipboard= __str_subtract(sLineAfter, sLineBefore)
// generate the cursor selection
rCursor = rSel
rCursor.ichLim = rCursor.ichFirst
rCursor.fExtended = False
while (lnCurrentLine <= rSel.lnLast)
{
rCursor.lnFirst = lnCurrentLine
rCursor.lnLast = rCursor.lnFirst
if (strlen(GetBufLine(hBuf, lnCurrentLine)) < rCursor.ichFirst)
PutBufLine(hBuf, lnCurrentLine, __str_padding(GetBufLine(hBuf, lnCurrentLine), rCursor.ichFirst))
SetWndSel(hWnd, rCursor)
SetBufSelText(hBuf, sClipboard)
lnCurrentLine++
}
}
/*-------------------------------------------------------------------------
This macro allows users to bind macros with keys in Emacs style, e.g.
<Ctrl+k><Ctrl+x>, in this case, macro simcEmacsStyleKeyBinding is binded
to <Ctrl+k>, and <Ctrl+x> is binded to the macro or command that the user
want to execute.
The main function of this macro is to extend the number of key bindings
-------------------------------------------------------------------------*/
macro simcEmacsStyleKeyBinding()
{
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var functionKey; functionKey = GetKey()
// Map the functionKey code into a simple character.
ch = CharFromKey(functionKey)
if ch == "w"
simcCloseAllNonDirtyWindow
else if ch == "s"
simcSurrounder
else if ch == "c"
simcCommentLineOut
else if ch == "C"
simcUncommentLineOut
else if ch == "b"
simcCommentBlockOut
else if ch == "B"
simcUncommentBlockOut
else if ch == "t"
simcTrimSpaces
else if ch == "d"
simcMatchDelimiter
else if ch == "i"
simcBatchInsert
}
/*-------------------------------------------------------------------------
Macro command that performs a progressive forward search as the user types,
the search is case-insensitive and regex is not supported.
Quit progressive search with 'Enter'
-------------------------------------------------------------------------*/
macro simcProgressiveSearch()
{
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var rSearchRes
var iKeyCode
var cChar
var sSearchStr; sSearchStr = ""
while 1
{
iKeyCode = GetKey()
cChar = CharFromKey(iKeyCode)
if iKeyCode == 13 // Enter - perform search
stop
else if iKeyCode == 8 // Backspace
{
if strlen(sSearchStr) > 0
sSearchStr = strtrunc(sSearchStr, strlen(sSearchStr)-1)
else
continue
}
else
sSearchStr = cat(sSearchStr, cChar)
rSearchRes = SearchInBuf(hBuf, sSearchStr, rSel.lnFirst, rSel.ichFirst, 0, 0, 0)
// wrap search
if rSearchRes == ""
rSearchRes = SearchInBuf(hBuf, sSearchStr, 0, 0, 0, 0, 0)
if rSearchRes!= ""
{
ScrollWndToLine(hWnd, rSearchRes.lnFirst)
SetWndSel(hWnd, rSearchRes)
LoadSearchPattern(sSearchStr, 0, 0, 0)
}
}
}
macro simcJumpForward()
{
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var rSearchRes
var iKeyCode
var cChar
var sSearchStr; sSearchStr = ""
while 1
{
iKeyCode = GetKey()
cChar = CharFromKey(iKeyCode)
if iKeyCode == 13 // Enter - perform search
break
else if iKeyCode == 8 // Backspace
{
if strlen(sSearchStr) > 0
sSearchStr = strtrunc(sSearchStr, strlen(sSearchStr)-1)
else
continue
}
else
sSearchStr = cat(sSearchStr, cChar)
}
LoadSearchPattern(sSearchStr, 0, 0, 0)
Search_Forward
}
macro simcJumpBackward()
{
var hBuf; hBuf = GetCurrentBuf()
var hWnd; hWnd = GetCurrentWnd()
var rSel; rSel = GetWndSel(hWnd)
var rSearchRes
var iKeyCode
var cChar
var sSearchStr; sSearchStr = ""
while 1
{
iKeyCode = GetKey()
cChar = CharFromKey(iKeyCode)
if iKeyCode == 13 // Enter - perform search
break
else if iKeyCode == 8 // Backspace
{
if strlen(sSearchStr) > 0
sSearchStr = strtrunc(sSearchStr, strlen(sSearchStr)-1)
else
continue
}
else
sSearchStr = cat(sSearchStr, cChar)
}
LoadSearchPattern(sSearchStr, 0, 0, 0)
Search_Backward
}
/*-------------------------------------------------------------------------
Finds matching scoping delimiters and jumps to them.
If the cursor is not positioned on a delimiter but is inside
a matching part then the macro will jump to the start of the closest
scope.
Currently matches [], (), <>, {}
-------------------------------------------------------------------------*/
macro simcMatchDelimiter()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSel; rSel = GetWndSel(hWnd)
var sOpenDelim; sOpenDelim = "[{(<"
var sClosDelim; sClosDelim = "]})>"
var sCurrentLine
var cCurrentChar
var iNumofSquareBracket;iNumofSquareBracket = 0
var iNumofParentheses;iNumofParentheses = 0
var iNumofFrenchQuotes;iNumofFrenchQuotes = 0
var iNumofBrace;iNumofBrace = 0
sCurrentLine = GetBufLine(hBuf, rSel.lnFirst)
cCurrentChar = sCurrentLine[rSel.ichFirst]
if(__str_contain(sOpenDelim, cCurrentChar))
jump_to_match
else if(__str_contain(sClosDelim, cCurrentChar))
jump_to_match
else
{
while 1
{
LoadSearchPattern("[\\[(<{}>)\\]]", 0, 1, 0)
search_backward
rSel = GetWndSel(hWnd)
sCurrentLine = GetBufLine(hBuf, rSel.lnFirst)
cCurrentChar = sCurrentLine[rSel.ichFirst]
if cCurrentChar == "["
iNumofSquareBracket++
else if cCurrentChar == "]"
iNumofSquareBracket--
else if cCurrentChar == "{"
iNumofBrace++
else if cCurrentChar == "}"
iNumofBrace--
else if cCurrentChar == "("
iNumofParentheses++
else if cCurrentChar == ")"
iNumofParentheses--
else if cCurrentChar == "<"
iNumofFrenchQuotes++
else if cCurrentChar == ">"
iNumofFrenchQuotes--
if iNumofBrace > 0 || iNumofFrenchQuotes > 0 || iNumofParentheses > 0 || iNumofSquareBracket >0
break
}
}
}
/*-------------------------------------------------------------------------
Surround the selection with what you type.
Hit 'Enter' to quit.
-------------------------------------------------------------------------*/
macro simcSurrounder()
{
var hWnd; hWnd = GetCurrentWnd()
var hBuf; hBuf = GetCurrentBuf()
var rSel; rSel = GetWndSel(hWnd)
var rSelOrig; rSelOrig = rSel
var iKeyCode
var cChar
var sSelection
var iLenSel; iLenSel = strlen(GetBufSelText(hBuf))
var sSurroundSymbol; sSurroundSymbol = ""
var sSurroundSymbolPrev; sSurroundSymbolPrev = sSurroundSymbol
if !rSel.fExtended
stop
while 1
{
SetWndSel(hWnd, rSel)
iKeyCode = GetKey()
cChar = CharFromKey(iKeyCode)
if iKeyCode == 13 // Enter
stop
else if iKeyCode == 8 // && sSurroundSymbolPrev!= "" // Backspace
{
sSelection = GetBufSelText(hBuf)
if strlen(sSelection) >= 2
{
SetBufSelText(hBuf, strmid(sSelection, 1, strlen(sSelection)-1))
// update selection
rSel.ichLim = rSel.ichLim - 2
}
}
else
{
sSurroundSymbol = cat(sSurroundSymbol, cChar)
if sSurroundSymbol != ""
{
sSurroundSymbolPrev = sSurroundSymbol
rSel.ichLim = rSel.ichLim + 2 * strlen(sSurroundSymbol)
sSelection = GetBufSelText(hBuf)
// insert surrounder
if sSurroundSymbol == "(" || sSurroundSymbol == ")"
SetBufSelText(hBuf, cat("(", cat(sSelection, ")")))
else if sSurroundSymbol == "[" || sSurroundSymbol == "]"
SetBufSelText(hBuf, cat("[", cat(sSelection, "]")))
else if sSurroundSymbol == "{" || sSurroundSymbol == "}"
SetBufSelText(hBuf, cat("{", cat(sSelection, "}")))
else if sSurroundSymbol == "<" || sSurroundSymbol == ">"
SetBufSelText(hBuf, cat("<", cat(sSelection, ">")))
else
SetBufSelText(hBuf, cat(sSurroundSymbol, cat(sSelection, sSurroundSymbol)))
sSurroundSymbol = ""
}
}
}
}
/*-------------------------------------------------------------------------
Return the FIRST different part from string sA, sB must be a substring
of sA.
for exampele:
sA = "Hello world!"
sB = "world"
__str_subtract will return: "Hello "
-------------------------------------------------------------------------*/
macro __str_subtract(sA, sB)
{
var iLenA; iLenA = strlen(sA)
var iLenB; iLenB = strlen(sB)
var iLim; iLim = iLenA - iLenB
var iCh; iCh = 0
if iLenA <= iLenB // sB must be a substing of sA
return ""
while iCh <= iLim
{
i = 0
while(sA[iCh+i] == sB[i])
{
cA = sA[iCh+i]
cB = sB[i]
if (i < iLenB)
i++
else
return strmid(sA, 0, iCh)
}
iCh++
}
}
/*-------------------------------------------------------------------------
Padding space at the end of the string to length iLen
-------------------------------------------------------------------------*/
macro __str_padding(sLine, iLen)
{
var iLenLine; iLenLine = strlen(sLine)
var SPACE; SPACE = CharFromAscii(32)
var iLenDiff
iLenDiff = iLenLine - iLen
if iLenDiff >= 0
return sLine
while iLenDiff < 0
{
sLine = cat(sLine, SPACE)
iLenDiff++
}
return sLine
}
/*-------------------------------------------------------------------------
Repeat strings and return the result
-------------------------------------------------------------------------*/
macro __str_rep(sString, iRepeatTimes)
{
var iIndex; iIndex = 0
var sRet; sRet = ""
while (iIndex++ < iRepeatTimes)
{
sRet = sRet # sString
}
return sRet
}
/*-------------------------------------------------------------------------
If string contains substring, return True, else False
-------------------------------------------------------------------------*/
macro __str_contain(sStr, sSubStr)
{
var iStrLen; iStrLen = strlen(sStr)
var iSubStrLen; iSubStrLen = strlen(sSubStr)
var iChar; iChar = 0
var iChStrLim; iChStrLim = iStrLen - iSubStrLen
// every string contains a trailing ""
if(iSubStrLen == 0)
return True
while (iChar <= iChStrLim)
{
if (iSubStrLen != 1)
{
if (strmid(sStr, iChar, iChar+iSubStrLen) == sSubStr)
return True
}
else
{
// this will improve the performance dramatically!
if (sStr[iChar] == sSubStr)
return True
}
iChar++
}
return False
}
/*-------------------------------------------------------------------------
If string only contains chars which in substring, return True, else False
-------------------------------------------------------------------------*/
macro __str_only_contain(sStr, sSubStr)
{
var iStrLen; iStrLen = strlen(sStr)
var iSubStrLen; iSubStrLen = strlen(sSubStr)
var iChStr; iChStr = 0
var iChSubStr; iChSubStr = 0
var cCharInStr
while (iChStr <= iStrLen)
{
cCharInStr = sStr[iChStr++]
if !(__str_contain(sSubStr, cCharInStr))
return False
}
return True
}
/*-------------------------------------------------------------------------
If string begins with substring, then return true.
the prefix spaces/tabs are ignored.
-------------------------------------------------------------------------*/
macro __str_begin_with(sStr, sSubStr)
{
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
sStr = __str_lstrip(sStr, TAB # SPACE)
sSubStr = __str_lstrip(sSubStr, TAB # SPACE)
var iStrLen; iStrLen = strlen(sStr)
var iSubStrLen; iSubStrLen = strlen(sSubStr)
if iSubStrLen > iStrLen
return False
if (strmid(sStr, 0, iSubStrLen) == sSubStr)
return True
else
return False
}
/*-------------------------------------------------------------------------
If string ends with substring, then return true.
the suffix spaces/tabs are ignored.
-------------------------------------------------------------------------*/
macro __str_end_with(sStr, sSubStr)
{
var TAB; TAB = CharFromAscii(9)
var SPACE; SPACE = CharFromAscii(32)
sStr = __str_rstrip(sStr, TAB # SPACE)
sSubStr = __str_rstrip(sSubStr, TAB # SPACE)
var iStrLen; iStrLen = strlen(sStr)