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