Created
February 10, 2009 09:11
-
-
Save dhess/61318 to your computer and use it in GitHub Desktop.
A Chicken Scheme macro for temporarily changing the current working directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; Preserve the current working directory, then change to a new directory | |
| ;;; DIR and evaluate THUNK. Finally, restore the working directory and | |
| ;;; return THUNK's value. | |
| ;;; | |
| ;;; Uses dynamic-wind, so it's signal- and continuation-safe. | |
| ;;; | |
| ;;; Works with Chicken Scheme. | |
| (use posix) | |
| (define (with-current-directory dirname thunk) | |
| (let ((cwd (current-directory))) | |
| (dynamic-wind | |
| (lambda () (change-directory dirname)) | |
| thunk | |
| (lambda () (change-directory cwd))))) | |
| ;;; Example: should print "/" and return 5. | |
| (with-current-directory "/" | |
| (lambda () | |
| (print (current-directory)) | |
| 3 | |
| 4 | |
| 5)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment