Sources/OrgApp/TableReason.swift

7b8e9c5338e64edcb9d405f5449784741a5bdfa2
orgstar/Sources/OrgApp/TableReason.swift

131 lines · 10618 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 number = value("number size ") { return String(localized: "number size \(number)", comment: "Why a table needs Emacs: a number too large") }
 21        if let flag = value("format flag ") { return String(localized: "format flag \(flag)", comment: "Why a table needs Emacs: a format specifier flag") }
 22        return nil
 23    }
 24
 25    /// `Elisp.Unsupported`'s reasons.
 26    static func lisp(_ what: String) -> String? {
 27        let comment: StaticString = "Why a table needs Emacs: something a Lisp formula does"
 28        switch what {
 29        case "integer size": return fixed(what)
 30        case "a closure as a list": return String(localized: "a closure as a list", comment: comment)
 31        case "capitalize past ASCII": return String(localized: "capitalize past ASCII", comment: comment)
 32        case "case folding past ASCII": return String(localized: "case folding past ASCII", comment: comment)
 33        case "cl-loop destructuring": return String(localized: "cl-loop destructuring", comment: comment)
 34        case "cl-loop with collect and sum": return String(localized: "cl-loop with collect and sum", comment: comment)
 35        case "cl-loop without for": return String(localized: "cl-loop without for", comment: comment)
 36        case "float times": return String(localized: "float times", comment: comment)
 37        case "float-time of picoseconds": return String(localized: "float-time of picoseconds", comment: comment)
 38        case "org-time-string-to-absolute with a day": return String(localized: "org-time-string-to-absolute with a day", comment: comment)
 39        case "random with a seed": return String(localized: "random with a seed", comment: comment)
 40        case "regexp classes on text past ASCII": return String(localized: "regexp classes on text past ASCII", comment: comment)
 41        case "replace-match case past ASCII": return String(localized: "replace-match case past ASCII", comment: comment)
 42        case "replace-match in a buffer": return String(localized: "replace-match in a buffer", comment: comment)
 43        case "replace-regexp-in-string from the end": return String(localized: "replace-regexp-in-string from the end", comment: comment)
 44        case "seq-uniq with a test": return String(localized: "seq-uniq with a test", comment: comment)
 45        case "sort on a string": return String(localized: "sort on a string", comment: comment)
 46        case "the data of this error": return String(localized: "the data of this error", comment: comment)
 47        default: break
 48        }
 49        func value(_ prefix: String) -> String? { what.hasPrefix(prefix) ? String(what.dropFirst(prefix.count)) : nil }
 50        func split(_ text: String, _ separator: String) -> (String, String)? {
 51            guard let range = text.range(of: separator) else { return nil }
 52            return (String(text[..<range.lowerBound]), String(text[range.upperBound...]))
 53        }
 54        if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
 55        if let time = value("time value ") { return String(localized: "time value \(time)", comment: "Why a table needs Emacs: a Lisp time value") }
 56        if let number = value("date number ") { return String(localized: "date number \(number)", comment: "Why a table needs Emacs: a Lisp date number") }
 57        if let text = value("date-to-time of ") { return String(localized: "date-to-time of \(text)", comment: comment) }
 58        if let parts = value("date ") { return String(localized: "date \(parts)", comment: comment) }
 59        if let text = value("time string ") { return String(localized: "time string \(text)", comment: comment) }
 60        if let zone = value("format-time-string zone ") { return String(localized: "format-time-string zone \(zone)", comment: comment) }
 61        if let code = value("format-time-string ") { return String(localized: "format-time-string \(code)", comment: comment) }
 62        if let clause = value("cl-loop ") { return String(localized: "cl-loop \(clause)", comment: comment) }
 63        if let names = value("condition-case on ") { return String(localized: "condition-case on \(names)", comment: comment) }
 64        if let pattern = value("pcase pattern ") { return String(localized: "pcase pattern \(pattern)", comment: comment) }
 65        if let name = value("variable ") { return String(localized: "variable \(name)", comment: "Why a table needs Emacs: a Lisp variable with no value") }
 66        if let name = value("let-alist ") { return String(localized: "let-alist \(name)", comment: comment) }
 67        if let text = value("string-width of ") { return String(localized: "string-width of \(text)", comment: comment) }
 68        if let rest = value("regexp "), let parts = split(rest, " in ") {
 69            return String(localized: "regexp \(parts.0) in \(parts.1)", comment: "Why a table needs Emacs: part of a Lisp regular expression")
 70        }
 71        if let rest = value("value< on "), let parts = split(rest, " and ") {
 72            return String(localized: "value< on \(parts.0) and \(parts.1)", comment: comment)
 73        }
 74        if what.hasSuffix(" on a place") {
 75            let name = String(what.dropLast(" on a place".count))
 76            return String(localized: "\(name) on a place", comment: "Why a table needs Emacs: a Lisp function used on a variable")
 77        }
 78        if what.hasSuffix(" on a closure") {
 79            let name = String(what.dropLast(" on a closure".count))
 80            return String(localized: "\(name) on a closure", comment: "Why a table needs Emacs: a Lisp function that looks inside a closure")
 81        }
 82        if what.hasSuffix(" on a list another value shares") {
 83            let name = String(what.dropLast(" on a list another value shares".count))
 84            return String(localized: "\(name) on a list another value shares", comment: "Why a table needs Emacs: a Lisp function that changes a shared list")
 85        }
 86        if what.hasSuffix(" in a buffer") {
 87            let name = String(what.dropLast(" in a buffer".count))
 88            return String(localized: "\(name) in a buffer", comment: comment)
 89        }
 90        if let parts = split(what, " with ") { return String(localized: "\(parts.0) with \(parts.1)", comment: comment) }
 91        return nil
 92    }
 93
 94    static func fixed(_ reason: String) -> String? {
 95        let comment: StaticString = "Why a table needs Emacs"
 96        switch reason {
 97        case "arguments": return String(localized: "arguments", comment: comment)
 98        case "calc precision": return String(localized: "calc precision", comment: comment)
 99        case "chained comparison": return String(localized: "chained comparison", comment: comment)
100        case "comparison with nan": return String(localized: "comparison with nan", comment: comment)
101        case "complex result": return String(localized: "complex result", comment: comment)
102        case "condition": return String(localized: "condition", comment: comment)
103        case "constants that refer to themselves": return String(localized: "constants that refer to themselves", comment: comment)
104        case "date arithmetic": return String(localized: "date arithmetic", comment: comment)
105        case "date syntax": return String(localized: "date syntax", comment: comment)
106        case "daylight saving time": return String(localized: "daylight saving time", comment: comment)
107        case "division by zero": return String(localized: "division by zero", comment: comment)
108        case "duration size": return String(localized: "duration size", comment: comment)
109        case "empty vector": return String(localized: "empty vector", comment: comment)
110        case "formula range": return String(localized: "formula range", comment: comment)
111        case "formula syntax": return String(localized: "formula syntax", comment: comment)
112        case "infinite result": return String(localized: "infinite result", comment: comment)
113        case "integer size": return String(localized: "integer size", comment: comment)
114        case "logarithm domain": return String(localized: "logarithm domain", comment: comment)
115        case "number": return String(localized: "number", comment: comment)
116        case "number size": return String(localized: "number size", comment: comment)
117        case "range outside row": return String(localized: "range outside row", comment: comment)
118        case "reference outside table": return String(localized: "reference outside table", comment: comment)
119        case "remote table name from a range": return String(localized: "remote table name from a range", comment: comment)
120        case "row out of range": return String(localized: "row out of range", comment: comment)
121        case "symbolic arithmetic": return String(localized: "symbolic arithmetic", comment: comment)
122        case "symbolic mode": return String(localized: "symbolic mode", comment: comment)
123        case "table position": return String(localized: "table position", comment: comment)
124        case "variance of one value": return String(localized: "variance of one value", comment: comment)
125        case "vector argument": return String(localized: "vector argument", comment: comment)
126        case "vector arithmetic": return String(localized: "vector arithmetic", comment: comment)
127        case "vector result": return String(localized: "vector result", comment: comment)
128        default: return nil
129        }
130    }
131}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form