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