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