#!/usr/bin/env python3
"""optmux-find — optmux Find: a file, a line or a symbol, opened in your editor.

Usage: optmux-find [PANE_ID] [--mode SOURCE] [--query TEXT]
(meant to run in a tmux popup; the active pane by default; --mode and --query start in that source with that
query, as optmux-extract does for a selection)

One picker with five sources, like ctrl-p in vim but remembering where you were:
  omni   (C-o)  tags and file paths ranked together, best match first, each row labeled
  files  (C-f)  git ls-files, then the ignored files find turns up
  grep   (C-g)  git grep, live: the list is re-searched as you type
  tags   (C-t)  a ctags `tags` file; without one, ctags builds it in the background on first use (an ⏳ row at the bottom, then the list fills in) and keeps it fresh
  lsp    (C-l)  symbols from the project's language servers, as you type (see optmux-lsp)
`C-M-f` (the key that opened the picker) cycles them, and their keys jump straight to one. Switching carries
the typed text along (try the omni search as grep, then as tags). Each source keeps its own record: opening
afresh resumes the last source with its own last query and the cursor on the entry it was on, `C-u` clears the
query, and `M-p` (then older ones; `M-n` goes forward) recalls that source's earlier queries (fzf's own history,
which also records queries you left with Esc or a switch). Enter opens the pick in an editor split next to the
coding tool, at the matching line. The source is a tmux option on the server (`@optmux-find-mode`); the rest is in
$OPTMUX_DIR/find/ (per project): SOURCE.{last,history}, and recent.json, what you opened, which pulls
those files and tags up the lists (to the top with no query; in omni, up to 20 places).

Internal: `optmux-find --list MODE QUERY` prints the candidates (what fzf reloads for grep).
"""

import argparse
import fnmatch
import functools
import hashlib
import itertools
import json
import os
import re
import shlex
import shutil
import signal
import subprocess
import sys
import threading
import time
from pathlib import Path

MODES = ["omni", "files", "grep", "tags", "lsp"]
MODE_KEYS = {"omni": "ctrl-o", "files": "ctrl-f", "grep": "ctrl-g", "tags": "ctrl-t", "lsp": "ctrl-l"}
LIVE = {"grep", "omni", "lsp"}  # lists made per query (fzf only displays them): grep searches, omni ranks
MODE_OPTION = "@optmux-find-mode"
CYCLE_KEY = "ctrl-alt-f"
MAX_RESULTS = 3000
# editors that do not take `+LINE FILE`
LINE_ARGS = {
    "code": lambda f, n: ["-g", f"{f}:{n}"], "cursor": lambda f, n: ["-g", f"{f}:{n}"],
    "wtcode": lambda f, n: ["-g", f"{f}:{n}"], "zed": lambda f, n: [f"{f}:{n}"],
    "hx": lambda f, n: [f"{f}:{n}"], "micro": lambda f, n: [f"{f}:{n}"],
}


def run(*cmd, **kw):
    return subprocess.run(cmd, capture_output=True, text=True, **kw)


def tmux(*args, **kw):
    return subprocess.run(["tmux", *args], check=True, **kw)


def tmux_option(name):
    return run("tmux", "show-options", "-gqv", name).stdout.rstrip("\n")


def set_tmux_option(name, value):
    tmux("set-option", "-g", name, value)


def repo_root():
    out = run("git", "rev-parse", "--show-toplevel")
    return out.stdout.strip() if out.returncode == 0 else None


def state_dir():
    """Per project: $OPTMUX_DIR (the project's .NAME.optmux.d) holds a tmux server's own state. Outside optmux,
    the user-wide XDG state dir."""
    base = os.environ.get("OPTMUX_DIR")
    d = Path(base, "find") if base else Path(os.environ.get("XDG_STATE_HOME") or Path.home() / ".local/state", "optmux")
    d.mkdir(parents=True, exist_ok=True)
    return d


# ---- candidates: lines of `FILE \t LINE \t DISPLAY` ----------------------------------------------------

def reap(proc):
    """Wait for a child we are not waiting for otherwise (one we killed, or started and forgot), so it does not stay
    a zombie (`<defunct>`) for as long as this picker is open. Returns `proc`."""
    threading.Thread(target=proc.wait, daemon=True).start()
    return proc


def ignore_patterns():
    """Glob patterns of names to leave out of the file list: every group of `+find: +ignore_files` (see
    optmux-defaults.yaml), and always .git."""
    groups = find_config().get("ignore_files")
    return [".git", *(p for g in (groups.values() if isinstance(groups, dict) else ()) if isinstance(g, list)
                      for p in g if isinstance(p, str))]


def ignore_tag_patterns():
    """Glob patterns of names to leave out of the tags index: every group of `+find: +ignore_tags` (see
    optmux-defaults.yaml). Data files and generated code hold hundreds of thousands of "symbols" (every JSON key is one
    to ctags) that bury the real ones and make the tags list take minutes."""
    groups = find_config().get("ignore_tags")
    return [p for g in (groups.values() if isinstance(groups, dict) else ()) if isinstance(g, list)
            for p in g if isinstance(p, str)]


def list_files(_query):
    """Every file under the working directory: what git knows first (tracked and untracked, minus ignored), then the
    ignored ones (scratch scripts, local notes, generated files); minus `+find: +ignore_files`. Outside a repo, all of it."""
    known = []
    if repo_root():
        known = run("git", "ls-files", "-co", "--exclude-standard").stdout.splitlines()
    ignored = ignore_patterns()
    prune = [x for pat in ignored for x in ("-name", pat, "-o")][:-1]
    rest = run("find", ".", "(", *prune, ")", "-prune", "-o", "-type", "f", "-print").stdout.splitlines()
    seen = set()
    for f in [*sorted(known), *sorted(f.removeprefix("./") for f in rest)]:
        if f and f not in seen and not any(fnmatch.fnmatchcase(n, pat) for n in f.split("/") for pat in ignored) and os.path.isfile(f):
            seen.add(f)
            yield row("files", "file", os.path.basename(f), f, 1)


MIN_GREP_QUERY = 2  # characters; one letter matches half the repo and only makes the list churn


def grep_command(query, fixed=False):
    # smart case, like fzf and ripgrep: ignore case unless the query has a capital
    flags = [*([] if query != query.lower() else ["-i"]), *(["-F"] if fixed else [])]
    return (["git", "grep", "-nI", "--no-color", *flags, "-e", query] if repo_root()
            else ["grep", "-rnI", "--exclude-dir=.git", *flags, "-e", query, "."])


def list_grep(query):
    """Matches of a regex, or of the literal text while the regex is still half-typed (`foo(`, `[a-`), sorted by
    file and line (git grep's own order varies from run to run: it searches on several threads)."""
    if len(query.strip()) < MIN_GREP_QUERY:
        return
    for fixed in (False, True):
        proc = subprocess.Popen(grep_command(query, fixed), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
                                text=True, errors="replace")
        # fzf kills us on the next keystroke: take the search down with us rather than leave it running
        signal.signal(signal.SIGTERM, lambda *_: (proc.kill(), sys.exit(1)))
        hits = []
        for line in proc.stdout:
            m = re.match(r"(.+?):(\d+):(.*)", line.rstrip("\n"))
            if m:
                hits.append((m[1].removeprefix("./"), int(m[2]), m[3].strip()))
            if len(hits) >= MAX_RESULTS:
                reap(proc).kill()
                break
        status = 0 if len(hits) >= MAX_RESULTS else proc.wait()
        if status not in (2, 128) or hits:  # 128 (git) / 2 (grep): not a valid regex; retry as plain text
            break
    for f, n, text in sorted(hits):
        yield row("grep", "hit", f"{os.path.basename(f)}:{n}", f, n, text=text)


