krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

scripts/update-char-fold.sh

15f6b0709d88971fb62ed432c3e5b8032643670a
orgstar/scripts/update-char-fold.sh history · blame · raw

40 lines · 1528 bytes · executable

 1#!/bin/sh
 2# Writes `char-fold-table` from Emacs to Sources/OrgCore/Keymap/CharFold.swift, for
 3# evil-snipe's `evil-snipe-char-fold`.
 4# Usage: scripts/update-char-fold.sh [emacs]
 5set -eu
 6cd "$(dirname "$0")/.."
 7emacs=${1:-emacs}
 8out=Sources/OrgCore/Keymap/CharFold.swift
 9{
10  echo "// Generated by scripts/update-char-fold.sh from $("$emacs" --version | head -1)."
11  echo
12  echo "extension CharFold {"
13  echo "    /// \`char-fold-table\`: a line per character with an entry, its code point in hex, a tab"
14  echo "    /// and the Emacs regexp it matches."
15  echo "    static let table = \"\"\""
16  "$emacs" -Q --batch --eval '
17(progn
18  (set-language-environment "UTF-8")
19  (require (quote char-fold))
20  (let (rows)
21    (map-char-table
22     (lambda (k v)
23       (when v
24         (dolist (c (if (consp k) (number-sequence (car k) (cdr k)) (list k)))
25           (push (cons c v) rows))))
26     char-fold-table)
27    (setq rows (sort rows (lambda (a b) (< (car a) (car b)))))
28    (dolist (row rows)
29      (princ (format "    %X\t" (car row)))
30      (mapc (lambda (c)
31              (princ (cond ((eq c ?\\) "\\\\")
32                           ((eq c ?\") "\\\"")
33                           ((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)))))
34                            (format "\\u{%X}" c))
35                           (t (char-to-string c)))))
36            (cdr row))
37      (princ "\n"))))' | iconv -f utf-8 -t utf-8
38  echo "    \"\"\""
39  echo "}"
40} > "$out"