ztheme 1.7.1

Fast asynchronous Zsh prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# ztheme shell integration
#
# Synchronous prompt segments (directory, command status, prompt character,
# user custom segments) are computed in Zsh during `precmd` through the
# generic `_ztheme_compute_sync_segments` dispatcher and held. Git and runtime
# values are styled in Rust and arrive as finished fragments from the per-shell
# `ztheme __client-daemon` process. The complete prompt is assembled and drawn
# in one atomic redraw once those fragments finish or the shared deadline
# expires.

_ztheme_initialize() {
autoload -Uz colors
autoload -Uz add-zsh-hook
autoload -Uz add-zle-hook-widget
autoload -Uz is-at-least

colors
setopt PROMPT_SUBST

# The client spawn relies on zsh/system's sysopen with close-on-exec so the
# prompt descriptors cannot leak into external commands or keep the client
# alive after the shell dies.
if (( $+functions[is-at-least] )) && ! is-at-least 5.9 "$ZSH_VERSION"; then
    print -u2 -- "ztheme: requires Zsh 5.9 or newer (running $ZSH_VERSION)"
    return 1
fi
if ! zmodload zsh/system 2>/dev/null; then
    print -u2 -- "ztheme: requires the zsh/system module"
    return 1
fi

if (( $+functions[_ztheme_stop_client] )); then
    { _ztheme_stop_client } 2>/dev/null
fi

# ---------------------------------------------------------------------------
# Compiled theme
# ---------------------------------------------------------------------------

@ZTHEME_COMPILED_THEME@

# ---------------------------------------------------------------------------
# Synchronous segment implementations
#
# Bundled segments are embedded assets shipped inside the binary. Custom
# segments were validated and allowlisted by the ztheme binary and are sourced
# here, once, during initialization; nothing in this section runs per prompt.
#
# Custom definitions are emitted first and bundled definitions afterward so
# bundled segment functions win under ordinary redefinition.
# ---------------------------------------------------------------------------

@ZTHEME_CUSTOM_SEGMENTS@
@ZTHEME_BUNDLED_SEGMENTS@

# ---------------------------------------------------------------------------
# State
# ---------------------------------------------------------------------------

typeset -g __ZTHEME_BIN=@ZTHEME_BIN@
typeset -ga __ZTHEME_INSTANCE_ARGS=(@ZTHEME_INSTANCE_ARGS@)
typeset -g __ZTHEME_AUTOSUGGESTIONS=@ZTHEME_AUTOSUGGESTIONS@
typeset -g __ZTHEME_SYNTAX_HIGHLIGHTING=@ZTHEME_SYNTAX_HIGHLIGHTING@
typeset -gi __ZTHEME_LOCK_GIT=@ZTHEME_LOCK_GIT@
typeset -gi __ZTHEME_LOCK_RUNTIME=@ZTHEME_LOCK_RUNTIME@
typeset -g ZTHEME_PROMPT=""
typeset -g ZTHEME_RPROMPT=""
typeset -g ZTHEME_CONTEXT_KEY=""
typeset -g ZTHEME_LAST_ERROR=""
typeset -gi ZTHEME_GENERATION=0
typeset -gi ZTHEME_LOCKED_PENDING=0
typeset -g ZTHEME_CLIENT_PID=""
typeset -gi ZTHEME_CLIENT_READY=0
typeset -gi ZTHEME_REQ_FD=-1
typeset -gi ZTHEME_RESP_FD=-1
typeset -gi ZTHEME_FIFO_SEQUENCE=0
typeset -gi ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN=${ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN:-0}
typeset -gi ZTHEME_SYNTAX_WARNING_SHOWN=${ZTHEME_SYNTAX_WARNING_SHOWN:-0}
typeset -g ZSH_AUTOSUGGEST_MANUAL_REBIND=1
typeset -g __ZTHEME_TERM_ENTER="${__ZTHEME_TERM_ENTER:-}"
typeset -g __ZTHEME_TERM_LEAVE="${__ZTHEME_TERM_LEAVE:-}"
typeset -g __ZTHEME_FOCUS_ENTER=""
typeset -g __ZTHEME_FOCUS_LEAVE=""

if [[ "${ZTHEME_FOCUS_REPORTING:-1}" != 0 &&
      "${TERM:-dumb}" != dumb ]]
then
    __ZTHEME_FOCUS_ENTER=$'\e[?1004h\e[?25h'
    __ZTHEME_FOCUS_LEAVE=$'\e[?1004l\e[?25h'
fi

@ZTHEME_SHELL_DEFAULTS@

_ztheme_start_client() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    (( __ZTHEME_HAS_ASYNC )) || return 1
    [[ -x "$__ZTHEME_BIN" ]] || return 1

    local base="${TMPDIR:-/tmp}"
    local -i sequence=$(( ++ZTHEME_FIFO_SEQUENCE ))
    local prefix="$base/ztheme-$UID-$$-$sequence"
    local request="$prefix.req"
    local response="$prefix.resp"
    local -i request_client_fd=-1
    local -i response_bootstrap_fd=-1
    local -i response_client_fd=-1

    command rm -f "$request" "$response" 2>/dev/null
    if ! command mkfifo -m 600 "$request" "$response" 2>/dev/null; then
        return 1
    fi

    # Every descriptor is opened with close-on-exec so it cannot leak into
    # the client (which must not inherit a request writer, or EOF on its
    # stdin would never arrive) or into ordinary external commands. sysopen
    # reports open failures through its exit status, which commandless
    # `exec` redirections do not. Holding an O_RDWR endpoint on each FIFO
    # until both ends are open keeps every other open below from blocking.
    if ! sysopen -r -w -o cloexec -u ZTHEME_REQ_FD "$request" 2>/dev/null; then
        command rm -f "$request" "$response" 2>/dev/null
        return 1
    fi
    if ! sysopen -r -o cloexec -u request_client_fd "$request" 2>/dev/null; then
        _ztheme_close_client_fds "$request_client_fd" "$response_bootstrap_fd" "$response_client_fd"
        command rm -f "$request" "$response" 2>/dev/null
        return 1
    fi
    if ! sysopen -r -w -o cloexec -u response_bootstrap_fd "$response" 2>/dev/null; then
        _ztheme_close_client_fds "$request_client_fd" "$response_bootstrap_fd" "$response_client_fd"
        command rm -f "$request" "$response" 2>/dev/null
        return 1
    fi
    if ! sysopen -w -o cloexec -u response_client_fd "$response" 2>/dev/null; then
        _ztheme_close_client_fds "$request_client_fd" "$response_bootstrap_fd" "$response_client_fd"
        command rm -f "$request" "$response" 2>/dev/null
        return 1
    fi
    if ! sysopen -r -o cloexec -u ZTHEME_RESP_FD "$response" 2>/dev/null; then
        _ztheme_close_client_fds "$request_client_fd" "$response_bootstrap_fd" "$response_client_fd"
        command rm -f "$request" "$response" 2>/dev/null
        return 1
    fi

    # All endpoints are open. Drop the temporary bootstrap and unlink the
    # paths so a shell killed with SIGKILL leaves no entries in $TMPDIR, and
    # the predictable names cannot be interfered with.
    exec {response_bootstrap_fd}>&-
    response_bootstrap_fd=-1
    command rm -f "$request" "$response" 2>/dev/null

    # Spawn the client from the already-open endpoints: it receives the
    # request read end as stdin and the response write end as stdout. Every
    # other descriptor is close-on-exec and disappears during exec, so the
    # client never holds its own request writer. The shell passes its own PID
    # so the client can detect reparenting as a fallback to stdin EOF.
    local ztheme_bg_nice="$options[bgnice]"
    unsetopt BG_NICE
    command "$__ZTHEME_BIN" __client-daemon \
        --shell-pid "$$" \
        --theme "$__ZTHEME_ASYNC_THEME" \
        "${__ZTHEME_INSTANCE_ARGS[@]}" \
        <&"$request_client_fd" >&"$response_client_fd" 2>/dev/null &!
    [[ "$ztheme_bg_nice" == on ]] && setopt BG_NICE
    unset ztheme_bg_nice

    ZTHEME_CLIENT_PID=$!
    ZTHEME_CLIENT_READY=0
    if (( ZTHEME_CLIENT_PID <= 0 )) ||
        ! kill -0 "$ZTHEME_CLIENT_PID" 2>/dev/null
    then
        _ztheme_close_client_fds "$request_client_fd" "$response_bootstrap_fd" "$response_client_fd"
        return 1
    fi

    # The client holds the endpoints as its stdin/stdout; close the shell's
    # copies so the descriptors are not inherited twice.
    exec {request_client_fd}<&-
    request_client_fd=-1
    exec {response_client_fd}>&-
    response_client_fd=-1

    if builtin zle -F "$ZTHEME_RESP_FD" _ztheme_async_callback 2>/dev/null; then
        ZTHEME_CLIENT_READY=1
    fi
    return 0
}

