forked from jdeast/EXOFASTv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexofast_textoidl.pro
1417 lines (1316 loc) · 52.3 KB
/
exofast_textoidl.pro
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
;+
; NAME:
; TEXTOIDL_STRTRANS
; PURPOSE:
; Translate all occurences of one substring to another.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; new = textoidl_strtrans(oldstr,from,to,ned)
; INPUTS:
; oldstr -- string on which to operate. in
; May be an array.
; from -- substrings to be translated. May be in
; an array.
; to -- what strings in from should be in
; translated to. May be an array.
; KEYWORD PARAMETERS:
; /HELP -- Set this to print useful message and
; exit.
; OUTPUTS:
; new -- Translated string. Array if oldstr is out
; an array.
; ned -- number of substitutions performed in out
; oldstr. Array if oldstr is an array.
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; - Any of old, from, and to can be arrays.
; - from and to must have the same number of elements.
; EXAMPLE:
; inp='Many*bad!chars+in_here'
; from=['*','!','+','_']
; to =[' ',' ',' ',' ']
; out = textoidl_strtrans(inp,from,to,ned)
; Will produce out='Many bad chars in here', and set ned to 4.
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
;
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.7 2000/11/19 18:25:00 dfink / Jonathan Swift
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.3 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.2 1996/05/09 00:22:17 mcraig
; Sped up significantly by using str_sep to handle the translation. No longer
; relies on routines fromother user libraries.
;
; Revision 1.1 1996/01/31 18:47:37 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
;
FUNCTION textoidl_strtrans, InputString, from, to, ned, $
HELP=Help
; Bomb out to caller if error.
On_error, 2
; Offer help if we don't have at least InputString, from, and to, or
; if the user asks for it.
IF (n_params() LT 3) OR keyword_set(help) THEN BEGIN
offset = ' '
print, offset+'Translate all occurences of one substring to another.'
print, offset+'new = textoidl_strtrans(oldstr,from,to,ned)'
print, offset+'Inputs:'
print, offset+offset+'oldstr -- string on which to operate. in'
print, offset+offset+' May be an array.'
print, offset+offset+'from -- substrings to be translated. May be in'
print, offset+offset+' an array.'
print, offset+offset+'to -- what strings in from should be in'
print, offset+offset+' translated to. May be an array.'
print, offset+'Outputs:'
print, offset+offset+'new -- Translated string. Array if oldstr is out'
print, offset+offset+' an array.'
print, offset+offset+'ned -- number of substitutions performed in out'
print, offset+offset+' oldstr. Array if oldstr is an array.'
print, offset+'Notes:'
print, offset+offset+'- Any of old, from, and to can be arrays. '
print, offset+offset+'- from and to must have the same number of elements.'
return, -1
ENDIF
strn = InputString
; Check that From/To have same number of elements. RETURN if they don't.
NFrom = n_elements(from)
NTo = n_elements(to)
IF (NFrom EQ 0) OR (NTo EQ 0) THEN return, strn
IF NFrom NE NTo THEN BEGIN
print,'Error: Number of elements in from/to unequal'
return,-1
ENDIF
; Make sure there are no null strings in From. RETURN if there are.
FromLen = strlen(From)
IF (total(FromLen EQ 0) GT 0) THEN BEGIN
print, 'Error: elements of From must have nonzero length.'
return, -1
ENDIF
NStrings = n_elements(strn)
ned = lonarr(NStrings)
tmpned = 0L
; Say strn='a#b#c', from='#' and to='@'. Then the approach here is to
; first split strn at all occurances of '#', then recombine the pieces
; with '@' inserted instead. Do this for all elements of strn, and
; all elements of from.
FOR i = 0L, NStrings-1 DO BEGIN
ned[i] = 0L
FOR j=0L, NFrom-1 DO BEGIN
;; swapped obsolete str_sep with strsplit. JDE 2022-02-23
;; isn't this whole loop just repstr?
SepStr = str_sep(strn[i], from[j])
;; this breaks in GDL when the delimiter is "\'"
;; reverted 2022-05-23
; SepStr = strsplit(strn[i], from[j],/extract,/regex)
NSubs = n_elements(SepStr) - 1
strn[i] = SepStr[0]
FOR k=1L, NSubs DO strn[i] = strn[i] + To[j] + SepStr[k]
ned[i] = ned[i] + NSubs
ENDFOR
ENDFOR
return, strn
END
;
;+
; NAME:
; TEXTOIDL_TABLE
; PURPOSE:
; Returns a translation table from TeX to IDL.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; table = textoidl_table()
; INPUTS:
; None
; KEYWORD PARAMETERS:
; /POSTSCRIPT -- If set, return postscript translation
; table rather than vector fonts table.
; Default is translations for vector
; fonts.
; /HELP -- Print help and exit.
; OUTPUTS:
; table -- a 2D text array. table(0,*) contains out
; the words to be translated away, table(1,*)
; contains the words to translate them to.
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; To find out what TeX sequences are available, look at
; table(0,*).
; EXAMPLE:
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
;
; Revision 1.81 2018/03/08, eastman
; Rename to EXOFAST_TEXTOIDL, Remove \AA option, which is broken
; in IDL 8.7.
;
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.7 1996/07/22 23:56:08 mcraig
; Added \vartheta.
;
; Revision 1.6 1996/07/12 21:31:42 mcraig
; Fixed \varphi in vector font, added \circ.
;
; Revision 1.5 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.4 1996/05/09 00:22:17 mcraig
; Added command to return to previous font after switching to Greek or
; symbol font.
;
; Revision 1.3 1996/02/08 19:49:35 mcraig
; Removed control sequence \perp because the postscript code for it is '^'.
;
; Revision 1.2 1996/02/08 18:53:38 mcraig
; Added translations for PostScript fonts, and added several new TeX
; control sequences.
;
; Revision 1.1 1996/01/31 18:47:37 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
;
FUNCTION textoidl_table, POSTSCRIPT=ps, VECTOR=vec, HELP=Help
; Return to caller if error.
On_error, 2
; Print help if necessary.
IF keyword_set(Help) THEN BEGIN
offset = ' '
print, offset+'Returns a translation table from TeX to IDL.'
print, offset+'table = textoidl_table()'
print, offset+'Keywords:'
print, offset+offset+'/POSTSCRIPT -- If set, return postscript translation'
print, offset+offset+' table rather than vector fonts table.'
print, offset+offset+' Default is translations for vector'
print, offset+offset+' fonts.'
print, offset+offset+'/HELP -- Print help and exit.'
print, offset+'Outputs:'
print, offset+offset+'table -- a 2D text array. table(0,*) contains out'
print, offset+offset+' the words to be translated away, table(1,*)'
print, offset+offset+' contains the words to translate them to.'
print, offset+'Notes:'
print, offset+offset+'To find out what TeX sequences are available, look at'
print, offset+offset+'table(0,*).'
ENDIF
VECFONT=1 ; index of vector font in translation table
PSFONT=2 ; index of postscript font in trans table
IF keyword_set(ps) THEN FontSelection=PSFONT ELSE FontSelection=VECFONT
; Set IDL font sequence needed to switch to Greek letters.
GreekFont = strarr(3)
GreekFont(VECFONT) = '!7'
GreekFont(PSFONT) = '!M'
; Set IDL font sequence needed to switch to special symbol font.
SymbolFont = strarr(3)
SymbolFont(VECFONT) = '!M'
SymbolFont(PSFONT) = '!M'
; Set IDL font sequence needed to switch back to initial font.
PreviousFont = strarr(3)
PreviousFont(VECFONT) = '!X'
PreviousFont(PSFONT) = '!X'
; Set IDL font sequence for roman fonts.
RomanFont = strarr(3)
RomanFont(VECFONT) = '!3'
RomanFont(PSFONT) = '!M'
;lowercase Greek --
; Note there is some trickery involved in getting \varphi
; to work in the vector fonts, because it is actually
; a member of the symbol font set, not the Greek font
; set. Go figure. Solution is just to make the vector
; character a switch to symbol, the proper character from
; that font, and a switch back out of symbol. Same comment holds
; for \vartheta.
;; TeX SEQUENCE VECTOR POSTSCRIPT
LowercaseGreek = [$
[ '\alpha', 'a' , 'a' ],$
[ '\beta', 'b' , 'b' ],$
[ '\gamma', 'c' , 'g' ],$
[ '\delta', 'd' , 'd' ],$
[ '\epsilon', 'e' , 'e' ],$
[ '\zeta', 'f' , 'z' ],$
[ '\eta', 'g' , 'h' ],$
[ '\theta', 'h' , 'q' ],$
[ '\iota', 'i' , 'i' ],$
[ '\kappa', 'j' , 'k' ],$
[ '\lambda', 'k' , 'l' ],$
[ '\mu', 'l' , 'm' ],$
[ '\nu', 'm' , 'n' ],$
[ '\xi', 'n' , '!S !Rx' ],$
[ '\pi', 'p' , 'p' ],$
[ '\rho', 'q' , 'r' ],$
[ '\sigma', 'r' , 's' ],$
[ '\tau', 's' , 't' ],$
[ '\upsilon', 't' , 'u' ],$
[ '\phi', 'u' , 'f' ],$
[ '\chi', 'v' , 'c' ],$
[ '\psi', 'w' , 'y' ],$
[ '\omega', 'x' , 'w' ],$
[ '\varpi', 'p' , 'v' ],$
[ '\varepsilon', 'e' , 'e' ],$
[ '\varphi', $
SymbolFont(VECFONT)+'P'+PreviousFont(VECFONT) $
, 'j' ],$
[ '\vartheta', $
SymbolFont(VECFONT)+'t'+PreviousFont(VECFONT) $
, 'J' ]$
]
;Uppercase Greek --
;; TeX SEQUENCE VECTOR POSTSCRIPT
UppercaseGreek = [$
[ '\Gamma', 'C' , 'G' ],$
[ '\Delta', 'D' , 'D' ],$
[ '\Theta', 'H' , 'Q' ],$
[ '\Lambda', 'K' , 'L' ],$
[ '\Xi', 'N' , '!S !RX' ],$
[ '\Pi', 'P' , 'P' ],$
[ '\Sigma', 'R' , 'S' ],$
[ '\Upsilon', 'T' , string(byte(161)) ],$
[ '\Phi', 'U' , 'F' ],$
[ '\Psi', 'W' , 'Y' ],$
[ '\Omega', 'X' , 'W' ]$
]
;Special symbols --
; NOTES -- You must leave \infty before \in in the translatation
; table to avoid having the \in part of \infty translated
; away.
;
; DO NOT blindly add the control sequence \perp. Its
; PostScript code is '^', which leads to thing being
; interpreted as superscripts which shouldn't be.
;
;; TeX SEQUENCE VECTOR POSTSCRIPT
Symbols = [$
[ '\aleph', '@' , string(byte(192)) ],$
[ '\ast', '*' , '*' ],$
[ '\cap', '3' , string(byte(199)) ],$
[ '\cdot', '.' , string(byte(215)) ],$
[ '\odot', 'n' , '!SO!R!N!I ' + string(183b) + '!X' ],$
[ '\cup', '1' , string(byte(200)) ],$
[ '\exists', 'E' , '$' ],$
[ '\infty', '$' , string(byte(165)) ],$
[ '\in', 'e' , string(byte(206)) ],$
[ '\equiv', ':' , string(byte(186)) ],$
[ '\pm', '+' , string(byte(177)) ],$
[ '\div', '/' , string(byte(184)) ],$
[ '\subset', '0' , string(byte(204)) ],$
[ '\superset', '2' , string(byte(201)) ],$
[ '\leftarrow', '4' , string(byte(172)) ],$
[ '\downarrow', '5' , string(byte(175)) ],$
[ '\rightarrow', '6' , string(byte(174)) ],$
[ '\uparrow', '7' , string(byte(173)) ],$
[ '\neq', '=' , string(byte(185)) ],$
[ '\propto', '?' , string(byte(181)) ],$
[ '\sim', 'A' , string(byte(126)) ],$
[ '\partial', 'D' , string(byte(182)) ],$
[ '\nabla', 'G' , string(byte(209)) ],$
[ '\angle', 'a' , string(byte(208)) ],$
[ '\times', 'X' , string(byte(180)) ],$
[ '\geq', 'b' , string(byte(179)) ],$
[ '\leq', 'l' , string(byte(163)) ],$
[ "\'", "'" , string(byte(162)) ],$
[ '\prime', "'" , string(byte(162)) ],$
[ '\circ', "%" , string(byte(176)) ]$
]
LowercaseGreek(1,*) = $
GreekFont(FontSelection) $
+ LowercaseGreek(FontSelection,*) $
+ PreviousFont(FontSelection)
UppercaseGreek(1,*) = $
GreekFont(FontSelection) +$
UppercaseGreek(FontSelection,*) $
+ PreviousFont(FontSelection)
Symbols(1,*) = $
SymbolFont(FontSelection) $
+ Symbols(FontSelection,*) $
+ PreviousFont(FontSelection)
TranslationTable = [[LowercaseGreek],[UppercaseGreek],[Symbols]]
return,TranslationTable(0:1,*)
END
;
;+
; NAME:
; TEXTOIDL_STRTOK
; PURPOSE:
; Retrieve portion of string up to token.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; new = textoidl_strtok( old, token )
; INPUTS:
; old -- String to be split. Contains text after in, out
; token on output.
; token -- Token to use in splitting old. in
; KEYWORD PARAMETERS:
; /TRIM -- set to remove leading blanks from old
; before returning.
; /HELP -- print useful message and exit.
; OUTPUTS:
; new -- portion of string up to token. out
; old -- portion of old after token. out, in
; COMMON BLOCKS:
; SIDE EFFECTS:
; Input parameter old is modified.
; NOTES:
; Token may be one or more characters.
; If token is not found, returns old and sets old to ''.
; EXAMPLE:
; If old is 'foo44 bar', then textoidl_strtok( old, '44' ) would return
; 'foo', and upon return, old will be left with ' bar'. If /TRIM
; were set, old would be 'bar' on return.
;
; If old='xyz', then new=textoidl_strtok(old,'a') would return with
; new='xyz' and old=''.
; THANKS:
; To D. Linder who wrote GETTOK, part of the goddard library,
; upon which this is based.
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.3 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.2 1996/05/09 00:22:17 mcraig
; Added built in help.
;
; Revision 1.1 1996/01/31 18:47:37 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
FUNCTION Textoidl_Strtok, string, token, $
TRIM=trim, HELP=Help
; Back to the caller if error occurs.
On_error, 2
IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
offset = ' '
print, offset+'Retrieve portion of string up to token.'
print, offset+'new = textoidl_strtok( old, token )'
print, offset+'Inputs:'
print, offset+offset+'old -- String to be split. Contains text after in, out'
print, offset+offset+' token on output.'
print, offset+offset+'token -- Token to use in splitting old. in'
print, offset+'Keywords:'
print, offset+offset+'/TRIM -- set to remove leading blanks from old '
print, offset+offset+' before returning.'
print, offset+offset+'/HELP -- print useful message and exit.'
print, offset+'Outputs:'
print, offset+offset+'new -- portion of string up to token. out'
print, offset+offset+'old -- portion of old after token. out, in'
print, offset+'Side effects:'
print, offset+offset+'Input parameter old is modified.'
print, offset+'Notes:'
print, offset+offset+'Token may be one or more characters.'
print, offset+offset+"If token is not found, returns old and sets old to ''."
print, offset+'Examples:'
print, offset+offset+"If old is 'foo44 bar', then textoidl_strtok( old, '44' ) would return'"
print, offset+offset+" 'foo', and upon return, old will be left with ' bar'. If /TRIM"
print, offset+offset+" were set, old would be 'bar' on return."
;'
print, offset+offset+"If old='xyz', then new=textoidl_strtok(old,'a') would return with"
print, offset+offset+" new='xyz' and old=''."
return, -1
ENDIF
pos = strpos(string, token)
IF (pos GE 0) THEN BEGIN
front = strmid(string, 0, pos)
string = strmid(string, pos + strlen(token), strlen(string))
IF keyword_set(trim) THEN string = strtrim(string, 1)
return, front
ENDIF
front = string
string = ''
return, front
END
;
;+
; NAME:
; TEXTOIDL_NEXTTOK
; PURPOSE:
; Find the next occurance of any of a set of characters in a
; string and return the character which occurs next.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; tok = textoidl_nexttok( strn, tokens )
; INPUTS:
; strn -- string to be searched for sub/superscripts in
; tokens -- string containing characters to be found. in
; KEYWORD PARAMETERS:
; POSITION -- Set to a named variable to get position out
; of next token, or -1 if none found.
; /HELP -- Print useful message and exit.
; OUTPUTS:
; tok -- Contains the character among tokens which out
; occurs next in strn, or null '' if none found.
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; EXAMPLE:
; textoidl_nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets
; pos to 1.
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.3 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.2 1996/05/09 00:22:17 mcraig
; Generalized so that the next occurence of any of a set of characters will
; be returned.
;
; Revision 1.1 1996/01/31 18:41:06 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
FUNCTION textoidl_nexttok, strn, tokens, $
POSITION=position, $
HELP=Help
; Return to caller on error.
On_error, 2
; Help those in need of it.
IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
offset = ' '
print, offset+'Find the next occurance of any of a set of characters in a'
print, offset+'string and return the character which occurs next.'
; CALLING SEQUENCE:
print, offset+'tok = textoidl_nexttok( strn, tokens )'
; INPUTS:
print, offset+'Inputs:'
print, offset+offset+'strn -- string to be searched for sub/superscripts in'
print, offset+offset+'tokens -- string containing characters to be found. in'
; KEYWORD PARAMETERS:
print, offset+'Keywords:'
print, offset+offset+'POSITION -- Set to a named variable to get position out'
print, offset+offset+' of next token, or -1 if none found.'
print, offset+offset+'/HELP -- Print useful message and exit.'
; OUTPUTS:
print, offset+'Outputs:'
print, offset+offset+'tok -- Contains the character among tokens which out'
print, offset+offset+" occurs next in strn, or null '' if none found."
; EXAMPLE:
print, offset+'Example:'
print, offset+offset+"textoidl_nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets"
print, offset+offset+'pos to 1.'
return, ''
ENDIF
TmpStr = byte(strn)
TmpTok = byte(tokens)
NumToks = n_elements(TmpTok)
MatchIdx = 0L
Matches = 0L
FOR j=0, NumToks-1 DO BEGIN
TmpMatch = where(TmpStr EQ TmpTok(j), TmpCnt)
IF (TmpCnt GT 0) THEN BEGIN
MatchIdx = [MatchIdx, Replicate(j, TmpCnt)]
Matches = [Matches, TmpMatch]
ENDIF
ENDFOR
IF n_elements(MatchIdx) EQ 1 THEN BEGIN
Position = -1
return, ''
ENDIF
MatchIdx = MatchIdx(1:*)
Matches = Matches(1:*)
SortInd = sort(Matches)
Position = Matches(SortInd(0))
Tok = string(TmpTok(MatchIdx(SortInd(0))))
return, Tok
END
;
;+
; NAME:
; TEXTOIDL_MATCHDELIM
; PURPOSE:
; Match open/close delimiters in a string.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; position = textoidl_matchdelim( strn, [openpos])
; INPUTS:
; strn -- a string containing an open in
; delimiter (e.g. '{') in which you
; want to find the matching closing
; delimiter (e.g. '}')
; KEYWORD PARAMETERS:
; OPEN_DELIM -- A single character containing the opening in
; delimiter (e.g. '('). Default is '{'
; CLOSE_DELIM -- A single character containing the closing in
; delimiter (e.g. ')'). Default is '}'
; OUTPUTS:
; position -- returns the position in strn of the out
; closing delimiter, -1 if no closing found.
; openpos -- Set to a named variable to receive the out
; position of the first opening delimiter.
; Optional.
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; - Any pair of (nonidentical) characters can be used as
; delimiters.
; EXAMPLE:
; textoidl_matchdelim('{one{two}}three') returns 9, the character just
; before 'three'.
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.3 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.2 1996/05/09 00:22:17 mcraig
; Removed restriction that open delim must be first char. Added argument
; to allow for return of position of open delim.
;
; Revision 1.1 1996/01/31 18:41:06 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
;
FUNCTION Textoidl_Matchdelim, InString, OpenPos, $
OPEN_DELIM=OpenDelim, $
CLOSE_DELIM=CloseDelim, $
HELP=Help
; Return to caller if error.
On_error, 2
IF (n_params() LT 1) OR keyword_set(Help) THEN BEGIN
offset = ' '
print, offset+'Match open/close delimiters in a string.'
print, offset+'position = textoidl_matchdelim( strn, [openpos])'
print, offset+'Inputs:'
print, offset+offset+'strn -- a string containing an open in'
print, offset+offset+" delimiter (e.g. '{') in which you "
print, offset+offset+' want to find the matching closing '
print, offset+offset+" delimiter (e.g. '}')"
print, offset+'Keywords:'
print, offset+offset+'OPEN_DELIM -- A single character containing the opening in'
print, offset+offset+" delimiter (e.g. '('). Default is '{'"
print, offset+offset+'CLOSE_DELIM -- A single character containing the closing in'
print, offset+offset+" delimiter (e.g. ')'). Default is '}'"
print, offset+'Outputs:'
print, offset+offset+'position -- returns the position in strn of the out'
print, offset+offset+' closing delimiter, -1 if no closing found.'
print, offset+offset+'openpos -- Set to a named variable to receive the out'
print, offset+offset+' position of the first opening delimiter.'
print, offset+offset+' Optional.'
print, offset+'Example:'
print, offset+offset+"textoidl_matchdelim('a{one{two}}three') returns 10, the character just"
print, offset+offset+" before 'three'. "
print, offset+offset+$
"a=textoidl_matchdelim('aaa[bbb(ccc)]ddd[eee]',f,OP='[',CL=']')"
print, offset+offset+" returns a=12 (just before ddd), f=3 "+$
"(just before bbb)."
return, -1
ENDIF
; Set default delimiters.
IF n_elements(OpenDelim) EQ 0 THEN OpenDelim = '{'
IF n_elements(CloseDelim) EQ 0 THEN CloseDelim = '}'
; Make sure InString has more than 1 character.
length = strlen(InString)
IF (length LE 1) THEN return,-1
; Return if no open delimiter
OpenPos = strpos( InString, OpenDelim )
IF (OpenPos EQ -1) THEN BEGIN
print, 'Error: No opening delimiter'
return, -1
ENDIF
; Convert strings to array of integers to speed processing.
OpenDelim = fix((byte(OpenDelim))(0))
CloseDelim = fix((byte(CloseDelim))(0))
TmpStr = fix(byte(strmid( InString, OpenPos, length)))
; Leave the -1* in here. This forces conversion from BYTE to INTEGER,
; necessary because there are no negative BYTEs.
TmpStr = (TmpStr EQ OpenDelim) $
-1*(TmpStr EQ CloseDelim)
length = n_elements(TmpStr)
; Initialize count of number of delimiters. We've found one, the
; first opener.
BraceCnt = 1
i=0
WHILE (BraceCnt GT 0) AND (i LT length-1) DO BEGIN
i = i+1
BraceCnt = BraceCnt + TmpStr(i)
ENDWHILE
i = i + OpenPos
IF (BraceCnt GT 0) THEN i = -1
return, i
END
;
;+
; NAME:
; TEXTOIDL_STRCNT
; PURPOSE:
; Count number of occurrences of a substring in a string.
; CATEGORY:
; text/strings
; CALLING SEQUENCE:
; num = textoidl_strcnt(strn, substring, [pos])
; INPUTS:
; string -- The string in which to count occurences. in
; substring -- The substring to count occurrences of. in
; pos -- the position at which to begin the search. [in]
; If not supplied, start at beginning of
; string.
; KEYWORD PARAMETERS:
; /HELP -- Print useful message and return.
; OUTPUTS:
; num -- Number of occurances of substring in string. out
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; Overlapping occurances are not counted separately. For
; example, counting occurances of 'bb' in 'blah bbb' returns one
; occurance.
; EXAMPLE:
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.3 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.2 1996/05/09 00:22:17 mcraig
; Added fast processing using BYTE arrays if we are counting occurences of
; a single character. Added error handling.
;
; Revision 1.1 1996/01/31 18:47:37 mcraig
; Initial revision
;
; RELEASE:
; $Name: not supported by cvs2svn $
;
; COPYRIGHT:
; Copyright (C) 1996 The Regents of the University of California, All
; Rights Reserved. Written by Matthew W. Craig.
; See the file COPYRIGHT for restrictions on distrubting this code.
; This code comes with absolutely NO warranty; see DISCLAIMER for details.
;-
FUNCTION Textoidl_Strcnt, strn, substrn, startpos, $
HELP=Help
; Return to caller if error.
On_error, 2
; Help user, if needed.
IF (n_params() LT 2) OR keyword_set(Help) THEN BEGIN
offset = ' '
print, offset+'Count number of occurrences of a substring in a string.'
print, offset+'num = textoidl_strcnt(strn, substring, [pos])'
print, offset+'Inputs:'
print,offset+offset+'string -- The string in which to count occurences. in'
print,offset+offset+'substring -- The substring to count occurrences of. in'
print,offset+offset+'pos -- the position at which to begin the search. [in]'
print,offset+offset+' If not supplied, start at beginning of'
print,offset+offset+' string.'
print, offset+'Keywords:'
print,offset+offset+'/HELP -- Print useful message and return.'
print, offset+'Outputs:'
print,offset+offset+'num -- Number of occurances of substring in string. out'
return, -1
ENDIF
IF n_params() EQ 2 THEN startpos = 0
; return if we weren't really given a substring to search for. . .
IF strlen(substrn) EQ 0 THEN BEGIN
print, "Error: Can't count occurances of null string."
return, -1
ENDIF
; . . .or if we were told to start at the end of the string.
tmpstrn = strmid(strn, startpos, strlen(strn))
IF strlen(tmpstrn) EQ 0 THEN return, 0
; If looking for occurences of single character, process using BYTE
; array.
IF strlen(substrn) EQ 1 THEN BEGIN
tmpstrn = byte(TmpStrn)
count = n_elements(where(TmpStrn EQ (byte(substrn))(0)))
ENDIF ELSE BEGIN
count = 0L
pos = strpos(tmpstrn, substrn, /reverse_search)
WHILE pos GE 0 DO BEGIN
count = count + 1
pos = strpos(tmpstrn, substrn, pos, /reverse_search)
ENDWHILE
ENDELSE
return, count
END
; NOTE to future maintainers:
; Make sure textoidl_sub_sup stays before textoidl_trans_sub_super. At least
; for now, when IDL encounters a function and automatically compiles
; it, it only compiles the functions in the file up to the named
; function. So even if textoidl_sub_sup was declared with
; FORWARD_FUNCTION in textoidl_trans_sub_super, it would not properly
; compile.
;
;+
; SPECIAL NOTE:
; The file textoidl_trans_sub_super.pro contains two functions,
; textoidl_trans_sub_super, and textoidl_sub_sup. The former is the
; generic routine for processing TeX sub/superscripts, the
; latter is used only by textoidl_trans_sub_super and has no general
; utility. Hence it lives here. You will see documentation for
; textoidl_trans_sub_super second if you use DOC_LIBRARY.
;-
;
;
;+
; NAME:
; TEXTOIDL_SUB_SUP
; PURPOSE:
; Return the proper IDL font positioning command for TeX
; sub/superscripts.
; CATEGORY:
; TeXtoIDL
; CALLING SEQUENCE:
; fnt = textoidl_sub_sup( strn )
; INPUTS:
; strn -- Either '^' or '_', the TeX super/subscript in
; characters
; KEYWORD PARAMETERS:
; /FORCE_UD -- Set this to use !U/!D instead of !E/!I for
; sub/superscripts .
; /HELP -- Set to print useful message and exit.
; OUTPUTS:
; fnt -- Either '!U' or !E' for superscripts, out
; or '!D' or '!I' for subscripts.
; COMMON BLOCKS:
; SIDE EFFECTS:
; NOTES:
; Used only by textoidl_trans_sub_super. Should be kept in same
; file.
; EXAMPLE:
; MODIFICATION HISTORY:
; $Id: textoidl.pro,v 1.9 2007-08-28 17:04:45 blanton Exp $
; $Log: not supported by cvs2svn $
; Revision 1.8 2004/07/19 16:48:04 schlegel
; Replace calls to the obsolete function RSTRPOS() with STRPOS(/REVERSE_SEARCH).
;
; Revision 1.7 2000/11/20 02:27:24 dfink
; Added \AA option for Anstroms
;
; Revision 1.6 2000/07/12 14:31:33 hogg
; fixed another ()/[] bug.
;
; Revision 1.5 2000/06/15 18:21:23 hogg
; fixed tiny () -> [] bugs
;
; Revision 1.4 2000/06/03 17:12:42 dfink
; Fink's new textoidl - all procedures in one file; avoid name conflict
;
; Revision 1.4 1996/06/14 20:00:27 mcraig
; Updated Copyright info.
;
; Revision 1.3 1996/05/09 00:22:17 mcraig