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