1 ;------------------------------------------------------------------;
2 ; opus_libre -- 30-readconf.scm ;
4 ; (c) 2008-2010 Valentin Villenave <valentin@villenave.net> ;
6 ; opus_libre is a free framework for GNU LilyPond: you may ;
7 ; redistribute it and/or modify it under the terms of the GNU ;
8 ; General Public License, version 3 or later: gnu.org/licenses ;
10 ;------------------------------------------------------------------;
12 ; Load configuration files and set variables.
14 (define conf:conf-prefix "conf")
15 (define conf:ly-prefix "ly")
16 (define conf:conf-file "etc/ly.conf")
18 (define (parse-lines-in port prefix)
19 "Read a file line by line and look for defs."
20 (let ((line (read-line port)))
24 (set! line (regexp-substitute/global #f
25 (if prefix "#" "%") line 'pre))
26 ;; Do we have a = sign, and where?
27 (let ((eqchar-index (string-index line #\=)))
29 (let* ((var (string-take line eqchar-index))
30 (val (string-drop line (+ eqchar-index 1)))
31 ;; LilyPond variables are camelCased instead of hyphen-ated
32 (lyvar (string->symbol
33 (regexp-substitute/global #f "-[a-z]" var 'pre
35 (string-drop (string-upcase
36 (match:substring m)) 1)) 'post))))
38 (if (or (string-any (char-set #\{ #\# #\< #\\) var)
39 (string-any (char-set #\{ #\# #\< #\\) val)
40 (string-every char-set:whitespace val))
42 ;; Native .ly definitions take precedence over .conf defs
44 (if (defined-string? lyvar)
45 (set! val (string-append
46 "\"" (ly:parser-lookup parser lyvar) "\"")))
47 (if (not (string=? prefix ""))
48 (set! var (string-append prefix ":" var)))))
49 (if val (let ((str (format #f "(define-public ~a ~a)" var val)))
50 (eval-string str))))))
51 ;; then move on to the next line, until EOF.
52 (parse-lines-in port prefix))
55 (define (parse-def-file file prefix)
56 "Read FILE and turn all definitions into Scheme values."
57 (let ((port (open-input-file file))
58 ;; we don't want ly:prefixed variables, use conf: instead.
59 (prefix (if (equal? conf:ly-prefix prefix) conf:conf-prefix prefix)))
60 (ly:debug-message "Parsing configuration file ~a..." file)
61 (parse-lines-in port prefix)))
63 (define (parse-def-dir dir . prefix)
64 "Parse all .conf files found in DIR."
65 (let ((def-files (find-files dir ".conf$" #f)))
68 (if (string? prefix) prefix
70 (regexp-substitute/global #f "/+" x 'post)
75 ;; "Read all conf files: first in the global conf dir, then in a
76 ;; dedicated subdir of the score dir, or if none can be found, in
77 ;; the score dir itself. This allows for local overrides to be
78 ;; loaded early in the compilation process."
79 (let ((local-score (string-append score-dir "/score.ly"))
80 (usr-conf (if (defined-string? 'conf:local-conf-dir)
81 (let ((usr-dir (string-append score-dir "/" conf:local-conf-dir)))
84 (ly:debug-message "Local configuration dir found in ~a" usr-dir)
87 (ly:debug-message "~a does not exist; looking for overrides in parent directory."
91 (parse-def-file conf:conf-file conf:conf-prefix)
92 (parse-def-dir conf:conf-dir)
93 (if (exists? local-score)
94 (if (not (ly:get-option 'skip-local-score-file))
95 (begin (ly:debug-message "Parsing local definitions in ~a" local-score)
96 (parse-def-file local-score #f))))
97 ;; Set the conf:local-conf-dir variable, that will
98 ;; be used later for macros, themes, local overrides etc.
99 (set! conf:local-conf-dir usr-conf)
100 (parse-def-dir conf:local-conf-dir)))