]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
LSR: add more files, and remove unnecessary input/test/ files.
[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-page-breaking-scoring #f "dump scores for many different page breaking configurations")
39     (debug-parser #f "debug the bison parser")
40     (debug-property-callbacks #f "debug cyclic callback chains")
41     (debug-skylines #f "debug skylines")
42     (delete-intermediate-files #f
43                                "delete unusable PostScript files")
44     (dump-profile #f "dump timing information for each file")
45     (dump-signatures #f "dump output signatures of each system.  Used for regression testing.")
46     
47     (eps-box-padding #f "Pad EPS bounding box left edge.  Guarantee alignment between systems in LaTeX.")
48     (gs-load-fonts #f
49                    "load fonts via Ghostscript.")
50     (gui #f "running from gui; redirect stderr to log file")
51     (help #f "show this help.") 
52     (include-book-title-preview #t "include book-titles in preview images.")
53     (include-eps-fonts #t "Include fonts in separate-system EPS files.")
54     (job-count #f "Process in parallel") 
55     (log-file #f "redirect output to log FILE.log")
56
57     (old-relative #f
58                   "relative for simultaneous music works
59 similar to chord syntax")
60     (point-and-click #t "use point & click")
61     (paper-size "a4" "the default paper size")
62     (pixmap-format "png16m" "GS format to use for pixel images")
63     (preview #f "make a incipit image. ")
64     (print-pages #t "print pages normally. ")
65     (protected-scheme-parsing #t "continue when finding errors in inline
66 scheme are caught in the parser. If off, halt 
67 on errors, and print a stack trace.")
68     (profile-property-accesses #f "keep statistics of get_property() calls.")
69     
70     (resolution 101 "resolution for generating PNG bitmaps")
71     (read-file-list #f "Read files to be processed from command line arguments")
72
73     (safe #f "Run safely")
74     (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
75     (separate-log-files #f "Output to FILE.log per file.")
76     (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
77     (trace-scheme-coverage #f "Record coverage of Scheme files") 
78     (ttf-verbosity 0
79                    "how much verbosity for TTF font embedding?")
80     (show-available-fonts #f
81                           "List font names available.")
82     (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
83     ))
84
85 ;; need to do this in the beginning. Other parts of the
86 ;; Scheme init depend on these options.
87 ;;
88 (for-each
89  (lambda (x)
90    (ly:add-option (car x) (cadr x) (caddr x)))
91  scheme-options-definitions)
92
93 (for-each
94  (lambda (x)
95    (ly:set-option (car x) (cdr x)))
96  (eval-string (ly:command-line-options)))
97
98 (debug-set! stack 0)
99
100 (if (defined? 'set-debug-cell-accesses!)
101     (set-debug-cell-accesses! #f))
102
103                                         ;(set-debug-cell-accesses! 1000)
104
105 (use-modules (ice-9 regex)
106              (ice-9 safe)
107              (ice-9 rdelim)
108              (ice-9 optargs)
109              (oop goops)
110              (srfi srfi-1)
111              (srfi srfi-13)
112              (srfi srfi-14)
113              (scm clip-region)
114              (scm memory-trace)
115              (scm coverage)
116              )
117
118 ;; my display
119 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
120   v)
121
122 (define-public (print . args)
123   (apply format (cons (current-output-port) args)))
124
125
126 ;;; General settings
127 ;;; debugging evaluator is slower.  This should
128 ;;; have a more sensible default.
129
130 (if (or (ly:get-option 'verbose)
131         (ly:get-option 'trace-memory-frequencency)
132         (ly:get-option 'trace-scheme-coverage)
133         )
134     (begin
135       (ly:set-option 'protected-scheme-parsing #f)
136       (debug-enable 'debug)
137       (debug-enable 'backtrace)
138       (read-enable 'positions)))
139
140
141 (if (ly:get-option 'trace-scheme-coverage)
142     (coverage:enable))
143
144 (define-public tex-backend?
145   (member (ly:get-option 'backend) '(texstr tex)))
146
147 (define-public parser #f)
148
149 (define-public (lilypond-version)
150   (string-join
151    (map (lambda (x) (if (symbol? x)
152                         (symbol->string x)
153                         (number->string x)))
154         (ly:version))
155    "."))
156
157
158 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT 
159 (define-public TEX_STRING_HASHLIMIT 10000000)
160
161
162
163 ;; gettext wrapper for guile < 1.7.2
164 (if (defined? 'gettext)
165     (define-public _ gettext)
166     (define-public _ ly:gettext))
167
168 (define-public (ly:load x)
169   (let* ((file-name (%search-load-path x)))
170     (if (ly:get-option 'verbose)
171         (ly:progress "[~A" file-name))
172     (if (not file-name)
173         (ly:error (_ "cannot find: ~A") x))
174     (primitive-load file-name)
175     (if (ly:get-option 'verbose)
176         (ly:progress "]"))))
177
178 ;; Cygwin
179 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
180 ;;
181 ;; Debian
182 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
183 ;;
184 ;; Mingw
185 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
186 ;;
187
188 ;; ugh, code dup.
189 (define-public PLATFORM
190   (string->symbol
191    (string-downcase
192     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
193
194 (define-public DOS
195   (let ((platform (string-tokenize
196                    (vector-ref (uname) 0) char-set:letter+digit)))
197     (if (null? (cdr platform)) #f
198         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
199
200 (case PLATFORM
201   ((windows)
202    (define native-getcwd getcwd)
203    (define (slashify x)
204      (if (string-index x #\\)
205          x
206          (string-regexp-substitute
207           "//*" "/"
208           (string-regexp-substitute "\\\\" "/" x))))
209    ;; FIXME: this prints a warning.
210    (define-public (ly-getcwd)
211      (slashify (native-getcwd))))
212   (else (define-public ly-getcwd getcwd)))
213
214 (define-public (is-absolute? file-name)
215   (let ((file-name-length (string-length file-name)))
216     (if (= file-name-length 0)
217         #f
218         (or (eq? (string-ref file-name 0) #\/)
219             (and (eq? PLATFORM 'windows)
220                  (> file-name-length 2)
221                  (eq? (string-ref file-name 1) #\:)
222                  (eq? (string-ref file-name 2) #\/))))))
223
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225
226 (define (type-check-list location signature arguments)
227   "Typecheck a list of arguments against a list of type
228 predicates. Print a message at LOCATION if any predicate failed."
229
230   (define (recursion-helper signature arguments count) 
231     (define (helper pred? arg count) 
232       (if (not (pred? arg))
233
234           (begin
235             (ly:input-message
236              location
237              (format
238               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
239               count (type-name pred?) arg))
240             #f)
241           #t))
242
243     (if (null? signature)
244         #t
245         (and (helper (car signature) (car arguments) count)
246              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
247
248   (recursion-helper signature arguments 1))
249
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
251 ;;  output
252
253
254 ;;(define-public (output-framework) (write "hello\n"))
255
256 (define output-tex-module
257   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
258 (define output-ps-module
259   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
260
261 (define-public (ps-output-expression expr port)
262   (display (eval expr output-ps-module) port))
263
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; Safe definitions utility
266 (define safe-objects (list))
267
268 (define-macro (define-safe-public arglist . body)
269   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
270 The syntax is the same as `define*-public'."
271   (define (get-symbol arg)
272     (if (pair? arg)
273         (get-symbol (car arg))
274         arg))
275   (let ((safe-symbol (get-symbol arglist)))
276     `(begin
277        (define*-public ,arglist
278          ,@body)
279        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
280                                 safe-objects))
281        ,safe-symbol)))
282
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;; init pitch system
285
286 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
287
288
289
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;; other files.
292
293
294 (define
295   init-scheme-files
296   '("lily-library.scm"
297             "file-cache.scm"
298             "define-event-classes.scm"
299             "define-music-types.scm"
300             "output-lib.scm"
301             "c++.scm"
302             "chord-ignatzek-names.scm"
303             "chord-entry.scm"
304             "chord-generic-names.scm"
305             "stencil.scm"
306             "markup.scm"
307             "music-functions.scm"
308             "part-combiner.scm"
309             "autochange.scm"
310             "define-music-properties.scm"
311             "auto-beam.scm"
312             "chord-name.scm"
313
314             "parser-ly-from-scheme.scm"
315             "ly-syntax-constructors.scm"
316             
317             "define-context-properties.scm"
318             "translation-functions.scm"
319             "script.scm"
320             "midi.scm"
321             "layout-beam.scm"
322             "parser-clef.scm"
323             "layout-slur.scm"
324             "font.scm"
325             "encoding.scm"
326             
327             "fret-diagrams.scm"
328             "define-markup-commands.scm"
329             "define-grob-properties.scm"
330             "define-grobs.scm"
331             "define-grob-interfaces.scm"
332             "define-stencil-commands.scm"
333             "titling.scm"
334             
335             "paper.scm"
336             "backend-library.scm"
337             "x11-color.scm"
338
339             ;; must be after everything has been defined
340             "safe-lily.scm"))
341
342
343 (for-each ly:load init-scheme-files)
344
345
346 (set! type-p-name-alist
347       `(
348         (,boolean-or-symbol? . "boolean or symbol")
349         (,boolean? . "boolean")
350         (,char? . "char")
351         (,grob-list? . "list of grobs")
352         (,hash-table? . "hash table")
353         (,input-port? . "input port")
354         (,integer? . "integer")
355         (,list? . "list")
356         (,ly:context? . "context")
357         (,ly:dimension? . "dimension, in staff space")
358         (,ly:dir? . "direction")
359         (,ly:duration? . "duration")
360         (,ly:grob? . "layout object")
361         (,ly:input-location? . "input location")
362         (,ly:moment? . "moment")
363         (,ly:music? . "music")
364         (,ly:pitch? . "pitch")
365         (,ly:translator? . "translator")
366         (,ly:font-metric? . "font metric")
367         (,ly:simple-closure? . "simple closure")
368         (,markup-list? . "list of markups")
369         (,markup? . "markup")
370         (,ly:music-list? . "list of music")
371         (,number-or-grob? . "number or grob")
372         (,number-or-string? . "number or string")
373         (,number-pair? . "pair of numbers")
374         (,number? . "number")
375         (,output-port? . "output port")   
376         (,pair? . "pair")
377         (,procedure? . "procedure")
378         (,rhythmic-location? . "rhythmic location")
379         (,scheme? . "any type")
380         (,string? . "string")
381         (,symbol? . "symbol")
382         (,vector? . "vector")))
383
384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 ;; timing
386
387 (define (profile-measurements)
388   (let* ((t (times))
389          (stats (gc-stats)))
390     
391     (list
392      (- (+ (tms:cutime t)
393            (tms:utime t))
394         (ly:assoc-get 'gc-time-taken stats))
395      
396      (ly:assoc-get 'total-cells-allocated  stats 0)
397      )))
398
399 (define (dump-profile base last this)
400   (let*
401       ((outname (format "~a.profile" (basename base ".ly")))
402        (diff (map (lambda (y) (apply - y)) (zip this last))))
403     
404     (ly:progress "\nWriting timing to ~a..." outname)
405     (format (open-file outname "w")
406             "time: ~a\ncells: ~a\n"
407             (car diff)
408             (cadr diff)
409             )))
410
411
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;; debug mem leaks
414
415 (define gc-dumping #f)
416 (define gc-protect-stat-count 0)
417
418 (define-public (dump-live-object-stats outfile)
419   (for-each
420    (lambda (x)
421      (format outfile "~a: ~a\n" (car x) (cdr x)))
422    (sort (gc-live-object-stats)
423          (lambda (x y)
424            (string<? (car x) (car y))))))
425
426 (define-public (dump-gc-protects)
427   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
428   (let* ((protects (sort
429                     (hash-table->alist (ly:protects))
430                     (lambda (a b)
431                       (< (object-address (car a))
432                          (object-address (car b))))))
433
434          (out-file-name (string-append
435                          "gcstat-" (number->string gc-protect-stat-count)
436                          ".scm"))
437          (outfile    (open-file  out-file-name  "w")))
438
439     (set! gc-dumping #t)
440     (display (format "Dumping GC statistics ~a...\n" out-file-name))
441     (display
442      (map (lambda (y)
443             (let ((x (car y))
444                   (c (cdr y)))
445               (display 
446                (format "~a (~a) = ~a\n" (object-address x) c x)
447                outfile)))
448           (filter
449            (lambda (x)
450              (not (symbol? (car x))))
451            protects))
452      outfile)
453
454     (format outfile "\nprotected symbols: ~a\n"
455             (length (filter symbol?  (map car protects))))
456     
457              
458
459     ;; (display (ly:smob-protects))
460     (newline outfile)
461     (if (defined? 'gc-live-object-stats)
462         (let* ((stats #f))
463           (display "Live object statistics: GC'ing\n")
464           (ly:reset-all-fonts)
465           (gc)
466           (gc)
467           (display "Asserting dead objects\n")
468           (ly:set-option 'debug-gc-assert-parsed-dead #t)
469           (gc)
470           (ly:set-option 'debug-gc-assert-parsed-dead #f)
471
472           (set! stats (gc-live-object-stats))
473           (display "Dumping live object statistics.\n")
474           (dump-live-object-stats outfile)))
475
476     (newline outfile)
477     (let*
478         ((stats (gc-stats)))
479       
480       (for-each
481        (lambda (sym)
482          (display
483           (format "~a ~a ~a\n"
484                   gc-protect-stat-count
485                   sym
486                   (let ((sym-stat (assoc sym stats)))
487                     (if sym-stat 
488                         (cdr sym-stat)
489                         "?")))
490           outfile))
491        '(protected-objects bytes-malloced cell-heap-size
492                            
493                            )))
494
495     (set! gc-dumping #f)
496     (close-port outfile)
497     
498     ))
499
500
501 (define (check-memory)
502   "read /proc/self to check up on memory use." 
503   (define (gulp-file name)
504     (let* ((file (open-input-file name))
505            (text (read-delimited "" file)))
506       (close file)
507       text))
508   (let*
509       ((stat (gulp-file "/proc/self/status"))
510        (lines (string-split stat #\newline))
511        (interesting (filter identity
512                             (map
513                              (lambda (l)
514                                (string-match "^VmData:[ \t]*([0-9]*) kB" l))
515                              lines)))
516        (mem (string->number (match:substring (car interesting) 1)))
517        )
518
519     
520     (display (format  "VMDATA: ~a\n" mem))
521     (display (gc-stats))
522     (if (> mem 100000)
523         (begin
524           (dump-gc-protects)
525           (raise 1)))
526     
527     ))
528
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
530
531
532 (define (multi-fork count)
533   (define (helper count acc)
534     (if (> count 0)
535       (let*
536           ((pid  (primitive-fork)))
537         (if (= pid 0)
538             (1- count)
539             (helper (1- count) (cons pid acc))))
540       acc))
541   (helper count '()))
542
543
544 (define-public (lilypond-main files)
545   "Entry point for LilyPond."
546
547
548   (eval-string (ly:command-line-code))
549
550   (if (ly:get-option 'help)
551       (begin
552         (ly:option-usage)
553         (exit 0)))
554
555   (if (ly:get-option 'show-available-fonts)
556       (begin
557         (ly:font-config-display-fonts)
558         (exit 0)
559         ))
560   
561   
562   (if (ly:get-option 'gui)
563       (gui-main files))
564
565   (if (null? files)
566       (begin
567         (ly:usage)
568         (exit 2)))
569
570   (if (ly:get-option 'read-file-list)
571       (set! files
572             (filter (lambda (s)
573                       (> (string-length s) 0))
574                     (apply append
575                            (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
576                                 files)))
577             ))
578   
579   (if (and (number? (ly:get-option 'job-count))
580            (>= (length files) (ly:get-option 'job-count)))
581       
582       (let*
583           ((count (ly:get-option 'job-count))
584            (split-todo (split-list files count)) 
585            (joblist (multi-fork count))
586            (errors '()))
587
588         (if (not (string-or-symbol? (ly:get-option 'log-file)))
589             (ly:set-option 'log-file "lilypond-multi-run"))
590         
591         (if (number? joblist)
592             (begin
593               (ly:set-option 'log-file (format "~a-~a"
594                                                (ly:get-option 'log-file) joblist))
595               (set! files (vector-ref split-todo joblist)))
596
597             (begin
598               (ly:progress "\nForking into jobs:  ~a\n" joblist)
599               (for-each
600                (lambda (pid)
601                  (let* ((stat (cdr (waitpid pid))))
602                    
603                    (if (not (= stat 0))
604                        (set! errors (acons (list-element-index joblist pid) stat errors)))))
605                joblist)
606
607               (for-each
608                (lambda (x)
609                  (let* ((job (car x))
610                         (state (cdr x))
611                         (logfile  (format "~a-~a.log"
612                                           (ly:get-option 'log-file) job))
613                         (log (ly:gulp-file logfile))
614                         (len (string-length log))
615                         (tail (substring  log (max 0 (- len 1024)))))
616
617                    (if (status:term-sig state)
618                        (ly:message "\n\n~a\n"
619                                    (format (_ "job ~a terminated with signal: ~a")
620                                            job
621                                            (status:term-sig state)))
622                        (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
623
624                errors)
625
626               (if (pair? errors)
627                   (ly:error "Children ~a exited with errors." (map car errors)))
628
629               ;; must overwrite individual entries
630               (if (ly:get-option 'dump-profile)
631                   (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
632
633             (exit (if (null? errors) 0 1))))))
634               
635            
636   (if (string-or-symbol? (ly:get-option 'log-file))
637       (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
638
639   
640   (let ((failed (lilypond-all files)))
641     (if (ly:get-option 'trace-scheme-coverage)
642         (begin
643           (coverage:show-all (lambda (f) (string-contains f "lilypond"))
644                              )))
645           
646     
647     (if (pair? failed)
648         (begin
649           (ly:error (_ "failed files: ~S") (string-join failed))
650           (exit 1))
651         (begin
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)))