# Read and change options and hooks

Source: https://libtmux.org/en/haskell/latest/guides/options/

> Set a local history limit with setOption and read it with readOption.

Set a local history limit with [`setOption`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-setoption/) and
read it with [`readOption`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-readoption/). Register one indexed
rename hook with [`setHook`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-sethook/), inspect its definition
with [`readHook`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-readhook/), then observe its effect.

Run from a [provisioned checkout](https://github.com/libtmux/libtmux-haskell/blob/72edb54317bf619c6dc54bff311844f062f8984c/.github/CONTRIBUTING.md#toolchain).

```console
$ cabal run --verbose=0 configure-session
```

<!-- example-output: configure-session -->
```text
local history-limit = 500
rename hook recorded the change
```

## Select a store

| Store | Target |
| --- | --- |
| Server | [`ServerOptions`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-serveroptions/>) |
| Session default | [`GlobalSessionOptions`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-globalsessionoptions/>) |
| Exact session | `SessionOptionsAt session` |
| Window default | [`GlobalWindowOptions`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-globalwindowoptions/>) |
| Physical window | `WindowOptionsAt window` |
| Exact pane | `PaneOptionsAt pane` |

[`LocalOption`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-localoption/>) reads the selected store. [`EffectiveOption`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-effectiveoption/>) includes native
inheritance. An empty [`StringValue`](<https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-stringvalue/>) and an absent value are distinct.
[`unsetOption`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-unsetoption/>) removes an override or resets a built-in global value.
User options use an at sign followed by ASCII letters, digits, hyphens,
underscores or dots. Their values remain literal bytes and cannot contain NUL.

Built-in scalar operations use definitions from the pinned releases, from
3.3a through 3.8. An incompatible scope or kind raises [`InvalidSetting`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-invalidsetting/>)
before dispatch. tmux checks its own value constraints. Option arrays are
listed in the catalogue. Their mutation APIs remain open.

Hooks use session, window or pane stores according to the event's native
scope. [`setHook`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-sethook/>) replaces one numeric entry. tmux parses its command source
when registering it. [`readHook`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-readhook/>) returns the local native listing, keeping
keys and line endings. [`unsetHook`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-unsethook/>) clears the local override. Custom hook
operations and writing named keys remain open.

Reads are bounded to 64 KiB. A failed after-hook raises
[`SettingReadHookFailed`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-settingreadhookfailed/>); the reader does not use hook output as a value.
See [mutation recovery](https://libtmux.org/en/haskell/latest/guides/mutations/) for command outcomes and deadlines.

## Find a built-in name

Use [`serverCapabilities`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-servercapabilities/) to read the version
of the daemon you opened. Pass those capabilities to
[`lookupOption`](https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-lookupoption/) or
[`lookupHook`](https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-lookuphook/). Use the full native name, such
as [`history-limit`](https://libtmux.org/en/tmux/latest/catalog/options/history-limit/) or [`after-new-session`](https://libtmux.org/en/tmux/latest/catalog/hooks/after-new-session/). The
[creation example](https://libtmux.org/en/haskell/latest/guides/creation/) prints the option's value kind and the
hook's scope.

[`knownOptions`](https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-knownoptions/) lists names, types, supported
scopes, array shape and named choices.
[`knownHooks`](https://libtmux.org/en/haskell/latest/reference/libtmux-options-v-knownhooks/) lists events and their scopes.
Both report when each entry became available within the pinned releases.

The catalogue selects the latest pinned table at or below the daemon's
reported release. The pins span tmux 3.3a through 3.8. Entries already
available at 3.3a report that release as their first qualified version.
A newer daemon can have additional entries. See the
[tmux source pins and table digests](https://github.com/libtmux/libtmux-haskell/blob/72edb54317bf619c6dc54bff311844f062f8984c/scripts/generate_options/sources.json).

## Complete program

<!-- example: configure-session -->
```haskell
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Exception (bracket)
import Control.Monad (unless)
import LibTmux qualified as Tmux
import LibTmux.Creation qualified as Creation
import System.Exit (die)

main :: IO ()
main = do
  let options = Creation.defaultSessionOptions {Creation.process = Creation.ShellCommand "exec /bin/cat"}
  Tmux.withOwnedServer Tmux.defaultOwnedServerConfig $ \server ->
    bracket (Tmux.createSession server options) (Tmux.killSession server . (.session)) $ \created -> do
      let local = Tmux.SessionOptionsAt created.session
          event = "after-rename-window"
      Tmux.setOption server local "history-limit" (Tmux.NumberValue 500)
      stored <- Tmux.readOption server local Tmux.LocalOption "history-limit"
      case stored of
        Just (Tmux.NumberValue rows) -> putStrLn ("local history-limit = " <> show rows)
        _ -> die "Expected a local numeric history limit"
      Tmux.setHook server local event 0 "set-option @last-rename seen"
      hook <- Tmux.readHook server local event
      unless (hook == Just "after-rename-window[0] set-option @last-rename seen\n")
        (die "Expected one entry in the local rename hook")
      Tmux.renameWindow server created.window "review"
      observed <- Tmux.readOption server local Tmux.LocalOption "@last-rename"
      unless (observed == Just (Tmux.StringValue "seen")) (die "The rename hook did not run")
      putStrLn "rename hook recorded the change"
```