# Closes the descriptors opened by _ztheme_start_client on its failure paths.
# The three client-side endpoint copies are passed as arguments because they
# are local to the caller; ZTHEME_REQ_FD and ZTHEME_RESP_FD are global. Each
# fd is only closed when its variable is non-negative, which sysopen sets
# exclusively on success.
_ztheme_close_client_fds() {
    emulate -L zsh

    local -i request_client_fd="${1:--1}"
    local -i response_bootstrap_fd="${2:--1}"
    local -i response_client_fd="${3:--1}"
    (( request_client_fd >= 0 )) && { exec {request_client_fd}<&-; } 2>/dev/null
    (( response_bootstrap_fd >= 0 )) && { exec {response_bootstrap_fd}>&-; } 2>/dev/null
    (( response_client_fd >= 0 )) && { exec {response_client_fd}>&-; } 2>/dev/null
    if (( ZTHEME_REQ_FD >= 0 )); then
        { exec {ZTHEME_REQ_FD}<&-; } 2>/dev/null
        ZTHEME_REQ_FD=-1
    fi
    if (( ZTHEME_RESP_FD >= 0 )); then
        { exec {ZTHEME_RESP_FD}<&-; } 2>/dev/null
        ZTHEME_RESP_FD=-1
    fi
    return 0
}

