| @@ -1,9 +1,9 @@ |
| 1 | 1 | import Foundation |
| 2 | 2 | |
| 3 | 3 | // Table formulas (org-table.el, Org 9.8.7): `org-table-recalculate` and |
| 4 | | // `org-table-eval-formula` with Calc evaluated natively. Column names, constants, named |
| 5 | | // fields, marked rows, `remote()`, Lisp formulas, durations and Calc features beyond `Calc` |
| 6 | | // throw `Calc.Unsupported`; the caller then recalculates the table in Emacs. |
| 4 | // `org-table-eval-formula` with Calc and Lisp evaluated natively. Symbolic results, Calc |
| 5 | // features beyond `Calc`, Lisp beyond `Elisp`, remote tables found by ID and relative field |
| 6 | // targets throw `Calc.Unsupported`; the caller then recalculates the table in Emacs. |
| 7 | 7 | |
| 8 | 8 | extension EmacsBuffer { |
| 9 | 9 | struct TableAnalysis { |
| @@ -13,6 +13,14 @@ extension EmacsBuffer { |
| 13 | 13 | var types: [Bool] |
| 14 | 14 | var ncol: Int |
| 15 | 15 | var begin: Int |
| 16 | /// `org-table-column-names`: from the first `!` row. |
| 17 | var columnNames: [(name: String, column: Int)] = [] |
| 18 | /// `org-table-local-parameters`, in the order org pushes them; the last match wins. |
| 19 | var parameters: [(name: String, value: String)] = [] |
| 20 | /// `org-table-named-field-locations`: the table-relative line and the column. |
| 21 | var namedFields: [(name: String, line: Int, column: Int)] = [] |
| 22 | /// A row marked for calculation (`org-table-calculate-mark-regexp`). |
| 23 | var marked = false |
| 16 | 24 | } |
| 17 | 25 | |
| 18 | 26 | static let tblfmPattern = "^[ \\t]*#\\+TBLFM: *(.*)" |
| @@ -26,13 +34,12 @@ extension EmacsBuffer { |
| 26 | 34 | point = begin |
| 27 | 35 | var dlines = [0] |
| 28 | 36 | var types: [Bool] = [] |
| 37 | var lines: [String] = [] |
| 29 | 38 | var line = 0 |
| 30 | 39 | while point < end || (point == end && false) { |
| 31 | 40 | let content = substring(lineStart(point), lineEnd(point)) |
| 32 | 41 | guard content.range(of: "^[ \\t]*\\|", options: .regularExpression) != nil else { break } |
| 33 | | if content.range(of: "^[ \\t]*\\| *[!$_^] *(\\||$)", options: .regularExpression) != nil { |
| 34 | | throw Calc.Unsupported("named columns, parameters or fields") |
| 35 | | } |
| 42 | lines.append(content) |
| 36 | 43 | let isHline = content.range(of: "^[ \\t]*\\|-", options: .regularExpression) != nil |
| 37 | 44 | types.append(isHline) |
| 38 | 45 | if !isHline { dlines.append(line) } |
| @@ -45,10 +52,43 @@ extension EmacsBuffer { |
| 45 | 52 | forwardLine(dlines[1]) |
| 46 | 53 | let first = substring(lineStart(point), lineEnd(point)) |
| 47 | 54 | let ncol = Self.splitFields(first, separator: "[ \\t]*\\|[ \\t]*").count |
| 48 | | return TableAnalysis(dlines: dlines, types: types, ncol: ncol, begin: begin) |
| 55 | var analysis = TableAnalysis(dlines: dlines, types: types, ncol: ncol, begin: begin) |
| 56 | Self.names(lines, into: &analysis) |
| 57 | return analysis |
| 49 | 58 | } |
| 50 | 59 | } |
| 51 | 60 | |
| 61 | /// The names part of `org-table-analyze`. |
| 62 | static func names(_ lines: [String], into analysis: inout TableAnalysis) { |
| 63 | let identifier = "^[a-zA-Z][_a-zA-Z0-9]*$" |
| 64 | func rest(_ line: String, _ pattern: String) -> String? { |
| 65 | guard let m = try! NSRegularExpression(pattern: pattern).firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) else { return nil } |
| 66 | return (line as NSString).substring(with: m.range(at: m.numberOfRanges - 1)) |
| 67 | } |
| 68 | if let names = lines.lazy.compactMap({ rest($0, "^[ \\t]*\\| *! *(\\|.*)") }).first { |
| 69 | for (i, name) in splitFields(names, separator: " *\\| *").enumerated() where name.range(of: identifier, options: .regularExpression) != nil { |
| 70 | analysis.columnNames.append((name, i + 2)) |
| 71 | } |
| 72 | } |
| 73 | for line in lines { |
| 74 | guard let fields = rest(line, "^[ \\t]*\\| *\\$ *(\\|.*)") else { continue } |
| 75 | for field in splitFields(fields, separator: " *\\| *") { |
| 76 | if let m = field.firstMatch(of: /^([a-zA-Z][_a-zA-Z0-9]*|%) *= *(.*)/) { analysis.parameters.append((String(m.1), String(m.2))) } |
| 77 | } |
| 78 | } |
| 79 | for (k, line) in lines.enumerated() { |
| 80 | guard let m = line.firstMatch(of: /^[ \t]*\| *([_^]) *(\|.*)/) else { continue } |
| 81 | let target = m.1 == "_" ? k + 1 : k - 1 |
| 82 | guard lines.indices.contains(target), let values = rest(lines[target], "^[ \\t]*\\|[^|]*(\\|.*)") else { continue } |
| 83 | for (i, (name, value)) in zip(splitFields(String(m.2), separator: " *\\| *"), splitFields(values, separator: " *\\| *")).enumerated() |
| 84 | where name.range(of: identifier, options: .regularExpression) != nil { |
| 85 | analysis.parameters.append((name, value)) |
| 86 | analysis.namedFields.append((name, target, i + 2)) |
| 87 | } |
| 88 | } |
| 89 | analysis.marked = lines.contains { $0.range(of: "^[ \\t]*\\| *[!$^_#*] *(\\||$)", options: .regularExpression) != nil } |
| 90 | } |
| 91 | |
| 52 | 92 | /// `org-split-string`: leading and trailing separators produce no empty strings. |
| 53 | 93 | static func splitFields(_ s: String, separator: String) -> [String] { |
| 54 | 94 | let ns = s as NSString |
| @@ -259,28 +299,180 @@ extension EmacsBuffer { |
| 259 | 299 | } |
| 260 | 300 | |
| 261 | 301 | static let calcNumber = "^[-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?$" |
| 302 | /// `org-ts-regexp-both`. |
| 303 | static let timestampBoth = "[\\[<]([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)[\\]>]" |
| 304 | |
| 305 | /// How `org-table-make-reference` turns fields into formula text. |
| 306 | struct ReferenceMode { |
| 307 | enum Lisp { case none, lisp, literal } |
| 308 | var keepEmpty = false |
| 309 | var numbers = false |
| 310 | var lisp = Lisp.none |
| 311 | } |
| 312 | |
| 313 | /// `number-to-string` of `string-to-number`. |
| 314 | static func numberText(_ s: String) -> String { Elisp.printed(Elisp.stringToNumber(s)) } |
| 262 | 315 | |
| 263 | | /// `org-table-make-reference` for Calc, refusing what isn't a number. |
| 264 | | static func reference(_ value: TableRangeValue) throws -> String { |
| 265 | | func number(_ s: String) throws -> String { |
| 266 | | guard s.range(of: calcNumber, options: .regularExpression) != nil else { throw Calc.Unsupported("non-numeric field \(s)") } |
| 267 | | return s |
| 316 | /// `org-table-make-reference`. For Calc, a field that isn't a number, a date or `nan` |
| 317 | /// would be a symbol, which isn't supported. |
| 318 | static func makeReference(_ value: TableRangeValue, _ mode: ReferenceMode) throws -> String { |
| 319 | func blank(_ s: String) -> Bool { !s.contains { !$0.isWhitespace } } |
| 320 | func lisp(_ s: String) -> String { |
| 321 | switch mode.lisp { |
| 322 | case .literal: s |
| 323 | default: mode.numbers ? numberText(s) : Elisp.printed(.string(s), escape: true) |
| 324 | } |
| 268 | 325 | } |
| 326 | func calc(_ s: String) throws -> String { |
| 327 | let text = mode.numbers ? numberText(s) : s |
| 328 | guard text.range(of: calcNumber, options: .regularExpression) != nil || text == "nan" |
| 329 | || text.range(of: "^" + timestampBoth + "$", options: .regularExpression) != nil else { |
| 330 | throw Calc.Unsupported("non-numeric field \(text)") |
| 331 | } |
| 332 | return text |
| 333 | } |
| 334 | let empty = !mode.keepEmpty || mode.numbers ? "0" : "nan" |
| 269 | 335 | switch value { |
| 270 | 336 | case .field(let s): |
| 271 | | return s.contains(where: { !$0.isWhitespace }) ? "(" + (try number(s)) + ")" : "(0)" |
| 272 | | case .list(let items): |
| 273 | | return "[" + (try items.filter { $0.contains(where: { !$0.isWhitespace }) }.map(number)).joined(separator: ",") + "]" |
| 337 | if mode.lisp != .none { return lisp(s) } |
| 338 | return blank(s) ? (empty == "0" ? "(0)" : "nan") : "(" + (try calc(s)) + ")" |
| 339 | case .list(var items): |
| 340 | if !mode.keepEmpty { items = items.filter { !blank($0) } } |
| 341 | if mode.lisp != .none { return items.map(lisp).joined(separator: " ") } |
| 342 | return "[" + (try items.map { blank($0) ? empty : try calc($0) }).joined(separator: ",") + "]" |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// `org-table-time-string-to-seconds`. |
| 347 | static func timeStringToSeconds(_ s: String) -> String { |
| 348 | guard !s.isEmpty else { return s } |
| 349 | func seconds(_ sign: Substring, _ parts: [Substring]) -> String { |
| 350 | let total = parts.enumerated().reduce(0) { sum, part in |
| 351 | sum + (Int(part.element) ?? 0) * [3600, 60, 1][part.offset] |
| 352 | } |
| 353 | return String(sign.isEmpty ? total : -total) |
| 354 | } |
| 355 | if let m = s.firstMatch(of: /(-?)([0-9]+):([0-9]+):([0-9]+)/) { return seconds(m.1, [m.2, m.3, m.4]) } |
| 356 | if s.range(of: timestampBoth, options: .regularExpression) == nil, let m = s.firstMatch(of: /(-?)([0-9]+):([0-9]+)/) { |
| 357 | return seconds(m.1, [m.2, m.3]) |
| 358 | } |
| 359 | return numberText(s) |
| 360 | } |
| 361 | |
| 362 | enum DurationFormat { case hms, hhmm, hours } |
| 363 | |
| 364 | /// `org-table-time-seconds-to-string`, with `org-table-duration-custom-format` `hours` |
| 365 | /// and zero-padded hours. |
| 366 | static func secondsToString(_ seconds: Sexp, _ format: DurationFormat) -> String { |
| 367 | let value = (try? Elisp.number(seconds))?.double ?? 0 |
| 368 | let magnitude = abs(value) |
| 369 | let result: String |
| 370 | switch format { |
| 371 | case .hours: |
| 372 | result = String(format: "%.2f", magnitude / 3600) |
| 373 | case .hms, .hhmm: |
| 374 | let total = Int(magnitude.rounded(.down)) |
| 375 | let hms = String(format: "%02d:%02d:%02d", total / 3600, total / 60 % 60, total % 60) |
| 376 | result = format == .hhmm ? String(hms.dropLast(3)) : hms |
| 377 | } |
| 378 | return value < 0 ? "-" + result : result |
| 379 | } |
| 380 | |
| 381 | /// `org-table-convert-refs-to-rc` for a remote reference: `B3` is `@3$2`. |
| 382 | static func refsToRC(_ s: String) -> String { |
| 383 | let regex = try! NSRegularExpression(pattern: "(?<![A-Za-z0-9_$@])([a-zA-Z]{1,2})([0-9]+)(?![A-Za-z0-9_])") |
| 384 | var out = s |
| 385 | while let m = regex.firstMatch(in: out, range: NSRange(location: 0, length: (out as NSString).length)) { |
| 386 | let ns = out as NSString |
| 387 | let column = ns.substring(with: m.range(at: 1)).uppercased().unicodeScalars.reduce(0) { $0 * 26 + Int($1.value) - 64 } |
| 388 | out = ns.replacingCharacters(in: m.range, with: "@\(ns.substring(with: m.range(at: 2)))$\(column)") |
| 389 | } |
| 390 | return out |
| 391 | } |
| 392 | |
| 393 | /// `org-table-get-constant`: a parameter or named field, a `#+CONSTANTS` value, or the |
| 394 | /// entry's property for `PROP_name`. |
| 395 | func tableConstant(_ name: String, analysis: TableAnalysis) -> String { |
| 396 | if let parameter = analysis.parameters.last(where: { $0.name == name }) { return parameter.value } |
| 397 | for m in (settings.setupKeywords + string).matches(of: #/(?mi)^[ \t]*#\+CONSTANTS:(.*)$/#) { |
| 398 | for pair in m.1.split(whereSeparator: \.isWhitespace) { |
| 399 | let parts = pair.split(separator: "=", maxSplits: 1) |
| 400 | if parts.count == 2, parts[0] == name { return String(parts[1]) } |
| 401 | } |
| 274 | 402 | } |
| 403 | if name.hasPrefix("PROP_"), let value = inheritedProperty(String(name.dropFirst(5)))?.value { return value } |
| 404 | return "#UNDEFINED_NAME" |
| 275 | 405 | } |
| 276 | 406 | |
| 277 | | /// `org-table-eval-formula` with the equation given, at point. |
| 278 | | func tableEvalFormula(_ formula: String, analysis: TableAnalysis) throws { |
| 407 | /// `org-table-formula-substitute-names`: column names become `$N`; parameters, named |
| 408 | /// fields and constants their values, in parentheses outside Lisp formulas. |
| 409 | func substituteNames(_ formula: String, analysis: TableAnalysis) -> String { |
| 410 | let parenthesize = !formula.hasPrefix("'") |
| 411 | let duration = formula.range(of: ";.*[Tt].*$", options: .regularExpression) != nil |
| 412 | var new = formula |
| 413 | if !analysis.columnNames.isEmpty { |
| 414 | let names = analysis.columnNames.map { NSRegularExpression.escapedPattern(for: $0.name) }.joined(separator: "|") |
| 415 | let regex = try! NSRegularExpression(pattern: "\\$(\(names))(?![[:alnum:]])") |
| 416 | var start = 0 |
| 417 | while let m = regex.firstMatch(in: new, range: NSRange(location: start, length: (new as NSString).length - start)) { |
| 418 | let ns = new as NSString |
| 419 | let column = analysis.columnNames.first { $0.name == ns.substring(with: m.range(at: 1)) }!.column |
| 420 | new = ns.replacingCharacters(in: m.range, with: "$\(column)") |
| 421 | start = m.range.location + 1 |
| 422 | } |
| 423 | } |
| 424 | let regex = try! NSRegularExpression(pattern: "\\$([a-zA-Z][_a-zA-Z0-9]*)|(\\bremote\\([^)]*\\))") |
| 425 | var start = 0 |
| 426 | while let m = regex.firstMatch(in: new, range: NSRange(location: start, length: (new as NSString).length - start)) { |
| 427 | let ns = new as NSString |
| 428 | if m.range(at: 2).location != NSNotFound { |
| 429 | start = NSMaxRange(m.range(at: 2)) |
| 430 | continue |
| 431 | } |
| 432 | start = m.range.location + 1 |
| 433 | var value = tableConstant(ns.substring(with: m.range(at: 1)), analysis: analysis) |
| 434 | if duration, value.contains(where: { !$0.isWhitespace }) { value = Self.timeStringToSeconds(value) } |
| 435 | new = ns.replacingCharacters(in: m.range, with: parenthesize ? "(" + value + ")" : value) |
| 436 | } |
| 437 | return new |
| 438 | } |
| 439 | |
| 440 | /// `org-table-get-remote-range`: the table after `#+NAME: name` in this file. |
| 441 | func remoteRange(_ name: String, _ form: String) throws -> TableRangeValue { |
| 442 | let ns = string as NSString |
| 443 | let pattern = "^[ \\t]*#\\+(tbl)?name:[ \\t]*" + NSRegularExpression.escapedPattern(for: name) + "[ \\t]*$" |
| 444 | let keyword = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive]) |
| 445 | guard let found = keyword.firstMatch(in: string, range: NSRange(location: 0, length: ns.length)) else { |
| 446 | throw Calc.Unsupported("remote table \(name) by ID") |
| 447 | } |
| 448 | let after = min(ns.length, found.range.location + 1) |
| 449 | let next = try! NSRegularExpression(pattern: "^(\\*+ )|^[ \\t]*\\|", options: .anchorsMatchLines) |
| 450 | guard let m = next.firstMatch(in: string, range: NSRange(location: after, length: ns.length - after)), |
| 451 | m.range(at: 1).location == NSNotFound else { |
| 452 | throw UserError(message: "Cannot find a table at NAME or ID \(name)") |
| 453 | } |
| 454 | let remote = EmacsBuffer(string, point: m.range.location, settings: settings, options: options) |
| 455 | let analysis = try remote.tableAnalyze() |
| 456 | let resolved = remote.substituteNames(try Self.handleFirstLast(Self.refsToRC(form), analysis: analysis), analysis: analysis) |
| 457 | let range = try! NSRegularExpression(pattern: Self.rangePattern) |
| 458 | let rns = resolved as NSString |
| 459 | if let r = range.firstMatch(in: resolved, range: NSRange(location: 0, length: rns.length)), r.range.length > 1 { |
| 460 | return try remote.tableRange(rns.substring(with: r.range), thisline: 0, col: 1, analysis: analysis) |
| 461 | } |
| 462 | return .field(resolved) |
| 463 | } |
| 464 | |
| 465 | /// `org-table-eval-formula` with the equation given, at point. `substituted` when names |
| 466 | /// and constants were already replaced, as `org-table-recalculate` does. |
| 467 | func tableEvalFormula(_ formula: String, analysis: TableAnalysis, substituted: Bool = true) throws { |
| 279 | 468 | var formula = formula |
| 280 | 469 | var modes = Calc.Modes() |
| 470 | var mode = ReferenceMode() |
| 471 | var duration: DurationFormat? |
| 472 | var literal = false |
| 281 | 473 | var fmt: String? |
| 282 | 474 | if let semicolon = formula.lastIndex(of: ";") { |
| 283 | | var flags = String(formula[formula.index(after: semicolon)...]) |
| 475 | var flags = (analysis.parameters.last { $0.name == "%" }?.value ?? "") + formula[formula.index(after: semicolon)...] |
| 284 | 476 | formula = String(formula[..<semicolon]) |
| 285 | 477 | let numeric = try! NSRegularExpression(pattern: "([pnfse])(-?[0-9]+)") |
| 286 | 478 | while let m = numeric.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
| @@ -299,27 +491,32 @@ extension EmacsBuffer { |
| 299 | 491 | while let m = letters.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
| 300 | 492 | let ns = flags as NSString |
| 301 | 493 | switch ns.substring(with: m.range) { |
| 494 | case "t": (duration, mode.numbers) = (.hours, true) |
| 495 | case "T": (duration, mode.numbers) = (.hms, true) |
| 496 | case "U": (duration, mode.numbers) = (.hhmm, true) |
| 497 | case "N": mode.numbers = true |
| 498 | case "L": literal = true |
| 499 | case "E": mode.keepEmpty = true |
| 302 | 500 | case "D": modes.degrees = true |
| 303 | 501 | case "R": modes.degrees = false |
| 304 | 502 | default: throw Calc.Unsupported("format flag \(ns.substring(with: m.range))") |
| 305 | 503 | } |
| 306 | 504 | flags = ns.replacingCharacters(in: m.range, with: "") |
| 307 | 505 | } |
| 308 | | if flags.contains(where: { !$0.isWhitespace }) { |
| 309 | | guard flags.range(of: "^%[-+ #0]*[0-9]*(\\.[0-9]+)?[fgeE]$", options: .regularExpression) != nil else { |
| 310 | | throw Calc.Unsupported("format \(flags)") |
| 311 | | } |
| 312 | | fmt = flags |
| 313 | | } |
| 506 | if flags.contains(where: { !$0.isWhitespace }) { fmt = flags } |
| 314 | 507 | } |
| 315 | | if formula.hasPrefix("'(") { throw Calc.Unsupported("Lisp formula") } |
| 316 | | formula = substituteConstants(formula) |
| 317 | | if formula.range(of: "\\$[a-zA-Z_]", options: .regularExpression) != nil || formula.contains("remote(") |
| 318 | | || formula.range(of: "[<\\[][0-9]{4}-", options: .regularExpression) != nil || formula.contains("&") { |
| 319 | | throw Calc.Unsupported("names, constants, remote references or dates") |
| 508 | if !substituted { formula = try Self.handleFirstLast(substituteNames(formula, analysis: analysis), analysis: analysis) } |
| 509 | var fields = Self.splitFields(substring(lineStart(point), lineEnd(point)).trimmingCharacters(in: .whitespaces), separator: " *\\| *") |
| 510 | if duration != nil { fields = fields.map(Self.timeStringToSeconds) } |
| 511 | if mode.numbers { fields = fields.map { $0.contains(where: { !$0.isWhitespace }) ? Self.numberText($0) : $0 } } |
| 512 | if formula.count > 2, formula.hasPrefix("'(") { mode.lisp = literal ? .literal : .lisp } |
| 513 | func reference(_ value: TableRangeValue) throws -> String { |
| 514 | guard duration != nil else { return try Self.makeReference(value, mode) } |
| 515 | switch value { |
| 516 | case .field(let s): return try Self.makeReference(.field(Self.timeStringToSeconds(s)), mode) |
| 517 | case .list(let items): return try Self.makeReference(.list(items.map(Self.timeStringToSeconds)), mode) |
| 518 | } |
| 320 | 519 | } |
| 321 | | let line = substring(lineStart(point), lineEnd(point)).trimmingCharacters(in: .whitespaces) |
| 322 | | let fields = Self.splitFields(line, separator: " *\\| *") |
| 323 | 520 | let n0 = tableCurrentColumn() |
| 324 | 521 | let thisline = try saveExcursion { () -> Int in |
| 325 | 522 | let here = lineStart(point) |
| @@ -334,10 +531,31 @@ extension EmacsBuffer { |
| 334 | 531 | var form = formula |
| 335 | 532 | form = form.replacingOccurrences(of: "@#", with: String(tableCurrentDataLine())) |
| 336 | 533 | form = form.replacingOccurrences(of: "$#", with: String(n0)) |
| 534 | // `org-table-remote-reference-indirection`: `remote($1, …)` names the table in a field. |
| 535 | let indirect = try! NSRegularExpression(pattern: "\\bremote\\([ \\t]*([@$][^ \\t,]+)[ \\t]*,[ \\t]*([^\\n)]+)\\)") |
| 536 | var start = 0 |
| 537 | while let m = indirect.firstMatch(in: form, range: NSRange(location: start, length: (form as NSString).length - start)) { |
| 538 | let ns = form as NSString |
| 539 | var eq = try Self.handleFirstLast(ns.substring(with: m.range(at: 1)), analysis: analysis) |
| 540 | if eq.range(of: "^\\$[0-9]+$", options: .regularExpression) != nil { eq = "@0" + eq } |
| 541 | guard case .field(let name) = try tableRange(eq, thisline: thisline, col: n0, analysis: analysis) else { |
| 542 | throw Calc.Unsupported("remote table name from a range") |
| 543 | } |
| 544 | form = ns.replacingCharacters(in: m.range(at: 1), with: name) |
| 545 | start = m.range(at: 1).location + (name as NSString).length |
| 546 | } |
| 547 | let remote = try! NSRegularExpression(pattern: "\\bremote\\([ \\t]*([^,)]+)[ \\t]*,[ \\t]*([^\\n)]+)\\)") |
| 548 | while let m = remote.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)) { |
| 549 | let ns = form as NSString |
| 550 | let name = ns.substring(with: m.range(at: 1)).trimmingCharacters(in: .whitespaces) |
| 551 | let value = try remoteRange(name, ns.substring(with: m.range(at: 2))) |
| 552 | form = ns.replacingCharacters(in: m.range, with: try reference(value)) |
| 553 | } |
| 337 | 554 | let range = try! NSRegularExpression(pattern: Self.rangePattern) |
| 338 | 555 | while let m = range.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)), m.range.length > 1 { |
| 339 | 556 | let ns = form as NSString |
| 340 | | let replacement = try Self.reference(try tableRange(ns.substring(with: m.range), thisline: thisline, col: n0, analysis: analysis)) |
| 557 | let replacement = try reference(try tableRange(ns.substring(with: m.range), thisline: thisline, col: n0, analysis: analysis)) |
| 558 | if replacement.contains(form) { throw UserError(message: "Spreadsheet error: invalid reference \"\(form)\"") } |
| 341 | 559 | form = ns.replacingCharacters(in: m.range, with: replacement) |
| 342 | 560 | } |
| 343 | 561 | let rowRange = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)\\.\\.\\$(([-+])?[0-9]+)") |
| @@ -346,7 +564,7 @@ extension EmacsBuffer { |
| 346 | 564 | let a = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
| 347 | 565 | let b = Int(ns.substring(with: m.range(at: 3)))! + (m.range(at: 4).location != NSNotFound ? n0 : 0) |
| 348 | 566 | guard a >= 1, b <= fields.count, a - 1 <= b else { throw Calc.Unsupported("range outside row") } |
| 349 | | form = ns.replacingCharacters(in: m.range, with: try Self.reference(.list(Array(fields[(a - 1)..<b])))) |
| 567 | form = ns.replacingCharacters(in: m.range, with: try Self.makeReference(.list(Array(fields[(a - 1)..<b])), mode)) |
| 350 | 568 | } |
| 351 | 569 | let single = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)") |
| 352 | 570 | while let m = single.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)) { |
| @@ -354,37 +572,100 @@ extension EmacsBuffer { |
| 354 | 572 | let n = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
| 355 | 573 | let index = (n == 0 ? n0 : max(n, 1)) - 1 |
| 356 | 574 | guard index < fields.count else { throw UserError(message: "Invalid field specifier \"\(ns.substring(with: m.range))\"") } |
| 357 | | form = ns.replacingCharacters(in: m.range, with: try Self.reference(.field(fields[index]))) |
| 575 | let replacement = try Self.makeReference(.field(fields[index]), mode) |
| 576 | if replacement.contains(formula) { throw UserError(message: "Invalid field specifier \"\(ns.substring(with: m.range))\"") } |
| 577 | form = ns.replacingCharacters(in: m.range, with: replacement) |
| 578 | } |
| 579 | |
| 580 | var result: String |
| 581 | if mode.lisp != .none { |
| 582 | result = try evalLisp(form, duration: duration) |
| 583 | } else { |
| 584 | form = form.replacingOccurrences(of: "\\[([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)\\]", with: "<$1>", options: .regularExpression) |
| 585 | form = Self.normalizeTimestamps(form) |
| 586 | if duration != nil, form.range(of: "^[0-9]+:[0-9]+(:[0-9]+)?$", options: .regularExpression) != nil { |
| 587 | result = form |
| 588 | } else { |
| 589 | do { |
| 590 | result = try Calc.evaluate(form, modes: modes, numeric: !mode.keepEmpty && mode.numbers) |
| 591 | } catch is Calc.Failure { |
| 592 | (result, fmt) = ("#ERROR", nil) |
| 593 | } |
| 594 | } |
| 595 | if let duration, !result.isEmpty, result != "#ERROR" { |
| 596 | let seconds = result.range(of: "^[0-9]+:[0-9]+(:[0-9]+)?$", options: .regularExpression) != nil ? Self.timeStringToSeconds(result) : result |
| 597 | result = Self.secondsToString(Elisp.stringToNumber(seconds), duration) |
| 598 | } |
| 358 | 599 | } |
| 359 | | var result = try Calc.evaluate(form, modes: modes) |
| 360 | 600 | if let fmt { |
| 361 | | result = String(format: fmt, Self.stringToNumber(result)) |
| 601 | do { |
| 602 | result = try Elisp.format([.string(fmt), Elisp.stringToNumber(result)]) |
| 603 | } catch let error as Elisp.Signal { |
| 604 | throw UserError(message: error.message) |
| 605 | } |
| 606 | } else { |
| 607 | result = result.replacingOccurrences(of: "<([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)>", with: "[$1]", options: .regularExpression) |
| 362 | 608 | } |
| 363 | 609 | tableSetField(result) |
| 364 | 610 | } |
| 365 | 611 | |
| 366 | | /// `org-table-formula-substitute-names` for constants: `$name` from `#+CONSTANTS`, or |
| 367 | | /// `$PROP_name` from the entry's property, in parentheses; unknown names become |
| 368 | | /// `#UNDEFINED_NAME`. |
| 369 | | func substituteConstants(_ formula: String) -> String { |
| 370 | | guard formula.range(of: "\\$[a-zA-Z]", options: .regularExpression) != nil else { return formula } |
| 371 | | var constants: [String: String] = [:] |
| 372 | | for m in (settings.setupKeywords + string).matches(of: #/(?mi)^[ \t]*#\+CONSTANTS:(.*)$/#) { |
| 373 | | for pair in m.1.split(whereSeparator: \.isWhitespace) { |
| 374 | | let parts = pair.split(separator: "=", maxSplits: 1) |
| 375 | | if parts.count == 2, constants[String(parts[0])] == nil { constants[String(parts[0])] = String(parts[1]) } |
| 612 | /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`. |
| 613 | func evalLisp(_ form: String, duration: DurationFormat?) throws -> String { |
| 614 | let lisp = Elisp() |
| 615 | Self.defineLookups(lisp) |
| 616 | let value: Sexp |
| 617 | do { |
| 618 | value = try lisp.eval(try lisp.eval(try LispReader.readFirst(form).sexp)) |
| 619 | } catch let unsupported as Elisp.Unsupported { |
| 620 | throw Calc.Unsupported("Lisp \(unsupported.what)") |
| 621 | } catch { |
| 622 | return "#ERROR" |
| 623 | } |
| 624 | var result = Elisp.printed(value) |
| 625 | if let duration { result = Self.secondsToString(Elisp.stringToNumber(result), duration) } |
| 626 | return result |
| 627 | } |
| 628 | |
| 629 | /// `org-lookup-first`, `org-lookup-last` and `org-lookup-all`. |
| 630 | static func defineLookups(_ lisp: Elisp) { |
| 631 | func matches(_ lisp: Elisp, _ args: [Sexp]) throws -> [Sexp] { |
| 632 | try Elisp.arity(args, 3...4, "org-lookup") |
| 633 | let keys = try Elisp.elements(args[1]) |
| 634 | let values = Elisp.isNil(args[2]) ? keys : try Elisp.elements(args[2]) |
| 635 | let predicate = args.count > 3 && !Elisp.isNil(args[3]) ? args[3] : .symbol("equal") |
| 636 | var found: [Sexp] = [] |
| 637 | for (i, key) in keys.enumerated() where !Elisp.isNil(try lisp.call(predicate, [args[0], key])) { |
| 638 | found.append(i < values.count ? values[i] : .nil) |
| 376 | 639 | } |
| 640 | return found |
| 377 | 641 | } |
| 378 | | let regex = try! NSRegularExpression(pattern: "\\$([a-zA-Z][_a-zA-Z0-9]*)") |
| 379 | | var out = formula |
| 380 | | while let m = regex.firstMatch(in: out, range: NSRange(location: 0, length: (out as NSString).length)) { |
| 642 | lisp.define("org-lookup-first") { lisp, args in try matches(lisp, args).first ?? .nil } |
| 643 | lisp.define("org-lookup-last") { lisp, args in try matches(lisp, args).last ?? .nil } |
| 644 | lisp.define("org-lookup-all") { lisp, args in |
| 645 | let found = try matches(lisp, args) |
| 646 | return found.isEmpty ? .nil : .list(found) |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /// Timestamps in a formula as Calc reads them: `<YYYY-MM-DD Www>`, with ` HH:MM` when |
| 651 | /// the stamp has a time, as org formats them for Calc. |
| 652 | static func normalizeTimestamps(_ form: String) -> String { |
| 653 | let regex = try! NSRegularExpression(pattern: "<([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)>") |
| 654 | var out = form |
| 655 | var start = 0 |
| 656 | while let m = regex.firstMatch(in: out, range: NSRange(location: start, length: (out as NSString).length - start)) { |
| 381 | 657 | let ns = out as NSString |
| 382 | | let name = ns.substring(with: m.range(at: 1)) |
| 383 | | var value = constants[name] |
| 384 | | if value == nil, name.hasPrefix("PROP_") { |
| 385 | | value = inheritedProperty(String(name.dropFirst(5)))?.value |
| 658 | let stamp = ns.substring(with: m.range(at: 1)) |
| 659 | let parts = stamp.firstMatch(of: /^([0-9]{4})-([0-9]{2})-([0-9]{2})/)! |
| 660 | let day = Days.absolute(year: Int(parts.1)!, month: Int(parts.2)!, day: Int(parts.3)!) |
| 661 | let date = Days.date(day) |
| 662 | var text = String(format: "<%04d-%02d-%02d %@", date.year, date.month, date.day, Calc.weekdays[Days.weekday(day)]) |
| 663 | if let time = stamp.firstMatch(of: /([0-9]{1,2}):([0-9]{2})/) { |
| 664 | text += String(format: " %02d:%02d", Int(time.1)!, Int(time.2)!) |
| 386 | 665 | } |
| 387 | | out = ns.replacingCharacters(in: m.range, with: "(" + (value ?? "#UNDEFINED_NAME") + ")") |
| 666 | text += ">" |
| 667 | out = ns.replacingCharacters(in: m.range, with: text) |
| 668 | start = m.range.location + (text as NSString).length |
| 388 | 669 | } |
| 389 | 670 | return out |
| 390 | 671 | } |
| @@ -429,7 +710,7 @@ extension EmacsBuffer { |
| 429 | 710 | var columnEquations: [(Int, String)] = [] |
| 430 | 711 | var fieldEquations: [(String, String)] = [] |
| 431 | 712 | for (oldLHS, rawRHS) in stored { |
| 432 | | let rhs = try Self.handleFirstLast(rawRHS, analysis: analysis) |
| 713 | let rhs = substituteNames(try Self.handleFirstLast(rawRHS, analysis: analysis), analysis: analysis) |
| 433 | 714 | if oldLHS.range(of: "^@-?I+", options: .regularExpression) != nil { throw UserError(message: "Can't assign to hline relative reference") } |
| 434 | 715 | let lhs = try Self.handleFirstLast(oldLHS, analysis: analysis) |
| 435 | 716 | if lhs.range(of: "^\\$[0-9]+$", options: .regularExpression) != nil { |
| @@ -443,8 +724,13 @@ extension EmacsBuffer { |
| 443 | 724 | for (lhs, rhs) in fieldEquations { |
| 444 | 725 | if let m = lhs.firstMatch(of: /^@([0-9]+)\$([0-9]+)$/) { |
| 445 | 726 | fields.append((Int(m.1)!, Int(m.2)!, rhs)) |
| 446 | | } else if lhs.range(of: "^@[-+0-9]+\\$-?[0-9]+$", options: .regularExpression) != nil || lhs.range(of: "^[a-zA-Z]", options: .regularExpression) != nil { |
| 447 | | throw Calc.Unsupported("relative or named field formula") |
| 727 | } else if lhs.range(of: "^[a-zA-Z][_a-zA-Z0-9]*$", options: .regularExpression) != nil { |
| 728 | guard let location = analysis.namedFields.last(where: { $0.name == lhs }), let row = Self.lineToDline(location.line, analysis) else { |
| 729 | throw UserError(message: "Unknown field: \(lhs)") |
| 730 | } |
| 731 | fields.append((row, location.column, rhs)) |
| 732 | } else if lhs.range(of: "^@[-+0-9]+\\$-?[0-9]+$", options: .regularExpression) != nil { |
| 733 | throw Calc.Unsupported("relative field formula") |
| 448 | 734 | } else if let m = lhs.firstMatch(of: /^@([0-9]+)$/) { |
| 449 | 735 | for c in 1...analysis.ncol { fields.append((Int(m.1)!, c, rhs)) } |
| 450 | 736 | } else { |
| @@ -463,7 +749,11 @@ extension EmacsBuffer { |
| 463 | 749 | let endMarker = marker(end) |
| 464 | 750 | defer { release(endMarker) } |
| 465 | 751 | var beg: Int |
| 466 | | if all { |
| 752 | var lineRegexp = "^[ \\t]*\\|[^-]" |
| 753 | if all, analysis.marked { |
| 754 | beg = analysis.begin |
| 755 | lineRegexp = "^[ \\t]*\\| *[#*] *(\\||$)" |
| 756 | } else if all { |
| 467 | 757 | beg = analysis.begin |
| 468 | 758 | point = beg |
| 469 | 759 | if searchForward("^[ \\t]*\\|[^-]", bound: end, caseInsensitive: false) != nil, |
| @@ -493,7 +783,7 @@ extension EmacsBuffer { |
| 493 | 783 | point = beg |
| 494 | 784 | let rowMarker = marker(beg) |
| 495 | 785 | defer { release(rowMarker) } |
| 496 | | while let match = searchForward("^[ \\t]*\\|[^-]", bound: endMarker.position, caseInsensitive: false) { |
| 786 | while let match = searchForward(lineRegexp, bound: endMarker.position, caseInsensitive: false) { |
| 497 | 787 | _ = match |
| 498 | 788 | let rowStart = lineStart(point) |
| 499 | 789 | rowMarker.position = rowStart |
| @@ -713,7 +1003,7 @@ extension EmacsBuffer { |
| 713 | 1003 | let analysis = try tableAnalyze() |
| 714 | 1004 | let formula = try tableGetFormula(equation, named: named, analysis: analysis) |
| 715 | 1005 | do { |
| 716 | | try tableEvalFormula(try Self.handleFirstLast(formula, analysis: analysis), analysis: analysis) |
| 1006 | try tableEvalFormula(formula, analysis: analysis, substituted: false) |
| 717 | 1007 | } catch let error as Calc.Unsupported { |
| 718 | 1008 | throw UserError(message: "The formula was stored; recalculating it needs Emacs: " + error.reason) |
| 719 | 1009 | } |