doc: remove "above" and "below" references
[platform/upstream/nodejs.git] / doc / api / repl.markdown
index f298266..b479f99 100644 (file)
@@ -37,10 +37,10 @@ via the following environment variables:
  - `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
    will be saved to the specified file rather than `.node_repl_history` in the
    user's home directory. Setting this value to `""` will disable persistent
-   REPL history.
- - `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of
+   REPL history. Whitespace will be trimmed from the value.
+ - `NODE_REPL_HISTORY_SIZE` - Defaults to `1000`. Controls how many lines of
    history will be persisted if history is available. Must be a positive number.
- - `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults
+ - `NODE_REPL_MODE` - May be any of `sloppy`, `strict`, or `magic`. Defaults
    to `magic`, which will automatically run "strict mode only" statements in
    strict mode.
 
@@ -59,7 +59,7 @@ Previously in Node.js/io.js v2.x, REPL history was controlled by using a
 format. This variable has now been deprecated, and your REPL history will
 automatically be converted to using plain text. The new file will be saved to
 either your home directory, or a directory defined by the `NODE_REPL_HISTORY`
-variable, as documented below.
+variable, as documented [here](#repl_environment_variable_options).
 
 ## REPL Features
 
@@ -85,8 +85,8 @@ a variable to the REPL explicitly by assigning it to the `context` object
 associated with each `REPLServer`.  For example:
 
     // repl_test.js
-    var repl = require('repl'),
-        msg = 'message';
+    const repl = require('repl');
+    var msg = 'message';
 
     repl.start('> ').context.m = msg;
 
@@ -120,7 +120,7 @@ The following key combinations in the REPL have these special effects:
 ### Customizing Object displays in the REPL
 
 The REPL module internally uses
-[util.inspect()][], when printing values. However, `util.inspect` delegates the
+[`util.inspect()`][], when printing values. However, `util.inspect` delegates the
  call to the object's `inspect()` function, if it has one. You can read more
  about this delegation [here][].
 
@@ -138,9 +138,88 @@ and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
     > obj
     { bar: 'baz' }
 
-[Readline Interface]: readline.html#readline_class_interface
-[util.inspect()]: util.html#util_util_inspect_object_options
-[here]: util.html#util_custom_inspect_function_on_objects
+## Class: REPLServer
+
+This inherits from [Readline Interface][] with the following events:
+
+### Event: 'exit'
+
+`function () {}`
+
+Emitted when the user exits the REPL in any of the defined ways. Namely, typing
+`.exit` at the repl, pressing Ctrl+C twice to signal `SIGINT`, or pressing Ctrl+D
+to signal `'end'` on the `input` stream.
+
+Example of listening for `exit`:
+
+    replServer.on('exit', () => {
+      console.log('Got "exit" event from repl!');
+      process.exit();
+    });
+
+
+### Event: 'reset'
+
+`function (context) {}`
+
+Emitted when the REPL's context is reset. This happens when you type `.clear`.
+If you start the repl with `{ useGlobal: true }` then this event will never
+be emitted.
+
+Example of listening for `reset`:
+
+    // Extend the initial repl context.
+    var replServer = repl.start({ options ... });
+    someExtension.extend(r.context);
+
+    // When a new context is created extend it as well.
+    replServer.on('reset', (context) => {
+      console.log('repl has a new context');
+      someExtension.extend(context);
+    });
+
+### replServer.defineCommand(keyword, cmd)
+
+* `keyword` {String}
+* `cmd` {Object|Function}
+
+Makes a command available in the REPL. The command is invoked by typing a `.`
+followed by the keyword. The `cmd` is an object with the following values:
+
+ - `help` - help text to be displayed when `.help` is entered (Optional).
+ - `action` - a function to execute, potentially taking in a string argument,
+   when the command is invoked, bound to the REPLServer instance (Required).
+
+If a function is provided instead of an object for `cmd`, it is treated as the
+`action`.
+
+Example of defining a command:
+
+    // repl_test.js
+    const repl = require('repl');
+
+    var replServer = repl.start();
+    replServer.defineCommand('sayhello', {
+      help: 'Say hello',
+      action: function(name) {
+        this.write(`Hello, ${name}!\n');
+        this.displayPrompt();
+      }
+    });
+
+Example of invoking that command from the REPL:
+
+    > .sayhello Node.js User
+    Hello, Node.js User!
+
+### replServer.displayPrompt([preserveCursor])
+
+* `preserveCursor` {Boolean}
+
+Like [`readline.prompt`][] except also adding indents with ellipses when inside
+blocks. The `preserveCursor` argument is passed to [`readline.prompt`][]. This is
+used primarily with `defineCommand`. It's also used internally to render each
+prompt line.
 
 ## repl.start(options)
 
@@ -190,7 +269,7 @@ You can use your own `eval` function if it has following signature:
       callback(null, result);
     }
 
-On tab completion - `eval` will be called with `.scope` as an input string. It
+On tab completion, `eval` will be called with `.scope` as an input string. It
 is expected to return an array of scope names to be used for the auto-completion.
 
 Multiple REPLs may be started against the same running instance of Node.js.  Each
@@ -198,9 +277,9 @@ will share the same global object but will have unique I/O.
 
 Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
 
-    var net = require('net'),
-        repl = require('repl'),
-        connections = 0;
+    const net = require('net');
+    const repl = require('repl');
+    var connections = 0;
 
     repl.start({
       prompt: 'Node.js via stdin> ',
@@ -208,24 +287,24 @@ Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
       output: process.stdout
     });
 
-    net.createServer(function (socket) {
+    net.createServer((socket) => {
       connections += 1;
       repl.start({
         prompt: 'Node.js via Unix socket> ',
         input: socket,
         output: socket
-      }).on('exit', function() {
+      }).on('exit', () => {
         socket.end();
       })
     }).listen('/tmp/node-repl-sock');
 
-    net.createServer(function (socket) {
+    net.createServer((socket) => {
       connections += 1;
       repl.start({
         prompt: 'Node.js via TCP socket> ',
         input: socket,
         output: socket
-      }).on('exit', function() {
+      }).on('exit', () => {
         socket.end();
       });
     }).listen(5001);
@@ -244,38 +323,7 @@ a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
 For an example of running a REPL instance over `curl(1)`,
 see: https://gist.github.com/2053342
 
-### Event: 'exit'
-
-`function () {}`
-
-Emitted when the user exits the REPL in any of the defined ways. Namely, typing
-`.exit` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
-to signal "end" on the `input` stream.
-
-Example of listening for `exit`:
-
-    r.on('exit', function () {
-      console.log('Got "exit" event from repl!');
-      process.exit();
-    });
-
-
-### Event: 'reset'
-
-`function (context) {}`
-
-Emitted when the REPL's context is reset. This happens when you type `.clear`.
-If you start the repl with `{ useGlobal: true }` then this event will never
-be emitted.
-
-Example of listening for `reset`:
-
-    // Extend the initial repl context.
-    var r = repl.start({ options ... });
-    someExtension.extend(r.context);
-
-    // When a new context is created extend it as well.
-    r.on('reset', function (context) {
-      console.log('repl has a new context');
-      someExtension.extend(context);
-    });
+[`readline.prompt`]: readline.html#readline_rl_prompt_preservecursor
+[`util.inspect()`]: util.html#util_util_inspect_object_options
+[here]: util.html#util_custom_inspect_function_on_objects
+[Readline Interface]: readline.html#readline_class_interface