Sources/OrgApp/TableReason.swift
cmc Run Emacs Lisp natively when Emacs is missing; translate the new Lisp reasons
db6552840c 6 days ago116 lines · 9282 bytes
1import Foundation
2import OrgCore
3
4/// Why a table's formulas go to Emacs, `Calc.Unsupported`'s reason, in the app's language.
5public enum TableReason {
6 /// The reason translated, or as it is when it's one this doesn't know.
7 public static func localized(_ reason: String) -> String { translated(reason) ?? reason }
8
9 /// Nil for a reason not known here.
10 static func translated(_ reason: String) -> String? {
11 if reason.hasPrefix("Lisp ") {
12 let inner = String(reason.dropFirst("Lisp ".count))
13 guard let what = lisp(inner) else { return nil }
14 return String(localized: "Lisp \(what)", comment: "Why a table needs Emacs: something its Lisp formula does")
15 }
16 if let fixed = fixed(reason) { return fixed }
17 func value(_ prefix: String) -> String? { reason.hasPrefix(prefix) ? String(reason.dropFirst(prefix.count)) : nil }
18 if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
19 if let name = value("variable ") { return String(localized: "variable \(name)", comment: "Why a table needs Emacs: a Calc variable") }
20 if let text = value("non-numeric field ") { return String(localized: "non-numeric field \(text)", comment: "Why a table needs Emacs") }
21 if let number = value("number size ") { return String(localized: "number size \(number)", comment: "Why a table needs Emacs: a number too large") }
22 if let flag = value("format flag ") { return String(localized: "format flag \(flag)", comment: "Why a table needs Emacs: a format specifier flag") }
23 return nil
24 }
25
26 /// `Elisp.Unsupported`'s reasons.
27 static func lisp(_ what: String) -> String? {
28 let comment: StaticString = "Why a table needs Emacs: something a Lisp formula does"
29 switch what {
30 case "integer size": return fixed(what)
31 case "case folding past ASCII": return String(localized: "case folding past ASCII", comment: comment)
32 case "cl-loop destructuring": return String(localized: "cl-loop destructuring", comment: comment)
33 case "cl-loop with collect and sum": return String(localized: "cl-loop with collect and sum", comment: comment)
34 case "cl-loop without for": return String(localized: "cl-loop without for", comment: comment)
35 case "float times": return String(localized: "float times", comment: comment)
36 case "float-time of picoseconds": return String(localized: "float-time of picoseconds", comment: comment)
37 case "org-time-string-to-absolute with a day": return String(localized: "org-time-string-to-absolute with a day", comment: comment)
38 case "random with a seed": return String(localized: "random with a seed", comment: comment)
39 case "regexp classes on text past ASCII": return String(localized: "regexp classes on text past ASCII", comment: comment)
40 case "replace-match case past ASCII": return String(localized: "replace-match case past ASCII", comment: comment)
41 case "replace-match in a buffer": return String(localized: "replace-match in a buffer", comment: comment)
42 case "replace-regexp-in-string from the end": return String(localized: "replace-regexp-in-string from the end", comment: comment)
43 case "seq-uniq with a test": return String(localized: "seq-uniq with a test", comment: comment)
44 case "sort on a string": return String(localized: "sort on a string", comment: comment)
45 default: break
46 }
47 func value(_ prefix: String) -> String? { what.hasPrefix(prefix) ? String(what.dropFirst(prefix.count)) : nil }
48 func split(_ text: String, _ separator: String) -> (String, String)? {
49 guard let range = text.range(of: separator) else { return nil }
50 return (String(text[..<range.lowerBound]), String(text[range.upperBound...]))
51 }
52 if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
53 if let time = value("time value ") { return String(localized: "time value \(time)", comment: "Why a table needs Emacs: a Lisp time value") }
54 if let number = value("date number ") { return String(localized: "date number \(number)", comment: "Why a table needs Emacs: a Lisp date number") }
55 if let text = value("date-to-time of ") { return String(localized: "date-to-time of \(text)", comment: comment) }
56 if let parts = value("date ") { return String(localized: "date \(parts)", comment: comment) }
57 if let text = value("time string ") { return String(localized: "time string \(text)", comment: comment) }
58 if let zone = value("format-time-string zone ") { return String(localized: "format-time-string zone \(zone)", comment: comment) }
59 if let code = value("format-time-string ") { return String(localized: "format-time-string \(code)", comment: comment) }
60 if let clause = value("cl-loop ") { return String(localized: "cl-loop \(clause)", comment: comment) }
61 if let pattern = value("pcase pattern ") { return String(localized: "pcase pattern \(pattern)", comment: comment) }
62 if let name = value("let-alist ") { return String(localized: "let-alist \(name)", comment: comment) }
63 if let text = value("string-width of ") { return String(localized: "string-width of \(text)", comment: comment) }
64 if let rest = value("regexp "), let parts = split(rest, " in ") {
65 return String(localized: "regexp \(parts.0) in \(parts.1)", comment: "Why a table needs Emacs: part of a Lisp regular expression")
66 }
67 if let rest = value("value< on "), let parts = split(rest, " and ") {
68 return String(localized: "value< on \(parts.0) and \(parts.1)", comment: comment)
69 }
70 if what.hasSuffix(" on a place") {
71 let name = String(what.dropLast(" on a place".count))
72 return String(localized: "\(name) on a place", comment: "Why a table needs Emacs: a Lisp function used on a variable")
73 }
74 if what.hasSuffix(" in a buffer") {
75 let name = String(what.dropLast(" in a buffer".count))
76 return String(localized: "\(name) in a buffer", comment: comment)
77 }
78 if let parts = split(what, " with ") { return String(localized: "\(parts.0) with \(parts.1)", comment: comment) }
79 return nil
80 }
81
82 static func fixed(_ reason: String) -> String? {
83 let comment: StaticString = "Why a table needs Emacs"
84 switch reason {
85 case "arguments": return String(localized: "arguments", comment: comment)
86 case "calc precision": return String(localized: "calc precision", comment: comment)
87 case "chained comparison": return String(localized: "chained comparison", comment: comment)
88 case "comparison with nan": return String(localized: "comparison with nan", comment: comment)
89 case "complex result": return String(localized: "complex result", comment: comment)
90 case "condition": return String(localized: "condition", comment: comment)
91 case "constants that refer to themselves": return String(localized: "constants that refer to themselves", comment: comment)
92 case "date arithmetic": return String(localized: "date arithmetic", comment: comment)
93 case "date syntax": return String(localized: "date syntax", comment: comment)
94 case "division by zero": return String(localized: "division by zero", comment: comment)
95 case "duration size": return String(localized: "duration size", comment: comment)
96 case "empty vector": return String(localized: "empty vector", comment: comment)
97 case "formula range": return String(localized: "formula range", comment: comment)
98 case "formula syntax": return String(localized: "formula syntax", comment: comment)
99 case "infinite result": return String(localized: "infinite result", comment: comment)
100 case "integer size": return String(localized: "integer size", comment: comment)
101 case "logarithm domain": return String(localized: "logarithm domain", comment: comment)
102 case "number": return String(localized: "number", comment: comment)
103 case "number size": return String(localized: "number size", comment: comment)
104 case "range outside row": return String(localized: "range outside row", comment: comment)
105 case "reference outside table": return String(localized: "reference outside table", comment: comment)
106 case "remote table name from a range": return String(localized: "remote table name from a range", comment: comment)
107 case "row out of range": return String(localized: "row out of range", comment: comment)
108 case "table position": return String(localized: "table position", comment: comment)
109 case "variance of one value": return String(localized: "variance of one value", comment: comment)
110 case "vector argument": return String(localized: "vector argument", comment: comment)
111 case "vector arithmetic": return String(localized: "vector arithmetic", comment: comment)
112 case "vector result": return String(localized: "vector result", comment: comment)
113 default: return nil
114 }
115 }
116}