_ztheme_stop_client() {
    emulate -L zsh

    local -i client_pid="${ZTHEME_CLIENT_PID:-0}"
    local -i request_fd="${ZTHEME_REQ_FD:--1}"
    local -i response_fd="${ZTHEME_RESP_FD:--1}"
    ZTHEME_CLIENT_PID=""
    ZTHEME_CLIENT_READY=0
    ZTHEME_LOCKED_PENDING=0
    ZTHEME_REQ_FD=-1
    ZTHEME_RESP_FD=-1

    # Unregister the callback, then close the request writer first so the
    # client sees stdin EOF and exits on its own; the explicit kill is a
    # final guarantee for a client whose EOF was masked by a leaked writer.
    if (( response_fd >= 0 )); then
        builtin zle -F "$response_fd" 2>/dev/null
    fi
    if (( request_fd >= 0 )); then
        exec {request_fd}<&-
    fi
    if (( response_fd >= 0 )); then
        exec {response_fd}<&-
    fi
    if (( client_pid > 0 )); then
        kill "$client_pid" 2>/dev/null
    fi
    return 0
}

_ztheme_async_callback() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    local -i fd=$1
    local protocol generation kind segment fragment
    local -i redraw=0

    if ! IFS=$'\t' read -r -u "$fd" \
        protocol generation kind segment fragment; then
        _ztheme_stop_client
        _ztheme_render_layout
        builtin zle reset-prompt
        return
    fi

    # Records from superseded generations are ignored, never rendered; the
    # client daemon itself is long-lived, so nothing is torn down here.
    if [[ "$protocol" != ZTHEME1 ||
          "$generation" != "$ZTHEME_GENERATION" ]]; then
        return
    fi

    case "$kind" in
        segment)
            ZTHEME_LAST_ERROR=""
            # Only redraw when the value actually changed: an unchanged or empty
            # fragment (runtimes emit one record per configured runtime) needs
            # no second `reset-prompt`.
            if _ztheme_assign_async_segment "$segment" "$fragment"; then
                # An unlocked segment arriving after the prompt rendered appears
                # via a redraw; a segment for a still-locked group is stored and
                # shown when that group's `complete` releases the barrier.
                (( ! ZTHEME_LOCKED_PENDING )) && redraw=1
            fi
            ;;
        error)
            # Clear only the group that errored (`git` or `runtime`); a
            # successful unlocked group must not be erased. An unrecognized
            # source (such as an internal `snapshot` error) clears nothing and
            # needs no redraw. Otherwise, when the prompt already rendered,
            # redraw so the visible prompt reflects the cleared group.
            if _ztheme_clear_async_group "$segment"; then
                (( ! ZTHEME_LOCKED_PENDING )) && redraw=1
            fi
            if [[ -n "$fragment" && "$fragment" != "$ZTHEME_LAST_ERROR" ]]; then
                ZTHEME_LAST_ERROR="$fragment"
                builtin zle -M "ztheme: $fragment"
            fi
            ;;
        complete)
            # A group finished. Release its rendering lock (when present and
            # locked); when no locked group remains pending, render the prompt.
            case "$segment" in
                git)
                    if (( __ZTHEME_HAS_GIT && __ZTHEME_LOCK_GIT && ZTHEME_LOCKED_PENDING )); then
                        (( ZTHEME_LOCKED_PENDING-- ))
                        (( ZTHEME_LOCKED_PENDING == 0 )) && redraw=1
                    fi
                    ;;
                runtime)
                    if (( __ZTHEME_HAS_RUNTIME && __ZTHEME_LOCK_RUNTIME && ZTHEME_LOCKED_PENDING )); then
                        (( ZTHEME_LOCKED_PENDING-- ))
                        (( ZTHEME_LOCKED_PENDING == 0 )) && redraw=1
                    fi
                    ;;
            esac
            ;;
        done)
            # Shared deadline passed or all work finished. This is the safety
            # net for a locked group that never delivered a `complete` before
            # the deadline: when nothing is pending the prompt was already
            # rendered, so skip the redundant second redraw.
            if (( ZTHEME_LOCKED_PENDING )); then
                ZTHEME_LOCKED_PENDING=0
                redraw=1
            fi
            ;;
        *)
            return
            ;;
    esac

    if (( redraw )); then
        _ztheme_render_layout
        builtin zle reset-prompt
    fi
}

