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