]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
(PLATFORM, slashify): Remove double slashes.
[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 (define-public PLATFORM
90   (string->symbol
91    (string-downcase
92     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
93
94 (case PLATFORM
95   ((windows)
96    (define native-getcwd getcwd)
97    (define (slashify x)
98      (if (string-index x #\\)
99          x
100          (string-regexp-substitute
101           "//*" "/"
102           (string-regexp-substitute "\\\\" "/" x))))
103    ;; FIXME: this prints a warning.
104   (define-public (ly-getcwd)
105      (slashify (native-getcwd))))
106   (else (define-public ly-getcwd getcwd)))
107
108 (define-public (is-absolute? file-name)
109   (let ((file-name-length (string-length file-name)))
110     (if (= file-name-length 0)
111         #f
112         (or (eq? (string-ref file-name 0) #\/)
113             (and (eq? PLATFORM 'windows)
114                  (> file-name-length 2)
115                  (eq? (string-ref file-name 1) #\:)
116                  (eq? (string-ref file-name 2) #\/))))))
117
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119
120 (define (type-check-list location signature arguments)
121   "Typecheck a list of arguments against a list of type
122 predicates. Print a message at LOCATION if any predicate failed."
123   (define (recursion-helper signature arguments count) 
124     (define (helper pred? arg count) 
125       (if (not (pred? arg))
126
127           (begin
128             (ly:input-message
129              location
130              (format
131               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
132               count (type-name pred?) arg))
133             #f)
134           #t))
135
136     (if (null? signature)
137         #t
138         (and (helper (car signature) (car arguments) count)
139              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
140   (recursion-helper signature arguments 1))
141
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;;  output
144
145
146 ;;(define-public (output-framework) (write "hello\n"))
147
148 (define output-tex-module
149   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
150 (define output-ps-module
151   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
152
153 (define-public (ps-output-expression expr port)
154   (display (eval expr output-ps-module) port))
155
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Safe definitions utility
158 (define safe-objects (list))
159
160 (define-macro (define-safe-public arglist . body)
161   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
162 The syntax is the same as `define*-public'."
163   (define (get-symbol arg)
164     (if (pair? arg)
165         (get-symbol (car arg))
166         arg))
167   (let ((safe-symbol (get-symbol arglist)))
168     `(begin
169        (define*-public ,arglist
170          ,@body)
171        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
172                                 safe-objects))
173        ,safe-symbol)))
174
175
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177 ;; other files.
178
179 (for-each ly:load
180           ;; load-from-path
181           '("lily-library.scm"
182             "file-cache.scm"
183             "define-music-types.scm"
184             "output-lib.scm"
185             "c++.scm"
186             "chord-ignatzek-names.scm"
187             "chord-entry.scm"
188             "chord-generic-names.scm"
189             "stencil.scm"
190             "markup.scm"
191             "bass-figure.scm"
192             "music-functions.scm"
193             "part-combiner.scm"
194             "define-music-properties.scm"
195             "auto-beam.scm"
196             "chord-name.scm"
197
198             "ly-from-scheme.scm"
199             
200             "define-context-properties.scm"
201             "translation-functions.scm"
202             "script.scm"
203             "midi.scm"
204             "beam.scm"
205             "clef.scm"
206             "slur.scm"
207             "font.scm"
208             "encoding.scm"
209             
210             "fret-diagrams.scm"
211             "define-markup-commands.scm"
212             "define-grob-properties.scm"
213             "define-grobs.scm"
214             "define-grob-interfaces.scm"
215             "define-stencil-commands.scm"
216             "page-layout.scm"
217             "titling.scm"
218             
219             "paper.scm"
220             "backend-library.scm"
221             "x11-color.scm"
222
223             ;; must be after everything has been defined
224             "safe-lily.scm"))
225
226
227 (set! type-p-name-alist
228       `(
229         (,boolean-or-symbol? . "boolean or symbol")
230         (,boolean? . "boolean")
231         (,char? . "char")
232         (,grob-list? . "list of grobs")
233         (,hash-table? . "hash table")
234         (,input-port? . "input port")
235         (,integer? . "integer")
236         (,list? . "list")
237         (,ly:context? . "context")
238         (,ly:dimension? . "dimension, in staff space")
239         (,ly:dir? . "direction")
240         (,ly:duration? . "duration")
241         (,ly:grob? . "layout object")
242         (,ly:input-location? . "input location")
243         (,ly:moment? . "moment")
244         (,ly:music? . "music")
245         (,ly:pitch? . "pitch")
246         (,ly:translator? . "translator")
247         (,ly:font-metric? . "font metric")
248         (,markup-list? . "list of markups")
249         (,markup? . "markup")
250         (,ly:music-list? . "list of music")
251         (,number-or-grob? . "number or grob")
252         (,number-or-string? . "number or string")
253         (,number-pair? . "pair of numbers")
254         (,number? . "number")
255         (,output-port? . "output port")   
256         (,pair? . "pair")
257         (,procedure? . "procedure") 
258         (,scheme? . "any type")
259         (,string? . "string")
260         (,symbol? . "symbol")
261         (,vector? . "vector")))
262
263
264 ;; debug mem leaks
265
266 (define gc-protect-stat-count 0)
267 (define-public (dump-gc-protects)
268   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
269   (let* ((protects (sort
270                     (hash-table->alist (ly:protects))
271                     (lambda (a b)
272                       (< (object-address (car a))
273                          (object-address (car b))))))
274          (out-file-name (string-append
275                          "gcstat-" (number->string gc-protect-stat-count)
276                          ".scm"))
277          (outfile    (open-file  out-file-name  "w")))
278
279     (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
280     (display
281      (filter
282       (lambda (x) (not (symbol? x))) 
283       (map (lambda (y)
284              (let ((x (car y))
285                    (c (cdr y)))
286
287                (string-append
288                 (string-join
289                  (map object->string (list (object-address x) c x))
290                  " ")
291                 "\n")))
292            protects))
293      outfile)))
294
295 (define-public (tweak-grob-property grob sym val)
296   (set! (ly:grob-property grob sym) val))
297
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 (define-public (lilypond-main files)
300   "Entry point for LilyPond."
301
302   (if (null? files)
303       (no-files-handler))
304
305   (let ((failed (lilypond-all files)))
306     (if (pair? failed)
307         (begin
308           (ly:error (_ "failed files: ~S") (string-join failed))
309           (exit 1))
310         (begin
311           ;; HACK: be sure to exit with single newline
312           (ly:message "")
313           (exit 0)))))
314
315 (define (no-files-handler)
316   (ly:usage)
317   (exit 2))
318
319 (define-public (lilypond-all files)
320   (let* ((failed '())
321          (handler (lambda (key failed-file)
322                     (set! failed (append (list failed-file) failed)))))
323     ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
324     (for-each (lambda (x) (lilypond-file handler x)) files)
325     failed))
326
327 (define (lilypond-file handler file-name)
328   (catch 'ly-file-failed
329          (lambda () (ly:parse-file file-name))
330          (lambda (x . args) (handler x file-name)))
331
332   (if #f
333       (dump-gc-protects)))
334
335 (use-modules (scm editor))
336
337 (define (running-from-gui?)
338   (let ((have-tty? (isatty? (current-input-port))))
339     ;; If no TTY and not using safe, assume running from GUI.
340     (cond
341      ((eq? PLATFORM 'windows)
342       ;; This only works for i586-mingw32msvc-gcc -mwindows
343       (not (string-match "standard input"
344                          (format #f "~S" (current-input-port)))))
345      ((eq? PLATFORM 'darwin) #f)
346      (else
347       (not have-tty?)))))
348
349 (define-public (gui-main files)
350   (if (null? files) (gui-no-files-handler))
351   (let* ((base (basename (car files) ".ly"))
352          (log-name (string-append base ".log")))
353     (if (not (running-from-gui?))
354         (ly:message (_ "Redirecting output to ~a...") log-name))
355     (ly:stderr-redirect log-name "w")
356     (ly:message "# -*-compilation-*-")
357     (let ((failed (lilypond-all files)))
358       (if (pair? failed)
359           (begin
360             ;; ugh
361             (ly:stderr-redirect "foo" "r")
362             (system (get-editor-command log-name 0 0))
363             (ly:error (_ "failed files: ~S") (string-join failed))
364             ;; not reached?
365             (exit 1))
366           (exit 0)))))
367
368 (define (gui-no-files-handler)
369   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
370          ;; FIXME: soft-code, localize
371          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
372          (cmd (get-editor-command welcome-ly 0 0)))
373     (ly:message (_ "Invoking `~a'...") cmd)
374     (system cmd)
375     (exit 1)))
376
377 (or (not (running-from-gui?))
378     (ly:get-option 'safe)
379     (define lilypond-main gui-main))