slides_for-syntax.rkt
(module slides_for-syntax racket

  (provide amkhlv/formula-syntax)
  (define (amkhlv/formula-syntax #:autoalign-formula-prefix auto-prefix 
                                 #:manual-formula-prefix    formula-prefix 
                                 #:display-math-prefix      display-math-prefix 
                                 #:size-change-notation     size-change-id 
                                 #:size-restore-notation    size-restore-id 
                                 stx)
    (let* (
           [calc-align (lambda (s) (list '+ 'manual-base-alignment s))]
           [calc-size  (lambda (s) (list '+ 'formula-size (* 2 s)))]
           [auto (list 'define (string->symbol auto-prefix) 
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor
                                   '#:use-depth #t '#:aa-adjust 'autoalign-adjust '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u))))]
           [disp (list 'define 
                       `( ,(string->symbol display-math-prefix) 
                          #:label (lbl #f) #:hspace (hsp 4) #:size (n formula-size) . x)
                       (list 'amkhlv/equation 
                             '#:label 'lbl '#:hspace 'hsp '#:size 'n '#:shell-command 'formula-processor 
                             '#:aa-adjust 'autoalign-adjust 
                             '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                             'x)
                       )]
           [oldsz (list 'define 'old-formula-size 'formula-size)]
           [oldaa (list 'define 'old-autoalign-adjust 'autoalign-adjust)]
           [ch-sz (list 'define (list (string->symbol size-change-id) (list 'i #f) (list 'aaadj '#f))
                        (list 'if 'i (list 'begin 
                                           (list 'set! 'old-formula-size 'formula-size)
                                           (list 'set! 'formula-size 'i)
                                           (list 'when 'aaadj (list 'begin 
                                                                    (list 'set! 'old-autoalign-adjust 'autoalign-adjust)
                                                                    (list 'set! 'autoalign-adjust 'aaadj))))
                              (list 'begin 
                                    (list 'set! 'formula-size 'old-formula-size)
                                    (list 'set! 'autoalign-adjust 'old-autoalign-adjust)
                                    ))
                        )]
           [rs-sz (list 'define (list (string->symbol size-restore-id))
                        (list 'set! 'formula-size 'old-formula-size)
                        (list 'set! 'autoalign-adjust 'old-autoalign-adjust)
                        )]
           [initf (list 'define 'amkhlv/setupformuladatabase
                        (list 'amkhlv/initialize-formula-collection 
                              'formula-database-name 'formula-dir-name))]
           [l    (list 'define (string->symbol formula-prefix)
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor
                                   '#:align (calc-align 0) '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u))))]
           [l+0  (list 'define (string->symbol (string-append formula-prefix "+0"))
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor
                                   '#:align (calc-align 0) '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u))))]
           [l-0  (list 'define (string->symbol (string-append formula-prefix "-0"))
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor
                                   '#:align (calc-align 0) '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u))))]
           [l+0+ (lambda (z)
                   (list 'define 
                         (string->symbol (string-append formula-prefix "+0+" (number->string z)))
                         (list 'lambda 'u 
                               (list 'amkhlv/formula '#:shell-command 'formula-processor
                                     '#:align (calc-align 0) '#:size (calc-size z)
                                     '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                     (list 'apply 'string-append 'u)))
                         ))]
           [l-0+ (lambda (z)
                   (list 'define 
                         (string->symbol (string-append formula-prefix "-0+" (number->string z)))
                         (list 'lambda 'u 
                               (list 'amkhlv/formula '#:shell-command 'formula-processor
                                     '#:align (calc-align 0) '#:size (calc-size z)
                                     '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                     (list 'apply 'string-append 'u)))
                         ))]
           [l+0- (lambda (z)
                   (list 'define 
                         (string->symbol (string-append formula-prefix "+0-" (number->string z)))
                         (list 'lambda 'u 
                               (list 'amkhlv/formula '#:shell-command 'formula-processor
                                     '#:align (calc-align 0) '#:size (calc-size (- z))
                                     '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                     (list 'apply 'string-append 'u)))
                         ))]
           [l-0- (lambda (z)
                   (list 'define 
                         (string->symbol (string-append formula-prefix "-0-" (number->string z)))
                         (list 'lambda 'u 
                               (list 'amkhlv/formula '#:shell-command 'formula-processor
                                     '#:align (calc-align 0) '#:size (calc-size (- z))
                                     '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                     (list 'apply 'string-append 'u)))
                         ))]
           [l+ (lambda (m)
                 (list 'define 
                       (string->symbol (string-append formula-prefix    
                                                      "+"
                                                      (number->string m)))
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor
                                   '#:align (calc-align (* 5 m)) '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u)))
                       ))]
           [l- (lambda (m)
                 (list 'define 
                       (string->symbol (string-append formula-prefix    
                                                      "-"
                                                      (number->string m)))
                       (list 'lambda 'u 
                             (list 'amkhlv/formula '#:shell-command 'formula-processor 
                                   '#:align (calc-align (* (- 5) m)) '#:size 'formula-size 
                                   '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                   (list 'apply 'string-append 'u)))
                       ))]
           [l++ (lambda (m z)
                  (list 'define 
                        (string->symbol (string-append formula-prefix    
                                                       "+" (number->string m)
                                                       "+" (number->string z)
                                                       ))
                        (list 'lambda 'u 
                              (list 'amkhlv/formula '#:shell-command 'formula-processor
                                    '#:align (calc-align (* 5 m)) '#:size (calc-size z)
                                    '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                    (list 'apply 'string-append 'u)))
                        ))]
           [l+- (lambda (m z)
                  (list 'define 
                        (string->symbol (string-append formula-prefix    
                                                       "+" (number->string m)
                                                       "-" (number->string z)
                                                       ))
                        (list 'lambda 'u 
                              (list 'amkhlv/formula '#:shell-command 'formula-processor 
                                    '#:align (calc-align (* 5 m)) '#:size (calc-size (- z))
                                    '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                    (list 'apply 'string-append 'u)))
                        ))]
           [l-+ (lambda (m z)
                  (list 'define 
                        (string->symbol (string-append formula-prefix    
                                                       "-" (number->string m)
                                                       "+" (number->string z)
                                                       ))
                        (list 'lambda 'u 
                              (list 'amkhlv/formula '#:shell-command 'formula-processor
                                    '#:align (calc-align (* (- 5) m)) '#:size (calc-size z)
                                    '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                    (list 'apply 'string-append 'u)))
                        ))]
           [l-- (lambda (m z)
                  (list 'define 
                        (string->symbol (string-append formula-prefix    
                                                       "-" (number->string m)
                                                       "-" (number->string z)
                                                       ))
                        (list 'lambda 'u 
                              (list 'amkhlv/formula '#:shell-command 'formula-processor
                                    '#:align (calc-align (* (- 5) m)) '#:size (calc-size (- z)) 
                                    '#:database 'formula-database '#:formulas-in-dir 'formula-dir-name
                                    (list 'apply 'string-append 'u)))
                        ))]
           [def-list+ (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l+ i))]
           [def-list- (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l- i))]
           [def-list+0+ (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l+0+ i))]
           [def-list-0+ (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l-0+ i))]
           [def-list+0- (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l+0- i))]
           [def-list-0- (for/list ([i '(1 2 3 4 5 6 7 8 9)]) (l-0- i))]
           [def-list++ (apply append (for/list ([i '(1 2 3 4 5 6 7 8 9)]) 
                                               (for/list ([z '(1 2 3 4 5 6 7 8 9)]) (l++ i z))))]
           [def-list+- (apply append (for/list ([i '(1 2 3 4 5 6 7 8 9)]) 
                                               (for/list ([z '(1 2 3 4 5 6 7 8 9)]) (l+- i z))))]
           [def-list-+ (apply append (for/list ([i '(1 2 3 4 5 6 7 8 9)]) 
                                               (for/list ([z '(1 2 3 4 5 6 7 8 9)]) (l-+ i z))))]
           [def-list-- (apply append (for/list ([i '(1 2 3 4 5 6 7 8 9)]) 
                                               (for/list ([z '(1 2 3 4 5 6 7 8 9)]) (l-- i z))))]
           )
      (syntax-case stx () 
        [(_ x) (datum->syntax 
                #'x
                (append (list 'begin auto disp oldsz oldaa ch-sz rs-sz initf l l+0 l-0) 
                        def-list+ def-list-
                        def-list+0+ def-list+0- def-list-0+ def-list-0-
                        def-list++ def-list+- def-list-+ def-list--
                        )
                #'x #'x #'x)])
      )
    )

  )