helper.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!r6rs
  2. ;;; helper.sls --- Helper procedures for (arguile lib private include)
  3. ;; Copyright (C) 2009, 2010 Andreas Rottmann <a.rottmann@gmx.at>
  4. ;; Copyright (C) 2009 Derick Eddington
  5. ;; This program is free software, you can redistribute it and/or
  6. ;; modify it under the terms of the MIT/X11 license.
  7. ;; You should have received a copy of the MIT/X11 license along with
  8. ;; this program. If not, see
  9. ;; <http://www.opensource.org/licenses/mit-license.php>.
  10. ;;; Commentary:
  11. ;;; Code:
  12. (library (arguile lib private include helper)
  13. (export include-file/aux)
  14. (import (rnrs)
  15. (arguile lib private include utils)
  16. (arguile lib private include filesys)
  17. (for (arguile lib private include compat) run (meta -1)))
  18. (define (error/conditions who msg irrts . cndts)
  19. (raise
  20. (apply condition
  21. (make-error)
  22. (make-who-condition who)
  23. (make-message-condition msg)
  24. (make-irritants-condition irrts)
  25. cndts)))
  26. (define (maybe-symbol->string thing)
  27. (cond ((symbol? thing) (symbol->string thing))
  28. (else thing)))
  29. (define (filespec->path name)
  30. (cond ((string? name) (list name))
  31. ((symbol? name) (list (string-append (symbol->string name) ".scm")))
  32. ((pair? name)
  33. (append
  34. (if (pair? (car name))
  35. (map maybe-symbol->string (car name))
  36. (list (maybe-symbol->string (car name))))
  37. (list (cond ((symbol? (cadr name))
  38. (string-append (symbol->string (cadr name)) ".scm"))
  39. (else
  40. (cadr name))))))
  41. (else (list name))))
  42. (define (include-file/aux who ctxt path transformer)
  43. (let* ((relpath (filespec->path path))
  44. (filename (find-file relpath (library-search-paths))))
  45. (unless filename
  46. (error who
  47. "cannot find library file in search paths"
  48. relpath
  49. (library-search-paths)))
  50. (with-exception-handler
  51. (lambda (ex)
  52. (error/conditions who
  53. "error while trying to include"
  54. (list filename)
  55. (if (condition? ex)
  56. ex
  57. (make-irritants-condition (list ex)))))
  58. (lambda ()
  59. (call-with-input-file filename
  60. (lambda (port)
  61. (let loop ((x (read-annotated port)) (forms '()))
  62. (if (eof-object? x)
  63. #`(stale-when (or (not (file-exists? #,filename))
  64. (> (file-mtime #,filename)
  65. #,(file-mtime filename)))
  66. #,@(datum->syntax ctxt (reverse forms)))
  67. (loop (read-annotated port)
  68. (cons (transformer x) forms))))))))))
  69. )
  70. ;; Local Variables:
  71. ;; scheme-indent-styles: ((stale-when 1))
  72. ;; End: