flake-update-20260505
 1#!/usr/bin/env bash
 2# List recent files from Emacs recentf for raffi script filter.
 3# Outputs Alfred Script Filter JSON format.
 4# Usage: emacs-recentf-raffi [query]
 5# shellcheck disable=SC2016
 6set -euo pipefail
 7
 8query="${1:-}"
 9
10# Get recentf list from Emacs
11files=$(emacsclient --eval '(mapconcat (function identity) recentf-list "\n")' 2>/dev/null | tr -d '"' || true)
12
13if [[ -z "$files" ]]; then
14    jq -n '{"items": [{"title": "No recent files", "subtitle": "Emacs recentf is empty or Emacs is not running", "arg": ""}]}'
15    exit 0
16fi
17
18items="[]"
19while IFS= read -r f; do
20    [[ -z "$f" ]] && continue
21    # Skip tramp/remote paths
22    [[ "$f" == /ssh:* || "$f" == /sudo:* || "$f" == /scp:* ]] && continue
23    # Filter by query
24    if [[ -n "$query" ]] && ! echo "$f" | grep -qi "$query"; then
25        continue
26    fi
27    name=$(basename "$f")
28    dir=$(dirname "$f")
29    # Shorten home directory
30    dir="${dir/#"$HOME"/~}"
31    items=$(jq --arg title "$name" --arg sub "$dir" --arg arg "$f" \
32        '. + [{"title": $title, "subtitle": $sub, "arg": $arg}]' <<< "$items")
33done <<< "$files"
34
35jq -n --argjson items "$items" '{"items": $items}'