def find_ctags():
    """The ctags to use, universal-ctags if it is around. Homebrew's dirs come first on the search path, as in
    a git hook (a tmux popup's PATH may lack them, and /usr/bin/ctags is macOS's BSD one, which reads no file
    list and knows only C and a few others)."""
    path = os.pathsep.join(d for d in ("/opt/homebrew/bin", "/usr/local/bin", os.environ.get("PATH", "")) if d)
    return next((p for c in ("universal-ctags", "uctags", "ctags") if (p := shutil.which(c, path=path))), None)


LOCK_TTL = 300    # seconds a build may hold the lock before it is presumed dead
RETRY_AFTER = 600  # seconds to leave a failing ctags alone
CHECK_TAGS = True  # check tags for freshness (and build them) when listing; off for fzf's per-keystroke reloads


def tags_cache(root):
    """Where optmux keeps its own tags for the project at `root` (in the project's state, keyed by root)."""
    return state_dir() / f"optmux-{hashlib.sha1(root.encode()).hexdigest()[:10]}.tags"


TAGS_FORMAT = "kind-names"  # changes when what we ask ctags for does: tags built the old way are stale


def tags_stamp(root):
    """What the tags are built from: the commit, and which tracked files are changed since and when. Differs once
    something worth re-indexing happened."""
    dirty = []  # tracked files only, the ones that get indexed
    status = run("git", "--no-optional-locks", "status", "--porcelain=v1", "-uno", cwd=root).stdout
    for line in status.splitlines():
        path = line[3:].split(" -> ")[-1]
        try:
            dirty.append(f"{path}:{os.stat(Path(root, path)).st_mtime_ns}")
        except OSError:
            dirty.append(path)
    head = run("git", "rev-parse", "HEAD", cwd=root).stdout.strip()
    return hashlib.sha1("\n".join([TAGS_FORMAT, *ignore_tag_patterns(), head, *dirty]).encode()).hexdigest()


def project_files(root):
    """What to index: the files git tracks (like a ctags hook would, `git ls-files`), minus optmux's own state in
    case the project does not ignore it. NUL-separated, or git quotes odd names (`"\\303\\251.py"`)."""
    files = run("git", "ls-files", "-z", cwd=root).stdout.split("\0")
    mine = os.path.relpath(state_dir(), root)
    skip = ignore_tag_patterns()
    return [f for f in files if f and not f.startswith(mine + os.sep) and os.path.isfile(Path(root, f))
            and not any(fnmatch.fnmatchcase(n, pat) for n in f.split("/") for pat in skip)]


def build_tags(root, out):
    """Index the project into `out` (replacing it only once complete): a file list on stdin, paths relative to the
    project root (not to `out`). Universal and exuberant ctags both read a list; line numbers and kind names (`K`: `function`, not `f`) are asked
    for first and dropped if refused (we then find the line from the pattern)."""
    ctags = find_ctags()
    if not ctags:
        return False
    stamp = tags_stamp(root)  # before indexing: a change made meanwhile must still count as stale
    stdin = "\n".join(project_files(root)) + "\n"
    tmp = out.with_suffix(".tmp")
    for flags in (["--fields=+nK", "--tag-relative=no"], ["--fields=+n", "--tag-relative=no"], ["--tag-relative=no"], []):
        tmp.unlink(missing_ok=True)
        done = subprocess.run([ctags, *flags, "-L", "-", "-f", str(tmp)], input=stdin, text=True, cwd=root,
                              stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if done.returncode == 0 and tmp.is_file():
            tmp.replace(out)
            out.with_suffix(".stamp").write_text(stamp)
            return True
    tmp.unlink(missing_ok=True)
    return False


def take_lock(lock):
    """Atomically become the one process building tags; False if another is (and has not been at it for ages)."""
    for _ in range(2):
        try:
            os.close(os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY))
            return True
        except FileExistsError:
            try:
                if time.time() - lock.stat().st_mtime < LOCK_TTL:
                    return False
                lock.unlink()  # presumed dead
            except FileNotFoundError:
                pass
    return False


def build_locked(root, out):
    """Build, remembering a failure so the next opens leave a broken ctags alone for a while. Lock held."""
    try:
        if build_tags(root, out):
            write_tag_rows(out)
        else:
            out.with_suffix(".failed").touch()
    finally:
        out.with_suffix(".lock").unlink(missing_ok=True)


def refresh_tags(root):
    """Rebuild the tags in the background, detached, so opening the picker never waits for it."""
    reap(subprocess.Popen([os.path.abspath(sys.argv[0]), "--build-tags", root], start_new_session=True,
                          stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))


def tags_file(check=None):
    """The tags of the project (the git repository of the working directory): built by ctags on first use and refreshed
    in the background once the project changed (this open still uses the tags it has). Always optmux's own, never a
    `tags` file in the project: what it holds (patterns without line numbers, JSON keys, a hook's options) is not
    ours to know, and the picker depends on what it holds. `check` False only reads what exists: fzf re-lists omni on
    every keystroke."""
    root = repo_root()
    if not root:
        return None  # no git to say what to index, and a whole directory tree is no project
    out = tags_cache(root)
    if (CHECK_TAGS if check is None else check) and find_ctags():
        failed, lock = out.with_suffix(".failed"), out.with_suffix(".lock")
        recently_failed = failed.exists() and time.time() - failed.stat().st_mtime < RETRY_AFTER
        if not recently_failed:
            try:  # (none yet: the first build is no different, the list shows a wait row until it is done)
                stale = not out.is_file() or out.with_suffix(".stamp").read_text() != tags_stamp(root)
            except OSError:
                stale = True
            if stale and take_lock(lock):
                try:
                    refresh_tags(root)
                except OSError:
                    lock.unlink(missing_ok=True)
    return out if out.is_file() else None


def lock_live(out):
    """Whether someone is building the tags at `out` (and has not been at it for ages)."""
    try:
        return time.time() - out.with_suffix(".lock").stat().st_mtime < LOCK_TTL
    except OSError:
        return False


def tags_building():
    """Whether there is nothing to list yet but something is coming: a build is on and the tags or their drawn
    rows (see write_tag_rows) are not there. Once there are rows, even old ones, they are listed meanwhile."""
    root = repo_root()
    if not root:
        return False
    out = tags_cache(root)
    return lock_live(out) and not (out.is_file() and out.with_suffix(".rows").is_file())


def tags_for_editors():
    """Path of tags an editor can use from anywhere, or None. Ours name files relative to the project root, but an
    editor reads them relative to the tags file (vim), which lives elsewhere: this copy has absolute paths."""
    found = tags_file()
    deadline = time.time() + 120
    while not found and tags_building() and time.time() < deadline:  # (an editor wants tags now: wait for the first build)
        time.sleep(0.2)
        found = tags_file(check=False)
    root = repo_root()
    if not found or not root:
        return found
    absolute = found.with_suffix(".abs.tags")
    if not absolute.is_file() or absolute.stat().st_mtime < found.stat().st_mtime:
        prefix = os.path.join(root, "").encode()
        lines = []
        for line in found.read_bytes().split(b"\n"):  # (bytes, split on newlines only: patterns may hold form feeds and the like)
            name, tab, rest = line.partition(b"\t")
            if tab and not name.startswith(b"!_"):
                line = name + tab + prefix + rest if not rest.startswith(b"/") else line
            lines.append(line)
        temp = absolute.with_suffix(f".{os.getpid()}")
        temp.write_bytes(b"\n".join(lines))
        os.replace(temp, absolute)  # (an editor never reads half of it)
    return absolute


