line-prefix.scm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. (declare (usual-integrations))
  21. (define (make-line-prefix-port port prefix)
  22. (make-port line-prefix-port-type
  23. (vector port prefix #t)))
  24. (define line-prefix-port-type
  25. (make-port-type
  26. `((write-char
  27. ,(lambda (port char)
  28. (let ((v (port/state port)))
  29. (let ((port* (vector-ref v 0)))
  30. (if (vector-ref v 2)
  31. (write-string (vector-ref v 1) port*))
  32. (let ((n (output-port/write-char port* char)))
  33. (vector-set! v 2 (char=? char #\newline))
  34. n)))))
  35. (x-size
  36. ,(lambda (port)
  37. (let ((v (port/state port)))
  38. (let ((port* (vector-ref v 0)))
  39. (let ((op (port/operation port* 'X-SIZE)))
  40. (and op
  41. (let ((n (op port*)))
  42. (and n
  43. (max (- n (string-length (vector-ref v 1)))
  44. 0)))))))))
  45. (column
  46. ,(lambda (port)
  47. (let ((v (port/state port)))
  48. (let ((port* (vector-ref v 0)))
  49. (let ((op (port/operation port* 'COLUMN)))
  50. (and op
  51. (let ((n (op port*)))
  52. (and n
  53. (max (- n (string-length (vector-ref v 1)))
  54. 0)))))))))
  55. (flush-output
  56. ,(lambda (port)
  57. (let ((v (port/state port)))
  58. (output-port/flush-output (vector-ref v 0)))))
  59. (discretionary-flush-output
  60. ,(lambda (port)
  61. (let ((v (port/state port)))
  62. (output-port/discretionary-flush (vector-ref v 0))))))
  63. #f))
  64. (define* ((pp-line-prefix prefix) object #:optional port . rest)
  65. (apply pp
  66. object
  67. (make-line-prefix-port (if (default-object? port)
  68. (current-output-port)
  69. port)
  70. prefix)
  71. rest))
  72. (define pp-comment (pp-line-prefix ";"))
  73. ;;; FBE: commented out and put at end of library
  74. ;;(set! repl:write-result-hash-numbers? #f)