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