REPL-Console.xhtml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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>The REPL (read-eval-print-loop) console</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="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Exiting.xhtml" title="Exiting Kawa"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="The REPL (read-eval-print-loop) console" epub:type="subchapter" id="REPL-Console">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">The REPL (read-eval-print-loop) console</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The read-eval-print-loop (REPL) console is a convenient way to
  23. do simple programming, test out things, and experiment.
  24. As the name implies, the REPL repeatedly (in a loop)
  25. prints out a prompt, reads an input command, evaluates it, then prints the result.
  26. </p>
  27. <p>The REPL is started when you invoke the <code class="literal">kawa</code> command
  28. with no arguments. For example:
  29. </p>
  30. <pre class="screen"><span class="prompt">$ </span><strong class="userinput"><code>kawa</code></strong>
  31. <span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(define pi (* 2 (asin 1)))</code></strong>
  32. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code>(list pi (sqrt pi))</code></strong>
  33. (3.141592653589793 1.7724538509055159)
  34. <span class="prompt">#|kawa:3|# </span><strong class="userinput"><code/></strong>
  35. </pre>
  36. <p>The colors and styles used for the prompt and the user input
  37. depend on user preference and the capabilities of the console device.
  38. (If you read this on a color screen you should see pale green for the
  39. prompt and pale yellow for the user input;
  40. this matches the defaults for the DomTerm console.)
  41. </p>
  42. <p>You can <a class="link" href="Ports.xhtml#Prompts">change the prompt string</a> if you want.
  43. The default format depends on the (programming) language used;
  44. the one shown above is used for Scheme.
  45. It has the form of a comment, which can be convenient for copying
  46. and pasting lines.
  47. </p>
  48. <p>You can <a class="link" href="Named-output-formats.xhtml" title="Named output formats">change the output formatting</a> with
  49. the <code class="literal">--output-format</code> command-line option.
  50. </p>
  51. <p>The basic console has few frills, but should work in any enviroment
  52. where you have a console or terminal. It has no dependencies,
  53. except the kawa <code class="literal">.jar</code> file (and Java):
  54. </p>
  55. <pre class="screen"><span class="prompt">$ </span><strong class="userinput"><code>java kawa-2.92_invoke.jar</code></strong>
  56. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code/></strong>
  57. </pre>
  58. <p>On rare occason you may need to specify the <code class="literal">--console</code> flag.
  59. </p>
  60. <section class="sect2" title="Input line editing and history" epub:type="division" id="idm139667880015696">
  61. <div class="titlepage">
  62. <div>
  63. <div>
  64. <h3 class="title">Input line editing and history</h3>
  65. </div>
  66. </div>
  67. </div>
  68. <p>When typing a command in a console it is helpful to go back
  69. and correct mistakes, repeat and edit previous commands, and so on.
  70. How well you can do this varies a lot depending on which tools you use.
  71. Kawa delegates input editing to an external tool.
  72. The recommended and default input-editing tool is
  73. the <a class="ulink" href="https://github.com/jline/jline3" target="_top">JLine3 library</a>,
  74. which is bundled with the Kawa binary distribution.
  75. </p>
  76. <p>JLine3 handles the normal editing comands, including arrow keys
  77. for moving around in the input, and deleting with backspace or delete.
  78. In general, JLine3 uses the same keybindings as GNU readline,
  79. which are based on Emacs key-bindings.
  80. </p>
  81. <p>You can use the up-arrow to move to previous commands in the
  82. input history and down-arrow to go forwards.
  83. Control-R (“reverse search” searches backwards in the history
  84. for a previous command that contains the search string.
  85. </p>
  86. <p>Multi-line commands are treated as a unit by JLine3:
  87. If Kawa determines that input is “incomplete” it will
  88. ask for continuation lines - and you can go back and edit previous
  89. lines in the same command.
  90. You can explicitly create a multi-line command with Escape-Space.
  91. An entry in the command history may be multiple lines.
  92. </p>
  93. <p>Tab-completion works for Kawa-Scheme identifiers: If you type TAB
  94. after an identifier, Kawa will present a list of possible completions.
  95. </p>
  96. <p>There are multiple alternatives to using JLine3.
  97. You can use GNU readline (if you configured with <code class="literal">--enable-kawa-frontend</code>).
  98. You can use a front-end program like <code class="literal">rlfe</code> or <code class="literal">fep</code>.
  99. You can use Emacs shell or scheme mode.
  100. You can also use DomTerm in line-edit mode, where the browser handles
  101. the editing.
  102. </p>
  103. <div class="variablelist" epub:type="list">
  104. <dl class="variablelist">
  105. <dt class="term"><code class="literal">console:use-jline=</code>[<code class="literal">yes</code>|<code class="literal">no</code>]
  106. </dt>
  107. <dd>
  108. <p>Disable (with <code class="literal">no</code>) or enable (with <code class="literal">yes</code>, which is the default)
  109. input line editing with JLine.
  110. </p>
  111. </dd>
  112. <dt class="term"><code class="literal">console:console:jline-mouse=</code>[<code class="literal">yes</code>|<code class="literal">no</code>]
  113. </dt>
  114. <dd>
  115. <p>Enable (with <code class="literal">yes</code>) mouse click reporting from
  116. most xterm-like terminals to JLine, which means you
  117. can move the input cursor with the mouse.
  118. This is disabled by default because it conflicts with other useful mouse
  119. actions (text selection using drag; middle-button paste; right-button
  120. context menu; and wheel mouse scrolling).
  121. If you enable mouse-reporting, on most terminals you can get the
  122. standard behavior when pressing the shift key. E.g. to enable selection,
  123. drag with the shift key pressed. (However, mouse-wheel scrolling
  124. may not work even with shift pressed.)
  125. </p>
  126. </dd>
  127. </dl>
  128. </div>
  129. <span id="New-Window"/>
  130. </section>
  131. <section class="sect2" title="Running a Command Interpreter in a new Window" epub:type="division" id="idm139667880001952">
  132. <div class="titlepage">
  133. <div>
  134. <div>
  135. <h3 class="title">Running a Command Interpreter in a new Window</h3>
  136. </div>
  137. </div>
  138. </div>
  139. <p>Instead of using an existing terminal window for Kawa’s REPL console,
  140. you can request a new window.
  141. The command-line options <code class="literal">-w</code> creates a new window.
  142. Kawa also creates a new window when it needs to create a REPL
  143. (for example if invoked with no options) and it is not running
  144. in a console.
  145. </p>
  146. <p>You have a number of options for how the window appears and
  147. what it supports, controlled by text following <code class="literal">-w</code>.
  148. All except <code class="literal">-wswing</code> (and <code class="literal">-wconsole</code>) use DomTerm,
  149. so they depend on some kind of web browser technology.
  150. All except <code class="literal">-wswing</code> by default use JLine3 input editing,
  151. if available.
  152. </p>
  153. <div class="variablelist" epub:type="list">
  154. <dl class="variablelist">
  155. <dt class="term"><code class="literal">-w</code>
  156. </dt>
  157. <dd>
  158. <p>Pick the default/preferred console implementation.
  159. You can specify your preference with the <code class="literal">console:type=</code> option,
  160. which is followed by one of the options below (without the <code class="literal">"-w"</code> prefix),
  161. It can also be list of options separated by semi-colons, in which
  162. case they are tried in order.
  163. </p>
  164. <p>The current default (it may change) is as if you specified:
  165. </p>
  166. <pre class="screen">console:type="google-chrome;browser;javafx;swing;console"
  167. </pre>
  168. </dd>
  169. <dt class="term"><code class="literal">-wbrowser</code>
  170. </dt>
  171. <dd>
  172. <p>Creates a Kawa window or tab in your preferred desktop browser.
  173. Kawa starts a builtin HTTP and WebSocket server to communicate with the browser.
  174. </p>
  175. </dd>
  176. <dt class="term"><code class="literal">-wbrowser=<em class="replaceable"><code>command</code></em></code>
  177. </dt>
  178. <dd>
  179. <p>Uses <em class="replaceable"><code>command</code></em> to display the Kawa REPL.
  180. The <em class="replaceable"><code>command</code></em> should include the pattern <code class="literal">%U</code>, which Kawa replaces
  181. with a URL that it listens to.
  182. (Alternatively, it can use the pattern <code class="literal">%W</code>, which Kawa replaces
  183. with the port number of its WebSocket server. However, this feature may be removed.)
  184. If the is no <code class="literal">%</code> in the <em class="replaceable"><code>command</code></em>, Kawa add <code class="literal">" %U"</code>.
  185. Thus <code class="literal">-wbrowser=firefox</code> is the same as <code class="literal">-wbrowser="firefox %U"</code>.
  186. </p>
  187. </dd>
  188. <dt class="term"><code class="literal">-wgoogle-chrome</code>
  189. </dt>
  190. <dd>
  191. <p>Creates a new Google Chrome window in “app mode” - i.e. with no location or menu bar.
  192. This is the same as <code class="literal">-wbrowser="google-chrome --app=%U"</code>.
  193. </p>
  194. </dd>
  195. <dt class="term"><code class="literal">-wqtdomterm</code>
  196. </dt>
  197. <dd>
  198. <p>Uses the <code class="literal">QtDomTerm</code> application.
  199. Same as <code class="literal">-wbrowser="qtdomterm --connect localhost:%W"</code>, where <code class="literal">%W</code>
  200. is the port of the WebSocke server that Kawa starts.
  201. </p>
  202. </dd>
  203. <dt class="term"><code class="literal">-wjavafx</code>
  204. </dt>
  205. <dd>
  206. <p>Creates a new window using JavaFX WebView, which runs in the same JVM as Kawa.
  207. While this doesn’t currently have much in the way of Kawa-specific menus
  208. or other features, it has the most potential for adding them in the future.
  209. However, it does require JavaFX, which is not always available,
  210. and which does not see a lot of love from Oracle. (It uses an old version of WebKit.)
  211. </p>
  212. </dd>
  213. <dt class="term"><code class="literal">-wswing</code>
  214. </dt>
  215. <dd>
  216. <p>Create a console using the Swing toolkit.
  217. This is the old implementation of <code class="literal">-w</code>.
  218. It is deprecated because it only supports the builtin Swing line editing.
  219. (I.e. neither DomTerm or JLine3 features are available, though
  220. “printing” <a class="link" href="Composable-pictures.xhtml" title="Composable pictures">pictures</a> does work.)
  221. </p>
  222. </dd>
  223. <dt class="term"><code class="literal">-wserve</code>
  224. </dt>
  225. <dt class="term"><code class="literal">-wserve=</code><em class="replaceable"><code>port</code></em>
  226. </dt>
  227. <dd>
  228. <p>Starts up an HTTP server (along with a WebSocket server),
  229. but does not automatically create any browser windows.
  230. Instead you can use any modern browser to load <code class="literal">http://localhost:<em class="replaceable"><code>port</code></em>/</code>.
  231. If <em class="replaceable"><code>port</code></em> is not specified, the systems selects it (and prints it out).
  232. </p>
  233. </dd>
  234. <dt class="term"><code class="literal">-wconsole</code>
  235. </dt>
  236. <dd>
  237. <p>Same as <code class="literal">"--"</code> - i.e. it uses the existing console.
  238. </p>
  239. </dd>
  240. <dt class="term"><code class="literal">console:type=</code><em class="replaceable"><code>preference-list</code></em>
  241. </dt>
  242. <dd>
  243. <p>Specify the behavior of plain <code class="literal">-w</code>.
  244. </p>
  245. </dd>
  246. </dl>
  247. </div>
  248. <span id="Using-DomTerm"/>
  249. </section>
  250. <section class="sect2" title="Using DomTerm" epub:type="division" id="idm139667879970864">
  251. <div class="titlepage">
  252. <div>
  253. <div>
  254. <h3 class="title">Using DomTerm</h3>
  255. </div>
  256. </div>
  257. </div>
  258. <p><a class="ulink" href="http://domterm.org" target="_top">DomTerm</a> is a family of terminal emulators that use
  259. the DomTerm JavaScript library.
  260. </p>
  261. <p>You can either have Kawa start DomTerm:
  262. </p>
  263. <pre class="screen">$ kawa <em class="replaceable"><code>options</code></em> -w
  264. </pre>
  265. <p>or start a DomTerm terminal emulator and have it start Kawa:
  266. </p>
  267. <pre class="screen">$ domterm kawa <em class="replaceable"><code>options</code></em> --
  268. </pre>
  269. <p>(You can also start a shell in a <code class="literal">domterm</code> window, and then start <code class="literal">kawa</code>.)
  270. </p>
  271. <p>Either approach works and both give you the benefits of DomTerm:
  272. </p>
  273. <div class="itemizedlist" epub:type="list">
  274. <ul class="itemizedlist" style="list-style-type: disc; ">
  275. <li class="listitem" epub:type="list-item">
  276. <p>A xterm/ansi-compatible terminal emulator,
  277. which means you can use (for example) JLine3 for input editing.
  278. </p>
  279. </li>
  280. <li class="listitem" epub:type="list-item">
  281. <p>You can “print” images, <a class="link" href="Composable-pictures.xhtml" title="Composable pictures">pictures</a>, or HTML elements.
  282. </p>
  283. </li>
  284. <li class="listitem" epub:type="list-item">
  285. <p>Pretty-printing is handled by the terminal,
  286. which means line-breaking is re-computed when window width changes.
  287. </p>
  288. </li>
  289. <li class="listitem" epub:type="list-item">
  290. <p>Hide/show buttons allow you to temporarily hide/unhide the output from a specific command.
  291. </p>
  292. </li>
  293. <li class="listitem" epub:type="list-item">
  294. <p>You can save a session as an HTML file,
  295. which can be viewed later.
  296. (Still with dynamic line-breaking and pretty-printing, as well as working hide/show buttons.)
  297. The file is actually XHTML, so it can be processed with XML-reading tools.
  298. </p>
  299. </li>
  300. <li class="listitem" epub:type="list-item">
  301. <p>Distinct styles for prompts, input, error output and regular output,
  302. which can be customized with CSS.
  303. </p>
  304. </li>
  305. </ul>
  306. </div>
  307. <p>For now it is recommended to use both DomTerm and JLine3.
  308. </p>
  309. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667879960416" class="indexterm"/> <code class="function">domterm-load-stylesheet</code> <em class="replaceable"><code>stylesheet</code></em> [<em class="replaceable"><code>name</code></em>]</p>
  310. <div class="blockquote">
  311. <blockquote class="blockquote">
  312. <p>The string <em class="replaceable"><code>stylesheet</code></em> should be a literal CSS stylesheet
  313. which is downloaded into the current DomTerm console.
  314. The new stylesheet is given the attribute <code class="literal">name=<em class="replaceable"><code>name</code></em></code>,
  315. where <em class="replaceable"><code>name</code></em> defaults to <code class="literal">"Kawa"</code>. If there is an
  316. existing stylesheey whose <code class="literal">name</code> attribute is <em class="replaceable"><code>name</code></em>,
  317. it is replaced.
  318. In this example we change the background color to light gray:
  319. </p>
  320. <pre class="screen">(domterm-load-stylesheet "div.domterm { background-color: lightgray}")
  321. </pre>
  322. </blockquote>
  323. </div>
  324. </section>
  325. </section>
  326. <footer>
  327. <div class="navfooter">
  328. <ul>
  329. <li>
  330. <b class="toc">
  331. <a href="REPL-Console.xhtml#idm139667880015696">Input line editing and history</a>
  332. </b>
  333. </li>
  334. <li>
  335. <b class="toc">
  336. <a href="REPL-Console.xhtml#idm139667880001952">Running a Command Interpreter in a new Window</a>
  337. </b>
  338. </li>
  339. <li>
  340. <b class="toc">
  341. <a href="REPL-Console.xhtml#idm139667879970864">Using DomTerm</a>
  342. </b>
  343. </li>
  344. </ul>
  345. <p>
  346. Up: <a accesskey="u" href="Running.xhtml">How to start up and run Kawa</a></p>
  347. <p>
  348. Previous: <a accesskey="p" href="Scripts.xhtml">Running Command Scripts</a></p>
  349. <p>
  350. Next: <a accesskey="n" href="Exiting.xhtml">Exiting Kawa</a></p>
  351. </div>
  352. </footer>
  353. </body>
  354. </html>