scripts/update-word-syntax.sh
43 lines · 1754 bytes · executable
1#!/bin/sh
2# Writes what `forward-word` needs from Emacs to Sources/OrgCore/Semantic/WordSyntax.swift:
3# the characters without word syntax in an org-mode buffer, and `char-script-table`.
4# Usage: scripts/update-word-syntax.sh [emacs]
5set -eu
6cd "$(dirname "$0")/.."
7emacs=${1:-emacs}
8out=Sources/OrgCore/Semantic/WordSyntax.swift
9{
10 echo "// Generated by scripts/update-word-syntax.sh from $("$emacs" --version | head -1) and Org $("$emacs" -Q --batch --eval '(progn (require (quote org)) (princ (org-version)))')."
11 echo
12 echo "extension TextStats {"
13 echo " /// Code points without word syntax in org-mode, sorted."
14 echo " static let nonWord: [ClosedRange<UInt32>] = ["
15 "$emacs" -Q --batch --eval '
16(progn
17 (require (quote org))
18 (with-temp-buffer
19 (org-mode)
20 (let ((c 0) start)
21 (while (<= c #x110000)
22 (let ((word (and (< c #x110000) (eq (char-syntax c) ?w))))
23 (cond ((and (not word) (not start) (< c #x110000)) (setq start c))
24 ((and (or word (= c #x110000)) start)
25 (princ (format " 0x%X...0x%X,\n" start (1- c)))
26 (setq start nil))))
27 (setq c (1+ c))))))'
28 echo " ]"
29 echo
30 echo " /// \`char-script-table\`: where each script's range starts, and its script."
31 echo " static let scripts: [(start: UInt32, script: UInt16)] = ["
32 "$emacs" -Q --batch --eval '
33(let ((c 0) (ids (make-hash-table)) prev)
34 (while (< c #x110000)
35 (let ((s (aref char-script-table c)))
36 (unless (and (> c 0) (eq s prev))
37 (unless (gethash s ids) (puthash s (hash-table-count ids) ids))
38 (princ (format " (0x%X, %d),\n" c (gethash s ids)))
39 (setq prev s)))
40 (setq c (1+ c))))'
41 echo " ]"
42 echo "}"
43} > "$out"