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