lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "include" music-function


From: Jan-Peter Voigt
Subject: Re: "include" music-function
Date: Fri, 06 Jan 2012 15:22:51 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.24) Gecko/20111108 Lightning/1.0b2 Thunderbird/3.1.16

Am 06.01.2012 10:17, schrieb Jan-Peter Voigt:
Am 06.01.2012 09:35, schrieb Jan-Peter Voigt:
Hello David,

the \sourcefilename hint is helpful! Thank you!
If I use this in my function and do a ly:parser-clone, the location is up to date:
...
Now I will look, if this is working also with ly:parser-include-string while resetting filename and fileline at the end, so that definitions in the included file get into the current parser.


... yes it does:

--snip--
#(define-public includeLocal (define-music-function (parser location file)(string?)
    (let ((outname (format "~A.ly" (ly:parser-output-name parser)))
          (locname (car (ly:input-file-line-char-column location)))
          (locpos (cadr (ly:input-file-line-char-column location))))
(if (or (string=? outname locname)(string-suffix? outname locname))
             (begin
(ly:parser-include-string parser (format "\\sourcefilename \"~A\" \\sourcefileline 0\n" (ly:find-file file)))
               (ly:parser-include-string parser (ly:gulp-file file))
(ly:parser-include-string parser (format " \\sourcefilename \"~A\" \\sourcefileline ~A\n" locname locpos))))
         (make-music 'SequentialMusic 'void #t))))
--snip--

... but if I include a file in an included file, it will not parse. I will investigate that for more info.
OK, now I had lunch and saw my own mistakes ...

Here are the two functions I implemented ... still a bit clumsy, but it works for now ... if you are interested *how* and *why* to use them, don't hesitate to mail me!

--snip--
\version "2.15.21"

#(define-public includeLocal (define-music-function (parser location file)(string?)
    (let ((outname (format "~A.ly" (ly:parser-output-name parser)))
          (locname (car (ly:input-file-line-char-column location)))
          (file (ly:find-file file)))
(if (or (string=? outname locname) (string-suffix? outname locname))
             (begin
               ;(ly:input-message location "include ~A" file)
(ly:parser-include-string parser (format "\\sourcefilename \"~A\" \\sourcefileline 0\n~A"
                   file (ly:gulp-file file))))
         )
         (make-music 'SequentialMusic 'void #t))))

% EXAMPLE
% include file from lilypond search path (like \include)
\includeLocal "test-local.ily"

#(use-modules (ice-9 regex))
#(define-public includePattern (define-music-function (parser location idir pattern)(string? string?)
    (let* ((normalize-list (lambda (path)
                     (let ((ret '()))
                          (for-each (lambda (e)
(set! ret (cond ((equal? e "..")(if (> (length ret) 1) (cdr ret) '())) ((equal? e ".") ret) (else `(,e ,@ret))))) path)
                          (reverse ret))))
(normalize-path (lambda (s) (string-join (normalize-list (string-split s #\/)) "/" 'infix)))
           (extract-path (lambda (location)
(let* ((loc (car (ly:input-file-line-char-column location)))
                            (dirmatch (string-match "(.*/).*" loc))
(dirname (if (regexp-match? dirmatch) (normalize-path (match:substring dirmatch 1)) "./")))
                           dirname
           )))

           (dirname (string-append (extract-path location) idir)))

(if (not (eq? #\. (string-ref dirname 0))) (set! dirname (normalize-path dirname)))
          (if (or (= (string-length dirname) 0)
(not (eq? #\/ (string-ref dirname (- (string-length dirname) 1)))))
              (set! dirname (string-append dirname "/")))
(if (or (not (file-exists? dirname)) (not (eq? 'directory (stat:type (stat dirname)))))
              (set! dirname #f))

          (if dirname (let* ((dir (opendir dirname))
                   (entry (readdir dir)))
                  (while (not (eof-object? entry))
                         (if (regexp-match? (string-match pattern entry))
                             (let ((file (string-append dirname entry)))
;(ly:input-message location "include ~A" file)
                                  (ly:parser-include-string parser
(format "\\sourcefilename \"~A\" \\sourcefileline 0\n~A" file (ly:gulp-file file)))))
                         (set! entry (readdir dir))
                  )
                  (closedir dir)
          ))
    )
    (make-music 'SequentialMusic 'void #t)))

% EXAMPLE
% param 1: file path of containing directory, relative to the including file!
% param 2: regular expression to match filenames
\includePattern "../test/scheme" "^.*\.ily$"
--snip--

Cheers,
Jan-Peter




reply via email to

[Prev in Thread] Current Thread [Next in Thread]