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