_ztheme_start_worker() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    (( ++ZTHEME_GENERATION ))
    _ztheme_clear_async_segments
    (( __ZTHEME_HAS_ASYNC )) || return 1
    [[ -x "$__ZTHEME_BIN" ]] || return 1

    if [[ -z "$ZTHEME_CLIENT_PID" ]] ||
        ! kill -0 "$ZTHEME_CLIENT_PID" 2>/dev/null
    then
        _ztheme_stop_client
        _ztheme_start_client || return 1
    fi
    if (( ! ZTHEME_CLIENT_READY )); then
        if builtin zle -F "$ZTHEME_RESP_FD" _ztheme_async_callback 2>/dev/null; then
            ZTHEME_CLIENT_READY=1
        else
            # Without a registered callback nobody will ever consume the
            # response records, so the prompt would stay empty; stop the
            # client and let the caller render without async segments.
            _ztheme_stop_client
            return 1
        fi
    fi

    local -i generation=$ZTHEME_GENERATION
    # zsh strings can hold NUL bytes, and backslash continuations would join
    # separate arguments, so the NUL-delimited request is assembled with
    # incremental appends, one assignment per line.
    local request_line="ZTREQ"$'\0'"@ZTHEME_REQUEST_VERSION@"$'\0'"$generation"$'\0'"$PWD"$'\0'
    @ZTHEME_REQUEST_FIELDS@
    if ! print -rn -- "$request_line" >&"$ZTHEME_REQ_FD" 2>/dev/null; then
        _ztheme_stop_client
        return 1
    fi

    # Count how many present asynchronous groups are locked (wait for them
    # before rendering). Any unlocked group does not block the prompt; it is
    # drawn in via a redraw when its records arrive.
    ZTHEME_LOCKED_PENDING=0
    (( __ZTHEME_HAS_GIT && __ZTHEME_LOCK_GIT )) && (( ZTHEME_LOCKED_PENDING++ ))
    (( __ZTHEME_HAS_RUNTIME && __ZTHEME_LOCK_RUNTIME )) && (( ZTHEME_LOCKED_PENDING++ ))
    ZTHEME_PROMPT=""
    ZTHEME_RPROMPT=""
    return 0
}

