1 ;------------------------------------------------------------------;
\r
2 ; opus_libre -- 20-readconf.scm ;
\r
4 ; (c) 2008-2010 Valentin Villenave <valentin@villenave.net> ;
\r
6 ; opus_libre is a free framework for GNU LilyPond: you may ;
\r
7 ; redistribute it and/or modify it under the terms of the GNU ;
\r
8 ; General Public License, version 3 or later: gnu.org/licenses ;
\r
10 ;------------------------------------------------------------------;
\r
12 ; Load configuration files and set variables.
\r
14 (define conf:conf-prefix "conf")
\r
15 (define conf:ly-prefix "ly")
\r
16 (define conf:conf-file "etc/ly.conf")
\r
18 (define (parse-lines-in port prefix)
\r
19 "Read a file line by line and look for defs."
\r
20 (let ((line (read-line port)))
\r
24 (set! line (regexp-substitute/global #f "#" line 'pre))
\r
25 ;; Do we have a = sign, and where?
\r
26 (let ((eq (string-index line #\=)))
\r
27 (if (not (boolean? eq))
\r
28 (let* ((var (string-take line (- eq 1)))
\r
29 (val (string-drop line (+ eq 1)))
\r
30 ;; LilyPond variables are camelCased instead of hyphen-ated
\r
31 (lyvar (string->symbol
\r
32 (regexp-substitute/global #f "-[a-z]" var 'pre
\r
34 (string-drop (string-upcase
\r
35 (match:substring m)) 1)) 'post))))
\r
36 (if (not (string=? prefix ""))
\r
37 (set! var (string-append prefix ":" var)))
\r
38 ;; Native .ly definitions take precedence over .conf defs
\r
39 (if (defined-string? lyvar)
\r
40 (set! val (string-append "\""
\r
41 (ly:parser-lookup parser lyvar) "\"")))
\r
43 (format #f "(define-public ~a ~a)" var val)))))
\r
44 ;; then move on to the next line, until EOF.
\r
45 (parse-lines-in port prefix))
\r
46 (close-port port))))
\r
48 (define (parse-def-file file prefix)
\r
49 "Read FILE and turn all definitions into Scheme values."
\r
50 (let ((port (open-input-file file))
\r
51 ;; we don't want ly:prefixed variables, use conf: instead.
\r
52 (prefix (if (eq? conf:ly-prefix prefix) conf:conf-prefix prefix)))
\r
53 (if (ly:get-option 'debug-messages)
\r
54 (ly:message "Parsing configuration file ~a..." file))
\r
55 (parse-lines-in port prefix)))
\r
57 (define (parse-def-dir dir . prefix)
\r
58 "Parse all .conf files found in DIR."
\r
59 (let ((def-files (find-files dir ".conf$" #f)))
\r
62 (if (string? prefix) prefix
\r
64 (regexp-substitute/global #f "/+" x 'post)
\r
69 (let ((usr-conf (if (defined-string? 'conf:local-conf-dir)
\r
70 (let ((usr-dir (string-append score-dir "/" conf:local-conf-dir)))
\r
71 (if (exists? usr-dir)
\r
73 (if (ly:get-option 'debug-messages)
\r
74 (ly:message "Local configuration dir found in ~a" usr-dir))
\r
77 (if (ly:get-option 'debug-messages)
\r
78 (ly:message "No local overrides found: ~a does not exist." usr-dir))
\r
81 (parse-def-file conf:conf-file conf:conf-prefix)
\r
82 (parse-def-dir conf:conf-dir)
\r
83 (set! conf:local-conf-dir usr-conf)
\r
84 (parse-def-dir conf:local-conf-dir)))
\r