]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
* scm/define-markup-commands.scm (verbatim-file): new markup
[lilypond.git] / scm / lily.scm
1 ;;;; lily.scm -- toplevel Scheme stuff
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define (define-scheme-options)
10   (for-each (lambda (x)
11               (ly:add-option (car x) (cadr x) (caddr x)))
12           
13             `((point-and-click #t "use point & click")
14               (paper-size "a4" "the default paper size")
15               (midi-debug #f "generate human readable MIDI")
16               (internal-type-checking #f "check every property assignment for types")
17               (parse-protect #t    "continue when finding errors in inline
18 scheme are caught in the parser. If off, halt 
19 on errors, and print a stack trace.")
20               (profile-property-accesses #f "keep statistics of get_property() calls.")
21               (old-relative #f
22                             "relative for simultaneous music works
23 similar to chord syntax")
24               (object-keys #f
25                            "experimental mechanism for remembering tweaks")
26               (resolution 101 "resolution for generating bitmaps")
27               (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
28               (preview-include-book-title #t "include book-titles in preview images.")
29               (gs-font-load #f
30                             "load fonts via Ghostscript.")
31               (gui #f "running from gui; redirect stderr to log file")
32               (delete-intermediate-files #f
33                                          "delete unusable PostScript files")
34               (safe #f "Run safely")
35               (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
36               (ttf-verbosity 0
37                            "how much verbosity for TTF font embedding?")
38               (debug-gc #f
39                         "dump GC protection info"))
40             ))
41
42 ;; FIXME: stray statement
43 (define-scheme-options)
44
45 (if (defined? 'set-debug-cell-accesses!)
46     (set-debug-cell-accesses! #f))
47
48 ;(set-debug-cell-accesses! 1000)
49
50 (use-modules (ice-9 regex)
51              (ice-9 safe)
52              (ice-9 optargs)
53              (oop goops)
54              (srfi srfi-1)
55              (srfi srfi-13)
56              (srfi srfi-14))
57
58
59 ;; my display
60 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
61
62 (define-public (print . args)
63   (apply format (cons (current-output-port) args)))
64
65
66 ;;; General settings
67 ;;; debugging evaluator is slower.  This should
68 ;;; have a more sensible default.
69
70 (if (ly:get-option 'verbose)
71     (begin
72       (debug-enable 'debug)
73       (debug-enable 'backtrace)
74       (read-enable 'positions)))
75
76 (define-public tex-backend?
77   (member (ly:output-backend) '("texstr" "tex")))
78
79 (define-public parser #f)
80
81 (define-public (lilypond-version)
82   (string-join
83    (map (lambda (x) (if (symbol? x)
84                         (symbol->string x)
85                         (number->string x)))
86         (ly:version))
87    "."))
88
89
90
91
92 ;; gettext wrapper for guile < 1.7.2
93 (if (defined? 'gettext)
94     (define-public _ gettext)
95     (define-public _ ly:gettext))
96
97 (define-public (ly:load x)
98   (let* ((file-name (%search-load-path x)))
99     (if (ly:get-option 'verbose)
100         (ly:progress "[~A" file-name))
101     (primitive-load file-name)
102     (if (ly:get-option 'verbose)
103         (ly:progress "]"))))
104
105 (define-public TEX_STRING_HASHLIMIT 10000000)
106
107 ;; Cygwin
108 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
109 ;;
110 ;; Debian
111 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
112 ;;
113 ;; Mingw
114 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
115 ;;
116
117 ;; ugh, code dup.
118 (define-public PLATFORM
119   (string->symbol
120    (string-downcase
121     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
122
123 (define-public DOS
124   (let ((platform (string-tokenize
125                    (vector-ref (uname) 0) char-set:letter+digit)))
126     (if (null? (cdr platform)) #f
127         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
128
129 (case PLATFORM
130   ((windows)
131    (define native-getcwd getcwd)
132    (define (slashify x)
133      (if (string-index x #\\)
134          x
135          (string-regexp-substitute
136           "//*" "/"
137           (string-regexp-substitute "\\\\" "/" x))))
138    ;; FIXME: this prints a warning.
139   (define-public (ly-getcwd)
140      (slashify (native-getcwd))))
141   (else (define-public ly-getcwd getcwd)))
142
143 (define-public (is-absolute? file-name)
144   (let ((file-name-length (string-length file-name)))
145     (if (= file-name-length 0)
146         #f
147         (or (eq? (string-ref file-name 0) #\/)
148             (and (eq? PLATFORM 'windows)
149                  (> file-name-length 2)
150                  (eq? (string-ref file-name 1) #\:)
151                  (eq? (string-ref file-name 2) #\/))))))
152
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154
155 (define (type-check-list location signature arguments)
156   "Typecheck a list of arguments against a list of type
157 predicates. Print a message at LOCATION if any predicate failed."
158   (define (recursion-helper signature arguments count) 
159     (define (helper pred? arg count) 
160       (if (not (pred? arg))
161
162           (begin
163             (ly:input-message
164              location
165              (format
166               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
167               count (type-name pred?) arg))
168             #f)
169           #t))
170
171     (if (null? signature)
172         #t
173         (and (helper (car signature) (car arguments) count)
174              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
175   (recursion-helper signature arguments 1))
176
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;;  output
179
180
181 ;;(define-public (output-framework) (write "hello\n"))
182
183 (define output-tex-module
184   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
185 (define output-ps-module
186   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
187
188 (define-public (ps-output-expression expr port)
189   (display (eval expr output-ps-module) port))
190
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;; Safe definitions utility
193 (define safe-objects (list))
194
195 (define-macro (define-safe-public arglist . body)
196   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
197 The syntax is the same as `define*-public'."
198   (define (get-symbol arg)
199     (if (pair? arg)
200         (get-symbol (car arg))
201         arg))
202   (let ((safe-symbol (get-symbol arglist)))
203     `(begin
204        (define*-public ,arglist
205          ,@body)
206        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
207                                 safe-objects))
208        ,safe-symbol)))
209
210
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;; other files.
213
214 (for-each ly:load
215           ;; load-from-path
216           '("lily-library.scm"
217             "file-cache.scm"
218             "define-music-types.scm"
219             "output-lib.scm"
220             "c++.scm"
221             "chord-ignatzek-names.scm"
222             "chord-entry.scm"
223             "chord-generic-names.scm"
224             "stencil.scm"
225             "markup.scm"
226             "music-functions.scm"
227             "part-combiner.scm"
228             "autochange.scm"
229             "define-music-properties.scm"
230             "auto-beam.scm"
231             "chord-name.scm"
232
233             "parser-ly-from-scheme.scm"
234             
235             "define-context-properties.scm"
236             "translation-functions.scm"
237             "script.scm"
238             "midi.scm"
239             "layout-beam.scm"
240             "parser-clef.scm"
241             "layout-slur.scm"
242             "font.scm"
243             "encoding.scm"
244             
245             "fret-diagrams.scm"
246             "define-markup-commands.scm"
247             "define-grob-properties.scm"
248             "define-grobs.scm"
249             "define-grob-interfaces.scm"
250             "define-stencil-commands.scm"
251             "layout-page-layout.scm"
252             "titling.scm"
253             
254             "paper.scm"
255             "backend-library.scm"
256             "x11-color.scm"
257
258             ;; must be after everything has been defined
259             "safe-lily.scm"))
260
261
262 (set! type-p-name-alist
263       `(
264         (,boolean-or-symbol? . "boolean or symbol")
265         (,boolean? . "boolean")
266         (,char? . "char")
267         (,grob-list? . "list of grobs")
268         (,hash-table? . "hash table")
269         (,input-port? . "input port")
270         (,integer? . "integer")
271         (,list? . "list")
272         (,ly:context? . "context")
273         (,ly:dimension? . "dimension, in staff space")
274         (,ly:dir? . "direction")
275         (,ly:duration? . "duration")
276         (,ly:grob? . "layout object")
277         (,ly:input-location? . "input location")
278         (,ly:moment? . "moment")
279         (,ly:music? . "music")
280         (,ly:pitch? . "pitch")
281         (,ly:translator? . "translator")
282         (,ly:font-metric? . "font metric")
283         (,ly:simple-closure? . "simple closure")
284         (,markup-list? . "list of markups")
285         (,markup? . "markup")
286         (,ly:music-list? . "list of music")
287         (,number-or-grob? . "number or grob")
288         (,number-or-string? . "number or string")
289         (,number-pair? . "pair of numbers")
290         (,number? . "number")
291         (,output-port? . "output port")   
292         (,pair? . "pair")
293         (,procedure? . "procedure") 
294         (,scheme? . "any type")
295         (,string? . "string")
296         (,symbol? . "symbol")
297         (,vector? . "vector")))
298
299
300 ;; debug mem leaks
301
302 (define gc-protect-stat-count 0)
303 (define-public (dump-gc-protects)
304   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
305   (let* ((protects (sort
306                     (hash-table->alist (ly:protects))
307                     (lambda (a b)
308                       (< (object-address (car a))
309                          (object-address (car b))))))
310
311          (out-file-name (string-append
312                          "gcstat-" (number->string gc-protect-stat-count)
313                          ".scm"))
314          (outfile    (open-file  out-file-name  "w")))
315
316     (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
317     (display
318      (map (lambda (y)
319             (let ((x (car y))
320                   (c (cdr y)))
321               
322               (string-append
323                (string-join
324                 (map object->string (list (object-address x) c x))
325                 " ")
326                "\n")))
327
328           (filter
329            (lambda (x)
330              (not (symbol? (car x))))
331            protects))
332      outfile)
333
334 ;    (display (ly:smob-protects))
335     (newline outfile)
336     (if (defined? 'gc-live-object-stats)
337         (let* ((stats #f))
338           (display "Live object statistics: GC'ing\n")
339           (gc)
340           (gc)
341           
342           (set! stats (gc-live-object-stats))
343           (display "Dumping live object statistics.\n")
344           
345           (for-each
346            (lambda (x)
347              (format outfile "~a: ~a\n" (car x) (cdr x)))
348            (sort (gc-live-object-stats)
349                  (lambda (x y)
350                    (string<? (car x) (car y)))))))))
351
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 (define-public (lilypond-main files)
354   "Entry point for LilyPond."
355   
356   (define (no-files-handler)
357     (ly:usage)
358     (exit 2))
359
360   (if (ly:get-option 'gui)
361       (gui-main files))
362
363   (if (null? files)
364       (no-files-handler))
365
366   (let ((failed (lilypond-all files)))
367     (if (pair? failed)
368         (begin
369           (ly:error (_ "failed files: ~S") (string-join failed))
370           (exit 1))
371         (begin
372           ;; HACK: be sure to exit with single newline
373           (ly:message "")
374           (exit 0)))))
375
376 (define-public (lilypond-all files)
377
378   (let* ((failed '())
379          (handler (lambda (key failed-file)
380                     (set! failed (append (list failed-file) failed)))))
381
382     (for-each
383      (lambda (x)
384        (lilypond-file handler x)
385        (ly:clear-anonymous-modules)
386        (if (ly:get-option 'debug-gc)
387            (dump-gc-protects)))
388      
389      files)
390     failed))
391
392 (define (lilypond-file handler file-name)
393   (catch 'ly-file-failed
394          (lambda () (ly:parse-file file-name))
395          (lambda (x . args) (handler x file-name))))
396
397 (use-modules (scm editor))
398
399 (define-public (gui-main files)
400   (if (null? files)
401       (gui-no-files-handler))
402   (let* ((base (basename (car files) ".ly"))
403          (log-name (string-append base ".log")))
404     (if (not (ly:get-option 'gui))
405         (ly:message (_ "Redirecting output to ~a...") log-name))
406     (ly:stderr-redirect log-name "w")
407     (ly:message "# -*-compilation-*-")
408     
409     (let ((failed (lilypond-all files)))
410       (if (pair? failed)
411           (begin
412             ;; ugh
413             (ly:stderr-redirect "foo" "r")
414             (system (get-editor-command log-name 0 0 0))
415             (ly:error (_ "failed files: ~S") (string-join failed))
416             ;; not reached?
417             (exit 1))
418           (exit 0)))))
419
420 (define (gui-no-files-handler)
421   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
422          ;; FIXME: soft-code, localize
423          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
424          (cmd (get-editor-command welcome-ly 0 0 0)))
425     (ly:message (_ "Invoking `~a'...") cmd)
426     (system cmd)
427     (exit 1)))