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