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