# ---------------------------------------------------------------------------
# Immediate prompt state
# ---------------------------------------------------------------------------

# Wraps a computed segment value in its theme-provided styling. The OPEN/CLOSE
# style maps are generated by the theme compiler keyed by `id:variant`; the
# value is the segment's prompt fragment and is not escaped.
_ztheme_segment_render() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    local id=$1
    local value=$2
    local variant=${3:-default}
    local key="${id}:${variant}"

    REPLY="${__ZTHEME_SEGMENT_OPEN[$key]:-}"
    REPLY+="$value"
    REPLY+="${__ZTHEME_SEGMENT_CLOSE[$key]:-}"
}

# Computes every synchronous segment present in the active layout exactly
# once, in deterministic layout order. Each segment function receives the
# previous command status as $1 and reports its value through $REPLY, which is
# then stored in its ZTHEME_SEGMENT_<ID> state variable. A missing function or
# a failed segment leaves an empty fragment for that prompt only.
_ztheme_compute_sync_segments() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    local -i last_status=$1
    local id function_name variable_name

    for id in "${__ZTHEME_SYNC_SEGMENTS[@]}"; do
        function_name="ztheme_segment_${id}"
        variable_name="ZTHEME_SEGMENT_${(U)id}"
        REPLY=""

        if (( ! $+functions[$function_name] )); then
            # Initialization should already have validated this.
            typeset -g "$variable_name="
            continue
        fi

        if ! "$function_name" "$last_status"; then
            REPLY=""
        fi

        typeset -g "$variable_name=$REPLY"
    done
}

_ztheme_precmd() {
    local -i last_status=$?

    emulate -L zsh
    setopt localoptions no_shwordsplit

    _ztheme_start_worker

    _ztheme_compute_sync_segments "$last_status"

    local context_key
    context_key="$PWD"
    @ZTHEME_CONTEXT_FIELDS@
    context_key+="|${NVM_BIN:-}|$PATH"

    if [[ "$context_key" != "$ZTHEME_CONTEXT_KEY" ]]; then
        ZTHEME_CONTEXT_KEY="$context_key"
        _ztheme_clear_async_segments
        ZTHEME_LAST_ERROR=""
    fi

    # Rendering is always attempted here: `_ztheme_render_layout` returns early
    # while a locked group is still pending, and otherwise draws the prompt
    # immediately (which is what lets an unlocked-only layout appear at once
    # instead of waiting for the async records).
    _ztheme_render_layout
}

