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