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