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