main dotfiles / bin / kak-file-symbols
Eric Bower  ·  2026-09-08
 1#!/usr/bin/env bash
 2file="$1"
 3
 4if [[ ! -f "$file" ]]; then
 5    echo "echo -markup \"{Error}File not found\""
 6    exit 0
 7fi
 8
 9ext="${file##*.}"
10
11if [[ "$ext" == "zig" ]]; then
12    awk '
13        /^(pub )?const [a-zA-Z0-9_]+ = (struct|enum|union|opaque)/ {
14            match($0, /const ([a-zA-Z0-9_]+)/, m)
15            curr_struct = m[1]
16        }
17        /^};?/ {
18            curr_struct = ""
19        }
20        /(pub |inline |export |extern )*fn [a-zA-Z0-9_]+/ {
21            match($0, /fn ([a-zA-Z0-9_]+)/, m)
22            fn_name = m[1]
23            if (fn_name != "") {
24                recv = curr_struct
25                if (recv == "" && match($0, /\((self|it): \*?([a-zA-Z0-9_]+)/, r)) {
26                    recv = r[2]
27                }
28                label = (recv != "" ? recv " > " fn_name : fn_name) " : " NR
29                gsub("!", "!!", label)
30                action = "execute-keys %|" NR "gvc|"
31                out = out "%!" label "! %!evaluate-commands %# try %& " action " & # !"
32            }
33        }
34        END {
35            if (length(out) == 0) {
36                print "echo -markup \"{Error}No functions found in current file\""
37            } else {
38                print "menu " out
39            }
40        }
41    ' "$file"
42else
43    ctags -f - --sort=no --fields=+nK "$file" 2>/dev/null | awk -F'\t' '
44        {
45            name = $1
46            kind = $4
47            line = $5
48            sub(/^line:/, "", line)
49            scope = ""
50            for (i = 6; i <= NF; i++) {
51                if ($i ~ /^(class|struct|interface|type):/) {
52                    scope = $i
53                    sub(/^[a-z]+:/, "", scope)
54                    sub(/.*[.]/, "", scope)
55                }
56            }
57            if (kind ~ /^(func|function|method|member|methodSpec|constructor)$/) {
58                label = (scope != "" ? scope " > " name : name) " : " line
59                if (seen[line, label]++) next
60                gsub("!", "!!", label)
61                action = "execute-keys %|" line "gvc|"
62                out = out "%!" label "! %!evaluate-commands %# try %& " action " & # !"
63            }
64        }
65        END {
66            if (length(out) == 0) {
67                print "echo -markup \"{Error}No functions found in current file\""
68            } else {
69                print "menu " out
70            }
71        }
72    '
73fi