@functools.lru_cache(maxsize=256)
def file_lines(root, path):
    try:
        return tuple(Path(root, path).read_text(errors="replace").splitlines())
    except OSError:
        return ()


@functools.lru_cache(maxsize=256)
def file_index(root, path):
    """Each distinct line of a file and the number of its first occurrence, so a tag's pattern is found without
    reading the file again (a generated file can have ten thousand tags)."""
    index = {}
    for n, line in enumerate(file_lines(root, path), 1):
        index.setdefault(line, n)
    return index


def tag_line(root, path, excmd):
    """Line number of a tag whose ex command is a /pattern/ rather than a number (older ctags give no line)."""
    if excmd.isdigit():
        return excmd
    pattern = re.sub(r"^[/?]\^?|\$?[/?]$", "", excmd).replace("\\/", "/").replace("\\\\", "\\")
    found = file_index(str(root), path).get(pattern)  # (ctags patterns are whole lines: one lookup, not a scan)
    if found:
        return str(found)
    for n, line in enumerate(file_lines(str(root), path), 1):  # (a pattern cut short or an edited file: the slow way)
        if pattern and pattern in line:
            return str(n)
    return "1"


def render_tags(tags):
    """(name, row) of every tag in a tags file, rows as `row` draws them. Slow on a big project (hundreds of
    thousands of tags: seconds), so tag_entries keeps the result."""
    root = repo_root() or os.getcwd()
    for raw in tags.read_text(errors="replace").splitlines():
        if raw.startswith("!_TAG_"):
            continue
        name, path, rest = (raw.split("\t", 2) + ["", ""])[:3]
        excmd, _, ext = rest.partition(';"')
        fields = dict(f.split(":", 1) for f in ext.split("\t") if ":" in f)
        kind = fields.get("kind") or (ext.split("\t")[1] if len(ext.split("\t")) > 1 and ":" not in ext.split("\t")[1] else "")
        line = fields.get("line") or tag_line(root, path, excmd)
        entry = row("tags", kind, name, path, line)
        yield what_of(entry)[2], entry


ROWS_FORMAT = "rows1"  # changes when what a row looks like does (the icon style is part of the head, too)


def write_tag_rows(tags):
    """Keep the drawn rows of `tags` beside it (`NAME \t ROW` per line), so opening the picker reads a file
    instead of parsing and drawing every tag again. Made by the background build; made on the spot if missing."""
    rows = tags.with_suffix(".rows")
    for old in rows.parent.glob(rows.name + ".*"):  # (left by a listing fzf killed half way)
        try:
            if time.time() - old.stat().st_mtime > 60:
                old.unlink()
        except OSError:
            pass
    temp = rows.with_name(f"{rows.name}.{os.getpid()}")
    with open(temp, "w") as f:
        f.write(f"{ROWS_FORMAT} {icon_style()}\n")
        for name, entry in render_tags(tags):
            f.write(f"{name}\t{entry}\n")
    os.replace(temp, rows)
    return rows


def tag_entries(warn=False):
    """(key, row) of every tag, the key being what recency remembers (tag_key). Rows are not parsed here: they
    come from the file write_tag_rows keeps. Leaves PENDING a note while the first build is on."""
    tags = tags_file()
    if not tags:
        if tags_building():
            PENDING.append(("tags",))
        elif warn:
            print("no tags: needs a ctags that reads a file list, like universal-ctags (brew install universal-ctags); macOS's own does not", file=sys.stderr)
        return
    rows = tags.with_suffix(".rows")
    head = f"{ROWS_FORMAT} {icon_style()}\n"
    for _ in range(2):
        try:
            with open(rows) as f:
                # (a build in progress writes the rows last: until it is done, old ones are better than drawing them twice)
                if f.readline() == head and (rows.stat().st_mtime >= tags.stat().st_mtime or lock_live(tags)):
                    for line in f:
                        name, _, entry = line[:-1].partition("\t")
                        yield f"{name}\t{entry.partition(chr(9))[0]}", entry
                    return
        except OSError:
            pass
        if lock_live(tags):
            PENDING.append(("tags",))
            return
        write_tag_rows(tags)


def list_tags(_query, warn=True):
    for _, entry in tag_entries(warn):
        yield entry


RECENT_KEEP = 300   # entries remembered
RECENT_BOOST = 20   # places the most recently opened entry may jump in omni; the 20th gets none


def tag_key(name, path):
    return f"{name}\t{path}"


def load_recent():
    """Keys of what was opened here (a file's path, or a tag's name and path), most recent first."""
    try:
        keys = json.loads((state_dir() / "recent.json").read_text())
        return [k for k in keys if isinstance(k, str)]
    except (OSError, ValueError):
        return []


def record_opened(mode, entry):
    """Remember an opened entry: its file, and its tag or symbol too if it was one."""
    f = entry.split("\t", 1)[0]
    source, _, name, _ = what_of(entry)
    keys = [f]
    if source in ("tags", "lsp"):
        keys.insert(0, tag_key(name, f))
    (state_dir() / "recent.json").write_text(json.dumps((keys + [k for k in load_recent() if k not in keys])[:RECENT_KEEP]))


def by_recency(entries, key_of, recent):
    """Entries with the recently opened first (most recent first), the rest in their order."""
    where = {k: i for i, k in enumerate(recent)}
    return sorted(entries, key=lambda e: where.get(key_of(e), len(recent)))


def recent_first(pairs, recent):
    """(key, row) pairs with the recently opened ones first (most recent first), the rest in their order: one pass."""
    where = {k: i for i, k in enumerate(recent)}
    picked, rest = [], []
    for pair in pairs:
        (picked if pair[0] in where else rest).append(pair)
    picked.sort(key=lambda pair: where[pair[0]])
    return picked + rest


def boost(keys, recent):
    """Positions of `keys` (given in match order) after recency pulls the recently opened ahead: the most recent
    by up to RECENT_BOOST places, the next one a place less, and so on. Bounded, so a strong match still beats
    a weak recent one."""
    where = {k: i for i, k in enumerate(recent)}
    return sorted(range(len(keys)), key=lambda p: (p - max(0, RECENT_BOOST - where.get(keys[p], RECENT_BOOST)), p))


