Commit d14a973265
Verified · cmc
Layout: unified · split
Sources/OrgCore/Commands/Table.swift +76 −1
| @@ -285,12 +285,87 @@ extension EmacsBuffer { | |||
| 285 | point = min(lineEnd(point), start + 1) | 285 | point = min(lineEnd(point), start + 1) |
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | /// Display columns of the bars of the table line at `start`, and of the `+` in a rule; nil | ||
| 289 | /// unless the line has the form `org-table-align` gives it: a bar at the end, a space on | ||
| 290 | /// each side of every field, and only dashes between the bars of a rule. | ||
| 291 | func tableSeparatorColumns(_ start: Int) -> [Int]? { | ||
| 292 | let line = substring(start, lineEnd(start)) | ||
| 293 | guard line.hasSuffix("|") else { return nil } | ||
| 294 | let rule = line.drop { $0 == " " || $0 == "\t" }.hasPrefix("|-") | ||
| 295 | var columns: [Int] = [] | ||
| 296 | var column = 0 | ||
| 297 | var segment = "" | ||
| 298 | for c in line { | ||
| 299 | if c == "|" || rule && c == "+" { | ||
| 300 | if !columns.isEmpty { | ||
| 301 | if rule { | ||
| 302 | guard !segment.isEmpty, segment.allSatisfy({ $0 == "-" }) else { return nil } | ||
| 303 | } else { | ||
| 304 | guard segment.count >= 3, segment.first == " ", segment.last == " " else { return nil } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | column += cellWidth(segment) | ||
| 308 | columns.append(column) | ||
| 309 | column += 1 | ||
| 310 | segment = "" | ||
| 311 | } else { | ||
| 312 | segment.append(c) | ||
| 313 | } | ||
| 314 | } | ||
| 315 | return columns | ||
| 316 | } | ||
| 317 | |||
| 318 | /// Whether the table line at point is aligned and its bars line up with the line above | ||
| 319 | /// (below, on the first line): no field of it changed width since the table was aligned. | ||
| 320 | /// A field at point that lost characters but still ends in two spaces gets its width back | ||
| 321 | /// first, as `org-delete-char` keeps it, leaving `org-table-may-need-update` alone. | ||
| 322 | func tableLineLinesUp() -> Bool { | ||
| 323 | let start = lineStart(point) | ||
| 324 | func isTableLine(_ offset: Int) -> Bool { | ||
| 325 | substring(offset, lineEnd(offset)).drop { $0 == " " || $0 == "\t" }.hasPrefix("|") | ||
| 326 | } | ||
| 327 | let reference: Int | ||
| 328 | if start > 0, isTableLine(lineStart(start - 1)) { | ||
| 329 | reference = lineStart(start - 1) | ||
| 330 | } else if lineEnd(start) < length, isTableLine(lineEnd(start) + 1) { | ||
| 331 | reference = lineEnd(start) + 1 | ||
| 332 | } else { | ||
| 333 | return false | ||
| 334 | } | ||
| 335 | guard let mine = tableSeparatorColumns(start), let theirs = tableSeparatorColumns(reference), mine.count == theirs.count else { return false } | ||
| 336 | if mine == theirs { return true } | ||
| 337 | // The field at point lies between bars `column - 1` and `column`. | ||
| 338 | let column = tableCurrentColumn() | ||
| 339 | guard !atTableHline, column > 0, column < mine.count, mine[..<column] == theirs[..<column] else { return false } | ||
| 340 | let shift = mine[column] - theirs[column] | ||
| 341 | guard shift < 0, (column..<mine.count).allSatisfy({ mine[$0] - theirs[$0] == shift }) else { return false } | ||
| 342 | var bar = start | ||
| 343 | var bars = 0 | ||
| 344 | while bar < lineEnd(start) { | ||
| 345 | if character(at: bar) == 124 { | ||
| 346 | if bars == column { break } | ||
| 347 | bars += 1 | ||
| 348 | } | ||
| 349 | bar += 1 | ||
| 350 | } | ||
| 351 | guard character(at: bar - 1) == 32, character(at: bar - 2) == 32 else { return false } | ||
| 352 | saveExcursion { | ||
| 353 | point = bar | ||
| 354 | insert(String(repeating: " ", count: -shift)) | ||
| 355 | } | ||
| 356 | return true | ||
| 357 | } | ||
| 358 | |||
| 288 | /// `org-table-next-field`. | 359 | /// `org-table-next-field`. |
| 289 | func tableNextField() throws { | 360 | func tableNextField() throws { |
| 361 | let unchanged = (unchangedPrefix, unchangedSuffix) | ||
| 290 | try tableMaybeEvalFormula() | 362 | try tableMaybeEvalFormula() |
| 291 | try tableMaybeRecalculateLine() | 363 | try tableMaybeRecalculateLine() |
| 292 | moveIntoField() | 364 | moveIntoField() |
| 293 | tableAlign() | 365 | // `org-table-may-need-update`: Org realigns only after the buffer changed since the last |
| 366 | // alignment. Without that history, a line that still lines up stands for no change; a | ||
| 367 | // formula's result is a change. | ||
| 368 | if unchanged != (unchangedPrefix, unchangedSuffix) || !tableLineLinesUp() { tableAlign() } | ||
| 294 | let end = tableEnd() | 369 | let end = tableEnd() |
| 295 | if atTableHline { endOfLine() } | 370 | if atTableHline { endOfLine() } |
| 296 | func toNextBar() -> Bool { searchForward("\\|", bound: end, caseInsensitive: false) != nil } | 371 | func toNextBar() -> Bool { searchForward("\\|", bound: end, caseInsensitive: false) != nil } |
Tests/OrgCoreTests/TableTests.swift +27
| @@ -53,6 +53,33 @@ struct TableTests { | |||
| 53 | try compareWithEmacs(inputs, Self.commands, options: doomOptions) | 53 | try compareWithEmacs(inputs, Self.commands, options: doomOptions) |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | /// TAB realigns only after an edit (`org-table-may-need-update`), which here changes the width | ||
| 57 | /// of the field left behind. The last row is out of line, so a realign shows. Emacs starts | ||
| 58 | /// as if just aligned, makes the edit, then TABs; we TAB in the same text after the same edit. | ||
| 59 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | ||
| 60 | func nextFieldRealignsOnlyAfterAWidthChange() throws { | ||
| 61 | let table = "| a | b |\n|------+----|\n| cc | dd |\n| eeeeeeee | f |\n" | ||
| 62 | let dd = (table as NSString).range(of: "dd").upperBound | ||
| 63 | func edit(_ range: Range<Int>, _ replacement: String) -> String { | ||
| 64 | (table as NSString).replacingCharacters(in: NSRange(range), with: replacement) | ||
| 65 | } | ||
| 66 | let cases: [(label: String, form: String, caret: Int, text: String, after: Int)] = [ | ||
| 67 | ("no edit", "", 3, table, 3), | ||
| 68 | ("typed into spare space", "(let ((last-command-event ?x)) (org-self-insert-command 1))", 3, edit(3..<3, "x"), 4), | ||
| 69 | ("typed past the column's width", "(insert \"xxxxxxxxxx\")", 3, edit(3..<3, "xxxxxxxxxx"), 13), | ||
| 70 | ("deleted with spare space left", "(org-delete-backward-char 1)", 3, edit(2..<3, ""), 2), | ||
| 71 | ("deleted from a full field", "(org-delete-backward-char 1)", dd, edit(dd - 1..<dd, ""), dd - 1), | ||
| 72 | ] | ||
| 73 | let emacs = try EmacsOracle.run(cases.map { | ||
| 74 | EmacsOracle.Case(text: table, point: EmacsOracle.point($0.caret, in: table), form: "(progn (setq org-table-may-need-update nil) \($0.form) (org-table-next-field))") | ||
| 75 | }) | ||
| 76 | for (c, theirs) in zip(cases, emacs) { | ||
| 77 | let ours = runCommand(TableCommand(.nextField), c.text, caret: c.after) | ||
| 78 | let caret = EmacsOracle.offset(theirs.point, in: theirs.text) | ||
| 79 | #expect(ours.text == theirs.text && ours.caret == caret, "\(c.label): ours \(ours.text.debugDescription)@\(ours.caret) emacs \(theirs.text.debugDescription)@\(caret)") | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 56 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | 83 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| 57 | func createTableMatchesEmacs() throws { | 84 | func createTableMatchesEmacs() throws { |
| 58 | for (text, caret) in [("", 0), ("x\n", 1), (" \n", 2)] { | 85 | for (text, caret) in [("", 0), ("x\n", 1), (" \n", 2)] { |
docs/manual/guide/10-tables.org +7 −2
| @@ -43,7 +43,7 @@ With text selected, the command converts the selected lines to a table instead | |||
| 43 | 43 | ||
| 44 | | Key | Org command | Action | | 44 | | Key | Org command | Action | |
| 45 | |---------+----------------------------+----------------------------------------------------------------------------| | 45 | |---------+----------------------------+----------------------------------------------------------------------------| |
| 46 | | =TAB= | =org-table-next-field= | Aligns the table and moves to the next field, skipping rules. In the last field it adds a row. | | 46 | | =TAB= | =org-table-next-field= | Aligns the table if the row it leaves changed width, and moves to the next field, skipping rules. In the last field it adds a row. | |
| 47 | | =S-TAB= | =org-table-previous-field= | Aligns the table and moves to the previous field. | | 47 | | =S-TAB= | =org-table-previous-field= | Aligns the table and moves to the previous field. | |
| 48 | | =RET= | =org-table-next-row= | Aligns the table and moves down a row in the same column. Before a rule or at the end of the table it inserts a row. | | 48 | | =RET= | =org-table-next-row= | Aligns the table and moves down a row in the same column. Before a rule or at the end of the table it inserts a row. | |
| 49 | 49 | ||
| @@ -58,7 +58,12 @@ Widths count display columns, so wide characters and hidden link markup are meas | |||
| 58 | as they appear. | 58 | as they appear. |
| 59 | 59 | ||
| 60 | Alignment happens when you press =TAB=, =S-TAB= or =RET= in a table and after every | 60 | Alignment happens when you press =TAB=, =S-TAB= or =RET= in a table and after every |
| 61 | table command, not while you type. To align without moving, use Org ▸ Align Table: | 61 | table command, not while you type. Org's =TAB= aligns only when the buffer changed |
| 62 | since the last alignment; orgstar's aligns when the row it leaves no longer lines up | ||
| 63 | with the row above (below, in the first row), so a row you changed and left some other | ||
| 64 | way stays as it is until the next alignment. As in Org, deleting from a field that still | ||
| 65 | ends in two spaces doesn't count as a change: =TAB= pads that field back to its width. | ||
| 66 | To align without moving, use Org ▸ Align Table: | ||
| 62 | 67 | ||
| 63 | | Preset | Key | | 68 | | Preset | Key | |
| 64 | |--------+------------------------------| | 69 | |--------+------------------------------| |