_ztheme_load_shell_plugins() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    precmd_functions=(
        ${precmd_functions:#_ztheme_load_shell_plugins}
    )

    if (( ! $+functions[_zsh_autosuggest_start] )); then
        if [[ -r "$__ZTHEME_AUTOSUGGESTIONS" ]]; then
            builtin source "$__ZTHEME_AUTOSUGGESTIONS"
        fi
    fi

    if (( $+functions[_zsh_autosuggest_start] )); then
        add-zle-hook-widget -D line-init \
            _ztheme_initialize_autosuggestions 2>/dev/null
        add-zle-hook-widget line-init _ztheme_initialize_autosuggestions
    elif (( ! ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN )); then
        builtin print -u2 -r -- \
            "ztheme: autosuggestions are unavailable; run \`ztheme setup\` to install them."
        ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN=1
    fi

    if [[ -n "${ZSH_HIGHLIGHT_VERSION:-}" ]] ||
        (( $+functions[_zsh_highlight] ))
    then
        return 0
    fi

    if [[ -r "$__ZTHEME_SYNTAX_HIGHLIGHTING" ]]; then
        if builtin source "$__ZTHEME_SYNTAX_HIGHLIGHTING" &&
            { [[ -n "${ZSH_HIGHLIGHT_VERSION:-}" ]] ||
                (( $+functions[_zsh_highlight] )) }
        then
            return 0
        fi
    fi

    if (( ! ZTHEME_SYNTAX_WARNING_SHOWN )); then
        builtin print -u2 -r -- \
            "ztheme: syntax highlighting is unavailable; run \`ztheme setup\` to install it."
        ZTHEME_SYNTAX_WARNING_SHOWN=1
    fi
    return 0
}

_ztheme_initialize_autosuggestions() {
    emulate -L zsh

    add-zle-hook-widget -D line-init \
        _ztheme_initialize_autosuggestions 2>/dev/null
    _zsh_autosuggest_start

    if (( $+widgets[autosuggest-accept] )); then
        return 0
    fi

    if (( ! ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN )); then
        builtin zle -M \
            "ztheme: autosuggestions failed to initialize; run \`ztheme setup\` to reinstall them."
        ZTHEME_AUTOSUGGESTIONS_WARNING_SHOWN=1
    fi
    return 0
}

_ztheme_preexec() {
    emulate -L zsh

    (( ++ZTHEME_GENERATION ))
}

_ztheme_chpwd() {
    emulate -L zsh

    (( ++ZTHEME_GENERATION ))
    ZTHEME_CONTEXT_KEY=""
    _ztheme_clear_async_segments
}

_ztheme_zshexit() {
    emulate -L zsh

    _ztheme_stop_client
    if [[ -o interactive &&
          -n "$__ZTHEME_TERM_LEAVE$__ZTHEME_FOCUS_LEAVE" ]]
    then
        builtin printf '%s%s' \
            "$__ZTHEME_TERM_LEAVE" "$__ZTHEME_FOCUS_LEAVE"
    fi
}

# ---------------------------------------------------------------------------
# Public command bridge
# ---------------------------------------------------------------------------