ICON_STYLES = {  # the row's kind, drawn: nerd needs a Nerd Font, emoji work in any terminal, text always does, none draws nothing
    "emoji": {"file": "📄", "dir": "📁", "hit": "🔍", "sym": "🔖", "call": "⚡️", "type": "🧩", "mod": "📖", "pkg": "📦",
              "val": "🏷️", "install": "⚠️", "wait": "⏳", "note": "💬"},
    "nerd": {"file": "\uf016", "dir": "\uf07b", "hit": "\uf002", "sym": "\ueb63", "call": "\uf0e7", "type": "\uf12e",
             "mod": "\uf1b2", "pkg": "\uf1b3", "val": "\uf02b", "install": "\uf071", "wait": "\uf252", "note": "\uf05a"},
    "text": {"file": "-", "dir": "d", "hit": "/", "sym": "o", "call": "f", "type": "t", "mod": "m", "pkg": "p", "val": "v",
             "install": "!", "wait": "~", "note": "i"},
}
ICON_STYLES["none"] = {kind: "" for kind in ICON_STYLES["text"]}
DEFAULT_ICONS = "nerd"
# What a symbol is (the words ctags and optmux-lsp print), by group, so five icons say it at a glance
SYMBOL_GROUPS = {
    "call": "function func method methodspec constructor operator event macro procedure subroutine routine".split(),
    "type": "class struct interface enum typeparameter typedef union trait protocol alias typealias record".split(),
    "mod": "module namespace file library unit".split(),
    "pkg": ["package"],
    "val": ("variable var constant const field member property enummember enumerator define local key string number "
            "boolean array object null").split(),
}
SYMBOL_GROUP = {kind: group for group, kinds in SYMBOL_GROUPS.items() for kind in kinds}
# ctags' one-letter kinds, if that is all a tags file has (they differ by language: a best guess)
LETTER_GROUP = {**dict.fromkeys("fm", "call"), **dict.fromkeys("cstiegu", "type"), **dict.fromkeys("vdr", "val"),
                "p": "pkg", "n": "mod"}
SOURCE_LETTERS = {"files": "F", "grep": "G", "tags": "T", "lsp": "L"}
SOURCE_NAMES = {"files": "File path", "grep": "Grep match", "tags": "Symbol", "lsp": "Symbol"}
FIELD_SEP = "\x1f"  # between the parts of a row's last field (see row)


@functools.lru_cache(maxsize=1)
def find_config():
    """The `+find:` section of the optmux config ($OPTMUX_DIR/find.json), {} if there is none."""
    try:
        config = json.loads(Path(os.environ["OPTMUX_DIR"], "find.json").read_text())
    except (KeyError, OSError, ValueError):
        return {}
    return config if isinstance(config, dict) else {}


@functools.lru_cache(maxsize=1)
def icon_style():
    """The `icons` setting, or OPTMUX_ICONS."""
    style = os.environ.get("OPTMUX_ICONS") or find_config().get("icons")
    return style if style in ICON_STYLES else DEFAULT_ICONS


def kind_group(word):
    """The icon kind (call, type, mod, val, or sym for the rest) of a kind word, or of a ctags kind letter."""
    word = word.strip()
    if len(word) == 1:
        return LETTER_GROUP.get(word, "sym")
    return SYMBOL_GROUP.get(word.lower(), "sym")


def with_icon(kind, text):
    icon = ICON_STYLES[icon_style()][kind]
    if kind == "install" and icon:  # (the emoji is yellow already)
        icon = f"{YELLOW}{icon}{UNDIM}"
    return f"{icon} {text}" if icon else text


def split_icon(display):
    """(kind, rest) of a text displayed by with_icon, whatever style it was drawn in; kind None if it has no icon."""
    for icons in ICON_STYLES.values():
        for kind, icon in icons.items():
            if icon and display.startswith(icon + " "):
                return kind, display[len(icon) + 1:]
    return None, display


DIM, UNDIM = "\x1b[2m", "\x1b[0m"
BOLD, YELLOW_BOLD = "\x1b[1m", "\x1b[1;33m"


# Colors for where a row is, in the list and the preview alike: the file name a dark steel blue (not clashing with
# a symbol's name), its directory a dark green, the line number yellow. No bold in the list: fzf bolds the selection.
FILE_COLOR, DIR_COLOR, YELLOW = "\x1b[38;5;67m", "\x1b[38;5;65m", "\x1b[33m"


def list_file_line(base, line):
    """`file.go:105` as the list draws it: the file name blue, the colon muted, the line number yellow."""
    return f"{FILE_COLOR}{base}{UNDIM}{DIM}:{UNDIM}{YELLOW}{line}{UNDIM}"


def file_line(base, line):
    """`file.go:105` as the preview draws it: the file name bold blue, the colon muted, the line number bold yellow."""
    return f"{BOLD}{FILE_COLOR}{base}{UNDIM}{DIM}:{UNDIM}{YELLOW_BOLD}{line}{UNDIM}"


def row(source, kind, name, path, line, container="", text="", detail=""):
    """A candidate: `FILE \t LINE \t DISPLAY \t TEXT \t WHAT`. Every source is shown the same way: the source's letter
    and what it is (an icon), the name, then where: `file:line` and the directory in parens, dim. TEXT is
    what grep matched (the list shows it only when there is no preview). WHAT (SOURCE, kind word, name,
    container, then which language server, and grep's text, joined by FIELD_SEP) is for the program: recency, the preview's header."""
    group = kind if kind in ("file", "hit") else kind_group(kind)
    # (source lines have tabs, which would split the row, and escape sequences, which --ansi would draw)
    clean = lambda x: re.sub(r"[\x00-\x1f\x7f]", " ", str(x)).strip()
    name, container, detail, text = clean(name), clean(container), clean(detail), clean(text)
    directory, base = os.path.split(path)
    drawn = name  # (`name` itself goes in WHAT, plain)
    if source == "files":
        drawn = f"{FILE_COLOR}{name}{UNDIM}"
    elif source == "grep":
        drawn = list_file_line(base, line)
    where = "" if source in ("files", "grep") else f"  {list_file_line(base, line)}"
    shown = f"{DIM}{SOURCE_LETTERS[source]}{ICON_STYLES[icon_style()][group]}{UNDIM} {drawn}{where}"
    if directory:
        shown += f"  {DIM}({UNDIM}{DIR_COLOR}{directory}{UNDIM}{DIM}){UNDIM}"
    what = FIELD_SEP.join((source, kind, name, container, detail, text[:300]))
    return f"{path}\t{line}\t{shown}\t{text}\t{what}"


def what_of(entry):
    """(source, kind word, name, container) of a row made by `row` (empty strings for a row without them)."""
    parts = (entry.split("\t") + [""] * 5)[4].split(FIELD_SEP)
    return tuple((parts + [""] * 4)[:4])


def what_all(what):
    """(source, kind word, name, container, detail, text) of a row's WHAT field."""
    return tuple((what.split(FIELD_SEP) + [""] * 6)[:6])


OMNI_LIMIT = 100  # rows; the best matches of tags and files together


def rank(matchables, query):
    """Indexes of `matchables` matching `query` the way fzf would rank them, best first (one pass over all of
    them, so scores from different sources compete: an exact file name beats a weak tag hit; of equal
    scores the shorter, i.e. the more specific, wins)."""
    if not query:
        return list(range(len(matchables)))
    out = run("fzf", "--filter", query, "--delimiter=\t", "--with-nth=2", "--tiebreak=length,index",
              input="".join(f"{i}\t{m}\n" for i, m in enumerate(matchables))).stdout
    return [int(line.split("\t", 1)[0]) for line in out.splitlines()]


def lsp_exe():
    return str(Path(os.path.abspath(sys.argv[0])).with_name("optmux-lsp"))


def lsp_start(args):
    """Start `optmux-lsp query ...` without waiting for it, so other work can go on meanwhile."""
    proc = subprocess.Popen([sys.executable, lsp_exe(), "query", *args], stdout=subprocess.PIPE, text=True,
                            stderr=subprocess.DEVNULL)
    # fzf kills us on the next keystroke: take the query with us, or stale ones pile up in front of the servers
    signal.signal(signal.SIGTERM, lambda *_: (proc.kill(), sys.exit(1)))
    return proc


