From ccc0bf824d38a0780b85dadcdb3255cd1d1f6eb7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 9 Oct 2020 22:50:46 -0700 Subject: [PATCH] docs: lua: add undocumented wezterm.XXX items Adds docs for some of the newer lua scripting functions. refs: #222 refs: #223 refs: #225 --- docs/config/lua/wezterm/action.md | 26 +++++ docs/config/lua/wezterm/emit.md | 22 +++++ docs/config/lua/wezterm/executable_dir.md | 11 +++ docs/config/lua/wezterm/on.md | 110 ++++++++++++++++++++++ docs/config/lua/wezterm/sleep_ms.md | 8 ++ 5 files changed, 177 insertions(+) create mode 100644 docs/config/lua/wezterm/action.md create mode 100644 docs/config/lua/wezterm/emit.md create mode 100644 docs/config/lua/wezterm/executable_dir.md create mode 100644 docs/config/lua/wezterm/on.md create mode 100644 docs/config/lua/wezterm/sleep_ms.md diff --git a/docs/config/lua/wezterm/action.md b/docs/config/lua/wezterm/action.md new file mode 100644 index 000000000..71e8e02c4 --- /dev/null +++ b/docs/config/lua/wezterm/action.md @@ -0,0 +1,26 @@ +# `wezterm.action` + +Helper for defining key assignment actions in your configuration file. +This is really just sugar for the underlying Lua -> Rust deserialation +mapping that makes it a bit easier to identify where syntax errors may +exist in your configuration file. + +Usage looks like this: + +```lua +local wezterm = require 'wezterm'; +return { + keys = { + {key="{", mods="CTRL", action=wezterm.action{ActivateTabRelative=-1}}, + {key="}", mods="CTRL", action=wezterm.action{ActivateTabRelative=1}}, + } +} +``` + +The parameter is a lua representation of the underlying +[KeyAssignment](https://github.com/wez/wezterm/blob/master/config/src/keyassignment.rs#L114) +enum from the configuration code. These docs aim to spell out sufficient +examples that you shouldn't need to learn to read Rust code, but there +are occasions where newly developed features are not yet documented and +an enterprising user may wish to go spelunking to figure them out! + diff --git a/docs/config/lua/wezterm/emit.md b/docs/config/lua/wezterm/emit.md new file mode 100644 index 000000000..2fc577259 --- /dev/null +++ b/docs/config/lua/wezterm/emit.md @@ -0,0 +1,22 @@ +# `wezterm.emit(event_name, args...)` + +*Since: nightly builds only* + +`wezterm.emit` resolves the registered callback(s) for the specified +event name and calls each of them in turn, passing the additional +arguments through to the callback. + +If a callback returns `false` then it prevents later callbacks from +being called for this particular call to `wezterm.emit`, and `wezterm.emit` +will return `false` to indicate that no additional/default processing +should take place. + +If none of the callbacks returned `false` then `wezterm.emit` will +itself return `true` to indicate that default processing should take +place. + +This function has no special knowledge of which events are defined by +wezterm, or what their required arguments might be. + +See [wezterm.on](on.md) for more information about event handling. + diff --git a/docs/config/lua/wezterm/executable_dir.md b/docs/config/lua/wezterm/executable_dir.md new file mode 100644 index 000000000..ce4dd19a6 --- /dev/null +++ b/docs/config/lua/wezterm/executable_dir.md @@ -0,0 +1,11 @@ +# `wezterm.executable_dir` + +This constant is set to the directory containing the `wezterm` +executable file. + +```lua +local wezterm = require 'wezterm'; +wezterm.log_error("Exe dir " .. wezterm.executable_dir) +``` + + diff --git a/docs/config/lua/wezterm/on.md b/docs/config/lua/wezterm/on.md new file mode 100644 index 000000000..c79fde39c --- /dev/null +++ b/docs/config/lua/wezterm/on.md @@ -0,0 +1,110 @@ +# `wezterm.on(event_name, callback)` + +*Since: nightly builds only* + +This function follows the html/javascript naming for defining event handlers. + +`wezterm.on` causes your specified `callback` to be called when `event_name` +is emitted. Events can be emitted by wezterm itself, or through code/configuration +that you specify. + +`wezterm.on` can register multiple callbacks for the same event; internally +an ordered list of callbacks is maintained for each event. When the event +is emitted, each of the registered callbacks is called in the order that +they were registered. + +If a callback returns `false` it will prevent any callbacks that were registered +after it from being triggered for the current event. Some events have +a defined default action; returning `false` will prevent that default action +from being taken for the current event. + +There is no way to de-register an event handler. However, since the Lua +state is built from scratch when the configuration is reloaded, simply +reloading the configuration will clear any existing event handlers. + +## Predefined Events + +### `open-uri` + +The `open-uri` event is emitted when the `CompleteSelectionOrOpenLinkAtMouseCursor` +key/mouse assignment is triggered. + +The default action is to open the active URI in your browser, but if you +register for this event you can co-opt the default behavior. + +For example, if you prefer to launch your preferred MUA in a new window +in response to clicking on `mailto:` URLs, you could do something like: + +```lua +local wezterm = require 'wezterm'; + +wezterm.on("open-uri", function(window, pane, uri) + local start, end = uri:find("mailto:") + if start == 1 then + local recipient = uri:sub(end+1) + window.perform_action(wezterm.action{SpawnCommandInNewWindow={ + args={"mutt", recipient} + }}); + -- prevent the default action from opening in a browser + return false + end + -- otherwise, by not specifying a return value, we allow later + -- handlers and ultimately the default action to caused the + -- URI to be opened in the browser +end) +``` + +## Custom Events + +You may register handlers for arbitrary events for which wezterm itself +has no special knowledge. It is recommended that you avoid event names +that are likely to be used future versions of wezterm in order to avoid +unexpected behavior if/when those names might be used in future. + +The `wezterm.emit` function and the `EmitEvent` key assignment can be used +emit events. + +In this example, a key is assigned to capture the content of the active +pane, write it to a file and then open that file in the `vim` editor: + +```lua +local wezterm = require 'wezterm'; +local io = require 'io'; +local os = require 'os'; + +wezterm.on("trigger-vim-with-scrollback", function(window, pane) + -- Retrieve the current viewport's text. + -- Pass an optional number of lines (eg: 2000) to retrieve + -- that number of lines starting from the bottom of the viewport. + local scrollback = pane:get_lines_as_text(); + + -- Create a temporary file to pass to vim + local name = os.tmpname(); + local f = io.open(name, "w+"); + f:write(scrollback); + f:flush(); + f:close(); + + -- Open a new window running vim and tell it to open the file + window:perform_action(wezterm.action{SpawnCommandInNewWindow={ + args={"vim", name}} + }, pane) + + -- wait "enough" time for vim to read the file before we remove it. + -- The window creation and process spawn are asynchronous + -- wrt. running this script and are not awaitable, so we just pick + -- a number. We don't strictly need to remove this file, but it + -- is nice to avoid cluttering up the temporary file directory + -- location. + wezterm.sleep_ms(1000); + os.remove(name); +end) + +return { + keys = { + {key="E", mods="CTRL", + action=wezterm.action{EmitEvent="trigger-vim-with-scrollback"}}, + } +} +``` + diff --git a/docs/config/lua/wezterm/sleep_ms.md b/docs/config/lua/wezterm/sleep_ms.md new file mode 100644 index 000000000..698e9f919 --- /dev/null +++ b/docs/config/lua/wezterm/sleep_ms.md @@ -0,0 +1,8 @@ +# `wezterm.sleep_ms(milliseconds)` + +*Since: nightly builds only* + +`wezterm.sleep_ms` suspends execution of the script for the specified +number of milliseconds. After that time period has elapsed, the script +continues running at the next statement. +