hedit

General set of functions to programmatically interact with HEdit.

Members

static, readonly mode :string

The name of the mode the editor is currently in.

static, readonly view :string

The name of the currently active view.

Methods

static command(cmd) → {boolean}

Executes a command.

Parameters:
Name Type Description
cmd string

Command to execute.

Returns:
boolean -

Returns true if the command executed successfully, false otherwise.

Example
hedit.command('q!'); // Exits the editor

static emitKeys(keys)

Emits the given keys as if the user actually typed them.

Parameters:
Name Type Description
keys string

Keys to emit.

Example
hedit.emitKeys('<Escape>:w<Enter>i');

static get(name) → {*}

Retrives the value of an option.

Parameters:
Name Type Description
name string

Option name.

Throws:

Throws if the option name is invalid.

Returns:
* -

The current value of the option.

Example
log.info('Current colwidth:', hedit.get('colwidth'));

static map(mode, from, to, forceopt)

Registers a new key mapping.

Parameters:
Name Type Attributes Default Description
mode string

Mode the new mapping is valid in.

from string

Key to map.

to string

The key sequence that the from key will be expanded to.

force boolean <optional>
false

Skip the check for an existing mapping for the same key.

Throws:

Throws if the mapping registration fails.

See:
  • emitKeys for more information about the format of the keys.

Example
hedit.map('insert', '<C-j>', 'cafebabe');

static registerCommand(name, handler)

Registers a new command, whose implementation is up to the user.

Parameters:
Name Type Description
name string

Command name.

handler CommandCallback

Function implementing the command.

Throws:

Throws if the command registration fails.

Example
hedit.registerCommand('special', n => {
    log.info('The argument is', parseInt(n, 10) % 2 == 0 ? 'even' : 'odd');
});

static registerOption(name, defaultValue, handler)

Registers a new option, whose implementation is up to the user.

Parameters:
Name Type Description
name string

Name of the option.

defaultValue string

Default option value.

handler OptionCallback

Function to be called when the value of the option changes.

Example
hedit.registerOption('cool', false, newValue => {
    if (newValue === 'true') {
        log.info('The coolness is now on!');
        return true;
    } else if (newValue === 'false') {
        log.info('So sad :(');
        return true;
    } else {
        // Invalid value
        return false;
    }
});

static set(name, value)

Sets the value of an option.

Parameters:
Name Type Description
name string

Option name.

value string | number

Value of the option.

Throws:

Throws if there's an error setting the option.

Example
hedit.set('colwidth', 8);

static setTheme(t)

Sets the current theme.

A theme determines the colors used by the editor to render itself. Different parts of the UI can be drawn with different pens, which contains the actual style information. For example, to draw the line offsets bold green and text blue, we can use:

{
    linenos: { fg: '00ff00', bold: true },
    text: '0000ff' // Shortcut for `{ fg: '0000ff' }`
}

The following parts of the UI can be themed:

  • text
  • linenos
  • error
  • block_cursor
  • soft_cursor
  • statusbar
  • commandbar
  • log_debug
  • log_info
  • log_warn
  • log_error
  • log_fatal

Each of these parts can be themed with a pen, which has 4 properties:

  • fg
  • bg
  • bold
  • under

Colors can be specified either using standard ANSI number or hex RGB values, which will be rounded to the closest color supported by the terminal.

If any of the previous properties is missing, the default value will be used.

For an example of the usage of setTheme, see A colorful statusbar.

Parameters:
Name Type Description
t object

Theme description.

Throws:

Throws if an invalid theme description is passed.

See:

static switchMode(name)

Switches the editor to the given mode.

Parameters:
Name Type Description
name string

Name of the mode to switch to.

Example
hedit.switchMode('insert');

Type Definitions

CommandCallback(…args)

Handler of a custom command.

Parameters:
Name Type Attributes Description
args string <repeatable>

All the commands supplied by the user at the moment of the invocation of the command.

OptionCallback(newValue) → {boolean}

Handler of a custom option.

Parameters:
Name Type Description
newValue string

New value of the option.

Returns:
boolean -

Returns true if the change is accepted, false otherwise.

Events

file/change

Event raised when the contents of the file change.

Parameters:
Name Type Description
offset integer

Offset of the change.

len integer

Length of the change.

file/close

Event raised when the open file is closed.

file/open

Event raised when a new file is opened by the user.

file/write

Event raised when the file has been successfully written to disk.

load

Event raised only once when the editor is fully loaded and ready.

mode-switch

Event raised when the user switches mode.

Parameters:
Name Type Description
mode string

New mode set.

quit

Event raised only once when the editor is going to close.

view-switch

Event raised when the user switches view.

Parameters:
Name Type Description
view string

New active view.