Ports.xhtml 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Ports</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Reading-and-writing-whole-files.xhtml" title="Reading and writing whole files"/>
  10. <link rel="next" href="Format.xhtml" title="Formatted Output (Common-Lisp-style)"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Ports" epub:type="subchapter" id="Ports">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Ports</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Ports represent input and output devices.
  23. An input port is a Scheme object that can deliver data upon
  24. command, while an output port is a Scheme object that can
  25. accept data.
  26. </p>
  27. <p>Different <em class="firstterm">port types</em> operate on different data:
  28. </p>
  29. <div class="itemizedlist" epub:type="list">
  30. <ul class="itemizedlist" style="list-style-type: disc; ">
  31. <li class="listitem" epub:type="list-item">
  32. <p>A <em class="firstterm">textual port</em> supports reading or writing of individual
  33. characters from or to a backing store containing characters
  34. using <code class="literal">read-char</code> and <code class="literal">write-char</code> below, and it supports
  35. operations defined in terms of characters, such as <code class="literal">read</code> and
  36. <code class="literal">write</code>.
  37. </p>
  38. </li>
  39. <li class="listitem" epub:type="list-item">
  40. <p>A <em class="firstterm">binary port</em> supports reading or writing of individual
  41. bytes from or to a backing store containing bytes using
  42. <code class="literal">read-u8</code> and <code class="literal">write-u8</code> below, as well as operations defined
  43. in terms of bytes (integers in the range 0 to 255).
  44. </p>
  45. <p>All Kawa binary ports created by procedures documented here
  46. are also textual ports. Thus you can either read/write
  47. bytes as described above, or read/write
  48. characters whose scalar value is in the range 0 to 255
  49. (i.e. the Latin-1 character set), using <code class="literal">read-char</code> and <code class="literal">write-char</code>.
  50. </p>
  51. <p>A native binary port is a <code class="literal">java.io.InputStream</code>
  52. or <code class="literal">java.io.OutputStream</code> instance. These are not textual ports.
  53. You can use methods <code class="literal">read-u8</code> and <code class="literal">write-u8</code>,
  54. but not <code class="literal">read-char</code> and <code class="literal">write-char</code> on native binary ports.
  55. (The functions <code class="literal">input-port?</code>, <code class="literal">output-port?</code>, <code class="literal">binary-port?</code>,
  56. and <code class="literal">port?</code> all currently return false on native binary ports,
  57. but that may change.)
  58. </p>
  59. </li>
  60. </ul>
  61. </div>
  62. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872772224" class="indexterm"/> <code class="function">call-with-port</code> <em class="replaceable"><code>port</code></em> <em class="replaceable"><code>proc</code></em></p>
  63. <div class="blockquote">
  64. <blockquote class="blockquote">
  65. <p>The <code class="literal">call-with-port</code> procedure calls <em class="replaceable"><code>proc</code></em> with port as an
  66. argument. If <em class="replaceable"><code>proc</code></em> returns, then the port is closed automatically
  67. and the values yielded by the proc are returned.
  68. </p>
  69. <p>If <em class="replaceable"><code>proc</code></em> does not return, then the port must not be closed
  70. automatically unless it is possible to prove that the port
  71. will never again be used for a read or write operation.
  72. </p>
  73. <p>As a Kawa extension, <em class="replaceable"><code>port</code></em> may be any object
  74. that implements <code class="literal">java.io.Closeable</code>.
  75. It is an error if <em class="replaceable"><code>proc</code></em> does not accept one argument.
  76. </p>
  77. </blockquote>
  78. </div>
  79. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872761808" class="indexterm"/> <code class="function">call-with-input-file</code> <em class="replaceable"><code>path</code></em> <em class="replaceable"><code>proc</code></em></p>
  80. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872758560" class="indexterm"/> <code class="function">call-with-output-file</code> <em class="replaceable"><code>path</code></em> <em class="replaceable"><code>proc</code></em></p>
  81. <div class="blockquote">
  82. <blockquote class="blockquote">
  83. <p>These procedures obtain a textual port obtained by
  84. opening the named file for input or output as if by
  85. <code class="literal">open-input-file</code> or <code class="literal">open-output-file</code>. The port and
  86. <em class="replaceable"><code>proc</code></em> are then passed to a procedure equivalent to
  87. <code class="literal">call-with-port</code>.
  88. </p>
  89. <p>It is an error if <em class="replaceable"><code>proc</code></em> does not accept one argument.
  90. </p>
  91. </blockquote>
  92. </div>
  93. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872752016" class="indexterm"/> <code class="function">input-port?</code> <em class="replaceable"><code>obj</code></em></p>
  94. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872749216" class="indexterm"/> <code class="function">output-port?</code> <em class="replaceable"><code>obj</code></em></p>
  95. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872746416" class="indexterm"/> <code class="function">textual-port?</code> <em class="replaceable"><code>obj</code></em></p>
  96. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872743616" class="indexterm"/> <code class="function">binary-port?</code> <em class="replaceable"><code>obj</code></em></p>
  97. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872740816" class="indexterm"/> <code class="function">port?</code> <em class="replaceable"><code>obj</code></em></p>
  98. <div class="blockquote">
  99. <blockquote class="blockquote">
  100. <p>These procedures return <code class="literal">#t</code> if obj is an input port, output port,
  101. textual port, binary port, or any kind of port,
  102. respectively. Otherwise they return <code class="literal">#f</code>.
  103. </p>
  104. <p>These procedures currently return <code class="literal">#f</code> on a native Java streams
  105. (<code class="literal">java.io.InputStream</code> or <code class="literal">java.io.OutputStream</code>),
  106. a native reader (a <code class="literal">java.io.Reader</code> that is not an
  107. <code class="literal">gnu.mapping.Inport</code>), or a native writer (a <code class="literal">java.io.Writer</code>
  108. that is not an <code class="literal">gnu.mapping.Outport</code>). This may change if
  109. conversions between native ports and Scheme ports becomes more seamless.
  110. </p>
  111. </blockquote>
  112. </div>
  113. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872732768" class="indexterm"/> <code class="function">input-port-open?</code> <em class="replaceable"><code>port</code></em></p>
  114. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872729936" class="indexterm"/> <code class="function">output-port-open?</code> <em class="replaceable"><code>port</code></em></p>
  115. <div class="blockquote">
  116. <blockquote class="blockquote">
  117. <p>Returns <code class="literal">#t</code> if <em class="replaceable"><code>port</code></em> is still open and capable of performing
  118. input or output, respectively, and <code class="literal">#f</code> otherwise.
  119. (Not supported for native binary ports - i.e. <code class="literal">java.io.InputStteam</code>
  120. or <code class="literal">java.io.OutputStream</code>.)
  121. </p>
  122. </blockquote>
  123. </div>
  124. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872724368" class="indexterm"/> <code class="function">current-input-port</code></p>
  125. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872721952" class="indexterm"/> <code class="function">current-output-port</code></p>
  126. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872719536" class="indexterm"/> <code class="function">current-error-port</code></p>
  127. <div class="blockquote">
  128. <blockquote class="blockquote">
  129. <p>Returns the current default input port, output port, or
  130. error port (an output port), respectively.
  131. (The error port is the port to which errors and warnings should be sent
  132. - the <em class="firstterm">standard error</em> in Unix and C terminology.)
  133. These procedures are <a class="link" href="Parameter-objects.xhtml" title="Parameter objects">parameter objects</a>,
  134. which can be overridden with <a class="link" href="Parameter-objects.xhtml#parameterize-syntax"><code class="literal">parameterize</code></a>.
  135. </p>
  136. <p>The initial bindings for <code class="literal">(current-output-port)</code> and
  137. <code class="literal">(current-error-port)</code> are hybrid textual/binary
  138. ports that wrap the values of the corresponding <code class="literal">java.lang.System</code> fields
  139. <code class="literal">out</code>, and <code class="literal">err</code>. The latter, in turn are bound
  140. to the standard output and error streams of the JVM process.
  141. This means you can write binary data to standard output
  142. using <code class="literal">write-bytevector</code> and <code class="literal">write-u8</code>.
  143. </p>
  144. <p>The initial value <code class="literal">(current-input-port)</code> similarly is a textual port
  145. that wraps the <code class="literal">java.lang.System</code> field <code class="literal">in</code>, which is bound
  146. to the standard input stream of the JVM process.
  147. It is a <span class="emphasis"><em>hybrid</em></span> textual/binary port only if there
  148. is no console (as determined by <code class="literal">(java.lang.System:console)</code>
  149. returning <code class="literal">#!null</code>) - i.e. if standard input is not a tty.
  150. </p>
  151. <p>Here is an example that copies standard input to standard output:
  152. </p>
  153. <pre class="screen">(let* ((in (current-input-port))
  154. (out (current-output-port))
  155. (blen ::int 2048)
  156. (buf (make-bytevector blen)))
  157. (let loop ()
  158. (define n (read-bytevector! buf in))
  159. (cond ((not (eof-object? n))
  160. (write-bytevector buf out 0 n)
  161. (loop)))))
  162. </pre>
  163. </blockquote>
  164. </div>
  165. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872706240" class="indexterm"/> <code class="function">with-input-from-file</code> <em class="replaceable"><code>path</code></em> <em class="replaceable"><code>thunk</code></em></p>
  166. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872702992" class="indexterm"/> <code class="function">with-output-to-file</code> <em class="replaceable"><code>path</code></em> <em class="replaceable"><code>thunk</code></em></p>
  167. <div class="blockquote">
  168. <blockquote class="blockquote">
  169. <p>The file is opened for input or output as if by
  170. <code class="literal">open-input-file</code> or <code class="literal">open-output-file</code>, and the new port
  171. is made to be the value returned by <code class="literal">current-input-port</code>
  172. or <code class="literal">current-output-port</code> (as used by <code class="literal">(read)</code>,
  173. <code class="literal">(write <em class="replaceable"><code>obj</code></em>)</code>, and so forth). The thunk is then called with no
  174. arguments. When the <em class="replaceable"><code>thunk</code></em> returns, the port is closed
  175. and the previous default is restored. It is an error if <em class="replaceable"><code>thunk</code></em>
  176. does not accept zero arguments. Both procedures return
  177. the values yielded by <em class="replaceable"><code>thunk</code></em>. If an escape procedure is used
  178. to escape from the continuation of these procedures, they
  179. behave exactly as if the current input or output port had
  180. been bound dynamically with <code class="literal">parameterize</code>.
  181. </p>
  182. </blockquote>
  183. </div>
  184. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872694000" class="indexterm"/> <code class="function">open-input-file</code> <em class="replaceable"><code>path</code></em></p>
  185. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872691168" class="indexterm"/> <code class="function">open-binary-input-file</code> <em class="replaceable"><code>path</code></em></p>
  186. <div class="blockquote">
  187. <blockquote class="blockquote">
  188. <p>Takes a <em class="replaceable"><code>path</code></em> naming an existing file and returns a textual
  189. input port or binary input port that is capable of delivering
  190. data from the file.
  191. </p>
  192. <p>The procedure <code class="literal">open-input-file</code> checks the fluid variable
  193. <a class="link" href="Ports.xhtml#port-char-encoding"><code class="literal">port-char-encoding</code></a> to determine how bytes are decoded
  194. into characters. The procedure <code class="literal">open-binary-input-file</code>
  195. is equivalent to calling <code class="literal">open-input-file</code> with
  196. <code class="literal">port-char-encoding</code> set to <code class="literal">#f</code>.
  197. </p>
  198. </blockquote>
  199. </div>
  200. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872682960" class="indexterm"/> <code class="function">open-output-file</code> <em class="replaceable"><code>path</code></em></p>
  201. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872680128" class="indexterm"/> <code class="function">open-binary-output-file</code> <em class="replaceable"><code>path</code></em></p>
  202. <div class="blockquote">
  203. <blockquote class="blockquote">
  204. <p>Takes a <em class="replaceable"><code>path</code></em> naming an output file to be created and returns
  205. respectively a textual output port or binary output port that is
  206. capable of writing data to a new file by that name. If a
  207. file with the given name already exists, the effect is unspecified.
  208. </p>
  209. <p>The procedure <code class="literal">open-output-file</code> checks the fluid variable
  210. <a class="link" href="Ports.xhtml#port-char-encoding"><code class="literal">port-char-encoding</code></a> to determine how characters are
  211. encoded as bytes. The procedure <code class="literal">open-binary-output-file</code>
  212. is equivalent to calling <code class="literal">open-output-file</code> with
  213. <code class="literal">port-char-encoding</code> set to <code class="literal">#f</code>.
  214. </p>
  215. </blockquote>
  216. </div>
  217. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872671904" class="indexterm"/> <code class="function">close-port</code> <em class="replaceable"><code>port</code></em></p>
  218. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872669072" class="indexterm"/> <code class="function">close-input-port</code> <em class="replaceable"><code>port</code></em></p>
  219. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872666240" class="indexterm"/> <code class="function">close-output-port</code> <em class="replaceable"><code>port</code></em></p>
  220. <div class="blockquote">
  221. <blockquote class="blockquote">
  222. <p>Closes the resource associated with <em class="replaceable"><code>port</code></em>, rendering the port
  223. incapable of delivering or accepting data. It is an error to
  224. apply the last two procedures to a port which is not an
  225. input or output port, respectively.
  226. (Specifically, <code class="literal">close-input-port</code> requires a <code class="literal">java.io.Reader</code>,
  227. while <code class="literal">close-output-port</code> requires a <code class="literal">java.io.Writer</code>.
  228. In contrast <code class="literal">close-port</code> accepts any object whose class
  229. implements <code class="literal">java.io.Closeable</code>.)
  230. </p>
  231. <p>These routines have no effect if the port has already been
  232. closed.
  233. </p>
  234. </blockquote>
  235. </div>
  236. <section class="sect2" title="String and bytevector ports" epub:type="division" id="idm139667872657696">
  237. <div class="titlepage">
  238. <div>
  239. <div>
  240. <h3 class="title">String and bytevector ports</h3>
  241. </div>
  242. </div>
  243. </div>
  244. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872656608" class="indexterm"/> <code class="function">open-input-string</code> <em class="replaceable"><code>string</code></em></p>
  245. <div class="blockquote">
  246. <blockquote class="blockquote">
  247. <p>Takes a string and returns a text input port that delivers characters
  248. from the string. The port can be closed by <code class="literal">close-input-port</code>,
  249. though its storage will be reclaimed by the
  250. garbage collector if it becomes inaccessible.
  251. </p>
  252. <pre class="screen">(define p
  253. (open-input-string "(a . (b c . ())) 34"))
  254. (input-port? p) ⇒ #t
  255. (read p) ⇒ (a b c)
  256. (read p) ⇒ 34
  257. (eof-object? (peek-char p)) ⇒ #t
  258. </pre>
  259. </blockquote>
  260. </div>
  261. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872651728" class="indexterm"/> <code class="function">open-output-string</code></p>
  262. <div class="blockquote">
  263. <blockquote class="blockquote">
  264. <p>Returns an textual output port that will accumulate characters
  265. for retrieval by <code class="literal">get-output-string</code>.
  266. The port can be closed by the procedure <code class="literal">close-output-port</code>,
  267. though its storage will be reclaimed by the garbage collector
  268. if it becomes inaccessible.
  269. </p>
  270. <pre class="screen">(let ((q (open-output-string))
  271. (x '(a b c)))
  272. (write (car x) q)
  273. (write (cdr x) q)
  274. (get-output-string q)) ⇒ "a(b c)"
  275. </pre>
  276. </blockquote>
  277. </div>
  278. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872647264" class="indexterm"/> <code class="function">get-output-string</code> <em class="replaceable"><code>output-port</code></em></p>
  279. <div class="blockquote">
  280. <blockquote class="blockquote">
  281. <p>Given an output port created by <code class="literal">open-output-string</code>,
  282. returns a string consisting of the characters that have been
  283. output to the port so far in the order they were output.
  284. If the result string is modified, the effect is unspecified.
  285. </p>
  286. <pre class="screen">(parameterize
  287. ((current-output-port (open-output-string)))
  288. (display "piece")
  289. (display " by piece ")
  290. (display "by piece.")
  291. (newline)
  292. (get-output-string (current-output-port)))
  293. ⇒ "piece by piece by piece.\n"
  294. </pre>
  295. </blockquote>
  296. </div>
  297. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872642416" class="indexterm"/> <code class="function">call-with-input-string</code> <em class="replaceable"><code>string</code></em> <em class="replaceable"><code>proc</code></em></p>
  298. <div class="blockquote">
  299. <blockquote class="blockquote">
  300. <p>Create an input port that gets its data from <em class="replaceable"><code>string</code></em>,
  301. call <em class="replaceable"><code>proc</code></em> with that port as its one argument, and return
  302. the result from the call of <em class="replaceable"><code>proc</code></em>
  303. </p>
  304. </blockquote>
  305. </div>
  306. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872637216" class="indexterm"/> <code class="function">call-with-output-string</code> <em class="replaceable"><code>proc</code></em></p>
  307. <div class="blockquote">
  308. <blockquote class="blockquote">
  309. <p>Create an output port that writes its data to a <em class="replaceable"><code>string</code></em>,
  310. and call <em class="replaceable"><code>proc</code></em> with that port as its one argument.
  311. Return a string consisting of the data written to the port.
  312. </p>
  313. </blockquote>
  314. </div>
  315. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872632832" class="indexterm"/> <code class="function">open-input-bytevector</code> <em class="replaceable"><code>bytevector</code></em></p>
  316. <div class="blockquote">
  317. <blockquote class="blockquote">
  318. <p>Takes a bytevector and returns a binary input port that
  319. delivers bytes from the bytevector.
  320. </p>
  321. </blockquote>
  322. </div>
  323. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872629376" class="indexterm"/> <code class="function">open-output-bytevector</code></p>
  324. <div class="blockquote">
  325. <blockquote class="blockquote">
  326. <p>Returns a binary output port that will accumulate bytes
  327. for retrieval by <code class="literal">get-output-bytevector</code>.
  328. </p>
  329. </blockquote>
  330. </div>
  331. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872625936" class="indexterm"/> <code class="function">get-output-bytevector</code> <em class="replaceable"><code>port</code></em></p>
  332. <div class="blockquote">
  333. <blockquote class="blockquote">
  334. <p>Returns a bytevector consisting of the bytes that have been
  335. output to the port so far in the order they were output.
  336. It is an error if <em class="replaceable"><code>port</code></em> was not created with <code class="literal">open-output-bytevector</code>.
  337. </p>
  338. </blockquote>
  339. </div>
  340. </section>
  341. <section class="sect2" title="Input" epub:type="division" id="idm139667872621584">
  342. <div class="titlepage">
  343. <div>
  344. <div>
  345. <h3 class="title">Input</h3>
  346. </div>
  347. </div>
  348. </div>
  349. <p>If <em class="replaceable"><code>port</code></em> is omitted from any input procedure, it defaults
  350. to the value returned by <code class="literal">(current-input-port)</code>. It is an
  351. error to attempt an input operation on a closed port.
  352. </p>
  353. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872619120" class="indexterm"/> <code class="function">read</code> [<em class="replaceable"><code>port</code></em>]</p>
  354. <div class="blockquote">
  355. <blockquote class="blockquote">
  356. <p>The <code class="literal">read</code> procedure converts external representations of
  357. Scheme objects into the objects themselves. That is, it is
  358. a parser for the non-terminal <a class="link" href="Datum-syntax.xhtml#meta-datum"><em class="replaceable"><code>datum</code></em></a>.
  359. It returns the next object parsable from the
  360. given textual input port, updating port to point to the
  361. first character past the end of the external representation
  362. of the object.
  363. </p>
  364. <p>If an end of file is encountered in the input before any
  365. characters are found that can begin an object, then an
  366. end-of-file object is returned. The port remains open, and
  367. further attempts to read will also return an end-of-file object.
  368. If an end of file is encountered after the beginning of
  369. an object’s external representation, but the external representation
  370. is incomplete and therefore not parsable, an error
  371. that satisfies <code class="literal">read-error?</code> is signaled.
  372. </p>
  373. </blockquote>
  374. </div>
  375. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872612320" class="indexterm"/> <code class="function">read-char</code> [<em class="replaceable"><code>port</code></em>]</p>
  376. <div class="blockquote">
  377. <blockquote class="blockquote">
  378. <p>Returns the next character available from the textual input
  379. <em class="replaceable"><code>port</code></em>, updating the port to point to the following character.
  380. If no more characters are available, an end-of-file value is
  381. returned.
  382. </p>
  383. <p>The result type is <code class="literal">character-or-eof</code>.
  384. </p>
  385. </blockquote>
  386. </div>
  387. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872607376" class="indexterm"/> <code class="function">peek-char</code> [<em class="replaceable"><code>port</code></em>]</p>
  388. <div class="blockquote">
  389. <blockquote class="blockquote">
  390. <p>Returns the next character available from the textual input <em class="replaceable"><code>port</code></em>,
  391. but <span class="emphasis"><em>without</em></span> updating the port to point to the
  392. following character. If no more characters are available, an
  393. end-of-file value is returned.
  394. </p>
  395. <p>The result type is <code class="literal">character-or-eof</code>.
  396. </p>
  397. <p><span class="emphasis"><em>Note:</em></span> The value returned by a call to <code class="literal">peek-char</code> is the same as
  398. the value that would have been returned by a call to <code class="literal">read-char</code>
  399. with the same <em class="replaceable"><code>port</code></em>. The only difference is that the very next call
  400. to <code class="literal">read-char</code> or <code class="literal">peek-char</code> on that <em class="replaceable"><code>port</code></em> will return the
  401. value returned by the preceding call to <code class="literal">peek-char</code>. In particular, a
  402. call to <code class="literal">peek-char</code> on an interactive port will hang waiting for
  403. input whenever a call to <code class="literal">read-char</code> would have hung.
  404. </p>
  405. </blockquote>
  406. </div>
  407. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872597040" class="indexterm"/> <code class="function">read-line</code> [<em class="replaceable"><code>port</code></em> [<em class="replaceable"><code>handle-newline</code></em>]]</p>
  408. <div class="blockquote">
  409. <blockquote class="blockquote">
  410. <p>Reads a line of input from the textual input <em class="replaceable"><code>port</code></em>.
  411. The <em class="replaceable"><code>handle-newline</code></em> parameter determines what is done with
  412. terminating end-of-line delimiter.
  413. The default, <code class="literal">'trim</code>, ignores the delimiter;
  414. <code class="literal">'peek</code> leaves the delimiter in the input stream;
  415. <code class="literal">'concat</code> appends the delimiter to the returned value;
  416. and <code class="literal">'split</code> returns the delimiter as a second value.
  417. You can use the last three options to tell if the string was
  418. terminated by end-or-line or by end-of-file.
  419. If an end of file is encountered before any end of
  420. line is read, but some characters have been read, a string
  421. containing those characters is returned.
  422. (In this case, <code class="literal">'trim</code>, <code class="literal">'peek</code>, and <code class="literal">'concat</code>
  423. have the same result and effect. The <code class="literal">'split</code> case returns two
  424. values: The characters read, and the delimiter is an empty string.)
  425. If an end of file is encountered before any characters are read,
  426. an end-of-file object is returned.
  427. For the purpose of this procedure, an
  428. end of line consists of either a linefeed character, a carriage
  429. return character, or a sequence of a carriage return character
  430. followed by a linefeed character.
  431. </p>
  432. </blockquote>
  433. </div>
  434. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872587792" class="indexterm"/> <code class="function">eof-object?</code> <em class="replaceable"><code>obj</code></em></p>
  435. <div class="blockquote">
  436. <blockquote class="blockquote">
  437. <p>Returns <code class="literal">#t</code> if <em class="replaceable"><code>obj</code></em> is an end-of-file object,
  438. otherwise returns <code class="literal">#f</code>.
  439. </p>
  440. <p><code class="literal">Performance note</code>: If <em class="replaceable"><code>obj</code></em> has type <code class="literal">character-or-eof</code>,
  441. this is compiled as an <code class="literal">int</code> comparison with -1.
  442. </p>
  443. </blockquote>
  444. </div>
  445. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872581200" class="indexterm"/> <code class="function">eof-object</code></p>
  446. <div class="blockquote">
  447. <blockquote class="blockquote">
  448. <p>Returns an end-of-file object.
  449. </p>
  450. </blockquote>
  451. </div>
  452. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872578224" class="indexterm"/> <code class="function">char-ready?</code> [<em class="replaceable"><code>port</code></em>]</p>
  453. <div class="blockquote">
  454. <blockquote class="blockquote">
  455. <p>Returns <code class="literal">#t</code> if a character is ready on the textual input
  456. <em class="replaceable"><code>port</code></em> and returns <code class="literal">#f</code> otherwise. If char-ready returns <code class="literal">#t</code>
  457. then the next <code class="literal">read-char</code> operation on the given <em class="replaceable"><code>port</code></em> is
  458. guaranteed not to hang. If the port is at end of file then
  459. <code class="literal">char-ready?</code> returns <code class="literal">#t</code>.
  460. </p>
  461. <p><span class="emphasis"><em>Rationale:</em></span> The <code class="literal">char-ready?</code> procedure exists to make it
  462. possible for a program to accept characters from interactive ports
  463. without getting stuck waiting for input. Any input editors as-
  464. sociated with such ports must ensure that characters whose
  465. existence has been asserted by <code class="literal">char-ready?</code> cannot be removed
  466. from the input. If <code class="literal">char-ready?</code> were to return <code class="literal">#f</code> at end of
  467. file, a port at end-of-file would be indistinguishable from an
  468. interactive port that has no ready characters.
  469. </p>
  470. </blockquote>
  471. </div>
  472. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872568352" class="indexterm"/> <code class="function">read-string</code> <em class="replaceable"><code>k</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  473. <div class="blockquote">
  474. <blockquote class="blockquote">
  475. <p>Reads the next <em class="replaceable"><code>k</code></em> characters, or as many as are available
  476. before the end of file, from the textual input <em class="replaceable"><code>port</code></em> into a
  477. newly allocated string in left-to-right order and returns the
  478. string. If no characters are available before the end of file,
  479. an end-of-file object is returned.
  480. </p>
  481. </blockquote>
  482. </div>
  483. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872563360" class="indexterm"/> <code class="function">read-u8</code> [<em class="replaceable"><code>port</code></em>]</p>
  484. <div class="blockquote">
  485. <blockquote class="blockquote">
  486. <p>Returns the next byte available from the binary input <em class="replaceable"><code>port</code></em>,
  487. updating the <em class="replaceable"><code>port</code></em> to point to the following byte. If no more
  488. bytes are available, an end-of-file object is returned.
  489. </p>
  490. </blockquote>
  491. </div>
  492. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872558848" class="indexterm"/> <code class="function">peek-u8</code> [<em class="replaceable"><code>port</code></em>]</p>
  493. <div class="blockquote">
  494. <blockquote class="blockquote">
  495. <p>Returns the next byte available from the binary input <em class="replaceable"><code>port</code></em>,
  496. but <span class="emphasis"><em>without</em></span> updating the <em class="replaceable"><code>port</code></em> to point to the following byte.
  497. If no more bytes are available, an end-of-file object is returned.
  498. </p>
  499. </blockquote>
  500. </div>
  501. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872553888" class="indexterm"/> <code class="function">u8-ready?</code> [<em class="replaceable"><code>port</code></em>]</p>
  502. <div class="blockquote">
  503. <blockquote class="blockquote">
  504. <p>Returns <code class="literal">#t</code> if a byte is ready on the binary input <em class="replaceable"><code>port</code></em> and
  505. returns <code class="literal">#f</code> otherwise. If <code class="literal">u8-ready?</code> returns <code class="literal">#t</code> then the
  506. next <code class="literal">read-u8</code> operation on the given port is guaranteed
  507. not to hang. If the port is at end of file then <code class="literal">u8-ready?</code>
  508. returns <code class="literal">#t</code>.
  509. </p>
  510. </blockquote>
  511. </div>
  512. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872546848" class="indexterm"/> <code class="function">read-bytevector</code> <em class="replaceable"><code>k</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  513. <div class="blockquote">
  514. <blockquote class="blockquote">
  515. <p>Reads the next <em class="replaceable"><code>k</code></em> bytes, or as many as are available before
  516. the end of file, from the binary input <em class="replaceable"><code>port</code></em> into a newly
  517. allocated bytevector in left-to-right order and returns the
  518. bytevector. If no bytes are available before the end of file,
  519. an end-of-file object is returned.
  520. </p>
  521. </blockquote>
  522. </div>
  523. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872541872" class="indexterm"/> <code class="function">read-bytevector!</code> <em class="replaceable"><code>bytevector</code></em> [<em class="replaceable"><code>port</code></em> [<em class="replaceable"><code>start</code></em> [<em class="replaceable"><code>end</code></em>]]]</p>
  524. <div class="blockquote">
  525. <blockquote class="blockquote">
  526. <p>Reads the next <em class="replaceable"><code>end</code></em> − <em class="replaceable"><code>start</code></em> bytes, or as many as are
  527. available before the end of file, from the binary input <em class="replaceable"><code>port</code></em>
  528. into <em class="replaceable"><code>bytevector</code></em> in left-to-right order beginning at the <em class="replaceable"><code>start</code></em>
  529. position. If <em class="replaceable"><code>end</code></em> is not supplied, reads until the end of
  530. <em class="replaceable"><code>bytevector</code></em> has been reached. If <em class="replaceable"><code>start</code></em> is not supplied, reads
  531. beginning at position 0. Returns the number of bytes read.
  532. If no bytes are available, an end-of-file object is returned.
  533. </p>
  534. </blockquote>
  535. </div>
  536. </section>
  537. <section class="sect2" title="Output" epub:type="division" id="idm139667872533328">
  538. <div class="titlepage">
  539. <div>
  540. <div>
  541. <h3 class="title">Output</h3>
  542. </div>
  543. </div>
  544. </div>
  545. <p>If <em class="replaceable"><code>port</code></em> is omitted from any output procedure, it defaults
  546. to the value returned by <code class="literal">(current-output-port)</code>. It is an
  547. error to attempt an output operation on a closed port.
  548. </p>
  549. <p>The return type of these methods is <code class="literal">void</code>.
  550. </p>
  551. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872530016" class="indexterm"/> <code class="function">write</code> <em class="replaceable"><code>obj</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  552. <div class="blockquote">
  553. <blockquote class="blockquote">
  554. <p>Writes a representation of <em class="replaceable"><code>obj</code></em> to the given textual output
  555. <em class="replaceable"><code>port</code></em>. Strings that appear in the written representation
  556. are enclosed in quotation marks, and within those strings
  557. backslash and quotation mark characters are escaped by
  558. backslashes.
  559. Symbols that contain non-ASCII characters
  560. are escaped with vertical lines.
  561. Character objects are written using the <code class="literal">#\</code> notation.
  562. </p>
  563. <p>If <em class="replaceable"><code>obj</code></em> contains cycles which would cause an infinite loop
  564. using the normal written representation, then at least the
  565. objects that form part of the cycle must be represented
  566. using <a class="link" href="Datum-syntax.xhtml#datum-labels">datum labels</a>. Datum
  567. labels must not be used if there are no cycles.
  568. </p>
  569. </blockquote>
  570. </div>
  571. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872522816" class="indexterm"/> <code class="function">write-shared</code> <em class="replaceable"><code>obj</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  572. <div class="blockquote">
  573. <blockquote class="blockquote">
  574. <p>The <code class="literal">write-shared</code> procedure is the same as <code class="literal">write</code>, except
  575. that shared structure must be represented using datum
  576. labels for all pairs and vectors that appear more than once
  577. in the output.
  578. </p>
  579. </blockquote>
  580. </div>
  581. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872517888" class="indexterm"/> <code class="function">write-simple</code> <em class="replaceable"><code>obj</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  582. <div class="blockquote">
  583. <blockquote class="blockquote">
  584. <p>The <code class="literal">write-simple</code> procedure is the same as <code class="literal">write</code>, except
  585. that shared structure is never represented using datum labels.
  586. This can cause write-simple not to terminate if <em class="replaceable"><code>obj</code></em>
  587. contains circular structure.
  588. </p>
  589. </blockquote>
  590. </div>
  591. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872512544" class="indexterm"/> <code class="function">display</code> <em class="replaceable"><code>obj</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  592. <div class="blockquote">
  593. <blockquote class="blockquote">
  594. <p>Writes a representation of <em class="replaceable"><code>obj</code></em> to the given textual output
  595. port. Strings that appear in the written representation
  596. are output as if by <code class="literal">write-string</code> instead of by <code class="literal">write</code>.
  597. Symbols are not escaped. Character objects appear in the
  598. representation as if written by <code class="literal">write-char</code> instead of by
  599. <code class="literal">write</code>.
  600. The <code class="literal">display</code> representation of other objects is unspecified.
  601. </p>
  602. </blockquote>
  603. </div>
  604. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872504560" class="indexterm"/> <code class="function">newline</code> [<em class="replaceable"><code>port</code></em>]</p>
  605. <div class="blockquote">
  606. <blockquote class="blockquote">
  607. <p>Writes an end of line to textual output <em class="replaceable"><code>port</code></em>.
  608. This is done using the <code class="literal">println</code> method
  609. of the Java class <code class="literal">java.io.PrintWriter</code>.
  610. </p>
  611. </blockquote>
  612. </div>
  613. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872499792" class="indexterm"/> <code class="function">write-char</code> <em class="replaceable"><code>char</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  614. <div class="blockquote">
  615. <blockquote class="blockquote">
  616. <p>Writes the character <em class="replaceable"><code>char</code></em> (not an external representation
  617. of the character) to the given textual output <em class="replaceable"><code>port</code></em>.
  618. </p>
  619. </blockquote>
  620. </div>
  621. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872494944" class="indexterm"/> <code class="function">write-string</code> <em class="replaceable"><code>string</code></em> [<em class="replaceable"><code>port</code></em> [<em class="replaceable"><code>start</code></em> [<em class="replaceable"><code>end</code></em>]]]</p>
  622. <div class="blockquote">
  623. <blockquote class="blockquote">
  624. <p>Writes the characters of <em class="replaceable"><code>string</code></em> from <em class="replaceable"><code>start</code></em> to <em class="replaceable"><code>end</code></em>
  625. in left-to-right order to the textual output <em class="replaceable"><code>port</code></em>.
  626. </p>
  627. </blockquote>
  628. </div>
  629. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872488448" class="indexterm"/> <code class="function">write-u8</code> <em class="replaceable"><code>byte</code></em> [<em class="replaceable"><code>port</code></em>]</p>
  630. <div class="blockquote">
  631. <blockquote class="blockquote">
  632. <p>Writes the <em class="replaceable"><code>byte</code></em> to the given binary output port.
  633. </p>
  634. </blockquote>
  635. </div>
  636. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872484064" class="indexterm"/> <code class="function">write-bytevector</code> <em class="replaceable"><code>bytevector</code></em> [<em class="replaceable"><code>port</code></em> [<em class="replaceable"><code>start</code></em> [<em class="replaceable"><code>end</code></em>]]]</p>
  637. <div class="blockquote">
  638. <blockquote class="blockquote">
  639. <p>Writes the bytes of <em class="replaceable"><code>bytevector</code></em> from <em class="replaceable"><code>start</code></em> to <em class="replaceable"><code>end</code></em>
  640. in left-to-right order to the binary output <em class="replaceable"><code>port</code></em>.
  641. </p>
  642. </blockquote>
  643. </div>
  644. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872477584" class="indexterm"/> <code class="function">flush-output-port</code> [<em class="replaceable"><code>port</code></em>]</p>
  645. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872474624" class="indexterm"/> <code class="function">force-output</code> [<em class="replaceable"><code>port</code></em>]</p>
  646. <div class="blockquote">
  647. <blockquote class="blockquote">
  648. <p>Forces any pending output on <em class="replaceable"><code>port</code></em> to be delivered to the output file or
  649. device and returns an unspecified value. If the <em class="replaceable"><code>port</code></em> argument is
  650. omitted it defaults to the value returned by <code class="literal">(current-output-port)</code>.
  651. (The name <code class="literal">force-output</code> is older,
  652. while R6RS added <code class="literal">flush-output-port</code>. They have the same effect.)
  653. </p>
  654. </blockquote>
  655. </div>
  656. <span id="Prompts"/>
  657. <a id="idm139667872468512" class="indexterm"/>
  658. </section>
  659. <section class="sect2" title="Prompts for interactive consoles (REPLs)" epub:type="division" id="idm139667872467600">
  660. <div class="titlepage">
  661. <div>
  662. <div>
  663. <h3 class="title">Prompts for interactive consoles (REPLs)</h3>
  664. </div>
  665. </div>
  666. </div>
  667. <p>When an interactive input port is used for a read-eval-print-loop (REPL
  668. or console) it is traditional for the REPL to print a short
  669. <em class="firstterm">prompt</em> string to signal that the user is expected to type
  670. an expression. These prompt strings can be customized.
  671. </p>
  672. <span id="input-prompt1"/>
  673. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872465024" class="indexterm"/> <code class="varname">input-prompt1</code></p>
  674. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872462608" class="indexterm"/> <code class="varname">input-prompt2</code></p>
  675. <div class="blockquote">
  676. <blockquote class="blockquote">
  677. <p>These are fluid variable whose values are string templates
  678. with placeholders similar to <code class="literal">printf</code>-style format.
  679. The placeholders are expanded (depending on the current state),
  680. and the resulting string printed in front of the input line.
  681. </p>
  682. <p>The <code class="literal">input-prompt1</code> is used normally. For multi-line input commands
  683. (for example if the first line is incomplete), <code class="literal">input-prompt1</code>
  684. is used for the first line of each command, while <code class="literal">input-prompt2</code> is
  685. used for subsequent “continuation” lines.
  686. </p>
  687. <p>The following placeholders are handled:
  688. </p>
  689. <div class="variablelist" epub:type="list">
  690. <dl class="variablelist">
  691. <dt class="term"><code class="literal"><span class="bold"><strong>%%</strong></span></code>
  692. </dt>
  693. <dd>
  694. <p>A literal ‘<code class="literal">%</code>’.
  695. </p>
  696. </dd>
  697. <dt class="term"><code class="literal"><span class="bold"><strong>%N</strong></span></code>
  698. </dt>
  699. <dd>
  700. <p>The current line number. This is <code class="literal">(+ 1 (port-line <em class="replaceable"><code>port</code></em>))</code>.
  701. </p>
  702. </dd>
  703. <dt class="term"><code class="literal"><span class="bold"><strong>%</strong></span></code><em class="replaceable"><code>n</code></em><code class="literal"><span class="bold"><strong>P</strong></span></code><em class="replaceable"><code>c</code></em>
  704. </dt>
  705. <dd>
  706. <p>Insert padding at this possion, repeating the following
  707. character <code class="literal"><em class="replaceable"><code>c</code></em></code> as needed to bring the total number of columns
  708. of the prompt to that specified by the digits <code class="literal"><em class="replaceable"><code>n</code></em></code>.
  709. </p>
  710. </dd>
  711. <dt class="term"><code class="literal"><span class="bold"><strong>%P</strong></span></code><em class="replaceable"><code>c</code></em>
  712. </dt>
  713. <dd>
  714. <p>Same as <code class="literal">%<em class="replaceable"><code>n</code></em>P<em class="replaceable"><code>c</code></em></code>, but <code class="literal"><em class="replaceable"><code>n</code></em></code> defaults to the number
  715. of columns in the initial prompt from the expansion of <code class="literal">input-prompt1</code>.
  716. This is only meaningful when expanding <code class="literal">input-prompt2</code> for
  717. continuation lines.
  718. </p>
  719. </dd>
  720. <dt class="term"><code class="literal"><span class="bold"><strong>%</strong></span></code><code class="literal"><span class="bold"><strong>{</strong></span></code><em class="replaceable"><code>hidden</code></em><code class="literal"><span class="bold"><strong>%}</strong></span></code>
  721. </dt>
  722. <dd>
  723. <p>Same as <em class="replaceable"><code>hidden</code></em>, but the characters of <em class="replaceable"><code>hidden</code></em>
  724. are assumed to have zero visible width. Can be used for
  725. <a class="ulink" href="https://en.wikipedia.org/wiki/ANSI_escape_code" target="_top">ANSI escape sequences</a>
  726. to change color or style:
  727. </p>
  728. <pre class="screen">(set! input-prompt1 "%{\e[48;5;51m%}{Kawa:%N} %{\e[0m%}")
  729. </pre>
  730. <p>The above changes both the text and the background color
  731. (to a pale blue).
  732. </p>
  733. </dd>
  734. <dt class="term"><code class="literal"><span class="bold"><strong>%M</strong></span></code>
  735. </dt>
  736. <dd>
  737. <p>Insert a “message” string.
  738. Not normally used by Kawa, but supported by JLine.
  739. </p>
  740. </dd>
  741. </dl>
  742. </div>
  743. <p>These variables can be initialized by the command-line
  744. arguments <code class="literal">console:prompt1=<em class="replaceable"><code>prompt1</code></em></code> and
  745. <code class="literal">console:prompt2=<em class="replaceable"><code>prompt2</code></em></code>, respectively. If these are
  746. not specified, languages-specific defaults are used.
  747. For example for Scheme the default value of <code class="literal">input-prompt1</code>
  748. is <code class="literal">"#|kawa:%N|# "</code> and <code class="literal">input-prompt2</code> is <code class="literal">"#|$P.%N| "</code>.
  749. These have the form of Scheme comments, to make it easier to cut-and-paste.
  750. </p>
  751. <p>If <code class="literal">input-prompt1</code> (respectively <code class="literal">input-prompt2</code>) does not contain
  752. an escape sequence (either <code class="literal">"%{</code> or the escape character <code class="literal">"\e"</code>)
  753. then ANSI escape sequences are added to to highlight the prompt.
  754. (Under DomTerm this sets the <code class="literal">prompt</code> style, which can be customised
  755. with CSS but defaults to a light green background;
  756. if using JLine the background is set to light green.)
  757. </p>
  758. </blockquote>
  759. </div>
  760. <p>For greater flexibility, you can also set a prompter procedure.
  761. </p>
  762. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872429600" class="indexterm"/> <code class="function">set-input-port-prompter!</code> <em class="replaceable"><code>port</code></em> <em class="replaceable"><code>prompter</code></em></p>
  763. <div class="blockquote">
  764. <blockquote class="blockquote">
  765. <p>Set the prompt procedure associated with <em class="replaceable"><code>port</code></em> to <em class="replaceable"><code>prompter</code></em>,
  766. which must be a one-argument procedure taking an input port,
  767. and returning a string.
  768. The procedure is called before reading the first line of a command;
  769. its return value is used as the first-line prompt.
  770. </p>
  771. <p>The prompt procedure can have side effects.
  772. In Bash shell terms: It combines the features of <code class="literal">PROMPT_COMMAND</code>
  773. and <code class="literal">PS1</code>.
  774. </p>
  775. <p>The initial <em class="replaceable"><code>prompter</code></em> is <code class="literal">default-prompter</code>,
  776. which returns the expansion of <code class="literal">input-prompt1</code>.
  777. </p>
  778. </blockquote>
  779. </div>
  780. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872421584" class="indexterm"/> <code class="function">input-port-prompter</code> <em class="replaceable"><code>port</code></em></p>
  781. <div class="blockquote">
  782. <blockquote class="blockquote">
  783. <p>Get the prompt procedure associated with <em class="replaceable"><code>port</code></em>.
  784. </p>
  785. </blockquote>
  786. </div>
  787. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872417760" class="indexterm"/> <code class="function">default-prompter</code> <em class="replaceable"><code>port</code></em></p>
  788. <div class="blockquote">
  789. <blockquote class="blockquote">
  790. <p>The default prompt procedure.
  791. Normally (i.e. when <code class="literal">input-port-read-state</code> is a space)
  792. returns <code class="literal">input-prompt1</code> after expanding the <code class="literal">%</code>-placeholders.
  793. Can also expand <code class="literal">input-prompt2</code> when <code class="literal">input-port-read-state</code>
  794. is not whitespace.
  795. </p>
  796. </blockquote>
  797. </div>
  798. </section>
  799. <section class="sect2" title="Line numbers and other input port properties" epub:type="division" id="idm139667872412128">
  800. <div class="titlepage">
  801. <div>
  802. <div>
  803. <h3 class="title">Line numbers and other input port properties</h3>
  804. </div>
  805. </div>
  806. </div>
  807. <p class="synopsis" kind="Function"><span class="kind">Function</span><span class="ignore">: </span><a id="idm139667872411024" class="indexterm"/> <code class="function">port-column</code> <em class="replaceable"><code>input-port</code></em></p>
  808. <p class="synopsis" kind="Function"><span class="kind">Function</span><span class="ignore">: </span><a id="idm139667872408192" class="indexterm"/> <code class="function">port-line</code> <em class="replaceable"><code>input-port</code></em></p>
  809. <div class="blockquote">
  810. <blockquote class="blockquote">
  811. <p>Return the current column number or line number of <em class="replaceable"><code>input-port</code></em>,
  812. using the current input port if none is specified.
  813. If the number is unknown, the result is <code class="literal">#f</code>. Otherwise,
  814. the result is a 0-origin integer - i.e. the first character
  815. of the first line is line 0, column 0. (However, when you
  816. display a file position, for example in an error message,
  817. we recommend you add 1 to get 1-origin integers. This is
  818. because lines and column numbers traditionally start with
  819. 1, and that is what non-programmers will find most natural.)
  820. </p>
  821. </blockquote>
  822. </div>
  823. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872403488" class="indexterm"/> <code class="function">set-port-line!</code> <em class="replaceable"><code>port</code></em> <em class="replaceable"><code>line</code></em></p>
  824. <div class="blockquote">
  825. <blockquote class="blockquote">
  826. <p>Set (0-origin) line number of the current line of <em class="replaceable"><code>port</code></em> to <em class="replaceable"><code>num</code></em>.
  827. </p>
  828. </blockquote>
  829. </div>
  830. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872398784" class="indexterm"/> <code class="function">input-port-line-number</code> <em class="replaceable"><code>port</code></em></p>
  831. <div class="blockquote">
  832. <blockquote class="blockquote">
  833. <p>Get the line number of the current line of <em class="replaceable"><code>port</code></em>,
  834. which must be a (non-binary) input port.
  835. The initial line is line 1.
  836. Deprecated; replaced by <code class="literal">(+ 1 (port-line <em class="replaceable"><code>port</code></em>))</code>.
  837. </p>
  838. </blockquote>
  839. </div>
  840. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872394016" class="indexterm"/> <code class="function">set-input-port-line-number!</code> <em class="replaceable"><code>port</code></em> <em class="replaceable"><code>num</code></em></p>
  841. <div class="blockquote">
  842. <blockquote class="blockquote">
  843. <p>Set line number of the current line of <em class="replaceable"><code>port</code></em> to <em class="replaceable"><code>num</code></em>.
  844. Deprecated; replaced by <code class="literal">(set-port-line! <em class="replaceable"><code>port</code></em> (- <em class="replaceable"><code>num</code></em> 1))</code>.
  845. </p>
  846. </blockquote>
  847. </div>
  848. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872388048" class="indexterm"/> <code class="function">input-port-column-number</code> <em class="replaceable"><code>port</code></em> </p>
  849. <div class="blockquote">
  850. <blockquote class="blockquote">
  851. <p>Get the column number of the current line of <em class="replaceable"><code>port</code></em>,
  852. which must be a (non-binary) input port.
  853. The initial column is column 1.
  854. Deprecated; replaced by <code class="literal">(+ 1 (port-column <em class="replaceable"><code>port</code></em>))</code>.
  855. </p>
  856. </blockquote>
  857. </div>
  858. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872383120" class="indexterm"/> <code class="function">input-port-read-state</code> <em class="replaceable"><code>port</code></em></p>
  859. <div class="blockquote">
  860. <blockquote class="blockquote">
  861. <p>Returns a character indicating the current <code class="literal">read</code> state of the <em class="replaceable"><code>port</code></em>.
  862. Returns <code class="literal">#\Return</code> if not current doing a <em class="replaceable"><code>read</code></em>,
  863. <code class="literal">#\"</code> if reading a string; <code class="literal">#\|</code> if reading a comment; <code class="literal">#\(</code>
  864. if inside a list; and <code class="literal">#\Space</code> when otherwise in a <code class="literal">read</code>.
  865. The result is intended for use by prompt prcedures, and is not necessarily
  866. correct except when reading a new-line.
  867. </p>
  868. </blockquote>
  869. </div>
  870. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872375552" class="indexterm"/> <code class="varname">symbol-read-case</code></p>
  871. <div class="blockquote">
  872. <blockquote class="blockquote">
  873. <p>A symbol that controls how <code class="literal">read</code> handles letters when reading a symbol.
  874. If the first letter is ‘<code class="literal">U</code>’, then letters in symbols are upper-cased.
  875. If the first letter is ‘<code class="literal">D</code>’ or ‘<code class="literal">L</code>’, then letters
  876. in symbols are down-cased.
  877. If the first letter is ‘<code class="literal">I</code>’, then the case of letters in symbols
  878. is inverted.
  879. Otherwise (the default), the letter is not changed.
  880. (Letters following a ‘<code class="literal">\</code>’ are always unchanged.)
  881. The value of <code class="literal">symbol-read-case</code> only checked
  882. when a reader is created, not each time a symbol is read.
  883. </p>
  884. </blockquote>
  885. </div>
  886. </section>
  887. <section class="sect2" title="Miscellaneous" epub:type="division" id="idm139667872368816">
  888. <div class="titlepage">
  889. <div>
  890. <div>
  891. <h3 class="title">Miscellaneous</h3>
  892. </div>
  893. </div>
  894. </div>
  895. <span id="port-char-encoding"/>
  896. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872367344" class="indexterm"/> <code class="varname">port-char-encoding</code></p>
  897. <div class="blockquote">
  898. <blockquote class="blockquote">
  899. <p>Controls how bytes in external files are converted to/from internal
  900. Unicode characters. Can be either a symbol or a boolean.
  901. If <code class="literal">port-char-encoding</code> is <code class="literal">#f</code>, the file is assumed
  902. to be a binary file and no conversion is done.
  903. Otherwise, the file is a text file. The default is <code class="literal">#t</code>, which
  904. uses a locale-dependent conversion. If <code class="literal">port-char-encoding</code>
  905. is a symbol, it must be the name of a character encoding known to Java.
  906. For all text files (that is if <code class="literal">port-char-encoding</code> is not <code class="literal">#f</code>),
  907. on input a <code class="literal">#\Return</code> character or
  908. a <code class="literal">#\Return</code> followed by <code class="literal">#\Newline</code>
  909. are converted into plain <code class="literal">#\Newline</code>.
  910. </p>
  911. <p>This variable is checked when the file is opened; not when actually
  912. reading or writing. Here is an example of how you can safely
  913. change the encoding temporarily:
  914. </p>
  915. <pre class="screen">(define (open-binary-input-file name)
  916. (fluid-let ((port-char-encoding #f)) (open-input-file name)))
  917. </pre>
  918. </blockquote>
  919. </div>
  920. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872358736" class="indexterm"/> <code class="varname">*print-base*</code></p>
  921. <div class="blockquote">
  922. <blockquote class="blockquote">
  923. <p>The number base (radix) to use by default when printing rational numbers.
  924. Must be an integer between 2 and 36, and the default is of course 10.
  925. For example setting <code class="literal">*print-base*</code> to 16 produces hexadecimal output.
  926. </p>
  927. </blockquote>
  928. </div>
  929. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872355168" class="indexterm"/> <code class="varname">*print-radix*</code></p>
  930. <div class="blockquote">
  931. <blockquote class="blockquote">
  932. <p>If true, prints an indicator of the radix used when printing rational numbers.
  933. If <code class="literal">*print-base*</code> is respectively 2, 8, or 16, then
  934. <code class="literal">#b</code>, <code class="literal">#o</code> or <code class="literal">#x</code> is written before the number;
  935. otherwise <code class="literal">#<em class="replaceable"><code>N</code></em>r</code> is written, where <code class="literal"><em class="replaceable"><code>N</code></em></code> is the base.
  936. An exception is when <code class="literal">*print-base*</code> is 10, in which case a period
  937. is written <span class="emphasis"><em>after</em></span> the number, to match Common Lisp; this may
  938. be inappropriate for Scheme, so is likely to change.
  939. </p>
  940. </blockquote>
  941. </div>
  942. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872348064" class="indexterm"/> <code class="varname">*print-right-margin*</code></p>
  943. <div class="blockquote">
  944. <blockquote class="blockquote">
  945. <p>The right margin (or line width) to use when pretty-printing.
  946. </p>
  947. </blockquote>
  948. </div>
  949. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872345056" class="indexterm"/> <code class="varname">*print-miser-width*</code></p>
  950. <div class="blockquote">
  951. <blockquote class="blockquote">
  952. <p>If this an integer, and the available width is less or equal to this value,
  953. then the pretty printer switch to the more <em class="firstterm">miser</em> compact style.
  954. </p>
  955. </blockquote>
  956. </div>
  957. <p class="synopsis" kind="Variable"><span class="kind">Variable</span><span class="ignore">: </span><a id="idm139667872341552" class="indexterm"/> <code class="varname">*print-xml-indent*</code></p>
  958. <div class="blockquote">
  959. <blockquote class="blockquote">
  960. <p>When writing to XML, controls pretty-printing and indentation.
  961. If the value is <code class="literal">'always</code> or <code class="literal">'yes</code> force each element to
  962. start on a new suitably-indented line.
  963. If the value is <code class="literal">'pretty</code> only force new lines for elements that
  964. won’t fit completely on a line.
  965. The the value is <code class="literal">'no</code> or unset, don’t add extra whitespace.
  966. </p>
  967. </blockquote>
  968. </div>
  969. </section>
  970. </section>
  971. <footer>
  972. <div class="navfooter">
  973. <ul>
  974. <li>
  975. <b class="toc">
  976. <a href="Ports.xhtml#idm139667872657696">String and bytevector ports</a>
  977. </b>
  978. </li>
  979. <li>
  980. <b class="toc">
  981. <a href="Ports.xhtml#idm139667872621584">Input</a>
  982. </b>
  983. </li>
  984. <li>
  985. <b class="toc">
  986. <a href="Ports.xhtml#idm139667872533328">Output</a>
  987. </b>
  988. </li>
  989. <li>
  990. <b class="toc">
  991. <a href="Ports.xhtml#idm139667872467600">Prompts for interactive consoles (REPLs)</a>
  992. </b>
  993. </li>
  994. <li>
  995. <b class="toc">
  996. <a href="Ports.xhtml#idm139667872412128">Line numbers and other input port properties</a>
  997. </b>
  998. </li>
  999. <li>
  1000. <b class="toc">
  1001. <a href="Ports.xhtml#idm139667872368816">Miscellaneous</a>
  1002. </b>
  1003. </li>
  1004. </ul>
  1005. <p>
  1006. Up: <a accesskey="u" href="Input-Output.xhtml">Input, output, and file handling</a></p>
  1007. <p>
  1008. Previous: <a accesskey="p" href="Reading-and-writing-whole-files.xhtml">Reading and writing whole files</a></p>
  1009. <p>
  1010. Next: <a accesskey="n" href="Format.xhtml">Formatted Output (Common-Lisp-style)</a></p>
  1011. </div>
  1012. </footer>
  1013. </body>
  1014. </html>