2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4
;; MODULE : tmtm-tidy.scm
5
;; DESCRIPTION : commodity routines for improving a document
6
;; COPYRIGHT : (C) 2003 Joris van der Hoeven
8
;; This software falls under the GNU general public license and comes WITHOUT
9
;; ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for details.
10
;; If you don't have this file, write to the Free Software Foundation, Inc.,
11
;; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
(texmacs-module (convert rewrite tmtm-tidy)
16
(:export tmtm-modernize-newlines
17
tmtm-eat-space-around-control
18
tmtm-remove-superfluous-newlines
19
tmtm-concat-document-correct))
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
;; Some useful subroutines
23
;; FIXME: concat- and document- correction should go elsewhere
24
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26
(define (tmtm-concat l)
28
((null? (cdr l)) (car l))
29
(else (cons 'concat l))))
31
(define (tmtm-document-sub l)
33
((func? (car l) 'document)
34
(append (tmtm-document-sub (cdar l))
35
(tmtm-document-sub (cdr l))))
36
(else (cons (car l) (tmtm-document-sub (cdr l))))))
38
(define (tmtm-document l)
39
(with r (tmtm-document-sub l)
40
(if (null? r) "" (cons 'document r))))
42
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43
;; Transform formatting newlines into documents
44
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
(define (tmtm-new-line? x) (== x '(new-line)))
47
(define (tmtm-document? x) (func? x 'document))
49
(define (tmtm-modernize-newlines l)
51
(with r (cons (car l) (map tmtm-modernize-newlines (cdr l)))
52
(cond ((func? r 'concat)
53
(with ll (list-scatter (cdr r) tmtm-new-line? #f)
54
(if (< (length ll) 2) r
55
(tmtm-document (map tmtm-concat ll)))))
56
((func? r 'document) (tmtm-document (cdr r)))
57
((and (list-find (cdr r) tmtm-document?)
58
(not (list-find (cdr l) tmtm-document?)))
62
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
;; Remove spaces before (and possibly after) control markup
64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66
(define (tmtm-control? l)
67
(and (list? l) (in? (car l) '(label index))))
69
(define (tmtm-really-eat? l)
71
(and (string? (car l)) (string-starts? (car l) " "))
72
(and (tmtm-control? (car l)) (tmtm-really-eat? (cdr l)))))
74
(define (tmtm-eat-before? l first?)
75
;; eat space before if possible and return #f otherwise
76
(cond ((< (length l) 2) #f)
77
((not (and (string? (car l)) (string-ends? (car l) " "))) #f)
78
((not (tmtm-control? (cadr l))) #f)
79
((and (== (car l) " ") first?) (cdr l))
80
((tmtm-really-eat? (cdr l))
83
(cons (string-drop-right (car l) 1) (cdr l))))
86
(define (tmtm-eat-after? l first?)
87
;; eat space after if possible and return #f otherwise
88
(cond ((or (null? l) (== l '(" "))) '())
89
((tmtm-control? (car l))
90
(with r (tmtm-eat-after? (cdr l) first?)
91
(if r (cons (car l) r) #f)))
92
((and (string? (car l)) (string-starts? (car l) " ") first?)
93
(if (== (car l) " ") '() (list (string-drop (car l) 1))))
96
(define (tmtm-eat-around l first?)
97
(with r (tmtm-eat-before? l first?)
98
(cond (r (tmtm-eat-around r #f))
100
((tmtm-control? (car l))
101
(with r (tmtm-eat-after? (cdr l) first?)
102
(cons (car l) (tmtm-eat-around (if r r (cdr l)) #f))))
103
(else (cons (car l) (tmtm-eat-around (cdr l) #f))))))
105
(define (tmtm-eat-space-around-control l)
106
(if (not (list? l)) l
107
(with r (map tmtm-eat-space-around-control (cdr l))
108
(if (func? l 'concat)
109
(tmtm-concat (tmtm-eat-around r #t))
112
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113
;; Remove superfluous newlines (i.e. remove empty paragraphs)
114
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116
(define (tmtm-preserve-space? l)
117
(and (list? l) (in? (car l) '(verbatim code))))
119
(define (tmtm-remove-superfluous-newlines l)
120
(cond ((tmtm-preserve-space? l) l)
122
(with r (map tmtm-remove-superfluous-newlines (cdr l))
123
(with f (list-filter r (lambda (x) (not (== x ""))))
124
(if (null? f) '(document "") (cons 'document f)))))
126
(cons (car l) (map tmtm-remove-superfluous-newlines (cdr l))))
129
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130
;; Glue concats with document items in it to yield a document
131
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133
(define (tmtm-concat-document-sub l)
136
(append (cdar l) (list (tmtm-concat (cdr l))))))
138
(define (tmtm-concat-document-correct l)
139
;; FIXME: might go into tmtm-concat constructor
140
(if (not (list? l)) l
141
(with r (cons (car l) (map tmtm-concat-document-correct (cdr l)))
142
(if (and (func? r 'concat) (list-find (cdr r) tmtm-document?))
143
(let* ((ll (list-scatter (cdr r) tmtm-document? #t))
144
(head (tmtm-concat (car ll)))
145
(aux (map tmtm-concat-document-sub (cdr ll)))
146
(tail (apply append aux)))
149
(tmtm-document (cons head tail))))