142 lines
34 KiB
HTML
142 lines
34 KiB
HTML
<!DOCTYPE html> <html> <head> <title>repl.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> repl.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
|
|
and evaluates it. Good for simple tests, or poking around the <strong>Node.js</strong> API.
|
|
Using it looks like this:</p>
|
|
|
|
<pre><code>coffee> console.log "#{num} bottles of beer" for num in [99..1]
|
|
</code></pre> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Start by opening up <code>stdin</code> and <code>stdout</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">stdin = </span><span class="nx">process</span><span class="p">.</span><span class="nx">openStdin</span><span class="p">()</span>
|
|
<span class="nv">stdout = </span><span class="nx">process</span><span class="p">.</span><span class="nx">stdout</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Require the <strong>coffee-script</strong> module to get access to the compiler.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">CoffeeScript = </span><span class="nx">require</span> <span class="s">'./coffee-script'</span>
|
|
<span class="nv">readline = </span><span class="nx">require</span> <span class="s">'readline'</span>
|
|
<span class="p">{</span><span class="nx">inspect</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s">'util'</span>
|
|
<span class="p">{</span><span class="nx">Script</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s">'vm'</span>
|
|
<span class="nv">Module = </span><span class="nx">require</span> <span class="s">'module'</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>REPL Setup</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Config</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">REPL_PROMPT = </span><span class="s">'coffee> '</span>
|
|
<span class="nv">REPL_PROMPT_MULTILINE = </span><span class="s">'------> '</span>
|
|
<span class="nv">REPL_PROMPT_CONTINUATION = </span><span class="s">'......> '</span>
|
|
<span class="nv">enableColours = </span><span class="kc">no</span>
|
|
<span class="k">unless</span> <span class="nx">process</span><span class="p">.</span><span class="nx">platform</span> <span class="o">is</span> <span class="s">'win32'</span>
|
|
<span class="nv">enableColours = </span><span class="o">not</span> <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">NODE_DISABLE_COLORS</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Log an error.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">error = </span><span class="nf">(err) -></span>
|
|
<span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span> <span class="p">(</span><span class="nx">err</span><span class="p">.</span><span class="nx">stack</span> <span class="o">or</span> <span class="nx">err</span><span class="p">.</span><span class="nx">toString</span><span class="p">())</span> <span class="o">+</span> <span class="s">'\n'</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <h1>Autocompletion</h1> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Regexes to match complete-able bits of text.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">ACCESSOR = </span><span class="sr">/\s*([\w\.]+)(?:\.(\w*))$/</span>
|
|
<span class="nv">SIMPLEVAR = </span><span class="sr">/(\w+)$/i</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Returns a list of completions, and the completed text.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">autocomplete = </span><span class="nf">(text) -></span>
|
|
<span class="nx">completeAttribute</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="o">or</span> <span class="nx">completeVariable</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="o">or</span> <span class="p">[[],</span> <span class="nx">text</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Attempt to autocomplete a chained dotted attribute: <code>one.two.three</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">completeAttribute = </span><span class="nf">(text) -></span>
|
|
<span class="k">if</span> <span class="nv">match = </span><span class="nx">text</span><span class="p">.</span><span class="nx">match</span> <span class="nx">ACCESSOR</span>
|
|
<span class="p">[</span><span class="nx">all</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">prefix</span><span class="p">]</span> <span class="o">=</span> <span class="nx">match</span>
|
|
<span class="k">try</span> <span class="nv">obj = </span><span class="nx">Script</span><span class="p">.</span><span class="nx">runInThisContext</span> <span class="nx">obj</span>
|
|
<span class="k">catch</span> <span class="nx">e</span>
|
|
<span class="k">return</span>
|
|
<span class="k">return</span> <span class="k">unless</span> <span class="nx">obj</span><span class="o">?</span>
|
|
<span class="nv">obj = </span><span class="nb">Object</span> <span class="nx">obj</span>
|
|
<span class="nv">candidates = </span><span class="nb">Object</span><span class="p">.</span><span class="nx">getOwnPropertyNames</span> <span class="nx">obj</span>
|
|
<span class="k">while</span> <span class="nv">obj = </span><span class="nb">Object</span><span class="p">.</span><span class="nx">getPrototypeOf</span> <span class="nx">obj</span>
|
|
<span class="k">for</span> <span class="nx">key</span> <span class="k">in</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getOwnPropertyNames</span> <span class="nx">obj</span> <span class="k">when</span> <span class="nx">key</span> <span class="o">not</span> <span class="k">in</span> <span class="nx">candidates</span>
|
|
<span class="nx">candidates</span><span class="p">.</span><span class="nx">push</span> <span class="nx">key</span>
|
|
<span class="nv">completions = </span><span class="nx">getCompletions</span> <span class="nx">prefix</span><span class="p">,</span> <span class="nx">candidates</span>
|
|
<span class="p">[</span><span class="nx">completions</span><span class="p">,</span> <span class="nx">prefix</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Attempt to autocomplete an in-scope free variable: <code>one</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">completeVariable = </span><span class="nf">(text) -></span>
|
|
<span class="nv">free = </span><span class="nx">text</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLEVAR</span><span class="p">)</span><span class="o">?</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="nv">free = </span><span class="s">""</span> <span class="k">if</span> <span class="nx">text</span> <span class="o">is</span> <span class="s">""</span>
|
|
<span class="k">if</span> <span class="nx">free</span><span class="o">?</span>
|
|
<span class="nv">vars = </span><span class="nx">Script</span><span class="p">.</span><span class="nx">runInThisContext</span> <span class="s">'Object.getOwnPropertyNames(Object(this))'</span>
|
|
<span class="nv">keywords = </span><span class="p">(</span><span class="nx">r</span> <span class="k">for</span> <span class="nx">r</span> <span class="k">in</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">RESERVED</span> <span class="k">when</span> <span class="nx">r</span><span class="p">[..</span><span class="mi">1</span><span class="p">]</span> <span class="o">isnt</span> <span class="s">'__'</span><span class="p">)</span>
|
|
<span class="nv">candidates = </span><span class="nx">vars</span>
|
|
<span class="k">for</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">keywords</span> <span class="k">when</span> <span class="nx">key</span> <span class="o">not</span> <span class="k">in</span> <span class="nx">candidates</span>
|
|
<span class="nx">candidates</span><span class="p">.</span><span class="nx">push</span> <span class="nx">key</span>
|
|
<span class="nv">completions = </span><span class="nx">getCompletions</span> <span class="nx">free</span><span class="p">,</span> <span class="nx">candidates</span>
|
|
<span class="p">[</span><span class="nx">completions</span><span class="p">,</span> <span class="nx">free</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Return elements of candidates for which <code>prefix</code> is a prefix.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">getCompletions = </span><span class="nf">(prefix, candidates) -></span>
|
|
<span class="nx">el</span> <span class="k">for</span> <span class="nx">el</span> <span class="k">in</span> <span class="nx">candidates</span> <span class="k">when</span> <span class="mi">0</span> <span class="o">is</span> <span class="nx">el</span><span class="p">.</span><span class="nx">indexOf</span> <span class="nx">prefix</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Make sure that uncaught exceptions don't kill the REPL.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">process</span><span class="p">.</span><span class="nx">on</span> <span class="s">'uncaughtException'</span><span class="p">,</span> <span class="nx">error</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>The current backlog of multi-line code.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">backlog = </span><span class="s">''</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>The main REPL function. <strong>run</strong> is called every time a line of code is entered.
|
|
Attempt to evaluate the command. If there's an exception, print it out instead
|
|
of exiting.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">run = </span><span class="nf">(buffer) -></span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>remove single-line comments</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">buffer = </span><span class="nx">buffer</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/</span><span class="p">,</span> <span class="s">"$1$2$3"</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>remove trailing newlines</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">buffer = </span><span class="nx">buffer</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/[\r\n]+$/</span><span class="p">,</span> <span class="s">""</span>
|
|
<span class="k">if</span> <span class="nx">multilineMode</span>
|
|
<span class="nx">backlog</span> <span class="o">+=</span> <span class="s">"</span><span class="si">#{</span><span class="nx">buffer</span><span class="si">}</span><span class="s">\n"</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="nx">REPL_PROMPT_CONTINUATION</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
<span class="k">return</span>
|
|
<span class="k">if</span> <span class="o">!</span><span class="nx">buffer</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">trim</span><span class="p">()</span> <span class="o">and</span> <span class="o">!</span><span class="nx">backlog</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
<span class="k">return</span>
|
|
<span class="nv">code = </span><span class="nx">backlog</span> <span class="o">+=</span> <span class="nx">buffer</span>
|
|
<span class="k">if</span> <span class="nx">code</span><span class="p">[</span><span class="nx">code</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">is</span> <span class="s">'\\'</span>
|
|
<span class="nv">backlog = </span><span class="s">"</span><span class="si">#{</span><span class="nx">backlog</span><span class="p">[...</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">\n"</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="nx">REPL_PROMPT_CONTINUATION</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
<span class="k">return</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="nx">REPL_PROMPT</span>
|
|
<span class="nv">backlog = </span><span class="s">''</span>
|
|
<span class="k">try</span>
|
|
<span class="nv">_ = </span><span class="nx">global</span><span class="p">.</span><span class="nx">_</span>
|
|
<span class="nv">returnValue = </span><span class="nx">CoffeeScript</span><span class="p">.</span><span class="nb">eval</span> <span class="s">"_=(</span><span class="si">#{</span><span class="nx">code</span><span class="si">}</span><span class="s">\n)"</span><span class="p">,</span> <span class="p">{</span>
|
|
<span class="nv">filename: </span><span class="s">'repl'</span>
|
|
<span class="nv">modulename: </span><span class="s">'repl'</span>
|
|
<span class="p">}</span>
|
|
<span class="k">if</span> <span class="nx">returnValue</span> <span class="o">is</span> <span class="kc">undefined</span>
|
|
<span class="nv">global._ = </span><span class="nx">_</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">write</span> <span class="s">"</span><span class="si">#{</span><span class="nx">inspect</span> <span class="nx">returnValue</span><span class="p">,</span> <span class="kc">no</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">enableColours</span><span class="si">}</span><span class="s">\n"</span>
|
|
<span class="k">catch</span> <span class="nx">err</span>
|
|
<span class="nx">error</span> <span class="nx">err</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
|
|
<span class="k">if</span> <span class="nx">stdin</span><span class="p">.</span><span class="nx">readable</span> <span class="o">and</span> <span class="nx">stdin</span><span class="p">.</span><span class="nx">isRaw</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>handle piped input</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">pipedInput = </span><span class="s">''</span>
|
|
<span class="nv">repl =</span>
|
|
<span class="nv">prompt: </span><span class="nf">-></span> <span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span> <span class="nx">@_prompt</span>
|
|
<span class="nv">setPrompt: </span><span class="nf">(p) -></span> <span class="vi">@_prompt = </span><span class="nx">p</span>
|
|
<span class="nv">input: </span><span class="nx">stdin</span>
|
|
<span class="nv">output: </span><span class="nx">stdout</span>
|
|
<span class="kc">on</span><span class="o">:</span> <span class="nf">-></span>
|
|
<span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span> <span class="s">'data'</span><span class="p">,</span> <span class="nf">(chunk) -></span>
|
|
<span class="nx">pipedInput</span> <span class="o">+=</span> <span class="nx">chunk</span>
|
|
<span class="k">return</span> <span class="k">unless</span> <span class="sr">/\n/</span><span class="p">.</span><span class="nx">test</span> <span class="nx">pipedInput</span>
|
|
<span class="nv">lines = </span><span class="nx">pipedInput</span><span class="p">.</span><span class="nx">split</span> <span class="s">"\n"</span>
|
|
<span class="nv">pipedInput = </span><span class="nx">lines</span><span class="p">[</span><span class="nx">lines</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">for</span> <span class="nx">line</span> <span class="k">in</span> <span class="nx">lines</span><span class="p">[...</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">when</span> <span class="nx">line</span>
|
|
<span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span> <span class="s">"</span><span class="si">#{</span><span class="nx">line</span><span class="si">}</span><span class="s">\n"</span>
|
|
<span class="nx">run</span> <span class="nx">line</span>
|
|
<span class="k">return</span>
|
|
<span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span> <span class="s">'end'</span><span class="p">,</span> <span class="nf">-></span>
|
|
<span class="k">for</span> <span class="nx">line</span> <span class="k">in</span> <span class="nx">pipedInput</span><span class="p">.</span><span class="nx">trim</span><span class="p">().</span><span class="nx">split</span> <span class="s">"\n"</span> <span class="k">when</span> <span class="nx">line</span>
|
|
<span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span> <span class="s">"</span><span class="si">#{</span><span class="nx">line</span><span class="si">}</span><span class="s">\n"</span>
|
|
<span class="nx">run</span> <span class="nx">line</span>
|
|
<span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span> <span class="s">'\n'</span>
|
|
<span class="nx">process</span><span class="p">.</span><span class="nx">exit</span> <span class="mi">0</span>
|
|
<span class="k">else</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>Create the REPL by listening to <strong>stdin</strong>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">readline</span><span class="p">.</span><span class="nx">createInterface</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">3</span>
|
|
<span class="nv">repl = </span><span class="nx">readline</span><span class="p">.</span><span class="nx">createInterface</span> <span class="nx">stdin</span><span class="p">,</span> <span class="nx">autocomplete</span>
|
|
<span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span> <span class="s">'data'</span><span class="p">,</span> <span class="nf">(buffer) -></span> <span class="nx">repl</span><span class="p">.</span><span class="nx">write</span> <span class="nx">buffer</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">repl = </span><span class="nx">readline</span><span class="p">.</span><span class="nx">createInterface</span> <span class="nx">stdin</span><span class="p">,</span> <span class="nx">stdout</span><span class="p">,</span> <span class="nx">autocomplete</span>
|
|
|
|
<span class="nv">multilineMode = </span><span class="kc">off</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Handle multi-line mode switch</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">repl</span><span class="p">.</span><span class="nx">input</span><span class="p">.</span><span class="nx">on</span> <span class="s">'keypress'</span><span class="p">,</span> <span class="nf">(char, key) -></span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>test for Ctrl-v</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="k">unless</span> <span class="nx">key</span> <span class="o">and</span> <span class="nx">key</span><span class="p">.</span><span class="nx">ctrl</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">key</span><span class="p">.</span><span class="nx">meta</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">key</span><span class="p">.</span><span class="nx">shift</span> <span class="o">and</span> <span class="nx">key</span><span class="p">.</span><span class="nx">name</span> <span class="o">is</span> <span class="s">'v'</span>
|
|
<span class="nv">cursorPos = </span><span class="nx">repl</span><span class="p">.</span><span class="nx">cursor</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">cursorTo</span> <span class="mi">0</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">clearLine</span> <span class="mi">1</span>
|
|
<span class="nv">multilineMode = </span><span class="o">not</span> <span class="nx">multilineMode</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">_line</span><span class="p">()</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">multilineMode</span> <span class="o">and</span> <span class="nx">backlog</span>
|
|
<span class="nv">backlog = </span><span class="s">''</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="p">(</span><span class="nv">newPrompt = </span><span class="k">if</span> <span class="nx">multilineMode</span> <span class="k">then</span> <span class="nx">REPL_PROMPT_MULTILINE</span> <span class="k">else</span> <span class="nx">REPL_PROMPT</span><span class="p">)</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">cursorTo</span> <span class="nx">newPrompt</span><span class="p">.</span><span class="nx">length</span> <span class="o">+</span> <span class="p">(</span><span class="nv">repl.cursor = </span><span class="nx">cursorPos</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Handle Ctrl-d press at end of last line in multiline mode</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">repl</span><span class="p">.</span><span class="nx">input</span><span class="p">.</span><span class="nx">on</span> <span class="s">'keypress'</span><span class="p">,</span> <span class="nf">(char, key) -></span>
|
|
<span class="k">return</span> <span class="k">unless</span> <span class="nx">multilineMode</span> <span class="o">and</span> <span class="nx">repl</span><span class="p">.</span><span class="nx">line</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>test for Ctrl-d</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="k">unless</span> <span class="nx">key</span> <span class="o">and</span> <span class="nx">key</span><span class="p">.</span><span class="nx">ctrl</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">key</span><span class="p">.</span><span class="nx">meta</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">key</span><span class="p">.</span><span class="nx">shift</span> <span class="o">and</span> <span class="nx">key</span><span class="p">.</span><span class="nx">name</span> <span class="o">is</span> <span class="s">'d'</span>
|
|
<span class="nv">multilineMode = </span><span class="kc">off</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">_line</span><span class="p">()</span>
|
|
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">on</span> <span class="s">'attemptClose'</span><span class="p">,</span> <span class="nf">-></span>
|
|
<span class="k">if</span> <span class="nx">multilineMode</span>
|
|
<span class="nv">multilineMode = </span><span class="kc">off</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">cursorTo</span> <span class="mi">0</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">clearLine</span> <span class="mi">1</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">_onLine</span> <span class="nx">repl</span><span class="p">.</span><span class="nx">line</span>
|
|
<span class="k">return</span>
|
|
<span class="k">if</span> <span class="nx">backlog</span> <span class="o">or</span> <span class="nx">repl</span><span class="p">.</span><span class="nx">line</span>
|
|
<span class="nv">backlog = </span><span class="s">''</span>
|
|
<span class="nv">repl.historyIndex = </span><span class="o">-</span><span class="mi">1</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="nx">REPL_PROMPT</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">write</span> <span class="s">'\n(^C again to quit)'</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">_line</span> <span class="p">(</span><span class="nv">repl.line = </span><span class="s">''</span><span class="p">)</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">close</span><span class="p">()</span>
|
|
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">on</span> <span class="s">'close'</span><span class="p">,</span> <span class="nf">-></span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">output</span><span class="p">.</span><span class="nx">write</span> <span class="s">'\n'</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">input</span><span class="p">.</span><span class="nx">destroy</span><span class="p">()</span>
|
|
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">on</span> <span class="s">'line'</span><span class="p">,</span> <span class="nx">run</span>
|
|
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="nx">REPL_PROMPT</span>
|
|
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
|
|
|
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |