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