-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormatted references_02.tm
More file actions
1256 lines (894 loc) · 42.2 KB
/
Copy pathFormatted references_02.tm
File metadata and controls
1256 lines (894 loc) · 42.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<TeXmacs|1.99.9>
<style|generic>
<\body>
<\hide-preamble>
\;
<assign|the-example|<macro|<compound|display-example|<value|<compound|counter-example>>>>>
</hide-preamble>
<assign|formatRef|<macro|str|<extern|formatRefScheme|<arg|str>><reference|<arg|str>>>>
<assign|addToFormatRef|<macro|formatCode|formatString|<extern|addToFormatListScheme|<arg|formatCode>|<arg|formatString>>>>
<assign|deleteFromFormatRef|<macro|formatCode|n|<extern|deleteFromFormatListScheme|<arg|formatCode>|<arg|n>>>>
<assign|replaceInFormatRef|<macro|formatCode|formatString|n|<extern|replaceInFormatListScheme|<arg|formatCode>|<arg|formatString>|<arg|n>>>>
<doc-data|<doc-title|Formatted references>|<\doc-subtitle>
A set of Scheme functions for formatting references
in a <TeXmacs> document - v 0.2
</doc-subtitle>>
<section|Description>
This program is inspired by the <LaTeX> package <verbatim|prettyref>.
It provides the macro <verbatim|formatRef><marginal-note|normal|c|<small|Are
formatRef and formatList the names we want?>>
(<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|lab>\<gtr\>>>) which formats a reference choosing in a list of formats
saved in the global variable <verbatim|formatList>. Each format is a list
consisting of two members; the first member is a format code, which is a
short string, whicle the second member is the corresponding format to apply
to the reference. The function selects the format by comparing the format
code with the beginning of the label: the first element of
<verbatim|formatList> that matches is selected and the corresponding format
string <verbatim|formatString> is prepended to the TeXmacs reference, so
that the reference obtained with the macro
<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|lab>\<gtr\>>> looks like\
<verbatim|formatString><with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|lab>\<gtr\>>>
The labels must be assigned according to the format codes so that the
macros have an effect. If no format code matches, the macro returns
<with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|lab>\<gtr\>>>
<section|<TeXmacs> macros and their use>
I have defined the <TeXmacs> macros in the preamble of this document
<paragraph|<with|color|blue|formatRef>>
<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|lab<with|color|blue|_string>>\<gtr\>>>
Formats a reference so that it looks like
<verbatim|formatString> <with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|lab>\<gtr\>>>
where <verbatim|formatString> is the format string of the first matching
format (empty string if there is no match).
<paragraph|<with|color|blue|addToFormatRef>>
<with|color|blue|<verbatim|\<less\><with|color|blue|addToFormatRef>\|<with|color|dark
blue|formatCode<with|color|blue|_string>>\|<with|color|dark
blue|formatString<with|color|blue|_string>>\<gtr\>>>
Adds to the list of formats <verbatim|formatList> (which is a global Scheme
variable) the new format which is composed of the strings
<verbatim|<with|color|dark blue|formatCode>> and <verbatim|<with|color|dark
blue|formatString>>.
<strong|Example:>
<with|color|blue|<verbatim|\<less\><with|color|blue|addToFormatRef>\|fig:\|Figure
\<gtr\>>> makes the new format <verbatim|("fig:" "Figure")> available, so
that the application of the macro
<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|fig:figure1>\<gtr\>>>
returns
Figure <with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|<with|color|dark blue|fig:figure1>>\<gtr\>>>
<paragraph|<with|color|blue|deleteFromFormatRef>>
<with|color|blue|<verbatim|\<less\><with|color|blue|deleteFromFormatRef>\|<with|color|dark
blue|formatCode<with|color|blue|_string>>\|<with|color|dark
blue|n<with|color|blue|_integer>>\<gtr\>>>
Deletes from <verbatim|formatList> the n-th format that matches
<verbatim|<with|color|dark blue|formatCode>>. If no format that matches
format code remains after deletion, then\
<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|lab>\<gtr\>>>
will return
<with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|lab>\<gtr\>>>
<strong|Example:>
<with|color|blue|<verbatim|\<less\><with|color|blue|<with|color|blue|deleteFromFormatRef>>\|fig:\|1\<gtr\>>>
deletes from <verbatim|formatList> the first occurrence of the format with
format code <verbatim|"fig:">
<paragraph|<with|color|blue|replaceInFormatRef>>
<with|color|blue|<verbatim|\<less\><with|color|blue|replaceInFormatRef>\|<with|color|dark
blue|formatCode<with|color|blue|_string>>\|<with|color|dark
blue|formatString<with|color|blue|_string>>\|<with|color|dark
blue|n<with|color|blue|_integer>>\<gtr\>>>
Replaces in <verbatim|formatList> the n-th format with matching
<verbatim|formatCode> with the format <verbatim|("formatCode"
"formatString")>.
<strong|Example:>
Let the initial value of <code*|formatList> be <code*|(list (list "eq:"
"eqn. ") (list "Sec:" "Section ") (list "eq:" "equation "))>
<with|color|blue|<verbatim|\<less\><with|color|blue|<with|color|blue|deleteFromFormatRef>>\|eq:\|Equation
\<gtr\>>>
transforms <verbatim|formatList> into\
<code*|(list (list "eq:" "eqn. ") (list "Sec:" "Section ") (list "eq:"
"Equation "))>
in this case the application of the macro\
<with|color|blue|<verbatim|\<less\><with|color|blue|formatRef>\|<with|color|dark
blue|eq:equation1>\<gtr\>>>
returns
eqn. <with|color|blue|<verbatim|\<less\><with|color|blue|reference>\|<with|color|dark
blue|<with|color|dark blue|eq:equation1>>\<gtr\>>>
like it did originally, as the matching format is the first one that
matches the format code.
<section|Scheme functions and their use>
<paragraph|<with|color|blue|formatList>>
Global variable, accessible from the whole document.\
A list of formats. Each element of a list is a list of two elements (both
strings). The first element is a \Pformat code\Q and is examined by the
program to match label and format; the second element is the string which
is applied to format the reference.
<paragraph|<with|color|blue|extractFormatString>>
<verbatim|(extractFormatString label_string \ formatList_list)>
Extracts from the list of formats <verbatim|formatList> the matching
format; returns the second member of the list that matches the format (that
is, the format string).
A format matches if its first element (the \Pformat code\Q) is equal to the
beginning of the <verbatim|label> string.
<strong|Example:>
<verbatim|(extractFormatString "fig:" (list (list "fig:" "Figure ") (list
"Sec:" "Section ")))>
returns <verbatim|"Figure ">
<paragraph|<with|color|blue|addToFormatList>>
<verbatim|(addToFormatList format_list)>
Adds to the list of formats <verbatim|formatList> (which is a global
variable) the new format <verbatim|format>.
Checks that <code*|format> is syntactically correct and that does not
already exists in <verbatim|formatList>.
\;
<strong|Example:>
Let the initial value of <code*|formatList> be <code*|(list (list "fig:"
"Figure ") (list "Sec:" "Section "))>
<verbatim|(addToFormatList (list "eq:" "Equation "))>
transforms <verbatim|formatList> into\
<code*|(list (list "fig:" "Figure ") (list "Sec:" "Section ") (list "eq:"
"Equation "))>
<paragraph|<with|color|blue|deleteFromFormatList>>
<verbatim|(deleteFromFormatList formatToDelete_list n_integer)>
Deletes from <verbatim|formatList> the n-th format that matches
<verbatim|formatToDelete>.
<verbatim|formatToDelete> must be a list<marginal-note|normal|c|<small|I
would like to rewrite the function so that is accepts either lists or
strings>>; it must not a syntactically correct format, as only its first
element will be matched to formats.
<strong|Example:>
Let the initial value of <code*|formatList> be <code*|(list (list "eq:"
"eqn. ") (list "Sec:" "Section ") (list "eq:" "equation "))>
<verbatim|(deleteFromFormatList (list "eq:") 2)>
transforms <verbatim|formatList> into\
<code*|(list (list "eq:" "eqn. ") (list "Sec:" "Section "))>
<paragraph|<with|color|blue|replaceFormat>>
<verbatim|(replaceFormat newFormat_list n_integer)>
Replaces in <verbatim|formatList> the n-th format that matches
<verbatim|newFormat> with <verbatim|newFormat> itself.
<verbatim|newFormat> must be a syntactically correct format. The match is
done checking the first element of the list <verbatim|newFormat> (like the
match done by <verbatim|extractFormatString>).
<strong|Example:>
Let the initial value of <code*|formatList> be <code*|(list (list "eq:"
"eqn. ") (list "Sec:" "Section ") (list "eq:" "equation "))>
<verbatim|(replaceFormat (list "eq:" "Equation ") 2)>
transforms <verbatim|formatList> into\
<code*|(list (list "eq:" "eqn. ") (list "Sec:" "Section ") (list "eq:"
"Equation "))>
\;
<section|Functions - code and detailed description>
<subsection|Global variables>
<\session|scheme|default>
<\textput>
<verbatim|formatList> is the list of all formats (for the moment empty)
</textput>
<\input|Scheme] >
(define formatList (list))
</input>
<\input|Scheme] >
\;
</input>
</session>
<subsection|Comparison to format code>
We compare the reference string <verbatim|str> to a format code
<verbatim|ref><verbatim|><marginal-note|normal|c|<small|<verbatim|ref> is a
bad name here as the string is is already called \Preference\Q>>. To do
this, we extract first from the string the intial substring of length equal
to the length of <verbatim|ref>; we then compare the extracted substring to
the format code.
<\session|scheme|default>
<\textput>
Check that <verbatim|str> is at least as long as <verbatim|ref> before
extracting the initial substring from <verbatim|str>.
</textput>
<\input|Scheme] >
(define (checkLength str ref)
\ \ (\<less\>= (string-length ref) (string-length str)))
</input>
<\input|Scheme] >
(define (extractBase str ref)
\ \ \ (substring str 0 (string-length ref)))
</input>
<\textput>
If <verbatim|str> is at least as long as <verbatim|ref>, extract the
substring; if not, return the empty string.
If the function returns the empty string, the format will not match, as
we define it not to match in the matching function.
</textput>
<\input|Scheme] >
(define (extract str ref)
\ \ \ (if (checkLength str ref)
\ \ \ \ \ \ \ (extractBase str ref)
\ \ \ \ \ \ \ \ ""))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsubsection|Comparison between string and format code>
<\session|scheme|default>
<\textput>
The matching function will return false if either <verbatim|str> or
<verbatim|ref> is the empty string (i.e., no format will be applied to
an empty reference and no format with empty format code will be applied
to a reference)
</textput>
<\input|Scheme] >
(define (checkLabelInputs str ref)
\ \ \ (and
\ \ \ \ (not (equal? str ""))
\ \ \ \ (not (equal? ref ""))))
</input>
<\textput>
Use the <verbatim|extract> function to compare <verbatim|str> and
<verbatim|ref>.
</textput>
<\input|Scheme] >
(define (compareSubstrBase str ref)
\ \ \ (equal? ref (extract str ref)))
</input>
<\textput>
Check first for empty <verbatim|ref> or <verbatim|str>, then compare.
</textput>
<\input|Scheme] >
(define (compareSubstr str ref)
\ \ \ (if (checkLabelInputs str ref)
\ \ \ \ \ \ \ (compareSubstrBase str ref)
\ \ \ \ \ \ \ \ #f))
</input>
<\textput>
<paragraph|Compare the string to the first element of a format >
The format is a list of two elements, the format code and the format
itself
</textput>
<\textput>
We want to match only on correctly formed formats.
Check that <verbatim|lst> is a list of length 2.
We probably need a comprehensive check that the format is correctly
formed, that is <verbatim|lst> is a list of length 2 and both elements
are strings.<marginal-note|normal|c|<small|The check on <verbatim|lst>
needs to be complete>>
<\note>
I already wrote the complete check in the functions the compose the
list; I need to apply it consistently
</note>
</textput>
<\input|Scheme] >
(define (listCond lst)
\ \ \ (if (not (list? lst))
\ \ \ \ \ \ \ \ #f
\ \ \ \ \ \ \ \ (= (length lst) 2)))
</input>
<\textput>
If <verbatim|ft> is correctly formed, compare <verbatim|str> to the
first element of <verbatim|ft>; otherwise return <verbatim|#f>
</textput>
<\input|Scheme] >
(define (compareSubstrFt str ft)
\ \ (if (listCond ft)
\ \ \ \ \ \ \ (compareSubstr str (list-ref ft 0))
\ \ \ \ \ \ \ #f))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsection|Extraction of format string>
We extract from the format list the first matching format and from the
format its format proper.<marginal-note|normal|c|<small|Find a good way of
referring to the \Pformat string\Q part of the format>>
<\session|scheme|default>
<\textput>
Filter format list <verbatim|labelFormats> according to the
<verbatim|compareSubstrFt> predicate. In a subsequent function, we will
select the first element of the filtered list; if the filtered list is
empty we will return an empty list.
</textput>
<\input|Scheme] >
(define (filterFormats str labelFormats)
\ \ (filter
\ \ \ (lambda (x) (compareSubstrFt str x))
\ \ \ \ \ \ labelFormats))
</input>
<\textput>
We select here the first element of the filtered list; if the filtered
list is empty we return an empty list (we need to do that in a separate
case, because <verbatim|car> does not work on an empty
list<marginal-note|normal|c|<small|Check again <verbatim|car> on an
empty list>>)
</textput>
<\input|Scheme] >
(define (extractFormat str labelFormats)
\ \ \ (let ((matchList (filterFormats str labelFormats)))
\ \ \ \ \ (if (null? matchList)
\ \ \ \ \ \ \ \ \ (list)
\ \ \ \ \ \ \ \ \ (car matchList))))
</input>
<\textput>
From the format, extract the format proper, which is the element in
second position; return an empty string if the format is an empty list
(i.e. we did not find any match for the reference string)
</textput>
<\input|Scheme] >
(define (extractFormatString str labelFormats)
\ \ \ (let ((format (extractFormat str labelFormats)))
\ \ \ \ \ (if (not (= (length format) 2))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ""
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (car (cdr format)))))
</input>
<\input|Scheme] >
\;
</input>
</session>
<subsection|Manipulation of format list >
<subsubsection|Check that a format list is syntactically correct>
<\session|scheme|default>
<\textput>
Checks that a single format <verbatim|formatGroup><marginal-note|normal|c|<small|Is
<verbatim|checkGroup> a good name for this function? Same question for
the other function names in this section>> is a list of two elements
(<with|color|red|to do>: must issue error).
</textput>
<\input|Scheme] >
(define (checkGroup formatGroup)
\ \ (cond
\ \ \ \ \ ((not (list? formatGroup)) #f)
\ \ \ \ \ \ ((not (= (length formatGroup) 2)) #f)
\ \ \ \ \ \ (else #t)))
</input>
<\textput>
Checks that all elements of <verbatim|formatGroup> are strings
(<with|color|red|to do>: must issue error).
</textput>
<\input|Scheme] >
(define (checkFormatStrings formatGroup)
\ \ \ (apply and-list (map string? formatGroup)))
</input>
<\textput>
Checks that the format is syntactically correct: it must be a list of
two elements, each of which is a string.
</textput>
<\input|Scheme] >
(define (checkFormatGroup formatGroup)
\ \ (and (checkGroup formatGroup)
\ \ \ \ \ \ \ (checkFormatStrings formatGroup)))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsubsection|Check whether a format is present in the list>
<\session|scheme|default>
<\textput>
Compare two formats (different from the \Pmatching\Q function because
it compares the lists that define formats)
</textput>
<\input|Scheme] >
(define (checkFormatEqual formatGroup1 formatGroup2)
\ \ \ (equal? (car formatGroup1) (car formatGroup2)))
</input>
<\textput>
Check that a format is present in the global variable
<verbatim|formatList> using <verbatim|checkFormatEqual> and the
<verbatim|find> function of Scheme.
</textput>
<\input|Scheme] >
(define (checkFormatPresent formatGroup)
\ \ (not
\ \ \ (equal?
\ \ \ \ (filter (lambda (x) (checkFormatEqual formatGroup x))
formatList)
\ \ \ \ (list))))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsubsection|Adding a format to the list>
<\session|scheme|default>
<\textput>
Check on input:
<\itemize>
<item>The format is a list of two elements
<item>The format list consists of strings
<item>The format is not already present
</itemize>
</textput>
<\note>
The first two checks should be combined into one, the test of the
correct syntax of the format
</note>
<\input|Scheme] >
(define (checkFormatGroupAdd formatGroup)
\ \ \ (cond
\ \ \ \ \ \ ((not (checkGroup formatGroup)) (begin (display "Format
must be a list of two elements") #f))
\ \ \ \ \ \ \ ((not (checkFormatStrings formatGroup)) (begin (display
"Format must be a list of two strings") #f))
\ \ \ \ \ \ \ \ ((checkFormatPresent formatGroup) (begin (display
"Format is already present, use replaceFormatGroup to replace") #f))
\ \ \ \ \ \ \ \ \ (else #t)))
</input>
<\input|Scheme] >
(define (addToFormatList formatGroup)
\ \ (if (checkFormatGroupAdd formatGroup)
\ \ \ \ \ \ (set! formatList (append formatList (list formatGroup)))
\ \ \ \ \ \ \ (display "\\nincorrect application of addToFormatList")))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsubsection|Deleting a format from the list>
<\session|scheme|default>
<\textput>
<paragraph|Delete the n-th element of a list for which the predicate
<verbatim|cd> (for condition)<marginal-note|normal|c|<with|font-series|medium|<small|I
could call the input parameter <verbatim|cd> with the name
<verbatim|pred>>>> is true.>
In part copied from a <hlink|Stackexchange
question|https://stackoverflow.com/questions/29273374/scheme-guile-remove-only-first-occurrence-from-a-list-non-destructive>.
</textput>
<\input|Scheme] >
(define (delete-nth-cond cd lst n)
\ \ \ (cond ((null? lst) '()) ; base case
\ \ \ \ \ \ \ \ \ ((cd (car lst)) (cond ((= n 1) (cdr lst)); if n = 1
return the cdr, otherwise we call it again with n-1
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else
(cons (car lst)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (delete-nth-cond
cd (cdr lst) (+ -1 n))))))
\ \ \ \ \ \ \ \ \ (else (cons (car lst) ; if the condition is not
satisfied retain the last element
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ; and apply
the function to the rest of the list
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (delete-nth-cond cd (cdr
lst) n)))))
</input>
\;
<\textput>
<paragraph|Delete the n-th occurrence of a format from the list>
</textput>
<\input|Scheme] >
(define (deleteFromFormatList formatGroup n)
\ \ (set! formatList
\ \ \ \ \ \ \ \ (delete-nth-cond
\ \ \ \ \ \ \ \ \ \ (lambda (x) (checkFormatEqual formatGroup x))
formatList n)))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<subsubsection|Replace the n-th element of a list for which the predicate
<verbatim|test> (for condition)<marginal-note|normal|c|<with|font-series|medium|<small|Here
the input parameter for the condition is called <verbatim|test>; perhaps it
is a good name for <verbatim|delete-nth-cond> too>>> is true.>
<\session|scheme|default>
<\textput>
<paragraph|Replace the n-th element of a list that satisfied test>
</textput>
<\textput>
This is copied from <verbatim|replace-nth-Test2> in the Emacs file, I
have renamed it here
</textput>
<\input|Scheme] >
(define (replace-nth-ElementCond lst test to n)
\ \ \ (cond ((null? lst) '()) ; base case = end of input - issue
warning here?
\ \ \ \ \ \ \ \ \ \ ((test (car lst)) (if (= n 1) (cons to (cdr lst))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons
(car lst)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (replace-nth-ElementCond
(cdr lst) test to (+ n -1)))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else (cons (car lst)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (replace-nth-ElementCond
(cdr lst) test to n)))))
</input>
\;
<\textput>
<paragraph|Replace the n-th occurrence of a format in the list>
Uses ideas from a <hlink|Stackexchange
question|https://stackoverflow.com/questions/15856346/replacing-an-element-into-a-list-scheme>.
Checks that there are at least n elements of the format list that match
<verbatim|formatGroup>.
</textput>
<\input|Scheme] >
(define (replaceFormatCore formatGroup n)
\ \ (let ((condition (lambda (x) (checkFormatEqual formatGroup x))))
\ \ \ \ \ (let ((lengthFts \ (length (filter condition formatList))))
\ \ \ \ \ \ \ (if (\<gtr\>= (length (filter condition formatList)) n)
\ \ \ \ \ \ \ \ \ \ \ \ (set! formatList (replace-nth-ElementCond
formatList condition formatGroup n))
\ \ \ \ \ \ \ \ \ \ \ \ \ (begin (display "Format list contains only ")
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (display lengthFts)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (display " formats
corresponding to the input"))))))
</input>
\;
<\textput>
Checks that <verbatim|formatGroup> is sintactically correct, then
replaces the n-th format whose format code matches.
</textput>
<\input|Scheme] >
(define (replaceFormat formatGroup n)
\ \ (if (checkFormatGroup formatGroup)
\ \ \ \ \ \ \ (replaceFormatCore formatGroup n)
\ \ \ \ \ \ \ \ (display "did not execute replacement function")))
</input>
<\input|Scheme] >
\;
</input>
\;
</session>
<\section>
<subsection|Helper functions>
</section>
An <verbatim|and> function that can be applied to lists (using the Scheme
function <verbatim|apply>). It returns <verbatim|#t> if all elements of the
list are <verbatim|#t>. Copied from a <hlink|Stackexchange
question|https://stackoverflow.com/questions/387775/using-and-with-the-apply-function-in-scheme>.
<\session|scheme|default>
<\input|Scheme] >
(define and-list (lambda x
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (null? x)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ #t
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (car x) (apply
and-list (cdr x)) #f))))
</input>
<\textput>
<\note>
<verbatim|and-list> is lambda variadic, so it can be applied to
either an arbitrary number of arguments - for example <code*|(and-l
#t #t #f)> - or to a list using <verbatim|apply> - example
<verbatim|(apply and-list (list #t #t
#f))>matches\Q?<marginal-note|normal|c|<small|I do not understand how
the parameters of <verbatim|and-list> are determined. Is there a
general rule that applied to this case and I do not know?>>
</note>
</textput>
<\input|Scheme] >
\;
</input>
</session>
<section|Interface to <TeXmacs>>
<\session|scheme|default>
<\input|Scheme] >
(set! formatList (list (list "eq:" "Equation ") (list "fig:" "Figure
")))
</input>
<\unfolded-io|Scheme] >
(tm-define (formatRefScheme str)
\ \ (set! str (tree-\<gtr\>stree str))
\ \ (stree-\<gtr\>tree (extractFormatString str formatList)))
<|unfolded-io>
((guile-user) (guile-user))
</unfolded-io>
<\unfolded-io|Scheme] >
(extractFormatString "eq:" formatList)
<|unfolded-io>
"Equation "
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<subparagraph|Test>
<\equation>
sin<around*|(|x|)><rsup|2>+cos<around*|(|x|)><rsup|2>=1<label|eq:testFormat>
</equation>
<formatRef|eq:testFormat>
<subsection|Manipulation of format list><label|sec:listManipulation>
<subsubsection|Add to format list>
<\session|scheme|default>
<\unfolded-io|Scheme] >
(tm-define (addToFormatListScheme formatCode formatString)
\ \ (set! formatCode (tree-\<gtr\>stree formatCode))
\ \ (set! formatString (tree-\<gtr\>stree formatString))
\ \ (let ((ft (cons formatCode (cons formatString (list) ))
\ \ \ \ \ \ \ \ \ \ \ \ ))
\ \ \ \ \ \ \ \ \ \ \ \ (addToFormatList ft)))
<|unfolded-io>
((guile-user) (guile-user) (guile-user) (guile-user))
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<addToFormatRef|sec:|Section >
<\note>
I need to execute the macro addToFormatRefList without returning anything
</note>
<\session|scheme|default>
<\unfolded-io|Scheme] >
formatList
<|unfolded-io>
(("eq:" "Equation ") ("fig:" "Figure "))
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<subparagraph|Test>
<formatRef|sec:listManipulation>
<subsubsection|Delete from format list>
<\session|scheme|default>
<\unfolded-io|Scheme] >
(tm-define (deleteFromFormatListScheme formatCode n)
\ \ (set! formatCode (tree-\<gtr\>stree formatCode))
\ \ (set! n (string-\<gtr\>number (tree-\<gtr\>stree n)))
\ \ \ \ \ \ \ \ (let (
(ft (cons formatCode (list)))
)
\ \ \ \ \ \ \ \ \ \ (deleteFromFormatList ft n)))
<|unfolded-io>
((guile-user) (guile-user) (guile-user))
</unfolded-io>
<\input|Scheme] >
\;
</input>
<\textput>
<subparagraph|Test>
<deleteFromFormatRef|sec:|1>
<\session|scheme|default>
<\unfolded-io|Scheme] >
formatList
<|unfolded-io>
(("eq:" "Equation ") ("fig:" "Figure "))
</unfolded-io>
<\input|Scheme] >
\;
</input>
<\textput>
<formatRef|sec:listManipulation>
</textput>
</session>
</textput>
</session>
<subsubsection|Replace in format list>
<\session|scheme|default>
<\unfolded-io|Scheme] >
(tm-define (replaceInFormatListScheme formatCode formatString n)
\ \ (set! formatCode (tree-\<gtr\>stree formatCode))
\ \ (set! formatString (tree-\<gtr\>stree formatString))
\ \ (set! n (string-\<gtr\>number (tree-\<gtr\>stree n)))
\ \ (let (
(ft (cons formatCode (cons formatString (list))))
\ )
\ \ \ \ (replaceFormat ft n)))
<|unfolded-io>
((guile-user))
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<subparagraph|Test>
<addToFormatRef|sec:|Section >
<replaceInFormatRef|sec:|sect. |1>