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