ztheme() {
    emulate -L zsh
    setopt localoptions no_shwordsplit

    if [[ "$1" == theme && "$2" == apply ]]; then
        if (( $# != 3 )); then
            command "$__ZTHEME_BIN" "$@"
            return
        fi

        local integration
        integration="$(
            command "$__ZTHEME_BIN" __theme-apply-zsh \
                --theme "$3" "${__ZTHEME_INSTANCE_ARGS[@]}"
        )" || return
        builtin eval "$integration" || return
        builtin print -r -- \
            "Applied the selected theme to this shell and future shells."
        return
    fi

    if [[ "$1" == theme && "$2" == reload ]]; then
        if (( $# != 2 )); then
            command "$__ZTHEME_BIN" "$@"
            return
        fi

        local integration
        integration="$(
            command "$__ZTHEME_BIN" __theme-reload-zsh \
                --theme "$__ZTHEME_THEME_SELECTOR" \
                "${__ZTHEME_INSTANCE_ARGS[@]}"
        )" || return
        builtin eval "$integration" || return
        builtin print -r -- "Reloaded the current theme."
        return
    fi

    command "$__ZTHEME_BIN" "$@"
}

# ---------------------------------------------------------------------------
# Terminal state
# ---------------------------------------------------------------------------

_ztheme_focus_in() {
    emulate -L zsh

    builtin printf '\e[?25h'
    REPLY=""
    ztheme_segment_directory
    typeset -g ZTHEME_SEGMENT_DIRECTORY="$REPLY"
    _ztheme_render_layout
    builtin zle reset-prompt
}

_ztheme_focus_out() {
    emulate -L zsh
    builtin printf '\e[?25l'
}

_ztheme_zle_line_init() {
    emulate -L zsh
    builtin printf '%s%s' \
        "$__ZTHEME_TERM_ENTER" "$__ZTHEME_FOCUS_ENTER"
}

_ztheme_zle_line_finish() {
    emulate -L zsh
    builtin printf '%s%s' \
        "$__ZTHEME_TERM_LEAVE" "$__ZTHEME_FOCUS_LEAVE"
}

# ---------------------------------------------------------------------------
# Hooks
# ---------------------------------------------------------------------------

precmd_functions=(${precmd_functions:#_ztheme_precmd})
precmd_functions=(${precmd_functions:#_ztheme_load_shell_plugins})
precmd_functions=(_ztheme_precmd $precmd_functions)
precmd_functions+=(_ztheme_load_shell_plugins)

for hook_spec in \
    preexec:_ztheme_preexec \
    chpwd:_ztheme_chpwd \
    zshexit:_ztheme_zshexit
do
    hook="${hook_spec%%:*}"
    function="${hook_spec#*:}"
    add-zsh-hook -D "$hook" "$function" 2>/dev/null
    add-zsh-hook "$hook" "$function"
done
unset hook_spec hook function

if [[ -n "$__ZTHEME_FOCUS_ENTER" &&
      ! ${ZTHEME_FOCUS_BINDINGS_INITIALIZED:-0} -eq 1 ]]
then
    typeset -gi ZTHEME_FOCUS_BINDINGS_INITIALIZED=1
    builtin zle -N _ztheme_focus_in
    builtin zle -N _ztheme_focus_out

    local ztheme_focus_keymap
    for ztheme_focus_keymap in emacs viins vicmd; do
        builtin bindkey -M "$ztheme_focus_keymap" \
            $'\e[I' _ztheme_focus_in \
            $'\e[O' _ztheme_focus_out
    done
    unset ztheme_focus_keymap
fi

add-zle-hook-widget -D line-init _ztheme_zle_line_init 2>/dev/null
add-zle-hook-widget -D line-finish _ztheme_zle_line_finish 2>/dev/null
if [[ -n "$__ZTHEME_TERM_ENTER$__ZTHEME_FOCUS_ENTER" ]]; then
    add-zle-hook-widget line-init _ztheme_zle_line_init
fi
if [[ -n "$__ZTHEME_TERM_LEAVE$__ZTHEME_FOCUS_LEAVE" ]]; then
    add-zle-hook-widget line-finish _ztheme_zle_line_finish
fi

PROMPT='${ZTHEME_PROMPT}'
RPROMPT='${ZTHEME_RPROMPT}'

if (( __ZTHEME_HAS_ASYNC )); then
    local ztheme_bg_nice="$options[bgnice]"
    unsetopt BG_NICE
    command "$__ZTHEME_BIN" __daemon \
        "${__ZTHEME_INSTANCE_ARGS[@]}" >/dev/null 2>&1 &!
    _ztheme_start_client
    [[ "$ztheme_bg_nice" == on ]] && setopt BG_NICE
    unset ztheme_bg_nice
fi

typeset -g ZTHEME_SHELL_INITIALIZED=1
}

if ! _ztheme_initialize; then
    unfunction _ztheme_initialize
    return 1
fi
unfunction _ztheme_initialize