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