def lsp_finish(proc, timeout):
    """The rows of a query started by lsp_start; [] if it takes too long."""
    try:
        out, _ = proc.communicate(timeout=timeout)
    except subprocess.TimeoutExpired:
        reap(proc).kill()
        return []
    return [line for line in out.splitlines() if line.count("\t") >= 2]


def lsp_query(args, timeout):
    return lsp_finish(lsp_start(args), timeout)


# ---- slow sources: answer now, fill in later -----------------------------------------------------------
# A source that needs a while (a first ctags run, language servers thinking) never holds the list up. It leaves
# what it is waiting for in PENDING, and the list ends with a row for each (an ⏳ one, always last, so it is not
# taken for a result, nor its absence for "nothing found"). While anything is pending, fzf polls (see `--poll`)
# and reloads the list once something is done.

PENDING = []        # ("tags",) or ("lsp", KIND, QUERY): what the lists made in this process are waiting for
WAIT_FETCH = 20     # seconds a background fetch may take before it is presumed dead
WAIT_LABELS = {"tags": "indexing the project with ctags...", "lsp": "waiting for language servers..."}


def waiting_marker():
    """Per picker (the picker exports its own to the fzf it runs), or two pickers would clear each other's."""
    return Path(os.environ.get("OPTMUX_FIND_MARKER") or state_dir() / "waiting")


def lsp_paths(kind, query):
    """(answer file, running file) of a background question to the language servers."""
    directory = state_dir() / "lspcache"
    directory.mkdir(exist_ok=True)
    key = hashlib.sha1(f"{repo_root()}\0{query}".encode()).hexdigest()[:16]
    return directory / f"{kind}-{key}.json", directory / f"{kind}-{key}.run"


def lsp_rows(kind, query):
    """The language servers' rows for `query` (kind: "omni" asks what is quick, "lsp" everything), or None while they
    are being asked in the background. An answer is kept for a while, so the reload that follows it finds it."""
    done, running = lsp_paths(kind, query)
    try:
        answer = json.loads(done.read_text())
        if time.time() - answer["t"] < answer["ttl"]:
            return answer["rows"]
    except (OSError, ValueError, KeyError):
        pass
    try:
        asking = time.time() - running.stat().st_mtime < WAIT_FETCH
    except OSError:
        asking = False
    if not asking:
        for old in done.parent.iterdir():  # (the answers of earlier queries; and the asking of an earlier query of this kind)
            try:
                if old.name.startswith(kind + "-") and old.suffix == ".run" and old != running:
                    try:
                        os.kill(int(old.read_text()), signal.SIGTERM)  # (typing on: that answer is not wanted any more)
                    except (OSError, ValueError):
                        pass  # (it is gone already)
                    old.unlink()
                elif time.time() - old.stat().st_mtime > 300:
                    old.unlink()
            except (OSError, ValueError):
                pass
        running.touch()
        try:
            worker = reap(subprocess.Popen([sys.executable, os.path.abspath(sys.argv[0]), "--lsp-fetch", kind, query],
                                  start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL))
            running.write_text(str(worker.pid))
        except OSError:
            running.unlink(missing_ok=True)
            return []
    PENDING.append(("lsp", kind, query))
    return None


def lsp_fetch(kind, query):
    """The background half of lsp_rows: ask, then leave the answer for the next listing."""
    done, running = lsp_paths(kind, query)
    try:
        rows = lsp_query(["--fast", query], 3) if kind == "omni" else lsp_query([query], 8)
        ttl = 2 if any(r.startswith("!wait") for r in rows) else 30 if rows else 3  # (servers still starting: ask again soon)
        temp = done.with_suffix(f".{os.getpid()}")
        temp.write_text(json.dumps({"t": time.time(), "ttl": ttl, "rows": rows}))
        os.replace(temp, done)
    finally:
        running.unlink(missing_ok=True)


def wait_rows():
    """One row per kind of thing pending, to go last in the list (FILE `!wait:KIND`, see marker_kind)."""
    for kind in dict.fromkeys(item[0] for item in PENDING):
        yield f"!wait:{kind}\t0\t{with_icon('wait', WAIT_LABELS[kind])}"


def pending_done(item):
    if item[0] == "tags":
        return not tags_building()
    try:
        return time.time() - lsp_paths(item[1], item[2])[1].stat().st_mtime >= WAIT_FETCH
    except OSError:
        return True  # (the fetch removed its file: it is done)


def candidates(mode, query):
    """The list of a source as the picker shows it: its rows (recent ones first for files and tags), then the wait
    rows. Notes what is pending for `poll`."""
    PENDING.clear()
    rows = list(LISTERS[mode](query)) if mode != "tags" else []
    if mode == "files":  # fzf breaks ties by list order: put what was opened lately first
        rows = by_recency(rows, lambda e: e.split("\t", 1)[0], load_recent())
    elif mode == "tags":
        rows = [entry for _, entry in recent_first(tag_entries(warn=True), load_recent())]
    waits = list(wait_rows())
    try:
        if PENDING:
            waiting_marker().write_text(json.dumps(PENDING))
        else:
            waiting_marker().unlink(missing_ok=True)
    except OSError:
        pass
    return rows + waits


def poll(me, mode):
    """fzf's `every` tick, run when something is pending: the action that reloads the list if any of it is done,
    else nothing."""
    try:
        pending = json.loads(waiting_marker().read_text())
    except (OSError, ValueError):
        return ""
    if not any(pending_done(item) for item in pending):
        return ""
    waiting_marker().unlink(missing_ok=True)  # (the listing makes it again if anything is left)
    again = f'reload({shlex.quote(sys.executable)} {shlex.quote(me)} --list {mode} "$FZF_QUERY")'
    # a source fzf filters itself was started with the search off (the list was only the wait row): on now
    return ("enable-search+" if mode not in LIVE else "") + again


def list_omni(query):
    """Tags, language-server symbols and file paths in one list, the stronger matches higher whichever source they
    come from. A tag or symbol is matched on its name (its path is the file's row), a file on its path; the
    letter and icon say which is which. A symbol the servers know replaces the tag for the same name in the same
    file. What you opened lately is pulled up (see boost); with no query, it comes first."""
    files = list(list_files(""))
    asked = (lsp_rows("omni", query) or []) if query.strip() else []  # (None: still asking, the wait row says so)
    symbols = list(render_lsp(r for r in asked if not r.startswith("!")))  # (never a marker row)
    # ctags and servers often disagree on the line: one of the two for a name in a file
    defined = {(e.split("\t", 1)[0], what_of(e)[2]) for e in symbols}
    recent = load_recent()
    if not query:  # nothing to rank: the recent ones, then the first of the list. Not all of a huge project's tags drawn
        where = {k: i for i, k in enumerate(recent)}
        picked, rest = [], []
        for key, entry in itertools.chain(tag_entries(), ((e.split("\t", 1)[0], e) for e in files)):
            if key in where:
                picked.append((where[key], entry))
            elif len(rest) < OMNI_LIMIT:
                rest.append(entry)
        picked.sort(key=lambda p: p[0])
        yield from [e for _, e in picked][:OMNI_LIMIT]
        yield from rest[:max(0, OMNI_LIMIT - len(picked))]
        return
    rows = []  # (what is matched, recency key, entry)
    for e in symbols:
        f, name = e.split("\t", 1)[0], what_of(e)[2]
        rows.append((name, tag_key(name, f), e))
    for key, e in tag_entries():
        name, _, f = key.partition("\t")
        if (f, name) not in defined:
            rows.append((name, key, e))
    for e in files:
        f = e.split("\t", 1)[0]
        rows.append((f, f, e))
    order = rank([m for m, _, _ in rows], query)
    order = [order[p] for p in boost([rows[i][1] for i in order], recent)]
    for i in order[:OMNI_LIMIT]:
        yield rows[i][2]


