#!/bin/sh # Writes `char-fold-table` from Emacs to Sources/OrgCore/Keymap/CharFold.swift, for # evil-snipe's `evil-snipe-char-fold`. # Usage: scripts/update-char-fold.sh [emacs] set -eu cd "$(dirname "$0")/.." emacs=${1:-emacs} out=Sources/OrgCore/Keymap/CharFold.swift { echo "// Generated by scripts/update-char-fold.sh from $("$emacs" --version | head -1)." echo echo "extension CharFold {" echo " /// \`char-fold-table\`: a line per character with an entry, its code point in hex, a tab" echo " /// and the Emacs regexp it matches." echo " static let table = \"\"\"" "$emacs" -Q --batch --eval ' (progn (set-language-environment "UTF-8") (require (quote char-fold)) (let (rows) (map-char-table (lambda (k v) (when v (dolist (c (if (consp k) (number-sequence (car k) (cdr k)) (list k))) (push (cons c v) rows)))) char-fold-table) (setq rows (sort rows (lambda (a b) (< (car a) (car b))))) (dolist (row rows) (princ (format " %X\t" (car row))) (mapc (lambda (c) (princ (cond ((eq c ?\\) "\\\\") ((eq c ?\") "\\\"") ((or (< c 32) (and (/= c 32) (memq (get-char-code-property c (quote general-category)) (quote (Mn Mc Me Cc Cf Co Cn Cs Zl Zp Zs))))) (format "\\u{%X}" c)) (t (char-to-string c))))) (cdr row)) (princ "\n"))))' | iconv -f utf-8 -t utf-8 echo " \"\"\"" echo "}" } > "$out"