aboutsummaryrefslogtreecommitdiff
path: root/citekey-menu
blob: 3b94bbe9f1347596ed80c0f2a61881d258d62760 (plain) (blame)
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
#!/usr/bin/env bash
set -euo pipefail

BIB="${BIB:-/home/lukasz/thesis_main/05_thesis/bibliography.bib}"
DMENU="${DMENU:-dmenu -i -l 20 -p Citekey}"
CLIP="${CLIP:-xclip -selection clipboard}"

if [[ ! -r "$BIB" ]]; then
  printf "ERROR: cannot read %s\n" "$BIB" >&2
  exit 1
fi

# Build menu lines: "type<TAB>citekey"
choices="$(
  rg -n '^\@(\w+)\{([^,]+),' "$BIB" \
  | sed -E 's/^[0-9]+:\@(\w+)\{([^,]+),/\1\t\2/' \
  | sort -u
)"

picked="$(printf "%s\n" "$choices" | eval "$DMENU" || true)"
[[ -z "$picked" ]] && exit 0

key="$(printf "%s" "$picked" | awk -F'\t' '{print $2}')"
[[ -z "$key" ]] && exit 0

printf "[@%s]" "$key" | eval "$CLIP"
printf "Copied: [@%s]\n" "$key" >&2