MARKER_KINDS = {"!install": "install", "!wait": "wait", "!note": "note"}


BAT = ("bat", "batcat")  # Debian and Ubuntu install it as batcat
CONTROL = re.compile(r"[\x00-\x08\x0b-\x1f\x7f]")  # all but tab and newline: they would recolor or garble the pane


PREVIEW_HEADER_LINES = 5


LANGUAGE_NAMES = {"c": "C", "typescript": "TypeScript", "javascript": "JavaScript"}  # (the rest: capitalized)


def source_title(source, detail):
    """Where a row came from, in words: `Symbol (ctags: tags)`, `Go symbol (lsp: gopls)`, `File path (git ls-files)`,
    `Grep match (git grep)`. A language server says which language the symbol is of (`detail` is `go: gopls`)."""
    name = SOURCE_NAMES[source]
    if source == "files":
        how = "git ls-files + find" if repo_root() else "find"
    elif source == "grep":
        how = "git grep" if repo_root() else "grep"
    elif source == "lsp":
        language, _, server = detail.partition(": ")
        how = f"lsp: {server}" if server else "lsp"
        if language:
            name = f"{LANGUAGE_NAMES.get(language, language.capitalize())} symbol"
    else:
        found = tags_file(check=False)
        shown = os.path.relpath(found, repo_root() or os.getcwd()) if found else ""
        if shown.startswith(".."):  # outside the project: where it is, not how to climb to it
            shown = str(found).replace(str(Path.home()), "~", 1)
        how = f"ctags: {shown}" if found else "ctags"
    return f"{name} ({how})"


def preview_header(what, path, line):
    """The preview's first lines, the same five every time, each saying the same kind of thing whatever the row,
    so that going up and down changes only what is new, and nothing is said twice:
      where it comes from; what it is, then its name (`🔧 method · Gui.Layout`); the module it is in; where it is.
    Returns how many lines it took."""
    source, kind, name, container, detail, text = what_all(what)
    if source not in SOURCE_NAMES:
        return 0
    icons = ICON_STYLES[icon_style()]
    room = int(os.environ.get("FZF_PREVIEW_COLUMNS") or 80) - 8  # (fzf draws its position, 1/43, at the top right)
    directory, base = os.path.split(path)
    directory = directory + "/" if directory else ""
    if source == "files":
        group, what_is, title, module = "file", "file", name, ""
        icon, far, near = icons["dir"], directory, ("", "")
    elif source == "grep":
        group, what_is, title, module = "hit", "match", text or name, ""
        icon, far, near = icons["file"], directory, (base, line)
    else:
        group, what_is, title, module = kind_group(kind), kind or "symbol", name, container
        icon, far, near = icons["file"], directory, (base, line)
    print(f"{DIM}{source_title(source, detail)[:room]}{UNDIM}")
    print(f"{DIM}{icons[group] + ' ' if icons[group] else ''}{what_is} ·{UNDIM} \x1b[1m{title[:max(0, room - len(what_is) - 5)]}\x1b[0m")
    print((f"{DIM}{icons['mod']}{UNDIM} " if icons["mod"] else "") + module[:room] if module else "")  # the module, plain: it is the one line that holds still
    file_name, number = near
    width = len(file_name) + len(str(number)) + 1
    far = far[-max(0, room - width - 3):] if far else ""  # (the end of a long directory, near the file, is what counts)
    # the directory green, the file name bold blue, the colon between them muted, the line number bold yellow
    where = file_line(file_name, number) if file_name else ""
    print(f"{DIM}{icon}{UNDIM} {DIR_COLOR}{far}{UNDIM}{where}".replace(f"{DIM}{UNDIM} ", "", 1))
    print(f"{DIM}{'─' * 200}{UNDIM}")  # (cut to the window by nowrap)
    sys.stdout.flush()  # bat takes over this process
    return PREVIEW_HEADER_LINES


