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