Sources/OrgApp/TableReason.swift

150 lines · 11920 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    /// Why a calc block can't be calculated here, `Calc.Unsupported`'s reason, in the app's language.
 10    public static func calcRefusal(_ reason: String) -> String {
 11        String(localized: "This calc block can't be calculated here: \(localized(reason)).", comment: "A calc block orgstar can't calculate")
 12    }
 13
 14    /// A calc block's result, or a refusal in the app's language.
 15    public static func calcBlock(_ job: BabelJob) throws -> BabelOutput {
 16        do {
 17            return try Babel.evaluateCalc(job)
 18        } catch let refusal as Calc.Unsupported {
 19            throw Babel.Failure.message(calcRefusal(refusal.reason))
 20        }
 21    }
 22
 23    /// Nil for a reason not known here.
 24    static func translated(_ reason: String) -> String? {
 25        if reason.hasPrefix("Lisp ") {
 26            let inner = String(reason.dropFirst("Lisp ".count))
 27            guard let what = lisp(inner) else { return nil }
 28            return String(localized: "Lisp \(what)", comment: "Why a table needs Emacs: something its Lisp formula does")
 29        }
 30        if let fixed = fixed(reason) { return fixed }
 31        func value(_ prefix: String) -> String? { reason.hasPrefix(prefix) ? String(reason.dropFirst(prefix.count)) : nil }
 32        if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
 33        if let name = value("variable ") { return String(localized: "variable \(name)", comment: "Why a table needs Emacs: a Calc variable") }
 34        if let number = value("number size ") { return String(localized: "number size \(number)", comment: "Why a table needs Emacs: a number too large") }
 35        if let flag = value("format flag ") { return String(localized: "format flag \(flag)", comment: "Why a table needs Emacs: a format specifier flag") }
 36        if let key = value("stack command ") { return String(localized: "stack command \(key)", comment: "Why a calc block needs Emacs: a key it runs on the stack") }
 37        if let name = value("value of :var ") { return String(localized: "value of :var \(name)", comment: "Why a calc block needs Emacs: a variable's value") }
 38        return nil
 39    }
 40
 41    /// `Elisp.Unsupported`'s reasons.
 42    static func lisp(_ what: String) -> String? {
 43        let comment: StaticString = "Why a table needs Emacs: something a Lisp formula does"
 44        switch what {
 45        case "integer size": return fixed(what)
 46        case "a closure as a list": return String(localized: "a closure as a list", comment: comment)
 47        case "capitalize past ASCII": return String(localized: "capitalize past ASCII", comment: comment)
 48        case "case folding past ASCII": return String(localized: "case folding past ASCII", comment: comment)
 49        case "cl-loop destructuring": return String(localized: "cl-loop destructuring", comment: comment)
 50        case "cl-loop with collect and sum": return String(localized: "cl-loop with collect and sum", comment: comment)
 51        case "cl-loop without for": return String(localized: "cl-loop without for", comment: comment)
 52        case "float times": return String(localized: "float times", comment: comment)
 53        case "float-time of picoseconds": return String(localized: "float-time of picoseconds", comment: comment)
 54        case "org-time-string-to-absolute with a day": return String(localized: "org-time-string-to-absolute with a day", comment: comment)
 55        case "random with a seed": return String(localized: "random with a seed", comment: comment)
 56        case "regexp classes on text past ASCII": return String(localized: "regexp classes on text past ASCII", comment: comment)
 57        case "replace-match case past ASCII": return String(localized: "replace-match case past ASCII", comment: comment)
 58        case "replace-match in a buffer": return String(localized: "replace-match in a buffer", comment: comment)
 59        case "replace-regexp-in-string from the end": return String(localized: "replace-regexp-in-string from the end", comment: comment)
 60        case "seq-uniq with a test": return String(localized: "seq-uniq with a test", comment: comment)
 61        case "sort on a string": return String(localized: "sort on a string", comment: comment)
 62        case "the data of this error": return String(localized: "the data of this error", comment: comment)
 63        default: break
 64        }
 65        func value(_ prefix: String) -> String? { what.hasPrefix(prefix) ? String(what.dropFirst(prefix.count)) : nil }
 66        func split(_ text: String, _ separator: String) -> (String, String)? {
 67            guard let range = text.range(of: separator) else { return nil }
 68            return (String(text[..<range.lowerBound]), String(text[range.upperBound...]))
 69        }
 70        if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
 71        if let time = value("time value ") { return String(localized: "time value \(time)", comment: "Why a table needs Emacs: a Lisp time value") }
 72        if let number = value("date number ") { return String(localized: "date number \(number)", comment: "Why a table needs Emacs: a Lisp date number") }
 73        if let text = value("date-to-time of ") { return String(localized: "date-to-time of \(text)", comment: comment) }
 74        if let parts = value("date ") { return String(localized: "date \(parts)", comment: comment) }
 75        if let text = value("time string ") { return String(localized: "time string \(text)", comment: comment) }
 76        if let zone = value("format-time-string zone ") { return String(localized: "format-time-string zone \(zone)", comment: comment) }
 77        if let code = value("format-time-string ") { return String(localized: "format-time-string \(code)", comment: comment) }
 78        if let clause = value("cl-loop ") { return String(localized: "cl-loop \(clause)", comment: comment) }
 79        if let names = value("condition-case on ") { return String(localized: "condition-case on \(names)", comment: comment) }
 80        if let pattern = value("pcase pattern ") { return String(localized: "pcase pattern \(pattern)", comment: comment) }
 81        if let name = value("variable ") { return String(localized: "variable \(name)", comment: "Why a table needs Emacs: a Lisp variable with no value") }
 82        if let name = value("let-alist ") { return String(localized: "let-alist \(name)", comment: comment) }
 83        if let text = value("string-width of ") { return String(localized: "string-width of \(text)", comment: comment) }
 84        if let rest = value("regexp "), let parts = split(rest, " in ") {
 85            return String(localized: "regexp \(parts.0) in \(parts.1)", comment: "Why a table needs Emacs: part of a Lisp regular expression")
 86        }
 87        if let rest = value("value< on "), let parts = split(rest, " and ") {
 88            return String(localized: "value< on \(parts.0) and \(parts.1)", comment: comment)
 89        }
 90        if what.hasSuffix(" on a place") {
 91            let name = String(what.dropLast(" on a place".count))
 92            return String(localized: "\(name) on a place", comment: "Why a table needs Emacs: a Lisp function used on a variable")
 93        }
 94        if what.hasSuffix(" on a closure") {
 95            let name = String(what.dropLast(" on a closure".count))
 96            return String(localized: "\(name) on a closure", comment: "Why a table needs Emacs: a Lisp function that looks inside a closure")
 97        }
 98        if what.hasSuffix(" on a list another value shares") {
 99            let name = String(what.dropLast(" on a list another value shares".count))
100            return String(localized: "\(name) on a list another value shares", comment: "Why a table needs Emacs: a Lisp function that changes a shared list")
101        }
102        if what.hasSuffix(" in a buffer") {
103            let name = String(what.dropLast(" in a buffer".count))
104            return String(localized: "\(name) in a buffer", comment: comment)
105        }
106        if let parts = split(what, " with ") { return String(localized: "\(parts.0) with \(parts.1)", comment: comment) }
107        return nil
108    }
109
110    static func fixed(_ reason: String) -> String? {
111        let comment: StaticString = "Why a table needs Emacs"
112        switch reason {
113        case "arguments": return String(localized: "arguments", comment: comment)
114        case "calc precision": return String(localized: "calc precision", comment: comment)
115        case "Calc constants": return String(localized: "Calc constants", comment: comment)
116        case "chained comparison": return String(localized: "chained comparison", comment: comment)
117        case "comparison with nan": return String(localized: "comparison with nan", comment: comment)
118        case "complex result": return String(localized: "complex result", comment: comment)
119        case "condition": return String(localized: "condition", comment: comment)
120        case "constants that refer to themselves": return String(localized: "constants that refer to themselves", comment: comment)
121        case "date arithmetic": return String(localized: "date arithmetic", comment: comment)
122        case "date syntax": return String(localized: "date syntax", comment: comment)
123        case "dates in calc blocks": return String(localized: "dates in calc blocks", comment: comment)
124        case "daylight saving time": return String(localized: "daylight saving time", comment: comment)
125        case "division by zero": return String(localized: "division by zero", comment: comment)
126        case "duration size": return String(localized: "duration size", comment: comment)
127        case "empty vector": return String(localized: "empty vector", comment: comment)
128        case "formula range": return String(localized: "formula range", comment: comment)
129        case "formula syntax": return String(localized: "formula syntax", comment: comment)
130        case "infinite result": return String(localized: "infinite result", comment: comment)
131        case "integer size": return String(localized: "integer size", comment: comment)
132        case "logarithm domain": return String(localized: "logarithm domain", comment: comment)
133        case "number": return String(localized: "number", comment: comment)
134        case "number size": return String(localized: "number size", comment: comment)
135        case "numeric :prologue or :epilogue": return String(localized: "numeric :prologue or :epilogue", comment: comment)
136        case "range outside row": return String(localized: "range outside row", comment: comment)
137        case "reference outside table": return String(localized: "reference outside table", comment: comment)
138        case "remote table name from a range": return String(localized: "remote table name from a range", comment: comment)
139        case "row out of range": return String(localized: "row out of range", comment: comment)
140        case "symbolic arithmetic": return String(localized: "symbolic arithmetic", comment: comment)
141        case "symbolic mode": return String(localized: "symbolic mode", comment: comment)
142        case "table position": return String(localized: "table position", comment: comment)
143        case "variance of one value": return String(localized: "variance of one value", comment: comment)
144        case "vector argument": return String(localized: "vector argument", comment: comment)
145        case "vector arithmetic": return String(localized: "vector arithmetic", comment: comment)
146        case "vector result": return String(localized: "vector result", comment: comment)
147        default: return nil
148        }
149    }
150}