| @@ -1,9 +1,9 @@ |
| 1 | import Foundation |
1 | import Foundation |
| 2 | |
2 | |
| 3 | // Table formulas (org-table.el, Org 9.8.7): `org-table-recalculate` and |
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 |
4 | // `org-table-eval-formula` with Calc and Lisp evaluated natively. Symbolic results, Calc |
| 5 | // fields, marked rows, `remote()`, Lisp formulas, durations and Calc features beyond `Calc` |
5 | // features beyond `Calc`, Lisp beyond `Elisp`, remote tables found by ID and relative field |
| 6 | // throw `Calc.Unsupported`; the caller then recalculates the table in Emacs. |
6 | // targets throw `Calc.Unsupported`; the caller then recalculates the table in Emacs. |
| 7 | |
7 | |
| 8 | extension EmacsBuffer { |
8 | extension EmacsBuffer { |
| 9 | struct TableAnalysis { |
9 | struct TableAnalysis { |
| @@ -13,6 +13,14 @@ extension EmacsBuffer { |
| 13 | var types: [Bool] |
13 | var types: [Bool] |
| 14 | var ncol: Int |
14 | var ncol: Int |
| 15 | var begin: Int |
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 | static let tblfmPattern = "^[ \\t]*#\\+TBLFM: *(.*)" |
26 | static let tblfmPattern = "^[ \\t]*#\\+TBLFM: *(.*)" |
| @@ -26,13 +34,12 @@ extension EmacsBuffer { |
| 26 | point = begin |
34 | point = begin |
| 27 | var dlines = [0] |
35 | var dlines = [0] |
| 28 | var types: [Bool] = [] |
36 | var types: [Bool] = [] |
| |
37 | var lines: [String] = [] |
| 29 | var line = 0 |
38 | var line = 0 |
| 30 | while point < end || (point == end && false) { |
39 | while point < end || (point == end && false) { |
| 31 | let content = substring(lineStart(point), lineEnd(point)) |
40 | let content = substring(lineStart(point), lineEnd(point)) |
| 32 | guard content.range(of: "^[ \\t]*\\|", options: .regularExpression) != nil else { break } |
41 | guard content.range(of: "^[ \\t]*\\|", options: .regularExpression) != nil else { break } |
| 33 | if content.range(of: "^[ \\t]*\\| *[!$_^] *(\\||$)", options: .regularExpression) != nil { |
42 | lines.append(content) |
| 34 | throw Calc.Unsupported("named columns, parameters or fields") |
| |
| 35 | } |
| |
| 36 | let isHline = content.range(of: "^[ \\t]*\\|-", options: .regularExpression) != nil |
43 | let isHline = content.range(of: "^[ \\t]*\\|-", options: .regularExpression) != nil |
| 37 | types.append(isHline) |
44 | types.append(isHline) |
| 38 | if !isHline { dlines.append(line) } |
45 | if !isHline { dlines.append(line) } |
| @@ -45,10 +52,43 @@ extension EmacsBuffer { |
| 45 | forwardLine(dlines[1]) |
52 | forwardLine(dlines[1]) |
| 46 | let first = substring(lineStart(point), lineEnd(point)) |
53 | let first = substring(lineStart(point), lineEnd(point)) |
| 47 | let ncol = Self.splitFields(first, separator: "[ \\t]*\\|[ \\t]*").count |
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 | /// `org-split-string`: leading and trailing separators produce no empty strings. |
92 | /// `org-split-string`: leading and trailing separators produce no empty strings. |
| 53 | static func splitFields(_ s: String, separator: String) -> [String] { |
93 | static func splitFields(_ s: String, separator: String) -> [String] { |
| 54 | let ns = s as NSString |
94 | let ns = s as NSString |
| @@ -259,28 +299,180 @@ extension EmacsBuffer { |
| 259 | } |
299 | } |
| 260 | |
300 | |
| 261 | static let calcNumber = "^[-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?$" |
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. |
316 | /// `org-table-make-reference`. For Calc, a field that isn't a number, a date or `nan` |
| 264 | static func reference(_ value: TableRangeValue) throws -> String { |
317 | /// would be a symbol, which isn't supported. |
| 265 | func number(_ s: String) throws -> String { |
318 | static func makeReference(_ value: TableRangeValue, _ mode: ReferenceMode) throws -> String { |
| 266 | guard s.range(of: calcNumber, options: .regularExpression) != nil else { throw Calc.Unsupported("non-numeric field \(s)") } |
319 | func blank(_ s: String) -> Bool { !s.contains { !$0.isWhitespace } } |
| 267 | return s |
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 | switch value { |
335 | switch value { |
| 270 | case .field(let s): |
336 | case .field(let s): |
| 271 | return s.contains(where: { !$0.isWhitespace }) ? "(" + (try number(s)) + ")" : "(0)" |
337 | if mode.lisp != .none { return lisp(s) } |
| 272 | case .list(let items): |
338 | return blank(s) ? (empty == "0" ? "(0)" : "nan") : "(" + (try calc(s)) + ")" |
| 273 | return "[" + (try items.filter { $0.contains(where: { !$0.isWhitespace }) }.map(number)).joined(separator: ",") + "]" |
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. |
407 | /// `org-table-formula-substitute-names`: column names become `$N`; parameters, named |
| 278 | func tableEvalFormula(_ formula: String, analysis: TableAnalysis) throws { |
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 | var formula = formula |
468 | var formula = formula |
| 280 | var modes = Calc.Modes() |
469 | var modes = Calc.Modes() |
| |
470 | var mode = ReferenceMode() |
| |
471 | var duration: DurationFormat? |
| |
472 | var literal = false |
| 281 | var fmt: String? |
473 | var fmt: String? |
| 282 | if let semicolon = formula.lastIndex(of: ";") { |
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 | formula = String(formula[..<semicolon]) |
476 | formula = String(formula[..<semicolon]) |
| 285 | let numeric = try! NSRegularExpression(pattern: "([pnfse])(-?[0-9]+)") |
477 | let numeric = try! NSRegularExpression(pattern: "([pnfse])(-?[0-9]+)") |
| 286 | while let m = numeric.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
478 | while let m = numeric.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
| @@ -299,27 +491,32 @@ extension EmacsBuffer { |
| 299 | while let m = letters.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
491 | while let m = letters.firstMatch(in: flags, range: NSRange(location: 0, length: (flags as NSString).length)) { |
| 300 | let ns = flags as NSString |
492 | let ns = flags as NSString |
| 301 | switch ns.substring(with: m.range) { |
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 | case "D": modes.degrees = true |
500 | case "D": modes.degrees = true |
| 303 | case "R": modes.degrees = false |
501 | case "R": modes.degrees = false |
| 304 | default: throw Calc.Unsupported("format flag \(ns.substring(with: m.range))") |
502 | default: throw Calc.Unsupported("format flag \(ns.substring(with: m.range))") |
| 305 | } |
503 | } |
| 306 | flags = ns.replacingCharacters(in: m.range, with: "") |
504 | flags = ns.replacingCharacters(in: m.range, with: "") |
| 307 | } |
505 | } |
| 308 | if flags.contains(where: { !$0.isWhitespace }) { |
506 | if flags.contains(where: { !$0.isWhitespace }) { fmt = flags } |
| 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 | } |
| |
| 314 | } |
507 | } |
| 315 | if formula.hasPrefix("'(") { throw Calc.Unsupported("Lisp formula") } |
508 | if !substituted { formula = try Self.handleFirstLast(substituteNames(formula, analysis: analysis), analysis: analysis) } |
| 316 | formula = substituteConstants(formula) |
509 | var fields = Self.splitFields(substring(lineStart(point), lineEnd(point)).trimmingCharacters(in: .whitespaces), separator: " *\\| *") |
| 317 | if formula.range(of: "\\$[a-zA-Z_]", options: .regularExpression) != nil || formula.contains("remote(") |
510 | if duration != nil { fields = fields.map(Self.timeStringToSeconds) } |
| 318 | || formula.range(of: "[<\\[][0-9]{4}-", options: .regularExpression) != nil || formula.contains("&") { |
511 | if mode.numbers { fields = fields.map { $0.contains(where: { !$0.isWhitespace }) ? Self.numberText($0) : $0 } } |
| 319 | throw Calc.Unsupported("names, constants, remote references or dates") |
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 | let n0 = tableCurrentColumn() |
520 | let n0 = tableCurrentColumn() |
| 324 | let thisline = try saveExcursion { () -> Int in |
521 | let thisline = try saveExcursion { () -> Int in |
| 325 | let here = lineStart(point) |
522 | let here = lineStart(point) |
| @@ -334,10 +531,31 @@ extension EmacsBuffer { |
| 334 | var form = formula |
531 | var form = formula |
| 335 | form = form.replacingOccurrences(of: "@#", with: String(tableCurrentDataLine())) |
532 | form = form.replacingOccurrences(of: "@#", with: String(tableCurrentDataLine())) |
| 336 | form = form.replacingOccurrences(of: "$#", with: String(n0)) |
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 | let range = try! NSRegularExpression(pattern: Self.rangePattern) |
554 | let range = try! NSRegularExpression(pattern: Self.rangePattern) |
| 338 | while let m = range.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)), m.range.length > 1 { |
555 | while let m = range.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)), m.range.length > 1 { |
| 339 | let ns = form as NSString |
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 | form = ns.replacingCharacters(in: m.range, with: replacement) |
559 | form = ns.replacingCharacters(in: m.range, with: replacement) |
| 342 | } |
560 | } |
| 343 | let rowRange = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)\\.\\.\\$(([-+])?[0-9]+)") |
561 | let rowRange = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)\\.\\.\\$(([-+])?[0-9]+)") |
| @@ -346,7 +564,7 @@ extension EmacsBuffer { |
| 346 | let a = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
564 | let a = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
| 347 | let b = Int(ns.substring(with: m.range(at: 3)))! + (m.range(at: 4).location != NSNotFound ? n0 : 0) |
565 | let b = Int(ns.substring(with: m.range(at: 3)))! + (m.range(at: 4).location != NSNotFound ? n0 : 0) |
| 348 | guard a >= 1, b <= fields.count, a - 1 <= b else { throw Calc.Unsupported("range outside row") } |
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 | let single = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)") |
569 | let single = try! NSRegularExpression(pattern: "\\$(([-+])?[0-9]+)") |
| 352 | while let m = single.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)) { |
570 | while let m = single.firstMatch(in: form, range: NSRange(location: 0, length: (form as NSString).length)) { |
| @@ -354,37 +572,100 @@ extension EmacsBuffer { |
| 354 | let n = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
572 | let n = Int(ns.substring(with: m.range(at: 1)))! + (m.range(at: 2).location != NSNotFound ? n0 : 0) |
| 355 | let index = (n == 0 ? n0 : max(n, 1)) - 1 |
573 | let index = (n == 0 ? n0 : max(n, 1)) - 1 |
| 356 | guard index < fields.count else { throw UserError(message: "Invalid field specifier \"\(ns.substring(with: m.range))\"") } |
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 | if let fmt { |
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 | tableSetField(result) |
609 | tableSetField(result) |
| 364 | } |
610 | } |
| 365 | |
611 | |
| 366 | /// `org-table-formula-substitute-names` for constants: `$name` from `#+CONSTANTS`, or |
612 | /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`. |
| 367 | /// `$PROP_name` from the entry's property, in parentheses; unknown names become |
613 | func evalLisp(_ form: String, duration: DurationFormat?) throws -> String { |
| 368 | /// `#UNDEFINED_NAME`. |
614 | let lisp = Elisp() |
| 369 | func substituteConstants(_ formula: String) -> String { |
615 | Self.defineLookups(lisp) |
| 370 | guard formula.range(of: "\\$[a-zA-Z]", options: .regularExpression) != nil else { return formula } |
616 | let value: Sexp |
| 371 | var constants: [String: String] = [:] |
617 | do { |
| 372 | for m in (settings.setupKeywords + string).matches(of: #/(?mi)^[ \t]*#\+CONSTANTS:(.*)$/#) { |
618 | value = try lisp.eval(try lisp.eval(try LispReader.readFirst(form).sexp)) |
| 373 | for pair in m.1.split(whereSeparator: \.isWhitespace) { |
619 | } catch let unsupported as Elisp.Unsupported { |
| 374 | let parts = pair.split(separator: "=", maxSplits: 1) |
620 | throw Calc.Unsupported("Lisp \(unsupported.what)") |
| 375 | if parts.count == 2, constants[String(parts[0])] == nil { constants[String(parts[0])] = String(parts[1]) } |
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]*)") |
642 | lisp.define("org-lookup-first") { lisp, args in try matches(lisp, args).first ?? .nil } |
| 379 | var out = formula |
643 | lisp.define("org-lookup-last") { lisp, args in try matches(lisp, args).last ?? .nil } |
| 380 | while let m = regex.firstMatch(in: out, range: NSRange(location: 0, length: (out as NSString).length)) { |
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 | let ns = out as NSString |
657 | let ns = out as NSString |
| 382 | let name = ns.substring(with: m.range(at: 1)) |
658 | let stamp = ns.substring(with: m.range(at: 1)) |
| 383 | var value = constants[name] |
659 | let parts = stamp.firstMatch(of: /^([0-9]{4})-([0-9]{2})-([0-9]{2})/)! |
| 384 | if value == nil, name.hasPrefix("PROP_") { |
660 | let day = Days.absolute(year: Int(parts.1)!, month: Int(parts.2)!, day: Int(parts.3)!) |
| 385 | value = inheritedProperty(String(name.dropFirst(5)))?.value |
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 | return out |
670 | return out |
| 390 | } |
671 | } |
| @@ -429,7 +710,7 @@ extension EmacsBuffer { |
| 429 | var columnEquations: [(Int, String)] = [] |
710 | var columnEquations: [(Int, String)] = [] |
| 430 | var fieldEquations: [(String, String)] = [] |
711 | var fieldEquations: [(String, String)] = [] |
| 431 | for (oldLHS, rawRHS) in stored { |
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 | if oldLHS.range(of: "^@-?I+", options: .regularExpression) != nil { throw UserError(message: "Can't assign to hline relative reference") } |
714 | if oldLHS.range(of: "^@-?I+", options: .regularExpression) != nil { throw UserError(message: "Can't assign to hline relative reference") } |
| 434 | let lhs = try Self.handleFirstLast(oldLHS, analysis: analysis) |
715 | let lhs = try Self.handleFirstLast(oldLHS, analysis: analysis) |
| 435 | if lhs.range(of: "^\\$[0-9]+$", options: .regularExpression) != nil { |
716 | if lhs.range(of: "^\\$[0-9]+$", options: .regularExpression) != nil { |
| @@ -443,8 +724,13 @@ extension EmacsBuffer { |
| 443 | for (lhs, rhs) in fieldEquations { |
724 | for (lhs, rhs) in fieldEquations { |
| 444 | if let m = lhs.firstMatch(of: /^@([0-9]+)\$([0-9]+)$/) { |
725 | if let m = lhs.firstMatch(of: /^@([0-9]+)\$([0-9]+)$/) { |
| 445 | fields.append((Int(m.1)!, Int(m.2)!, rhs)) |
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 { |
727 | } else if lhs.range(of: "^[a-zA-Z][_a-zA-Z0-9]*$", options: .regularExpression) != nil { |
| 447 | throw Calc.Unsupported("relative or named field formula") |
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 | } else if let m = lhs.firstMatch(of: /^@([0-9]+)$/) { |
734 | } else if let m = lhs.firstMatch(of: /^@([0-9]+)$/) { |
| 449 | for c in 1...analysis.ncol { fields.append((Int(m.1)!, c, rhs)) } |
735 | for c in 1...analysis.ncol { fields.append((Int(m.1)!, c, rhs)) } |
| 450 | } else { |
736 | } else { |
| @@ -463,7 +749,11 @@ extension EmacsBuffer { |
| 463 | let endMarker = marker(end) |
749 | let endMarker = marker(end) |
| 464 | defer { release(endMarker) } |
750 | defer { release(endMarker) } |
| 465 | var beg: Int |
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 | beg = analysis.begin |
757 | beg = analysis.begin |
| 468 | point = beg |
758 | point = beg |
| 469 | if searchForward("^[ \\t]*\\|[^-]", bound: end, caseInsensitive: false) != nil, |
759 | if searchForward("^[ \\t]*\\|[^-]", bound: end, caseInsensitive: false) != nil, |
| @@ -493,7 +783,7 @@ extension EmacsBuffer { |
| 493 | point = beg |
783 | point = beg |
| 494 | let rowMarker = marker(beg) |
784 | let rowMarker = marker(beg) |
| 495 | defer { release(rowMarker) } |
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 | _ = match |
787 | _ = match |
| 498 | let rowStart = lineStart(point) |
788 | let rowStart = lineStart(point) |
| 499 | rowMarker.position = rowStart |
789 | rowMarker.position = rowStart |
| @@ -713,7 +1003,7 @@ extension EmacsBuffer { |
| 713 | let analysis = try tableAnalyze() |
1003 | let analysis = try tableAnalyze() |
| 714 | let formula = try tableGetFormula(equation, named: named, analysis: analysis) |
1004 | let formula = try tableGetFormula(equation, named: named, analysis: analysis) |
| 715 | do { |
1005 | do { |
| 716 | try tableEvalFormula(try Self.handleFirstLast(formula, analysis: analysis), analysis: analysis) |
1006 | try tableEvalFormula(formula, analysis: analysis, substituted: false) |
| 717 | } catch let error as Calc.Unsupported { |
1007 | } catch let error as Calc.Unsupported { |
| 718 | throw UserError(message: "The formula was stored; recalculating it needs Emacs: " + error.reason) |
1008 | throw UserError(message: "The formula was stored; recalculating it needs Emacs: " + error.reason) |
| 719 | } |
1009 | } |