cd.scm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (library (commands cd)
  2. (export cd
  3. change-directory)
  4. (import (except (rnrs base) error map)
  5. (only (guile)
  6. lambda* λ
  7. chdir
  8. simple-format)
  9. ;; receive form
  10. (srfi srfi-8)
  11. (alias)
  12. (shell-state)
  13. (commands pwd))
  14. ;; TODO: use previous result conditionally
  15. (define cd
  16. (lambda* (#:optional
  17. (filename #f)
  18. #:key
  19. ;; command interface
  20. (previous-result '())
  21. (shell-state default-shell-state)
  22. (silent #f))
  23. "Change the current working directory. Use FILENAME, if given,
  24. otherwise use first element of #:PREVIOUS-RESULT."
  25. (let ([actual-filename
  26. (if filename
  27. filename
  28. (car previous-result))])
  29. ;; (simple-format #t "actual filename: ~a\n" actual-filename)
  30. (chdir actual-filename)
  31. (let ([new-pwd (receive (result _) (pwd #:silent #t) (car result))])
  32. ;; (simple-format #t "new pwd: ~a\n" new-pwd)
  33. (values (list new-pwd)
  34. (update-shell-state shell-state
  35. 'current-working-directory
  36. new-pwd))))))
  37. (alias change-directory cd))