]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
o * lily/spacing-engraver.cc (stop_translation_timestep): directly
[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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define (define-scheme-options)
10   (for-each (lambda (x)
11               (ly:add-option (car x) (cadr x) (caddr x)))
12           
13             '((point-and-click #t "use point & click")
14               (midi-debug #f "generate human readable MIDI")
15               (internal-type-checking #f "check every property assignment for types")
16               (parse-protect #t    "continue when finding errors in inline
17 scheme are caught in the parser. If off, halt 
18 on errors, and print a stack trace.")
19               (profile-property-accesses #f "keep statistics of get_property() calls.")
20               (old-relative #f
21                             "relative for simultaneous music works
22 similar to chord syntax")
23               (object-keys #f
24                            "experimental mechanism for remembering tweaks")
25               (resolution 101 "resolution for generating bitmaps")
26               (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
27               (preview-include-book-title #t "include book-titles in preview images.")
28               (gs-font-load #f
29                             "load fonts via Ghostscript.")
30               (gui #f "running from gui; redirect stderr to log file")
31               (delete-intermediate-files #f
32                                          "delete unusable PostScript files")
33               (verbose #f "value for the --verbose flag")
34               (ttf-verbosity 0
35                            "how much verbosity for TTF font embedding?")
36               (debug-gc #f
37                         "dump GC protection info"))))
38
39 ;; FIXME: stray statement
40 (define-scheme-options)
41
42 (if (defined? 'set-debug-cell-accesses!)
43     (set-debug-cell-accesses! #f))
44
45 ;(set-debug-cell-accesses! 1000)
46
47 (use-modules (ice-9 regex)
48              (ice-9 safe)
49              (ice-9 optargs)
50              (oop goops)
51              (srfi srfi-1)
52              (srfi srfi-13)
53              (srfi srfi-14))
54
55
56 ;; my display
57 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
58
59 (define-public (print . args)
60   (apply format (cons (current-output-port) args)))
61
62
63 ;;; General settings
64 ;;; debugging evaluator is slower.  This should
65 ;;; have a more sensible default.
66
67 (if (ly:get-option 'verbose)
68     (begin
69       (debug-enable 'debug)
70       (debug-enable 'backtrace)
71       (read-enable 'positions)))
72
73
74 (define-public tex-backend?
75   (member (ly:output-backend) '("texstr" "tex")))
76
77 (define-public parser #f)
78
79 (define-public (lilypond-version)
80   (string-join
81    (map (lambda (x) (if (symbol? x)
82                         (symbol->string x)
83                         (number->string x)))
84         (ly:version))
85    "."))
86
87
88
89 ;; cpp hack to get useful error message
90 (define ifdef "First run this through cpp.")
91 (define ifndef "First run this through cpp.")
92
93 ;; gettext wrapper for guile < 1.7.2
94 (if (defined? 'gettext)
95     (define-public _ gettext)
96     (define-public _ ly:gettext))
97
98 (define-public (ly:load x)
99   (let* ((file-name (%search-load-path x)))
100     (if (ly:get-option 'verbose)
101         (ly:progress "[~A" file-name))
102     (primitive-load file-name)
103     (if (ly:get-option 'verbose)
104         (ly:progress "]"))))
105
106 (define-public TEX_STRING_HASHLIMIT 10000000)
107
108 ;; Cygwin
109 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
110 ;;
111 ;; Debian
112 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
113 ;;
114 ;; Mingw
115 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
116 ;;
117
118 ;; ugh, code dup.
119 (define-public PLATFORM
120   (string->symbol
121    (string-downcase
122     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
123
124 (define-public DOS
125   (let ((platform (string-tokenize
126                    (vector-ref (uname) 0) char-set:letter+digit)))
127     (if (null? (cdr platform)) #f
128         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
129
130 (case PLATFORM
131   ((windows)
132    (define native-getcwd getcwd)
133    (define (slashify x)
134      (if (string-index x #\\)
135          x
136          (string-regexp-substitute
137           "//*" "/"
138           (string-regexp-substitute "\\\\" "/" x))))
139    ;; FIXME: this prints a warning.
140   (define-public (ly-getcwd)
141      (slashify (native-getcwd))))
142   (else (define-public ly-getcwd getcwd)))
143
144 (define-public (is-absolute? file-name)
145   (let ((file-name-length (string-length file-name)))
146     (if (= file-name-length 0)
147         #f
148         (or (eq? (string-ref file-name 0) #\/)
149             (and (eq? PLATFORM 'windows)
150                  (> file-name-length 2)
151                  (eq? (string-ref file-name 1) #\:)
152                  (eq? (string-ref file-name 2) #\/))))))
153
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155
156 (define (type-check-list location signature arguments)
157   "Typecheck a list of arguments against a list of type
158 predicates. Print a message at LOCATION if any predicate failed."
159   (define (recursion-helper signature arguments count) 
160     (define (helper pred? arg count) 
161       (if (not (pred? arg))
162
163           (begin
164             (ly:input-message
165              location
166              (format
167               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
168               count (type-name pred?) arg))
169             #f)
170           #t))
171
172     (if (null? signature)
173         #t
174         (and (helper (car signature) (car arguments) count)
175              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
176   (recursion-helper signature arguments 1))
177
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179 ;;  output
180
181
182 ;;(define-public (output-framework) (write "hello\n"))
183
184 (define output-tex-module
185   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
186 (define output-ps-module
187   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
188
189 (define-public (ps-output-expression expr port)
190   (display (eval expr output-ps-module) port))
191
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; Safe definitions utility
194 (define safe-objects (list))
195
196 (define-macro (define-safe-public arglist . body)
197   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
198 The syntax is the same as `define*-public'."
199   (define (get-symbol arg)
200     (if (pair? arg)
201         (get-symbol (car arg))
202         arg))
203   (let ((safe-symbol (get-symbol arglist)))
204     `(begin
205        (define*-public ,arglist
206          ,@body)
207        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
208                                 safe-objects))
209        ,safe-symbol)))
210
211
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 ;; other files.
214
215 (for-each ly:load
216           ;; load-from-path
217           '("lily-library.scm"
218             "file-cache.scm"
219             "define-music-types.scm"
220             "output-lib.scm"
221             "c++.scm"
222             "chord-ignatzek-names.scm"
223             "chord-entry.scm"
224             "chord-generic-names.scm"
225             "stencil.scm"
226             "markup.scm"
227             "bass-figure.scm"
228             "music-functions.scm"
229             "part-combiner.scm"
230             "autochange.scm"
231             "define-music-properties.scm"
232             "auto-beam.scm"
233             "chord-name.scm"
234
235             "ly-from-scheme.scm"
236             
237             "define-context-properties.scm"
238             "translation-functions.scm"
239             "script.scm"
240             "midi.scm"
241             "beam.scm"
242             "clef.scm"
243             "slur.scm"
244             "font.scm"
245             "encoding.scm"
246             
247             "fret-diagrams.scm"
248             "define-markup-commands.scm"
249             "define-grob-properties.scm"
250             "define-grobs.scm"
251             "define-grob-interfaces.scm"
252             "define-stencil-commands.scm"
253             "page-layout.scm"
254             "titling.scm"
255             
256             "paper.scm"
257             "backend-library.scm"
258             "x11-color.scm"
259
260             ;; must be after everything has been defined
261             "safe-lily.scm"))
262
263
264 (set! type-p-name-alist
265       `(
266         (,boolean-or-symbol? . "boolean or symbol")
267         (,boolean? . "boolean")
268         (,char? . "char")
269         (,grob-list? . "list of grobs")
270         (,hash-table? . "hash table")
271         (,input-port? . "input port")
272         (,integer? . "integer")
273         (,list? . "list")
274         (,ly:context? . "context")
275         (,ly:dimension? . "dimension, in staff space")
276         (,ly:dir? . "direction")
277         (,ly:duration? . "duration")
278         (,ly:grob? . "layout object")
279         (,ly:input-location? . "input location")
280         (,ly:moment? . "moment")
281         (,ly:music? . "music")
282         (,ly:pitch? . "pitch")
283         (,ly:translator? . "translator")
284         (,ly:font-metric? . "font metric")
285         (,markup-list? . "list of markups")
286         (,markup? . "markup")
287         (,ly:music-list? . "list of music")
288         (,number-or-grob? . "number or grob")
289         (,number-or-string? . "number or string")
290         (,number-pair? . "pair of numbers")
291         (,number? . "number")
292         (,output-port? . "output port")   
293         (,pair? . "pair")
294         (,procedure? . "procedure") 
295         (,scheme? . "any type")
296         (,string? . "string")
297         (,symbol? . "symbol")
298         (,vector? . "vector")))
299
300
301 ;; debug mem leaks
302
303 (define gc-protect-stat-count 0)
304 (define-public (dump-gc-protects)
305   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
306   (let* ((protects (sort
307                     (hash-table->alist (ly:protects))
308                     (lambda (a b)
309                       (< (object-address (car a))
310                          (object-address (car b))))))
311
312          (out-file-name (string-append
313                          "gcstat-" (number->string gc-protect-stat-count)
314                          ".scm"))
315          (outfile    (open-file  out-file-name  "w")))
316
317     (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
318     (display
319      (filter
320       (lambda (x) (not (symbol? x))) 
321       (map (lambda (y)
322              (let ((x (car y))
323                    (c (cdr y)))
324
325                (string-append
326                 (string-join
327                  (map object->string (list (object-address x) c x))
328                  " ")
329                 "\n")))
330            protects))
331      outfile)
332
333     (if (defined? 'gc-live-object-stats)
334         (let* ((stats #f))
335           (display "Live object statistics: GC'ing\n")
336           (gc)
337           (gc)
338           
339           (set! stats (gc-live-object-stats))
340           (display "Dumping live object statistics.\n")
341           
342           (for-each
343            (lambda (x)
344              (format outfile "~a: ~a\n" (car x) (cdr x)))
345            (sort (gc-live-object-stats)
346                  (lambda (x y)
347                    (string<? (car x) (car y)))))))))
348
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 (define-public (lilypond-main files)
351   "Entry point for LilyPond."
352
353   (define (no-files-handler)
354     (ly:usage)
355     (exit 2))
356   
357
358   (if (null? files)
359       (no-files-handler))
360
361   (let ((failed (lilypond-all files)))
362     (if (pair? failed)
363         (begin
364           (ly:error (_ "failed files: ~S") (string-join failed))
365           (exit 1))
366         (begin
367           ;; HACK: be sure to exit with single newline
368           (ly:message "")
369           (exit 0)))))
370
371 (define-public (lilypond-all files)
372   (let* ((failed '())
373          (handler (lambda (key failed-file)
374                     (set! failed (append (list failed-file) failed)))))
375     (for-each
376      (lambda (x)
377        (lilypond-file handler x)
378        (ly:clear-anonymous-modules))
379      files)
380     failed))
381
382 (define (lilypond-file handler file-name)
383   (catch 'ly-file-failed
384          (lambda () (ly:parse-file file-name))
385          (lambda (x . args) (handler x file-name)))
386
387   (if (ly:get-option 'debug-gc)
388       (dump-gc-protects)))
389
390 (use-modules (scm editor))
391
392 (define-public (running-from-gui?)
393   (let ((have-tty? (isatty? (current-input-port))))
394     ;; If no TTY and not using safe, assume running from GUI.
395     (cond
396      ((eq? PLATFORM 'windows)
397       ;; Always write to .log file.
398       (if DOS #t
399       ;; This only works for i586-mingw32msvc-gcc -mwindows
400       (not (string-match "standard input"
401                          (format #f "~S" (current-input-port))))))
402      ;; FIXME: using -dgui would be nice, but it does not work
403      ((eq? PLATFORM 'foo-windows)
404       (ly:get-option 'gui))
405      ((eq? PLATFORM 'darwin) #f)
406      (else
407       (not have-tty?)))))
408
409 (define-public (gui-main files)
410   (if (null? files)
411       (gui-no-files-handler))
412   (let* ((base (basename (car files) ".ly"))
413          (log-name (string-append base ".log")))
414     (if (not (running-from-gui?))
415         (ly:message (_ "Redirecting output to ~a...") log-name))
416     (ly:stderr-redirect log-name "w")
417     (ly:message "# -*-compilation-*-")
418     (let ((failed (lilypond-all files)))
419       (if (pair? failed)
420           (begin
421             ;; ugh
422             (ly:stderr-redirect "foo" "r")
423             (system (get-editor-command log-name 0 0 0))
424             (ly:error (_ "failed files: ~S") (string-join failed))
425             ;; not reached?
426             (exit 1))
427           (exit 0)))))
428
429 (define (gui-no-files-handler)
430   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
431          ;; FIXME: soft-code, localize
432          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
433          (cmd (get-editor-command welcome-ly 0 0 0)))
434     (ly:message (_ "Invoking `~a'...") cmd)
435     (system cmd)
436     (exit 1)))
437
438 (or (not (running-from-gui?))
439     (ly:get-option 'safe)
440     (define lilypond-main gui-main))