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