]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[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@xs4all.nl>
7
8 ;; Internationalisation: (_i "to be translated") gets an entry in the
9 ;; POT file (gettext ) must be invoked explicitely to do the actual
10 ;; "translation".
11 ;;(define-macro (_i x) x)
12 ;;(define-macro-public _i (x) x)
13 ;;(define-public-macro _i (x) x)
14 ;; Abbrv-PWR!
15 (defmacro-public _i (x) x)
16
17 (read-enable 'positions)
18 (debug-enable 'debug)
19
20
21
22 (define scheme-options-definitions
23   `(
24
25     ;; NAMING: either
26
27     ;; - [subject-]object-object-verb +"ing"
28     ;; - [subject-]-verb-object-object
29
30     (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
31     (backend ps "which backend to use by default; Options: eps, ps [default], scm, svg, tex, texstr)")
32     (check-internal-types #f "check every property assignment for types")
33     (clip-systems #f "Generate cut-out snippets of a score")
34     (datadir #f "LilyPond prefix for data files (Readonly).")
35     (debug-gc #f "dump memory debugging statistics")
36     (debug-gc-assert-parsed-dead #f "for memory debugging:
37 ensure that all refs to parsed objects are dead.  This is an internal option, and is switched on automatically for -ddebug-gc.") 
38     (debug-lexer #f "debug the flex lexer")
39     (debug-page-breaking-scoring #f "dump scores for many different page breaking configurations")
40     (debug-parser #f "debug the bison parser")
41     (debug-property-callbacks #f "debug cyclic callback chains")
42     (debug-skylines #f "debug skylines")
43     (delete-intermediate-files #f
44                                "delete unusable PostScript files")
45     (dump-profile #f "dump timing information for each file")
46     (dump-signatures #f "dump output signatures of each system.  Used for regression testing.")
47     
48     (eps-box-padding #f "Pad EPS bounding box left edge.  Guarantee alignment between systems in LaTeX.")
49     (gs-load-fonts #f
50                    "load fonts via Ghostscript.")
51     (gui #f "running from gui; redirect stderr to log file")
52     (help #f "show this help.") 
53     (include-book-title-preview #t "include book-titles in preview images.")
54     (include-eps-fonts #t "Include fonts in separate-system EPS files.")
55     (job-count #f "Process in parallel") 
56     (log-file #f "redirect output to log FILE.log")
57
58     (old-relative #f
59                   "relative for simultaneous music works
60 similar to chord syntax")
61     (point-and-click #t "use point & click")
62     (paper-size "a4" "the default paper size")
63     (pixmap-format "png16m" "GS format to use for pixel images")
64     (preview #f "make a incipit image. ")
65     (print-pages #t "print pages normally. ")
66     (protected-scheme-parsing #t "continue when finding errors in inline
67 scheme are caught in the parser. If off, halt 
68 on errors, and print a stack trace.")
69     (profile-property-accesses #f "keep statistics of get_property() calls.")
70     
71     (resolution 101 "resolution for generating PNG bitmaps")
72     (read-file-list #f "Read files to be processed from command line arguments")
73
74     (safe #f "Run safely")
75     (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
76     (separate-log-files #f "Output to FILE.log per file.")
77     (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
78     (trace-scheme-coverage #f "Record coverage of Scheme files") 
79     (ttf-verbosity 0
80                    "how much verbosity for TTF font embedding?")
81     (show-available-fonts #f
82                           "List font names available.")
83     (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
84     ))
85
86 ;; need to do this in the beginning. Other parts of the
87 ;; Scheme init depend on these options.
88 ;;
89 (for-each
90  (lambda (x)
91    (ly:add-option (car x) (cadr x) (caddr x)))
92  scheme-options-definitions)
93
94 (for-each
95  (lambda (x)
96    (ly:set-option (car x) (cdr x)))
97  (eval-string (ly:command-line-options)))
98
99 (debug-set! stack 0)
100
101 (if (defined? 'set-debug-cell-accesses!)
102     (set-debug-cell-accesses! #f))
103
104                                         ;(set-debug-cell-accesses! 1000)
105
106 (use-modules (ice-9 regex)
107              (ice-9 safe)
108              (ice-9 rdelim)
109              (ice-9 optargs)
110              (oop goops)
111              (srfi srfi-1)
112              (srfi srfi-13)
113              (srfi srfi-14)
114              (scm clip-region)
115              (scm memory-trace)
116              (scm coverage)
117              )
118
119 ;; my display
120 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
121   v)
122
123 (define-public (print . args)
124   (apply format (cons (current-output-port) args)))
125
126
127 ;;; General settings
128 ;;; debugging evaluator is slower.  This should
129 ;;; have a more sensible default.
130
131 (if (or (ly:get-option 'verbose)
132         (ly:get-option 'trace-memory-frequencency)
133         (ly:get-option 'trace-scheme-coverage)
134         )
135     (begin
136       (ly:set-option 'protected-scheme-parsing #f)
137       (debug-enable 'debug)
138       (debug-enable 'backtrace)
139       (read-enable 'positions)))
140
141
142 (if (ly:get-option 'trace-scheme-coverage)
143     (coverage:enable))
144
145 (define-public tex-backend?
146   (member (ly:get-option 'backend) '(texstr tex)))
147
148 (define-public parser #f)
149
150 (define-public (lilypond-version)
151   (string-join
152    (map (lambda (x) (if (symbol? x)
153                         (symbol->string x)
154                         (number->string x)))
155         (ly:version))
156    "."))
157
158
159 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT 
160 (define-public TEX_STRING_HASHLIMIT 10000000)
161
162
163
164 ;; gettext wrapper for guile < 1.7.2
165 (if (defined? 'gettext)
166     (define-public _ gettext)
167     (define-public _ ly:gettext))
168
169 (define-public (ly:load x)
170   (let* ((file-name (%search-load-path x)))
171     (if (ly:get-option 'verbose)
172         (ly:progress "[~A" file-name))
173     (if (not file-name)
174         (ly:error (_ "cannot find: ~A") x))
175     (primitive-load file-name)
176     (if (ly:get-option 'verbose)
177         (ly:progress "]"))))
178
179 ;; Cygwin
180 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
181 ;;
182 ;; Debian
183 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
184 ;;
185 ;; Mingw
186 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
187 ;;
188
189 ;; ugh, code dup.
190 (define-public PLATFORM
191   (string->symbol
192    (string-downcase
193     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
194
195 (define-public DOS
196   (let ((platform (string-tokenize
197                    (vector-ref (uname) 0) char-set:letter+digit)))
198     (if (null? (cdr platform)) #f
199         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
200
201 (case PLATFORM
202   ((windows)
203    (define native-getcwd getcwd)
204    (define (slashify x)
205      (if (string-index x #\\)
206          x
207          (string-regexp-substitute
208           "//*" "/"
209           (string-regexp-substitute "\\\\" "/" x))))
210    ;; FIXME: this prints a warning.
211    (define-public (ly-getcwd)
212      (slashify (native-getcwd))))
213   (else (define-public ly-getcwd getcwd)))
214
215 (define-public (is-absolute? file-name)
216   (let ((file-name-length (string-length file-name)))
217     (if (= file-name-length 0)
218         #f
219         (or (eq? (string-ref file-name 0) #\/)
220             (and (eq? PLATFORM 'windows)
221                  (> file-name-length 2)
222                  (eq? (string-ref file-name 1) #\:)
223                  (eq? (string-ref file-name 2) #\/))))))
224
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226
227 (define (type-check-list location signature arguments)
228   "Typecheck a list of arguments against a list of type
229 predicates. Print a message at LOCATION if any predicate failed."
230
231   (define (recursion-helper signature arguments count) 
232     (define (helper pred? arg count) 
233       (if (not (pred? arg))
234
235           (begin
236             (ly:input-message
237              location
238              (format
239               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
240               count (type-name pred?) arg))
241             #f)
242           #t))
243
244     (if (null? signature)
245         #t
246         (and (helper (car signature) (car arguments) count)
247              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
248
249   (recursion-helper signature arguments 1))
250
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252 ;;  output
253
254
255 ;;(define-public (output-framework) (write "hello\n"))
256
257 (define output-tex-module
258   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
259 (define output-ps-module
260   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
261
262 (define-public (ps-output-expression expr port)
263   (display (eval expr output-ps-module) port))
264
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; Safe definitions utility
267 (define safe-objects (list))
268
269 (define-macro (define-safe-public arglist . body)
270   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
271 The syntax is the same as `define*-public'."
272   (define (get-symbol arg)
273     (if (pair? arg)
274         (get-symbol (car arg))
275         arg))
276   (let ((safe-symbol (get-symbol arglist)))
277     `(begin
278        (define*-public ,arglist
279          ,@body)
280        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
281                                 safe-objects))
282        ,safe-symbol)))
283
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; init pitch system
286
287 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
288
289
290
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 ;; other files.
293
294
295 (define
296   init-scheme-files
297   '("lily-library.scm"
298             "file-cache.scm"
299             "define-event-classes.scm"
300             "define-music-types.scm"
301             "output-lib.scm"
302             "c++.scm"
303             "chord-ignatzek-names.scm"
304             "chord-entry.scm"
305             "chord-generic-names.scm"
306             "stencil.scm"
307             "markup.scm"
308             "music-functions.scm"
309             "part-combiner.scm"
310             "autochange.scm"
311             "define-music-properties.scm"
312             "auto-beam.scm"
313             "chord-name.scm"
314
315             "parser-ly-from-scheme.scm"
316             "ly-syntax-constructors.scm"
317             
318             "define-context-properties.scm"
319             "translation-functions.scm"
320             "script.scm"
321             "midi.scm"
322             "layout-beam.scm"
323             "parser-clef.scm"
324             "layout-slur.scm"
325             "font.scm"
326             "encoding.scm"
327             
328             "fret-diagrams.scm"
329             "define-markup-commands.scm"
330             "define-grob-properties.scm"
331             "define-grobs.scm"
332             "define-grob-interfaces.scm"
333             "define-stencil-commands.scm"
334             "titling.scm"
335             
336             "paper.scm"
337             "backend-library.scm"
338             "x11-color.scm"
339
340             ;; must be after everything has been defined
341             "safe-lily.scm"))
342
343
344 (for-each ly:load init-scheme-files)
345
346
347 (set! type-p-name-alist
348       `(
349         (,boolean-or-symbol? . "boolean or symbol")
350         (,boolean? . "boolean")
351         (,char? . "char")
352         (,grob-list? . "list of grobs")
353         (,hash-table? . "hash table")
354         (,input-port? . "input port")
355         (,integer? . "integer")
356         (,list? . "list")
357         (,ly:context? . "context")
358         (,ly:dimension? . "dimension, in staff space")
359         (,ly:dir? . "direction")
360         (,ly:duration? . "duration")
361         (,ly:grob? . "layout object")
362         (,ly:input-location? . "input location")
363         (,ly:moment? . "moment")
364         (,ly:music? . "music")
365         (,ly:pitch? . "pitch")
366         (,ly:translator? . "translator")
367         (,ly:font-metric? . "font metric")
368         (,ly:simple-closure? . "simple closure")
369         (,markup-list? . "list of markups")
370         (,markup? . "markup")
371         (,ly:music-list? . "list of music")
372         (,number-or-grob? . "number or grob")
373         (,number-or-string? . "number or string")
374         (,number-pair? . "pair of numbers")
375         (,number? . "number")
376         (,output-port? . "output port")   
377         (,pair? . "pair")
378         (,procedure? . "procedure")
379         (,rhythmic-location? . "rhythmic location")
380         (,scheme? . "any type")
381         (,string? . "string")
382         (,symbol? . "symbol")
383         (,vector? . "vector")))
384
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; timing
387
388 (define (profile-measurements)
389   (let* ((t (times))
390          (stats (gc-stats)))
391     
392     (list
393      (- (+ (tms:cutime t)
394            (tms:utime t))
395         (ly:assoc-get 'gc-time-taken stats))
396      
397      (ly:assoc-get 'total-cells-allocated  stats 0)
398      )))
399
400 (define (dump-profile base last this)
401   (let*
402       ((outname (format "~a.profile" (basename base ".ly")))
403        (diff (map (lambda (y) (apply - y)) (zip this last))))
404     
405     (ly:progress "\nWriting timing to ~a..." outname)
406     (format (open-file outname "w")
407             "time: ~a\ncells: ~a\n"
408             (car diff)
409             (cadr diff)
410             )))
411
412
413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ;; debug mem leaks
415
416 (define gc-dumping #f)
417 (define gc-protect-stat-count 0)
418
419 (define-public (dump-live-object-stats outfile)
420   (for-each
421    (lambda (x)
422      (format outfile "~a: ~a\n" (car x) (cdr x)))
423    (sort (gc-live-object-stats)
424          (lambda (x y)
425            (string<? (car x) (car y))))))
426
427 (define-public (dump-gc-protects)
428   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
429   (let* ((protects (sort
430                     (hash-table->alist (ly:protects))
431                     (lambda (a b)
432                       (< (object-address (car a))
433                          (object-address (car b))))))
434
435          (out-file-name (string-append
436                          "gcstat-" (number->string gc-protect-stat-count)
437                          ".scm"))
438          (outfile    (open-file  out-file-name  "w")))
439
440     (set! gc-dumping #t)
441     (display (format "Dumping GC statistics ~a...\n" out-file-name))
442     (display
443      (map (lambda (y)
444             (let ((x (car y))
445                   (c (cdr y)))
446               (display 
447                (format "~a (~a) = ~a\n" (object-address x) c x)
448                outfile)))
449           (filter
450            (lambda (x)
451              (not (symbol? (car x))))
452            protects))
453      outfile)
454
455     (format outfile "\nprotected symbols: ~a\n"
456             (apply + (map (lambda (obj-count) (if (symbol? (car obj-count))
457                                                   (cdr obj-count)
458                                                   0))
459                              protects)))             
460
461     ;; (display (ly:smob-protects))
462     (newline outfile)
463     (if (defined? 'gc-live-object-stats)
464         (let* ((stats #f))
465           (display "Live object statistics: GC'ing\n")
466           (ly:reset-all-fonts)
467           (gc)
468           (gc)
469           (display "Asserting dead objects\n")
470           (ly:set-option 'debug-gc-assert-parsed-dead #t)
471           (gc)
472           (ly:set-option 'debug-gc-assert-parsed-dead #f)
473
474           (set! stats (gc-live-object-stats))
475           (display "Dumping live object statistics.\n")
476           (dump-live-object-stats outfile)))
477
478     (newline outfile)
479     (let*
480         ((stats (gc-stats)))
481       
482       (for-each
483        (lambda (sym)
484          (display
485           (format "~a ~a ~a\n"
486                   gc-protect-stat-count
487                   sym
488                   (let ((sym-stat (assoc sym stats)))
489                     (if sym-stat 
490                         (cdr sym-stat)
491                         "?")))
492           outfile))
493        '(protected-objects bytes-malloced cell-heap-size
494                            
495                            )))
496
497     (set! gc-dumping #f)
498     (close-port outfile)
499     
500     ))
501
502
503 (define (check-memory)
504   "read /proc/self to check up on memory use." 
505   (define (gulp-file name)
506     (let* ((file (open-input-file name))
507            (text (read-delimited "" file)))
508       (close file)
509       text))
510   (let*
511       ((stat (gulp-file "/proc/self/status"))
512        (lines (string-split stat #\newline))
513        (interesting (filter identity
514                             (map
515                              (lambda (l)
516                                (string-match "^VmData:[ \t]*([0-9]*) kB" l))
517                              lines)))
518        (mem (string->number (match:substring (car interesting) 1)))
519        )
520
521     
522     (display (format  "VMDATA: ~a\n" mem))
523     (display (gc-stats))
524     (if (> mem 100000)
525         (begin
526           (dump-gc-protects)
527           (raise 1)))
528     
529     ))
530
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532
533
534 (define (multi-fork count)
535   (define (helper count acc)
536     (if (> count 0)
537       (let*
538           ((pid  (primitive-fork)))
539         (if (= pid 0)
540             (1- count)
541             (helper (1- count) (cons pid acc))))
542       acc))
543   (helper count '()))
544
545
546 (define-public (lilypond-main files)
547   "Entry point for LilyPond."
548
549
550   (eval-string (ly:command-line-code))
551
552   (if (ly:get-option 'help)
553       (begin
554         (ly:option-usage)
555         (exit 0)))
556
557   (if (ly:get-option 'show-available-fonts)
558       (begin
559         (ly:font-config-display-fonts)
560         (exit 0)
561         ))
562   
563   
564   (if (ly:get-option 'gui)
565       (gui-main files))
566
567   (if (null? files)
568       (begin
569         (ly:usage)
570         (exit 2)))
571
572   (if (ly:get-option 'read-file-list)
573       (set! files
574             (filter (lambda (s)
575                       (> (string-length s) 0))
576                     (apply append
577                            (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
578                                 files)))
579             ))
580   
581   (if (and (number? (ly:get-option 'job-count))
582            (>= (length files) (ly:get-option 'job-count)))
583       
584       (let*
585           ((count (ly:get-option 'job-count))
586            (split-todo (split-list files count)) 
587            (joblist (multi-fork count))
588            (errors '()))
589
590         (if (not (string-or-symbol? (ly:get-option 'log-file)))
591             (ly:set-option 'log-file "lilypond-multi-run"))
592         
593         (if (number? joblist)
594             (begin
595               (ly:set-option 'log-file (format "~a-~a"
596                                                (ly:get-option 'log-file) joblist))
597               (set! files (vector-ref split-todo joblist)))
598
599             (begin
600               (ly:progress "\nForking into jobs:  ~a\n" joblist)
601               (for-each
602                (lambda (pid)
603                  (let* ((stat (cdr (waitpid pid))))
604                    
605                    (if (not (= stat 0))
606                        (set! errors (acons (list-element-index joblist pid) stat errors)))))
607                joblist)
608
609               (for-each
610                (lambda (x)
611                  (let* ((job (car x))
612                         (state (cdr x))
613                         (logfile  (format "~a-~a.log"
614                                           (ly:get-option 'log-file) job))
615                         (log (ly:gulp-file logfile))
616                         (len (string-length log))
617                         (tail (substring  log (max 0 (- len 1024)))))
618
619                    (if (status:term-sig state)
620                        (ly:message "\n\n~a\n"
621                                    (format (_ "job ~a terminated with signal: ~a")
622                                            job
623                                            (status:term-sig state)))
624                        (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
625
626                errors)
627
628               (if (pair? errors)
629                   (ly:error "Children ~a exited with errors." (map car errors)))
630
631               ;; must overwrite individual entries
632               (if (ly:get-option 'dump-profile)
633                   (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
634
635             (exit (if (null? errors) 0 1))))))
636               
637            
638   (if (string-or-symbol? (ly:get-option 'log-file))
639       (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
640
641   
642   (let ((failed (lilypond-all files)))
643     (if (ly:get-option 'trace-scheme-coverage)
644         (begin
645           (coverage:show-all (lambda (f) (string-contains f "lilypond"))
646                              )))
647           
648     
649     (if (pair? failed)
650         (begin
651           (ly:error (_ "failed files: ~S") (string-join failed))
652           (exit 1))
653         (begin
654           ;; HACK: be sure to exit with single newline
655           (ly:message "")
656           (exit 0)))))
657
658 (define-public (lilypond-all files)
659   (let* ((failed '())
660          (separate-logs (ly:get-option 'separate-log-files))
661          (do-measurements (ly:get-option 'dump-profile))
662          (handler (lambda (key failed-file)
663                     (set! failed (append (list failed-file) failed)))))
664
665     (gc)
666     (for-each
667      (lambda (x)
668        (let*
669            ((start-measurements (if do-measurements
670                                     (profile-measurements)
671                                     #f))
672             (base (basename x ".ly"))
673             (all-settings (ly:all-options)))
674
675          (if separate-logs
676              (ly:stderr-redirect (format "~a.log" base) "w"))
677          (if (ly:get-option 'trace-memory-frequency) 
678              (mtrace:start-trace  (ly:get-option 'trace-memory-frequency)))
679          
680          (lilypond-file handler x)
681          (if start-measurements
682              (dump-profile x start-measurements (profile-measurements)))
683
684          (if (ly:get-option 'trace-memory-frequency)
685              (begin
686                (mtrace:stop-trace)
687                (mtrace:dump-results base)))
688                  
689          (for-each
690           (lambda (s)
691             (ly:set-option (car s) (cdr s)))
692           all-settings)
693
694          (ly:clear-anonymous-modules)
695          (ly:set-option 'debug-gc-assert-parsed-dead #t)
696          (gc)
697          (ly:set-option 'debug-gc-assert-parsed-dead #f)
698
699          
700          (if (ly:get-option 'debug-gc)
701              (dump-gc-protects)
702              (if (= (random 40) 1)
703                  (ly:reset-all-fonts)))))
704
705      files)
706
707     ;; we want the failed-files notice in the aggregrate logfile.
708     (if (ly:get-option 'separate-logs)
709         (ly:stderr-redirect
710          (if (string-or-symbol? (ly:get-option 'log-file))
711              (format "~a.log" (ly:get-option 'log-file))
712              "/dev/tty") "a"))
713
714     (if (ly:get-option 'dump-profile)
715         (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
716
717     failed))
718
719 (define (lilypond-file handler file-name)
720   (catch 'ly-file-failed
721          (lambda () (ly:parse-file file-name))
722          (lambda (x . args) (handler x file-name))))
723
724 (use-modules (scm editor))
725
726 (define-public (gui-main files)
727   (if (null? files)
728       (gui-no-files-handler))
729
730   (if (not (string? (ly:get-option 'log-file)))
731       (let* ((base (basename (car files) ".ly"))
732              (log-name (string-append base ".log")))
733         (if (not (ly:get-option 'gui))
734             (ly:message (_ "Redirecting output to ~a...") log-name))
735         (ly:stderr-redirect log-name "w")
736         (ly:message "# -*-compilation-*-"))
737     
738     (let ((failed (lilypond-all files)))
739       (if (pair? failed)
740           (begin
741             ;; ugh
742             (ly:stderr-redirect "foo" "r")
743             (system (get-editor-command log-name 0 0 0))
744             (ly:error (_ "failed files: ~S") (string-join failed))
745             ;; not reached?
746             (exit 1))
747           (exit 0)))))
748
749 (define (gui-no-files-handler)
750   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
751          ;; FIXME: soft-code, localize
752          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
753          (cmd (get-editor-command welcome-ly 0 0 0)))
754     (ly:message (_ "Invoking `~a'...") cmd)
755     (system cmd)
756     (exit 1)))