def preview(mode, path, line, display, what, height):
    """What fzf shows beside the selected row: the file around its line, or what a non-file row (install, wait,
    note) means. Uses bat if there is one, else plain numbered lines with the line marked. Long lines are cut,
    not wrapped, so the window holds `height` lines and the marked one stays in view."""
    if not path:  # nothing selected
        return 0
    if marker_kind(mode, path):
        print(split_icon(display)[1])
        return 0
    if os.path.isdir(path):
        print(f"({path} is a directory)")
        return 0
    try:
        target = max(1, int(line))
    except ValueError:
        target = 1
    if height >= PREVIEW_HEADER_LINES + 3:  # (a short window is all code)
        height -= preview_header(what, path, line)
    start = max(1, target - height // 3)
    try:
        with open(path, "rb") as f:
            if b"\0" in f.read(8000):
                print("(binary file)")
                return 0
            f.seek(0)
            bat = next((b for b in BAT if shutil.which(b)), None)
            if bat:
                os.execvp(bat, [bat, "--color=always", "--style=numbers", "--paging=never", "--wrap=never",
                                f"--highlight-line={target}", f"--line-range={start}:{start + height - 1}", "--", path])
            for n, raw in enumerate(f, 1):
                if n >= start + height:
                    break
                if n >= start:
                    mark = "\x1b[1;33m" if n == target else "\x1b[2m"
                    text = CONTROL.sub("·", raw.decode(errors="replace").rstrip("\r\n"))[:300]
                    print(f"{mark}{'>' if n == target else ' '}{n:>5}\x1b[0m {text}")
    except OSError:
        print(f"({path} is not there)")
    return 0


PREVIEW_MIN_COLUMNS = 100  # narrower terminals have no room for the preview (see `<55(hidden)` below)
PREVIEW_KEYS = ("ctrl-/", "ctrl-_")  # Terminal.app and some others send ^_ for ctrl-/ (and for ctrl--)


def preview_showing():
    """Whether the preview is on screen at the start: enabled, and room for it."""
    try:
        return preview_enabled() and os.get_terminal_size(2).columns >= PREVIEW_MIN_COLUMNS
    except OSError:
        return False


def preview_args(me, mode, compact=False):
    """fzf's options for the preview beside the list. `compact`: grep's rows go without the matching text
    (the preview shows it) while the preview is on; the toggle swaps the two."""
    # `<55(hidden)`: fzf compares that with the preview's own width (55% of the screen), so it is hidden when the
    # terminal is under about 100 columns
    args = ["--preview", f"{shlex.quote(sys.executable)} {shlex.quote(me)} --preview {shlex.quote(mode)} {{1}} {{2}} {{3}} {{5}}",
            "--preview-window=right,55%,border-left,nowrap,<55(hidden)"]
    swap = ("+change-with-nth(" + ("3..4|3" if compact else "3|3..4") + ")") if mode == "grep" else ""
    return args + [f"--bind={key}:toggle-preview{swap}" for key in PREVIEW_KEYS]


def preview_enabled():
    """+find: +preview (default on), and OPTMUX_FIND_PREVIEW=0 to turn it off."""
    if os.environ.get("OPTMUX_FIND_PREVIEW") == "0":
        return False
    return find_config().get("preview", True) is not False


def marker_kind(mode, f):
    """What a row's FILE field stands for if it is not a file: "install", "wait" or "note". Only C-l has such rows,
    besides the `!wait:KIND` of a source still at work (a tracked file may well be named `!x`)."""
    if f.startswith("!wait:"):
        return "wait" if f[6:] in WAIT_LABELS else None
    return MARKER_KINDS.get(f.split(":", 1)[0]) if mode == "lsp" else None


def render_lsp(raw_rows):
    """Candidates from optmux-lsp's rows (`FILE \\t LINE \\t NAME ^_ KIND ^_ CONTAINER`). Rows that are not symbols
    (install, wait and note rows) get an icon of their own."""
    for raw in raw_rows:
        f, n, display = raw.split("\t", 2)
        marker = MARKER_KINDS.get(f.split(":", 1)[0])
        if marker:
            yield f"{f}\t{n}\t{with_icon(marker, display)}"
            continue
        name, kind, container, server = (display.split(FIELD_SEP) + ["", "", ""])[:4]
        yield row("lsp", kind, name, f, n, container, detail=server)


def list_lsp(query):
    """Symbols from the project's language servers (see optmux-lsp), as the user types. When there are none, rows
    that say why, each with an icon of its own: one to install a missing server (Enter), others to wait or read."""
    if not query.strip():
        return
    yield from render_lsp(lsp_rows("lsp", query) or [])


LISTERS = {"omni": list_omni, "files": list_files, "grep": list_grep, "tags": list_tags, "lsp": list_lsp}


# ---- opening -------------------------------------------------------------------------------------------

def editor_command(path, line):
    editor = shlex.split(os.environ.get("VISUAL") or os.environ.get("EDITOR") or "vim")
    args = LINE_ARGS.get(os.path.basename(editor[0]), lambda f, n: [f"+{n}", f])(path, line)
    return " ".join(shlex.quote(a) for a in [*editor, *args])


def after_zoom(command):
    """Start `command` only once the pane has its zoomed size. An editor reads the size once at startup; resize-pane -Z
    flips tmux's zoom flag before the pty is resized, so an editor started right away draws for the old, smaller pane
    and garbles when scrolling (:!reset repairs it). Waits for the flag and for the tty to agree (2s at most)."""
    ask = 'tmux display-message -p -t "$TMUX_PANE" "#{window_zoomed_flag} #{pane_height}"'
    return (f'n=0; until [ "$({ask})" = "1 $(stty size | cut -d" " -f1)" ] || [ $((n+=1)) -gt 100 ]; do sleep 0.02; done; '
            f"exec {command}")


def split_pane(pane, command):
    """Run `command` in a new zoomed pane above `pane`, as the other C-M-* splits do: above, or across the window
    edge when out of room."""
    fits = run(os.path.join(os.environ["OPTMUX_DIR"], "bin", "optmux-split-fits"), pane).returncode == 0
    new = tmux("split-window", "-P", "-F", "#{pane_id}", *([] if fits else ["-f"]), "-v", "-b", "-c", os.getcwd(),
               "-t", pane, command, capture_output=True, text=True).stdout.strip()
    tmux("resize-pane", "-Z", "-t", new)


def open_in_split(pane, path, line):
    split_pane(pane, after_zoom(editor_command(path, line)))


def install_here(lang):
    """Install a language server right here, in the picker's own terminal (the popup), so its output (and any
    password prompt) can be seen; the picker comes back when it is done. (A popup cannot open another popup over
    itself, and a pane behind it would hide the result.) Run by sh, whatever shell tmux is set to use."""
    script = (f"OPTMUX_DIR={shlex.quote(os.environ.get('OPTMUX_DIR', ''))} {shlex.quote(sys.executable)} "
              f"{shlex.quote(lsp_exe())} install --yes {shlex.quote(lang)}; printf '\\nPress Enter to return to Find '; read _")
    sys.stdout.write("\033[2J\033[H")
    sys.stdout.flush()
    subprocess.run(["sh", "-c", script])


def dedupe_history(path):
    """fzf appends every accepted query, repeats included; keep the latest occurrence of each (M-p walks back)."""
    try:
        lines = path.read_text().splitlines()
    except OSError:
        return
    kept = list(dict.fromkeys(reversed([l for l in lines if l.strip()])))[::-1]
    if kept != lines:
        path.write_text("\n".join(kept) + "\n" if kept else "")


def remember_query(path, query):
    """fzf records only queries accepted with Enter; also keep the ones left by Esc or a mode switch."""
    if query.strip():
        with open(path, "a") as f:
            f.write(query + "\n")
        dedupe_history(path)


# ---- the picker ----------------------------------------------------------------------------------------

def header(mode):
    tabs = "  ".join(f"[{m} {MODE_KEYS[m].replace('ctrl-', 'C-')}]" if m == mode else f"{m} {MODE_KEYS[m].replace('ctrl-', 'C-')}"
                     for m in MODES)
    return f"{tabs}  C-M-f: next\nC-u: clear;  M-p/M-n: history;  C-/: preview;  Enter: open"  # two lines: the list may be narrow


def parse_output(stdout):
    """(query, key, line) from `fzf --print-query --expect=...`; line is the whole `FILE \t LINE \t DISPLAY` entry
    under the cursor (also when leaving by Esc or a mode key), or None."""
    query, key, *picked = stdout.split("\n")[:-1] or ["", "esc"]
    return query, key, picked[0] if picked else None


def target(entry):
    f, n, _ = (entry.split("\t", 2) + ["1", ""])[:3]
    return f, n


def load_state(mode):
    """(query, entry) this source was left with: its search text, and the entry the cursor was on."""
    try:
        d = json.loads((state_dir() / f"{mode}.last").read_text())
        return str(d.get("query", "")), d.get("entry")
    except (OSError, ValueError, AttributeError):
        return "", None


def save_state(mode, query, entry):
    (state_dir() / f"{mode}.last").write_text(json.dumps({"query": query, "entry": entry}))


ANSI = re.compile(r"\x1b\[[0-9;]*m")


def cursor_position(entries, query, entry, filtered=True):
    """1-based position of `entry` in the list as fzf will show it for `query` (best match first), or None.
    `filtered`: fzf fuzzy-matches the query against the entries (not so for grep, whose list is already the answer).
    The entry is what fzf printed, which has the dim codes of the display stripped (--ansi): compared without them."""
    if not entry or not entries:
        return None
    if query and filtered:
        entries = run("fzf", "--ansi", "--filter", query, "--delimiter=\t", "--with-nth=3", "--tiebreak=index",
                      input="\n".join(entries) + "\n").stdout.splitlines()
    entries = [ANSI.sub("", e) for e in entries]
    entry = ANSI.sub("", entry)
    return entries.index(entry) + 1 if entry in entries else None


def enter_project_root():
    """Paths of every source (git ls-files, grep, tags, language servers) are relative to the project root; the
    editor is started there too. (Tags and language servers cannot be asked for paths relative to a subdirectory.)"""
    root = repo_root()
    if root:
        os.chdir(root)
    return root


def parse_args(argv):
    """([pane id], {mode, query}) of optmux-find's command line."""
    parser = argparse.ArgumentParser(prog="optmux-find", description="OPTMUX Find: a file, a line or a symbol, opened in your editor.")
    parser.add_argument("pane", nargs="?", help="the tmux pane to work on (the active one by default)")
    parser.add_argument("--mode", choices=MODES, help="start in this source")
    parser.add_argument("--query", help="start with this query")
    parser.add_argument("--query-file", help="start with the query in this file (which is deleted): for text no command line should carry")
    ns = parser.parse_args(argv)
    if ns.query_file:
        try:
            ns.query = Path(ns.query_file).read_text()
        except OSError:
            pass
        Path(ns.query_file).unlink(missing_ok=True)
    return ([ns.pane] if ns.pane else []), {k: v for k, v in (("mode", ns.mode), ("query", ns.query)) if v is not None}


def pick(pane, start_mode=None, start_query=None):
    enter_project_root()
    mode = start_mode if start_mode in MODES else tmux_option(MODE_OPTION)
    mode = mode if mode in MODES else MODES[0]
    me = os.path.abspath(sys.argv[0])
    os.environ["OPTMUX_FIND_MARKER"] = str(state_dir() / f"waiting.{os.getpid()}")  # (fzf's --list and --poll inherit it)
    carried = start_query  # what was typed in the source we just switched away from (or handed to us)
    while True:
        # A fresh start resumes the source's own query and the cursor on the entry it was on. After a switch
        # the text just typed comes along (the cursor is only restored if it is the same query as the saved one).
        query, entry = load_state(mode)
        if carried is not None:
            query, entry = carried, (entry if carried == query else None)
        history = state_dir() / f"{mode}.history"
        dedupe_history(history)
        compact = mode == "grep" and preview_showing()
        fzf = ["fzf", "--ansi", "--no-hscroll", "--delimiter=\t",  # (no scrolling the row to show a match: the name stays in view)
               "--with-nth=3" if compact or mode != "grep" else "--with-nth=3..4", "--no-sort" if mode in LIVE else "--tiebreak=index",
               "--print-query", f"--query={query}", f"--prompt={mode} > ", f"--header={header(mode)}",
               f"--history={history}", "--history-size=200",
               "--bind=ctrl-p:up,ctrl-n:down,alt-p:prev-history,alt-n:next-history",  # (history is on M-p/M-n: C-p/C-n keep moving the cursor)
               f"--expect={','.join([*MODE_KEYS.values(), CYCLE_KEY, 'ctrl-c', 'esc'])}"]
        if preview_enabled():  # beside the list, the file at the line; C-/ toggles it
            fzf += preview_args(me, mode, compact)
        if mode in ("lsp", "omni"):  # start the hub (and so the servers) now; they boot while the user types
            reap(subprocess.Popen([sys.executable, lsp_exe(), "status"],
                                  stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))
        rows = candidates(mode, query)
        pos = cursor_position(rows, query, entry, filtered=mode not in LIVE)
        # The list goes in on stdin for every source. Once loaded, move the cursor and stop watching `load`
        # (later changes, from typing, are not ours to steer). The live sources (grep, omni): fzf matches nothing itself and each
        # keystroke re-runs the search, debounced by the sleep (fzf kills a pending reload on the next key).
        # `change` must wait for that first load, because the initial --query counts as a change, and the
        # reload it triggers would put the cursor back on top. (The reload is parenthesized, or it swallows the
        # next binding.)
        first_load = (f"pos({pos})+" if pos else "") + "unbind(load)"
        if mode in LIVE:
            fzf += ["--disabled", "--bind=start:unbind(change)", f"--bind=load:{first_load}+rebind(change)",
                    f"--bind=change:reload(sleep 0.1; {shlex.quote(me)} --list {mode} {{q}})"]
        else:
            fzf += [f"--bind=load:{first_load}"]
            if PENDING:  # only a wait row so far, which a query must not filter away: search is off until the poll turns it on
                fzf += ["--disabled"]
        # while anything is pending, look every half second whether it is done (`[ -e ]` is all it costs otherwise)
        fzf += [f"--bind=every(0.5):transform([ -e {shlex.quote(str(waiting_marker()))} ] && "
                f"{shlex.quote(sys.executable)} {shlex.quote(me)} --poll {mode} || true)"]
        stdin = "\n".join(rows) + "\n"
        proc = subprocess.run(fzf, input=stdin, stdout=subprocess.PIPE, text=True)
        waiting_marker().unlink(missing_ok=True)
        if proc.returncode not in (0, 1, 130):
            input(f"[optmux-find] fzf exited with status {proc.returncode}. Press Return/Enter to dismiss...")
            return proc.returncode
        query, key, entry = parse_output(proc.stdout)
        remember_query(history, query)
        save_state(mode, query, entry)
        carried = query
        if key == CYCLE_KEY:
            mode = MODES[(MODES.index(mode) + 1) % len(MODES)]
        elif key in MODE_KEYS.values():
            mode = next(m for m, k in MODE_KEYS.items() if k == key)
        else:
            set_tmux_option(MODE_OPTION, mode)
            f, n = target(entry) if entry else ("", "")
            if key in ("ctrl-c", "esc") or not f:
                return 0
            marker = marker_kind(mode, f)
            if marker == "install":  # a row that offers to install a language server
                install_here(f.split(":", 1)[1])
                carried = query  # and back to the picker, where the server should show up within seconds
                continue
            if marker:  # waiting, or a note: nothing to open, stay
                carried = query
                continue
            record_opened(mode, entry)
            open_in_split(pane, f, n)
            return 0
        set_tmux_option(MODE_OPTION, mode)


if __name__ == "__main__":
    if sys.argv[1:2] == ["--build-tags"]:  # the background refresh, see tags_file (which took the lock)
        os.chdir(sys.argv[2])
        build_locked(sys.argv[2], tags_cache(sys.argv[2]))
        sys.exit(0)
    if sys.argv[1:2] == ["--tags-path"]:  # for an editor: `set tags+=$(optmux-find --tags-path)`; builds them if need be
        found = tags_for_editors()
        if found:
            print(found)
        sys.exit(0 if found else 1)
    if sys.argv[1:2] == ["--preview"]:  # fzf's preview command: MODE FILE LINE DISPLAY WHAT, see preview()
        args = (sys.argv[2:] + ["", "", "", "", ""])[:5]
        sys.exit(preview(*args, int(os.environ.get("FZF_PREVIEW_LINES") or 30)))
    if sys.argv[1:2] == ["--lsp-fetch"]:  # the background half of lsp_rows: KIND QUERY
        lsp_fetch(sys.argv[2], sys.argv[3])
        sys.exit(0)
    if sys.argv[1:2] == ["--poll"]:  # fzf's every(): MODE; prints the action to take, if any
        enter_project_root()
        print(poll(os.path.abspath(sys.argv[0]), sys.argv[2]))
        sys.exit(0)
    if sys.argv[1:2] == ["--list"]:
        CHECK_TAGS = False  # a reload per keystroke: read the tags, leave building and checking to the picker
        mode, query = sys.argv[2], (sys.argv[3] if len(sys.argv) > 3 else "")
        enter_project_root()
        sys.stdout.write("".join(f"{line}\n" for line in candidates(mode, query)))
        sys.exit(0)
    args, options = parse_args(sys.argv[1:])
    pane = args[0] if args else tmux("display-message", "-p", "#{pane_id}", capture_output=True, text=True).stdout.strip()
    sys.exit(pick(pane, options.get("mode"), options.get("query")))
