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