Code and doc cleanup
[opus_libre.git] / lib / init.scm
blob87c591e72e3f8403ba75b9d6a1155855549ac65b
1 ;------------------------------------------------------------------;\r
2 ; opus_libre -- init.scm                                           ;\r
3 ;                                                                  ;\r
4 ; (c) 2008-2010 Valentin Villenave <valentin@villenave.net>        ;\r
5 ;                                                                  ;\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
9 ;                                                                  ;\r
10 ;------------------------------------------------------------------;\r
12 ; Init file: mandatory variables and functions.\r
14 (use-modules\r
15  (ice-9 regex)\r
16  (ice-9 optargs)\r
17  (ice-9 rdelim)\r
18  (ice-9 popen))\r
21 (define-public (ly:debug-message string . rest)\r
22    (if (ly:get-option 'verbose)\r
23        (apply ly:message (cons string rest))))\r
25 ;; Base variables initialization ----------------------------------;\r
26 ;; (may be overriden later when parsing conf-file)\r
28 (define conf:lib-dir "lib")\r
30 ;; Filesystem browsing --------------------------------------------;\r
32 ;;;; The following function was retrieved from\r
33 ;;;; a mail posted by Russ McManus in 1998...\r
34 ;;;; http://sources.redhat.com/ml/guile/1998-07/msg00370.html\r
36 (define-public (find-files dir . arg-ls)\r
37   "List files in DIR, in alphabetical order.  Two optional arguments\r
38  are supported: a regexp filter, and a boolean that determines whether\r
39  subdirectories should be included (defaults to true)."\r
40   (let* ((n-args (length arg-ls))\r
41          (pred (cond ((= n-args 0)\r
42                       (lambda (file) #t))\r
43                      ((procedure? (list-ref arg-ls 0))\r
44                       (list-ref arg-ls 0))\r
45                      ((string? (list-ref arg-ls 0))\r
46                       (let ((rx (make-regexp (list-ref arg-ls 0)\r
47                                              ;; better use case-insensitive flag here\r
48                                              regexp/icase)))\r
49                         (lambda (file) (regexp-exec rx file))))\r
50                      (#t (error "bad predicate" (list-ref arg-ls 0)))))\r
51          (recurse? (if (>= n-args 2) (list-ref arg-ls 1) #t)))\r
52     (define (do-file file basename ret-ls)\r
53       (let* ((v (lstat file)))\r
54         (cond ((string=? basename ".") ret-ls)\r
55               ((string=? basename "..") ret-ls)\r
56               ((and (eq? (stat:type v) 'directory)\r
57                     recurse?)\r
58                (do-dir file ret-ls))\r
59               ((pred file) (cons file ret-ls))\r
60               (#t ret-ls))))\r
61     (define (do-dir dir-name ret-ls)\r
62       (let ((dir (opendir dir-name)))\r
63         (do ((file (readdir dir) (readdir dir)))\r
64             ((eof-object? file) ret-ls)\r
65           (set! ret-ls\r
66                 (do-file\r
67                  ;; (string) now only accepts chars,\r
68                  ;; use (string-append) instead\r
69                  (string-append dir-name "/" file) file ret-ls)))\r
70         (closedir dir)\r
71         ret-ls))\r
72     (sort (do-dir dir '()) string<?)))\r
74 ;; Automatic includes ---------------------------------------------;\r
75 (define-public (include-scm dir . numbered?)\r
76   "Load all Scheme files in DIR. If NUMBERED is set,\r
77  load only numbered files."\r
78   (let* ((regx (if numbered? "[0-9].*.scm$" ".scm$"))\r
79          (scm-files (find-files dir regx)))\r
80     (map (lambda (x)\r
81            (begin (ly:debug-message "Loading ~a..." x)\r
82                   (load x)))\r
83          scm-files)))\r
85 ;------------------------------------------------------------------;\r
86 ;\r